(syms_of_xdisp) <nobreak-char-display>: Doc fix.
[emacs.git] / src / xdisp.c
blob5bb157cae73e3c641c741212b8ff4880cca6e6ff
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
63 expose_window (asynchronous) |
65 X expose events -----+
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
89 Direct operations.
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
110 Desired matrices.
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
148 Frame matrices.
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
169 #include <config.h>
170 #include <stdio.h>
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
203 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
207 #define INFINITY 10000000
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
215 extern int interrupt_input;
216 extern int command_loop_level;
218 extern Lisp_Object do_mouse_tracking;
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
248 Lisp_Object Qrisky_local_variable;
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
253 /* Functions called to fontify regions of text. */
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
265 int auto_raise_tool_bar_buttons_p;
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
269 int make_cursor_line_fully_visible_p;
271 /* Margin around tool bar buttons in pixels. */
273 Lisp_Object Vtool_bar_button_margin;
275 /* Thickness of shadow to draw around tool bar buttons. */
277 EMACS_INT tool_bar_button_relief;
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
282 int auto_resize_tool_bars_p;
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
288 int x_stretch_cursor_p;
290 /* Non-nil means don't actually do any redisplay. */
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
296 int inhibit_eval_during_redisplay;
298 /* Names of text properties relevant for redisplay. */
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
303 /* Symbols used in text property values. */
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
317 /* Non-nil means highlight trailing whitespace. */
319 Lisp_Object Vshow_trailing_whitespace;
321 /* Non-nil means escape non-break space and hyphens. */
323 Lisp_Object Vnobreak_char_display;
325 #ifdef HAVE_WINDOW_SYSTEM
326 extern Lisp_Object Voverflow_newline_into_fringe;
328 /* Test if overflow newline into fringe. Called with iterator IT
329 at or past right window margin, and with IT->current_x set. */
331 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
332 (!NILP (Voverflow_newline_into_fringe) \
333 && FRAME_WINDOW_P (it->f) \
334 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
335 && it->current_x == it->last_visible_x)
337 #endif /* HAVE_WINDOW_SYSTEM */
339 /* Non-nil means show the text cursor in void text areas
340 i.e. in blank areas after eol and eob. This used to be
341 the default in 21.3. */
343 Lisp_Object Vvoid_text_area_pointer;
345 /* Name of the face used to highlight trailing whitespace. */
347 Lisp_Object Qtrailing_whitespace;
349 /* Name and number of the face used to highlight escape glyphs. */
351 Lisp_Object Qescape_glyph;
353 /* Name and number of the face used to highlight non-breaking spaces. */
355 Lisp_Object Qnobreak_space;
357 /* The symbol `image' which is the car of the lists used to represent
358 images in Lisp. */
360 Lisp_Object Qimage;
362 /* The image map types. */
363 Lisp_Object QCmap, QCpointer;
364 Lisp_Object Qrect, Qcircle, Qpoly;
366 /* Non-zero means print newline to stdout before next mini-buffer
367 message. */
369 int noninteractive_need_newline;
371 /* Non-zero means print newline to message log before next message. */
373 static int message_log_need_newline;
375 /* Three markers that message_dolog uses.
376 It could allocate them itself, but that causes trouble
377 in handling memory-full errors. */
378 static Lisp_Object message_dolog_marker1;
379 static Lisp_Object message_dolog_marker2;
380 static Lisp_Object message_dolog_marker3;
382 /* The buffer position of the first character appearing entirely or
383 partially on the line of the selected window which contains the
384 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
385 redisplay optimization in redisplay_internal. */
387 static struct text_pos this_line_start_pos;
389 /* Number of characters past the end of the line above, including the
390 terminating newline. */
392 static struct text_pos this_line_end_pos;
394 /* The vertical positions and the height of this line. */
396 static int this_line_vpos;
397 static int this_line_y;
398 static int this_line_pixel_height;
400 /* X position at which this display line starts. Usually zero;
401 negative if first character is partially visible. */
403 static int this_line_start_x;
405 /* Buffer that this_line_.* variables are referring to. */
407 static struct buffer *this_line_buffer;
409 /* Nonzero means truncate lines in all windows less wide than the
410 frame. */
412 int truncate_partial_width_windows;
414 /* A flag to control how to display unibyte 8-bit character. */
416 int unibyte_display_via_language_environment;
418 /* Nonzero means we have more than one non-mini-buffer-only frame.
419 Not guaranteed to be accurate except while parsing
420 frame-title-format. */
422 int multiple_frames;
424 Lisp_Object Vglobal_mode_string;
427 /* List of variables (symbols) which hold markers for overlay arrows.
428 The symbols on this list are examined during redisplay to determine
429 where to display overlay arrows. */
431 Lisp_Object Voverlay_arrow_variable_list;
433 /* Marker for where to display an arrow on top of the buffer text. */
435 Lisp_Object Voverlay_arrow_position;
437 /* String to display for the arrow. Only used on terminal frames. */
439 Lisp_Object Voverlay_arrow_string;
441 /* Values of those variables at last redisplay are stored as
442 properties on `overlay-arrow-position' symbol. However, if
443 Voverlay_arrow_position is a marker, last-arrow-position is its
444 numerical position. */
446 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
448 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
449 properties on a symbol in overlay-arrow-variable-list. */
451 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
453 /* Like mode-line-format, but for the title bar on a visible frame. */
455 Lisp_Object Vframe_title_format;
457 /* Like mode-line-format, but for the title bar on an iconified frame. */
459 Lisp_Object Vicon_title_format;
461 /* List of functions to call when a window's size changes. These
462 functions get one arg, a frame on which one or more windows' sizes
463 have changed. */
465 static Lisp_Object Vwindow_size_change_functions;
467 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
469 /* Nonzero if an overlay arrow has been displayed in this window. */
471 static int overlay_arrow_seen;
473 /* Nonzero means highlight the region even in nonselected windows. */
475 int highlight_nonselected_windows;
477 /* If cursor motion alone moves point off frame, try scrolling this
478 many lines up or down if that will bring it back. */
480 static EMACS_INT scroll_step;
482 /* Nonzero means scroll just far enough to bring point back on the
483 screen, when appropriate. */
485 static EMACS_INT scroll_conservatively;
487 /* Recenter the window whenever point gets within this many lines of
488 the top or bottom of the window. This value is translated into a
489 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
490 that there is really a fixed pixel height scroll margin. */
492 EMACS_INT scroll_margin;
494 /* Number of windows showing the buffer of the selected window (or
495 another buffer with the same base buffer). keyboard.c refers to
496 this. */
498 int buffer_shared;
500 /* Vector containing glyphs for an ellipsis `...'. */
502 static Lisp_Object default_invis_vector[3];
504 /* Zero means display the mode-line/header-line/menu-bar in the default face
505 (this slightly odd definition is for compatibility with previous versions
506 of emacs), non-zero means display them using their respective faces.
508 This variable is deprecated. */
510 int mode_line_inverse_video;
512 /* Prompt to display in front of the mini-buffer contents. */
514 Lisp_Object minibuf_prompt;
516 /* Width of current mini-buffer prompt. Only set after display_line
517 of the line that contains the prompt. */
519 int minibuf_prompt_width;
521 /* This is the window where the echo area message was displayed. It
522 is always a mini-buffer window, but it may not be the same window
523 currently active as a mini-buffer. */
525 Lisp_Object echo_area_window;
527 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
528 pushes the current message and the value of
529 message_enable_multibyte on the stack, the function restore_message
530 pops the stack and displays MESSAGE again. */
532 Lisp_Object Vmessage_stack;
534 /* Nonzero means multibyte characters were enabled when the echo area
535 message was specified. */
537 int message_enable_multibyte;
539 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541 int update_mode_lines;
543 /* Nonzero if window sizes or contents have changed since last
544 redisplay that finished. */
546 int windows_or_buffers_changed;
548 /* Nonzero means a frame's cursor type has been changed. */
550 int cursor_type_changed;
552 /* Nonzero after display_mode_line if %l was used and it displayed a
553 line number. */
555 int line_number_displayed;
557 /* Maximum buffer size for which to display line numbers. */
559 Lisp_Object Vline_number_display_limit;
561 /* Line width to consider when repositioning for line number display. */
563 static EMACS_INT line_number_display_limit_width;
565 /* Number of lines to keep in the message log buffer. t means
566 infinite. nil means don't log at all. */
568 Lisp_Object Vmessage_log_max;
570 /* The name of the *Messages* buffer, a string. */
572 static Lisp_Object Vmessages_buffer_name;
574 /* Index 0 is the buffer that holds the current (desired) echo area message,
575 or nil if none is desired right now.
577 Index 1 is the buffer that holds the previously displayed echo area message,
578 or nil to indicate no message. This is normally what's on the screen now.
580 These two can point to the same buffer. That happens when the last
581 message output by the user (or made by echoing) has been displayed. */
583 Lisp_Object echo_area_buffer[2];
585 /* Permanent pointers to the two buffers that are used for echo area
586 purposes. Once the two buffers are made, and their pointers are
587 placed here, these two slots remain unchanged unless those buffers
588 need to be created afresh. */
590 static Lisp_Object echo_buffer[2];
592 /* A vector saved used in with_area_buffer to reduce consing. */
594 static Lisp_Object Vwith_echo_area_save_vector;
596 /* Non-zero means display_echo_area should display the last echo area
597 message again. Set by redisplay_preserve_echo_area. */
599 static int display_last_displayed_message_p;
601 /* Nonzero if echo area is being used by print; zero if being used by
602 message. */
604 int message_buf_print;
606 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
608 Lisp_Object Qinhibit_menubar_update;
609 int inhibit_menubar_update;
611 /* Maximum height for resizing mini-windows. Either a float
612 specifying a fraction of the available height, or an integer
613 specifying a number of lines. */
615 Lisp_Object Vmax_mini_window_height;
617 /* Non-zero means messages should be displayed with truncated
618 lines instead of being continued. */
620 int message_truncate_lines;
621 Lisp_Object Qmessage_truncate_lines;
623 /* Set to 1 in clear_message to make redisplay_internal aware
624 of an emptied echo area. */
626 static int message_cleared_p;
628 /* How to blink the default frame cursor off. */
629 Lisp_Object Vblink_cursor_alist;
631 /* A scratch glyph row with contents used for generating truncation
632 glyphs. Also used in direct_output_for_insert. */
634 #define MAX_SCRATCH_GLYPHS 100
635 struct glyph_row scratch_glyph_row;
636 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
638 /* Ascent and height of the last line processed by move_it_to. */
640 static int last_max_ascent, last_height;
642 /* Non-zero if there's a help-echo in the echo area. */
644 int help_echo_showing_p;
646 /* If >= 0, computed, exact values of mode-line and header-line height
647 to use in the macros CURRENT_MODE_LINE_HEIGHT and
648 CURRENT_HEADER_LINE_HEIGHT. */
650 int current_mode_line_height, current_header_line_height;
652 /* The maximum distance to look ahead for text properties. Values
653 that are too small let us call compute_char_face and similar
654 functions too often which is expensive. Values that are too large
655 let us call compute_char_face and alike too often because we
656 might not be interested in text properties that far away. */
658 #define TEXT_PROP_DISTANCE_LIMIT 100
660 #if GLYPH_DEBUG
662 /* Variables to turn off display optimizations from Lisp. */
664 int inhibit_try_window_id, inhibit_try_window_reusing;
665 int inhibit_try_cursor_movement;
667 /* Non-zero means print traces of redisplay if compiled with
668 GLYPH_DEBUG != 0. */
670 int trace_redisplay_p;
672 #endif /* GLYPH_DEBUG */
674 #ifdef DEBUG_TRACE_MOVE
675 /* Non-zero means trace with TRACE_MOVE to stderr. */
676 int trace_move;
678 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
679 #else
680 #define TRACE_MOVE(x) (void) 0
681 #endif
683 /* Non-zero means automatically scroll windows horizontally to make
684 point visible. */
686 int automatic_hscrolling_p;
688 /* How close to the margin can point get before the window is scrolled
689 horizontally. */
690 EMACS_INT hscroll_margin;
692 /* How much to scroll horizontally when point is inside the above margin. */
693 Lisp_Object Vhscroll_step;
695 /* The variable `resize-mini-windows'. If nil, don't resize
696 mini-windows. If t, always resize them to fit the text they
697 display. If `grow-only', let mini-windows grow only until they
698 become empty. */
700 Lisp_Object Vresize_mini_windows;
702 /* Buffer being redisplayed -- for redisplay_window_error. */
704 struct buffer *displayed_buffer;
706 /* Value returned from text property handlers (see below). */
708 enum prop_handled
710 HANDLED_NORMALLY,
711 HANDLED_RECOMPUTE_PROPS,
712 HANDLED_OVERLAY_STRING_CONSUMED,
713 HANDLED_RETURN
716 /* A description of text properties that redisplay is interested
717 in. */
719 struct props
721 /* The name of the property. */
722 Lisp_Object *name;
724 /* A unique index for the property. */
725 enum prop_idx idx;
727 /* A handler function called to set up iterator IT from the property
728 at IT's current position. Value is used to steer handle_stop. */
729 enum prop_handled (*handler) P_ ((struct it *it));
732 static enum prop_handled handle_face_prop P_ ((struct it *));
733 static enum prop_handled handle_invisible_prop P_ ((struct it *));
734 static enum prop_handled handle_display_prop P_ ((struct it *));
735 static enum prop_handled handle_composition_prop P_ ((struct it *));
736 static enum prop_handled handle_overlay_change P_ ((struct it *));
737 static enum prop_handled handle_fontified_prop P_ ((struct it *));
739 /* Properties handled by iterators. */
741 static struct props it_props[] =
743 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
744 /* Handle `face' before `display' because some sub-properties of
745 `display' need to know the face. */
746 {&Qface, FACE_PROP_IDX, handle_face_prop},
747 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
748 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
749 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
750 {NULL, 0, NULL}
753 /* Value is the position described by X. If X is a marker, value is
754 the marker_position of X. Otherwise, value is X. */
756 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
758 /* Enumeration returned by some move_it_.* functions internally. */
760 enum move_it_result
762 /* Not used. Undefined value. */
763 MOVE_UNDEFINED,
765 /* Move ended at the requested buffer position or ZV. */
766 MOVE_POS_MATCH_OR_ZV,
768 /* Move ended at the requested X pixel position. */
769 MOVE_X_REACHED,
771 /* Move within a line ended at the end of a line that must be
772 continued. */
773 MOVE_LINE_CONTINUED,
775 /* Move within a line ended at the end of a line that would
776 be displayed truncated. */
777 MOVE_LINE_TRUNCATED,
779 /* Move within a line ended at a line end. */
780 MOVE_NEWLINE_OR_CR
783 /* This counter is used to clear the face cache every once in a while
784 in redisplay_internal. It is incremented for each redisplay.
785 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
786 cleared. */
788 #define CLEAR_FACE_CACHE_COUNT 500
789 static int clear_face_cache_count;
791 /* Similarly for the image cache. */
793 #ifdef HAVE_WINDOW_SYSTEM
794 #define CLEAR_IMAGE_CACHE_COUNT 101
795 static int clear_image_cache_count;
796 #endif
798 /* Record the previous terminal frame we displayed. */
800 static struct frame *previous_terminal_frame;
802 /* Non-zero while redisplay_internal is in progress. */
804 int redisplaying_p;
806 /* Non-zero means don't free realized faces. Bound while freeing
807 realized faces is dangerous because glyph matrices might still
808 reference them. */
810 int inhibit_free_realized_faces;
811 Lisp_Object Qinhibit_free_realized_faces;
813 /* If a string, XTread_socket generates an event to display that string.
814 (The display is done in read_char.) */
816 Lisp_Object help_echo_string;
817 Lisp_Object help_echo_window;
818 Lisp_Object help_echo_object;
819 int help_echo_pos;
821 /* Temporary variable for XTread_socket. */
823 Lisp_Object previous_help_echo_string;
825 /* Null glyph slice */
827 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
830 /* Function prototypes. */
832 static void setup_for_ellipsis P_ ((struct it *, int));
833 static void mark_window_display_accurate_1 P_ ((struct window *, int));
834 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
835 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
837 static int redisplay_mode_lines P_ ((Lisp_Object, int));
838 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
840 #if 0
841 static int invisible_text_between_p P_ ((struct it *, int, int));
842 #endif
844 static void pint2str P_ ((char *, int, int));
845 static void pint2hrstr P_ ((char *, int, int));
846 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
847 struct text_pos));
848 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
849 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
850 static void store_mode_line_noprop_char P_ ((char));
851 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
852 static void x_consider_frame_title P_ ((Lisp_Object));
853 static void handle_stop P_ ((struct it *));
854 static int tool_bar_lines_needed P_ ((struct frame *));
855 static int single_display_spec_intangible_p P_ ((Lisp_Object));
856 static void ensure_echo_area_buffers P_ ((void));
857 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
858 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
859 static int with_echo_area_buffer P_ ((struct window *, int,
860 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
861 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
862 static void clear_garbaged_frames P_ ((void));
863 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
864 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int display_echo_area P_ ((struct window *));
867 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
868 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
870 static int string_char_and_length P_ ((const unsigned char *, int, int *));
871 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
872 struct text_pos));
873 static int compute_window_start_on_continuation_line P_ ((struct window *));
874 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
875 static void insert_left_trunc_glyphs P_ ((struct it *));
876 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
877 Lisp_Object));
878 static void extend_face_to_end_of_line P_ ((struct it *));
879 static int append_space_for_newline P_ ((struct it *, int));
880 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
881 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
882 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
883 static int trailing_whitespace_p P_ ((int));
884 static int message_log_check_duplicate P_ ((int, int, int, int));
885 static void push_it P_ ((struct it *));
886 static void pop_it P_ ((struct it *));
887 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
888 static void select_frame_for_redisplay P_ ((Lisp_Object));
889 static void redisplay_internal P_ ((int));
890 static int echo_area_display P_ ((int));
891 static void redisplay_windows P_ ((Lisp_Object));
892 static void redisplay_window P_ ((Lisp_Object, int));
893 static Lisp_Object redisplay_window_error ();
894 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
895 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
896 static void update_menu_bar P_ ((struct frame *, int));
897 static int try_window_reusing_current_matrix P_ ((struct window *));
898 static int try_window_id P_ ((struct window *));
899 static int display_line P_ ((struct it *));
900 static int display_mode_lines P_ ((struct window *));
901 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
902 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
903 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
904 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
905 static void display_menu_bar P_ ((struct window *));
906 static int display_count_lines P_ ((int, int, int, int, int *));
907 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
908 int, int, struct it *, int, int, int, int));
909 static void compute_line_metrics P_ ((struct it *));
910 static void run_redisplay_end_trigger_hook P_ ((struct it *));
911 static int get_overlay_strings P_ ((struct it *, int));
912 static void next_overlay_string P_ ((struct it *));
913 static void reseat P_ ((struct it *, struct text_pos, int));
914 static void reseat_1 P_ ((struct it *, struct text_pos, int));
915 static void back_to_previous_visible_line_start P_ ((struct it *));
916 void reseat_at_previous_visible_line_start P_ ((struct it *));
917 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
918 static int next_element_from_ellipsis P_ ((struct it *));
919 static int next_element_from_display_vector P_ ((struct it *));
920 static int next_element_from_string P_ ((struct it *));
921 static int next_element_from_c_string P_ ((struct it *));
922 static int next_element_from_buffer P_ ((struct it *));
923 static int next_element_from_composition P_ ((struct it *));
924 static int next_element_from_image P_ ((struct it *));
925 static int next_element_from_stretch P_ ((struct it *));
926 static void load_overlay_strings P_ ((struct it *, int));
927 static int init_from_display_pos P_ ((struct it *, struct window *,
928 struct display_pos *));
929 static void reseat_to_string P_ ((struct it *, unsigned char *,
930 Lisp_Object, int, int, int, int));
931 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
932 int, int, int));
933 void move_it_vertically_backward P_ ((struct it *, int));
934 static void init_to_row_start P_ ((struct it *, struct window *,
935 struct glyph_row *));
936 static int init_to_row_end P_ ((struct it *, struct window *,
937 struct glyph_row *));
938 static void back_to_previous_line_start P_ ((struct it *));
939 static int forward_to_next_line_start P_ ((struct it *, int *));
940 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
941 Lisp_Object, int));
942 static struct text_pos string_pos P_ ((int, Lisp_Object));
943 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
944 static int number_of_chars P_ ((unsigned char *, int));
945 static void compute_stop_pos P_ ((struct it *));
946 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
947 Lisp_Object));
948 static int face_before_or_after_it_pos P_ ((struct it *, int));
949 static int next_overlay_change P_ ((int));
950 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
951 Lisp_Object, struct text_pos *,
952 int));
953 static int underlying_face_id P_ ((struct it *));
954 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
955 struct window *));
957 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
958 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
960 #ifdef HAVE_WINDOW_SYSTEM
962 static void update_tool_bar P_ ((struct frame *, int));
963 static void build_desired_tool_bar_string P_ ((struct frame *f));
964 static int redisplay_tool_bar P_ ((struct frame *));
965 static void display_tool_bar_line P_ ((struct it *));
966 static void notice_overwritten_cursor P_ ((struct window *,
967 enum glyph_row_area,
968 int, int, int, int));
972 #endif /* HAVE_WINDOW_SYSTEM */
975 /***********************************************************************
976 Window display dimensions
977 ***********************************************************************/
979 /* Return the bottom boundary y-position for text lines in window W.
980 This is the first y position at which a line cannot start.
981 It is relative to the top of the window.
983 This is the height of W minus the height of a mode line, if any. */
985 INLINE int
986 window_text_bottom_y (w)
987 struct window *w;
989 int height = WINDOW_TOTAL_HEIGHT (w);
991 if (WINDOW_WANTS_MODELINE_P (w))
992 height -= CURRENT_MODE_LINE_HEIGHT (w);
993 return height;
996 /* Return the pixel width of display area AREA of window W. AREA < 0
997 means return the total width of W, not including fringes to
998 the left and right of the window. */
1000 INLINE int
1001 window_box_width (w, area)
1002 struct window *w;
1003 int area;
1005 int cols = XFASTINT (w->total_cols);
1006 int pixels = 0;
1008 if (!w->pseudo_window_p)
1010 cols -= WINDOW_SCROLL_BAR_COLS (w);
1012 if (area == TEXT_AREA)
1014 if (INTEGERP (w->left_margin_cols))
1015 cols -= XFASTINT (w->left_margin_cols);
1016 if (INTEGERP (w->right_margin_cols))
1017 cols -= XFASTINT (w->right_margin_cols);
1018 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1020 else if (area == LEFT_MARGIN_AREA)
1022 cols = (INTEGERP (w->left_margin_cols)
1023 ? XFASTINT (w->left_margin_cols) : 0);
1024 pixels = 0;
1026 else if (area == RIGHT_MARGIN_AREA)
1028 cols = (INTEGERP (w->right_margin_cols)
1029 ? XFASTINT (w->right_margin_cols) : 0);
1030 pixels = 0;
1034 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1038 /* Return the pixel height of the display area of window W, not
1039 including mode lines of W, if any. */
1041 INLINE int
1042 window_box_height (w)
1043 struct window *w;
1045 struct frame *f = XFRAME (w->frame);
1046 int height = WINDOW_TOTAL_HEIGHT (w);
1048 xassert (height >= 0);
1050 /* Note: the code below that determines the mode-line/header-line
1051 height is essentially the same as that contained in the macro
1052 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1053 the appropriate glyph row has its `mode_line_p' flag set,
1054 and if it doesn't, uses estimate_mode_line_height instead. */
1056 if (WINDOW_WANTS_MODELINE_P (w))
1058 struct glyph_row *ml_row
1059 = (w->current_matrix && w->current_matrix->rows
1060 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1061 : 0);
1062 if (ml_row && ml_row->mode_line_p)
1063 height -= ml_row->height;
1064 else
1065 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1068 if (WINDOW_WANTS_HEADER_LINE_P (w))
1070 struct glyph_row *hl_row
1071 = (w->current_matrix && w->current_matrix->rows
1072 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1073 : 0);
1074 if (hl_row && hl_row->mode_line_p)
1075 height -= hl_row->height;
1076 else
1077 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1080 /* With a very small font and a mode-line that's taller than
1081 default, we might end up with a negative height. */
1082 return max (0, height);
1085 /* Return the window-relative coordinate of the left edge of display
1086 area AREA of window W. AREA < 0 means return the left edge of the
1087 whole window, to the right of the left fringe of W. */
1089 INLINE int
1090 window_box_left_offset (w, area)
1091 struct window *w;
1092 int area;
1094 int x;
1096 if (w->pseudo_window_p)
1097 return 0;
1099 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1101 if (area == TEXT_AREA)
1102 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1103 + window_box_width (w, LEFT_MARGIN_AREA));
1104 else if (area == RIGHT_MARGIN_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA)
1107 + window_box_width (w, TEXT_AREA)
1108 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1110 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1111 else if (area == LEFT_MARGIN_AREA
1112 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1113 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1115 return x;
1119 /* Return the window-relative coordinate of the right edge of display
1120 area AREA of window W. AREA < 0 means return the left edge of the
1121 whole window, to the left of the right fringe of W. */
1123 INLINE int
1124 window_box_right_offset (w, area)
1125 struct window *w;
1126 int area;
1128 return window_box_left_offset (w, area) + window_box_width (w, area);
1131 /* Return the frame-relative coordinate of the left edge of display
1132 area AREA of window W. AREA < 0 means return the left edge of the
1133 whole window, to the right of the left fringe of W. */
1135 INLINE int
1136 window_box_left (w, area)
1137 struct window *w;
1138 int area;
1140 struct frame *f = XFRAME (w->frame);
1141 int x;
1143 if (w->pseudo_window_p)
1144 return FRAME_INTERNAL_BORDER_WIDTH (f);
1146 x = (WINDOW_LEFT_EDGE_X (w)
1147 + window_box_left_offset (w, area));
1149 return x;
1153 /* Return the frame-relative coordinate of the right edge of display
1154 area AREA of window W. AREA < 0 means return the left edge of the
1155 whole window, to the left of the right fringe of W. */
1157 INLINE int
1158 window_box_right (w, area)
1159 struct window *w;
1160 int area;
1162 return window_box_left (w, area) + window_box_width (w, area);
1165 /* Get the bounding box of the display area AREA of window W, without
1166 mode lines, in frame-relative coordinates. AREA < 0 means the
1167 whole window, not including the left and right fringes of
1168 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1169 coordinates of the upper-left corner of the box. Return in
1170 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1172 INLINE void
1173 window_box (w, area, box_x, box_y, box_width, box_height)
1174 struct window *w;
1175 int area;
1176 int *box_x, *box_y, *box_width, *box_height;
1178 if (box_width)
1179 *box_width = window_box_width (w, area);
1180 if (box_height)
1181 *box_height = window_box_height (w);
1182 if (box_x)
1183 *box_x = window_box_left (w, area);
1184 if (box_y)
1186 *box_y = WINDOW_TOP_EDGE_Y (w);
1187 if (WINDOW_WANTS_HEADER_LINE_P (w))
1188 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1193 /* Get the bounding box of the display area AREA of window W, without
1194 mode lines. AREA < 0 means the whole window, not including the
1195 left and right fringe of the window. Return in *TOP_LEFT_X
1196 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1197 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1198 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1199 box. */
1201 INLINE void
1202 window_box_edges (w, area, top_left_x, top_left_y,
1203 bottom_right_x, bottom_right_y)
1204 struct window *w;
1205 int area;
1206 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1208 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1209 bottom_right_y);
1210 *bottom_right_x += *top_left_x;
1211 *bottom_right_y += *top_left_y;
1216 /***********************************************************************
1217 Utilities
1218 ***********************************************************************/
1220 /* Return the bottom y-position of the line the iterator IT is in.
1221 This can modify IT's settings. */
1224 line_bottom_y (it)
1225 struct it *it;
1227 int line_height = it->max_ascent + it->max_descent;
1228 int line_top_y = it->current_y;
1230 if (line_height == 0)
1232 if (last_height)
1233 line_height = last_height;
1234 else if (IT_CHARPOS (*it) < ZV)
1236 move_it_by_lines (it, 1, 1);
1237 line_height = (it->max_ascent || it->max_descent
1238 ? it->max_ascent + it->max_descent
1239 : last_height);
1241 else
1243 struct glyph_row *row = it->glyph_row;
1245 /* Use the default character height. */
1246 it->glyph_row = NULL;
1247 it->what = IT_CHARACTER;
1248 it->c = ' ';
1249 it->len = 1;
1250 PRODUCE_GLYPHS (it);
1251 line_height = it->ascent + it->descent;
1252 it->glyph_row = row;
1256 return line_top_y + line_height;
1260 /* Return 1 if position CHARPOS is visible in window W.
1261 If visible, set *X and *Y to pixel coordinates of top left corner.
1262 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1263 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1264 and header-lines heights. */
1267 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1268 struct window *w;
1269 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1271 struct it it;
1272 struct text_pos top;
1273 int visible_p = 0;
1274 struct buffer *old_buffer = NULL;
1276 if (noninteractive)
1277 return visible_p;
1279 if (XBUFFER (w->buffer) != current_buffer)
1281 old_buffer = current_buffer;
1282 set_buffer_internal_1 (XBUFFER (w->buffer));
1285 SET_TEXT_POS_FROM_MARKER (top, w->start);
1287 /* Compute exact mode line heights, if requested. */
1288 if (exact_mode_line_heights_p)
1290 if (WINDOW_WANTS_MODELINE_P (w))
1291 current_mode_line_height
1292 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1293 current_buffer->mode_line_format);
1295 if (WINDOW_WANTS_HEADER_LINE_P (w))
1296 current_header_line_height
1297 = display_mode_line (w, HEADER_LINE_FACE_ID,
1298 current_buffer->header_line_format);
1301 start_display (&it, w, top);
1302 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1303 MOVE_TO_POS | MOVE_TO_Y);
1305 /* Note that we may overshoot because of invisible text. */
1306 if (IT_CHARPOS (it) >= charpos)
1308 int top_x = it.current_x;
1309 int top_y = it.current_y;
1310 int bottom_y = (last_height = 0, line_bottom_y (&it));
1311 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1313 if (top_y < window_top_y)
1314 visible_p = bottom_y > window_top_y;
1315 else if (top_y < it.last_visible_y)
1316 visible_p = 1;
1317 if (visible_p)
1319 *x = top_x;
1320 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1321 *rtop = max (0, window_top_y - top_y);
1322 *rbot = max (0, bottom_y - it.last_visible_y);
1325 else
1327 struct it it2;
1329 it2 = it;
1330 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1331 move_it_by_lines (&it, 1, 0);
1332 if (charpos < IT_CHARPOS (it))
1334 visible_p = 1;
1335 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1336 *x = it2.current_x;
1337 *y = it2.current_y + it2.max_ascent - it2.ascent;
1338 *rtop = max (0, -it2.current_y);
1339 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1340 - it.last_visible_y));
1344 if (old_buffer)
1345 set_buffer_internal_1 (old_buffer);
1347 current_header_line_height = current_mode_line_height = -1;
1349 return visible_p;
1353 /* Return the next character from STR which is MAXLEN bytes long.
1354 Return in *LEN the length of the character. This is like
1355 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1356 we find one, we return a `?', but with the length of the invalid
1357 character. */
1359 static INLINE int
1360 string_char_and_length (str, maxlen, len)
1361 const unsigned char *str;
1362 int maxlen, *len;
1364 int c;
1366 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1367 if (!CHAR_VALID_P (c, 1))
1368 /* We may not change the length here because other places in Emacs
1369 don't use this function, i.e. they silently accept invalid
1370 characters. */
1371 c = '?';
1373 return c;
1378 /* Given a position POS containing a valid character and byte position
1379 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1381 static struct text_pos
1382 string_pos_nchars_ahead (pos, string, nchars)
1383 struct text_pos pos;
1384 Lisp_Object string;
1385 int nchars;
1387 xassert (STRINGP (string) && nchars >= 0);
1389 if (STRING_MULTIBYTE (string))
1391 int rest = SBYTES (string) - BYTEPOS (pos);
1392 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1393 int len;
1395 while (nchars--)
1397 string_char_and_length (p, rest, &len);
1398 p += len, rest -= len;
1399 xassert (rest >= 0);
1400 CHARPOS (pos) += 1;
1401 BYTEPOS (pos) += len;
1404 else
1405 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1407 return pos;
1411 /* Value is the text position, i.e. character and byte position,
1412 for character position CHARPOS in STRING. */
1414 static INLINE struct text_pos
1415 string_pos (charpos, string)
1416 int charpos;
1417 Lisp_Object string;
1419 struct text_pos pos;
1420 xassert (STRINGP (string));
1421 xassert (charpos >= 0);
1422 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1423 return pos;
1427 /* Value is a text position, i.e. character and byte position, for
1428 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1429 means recognize multibyte characters. */
1431 static struct text_pos
1432 c_string_pos (charpos, s, multibyte_p)
1433 int charpos;
1434 unsigned char *s;
1435 int multibyte_p;
1437 struct text_pos pos;
1439 xassert (s != NULL);
1440 xassert (charpos >= 0);
1442 if (multibyte_p)
1444 int rest = strlen (s), len;
1446 SET_TEXT_POS (pos, 0, 0);
1447 while (charpos--)
1449 string_char_and_length (s, rest, &len);
1450 s += len, rest -= len;
1451 xassert (rest >= 0);
1452 CHARPOS (pos) += 1;
1453 BYTEPOS (pos) += len;
1456 else
1457 SET_TEXT_POS (pos, charpos, charpos);
1459 return pos;
1463 /* Value is the number of characters in C string S. MULTIBYTE_P
1464 non-zero means recognize multibyte characters. */
1466 static int
1467 number_of_chars (s, multibyte_p)
1468 unsigned char *s;
1469 int multibyte_p;
1471 int nchars;
1473 if (multibyte_p)
1475 int rest = strlen (s), len;
1476 unsigned char *p = (unsigned char *) s;
1478 for (nchars = 0; rest > 0; ++nchars)
1480 string_char_and_length (p, rest, &len);
1481 rest -= len, p += len;
1484 else
1485 nchars = strlen (s);
1487 return nchars;
1491 /* Compute byte position NEWPOS->bytepos corresponding to
1492 NEWPOS->charpos. POS is a known position in string STRING.
1493 NEWPOS->charpos must be >= POS.charpos. */
1495 static void
1496 compute_string_pos (newpos, pos, string)
1497 struct text_pos *newpos, pos;
1498 Lisp_Object string;
1500 xassert (STRINGP (string));
1501 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1503 if (STRING_MULTIBYTE (string))
1504 *newpos = string_pos_nchars_ahead (pos, string,
1505 CHARPOS (*newpos) - CHARPOS (pos));
1506 else
1507 BYTEPOS (*newpos) = CHARPOS (*newpos);
1510 /* EXPORT:
1511 Return an estimation of the pixel height of mode or top lines on
1512 frame F. FACE_ID specifies what line's height to estimate. */
1515 estimate_mode_line_height (f, face_id)
1516 struct frame *f;
1517 enum face_id face_id;
1519 #ifdef HAVE_WINDOW_SYSTEM
1520 if (FRAME_WINDOW_P (f))
1522 int height = FONT_HEIGHT (FRAME_FONT (f));
1524 /* This function is called so early when Emacs starts that the face
1525 cache and mode line face are not yet initialized. */
1526 if (FRAME_FACE_CACHE (f))
1528 struct face *face = FACE_FROM_ID (f, face_id);
1529 if (face)
1531 if (face->font)
1532 height = FONT_HEIGHT (face->font);
1533 if (face->box_line_width > 0)
1534 height += 2 * face->box_line_width;
1538 return height;
1540 #endif
1542 return 1;
1545 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1546 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1547 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1548 not force the value into range. */
1550 void
1551 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1552 FRAME_PTR f;
1553 register int pix_x, pix_y;
1554 int *x, *y;
1555 NativeRectangle *bounds;
1556 int noclip;
1559 #ifdef HAVE_WINDOW_SYSTEM
1560 if (FRAME_WINDOW_P (f))
1562 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1563 even for negative values. */
1564 if (pix_x < 0)
1565 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1566 if (pix_y < 0)
1567 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1569 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1570 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1572 if (bounds)
1573 STORE_NATIVE_RECT (*bounds,
1574 FRAME_COL_TO_PIXEL_X (f, pix_x),
1575 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1576 FRAME_COLUMN_WIDTH (f) - 1,
1577 FRAME_LINE_HEIGHT (f) - 1);
1579 if (!noclip)
1581 if (pix_x < 0)
1582 pix_x = 0;
1583 else if (pix_x > FRAME_TOTAL_COLS (f))
1584 pix_x = FRAME_TOTAL_COLS (f);
1586 if (pix_y < 0)
1587 pix_y = 0;
1588 else if (pix_y > FRAME_LINES (f))
1589 pix_y = FRAME_LINES (f);
1592 #endif
1594 *x = pix_x;
1595 *y = pix_y;
1599 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1600 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1601 can't tell the positions because W's display is not up to date,
1602 return 0. */
1605 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1606 struct window *w;
1607 int hpos, vpos;
1608 int *frame_x, *frame_y;
1610 #ifdef HAVE_WINDOW_SYSTEM
1611 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1613 int success_p;
1615 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1616 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1618 if (display_completed)
1620 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1621 struct glyph *glyph = row->glyphs[TEXT_AREA];
1622 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1624 hpos = row->x;
1625 vpos = row->y;
1626 while (glyph < end)
1628 hpos += glyph->pixel_width;
1629 ++glyph;
1632 /* If first glyph is partially visible, its first visible position is still 0. */
1633 if (hpos < 0)
1634 hpos = 0;
1636 success_p = 1;
1638 else
1640 hpos = vpos = 0;
1641 success_p = 0;
1644 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1645 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1646 return success_p;
1648 #endif
1650 *frame_x = hpos;
1651 *frame_y = vpos;
1652 return 1;
1656 #ifdef HAVE_WINDOW_SYSTEM
1658 /* Find the glyph under window-relative coordinates X/Y in window W.
1659 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1660 strings. Return in *HPOS and *VPOS the row and column number of
1661 the glyph found. Return in *AREA the glyph area containing X.
1662 Value is a pointer to the glyph found or null if X/Y is not on
1663 text, or we can't tell because W's current matrix is not up to
1664 date. */
1666 static struct glyph *
1667 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1668 struct window *w;
1669 int x, y;
1670 int *hpos, *vpos, *dx, *dy, *area;
1672 struct glyph *glyph, *end;
1673 struct glyph_row *row = NULL;
1674 int x0, i;
1676 /* Find row containing Y. Give up if some row is not enabled. */
1677 for (i = 0; i < w->current_matrix->nrows; ++i)
1679 row = MATRIX_ROW (w->current_matrix, i);
1680 if (!row->enabled_p)
1681 return NULL;
1682 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1683 break;
1686 *vpos = i;
1687 *hpos = 0;
1689 /* Give up if Y is not in the window. */
1690 if (i == w->current_matrix->nrows)
1691 return NULL;
1693 /* Get the glyph area containing X. */
1694 if (w->pseudo_window_p)
1696 *area = TEXT_AREA;
1697 x0 = 0;
1699 else
1701 if (x < window_box_left_offset (w, TEXT_AREA))
1703 *area = LEFT_MARGIN_AREA;
1704 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1706 else if (x < window_box_right_offset (w, TEXT_AREA))
1708 *area = TEXT_AREA;
1709 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1711 else
1713 *area = RIGHT_MARGIN_AREA;
1714 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1718 /* Find glyph containing X. */
1719 glyph = row->glyphs[*area];
1720 end = glyph + row->used[*area];
1721 x -= x0;
1722 while (glyph < end && x >= glyph->pixel_width)
1724 x -= glyph->pixel_width;
1725 ++glyph;
1728 if (glyph == end)
1729 return NULL;
1731 if (dx)
1733 *dx = x;
1734 *dy = y - (row->y + row->ascent - glyph->ascent);
1737 *hpos = glyph - row->glyphs[*area];
1738 return glyph;
1742 /* EXPORT:
1743 Convert frame-relative x/y to coordinates relative to window W.
1744 Takes pseudo-windows into account. */
1746 void
1747 frame_to_window_pixel_xy (w, x, y)
1748 struct window *w;
1749 int *x, *y;
1751 if (w->pseudo_window_p)
1753 /* A pseudo-window is always full-width, and starts at the
1754 left edge of the frame, plus a frame border. */
1755 struct frame *f = XFRAME (w->frame);
1756 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1757 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1759 else
1761 *x -= WINDOW_LEFT_EDGE_X (w);
1762 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1766 /* EXPORT:
1767 Return in *R the clipping rectangle for glyph string S. */
1769 void
1770 get_glyph_string_clip_rect (s, nr)
1771 struct glyph_string *s;
1772 NativeRectangle *nr;
1774 XRectangle r;
1776 if (s->row->full_width_p)
1778 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1779 r.x = WINDOW_LEFT_EDGE_X (s->w);
1780 r.width = WINDOW_TOTAL_WIDTH (s->w);
1782 /* Unless displaying a mode or menu bar line, which are always
1783 fully visible, clip to the visible part of the row. */
1784 if (s->w->pseudo_window_p)
1785 r.height = s->row->visible_height;
1786 else
1787 r.height = s->height;
1789 else
1791 /* This is a text line that may be partially visible. */
1792 r.x = window_box_left (s->w, s->area);
1793 r.width = window_box_width (s->w, s->area);
1794 r.height = s->row->visible_height;
1797 if (s->clip_head)
1798 if (r.x < s->clip_head->x)
1800 if (r.width >= s->clip_head->x - r.x)
1801 r.width -= s->clip_head->x - r.x;
1802 else
1803 r.width = 0;
1804 r.x = s->clip_head->x;
1806 if (s->clip_tail)
1807 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1809 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1810 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1811 else
1812 r.width = 0;
1815 /* If S draws overlapping rows, it's sufficient to use the top and
1816 bottom of the window for clipping because this glyph string
1817 intentionally draws over other lines. */
1818 if (s->for_overlaps_p)
1820 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1821 r.height = window_text_bottom_y (s->w) - r.y;
1823 else
1825 /* Don't use S->y for clipping because it doesn't take partially
1826 visible lines into account. For example, it can be negative for
1827 partially visible lines at the top of a window. */
1828 if (!s->row->full_width_p
1829 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1830 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1831 else
1832 r.y = max (0, s->row->y);
1834 /* If drawing a tool-bar window, draw it over the internal border
1835 at the top of the window. */
1836 if (WINDOWP (s->f->tool_bar_window)
1837 && s->w == XWINDOW (s->f->tool_bar_window))
1838 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1841 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1843 /* If drawing the cursor, don't let glyph draw outside its
1844 advertised boundaries. Cleartype does this under some circumstances. */
1845 if (s->hl == DRAW_CURSOR)
1847 struct glyph *glyph = s->first_glyph;
1848 int height, max_y;
1850 if (s->x > r.x)
1852 r.width -= s->x - r.x;
1853 r.x = s->x;
1855 r.width = min (r.width, glyph->pixel_width);
1857 /* If r.y is below window bottom, ensure that we still see a cursor. */
1858 height = min (glyph->ascent + glyph->descent,
1859 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1860 max_y = window_text_bottom_y (s->w) - height;
1861 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1862 if (s->ybase - glyph->ascent > max_y)
1864 r.y = max_y;
1865 r.height = height;
1867 else
1869 /* Don't draw cursor glyph taller than our actual glyph. */
1870 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1871 if (height < r.height)
1873 max_y = r.y + r.height;
1874 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1875 r.height = min (max_y - r.y, height);
1880 #ifdef CONVERT_FROM_XRECT
1881 CONVERT_FROM_XRECT (r, *nr);
1882 #else
1883 *nr = r;
1884 #endif
1888 /* EXPORT:
1889 Return the position and height of the phys cursor in window W.
1890 Set w->phys_cursor_width to width of phys cursor.
1894 get_phys_cursor_geometry (w, row, glyph, heightp)
1895 struct window *w;
1896 struct glyph_row *row;
1897 struct glyph *glyph;
1898 int *heightp;
1900 struct frame *f = XFRAME (WINDOW_FRAME (w));
1901 int y, wd, h, h0, y0;
1903 /* Compute the width of the rectangle to draw. If on a stretch
1904 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1905 rectangle as wide as the glyph, but use a canonical character
1906 width instead. */
1907 wd = glyph->pixel_width - 1;
1908 #ifdef HAVE_NTGUI
1909 wd++; /* Why? */
1910 #endif
1911 if (glyph->type == STRETCH_GLYPH
1912 && !x_stretch_cursor_p)
1913 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1914 w->phys_cursor_width = wd;
1916 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1918 /* If y is below window bottom, ensure that we still see a cursor. */
1919 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1921 h = max (h0, glyph->ascent + glyph->descent);
1922 h0 = min (h0, glyph->ascent + glyph->descent);
1924 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1925 if (y < y0)
1927 h = max (h - (y0 - y) + 1, h0);
1928 y = y0 - 1;
1930 else
1932 y0 = window_text_bottom_y (w) - h0;
1933 if (y > y0)
1935 h += y - y0;
1936 y = y0;
1940 *heightp = h - 1;
1941 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1945 #endif /* HAVE_WINDOW_SYSTEM */
1948 /***********************************************************************
1949 Lisp form evaluation
1950 ***********************************************************************/
1952 /* Error handler for safe_eval and safe_call. */
1954 static Lisp_Object
1955 safe_eval_handler (arg)
1956 Lisp_Object arg;
1958 add_to_log ("Error during redisplay: %s", arg, Qnil);
1959 return Qnil;
1963 /* Evaluate SEXPR and return the result, or nil if something went
1964 wrong. Prevent redisplay during the evaluation. */
1966 Lisp_Object
1967 safe_eval (sexpr)
1968 Lisp_Object sexpr;
1970 Lisp_Object val;
1972 if (inhibit_eval_during_redisplay)
1973 val = Qnil;
1974 else
1976 int count = SPECPDL_INDEX ();
1977 struct gcpro gcpro1;
1979 GCPRO1 (sexpr);
1980 specbind (Qinhibit_redisplay, Qt);
1981 /* Use Qt to ensure debugger does not run,
1982 so there is no possibility of wanting to redisplay. */
1983 val = internal_condition_case_1 (Feval, sexpr, Qt,
1984 safe_eval_handler);
1985 UNGCPRO;
1986 val = unbind_to (count, val);
1989 return val;
1993 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1994 Return the result, or nil if something went wrong. Prevent
1995 redisplay during the evaluation. */
1997 Lisp_Object
1998 safe_call (nargs, args)
1999 int nargs;
2000 Lisp_Object *args;
2002 Lisp_Object val;
2004 if (inhibit_eval_during_redisplay)
2005 val = Qnil;
2006 else
2008 int count = SPECPDL_INDEX ();
2009 struct gcpro gcpro1;
2011 GCPRO1 (args[0]);
2012 gcpro1.nvars = nargs;
2013 specbind (Qinhibit_redisplay, Qt);
2014 /* Use Qt to ensure debugger does not run,
2015 so there is no possibility of wanting to redisplay. */
2016 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2017 safe_eval_handler);
2018 UNGCPRO;
2019 val = unbind_to (count, val);
2022 return val;
2026 /* Call function FN with one argument ARG.
2027 Return the result, or nil if something went wrong. */
2029 Lisp_Object
2030 safe_call1 (fn, arg)
2031 Lisp_Object fn, arg;
2033 Lisp_Object args[2];
2034 args[0] = fn;
2035 args[1] = arg;
2036 return safe_call (2, args);
2041 /***********************************************************************
2042 Debugging
2043 ***********************************************************************/
2045 #if 0
2047 /* Define CHECK_IT to perform sanity checks on iterators.
2048 This is for debugging. It is too slow to do unconditionally. */
2050 static void
2051 check_it (it)
2052 struct it *it;
2054 if (it->method == GET_FROM_STRING)
2056 xassert (STRINGP (it->string));
2057 xassert (IT_STRING_CHARPOS (*it) >= 0);
2059 else
2061 xassert (IT_STRING_CHARPOS (*it) < 0);
2062 if (it->method == GET_FROM_BUFFER)
2064 /* Check that character and byte positions agree. */
2065 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2069 if (it->dpvec)
2070 xassert (it->current.dpvec_index >= 0);
2071 else
2072 xassert (it->current.dpvec_index < 0);
2075 #define CHECK_IT(IT) check_it ((IT))
2077 #else /* not 0 */
2079 #define CHECK_IT(IT) (void) 0
2081 #endif /* not 0 */
2084 #if GLYPH_DEBUG
2086 /* Check that the window end of window W is what we expect it
2087 to be---the last row in the current matrix displaying text. */
2089 static void
2090 check_window_end (w)
2091 struct window *w;
2093 if (!MINI_WINDOW_P (w)
2094 && !NILP (w->window_end_valid))
2096 struct glyph_row *row;
2097 xassert ((row = MATRIX_ROW (w->current_matrix,
2098 XFASTINT (w->window_end_vpos)),
2099 !row->enabled_p
2100 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2101 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2105 #define CHECK_WINDOW_END(W) check_window_end ((W))
2107 #else /* not GLYPH_DEBUG */
2109 #define CHECK_WINDOW_END(W) (void) 0
2111 #endif /* not GLYPH_DEBUG */
2115 /***********************************************************************
2116 Iterator initialization
2117 ***********************************************************************/
2119 /* Initialize IT for displaying current_buffer in window W, starting
2120 at character position CHARPOS. CHARPOS < 0 means that no buffer
2121 position is specified which is useful when the iterator is assigned
2122 a position later. BYTEPOS is the byte position corresponding to
2123 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2125 If ROW is not null, calls to produce_glyphs with IT as parameter
2126 will produce glyphs in that row.
2128 BASE_FACE_ID is the id of a base face to use. It must be one of
2129 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2130 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2131 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2133 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2134 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2135 will be initialized to use the corresponding mode line glyph row of
2136 the desired matrix of W. */
2138 void
2139 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2140 struct it *it;
2141 struct window *w;
2142 int charpos, bytepos;
2143 struct glyph_row *row;
2144 enum face_id base_face_id;
2146 int highlight_region_p;
2148 /* Some precondition checks. */
2149 xassert (w != NULL && it != NULL);
2150 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2151 && charpos <= ZV));
2153 /* If face attributes have been changed since the last redisplay,
2154 free realized faces now because they depend on face definitions
2155 that might have changed. Don't free faces while there might be
2156 desired matrices pending which reference these faces. */
2157 if (face_change_count && !inhibit_free_realized_faces)
2159 face_change_count = 0;
2160 free_all_realized_faces (Qnil);
2163 /* Use one of the mode line rows of W's desired matrix if
2164 appropriate. */
2165 if (row == NULL)
2167 if (base_face_id == MODE_LINE_FACE_ID
2168 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2169 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2170 else if (base_face_id == HEADER_LINE_FACE_ID)
2171 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2174 /* Clear IT. */
2175 bzero (it, sizeof *it);
2176 it->current.overlay_string_index = -1;
2177 it->current.dpvec_index = -1;
2178 it->base_face_id = base_face_id;
2179 it->string = Qnil;
2180 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2182 /* The window in which we iterate over current_buffer: */
2183 XSETWINDOW (it->window, w);
2184 it->w = w;
2185 it->f = XFRAME (w->frame);
2187 /* Extra space between lines (on window systems only). */
2188 if (base_face_id == DEFAULT_FACE_ID
2189 && FRAME_WINDOW_P (it->f))
2191 if (NATNUMP (current_buffer->extra_line_spacing))
2192 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2193 else if (FLOATP (current_buffer->extra_line_spacing))
2194 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2195 * FRAME_LINE_HEIGHT (it->f));
2196 else if (it->f->extra_line_spacing > 0)
2197 it->extra_line_spacing = it->f->extra_line_spacing;
2198 it->max_extra_line_spacing = 0;
2201 /* If realized faces have been removed, e.g. because of face
2202 attribute changes of named faces, recompute them. When running
2203 in batch mode, the face cache of Vterminal_frame is null. If
2204 we happen to get called, make a dummy face cache. */
2205 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2206 init_frame_faces (it->f);
2207 if (FRAME_FACE_CACHE (it->f)->used == 0)
2208 recompute_basic_faces (it->f);
2210 /* Current value of the `slice', `space-width', and 'height' properties. */
2211 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2212 it->space_width = Qnil;
2213 it->font_height = Qnil;
2214 it->override_ascent = -1;
2216 /* Are control characters displayed as `^C'? */
2217 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2219 /* -1 means everything between a CR and the following line end
2220 is invisible. >0 means lines indented more than this value are
2221 invisible. */
2222 it->selective = (INTEGERP (current_buffer->selective_display)
2223 ? XFASTINT (current_buffer->selective_display)
2224 : (!NILP (current_buffer->selective_display)
2225 ? -1 : 0));
2226 it->selective_display_ellipsis_p
2227 = !NILP (current_buffer->selective_display_ellipses);
2229 /* Display table to use. */
2230 it->dp = window_display_table (w);
2232 /* Are multibyte characters enabled in current_buffer? */
2233 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2235 /* Non-zero if we should highlight the region. */
2236 highlight_region_p
2237 = (!NILP (Vtransient_mark_mode)
2238 && !NILP (current_buffer->mark_active)
2239 && XMARKER (current_buffer->mark)->buffer != 0);
2241 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2242 start and end of a visible region in window IT->w. Set both to
2243 -1 to indicate no region. */
2244 if (highlight_region_p
2245 /* Maybe highlight only in selected window. */
2246 && (/* Either show region everywhere. */
2247 highlight_nonselected_windows
2248 /* Or show region in the selected window. */
2249 || w == XWINDOW (selected_window)
2250 /* Or show the region if we are in the mini-buffer and W is
2251 the window the mini-buffer refers to. */
2252 || (MINI_WINDOW_P (XWINDOW (selected_window))
2253 && WINDOWP (minibuf_selected_window)
2254 && w == XWINDOW (minibuf_selected_window))))
2256 int charpos = marker_position (current_buffer->mark);
2257 it->region_beg_charpos = min (PT, charpos);
2258 it->region_end_charpos = max (PT, charpos);
2260 else
2261 it->region_beg_charpos = it->region_end_charpos = -1;
2263 /* Get the position at which the redisplay_end_trigger hook should
2264 be run, if it is to be run at all. */
2265 if (MARKERP (w->redisplay_end_trigger)
2266 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2267 it->redisplay_end_trigger_charpos
2268 = marker_position (w->redisplay_end_trigger);
2269 else if (INTEGERP (w->redisplay_end_trigger))
2270 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2272 /* Correct bogus values of tab_width. */
2273 it->tab_width = XINT (current_buffer->tab_width);
2274 if (it->tab_width <= 0 || it->tab_width > 1000)
2275 it->tab_width = 8;
2277 /* Are lines in the display truncated? */
2278 it->truncate_lines_p
2279 = (base_face_id != DEFAULT_FACE_ID
2280 || XINT (it->w->hscroll)
2281 || (truncate_partial_width_windows
2282 && !WINDOW_FULL_WIDTH_P (it->w))
2283 || !NILP (current_buffer->truncate_lines));
2285 /* Get dimensions of truncation and continuation glyphs. These are
2286 displayed as fringe bitmaps under X, so we don't need them for such
2287 frames. */
2288 if (!FRAME_WINDOW_P (it->f))
2290 if (it->truncate_lines_p)
2292 /* We will need the truncation glyph. */
2293 xassert (it->glyph_row == NULL);
2294 produce_special_glyphs (it, IT_TRUNCATION);
2295 it->truncation_pixel_width = it->pixel_width;
2297 else
2299 /* We will need the continuation glyph. */
2300 xassert (it->glyph_row == NULL);
2301 produce_special_glyphs (it, IT_CONTINUATION);
2302 it->continuation_pixel_width = it->pixel_width;
2305 /* Reset these values to zero because the produce_special_glyphs
2306 above has changed them. */
2307 it->pixel_width = it->ascent = it->descent = 0;
2308 it->phys_ascent = it->phys_descent = 0;
2311 /* Set this after getting the dimensions of truncation and
2312 continuation glyphs, so that we don't produce glyphs when calling
2313 produce_special_glyphs, above. */
2314 it->glyph_row = row;
2315 it->area = TEXT_AREA;
2317 /* Get the dimensions of the display area. The display area
2318 consists of the visible window area plus a horizontally scrolled
2319 part to the left of the window. All x-values are relative to the
2320 start of this total display area. */
2321 if (base_face_id != DEFAULT_FACE_ID)
2323 /* Mode lines, menu bar in terminal frames. */
2324 it->first_visible_x = 0;
2325 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2327 else
2329 it->first_visible_x
2330 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2331 it->last_visible_x = (it->first_visible_x
2332 + window_box_width (w, TEXT_AREA));
2334 /* If we truncate lines, leave room for the truncator glyph(s) at
2335 the right margin. Otherwise, leave room for the continuation
2336 glyph(s). Truncation and continuation glyphs are not inserted
2337 for window-based redisplay. */
2338 if (!FRAME_WINDOW_P (it->f))
2340 if (it->truncate_lines_p)
2341 it->last_visible_x -= it->truncation_pixel_width;
2342 else
2343 it->last_visible_x -= it->continuation_pixel_width;
2346 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2347 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2350 /* Leave room for a border glyph. */
2351 if (!FRAME_WINDOW_P (it->f)
2352 && !WINDOW_RIGHTMOST_P (it->w))
2353 it->last_visible_x -= 1;
2355 it->last_visible_y = window_text_bottom_y (w);
2357 /* For mode lines and alike, arrange for the first glyph having a
2358 left box line if the face specifies a box. */
2359 if (base_face_id != DEFAULT_FACE_ID)
2361 struct face *face;
2363 it->face_id = base_face_id;
2365 /* If we have a boxed mode line, make the first character appear
2366 with a left box line. */
2367 face = FACE_FROM_ID (it->f, base_face_id);
2368 if (face->box != FACE_NO_BOX)
2369 it->start_of_box_run_p = 1;
2372 /* If a buffer position was specified, set the iterator there,
2373 getting overlays and face properties from that position. */
2374 if (charpos >= BUF_BEG (current_buffer))
2376 it->end_charpos = ZV;
2377 it->face_id = -1;
2378 IT_CHARPOS (*it) = charpos;
2380 /* Compute byte position if not specified. */
2381 if (bytepos < charpos)
2382 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2383 else
2384 IT_BYTEPOS (*it) = bytepos;
2386 it->start = it->current;
2388 /* Compute faces etc. */
2389 reseat (it, it->current.pos, 1);
2392 CHECK_IT (it);
2396 /* Initialize IT for the display of window W with window start POS. */
2398 void
2399 start_display (it, w, pos)
2400 struct it *it;
2401 struct window *w;
2402 struct text_pos pos;
2404 struct glyph_row *row;
2405 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2407 row = w->desired_matrix->rows + first_vpos;
2408 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2409 it->first_vpos = first_vpos;
2411 if (!it->truncate_lines_p)
2413 int start_at_line_beg_p;
2414 int first_y = it->current_y;
2416 /* If window start is not at a line start, skip forward to POS to
2417 get the correct continuation lines width. */
2418 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2419 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2420 if (!start_at_line_beg_p)
2422 int new_x;
2424 reseat_at_previous_visible_line_start (it);
2425 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2427 new_x = it->current_x + it->pixel_width;
2429 /* If lines are continued, this line may end in the middle
2430 of a multi-glyph character (e.g. a control character
2431 displayed as \003, or in the middle of an overlay
2432 string). In this case move_it_to above will not have
2433 taken us to the start of the continuation line but to the
2434 end of the continued line. */
2435 if (it->current_x > 0
2436 && !it->truncate_lines_p /* Lines are continued. */
2437 && (/* And glyph doesn't fit on the line. */
2438 new_x > it->last_visible_x
2439 /* Or it fits exactly and we're on a window
2440 system frame. */
2441 || (new_x == it->last_visible_x
2442 && FRAME_WINDOW_P (it->f))))
2444 if (it->current.dpvec_index >= 0
2445 || it->current.overlay_string_index >= 0)
2447 set_iterator_to_next (it, 1);
2448 move_it_in_display_line_to (it, -1, -1, 0);
2451 it->continuation_lines_width += it->current_x;
2454 /* We're starting a new display line, not affected by the
2455 height of the continued line, so clear the appropriate
2456 fields in the iterator structure. */
2457 it->max_ascent = it->max_descent = 0;
2458 it->max_phys_ascent = it->max_phys_descent = 0;
2460 it->current_y = first_y;
2461 it->vpos = 0;
2462 it->current_x = it->hpos = 0;
2466 #if 0 /* Don't assert the following because start_display is sometimes
2467 called intentionally with a window start that is not at a
2468 line start. Please leave this code in as a comment. */
2470 /* Window start should be on a line start, now. */
2471 xassert (it->continuation_lines_width
2472 || IT_CHARPOS (it) == BEGV
2473 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2474 #endif /* 0 */
2478 /* Return 1 if POS is a position in ellipses displayed for invisible
2479 text. W is the window we display, for text property lookup. */
2481 static int
2482 in_ellipses_for_invisible_text_p (pos, w)
2483 struct display_pos *pos;
2484 struct window *w;
2486 Lisp_Object prop, window;
2487 int ellipses_p = 0;
2488 int charpos = CHARPOS (pos->pos);
2490 /* If POS specifies a position in a display vector, this might
2491 be for an ellipsis displayed for invisible text. We won't
2492 get the iterator set up for delivering that ellipsis unless
2493 we make sure that it gets aware of the invisible text. */
2494 if (pos->dpvec_index >= 0
2495 && pos->overlay_string_index < 0
2496 && CHARPOS (pos->string_pos) < 0
2497 && charpos > BEGV
2498 && (XSETWINDOW (window, w),
2499 prop = Fget_char_property (make_number (charpos),
2500 Qinvisible, window),
2501 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2503 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2504 window);
2505 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2508 return ellipses_p;
2512 /* Initialize IT for stepping through current_buffer in window W,
2513 starting at position POS that includes overlay string and display
2514 vector/ control character translation position information. Value
2515 is zero if there are overlay strings with newlines at POS. */
2517 static int
2518 init_from_display_pos (it, w, pos)
2519 struct it *it;
2520 struct window *w;
2521 struct display_pos *pos;
2523 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2524 int i, overlay_strings_with_newlines = 0;
2526 /* If POS specifies a position in a display vector, this might
2527 be for an ellipsis displayed for invisible text. We won't
2528 get the iterator set up for delivering that ellipsis unless
2529 we make sure that it gets aware of the invisible text. */
2530 if (in_ellipses_for_invisible_text_p (pos, w))
2532 --charpos;
2533 bytepos = 0;
2536 /* Keep in mind: the call to reseat in init_iterator skips invisible
2537 text, so we might end up at a position different from POS. This
2538 is only a problem when POS is a row start after a newline and an
2539 overlay starts there with an after-string, and the overlay has an
2540 invisible property. Since we don't skip invisible text in
2541 display_line and elsewhere immediately after consuming the
2542 newline before the row start, such a POS will not be in a string,
2543 but the call to init_iterator below will move us to the
2544 after-string. */
2545 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2547 /* This only scans the current chunk -- it should scan all chunks.
2548 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2549 to 16 in 22.1 to make this a lesser problem. */
2550 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2552 const char *s = SDATA (it->overlay_strings[i]);
2553 const char *e = s + SBYTES (it->overlay_strings[i]);
2555 while (s < e && *s != '\n')
2556 ++s;
2558 if (s < e)
2560 overlay_strings_with_newlines = 1;
2561 break;
2565 /* If position is within an overlay string, set up IT to the right
2566 overlay string. */
2567 if (pos->overlay_string_index >= 0)
2569 int relative_index;
2571 /* If the first overlay string happens to have a `display'
2572 property for an image, the iterator will be set up for that
2573 image, and we have to undo that setup first before we can
2574 correct the overlay string index. */
2575 if (it->method == GET_FROM_IMAGE)
2576 pop_it (it);
2578 /* We already have the first chunk of overlay strings in
2579 IT->overlay_strings. Load more until the one for
2580 pos->overlay_string_index is in IT->overlay_strings. */
2581 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2583 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2584 it->current.overlay_string_index = 0;
2585 while (n--)
2587 load_overlay_strings (it, 0);
2588 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2592 it->current.overlay_string_index = pos->overlay_string_index;
2593 relative_index = (it->current.overlay_string_index
2594 % OVERLAY_STRING_CHUNK_SIZE);
2595 it->string = it->overlay_strings[relative_index];
2596 xassert (STRINGP (it->string));
2597 it->current.string_pos = pos->string_pos;
2598 it->method = GET_FROM_STRING;
2601 #if 0 /* This is bogus because POS not having an overlay string
2602 position does not mean it's after the string. Example: A
2603 line starting with a before-string and initialization of IT
2604 to the previous row's end position. */
2605 else if (it->current.overlay_string_index >= 0)
2607 /* If POS says we're already after an overlay string ending at
2608 POS, make sure to pop the iterator because it will be in
2609 front of that overlay string. When POS is ZV, we've thereby
2610 also ``processed'' overlay strings at ZV. */
2611 while (it->sp)
2612 pop_it (it);
2613 it->current.overlay_string_index = -1;
2614 it->method = GET_FROM_BUFFER;
2615 if (CHARPOS (pos->pos) == ZV)
2616 it->overlay_strings_at_end_processed_p = 1;
2618 #endif /* 0 */
2620 if (CHARPOS (pos->string_pos) >= 0)
2622 /* Recorded position is not in an overlay string, but in another
2623 string. This can only be a string from a `display' property.
2624 IT should already be filled with that string. */
2625 it->current.string_pos = pos->string_pos;
2626 xassert (STRINGP (it->string));
2629 /* Restore position in display vector translations, control
2630 character translations or ellipses. */
2631 if (pos->dpvec_index >= 0)
2633 if (it->dpvec == NULL)
2634 get_next_display_element (it);
2635 xassert (it->dpvec && it->current.dpvec_index == 0);
2636 it->current.dpvec_index = pos->dpvec_index;
2639 CHECK_IT (it);
2640 return !overlay_strings_with_newlines;
2644 /* Initialize IT for stepping through current_buffer in window W
2645 starting at ROW->start. */
2647 static void
2648 init_to_row_start (it, w, row)
2649 struct it *it;
2650 struct window *w;
2651 struct glyph_row *row;
2653 init_from_display_pos (it, w, &row->start);
2654 it->start = row->start;
2655 it->continuation_lines_width = row->continuation_lines_width;
2656 CHECK_IT (it);
2660 /* Initialize IT for stepping through current_buffer in window W
2661 starting in the line following ROW, i.e. starting at ROW->end.
2662 Value is zero if there are overlay strings with newlines at ROW's
2663 end position. */
2665 static int
2666 init_to_row_end (it, w, row)
2667 struct it *it;
2668 struct window *w;
2669 struct glyph_row *row;
2671 int success = 0;
2673 if (init_from_display_pos (it, w, &row->end))
2675 if (row->continued_p)
2676 it->continuation_lines_width
2677 = row->continuation_lines_width + row->pixel_width;
2678 CHECK_IT (it);
2679 success = 1;
2682 return success;
2688 /***********************************************************************
2689 Text properties
2690 ***********************************************************************/
2692 /* Called when IT reaches IT->stop_charpos. Handle text property and
2693 overlay changes. Set IT->stop_charpos to the next position where
2694 to stop. */
2696 static void
2697 handle_stop (it)
2698 struct it *it;
2700 enum prop_handled handled;
2701 int handle_overlay_change_p = 1;
2702 struct props *p;
2704 it->dpvec = NULL;
2705 it->current.dpvec_index = -1;
2707 /* Use face of preceding text for ellipsis (if invisible) */
2708 if (it->selective_display_ellipsis_p)
2709 it->saved_face_id = it->face_id;
2713 handled = HANDLED_NORMALLY;
2715 /* Call text property handlers. */
2716 for (p = it_props; p->handler; ++p)
2718 handled = p->handler (it);
2720 if (handled == HANDLED_RECOMPUTE_PROPS)
2721 break;
2722 else if (handled == HANDLED_RETURN)
2723 return;
2724 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2725 handle_overlay_change_p = 0;
2728 if (handled != HANDLED_RECOMPUTE_PROPS)
2730 /* Don't check for overlay strings below when set to deliver
2731 characters from a display vector. */
2732 if (it->method == GET_FROM_DISPLAY_VECTOR)
2733 handle_overlay_change_p = 0;
2735 /* Handle overlay changes. */
2736 if (handle_overlay_change_p)
2737 handled = handle_overlay_change (it);
2739 /* Determine where to stop next. */
2740 if (handled == HANDLED_NORMALLY)
2741 compute_stop_pos (it);
2744 while (handled == HANDLED_RECOMPUTE_PROPS);
2748 /* Compute IT->stop_charpos from text property and overlay change
2749 information for IT's current position. */
2751 static void
2752 compute_stop_pos (it)
2753 struct it *it;
2755 register INTERVAL iv, next_iv;
2756 Lisp_Object object, limit, position;
2758 /* If nowhere else, stop at the end. */
2759 it->stop_charpos = it->end_charpos;
2761 if (STRINGP (it->string))
2763 /* Strings are usually short, so don't limit the search for
2764 properties. */
2765 object = it->string;
2766 limit = Qnil;
2767 position = make_number (IT_STRING_CHARPOS (*it));
2769 else
2771 int charpos;
2773 /* If next overlay change is in front of the current stop pos
2774 (which is IT->end_charpos), stop there. Note: value of
2775 next_overlay_change is point-max if no overlay change
2776 follows. */
2777 charpos = next_overlay_change (IT_CHARPOS (*it));
2778 if (charpos < it->stop_charpos)
2779 it->stop_charpos = charpos;
2781 /* If showing the region, we have to stop at the region
2782 start or end because the face might change there. */
2783 if (it->region_beg_charpos > 0)
2785 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2786 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2787 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2788 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2791 /* Set up variables for computing the stop position from text
2792 property changes. */
2793 XSETBUFFER (object, current_buffer);
2794 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2795 position = make_number (IT_CHARPOS (*it));
2799 /* Get the interval containing IT's position. Value is a null
2800 interval if there isn't such an interval. */
2801 iv = validate_interval_range (object, &position, &position, 0);
2802 if (!NULL_INTERVAL_P (iv))
2804 Lisp_Object values_here[LAST_PROP_IDX];
2805 struct props *p;
2807 /* Get properties here. */
2808 for (p = it_props; p->handler; ++p)
2809 values_here[p->idx] = textget (iv->plist, *p->name);
2811 /* Look for an interval following iv that has different
2812 properties. */
2813 for (next_iv = next_interval (iv);
2814 (!NULL_INTERVAL_P (next_iv)
2815 && (NILP (limit)
2816 || XFASTINT (limit) > next_iv->position));
2817 next_iv = next_interval (next_iv))
2819 for (p = it_props; p->handler; ++p)
2821 Lisp_Object new_value;
2823 new_value = textget (next_iv->plist, *p->name);
2824 if (!EQ (values_here[p->idx], new_value))
2825 break;
2828 if (p->handler)
2829 break;
2832 if (!NULL_INTERVAL_P (next_iv))
2834 if (INTEGERP (limit)
2835 && next_iv->position >= XFASTINT (limit))
2836 /* No text property change up to limit. */
2837 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2838 else
2839 /* Text properties change in next_iv. */
2840 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2844 xassert (STRINGP (it->string)
2845 || (it->stop_charpos >= BEGV
2846 && it->stop_charpos >= IT_CHARPOS (*it)));
2850 /* Return the position of the next overlay change after POS in
2851 current_buffer. Value is point-max if no overlay change
2852 follows. This is like `next-overlay-change' but doesn't use
2853 xmalloc. */
2855 static int
2856 next_overlay_change (pos)
2857 int pos;
2859 int noverlays;
2860 int endpos;
2861 Lisp_Object *overlays;
2862 int i;
2864 /* Get all overlays at the given position. */
2865 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2867 /* If any of these overlays ends before endpos,
2868 use its ending point instead. */
2869 for (i = 0; i < noverlays; ++i)
2871 Lisp_Object oend;
2872 int oendpos;
2874 oend = OVERLAY_END (overlays[i]);
2875 oendpos = OVERLAY_POSITION (oend);
2876 endpos = min (endpos, oendpos);
2879 return endpos;
2884 /***********************************************************************
2885 Fontification
2886 ***********************************************************************/
2888 /* Handle changes in the `fontified' property of the current buffer by
2889 calling hook functions from Qfontification_functions to fontify
2890 regions of text. */
2892 static enum prop_handled
2893 handle_fontified_prop (it)
2894 struct it *it;
2896 Lisp_Object prop, pos;
2897 enum prop_handled handled = HANDLED_NORMALLY;
2899 /* Get the value of the `fontified' property at IT's current buffer
2900 position. (The `fontified' property doesn't have a special
2901 meaning in strings.) If the value is nil, call functions from
2902 Qfontification_functions. */
2903 if (!STRINGP (it->string)
2904 && it->s == NULL
2905 && !NILP (Vfontification_functions)
2906 && !NILP (Vrun_hooks)
2907 && (pos = make_number (IT_CHARPOS (*it)),
2908 prop = Fget_char_property (pos, Qfontified, Qnil),
2909 NILP (prop)))
2911 int count = SPECPDL_INDEX ();
2912 Lisp_Object val;
2914 val = Vfontification_functions;
2915 specbind (Qfontification_functions, Qnil);
2917 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2918 safe_call1 (val, pos);
2919 else
2921 Lisp_Object globals, fn;
2922 struct gcpro gcpro1, gcpro2;
2924 globals = Qnil;
2925 GCPRO2 (val, globals);
2927 for (; CONSP (val); val = XCDR (val))
2929 fn = XCAR (val);
2931 if (EQ (fn, Qt))
2933 /* A value of t indicates this hook has a local
2934 binding; it means to run the global binding too.
2935 In a global value, t should not occur. If it
2936 does, we must ignore it to avoid an endless
2937 loop. */
2938 for (globals = Fdefault_value (Qfontification_functions);
2939 CONSP (globals);
2940 globals = XCDR (globals))
2942 fn = XCAR (globals);
2943 if (!EQ (fn, Qt))
2944 safe_call1 (fn, pos);
2947 else
2948 safe_call1 (fn, pos);
2951 UNGCPRO;
2954 unbind_to (count, Qnil);
2956 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2957 something. This avoids an endless loop if they failed to
2958 fontify the text for which reason ever. */
2959 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2960 handled = HANDLED_RECOMPUTE_PROPS;
2963 return handled;
2968 /***********************************************************************
2969 Faces
2970 ***********************************************************************/
2972 /* Set up iterator IT from face properties at its current position.
2973 Called from handle_stop. */
2975 static enum prop_handled
2976 handle_face_prop (it)
2977 struct it *it;
2979 int new_face_id, next_stop;
2981 if (!STRINGP (it->string))
2983 new_face_id
2984 = face_at_buffer_position (it->w,
2985 IT_CHARPOS (*it),
2986 it->region_beg_charpos,
2987 it->region_end_charpos,
2988 &next_stop,
2989 (IT_CHARPOS (*it)
2990 + TEXT_PROP_DISTANCE_LIMIT),
2993 /* Is this a start of a run of characters with box face?
2994 Caveat: this can be called for a freshly initialized
2995 iterator; face_id is -1 in this case. We know that the new
2996 face will not change until limit, i.e. if the new face has a
2997 box, all characters up to limit will have one. But, as
2998 usual, we don't know whether limit is really the end. */
2999 if (new_face_id != it->face_id)
3001 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3003 /* If new face has a box but old face has not, this is
3004 the start of a run of characters with box, i.e. it has
3005 a shadow on the left side. The value of face_id of the
3006 iterator will be -1 if this is the initial call that gets
3007 the face. In this case, we have to look in front of IT's
3008 position and see whether there is a face != new_face_id. */
3009 it->start_of_box_run_p
3010 = (new_face->box != FACE_NO_BOX
3011 && (it->face_id >= 0
3012 || IT_CHARPOS (*it) == BEG
3013 || new_face_id != face_before_it_pos (it)));
3014 it->face_box_p = new_face->box != FACE_NO_BOX;
3017 else
3019 int base_face_id, bufpos;
3021 if (it->current.overlay_string_index >= 0)
3022 bufpos = IT_CHARPOS (*it);
3023 else
3024 bufpos = 0;
3026 /* For strings from a buffer, i.e. overlay strings or strings
3027 from a `display' property, use the face at IT's current
3028 buffer position as the base face to merge with, so that
3029 overlay strings appear in the same face as surrounding
3030 text, unless they specify their own faces. */
3031 base_face_id = underlying_face_id (it);
3033 new_face_id = face_at_string_position (it->w,
3034 it->string,
3035 IT_STRING_CHARPOS (*it),
3036 bufpos,
3037 it->region_beg_charpos,
3038 it->region_end_charpos,
3039 &next_stop,
3040 base_face_id, 0);
3042 #if 0 /* This shouldn't be neccessary. Let's check it. */
3043 /* If IT is used to display a mode line we would really like to
3044 use the mode line face instead of the frame's default face. */
3045 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3046 && new_face_id == DEFAULT_FACE_ID)
3047 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3048 #endif
3050 /* Is this a start of a run of characters with box? Caveat:
3051 this can be called for a freshly allocated iterator; face_id
3052 is -1 is this case. We know that the new face will not
3053 change until the next check pos, i.e. if the new face has a
3054 box, all characters up to that position will have a
3055 box. But, as usual, we don't know whether that position
3056 is really the end. */
3057 if (new_face_id != it->face_id)
3059 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3060 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3062 /* If new face has a box but old face hasn't, this is the
3063 start of a run of characters with box, i.e. it has a
3064 shadow on the left side. */
3065 it->start_of_box_run_p
3066 = new_face->box && (old_face == NULL || !old_face->box);
3067 it->face_box_p = new_face->box != FACE_NO_BOX;
3071 it->face_id = new_face_id;
3072 return HANDLED_NORMALLY;
3076 /* Return the ID of the face ``underlying'' IT's current position,
3077 which is in a string. If the iterator is associated with a
3078 buffer, return the face at IT's current buffer position.
3079 Otherwise, use the iterator's base_face_id. */
3081 static int
3082 underlying_face_id (it)
3083 struct it *it;
3085 int face_id = it->base_face_id, i;
3087 xassert (STRINGP (it->string));
3089 for (i = it->sp - 1; i >= 0; --i)
3090 if (NILP (it->stack[i].string))
3091 face_id = it->stack[i].face_id;
3093 return face_id;
3097 /* Compute the face one character before or after the current position
3098 of IT. BEFORE_P non-zero means get the face in front of IT's
3099 position. Value is the id of the face. */
3101 static int
3102 face_before_or_after_it_pos (it, before_p)
3103 struct it *it;
3104 int before_p;
3106 int face_id, limit;
3107 int next_check_charpos;
3108 struct text_pos pos;
3110 xassert (it->s == NULL);
3112 if (STRINGP (it->string))
3114 int bufpos, base_face_id;
3116 /* No face change past the end of the string (for the case
3117 we are padding with spaces). No face change before the
3118 string start. */
3119 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3120 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3121 return it->face_id;
3123 /* Set pos to the position before or after IT's current position. */
3124 if (before_p)
3125 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3126 else
3127 /* For composition, we must check the character after the
3128 composition. */
3129 pos = (it->what == IT_COMPOSITION
3130 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3131 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3133 if (it->current.overlay_string_index >= 0)
3134 bufpos = IT_CHARPOS (*it);
3135 else
3136 bufpos = 0;
3138 base_face_id = underlying_face_id (it);
3140 /* Get the face for ASCII, or unibyte. */
3141 face_id = face_at_string_position (it->w,
3142 it->string,
3143 CHARPOS (pos),
3144 bufpos,
3145 it->region_beg_charpos,
3146 it->region_end_charpos,
3147 &next_check_charpos,
3148 base_face_id, 0);
3150 /* Correct the face for charsets different from ASCII. Do it
3151 for the multibyte case only. The face returned above is
3152 suitable for unibyte text if IT->string is unibyte. */
3153 if (STRING_MULTIBYTE (it->string))
3155 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3156 int rest = SBYTES (it->string) - BYTEPOS (pos);
3157 int c, len;
3158 struct face *face = FACE_FROM_ID (it->f, face_id);
3160 c = string_char_and_length (p, rest, &len);
3161 face_id = FACE_FOR_CHAR (it->f, face, c);
3164 else
3166 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3167 || (IT_CHARPOS (*it) <= BEGV && before_p))
3168 return it->face_id;
3170 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3171 pos = it->current.pos;
3173 if (before_p)
3174 DEC_TEXT_POS (pos, it->multibyte_p);
3175 else
3177 if (it->what == IT_COMPOSITION)
3178 /* For composition, we must check the position after the
3179 composition. */
3180 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3181 else
3182 INC_TEXT_POS (pos, it->multibyte_p);
3185 /* Determine face for CHARSET_ASCII, or unibyte. */
3186 face_id = face_at_buffer_position (it->w,
3187 CHARPOS (pos),
3188 it->region_beg_charpos,
3189 it->region_end_charpos,
3190 &next_check_charpos,
3191 limit, 0);
3193 /* Correct the face for charsets different from ASCII. Do it
3194 for the multibyte case only. The face returned above is
3195 suitable for unibyte text if current_buffer is unibyte. */
3196 if (it->multibyte_p)
3198 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3199 struct face *face = FACE_FROM_ID (it->f, face_id);
3200 face_id = FACE_FOR_CHAR (it->f, face, c);
3204 return face_id;
3209 /***********************************************************************
3210 Invisible text
3211 ***********************************************************************/
3213 /* Set up iterator IT from invisible properties at its current
3214 position. Called from handle_stop. */
3216 static enum prop_handled
3217 handle_invisible_prop (it)
3218 struct it *it;
3220 enum prop_handled handled = HANDLED_NORMALLY;
3222 if (STRINGP (it->string))
3224 extern Lisp_Object Qinvisible;
3225 Lisp_Object prop, end_charpos, limit, charpos;
3227 /* Get the value of the invisible text property at the
3228 current position. Value will be nil if there is no such
3229 property. */
3230 charpos = make_number (IT_STRING_CHARPOS (*it));
3231 prop = Fget_text_property (charpos, Qinvisible, it->string);
3233 if (!NILP (prop)
3234 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3236 handled = HANDLED_RECOMPUTE_PROPS;
3238 /* Get the position at which the next change of the
3239 invisible text property can be found in IT->string.
3240 Value will be nil if the property value is the same for
3241 all the rest of IT->string. */
3242 XSETINT (limit, SCHARS (it->string));
3243 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3244 it->string, limit);
3246 /* Text at current position is invisible. The next
3247 change in the property is at position end_charpos.
3248 Move IT's current position to that position. */
3249 if (INTEGERP (end_charpos)
3250 && XFASTINT (end_charpos) < XFASTINT (limit))
3252 struct text_pos old;
3253 old = it->current.string_pos;
3254 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3255 compute_string_pos (&it->current.string_pos, old, it->string);
3257 else
3259 /* The rest of the string is invisible. If this is an
3260 overlay string, proceed with the next overlay string
3261 or whatever comes and return a character from there. */
3262 if (it->current.overlay_string_index >= 0)
3264 next_overlay_string (it);
3265 /* Don't check for overlay strings when we just
3266 finished processing them. */
3267 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3269 else
3271 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3272 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3277 else
3279 int invis_p, newpos, next_stop, start_charpos;
3280 Lisp_Object pos, prop, overlay;
3282 /* First of all, is there invisible text at this position? */
3283 start_charpos = IT_CHARPOS (*it);
3284 pos = make_number (IT_CHARPOS (*it));
3285 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3286 &overlay);
3287 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3289 /* If we are on invisible text, skip over it. */
3290 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3292 /* Record whether we have to display an ellipsis for the
3293 invisible text. */
3294 int display_ellipsis_p = invis_p == 2;
3296 handled = HANDLED_RECOMPUTE_PROPS;
3298 /* Loop skipping over invisible text. The loop is left at
3299 ZV or with IT on the first char being visible again. */
3302 /* Try to skip some invisible text. Return value is the
3303 position reached which can be equal to IT's position
3304 if there is nothing invisible here. This skips both
3305 over invisible text properties and overlays with
3306 invisible property. */
3307 newpos = skip_invisible (IT_CHARPOS (*it),
3308 &next_stop, ZV, it->window);
3310 /* If we skipped nothing at all we weren't at invisible
3311 text in the first place. If everything to the end of
3312 the buffer was skipped, end the loop. */
3313 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3314 invis_p = 0;
3315 else
3317 /* We skipped some characters but not necessarily
3318 all there are. Check if we ended up on visible
3319 text. Fget_char_property returns the property of
3320 the char before the given position, i.e. if we
3321 get invis_p = 0, this means that the char at
3322 newpos is visible. */
3323 pos = make_number (newpos);
3324 prop = Fget_char_property (pos, Qinvisible, it->window);
3325 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3328 /* If we ended up on invisible text, proceed to
3329 skip starting with next_stop. */
3330 if (invis_p)
3331 IT_CHARPOS (*it) = next_stop;
3333 while (invis_p);
3335 /* The position newpos is now either ZV or on visible text. */
3336 IT_CHARPOS (*it) = newpos;
3337 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3339 /* If there are before-strings at the start of invisible
3340 text, and the text is invisible because of a text
3341 property, arrange to show before-strings because 20.x did
3342 it that way. (If the text is invisible because of an
3343 overlay property instead of a text property, this is
3344 already handled in the overlay code.) */
3345 if (NILP (overlay)
3346 && get_overlay_strings (it, start_charpos))
3348 handled = HANDLED_RECOMPUTE_PROPS;
3349 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3351 else if (display_ellipsis_p)
3352 setup_for_ellipsis (it, 0);
3356 return handled;
3360 /* Make iterator IT return `...' next.
3361 Replaces LEN characters from buffer. */
3363 static void
3364 setup_for_ellipsis (it, len)
3365 struct it *it;
3366 int len;
3368 /* Use the display table definition for `...'. Invalid glyphs
3369 will be handled by the method returning elements from dpvec. */
3370 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3372 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3373 it->dpvec = v->contents;
3374 it->dpend = v->contents + v->size;
3376 else
3378 /* Default `...'. */
3379 it->dpvec = default_invis_vector;
3380 it->dpend = default_invis_vector + 3;
3383 it->dpvec_char_len = len;
3384 it->current.dpvec_index = 0;
3385 it->dpvec_face_id = -1;
3387 /* Remember the current face id in case glyphs specify faces.
3388 IT's face is restored in set_iterator_to_next.
3389 saved_face_id was set to preceding char's face in handle_stop. */
3390 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3391 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3393 it->method = GET_FROM_DISPLAY_VECTOR;
3394 it->ellipsis_p = 1;
3399 /***********************************************************************
3400 'display' property
3401 ***********************************************************************/
3403 /* Set up iterator IT from `display' property at its current position.
3404 Called from handle_stop.
3405 We return HANDLED_RETURN if some part of the display property
3406 overrides the display of the buffer text itself.
3407 Otherwise we return HANDLED_NORMALLY. */
3409 static enum prop_handled
3410 handle_display_prop (it)
3411 struct it *it;
3413 Lisp_Object prop, object;
3414 struct text_pos *position;
3415 /* Nonzero if some property replaces the display of the text itself. */
3416 int display_replaced_p = 0;
3418 if (STRINGP (it->string))
3420 object = it->string;
3421 position = &it->current.string_pos;
3423 else
3425 object = it->w->buffer;
3426 position = &it->current.pos;
3429 /* Reset those iterator values set from display property values. */
3430 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3431 it->space_width = Qnil;
3432 it->font_height = Qnil;
3433 it->voffset = 0;
3435 /* We don't support recursive `display' properties, i.e. string
3436 values that have a string `display' property, that have a string
3437 `display' property etc. */
3438 if (!it->string_from_display_prop_p)
3439 it->area = TEXT_AREA;
3441 prop = Fget_char_property (make_number (position->charpos),
3442 Qdisplay, object);
3443 if (NILP (prop))
3444 return HANDLED_NORMALLY;
3446 if (CONSP (prop)
3447 /* Simple properties. */
3448 && !EQ (XCAR (prop), Qimage)
3449 && !EQ (XCAR (prop), Qspace)
3450 && !EQ (XCAR (prop), Qwhen)
3451 && !EQ (XCAR (prop), Qslice)
3452 && !EQ (XCAR (prop), Qspace_width)
3453 && !EQ (XCAR (prop), Qheight)
3454 && !EQ (XCAR (prop), Qraise)
3455 /* Marginal area specifications. */
3456 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3457 && !EQ (XCAR (prop), Qleft_fringe)
3458 && !EQ (XCAR (prop), Qright_fringe)
3459 && !NILP (XCAR (prop)))
3461 for (; CONSP (prop); prop = XCDR (prop))
3463 if (handle_single_display_spec (it, XCAR (prop), object,
3464 position, display_replaced_p))
3465 display_replaced_p = 1;
3468 else if (VECTORP (prop))
3470 int i;
3471 for (i = 0; i < ASIZE (prop); ++i)
3472 if (handle_single_display_spec (it, AREF (prop, i), object,
3473 position, display_replaced_p))
3474 display_replaced_p = 1;
3476 else
3478 int ret = handle_single_display_spec (it, prop, object, position, 0);
3479 if (ret < 0) /* Replaced by "", i.e. nothing. */
3480 return HANDLED_RECOMPUTE_PROPS;
3481 if (ret)
3482 display_replaced_p = 1;
3485 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3489 /* Value is the position of the end of the `display' property starting
3490 at START_POS in OBJECT. */
3492 static struct text_pos
3493 display_prop_end (it, object, start_pos)
3494 struct it *it;
3495 Lisp_Object object;
3496 struct text_pos start_pos;
3498 Lisp_Object end;
3499 struct text_pos end_pos;
3501 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3502 Qdisplay, object, Qnil);
3503 CHARPOS (end_pos) = XFASTINT (end);
3504 if (STRINGP (object))
3505 compute_string_pos (&end_pos, start_pos, it->string);
3506 else
3507 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3509 return end_pos;
3513 /* Set up IT from a single `display' specification PROP. OBJECT
3514 is the object in which the `display' property was found. *POSITION
3515 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3516 means that we previously saw a display specification which already
3517 replaced text display with something else, for example an image;
3518 we ignore such properties after the first one has been processed.
3520 If PROP is a `space' or `image' specification, and in some other
3521 cases too, set *POSITION to the position where the `display'
3522 property ends.
3524 Value is non-zero if something was found which replaces the display
3525 of buffer or string text. Specifically, the value is -1 if that
3526 "something" is "nothing". */
3528 static int
3529 handle_single_display_spec (it, spec, object, position,
3530 display_replaced_before_p)
3531 struct it *it;
3532 Lisp_Object spec;
3533 Lisp_Object object;
3534 struct text_pos *position;
3535 int display_replaced_before_p;
3537 Lisp_Object form;
3538 Lisp_Object location, value;
3539 struct text_pos start_pos;
3540 int valid_p;
3542 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3543 If the result is non-nil, use VALUE instead of SPEC. */
3544 form = Qt;
3545 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3547 spec = XCDR (spec);
3548 if (!CONSP (spec))
3549 return 0;
3550 form = XCAR (spec);
3551 spec = XCDR (spec);
3554 if (!NILP (form) && !EQ (form, Qt))
3556 int count = SPECPDL_INDEX ();
3557 struct gcpro gcpro1;
3559 /* Bind `object' to the object having the `display' property, a
3560 buffer or string. Bind `position' to the position in the
3561 object where the property was found, and `buffer-position'
3562 to the current position in the buffer. */
3563 specbind (Qobject, object);
3564 specbind (Qposition, make_number (CHARPOS (*position)));
3565 specbind (Qbuffer_position,
3566 make_number (STRINGP (object)
3567 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3568 GCPRO1 (form);
3569 form = safe_eval (form);
3570 UNGCPRO;
3571 unbind_to (count, Qnil);
3574 if (NILP (form))
3575 return 0;
3577 /* Handle `(height HEIGHT)' specifications. */
3578 if (CONSP (spec)
3579 && EQ (XCAR (spec), Qheight)
3580 && CONSP (XCDR (spec)))
3582 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3583 return 0;
3585 it->font_height = XCAR (XCDR (spec));
3586 if (!NILP (it->font_height))
3588 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3589 int new_height = -1;
3591 if (CONSP (it->font_height)
3592 && (EQ (XCAR (it->font_height), Qplus)
3593 || EQ (XCAR (it->font_height), Qminus))
3594 && CONSP (XCDR (it->font_height))
3595 && INTEGERP (XCAR (XCDR (it->font_height))))
3597 /* `(+ N)' or `(- N)' where N is an integer. */
3598 int steps = XINT (XCAR (XCDR (it->font_height)));
3599 if (EQ (XCAR (it->font_height), Qplus))
3600 steps = - steps;
3601 it->face_id = smaller_face (it->f, it->face_id, steps);
3603 else if (FUNCTIONP (it->font_height))
3605 /* Call function with current height as argument.
3606 Value is the new height. */
3607 Lisp_Object height;
3608 height = safe_call1 (it->font_height,
3609 face->lface[LFACE_HEIGHT_INDEX]);
3610 if (NUMBERP (height))
3611 new_height = XFLOATINT (height);
3613 else if (NUMBERP (it->font_height))
3615 /* Value is a multiple of the canonical char height. */
3616 struct face *face;
3618 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3619 new_height = (XFLOATINT (it->font_height)
3620 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3622 else
3624 /* Evaluate IT->font_height with `height' bound to the
3625 current specified height to get the new height. */
3626 int count = SPECPDL_INDEX ();
3628 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3629 value = safe_eval (it->font_height);
3630 unbind_to (count, Qnil);
3632 if (NUMBERP (value))
3633 new_height = XFLOATINT (value);
3636 if (new_height > 0)
3637 it->face_id = face_with_height (it->f, it->face_id, new_height);
3640 return 0;
3643 /* Handle `(space_width WIDTH)'. */
3644 if (CONSP (spec)
3645 && EQ (XCAR (spec), Qspace_width)
3646 && CONSP (XCDR (spec)))
3648 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3649 return 0;
3651 value = XCAR (XCDR (spec));
3652 if (NUMBERP (value) && XFLOATINT (value) > 0)
3653 it->space_width = value;
3655 return 0;
3658 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3659 if (CONSP (spec)
3660 && EQ (XCAR (spec), Qslice))
3662 Lisp_Object tem;
3664 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3665 return 0;
3667 if (tem = XCDR (spec), CONSP (tem))
3669 it->slice.x = XCAR (tem);
3670 if (tem = XCDR (tem), CONSP (tem))
3672 it->slice.y = XCAR (tem);
3673 if (tem = XCDR (tem), CONSP (tem))
3675 it->slice.width = XCAR (tem);
3676 if (tem = XCDR (tem), CONSP (tem))
3677 it->slice.height = XCAR (tem);
3682 return 0;
3685 /* Handle `(raise FACTOR)'. */
3686 if (CONSP (spec)
3687 && EQ (XCAR (spec), Qraise)
3688 && CONSP (XCDR (spec)))
3690 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3691 return 0;
3693 #ifdef HAVE_WINDOW_SYSTEM
3694 value = XCAR (XCDR (spec));
3695 if (NUMBERP (value))
3697 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3698 it->voffset = - (XFLOATINT (value)
3699 * (FONT_HEIGHT (face->font)));
3701 #endif /* HAVE_WINDOW_SYSTEM */
3703 return 0;
3706 /* Don't handle the other kinds of display specifications
3707 inside a string that we got from a `display' property. */
3708 if (it->string_from_display_prop_p)
3709 return 0;
3711 /* Characters having this form of property are not displayed, so
3712 we have to find the end of the property. */
3713 start_pos = *position;
3714 *position = display_prop_end (it, object, start_pos);
3715 value = Qnil;
3717 /* Stop the scan at that end position--we assume that all
3718 text properties change there. */
3719 it->stop_charpos = position->charpos;
3721 /* Handle `(left-fringe BITMAP [FACE])'
3722 and `(right-fringe BITMAP [FACE])'. */
3723 if (CONSP (spec)
3724 && (EQ (XCAR (spec), Qleft_fringe)
3725 || EQ (XCAR (spec), Qright_fringe))
3726 && CONSP (XCDR (spec)))
3728 int face_id = DEFAULT_FACE_ID;
3729 int fringe_bitmap;
3731 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3732 /* If we return here, POSITION has been advanced
3733 across the text with this property. */
3734 return 0;
3736 #ifdef HAVE_WINDOW_SYSTEM
3737 value = XCAR (XCDR (spec));
3738 if (!SYMBOLP (value)
3739 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3740 /* If we return here, POSITION has been advanced
3741 across the text with this property. */
3742 return 0;
3744 if (CONSP (XCDR (XCDR (spec))))
3746 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3747 int face_id2 = lookup_derived_face (it->f, face_name,
3748 'A', FRINGE_FACE_ID, 0);
3749 if (face_id2 >= 0)
3750 face_id = face_id2;
3753 /* Save current settings of IT so that we can restore them
3754 when we are finished with the glyph property value. */
3756 push_it (it);
3758 it->area = TEXT_AREA;
3759 it->what = IT_IMAGE;
3760 it->image_id = -1; /* no image */
3761 it->position = start_pos;
3762 it->object = NILP (object) ? it->w->buffer : object;
3763 it->method = GET_FROM_IMAGE;
3764 it->face_id = face_id;
3766 /* Say that we haven't consumed the characters with
3767 `display' property yet. The call to pop_it in
3768 set_iterator_to_next will clean this up. */
3769 *position = start_pos;
3771 if (EQ (XCAR (spec), Qleft_fringe))
3773 it->left_user_fringe_bitmap = fringe_bitmap;
3774 it->left_user_fringe_face_id = face_id;
3776 else
3778 it->right_user_fringe_bitmap = fringe_bitmap;
3779 it->right_user_fringe_face_id = face_id;
3781 #endif /* HAVE_WINDOW_SYSTEM */
3782 return 1;
3785 /* Prepare to handle `((margin left-margin) ...)',
3786 `((margin right-margin) ...)' and `((margin nil) ...)'
3787 prefixes for display specifications. */
3788 location = Qunbound;
3789 if (CONSP (spec) && CONSP (XCAR (spec)))
3791 Lisp_Object tem;
3793 value = XCDR (spec);
3794 if (CONSP (value))
3795 value = XCAR (value);
3797 tem = XCAR (spec);
3798 if (EQ (XCAR (tem), Qmargin)
3799 && (tem = XCDR (tem),
3800 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3801 (NILP (tem)
3802 || EQ (tem, Qleft_margin)
3803 || EQ (tem, Qright_margin))))
3804 location = tem;
3807 if (EQ (location, Qunbound))
3809 location = Qnil;
3810 value = spec;
3813 /* After this point, VALUE is the property after any
3814 margin prefix has been stripped. It must be a string,
3815 an image specification, or `(space ...)'.
3817 LOCATION specifies where to display: `left-margin',
3818 `right-margin' or nil. */
3820 valid_p = (STRINGP (value)
3821 #ifdef HAVE_WINDOW_SYSTEM
3822 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3823 #endif /* not HAVE_WINDOW_SYSTEM */
3824 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3826 if (valid_p && !display_replaced_before_p)
3828 /* Save current settings of IT so that we can restore them
3829 when we are finished with the glyph property value. */
3830 push_it (it);
3832 if (NILP (location))
3833 it->area = TEXT_AREA;
3834 else if (EQ (location, Qleft_margin))
3835 it->area = LEFT_MARGIN_AREA;
3836 else
3837 it->area = RIGHT_MARGIN_AREA;
3839 if (STRINGP (value))
3841 if (SCHARS (value) == 0)
3843 pop_it (it);
3844 return -1; /* Replaced by "", i.e. nothing. */
3846 it->string = value;
3847 it->multibyte_p = STRING_MULTIBYTE (it->string);
3848 it->current.overlay_string_index = -1;
3849 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3850 it->end_charpos = it->string_nchars = SCHARS (it->string);
3851 it->method = GET_FROM_STRING;
3852 it->stop_charpos = 0;
3853 it->string_from_display_prop_p = 1;
3854 /* Say that we haven't consumed the characters with
3855 `display' property yet. The call to pop_it in
3856 set_iterator_to_next will clean this up. */
3857 *position = start_pos;
3859 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3861 it->method = GET_FROM_STRETCH;
3862 it->object = value;
3863 it->current.pos = it->position = start_pos;
3865 #ifdef HAVE_WINDOW_SYSTEM
3866 else
3868 it->what = IT_IMAGE;
3869 it->image_id = lookup_image (it->f, value);
3870 it->position = start_pos;
3871 it->object = NILP (object) ? it->w->buffer : object;
3872 it->method = GET_FROM_IMAGE;
3874 /* Say that we haven't consumed the characters with
3875 `display' property yet. The call to pop_it in
3876 set_iterator_to_next will clean this up. */
3877 *position = start_pos;
3879 #endif /* HAVE_WINDOW_SYSTEM */
3881 return 1;
3884 /* Invalid property or property not supported. Restore
3885 POSITION to what it was before. */
3886 *position = start_pos;
3887 return 0;
3891 /* Check if SPEC is a display specification value whose text should be
3892 treated as intangible. */
3894 static int
3895 single_display_spec_intangible_p (prop)
3896 Lisp_Object prop;
3898 /* Skip over `when FORM'. */
3899 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3901 prop = XCDR (prop);
3902 if (!CONSP (prop))
3903 return 0;
3904 prop = XCDR (prop);
3907 if (STRINGP (prop))
3908 return 1;
3910 if (!CONSP (prop))
3911 return 0;
3913 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3914 we don't need to treat text as intangible. */
3915 if (EQ (XCAR (prop), Qmargin))
3917 prop = XCDR (prop);
3918 if (!CONSP (prop))
3919 return 0;
3921 prop = XCDR (prop);
3922 if (!CONSP (prop)
3923 || EQ (XCAR (prop), Qleft_margin)
3924 || EQ (XCAR (prop), Qright_margin))
3925 return 0;
3928 return (CONSP (prop)
3929 && (EQ (XCAR (prop), Qimage)
3930 || EQ (XCAR (prop), Qspace)));
3934 /* Check if PROP is a display property value whose text should be
3935 treated as intangible. */
3938 display_prop_intangible_p (prop)
3939 Lisp_Object prop;
3941 if (CONSP (prop)
3942 && CONSP (XCAR (prop))
3943 && !EQ (Qmargin, XCAR (XCAR (prop))))
3945 /* A list of sub-properties. */
3946 while (CONSP (prop))
3948 if (single_display_spec_intangible_p (XCAR (prop)))
3949 return 1;
3950 prop = XCDR (prop);
3953 else if (VECTORP (prop))
3955 /* A vector of sub-properties. */
3956 int i;
3957 for (i = 0; i < ASIZE (prop); ++i)
3958 if (single_display_spec_intangible_p (AREF (prop, i)))
3959 return 1;
3961 else
3962 return single_display_spec_intangible_p (prop);
3964 return 0;
3968 /* Return 1 if PROP is a display sub-property value containing STRING. */
3970 static int
3971 single_display_spec_string_p (prop, string)
3972 Lisp_Object prop, string;
3974 if (EQ (string, prop))
3975 return 1;
3977 /* Skip over `when FORM'. */
3978 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3980 prop = XCDR (prop);
3981 if (!CONSP (prop))
3982 return 0;
3983 prop = XCDR (prop);
3986 if (CONSP (prop))
3987 /* Skip over `margin LOCATION'. */
3988 if (EQ (XCAR (prop), Qmargin))
3990 prop = XCDR (prop);
3991 if (!CONSP (prop))
3992 return 0;
3994 prop = XCDR (prop);
3995 if (!CONSP (prop))
3996 return 0;
3999 return CONSP (prop) && EQ (XCAR (prop), string);
4003 /* Return 1 if STRING appears in the `display' property PROP. */
4005 static int
4006 display_prop_string_p (prop, string)
4007 Lisp_Object prop, string;
4009 if (CONSP (prop)
4010 && CONSP (XCAR (prop))
4011 && !EQ (Qmargin, XCAR (XCAR (prop))))
4013 /* A list of sub-properties. */
4014 while (CONSP (prop))
4016 if (single_display_spec_string_p (XCAR (prop), string))
4017 return 1;
4018 prop = XCDR (prop);
4021 else if (VECTORP (prop))
4023 /* A vector of sub-properties. */
4024 int i;
4025 for (i = 0; i < ASIZE (prop); ++i)
4026 if (single_display_spec_string_p (AREF (prop, i), string))
4027 return 1;
4029 else
4030 return single_display_spec_string_p (prop, string);
4032 return 0;
4036 /* Determine from which buffer position in W's buffer STRING comes
4037 from. AROUND_CHARPOS is an approximate position where it could
4038 be from. Value is the buffer position or 0 if it couldn't be
4039 determined.
4041 W's buffer must be current.
4043 This function is necessary because we don't record buffer positions
4044 in glyphs generated from strings (to keep struct glyph small).
4045 This function may only use code that doesn't eval because it is
4046 called asynchronously from note_mouse_highlight. */
4049 string_buffer_position (w, string, around_charpos)
4050 struct window *w;
4051 Lisp_Object string;
4052 int around_charpos;
4054 Lisp_Object limit, prop, pos;
4055 const int MAX_DISTANCE = 1000;
4056 int found = 0;
4058 pos = make_number (around_charpos);
4059 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4060 while (!found && !EQ (pos, limit))
4062 prop = Fget_char_property (pos, Qdisplay, Qnil);
4063 if (!NILP (prop) && display_prop_string_p (prop, string))
4064 found = 1;
4065 else
4066 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4069 if (!found)
4071 pos = make_number (around_charpos);
4072 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4073 while (!found && !EQ (pos, limit))
4075 prop = Fget_char_property (pos, Qdisplay, Qnil);
4076 if (!NILP (prop) && display_prop_string_p (prop, string))
4077 found = 1;
4078 else
4079 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4080 limit);
4084 return found ? XINT (pos) : 0;
4089 /***********************************************************************
4090 `composition' property
4091 ***********************************************************************/
4093 /* Set up iterator IT from `composition' property at its current
4094 position. Called from handle_stop. */
4096 static enum prop_handled
4097 handle_composition_prop (it)
4098 struct it *it;
4100 Lisp_Object prop, string;
4101 int pos, pos_byte, end;
4102 enum prop_handled handled = HANDLED_NORMALLY;
4104 if (STRINGP (it->string))
4106 pos = IT_STRING_CHARPOS (*it);
4107 pos_byte = IT_STRING_BYTEPOS (*it);
4108 string = it->string;
4110 else
4112 pos = IT_CHARPOS (*it);
4113 pos_byte = IT_BYTEPOS (*it);
4114 string = Qnil;
4117 /* If there's a valid composition and point is not inside of the
4118 composition (in the case that the composition is from the current
4119 buffer), draw a glyph composed from the composition components. */
4120 if (find_composition (pos, -1, &pos, &end, &prop, string)
4121 && COMPOSITION_VALID_P (pos, end, prop)
4122 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4124 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4126 if (id >= 0)
4128 it->method = GET_FROM_COMPOSITION;
4129 it->cmp_id = id;
4130 it->cmp_len = COMPOSITION_LENGTH (prop);
4131 /* For a terminal, draw only the first character of the
4132 components. */
4133 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4134 it->len = (STRINGP (it->string)
4135 ? string_char_to_byte (it->string, end)
4136 : CHAR_TO_BYTE (end)) - pos_byte;
4137 it->stop_charpos = end;
4138 handled = HANDLED_RETURN;
4142 return handled;
4147 /***********************************************************************
4148 Overlay strings
4149 ***********************************************************************/
4151 /* The following structure is used to record overlay strings for
4152 later sorting in load_overlay_strings. */
4154 struct overlay_entry
4156 Lisp_Object overlay;
4157 Lisp_Object string;
4158 int priority;
4159 int after_string_p;
4163 /* Set up iterator IT from overlay strings at its current position.
4164 Called from handle_stop. */
4166 static enum prop_handled
4167 handle_overlay_change (it)
4168 struct it *it;
4170 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4171 return HANDLED_RECOMPUTE_PROPS;
4172 else
4173 return HANDLED_NORMALLY;
4177 /* Set up the next overlay string for delivery by IT, if there is an
4178 overlay string to deliver. Called by set_iterator_to_next when the
4179 end of the current overlay string is reached. If there are more
4180 overlay strings to display, IT->string and
4181 IT->current.overlay_string_index are set appropriately here.
4182 Otherwise IT->string is set to nil. */
4184 static void
4185 next_overlay_string (it)
4186 struct it *it;
4188 ++it->current.overlay_string_index;
4189 if (it->current.overlay_string_index == it->n_overlay_strings)
4191 /* No more overlay strings. Restore IT's settings to what
4192 they were before overlay strings were processed, and
4193 continue to deliver from current_buffer. */
4194 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4196 pop_it (it);
4197 xassert (it->stop_charpos >= BEGV
4198 && it->stop_charpos <= it->end_charpos);
4199 it->string = Qnil;
4200 it->current.overlay_string_index = -1;
4201 SET_TEXT_POS (it->current.string_pos, -1, -1);
4202 it->n_overlay_strings = 0;
4203 it->method = GET_FROM_BUFFER;
4205 /* If we're at the end of the buffer, record that we have
4206 processed the overlay strings there already, so that
4207 next_element_from_buffer doesn't try it again. */
4208 if (IT_CHARPOS (*it) >= it->end_charpos)
4209 it->overlay_strings_at_end_processed_p = 1;
4211 /* If we have to display `...' for invisible text, set
4212 the iterator up for that. */
4213 if (display_ellipsis_p)
4214 setup_for_ellipsis (it, 0);
4216 else
4218 /* There are more overlay strings to process. If
4219 IT->current.overlay_string_index has advanced to a position
4220 where we must load IT->overlay_strings with more strings, do
4221 it. */
4222 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4224 if (it->current.overlay_string_index && i == 0)
4225 load_overlay_strings (it, 0);
4227 /* Initialize IT to deliver display elements from the overlay
4228 string. */
4229 it->string = it->overlay_strings[i];
4230 it->multibyte_p = STRING_MULTIBYTE (it->string);
4231 SET_TEXT_POS (it->current.string_pos, 0, 0);
4232 it->method = GET_FROM_STRING;
4233 it->stop_charpos = 0;
4236 CHECK_IT (it);
4240 /* Compare two overlay_entry structures E1 and E2. Used as a
4241 comparison function for qsort in load_overlay_strings. Overlay
4242 strings for the same position are sorted so that
4244 1. All after-strings come in front of before-strings, except
4245 when they come from the same overlay.
4247 2. Within after-strings, strings are sorted so that overlay strings
4248 from overlays with higher priorities come first.
4250 2. Within before-strings, strings are sorted so that overlay
4251 strings from overlays with higher priorities come last.
4253 Value is analogous to strcmp. */
4256 static int
4257 compare_overlay_entries (e1, e2)
4258 void *e1, *e2;
4260 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4261 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4262 int result;
4264 if (entry1->after_string_p != entry2->after_string_p)
4266 /* Let after-strings appear in front of before-strings if
4267 they come from different overlays. */
4268 if (EQ (entry1->overlay, entry2->overlay))
4269 result = entry1->after_string_p ? 1 : -1;
4270 else
4271 result = entry1->after_string_p ? -1 : 1;
4273 else if (entry1->after_string_p)
4274 /* After-strings sorted in order of decreasing priority. */
4275 result = entry2->priority - entry1->priority;
4276 else
4277 /* Before-strings sorted in order of increasing priority. */
4278 result = entry1->priority - entry2->priority;
4280 return result;
4284 /* Load the vector IT->overlay_strings with overlay strings from IT's
4285 current buffer position, or from CHARPOS if that is > 0. Set
4286 IT->n_overlays to the total number of overlay strings found.
4288 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4289 a time. On entry into load_overlay_strings,
4290 IT->current.overlay_string_index gives the number of overlay
4291 strings that have already been loaded by previous calls to this
4292 function.
4294 IT->add_overlay_start contains an additional overlay start
4295 position to consider for taking overlay strings from, if non-zero.
4296 This position comes into play when the overlay has an `invisible'
4297 property, and both before and after-strings. When we've skipped to
4298 the end of the overlay, because of its `invisible' property, we
4299 nevertheless want its before-string to appear.
4300 IT->add_overlay_start will contain the overlay start position
4301 in this case.
4303 Overlay strings are sorted so that after-string strings come in
4304 front of before-string strings. Within before and after-strings,
4305 strings are sorted by overlay priority. See also function
4306 compare_overlay_entries. */
4308 static void
4309 load_overlay_strings (it, charpos)
4310 struct it *it;
4311 int charpos;
4313 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4314 Lisp_Object overlay, window, str, invisible;
4315 struct Lisp_Overlay *ov;
4316 int start, end;
4317 int size = 20;
4318 int n = 0, i, j, invis_p;
4319 struct overlay_entry *entries
4320 = (struct overlay_entry *) alloca (size * sizeof *entries);
4322 if (charpos <= 0)
4323 charpos = IT_CHARPOS (*it);
4325 /* Append the overlay string STRING of overlay OVERLAY to vector
4326 `entries' which has size `size' and currently contains `n'
4327 elements. AFTER_P non-zero means STRING is an after-string of
4328 OVERLAY. */
4329 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4330 do \
4332 Lisp_Object priority; \
4334 if (n == size) \
4336 int new_size = 2 * size; \
4337 struct overlay_entry *old = entries; \
4338 entries = \
4339 (struct overlay_entry *) alloca (new_size \
4340 * sizeof *entries); \
4341 bcopy (old, entries, size * sizeof *entries); \
4342 size = new_size; \
4345 entries[n].string = (STRING); \
4346 entries[n].overlay = (OVERLAY); \
4347 priority = Foverlay_get ((OVERLAY), Qpriority); \
4348 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4349 entries[n].after_string_p = (AFTER_P); \
4350 ++n; \
4352 while (0)
4354 /* Process overlay before the overlay center. */
4355 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4357 XSETMISC (overlay, ov);
4358 xassert (OVERLAYP (overlay));
4359 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4360 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4362 if (end < charpos)
4363 break;
4365 /* Skip this overlay if it doesn't start or end at IT's current
4366 position. */
4367 if (end != charpos && start != charpos)
4368 continue;
4370 /* Skip this overlay if it doesn't apply to IT->w. */
4371 window = Foverlay_get (overlay, Qwindow);
4372 if (WINDOWP (window) && XWINDOW (window) != it->w)
4373 continue;
4375 /* If the text ``under'' the overlay is invisible, both before-
4376 and after-strings from this overlay are visible; start and
4377 end position are indistinguishable. */
4378 invisible = Foverlay_get (overlay, Qinvisible);
4379 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4381 /* If overlay has a non-empty before-string, record it. */
4382 if ((start == charpos || (end == charpos && invis_p))
4383 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4384 && SCHARS (str))
4385 RECORD_OVERLAY_STRING (overlay, str, 0);
4387 /* If overlay has a non-empty after-string, record it. */
4388 if ((end == charpos || (start == charpos && invis_p))
4389 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4390 && SCHARS (str))
4391 RECORD_OVERLAY_STRING (overlay, str, 1);
4394 /* Process overlays after the overlay center. */
4395 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4397 XSETMISC (overlay, ov);
4398 xassert (OVERLAYP (overlay));
4399 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4400 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4402 if (start > charpos)
4403 break;
4405 /* Skip this overlay if it doesn't start or end at IT's current
4406 position. */
4407 if (end != charpos && start != charpos)
4408 continue;
4410 /* Skip this overlay if it doesn't apply to IT->w. */
4411 window = Foverlay_get (overlay, Qwindow);
4412 if (WINDOWP (window) && XWINDOW (window) != it->w)
4413 continue;
4415 /* If the text ``under'' the overlay is invisible, it has a zero
4416 dimension, and both before- and after-strings apply. */
4417 invisible = Foverlay_get (overlay, Qinvisible);
4418 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4420 /* If overlay has a non-empty before-string, record it. */
4421 if ((start == charpos || (end == charpos && invis_p))
4422 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4423 && SCHARS (str))
4424 RECORD_OVERLAY_STRING (overlay, str, 0);
4426 /* If overlay has a non-empty after-string, record it. */
4427 if ((end == charpos || (start == charpos && invis_p))
4428 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4429 && SCHARS (str))
4430 RECORD_OVERLAY_STRING (overlay, str, 1);
4433 #undef RECORD_OVERLAY_STRING
4435 /* Sort entries. */
4436 if (n > 1)
4437 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4439 /* Record the total number of strings to process. */
4440 it->n_overlay_strings = n;
4442 /* IT->current.overlay_string_index is the number of overlay strings
4443 that have already been consumed by IT. Copy some of the
4444 remaining overlay strings to IT->overlay_strings. */
4445 i = 0;
4446 j = it->current.overlay_string_index;
4447 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4448 it->overlay_strings[i++] = entries[j++].string;
4450 CHECK_IT (it);
4454 /* Get the first chunk of overlay strings at IT's current buffer
4455 position, or at CHARPOS if that is > 0. Value is non-zero if at
4456 least one overlay string was found. */
4458 static int
4459 get_overlay_strings (it, charpos)
4460 struct it *it;
4461 int charpos;
4463 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4464 process. This fills IT->overlay_strings with strings, and sets
4465 IT->n_overlay_strings to the total number of strings to process.
4466 IT->pos.overlay_string_index has to be set temporarily to zero
4467 because load_overlay_strings needs this; it must be set to -1
4468 when no overlay strings are found because a zero value would
4469 indicate a position in the first overlay string. */
4470 it->current.overlay_string_index = 0;
4471 load_overlay_strings (it, charpos);
4473 /* If we found overlay strings, set up IT to deliver display
4474 elements from the first one. Otherwise set up IT to deliver
4475 from current_buffer. */
4476 if (it->n_overlay_strings)
4478 /* Make sure we know settings in current_buffer, so that we can
4479 restore meaningful values when we're done with the overlay
4480 strings. */
4481 compute_stop_pos (it);
4482 xassert (it->face_id >= 0);
4484 /* Save IT's settings. They are restored after all overlay
4485 strings have been processed. */
4486 xassert (it->sp == 0);
4487 push_it (it);
4489 /* Set up IT to deliver display elements from the first overlay
4490 string. */
4491 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4492 it->string = it->overlay_strings[0];
4493 it->stop_charpos = 0;
4494 xassert (STRINGP (it->string));
4495 it->end_charpos = SCHARS (it->string);
4496 it->multibyte_p = STRING_MULTIBYTE (it->string);
4497 it->method = GET_FROM_STRING;
4499 else
4501 it->string = Qnil;
4502 it->current.overlay_string_index = -1;
4503 it->method = GET_FROM_BUFFER;
4506 CHECK_IT (it);
4508 /* Value is non-zero if we found at least one overlay string. */
4509 return STRINGP (it->string);
4514 /***********************************************************************
4515 Saving and restoring state
4516 ***********************************************************************/
4518 /* Save current settings of IT on IT->stack. Called, for example,
4519 before setting up IT for an overlay string, to be able to restore
4520 IT's settings to what they were after the overlay string has been
4521 processed. */
4523 static void
4524 push_it (it)
4525 struct it *it;
4527 struct iterator_stack_entry *p;
4529 xassert (it->sp < 2);
4530 p = it->stack + it->sp;
4532 p->stop_charpos = it->stop_charpos;
4533 xassert (it->face_id >= 0);
4534 p->face_id = it->face_id;
4535 p->string = it->string;
4536 p->pos = it->current;
4537 p->end_charpos = it->end_charpos;
4538 p->string_nchars = it->string_nchars;
4539 p->area = it->area;
4540 p->multibyte_p = it->multibyte_p;
4541 p->slice = it->slice;
4542 p->space_width = it->space_width;
4543 p->font_height = it->font_height;
4544 p->voffset = it->voffset;
4545 p->string_from_display_prop_p = it->string_from_display_prop_p;
4546 p->display_ellipsis_p = 0;
4547 ++it->sp;
4551 /* Restore IT's settings from IT->stack. Called, for example, when no
4552 more overlay strings must be processed, and we return to delivering
4553 display elements from a buffer, or when the end of a string from a
4554 `display' property is reached and we return to delivering display
4555 elements from an overlay string, or from a buffer. */
4557 static void
4558 pop_it (it)
4559 struct it *it;
4561 struct iterator_stack_entry *p;
4563 xassert (it->sp > 0);
4564 --it->sp;
4565 p = it->stack + it->sp;
4566 it->stop_charpos = p->stop_charpos;
4567 it->face_id = p->face_id;
4568 it->string = p->string;
4569 it->current = p->pos;
4570 it->end_charpos = p->end_charpos;
4571 it->string_nchars = p->string_nchars;
4572 it->area = p->area;
4573 it->multibyte_p = p->multibyte_p;
4574 it->slice = p->slice;
4575 it->space_width = p->space_width;
4576 it->font_height = p->font_height;
4577 it->voffset = p->voffset;
4578 it->string_from_display_prop_p = p->string_from_display_prop_p;
4583 /***********************************************************************
4584 Moving over lines
4585 ***********************************************************************/
4587 /* Set IT's current position to the previous line start. */
4589 static void
4590 back_to_previous_line_start (it)
4591 struct it *it;
4593 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4594 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4598 /* Move IT to the next line start.
4600 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4601 we skipped over part of the text (as opposed to moving the iterator
4602 continuously over the text). Otherwise, don't change the value
4603 of *SKIPPED_P.
4605 Newlines may come from buffer text, overlay strings, or strings
4606 displayed via the `display' property. That's the reason we can't
4607 simply use find_next_newline_no_quit.
4609 Note that this function may not skip over invisible text that is so
4610 because of text properties and immediately follows a newline. If
4611 it would, function reseat_at_next_visible_line_start, when called
4612 from set_iterator_to_next, would effectively make invisible
4613 characters following a newline part of the wrong glyph row, which
4614 leads to wrong cursor motion. */
4616 static int
4617 forward_to_next_line_start (it, skipped_p)
4618 struct it *it;
4619 int *skipped_p;
4621 int old_selective, newline_found_p, n;
4622 const int MAX_NEWLINE_DISTANCE = 500;
4624 /* If already on a newline, just consume it to avoid unintended
4625 skipping over invisible text below. */
4626 if (it->what == IT_CHARACTER
4627 && it->c == '\n'
4628 && CHARPOS (it->position) == IT_CHARPOS (*it))
4630 set_iterator_to_next (it, 0);
4631 it->c = 0;
4632 return 1;
4635 /* Don't handle selective display in the following. It's (a)
4636 unnecessary because it's done by the caller, and (b) leads to an
4637 infinite recursion because next_element_from_ellipsis indirectly
4638 calls this function. */
4639 old_selective = it->selective;
4640 it->selective = 0;
4642 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4643 from buffer text. */
4644 for (n = newline_found_p = 0;
4645 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4646 n += STRINGP (it->string) ? 0 : 1)
4648 if (!get_next_display_element (it))
4649 return 0;
4650 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4651 set_iterator_to_next (it, 0);
4654 /* If we didn't find a newline near enough, see if we can use a
4655 short-cut. */
4656 if (!newline_found_p)
4658 int start = IT_CHARPOS (*it);
4659 int limit = find_next_newline_no_quit (start, 1);
4660 Lisp_Object pos;
4662 xassert (!STRINGP (it->string));
4664 /* If there isn't any `display' property in sight, and no
4665 overlays, we can just use the position of the newline in
4666 buffer text. */
4667 if (it->stop_charpos >= limit
4668 || ((pos = Fnext_single_property_change (make_number (start),
4669 Qdisplay,
4670 Qnil, make_number (limit)),
4671 NILP (pos))
4672 && next_overlay_change (start) == ZV))
4674 IT_CHARPOS (*it) = limit;
4675 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4676 *skipped_p = newline_found_p = 1;
4678 else
4680 while (get_next_display_element (it)
4681 && !newline_found_p)
4683 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4684 set_iterator_to_next (it, 0);
4689 it->selective = old_selective;
4690 return newline_found_p;
4694 /* Set IT's current position to the previous visible line start. Skip
4695 invisible text that is so either due to text properties or due to
4696 selective display. Caution: this does not change IT->current_x and
4697 IT->hpos. */
4699 static void
4700 back_to_previous_visible_line_start (it)
4701 struct it *it;
4703 while (IT_CHARPOS (*it) > BEGV)
4705 back_to_previous_line_start (it);
4706 if (IT_CHARPOS (*it) <= BEGV)
4707 break;
4709 /* If selective > 0, then lines indented more than that values
4710 are invisible. */
4711 if (it->selective > 0
4712 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4713 (double) it->selective)) /* iftc */
4714 continue;
4716 /* Check the newline before point for invisibility. */
4718 Lisp_Object prop;
4719 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4720 Qinvisible, it->window);
4721 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4722 continue;
4725 /* If newline has a display property that replaces the newline with something
4726 else (image or text), find start of overlay or interval and continue search
4727 from that point. */
4728 if (IT_CHARPOS (*it) > BEGV)
4730 struct it it2 = *it;
4731 int pos;
4732 int beg, end;
4733 Lisp_Object val, overlay;
4735 pos = --IT_CHARPOS (it2);
4736 --IT_BYTEPOS (it2);
4737 it2.sp = 0;
4738 if (handle_display_prop (&it2) == HANDLED_RETURN
4739 && !NILP (val = get_char_property_and_overlay
4740 (make_number (pos), Qdisplay, Qnil, &overlay))
4741 && (OVERLAYP (overlay)
4742 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4743 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4745 if (beg < BEGV)
4746 beg = BEGV;
4747 IT_CHARPOS (*it) = beg;
4748 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4749 continue;
4753 break;
4756 xassert (IT_CHARPOS (*it) >= BEGV);
4757 xassert (IT_CHARPOS (*it) == BEGV
4758 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4759 CHECK_IT (it);
4763 /* Reseat iterator IT at the previous visible line start. Skip
4764 invisible text that is so either due to text properties or due to
4765 selective display. At the end, update IT's overlay information,
4766 face information etc. */
4768 void
4769 reseat_at_previous_visible_line_start (it)
4770 struct it *it;
4772 back_to_previous_visible_line_start (it);
4773 reseat (it, it->current.pos, 1);
4774 CHECK_IT (it);
4778 /* Reseat iterator IT on the next visible line start in the current
4779 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4780 preceding the line start. Skip over invisible text that is so
4781 because of selective display. Compute faces, overlays etc at the
4782 new position. Note that this function does not skip over text that
4783 is invisible because of text properties. */
4785 static void
4786 reseat_at_next_visible_line_start (it, on_newline_p)
4787 struct it *it;
4788 int on_newline_p;
4790 int newline_found_p, skipped_p = 0;
4792 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4794 /* Skip over lines that are invisible because they are indented
4795 more than the value of IT->selective. */
4796 if (it->selective > 0)
4797 while (IT_CHARPOS (*it) < ZV
4798 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4799 (double) it->selective)) /* iftc */
4801 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4802 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4805 /* Position on the newline if that's what's requested. */
4806 if (on_newline_p && newline_found_p)
4808 if (STRINGP (it->string))
4810 if (IT_STRING_CHARPOS (*it) > 0)
4812 --IT_STRING_CHARPOS (*it);
4813 --IT_STRING_BYTEPOS (*it);
4816 else if (IT_CHARPOS (*it) > BEGV)
4818 --IT_CHARPOS (*it);
4819 --IT_BYTEPOS (*it);
4820 reseat (it, it->current.pos, 0);
4823 else if (skipped_p)
4824 reseat (it, it->current.pos, 0);
4826 CHECK_IT (it);
4831 /***********************************************************************
4832 Changing an iterator's position
4833 ***********************************************************************/
4835 /* Change IT's current position to POS in current_buffer. If FORCE_P
4836 is non-zero, always check for text properties at the new position.
4837 Otherwise, text properties are only looked up if POS >=
4838 IT->check_charpos of a property. */
4840 static void
4841 reseat (it, pos, force_p)
4842 struct it *it;
4843 struct text_pos pos;
4844 int force_p;
4846 int original_pos = IT_CHARPOS (*it);
4848 reseat_1 (it, pos, 0);
4850 /* Determine where to check text properties. Avoid doing it
4851 where possible because text property lookup is very expensive. */
4852 if (force_p
4853 || CHARPOS (pos) > it->stop_charpos
4854 || CHARPOS (pos) < original_pos)
4855 handle_stop (it);
4857 CHECK_IT (it);
4861 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4862 IT->stop_pos to POS, also. */
4864 static void
4865 reseat_1 (it, pos, set_stop_p)
4866 struct it *it;
4867 struct text_pos pos;
4868 int set_stop_p;
4870 /* Don't call this function when scanning a C string. */
4871 xassert (it->s == NULL);
4873 /* POS must be a reasonable value. */
4874 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4876 it->current.pos = it->position = pos;
4877 XSETBUFFER (it->object, current_buffer);
4878 it->end_charpos = ZV;
4879 it->dpvec = NULL;
4880 it->current.dpvec_index = -1;
4881 it->current.overlay_string_index = -1;
4882 IT_STRING_CHARPOS (*it) = -1;
4883 IT_STRING_BYTEPOS (*it) = -1;
4884 it->string = Qnil;
4885 it->method = GET_FROM_BUFFER;
4886 /* RMS: I added this to fix a bug in move_it_vertically_backward
4887 where it->area continued to relate to the starting point
4888 for the backward motion. Bug report from
4889 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4890 However, I am not sure whether reseat still does the right thing
4891 in general after this change. */
4892 it->area = TEXT_AREA;
4893 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4894 it->sp = 0;
4895 it->face_before_selective_p = 0;
4897 if (set_stop_p)
4898 it->stop_charpos = CHARPOS (pos);
4902 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4903 If S is non-null, it is a C string to iterate over. Otherwise,
4904 STRING gives a Lisp string to iterate over.
4906 If PRECISION > 0, don't return more then PRECISION number of
4907 characters from the string.
4909 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4910 characters have been returned. FIELD_WIDTH < 0 means an infinite
4911 field width.
4913 MULTIBYTE = 0 means disable processing of multibyte characters,
4914 MULTIBYTE > 0 means enable it,
4915 MULTIBYTE < 0 means use IT->multibyte_p.
4917 IT must be initialized via a prior call to init_iterator before
4918 calling this function. */
4920 static void
4921 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4922 struct it *it;
4923 unsigned char *s;
4924 Lisp_Object string;
4925 int charpos;
4926 int precision, field_width, multibyte;
4928 /* No region in strings. */
4929 it->region_beg_charpos = it->region_end_charpos = -1;
4931 /* No text property checks performed by default, but see below. */
4932 it->stop_charpos = -1;
4934 /* Set iterator position and end position. */
4935 bzero (&it->current, sizeof it->current);
4936 it->current.overlay_string_index = -1;
4937 it->current.dpvec_index = -1;
4938 xassert (charpos >= 0);
4940 /* If STRING is specified, use its multibyteness, otherwise use the
4941 setting of MULTIBYTE, if specified. */
4942 if (multibyte >= 0)
4943 it->multibyte_p = multibyte > 0;
4945 if (s == NULL)
4947 xassert (STRINGP (string));
4948 it->string = string;
4949 it->s = NULL;
4950 it->end_charpos = it->string_nchars = SCHARS (string);
4951 it->method = GET_FROM_STRING;
4952 it->current.string_pos = string_pos (charpos, string);
4954 else
4956 it->s = s;
4957 it->string = Qnil;
4959 /* Note that we use IT->current.pos, not it->current.string_pos,
4960 for displaying C strings. */
4961 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4962 if (it->multibyte_p)
4964 it->current.pos = c_string_pos (charpos, s, 1);
4965 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4967 else
4969 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4970 it->end_charpos = it->string_nchars = strlen (s);
4973 it->method = GET_FROM_C_STRING;
4976 /* PRECISION > 0 means don't return more than PRECISION characters
4977 from the string. */
4978 if (precision > 0 && it->end_charpos - charpos > precision)
4979 it->end_charpos = it->string_nchars = charpos + precision;
4981 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4982 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4983 FIELD_WIDTH < 0 means infinite field width. This is useful for
4984 padding with `-' at the end of a mode line. */
4985 if (field_width < 0)
4986 field_width = INFINITY;
4987 if (field_width > it->end_charpos - charpos)
4988 it->end_charpos = charpos + field_width;
4990 /* Use the standard display table for displaying strings. */
4991 if (DISP_TABLE_P (Vstandard_display_table))
4992 it->dp = XCHAR_TABLE (Vstandard_display_table);
4994 it->stop_charpos = charpos;
4995 CHECK_IT (it);
5000 /***********************************************************************
5001 Iteration
5002 ***********************************************************************/
5004 /* Map enum it_method value to corresponding next_element_from_* function. */
5006 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5008 next_element_from_buffer,
5009 next_element_from_display_vector,
5010 next_element_from_composition,
5011 next_element_from_string,
5012 next_element_from_c_string,
5013 next_element_from_image,
5014 next_element_from_stretch
5018 /* Load IT's display element fields with information about the next
5019 display element from the current position of IT. Value is zero if
5020 end of buffer (or C string) is reached. */
5023 get_next_display_element (it)
5024 struct it *it;
5026 /* Non-zero means that we found a display element. Zero means that
5027 we hit the end of what we iterate over. Performance note: the
5028 function pointer `method' used here turns out to be faster than
5029 using a sequence of if-statements. */
5030 int success_p;
5032 get_next:
5033 success_p = (*get_next_element[it->method]) (it);
5035 if (it->what == IT_CHARACTER)
5037 /* Map via display table or translate control characters.
5038 IT->c, IT->len etc. have been set to the next character by
5039 the function call above. If we have a display table, and it
5040 contains an entry for IT->c, translate it. Don't do this if
5041 IT->c itself comes from a display table, otherwise we could
5042 end up in an infinite recursion. (An alternative could be to
5043 count the recursion depth of this function and signal an
5044 error when a certain maximum depth is reached.) Is it worth
5045 it? */
5046 if (success_p && it->dpvec == NULL)
5048 Lisp_Object dv;
5050 if (it->dp
5051 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5052 VECTORP (dv)))
5054 struct Lisp_Vector *v = XVECTOR (dv);
5056 /* Return the first character from the display table
5057 entry, if not empty. If empty, don't display the
5058 current character. */
5059 if (v->size)
5061 it->dpvec_char_len = it->len;
5062 it->dpvec = v->contents;
5063 it->dpend = v->contents + v->size;
5064 it->current.dpvec_index = 0;
5065 it->dpvec_face_id = -1;
5066 it->saved_face_id = it->face_id;
5067 it->method = GET_FROM_DISPLAY_VECTOR;
5068 it->ellipsis_p = 0;
5070 else
5072 set_iterator_to_next (it, 0);
5074 goto get_next;
5077 /* Translate control characters into `\003' or `^C' form.
5078 Control characters coming from a display table entry are
5079 currently not translated because we use IT->dpvec to hold
5080 the translation. This could easily be changed but I
5081 don't believe that it is worth doing.
5083 If it->multibyte_p is nonzero, eight-bit characters and
5084 non-printable multibyte characters are also translated to
5085 octal form.
5087 If it->multibyte_p is zero, eight-bit characters that
5088 don't have corresponding multibyte char code are also
5089 translated to octal form. */
5090 else if ((it->c < ' '
5091 && (it->area != TEXT_AREA
5092 /* In mode line, treat \n like other crl chars. */
5093 || (it->c != '\t'
5094 && it->glyph_row && it->glyph_row->mode_line_p)
5095 || (it->c != '\n' && it->c != '\t')))
5096 || (it->multibyte_p
5097 ? ((it->c >= 127
5098 && it->len == 1)
5099 || !CHAR_PRINTABLE_P (it->c)
5100 || (!NILP (Vnobreak_char_display)
5101 && (it->c == 0x8a0 || it->c == 0x8ad
5102 || it->c == 0x920 || it->c == 0x92d
5103 || it->c == 0xe20 || it->c == 0xe2d
5104 || it->c == 0xf20 || it->c == 0xf2d)))
5105 : (it->c >= 127
5106 && (!unibyte_display_via_language_environment
5107 || it->c == unibyte_char_to_multibyte (it->c)))))
5109 /* IT->c is a control character which must be displayed
5110 either as '\003' or as `^C' where the '\\' and '^'
5111 can be defined in the display table. Fill
5112 IT->ctl_chars with glyphs for what we have to
5113 display. Then, set IT->dpvec to these glyphs. */
5114 GLYPH g;
5115 int ctl_len;
5116 int face_id, lface_id = 0 ;
5117 GLYPH escape_glyph;
5119 /* Handle control characters with ^. */
5121 if (it->c < 128 && it->ctl_arrow_p)
5123 g = '^'; /* default glyph for Control */
5124 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5125 if (it->dp
5126 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5127 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5129 g = XINT (DISP_CTRL_GLYPH (it->dp));
5130 lface_id = FAST_GLYPH_FACE (g);
5132 if (lface_id)
5134 g = FAST_GLYPH_CHAR (g);
5135 face_id = merge_faces (it->f, Qt, lface_id,
5136 it->face_id);
5138 else
5140 /* Merge the escape-glyph face into the current face. */
5141 face_id = merge_faces (it->f, Qescape_glyph, 0,
5142 it->face_id);
5145 XSETINT (it->ctl_chars[0], g);
5146 g = it->c ^ 0100;
5147 XSETINT (it->ctl_chars[1], g);
5148 ctl_len = 2;
5149 goto display_control;
5152 /* Handle non-break space in the mode where it only gets
5153 highlighting. */
5155 if (EQ (Vnobreak_char_display, Qt)
5156 && (it->c == 0x8a0 || it->c == 0x920
5157 || it->c == 0xe20 || it->c == 0xf20))
5159 /* Merge the no-break-space face into the current face. */
5160 face_id = merge_faces (it->f, Qnobreak_space, 0,
5161 it->face_id);
5163 g = it->c = ' ';
5164 XSETINT (it->ctl_chars[0], g);
5165 ctl_len = 1;
5166 goto display_control;
5169 /* Handle sequences that start with the "escape glyph". */
5171 /* the default escape glyph is \. */
5172 escape_glyph = '\\';
5174 if (it->dp
5175 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5176 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5178 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5179 lface_id = FAST_GLYPH_FACE (escape_glyph);
5181 if (lface_id)
5183 /* The display table specified a face.
5184 Merge it into face_id and also into escape_glyph. */
5185 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5186 face_id = merge_faces (it->f, Qt, lface_id,
5187 it->face_id);
5189 else
5191 /* Merge the escape-glyph face into the current face. */
5192 face_id = merge_faces (it->f, Qescape_glyph, 0,
5193 it->face_id);
5196 /* Handle soft hyphens in the mode where they only get
5197 highlighting. */
5199 if (EQ (Vnobreak_char_display, Qt)
5200 && (it->c == 0x8ad || it->c == 0x92d
5201 || it->c == 0xe2d || it->c == 0xf2d))
5203 g = it->c = '-';
5204 XSETINT (it->ctl_chars[0], g);
5205 ctl_len = 1;
5206 goto display_control;
5209 /* Handle non-break space and soft hyphen
5210 with the escape glyph. */
5212 if (it->c == 0x8a0 || it->c == 0x8ad
5213 || it->c == 0x920 || it->c == 0x92d
5214 || it->c == 0xe20 || it->c == 0xe2d
5215 || it->c == 0xf20 || it->c == 0xf2d)
5217 XSETINT (it->ctl_chars[0], escape_glyph);
5218 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5219 XSETINT (it->ctl_chars[1], g);
5220 ctl_len = 2;
5221 goto display_control;
5225 unsigned char str[MAX_MULTIBYTE_LENGTH];
5226 int len;
5227 int i;
5229 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5230 if (SINGLE_BYTE_CHAR_P (it->c))
5231 str[0] = it->c, len = 1;
5232 else
5234 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5235 if (len < 0)
5237 /* It's an invalid character, which shouldn't
5238 happen actually, but due to bugs it may
5239 happen. Let's print the char as is, there's
5240 not much meaningful we can do with it. */
5241 str[0] = it->c;
5242 str[1] = it->c >> 8;
5243 str[2] = it->c >> 16;
5244 str[3] = it->c >> 24;
5245 len = 4;
5249 for (i = 0; i < len; i++)
5251 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5252 /* Insert three more glyphs into IT->ctl_chars for
5253 the octal display of the character. */
5254 g = ((str[i] >> 6) & 7) + '0';
5255 XSETINT (it->ctl_chars[i * 4 + 1], g);
5256 g = ((str[i] >> 3) & 7) + '0';
5257 XSETINT (it->ctl_chars[i * 4 + 2], g);
5258 g = (str[i] & 7) + '0';
5259 XSETINT (it->ctl_chars[i * 4 + 3], g);
5261 ctl_len = len * 4;
5264 display_control:
5265 /* Set up IT->dpvec and return first character from it. */
5266 it->dpvec_char_len = it->len;
5267 it->dpvec = it->ctl_chars;
5268 it->dpend = it->dpvec + ctl_len;
5269 it->current.dpvec_index = 0;
5270 it->dpvec_face_id = face_id;
5271 it->saved_face_id = it->face_id;
5272 it->method = GET_FROM_DISPLAY_VECTOR;
5273 it->ellipsis_p = 0;
5274 goto get_next;
5278 /* Adjust face id for a multibyte character. There are no
5279 multibyte character in unibyte text. */
5280 if (it->multibyte_p
5281 && success_p
5282 && FRAME_WINDOW_P (it->f))
5284 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5285 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5289 /* Is this character the last one of a run of characters with
5290 box? If yes, set IT->end_of_box_run_p to 1. */
5291 if (it->face_box_p
5292 && it->s == NULL)
5294 int face_id;
5295 struct face *face;
5297 it->end_of_box_run_p
5298 = ((face_id = face_after_it_pos (it),
5299 face_id != it->face_id)
5300 && (face = FACE_FROM_ID (it->f, face_id),
5301 face->box == FACE_NO_BOX));
5304 /* Value is 0 if end of buffer or string reached. */
5305 return success_p;
5309 /* Move IT to the next display element.
5311 RESEAT_P non-zero means if called on a newline in buffer text,
5312 skip to the next visible line start.
5314 Functions get_next_display_element and set_iterator_to_next are
5315 separate because I find this arrangement easier to handle than a
5316 get_next_display_element function that also increments IT's
5317 position. The way it is we can first look at an iterator's current
5318 display element, decide whether it fits on a line, and if it does,
5319 increment the iterator position. The other way around we probably
5320 would either need a flag indicating whether the iterator has to be
5321 incremented the next time, or we would have to implement a
5322 decrement position function which would not be easy to write. */
5324 void
5325 set_iterator_to_next (it, reseat_p)
5326 struct it *it;
5327 int reseat_p;
5329 /* Reset flags indicating start and end of a sequence of characters
5330 with box. Reset them at the start of this function because
5331 moving the iterator to a new position might set them. */
5332 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5334 switch (it->method)
5336 case GET_FROM_BUFFER:
5337 /* The current display element of IT is a character from
5338 current_buffer. Advance in the buffer, and maybe skip over
5339 invisible lines that are so because of selective display. */
5340 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5341 reseat_at_next_visible_line_start (it, 0);
5342 else
5344 xassert (it->len != 0);
5345 IT_BYTEPOS (*it) += it->len;
5346 IT_CHARPOS (*it) += 1;
5347 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5349 break;
5351 case GET_FROM_COMPOSITION:
5352 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5353 if (STRINGP (it->string))
5355 IT_STRING_BYTEPOS (*it) += it->len;
5356 IT_STRING_CHARPOS (*it) += it->cmp_len;
5357 it->method = GET_FROM_STRING;
5358 goto consider_string_end;
5360 else
5362 IT_BYTEPOS (*it) += it->len;
5363 IT_CHARPOS (*it) += it->cmp_len;
5364 it->method = GET_FROM_BUFFER;
5366 break;
5368 case GET_FROM_C_STRING:
5369 /* Current display element of IT is from a C string. */
5370 IT_BYTEPOS (*it) += it->len;
5371 IT_CHARPOS (*it) += 1;
5372 break;
5374 case GET_FROM_DISPLAY_VECTOR:
5375 /* Current display element of IT is from a display table entry.
5376 Advance in the display table definition. Reset it to null if
5377 end reached, and continue with characters from buffers/
5378 strings. */
5379 ++it->current.dpvec_index;
5381 /* Restore face of the iterator to what they were before the
5382 display vector entry (these entries may contain faces). */
5383 it->face_id = it->saved_face_id;
5385 if (it->dpvec + it->current.dpvec_index == it->dpend)
5387 if (it->s)
5388 it->method = GET_FROM_C_STRING;
5389 else if (STRINGP (it->string))
5390 it->method = GET_FROM_STRING;
5391 else
5392 it->method = GET_FROM_BUFFER;
5394 it->dpvec = NULL;
5395 it->current.dpvec_index = -1;
5397 /* Skip over characters which were displayed via IT->dpvec. */
5398 if (it->dpvec_char_len < 0)
5399 reseat_at_next_visible_line_start (it, 1);
5400 else if (it->dpvec_char_len > 0)
5402 it->len = it->dpvec_char_len;
5403 set_iterator_to_next (it, reseat_p);
5406 /* Recheck faces after display vector */
5407 it->stop_charpos = IT_CHARPOS (*it);
5409 break;
5411 case GET_FROM_STRING:
5412 /* Current display element is a character from a Lisp string. */
5413 xassert (it->s == NULL && STRINGP (it->string));
5414 IT_STRING_BYTEPOS (*it) += it->len;
5415 IT_STRING_CHARPOS (*it) += 1;
5417 consider_string_end:
5419 if (it->current.overlay_string_index >= 0)
5421 /* IT->string is an overlay string. Advance to the
5422 next, if there is one. */
5423 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5424 next_overlay_string (it);
5426 else
5428 /* IT->string is not an overlay string. If we reached
5429 its end, and there is something on IT->stack, proceed
5430 with what is on the stack. This can be either another
5431 string, this time an overlay string, or a buffer. */
5432 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5433 && it->sp > 0)
5435 pop_it (it);
5436 if (STRINGP (it->string))
5437 goto consider_string_end;
5438 it->method = GET_FROM_BUFFER;
5441 break;
5443 case GET_FROM_IMAGE:
5444 case GET_FROM_STRETCH:
5445 /* The position etc with which we have to proceed are on
5446 the stack. The position may be at the end of a string,
5447 if the `display' property takes up the whole string. */
5448 xassert (it->sp > 0);
5449 pop_it (it);
5450 it->image_id = 0;
5451 if (STRINGP (it->string))
5453 it->method = GET_FROM_STRING;
5454 goto consider_string_end;
5456 it->method = GET_FROM_BUFFER;
5457 break;
5459 default:
5460 /* There are no other methods defined, so this should be a bug. */
5461 abort ();
5464 xassert (it->method != GET_FROM_STRING
5465 || (STRINGP (it->string)
5466 && IT_STRING_CHARPOS (*it) >= 0));
5469 /* Load IT's display element fields with information about the next
5470 display element which comes from a display table entry or from the
5471 result of translating a control character to one of the forms `^C'
5472 or `\003'.
5474 IT->dpvec holds the glyphs to return as characters.
5475 IT->saved_face_id holds the face id before the display vector--
5476 it is restored into IT->face_idin set_iterator_to_next. */
5478 static int
5479 next_element_from_display_vector (it)
5480 struct it *it;
5482 /* Precondition. */
5483 xassert (it->dpvec && it->current.dpvec_index >= 0);
5485 it->face_id = it->saved_face_id;
5487 if (INTEGERP (*it->dpvec)
5488 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5490 GLYPH g;
5492 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5493 it->c = FAST_GLYPH_CHAR (g);
5494 it->len = CHAR_BYTES (it->c);
5496 /* The entry may contain a face id to use. Such a face id is
5497 the id of a Lisp face, not a realized face. A face id of
5498 zero means no face is specified. */
5499 if (it->dpvec_face_id >= 0)
5500 it->face_id = it->dpvec_face_id;
5501 else
5503 int lface_id = FAST_GLYPH_FACE (g);
5504 if (lface_id > 0)
5505 it->face_id = merge_faces (it->f, Qt, lface_id,
5506 it->saved_face_id);
5509 else
5510 /* Display table entry is invalid. Return a space. */
5511 it->c = ' ', it->len = 1;
5513 /* Don't change position and object of the iterator here. They are
5514 still the values of the character that had this display table
5515 entry or was translated, and that's what we want. */
5516 it->what = IT_CHARACTER;
5517 return 1;
5521 /* Load IT with the next display element from Lisp string IT->string.
5522 IT->current.string_pos is the current position within the string.
5523 If IT->current.overlay_string_index >= 0, the Lisp string is an
5524 overlay string. */
5526 static int
5527 next_element_from_string (it)
5528 struct it *it;
5530 struct text_pos position;
5532 xassert (STRINGP (it->string));
5533 xassert (IT_STRING_CHARPOS (*it) >= 0);
5534 position = it->current.string_pos;
5536 /* Time to check for invisible text? */
5537 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5538 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5540 handle_stop (it);
5542 /* Since a handler may have changed IT->method, we must
5543 recurse here. */
5544 return get_next_display_element (it);
5547 if (it->current.overlay_string_index >= 0)
5549 /* Get the next character from an overlay string. In overlay
5550 strings, There is no field width or padding with spaces to
5551 do. */
5552 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5554 it->what = IT_EOB;
5555 return 0;
5557 else if (STRING_MULTIBYTE (it->string))
5559 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5560 const unsigned char *s = (SDATA (it->string)
5561 + IT_STRING_BYTEPOS (*it));
5562 it->c = string_char_and_length (s, remaining, &it->len);
5564 else
5566 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5567 it->len = 1;
5570 else
5572 /* Get the next character from a Lisp string that is not an
5573 overlay string. Such strings come from the mode line, for
5574 example. We may have to pad with spaces, or truncate the
5575 string. See also next_element_from_c_string. */
5576 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5578 it->what = IT_EOB;
5579 return 0;
5581 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5583 /* Pad with spaces. */
5584 it->c = ' ', it->len = 1;
5585 CHARPOS (position) = BYTEPOS (position) = -1;
5587 else if (STRING_MULTIBYTE (it->string))
5589 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5590 const unsigned char *s = (SDATA (it->string)
5591 + IT_STRING_BYTEPOS (*it));
5592 it->c = string_char_and_length (s, maxlen, &it->len);
5594 else
5596 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5597 it->len = 1;
5601 /* Record what we have and where it came from. Note that we store a
5602 buffer position in IT->position although it could arguably be a
5603 string position. */
5604 it->what = IT_CHARACTER;
5605 it->object = it->string;
5606 it->position = position;
5607 return 1;
5611 /* Load IT with next display element from C string IT->s.
5612 IT->string_nchars is the maximum number of characters to return
5613 from the string. IT->end_charpos may be greater than
5614 IT->string_nchars when this function is called, in which case we
5615 may have to return padding spaces. Value is zero if end of string
5616 reached, including padding spaces. */
5618 static int
5619 next_element_from_c_string (it)
5620 struct it *it;
5622 int success_p = 1;
5624 xassert (it->s);
5625 it->what = IT_CHARACTER;
5626 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5627 it->object = Qnil;
5629 /* IT's position can be greater IT->string_nchars in case a field
5630 width or precision has been specified when the iterator was
5631 initialized. */
5632 if (IT_CHARPOS (*it) >= it->end_charpos)
5634 /* End of the game. */
5635 it->what = IT_EOB;
5636 success_p = 0;
5638 else if (IT_CHARPOS (*it) >= it->string_nchars)
5640 /* Pad with spaces. */
5641 it->c = ' ', it->len = 1;
5642 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5644 else if (it->multibyte_p)
5646 /* Implementation note: The calls to strlen apparently aren't a
5647 performance problem because there is no noticeable performance
5648 difference between Emacs running in unibyte or multibyte mode. */
5649 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5650 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5651 maxlen, &it->len);
5653 else
5654 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5656 return success_p;
5660 /* Set up IT to return characters from an ellipsis, if appropriate.
5661 The definition of the ellipsis glyphs may come from a display table
5662 entry. This function Fills IT with the first glyph from the
5663 ellipsis if an ellipsis is to be displayed. */
5665 static int
5666 next_element_from_ellipsis (it)
5667 struct it *it;
5669 if (it->selective_display_ellipsis_p)
5670 setup_for_ellipsis (it, it->len);
5671 else
5673 /* The face at the current position may be different from the
5674 face we find after the invisible text. Remember what it
5675 was in IT->saved_face_id, and signal that it's there by
5676 setting face_before_selective_p. */
5677 it->saved_face_id = it->face_id;
5678 it->method = GET_FROM_BUFFER;
5679 reseat_at_next_visible_line_start (it, 1);
5680 it->face_before_selective_p = 1;
5683 return get_next_display_element (it);
5687 /* Deliver an image display element. The iterator IT is already
5688 filled with image information (done in handle_display_prop). Value
5689 is always 1. */
5692 static int
5693 next_element_from_image (it)
5694 struct it *it;
5696 it->what = IT_IMAGE;
5697 return 1;
5701 /* Fill iterator IT with next display element from a stretch glyph
5702 property. IT->object is the value of the text property. Value is
5703 always 1. */
5705 static int
5706 next_element_from_stretch (it)
5707 struct it *it;
5709 it->what = IT_STRETCH;
5710 return 1;
5714 /* Load IT with the next display element from current_buffer. Value
5715 is zero if end of buffer reached. IT->stop_charpos is the next
5716 position at which to stop and check for text properties or buffer
5717 end. */
5719 static int
5720 next_element_from_buffer (it)
5721 struct it *it;
5723 int success_p = 1;
5725 /* Check this assumption, otherwise, we would never enter the
5726 if-statement, below. */
5727 xassert (IT_CHARPOS (*it) >= BEGV
5728 && IT_CHARPOS (*it) <= it->stop_charpos);
5730 if (IT_CHARPOS (*it) >= it->stop_charpos)
5732 if (IT_CHARPOS (*it) >= it->end_charpos)
5734 int overlay_strings_follow_p;
5736 /* End of the game, except when overlay strings follow that
5737 haven't been returned yet. */
5738 if (it->overlay_strings_at_end_processed_p)
5739 overlay_strings_follow_p = 0;
5740 else
5742 it->overlay_strings_at_end_processed_p = 1;
5743 overlay_strings_follow_p = get_overlay_strings (it, 0);
5746 if (overlay_strings_follow_p)
5747 success_p = get_next_display_element (it);
5748 else
5750 it->what = IT_EOB;
5751 it->position = it->current.pos;
5752 success_p = 0;
5755 else
5757 handle_stop (it);
5758 return get_next_display_element (it);
5761 else
5763 /* No face changes, overlays etc. in sight, so just return a
5764 character from current_buffer. */
5765 unsigned char *p;
5767 /* Maybe run the redisplay end trigger hook. Performance note:
5768 This doesn't seem to cost measurable time. */
5769 if (it->redisplay_end_trigger_charpos
5770 && it->glyph_row
5771 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5772 run_redisplay_end_trigger_hook (it);
5774 /* Get the next character, maybe multibyte. */
5775 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5776 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5778 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5779 - IT_BYTEPOS (*it));
5780 it->c = string_char_and_length (p, maxlen, &it->len);
5782 else
5783 it->c = *p, it->len = 1;
5785 /* Record what we have and where it came from. */
5786 it->what = IT_CHARACTER;;
5787 it->object = it->w->buffer;
5788 it->position = it->current.pos;
5790 /* Normally we return the character found above, except when we
5791 really want to return an ellipsis for selective display. */
5792 if (it->selective)
5794 if (it->c == '\n')
5796 /* A value of selective > 0 means hide lines indented more
5797 than that number of columns. */
5798 if (it->selective > 0
5799 && IT_CHARPOS (*it) + 1 < ZV
5800 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5801 IT_BYTEPOS (*it) + 1,
5802 (double) it->selective)) /* iftc */
5804 success_p = next_element_from_ellipsis (it);
5805 it->dpvec_char_len = -1;
5808 else if (it->c == '\r' && it->selective == -1)
5810 /* A value of selective == -1 means that everything from the
5811 CR to the end of the line is invisible, with maybe an
5812 ellipsis displayed for it. */
5813 success_p = next_element_from_ellipsis (it);
5814 it->dpvec_char_len = -1;
5819 /* Value is zero if end of buffer reached. */
5820 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5821 return success_p;
5825 /* Run the redisplay end trigger hook for IT. */
5827 static void
5828 run_redisplay_end_trigger_hook (it)
5829 struct it *it;
5831 Lisp_Object args[3];
5833 /* IT->glyph_row should be non-null, i.e. we should be actually
5834 displaying something, or otherwise we should not run the hook. */
5835 xassert (it->glyph_row);
5837 /* Set up hook arguments. */
5838 args[0] = Qredisplay_end_trigger_functions;
5839 args[1] = it->window;
5840 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5841 it->redisplay_end_trigger_charpos = 0;
5843 /* Since we are *trying* to run these functions, don't try to run
5844 them again, even if they get an error. */
5845 it->w->redisplay_end_trigger = Qnil;
5846 Frun_hook_with_args (3, args);
5848 /* Notice if it changed the face of the character we are on. */
5849 handle_face_prop (it);
5853 /* Deliver a composition display element. The iterator IT is already
5854 filled with composition information (done in
5855 handle_composition_prop). Value is always 1. */
5857 static int
5858 next_element_from_composition (it)
5859 struct it *it;
5861 it->what = IT_COMPOSITION;
5862 it->position = (STRINGP (it->string)
5863 ? it->current.string_pos
5864 : it->current.pos);
5865 return 1;
5870 /***********************************************************************
5871 Moving an iterator without producing glyphs
5872 ***********************************************************************/
5874 /* Move iterator IT to a specified buffer or X position within one
5875 line on the display without producing glyphs.
5877 OP should be a bit mask including some or all of these bits:
5878 MOVE_TO_X: Stop on reaching x-position TO_X.
5879 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5880 Regardless of OP's value, stop in reaching the end of the display line.
5882 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5883 This means, in particular, that TO_X includes window's horizontal
5884 scroll amount.
5886 The return value has several possible values that
5887 say what condition caused the scan to stop:
5889 MOVE_POS_MATCH_OR_ZV
5890 - when TO_POS or ZV was reached.
5892 MOVE_X_REACHED
5893 -when TO_X was reached before TO_POS or ZV were reached.
5895 MOVE_LINE_CONTINUED
5896 - when we reached the end of the display area and the line must
5897 be continued.
5899 MOVE_LINE_TRUNCATED
5900 - when we reached the end of the display area and the line is
5901 truncated.
5903 MOVE_NEWLINE_OR_CR
5904 - when we stopped at a line end, i.e. a newline or a CR and selective
5905 display is on. */
5907 static enum move_it_result
5908 move_it_in_display_line_to (it, to_charpos, to_x, op)
5909 struct it *it;
5910 int to_charpos, to_x, op;
5912 enum move_it_result result = MOVE_UNDEFINED;
5913 struct glyph_row *saved_glyph_row;
5915 /* Don't produce glyphs in produce_glyphs. */
5916 saved_glyph_row = it->glyph_row;
5917 it->glyph_row = NULL;
5919 #define BUFFER_POS_REACHED_P() \
5920 ((op & MOVE_TO_POS) != 0 \
5921 && BUFFERP (it->object) \
5922 && IT_CHARPOS (*it) >= to_charpos \
5923 && (it->method == GET_FROM_BUFFER \
5924 || (it->method == GET_FROM_DISPLAY_VECTOR \
5925 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5928 while (1)
5930 int x, i, ascent = 0, descent = 0;
5932 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5933 if ((op & MOVE_TO_POS) != 0
5934 && BUFFERP (it->object)
5935 && it->method == GET_FROM_BUFFER
5936 && IT_CHARPOS (*it) > to_charpos)
5938 result = MOVE_POS_MATCH_OR_ZV;
5939 break;
5942 /* Stop when ZV reached.
5943 We used to stop here when TO_CHARPOS reached as well, but that is
5944 too soon if this glyph does not fit on this line. So we handle it
5945 explicitly below. */
5946 if (!get_next_display_element (it)
5947 || (it->truncate_lines_p
5948 && BUFFER_POS_REACHED_P ()))
5950 result = MOVE_POS_MATCH_OR_ZV;
5951 break;
5954 /* The call to produce_glyphs will get the metrics of the
5955 display element IT is loaded with. We record in x the
5956 x-position before this display element in case it does not
5957 fit on the line. */
5958 x = it->current_x;
5960 /* Remember the line height so far in case the next element doesn't
5961 fit on the line. */
5962 if (!it->truncate_lines_p)
5964 ascent = it->max_ascent;
5965 descent = it->max_descent;
5968 PRODUCE_GLYPHS (it);
5970 if (it->area != TEXT_AREA)
5972 set_iterator_to_next (it, 1);
5973 continue;
5976 /* The number of glyphs we get back in IT->nglyphs will normally
5977 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5978 character on a terminal frame, or (iii) a line end. For the
5979 second case, IT->nglyphs - 1 padding glyphs will be present
5980 (on X frames, there is only one glyph produced for a
5981 composite character.
5983 The behavior implemented below means, for continuation lines,
5984 that as many spaces of a TAB as fit on the current line are
5985 displayed there. For terminal frames, as many glyphs of a
5986 multi-glyph character are displayed in the current line, too.
5987 This is what the old redisplay code did, and we keep it that
5988 way. Under X, the whole shape of a complex character must
5989 fit on the line or it will be completely displayed in the
5990 next line.
5992 Note that both for tabs and padding glyphs, all glyphs have
5993 the same width. */
5994 if (it->nglyphs)
5996 /* More than one glyph or glyph doesn't fit on line. All
5997 glyphs have the same width. */
5998 int single_glyph_width = it->pixel_width / it->nglyphs;
5999 int new_x;
6001 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6003 new_x = x + single_glyph_width;
6005 /* We want to leave anything reaching TO_X to the caller. */
6006 if ((op & MOVE_TO_X) && new_x > to_x)
6008 if (BUFFER_POS_REACHED_P ())
6009 goto buffer_pos_reached;
6010 it->current_x = x;
6011 result = MOVE_X_REACHED;
6012 break;
6014 else if (/* Lines are continued. */
6015 !it->truncate_lines_p
6016 && (/* And glyph doesn't fit on the line. */
6017 new_x > it->last_visible_x
6018 /* Or it fits exactly and we're on a window
6019 system frame. */
6020 || (new_x == it->last_visible_x
6021 && FRAME_WINDOW_P (it->f))))
6023 if (/* IT->hpos == 0 means the very first glyph
6024 doesn't fit on the line, e.g. a wide image. */
6025 it->hpos == 0
6026 || (new_x == it->last_visible_x
6027 && FRAME_WINDOW_P (it->f)))
6029 ++it->hpos;
6030 it->current_x = new_x;
6031 if (i == it->nglyphs - 1)
6033 set_iterator_to_next (it, 1);
6034 #ifdef HAVE_WINDOW_SYSTEM
6035 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6037 if (!get_next_display_element (it))
6039 result = MOVE_POS_MATCH_OR_ZV;
6040 break;
6042 if (BUFFER_POS_REACHED_P ())
6044 if (ITERATOR_AT_END_OF_LINE_P (it))
6045 result = MOVE_POS_MATCH_OR_ZV;
6046 else
6047 result = MOVE_LINE_CONTINUED;
6048 break;
6050 if (ITERATOR_AT_END_OF_LINE_P (it))
6052 result = MOVE_NEWLINE_OR_CR;
6053 break;
6056 #endif /* HAVE_WINDOW_SYSTEM */
6059 else
6061 it->current_x = x;
6062 it->max_ascent = ascent;
6063 it->max_descent = descent;
6066 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6067 IT_CHARPOS (*it)));
6068 result = MOVE_LINE_CONTINUED;
6069 break;
6071 else if (BUFFER_POS_REACHED_P ())
6072 goto buffer_pos_reached;
6073 else if (new_x > it->first_visible_x)
6075 /* Glyph is visible. Increment number of glyphs that
6076 would be displayed. */
6077 ++it->hpos;
6079 else
6081 /* Glyph is completely off the left margin of the display
6082 area. Nothing to do. */
6086 if (result != MOVE_UNDEFINED)
6087 break;
6089 else if (BUFFER_POS_REACHED_P ())
6091 buffer_pos_reached:
6092 it->current_x = x;
6093 it->max_ascent = ascent;
6094 it->max_descent = descent;
6095 result = MOVE_POS_MATCH_OR_ZV;
6096 break;
6098 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6100 /* Stop when TO_X specified and reached. This check is
6101 necessary here because of lines consisting of a line end,
6102 only. The line end will not produce any glyphs and we
6103 would never get MOVE_X_REACHED. */
6104 xassert (it->nglyphs == 0);
6105 result = MOVE_X_REACHED;
6106 break;
6109 /* Is this a line end? If yes, we're done. */
6110 if (ITERATOR_AT_END_OF_LINE_P (it))
6112 result = MOVE_NEWLINE_OR_CR;
6113 break;
6116 /* The current display element has been consumed. Advance
6117 to the next. */
6118 set_iterator_to_next (it, 1);
6120 /* Stop if lines are truncated and IT's current x-position is
6121 past the right edge of the window now. */
6122 if (it->truncate_lines_p
6123 && it->current_x >= it->last_visible_x)
6125 #ifdef HAVE_WINDOW_SYSTEM
6126 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6128 if (!get_next_display_element (it)
6129 || BUFFER_POS_REACHED_P ())
6131 result = MOVE_POS_MATCH_OR_ZV;
6132 break;
6134 if (ITERATOR_AT_END_OF_LINE_P (it))
6136 result = MOVE_NEWLINE_OR_CR;
6137 break;
6140 #endif /* HAVE_WINDOW_SYSTEM */
6141 result = MOVE_LINE_TRUNCATED;
6142 break;
6146 #undef BUFFER_POS_REACHED_P
6148 /* Restore the iterator settings altered at the beginning of this
6149 function. */
6150 it->glyph_row = saved_glyph_row;
6151 return result;
6155 /* Move IT forward until it satisfies one or more of the criteria in
6156 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6158 OP is a bit-mask that specifies where to stop, and in particular,
6159 which of those four position arguments makes a difference. See the
6160 description of enum move_operation_enum.
6162 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6163 screen line, this function will set IT to the next position >
6164 TO_CHARPOS. */
6166 void
6167 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6168 struct it *it;
6169 int to_charpos, to_x, to_y, to_vpos;
6170 int op;
6172 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6173 int line_height;
6174 int reached = 0;
6176 for (;;)
6178 if (op & MOVE_TO_VPOS)
6180 /* If no TO_CHARPOS and no TO_X specified, stop at the
6181 start of the line TO_VPOS. */
6182 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6184 if (it->vpos == to_vpos)
6186 reached = 1;
6187 break;
6189 else
6190 skip = move_it_in_display_line_to (it, -1, -1, 0);
6192 else
6194 /* TO_VPOS >= 0 means stop at TO_X in the line at
6195 TO_VPOS, or at TO_POS, whichever comes first. */
6196 if (it->vpos == to_vpos)
6198 reached = 2;
6199 break;
6202 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6204 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6206 reached = 3;
6207 break;
6209 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6211 /* We have reached TO_X but not in the line we want. */
6212 skip = move_it_in_display_line_to (it, to_charpos,
6213 -1, MOVE_TO_POS);
6214 if (skip == MOVE_POS_MATCH_OR_ZV)
6216 reached = 4;
6217 break;
6222 else if (op & MOVE_TO_Y)
6224 struct it it_backup;
6226 /* TO_Y specified means stop at TO_X in the line containing
6227 TO_Y---or at TO_CHARPOS if this is reached first. The
6228 problem is that we can't really tell whether the line
6229 contains TO_Y before we have completely scanned it, and
6230 this may skip past TO_X. What we do is to first scan to
6231 TO_X.
6233 If TO_X is not specified, use a TO_X of zero. The reason
6234 is to make the outcome of this function more predictable.
6235 If we didn't use TO_X == 0, we would stop at the end of
6236 the line which is probably not what a caller would expect
6237 to happen. */
6238 skip = move_it_in_display_line_to (it, to_charpos,
6239 ((op & MOVE_TO_X)
6240 ? to_x : 0),
6241 (MOVE_TO_X
6242 | (op & MOVE_TO_POS)));
6244 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6245 if (skip == MOVE_POS_MATCH_OR_ZV)
6247 reached = 5;
6248 break;
6251 /* If TO_X was reached, we would like to know whether TO_Y
6252 is in the line. This can only be said if we know the
6253 total line height which requires us to scan the rest of
6254 the line. */
6255 if (skip == MOVE_X_REACHED)
6257 it_backup = *it;
6258 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6259 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6260 op & MOVE_TO_POS);
6261 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6264 /* Now, decide whether TO_Y is in this line. */
6265 line_height = it->max_ascent + it->max_descent;
6266 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6268 if (to_y >= it->current_y
6269 && to_y < it->current_y + line_height)
6271 if (skip == MOVE_X_REACHED)
6272 /* If TO_Y is in this line and TO_X was reached above,
6273 we scanned too far. We have to restore IT's settings
6274 to the ones before skipping. */
6275 *it = it_backup;
6276 reached = 6;
6278 else if (skip == MOVE_X_REACHED)
6280 skip = skip2;
6281 if (skip == MOVE_POS_MATCH_OR_ZV)
6282 reached = 7;
6285 if (reached)
6286 break;
6288 else
6289 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6291 switch (skip)
6293 case MOVE_POS_MATCH_OR_ZV:
6294 reached = 8;
6295 goto out;
6297 case MOVE_NEWLINE_OR_CR:
6298 set_iterator_to_next (it, 1);
6299 it->continuation_lines_width = 0;
6300 break;
6302 case MOVE_LINE_TRUNCATED:
6303 it->continuation_lines_width = 0;
6304 reseat_at_next_visible_line_start (it, 0);
6305 if ((op & MOVE_TO_POS) != 0
6306 && IT_CHARPOS (*it) > to_charpos)
6308 reached = 9;
6309 goto out;
6311 break;
6313 case MOVE_LINE_CONTINUED:
6314 it->continuation_lines_width += it->current_x;
6315 break;
6317 default:
6318 abort ();
6321 /* Reset/increment for the next run. */
6322 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6323 it->current_x = it->hpos = 0;
6324 it->current_y += it->max_ascent + it->max_descent;
6325 ++it->vpos;
6326 last_height = it->max_ascent + it->max_descent;
6327 last_max_ascent = it->max_ascent;
6328 it->max_ascent = it->max_descent = 0;
6331 out:
6333 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6337 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6339 If DY > 0, move IT backward at least that many pixels. DY = 0
6340 means move IT backward to the preceding line start or BEGV. This
6341 function may move over more than DY pixels if IT->current_y - DY
6342 ends up in the middle of a line; in this case IT->current_y will be
6343 set to the top of the line moved to. */
6345 void
6346 move_it_vertically_backward (it, dy)
6347 struct it *it;
6348 int dy;
6350 int nlines, h;
6351 struct it it2, it3;
6352 int start_pos;
6354 move_further_back:
6355 xassert (dy >= 0);
6357 start_pos = IT_CHARPOS (*it);
6359 /* Estimate how many newlines we must move back. */
6360 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6362 /* Set the iterator's position that many lines back. */
6363 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6364 back_to_previous_visible_line_start (it);
6366 /* Reseat the iterator here. When moving backward, we don't want
6367 reseat to skip forward over invisible text, set up the iterator
6368 to deliver from overlay strings at the new position etc. So,
6369 use reseat_1 here. */
6370 reseat_1 (it, it->current.pos, 1);
6372 /* We are now surely at a line start. */
6373 it->current_x = it->hpos = 0;
6374 it->continuation_lines_width = 0;
6376 /* Move forward and see what y-distance we moved. First move to the
6377 start of the next line so that we get its height. We need this
6378 height to be able to tell whether we reached the specified
6379 y-distance. */
6380 it2 = *it;
6381 it2.max_ascent = it2.max_descent = 0;
6382 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6383 MOVE_TO_POS | MOVE_TO_VPOS);
6384 xassert (IT_CHARPOS (*it) >= BEGV);
6385 it3 = it2;
6387 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6388 xassert (IT_CHARPOS (*it) >= BEGV);
6389 /* H is the actual vertical distance from the position in *IT
6390 and the starting position. */
6391 h = it2.current_y - it->current_y;
6392 /* NLINES is the distance in number of lines. */
6393 nlines = it2.vpos - it->vpos;
6395 /* Correct IT's y and vpos position
6396 so that they are relative to the starting point. */
6397 it->vpos -= nlines;
6398 it->current_y -= h;
6400 if (dy == 0)
6402 /* DY == 0 means move to the start of the screen line. The
6403 value of nlines is > 0 if continuation lines were involved. */
6404 if (nlines > 0)
6405 move_it_by_lines (it, nlines, 1);
6406 #if 0
6407 /* I think this assert is bogus if buffer contains
6408 invisible text or images. KFS. */
6409 xassert (IT_CHARPOS (*it) <= start_pos);
6410 #endif
6412 else
6414 /* The y-position we try to reach, relative to *IT.
6415 Note that H has been subtracted in front of the if-statement. */
6416 int target_y = it->current_y + h - dy;
6417 int y0 = it3.current_y;
6418 int y1 = line_bottom_y (&it3);
6419 int line_height = y1 - y0;
6421 /* If we did not reach target_y, try to move further backward if
6422 we can. If we moved too far backward, try to move forward. */
6423 if (target_y < it->current_y
6424 /* This is heuristic. In a window that's 3 lines high, with
6425 a line height of 13 pixels each, recentering with point
6426 on the bottom line will try to move -39/2 = 19 pixels
6427 backward. Try to avoid moving into the first line. */
6428 && (it->current_y - target_y
6429 > min (window_box_height (it->w), line_height * 2 / 3))
6430 && IT_CHARPOS (*it) > BEGV)
6432 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6433 target_y - it->current_y));
6434 dy = it->current_y - target_y;
6435 goto move_further_back;
6437 else if (target_y >= it->current_y + line_height
6438 && IT_CHARPOS (*it) < ZV)
6440 /* Should move forward by at least one line, maybe more.
6442 Note: Calling move_it_by_lines can be expensive on
6443 terminal frames, where compute_motion is used (via
6444 vmotion) to do the job, when there are very long lines
6445 and truncate-lines is nil. That's the reason for
6446 treating terminal frames specially here. */
6448 if (!FRAME_WINDOW_P (it->f))
6449 move_it_vertically (it, target_y - (it->current_y + line_height));
6450 else
6454 move_it_by_lines (it, 1, 1);
6456 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6459 #if 0
6460 /* I think this assert is bogus if buffer contains
6461 invisible text or images. KFS. */
6462 xassert (IT_CHARPOS (*it) >= BEGV);
6463 #endif
6469 /* Move IT by a specified amount of pixel lines DY. DY negative means
6470 move backwards. DY = 0 means move to start of screen line. At the
6471 end, IT will be on the start of a screen line. */
6473 void
6474 move_it_vertically (it, dy)
6475 struct it *it;
6476 int dy;
6478 if (dy <= 0)
6479 move_it_vertically_backward (it, -dy);
6480 else
6482 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6483 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6484 MOVE_TO_POS | MOVE_TO_Y);
6485 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6487 /* If buffer ends in ZV without a newline, move to the start of
6488 the line to satisfy the post-condition. */
6489 if (IT_CHARPOS (*it) == ZV
6490 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6491 move_it_by_lines (it, 0, 0);
6496 /* Move iterator IT past the end of the text line it is in. */
6498 void
6499 move_it_past_eol (it)
6500 struct it *it;
6502 enum move_it_result rc;
6504 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6505 if (rc == MOVE_NEWLINE_OR_CR)
6506 set_iterator_to_next (it, 0);
6510 #if 0 /* Currently not used. */
6512 /* Return non-zero if some text between buffer positions START_CHARPOS
6513 and END_CHARPOS is invisible. IT->window is the window for text
6514 property lookup. */
6516 static int
6517 invisible_text_between_p (it, start_charpos, end_charpos)
6518 struct it *it;
6519 int start_charpos, end_charpos;
6521 Lisp_Object prop, limit;
6522 int invisible_found_p;
6524 xassert (it != NULL && start_charpos <= end_charpos);
6526 /* Is text at START invisible? */
6527 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6528 it->window);
6529 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6530 invisible_found_p = 1;
6531 else
6533 limit = Fnext_single_char_property_change (make_number (start_charpos),
6534 Qinvisible, Qnil,
6535 make_number (end_charpos));
6536 invisible_found_p = XFASTINT (limit) < end_charpos;
6539 return invisible_found_p;
6542 #endif /* 0 */
6545 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6546 negative means move up. DVPOS == 0 means move to the start of the
6547 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6548 NEED_Y_P is zero, IT->current_y will be left unchanged.
6550 Further optimization ideas: If we would know that IT->f doesn't use
6551 a face with proportional font, we could be faster for
6552 truncate-lines nil. */
6554 void
6555 move_it_by_lines (it, dvpos, need_y_p)
6556 struct it *it;
6557 int dvpos, need_y_p;
6559 struct position pos;
6561 if (!FRAME_WINDOW_P (it->f))
6563 struct text_pos textpos;
6565 /* We can use vmotion on frames without proportional fonts. */
6566 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6567 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6568 reseat (it, textpos, 1);
6569 it->vpos += pos.vpos;
6570 it->current_y += pos.vpos;
6572 else if (dvpos == 0)
6574 /* DVPOS == 0 means move to the start of the screen line. */
6575 move_it_vertically_backward (it, 0);
6576 xassert (it->current_x == 0 && it->hpos == 0);
6577 /* Let next call to line_bottom_y calculate real line height */
6578 last_height = 0;
6580 else if (dvpos > 0)
6581 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6582 else
6584 struct it it2;
6585 int start_charpos, i;
6587 /* Start at the beginning of the screen line containing IT's
6588 position. */
6589 move_it_vertically_backward (it, 0);
6591 /* Go back -DVPOS visible lines and reseat the iterator there. */
6592 start_charpos = IT_CHARPOS (*it);
6593 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6594 back_to_previous_visible_line_start (it);
6595 reseat (it, it->current.pos, 1);
6596 it->current_x = it->hpos = 0;
6598 /* Above call may have moved too far if continuation lines
6599 are involved. Scan forward and see if it did. */
6600 it2 = *it;
6601 it2.vpos = it2.current_y = 0;
6602 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6603 it->vpos -= it2.vpos;
6604 it->current_y -= it2.current_y;
6605 it->current_x = it->hpos = 0;
6607 /* If we moved too far back, move IT some lines forward. */
6608 if (it2.vpos > -dvpos)
6610 int delta = it2.vpos + dvpos;
6611 it2 = *it;
6612 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6613 /* Move back again if we got too far ahead. */
6614 if (IT_CHARPOS (*it) >= start_charpos)
6615 *it = it2;
6620 /* Return 1 if IT points into the middle of a display vector. */
6623 in_display_vector_p (it)
6624 struct it *it;
6626 return (it->method == GET_FROM_DISPLAY_VECTOR
6627 && it->current.dpvec_index > 0
6628 && it->dpvec + it->current.dpvec_index != it->dpend);
6632 /***********************************************************************
6633 Messages
6634 ***********************************************************************/
6637 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6638 to *Messages*. */
6640 void
6641 add_to_log (format, arg1, arg2)
6642 char *format;
6643 Lisp_Object arg1, arg2;
6645 Lisp_Object args[3];
6646 Lisp_Object msg, fmt;
6647 char *buffer;
6648 int len;
6649 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6650 USE_SAFE_ALLOCA;
6652 /* Do nothing if called asynchronously. Inserting text into
6653 a buffer may call after-change-functions and alike and
6654 that would means running Lisp asynchronously. */
6655 if (handling_signal)
6656 return;
6658 fmt = msg = Qnil;
6659 GCPRO4 (fmt, msg, arg1, arg2);
6661 args[0] = fmt = build_string (format);
6662 args[1] = arg1;
6663 args[2] = arg2;
6664 msg = Fformat (3, args);
6666 len = SBYTES (msg) + 1;
6667 SAFE_ALLOCA (buffer, char *, len);
6668 bcopy (SDATA (msg), buffer, len);
6670 message_dolog (buffer, len - 1, 1, 0);
6671 SAFE_FREE ();
6673 UNGCPRO;
6677 /* Output a newline in the *Messages* buffer if "needs" one. */
6679 void
6680 message_log_maybe_newline ()
6682 if (message_log_need_newline)
6683 message_dolog ("", 0, 1, 0);
6687 /* Add a string M of length NBYTES to the message log, optionally
6688 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6689 nonzero, means interpret the contents of M as multibyte. This
6690 function calls low-level routines in order to bypass text property
6691 hooks, etc. which might not be safe to run. */
6693 void
6694 message_dolog (m, nbytes, nlflag, multibyte)
6695 const char *m;
6696 int nbytes, nlflag, multibyte;
6698 if (!NILP (Vmemory_full))
6699 return;
6701 if (!NILP (Vmessage_log_max))
6703 struct buffer *oldbuf;
6704 Lisp_Object oldpoint, oldbegv, oldzv;
6705 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6706 int point_at_end = 0;
6707 int zv_at_end = 0;
6708 Lisp_Object old_deactivate_mark, tem;
6709 struct gcpro gcpro1;
6711 old_deactivate_mark = Vdeactivate_mark;
6712 oldbuf = current_buffer;
6713 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6714 current_buffer->undo_list = Qt;
6716 oldpoint = message_dolog_marker1;
6717 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6718 oldbegv = message_dolog_marker2;
6719 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6720 oldzv = message_dolog_marker3;
6721 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6722 GCPRO1 (old_deactivate_mark);
6724 if (PT == Z)
6725 point_at_end = 1;
6726 if (ZV == Z)
6727 zv_at_end = 1;
6729 BEGV = BEG;
6730 BEGV_BYTE = BEG_BYTE;
6731 ZV = Z;
6732 ZV_BYTE = Z_BYTE;
6733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6735 /* Insert the string--maybe converting multibyte to single byte
6736 or vice versa, so that all the text fits the buffer. */
6737 if (multibyte
6738 && NILP (current_buffer->enable_multibyte_characters))
6740 int i, c, char_bytes;
6741 unsigned char work[1];
6743 /* Convert a multibyte string to single-byte
6744 for the *Message* buffer. */
6745 for (i = 0; i < nbytes; i += char_bytes)
6747 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6748 work[0] = (SINGLE_BYTE_CHAR_P (c)
6750 : multibyte_char_to_unibyte (c, Qnil));
6751 insert_1_both (work, 1, 1, 1, 0, 0);
6754 else if (! multibyte
6755 && ! NILP (current_buffer->enable_multibyte_characters))
6757 int i, c, char_bytes;
6758 unsigned char *msg = (unsigned char *) m;
6759 unsigned char str[MAX_MULTIBYTE_LENGTH];
6760 /* Convert a single-byte string to multibyte
6761 for the *Message* buffer. */
6762 for (i = 0; i < nbytes; i++)
6764 c = unibyte_char_to_multibyte (msg[i]);
6765 char_bytes = CHAR_STRING (c, str);
6766 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6769 else if (nbytes)
6770 insert_1 (m, nbytes, 1, 0, 0);
6772 if (nlflag)
6774 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6775 insert_1 ("\n", 1, 1, 0, 0);
6777 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6778 this_bol = PT;
6779 this_bol_byte = PT_BYTE;
6781 /* See if this line duplicates the previous one.
6782 If so, combine duplicates. */
6783 if (this_bol > BEG)
6785 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6786 prev_bol = PT;
6787 prev_bol_byte = PT_BYTE;
6789 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6790 this_bol, this_bol_byte);
6791 if (dup)
6793 del_range_both (prev_bol, prev_bol_byte,
6794 this_bol, this_bol_byte, 0);
6795 if (dup > 1)
6797 char dupstr[40];
6798 int duplen;
6800 /* If you change this format, don't forget to also
6801 change message_log_check_duplicate. */
6802 sprintf (dupstr, " [%d times]", dup);
6803 duplen = strlen (dupstr);
6804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6805 insert_1 (dupstr, duplen, 1, 0, 1);
6810 /* If we have more than the desired maximum number of lines
6811 in the *Messages* buffer now, delete the oldest ones.
6812 This is safe because we don't have undo in this buffer. */
6814 if (NATNUMP (Vmessage_log_max))
6816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6817 -XFASTINT (Vmessage_log_max) - 1, 0);
6818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6821 BEGV = XMARKER (oldbegv)->charpos;
6822 BEGV_BYTE = marker_byte_position (oldbegv);
6824 if (zv_at_end)
6826 ZV = Z;
6827 ZV_BYTE = Z_BYTE;
6829 else
6831 ZV = XMARKER (oldzv)->charpos;
6832 ZV_BYTE = marker_byte_position (oldzv);
6835 if (point_at_end)
6836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6837 else
6838 /* We can't do Fgoto_char (oldpoint) because it will run some
6839 Lisp code. */
6840 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6841 XMARKER (oldpoint)->bytepos);
6843 UNGCPRO;
6844 unchain_marker (XMARKER (oldpoint));
6845 unchain_marker (XMARKER (oldbegv));
6846 unchain_marker (XMARKER (oldzv));
6848 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6849 set_buffer_internal (oldbuf);
6850 if (NILP (tem))
6851 windows_or_buffers_changed = old_windows_or_buffers_changed;
6852 message_log_need_newline = !nlflag;
6853 Vdeactivate_mark = old_deactivate_mark;
6858 /* We are at the end of the buffer after just having inserted a newline.
6859 (Note: We depend on the fact we won't be crossing the gap.)
6860 Check to see if the most recent message looks a lot like the previous one.
6861 Return 0 if different, 1 if the new one should just replace it, or a
6862 value N > 1 if we should also append " [N times]". */
6864 static int
6865 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6866 int prev_bol, this_bol;
6867 int prev_bol_byte, this_bol_byte;
6869 int i;
6870 int len = Z_BYTE - 1 - this_bol_byte;
6871 int seen_dots = 0;
6872 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6873 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6875 for (i = 0; i < len; i++)
6877 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6878 seen_dots = 1;
6879 if (p1[i] != p2[i])
6880 return seen_dots;
6882 p1 += len;
6883 if (*p1 == '\n')
6884 return 2;
6885 if (*p1++ == ' ' && *p1++ == '[')
6887 int n = 0;
6888 while (*p1 >= '0' && *p1 <= '9')
6889 n = n * 10 + *p1++ - '0';
6890 if (strncmp (p1, " times]\n", 8) == 0)
6891 return n+1;
6893 return 0;
6897 /* Display an echo area message M with a specified length of NBYTES
6898 bytes. The string may include null characters. If M is 0, clear
6899 out any existing message, and let the mini-buffer text show
6900 through.
6902 The buffer M must continue to exist until after the echo area gets
6903 cleared or some other message gets displayed there. This means do
6904 not pass text that is stored in a Lisp string; do not pass text in
6905 a buffer that was alloca'd. */
6907 void
6908 message2 (m, nbytes, multibyte)
6909 const char *m;
6910 int nbytes;
6911 int multibyte;
6913 /* First flush out any partial line written with print. */
6914 message_log_maybe_newline ();
6915 if (m)
6916 message_dolog (m, nbytes, 1, multibyte);
6917 message2_nolog (m, nbytes, multibyte);
6921 /* The non-logging counterpart of message2. */
6923 void
6924 message2_nolog (m, nbytes, multibyte)
6925 const char *m;
6926 int nbytes, multibyte;
6928 struct frame *sf = SELECTED_FRAME ();
6929 message_enable_multibyte = multibyte;
6931 if (noninteractive)
6933 if (noninteractive_need_newline)
6934 putc ('\n', stderr);
6935 noninteractive_need_newline = 0;
6936 if (m)
6937 fwrite (m, nbytes, 1, stderr);
6938 if (cursor_in_echo_area == 0)
6939 fprintf (stderr, "\n");
6940 fflush (stderr);
6942 /* A null message buffer means that the frame hasn't really been
6943 initialized yet. Error messages get reported properly by
6944 cmd_error, so this must be just an informative message; toss it. */
6945 else if (INTERACTIVE
6946 && sf->glyphs_initialized_p
6947 && FRAME_MESSAGE_BUF (sf))
6949 Lisp_Object mini_window;
6950 struct frame *f;
6952 /* Get the frame containing the mini-buffer
6953 that the selected frame is using. */
6954 mini_window = FRAME_MINIBUF_WINDOW (sf);
6955 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6957 FRAME_SAMPLE_VISIBILITY (f);
6958 if (FRAME_VISIBLE_P (sf)
6959 && ! FRAME_VISIBLE_P (f))
6960 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6962 if (m)
6964 set_message (m, Qnil, nbytes, multibyte);
6965 if (minibuffer_auto_raise)
6966 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6968 else
6969 clear_message (1, 1);
6971 do_pending_window_change (0);
6972 echo_area_display (1);
6973 do_pending_window_change (0);
6974 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6975 (*frame_up_to_date_hook) (f);
6980 /* Display an echo area message M with a specified length of NBYTES
6981 bytes. The string may include null characters. If M is not a
6982 string, clear out any existing message, and let the mini-buffer
6983 text show through.
6985 This function cancels echoing. */
6987 void
6988 message3 (m, nbytes, multibyte)
6989 Lisp_Object m;
6990 int nbytes;
6991 int multibyte;
6993 struct gcpro gcpro1;
6995 GCPRO1 (m);
6996 clear_message (1,1);
6997 cancel_echoing ();
6999 /* First flush out any partial line written with print. */
7000 message_log_maybe_newline ();
7001 if (STRINGP (m))
7002 message_dolog (SDATA (m), nbytes, 1, multibyte);
7003 message3_nolog (m, nbytes, multibyte);
7005 UNGCPRO;
7009 /* The non-logging version of message3.
7010 This does not cancel echoing, because it is used for echoing.
7011 Perhaps we need to make a separate function for echoing
7012 and make this cancel echoing. */
7014 void
7015 message3_nolog (m, nbytes, multibyte)
7016 Lisp_Object m;
7017 int nbytes, multibyte;
7019 struct frame *sf = SELECTED_FRAME ();
7020 message_enable_multibyte = multibyte;
7022 if (noninteractive)
7024 if (noninteractive_need_newline)
7025 putc ('\n', stderr);
7026 noninteractive_need_newline = 0;
7027 if (STRINGP (m))
7028 fwrite (SDATA (m), nbytes, 1, stderr);
7029 if (cursor_in_echo_area == 0)
7030 fprintf (stderr, "\n");
7031 fflush (stderr);
7033 /* A null message buffer means that the frame hasn't really been
7034 initialized yet. Error messages get reported properly by
7035 cmd_error, so this must be just an informative message; toss it. */
7036 else if (INTERACTIVE
7037 && sf->glyphs_initialized_p
7038 && FRAME_MESSAGE_BUF (sf))
7040 Lisp_Object mini_window;
7041 Lisp_Object frame;
7042 struct frame *f;
7044 /* Get the frame containing the mini-buffer
7045 that the selected frame is using. */
7046 mini_window = FRAME_MINIBUF_WINDOW (sf);
7047 frame = XWINDOW (mini_window)->frame;
7048 f = XFRAME (frame);
7050 FRAME_SAMPLE_VISIBILITY (f);
7051 if (FRAME_VISIBLE_P (sf)
7052 && !FRAME_VISIBLE_P (f))
7053 Fmake_frame_visible (frame);
7055 if (STRINGP (m) && SCHARS (m) > 0)
7057 set_message (NULL, m, nbytes, multibyte);
7058 if (minibuffer_auto_raise)
7059 Fraise_frame (frame);
7061 else
7062 clear_message (1, 1);
7064 do_pending_window_change (0);
7065 echo_area_display (1);
7066 do_pending_window_change (0);
7067 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7068 (*frame_up_to_date_hook) (f);
7073 /* Display a null-terminated echo area message M. If M is 0, clear
7074 out any existing message, and let the mini-buffer text show through.
7076 The buffer M must continue to exist until after the echo area gets
7077 cleared or some other message gets displayed there. Do not pass
7078 text that is stored in a Lisp string. Do not pass text in a buffer
7079 that was alloca'd. */
7081 void
7082 message1 (m)
7083 char *m;
7085 message2 (m, (m ? strlen (m) : 0), 0);
7089 /* The non-logging counterpart of message1. */
7091 void
7092 message1_nolog (m)
7093 char *m;
7095 message2_nolog (m, (m ? strlen (m) : 0), 0);
7098 /* Display a message M which contains a single %s
7099 which gets replaced with STRING. */
7101 void
7102 message_with_string (m, string, log)
7103 char *m;
7104 Lisp_Object string;
7105 int log;
7107 CHECK_STRING (string);
7109 if (noninteractive)
7111 if (m)
7113 if (noninteractive_need_newline)
7114 putc ('\n', stderr);
7115 noninteractive_need_newline = 0;
7116 fprintf (stderr, m, SDATA (string));
7117 if (cursor_in_echo_area == 0)
7118 fprintf (stderr, "\n");
7119 fflush (stderr);
7122 else if (INTERACTIVE)
7124 /* The frame whose minibuffer we're going to display the message on.
7125 It may be larger than the selected frame, so we need
7126 to use its buffer, not the selected frame's buffer. */
7127 Lisp_Object mini_window;
7128 struct frame *f, *sf = SELECTED_FRAME ();
7130 /* Get the frame containing the minibuffer
7131 that the selected frame is using. */
7132 mini_window = FRAME_MINIBUF_WINDOW (sf);
7133 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7135 /* A null message buffer means that the frame hasn't really been
7136 initialized yet. Error messages get reported properly by
7137 cmd_error, so this must be just an informative message; toss it. */
7138 if (FRAME_MESSAGE_BUF (f))
7140 Lisp_Object args[2], message;
7141 struct gcpro gcpro1, gcpro2;
7143 args[0] = build_string (m);
7144 args[1] = message = string;
7145 GCPRO2 (args[0], message);
7146 gcpro1.nvars = 2;
7148 message = Fformat (2, args);
7150 if (log)
7151 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7152 else
7153 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7155 UNGCPRO;
7157 /* Print should start at the beginning of the message
7158 buffer next time. */
7159 message_buf_print = 0;
7165 /* Dump an informative message to the minibuf. If M is 0, clear out
7166 any existing message, and let the mini-buffer text show through. */
7168 /* VARARGS 1 */
7169 void
7170 message (m, a1, a2, a3)
7171 char *m;
7172 EMACS_INT a1, a2, a3;
7174 if (noninteractive)
7176 if (m)
7178 if (noninteractive_need_newline)
7179 putc ('\n', stderr);
7180 noninteractive_need_newline = 0;
7181 fprintf (stderr, m, a1, a2, a3);
7182 if (cursor_in_echo_area == 0)
7183 fprintf (stderr, "\n");
7184 fflush (stderr);
7187 else if (INTERACTIVE)
7189 /* The frame whose mini-buffer we're going to display the message
7190 on. It may be larger than the selected frame, so we need to
7191 use its buffer, not the selected frame's buffer. */
7192 Lisp_Object mini_window;
7193 struct frame *f, *sf = SELECTED_FRAME ();
7195 /* Get the frame containing the mini-buffer
7196 that the selected frame is using. */
7197 mini_window = FRAME_MINIBUF_WINDOW (sf);
7198 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7200 /* A null message buffer means that the frame hasn't really been
7201 initialized yet. Error messages get reported properly by
7202 cmd_error, so this must be just an informative message; toss
7203 it. */
7204 if (FRAME_MESSAGE_BUF (f))
7206 if (m)
7208 int len;
7209 #ifdef NO_ARG_ARRAY
7210 char *a[3];
7211 a[0] = (char *) a1;
7212 a[1] = (char *) a2;
7213 a[2] = (char *) a3;
7215 len = doprnt (FRAME_MESSAGE_BUF (f),
7216 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7217 #else
7218 len = doprnt (FRAME_MESSAGE_BUF (f),
7219 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7220 (char **) &a1);
7221 #endif /* NO_ARG_ARRAY */
7223 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7225 else
7226 message1 (0);
7228 /* Print should start at the beginning of the message
7229 buffer next time. */
7230 message_buf_print = 0;
7236 /* The non-logging version of message. */
7238 void
7239 message_nolog (m, a1, a2, a3)
7240 char *m;
7241 EMACS_INT a1, a2, a3;
7243 Lisp_Object old_log_max;
7244 old_log_max = Vmessage_log_max;
7245 Vmessage_log_max = Qnil;
7246 message (m, a1, a2, a3);
7247 Vmessage_log_max = old_log_max;
7251 /* Display the current message in the current mini-buffer. This is
7252 only called from error handlers in process.c, and is not time
7253 critical. */
7255 void
7256 update_echo_area ()
7258 if (!NILP (echo_area_buffer[0]))
7260 Lisp_Object string;
7261 string = Fcurrent_message ();
7262 message3 (string, SBYTES (string),
7263 !NILP (current_buffer->enable_multibyte_characters));
7268 /* Make sure echo area buffers in `echo_buffers' are live.
7269 If they aren't, make new ones. */
7271 static void
7272 ensure_echo_area_buffers ()
7274 int i;
7276 for (i = 0; i < 2; ++i)
7277 if (!BUFFERP (echo_buffer[i])
7278 || NILP (XBUFFER (echo_buffer[i])->name))
7280 char name[30];
7281 Lisp_Object old_buffer;
7282 int j;
7284 old_buffer = echo_buffer[i];
7285 sprintf (name, " *Echo Area %d*", i);
7286 echo_buffer[i] = Fget_buffer_create (build_string (name));
7287 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7289 for (j = 0; j < 2; ++j)
7290 if (EQ (old_buffer, echo_area_buffer[j]))
7291 echo_area_buffer[j] = echo_buffer[i];
7296 /* Call FN with args A1..A4 with either the current or last displayed
7297 echo_area_buffer as current buffer.
7299 WHICH zero means use the current message buffer
7300 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7301 from echo_buffer[] and clear it.
7303 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7304 suitable buffer from echo_buffer[] and clear it.
7306 Value is what FN returns. */
7308 static int
7309 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7310 struct window *w;
7311 int which;
7312 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7313 EMACS_INT a1;
7314 Lisp_Object a2;
7315 EMACS_INT a3, a4;
7317 Lisp_Object buffer;
7318 int this_one, the_other, clear_buffer_p, rc;
7319 int count = SPECPDL_INDEX ();
7321 /* If buffers aren't live, make new ones. */
7322 ensure_echo_area_buffers ();
7324 clear_buffer_p = 0;
7326 if (which == 0)
7327 this_one = 0, the_other = 1;
7328 else if (which > 0)
7329 this_one = 1, the_other = 0;
7331 /* Choose a suitable buffer from echo_buffer[] is we don't
7332 have one. */
7333 if (NILP (echo_area_buffer[this_one]))
7335 echo_area_buffer[this_one]
7336 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7337 ? echo_buffer[the_other]
7338 : echo_buffer[this_one]);
7339 clear_buffer_p = 1;
7342 buffer = echo_area_buffer[this_one];
7344 /* Don't get confused by reusing the buffer used for echoing
7345 for a different purpose. */
7346 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7347 cancel_echoing ();
7349 record_unwind_protect (unwind_with_echo_area_buffer,
7350 with_echo_area_buffer_unwind_data (w));
7352 /* Make the echo area buffer current. Note that for display
7353 purposes, it is not necessary that the displayed window's buffer
7354 == current_buffer, except for text property lookup. So, let's
7355 only set that buffer temporarily here without doing a full
7356 Fset_window_buffer. We must also change w->pointm, though,
7357 because otherwise an assertions in unshow_buffer fails, and Emacs
7358 aborts. */
7359 set_buffer_internal_1 (XBUFFER (buffer));
7360 if (w)
7362 w->buffer = buffer;
7363 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7366 current_buffer->undo_list = Qt;
7367 current_buffer->read_only = Qnil;
7368 specbind (Qinhibit_read_only, Qt);
7369 specbind (Qinhibit_modification_hooks, Qt);
7371 if (clear_buffer_p && Z > BEG)
7372 del_range (BEG, Z);
7374 xassert (BEGV >= BEG);
7375 xassert (ZV <= Z && ZV >= BEGV);
7377 rc = fn (a1, a2, a3, a4);
7379 xassert (BEGV >= BEG);
7380 xassert (ZV <= Z && ZV >= BEGV);
7382 unbind_to (count, Qnil);
7383 return rc;
7387 /* Save state that should be preserved around the call to the function
7388 FN called in with_echo_area_buffer. */
7390 static Lisp_Object
7391 with_echo_area_buffer_unwind_data (w)
7392 struct window *w;
7394 int i = 0;
7395 Lisp_Object vector;
7397 /* Reduce consing by keeping one vector in
7398 Vwith_echo_area_save_vector. */
7399 vector = Vwith_echo_area_save_vector;
7400 Vwith_echo_area_save_vector = Qnil;
7402 if (NILP (vector))
7403 vector = Fmake_vector (make_number (7), Qnil);
7405 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7406 AREF (vector, i) = Vdeactivate_mark, ++i;
7407 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7409 if (w)
7411 XSETWINDOW (AREF (vector, i), w); ++i;
7412 AREF (vector, i) = w->buffer; ++i;
7413 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7414 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7416 else
7418 int end = i + 4;
7419 for (; i < end; ++i)
7420 AREF (vector, i) = Qnil;
7423 xassert (i == ASIZE (vector));
7424 return vector;
7428 /* Restore global state from VECTOR which was created by
7429 with_echo_area_buffer_unwind_data. */
7431 static Lisp_Object
7432 unwind_with_echo_area_buffer (vector)
7433 Lisp_Object vector;
7435 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7436 Vdeactivate_mark = AREF (vector, 1);
7437 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7439 if (WINDOWP (AREF (vector, 3)))
7441 struct window *w;
7442 Lisp_Object buffer, charpos, bytepos;
7444 w = XWINDOW (AREF (vector, 3));
7445 buffer = AREF (vector, 4);
7446 charpos = AREF (vector, 5);
7447 bytepos = AREF (vector, 6);
7449 w->buffer = buffer;
7450 set_marker_both (w->pointm, buffer,
7451 XFASTINT (charpos), XFASTINT (bytepos));
7454 Vwith_echo_area_save_vector = vector;
7455 return Qnil;
7459 /* Set up the echo area for use by print functions. MULTIBYTE_P
7460 non-zero means we will print multibyte. */
7462 void
7463 setup_echo_area_for_printing (multibyte_p)
7464 int multibyte_p;
7466 /* If we can't find an echo area any more, exit. */
7467 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7468 Fkill_emacs (Qnil);
7470 ensure_echo_area_buffers ();
7472 if (!message_buf_print)
7474 /* A message has been output since the last time we printed.
7475 Choose a fresh echo area buffer. */
7476 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7477 echo_area_buffer[0] = echo_buffer[1];
7478 else
7479 echo_area_buffer[0] = echo_buffer[0];
7481 /* Switch to that buffer and clear it. */
7482 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7483 current_buffer->truncate_lines = Qnil;
7485 if (Z > BEG)
7487 int count = SPECPDL_INDEX ();
7488 specbind (Qinhibit_read_only, Qt);
7489 /* Note that undo recording is always disabled. */
7490 del_range (BEG, Z);
7491 unbind_to (count, Qnil);
7493 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7495 /* Set up the buffer for the multibyteness we need. */
7496 if (multibyte_p
7497 != !NILP (current_buffer->enable_multibyte_characters))
7498 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7500 /* Raise the frame containing the echo area. */
7501 if (minibuffer_auto_raise)
7503 struct frame *sf = SELECTED_FRAME ();
7504 Lisp_Object mini_window;
7505 mini_window = FRAME_MINIBUF_WINDOW (sf);
7506 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7509 message_log_maybe_newline ();
7510 message_buf_print = 1;
7512 else
7514 if (NILP (echo_area_buffer[0]))
7516 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7517 echo_area_buffer[0] = echo_buffer[1];
7518 else
7519 echo_area_buffer[0] = echo_buffer[0];
7522 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7524 /* Someone switched buffers between print requests. */
7525 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7526 current_buffer->truncate_lines = Qnil;
7532 /* Display an echo area message in window W. Value is non-zero if W's
7533 height is changed. If display_last_displayed_message_p is
7534 non-zero, display the message that was last displayed, otherwise
7535 display the current message. */
7537 static int
7538 display_echo_area (w)
7539 struct window *w;
7541 int i, no_message_p, window_height_changed_p, count;
7543 /* Temporarily disable garbage collections while displaying the echo
7544 area. This is done because a GC can print a message itself.
7545 That message would modify the echo area buffer's contents while a
7546 redisplay of the buffer is going on, and seriously confuse
7547 redisplay. */
7548 count = inhibit_garbage_collection ();
7550 /* If there is no message, we must call display_echo_area_1
7551 nevertheless because it resizes the window. But we will have to
7552 reset the echo_area_buffer in question to nil at the end because
7553 with_echo_area_buffer will sets it to an empty buffer. */
7554 i = display_last_displayed_message_p ? 1 : 0;
7555 no_message_p = NILP (echo_area_buffer[i]);
7557 window_height_changed_p
7558 = with_echo_area_buffer (w, display_last_displayed_message_p,
7559 display_echo_area_1,
7560 (EMACS_INT) w, Qnil, 0, 0);
7562 if (no_message_p)
7563 echo_area_buffer[i] = Qnil;
7565 unbind_to (count, Qnil);
7566 return window_height_changed_p;
7570 /* Helper for display_echo_area. Display the current buffer which
7571 contains the current echo area message in window W, a mini-window,
7572 a pointer to which is passed in A1. A2..A4 are currently not used.
7573 Change the height of W so that all of the message is displayed.
7574 Value is non-zero if height of W was changed. */
7576 static int
7577 display_echo_area_1 (a1, a2, a3, a4)
7578 EMACS_INT a1;
7579 Lisp_Object a2;
7580 EMACS_INT a3, a4;
7582 struct window *w = (struct window *) a1;
7583 Lisp_Object window;
7584 struct text_pos start;
7585 int window_height_changed_p = 0;
7587 /* Do this before displaying, so that we have a large enough glyph
7588 matrix for the display. */
7589 window_height_changed_p = resize_mini_window (w, 0);
7591 /* Display. */
7592 clear_glyph_matrix (w->desired_matrix);
7593 XSETWINDOW (window, w);
7594 SET_TEXT_POS (start, BEG, BEG_BYTE);
7595 try_window (window, start);
7597 return window_height_changed_p;
7601 /* Resize the echo area window to exactly the size needed for the
7602 currently displayed message, if there is one. If a mini-buffer
7603 is active, don't shrink it. */
7605 void
7606 resize_echo_area_exactly ()
7608 if (BUFFERP (echo_area_buffer[0])
7609 && WINDOWP (echo_area_window))
7611 struct window *w = XWINDOW (echo_area_window);
7612 int resized_p;
7613 Lisp_Object resize_exactly;
7615 if (minibuf_level == 0)
7616 resize_exactly = Qt;
7617 else
7618 resize_exactly = Qnil;
7620 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7621 (EMACS_INT) w, resize_exactly, 0, 0);
7622 if (resized_p)
7624 ++windows_or_buffers_changed;
7625 ++update_mode_lines;
7626 redisplay_internal (0);
7632 /* Callback function for with_echo_area_buffer, when used from
7633 resize_echo_area_exactly. A1 contains a pointer to the window to
7634 resize, EXACTLY non-nil means resize the mini-window exactly to the
7635 size of the text displayed. A3 and A4 are not used. Value is what
7636 resize_mini_window returns. */
7638 static int
7639 resize_mini_window_1 (a1, exactly, a3, a4)
7640 EMACS_INT a1;
7641 Lisp_Object exactly;
7642 EMACS_INT a3, a4;
7644 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7648 /* Resize mini-window W to fit the size of its contents. EXACT:P
7649 means size the window exactly to the size needed. Otherwise, it's
7650 only enlarged until W's buffer is empty. Value is non-zero if
7651 the window height has been changed. */
7654 resize_mini_window (w, exact_p)
7655 struct window *w;
7656 int exact_p;
7658 struct frame *f = XFRAME (w->frame);
7659 int window_height_changed_p = 0;
7661 xassert (MINI_WINDOW_P (w));
7663 /* Don't resize windows while redisplaying a window; it would
7664 confuse redisplay functions when the size of the window they are
7665 displaying changes from under them. Such a resizing can happen,
7666 for instance, when which-func prints a long message while
7667 we are running fontification-functions. We're running these
7668 functions with safe_call which binds inhibit-redisplay to t. */
7669 if (!NILP (Vinhibit_redisplay))
7670 return 0;
7672 /* Nil means don't try to resize. */
7673 if (NILP (Vresize_mini_windows)
7674 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7675 return 0;
7677 if (!FRAME_MINIBUF_ONLY_P (f))
7679 struct it it;
7680 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7681 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7682 int height, max_height;
7683 int unit = FRAME_LINE_HEIGHT (f);
7684 struct text_pos start;
7685 struct buffer *old_current_buffer = NULL;
7687 if (current_buffer != XBUFFER (w->buffer))
7689 old_current_buffer = current_buffer;
7690 set_buffer_internal (XBUFFER (w->buffer));
7693 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7695 /* Compute the max. number of lines specified by the user. */
7696 if (FLOATP (Vmax_mini_window_height))
7697 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7698 else if (INTEGERP (Vmax_mini_window_height))
7699 max_height = XINT (Vmax_mini_window_height);
7700 else
7701 max_height = total_height / 4;
7703 /* Correct that max. height if it's bogus. */
7704 max_height = max (1, max_height);
7705 max_height = min (total_height, max_height);
7707 /* Find out the height of the text in the window. */
7708 if (it.truncate_lines_p)
7709 height = 1;
7710 else
7712 last_height = 0;
7713 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7714 if (it.max_ascent == 0 && it.max_descent == 0)
7715 height = it.current_y + last_height;
7716 else
7717 height = it.current_y + it.max_ascent + it.max_descent;
7718 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7719 height = (height + unit - 1) / unit;
7722 /* Compute a suitable window start. */
7723 if (height > max_height)
7725 height = max_height;
7726 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7727 move_it_vertically_backward (&it, (height - 1) * unit);
7728 start = it.current.pos;
7730 else
7731 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7732 SET_MARKER_FROM_TEXT_POS (w->start, start);
7734 if (EQ (Vresize_mini_windows, Qgrow_only))
7736 /* Let it grow only, until we display an empty message, in which
7737 case the window shrinks again. */
7738 if (height > WINDOW_TOTAL_LINES (w))
7740 int old_height = WINDOW_TOTAL_LINES (w);
7741 freeze_window_starts (f, 1);
7742 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7743 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7745 else if (height < WINDOW_TOTAL_LINES (w)
7746 && (exact_p || BEGV == ZV))
7748 int old_height = WINDOW_TOTAL_LINES (w);
7749 freeze_window_starts (f, 0);
7750 shrink_mini_window (w);
7751 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7754 else
7756 /* Always resize to exact size needed. */
7757 if (height > WINDOW_TOTAL_LINES (w))
7759 int old_height = WINDOW_TOTAL_LINES (w);
7760 freeze_window_starts (f, 1);
7761 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7762 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7764 else if (height < WINDOW_TOTAL_LINES (w))
7766 int old_height = WINDOW_TOTAL_LINES (w);
7767 freeze_window_starts (f, 0);
7768 shrink_mini_window (w);
7770 if (height)
7772 freeze_window_starts (f, 1);
7773 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7776 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7780 if (old_current_buffer)
7781 set_buffer_internal (old_current_buffer);
7784 return window_height_changed_p;
7788 /* Value is the current message, a string, or nil if there is no
7789 current message. */
7791 Lisp_Object
7792 current_message ()
7794 Lisp_Object msg;
7796 if (NILP (echo_area_buffer[0]))
7797 msg = Qnil;
7798 else
7800 with_echo_area_buffer (0, 0, current_message_1,
7801 (EMACS_INT) &msg, Qnil, 0, 0);
7802 if (NILP (msg))
7803 echo_area_buffer[0] = Qnil;
7806 return msg;
7810 static int
7811 current_message_1 (a1, a2, a3, a4)
7812 EMACS_INT a1;
7813 Lisp_Object a2;
7814 EMACS_INT a3, a4;
7816 Lisp_Object *msg = (Lisp_Object *) a1;
7818 if (Z > BEG)
7819 *msg = make_buffer_string (BEG, Z, 1);
7820 else
7821 *msg = Qnil;
7822 return 0;
7826 /* Push the current message on Vmessage_stack for later restauration
7827 by restore_message. Value is non-zero if the current message isn't
7828 empty. This is a relatively infrequent operation, so it's not
7829 worth optimizing. */
7832 push_message ()
7834 Lisp_Object msg;
7835 msg = current_message ();
7836 Vmessage_stack = Fcons (msg, Vmessage_stack);
7837 return STRINGP (msg);
7841 /* Restore message display from the top of Vmessage_stack. */
7843 void
7844 restore_message ()
7846 Lisp_Object msg;
7848 xassert (CONSP (Vmessage_stack));
7849 msg = XCAR (Vmessage_stack);
7850 if (STRINGP (msg))
7851 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7852 else
7853 message3_nolog (msg, 0, 0);
7857 /* Handler for record_unwind_protect calling pop_message. */
7859 Lisp_Object
7860 pop_message_unwind (dummy)
7861 Lisp_Object dummy;
7863 pop_message ();
7864 return Qnil;
7867 /* Pop the top-most entry off Vmessage_stack. */
7869 void
7870 pop_message ()
7872 xassert (CONSP (Vmessage_stack));
7873 Vmessage_stack = XCDR (Vmessage_stack);
7877 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7878 exits. If the stack is not empty, we have a missing pop_message
7879 somewhere. */
7881 void
7882 check_message_stack ()
7884 if (!NILP (Vmessage_stack))
7885 abort ();
7889 /* Truncate to NCHARS what will be displayed in the echo area the next
7890 time we display it---but don't redisplay it now. */
7892 void
7893 truncate_echo_area (nchars)
7894 int nchars;
7896 if (nchars == 0)
7897 echo_area_buffer[0] = Qnil;
7898 /* A null message buffer means that the frame hasn't really been
7899 initialized yet. Error messages get reported properly by
7900 cmd_error, so this must be just an informative message; toss it. */
7901 else if (!noninteractive
7902 && INTERACTIVE
7903 && !NILP (echo_area_buffer[0]))
7905 struct frame *sf = SELECTED_FRAME ();
7906 if (FRAME_MESSAGE_BUF (sf))
7907 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7912 /* Helper function for truncate_echo_area. Truncate the current
7913 message to at most NCHARS characters. */
7915 static int
7916 truncate_message_1 (nchars, a2, a3, a4)
7917 EMACS_INT nchars;
7918 Lisp_Object a2;
7919 EMACS_INT a3, a4;
7921 if (BEG + nchars < Z)
7922 del_range (BEG + nchars, Z);
7923 if (Z == BEG)
7924 echo_area_buffer[0] = Qnil;
7925 return 0;
7929 /* Set the current message to a substring of S or STRING.
7931 If STRING is a Lisp string, set the message to the first NBYTES
7932 bytes from STRING. NBYTES zero means use the whole string. If
7933 STRING is multibyte, the message will be displayed multibyte.
7935 If S is not null, set the message to the first LEN bytes of S. LEN
7936 zero means use the whole string. MULTIBYTE_P non-zero means S is
7937 multibyte. Display the message multibyte in that case. */
7939 void
7940 set_message (s, string, nbytes, multibyte_p)
7941 const char *s;
7942 Lisp_Object string;
7943 int nbytes, multibyte_p;
7945 message_enable_multibyte
7946 = ((s && multibyte_p)
7947 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7949 with_echo_area_buffer (0, 0, set_message_1,
7950 (EMACS_INT) s, string, nbytes, multibyte_p);
7951 message_buf_print = 0;
7952 help_echo_showing_p = 0;
7956 /* Helper function for set_message. Arguments have the same meaning
7957 as there, with A1 corresponding to S and A2 corresponding to STRING
7958 This function is called with the echo area buffer being
7959 current. */
7961 static int
7962 set_message_1 (a1, a2, nbytes, multibyte_p)
7963 EMACS_INT a1;
7964 Lisp_Object a2;
7965 EMACS_INT nbytes, multibyte_p;
7967 const char *s = (const char *) a1;
7968 Lisp_Object string = a2;
7970 /* Change multibyteness of the echo buffer appropriately. */
7971 if (message_enable_multibyte
7972 != !NILP (current_buffer->enable_multibyte_characters))
7973 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7975 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7977 /* Insert new message at BEG. */
7978 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7979 Ferase_buffer ();
7981 if (STRINGP (string))
7983 int nchars;
7985 if (nbytes == 0)
7986 nbytes = SBYTES (string);
7987 nchars = string_byte_to_char (string, nbytes);
7989 /* This function takes care of single/multibyte conversion. We
7990 just have to ensure that the echo area buffer has the right
7991 setting of enable_multibyte_characters. */
7992 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7994 else if (s)
7996 if (nbytes == 0)
7997 nbytes = strlen (s);
7999 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8001 /* Convert from multi-byte to single-byte. */
8002 int i, c, n;
8003 unsigned char work[1];
8005 /* Convert a multibyte string to single-byte. */
8006 for (i = 0; i < nbytes; i += n)
8008 c = string_char_and_length (s + i, nbytes - i, &n);
8009 work[0] = (SINGLE_BYTE_CHAR_P (c)
8011 : multibyte_char_to_unibyte (c, Qnil));
8012 insert_1_both (work, 1, 1, 1, 0, 0);
8015 else if (!multibyte_p
8016 && !NILP (current_buffer->enable_multibyte_characters))
8018 /* Convert from single-byte to multi-byte. */
8019 int i, c, n;
8020 const unsigned char *msg = (const unsigned char *) s;
8021 unsigned char str[MAX_MULTIBYTE_LENGTH];
8023 /* Convert a single-byte string to multibyte. */
8024 for (i = 0; i < nbytes; i++)
8026 c = unibyte_char_to_multibyte (msg[i]);
8027 n = CHAR_STRING (c, str);
8028 insert_1_both (str, 1, n, 1, 0, 0);
8031 else
8032 insert_1 (s, nbytes, 1, 0, 0);
8035 return 0;
8039 /* Clear messages. CURRENT_P non-zero means clear the current
8040 message. LAST_DISPLAYED_P non-zero means clear the message
8041 last displayed. */
8043 void
8044 clear_message (current_p, last_displayed_p)
8045 int current_p, last_displayed_p;
8047 if (current_p)
8049 echo_area_buffer[0] = Qnil;
8050 message_cleared_p = 1;
8053 if (last_displayed_p)
8054 echo_area_buffer[1] = Qnil;
8056 message_buf_print = 0;
8059 /* Clear garbaged frames.
8061 This function is used where the old redisplay called
8062 redraw_garbaged_frames which in turn called redraw_frame which in
8063 turn called clear_frame. The call to clear_frame was a source of
8064 flickering. I believe a clear_frame is not necessary. It should
8065 suffice in the new redisplay to invalidate all current matrices,
8066 and ensure a complete redisplay of all windows. */
8068 static void
8069 clear_garbaged_frames ()
8071 if (frame_garbaged)
8073 Lisp_Object tail, frame;
8074 int changed_count = 0;
8076 FOR_EACH_FRAME (tail, frame)
8078 struct frame *f = XFRAME (frame);
8080 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8082 if (f->resized_p)
8084 Fredraw_frame (frame);
8085 f->force_flush_display_p = 1;
8087 clear_current_matrices (f);
8088 changed_count++;
8089 f->garbaged = 0;
8090 f->resized_p = 0;
8094 frame_garbaged = 0;
8095 if (changed_count)
8096 ++windows_or_buffers_changed;
8101 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8102 is non-zero update selected_frame. Value is non-zero if the
8103 mini-windows height has been changed. */
8105 static int
8106 echo_area_display (update_frame_p)
8107 int update_frame_p;
8109 Lisp_Object mini_window;
8110 struct window *w;
8111 struct frame *f;
8112 int window_height_changed_p = 0;
8113 struct frame *sf = SELECTED_FRAME ();
8115 mini_window = FRAME_MINIBUF_WINDOW (sf);
8116 w = XWINDOW (mini_window);
8117 f = XFRAME (WINDOW_FRAME (w));
8119 /* Don't display if frame is invisible or not yet initialized. */
8120 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8121 return 0;
8123 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8124 #ifndef MAC_OS8
8125 #ifdef HAVE_WINDOW_SYSTEM
8126 /* When Emacs starts, selected_frame may be a visible terminal
8127 frame, even if we run under a window system. If we let this
8128 through, a message would be displayed on the terminal. */
8129 if (EQ (selected_frame, Vterminal_frame)
8130 && !NILP (Vwindow_system))
8131 return 0;
8132 #endif /* HAVE_WINDOW_SYSTEM */
8133 #endif
8135 /* Redraw garbaged frames. */
8136 if (frame_garbaged)
8137 clear_garbaged_frames ();
8139 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8141 echo_area_window = mini_window;
8142 window_height_changed_p = display_echo_area (w);
8143 w->must_be_updated_p = 1;
8145 /* Update the display, unless called from redisplay_internal.
8146 Also don't update the screen during redisplay itself. The
8147 update will happen at the end of redisplay, and an update
8148 here could cause confusion. */
8149 if (update_frame_p && !redisplaying_p)
8151 int n = 0;
8153 /* If the display update has been interrupted by pending
8154 input, update mode lines in the frame. Due to the
8155 pending input, it might have been that redisplay hasn't
8156 been called, so that mode lines above the echo area are
8157 garbaged. This looks odd, so we prevent it here. */
8158 if (!display_completed)
8159 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8161 if (window_height_changed_p
8162 /* Don't do this if Emacs is shutting down. Redisplay
8163 needs to run hooks. */
8164 && !NILP (Vrun_hooks))
8166 /* Must update other windows. Likewise as in other
8167 cases, don't let this update be interrupted by
8168 pending input. */
8169 int count = SPECPDL_INDEX ();
8170 specbind (Qredisplay_dont_pause, Qt);
8171 windows_or_buffers_changed = 1;
8172 redisplay_internal (0);
8173 unbind_to (count, Qnil);
8175 else if (FRAME_WINDOW_P (f) && n == 0)
8177 /* Window configuration is the same as before.
8178 Can do with a display update of the echo area,
8179 unless we displayed some mode lines. */
8180 update_single_window (w, 1);
8181 rif->flush_display (f);
8183 else
8184 update_frame (f, 1, 1);
8186 /* If cursor is in the echo area, make sure that the next
8187 redisplay displays the minibuffer, so that the cursor will
8188 be replaced with what the minibuffer wants. */
8189 if (cursor_in_echo_area)
8190 ++windows_or_buffers_changed;
8193 else if (!EQ (mini_window, selected_window))
8194 windows_or_buffers_changed++;
8196 /* The current message is now also the last one displayed. */
8197 echo_area_buffer[1] = echo_area_buffer[0];
8199 /* Prevent redisplay optimization in redisplay_internal by resetting
8200 this_line_start_pos. This is done because the mini-buffer now
8201 displays the message instead of its buffer text. */
8202 if (EQ (mini_window, selected_window))
8203 CHARPOS (this_line_start_pos) = 0;
8205 return window_height_changed_p;
8210 /***********************************************************************
8211 Mode Lines and Frame Titles
8212 ***********************************************************************/
8214 /* A buffer for constructing non-propertized mode-line strings and
8215 frame titles in it; allocated from the heap in init_xdisp and
8216 resized as needed in store_mode_line_noprop_char. */
8218 static char *mode_line_noprop_buf;
8220 /* The buffer's end, and a current output position in it. */
8222 static char *mode_line_noprop_buf_end;
8223 static char *mode_line_noprop_ptr;
8225 #define MODE_LINE_NOPROP_LEN(start) \
8226 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8228 static enum {
8229 MODE_LINE_DISPLAY = 0,
8230 MODE_LINE_TITLE,
8231 MODE_LINE_NOPROP,
8232 MODE_LINE_STRING
8233 } mode_line_target;
8235 /* Alist that caches the results of :propertize.
8236 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8237 static Lisp_Object mode_line_proptrans_alist;
8239 /* List of strings making up the mode-line. */
8240 static Lisp_Object mode_line_string_list;
8242 /* Base face property when building propertized mode line string. */
8243 static Lisp_Object mode_line_string_face;
8244 static Lisp_Object mode_line_string_face_prop;
8247 /* Unwind data for mode line strings */
8249 static Lisp_Object Vmode_line_unwind_vector;
8251 static Lisp_Object
8252 format_mode_line_unwind_data (obuf)
8253 struct buffer *obuf;
8255 Lisp_Object vector;
8257 /* Reduce consing by keeping one vector in
8258 Vwith_echo_area_save_vector. */
8259 vector = Vmode_line_unwind_vector;
8260 Vmode_line_unwind_vector = Qnil;
8262 if (NILP (vector))
8263 vector = Fmake_vector (make_number (7), Qnil);
8265 AREF (vector, 0) = make_number (mode_line_target);
8266 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8267 AREF (vector, 2) = mode_line_string_list;
8268 AREF (vector, 3) = mode_line_proptrans_alist;
8269 AREF (vector, 4) = mode_line_string_face;
8270 AREF (vector, 5) = mode_line_string_face_prop;
8272 if (obuf)
8273 XSETBUFFER (AREF (vector, 6), obuf);
8274 else
8275 AREF (vector, 6) = Qnil;
8277 return vector;
8280 static Lisp_Object
8281 unwind_format_mode_line (vector)
8282 Lisp_Object vector;
8284 mode_line_target = XINT (AREF (vector, 0));
8285 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8286 mode_line_string_list = AREF (vector, 2);
8287 mode_line_proptrans_alist = AREF (vector, 3);
8288 mode_line_string_face = AREF (vector, 4);
8289 mode_line_string_face_prop = AREF (vector, 5);
8291 if (!NILP (AREF (vector, 6)))
8293 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8294 AREF (vector, 6) = Qnil;
8297 Vmode_line_unwind_vector = vector;
8298 return Qnil;
8302 /* Store a single character C for the frame title in mode_line_noprop_buf.
8303 Re-allocate mode_line_noprop_buf if necessary. */
8305 static void
8306 #ifdef PROTOTYPES
8307 store_mode_line_noprop_char (char c)
8308 #else
8309 store_mode_line_noprop_char (c)
8310 char c;
8311 #endif
8313 /* If output position has reached the end of the allocated buffer,
8314 double the buffer's size. */
8315 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8317 int len = MODE_LINE_NOPROP_LEN (0);
8318 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8319 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8320 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8321 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8324 *mode_line_noprop_ptr++ = c;
8328 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8329 mode_line_noprop_ptr. STR is the string to store. Do not copy
8330 characters that yield more columns than PRECISION; PRECISION <= 0
8331 means copy the whole string. Pad with spaces until FIELD_WIDTH
8332 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8333 pad. Called from display_mode_element when it is used to build a
8334 frame title. */
8336 static int
8337 store_mode_line_noprop (str, field_width, precision)
8338 const unsigned char *str;
8339 int field_width, precision;
8341 int n = 0;
8342 int dummy, nbytes;
8344 /* Copy at most PRECISION chars from STR. */
8345 nbytes = strlen (str);
8346 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8347 while (nbytes--)
8348 store_mode_line_noprop_char (*str++);
8350 /* Fill up with spaces until FIELD_WIDTH reached. */
8351 while (field_width > 0
8352 && n < field_width)
8354 store_mode_line_noprop_char (' ');
8355 ++n;
8358 return n;
8361 /***********************************************************************
8362 Frame Titles
8363 ***********************************************************************/
8365 #ifdef HAVE_WINDOW_SYSTEM
8367 /* Set the title of FRAME, if it has changed. The title format is
8368 Vicon_title_format if FRAME is iconified, otherwise it is
8369 frame_title_format. */
8371 static void
8372 x_consider_frame_title (frame)
8373 Lisp_Object frame;
8375 struct frame *f = XFRAME (frame);
8377 if (FRAME_WINDOW_P (f)
8378 || FRAME_MINIBUF_ONLY_P (f)
8379 || f->explicit_name)
8381 /* Do we have more than one visible frame on this X display? */
8382 Lisp_Object tail;
8383 Lisp_Object fmt;
8384 int title_start;
8385 char *title;
8386 int len;
8387 struct it it;
8388 int count = SPECPDL_INDEX ();
8390 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8392 Lisp_Object other_frame = XCAR (tail);
8393 struct frame *tf = XFRAME (other_frame);
8395 if (tf != f
8396 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8397 && !FRAME_MINIBUF_ONLY_P (tf)
8398 && !EQ (other_frame, tip_frame)
8399 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8400 break;
8403 /* Set global variable indicating that multiple frames exist. */
8404 multiple_frames = CONSP (tail);
8406 /* Switch to the buffer of selected window of the frame. Set up
8407 mode_line_target so that display_mode_element will output into
8408 mode_line_noprop_buf; then display the title. */
8409 record_unwind_protect (unwind_format_mode_line,
8410 format_mode_line_unwind_data (current_buffer));
8412 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8413 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8415 mode_line_target = MODE_LINE_TITLE;
8416 title_start = MODE_LINE_NOPROP_LEN (0);
8417 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8418 NULL, DEFAULT_FACE_ID);
8419 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8420 len = MODE_LINE_NOPROP_LEN (title_start);
8421 title = mode_line_noprop_buf + title_start;
8422 unbind_to (count, Qnil);
8424 /* Set the title only if it's changed. This avoids consing in
8425 the common case where it hasn't. (If it turns out that we've
8426 already wasted too much time by walking through the list with
8427 display_mode_element, then we might need to optimize at a
8428 higher level than this.) */
8429 if (! STRINGP (f->name)
8430 || SBYTES (f->name) != len
8431 || bcmp (title, SDATA (f->name), len) != 0)
8432 x_implicitly_set_name (f, make_string (title, len), Qnil);
8436 #endif /* not HAVE_WINDOW_SYSTEM */
8441 /***********************************************************************
8442 Menu Bars
8443 ***********************************************************************/
8446 /* Prepare for redisplay by updating menu-bar item lists when
8447 appropriate. This can call eval. */
8449 void
8450 prepare_menu_bars ()
8452 int all_windows;
8453 struct gcpro gcpro1, gcpro2;
8454 struct frame *f;
8455 Lisp_Object tooltip_frame;
8457 #ifdef HAVE_WINDOW_SYSTEM
8458 tooltip_frame = tip_frame;
8459 #else
8460 tooltip_frame = Qnil;
8461 #endif
8463 /* Update all frame titles based on their buffer names, etc. We do
8464 this before the menu bars so that the buffer-menu will show the
8465 up-to-date frame titles. */
8466 #ifdef HAVE_WINDOW_SYSTEM
8467 if (windows_or_buffers_changed || update_mode_lines)
8469 Lisp_Object tail, frame;
8471 FOR_EACH_FRAME (tail, frame)
8473 f = XFRAME (frame);
8474 if (!EQ (frame, tooltip_frame)
8475 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8476 x_consider_frame_title (frame);
8479 #endif /* HAVE_WINDOW_SYSTEM */
8481 /* Update the menu bar item lists, if appropriate. This has to be
8482 done before any actual redisplay or generation of display lines. */
8483 all_windows = (update_mode_lines
8484 || buffer_shared > 1
8485 || windows_or_buffers_changed);
8486 if (all_windows)
8488 Lisp_Object tail, frame;
8489 int count = SPECPDL_INDEX ();
8491 record_unwind_save_match_data ();
8493 FOR_EACH_FRAME (tail, frame)
8495 f = XFRAME (frame);
8497 /* Ignore tooltip frame. */
8498 if (EQ (frame, tooltip_frame))
8499 continue;
8501 /* If a window on this frame changed size, report that to
8502 the user and clear the size-change flag. */
8503 if (FRAME_WINDOW_SIZES_CHANGED (f))
8505 Lisp_Object functions;
8507 /* Clear flag first in case we get an error below. */
8508 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8509 functions = Vwindow_size_change_functions;
8510 GCPRO2 (tail, functions);
8512 while (CONSP (functions))
8514 call1 (XCAR (functions), frame);
8515 functions = XCDR (functions);
8517 UNGCPRO;
8520 GCPRO1 (tail);
8521 update_menu_bar (f, 0);
8522 #ifdef HAVE_WINDOW_SYSTEM
8523 update_tool_bar (f, 0);
8524 #endif
8525 UNGCPRO;
8528 unbind_to (count, Qnil);
8530 else
8532 struct frame *sf = SELECTED_FRAME ();
8533 update_menu_bar (sf, 1);
8534 #ifdef HAVE_WINDOW_SYSTEM
8535 update_tool_bar (sf, 1);
8536 #endif
8539 /* Motif needs this. See comment in xmenu.c. Turn it off when
8540 pending_menu_activation is not defined. */
8541 #ifdef USE_X_TOOLKIT
8542 pending_menu_activation = 0;
8543 #endif
8547 /* Update the menu bar item list for frame F. This has to be done
8548 before we start to fill in any display lines, because it can call
8549 eval.
8551 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8553 static void
8554 update_menu_bar (f, save_match_data)
8555 struct frame *f;
8556 int save_match_data;
8558 Lisp_Object window;
8559 register struct window *w;
8561 /* If called recursively during a menu update, do nothing. This can
8562 happen when, for instance, an activate-menubar-hook causes a
8563 redisplay. */
8564 if (inhibit_menubar_update)
8565 return;
8567 window = FRAME_SELECTED_WINDOW (f);
8568 w = XWINDOW (window);
8570 #if 0 /* The if statement below this if statement used to include the
8571 condition !NILP (w->update_mode_line), rather than using
8572 update_mode_lines directly, and this if statement may have
8573 been added to make that condition work. Now the if
8574 statement below matches its comment, this isn't needed. */
8575 if (update_mode_lines)
8576 w->update_mode_line = Qt;
8577 #endif
8579 if (FRAME_WINDOW_P (f)
8581 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8582 || defined (USE_GTK)
8583 FRAME_EXTERNAL_MENU_BAR (f)
8584 #else
8585 FRAME_MENU_BAR_LINES (f) > 0
8586 #endif
8587 : FRAME_MENU_BAR_LINES (f) > 0)
8589 /* If the user has switched buffers or windows, we need to
8590 recompute to reflect the new bindings. But we'll
8591 recompute when update_mode_lines is set too; that means
8592 that people can use force-mode-line-update to request
8593 that the menu bar be recomputed. The adverse effect on
8594 the rest of the redisplay algorithm is about the same as
8595 windows_or_buffers_changed anyway. */
8596 if (windows_or_buffers_changed
8597 /* This used to test w->update_mode_line, but we believe
8598 there is no need to recompute the menu in that case. */
8599 || update_mode_lines
8600 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8601 < BUF_MODIFF (XBUFFER (w->buffer)))
8602 != !NILP (w->last_had_star))
8603 || ((!NILP (Vtransient_mark_mode)
8604 && !NILP (XBUFFER (w->buffer)->mark_active))
8605 != !NILP (w->region_showing)))
8607 struct buffer *prev = current_buffer;
8608 int count = SPECPDL_INDEX ();
8610 specbind (Qinhibit_menubar_update, Qt);
8612 set_buffer_internal_1 (XBUFFER (w->buffer));
8613 if (save_match_data)
8614 record_unwind_save_match_data ();
8615 if (NILP (Voverriding_local_map_menu_flag))
8617 specbind (Qoverriding_terminal_local_map, Qnil);
8618 specbind (Qoverriding_local_map, Qnil);
8621 /* Run the Lucid hook. */
8622 safe_run_hooks (Qactivate_menubar_hook);
8624 /* If it has changed current-menubar from previous value,
8625 really recompute the menu-bar from the value. */
8626 if (! NILP (Vlucid_menu_bar_dirty_flag))
8627 call0 (Qrecompute_lucid_menubar);
8629 safe_run_hooks (Qmenu_bar_update_hook);
8630 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8632 /* Redisplay the menu bar in case we changed it. */
8633 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8634 || defined (USE_GTK)
8635 if (FRAME_WINDOW_P (f)
8636 #if defined (MAC_OS)
8637 /* All frames on Mac OS share the same menubar. So only the
8638 selected frame should be allowed to set it. */
8639 && f == SELECTED_FRAME ()
8640 #endif
8642 set_frame_menubar (f, 0, 0);
8643 else
8644 /* On a terminal screen, the menu bar is an ordinary screen
8645 line, and this makes it get updated. */
8646 w->update_mode_line = Qt;
8647 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8648 /* In the non-toolkit version, the menu bar is an ordinary screen
8649 line, and this makes it get updated. */
8650 w->update_mode_line = Qt;
8651 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8653 unbind_to (count, Qnil);
8654 set_buffer_internal_1 (prev);
8661 /***********************************************************************
8662 Output Cursor
8663 ***********************************************************************/
8665 #ifdef HAVE_WINDOW_SYSTEM
8667 /* EXPORT:
8668 Nominal cursor position -- where to draw output.
8669 HPOS and VPOS are window relative glyph matrix coordinates.
8670 X and Y are window relative pixel coordinates. */
8672 struct cursor_pos output_cursor;
8675 /* EXPORT:
8676 Set the global variable output_cursor to CURSOR. All cursor
8677 positions are relative to updated_window. */
8679 void
8680 set_output_cursor (cursor)
8681 struct cursor_pos *cursor;
8683 output_cursor.hpos = cursor->hpos;
8684 output_cursor.vpos = cursor->vpos;
8685 output_cursor.x = cursor->x;
8686 output_cursor.y = cursor->y;
8690 /* EXPORT for RIF:
8691 Set a nominal cursor position.
8693 HPOS and VPOS are column/row positions in a window glyph matrix. X
8694 and Y are window text area relative pixel positions.
8696 If this is done during an update, updated_window will contain the
8697 window that is being updated and the position is the future output
8698 cursor position for that window. If updated_window is null, use
8699 selected_window and display the cursor at the given position. */
8701 void
8702 x_cursor_to (vpos, hpos, y, x)
8703 int vpos, hpos, y, x;
8705 struct window *w;
8707 /* If updated_window is not set, work on selected_window. */
8708 if (updated_window)
8709 w = updated_window;
8710 else
8711 w = XWINDOW (selected_window);
8713 /* Set the output cursor. */
8714 output_cursor.hpos = hpos;
8715 output_cursor.vpos = vpos;
8716 output_cursor.x = x;
8717 output_cursor.y = y;
8719 /* If not called as part of an update, really display the cursor.
8720 This will also set the cursor position of W. */
8721 if (updated_window == NULL)
8723 BLOCK_INPUT;
8724 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8725 if (rif->flush_display_optional)
8726 rif->flush_display_optional (SELECTED_FRAME ());
8727 UNBLOCK_INPUT;
8731 #endif /* HAVE_WINDOW_SYSTEM */
8734 /***********************************************************************
8735 Tool-bars
8736 ***********************************************************************/
8738 #ifdef HAVE_WINDOW_SYSTEM
8740 /* Where the mouse was last time we reported a mouse event. */
8742 FRAME_PTR last_mouse_frame;
8744 /* Tool-bar item index of the item on which a mouse button was pressed
8745 or -1. */
8747 int last_tool_bar_item;
8750 /* Update the tool-bar item list for frame F. This has to be done
8751 before we start to fill in any display lines. Called from
8752 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8753 and restore it here. */
8755 static void
8756 update_tool_bar (f, save_match_data)
8757 struct frame *f;
8758 int save_match_data;
8760 #ifdef USE_GTK
8761 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8762 #else
8763 int do_update = WINDOWP (f->tool_bar_window)
8764 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8765 #endif
8767 if (do_update)
8769 Lisp_Object window;
8770 struct window *w;
8772 window = FRAME_SELECTED_WINDOW (f);
8773 w = XWINDOW (window);
8775 /* If the user has switched buffers or windows, we need to
8776 recompute to reflect the new bindings. But we'll
8777 recompute when update_mode_lines is set too; that means
8778 that people can use force-mode-line-update to request
8779 that the menu bar be recomputed. The adverse effect on
8780 the rest of the redisplay algorithm is about the same as
8781 windows_or_buffers_changed anyway. */
8782 if (windows_or_buffers_changed
8783 || !NILP (w->update_mode_line)
8784 || update_mode_lines
8785 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8786 < BUF_MODIFF (XBUFFER (w->buffer)))
8787 != !NILP (w->last_had_star))
8788 || ((!NILP (Vtransient_mark_mode)
8789 && !NILP (XBUFFER (w->buffer)->mark_active))
8790 != !NILP (w->region_showing)))
8792 struct buffer *prev = current_buffer;
8793 int count = SPECPDL_INDEX ();
8794 Lisp_Object new_tool_bar;
8795 int new_n_tool_bar;
8796 struct gcpro gcpro1;
8798 /* Set current_buffer to the buffer of the selected
8799 window of the frame, so that we get the right local
8800 keymaps. */
8801 set_buffer_internal_1 (XBUFFER (w->buffer));
8803 /* Save match data, if we must. */
8804 if (save_match_data)
8805 record_unwind_save_match_data ();
8807 /* Make sure that we don't accidentally use bogus keymaps. */
8808 if (NILP (Voverriding_local_map_menu_flag))
8810 specbind (Qoverriding_terminal_local_map, Qnil);
8811 specbind (Qoverriding_local_map, Qnil);
8814 GCPRO1 (new_tool_bar);
8816 /* Build desired tool-bar items from keymaps. */
8817 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8818 &new_n_tool_bar);
8820 /* Redisplay the tool-bar if we changed it. */
8821 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8823 /* Redisplay that happens asynchronously due to an expose event
8824 may access f->tool_bar_items. Make sure we update both
8825 variables within BLOCK_INPUT so no such event interrupts. */
8826 BLOCK_INPUT;
8827 f->tool_bar_items = new_tool_bar;
8828 f->n_tool_bar_items = new_n_tool_bar;
8829 w->update_mode_line = Qt;
8830 UNBLOCK_INPUT;
8833 UNGCPRO;
8835 unbind_to (count, Qnil);
8836 set_buffer_internal_1 (prev);
8842 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8843 F's desired tool-bar contents. F->tool_bar_items must have
8844 been set up previously by calling prepare_menu_bars. */
8846 static void
8847 build_desired_tool_bar_string (f)
8848 struct frame *f;
8850 int i, size, size_needed;
8851 struct gcpro gcpro1, gcpro2, gcpro3;
8852 Lisp_Object image, plist, props;
8854 image = plist = props = Qnil;
8855 GCPRO3 (image, plist, props);
8857 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8858 Otherwise, make a new string. */
8860 /* The size of the string we might be able to reuse. */
8861 size = (STRINGP (f->desired_tool_bar_string)
8862 ? SCHARS (f->desired_tool_bar_string)
8863 : 0);
8865 /* We need one space in the string for each image. */
8866 size_needed = f->n_tool_bar_items;
8868 /* Reuse f->desired_tool_bar_string, if possible. */
8869 if (size < size_needed || NILP (f->desired_tool_bar_string))
8870 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8871 make_number (' '));
8872 else
8874 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8875 Fremove_text_properties (make_number (0), make_number (size),
8876 props, f->desired_tool_bar_string);
8879 /* Put a `display' property on the string for the images to display,
8880 put a `menu_item' property on tool-bar items with a value that
8881 is the index of the item in F's tool-bar item vector. */
8882 for (i = 0; i < f->n_tool_bar_items; ++i)
8884 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8886 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8887 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8888 int hmargin, vmargin, relief, idx, end;
8889 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8891 /* If image is a vector, choose the image according to the
8892 button state. */
8893 image = PROP (TOOL_BAR_ITEM_IMAGES);
8894 if (VECTORP (image))
8896 if (enabled_p)
8897 idx = (selected_p
8898 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8899 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8900 else
8901 idx = (selected_p
8902 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8903 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8905 xassert (ASIZE (image) >= idx);
8906 image = AREF (image, idx);
8908 else
8909 idx = -1;
8911 /* Ignore invalid image specifications. */
8912 if (!valid_image_p (image))
8913 continue;
8915 /* Display the tool-bar button pressed, or depressed. */
8916 plist = Fcopy_sequence (XCDR (image));
8918 /* Compute margin and relief to draw. */
8919 relief = (tool_bar_button_relief >= 0
8920 ? tool_bar_button_relief
8921 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8922 hmargin = vmargin = relief;
8924 if (INTEGERP (Vtool_bar_button_margin)
8925 && XINT (Vtool_bar_button_margin) > 0)
8927 hmargin += XFASTINT (Vtool_bar_button_margin);
8928 vmargin += XFASTINT (Vtool_bar_button_margin);
8930 else if (CONSP (Vtool_bar_button_margin))
8932 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8933 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8934 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8936 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8937 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8938 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8941 if (auto_raise_tool_bar_buttons_p)
8943 /* Add a `:relief' property to the image spec if the item is
8944 selected. */
8945 if (selected_p)
8947 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8948 hmargin -= relief;
8949 vmargin -= relief;
8952 else
8954 /* If image is selected, display it pressed, i.e. with a
8955 negative relief. If it's not selected, display it with a
8956 raised relief. */
8957 plist = Fplist_put (plist, QCrelief,
8958 (selected_p
8959 ? make_number (-relief)
8960 : make_number (relief)));
8961 hmargin -= relief;
8962 vmargin -= relief;
8965 /* Put a margin around the image. */
8966 if (hmargin || vmargin)
8968 if (hmargin == vmargin)
8969 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8970 else
8971 plist = Fplist_put (plist, QCmargin,
8972 Fcons (make_number (hmargin),
8973 make_number (vmargin)));
8976 /* If button is not enabled, and we don't have special images
8977 for the disabled state, make the image appear disabled by
8978 applying an appropriate algorithm to it. */
8979 if (!enabled_p && idx < 0)
8980 plist = Fplist_put (plist, QCconversion, Qdisabled);
8982 /* Put a `display' text property on the string for the image to
8983 display. Put a `menu-item' property on the string that gives
8984 the start of this item's properties in the tool-bar items
8985 vector. */
8986 image = Fcons (Qimage, plist);
8987 props = list4 (Qdisplay, image,
8988 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8990 /* Let the last image hide all remaining spaces in the tool bar
8991 string. The string can be longer than needed when we reuse a
8992 previous string. */
8993 if (i + 1 == f->n_tool_bar_items)
8994 end = SCHARS (f->desired_tool_bar_string);
8995 else
8996 end = i + 1;
8997 Fadd_text_properties (make_number (i), make_number (end),
8998 props, f->desired_tool_bar_string);
8999 #undef PROP
9002 UNGCPRO;
9006 /* Display one line of the tool-bar of frame IT->f. */
9008 static void
9009 display_tool_bar_line (it)
9010 struct it *it;
9012 struct glyph_row *row = it->glyph_row;
9013 int max_x = it->last_visible_x;
9014 struct glyph *last;
9016 prepare_desired_row (row);
9017 row->y = it->current_y;
9019 /* Note that this isn't made use of if the face hasn't a box,
9020 so there's no need to check the face here. */
9021 it->start_of_box_run_p = 1;
9023 while (it->current_x < max_x)
9025 int x_before, x, n_glyphs_before, i, nglyphs;
9027 /* Get the next display element. */
9028 if (!get_next_display_element (it))
9029 break;
9031 /* Produce glyphs. */
9032 x_before = it->current_x;
9033 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9034 PRODUCE_GLYPHS (it);
9036 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9037 i = 0;
9038 x = x_before;
9039 while (i < nglyphs)
9041 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9043 if (x + glyph->pixel_width > max_x)
9045 /* Glyph doesn't fit on line. */
9046 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9047 it->current_x = x;
9048 goto out;
9051 ++it->hpos;
9052 x += glyph->pixel_width;
9053 ++i;
9056 /* Stop at line ends. */
9057 if (ITERATOR_AT_END_OF_LINE_P (it))
9058 break;
9060 set_iterator_to_next (it, 1);
9063 out:;
9065 row->displays_text_p = row->used[TEXT_AREA] != 0;
9066 extend_face_to_end_of_line (it);
9067 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9068 last->right_box_line_p = 1;
9069 if (last == row->glyphs[TEXT_AREA])
9070 last->left_box_line_p = 1;
9071 compute_line_metrics (it);
9073 /* If line is empty, make it occupy the rest of the tool-bar. */
9074 if (!row->displays_text_p)
9076 row->height = row->phys_height = it->last_visible_y - row->y;
9077 row->ascent = row->phys_ascent = 0;
9078 row->extra_line_spacing = 0;
9081 row->full_width_p = 1;
9082 row->continued_p = 0;
9083 row->truncated_on_left_p = 0;
9084 row->truncated_on_right_p = 0;
9086 it->current_x = it->hpos = 0;
9087 it->current_y += row->height;
9088 ++it->vpos;
9089 ++it->glyph_row;
9093 /* Value is the number of screen lines needed to make all tool-bar
9094 items of frame F visible. */
9096 static int
9097 tool_bar_lines_needed (f)
9098 struct frame *f;
9100 struct window *w = XWINDOW (f->tool_bar_window);
9101 struct it it;
9103 /* Initialize an iterator for iteration over
9104 F->desired_tool_bar_string in the tool-bar window of frame F. */
9105 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9106 it.first_visible_x = 0;
9107 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9108 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9110 while (!ITERATOR_AT_END_P (&it))
9112 it.glyph_row = w->desired_matrix->rows;
9113 clear_glyph_row (it.glyph_row);
9114 display_tool_bar_line (&it);
9117 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9121 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9122 0, 1, 0,
9123 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9124 (frame)
9125 Lisp_Object frame;
9127 struct frame *f;
9128 struct window *w;
9129 int nlines = 0;
9131 if (NILP (frame))
9132 frame = selected_frame;
9133 else
9134 CHECK_FRAME (frame);
9135 f = XFRAME (frame);
9137 if (WINDOWP (f->tool_bar_window)
9138 || (w = XWINDOW (f->tool_bar_window),
9139 WINDOW_TOTAL_LINES (w) > 0))
9141 update_tool_bar (f, 1);
9142 if (f->n_tool_bar_items)
9144 build_desired_tool_bar_string (f);
9145 nlines = tool_bar_lines_needed (f);
9149 return make_number (nlines);
9153 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9154 height should be changed. */
9156 static int
9157 redisplay_tool_bar (f)
9158 struct frame *f;
9160 struct window *w;
9161 struct it it;
9162 struct glyph_row *row;
9163 int change_height_p = 0;
9165 #ifdef USE_GTK
9166 if (FRAME_EXTERNAL_TOOL_BAR (f))
9167 update_frame_tool_bar (f);
9168 return 0;
9169 #endif
9171 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9172 do anything. This means you must start with tool-bar-lines
9173 non-zero to get the auto-sizing effect. Or in other words, you
9174 can turn off tool-bars by specifying tool-bar-lines zero. */
9175 if (!WINDOWP (f->tool_bar_window)
9176 || (w = XWINDOW (f->tool_bar_window),
9177 WINDOW_TOTAL_LINES (w) == 0))
9178 return 0;
9180 /* Set up an iterator for the tool-bar window. */
9181 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9182 it.first_visible_x = 0;
9183 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9184 row = it.glyph_row;
9186 /* Build a string that represents the contents of the tool-bar. */
9187 build_desired_tool_bar_string (f);
9188 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9190 /* Display as many lines as needed to display all tool-bar items. */
9191 while (it.current_y < it.last_visible_y)
9192 display_tool_bar_line (&it);
9194 /* It doesn't make much sense to try scrolling in the tool-bar
9195 window, so don't do it. */
9196 w->desired_matrix->no_scrolling_p = 1;
9197 w->must_be_updated_p = 1;
9199 if (auto_resize_tool_bars_p)
9201 int nlines;
9203 /* If we couldn't display everything, change the tool-bar's
9204 height. */
9205 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9206 change_height_p = 1;
9208 /* If there are blank lines at the end, except for a partially
9209 visible blank line at the end that is smaller than
9210 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9211 row = it.glyph_row - 1;
9212 if (!row->displays_text_p
9213 && row->height >= FRAME_LINE_HEIGHT (f))
9214 change_height_p = 1;
9216 /* If row displays tool-bar items, but is partially visible,
9217 change the tool-bar's height. */
9218 if (row->displays_text_p
9219 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9220 change_height_p = 1;
9222 /* Resize windows as needed by changing the `tool-bar-lines'
9223 frame parameter. */
9224 if (change_height_p
9225 && (nlines = tool_bar_lines_needed (f),
9226 nlines != WINDOW_TOTAL_LINES (w)))
9228 extern Lisp_Object Qtool_bar_lines;
9229 Lisp_Object frame;
9230 int old_height = WINDOW_TOTAL_LINES (w);
9232 XSETFRAME (frame, f);
9233 clear_glyph_matrix (w->desired_matrix);
9234 Fmodify_frame_parameters (frame,
9235 Fcons (Fcons (Qtool_bar_lines,
9236 make_number (nlines)),
9237 Qnil));
9238 if (WINDOW_TOTAL_LINES (w) != old_height)
9239 fonts_changed_p = 1;
9243 return change_height_p;
9247 /* Get information about the tool-bar item which is displayed in GLYPH
9248 on frame F. Return in *PROP_IDX the index where tool-bar item
9249 properties start in F->tool_bar_items. Value is zero if
9250 GLYPH doesn't display a tool-bar item. */
9252 static int
9253 tool_bar_item_info (f, glyph, prop_idx)
9254 struct frame *f;
9255 struct glyph *glyph;
9256 int *prop_idx;
9258 Lisp_Object prop;
9259 int success_p;
9260 int charpos;
9262 /* This function can be called asynchronously, which means we must
9263 exclude any possibility that Fget_text_property signals an
9264 error. */
9265 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9266 charpos = max (0, charpos);
9268 /* Get the text property `menu-item' at pos. The value of that
9269 property is the start index of this item's properties in
9270 F->tool_bar_items. */
9271 prop = Fget_text_property (make_number (charpos),
9272 Qmenu_item, f->current_tool_bar_string);
9273 if (INTEGERP (prop))
9275 *prop_idx = XINT (prop);
9276 success_p = 1;
9278 else
9279 success_p = 0;
9281 return success_p;
9285 /* Get information about the tool-bar item at position X/Y on frame F.
9286 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9287 the current matrix of the tool-bar window of F, or NULL if not
9288 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9289 item in F->tool_bar_items. Value is
9291 -1 if X/Y is not on a tool-bar item
9292 0 if X/Y is on the same item that was highlighted before.
9293 1 otherwise. */
9295 static int
9296 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9297 struct frame *f;
9298 int x, y;
9299 struct glyph **glyph;
9300 int *hpos, *vpos, *prop_idx;
9302 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9303 struct window *w = XWINDOW (f->tool_bar_window);
9304 int area;
9306 /* Find the glyph under X/Y. */
9307 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9308 if (*glyph == NULL)
9309 return -1;
9311 /* Get the start of this tool-bar item's properties in
9312 f->tool_bar_items. */
9313 if (!tool_bar_item_info (f, *glyph, prop_idx))
9314 return -1;
9316 /* Is mouse on the highlighted item? */
9317 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9318 && *vpos >= dpyinfo->mouse_face_beg_row
9319 && *vpos <= dpyinfo->mouse_face_end_row
9320 && (*vpos > dpyinfo->mouse_face_beg_row
9321 || *hpos >= dpyinfo->mouse_face_beg_col)
9322 && (*vpos < dpyinfo->mouse_face_end_row
9323 || *hpos < dpyinfo->mouse_face_end_col
9324 || dpyinfo->mouse_face_past_end))
9325 return 0;
9327 return 1;
9331 /* EXPORT:
9332 Handle mouse button event on the tool-bar of frame F, at
9333 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9334 0 for button release. MODIFIERS is event modifiers for button
9335 release. */
9337 void
9338 handle_tool_bar_click (f, x, y, down_p, modifiers)
9339 struct frame *f;
9340 int x, y, down_p;
9341 unsigned int modifiers;
9343 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9344 struct window *w = XWINDOW (f->tool_bar_window);
9345 int hpos, vpos, prop_idx;
9346 struct glyph *glyph;
9347 Lisp_Object enabled_p;
9349 /* If not on the highlighted tool-bar item, return. */
9350 frame_to_window_pixel_xy (w, &x, &y);
9351 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9352 return;
9354 /* If item is disabled, do nothing. */
9355 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9356 if (NILP (enabled_p))
9357 return;
9359 if (down_p)
9361 /* Show item in pressed state. */
9362 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9363 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9364 last_tool_bar_item = prop_idx;
9366 else
9368 Lisp_Object key, frame;
9369 struct input_event event;
9370 EVENT_INIT (event);
9372 /* Show item in released state. */
9373 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9374 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9376 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9378 XSETFRAME (frame, f);
9379 event.kind = TOOL_BAR_EVENT;
9380 event.frame_or_window = frame;
9381 event.arg = frame;
9382 kbd_buffer_store_event (&event);
9384 event.kind = TOOL_BAR_EVENT;
9385 event.frame_or_window = frame;
9386 event.arg = key;
9387 event.modifiers = modifiers;
9388 kbd_buffer_store_event (&event);
9389 last_tool_bar_item = -1;
9394 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9395 tool-bar window-relative coordinates X/Y. Called from
9396 note_mouse_highlight. */
9398 static void
9399 note_tool_bar_highlight (f, x, y)
9400 struct frame *f;
9401 int x, y;
9403 Lisp_Object window = f->tool_bar_window;
9404 struct window *w = XWINDOW (window);
9405 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9406 int hpos, vpos;
9407 struct glyph *glyph;
9408 struct glyph_row *row;
9409 int i;
9410 Lisp_Object enabled_p;
9411 int prop_idx;
9412 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9413 int mouse_down_p, rc;
9415 /* Function note_mouse_highlight is called with negative x(y
9416 values when mouse moves outside of the frame. */
9417 if (x <= 0 || y <= 0)
9419 clear_mouse_face (dpyinfo);
9420 return;
9423 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9424 if (rc < 0)
9426 /* Not on tool-bar item. */
9427 clear_mouse_face (dpyinfo);
9428 return;
9430 else if (rc == 0)
9431 /* On same tool-bar item as before. */
9432 goto set_help_echo;
9434 clear_mouse_face (dpyinfo);
9436 /* Mouse is down, but on different tool-bar item? */
9437 mouse_down_p = (dpyinfo->grabbed
9438 && f == last_mouse_frame
9439 && FRAME_LIVE_P (f));
9440 if (mouse_down_p
9441 && last_tool_bar_item != prop_idx)
9442 return;
9444 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9445 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9447 /* If tool-bar item is not enabled, don't highlight it. */
9448 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9449 if (!NILP (enabled_p))
9451 /* Compute the x-position of the glyph. In front and past the
9452 image is a space. We include this in the highlighted area. */
9453 row = MATRIX_ROW (w->current_matrix, vpos);
9454 for (i = x = 0; i < hpos; ++i)
9455 x += row->glyphs[TEXT_AREA][i].pixel_width;
9457 /* Record this as the current active region. */
9458 dpyinfo->mouse_face_beg_col = hpos;
9459 dpyinfo->mouse_face_beg_row = vpos;
9460 dpyinfo->mouse_face_beg_x = x;
9461 dpyinfo->mouse_face_beg_y = row->y;
9462 dpyinfo->mouse_face_past_end = 0;
9464 dpyinfo->mouse_face_end_col = hpos + 1;
9465 dpyinfo->mouse_face_end_row = vpos;
9466 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9467 dpyinfo->mouse_face_end_y = row->y;
9468 dpyinfo->mouse_face_window = window;
9469 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9471 /* Display it as active. */
9472 show_mouse_face (dpyinfo, draw);
9473 dpyinfo->mouse_face_image_state = draw;
9476 set_help_echo:
9478 /* Set help_echo_string to a help string to display for this tool-bar item.
9479 XTread_socket does the rest. */
9480 help_echo_object = help_echo_window = Qnil;
9481 help_echo_pos = -1;
9482 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9483 if (NILP (help_echo_string))
9484 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9487 #endif /* HAVE_WINDOW_SYSTEM */
9491 /************************************************************************
9492 Horizontal scrolling
9493 ************************************************************************/
9495 static int hscroll_window_tree P_ ((Lisp_Object));
9496 static int hscroll_windows P_ ((Lisp_Object));
9498 /* For all leaf windows in the window tree rooted at WINDOW, set their
9499 hscroll value so that PT is (i) visible in the window, and (ii) so
9500 that it is not within a certain margin at the window's left and
9501 right border. Value is non-zero if any window's hscroll has been
9502 changed. */
9504 static int
9505 hscroll_window_tree (window)
9506 Lisp_Object window;
9508 int hscrolled_p = 0;
9509 int hscroll_relative_p = FLOATP (Vhscroll_step);
9510 int hscroll_step_abs = 0;
9511 double hscroll_step_rel = 0;
9513 if (hscroll_relative_p)
9515 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9516 if (hscroll_step_rel < 0)
9518 hscroll_relative_p = 0;
9519 hscroll_step_abs = 0;
9522 else if (INTEGERP (Vhscroll_step))
9524 hscroll_step_abs = XINT (Vhscroll_step);
9525 if (hscroll_step_abs < 0)
9526 hscroll_step_abs = 0;
9528 else
9529 hscroll_step_abs = 0;
9531 while (WINDOWP (window))
9533 struct window *w = XWINDOW (window);
9535 if (WINDOWP (w->hchild))
9536 hscrolled_p |= hscroll_window_tree (w->hchild);
9537 else if (WINDOWP (w->vchild))
9538 hscrolled_p |= hscroll_window_tree (w->vchild);
9539 else if (w->cursor.vpos >= 0)
9541 int h_margin;
9542 int text_area_width;
9543 struct glyph_row *current_cursor_row
9544 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9545 struct glyph_row *desired_cursor_row
9546 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9547 struct glyph_row *cursor_row
9548 = (desired_cursor_row->enabled_p
9549 ? desired_cursor_row
9550 : current_cursor_row);
9552 text_area_width = window_box_width (w, TEXT_AREA);
9554 /* Scroll when cursor is inside this scroll margin. */
9555 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9557 if ((XFASTINT (w->hscroll)
9558 && w->cursor.x <= h_margin)
9559 || (cursor_row->enabled_p
9560 && cursor_row->truncated_on_right_p
9561 && (w->cursor.x >= text_area_width - h_margin)))
9563 struct it it;
9564 int hscroll;
9565 struct buffer *saved_current_buffer;
9566 int pt;
9567 int wanted_x;
9569 /* Find point in a display of infinite width. */
9570 saved_current_buffer = current_buffer;
9571 current_buffer = XBUFFER (w->buffer);
9573 if (w == XWINDOW (selected_window))
9574 pt = BUF_PT (current_buffer);
9575 else
9577 pt = marker_position (w->pointm);
9578 pt = max (BEGV, pt);
9579 pt = min (ZV, pt);
9582 /* Move iterator to pt starting at cursor_row->start in
9583 a line with infinite width. */
9584 init_to_row_start (&it, w, cursor_row);
9585 it.last_visible_x = INFINITY;
9586 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9587 current_buffer = saved_current_buffer;
9589 /* Position cursor in window. */
9590 if (!hscroll_relative_p && hscroll_step_abs == 0)
9591 hscroll = max (0, (it.current_x
9592 - (ITERATOR_AT_END_OF_LINE_P (&it)
9593 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9594 : (text_area_width / 2))))
9595 / FRAME_COLUMN_WIDTH (it.f);
9596 else if (w->cursor.x >= text_area_width - h_margin)
9598 if (hscroll_relative_p)
9599 wanted_x = text_area_width * (1 - hscroll_step_rel)
9600 - h_margin;
9601 else
9602 wanted_x = text_area_width
9603 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9604 - h_margin;
9605 hscroll
9606 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9608 else
9610 if (hscroll_relative_p)
9611 wanted_x = text_area_width * hscroll_step_rel
9612 + h_margin;
9613 else
9614 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9615 + h_margin;
9616 hscroll
9617 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9619 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9621 /* Don't call Fset_window_hscroll if value hasn't
9622 changed because it will prevent redisplay
9623 optimizations. */
9624 if (XFASTINT (w->hscroll) != hscroll)
9626 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9627 w->hscroll = make_number (hscroll);
9628 hscrolled_p = 1;
9633 window = w->next;
9636 /* Value is non-zero if hscroll of any leaf window has been changed. */
9637 return hscrolled_p;
9641 /* Set hscroll so that cursor is visible and not inside horizontal
9642 scroll margins for all windows in the tree rooted at WINDOW. See
9643 also hscroll_window_tree above. Value is non-zero if any window's
9644 hscroll has been changed. If it has, desired matrices on the frame
9645 of WINDOW are cleared. */
9647 static int
9648 hscroll_windows (window)
9649 Lisp_Object window;
9651 int hscrolled_p;
9653 if (automatic_hscrolling_p)
9655 hscrolled_p = hscroll_window_tree (window);
9656 if (hscrolled_p)
9657 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9659 else
9660 hscrolled_p = 0;
9661 return hscrolled_p;
9666 /************************************************************************
9667 Redisplay
9668 ************************************************************************/
9670 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9671 to a non-zero value. This is sometimes handy to have in a debugger
9672 session. */
9674 #if GLYPH_DEBUG
9676 /* First and last unchanged row for try_window_id. */
9678 int debug_first_unchanged_at_end_vpos;
9679 int debug_last_unchanged_at_beg_vpos;
9681 /* Delta vpos and y. */
9683 int debug_dvpos, debug_dy;
9685 /* Delta in characters and bytes for try_window_id. */
9687 int debug_delta, debug_delta_bytes;
9689 /* Values of window_end_pos and window_end_vpos at the end of
9690 try_window_id. */
9692 EMACS_INT debug_end_pos, debug_end_vpos;
9694 /* Append a string to W->desired_matrix->method. FMT is a printf
9695 format string. A1...A9 are a supplement for a variable-length
9696 argument list. If trace_redisplay_p is non-zero also printf the
9697 resulting string to stderr. */
9699 static void
9700 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9701 struct window *w;
9702 char *fmt;
9703 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9705 char buffer[512];
9706 char *method = w->desired_matrix->method;
9707 int len = strlen (method);
9708 int size = sizeof w->desired_matrix->method;
9709 int remaining = size - len - 1;
9711 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9712 if (len && remaining)
9714 method[len] = '|';
9715 --remaining, ++len;
9718 strncpy (method + len, buffer, remaining);
9720 if (trace_redisplay_p)
9721 fprintf (stderr, "%p (%s): %s\n",
9723 ((BUFFERP (w->buffer)
9724 && STRINGP (XBUFFER (w->buffer)->name))
9725 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9726 : "no buffer"),
9727 buffer);
9730 #endif /* GLYPH_DEBUG */
9733 /* Value is non-zero if all changes in window W, which displays
9734 current_buffer, are in the text between START and END. START is a
9735 buffer position, END is given as a distance from Z. Used in
9736 redisplay_internal for display optimization. */
9738 static INLINE int
9739 text_outside_line_unchanged_p (w, start, end)
9740 struct window *w;
9741 int start, end;
9743 int unchanged_p = 1;
9745 /* If text or overlays have changed, see where. */
9746 if (XFASTINT (w->last_modified) < MODIFF
9747 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9749 /* Gap in the line? */
9750 if (GPT < start || Z - GPT < end)
9751 unchanged_p = 0;
9753 /* Changes start in front of the line, or end after it? */
9754 if (unchanged_p
9755 && (BEG_UNCHANGED < start - 1
9756 || END_UNCHANGED < end))
9757 unchanged_p = 0;
9759 /* If selective display, can't optimize if changes start at the
9760 beginning of the line. */
9761 if (unchanged_p
9762 && INTEGERP (current_buffer->selective_display)
9763 && XINT (current_buffer->selective_display) > 0
9764 && (BEG_UNCHANGED < start || GPT <= start))
9765 unchanged_p = 0;
9767 /* If there are overlays at the start or end of the line, these
9768 may have overlay strings with newlines in them. A change at
9769 START, for instance, may actually concern the display of such
9770 overlay strings as well, and they are displayed on different
9771 lines. So, quickly rule out this case. (For the future, it
9772 might be desirable to implement something more telling than
9773 just BEG/END_UNCHANGED.) */
9774 if (unchanged_p)
9776 if (BEG + BEG_UNCHANGED == start
9777 && overlay_touches_p (start))
9778 unchanged_p = 0;
9779 if (END_UNCHANGED == end
9780 && overlay_touches_p (Z - end))
9781 unchanged_p = 0;
9785 return unchanged_p;
9789 /* Do a frame update, taking possible shortcuts into account. This is
9790 the main external entry point for redisplay.
9792 If the last redisplay displayed an echo area message and that message
9793 is no longer requested, we clear the echo area or bring back the
9794 mini-buffer if that is in use. */
9796 void
9797 redisplay ()
9799 redisplay_internal (0);
9803 static Lisp_Object
9804 overlay_arrow_string_or_property (var)
9805 Lisp_Object var;
9807 Lisp_Object val;
9809 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9810 return val;
9812 return Voverlay_arrow_string;
9815 /* Return 1 if there are any overlay-arrows in current_buffer. */
9816 static int
9817 overlay_arrow_in_current_buffer_p ()
9819 Lisp_Object vlist;
9821 for (vlist = Voverlay_arrow_variable_list;
9822 CONSP (vlist);
9823 vlist = XCDR (vlist))
9825 Lisp_Object var = XCAR (vlist);
9826 Lisp_Object val;
9828 if (!SYMBOLP (var))
9829 continue;
9830 val = find_symbol_value (var);
9831 if (MARKERP (val)
9832 && current_buffer == XMARKER (val)->buffer)
9833 return 1;
9835 return 0;
9839 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9840 has changed. */
9842 static int
9843 overlay_arrows_changed_p ()
9845 Lisp_Object vlist;
9847 for (vlist = Voverlay_arrow_variable_list;
9848 CONSP (vlist);
9849 vlist = XCDR (vlist))
9851 Lisp_Object var = XCAR (vlist);
9852 Lisp_Object val, pstr;
9854 if (!SYMBOLP (var))
9855 continue;
9856 val = find_symbol_value (var);
9857 if (!MARKERP (val))
9858 continue;
9859 if (! EQ (COERCE_MARKER (val),
9860 Fget (var, Qlast_arrow_position))
9861 || ! (pstr = overlay_arrow_string_or_property (var),
9862 EQ (pstr, Fget (var, Qlast_arrow_string))))
9863 return 1;
9865 return 0;
9868 /* Mark overlay arrows to be updated on next redisplay. */
9870 static void
9871 update_overlay_arrows (up_to_date)
9872 int up_to_date;
9874 Lisp_Object vlist;
9876 for (vlist = Voverlay_arrow_variable_list;
9877 CONSP (vlist);
9878 vlist = XCDR (vlist))
9880 Lisp_Object var = XCAR (vlist);
9882 if (!SYMBOLP (var))
9883 continue;
9885 if (up_to_date > 0)
9887 Lisp_Object val = find_symbol_value (var);
9888 Fput (var, Qlast_arrow_position,
9889 COERCE_MARKER (val));
9890 Fput (var, Qlast_arrow_string,
9891 overlay_arrow_string_or_property (var));
9893 else if (up_to_date < 0
9894 || !NILP (Fget (var, Qlast_arrow_position)))
9896 Fput (var, Qlast_arrow_position, Qt);
9897 Fput (var, Qlast_arrow_string, Qt);
9903 /* Return overlay arrow string to display at row.
9904 Return integer (bitmap number) for arrow bitmap in left fringe.
9905 Return nil if no overlay arrow. */
9907 static Lisp_Object
9908 overlay_arrow_at_row (it, row)
9909 struct it *it;
9910 struct glyph_row *row;
9912 Lisp_Object vlist;
9914 for (vlist = Voverlay_arrow_variable_list;
9915 CONSP (vlist);
9916 vlist = XCDR (vlist))
9918 Lisp_Object var = XCAR (vlist);
9919 Lisp_Object val;
9921 if (!SYMBOLP (var))
9922 continue;
9924 val = find_symbol_value (var);
9926 if (MARKERP (val)
9927 && current_buffer == XMARKER (val)->buffer
9928 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9930 if (FRAME_WINDOW_P (it->f)
9931 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9933 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9935 int fringe_bitmap;
9936 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9937 return make_number (fringe_bitmap);
9939 return make_number (-1); /* Use default arrow bitmap */
9941 return overlay_arrow_string_or_property (var);
9945 return Qnil;
9948 /* Return 1 if point moved out of or into a composition. Otherwise
9949 return 0. PREV_BUF and PREV_PT are the last point buffer and
9950 position. BUF and PT are the current point buffer and position. */
9953 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9954 struct buffer *prev_buf, *buf;
9955 int prev_pt, pt;
9957 int start, end;
9958 Lisp_Object prop;
9959 Lisp_Object buffer;
9961 XSETBUFFER (buffer, buf);
9962 /* Check a composition at the last point if point moved within the
9963 same buffer. */
9964 if (prev_buf == buf)
9966 if (prev_pt == pt)
9967 /* Point didn't move. */
9968 return 0;
9970 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9971 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9972 && COMPOSITION_VALID_P (start, end, prop)
9973 && start < prev_pt && end > prev_pt)
9974 /* The last point was within the composition. Return 1 iff
9975 point moved out of the composition. */
9976 return (pt <= start || pt >= end);
9979 /* Check a composition at the current point. */
9980 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9981 && find_composition (pt, -1, &start, &end, &prop, buffer)
9982 && COMPOSITION_VALID_P (start, end, prop)
9983 && start < pt && end > pt);
9987 /* Reconsider the setting of B->clip_changed which is displayed
9988 in window W. */
9990 static INLINE void
9991 reconsider_clip_changes (w, b)
9992 struct window *w;
9993 struct buffer *b;
9995 if (b->clip_changed
9996 && !NILP (w->window_end_valid)
9997 && w->current_matrix->buffer == b
9998 && w->current_matrix->zv == BUF_ZV (b)
9999 && w->current_matrix->begv == BUF_BEGV (b))
10000 b->clip_changed = 0;
10002 /* If display wasn't paused, and W is not a tool bar window, see if
10003 point has been moved into or out of a composition. In that case,
10004 we set b->clip_changed to 1 to force updating the screen. If
10005 b->clip_changed has already been set to 1, we can skip this
10006 check. */
10007 if (!b->clip_changed
10008 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10010 int pt;
10012 if (w == XWINDOW (selected_window))
10013 pt = BUF_PT (current_buffer);
10014 else
10015 pt = marker_position (w->pointm);
10017 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10018 || pt != XINT (w->last_point))
10019 && check_point_in_composition (w->current_matrix->buffer,
10020 XINT (w->last_point),
10021 XBUFFER (w->buffer), pt))
10022 b->clip_changed = 1;
10027 /* Select FRAME to forward the values of frame-local variables into C
10028 variables so that the redisplay routines can access those values
10029 directly. */
10031 static void
10032 select_frame_for_redisplay (frame)
10033 Lisp_Object frame;
10035 Lisp_Object tail, sym, val;
10036 Lisp_Object old = selected_frame;
10038 selected_frame = frame;
10040 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10041 if (CONSP (XCAR (tail))
10042 && (sym = XCAR (XCAR (tail)),
10043 SYMBOLP (sym))
10044 && (sym = indirect_variable (sym),
10045 val = SYMBOL_VALUE (sym),
10046 (BUFFER_LOCAL_VALUEP (val)
10047 || SOME_BUFFER_LOCAL_VALUEP (val)))
10048 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10049 Fsymbol_value (sym);
10051 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10052 if (CONSP (XCAR (tail))
10053 && (sym = XCAR (XCAR (tail)),
10054 SYMBOLP (sym))
10055 && (sym = indirect_variable (sym),
10056 val = SYMBOL_VALUE (sym),
10057 (BUFFER_LOCAL_VALUEP (val)
10058 || SOME_BUFFER_LOCAL_VALUEP (val)))
10059 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10060 Fsymbol_value (sym);
10064 #define STOP_POLLING \
10065 do { if (! polling_stopped_here) stop_polling (); \
10066 polling_stopped_here = 1; } while (0)
10068 #define RESUME_POLLING \
10069 do { if (polling_stopped_here) start_polling (); \
10070 polling_stopped_here = 0; } while (0)
10073 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10074 response to any user action; therefore, we should preserve the echo
10075 area. (Actually, our caller does that job.) Perhaps in the future
10076 avoid recentering windows if it is not necessary; currently that
10077 causes some problems. */
10079 static void
10080 redisplay_internal (preserve_echo_area)
10081 int preserve_echo_area;
10083 struct window *w = XWINDOW (selected_window);
10084 struct frame *f = XFRAME (w->frame);
10085 int pause;
10086 int must_finish = 0;
10087 struct text_pos tlbufpos, tlendpos;
10088 int number_of_visible_frames;
10089 int count;
10090 struct frame *sf = SELECTED_FRAME ();
10091 int polling_stopped_here = 0;
10093 /* Non-zero means redisplay has to consider all windows on all
10094 frames. Zero means, only selected_window is considered. */
10095 int consider_all_windows_p;
10097 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10099 /* No redisplay if running in batch mode or frame is not yet fully
10100 initialized, or redisplay is explicitly turned off by setting
10101 Vinhibit_redisplay. */
10102 if (noninteractive
10103 || !NILP (Vinhibit_redisplay)
10104 || !f->glyphs_initialized_p)
10105 return;
10107 /* The flag redisplay_performed_directly_p is set by
10108 direct_output_for_insert when it already did the whole screen
10109 update necessary. */
10110 if (redisplay_performed_directly_p)
10112 redisplay_performed_directly_p = 0;
10113 if (!hscroll_windows (selected_window))
10114 return;
10117 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10118 if (popup_activated ())
10119 return;
10120 #endif
10122 /* I don't think this happens but let's be paranoid. */
10123 if (redisplaying_p)
10124 return;
10126 /* Record a function that resets redisplaying_p to its old value
10127 when we leave this function. */
10128 count = SPECPDL_INDEX ();
10129 record_unwind_protect (unwind_redisplay,
10130 Fcons (make_number (redisplaying_p), selected_frame));
10131 ++redisplaying_p;
10132 specbind (Qinhibit_free_realized_faces, Qnil);
10134 retry:
10135 pause = 0;
10136 reconsider_clip_changes (w, current_buffer);
10138 /* If new fonts have been loaded that make a glyph matrix adjustment
10139 necessary, do it. */
10140 if (fonts_changed_p)
10142 adjust_glyphs (NULL);
10143 ++windows_or_buffers_changed;
10144 fonts_changed_p = 0;
10147 /* If face_change_count is non-zero, init_iterator will free all
10148 realized faces, which includes the faces referenced from current
10149 matrices. So, we can't reuse current matrices in this case. */
10150 if (face_change_count)
10151 ++windows_or_buffers_changed;
10153 if (! FRAME_WINDOW_P (sf)
10154 && previous_terminal_frame != sf)
10156 /* Since frames on an ASCII terminal share the same display
10157 area, displaying a different frame means redisplay the whole
10158 thing. */
10159 windows_or_buffers_changed++;
10160 SET_FRAME_GARBAGED (sf);
10161 XSETFRAME (Vterminal_frame, sf);
10163 previous_terminal_frame = sf;
10165 /* Set the visible flags for all frames. Do this before checking
10166 for resized or garbaged frames; they want to know if their frames
10167 are visible. See the comment in frame.h for
10168 FRAME_SAMPLE_VISIBILITY. */
10170 Lisp_Object tail, frame;
10172 number_of_visible_frames = 0;
10174 FOR_EACH_FRAME (tail, frame)
10176 struct frame *f = XFRAME (frame);
10178 FRAME_SAMPLE_VISIBILITY (f);
10179 if (FRAME_VISIBLE_P (f))
10180 ++number_of_visible_frames;
10181 clear_desired_matrices (f);
10185 /* Notice any pending interrupt request to change frame size. */
10186 do_pending_window_change (1);
10188 /* Clear frames marked as garbaged. */
10189 if (frame_garbaged)
10190 clear_garbaged_frames ();
10192 /* Build menubar and tool-bar items. */
10193 prepare_menu_bars ();
10195 if (windows_or_buffers_changed)
10196 update_mode_lines++;
10198 /* Detect case that we need to write or remove a star in the mode line. */
10199 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10201 w->update_mode_line = Qt;
10202 if (buffer_shared > 1)
10203 update_mode_lines++;
10206 /* If %c is in the mode line, update it if needed. */
10207 if (!NILP (w->column_number_displayed)
10208 /* This alternative quickly identifies a common case
10209 where no change is needed. */
10210 && !(PT == XFASTINT (w->last_point)
10211 && XFASTINT (w->last_modified) >= MODIFF
10212 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10213 && (XFASTINT (w->column_number_displayed)
10214 != (int) current_column ())) /* iftc */
10215 w->update_mode_line = Qt;
10217 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10219 /* The variable buffer_shared is set in redisplay_window and
10220 indicates that we redisplay a buffer in different windows. See
10221 there. */
10222 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10223 || cursor_type_changed);
10225 /* If specs for an arrow have changed, do thorough redisplay
10226 to ensure we remove any arrow that should no longer exist. */
10227 if (overlay_arrows_changed_p ())
10228 consider_all_windows_p = windows_or_buffers_changed = 1;
10230 /* Normally the message* functions will have already displayed and
10231 updated the echo area, but the frame may have been trashed, or
10232 the update may have been preempted, so display the echo area
10233 again here. Checking message_cleared_p captures the case that
10234 the echo area should be cleared. */
10235 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10236 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10237 || (message_cleared_p
10238 && minibuf_level == 0
10239 /* If the mini-window is currently selected, this means the
10240 echo-area doesn't show through. */
10241 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10243 int window_height_changed_p = echo_area_display (0);
10244 must_finish = 1;
10246 /* If we don't display the current message, don't clear the
10247 message_cleared_p flag, because, if we did, we wouldn't clear
10248 the echo area in the next redisplay which doesn't preserve
10249 the echo area. */
10250 if (!display_last_displayed_message_p)
10251 message_cleared_p = 0;
10253 if (fonts_changed_p)
10254 goto retry;
10255 else if (window_height_changed_p)
10257 consider_all_windows_p = 1;
10258 ++update_mode_lines;
10259 ++windows_or_buffers_changed;
10261 /* If window configuration was changed, frames may have been
10262 marked garbaged. Clear them or we will experience
10263 surprises wrt scrolling. */
10264 if (frame_garbaged)
10265 clear_garbaged_frames ();
10268 else if (EQ (selected_window, minibuf_window)
10269 && (current_buffer->clip_changed
10270 || XFASTINT (w->last_modified) < MODIFF
10271 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10272 && resize_mini_window (w, 0))
10274 /* Resized active mini-window to fit the size of what it is
10275 showing if its contents might have changed. */
10276 must_finish = 1;
10277 consider_all_windows_p = 1;
10278 ++windows_or_buffers_changed;
10279 ++update_mode_lines;
10281 /* If window configuration was changed, frames may have been
10282 marked garbaged. Clear them or we will experience
10283 surprises wrt scrolling. */
10284 if (frame_garbaged)
10285 clear_garbaged_frames ();
10289 /* If showing the region, and mark has changed, we must redisplay
10290 the whole window. The assignment to this_line_start_pos prevents
10291 the optimization directly below this if-statement. */
10292 if (((!NILP (Vtransient_mark_mode)
10293 && !NILP (XBUFFER (w->buffer)->mark_active))
10294 != !NILP (w->region_showing))
10295 || (!NILP (w->region_showing)
10296 && !EQ (w->region_showing,
10297 Fmarker_position (XBUFFER (w->buffer)->mark))))
10298 CHARPOS (this_line_start_pos) = 0;
10300 /* Optimize the case that only the line containing the cursor in the
10301 selected window has changed. Variables starting with this_ are
10302 set in display_line and record information about the line
10303 containing the cursor. */
10304 tlbufpos = this_line_start_pos;
10305 tlendpos = this_line_end_pos;
10306 if (!consider_all_windows_p
10307 && CHARPOS (tlbufpos) > 0
10308 && NILP (w->update_mode_line)
10309 && !current_buffer->clip_changed
10310 && !current_buffer->prevent_redisplay_optimizations_p
10311 && FRAME_VISIBLE_P (XFRAME (w->frame))
10312 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10313 /* Make sure recorded data applies to current buffer, etc. */
10314 && this_line_buffer == current_buffer
10315 && current_buffer == XBUFFER (w->buffer)
10316 && NILP (w->force_start)
10317 && NILP (w->optional_new_start)
10318 /* Point must be on the line that we have info recorded about. */
10319 && PT >= CHARPOS (tlbufpos)
10320 && PT <= Z - CHARPOS (tlendpos)
10321 /* All text outside that line, including its final newline,
10322 must be unchanged */
10323 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10324 CHARPOS (tlendpos)))
10326 if (CHARPOS (tlbufpos) > BEGV
10327 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10328 && (CHARPOS (tlbufpos) == ZV
10329 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10330 /* Former continuation line has disappeared by becoming empty */
10331 goto cancel;
10332 else if (XFASTINT (w->last_modified) < MODIFF
10333 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10334 || MINI_WINDOW_P (w))
10336 /* We have to handle the case of continuation around a
10337 wide-column character (See the comment in indent.c around
10338 line 885).
10340 For instance, in the following case:
10342 -------- Insert --------
10343 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10344 J_I_ ==> J_I_ `^^' are cursors.
10345 ^^ ^^
10346 -------- --------
10348 As we have to redraw the line above, we should goto cancel. */
10350 struct it it;
10351 int line_height_before = this_line_pixel_height;
10353 /* Note that start_display will handle the case that the
10354 line starting at tlbufpos is a continuation lines. */
10355 start_display (&it, w, tlbufpos);
10357 /* Implementation note: It this still necessary? */
10358 if (it.current_x != this_line_start_x)
10359 goto cancel;
10361 TRACE ((stderr, "trying display optimization 1\n"));
10362 w->cursor.vpos = -1;
10363 overlay_arrow_seen = 0;
10364 it.vpos = this_line_vpos;
10365 it.current_y = this_line_y;
10366 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10367 display_line (&it);
10369 /* If line contains point, is not continued,
10370 and ends at same distance from eob as before, we win */
10371 if (w->cursor.vpos >= 0
10372 /* Line is not continued, otherwise this_line_start_pos
10373 would have been set to 0 in display_line. */
10374 && CHARPOS (this_line_start_pos)
10375 /* Line ends as before. */
10376 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10377 /* Line has same height as before. Otherwise other lines
10378 would have to be shifted up or down. */
10379 && this_line_pixel_height == line_height_before)
10381 /* If this is not the window's last line, we must adjust
10382 the charstarts of the lines below. */
10383 if (it.current_y < it.last_visible_y)
10385 struct glyph_row *row
10386 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10387 int delta, delta_bytes;
10389 if (Z - CHARPOS (tlendpos) == ZV)
10391 /* This line ends at end of (accessible part of)
10392 buffer. There is no newline to count. */
10393 delta = (Z
10394 - CHARPOS (tlendpos)
10395 - MATRIX_ROW_START_CHARPOS (row));
10396 delta_bytes = (Z_BYTE
10397 - BYTEPOS (tlendpos)
10398 - MATRIX_ROW_START_BYTEPOS (row));
10400 else
10402 /* This line ends in a newline. Must take
10403 account of the newline and the rest of the
10404 text that follows. */
10405 delta = (Z
10406 - CHARPOS (tlendpos)
10407 - MATRIX_ROW_START_CHARPOS (row));
10408 delta_bytes = (Z_BYTE
10409 - BYTEPOS (tlendpos)
10410 - MATRIX_ROW_START_BYTEPOS (row));
10413 increment_matrix_positions (w->current_matrix,
10414 this_line_vpos + 1,
10415 w->current_matrix->nrows,
10416 delta, delta_bytes);
10419 /* If this row displays text now but previously didn't,
10420 or vice versa, w->window_end_vpos may have to be
10421 adjusted. */
10422 if ((it.glyph_row - 1)->displays_text_p)
10424 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10425 XSETINT (w->window_end_vpos, this_line_vpos);
10427 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10428 && this_line_vpos > 0)
10429 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10430 w->window_end_valid = Qnil;
10432 /* Update hint: No need to try to scroll in update_window. */
10433 w->desired_matrix->no_scrolling_p = 1;
10435 #if GLYPH_DEBUG
10436 *w->desired_matrix->method = 0;
10437 debug_method_add (w, "optimization 1");
10438 #endif
10439 #ifdef HAVE_WINDOW_SYSTEM
10440 update_window_fringes (w, 0);
10441 #endif
10442 goto update;
10444 else
10445 goto cancel;
10447 else if (/* Cursor position hasn't changed. */
10448 PT == XFASTINT (w->last_point)
10449 /* Make sure the cursor was last displayed
10450 in this window. Otherwise we have to reposition it. */
10451 && 0 <= w->cursor.vpos
10452 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10454 if (!must_finish)
10456 do_pending_window_change (1);
10458 /* We used to always goto end_of_redisplay here, but this
10459 isn't enough if we have a blinking cursor. */
10460 if (w->cursor_off_p == w->last_cursor_off_p)
10461 goto end_of_redisplay;
10463 goto update;
10465 /* If highlighting the region, or if the cursor is in the echo area,
10466 then we can't just move the cursor. */
10467 else if (! (!NILP (Vtransient_mark_mode)
10468 && !NILP (current_buffer->mark_active))
10469 && (EQ (selected_window, current_buffer->last_selected_window)
10470 || highlight_nonselected_windows)
10471 && NILP (w->region_showing)
10472 && NILP (Vshow_trailing_whitespace)
10473 && !cursor_in_echo_area)
10475 struct it it;
10476 struct glyph_row *row;
10478 /* Skip from tlbufpos to PT and see where it is. Note that
10479 PT may be in invisible text. If so, we will end at the
10480 next visible position. */
10481 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10482 NULL, DEFAULT_FACE_ID);
10483 it.current_x = this_line_start_x;
10484 it.current_y = this_line_y;
10485 it.vpos = this_line_vpos;
10487 /* The call to move_it_to stops in front of PT, but
10488 moves over before-strings. */
10489 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10491 if (it.vpos == this_line_vpos
10492 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10493 row->enabled_p))
10495 xassert (this_line_vpos == it.vpos);
10496 xassert (this_line_y == it.current_y);
10497 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10498 #if GLYPH_DEBUG
10499 *w->desired_matrix->method = 0;
10500 debug_method_add (w, "optimization 3");
10501 #endif
10502 goto update;
10504 else
10505 goto cancel;
10508 cancel:
10509 /* Text changed drastically or point moved off of line. */
10510 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10513 CHARPOS (this_line_start_pos) = 0;
10514 consider_all_windows_p |= buffer_shared > 1;
10515 ++clear_face_cache_count;
10516 #ifdef HAVE_WINDOW_SYSTEM
10517 ++clear_image_cache_count;
10518 #endif
10520 /* Build desired matrices, and update the display. If
10521 consider_all_windows_p is non-zero, do it for all windows on all
10522 frames. Otherwise do it for selected_window, only. */
10524 if (consider_all_windows_p)
10526 Lisp_Object tail, frame;
10527 int i, n = 0, size = 50;
10528 struct frame **updated
10529 = (struct frame **) alloca (size * sizeof *updated);
10531 /* Recompute # windows showing selected buffer. This will be
10532 incremented each time such a window is displayed. */
10533 buffer_shared = 0;
10535 FOR_EACH_FRAME (tail, frame)
10537 struct frame *f = XFRAME (frame);
10539 if (FRAME_WINDOW_P (f) || f == sf)
10541 if (! EQ (frame, selected_frame))
10542 /* Select the frame, for the sake of frame-local
10543 variables. */
10544 select_frame_for_redisplay (frame);
10546 /* Mark all the scroll bars to be removed; we'll redeem
10547 the ones we want when we redisplay their windows. */
10548 if (condemn_scroll_bars_hook)
10549 condemn_scroll_bars_hook (f);
10551 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10552 redisplay_windows (FRAME_ROOT_WINDOW (f));
10554 /* Any scroll bars which redisplay_windows should have
10555 nuked should now go away. */
10556 if (judge_scroll_bars_hook)
10557 judge_scroll_bars_hook (f);
10559 /* If fonts changed, display again. */
10560 /* ??? rms: I suspect it is a mistake to jump all the way
10561 back to retry here. It should just retry this frame. */
10562 if (fonts_changed_p)
10563 goto retry;
10565 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10567 /* See if we have to hscroll. */
10568 if (hscroll_windows (f->root_window))
10569 goto retry;
10571 /* Prevent various kinds of signals during display
10572 update. stdio is not robust about handling
10573 signals, which can cause an apparent I/O
10574 error. */
10575 if (interrupt_input)
10576 unrequest_sigio ();
10577 STOP_POLLING;
10579 /* Update the display. */
10580 set_window_update_flags (XWINDOW (f->root_window), 1);
10581 pause |= update_frame (f, 0, 0);
10582 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10583 if (pause)
10584 break;
10585 #endif
10587 if (n == size)
10589 int nbytes = size * sizeof *updated;
10590 struct frame **p = (struct frame **) alloca (2 * nbytes);
10591 bcopy (updated, p, nbytes);
10592 size *= 2;
10595 updated[n++] = f;
10600 if (!pause)
10602 /* Do the mark_window_display_accurate after all windows have
10603 been redisplayed because this call resets flags in buffers
10604 which are needed for proper redisplay. */
10605 for (i = 0; i < n; ++i)
10607 struct frame *f = updated[i];
10608 mark_window_display_accurate (f->root_window, 1);
10609 if (frame_up_to_date_hook)
10610 frame_up_to_date_hook (f);
10614 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10616 Lisp_Object mini_window;
10617 struct frame *mini_frame;
10619 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10620 /* Use list_of_error, not Qerror, so that
10621 we catch only errors and don't run the debugger. */
10622 internal_condition_case_1 (redisplay_window_1, selected_window,
10623 list_of_error,
10624 redisplay_window_error);
10626 /* Compare desired and current matrices, perform output. */
10628 update:
10629 /* If fonts changed, display again. */
10630 if (fonts_changed_p)
10631 goto retry;
10633 /* Prevent various kinds of signals during display update.
10634 stdio is not robust about handling signals,
10635 which can cause an apparent I/O error. */
10636 if (interrupt_input)
10637 unrequest_sigio ();
10638 STOP_POLLING;
10640 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10642 if (hscroll_windows (selected_window))
10643 goto retry;
10645 XWINDOW (selected_window)->must_be_updated_p = 1;
10646 pause = update_frame (sf, 0, 0);
10649 /* We may have called echo_area_display at the top of this
10650 function. If the echo area is on another frame, that may
10651 have put text on a frame other than the selected one, so the
10652 above call to update_frame would not have caught it. Catch
10653 it here. */
10654 mini_window = FRAME_MINIBUF_WINDOW (sf);
10655 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10657 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10659 XWINDOW (mini_window)->must_be_updated_p = 1;
10660 pause |= update_frame (mini_frame, 0, 0);
10661 if (!pause && hscroll_windows (mini_window))
10662 goto retry;
10666 /* If display was paused because of pending input, make sure we do a
10667 thorough update the next time. */
10668 if (pause)
10670 /* Prevent the optimization at the beginning of
10671 redisplay_internal that tries a single-line update of the
10672 line containing the cursor in the selected window. */
10673 CHARPOS (this_line_start_pos) = 0;
10675 /* Let the overlay arrow be updated the next time. */
10676 update_overlay_arrows (0);
10678 /* If we pause after scrolling, some rows in the current
10679 matrices of some windows are not valid. */
10680 if (!WINDOW_FULL_WIDTH_P (w)
10681 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10682 update_mode_lines = 1;
10684 else
10686 if (!consider_all_windows_p)
10688 /* This has already been done above if
10689 consider_all_windows_p is set. */
10690 mark_window_display_accurate_1 (w, 1);
10692 /* Say overlay arrows are up to date. */
10693 update_overlay_arrows (1);
10695 if (frame_up_to_date_hook != 0)
10696 frame_up_to_date_hook (sf);
10699 update_mode_lines = 0;
10700 windows_or_buffers_changed = 0;
10701 cursor_type_changed = 0;
10704 /* Start SIGIO interrupts coming again. Having them off during the
10705 code above makes it less likely one will discard output, but not
10706 impossible, since there might be stuff in the system buffer here.
10707 But it is much hairier to try to do anything about that. */
10708 if (interrupt_input)
10709 request_sigio ();
10710 RESUME_POLLING;
10712 /* If a frame has become visible which was not before, redisplay
10713 again, so that we display it. Expose events for such a frame
10714 (which it gets when becoming visible) don't call the parts of
10715 redisplay constructing glyphs, so simply exposing a frame won't
10716 display anything in this case. So, we have to display these
10717 frames here explicitly. */
10718 if (!pause)
10720 Lisp_Object tail, frame;
10721 int new_count = 0;
10723 FOR_EACH_FRAME (tail, frame)
10725 int this_is_visible = 0;
10727 if (XFRAME (frame)->visible)
10728 this_is_visible = 1;
10729 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10730 if (XFRAME (frame)->visible)
10731 this_is_visible = 1;
10733 if (this_is_visible)
10734 new_count++;
10737 if (new_count != number_of_visible_frames)
10738 windows_or_buffers_changed++;
10741 /* Change frame size now if a change is pending. */
10742 do_pending_window_change (1);
10744 /* If we just did a pending size change, or have additional
10745 visible frames, redisplay again. */
10746 if (windows_or_buffers_changed && !pause)
10747 goto retry;
10749 /* Clear the face cache eventually. */
10750 if (consider_all_windows_p)
10752 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10754 clear_face_cache (0);
10755 clear_face_cache_count = 0;
10757 #ifdef HAVE_WINDOW_SYSTEM
10758 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10760 Lisp_Object tail, frame;
10761 FOR_EACH_FRAME (tail, frame)
10763 struct frame *f = XFRAME (frame);
10764 if (FRAME_WINDOW_P (f))
10765 clear_image_cache (f, 0);
10767 clear_image_cache_count = 0;
10769 #endif /* HAVE_WINDOW_SYSTEM */
10772 end_of_redisplay:
10773 unbind_to (count, Qnil);
10774 RESUME_POLLING;
10778 /* Redisplay, but leave alone any recent echo area message unless
10779 another message has been requested in its place.
10781 This is useful in situations where you need to redisplay but no
10782 user action has occurred, making it inappropriate for the message
10783 area to be cleared. See tracking_off and
10784 wait_reading_process_output for examples of these situations.
10786 FROM_WHERE is an integer saying from where this function was
10787 called. This is useful for debugging. */
10789 void
10790 redisplay_preserve_echo_area (from_where)
10791 int from_where;
10793 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10795 if (!NILP (echo_area_buffer[1]))
10797 /* We have a previously displayed message, but no current
10798 message. Redisplay the previous message. */
10799 display_last_displayed_message_p = 1;
10800 redisplay_internal (1);
10801 display_last_displayed_message_p = 0;
10803 else
10804 redisplay_internal (1);
10806 if (rif != NULL && rif->flush_display_optional)
10807 rif->flush_display_optional (NULL);
10811 /* Function registered with record_unwind_protect in
10812 redisplay_internal. Reset redisplaying_p to the value it had
10813 before redisplay_internal was called, and clear
10814 prevent_freeing_realized_faces_p. It also selects the previously
10815 selected frame. */
10817 static Lisp_Object
10818 unwind_redisplay (val)
10819 Lisp_Object val;
10821 Lisp_Object old_redisplaying_p, old_frame;
10823 old_redisplaying_p = XCAR (val);
10824 redisplaying_p = XFASTINT (old_redisplaying_p);
10825 old_frame = XCDR (val);
10826 if (! EQ (old_frame, selected_frame))
10827 select_frame_for_redisplay (old_frame);
10828 return Qnil;
10832 /* Mark the display of window W as accurate or inaccurate. If
10833 ACCURATE_P is non-zero mark display of W as accurate. If
10834 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10835 redisplay_internal is called. */
10837 static void
10838 mark_window_display_accurate_1 (w, accurate_p)
10839 struct window *w;
10840 int accurate_p;
10842 if (BUFFERP (w->buffer))
10844 struct buffer *b = XBUFFER (w->buffer);
10846 w->last_modified
10847 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10848 w->last_overlay_modified
10849 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10850 w->last_had_star
10851 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10853 if (accurate_p)
10855 b->clip_changed = 0;
10856 b->prevent_redisplay_optimizations_p = 0;
10858 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10859 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10860 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10861 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10863 w->current_matrix->buffer = b;
10864 w->current_matrix->begv = BUF_BEGV (b);
10865 w->current_matrix->zv = BUF_ZV (b);
10867 w->last_cursor = w->cursor;
10868 w->last_cursor_off_p = w->cursor_off_p;
10870 if (w == XWINDOW (selected_window))
10871 w->last_point = make_number (BUF_PT (b));
10872 else
10873 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10877 if (accurate_p)
10879 w->window_end_valid = w->buffer;
10880 #if 0 /* This is incorrect with variable-height lines. */
10881 xassert (XINT (w->window_end_vpos)
10882 < (WINDOW_TOTAL_LINES (w)
10883 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10884 #endif
10885 w->update_mode_line = Qnil;
10890 /* Mark the display of windows in the window tree rooted at WINDOW as
10891 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10892 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10893 be redisplayed the next time redisplay_internal is called. */
10895 void
10896 mark_window_display_accurate (window, accurate_p)
10897 Lisp_Object window;
10898 int accurate_p;
10900 struct window *w;
10902 for (; !NILP (window); window = w->next)
10904 w = XWINDOW (window);
10905 mark_window_display_accurate_1 (w, accurate_p);
10907 if (!NILP (w->vchild))
10908 mark_window_display_accurate (w->vchild, accurate_p);
10909 if (!NILP (w->hchild))
10910 mark_window_display_accurate (w->hchild, accurate_p);
10913 if (accurate_p)
10915 update_overlay_arrows (1);
10917 else
10919 /* Force a thorough redisplay the next time by setting
10920 last_arrow_position and last_arrow_string to t, which is
10921 unequal to any useful value of Voverlay_arrow_... */
10922 update_overlay_arrows (-1);
10927 /* Return value in display table DP (Lisp_Char_Table *) for character
10928 C. Since a display table doesn't have any parent, we don't have to
10929 follow parent. Do not call this function directly but use the
10930 macro DISP_CHAR_VECTOR. */
10932 Lisp_Object
10933 disp_char_vector (dp, c)
10934 struct Lisp_Char_Table *dp;
10935 int c;
10937 int code[4], i;
10938 Lisp_Object val;
10940 if (SINGLE_BYTE_CHAR_P (c))
10941 return (dp->contents[c]);
10943 SPLIT_CHAR (c, code[0], code[1], code[2]);
10944 if (code[1] < 32)
10945 code[1] = -1;
10946 else if (code[2] < 32)
10947 code[2] = -1;
10949 /* Here, the possible range of code[0] (== charset ID) is
10950 128..max_charset. Since the top level char table contains data
10951 for multibyte characters after 256th element, we must increment
10952 code[0] by 128 to get a correct index. */
10953 code[0] += 128;
10954 code[3] = -1; /* anchor */
10956 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10958 val = dp->contents[code[i]];
10959 if (!SUB_CHAR_TABLE_P (val))
10960 return (NILP (val) ? dp->defalt : val);
10963 /* Here, val is a sub char table. We return the default value of
10964 it. */
10965 return (dp->defalt);
10970 /***********************************************************************
10971 Window Redisplay
10972 ***********************************************************************/
10974 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10976 static void
10977 redisplay_windows (window)
10978 Lisp_Object window;
10980 while (!NILP (window))
10982 struct window *w = XWINDOW (window);
10984 if (!NILP (w->hchild))
10985 redisplay_windows (w->hchild);
10986 else if (!NILP (w->vchild))
10987 redisplay_windows (w->vchild);
10988 else
10990 displayed_buffer = XBUFFER (w->buffer);
10991 /* Use list_of_error, not Qerror, so that
10992 we catch only errors and don't run the debugger. */
10993 internal_condition_case_1 (redisplay_window_0, window,
10994 list_of_error,
10995 redisplay_window_error);
10998 window = w->next;
11002 static Lisp_Object
11003 redisplay_window_error ()
11005 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11006 return Qnil;
11009 static Lisp_Object
11010 redisplay_window_0 (window)
11011 Lisp_Object window;
11013 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11014 redisplay_window (window, 0);
11015 return Qnil;
11018 static Lisp_Object
11019 redisplay_window_1 (window)
11020 Lisp_Object window;
11022 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11023 redisplay_window (window, 1);
11024 return Qnil;
11028 /* Increment GLYPH until it reaches END or CONDITION fails while
11029 adding (GLYPH)->pixel_width to X. */
11031 #define SKIP_GLYPHS(glyph, end, x, condition) \
11032 do \
11034 (x) += (glyph)->pixel_width; \
11035 ++(glyph); \
11037 while ((glyph) < (end) && (condition))
11040 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11041 DELTA is the number of bytes by which positions recorded in ROW
11042 differ from current buffer positions. */
11044 void
11045 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11046 struct window *w;
11047 struct glyph_row *row;
11048 struct glyph_matrix *matrix;
11049 int delta, delta_bytes, dy, dvpos;
11051 struct glyph *glyph = row->glyphs[TEXT_AREA];
11052 struct glyph *end = glyph + row->used[TEXT_AREA];
11053 struct glyph *cursor = NULL;
11054 /* The first glyph that starts a sequence of glyphs from string. */
11055 struct glyph *string_start;
11056 /* The X coordinate of string_start. */
11057 int string_start_x;
11058 /* The last known character position. */
11059 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11060 /* The last known character position before string_start. */
11061 int string_before_pos;
11062 int x = row->x;
11063 int cursor_x = x;
11064 int cursor_from_overlay_pos = 0;
11065 int pt_old = PT - delta;
11067 /* Skip over glyphs not having an object at the start of the row.
11068 These are special glyphs like truncation marks on terminal
11069 frames. */
11070 if (row->displays_text_p)
11071 while (glyph < end
11072 && INTEGERP (glyph->object)
11073 && glyph->charpos < 0)
11075 x += glyph->pixel_width;
11076 ++glyph;
11079 string_start = NULL;
11080 while (glyph < end
11081 && !INTEGERP (glyph->object)
11082 && (!BUFFERP (glyph->object)
11083 || (last_pos = glyph->charpos) < pt_old))
11085 if (! STRINGP (glyph->object))
11087 string_start = NULL;
11088 x += glyph->pixel_width;
11089 ++glyph;
11090 if (cursor_from_overlay_pos
11091 && last_pos > cursor_from_overlay_pos)
11093 cursor_from_overlay_pos = 0;
11094 cursor = 0;
11097 else
11099 string_before_pos = last_pos;
11100 string_start = glyph;
11101 string_start_x = x;
11102 /* Skip all glyphs from string. */
11105 int pos;
11106 if ((cursor == NULL || glyph > cursor)
11107 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11108 Qcursor, (glyph)->object))
11109 && (pos = string_buffer_position (w, glyph->object,
11110 string_before_pos),
11111 (pos == 0 /* From overlay */
11112 || pos == pt_old)))
11114 /* Estimate overlay buffer position from the buffer
11115 positions of the glyphs before and after the overlay.
11116 Add 1 to last_pos so that if point corresponds to the
11117 glyph right after the overlay, we still use a 'cursor'
11118 property found in that overlay. */
11119 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11120 cursor = glyph;
11121 cursor_x = x;
11123 x += glyph->pixel_width;
11124 ++glyph;
11126 while (glyph < end && STRINGP (glyph->object));
11130 if (cursor != NULL)
11132 glyph = cursor;
11133 x = cursor_x;
11135 else if (row->ends_in_ellipsis_p && glyph == end)
11137 /* Scan back over the ellipsis glyphs, decrementing positions. */
11138 while (glyph > row->glyphs[TEXT_AREA]
11139 && (glyph - 1)->charpos == last_pos)
11140 glyph--, x -= glyph->pixel_width;
11141 /* That loop always goes one position too far,
11142 including the glyph before the ellipsis.
11143 So scan forward over that one. */
11144 x += glyph->pixel_width;
11145 glyph++;
11147 else if (string_start
11148 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11150 /* We may have skipped over point because the previous glyphs
11151 are from string. As there's no easy way to know the
11152 character position of the current glyph, find the correct
11153 glyph on point by scanning from string_start again. */
11154 Lisp_Object limit;
11155 Lisp_Object string;
11156 int pos;
11158 limit = make_number (pt_old + 1);
11159 end = glyph;
11160 glyph = string_start;
11161 x = string_start_x;
11162 string = glyph->object;
11163 pos = string_buffer_position (w, string, string_before_pos);
11164 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11165 because we always put cursor after overlay strings. */
11166 while (pos == 0 && glyph < end)
11168 string = glyph->object;
11169 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11170 if (glyph < end)
11171 pos = string_buffer_position (w, glyph->object, string_before_pos);
11174 while (glyph < end)
11176 pos = XINT (Fnext_single_char_property_change
11177 (make_number (pos), Qdisplay, Qnil, limit));
11178 if (pos > pt_old)
11179 break;
11180 /* Skip glyphs from the same string. */
11181 string = glyph->object;
11182 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11183 /* Skip glyphs from an overlay. */
11184 while (glyph < end
11185 && ! string_buffer_position (w, glyph->object, pos))
11187 string = glyph->object;
11188 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11193 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11194 w->cursor.x = x;
11195 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11196 w->cursor.y = row->y + dy;
11198 if (w == XWINDOW (selected_window))
11200 if (!row->continued_p
11201 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11202 && row->x == 0)
11204 this_line_buffer = XBUFFER (w->buffer);
11206 CHARPOS (this_line_start_pos)
11207 = MATRIX_ROW_START_CHARPOS (row) + delta;
11208 BYTEPOS (this_line_start_pos)
11209 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11211 CHARPOS (this_line_end_pos)
11212 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11213 BYTEPOS (this_line_end_pos)
11214 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11216 this_line_y = w->cursor.y;
11217 this_line_pixel_height = row->height;
11218 this_line_vpos = w->cursor.vpos;
11219 this_line_start_x = row->x;
11221 else
11222 CHARPOS (this_line_start_pos) = 0;
11227 /* Run window scroll functions, if any, for WINDOW with new window
11228 start STARTP. Sets the window start of WINDOW to that position.
11230 We assume that the window's buffer is really current. */
11232 static INLINE struct text_pos
11233 run_window_scroll_functions (window, startp)
11234 Lisp_Object window;
11235 struct text_pos startp;
11237 struct window *w = XWINDOW (window);
11238 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11240 if (current_buffer != XBUFFER (w->buffer))
11241 abort ();
11243 if (!NILP (Vwindow_scroll_functions))
11245 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11246 make_number (CHARPOS (startp)));
11247 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11248 /* In case the hook functions switch buffers. */
11249 if (current_buffer != XBUFFER (w->buffer))
11250 set_buffer_internal_1 (XBUFFER (w->buffer));
11253 return startp;
11257 /* Make sure the line containing the cursor is fully visible.
11258 A value of 1 means there is nothing to be done.
11259 (Either the line is fully visible, or it cannot be made so,
11260 or we cannot tell.)
11262 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11263 is higher than window.
11265 A value of 0 means the caller should do scrolling
11266 as if point had gone off the screen. */
11268 static int
11269 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11270 struct window *w;
11271 int force_p;
11273 struct glyph_matrix *matrix;
11274 struct glyph_row *row;
11275 int window_height;
11277 if (!make_cursor_line_fully_visible_p)
11278 return 1;
11280 /* It's not always possible to find the cursor, e.g, when a window
11281 is full of overlay strings. Don't do anything in that case. */
11282 if (w->cursor.vpos < 0)
11283 return 1;
11285 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11286 row = MATRIX_ROW (matrix, w->cursor.vpos);
11288 /* If the cursor row is not partially visible, there's nothing to do. */
11289 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11290 return 1;
11292 /* If the row the cursor is in is taller than the window's height,
11293 it's not clear what to do, so do nothing. */
11294 window_height = window_box_height (w);
11295 if (row->height >= window_height)
11297 if (!force_p || w->vscroll)
11298 return 1;
11300 return 0;
11302 #if 0
11303 /* This code used to try to scroll the window just enough to make
11304 the line visible. It returned 0 to say that the caller should
11305 allocate larger glyph matrices. */
11307 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11309 int dy = row->height - row->visible_height;
11310 w->vscroll = 0;
11311 w->cursor.y += dy;
11312 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11314 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11316 int dy = - (row->height - row->visible_height);
11317 w->vscroll = dy;
11318 w->cursor.y += dy;
11319 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11322 /* When we change the cursor y-position of the selected window,
11323 change this_line_y as well so that the display optimization for
11324 the cursor line of the selected window in redisplay_internal uses
11325 the correct y-position. */
11326 if (w == XWINDOW (selected_window))
11327 this_line_y = w->cursor.y;
11329 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11330 redisplay with larger matrices. */
11331 if (matrix->nrows < required_matrix_height (w))
11333 fonts_changed_p = 1;
11334 return 0;
11337 return 1;
11338 #endif /* 0 */
11342 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11343 non-zero means only WINDOW is redisplayed in redisplay_internal.
11344 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11345 in redisplay_window to bring a partially visible line into view in
11346 the case that only the cursor has moved.
11348 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11349 last screen line's vertical height extends past the end of the screen.
11351 Value is
11353 1 if scrolling succeeded
11355 0 if scrolling didn't find point.
11357 -1 if new fonts have been loaded so that we must interrupt
11358 redisplay, adjust glyph matrices, and try again. */
11360 enum
11362 SCROLLING_SUCCESS,
11363 SCROLLING_FAILED,
11364 SCROLLING_NEED_LARGER_MATRICES
11367 static int
11368 try_scrolling (window, just_this_one_p, scroll_conservatively,
11369 scroll_step, temp_scroll_step, last_line_misfit)
11370 Lisp_Object window;
11371 int just_this_one_p;
11372 EMACS_INT scroll_conservatively, scroll_step;
11373 int temp_scroll_step;
11374 int last_line_misfit;
11376 struct window *w = XWINDOW (window);
11377 struct frame *f = XFRAME (w->frame);
11378 struct text_pos scroll_margin_pos;
11379 struct text_pos pos;
11380 struct text_pos startp;
11381 struct it it;
11382 Lisp_Object window_end;
11383 int this_scroll_margin;
11384 int dy = 0;
11385 int scroll_max;
11386 int rc;
11387 int amount_to_scroll = 0;
11388 Lisp_Object aggressive;
11389 int height;
11390 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11392 #if GLYPH_DEBUG
11393 debug_method_add (w, "try_scrolling");
11394 #endif
11396 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11398 /* Compute scroll margin height in pixels. We scroll when point is
11399 within this distance from the top or bottom of the window. */
11400 if (scroll_margin > 0)
11402 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11403 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11405 else
11406 this_scroll_margin = 0;
11408 /* Force scroll_conservatively to have a reasonable value so it doesn't
11409 cause an overflow while computing how much to scroll. */
11410 if (scroll_conservatively)
11411 scroll_conservatively = min (scroll_conservatively,
11412 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11414 /* Compute how much we should try to scroll maximally to bring point
11415 into view. */
11416 if (scroll_step || scroll_conservatively || temp_scroll_step)
11417 scroll_max = max (scroll_step,
11418 max (scroll_conservatively, temp_scroll_step));
11419 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11420 || NUMBERP (current_buffer->scroll_up_aggressively))
11421 /* We're trying to scroll because of aggressive scrolling
11422 but no scroll_step is set. Choose an arbitrary one. Maybe
11423 there should be a variable for this. */
11424 scroll_max = 10;
11425 else
11426 scroll_max = 0;
11427 scroll_max *= FRAME_LINE_HEIGHT (f);
11429 /* Decide whether we have to scroll down. Start at the window end
11430 and move this_scroll_margin up to find the position of the scroll
11431 margin. */
11432 window_end = Fwindow_end (window, Qt);
11434 too_near_end:
11436 CHARPOS (scroll_margin_pos) = XINT (window_end);
11437 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11439 if (this_scroll_margin || extra_scroll_margin_lines)
11441 start_display (&it, w, scroll_margin_pos);
11442 if (this_scroll_margin)
11443 move_it_vertically_backward (&it, this_scroll_margin);
11444 if (extra_scroll_margin_lines)
11445 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11446 scroll_margin_pos = it.current.pos;
11449 if (PT >= CHARPOS (scroll_margin_pos))
11451 int y0;
11453 /* Point is in the scroll margin at the bottom of the window, or
11454 below. Compute a new window start that makes point visible. */
11456 /* Compute the distance from the scroll margin to PT.
11457 Give up if the distance is greater than scroll_max. */
11458 start_display (&it, w, scroll_margin_pos);
11459 y0 = it.current_y;
11460 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11461 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11463 /* To make point visible, we have to move the window start
11464 down so that the line the cursor is in is visible, which
11465 means we have to add in the height of the cursor line. */
11466 dy = line_bottom_y (&it) - y0;
11468 if (dy > scroll_max)
11469 return SCROLLING_FAILED;
11471 /* Move the window start down. If scrolling conservatively,
11472 move it just enough down to make point visible. If
11473 scroll_step is set, move it down by scroll_step. */
11474 start_display (&it, w, startp);
11476 if (scroll_conservatively)
11477 /* Set AMOUNT_TO_SCROLL to at least one line,
11478 and at most scroll_conservatively lines. */
11479 amount_to_scroll
11480 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11481 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11482 else if (scroll_step || temp_scroll_step)
11483 amount_to_scroll = scroll_max;
11484 else
11486 aggressive = current_buffer->scroll_up_aggressively;
11487 height = WINDOW_BOX_TEXT_HEIGHT (w);
11488 if (NUMBERP (aggressive))
11490 double float_amount = XFLOATINT (aggressive) * height;
11491 amount_to_scroll = float_amount;
11492 if (amount_to_scroll == 0 && float_amount > 0)
11493 amount_to_scroll = 1;
11497 if (amount_to_scroll <= 0)
11498 return SCROLLING_FAILED;
11500 /* If moving by amount_to_scroll leaves STARTP unchanged,
11501 move it down one screen line. */
11503 move_it_vertically (&it, amount_to_scroll);
11504 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11505 move_it_by_lines (&it, 1, 1);
11506 startp = it.current.pos;
11508 else
11510 /* See if point is inside the scroll margin at the top of the
11511 window. */
11512 scroll_margin_pos = startp;
11513 if (this_scroll_margin)
11515 start_display (&it, w, startp);
11516 move_it_vertically (&it, this_scroll_margin);
11517 scroll_margin_pos = it.current.pos;
11520 if (PT < CHARPOS (scroll_margin_pos))
11522 /* Point is in the scroll margin at the top of the window or
11523 above what is displayed in the window. */
11524 int y0;
11526 /* Compute the vertical distance from PT to the scroll
11527 margin position. Give up if distance is greater than
11528 scroll_max. */
11529 SET_TEXT_POS (pos, PT, PT_BYTE);
11530 start_display (&it, w, pos);
11531 y0 = it.current_y;
11532 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11533 it.last_visible_y, -1,
11534 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11535 dy = it.current_y - y0;
11536 if (dy > scroll_max)
11537 return SCROLLING_FAILED;
11539 /* Compute new window start. */
11540 start_display (&it, w, startp);
11542 if (scroll_conservatively)
11543 amount_to_scroll
11544 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11545 else if (scroll_step || temp_scroll_step)
11546 amount_to_scroll = scroll_max;
11547 else
11549 aggressive = current_buffer->scroll_down_aggressively;
11550 height = WINDOW_BOX_TEXT_HEIGHT (w);
11551 if (NUMBERP (aggressive))
11553 double float_amount = XFLOATINT (aggressive) * height;
11554 amount_to_scroll = float_amount;
11555 if (amount_to_scroll == 0 && float_amount > 0)
11556 amount_to_scroll = 1;
11560 if (amount_to_scroll <= 0)
11561 return SCROLLING_FAILED;
11563 move_it_vertically_backward (&it, amount_to_scroll);
11564 startp = it.current.pos;
11568 /* Run window scroll functions. */
11569 startp = run_window_scroll_functions (window, startp);
11571 /* Display the window. Give up if new fonts are loaded, or if point
11572 doesn't appear. */
11573 if (!try_window (window, startp))
11574 rc = SCROLLING_NEED_LARGER_MATRICES;
11575 else if (w->cursor.vpos < 0)
11577 clear_glyph_matrix (w->desired_matrix);
11578 rc = SCROLLING_FAILED;
11580 else
11582 /* Maybe forget recorded base line for line number display. */
11583 if (!just_this_one_p
11584 || current_buffer->clip_changed
11585 || BEG_UNCHANGED < CHARPOS (startp))
11586 w->base_line_number = Qnil;
11588 /* If cursor ends up on a partially visible line,
11589 treat that as being off the bottom of the screen. */
11590 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11592 clear_glyph_matrix (w->desired_matrix);
11593 ++extra_scroll_margin_lines;
11594 goto too_near_end;
11596 rc = SCROLLING_SUCCESS;
11599 return rc;
11603 /* Compute a suitable window start for window W if display of W starts
11604 on a continuation line. Value is non-zero if a new window start
11605 was computed.
11607 The new window start will be computed, based on W's width, starting
11608 from the start of the continued line. It is the start of the
11609 screen line with the minimum distance from the old start W->start. */
11611 static int
11612 compute_window_start_on_continuation_line (w)
11613 struct window *w;
11615 struct text_pos pos, start_pos;
11616 int window_start_changed_p = 0;
11618 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11620 /* If window start is on a continuation line... Window start may be
11621 < BEGV in case there's invisible text at the start of the
11622 buffer (M-x rmail, for example). */
11623 if (CHARPOS (start_pos) > BEGV
11624 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11626 struct it it;
11627 struct glyph_row *row;
11629 /* Handle the case that the window start is out of range. */
11630 if (CHARPOS (start_pos) < BEGV)
11631 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11632 else if (CHARPOS (start_pos) > ZV)
11633 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11635 /* Find the start of the continued line. This should be fast
11636 because scan_buffer is fast (newline cache). */
11637 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11638 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11639 row, DEFAULT_FACE_ID);
11640 reseat_at_previous_visible_line_start (&it);
11642 /* If the line start is "too far" away from the window start,
11643 say it takes too much time to compute a new window start. */
11644 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11645 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11647 int min_distance, distance;
11649 /* Move forward by display lines to find the new window
11650 start. If window width was enlarged, the new start can
11651 be expected to be > the old start. If window width was
11652 decreased, the new window start will be < the old start.
11653 So, we're looking for the display line start with the
11654 minimum distance from the old window start. */
11655 pos = it.current.pos;
11656 min_distance = INFINITY;
11657 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11658 distance < min_distance)
11660 min_distance = distance;
11661 pos = it.current.pos;
11662 move_it_by_lines (&it, 1, 0);
11665 /* Set the window start there. */
11666 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11667 window_start_changed_p = 1;
11671 return window_start_changed_p;
11675 /* Try cursor movement in case text has not changed in window WINDOW,
11676 with window start STARTP. Value is
11678 CURSOR_MOVEMENT_SUCCESS if successful
11680 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11682 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11683 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11684 we want to scroll as if scroll-step were set to 1. See the code.
11686 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11687 which case we have to abort this redisplay, and adjust matrices
11688 first. */
11690 enum
11692 CURSOR_MOVEMENT_SUCCESS,
11693 CURSOR_MOVEMENT_CANNOT_BE_USED,
11694 CURSOR_MOVEMENT_MUST_SCROLL,
11695 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11698 static int
11699 try_cursor_movement (window, startp, scroll_step)
11700 Lisp_Object window;
11701 struct text_pos startp;
11702 int *scroll_step;
11704 struct window *w = XWINDOW (window);
11705 struct frame *f = XFRAME (w->frame);
11706 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11708 #if GLYPH_DEBUG
11709 if (inhibit_try_cursor_movement)
11710 return rc;
11711 #endif
11713 /* Handle case where text has not changed, only point, and it has
11714 not moved off the frame. */
11715 if (/* Point may be in this window. */
11716 PT >= CHARPOS (startp)
11717 /* Selective display hasn't changed. */
11718 && !current_buffer->clip_changed
11719 /* Function force-mode-line-update is used to force a thorough
11720 redisplay. It sets either windows_or_buffers_changed or
11721 update_mode_lines. So don't take a shortcut here for these
11722 cases. */
11723 && !update_mode_lines
11724 && !windows_or_buffers_changed
11725 && !cursor_type_changed
11726 /* Can't use this case if highlighting a region. When a
11727 region exists, cursor movement has to do more than just
11728 set the cursor. */
11729 && !(!NILP (Vtransient_mark_mode)
11730 && !NILP (current_buffer->mark_active))
11731 && NILP (w->region_showing)
11732 && NILP (Vshow_trailing_whitespace)
11733 /* Right after splitting windows, last_point may be nil. */
11734 && INTEGERP (w->last_point)
11735 /* This code is not used for mini-buffer for the sake of the case
11736 of redisplaying to replace an echo area message; since in
11737 that case the mini-buffer contents per se are usually
11738 unchanged. This code is of no real use in the mini-buffer
11739 since the handling of this_line_start_pos, etc., in redisplay
11740 handles the same cases. */
11741 && !EQ (window, minibuf_window)
11742 /* When splitting windows or for new windows, it happens that
11743 redisplay is called with a nil window_end_vpos or one being
11744 larger than the window. This should really be fixed in
11745 window.c. I don't have this on my list, now, so we do
11746 approximately the same as the old redisplay code. --gerd. */
11747 && INTEGERP (w->window_end_vpos)
11748 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11749 && (FRAME_WINDOW_P (f)
11750 || !overlay_arrow_in_current_buffer_p ()))
11752 int this_scroll_margin, top_scroll_margin;
11753 struct glyph_row *row = NULL;
11755 #if GLYPH_DEBUG
11756 debug_method_add (w, "cursor movement");
11757 #endif
11759 /* Scroll if point within this distance from the top or bottom
11760 of the window. This is a pixel value. */
11761 this_scroll_margin = max (0, scroll_margin);
11762 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11763 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11765 top_scroll_margin = this_scroll_margin;
11766 if (WINDOW_WANTS_HEADER_LINE_P (w))
11767 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11769 /* Start with the row the cursor was displayed during the last
11770 not paused redisplay. Give up if that row is not valid. */
11771 if (w->last_cursor.vpos < 0
11772 || w->last_cursor.vpos >= w->current_matrix->nrows)
11773 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11774 else
11776 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11777 if (row->mode_line_p)
11778 ++row;
11779 if (!row->enabled_p)
11780 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11783 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11785 int scroll_p = 0;
11786 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11788 if (PT > XFASTINT (w->last_point))
11790 /* Point has moved forward. */
11791 while (MATRIX_ROW_END_CHARPOS (row) < PT
11792 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11794 xassert (row->enabled_p);
11795 ++row;
11798 /* The end position of a row equals the start position
11799 of the next row. If PT is there, we would rather
11800 display it in the next line. */
11801 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11802 && MATRIX_ROW_END_CHARPOS (row) == PT
11803 && !cursor_row_p (w, row))
11804 ++row;
11806 /* If within the scroll margin, scroll. Note that
11807 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11808 the next line would be drawn, and that
11809 this_scroll_margin can be zero. */
11810 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11811 || PT > MATRIX_ROW_END_CHARPOS (row)
11812 /* Line is completely visible last line in window
11813 and PT is to be set in the next line. */
11814 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11815 && PT == MATRIX_ROW_END_CHARPOS (row)
11816 && !row->ends_at_zv_p
11817 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11818 scroll_p = 1;
11820 else if (PT < XFASTINT (w->last_point))
11822 /* Cursor has to be moved backward. Note that PT >=
11823 CHARPOS (startp) because of the outer if-statement. */
11824 while (!row->mode_line_p
11825 && (MATRIX_ROW_START_CHARPOS (row) > PT
11826 || (MATRIX_ROW_START_CHARPOS (row) == PT
11827 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11828 && (row->y > top_scroll_margin
11829 || CHARPOS (startp) == BEGV))
11831 xassert (row->enabled_p);
11832 --row;
11835 /* Consider the following case: Window starts at BEGV,
11836 there is invisible, intangible text at BEGV, so that
11837 display starts at some point START > BEGV. It can
11838 happen that we are called with PT somewhere between
11839 BEGV and START. Try to handle that case. */
11840 if (row < w->current_matrix->rows
11841 || row->mode_line_p)
11843 row = w->current_matrix->rows;
11844 if (row->mode_line_p)
11845 ++row;
11848 /* Due to newlines in overlay strings, we may have to
11849 skip forward over overlay strings. */
11850 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11851 && MATRIX_ROW_END_CHARPOS (row) == PT
11852 && !cursor_row_p (w, row))
11853 ++row;
11855 /* If within the scroll margin, scroll. */
11856 if (row->y < top_scroll_margin
11857 && CHARPOS (startp) != BEGV)
11858 scroll_p = 1;
11860 else
11862 /* Cursor did not move. So don't scroll even if cursor line
11863 is partially visible, as it was so before. */
11864 rc = CURSOR_MOVEMENT_SUCCESS;
11867 if (PT < MATRIX_ROW_START_CHARPOS (row)
11868 || PT > MATRIX_ROW_END_CHARPOS (row))
11870 /* if PT is not in the glyph row, give up. */
11871 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11873 else if (rc != CURSOR_MOVEMENT_SUCCESS
11874 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11875 && make_cursor_line_fully_visible_p)
11877 if (PT == MATRIX_ROW_END_CHARPOS (row)
11878 && !row->ends_at_zv_p
11879 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11880 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11881 else if (row->height > window_box_height (w))
11883 /* If we end up in a partially visible line, let's
11884 make it fully visible, except when it's taller
11885 than the window, in which case we can't do much
11886 about it. */
11887 *scroll_step = 1;
11888 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11890 else
11892 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11893 if (!cursor_row_fully_visible_p (w, 0, 1))
11894 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11895 else
11896 rc = CURSOR_MOVEMENT_SUCCESS;
11899 else if (scroll_p)
11900 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11901 else
11903 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11904 rc = CURSOR_MOVEMENT_SUCCESS;
11909 return rc;
11912 void
11913 set_vertical_scroll_bar (w)
11914 struct window *w;
11916 int start, end, whole;
11918 /* Calculate the start and end positions for the current window.
11919 At some point, it would be nice to choose between scrollbars
11920 which reflect the whole buffer size, with special markers
11921 indicating narrowing, and scrollbars which reflect only the
11922 visible region.
11924 Note that mini-buffers sometimes aren't displaying any text. */
11925 if (!MINI_WINDOW_P (w)
11926 || (w == XWINDOW (minibuf_window)
11927 && NILP (echo_area_buffer[0])))
11929 struct buffer *buf = XBUFFER (w->buffer);
11930 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11931 start = marker_position (w->start) - BUF_BEGV (buf);
11932 /* I don't think this is guaranteed to be right. For the
11933 moment, we'll pretend it is. */
11934 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11936 if (end < start)
11937 end = start;
11938 if (whole < (end - start))
11939 whole = end - start;
11941 else
11942 start = end = whole = 0;
11944 /* Indicate what this scroll bar ought to be displaying now. */
11945 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11949 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11950 selected_window is redisplayed.
11952 We can return without actually redisplaying the window if
11953 fonts_changed_p is nonzero. In that case, redisplay_internal will
11954 retry. */
11956 static void
11957 redisplay_window (window, just_this_one_p)
11958 Lisp_Object window;
11959 int just_this_one_p;
11961 struct window *w = XWINDOW (window);
11962 struct frame *f = XFRAME (w->frame);
11963 struct buffer *buffer = XBUFFER (w->buffer);
11964 struct buffer *old = current_buffer;
11965 struct text_pos lpoint, opoint, startp;
11966 int update_mode_line;
11967 int tem;
11968 struct it it;
11969 /* Record it now because it's overwritten. */
11970 int current_matrix_up_to_date_p = 0;
11971 int used_current_matrix_p = 0;
11972 /* This is less strict than current_matrix_up_to_date_p.
11973 It indictes that the buffer contents and narrowing are unchanged. */
11974 int buffer_unchanged_p = 0;
11975 int temp_scroll_step = 0;
11976 int count = SPECPDL_INDEX ();
11977 int rc;
11978 int centering_position = -1;
11979 int last_line_misfit = 0;
11981 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11982 opoint = lpoint;
11984 /* W must be a leaf window here. */
11985 xassert (!NILP (w->buffer));
11986 #if GLYPH_DEBUG
11987 *w->desired_matrix->method = 0;
11988 #endif
11990 specbind (Qinhibit_point_motion_hooks, Qt);
11992 reconsider_clip_changes (w, buffer);
11994 /* Has the mode line to be updated? */
11995 update_mode_line = (!NILP (w->update_mode_line)
11996 || update_mode_lines
11997 || buffer->clip_changed
11998 || buffer->prevent_redisplay_optimizations_p);
12000 if (MINI_WINDOW_P (w))
12002 if (w == XWINDOW (echo_area_window)
12003 && !NILP (echo_area_buffer[0]))
12005 if (update_mode_line)
12006 /* We may have to update a tty frame's menu bar or a
12007 tool-bar. Example `M-x C-h C-h C-g'. */
12008 goto finish_menu_bars;
12009 else
12010 /* We've already displayed the echo area glyphs in this window. */
12011 goto finish_scroll_bars;
12013 else if ((w != XWINDOW (minibuf_window)
12014 || minibuf_level == 0)
12015 /* When buffer is nonempty, redisplay window normally. */
12016 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12017 /* Quail displays non-mini buffers in minibuffer window.
12018 In that case, redisplay the window normally. */
12019 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12021 /* W is a mini-buffer window, but it's not active, so clear
12022 it. */
12023 int yb = window_text_bottom_y (w);
12024 struct glyph_row *row;
12025 int y;
12027 for (y = 0, row = w->desired_matrix->rows;
12028 y < yb;
12029 y += row->height, ++row)
12030 blank_row (w, row, y);
12031 goto finish_scroll_bars;
12034 clear_glyph_matrix (w->desired_matrix);
12037 /* Otherwise set up data on this window; select its buffer and point
12038 value. */
12039 /* Really select the buffer, for the sake of buffer-local
12040 variables. */
12041 set_buffer_internal_1 (XBUFFER (w->buffer));
12042 SET_TEXT_POS (opoint, PT, PT_BYTE);
12044 current_matrix_up_to_date_p
12045 = (!NILP (w->window_end_valid)
12046 && !current_buffer->clip_changed
12047 && !current_buffer->prevent_redisplay_optimizations_p
12048 && XFASTINT (w->last_modified) >= MODIFF
12049 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12051 buffer_unchanged_p
12052 = (!NILP (w->window_end_valid)
12053 && !current_buffer->clip_changed
12054 && XFASTINT (w->last_modified) >= MODIFF
12055 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12057 /* When windows_or_buffers_changed is non-zero, we can't rely on
12058 the window end being valid, so set it to nil there. */
12059 if (windows_or_buffers_changed)
12061 /* If window starts on a continuation line, maybe adjust the
12062 window start in case the window's width changed. */
12063 if (XMARKER (w->start)->buffer == current_buffer)
12064 compute_window_start_on_continuation_line (w);
12066 w->window_end_valid = Qnil;
12069 /* Some sanity checks. */
12070 CHECK_WINDOW_END (w);
12071 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12072 abort ();
12073 if (BYTEPOS (opoint) < CHARPOS (opoint))
12074 abort ();
12076 /* If %c is in mode line, update it if needed. */
12077 if (!NILP (w->column_number_displayed)
12078 /* This alternative quickly identifies a common case
12079 where no change is needed. */
12080 && !(PT == XFASTINT (w->last_point)
12081 && XFASTINT (w->last_modified) >= MODIFF
12082 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12083 && (XFASTINT (w->column_number_displayed)
12084 != (int) current_column ())) /* iftc */
12085 update_mode_line = 1;
12087 /* Count number of windows showing the selected buffer. An indirect
12088 buffer counts as its base buffer. */
12089 if (!just_this_one_p)
12091 struct buffer *current_base, *window_base;
12092 current_base = current_buffer;
12093 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12094 if (current_base->base_buffer)
12095 current_base = current_base->base_buffer;
12096 if (window_base->base_buffer)
12097 window_base = window_base->base_buffer;
12098 if (current_base == window_base)
12099 buffer_shared++;
12102 /* Point refers normally to the selected window. For any other
12103 window, set up appropriate value. */
12104 if (!EQ (window, selected_window))
12106 int new_pt = XMARKER (w->pointm)->charpos;
12107 int new_pt_byte = marker_byte_position (w->pointm);
12108 if (new_pt < BEGV)
12110 new_pt = BEGV;
12111 new_pt_byte = BEGV_BYTE;
12112 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12114 else if (new_pt > (ZV - 1))
12116 new_pt = ZV;
12117 new_pt_byte = ZV_BYTE;
12118 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12121 /* We don't use SET_PT so that the point-motion hooks don't run. */
12122 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12125 /* If any of the character widths specified in the display table
12126 have changed, invalidate the width run cache. It's true that
12127 this may be a bit late to catch such changes, but the rest of
12128 redisplay goes (non-fatally) haywire when the display table is
12129 changed, so why should we worry about doing any better? */
12130 if (current_buffer->width_run_cache)
12132 struct Lisp_Char_Table *disptab = buffer_display_table ();
12134 if (! disptab_matches_widthtab (disptab,
12135 XVECTOR (current_buffer->width_table)))
12137 invalidate_region_cache (current_buffer,
12138 current_buffer->width_run_cache,
12139 BEG, Z);
12140 recompute_width_table (current_buffer, disptab);
12144 /* If window-start is screwed up, choose a new one. */
12145 if (XMARKER (w->start)->buffer != current_buffer)
12146 goto recenter;
12148 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12150 /* If someone specified a new starting point but did not insist,
12151 check whether it can be used. */
12152 if (!NILP (w->optional_new_start)
12153 && CHARPOS (startp) >= BEGV
12154 && CHARPOS (startp) <= ZV)
12156 w->optional_new_start = Qnil;
12157 start_display (&it, w, startp);
12158 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12159 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12160 if (IT_CHARPOS (it) == PT)
12161 w->force_start = Qt;
12162 /* IT may overshoot PT if text at PT is invisible. */
12163 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12164 w->force_start = Qt;
12169 /* Handle case where place to start displaying has been specified,
12170 unless the specified location is outside the accessible range. */
12171 if (!NILP (w->force_start)
12172 || w->frozen_window_start_p)
12174 /* We set this later on if we have to adjust point. */
12175 int new_vpos = -1;
12177 w->force_start = Qnil;
12178 w->vscroll = 0;
12179 w->window_end_valid = Qnil;
12181 /* Forget any recorded base line for line number display. */
12182 if (!buffer_unchanged_p)
12183 w->base_line_number = Qnil;
12185 /* Redisplay the mode line. Select the buffer properly for that.
12186 Also, run the hook window-scroll-functions
12187 because we have scrolled. */
12188 /* Note, we do this after clearing force_start because
12189 if there's an error, it is better to forget about force_start
12190 than to get into an infinite loop calling the hook functions
12191 and having them get more errors. */
12192 if (!update_mode_line
12193 || ! NILP (Vwindow_scroll_functions))
12195 update_mode_line = 1;
12196 w->update_mode_line = Qt;
12197 startp = run_window_scroll_functions (window, startp);
12200 w->last_modified = make_number (0);
12201 w->last_overlay_modified = make_number (0);
12202 if (CHARPOS (startp) < BEGV)
12203 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12204 else if (CHARPOS (startp) > ZV)
12205 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12207 /* Redisplay, then check if cursor has been set during the
12208 redisplay. Give up if new fonts were loaded. */
12209 if (!try_window (window, startp))
12211 w->force_start = Qt;
12212 clear_glyph_matrix (w->desired_matrix);
12213 goto need_larger_matrices;
12216 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12218 /* If point does not appear, try to move point so it does
12219 appear. The desired matrix has been built above, so we
12220 can use it here. */
12221 new_vpos = window_box_height (w) / 2;
12224 if (!cursor_row_fully_visible_p (w, 0, 0))
12226 /* Point does appear, but on a line partly visible at end of window.
12227 Move it back to a fully-visible line. */
12228 new_vpos = window_box_height (w);
12231 /* If we need to move point for either of the above reasons,
12232 now actually do it. */
12233 if (new_vpos >= 0)
12235 struct glyph_row *row;
12237 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12238 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12239 ++row;
12241 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12242 MATRIX_ROW_START_BYTEPOS (row));
12244 if (w != XWINDOW (selected_window))
12245 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12246 else if (current_buffer == old)
12247 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12249 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12251 /* If we are highlighting the region, then we just changed
12252 the region, so redisplay to show it. */
12253 if (!NILP (Vtransient_mark_mode)
12254 && !NILP (current_buffer->mark_active))
12256 clear_glyph_matrix (w->desired_matrix);
12257 if (!try_window (window, startp))
12258 goto need_larger_matrices;
12262 #if GLYPH_DEBUG
12263 debug_method_add (w, "forced window start");
12264 #endif
12265 goto done;
12268 /* Handle case where text has not changed, only point, and it has
12269 not moved off the frame, and we are not retrying after hscroll.
12270 (current_matrix_up_to_date_p is nonzero when retrying.) */
12271 if (current_matrix_up_to_date_p
12272 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12273 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12275 switch (rc)
12277 case CURSOR_MOVEMENT_SUCCESS:
12278 used_current_matrix_p = 1;
12279 goto done;
12281 #if 0 /* try_cursor_movement never returns this value. */
12282 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12283 goto need_larger_matrices;
12284 #endif
12286 case CURSOR_MOVEMENT_MUST_SCROLL:
12287 goto try_to_scroll;
12289 default:
12290 abort ();
12293 /* If current starting point was originally the beginning of a line
12294 but no longer is, find a new starting point. */
12295 else if (!NILP (w->start_at_line_beg)
12296 && !(CHARPOS (startp) <= BEGV
12297 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12299 #if GLYPH_DEBUG
12300 debug_method_add (w, "recenter 1");
12301 #endif
12302 goto recenter;
12305 /* Try scrolling with try_window_id. Value is > 0 if update has
12306 been done, it is -1 if we know that the same window start will
12307 not work. It is 0 if unsuccessful for some other reason. */
12308 else if ((tem = try_window_id (w)) != 0)
12310 #if GLYPH_DEBUG
12311 debug_method_add (w, "try_window_id %d", tem);
12312 #endif
12314 if (fonts_changed_p)
12315 goto need_larger_matrices;
12316 if (tem > 0)
12317 goto done;
12319 /* Otherwise try_window_id has returned -1 which means that we
12320 don't want the alternative below this comment to execute. */
12322 else if (CHARPOS (startp) >= BEGV
12323 && CHARPOS (startp) <= ZV
12324 && PT >= CHARPOS (startp)
12325 && (CHARPOS (startp) < ZV
12326 /* Avoid starting at end of buffer. */
12327 || CHARPOS (startp) == BEGV
12328 || (XFASTINT (w->last_modified) >= MODIFF
12329 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12331 #if GLYPH_DEBUG
12332 debug_method_add (w, "same window start");
12333 #endif
12335 /* Try to redisplay starting at same place as before.
12336 If point has not moved off frame, accept the results. */
12337 if (!current_matrix_up_to_date_p
12338 /* Don't use try_window_reusing_current_matrix in this case
12339 because a window scroll function can have changed the
12340 buffer. */
12341 || !NILP (Vwindow_scroll_functions)
12342 || MINI_WINDOW_P (w)
12343 || !(used_current_matrix_p
12344 = try_window_reusing_current_matrix (w)))
12346 IF_DEBUG (debug_method_add (w, "1"));
12347 try_window (window, startp);
12350 if (fonts_changed_p)
12351 goto need_larger_matrices;
12353 if (w->cursor.vpos >= 0)
12355 if (!just_this_one_p
12356 || current_buffer->clip_changed
12357 || BEG_UNCHANGED < CHARPOS (startp))
12358 /* Forget any recorded base line for line number display. */
12359 w->base_line_number = Qnil;
12361 if (!cursor_row_fully_visible_p (w, 1, 0))
12363 clear_glyph_matrix (w->desired_matrix);
12364 last_line_misfit = 1;
12366 /* Drop through and scroll. */
12367 else
12368 goto done;
12370 else
12371 clear_glyph_matrix (w->desired_matrix);
12374 try_to_scroll:
12376 w->last_modified = make_number (0);
12377 w->last_overlay_modified = make_number (0);
12379 /* Redisplay the mode line. Select the buffer properly for that. */
12380 if (!update_mode_line)
12382 update_mode_line = 1;
12383 w->update_mode_line = Qt;
12386 /* Try to scroll by specified few lines. */
12387 if ((scroll_conservatively
12388 || scroll_step
12389 || temp_scroll_step
12390 || NUMBERP (current_buffer->scroll_up_aggressively)
12391 || NUMBERP (current_buffer->scroll_down_aggressively))
12392 && !current_buffer->clip_changed
12393 && CHARPOS (startp) >= BEGV
12394 && CHARPOS (startp) <= ZV)
12396 /* The function returns -1 if new fonts were loaded, 1 if
12397 successful, 0 if not successful. */
12398 int rc = try_scrolling (window, just_this_one_p,
12399 scroll_conservatively,
12400 scroll_step,
12401 temp_scroll_step, last_line_misfit);
12402 switch (rc)
12404 case SCROLLING_SUCCESS:
12405 goto done;
12407 case SCROLLING_NEED_LARGER_MATRICES:
12408 goto need_larger_matrices;
12410 case SCROLLING_FAILED:
12411 break;
12413 default:
12414 abort ();
12418 /* Finally, just choose place to start which centers point */
12420 recenter:
12421 if (centering_position < 0)
12422 centering_position = window_box_height (w) / 2;
12424 #if GLYPH_DEBUG
12425 debug_method_add (w, "recenter");
12426 #endif
12428 /* w->vscroll = 0; */
12430 /* Forget any previously recorded base line for line number display. */
12431 if (!buffer_unchanged_p)
12432 w->base_line_number = Qnil;
12434 /* Move backward half the height of the window. */
12435 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12436 it.current_y = it.last_visible_y;
12437 move_it_vertically_backward (&it, centering_position);
12438 xassert (IT_CHARPOS (it) >= BEGV);
12440 /* The function move_it_vertically_backward may move over more
12441 than the specified y-distance. If it->w is small, e.g. a
12442 mini-buffer window, we may end up in front of the window's
12443 display area. Start displaying at the start of the line
12444 containing PT in this case. */
12445 if (it.current_y <= 0)
12447 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12448 move_it_vertically_backward (&it, 0);
12449 #if 0
12450 /* I think this assert is bogus if buffer contains
12451 invisible text or images. KFS. */
12452 xassert (IT_CHARPOS (it) <= PT);
12453 #endif
12454 it.current_y = 0;
12457 it.current_x = it.hpos = 0;
12459 /* Set startp here explicitly in case that helps avoid an infinite loop
12460 in case the window-scroll-functions functions get errors. */
12461 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12463 /* Run scroll hooks. */
12464 startp = run_window_scroll_functions (window, it.current.pos);
12466 /* Redisplay the window. */
12467 if (!current_matrix_up_to_date_p
12468 || windows_or_buffers_changed
12469 || cursor_type_changed
12470 /* Don't use try_window_reusing_current_matrix in this case
12471 because it can have changed the buffer. */
12472 || !NILP (Vwindow_scroll_functions)
12473 || !just_this_one_p
12474 || MINI_WINDOW_P (w)
12475 || !(used_current_matrix_p
12476 = try_window_reusing_current_matrix (w)))
12477 try_window (window, startp);
12479 /* If new fonts have been loaded (due to fontsets), give up. We
12480 have to start a new redisplay since we need to re-adjust glyph
12481 matrices. */
12482 if (fonts_changed_p)
12483 goto need_larger_matrices;
12485 /* If cursor did not appear assume that the middle of the window is
12486 in the first line of the window. Do it again with the next line.
12487 (Imagine a window of height 100, displaying two lines of height
12488 60. Moving back 50 from it->last_visible_y will end in the first
12489 line.) */
12490 if (w->cursor.vpos < 0)
12492 if (!NILP (w->window_end_valid)
12493 && PT >= Z - XFASTINT (w->window_end_pos))
12495 clear_glyph_matrix (w->desired_matrix);
12496 move_it_by_lines (&it, 1, 0);
12497 try_window (window, it.current.pos);
12499 else if (PT < IT_CHARPOS (it))
12501 clear_glyph_matrix (w->desired_matrix);
12502 move_it_by_lines (&it, -1, 0);
12503 try_window (window, it.current.pos);
12505 else
12507 /* Not much we can do about it. */
12511 /* Consider the following case: Window starts at BEGV, there is
12512 invisible, intangible text at BEGV, so that display starts at
12513 some point START > BEGV. It can happen that we are called with
12514 PT somewhere between BEGV and START. Try to handle that case. */
12515 if (w->cursor.vpos < 0)
12517 struct glyph_row *row = w->current_matrix->rows;
12518 if (row->mode_line_p)
12519 ++row;
12520 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12523 if (!cursor_row_fully_visible_p (w, 0, 0))
12525 /* If vscroll is enabled, disable it and try again. */
12526 if (w->vscroll)
12528 w->vscroll = 0;
12529 clear_glyph_matrix (w->desired_matrix);
12530 goto recenter;
12533 /* If centering point failed to make the whole line visible,
12534 put point at the top instead. That has to make the whole line
12535 visible, if it can be done. */
12536 if (centering_position == 0)
12537 goto done;
12539 clear_glyph_matrix (w->desired_matrix);
12540 centering_position = 0;
12541 goto recenter;
12544 done:
12546 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12547 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12548 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12549 ? Qt : Qnil);
12551 /* Display the mode line, if we must. */
12552 if ((update_mode_line
12553 /* If window not full width, must redo its mode line
12554 if (a) the window to its side is being redone and
12555 (b) we do a frame-based redisplay. This is a consequence
12556 of how inverted lines are drawn in frame-based redisplay. */
12557 || (!just_this_one_p
12558 && !FRAME_WINDOW_P (f)
12559 && !WINDOW_FULL_WIDTH_P (w))
12560 /* Line number to display. */
12561 || INTEGERP (w->base_line_pos)
12562 /* Column number is displayed and different from the one displayed. */
12563 || (!NILP (w->column_number_displayed)
12564 && (XFASTINT (w->column_number_displayed)
12565 != (int) current_column ()))) /* iftc */
12566 /* This means that the window has a mode line. */
12567 && (WINDOW_WANTS_MODELINE_P (w)
12568 || WINDOW_WANTS_HEADER_LINE_P (w)))
12570 display_mode_lines (w);
12572 /* If mode line height has changed, arrange for a thorough
12573 immediate redisplay using the correct mode line height. */
12574 if (WINDOW_WANTS_MODELINE_P (w)
12575 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12577 fonts_changed_p = 1;
12578 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12579 = DESIRED_MODE_LINE_HEIGHT (w);
12582 /* If top line height has changed, arrange for a thorough
12583 immediate redisplay using the correct mode line height. */
12584 if (WINDOW_WANTS_HEADER_LINE_P (w)
12585 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12587 fonts_changed_p = 1;
12588 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12589 = DESIRED_HEADER_LINE_HEIGHT (w);
12592 if (fonts_changed_p)
12593 goto need_larger_matrices;
12596 if (!line_number_displayed
12597 && !BUFFERP (w->base_line_pos))
12599 w->base_line_pos = Qnil;
12600 w->base_line_number = Qnil;
12603 finish_menu_bars:
12605 /* When we reach a frame's selected window, redo the frame's menu bar. */
12606 if (update_mode_line
12607 && EQ (FRAME_SELECTED_WINDOW (f), window))
12609 int redisplay_menu_p = 0;
12610 int redisplay_tool_bar_p = 0;
12612 if (FRAME_WINDOW_P (f))
12614 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12615 || defined (USE_GTK)
12616 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12617 #else
12618 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12619 #endif
12621 else
12622 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12624 if (redisplay_menu_p)
12625 display_menu_bar (w);
12627 #ifdef HAVE_WINDOW_SYSTEM
12628 #ifdef USE_GTK
12629 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12630 #else
12631 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12632 && (FRAME_TOOL_BAR_LINES (f) > 0
12633 || auto_resize_tool_bars_p);
12635 #endif
12637 if (redisplay_tool_bar_p)
12638 redisplay_tool_bar (f);
12639 #endif
12642 #ifdef HAVE_WINDOW_SYSTEM
12643 if (FRAME_WINDOW_P (f)
12644 && update_window_fringes (w, 0)
12645 && !just_this_one_p
12646 && (used_current_matrix_p || overlay_arrow_seen)
12647 && !w->pseudo_window_p)
12649 update_begin (f);
12650 BLOCK_INPUT;
12651 if (draw_window_fringes (w, 1))
12652 x_draw_vertical_border (w);
12653 UNBLOCK_INPUT;
12654 update_end (f);
12656 #endif /* HAVE_WINDOW_SYSTEM */
12658 /* We go to this label, with fonts_changed_p nonzero,
12659 if it is necessary to try again using larger glyph matrices.
12660 We have to redeem the scroll bar even in this case,
12661 because the loop in redisplay_internal expects that. */
12662 need_larger_matrices:
12664 finish_scroll_bars:
12666 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12668 /* Set the thumb's position and size. */
12669 set_vertical_scroll_bar (w);
12671 /* Note that we actually used the scroll bar attached to this
12672 window, so it shouldn't be deleted at the end of redisplay. */
12673 redeem_scroll_bar_hook (w);
12676 /* Restore current_buffer and value of point in it. */
12677 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12678 set_buffer_internal_1 (old);
12679 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12681 unbind_to (count, Qnil);
12685 /* Build the complete desired matrix of WINDOW with a window start
12686 buffer position POS. Value is non-zero if successful. It is zero
12687 if fonts were loaded during redisplay which makes re-adjusting
12688 glyph matrices necessary. */
12691 try_window (window, pos)
12692 Lisp_Object window;
12693 struct text_pos pos;
12695 struct window *w = XWINDOW (window);
12696 struct it it;
12697 struct glyph_row *last_text_row = NULL;
12699 /* Make POS the new window start. */
12700 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12702 /* Mark cursor position as unknown. No overlay arrow seen. */
12703 w->cursor.vpos = -1;
12704 overlay_arrow_seen = 0;
12706 /* Initialize iterator and info to start at POS. */
12707 start_display (&it, w, pos);
12709 /* Display all lines of W. */
12710 while (it.current_y < it.last_visible_y)
12712 if (display_line (&it))
12713 last_text_row = it.glyph_row - 1;
12714 if (fonts_changed_p)
12715 return 0;
12718 /* If bottom moved off end of frame, change mode line percentage. */
12719 if (XFASTINT (w->window_end_pos) <= 0
12720 && Z != IT_CHARPOS (it))
12721 w->update_mode_line = Qt;
12723 /* Set window_end_pos to the offset of the last character displayed
12724 on the window from the end of current_buffer. Set
12725 window_end_vpos to its row number. */
12726 if (last_text_row)
12728 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12729 w->window_end_bytepos
12730 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12731 w->window_end_pos
12732 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12733 w->window_end_vpos
12734 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12735 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12736 ->displays_text_p);
12738 else
12740 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12741 w->window_end_pos = make_number (Z - ZV);
12742 w->window_end_vpos = make_number (0);
12745 /* But that is not valid info until redisplay finishes. */
12746 w->window_end_valid = Qnil;
12747 return 1;
12752 /************************************************************************
12753 Window redisplay reusing current matrix when buffer has not changed
12754 ************************************************************************/
12756 /* Try redisplay of window W showing an unchanged buffer with a
12757 different window start than the last time it was displayed by
12758 reusing its current matrix. Value is non-zero if successful.
12759 W->start is the new window start. */
12761 static int
12762 try_window_reusing_current_matrix (w)
12763 struct window *w;
12765 struct frame *f = XFRAME (w->frame);
12766 struct glyph_row *row, *bottom_row;
12767 struct it it;
12768 struct run run;
12769 struct text_pos start, new_start;
12770 int nrows_scrolled, i;
12771 struct glyph_row *last_text_row;
12772 struct glyph_row *last_reused_text_row;
12773 struct glyph_row *start_row;
12774 int start_vpos, min_y, max_y;
12776 #if GLYPH_DEBUG
12777 if (inhibit_try_window_reusing)
12778 return 0;
12779 #endif
12781 if (/* This function doesn't handle terminal frames. */
12782 !FRAME_WINDOW_P (f)
12783 /* Don't try to reuse the display if windows have been split
12784 or such. */
12785 || windows_or_buffers_changed
12786 || cursor_type_changed)
12787 return 0;
12789 /* Can't do this if region may have changed. */
12790 if ((!NILP (Vtransient_mark_mode)
12791 && !NILP (current_buffer->mark_active))
12792 || !NILP (w->region_showing)
12793 || !NILP (Vshow_trailing_whitespace))
12794 return 0;
12796 /* If top-line visibility has changed, give up. */
12797 if (WINDOW_WANTS_HEADER_LINE_P (w)
12798 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12799 return 0;
12801 /* Give up if old or new display is scrolled vertically. We could
12802 make this function handle this, but right now it doesn't. */
12803 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12804 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12805 return 0;
12807 /* The variable new_start now holds the new window start. The old
12808 start `start' can be determined from the current matrix. */
12809 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12810 start = start_row->start.pos;
12811 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12813 /* Clear the desired matrix for the display below. */
12814 clear_glyph_matrix (w->desired_matrix);
12816 if (CHARPOS (new_start) <= CHARPOS (start))
12818 int first_row_y;
12820 /* Don't use this method if the display starts with an ellipsis
12821 displayed for invisible text. It's not easy to handle that case
12822 below, and it's certainly not worth the effort since this is
12823 not a frequent case. */
12824 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12825 return 0;
12827 IF_DEBUG (debug_method_add (w, "twu1"));
12829 /* Display up to a row that can be reused. The variable
12830 last_text_row is set to the last row displayed that displays
12831 text. Note that it.vpos == 0 if or if not there is a
12832 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12833 start_display (&it, w, new_start);
12834 first_row_y = it.current_y;
12835 w->cursor.vpos = -1;
12836 last_text_row = last_reused_text_row = NULL;
12838 while (it.current_y < it.last_visible_y
12839 && !fonts_changed_p)
12841 /* If we have reached into the characters in the START row,
12842 that means the line boundaries have changed. So we
12843 can't start copying with the row START. Maybe it will
12844 work to start copying with the following row. */
12845 while (IT_CHARPOS (it) > CHARPOS (start))
12847 /* Advance to the next row as the "start". */
12848 start_row++;
12849 start = start_row->start.pos;
12850 /* If there are no more rows to try, or just one, give up. */
12851 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12852 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12853 || CHARPOS (start) == ZV)
12855 clear_glyph_matrix (w->desired_matrix);
12856 return 0;
12859 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12861 /* If we have reached alignment,
12862 we can copy the rest of the rows. */
12863 if (IT_CHARPOS (it) == CHARPOS (start))
12864 break;
12866 if (display_line (&it))
12867 last_text_row = it.glyph_row - 1;
12870 /* A value of current_y < last_visible_y means that we stopped
12871 at the previous window start, which in turn means that we
12872 have at least one reusable row. */
12873 if (it.current_y < it.last_visible_y)
12875 /* IT.vpos always starts from 0; it counts text lines. */
12876 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12878 /* Find PT if not already found in the lines displayed. */
12879 if (w->cursor.vpos < 0)
12881 int dy = it.current_y - start_row->y;
12883 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12884 row = row_containing_pos (w, PT, row, NULL, dy);
12885 if (row)
12886 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12887 dy, nrows_scrolled);
12888 else
12890 clear_glyph_matrix (w->desired_matrix);
12891 return 0;
12895 /* Scroll the display. Do it before the current matrix is
12896 changed. The problem here is that update has not yet
12897 run, i.e. part of the current matrix is not up to date.
12898 scroll_run_hook will clear the cursor, and use the
12899 current matrix to get the height of the row the cursor is
12900 in. */
12901 run.current_y = start_row->y;
12902 run.desired_y = it.current_y;
12903 run.height = it.last_visible_y - it.current_y;
12905 if (run.height > 0 && run.current_y != run.desired_y)
12907 update_begin (f);
12908 rif->update_window_begin_hook (w);
12909 rif->clear_window_mouse_face (w);
12910 rif->scroll_run_hook (w, &run);
12911 rif->update_window_end_hook (w, 0, 0);
12912 update_end (f);
12915 /* Shift current matrix down by nrows_scrolled lines. */
12916 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12917 rotate_matrix (w->current_matrix,
12918 start_vpos,
12919 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12920 nrows_scrolled);
12922 /* Disable lines that must be updated. */
12923 for (i = 0; i < it.vpos; ++i)
12924 (start_row + i)->enabled_p = 0;
12926 /* Re-compute Y positions. */
12927 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12928 max_y = it.last_visible_y;
12929 for (row = start_row + nrows_scrolled;
12930 row < bottom_row;
12931 ++row)
12933 row->y = it.current_y;
12934 row->visible_height = row->height;
12936 if (row->y < min_y)
12937 row->visible_height -= min_y - row->y;
12938 if (row->y + row->height > max_y)
12939 row->visible_height -= row->y + row->height - max_y;
12940 row->redraw_fringe_bitmaps_p = 1;
12942 it.current_y += row->height;
12944 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12945 last_reused_text_row = row;
12946 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12947 break;
12950 /* Disable lines in the current matrix which are now
12951 below the window. */
12952 for (++row; row < bottom_row; ++row)
12953 row->enabled_p = 0;
12956 /* Update window_end_pos etc.; last_reused_text_row is the last
12957 reused row from the current matrix containing text, if any.
12958 The value of last_text_row is the last displayed line
12959 containing text. */
12960 if (last_reused_text_row)
12962 w->window_end_bytepos
12963 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12964 w->window_end_pos
12965 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12966 w->window_end_vpos
12967 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12968 w->current_matrix));
12970 else if (last_text_row)
12972 w->window_end_bytepos
12973 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12974 w->window_end_pos
12975 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12976 w->window_end_vpos
12977 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12979 else
12981 /* This window must be completely empty. */
12982 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12983 w->window_end_pos = make_number (Z - ZV);
12984 w->window_end_vpos = make_number (0);
12986 w->window_end_valid = Qnil;
12988 /* Update hint: don't try scrolling again in update_window. */
12989 w->desired_matrix->no_scrolling_p = 1;
12991 #if GLYPH_DEBUG
12992 debug_method_add (w, "try_window_reusing_current_matrix 1");
12993 #endif
12994 return 1;
12996 else if (CHARPOS (new_start) > CHARPOS (start))
12998 struct glyph_row *pt_row, *row;
12999 struct glyph_row *first_reusable_row;
13000 struct glyph_row *first_row_to_display;
13001 int dy;
13002 int yb = window_text_bottom_y (w);
13004 /* Find the row starting at new_start, if there is one. Don't
13005 reuse a partially visible line at the end. */
13006 first_reusable_row = start_row;
13007 while (first_reusable_row->enabled_p
13008 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13009 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13010 < CHARPOS (new_start)))
13011 ++first_reusable_row;
13013 /* Give up if there is no row to reuse. */
13014 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13015 || !first_reusable_row->enabled_p
13016 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13017 != CHARPOS (new_start)))
13018 return 0;
13020 /* We can reuse fully visible rows beginning with
13021 first_reusable_row to the end of the window. Set
13022 first_row_to_display to the first row that cannot be reused.
13023 Set pt_row to the row containing point, if there is any. */
13024 pt_row = NULL;
13025 for (first_row_to_display = first_reusable_row;
13026 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13027 ++first_row_to_display)
13029 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13030 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13031 pt_row = first_row_to_display;
13034 /* Start displaying at the start of first_row_to_display. */
13035 xassert (first_row_to_display->y < yb);
13036 init_to_row_start (&it, w, first_row_to_display);
13038 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13039 - start_vpos);
13040 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13041 - nrows_scrolled);
13042 it.current_y = (first_row_to_display->y - first_reusable_row->y
13043 + WINDOW_HEADER_LINE_HEIGHT (w));
13045 /* Display lines beginning with first_row_to_display in the
13046 desired matrix. Set last_text_row to the last row displayed
13047 that displays text. */
13048 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13049 if (pt_row == NULL)
13050 w->cursor.vpos = -1;
13051 last_text_row = NULL;
13052 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13053 if (display_line (&it))
13054 last_text_row = it.glyph_row - 1;
13056 /* Give up If point isn't in a row displayed or reused. */
13057 if (w->cursor.vpos < 0)
13059 clear_glyph_matrix (w->desired_matrix);
13060 return 0;
13063 /* If point is in a reused row, adjust y and vpos of the cursor
13064 position. */
13065 if (pt_row)
13067 w->cursor.vpos -= nrows_scrolled;
13068 w->cursor.y -= first_reusable_row->y - start_row->y;
13071 /* Scroll the display. */
13072 run.current_y = first_reusable_row->y;
13073 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13074 run.height = it.last_visible_y - run.current_y;
13075 dy = run.current_y - run.desired_y;
13077 if (run.height)
13079 update_begin (f);
13080 rif->update_window_begin_hook (w);
13081 rif->clear_window_mouse_face (w);
13082 rif->scroll_run_hook (w, &run);
13083 rif->update_window_end_hook (w, 0, 0);
13084 update_end (f);
13087 /* Adjust Y positions of reused rows. */
13088 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13089 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13090 max_y = it.last_visible_y;
13091 for (row = first_reusable_row; row < first_row_to_display; ++row)
13093 row->y -= dy;
13094 row->visible_height = row->height;
13095 if (row->y < min_y)
13096 row->visible_height -= min_y - row->y;
13097 if (row->y + row->height > max_y)
13098 row->visible_height -= row->y + row->height - max_y;
13099 row->redraw_fringe_bitmaps_p = 1;
13102 /* Scroll the current matrix. */
13103 xassert (nrows_scrolled > 0);
13104 rotate_matrix (w->current_matrix,
13105 start_vpos,
13106 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13107 -nrows_scrolled);
13109 /* Disable rows not reused. */
13110 for (row -= nrows_scrolled; row < bottom_row; ++row)
13111 row->enabled_p = 0;
13113 /* Point may have moved to a different line, so we cannot assume that
13114 the previous cursor position is valid; locate the correct row. */
13115 if (pt_row)
13117 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13118 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13119 row++)
13121 w->cursor.vpos++;
13122 w->cursor.y = row->y;
13124 if (row < bottom_row)
13126 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13127 while (glyph->charpos < PT)
13129 w->cursor.hpos++;
13130 w->cursor.x += glyph->pixel_width;
13131 glyph++;
13136 /* Adjust window end. A null value of last_text_row means that
13137 the window end is in reused rows which in turn means that
13138 only its vpos can have changed. */
13139 if (last_text_row)
13141 w->window_end_bytepos
13142 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13143 w->window_end_pos
13144 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13145 w->window_end_vpos
13146 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13148 else
13150 w->window_end_vpos
13151 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13154 w->window_end_valid = Qnil;
13155 w->desired_matrix->no_scrolling_p = 1;
13157 #if GLYPH_DEBUG
13158 debug_method_add (w, "try_window_reusing_current_matrix 2");
13159 #endif
13160 return 1;
13163 return 0;
13168 /************************************************************************
13169 Window redisplay reusing current matrix when buffer has changed
13170 ************************************************************************/
13172 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13173 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13174 int *, int *));
13175 static struct glyph_row *
13176 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13177 struct glyph_row *));
13180 /* Return the last row in MATRIX displaying text. If row START is
13181 non-null, start searching with that row. IT gives the dimensions
13182 of the display. Value is null if matrix is empty; otherwise it is
13183 a pointer to the row found. */
13185 static struct glyph_row *
13186 find_last_row_displaying_text (matrix, it, start)
13187 struct glyph_matrix *matrix;
13188 struct it *it;
13189 struct glyph_row *start;
13191 struct glyph_row *row, *row_found;
13193 /* Set row_found to the last row in IT->w's current matrix
13194 displaying text. The loop looks funny but think of partially
13195 visible lines. */
13196 row_found = NULL;
13197 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13198 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13200 xassert (row->enabled_p);
13201 row_found = row;
13202 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13203 break;
13204 ++row;
13207 return row_found;
13211 /* Return the last row in the current matrix of W that is not affected
13212 by changes at the start of current_buffer that occurred since W's
13213 current matrix was built. Value is null if no such row exists.
13215 BEG_UNCHANGED us the number of characters unchanged at the start of
13216 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13217 first changed character in current_buffer. Characters at positions <
13218 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13219 when the current matrix was built. */
13221 static struct glyph_row *
13222 find_last_unchanged_at_beg_row (w)
13223 struct window *w;
13225 int first_changed_pos = BEG + BEG_UNCHANGED;
13226 struct glyph_row *row;
13227 struct glyph_row *row_found = NULL;
13228 int yb = window_text_bottom_y (w);
13230 /* Find the last row displaying unchanged text. */
13231 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13232 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13233 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13235 if (/* If row ends before first_changed_pos, it is unchanged,
13236 except in some case. */
13237 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13238 /* When row ends in ZV and we write at ZV it is not
13239 unchanged. */
13240 && !row->ends_at_zv_p
13241 /* When first_changed_pos is the end of a continued line,
13242 row is not unchanged because it may be no longer
13243 continued. */
13244 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13245 && (row->continued_p
13246 || row->exact_window_width_line_p)))
13247 row_found = row;
13249 /* Stop if last visible row. */
13250 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13251 break;
13253 ++row;
13256 return row_found;
13260 /* Find the first glyph row in the current matrix of W that is not
13261 affected by changes at the end of current_buffer since the
13262 time W's current matrix was built.
13264 Return in *DELTA the number of chars by which buffer positions in
13265 unchanged text at the end of current_buffer must be adjusted.
13267 Return in *DELTA_BYTES the corresponding number of bytes.
13269 Value is null if no such row exists, i.e. all rows are affected by
13270 changes. */
13272 static struct glyph_row *
13273 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13274 struct window *w;
13275 int *delta, *delta_bytes;
13277 struct glyph_row *row;
13278 struct glyph_row *row_found = NULL;
13280 *delta = *delta_bytes = 0;
13282 /* Display must not have been paused, otherwise the current matrix
13283 is not up to date. */
13284 if (NILP (w->window_end_valid))
13285 abort ();
13287 /* A value of window_end_pos >= END_UNCHANGED means that the window
13288 end is in the range of changed text. If so, there is no
13289 unchanged row at the end of W's current matrix. */
13290 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13291 return NULL;
13293 /* Set row to the last row in W's current matrix displaying text. */
13294 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13296 /* If matrix is entirely empty, no unchanged row exists. */
13297 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13299 /* The value of row is the last glyph row in the matrix having a
13300 meaningful buffer position in it. The end position of row
13301 corresponds to window_end_pos. This allows us to translate
13302 buffer positions in the current matrix to current buffer
13303 positions for characters not in changed text. */
13304 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13305 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13306 int last_unchanged_pos, last_unchanged_pos_old;
13307 struct glyph_row *first_text_row
13308 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13310 *delta = Z - Z_old;
13311 *delta_bytes = Z_BYTE - Z_BYTE_old;
13313 /* Set last_unchanged_pos to the buffer position of the last
13314 character in the buffer that has not been changed. Z is the
13315 index + 1 of the last character in current_buffer, i.e. by
13316 subtracting END_UNCHANGED we get the index of the last
13317 unchanged character, and we have to add BEG to get its buffer
13318 position. */
13319 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13320 last_unchanged_pos_old = last_unchanged_pos - *delta;
13322 /* Search backward from ROW for a row displaying a line that
13323 starts at a minimum position >= last_unchanged_pos_old. */
13324 for (; row > first_text_row; --row)
13326 /* This used to abort, but it can happen.
13327 It is ok to just stop the search instead here. KFS. */
13328 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13329 break;
13331 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13332 row_found = row;
13336 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13337 abort ();
13339 return row_found;
13343 /* Make sure that glyph rows in the current matrix of window W
13344 reference the same glyph memory as corresponding rows in the
13345 frame's frame matrix. This function is called after scrolling W's
13346 current matrix on a terminal frame in try_window_id and
13347 try_window_reusing_current_matrix. */
13349 static void
13350 sync_frame_with_window_matrix_rows (w)
13351 struct window *w;
13353 struct frame *f = XFRAME (w->frame);
13354 struct glyph_row *window_row, *window_row_end, *frame_row;
13356 /* Preconditions: W must be a leaf window and full-width. Its frame
13357 must have a frame matrix. */
13358 xassert (NILP (w->hchild) && NILP (w->vchild));
13359 xassert (WINDOW_FULL_WIDTH_P (w));
13360 xassert (!FRAME_WINDOW_P (f));
13362 /* If W is a full-width window, glyph pointers in W's current matrix
13363 have, by definition, to be the same as glyph pointers in the
13364 corresponding frame matrix. Note that frame matrices have no
13365 marginal areas (see build_frame_matrix). */
13366 window_row = w->current_matrix->rows;
13367 window_row_end = window_row + w->current_matrix->nrows;
13368 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13369 while (window_row < window_row_end)
13371 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13372 struct glyph *end = window_row->glyphs[LAST_AREA];
13374 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13375 frame_row->glyphs[TEXT_AREA] = start;
13376 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13377 frame_row->glyphs[LAST_AREA] = end;
13379 /* Disable frame rows whose corresponding window rows have
13380 been disabled in try_window_id. */
13381 if (!window_row->enabled_p)
13382 frame_row->enabled_p = 0;
13384 ++window_row, ++frame_row;
13389 /* Find the glyph row in window W containing CHARPOS. Consider all
13390 rows between START and END (not inclusive). END null means search
13391 all rows to the end of the display area of W. Value is the row
13392 containing CHARPOS or null. */
13394 struct glyph_row *
13395 row_containing_pos (w, charpos, start, end, dy)
13396 struct window *w;
13397 int charpos;
13398 struct glyph_row *start, *end;
13399 int dy;
13401 struct glyph_row *row = start;
13402 int last_y;
13404 /* If we happen to start on a header-line, skip that. */
13405 if (row->mode_line_p)
13406 ++row;
13408 if ((end && row >= end) || !row->enabled_p)
13409 return NULL;
13411 last_y = window_text_bottom_y (w) - dy;
13413 while (1)
13415 /* Give up if we have gone too far. */
13416 if (end && row >= end)
13417 return NULL;
13418 /* This formerly returned if they were equal.
13419 I think that both quantities are of a "last plus one" type;
13420 if so, when they are equal, the row is within the screen. -- rms. */
13421 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13422 return NULL;
13424 /* If it is in this row, return this row. */
13425 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13426 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13427 /* The end position of a row equals the start
13428 position of the next row. If CHARPOS is there, we
13429 would rather display it in the next line, except
13430 when this line ends in ZV. */
13431 && !row->ends_at_zv_p
13432 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13433 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13434 return row;
13435 ++row;
13440 /* Try to redisplay window W by reusing its existing display. W's
13441 current matrix must be up to date when this function is called,
13442 i.e. window_end_valid must not be nil.
13444 Value is
13446 1 if display has been updated
13447 0 if otherwise unsuccessful
13448 -1 if redisplay with same window start is known not to succeed
13450 The following steps are performed:
13452 1. Find the last row in the current matrix of W that is not
13453 affected by changes at the start of current_buffer. If no such row
13454 is found, give up.
13456 2. Find the first row in W's current matrix that is not affected by
13457 changes at the end of current_buffer. Maybe there is no such row.
13459 3. Display lines beginning with the row + 1 found in step 1 to the
13460 row found in step 2 or, if step 2 didn't find a row, to the end of
13461 the window.
13463 4. If cursor is not known to appear on the window, give up.
13465 5. If display stopped at the row found in step 2, scroll the
13466 display and current matrix as needed.
13468 6. Maybe display some lines at the end of W, if we must. This can
13469 happen under various circumstances, like a partially visible line
13470 becoming fully visible, or because newly displayed lines are displayed
13471 in smaller font sizes.
13473 7. Update W's window end information. */
13475 static int
13476 try_window_id (w)
13477 struct window *w;
13479 struct frame *f = XFRAME (w->frame);
13480 struct glyph_matrix *current_matrix = w->current_matrix;
13481 struct glyph_matrix *desired_matrix = w->desired_matrix;
13482 struct glyph_row *last_unchanged_at_beg_row;
13483 struct glyph_row *first_unchanged_at_end_row;
13484 struct glyph_row *row;
13485 struct glyph_row *bottom_row;
13486 int bottom_vpos;
13487 struct it it;
13488 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13489 struct text_pos start_pos;
13490 struct run run;
13491 int first_unchanged_at_end_vpos = 0;
13492 struct glyph_row *last_text_row, *last_text_row_at_end;
13493 struct text_pos start;
13494 int first_changed_charpos, last_changed_charpos;
13496 #if GLYPH_DEBUG
13497 if (inhibit_try_window_id)
13498 return 0;
13499 #endif
13501 /* This is handy for debugging. */
13502 #if 0
13503 #define GIVE_UP(X) \
13504 do { \
13505 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13506 return 0; \
13507 } while (0)
13508 #else
13509 #define GIVE_UP(X) return 0
13510 #endif
13512 SET_TEXT_POS_FROM_MARKER (start, w->start);
13514 /* Don't use this for mini-windows because these can show
13515 messages and mini-buffers, and we don't handle that here. */
13516 if (MINI_WINDOW_P (w))
13517 GIVE_UP (1);
13519 /* This flag is used to prevent redisplay optimizations. */
13520 if (windows_or_buffers_changed || cursor_type_changed)
13521 GIVE_UP (2);
13523 /* Verify that narrowing has not changed.
13524 Also verify that we were not told to prevent redisplay optimizations.
13525 It would be nice to further
13526 reduce the number of cases where this prevents try_window_id. */
13527 if (current_buffer->clip_changed
13528 || current_buffer->prevent_redisplay_optimizations_p)
13529 GIVE_UP (3);
13531 /* Window must either use window-based redisplay or be full width. */
13532 if (!FRAME_WINDOW_P (f)
13533 && (!line_ins_del_ok
13534 || !WINDOW_FULL_WIDTH_P (w)))
13535 GIVE_UP (4);
13537 /* Give up if point is not known NOT to appear in W. */
13538 if (PT < CHARPOS (start))
13539 GIVE_UP (5);
13541 /* Another way to prevent redisplay optimizations. */
13542 if (XFASTINT (w->last_modified) == 0)
13543 GIVE_UP (6);
13545 /* Verify that window is not hscrolled. */
13546 if (XFASTINT (w->hscroll) != 0)
13547 GIVE_UP (7);
13549 /* Verify that display wasn't paused. */
13550 if (NILP (w->window_end_valid))
13551 GIVE_UP (8);
13553 /* Can't use this if highlighting a region because a cursor movement
13554 will do more than just set the cursor. */
13555 if (!NILP (Vtransient_mark_mode)
13556 && !NILP (current_buffer->mark_active))
13557 GIVE_UP (9);
13559 /* Likewise if highlighting trailing whitespace. */
13560 if (!NILP (Vshow_trailing_whitespace))
13561 GIVE_UP (11);
13563 /* Likewise if showing a region. */
13564 if (!NILP (w->region_showing))
13565 GIVE_UP (10);
13567 /* Can use this if overlay arrow position and or string have changed. */
13568 if (overlay_arrows_changed_p ())
13569 GIVE_UP (12);
13572 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13573 only if buffer has really changed. The reason is that the gap is
13574 initially at Z for freshly visited files. The code below would
13575 set end_unchanged to 0 in that case. */
13576 if (MODIFF > SAVE_MODIFF
13577 /* This seems to happen sometimes after saving a buffer. */
13578 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13580 if (GPT - BEG < BEG_UNCHANGED)
13581 BEG_UNCHANGED = GPT - BEG;
13582 if (Z - GPT < END_UNCHANGED)
13583 END_UNCHANGED = Z - GPT;
13586 /* The position of the first and last character that has been changed. */
13587 first_changed_charpos = BEG + BEG_UNCHANGED;
13588 last_changed_charpos = Z - END_UNCHANGED;
13590 /* If window starts after a line end, and the last change is in
13591 front of that newline, then changes don't affect the display.
13592 This case happens with stealth-fontification. Note that although
13593 the display is unchanged, glyph positions in the matrix have to
13594 be adjusted, of course. */
13595 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13596 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13597 && ((last_changed_charpos < CHARPOS (start)
13598 && CHARPOS (start) == BEGV)
13599 || (last_changed_charpos < CHARPOS (start) - 1
13600 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13602 int Z_old, delta, Z_BYTE_old, delta_bytes;
13603 struct glyph_row *r0;
13605 /* Compute how many chars/bytes have been added to or removed
13606 from the buffer. */
13607 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13608 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13609 delta = Z - Z_old;
13610 delta_bytes = Z_BYTE - Z_BYTE_old;
13612 /* Give up if PT is not in the window. Note that it already has
13613 been checked at the start of try_window_id that PT is not in
13614 front of the window start. */
13615 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13616 GIVE_UP (13);
13618 /* If window start is unchanged, we can reuse the whole matrix
13619 as is, after adjusting glyph positions. No need to compute
13620 the window end again, since its offset from Z hasn't changed. */
13621 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13622 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13623 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13624 /* PT must not be in a partially visible line. */
13625 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13626 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13628 /* Adjust positions in the glyph matrix. */
13629 if (delta || delta_bytes)
13631 struct glyph_row *r1
13632 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13633 increment_matrix_positions (w->current_matrix,
13634 MATRIX_ROW_VPOS (r0, current_matrix),
13635 MATRIX_ROW_VPOS (r1, current_matrix),
13636 delta, delta_bytes);
13639 /* Set the cursor. */
13640 row = row_containing_pos (w, PT, r0, NULL, 0);
13641 if (row)
13642 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13643 else
13644 abort ();
13645 return 1;
13649 /* Handle the case that changes are all below what is displayed in
13650 the window, and that PT is in the window. This shortcut cannot
13651 be taken if ZV is visible in the window, and text has been added
13652 there that is visible in the window. */
13653 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13654 /* ZV is not visible in the window, or there are no
13655 changes at ZV, actually. */
13656 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13657 || first_changed_charpos == last_changed_charpos))
13659 struct glyph_row *r0;
13661 /* Give up if PT is not in the window. Note that it already has
13662 been checked at the start of try_window_id that PT is not in
13663 front of the window start. */
13664 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13665 GIVE_UP (14);
13667 /* If window start is unchanged, we can reuse the whole matrix
13668 as is, without changing glyph positions since no text has
13669 been added/removed in front of the window end. */
13670 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13671 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13672 /* PT must not be in a partially visible line. */
13673 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13674 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13676 /* We have to compute the window end anew since text
13677 can have been added/removed after it. */
13678 w->window_end_pos
13679 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13680 w->window_end_bytepos
13681 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13683 /* Set the cursor. */
13684 row = row_containing_pos (w, PT, r0, NULL, 0);
13685 if (row)
13686 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13687 else
13688 abort ();
13689 return 2;
13693 /* Give up if window start is in the changed area.
13695 The condition used to read
13697 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13699 but why that was tested escapes me at the moment. */
13700 if (CHARPOS (start) >= first_changed_charpos
13701 && CHARPOS (start) <= last_changed_charpos)
13702 GIVE_UP (15);
13704 /* Check that window start agrees with the start of the first glyph
13705 row in its current matrix. Check this after we know the window
13706 start is not in changed text, otherwise positions would not be
13707 comparable. */
13708 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13709 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13710 GIVE_UP (16);
13712 /* Give up if the window ends in strings. Overlay strings
13713 at the end are difficult to handle, so don't try. */
13714 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13715 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13716 GIVE_UP (20);
13718 /* Compute the position at which we have to start displaying new
13719 lines. Some of the lines at the top of the window might be
13720 reusable because they are not displaying changed text. Find the
13721 last row in W's current matrix not affected by changes at the
13722 start of current_buffer. Value is null if changes start in the
13723 first line of window. */
13724 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13725 if (last_unchanged_at_beg_row)
13727 /* Avoid starting to display in the moddle of a character, a TAB
13728 for instance. This is easier than to set up the iterator
13729 exactly, and it's not a frequent case, so the additional
13730 effort wouldn't really pay off. */
13731 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13732 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13733 && last_unchanged_at_beg_row > w->current_matrix->rows)
13734 --last_unchanged_at_beg_row;
13736 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13737 GIVE_UP (17);
13739 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13740 GIVE_UP (18);
13741 start_pos = it.current.pos;
13743 /* Start displaying new lines in the desired matrix at the same
13744 vpos we would use in the current matrix, i.e. below
13745 last_unchanged_at_beg_row. */
13746 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13747 current_matrix);
13748 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13749 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13751 xassert (it.hpos == 0 && it.current_x == 0);
13753 else
13755 /* There are no reusable lines at the start of the window.
13756 Start displaying in the first text line. */
13757 start_display (&it, w, start);
13758 it.vpos = it.first_vpos;
13759 start_pos = it.current.pos;
13762 /* Find the first row that is not affected by changes at the end of
13763 the buffer. Value will be null if there is no unchanged row, in
13764 which case we must redisplay to the end of the window. delta
13765 will be set to the value by which buffer positions beginning with
13766 first_unchanged_at_end_row have to be adjusted due to text
13767 changes. */
13768 first_unchanged_at_end_row
13769 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13770 IF_DEBUG (debug_delta = delta);
13771 IF_DEBUG (debug_delta_bytes = delta_bytes);
13773 /* Set stop_pos to the buffer position up to which we will have to
13774 display new lines. If first_unchanged_at_end_row != NULL, this
13775 is the buffer position of the start of the line displayed in that
13776 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13777 that we don't stop at a buffer position. */
13778 stop_pos = 0;
13779 if (first_unchanged_at_end_row)
13781 xassert (last_unchanged_at_beg_row == NULL
13782 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13784 /* If this is a continuation line, move forward to the next one
13785 that isn't. Changes in lines above affect this line.
13786 Caution: this may move first_unchanged_at_end_row to a row
13787 not displaying text. */
13788 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13789 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13790 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13791 < it.last_visible_y))
13792 ++first_unchanged_at_end_row;
13794 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13795 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13796 >= it.last_visible_y))
13797 first_unchanged_at_end_row = NULL;
13798 else
13800 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13801 + delta);
13802 first_unchanged_at_end_vpos
13803 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13804 xassert (stop_pos >= Z - END_UNCHANGED);
13807 else if (last_unchanged_at_beg_row == NULL)
13808 GIVE_UP (19);
13811 #if GLYPH_DEBUG
13813 /* Either there is no unchanged row at the end, or the one we have
13814 now displays text. This is a necessary condition for the window
13815 end pos calculation at the end of this function. */
13816 xassert (first_unchanged_at_end_row == NULL
13817 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13819 debug_last_unchanged_at_beg_vpos
13820 = (last_unchanged_at_beg_row
13821 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13822 : -1);
13823 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13825 #endif /* GLYPH_DEBUG != 0 */
13828 /* Display new lines. Set last_text_row to the last new line
13829 displayed which has text on it, i.e. might end up as being the
13830 line where the window_end_vpos is. */
13831 w->cursor.vpos = -1;
13832 last_text_row = NULL;
13833 overlay_arrow_seen = 0;
13834 while (it.current_y < it.last_visible_y
13835 && !fonts_changed_p
13836 && (first_unchanged_at_end_row == NULL
13837 || IT_CHARPOS (it) < stop_pos))
13839 if (display_line (&it))
13840 last_text_row = it.glyph_row - 1;
13843 if (fonts_changed_p)
13844 return -1;
13847 /* Compute differences in buffer positions, y-positions etc. for
13848 lines reused at the bottom of the window. Compute what we can
13849 scroll. */
13850 if (first_unchanged_at_end_row
13851 /* No lines reused because we displayed everything up to the
13852 bottom of the window. */
13853 && it.current_y < it.last_visible_y)
13855 dvpos = (it.vpos
13856 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13857 current_matrix));
13858 dy = it.current_y - first_unchanged_at_end_row->y;
13859 run.current_y = first_unchanged_at_end_row->y;
13860 run.desired_y = run.current_y + dy;
13861 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13863 else
13865 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13866 first_unchanged_at_end_row = NULL;
13868 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13871 /* Find the cursor if not already found. We have to decide whether
13872 PT will appear on this window (it sometimes doesn't, but this is
13873 not a very frequent case.) This decision has to be made before
13874 the current matrix is altered. A value of cursor.vpos < 0 means
13875 that PT is either in one of the lines beginning at
13876 first_unchanged_at_end_row or below the window. Don't care for
13877 lines that might be displayed later at the window end; as
13878 mentioned, this is not a frequent case. */
13879 if (w->cursor.vpos < 0)
13881 /* Cursor in unchanged rows at the top? */
13882 if (PT < CHARPOS (start_pos)
13883 && last_unchanged_at_beg_row)
13885 row = row_containing_pos (w, PT,
13886 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13887 last_unchanged_at_beg_row + 1, 0);
13888 if (row)
13889 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13892 /* Start from first_unchanged_at_end_row looking for PT. */
13893 else if (first_unchanged_at_end_row)
13895 row = row_containing_pos (w, PT - delta,
13896 first_unchanged_at_end_row, NULL, 0);
13897 if (row)
13898 set_cursor_from_row (w, row, w->current_matrix, delta,
13899 delta_bytes, dy, dvpos);
13902 /* Give up if cursor was not found. */
13903 if (w->cursor.vpos < 0)
13905 clear_glyph_matrix (w->desired_matrix);
13906 return -1;
13910 /* Don't let the cursor end in the scroll margins. */
13912 int this_scroll_margin, cursor_height;
13914 this_scroll_margin = max (0, scroll_margin);
13915 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13916 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13917 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13919 if ((w->cursor.y < this_scroll_margin
13920 && CHARPOS (start) > BEGV)
13921 /* Old redisplay didn't take scroll margin into account at the bottom,
13922 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13923 || (w->cursor.y + (make_cursor_line_fully_visible_p
13924 ? cursor_height + this_scroll_margin
13925 : 1)) > it.last_visible_y)
13927 w->cursor.vpos = -1;
13928 clear_glyph_matrix (w->desired_matrix);
13929 return -1;
13933 /* Scroll the display. Do it before changing the current matrix so
13934 that xterm.c doesn't get confused about where the cursor glyph is
13935 found. */
13936 if (dy && run.height)
13938 update_begin (f);
13940 if (FRAME_WINDOW_P (f))
13942 rif->update_window_begin_hook (w);
13943 rif->clear_window_mouse_face (w);
13944 rif->scroll_run_hook (w, &run);
13945 rif->update_window_end_hook (w, 0, 0);
13947 else
13949 /* Terminal frame. In this case, dvpos gives the number of
13950 lines to scroll by; dvpos < 0 means scroll up. */
13951 int first_unchanged_at_end_vpos
13952 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13953 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13954 int end = (WINDOW_TOP_EDGE_LINE (w)
13955 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13956 + window_internal_height (w));
13958 /* Perform the operation on the screen. */
13959 if (dvpos > 0)
13961 /* Scroll last_unchanged_at_beg_row to the end of the
13962 window down dvpos lines. */
13963 set_terminal_window (end);
13965 /* On dumb terminals delete dvpos lines at the end
13966 before inserting dvpos empty lines. */
13967 if (!scroll_region_ok)
13968 ins_del_lines (end - dvpos, -dvpos);
13970 /* Insert dvpos empty lines in front of
13971 last_unchanged_at_beg_row. */
13972 ins_del_lines (from, dvpos);
13974 else if (dvpos < 0)
13976 /* Scroll up last_unchanged_at_beg_vpos to the end of
13977 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13978 set_terminal_window (end);
13980 /* Delete dvpos lines in front of
13981 last_unchanged_at_beg_vpos. ins_del_lines will set
13982 the cursor to the given vpos and emit |dvpos| delete
13983 line sequences. */
13984 ins_del_lines (from + dvpos, dvpos);
13986 /* On a dumb terminal insert dvpos empty lines at the
13987 end. */
13988 if (!scroll_region_ok)
13989 ins_del_lines (end + dvpos, -dvpos);
13992 set_terminal_window (0);
13995 update_end (f);
13998 /* Shift reused rows of the current matrix to the right position.
13999 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14000 text. */
14001 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14002 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14003 if (dvpos < 0)
14005 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14006 bottom_vpos, dvpos);
14007 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14008 bottom_vpos, 0);
14010 else if (dvpos > 0)
14012 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14013 bottom_vpos, dvpos);
14014 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14015 first_unchanged_at_end_vpos + dvpos, 0);
14018 /* For frame-based redisplay, make sure that current frame and window
14019 matrix are in sync with respect to glyph memory. */
14020 if (!FRAME_WINDOW_P (f))
14021 sync_frame_with_window_matrix_rows (w);
14023 /* Adjust buffer positions in reused rows. */
14024 if (delta)
14025 increment_matrix_positions (current_matrix,
14026 first_unchanged_at_end_vpos + dvpos,
14027 bottom_vpos, delta, delta_bytes);
14029 /* Adjust Y positions. */
14030 if (dy)
14031 shift_glyph_matrix (w, current_matrix,
14032 first_unchanged_at_end_vpos + dvpos,
14033 bottom_vpos, dy);
14035 if (first_unchanged_at_end_row)
14037 first_unchanged_at_end_row += dvpos;
14038 if (first_unchanged_at_end_row->y >= it.last_visible_y
14039 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14040 first_unchanged_at_end_row = NULL;
14043 /* If scrolling up, there may be some lines to display at the end of
14044 the window. */
14045 last_text_row_at_end = NULL;
14046 if (dy < 0)
14048 /* Scrolling up can leave for example a partially visible line
14049 at the end of the window to be redisplayed. */
14050 /* Set last_row to the glyph row in the current matrix where the
14051 window end line is found. It has been moved up or down in
14052 the matrix by dvpos. */
14053 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14054 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14056 /* If last_row is the window end line, it should display text. */
14057 xassert (last_row->displays_text_p);
14059 /* If window end line was partially visible before, begin
14060 displaying at that line. Otherwise begin displaying with the
14061 line following it. */
14062 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14064 init_to_row_start (&it, w, last_row);
14065 it.vpos = last_vpos;
14066 it.current_y = last_row->y;
14068 else
14070 init_to_row_end (&it, w, last_row);
14071 it.vpos = 1 + last_vpos;
14072 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14073 ++last_row;
14076 /* We may start in a continuation line. If so, we have to
14077 get the right continuation_lines_width and current_x. */
14078 it.continuation_lines_width = last_row->continuation_lines_width;
14079 it.hpos = it.current_x = 0;
14081 /* Display the rest of the lines at the window end. */
14082 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14083 while (it.current_y < it.last_visible_y
14084 && !fonts_changed_p)
14086 /* Is it always sure that the display agrees with lines in
14087 the current matrix? I don't think so, so we mark rows
14088 displayed invalid in the current matrix by setting their
14089 enabled_p flag to zero. */
14090 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14091 if (display_line (&it))
14092 last_text_row_at_end = it.glyph_row - 1;
14096 /* Update window_end_pos and window_end_vpos. */
14097 if (first_unchanged_at_end_row
14098 && !last_text_row_at_end)
14100 /* Window end line if one of the preserved rows from the current
14101 matrix. Set row to the last row displaying text in current
14102 matrix starting at first_unchanged_at_end_row, after
14103 scrolling. */
14104 xassert (first_unchanged_at_end_row->displays_text_p);
14105 row = find_last_row_displaying_text (w->current_matrix, &it,
14106 first_unchanged_at_end_row);
14107 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14109 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14110 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14111 w->window_end_vpos
14112 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14113 xassert (w->window_end_bytepos >= 0);
14114 IF_DEBUG (debug_method_add (w, "A"));
14116 else if (last_text_row_at_end)
14118 w->window_end_pos
14119 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14120 w->window_end_bytepos
14121 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14122 w->window_end_vpos
14123 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14124 xassert (w->window_end_bytepos >= 0);
14125 IF_DEBUG (debug_method_add (w, "B"));
14127 else if (last_text_row)
14129 /* We have displayed either to the end of the window or at the
14130 end of the window, i.e. the last row with text is to be found
14131 in the desired matrix. */
14132 w->window_end_pos
14133 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14134 w->window_end_bytepos
14135 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14136 w->window_end_vpos
14137 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14138 xassert (w->window_end_bytepos >= 0);
14140 else if (first_unchanged_at_end_row == NULL
14141 && last_text_row == NULL
14142 && last_text_row_at_end == NULL)
14144 /* Displayed to end of window, but no line containing text was
14145 displayed. Lines were deleted at the end of the window. */
14146 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14147 int vpos = XFASTINT (w->window_end_vpos);
14148 struct glyph_row *current_row = current_matrix->rows + vpos;
14149 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14151 for (row = NULL;
14152 row == NULL && vpos >= first_vpos;
14153 --vpos, --current_row, --desired_row)
14155 if (desired_row->enabled_p)
14157 if (desired_row->displays_text_p)
14158 row = desired_row;
14160 else if (current_row->displays_text_p)
14161 row = current_row;
14164 xassert (row != NULL);
14165 w->window_end_vpos = make_number (vpos + 1);
14166 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14167 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14168 xassert (w->window_end_bytepos >= 0);
14169 IF_DEBUG (debug_method_add (w, "C"));
14171 else
14172 abort ();
14174 #if 0 /* This leads to problems, for instance when the cursor is
14175 at ZV, and the cursor line displays no text. */
14176 /* Disable rows below what's displayed in the window. This makes
14177 debugging easier. */
14178 enable_glyph_matrix_rows (current_matrix,
14179 XFASTINT (w->window_end_vpos) + 1,
14180 bottom_vpos, 0);
14181 #endif
14183 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14184 debug_end_vpos = XFASTINT (w->window_end_vpos));
14186 /* Record that display has not been completed. */
14187 w->window_end_valid = Qnil;
14188 w->desired_matrix->no_scrolling_p = 1;
14189 return 3;
14191 #undef GIVE_UP
14196 /***********************************************************************
14197 More debugging support
14198 ***********************************************************************/
14200 #if GLYPH_DEBUG
14202 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14203 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14204 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14207 /* Dump the contents of glyph matrix MATRIX on stderr.
14209 GLYPHS 0 means don't show glyph contents.
14210 GLYPHS 1 means show glyphs in short form
14211 GLYPHS > 1 means show glyphs in long form. */
14213 void
14214 dump_glyph_matrix (matrix, glyphs)
14215 struct glyph_matrix *matrix;
14216 int glyphs;
14218 int i;
14219 for (i = 0; i < matrix->nrows; ++i)
14220 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14224 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14225 the glyph row and area where the glyph comes from. */
14227 void
14228 dump_glyph (row, glyph, area)
14229 struct glyph_row *row;
14230 struct glyph *glyph;
14231 int area;
14233 if (glyph->type == CHAR_GLYPH)
14235 fprintf (stderr,
14236 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14237 glyph - row->glyphs[TEXT_AREA],
14238 'C',
14239 glyph->charpos,
14240 (BUFFERP (glyph->object)
14241 ? 'B'
14242 : (STRINGP (glyph->object)
14243 ? 'S'
14244 : '-')),
14245 glyph->pixel_width,
14246 glyph->u.ch,
14247 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14248 ? glyph->u.ch
14249 : '.'),
14250 glyph->face_id,
14251 glyph->left_box_line_p,
14252 glyph->right_box_line_p);
14254 else if (glyph->type == STRETCH_GLYPH)
14256 fprintf (stderr,
14257 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14258 glyph - row->glyphs[TEXT_AREA],
14259 'S',
14260 glyph->charpos,
14261 (BUFFERP (glyph->object)
14262 ? 'B'
14263 : (STRINGP (glyph->object)
14264 ? 'S'
14265 : '-')),
14266 glyph->pixel_width,
14268 '.',
14269 glyph->face_id,
14270 glyph->left_box_line_p,
14271 glyph->right_box_line_p);
14273 else if (glyph->type == IMAGE_GLYPH)
14275 fprintf (stderr,
14276 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14277 glyph - row->glyphs[TEXT_AREA],
14278 'I',
14279 glyph->charpos,
14280 (BUFFERP (glyph->object)
14281 ? 'B'
14282 : (STRINGP (glyph->object)
14283 ? 'S'
14284 : '-')),
14285 glyph->pixel_width,
14286 glyph->u.img_id,
14287 '.',
14288 glyph->face_id,
14289 glyph->left_box_line_p,
14290 glyph->right_box_line_p);
14295 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14296 GLYPHS 0 means don't show glyph contents.
14297 GLYPHS 1 means show glyphs in short form
14298 GLYPHS > 1 means show glyphs in long form. */
14300 void
14301 dump_glyph_row (row, vpos, glyphs)
14302 struct glyph_row *row;
14303 int vpos, glyphs;
14305 if (glyphs != 1)
14307 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14308 fprintf (stderr, "======================================================================\n");
14310 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14311 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14312 vpos,
14313 MATRIX_ROW_START_CHARPOS (row),
14314 MATRIX_ROW_END_CHARPOS (row),
14315 row->used[TEXT_AREA],
14316 row->contains_overlapping_glyphs_p,
14317 row->enabled_p,
14318 row->truncated_on_left_p,
14319 row->truncated_on_right_p,
14320 row->continued_p,
14321 MATRIX_ROW_CONTINUATION_LINE_P (row),
14322 row->displays_text_p,
14323 row->ends_at_zv_p,
14324 row->fill_line_p,
14325 row->ends_in_middle_of_char_p,
14326 row->starts_in_middle_of_char_p,
14327 row->mouse_face_p,
14328 row->x,
14329 row->y,
14330 row->pixel_width,
14331 row->height,
14332 row->visible_height,
14333 row->ascent,
14334 row->phys_ascent);
14335 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14336 row->end.overlay_string_index,
14337 row->continuation_lines_width);
14338 fprintf (stderr, "%9d %5d\n",
14339 CHARPOS (row->start.string_pos),
14340 CHARPOS (row->end.string_pos));
14341 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14342 row->end.dpvec_index);
14345 if (glyphs > 1)
14347 int area;
14349 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14351 struct glyph *glyph = row->glyphs[area];
14352 struct glyph *glyph_end = glyph + row->used[area];
14354 /* Glyph for a line end in text. */
14355 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14356 ++glyph_end;
14358 if (glyph < glyph_end)
14359 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14361 for (; glyph < glyph_end; ++glyph)
14362 dump_glyph (row, glyph, area);
14365 else if (glyphs == 1)
14367 int area;
14369 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14371 char *s = (char *) alloca (row->used[area] + 1);
14372 int i;
14374 for (i = 0; i < row->used[area]; ++i)
14376 struct glyph *glyph = row->glyphs[area] + i;
14377 if (glyph->type == CHAR_GLYPH
14378 && glyph->u.ch < 0x80
14379 && glyph->u.ch >= ' ')
14380 s[i] = glyph->u.ch;
14381 else
14382 s[i] = '.';
14385 s[i] = '\0';
14386 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14392 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14393 Sdump_glyph_matrix, 0, 1, "p",
14394 doc: /* Dump the current matrix of the selected window to stderr.
14395 Shows contents of glyph row structures. With non-nil
14396 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14397 glyphs in short form, otherwise show glyphs in long form. */)
14398 (glyphs)
14399 Lisp_Object glyphs;
14401 struct window *w = XWINDOW (selected_window);
14402 struct buffer *buffer = XBUFFER (w->buffer);
14404 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14405 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14406 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14407 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14408 fprintf (stderr, "=============================================\n");
14409 dump_glyph_matrix (w->current_matrix,
14410 NILP (glyphs) ? 0 : XINT (glyphs));
14411 return Qnil;
14415 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14416 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14419 struct frame *f = XFRAME (selected_frame);
14420 dump_glyph_matrix (f->current_matrix, 1);
14421 return Qnil;
14425 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14426 doc: /* Dump glyph row ROW to stderr.
14427 GLYPH 0 means don't dump glyphs.
14428 GLYPH 1 means dump glyphs in short form.
14429 GLYPH > 1 or omitted means dump glyphs in long form. */)
14430 (row, glyphs)
14431 Lisp_Object row, glyphs;
14433 struct glyph_matrix *matrix;
14434 int vpos;
14436 CHECK_NUMBER (row);
14437 matrix = XWINDOW (selected_window)->current_matrix;
14438 vpos = XINT (row);
14439 if (vpos >= 0 && vpos < matrix->nrows)
14440 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14441 vpos,
14442 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14443 return Qnil;
14447 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14448 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14449 GLYPH 0 means don't dump glyphs.
14450 GLYPH 1 means dump glyphs in short form.
14451 GLYPH > 1 or omitted means dump glyphs in long form. */)
14452 (row, glyphs)
14453 Lisp_Object row, glyphs;
14455 struct frame *sf = SELECTED_FRAME ();
14456 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14457 int vpos;
14459 CHECK_NUMBER (row);
14460 vpos = XINT (row);
14461 if (vpos >= 0 && vpos < m->nrows)
14462 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14463 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14464 return Qnil;
14468 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14469 doc: /* Toggle tracing of redisplay.
14470 With ARG, turn tracing on if and only if ARG is positive. */)
14471 (arg)
14472 Lisp_Object arg;
14474 if (NILP (arg))
14475 trace_redisplay_p = !trace_redisplay_p;
14476 else
14478 arg = Fprefix_numeric_value (arg);
14479 trace_redisplay_p = XINT (arg) > 0;
14482 return Qnil;
14486 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14487 doc: /* Like `format', but print result to stderr.
14488 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14489 (nargs, args)
14490 int nargs;
14491 Lisp_Object *args;
14493 Lisp_Object s = Fformat (nargs, args);
14494 fprintf (stderr, "%s", SDATA (s));
14495 return Qnil;
14498 #endif /* GLYPH_DEBUG */
14502 /***********************************************************************
14503 Building Desired Matrix Rows
14504 ***********************************************************************/
14506 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14507 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14509 static struct glyph_row *
14510 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14511 struct window *w;
14512 Lisp_Object overlay_arrow_string;
14514 struct frame *f = XFRAME (WINDOW_FRAME (w));
14515 struct buffer *buffer = XBUFFER (w->buffer);
14516 struct buffer *old = current_buffer;
14517 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14518 int arrow_len = SCHARS (overlay_arrow_string);
14519 const unsigned char *arrow_end = arrow_string + arrow_len;
14520 const unsigned char *p;
14521 struct it it;
14522 int multibyte_p;
14523 int n_glyphs_before;
14525 set_buffer_temp (buffer);
14526 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14527 it.glyph_row->used[TEXT_AREA] = 0;
14528 SET_TEXT_POS (it.position, 0, 0);
14530 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14531 p = arrow_string;
14532 while (p < arrow_end)
14534 Lisp_Object face, ilisp;
14536 /* Get the next character. */
14537 if (multibyte_p)
14538 it.c = string_char_and_length (p, arrow_len, &it.len);
14539 else
14540 it.c = *p, it.len = 1;
14541 p += it.len;
14543 /* Get its face. */
14544 ilisp = make_number (p - arrow_string);
14545 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14546 it.face_id = compute_char_face (f, it.c, face);
14548 /* Compute its width, get its glyphs. */
14549 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14550 SET_TEXT_POS (it.position, -1, -1);
14551 PRODUCE_GLYPHS (&it);
14553 /* If this character doesn't fit any more in the line, we have
14554 to remove some glyphs. */
14555 if (it.current_x > it.last_visible_x)
14557 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14558 break;
14562 set_buffer_temp (old);
14563 return it.glyph_row;
14567 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14568 glyphs are only inserted for terminal frames since we can't really
14569 win with truncation glyphs when partially visible glyphs are
14570 involved. Which glyphs to insert is determined by
14571 produce_special_glyphs. */
14573 static void
14574 insert_left_trunc_glyphs (it)
14575 struct it *it;
14577 struct it truncate_it;
14578 struct glyph *from, *end, *to, *toend;
14580 xassert (!FRAME_WINDOW_P (it->f));
14582 /* Get the truncation glyphs. */
14583 truncate_it = *it;
14584 truncate_it.current_x = 0;
14585 truncate_it.face_id = DEFAULT_FACE_ID;
14586 truncate_it.glyph_row = &scratch_glyph_row;
14587 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14588 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14589 truncate_it.object = make_number (0);
14590 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14592 /* Overwrite glyphs from IT with truncation glyphs. */
14593 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14594 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14595 to = it->glyph_row->glyphs[TEXT_AREA];
14596 toend = to + it->glyph_row->used[TEXT_AREA];
14598 while (from < end)
14599 *to++ = *from++;
14601 /* There may be padding glyphs left over. Overwrite them too. */
14602 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14604 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14605 while (from < end)
14606 *to++ = *from++;
14609 if (to > toend)
14610 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14614 /* Compute the pixel height and width of IT->glyph_row.
14616 Most of the time, ascent and height of a display line will be equal
14617 to the max_ascent and max_height values of the display iterator
14618 structure. This is not the case if
14620 1. We hit ZV without displaying anything. In this case, max_ascent
14621 and max_height will be zero.
14623 2. We have some glyphs that don't contribute to the line height.
14624 (The glyph row flag contributes_to_line_height_p is for future
14625 pixmap extensions).
14627 The first case is easily covered by using default values because in
14628 these cases, the line height does not really matter, except that it
14629 must not be zero. */
14631 static void
14632 compute_line_metrics (it)
14633 struct it *it;
14635 struct glyph_row *row = it->glyph_row;
14636 int area, i;
14638 if (FRAME_WINDOW_P (it->f))
14640 int i, min_y, max_y;
14642 /* The line may consist of one space only, that was added to
14643 place the cursor on it. If so, the row's height hasn't been
14644 computed yet. */
14645 if (row->height == 0)
14647 if (it->max_ascent + it->max_descent == 0)
14648 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14649 row->ascent = it->max_ascent;
14650 row->height = it->max_ascent + it->max_descent;
14651 row->phys_ascent = it->max_phys_ascent;
14652 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14653 row->extra_line_spacing = it->max_extra_line_spacing;
14656 /* Compute the width of this line. */
14657 row->pixel_width = row->x;
14658 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14659 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14661 xassert (row->pixel_width >= 0);
14662 xassert (row->ascent >= 0 && row->height > 0);
14664 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14665 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14667 /* If first line's physical ascent is larger than its logical
14668 ascent, use the physical ascent, and make the row taller.
14669 This makes accented characters fully visible. */
14670 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14671 && row->phys_ascent > row->ascent)
14673 row->height += row->phys_ascent - row->ascent;
14674 row->ascent = row->phys_ascent;
14677 /* Compute how much of the line is visible. */
14678 row->visible_height = row->height;
14680 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14681 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14683 if (row->y < min_y)
14684 row->visible_height -= min_y - row->y;
14685 if (row->y + row->height > max_y)
14686 row->visible_height -= row->y + row->height - max_y;
14688 else
14690 row->pixel_width = row->used[TEXT_AREA];
14691 if (row->continued_p)
14692 row->pixel_width -= it->continuation_pixel_width;
14693 else if (row->truncated_on_right_p)
14694 row->pixel_width -= it->truncation_pixel_width;
14695 row->ascent = row->phys_ascent = 0;
14696 row->height = row->phys_height = row->visible_height = 1;
14697 row->extra_line_spacing = 0;
14700 /* Compute a hash code for this row. */
14701 row->hash = 0;
14702 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14703 for (i = 0; i < row->used[area]; ++i)
14704 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14705 + row->glyphs[area][i].u.val
14706 + row->glyphs[area][i].face_id
14707 + row->glyphs[area][i].padding_p
14708 + (row->glyphs[area][i].type << 2));
14710 it->max_ascent = it->max_descent = 0;
14711 it->max_phys_ascent = it->max_phys_descent = 0;
14715 /* Append one space to the glyph row of iterator IT if doing a
14716 window-based redisplay. The space has the same face as
14717 IT->face_id. Value is non-zero if a space was added.
14719 This function is called to make sure that there is always one glyph
14720 at the end of a glyph row that the cursor can be set on under
14721 window-systems. (If there weren't such a glyph we would not know
14722 how wide and tall a box cursor should be displayed).
14724 At the same time this space let's a nicely handle clearing to the
14725 end of the line if the row ends in italic text. */
14727 static int
14728 append_space_for_newline (it, default_face_p)
14729 struct it *it;
14730 int default_face_p;
14732 if (FRAME_WINDOW_P (it->f))
14734 int n = it->glyph_row->used[TEXT_AREA];
14736 if (it->glyph_row->glyphs[TEXT_AREA] + n
14737 < it->glyph_row->glyphs[1 + TEXT_AREA])
14739 /* Save some values that must not be changed.
14740 Must save IT->c and IT->len because otherwise
14741 ITERATOR_AT_END_P wouldn't work anymore after
14742 append_space_for_newline has been called. */
14743 enum display_element_type saved_what = it->what;
14744 int saved_c = it->c, saved_len = it->len;
14745 int saved_x = it->current_x;
14746 int saved_face_id = it->face_id;
14747 struct text_pos saved_pos;
14748 Lisp_Object saved_object;
14749 struct face *face;
14751 saved_object = it->object;
14752 saved_pos = it->position;
14754 it->what = IT_CHARACTER;
14755 bzero (&it->position, sizeof it->position);
14756 it->object = make_number (0);
14757 it->c = ' ';
14758 it->len = 1;
14760 if (default_face_p)
14761 it->face_id = DEFAULT_FACE_ID;
14762 else if (it->face_before_selective_p)
14763 it->face_id = it->saved_face_id;
14764 face = FACE_FROM_ID (it->f, it->face_id);
14765 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14767 PRODUCE_GLYPHS (it);
14769 it->override_ascent = -1;
14770 it->constrain_row_ascent_descent_p = 0;
14771 it->current_x = saved_x;
14772 it->object = saved_object;
14773 it->position = saved_pos;
14774 it->what = saved_what;
14775 it->face_id = saved_face_id;
14776 it->len = saved_len;
14777 it->c = saved_c;
14778 return 1;
14782 return 0;
14786 /* Extend the face of the last glyph in the text area of IT->glyph_row
14787 to the end of the display line. Called from display_line.
14788 If the glyph row is empty, add a space glyph to it so that we
14789 know the face to draw. Set the glyph row flag fill_line_p. */
14791 static void
14792 extend_face_to_end_of_line (it)
14793 struct it *it;
14795 struct face *face;
14796 struct frame *f = it->f;
14798 /* If line is already filled, do nothing. */
14799 if (it->current_x >= it->last_visible_x)
14800 return;
14802 /* Face extension extends the background and box of IT->face_id
14803 to the end of the line. If the background equals the background
14804 of the frame, we don't have to do anything. */
14805 if (it->face_before_selective_p)
14806 face = FACE_FROM_ID (it->f, it->saved_face_id);
14807 else
14808 face = FACE_FROM_ID (f, it->face_id);
14810 if (FRAME_WINDOW_P (f)
14811 && face->box == FACE_NO_BOX
14812 && face->background == FRAME_BACKGROUND_PIXEL (f)
14813 && !face->stipple)
14814 return;
14816 /* Set the glyph row flag indicating that the face of the last glyph
14817 in the text area has to be drawn to the end of the text area. */
14818 it->glyph_row->fill_line_p = 1;
14820 /* If current character of IT is not ASCII, make sure we have the
14821 ASCII face. This will be automatically undone the next time
14822 get_next_display_element returns a multibyte character. Note
14823 that the character will always be single byte in unibyte text. */
14824 if (!SINGLE_BYTE_CHAR_P (it->c))
14826 it->face_id = FACE_FOR_CHAR (f, face, 0);
14829 if (FRAME_WINDOW_P (f))
14831 /* If the row is empty, add a space with the current face of IT,
14832 so that we know which face to draw. */
14833 if (it->glyph_row->used[TEXT_AREA] == 0)
14835 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14836 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14837 it->glyph_row->used[TEXT_AREA] = 1;
14840 else
14842 /* Save some values that must not be changed. */
14843 int saved_x = it->current_x;
14844 struct text_pos saved_pos;
14845 Lisp_Object saved_object;
14846 enum display_element_type saved_what = it->what;
14847 int saved_face_id = it->face_id;
14849 saved_object = it->object;
14850 saved_pos = it->position;
14852 it->what = IT_CHARACTER;
14853 bzero (&it->position, sizeof it->position);
14854 it->object = make_number (0);
14855 it->c = ' ';
14856 it->len = 1;
14857 it->face_id = face->id;
14859 PRODUCE_GLYPHS (it);
14861 while (it->current_x <= it->last_visible_x)
14862 PRODUCE_GLYPHS (it);
14864 /* Don't count these blanks really. It would let us insert a left
14865 truncation glyph below and make us set the cursor on them, maybe. */
14866 it->current_x = saved_x;
14867 it->object = saved_object;
14868 it->position = saved_pos;
14869 it->what = saved_what;
14870 it->face_id = saved_face_id;
14875 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14876 trailing whitespace. */
14878 static int
14879 trailing_whitespace_p (charpos)
14880 int charpos;
14882 int bytepos = CHAR_TO_BYTE (charpos);
14883 int c = 0;
14885 while (bytepos < ZV_BYTE
14886 && (c = FETCH_CHAR (bytepos),
14887 c == ' ' || c == '\t'))
14888 ++bytepos;
14890 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14892 if (bytepos != PT_BYTE)
14893 return 1;
14895 return 0;
14899 /* Highlight trailing whitespace, if any, in ROW. */
14901 void
14902 highlight_trailing_whitespace (f, row)
14903 struct frame *f;
14904 struct glyph_row *row;
14906 int used = row->used[TEXT_AREA];
14908 if (used)
14910 struct glyph *start = row->glyphs[TEXT_AREA];
14911 struct glyph *glyph = start + used - 1;
14913 /* Skip over glyphs inserted to display the cursor at the
14914 end of a line, for extending the face of the last glyph
14915 to the end of the line on terminals, and for truncation
14916 and continuation glyphs. */
14917 while (glyph >= start
14918 && glyph->type == CHAR_GLYPH
14919 && INTEGERP (glyph->object))
14920 --glyph;
14922 /* If last glyph is a space or stretch, and it's trailing
14923 whitespace, set the face of all trailing whitespace glyphs in
14924 IT->glyph_row to `trailing-whitespace'. */
14925 if (glyph >= start
14926 && BUFFERP (glyph->object)
14927 && (glyph->type == STRETCH_GLYPH
14928 || (glyph->type == CHAR_GLYPH
14929 && glyph->u.ch == ' '))
14930 && trailing_whitespace_p (glyph->charpos))
14932 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14933 if (face_id < 0)
14934 return;
14936 while (glyph >= start
14937 && BUFFERP (glyph->object)
14938 && (glyph->type == STRETCH_GLYPH
14939 || (glyph->type == CHAR_GLYPH
14940 && glyph->u.ch == ' ')))
14941 (glyph--)->face_id = face_id;
14947 /* Value is non-zero if glyph row ROW in window W should be
14948 used to hold the cursor. */
14950 static int
14951 cursor_row_p (w, row)
14952 struct window *w;
14953 struct glyph_row *row;
14955 int cursor_row_p = 1;
14957 if (PT == MATRIX_ROW_END_CHARPOS (row))
14959 /* If the row ends with a newline from a string, we don't want
14960 the cursor there (if the row is continued it doesn't end in a
14961 newline). */
14962 if (CHARPOS (row->end.string_pos) >= 0)
14963 cursor_row_p = row->continued_p;
14964 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14966 /* If the row ends in middle of a real character,
14967 and the line is continued, we want the cursor here.
14968 That's because MATRIX_ROW_END_CHARPOS would equal
14969 PT if PT is before the character. */
14970 if (!row->ends_in_ellipsis_p)
14971 cursor_row_p = row->continued_p;
14972 else
14973 /* If the row ends in an ellipsis, then
14974 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14975 We want that position to be displayed after the ellipsis. */
14976 cursor_row_p = 0;
14978 /* If the row ends at ZV, display the cursor at the end of that
14979 row instead of at the start of the row below. */
14980 else if (row->ends_at_zv_p)
14981 cursor_row_p = 1;
14982 else
14983 cursor_row_p = 0;
14986 return cursor_row_p;
14990 /* Construct the glyph row IT->glyph_row in the desired matrix of
14991 IT->w from text at the current position of IT. See dispextern.h
14992 for an overview of struct it. Value is non-zero if
14993 IT->glyph_row displays text, as opposed to a line displaying ZV
14994 only. */
14996 static int
14997 display_line (it)
14998 struct it *it;
15000 struct glyph_row *row = it->glyph_row;
15001 Lisp_Object overlay_arrow_string;
15003 /* We always start displaying at hpos zero even if hscrolled. */
15004 xassert (it->hpos == 0 && it->current_x == 0);
15006 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15007 >= it->w->desired_matrix->nrows)
15009 it->w->nrows_scale_factor++;
15010 fonts_changed_p = 1;
15011 return 0;
15014 /* Is IT->w showing the region? */
15015 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15017 /* Clear the result glyph row and enable it. */
15018 prepare_desired_row (row);
15020 row->y = it->current_y;
15021 row->start = it->start;
15022 row->continuation_lines_width = it->continuation_lines_width;
15023 row->displays_text_p = 1;
15024 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15025 it->starts_in_middle_of_char_p = 0;
15027 /* Arrange the overlays nicely for our purposes. Usually, we call
15028 display_line on only one line at a time, in which case this
15029 can't really hurt too much, or we call it on lines which appear
15030 one after another in the buffer, in which case all calls to
15031 recenter_overlay_lists but the first will be pretty cheap. */
15032 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15034 /* Move over display elements that are not visible because we are
15035 hscrolled. This may stop at an x-position < IT->first_visible_x
15036 if the first glyph is partially visible or if we hit a line end. */
15037 if (it->current_x < it->first_visible_x)
15039 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15040 MOVE_TO_POS | MOVE_TO_X);
15043 /* Get the initial row height. This is either the height of the
15044 text hscrolled, if there is any, or zero. */
15045 row->ascent = it->max_ascent;
15046 row->height = it->max_ascent + it->max_descent;
15047 row->phys_ascent = it->max_phys_ascent;
15048 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15049 row->extra_line_spacing = it->max_extra_line_spacing;
15051 /* Loop generating characters. The loop is left with IT on the next
15052 character to display. */
15053 while (1)
15055 int n_glyphs_before, hpos_before, x_before;
15056 int x, i, nglyphs;
15057 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15059 /* Retrieve the next thing to display. Value is zero if end of
15060 buffer reached. */
15061 if (!get_next_display_element (it))
15063 /* Maybe add a space at the end of this line that is used to
15064 display the cursor there under X. Set the charpos of the
15065 first glyph of blank lines not corresponding to any text
15066 to -1. */
15067 #ifdef HAVE_WINDOW_SYSTEM
15068 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15069 row->exact_window_width_line_p = 1;
15070 else
15071 #endif /* HAVE_WINDOW_SYSTEM */
15072 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15073 || row->used[TEXT_AREA] == 0)
15075 row->glyphs[TEXT_AREA]->charpos = -1;
15076 row->displays_text_p = 0;
15078 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15079 && (!MINI_WINDOW_P (it->w)
15080 || (minibuf_level && EQ (it->window, minibuf_window))))
15081 row->indicate_empty_line_p = 1;
15084 it->continuation_lines_width = 0;
15085 row->ends_at_zv_p = 1;
15086 break;
15089 /* Now, get the metrics of what we want to display. This also
15090 generates glyphs in `row' (which is IT->glyph_row). */
15091 n_glyphs_before = row->used[TEXT_AREA];
15092 x = it->current_x;
15094 /* Remember the line height so far in case the next element doesn't
15095 fit on the line. */
15096 if (!it->truncate_lines_p)
15098 ascent = it->max_ascent;
15099 descent = it->max_descent;
15100 phys_ascent = it->max_phys_ascent;
15101 phys_descent = it->max_phys_descent;
15104 PRODUCE_GLYPHS (it);
15106 /* If this display element was in marginal areas, continue with
15107 the next one. */
15108 if (it->area != TEXT_AREA)
15110 row->ascent = max (row->ascent, it->max_ascent);
15111 row->height = max (row->height, it->max_ascent + it->max_descent);
15112 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15113 row->phys_height = max (row->phys_height,
15114 it->max_phys_ascent + it->max_phys_descent);
15115 row->extra_line_spacing = max (row->extra_line_spacing,
15116 it->max_extra_line_spacing);
15117 set_iterator_to_next (it, 1);
15118 continue;
15121 /* Does the display element fit on the line? If we truncate
15122 lines, we should draw past the right edge of the window. If
15123 we don't truncate, we want to stop so that we can display the
15124 continuation glyph before the right margin. If lines are
15125 continued, there are two possible strategies for characters
15126 resulting in more than 1 glyph (e.g. tabs): Display as many
15127 glyphs as possible in this line and leave the rest for the
15128 continuation line, or display the whole element in the next
15129 line. Original redisplay did the former, so we do it also. */
15130 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15131 hpos_before = it->hpos;
15132 x_before = x;
15134 if (/* Not a newline. */
15135 nglyphs > 0
15136 /* Glyphs produced fit entirely in the line. */
15137 && it->current_x < it->last_visible_x)
15139 it->hpos += nglyphs;
15140 row->ascent = max (row->ascent, it->max_ascent);
15141 row->height = max (row->height, it->max_ascent + it->max_descent);
15142 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15143 row->phys_height = max (row->phys_height,
15144 it->max_phys_ascent + it->max_phys_descent);
15145 row->extra_line_spacing = max (row->extra_line_spacing,
15146 it->max_extra_line_spacing);
15147 if (it->current_x - it->pixel_width < it->first_visible_x)
15148 row->x = x - it->first_visible_x;
15150 else
15152 int new_x;
15153 struct glyph *glyph;
15155 for (i = 0; i < nglyphs; ++i, x = new_x)
15157 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15158 new_x = x + glyph->pixel_width;
15160 if (/* Lines are continued. */
15161 !it->truncate_lines_p
15162 && (/* Glyph doesn't fit on the line. */
15163 new_x > it->last_visible_x
15164 /* Or it fits exactly on a window system frame. */
15165 || (new_x == it->last_visible_x
15166 && FRAME_WINDOW_P (it->f))))
15168 /* End of a continued line. */
15170 if (it->hpos == 0
15171 || (new_x == it->last_visible_x
15172 && FRAME_WINDOW_P (it->f)))
15174 /* Current glyph is the only one on the line or
15175 fits exactly on the line. We must continue
15176 the line because we can't draw the cursor
15177 after the glyph. */
15178 row->continued_p = 1;
15179 it->current_x = new_x;
15180 it->continuation_lines_width += new_x;
15181 ++it->hpos;
15182 if (i == nglyphs - 1)
15184 set_iterator_to_next (it, 1);
15185 #ifdef HAVE_WINDOW_SYSTEM
15186 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15188 if (!get_next_display_element (it))
15190 row->exact_window_width_line_p = 1;
15191 it->continuation_lines_width = 0;
15192 row->continued_p = 0;
15193 row->ends_at_zv_p = 1;
15195 else if (ITERATOR_AT_END_OF_LINE_P (it))
15197 row->continued_p = 0;
15198 row->exact_window_width_line_p = 1;
15201 #endif /* HAVE_WINDOW_SYSTEM */
15204 else if (CHAR_GLYPH_PADDING_P (*glyph)
15205 && !FRAME_WINDOW_P (it->f))
15207 /* A padding glyph that doesn't fit on this line.
15208 This means the whole character doesn't fit
15209 on the line. */
15210 row->used[TEXT_AREA] = n_glyphs_before;
15212 /* Fill the rest of the row with continuation
15213 glyphs like in 20.x. */
15214 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15215 < row->glyphs[1 + TEXT_AREA])
15216 produce_special_glyphs (it, IT_CONTINUATION);
15218 row->continued_p = 1;
15219 it->current_x = x_before;
15220 it->continuation_lines_width += x_before;
15222 /* Restore the height to what it was before the
15223 element not fitting on the line. */
15224 it->max_ascent = ascent;
15225 it->max_descent = descent;
15226 it->max_phys_ascent = phys_ascent;
15227 it->max_phys_descent = phys_descent;
15229 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15231 /* A TAB that extends past the right edge of the
15232 window. This produces a single glyph on
15233 window system frames. We leave the glyph in
15234 this row and let it fill the row, but don't
15235 consume the TAB. */
15236 it->continuation_lines_width += it->last_visible_x;
15237 row->ends_in_middle_of_char_p = 1;
15238 row->continued_p = 1;
15239 glyph->pixel_width = it->last_visible_x - x;
15240 it->starts_in_middle_of_char_p = 1;
15242 else
15244 /* Something other than a TAB that draws past
15245 the right edge of the window. Restore
15246 positions to values before the element. */
15247 row->used[TEXT_AREA] = n_glyphs_before + i;
15249 /* Display continuation glyphs. */
15250 if (!FRAME_WINDOW_P (it->f))
15251 produce_special_glyphs (it, IT_CONTINUATION);
15252 row->continued_p = 1;
15254 it->continuation_lines_width += x;
15256 if (nglyphs > 1 && i > 0)
15258 row->ends_in_middle_of_char_p = 1;
15259 it->starts_in_middle_of_char_p = 1;
15262 /* Restore the height to what it was before the
15263 element not fitting on the line. */
15264 it->max_ascent = ascent;
15265 it->max_descent = descent;
15266 it->max_phys_ascent = phys_ascent;
15267 it->max_phys_descent = phys_descent;
15270 break;
15272 else if (new_x > it->first_visible_x)
15274 /* Increment number of glyphs actually displayed. */
15275 ++it->hpos;
15277 if (x < it->first_visible_x)
15278 /* Glyph is partially visible, i.e. row starts at
15279 negative X position. */
15280 row->x = x - it->first_visible_x;
15282 else
15284 /* Glyph is completely off the left margin of the
15285 window. This should not happen because of the
15286 move_it_in_display_line at the start of this
15287 function, unless the text display area of the
15288 window is empty. */
15289 xassert (it->first_visible_x <= it->last_visible_x);
15293 row->ascent = max (row->ascent, it->max_ascent);
15294 row->height = max (row->height, it->max_ascent + it->max_descent);
15295 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15296 row->phys_height = max (row->phys_height,
15297 it->max_phys_ascent + it->max_phys_descent);
15298 row->extra_line_spacing = max (row->extra_line_spacing,
15299 it->max_extra_line_spacing);
15301 /* End of this display line if row is continued. */
15302 if (row->continued_p || row->ends_at_zv_p)
15303 break;
15306 at_end_of_line:
15307 /* Is this a line end? If yes, we're also done, after making
15308 sure that a non-default face is extended up to the right
15309 margin of the window. */
15310 if (ITERATOR_AT_END_OF_LINE_P (it))
15312 int used_before = row->used[TEXT_AREA];
15314 row->ends_in_newline_from_string_p = STRINGP (it->object);
15316 #ifdef HAVE_WINDOW_SYSTEM
15317 /* Add a space at the end of the line that is used to
15318 display the cursor there. */
15319 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15320 append_space_for_newline (it, 0);
15321 #endif /* HAVE_WINDOW_SYSTEM */
15323 /* Extend the face to the end of the line. */
15324 extend_face_to_end_of_line (it);
15326 /* Make sure we have the position. */
15327 if (used_before == 0)
15328 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15330 /* Consume the line end. This skips over invisible lines. */
15331 set_iterator_to_next (it, 1);
15332 it->continuation_lines_width = 0;
15333 break;
15336 /* Proceed with next display element. Note that this skips
15337 over lines invisible because of selective display. */
15338 set_iterator_to_next (it, 1);
15340 /* If we truncate lines, we are done when the last displayed
15341 glyphs reach past the right margin of the window. */
15342 if (it->truncate_lines_p
15343 && (FRAME_WINDOW_P (it->f)
15344 ? (it->current_x >= it->last_visible_x)
15345 : (it->current_x > it->last_visible_x)))
15347 /* Maybe add truncation glyphs. */
15348 if (!FRAME_WINDOW_P (it->f))
15350 int i, n;
15352 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15353 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15354 break;
15356 for (n = row->used[TEXT_AREA]; i < n; ++i)
15358 row->used[TEXT_AREA] = i;
15359 produce_special_glyphs (it, IT_TRUNCATION);
15362 #ifdef HAVE_WINDOW_SYSTEM
15363 else
15365 /* Don't truncate if we can overflow newline into fringe. */
15366 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15368 if (!get_next_display_element (it))
15370 it->continuation_lines_width = 0;
15371 row->ends_at_zv_p = 1;
15372 row->exact_window_width_line_p = 1;
15373 break;
15375 if (ITERATOR_AT_END_OF_LINE_P (it))
15377 row->exact_window_width_line_p = 1;
15378 goto at_end_of_line;
15382 #endif /* HAVE_WINDOW_SYSTEM */
15384 row->truncated_on_right_p = 1;
15385 it->continuation_lines_width = 0;
15386 reseat_at_next_visible_line_start (it, 0);
15387 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15388 it->hpos = hpos_before;
15389 it->current_x = x_before;
15390 break;
15394 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15395 at the left window margin. */
15396 if (it->first_visible_x
15397 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15399 if (!FRAME_WINDOW_P (it->f))
15400 insert_left_trunc_glyphs (it);
15401 row->truncated_on_left_p = 1;
15404 /* If the start of this line is the overlay arrow-position, then
15405 mark this glyph row as the one containing the overlay arrow.
15406 This is clearly a mess with variable size fonts. It would be
15407 better to let it be displayed like cursors under X. */
15408 if ((row->displays_text_p || !overlay_arrow_seen)
15409 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15410 !NILP (overlay_arrow_string)))
15412 /* Overlay arrow in window redisplay is a fringe bitmap. */
15413 if (STRINGP (overlay_arrow_string))
15415 struct glyph_row *arrow_row
15416 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15417 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15418 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15419 struct glyph *p = row->glyphs[TEXT_AREA];
15420 struct glyph *p2, *end;
15422 /* Copy the arrow glyphs. */
15423 while (glyph < arrow_end)
15424 *p++ = *glyph++;
15426 /* Throw away padding glyphs. */
15427 p2 = p;
15428 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15429 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15430 ++p2;
15431 if (p2 > p)
15433 while (p2 < end)
15434 *p++ = *p2++;
15435 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15438 else
15440 xassert (INTEGERP (overlay_arrow_string));
15441 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15443 overlay_arrow_seen = 1;
15446 /* Compute pixel dimensions of this line. */
15447 compute_line_metrics (it);
15449 /* Remember the position at which this line ends. */
15450 row->end = it->current;
15452 /* Record whether this row ends inside an ellipsis. */
15453 row->ends_in_ellipsis_p
15454 = (it->method == GET_FROM_DISPLAY_VECTOR
15455 && it->ellipsis_p);
15457 /* Save fringe bitmaps in this row. */
15458 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15459 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15460 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15461 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15463 it->left_user_fringe_bitmap = 0;
15464 it->left_user_fringe_face_id = 0;
15465 it->right_user_fringe_bitmap = 0;
15466 it->right_user_fringe_face_id = 0;
15468 /* Maybe set the cursor. */
15469 if (it->w->cursor.vpos < 0
15470 && PT >= MATRIX_ROW_START_CHARPOS (row)
15471 && PT <= MATRIX_ROW_END_CHARPOS (row)
15472 && cursor_row_p (it->w, row))
15473 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15475 /* Highlight trailing whitespace. */
15476 if (!NILP (Vshow_trailing_whitespace))
15477 highlight_trailing_whitespace (it->f, it->glyph_row);
15479 /* Prepare for the next line. This line starts horizontally at (X
15480 HPOS) = (0 0). Vertical positions are incremented. As a
15481 convenience for the caller, IT->glyph_row is set to the next
15482 row to be used. */
15483 it->current_x = it->hpos = 0;
15484 it->current_y += row->height;
15485 ++it->vpos;
15486 ++it->glyph_row;
15487 it->start = it->current;
15488 return row->displays_text_p;
15493 /***********************************************************************
15494 Menu Bar
15495 ***********************************************************************/
15497 /* Redisplay the menu bar in the frame for window W.
15499 The menu bar of X frames that don't have X toolkit support is
15500 displayed in a special window W->frame->menu_bar_window.
15502 The menu bar of terminal frames is treated specially as far as
15503 glyph matrices are concerned. Menu bar lines are not part of
15504 windows, so the update is done directly on the frame matrix rows
15505 for the menu bar. */
15507 static void
15508 display_menu_bar (w)
15509 struct window *w;
15511 struct frame *f = XFRAME (WINDOW_FRAME (w));
15512 struct it it;
15513 Lisp_Object items;
15514 int i;
15516 /* Don't do all this for graphical frames. */
15517 #ifdef HAVE_NTGUI
15518 if (!NILP (Vwindow_system))
15519 return;
15520 #endif
15521 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15522 if (FRAME_X_P (f))
15523 return;
15524 #endif
15525 #ifdef MAC_OS
15526 if (FRAME_MAC_P (f))
15527 return;
15528 #endif
15530 #ifdef USE_X_TOOLKIT
15531 xassert (!FRAME_WINDOW_P (f));
15532 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15533 it.first_visible_x = 0;
15534 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15535 #else /* not USE_X_TOOLKIT */
15536 if (FRAME_WINDOW_P (f))
15538 /* Menu bar lines are displayed in the desired matrix of the
15539 dummy window menu_bar_window. */
15540 struct window *menu_w;
15541 xassert (WINDOWP (f->menu_bar_window));
15542 menu_w = XWINDOW (f->menu_bar_window);
15543 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15544 MENU_FACE_ID);
15545 it.first_visible_x = 0;
15546 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15548 else
15550 /* This is a TTY frame, i.e. character hpos/vpos are used as
15551 pixel x/y. */
15552 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15553 MENU_FACE_ID);
15554 it.first_visible_x = 0;
15555 it.last_visible_x = FRAME_COLS (f);
15557 #endif /* not USE_X_TOOLKIT */
15559 if (! mode_line_inverse_video)
15560 /* Force the menu-bar to be displayed in the default face. */
15561 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15563 /* Clear all rows of the menu bar. */
15564 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15566 struct glyph_row *row = it.glyph_row + i;
15567 clear_glyph_row (row);
15568 row->enabled_p = 1;
15569 row->full_width_p = 1;
15572 /* Display all items of the menu bar. */
15573 items = FRAME_MENU_BAR_ITEMS (it.f);
15574 for (i = 0; i < XVECTOR (items)->size; i += 4)
15576 Lisp_Object string;
15578 /* Stop at nil string. */
15579 string = AREF (items, i + 1);
15580 if (NILP (string))
15581 break;
15583 /* Remember where item was displayed. */
15584 AREF (items, i + 3) = make_number (it.hpos);
15586 /* Display the item, pad with one space. */
15587 if (it.current_x < it.last_visible_x)
15588 display_string (NULL, string, Qnil, 0, 0, &it,
15589 SCHARS (string) + 1, 0, 0, -1);
15592 /* Fill out the line with spaces. */
15593 if (it.current_x < it.last_visible_x)
15594 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15596 /* Compute the total height of the lines. */
15597 compute_line_metrics (&it);
15602 /***********************************************************************
15603 Mode Line
15604 ***********************************************************************/
15606 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15607 FORCE is non-zero, redisplay mode lines unconditionally.
15608 Otherwise, redisplay only mode lines that are garbaged. Value is
15609 the number of windows whose mode lines were redisplayed. */
15611 static int
15612 redisplay_mode_lines (window, force)
15613 Lisp_Object window;
15614 int force;
15616 int nwindows = 0;
15618 while (!NILP (window))
15620 struct window *w = XWINDOW (window);
15622 if (WINDOWP (w->hchild))
15623 nwindows += redisplay_mode_lines (w->hchild, force);
15624 else if (WINDOWP (w->vchild))
15625 nwindows += redisplay_mode_lines (w->vchild, force);
15626 else if (force
15627 || FRAME_GARBAGED_P (XFRAME (w->frame))
15628 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15630 struct text_pos lpoint;
15631 struct buffer *old = current_buffer;
15633 /* Set the window's buffer for the mode line display. */
15634 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15635 set_buffer_internal_1 (XBUFFER (w->buffer));
15637 /* Point refers normally to the selected window. For any
15638 other window, set up appropriate value. */
15639 if (!EQ (window, selected_window))
15641 struct text_pos pt;
15643 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15644 if (CHARPOS (pt) < BEGV)
15645 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15646 else if (CHARPOS (pt) > (ZV - 1))
15647 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15648 else
15649 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15652 /* Display mode lines. */
15653 clear_glyph_matrix (w->desired_matrix);
15654 if (display_mode_lines (w))
15656 ++nwindows;
15657 w->must_be_updated_p = 1;
15660 /* Restore old settings. */
15661 set_buffer_internal_1 (old);
15662 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15665 window = w->next;
15668 return nwindows;
15672 /* Display the mode and/or top line of window W. Value is the number
15673 of mode lines displayed. */
15675 static int
15676 display_mode_lines (w)
15677 struct window *w;
15679 Lisp_Object old_selected_window, old_selected_frame;
15680 int n = 0;
15682 old_selected_frame = selected_frame;
15683 selected_frame = w->frame;
15684 old_selected_window = selected_window;
15685 XSETWINDOW (selected_window, w);
15687 /* These will be set while the mode line specs are processed. */
15688 line_number_displayed = 0;
15689 w->column_number_displayed = Qnil;
15691 if (WINDOW_WANTS_MODELINE_P (w))
15693 struct window *sel_w = XWINDOW (old_selected_window);
15695 /* Select mode line face based on the real selected window. */
15696 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15697 current_buffer->mode_line_format);
15698 ++n;
15701 if (WINDOW_WANTS_HEADER_LINE_P (w))
15703 display_mode_line (w, HEADER_LINE_FACE_ID,
15704 current_buffer->header_line_format);
15705 ++n;
15708 selected_frame = old_selected_frame;
15709 selected_window = old_selected_window;
15710 return n;
15714 /* Display mode or top line of window W. FACE_ID specifies which line
15715 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15716 FORMAT is the mode line format to display. Value is the pixel
15717 height of the mode line displayed. */
15719 static int
15720 display_mode_line (w, face_id, format)
15721 struct window *w;
15722 enum face_id face_id;
15723 Lisp_Object format;
15725 struct it it;
15726 struct face *face;
15727 int count = SPECPDL_INDEX ();
15729 init_iterator (&it, w, -1, -1, NULL, face_id);
15730 prepare_desired_row (it.glyph_row);
15732 it.glyph_row->mode_line_p = 1;
15734 if (! mode_line_inverse_video)
15735 /* Force the mode-line to be displayed in the default face. */
15736 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15738 record_unwind_protect (unwind_format_mode_line,
15739 format_mode_line_unwind_data (NULL));
15741 mode_line_target = MODE_LINE_DISPLAY;
15743 /* Temporarily make frame's keyboard the current kboard so that
15744 kboard-local variables in the mode_line_format will get the right
15745 values. */
15746 push_frame_kboard (it.f);
15747 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15748 pop_frame_kboard ();
15750 unbind_to (count, Qnil);
15752 /* Fill up with spaces. */
15753 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15755 compute_line_metrics (&it);
15756 it.glyph_row->full_width_p = 1;
15757 it.glyph_row->continued_p = 0;
15758 it.glyph_row->truncated_on_left_p = 0;
15759 it.glyph_row->truncated_on_right_p = 0;
15761 /* Make a 3D mode-line have a shadow at its right end. */
15762 face = FACE_FROM_ID (it.f, face_id);
15763 extend_face_to_end_of_line (&it);
15764 if (face->box != FACE_NO_BOX)
15766 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15767 + it.glyph_row->used[TEXT_AREA] - 1);
15768 last->right_box_line_p = 1;
15771 return it.glyph_row->height;
15774 /* Contribute ELT to the mode line for window IT->w. How it
15775 translates into text depends on its data type.
15777 IT describes the display environment in which we display, as usual.
15779 DEPTH is the depth in recursion. It is used to prevent
15780 infinite recursion here.
15782 FIELD_WIDTH is the number of characters the display of ELT should
15783 occupy in the mode line, and PRECISION is the maximum number of
15784 characters to display from ELT's representation. See
15785 display_string for details.
15787 Returns the hpos of the end of the text generated by ELT.
15789 PROPS is a property list to add to any string we encounter.
15791 If RISKY is nonzero, remove (disregard) any properties in any string
15792 we encounter, and ignore :eval and :propertize.
15794 The global variable `mode_line_target' determines whether the
15795 output is passed to `store_mode_line_noprop',
15796 `store_mode_line_string', or `display_string'. */
15798 static int
15799 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15800 struct it *it;
15801 int depth;
15802 int field_width, precision;
15803 Lisp_Object elt, props;
15804 int risky;
15806 int n = 0, field, prec;
15807 int literal = 0;
15809 tail_recurse:
15810 if (depth > 100)
15811 elt = build_string ("*too-deep*");
15813 depth++;
15815 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15817 case Lisp_String:
15819 /* A string: output it and check for %-constructs within it. */
15820 unsigned char c;
15821 const unsigned char *this, *lisp_string;
15823 if (!NILP (props) || risky)
15825 Lisp_Object oprops, aelt;
15826 oprops = Ftext_properties_at (make_number (0), elt);
15828 /* If the starting string's properties are not what
15829 we want, translate the string. Also, if the string
15830 is risky, do that anyway. */
15832 if (NILP (Fequal (props, oprops)) || risky)
15834 /* If the starting string has properties,
15835 merge the specified ones onto the existing ones. */
15836 if (! NILP (oprops) && !risky)
15838 Lisp_Object tem;
15840 oprops = Fcopy_sequence (oprops);
15841 tem = props;
15842 while (CONSP (tem))
15844 oprops = Fplist_put (oprops, XCAR (tem),
15845 XCAR (XCDR (tem)));
15846 tem = XCDR (XCDR (tem));
15848 props = oprops;
15851 aelt = Fassoc (elt, mode_line_proptrans_alist);
15852 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15854 mode_line_proptrans_alist
15855 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15856 elt = XCAR (aelt);
15858 else
15860 Lisp_Object tem;
15862 elt = Fcopy_sequence (elt);
15863 Fset_text_properties (make_number (0), Flength (elt),
15864 props, elt);
15865 /* Add this item to mode_line_proptrans_alist. */
15866 mode_line_proptrans_alist
15867 = Fcons (Fcons (elt, props),
15868 mode_line_proptrans_alist);
15869 /* Truncate mode_line_proptrans_alist
15870 to at most 50 elements. */
15871 tem = Fnthcdr (make_number (50),
15872 mode_line_proptrans_alist);
15873 if (! NILP (tem))
15874 XSETCDR (tem, Qnil);
15879 this = SDATA (elt);
15880 lisp_string = this;
15882 if (literal)
15884 prec = precision - n;
15885 switch (mode_line_target)
15887 case MODE_LINE_NOPROP:
15888 case MODE_LINE_TITLE:
15889 n += store_mode_line_noprop (SDATA (elt), -1, prec);
15890 break;
15891 case MODE_LINE_STRING:
15892 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15893 break;
15894 case MODE_LINE_DISPLAY:
15895 n += display_string (NULL, elt, Qnil, 0, 0, it,
15896 0, prec, 0, STRING_MULTIBYTE (elt));
15897 break;
15900 break;
15903 while ((precision <= 0 || n < precision)
15904 && *this
15905 && (mode_line_target != MODE_LINE_DISPLAY
15906 || it->current_x < it->last_visible_x))
15908 const unsigned char *last = this;
15910 /* Advance to end of string or next format specifier. */
15911 while ((c = *this++) != '\0' && c != '%')
15914 if (this - 1 != last)
15916 int nchars, nbytes;
15918 /* Output to end of string or up to '%'. Field width
15919 is length of string. Don't output more than
15920 PRECISION allows us. */
15921 --this;
15923 prec = c_string_width (last, this - last, precision - n,
15924 &nchars, &nbytes);
15926 switch (mode_line_target)
15928 case MODE_LINE_NOPROP:
15929 case MODE_LINE_TITLE:
15930 n += store_mode_line_noprop (last, 0, prec);
15931 break;
15932 case MODE_LINE_STRING:
15934 int bytepos = last - lisp_string;
15935 int charpos = string_byte_to_char (elt, bytepos);
15936 int endpos = (precision <= 0
15937 ? string_byte_to_char (elt,
15938 this - lisp_string)
15939 : charpos + nchars);
15941 n += store_mode_line_string (NULL,
15942 Fsubstring (elt, make_number (charpos),
15943 make_number (endpos)),
15944 0, 0, 0, Qnil);
15946 break;
15947 case MODE_LINE_DISPLAY:
15949 int bytepos = last - lisp_string;
15950 int charpos = string_byte_to_char (elt, bytepos);
15951 n += display_string (NULL, elt, Qnil, 0, charpos,
15952 it, 0, prec, 0,
15953 STRING_MULTIBYTE (elt));
15955 break;
15958 else /* c == '%' */
15960 const unsigned char *percent_position = this;
15962 /* Get the specified minimum width. Zero means
15963 don't pad. */
15964 field = 0;
15965 while ((c = *this++) >= '0' && c <= '9')
15966 field = field * 10 + c - '0';
15968 /* Don't pad beyond the total padding allowed. */
15969 if (field_width - n > 0 && field > field_width - n)
15970 field = field_width - n;
15972 /* Note that either PRECISION <= 0 or N < PRECISION. */
15973 prec = precision - n;
15975 if (c == 'M')
15976 n += display_mode_element (it, depth, field, prec,
15977 Vglobal_mode_string, props,
15978 risky);
15979 else if (c != 0)
15981 int multibyte;
15982 int bytepos, charpos;
15983 unsigned char *spec;
15985 bytepos = percent_position - lisp_string;
15986 charpos = (STRING_MULTIBYTE (elt)
15987 ? string_byte_to_char (elt, bytepos)
15988 : bytepos);
15990 spec
15991 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15993 switch (mode_line_target)
15995 case MODE_LINE_NOPROP:
15996 case MODE_LINE_TITLE:
15997 n += store_mode_line_noprop (spec, field, prec);
15998 break;
15999 case MODE_LINE_STRING:
16001 int len = strlen (spec);
16002 Lisp_Object tem = make_string (spec, len);
16003 props = Ftext_properties_at (make_number (charpos), elt);
16004 /* Should only keep face property in props */
16005 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16007 break;
16008 case MODE_LINE_DISPLAY:
16010 int nglyphs_before, nwritten;
16012 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16013 nwritten = display_string (spec, Qnil, elt,
16014 charpos, 0, it,
16015 field, prec, 0,
16016 multibyte);
16018 /* Assign to the glyphs written above the
16019 string where the `%x' came from, position
16020 of the `%'. */
16021 if (nwritten > 0)
16023 struct glyph *glyph
16024 = (it->glyph_row->glyphs[TEXT_AREA]
16025 + nglyphs_before);
16026 int i;
16028 for (i = 0; i < nwritten; ++i)
16030 glyph[i].object = elt;
16031 glyph[i].charpos = charpos;
16034 n += nwritten;
16037 break;
16040 else /* c == 0 */
16041 break;
16045 break;
16047 case Lisp_Symbol:
16048 /* A symbol: process the value of the symbol recursively
16049 as if it appeared here directly. Avoid error if symbol void.
16050 Special case: if value of symbol is a string, output the string
16051 literally. */
16053 register Lisp_Object tem;
16055 /* If the variable is not marked as risky to set
16056 then its contents are risky to use. */
16057 if (NILP (Fget (elt, Qrisky_local_variable)))
16058 risky = 1;
16060 tem = Fboundp (elt);
16061 if (!NILP (tem))
16063 tem = Fsymbol_value (elt);
16064 /* If value is a string, output that string literally:
16065 don't check for % within it. */
16066 if (STRINGP (tem))
16067 literal = 1;
16069 if (!EQ (tem, elt))
16071 /* Give up right away for nil or t. */
16072 elt = tem;
16073 goto tail_recurse;
16077 break;
16079 case Lisp_Cons:
16081 register Lisp_Object car, tem;
16083 /* A cons cell: five distinct cases.
16084 If first element is :eval or :propertize, do something special.
16085 If first element is a string or a cons, process all the elements
16086 and effectively concatenate them.
16087 If first element is a negative number, truncate displaying cdr to
16088 at most that many characters. If positive, pad (with spaces)
16089 to at least that many characters.
16090 If first element is a symbol, process the cadr or caddr recursively
16091 according to whether the symbol's value is non-nil or nil. */
16092 car = XCAR (elt);
16093 if (EQ (car, QCeval))
16095 /* An element of the form (:eval FORM) means evaluate FORM
16096 and use the result as mode line elements. */
16098 if (risky)
16099 break;
16101 if (CONSP (XCDR (elt)))
16103 Lisp_Object spec;
16104 spec = safe_eval (XCAR (XCDR (elt)));
16105 n += display_mode_element (it, depth, field_width - n,
16106 precision - n, spec, props,
16107 risky);
16110 else if (EQ (car, QCpropertize))
16112 /* An element of the form (:propertize ELT PROPS...)
16113 means display ELT but applying properties PROPS. */
16115 if (risky)
16116 break;
16118 if (CONSP (XCDR (elt)))
16119 n += display_mode_element (it, depth, field_width - n,
16120 precision - n, XCAR (XCDR (elt)),
16121 XCDR (XCDR (elt)), risky);
16123 else if (SYMBOLP (car))
16125 tem = Fboundp (car);
16126 elt = XCDR (elt);
16127 if (!CONSP (elt))
16128 goto invalid;
16129 /* elt is now the cdr, and we know it is a cons cell.
16130 Use its car if CAR has a non-nil value. */
16131 if (!NILP (tem))
16133 tem = Fsymbol_value (car);
16134 if (!NILP (tem))
16136 elt = XCAR (elt);
16137 goto tail_recurse;
16140 /* Symbol's value is nil (or symbol is unbound)
16141 Get the cddr of the original list
16142 and if possible find the caddr and use that. */
16143 elt = XCDR (elt);
16144 if (NILP (elt))
16145 break;
16146 else if (!CONSP (elt))
16147 goto invalid;
16148 elt = XCAR (elt);
16149 goto tail_recurse;
16151 else if (INTEGERP (car))
16153 register int lim = XINT (car);
16154 elt = XCDR (elt);
16155 if (lim < 0)
16157 /* Negative int means reduce maximum width. */
16158 if (precision <= 0)
16159 precision = -lim;
16160 else
16161 precision = min (precision, -lim);
16163 else if (lim > 0)
16165 /* Padding specified. Don't let it be more than
16166 current maximum. */
16167 if (precision > 0)
16168 lim = min (precision, lim);
16170 /* If that's more padding than already wanted, queue it.
16171 But don't reduce padding already specified even if
16172 that is beyond the current truncation point. */
16173 field_width = max (lim, field_width);
16175 goto tail_recurse;
16177 else if (STRINGP (car) || CONSP (car))
16179 register int limit = 50;
16180 /* Limit is to protect against circular lists. */
16181 while (CONSP (elt)
16182 && --limit > 0
16183 && (precision <= 0 || n < precision))
16185 n += display_mode_element (it, depth,
16186 /* Do padding only after the last
16187 element in the list. */
16188 (! CONSP (XCDR (elt))
16189 ? field_width - n
16190 : 0),
16191 precision - n, XCAR (elt),
16192 props, risky);
16193 elt = XCDR (elt);
16197 break;
16199 default:
16200 invalid:
16201 elt = build_string ("*invalid*");
16202 goto tail_recurse;
16205 /* Pad to FIELD_WIDTH. */
16206 if (field_width > 0 && n < field_width)
16208 switch (mode_line_target)
16210 case MODE_LINE_NOPROP:
16211 case MODE_LINE_TITLE:
16212 n += store_mode_line_noprop ("", field_width - n, 0);
16213 break;
16214 case MODE_LINE_STRING:
16215 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16216 break;
16217 case MODE_LINE_DISPLAY:
16218 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16219 0, 0, 0);
16220 break;
16224 return n;
16227 /* Store a mode-line string element in mode_line_string_list.
16229 If STRING is non-null, display that C string. Otherwise, the Lisp
16230 string LISP_STRING is displayed.
16232 FIELD_WIDTH is the minimum number of output glyphs to produce.
16233 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16234 with spaces. FIELD_WIDTH <= 0 means don't pad.
16236 PRECISION is the maximum number of characters to output from
16237 STRING. PRECISION <= 0 means don't truncate the string.
16239 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16240 properties to the string.
16242 PROPS are the properties to add to the string.
16243 The mode_line_string_face face property is always added to the string.
16246 static int
16247 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16248 char *string;
16249 Lisp_Object lisp_string;
16250 int copy_string;
16251 int field_width;
16252 int precision;
16253 Lisp_Object props;
16255 int len;
16256 int n = 0;
16258 if (string != NULL)
16260 len = strlen (string);
16261 if (precision > 0 && len > precision)
16262 len = precision;
16263 lisp_string = make_string (string, len);
16264 if (NILP (props))
16265 props = mode_line_string_face_prop;
16266 else if (!NILP (mode_line_string_face))
16268 Lisp_Object face = Fplist_get (props, Qface);
16269 props = Fcopy_sequence (props);
16270 if (NILP (face))
16271 face = mode_line_string_face;
16272 else
16273 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16274 props = Fplist_put (props, Qface, face);
16276 Fadd_text_properties (make_number (0), make_number (len),
16277 props, lisp_string);
16279 else
16281 len = XFASTINT (Flength (lisp_string));
16282 if (precision > 0 && len > precision)
16284 len = precision;
16285 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16286 precision = -1;
16288 if (!NILP (mode_line_string_face))
16290 Lisp_Object face;
16291 if (NILP (props))
16292 props = Ftext_properties_at (make_number (0), lisp_string);
16293 face = Fplist_get (props, Qface);
16294 if (NILP (face))
16295 face = mode_line_string_face;
16296 else
16297 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16298 props = Fcons (Qface, Fcons (face, Qnil));
16299 if (copy_string)
16300 lisp_string = Fcopy_sequence (lisp_string);
16302 if (!NILP (props))
16303 Fadd_text_properties (make_number (0), make_number (len),
16304 props, lisp_string);
16307 if (len > 0)
16309 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16310 n += len;
16313 if (field_width > len)
16315 field_width -= len;
16316 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16317 if (!NILP (props))
16318 Fadd_text_properties (make_number (0), make_number (field_width),
16319 props, lisp_string);
16320 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16321 n += field_width;
16324 return n;
16328 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16329 1, 4, 0,
16330 doc: /* Format a string out of a mode line format specification.
16331 First arg FORMAT specifies the mode line format (see `mode-line-format'
16332 for details) to use.
16334 Optional second arg FACE specifies the face property to put
16335 on all characters for which no face is specified.
16336 t means whatever face the window's mode line currently uses
16337 \(either `mode-line' or `mode-line-inactive', depending).
16338 nil means the default is no face property.
16339 If FACE is an integer, the value string has no text properties.
16341 Optional third and fourth args WINDOW and BUFFER specify the window
16342 and buffer to use as the context for the formatting (defaults
16343 are the selected window and the window's buffer). */)
16344 (format, face, window, buffer)
16345 Lisp_Object format, face, window, buffer;
16347 struct it it;
16348 int len;
16349 struct window *w;
16350 struct buffer *old_buffer = NULL;
16351 int face_id = -1;
16352 int no_props = INTEGERP (face);
16353 int count = SPECPDL_INDEX ();
16354 Lisp_Object str;
16355 int string_start = 0;
16357 if (NILP (window))
16358 window = selected_window;
16359 CHECK_WINDOW (window);
16360 w = XWINDOW (window);
16362 if (NILP (buffer))
16363 buffer = w->buffer;
16364 CHECK_BUFFER (buffer);
16366 if (NILP (format))
16367 return build_string ("");
16369 if (no_props)
16370 face = Qnil;
16372 if (!NILP (face))
16374 if (EQ (face, Qt))
16375 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16376 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16379 if (face_id < 0)
16380 face_id = DEFAULT_FACE_ID;
16382 if (XBUFFER (buffer) != current_buffer)
16383 old_buffer = current_buffer;
16385 record_unwind_protect (unwind_format_mode_line,
16386 format_mode_line_unwind_data (old_buffer));
16388 if (old_buffer)
16389 set_buffer_internal_1 (XBUFFER (buffer));
16391 init_iterator (&it, w, -1, -1, NULL, face_id);
16393 if (no_props)
16395 mode_line_target = MODE_LINE_NOPROP;
16396 mode_line_string_face_prop = Qnil;
16397 mode_line_string_list = Qnil;
16398 string_start = MODE_LINE_NOPROP_LEN (0);
16400 else
16402 mode_line_target = MODE_LINE_STRING;
16403 mode_line_string_list = Qnil;
16404 mode_line_string_face = face;
16405 mode_line_string_face_prop
16406 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16409 push_frame_kboard (it.f);
16410 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16411 pop_frame_kboard ();
16413 if (no_props)
16415 len = MODE_LINE_NOPROP_LEN (string_start);
16416 str = make_string (mode_line_noprop_buf + string_start, len);
16418 else
16420 mode_line_string_list = Fnreverse (mode_line_string_list);
16421 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16422 make_string ("", 0));
16425 unbind_to (count, Qnil);
16426 return str;
16429 /* Write a null-terminated, right justified decimal representation of
16430 the positive integer D to BUF using a minimal field width WIDTH. */
16432 static void
16433 pint2str (buf, width, d)
16434 register char *buf;
16435 register int width;
16436 register int d;
16438 register char *p = buf;
16440 if (d <= 0)
16441 *p++ = '0';
16442 else
16444 while (d > 0)
16446 *p++ = d % 10 + '0';
16447 d /= 10;
16451 for (width -= (int) (p - buf); width > 0; --width)
16452 *p++ = ' ';
16453 *p-- = '\0';
16454 while (p > buf)
16456 d = *buf;
16457 *buf++ = *p;
16458 *p-- = d;
16462 /* Write a null-terminated, right justified decimal and "human
16463 readable" representation of the nonnegative integer D to BUF using
16464 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16466 static const char power_letter[] =
16468 0, /* not used */
16469 'k', /* kilo */
16470 'M', /* mega */
16471 'G', /* giga */
16472 'T', /* tera */
16473 'P', /* peta */
16474 'E', /* exa */
16475 'Z', /* zetta */
16476 'Y' /* yotta */
16479 static void
16480 pint2hrstr (buf, width, d)
16481 char *buf;
16482 int width;
16483 int d;
16485 /* We aim to represent the nonnegative integer D as
16486 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16487 int quotient = d;
16488 int remainder = 0;
16489 /* -1 means: do not use TENTHS. */
16490 int tenths = -1;
16491 int exponent = 0;
16493 /* Length of QUOTIENT.TENTHS as a string. */
16494 int length;
16496 char * psuffix;
16497 char * p;
16499 if (1000 <= quotient)
16501 /* Scale to the appropriate EXPONENT. */
16504 remainder = quotient % 1000;
16505 quotient /= 1000;
16506 exponent++;
16508 while (1000 <= quotient);
16510 /* Round to nearest and decide whether to use TENTHS or not. */
16511 if (quotient <= 9)
16513 tenths = remainder / 100;
16514 if (50 <= remainder % 100)
16516 if (tenths < 9)
16517 tenths++;
16518 else
16520 quotient++;
16521 if (quotient == 10)
16522 tenths = -1;
16523 else
16524 tenths = 0;
16528 else
16529 if (500 <= remainder)
16531 if (quotient < 999)
16532 quotient++;
16533 else
16535 quotient = 1;
16536 exponent++;
16537 tenths = 0;
16542 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16543 if (tenths == -1 && quotient <= 99)
16544 if (quotient <= 9)
16545 length = 1;
16546 else
16547 length = 2;
16548 else
16549 length = 3;
16550 p = psuffix = buf + max (width, length);
16552 /* Print EXPONENT. */
16553 if (exponent)
16554 *psuffix++ = power_letter[exponent];
16555 *psuffix = '\0';
16557 /* Print TENTHS. */
16558 if (tenths >= 0)
16560 *--p = '0' + tenths;
16561 *--p = '.';
16564 /* Print QUOTIENT. */
16567 int digit = quotient % 10;
16568 *--p = '0' + digit;
16570 while ((quotient /= 10) != 0);
16572 /* Print leading spaces. */
16573 while (buf < p)
16574 *--p = ' ';
16577 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16578 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16579 type of CODING_SYSTEM. Return updated pointer into BUF. */
16581 static unsigned char invalid_eol_type[] = "(*invalid*)";
16583 static char *
16584 decode_mode_spec_coding (coding_system, buf, eol_flag)
16585 Lisp_Object coding_system;
16586 register char *buf;
16587 int eol_flag;
16589 Lisp_Object val;
16590 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16591 const unsigned char *eol_str;
16592 int eol_str_len;
16593 /* The EOL conversion we are using. */
16594 Lisp_Object eoltype;
16596 val = Fget (coding_system, Qcoding_system);
16597 eoltype = Qnil;
16599 if (!VECTORP (val)) /* Not yet decided. */
16601 if (multibyte)
16602 *buf++ = '-';
16603 if (eol_flag)
16604 eoltype = eol_mnemonic_undecided;
16605 /* Don't mention EOL conversion if it isn't decided. */
16607 else
16609 Lisp_Object eolvalue;
16611 eolvalue = Fget (coding_system, Qeol_type);
16613 if (multibyte)
16614 *buf++ = XFASTINT (AREF (val, 1));
16616 if (eol_flag)
16618 /* The EOL conversion that is normal on this system. */
16620 if (NILP (eolvalue)) /* Not yet decided. */
16621 eoltype = eol_mnemonic_undecided;
16622 else if (VECTORP (eolvalue)) /* Not yet decided. */
16623 eoltype = eol_mnemonic_undecided;
16624 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16625 eoltype = (XFASTINT (eolvalue) == 0
16626 ? eol_mnemonic_unix
16627 : (XFASTINT (eolvalue) == 1
16628 ? eol_mnemonic_dos : eol_mnemonic_mac));
16632 if (eol_flag)
16634 /* Mention the EOL conversion if it is not the usual one. */
16635 if (STRINGP (eoltype))
16637 eol_str = SDATA (eoltype);
16638 eol_str_len = SBYTES (eoltype);
16640 else if (INTEGERP (eoltype)
16641 && CHAR_VALID_P (XINT (eoltype), 0))
16643 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16644 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16645 eol_str = tmp;
16647 else
16649 eol_str = invalid_eol_type;
16650 eol_str_len = sizeof (invalid_eol_type) - 1;
16652 bcopy (eol_str, buf, eol_str_len);
16653 buf += eol_str_len;
16656 return buf;
16659 /* Return a string for the output of a mode line %-spec for window W,
16660 generated by character C. PRECISION >= 0 means don't return a
16661 string longer than that value. FIELD_WIDTH > 0 means pad the
16662 string returned with spaces to that value. Return 1 in *MULTIBYTE
16663 if the result is multibyte text.
16665 Note we operate on the current buffer for most purposes,
16666 the exception being w->base_line_pos. */
16668 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16670 static char *
16671 decode_mode_spec (w, c, field_width, precision, multibyte)
16672 struct window *w;
16673 register int c;
16674 int field_width, precision;
16675 int *multibyte;
16677 Lisp_Object obj;
16678 struct frame *f = XFRAME (WINDOW_FRAME (w));
16679 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16680 struct buffer *b = current_buffer;
16682 obj = Qnil;
16683 *multibyte = 0;
16685 switch (c)
16687 case '*':
16688 if (!NILP (b->read_only))
16689 return "%";
16690 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16691 return "*";
16692 return "-";
16694 case '+':
16695 /* This differs from %* only for a modified read-only buffer. */
16696 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16697 return "*";
16698 if (!NILP (b->read_only))
16699 return "%";
16700 return "-";
16702 case '&':
16703 /* This differs from %* in ignoring read-only-ness. */
16704 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16705 return "*";
16706 return "-";
16708 case '%':
16709 return "%";
16711 case '[':
16713 int i;
16714 char *p;
16716 if (command_loop_level > 5)
16717 return "[[[... ";
16718 p = decode_mode_spec_buf;
16719 for (i = 0; i < command_loop_level; i++)
16720 *p++ = '[';
16721 *p = 0;
16722 return decode_mode_spec_buf;
16725 case ']':
16727 int i;
16728 char *p;
16730 if (command_loop_level > 5)
16731 return " ...]]]";
16732 p = decode_mode_spec_buf;
16733 for (i = 0; i < command_loop_level; i++)
16734 *p++ = ']';
16735 *p = 0;
16736 return decode_mode_spec_buf;
16739 case '-':
16741 register int i;
16743 /* Let lots_of_dashes be a string of infinite length. */
16744 if (mode_line_target == MODE_LINE_NOPROP ||
16745 mode_line_target == MODE_LINE_STRING)
16746 return "--";
16747 if (field_width <= 0
16748 || field_width > sizeof (lots_of_dashes))
16750 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16751 decode_mode_spec_buf[i] = '-';
16752 decode_mode_spec_buf[i] = '\0';
16753 return decode_mode_spec_buf;
16755 else
16756 return lots_of_dashes;
16759 case 'b':
16760 obj = b->name;
16761 break;
16763 case 'c':
16765 int col = (int) current_column (); /* iftc */
16766 w->column_number_displayed = make_number (col);
16767 pint2str (decode_mode_spec_buf, field_width, col);
16768 return decode_mode_spec_buf;
16771 case 'F':
16772 /* %F displays the frame name. */
16773 if (!NILP (f->title))
16774 return (char *) SDATA (f->title);
16775 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16776 return (char *) SDATA (f->name);
16777 return "Emacs";
16779 case 'f':
16780 obj = b->filename;
16781 break;
16783 case 'i':
16785 int size = ZV - BEGV;
16786 pint2str (decode_mode_spec_buf, field_width, size);
16787 return decode_mode_spec_buf;
16790 case 'I':
16792 int size = ZV - BEGV;
16793 pint2hrstr (decode_mode_spec_buf, field_width, size);
16794 return decode_mode_spec_buf;
16797 case 'l':
16799 int startpos = XMARKER (w->start)->charpos;
16800 int startpos_byte = marker_byte_position (w->start);
16801 int line, linepos, linepos_byte, topline;
16802 int nlines, junk;
16803 int height = WINDOW_TOTAL_LINES (w);
16805 /* If we decided that this buffer isn't suitable for line numbers,
16806 don't forget that too fast. */
16807 if (EQ (w->base_line_pos, w->buffer))
16808 goto no_value;
16809 /* But do forget it, if the window shows a different buffer now. */
16810 else if (BUFFERP (w->base_line_pos))
16811 w->base_line_pos = Qnil;
16813 /* If the buffer is very big, don't waste time. */
16814 if (INTEGERP (Vline_number_display_limit)
16815 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16817 w->base_line_pos = Qnil;
16818 w->base_line_number = Qnil;
16819 goto no_value;
16822 if (!NILP (w->base_line_number)
16823 && !NILP (w->base_line_pos)
16824 && XFASTINT (w->base_line_pos) <= startpos)
16826 line = XFASTINT (w->base_line_number);
16827 linepos = XFASTINT (w->base_line_pos);
16828 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16830 else
16832 line = 1;
16833 linepos = BUF_BEGV (b);
16834 linepos_byte = BUF_BEGV_BYTE (b);
16837 /* Count lines from base line to window start position. */
16838 nlines = display_count_lines (linepos, linepos_byte,
16839 startpos_byte,
16840 startpos, &junk);
16842 topline = nlines + line;
16844 /* Determine a new base line, if the old one is too close
16845 or too far away, or if we did not have one.
16846 "Too close" means it's plausible a scroll-down would
16847 go back past it. */
16848 if (startpos == BUF_BEGV (b))
16850 w->base_line_number = make_number (topline);
16851 w->base_line_pos = make_number (BUF_BEGV (b));
16853 else if (nlines < height + 25 || nlines > height * 3 + 50
16854 || linepos == BUF_BEGV (b))
16856 int limit = BUF_BEGV (b);
16857 int limit_byte = BUF_BEGV_BYTE (b);
16858 int position;
16859 int distance = (height * 2 + 30) * line_number_display_limit_width;
16861 if (startpos - distance > limit)
16863 limit = startpos - distance;
16864 limit_byte = CHAR_TO_BYTE (limit);
16867 nlines = display_count_lines (startpos, startpos_byte,
16868 limit_byte,
16869 - (height * 2 + 30),
16870 &position);
16871 /* If we couldn't find the lines we wanted within
16872 line_number_display_limit_width chars per line,
16873 give up on line numbers for this window. */
16874 if (position == limit_byte && limit == startpos - distance)
16876 w->base_line_pos = w->buffer;
16877 w->base_line_number = Qnil;
16878 goto no_value;
16881 w->base_line_number = make_number (topline - nlines);
16882 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16885 /* Now count lines from the start pos to point. */
16886 nlines = display_count_lines (startpos, startpos_byte,
16887 PT_BYTE, PT, &junk);
16889 /* Record that we did display the line number. */
16890 line_number_displayed = 1;
16892 /* Make the string to show. */
16893 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16894 return decode_mode_spec_buf;
16895 no_value:
16897 char* p = decode_mode_spec_buf;
16898 int pad = field_width - 2;
16899 while (pad-- > 0)
16900 *p++ = ' ';
16901 *p++ = '?';
16902 *p++ = '?';
16903 *p = '\0';
16904 return decode_mode_spec_buf;
16907 break;
16909 case 'm':
16910 obj = b->mode_name;
16911 break;
16913 case 'n':
16914 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16915 return " Narrow";
16916 break;
16918 case 'p':
16920 int pos = marker_position (w->start);
16921 int total = BUF_ZV (b) - BUF_BEGV (b);
16923 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16925 if (pos <= BUF_BEGV (b))
16926 return "All";
16927 else
16928 return "Bottom";
16930 else if (pos <= BUF_BEGV (b))
16931 return "Top";
16932 else
16934 if (total > 1000000)
16935 /* Do it differently for a large value, to avoid overflow. */
16936 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16937 else
16938 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16939 /* We can't normally display a 3-digit number,
16940 so get us a 2-digit number that is close. */
16941 if (total == 100)
16942 total = 99;
16943 sprintf (decode_mode_spec_buf, "%2d%%", total);
16944 return decode_mode_spec_buf;
16948 /* Display percentage of size above the bottom of the screen. */
16949 case 'P':
16951 int toppos = marker_position (w->start);
16952 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16953 int total = BUF_ZV (b) - BUF_BEGV (b);
16955 if (botpos >= BUF_ZV (b))
16957 if (toppos <= BUF_BEGV (b))
16958 return "All";
16959 else
16960 return "Bottom";
16962 else
16964 if (total > 1000000)
16965 /* Do it differently for a large value, to avoid overflow. */
16966 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16967 else
16968 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16969 /* We can't normally display a 3-digit number,
16970 so get us a 2-digit number that is close. */
16971 if (total == 100)
16972 total = 99;
16973 if (toppos <= BUF_BEGV (b))
16974 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16975 else
16976 sprintf (decode_mode_spec_buf, "%2d%%", total);
16977 return decode_mode_spec_buf;
16981 case 's':
16982 /* status of process */
16983 obj = Fget_buffer_process (Fcurrent_buffer ());
16984 if (NILP (obj))
16985 return "no process";
16986 #ifdef subprocesses
16987 obj = Fsymbol_name (Fprocess_status (obj));
16988 #endif
16989 break;
16991 case 't': /* indicate TEXT or BINARY */
16992 #ifdef MODE_LINE_BINARY_TEXT
16993 return MODE_LINE_BINARY_TEXT (b);
16994 #else
16995 return "T";
16996 #endif
16998 case 'z':
16999 /* coding-system (not including end-of-line format) */
17000 case 'Z':
17001 /* coding-system (including end-of-line type) */
17003 int eol_flag = (c == 'Z');
17004 char *p = decode_mode_spec_buf;
17006 if (! FRAME_WINDOW_P (f))
17008 /* No need to mention EOL here--the terminal never needs
17009 to do EOL conversion. */
17010 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17011 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17013 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17014 p, eol_flag);
17016 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17017 #ifdef subprocesses
17018 obj = Fget_buffer_process (Fcurrent_buffer ());
17019 if (PROCESSP (obj))
17021 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17022 p, eol_flag);
17023 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17024 p, eol_flag);
17026 #endif /* subprocesses */
17027 #endif /* 0 */
17028 *p = 0;
17029 return decode_mode_spec_buf;
17033 if (STRINGP (obj))
17035 *multibyte = STRING_MULTIBYTE (obj);
17036 return (char *) SDATA (obj);
17038 else
17039 return "";
17043 /* Count up to COUNT lines starting from START / START_BYTE.
17044 But don't go beyond LIMIT_BYTE.
17045 Return the number of lines thus found (always nonnegative).
17047 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17049 static int
17050 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17051 int start, start_byte, limit_byte, count;
17052 int *byte_pos_ptr;
17054 register unsigned char *cursor;
17055 unsigned char *base;
17057 register int ceiling;
17058 register unsigned char *ceiling_addr;
17059 int orig_count = count;
17061 /* If we are not in selective display mode,
17062 check only for newlines. */
17063 int selective_display = (!NILP (current_buffer->selective_display)
17064 && !INTEGERP (current_buffer->selective_display));
17066 if (count > 0)
17068 while (start_byte < limit_byte)
17070 ceiling = BUFFER_CEILING_OF (start_byte);
17071 ceiling = min (limit_byte - 1, ceiling);
17072 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17073 base = (cursor = BYTE_POS_ADDR (start_byte));
17074 while (1)
17076 if (selective_display)
17077 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17079 else
17080 while (*cursor != '\n' && ++cursor != ceiling_addr)
17083 if (cursor != ceiling_addr)
17085 if (--count == 0)
17087 start_byte += cursor - base + 1;
17088 *byte_pos_ptr = start_byte;
17089 return orig_count;
17091 else
17092 if (++cursor == ceiling_addr)
17093 break;
17095 else
17096 break;
17098 start_byte += cursor - base;
17101 else
17103 while (start_byte > limit_byte)
17105 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17106 ceiling = max (limit_byte, ceiling);
17107 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17108 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17109 while (1)
17111 if (selective_display)
17112 while (--cursor != ceiling_addr
17113 && *cursor != '\n' && *cursor != 015)
17115 else
17116 while (--cursor != ceiling_addr && *cursor != '\n')
17119 if (cursor != ceiling_addr)
17121 if (++count == 0)
17123 start_byte += cursor - base + 1;
17124 *byte_pos_ptr = start_byte;
17125 /* When scanning backwards, we should
17126 not count the newline posterior to which we stop. */
17127 return - orig_count - 1;
17130 else
17131 break;
17133 /* Here we add 1 to compensate for the last decrement
17134 of CURSOR, which took it past the valid range. */
17135 start_byte += cursor - base + 1;
17139 *byte_pos_ptr = limit_byte;
17141 if (count < 0)
17142 return - orig_count + count;
17143 return orig_count - count;
17149 /***********************************************************************
17150 Displaying strings
17151 ***********************************************************************/
17153 /* Display a NUL-terminated string, starting with index START.
17155 If STRING is non-null, display that C string. Otherwise, the Lisp
17156 string LISP_STRING is displayed.
17158 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17159 FACE_STRING. Display STRING or LISP_STRING with the face at
17160 FACE_STRING_POS in FACE_STRING:
17162 Display the string in the environment given by IT, but use the
17163 standard display table, temporarily.
17165 FIELD_WIDTH is the minimum number of output glyphs to produce.
17166 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17167 with spaces. If STRING has more characters, more than FIELD_WIDTH
17168 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17170 PRECISION is the maximum number of characters to output from
17171 STRING. PRECISION < 0 means don't truncate the string.
17173 This is roughly equivalent to printf format specifiers:
17175 FIELD_WIDTH PRECISION PRINTF
17176 ----------------------------------------
17177 -1 -1 %s
17178 -1 10 %.10s
17179 10 -1 %10s
17180 20 10 %20.10s
17182 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17183 display them, and < 0 means obey the current buffer's value of
17184 enable_multibyte_characters.
17186 Value is the number of glyphs produced. */
17188 static int
17189 display_string (string, lisp_string, face_string, face_string_pos,
17190 start, it, field_width, precision, max_x, multibyte)
17191 unsigned char *string;
17192 Lisp_Object lisp_string;
17193 Lisp_Object face_string;
17194 int face_string_pos;
17195 int start;
17196 struct it *it;
17197 int field_width, precision, max_x;
17198 int multibyte;
17200 int hpos_at_start = it->hpos;
17201 int saved_face_id = it->face_id;
17202 struct glyph_row *row = it->glyph_row;
17204 /* Initialize the iterator IT for iteration over STRING beginning
17205 with index START. */
17206 reseat_to_string (it, string, lisp_string, start,
17207 precision, field_width, multibyte);
17209 /* If displaying STRING, set up the face of the iterator
17210 from LISP_STRING, if that's given. */
17211 if (STRINGP (face_string))
17213 int endptr;
17214 struct face *face;
17216 it->face_id
17217 = face_at_string_position (it->w, face_string, face_string_pos,
17218 0, it->region_beg_charpos,
17219 it->region_end_charpos,
17220 &endptr, it->base_face_id, 0);
17221 face = FACE_FROM_ID (it->f, it->face_id);
17222 it->face_box_p = face->box != FACE_NO_BOX;
17225 /* Set max_x to the maximum allowed X position. Don't let it go
17226 beyond the right edge of the window. */
17227 if (max_x <= 0)
17228 max_x = it->last_visible_x;
17229 else
17230 max_x = min (max_x, it->last_visible_x);
17232 /* Skip over display elements that are not visible. because IT->w is
17233 hscrolled. */
17234 if (it->current_x < it->first_visible_x)
17235 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17236 MOVE_TO_POS | MOVE_TO_X);
17238 row->ascent = it->max_ascent;
17239 row->height = it->max_ascent + it->max_descent;
17240 row->phys_ascent = it->max_phys_ascent;
17241 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17242 row->extra_line_spacing = it->max_extra_line_spacing;
17244 /* This condition is for the case that we are called with current_x
17245 past last_visible_x. */
17246 while (it->current_x < max_x)
17248 int x_before, x, n_glyphs_before, i, nglyphs;
17250 /* Get the next display element. */
17251 if (!get_next_display_element (it))
17252 break;
17254 /* Produce glyphs. */
17255 x_before = it->current_x;
17256 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17257 PRODUCE_GLYPHS (it);
17259 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17260 i = 0;
17261 x = x_before;
17262 while (i < nglyphs)
17264 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17266 if (!it->truncate_lines_p
17267 && x + glyph->pixel_width > max_x)
17269 /* End of continued line or max_x reached. */
17270 if (CHAR_GLYPH_PADDING_P (*glyph))
17272 /* A wide character is unbreakable. */
17273 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17274 it->current_x = x_before;
17276 else
17278 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17279 it->current_x = x;
17281 break;
17283 else if (x + glyph->pixel_width > it->first_visible_x)
17285 /* Glyph is at least partially visible. */
17286 ++it->hpos;
17287 if (x < it->first_visible_x)
17288 it->glyph_row->x = x - it->first_visible_x;
17290 else
17292 /* Glyph is off the left margin of the display area.
17293 Should not happen. */
17294 abort ();
17297 row->ascent = max (row->ascent, it->max_ascent);
17298 row->height = max (row->height, it->max_ascent + it->max_descent);
17299 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17300 row->phys_height = max (row->phys_height,
17301 it->max_phys_ascent + it->max_phys_descent);
17302 row->extra_line_spacing = max (row->extra_line_spacing,
17303 it->max_extra_line_spacing);
17304 x += glyph->pixel_width;
17305 ++i;
17308 /* Stop if max_x reached. */
17309 if (i < nglyphs)
17310 break;
17312 /* Stop at line ends. */
17313 if (ITERATOR_AT_END_OF_LINE_P (it))
17315 it->continuation_lines_width = 0;
17316 break;
17319 set_iterator_to_next (it, 1);
17321 /* Stop if truncating at the right edge. */
17322 if (it->truncate_lines_p
17323 && it->current_x >= it->last_visible_x)
17325 /* Add truncation mark, but don't do it if the line is
17326 truncated at a padding space. */
17327 if (IT_CHARPOS (*it) < it->string_nchars)
17329 if (!FRAME_WINDOW_P (it->f))
17331 int i, n;
17333 if (it->current_x > it->last_visible_x)
17335 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17336 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17337 break;
17338 for (n = row->used[TEXT_AREA]; i < n; ++i)
17340 row->used[TEXT_AREA] = i;
17341 produce_special_glyphs (it, IT_TRUNCATION);
17344 produce_special_glyphs (it, IT_TRUNCATION);
17346 it->glyph_row->truncated_on_right_p = 1;
17348 break;
17352 /* Maybe insert a truncation at the left. */
17353 if (it->first_visible_x
17354 && IT_CHARPOS (*it) > 0)
17356 if (!FRAME_WINDOW_P (it->f))
17357 insert_left_trunc_glyphs (it);
17358 it->glyph_row->truncated_on_left_p = 1;
17361 it->face_id = saved_face_id;
17363 /* Value is number of columns displayed. */
17364 return it->hpos - hpos_at_start;
17369 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17370 appears as an element of LIST or as the car of an element of LIST.
17371 If PROPVAL is a list, compare each element against LIST in that
17372 way, and return 1/2 if any element of PROPVAL is found in LIST.
17373 Otherwise return 0. This function cannot quit.
17374 The return value is 2 if the text is invisible but with an ellipsis
17375 and 1 if it's invisible and without an ellipsis. */
17378 invisible_p (propval, list)
17379 register Lisp_Object propval;
17380 Lisp_Object list;
17382 register Lisp_Object tail, proptail;
17384 for (tail = list; CONSP (tail); tail = XCDR (tail))
17386 register Lisp_Object tem;
17387 tem = XCAR (tail);
17388 if (EQ (propval, tem))
17389 return 1;
17390 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17391 return NILP (XCDR (tem)) ? 1 : 2;
17394 if (CONSP (propval))
17396 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17398 Lisp_Object propelt;
17399 propelt = XCAR (proptail);
17400 for (tail = list; CONSP (tail); tail = XCDR (tail))
17402 register Lisp_Object tem;
17403 tem = XCAR (tail);
17404 if (EQ (propelt, tem))
17405 return 1;
17406 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17407 return NILP (XCDR (tem)) ? 1 : 2;
17412 return 0;
17415 /* Calculate a width or height in pixels from a specification using
17416 the following elements:
17418 SPEC ::=
17419 NUM - a (fractional) multiple of the default font width/height
17420 (NUM) - specifies exactly NUM pixels
17421 UNIT - a fixed number of pixels, see below.
17422 ELEMENT - size of a display element in pixels, see below.
17423 (NUM . SPEC) - equals NUM * SPEC
17424 (+ SPEC SPEC ...) - add pixel values
17425 (- SPEC SPEC ...) - subtract pixel values
17426 (- SPEC) - negate pixel value
17428 NUM ::=
17429 INT or FLOAT - a number constant
17430 SYMBOL - use symbol's (buffer local) variable binding.
17432 UNIT ::=
17433 in - pixels per inch *)
17434 mm - pixels per 1/1000 meter *)
17435 cm - pixels per 1/100 meter *)
17436 width - width of current font in pixels.
17437 height - height of current font in pixels.
17439 *) using the ratio(s) defined in display-pixels-per-inch.
17441 ELEMENT ::=
17443 left-fringe - left fringe width in pixels
17444 right-fringe - right fringe width in pixels
17446 left-margin - left margin width in pixels
17447 right-margin - right margin width in pixels
17449 scroll-bar - scroll-bar area width in pixels
17451 Examples:
17453 Pixels corresponding to 5 inches:
17454 (5 . in)
17456 Total width of non-text areas on left side of window (if scroll-bar is on left):
17457 '(space :width (+ left-fringe left-margin scroll-bar))
17459 Align to first text column (in header line):
17460 '(space :align-to 0)
17462 Align to middle of text area minus half the width of variable `my-image'
17463 containing a loaded image:
17464 '(space :align-to (0.5 . (- text my-image)))
17466 Width of left margin minus width of 1 character in the default font:
17467 '(space :width (- left-margin 1))
17469 Width of left margin minus width of 2 characters in the current font:
17470 '(space :width (- left-margin (2 . width)))
17472 Center 1 character over left-margin (in header line):
17473 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17475 Different ways to express width of left fringe plus left margin minus one pixel:
17476 '(space :width (- (+ left-fringe left-margin) (1)))
17477 '(space :width (+ left-fringe left-margin (- (1))))
17478 '(space :width (+ left-fringe left-margin (-1)))
17482 #define NUMVAL(X) \
17483 ((INTEGERP (X) || FLOATP (X)) \
17484 ? XFLOATINT (X) \
17485 : - 1)
17488 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17489 double *res;
17490 struct it *it;
17491 Lisp_Object prop;
17492 void *font;
17493 int width_p, *align_to;
17495 double pixels;
17497 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17498 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17500 if (NILP (prop))
17501 return OK_PIXELS (0);
17503 if (SYMBOLP (prop))
17505 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17507 char *unit = SDATA (SYMBOL_NAME (prop));
17509 if (unit[0] == 'i' && unit[1] == 'n')
17510 pixels = 1.0;
17511 else if (unit[0] == 'm' && unit[1] == 'm')
17512 pixels = 25.4;
17513 else if (unit[0] == 'c' && unit[1] == 'm')
17514 pixels = 2.54;
17515 else
17516 pixels = 0;
17517 if (pixels > 0)
17519 double ppi;
17520 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17521 || (CONSP (Vdisplay_pixels_per_inch)
17522 && (ppi = (width_p
17523 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17524 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17525 ppi > 0)))
17526 return OK_PIXELS (ppi / pixels);
17528 return 0;
17532 #ifdef HAVE_WINDOW_SYSTEM
17533 if (EQ (prop, Qheight))
17534 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17535 if (EQ (prop, Qwidth))
17536 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17537 #else
17538 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17539 return OK_PIXELS (1);
17540 #endif
17542 if (EQ (prop, Qtext))
17543 return OK_PIXELS (width_p
17544 ? window_box_width (it->w, TEXT_AREA)
17545 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17547 if (align_to && *align_to < 0)
17549 *res = 0;
17550 if (EQ (prop, Qleft))
17551 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17552 if (EQ (prop, Qright))
17553 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17554 if (EQ (prop, Qcenter))
17555 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17556 + window_box_width (it->w, TEXT_AREA) / 2);
17557 if (EQ (prop, Qleft_fringe))
17558 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17559 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17560 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17561 if (EQ (prop, Qright_fringe))
17562 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17563 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17564 : window_box_right_offset (it->w, TEXT_AREA));
17565 if (EQ (prop, Qleft_margin))
17566 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17567 if (EQ (prop, Qright_margin))
17568 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17569 if (EQ (prop, Qscroll_bar))
17570 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17572 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17573 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17574 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17575 : 0)));
17577 else
17579 if (EQ (prop, Qleft_fringe))
17580 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17581 if (EQ (prop, Qright_fringe))
17582 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17583 if (EQ (prop, Qleft_margin))
17584 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17585 if (EQ (prop, Qright_margin))
17586 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17587 if (EQ (prop, Qscroll_bar))
17588 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17591 prop = Fbuffer_local_value (prop, it->w->buffer);
17594 if (INTEGERP (prop) || FLOATP (prop))
17596 int base_unit = (width_p
17597 ? FRAME_COLUMN_WIDTH (it->f)
17598 : FRAME_LINE_HEIGHT (it->f));
17599 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17602 if (CONSP (prop))
17604 Lisp_Object car = XCAR (prop);
17605 Lisp_Object cdr = XCDR (prop);
17607 if (SYMBOLP (car))
17609 #ifdef HAVE_WINDOW_SYSTEM
17610 if (valid_image_p (prop))
17612 int id = lookup_image (it->f, prop);
17613 struct image *img = IMAGE_FROM_ID (it->f, id);
17615 return OK_PIXELS (width_p ? img->width : img->height);
17617 #endif
17618 if (EQ (car, Qplus) || EQ (car, Qminus))
17620 int first = 1;
17621 double px;
17623 pixels = 0;
17624 while (CONSP (cdr))
17626 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17627 font, width_p, align_to))
17628 return 0;
17629 if (first)
17630 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17631 else
17632 pixels += px;
17633 cdr = XCDR (cdr);
17635 if (EQ (car, Qminus))
17636 pixels = -pixels;
17637 return OK_PIXELS (pixels);
17640 car = Fbuffer_local_value (car, it->w->buffer);
17643 if (INTEGERP (car) || FLOATP (car))
17645 double fact;
17646 pixels = XFLOATINT (car);
17647 if (NILP (cdr))
17648 return OK_PIXELS (pixels);
17649 if (calc_pixel_width_or_height (&fact, it, cdr,
17650 font, width_p, align_to))
17651 return OK_PIXELS (pixels * fact);
17652 return 0;
17655 return 0;
17658 return 0;
17662 /***********************************************************************
17663 Glyph Display
17664 ***********************************************************************/
17666 #ifdef HAVE_WINDOW_SYSTEM
17668 #if GLYPH_DEBUG
17670 void
17671 dump_glyph_string (s)
17672 struct glyph_string *s;
17674 fprintf (stderr, "glyph string\n");
17675 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17676 s->x, s->y, s->width, s->height);
17677 fprintf (stderr, " ybase = %d\n", s->ybase);
17678 fprintf (stderr, " hl = %d\n", s->hl);
17679 fprintf (stderr, " left overhang = %d, right = %d\n",
17680 s->left_overhang, s->right_overhang);
17681 fprintf (stderr, " nchars = %d\n", s->nchars);
17682 fprintf (stderr, " extends to end of line = %d\n",
17683 s->extends_to_end_of_line_p);
17684 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17685 fprintf (stderr, " bg width = %d\n", s->background_width);
17688 #endif /* GLYPH_DEBUG */
17690 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17691 of XChar2b structures for S; it can't be allocated in
17692 init_glyph_string because it must be allocated via `alloca'. W
17693 is the window on which S is drawn. ROW and AREA are the glyph row
17694 and area within the row from which S is constructed. START is the
17695 index of the first glyph structure covered by S. HL is a
17696 face-override for drawing S. */
17698 #ifdef HAVE_NTGUI
17699 #define OPTIONAL_HDC(hdc) hdc,
17700 #define DECLARE_HDC(hdc) HDC hdc;
17701 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17702 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17703 #endif
17705 #ifndef OPTIONAL_HDC
17706 #define OPTIONAL_HDC(hdc)
17707 #define DECLARE_HDC(hdc)
17708 #define ALLOCATE_HDC(hdc, f)
17709 #define RELEASE_HDC(hdc, f)
17710 #endif
17712 static void
17713 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17714 struct glyph_string *s;
17715 DECLARE_HDC (hdc)
17716 XChar2b *char2b;
17717 struct window *w;
17718 struct glyph_row *row;
17719 enum glyph_row_area area;
17720 int start;
17721 enum draw_glyphs_face hl;
17723 bzero (s, sizeof *s);
17724 s->w = w;
17725 s->f = XFRAME (w->frame);
17726 #ifdef HAVE_NTGUI
17727 s->hdc = hdc;
17728 #endif
17729 s->display = FRAME_X_DISPLAY (s->f);
17730 s->window = FRAME_X_WINDOW (s->f);
17731 s->char2b = char2b;
17732 s->hl = hl;
17733 s->row = row;
17734 s->area = area;
17735 s->first_glyph = row->glyphs[area] + start;
17736 s->height = row->height;
17737 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17739 /* Display the internal border below the tool-bar window. */
17740 if (WINDOWP (s->f->tool_bar_window)
17741 && s->w == XWINDOW (s->f->tool_bar_window))
17742 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17744 s->ybase = s->y + row->ascent;
17748 /* Append the list of glyph strings with head H and tail T to the list
17749 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17751 static INLINE void
17752 append_glyph_string_lists (head, tail, h, t)
17753 struct glyph_string **head, **tail;
17754 struct glyph_string *h, *t;
17756 if (h)
17758 if (*head)
17759 (*tail)->next = h;
17760 else
17761 *head = h;
17762 h->prev = *tail;
17763 *tail = t;
17768 /* Prepend the list of glyph strings with head H and tail T to the
17769 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17770 result. */
17772 static INLINE void
17773 prepend_glyph_string_lists (head, tail, h, t)
17774 struct glyph_string **head, **tail;
17775 struct glyph_string *h, *t;
17777 if (h)
17779 if (*head)
17780 (*head)->prev = t;
17781 else
17782 *tail = t;
17783 t->next = *head;
17784 *head = h;
17789 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17790 Set *HEAD and *TAIL to the resulting list. */
17792 static INLINE void
17793 append_glyph_string (head, tail, s)
17794 struct glyph_string **head, **tail;
17795 struct glyph_string *s;
17797 s->next = s->prev = NULL;
17798 append_glyph_string_lists (head, tail, s, s);
17802 /* Get face and two-byte form of character glyph GLYPH on frame F.
17803 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17804 a pointer to a realized face that is ready for display. */
17806 static INLINE struct face *
17807 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17808 struct frame *f;
17809 struct glyph *glyph;
17810 XChar2b *char2b;
17811 int *two_byte_p;
17813 struct face *face;
17815 xassert (glyph->type == CHAR_GLYPH);
17816 face = FACE_FROM_ID (f, glyph->face_id);
17818 if (two_byte_p)
17819 *two_byte_p = 0;
17821 if (!glyph->multibyte_p)
17823 /* Unibyte case. We don't have to encode, but we have to make
17824 sure to use a face suitable for unibyte. */
17825 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17827 else if (glyph->u.ch < 128
17828 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17830 /* Case of ASCII in a face known to fit ASCII. */
17831 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17833 else
17835 int c1, c2, charset;
17837 /* Split characters into bytes. If c2 is -1 afterwards, C is
17838 really a one-byte character so that byte1 is zero. */
17839 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17840 if (c2 > 0)
17841 STORE_XCHAR2B (char2b, c1, c2);
17842 else
17843 STORE_XCHAR2B (char2b, 0, c1);
17845 /* Maybe encode the character in *CHAR2B. */
17846 if (charset != CHARSET_ASCII)
17848 struct font_info *font_info
17849 = FONT_INFO_FROM_ID (f, face->font_info_id);
17850 if (font_info)
17851 glyph->font_type
17852 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17856 /* Make sure X resources of the face are allocated. */
17857 xassert (face != NULL);
17858 PREPARE_FACE_FOR_DISPLAY (f, face);
17859 return face;
17863 /* Fill glyph string S with composition components specified by S->cmp.
17865 FACES is an array of faces for all components of this composition.
17866 S->gidx is the index of the first component for S.
17867 OVERLAPS_P non-zero means S should draw the foreground only, and
17868 use its physical height for clipping.
17870 Value is the index of a component not in S. */
17872 static int
17873 fill_composite_glyph_string (s, faces, overlaps_p)
17874 struct glyph_string *s;
17875 struct face **faces;
17876 int overlaps_p;
17878 int i;
17880 xassert (s);
17882 s->for_overlaps_p = overlaps_p;
17884 s->face = faces[s->gidx];
17885 s->font = s->face->font;
17886 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17888 /* For all glyphs of this composition, starting at the offset
17889 S->gidx, until we reach the end of the definition or encounter a
17890 glyph that requires the different face, add it to S. */
17891 ++s->nchars;
17892 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17893 ++s->nchars;
17895 /* All glyph strings for the same composition has the same width,
17896 i.e. the width set for the first component of the composition. */
17898 s->width = s->first_glyph->pixel_width;
17900 /* If the specified font could not be loaded, use the frame's
17901 default font, but record the fact that we couldn't load it in
17902 the glyph string so that we can draw rectangles for the
17903 characters of the glyph string. */
17904 if (s->font == NULL)
17906 s->font_not_found_p = 1;
17907 s->font = FRAME_FONT (s->f);
17910 /* Adjust base line for subscript/superscript text. */
17911 s->ybase += s->first_glyph->voffset;
17913 xassert (s->face && s->face->gc);
17915 /* This glyph string must always be drawn with 16-bit functions. */
17916 s->two_byte_p = 1;
17918 return s->gidx + s->nchars;
17922 /* Fill glyph string S from a sequence of character glyphs.
17924 FACE_ID is the face id of the string. START is the index of the
17925 first glyph to consider, END is the index of the last + 1.
17926 OVERLAPS_P non-zero means S should draw the foreground only, and
17927 use its physical height for clipping.
17929 Value is the index of the first glyph not in S. */
17931 static int
17932 fill_glyph_string (s, face_id, start, end, overlaps_p)
17933 struct glyph_string *s;
17934 int face_id;
17935 int start, end, overlaps_p;
17937 struct glyph *glyph, *last;
17938 int voffset;
17939 int glyph_not_available_p;
17941 xassert (s->f == XFRAME (s->w->frame));
17942 xassert (s->nchars == 0);
17943 xassert (start >= 0 && end > start);
17945 s->for_overlaps_p = overlaps_p,
17946 glyph = s->row->glyphs[s->area] + start;
17947 last = s->row->glyphs[s->area] + end;
17948 voffset = glyph->voffset;
17950 glyph_not_available_p = glyph->glyph_not_available_p;
17952 while (glyph < last
17953 && glyph->type == CHAR_GLYPH
17954 && glyph->voffset == voffset
17955 /* Same face id implies same font, nowadays. */
17956 && glyph->face_id == face_id
17957 && glyph->glyph_not_available_p == glyph_not_available_p)
17959 int two_byte_p;
17961 s->face = get_glyph_face_and_encoding (s->f, glyph,
17962 s->char2b + s->nchars,
17963 &two_byte_p);
17964 s->two_byte_p = two_byte_p;
17965 ++s->nchars;
17966 xassert (s->nchars <= end - start);
17967 s->width += glyph->pixel_width;
17968 ++glyph;
17971 s->font = s->face->font;
17972 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17974 /* If the specified font could not be loaded, use the frame's font,
17975 but record the fact that we couldn't load it in
17976 S->font_not_found_p so that we can draw rectangles for the
17977 characters of the glyph string. */
17978 if (s->font == NULL || glyph_not_available_p)
17980 s->font_not_found_p = 1;
17981 s->font = FRAME_FONT (s->f);
17984 /* Adjust base line for subscript/superscript text. */
17985 s->ybase += voffset;
17987 xassert (s->face && s->face->gc);
17988 return glyph - s->row->glyphs[s->area];
17992 /* Fill glyph string S from image glyph S->first_glyph. */
17994 static void
17995 fill_image_glyph_string (s)
17996 struct glyph_string *s;
17998 xassert (s->first_glyph->type == IMAGE_GLYPH);
17999 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18000 xassert (s->img);
18001 s->slice = s->first_glyph->slice;
18002 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18003 s->font = s->face->font;
18004 s->width = s->first_glyph->pixel_width;
18006 /* Adjust base line for subscript/superscript text. */
18007 s->ybase += s->first_glyph->voffset;
18011 /* Fill glyph string S from a sequence of stretch glyphs.
18013 ROW is the glyph row in which the glyphs are found, AREA is the
18014 area within the row. START is the index of the first glyph to
18015 consider, END is the index of the last + 1.
18017 Value is the index of the first glyph not in S. */
18019 static int
18020 fill_stretch_glyph_string (s, row, area, start, end)
18021 struct glyph_string *s;
18022 struct glyph_row *row;
18023 enum glyph_row_area area;
18024 int start, end;
18026 struct glyph *glyph, *last;
18027 int voffset, face_id;
18029 xassert (s->first_glyph->type == STRETCH_GLYPH);
18031 glyph = s->row->glyphs[s->area] + start;
18032 last = s->row->glyphs[s->area] + end;
18033 face_id = glyph->face_id;
18034 s->face = FACE_FROM_ID (s->f, face_id);
18035 s->font = s->face->font;
18036 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18037 s->width = glyph->pixel_width;
18038 voffset = glyph->voffset;
18040 for (++glyph;
18041 (glyph < last
18042 && glyph->type == STRETCH_GLYPH
18043 && glyph->voffset == voffset
18044 && glyph->face_id == face_id);
18045 ++glyph)
18046 s->width += glyph->pixel_width;
18048 /* Adjust base line for subscript/superscript text. */
18049 s->ybase += voffset;
18051 /* The case that face->gc == 0 is handled when drawing the glyph
18052 string by calling PREPARE_FACE_FOR_DISPLAY. */
18053 xassert (s->face);
18054 return glyph - s->row->glyphs[s->area];
18058 /* EXPORT for RIF:
18059 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18060 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18061 assumed to be zero. */
18063 void
18064 x_get_glyph_overhangs (glyph, f, left, right)
18065 struct glyph *glyph;
18066 struct frame *f;
18067 int *left, *right;
18069 *left = *right = 0;
18071 if (glyph->type == CHAR_GLYPH)
18073 XFontStruct *font;
18074 struct face *face;
18075 struct font_info *font_info;
18076 XChar2b char2b;
18077 XCharStruct *pcm;
18079 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18080 font = face->font;
18081 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18082 if (font /* ++KFS: Should this be font_info ? */
18083 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18085 if (pcm->rbearing > pcm->width)
18086 *right = pcm->rbearing - pcm->width;
18087 if (pcm->lbearing < 0)
18088 *left = -pcm->lbearing;
18094 /* Return the index of the first glyph preceding glyph string S that
18095 is overwritten by S because of S's left overhang. Value is -1
18096 if no glyphs are overwritten. */
18098 static int
18099 left_overwritten (s)
18100 struct glyph_string *s;
18102 int k;
18104 if (s->left_overhang)
18106 int x = 0, i;
18107 struct glyph *glyphs = s->row->glyphs[s->area];
18108 int first = s->first_glyph - glyphs;
18110 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18111 x -= glyphs[i].pixel_width;
18113 k = i + 1;
18115 else
18116 k = -1;
18118 return k;
18122 /* Return the index of the first glyph preceding glyph string S that
18123 is overwriting S because of its right overhang. Value is -1 if no
18124 glyph in front of S overwrites S. */
18126 static int
18127 left_overwriting (s)
18128 struct glyph_string *s;
18130 int i, k, x;
18131 struct glyph *glyphs = s->row->glyphs[s->area];
18132 int first = s->first_glyph - glyphs;
18134 k = -1;
18135 x = 0;
18136 for (i = first - 1; i >= 0; --i)
18138 int left, right;
18139 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18140 if (x + right > 0)
18141 k = i;
18142 x -= glyphs[i].pixel_width;
18145 return k;
18149 /* Return the index of the last glyph following glyph string S that is
18150 not overwritten by S because of S's right overhang. Value is -1 if
18151 no such glyph is found. */
18153 static int
18154 right_overwritten (s)
18155 struct glyph_string *s;
18157 int k = -1;
18159 if (s->right_overhang)
18161 int x = 0, i;
18162 struct glyph *glyphs = s->row->glyphs[s->area];
18163 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18164 int end = s->row->used[s->area];
18166 for (i = first; i < end && s->right_overhang > x; ++i)
18167 x += glyphs[i].pixel_width;
18169 k = i;
18172 return k;
18176 /* Return the index of the last glyph following glyph string S that
18177 overwrites S because of its left overhang. Value is negative
18178 if no such glyph is found. */
18180 static int
18181 right_overwriting (s)
18182 struct glyph_string *s;
18184 int i, k, x;
18185 int end = s->row->used[s->area];
18186 struct glyph *glyphs = s->row->glyphs[s->area];
18187 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18189 k = -1;
18190 x = 0;
18191 for (i = first; i < end; ++i)
18193 int left, right;
18194 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18195 if (x - left < 0)
18196 k = i;
18197 x += glyphs[i].pixel_width;
18200 return k;
18204 /* Get face and two-byte form of character C in face FACE_ID on frame
18205 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18206 means we want to display multibyte text. DISPLAY_P non-zero means
18207 make sure that X resources for the face returned are allocated.
18208 Value is a pointer to a realized face that is ready for display if
18209 DISPLAY_P is non-zero. */
18211 static INLINE struct face *
18212 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18213 struct frame *f;
18214 int c, face_id;
18215 XChar2b *char2b;
18216 int multibyte_p, display_p;
18218 struct face *face = FACE_FROM_ID (f, face_id);
18220 if (!multibyte_p)
18222 /* Unibyte case. We don't have to encode, but we have to make
18223 sure to use a face suitable for unibyte. */
18224 STORE_XCHAR2B (char2b, 0, c);
18225 face_id = FACE_FOR_CHAR (f, face, c);
18226 face = FACE_FROM_ID (f, face_id);
18228 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18230 /* Case of ASCII in a face known to fit ASCII. */
18231 STORE_XCHAR2B (char2b, 0, c);
18233 else
18235 int c1, c2, charset;
18237 /* Split characters into bytes. If c2 is -1 afterwards, C is
18238 really a one-byte character so that byte1 is zero. */
18239 SPLIT_CHAR (c, charset, c1, c2);
18240 if (c2 > 0)
18241 STORE_XCHAR2B (char2b, c1, c2);
18242 else
18243 STORE_XCHAR2B (char2b, 0, c1);
18245 /* Maybe encode the character in *CHAR2B. */
18246 if (face->font != NULL)
18248 struct font_info *font_info
18249 = FONT_INFO_FROM_ID (f, face->font_info_id);
18250 if (font_info)
18251 rif->encode_char (c, char2b, font_info, 0);
18255 /* Make sure X resources of the face are allocated. */
18256 #ifdef HAVE_X_WINDOWS
18257 if (display_p)
18258 #endif
18260 xassert (face != NULL);
18261 PREPARE_FACE_FOR_DISPLAY (f, face);
18264 return face;
18268 /* Set background width of glyph string S. START is the index of the
18269 first glyph following S. LAST_X is the right-most x-position + 1
18270 in the drawing area. */
18272 static INLINE void
18273 set_glyph_string_background_width (s, start, last_x)
18274 struct glyph_string *s;
18275 int start;
18276 int last_x;
18278 /* If the face of this glyph string has to be drawn to the end of
18279 the drawing area, set S->extends_to_end_of_line_p. */
18280 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18282 if (start == s->row->used[s->area]
18283 && s->area == TEXT_AREA
18284 && ((s->hl == DRAW_NORMAL_TEXT
18285 && (s->row->fill_line_p
18286 || s->face->background != default_face->background
18287 || s->face->stipple != default_face->stipple
18288 || s->row->mouse_face_p))
18289 || s->hl == DRAW_MOUSE_FACE
18290 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18291 && s->row->fill_line_p)))
18292 s->extends_to_end_of_line_p = 1;
18294 /* If S extends its face to the end of the line, set its
18295 background_width to the distance to the right edge of the drawing
18296 area. */
18297 if (s->extends_to_end_of_line_p)
18298 s->background_width = last_x - s->x + 1;
18299 else
18300 s->background_width = s->width;
18304 /* Compute overhangs and x-positions for glyph string S and its
18305 predecessors, or successors. X is the starting x-position for S.
18306 BACKWARD_P non-zero means process predecessors. */
18308 static void
18309 compute_overhangs_and_x (s, x, backward_p)
18310 struct glyph_string *s;
18311 int x;
18312 int backward_p;
18314 if (backward_p)
18316 while (s)
18318 if (rif->compute_glyph_string_overhangs)
18319 rif->compute_glyph_string_overhangs (s);
18320 x -= s->width;
18321 s->x = x;
18322 s = s->prev;
18325 else
18327 while (s)
18329 if (rif->compute_glyph_string_overhangs)
18330 rif->compute_glyph_string_overhangs (s);
18331 s->x = x;
18332 x += s->width;
18333 s = s->next;
18340 /* The following macros are only called from draw_glyphs below.
18341 They reference the following parameters of that function directly:
18342 `w', `row', `area', and `overlap_p'
18343 as well as the following local variables:
18344 `s', `f', and `hdc' (in W32) */
18346 #ifdef HAVE_NTGUI
18347 /* On W32, silently add local `hdc' variable to argument list of
18348 init_glyph_string. */
18349 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18350 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18351 #else
18352 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18353 init_glyph_string (s, char2b, w, row, area, start, hl)
18354 #endif
18356 /* Add a glyph string for a stretch glyph to the list of strings
18357 between HEAD and TAIL. START is the index of the stretch glyph in
18358 row area AREA of glyph row ROW. END is the index of the last glyph
18359 in that glyph row area. X is the current output position assigned
18360 to the new glyph string constructed. HL overrides that face of the
18361 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18362 is the right-most x-position of the drawing area. */
18364 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18365 and below -- keep them on one line. */
18366 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18367 do \
18369 s = (struct glyph_string *) alloca (sizeof *s); \
18370 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18371 START = fill_stretch_glyph_string (s, row, area, START, END); \
18372 append_glyph_string (&HEAD, &TAIL, s); \
18373 s->x = (X); \
18375 while (0)
18378 /* Add a glyph string for an image glyph to the list of strings
18379 between HEAD and TAIL. START is the index of the image glyph in
18380 row area AREA of glyph row ROW. END is the index of the last glyph
18381 in that glyph row area. X is the current output position assigned
18382 to the new glyph string constructed. HL overrides that face of the
18383 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18384 is the right-most x-position of the drawing area. */
18386 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18387 do \
18389 s = (struct glyph_string *) alloca (sizeof *s); \
18390 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18391 fill_image_glyph_string (s); \
18392 append_glyph_string (&HEAD, &TAIL, s); \
18393 ++START; \
18394 s->x = (X); \
18396 while (0)
18399 /* Add a glyph string for a sequence of character glyphs to the list
18400 of strings between HEAD and TAIL. START is the index of the first
18401 glyph in row area AREA of glyph row ROW that is part of the new
18402 glyph string. END is the index of the last glyph in that glyph row
18403 area. X is the current output position assigned to the new glyph
18404 string constructed. HL overrides that face of the glyph; e.g. it
18405 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18406 right-most x-position of the drawing area. */
18408 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18409 do \
18411 int c, face_id; \
18412 XChar2b *char2b; \
18414 c = (row)->glyphs[area][START].u.ch; \
18415 face_id = (row)->glyphs[area][START].face_id; \
18417 s = (struct glyph_string *) alloca (sizeof *s); \
18418 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18419 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18420 append_glyph_string (&HEAD, &TAIL, s); \
18421 s->x = (X); \
18422 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18424 while (0)
18427 /* Add a glyph string for a composite sequence to the list of strings
18428 between HEAD and TAIL. START is the index of the first glyph in
18429 row area AREA of glyph row ROW that is part of the new glyph
18430 string. END is the index of the last glyph in that glyph row area.
18431 X is the current output position assigned to the new glyph string
18432 constructed. HL overrides that face of the glyph; e.g. it is
18433 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18434 x-position of the drawing area. */
18436 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18437 do { \
18438 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18439 int face_id = (row)->glyphs[area][START].face_id; \
18440 struct face *base_face = FACE_FROM_ID (f, face_id); \
18441 struct composition *cmp = composition_table[cmp_id]; \
18442 int glyph_len = cmp->glyph_len; \
18443 XChar2b *char2b; \
18444 struct face **faces; \
18445 struct glyph_string *first_s = NULL; \
18446 int n; \
18448 base_face = base_face->ascii_face; \
18449 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18450 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18451 /* At first, fill in `char2b' and `faces'. */ \
18452 for (n = 0; n < glyph_len; n++) \
18454 int c = COMPOSITION_GLYPH (cmp, n); \
18455 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18456 faces[n] = FACE_FROM_ID (f, this_face_id); \
18457 get_char_face_and_encoding (f, c, this_face_id, \
18458 char2b + n, 1, 1); \
18461 /* Make glyph_strings for each glyph sequence that is drawable by \
18462 the same face, and append them to HEAD/TAIL. */ \
18463 for (n = 0; n < cmp->glyph_len;) \
18465 s = (struct glyph_string *) alloca (sizeof *s); \
18466 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18467 append_glyph_string (&(HEAD), &(TAIL), s); \
18468 s->cmp = cmp; \
18469 s->gidx = n; \
18470 s->x = (X); \
18472 if (n == 0) \
18473 first_s = s; \
18475 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18478 ++START; \
18479 s = first_s; \
18480 } while (0)
18483 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18484 of AREA of glyph row ROW on window W between indices START and END.
18485 HL overrides the face for drawing glyph strings, e.g. it is
18486 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18487 x-positions of the drawing area.
18489 This is an ugly monster macro construct because we must use alloca
18490 to allocate glyph strings (because draw_glyphs can be called
18491 asynchronously). */
18493 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18494 do \
18496 HEAD = TAIL = NULL; \
18497 while (START < END) \
18499 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18500 switch (first_glyph->type) \
18502 case CHAR_GLYPH: \
18503 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18504 HL, X, LAST_X); \
18505 break; \
18507 case COMPOSITE_GLYPH: \
18508 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18509 HL, X, LAST_X); \
18510 break; \
18512 case STRETCH_GLYPH: \
18513 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18514 HL, X, LAST_X); \
18515 break; \
18517 case IMAGE_GLYPH: \
18518 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18519 HL, X, LAST_X); \
18520 break; \
18522 default: \
18523 abort (); \
18526 set_glyph_string_background_width (s, START, LAST_X); \
18527 (X) += s->width; \
18530 while (0)
18533 /* Draw glyphs between START and END in AREA of ROW on window W,
18534 starting at x-position X. X is relative to AREA in W. HL is a
18535 face-override with the following meaning:
18537 DRAW_NORMAL_TEXT draw normally
18538 DRAW_CURSOR draw in cursor face
18539 DRAW_MOUSE_FACE draw in mouse face.
18540 DRAW_INVERSE_VIDEO draw in mode line face
18541 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18542 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18544 If OVERLAPS_P is non-zero, draw only the foreground of characters
18545 and clip to the physical height of ROW.
18547 Value is the x-position reached, relative to AREA of W. */
18549 static int
18550 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18551 struct window *w;
18552 int x;
18553 struct glyph_row *row;
18554 enum glyph_row_area area;
18555 int start, end;
18556 enum draw_glyphs_face hl;
18557 int overlaps_p;
18559 struct glyph_string *head, *tail;
18560 struct glyph_string *s;
18561 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18562 int last_x, area_width;
18563 int x_reached;
18564 int i, j;
18565 struct frame *f = XFRAME (WINDOW_FRAME (w));
18566 DECLARE_HDC (hdc);
18568 ALLOCATE_HDC (hdc, f);
18570 /* Let's rather be paranoid than getting a SEGV. */
18571 end = min (end, row->used[area]);
18572 start = max (0, start);
18573 start = min (end, start);
18575 /* Translate X to frame coordinates. Set last_x to the right
18576 end of the drawing area. */
18577 if (row->full_width_p)
18579 /* X is relative to the left edge of W, without scroll bars
18580 or fringes. */
18581 x += WINDOW_LEFT_EDGE_X (w);
18582 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18584 else
18586 int area_left = window_box_left (w, area);
18587 x += area_left;
18588 area_width = window_box_width (w, area);
18589 last_x = area_left + area_width;
18592 /* Build a doubly-linked list of glyph_string structures between
18593 head and tail from what we have to draw. Note that the macro
18594 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18595 the reason we use a separate variable `i'. */
18596 i = start;
18597 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18598 if (tail)
18599 x_reached = tail->x + tail->background_width;
18600 else
18601 x_reached = x;
18603 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18604 the row, redraw some glyphs in front or following the glyph
18605 strings built above. */
18606 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18608 int dummy_x = 0;
18609 struct glyph_string *h, *t;
18611 /* Compute overhangs for all glyph strings. */
18612 if (rif->compute_glyph_string_overhangs)
18613 for (s = head; s; s = s->next)
18614 rif->compute_glyph_string_overhangs (s);
18616 /* Prepend glyph strings for glyphs in front of the first glyph
18617 string that are overwritten because of the first glyph
18618 string's left overhang. The background of all strings
18619 prepended must be drawn because the first glyph string
18620 draws over it. */
18621 i = left_overwritten (head);
18622 if (i >= 0)
18624 j = i;
18625 BUILD_GLYPH_STRINGS (j, start, h, t,
18626 DRAW_NORMAL_TEXT, dummy_x, last_x);
18627 start = i;
18628 compute_overhangs_and_x (t, head->x, 1);
18629 prepend_glyph_string_lists (&head, &tail, h, t);
18630 clip_head = head;
18633 /* Prepend glyph strings for glyphs in front of the first glyph
18634 string that overwrite that glyph string because of their
18635 right overhang. For these strings, only the foreground must
18636 be drawn, because it draws over the glyph string at `head'.
18637 The background must not be drawn because this would overwrite
18638 right overhangs of preceding glyphs for which no glyph
18639 strings exist. */
18640 i = left_overwriting (head);
18641 if (i >= 0)
18643 clip_head = head;
18644 BUILD_GLYPH_STRINGS (i, start, h, t,
18645 DRAW_NORMAL_TEXT, dummy_x, last_x);
18646 for (s = h; s; s = s->next)
18647 s->background_filled_p = 1;
18648 compute_overhangs_and_x (t, head->x, 1);
18649 prepend_glyph_string_lists (&head, &tail, h, t);
18652 /* Append glyphs strings for glyphs following the last glyph
18653 string tail that are overwritten by tail. The background of
18654 these strings has to be drawn because tail's foreground draws
18655 over it. */
18656 i = right_overwritten (tail);
18657 if (i >= 0)
18659 BUILD_GLYPH_STRINGS (end, i, h, t,
18660 DRAW_NORMAL_TEXT, x, last_x);
18661 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18662 append_glyph_string_lists (&head, &tail, h, t);
18663 clip_tail = tail;
18666 /* Append glyph strings for glyphs following the last glyph
18667 string tail that overwrite tail. The foreground of such
18668 glyphs has to be drawn because it writes into the background
18669 of tail. The background must not be drawn because it could
18670 paint over the foreground of following glyphs. */
18671 i = right_overwriting (tail);
18672 if (i >= 0)
18674 clip_tail = tail;
18675 BUILD_GLYPH_STRINGS (end, i, h, t,
18676 DRAW_NORMAL_TEXT, x, last_x);
18677 for (s = h; s; s = s->next)
18678 s->background_filled_p = 1;
18679 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18680 append_glyph_string_lists (&head, &tail, h, t);
18682 if (clip_head || clip_tail)
18683 for (s = head; s; s = s->next)
18685 s->clip_head = clip_head;
18686 s->clip_tail = clip_tail;
18690 /* Draw all strings. */
18691 for (s = head; s; s = s->next)
18692 rif->draw_glyph_string (s);
18694 if (area == TEXT_AREA
18695 && !row->full_width_p
18696 /* When drawing overlapping rows, only the glyph strings'
18697 foreground is drawn, which doesn't erase a cursor
18698 completely. */
18699 && !overlaps_p)
18701 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18702 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18703 : (tail ? tail->x + tail->background_width : x));
18705 int text_left = window_box_left (w, TEXT_AREA);
18706 x0 -= text_left;
18707 x1 -= text_left;
18709 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18710 row->y, MATRIX_ROW_BOTTOM_Y (row));
18713 /* Value is the x-position up to which drawn, relative to AREA of W.
18714 This doesn't include parts drawn because of overhangs. */
18715 if (row->full_width_p)
18716 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18717 else
18718 x_reached -= window_box_left (w, area);
18720 RELEASE_HDC (hdc, f);
18722 return x_reached;
18725 /* Expand row matrix if too narrow. Don't expand if area
18726 is not present. */
18728 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18730 if (!fonts_changed_p \
18731 && (it->glyph_row->glyphs[area] \
18732 < it->glyph_row->glyphs[area + 1])) \
18734 it->w->ncols_scale_factor++; \
18735 fonts_changed_p = 1; \
18739 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18740 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18742 static INLINE void
18743 append_glyph (it)
18744 struct it *it;
18746 struct glyph *glyph;
18747 enum glyph_row_area area = it->area;
18749 xassert (it->glyph_row);
18750 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18752 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18753 if (glyph < it->glyph_row->glyphs[area + 1])
18755 glyph->charpos = CHARPOS (it->position);
18756 glyph->object = it->object;
18757 glyph->pixel_width = it->pixel_width;
18758 glyph->ascent = it->ascent;
18759 glyph->descent = it->descent;
18760 glyph->voffset = it->voffset;
18761 glyph->type = CHAR_GLYPH;
18762 glyph->multibyte_p = it->multibyte_p;
18763 glyph->left_box_line_p = it->start_of_box_run_p;
18764 glyph->right_box_line_p = it->end_of_box_run_p;
18765 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18766 || it->phys_descent > it->descent);
18767 glyph->padding_p = 0;
18768 glyph->glyph_not_available_p = it->glyph_not_available_p;
18769 glyph->face_id = it->face_id;
18770 glyph->u.ch = it->char_to_display;
18771 glyph->slice = null_glyph_slice;
18772 glyph->font_type = FONT_TYPE_UNKNOWN;
18773 ++it->glyph_row->used[area];
18775 else
18776 IT_EXPAND_MATRIX_WIDTH (it, area);
18779 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18780 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18782 static INLINE void
18783 append_composite_glyph (it)
18784 struct it *it;
18786 struct glyph *glyph;
18787 enum glyph_row_area area = it->area;
18789 xassert (it->glyph_row);
18791 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18792 if (glyph < it->glyph_row->glyphs[area + 1])
18794 glyph->charpos = CHARPOS (it->position);
18795 glyph->object = it->object;
18796 glyph->pixel_width = it->pixel_width;
18797 glyph->ascent = it->ascent;
18798 glyph->descent = it->descent;
18799 glyph->voffset = it->voffset;
18800 glyph->type = COMPOSITE_GLYPH;
18801 glyph->multibyte_p = it->multibyte_p;
18802 glyph->left_box_line_p = it->start_of_box_run_p;
18803 glyph->right_box_line_p = it->end_of_box_run_p;
18804 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18805 || it->phys_descent > it->descent);
18806 glyph->padding_p = 0;
18807 glyph->glyph_not_available_p = 0;
18808 glyph->face_id = it->face_id;
18809 glyph->u.cmp_id = it->cmp_id;
18810 glyph->slice = null_glyph_slice;
18811 glyph->font_type = FONT_TYPE_UNKNOWN;
18812 ++it->glyph_row->used[area];
18814 else
18815 IT_EXPAND_MATRIX_WIDTH (it, area);
18819 /* Change IT->ascent and IT->height according to the setting of
18820 IT->voffset. */
18822 static INLINE void
18823 take_vertical_position_into_account (it)
18824 struct it *it;
18826 if (it->voffset)
18828 if (it->voffset < 0)
18829 /* Increase the ascent so that we can display the text higher
18830 in the line. */
18831 it->ascent -= it->voffset;
18832 else
18833 /* Increase the descent so that we can display the text lower
18834 in the line. */
18835 it->descent += it->voffset;
18840 /* Produce glyphs/get display metrics for the image IT is loaded with.
18841 See the description of struct display_iterator in dispextern.h for
18842 an overview of struct display_iterator. */
18844 static void
18845 produce_image_glyph (it)
18846 struct it *it;
18848 struct image *img;
18849 struct face *face;
18850 int glyph_ascent;
18851 struct glyph_slice slice;
18853 xassert (it->what == IT_IMAGE);
18855 face = FACE_FROM_ID (it->f, it->face_id);
18856 xassert (face);
18857 /* Make sure X resources of the face is loaded. */
18858 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18860 if (it->image_id < 0)
18862 /* Fringe bitmap. */
18863 it->ascent = it->phys_ascent = 0;
18864 it->descent = it->phys_descent = 0;
18865 it->pixel_width = 0;
18866 it->nglyphs = 0;
18867 return;
18870 img = IMAGE_FROM_ID (it->f, it->image_id);
18871 xassert (img);
18872 /* Make sure X resources of the image is loaded. */
18873 prepare_image_for_display (it->f, img);
18875 slice.x = slice.y = 0;
18876 slice.width = img->width;
18877 slice.height = img->height;
18879 if (INTEGERP (it->slice.x))
18880 slice.x = XINT (it->slice.x);
18881 else if (FLOATP (it->slice.x))
18882 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18884 if (INTEGERP (it->slice.y))
18885 slice.y = XINT (it->slice.y);
18886 else if (FLOATP (it->slice.y))
18887 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18889 if (INTEGERP (it->slice.width))
18890 slice.width = XINT (it->slice.width);
18891 else if (FLOATP (it->slice.width))
18892 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18894 if (INTEGERP (it->slice.height))
18895 slice.height = XINT (it->slice.height);
18896 else if (FLOATP (it->slice.height))
18897 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18899 if (slice.x >= img->width)
18900 slice.x = img->width;
18901 if (slice.y >= img->height)
18902 slice.y = img->height;
18903 if (slice.x + slice.width >= img->width)
18904 slice.width = img->width - slice.x;
18905 if (slice.y + slice.height > img->height)
18906 slice.height = img->height - slice.y;
18908 if (slice.width == 0 || slice.height == 0)
18909 return;
18911 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18913 it->descent = slice.height - glyph_ascent;
18914 if (slice.y == 0)
18915 it->descent += img->vmargin;
18916 if (slice.y + slice.height == img->height)
18917 it->descent += img->vmargin;
18918 it->phys_descent = it->descent;
18920 it->pixel_width = slice.width;
18921 if (slice.x == 0)
18922 it->pixel_width += img->hmargin;
18923 if (slice.x + slice.width == img->width)
18924 it->pixel_width += img->hmargin;
18926 /* It's quite possible for images to have an ascent greater than
18927 their height, so don't get confused in that case. */
18928 if (it->descent < 0)
18929 it->descent = 0;
18931 #if 0 /* this breaks image tiling */
18932 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18933 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18934 if (face_ascent > it->ascent)
18935 it->ascent = it->phys_ascent = face_ascent;
18936 #endif
18938 it->nglyphs = 1;
18940 if (face->box != FACE_NO_BOX)
18942 if (face->box_line_width > 0)
18944 if (slice.y == 0)
18945 it->ascent += face->box_line_width;
18946 if (slice.y + slice.height == img->height)
18947 it->descent += face->box_line_width;
18950 if (it->start_of_box_run_p && slice.x == 0)
18951 it->pixel_width += abs (face->box_line_width);
18952 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18953 it->pixel_width += abs (face->box_line_width);
18956 take_vertical_position_into_account (it);
18958 if (it->glyph_row)
18960 struct glyph *glyph;
18961 enum glyph_row_area area = it->area;
18963 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18964 if (glyph < it->glyph_row->glyphs[area + 1])
18966 glyph->charpos = CHARPOS (it->position);
18967 glyph->object = it->object;
18968 glyph->pixel_width = it->pixel_width;
18969 glyph->ascent = glyph_ascent;
18970 glyph->descent = it->descent;
18971 glyph->voffset = it->voffset;
18972 glyph->type = IMAGE_GLYPH;
18973 glyph->multibyte_p = it->multibyte_p;
18974 glyph->left_box_line_p = it->start_of_box_run_p;
18975 glyph->right_box_line_p = it->end_of_box_run_p;
18976 glyph->overlaps_vertically_p = 0;
18977 glyph->padding_p = 0;
18978 glyph->glyph_not_available_p = 0;
18979 glyph->face_id = it->face_id;
18980 glyph->u.img_id = img->id;
18981 glyph->slice = slice;
18982 glyph->font_type = FONT_TYPE_UNKNOWN;
18983 ++it->glyph_row->used[area];
18985 else
18986 IT_EXPAND_MATRIX_WIDTH (it, area);
18991 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18992 of the glyph, WIDTH and HEIGHT are the width and height of the
18993 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18995 static void
18996 append_stretch_glyph (it, object, width, height, ascent)
18997 struct it *it;
18998 Lisp_Object object;
18999 int width, height;
19000 int ascent;
19002 struct glyph *glyph;
19003 enum glyph_row_area area = it->area;
19005 xassert (ascent >= 0 && ascent <= height);
19007 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19008 if (glyph < it->glyph_row->glyphs[area + 1])
19010 glyph->charpos = CHARPOS (it->position);
19011 glyph->object = object;
19012 glyph->pixel_width = width;
19013 glyph->ascent = ascent;
19014 glyph->descent = height - ascent;
19015 glyph->voffset = it->voffset;
19016 glyph->type = STRETCH_GLYPH;
19017 glyph->multibyte_p = it->multibyte_p;
19018 glyph->left_box_line_p = it->start_of_box_run_p;
19019 glyph->right_box_line_p = it->end_of_box_run_p;
19020 glyph->overlaps_vertically_p = 0;
19021 glyph->padding_p = 0;
19022 glyph->glyph_not_available_p = 0;
19023 glyph->face_id = it->face_id;
19024 glyph->u.stretch.ascent = ascent;
19025 glyph->u.stretch.height = height;
19026 glyph->slice = null_glyph_slice;
19027 glyph->font_type = FONT_TYPE_UNKNOWN;
19028 ++it->glyph_row->used[area];
19030 else
19031 IT_EXPAND_MATRIX_WIDTH (it, area);
19035 /* Produce a stretch glyph for iterator IT. IT->object is the value
19036 of the glyph property displayed. The value must be a list
19037 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19038 being recognized:
19040 1. `:width WIDTH' specifies that the space should be WIDTH *
19041 canonical char width wide. WIDTH may be an integer or floating
19042 point number.
19044 2. `:relative-width FACTOR' specifies that the width of the stretch
19045 should be computed from the width of the first character having the
19046 `glyph' property, and should be FACTOR times that width.
19048 3. `:align-to HPOS' specifies that the space should be wide enough
19049 to reach HPOS, a value in canonical character units.
19051 Exactly one of the above pairs must be present.
19053 4. `:height HEIGHT' specifies that the height of the stretch produced
19054 should be HEIGHT, measured in canonical character units.
19056 5. `:relative-height FACTOR' specifies that the height of the
19057 stretch should be FACTOR times the height of the characters having
19058 the glyph property.
19060 Either none or exactly one of 4 or 5 must be present.
19062 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19063 of the stretch should be used for the ascent of the stretch.
19064 ASCENT must be in the range 0 <= ASCENT <= 100. */
19066 static void
19067 produce_stretch_glyph (it)
19068 struct it *it;
19070 /* (space :width WIDTH :height HEIGHT ...) */
19071 Lisp_Object prop, plist;
19072 int width = 0, height = 0, align_to = -1;
19073 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19074 int ascent = 0;
19075 double tem;
19076 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19077 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19079 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19081 /* List should start with `space'. */
19082 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19083 plist = XCDR (it->object);
19085 /* Compute the width of the stretch. */
19086 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19087 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19089 /* Absolute width `:width WIDTH' specified and valid. */
19090 zero_width_ok_p = 1;
19091 width = (int)tem;
19093 else if (prop = Fplist_get (plist, QCrelative_width),
19094 NUMVAL (prop) > 0)
19096 /* Relative width `:relative-width FACTOR' specified and valid.
19097 Compute the width of the characters having the `glyph'
19098 property. */
19099 struct it it2;
19100 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19102 it2 = *it;
19103 if (it->multibyte_p)
19105 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19106 - IT_BYTEPOS (*it));
19107 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19109 else
19110 it2.c = *p, it2.len = 1;
19112 it2.glyph_row = NULL;
19113 it2.what = IT_CHARACTER;
19114 x_produce_glyphs (&it2);
19115 width = NUMVAL (prop) * it2.pixel_width;
19117 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19118 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19120 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19121 align_to = (align_to < 0
19123 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19124 else if (align_to < 0)
19125 align_to = window_box_left_offset (it->w, TEXT_AREA);
19126 width = max (0, (int)tem + align_to - it->current_x);
19127 zero_width_ok_p = 1;
19129 else
19130 /* Nothing specified -> width defaults to canonical char width. */
19131 width = FRAME_COLUMN_WIDTH (it->f);
19133 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19134 width = 1;
19136 /* Compute height. */
19137 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19138 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19140 height = (int)tem;
19141 zero_height_ok_p = 1;
19143 else if (prop = Fplist_get (plist, QCrelative_height),
19144 NUMVAL (prop) > 0)
19145 height = FONT_HEIGHT (font) * NUMVAL (prop);
19146 else
19147 height = FONT_HEIGHT (font);
19149 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19150 height = 1;
19152 /* Compute percentage of height used for ascent. If
19153 `:ascent ASCENT' is present and valid, use that. Otherwise,
19154 derive the ascent from the font in use. */
19155 if (prop = Fplist_get (plist, QCascent),
19156 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19157 ascent = height * NUMVAL (prop) / 100.0;
19158 else if (!NILP (prop)
19159 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19160 ascent = min (max (0, (int)tem), height);
19161 else
19162 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19164 if (width > 0 && height > 0 && it->glyph_row)
19166 Lisp_Object object = it->stack[it->sp - 1].string;
19167 if (!STRINGP (object))
19168 object = it->w->buffer;
19169 append_stretch_glyph (it, object, width, height, ascent);
19172 it->pixel_width = width;
19173 it->ascent = it->phys_ascent = ascent;
19174 it->descent = it->phys_descent = height - it->ascent;
19175 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19177 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19179 if (face->box_line_width > 0)
19181 it->ascent += face->box_line_width;
19182 it->descent += face->box_line_width;
19185 if (it->start_of_box_run_p)
19186 it->pixel_width += abs (face->box_line_width);
19187 if (it->end_of_box_run_p)
19188 it->pixel_width += abs (face->box_line_width);
19191 take_vertical_position_into_account (it);
19194 /* Get line-height and line-spacing property at point.
19195 If line-height has format (HEIGHT TOTAL), return TOTAL
19196 in TOTAL_HEIGHT. */
19198 static Lisp_Object
19199 get_line_height_property (it, prop)
19200 struct it *it;
19201 Lisp_Object prop;
19203 Lisp_Object position;
19205 if (STRINGP (it->object))
19206 position = make_number (IT_STRING_CHARPOS (*it));
19207 else if (BUFFERP (it->object))
19208 position = make_number (IT_CHARPOS (*it));
19209 else
19210 return Qnil;
19212 return Fget_char_property (position, prop, it->object);
19215 /* Calculate line-height and line-spacing properties.
19216 An integer value specifies explicit pixel value.
19217 A float value specifies relative value to current face height.
19218 A cons (float . face-name) specifies relative value to
19219 height of specified face font.
19221 Returns height in pixels, or nil. */
19224 static Lisp_Object
19225 calc_line_height_property (it, val, font, boff, override)
19226 struct it *it;
19227 Lisp_Object val;
19228 XFontStruct *font;
19229 int boff, override;
19231 Lisp_Object face_name = Qnil;
19232 int ascent, descent, height;
19234 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19235 return val;
19237 if (CONSP (val))
19239 face_name = XCAR (val);
19240 val = XCDR (val);
19241 if (!NUMBERP (val))
19242 val = make_number (1);
19243 if (NILP (face_name))
19245 height = it->ascent + it->descent;
19246 goto scale;
19250 if (NILP (face_name))
19252 font = FRAME_FONT (it->f);
19253 boff = FRAME_BASELINE_OFFSET (it->f);
19255 else if (EQ (face_name, Qt))
19257 override = 0;
19259 else
19261 int face_id;
19262 struct face *face;
19263 struct font_info *font_info;
19265 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19266 if (face_id < 0)
19267 return make_number (-1);
19269 face = FACE_FROM_ID (it->f, face_id);
19270 font = face->font;
19271 if (font == NULL)
19272 return make_number (-1);
19274 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19275 boff = font_info->baseline_offset;
19276 if (font_info->vertical_centering)
19277 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19280 ascent = FONT_BASE (font) + boff;
19281 descent = FONT_DESCENT (font) - boff;
19283 if (override)
19285 it->override_ascent = ascent;
19286 it->override_descent = descent;
19287 it->override_boff = boff;
19290 height = ascent + descent;
19292 scale:
19293 if (FLOATP (val))
19294 height = (int)(XFLOAT_DATA (val) * height);
19295 else if (INTEGERP (val))
19296 height *= XINT (val);
19298 return make_number (height);
19302 /* RIF:
19303 Produce glyphs/get display metrics for the display element IT is
19304 loaded with. See the description of struct display_iterator in
19305 dispextern.h for an overview of struct display_iterator. */
19307 void
19308 x_produce_glyphs (it)
19309 struct it *it;
19311 int extra_line_spacing = it->extra_line_spacing;
19313 it->glyph_not_available_p = 0;
19315 if (it->what == IT_CHARACTER)
19317 XChar2b char2b;
19318 XFontStruct *font;
19319 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19320 XCharStruct *pcm;
19321 int font_not_found_p;
19322 struct font_info *font_info;
19323 int boff; /* baseline offset */
19324 /* We may change it->multibyte_p upon unibyte<->multibyte
19325 conversion. So, save the current value now and restore it
19326 later.
19328 Note: It seems that we don't have to record multibyte_p in
19329 struct glyph because the character code itself tells if or
19330 not the character is multibyte. Thus, in the future, we must
19331 consider eliminating the field `multibyte_p' in the struct
19332 glyph. */
19333 int saved_multibyte_p = it->multibyte_p;
19335 /* Maybe translate single-byte characters to multibyte, or the
19336 other way. */
19337 it->char_to_display = it->c;
19338 if (!ASCII_BYTE_P (it->c))
19340 if (unibyte_display_via_language_environment
19341 && SINGLE_BYTE_CHAR_P (it->c)
19342 && (it->c >= 0240
19343 || !NILP (Vnonascii_translation_table)))
19345 it->char_to_display = unibyte_char_to_multibyte (it->c);
19346 it->multibyte_p = 1;
19347 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19348 face = FACE_FROM_ID (it->f, it->face_id);
19350 else if (!SINGLE_BYTE_CHAR_P (it->c)
19351 && !it->multibyte_p)
19353 it->multibyte_p = 1;
19354 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19355 face = FACE_FROM_ID (it->f, it->face_id);
19359 /* Get font to use. Encode IT->char_to_display. */
19360 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19361 &char2b, it->multibyte_p, 0);
19362 font = face->font;
19364 /* When no suitable font found, use the default font. */
19365 font_not_found_p = font == NULL;
19366 if (font_not_found_p)
19368 font = FRAME_FONT (it->f);
19369 boff = FRAME_BASELINE_OFFSET (it->f);
19370 font_info = NULL;
19372 else
19374 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19375 boff = font_info->baseline_offset;
19376 if (font_info->vertical_centering)
19377 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19380 if (it->char_to_display >= ' '
19381 && (!it->multibyte_p || it->char_to_display < 128))
19383 /* Either unibyte or ASCII. */
19384 int stretched_p;
19386 it->nglyphs = 1;
19388 pcm = rif->per_char_metric (font, &char2b,
19389 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19391 if (it->override_ascent >= 0)
19393 it->ascent = it->override_ascent;
19394 it->descent = it->override_descent;
19395 boff = it->override_boff;
19397 else
19399 it->ascent = FONT_BASE (font) + boff;
19400 it->descent = FONT_DESCENT (font) - boff;
19403 if (pcm)
19405 it->phys_ascent = pcm->ascent + boff;
19406 it->phys_descent = pcm->descent - boff;
19407 it->pixel_width = pcm->width;
19409 else
19411 it->glyph_not_available_p = 1;
19412 it->phys_ascent = it->ascent;
19413 it->phys_descent = it->descent;
19414 it->pixel_width = FONT_WIDTH (font);
19417 if (it->constrain_row_ascent_descent_p)
19419 if (it->descent > it->max_descent)
19421 it->ascent += it->descent - it->max_descent;
19422 it->descent = it->max_descent;
19424 if (it->ascent > it->max_ascent)
19426 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19427 it->ascent = it->max_ascent;
19429 it->phys_ascent = min (it->phys_ascent, it->ascent);
19430 it->phys_descent = min (it->phys_descent, it->descent);
19431 extra_line_spacing = 0;
19434 /* If this is a space inside a region of text with
19435 `space-width' property, change its width. */
19436 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19437 if (stretched_p)
19438 it->pixel_width *= XFLOATINT (it->space_width);
19440 /* If face has a box, add the box thickness to the character
19441 height. If character has a box line to the left and/or
19442 right, add the box line width to the character's width. */
19443 if (face->box != FACE_NO_BOX)
19445 int thick = face->box_line_width;
19447 if (thick > 0)
19449 it->ascent += thick;
19450 it->descent += thick;
19452 else
19453 thick = -thick;
19455 if (it->start_of_box_run_p)
19456 it->pixel_width += thick;
19457 if (it->end_of_box_run_p)
19458 it->pixel_width += thick;
19461 /* If face has an overline, add the height of the overline
19462 (1 pixel) and a 1 pixel margin to the character height. */
19463 if (face->overline_p)
19464 it->ascent += 2;
19466 if (it->constrain_row_ascent_descent_p)
19468 if (it->ascent > it->max_ascent)
19469 it->ascent = it->max_ascent;
19470 if (it->descent > it->max_descent)
19471 it->descent = it->max_descent;
19474 take_vertical_position_into_account (it);
19476 /* If we have to actually produce glyphs, do it. */
19477 if (it->glyph_row)
19479 if (stretched_p)
19481 /* Translate a space with a `space-width' property
19482 into a stretch glyph. */
19483 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19484 / FONT_HEIGHT (font));
19485 append_stretch_glyph (it, it->object, it->pixel_width,
19486 it->ascent + it->descent, ascent);
19488 else
19489 append_glyph (it);
19491 /* If characters with lbearing or rbearing are displayed
19492 in this line, record that fact in a flag of the
19493 glyph row. This is used to optimize X output code. */
19494 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19495 it->glyph_row->contains_overlapping_glyphs_p = 1;
19498 else if (it->char_to_display == '\n')
19500 /* A newline has no width but we need the height of the line.
19501 But if previous part of the line set a height, don't
19502 increase that height */
19504 Lisp_Object height;
19505 Lisp_Object total_height = Qnil;
19507 it->override_ascent = -1;
19508 it->pixel_width = 0;
19509 it->nglyphs = 0;
19511 height = get_line_height_property(it, Qline_height);
19512 /* Split (line-height total-height) list */
19513 if (CONSP (height)
19514 && CONSP (XCDR (height))
19515 && NILP (XCDR (XCDR (height))))
19517 total_height = XCAR (XCDR (height));
19518 height = XCAR (height);
19520 height = calc_line_height_property(it, height, font, boff, 1);
19522 if (it->override_ascent >= 0)
19524 it->ascent = it->override_ascent;
19525 it->descent = it->override_descent;
19526 boff = it->override_boff;
19528 else
19530 it->ascent = FONT_BASE (font) + boff;
19531 it->descent = FONT_DESCENT (font) - boff;
19534 if (EQ (height, Qt))
19536 if (it->descent > it->max_descent)
19538 it->ascent += it->descent - it->max_descent;
19539 it->descent = it->max_descent;
19541 if (it->ascent > it->max_ascent)
19543 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19544 it->ascent = it->max_ascent;
19546 it->phys_ascent = min (it->phys_ascent, it->ascent);
19547 it->phys_descent = min (it->phys_descent, it->descent);
19548 it->constrain_row_ascent_descent_p = 1;
19549 extra_line_spacing = 0;
19551 else
19553 Lisp_Object spacing;
19555 it->phys_ascent = it->ascent;
19556 it->phys_descent = it->descent;
19558 if ((it->max_ascent > 0 || it->max_descent > 0)
19559 && face->box != FACE_NO_BOX
19560 && face->box_line_width > 0)
19562 it->ascent += face->box_line_width;
19563 it->descent += face->box_line_width;
19565 if (!NILP (height)
19566 && XINT (height) > it->ascent + it->descent)
19567 it->ascent = XINT (height) - it->descent;
19569 if (!NILP (total_height))
19570 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19571 else
19573 spacing = get_line_height_property(it, Qline_spacing);
19574 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19576 if (INTEGERP (spacing))
19578 extra_line_spacing = XINT (spacing);
19579 if (!NILP (total_height))
19580 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19584 else if (it->char_to_display == '\t')
19586 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19587 int x = it->current_x + it->continuation_lines_width;
19588 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19590 /* If the distance from the current position to the next tab
19591 stop is less than a space character width, use the
19592 tab stop after that. */
19593 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19594 next_tab_x += tab_width;
19596 it->pixel_width = next_tab_x - x;
19597 it->nglyphs = 1;
19598 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19599 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19601 if (it->glyph_row)
19603 append_stretch_glyph (it, it->object, it->pixel_width,
19604 it->ascent + it->descent, it->ascent);
19607 else
19609 /* A multi-byte character. Assume that the display width of the
19610 character is the width of the character multiplied by the
19611 width of the font. */
19613 /* If we found a font, this font should give us the right
19614 metrics. If we didn't find a font, use the frame's
19615 default font and calculate the width of the character
19616 from the charset width; this is what old redisplay code
19617 did. */
19619 pcm = rif->per_char_metric (font, &char2b,
19620 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19622 if (font_not_found_p || !pcm)
19624 int charset = CHAR_CHARSET (it->char_to_display);
19626 it->glyph_not_available_p = 1;
19627 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19628 * CHARSET_WIDTH (charset));
19629 it->phys_ascent = FONT_BASE (font) + boff;
19630 it->phys_descent = FONT_DESCENT (font) - boff;
19632 else
19634 it->pixel_width = pcm->width;
19635 it->phys_ascent = pcm->ascent + boff;
19636 it->phys_descent = pcm->descent - boff;
19637 if (it->glyph_row
19638 && (pcm->lbearing < 0
19639 || pcm->rbearing > pcm->width))
19640 it->glyph_row->contains_overlapping_glyphs_p = 1;
19642 it->nglyphs = 1;
19643 it->ascent = FONT_BASE (font) + boff;
19644 it->descent = FONT_DESCENT (font) - boff;
19645 if (face->box != FACE_NO_BOX)
19647 int thick = face->box_line_width;
19649 if (thick > 0)
19651 it->ascent += thick;
19652 it->descent += thick;
19654 else
19655 thick = - thick;
19657 if (it->start_of_box_run_p)
19658 it->pixel_width += thick;
19659 if (it->end_of_box_run_p)
19660 it->pixel_width += thick;
19663 /* If face has an overline, add the height of the overline
19664 (1 pixel) and a 1 pixel margin to the character height. */
19665 if (face->overline_p)
19666 it->ascent += 2;
19668 take_vertical_position_into_account (it);
19670 if (it->glyph_row)
19671 append_glyph (it);
19673 it->multibyte_p = saved_multibyte_p;
19675 else if (it->what == IT_COMPOSITION)
19677 /* Note: A composition is represented as one glyph in the
19678 glyph matrix. There are no padding glyphs. */
19679 XChar2b char2b;
19680 XFontStruct *font;
19681 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19682 XCharStruct *pcm;
19683 int font_not_found_p;
19684 struct font_info *font_info;
19685 int boff; /* baseline offset */
19686 struct composition *cmp = composition_table[it->cmp_id];
19688 /* Maybe translate single-byte characters to multibyte. */
19689 it->char_to_display = it->c;
19690 if (unibyte_display_via_language_environment
19691 && SINGLE_BYTE_CHAR_P (it->c)
19692 && (it->c >= 0240
19693 || (it->c >= 0200
19694 && !NILP (Vnonascii_translation_table))))
19696 it->char_to_display = unibyte_char_to_multibyte (it->c);
19699 /* Get face and font to use. Encode IT->char_to_display. */
19700 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19701 face = FACE_FROM_ID (it->f, it->face_id);
19702 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19703 &char2b, it->multibyte_p, 0);
19704 font = face->font;
19706 /* When no suitable font found, use the default font. */
19707 font_not_found_p = font == NULL;
19708 if (font_not_found_p)
19710 font = FRAME_FONT (it->f);
19711 boff = FRAME_BASELINE_OFFSET (it->f);
19712 font_info = NULL;
19714 else
19716 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19717 boff = font_info->baseline_offset;
19718 if (font_info->vertical_centering)
19719 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19722 /* There are no padding glyphs, so there is only one glyph to
19723 produce for the composition. Important is that pixel_width,
19724 ascent and descent are the values of what is drawn by
19725 draw_glyphs (i.e. the values of the overall glyphs composed). */
19726 it->nglyphs = 1;
19728 /* If we have not yet calculated pixel size data of glyphs of
19729 the composition for the current face font, calculate them
19730 now. Theoretically, we have to check all fonts for the
19731 glyphs, but that requires much time and memory space. So,
19732 here we check only the font of the first glyph. This leads
19733 to incorrect display very rarely, and C-l (recenter) can
19734 correct the display anyway. */
19735 if (cmp->font != (void *) font)
19737 /* Ascent and descent of the font of the first character of
19738 this composition (adjusted by baseline offset). Ascent
19739 and descent of overall glyphs should not be less than
19740 them respectively. */
19741 int font_ascent = FONT_BASE (font) + boff;
19742 int font_descent = FONT_DESCENT (font) - boff;
19743 /* Bounding box of the overall glyphs. */
19744 int leftmost, rightmost, lowest, highest;
19745 int i, width, ascent, descent;
19747 cmp->font = (void *) font;
19749 /* Initialize the bounding box. */
19750 if (font_info
19751 && (pcm = rif->per_char_metric (font, &char2b,
19752 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19754 width = pcm->width;
19755 ascent = pcm->ascent;
19756 descent = pcm->descent;
19758 else
19760 width = FONT_WIDTH (font);
19761 ascent = FONT_BASE (font);
19762 descent = FONT_DESCENT (font);
19765 rightmost = width;
19766 lowest = - descent + boff;
19767 highest = ascent + boff;
19768 leftmost = 0;
19770 if (font_info
19771 && font_info->default_ascent
19772 && CHAR_TABLE_P (Vuse_default_ascent)
19773 && !NILP (Faref (Vuse_default_ascent,
19774 make_number (it->char_to_display))))
19775 highest = font_info->default_ascent + boff;
19777 /* Draw the first glyph at the normal position. It may be
19778 shifted to right later if some other glyphs are drawn at
19779 the left. */
19780 cmp->offsets[0] = 0;
19781 cmp->offsets[1] = boff;
19783 /* Set cmp->offsets for the remaining glyphs. */
19784 for (i = 1; i < cmp->glyph_len; i++)
19786 int left, right, btm, top;
19787 int ch = COMPOSITION_GLYPH (cmp, i);
19788 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19790 face = FACE_FROM_ID (it->f, face_id);
19791 get_char_face_and_encoding (it->f, ch, face->id,
19792 &char2b, it->multibyte_p, 0);
19793 font = face->font;
19794 if (font == NULL)
19796 font = FRAME_FONT (it->f);
19797 boff = FRAME_BASELINE_OFFSET (it->f);
19798 font_info = NULL;
19800 else
19802 font_info
19803 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19804 boff = font_info->baseline_offset;
19805 if (font_info->vertical_centering)
19806 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19809 if (font_info
19810 && (pcm = rif->per_char_metric (font, &char2b,
19811 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19813 width = pcm->width;
19814 ascent = pcm->ascent;
19815 descent = pcm->descent;
19817 else
19819 width = FONT_WIDTH (font);
19820 ascent = 1;
19821 descent = 0;
19824 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19826 /* Relative composition with or without
19827 alternate chars. */
19828 left = (leftmost + rightmost - width) / 2;
19829 btm = - descent + boff;
19830 if (font_info && font_info->relative_compose
19831 && (! CHAR_TABLE_P (Vignore_relative_composition)
19832 || NILP (Faref (Vignore_relative_composition,
19833 make_number (ch)))))
19836 if (- descent >= font_info->relative_compose)
19837 /* One extra pixel between two glyphs. */
19838 btm = highest + 1;
19839 else if (ascent <= 0)
19840 /* One extra pixel between two glyphs. */
19841 btm = lowest - 1 - ascent - descent;
19844 else
19846 /* A composition rule is specified by an integer
19847 value that encodes global and new reference
19848 points (GREF and NREF). GREF and NREF are
19849 specified by numbers as below:
19851 0---1---2 -- ascent
19855 9--10--11 -- center
19857 ---3---4---5--- baseline
19859 6---7---8 -- descent
19861 int rule = COMPOSITION_RULE (cmp, i);
19862 int gref, nref, grefx, grefy, nrefx, nrefy;
19864 COMPOSITION_DECODE_RULE (rule, gref, nref);
19865 grefx = gref % 3, nrefx = nref % 3;
19866 grefy = gref / 3, nrefy = nref / 3;
19868 left = (leftmost
19869 + grefx * (rightmost - leftmost) / 2
19870 - nrefx * width / 2);
19871 btm = ((grefy == 0 ? highest
19872 : grefy == 1 ? 0
19873 : grefy == 2 ? lowest
19874 : (highest + lowest) / 2)
19875 - (nrefy == 0 ? ascent + descent
19876 : nrefy == 1 ? descent - boff
19877 : nrefy == 2 ? 0
19878 : (ascent + descent) / 2));
19881 cmp->offsets[i * 2] = left;
19882 cmp->offsets[i * 2 + 1] = btm + descent;
19884 /* Update the bounding box of the overall glyphs. */
19885 right = left + width;
19886 top = btm + descent + ascent;
19887 if (left < leftmost)
19888 leftmost = left;
19889 if (right > rightmost)
19890 rightmost = right;
19891 if (top > highest)
19892 highest = top;
19893 if (btm < lowest)
19894 lowest = btm;
19897 /* If there are glyphs whose x-offsets are negative,
19898 shift all glyphs to the right and make all x-offsets
19899 non-negative. */
19900 if (leftmost < 0)
19902 for (i = 0; i < cmp->glyph_len; i++)
19903 cmp->offsets[i * 2] -= leftmost;
19904 rightmost -= leftmost;
19907 cmp->pixel_width = rightmost;
19908 cmp->ascent = highest;
19909 cmp->descent = - lowest;
19910 if (cmp->ascent < font_ascent)
19911 cmp->ascent = font_ascent;
19912 if (cmp->descent < font_descent)
19913 cmp->descent = font_descent;
19916 it->pixel_width = cmp->pixel_width;
19917 it->ascent = it->phys_ascent = cmp->ascent;
19918 it->descent = it->phys_descent = cmp->descent;
19920 if (face->box != FACE_NO_BOX)
19922 int thick = face->box_line_width;
19924 if (thick > 0)
19926 it->ascent += thick;
19927 it->descent += thick;
19929 else
19930 thick = - thick;
19932 if (it->start_of_box_run_p)
19933 it->pixel_width += thick;
19934 if (it->end_of_box_run_p)
19935 it->pixel_width += thick;
19938 /* If face has an overline, add the height of the overline
19939 (1 pixel) and a 1 pixel margin to the character height. */
19940 if (face->overline_p)
19941 it->ascent += 2;
19943 take_vertical_position_into_account (it);
19945 if (it->glyph_row)
19946 append_composite_glyph (it);
19948 else if (it->what == IT_IMAGE)
19949 produce_image_glyph (it);
19950 else if (it->what == IT_STRETCH)
19951 produce_stretch_glyph (it);
19953 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19954 because this isn't true for images with `:ascent 100'. */
19955 xassert (it->ascent >= 0 && it->descent >= 0);
19956 if (it->area == TEXT_AREA)
19957 it->current_x += it->pixel_width;
19959 if (extra_line_spacing > 0)
19961 it->descent += extra_line_spacing;
19962 if (extra_line_spacing > it->max_extra_line_spacing)
19963 it->max_extra_line_spacing = extra_line_spacing;
19966 it->max_ascent = max (it->max_ascent, it->ascent);
19967 it->max_descent = max (it->max_descent, it->descent);
19968 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19969 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19972 /* EXPORT for RIF:
19973 Output LEN glyphs starting at START at the nominal cursor position.
19974 Advance the nominal cursor over the text. The global variable
19975 updated_window contains the window being updated, updated_row is
19976 the glyph row being updated, and updated_area is the area of that
19977 row being updated. */
19979 void
19980 x_write_glyphs (start, len)
19981 struct glyph *start;
19982 int len;
19984 int x, hpos;
19986 xassert (updated_window && updated_row);
19987 BLOCK_INPUT;
19989 /* Write glyphs. */
19991 hpos = start - updated_row->glyphs[updated_area];
19992 x = draw_glyphs (updated_window, output_cursor.x,
19993 updated_row, updated_area,
19994 hpos, hpos + len,
19995 DRAW_NORMAL_TEXT, 0);
19997 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19998 if (updated_area == TEXT_AREA
19999 && updated_window->phys_cursor_on_p
20000 && updated_window->phys_cursor.vpos == output_cursor.vpos
20001 && updated_window->phys_cursor.hpos >= hpos
20002 && updated_window->phys_cursor.hpos < hpos + len)
20003 updated_window->phys_cursor_on_p = 0;
20005 UNBLOCK_INPUT;
20007 /* Advance the output cursor. */
20008 output_cursor.hpos += len;
20009 output_cursor.x = x;
20013 /* EXPORT for RIF:
20014 Insert LEN glyphs from START at the nominal cursor position. */
20016 void
20017 x_insert_glyphs (start, len)
20018 struct glyph *start;
20019 int len;
20021 struct frame *f;
20022 struct window *w;
20023 int line_height, shift_by_width, shifted_region_width;
20024 struct glyph_row *row;
20025 struct glyph *glyph;
20026 int frame_x, frame_y, hpos;
20028 xassert (updated_window && updated_row);
20029 BLOCK_INPUT;
20030 w = updated_window;
20031 f = XFRAME (WINDOW_FRAME (w));
20033 /* Get the height of the line we are in. */
20034 row = updated_row;
20035 line_height = row->height;
20037 /* Get the width of the glyphs to insert. */
20038 shift_by_width = 0;
20039 for (glyph = start; glyph < start + len; ++glyph)
20040 shift_by_width += glyph->pixel_width;
20042 /* Get the width of the region to shift right. */
20043 shifted_region_width = (window_box_width (w, updated_area)
20044 - output_cursor.x
20045 - shift_by_width);
20047 /* Shift right. */
20048 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20049 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20051 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20052 line_height, shift_by_width);
20054 /* Write the glyphs. */
20055 hpos = start - row->glyphs[updated_area];
20056 draw_glyphs (w, output_cursor.x, row, updated_area,
20057 hpos, hpos + len,
20058 DRAW_NORMAL_TEXT, 0);
20060 /* Advance the output cursor. */
20061 output_cursor.hpos += len;
20062 output_cursor.x += shift_by_width;
20063 UNBLOCK_INPUT;
20067 /* EXPORT for RIF:
20068 Erase the current text line from the nominal cursor position
20069 (inclusive) to pixel column TO_X (exclusive). The idea is that
20070 everything from TO_X onward is already erased.
20072 TO_X is a pixel position relative to updated_area of
20073 updated_window. TO_X == -1 means clear to the end of this area. */
20075 void
20076 x_clear_end_of_line (to_x)
20077 int to_x;
20079 struct frame *f;
20080 struct window *w = updated_window;
20081 int max_x, min_y, max_y;
20082 int from_x, from_y, to_y;
20084 xassert (updated_window && updated_row);
20085 f = XFRAME (w->frame);
20087 if (updated_row->full_width_p)
20088 max_x = WINDOW_TOTAL_WIDTH (w);
20089 else
20090 max_x = window_box_width (w, updated_area);
20091 max_y = window_text_bottom_y (w);
20093 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20094 of window. For TO_X > 0, truncate to end of drawing area. */
20095 if (to_x == 0)
20096 return;
20097 else if (to_x < 0)
20098 to_x = max_x;
20099 else
20100 to_x = min (to_x, max_x);
20102 to_y = min (max_y, output_cursor.y + updated_row->height);
20104 /* Notice if the cursor will be cleared by this operation. */
20105 if (!updated_row->full_width_p)
20106 notice_overwritten_cursor (w, updated_area,
20107 output_cursor.x, -1,
20108 updated_row->y,
20109 MATRIX_ROW_BOTTOM_Y (updated_row));
20111 from_x = output_cursor.x;
20113 /* Translate to frame coordinates. */
20114 if (updated_row->full_width_p)
20116 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20117 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20119 else
20121 int area_left = window_box_left (w, updated_area);
20122 from_x += area_left;
20123 to_x += area_left;
20126 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20127 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20128 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20130 /* Prevent inadvertently clearing to end of the X window. */
20131 if (to_x > from_x && to_y > from_y)
20133 BLOCK_INPUT;
20134 rif->clear_frame_area (f, from_x, from_y,
20135 to_x - from_x, to_y - from_y);
20136 UNBLOCK_INPUT;
20140 #endif /* HAVE_WINDOW_SYSTEM */
20144 /***********************************************************************
20145 Cursor types
20146 ***********************************************************************/
20148 /* Value is the internal representation of the specified cursor type
20149 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20150 of the bar cursor. */
20152 static enum text_cursor_kinds
20153 get_specified_cursor_type (arg, width)
20154 Lisp_Object arg;
20155 int *width;
20157 enum text_cursor_kinds type;
20159 if (NILP (arg))
20160 return NO_CURSOR;
20162 if (EQ (arg, Qbox))
20163 return FILLED_BOX_CURSOR;
20165 if (EQ (arg, Qhollow))
20166 return HOLLOW_BOX_CURSOR;
20168 if (EQ (arg, Qbar))
20170 *width = 2;
20171 return BAR_CURSOR;
20174 if (CONSP (arg)
20175 && EQ (XCAR (arg), Qbar)
20176 && INTEGERP (XCDR (arg))
20177 && XINT (XCDR (arg)) >= 0)
20179 *width = XINT (XCDR (arg));
20180 return BAR_CURSOR;
20183 if (EQ (arg, Qhbar))
20185 *width = 2;
20186 return HBAR_CURSOR;
20189 if (CONSP (arg)
20190 && EQ (XCAR (arg), Qhbar)
20191 && INTEGERP (XCDR (arg))
20192 && XINT (XCDR (arg)) >= 0)
20194 *width = XINT (XCDR (arg));
20195 return HBAR_CURSOR;
20198 /* Treat anything unknown as "hollow box cursor".
20199 It was bad to signal an error; people have trouble fixing
20200 .Xdefaults with Emacs, when it has something bad in it. */
20201 type = HOLLOW_BOX_CURSOR;
20203 return type;
20206 /* Set the default cursor types for specified frame. */
20207 void
20208 set_frame_cursor_types (f, arg)
20209 struct frame *f;
20210 Lisp_Object arg;
20212 int width;
20213 Lisp_Object tem;
20215 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20216 FRAME_CURSOR_WIDTH (f) = width;
20218 /* By default, set up the blink-off state depending on the on-state. */
20220 tem = Fassoc (arg, Vblink_cursor_alist);
20221 if (!NILP (tem))
20223 FRAME_BLINK_OFF_CURSOR (f)
20224 = get_specified_cursor_type (XCDR (tem), &width);
20225 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20227 else
20228 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20232 /* Return the cursor we want to be displayed in window W. Return
20233 width of bar/hbar cursor through WIDTH arg. Return with
20234 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20235 (i.e. if the `system caret' should track this cursor).
20237 In a mini-buffer window, we want the cursor only to appear if we
20238 are reading input from this window. For the selected window, we
20239 want the cursor type given by the frame parameter or buffer local
20240 setting of cursor-type. If explicitly marked off, draw no cursor.
20241 In all other cases, we want a hollow box cursor. */
20243 static enum text_cursor_kinds
20244 get_window_cursor_type (w, glyph, width, active_cursor)
20245 struct window *w;
20246 struct glyph *glyph;
20247 int *width;
20248 int *active_cursor;
20250 struct frame *f = XFRAME (w->frame);
20251 struct buffer *b = XBUFFER (w->buffer);
20252 int cursor_type = DEFAULT_CURSOR;
20253 Lisp_Object alt_cursor;
20254 int non_selected = 0;
20256 *active_cursor = 1;
20258 /* Echo area */
20259 if (cursor_in_echo_area
20260 && FRAME_HAS_MINIBUF_P (f)
20261 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20263 if (w == XWINDOW (echo_area_window))
20265 *width = FRAME_CURSOR_WIDTH (f);
20266 return FRAME_DESIRED_CURSOR (f);
20269 *active_cursor = 0;
20270 non_selected = 1;
20273 /* Nonselected window or nonselected frame. */
20274 else if (w != XWINDOW (f->selected_window)
20275 #ifdef HAVE_WINDOW_SYSTEM
20276 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20277 #endif
20280 *active_cursor = 0;
20282 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20283 return NO_CURSOR;
20285 non_selected = 1;
20288 /* Never display a cursor in a window in which cursor-type is nil. */
20289 if (NILP (b->cursor_type))
20290 return NO_CURSOR;
20292 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20293 if (non_selected)
20295 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20296 return get_specified_cursor_type (alt_cursor, width);
20299 /* Get the normal cursor type for this window. */
20300 if (EQ (b->cursor_type, Qt))
20302 cursor_type = FRAME_DESIRED_CURSOR (f);
20303 *width = FRAME_CURSOR_WIDTH (f);
20305 else
20306 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20308 /* Use normal cursor if not blinked off. */
20309 if (!w->cursor_off_p)
20311 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20312 if (cursor_type == FILLED_BOX_CURSOR)
20313 cursor_type = HOLLOW_BOX_CURSOR;
20315 return cursor_type;
20318 /* Cursor is blinked off, so determine how to "toggle" it. */
20320 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20321 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20322 return get_specified_cursor_type (XCDR (alt_cursor), width);
20324 /* Then see if frame has specified a specific blink off cursor type. */
20325 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20327 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20328 return FRAME_BLINK_OFF_CURSOR (f);
20331 #if 0
20332 /* Some people liked having a permanently visible blinking cursor,
20333 while others had very strong opinions against it. So it was
20334 decided to remove it. KFS 2003-09-03 */
20336 /* Finally perform built-in cursor blinking:
20337 filled box <-> hollow box
20338 wide [h]bar <-> narrow [h]bar
20339 narrow [h]bar <-> no cursor
20340 other type <-> no cursor */
20342 if (cursor_type == FILLED_BOX_CURSOR)
20343 return HOLLOW_BOX_CURSOR;
20345 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20347 *width = 1;
20348 return cursor_type;
20350 #endif
20352 return NO_CURSOR;
20356 #ifdef HAVE_WINDOW_SYSTEM
20358 /* Notice when the text cursor of window W has been completely
20359 overwritten by a drawing operation that outputs glyphs in AREA
20360 starting at X0 and ending at X1 in the line starting at Y0 and
20361 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20362 the rest of the line after X0 has been written. Y coordinates
20363 are window-relative. */
20365 static void
20366 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20367 struct window *w;
20368 enum glyph_row_area area;
20369 int x0, y0, x1, y1;
20371 int cx0, cx1, cy0, cy1;
20372 struct glyph_row *row;
20374 if (!w->phys_cursor_on_p)
20375 return;
20376 if (area != TEXT_AREA)
20377 return;
20379 if (w->phys_cursor.vpos < 0
20380 || w->phys_cursor.vpos >= w->current_matrix->nrows
20381 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20382 !(row->enabled_p && row->displays_text_p)))
20383 return;
20385 if (row->cursor_in_fringe_p)
20387 row->cursor_in_fringe_p = 0;
20388 draw_fringe_bitmap (w, row, 0);
20389 w->phys_cursor_on_p = 0;
20390 return;
20393 cx0 = w->phys_cursor.x;
20394 cx1 = cx0 + w->phys_cursor_width;
20395 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20396 return;
20398 /* The cursor image will be completely removed from the
20399 screen if the output area intersects the cursor area in
20400 y-direction. When we draw in [y0 y1[, and some part of
20401 the cursor is at y < y0, that part must have been drawn
20402 before. When scrolling, the cursor is erased before
20403 actually scrolling, so we don't come here. When not
20404 scrolling, the rows above the old cursor row must have
20405 changed, and in this case these rows must have written
20406 over the cursor image.
20408 Likewise if part of the cursor is below y1, with the
20409 exception of the cursor being in the first blank row at
20410 the buffer and window end because update_text_area
20411 doesn't draw that row. (Except when it does, but
20412 that's handled in update_text_area.) */
20414 cy0 = w->phys_cursor.y;
20415 cy1 = cy0 + w->phys_cursor_height;
20416 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20417 return;
20419 w->phys_cursor_on_p = 0;
20422 #endif /* HAVE_WINDOW_SYSTEM */
20425 /************************************************************************
20426 Mouse Face
20427 ************************************************************************/
20429 #ifdef HAVE_WINDOW_SYSTEM
20431 /* EXPORT for RIF:
20432 Fix the display of area AREA of overlapping row ROW in window W. */
20434 void
20435 x_fix_overlapping_area (w, row, area)
20436 struct window *w;
20437 struct glyph_row *row;
20438 enum glyph_row_area area;
20440 int i, x;
20442 BLOCK_INPUT;
20444 x = 0;
20445 for (i = 0; i < row->used[area];)
20447 if (row->glyphs[area][i].overlaps_vertically_p)
20449 int start = i, start_x = x;
20453 x += row->glyphs[area][i].pixel_width;
20454 ++i;
20456 while (i < row->used[area]
20457 && row->glyphs[area][i].overlaps_vertically_p);
20459 draw_glyphs (w, start_x, row, area,
20460 start, i,
20461 DRAW_NORMAL_TEXT, 1);
20463 else
20465 x += row->glyphs[area][i].pixel_width;
20466 ++i;
20470 UNBLOCK_INPUT;
20474 /* EXPORT:
20475 Draw the cursor glyph of window W in glyph row ROW. See the
20476 comment of draw_glyphs for the meaning of HL. */
20478 void
20479 draw_phys_cursor_glyph (w, row, hl)
20480 struct window *w;
20481 struct glyph_row *row;
20482 enum draw_glyphs_face hl;
20484 /* If cursor hpos is out of bounds, don't draw garbage. This can
20485 happen in mini-buffer windows when switching between echo area
20486 glyphs and mini-buffer. */
20487 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20489 int on_p = w->phys_cursor_on_p;
20490 int x1;
20491 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20492 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20493 hl, 0);
20494 w->phys_cursor_on_p = on_p;
20496 if (hl == DRAW_CURSOR)
20497 w->phys_cursor_width = x1 - w->phys_cursor.x;
20498 /* When we erase the cursor, and ROW is overlapped by other
20499 rows, make sure that these overlapping parts of other rows
20500 are redrawn. */
20501 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20503 if (row > w->current_matrix->rows
20504 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20505 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20507 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20508 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20509 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20515 /* EXPORT:
20516 Erase the image of a cursor of window W from the screen. */
20518 void
20519 erase_phys_cursor (w)
20520 struct window *w;
20522 struct frame *f = XFRAME (w->frame);
20523 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20524 int hpos = w->phys_cursor.hpos;
20525 int vpos = w->phys_cursor.vpos;
20526 int mouse_face_here_p = 0;
20527 struct glyph_matrix *active_glyphs = w->current_matrix;
20528 struct glyph_row *cursor_row;
20529 struct glyph *cursor_glyph;
20530 enum draw_glyphs_face hl;
20532 /* No cursor displayed or row invalidated => nothing to do on the
20533 screen. */
20534 if (w->phys_cursor_type == NO_CURSOR)
20535 goto mark_cursor_off;
20537 /* VPOS >= active_glyphs->nrows means that window has been resized.
20538 Don't bother to erase the cursor. */
20539 if (vpos >= active_glyphs->nrows)
20540 goto mark_cursor_off;
20542 /* If row containing cursor is marked invalid, there is nothing we
20543 can do. */
20544 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20545 if (!cursor_row->enabled_p)
20546 goto mark_cursor_off;
20548 /* If line spacing is > 0, old cursor may only be partially visible in
20549 window after split-window. So adjust visible height. */
20550 cursor_row->visible_height = min (cursor_row->visible_height,
20551 window_text_bottom_y (w) - cursor_row->y);
20553 /* If row is completely invisible, don't attempt to delete a cursor which
20554 isn't there. This can happen if cursor is at top of a window, and
20555 we switch to a buffer with a header line in that window. */
20556 if (cursor_row->visible_height <= 0)
20557 goto mark_cursor_off;
20559 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20560 if (cursor_row->cursor_in_fringe_p)
20562 cursor_row->cursor_in_fringe_p = 0;
20563 draw_fringe_bitmap (w, cursor_row, 0);
20564 goto mark_cursor_off;
20567 /* This can happen when the new row is shorter than the old one.
20568 In this case, either draw_glyphs or clear_end_of_line
20569 should have cleared the cursor. Note that we wouldn't be
20570 able to erase the cursor in this case because we don't have a
20571 cursor glyph at hand. */
20572 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20573 goto mark_cursor_off;
20575 /* If the cursor is in the mouse face area, redisplay that when
20576 we clear the cursor. */
20577 if (! NILP (dpyinfo->mouse_face_window)
20578 && w == XWINDOW (dpyinfo->mouse_face_window)
20579 && (vpos > dpyinfo->mouse_face_beg_row
20580 || (vpos == dpyinfo->mouse_face_beg_row
20581 && hpos >= dpyinfo->mouse_face_beg_col))
20582 && (vpos < dpyinfo->mouse_face_end_row
20583 || (vpos == dpyinfo->mouse_face_end_row
20584 && hpos < dpyinfo->mouse_face_end_col))
20585 /* Don't redraw the cursor's spot in mouse face if it is at the
20586 end of a line (on a newline). The cursor appears there, but
20587 mouse highlighting does not. */
20588 && cursor_row->used[TEXT_AREA] > hpos)
20589 mouse_face_here_p = 1;
20591 /* Maybe clear the display under the cursor. */
20592 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20594 int x, y;
20595 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20596 int width;
20598 cursor_glyph = get_phys_cursor_glyph (w);
20599 if (cursor_glyph == NULL)
20600 goto mark_cursor_off;
20602 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20603 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20604 width = min (cursor_glyph->pixel_width,
20605 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20607 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20610 /* Erase the cursor by redrawing the character underneath it. */
20611 if (mouse_face_here_p)
20612 hl = DRAW_MOUSE_FACE;
20613 else
20614 hl = DRAW_NORMAL_TEXT;
20615 draw_phys_cursor_glyph (w, cursor_row, hl);
20617 mark_cursor_off:
20618 w->phys_cursor_on_p = 0;
20619 w->phys_cursor_type = NO_CURSOR;
20623 /* EXPORT:
20624 Display or clear cursor of window W. If ON is zero, clear the
20625 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20626 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20628 void
20629 display_and_set_cursor (w, on, hpos, vpos, x, y)
20630 struct window *w;
20631 int on, hpos, vpos, x, y;
20633 struct frame *f = XFRAME (w->frame);
20634 int new_cursor_type;
20635 int new_cursor_width;
20636 int active_cursor;
20637 struct glyph_row *glyph_row;
20638 struct glyph *glyph;
20640 /* This is pointless on invisible frames, and dangerous on garbaged
20641 windows and frames; in the latter case, the frame or window may
20642 be in the midst of changing its size, and x and y may be off the
20643 window. */
20644 if (! FRAME_VISIBLE_P (f)
20645 || FRAME_GARBAGED_P (f)
20646 || vpos >= w->current_matrix->nrows
20647 || hpos >= w->current_matrix->matrix_w)
20648 return;
20650 /* If cursor is off and we want it off, return quickly. */
20651 if (!on && !w->phys_cursor_on_p)
20652 return;
20654 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20655 /* If cursor row is not enabled, we don't really know where to
20656 display the cursor. */
20657 if (!glyph_row->enabled_p)
20659 w->phys_cursor_on_p = 0;
20660 return;
20663 glyph = NULL;
20664 if (!glyph_row->exact_window_width_line_p
20665 || hpos < glyph_row->used[TEXT_AREA])
20666 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20668 xassert (interrupt_input_blocked);
20670 /* Set new_cursor_type to the cursor we want to be displayed. */
20671 new_cursor_type = get_window_cursor_type (w, glyph,
20672 &new_cursor_width, &active_cursor);
20674 /* If cursor is currently being shown and we don't want it to be or
20675 it is in the wrong place, or the cursor type is not what we want,
20676 erase it. */
20677 if (w->phys_cursor_on_p
20678 && (!on
20679 || w->phys_cursor.x != x
20680 || w->phys_cursor.y != y
20681 || new_cursor_type != w->phys_cursor_type
20682 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20683 && new_cursor_width != w->phys_cursor_width)))
20684 erase_phys_cursor (w);
20686 /* Don't check phys_cursor_on_p here because that flag is only set
20687 to zero in some cases where we know that the cursor has been
20688 completely erased, to avoid the extra work of erasing the cursor
20689 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20690 still not be visible, or it has only been partly erased. */
20691 if (on)
20693 w->phys_cursor_ascent = glyph_row->ascent;
20694 w->phys_cursor_height = glyph_row->height;
20696 /* Set phys_cursor_.* before x_draw_.* is called because some
20697 of them may need the information. */
20698 w->phys_cursor.x = x;
20699 w->phys_cursor.y = glyph_row->y;
20700 w->phys_cursor.hpos = hpos;
20701 w->phys_cursor.vpos = vpos;
20704 rif->draw_window_cursor (w, glyph_row, x, y,
20705 new_cursor_type, new_cursor_width,
20706 on, active_cursor);
20710 /* Switch the display of W's cursor on or off, according to the value
20711 of ON. */
20713 static void
20714 update_window_cursor (w, on)
20715 struct window *w;
20716 int on;
20718 /* Don't update cursor in windows whose frame is in the process
20719 of being deleted. */
20720 if (w->current_matrix)
20722 BLOCK_INPUT;
20723 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20724 w->phys_cursor.x, w->phys_cursor.y);
20725 UNBLOCK_INPUT;
20730 /* Call update_window_cursor with parameter ON_P on all leaf windows
20731 in the window tree rooted at W. */
20733 static void
20734 update_cursor_in_window_tree (w, on_p)
20735 struct window *w;
20736 int on_p;
20738 while (w)
20740 if (!NILP (w->hchild))
20741 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20742 else if (!NILP (w->vchild))
20743 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20744 else
20745 update_window_cursor (w, on_p);
20747 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20752 /* EXPORT:
20753 Display the cursor on window W, or clear it, according to ON_P.
20754 Don't change the cursor's position. */
20756 void
20757 x_update_cursor (f, on_p)
20758 struct frame *f;
20759 int on_p;
20761 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20765 /* EXPORT:
20766 Clear the cursor of window W to background color, and mark the
20767 cursor as not shown. This is used when the text where the cursor
20768 is is about to be rewritten. */
20770 void
20771 x_clear_cursor (w)
20772 struct window *w;
20774 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20775 update_window_cursor (w, 0);
20779 /* EXPORT:
20780 Display the active region described by mouse_face_* according to DRAW. */
20782 void
20783 show_mouse_face (dpyinfo, draw)
20784 Display_Info *dpyinfo;
20785 enum draw_glyphs_face draw;
20787 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20788 struct frame *f = XFRAME (WINDOW_FRAME (w));
20790 if (/* If window is in the process of being destroyed, don't bother
20791 to do anything. */
20792 w->current_matrix != NULL
20793 /* Don't update mouse highlight if hidden */
20794 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20795 /* Recognize when we are called to operate on rows that don't exist
20796 anymore. This can happen when a window is split. */
20797 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20799 int phys_cursor_on_p = w->phys_cursor_on_p;
20800 struct glyph_row *row, *first, *last;
20802 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20803 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20805 for (row = first; row <= last && row->enabled_p; ++row)
20807 int start_hpos, end_hpos, start_x;
20809 /* For all but the first row, the highlight starts at column 0. */
20810 if (row == first)
20812 start_hpos = dpyinfo->mouse_face_beg_col;
20813 start_x = dpyinfo->mouse_face_beg_x;
20815 else
20817 start_hpos = 0;
20818 start_x = 0;
20821 if (row == last)
20822 end_hpos = dpyinfo->mouse_face_end_col;
20823 else
20824 end_hpos = row->used[TEXT_AREA];
20826 if (end_hpos > start_hpos)
20828 draw_glyphs (w, start_x, row, TEXT_AREA,
20829 start_hpos, end_hpos,
20830 draw, 0);
20832 row->mouse_face_p
20833 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20837 /* When we've written over the cursor, arrange for it to
20838 be displayed again. */
20839 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20841 BLOCK_INPUT;
20842 display_and_set_cursor (w, 1,
20843 w->phys_cursor.hpos, w->phys_cursor.vpos,
20844 w->phys_cursor.x, w->phys_cursor.y);
20845 UNBLOCK_INPUT;
20849 /* Change the mouse cursor. */
20850 if (draw == DRAW_NORMAL_TEXT)
20851 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20852 else if (draw == DRAW_MOUSE_FACE)
20853 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20854 else
20855 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20858 /* EXPORT:
20859 Clear out the mouse-highlighted active region.
20860 Redraw it un-highlighted first. Value is non-zero if mouse
20861 face was actually drawn unhighlighted. */
20864 clear_mouse_face (dpyinfo)
20865 Display_Info *dpyinfo;
20867 int cleared = 0;
20869 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20871 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20872 cleared = 1;
20875 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20876 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20877 dpyinfo->mouse_face_window = Qnil;
20878 dpyinfo->mouse_face_overlay = Qnil;
20879 return cleared;
20883 /* EXPORT:
20884 Non-zero if physical cursor of window W is within mouse face. */
20887 cursor_in_mouse_face_p (w)
20888 struct window *w;
20890 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20891 int in_mouse_face = 0;
20893 if (WINDOWP (dpyinfo->mouse_face_window)
20894 && XWINDOW (dpyinfo->mouse_face_window) == w)
20896 int hpos = w->phys_cursor.hpos;
20897 int vpos = w->phys_cursor.vpos;
20899 if (vpos >= dpyinfo->mouse_face_beg_row
20900 && vpos <= dpyinfo->mouse_face_end_row
20901 && (vpos > dpyinfo->mouse_face_beg_row
20902 || hpos >= dpyinfo->mouse_face_beg_col)
20903 && (vpos < dpyinfo->mouse_face_end_row
20904 || hpos < dpyinfo->mouse_face_end_col
20905 || dpyinfo->mouse_face_past_end))
20906 in_mouse_face = 1;
20909 return in_mouse_face;
20915 /* Find the glyph matrix position of buffer position CHARPOS in window
20916 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20917 current glyphs must be up to date. If CHARPOS is above window
20918 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20919 of last line in W. In the row containing CHARPOS, stop before glyphs
20920 having STOP as object. */
20922 #if 1 /* This is a version of fast_find_position that's more correct
20923 in the presence of hscrolling, for example. I didn't install
20924 it right away because the problem fixed is minor, it failed
20925 in 20.x as well, and I think it's too risky to install
20926 so near the release of 21.1. 2001-09-25 gerd. */
20928 static int
20929 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20930 struct window *w;
20931 int charpos;
20932 int *hpos, *vpos, *x, *y;
20933 Lisp_Object stop;
20935 struct glyph_row *row, *first;
20936 struct glyph *glyph, *end;
20937 int past_end = 0;
20939 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20940 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20942 *x = first->x;
20943 *y = first->y;
20944 *hpos = 0;
20945 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20946 return 1;
20949 row = row_containing_pos (w, charpos, first, NULL, 0);
20950 if (row == NULL)
20952 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20953 past_end = 1;
20956 /* If whole rows or last part of a row came from a display overlay,
20957 row_containing_pos will skip over such rows because their end pos
20958 equals the start pos of the overlay or interval.
20960 Move back if we have a STOP object and previous row's
20961 end glyph came from STOP. */
20962 if (!NILP (stop))
20964 struct glyph_row *prev;
20965 while ((prev = row - 1, prev >= first)
20966 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20967 && prev->used[TEXT_AREA] > 0)
20969 struct glyph *beg = prev->glyphs[TEXT_AREA];
20970 glyph = beg + prev->used[TEXT_AREA];
20971 while (--glyph >= beg
20972 && INTEGERP (glyph->object));
20973 if (glyph < beg
20974 || !EQ (stop, glyph->object))
20975 break;
20976 row = prev;
20980 *x = row->x;
20981 *y = row->y;
20982 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20984 glyph = row->glyphs[TEXT_AREA];
20985 end = glyph + row->used[TEXT_AREA];
20987 /* Skip over glyphs not having an object at the start of the row.
20988 These are special glyphs like truncation marks on terminal
20989 frames. */
20990 if (row->displays_text_p)
20991 while (glyph < end
20992 && INTEGERP (glyph->object)
20993 && !EQ (stop, glyph->object)
20994 && glyph->charpos < 0)
20996 *x += glyph->pixel_width;
20997 ++glyph;
21000 while (glyph < end
21001 && !INTEGERP (glyph->object)
21002 && !EQ (stop, glyph->object)
21003 && (!BUFFERP (glyph->object)
21004 || glyph->charpos < charpos))
21006 *x += glyph->pixel_width;
21007 ++glyph;
21010 *hpos = glyph - row->glyphs[TEXT_AREA];
21011 return !past_end;
21014 #else /* not 1 */
21016 static int
21017 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21018 struct window *w;
21019 int pos;
21020 int *hpos, *vpos, *x, *y;
21021 Lisp_Object stop;
21023 int i;
21024 int lastcol;
21025 int maybe_next_line_p = 0;
21026 int line_start_position;
21027 int yb = window_text_bottom_y (w);
21028 struct glyph_row *row, *best_row;
21029 int row_vpos, best_row_vpos;
21030 int current_x;
21032 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21033 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21035 while (row->y < yb)
21037 if (row->used[TEXT_AREA])
21038 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21039 else
21040 line_start_position = 0;
21042 if (line_start_position > pos)
21043 break;
21044 /* If the position sought is the end of the buffer,
21045 don't include the blank lines at the bottom of the window. */
21046 else if (line_start_position == pos
21047 && pos == BUF_ZV (XBUFFER (w->buffer)))
21049 maybe_next_line_p = 1;
21050 break;
21052 else if (line_start_position > 0)
21054 best_row = row;
21055 best_row_vpos = row_vpos;
21058 if (row->y + row->height >= yb)
21059 break;
21061 ++row;
21062 ++row_vpos;
21065 /* Find the right column within BEST_ROW. */
21066 lastcol = 0;
21067 current_x = best_row->x;
21068 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21070 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21071 int charpos = glyph->charpos;
21073 if (BUFFERP (glyph->object))
21075 if (charpos == pos)
21077 *hpos = i;
21078 *vpos = best_row_vpos;
21079 *x = current_x;
21080 *y = best_row->y;
21081 return 1;
21083 else if (charpos > pos)
21084 break;
21086 else if (EQ (glyph->object, stop))
21087 break;
21089 if (charpos > 0)
21090 lastcol = i;
21091 current_x += glyph->pixel_width;
21094 /* If we're looking for the end of the buffer,
21095 and we didn't find it in the line we scanned,
21096 use the start of the following line. */
21097 if (maybe_next_line_p)
21099 ++best_row;
21100 ++best_row_vpos;
21101 lastcol = 0;
21102 current_x = best_row->x;
21105 *vpos = best_row_vpos;
21106 *hpos = lastcol + 1;
21107 *x = current_x;
21108 *y = best_row->y;
21109 return 0;
21112 #endif /* not 1 */
21115 /* Find the position of the glyph for position POS in OBJECT in
21116 window W's current matrix, and return in *X, *Y the pixel
21117 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21119 RIGHT_P non-zero means return the position of the right edge of the
21120 glyph, RIGHT_P zero means return the left edge position.
21122 If no glyph for POS exists in the matrix, return the position of
21123 the glyph with the next smaller position that is in the matrix, if
21124 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21125 exists in the matrix, return the position of the glyph with the
21126 next larger position in OBJECT.
21128 Value is non-zero if a glyph was found. */
21130 static int
21131 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21132 struct window *w;
21133 int pos;
21134 Lisp_Object object;
21135 int *hpos, *vpos, *x, *y;
21136 int right_p;
21138 int yb = window_text_bottom_y (w);
21139 struct glyph_row *r;
21140 struct glyph *best_glyph = NULL;
21141 struct glyph_row *best_row = NULL;
21142 int best_x = 0;
21144 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21145 r->enabled_p && r->y < yb;
21146 ++r)
21148 struct glyph *g = r->glyphs[TEXT_AREA];
21149 struct glyph *e = g + r->used[TEXT_AREA];
21150 int gx;
21152 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21153 if (EQ (g->object, object))
21155 if (g->charpos == pos)
21157 best_glyph = g;
21158 best_x = gx;
21159 best_row = r;
21160 goto found;
21162 else if (best_glyph == NULL
21163 || ((abs (g->charpos - pos)
21164 < abs (best_glyph->charpos - pos))
21165 && (right_p
21166 ? g->charpos < pos
21167 : g->charpos > pos)))
21169 best_glyph = g;
21170 best_x = gx;
21171 best_row = r;
21176 found:
21178 if (best_glyph)
21180 *x = best_x;
21181 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21183 if (right_p)
21185 *x += best_glyph->pixel_width;
21186 ++*hpos;
21189 *y = best_row->y;
21190 *vpos = best_row - w->current_matrix->rows;
21193 return best_glyph != NULL;
21197 /* See if position X, Y is within a hot-spot of an image. */
21199 static int
21200 on_hot_spot_p (hot_spot, x, y)
21201 Lisp_Object hot_spot;
21202 int x, y;
21204 if (!CONSP (hot_spot))
21205 return 0;
21207 if (EQ (XCAR (hot_spot), Qrect))
21209 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21210 Lisp_Object rect = XCDR (hot_spot);
21211 Lisp_Object tem;
21212 if (!CONSP (rect))
21213 return 0;
21214 if (!CONSP (XCAR (rect)))
21215 return 0;
21216 if (!CONSP (XCDR (rect)))
21217 return 0;
21218 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21219 return 0;
21220 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21221 return 0;
21222 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21223 return 0;
21224 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21225 return 0;
21226 return 1;
21228 else if (EQ (XCAR (hot_spot), Qcircle))
21230 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21231 Lisp_Object circ = XCDR (hot_spot);
21232 Lisp_Object lr, lx0, ly0;
21233 if (CONSP (circ)
21234 && CONSP (XCAR (circ))
21235 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21236 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21237 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21239 double r = XFLOATINT (lr);
21240 double dx = XINT (lx0) - x;
21241 double dy = XINT (ly0) - y;
21242 return (dx * dx + dy * dy <= r * r);
21245 else if (EQ (XCAR (hot_spot), Qpoly))
21247 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21248 if (VECTORP (XCDR (hot_spot)))
21250 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21251 Lisp_Object *poly = v->contents;
21252 int n = v->size;
21253 int i;
21254 int inside = 0;
21255 Lisp_Object lx, ly;
21256 int x0, y0;
21258 /* Need an even number of coordinates, and at least 3 edges. */
21259 if (n < 6 || n & 1)
21260 return 0;
21262 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21263 If count is odd, we are inside polygon. Pixels on edges
21264 may or may not be included depending on actual geometry of the
21265 polygon. */
21266 if ((lx = poly[n-2], !INTEGERP (lx))
21267 || (ly = poly[n-1], !INTEGERP (lx)))
21268 return 0;
21269 x0 = XINT (lx), y0 = XINT (ly);
21270 for (i = 0; i < n; i += 2)
21272 int x1 = x0, y1 = y0;
21273 if ((lx = poly[i], !INTEGERP (lx))
21274 || (ly = poly[i+1], !INTEGERP (ly)))
21275 return 0;
21276 x0 = XINT (lx), y0 = XINT (ly);
21278 /* Does this segment cross the X line? */
21279 if (x0 >= x)
21281 if (x1 >= x)
21282 continue;
21284 else if (x1 < x)
21285 continue;
21286 if (y > y0 && y > y1)
21287 continue;
21288 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21289 inside = !inside;
21291 return inside;
21294 /* If we don't understand the format, pretend we're not in the hot-spot. */
21295 return 0;
21298 Lisp_Object
21299 find_hot_spot (map, x, y)
21300 Lisp_Object map;
21301 int x, y;
21303 while (CONSP (map))
21305 if (CONSP (XCAR (map))
21306 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21307 return XCAR (map);
21308 map = XCDR (map);
21311 return Qnil;
21314 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21315 3, 3, 0,
21316 doc: /* Lookup in image map MAP coordinates X and Y.
21317 An image map is an alist where each element has the format (AREA ID PLIST).
21318 An AREA is specified as either a rectangle, a circle, or a polygon:
21319 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21320 pixel coordinates of the upper left and bottom right corners.
21321 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21322 and the radius of the circle; r may be a float or integer.
21323 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21324 vector describes one corner in the polygon.
21325 Returns the alist element for the first matching AREA in MAP. */)
21326 (map, x, y)
21327 Lisp_Object map;
21328 Lisp_Object x, y;
21330 if (NILP (map))
21331 return Qnil;
21333 CHECK_NUMBER (x);
21334 CHECK_NUMBER (y);
21336 return find_hot_spot (map, XINT (x), XINT (y));
21340 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21341 static void
21342 define_frame_cursor1 (f, cursor, pointer)
21343 struct frame *f;
21344 Cursor cursor;
21345 Lisp_Object pointer;
21347 /* Do not change cursor shape while dragging mouse. */
21348 if (!NILP (do_mouse_tracking))
21349 return;
21351 if (!NILP (pointer))
21353 if (EQ (pointer, Qarrow))
21354 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21355 else if (EQ (pointer, Qhand))
21356 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21357 else if (EQ (pointer, Qtext))
21358 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21359 else if (EQ (pointer, intern ("hdrag")))
21360 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21361 #ifdef HAVE_X_WINDOWS
21362 else if (EQ (pointer, intern ("vdrag")))
21363 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21364 #endif
21365 else if (EQ (pointer, intern ("hourglass")))
21366 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21367 else if (EQ (pointer, Qmodeline))
21368 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21369 else
21370 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21373 if (cursor != No_Cursor)
21374 rif->define_frame_cursor (f, cursor);
21377 /* Take proper action when mouse has moved to the mode or header line
21378 or marginal area AREA of window W, x-position X and y-position Y.
21379 X is relative to the start of the text display area of W, so the
21380 width of bitmap areas and scroll bars must be subtracted to get a
21381 position relative to the start of the mode line. */
21383 static void
21384 note_mode_line_or_margin_highlight (window, x, y, area)
21385 Lisp_Object window;
21386 int x, y;
21387 enum window_part area;
21389 struct window *w = XWINDOW (window);
21390 struct frame *f = XFRAME (w->frame);
21391 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21392 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21393 Lisp_Object pointer = Qnil;
21394 int charpos, dx, dy, width, height;
21395 Lisp_Object string, object = Qnil;
21396 Lisp_Object pos, help;
21398 Lisp_Object mouse_face;
21399 int original_x_pixel = x;
21400 struct glyph * glyph = NULL;
21401 struct glyph_row *row;
21403 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21405 int x0;
21406 struct glyph *end;
21408 string = mode_line_string (w, area, &x, &y, &charpos,
21409 &object, &dx, &dy, &width, &height);
21411 row = (area == ON_MODE_LINE
21412 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21413 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21415 /* Find glyph */
21416 if (row->mode_line_p && row->enabled_p)
21418 glyph = row->glyphs[TEXT_AREA];
21419 end = glyph + row->used[TEXT_AREA];
21421 for (x0 = original_x_pixel;
21422 glyph < end && x0 >= glyph->pixel_width;
21423 ++glyph)
21424 x0 -= glyph->pixel_width;
21426 if (glyph >= end)
21427 glyph = NULL;
21430 else
21432 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21433 string = marginal_area_string (w, area, &x, &y, &charpos,
21434 &object, &dx, &dy, &width, &height);
21437 help = Qnil;
21439 if (IMAGEP (object))
21441 Lisp_Object image_map, hotspot;
21442 if ((image_map = Fplist_get (XCDR (object), QCmap),
21443 !NILP (image_map))
21444 && (hotspot = find_hot_spot (image_map, dx, dy),
21445 CONSP (hotspot))
21446 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21448 Lisp_Object area_id, plist;
21450 area_id = XCAR (hotspot);
21451 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21452 If so, we could look for mouse-enter, mouse-leave
21453 properties in PLIST (and do something...). */
21454 hotspot = XCDR (hotspot);
21455 if (CONSP (hotspot)
21456 && (plist = XCAR (hotspot), CONSP (plist)))
21458 pointer = Fplist_get (plist, Qpointer);
21459 if (NILP (pointer))
21460 pointer = Qhand;
21461 help = Fplist_get (plist, Qhelp_echo);
21462 if (!NILP (help))
21464 help_echo_string = help;
21465 /* Is this correct? ++kfs */
21466 XSETWINDOW (help_echo_window, w);
21467 help_echo_object = w->buffer;
21468 help_echo_pos = charpos;
21472 if (NILP (pointer))
21473 pointer = Fplist_get (XCDR (object), QCpointer);
21476 if (STRINGP (string))
21478 pos = make_number (charpos);
21479 /* If we're on a string with `help-echo' text property, arrange
21480 for the help to be displayed. This is done by setting the
21481 global variable help_echo_string to the help string. */
21482 if (NILP (help))
21484 help = Fget_text_property (pos, Qhelp_echo, string);
21485 if (!NILP (help))
21487 help_echo_string = help;
21488 XSETWINDOW (help_echo_window, w);
21489 help_echo_object = string;
21490 help_echo_pos = charpos;
21494 if (NILP (pointer))
21495 pointer = Fget_text_property (pos, Qpointer, string);
21497 /* Change the mouse pointer according to what is under X/Y. */
21498 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21500 Lisp_Object map;
21501 map = Fget_text_property (pos, Qlocal_map, string);
21502 if (!KEYMAPP (map))
21503 map = Fget_text_property (pos, Qkeymap, string);
21504 if (!KEYMAPP (map))
21505 cursor = dpyinfo->vertical_scroll_bar_cursor;
21508 /* Change the mouse face according to what is under X/Y. */
21509 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21510 if (!NILP (mouse_face)
21511 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21512 && glyph)
21514 Lisp_Object b, e;
21516 struct glyph * tmp_glyph;
21518 int gpos;
21519 int gseq_length;
21520 int total_pixel_width;
21521 int ignore;
21523 int vpos, hpos;
21525 b = Fprevious_single_property_change (make_number (charpos + 1),
21526 Qmouse_face, string, Qnil);
21527 if (NILP (b))
21528 b = make_number (0);
21530 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21531 if (NILP (e))
21532 e = make_number (SCHARS (string));
21534 /* Calculate the position(glyph position: GPOS) of GLYPH in
21535 displayed string. GPOS is different from CHARPOS.
21537 CHARPOS is the position of glyph in internal string
21538 object. A mode line string format has structures which
21539 is converted to a flatten by emacs lisp interpreter.
21540 The internal string is an element of the structures.
21541 The displayed string is the flatten string. */
21542 for (tmp_glyph = glyph - 1, gpos = 0;
21543 tmp_glyph->charpos >= XINT (b);
21544 tmp_glyph--, gpos++)
21546 if (!EQ (tmp_glyph->object, glyph->object))
21547 break;
21550 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21551 displayed string holding GLYPH.
21553 GSEQ_LENGTH is different from SCHARS (STRING).
21554 SCHARS (STRING) returns the length of the internal string. */
21555 for (tmp_glyph = glyph, gseq_length = gpos;
21556 tmp_glyph->charpos < XINT (e);
21557 tmp_glyph++, gseq_length++)
21559 if (!EQ (tmp_glyph->object, glyph->object))
21560 break;
21563 total_pixel_width = 0;
21564 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21565 total_pixel_width += tmp_glyph->pixel_width;
21567 /* Pre calculation of re-rendering position */
21568 vpos = (x - gpos);
21569 hpos = (area == ON_MODE_LINE
21570 ? (w->current_matrix)->nrows - 1
21571 : 0);
21573 /* If the re-rendering position is included in the last
21574 re-rendering area, we should do nothing. */
21575 if ( EQ (window, dpyinfo->mouse_face_window)
21576 && dpyinfo->mouse_face_beg_col <= vpos
21577 && vpos < dpyinfo->mouse_face_end_col
21578 && dpyinfo->mouse_face_beg_row == hpos )
21579 return;
21581 if (clear_mouse_face (dpyinfo))
21582 cursor = No_Cursor;
21584 dpyinfo->mouse_face_beg_col = vpos;
21585 dpyinfo->mouse_face_beg_row = hpos;
21587 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21588 dpyinfo->mouse_face_beg_y = 0;
21590 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21591 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21593 dpyinfo->mouse_face_end_x = 0;
21594 dpyinfo->mouse_face_end_y = 0;
21596 dpyinfo->mouse_face_past_end = 0;
21597 dpyinfo->mouse_face_window = window;
21599 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21600 charpos,
21601 0, 0, 0, &ignore,
21602 glyph->face_id, 1);
21603 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21605 if (NILP (pointer))
21606 pointer = Qhand;
21608 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21609 clear_mouse_face (dpyinfo);
21611 define_frame_cursor1 (f, cursor, pointer);
21615 /* EXPORT:
21616 Take proper action when the mouse has moved to position X, Y on
21617 frame F as regards highlighting characters that have mouse-face
21618 properties. Also de-highlighting chars where the mouse was before.
21619 X and Y can be negative or out of range. */
21621 void
21622 note_mouse_highlight (f, x, y)
21623 struct frame *f;
21624 int x, y;
21626 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21627 enum window_part part;
21628 Lisp_Object window;
21629 struct window *w;
21630 Cursor cursor = No_Cursor;
21631 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21632 struct buffer *b;
21634 /* When a menu is active, don't highlight because this looks odd. */
21635 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21636 if (popup_activated ())
21637 return;
21638 #endif
21640 if (NILP (Vmouse_highlight)
21641 || !f->glyphs_initialized_p)
21642 return;
21644 dpyinfo->mouse_face_mouse_x = x;
21645 dpyinfo->mouse_face_mouse_y = y;
21646 dpyinfo->mouse_face_mouse_frame = f;
21648 if (dpyinfo->mouse_face_defer)
21649 return;
21651 if (gc_in_progress)
21653 dpyinfo->mouse_face_deferred_gc = 1;
21654 return;
21657 /* Which window is that in? */
21658 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21660 /* If we were displaying active text in another window, clear that.
21661 Also clear if we move out of text area in same window. */
21662 if (! EQ (window, dpyinfo->mouse_face_window)
21663 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21664 && !NILP (dpyinfo->mouse_face_window)))
21665 clear_mouse_face (dpyinfo);
21667 /* Not on a window -> return. */
21668 if (!WINDOWP (window))
21669 return;
21671 /* Reset help_echo_string. It will get recomputed below. */
21672 help_echo_string = Qnil;
21674 /* Convert to window-relative pixel coordinates. */
21675 w = XWINDOW (window);
21676 frame_to_window_pixel_xy (w, &x, &y);
21678 /* Handle tool-bar window differently since it doesn't display a
21679 buffer. */
21680 if (EQ (window, f->tool_bar_window))
21682 note_tool_bar_highlight (f, x, y);
21683 return;
21686 /* Mouse is on the mode, header line or margin? */
21687 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21688 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21690 note_mode_line_or_margin_highlight (window, x, y, part);
21691 return;
21694 if (part == ON_VERTICAL_BORDER)
21695 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21696 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21697 || part == ON_SCROLL_BAR)
21698 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21699 else
21700 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21702 /* Are we in a window whose display is up to date?
21703 And verify the buffer's text has not changed. */
21704 b = XBUFFER (w->buffer);
21705 if (part == ON_TEXT
21706 && EQ (w->window_end_valid, w->buffer)
21707 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21708 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21710 int hpos, vpos, pos, i, dx, dy, area;
21711 struct glyph *glyph;
21712 Lisp_Object object;
21713 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21714 Lisp_Object *overlay_vec = NULL;
21715 int noverlays;
21716 struct buffer *obuf;
21717 int obegv, ozv, same_region;
21719 /* Find the glyph under X/Y. */
21720 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21722 /* Look for :pointer property on image. */
21723 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21725 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21726 if (img != NULL && IMAGEP (img->spec))
21728 Lisp_Object image_map, hotspot;
21729 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21730 !NILP (image_map))
21731 && (hotspot = find_hot_spot (image_map,
21732 glyph->slice.x + dx,
21733 glyph->slice.y + dy),
21734 CONSP (hotspot))
21735 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21737 Lisp_Object area_id, plist;
21739 area_id = XCAR (hotspot);
21740 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21741 If so, we could look for mouse-enter, mouse-leave
21742 properties in PLIST (and do something...). */
21743 hotspot = XCDR (hotspot);
21744 if (CONSP (hotspot)
21745 && (plist = XCAR (hotspot), CONSP (plist)))
21747 pointer = Fplist_get (plist, Qpointer);
21748 if (NILP (pointer))
21749 pointer = Qhand;
21750 help_echo_string = Fplist_get (plist, Qhelp_echo);
21751 if (!NILP (help_echo_string))
21753 help_echo_window = window;
21754 help_echo_object = glyph->object;
21755 help_echo_pos = glyph->charpos;
21759 if (NILP (pointer))
21760 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21764 /* Clear mouse face if X/Y not over text. */
21765 if (glyph == NULL
21766 || area != TEXT_AREA
21767 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21769 if (clear_mouse_face (dpyinfo))
21770 cursor = No_Cursor;
21771 if (NILP (pointer))
21773 if (area != TEXT_AREA)
21774 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21775 else
21776 pointer = Vvoid_text_area_pointer;
21778 goto set_cursor;
21781 pos = glyph->charpos;
21782 object = glyph->object;
21783 if (!STRINGP (object) && !BUFFERP (object))
21784 goto set_cursor;
21786 /* If we get an out-of-range value, return now; avoid an error. */
21787 if (BUFFERP (object) && pos > BUF_Z (b))
21788 goto set_cursor;
21790 /* Make the window's buffer temporarily current for
21791 overlays_at and compute_char_face. */
21792 obuf = current_buffer;
21793 current_buffer = b;
21794 obegv = BEGV;
21795 ozv = ZV;
21796 BEGV = BEG;
21797 ZV = Z;
21799 /* Is this char mouse-active or does it have help-echo? */
21800 position = make_number (pos);
21802 if (BUFFERP (object))
21804 /* Put all the overlays we want in a vector in overlay_vec. */
21805 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21806 /* Sort overlays into increasing priority order. */
21807 noverlays = sort_overlays (overlay_vec, noverlays, w);
21809 else
21810 noverlays = 0;
21812 same_region = (EQ (window, dpyinfo->mouse_face_window)
21813 && vpos >= dpyinfo->mouse_face_beg_row
21814 && vpos <= dpyinfo->mouse_face_end_row
21815 && (vpos > dpyinfo->mouse_face_beg_row
21816 || hpos >= dpyinfo->mouse_face_beg_col)
21817 && (vpos < dpyinfo->mouse_face_end_row
21818 || hpos < dpyinfo->mouse_face_end_col
21819 || dpyinfo->mouse_face_past_end));
21821 if (same_region)
21822 cursor = No_Cursor;
21824 /* Check mouse-face highlighting. */
21825 if (! same_region
21826 /* If there exists an overlay with mouse-face overlapping
21827 the one we are currently highlighting, we have to
21828 check if we enter the overlapping overlay, and then
21829 highlight only that. */
21830 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21831 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21833 /* Find the highest priority overlay that has a mouse-face
21834 property. */
21835 overlay = Qnil;
21836 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21838 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21839 if (!NILP (mouse_face))
21840 overlay = overlay_vec[i];
21843 /* If we're actually highlighting the same overlay as
21844 before, there's no need to do that again. */
21845 if (!NILP (overlay)
21846 && EQ (overlay, dpyinfo->mouse_face_overlay))
21847 goto check_help_echo;
21849 dpyinfo->mouse_face_overlay = overlay;
21851 /* Clear the display of the old active region, if any. */
21852 if (clear_mouse_face (dpyinfo))
21853 cursor = No_Cursor;
21855 /* If no overlay applies, get a text property. */
21856 if (NILP (overlay))
21857 mouse_face = Fget_text_property (position, Qmouse_face, object);
21859 /* Handle the overlay case. */
21860 if (!NILP (overlay))
21862 /* Find the range of text around this char that
21863 should be active. */
21864 Lisp_Object before, after;
21865 int ignore;
21867 before = Foverlay_start (overlay);
21868 after = Foverlay_end (overlay);
21869 /* Record this as the current active region. */
21870 fast_find_position (w, XFASTINT (before),
21871 &dpyinfo->mouse_face_beg_col,
21872 &dpyinfo->mouse_face_beg_row,
21873 &dpyinfo->mouse_face_beg_x,
21874 &dpyinfo->mouse_face_beg_y, Qnil);
21876 dpyinfo->mouse_face_past_end
21877 = !fast_find_position (w, XFASTINT (after),
21878 &dpyinfo->mouse_face_end_col,
21879 &dpyinfo->mouse_face_end_row,
21880 &dpyinfo->mouse_face_end_x,
21881 &dpyinfo->mouse_face_end_y, Qnil);
21882 dpyinfo->mouse_face_window = window;
21884 dpyinfo->mouse_face_face_id
21885 = face_at_buffer_position (w, pos, 0, 0,
21886 &ignore, pos + 1,
21887 !dpyinfo->mouse_face_hidden);
21889 /* Display it as active. */
21890 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21891 cursor = No_Cursor;
21893 /* Handle the text property case. */
21894 else if (!NILP (mouse_face) && BUFFERP (object))
21896 /* Find the range of text around this char that
21897 should be active. */
21898 Lisp_Object before, after, beginning, end;
21899 int ignore;
21901 beginning = Fmarker_position (w->start);
21902 end = make_number (BUF_Z (XBUFFER (object))
21903 - XFASTINT (w->window_end_pos));
21904 before
21905 = Fprevious_single_property_change (make_number (pos + 1),
21906 Qmouse_face,
21907 object, beginning);
21908 after
21909 = Fnext_single_property_change (position, Qmouse_face,
21910 object, end);
21912 /* Record this as the current active region. */
21913 fast_find_position (w, XFASTINT (before),
21914 &dpyinfo->mouse_face_beg_col,
21915 &dpyinfo->mouse_face_beg_row,
21916 &dpyinfo->mouse_face_beg_x,
21917 &dpyinfo->mouse_face_beg_y, Qnil);
21918 dpyinfo->mouse_face_past_end
21919 = !fast_find_position (w, XFASTINT (after),
21920 &dpyinfo->mouse_face_end_col,
21921 &dpyinfo->mouse_face_end_row,
21922 &dpyinfo->mouse_face_end_x,
21923 &dpyinfo->mouse_face_end_y, Qnil);
21924 dpyinfo->mouse_face_window = window;
21926 if (BUFFERP (object))
21927 dpyinfo->mouse_face_face_id
21928 = face_at_buffer_position (w, pos, 0, 0,
21929 &ignore, pos + 1,
21930 !dpyinfo->mouse_face_hidden);
21932 /* Display it as active. */
21933 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21934 cursor = No_Cursor;
21936 else if (!NILP (mouse_face) && STRINGP (object))
21938 Lisp_Object b, e;
21939 int ignore;
21941 b = Fprevious_single_property_change (make_number (pos + 1),
21942 Qmouse_face,
21943 object, Qnil);
21944 e = Fnext_single_property_change (position, Qmouse_face,
21945 object, Qnil);
21946 if (NILP (b))
21947 b = make_number (0);
21948 if (NILP (e))
21949 e = make_number (SCHARS (object) - 1);
21951 fast_find_string_pos (w, XINT (b), object,
21952 &dpyinfo->mouse_face_beg_col,
21953 &dpyinfo->mouse_face_beg_row,
21954 &dpyinfo->mouse_face_beg_x,
21955 &dpyinfo->mouse_face_beg_y, 0);
21956 fast_find_string_pos (w, XINT (e), object,
21957 &dpyinfo->mouse_face_end_col,
21958 &dpyinfo->mouse_face_end_row,
21959 &dpyinfo->mouse_face_end_x,
21960 &dpyinfo->mouse_face_end_y, 1);
21961 dpyinfo->mouse_face_past_end = 0;
21962 dpyinfo->mouse_face_window = window;
21963 dpyinfo->mouse_face_face_id
21964 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21965 glyph->face_id, 1);
21966 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21967 cursor = No_Cursor;
21969 else if (STRINGP (object) && NILP (mouse_face))
21971 /* A string which doesn't have mouse-face, but
21972 the text ``under'' it might have. */
21973 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21974 int start = MATRIX_ROW_START_CHARPOS (r);
21976 pos = string_buffer_position (w, object, start);
21977 if (pos > 0)
21978 mouse_face = get_char_property_and_overlay (make_number (pos),
21979 Qmouse_face,
21980 w->buffer,
21981 &overlay);
21982 if (!NILP (mouse_face) && !NILP (overlay))
21984 Lisp_Object before = Foverlay_start (overlay);
21985 Lisp_Object after = Foverlay_end (overlay);
21986 int ignore;
21988 /* Note that we might not be able to find position
21989 BEFORE in the glyph matrix if the overlay is
21990 entirely covered by a `display' property. In
21991 this case, we overshoot. So let's stop in
21992 the glyph matrix before glyphs for OBJECT. */
21993 fast_find_position (w, XFASTINT (before),
21994 &dpyinfo->mouse_face_beg_col,
21995 &dpyinfo->mouse_face_beg_row,
21996 &dpyinfo->mouse_face_beg_x,
21997 &dpyinfo->mouse_face_beg_y,
21998 object);
22000 dpyinfo->mouse_face_past_end
22001 = !fast_find_position (w, XFASTINT (after),
22002 &dpyinfo->mouse_face_end_col,
22003 &dpyinfo->mouse_face_end_row,
22004 &dpyinfo->mouse_face_end_x,
22005 &dpyinfo->mouse_face_end_y,
22006 Qnil);
22007 dpyinfo->mouse_face_window = window;
22008 dpyinfo->mouse_face_face_id
22009 = face_at_buffer_position (w, pos, 0, 0,
22010 &ignore, pos + 1,
22011 !dpyinfo->mouse_face_hidden);
22013 /* Display it as active. */
22014 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22015 cursor = No_Cursor;
22020 check_help_echo:
22022 /* Look for a `help-echo' property. */
22023 if (NILP (help_echo_string)) {
22024 Lisp_Object help, overlay;
22026 /* Check overlays first. */
22027 help = overlay = Qnil;
22028 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22030 overlay = overlay_vec[i];
22031 help = Foverlay_get (overlay, Qhelp_echo);
22034 if (!NILP (help))
22036 help_echo_string = help;
22037 help_echo_window = window;
22038 help_echo_object = overlay;
22039 help_echo_pos = pos;
22041 else
22043 Lisp_Object object = glyph->object;
22044 int charpos = glyph->charpos;
22046 /* Try text properties. */
22047 if (STRINGP (object)
22048 && charpos >= 0
22049 && charpos < SCHARS (object))
22051 help = Fget_text_property (make_number (charpos),
22052 Qhelp_echo, object);
22053 if (NILP (help))
22055 /* If the string itself doesn't specify a help-echo,
22056 see if the buffer text ``under'' it does. */
22057 struct glyph_row *r
22058 = MATRIX_ROW (w->current_matrix, vpos);
22059 int start = MATRIX_ROW_START_CHARPOS (r);
22060 int pos = string_buffer_position (w, object, start);
22061 if (pos > 0)
22063 help = Fget_char_property (make_number (pos),
22064 Qhelp_echo, w->buffer);
22065 if (!NILP (help))
22067 charpos = pos;
22068 object = w->buffer;
22073 else if (BUFFERP (object)
22074 && charpos >= BEGV
22075 && charpos < ZV)
22076 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22077 object);
22079 if (!NILP (help))
22081 help_echo_string = help;
22082 help_echo_window = window;
22083 help_echo_object = object;
22084 help_echo_pos = charpos;
22089 /* Look for a `pointer' property. */
22090 if (NILP (pointer))
22092 /* Check overlays first. */
22093 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22094 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22096 if (NILP (pointer))
22098 Lisp_Object object = glyph->object;
22099 int charpos = glyph->charpos;
22101 /* Try text properties. */
22102 if (STRINGP (object)
22103 && charpos >= 0
22104 && charpos < SCHARS (object))
22106 pointer = Fget_text_property (make_number (charpos),
22107 Qpointer, object);
22108 if (NILP (pointer))
22110 /* If the string itself doesn't specify a pointer,
22111 see if the buffer text ``under'' it does. */
22112 struct glyph_row *r
22113 = MATRIX_ROW (w->current_matrix, vpos);
22114 int start = MATRIX_ROW_START_CHARPOS (r);
22115 int pos = string_buffer_position (w, object, start);
22116 if (pos > 0)
22117 pointer = Fget_char_property (make_number (pos),
22118 Qpointer, w->buffer);
22121 else if (BUFFERP (object)
22122 && charpos >= BEGV
22123 && charpos < ZV)
22124 pointer = Fget_text_property (make_number (charpos),
22125 Qpointer, object);
22129 BEGV = obegv;
22130 ZV = ozv;
22131 current_buffer = obuf;
22134 set_cursor:
22136 define_frame_cursor1 (f, cursor, pointer);
22140 /* EXPORT for RIF:
22141 Clear any mouse-face on window W. This function is part of the
22142 redisplay interface, and is called from try_window_id and similar
22143 functions to ensure the mouse-highlight is off. */
22145 void
22146 x_clear_window_mouse_face (w)
22147 struct window *w;
22149 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22150 Lisp_Object window;
22152 BLOCK_INPUT;
22153 XSETWINDOW (window, w);
22154 if (EQ (window, dpyinfo->mouse_face_window))
22155 clear_mouse_face (dpyinfo);
22156 UNBLOCK_INPUT;
22160 /* EXPORT:
22161 Just discard the mouse face information for frame F, if any.
22162 This is used when the size of F is changed. */
22164 void
22165 cancel_mouse_face (f)
22166 struct frame *f;
22168 Lisp_Object window;
22169 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22171 window = dpyinfo->mouse_face_window;
22172 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22174 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22175 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22176 dpyinfo->mouse_face_window = Qnil;
22181 #endif /* HAVE_WINDOW_SYSTEM */
22184 /***********************************************************************
22185 Exposure Events
22186 ***********************************************************************/
22188 #ifdef HAVE_WINDOW_SYSTEM
22190 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22191 which intersects rectangle R. R is in window-relative coordinates. */
22193 static void
22194 expose_area (w, row, r, area)
22195 struct window *w;
22196 struct glyph_row *row;
22197 XRectangle *r;
22198 enum glyph_row_area area;
22200 struct glyph *first = row->glyphs[area];
22201 struct glyph *end = row->glyphs[area] + row->used[area];
22202 struct glyph *last;
22203 int first_x, start_x, x;
22205 if (area == TEXT_AREA && row->fill_line_p)
22206 /* If row extends face to end of line write the whole line. */
22207 draw_glyphs (w, 0, row, area,
22208 0, row->used[area],
22209 DRAW_NORMAL_TEXT, 0);
22210 else
22212 /* Set START_X to the window-relative start position for drawing glyphs of
22213 AREA. The first glyph of the text area can be partially visible.
22214 The first glyphs of other areas cannot. */
22215 start_x = window_box_left_offset (w, area);
22216 x = start_x;
22217 if (area == TEXT_AREA)
22218 x += row->x;
22220 /* Find the first glyph that must be redrawn. */
22221 while (first < end
22222 && x + first->pixel_width < r->x)
22224 x += first->pixel_width;
22225 ++first;
22228 /* Find the last one. */
22229 last = first;
22230 first_x = x;
22231 while (last < end
22232 && x < r->x + r->width)
22234 x += last->pixel_width;
22235 ++last;
22238 /* Repaint. */
22239 if (last > first)
22240 draw_glyphs (w, first_x - start_x, row, area,
22241 first - row->glyphs[area], last - row->glyphs[area],
22242 DRAW_NORMAL_TEXT, 0);
22247 /* Redraw the parts of the glyph row ROW on window W intersecting
22248 rectangle R. R is in window-relative coordinates. Value is
22249 non-zero if mouse-face was overwritten. */
22251 static int
22252 expose_line (w, row, r)
22253 struct window *w;
22254 struct glyph_row *row;
22255 XRectangle *r;
22257 xassert (row->enabled_p);
22259 if (row->mode_line_p || w->pseudo_window_p)
22260 draw_glyphs (w, 0, row, TEXT_AREA,
22261 0, row->used[TEXT_AREA],
22262 DRAW_NORMAL_TEXT, 0);
22263 else
22265 if (row->used[LEFT_MARGIN_AREA])
22266 expose_area (w, row, r, LEFT_MARGIN_AREA);
22267 if (row->used[TEXT_AREA])
22268 expose_area (w, row, r, TEXT_AREA);
22269 if (row->used[RIGHT_MARGIN_AREA])
22270 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22271 draw_row_fringe_bitmaps (w, row);
22274 return row->mouse_face_p;
22278 /* Redraw those parts of glyphs rows during expose event handling that
22279 overlap other rows. Redrawing of an exposed line writes over parts
22280 of lines overlapping that exposed line; this function fixes that.
22282 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22283 row in W's current matrix that is exposed and overlaps other rows.
22284 LAST_OVERLAPPING_ROW is the last such row. */
22286 static void
22287 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22288 struct window *w;
22289 struct glyph_row *first_overlapping_row;
22290 struct glyph_row *last_overlapping_row;
22292 struct glyph_row *row;
22294 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22295 if (row->overlapping_p)
22297 xassert (row->enabled_p && !row->mode_line_p);
22299 if (row->used[LEFT_MARGIN_AREA])
22300 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22302 if (row->used[TEXT_AREA])
22303 x_fix_overlapping_area (w, row, TEXT_AREA);
22305 if (row->used[RIGHT_MARGIN_AREA])
22306 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22311 /* Return non-zero if W's cursor intersects rectangle R. */
22313 static int
22314 phys_cursor_in_rect_p (w, r)
22315 struct window *w;
22316 XRectangle *r;
22318 XRectangle cr, result;
22319 struct glyph *cursor_glyph;
22321 cursor_glyph = get_phys_cursor_glyph (w);
22322 if (cursor_glyph)
22324 /* r is relative to W's box, but w->phys_cursor.x is relative
22325 to left edge of W's TEXT area. Adjust it. */
22326 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22327 cr.y = w->phys_cursor.y;
22328 cr.width = cursor_glyph->pixel_width;
22329 cr.height = w->phys_cursor_height;
22330 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22331 I assume the effect is the same -- and this is portable. */
22332 return x_intersect_rectangles (&cr, r, &result);
22334 else
22335 return 0;
22339 /* EXPORT:
22340 Draw a vertical window border to the right of window W if W doesn't
22341 have vertical scroll bars. */
22343 void
22344 x_draw_vertical_border (w)
22345 struct window *w;
22347 /* We could do better, if we knew what type of scroll-bar the adjacent
22348 windows (on either side) have... But we don't :-(
22349 However, I think this works ok. ++KFS 2003-04-25 */
22351 /* Redraw borders between horizontally adjacent windows. Don't
22352 do it for frames with vertical scroll bars because either the
22353 right scroll bar of a window, or the left scroll bar of its
22354 neighbor will suffice as a border. */
22355 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22356 return;
22358 if (!WINDOW_RIGHTMOST_P (w)
22359 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22361 int x0, x1, y0, y1;
22363 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22364 y1 -= 1;
22366 rif->draw_vertical_window_border (w, x1, y0, y1);
22368 else if (!WINDOW_LEFTMOST_P (w)
22369 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22371 int x0, x1, y0, y1;
22373 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22374 y1 -= 1;
22376 rif->draw_vertical_window_border (w, x0, y0, y1);
22381 /* Redraw the part of window W intersection rectangle FR. Pixel
22382 coordinates in FR are frame-relative. Call this function with
22383 input blocked. Value is non-zero if the exposure overwrites
22384 mouse-face. */
22386 static int
22387 expose_window (w, fr)
22388 struct window *w;
22389 XRectangle *fr;
22391 struct frame *f = XFRAME (w->frame);
22392 XRectangle wr, r;
22393 int mouse_face_overwritten_p = 0;
22395 /* If window is not yet fully initialized, do nothing. This can
22396 happen when toolkit scroll bars are used and a window is split.
22397 Reconfiguring the scroll bar will generate an expose for a newly
22398 created window. */
22399 if (w->current_matrix == NULL)
22400 return 0;
22402 /* When we're currently updating the window, display and current
22403 matrix usually don't agree. Arrange for a thorough display
22404 later. */
22405 if (w == updated_window)
22407 SET_FRAME_GARBAGED (f);
22408 return 0;
22411 /* Frame-relative pixel rectangle of W. */
22412 wr.x = WINDOW_LEFT_EDGE_X (w);
22413 wr.y = WINDOW_TOP_EDGE_Y (w);
22414 wr.width = WINDOW_TOTAL_WIDTH (w);
22415 wr.height = WINDOW_TOTAL_HEIGHT (w);
22417 if (x_intersect_rectangles (fr, &wr, &r))
22419 int yb = window_text_bottom_y (w);
22420 struct glyph_row *row;
22421 int cursor_cleared_p;
22422 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22424 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22425 r.x, r.y, r.width, r.height));
22427 /* Convert to window coordinates. */
22428 r.x -= WINDOW_LEFT_EDGE_X (w);
22429 r.y -= WINDOW_TOP_EDGE_Y (w);
22431 /* Turn off the cursor. */
22432 if (!w->pseudo_window_p
22433 && phys_cursor_in_rect_p (w, &r))
22435 x_clear_cursor (w);
22436 cursor_cleared_p = 1;
22438 else
22439 cursor_cleared_p = 0;
22441 /* Update lines intersecting rectangle R. */
22442 first_overlapping_row = last_overlapping_row = NULL;
22443 for (row = w->current_matrix->rows;
22444 row->enabled_p;
22445 ++row)
22447 int y0 = row->y;
22448 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22450 if ((y0 >= r.y && y0 < r.y + r.height)
22451 || (y1 > r.y && y1 < r.y + r.height)
22452 || (r.y >= y0 && r.y < y1)
22453 || (r.y + r.height > y0 && r.y + r.height < y1))
22455 /* A header line may be overlapping, but there is no need
22456 to fix overlapping areas for them. KFS 2005-02-12 */
22457 if (row->overlapping_p && !row->mode_line_p)
22459 if (first_overlapping_row == NULL)
22460 first_overlapping_row = row;
22461 last_overlapping_row = row;
22464 if (expose_line (w, row, &r))
22465 mouse_face_overwritten_p = 1;
22468 if (y1 >= yb)
22469 break;
22472 /* Display the mode line if there is one. */
22473 if (WINDOW_WANTS_MODELINE_P (w)
22474 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22475 row->enabled_p)
22476 && row->y < r.y + r.height)
22478 if (expose_line (w, row, &r))
22479 mouse_face_overwritten_p = 1;
22482 if (!w->pseudo_window_p)
22484 /* Fix the display of overlapping rows. */
22485 if (first_overlapping_row)
22486 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22488 /* Draw border between windows. */
22489 x_draw_vertical_border (w);
22491 /* Turn the cursor on again. */
22492 if (cursor_cleared_p)
22493 update_window_cursor (w, 1);
22497 return mouse_face_overwritten_p;
22502 /* Redraw (parts) of all windows in the window tree rooted at W that
22503 intersect R. R contains frame pixel coordinates. Value is
22504 non-zero if the exposure overwrites mouse-face. */
22506 static int
22507 expose_window_tree (w, r)
22508 struct window *w;
22509 XRectangle *r;
22511 struct frame *f = XFRAME (w->frame);
22512 int mouse_face_overwritten_p = 0;
22514 while (w && !FRAME_GARBAGED_P (f))
22516 if (!NILP (w->hchild))
22517 mouse_face_overwritten_p
22518 |= expose_window_tree (XWINDOW (w->hchild), r);
22519 else if (!NILP (w->vchild))
22520 mouse_face_overwritten_p
22521 |= expose_window_tree (XWINDOW (w->vchild), r);
22522 else
22523 mouse_face_overwritten_p |= expose_window (w, r);
22525 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22528 return mouse_face_overwritten_p;
22532 /* EXPORT:
22533 Redisplay an exposed area of frame F. X and Y are the upper-left
22534 corner of the exposed rectangle. W and H are width and height of
22535 the exposed area. All are pixel values. W or H zero means redraw
22536 the entire frame. */
22538 void
22539 expose_frame (f, x, y, w, h)
22540 struct frame *f;
22541 int x, y, w, h;
22543 XRectangle r;
22544 int mouse_face_overwritten_p = 0;
22546 TRACE ((stderr, "expose_frame "));
22548 /* No need to redraw if frame will be redrawn soon. */
22549 if (FRAME_GARBAGED_P (f))
22551 TRACE ((stderr, " garbaged\n"));
22552 return;
22555 /* If basic faces haven't been realized yet, there is no point in
22556 trying to redraw anything. This can happen when we get an expose
22557 event while Emacs is starting, e.g. by moving another window. */
22558 if (FRAME_FACE_CACHE (f) == NULL
22559 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22561 TRACE ((stderr, " no faces\n"));
22562 return;
22565 if (w == 0 || h == 0)
22567 r.x = r.y = 0;
22568 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22569 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22571 else
22573 r.x = x;
22574 r.y = y;
22575 r.width = w;
22576 r.height = h;
22579 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22580 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22582 if (WINDOWP (f->tool_bar_window))
22583 mouse_face_overwritten_p
22584 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22586 #ifdef HAVE_X_WINDOWS
22587 #ifndef MSDOS
22588 #ifndef USE_X_TOOLKIT
22589 if (WINDOWP (f->menu_bar_window))
22590 mouse_face_overwritten_p
22591 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22592 #endif /* not USE_X_TOOLKIT */
22593 #endif
22594 #endif
22596 /* Some window managers support a focus-follows-mouse style with
22597 delayed raising of frames. Imagine a partially obscured frame,
22598 and moving the mouse into partially obscured mouse-face on that
22599 frame. The visible part of the mouse-face will be highlighted,
22600 then the WM raises the obscured frame. With at least one WM, KDE
22601 2.1, Emacs is not getting any event for the raising of the frame
22602 (even tried with SubstructureRedirectMask), only Expose events.
22603 These expose events will draw text normally, i.e. not
22604 highlighted. Which means we must redo the highlight here.
22605 Subsume it under ``we love X''. --gerd 2001-08-15 */
22606 /* Included in Windows version because Windows most likely does not
22607 do the right thing if any third party tool offers
22608 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22609 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22611 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22612 if (f == dpyinfo->mouse_face_mouse_frame)
22614 int x = dpyinfo->mouse_face_mouse_x;
22615 int y = dpyinfo->mouse_face_mouse_y;
22616 clear_mouse_face (dpyinfo);
22617 note_mouse_highlight (f, x, y);
22623 /* EXPORT:
22624 Determine the intersection of two rectangles R1 and R2. Return
22625 the intersection in *RESULT. Value is non-zero if RESULT is not
22626 empty. */
22629 x_intersect_rectangles (r1, r2, result)
22630 XRectangle *r1, *r2, *result;
22632 XRectangle *left, *right;
22633 XRectangle *upper, *lower;
22634 int intersection_p = 0;
22636 /* Rearrange so that R1 is the left-most rectangle. */
22637 if (r1->x < r2->x)
22638 left = r1, right = r2;
22639 else
22640 left = r2, right = r1;
22642 /* X0 of the intersection is right.x0, if this is inside R1,
22643 otherwise there is no intersection. */
22644 if (right->x <= left->x + left->width)
22646 result->x = right->x;
22648 /* The right end of the intersection is the minimum of the
22649 the right ends of left and right. */
22650 result->width = (min (left->x + left->width, right->x + right->width)
22651 - result->x);
22653 /* Same game for Y. */
22654 if (r1->y < r2->y)
22655 upper = r1, lower = r2;
22656 else
22657 upper = r2, lower = r1;
22659 /* The upper end of the intersection is lower.y0, if this is inside
22660 of upper. Otherwise, there is no intersection. */
22661 if (lower->y <= upper->y + upper->height)
22663 result->y = lower->y;
22665 /* The lower end of the intersection is the minimum of the lower
22666 ends of upper and lower. */
22667 result->height = (min (lower->y + lower->height,
22668 upper->y + upper->height)
22669 - result->y);
22670 intersection_p = 1;
22674 return intersection_p;
22677 #endif /* HAVE_WINDOW_SYSTEM */
22680 /***********************************************************************
22681 Initialization
22682 ***********************************************************************/
22684 void
22685 syms_of_xdisp ()
22687 Vwith_echo_area_save_vector = Qnil;
22688 staticpro (&Vwith_echo_area_save_vector);
22690 Vmessage_stack = Qnil;
22691 staticpro (&Vmessage_stack);
22693 Qinhibit_redisplay = intern ("inhibit-redisplay");
22694 staticpro (&Qinhibit_redisplay);
22696 message_dolog_marker1 = Fmake_marker ();
22697 staticpro (&message_dolog_marker1);
22698 message_dolog_marker2 = Fmake_marker ();
22699 staticpro (&message_dolog_marker2);
22700 message_dolog_marker3 = Fmake_marker ();
22701 staticpro (&message_dolog_marker3);
22703 #if GLYPH_DEBUG
22704 defsubr (&Sdump_frame_glyph_matrix);
22705 defsubr (&Sdump_glyph_matrix);
22706 defsubr (&Sdump_glyph_row);
22707 defsubr (&Sdump_tool_bar_row);
22708 defsubr (&Strace_redisplay);
22709 defsubr (&Strace_to_stderr);
22710 #endif
22711 #ifdef HAVE_WINDOW_SYSTEM
22712 defsubr (&Stool_bar_lines_needed);
22713 defsubr (&Slookup_image_map);
22714 #endif
22715 defsubr (&Sformat_mode_line);
22717 staticpro (&Qmenu_bar_update_hook);
22718 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22720 staticpro (&Qoverriding_terminal_local_map);
22721 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22723 staticpro (&Qoverriding_local_map);
22724 Qoverriding_local_map = intern ("overriding-local-map");
22726 staticpro (&Qwindow_scroll_functions);
22727 Qwindow_scroll_functions = intern ("window-scroll-functions");
22729 staticpro (&Qredisplay_end_trigger_functions);
22730 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22732 staticpro (&Qinhibit_point_motion_hooks);
22733 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22735 QCdata = intern (":data");
22736 staticpro (&QCdata);
22737 Qdisplay = intern ("display");
22738 staticpro (&Qdisplay);
22739 Qspace_width = intern ("space-width");
22740 staticpro (&Qspace_width);
22741 Qraise = intern ("raise");
22742 staticpro (&Qraise);
22743 Qslice = intern ("slice");
22744 staticpro (&Qslice);
22745 Qspace = intern ("space");
22746 staticpro (&Qspace);
22747 Qmargin = intern ("margin");
22748 staticpro (&Qmargin);
22749 Qpointer = intern ("pointer");
22750 staticpro (&Qpointer);
22751 Qleft_margin = intern ("left-margin");
22752 staticpro (&Qleft_margin);
22753 Qright_margin = intern ("right-margin");
22754 staticpro (&Qright_margin);
22755 Qcenter = intern ("center");
22756 staticpro (&Qcenter);
22757 Qline_height = intern ("line-height");
22758 staticpro (&Qline_height);
22759 QCalign_to = intern (":align-to");
22760 staticpro (&QCalign_to);
22761 QCrelative_width = intern (":relative-width");
22762 staticpro (&QCrelative_width);
22763 QCrelative_height = intern (":relative-height");
22764 staticpro (&QCrelative_height);
22765 QCeval = intern (":eval");
22766 staticpro (&QCeval);
22767 QCpropertize = intern (":propertize");
22768 staticpro (&QCpropertize);
22769 QCfile = intern (":file");
22770 staticpro (&QCfile);
22771 Qfontified = intern ("fontified");
22772 staticpro (&Qfontified);
22773 Qfontification_functions = intern ("fontification-functions");
22774 staticpro (&Qfontification_functions);
22775 Qtrailing_whitespace = intern ("trailing-whitespace");
22776 staticpro (&Qtrailing_whitespace);
22777 Qescape_glyph = intern ("escape-glyph");
22778 staticpro (&Qescape_glyph);
22779 Qnobreak_space = intern ("nobreak-space");
22780 staticpro (&Qnobreak_space);
22781 Qimage = intern ("image");
22782 staticpro (&Qimage);
22783 QCmap = intern (":map");
22784 staticpro (&QCmap);
22785 QCpointer = intern (":pointer");
22786 staticpro (&QCpointer);
22787 Qrect = intern ("rect");
22788 staticpro (&Qrect);
22789 Qcircle = intern ("circle");
22790 staticpro (&Qcircle);
22791 Qpoly = intern ("poly");
22792 staticpro (&Qpoly);
22793 Qmessage_truncate_lines = intern ("message-truncate-lines");
22794 staticpro (&Qmessage_truncate_lines);
22795 Qgrow_only = intern ("grow-only");
22796 staticpro (&Qgrow_only);
22797 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22798 staticpro (&Qinhibit_menubar_update);
22799 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22800 staticpro (&Qinhibit_eval_during_redisplay);
22801 Qposition = intern ("position");
22802 staticpro (&Qposition);
22803 Qbuffer_position = intern ("buffer-position");
22804 staticpro (&Qbuffer_position);
22805 Qobject = intern ("object");
22806 staticpro (&Qobject);
22807 Qbar = intern ("bar");
22808 staticpro (&Qbar);
22809 Qhbar = intern ("hbar");
22810 staticpro (&Qhbar);
22811 Qbox = intern ("box");
22812 staticpro (&Qbox);
22813 Qhollow = intern ("hollow");
22814 staticpro (&Qhollow);
22815 Qhand = intern ("hand");
22816 staticpro (&Qhand);
22817 Qarrow = intern ("arrow");
22818 staticpro (&Qarrow);
22819 Qtext = intern ("text");
22820 staticpro (&Qtext);
22821 Qrisky_local_variable = intern ("risky-local-variable");
22822 staticpro (&Qrisky_local_variable);
22823 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22824 staticpro (&Qinhibit_free_realized_faces);
22826 list_of_error = Fcons (Fcons (intern ("error"),
22827 Fcons (intern ("void-variable"), Qnil)),
22828 Qnil);
22829 staticpro (&list_of_error);
22831 Qlast_arrow_position = intern ("last-arrow-position");
22832 staticpro (&Qlast_arrow_position);
22833 Qlast_arrow_string = intern ("last-arrow-string");
22834 staticpro (&Qlast_arrow_string);
22836 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22837 staticpro (&Qoverlay_arrow_string);
22838 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22839 staticpro (&Qoverlay_arrow_bitmap);
22841 echo_buffer[0] = echo_buffer[1] = Qnil;
22842 staticpro (&echo_buffer[0]);
22843 staticpro (&echo_buffer[1]);
22845 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22846 staticpro (&echo_area_buffer[0]);
22847 staticpro (&echo_area_buffer[1]);
22849 Vmessages_buffer_name = build_string ("*Messages*");
22850 staticpro (&Vmessages_buffer_name);
22852 mode_line_proptrans_alist = Qnil;
22853 staticpro (&mode_line_proptrans_alist);
22854 mode_line_string_list = Qnil;
22855 staticpro (&mode_line_string_list);
22856 mode_line_string_face = Qnil;
22857 staticpro (&mode_line_string_face);
22858 mode_line_string_face_prop = Qnil;
22859 staticpro (&mode_line_string_face_prop);
22860 Vmode_line_unwind_vector = Qnil;
22861 staticpro (&Vmode_line_unwind_vector);
22863 help_echo_string = Qnil;
22864 staticpro (&help_echo_string);
22865 help_echo_object = Qnil;
22866 staticpro (&help_echo_object);
22867 help_echo_window = Qnil;
22868 staticpro (&help_echo_window);
22869 previous_help_echo_string = Qnil;
22870 staticpro (&previous_help_echo_string);
22871 help_echo_pos = -1;
22873 #ifdef HAVE_WINDOW_SYSTEM
22874 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22875 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22876 For example, if a block cursor is over a tab, it will be drawn as
22877 wide as that tab on the display. */);
22878 x_stretch_cursor_p = 0;
22879 #endif
22881 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22882 doc: /* *Non-nil means highlight trailing whitespace.
22883 The face used for trailing whitespace is `trailing-whitespace'. */);
22884 Vshow_trailing_whitespace = Qnil;
22886 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
22887 doc: /* *Control highlighting of nobreak space and soft hyphen.
22888 A value of t means highlight the character itself (for nobreak space,
22889 use face `nobreak-space').
22890 A value of nil means no highlighting.
22891 Other values mean display the escape glyph followed by an ordinary
22892 space or ordinary hyphen. */);
22893 Vnobreak_char_display = Qt;
22895 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22896 doc: /* *The pointer shape to show in void text areas.
22897 A value of nil means to show the text pointer. Other options are `arrow',
22898 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22899 Vvoid_text_area_pointer = Qarrow;
22901 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22902 doc: /* Non-nil means don't actually do any redisplay.
22903 This is used for internal purposes. */);
22904 Vinhibit_redisplay = Qnil;
22906 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22907 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22908 Vglobal_mode_string = Qnil;
22910 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22911 doc: /* Marker for where to display an arrow on top of the buffer text.
22912 This must be the beginning of a line in order to work.
22913 See also `overlay-arrow-string'. */);
22914 Voverlay_arrow_position = Qnil;
22916 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22917 doc: /* String to display as an arrow in non-window frames.
22918 See also `overlay-arrow-position'. */);
22919 Voverlay_arrow_string = build_string ("=>");
22921 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22922 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22923 The symbols on this list are examined during redisplay to determine
22924 where to display overlay arrows. */);
22925 Voverlay_arrow_variable_list
22926 = Fcons (intern ("overlay-arrow-position"), Qnil);
22928 DEFVAR_INT ("scroll-step", &scroll_step,
22929 doc: /* *The number of lines to try scrolling a window by when point moves out.
22930 If that fails to bring point back on frame, point is centered instead.
22931 If this is zero, point is always centered after it moves off frame.
22932 If you want scrolling to always be a line at a time, you should set
22933 `scroll-conservatively' to a large value rather than set this to 1. */);
22935 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22936 doc: /* *Scroll up to this many lines, to bring point back on screen.
22937 A value of zero means to scroll the text to center point vertically
22938 in the window. */);
22939 scroll_conservatively = 0;
22941 DEFVAR_INT ("scroll-margin", &scroll_margin,
22942 doc: /* *Number of lines of margin at the top and bottom of a window.
22943 Recenter the window whenever point gets within this many lines
22944 of the top or bottom of the window. */);
22945 scroll_margin = 0;
22947 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22948 doc: /* Pixels per inch on current display.
22949 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22950 Vdisplay_pixels_per_inch = make_float (72.0);
22952 #if GLYPH_DEBUG
22953 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22954 #endif
22956 DEFVAR_BOOL ("truncate-partial-width-windows",
22957 &truncate_partial_width_windows,
22958 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22959 truncate_partial_width_windows = 1;
22961 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22962 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22963 Any other value means to use the appropriate face, `mode-line',
22964 `header-line', or `menu' respectively. */);
22965 mode_line_inverse_video = 1;
22967 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22968 doc: /* *Maximum buffer size for which line number should be displayed.
22969 If the buffer is bigger than this, the line number does not appear
22970 in the mode line. A value of nil means no limit. */);
22971 Vline_number_display_limit = Qnil;
22973 DEFVAR_INT ("line-number-display-limit-width",
22974 &line_number_display_limit_width,
22975 doc: /* *Maximum line width (in characters) for line number display.
22976 If the average length of the lines near point is bigger than this, then the
22977 line number may be omitted from the mode line. */);
22978 line_number_display_limit_width = 200;
22980 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22981 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22982 highlight_nonselected_windows = 0;
22984 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22985 doc: /* Non-nil if more than one frame is visible on this display.
22986 Minibuffer-only frames don't count, but iconified frames do.
22987 This variable is not guaranteed to be accurate except while processing
22988 `frame-title-format' and `icon-title-format'. */);
22990 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22991 doc: /* Template for displaying the title bar of visible frames.
22992 \(Assuming the window manager supports this feature.)
22993 This variable has the same structure as `mode-line-format' (which see),
22994 and is used only on frames for which no explicit name has been set
22995 \(see `modify-frame-parameters'). */);
22997 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22998 doc: /* Template for displaying the title bar of an iconified frame.
22999 \(Assuming the window manager supports this feature.)
23000 This variable has the same structure as `mode-line-format' (which see),
23001 and is used only on frames for which no explicit name has been set
23002 \(see `modify-frame-parameters'). */);
23003 Vicon_title_format
23004 = Vframe_title_format
23005 = Fcons (intern ("multiple-frames"),
23006 Fcons (build_string ("%b"),
23007 Fcons (Fcons (empty_string,
23008 Fcons (intern ("invocation-name"),
23009 Fcons (build_string ("@"),
23010 Fcons (intern ("system-name"),
23011 Qnil)))),
23012 Qnil)));
23014 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23015 doc: /* Maximum number of lines to keep in the message log buffer.
23016 If nil, disable message logging. If t, log messages but don't truncate
23017 the buffer when it becomes large. */);
23018 Vmessage_log_max = make_number (50);
23020 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23021 doc: /* Functions called before redisplay, if window sizes have changed.
23022 The value should be a list of functions that take one argument.
23023 Just before redisplay, for each frame, if any of its windows have changed
23024 size since the last redisplay, or have been split or deleted,
23025 all the functions in the list are called, with the frame as argument. */);
23026 Vwindow_size_change_functions = Qnil;
23028 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23029 doc: /* List of functions to call before redisplaying a window with scrolling.
23030 Each function is called with two arguments, the window
23031 and its new display-start position. Note that the value of `window-end'
23032 is not valid when these functions are called. */);
23033 Vwindow_scroll_functions = Qnil;
23035 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23036 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23037 mouse_autoselect_window = 0;
23039 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23040 doc: /* *Non-nil means automatically resize tool-bars.
23041 This increases a tool-bar's height if not all tool-bar items are visible.
23042 It decreases a tool-bar's height when it would display blank lines
23043 otherwise. */);
23044 auto_resize_tool_bars_p = 1;
23046 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23047 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23048 auto_raise_tool_bar_buttons_p = 1;
23050 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23051 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23052 make_cursor_line_fully_visible_p = 1;
23054 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23055 doc: /* *Margin around tool-bar buttons in pixels.
23056 If an integer, use that for both horizontal and vertical margins.
23057 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23058 HORZ specifying the horizontal margin, and VERT specifying the
23059 vertical margin. */);
23060 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23062 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23063 doc: /* *Relief thickness of tool-bar buttons. */);
23064 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23066 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23067 doc: /* List of functions to call to fontify regions of text.
23068 Each function is called with one argument POS. Functions must
23069 fontify a region starting at POS in the current buffer, and give
23070 fontified regions the property `fontified'. */);
23071 Vfontification_functions = Qnil;
23072 Fmake_variable_buffer_local (Qfontification_functions);
23074 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23075 &unibyte_display_via_language_environment,
23076 doc: /* *Non-nil means display unibyte text according to language environment.
23077 Specifically this means that unibyte non-ASCII characters
23078 are displayed by converting them to the equivalent multibyte characters
23079 according to the current language environment. As a result, they are
23080 displayed according to the current fontset. */);
23081 unibyte_display_via_language_environment = 0;
23083 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23084 doc: /* *Maximum height for resizing mini-windows.
23085 If a float, it specifies a fraction of the mini-window frame's height.
23086 If an integer, it specifies a number of lines. */);
23087 Vmax_mini_window_height = make_float (0.25);
23089 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23090 doc: /* *How to resize mini-windows.
23091 A value of nil means don't automatically resize mini-windows.
23092 A value of t means resize them to fit the text displayed in them.
23093 A value of `grow-only', the default, means let mini-windows grow
23094 only, until their display becomes empty, at which point the windows
23095 go back to their normal size. */);
23096 Vresize_mini_windows = Qgrow_only;
23098 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23099 doc: /* Alist specifying how to blink the cursor off.
23100 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23101 `cursor-type' frame-parameter or variable equals ON-STATE,
23102 comparing using `equal', Emacs uses OFF-STATE to specify
23103 how to blink it off. */);
23104 Vblink_cursor_alist = Qnil;
23106 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23107 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23108 automatic_hscrolling_p = 1;
23110 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23111 doc: /* *How many columns away from the window edge point is allowed to get
23112 before automatic hscrolling will horizontally scroll the window. */);
23113 hscroll_margin = 5;
23115 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23116 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23117 When point is less than `automatic-hscroll-margin' columns from the window
23118 edge, automatic hscrolling will scroll the window by the amount of columns
23119 determined by this variable. If its value is a positive integer, scroll that
23120 many columns. If it's a positive floating-point number, it specifies the
23121 fraction of the window's width to scroll. If it's nil or zero, point will be
23122 centered horizontally after the scroll. Any other value, including negative
23123 numbers, are treated as if the value were zero.
23125 Automatic hscrolling always moves point outside the scroll margin, so if
23126 point was more than scroll step columns inside the margin, the window will
23127 scroll more than the value given by the scroll step.
23129 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23130 and `scroll-right' overrides this variable's effect. */);
23131 Vhscroll_step = make_number (0);
23133 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23134 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23135 Bind this around calls to `message' to let it take effect. */);
23136 message_truncate_lines = 0;
23138 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23139 doc: /* Normal hook run to update the menu bar definitions.
23140 Redisplay runs this hook before it redisplays the menu bar.
23141 This is used to update submenus such as Buffers,
23142 whose contents depend on various data. */);
23143 Vmenu_bar_update_hook = Qnil;
23145 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23146 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23147 inhibit_menubar_update = 0;
23149 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23150 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23151 inhibit_eval_during_redisplay = 0;
23153 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23154 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23155 inhibit_free_realized_faces = 0;
23157 #if GLYPH_DEBUG
23158 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23159 doc: /* Inhibit try_window_id display optimization. */);
23160 inhibit_try_window_id = 0;
23162 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23163 doc: /* Inhibit try_window_reusing display optimization. */);
23164 inhibit_try_window_reusing = 0;
23166 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23167 doc: /* Inhibit try_cursor_movement display optimization. */);
23168 inhibit_try_cursor_movement = 0;
23169 #endif /* GLYPH_DEBUG */
23173 /* Initialize this module when Emacs starts. */
23175 void
23176 init_xdisp ()
23178 Lisp_Object root_window;
23179 struct window *mini_w;
23181 current_header_line_height = current_mode_line_height = -1;
23183 CHARPOS (this_line_start_pos) = 0;
23185 mini_w = XWINDOW (minibuf_window);
23186 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23188 if (!noninteractive)
23190 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23191 int i;
23193 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23194 set_window_height (root_window,
23195 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23197 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23198 set_window_height (minibuf_window, 1, 0);
23200 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23201 mini_w->total_cols = make_number (FRAME_COLS (f));
23203 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23204 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23205 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23207 /* The default ellipsis glyphs `...'. */
23208 for (i = 0; i < 3; ++i)
23209 default_invis_vector[i] = make_number ('.');
23213 /* Allocate the buffer for frame titles.
23214 Also used for `format-mode-line'. */
23215 int size = 100;
23216 mode_line_noprop_buf = (char *) xmalloc (size);
23217 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23218 mode_line_noprop_ptr = mode_line_noprop_buf;
23219 mode_line_target = MODE_LINE_DISPLAY;
23222 help_echo_showing_p = 0;
23226 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23227 (do not change this comment) */