Replace `string-to-int' by `string-to-number'.
[emacs.git] / src / xdisp.c
blob8bbe2811869320a7a835f861b6804d4d7672659e
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 Vshow_nonbreak_escape;
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 /* The symbol `image' which is the car of the lists used to represent
354 images in Lisp. */
356 Lisp_Object Qimage;
358 /* The image map types. */
359 Lisp_Object QCmap, QCpointer;
360 Lisp_Object Qrect, Qcircle, Qpoly;
362 /* Non-zero means print newline to stdout before next mini-buffer
363 message. */
365 int noninteractive_need_newline;
367 /* Non-zero means print newline to message log before next message. */
369 static int message_log_need_newline;
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
383 static struct text_pos this_line_start_pos;
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
388 static struct text_pos this_line_end_pos;
390 /* The vertical positions and the height of this line. */
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
399 static int this_line_start_x;
401 /* Buffer that this_line_.* variables are referring to. */
403 static struct buffer *this_line_buffer;
405 /* Nonzero means truncate lines in all windows less wide than the
406 frame. */
408 int truncate_partial_width_windows;
410 /* A flag to control how to display unibyte 8-bit character. */
412 int unibyte_display_via_language_environment;
414 /* Nonzero means we have more than one non-mini-buffer-only frame.
415 Not guaranteed to be accurate except while parsing
416 frame-title-format. */
418 int multiple_frames;
420 Lisp_Object Vglobal_mode_string;
423 /* List of variables (symbols) which hold markers for overlay arrows.
424 The symbols on this list are examined during redisplay to determine
425 where to display overlay arrows. */
427 Lisp_Object Voverlay_arrow_variable_list;
429 /* Marker for where to display an arrow on top of the buffer text. */
431 Lisp_Object Voverlay_arrow_position;
433 /* String to display for the arrow. Only used on terminal frames. */
435 Lisp_Object Voverlay_arrow_string;
437 /* Values of those variables at last redisplay are stored as
438 properties on `overlay-arrow-position' symbol. However, if
439 Voverlay_arrow_position is a marker, last-arrow-position is its
440 numerical position. */
442 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
444 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
445 properties on a symbol in overlay-arrow-variable-list. */
447 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
449 /* Like mode-line-format, but for the title bar on a visible frame. */
451 Lisp_Object Vframe_title_format;
453 /* Like mode-line-format, but for the title bar on an iconified frame. */
455 Lisp_Object Vicon_title_format;
457 /* List of functions to call when a window's size changes. These
458 functions get one arg, a frame on which one or more windows' sizes
459 have changed. */
461 static Lisp_Object Vwindow_size_change_functions;
463 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
465 /* Nonzero if an overlay arrow has been displayed in this window. */
467 static int overlay_arrow_seen;
469 /* Nonzero means highlight the region even in nonselected windows. */
471 int highlight_nonselected_windows;
473 /* If cursor motion alone moves point off frame, try scrolling this
474 many lines up or down if that will bring it back. */
476 static EMACS_INT scroll_step;
478 /* Nonzero means scroll just far enough to bring point back on the
479 screen, when appropriate. */
481 static EMACS_INT scroll_conservatively;
483 /* Recenter the window whenever point gets within this many lines of
484 the top or bottom of the window. This value is translated into a
485 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
486 that there is really a fixed pixel height scroll margin. */
488 EMACS_INT scroll_margin;
490 /* Number of windows showing the buffer of the selected window (or
491 another buffer with the same base buffer). keyboard.c refers to
492 this. */
494 int buffer_shared;
496 /* Vector containing glyphs for an ellipsis `...'. */
498 static Lisp_Object default_invis_vector[3];
500 /* Zero means display the mode-line/header-line/menu-bar in the default face
501 (this slightly odd definition is for compatibility with previous versions
502 of emacs), non-zero means display them using their respective faces.
504 This variable is deprecated. */
506 int mode_line_inverse_video;
508 /* Prompt to display in front of the mini-buffer contents. */
510 Lisp_Object minibuf_prompt;
512 /* Width of current mini-buffer prompt. Only set after display_line
513 of the line that contains the prompt. */
515 int minibuf_prompt_width;
517 /* This is the window where the echo area message was displayed. It
518 is always a mini-buffer window, but it may not be the same window
519 currently active as a mini-buffer. */
521 Lisp_Object echo_area_window;
523 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
524 pushes the current message and the value of
525 message_enable_multibyte on the stack, the function restore_message
526 pops the stack and displays MESSAGE again. */
528 Lisp_Object Vmessage_stack;
530 /* Nonzero means multibyte characters were enabled when the echo area
531 message was specified. */
533 int message_enable_multibyte;
535 /* Nonzero if we should redraw the mode lines on the next redisplay. */
537 int update_mode_lines;
539 /* Nonzero if window sizes or contents have changed since last
540 redisplay that finished. */
542 int windows_or_buffers_changed;
544 /* Nonzero means a frame's cursor type has been changed. */
546 int cursor_type_changed;
548 /* Nonzero after display_mode_line if %l was used and it displayed a
549 line number. */
551 int line_number_displayed;
553 /* Maximum buffer size for which to display line numbers. */
555 Lisp_Object Vline_number_display_limit;
557 /* Line width to consider when repositioning for line number display. */
559 static EMACS_INT line_number_display_limit_width;
561 /* Number of lines to keep in the message log buffer. t means
562 infinite. nil means don't log at all. */
564 Lisp_Object Vmessage_log_max;
566 /* The name of the *Messages* buffer, a string. */
568 static Lisp_Object Vmessages_buffer_name;
570 /* Index 0 is the buffer that holds the current (desired) echo area message,
571 or nil if none is desired right now.
573 Index 1 is the buffer that holds the previously displayed echo area message,
574 or nil to indicate no message. This is normally what's on the screen now.
576 These two can point to the same buffer. That happens when the last
577 message output by the user (or made by echoing) has been displayed. */
579 Lisp_Object echo_area_buffer[2];
581 /* Permanent pointers to the two buffers that are used for echo area
582 purposes. Once the two buffers are made, and their pointers are
583 placed here, these two slots remain unchanged unless those buffers
584 need to be created afresh. */
586 static Lisp_Object echo_buffer[2];
588 /* A vector saved used in with_area_buffer to reduce consing. */
590 static Lisp_Object Vwith_echo_area_save_vector;
592 /* Non-zero means display_echo_area should display the last echo area
593 message again. Set by redisplay_preserve_echo_area. */
595 static int display_last_displayed_message_p;
597 /* Nonzero if echo area is being used by print; zero if being used by
598 message. */
600 int message_buf_print;
602 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
604 Lisp_Object Qinhibit_menubar_update;
605 int inhibit_menubar_update;
607 /* Maximum height for resizing mini-windows. Either a float
608 specifying a fraction of the available height, or an integer
609 specifying a number of lines. */
611 Lisp_Object Vmax_mini_window_height;
613 /* Non-zero means messages should be displayed with truncated
614 lines instead of being continued. */
616 int message_truncate_lines;
617 Lisp_Object Qmessage_truncate_lines;
619 /* Set to 1 in clear_message to make redisplay_internal aware
620 of an emptied echo area. */
622 static int message_cleared_p;
624 /* How to blink the default frame cursor off. */
625 Lisp_Object Vblink_cursor_alist;
627 /* A scratch glyph row with contents used for generating truncation
628 glyphs. Also used in direct_output_for_insert. */
630 #define MAX_SCRATCH_GLYPHS 100
631 struct glyph_row scratch_glyph_row;
632 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
634 /* Ascent and height of the last line processed by move_it_to. */
636 static int last_max_ascent, last_height;
638 /* Non-zero if there's a help-echo in the echo area. */
640 int help_echo_showing_p;
642 /* If >= 0, computed, exact values of mode-line and header-line height
643 to use in the macros CURRENT_MODE_LINE_HEIGHT and
644 CURRENT_HEADER_LINE_HEIGHT. */
646 int current_mode_line_height, current_header_line_height;
648 /* The maximum distance to look ahead for text properties. Values
649 that are too small let us call compute_char_face and similar
650 functions too often which is expensive. Values that are too large
651 let us call compute_char_face and alike too often because we
652 might not be interested in text properties that far away. */
654 #define TEXT_PROP_DISTANCE_LIMIT 100
656 #if GLYPH_DEBUG
658 /* Variables to turn off display optimizations from Lisp. */
660 int inhibit_try_window_id, inhibit_try_window_reusing;
661 int inhibit_try_cursor_movement;
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG != 0. */
666 int trace_redisplay_p;
668 #endif /* GLYPH_DEBUG */
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
679 /* Non-zero means automatically scroll windows horizontally to make
680 point visible. */
682 int automatic_hscrolling_p;
684 /* How close to the margin can point get before the window is scrolled
685 horizontally. */
686 EMACS_INT hscroll_margin;
688 /* How much to scroll horizontally when point is inside the above margin. */
689 Lisp_Object Vhscroll_step;
691 /* The variable `resize-mini-windows'. If nil, don't resize
692 mini-windows. If t, always resize them to fit the text they
693 display. If `grow-only', let mini-windows grow only until they
694 become empty. */
696 Lisp_Object Vresize_mini_windows;
698 /* Buffer being redisplayed -- for redisplay_window_error. */
700 struct buffer *displayed_buffer;
702 /* Value returned from text property handlers (see below). */
704 enum prop_handled
706 HANDLED_NORMALLY,
707 HANDLED_RECOMPUTE_PROPS,
708 HANDLED_OVERLAY_STRING_CONSUMED,
709 HANDLED_RETURN
712 /* A description of text properties that redisplay is interested
713 in. */
715 struct props
717 /* The name of the property. */
718 Lisp_Object *name;
720 /* A unique index for the property. */
721 enum prop_idx idx;
723 /* A handler function called to set up iterator IT from the property
724 at IT's current position. Value is used to steer handle_stop. */
725 enum prop_handled (*handler) P_ ((struct it *it));
728 static enum prop_handled handle_face_prop P_ ((struct it *));
729 static enum prop_handled handle_invisible_prop P_ ((struct it *));
730 static enum prop_handled handle_display_prop P_ ((struct it *));
731 static enum prop_handled handle_composition_prop P_ ((struct it *));
732 static enum prop_handled handle_overlay_change P_ ((struct it *));
733 static enum prop_handled handle_fontified_prop P_ ((struct it *));
735 /* Properties handled by iterators. */
737 static struct props it_props[] =
739 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
740 /* Handle `face' before `display' because some sub-properties of
741 `display' need to know the face. */
742 {&Qface, FACE_PROP_IDX, handle_face_prop},
743 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
744 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
745 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
746 {NULL, 0, NULL}
749 /* Value is the position described by X. If X is a marker, value is
750 the marker_position of X. Otherwise, value is X. */
752 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
754 /* Enumeration returned by some move_it_.* functions internally. */
756 enum move_it_result
758 /* Not used. Undefined value. */
759 MOVE_UNDEFINED,
761 /* Move ended at the requested buffer position or ZV. */
762 MOVE_POS_MATCH_OR_ZV,
764 /* Move ended at the requested X pixel position. */
765 MOVE_X_REACHED,
767 /* Move within a line ended at the end of a line that must be
768 continued. */
769 MOVE_LINE_CONTINUED,
771 /* Move within a line ended at the end of a line that would
772 be displayed truncated. */
773 MOVE_LINE_TRUNCATED,
775 /* Move within a line ended at a line end. */
776 MOVE_NEWLINE_OR_CR
779 /* This counter is used to clear the face cache every once in a while
780 in redisplay_internal. It is incremented for each redisplay.
781 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
782 cleared. */
784 #define CLEAR_FACE_CACHE_COUNT 500
785 static int clear_face_cache_count;
787 /* Similarly for the image cache. */
789 #ifdef HAVE_WINDOW_SYSTEM
790 #define CLEAR_IMAGE_CACHE_COUNT 101
791 static int clear_image_cache_count;
792 #endif
794 /* Record the previous terminal frame we displayed. */
796 static struct frame *previous_terminal_frame;
798 /* Non-zero while redisplay_internal is in progress. */
800 int redisplaying_p;
802 /* Non-zero means don't free realized faces. Bound while freeing
803 realized faces is dangerous because glyph matrices might still
804 reference them. */
806 int inhibit_free_realized_faces;
807 Lisp_Object Qinhibit_free_realized_faces;
809 /* If a string, XTread_socket generates an event to display that string.
810 (The display is done in read_char.) */
812 Lisp_Object help_echo_string;
813 Lisp_Object help_echo_window;
814 Lisp_Object help_echo_object;
815 int help_echo_pos;
817 /* Temporary variable for XTread_socket. */
819 Lisp_Object previous_help_echo_string;
821 /* Null glyph slice */
823 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
826 /* Function prototypes. */
828 static void setup_for_ellipsis P_ ((struct it *, int));
829 static void mark_window_display_accurate_1 P_ ((struct window *, int));
830 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
831 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
832 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
833 static int redisplay_mode_lines P_ ((Lisp_Object, int));
834 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
836 #if 0
837 static int invisible_text_between_p P_ ((struct it *, int, int));
838 #endif
840 static void pint2str P_ ((char *, int, int));
841 static void pint2hrstr P_ ((char *, int, int));
842 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
843 struct text_pos));
844 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
845 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
846 static void store_frame_title_char P_ ((char));
847 static int store_frame_title P_ ((const unsigned char *, int, int));
848 static void x_consider_frame_title P_ ((Lisp_Object));
849 static void handle_stop P_ ((struct it *));
850 static int tool_bar_lines_needed P_ ((struct frame *));
851 static int single_display_spec_intangible_p P_ ((Lisp_Object));
852 static void ensure_echo_area_buffers P_ ((void));
853 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
854 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
855 static int with_echo_area_buffer P_ ((struct window *, int,
856 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
857 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
858 static void clear_garbaged_frames P_ ((void));
859 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
860 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
861 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
862 static int display_echo_area P_ ((struct window *));
863 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
864 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
866 static int string_char_and_length P_ ((const unsigned char *, int, int *));
867 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
868 struct text_pos));
869 static int compute_window_start_on_continuation_line P_ ((struct window *));
870 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
871 static void insert_left_trunc_glyphs P_ ((struct it *));
872 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
873 Lisp_Object));
874 static void extend_face_to_end_of_line P_ ((struct it *));
875 static int append_space_for_newline P_ ((struct it *, int));
876 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
877 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
878 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
879 static int trailing_whitespace_p P_ ((int));
880 static int message_log_check_duplicate P_ ((int, int, int, int));
881 static void push_it P_ ((struct it *));
882 static void pop_it P_ ((struct it *));
883 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
884 static void select_frame_for_redisplay P_ ((Lisp_Object));
885 static void redisplay_internal P_ ((int));
886 static int echo_area_display P_ ((int));
887 static void redisplay_windows P_ ((Lisp_Object));
888 static void redisplay_window P_ ((Lisp_Object, int));
889 static Lisp_Object redisplay_window_error ();
890 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
891 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
892 static void update_menu_bar P_ ((struct frame *, int));
893 static int try_window_reusing_current_matrix P_ ((struct window *));
894 static int try_window_id P_ ((struct window *));
895 static int display_line P_ ((struct it *));
896 static int display_mode_lines P_ ((struct window *));
897 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
898 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
899 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
900 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
901 static void display_menu_bar P_ ((struct window *));
902 static int display_count_lines P_ ((int, int, int, int, int *));
903 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
904 int, int, struct it *, int, int, int, int));
905 static void compute_line_metrics P_ ((struct it *));
906 static void run_redisplay_end_trigger_hook P_ ((struct it *));
907 static int get_overlay_strings P_ ((struct it *, int));
908 static void next_overlay_string P_ ((struct it *));
909 static void reseat P_ ((struct it *, struct text_pos, int));
910 static void reseat_1 P_ ((struct it *, struct text_pos, int));
911 static void back_to_previous_visible_line_start P_ ((struct it *));
912 void reseat_at_previous_visible_line_start P_ ((struct it *));
913 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
914 static int next_element_from_ellipsis P_ ((struct it *));
915 static int next_element_from_display_vector P_ ((struct it *));
916 static int next_element_from_string P_ ((struct it *));
917 static int next_element_from_c_string P_ ((struct it *));
918 static int next_element_from_buffer P_ ((struct it *));
919 static int next_element_from_composition P_ ((struct it *));
920 static int next_element_from_image P_ ((struct it *));
921 static int next_element_from_stretch P_ ((struct it *));
922 static void load_overlay_strings P_ ((struct it *, int));
923 static int init_from_display_pos P_ ((struct it *, struct window *,
924 struct display_pos *));
925 static void reseat_to_string P_ ((struct it *, unsigned char *,
926 Lisp_Object, int, int, int, int));
927 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
928 int, int, int));
929 void move_it_vertically_backward P_ ((struct it *, int));
930 static void init_to_row_start P_ ((struct it *, struct window *,
931 struct glyph_row *));
932 static int init_to_row_end P_ ((struct it *, struct window *,
933 struct glyph_row *));
934 static void back_to_previous_line_start P_ ((struct it *));
935 static int forward_to_next_line_start P_ ((struct it *, int *));
936 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
937 Lisp_Object, int));
938 static struct text_pos string_pos P_ ((int, Lisp_Object));
939 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
940 static int number_of_chars P_ ((unsigned char *, int));
941 static void compute_stop_pos P_ ((struct it *));
942 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
943 Lisp_Object));
944 static int face_before_or_after_it_pos P_ ((struct it *, int));
945 static int next_overlay_change P_ ((int));
946 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
947 Lisp_Object, struct text_pos *,
948 int));
949 static int underlying_face_id P_ ((struct it *));
950 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
951 struct window *));
953 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
954 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
956 #ifdef HAVE_WINDOW_SYSTEM
958 static void update_tool_bar P_ ((struct frame *, int));
959 static void build_desired_tool_bar_string P_ ((struct frame *f));
960 static int redisplay_tool_bar P_ ((struct frame *));
961 static void display_tool_bar_line P_ ((struct it *));
962 static void notice_overwritten_cursor P_ ((struct window *,
963 enum glyph_row_area,
964 int, int, int, int));
968 #endif /* HAVE_WINDOW_SYSTEM */
971 /***********************************************************************
972 Window display dimensions
973 ***********************************************************************/
975 /* Return the bottom boundary y-position for text lines in window W.
976 This is the first y position at which a line cannot start.
977 It is relative to the top of the window.
979 This is the height of W minus the height of a mode line, if any. */
981 INLINE int
982 window_text_bottom_y (w)
983 struct window *w;
985 int height = WINDOW_TOTAL_HEIGHT (w);
987 if (WINDOW_WANTS_MODELINE_P (w))
988 height -= CURRENT_MODE_LINE_HEIGHT (w);
989 return height;
992 /* Return the pixel width of display area AREA of window W. AREA < 0
993 means return the total width of W, not including fringes to
994 the left and right of the window. */
996 INLINE int
997 window_box_width (w, area)
998 struct window *w;
999 int area;
1001 int cols = XFASTINT (w->total_cols);
1002 int pixels = 0;
1004 if (!w->pseudo_window_p)
1006 cols -= WINDOW_SCROLL_BAR_COLS (w);
1008 if (area == TEXT_AREA)
1010 if (INTEGERP (w->left_margin_cols))
1011 cols -= XFASTINT (w->left_margin_cols);
1012 if (INTEGERP (w->right_margin_cols))
1013 cols -= XFASTINT (w->right_margin_cols);
1014 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1016 else if (area == LEFT_MARGIN_AREA)
1018 cols = (INTEGERP (w->left_margin_cols)
1019 ? XFASTINT (w->left_margin_cols) : 0);
1020 pixels = 0;
1022 else if (area == RIGHT_MARGIN_AREA)
1024 cols = (INTEGERP (w->right_margin_cols)
1025 ? XFASTINT (w->right_margin_cols) : 0);
1026 pixels = 0;
1030 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1034 /* Return the pixel height of the display area of window W, not
1035 including mode lines of W, if any. */
1037 INLINE int
1038 window_box_height (w)
1039 struct window *w;
1041 struct frame *f = XFRAME (w->frame);
1042 int height = WINDOW_TOTAL_HEIGHT (w);
1044 xassert (height >= 0);
1046 /* Note: the code below that determines the mode-line/header-line
1047 height is essentially the same as that contained in the macro
1048 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1049 the appropriate glyph row has its `mode_line_p' flag set,
1050 and if it doesn't, uses estimate_mode_line_height instead. */
1052 if (WINDOW_WANTS_MODELINE_P (w))
1054 struct glyph_row *ml_row
1055 = (w->current_matrix && w->current_matrix->rows
1056 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1057 : 0);
1058 if (ml_row && ml_row->mode_line_p)
1059 height -= ml_row->height;
1060 else
1061 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1064 if (WINDOW_WANTS_HEADER_LINE_P (w))
1066 struct glyph_row *hl_row
1067 = (w->current_matrix && w->current_matrix->rows
1068 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1069 : 0);
1070 if (hl_row && hl_row->mode_line_p)
1071 height -= hl_row->height;
1072 else
1073 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1076 /* With a very small font and a mode-line that's taller than
1077 default, we might end up with a negative height. */
1078 return max (0, height);
1081 /* Return the window-relative coordinate of the left edge of display
1082 area AREA of window W. AREA < 0 means return the left edge of the
1083 whole window, to the right of the left fringe of W. */
1085 INLINE int
1086 window_box_left_offset (w, area)
1087 struct window *w;
1088 int area;
1090 int x;
1092 if (w->pseudo_window_p)
1093 return 0;
1095 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1097 if (area == TEXT_AREA)
1098 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1099 + window_box_width (w, LEFT_MARGIN_AREA));
1100 else if (area == RIGHT_MARGIN_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA)
1103 + window_box_width (w, TEXT_AREA)
1104 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1106 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1107 else if (area == LEFT_MARGIN_AREA
1108 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1109 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1111 return x;
1115 /* Return the window-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the left edge of the
1117 whole window, to the left of the right fringe of W. */
1119 INLINE int
1120 window_box_right_offset (w, area)
1121 struct window *w;
1122 int area;
1124 return window_box_left_offset (w, area) + window_box_width (w, area);
1127 /* Return the frame-relative coordinate of the left edge of display
1128 area AREA of window W. AREA < 0 means return the left edge of the
1129 whole window, to the right of the left fringe of W. */
1131 INLINE int
1132 window_box_left (w, area)
1133 struct window *w;
1134 int area;
1136 struct frame *f = XFRAME (w->frame);
1137 int x;
1139 if (w->pseudo_window_p)
1140 return FRAME_INTERNAL_BORDER_WIDTH (f);
1142 x = (WINDOW_LEFT_EDGE_X (w)
1143 + window_box_left_offset (w, area));
1145 return x;
1149 /* Return the frame-relative coordinate of the right edge of display
1150 area AREA of window W. AREA < 0 means return the left edge of the
1151 whole window, to the left of the right fringe of W. */
1153 INLINE int
1154 window_box_right (w, area)
1155 struct window *w;
1156 int area;
1158 return window_box_left (w, area) + window_box_width (w, area);
1161 /* Get the bounding box of the display area AREA of window W, without
1162 mode lines, in frame-relative coordinates. AREA < 0 means the
1163 whole window, not including the left and right fringes of
1164 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1165 coordinates of the upper-left corner of the box. Return in
1166 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1168 INLINE void
1169 window_box (w, area, box_x, box_y, box_width, box_height)
1170 struct window *w;
1171 int area;
1172 int *box_x, *box_y, *box_width, *box_height;
1174 if (box_width)
1175 *box_width = window_box_width (w, area);
1176 if (box_height)
1177 *box_height = window_box_height (w);
1178 if (box_x)
1179 *box_x = window_box_left (w, area);
1180 if (box_y)
1182 *box_y = WINDOW_TOP_EDGE_Y (w);
1183 if (WINDOW_WANTS_HEADER_LINE_P (w))
1184 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1189 /* Get the bounding box of the display area AREA of window W, without
1190 mode lines. AREA < 0 means the whole window, not including the
1191 left and right fringe of the window. Return in *TOP_LEFT_X
1192 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1193 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1194 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1195 box. */
1197 INLINE void
1198 window_box_edges (w, area, top_left_x, top_left_y,
1199 bottom_right_x, bottom_right_y)
1200 struct window *w;
1201 int area;
1202 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1204 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1205 bottom_right_y);
1206 *bottom_right_x += *top_left_x;
1207 *bottom_right_y += *top_left_y;
1212 /***********************************************************************
1213 Utilities
1214 ***********************************************************************/
1216 /* Return the bottom y-position of the line the iterator IT is in.
1217 This can modify IT's settings. */
1220 line_bottom_y (it)
1221 struct it *it;
1223 int line_height = it->max_ascent + it->max_descent;
1224 int line_top_y = it->current_y;
1226 if (line_height == 0)
1228 if (last_height)
1229 line_height = last_height;
1230 else if (IT_CHARPOS (*it) < ZV)
1232 move_it_by_lines (it, 1, 1);
1233 line_height = (it->max_ascent || it->max_descent
1234 ? it->max_ascent + it->max_descent
1235 : last_height);
1237 else
1239 struct glyph_row *row = it->glyph_row;
1241 /* Use the default character height. */
1242 it->glyph_row = NULL;
1243 it->what = IT_CHARACTER;
1244 it->c = ' ';
1245 it->len = 1;
1246 PRODUCE_GLYPHS (it);
1247 line_height = it->ascent + it->descent;
1248 it->glyph_row = row;
1252 return line_top_y + line_height;
1256 /* Return 1 if position CHARPOS is visible in window W.
1257 If visible, set *X and *Y to pixel coordinates of top left corner.
1258 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1259 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1260 and header-lines heights. */
1263 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1264 struct window *w;
1265 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1267 struct it it;
1268 struct text_pos top;
1269 int visible_p = 0;
1270 struct buffer *old_buffer = NULL;
1272 if (noninteractive)
1273 return visible_p;
1275 if (XBUFFER (w->buffer) != current_buffer)
1277 old_buffer = current_buffer;
1278 set_buffer_internal_1 (XBUFFER (w->buffer));
1281 SET_TEXT_POS_FROM_MARKER (top, w->start);
1283 /* Compute exact mode line heights, if requested. */
1284 if (exact_mode_line_heights_p)
1286 if (WINDOW_WANTS_MODELINE_P (w))
1287 current_mode_line_height
1288 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1289 current_buffer->mode_line_format);
1291 if (WINDOW_WANTS_HEADER_LINE_P (w))
1292 current_header_line_height
1293 = display_mode_line (w, HEADER_LINE_FACE_ID,
1294 current_buffer->header_line_format);
1297 start_display (&it, w, top);
1298 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1299 MOVE_TO_POS | MOVE_TO_Y);
1301 /* Note that we may overshoot because of invisible text. */
1302 if (IT_CHARPOS (it) >= charpos)
1304 int top_x = it.current_x;
1305 int top_y = it.current_y;
1306 int bottom_y = (last_height = 0, line_bottom_y (&it));
1307 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1309 if (top_y < window_top_y)
1310 visible_p = bottom_y > window_top_y;
1311 else if (top_y < it.last_visible_y)
1312 visible_p = 1;
1313 if (visible_p)
1315 *x = top_x;
1316 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1317 *rtop = max (0, window_top_y - top_y);
1318 *rbot = max (0, bottom_y - it.last_visible_y);
1321 else
1323 struct it it2;
1325 it2 = it;
1326 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1327 move_it_by_lines (&it, 1, 0);
1328 if (charpos < IT_CHARPOS (it))
1330 visible_p = 1;
1331 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1332 *x = it2.current_x;
1333 *y = it2.current_y + it2.max_ascent - it2.ascent;
1334 *rtop = max (0, -it2.current_y);
1335 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1336 - it.last_visible_y));
1340 if (old_buffer)
1341 set_buffer_internal_1 (old_buffer);
1343 current_header_line_height = current_mode_line_height = -1;
1345 return visible_p;
1349 /* Return the next character from STR which is MAXLEN bytes long.
1350 Return in *LEN the length of the character. This is like
1351 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1352 we find one, we return a `?', but with the length of the invalid
1353 character. */
1355 static INLINE int
1356 string_char_and_length (str, maxlen, len)
1357 const unsigned char *str;
1358 int maxlen, *len;
1360 int c;
1362 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1363 if (!CHAR_VALID_P (c, 1))
1364 /* We may not change the length here because other places in Emacs
1365 don't use this function, i.e. they silently accept invalid
1366 characters. */
1367 c = '?';
1369 return c;
1374 /* Given a position POS containing a valid character and byte position
1375 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1377 static struct text_pos
1378 string_pos_nchars_ahead (pos, string, nchars)
1379 struct text_pos pos;
1380 Lisp_Object string;
1381 int nchars;
1383 xassert (STRINGP (string) && nchars >= 0);
1385 if (STRING_MULTIBYTE (string))
1387 int rest = SBYTES (string) - BYTEPOS (pos);
1388 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1389 int len;
1391 while (nchars--)
1393 string_char_and_length (p, rest, &len);
1394 p += len, rest -= len;
1395 xassert (rest >= 0);
1396 CHARPOS (pos) += 1;
1397 BYTEPOS (pos) += len;
1400 else
1401 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1403 return pos;
1407 /* Value is the text position, i.e. character and byte position,
1408 for character position CHARPOS in STRING. */
1410 static INLINE struct text_pos
1411 string_pos (charpos, string)
1412 int charpos;
1413 Lisp_Object string;
1415 struct text_pos pos;
1416 xassert (STRINGP (string));
1417 xassert (charpos >= 0);
1418 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1419 return pos;
1423 /* Value is a text position, i.e. character and byte position, for
1424 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1425 means recognize multibyte characters. */
1427 static struct text_pos
1428 c_string_pos (charpos, s, multibyte_p)
1429 int charpos;
1430 unsigned char *s;
1431 int multibyte_p;
1433 struct text_pos pos;
1435 xassert (s != NULL);
1436 xassert (charpos >= 0);
1438 if (multibyte_p)
1440 int rest = strlen (s), len;
1442 SET_TEXT_POS (pos, 0, 0);
1443 while (charpos--)
1445 string_char_and_length (s, rest, &len);
1446 s += len, rest -= len;
1447 xassert (rest >= 0);
1448 CHARPOS (pos) += 1;
1449 BYTEPOS (pos) += len;
1452 else
1453 SET_TEXT_POS (pos, charpos, charpos);
1455 return pos;
1459 /* Value is the number of characters in C string S. MULTIBYTE_P
1460 non-zero means recognize multibyte characters. */
1462 static int
1463 number_of_chars (s, multibyte_p)
1464 unsigned char *s;
1465 int multibyte_p;
1467 int nchars;
1469 if (multibyte_p)
1471 int rest = strlen (s), len;
1472 unsigned char *p = (unsigned char *) s;
1474 for (nchars = 0; rest > 0; ++nchars)
1476 string_char_and_length (p, rest, &len);
1477 rest -= len, p += len;
1480 else
1481 nchars = strlen (s);
1483 return nchars;
1487 /* Compute byte position NEWPOS->bytepos corresponding to
1488 NEWPOS->charpos. POS is a known position in string STRING.
1489 NEWPOS->charpos must be >= POS.charpos. */
1491 static void
1492 compute_string_pos (newpos, pos, string)
1493 struct text_pos *newpos, pos;
1494 Lisp_Object string;
1496 xassert (STRINGP (string));
1497 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1499 if (STRING_MULTIBYTE (string))
1500 *newpos = string_pos_nchars_ahead (pos, string,
1501 CHARPOS (*newpos) - CHARPOS (pos));
1502 else
1503 BYTEPOS (*newpos) = CHARPOS (*newpos);
1506 /* EXPORT:
1507 Return an estimation of the pixel height of mode or top lines on
1508 frame F. FACE_ID specifies what line's height to estimate. */
1511 estimate_mode_line_height (f, face_id)
1512 struct frame *f;
1513 enum face_id face_id;
1515 #ifdef HAVE_WINDOW_SYSTEM
1516 if (FRAME_WINDOW_P (f))
1518 int height = FONT_HEIGHT (FRAME_FONT (f));
1520 /* This function is called so early when Emacs starts that the face
1521 cache and mode line face are not yet initialized. */
1522 if (FRAME_FACE_CACHE (f))
1524 struct face *face = FACE_FROM_ID (f, face_id);
1525 if (face)
1527 if (face->font)
1528 height = FONT_HEIGHT (face->font);
1529 if (face->box_line_width > 0)
1530 height += 2 * face->box_line_width;
1534 return height;
1536 #endif
1538 return 1;
1541 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1542 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1543 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1544 not force the value into range. */
1546 void
1547 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1548 FRAME_PTR f;
1549 register int pix_x, pix_y;
1550 int *x, *y;
1551 NativeRectangle *bounds;
1552 int noclip;
1555 #ifdef HAVE_WINDOW_SYSTEM
1556 if (FRAME_WINDOW_P (f))
1558 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1559 even for negative values. */
1560 if (pix_x < 0)
1561 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1562 if (pix_y < 0)
1563 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1565 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1566 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1568 if (bounds)
1569 STORE_NATIVE_RECT (*bounds,
1570 FRAME_COL_TO_PIXEL_X (f, pix_x),
1571 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1572 FRAME_COLUMN_WIDTH (f) - 1,
1573 FRAME_LINE_HEIGHT (f) - 1);
1575 if (!noclip)
1577 if (pix_x < 0)
1578 pix_x = 0;
1579 else if (pix_x > FRAME_TOTAL_COLS (f))
1580 pix_x = FRAME_TOTAL_COLS (f);
1582 if (pix_y < 0)
1583 pix_y = 0;
1584 else if (pix_y > FRAME_LINES (f))
1585 pix_y = FRAME_LINES (f);
1588 #endif
1590 *x = pix_x;
1591 *y = pix_y;
1595 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1596 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1597 can't tell the positions because W's display is not up to date,
1598 return 0. */
1601 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1602 struct window *w;
1603 int hpos, vpos;
1604 int *frame_x, *frame_y;
1606 #ifdef HAVE_WINDOW_SYSTEM
1607 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1609 int success_p;
1611 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1612 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1614 if (display_completed)
1616 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1617 struct glyph *glyph = row->glyphs[TEXT_AREA];
1618 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1620 hpos = row->x;
1621 vpos = row->y;
1622 while (glyph < end)
1624 hpos += glyph->pixel_width;
1625 ++glyph;
1628 /* If first glyph is partially visible, its first visible position is still 0. */
1629 if (hpos < 0)
1630 hpos = 0;
1632 success_p = 1;
1634 else
1636 hpos = vpos = 0;
1637 success_p = 0;
1640 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1641 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1642 return success_p;
1644 #endif
1646 *frame_x = hpos;
1647 *frame_y = vpos;
1648 return 1;
1652 #ifdef HAVE_WINDOW_SYSTEM
1654 /* Find the glyph under window-relative coordinates X/Y in window W.
1655 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1656 strings. Return in *HPOS and *VPOS the row and column number of
1657 the glyph found. Return in *AREA the glyph area containing X.
1658 Value is a pointer to the glyph found or null if X/Y is not on
1659 text, or we can't tell because W's current matrix is not up to
1660 date. */
1662 static struct glyph *
1663 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1664 struct window *w;
1665 int x, y;
1666 int *hpos, *vpos, *dx, *dy, *area;
1668 struct glyph *glyph, *end;
1669 struct glyph_row *row = NULL;
1670 int x0, i;
1672 /* Find row containing Y. Give up if some row is not enabled. */
1673 for (i = 0; i < w->current_matrix->nrows; ++i)
1675 row = MATRIX_ROW (w->current_matrix, i);
1676 if (!row->enabled_p)
1677 return NULL;
1678 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1679 break;
1682 *vpos = i;
1683 *hpos = 0;
1685 /* Give up if Y is not in the window. */
1686 if (i == w->current_matrix->nrows)
1687 return NULL;
1689 /* Get the glyph area containing X. */
1690 if (w->pseudo_window_p)
1692 *area = TEXT_AREA;
1693 x0 = 0;
1695 else
1697 if (x < window_box_left_offset (w, TEXT_AREA))
1699 *area = LEFT_MARGIN_AREA;
1700 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1702 else if (x < window_box_right_offset (w, TEXT_AREA))
1704 *area = TEXT_AREA;
1705 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1707 else
1709 *area = RIGHT_MARGIN_AREA;
1710 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1714 /* Find glyph containing X. */
1715 glyph = row->glyphs[*area];
1716 end = glyph + row->used[*area];
1717 x -= x0;
1718 while (glyph < end && x >= glyph->pixel_width)
1720 x -= glyph->pixel_width;
1721 ++glyph;
1724 if (glyph == end)
1725 return NULL;
1727 if (dx)
1729 *dx = x;
1730 *dy = y - (row->y + row->ascent - glyph->ascent);
1733 *hpos = glyph - row->glyphs[*area];
1734 return glyph;
1738 /* EXPORT:
1739 Convert frame-relative x/y to coordinates relative to window W.
1740 Takes pseudo-windows into account. */
1742 void
1743 frame_to_window_pixel_xy (w, x, y)
1744 struct window *w;
1745 int *x, *y;
1747 if (w->pseudo_window_p)
1749 /* A pseudo-window is always full-width, and starts at the
1750 left edge of the frame, plus a frame border. */
1751 struct frame *f = XFRAME (w->frame);
1752 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1753 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1755 else
1757 *x -= WINDOW_LEFT_EDGE_X (w);
1758 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1762 /* EXPORT:
1763 Return in *R the clipping rectangle for glyph string S. */
1765 void
1766 get_glyph_string_clip_rect (s, nr)
1767 struct glyph_string *s;
1768 NativeRectangle *nr;
1770 XRectangle r;
1772 if (s->row->full_width_p)
1774 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1775 r.x = WINDOW_LEFT_EDGE_X (s->w);
1776 r.width = WINDOW_TOTAL_WIDTH (s->w);
1778 /* Unless displaying a mode or menu bar line, which are always
1779 fully visible, clip to the visible part of the row. */
1780 if (s->w->pseudo_window_p)
1781 r.height = s->row->visible_height;
1782 else
1783 r.height = s->height;
1785 else
1787 /* This is a text line that may be partially visible. */
1788 r.x = window_box_left (s->w, s->area);
1789 r.width = window_box_width (s->w, s->area);
1790 r.height = s->row->visible_height;
1793 if (s->clip_head)
1794 if (r.x < s->clip_head->x)
1796 if (r.width >= s->clip_head->x - r.x)
1797 r.width -= s->clip_head->x - r.x;
1798 else
1799 r.width = 0;
1800 r.x = s->clip_head->x;
1802 if (s->clip_tail)
1803 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1805 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1806 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1807 else
1808 r.width = 0;
1811 /* If S draws overlapping rows, it's sufficient to use the top and
1812 bottom of the window for clipping because this glyph string
1813 intentionally draws over other lines. */
1814 if (s->for_overlaps_p)
1816 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1817 r.height = window_text_bottom_y (s->w) - r.y;
1819 else
1821 /* Don't use S->y for clipping because it doesn't take partially
1822 visible lines into account. For example, it can be negative for
1823 partially visible lines at the top of a window. */
1824 if (!s->row->full_width_p
1825 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1826 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1827 else
1828 r.y = max (0, s->row->y);
1830 /* If drawing a tool-bar window, draw it over the internal border
1831 at the top of the window. */
1832 if (WINDOWP (s->f->tool_bar_window)
1833 && s->w == XWINDOW (s->f->tool_bar_window))
1834 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1837 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1839 /* If drawing the cursor, don't let glyph draw outside its
1840 advertised boundaries. Cleartype does this under some circumstances. */
1841 if (s->hl == DRAW_CURSOR)
1843 struct glyph *glyph = s->first_glyph;
1844 int height, max_y;
1846 if (s->x > r.x)
1848 r.width -= s->x - r.x;
1849 r.x = s->x;
1851 r.width = min (r.width, glyph->pixel_width);
1853 /* If r.y is below window bottom, ensure that we still see a cursor. */
1854 height = min (glyph->ascent + glyph->descent,
1855 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1856 max_y = window_text_bottom_y (s->w) - height;
1857 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1858 if (s->ybase - glyph->ascent > max_y)
1860 r.y = max_y;
1861 r.height = height;
1863 else
1865 /* Don't draw cursor glyph taller than our actual glyph. */
1866 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1867 if (height < r.height)
1869 max_y = r.y + r.height;
1870 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1871 r.height = min (max_y - r.y, height);
1876 #ifdef CONVERT_FROM_XRECT
1877 CONVERT_FROM_XRECT (r, *nr);
1878 #else
1879 *nr = r;
1880 #endif
1884 /* EXPORT:
1885 Return the position and height of the phys cursor in window W.
1886 Set w->phys_cursor_width to width of phys cursor.
1890 get_phys_cursor_geometry (w, row, glyph, heightp)
1891 struct window *w;
1892 struct glyph_row *row;
1893 struct glyph *glyph;
1894 int *heightp;
1896 struct frame *f = XFRAME (WINDOW_FRAME (w));
1897 int x, y, wd, h, h0, y0;
1899 /* Compute the width of the rectangle to draw. If on a stretch
1900 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1901 rectangle as wide as the glyph, but use a canonical character
1902 width instead. */
1903 wd = glyph->pixel_width - 1;
1904 #ifdef HAVE_NTGUI
1905 wd++; /* Why? */
1906 #endif
1907 if (glyph->type == STRETCH_GLYPH
1908 && !x_stretch_cursor_p)
1909 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1910 w->phys_cursor_width = wd;
1912 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1914 /* If y is below window bottom, ensure that we still see a cursor. */
1915 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1917 h = max (h0, glyph->ascent + glyph->descent);
1918 h0 = min (h0, glyph->ascent + glyph->descent);
1920 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1921 if (y < y0)
1923 h = max (h - (y0 - y) + 1, h0);
1924 y = y0 - 1;
1926 else
1928 y0 = window_text_bottom_y (w) - h0;
1929 if (y > y0)
1931 h += y - y0;
1932 y = y0;
1936 *heightp = h - 1;
1937 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1941 #endif /* HAVE_WINDOW_SYSTEM */
1944 /***********************************************************************
1945 Lisp form evaluation
1946 ***********************************************************************/
1948 /* Error handler for safe_eval and safe_call. */
1950 static Lisp_Object
1951 safe_eval_handler (arg)
1952 Lisp_Object arg;
1954 add_to_log ("Error during redisplay: %s", arg, Qnil);
1955 return Qnil;
1959 /* Evaluate SEXPR and return the result, or nil if something went
1960 wrong. Prevent redisplay during the evaluation. */
1962 Lisp_Object
1963 safe_eval (sexpr)
1964 Lisp_Object sexpr;
1966 Lisp_Object val;
1968 if (inhibit_eval_during_redisplay)
1969 val = Qnil;
1970 else
1972 int count = SPECPDL_INDEX ();
1973 struct gcpro gcpro1;
1975 GCPRO1 (sexpr);
1976 specbind (Qinhibit_redisplay, Qt);
1977 /* Use Qt to ensure debugger does not run,
1978 so there is no possibility of wanting to redisplay. */
1979 val = internal_condition_case_1 (Feval, sexpr, Qt,
1980 safe_eval_handler);
1981 UNGCPRO;
1982 val = unbind_to (count, val);
1985 return val;
1989 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1990 Return the result, or nil if something went wrong. Prevent
1991 redisplay during the evaluation. */
1993 Lisp_Object
1994 safe_call (nargs, args)
1995 int nargs;
1996 Lisp_Object *args;
1998 Lisp_Object val;
2000 if (inhibit_eval_during_redisplay)
2001 val = Qnil;
2002 else
2004 int count = SPECPDL_INDEX ();
2005 struct gcpro gcpro1;
2007 GCPRO1 (args[0]);
2008 gcpro1.nvars = nargs;
2009 specbind (Qinhibit_redisplay, Qt);
2010 /* Use Qt to ensure debugger does not run,
2011 so there is no possibility of wanting to redisplay. */
2012 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2013 safe_eval_handler);
2014 UNGCPRO;
2015 val = unbind_to (count, val);
2018 return val;
2022 /* Call function FN with one argument ARG.
2023 Return the result, or nil if something went wrong. */
2025 Lisp_Object
2026 safe_call1 (fn, arg)
2027 Lisp_Object fn, arg;
2029 Lisp_Object args[2];
2030 args[0] = fn;
2031 args[1] = arg;
2032 return safe_call (2, args);
2037 /***********************************************************************
2038 Debugging
2039 ***********************************************************************/
2041 #if 0
2043 /* Define CHECK_IT to perform sanity checks on iterators.
2044 This is for debugging. It is too slow to do unconditionally. */
2046 static void
2047 check_it (it)
2048 struct it *it;
2050 if (it->method == GET_FROM_STRING)
2052 xassert (STRINGP (it->string));
2053 xassert (IT_STRING_CHARPOS (*it) >= 0);
2055 else
2057 xassert (IT_STRING_CHARPOS (*it) < 0);
2058 if (it->method == GET_FROM_BUFFER)
2060 /* Check that character and byte positions agree. */
2061 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2065 if (it->dpvec)
2066 xassert (it->current.dpvec_index >= 0);
2067 else
2068 xassert (it->current.dpvec_index < 0);
2071 #define CHECK_IT(IT) check_it ((IT))
2073 #else /* not 0 */
2075 #define CHECK_IT(IT) (void) 0
2077 #endif /* not 0 */
2080 #if GLYPH_DEBUG
2082 /* Check that the window end of window W is what we expect it
2083 to be---the last row in the current matrix displaying text. */
2085 static void
2086 check_window_end (w)
2087 struct window *w;
2089 if (!MINI_WINDOW_P (w)
2090 && !NILP (w->window_end_valid))
2092 struct glyph_row *row;
2093 xassert ((row = MATRIX_ROW (w->current_matrix,
2094 XFASTINT (w->window_end_vpos)),
2095 !row->enabled_p
2096 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2097 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2101 #define CHECK_WINDOW_END(W) check_window_end ((W))
2103 #else /* not GLYPH_DEBUG */
2105 #define CHECK_WINDOW_END(W) (void) 0
2107 #endif /* not GLYPH_DEBUG */
2111 /***********************************************************************
2112 Iterator initialization
2113 ***********************************************************************/
2115 /* Initialize IT for displaying current_buffer in window W, starting
2116 at character position CHARPOS. CHARPOS < 0 means that no buffer
2117 position is specified which is useful when the iterator is assigned
2118 a position later. BYTEPOS is the byte position corresponding to
2119 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2121 If ROW is not null, calls to produce_glyphs with IT as parameter
2122 will produce glyphs in that row.
2124 BASE_FACE_ID is the id of a base face to use. It must be one of
2125 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2126 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2127 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2129 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2130 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2131 will be initialized to use the corresponding mode line glyph row of
2132 the desired matrix of W. */
2134 void
2135 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2136 struct it *it;
2137 struct window *w;
2138 int charpos, bytepos;
2139 struct glyph_row *row;
2140 enum face_id base_face_id;
2142 int highlight_region_p;
2144 /* Some precondition checks. */
2145 xassert (w != NULL && it != NULL);
2146 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2147 && charpos <= ZV));
2149 /* If face attributes have been changed since the last redisplay,
2150 free realized faces now because they depend on face definitions
2151 that might have changed. Don't free faces while there might be
2152 desired matrices pending which reference these faces. */
2153 if (face_change_count && !inhibit_free_realized_faces)
2155 face_change_count = 0;
2156 free_all_realized_faces (Qnil);
2159 /* Use one of the mode line rows of W's desired matrix if
2160 appropriate. */
2161 if (row == NULL)
2163 if (base_face_id == MODE_LINE_FACE_ID
2164 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2165 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2166 else if (base_face_id == HEADER_LINE_FACE_ID)
2167 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2170 /* Clear IT. */
2171 bzero (it, sizeof *it);
2172 it->current.overlay_string_index = -1;
2173 it->current.dpvec_index = -1;
2174 it->base_face_id = base_face_id;
2175 it->string = Qnil;
2176 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2178 /* The window in which we iterate over current_buffer: */
2179 XSETWINDOW (it->window, w);
2180 it->w = w;
2181 it->f = XFRAME (w->frame);
2183 /* Extra space between lines (on window systems only). */
2184 if (base_face_id == DEFAULT_FACE_ID
2185 && FRAME_WINDOW_P (it->f))
2187 if (NATNUMP (current_buffer->extra_line_spacing))
2188 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2189 else if (FLOATP (current_buffer->extra_line_spacing))
2190 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2191 * FRAME_LINE_HEIGHT (it->f));
2192 else if (it->f->extra_line_spacing > 0)
2193 it->extra_line_spacing = it->f->extra_line_spacing;
2194 it->max_extra_line_spacing = 0;
2197 /* If realized faces have been removed, e.g. because of face
2198 attribute changes of named faces, recompute them. When running
2199 in batch mode, the face cache of Vterminal_frame is null. If
2200 we happen to get called, make a dummy face cache. */
2201 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2202 init_frame_faces (it->f);
2203 if (FRAME_FACE_CACHE (it->f)->used == 0)
2204 recompute_basic_faces (it->f);
2206 /* Current value of the `slice', `space-width', and 'height' properties. */
2207 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2208 it->space_width = Qnil;
2209 it->font_height = Qnil;
2210 it->override_ascent = -1;
2212 /* Are control characters displayed as `^C'? */
2213 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2215 /* -1 means everything between a CR and the following line end
2216 is invisible. >0 means lines indented more than this value are
2217 invisible. */
2218 it->selective = (INTEGERP (current_buffer->selective_display)
2219 ? XFASTINT (current_buffer->selective_display)
2220 : (!NILP (current_buffer->selective_display)
2221 ? -1 : 0));
2222 it->selective_display_ellipsis_p
2223 = !NILP (current_buffer->selective_display_ellipses);
2225 /* Display table to use. */
2226 it->dp = window_display_table (w);
2228 /* Are multibyte characters enabled in current_buffer? */
2229 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2231 /* Non-zero if we should highlight the region. */
2232 highlight_region_p
2233 = (!NILP (Vtransient_mark_mode)
2234 && !NILP (current_buffer->mark_active)
2235 && XMARKER (current_buffer->mark)->buffer != 0);
2237 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2238 start and end of a visible region in window IT->w. Set both to
2239 -1 to indicate no region. */
2240 if (highlight_region_p
2241 /* Maybe highlight only in selected window. */
2242 && (/* Either show region everywhere. */
2243 highlight_nonselected_windows
2244 /* Or show region in the selected window. */
2245 || w == XWINDOW (selected_window)
2246 /* Or show the region if we are in the mini-buffer and W is
2247 the window the mini-buffer refers to. */
2248 || (MINI_WINDOW_P (XWINDOW (selected_window))
2249 && WINDOWP (minibuf_selected_window)
2250 && w == XWINDOW (minibuf_selected_window))))
2252 int charpos = marker_position (current_buffer->mark);
2253 it->region_beg_charpos = min (PT, charpos);
2254 it->region_end_charpos = max (PT, charpos);
2256 else
2257 it->region_beg_charpos = it->region_end_charpos = -1;
2259 /* Get the position at which the redisplay_end_trigger hook should
2260 be run, if it is to be run at all. */
2261 if (MARKERP (w->redisplay_end_trigger)
2262 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2263 it->redisplay_end_trigger_charpos
2264 = marker_position (w->redisplay_end_trigger);
2265 else if (INTEGERP (w->redisplay_end_trigger))
2266 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2268 /* Correct bogus values of tab_width. */
2269 it->tab_width = XINT (current_buffer->tab_width);
2270 if (it->tab_width <= 0 || it->tab_width > 1000)
2271 it->tab_width = 8;
2273 /* Are lines in the display truncated? */
2274 it->truncate_lines_p
2275 = (base_face_id != DEFAULT_FACE_ID
2276 || XINT (it->w->hscroll)
2277 || (truncate_partial_width_windows
2278 && !WINDOW_FULL_WIDTH_P (it->w))
2279 || !NILP (current_buffer->truncate_lines));
2281 /* Get dimensions of truncation and continuation glyphs. These are
2282 displayed as fringe bitmaps under X, so we don't need them for such
2283 frames. */
2284 if (!FRAME_WINDOW_P (it->f))
2286 if (it->truncate_lines_p)
2288 /* We will need the truncation glyph. */
2289 xassert (it->glyph_row == NULL);
2290 produce_special_glyphs (it, IT_TRUNCATION);
2291 it->truncation_pixel_width = it->pixel_width;
2293 else
2295 /* We will need the continuation glyph. */
2296 xassert (it->glyph_row == NULL);
2297 produce_special_glyphs (it, IT_CONTINUATION);
2298 it->continuation_pixel_width = it->pixel_width;
2301 /* Reset these values to zero because the produce_special_glyphs
2302 above has changed them. */
2303 it->pixel_width = it->ascent = it->descent = 0;
2304 it->phys_ascent = it->phys_descent = 0;
2307 /* Set this after getting the dimensions of truncation and
2308 continuation glyphs, so that we don't produce glyphs when calling
2309 produce_special_glyphs, above. */
2310 it->glyph_row = row;
2311 it->area = TEXT_AREA;
2313 /* Get the dimensions of the display area. The display area
2314 consists of the visible window area plus a horizontally scrolled
2315 part to the left of the window. All x-values are relative to the
2316 start of this total display area. */
2317 if (base_face_id != DEFAULT_FACE_ID)
2319 /* Mode lines, menu bar in terminal frames. */
2320 it->first_visible_x = 0;
2321 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2323 else
2325 it->first_visible_x
2326 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2327 it->last_visible_x = (it->first_visible_x
2328 + window_box_width (w, TEXT_AREA));
2330 /* If we truncate lines, leave room for the truncator glyph(s) at
2331 the right margin. Otherwise, leave room for the continuation
2332 glyph(s). Truncation and continuation glyphs are not inserted
2333 for window-based redisplay. */
2334 if (!FRAME_WINDOW_P (it->f))
2336 if (it->truncate_lines_p)
2337 it->last_visible_x -= it->truncation_pixel_width;
2338 else
2339 it->last_visible_x -= it->continuation_pixel_width;
2342 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2343 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2346 /* Leave room for a border glyph. */
2347 if (!FRAME_WINDOW_P (it->f)
2348 && !WINDOW_RIGHTMOST_P (it->w))
2349 it->last_visible_x -= 1;
2351 it->last_visible_y = window_text_bottom_y (w);
2353 /* For mode lines and alike, arrange for the first glyph having a
2354 left box line if the face specifies a box. */
2355 if (base_face_id != DEFAULT_FACE_ID)
2357 struct face *face;
2359 it->face_id = base_face_id;
2361 /* If we have a boxed mode line, make the first character appear
2362 with a left box line. */
2363 face = FACE_FROM_ID (it->f, base_face_id);
2364 if (face->box != FACE_NO_BOX)
2365 it->start_of_box_run_p = 1;
2368 /* If a buffer position was specified, set the iterator there,
2369 getting overlays and face properties from that position. */
2370 if (charpos >= BUF_BEG (current_buffer))
2372 it->end_charpos = ZV;
2373 it->face_id = -1;
2374 IT_CHARPOS (*it) = charpos;
2376 /* Compute byte position if not specified. */
2377 if (bytepos < charpos)
2378 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2379 else
2380 IT_BYTEPOS (*it) = bytepos;
2382 it->start = it->current;
2384 /* Compute faces etc. */
2385 reseat (it, it->current.pos, 1);
2388 CHECK_IT (it);
2392 /* Initialize IT for the display of window W with window start POS. */
2394 void
2395 start_display (it, w, pos)
2396 struct it *it;
2397 struct window *w;
2398 struct text_pos pos;
2400 struct glyph_row *row;
2401 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2403 row = w->desired_matrix->rows + first_vpos;
2404 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2405 it->first_vpos = first_vpos;
2407 if (!it->truncate_lines_p)
2409 int start_at_line_beg_p;
2410 int first_y = it->current_y;
2412 /* If window start is not at a line start, skip forward to POS to
2413 get the correct continuation lines width. */
2414 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2415 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2416 if (!start_at_line_beg_p)
2418 int new_x;
2420 reseat_at_previous_visible_line_start (it);
2421 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2423 new_x = it->current_x + it->pixel_width;
2425 /* If lines are continued, this line may end in the middle
2426 of a multi-glyph character (e.g. a control character
2427 displayed as \003, or in the middle of an overlay
2428 string). In this case move_it_to above will not have
2429 taken us to the start of the continuation line but to the
2430 end of the continued line. */
2431 if (it->current_x > 0
2432 && !it->truncate_lines_p /* Lines are continued. */
2433 && (/* And glyph doesn't fit on the line. */
2434 new_x > it->last_visible_x
2435 /* Or it fits exactly and we're on a window
2436 system frame. */
2437 || (new_x == it->last_visible_x
2438 && FRAME_WINDOW_P (it->f))))
2440 if (it->current.dpvec_index >= 0
2441 || it->current.overlay_string_index >= 0)
2443 set_iterator_to_next (it, 1);
2444 move_it_in_display_line_to (it, -1, -1, 0);
2447 it->continuation_lines_width += it->current_x;
2450 /* We're starting a new display line, not affected by the
2451 height of the continued line, so clear the appropriate
2452 fields in the iterator structure. */
2453 it->max_ascent = it->max_descent = 0;
2454 it->max_phys_ascent = it->max_phys_descent = 0;
2456 it->current_y = first_y;
2457 it->vpos = 0;
2458 it->current_x = it->hpos = 0;
2462 #if 0 /* Don't assert the following because start_display is sometimes
2463 called intentionally with a window start that is not at a
2464 line start. Please leave this code in as a comment. */
2466 /* Window start should be on a line start, now. */
2467 xassert (it->continuation_lines_width
2468 || IT_CHARPOS (it) == BEGV
2469 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2470 #endif /* 0 */
2474 /* Return 1 if POS is a position in ellipses displayed for invisible
2475 text. W is the window we display, for text property lookup. */
2477 static int
2478 in_ellipses_for_invisible_text_p (pos, w)
2479 struct display_pos *pos;
2480 struct window *w;
2482 Lisp_Object prop, window;
2483 int ellipses_p = 0;
2484 int charpos = CHARPOS (pos->pos);
2486 /* If POS specifies a position in a display vector, this might
2487 be for an ellipsis displayed for invisible text. We won't
2488 get the iterator set up for delivering that ellipsis unless
2489 we make sure that it gets aware of the invisible text. */
2490 if (pos->dpvec_index >= 0
2491 && pos->overlay_string_index < 0
2492 && CHARPOS (pos->string_pos) < 0
2493 && charpos > BEGV
2494 && (XSETWINDOW (window, w),
2495 prop = Fget_char_property (make_number (charpos),
2496 Qinvisible, window),
2497 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2499 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2500 window);
2501 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2504 return ellipses_p;
2508 /* Initialize IT for stepping through current_buffer in window W,
2509 starting at position POS that includes overlay string and display
2510 vector/ control character translation position information. Value
2511 is zero if there are overlay strings with newlines at POS. */
2513 static int
2514 init_from_display_pos (it, w, pos)
2515 struct it *it;
2516 struct window *w;
2517 struct display_pos *pos;
2519 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2520 int i, overlay_strings_with_newlines = 0;
2522 /* If POS specifies a position in a display vector, this might
2523 be for an ellipsis displayed for invisible text. We won't
2524 get the iterator set up for delivering that ellipsis unless
2525 we make sure that it gets aware of the invisible text. */
2526 if (in_ellipses_for_invisible_text_p (pos, w))
2528 --charpos;
2529 bytepos = 0;
2532 /* Keep in mind: the call to reseat in init_iterator skips invisible
2533 text, so we might end up at a position different from POS. This
2534 is only a problem when POS is a row start after a newline and an
2535 overlay starts there with an after-string, and the overlay has an
2536 invisible property. Since we don't skip invisible text in
2537 display_line and elsewhere immediately after consuming the
2538 newline before the row start, such a POS will not be in a string,
2539 but the call to init_iterator below will move us to the
2540 after-string. */
2541 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2543 /* This only scans the current chunk -- it should scan all chunks.
2544 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2545 to 16 in 22.1 to make this a lesser problem. */
2546 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2548 const char *s = SDATA (it->overlay_strings[i]);
2549 const char *e = s + SBYTES (it->overlay_strings[i]);
2551 while (s < e && *s != '\n')
2552 ++s;
2554 if (s < e)
2556 overlay_strings_with_newlines = 1;
2557 break;
2561 /* If position is within an overlay string, set up IT to the right
2562 overlay string. */
2563 if (pos->overlay_string_index >= 0)
2565 int relative_index;
2567 /* If the first overlay string happens to have a `display'
2568 property for an image, the iterator will be set up for that
2569 image, and we have to undo that setup first before we can
2570 correct the overlay string index. */
2571 if (it->method == GET_FROM_IMAGE)
2572 pop_it (it);
2574 /* We already have the first chunk of overlay strings in
2575 IT->overlay_strings. Load more until the one for
2576 pos->overlay_string_index is in IT->overlay_strings. */
2577 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2579 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2580 it->current.overlay_string_index = 0;
2581 while (n--)
2583 load_overlay_strings (it, 0);
2584 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2588 it->current.overlay_string_index = pos->overlay_string_index;
2589 relative_index = (it->current.overlay_string_index
2590 % OVERLAY_STRING_CHUNK_SIZE);
2591 it->string = it->overlay_strings[relative_index];
2592 xassert (STRINGP (it->string));
2593 it->current.string_pos = pos->string_pos;
2594 it->method = GET_FROM_STRING;
2597 #if 0 /* This is bogus because POS not having an overlay string
2598 position does not mean it's after the string. Example: A
2599 line starting with a before-string and initialization of IT
2600 to the previous row's end position. */
2601 else if (it->current.overlay_string_index >= 0)
2603 /* If POS says we're already after an overlay string ending at
2604 POS, make sure to pop the iterator because it will be in
2605 front of that overlay string. When POS is ZV, we've thereby
2606 also ``processed'' overlay strings at ZV. */
2607 while (it->sp)
2608 pop_it (it);
2609 it->current.overlay_string_index = -1;
2610 it->method = GET_FROM_BUFFER;
2611 if (CHARPOS (pos->pos) == ZV)
2612 it->overlay_strings_at_end_processed_p = 1;
2614 #endif /* 0 */
2616 if (CHARPOS (pos->string_pos) >= 0)
2618 /* Recorded position is not in an overlay string, but in another
2619 string. This can only be a string from a `display' property.
2620 IT should already be filled with that string. */
2621 it->current.string_pos = pos->string_pos;
2622 xassert (STRINGP (it->string));
2625 /* Restore position in display vector translations, control
2626 character translations or ellipses. */
2627 if (pos->dpvec_index >= 0)
2629 if (it->dpvec == NULL)
2630 get_next_display_element (it);
2631 xassert (it->dpvec && it->current.dpvec_index == 0);
2632 it->current.dpvec_index = pos->dpvec_index;
2635 CHECK_IT (it);
2636 return !overlay_strings_with_newlines;
2640 /* Initialize IT for stepping through current_buffer in window W
2641 starting at ROW->start. */
2643 static void
2644 init_to_row_start (it, w, row)
2645 struct it *it;
2646 struct window *w;
2647 struct glyph_row *row;
2649 init_from_display_pos (it, w, &row->start);
2650 it->start = row->start;
2651 it->continuation_lines_width = row->continuation_lines_width;
2652 CHECK_IT (it);
2656 /* Initialize IT for stepping through current_buffer in window W
2657 starting in the line following ROW, i.e. starting at ROW->end.
2658 Value is zero if there are overlay strings with newlines at ROW's
2659 end position. */
2661 static int
2662 init_to_row_end (it, w, row)
2663 struct it *it;
2664 struct window *w;
2665 struct glyph_row *row;
2667 int success = 0;
2669 if (init_from_display_pos (it, w, &row->end))
2671 if (row->continued_p)
2672 it->continuation_lines_width
2673 = row->continuation_lines_width + row->pixel_width;
2674 CHECK_IT (it);
2675 success = 1;
2678 return success;
2684 /***********************************************************************
2685 Text properties
2686 ***********************************************************************/
2688 /* Called when IT reaches IT->stop_charpos. Handle text property and
2689 overlay changes. Set IT->stop_charpos to the next position where
2690 to stop. */
2692 static void
2693 handle_stop (it)
2694 struct it *it;
2696 enum prop_handled handled;
2697 int handle_overlay_change_p = 1;
2698 struct props *p;
2700 it->dpvec = NULL;
2701 it->current.dpvec_index = -1;
2703 /* Use face of preceding text for ellipsis (if invisible) */
2704 if (it->selective_display_ellipsis_p)
2705 it->saved_face_id = it->face_id;
2709 handled = HANDLED_NORMALLY;
2711 /* Call text property handlers. */
2712 for (p = it_props; p->handler; ++p)
2714 handled = p->handler (it);
2716 if (handled == HANDLED_RECOMPUTE_PROPS)
2717 break;
2718 else if (handled == HANDLED_RETURN)
2719 return;
2720 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2721 handle_overlay_change_p = 0;
2724 if (handled != HANDLED_RECOMPUTE_PROPS)
2726 /* Don't check for overlay strings below when set to deliver
2727 characters from a display vector. */
2728 if (it->method == GET_FROM_DISPLAY_VECTOR)
2729 handle_overlay_change_p = 0;
2731 /* Handle overlay changes. */
2732 if (handle_overlay_change_p)
2733 handled = handle_overlay_change (it);
2735 /* Determine where to stop next. */
2736 if (handled == HANDLED_NORMALLY)
2737 compute_stop_pos (it);
2740 while (handled == HANDLED_RECOMPUTE_PROPS);
2744 /* Compute IT->stop_charpos from text property and overlay change
2745 information for IT's current position. */
2747 static void
2748 compute_stop_pos (it)
2749 struct it *it;
2751 register INTERVAL iv, next_iv;
2752 Lisp_Object object, limit, position;
2754 /* If nowhere else, stop at the end. */
2755 it->stop_charpos = it->end_charpos;
2757 if (STRINGP (it->string))
2759 /* Strings are usually short, so don't limit the search for
2760 properties. */
2761 object = it->string;
2762 limit = Qnil;
2763 position = make_number (IT_STRING_CHARPOS (*it));
2765 else
2767 int charpos;
2769 /* If next overlay change is in front of the current stop pos
2770 (which is IT->end_charpos), stop there. Note: value of
2771 next_overlay_change is point-max if no overlay change
2772 follows. */
2773 charpos = next_overlay_change (IT_CHARPOS (*it));
2774 if (charpos < it->stop_charpos)
2775 it->stop_charpos = charpos;
2777 /* If showing the region, we have to stop at the region
2778 start or end because the face might change there. */
2779 if (it->region_beg_charpos > 0)
2781 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2782 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2783 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2784 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2787 /* Set up variables for computing the stop position from text
2788 property changes. */
2789 XSETBUFFER (object, current_buffer);
2790 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2791 position = make_number (IT_CHARPOS (*it));
2795 /* Get the interval containing IT's position. Value is a null
2796 interval if there isn't such an interval. */
2797 iv = validate_interval_range (object, &position, &position, 0);
2798 if (!NULL_INTERVAL_P (iv))
2800 Lisp_Object values_here[LAST_PROP_IDX];
2801 struct props *p;
2803 /* Get properties here. */
2804 for (p = it_props; p->handler; ++p)
2805 values_here[p->idx] = textget (iv->plist, *p->name);
2807 /* Look for an interval following iv that has different
2808 properties. */
2809 for (next_iv = next_interval (iv);
2810 (!NULL_INTERVAL_P (next_iv)
2811 && (NILP (limit)
2812 || XFASTINT (limit) > next_iv->position));
2813 next_iv = next_interval (next_iv))
2815 for (p = it_props; p->handler; ++p)
2817 Lisp_Object new_value;
2819 new_value = textget (next_iv->plist, *p->name);
2820 if (!EQ (values_here[p->idx], new_value))
2821 break;
2824 if (p->handler)
2825 break;
2828 if (!NULL_INTERVAL_P (next_iv))
2830 if (INTEGERP (limit)
2831 && next_iv->position >= XFASTINT (limit))
2832 /* No text property change up to limit. */
2833 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2834 else
2835 /* Text properties change in next_iv. */
2836 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2840 xassert (STRINGP (it->string)
2841 || (it->stop_charpos >= BEGV
2842 && it->stop_charpos >= IT_CHARPOS (*it)));
2846 /* Return the position of the next overlay change after POS in
2847 current_buffer. Value is point-max if no overlay change
2848 follows. This is like `next-overlay-change' but doesn't use
2849 xmalloc. */
2851 static int
2852 next_overlay_change (pos)
2853 int pos;
2855 int noverlays;
2856 int endpos;
2857 Lisp_Object *overlays;
2858 int i;
2860 /* Get all overlays at the given position. */
2861 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2863 /* If any of these overlays ends before endpos,
2864 use its ending point instead. */
2865 for (i = 0; i < noverlays; ++i)
2867 Lisp_Object oend;
2868 int oendpos;
2870 oend = OVERLAY_END (overlays[i]);
2871 oendpos = OVERLAY_POSITION (oend);
2872 endpos = min (endpos, oendpos);
2875 return endpos;
2880 /***********************************************************************
2881 Fontification
2882 ***********************************************************************/
2884 /* Handle changes in the `fontified' property of the current buffer by
2885 calling hook functions from Qfontification_functions to fontify
2886 regions of text. */
2888 static enum prop_handled
2889 handle_fontified_prop (it)
2890 struct it *it;
2892 Lisp_Object prop, pos;
2893 enum prop_handled handled = HANDLED_NORMALLY;
2895 /* Get the value of the `fontified' property at IT's current buffer
2896 position. (The `fontified' property doesn't have a special
2897 meaning in strings.) If the value is nil, call functions from
2898 Qfontification_functions. */
2899 if (!STRINGP (it->string)
2900 && it->s == NULL
2901 && !NILP (Vfontification_functions)
2902 && !NILP (Vrun_hooks)
2903 && (pos = make_number (IT_CHARPOS (*it)),
2904 prop = Fget_char_property (pos, Qfontified, Qnil),
2905 NILP (prop)))
2907 int count = SPECPDL_INDEX ();
2908 Lisp_Object val;
2910 val = Vfontification_functions;
2911 specbind (Qfontification_functions, Qnil);
2913 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2914 safe_call1 (val, pos);
2915 else
2917 Lisp_Object globals, fn;
2918 struct gcpro gcpro1, gcpro2;
2920 globals = Qnil;
2921 GCPRO2 (val, globals);
2923 for (; CONSP (val); val = XCDR (val))
2925 fn = XCAR (val);
2927 if (EQ (fn, Qt))
2929 /* A value of t indicates this hook has a local
2930 binding; it means to run the global binding too.
2931 In a global value, t should not occur. If it
2932 does, we must ignore it to avoid an endless
2933 loop. */
2934 for (globals = Fdefault_value (Qfontification_functions);
2935 CONSP (globals);
2936 globals = XCDR (globals))
2938 fn = XCAR (globals);
2939 if (!EQ (fn, Qt))
2940 safe_call1 (fn, pos);
2943 else
2944 safe_call1 (fn, pos);
2947 UNGCPRO;
2950 unbind_to (count, Qnil);
2952 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2953 something. This avoids an endless loop if they failed to
2954 fontify the text for which reason ever. */
2955 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2956 handled = HANDLED_RECOMPUTE_PROPS;
2959 return handled;
2964 /***********************************************************************
2965 Faces
2966 ***********************************************************************/
2968 /* Set up iterator IT from face properties at its current position.
2969 Called from handle_stop. */
2971 static enum prop_handled
2972 handle_face_prop (it)
2973 struct it *it;
2975 int new_face_id, next_stop;
2977 if (!STRINGP (it->string))
2979 new_face_id
2980 = face_at_buffer_position (it->w,
2981 IT_CHARPOS (*it),
2982 it->region_beg_charpos,
2983 it->region_end_charpos,
2984 &next_stop,
2985 (IT_CHARPOS (*it)
2986 + TEXT_PROP_DISTANCE_LIMIT),
2989 /* Is this a start of a run of characters with box face?
2990 Caveat: this can be called for a freshly initialized
2991 iterator; face_id is -1 in this case. We know that the new
2992 face will not change until limit, i.e. if the new face has a
2993 box, all characters up to limit will have one. But, as
2994 usual, we don't know whether limit is really the end. */
2995 if (new_face_id != it->face_id)
2997 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2999 /* If new face has a box but old face has not, this is
3000 the start of a run of characters with box, i.e. it has
3001 a shadow on the left side. The value of face_id of the
3002 iterator will be -1 if this is the initial call that gets
3003 the face. In this case, we have to look in front of IT's
3004 position and see whether there is a face != new_face_id. */
3005 it->start_of_box_run_p
3006 = (new_face->box != FACE_NO_BOX
3007 && (it->face_id >= 0
3008 || IT_CHARPOS (*it) == BEG
3009 || new_face_id != face_before_it_pos (it)));
3010 it->face_box_p = new_face->box != FACE_NO_BOX;
3013 else
3015 int base_face_id, bufpos;
3017 if (it->current.overlay_string_index >= 0)
3018 bufpos = IT_CHARPOS (*it);
3019 else
3020 bufpos = 0;
3022 /* For strings from a buffer, i.e. overlay strings or strings
3023 from a `display' property, use the face at IT's current
3024 buffer position as the base face to merge with, so that
3025 overlay strings appear in the same face as surrounding
3026 text, unless they specify their own faces. */
3027 base_face_id = underlying_face_id (it);
3029 new_face_id = face_at_string_position (it->w,
3030 it->string,
3031 IT_STRING_CHARPOS (*it),
3032 bufpos,
3033 it->region_beg_charpos,
3034 it->region_end_charpos,
3035 &next_stop,
3036 base_face_id, 0);
3038 #if 0 /* This shouldn't be neccessary. Let's check it. */
3039 /* If IT is used to display a mode line we would really like to
3040 use the mode line face instead of the frame's default face. */
3041 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3042 && new_face_id == DEFAULT_FACE_ID)
3043 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3044 #endif
3046 /* Is this a start of a run of characters with box? Caveat:
3047 this can be called for a freshly allocated iterator; face_id
3048 is -1 is this case. We know that the new face will not
3049 change until the next check pos, i.e. if the new face has a
3050 box, all characters up to that position will have a
3051 box. But, as usual, we don't know whether that position
3052 is really the end. */
3053 if (new_face_id != it->face_id)
3055 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3056 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3058 /* If new face has a box but old face hasn't, this is the
3059 start of a run of characters with box, i.e. it has a
3060 shadow on the left side. */
3061 it->start_of_box_run_p
3062 = new_face->box && (old_face == NULL || !old_face->box);
3063 it->face_box_p = new_face->box != FACE_NO_BOX;
3067 it->face_id = new_face_id;
3068 return HANDLED_NORMALLY;
3072 /* Return the ID of the face ``underlying'' IT's current position,
3073 which is in a string. If the iterator is associated with a
3074 buffer, return the face at IT's current buffer position.
3075 Otherwise, use the iterator's base_face_id. */
3077 static int
3078 underlying_face_id (it)
3079 struct it *it;
3081 int face_id = it->base_face_id, i;
3083 xassert (STRINGP (it->string));
3085 for (i = it->sp - 1; i >= 0; --i)
3086 if (NILP (it->stack[i].string))
3087 face_id = it->stack[i].face_id;
3089 return face_id;
3093 /* Compute the face one character before or after the current position
3094 of IT. BEFORE_P non-zero means get the face in front of IT's
3095 position. Value is the id of the face. */
3097 static int
3098 face_before_or_after_it_pos (it, before_p)
3099 struct it *it;
3100 int before_p;
3102 int face_id, limit;
3103 int next_check_charpos;
3104 struct text_pos pos;
3106 xassert (it->s == NULL);
3108 if (STRINGP (it->string))
3110 int bufpos, base_face_id;
3112 /* No face change past the end of the string (for the case
3113 we are padding with spaces). No face change before the
3114 string start. */
3115 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3116 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3117 return it->face_id;
3119 /* Set pos to the position before or after IT's current position. */
3120 if (before_p)
3121 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3122 else
3123 /* For composition, we must check the character after the
3124 composition. */
3125 pos = (it->what == IT_COMPOSITION
3126 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3127 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3129 if (it->current.overlay_string_index >= 0)
3130 bufpos = IT_CHARPOS (*it);
3131 else
3132 bufpos = 0;
3134 base_face_id = underlying_face_id (it);
3136 /* Get the face for ASCII, or unibyte. */
3137 face_id = face_at_string_position (it->w,
3138 it->string,
3139 CHARPOS (pos),
3140 bufpos,
3141 it->region_beg_charpos,
3142 it->region_end_charpos,
3143 &next_check_charpos,
3144 base_face_id, 0);
3146 /* Correct the face for charsets different from ASCII. Do it
3147 for the multibyte case only. The face returned above is
3148 suitable for unibyte text if IT->string is unibyte. */
3149 if (STRING_MULTIBYTE (it->string))
3151 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3152 int rest = SBYTES (it->string) - BYTEPOS (pos);
3153 int c, len;
3154 struct face *face = FACE_FROM_ID (it->f, face_id);
3156 c = string_char_and_length (p, rest, &len);
3157 face_id = FACE_FOR_CHAR (it->f, face, c);
3160 else
3162 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3163 || (IT_CHARPOS (*it) <= BEGV && before_p))
3164 return it->face_id;
3166 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3167 pos = it->current.pos;
3169 if (before_p)
3170 DEC_TEXT_POS (pos, it->multibyte_p);
3171 else
3173 if (it->what == IT_COMPOSITION)
3174 /* For composition, we must check the position after the
3175 composition. */
3176 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3177 else
3178 INC_TEXT_POS (pos, it->multibyte_p);
3181 /* Determine face for CHARSET_ASCII, or unibyte. */
3182 face_id = face_at_buffer_position (it->w,
3183 CHARPOS (pos),
3184 it->region_beg_charpos,
3185 it->region_end_charpos,
3186 &next_check_charpos,
3187 limit, 0);
3189 /* Correct the face for charsets different from ASCII. Do it
3190 for the multibyte case only. The face returned above is
3191 suitable for unibyte text if current_buffer is unibyte. */
3192 if (it->multibyte_p)
3194 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3195 struct face *face = FACE_FROM_ID (it->f, face_id);
3196 face_id = FACE_FOR_CHAR (it->f, face, c);
3200 return face_id;
3205 /***********************************************************************
3206 Invisible text
3207 ***********************************************************************/
3209 /* Set up iterator IT from invisible properties at its current
3210 position. Called from handle_stop. */
3212 static enum prop_handled
3213 handle_invisible_prop (it)
3214 struct it *it;
3216 enum prop_handled handled = HANDLED_NORMALLY;
3218 if (STRINGP (it->string))
3220 extern Lisp_Object Qinvisible;
3221 Lisp_Object prop, end_charpos, limit, charpos;
3223 /* Get the value of the invisible text property at the
3224 current position. Value will be nil if there is no such
3225 property. */
3226 charpos = make_number (IT_STRING_CHARPOS (*it));
3227 prop = Fget_text_property (charpos, Qinvisible, it->string);
3229 if (!NILP (prop)
3230 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3232 handled = HANDLED_RECOMPUTE_PROPS;
3234 /* Get the position at which the next change of the
3235 invisible text property can be found in IT->string.
3236 Value will be nil if the property value is the same for
3237 all the rest of IT->string. */
3238 XSETINT (limit, SCHARS (it->string));
3239 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3240 it->string, limit);
3242 /* Text at current position is invisible. The next
3243 change in the property is at position end_charpos.
3244 Move IT's current position to that position. */
3245 if (INTEGERP (end_charpos)
3246 && XFASTINT (end_charpos) < XFASTINT (limit))
3248 struct text_pos old;
3249 old = it->current.string_pos;
3250 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3251 compute_string_pos (&it->current.string_pos, old, it->string);
3253 else
3255 /* The rest of the string is invisible. If this is an
3256 overlay string, proceed with the next overlay string
3257 or whatever comes and return a character from there. */
3258 if (it->current.overlay_string_index >= 0)
3260 next_overlay_string (it);
3261 /* Don't check for overlay strings when we just
3262 finished processing them. */
3263 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3265 else
3267 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3268 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3273 else
3275 int invis_p, newpos, next_stop, start_charpos;
3276 Lisp_Object pos, prop, overlay;
3278 /* First of all, is there invisible text at this position? */
3279 start_charpos = IT_CHARPOS (*it);
3280 pos = make_number (IT_CHARPOS (*it));
3281 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3282 &overlay);
3283 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3285 /* If we are on invisible text, skip over it. */
3286 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3288 /* Record whether we have to display an ellipsis for the
3289 invisible text. */
3290 int display_ellipsis_p = invis_p == 2;
3292 handled = HANDLED_RECOMPUTE_PROPS;
3294 /* Loop skipping over invisible text. The loop is left at
3295 ZV or with IT on the first char being visible again. */
3298 /* Try to skip some invisible text. Return value is the
3299 position reached which can be equal to IT's position
3300 if there is nothing invisible here. This skips both
3301 over invisible text properties and overlays with
3302 invisible property. */
3303 newpos = skip_invisible (IT_CHARPOS (*it),
3304 &next_stop, ZV, it->window);
3306 /* If we skipped nothing at all we weren't at invisible
3307 text in the first place. If everything to the end of
3308 the buffer was skipped, end the loop. */
3309 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3310 invis_p = 0;
3311 else
3313 /* We skipped some characters but not necessarily
3314 all there are. Check if we ended up on visible
3315 text. Fget_char_property returns the property of
3316 the char before the given position, i.e. if we
3317 get invis_p = 0, this means that the char at
3318 newpos is visible. */
3319 pos = make_number (newpos);
3320 prop = Fget_char_property (pos, Qinvisible, it->window);
3321 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3324 /* If we ended up on invisible text, proceed to
3325 skip starting with next_stop. */
3326 if (invis_p)
3327 IT_CHARPOS (*it) = next_stop;
3329 while (invis_p);
3331 /* The position newpos is now either ZV or on visible text. */
3332 IT_CHARPOS (*it) = newpos;
3333 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3335 /* If there are before-strings at the start of invisible
3336 text, and the text is invisible because of a text
3337 property, arrange to show before-strings because 20.x did
3338 it that way. (If the text is invisible because of an
3339 overlay property instead of a text property, this is
3340 already handled in the overlay code.) */
3341 if (NILP (overlay)
3342 && get_overlay_strings (it, start_charpos))
3344 handled = HANDLED_RECOMPUTE_PROPS;
3345 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3347 else if (display_ellipsis_p)
3348 setup_for_ellipsis (it, 0);
3352 return handled;
3356 /* Make iterator IT return `...' next.
3357 Replaces LEN characters from buffer. */
3359 static void
3360 setup_for_ellipsis (it, len)
3361 struct it *it;
3362 int len;
3364 /* Use the display table definition for `...'. Invalid glyphs
3365 will be handled by the method returning elements from dpvec. */
3366 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3368 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3369 it->dpvec = v->contents;
3370 it->dpend = v->contents + v->size;
3372 else
3374 /* Default `...'. */
3375 it->dpvec = default_invis_vector;
3376 it->dpend = default_invis_vector + 3;
3379 it->dpvec_char_len = len;
3380 it->current.dpvec_index = 0;
3381 it->dpvec_face_id = -1;
3383 /* Remember the current face id in case glyphs specify faces.
3384 IT's face is restored in set_iterator_to_next.
3385 saved_face_id was set to preceding char's face in handle_stop. */
3386 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3387 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3389 it->method = GET_FROM_DISPLAY_VECTOR;
3390 it->ellipsis_p = 1;
3395 /***********************************************************************
3396 'display' property
3397 ***********************************************************************/
3399 /* Set up iterator IT from `display' property at its current position.
3400 Called from handle_stop.
3401 We return HANDLED_RETURN if some part of the display property
3402 overrides the display of the buffer text itself.
3403 Otherwise we return HANDLED_NORMALLY. */
3405 static enum prop_handled
3406 handle_display_prop (it)
3407 struct it *it;
3409 Lisp_Object prop, object;
3410 struct text_pos *position;
3411 /* Nonzero if some property replaces the display of the text itself. */
3412 int display_replaced_p = 0;
3414 if (STRINGP (it->string))
3416 object = it->string;
3417 position = &it->current.string_pos;
3419 else
3421 object = it->w->buffer;
3422 position = &it->current.pos;
3425 /* Reset those iterator values set from display property values. */
3426 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3427 it->space_width = Qnil;
3428 it->font_height = Qnil;
3429 it->voffset = 0;
3431 /* We don't support recursive `display' properties, i.e. string
3432 values that have a string `display' property, that have a string
3433 `display' property etc. */
3434 if (!it->string_from_display_prop_p)
3435 it->area = TEXT_AREA;
3437 prop = Fget_char_property (make_number (position->charpos),
3438 Qdisplay, object);
3439 if (NILP (prop))
3440 return HANDLED_NORMALLY;
3442 if (CONSP (prop)
3443 /* Simple properties. */
3444 && !EQ (XCAR (prop), Qimage)
3445 && !EQ (XCAR (prop), Qspace)
3446 && !EQ (XCAR (prop), Qwhen)
3447 && !EQ (XCAR (prop), Qslice)
3448 && !EQ (XCAR (prop), Qspace_width)
3449 && !EQ (XCAR (prop), Qheight)
3450 && !EQ (XCAR (prop), Qraise)
3451 /* Marginal area specifications. */
3452 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3453 && !EQ (XCAR (prop), Qleft_fringe)
3454 && !EQ (XCAR (prop), Qright_fringe)
3455 && !NILP (XCAR (prop)))
3457 for (; CONSP (prop); prop = XCDR (prop))
3459 if (handle_single_display_spec (it, XCAR (prop), object,
3460 position, display_replaced_p))
3461 display_replaced_p = 1;
3464 else if (VECTORP (prop))
3466 int i;
3467 for (i = 0; i < ASIZE (prop); ++i)
3468 if (handle_single_display_spec (it, AREF (prop, i), object,
3469 position, display_replaced_p))
3470 display_replaced_p = 1;
3472 else
3474 if (handle_single_display_spec (it, prop, object, position, 0))
3475 display_replaced_p = 1;
3478 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3482 /* Value is the position of the end of the `display' property starting
3483 at START_POS in OBJECT. */
3485 static struct text_pos
3486 display_prop_end (it, object, start_pos)
3487 struct it *it;
3488 Lisp_Object object;
3489 struct text_pos start_pos;
3491 Lisp_Object end;
3492 struct text_pos end_pos;
3494 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3495 Qdisplay, object, Qnil);
3496 CHARPOS (end_pos) = XFASTINT (end);
3497 if (STRINGP (object))
3498 compute_string_pos (&end_pos, start_pos, it->string);
3499 else
3500 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3502 return end_pos;
3506 /* Set up IT from a single `display' specification PROP. OBJECT
3507 is the object in which the `display' property was found. *POSITION
3508 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3509 means that we previously saw a display specification which already
3510 replaced text display with something else, for example an image;
3511 we ignore such properties after the first one has been processed.
3513 If PROP is a `space' or `image' specification, and in some other
3514 cases too, set *POSITION to the position where the `display'
3515 property ends.
3517 Value is non-zero if something was found which replaces the display
3518 of buffer or string text. */
3520 static int
3521 handle_single_display_spec (it, spec, object, position,
3522 display_replaced_before_p)
3523 struct it *it;
3524 Lisp_Object spec;
3525 Lisp_Object object;
3526 struct text_pos *position;
3527 int display_replaced_before_p;
3529 Lisp_Object form;
3530 Lisp_Object location, value;
3531 struct text_pos start_pos;
3532 int valid_p;
3534 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3535 If the result is non-nil, use VALUE instead of SPEC. */
3536 form = Qt;
3537 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3539 spec = XCDR (spec);
3540 if (!CONSP (spec))
3541 return 0;
3542 form = XCAR (spec);
3543 spec = XCDR (spec);
3546 if (!NILP (form) && !EQ (form, Qt))
3548 int count = SPECPDL_INDEX ();
3549 struct gcpro gcpro1;
3551 /* Bind `object' to the object having the `display' property, a
3552 buffer or string. Bind `position' to the position in the
3553 object where the property was found, and `buffer-position'
3554 to the current position in the buffer. */
3555 specbind (Qobject, object);
3556 specbind (Qposition, make_number (CHARPOS (*position)));
3557 specbind (Qbuffer_position,
3558 make_number (STRINGP (object)
3559 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3560 GCPRO1 (form);
3561 form = safe_eval (form);
3562 UNGCPRO;
3563 unbind_to (count, Qnil);
3566 if (NILP (form))
3567 return 0;
3569 /* Handle `(height HEIGHT)' specifications. */
3570 if (CONSP (spec)
3571 && EQ (XCAR (spec), Qheight)
3572 && CONSP (XCDR (spec)))
3574 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3575 return 0;
3577 it->font_height = XCAR (XCDR (spec));
3578 if (!NILP (it->font_height))
3580 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3581 int new_height = -1;
3583 if (CONSP (it->font_height)
3584 && (EQ (XCAR (it->font_height), Qplus)
3585 || EQ (XCAR (it->font_height), Qminus))
3586 && CONSP (XCDR (it->font_height))
3587 && INTEGERP (XCAR (XCDR (it->font_height))))
3589 /* `(+ N)' or `(- N)' where N is an integer. */
3590 int steps = XINT (XCAR (XCDR (it->font_height)));
3591 if (EQ (XCAR (it->font_height), Qplus))
3592 steps = - steps;
3593 it->face_id = smaller_face (it->f, it->face_id, steps);
3595 else if (FUNCTIONP (it->font_height))
3597 /* Call function with current height as argument.
3598 Value is the new height. */
3599 Lisp_Object height;
3600 height = safe_call1 (it->font_height,
3601 face->lface[LFACE_HEIGHT_INDEX]);
3602 if (NUMBERP (height))
3603 new_height = XFLOATINT (height);
3605 else if (NUMBERP (it->font_height))
3607 /* Value is a multiple of the canonical char height. */
3608 struct face *face;
3610 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3611 new_height = (XFLOATINT (it->font_height)
3612 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3614 else
3616 /* Evaluate IT->font_height with `height' bound to the
3617 current specified height to get the new height. */
3618 int count = SPECPDL_INDEX ();
3620 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3621 value = safe_eval (it->font_height);
3622 unbind_to (count, Qnil);
3624 if (NUMBERP (value))
3625 new_height = XFLOATINT (value);
3628 if (new_height > 0)
3629 it->face_id = face_with_height (it->f, it->face_id, new_height);
3632 return 0;
3635 /* Handle `(space_width WIDTH)'. */
3636 if (CONSP (spec)
3637 && EQ (XCAR (spec), Qspace_width)
3638 && CONSP (XCDR (spec)))
3640 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3641 return 0;
3643 value = XCAR (XCDR (spec));
3644 if (NUMBERP (value) && XFLOATINT (value) > 0)
3645 it->space_width = value;
3647 return 0;
3650 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3651 if (CONSP (spec)
3652 && EQ (XCAR (spec), Qslice))
3654 Lisp_Object tem;
3656 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3657 return 0;
3659 if (tem = XCDR (spec), CONSP (tem))
3661 it->slice.x = XCAR (tem);
3662 if (tem = XCDR (tem), CONSP (tem))
3664 it->slice.y = XCAR (tem);
3665 if (tem = XCDR (tem), CONSP (tem))
3667 it->slice.width = XCAR (tem);
3668 if (tem = XCDR (tem), CONSP (tem))
3669 it->slice.height = XCAR (tem);
3674 return 0;
3677 /* Handle `(raise FACTOR)'. */
3678 if (CONSP (spec)
3679 && EQ (XCAR (spec), Qraise)
3680 && CONSP (XCDR (spec)))
3682 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3683 return 0;
3685 #ifdef HAVE_WINDOW_SYSTEM
3686 value = XCAR (XCDR (spec));
3687 if (NUMBERP (value))
3689 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3690 it->voffset = - (XFLOATINT (value)
3691 * (FONT_HEIGHT (face->font)));
3693 #endif /* HAVE_WINDOW_SYSTEM */
3695 return 0;
3698 /* Don't handle the other kinds of display specifications
3699 inside a string that we got from a `display' property. */
3700 if (it->string_from_display_prop_p)
3701 return 0;
3703 /* Characters having this form of property are not displayed, so
3704 we have to find the end of the property. */
3705 start_pos = *position;
3706 *position = display_prop_end (it, object, start_pos);
3707 value = Qnil;
3709 /* Stop the scan at that end position--we assume that all
3710 text properties change there. */
3711 it->stop_charpos = position->charpos;
3713 /* Handle `(left-fringe BITMAP [FACE])'
3714 and `(right-fringe BITMAP [FACE])'. */
3715 if (CONSP (spec)
3716 && (EQ (XCAR (spec), Qleft_fringe)
3717 || EQ (XCAR (spec), Qright_fringe))
3718 && CONSP (XCDR (spec)))
3720 int face_id = DEFAULT_FACE_ID;
3721 int fringe_bitmap;
3723 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3724 /* If we return here, POSITION has been advanced
3725 across the text with this property. */
3726 return 0;
3728 #ifdef HAVE_WINDOW_SYSTEM
3729 value = XCAR (XCDR (spec));
3730 if (!SYMBOLP (value)
3731 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3732 /* If we return here, POSITION has been advanced
3733 across the text with this property. */
3734 return 0;
3736 if (CONSP (XCDR (XCDR (spec))))
3738 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3739 int face_id2 = lookup_derived_face (it->f, face_name,
3740 'A', FRINGE_FACE_ID, 0);
3741 if (face_id2 >= 0)
3742 face_id = face_id2;
3745 /* Save current settings of IT so that we can restore them
3746 when we are finished with the glyph property value. */
3748 push_it (it);
3750 it->area = TEXT_AREA;
3751 it->what = IT_IMAGE;
3752 it->image_id = -1; /* no image */
3753 it->position = start_pos;
3754 it->object = NILP (object) ? it->w->buffer : object;
3755 it->method = GET_FROM_IMAGE;
3756 it->face_id = face_id;
3758 /* Say that we haven't consumed the characters with
3759 `display' property yet. The call to pop_it in
3760 set_iterator_to_next will clean this up. */
3761 *position = start_pos;
3763 if (EQ (XCAR (spec), Qleft_fringe))
3765 it->left_user_fringe_bitmap = fringe_bitmap;
3766 it->left_user_fringe_face_id = face_id;
3768 else
3770 it->right_user_fringe_bitmap = fringe_bitmap;
3771 it->right_user_fringe_face_id = face_id;
3773 #endif /* HAVE_WINDOW_SYSTEM */
3774 return 1;
3777 /* Prepare to handle `((margin left-margin) ...)',
3778 `((margin right-margin) ...)' and `((margin nil) ...)'
3779 prefixes for display specifications. */
3780 location = Qunbound;
3781 if (CONSP (spec) && CONSP (XCAR (spec)))
3783 Lisp_Object tem;
3785 value = XCDR (spec);
3786 if (CONSP (value))
3787 value = XCAR (value);
3789 tem = XCAR (spec);
3790 if (EQ (XCAR (tem), Qmargin)
3791 && (tem = XCDR (tem),
3792 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3793 (NILP (tem)
3794 || EQ (tem, Qleft_margin)
3795 || EQ (tem, Qright_margin))))
3796 location = tem;
3799 if (EQ (location, Qunbound))
3801 location = Qnil;
3802 value = spec;
3805 /* After this point, VALUE is the property after any
3806 margin prefix has been stripped. It must be a string,
3807 an image specification, or `(space ...)'.
3809 LOCATION specifies where to display: `left-margin',
3810 `right-margin' or nil. */
3812 valid_p = (STRINGP (value)
3813 #ifdef HAVE_WINDOW_SYSTEM
3814 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3815 #endif /* not HAVE_WINDOW_SYSTEM */
3816 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3818 if (valid_p && !display_replaced_before_p)
3820 /* Save current settings of IT so that we can restore them
3821 when we are finished with the glyph property value. */
3822 push_it (it);
3824 if (NILP (location))
3825 it->area = TEXT_AREA;
3826 else if (EQ (location, Qleft_margin))
3827 it->area = LEFT_MARGIN_AREA;
3828 else
3829 it->area = RIGHT_MARGIN_AREA;
3831 if (STRINGP (value))
3833 it->string = value;
3834 it->multibyte_p = STRING_MULTIBYTE (it->string);
3835 it->current.overlay_string_index = -1;
3836 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3837 it->end_charpos = it->string_nchars = SCHARS (it->string);
3838 it->method = GET_FROM_STRING;
3839 it->stop_charpos = 0;
3840 it->string_from_display_prop_p = 1;
3841 /* Say that we haven't consumed the characters with
3842 `display' property yet. The call to pop_it in
3843 set_iterator_to_next will clean this up. */
3844 *position = start_pos;
3846 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3848 it->method = GET_FROM_STRETCH;
3849 it->object = value;
3850 it->current.pos = it->position = start_pos;
3852 #ifdef HAVE_WINDOW_SYSTEM
3853 else
3855 it->what = IT_IMAGE;
3856 it->image_id = lookup_image (it->f, value);
3857 it->position = start_pos;
3858 it->object = NILP (object) ? it->w->buffer : object;
3859 it->method = GET_FROM_IMAGE;
3861 /* Say that we haven't consumed the characters with
3862 `display' property yet. The call to pop_it in
3863 set_iterator_to_next will clean this up. */
3864 *position = start_pos;
3866 #endif /* HAVE_WINDOW_SYSTEM */
3868 return 1;
3871 /* Invalid property or property not supported. Restore
3872 POSITION to what it was before. */
3873 *position = start_pos;
3874 return 0;
3878 /* Check if SPEC is a display specification value whose text should be
3879 treated as intangible. */
3881 static int
3882 single_display_spec_intangible_p (prop)
3883 Lisp_Object prop;
3885 /* Skip over `when FORM'. */
3886 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3888 prop = XCDR (prop);
3889 if (!CONSP (prop))
3890 return 0;
3891 prop = XCDR (prop);
3894 if (STRINGP (prop))
3895 return 1;
3897 if (!CONSP (prop))
3898 return 0;
3900 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3901 we don't need to treat text as intangible. */
3902 if (EQ (XCAR (prop), Qmargin))
3904 prop = XCDR (prop);
3905 if (!CONSP (prop))
3906 return 0;
3908 prop = XCDR (prop);
3909 if (!CONSP (prop)
3910 || EQ (XCAR (prop), Qleft_margin)
3911 || EQ (XCAR (prop), Qright_margin))
3912 return 0;
3915 return (CONSP (prop)
3916 && (EQ (XCAR (prop), Qimage)
3917 || EQ (XCAR (prop), Qspace)));
3921 /* Check if PROP is a display property value whose text should be
3922 treated as intangible. */
3925 display_prop_intangible_p (prop)
3926 Lisp_Object prop;
3928 if (CONSP (prop)
3929 && CONSP (XCAR (prop))
3930 && !EQ (Qmargin, XCAR (XCAR (prop))))
3932 /* A list of sub-properties. */
3933 while (CONSP (prop))
3935 if (single_display_spec_intangible_p (XCAR (prop)))
3936 return 1;
3937 prop = XCDR (prop);
3940 else if (VECTORP (prop))
3942 /* A vector of sub-properties. */
3943 int i;
3944 for (i = 0; i < ASIZE (prop); ++i)
3945 if (single_display_spec_intangible_p (AREF (prop, i)))
3946 return 1;
3948 else
3949 return single_display_spec_intangible_p (prop);
3951 return 0;
3955 /* Return 1 if PROP is a display sub-property value containing STRING. */
3957 static int
3958 single_display_spec_string_p (prop, string)
3959 Lisp_Object prop, string;
3961 if (EQ (string, prop))
3962 return 1;
3964 /* Skip over `when FORM'. */
3965 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3967 prop = XCDR (prop);
3968 if (!CONSP (prop))
3969 return 0;
3970 prop = XCDR (prop);
3973 if (CONSP (prop))
3974 /* Skip over `margin LOCATION'. */
3975 if (EQ (XCAR (prop), Qmargin))
3977 prop = XCDR (prop);
3978 if (!CONSP (prop))
3979 return 0;
3981 prop = XCDR (prop);
3982 if (!CONSP (prop))
3983 return 0;
3986 return CONSP (prop) && EQ (XCAR (prop), string);
3990 /* Return 1 if STRING appears in the `display' property PROP. */
3992 static int
3993 display_prop_string_p (prop, string)
3994 Lisp_Object prop, string;
3996 if (CONSP (prop)
3997 && CONSP (XCAR (prop))
3998 && !EQ (Qmargin, XCAR (XCAR (prop))))
4000 /* A list of sub-properties. */
4001 while (CONSP (prop))
4003 if (single_display_spec_string_p (XCAR (prop), string))
4004 return 1;
4005 prop = XCDR (prop);
4008 else if (VECTORP (prop))
4010 /* A vector of sub-properties. */
4011 int i;
4012 for (i = 0; i < ASIZE (prop); ++i)
4013 if (single_display_spec_string_p (AREF (prop, i), string))
4014 return 1;
4016 else
4017 return single_display_spec_string_p (prop, string);
4019 return 0;
4023 /* Determine from which buffer position in W's buffer STRING comes
4024 from. AROUND_CHARPOS is an approximate position where it could
4025 be from. Value is the buffer position or 0 if it couldn't be
4026 determined.
4028 W's buffer must be current.
4030 This function is necessary because we don't record buffer positions
4031 in glyphs generated from strings (to keep struct glyph small).
4032 This function may only use code that doesn't eval because it is
4033 called asynchronously from note_mouse_highlight. */
4036 string_buffer_position (w, string, around_charpos)
4037 struct window *w;
4038 Lisp_Object string;
4039 int around_charpos;
4041 Lisp_Object limit, prop, pos;
4042 const int MAX_DISTANCE = 1000;
4043 int found = 0;
4045 pos = make_number (around_charpos);
4046 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4047 while (!found && !EQ (pos, limit))
4049 prop = Fget_char_property (pos, Qdisplay, Qnil);
4050 if (!NILP (prop) && display_prop_string_p (prop, string))
4051 found = 1;
4052 else
4053 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4056 if (!found)
4058 pos = make_number (around_charpos);
4059 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
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 = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4067 limit);
4071 return found ? XINT (pos) : 0;
4076 /***********************************************************************
4077 `composition' property
4078 ***********************************************************************/
4080 /* Set up iterator IT from `composition' property at its current
4081 position. Called from handle_stop. */
4083 static enum prop_handled
4084 handle_composition_prop (it)
4085 struct it *it;
4087 Lisp_Object prop, string;
4088 int pos, pos_byte, end;
4089 enum prop_handled handled = HANDLED_NORMALLY;
4091 if (STRINGP (it->string))
4093 pos = IT_STRING_CHARPOS (*it);
4094 pos_byte = IT_STRING_BYTEPOS (*it);
4095 string = it->string;
4097 else
4099 pos = IT_CHARPOS (*it);
4100 pos_byte = IT_BYTEPOS (*it);
4101 string = Qnil;
4104 /* If there's a valid composition and point is not inside of the
4105 composition (in the case that the composition is from the current
4106 buffer), draw a glyph composed from the composition components. */
4107 if (find_composition (pos, -1, &pos, &end, &prop, string)
4108 && COMPOSITION_VALID_P (pos, end, prop)
4109 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4111 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4113 if (id >= 0)
4115 it->method = GET_FROM_COMPOSITION;
4116 it->cmp_id = id;
4117 it->cmp_len = COMPOSITION_LENGTH (prop);
4118 /* For a terminal, draw only the first character of the
4119 components. */
4120 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4121 it->len = (STRINGP (it->string)
4122 ? string_char_to_byte (it->string, end)
4123 : CHAR_TO_BYTE (end)) - pos_byte;
4124 it->stop_charpos = end;
4125 handled = HANDLED_RETURN;
4129 return handled;
4134 /***********************************************************************
4135 Overlay strings
4136 ***********************************************************************/
4138 /* The following structure is used to record overlay strings for
4139 later sorting in load_overlay_strings. */
4141 struct overlay_entry
4143 Lisp_Object overlay;
4144 Lisp_Object string;
4145 int priority;
4146 int after_string_p;
4150 /* Set up iterator IT from overlay strings at its current position.
4151 Called from handle_stop. */
4153 static enum prop_handled
4154 handle_overlay_change (it)
4155 struct it *it;
4157 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4158 return HANDLED_RECOMPUTE_PROPS;
4159 else
4160 return HANDLED_NORMALLY;
4164 /* Set up the next overlay string for delivery by IT, if there is an
4165 overlay string to deliver. Called by set_iterator_to_next when the
4166 end of the current overlay string is reached. If there are more
4167 overlay strings to display, IT->string and
4168 IT->current.overlay_string_index are set appropriately here.
4169 Otherwise IT->string is set to nil. */
4171 static void
4172 next_overlay_string (it)
4173 struct it *it;
4175 ++it->current.overlay_string_index;
4176 if (it->current.overlay_string_index == it->n_overlay_strings)
4178 /* No more overlay strings. Restore IT's settings to what
4179 they were before overlay strings were processed, and
4180 continue to deliver from current_buffer. */
4181 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4183 pop_it (it);
4184 xassert (it->stop_charpos >= BEGV
4185 && it->stop_charpos <= it->end_charpos);
4186 it->string = Qnil;
4187 it->current.overlay_string_index = -1;
4188 SET_TEXT_POS (it->current.string_pos, -1, -1);
4189 it->n_overlay_strings = 0;
4190 it->method = GET_FROM_BUFFER;
4192 /* If we're at the end of the buffer, record that we have
4193 processed the overlay strings there already, so that
4194 next_element_from_buffer doesn't try it again. */
4195 if (IT_CHARPOS (*it) >= it->end_charpos)
4196 it->overlay_strings_at_end_processed_p = 1;
4198 /* If we have to display `...' for invisible text, set
4199 the iterator up for that. */
4200 if (display_ellipsis_p)
4201 setup_for_ellipsis (it, 0);
4203 else
4205 /* There are more overlay strings to process. If
4206 IT->current.overlay_string_index has advanced to a position
4207 where we must load IT->overlay_strings with more strings, do
4208 it. */
4209 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4211 if (it->current.overlay_string_index && i == 0)
4212 load_overlay_strings (it, 0);
4214 /* Initialize IT to deliver display elements from the overlay
4215 string. */
4216 it->string = it->overlay_strings[i];
4217 it->multibyte_p = STRING_MULTIBYTE (it->string);
4218 SET_TEXT_POS (it->current.string_pos, 0, 0);
4219 it->method = GET_FROM_STRING;
4220 it->stop_charpos = 0;
4223 CHECK_IT (it);
4227 /* Compare two overlay_entry structures E1 and E2. Used as a
4228 comparison function for qsort in load_overlay_strings. Overlay
4229 strings for the same position are sorted so that
4231 1. All after-strings come in front of before-strings, except
4232 when they come from the same overlay.
4234 2. Within after-strings, strings are sorted so that overlay strings
4235 from overlays with higher priorities come first.
4237 2. Within before-strings, strings are sorted so that overlay
4238 strings from overlays with higher priorities come last.
4240 Value is analogous to strcmp. */
4243 static int
4244 compare_overlay_entries (e1, e2)
4245 void *e1, *e2;
4247 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4248 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4249 int result;
4251 if (entry1->after_string_p != entry2->after_string_p)
4253 /* Let after-strings appear in front of before-strings if
4254 they come from different overlays. */
4255 if (EQ (entry1->overlay, entry2->overlay))
4256 result = entry1->after_string_p ? 1 : -1;
4257 else
4258 result = entry1->after_string_p ? -1 : 1;
4260 else if (entry1->after_string_p)
4261 /* After-strings sorted in order of decreasing priority. */
4262 result = entry2->priority - entry1->priority;
4263 else
4264 /* Before-strings sorted in order of increasing priority. */
4265 result = entry1->priority - entry2->priority;
4267 return result;
4271 /* Load the vector IT->overlay_strings with overlay strings from IT's
4272 current buffer position, or from CHARPOS if that is > 0. Set
4273 IT->n_overlays to the total number of overlay strings found.
4275 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4276 a time. On entry into load_overlay_strings,
4277 IT->current.overlay_string_index gives the number of overlay
4278 strings that have already been loaded by previous calls to this
4279 function.
4281 IT->add_overlay_start contains an additional overlay start
4282 position to consider for taking overlay strings from, if non-zero.
4283 This position comes into play when the overlay has an `invisible'
4284 property, and both before and after-strings. When we've skipped to
4285 the end of the overlay, because of its `invisible' property, we
4286 nevertheless want its before-string to appear.
4287 IT->add_overlay_start will contain the overlay start position
4288 in this case.
4290 Overlay strings are sorted so that after-string strings come in
4291 front of before-string strings. Within before and after-strings,
4292 strings are sorted by overlay priority. See also function
4293 compare_overlay_entries. */
4295 static void
4296 load_overlay_strings (it, charpos)
4297 struct it *it;
4298 int charpos;
4300 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4301 Lisp_Object overlay, window, str, invisible;
4302 struct Lisp_Overlay *ov;
4303 int start, end;
4304 int size = 20;
4305 int n = 0, i, j, invis_p;
4306 struct overlay_entry *entries
4307 = (struct overlay_entry *) alloca (size * sizeof *entries);
4309 if (charpos <= 0)
4310 charpos = IT_CHARPOS (*it);
4312 /* Append the overlay string STRING of overlay OVERLAY to vector
4313 `entries' which has size `size' and currently contains `n'
4314 elements. AFTER_P non-zero means STRING is an after-string of
4315 OVERLAY. */
4316 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4317 do \
4319 Lisp_Object priority; \
4321 if (n == size) \
4323 int new_size = 2 * size; \
4324 struct overlay_entry *old = entries; \
4325 entries = \
4326 (struct overlay_entry *) alloca (new_size \
4327 * sizeof *entries); \
4328 bcopy (old, entries, size * sizeof *entries); \
4329 size = new_size; \
4332 entries[n].string = (STRING); \
4333 entries[n].overlay = (OVERLAY); \
4334 priority = Foverlay_get ((OVERLAY), Qpriority); \
4335 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4336 entries[n].after_string_p = (AFTER_P); \
4337 ++n; \
4339 while (0)
4341 /* Process overlay before the overlay center. */
4342 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4344 XSETMISC (overlay, ov);
4345 xassert (OVERLAYP (overlay));
4346 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4347 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4349 if (end < charpos)
4350 break;
4352 /* Skip this overlay if it doesn't start or end at IT's current
4353 position. */
4354 if (end != charpos && start != charpos)
4355 continue;
4357 /* Skip this overlay if it doesn't apply to IT->w. */
4358 window = Foverlay_get (overlay, Qwindow);
4359 if (WINDOWP (window) && XWINDOW (window) != it->w)
4360 continue;
4362 /* If the text ``under'' the overlay is invisible, both before-
4363 and after-strings from this overlay are visible; start and
4364 end position are indistinguishable. */
4365 invisible = Foverlay_get (overlay, Qinvisible);
4366 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4368 /* If overlay has a non-empty before-string, record it. */
4369 if ((start == charpos || (end == charpos && invis_p))
4370 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4371 && SCHARS (str))
4372 RECORD_OVERLAY_STRING (overlay, str, 0);
4374 /* If overlay has a non-empty after-string, record it. */
4375 if ((end == charpos || (start == charpos && invis_p))
4376 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4377 && SCHARS (str))
4378 RECORD_OVERLAY_STRING (overlay, str, 1);
4381 /* Process overlays after the overlay center. */
4382 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4384 XSETMISC (overlay, ov);
4385 xassert (OVERLAYP (overlay));
4386 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4387 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4389 if (start > charpos)
4390 break;
4392 /* Skip this overlay if it doesn't start or end at IT's current
4393 position. */
4394 if (end != charpos && start != charpos)
4395 continue;
4397 /* Skip this overlay if it doesn't apply to IT->w. */
4398 window = Foverlay_get (overlay, Qwindow);
4399 if (WINDOWP (window) && XWINDOW (window) != it->w)
4400 continue;
4402 /* If the text ``under'' the overlay is invisible, it has a zero
4403 dimension, and both before- and after-strings apply. */
4404 invisible = Foverlay_get (overlay, Qinvisible);
4405 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4407 /* If overlay has a non-empty before-string, record it. */
4408 if ((start == charpos || (end == charpos && invis_p))
4409 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4410 && SCHARS (str))
4411 RECORD_OVERLAY_STRING (overlay, str, 0);
4413 /* If overlay has a non-empty after-string, record it. */
4414 if ((end == charpos || (start == charpos && invis_p))
4415 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4416 && SCHARS (str))
4417 RECORD_OVERLAY_STRING (overlay, str, 1);
4420 #undef RECORD_OVERLAY_STRING
4422 /* Sort entries. */
4423 if (n > 1)
4424 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4426 /* Record the total number of strings to process. */
4427 it->n_overlay_strings = n;
4429 /* IT->current.overlay_string_index is the number of overlay strings
4430 that have already been consumed by IT. Copy some of the
4431 remaining overlay strings to IT->overlay_strings. */
4432 i = 0;
4433 j = it->current.overlay_string_index;
4434 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4435 it->overlay_strings[i++] = entries[j++].string;
4437 CHECK_IT (it);
4441 /* Get the first chunk of overlay strings at IT's current buffer
4442 position, or at CHARPOS if that is > 0. Value is non-zero if at
4443 least one overlay string was found. */
4445 static int
4446 get_overlay_strings (it, charpos)
4447 struct it *it;
4448 int charpos;
4450 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4451 process. This fills IT->overlay_strings with strings, and sets
4452 IT->n_overlay_strings to the total number of strings to process.
4453 IT->pos.overlay_string_index has to be set temporarily to zero
4454 because load_overlay_strings needs this; it must be set to -1
4455 when no overlay strings are found because a zero value would
4456 indicate a position in the first overlay string. */
4457 it->current.overlay_string_index = 0;
4458 load_overlay_strings (it, charpos);
4460 /* If we found overlay strings, set up IT to deliver display
4461 elements from the first one. Otherwise set up IT to deliver
4462 from current_buffer. */
4463 if (it->n_overlay_strings)
4465 /* Make sure we know settings in current_buffer, so that we can
4466 restore meaningful values when we're done with the overlay
4467 strings. */
4468 compute_stop_pos (it);
4469 xassert (it->face_id >= 0);
4471 /* Save IT's settings. They are restored after all overlay
4472 strings have been processed. */
4473 xassert (it->sp == 0);
4474 push_it (it);
4476 /* Set up IT to deliver display elements from the first overlay
4477 string. */
4478 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4479 it->string = it->overlay_strings[0];
4480 it->stop_charpos = 0;
4481 xassert (STRINGP (it->string));
4482 it->end_charpos = SCHARS (it->string);
4483 it->multibyte_p = STRING_MULTIBYTE (it->string);
4484 it->method = GET_FROM_STRING;
4486 else
4488 it->string = Qnil;
4489 it->current.overlay_string_index = -1;
4490 it->method = GET_FROM_BUFFER;
4493 CHECK_IT (it);
4495 /* Value is non-zero if we found at least one overlay string. */
4496 return STRINGP (it->string);
4501 /***********************************************************************
4502 Saving and restoring state
4503 ***********************************************************************/
4505 /* Save current settings of IT on IT->stack. Called, for example,
4506 before setting up IT for an overlay string, to be able to restore
4507 IT's settings to what they were after the overlay string has been
4508 processed. */
4510 static void
4511 push_it (it)
4512 struct it *it;
4514 struct iterator_stack_entry *p;
4516 xassert (it->sp < 2);
4517 p = it->stack + it->sp;
4519 p->stop_charpos = it->stop_charpos;
4520 xassert (it->face_id >= 0);
4521 p->face_id = it->face_id;
4522 p->string = it->string;
4523 p->pos = it->current;
4524 p->end_charpos = it->end_charpos;
4525 p->string_nchars = it->string_nchars;
4526 p->area = it->area;
4527 p->multibyte_p = it->multibyte_p;
4528 p->slice = it->slice;
4529 p->space_width = it->space_width;
4530 p->font_height = it->font_height;
4531 p->voffset = it->voffset;
4532 p->string_from_display_prop_p = it->string_from_display_prop_p;
4533 p->display_ellipsis_p = 0;
4534 ++it->sp;
4538 /* Restore IT's settings from IT->stack. Called, for example, when no
4539 more overlay strings must be processed, and we return to delivering
4540 display elements from a buffer, or when the end of a string from a
4541 `display' property is reached and we return to delivering display
4542 elements from an overlay string, or from a buffer. */
4544 static void
4545 pop_it (it)
4546 struct it *it;
4548 struct iterator_stack_entry *p;
4550 xassert (it->sp > 0);
4551 --it->sp;
4552 p = it->stack + it->sp;
4553 it->stop_charpos = p->stop_charpos;
4554 it->face_id = p->face_id;
4555 it->string = p->string;
4556 it->current = p->pos;
4557 it->end_charpos = p->end_charpos;
4558 it->string_nchars = p->string_nchars;
4559 it->area = p->area;
4560 it->multibyte_p = p->multibyte_p;
4561 it->slice = p->slice;
4562 it->space_width = p->space_width;
4563 it->font_height = p->font_height;
4564 it->voffset = p->voffset;
4565 it->string_from_display_prop_p = p->string_from_display_prop_p;
4570 /***********************************************************************
4571 Moving over lines
4572 ***********************************************************************/
4574 /* Set IT's current position to the previous line start. */
4576 static void
4577 back_to_previous_line_start (it)
4578 struct it *it;
4580 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4581 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4585 /* Move IT to the next line start.
4587 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4588 we skipped over part of the text (as opposed to moving the iterator
4589 continuously over the text). Otherwise, don't change the value
4590 of *SKIPPED_P.
4592 Newlines may come from buffer text, overlay strings, or strings
4593 displayed via the `display' property. That's the reason we can't
4594 simply use find_next_newline_no_quit.
4596 Note that this function may not skip over invisible text that is so
4597 because of text properties and immediately follows a newline. If
4598 it would, function reseat_at_next_visible_line_start, when called
4599 from set_iterator_to_next, would effectively make invisible
4600 characters following a newline part of the wrong glyph row, which
4601 leads to wrong cursor motion. */
4603 static int
4604 forward_to_next_line_start (it, skipped_p)
4605 struct it *it;
4606 int *skipped_p;
4608 int old_selective, newline_found_p, n;
4609 const int MAX_NEWLINE_DISTANCE = 500;
4611 /* If already on a newline, just consume it to avoid unintended
4612 skipping over invisible text below. */
4613 if (it->what == IT_CHARACTER
4614 && it->c == '\n'
4615 && CHARPOS (it->position) == IT_CHARPOS (*it))
4617 set_iterator_to_next (it, 0);
4618 it->c = 0;
4619 return 1;
4622 /* Don't handle selective display in the following. It's (a)
4623 unnecessary because it's done by the caller, and (b) leads to an
4624 infinite recursion because next_element_from_ellipsis indirectly
4625 calls this function. */
4626 old_selective = it->selective;
4627 it->selective = 0;
4629 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4630 from buffer text. */
4631 for (n = newline_found_p = 0;
4632 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4633 n += STRINGP (it->string) ? 0 : 1)
4635 if (!get_next_display_element (it))
4636 return 0;
4637 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4638 set_iterator_to_next (it, 0);
4641 /* If we didn't find a newline near enough, see if we can use a
4642 short-cut. */
4643 if (!newline_found_p)
4645 int start = IT_CHARPOS (*it);
4646 int limit = find_next_newline_no_quit (start, 1);
4647 Lisp_Object pos;
4649 xassert (!STRINGP (it->string));
4651 /* If there isn't any `display' property in sight, and no
4652 overlays, we can just use the position of the newline in
4653 buffer text. */
4654 if (it->stop_charpos >= limit
4655 || ((pos = Fnext_single_property_change (make_number (start),
4656 Qdisplay,
4657 Qnil, make_number (limit)),
4658 NILP (pos))
4659 && next_overlay_change (start) == ZV))
4661 IT_CHARPOS (*it) = limit;
4662 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4663 *skipped_p = newline_found_p = 1;
4665 else
4667 while (get_next_display_element (it)
4668 && !newline_found_p)
4670 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4671 set_iterator_to_next (it, 0);
4676 it->selective = old_selective;
4677 return newline_found_p;
4681 /* Set IT's current position to the previous visible line start. Skip
4682 invisible text that is so either due to text properties or due to
4683 selective display. Caution: this does not change IT->current_x and
4684 IT->hpos. */
4686 static void
4687 back_to_previous_visible_line_start (it)
4688 struct it *it;
4690 while (IT_CHARPOS (*it) > BEGV)
4692 back_to_previous_line_start (it);
4693 if (IT_CHARPOS (*it) <= BEGV)
4694 break;
4696 /* If selective > 0, then lines indented more than that values
4697 are invisible. */
4698 if (it->selective > 0
4699 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4700 (double) it->selective)) /* iftc */
4701 continue;
4703 /* Check the newline before point for invisibility. */
4705 Lisp_Object prop;
4706 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4707 Qinvisible, it->window);
4708 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4709 continue;
4712 /* If newline has a display property that replaces the newline with something
4713 else (image or text), find start of overlay or interval and continue search
4714 from that point. */
4715 if (IT_CHARPOS (*it) > BEGV)
4717 struct it it2 = *it;
4718 int pos;
4719 int beg, end;
4720 Lisp_Object val, overlay;
4722 pos = --IT_CHARPOS (it2);
4723 --IT_BYTEPOS (it2);
4724 it2.sp = 0;
4725 if (handle_display_prop (&it2) == HANDLED_RETURN
4726 && !NILP (val = get_char_property_and_overlay
4727 (make_number (pos), Qdisplay, Qnil, &overlay))
4728 && (OVERLAYP (overlay)
4729 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4730 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4732 if (beg < BEGV)
4733 beg = BEGV;
4734 IT_CHARPOS (*it) = beg;
4735 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4736 continue;
4740 break;
4743 xassert (IT_CHARPOS (*it) >= BEGV);
4744 xassert (IT_CHARPOS (*it) == BEGV
4745 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4746 CHECK_IT (it);
4750 /* Reseat iterator IT at the previous visible line start. Skip
4751 invisible text that is so either due to text properties or due to
4752 selective display. At the end, update IT's overlay information,
4753 face information etc. */
4755 void
4756 reseat_at_previous_visible_line_start (it)
4757 struct it *it;
4759 back_to_previous_visible_line_start (it);
4760 reseat (it, it->current.pos, 1);
4761 CHECK_IT (it);
4765 /* Reseat iterator IT on the next visible line start in the current
4766 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4767 preceding the line start. Skip over invisible text that is so
4768 because of selective display. Compute faces, overlays etc at the
4769 new position. Note that this function does not skip over text that
4770 is invisible because of text properties. */
4772 static void
4773 reseat_at_next_visible_line_start (it, on_newline_p)
4774 struct it *it;
4775 int on_newline_p;
4777 int newline_found_p, skipped_p = 0;
4779 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4781 /* Skip over lines that are invisible because they are indented
4782 more than the value of IT->selective. */
4783 if (it->selective > 0)
4784 while (IT_CHARPOS (*it) < ZV
4785 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4786 (double) it->selective)) /* iftc */
4788 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4789 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4792 /* Position on the newline if that's what's requested. */
4793 if (on_newline_p && newline_found_p)
4795 if (STRINGP (it->string))
4797 if (IT_STRING_CHARPOS (*it) > 0)
4799 --IT_STRING_CHARPOS (*it);
4800 --IT_STRING_BYTEPOS (*it);
4803 else if (IT_CHARPOS (*it) > BEGV)
4805 --IT_CHARPOS (*it);
4806 --IT_BYTEPOS (*it);
4807 reseat (it, it->current.pos, 0);
4810 else if (skipped_p)
4811 reseat (it, it->current.pos, 0);
4813 CHECK_IT (it);
4818 /***********************************************************************
4819 Changing an iterator's position
4820 ***********************************************************************/
4822 /* Change IT's current position to POS in current_buffer. If FORCE_P
4823 is non-zero, always check for text properties at the new position.
4824 Otherwise, text properties are only looked up if POS >=
4825 IT->check_charpos of a property. */
4827 static void
4828 reseat (it, pos, force_p)
4829 struct it *it;
4830 struct text_pos pos;
4831 int force_p;
4833 int original_pos = IT_CHARPOS (*it);
4835 reseat_1 (it, pos, 0);
4837 /* Determine where to check text properties. Avoid doing it
4838 where possible because text property lookup is very expensive. */
4839 if (force_p
4840 || CHARPOS (pos) > it->stop_charpos
4841 || CHARPOS (pos) < original_pos)
4842 handle_stop (it);
4844 CHECK_IT (it);
4848 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4849 IT->stop_pos to POS, also. */
4851 static void
4852 reseat_1 (it, pos, set_stop_p)
4853 struct it *it;
4854 struct text_pos pos;
4855 int set_stop_p;
4857 /* Don't call this function when scanning a C string. */
4858 xassert (it->s == NULL);
4860 /* POS must be a reasonable value. */
4861 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4863 it->current.pos = it->position = pos;
4864 XSETBUFFER (it->object, current_buffer);
4865 it->end_charpos = ZV;
4866 it->dpvec = NULL;
4867 it->current.dpvec_index = -1;
4868 it->current.overlay_string_index = -1;
4869 IT_STRING_CHARPOS (*it) = -1;
4870 IT_STRING_BYTEPOS (*it) = -1;
4871 it->string = Qnil;
4872 it->method = GET_FROM_BUFFER;
4873 /* RMS: I added this to fix a bug in move_it_vertically_backward
4874 where it->area continued to relate to the starting point
4875 for the backward motion. Bug report from
4876 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4877 However, I am not sure whether reseat still does the right thing
4878 in general after this change. */
4879 it->area = TEXT_AREA;
4880 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4881 it->sp = 0;
4882 it->face_before_selective_p = 0;
4884 if (set_stop_p)
4885 it->stop_charpos = CHARPOS (pos);
4889 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4890 If S is non-null, it is a C string to iterate over. Otherwise,
4891 STRING gives a Lisp string to iterate over.
4893 If PRECISION > 0, don't return more then PRECISION number of
4894 characters from the string.
4896 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4897 characters have been returned. FIELD_WIDTH < 0 means an infinite
4898 field width.
4900 MULTIBYTE = 0 means disable processing of multibyte characters,
4901 MULTIBYTE > 0 means enable it,
4902 MULTIBYTE < 0 means use IT->multibyte_p.
4904 IT must be initialized via a prior call to init_iterator before
4905 calling this function. */
4907 static void
4908 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4909 struct it *it;
4910 unsigned char *s;
4911 Lisp_Object string;
4912 int charpos;
4913 int precision, field_width, multibyte;
4915 /* No region in strings. */
4916 it->region_beg_charpos = it->region_end_charpos = -1;
4918 /* No text property checks performed by default, but see below. */
4919 it->stop_charpos = -1;
4921 /* Set iterator position and end position. */
4922 bzero (&it->current, sizeof it->current);
4923 it->current.overlay_string_index = -1;
4924 it->current.dpvec_index = -1;
4925 xassert (charpos >= 0);
4927 /* If STRING is specified, use its multibyteness, otherwise use the
4928 setting of MULTIBYTE, if specified. */
4929 if (multibyte >= 0)
4930 it->multibyte_p = multibyte > 0;
4932 if (s == NULL)
4934 xassert (STRINGP (string));
4935 it->string = string;
4936 it->s = NULL;
4937 it->end_charpos = it->string_nchars = SCHARS (string);
4938 it->method = GET_FROM_STRING;
4939 it->current.string_pos = string_pos (charpos, string);
4941 else
4943 it->s = s;
4944 it->string = Qnil;
4946 /* Note that we use IT->current.pos, not it->current.string_pos,
4947 for displaying C strings. */
4948 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4949 if (it->multibyte_p)
4951 it->current.pos = c_string_pos (charpos, s, 1);
4952 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4954 else
4956 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4957 it->end_charpos = it->string_nchars = strlen (s);
4960 it->method = GET_FROM_C_STRING;
4963 /* PRECISION > 0 means don't return more than PRECISION characters
4964 from the string. */
4965 if (precision > 0 && it->end_charpos - charpos > precision)
4966 it->end_charpos = it->string_nchars = charpos + precision;
4968 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4969 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4970 FIELD_WIDTH < 0 means infinite field width. This is useful for
4971 padding with `-' at the end of a mode line. */
4972 if (field_width < 0)
4973 field_width = INFINITY;
4974 if (field_width > it->end_charpos - charpos)
4975 it->end_charpos = charpos + field_width;
4977 /* Use the standard display table for displaying strings. */
4978 if (DISP_TABLE_P (Vstandard_display_table))
4979 it->dp = XCHAR_TABLE (Vstandard_display_table);
4981 it->stop_charpos = charpos;
4982 CHECK_IT (it);
4987 /***********************************************************************
4988 Iteration
4989 ***********************************************************************/
4991 /* Map enum it_method value to corresponding next_element_from_* function. */
4993 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
4995 next_element_from_buffer,
4996 next_element_from_display_vector,
4997 next_element_from_composition,
4998 next_element_from_string,
4999 next_element_from_c_string,
5000 next_element_from_image,
5001 next_element_from_stretch
5005 /* Load IT's display element fields with information about the next
5006 display element from the current position of IT. Value is zero if
5007 end of buffer (or C string) is reached. */
5010 get_next_display_element (it)
5011 struct it *it;
5013 /* Non-zero means that we found a display element. Zero means that
5014 we hit the end of what we iterate over. Performance note: the
5015 function pointer `method' used here turns out to be faster than
5016 using a sequence of if-statements. */
5017 int success_p;
5019 get_next:
5020 success_p = (*get_next_element[it->method]) (it);
5022 if (it->what == IT_CHARACTER)
5024 /* Map via display table or translate control characters.
5025 IT->c, IT->len etc. have been set to the next character by
5026 the function call above. If we have a display table, and it
5027 contains an entry for IT->c, translate it. Don't do this if
5028 IT->c itself comes from a display table, otherwise we could
5029 end up in an infinite recursion. (An alternative could be to
5030 count the recursion depth of this function and signal an
5031 error when a certain maximum depth is reached.) Is it worth
5032 it? */
5033 if (success_p && it->dpvec == NULL)
5035 Lisp_Object dv;
5037 if (it->dp
5038 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5039 VECTORP (dv)))
5041 struct Lisp_Vector *v = XVECTOR (dv);
5043 /* Return the first character from the display table
5044 entry, if not empty. If empty, don't display the
5045 current character. */
5046 if (v->size)
5048 it->dpvec_char_len = it->len;
5049 it->dpvec = v->contents;
5050 it->dpend = v->contents + v->size;
5051 it->current.dpvec_index = 0;
5052 it->dpvec_face_id = -1;
5053 it->saved_face_id = it->face_id;
5054 it->method = GET_FROM_DISPLAY_VECTOR;
5055 it->ellipsis_p = 0;
5057 else
5059 set_iterator_to_next (it, 0);
5061 goto get_next;
5064 /* Translate control characters into `\003' or `^C' form.
5065 Control characters coming from a display table entry are
5066 currently not translated because we use IT->dpvec to hold
5067 the translation. This could easily be changed but I
5068 don't believe that it is worth doing.
5070 If it->multibyte_p is nonzero, eight-bit characters and
5071 non-printable multibyte characters are also translated to
5072 octal form.
5074 If it->multibyte_p is zero, eight-bit characters that
5075 don't have corresponding multibyte char code are also
5076 translated to octal form. */
5077 else if ((it->c < ' '
5078 && (it->area != TEXT_AREA
5079 /* In mode line, treat \n like other crl chars. */
5080 || (it->c != '\t'
5081 && it->glyph_row && it->glyph_row->mode_line_p)
5082 || (it->c != '\n' && it->c != '\t')))
5083 || (it->multibyte_p
5084 ? ((it->c >= 127
5085 && it->len == 1)
5086 || !CHAR_PRINTABLE_P (it->c)
5087 || (!NILP (Vshow_nonbreak_escape)
5088 && (it->c == 0x8ad || it->c == 0x8a0
5089 || it->c == 0xf2d || it->c == 0xf20)))
5090 : (it->c >= 127
5091 && (!unibyte_display_via_language_environment
5092 || it->c == unibyte_char_to_multibyte (it->c)))))
5094 /* IT->c is a control character which must be displayed
5095 either as '\003' or as `^C' where the '\\' and '^'
5096 can be defined in the display table. Fill
5097 IT->ctl_chars with glyphs for what we have to
5098 display. Then, set IT->dpvec to these glyphs. */
5099 GLYPH g;
5100 int ctl_len;
5101 int face_id, lface_id = 0 ;
5102 GLYPH escape_glyph;
5104 if (it->c < 128 && it->ctl_arrow_p)
5106 g = '^'; /* default glyph for Control */
5107 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5108 if (it->dp
5109 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5110 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5112 g = XINT (DISP_CTRL_GLYPH (it->dp));
5113 lface_id = FAST_GLYPH_FACE (g);
5115 if (lface_id)
5117 g = FAST_GLYPH_CHAR (g);
5118 face_id = merge_faces (it->f, Qt, lface_id,
5119 it->face_id);
5121 else
5123 /* Merge the escape-glyph face into the current face. */
5124 face_id = merge_faces (it->f, Qescape_glyph, 0,
5125 it->face_id);
5128 XSETINT (it->ctl_chars[0], g);
5129 g = it->c ^ 0100;
5130 XSETINT (it->ctl_chars[1], g);
5131 ctl_len = 2;
5132 goto display_control;
5135 escape_glyph = '\\'; /* default for Octal display */
5136 if (it->dp
5137 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5138 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5140 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5141 lface_id = FAST_GLYPH_FACE (escape_glyph);
5143 if (lface_id)
5145 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5146 face_id = merge_faces (it->f, Qt, lface_id,
5147 it->face_id);
5149 else
5151 /* Merge the escape-glyph face into the current face. */
5152 face_id = merge_faces (it->f, Qescape_glyph, 0,
5153 it->face_id);
5156 if (it->c == 0x8a0 || it->c == 0x8ad
5157 || it->c == 0xf20 || it->c == 0xf2d)
5159 XSETINT (it->ctl_chars[0], escape_glyph);
5160 g = it->c;
5161 XSETINT (it->ctl_chars[1], g);
5162 ctl_len = 2;
5163 goto display_control;
5167 unsigned char str[MAX_MULTIBYTE_LENGTH];
5168 int len;
5169 int i;
5171 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5172 if (SINGLE_BYTE_CHAR_P (it->c))
5173 str[0] = it->c, len = 1;
5174 else
5176 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5177 if (len < 0)
5179 /* It's an invalid character, which shouldn't
5180 happen actually, but due to bugs it may
5181 happen. Let's print the char as is, there's
5182 not much meaningful we can do with it. */
5183 str[0] = it->c;
5184 str[1] = it->c >> 8;
5185 str[2] = it->c >> 16;
5186 str[3] = it->c >> 24;
5187 len = 4;
5191 for (i = 0; i < len; i++)
5193 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5194 /* Insert three more glyphs into IT->ctl_chars for
5195 the octal display of the character. */
5196 g = ((str[i] >> 6) & 7) + '0';
5197 XSETINT (it->ctl_chars[i * 4 + 1], g);
5198 g = ((str[i] >> 3) & 7) + '0';
5199 XSETINT (it->ctl_chars[i * 4 + 2], g);
5200 g = (str[i] & 7) + '0';
5201 XSETINT (it->ctl_chars[i * 4 + 3], g);
5203 ctl_len = len * 4;
5206 display_control:
5207 /* Set up IT->dpvec and return first character from it. */
5208 it->dpvec_char_len = it->len;
5209 it->dpvec = it->ctl_chars;
5210 it->dpend = it->dpvec + ctl_len;
5211 it->current.dpvec_index = 0;
5212 it->dpvec_face_id = face_id;
5213 it->saved_face_id = it->face_id;
5214 it->method = GET_FROM_DISPLAY_VECTOR;
5215 it->ellipsis_p = 0;
5216 goto get_next;
5220 /* Adjust face id for a multibyte character. There are no
5221 multibyte character in unibyte text. */
5222 if (it->multibyte_p
5223 && success_p
5224 && FRAME_WINDOW_P (it->f))
5226 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5227 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5231 /* Is this character the last one of a run of characters with
5232 box? If yes, set IT->end_of_box_run_p to 1. */
5233 if (it->face_box_p
5234 && it->s == NULL)
5236 int face_id;
5237 struct face *face;
5239 it->end_of_box_run_p
5240 = ((face_id = face_after_it_pos (it),
5241 face_id != it->face_id)
5242 && (face = FACE_FROM_ID (it->f, face_id),
5243 face->box == FACE_NO_BOX));
5246 /* Value is 0 if end of buffer or string reached. */
5247 return success_p;
5251 /* Move IT to the next display element.
5253 RESEAT_P non-zero means if called on a newline in buffer text,
5254 skip to the next visible line start.
5256 Functions get_next_display_element and set_iterator_to_next are
5257 separate because I find this arrangement easier to handle than a
5258 get_next_display_element function that also increments IT's
5259 position. The way it is we can first look at an iterator's current
5260 display element, decide whether it fits on a line, and if it does,
5261 increment the iterator position. The other way around we probably
5262 would either need a flag indicating whether the iterator has to be
5263 incremented the next time, or we would have to implement a
5264 decrement position function which would not be easy to write. */
5266 void
5267 set_iterator_to_next (it, reseat_p)
5268 struct it *it;
5269 int reseat_p;
5271 /* Reset flags indicating start and end of a sequence of characters
5272 with box. Reset them at the start of this function because
5273 moving the iterator to a new position might set them. */
5274 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5276 switch (it->method)
5278 case GET_FROM_BUFFER:
5279 /* The current display element of IT is a character from
5280 current_buffer. Advance in the buffer, and maybe skip over
5281 invisible lines that are so because of selective display. */
5282 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5283 reseat_at_next_visible_line_start (it, 0);
5284 else
5286 xassert (it->len != 0);
5287 IT_BYTEPOS (*it) += it->len;
5288 IT_CHARPOS (*it) += 1;
5289 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5291 break;
5293 case GET_FROM_COMPOSITION:
5294 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5295 if (STRINGP (it->string))
5297 IT_STRING_BYTEPOS (*it) += it->len;
5298 IT_STRING_CHARPOS (*it) += it->cmp_len;
5299 it->method = GET_FROM_STRING;
5300 goto consider_string_end;
5302 else
5304 IT_BYTEPOS (*it) += it->len;
5305 IT_CHARPOS (*it) += it->cmp_len;
5306 it->method = GET_FROM_BUFFER;
5308 break;
5310 case GET_FROM_C_STRING:
5311 /* Current display element of IT is from a C string. */
5312 IT_BYTEPOS (*it) += it->len;
5313 IT_CHARPOS (*it) += 1;
5314 break;
5316 case GET_FROM_DISPLAY_VECTOR:
5317 /* Current display element of IT is from a display table entry.
5318 Advance in the display table definition. Reset it to null if
5319 end reached, and continue with characters from buffers/
5320 strings. */
5321 ++it->current.dpvec_index;
5323 /* Restore face of the iterator to what they were before the
5324 display vector entry (these entries may contain faces). */
5325 it->face_id = it->saved_face_id;
5327 if (it->dpvec + it->current.dpvec_index == it->dpend)
5329 if (it->s)
5330 it->method = GET_FROM_C_STRING;
5331 else if (STRINGP (it->string))
5332 it->method = GET_FROM_STRING;
5333 else
5334 it->method = GET_FROM_BUFFER;
5336 it->dpvec = NULL;
5337 it->current.dpvec_index = -1;
5339 /* Skip over characters which were displayed via IT->dpvec. */
5340 if (it->dpvec_char_len < 0)
5341 reseat_at_next_visible_line_start (it, 1);
5342 else if (it->dpvec_char_len > 0)
5344 it->len = it->dpvec_char_len;
5345 set_iterator_to_next (it, reseat_p);
5348 /* Recheck faces after display vector */
5349 it->stop_charpos = IT_CHARPOS (*it);
5351 break;
5353 case GET_FROM_STRING:
5354 /* Current display element is a character from a Lisp string. */
5355 xassert (it->s == NULL && STRINGP (it->string));
5356 IT_STRING_BYTEPOS (*it) += it->len;
5357 IT_STRING_CHARPOS (*it) += 1;
5359 consider_string_end:
5361 if (it->current.overlay_string_index >= 0)
5363 /* IT->string is an overlay string. Advance to the
5364 next, if there is one. */
5365 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5366 next_overlay_string (it);
5368 else
5370 /* IT->string is not an overlay string. If we reached
5371 its end, and there is something on IT->stack, proceed
5372 with what is on the stack. This can be either another
5373 string, this time an overlay string, or a buffer. */
5374 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5375 && it->sp > 0)
5377 pop_it (it);
5378 if (STRINGP (it->string))
5379 goto consider_string_end;
5380 it->method = GET_FROM_BUFFER;
5383 break;
5385 case GET_FROM_IMAGE:
5386 case GET_FROM_STRETCH:
5387 /* The position etc with which we have to proceed are on
5388 the stack. The position may be at the end of a string,
5389 if the `display' property takes up the whole string. */
5390 xassert (it->sp > 0);
5391 pop_it (it);
5392 it->image_id = 0;
5393 if (STRINGP (it->string))
5395 it->method = GET_FROM_STRING;
5396 goto consider_string_end;
5398 it->method = GET_FROM_BUFFER;
5399 break;
5401 default:
5402 /* There are no other methods defined, so this should be a bug. */
5403 abort ();
5406 xassert (it->method != GET_FROM_STRING
5407 || (STRINGP (it->string)
5408 && IT_STRING_CHARPOS (*it) >= 0));
5411 /* Load IT's display element fields with information about the next
5412 display element which comes from a display table entry or from the
5413 result of translating a control character to one of the forms `^C'
5414 or `\003'.
5416 IT->dpvec holds the glyphs to return as characters.
5417 IT->saved_face_id holds the face id before the display vector--
5418 it is restored into IT->face_idin set_iterator_to_next. */
5420 static int
5421 next_element_from_display_vector (it)
5422 struct it *it;
5424 /* Precondition. */
5425 xassert (it->dpvec && it->current.dpvec_index >= 0);
5427 it->face_id = it->saved_face_id;
5429 if (INTEGERP (*it->dpvec)
5430 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5432 GLYPH g;
5434 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5435 it->c = FAST_GLYPH_CHAR (g);
5436 it->len = CHAR_BYTES (it->c);
5438 /* The entry may contain a face id to use. Such a face id is
5439 the id of a Lisp face, not a realized face. A face id of
5440 zero means no face is specified. */
5441 if (it->dpvec_face_id >= 0)
5442 it->face_id = it->dpvec_face_id;
5443 else
5445 int lface_id = FAST_GLYPH_FACE (g);
5446 if (lface_id > 0)
5447 it->face_id = merge_faces (it->f, Qt, lface_id,
5448 it->saved_face_id);
5451 else
5452 /* Display table entry is invalid. Return a space. */
5453 it->c = ' ', it->len = 1;
5455 /* Don't change position and object of the iterator here. They are
5456 still the values of the character that had this display table
5457 entry or was translated, and that's what we want. */
5458 it->what = IT_CHARACTER;
5459 return 1;
5463 /* Load IT with the next display element from Lisp string IT->string.
5464 IT->current.string_pos is the current position within the string.
5465 If IT->current.overlay_string_index >= 0, the Lisp string is an
5466 overlay string. */
5468 static int
5469 next_element_from_string (it)
5470 struct it *it;
5472 struct text_pos position;
5474 xassert (STRINGP (it->string));
5475 xassert (IT_STRING_CHARPOS (*it) >= 0);
5476 position = it->current.string_pos;
5478 /* Time to check for invisible text? */
5479 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5480 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5482 handle_stop (it);
5484 /* Since a handler may have changed IT->method, we must
5485 recurse here. */
5486 return get_next_display_element (it);
5489 if (it->current.overlay_string_index >= 0)
5491 /* Get the next character from an overlay string. In overlay
5492 strings, There is no field width or padding with spaces to
5493 do. */
5494 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5496 it->what = IT_EOB;
5497 return 0;
5499 else if (STRING_MULTIBYTE (it->string))
5501 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5502 const unsigned char *s = (SDATA (it->string)
5503 + IT_STRING_BYTEPOS (*it));
5504 it->c = string_char_and_length (s, remaining, &it->len);
5506 else
5508 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5509 it->len = 1;
5512 else
5514 /* Get the next character from a Lisp string that is not an
5515 overlay string. Such strings come from the mode line, for
5516 example. We may have to pad with spaces, or truncate the
5517 string. See also next_element_from_c_string. */
5518 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5520 it->what = IT_EOB;
5521 return 0;
5523 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5525 /* Pad with spaces. */
5526 it->c = ' ', it->len = 1;
5527 CHARPOS (position) = BYTEPOS (position) = -1;
5529 else if (STRING_MULTIBYTE (it->string))
5531 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5532 const unsigned char *s = (SDATA (it->string)
5533 + IT_STRING_BYTEPOS (*it));
5534 it->c = string_char_and_length (s, maxlen, &it->len);
5536 else
5538 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5539 it->len = 1;
5543 /* Record what we have and where it came from. Note that we store a
5544 buffer position in IT->position although it could arguably be a
5545 string position. */
5546 it->what = IT_CHARACTER;
5547 it->object = it->string;
5548 it->position = position;
5549 return 1;
5553 /* Load IT with next display element from C string IT->s.
5554 IT->string_nchars is the maximum number of characters to return
5555 from the string. IT->end_charpos may be greater than
5556 IT->string_nchars when this function is called, in which case we
5557 may have to return padding spaces. Value is zero if end of string
5558 reached, including padding spaces. */
5560 static int
5561 next_element_from_c_string (it)
5562 struct it *it;
5564 int success_p = 1;
5566 xassert (it->s);
5567 it->what = IT_CHARACTER;
5568 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5569 it->object = Qnil;
5571 /* IT's position can be greater IT->string_nchars in case a field
5572 width or precision has been specified when the iterator was
5573 initialized. */
5574 if (IT_CHARPOS (*it) >= it->end_charpos)
5576 /* End of the game. */
5577 it->what = IT_EOB;
5578 success_p = 0;
5580 else if (IT_CHARPOS (*it) >= it->string_nchars)
5582 /* Pad with spaces. */
5583 it->c = ' ', it->len = 1;
5584 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5586 else if (it->multibyte_p)
5588 /* Implementation note: The calls to strlen apparently aren't a
5589 performance problem because there is no noticeable performance
5590 difference between Emacs running in unibyte or multibyte mode. */
5591 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5592 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5593 maxlen, &it->len);
5595 else
5596 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5598 return success_p;
5602 /* Set up IT to return characters from an ellipsis, if appropriate.
5603 The definition of the ellipsis glyphs may come from a display table
5604 entry. This function Fills IT with the first glyph from the
5605 ellipsis if an ellipsis is to be displayed. */
5607 static int
5608 next_element_from_ellipsis (it)
5609 struct it *it;
5611 if (it->selective_display_ellipsis_p)
5612 setup_for_ellipsis (it, it->len);
5613 else
5615 /* The face at the current position may be different from the
5616 face we find after the invisible text. Remember what it
5617 was in IT->saved_face_id, and signal that it's there by
5618 setting face_before_selective_p. */
5619 it->saved_face_id = it->face_id;
5620 it->method = GET_FROM_BUFFER;
5621 reseat_at_next_visible_line_start (it, 1);
5622 it->face_before_selective_p = 1;
5625 return get_next_display_element (it);
5629 /* Deliver an image display element. The iterator IT is already
5630 filled with image information (done in handle_display_prop). Value
5631 is always 1. */
5634 static int
5635 next_element_from_image (it)
5636 struct it *it;
5638 it->what = IT_IMAGE;
5639 return 1;
5643 /* Fill iterator IT with next display element from a stretch glyph
5644 property. IT->object is the value of the text property. Value is
5645 always 1. */
5647 static int
5648 next_element_from_stretch (it)
5649 struct it *it;
5651 it->what = IT_STRETCH;
5652 return 1;
5656 /* Load IT with the next display element from current_buffer. Value
5657 is zero if end of buffer reached. IT->stop_charpos is the next
5658 position at which to stop and check for text properties or buffer
5659 end. */
5661 static int
5662 next_element_from_buffer (it)
5663 struct it *it;
5665 int success_p = 1;
5667 /* Check this assumption, otherwise, we would never enter the
5668 if-statement, below. */
5669 xassert (IT_CHARPOS (*it) >= BEGV
5670 && IT_CHARPOS (*it) <= it->stop_charpos);
5672 if (IT_CHARPOS (*it) >= it->stop_charpos)
5674 if (IT_CHARPOS (*it) >= it->end_charpos)
5676 int overlay_strings_follow_p;
5678 /* End of the game, except when overlay strings follow that
5679 haven't been returned yet. */
5680 if (it->overlay_strings_at_end_processed_p)
5681 overlay_strings_follow_p = 0;
5682 else
5684 it->overlay_strings_at_end_processed_p = 1;
5685 overlay_strings_follow_p = get_overlay_strings (it, 0);
5688 if (overlay_strings_follow_p)
5689 success_p = get_next_display_element (it);
5690 else
5692 it->what = IT_EOB;
5693 it->position = it->current.pos;
5694 success_p = 0;
5697 else
5699 handle_stop (it);
5700 return get_next_display_element (it);
5703 else
5705 /* No face changes, overlays etc. in sight, so just return a
5706 character from current_buffer. */
5707 unsigned char *p;
5709 /* Maybe run the redisplay end trigger hook. Performance note:
5710 This doesn't seem to cost measurable time. */
5711 if (it->redisplay_end_trigger_charpos
5712 && it->glyph_row
5713 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5714 run_redisplay_end_trigger_hook (it);
5716 /* Get the next character, maybe multibyte. */
5717 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5718 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5720 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5721 - IT_BYTEPOS (*it));
5722 it->c = string_char_and_length (p, maxlen, &it->len);
5724 else
5725 it->c = *p, it->len = 1;
5727 /* Record what we have and where it came from. */
5728 it->what = IT_CHARACTER;;
5729 it->object = it->w->buffer;
5730 it->position = it->current.pos;
5732 /* Normally we return the character found above, except when we
5733 really want to return an ellipsis for selective display. */
5734 if (it->selective)
5736 if (it->c == '\n')
5738 /* A value of selective > 0 means hide lines indented more
5739 than that number of columns. */
5740 if (it->selective > 0
5741 && IT_CHARPOS (*it) + 1 < ZV
5742 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5743 IT_BYTEPOS (*it) + 1,
5744 (double) it->selective)) /* iftc */
5746 success_p = next_element_from_ellipsis (it);
5747 it->dpvec_char_len = -1;
5750 else if (it->c == '\r' && it->selective == -1)
5752 /* A value of selective == -1 means that everything from the
5753 CR to the end of the line is invisible, with maybe an
5754 ellipsis displayed for it. */
5755 success_p = next_element_from_ellipsis (it);
5756 it->dpvec_char_len = -1;
5761 /* Value is zero if end of buffer reached. */
5762 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5763 return success_p;
5767 /* Run the redisplay end trigger hook for IT. */
5769 static void
5770 run_redisplay_end_trigger_hook (it)
5771 struct it *it;
5773 Lisp_Object args[3];
5775 /* IT->glyph_row should be non-null, i.e. we should be actually
5776 displaying something, or otherwise we should not run the hook. */
5777 xassert (it->glyph_row);
5779 /* Set up hook arguments. */
5780 args[0] = Qredisplay_end_trigger_functions;
5781 args[1] = it->window;
5782 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5783 it->redisplay_end_trigger_charpos = 0;
5785 /* Since we are *trying* to run these functions, don't try to run
5786 them again, even if they get an error. */
5787 it->w->redisplay_end_trigger = Qnil;
5788 Frun_hook_with_args (3, args);
5790 /* Notice if it changed the face of the character we are on. */
5791 handle_face_prop (it);
5795 /* Deliver a composition display element. The iterator IT is already
5796 filled with composition information (done in
5797 handle_composition_prop). Value is always 1. */
5799 static int
5800 next_element_from_composition (it)
5801 struct it *it;
5803 it->what = IT_COMPOSITION;
5804 it->position = (STRINGP (it->string)
5805 ? it->current.string_pos
5806 : it->current.pos);
5807 return 1;
5812 /***********************************************************************
5813 Moving an iterator without producing glyphs
5814 ***********************************************************************/
5816 /* Move iterator IT to a specified buffer or X position within one
5817 line on the display without producing glyphs.
5819 OP should be a bit mask including some or all of these bits:
5820 MOVE_TO_X: Stop on reaching x-position TO_X.
5821 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5822 Regardless of OP's value, stop in reaching the end of the display line.
5824 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5825 This means, in particular, that TO_X includes window's horizontal
5826 scroll amount.
5828 The return value has several possible values that
5829 say what condition caused the scan to stop:
5831 MOVE_POS_MATCH_OR_ZV
5832 - when TO_POS or ZV was reached.
5834 MOVE_X_REACHED
5835 -when TO_X was reached before TO_POS or ZV were reached.
5837 MOVE_LINE_CONTINUED
5838 - when we reached the end of the display area and the line must
5839 be continued.
5841 MOVE_LINE_TRUNCATED
5842 - when we reached the end of the display area and the line is
5843 truncated.
5845 MOVE_NEWLINE_OR_CR
5846 - when we stopped at a line end, i.e. a newline or a CR and selective
5847 display is on. */
5849 static enum move_it_result
5850 move_it_in_display_line_to (it, to_charpos, to_x, op)
5851 struct it *it;
5852 int to_charpos, to_x, op;
5854 enum move_it_result result = MOVE_UNDEFINED;
5855 struct glyph_row *saved_glyph_row;
5857 /* Don't produce glyphs in produce_glyphs. */
5858 saved_glyph_row = it->glyph_row;
5859 it->glyph_row = NULL;
5861 #define BUFFER_POS_REACHED_P() \
5862 ((op & MOVE_TO_POS) != 0 \
5863 && BUFFERP (it->object) \
5864 && IT_CHARPOS (*it) >= to_charpos \
5865 && (it->method == GET_FROM_BUFFER \
5866 || (it->method == GET_FROM_DISPLAY_VECTOR \
5867 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5870 while (1)
5872 int x, i, ascent = 0, descent = 0;
5874 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5875 if ((op & MOVE_TO_POS) != 0
5876 && BUFFERP (it->object)
5877 && it->method == GET_FROM_BUFFER
5878 && IT_CHARPOS (*it) > to_charpos)
5880 result = MOVE_POS_MATCH_OR_ZV;
5881 break;
5884 /* Stop when ZV reached.
5885 We used to stop here when TO_CHARPOS reached as well, but that is
5886 too soon if this glyph does not fit on this line. So we handle it
5887 explicitly below. */
5888 if (!get_next_display_element (it)
5889 || (it->truncate_lines_p
5890 && BUFFER_POS_REACHED_P ()))
5892 result = MOVE_POS_MATCH_OR_ZV;
5893 break;
5896 /* The call to produce_glyphs will get the metrics of the
5897 display element IT is loaded with. We record in x the
5898 x-position before this display element in case it does not
5899 fit on the line. */
5900 x = it->current_x;
5902 /* Remember the line height so far in case the next element doesn't
5903 fit on the line. */
5904 if (!it->truncate_lines_p)
5906 ascent = it->max_ascent;
5907 descent = it->max_descent;
5910 PRODUCE_GLYPHS (it);
5912 if (it->area != TEXT_AREA)
5914 set_iterator_to_next (it, 1);
5915 continue;
5918 /* The number of glyphs we get back in IT->nglyphs will normally
5919 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5920 character on a terminal frame, or (iii) a line end. For the
5921 second case, IT->nglyphs - 1 padding glyphs will be present
5922 (on X frames, there is only one glyph produced for a
5923 composite character.
5925 The behavior implemented below means, for continuation lines,
5926 that as many spaces of a TAB as fit on the current line are
5927 displayed there. For terminal frames, as many glyphs of a
5928 multi-glyph character are displayed in the current line, too.
5929 This is what the old redisplay code did, and we keep it that
5930 way. Under X, the whole shape of a complex character must
5931 fit on the line or it will be completely displayed in the
5932 next line.
5934 Note that both for tabs and padding glyphs, all glyphs have
5935 the same width. */
5936 if (it->nglyphs)
5938 /* More than one glyph or glyph doesn't fit on line. All
5939 glyphs have the same width. */
5940 int single_glyph_width = it->pixel_width / it->nglyphs;
5941 int new_x;
5943 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5945 new_x = x + single_glyph_width;
5947 /* We want to leave anything reaching TO_X to the caller. */
5948 if ((op & MOVE_TO_X) && new_x > to_x)
5950 if (BUFFER_POS_REACHED_P ())
5951 goto buffer_pos_reached;
5952 it->current_x = x;
5953 result = MOVE_X_REACHED;
5954 break;
5956 else if (/* Lines are continued. */
5957 !it->truncate_lines_p
5958 && (/* And glyph doesn't fit on the line. */
5959 new_x > it->last_visible_x
5960 /* Or it fits exactly and we're on a window
5961 system frame. */
5962 || (new_x == it->last_visible_x
5963 && FRAME_WINDOW_P (it->f))))
5965 if (/* IT->hpos == 0 means the very first glyph
5966 doesn't fit on the line, e.g. a wide image. */
5967 it->hpos == 0
5968 || (new_x == it->last_visible_x
5969 && FRAME_WINDOW_P (it->f)))
5971 ++it->hpos;
5972 it->current_x = new_x;
5973 if (i == it->nglyphs - 1)
5975 set_iterator_to_next (it, 1);
5976 #ifdef HAVE_WINDOW_SYSTEM
5977 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5979 if (!get_next_display_element (it))
5981 result = MOVE_POS_MATCH_OR_ZV;
5982 break;
5984 if (BUFFER_POS_REACHED_P ())
5986 if (ITERATOR_AT_END_OF_LINE_P (it))
5987 result = MOVE_POS_MATCH_OR_ZV;
5988 else
5989 result = MOVE_LINE_CONTINUED;
5990 break;
5992 if (ITERATOR_AT_END_OF_LINE_P (it))
5994 result = MOVE_NEWLINE_OR_CR;
5995 break;
5998 #endif /* HAVE_WINDOW_SYSTEM */
6001 else
6003 it->current_x = x;
6004 it->max_ascent = ascent;
6005 it->max_descent = descent;
6008 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6009 IT_CHARPOS (*it)));
6010 result = MOVE_LINE_CONTINUED;
6011 break;
6013 else if (BUFFER_POS_REACHED_P ())
6014 goto buffer_pos_reached;
6015 else if (new_x > it->first_visible_x)
6017 /* Glyph is visible. Increment number of glyphs that
6018 would be displayed. */
6019 ++it->hpos;
6021 else
6023 /* Glyph is completely off the left margin of the display
6024 area. Nothing to do. */
6028 if (result != MOVE_UNDEFINED)
6029 break;
6031 else if (BUFFER_POS_REACHED_P ())
6033 buffer_pos_reached:
6034 it->current_x = x;
6035 it->max_ascent = ascent;
6036 it->max_descent = descent;
6037 result = MOVE_POS_MATCH_OR_ZV;
6038 break;
6040 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6042 /* Stop when TO_X specified and reached. This check is
6043 necessary here because of lines consisting of a line end,
6044 only. The line end will not produce any glyphs and we
6045 would never get MOVE_X_REACHED. */
6046 xassert (it->nglyphs == 0);
6047 result = MOVE_X_REACHED;
6048 break;
6051 /* Is this a line end? If yes, we're done. */
6052 if (ITERATOR_AT_END_OF_LINE_P (it))
6054 result = MOVE_NEWLINE_OR_CR;
6055 break;
6058 /* The current display element has been consumed. Advance
6059 to the next. */
6060 set_iterator_to_next (it, 1);
6062 /* Stop if lines are truncated and IT's current x-position is
6063 past the right edge of the window now. */
6064 if (it->truncate_lines_p
6065 && it->current_x >= it->last_visible_x)
6067 #ifdef HAVE_WINDOW_SYSTEM
6068 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6070 if (!get_next_display_element (it)
6071 || BUFFER_POS_REACHED_P ())
6073 result = MOVE_POS_MATCH_OR_ZV;
6074 break;
6076 if (ITERATOR_AT_END_OF_LINE_P (it))
6078 result = MOVE_NEWLINE_OR_CR;
6079 break;
6082 #endif /* HAVE_WINDOW_SYSTEM */
6083 result = MOVE_LINE_TRUNCATED;
6084 break;
6088 #undef BUFFER_POS_REACHED_P
6090 /* Restore the iterator settings altered at the beginning of this
6091 function. */
6092 it->glyph_row = saved_glyph_row;
6093 return result;
6097 /* Move IT forward until it satisfies one or more of the criteria in
6098 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6100 OP is a bit-mask that specifies where to stop, and in particular,
6101 which of those four position arguments makes a difference. See the
6102 description of enum move_operation_enum.
6104 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6105 screen line, this function will set IT to the next position >
6106 TO_CHARPOS. */
6108 void
6109 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6110 struct it *it;
6111 int to_charpos, to_x, to_y, to_vpos;
6112 int op;
6114 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6115 int line_height;
6116 int reached = 0;
6118 for (;;)
6120 if (op & MOVE_TO_VPOS)
6122 /* If no TO_CHARPOS and no TO_X specified, stop at the
6123 start of the line TO_VPOS. */
6124 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6126 if (it->vpos == to_vpos)
6128 reached = 1;
6129 break;
6131 else
6132 skip = move_it_in_display_line_to (it, -1, -1, 0);
6134 else
6136 /* TO_VPOS >= 0 means stop at TO_X in the line at
6137 TO_VPOS, or at TO_POS, whichever comes first. */
6138 if (it->vpos == to_vpos)
6140 reached = 2;
6141 break;
6144 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6146 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6148 reached = 3;
6149 break;
6151 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6153 /* We have reached TO_X but not in the line we want. */
6154 skip = move_it_in_display_line_to (it, to_charpos,
6155 -1, MOVE_TO_POS);
6156 if (skip == MOVE_POS_MATCH_OR_ZV)
6158 reached = 4;
6159 break;
6164 else if (op & MOVE_TO_Y)
6166 struct it it_backup;
6168 /* TO_Y specified means stop at TO_X in the line containing
6169 TO_Y---or at TO_CHARPOS if this is reached first. The
6170 problem is that we can't really tell whether the line
6171 contains TO_Y before we have completely scanned it, and
6172 this may skip past TO_X. What we do is to first scan to
6173 TO_X.
6175 If TO_X is not specified, use a TO_X of zero. The reason
6176 is to make the outcome of this function more predictable.
6177 If we didn't use TO_X == 0, we would stop at the end of
6178 the line which is probably not what a caller would expect
6179 to happen. */
6180 skip = move_it_in_display_line_to (it, to_charpos,
6181 ((op & MOVE_TO_X)
6182 ? to_x : 0),
6183 (MOVE_TO_X
6184 | (op & MOVE_TO_POS)));
6186 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6187 if (skip == MOVE_POS_MATCH_OR_ZV)
6189 reached = 5;
6190 break;
6193 /* If TO_X was reached, we would like to know whether TO_Y
6194 is in the line. This can only be said if we know the
6195 total line height which requires us to scan the rest of
6196 the line. */
6197 if (skip == MOVE_X_REACHED)
6199 it_backup = *it;
6200 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6201 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6202 op & MOVE_TO_POS);
6203 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6206 /* Now, decide whether TO_Y is in this line. */
6207 line_height = it->max_ascent + it->max_descent;
6208 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6210 if (to_y >= it->current_y
6211 && to_y < it->current_y + line_height)
6213 if (skip == MOVE_X_REACHED)
6214 /* If TO_Y is in this line and TO_X was reached above,
6215 we scanned too far. We have to restore IT's settings
6216 to the ones before skipping. */
6217 *it = it_backup;
6218 reached = 6;
6220 else if (skip == MOVE_X_REACHED)
6222 skip = skip2;
6223 if (skip == MOVE_POS_MATCH_OR_ZV)
6224 reached = 7;
6227 if (reached)
6228 break;
6230 else
6231 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6233 switch (skip)
6235 case MOVE_POS_MATCH_OR_ZV:
6236 reached = 8;
6237 goto out;
6239 case MOVE_NEWLINE_OR_CR:
6240 set_iterator_to_next (it, 1);
6241 it->continuation_lines_width = 0;
6242 break;
6244 case MOVE_LINE_TRUNCATED:
6245 it->continuation_lines_width = 0;
6246 reseat_at_next_visible_line_start (it, 0);
6247 if ((op & MOVE_TO_POS) != 0
6248 && IT_CHARPOS (*it) > to_charpos)
6250 reached = 9;
6251 goto out;
6253 break;
6255 case MOVE_LINE_CONTINUED:
6256 it->continuation_lines_width += it->current_x;
6257 break;
6259 default:
6260 abort ();
6263 /* Reset/increment for the next run. */
6264 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6265 it->current_x = it->hpos = 0;
6266 it->current_y += it->max_ascent + it->max_descent;
6267 ++it->vpos;
6268 last_height = it->max_ascent + it->max_descent;
6269 last_max_ascent = it->max_ascent;
6270 it->max_ascent = it->max_descent = 0;
6273 out:
6275 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6279 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6281 If DY > 0, move IT backward at least that many pixels. DY = 0
6282 means move IT backward to the preceding line start or BEGV. This
6283 function may move over more than DY pixels if IT->current_y - DY
6284 ends up in the middle of a line; in this case IT->current_y will be
6285 set to the top of the line moved to. */
6287 void
6288 move_it_vertically_backward (it, dy)
6289 struct it *it;
6290 int dy;
6292 int nlines, h;
6293 struct it it2, it3;
6294 int start_pos;
6296 move_further_back:
6297 xassert (dy >= 0);
6299 start_pos = IT_CHARPOS (*it);
6301 /* Estimate how many newlines we must move back. */
6302 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6304 /* Set the iterator's position that many lines back. */
6305 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6306 back_to_previous_visible_line_start (it);
6308 /* Reseat the iterator here. When moving backward, we don't want
6309 reseat to skip forward over invisible text, set up the iterator
6310 to deliver from overlay strings at the new position etc. So,
6311 use reseat_1 here. */
6312 reseat_1 (it, it->current.pos, 1);
6314 /* We are now surely at a line start. */
6315 it->current_x = it->hpos = 0;
6316 it->continuation_lines_width = 0;
6318 /* Move forward and see what y-distance we moved. First move to the
6319 start of the next line so that we get its height. We need this
6320 height to be able to tell whether we reached the specified
6321 y-distance. */
6322 it2 = *it;
6323 it2.max_ascent = it2.max_descent = 0;
6324 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6325 MOVE_TO_POS | MOVE_TO_VPOS);
6326 xassert (IT_CHARPOS (*it) >= BEGV);
6327 it3 = it2;
6329 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6330 xassert (IT_CHARPOS (*it) >= BEGV);
6331 /* H is the actual vertical distance from the position in *IT
6332 and the starting position. */
6333 h = it2.current_y - it->current_y;
6334 /* NLINES is the distance in number of lines. */
6335 nlines = it2.vpos - it->vpos;
6337 /* Correct IT's y and vpos position
6338 so that they are relative to the starting point. */
6339 it->vpos -= nlines;
6340 it->current_y -= h;
6342 if (dy == 0)
6344 /* DY == 0 means move to the start of the screen line. The
6345 value of nlines is > 0 if continuation lines were involved. */
6346 if (nlines > 0)
6347 move_it_by_lines (it, nlines, 1);
6348 #if 0
6349 /* I think this assert is bogus if buffer contains
6350 invisible text or images. KFS. */
6351 xassert (IT_CHARPOS (*it) <= start_pos);
6352 #endif
6354 else
6356 /* The y-position we try to reach, relative to *IT.
6357 Note that H has been subtracted in front of the if-statement. */
6358 int target_y = it->current_y + h - dy;
6359 int y0 = it3.current_y;
6360 int y1 = line_bottom_y (&it3);
6361 int line_height = y1 - y0;
6363 /* If we did not reach target_y, try to move further backward if
6364 we can. If we moved too far backward, try to move forward. */
6365 if (target_y < it->current_y
6366 /* This is heuristic. In a window that's 3 lines high, with
6367 a line height of 13 pixels each, recentering with point
6368 on the bottom line will try to move -39/2 = 19 pixels
6369 backward. Try to avoid moving into the first line. */
6370 && (it->current_y - target_y
6371 > min (window_box_height (it->w), line_height * 2 / 3))
6372 && IT_CHARPOS (*it) > BEGV)
6374 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6375 target_y - it->current_y));
6376 dy = it->current_y - target_y;
6377 goto move_further_back;
6379 else if (target_y >= it->current_y + line_height
6380 && IT_CHARPOS (*it) < ZV)
6382 /* Should move forward by at least one line, maybe more.
6384 Note: Calling move_it_by_lines can be expensive on
6385 terminal frames, where compute_motion is used (via
6386 vmotion) to do the job, when there are very long lines
6387 and truncate-lines is nil. That's the reason for
6388 treating terminal frames specially here. */
6390 if (!FRAME_WINDOW_P (it->f))
6391 move_it_vertically (it, target_y - (it->current_y + line_height));
6392 else
6396 move_it_by_lines (it, 1, 1);
6398 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6401 #if 0
6402 /* I think this assert is bogus if buffer contains
6403 invisible text or images. KFS. */
6404 xassert (IT_CHARPOS (*it) >= BEGV);
6405 #endif
6411 /* Move IT by a specified amount of pixel lines DY. DY negative means
6412 move backwards. DY = 0 means move to start of screen line. At the
6413 end, IT will be on the start of a screen line. */
6415 void
6416 move_it_vertically (it, dy)
6417 struct it *it;
6418 int dy;
6420 if (dy <= 0)
6421 move_it_vertically_backward (it, -dy);
6422 else
6424 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6425 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6426 MOVE_TO_POS | MOVE_TO_Y);
6427 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6429 /* If buffer ends in ZV without a newline, move to the start of
6430 the line to satisfy the post-condition. */
6431 if (IT_CHARPOS (*it) == ZV
6432 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6433 move_it_by_lines (it, 0, 0);
6438 /* Move iterator IT past the end of the text line it is in. */
6440 void
6441 move_it_past_eol (it)
6442 struct it *it;
6444 enum move_it_result rc;
6446 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6447 if (rc == MOVE_NEWLINE_OR_CR)
6448 set_iterator_to_next (it, 0);
6452 #if 0 /* Currently not used. */
6454 /* Return non-zero if some text between buffer positions START_CHARPOS
6455 and END_CHARPOS is invisible. IT->window is the window for text
6456 property lookup. */
6458 static int
6459 invisible_text_between_p (it, start_charpos, end_charpos)
6460 struct it *it;
6461 int start_charpos, end_charpos;
6463 Lisp_Object prop, limit;
6464 int invisible_found_p;
6466 xassert (it != NULL && start_charpos <= end_charpos);
6468 /* Is text at START invisible? */
6469 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6470 it->window);
6471 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6472 invisible_found_p = 1;
6473 else
6475 limit = Fnext_single_char_property_change (make_number (start_charpos),
6476 Qinvisible, Qnil,
6477 make_number (end_charpos));
6478 invisible_found_p = XFASTINT (limit) < end_charpos;
6481 return invisible_found_p;
6484 #endif /* 0 */
6487 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6488 negative means move up. DVPOS == 0 means move to the start of the
6489 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6490 NEED_Y_P is zero, IT->current_y will be left unchanged.
6492 Further optimization ideas: If we would know that IT->f doesn't use
6493 a face with proportional font, we could be faster for
6494 truncate-lines nil. */
6496 void
6497 move_it_by_lines (it, dvpos, need_y_p)
6498 struct it *it;
6499 int dvpos, need_y_p;
6501 struct position pos;
6503 if (!FRAME_WINDOW_P (it->f))
6505 struct text_pos textpos;
6507 /* We can use vmotion on frames without proportional fonts. */
6508 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6509 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6510 reseat (it, textpos, 1);
6511 it->vpos += pos.vpos;
6512 it->current_y += pos.vpos;
6514 else if (dvpos == 0)
6516 /* DVPOS == 0 means move to the start of the screen line. */
6517 move_it_vertically_backward (it, 0);
6518 xassert (it->current_x == 0 && it->hpos == 0);
6519 /* Let next call to line_bottom_y calculate real line height */
6520 last_height = 0;
6522 else if (dvpos > 0)
6523 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6524 else
6526 struct it it2;
6527 int start_charpos, i;
6529 /* Start at the beginning of the screen line containing IT's
6530 position. */
6531 move_it_vertically_backward (it, 0);
6533 /* Go back -DVPOS visible lines and reseat the iterator there. */
6534 start_charpos = IT_CHARPOS (*it);
6535 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6536 back_to_previous_visible_line_start (it);
6537 reseat (it, it->current.pos, 1);
6538 it->current_x = it->hpos = 0;
6540 /* Above call may have moved too far if continuation lines
6541 are involved. Scan forward and see if it did. */
6542 it2 = *it;
6543 it2.vpos = it2.current_y = 0;
6544 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6545 it->vpos -= it2.vpos;
6546 it->current_y -= it2.current_y;
6547 it->current_x = it->hpos = 0;
6549 /* If we moved too far back, move IT some lines forward. */
6550 if (it2.vpos > -dvpos)
6552 int delta = it2.vpos + dvpos;
6553 it2 = *it;
6554 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6555 /* Move back again if we got too far ahead. */
6556 if (IT_CHARPOS (*it) >= start_charpos)
6557 *it = it2;
6562 /* Return 1 if IT points into the middle of a display vector. */
6565 in_display_vector_p (it)
6566 struct it *it;
6568 return (it->method == GET_FROM_DISPLAY_VECTOR
6569 && it->current.dpvec_index > 0
6570 && it->dpvec + it->current.dpvec_index != it->dpend);
6574 /***********************************************************************
6575 Messages
6576 ***********************************************************************/
6579 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6580 to *Messages*. */
6582 void
6583 add_to_log (format, arg1, arg2)
6584 char *format;
6585 Lisp_Object arg1, arg2;
6587 Lisp_Object args[3];
6588 Lisp_Object msg, fmt;
6589 char *buffer;
6590 int len;
6591 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6592 USE_SAFE_ALLOCA;
6594 /* Do nothing if called asynchronously. Inserting text into
6595 a buffer may call after-change-functions and alike and
6596 that would means running Lisp asynchronously. */
6597 if (handling_signal)
6598 return;
6600 fmt = msg = Qnil;
6601 GCPRO4 (fmt, msg, arg1, arg2);
6603 args[0] = fmt = build_string (format);
6604 args[1] = arg1;
6605 args[2] = arg2;
6606 msg = Fformat (3, args);
6608 len = SBYTES (msg) + 1;
6609 SAFE_ALLOCA (buffer, char *, len);
6610 bcopy (SDATA (msg), buffer, len);
6612 message_dolog (buffer, len - 1, 1, 0);
6613 SAFE_FREE ();
6615 UNGCPRO;
6619 /* Output a newline in the *Messages* buffer if "needs" one. */
6621 void
6622 message_log_maybe_newline ()
6624 if (message_log_need_newline)
6625 message_dolog ("", 0, 1, 0);
6629 /* Add a string M of length NBYTES to the message log, optionally
6630 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6631 nonzero, means interpret the contents of M as multibyte. This
6632 function calls low-level routines in order to bypass text property
6633 hooks, etc. which might not be safe to run. */
6635 void
6636 message_dolog (m, nbytes, nlflag, multibyte)
6637 const char *m;
6638 int nbytes, nlflag, multibyte;
6640 if (!NILP (Vmemory_full))
6641 return;
6643 if (!NILP (Vmessage_log_max))
6645 struct buffer *oldbuf;
6646 Lisp_Object oldpoint, oldbegv, oldzv;
6647 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6648 int point_at_end = 0;
6649 int zv_at_end = 0;
6650 Lisp_Object old_deactivate_mark, tem;
6651 struct gcpro gcpro1;
6653 old_deactivate_mark = Vdeactivate_mark;
6654 oldbuf = current_buffer;
6655 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6656 current_buffer->undo_list = Qt;
6658 oldpoint = message_dolog_marker1;
6659 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6660 oldbegv = message_dolog_marker2;
6661 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6662 oldzv = message_dolog_marker3;
6663 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6664 GCPRO1 (old_deactivate_mark);
6666 if (PT == Z)
6667 point_at_end = 1;
6668 if (ZV == Z)
6669 zv_at_end = 1;
6671 BEGV = BEG;
6672 BEGV_BYTE = BEG_BYTE;
6673 ZV = Z;
6674 ZV_BYTE = Z_BYTE;
6675 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6677 /* Insert the string--maybe converting multibyte to single byte
6678 or vice versa, so that all the text fits the buffer. */
6679 if (multibyte
6680 && NILP (current_buffer->enable_multibyte_characters))
6682 int i, c, char_bytes;
6683 unsigned char work[1];
6685 /* Convert a multibyte string to single-byte
6686 for the *Message* buffer. */
6687 for (i = 0; i < nbytes; i += char_bytes)
6689 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6690 work[0] = (SINGLE_BYTE_CHAR_P (c)
6692 : multibyte_char_to_unibyte (c, Qnil));
6693 insert_1_both (work, 1, 1, 1, 0, 0);
6696 else if (! multibyte
6697 && ! NILP (current_buffer->enable_multibyte_characters))
6699 int i, c, char_bytes;
6700 unsigned char *msg = (unsigned char *) m;
6701 unsigned char str[MAX_MULTIBYTE_LENGTH];
6702 /* Convert a single-byte string to multibyte
6703 for the *Message* buffer. */
6704 for (i = 0; i < nbytes; i++)
6706 c = unibyte_char_to_multibyte (msg[i]);
6707 char_bytes = CHAR_STRING (c, str);
6708 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6711 else if (nbytes)
6712 insert_1 (m, nbytes, 1, 0, 0);
6714 if (nlflag)
6716 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6717 insert_1 ("\n", 1, 1, 0, 0);
6719 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6720 this_bol = PT;
6721 this_bol_byte = PT_BYTE;
6723 /* See if this line duplicates the previous one.
6724 If so, combine duplicates. */
6725 if (this_bol > BEG)
6727 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6728 prev_bol = PT;
6729 prev_bol_byte = PT_BYTE;
6731 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6732 this_bol, this_bol_byte);
6733 if (dup)
6735 del_range_both (prev_bol, prev_bol_byte,
6736 this_bol, this_bol_byte, 0);
6737 if (dup > 1)
6739 char dupstr[40];
6740 int duplen;
6742 /* If you change this format, don't forget to also
6743 change message_log_check_duplicate. */
6744 sprintf (dupstr, " [%d times]", dup);
6745 duplen = strlen (dupstr);
6746 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6747 insert_1 (dupstr, duplen, 1, 0, 1);
6752 /* If we have more than the desired maximum number of lines
6753 in the *Messages* buffer now, delete the oldest ones.
6754 This is safe because we don't have undo in this buffer. */
6756 if (NATNUMP (Vmessage_log_max))
6758 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6759 -XFASTINT (Vmessage_log_max) - 1, 0);
6760 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6763 BEGV = XMARKER (oldbegv)->charpos;
6764 BEGV_BYTE = marker_byte_position (oldbegv);
6766 if (zv_at_end)
6768 ZV = Z;
6769 ZV_BYTE = Z_BYTE;
6771 else
6773 ZV = XMARKER (oldzv)->charpos;
6774 ZV_BYTE = marker_byte_position (oldzv);
6777 if (point_at_end)
6778 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6779 else
6780 /* We can't do Fgoto_char (oldpoint) because it will run some
6781 Lisp code. */
6782 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6783 XMARKER (oldpoint)->bytepos);
6785 UNGCPRO;
6786 unchain_marker (XMARKER (oldpoint));
6787 unchain_marker (XMARKER (oldbegv));
6788 unchain_marker (XMARKER (oldzv));
6790 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6791 set_buffer_internal (oldbuf);
6792 if (NILP (tem))
6793 windows_or_buffers_changed = old_windows_or_buffers_changed;
6794 message_log_need_newline = !nlflag;
6795 Vdeactivate_mark = old_deactivate_mark;
6800 /* We are at the end of the buffer after just having inserted a newline.
6801 (Note: We depend on the fact we won't be crossing the gap.)
6802 Check to see if the most recent message looks a lot like the previous one.
6803 Return 0 if different, 1 if the new one should just replace it, or a
6804 value N > 1 if we should also append " [N times]". */
6806 static int
6807 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6808 int prev_bol, this_bol;
6809 int prev_bol_byte, this_bol_byte;
6811 int i;
6812 int len = Z_BYTE - 1 - this_bol_byte;
6813 int seen_dots = 0;
6814 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6815 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6817 for (i = 0; i < len; i++)
6819 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6820 seen_dots = 1;
6821 if (p1[i] != p2[i])
6822 return seen_dots;
6824 p1 += len;
6825 if (*p1 == '\n')
6826 return 2;
6827 if (*p1++ == ' ' && *p1++ == '[')
6829 int n = 0;
6830 while (*p1 >= '0' && *p1 <= '9')
6831 n = n * 10 + *p1++ - '0';
6832 if (strncmp (p1, " times]\n", 8) == 0)
6833 return n+1;
6835 return 0;
6839 /* Display an echo area message M with a specified length of NBYTES
6840 bytes. The string may include null characters. If M is 0, clear
6841 out any existing message, and let the mini-buffer text show
6842 through.
6844 The buffer M must continue to exist until after the echo area gets
6845 cleared or some other message gets displayed there. This means do
6846 not pass text that is stored in a Lisp string; do not pass text in
6847 a buffer that was alloca'd. */
6849 void
6850 message2 (m, nbytes, multibyte)
6851 const char *m;
6852 int nbytes;
6853 int multibyte;
6855 /* First flush out any partial line written with print. */
6856 message_log_maybe_newline ();
6857 if (m)
6858 message_dolog (m, nbytes, 1, multibyte);
6859 message2_nolog (m, nbytes, multibyte);
6863 /* The non-logging counterpart of message2. */
6865 void
6866 message2_nolog (m, nbytes, multibyte)
6867 const char *m;
6868 int nbytes, multibyte;
6870 struct frame *sf = SELECTED_FRAME ();
6871 message_enable_multibyte = multibyte;
6873 if (noninteractive)
6875 if (noninteractive_need_newline)
6876 putc ('\n', stderr);
6877 noninteractive_need_newline = 0;
6878 if (m)
6879 fwrite (m, nbytes, 1, stderr);
6880 if (cursor_in_echo_area == 0)
6881 fprintf (stderr, "\n");
6882 fflush (stderr);
6884 /* A null message buffer means that the frame hasn't really been
6885 initialized yet. Error messages get reported properly by
6886 cmd_error, so this must be just an informative message; toss it. */
6887 else if (INTERACTIVE
6888 && sf->glyphs_initialized_p
6889 && FRAME_MESSAGE_BUF (sf))
6891 Lisp_Object mini_window;
6892 struct frame *f;
6894 /* Get the frame containing the mini-buffer
6895 that the selected frame is using. */
6896 mini_window = FRAME_MINIBUF_WINDOW (sf);
6897 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6899 FRAME_SAMPLE_VISIBILITY (f);
6900 if (FRAME_VISIBLE_P (sf)
6901 && ! FRAME_VISIBLE_P (f))
6902 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6904 if (m)
6906 set_message (m, Qnil, nbytes, multibyte);
6907 if (minibuffer_auto_raise)
6908 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6910 else
6911 clear_message (1, 1);
6913 do_pending_window_change (0);
6914 echo_area_display (1);
6915 do_pending_window_change (0);
6916 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6917 (*frame_up_to_date_hook) (f);
6922 /* Display an echo area message M with a specified length of NBYTES
6923 bytes. The string may include null characters. If M is not a
6924 string, clear out any existing message, and let the mini-buffer
6925 text show through.
6927 This function cancels echoing. */
6929 void
6930 message3 (m, nbytes, multibyte)
6931 Lisp_Object m;
6932 int nbytes;
6933 int multibyte;
6935 struct gcpro gcpro1;
6937 GCPRO1 (m);
6938 clear_message (1,1);
6939 cancel_echoing ();
6941 /* First flush out any partial line written with print. */
6942 message_log_maybe_newline ();
6943 if (STRINGP (m))
6944 message_dolog (SDATA (m), nbytes, 1, multibyte);
6945 message3_nolog (m, nbytes, multibyte);
6947 UNGCPRO;
6951 /* The non-logging version of message3.
6952 This does not cancel echoing, because it is used for echoing.
6953 Perhaps we need to make a separate function for echoing
6954 and make this cancel echoing. */
6956 void
6957 message3_nolog (m, nbytes, multibyte)
6958 Lisp_Object m;
6959 int nbytes, multibyte;
6961 struct frame *sf = SELECTED_FRAME ();
6962 message_enable_multibyte = multibyte;
6964 if (noninteractive)
6966 if (noninteractive_need_newline)
6967 putc ('\n', stderr);
6968 noninteractive_need_newline = 0;
6969 if (STRINGP (m))
6970 fwrite (SDATA (m), nbytes, 1, stderr);
6971 if (cursor_in_echo_area == 0)
6972 fprintf (stderr, "\n");
6973 fflush (stderr);
6975 /* A null message buffer means that the frame hasn't really been
6976 initialized yet. Error messages get reported properly by
6977 cmd_error, so this must be just an informative message; toss it. */
6978 else if (INTERACTIVE
6979 && sf->glyphs_initialized_p
6980 && FRAME_MESSAGE_BUF (sf))
6982 Lisp_Object mini_window;
6983 Lisp_Object frame;
6984 struct frame *f;
6986 /* Get the frame containing the mini-buffer
6987 that the selected frame is using. */
6988 mini_window = FRAME_MINIBUF_WINDOW (sf);
6989 frame = XWINDOW (mini_window)->frame;
6990 f = XFRAME (frame);
6992 FRAME_SAMPLE_VISIBILITY (f);
6993 if (FRAME_VISIBLE_P (sf)
6994 && !FRAME_VISIBLE_P (f))
6995 Fmake_frame_visible (frame);
6997 if (STRINGP (m) && SCHARS (m) > 0)
6999 set_message (NULL, m, nbytes, multibyte);
7000 if (minibuffer_auto_raise)
7001 Fraise_frame (frame);
7003 else
7004 clear_message (1, 1);
7006 do_pending_window_change (0);
7007 echo_area_display (1);
7008 do_pending_window_change (0);
7009 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7010 (*frame_up_to_date_hook) (f);
7015 /* Display a null-terminated echo area message M. If M is 0, clear
7016 out any existing message, and let the mini-buffer text show through.
7018 The buffer M must continue to exist until after the echo area gets
7019 cleared or some other message gets displayed there. Do not pass
7020 text that is stored in a Lisp string. Do not pass text in a buffer
7021 that was alloca'd. */
7023 void
7024 message1 (m)
7025 char *m;
7027 message2 (m, (m ? strlen (m) : 0), 0);
7031 /* The non-logging counterpart of message1. */
7033 void
7034 message1_nolog (m)
7035 char *m;
7037 message2_nolog (m, (m ? strlen (m) : 0), 0);
7040 /* Display a message M which contains a single %s
7041 which gets replaced with STRING. */
7043 void
7044 message_with_string (m, string, log)
7045 char *m;
7046 Lisp_Object string;
7047 int log;
7049 CHECK_STRING (string);
7051 if (noninteractive)
7053 if (m)
7055 if (noninteractive_need_newline)
7056 putc ('\n', stderr);
7057 noninteractive_need_newline = 0;
7058 fprintf (stderr, m, SDATA (string));
7059 if (cursor_in_echo_area == 0)
7060 fprintf (stderr, "\n");
7061 fflush (stderr);
7064 else if (INTERACTIVE)
7066 /* The frame whose minibuffer we're going to display the message on.
7067 It may be larger than the selected frame, so we need
7068 to use its buffer, not the selected frame's buffer. */
7069 Lisp_Object mini_window;
7070 struct frame *f, *sf = SELECTED_FRAME ();
7072 /* Get the frame containing the minibuffer
7073 that the selected frame is using. */
7074 mini_window = FRAME_MINIBUF_WINDOW (sf);
7075 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7077 /* A null message buffer means that the frame hasn't really been
7078 initialized yet. Error messages get reported properly by
7079 cmd_error, so this must be just an informative message; toss it. */
7080 if (FRAME_MESSAGE_BUF (f))
7082 Lisp_Object args[2], message;
7083 struct gcpro gcpro1, gcpro2;
7085 args[0] = build_string (m);
7086 args[1] = message = string;
7087 GCPRO2 (args[0], message);
7088 gcpro1.nvars = 2;
7090 message = Fformat (2, args);
7092 if (log)
7093 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7094 else
7095 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7097 UNGCPRO;
7099 /* Print should start at the beginning of the message
7100 buffer next time. */
7101 message_buf_print = 0;
7107 /* Dump an informative message to the minibuf. If M is 0, clear out
7108 any existing message, and let the mini-buffer text show through. */
7110 /* VARARGS 1 */
7111 void
7112 message (m, a1, a2, a3)
7113 char *m;
7114 EMACS_INT a1, a2, a3;
7116 if (noninteractive)
7118 if (m)
7120 if (noninteractive_need_newline)
7121 putc ('\n', stderr);
7122 noninteractive_need_newline = 0;
7123 fprintf (stderr, m, a1, a2, a3);
7124 if (cursor_in_echo_area == 0)
7125 fprintf (stderr, "\n");
7126 fflush (stderr);
7129 else if (INTERACTIVE)
7131 /* The frame whose mini-buffer we're going to display the message
7132 on. It may be larger than the selected frame, so we need to
7133 use its buffer, not the selected frame's buffer. */
7134 Lisp_Object mini_window;
7135 struct frame *f, *sf = SELECTED_FRAME ();
7137 /* Get the frame containing the mini-buffer
7138 that the selected frame is using. */
7139 mini_window = FRAME_MINIBUF_WINDOW (sf);
7140 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7142 /* A null message buffer means that the frame hasn't really been
7143 initialized yet. Error messages get reported properly by
7144 cmd_error, so this must be just an informative message; toss
7145 it. */
7146 if (FRAME_MESSAGE_BUF (f))
7148 if (m)
7150 int len;
7151 #ifdef NO_ARG_ARRAY
7152 char *a[3];
7153 a[0] = (char *) a1;
7154 a[1] = (char *) a2;
7155 a[2] = (char *) a3;
7157 len = doprnt (FRAME_MESSAGE_BUF (f),
7158 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7159 #else
7160 len = doprnt (FRAME_MESSAGE_BUF (f),
7161 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7162 (char **) &a1);
7163 #endif /* NO_ARG_ARRAY */
7165 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7167 else
7168 message1 (0);
7170 /* Print should start at the beginning of the message
7171 buffer next time. */
7172 message_buf_print = 0;
7178 /* The non-logging version of message. */
7180 void
7181 message_nolog (m, a1, a2, a3)
7182 char *m;
7183 EMACS_INT a1, a2, a3;
7185 Lisp_Object old_log_max;
7186 old_log_max = Vmessage_log_max;
7187 Vmessage_log_max = Qnil;
7188 message (m, a1, a2, a3);
7189 Vmessage_log_max = old_log_max;
7193 /* Display the current message in the current mini-buffer. This is
7194 only called from error handlers in process.c, and is not time
7195 critical. */
7197 void
7198 update_echo_area ()
7200 if (!NILP (echo_area_buffer[0]))
7202 Lisp_Object string;
7203 string = Fcurrent_message ();
7204 message3 (string, SBYTES (string),
7205 !NILP (current_buffer->enable_multibyte_characters));
7210 /* Make sure echo area buffers in `echo_buffers' are live.
7211 If they aren't, make new ones. */
7213 static void
7214 ensure_echo_area_buffers ()
7216 int i;
7218 for (i = 0; i < 2; ++i)
7219 if (!BUFFERP (echo_buffer[i])
7220 || NILP (XBUFFER (echo_buffer[i])->name))
7222 char name[30];
7223 Lisp_Object old_buffer;
7224 int j;
7226 old_buffer = echo_buffer[i];
7227 sprintf (name, " *Echo Area %d*", i);
7228 echo_buffer[i] = Fget_buffer_create (build_string (name));
7229 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7231 for (j = 0; j < 2; ++j)
7232 if (EQ (old_buffer, echo_area_buffer[j]))
7233 echo_area_buffer[j] = echo_buffer[i];
7238 /* Call FN with args A1..A4 with either the current or last displayed
7239 echo_area_buffer as current buffer.
7241 WHICH zero means use the current message buffer
7242 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7243 from echo_buffer[] and clear it.
7245 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7246 suitable buffer from echo_buffer[] and clear it.
7248 Value is what FN returns. */
7250 static int
7251 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7252 struct window *w;
7253 int which;
7254 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7255 EMACS_INT a1;
7256 Lisp_Object a2;
7257 EMACS_INT a3, a4;
7259 Lisp_Object buffer;
7260 int this_one, the_other, clear_buffer_p, rc;
7261 int count = SPECPDL_INDEX ();
7263 /* If buffers aren't live, make new ones. */
7264 ensure_echo_area_buffers ();
7266 clear_buffer_p = 0;
7268 if (which == 0)
7269 this_one = 0, the_other = 1;
7270 else if (which > 0)
7271 this_one = 1, the_other = 0;
7273 /* Choose a suitable buffer from echo_buffer[] is we don't
7274 have one. */
7275 if (NILP (echo_area_buffer[this_one]))
7277 echo_area_buffer[this_one]
7278 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7279 ? echo_buffer[the_other]
7280 : echo_buffer[this_one]);
7281 clear_buffer_p = 1;
7284 buffer = echo_area_buffer[this_one];
7286 /* Don't get confused by reusing the buffer used for echoing
7287 for a different purpose. */
7288 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7289 cancel_echoing ();
7291 record_unwind_protect (unwind_with_echo_area_buffer,
7292 with_echo_area_buffer_unwind_data (w));
7294 /* Make the echo area buffer current. Note that for display
7295 purposes, it is not necessary that the displayed window's buffer
7296 == current_buffer, except for text property lookup. So, let's
7297 only set that buffer temporarily here without doing a full
7298 Fset_window_buffer. We must also change w->pointm, though,
7299 because otherwise an assertions in unshow_buffer fails, and Emacs
7300 aborts. */
7301 set_buffer_internal_1 (XBUFFER (buffer));
7302 if (w)
7304 w->buffer = buffer;
7305 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7308 current_buffer->undo_list = Qt;
7309 current_buffer->read_only = Qnil;
7310 specbind (Qinhibit_read_only, Qt);
7311 specbind (Qinhibit_modification_hooks, Qt);
7313 if (clear_buffer_p && Z > BEG)
7314 del_range (BEG, Z);
7316 xassert (BEGV >= BEG);
7317 xassert (ZV <= Z && ZV >= BEGV);
7319 rc = fn (a1, a2, a3, a4);
7321 xassert (BEGV >= BEG);
7322 xassert (ZV <= Z && ZV >= BEGV);
7324 unbind_to (count, Qnil);
7325 return rc;
7329 /* Save state that should be preserved around the call to the function
7330 FN called in with_echo_area_buffer. */
7332 static Lisp_Object
7333 with_echo_area_buffer_unwind_data (w)
7334 struct window *w;
7336 int i = 0;
7337 Lisp_Object vector;
7339 /* Reduce consing by keeping one vector in
7340 Vwith_echo_area_save_vector. */
7341 vector = Vwith_echo_area_save_vector;
7342 Vwith_echo_area_save_vector = Qnil;
7344 if (NILP (vector))
7345 vector = Fmake_vector (make_number (7), Qnil);
7347 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7348 AREF (vector, i) = Vdeactivate_mark, ++i;
7349 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7351 if (w)
7353 XSETWINDOW (AREF (vector, i), w); ++i;
7354 AREF (vector, i) = w->buffer; ++i;
7355 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7356 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7358 else
7360 int end = i + 4;
7361 for (; i < end; ++i)
7362 AREF (vector, i) = Qnil;
7365 xassert (i == ASIZE (vector));
7366 return vector;
7370 /* Restore global state from VECTOR which was created by
7371 with_echo_area_buffer_unwind_data. */
7373 static Lisp_Object
7374 unwind_with_echo_area_buffer (vector)
7375 Lisp_Object vector;
7377 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7378 Vdeactivate_mark = AREF (vector, 1);
7379 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7381 if (WINDOWP (AREF (vector, 3)))
7383 struct window *w;
7384 Lisp_Object buffer, charpos, bytepos;
7386 w = XWINDOW (AREF (vector, 3));
7387 buffer = AREF (vector, 4);
7388 charpos = AREF (vector, 5);
7389 bytepos = AREF (vector, 6);
7391 w->buffer = buffer;
7392 set_marker_both (w->pointm, buffer,
7393 XFASTINT (charpos), XFASTINT (bytepos));
7396 Vwith_echo_area_save_vector = vector;
7397 return Qnil;
7401 /* Set up the echo area for use by print functions. MULTIBYTE_P
7402 non-zero means we will print multibyte. */
7404 void
7405 setup_echo_area_for_printing (multibyte_p)
7406 int multibyte_p;
7408 /* If we can't find an echo area any more, exit. */
7409 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7410 Fkill_emacs (Qnil);
7412 ensure_echo_area_buffers ();
7414 if (!message_buf_print)
7416 /* A message has been output since the last time we printed.
7417 Choose a fresh echo area buffer. */
7418 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7419 echo_area_buffer[0] = echo_buffer[1];
7420 else
7421 echo_area_buffer[0] = echo_buffer[0];
7423 /* Switch to that buffer and clear it. */
7424 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7425 current_buffer->truncate_lines = Qnil;
7427 if (Z > BEG)
7429 int count = SPECPDL_INDEX ();
7430 specbind (Qinhibit_read_only, Qt);
7431 /* Note that undo recording is always disabled. */
7432 del_range (BEG, Z);
7433 unbind_to (count, Qnil);
7435 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7437 /* Set up the buffer for the multibyteness we need. */
7438 if (multibyte_p
7439 != !NILP (current_buffer->enable_multibyte_characters))
7440 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7442 /* Raise the frame containing the echo area. */
7443 if (minibuffer_auto_raise)
7445 struct frame *sf = SELECTED_FRAME ();
7446 Lisp_Object mini_window;
7447 mini_window = FRAME_MINIBUF_WINDOW (sf);
7448 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7451 message_log_maybe_newline ();
7452 message_buf_print = 1;
7454 else
7456 if (NILP (echo_area_buffer[0]))
7458 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7459 echo_area_buffer[0] = echo_buffer[1];
7460 else
7461 echo_area_buffer[0] = echo_buffer[0];
7464 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7466 /* Someone switched buffers between print requests. */
7467 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7468 current_buffer->truncate_lines = Qnil;
7474 /* Display an echo area message in window W. Value is non-zero if W's
7475 height is changed. If display_last_displayed_message_p is
7476 non-zero, display the message that was last displayed, otherwise
7477 display the current message. */
7479 static int
7480 display_echo_area (w)
7481 struct window *w;
7483 int i, no_message_p, window_height_changed_p, count;
7485 /* Temporarily disable garbage collections while displaying the echo
7486 area. This is done because a GC can print a message itself.
7487 That message would modify the echo area buffer's contents while a
7488 redisplay of the buffer is going on, and seriously confuse
7489 redisplay. */
7490 count = inhibit_garbage_collection ();
7492 /* If there is no message, we must call display_echo_area_1
7493 nevertheless because it resizes the window. But we will have to
7494 reset the echo_area_buffer in question to nil at the end because
7495 with_echo_area_buffer will sets it to an empty buffer. */
7496 i = display_last_displayed_message_p ? 1 : 0;
7497 no_message_p = NILP (echo_area_buffer[i]);
7499 window_height_changed_p
7500 = with_echo_area_buffer (w, display_last_displayed_message_p,
7501 display_echo_area_1,
7502 (EMACS_INT) w, Qnil, 0, 0);
7504 if (no_message_p)
7505 echo_area_buffer[i] = Qnil;
7507 unbind_to (count, Qnil);
7508 return window_height_changed_p;
7512 /* Helper for display_echo_area. Display the current buffer which
7513 contains the current echo area message in window W, a mini-window,
7514 a pointer to which is passed in A1. A2..A4 are currently not used.
7515 Change the height of W so that all of the message is displayed.
7516 Value is non-zero if height of W was changed. */
7518 static int
7519 display_echo_area_1 (a1, a2, a3, a4)
7520 EMACS_INT a1;
7521 Lisp_Object a2;
7522 EMACS_INT a3, a4;
7524 struct window *w = (struct window *) a1;
7525 Lisp_Object window;
7526 struct text_pos start;
7527 int window_height_changed_p = 0;
7529 /* Do this before displaying, so that we have a large enough glyph
7530 matrix for the display. */
7531 window_height_changed_p = resize_mini_window (w, 0);
7533 /* Display. */
7534 clear_glyph_matrix (w->desired_matrix);
7535 XSETWINDOW (window, w);
7536 SET_TEXT_POS (start, BEG, BEG_BYTE);
7537 try_window (window, start);
7539 return window_height_changed_p;
7543 /* Resize the echo area window to exactly the size needed for the
7544 currently displayed message, if there is one. If a mini-buffer
7545 is active, don't shrink it. */
7547 void
7548 resize_echo_area_exactly ()
7550 if (BUFFERP (echo_area_buffer[0])
7551 && WINDOWP (echo_area_window))
7553 struct window *w = XWINDOW (echo_area_window);
7554 int resized_p;
7555 Lisp_Object resize_exactly;
7557 if (minibuf_level == 0)
7558 resize_exactly = Qt;
7559 else
7560 resize_exactly = Qnil;
7562 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7563 (EMACS_INT) w, resize_exactly, 0, 0);
7564 if (resized_p)
7566 ++windows_or_buffers_changed;
7567 ++update_mode_lines;
7568 redisplay_internal (0);
7574 /* Callback function for with_echo_area_buffer, when used from
7575 resize_echo_area_exactly. A1 contains a pointer to the window to
7576 resize, EXACTLY non-nil means resize the mini-window exactly to the
7577 size of the text displayed. A3 and A4 are not used. Value is what
7578 resize_mini_window returns. */
7580 static int
7581 resize_mini_window_1 (a1, exactly, a3, a4)
7582 EMACS_INT a1;
7583 Lisp_Object exactly;
7584 EMACS_INT a3, a4;
7586 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7590 /* Resize mini-window W to fit the size of its contents. EXACT:P
7591 means size the window exactly to the size needed. Otherwise, it's
7592 only enlarged until W's buffer is empty. Value is non-zero if
7593 the window height has been changed. */
7596 resize_mini_window (w, exact_p)
7597 struct window *w;
7598 int exact_p;
7600 struct frame *f = XFRAME (w->frame);
7601 int window_height_changed_p = 0;
7603 xassert (MINI_WINDOW_P (w));
7605 /* Don't resize windows while redisplaying a window; it would
7606 confuse redisplay functions when the size of the window they are
7607 displaying changes from under them. Such a resizing can happen,
7608 for instance, when which-func prints a long message while
7609 we are running fontification-functions. We're running these
7610 functions with safe_call which binds inhibit-redisplay to t. */
7611 if (!NILP (Vinhibit_redisplay))
7612 return 0;
7614 /* Nil means don't try to resize. */
7615 if (NILP (Vresize_mini_windows)
7616 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7617 return 0;
7619 if (!FRAME_MINIBUF_ONLY_P (f))
7621 struct it it;
7622 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7623 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7624 int height, max_height;
7625 int unit = FRAME_LINE_HEIGHT (f);
7626 struct text_pos start;
7627 struct buffer *old_current_buffer = NULL;
7629 if (current_buffer != XBUFFER (w->buffer))
7631 old_current_buffer = current_buffer;
7632 set_buffer_internal (XBUFFER (w->buffer));
7635 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7637 /* Compute the max. number of lines specified by the user. */
7638 if (FLOATP (Vmax_mini_window_height))
7639 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7640 else if (INTEGERP (Vmax_mini_window_height))
7641 max_height = XINT (Vmax_mini_window_height);
7642 else
7643 max_height = total_height / 4;
7645 /* Correct that max. height if it's bogus. */
7646 max_height = max (1, max_height);
7647 max_height = min (total_height, max_height);
7649 /* Find out the height of the text in the window. */
7650 if (it.truncate_lines_p)
7651 height = 1;
7652 else
7654 last_height = 0;
7655 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7656 if (it.max_ascent == 0 && it.max_descent == 0)
7657 height = it.current_y + last_height;
7658 else
7659 height = it.current_y + it.max_ascent + it.max_descent;
7660 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7661 height = (height + unit - 1) / unit;
7664 /* Compute a suitable window start. */
7665 if (height > max_height)
7667 height = max_height;
7668 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7669 move_it_vertically_backward (&it, (height - 1) * unit);
7670 start = it.current.pos;
7672 else
7673 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7674 SET_MARKER_FROM_TEXT_POS (w->start, start);
7676 if (EQ (Vresize_mini_windows, Qgrow_only))
7678 /* Let it grow only, until we display an empty message, in which
7679 case the window shrinks again. */
7680 if (height > WINDOW_TOTAL_LINES (w))
7682 int old_height = WINDOW_TOTAL_LINES (w);
7683 freeze_window_starts (f, 1);
7684 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7685 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7687 else if (height < WINDOW_TOTAL_LINES (w)
7688 && (exact_p || BEGV == ZV))
7690 int old_height = WINDOW_TOTAL_LINES (w);
7691 freeze_window_starts (f, 0);
7692 shrink_mini_window (w);
7693 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7696 else
7698 /* Always resize to exact size needed. */
7699 if (height > WINDOW_TOTAL_LINES (w))
7701 int old_height = WINDOW_TOTAL_LINES (w);
7702 freeze_window_starts (f, 1);
7703 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7704 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7706 else if (height < WINDOW_TOTAL_LINES (w))
7708 int old_height = WINDOW_TOTAL_LINES (w);
7709 freeze_window_starts (f, 0);
7710 shrink_mini_window (w);
7712 if (height)
7714 freeze_window_starts (f, 1);
7715 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7718 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7722 if (old_current_buffer)
7723 set_buffer_internal (old_current_buffer);
7726 return window_height_changed_p;
7730 /* Value is the current message, a string, or nil if there is no
7731 current message. */
7733 Lisp_Object
7734 current_message ()
7736 Lisp_Object msg;
7738 if (NILP (echo_area_buffer[0]))
7739 msg = Qnil;
7740 else
7742 with_echo_area_buffer (0, 0, current_message_1,
7743 (EMACS_INT) &msg, Qnil, 0, 0);
7744 if (NILP (msg))
7745 echo_area_buffer[0] = Qnil;
7748 return msg;
7752 static int
7753 current_message_1 (a1, a2, a3, a4)
7754 EMACS_INT a1;
7755 Lisp_Object a2;
7756 EMACS_INT a3, a4;
7758 Lisp_Object *msg = (Lisp_Object *) a1;
7760 if (Z > BEG)
7761 *msg = make_buffer_string (BEG, Z, 1);
7762 else
7763 *msg = Qnil;
7764 return 0;
7768 /* Push the current message on Vmessage_stack for later restauration
7769 by restore_message. Value is non-zero if the current message isn't
7770 empty. This is a relatively infrequent operation, so it's not
7771 worth optimizing. */
7774 push_message ()
7776 Lisp_Object msg;
7777 msg = current_message ();
7778 Vmessage_stack = Fcons (msg, Vmessage_stack);
7779 return STRINGP (msg);
7783 /* Restore message display from the top of Vmessage_stack. */
7785 void
7786 restore_message ()
7788 Lisp_Object msg;
7790 xassert (CONSP (Vmessage_stack));
7791 msg = XCAR (Vmessage_stack);
7792 if (STRINGP (msg))
7793 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7794 else
7795 message3_nolog (msg, 0, 0);
7799 /* Handler for record_unwind_protect calling pop_message. */
7801 Lisp_Object
7802 pop_message_unwind (dummy)
7803 Lisp_Object dummy;
7805 pop_message ();
7806 return Qnil;
7809 /* Pop the top-most entry off Vmessage_stack. */
7811 void
7812 pop_message ()
7814 xassert (CONSP (Vmessage_stack));
7815 Vmessage_stack = XCDR (Vmessage_stack);
7819 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7820 exits. If the stack is not empty, we have a missing pop_message
7821 somewhere. */
7823 void
7824 check_message_stack ()
7826 if (!NILP (Vmessage_stack))
7827 abort ();
7831 /* Truncate to NCHARS what will be displayed in the echo area the next
7832 time we display it---but don't redisplay it now. */
7834 void
7835 truncate_echo_area (nchars)
7836 int nchars;
7838 if (nchars == 0)
7839 echo_area_buffer[0] = Qnil;
7840 /* A null message buffer means that the frame hasn't really been
7841 initialized yet. Error messages get reported properly by
7842 cmd_error, so this must be just an informative message; toss it. */
7843 else if (!noninteractive
7844 && INTERACTIVE
7845 && !NILP (echo_area_buffer[0]))
7847 struct frame *sf = SELECTED_FRAME ();
7848 if (FRAME_MESSAGE_BUF (sf))
7849 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7854 /* Helper function for truncate_echo_area. Truncate the current
7855 message to at most NCHARS characters. */
7857 static int
7858 truncate_message_1 (nchars, a2, a3, a4)
7859 EMACS_INT nchars;
7860 Lisp_Object a2;
7861 EMACS_INT a3, a4;
7863 if (BEG + nchars < Z)
7864 del_range (BEG + nchars, Z);
7865 if (Z == BEG)
7866 echo_area_buffer[0] = Qnil;
7867 return 0;
7871 /* Set the current message to a substring of S or STRING.
7873 If STRING is a Lisp string, set the message to the first NBYTES
7874 bytes from STRING. NBYTES zero means use the whole string. If
7875 STRING is multibyte, the message will be displayed multibyte.
7877 If S is not null, set the message to the first LEN bytes of S. LEN
7878 zero means use the whole string. MULTIBYTE_P non-zero means S is
7879 multibyte. Display the message multibyte in that case. */
7881 void
7882 set_message (s, string, nbytes, multibyte_p)
7883 const char *s;
7884 Lisp_Object string;
7885 int nbytes, multibyte_p;
7887 message_enable_multibyte
7888 = ((s && multibyte_p)
7889 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7891 with_echo_area_buffer (0, 0, set_message_1,
7892 (EMACS_INT) s, string, nbytes, multibyte_p);
7893 message_buf_print = 0;
7894 help_echo_showing_p = 0;
7898 /* Helper function for set_message. Arguments have the same meaning
7899 as there, with A1 corresponding to S and A2 corresponding to STRING
7900 This function is called with the echo area buffer being
7901 current. */
7903 static int
7904 set_message_1 (a1, a2, nbytes, multibyte_p)
7905 EMACS_INT a1;
7906 Lisp_Object a2;
7907 EMACS_INT nbytes, multibyte_p;
7909 const char *s = (const char *) a1;
7910 Lisp_Object string = a2;
7912 /* Change multibyteness of the echo buffer appropriately. */
7913 if (message_enable_multibyte
7914 != !NILP (current_buffer->enable_multibyte_characters))
7915 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7917 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7919 /* Insert new message at BEG. */
7920 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7921 Ferase_buffer ();
7923 if (STRINGP (string))
7925 int nchars;
7927 if (nbytes == 0)
7928 nbytes = SBYTES (string);
7929 nchars = string_byte_to_char (string, nbytes);
7931 /* This function takes care of single/multibyte conversion. We
7932 just have to ensure that the echo area buffer has the right
7933 setting of enable_multibyte_characters. */
7934 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7936 else if (s)
7938 if (nbytes == 0)
7939 nbytes = strlen (s);
7941 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7943 /* Convert from multi-byte to single-byte. */
7944 int i, c, n;
7945 unsigned char work[1];
7947 /* Convert a multibyte string to single-byte. */
7948 for (i = 0; i < nbytes; i += n)
7950 c = string_char_and_length (s + i, nbytes - i, &n);
7951 work[0] = (SINGLE_BYTE_CHAR_P (c)
7953 : multibyte_char_to_unibyte (c, Qnil));
7954 insert_1_both (work, 1, 1, 1, 0, 0);
7957 else if (!multibyte_p
7958 && !NILP (current_buffer->enable_multibyte_characters))
7960 /* Convert from single-byte to multi-byte. */
7961 int i, c, n;
7962 const unsigned char *msg = (const unsigned char *) s;
7963 unsigned char str[MAX_MULTIBYTE_LENGTH];
7965 /* Convert a single-byte string to multibyte. */
7966 for (i = 0; i < nbytes; i++)
7968 c = unibyte_char_to_multibyte (msg[i]);
7969 n = CHAR_STRING (c, str);
7970 insert_1_both (str, 1, n, 1, 0, 0);
7973 else
7974 insert_1 (s, nbytes, 1, 0, 0);
7977 return 0;
7981 /* Clear messages. CURRENT_P non-zero means clear the current
7982 message. LAST_DISPLAYED_P non-zero means clear the message
7983 last displayed. */
7985 void
7986 clear_message (current_p, last_displayed_p)
7987 int current_p, last_displayed_p;
7989 if (current_p)
7991 echo_area_buffer[0] = Qnil;
7992 message_cleared_p = 1;
7995 if (last_displayed_p)
7996 echo_area_buffer[1] = Qnil;
7998 message_buf_print = 0;
8001 /* Clear garbaged frames.
8003 This function is used where the old redisplay called
8004 redraw_garbaged_frames which in turn called redraw_frame which in
8005 turn called clear_frame. The call to clear_frame was a source of
8006 flickering. I believe a clear_frame is not necessary. It should
8007 suffice in the new redisplay to invalidate all current matrices,
8008 and ensure a complete redisplay of all windows. */
8010 static void
8011 clear_garbaged_frames ()
8013 if (frame_garbaged)
8015 Lisp_Object tail, frame;
8016 int changed_count = 0;
8018 FOR_EACH_FRAME (tail, frame)
8020 struct frame *f = XFRAME (frame);
8022 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8024 if (f->resized_p)
8026 Fredraw_frame (frame);
8027 f->force_flush_display_p = 1;
8029 clear_current_matrices (f);
8030 changed_count++;
8031 f->garbaged = 0;
8032 f->resized_p = 0;
8036 frame_garbaged = 0;
8037 if (changed_count)
8038 ++windows_or_buffers_changed;
8043 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8044 is non-zero update selected_frame. Value is non-zero if the
8045 mini-windows height has been changed. */
8047 static int
8048 echo_area_display (update_frame_p)
8049 int update_frame_p;
8051 Lisp_Object mini_window;
8052 struct window *w;
8053 struct frame *f;
8054 int window_height_changed_p = 0;
8055 struct frame *sf = SELECTED_FRAME ();
8057 mini_window = FRAME_MINIBUF_WINDOW (sf);
8058 w = XWINDOW (mini_window);
8059 f = XFRAME (WINDOW_FRAME (w));
8061 /* Don't display if frame is invisible or not yet initialized. */
8062 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8063 return 0;
8065 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8066 #ifndef MAC_OS8
8067 #ifdef HAVE_WINDOW_SYSTEM
8068 /* When Emacs starts, selected_frame may be a visible terminal
8069 frame, even if we run under a window system. If we let this
8070 through, a message would be displayed on the terminal. */
8071 if (EQ (selected_frame, Vterminal_frame)
8072 && !NILP (Vwindow_system))
8073 return 0;
8074 #endif /* HAVE_WINDOW_SYSTEM */
8075 #endif
8077 /* Redraw garbaged frames. */
8078 if (frame_garbaged)
8079 clear_garbaged_frames ();
8081 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8083 echo_area_window = mini_window;
8084 window_height_changed_p = display_echo_area (w);
8085 w->must_be_updated_p = 1;
8087 /* Update the display, unless called from redisplay_internal.
8088 Also don't update the screen during redisplay itself. The
8089 update will happen at the end of redisplay, and an update
8090 here could cause confusion. */
8091 if (update_frame_p && !redisplaying_p)
8093 int n = 0;
8095 /* If the display update has been interrupted by pending
8096 input, update mode lines in the frame. Due to the
8097 pending input, it might have been that redisplay hasn't
8098 been called, so that mode lines above the echo area are
8099 garbaged. This looks odd, so we prevent it here. */
8100 if (!display_completed)
8101 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8103 if (window_height_changed_p
8104 /* Don't do this if Emacs is shutting down. Redisplay
8105 needs to run hooks. */
8106 && !NILP (Vrun_hooks))
8108 /* Must update other windows. Likewise as in other
8109 cases, don't let this update be interrupted by
8110 pending input. */
8111 int count = SPECPDL_INDEX ();
8112 specbind (Qredisplay_dont_pause, Qt);
8113 windows_or_buffers_changed = 1;
8114 redisplay_internal (0);
8115 unbind_to (count, Qnil);
8117 else if (FRAME_WINDOW_P (f) && n == 0)
8119 /* Window configuration is the same as before.
8120 Can do with a display update of the echo area,
8121 unless we displayed some mode lines. */
8122 update_single_window (w, 1);
8123 rif->flush_display (f);
8125 else
8126 update_frame (f, 1, 1);
8128 /* If cursor is in the echo area, make sure that the next
8129 redisplay displays the minibuffer, so that the cursor will
8130 be replaced with what the minibuffer wants. */
8131 if (cursor_in_echo_area)
8132 ++windows_or_buffers_changed;
8135 else if (!EQ (mini_window, selected_window))
8136 windows_or_buffers_changed++;
8138 /* The current message is now also the last one displayed. */
8139 echo_area_buffer[1] = echo_area_buffer[0];
8141 /* Prevent redisplay optimization in redisplay_internal by resetting
8142 this_line_start_pos. This is done because the mini-buffer now
8143 displays the message instead of its buffer text. */
8144 if (EQ (mini_window, selected_window))
8145 CHARPOS (this_line_start_pos) = 0;
8147 return window_height_changed_p;
8152 /***********************************************************************
8153 Frame Titles
8154 ***********************************************************************/
8157 /* The frame title buffering code is also used by Fformat_mode_line.
8158 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8160 /* A buffer for constructing frame titles in it; allocated from the
8161 heap in init_xdisp and resized as needed in store_frame_title_char. */
8163 static char *frame_title_buf;
8165 /* The buffer's end, and a current output position in it. */
8167 static char *frame_title_buf_end;
8168 static char *frame_title_ptr;
8171 /* Store a single character C for the frame title in frame_title_buf.
8172 Re-allocate frame_title_buf if necessary. */
8174 static void
8175 #ifdef PROTOTYPES
8176 store_frame_title_char (char c)
8177 #else
8178 store_frame_title_char (c)
8179 char c;
8180 #endif
8182 /* If output position has reached the end of the allocated buffer,
8183 double the buffer's size. */
8184 if (frame_title_ptr == frame_title_buf_end)
8186 int len = frame_title_ptr - frame_title_buf;
8187 int new_size = 2 * len * sizeof *frame_title_buf;
8188 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8189 frame_title_buf_end = frame_title_buf + new_size;
8190 frame_title_ptr = frame_title_buf + len;
8193 *frame_title_ptr++ = c;
8197 /* Store part of a frame title in frame_title_buf, beginning at
8198 frame_title_ptr. STR is the string to store. Do not copy
8199 characters that yield more columns than PRECISION; PRECISION <= 0
8200 means copy the whole string. Pad with spaces until FIELD_WIDTH
8201 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8202 pad. Called from display_mode_element when it is used to build a
8203 frame title. */
8205 static int
8206 store_frame_title (str, field_width, precision)
8207 const unsigned char *str;
8208 int field_width, precision;
8210 int n = 0;
8211 int dummy, nbytes;
8213 /* Copy at most PRECISION chars from STR. */
8214 nbytes = strlen (str);
8215 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8216 while (nbytes--)
8217 store_frame_title_char (*str++);
8219 /* Fill up with spaces until FIELD_WIDTH reached. */
8220 while (field_width > 0
8221 && n < field_width)
8223 store_frame_title_char (' ');
8224 ++n;
8227 return n;
8230 #ifdef HAVE_WINDOW_SYSTEM
8232 /* Set the title of FRAME, if it has changed. The title format is
8233 Vicon_title_format if FRAME is iconified, otherwise it is
8234 frame_title_format. */
8236 static void
8237 x_consider_frame_title (frame)
8238 Lisp_Object frame;
8240 struct frame *f = XFRAME (frame);
8242 if (FRAME_WINDOW_P (f)
8243 || FRAME_MINIBUF_ONLY_P (f)
8244 || f->explicit_name)
8246 /* Do we have more than one visible frame on this X display? */
8247 Lisp_Object tail;
8248 Lisp_Object fmt;
8249 struct buffer *obuf;
8250 int len;
8251 struct it it;
8253 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8255 Lisp_Object other_frame = XCAR (tail);
8256 struct frame *tf = XFRAME (other_frame);
8258 if (tf != f
8259 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8260 && !FRAME_MINIBUF_ONLY_P (tf)
8261 && !EQ (other_frame, tip_frame)
8262 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8263 break;
8266 /* Set global variable indicating that multiple frames exist. */
8267 multiple_frames = CONSP (tail);
8269 /* Switch to the buffer of selected window of the frame. Set up
8270 frame_title_ptr so that display_mode_element will output into it;
8271 then display the title. */
8272 obuf = current_buffer;
8273 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8274 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8275 frame_title_ptr = frame_title_buf;
8276 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8277 NULL, DEFAULT_FACE_ID);
8278 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8279 len = frame_title_ptr - frame_title_buf;
8280 frame_title_ptr = NULL;
8281 set_buffer_internal_1 (obuf);
8283 /* Set the title only if it's changed. This avoids consing in
8284 the common case where it hasn't. (If it turns out that we've
8285 already wasted too much time by walking through the list with
8286 display_mode_element, then we might need to optimize at a
8287 higher level than this.) */
8288 if (! STRINGP (f->name)
8289 || SBYTES (f->name) != len
8290 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8291 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8295 #endif /* not HAVE_WINDOW_SYSTEM */
8300 /***********************************************************************
8301 Menu Bars
8302 ***********************************************************************/
8305 /* Prepare for redisplay by updating menu-bar item lists when
8306 appropriate. This can call eval. */
8308 void
8309 prepare_menu_bars ()
8311 int all_windows;
8312 struct gcpro gcpro1, gcpro2;
8313 struct frame *f;
8314 Lisp_Object tooltip_frame;
8316 #ifdef HAVE_WINDOW_SYSTEM
8317 tooltip_frame = tip_frame;
8318 #else
8319 tooltip_frame = Qnil;
8320 #endif
8322 /* Update all frame titles based on their buffer names, etc. We do
8323 this before the menu bars so that the buffer-menu will show the
8324 up-to-date frame titles. */
8325 #ifdef HAVE_WINDOW_SYSTEM
8326 if (windows_or_buffers_changed || update_mode_lines)
8328 Lisp_Object tail, frame;
8330 FOR_EACH_FRAME (tail, frame)
8332 f = XFRAME (frame);
8333 if (!EQ (frame, tooltip_frame)
8334 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8335 x_consider_frame_title (frame);
8338 #endif /* HAVE_WINDOW_SYSTEM */
8340 /* Update the menu bar item lists, if appropriate. This has to be
8341 done before any actual redisplay or generation of display lines. */
8342 all_windows = (update_mode_lines
8343 || buffer_shared > 1
8344 || windows_or_buffers_changed);
8345 if (all_windows)
8347 Lisp_Object tail, frame;
8348 int count = SPECPDL_INDEX ();
8350 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8352 FOR_EACH_FRAME (tail, frame)
8354 f = XFRAME (frame);
8356 /* Ignore tooltip frame. */
8357 if (EQ (frame, tooltip_frame))
8358 continue;
8360 /* If a window on this frame changed size, report that to
8361 the user and clear the size-change flag. */
8362 if (FRAME_WINDOW_SIZES_CHANGED (f))
8364 Lisp_Object functions;
8366 /* Clear flag first in case we get an error below. */
8367 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8368 functions = Vwindow_size_change_functions;
8369 GCPRO2 (tail, functions);
8371 while (CONSP (functions))
8373 call1 (XCAR (functions), frame);
8374 functions = XCDR (functions);
8376 UNGCPRO;
8379 GCPRO1 (tail);
8380 update_menu_bar (f, 0);
8381 #ifdef HAVE_WINDOW_SYSTEM
8382 update_tool_bar (f, 0);
8383 #endif
8384 UNGCPRO;
8387 unbind_to (count, Qnil);
8389 else
8391 struct frame *sf = SELECTED_FRAME ();
8392 update_menu_bar (sf, 1);
8393 #ifdef HAVE_WINDOW_SYSTEM
8394 update_tool_bar (sf, 1);
8395 #endif
8398 /* Motif needs this. See comment in xmenu.c. Turn it off when
8399 pending_menu_activation is not defined. */
8400 #ifdef USE_X_TOOLKIT
8401 pending_menu_activation = 0;
8402 #endif
8406 /* Update the menu bar item list for frame F. This has to be done
8407 before we start to fill in any display lines, because it can call
8408 eval.
8410 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8412 static void
8413 update_menu_bar (f, save_match_data)
8414 struct frame *f;
8415 int save_match_data;
8417 Lisp_Object window;
8418 register struct window *w;
8420 /* If called recursively during a menu update, do nothing. This can
8421 happen when, for instance, an activate-menubar-hook causes a
8422 redisplay. */
8423 if (inhibit_menubar_update)
8424 return;
8426 window = FRAME_SELECTED_WINDOW (f);
8427 w = XWINDOW (window);
8429 #if 0 /* The if statement below this if statement used to include the
8430 condition !NILP (w->update_mode_line), rather than using
8431 update_mode_lines directly, and this if statement may have
8432 been added to make that condition work. Now the if
8433 statement below matches its comment, this isn't needed. */
8434 if (update_mode_lines)
8435 w->update_mode_line = Qt;
8436 #endif
8438 if (FRAME_WINDOW_P (f)
8440 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8441 || defined (USE_GTK)
8442 FRAME_EXTERNAL_MENU_BAR (f)
8443 #else
8444 FRAME_MENU_BAR_LINES (f) > 0
8445 #endif
8446 : FRAME_MENU_BAR_LINES (f) > 0)
8448 /* If the user has switched buffers or windows, we need to
8449 recompute to reflect the new bindings. But we'll
8450 recompute when update_mode_lines is set too; that means
8451 that people can use force-mode-line-update to request
8452 that the menu bar be recomputed. The adverse effect on
8453 the rest of the redisplay algorithm is about the same as
8454 windows_or_buffers_changed anyway. */
8455 if (windows_or_buffers_changed
8456 /* This used to test w->update_mode_line, but we believe
8457 there is no need to recompute the menu in that case. */
8458 || update_mode_lines
8459 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8460 < BUF_MODIFF (XBUFFER (w->buffer)))
8461 != !NILP (w->last_had_star))
8462 || ((!NILP (Vtransient_mark_mode)
8463 && !NILP (XBUFFER (w->buffer)->mark_active))
8464 != !NILP (w->region_showing)))
8466 struct buffer *prev = current_buffer;
8467 int count = SPECPDL_INDEX ();
8469 specbind (Qinhibit_menubar_update, Qt);
8471 set_buffer_internal_1 (XBUFFER (w->buffer));
8472 if (save_match_data)
8473 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8474 if (NILP (Voverriding_local_map_menu_flag))
8476 specbind (Qoverriding_terminal_local_map, Qnil);
8477 specbind (Qoverriding_local_map, Qnil);
8480 /* Run the Lucid hook. */
8481 safe_run_hooks (Qactivate_menubar_hook);
8483 /* If it has changed current-menubar from previous value,
8484 really recompute the menu-bar from the value. */
8485 if (! NILP (Vlucid_menu_bar_dirty_flag))
8486 call0 (Qrecompute_lucid_menubar);
8488 safe_run_hooks (Qmenu_bar_update_hook);
8489 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8491 /* Redisplay the menu bar in case we changed it. */
8492 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8493 || defined (USE_GTK)
8494 if (FRAME_WINDOW_P (f)
8495 #if defined (MAC_OS)
8496 /* All frames on Mac OS share the same menubar. So only the
8497 selected frame should be allowed to set it. */
8498 && f == SELECTED_FRAME ()
8499 #endif
8501 set_frame_menubar (f, 0, 0);
8502 else
8503 /* On a terminal screen, the menu bar is an ordinary screen
8504 line, and this makes it get updated. */
8505 w->update_mode_line = Qt;
8506 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8507 /* In the non-toolkit version, the menu bar is an ordinary screen
8508 line, and this makes it get updated. */
8509 w->update_mode_line = Qt;
8510 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8512 unbind_to (count, Qnil);
8513 set_buffer_internal_1 (prev);
8520 /***********************************************************************
8521 Output Cursor
8522 ***********************************************************************/
8524 #ifdef HAVE_WINDOW_SYSTEM
8526 /* EXPORT:
8527 Nominal cursor position -- where to draw output.
8528 HPOS and VPOS are window relative glyph matrix coordinates.
8529 X and Y are window relative pixel coordinates. */
8531 struct cursor_pos output_cursor;
8534 /* EXPORT:
8535 Set the global variable output_cursor to CURSOR. All cursor
8536 positions are relative to updated_window. */
8538 void
8539 set_output_cursor (cursor)
8540 struct cursor_pos *cursor;
8542 output_cursor.hpos = cursor->hpos;
8543 output_cursor.vpos = cursor->vpos;
8544 output_cursor.x = cursor->x;
8545 output_cursor.y = cursor->y;
8549 /* EXPORT for RIF:
8550 Set a nominal cursor position.
8552 HPOS and VPOS are column/row positions in a window glyph matrix. X
8553 and Y are window text area relative pixel positions.
8555 If this is done during an update, updated_window will contain the
8556 window that is being updated and the position is the future output
8557 cursor position for that window. If updated_window is null, use
8558 selected_window and display the cursor at the given position. */
8560 void
8561 x_cursor_to (vpos, hpos, y, x)
8562 int vpos, hpos, y, x;
8564 struct window *w;
8566 /* If updated_window is not set, work on selected_window. */
8567 if (updated_window)
8568 w = updated_window;
8569 else
8570 w = XWINDOW (selected_window);
8572 /* Set the output cursor. */
8573 output_cursor.hpos = hpos;
8574 output_cursor.vpos = vpos;
8575 output_cursor.x = x;
8576 output_cursor.y = y;
8578 /* If not called as part of an update, really display the cursor.
8579 This will also set the cursor position of W. */
8580 if (updated_window == NULL)
8582 BLOCK_INPUT;
8583 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8584 if (rif->flush_display_optional)
8585 rif->flush_display_optional (SELECTED_FRAME ());
8586 UNBLOCK_INPUT;
8590 #endif /* HAVE_WINDOW_SYSTEM */
8593 /***********************************************************************
8594 Tool-bars
8595 ***********************************************************************/
8597 #ifdef HAVE_WINDOW_SYSTEM
8599 /* Where the mouse was last time we reported a mouse event. */
8601 FRAME_PTR last_mouse_frame;
8603 /* Tool-bar item index of the item on which a mouse button was pressed
8604 or -1. */
8606 int last_tool_bar_item;
8609 /* Update the tool-bar item list for frame F. This has to be done
8610 before we start to fill in any display lines. Called from
8611 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8612 and restore it here. */
8614 static void
8615 update_tool_bar (f, save_match_data)
8616 struct frame *f;
8617 int save_match_data;
8619 #ifdef USE_GTK
8620 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8621 #else
8622 int do_update = WINDOWP (f->tool_bar_window)
8623 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8624 #endif
8626 if (do_update)
8628 Lisp_Object window;
8629 struct window *w;
8631 window = FRAME_SELECTED_WINDOW (f);
8632 w = XWINDOW (window);
8634 /* If the user has switched buffers or windows, we need to
8635 recompute to reflect the new bindings. But we'll
8636 recompute when update_mode_lines is set too; that means
8637 that people can use force-mode-line-update to request
8638 that the menu bar be recomputed. The adverse effect on
8639 the rest of the redisplay algorithm is about the same as
8640 windows_or_buffers_changed anyway. */
8641 if (windows_or_buffers_changed
8642 || !NILP (w->update_mode_line)
8643 || update_mode_lines
8644 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8645 < BUF_MODIFF (XBUFFER (w->buffer)))
8646 != !NILP (w->last_had_star))
8647 || ((!NILP (Vtransient_mark_mode)
8648 && !NILP (XBUFFER (w->buffer)->mark_active))
8649 != !NILP (w->region_showing)))
8651 struct buffer *prev = current_buffer;
8652 int count = SPECPDL_INDEX ();
8653 Lisp_Object new_tool_bar;
8654 int new_n_tool_bar;
8655 struct gcpro gcpro1;
8657 /* Set current_buffer to the buffer of the selected
8658 window of the frame, so that we get the right local
8659 keymaps. */
8660 set_buffer_internal_1 (XBUFFER (w->buffer));
8662 /* Save match data, if we must. */
8663 if (save_match_data)
8664 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8666 /* Make sure that we don't accidentally use bogus keymaps. */
8667 if (NILP (Voverriding_local_map_menu_flag))
8669 specbind (Qoverriding_terminal_local_map, Qnil);
8670 specbind (Qoverriding_local_map, Qnil);
8673 GCPRO1 (new_tool_bar);
8675 /* Build desired tool-bar items from keymaps. */
8676 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8677 &new_n_tool_bar);
8679 /* Redisplay the tool-bar if we changed it. */
8680 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8682 /* Redisplay that happens asynchronously due to an expose event
8683 may access f->tool_bar_items. Make sure we update both
8684 variables within BLOCK_INPUT so no such event interrupts. */
8685 BLOCK_INPUT;
8686 f->tool_bar_items = new_tool_bar;
8687 f->n_tool_bar_items = new_n_tool_bar;
8688 w->update_mode_line = Qt;
8689 UNBLOCK_INPUT;
8692 UNGCPRO;
8694 unbind_to (count, Qnil);
8695 set_buffer_internal_1 (prev);
8701 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8702 F's desired tool-bar contents. F->tool_bar_items must have
8703 been set up previously by calling prepare_menu_bars. */
8705 static void
8706 build_desired_tool_bar_string (f)
8707 struct frame *f;
8709 int i, size, size_needed;
8710 struct gcpro gcpro1, gcpro2, gcpro3;
8711 Lisp_Object image, plist, props;
8713 image = plist = props = Qnil;
8714 GCPRO3 (image, plist, props);
8716 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8717 Otherwise, make a new string. */
8719 /* The size of the string we might be able to reuse. */
8720 size = (STRINGP (f->desired_tool_bar_string)
8721 ? SCHARS (f->desired_tool_bar_string)
8722 : 0);
8724 /* We need one space in the string for each image. */
8725 size_needed = f->n_tool_bar_items;
8727 /* Reuse f->desired_tool_bar_string, if possible. */
8728 if (size < size_needed || NILP (f->desired_tool_bar_string))
8729 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8730 make_number (' '));
8731 else
8733 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8734 Fremove_text_properties (make_number (0), make_number (size),
8735 props, f->desired_tool_bar_string);
8738 /* Put a `display' property on the string for the images to display,
8739 put a `menu_item' property on tool-bar items with a value that
8740 is the index of the item in F's tool-bar item vector. */
8741 for (i = 0; i < f->n_tool_bar_items; ++i)
8743 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8745 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8746 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8747 int hmargin, vmargin, relief, idx, end;
8748 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8750 /* If image is a vector, choose the image according to the
8751 button state. */
8752 image = PROP (TOOL_BAR_ITEM_IMAGES);
8753 if (VECTORP (image))
8755 if (enabled_p)
8756 idx = (selected_p
8757 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8758 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8759 else
8760 idx = (selected_p
8761 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8762 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8764 xassert (ASIZE (image) >= idx);
8765 image = AREF (image, idx);
8767 else
8768 idx = -1;
8770 /* Ignore invalid image specifications. */
8771 if (!valid_image_p (image))
8772 continue;
8774 /* Display the tool-bar button pressed, or depressed. */
8775 plist = Fcopy_sequence (XCDR (image));
8777 /* Compute margin and relief to draw. */
8778 relief = (tool_bar_button_relief >= 0
8779 ? tool_bar_button_relief
8780 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8781 hmargin = vmargin = relief;
8783 if (INTEGERP (Vtool_bar_button_margin)
8784 && XINT (Vtool_bar_button_margin) > 0)
8786 hmargin += XFASTINT (Vtool_bar_button_margin);
8787 vmargin += XFASTINT (Vtool_bar_button_margin);
8789 else if (CONSP (Vtool_bar_button_margin))
8791 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8792 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8793 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8795 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8796 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8797 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8800 if (auto_raise_tool_bar_buttons_p)
8802 /* Add a `:relief' property to the image spec if the item is
8803 selected. */
8804 if (selected_p)
8806 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8807 hmargin -= relief;
8808 vmargin -= relief;
8811 else
8813 /* If image is selected, display it pressed, i.e. with a
8814 negative relief. If it's not selected, display it with a
8815 raised relief. */
8816 plist = Fplist_put (plist, QCrelief,
8817 (selected_p
8818 ? make_number (-relief)
8819 : make_number (relief)));
8820 hmargin -= relief;
8821 vmargin -= relief;
8824 /* Put a margin around the image. */
8825 if (hmargin || vmargin)
8827 if (hmargin == vmargin)
8828 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8829 else
8830 plist = Fplist_put (plist, QCmargin,
8831 Fcons (make_number (hmargin),
8832 make_number (vmargin)));
8835 /* If button is not enabled, and we don't have special images
8836 for the disabled state, make the image appear disabled by
8837 applying an appropriate algorithm to it. */
8838 if (!enabled_p && idx < 0)
8839 plist = Fplist_put (plist, QCconversion, Qdisabled);
8841 /* Put a `display' text property on the string for the image to
8842 display. Put a `menu-item' property on the string that gives
8843 the start of this item's properties in the tool-bar items
8844 vector. */
8845 image = Fcons (Qimage, plist);
8846 props = list4 (Qdisplay, image,
8847 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8849 /* Let the last image hide all remaining spaces in the tool bar
8850 string. The string can be longer than needed when we reuse a
8851 previous string. */
8852 if (i + 1 == f->n_tool_bar_items)
8853 end = SCHARS (f->desired_tool_bar_string);
8854 else
8855 end = i + 1;
8856 Fadd_text_properties (make_number (i), make_number (end),
8857 props, f->desired_tool_bar_string);
8858 #undef PROP
8861 UNGCPRO;
8865 /* Display one line of the tool-bar of frame IT->f. */
8867 static void
8868 display_tool_bar_line (it)
8869 struct it *it;
8871 struct glyph_row *row = it->glyph_row;
8872 int max_x = it->last_visible_x;
8873 struct glyph *last;
8875 prepare_desired_row (row);
8876 row->y = it->current_y;
8878 /* Note that this isn't made use of if the face hasn't a box,
8879 so there's no need to check the face here. */
8880 it->start_of_box_run_p = 1;
8882 while (it->current_x < max_x)
8884 int x_before, x, n_glyphs_before, i, nglyphs;
8886 /* Get the next display element. */
8887 if (!get_next_display_element (it))
8888 break;
8890 /* Produce glyphs. */
8891 x_before = it->current_x;
8892 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8893 PRODUCE_GLYPHS (it);
8895 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8896 i = 0;
8897 x = x_before;
8898 while (i < nglyphs)
8900 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8902 if (x + glyph->pixel_width > max_x)
8904 /* Glyph doesn't fit on line. */
8905 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8906 it->current_x = x;
8907 goto out;
8910 ++it->hpos;
8911 x += glyph->pixel_width;
8912 ++i;
8915 /* Stop at line ends. */
8916 if (ITERATOR_AT_END_OF_LINE_P (it))
8917 break;
8919 set_iterator_to_next (it, 1);
8922 out:;
8924 row->displays_text_p = row->used[TEXT_AREA] != 0;
8925 extend_face_to_end_of_line (it);
8926 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8927 last->right_box_line_p = 1;
8928 if (last == row->glyphs[TEXT_AREA])
8929 last->left_box_line_p = 1;
8930 compute_line_metrics (it);
8932 /* If line is empty, make it occupy the rest of the tool-bar. */
8933 if (!row->displays_text_p)
8935 row->height = row->phys_height = it->last_visible_y - row->y;
8936 row->ascent = row->phys_ascent = 0;
8937 row->extra_line_spacing = 0;
8940 row->full_width_p = 1;
8941 row->continued_p = 0;
8942 row->truncated_on_left_p = 0;
8943 row->truncated_on_right_p = 0;
8945 it->current_x = it->hpos = 0;
8946 it->current_y += row->height;
8947 ++it->vpos;
8948 ++it->glyph_row;
8952 /* Value is the number of screen lines needed to make all tool-bar
8953 items of frame F visible. */
8955 static int
8956 tool_bar_lines_needed (f)
8957 struct frame *f;
8959 struct window *w = XWINDOW (f->tool_bar_window);
8960 struct it it;
8962 /* Initialize an iterator for iteration over
8963 F->desired_tool_bar_string in the tool-bar window of frame F. */
8964 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8965 it.first_visible_x = 0;
8966 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8967 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8969 while (!ITERATOR_AT_END_P (&it))
8971 it.glyph_row = w->desired_matrix->rows;
8972 clear_glyph_row (it.glyph_row);
8973 display_tool_bar_line (&it);
8976 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8980 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8981 0, 1, 0,
8982 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8983 (frame)
8984 Lisp_Object frame;
8986 struct frame *f;
8987 struct window *w;
8988 int nlines = 0;
8990 if (NILP (frame))
8991 frame = selected_frame;
8992 else
8993 CHECK_FRAME (frame);
8994 f = XFRAME (frame);
8996 if (WINDOWP (f->tool_bar_window)
8997 || (w = XWINDOW (f->tool_bar_window),
8998 WINDOW_TOTAL_LINES (w) > 0))
9000 update_tool_bar (f, 1);
9001 if (f->n_tool_bar_items)
9003 build_desired_tool_bar_string (f);
9004 nlines = tool_bar_lines_needed (f);
9008 return make_number (nlines);
9012 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9013 height should be changed. */
9015 static int
9016 redisplay_tool_bar (f)
9017 struct frame *f;
9019 struct window *w;
9020 struct it it;
9021 struct glyph_row *row;
9022 int change_height_p = 0;
9024 #ifdef USE_GTK
9025 if (FRAME_EXTERNAL_TOOL_BAR (f))
9026 update_frame_tool_bar (f);
9027 return 0;
9028 #endif
9030 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9031 do anything. This means you must start with tool-bar-lines
9032 non-zero to get the auto-sizing effect. Or in other words, you
9033 can turn off tool-bars by specifying tool-bar-lines zero. */
9034 if (!WINDOWP (f->tool_bar_window)
9035 || (w = XWINDOW (f->tool_bar_window),
9036 WINDOW_TOTAL_LINES (w) == 0))
9037 return 0;
9039 /* Set up an iterator for the tool-bar window. */
9040 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9041 it.first_visible_x = 0;
9042 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9043 row = it.glyph_row;
9045 /* Build a string that represents the contents of the tool-bar. */
9046 build_desired_tool_bar_string (f);
9047 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9049 /* Display as many lines as needed to display all tool-bar items. */
9050 while (it.current_y < it.last_visible_y)
9051 display_tool_bar_line (&it);
9053 /* It doesn't make much sense to try scrolling in the tool-bar
9054 window, so don't do it. */
9055 w->desired_matrix->no_scrolling_p = 1;
9056 w->must_be_updated_p = 1;
9058 if (auto_resize_tool_bars_p)
9060 int nlines;
9062 /* If we couldn't display everything, change the tool-bar's
9063 height. */
9064 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9065 change_height_p = 1;
9067 /* If there are blank lines at the end, except for a partially
9068 visible blank line at the end that is smaller than
9069 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9070 row = it.glyph_row - 1;
9071 if (!row->displays_text_p
9072 && row->height >= FRAME_LINE_HEIGHT (f))
9073 change_height_p = 1;
9075 /* If row displays tool-bar items, but is partially visible,
9076 change the tool-bar's height. */
9077 if (row->displays_text_p
9078 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9079 change_height_p = 1;
9081 /* Resize windows as needed by changing the `tool-bar-lines'
9082 frame parameter. */
9083 if (change_height_p
9084 && (nlines = tool_bar_lines_needed (f),
9085 nlines != WINDOW_TOTAL_LINES (w)))
9087 extern Lisp_Object Qtool_bar_lines;
9088 Lisp_Object frame;
9089 int old_height = WINDOW_TOTAL_LINES (w);
9091 XSETFRAME (frame, f);
9092 clear_glyph_matrix (w->desired_matrix);
9093 Fmodify_frame_parameters (frame,
9094 Fcons (Fcons (Qtool_bar_lines,
9095 make_number (nlines)),
9096 Qnil));
9097 if (WINDOW_TOTAL_LINES (w) != old_height)
9098 fonts_changed_p = 1;
9102 return change_height_p;
9106 /* Get information about the tool-bar item which is displayed in GLYPH
9107 on frame F. Return in *PROP_IDX the index where tool-bar item
9108 properties start in F->tool_bar_items. Value is zero if
9109 GLYPH doesn't display a tool-bar item. */
9111 static int
9112 tool_bar_item_info (f, glyph, prop_idx)
9113 struct frame *f;
9114 struct glyph *glyph;
9115 int *prop_idx;
9117 Lisp_Object prop;
9118 int success_p;
9119 int charpos;
9121 /* This function can be called asynchronously, which means we must
9122 exclude any possibility that Fget_text_property signals an
9123 error. */
9124 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9125 charpos = max (0, charpos);
9127 /* Get the text property `menu-item' at pos. The value of that
9128 property is the start index of this item's properties in
9129 F->tool_bar_items. */
9130 prop = Fget_text_property (make_number (charpos),
9131 Qmenu_item, f->current_tool_bar_string);
9132 if (INTEGERP (prop))
9134 *prop_idx = XINT (prop);
9135 success_p = 1;
9137 else
9138 success_p = 0;
9140 return success_p;
9144 /* Get information about the tool-bar item at position X/Y on frame F.
9145 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9146 the current matrix of the tool-bar window of F, or NULL if not
9147 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9148 item in F->tool_bar_items. Value is
9150 -1 if X/Y is not on a tool-bar item
9151 0 if X/Y is on the same item that was highlighted before.
9152 1 otherwise. */
9154 static int
9155 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9156 struct frame *f;
9157 int x, y;
9158 struct glyph **glyph;
9159 int *hpos, *vpos, *prop_idx;
9161 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9162 struct window *w = XWINDOW (f->tool_bar_window);
9163 int area;
9165 /* Find the glyph under X/Y. */
9166 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9167 if (*glyph == NULL)
9168 return -1;
9170 /* Get the start of this tool-bar item's properties in
9171 f->tool_bar_items. */
9172 if (!tool_bar_item_info (f, *glyph, prop_idx))
9173 return -1;
9175 /* Is mouse on the highlighted item? */
9176 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9177 && *vpos >= dpyinfo->mouse_face_beg_row
9178 && *vpos <= dpyinfo->mouse_face_end_row
9179 && (*vpos > dpyinfo->mouse_face_beg_row
9180 || *hpos >= dpyinfo->mouse_face_beg_col)
9181 && (*vpos < dpyinfo->mouse_face_end_row
9182 || *hpos < dpyinfo->mouse_face_end_col
9183 || dpyinfo->mouse_face_past_end))
9184 return 0;
9186 return 1;
9190 /* EXPORT:
9191 Handle mouse button event on the tool-bar of frame F, at
9192 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9193 0 for button release. MODIFIERS is event modifiers for button
9194 release. */
9196 void
9197 handle_tool_bar_click (f, x, y, down_p, modifiers)
9198 struct frame *f;
9199 int x, y, down_p;
9200 unsigned int modifiers;
9202 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9203 struct window *w = XWINDOW (f->tool_bar_window);
9204 int hpos, vpos, prop_idx;
9205 struct glyph *glyph;
9206 Lisp_Object enabled_p;
9208 /* If not on the highlighted tool-bar item, return. */
9209 frame_to_window_pixel_xy (w, &x, &y);
9210 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9211 return;
9213 /* If item is disabled, do nothing. */
9214 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9215 if (NILP (enabled_p))
9216 return;
9218 if (down_p)
9220 /* Show item in pressed state. */
9221 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9222 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9223 last_tool_bar_item = prop_idx;
9225 else
9227 Lisp_Object key, frame;
9228 struct input_event event;
9229 EVENT_INIT (event);
9231 /* Show item in released state. */
9232 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9233 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9235 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9237 XSETFRAME (frame, f);
9238 event.kind = TOOL_BAR_EVENT;
9239 event.frame_or_window = frame;
9240 event.arg = frame;
9241 kbd_buffer_store_event (&event);
9243 event.kind = TOOL_BAR_EVENT;
9244 event.frame_or_window = frame;
9245 event.arg = key;
9246 event.modifiers = modifiers;
9247 kbd_buffer_store_event (&event);
9248 last_tool_bar_item = -1;
9253 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9254 tool-bar window-relative coordinates X/Y. Called from
9255 note_mouse_highlight. */
9257 static void
9258 note_tool_bar_highlight (f, x, y)
9259 struct frame *f;
9260 int x, y;
9262 Lisp_Object window = f->tool_bar_window;
9263 struct window *w = XWINDOW (window);
9264 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9265 int hpos, vpos;
9266 struct glyph *glyph;
9267 struct glyph_row *row;
9268 int i;
9269 Lisp_Object enabled_p;
9270 int prop_idx;
9271 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9272 int mouse_down_p, rc;
9274 /* Function note_mouse_highlight is called with negative x(y
9275 values when mouse moves outside of the frame. */
9276 if (x <= 0 || y <= 0)
9278 clear_mouse_face (dpyinfo);
9279 return;
9282 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9283 if (rc < 0)
9285 /* Not on tool-bar item. */
9286 clear_mouse_face (dpyinfo);
9287 return;
9289 else if (rc == 0)
9290 /* On same tool-bar item as before. */
9291 goto set_help_echo;
9293 clear_mouse_face (dpyinfo);
9295 /* Mouse is down, but on different tool-bar item? */
9296 mouse_down_p = (dpyinfo->grabbed
9297 && f == last_mouse_frame
9298 && FRAME_LIVE_P (f));
9299 if (mouse_down_p
9300 && last_tool_bar_item != prop_idx)
9301 return;
9303 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9304 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9306 /* If tool-bar item is not enabled, don't highlight it. */
9307 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9308 if (!NILP (enabled_p))
9310 /* Compute the x-position of the glyph. In front and past the
9311 image is a space. We include this in the highlighted area. */
9312 row = MATRIX_ROW (w->current_matrix, vpos);
9313 for (i = x = 0; i < hpos; ++i)
9314 x += row->glyphs[TEXT_AREA][i].pixel_width;
9316 /* Record this as the current active region. */
9317 dpyinfo->mouse_face_beg_col = hpos;
9318 dpyinfo->mouse_face_beg_row = vpos;
9319 dpyinfo->mouse_face_beg_x = x;
9320 dpyinfo->mouse_face_beg_y = row->y;
9321 dpyinfo->mouse_face_past_end = 0;
9323 dpyinfo->mouse_face_end_col = hpos + 1;
9324 dpyinfo->mouse_face_end_row = vpos;
9325 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9326 dpyinfo->mouse_face_end_y = row->y;
9327 dpyinfo->mouse_face_window = window;
9328 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9330 /* Display it as active. */
9331 show_mouse_face (dpyinfo, draw);
9332 dpyinfo->mouse_face_image_state = draw;
9335 set_help_echo:
9337 /* Set help_echo_string to a help string to display for this tool-bar item.
9338 XTread_socket does the rest. */
9339 help_echo_object = help_echo_window = Qnil;
9340 help_echo_pos = -1;
9341 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9342 if (NILP (help_echo_string))
9343 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9346 #endif /* HAVE_WINDOW_SYSTEM */
9350 /************************************************************************
9351 Horizontal scrolling
9352 ************************************************************************/
9354 static int hscroll_window_tree P_ ((Lisp_Object));
9355 static int hscroll_windows P_ ((Lisp_Object));
9357 /* For all leaf windows in the window tree rooted at WINDOW, set their
9358 hscroll value so that PT is (i) visible in the window, and (ii) so
9359 that it is not within a certain margin at the window's left and
9360 right border. Value is non-zero if any window's hscroll has been
9361 changed. */
9363 static int
9364 hscroll_window_tree (window)
9365 Lisp_Object window;
9367 int hscrolled_p = 0;
9368 int hscroll_relative_p = FLOATP (Vhscroll_step);
9369 int hscroll_step_abs = 0;
9370 double hscroll_step_rel = 0;
9372 if (hscroll_relative_p)
9374 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9375 if (hscroll_step_rel < 0)
9377 hscroll_relative_p = 0;
9378 hscroll_step_abs = 0;
9381 else if (INTEGERP (Vhscroll_step))
9383 hscroll_step_abs = XINT (Vhscroll_step);
9384 if (hscroll_step_abs < 0)
9385 hscroll_step_abs = 0;
9387 else
9388 hscroll_step_abs = 0;
9390 while (WINDOWP (window))
9392 struct window *w = XWINDOW (window);
9394 if (WINDOWP (w->hchild))
9395 hscrolled_p |= hscroll_window_tree (w->hchild);
9396 else if (WINDOWP (w->vchild))
9397 hscrolled_p |= hscroll_window_tree (w->vchild);
9398 else if (w->cursor.vpos >= 0)
9400 int h_margin;
9401 int text_area_width;
9402 struct glyph_row *current_cursor_row
9403 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9404 struct glyph_row *desired_cursor_row
9405 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9406 struct glyph_row *cursor_row
9407 = (desired_cursor_row->enabled_p
9408 ? desired_cursor_row
9409 : current_cursor_row);
9411 text_area_width = window_box_width (w, TEXT_AREA);
9413 /* Scroll when cursor is inside this scroll margin. */
9414 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9416 if ((XFASTINT (w->hscroll)
9417 && w->cursor.x <= h_margin)
9418 || (cursor_row->enabled_p
9419 && cursor_row->truncated_on_right_p
9420 && (w->cursor.x >= text_area_width - h_margin)))
9422 struct it it;
9423 int hscroll;
9424 struct buffer *saved_current_buffer;
9425 int pt;
9426 int wanted_x;
9428 /* Find point in a display of infinite width. */
9429 saved_current_buffer = current_buffer;
9430 current_buffer = XBUFFER (w->buffer);
9432 if (w == XWINDOW (selected_window))
9433 pt = BUF_PT (current_buffer);
9434 else
9436 pt = marker_position (w->pointm);
9437 pt = max (BEGV, pt);
9438 pt = min (ZV, pt);
9441 /* Move iterator to pt starting at cursor_row->start in
9442 a line with infinite width. */
9443 init_to_row_start (&it, w, cursor_row);
9444 it.last_visible_x = INFINITY;
9445 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9446 current_buffer = saved_current_buffer;
9448 /* Position cursor in window. */
9449 if (!hscroll_relative_p && hscroll_step_abs == 0)
9450 hscroll = max (0, (it.current_x
9451 - (ITERATOR_AT_END_OF_LINE_P (&it)
9452 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9453 : (text_area_width / 2))))
9454 / FRAME_COLUMN_WIDTH (it.f);
9455 else if (w->cursor.x >= text_area_width - h_margin)
9457 if (hscroll_relative_p)
9458 wanted_x = text_area_width * (1 - hscroll_step_rel)
9459 - h_margin;
9460 else
9461 wanted_x = text_area_width
9462 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9463 - h_margin;
9464 hscroll
9465 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9467 else
9469 if (hscroll_relative_p)
9470 wanted_x = text_area_width * hscroll_step_rel
9471 + h_margin;
9472 else
9473 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9474 + h_margin;
9475 hscroll
9476 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9478 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9480 /* Don't call Fset_window_hscroll if value hasn't
9481 changed because it will prevent redisplay
9482 optimizations. */
9483 if (XFASTINT (w->hscroll) != hscroll)
9485 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9486 w->hscroll = make_number (hscroll);
9487 hscrolled_p = 1;
9492 window = w->next;
9495 /* Value is non-zero if hscroll of any leaf window has been changed. */
9496 return hscrolled_p;
9500 /* Set hscroll so that cursor is visible and not inside horizontal
9501 scroll margins for all windows in the tree rooted at WINDOW. See
9502 also hscroll_window_tree above. Value is non-zero if any window's
9503 hscroll has been changed. If it has, desired matrices on the frame
9504 of WINDOW are cleared. */
9506 static int
9507 hscroll_windows (window)
9508 Lisp_Object window;
9510 int hscrolled_p;
9512 if (automatic_hscrolling_p)
9514 hscrolled_p = hscroll_window_tree (window);
9515 if (hscrolled_p)
9516 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9518 else
9519 hscrolled_p = 0;
9520 return hscrolled_p;
9525 /************************************************************************
9526 Redisplay
9527 ************************************************************************/
9529 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9530 to a non-zero value. This is sometimes handy to have in a debugger
9531 session. */
9533 #if GLYPH_DEBUG
9535 /* First and last unchanged row for try_window_id. */
9537 int debug_first_unchanged_at_end_vpos;
9538 int debug_last_unchanged_at_beg_vpos;
9540 /* Delta vpos and y. */
9542 int debug_dvpos, debug_dy;
9544 /* Delta in characters and bytes for try_window_id. */
9546 int debug_delta, debug_delta_bytes;
9548 /* Values of window_end_pos and window_end_vpos at the end of
9549 try_window_id. */
9551 EMACS_INT debug_end_pos, debug_end_vpos;
9553 /* Append a string to W->desired_matrix->method. FMT is a printf
9554 format string. A1...A9 are a supplement for a variable-length
9555 argument list. If trace_redisplay_p is non-zero also printf the
9556 resulting string to stderr. */
9558 static void
9559 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9560 struct window *w;
9561 char *fmt;
9562 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9564 char buffer[512];
9565 char *method = w->desired_matrix->method;
9566 int len = strlen (method);
9567 int size = sizeof w->desired_matrix->method;
9568 int remaining = size - len - 1;
9570 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9571 if (len && remaining)
9573 method[len] = '|';
9574 --remaining, ++len;
9577 strncpy (method + len, buffer, remaining);
9579 if (trace_redisplay_p)
9580 fprintf (stderr, "%p (%s): %s\n",
9582 ((BUFFERP (w->buffer)
9583 && STRINGP (XBUFFER (w->buffer)->name))
9584 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9585 : "no buffer"),
9586 buffer);
9589 #endif /* GLYPH_DEBUG */
9592 /* Value is non-zero if all changes in window W, which displays
9593 current_buffer, are in the text between START and END. START is a
9594 buffer position, END is given as a distance from Z. Used in
9595 redisplay_internal for display optimization. */
9597 static INLINE int
9598 text_outside_line_unchanged_p (w, start, end)
9599 struct window *w;
9600 int start, end;
9602 int unchanged_p = 1;
9604 /* If text or overlays have changed, see where. */
9605 if (XFASTINT (w->last_modified) < MODIFF
9606 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9608 /* Gap in the line? */
9609 if (GPT < start || Z - GPT < end)
9610 unchanged_p = 0;
9612 /* Changes start in front of the line, or end after it? */
9613 if (unchanged_p
9614 && (BEG_UNCHANGED < start - 1
9615 || END_UNCHANGED < end))
9616 unchanged_p = 0;
9618 /* If selective display, can't optimize if changes start at the
9619 beginning of the line. */
9620 if (unchanged_p
9621 && INTEGERP (current_buffer->selective_display)
9622 && XINT (current_buffer->selective_display) > 0
9623 && (BEG_UNCHANGED < start || GPT <= start))
9624 unchanged_p = 0;
9626 /* If there are overlays at the start or end of the line, these
9627 may have overlay strings with newlines in them. A change at
9628 START, for instance, may actually concern the display of such
9629 overlay strings as well, and they are displayed on different
9630 lines. So, quickly rule out this case. (For the future, it
9631 might be desirable to implement something more telling than
9632 just BEG/END_UNCHANGED.) */
9633 if (unchanged_p)
9635 if (BEG + BEG_UNCHANGED == start
9636 && overlay_touches_p (start))
9637 unchanged_p = 0;
9638 if (END_UNCHANGED == end
9639 && overlay_touches_p (Z - end))
9640 unchanged_p = 0;
9644 return unchanged_p;
9648 /* Do a frame update, taking possible shortcuts into account. This is
9649 the main external entry point for redisplay.
9651 If the last redisplay displayed an echo area message and that message
9652 is no longer requested, we clear the echo area or bring back the
9653 mini-buffer if that is in use. */
9655 void
9656 redisplay ()
9658 redisplay_internal (0);
9662 static Lisp_Object
9663 overlay_arrow_string_or_property (var)
9664 Lisp_Object var;
9666 Lisp_Object val;
9668 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9669 return val;
9671 return Voverlay_arrow_string;
9674 /* Return 1 if there are any overlay-arrows in current_buffer. */
9675 static int
9676 overlay_arrow_in_current_buffer_p ()
9678 Lisp_Object vlist;
9680 for (vlist = Voverlay_arrow_variable_list;
9681 CONSP (vlist);
9682 vlist = XCDR (vlist))
9684 Lisp_Object var = XCAR (vlist);
9685 Lisp_Object val;
9687 if (!SYMBOLP (var))
9688 continue;
9689 val = find_symbol_value (var);
9690 if (MARKERP (val)
9691 && current_buffer == XMARKER (val)->buffer)
9692 return 1;
9694 return 0;
9698 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9699 has changed. */
9701 static int
9702 overlay_arrows_changed_p ()
9704 Lisp_Object vlist;
9706 for (vlist = Voverlay_arrow_variable_list;
9707 CONSP (vlist);
9708 vlist = XCDR (vlist))
9710 Lisp_Object var = XCAR (vlist);
9711 Lisp_Object val, pstr;
9713 if (!SYMBOLP (var))
9714 continue;
9715 val = find_symbol_value (var);
9716 if (!MARKERP (val))
9717 continue;
9718 if (! EQ (COERCE_MARKER (val),
9719 Fget (var, Qlast_arrow_position))
9720 || ! (pstr = overlay_arrow_string_or_property (var),
9721 EQ (pstr, Fget (var, Qlast_arrow_string))))
9722 return 1;
9724 return 0;
9727 /* Mark overlay arrows to be updated on next redisplay. */
9729 static void
9730 update_overlay_arrows (up_to_date)
9731 int up_to_date;
9733 Lisp_Object vlist;
9735 for (vlist = Voverlay_arrow_variable_list;
9736 CONSP (vlist);
9737 vlist = XCDR (vlist))
9739 Lisp_Object var = XCAR (vlist);
9741 if (!SYMBOLP (var))
9742 continue;
9744 if (up_to_date > 0)
9746 Lisp_Object val = find_symbol_value (var);
9747 Fput (var, Qlast_arrow_position,
9748 COERCE_MARKER (val));
9749 Fput (var, Qlast_arrow_string,
9750 overlay_arrow_string_or_property (var));
9752 else if (up_to_date < 0
9753 || !NILP (Fget (var, Qlast_arrow_position)))
9755 Fput (var, Qlast_arrow_position, Qt);
9756 Fput (var, Qlast_arrow_string, Qt);
9762 /* Return overlay arrow string to display at row.
9763 Return integer (bitmap number) for arrow bitmap in left fringe.
9764 Return nil if no overlay arrow. */
9766 static Lisp_Object
9767 overlay_arrow_at_row (it, row)
9768 struct it *it;
9769 struct glyph_row *row;
9771 Lisp_Object vlist;
9773 for (vlist = Voverlay_arrow_variable_list;
9774 CONSP (vlist);
9775 vlist = XCDR (vlist))
9777 Lisp_Object var = XCAR (vlist);
9778 Lisp_Object val;
9780 if (!SYMBOLP (var))
9781 continue;
9783 val = find_symbol_value (var);
9785 if (MARKERP (val)
9786 && current_buffer == XMARKER (val)->buffer
9787 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9789 if (FRAME_WINDOW_P (it->f)
9790 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9792 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9794 int fringe_bitmap;
9795 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9796 return make_number (fringe_bitmap);
9798 return make_number (-1); /* Use default arrow bitmap */
9800 return overlay_arrow_string_or_property (var);
9804 return Qnil;
9807 /* Return 1 if point moved out of or into a composition. Otherwise
9808 return 0. PREV_BUF and PREV_PT are the last point buffer and
9809 position. BUF and PT are the current point buffer and position. */
9812 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9813 struct buffer *prev_buf, *buf;
9814 int prev_pt, pt;
9816 int start, end;
9817 Lisp_Object prop;
9818 Lisp_Object buffer;
9820 XSETBUFFER (buffer, buf);
9821 /* Check a composition at the last point if point moved within the
9822 same buffer. */
9823 if (prev_buf == buf)
9825 if (prev_pt == pt)
9826 /* Point didn't move. */
9827 return 0;
9829 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9830 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9831 && COMPOSITION_VALID_P (start, end, prop)
9832 && start < prev_pt && end > prev_pt)
9833 /* The last point was within the composition. Return 1 iff
9834 point moved out of the composition. */
9835 return (pt <= start || pt >= end);
9838 /* Check a composition at the current point. */
9839 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9840 && find_composition (pt, -1, &start, &end, &prop, buffer)
9841 && COMPOSITION_VALID_P (start, end, prop)
9842 && start < pt && end > pt);
9846 /* Reconsider the setting of B->clip_changed which is displayed
9847 in window W. */
9849 static INLINE void
9850 reconsider_clip_changes (w, b)
9851 struct window *w;
9852 struct buffer *b;
9854 if (b->clip_changed
9855 && !NILP (w->window_end_valid)
9856 && w->current_matrix->buffer == b
9857 && w->current_matrix->zv == BUF_ZV (b)
9858 && w->current_matrix->begv == BUF_BEGV (b))
9859 b->clip_changed = 0;
9861 /* If display wasn't paused, and W is not a tool bar window, see if
9862 point has been moved into or out of a composition. In that case,
9863 we set b->clip_changed to 1 to force updating the screen. If
9864 b->clip_changed has already been set to 1, we can skip this
9865 check. */
9866 if (!b->clip_changed
9867 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9869 int pt;
9871 if (w == XWINDOW (selected_window))
9872 pt = BUF_PT (current_buffer);
9873 else
9874 pt = marker_position (w->pointm);
9876 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9877 || pt != XINT (w->last_point))
9878 && check_point_in_composition (w->current_matrix->buffer,
9879 XINT (w->last_point),
9880 XBUFFER (w->buffer), pt))
9881 b->clip_changed = 1;
9886 /* Select FRAME to forward the values of frame-local variables into C
9887 variables so that the redisplay routines can access those values
9888 directly. */
9890 static void
9891 select_frame_for_redisplay (frame)
9892 Lisp_Object frame;
9894 Lisp_Object tail, sym, val;
9895 Lisp_Object old = selected_frame;
9897 selected_frame = frame;
9899 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9900 if (CONSP (XCAR (tail))
9901 && (sym = XCAR (XCAR (tail)),
9902 SYMBOLP (sym))
9903 && (sym = indirect_variable (sym),
9904 val = SYMBOL_VALUE (sym),
9905 (BUFFER_LOCAL_VALUEP (val)
9906 || SOME_BUFFER_LOCAL_VALUEP (val)))
9907 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9908 Fsymbol_value (sym);
9910 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9911 if (CONSP (XCAR (tail))
9912 && (sym = XCAR (XCAR (tail)),
9913 SYMBOLP (sym))
9914 && (sym = indirect_variable (sym),
9915 val = SYMBOL_VALUE (sym),
9916 (BUFFER_LOCAL_VALUEP (val)
9917 || SOME_BUFFER_LOCAL_VALUEP (val)))
9918 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9919 Fsymbol_value (sym);
9923 #define STOP_POLLING \
9924 do { if (! polling_stopped_here) stop_polling (); \
9925 polling_stopped_here = 1; } while (0)
9927 #define RESUME_POLLING \
9928 do { if (polling_stopped_here) start_polling (); \
9929 polling_stopped_here = 0; } while (0)
9932 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9933 response to any user action; therefore, we should preserve the echo
9934 area. (Actually, our caller does that job.) Perhaps in the future
9935 avoid recentering windows if it is not necessary; currently that
9936 causes some problems. */
9938 static void
9939 redisplay_internal (preserve_echo_area)
9940 int preserve_echo_area;
9942 struct window *w = XWINDOW (selected_window);
9943 struct frame *f = XFRAME (w->frame);
9944 int pause;
9945 int must_finish = 0;
9946 struct text_pos tlbufpos, tlendpos;
9947 int number_of_visible_frames;
9948 int count;
9949 struct frame *sf = SELECTED_FRAME ();
9950 int polling_stopped_here = 0;
9952 /* Non-zero means redisplay has to consider all windows on all
9953 frames. Zero means, only selected_window is considered. */
9954 int consider_all_windows_p;
9956 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9958 /* No redisplay if running in batch mode or frame is not yet fully
9959 initialized, or redisplay is explicitly turned off by setting
9960 Vinhibit_redisplay. */
9961 if (noninteractive
9962 || !NILP (Vinhibit_redisplay)
9963 || !f->glyphs_initialized_p)
9964 return;
9966 /* The flag redisplay_performed_directly_p is set by
9967 direct_output_for_insert when it already did the whole screen
9968 update necessary. */
9969 if (redisplay_performed_directly_p)
9971 redisplay_performed_directly_p = 0;
9972 if (!hscroll_windows (selected_window))
9973 return;
9976 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9977 if (popup_activated ())
9978 return;
9979 #endif
9981 /* I don't think this happens but let's be paranoid. */
9982 if (redisplaying_p)
9983 return;
9985 /* Record a function that resets redisplaying_p to its old value
9986 when we leave this function. */
9987 count = SPECPDL_INDEX ();
9988 record_unwind_protect (unwind_redisplay,
9989 Fcons (make_number (redisplaying_p), selected_frame));
9990 ++redisplaying_p;
9991 specbind (Qinhibit_free_realized_faces, Qnil);
9993 retry:
9994 pause = 0;
9995 reconsider_clip_changes (w, current_buffer);
9997 /* If new fonts have been loaded that make a glyph matrix adjustment
9998 necessary, do it. */
9999 if (fonts_changed_p)
10001 adjust_glyphs (NULL);
10002 ++windows_or_buffers_changed;
10003 fonts_changed_p = 0;
10006 /* If face_change_count is non-zero, init_iterator will free all
10007 realized faces, which includes the faces referenced from current
10008 matrices. So, we can't reuse current matrices in this case. */
10009 if (face_change_count)
10010 ++windows_or_buffers_changed;
10012 if (! FRAME_WINDOW_P (sf)
10013 && previous_terminal_frame != sf)
10015 /* Since frames on an ASCII terminal share the same display
10016 area, displaying a different frame means redisplay the whole
10017 thing. */
10018 windows_or_buffers_changed++;
10019 SET_FRAME_GARBAGED (sf);
10020 XSETFRAME (Vterminal_frame, sf);
10022 previous_terminal_frame = sf;
10024 /* Set the visible flags for all frames. Do this before checking
10025 for resized or garbaged frames; they want to know if their frames
10026 are visible. See the comment in frame.h for
10027 FRAME_SAMPLE_VISIBILITY. */
10029 Lisp_Object tail, frame;
10031 number_of_visible_frames = 0;
10033 FOR_EACH_FRAME (tail, frame)
10035 struct frame *f = XFRAME (frame);
10037 FRAME_SAMPLE_VISIBILITY (f);
10038 if (FRAME_VISIBLE_P (f))
10039 ++number_of_visible_frames;
10040 clear_desired_matrices (f);
10044 /* Notice any pending interrupt request to change frame size. */
10045 do_pending_window_change (1);
10047 /* Clear frames marked as garbaged. */
10048 if (frame_garbaged)
10049 clear_garbaged_frames ();
10051 /* Build menubar and tool-bar items. */
10052 prepare_menu_bars ();
10054 if (windows_or_buffers_changed)
10055 update_mode_lines++;
10057 /* Detect case that we need to write or remove a star in the mode line. */
10058 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10060 w->update_mode_line = Qt;
10061 if (buffer_shared > 1)
10062 update_mode_lines++;
10065 /* If %c is in the mode line, update it if needed. */
10066 if (!NILP (w->column_number_displayed)
10067 /* This alternative quickly identifies a common case
10068 where no change is needed. */
10069 && !(PT == XFASTINT (w->last_point)
10070 && XFASTINT (w->last_modified) >= MODIFF
10071 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10072 && (XFASTINT (w->column_number_displayed)
10073 != (int) current_column ())) /* iftc */
10074 w->update_mode_line = Qt;
10076 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10078 /* The variable buffer_shared is set in redisplay_window and
10079 indicates that we redisplay a buffer in different windows. See
10080 there. */
10081 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10082 || cursor_type_changed);
10084 /* If specs for an arrow have changed, do thorough redisplay
10085 to ensure we remove any arrow that should no longer exist. */
10086 if (overlay_arrows_changed_p ())
10087 consider_all_windows_p = windows_or_buffers_changed = 1;
10089 /* Normally the message* functions will have already displayed and
10090 updated the echo area, but the frame may have been trashed, or
10091 the update may have been preempted, so display the echo area
10092 again here. Checking message_cleared_p captures the case that
10093 the echo area should be cleared. */
10094 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10095 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10096 || (message_cleared_p
10097 && minibuf_level == 0
10098 /* If the mini-window is currently selected, this means the
10099 echo-area doesn't show through. */
10100 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10102 int window_height_changed_p = echo_area_display (0);
10103 must_finish = 1;
10105 /* If we don't display the current message, don't clear the
10106 message_cleared_p flag, because, if we did, we wouldn't clear
10107 the echo area in the next redisplay which doesn't preserve
10108 the echo area. */
10109 if (!display_last_displayed_message_p)
10110 message_cleared_p = 0;
10112 if (fonts_changed_p)
10113 goto retry;
10114 else if (window_height_changed_p)
10116 consider_all_windows_p = 1;
10117 ++update_mode_lines;
10118 ++windows_or_buffers_changed;
10120 /* If window configuration was changed, frames may have been
10121 marked garbaged. Clear them or we will experience
10122 surprises wrt scrolling. */
10123 if (frame_garbaged)
10124 clear_garbaged_frames ();
10127 else if (EQ (selected_window, minibuf_window)
10128 && (current_buffer->clip_changed
10129 || XFASTINT (w->last_modified) < MODIFF
10130 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10131 && resize_mini_window (w, 0))
10133 /* Resized active mini-window to fit the size of what it is
10134 showing if its contents might have changed. */
10135 must_finish = 1;
10136 consider_all_windows_p = 1;
10137 ++windows_or_buffers_changed;
10138 ++update_mode_lines;
10140 /* If window configuration was changed, frames may have been
10141 marked garbaged. Clear them or we will experience
10142 surprises wrt scrolling. */
10143 if (frame_garbaged)
10144 clear_garbaged_frames ();
10148 /* If showing the region, and mark has changed, we must redisplay
10149 the whole window. The assignment to this_line_start_pos prevents
10150 the optimization directly below this if-statement. */
10151 if (((!NILP (Vtransient_mark_mode)
10152 && !NILP (XBUFFER (w->buffer)->mark_active))
10153 != !NILP (w->region_showing))
10154 || (!NILP (w->region_showing)
10155 && !EQ (w->region_showing,
10156 Fmarker_position (XBUFFER (w->buffer)->mark))))
10157 CHARPOS (this_line_start_pos) = 0;
10159 /* Optimize the case that only the line containing the cursor in the
10160 selected window has changed. Variables starting with this_ are
10161 set in display_line and record information about the line
10162 containing the cursor. */
10163 tlbufpos = this_line_start_pos;
10164 tlendpos = this_line_end_pos;
10165 if (!consider_all_windows_p
10166 && CHARPOS (tlbufpos) > 0
10167 && NILP (w->update_mode_line)
10168 && !current_buffer->clip_changed
10169 && !current_buffer->prevent_redisplay_optimizations_p
10170 && FRAME_VISIBLE_P (XFRAME (w->frame))
10171 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10172 /* Make sure recorded data applies to current buffer, etc. */
10173 && this_line_buffer == current_buffer
10174 && current_buffer == XBUFFER (w->buffer)
10175 && NILP (w->force_start)
10176 && NILP (w->optional_new_start)
10177 /* Point must be on the line that we have info recorded about. */
10178 && PT >= CHARPOS (tlbufpos)
10179 && PT <= Z - CHARPOS (tlendpos)
10180 /* All text outside that line, including its final newline,
10181 must be unchanged */
10182 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10183 CHARPOS (tlendpos)))
10185 if (CHARPOS (tlbufpos) > BEGV
10186 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10187 && (CHARPOS (tlbufpos) == ZV
10188 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10189 /* Former continuation line has disappeared by becoming empty */
10190 goto cancel;
10191 else if (XFASTINT (w->last_modified) < MODIFF
10192 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10193 || MINI_WINDOW_P (w))
10195 /* We have to handle the case of continuation around a
10196 wide-column character (See the comment in indent.c around
10197 line 885).
10199 For instance, in the following case:
10201 -------- Insert --------
10202 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10203 J_I_ ==> J_I_ `^^' are cursors.
10204 ^^ ^^
10205 -------- --------
10207 As we have to redraw the line above, we should goto cancel. */
10209 struct it it;
10210 int line_height_before = this_line_pixel_height;
10212 /* Note that start_display will handle the case that the
10213 line starting at tlbufpos is a continuation lines. */
10214 start_display (&it, w, tlbufpos);
10216 /* Implementation note: It this still necessary? */
10217 if (it.current_x != this_line_start_x)
10218 goto cancel;
10220 TRACE ((stderr, "trying display optimization 1\n"));
10221 w->cursor.vpos = -1;
10222 overlay_arrow_seen = 0;
10223 it.vpos = this_line_vpos;
10224 it.current_y = this_line_y;
10225 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10226 display_line (&it);
10228 /* If line contains point, is not continued,
10229 and ends at same distance from eob as before, we win */
10230 if (w->cursor.vpos >= 0
10231 /* Line is not continued, otherwise this_line_start_pos
10232 would have been set to 0 in display_line. */
10233 && CHARPOS (this_line_start_pos)
10234 /* Line ends as before. */
10235 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10236 /* Line has same height as before. Otherwise other lines
10237 would have to be shifted up or down. */
10238 && this_line_pixel_height == line_height_before)
10240 /* If this is not the window's last line, we must adjust
10241 the charstarts of the lines below. */
10242 if (it.current_y < it.last_visible_y)
10244 struct glyph_row *row
10245 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10246 int delta, delta_bytes;
10248 if (Z - CHARPOS (tlendpos) == ZV)
10250 /* This line ends at end of (accessible part of)
10251 buffer. There is no newline to count. */
10252 delta = (Z
10253 - CHARPOS (tlendpos)
10254 - MATRIX_ROW_START_CHARPOS (row));
10255 delta_bytes = (Z_BYTE
10256 - BYTEPOS (tlendpos)
10257 - MATRIX_ROW_START_BYTEPOS (row));
10259 else
10261 /* This line ends in a newline. Must take
10262 account of the newline and the rest of the
10263 text that follows. */
10264 delta = (Z
10265 - CHARPOS (tlendpos)
10266 - MATRIX_ROW_START_CHARPOS (row));
10267 delta_bytes = (Z_BYTE
10268 - BYTEPOS (tlendpos)
10269 - MATRIX_ROW_START_BYTEPOS (row));
10272 increment_matrix_positions (w->current_matrix,
10273 this_line_vpos + 1,
10274 w->current_matrix->nrows,
10275 delta, delta_bytes);
10278 /* If this row displays text now but previously didn't,
10279 or vice versa, w->window_end_vpos may have to be
10280 adjusted. */
10281 if ((it.glyph_row - 1)->displays_text_p)
10283 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10284 XSETINT (w->window_end_vpos, this_line_vpos);
10286 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10287 && this_line_vpos > 0)
10288 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10289 w->window_end_valid = Qnil;
10291 /* Update hint: No need to try to scroll in update_window. */
10292 w->desired_matrix->no_scrolling_p = 1;
10294 #if GLYPH_DEBUG
10295 *w->desired_matrix->method = 0;
10296 debug_method_add (w, "optimization 1");
10297 #endif
10298 #ifdef HAVE_WINDOW_SYSTEM
10299 update_window_fringes (w, 0);
10300 #endif
10301 goto update;
10303 else
10304 goto cancel;
10306 else if (/* Cursor position hasn't changed. */
10307 PT == XFASTINT (w->last_point)
10308 /* Make sure the cursor was last displayed
10309 in this window. Otherwise we have to reposition it. */
10310 && 0 <= w->cursor.vpos
10311 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10313 if (!must_finish)
10315 do_pending_window_change (1);
10317 /* We used to always goto end_of_redisplay here, but this
10318 isn't enough if we have a blinking cursor. */
10319 if (w->cursor_off_p == w->last_cursor_off_p)
10320 goto end_of_redisplay;
10322 goto update;
10324 /* If highlighting the region, or if the cursor is in the echo area,
10325 then we can't just move the cursor. */
10326 else if (! (!NILP (Vtransient_mark_mode)
10327 && !NILP (current_buffer->mark_active))
10328 && (EQ (selected_window, current_buffer->last_selected_window)
10329 || highlight_nonselected_windows)
10330 && NILP (w->region_showing)
10331 && NILP (Vshow_trailing_whitespace)
10332 && !cursor_in_echo_area)
10334 struct it it;
10335 struct glyph_row *row;
10337 /* Skip from tlbufpos to PT and see where it is. Note that
10338 PT may be in invisible text. If so, we will end at the
10339 next visible position. */
10340 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10341 NULL, DEFAULT_FACE_ID);
10342 it.current_x = this_line_start_x;
10343 it.current_y = this_line_y;
10344 it.vpos = this_line_vpos;
10346 /* The call to move_it_to stops in front of PT, but
10347 moves over before-strings. */
10348 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10350 if (it.vpos == this_line_vpos
10351 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10352 row->enabled_p))
10354 xassert (this_line_vpos == it.vpos);
10355 xassert (this_line_y == it.current_y);
10356 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10357 #if GLYPH_DEBUG
10358 *w->desired_matrix->method = 0;
10359 debug_method_add (w, "optimization 3");
10360 #endif
10361 goto update;
10363 else
10364 goto cancel;
10367 cancel:
10368 /* Text changed drastically or point moved off of line. */
10369 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10372 CHARPOS (this_line_start_pos) = 0;
10373 consider_all_windows_p |= buffer_shared > 1;
10374 ++clear_face_cache_count;
10375 #ifdef HAVE_WINDOW_SYSTEM
10376 ++clear_image_cache_count;
10377 #endif
10379 /* Build desired matrices, and update the display. If
10380 consider_all_windows_p is non-zero, do it for all windows on all
10381 frames. Otherwise do it for selected_window, only. */
10383 if (consider_all_windows_p)
10385 Lisp_Object tail, frame;
10386 int i, n = 0, size = 50;
10387 struct frame **updated
10388 = (struct frame **) alloca (size * sizeof *updated);
10390 /* Recompute # windows showing selected buffer. This will be
10391 incremented each time such a window is displayed. */
10392 buffer_shared = 0;
10394 FOR_EACH_FRAME (tail, frame)
10396 struct frame *f = XFRAME (frame);
10398 if (FRAME_WINDOW_P (f) || f == sf)
10400 if (! EQ (frame, selected_frame))
10401 /* Select the frame, for the sake of frame-local
10402 variables. */
10403 select_frame_for_redisplay (frame);
10405 /* Mark all the scroll bars to be removed; we'll redeem
10406 the ones we want when we redisplay their windows. */
10407 if (condemn_scroll_bars_hook)
10408 condemn_scroll_bars_hook (f);
10410 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10411 redisplay_windows (FRAME_ROOT_WINDOW (f));
10413 /* Any scroll bars which redisplay_windows should have
10414 nuked should now go away. */
10415 if (judge_scroll_bars_hook)
10416 judge_scroll_bars_hook (f);
10418 /* If fonts changed, display again. */
10419 /* ??? rms: I suspect it is a mistake to jump all the way
10420 back to retry here. It should just retry this frame. */
10421 if (fonts_changed_p)
10422 goto retry;
10424 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10426 /* See if we have to hscroll. */
10427 if (hscroll_windows (f->root_window))
10428 goto retry;
10430 /* Prevent various kinds of signals during display
10431 update. stdio is not robust about handling
10432 signals, which can cause an apparent I/O
10433 error. */
10434 if (interrupt_input)
10435 unrequest_sigio ();
10436 STOP_POLLING;
10438 /* Update the display. */
10439 set_window_update_flags (XWINDOW (f->root_window), 1);
10440 pause |= update_frame (f, 0, 0);
10441 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10442 if (pause)
10443 break;
10444 #endif
10446 if (n == size)
10448 int nbytes = size * sizeof *updated;
10449 struct frame **p = (struct frame **) alloca (2 * nbytes);
10450 bcopy (updated, p, nbytes);
10451 size *= 2;
10454 updated[n++] = f;
10459 if (!pause)
10461 /* Do the mark_window_display_accurate after all windows have
10462 been redisplayed because this call resets flags in buffers
10463 which are needed for proper redisplay. */
10464 for (i = 0; i < n; ++i)
10466 struct frame *f = updated[i];
10467 mark_window_display_accurate (f->root_window, 1);
10468 if (frame_up_to_date_hook)
10469 frame_up_to_date_hook (f);
10473 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10475 Lisp_Object mini_window;
10476 struct frame *mini_frame;
10478 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10479 /* Use list_of_error, not Qerror, so that
10480 we catch only errors and don't run the debugger. */
10481 internal_condition_case_1 (redisplay_window_1, selected_window,
10482 list_of_error,
10483 redisplay_window_error);
10485 /* Compare desired and current matrices, perform output. */
10487 update:
10488 /* If fonts changed, display again. */
10489 if (fonts_changed_p)
10490 goto retry;
10492 /* Prevent various kinds of signals during display update.
10493 stdio is not robust about handling signals,
10494 which can cause an apparent I/O error. */
10495 if (interrupt_input)
10496 unrequest_sigio ();
10497 STOP_POLLING;
10499 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10501 if (hscroll_windows (selected_window))
10502 goto retry;
10504 XWINDOW (selected_window)->must_be_updated_p = 1;
10505 pause = update_frame (sf, 0, 0);
10508 /* We may have called echo_area_display at the top of this
10509 function. If the echo area is on another frame, that may
10510 have put text on a frame other than the selected one, so the
10511 above call to update_frame would not have caught it. Catch
10512 it here. */
10513 mini_window = FRAME_MINIBUF_WINDOW (sf);
10514 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10516 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10518 XWINDOW (mini_window)->must_be_updated_p = 1;
10519 pause |= update_frame (mini_frame, 0, 0);
10520 if (!pause && hscroll_windows (mini_window))
10521 goto retry;
10525 /* If display was paused because of pending input, make sure we do a
10526 thorough update the next time. */
10527 if (pause)
10529 /* Prevent the optimization at the beginning of
10530 redisplay_internal that tries a single-line update of the
10531 line containing the cursor in the selected window. */
10532 CHARPOS (this_line_start_pos) = 0;
10534 /* Let the overlay arrow be updated the next time. */
10535 update_overlay_arrows (0);
10537 /* If we pause after scrolling, some rows in the current
10538 matrices of some windows are not valid. */
10539 if (!WINDOW_FULL_WIDTH_P (w)
10540 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10541 update_mode_lines = 1;
10543 else
10545 if (!consider_all_windows_p)
10547 /* This has already been done above if
10548 consider_all_windows_p is set. */
10549 mark_window_display_accurate_1 (w, 1);
10551 /* Say overlay arrows are up to date. */
10552 update_overlay_arrows (1);
10554 if (frame_up_to_date_hook != 0)
10555 frame_up_to_date_hook (sf);
10558 update_mode_lines = 0;
10559 windows_or_buffers_changed = 0;
10560 cursor_type_changed = 0;
10563 /* Start SIGIO interrupts coming again. Having them off during the
10564 code above makes it less likely one will discard output, but not
10565 impossible, since there might be stuff in the system buffer here.
10566 But it is much hairier to try to do anything about that. */
10567 if (interrupt_input)
10568 request_sigio ();
10569 RESUME_POLLING;
10571 /* If a frame has become visible which was not before, redisplay
10572 again, so that we display it. Expose events for such a frame
10573 (which it gets when becoming visible) don't call the parts of
10574 redisplay constructing glyphs, so simply exposing a frame won't
10575 display anything in this case. So, we have to display these
10576 frames here explicitly. */
10577 if (!pause)
10579 Lisp_Object tail, frame;
10580 int new_count = 0;
10582 FOR_EACH_FRAME (tail, frame)
10584 int this_is_visible = 0;
10586 if (XFRAME (frame)->visible)
10587 this_is_visible = 1;
10588 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10589 if (XFRAME (frame)->visible)
10590 this_is_visible = 1;
10592 if (this_is_visible)
10593 new_count++;
10596 if (new_count != number_of_visible_frames)
10597 windows_or_buffers_changed++;
10600 /* Change frame size now if a change is pending. */
10601 do_pending_window_change (1);
10603 /* If we just did a pending size change, or have additional
10604 visible frames, redisplay again. */
10605 if (windows_or_buffers_changed && !pause)
10606 goto retry;
10608 /* Clear the face cache eventually. */
10609 if (consider_all_windows_p)
10611 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10613 clear_face_cache (0);
10614 clear_face_cache_count = 0;
10616 #ifdef HAVE_WINDOW_SYSTEM
10617 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10619 Lisp_Object tail, frame;
10620 FOR_EACH_FRAME (tail, frame)
10622 struct frame *f = XFRAME (frame);
10623 if (FRAME_WINDOW_P (f))
10624 clear_image_cache (f, 0);
10626 clear_image_cache_count = 0;
10628 #endif /* HAVE_WINDOW_SYSTEM */
10631 end_of_redisplay:
10632 unbind_to (count, Qnil);
10633 RESUME_POLLING;
10637 /* Redisplay, but leave alone any recent echo area message unless
10638 another message has been requested in its place.
10640 This is useful in situations where you need to redisplay but no
10641 user action has occurred, making it inappropriate for the message
10642 area to be cleared. See tracking_off and
10643 wait_reading_process_output for examples of these situations.
10645 FROM_WHERE is an integer saying from where this function was
10646 called. This is useful for debugging. */
10648 void
10649 redisplay_preserve_echo_area (from_where)
10650 int from_where;
10652 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10654 if (!NILP (echo_area_buffer[1]))
10656 /* We have a previously displayed message, but no current
10657 message. Redisplay the previous message. */
10658 display_last_displayed_message_p = 1;
10659 redisplay_internal (1);
10660 display_last_displayed_message_p = 0;
10662 else
10663 redisplay_internal (1);
10665 if (rif != NULL && rif->flush_display_optional)
10666 rif->flush_display_optional (NULL);
10670 /* Function registered with record_unwind_protect in
10671 redisplay_internal. Reset redisplaying_p to the value it had
10672 before redisplay_internal was called, and clear
10673 prevent_freeing_realized_faces_p. It also selects the previously
10674 selected frame. */
10676 static Lisp_Object
10677 unwind_redisplay (val)
10678 Lisp_Object val;
10680 Lisp_Object old_redisplaying_p, old_frame;
10682 old_redisplaying_p = XCAR (val);
10683 redisplaying_p = XFASTINT (old_redisplaying_p);
10684 old_frame = XCDR (val);
10685 if (! EQ (old_frame, selected_frame))
10686 select_frame_for_redisplay (old_frame);
10687 return Qnil;
10691 /* Mark the display of window W as accurate or inaccurate. If
10692 ACCURATE_P is non-zero mark display of W as accurate. If
10693 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10694 redisplay_internal is called. */
10696 static void
10697 mark_window_display_accurate_1 (w, accurate_p)
10698 struct window *w;
10699 int accurate_p;
10701 if (BUFFERP (w->buffer))
10703 struct buffer *b = XBUFFER (w->buffer);
10705 w->last_modified
10706 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10707 w->last_overlay_modified
10708 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10709 w->last_had_star
10710 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10712 if (accurate_p)
10714 b->clip_changed = 0;
10715 b->prevent_redisplay_optimizations_p = 0;
10717 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10718 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10719 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10720 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10722 w->current_matrix->buffer = b;
10723 w->current_matrix->begv = BUF_BEGV (b);
10724 w->current_matrix->zv = BUF_ZV (b);
10726 w->last_cursor = w->cursor;
10727 w->last_cursor_off_p = w->cursor_off_p;
10729 if (w == XWINDOW (selected_window))
10730 w->last_point = make_number (BUF_PT (b));
10731 else
10732 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10736 if (accurate_p)
10738 w->window_end_valid = w->buffer;
10739 #if 0 /* This is incorrect with variable-height lines. */
10740 xassert (XINT (w->window_end_vpos)
10741 < (WINDOW_TOTAL_LINES (w)
10742 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10743 #endif
10744 w->update_mode_line = Qnil;
10749 /* Mark the display of windows in the window tree rooted at WINDOW as
10750 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10751 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10752 be redisplayed the next time redisplay_internal is called. */
10754 void
10755 mark_window_display_accurate (window, accurate_p)
10756 Lisp_Object window;
10757 int accurate_p;
10759 struct window *w;
10761 for (; !NILP (window); window = w->next)
10763 w = XWINDOW (window);
10764 mark_window_display_accurate_1 (w, accurate_p);
10766 if (!NILP (w->vchild))
10767 mark_window_display_accurate (w->vchild, accurate_p);
10768 if (!NILP (w->hchild))
10769 mark_window_display_accurate (w->hchild, accurate_p);
10772 if (accurate_p)
10774 update_overlay_arrows (1);
10776 else
10778 /* Force a thorough redisplay the next time by setting
10779 last_arrow_position and last_arrow_string to t, which is
10780 unequal to any useful value of Voverlay_arrow_... */
10781 update_overlay_arrows (-1);
10786 /* Return value in display table DP (Lisp_Char_Table *) for character
10787 C. Since a display table doesn't have any parent, we don't have to
10788 follow parent. Do not call this function directly but use the
10789 macro DISP_CHAR_VECTOR. */
10791 Lisp_Object
10792 disp_char_vector (dp, c)
10793 struct Lisp_Char_Table *dp;
10794 int c;
10796 int code[4], i;
10797 Lisp_Object val;
10799 if (SINGLE_BYTE_CHAR_P (c))
10800 return (dp->contents[c]);
10802 SPLIT_CHAR (c, code[0], code[1], code[2]);
10803 if (code[1] < 32)
10804 code[1] = -1;
10805 else if (code[2] < 32)
10806 code[2] = -1;
10808 /* Here, the possible range of code[0] (== charset ID) is
10809 128..max_charset. Since the top level char table contains data
10810 for multibyte characters after 256th element, we must increment
10811 code[0] by 128 to get a correct index. */
10812 code[0] += 128;
10813 code[3] = -1; /* anchor */
10815 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10817 val = dp->contents[code[i]];
10818 if (!SUB_CHAR_TABLE_P (val))
10819 return (NILP (val) ? dp->defalt : val);
10822 /* Here, val is a sub char table. We return the default value of
10823 it. */
10824 return (dp->defalt);
10829 /***********************************************************************
10830 Window Redisplay
10831 ***********************************************************************/
10833 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10835 static void
10836 redisplay_windows (window)
10837 Lisp_Object window;
10839 while (!NILP (window))
10841 struct window *w = XWINDOW (window);
10843 if (!NILP (w->hchild))
10844 redisplay_windows (w->hchild);
10845 else if (!NILP (w->vchild))
10846 redisplay_windows (w->vchild);
10847 else
10849 displayed_buffer = XBUFFER (w->buffer);
10850 /* Use list_of_error, not Qerror, so that
10851 we catch only errors and don't run the debugger. */
10852 internal_condition_case_1 (redisplay_window_0, window,
10853 list_of_error,
10854 redisplay_window_error);
10857 window = w->next;
10861 static Lisp_Object
10862 redisplay_window_error ()
10864 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10865 return Qnil;
10868 static Lisp_Object
10869 redisplay_window_0 (window)
10870 Lisp_Object window;
10872 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10873 redisplay_window (window, 0);
10874 return Qnil;
10877 static Lisp_Object
10878 redisplay_window_1 (window)
10879 Lisp_Object window;
10881 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10882 redisplay_window (window, 1);
10883 return Qnil;
10887 /* Increment GLYPH until it reaches END or CONDITION fails while
10888 adding (GLYPH)->pixel_width to X. */
10890 #define SKIP_GLYPHS(glyph, end, x, condition) \
10891 do \
10893 (x) += (glyph)->pixel_width; \
10894 ++(glyph); \
10896 while ((glyph) < (end) && (condition))
10899 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10900 DELTA is the number of bytes by which positions recorded in ROW
10901 differ from current buffer positions. */
10903 void
10904 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10905 struct window *w;
10906 struct glyph_row *row;
10907 struct glyph_matrix *matrix;
10908 int delta, delta_bytes, dy, dvpos;
10910 struct glyph *glyph = row->glyphs[TEXT_AREA];
10911 struct glyph *end = glyph + row->used[TEXT_AREA];
10912 struct glyph *cursor = NULL;
10913 /* The first glyph that starts a sequence of glyphs from string. */
10914 struct glyph *string_start;
10915 /* The X coordinate of string_start. */
10916 int string_start_x;
10917 /* The last known character position. */
10918 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10919 /* The last known character position before string_start. */
10920 int string_before_pos;
10921 int x = row->x;
10922 int cursor_x = x;
10923 int cursor_from_overlay_pos = 0;
10924 int pt_old = PT - delta;
10926 /* Skip over glyphs not having an object at the start of the row.
10927 These are special glyphs like truncation marks on terminal
10928 frames. */
10929 if (row->displays_text_p)
10930 while (glyph < end
10931 && INTEGERP (glyph->object)
10932 && glyph->charpos < 0)
10934 x += glyph->pixel_width;
10935 ++glyph;
10938 string_start = NULL;
10939 while (glyph < end
10940 && !INTEGERP (glyph->object)
10941 && (!BUFFERP (glyph->object)
10942 || (last_pos = glyph->charpos) < pt_old))
10944 if (! STRINGP (glyph->object))
10946 string_start = NULL;
10947 x += glyph->pixel_width;
10948 ++glyph;
10949 if (cursor_from_overlay_pos
10950 && last_pos > cursor_from_overlay_pos)
10952 cursor_from_overlay_pos = 0;
10953 cursor = 0;
10956 else
10958 string_before_pos = last_pos;
10959 string_start = glyph;
10960 string_start_x = x;
10961 /* Skip all glyphs from string. */
10964 int pos;
10965 if ((cursor == NULL || glyph > cursor)
10966 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10967 Qcursor, (glyph)->object))
10968 && (pos = string_buffer_position (w, glyph->object,
10969 string_before_pos),
10970 (pos == 0 /* From overlay */
10971 || pos == pt_old)))
10973 /* Estimate overlay buffer position from the buffer
10974 positions of the glyphs before and after the overlay.
10975 Add 1 to last_pos so that if point corresponds to the
10976 glyph right after the overlay, we still use a 'cursor'
10977 property found in that overlay. */
10978 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10979 cursor = glyph;
10980 cursor_x = x;
10982 x += glyph->pixel_width;
10983 ++glyph;
10985 while (glyph < end && STRINGP (glyph->object));
10989 if (cursor != NULL)
10991 glyph = cursor;
10992 x = cursor_x;
10994 else if (row->ends_in_ellipsis_p && glyph == end)
10996 /* Scan back over the ellipsis glyphs, decrementing positions. */
10997 while (glyph > row->glyphs[TEXT_AREA]
10998 && (glyph - 1)->charpos == last_pos)
10999 glyph--, x -= glyph->pixel_width;
11000 /* That loop always goes one position too far,
11001 including the glyph before the ellipsis.
11002 So scan forward over that one. */
11003 x += glyph->pixel_width;
11004 glyph++;
11006 else if (string_start
11007 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11009 /* We may have skipped over point because the previous glyphs
11010 are from string. As there's no easy way to know the
11011 character position of the current glyph, find the correct
11012 glyph on point by scanning from string_start again. */
11013 Lisp_Object limit;
11014 Lisp_Object string;
11015 int pos;
11017 limit = make_number (pt_old + 1);
11018 end = glyph;
11019 glyph = string_start;
11020 x = string_start_x;
11021 string = glyph->object;
11022 pos = string_buffer_position (w, string, string_before_pos);
11023 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11024 because we always put cursor after overlay strings. */
11025 while (pos == 0 && glyph < end)
11027 string = glyph->object;
11028 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11029 if (glyph < end)
11030 pos = string_buffer_position (w, glyph->object, string_before_pos);
11033 while (glyph < end)
11035 pos = XINT (Fnext_single_char_property_change
11036 (make_number (pos), Qdisplay, Qnil, limit));
11037 if (pos > pt_old)
11038 break;
11039 /* Skip glyphs from the same string. */
11040 string = glyph->object;
11041 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11042 /* Skip glyphs from an overlay. */
11043 while (glyph < end
11044 && ! string_buffer_position (w, glyph->object, pos))
11046 string = glyph->object;
11047 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11052 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11053 w->cursor.x = x;
11054 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11055 w->cursor.y = row->y + dy;
11057 if (w == XWINDOW (selected_window))
11059 if (!row->continued_p
11060 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11061 && row->x == 0)
11063 this_line_buffer = XBUFFER (w->buffer);
11065 CHARPOS (this_line_start_pos)
11066 = MATRIX_ROW_START_CHARPOS (row) + delta;
11067 BYTEPOS (this_line_start_pos)
11068 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11070 CHARPOS (this_line_end_pos)
11071 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11072 BYTEPOS (this_line_end_pos)
11073 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11075 this_line_y = w->cursor.y;
11076 this_line_pixel_height = row->height;
11077 this_line_vpos = w->cursor.vpos;
11078 this_line_start_x = row->x;
11080 else
11081 CHARPOS (this_line_start_pos) = 0;
11086 /* Run window scroll functions, if any, for WINDOW with new window
11087 start STARTP. Sets the window start of WINDOW to that position.
11089 We assume that the window's buffer is really current. */
11091 static INLINE struct text_pos
11092 run_window_scroll_functions (window, startp)
11093 Lisp_Object window;
11094 struct text_pos startp;
11096 struct window *w = XWINDOW (window);
11097 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11099 if (current_buffer != XBUFFER (w->buffer))
11100 abort ();
11102 if (!NILP (Vwindow_scroll_functions))
11104 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11105 make_number (CHARPOS (startp)));
11106 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11107 /* In case the hook functions switch buffers. */
11108 if (current_buffer != XBUFFER (w->buffer))
11109 set_buffer_internal_1 (XBUFFER (w->buffer));
11112 return startp;
11116 /* Make sure the line containing the cursor is fully visible.
11117 A value of 1 means there is nothing to be done.
11118 (Either the line is fully visible, or it cannot be made so,
11119 or we cannot tell.)
11121 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11122 is higher than window.
11124 A value of 0 means the caller should do scrolling
11125 as if point had gone off the screen. */
11127 static int
11128 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11129 struct window *w;
11130 int force_p;
11132 struct glyph_matrix *matrix;
11133 struct glyph_row *row;
11134 int window_height;
11136 if (!make_cursor_line_fully_visible_p)
11137 return 1;
11139 /* It's not always possible to find the cursor, e.g, when a window
11140 is full of overlay strings. Don't do anything in that case. */
11141 if (w->cursor.vpos < 0)
11142 return 1;
11144 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11145 row = MATRIX_ROW (matrix, w->cursor.vpos);
11147 /* If the cursor row is not partially visible, there's nothing to do. */
11148 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11149 return 1;
11151 /* If the row the cursor is in is taller than the window's height,
11152 it's not clear what to do, so do nothing. */
11153 window_height = window_box_height (w);
11154 if (row->height >= window_height)
11156 if (!force_p || w->vscroll)
11157 return 1;
11159 return 0;
11161 #if 0
11162 /* This code used to try to scroll the window just enough to make
11163 the line visible. It returned 0 to say that the caller should
11164 allocate larger glyph matrices. */
11166 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11168 int dy = row->height - row->visible_height;
11169 w->vscroll = 0;
11170 w->cursor.y += dy;
11171 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11173 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11175 int dy = - (row->height - row->visible_height);
11176 w->vscroll = dy;
11177 w->cursor.y += dy;
11178 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11181 /* When we change the cursor y-position of the selected window,
11182 change this_line_y as well so that the display optimization for
11183 the cursor line of the selected window in redisplay_internal uses
11184 the correct y-position. */
11185 if (w == XWINDOW (selected_window))
11186 this_line_y = w->cursor.y;
11188 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11189 redisplay with larger matrices. */
11190 if (matrix->nrows < required_matrix_height (w))
11192 fonts_changed_p = 1;
11193 return 0;
11196 return 1;
11197 #endif /* 0 */
11201 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11202 non-zero means only WINDOW is redisplayed in redisplay_internal.
11203 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11204 in redisplay_window to bring a partially visible line into view in
11205 the case that only the cursor has moved.
11207 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11208 last screen line's vertical height extends past the end of the screen.
11210 Value is
11212 1 if scrolling succeeded
11214 0 if scrolling didn't find point.
11216 -1 if new fonts have been loaded so that we must interrupt
11217 redisplay, adjust glyph matrices, and try again. */
11219 enum
11221 SCROLLING_SUCCESS,
11222 SCROLLING_FAILED,
11223 SCROLLING_NEED_LARGER_MATRICES
11226 static int
11227 try_scrolling (window, just_this_one_p, scroll_conservatively,
11228 scroll_step, temp_scroll_step, last_line_misfit)
11229 Lisp_Object window;
11230 int just_this_one_p;
11231 EMACS_INT scroll_conservatively, scroll_step;
11232 int temp_scroll_step;
11233 int last_line_misfit;
11235 struct window *w = XWINDOW (window);
11236 struct frame *f = XFRAME (w->frame);
11237 struct text_pos scroll_margin_pos;
11238 struct text_pos pos;
11239 struct text_pos startp;
11240 struct it it;
11241 Lisp_Object window_end;
11242 int this_scroll_margin;
11243 int dy = 0;
11244 int scroll_max;
11245 int rc;
11246 int amount_to_scroll = 0;
11247 Lisp_Object aggressive;
11248 int height;
11249 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11251 #if GLYPH_DEBUG
11252 debug_method_add (w, "try_scrolling");
11253 #endif
11255 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11257 /* Compute scroll margin height in pixels. We scroll when point is
11258 within this distance from the top or bottom of the window. */
11259 if (scroll_margin > 0)
11261 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11262 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11264 else
11265 this_scroll_margin = 0;
11267 /* Force scroll_conservatively to have a reasonable value so it doesn't
11268 cause an overflow while computing how much to scroll. */
11269 if (scroll_conservatively)
11270 scroll_conservatively = min (scroll_conservatively,
11271 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11273 /* Compute how much we should try to scroll maximally to bring point
11274 into view. */
11275 if (scroll_step || scroll_conservatively || temp_scroll_step)
11276 scroll_max = max (scroll_step,
11277 max (scroll_conservatively, temp_scroll_step));
11278 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11279 || NUMBERP (current_buffer->scroll_up_aggressively))
11280 /* We're trying to scroll because of aggressive scrolling
11281 but no scroll_step is set. Choose an arbitrary one. Maybe
11282 there should be a variable for this. */
11283 scroll_max = 10;
11284 else
11285 scroll_max = 0;
11286 scroll_max *= FRAME_LINE_HEIGHT (f);
11288 /* Decide whether we have to scroll down. Start at the window end
11289 and move this_scroll_margin up to find the position of the scroll
11290 margin. */
11291 window_end = Fwindow_end (window, Qt);
11293 too_near_end:
11295 CHARPOS (scroll_margin_pos) = XINT (window_end);
11296 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11298 if (this_scroll_margin || extra_scroll_margin_lines)
11300 start_display (&it, w, scroll_margin_pos);
11301 if (this_scroll_margin)
11302 move_it_vertically_backward (&it, this_scroll_margin);
11303 if (extra_scroll_margin_lines)
11304 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11305 scroll_margin_pos = it.current.pos;
11308 if (PT >= CHARPOS (scroll_margin_pos))
11310 int y0;
11312 /* Point is in the scroll margin at the bottom of the window, or
11313 below. Compute a new window start that makes point visible. */
11315 /* Compute the distance from the scroll margin to PT.
11316 Give up if the distance is greater than scroll_max. */
11317 start_display (&it, w, scroll_margin_pos);
11318 y0 = it.current_y;
11319 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11320 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11322 /* To make point visible, we have to move the window start
11323 down so that the line the cursor is in is visible, which
11324 means we have to add in the height of the cursor line. */
11325 dy = line_bottom_y (&it) - y0;
11327 if (dy > scroll_max)
11328 return SCROLLING_FAILED;
11330 /* Move the window start down. If scrolling conservatively,
11331 move it just enough down to make point visible. If
11332 scroll_step is set, move it down by scroll_step. */
11333 start_display (&it, w, startp);
11335 if (scroll_conservatively)
11336 /* Set AMOUNT_TO_SCROLL to at least one line,
11337 and at most scroll_conservatively lines. */
11338 amount_to_scroll
11339 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11340 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11341 else if (scroll_step || temp_scroll_step)
11342 amount_to_scroll = scroll_max;
11343 else
11345 aggressive = current_buffer->scroll_up_aggressively;
11346 height = WINDOW_BOX_TEXT_HEIGHT (w);
11347 if (NUMBERP (aggressive))
11349 double float_amount = XFLOATINT (aggressive) * height;
11350 amount_to_scroll = float_amount;
11351 if (amount_to_scroll == 0 && float_amount > 0)
11352 amount_to_scroll = 1;
11356 if (amount_to_scroll <= 0)
11357 return SCROLLING_FAILED;
11359 /* If moving by amount_to_scroll leaves STARTP unchanged,
11360 move it down one screen line. */
11362 move_it_vertically (&it, amount_to_scroll);
11363 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11364 move_it_by_lines (&it, 1, 1);
11365 startp = it.current.pos;
11367 else
11369 /* See if point is inside the scroll margin at the top of the
11370 window. */
11371 scroll_margin_pos = startp;
11372 if (this_scroll_margin)
11374 start_display (&it, w, startp);
11375 move_it_vertically (&it, this_scroll_margin);
11376 scroll_margin_pos = it.current.pos;
11379 if (PT < CHARPOS (scroll_margin_pos))
11381 /* Point is in the scroll margin at the top of the window or
11382 above what is displayed in the window. */
11383 int y0;
11385 /* Compute the vertical distance from PT to the scroll
11386 margin position. Give up if distance is greater than
11387 scroll_max. */
11388 SET_TEXT_POS (pos, PT, PT_BYTE);
11389 start_display (&it, w, pos);
11390 y0 = it.current_y;
11391 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11392 it.last_visible_y, -1,
11393 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11394 dy = it.current_y - y0;
11395 if (dy > scroll_max)
11396 return SCROLLING_FAILED;
11398 /* Compute new window start. */
11399 start_display (&it, w, startp);
11401 if (scroll_conservatively)
11402 amount_to_scroll
11403 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11404 else if (scroll_step || temp_scroll_step)
11405 amount_to_scroll = scroll_max;
11406 else
11408 aggressive = current_buffer->scroll_down_aggressively;
11409 height = WINDOW_BOX_TEXT_HEIGHT (w);
11410 if (NUMBERP (aggressive))
11412 double float_amount = XFLOATINT (aggressive) * height;
11413 amount_to_scroll = float_amount;
11414 if (amount_to_scroll == 0 && float_amount > 0)
11415 amount_to_scroll = 1;
11419 if (amount_to_scroll <= 0)
11420 return SCROLLING_FAILED;
11422 move_it_vertically_backward (&it, amount_to_scroll);
11423 startp = it.current.pos;
11427 /* Run window scroll functions. */
11428 startp = run_window_scroll_functions (window, startp);
11430 /* Display the window. Give up if new fonts are loaded, or if point
11431 doesn't appear. */
11432 if (!try_window (window, startp))
11433 rc = SCROLLING_NEED_LARGER_MATRICES;
11434 else if (w->cursor.vpos < 0)
11436 clear_glyph_matrix (w->desired_matrix);
11437 rc = SCROLLING_FAILED;
11439 else
11441 /* Maybe forget recorded base line for line number display. */
11442 if (!just_this_one_p
11443 || current_buffer->clip_changed
11444 || BEG_UNCHANGED < CHARPOS (startp))
11445 w->base_line_number = Qnil;
11447 /* If cursor ends up on a partially visible line,
11448 treat that as being off the bottom of the screen. */
11449 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11451 clear_glyph_matrix (w->desired_matrix);
11452 ++extra_scroll_margin_lines;
11453 goto too_near_end;
11455 rc = SCROLLING_SUCCESS;
11458 return rc;
11462 /* Compute a suitable window start for window W if display of W starts
11463 on a continuation line. Value is non-zero if a new window start
11464 was computed.
11466 The new window start will be computed, based on W's width, starting
11467 from the start of the continued line. It is the start of the
11468 screen line with the minimum distance from the old start W->start. */
11470 static int
11471 compute_window_start_on_continuation_line (w)
11472 struct window *w;
11474 struct text_pos pos, start_pos;
11475 int window_start_changed_p = 0;
11477 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11479 /* If window start is on a continuation line... Window start may be
11480 < BEGV in case there's invisible text at the start of the
11481 buffer (M-x rmail, for example). */
11482 if (CHARPOS (start_pos) > BEGV
11483 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11485 struct it it;
11486 struct glyph_row *row;
11488 /* Handle the case that the window start is out of range. */
11489 if (CHARPOS (start_pos) < BEGV)
11490 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11491 else if (CHARPOS (start_pos) > ZV)
11492 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11494 /* Find the start of the continued line. This should be fast
11495 because scan_buffer is fast (newline cache). */
11496 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11497 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11498 row, DEFAULT_FACE_ID);
11499 reseat_at_previous_visible_line_start (&it);
11501 /* If the line start is "too far" away from the window start,
11502 say it takes too much time to compute a new window start. */
11503 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11504 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11506 int min_distance, distance;
11508 /* Move forward by display lines to find the new window
11509 start. If window width was enlarged, the new start can
11510 be expected to be > the old start. If window width was
11511 decreased, the new window start will be < the old start.
11512 So, we're looking for the display line start with the
11513 minimum distance from the old window start. */
11514 pos = it.current.pos;
11515 min_distance = INFINITY;
11516 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11517 distance < min_distance)
11519 min_distance = distance;
11520 pos = it.current.pos;
11521 move_it_by_lines (&it, 1, 0);
11524 /* Set the window start there. */
11525 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11526 window_start_changed_p = 1;
11530 return window_start_changed_p;
11534 /* Try cursor movement in case text has not changed in window WINDOW,
11535 with window start STARTP. Value is
11537 CURSOR_MOVEMENT_SUCCESS if successful
11539 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11541 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11542 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11543 we want to scroll as if scroll-step were set to 1. See the code.
11545 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11546 which case we have to abort this redisplay, and adjust matrices
11547 first. */
11549 enum
11551 CURSOR_MOVEMENT_SUCCESS,
11552 CURSOR_MOVEMENT_CANNOT_BE_USED,
11553 CURSOR_MOVEMENT_MUST_SCROLL,
11554 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11557 static int
11558 try_cursor_movement (window, startp, scroll_step)
11559 Lisp_Object window;
11560 struct text_pos startp;
11561 int *scroll_step;
11563 struct window *w = XWINDOW (window);
11564 struct frame *f = XFRAME (w->frame);
11565 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11567 #if GLYPH_DEBUG
11568 if (inhibit_try_cursor_movement)
11569 return rc;
11570 #endif
11572 /* Handle case where text has not changed, only point, and it has
11573 not moved off the frame. */
11574 if (/* Point may be in this window. */
11575 PT >= CHARPOS (startp)
11576 /* Selective display hasn't changed. */
11577 && !current_buffer->clip_changed
11578 /* Function force-mode-line-update is used to force a thorough
11579 redisplay. It sets either windows_or_buffers_changed or
11580 update_mode_lines. So don't take a shortcut here for these
11581 cases. */
11582 && !update_mode_lines
11583 && !windows_or_buffers_changed
11584 && !cursor_type_changed
11585 /* Can't use this case if highlighting a region. When a
11586 region exists, cursor movement has to do more than just
11587 set the cursor. */
11588 && !(!NILP (Vtransient_mark_mode)
11589 && !NILP (current_buffer->mark_active))
11590 && NILP (w->region_showing)
11591 && NILP (Vshow_trailing_whitespace)
11592 /* Right after splitting windows, last_point may be nil. */
11593 && INTEGERP (w->last_point)
11594 /* This code is not used for mini-buffer for the sake of the case
11595 of redisplaying to replace an echo area message; since in
11596 that case the mini-buffer contents per se are usually
11597 unchanged. This code is of no real use in the mini-buffer
11598 since the handling of this_line_start_pos, etc., in redisplay
11599 handles the same cases. */
11600 && !EQ (window, minibuf_window)
11601 /* When splitting windows or for new windows, it happens that
11602 redisplay is called with a nil window_end_vpos or one being
11603 larger than the window. This should really be fixed in
11604 window.c. I don't have this on my list, now, so we do
11605 approximately the same as the old redisplay code. --gerd. */
11606 && INTEGERP (w->window_end_vpos)
11607 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11608 && (FRAME_WINDOW_P (f)
11609 || !overlay_arrow_in_current_buffer_p ()))
11611 int this_scroll_margin, top_scroll_margin;
11612 struct glyph_row *row = NULL;
11614 #if GLYPH_DEBUG
11615 debug_method_add (w, "cursor movement");
11616 #endif
11618 /* Scroll if point within this distance from the top or bottom
11619 of the window. This is a pixel value. */
11620 this_scroll_margin = max (0, scroll_margin);
11621 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11622 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11624 top_scroll_margin = this_scroll_margin;
11625 if (WINDOW_WANTS_HEADER_LINE_P (w))
11626 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11628 /* Start with the row the cursor was displayed during the last
11629 not paused redisplay. Give up if that row is not valid. */
11630 if (w->last_cursor.vpos < 0
11631 || w->last_cursor.vpos >= w->current_matrix->nrows)
11632 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11633 else
11635 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11636 if (row->mode_line_p)
11637 ++row;
11638 if (!row->enabled_p)
11639 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11642 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11644 int scroll_p = 0;
11645 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11647 if (PT > XFASTINT (w->last_point))
11649 /* Point has moved forward. */
11650 while (MATRIX_ROW_END_CHARPOS (row) < PT
11651 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11653 xassert (row->enabled_p);
11654 ++row;
11657 /* The end position of a row equals the start position
11658 of the next row. If PT is there, we would rather
11659 display it in the next line. */
11660 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11661 && MATRIX_ROW_END_CHARPOS (row) == PT
11662 && !cursor_row_p (w, row))
11663 ++row;
11665 /* If within the scroll margin, scroll. Note that
11666 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11667 the next line would be drawn, and that
11668 this_scroll_margin can be zero. */
11669 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11670 || PT > MATRIX_ROW_END_CHARPOS (row)
11671 /* Line is completely visible last line in window
11672 and PT is to be set in the next line. */
11673 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11674 && PT == MATRIX_ROW_END_CHARPOS (row)
11675 && !row->ends_at_zv_p
11676 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11677 scroll_p = 1;
11679 else if (PT < XFASTINT (w->last_point))
11681 /* Cursor has to be moved backward. Note that PT >=
11682 CHARPOS (startp) because of the outer if-statement. */
11683 while (!row->mode_line_p
11684 && (MATRIX_ROW_START_CHARPOS (row) > PT
11685 || (MATRIX_ROW_START_CHARPOS (row) == PT
11686 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11687 && (row->y > top_scroll_margin
11688 || CHARPOS (startp) == BEGV))
11690 xassert (row->enabled_p);
11691 --row;
11694 /* Consider the following case: Window starts at BEGV,
11695 there is invisible, intangible text at BEGV, so that
11696 display starts at some point START > BEGV. It can
11697 happen that we are called with PT somewhere between
11698 BEGV and START. Try to handle that case. */
11699 if (row < w->current_matrix->rows
11700 || row->mode_line_p)
11702 row = w->current_matrix->rows;
11703 if (row->mode_line_p)
11704 ++row;
11707 /* Due to newlines in overlay strings, we may have to
11708 skip forward over overlay strings. */
11709 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11710 && MATRIX_ROW_END_CHARPOS (row) == PT
11711 && !cursor_row_p (w, row))
11712 ++row;
11714 /* If within the scroll margin, scroll. */
11715 if (row->y < top_scroll_margin
11716 && CHARPOS (startp) != BEGV)
11717 scroll_p = 1;
11719 else
11721 /* Cursor did not move. So don't scroll even if cursor line
11722 is partially visible, as it was so before. */
11723 rc = CURSOR_MOVEMENT_SUCCESS;
11726 if (PT < MATRIX_ROW_START_CHARPOS (row)
11727 || PT > MATRIX_ROW_END_CHARPOS (row))
11729 /* if PT is not in the glyph row, give up. */
11730 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11732 else if (rc != CURSOR_MOVEMENT_SUCCESS
11733 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11734 && make_cursor_line_fully_visible_p)
11736 if (PT == MATRIX_ROW_END_CHARPOS (row)
11737 && !row->ends_at_zv_p
11738 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11739 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11740 else if (row->height > window_box_height (w))
11742 /* If we end up in a partially visible line, let's
11743 make it fully visible, except when it's taller
11744 than the window, in which case we can't do much
11745 about it. */
11746 *scroll_step = 1;
11747 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11749 else
11751 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11752 if (!cursor_row_fully_visible_p (w, 0, 1))
11753 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11754 else
11755 rc = CURSOR_MOVEMENT_SUCCESS;
11758 else if (scroll_p)
11759 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11760 else
11762 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11763 rc = CURSOR_MOVEMENT_SUCCESS;
11768 return rc;
11771 void
11772 set_vertical_scroll_bar (w)
11773 struct window *w;
11775 int start, end, whole;
11777 /* Calculate the start and end positions for the current window.
11778 At some point, it would be nice to choose between scrollbars
11779 which reflect the whole buffer size, with special markers
11780 indicating narrowing, and scrollbars which reflect only the
11781 visible region.
11783 Note that mini-buffers sometimes aren't displaying any text. */
11784 if (!MINI_WINDOW_P (w)
11785 || (w == XWINDOW (minibuf_window)
11786 && NILP (echo_area_buffer[0])))
11788 struct buffer *buf = XBUFFER (w->buffer);
11789 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11790 start = marker_position (w->start) - BUF_BEGV (buf);
11791 /* I don't think this is guaranteed to be right. For the
11792 moment, we'll pretend it is. */
11793 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11795 if (end < start)
11796 end = start;
11797 if (whole < (end - start))
11798 whole = end - start;
11800 else
11801 start = end = whole = 0;
11803 /* Indicate what this scroll bar ought to be displaying now. */
11804 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11808 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11809 selected_window is redisplayed.
11811 We can return without actually redisplaying the window if
11812 fonts_changed_p is nonzero. In that case, redisplay_internal will
11813 retry. */
11815 static void
11816 redisplay_window (window, just_this_one_p)
11817 Lisp_Object window;
11818 int just_this_one_p;
11820 struct window *w = XWINDOW (window);
11821 struct frame *f = XFRAME (w->frame);
11822 struct buffer *buffer = XBUFFER (w->buffer);
11823 struct buffer *old = current_buffer;
11824 struct text_pos lpoint, opoint, startp;
11825 int update_mode_line;
11826 int tem;
11827 struct it it;
11828 /* Record it now because it's overwritten. */
11829 int current_matrix_up_to_date_p = 0;
11830 int used_current_matrix_p = 0;
11831 /* This is less strict than current_matrix_up_to_date_p.
11832 It indictes that the buffer contents and narrowing are unchanged. */
11833 int buffer_unchanged_p = 0;
11834 int temp_scroll_step = 0;
11835 int count = SPECPDL_INDEX ();
11836 int rc;
11837 int centering_position = -1;
11838 int last_line_misfit = 0;
11840 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11841 opoint = lpoint;
11843 /* W must be a leaf window here. */
11844 xassert (!NILP (w->buffer));
11845 #if GLYPH_DEBUG
11846 *w->desired_matrix->method = 0;
11847 #endif
11849 specbind (Qinhibit_point_motion_hooks, Qt);
11851 reconsider_clip_changes (w, buffer);
11853 /* Has the mode line to be updated? */
11854 update_mode_line = (!NILP (w->update_mode_line)
11855 || update_mode_lines
11856 || buffer->clip_changed
11857 || buffer->prevent_redisplay_optimizations_p);
11859 if (MINI_WINDOW_P (w))
11861 if (w == XWINDOW (echo_area_window)
11862 && !NILP (echo_area_buffer[0]))
11864 if (update_mode_line)
11865 /* We may have to update a tty frame's menu bar or a
11866 tool-bar. Example `M-x C-h C-h C-g'. */
11867 goto finish_menu_bars;
11868 else
11869 /* We've already displayed the echo area glyphs in this window. */
11870 goto finish_scroll_bars;
11872 else if ((w != XWINDOW (minibuf_window)
11873 || minibuf_level == 0)
11874 /* When buffer is nonempty, redisplay window normally. */
11875 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11876 /* Quail displays non-mini buffers in minibuffer window.
11877 In that case, redisplay the window normally. */
11878 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11880 /* W is a mini-buffer window, but it's not active, so clear
11881 it. */
11882 int yb = window_text_bottom_y (w);
11883 struct glyph_row *row;
11884 int y;
11886 for (y = 0, row = w->desired_matrix->rows;
11887 y < yb;
11888 y += row->height, ++row)
11889 blank_row (w, row, y);
11890 goto finish_scroll_bars;
11893 clear_glyph_matrix (w->desired_matrix);
11896 /* Otherwise set up data on this window; select its buffer and point
11897 value. */
11898 /* Really select the buffer, for the sake of buffer-local
11899 variables. */
11900 set_buffer_internal_1 (XBUFFER (w->buffer));
11901 SET_TEXT_POS (opoint, PT, PT_BYTE);
11903 current_matrix_up_to_date_p
11904 = (!NILP (w->window_end_valid)
11905 && !current_buffer->clip_changed
11906 && !current_buffer->prevent_redisplay_optimizations_p
11907 && XFASTINT (w->last_modified) >= MODIFF
11908 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11910 buffer_unchanged_p
11911 = (!NILP (w->window_end_valid)
11912 && !current_buffer->clip_changed
11913 && XFASTINT (w->last_modified) >= MODIFF
11914 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11916 /* When windows_or_buffers_changed is non-zero, we can't rely on
11917 the window end being valid, so set it to nil there. */
11918 if (windows_or_buffers_changed)
11920 /* If window starts on a continuation line, maybe adjust the
11921 window start in case the window's width changed. */
11922 if (XMARKER (w->start)->buffer == current_buffer)
11923 compute_window_start_on_continuation_line (w);
11925 w->window_end_valid = Qnil;
11928 /* Some sanity checks. */
11929 CHECK_WINDOW_END (w);
11930 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11931 abort ();
11932 if (BYTEPOS (opoint) < CHARPOS (opoint))
11933 abort ();
11935 /* If %c is in mode line, update it if needed. */
11936 if (!NILP (w->column_number_displayed)
11937 /* This alternative quickly identifies a common case
11938 where no change is needed. */
11939 && !(PT == XFASTINT (w->last_point)
11940 && XFASTINT (w->last_modified) >= MODIFF
11941 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11942 && (XFASTINT (w->column_number_displayed)
11943 != (int) current_column ())) /* iftc */
11944 update_mode_line = 1;
11946 /* Count number of windows showing the selected buffer. An indirect
11947 buffer counts as its base buffer. */
11948 if (!just_this_one_p)
11950 struct buffer *current_base, *window_base;
11951 current_base = current_buffer;
11952 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11953 if (current_base->base_buffer)
11954 current_base = current_base->base_buffer;
11955 if (window_base->base_buffer)
11956 window_base = window_base->base_buffer;
11957 if (current_base == window_base)
11958 buffer_shared++;
11961 /* Point refers normally to the selected window. For any other
11962 window, set up appropriate value. */
11963 if (!EQ (window, selected_window))
11965 int new_pt = XMARKER (w->pointm)->charpos;
11966 int new_pt_byte = marker_byte_position (w->pointm);
11967 if (new_pt < BEGV)
11969 new_pt = BEGV;
11970 new_pt_byte = BEGV_BYTE;
11971 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11973 else if (new_pt > (ZV - 1))
11975 new_pt = ZV;
11976 new_pt_byte = ZV_BYTE;
11977 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11980 /* We don't use SET_PT so that the point-motion hooks don't run. */
11981 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11984 /* If any of the character widths specified in the display table
11985 have changed, invalidate the width run cache. It's true that
11986 this may be a bit late to catch such changes, but the rest of
11987 redisplay goes (non-fatally) haywire when the display table is
11988 changed, so why should we worry about doing any better? */
11989 if (current_buffer->width_run_cache)
11991 struct Lisp_Char_Table *disptab = buffer_display_table ();
11993 if (! disptab_matches_widthtab (disptab,
11994 XVECTOR (current_buffer->width_table)))
11996 invalidate_region_cache (current_buffer,
11997 current_buffer->width_run_cache,
11998 BEG, Z);
11999 recompute_width_table (current_buffer, disptab);
12003 /* If window-start is screwed up, choose a new one. */
12004 if (XMARKER (w->start)->buffer != current_buffer)
12005 goto recenter;
12007 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12009 /* If someone specified a new starting point but did not insist,
12010 check whether it can be used. */
12011 if (!NILP (w->optional_new_start)
12012 && CHARPOS (startp) >= BEGV
12013 && CHARPOS (startp) <= ZV)
12015 w->optional_new_start = Qnil;
12016 start_display (&it, w, startp);
12017 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12018 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12019 if (IT_CHARPOS (it) == PT)
12020 w->force_start = Qt;
12021 /* IT may overshoot PT if text at PT is invisible. */
12022 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12023 w->force_start = Qt;
12028 /* Handle case where place to start displaying has been specified,
12029 unless the specified location is outside the accessible range. */
12030 if (!NILP (w->force_start)
12031 || w->frozen_window_start_p)
12033 /* We set this later on if we have to adjust point. */
12034 int new_vpos = -1;
12036 w->force_start = Qnil;
12037 w->vscroll = 0;
12038 w->window_end_valid = Qnil;
12040 /* Forget any recorded base line for line number display. */
12041 if (!buffer_unchanged_p)
12042 w->base_line_number = Qnil;
12044 /* Redisplay the mode line. Select the buffer properly for that.
12045 Also, run the hook window-scroll-functions
12046 because we have scrolled. */
12047 /* Note, we do this after clearing force_start because
12048 if there's an error, it is better to forget about force_start
12049 than to get into an infinite loop calling the hook functions
12050 and having them get more errors. */
12051 if (!update_mode_line
12052 || ! NILP (Vwindow_scroll_functions))
12054 update_mode_line = 1;
12055 w->update_mode_line = Qt;
12056 startp = run_window_scroll_functions (window, startp);
12059 w->last_modified = make_number (0);
12060 w->last_overlay_modified = make_number (0);
12061 if (CHARPOS (startp) < BEGV)
12062 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12063 else if (CHARPOS (startp) > ZV)
12064 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12066 /* Redisplay, then check if cursor has been set during the
12067 redisplay. Give up if new fonts were loaded. */
12068 if (!try_window (window, startp))
12070 w->force_start = Qt;
12071 clear_glyph_matrix (w->desired_matrix);
12072 goto need_larger_matrices;
12075 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12077 /* If point does not appear, try to move point so it does
12078 appear. The desired matrix has been built above, so we
12079 can use it here. */
12080 new_vpos = window_box_height (w) / 2;
12083 if (!cursor_row_fully_visible_p (w, 0, 0))
12085 /* Point does appear, but on a line partly visible at end of window.
12086 Move it back to a fully-visible line. */
12087 new_vpos = window_box_height (w);
12090 /* If we need to move point for either of the above reasons,
12091 now actually do it. */
12092 if (new_vpos >= 0)
12094 struct glyph_row *row;
12096 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12097 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12098 ++row;
12100 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12101 MATRIX_ROW_START_BYTEPOS (row));
12103 if (w != XWINDOW (selected_window))
12104 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12105 else if (current_buffer == old)
12106 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12108 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12110 /* If we are highlighting the region, then we just changed
12111 the region, so redisplay to show it. */
12112 if (!NILP (Vtransient_mark_mode)
12113 && !NILP (current_buffer->mark_active))
12115 clear_glyph_matrix (w->desired_matrix);
12116 if (!try_window (window, startp))
12117 goto need_larger_matrices;
12121 #if GLYPH_DEBUG
12122 debug_method_add (w, "forced window start");
12123 #endif
12124 goto done;
12127 /* Handle case where text has not changed, only point, and it has
12128 not moved off the frame, and we are not retrying after hscroll.
12129 (current_matrix_up_to_date_p is nonzero when retrying.) */
12130 if (current_matrix_up_to_date_p
12131 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12132 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12134 switch (rc)
12136 case CURSOR_MOVEMENT_SUCCESS:
12137 used_current_matrix_p = 1;
12138 goto done;
12140 #if 0 /* try_cursor_movement never returns this value. */
12141 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12142 goto need_larger_matrices;
12143 #endif
12145 case CURSOR_MOVEMENT_MUST_SCROLL:
12146 goto try_to_scroll;
12148 default:
12149 abort ();
12152 /* If current starting point was originally the beginning of a line
12153 but no longer is, find a new starting point. */
12154 else if (!NILP (w->start_at_line_beg)
12155 && !(CHARPOS (startp) <= BEGV
12156 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12158 #if GLYPH_DEBUG
12159 debug_method_add (w, "recenter 1");
12160 #endif
12161 goto recenter;
12164 /* Try scrolling with try_window_id. Value is > 0 if update has
12165 been done, it is -1 if we know that the same window start will
12166 not work. It is 0 if unsuccessful for some other reason. */
12167 else if ((tem = try_window_id (w)) != 0)
12169 #if GLYPH_DEBUG
12170 debug_method_add (w, "try_window_id %d", tem);
12171 #endif
12173 if (fonts_changed_p)
12174 goto need_larger_matrices;
12175 if (tem > 0)
12176 goto done;
12178 /* Otherwise try_window_id has returned -1 which means that we
12179 don't want the alternative below this comment to execute. */
12181 else if (CHARPOS (startp) >= BEGV
12182 && CHARPOS (startp) <= ZV
12183 && PT >= CHARPOS (startp)
12184 && (CHARPOS (startp) < ZV
12185 /* Avoid starting at end of buffer. */
12186 || CHARPOS (startp) == BEGV
12187 || (XFASTINT (w->last_modified) >= MODIFF
12188 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12190 #if GLYPH_DEBUG
12191 debug_method_add (w, "same window start");
12192 #endif
12194 /* Try to redisplay starting at same place as before.
12195 If point has not moved off frame, accept the results. */
12196 if (!current_matrix_up_to_date_p
12197 /* Don't use try_window_reusing_current_matrix in this case
12198 because a window scroll function can have changed the
12199 buffer. */
12200 || !NILP (Vwindow_scroll_functions)
12201 || MINI_WINDOW_P (w)
12202 || !(used_current_matrix_p
12203 = try_window_reusing_current_matrix (w)))
12205 IF_DEBUG (debug_method_add (w, "1"));
12206 try_window (window, startp);
12209 if (fonts_changed_p)
12210 goto need_larger_matrices;
12212 if (w->cursor.vpos >= 0)
12214 if (!just_this_one_p
12215 || current_buffer->clip_changed
12216 || BEG_UNCHANGED < CHARPOS (startp))
12217 /* Forget any recorded base line for line number display. */
12218 w->base_line_number = Qnil;
12220 if (!cursor_row_fully_visible_p (w, 1, 0))
12222 clear_glyph_matrix (w->desired_matrix);
12223 last_line_misfit = 1;
12225 /* Drop through and scroll. */
12226 else
12227 goto done;
12229 else
12230 clear_glyph_matrix (w->desired_matrix);
12233 try_to_scroll:
12235 w->last_modified = make_number (0);
12236 w->last_overlay_modified = make_number (0);
12238 /* Redisplay the mode line. Select the buffer properly for that. */
12239 if (!update_mode_line)
12241 update_mode_line = 1;
12242 w->update_mode_line = Qt;
12245 /* Try to scroll by specified few lines. */
12246 if ((scroll_conservatively
12247 || scroll_step
12248 || temp_scroll_step
12249 || NUMBERP (current_buffer->scroll_up_aggressively)
12250 || NUMBERP (current_buffer->scroll_down_aggressively))
12251 && !current_buffer->clip_changed
12252 && CHARPOS (startp) >= BEGV
12253 && CHARPOS (startp) <= ZV)
12255 /* The function returns -1 if new fonts were loaded, 1 if
12256 successful, 0 if not successful. */
12257 int rc = try_scrolling (window, just_this_one_p,
12258 scroll_conservatively,
12259 scroll_step,
12260 temp_scroll_step, last_line_misfit);
12261 switch (rc)
12263 case SCROLLING_SUCCESS:
12264 goto done;
12266 case SCROLLING_NEED_LARGER_MATRICES:
12267 goto need_larger_matrices;
12269 case SCROLLING_FAILED:
12270 break;
12272 default:
12273 abort ();
12277 /* Finally, just choose place to start which centers point */
12279 recenter:
12280 if (centering_position < 0)
12281 centering_position = window_box_height (w) / 2;
12283 #if GLYPH_DEBUG
12284 debug_method_add (w, "recenter");
12285 #endif
12287 /* w->vscroll = 0; */
12289 /* Forget any previously recorded base line for line number display. */
12290 if (!buffer_unchanged_p)
12291 w->base_line_number = Qnil;
12293 /* Move backward half the height of the window. */
12294 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12295 it.current_y = it.last_visible_y;
12296 move_it_vertically_backward (&it, centering_position);
12297 xassert (IT_CHARPOS (it) >= BEGV);
12299 /* The function move_it_vertically_backward may move over more
12300 than the specified y-distance. If it->w is small, e.g. a
12301 mini-buffer window, we may end up in front of the window's
12302 display area. Start displaying at the start of the line
12303 containing PT in this case. */
12304 if (it.current_y <= 0)
12306 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12307 move_it_vertically_backward (&it, 0);
12308 #if 0
12309 /* I think this assert is bogus if buffer contains
12310 invisible text or images. KFS. */
12311 xassert (IT_CHARPOS (it) <= PT);
12312 #endif
12313 it.current_y = 0;
12316 it.current_x = it.hpos = 0;
12318 /* Set startp here explicitly in case that helps avoid an infinite loop
12319 in case the window-scroll-functions functions get errors. */
12320 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12322 /* Run scroll hooks. */
12323 startp = run_window_scroll_functions (window, it.current.pos);
12325 /* Redisplay the window. */
12326 if (!current_matrix_up_to_date_p
12327 || windows_or_buffers_changed
12328 || cursor_type_changed
12329 /* Don't use try_window_reusing_current_matrix in this case
12330 because it can have changed the buffer. */
12331 || !NILP (Vwindow_scroll_functions)
12332 || !just_this_one_p
12333 || MINI_WINDOW_P (w)
12334 || !(used_current_matrix_p
12335 = try_window_reusing_current_matrix (w)))
12336 try_window (window, startp);
12338 /* If new fonts have been loaded (due to fontsets), give up. We
12339 have to start a new redisplay since we need to re-adjust glyph
12340 matrices. */
12341 if (fonts_changed_p)
12342 goto need_larger_matrices;
12344 /* If cursor did not appear assume that the middle of the window is
12345 in the first line of the window. Do it again with the next line.
12346 (Imagine a window of height 100, displaying two lines of height
12347 60. Moving back 50 from it->last_visible_y will end in the first
12348 line.) */
12349 if (w->cursor.vpos < 0)
12351 if (!NILP (w->window_end_valid)
12352 && PT >= Z - XFASTINT (w->window_end_pos))
12354 clear_glyph_matrix (w->desired_matrix);
12355 move_it_by_lines (&it, 1, 0);
12356 try_window (window, it.current.pos);
12358 else if (PT < IT_CHARPOS (it))
12360 clear_glyph_matrix (w->desired_matrix);
12361 move_it_by_lines (&it, -1, 0);
12362 try_window (window, it.current.pos);
12364 else
12366 /* Not much we can do about it. */
12370 /* Consider the following case: Window starts at BEGV, there is
12371 invisible, intangible text at BEGV, so that display starts at
12372 some point START > BEGV. It can happen that we are called with
12373 PT somewhere between BEGV and START. Try to handle that case. */
12374 if (w->cursor.vpos < 0)
12376 struct glyph_row *row = w->current_matrix->rows;
12377 if (row->mode_line_p)
12378 ++row;
12379 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12382 if (!cursor_row_fully_visible_p (w, 0, 0))
12384 /* If vscroll is enabled, disable it and try again. */
12385 if (w->vscroll)
12387 w->vscroll = 0;
12388 clear_glyph_matrix (w->desired_matrix);
12389 goto recenter;
12392 /* If centering point failed to make the whole line visible,
12393 put point at the top instead. That has to make the whole line
12394 visible, if it can be done. */
12395 if (centering_position == 0)
12396 goto done;
12398 clear_glyph_matrix (w->desired_matrix);
12399 centering_position = 0;
12400 goto recenter;
12403 done:
12405 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12406 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12407 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12408 ? Qt : Qnil);
12410 /* Display the mode line, if we must. */
12411 if ((update_mode_line
12412 /* If window not full width, must redo its mode line
12413 if (a) the window to its side is being redone and
12414 (b) we do a frame-based redisplay. This is a consequence
12415 of how inverted lines are drawn in frame-based redisplay. */
12416 || (!just_this_one_p
12417 && !FRAME_WINDOW_P (f)
12418 && !WINDOW_FULL_WIDTH_P (w))
12419 /* Line number to display. */
12420 || INTEGERP (w->base_line_pos)
12421 /* Column number is displayed and different from the one displayed. */
12422 || (!NILP (w->column_number_displayed)
12423 && (XFASTINT (w->column_number_displayed)
12424 != (int) current_column ()))) /* iftc */
12425 /* This means that the window has a mode line. */
12426 && (WINDOW_WANTS_MODELINE_P (w)
12427 || WINDOW_WANTS_HEADER_LINE_P (w)))
12429 display_mode_lines (w);
12431 /* If mode line height has changed, arrange for a thorough
12432 immediate redisplay using the correct mode line height. */
12433 if (WINDOW_WANTS_MODELINE_P (w)
12434 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12436 fonts_changed_p = 1;
12437 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12438 = DESIRED_MODE_LINE_HEIGHT (w);
12441 /* If top line height has changed, arrange for a thorough
12442 immediate redisplay using the correct mode line height. */
12443 if (WINDOW_WANTS_HEADER_LINE_P (w)
12444 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12446 fonts_changed_p = 1;
12447 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12448 = DESIRED_HEADER_LINE_HEIGHT (w);
12451 if (fonts_changed_p)
12452 goto need_larger_matrices;
12455 if (!line_number_displayed
12456 && !BUFFERP (w->base_line_pos))
12458 w->base_line_pos = Qnil;
12459 w->base_line_number = Qnil;
12462 finish_menu_bars:
12464 /* When we reach a frame's selected window, redo the frame's menu bar. */
12465 if (update_mode_line
12466 && EQ (FRAME_SELECTED_WINDOW (f), window))
12468 int redisplay_menu_p = 0;
12469 int redisplay_tool_bar_p = 0;
12471 if (FRAME_WINDOW_P (f))
12473 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12474 || defined (USE_GTK)
12475 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12476 #else
12477 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12478 #endif
12480 else
12481 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12483 if (redisplay_menu_p)
12484 display_menu_bar (w);
12486 #ifdef HAVE_WINDOW_SYSTEM
12487 #ifdef USE_GTK
12488 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12489 #else
12490 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12491 && (FRAME_TOOL_BAR_LINES (f) > 0
12492 || auto_resize_tool_bars_p);
12494 #endif
12496 if (redisplay_tool_bar_p)
12497 redisplay_tool_bar (f);
12498 #endif
12501 #ifdef HAVE_WINDOW_SYSTEM
12502 if (FRAME_WINDOW_P (f)
12503 && update_window_fringes (w, 0)
12504 && !just_this_one_p
12505 && (used_current_matrix_p || overlay_arrow_seen)
12506 && !w->pseudo_window_p)
12508 update_begin (f);
12509 BLOCK_INPUT;
12510 if (draw_window_fringes (w, 1))
12511 x_draw_vertical_border (w);
12512 UNBLOCK_INPUT;
12513 update_end (f);
12515 #endif /* HAVE_WINDOW_SYSTEM */
12517 /* We go to this label, with fonts_changed_p nonzero,
12518 if it is necessary to try again using larger glyph matrices.
12519 We have to redeem the scroll bar even in this case,
12520 because the loop in redisplay_internal expects that. */
12521 need_larger_matrices:
12523 finish_scroll_bars:
12525 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12527 /* Set the thumb's position and size. */
12528 set_vertical_scroll_bar (w);
12530 /* Note that we actually used the scroll bar attached to this
12531 window, so it shouldn't be deleted at the end of redisplay. */
12532 redeem_scroll_bar_hook (w);
12535 /* Restore current_buffer and value of point in it. */
12536 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12537 set_buffer_internal_1 (old);
12538 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12540 unbind_to (count, Qnil);
12544 /* Build the complete desired matrix of WINDOW with a window start
12545 buffer position POS. Value is non-zero if successful. It is zero
12546 if fonts were loaded during redisplay which makes re-adjusting
12547 glyph matrices necessary. */
12550 try_window (window, pos)
12551 Lisp_Object window;
12552 struct text_pos pos;
12554 struct window *w = XWINDOW (window);
12555 struct it it;
12556 struct glyph_row *last_text_row = NULL;
12558 /* Make POS the new window start. */
12559 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12561 /* Mark cursor position as unknown. No overlay arrow seen. */
12562 w->cursor.vpos = -1;
12563 overlay_arrow_seen = 0;
12565 /* Initialize iterator and info to start at POS. */
12566 start_display (&it, w, pos);
12568 /* Display all lines of W. */
12569 while (it.current_y < it.last_visible_y)
12571 if (display_line (&it))
12572 last_text_row = it.glyph_row - 1;
12573 if (fonts_changed_p)
12574 return 0;
12577 /* If bottom moved off end of frame, change mode line percentage. */
12578 if (XFASTINT (w->window_end_pos) <= 0
12579 && Z != IT_CHARPOS (it))
12580 w->update_mode_line = Qt;
12582 /* Set window_end_pos to the offset of the last character displayed
12583 on the window from the end of current_buffer. Set
12584 window_end_vpos to its row number. */
12585 if (last_text_row)
12587 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12588 w->window_end_bytepos
12589 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12590 w->window_end_pos
12591 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12592 w->window_end_vpos
12593 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12594 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12595 ->displays_text_p);
12597 else
12599 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12600 w->window_end_pos = make_number (Z - ZV);
12601 w->window_end_vpos = make_number (0);
12604 /* But that is not valid info until redisplay finishes. */
12605 w->window_end_valid = Qnil;
12606 return 1;
12611 /************************************************************************
12612 Window redisplay reusing current matrix when buffer has not changed
12613 ************************************************************************/
12615 /* Try redisplay of window W showing an unchanged buffer with a
12616 different window start than the last time it was displayed by
12617 reusing its current matrix. Value is non-zero if successful.
12618 W->start is the new window start. */
12620 static int
12621 try_window_reusing_current_matrix (w)
12622 struct window *w;
12624 struct frame *f = XFRAME (w->frame);
12625 struct glyph_row *row, *bottom_row;
12626 struct it it;
12627 struct run run;
12628 struct text_pos start, new_start;
12629 int nrows_scrolled, i;
12630 struct glyph_row *last_text_row;
12631 struct glyph_row *last_reused_text_row;
12632 struct glyph_row *start_row;
12633 int start_vpos, min_y, max_y;
12635 #if GLYPH_DEBUG
12636 if (inhibit_try_window_reusing)
12637 return 0;
12638 #endif
12640 if (/* This function doesn't handle terminal frames. */
12641 !FRAME_WINDOW_P (f)
12642 /* Don't try to reuse the display if windows have been split
12643 or such. */
12644 || windows_or_buffers_changed
12645 || cursor_type_changed)
12646 return 0;
12648 /* Can't do this if region may have changed. */
12649 if ((!NILP (Vtransient_mark_mode)
12650 && !NILP (current_buffer->mark_active))
12651 || !NILP (w->region_showing)
12652 || !NILP (Vshow_trailing_whitespace))
12653 return 0;
12655 /* If top-line visibility has changed, give up. */
12656 if (WINDOW_WANTS_HEADER_LINE_P (w)
12657 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12658 return 0;
12660 /* Give up if old or new display is scrolled vertically. We could
12661 make this function handle this, but right now it doesn't. */
12662 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12663 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12664 return 0;
12666 /* The variable new_start now holds the new window start. The old
12667 start `start' can be determined from the current matrix. */
12668 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12669 start = start_row->start.pos;
12670 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12672 /* Clear the desired matrix for the display below. */
12673 clear_glyph_matrix (w->desired_matrix);
12675 if (CHARPOS (new_start) <= CHARPOS (start))
12677 int first_row_y;
12679 /* Don't use this method if the display starts with an ellipsis
12680 displayed for invisible text. It's not easy to handle that case
12681 below, and it's certainly not worth the effort since this is
12682 not a frequent case. */
12683 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12684 return 0;
12686 IF_DEBUG (debug_method_add (w, "twu1"));
12688 /* Display up to a row that can be reused. The variable
12689 last_text_row is set to the last row displayed that displays
12690 text. Note that it.vpos == 0 if or if not there is a
12691 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12692 start_display (&it, w, new_start);
12693 first_row_y = it.current_y;
12694 w->cursor.vpos = -1;
12695 last_text_row = last_reused_text_row = NULL;
12697 while (it.current_y < it.last_visible_y
12698 && !fonts_changed_p)
12700 /* If we have reached into the characters in the START row,
12701 that means the line boundaries have changed. So we
12702 can't start copying with the row START. Maybe it will
12703 work to start copying with the following row. */
12704 while (IT_CHARPOS (it) > CHARPOS (start))
12706 /* Advance to the next row as the "start". */
12707 start_row++;
12708 start = start_row->start.pos;
12709 /* If there are no more rows to try, or just one, give up. */
12710 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12711 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12712 || CHARPOS (start) == ZV)
12714 clear_glyph_matrix (w->desired_matrix);
12715 return 0;
12718 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12720 /* If we have reached alignment,
12721 we can copy the rest of the rows. */
12722 if (IT_CHARPOS (it) == CHARPOS (start))
12723 break;
12725 if (display_line (&it))
12726 last_text_row = it.glyph_row - 1;
12729 /* A value of current_y < last_visible_y means that we stopped
12730 at the previous window start, which in turn means that we
12731 have at least one reusable row. */
12732 if (it.current_y < it.last_visible_y)
12734 /* IT.vpos always starts from 0; it counts text lines. */
12735 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12737 /* Find PT if not already found in the lines displayed. */
12738 if (w->cursor.vpos < 0)
12740 int dy = it.current_y - start_row->y;
12742 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12743 row = row_containing_pos (w, PT, row, NULL, dy);
12744 if (row)
12745 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12746 dy, nrows_scrolled);
12747 else
12749 clear_glyph_matrix (w->desired_matrix);
12750 return 0;
12754 /* Scroll the display. Do it before the current matrix is
12755 changed. The problem here is that update has not yet
12756 run, i.e. part of the current matrix is not up to date.
12757 scroll_run_hook will clear the cursor, and use the
12758 current matrix to get the height of the row the cursor is
12759 in. */
12760 run.current_y = start_row->y;
12761 run.desired_y = it.current_y;
12762 run.height = it.last_visible_y - it.current_y;
12764 if (run.height > 0 && run.current_y != run.desired_y)
12766 update_begin (f);
12767 rif->update_window_begin_hook (w);
12768 rif->clear_window_mouse_face (w);
12769 rif->scroll_run_hook (w, &run);
12770 rif->update_window_end_hook (w, 0, 0);
12771 update_end (f);
12774 /* Shift current matrix down by nrows_scrolled lines. */
12775 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12776 rotate_matrix (w->current_matrix,
12777 start_vpos,
12778 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12779 nrows_scrolled);
12781 /* Disable lines that must be updated. */
12782 for (i = 0; i < it.vpos; ++i)
12783 (start_row + i)->enabled_p = 0;
12785 /* Re-compute Y positions. */
12786 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12787 max_y = it.last_visible_y;
12788 for (row = start_row + nrows_scrolled;
12789 row < bottom_row;
12790 ++row)
12792 row->y = it.current_y;
12793 row->visible_height = row->height;
12795 if (row->y < min_y)
12796 row->visible_height -= min_y - row->y;
12797 if (row->y + row->height > max_y)
12798 row->visible_height -= row->y + row->height - max_y;
12799 row->redraw_fringe_bitmaps_p = 1;
12801 it.current_y += row->height;
12803 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12804 last_reused_text_row = row;
12805 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12806 break;
12809 /* Disable lines in the current matrix which are now
12810 below the window. */
12811 for (++row; row < bottom_row; ++row)
12812 row->enabled_p = 0;
12815 /* Update window_end_pos etc.; last_reused_text_row is the last
12816 reused row from the current matrix containing text, if any.
12817 The value of last_text_row is the last displayed line
12818 containing text. */
12819 if (last_reused_text_row)
12821 w->window_end_bytepos
12822 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12823 w->window_end_pos
12824 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12825 w->window_end_vpos
12826 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12827 w->current_matrix));
12829 else if (last_text_row)
12831 w->window_end_bytepos
12832 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12833 w->window_end_pos
12834 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12835 w->window_end_vpos
12836 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12838 else
12840 /* This window must be completely empty. */
12841 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12842 w->window_end_pos = make_number (Z - ZV);
12843 w->window_end_vpos = make_number (0);
12845 w->window_end_valid = Qnil;
12847 /* Update hint: don't try scrolling again in update_window. */
12848 w->desired_matrix->no_scrolling_p = 1;
12850 #if GLYPH_DEBUG
12851 debug_method_add (w, "try_window_reusing_current_matrix 1");
12852 #endif
12853 return 1;
12855 else if (CHARPOS (new_start) > CHARPOS (start))
12857 struct glyph_row *pt_row, *row;
12858 struct glyph_row *first_reusable_row;
12859 struct glyph_row *first_row_to_display;
12860 int dy;
12861 int yb = window_text_bottom_y (w);
12863 /* Find the row starting at new_start, if there is one. Don't
12864 reuse a partially visible line at the end. */
12865 first_reusable_row = start_row;
12866 while (first_reusable_row->enabled_p
12867 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12868 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12869 < CHARPOS (new_start)))
12870 ++first_reusable_row;
12872 /* Give up if there is no row to reuse. */
12873 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12874 || !first_reusable_row->enabled_p
12875 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12876 != CHARPOS (new_start)))
12877 return 0;
12879 /* We can reuse fully visible rows beginning with
12880 first_reusable_row to the end of the window. Set
12881 first_row_to_display to the first row that cannot be reused.
12882 Set pt_row to the row containing point, if there is any. */
12883 pt_row = NULL;
12884 for (first_row_to_display = first_reusable_row;
12885 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12886 ++first_row_to_display)
12888 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12889 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12890 pt_row = first_row_to_display;
12893 /* Start displaying at the start of first_row_to_display. */
12894 xassert (first_row_to_display->y < yb);
12895 init_to_row_start (&it, w, first_row_to_display);
12897 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12898 - start_vpos);
12899 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12900 - nrows_scrolled);
12901 it.current_y = (first_row_to_display->y - first_reusable_row->y
12902 + WINDOW_HEADER_LINE_HEIGHT (w));
12904 /* Display lines beginning with first_row_to_display in the
12905 desired matrix. Set last_text_row to the last row displayed
12906 that displays text. */
12907 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12908 if (pt_row == NULL)
12909 w->cursor.vpos = -1;
12910 last_text_row = NULL;
12911 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12912 if (display_line (&it))
12913 last_text_row = it.glyph_row - 1;
12915 /* Give up If point isn't in a row displayed or reused. */
12916 if (w->cursor.vpos < 0)
12918 clear_glyph_matrix (w->desired_matrix);
12919 return 0;
12922 /* If point is in a reused row, adjust y and vpos of the cursor
12923 position. */
12924 if (pt_row)
12926 w->cursor.vpos -= nrows_scrolled;
12927 w->cursor.y -= first_reusable_row->y - start_row->y;
12930 /* Scroll the display. */
12931 run.current_y = first_reusable_row->y;
12932 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12933 run.height = it.last_visible_y - run.current_y;
12934 dy = run.current_y - run.desired_y;
12936 if (run.height)
12938 update_begin (f);
12939 rif->update_window_begin_hook (w);
12940 rif->clear_window_mouse_face (w);
12941 rif->scroll_run_hook (w, &run);
12942 rif->update_window_end_hook (w, 0, 0);
12943 update_end (f);
12946 /* Adjust Y positions of reused rows. */
12947 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12948 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12949 max_y = it.last_visible_y;
12950 for (row = first_reusable_row; row < first_row_to_display; ++row)
12952 row->y -= dy;
12953 row->visible_height = row->height;
12954 if (row->y < min_y)
12955 row->visible_height -= min_y - row->y;
12956 if (row->y + row->height > max_y)
12957 row->visible_height -= row->y + row->height - max_y;
12958 row->redraw_fringe_bitmaps_p = 1;
12961 /* Scroll the current matrix. */
12962 xassert (nrows_scrolled > 0);
12963 rotate_matrix (w->current_matrix,
12964 start_vpos,
12965 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12966 -nrows_scrolled);
12968 /* Disable rows not reused. */
12969 for (row -= nrows_scrolled; row < bottom_row; ++row)
12970 row->enabled_p = 0;
12972 /* Point may have moved to a different line, so we cannot assume that
12973 the previous cursor position is valid; locate the correct row. */
12974 if (pt_row)
12976 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12977 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12978 row++)
12980 w->cursor.vpos++;
12981 w->cursor.y = row->y;
12983 if (row < bottom_row)
12985 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12986 while (glyph->charpos < PT)
12988 w->cursor.hpos++;
12989 w->cursor.x += glyph->pixel_width;
12990 glyph++;
12995 /* Adjust window end. A null value of last_text_row means that
12996 the window end is in reused rows which in turn means that
12997 only its vpos can have changed. */
12998 if (last_text_row)
13000 w->window_end_bytepos
13001 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13002 w->window_end_pos
13003 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13004 w->window_end_vpos
13005 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13007 else
13009 w->window_end_vpos
13010 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13013 w->window_end_valid = Qnil;
13014 w->desired_matrix->no_scrolling_p = 1;
13016 #if GLYPH_DEBUG
13017 debug_method_add (w, "try_window_reusing_current_matrix 2");
13018 #endif
13019 return 1;
13022 return 0;
13027 /************************************************************************
13028 Window redisplay reusing current matrix when buffer has changed
13029 ************************************************************************/
13031 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13032 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13033 int *, int *));
13034 static struct glyph_row *
13035 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13036 struct glyph_row *));
13039 /* Return the last row in MATRIX displaying text. If row START is
13040 non-null, start searching with that row. IT gives the dimensions
13041 of the display. Value is null if matrix is empty; otherwise it is
13042 a pointer to the row found. */
13044 static struct glyph_row *
13045 find_last_row_displaying_text (matrix, it, start)
13046 struct glyph_matrix *matrix;
13047 struct it *it;
13048 struct glyph_row *start;
13050 struct glyph_row *row, *row_found;
13052 /* Set row_found to the last row in IT->w's current matrix
13053 displaying text. The loop looks funny but think of partially
13054 visible lines. */
13055 row_found = NULL;
13056 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13057 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13059 xassert (row->enabled_p);
13060 row_found = row;
13061 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13062 break;
13063 ++row;
13066 return row_found;
13070 /* Return the last row in the current matrix of W that is not affected
13071 by changes at the start of current_buffer that occurred since W's
13072 current matrix was built. Value is null if no such row exists.
13074 BEG_UNCHANGED us the number of characters unchanged at the start of
13075 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13076 first changed character in current_buffer. Characters at positions <
13077 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13078 when the current matrix was built. */
13080 static struct glyph_row *
13081 find_last_unchanged_at_beg_row (w)
13082 struct window *w;
13084 int first_changed_pos = BEG + BEG_UNCHANGED;
13085 struct glyph_row *row;
13086 struct glyph_row *row_found = NULL;
13087 int yb = window_text_bottom_y (w);
13089 /* Find the last row displaying unchanged text. */
13090 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13091 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13092 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13094 if (/* If row ends before first_changed_pos, it is unchanged,
13095 except in some case. */
13096 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13097 /* When row ends in ZV and we write at ZV it is not
13098 unchanged. */
13099 && !row->ends_at_zv_p
13100 /* When first_changed_pos is the end of a continued line,
13101 row is not unchanged because it may be no longer
13102 continued. */
13103 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13104 && (row->continued_p
13105 || row->exact_window_width_line_p)))
13106 row_found = row;
13108 /* Stop if last visible row. */
13109 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13110 break;
13112 ++row;
13115 return row_found;
13119 /* Find the first glyph row in the current matrix of W that is not
13120 affected by changes at the end of current_buffer since the
13121 time W's current matrix was built.
13123 Return in *DELTA the number of chars by which buffer positions in
13124 unchanged text at the end of current_buffer must be adjusted.
13126 Return in *DELTA_BYTES the corresponding number of bytes.
13128 Value is null if no such row exists, i.e. all rows are affected by
13129 changes. */
13131 static struct glyph_row *
13132 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13133 struct window *w;
13134 int *delta, *delta_bytes;
13136 struct glyph_row *row;
13137 struct glyph_row *row_found = NULL;
13139 *delta = *delta_bytes = 0;
13141 /* Display must not have been paused, otherwise the current matrix
13142 is not up to date. */
13143 if (NILP (w->window_end_valid))
13144 abort ();
13146 /* A value of window_end_pos >= END_UNCHANGED means that the window
13147 end is in the range of changed text. If so, there is no
13148 unchanged row at the end of W's current matrix. */
13149 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13150 return NULL;
13152 /* Set row to the last row in W's current matrix displaying text. */
13153 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13155 /* If matrix is entirely empty, no unchanged row exists. */
13156 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13158 /* The value of row is the last glyph row in the matrix having a
13159 meaningful buffer position in it. The end position of row
13160 corresponds to window_end_pos. This allows us to translate
13161 buffer positions in the current matrix to current buffer
13162 positions for characters not in changed text. */
13163 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13164 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13165 int last_unchanged_pos, last_unchanged_pos_old;
13166 struct glyph_row *first_text_row
13167 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13169 *delta = Z - Z_old;
13170 *delta_bytes = Z_BYTE - Z_BYTE_old;
13172 /* Set last_unchanged_pos to the buffer position of the last
13173 character in the buffer that has not been changed. Z is the
13174 index + 1 of the last character in current_buffer, i.e. by
13175 subtracting END_UNCHANGED we get the index of the last
13176 unchanged character, and we have to add BEG to get its buffer
13177 position. */
13178 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13179 last_unchanged_pos_old = last_unchanged_pos - *delta;
13181 /* Search backward from ROW for a row displaying a line that
13182 starts at a minimum position >= last_unchanged_pos_old. */
13183 for (; row > first_text_row; --row)
13185 /* This used to abort, but it can happen.
13186 It is ok to just stop the search instead here. KFS. */
13187 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13188 break;
13190 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13191 row_found = row;
13195 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13196 abort ();
13198 return row_found;
13202 /* Make sure that glyph rows in the current matrix of window W
13203 reference the same glyph memory as corresponding rows in the
13204 frame's frame matrix. This function is called after scrolling W's
13205 current matrix on a terminal frame in try_window_id and
13206 try_window_reusing_current_matrix. */
13208 static void
13209 sync_frame_with_window_matrix_rows (w)
13210 struct window *w;
13212 struct frame *f = XFRAME (w->frame);
13213 struct glyph_row *window_row, *window_row_end, *frame_row;
13215 /* Preconditions: W must be a leaf window and full-width. Its frame
13216 must have a frame matrix. */
13217 xassert (NILP (w->hchild) && NILP (w->vchild));
13218 xassert (WINDOW_FULL_WIDTH_P (w));
13219 xassert (!FRAME_WINDOW_P (f));
13221 /* If W is a full-width window, glyph pointers in W's current matrix
13222 have, by definition, to be the same as glyph pointers in the
13223 corresponding frame matrix. Note that frame matrices have no
13224 marginal areas (see build_frame_matrix). */
13225 window_row = w->current_matrix->rows;
13226 window_row_end = window_row + w->current_matrix->nrows;
13227 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13228 while (window_row < window_row_end)
13230 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13231 struct glyph *end = window_row->glyphs[LAST_AREA];
13233 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13234 frame_row->glyphs[TEXT_AREA] = start;
13235 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13236 frame_row->glyphs[LAST_AREA] = end;
13238 /* Disable frame rows whose corresponding window rows have
13239 been disabled in try_window_id. */
13240 if (!window_row->enabled_p)
13241 frame_row->enabled_p = 0;
13243 ++window_row, ++frame_row;
13248 /* Find the glyph row in window W containing CHARPOS. Consider all
13249 rows between START and END (not inclusive). END null means search
13250 all rows to the end of the display area of W. Value is the row
13251 containing CHARPOS or null. */
13253 struct glyph_row *
13254 row_containing_pos (w, charpos, start, end, dy)
13255 struct window *w;
13256 int charpos;
13257 struct glyph_row *start, *end;
13258 int dy;
13260 struct glyph_row *row = start;
13261 int last_y;
13263 /* If we happen to start on a header-line, skip that. */
13264 if (row->mode_line_p)
13265 ++row;
13267 if ((end && row >= end) || !row->enabled_p)
13268 return NULL;
13270 last_y = window_text_bottom_y (w) - dy;
13272 while (1)
13274 /* Give up if we have gone too far. */
13275 if (end && row >= end)
13276 return NULL;
13277 /* This formerly returned if they were equal.
13278 I think that both quantities are of a "last plus one" type;
13279 if so, when they are equal, the row is within the screen. -- rms. */
13280 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13281 return NULL;
13283 /* If it is in this row, return this row. */
13284 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13285 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13286 /* The end position of a row equals the start
13287 position of the next row. If CHARPOS is there, we
13288 would rather display it in the next line, except
13289 when this line ends in ZV. */
13290 && !row->ends_at_zv_p
13291 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13292 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13293 return row;
13294 ++row;
13299 /* Try to redisplay window W by reusing its existing display. W's
13300 current matrix must be up to date when this function is called,
13301 i.e. window_end_valid must not be nil.
13303 Value is
13305 1 if display has been updated
13306 0 if otherwise unsuccessful
13307 -1 if redisplay with same window start is known not to succeed
13309 The following steps are performed:
13311 1. Find the last row in the current matrix of W that is not
13312 affected by changes at the start of current_buffer. If no such row
13313 is found, give up.
13315 2. Find the first row in W's current matrix that is not affected by
13316 changes at the end of current_buffer. Maybe there is no such row.
13318 3. Display lines beginning with the row + 1 found in step 1 to the
13319 row found in step 2 or, if step 2 didn't find a row, to the end of
13320 the window.
13322 4. If cursor is not known to appear on the window, give up.
13324 5. If display stopped at the row found in step 2, scroll the
13325 display and current matrix as needed.
13327 6. Maybe display some lines at the end of W, if we must. This can
13328 happen under various circumstances, like a partially visible line
13329 becoming fully visible, or because newly displayed lines are displayed
13330 in smaller font sizes.
13332 7. Update W's window end information. */
13334 static int
13335 try_window_id (w)
13336 struct window *w;
13338 struct frame *f = XFRAME (w->frame);
13339 struct glyph_matrix *current_matrix = w->current_matrix;
13340 struct glyph_matrix *desired_matrix = w->desired_matrix;
13341 struct glyph_row *last_unchanged_at_beg_row;
13342 struct glyph_row *first_unchanged_at_end_row;
13343 struct glyph_row *row;
13344 struct glyph_row *bottom_row;
13345 int bottom_vpos;
13346 struct it it;
13347 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13348 struct text_pos start_pos;
13349 struct run run;
13350 int first_unchanged_at_end_vpos = 0;
13351 struct glyph_row *last_text_row, *last_text_row_at_end;
13352 struct text_pos start;
13353 int first_changed_charpos, last_changed_charpos;
13355 #if GLYPH_DEBUG
13356 if (inhibit_try_window_id)
13357 return 0;
13358 #endif
13360 /* This is handy for debugging. */
13361 #if 0
13362 #define GIVE_UP(X) \
13363 do { \
13364 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13365 return 0; \
13366 } while (0)
13367 #else
13368 #define GIVE_UP(X) return 0
13369 #endif
13371 SET_TEXT_POS_FROM_MARKER (start, w->start);
13373 /* Don't use this for mini-windows because these can show
13374 messages and mini-buffers, and we don't handle that here. */
13375 if (MINI_WINDOW_P (w))
13376 GIVE_UP (1);
13378 /* This flag is used to prevent redisplay optimizations. */
13379 if (windows_or_buffers_changed || cursor_type_changed)
13380 GIVE_UP (2);
13382 /* Verify that narrowing has not changed.
13383 Also verify that we were not told to prevent redisplay optimizations.
13384 It would be nice to further
13385 reduce the number of cases where this prevents try_window_id. */
13386 if (current_buffer->clip_changed
13387 || current_buffer->prevent_redisplay_optimizations_p)
13388 GIVE_UP (3);
13390 /* Window must either use window-based redisplay or be full width. */
13391 if (!FRAME_WINDOW_P (f)
13392 && (!line_ins_del_ok
13393 || !WINDOW_FULL_WIDTH_P (w)))
13394 GIVE_UP (4);
13396 /* Give up if point is not known NOT to appear in W. */
13397 if (PT < CHARPOS (start))
13398 GIVE_UP (5);
13400 /* Another way to prevent redisplay optimizations. */
13401 if (XFASTINT (w->last_modified) == 0)
13402 GIVE_UP (6);
13404 /* Verify that window is not hscrolled. */
13405 if (XFASTINT (w->hscroll) != 0)
13406 GIVE_UP (7);
13408 /* Verify that display wasn't paused. */
13409 if (NILP (w->window_end_valid))
13410 GIVE_UP (8);
13412 /* Can't use this if highlighting a region because a cursor movement
13413 will do more than just set the cursor. */
13414 if (!NILP (Vtransient_mark_mode)
13415 && !NILP (current_buffer->mark_active))
13416 GIVE_UP (9);
13418 /* Likewise if highlighting trailing whitespace. */
13419 if (!NILP (Vshow_trailing_whitespace))
13420 GIVE_UP (11);
13422 /* Likewise if showing a region. */
13423 if (!NILP (w->region_showing))
13424 GIVE_UP (10);
13426 /* Can use this if overlay arrow position and or string have changed. */
13427 if (overlay_arrows_changed_p ())
13428 GIVE_UP (12);
13431 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13432 only if buffer has really changed. The reason is that the gap is
13433 initially at Z for freshly visited files. The code below would
13434 set end_unchanged to 0 in that case. */
13435 if (MODIFF > SAVE_MODIFF
13436 /* This seems to happen sometimes after saving a buffer. */
13437 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13439 if (GPT - BEG < BEG_UNCHANGED)
13440 BEG_UNCHANGED = GPT - BEG;
13441 if (Z - GPT < END_UNCHANGED)
13442 END_UNCHANGED = Z - GPT;
13445 /* The position of the first and last character that has been changed. */
13446 first_changed_charpos = BEG + BEG_UNCHANGED;
13447 last_changed_charpos = Z - END_UNCHANGED;
13449 /* If window starts after a line end, and the last change is in
13450 front of that newline, then changes don't affect the display.
13451 This case happens with stealth-fontification. Note that although
13452 the display is unchanged, glyph positions in the matrix have to
13453 be adjusted, of course. */
13454 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13455 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13456 && ((last_changed_charpos < CHARPOS (start)
13457 && CHARPOS (start) == BEGV)
13458 || (last_changed_charpos < CHARPOS (start) - 1
13459 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13461 int Z_old, delta, Z_BYTE_old, delta_bytes;
13462 struct glyph_row *r0;
13464 /* Compute how many chars/bytes have been added to or removed
13465 from the buffer. */
13466 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13467 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13468 delta = Z - Z_old;
13469 delta_bytes = Z_BYTE - Z_BYTE_old;
13471 /* Give up if PT is not in the window. Note that it already has
13472 been checked at the start of try_window_id that PT is not in
13473 front of the window start. */
13474 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13475 GIVE_UP (13);
13477 /* If window start is unchanged, we can reuse the whole matrix
13478 as is, after adjusting glyph positions. No need to compute
13479 the window end again, since its offset from Z hasn't changed. */
13480 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13481 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13482 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13483 /* PT must not be in a partially visible line. */
13484 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13485 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13487 /* Adjust positions in the glyph matrix. */
13488 if (delta || delta_bytes)
13490 struct glyph_row *r1
13491 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13492 increment_matrix_positions (w->current_matrix,
13493 MATRIX_ROW_VPOS (r0, current_matrix),
13494 MATRIX_ROW_VPOS (r1, current_matrix),
13495 delta, delta_bytes);
13498 /* Set the cursor. */
13499 row = row_containing_pos (w, PT, r0, NULL, 0);
13500 if (row)
13501 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13502 else
13503 abort ();
13504 return 1;
13508 /* Handle the case that changes are all below what is displayed in
13509 the window, and that PT is in the window. This shortcut cannot
13510 be taken if ZV is visible in the window, and text has been added
13511 there that is visible in the window. */
13512 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13513 /* ZV is not visible in the window, or there are no
13514 changes at ZV, actually. */
13515 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13516 || first_changed_charpos == last_changed_charpos))
13518 struct glyph_row *r0;
13520 /* Give up if PT is not in the window. Note that it already has
13521 been checked at the start of try_window_id that PT is not in
13522 front of the window start. */
13523 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13524 GIVE_UP (14);
13526 /* If window start is unchanged, we can reuse the whole matrix
13527 as is, without changing glyph positions since no text has
13528 been added/removed in front of the window end. */
13529 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13530 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13531 /* PT must not be in a partially visible line. */
13532 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13533 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13535 /* We have to compute the window end anew since text
13536 can have been added/removed after it. */
13537 w->window_end_pos
13538 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13539 w->window_end_bytepos
13540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13542 /* Set the cursor. */
13543 row = row_containing_pos (w, PT, r0, NULL, 0);
13544 if (row)
13545 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13546 else
13547 abort ();
13548 return 2;
13552 /* Give up if window start is in the changed area.
13554 The condition used to read
13556 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13558 but why that was tested escapes me at the moment. */
13559 if (CHARPOS (start) >= first_changed_charpos
13560 && CHARPOS (start) <= last_changed_charpos)
13561 GIVE_UP (15);
13563 /* Check that window start agrees with the start of the first glyph
13564 row in its current matrix. Check this after we know the window
13565 start is not in changed text, otherwise positions would not be
13566 comparable. */
13567 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13568 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13569 GIVE_UP (16);
13571 /* Give up if the window ends in strings. Overlay strings
13572 at the end are difficult to handle, so don't try. */
13573 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13574 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13575 GIVE_UP (20);
13577 /* Compute the position at which we have to start displaying new
13578 lines. Some of the lines at the top of the window might be
13579 reusable because they are not displaying changed text. Find the
13580 last row in W's current matrix not affected by changes at the
13581 start of current_buffer. Value is null if changes start in the
13582 first line of window. */
13583 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13584 if (last_unchanged_at_beg_row)
13586 /* Avoid starting to display in the moddle of a character, a TAB
13587 for instance. This is easier than to set up the iterator
13588 exactly, and it's not a frequent case, so the additional
13589 effort wouldn't really pay off. */
13590 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13591 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13592 && last_unchanged_at_beg_row > w->current_matrix->rows)
13593 --last_unchanged_at_beg_row;
13595 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13596 GIVE_UP (17);
13598 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13599 GIVE_UP (18);
13600 start_pos = it.current.pos;
13602 /* Start displaying new lines in the desired matrix at the same
13603 vpos we would use in the current matrix, i.e. below
13604 last_unchanged_at_beg_row. */
13605 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13606 current_matrix);
13607 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13608 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13610 xassert (it.hpos == 0 && it.current_x == 0);
13612 else
13614 /* There are no reusable lines at the start of the window.
13615 Start displaying in the first text line. */
13616 start_display (&it, w, start);
13617 it.vpos = it.first_vpos;
13618 start_pos = it.current.pos;
13621 /* Find the first row that is not affected by changes at the end of
13622 the buffer. Value will be null if there is no unchanged row, in
13623 which case we must redisplay to the end of the window. delta
13624 will be set to the value by which buffer positions beginning with
13625 first_unchanged_at_end_row have to be adjusted due to text
13626 changes. */
13627 first_unchanged_at_end_row
13628 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13629 IF_DEBUG (debug_delta = delta);
13630 IF_DEBUG (debug_delta_bytes = delta_bytes);
13632 /* Set stop_pos to the buffer position up to which we will have to
13633 display new lines. If first_unchanged_at_end_row != NULL, this
13634 is the buffer position of the start of the line displayed in that
13635 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13636 that we don't stop at a buffer position. */
13637 stop_pos = 0;
13638 if (first_unchanged_at_end_row)
13640 xassert (last_unchanged_at_beg_row == NULL
13641 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13643 /* If this is a continuation line, move forward to the next one
13644 that isn't. Changes in lines above affect this line.
13645 Caution: this may move first_unchanged_at_end_row to a row
13646 not displaying text. */
13647 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13648 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13649 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13650 < it.last_visible_y))
13651 ++first_unchanged_at_end_row;
13653 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13654 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13655 >= it.last_visible_y))
13656 first_unchanged_at_end_row = NULL;
13657 else
13659 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13660 + delta);
13661 first_unchanged_at_end_vpos
13662 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13663 xassert (stop_pos >= Z - END_UNCHANGED);
13666 else if (last_unchanged_at_beg_row == NULL)
13667 GIVE_UP (19);
13670 #if GLYPH_DEBUG
13672 /* Either there is no unchanged row at the end, or the one we have
13673 now displays text. This is a necessary condition for the window
13674 end pos calculation at the end of this function. */
13675 xassert (first_unchanged_at_end_row == NULL
13676 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13678 debug_last_unchanged_at_beg_vpos
13679 = (last_unchanged_at_beg_row
13680 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13681 : -1);
13682 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13684 #endif /* GLYPH_DEBUG != 0 */
13687 /* Display new lines. Set last_text_row to the last new line
13688 displayed which has text on it, i.e. might end up as being the
13689 line where the window_end_vpos is. */
13690 w->cursor.vpos = -1;
13691 last_text_row = NULL;
13692 overlay_arrow_seen = 0;
13693 while (it.current_y < it.last_visible_y
13694 && !fonts_changed_p
13695 && (first_unchanged_at_end_row == NULL
13696 || IT_CHARPOS (it) < stop_pos))
13698 if (display_line (&it))
13699 last_text_row = it.glyph_row - 1;
13702 if (fonts_changed_p)
13703 return -1;
13706 /* Compute differences in buffer positions, y-positions etc. for
13707 lines reused at the bottom of the window. Compute what we can
13708 scroll. */
13709 if (first_unchanged_at_end_row
13710 /* No lines reused because we displayed everything up to the
13711 bottom of the window. */
13712 && it.current_y < it.last_visible_y)
13714 dvpos = (it.vpos
13715 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13716 current_matrix));
13717 dy = it.current_y - first_unchanged_at_end_row->y;
13718 run.current_y = first_unchanged_at_end_row->y;
13719 run.desired_y = run.current_y + dy;
13720 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13722 else
13724 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13725 first_unchanged_at_end_row = NULL;
13727 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13730 /* Find the cursor if not already found. We have to decide whether
13731 PT will appear on this window (it sometimes doesn't, but this is
13732 not a very frequent case.) This decision has to be made before
13733 the current matrix is altered. A value of cursor.vpos < 0 means
13734 that PT is either in one of the lines beginning at
13735 first_unchanged_at_end_row or below the window. Don't care for
13736 lines that might be displayed later at the window end; as
13737 mentioned, this is not a frequent case. */
13738 if (w->cursor.vpos < 0)
13740 /* Cursor in unchanged rows at the top? */
13741 if (PT < CHARPOS (start_pos)
13742 && last_unchanged_at_beg_row)
13744 row = row_containing_pos (w, PT,
13745 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13746 last_unchanged_at_beg_row + 1, 0);
13747 if (row)
13748 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13751 /* Start from first_unchanged_at_end_row looking for PT. */
13752 else if (first_unchanged_at_end_row)
13754 row = row_containing_pos (w, PT - delta,
13755 first_unchanged_at_end_row, NULL, 0);
13756 if (row)
13757 set_cursor_from_row (w, row, w->current_matrix, delta,
13758 delta_bytes, dy, dvpos);
13761 /* Give up if cursor was not found. */
13762 if (w->cursor.vpos < 0)
13764 clear_glyph_matrix (w->desired_matrix);
13765 return -1;
13769 /* Don't let the cursor end in the scroll margins. */
13771 int this_scroll_margin, cursor_height;
13773 this_scroll_margin = max (0, scroll_margin);
13774 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13775 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13776 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13778 if ((w->cursor.y < this_scroll_margin
13779 && CHARPOS (start) > BEGV)
13780 /* Old redisplay didn't take scroll margin into account at the bottom,
13781 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13782 || (w->cursor.y + (make_cursor_line_fully_visible_p
13783 ? cursor_height + this_scroll_margin
13784 : 1)) > it.last_visible_y)
13786 w->cursor.vpos = -1;
13787 clear_glyph_matrix (w->desired_matrix);
13788 return -1;
13792 /* Scroll the display. Do it before changing the current matrix so
13793 that xterm.c doesn't get confused about where the cursor glyph is
13794 found. */
13795 if (dy && run.height)
13797 update_begin (f);
13799 if (FRAME_WINDOW_P (f))
13801 rif->update_window_begin_hook (w);
13802 rif->clear_window_mouse_face (w);
13803 rif->scroll_run_hook (w, &run);
13804 rif->update_window_end_hook (w, 0, 0);
13806 else
13808 /* Terminal frame. In this case, dvpos gives the number of
13809 lines to scroll by; dvpos < 0 means scroll up. */
13810 int first_unchanged_at_end_vpos
13811 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13812 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13813 int end = (WINDOW_TOP_EDGE_LINE (w)
13814 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13815 + window_internal_height (w));
13817 /* Perform the operation on the screen. */
13818 if (dvpos > 0)
13820 /* Scroll last_unchanged_at_beg_row to the end of the
13821 window down dvpos lines. */
13822 set_terminal_window (end);
13824 /* On dumb terminals delete dvpos lines at the end
13825 before inserting dvpos empty lines. */
13826 if (!scroll_region_ok)
13827 ins_del_lines (end - dvpos, -dvpos);
13829 /* Insert dvpos empty lines in front of
13830 last_unchanged_at_beg_row. */
13831 ins_del_lines (from, dvpos);
13833 else if (dvpos < 0)
13835 /* Scroll up last_unchanged_at_beg_vpos to the end of
13836 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13837 set_terminal_window (end);
13839 /* Delete dvpos lines in front of
13840 last_unchanged_at_beg_vpos. ins_del_lines will set
13841 the cursor to the given vpos and emit |dvpos| delete
13842 line sequences. */
13843 ins_del_lines (from + dvpos, dvpos);
13845 /* On a dumb terminal insert dvpos empty lines at the
13846 end. */
13847 if (!scroll_region_ok)
13848 ins_del_lines (end + dvpos, -dvpos);
13851 set_terminal_window (0);
13854 update_end (f);
13857 /* Shift reused rows of the current matrix to the right position.
13858 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13859 text. */
13860 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13861 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13862 if (dvpos < 0)
13864 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13865 bottom_vpos, dvpos);
13866 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13867 bottom_vpos, 0);
13869 else if (dvpos > 0)
13871 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13872 bottom_vpos, dvpos);
13873 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13874 first_unchanged_at_end_vpos + dvpos, 0);
13877 /* For frame-based redisplay, make sure that current frame and window
13878 matrix are in sync with respect to glyph memory. */
13879 if (!FRAME_WINDOW_P (f))
13880 sync_frame_with_window_matrix_rows (w);
13882 /* Adjust buffer positions in reused rows. */
13883 if (delta)
13884 increment_matrix_positions (current_matrix,
13885 first_unchanged_at_end_vpos + dvpos,
13886 bottom_vpos, delta, delta_bytes);
13888 /* Adjust Y positions. */
13889 if (dy)
13890 shift_glyph_matrix (w, current_matrix,
13891 first_unchanged_at_end_vpos + dvpos,
13892 bottom_vpos, dy);
13894 if (first_unchanged_at_end_row)
13896 first_unchanged_at_end_row += dvpos;
13897 if (first_unchanged_at_end_row->y >= it.last_visible_y
13898 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
13899 first_unchanged_at_end_row = NULL;
13902 /* If scrolling up, there may be some lines to display at the end of
13903 the window. */
13904 last_text_row_at_end = NULL;
13905 if (dy < 0)
13907 /* Scrolling up can leave for example a partially visible line
13908 at the end of the window to be redisplayed. */
13909 /* Set last_row to the glyph row in the current matrix where the
13910 window end line is found. It has been moved up or down in
13911 the matrix by dvpos. */
13912 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13913 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13915 /* If last_row is the window end line, it should display text. */
13916 xassert (last_row->displays_text_p);
13918 /* If window end line was partially visible before, begin
13919 displaying at that line. Otherwise begin displaying with the
13920 line following it. */
13921 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13923 init_to_row_start (&it, w, last_row);
13924 it.vpos = last_vpos;
13925 it.current_y = last_row->y;
13927 else
13929 init_to_row_end (&it, w, last_row);
13930 it.vpos = 1 + last_vpos;
13931 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13932 ++last_row;
13935 /* We may start in a continuation line. If so, we have to
13936 get the right continuation_lines_width and current_x. */
13937 it.continuation_lines_width = last_row->continuation_lines_width;
13938 it.hpos = it.current_x = 0;
13940 /* Display the rest of the lines at the window end. */
13941 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13942 while (it.current_y < it.last_visible_y
13943 && !fonts_changed_p)
13945 /* Is it always sure that the display agrees with lines in
13946 the current matrix? I don't think so, so we mark rows
13947 displayed invalid in the current matrix by setting their
13948 enabled_p flag to zero. */
13949 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13950 if (display_line (&it))
13951 last_text_row_at_end = it.glyph_row - 1;
13955 /* Update window_end_pos and window_end_vpos. */
13956 if (first_unchanged_at_end_row
13957 && !last_text_row_at_end)
13959 /* Window end line if one of the preserved rows from the current
13960 matrix. Set row to the last row displaying text in current
13961 matrix starting at first_unchanged_at_end_row, after
13962 scrolling. */
13963 xassert (first_unchanged_at_end_row->displays_text_p);
13964 row = find_last_row_displaying_text (w->current_matrix, &it,
13965 first_unchanged_at_end_row);
13966 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13968 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13969 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13970 w->window_end_vpos
13971 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13972 xassert (w->window_end_bytepos >= 0);
13973 IF_DEBUG (debug_method_add (w, "A"));
13975 else if (last_text_row_at_end)
13977 w->window_end_pos
13978 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13979 w->window_end_bytepos
13980 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13981 w->window_end_vpos
13982 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13983 xassert (w->window_end_bytepos >= 0);
13984 IF_DEBUG (debug_method_add (w, "B"));
13986 else if (last_text_row)
13988 /* We have displayed either to the end of the window or at the
13989 end of the window, i.e. the last row with text is to be found
13990 in the desired matrix. */
13991 w->window_end_pos
13992 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13993 w->window_end_bytepos
13994 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13995 w->window_end_vpos
13996 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13997 xassert (w->window_end_bytepos >= 0);
13999 else if (first_unchanged_at_end_row == NULL
14000 && last_text_row == NULL
14001 && last_text_row_at_end == NULL)
14003 /* Displayed to end of window, but no line containing text was
14004 displayed. Lines were deleted at the end of the window. */
14005 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14006 int vpos = XFASTINT (w->window_end_vpos);
14007 struct glyph_row *current_row = current_matrix->rows + vpos;
14008 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14010 for (row = NULL;
14011 row == NULL && vpos >= first_vpos;
14012 --vpos, --current_row, --desired_row)
14014 if (desired_row->enabled_p)
14016 if (desired_row->displays_text_p)
14017 row = desired_row;
14019 else if (current_row->displays_text_p)
14020 row = current_row;
14023 xassert (row != NULL);
14024 w->window_end_vpos = make_number (vpos + 1);
14025 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14026 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14027 xassert (w->window_end_bytepos >= 0);
14028 IF_DEBUG (debug_method_add (w, "C"));
14030 else
14031 abort ();
14033 #if 0 /* This leads to problems, for instance when the cursor is
14034 at ZV, and the cursor line displays no text. */
14035 /* Disable rows below what's displayed in the window. This makes
14036 debugging easier. */
14037 enable_glyph_matrix_rows (current_matrix,
14038 XFASTINT (w->window_end_vpos) + 1,
14039 bottom_vpos, 0);
14040 #endif
14042 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14043 debug_end_vpos = XFASTINT (w->window_end_vpos));
14045 /* Record that display has not been completed. */
14046 w->window_end_valid = Qnil;
14047 w->desired_matrix->no_scrolling_p = 1;
14048 return 3;
14050 #undef GIVE_UP
14055 /***********************************************************************
14056 More debugging support
14057 ***********************************************************************/
14059 #if GLYPH_DEBUG
14061 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14062 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14063 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14066 /* Dump the contents of glyph matrix MATRIX on stderr.
14068 GLYPHS 0 means don't show glyph contents.
14069 GLYPHS 1 means show glyphs in short form
14070 GLYPHS > 1 means show glyphs in long form. */
14072 void
14073 dump_glyph_matrix (matrix, glyphs)
14074 struct glyph_matrix *matrix;
14075 int glyphs;
14077 int i;
14078 for (i = 0; i < matrix->nrows; ++i)
14079 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14083 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14084 the glyph row and area where the glyph comes from. */
14086 void
14087 dump_glyph (row, glyph, area)
14088 struct glyph_row *row;
14089 struct glyph *glyph;
14090 int area;
14092 if (glyph->type == CHAR_GLYPH)
14094 fprintf (stderr,
14095 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14096 glyph - row->glyphs[TEXT_AREA],
14097 'C',
14098 glyph->charpos,
14099 (BUFFERP (glyph->object)
14100 ? 'B'
14101 : (STRINGP (glyph->object)
14102 ? 'S'
14103 : '-')),
14104 glyph->pixel_width,
14105 glyph->u.ch,
14106 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14107 ? glyph->u.ch
14108 : '.'),
14109 glyph->face_id,
14110 glyph->left_box_line_p,
14111 glyph->right_box_line_p);
14113 else if (glyph->type == STRETCH_GLYPH)
14115 fprintf (stderr,
14116 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14117 glyph - row->glyphs[TEXT_AREA],
14118 'S',
14119 glyph->charpos,
14120 (BUFFERP (glyph->object)
14121 ? 'B'
14122 : (STRINGP (glyph->object)
14123 ? 'S'
14124 : '-')),
14125 glyph->pixel_width,
14127 '.',
14128 glyph->face_id,
14129 glyph->left_box_line_p,
14130 glyph->right_box_line_p);
14132 else if (glyph->type == IMAGE_GLYPH)
14134 fprintf (stderr,
14135 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14136 glyph - row->glyphs[TEXT_AREA],
14137 'I',
14138 glyph->charpos,
14139 (BUFFERP (glyph->object)
14140 ? 'B'
14141 : (STRINGP (glyph->object)
14142 ? 'S'
14143 : '-')),
14144 glyph->pixel_width,
14145 glyph->u.img_id,
14146 '.',
14147 glyph->face_id,
14148 glyph->left_box_line_p,
14149 glyph->right_box_line_p);
14154 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14155 GLYPHS 0 means don't show glyph contents.
14156 GLYPHS 1 means show glyphs in short form
14157 GLYPHS > 1 means show glyphs in long form. */
14159 void
14160 dump_glyph_row (row, vpos, glyphs)
14161 struct glyph_row *row;
14162 int vpos, glyphs;
14164 if (glyphs != 1)
14166 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14167 fprintf (stderr, "======================================================================\n");
14169 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14170 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14171 vpos,
14172 MATRIX_ROW_START_CHARPOS (row),
14173 MATRIX_ROW_END_CHARPOS (row),
14174 row->used[TEXT_AREA],
14175 row->contains_overlapping_glyphs_p,
14176 row->enabled_p,
14177 row->truncated_on_left_p,
14178 row->truncated_on_right_p,
14179 row->continued_p,
14180 MATRIX_ROW_CONTINUATION_LINE_P (row),
14181 row->displays_text_p,
14182 row->ends_at_zv_p,
14183 row->fill_line_p,
14184 row->ends_in_middle_of_char_p,
14185 row->starts_in_middle_of_char_p,
14186 row->mouse_face_p,
14187 row->x,
14188 row->y,
14189 row->pixel_width,
14190 row->height,
14191 row->visible_height,
14192 row->ascent,
14193 row->phys_ascent);
14194 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14195 row->end.overlay_string_index,
14196 row->continuation_lines_width);
14197 fprintf (stderr, "%9d %5d\n",
14198 CHARPOS (row->start.string_pos),
14199 CHARPOS (row->end.string_pos));
14200 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14201 row->end.dpvec_index);
14204 if (glyphs > 1)
14206 int area;
14208 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14210 struct glyph *glyph = row->glyphs[area];
14211 struct glyph *glyph_end = glyph + row->used[area];
14213 /* Glyph for a line end in text. */
14214 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14215 ++glyph_end;
14217 if (glyph < glyph_end)
14218 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14220 for (; glyph < glyph_end; ++glyph)
14221 dump_glyph (row, glyph, area);
14224 else if (glyphs == 1)
14226 int area;
14228 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14230 char *s = (char *) alloca (row->used[area] + 1);
14231 int i;
14233 for (i = 0; i < row->used[area]; ++i)
14235 struct glyph *glyph = row->glyphs[area] + i;
14236 if (glyph->type == CHAR_GLYPH
14237 && glyph->u.ch < 0x80
14238 && glyph->u.ch >= ' ')
14239 s[i] = glyph->u.ch;
14240 else
14241 s[i] = '.';
14244 s[i] = '\0';
14245 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14251 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14252 Sdump_glyph_matrix, 0, 1, "p",
14253 doc: /* Dump the current matrix of the selected window to stderr.
14254 Shows contents of glyph row structures. With non-nil
14255 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14256 glyphs in short form, otherwise show glyphs in long form. */)
14257 (glyphs)
14258 Lisp_Object glyphs;
14260 struct window *w = XWINDOW (selected_window);
14261 struct buffer *buffer = XBUFFER (w->buffer);
14263 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14264 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14265 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14266 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14267 fprintf (stderr, "=============================================\n");
14268 dump_glyph_matrix (w->current_matrix,
14269 NILP (glyphs) ? 0 : XINT (glyphs));
14270 return Qnil;
14274 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14275 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14278 struct frame *f = XFRAME (selected_frame);
14279 dump_glyph_matrix (f->current_matrix, 1);
14280 return Qnil;
14284 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14285 doc: /* Dump glyph row ROW to stderr.
14286 GLYPH 0 means don't dump glyphs.
14287 GLYPH 1 means dump glyphs in short form.
14288 GLYPH > 1 or omitted means dump glyphs in long form. */)
14289 (row, glyphs)
14290 Lisp_Object row, glyphs;
14292 struct glyph_matrix *matrix;
14293 int vpos;
14295 CHECK_NUMBER (row);
14296 matrix = XWINDOW (selected_window)->current_matrix;
14297 vpos = XINT (row);
14298 if (vpos >= 0 && vpos < matrix->nrows)
14299 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14300 vpos,
14301 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14302 return Qnil;
14306 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14307 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14308 GLYPH 0 means don't dump glyphs.
14309 GLYPH 1 means dump glyphs in short form.
14310 GLYPH > 1 or omitted means dump glyphs in long form. */)
14311 (row, glyphs)
14312 Lisp_Object row, glyphs;
14314 struct frame *sf = SELECTED_FRAME ();
14315 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14316 int vpos;
14318 CHECK_NUMBER (row);
14319 vpos = XINT (row);
14320 if (vpos >= 0 && vpos < m->nrows)
14321 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14322 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14323 return Qnil;
14327 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14328 doc: /* Toggle tracing of redisplay.
14329 With ARG, turn tracing on if and only if ARG is positive. */)
14330 (arg)
14331 Lisp_Object arg;
14333 if (NILP (arg))
14334 trace_redisplay_p = !trace_redisplay_p;
14335 else
14337 arg = Fprefix_numeric_value (arg);
14338 trace_redisplay_p = XINT (arg) > 0;
14341 return Qnil;
14345 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14346 doc: /* Like `format', but print result to stderr.
14347 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14348 (nargs, args)
14349 int nargs;
14350 Lisp_Object *args;
14352 Lisp_Object s = Fformat (nargs, args);
14353 fprintf (stderr, "%s", SDATA (s));
14354 return Qnil;
14357 #endif /* GLYPH_DEBUG */
14361 /***********************************************************************
14362 Building Desired Matrix Rows
14363 ***********************************************************************/
14365 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14366 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14368 static struct glyph_row *
14369 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14370 struct window *w;
14371 Lisp_Object overlay_arrow_string;
14373 struct frame *f = XFRAME (WINDOW_FRAME (w));
14374 struct buffer *buffer = XBUFFER (w->buffer);
14375 struct buffer *old = current_buffer;
14376 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14377 int arrow_len = SCHARS (overlay_arrow_string);
14378 const unsigned char *arrow_end = arrow_string + arrow_len;
14379 const unsigned char *p;
14380 struct it it;
14381 int multibyte_p;
14382 int n_glyphs_before;
14384 set_buffer_temp (buffer);
14385 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14386 it.glyph_row->used[TEXT_AREA] = 0;
14387 SET_TEXT_POS (it.position, 0, 0);
14389 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14390 p = arrow_string;
14391 while (p < arrow_end)
14393 Lisp_Object face, ilisp;
14395 /* Get the next character. */
14396 if (multibyte_p)
14397 it.c = string_char_and_length (p, arrow_len, &it.len);
14398 else
14399 it.c = *p, it.len = 1;
14400 p += it.len;
14402 /* Get its face. */
14403 ilisp = make_number (p - arrow_string);
14404 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14405 it.face_id = compute_char_face (f, it.c, face);
14407 /* Compute its width, get its glyphs. */
14408 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14409 SET_TEXT_POS (it.position, -1, -1);
14410 PRODUCE_GLYPHS (&it);
14412 /* If this character doesn't fit any more in the line, we have
14413 to remove some glyphs. */
14414 if (it.current_x > it.last_visible_x)
14416 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14417 break;
14421 set_buffer_temp (old);
14422 return it.glyph_row;
14426 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14427 glyphs are only inserted for terminal frames since we can't really
14428 win with truncation glyphs when partially visible glyphs are
14429 involved. Which glyphs to insert is determined by
14430 produce_special_glyphs. */
14432 static void
14433 insert_left_trunc_glyphs (it)
14434 struct it *it;
14436 struct it truncate_it;
14437 struct glyph *from, *end, *to, *toend;
14439 xassert (!FRAME_WINDOW_P (it->f));
14441 /* Get the truncation glyphs. */
14442 truncate_it = *it;
14443 truncate_it.current_x = 0;
14444 truncate_it.face_id = DEFAULT_FACE_ID;
14445 truncate_it.glyph_row = &scratch_glyph_row;
14446 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14447 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14448 truncate_it.object = make_number (0);
14449 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14451 /* Overwrite glyphs from IT with truncation glyphs. */
14452 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14453 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14454 to = it->glyph_row->glyphs[TEXT_AREA];
14455 toend = to + it->glyph_row->used[TEXT_AREA];
14457 while (from < end)
14458 *to++ = *from++;
14460 /* There may be padding glyphs left over. Overwrite them too. */
14461 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14463 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14464 while (from < end)
14465 *to++ = *from++;
14468 if (to > toend)
14469 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14473 /* Compute the pixel height and width of IT->glyph_row.
14475 Most of the time, ascent and height of a display line will be equal
14476 to the max_ascent and max_height values of the display iterator
14477 structure. This is not the case if
14479 1. We hit ZV without displaying anything. In this case, max_ascent
14480 and max_height will be zero.
14482 2. We have some glyphs that don't contribute to the line height.
14483 (The glyph row flag contributes_to_line_height_p is for future
14484 pixmap extensions).
14486 The first case is easily covered by using default values because in
14487 these cases, the line height does not really matter, except that it
14488 must not be zero. */
14490 static void
14491 compute_line_metrics (it)
14492 struct it *it;
14494 struct glyph_row *row = it->glyph_row;
14495 int area, i;
14497 if (FRAME_WINDOW_P (it->f))
14499 int i, min_y, max_y;
14501 /* The line may consist of one space only, that was added to
14502 place the cursor on it. If so, the row's height hasn't been
14503 computed yet. */
14504 if (row->height == 0)
14506 if (it->max_ascent + it->max_descent == 0)
14507 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14508 row->ascent = it->max_ascent;
14509 row->height = it->max_ascent + it->max_descent;
14510 row->phys_ascent = it->max_phys_ascent;
14511 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14512 row->extra_line_spacing = it->max_extra_line_spacing;
14515 /* Compute the width of this line. */
14516 row->pixel_width = row->x;
14517 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14518 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14520 xassert (row->pixel_width >= 0);
14521 xassert (row->ascent >= 0 && row->height > 0);
14523 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14524 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14526 /* If first line's physical ascent is larger than its logical
14527 ascent, use the physical ascent, and make the row taller.
14528 This makes accented characters fully visible. */
14529 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14530 && row->phys_ascent > row->ascent)
14532 row->height += row->phys_ascent - row->ascent;
14533 row->ascent = row->phys_ascent;
14536 /* Compute how much of the line is visible. */
14537 row->visible_height = row->height;
14539 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14540 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14542 if (row->y < min_y)
14543 row->visible_height -= min_y - row->y;
14544 if (row->y + row->height > max_y)
14545 row->visible_height -= row->y + row->height - max_y;
14547 else
14549 row->pixel_width = row->used[TEXT_AREA];
14550 if (row->continued_p)
14551 row->pixel_width -= it->continuation_pixel_width;
14552 else if (row->truncated_on_right_p)
14553 row->pixel_width -= it->truncation_pixel_width;
14554 row->ascent = row->phys_ascent = 0;
14555 row->height = row->phys_height = row->visible_height = 1;
14556 row->extra_line_spacing = 0;
14559 /* Compute a hash code for this row. */
14560 row->hash = 0;
14561 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14562 for (i = 0; i < row->used[area]; ++i)
14563 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14564 + row->glyphs[area][i].u.val
14565 + row->glyphs[area][i].face_id
14566 + row->glyphs[area][i].padding_p
14567 + (row->glyphs[area][i].type << 2));
14569 it->max_ascent = it->max_descent = 0;
14570 it->max_phys_ascent = it->max_phys_descent = 0;
14574 /* Append one space to the glyph row of iterator IT if doing a
14575 window-based redisplay. The space has the same face as
14576 IT->face_id. Value is non-zero if a space was added.
14578 This function is called to make sure that there is always one glyph
14579 at the end of a glyph row that the cursor can be set on under
14580 window-systems. (If there weren't such a glyph we would not know
14581 how wide and tall a box cursor should be displayed).
14583 At the same time this space let's a nicely handle clearing to the
14584 end of the line if the row ends in italic text. */
14586 static int
14587 append_space_for_newline (it, default_face_p)
14588 struct it *it;
14589 int default_face_p;
14591 if (FRAME_WINDOW_P (it->f))
14593 int n = it->glyph_row->used[TEXT_AREA];
14595 if (it->glyph_row->glyphs[TEXT_AREA] + n
14596 < it->glyph_row->glyphs[1 + TEXT_AREA])
14598 /* Save some values that must not be changed.
14599 Must save IT->c and IT->len because otherwise
14600 ITERATOR_AT_END_P wouldn't work anymore after
14601 append_space_for_newline has been called. */
14602 enum display_element_type saved_what = it->what;
14603 int saved_c = it->c, saved_len = it->len;
14604 int saved_x = it->current_x;
14605 int saved_face_id = it->face_id;
14606 struct text_pos saved_pos;
14607 Lisp_Object saved_object;
14608 struct face *face;
14610 saved_object = it->object;
14611 saved_pos = it->position;
14613 it->what = IT_CHARACTER;
14614 bzero (&it->position, sizeof it->position);
14615 it->object = make_number (0);
14616 it->c = ' ';
14617 it->len = 1;
14619 if (default_face_p)
14620 it->face_id = DEFAULT_FACE_ID;
14621 else if (it->face_before_selective_p)
14622 it->face_id = it->saved_face_id;
14623 face = FACE_FROM_ID (it->f, it->face_id);
14624 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14626 PRODUCE_GLYPHS (it);
14628 it->override_ascent = -1;
14629 it->constrain_row_ascent_descent_p = 0;
14630 it->current_x = saved_x;
14631 it->object = saved_object;
14632 it->position = saved_pos;
14633 it->what = saved_what;
14634 it->face_id = saved_face_id;
14635 it->len = saved_len;
14636 it->c = saved_c;
14637 return 1;
14641 return 0;
14645 /* Extend the face of the last glyph in the text area of IT->glyph_row
14646 to the end of the display line. Called from display_line.
14647 If the glyph row is empty, add a space glyph to it so that we
14648 know the face to draw. Set the glyph row flag fill_line_p. */
14650 static void
14651 extend_face_to_end_of_line (it)
14652 struct it *it;
14654 struct face *face;
14655 struct frame *f = it->f;
14657 /* If line is already filled, do nothing. */
14658 if (it->current_x >= it->last_visible_x)
14659 return;
14661 /* Face extension extends the background and box of IT->face_id
14662 to the end of the line. If the background equals the background
14663 of the frame, we don't have to do anything. */
14664 if (it->face_before_selective_p)
14665 face = FACE_FROM_ID (it->f, it->saved_face_id);
14666 else
14667 face = FACE_FROM_ID (f, it->face_id);
14669 if (FRAME_WINDOW_P (f)
14670 && face->box == FACE_NO_BOX
14671 && face->background == FRAME_BACKGROUND_PIXEL (f)
14672 && !face->stipple)
14673 return;
14675 /* Set the glyph row flag indicating that the face of the last glyph
14676 in the text area has to be drawn to the end of the text area. */
14677 it->glyph_row->fill_line_p = 1;
14679 /* If current character of IT is not ASCII, make sure we have the
14680 ASCII face. This will be automatically undone the next time
14681 get_next_display_element returns a multibyte character. Note
14682 that the character will always be single byte in unibyte text. */
14683 if (!SINGLE_BYTE_CHAR_P (it->c))
14685 it->face_id = FACE_FOR_CHAR (f, face, 0);
14688 if (FRAME_WINDOW_P (f))
14690 /* If the row is empty, add a space with the current face of IT,
14691 so that we know which face to draw. */
14692 if (it->glyph_row->used[TEXT_AREA] == 0)
14694 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14695 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14696 it->glyph_row->used[TEXT_AREA] = 1;
14699 else
14701 /* Save some values that must not be changed. */
14702 int saved_x = it->current_x;
14703 struct text_pos saved_pos;
14704 Lisp_Object saved_object;
14705 enum display_element_type saved_what = it->what;
14706 int saved_face_id = it->face_id;
14708 saved_object = it->object;
14709 saved_pos = it->position;
14711 it->what = IT_CHARACTER;
14712 bzero (&it->position, sizeof it->position);
14713 it->object = make_number (0);
14714 it->c = ' ';
14715 it->len = 1;
14716 it->face_id = face->id;
14718 PRODUCE_GLYPHS (it);
14720 while (it->current_x <= it->last_visible_x)
14721 PRODUCE_GLYPHS (it);
14723 /* Don't count these blanks really. It would let us insert a left
14724 truncation glyph below and make us set the cursor on them, maybe. */
14725 it->current_x = saved_x;
14726 it->object = saved_object;
14727 it->position = saved_pos;
14728 it->what = saved_what;
14729 it->face_id = saved_face_id;
14734 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14735 trailing whitespace. */
14737 static int
14738 trailing_whitespace_p (charpos)
14739 int charpos;
14741 int bytepos = CHAR_TO_BYTE (charpos);
14742 int c = 0;
14744 while (bytepos < ZV_BYTE
14745 && (c = FETCH_CHAR (bytepos),
14746 c == ' ' || c == '\t'))
14747 ++bytepos;
14749 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14751 if (bytepos != PT_BYTE)
14752 return 1;
14754 return 0;
14758 /* Highlight trailing whitespace, if any, in ROW. */
14760 void
14761 highlight_trailing_whitespace (f, row)
14762 struct frame *f;
14763 struct glyph_row *row;
14765 int used = row->used[TEXT_AREA];
14767 if (used)
14769 struct glyph *start = row->glyphs[TEXT_AREA];
14770 struct glyph *glyph = start + used - 1;
14772 /* Skip over glyphs inserted to display the cursor at the
14773 end of a line, for extending the face of the last glyph
14774 to the end of the line on terminals, and for truncation
14775 and continuation glyphs. */
14776 while (glyph >= start
14777 && glyph->type == CHAR_GLYPH
14778 && INTEGERP (glyph->object))
14779 --glyph;
14781 /* If last glyph is a space or stretch, and it's trailing
14782 whitespace, set the face of all trailing whitespace glyphs in
14783 IT->glyph_row to `trailing-whitespace'. */
14784 if (glyph >= start
14785 && BUFFERP (glyph->object)
14786 && (glyph->type == STRETCH_GLYPH
14787 || (glyph->type == CHAR_GLYPH
14788 && glyph->u.ch == ' '))
14789 && trailing_whitespace_p (glyph->charpos))
14791 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14792 if (face_id < 0)
14793 return;
14795 while (glyph >= start
14796 && BUFFERP (glyph->object)
14797 && (glyph->type == STRETCH_GLYPH
14798 || (glyph->type == CHAR_GLYPH
14799 && glyph->u.ch == ' ')))
14800 (glyph--)->face_id = face_id;
14806 /* Value is non-zero if glyph row ROW in window W should be
14807 used to hold the cursor. */
14809 static int
14810 cursor_row_p (w, row)
14811 struct window *w;
14812 struct glyph_row *row;
14814 int cursor_row_p = 1;
14816 if (PT == MATRIX_ROW_END_CHARPOS (row))
14818 /* If the row ends with a newline from a string, we don't want
14819 the cursor there (if the row is continued it doesn't end in a
14820 newline). */
14821 if (CHARPOS (row->end.string_pos) >= 0)
14822 cursor_row_p = row->continued_p;
14823 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14825 /* If the row ends in middle of a real character,
14826 and the line is continued, we want the cursor here.
14827 That's because MATRIX_ROW_END_CHARPOS would equal
14828 PT if PT is before the character. */
14829 if (!row->ends_in_ellipsis_p)
14830 cursor_row_p = row->continued_p;
14831 else
14832 /* If the row ends in an ellipsis, then
14833 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14834 We want that position to be displayed after the ellipsis. */
14835 cursor_row_p = 0;
14837 /* If the row ends at ZV, display the cursor at the end of that
14838 row instead of at the start of the row below. */
14839 else if (row->ends_at_zv_p)
14840 cursor_row_p = 1;
14841 else
14842 cursor_row_p = 0;
14845 return cursor_row_p;
14849 /* Construct the glyph row IT->glyph_row in the desired matrix of
14850 IT->w from text at the current position of IT. See dispextern.h
14851 for an overview of struct it. Value is non-zero if
14852 IT->glyph_row displays text, as opposed to a line displaying ZV
14853 only. */
14855 static int
14856 display_line (it)
14857 struct it *it;
14859 struct glyph_row *row = it->glyph_row;
14860 Lisp_Object overlay_arrow_string;
14862 /* We always start displaying at hpos zero even if hscrolled. */
14863 xassert (it->hpos == 0 && it->current_x == 0);
14865 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14866 >= it->w->desired_matrix->nrows)
14868 it->w->nrows_scale_factor++;
14869 fonts_changed_p = 1;
14870 return 0;
14873 /* Is IT->w showing the region? */
14874 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14876 /* Clear the result glyph row and enable it. */
14877 prepare_desired_row (row);
14879 row->y = it->current_y;
14880 row->start = it->start;
14881 row->continuation_lines_width = it->continuation_lines_width;
14882 row->displays_text_p = 1;
14883 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14884 it->starts_in_middle_of_char_p = 0;
14886 /* Arrange the overlays nicely for our purposes. Usually, we call
14887 display_line on only one line at a time, in which case this
14888 can't really hurt too much, or we call it on lines which appear
14889 one after another in the buffer, in which case all calls to
14890 recenter_overlay_lists but the first will be pretty cheap. */
14891 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14893 /* Move over display elements that are not visible because we are
14894 hscrolled. This may stop at an x-position < IT->first_visible_x
14895 if the first glyph is partially visible or if we hit a line end. */
14896 if (it->current_x < it->first_visible_x)
14898 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14899 MOVE_TO_POS | MOVE_TO_X);
14902 /* Get the initial row height. This is either the height of the
14903 text hscrolled, if there is any, or zero. */
14904 row->ascent = it->max_ascent;
14905 row->height = it->max_ascent + it->max_descent;
14906 row->phys_ascent = it->max_phys_ascent;
14907 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14908 row->extra_line_spacing = it->max_extra_line_spacing;
14910 /* Loop generating characters. The loop is left with IT on the next
14911 character to display. */
14912 while (1)
14914 int n_glyphs_before, hpos_before, x_before;
14915 int x, i, nglyphs;
14916 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14918 /* Retrieve the next thing to display. Value is zero if end of
14919 buffer reached. */
14920 if (!get_next_display_element (it))
14922 /* Maybe add a space at the end of this line that is used to
14923 display the cursor there under X. Set the charpos of the
14924 first glyph of blank lines not corresponding to any text
14925 to -1. */
14926 #ifdef HAVE_WINDOW_SYSTEM
14927 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14928 row->exact_window_width_line_p = 1;
14929 else
14930 #endif /* HAVE_WINDOW_SYSTEM */
14931 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14932 || row->used[TEXT_AREA] == 0)
14934 row->glyphs[TEXT_AREA]->charpos = -1;
14935 row->displays_text_p = 0;
14937 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14938 && (!MINI_WINDOW_P (it->w)
14939 || (minibuf_level && EQ (it->window, minibuf_window))))
14940 row->indicate_empty_line_p = 1;
14943 it->continuation_lines_width = 0;
14944 row->ends_at_zv_p = 1;
14945 break;
14948 /* Now, get the metrics of what we want to display. This also
14949 generates glyphs in `row' (which is IT->glyph_row). */
14950 n_glyphs_before = row->used[TEXT_AREA];
14951 x = it->current_x;
14953 /* Remember the line height so far in case the next element doesn't
14954 fit on the line. */
14955 if (!it->truncate_lines_p)
14957 ascent = it->max_ascent;
14958 descent = it->max_descent;
14959 phys_ascent = it->max_phys_ascent;
14960 phys_descent = it->max_phys_descent;
14963 PRODUCE_GLYPHS (it);
14965 /* If this display element was in marginal areas, continue with
14966 the next one. */
14967 if (it->area != TEXT_AREA)
14969 row->ascent = max (row->ascent, it->max_ascent);
14970 row->height = max (row->height, it->max_ascent + it->max_descent);
14971 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14972 row->phys_height = max (row->phys_height,
14973 it->max_phys_ascent + it->max_phys_descent);
14974 row->extra_line_spacing = max (row->extra_line_spacing,
14975 it->max_extra_line_spacing);
14976 set_iterator_to_next (it, 1);
14977 continue;
14980 /* Does the display element fit on the line? If we truncate
14981 lines, we should draw past the right edge of the window. If
14982 we don't truncate, we want to stop so that we can display the
14983 continuation glyph before the right margin. If lines are
14984 continued, there are two possible strategies for characters
14985 resulting in more than 1 glyph (e.g. tabs): Display as many
14986 glyphs as possible in this line and leave the rest for the
14987 continuation line, or display the whole element in the next
14988 line. Original redisplay did the former, so we do it also. */
14989 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14990 hpos_before = it->hpos;
14991 x_before = x;
14993 if (/* Not a newline. */
14994 nglyphs > 0
14995 /* Glyphs produced fit entirely in the line. */
14996 && it->current_x < it->last_visible_x)
14998 it->hpos += nglyphs;
14999 row->ascent = max (row->ascent, it->max_ascent);
15000 row->height = max (row->height, it->max_ascent + it->max_descent);
15001 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15002 row->phys_height = max (row->phys_height,
15003 it->max_phys_ascent + it->max_phys_descent);
15004 row->extra_line_spacing = max (row->extra_line_spacing,
15005 it->max_extra_line_spacing);
15006 if (it->current_x - it->pixel_width < it->first_visible_x)
15007 row->x = x - it->first_visible_x;
15009 else
15011 int new_x;
15012 struct glyph *glyph;
15014 for (i = 0; i < nglyphs; ++i, x = new_x)
15016 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15017 new_x = x + glyph->pixel_width;
15019 if (/* Lines are continued. */
15020 !it->truncate_lines_p
15021 && (/* Glyph doesn't fit on the line. */
15022 new_x > it->last_visible_x
15023 /* Or it fits exactly on a window system frame. */
15024 || (new_x == it->last_visible_x
15025 && FRAME_WINDOW_P (it->f))))
15027 /* End of a continued line. */
15029 if (it->hpos == 0
15030 || (new_x == it->last_visible_x
15031 && FRAME_WINDOW_P (it->f)))
15033 /* Current glyph is the only one on the line or
15034 fits exactly on the line. We must continue
15035 the line because we can't draw the cursor
15036 after the glyph. */
15037 row->continued_p = 1;
15038 it->current_x = new_x;
15039 it->continuation_lines_width += new_x;
15040 ++it->hpos;
15041 if (i == nglyphs - 1)
15043 set_iterator_to_next (it, 1);
15044 #ifdef HAVE_WINDOW_SYSTEM
15045 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15047 if (!get_next_display_element (it))
15049 row->exact_window_width_line_p = 1;
15050 it->continuation_lines_width = 0;
15051 row->continued_p = 0;
15052 row->ends_at_zv_p = 1;
15054 else if (ITERATOR_AT_END_OF_LINE_P (it))
15056 row->continued_p = 0;
15057 row->exact_window_width_line_p = 1;
15060 #endif /* HAVE_WINDOW_SYSTEM */
15063 else if (CHAR_GLYPH_PADDING_P (*glyph)
15064 && !FRAME_WINDOW_P (it->f))
15066 /* A padding glyph that doesn't fit on this line.
15067 This means the whole character doesn't fit
15068 on the line. */
15069 row->used[TEXT_AREA] = n_glyphs_before;
15071 /* Fill the rest of the row with continuation
15072 glyphs like in 20.x. */
15073 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15074 < row->glyphs[1 + TEXT_AREA])
15075 produce_special_glyphs (it, IT_CONTINUATION);
15077 row->continued_p = 1;
15078 it->current_x = x_before;
15079 it->continuation_lines_width += x_before;
15081 /* Restore the height to what it was before the
15082 element not fitting on the line. */
15083 it->max_ascent = ascent;
15084 it->max_descent = descent;
15085 it->max_phys_ascent = phys_ascent;
15086 it->max_phys_descent = phys_descent;
15088 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15090 /* A TAB that extends past the right edge of the
15091 window. This produces a single glyph on
15092 window system frames. We leave the glyph in
15093 this row and let it fill the row, but don't
15094 consume the TAB. */
15095 it->continuation_lines_width += it->last_visible_x;
15096 row->ends_in_middle_of_char_p = 1;
15097 row->continued_p = 1;
15098 glyph->pixel_width = it->last_visible_x - x;
15099 it->starts_in_middle_of_char_p = 1;
15101 else
15103 /* Something other than a TAB that draws past
15104 the right edge of the window. Restore
15105 positions to values before the element. */
15106 row->used[TEXT_AREA] = n_glyphs_before + i;
15108 /* Display continuation glyphs. */
15109 if (!FRAME_WINDOW_P (it->f))
15110 produce_special_glyphs (it, IT_CONTINUATION);
15111 row->continued_p = 1;
15113 it->continuation_lines_width += x;
15115 if (nglyphs > 1 && i > 0)
15117 row->ends_in_middle_of_char_p = 1;
15118 it->starts_in_middle_of_char_p = 1;
15121 /* Restore the height to what it was before the
15122 element not fitting on the line. */
15123 it->max_ascent = ascent;
15124 it->max_descent = descent;
15125 it->max_phys_ascent = phys_ascent;
15126 it->max_phys_descent = phys_descent;
15129 break;
15131 else if (new_x > it->first_visible_x)
15133 /* Increment number of glyphs actually displayed. */
15134 ++it->hpos;
15136 if (x < it->first_visible_x)
15137 /* Glyph is partially visible, i.e. row starts at
15138 negative X position. */
15139 row->x = x - it->first_visible_x;
15141 else
15143 /* Glyph is completely off the left margin of the
15144 window. This should not happen because of the
15145 move_it_in_display_line at the start of this
15146 function, unless the text display area of the
15147 window is empty. */
15148 xassert (it->first_visible_x <= it->last_visible_x);
15152 row->ascent = max (row->ascent, it->max_ascent);
15153 row->height = max (row->height, it->max_ascent + it->max_descent);
15154 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15155 row->phys_height = max (row->phys_height,
15156 it->max_phys_ascent + it->max_phys_descent);
15157 row->extra_line_spacing = max (row->extra_line_spacing,
15158 it->max_extra_line_spacing);
15160 /* End of this display line if row is continued. */
15161 if (row->continued_p || row->ends_at_zv_p)
15162 break;
15165 at_end_of_line:
15166 /* Is this a line end? If yes, we're also done, after making
15167 sure that a non-default face is extended up to the right
15168 margin of the window. */
15169 if (ITERATOR_AT_END_OF_LINE_P (it))
15171 int used_before = row->used[TEXT_AREA];
15173 row->ends_in_newline_from_string_p = STRINGP (it->object);
15175 #ifdef HAVE_WINDOW_SYSTEM
15176 /* Add a space at the end of the line that is used to
15177 display the cursor there. */
15178 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15179 append_space_for_newline (it, 0);
15180 #endif /* HAVE_WINDOW_SYSTEM */
15182 /* Extend the face to the end of the line. */
15183 extend_face_to_end_of_line (it);
15185 /* Make sure we have the position. */
15186 if (used_before == 0)
15187 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15189 /* Consume the line end. This skips over invisible lines. */
15190 set_iterator_to_next (it, 1);
15191 it->continuation_lines_width = 0;
15192 break;
15195 /* Proceed with next display element. Note that this skips
15196 over lines invisible because of selective display. */
15197 set_iterator_to_next (it, 1);
15199 /* If we truncate lines, we are done when the last displayed
15200 glyphs reach past the right margin of the window. */
15201 if (it->truncate_lines_p
15202 && (FRAME_WINDOW_P (it->f)
15203 ? (it->current_x >= it->last_visible_x)
15204 : (it->current_x > it->last_visible_x)))
15206 /* Maybe add truncation glyphs. */
15207 if (!FRAME_WINDOW_P (it->f))
15209 int i, n;
15211 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15212 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15213 break;
15215 for (n = row->used[TEXT_AREA]; i < n; ++i)
15217 row->used[TEXT_AREA] = i;
15218 produce_special_glyphs (it, IT_TRUNCATION);
15221 #ifdef HAVE_WINDOW_SYSTEM
15222 else
15224 /* Don't truncate if we can overflow newline into fringe. */
15225 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15227 if (!get_next_display_element (it))
15229 it->continuation_lines_width = 0;
15230 row->ends_at_zv_p = 1;
15231 row->exact_window_width_line_p = 1;
15232 break;
15234 if (ITERATOR_AT_END_OF_LINE_P (it))
15236 row->exact_window_width_line_p = 1;
15237 goto at_end_of_line;
15241 #endif /* HAVE_WINDOW_SYSTEM */
15243 row->truncated_on_right_p = 1;
15244 it->continuation_lines_width = 0;
15245 reseat_at_next_visible_line_start (it, 0);
15246 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15247 it->hpos = hpos_before;
15248 it->current_x = x_before;
15249 break;
15253 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15254 at the left window margin. */
15255 if (it->first_visible_x
15256 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15258 if (!FRAME_WINDOW_P (it->f))
15259 insert_left_trunc_glyphs (it);
15260 row->truncated_on_left_p = 1;
15263 /* If the start of this line is the overlay arrow-position, then
15264 mark this glyph row as the one containing the overlay arrow.
15265 This is clearly a mess with variable size fonts. It would be
15266 better to let it be displayed like cursors under X. */
15267 if ((row->displays_text_p || !overlay_arrow_seen)
15268 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15269 !NILP (overlay_arrow_string)))
15271 /* Overlay arrow in window redisplay is a fringe bitmap. */
15272 if (STRINGP (overlay_arrow_string))
15274 struct glyph_row *arrow_row
15275 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15276 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15277 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15278 struct glyph *p = row->glyphs[TEXT_AREA];
15279 struct glyph *p2, *end;
15281 /* Copy the arrow glyphs. */
15282 while (glyph < arrow_end)
15283 *p++ = *glyph++;
15285 /* Throw away padding glyphs. */
15286 p2 = p;
15287 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15288 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15289 ++p2;
15290 if (p2 > p)
15292 while (p2 < end)
15293 *p++ = *p2++;
15294 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15297 else
15299 xassert (INTEGERP (overlay_arrow_string));
15300 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15302 overlay_arrow_seen = 1;
15305 /* Compute pixel dimensions of this line. */
15306 compute_line_metrics (it);
15308 /* Remember the position at which this line ends. */
15309 row->end = it->current;
15311 /* Record whether this row ends inside an ellipsis. */
15312 row->ends_in_ellipsis_p
15313 = (it->method == GET_FROM_DISPLAY_VECTOR
15314 && it->ellipsis_p);
15316 /* Save fringe bitmaps in this row. */
15317 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15318 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15319 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15320 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15322 it->left_user_fringe_bitmap = 0;
15323 it->left_user_fringe_face_id = 0;
15324 it->right_user_fringe_bitmap = 0;
15325 it->right_user_fringe_face_id = 0;
15327 /* Maybe set the cursor. */
15328 if (it->w->cursor.vpos < 0
15329 && PT >= MATRIX_ROW_START_CHARPOS (row)
15330 && PT <= MATRIX_ROW_END_CHARPOS (row)
15331 && cursor_row_p (it->w, row))
15332 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15334 /* Highlight trailing whitespace. */
15335 if (!NILP (Vshow_trailing_whitespace))
15336 highlight_trailing_whitespace (it->f, it->glyph_row);
15338 /* Prepare for the next line. This line starts horizontally at (X
15339 HPOS) = (0 0). Vertical positions are incremented. As a
15340 convenience for the caller, IT->glyph_row is set to the next
15341 row to be used. */
15342 it->current_x = it->hpos = 0;
15343 it->current_y += row->height;
15344 ++it->vpos;
15345 ++it->glyph_row;
15346 it->start = it->current;
15347 return row->displays_text_p;
15352 /***********************************************************************
15353 Menu Bar
15354 ***********************************************************************/
15356 /* Redisplay the menu bar in the frame for window W.
15358 The menu bar of X frames that don't have X toolkit support is
15359 displayed in a special window W->frame->menu_bar_window.
15361 The menu bar of terminal frames is treated specially as far as
15362 glyph matrices are concerned. Menu bar lines are not part of
15363 windows, so the update is done directly on the frame matrix rows
15364 for the menu bar. */
15366 static void
15367 display_menu_bar (w)
15368 struct window *w;
15370 struct frame *f = XFRAME (WINDOW_FRAME (w));
15371 struct it it;
15372 Lisp_Object items;
15373 int i;
15375 /* Don't do all this for graphical frames. */
15376 #ifdef HAVE_NTGUI
15377 if (!NILP (Vwindow_system))
15378 return;
15379 #endif
15380 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15381 if (FRAME_X_P (f))
15382 return;
15383 #endif
15384 #ifdef MAC_OS
15385 if (FRAME_MAC_P (f))
15386 return;
15387 #endif
15389 #ifdef USE_X_TOOLKIT
15390 xassert (!FRAME_WINDOW_P (f));
15391 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15392 it.first_visible_x = 0;
15393 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15394 #else /* not USE_X_TOOLKIT */
15395 if (FRAME_WINDOW_P (f))
15397 /* Menu bar lines are displayed in the desired matrix of the
15398 dummy window menu_bar_window. */
15399 struct window *menu_w;
15400 xassert (WINDOWP (f->menu_bar_window));
15401 menu_w = XWINDOW (f->menu_bar_window);
15402 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15403 MENU_FACE_ID);
15404 it.first_visible_x = 0;
15405 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15407 else
15409 /* This is a TTY frame, i.e. character hpos/vpos are used as
15410 pixel x/y. */
15411 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15412 MENU_FACE_ID);
15413 it.first_visible_x = 0;
15414 it.last_visible_x = FRAME_COLS (f);
15416 #endif /* not USE_X_TOOLKIT */
15418 if (! mode_line_inverse_video)
15419 /* Force the menu-bar to be displayed in the default face. */
15420 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15422 /* Clear all rows of the menu bar. */
15423 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15425 struct glyph_row *row = it.glyph_row + i;
15426 clear_glyph_row (row);
15427 row->enabled_p = 1;
15428 row->full_width_p = 1;
15431 /* Display all items of the menu bar. */
15432 items = FRAME_MENU_BAR_ITEMS (it.f);
15433 for (i = 0; i < XVECTOR (items)->size; i += 4)
15435 Lisp_Object string;
15437 /* Stop at nil string. */
15438 string = AREF (items, i + 1);
15439 if (NILP (string))
15440 break;
15442 /* Remember where item was displayed. */
15443 AREF (items, i + 3) = make_number (it.hpos);
15445 /* Display the item, pad with one space. */
15446 if (it.current_x < it.last_visible_x)
15447 display_string (NULL, string, Qnil, 0, 0, &it,
15448 SCHARS (string) + 1, 0, 0, -1);
15451 /* Fill out the line with spaces. */
15452 if (it.current_x < it.last_visible_x)
15453 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15455 /* Compute the total height of the lines. */
15456 compute_line_metrics (&it);
15461 /***********************************************************************
15462 Mode Line
15463 ***********************************************************************/
15465 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15466 FORCE is non-zero, redisplay mode lines unconditionally.
15467 Otherwise, redisplay only mode lines that are garbaged. Value is
15468 the number of windows whose mode lines were redisplayed. */
15470 static int
15471 redisplay_mode_lines (window, force)
15472 Lisp_Object window;
15473 int force;
15475 int nwindows = 0;
15477 while (!NILP (window))
15479 struct window *w = XWINDOW (window);
15481 if (WINDOWP (w->hchild))
15482 nwindows += redisplay_mode_lines (w->hchild, force);
15483 else if (WINDOWP (w->vchild))
15484 nwindows += redisplay_mode_lines (w->vchild, force);
15485 else if (force
15486 || FRAME_GARBAGED_P (XFRAME (w->frame))
15487 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15489 struct text_pos lpoint;
15490 struct buffer *old = current_buffer;
15492 /* Set the window's buffer for the mode line display. */
15493 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15494 set_buffer_internal_1 (XBUFFER (w->buffer));
15496 /* Point refers normally to the selected window. For any
15497 other window, set up appropriate value. */
15498 if (!EQ (window, selected_window))
15500 struct text_pos pt;
15502 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15503 if (CHARPOS (pt) < BEGV)
15504 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15505 else if (CHARPOS (pt) > (ZV - 1))
15506 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15507 else
15508 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15511 /* Display mode lines. */
15512 clear_glyph_matrix (w->desired_matrix);
15513 if (display_mode_lines (w))
15515 ++nwindows;
15516 w->must_be_updated_p = 1;
15519 /* Restore old settings. */
15520 set_buffer_internal_1 (old);
15521 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15524 window = w->next;
15527 return nwindows;
15531 /* Display the mode and/or top line of window W. Value is the number
15532 of mode lines displayed. */
15534 static int
15535 display_mode_lines (w)
15536 struct window *w;
15538 Lisp_Object old_selected_window, old_selected_frame;
15539 int n = 0;
15541 old_selected_frame = selected_frame;
15542 selected_frame = w->frame;
15543 old_selected_window = selected_window;
15544 XSETWINDOW (selected_window, w);
15546 /* These will be set while the mode line specs are processed. */
15547 line_number_displayed = 0;
15548 w->column_number_displayed = Qnil;
15550 if (WINDOW_WANTS_MODELINE_P (w))
15552 struct window *sel_w = XWINDOW (old_selected_window);
15554 /* Select mode line face based on the real selected window. */
15555 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15556 current_buffer->mode_line_format);
15557 ++n;
15560 if (WINDOW_WANTS_HEADER_LINE_P (w))
15562 display_mode_line (w, HEADER_LINE_FACE_ID,
15563 current_buffer->header_line_format);
15564 ++n;
15567 selected_frame = old_selected_frame;
15568 selected_window = old_selected_window;
15569 return n;
15573 /* Display mode or top line of window W. FACE_ID specifies which line
15574 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15575 FORMAT is the mode line format to display. Value is the pixel
15576 height of the mode line displayed. */
15578 static int
15579 display_mode_line (w, face_id, format)
15580 struct window *w;
15581 enum face_id face_id;
15582 Lisp_Object format;
15584 struct it it;
15585 struct face *face;
15587 init_iterator (&it, w, -1, -1, NULL, face_id);
15588 prepare_desired_row (it.glyph_row);
15590 it.glyph_row->mode_line_p = 1;
15592 if (! mode_line_inverse_video)
15593 /* Force the mode-line to be displayed in the default face. */
15594 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15596 /* Temporarily make frame's keyboard the current kboard so that
15597 kboard-local variables in the mode_line_format will get the right
15598 values. */
15599 push_frame_kboard (it.f);
15600 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15601 pop_frame_kboard ();
15603 /* Fill up with spaces. */
15604 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15606 compute_line_metrics (&it);
15607 it.glyph_row->full_width_p = 1;
15608 it.glyph_row->continued_p = 0;
15609 it.glyph_row->truncated_on_left_p = 0;
15610 it.glyph_row->truncated_on_right_p = 0;
15612 /* Make a 3D mode-line have a shadow at its right end. */
15613 face = FACE_FROM_ID (it.f, face_id);
15614 extend_face_to_end_of_line (&it);
15615 if (face->box != FACE_NO_BOX)
15617 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15618 + it.glyph_row->used[TEXT_AREA] - 1);
15619 last->right_box_line_p = 1;
15622 return it.glyph_row->height;
15625 /* Alist that caches the results of :propertize.
15626 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15627 Lisp_Object mode_line_proptrans_alist;
15629 /* List of strings making up the mode-line. */
15630 Lisp_Object mode_line_string_list;
15632 /* Base face property when building propertized mode line string. */
15633 static Lisp_Object mode_line_string_face;
15634 static Lisp_Object mode_line_string_face_prop;
15637 /* Contribute ELT to the mode line for window IT->w. How it
15638 translates into text depends on its data type.
15640 IT describes the display environment in which we display, as usual.
15642 DEPTH is the depth in recursion. It is used to prevent
15643 infinite recursion here.
15645 FIELD_WIDTH is the number of characters the display of ELT should
15646 occupy in the mode line, and PRECISION is the maximum number of
15647 characters to display from ELT's representation. See
15648 display_string for details.
15650 Returns the hpos of the end of the text generated by ELT.
15652 PROPS is a property list to add to any string we encounter.
15654 If RISKY is nonzero, remove (disregard) any properties in any string
15655 we encounter, and ignore :eval and :propertize.
15657 If the global variable `frame_title_ptr' is non-NULL, then the output
15658 is passed to `store_frame_title' instead of `display_string'. */
15660 static int
15661 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15662 struct it *it;
15663 int depth;
15664 int field_width, precision;
15665 Lisp_Object elt, props;
15666 int risky;
15668 int n = 0, field, prec;
15669 int literal = 0;
15671 tail_recurse:
15672 if (depth > 100)
15673 elt = build_string ("*too-deep*");
15675 depth++;
15677 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15679 case Lisp_String:
15681 /* A string: output it and check for %-constructs within it. */
15682 unsigned char c;
15683 const unsigned char *this, *lisp_string;
15685 if (!NILP (props) || risky)
15687 Lisp_Object oprops, aelt;
15688 oprops = Ftext_properties_at (make_number (0), elt);
15690 /* If the starting string's properties are not what
15691 we want, translate the string. Also, if the string
15692 is risky, do that anyway. */
15694 if (NILP (Fequal (props, oprops)) || risky)
15696 /* If the starting string has properties,
15697 merge the specified ones onto the existing ones. */
15698 if (! NILP (oprops) && !risky)
15700 Lisp_Object tem;
15702 oprops = Fcopy_sequence (oprops);
15703 tem = props;
15704 while (CONSP (tem))
15706 oprops = Fplist_put (oprops, XCAR (tem),
15707 XCAR (XCDR (tem)));
15708 tem = XCDR (XCDR (tem));
15710 props = oprops;
15713 aelt = Fassoc (elt, mode_line_proptrans_alist);
15714 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15716 mode_line_proptrans_alist
15717 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15718 elt = XCAR (aelt);
15720 else
15722 Lisp_Object tem;
15724 elt = Fcopy_sequence (elt);
15725 Fset_text_properties (make_number (0), Flength (elt),
15726 props, elt);
15727 /* Add this item to mode_line_proptrans_alist. */
15728 mode_line_proptrans_alist
15729 = Fcons (Fcons (elt, props),
15730 mode_line_proptrans_alist);
15731 /* Truncate mode_line_proptrans_alist
15732 to at most 50 elements. */
15733 tem = Fnthcdr (make_number (50),
15734 mode_line_proptrans_alist);
15735 if (! NILP (tem))
15736 XSETCDR (tem, Qnil);
15741 this = SDATA (elt);
15742 lisp_string = this;
15744 if (literal)
15746 prec = precision - n;
15747 if (frame_title_ptr)
15748 n += store_frame_title (SDATA (elt), -1, prec);
15749 else if (!NILP (mode_line_string_list))
15750 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15751 else
15752 n += display_string (NULL, elt, Qnil, 0, 0, it,
15753 0, prec, 0, STRING_MULTIBYTE (elt));
15755 break;
15758 while ((precision <= 0 || n < precision)
15759 && *this
15760 && (frame_title_ptr
15761 || !NILP (mode_line_string_list)
15762 || it->current_x < it->last_visible_x))
15764 const unsigned char *last = this;
15766 /* Advance to end of string or next format specifier. */
15767 while ((c = *this++) != '\0' && c != '%')
15770 if (this - 1 != last)
15772 int nchars, nbytes;
15774 /* Output to end of string or up to '%'. Field width
15775 is length of string. Don't output more than
15776 PRECISION allows us. */
15777 --this;
15779 prec = c_string_width (last, this - last, precision - n,
15780 &nchars, &nbytes);
15782 if (frame_title_ptr)
15783 n += store_frame_title (last, 0, prec);
15784 else if (!NILP (mode_line_string_list))
15786 int bytepos = last - lisp_string;
15787 int charpos = string_byte_to_char (elt, bytepos);
15788 int endpos = (precision <= 0
15789 ? string_byte_to_char (elt,
15790 this - lisp_string)
15791 : charpos + nchars);
15793 n += store_mode_line_string (NULL,
15794 Fsubstring (elt, make_number (charpos),
15795 make_number (endpos)),
15796 0, 0, 0, Qnil);
15798 else
15800 int bytepos = last - lisp_string;
15801 int charpos = string_byte_to_char (elt, bytepos);
15802 n += display_string (NULL, elt, Qnil, 0, charpos,
15803 it, 0, prec, 0,
15804 STRING_MULTIBYTE (elt));
15807 else /* c == '%' */
15809 const unsigned char *percent_position = this;
15811 /* Get the specified minimum width. Zero means
15812 don't pad. */
15813 field = 0;
15814 while ((c = *this++) >= '0' && c <= '9')
15815 field = field * 10 + c - '0';
15817 /* Don't pad beyond the total padding allowed. */
15818 if (field_width - n > 0 && field > field_width - n)
15819 field = field_width - n;
15821 /* Note that either PRECISION <= 0 or N < PRECISION. */
15822 prec = precision - n;
15824 if (c == 'M')
15825 n += display_mode_element (it, depth, field, prec,
15826 Vglobal_mode_string, props,
15827 risky);
15828 else if (c != 0)
15830 int multibyte;
15831 int bytepos, charpos;
15832 unsigned char *spec;
15834 bytepos = percent_position - lisp_string;
15835 charpos = (STRING_MULTIBYTE (elt)
15836 ? string_byte_to_char (elt, bytepos)
15837 : bytepos);
15839 spec
15840 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15842 if (frame_title_ptr)
15843 n += store_frame_title (spec, field, prec);
15844 else if (!NILP (mode_line_string_list))
15846 int len = strlen (spec);
15847 Lisp_Object tem = make_string (spec, len);
15848 props = Ftext_properties_at (make_number (charpos), elt);
15849 /* Should only keep face property in props */
15850 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15852 else
15854 int nglyphs_before, nwritten;
15856 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15857 nwritten = display_string (spec, Qnil, elt,
15858 charpos, 0, it,
15859 field, prec, 0,
15860 multibyte);
15862 /* Assign to the glyphs written above the
15863 string where the `%x' came from, position
15864 of the `%'. */
15865 if (nwritten > 0)
15867 struct glyph *glyph
15868 = (it->glyph_row->glyphs[TEXT_AREA]
15869 + nglyphs_before);
15870 int i;
15872 for (i = 0; i < nwritten; ++i)
15874 glyph[i].object = elt;
15875 glyph[i].charpos = charpos;
15878 n += nwritten;
15882 else /* c == 0 */
15883 break;
15887 break;
15889 case Lisp_Symbol:
15890 /* A symbol: process the value of the symbol recursively
15891 as if it appeared here directly. Avoid error if symbol void.
15892 Special case: if value of symbol is a string, output the string
15893 literally. */
15895 register Lisp_Object tem;
15897 /* If the variable is not marked as risky to set
15898 then its contents are risky to use. */
15899 if (NILP (Fget (elt, Qrisky_local_variable)))
15900 risky = 1;
15902 tem = Fboundp (elt);
15903 if (!NILP (tem))
15905 tem = Fsymbol_value (elt);
15906 /* If value is a string, output that string literally:
15907 don't check for % within it. */
15908 if (STRINGP (tem))
15909 literal = 1;
15911 if (!EQ (tem, elt))
15913 /* Give up right away for nil or t. */
15914 elt = tem;
15915 goto tail_recurse;
15919 break;
15921 case Lisp_Cons:
15923 register Lisp_Object car, tem;
15925 /* A cons cell: five distinct cases.
15926 If first element is :eval or :propertize, do something special.
15927 If first element is a string or a cons, process all the elements
15928 and effectively concatenate them.
15929 If first element is a negative number, truncate displaying cdr to
15930 at most that many characters. If positive, pad (with spaces)
15931 to at least that many characters.
15932 If first element is a symbol, process the cadr or caddr recursively
15933 according to whether the symbol's value is non-nil or nil. */
15934 car = XCAR (elt);
15935 if (EQ (car, QCeval))
15937 /* An element of the form (:eval FORM) means evaluate FORM
15938 and use the result as mode line elements. */
15940 if (risky)
15941 break;
15943 if (CONSP (XCDR (elt)))
15945 Lisp_Object spec;
15946 spec = safe_eval (XCAR (XCDR (elt)));
15947 n += display_mode_element (it, depth, field_width - n,
15948 precision - n, spec, props,
15949 risky);
15952 else if (EQ (car, QCpropertize))
15954 /* An element of the form (:propertize ELT PROPS...)
15955 means display ELT but applying properties PROPS. */
15957 if (risky)
15958 break;
15960 if (CONSP (XCDR (elt)))
15961 n += display_mode_element (it, depth, field_width - n,
15962 precision - n, XCAR (XCDR (elt)),
15963 XCDR (XCDR (elt)), risky);
15965 else if (SYMBOLP (car))
15967 tem = Fboundp (car);
15968 elt = XCDR (elt);
15969 if (!CONSP (elt))
15970 goto invalid;
15971 /* elt is now the cdr, and we know it is a cons cell.
15972 Use its car if CAR has a non-nil value. */
15973 if (!NILP (tem))
15975 tem = Fsymbol_value (car);
15976 if (!NILP (tem))
15978 elt = XCAR (elt);
15979 goto tail_recurse;
15982 /* Symbol's value is nil (or symbol is unbound)
15983 Get the cddr of the original list
15984 and if possible find the caddr and use that. */
15985 elt = XCDR (elt);
15986 if (NILP (elt))
15987 break;
15988 else if (!CONSP (elt))
15989 goto invalid;
15990 elt = XCAR (elt);
15991 goto tail_recurse;
15993 else if (INTEGERP (car))
15995 register int lim = XINT (car);
15996 elt = XCDR (elt);
15997 if (lim < 0)
15999 /* Negative int means reduce maximum width. */
16000 if (precision <= 0)
16001 precision = -lim;
16002 else
16003 precision = min (precision, -lim);
16005 else if (lim > 0)
16007 /* Padding specified. Don't let it be more than
16008 current maximum. */
16009 if (precision > 0)
16010 lim = min (precision, lim);
16012 /* If that's more padding than already wanted, queue it.
16013 But don't reduce padding already specified even if
16014 that is beyond the current truncation point. */
16015 field_width = max (lim, field_width);
16017 goto tail_recurse;
16019 else if (STRINGP (car) || CONSP (car))
16021 register int limit = 50;
16022 /* Limit is to protect against circular lists. */
16023 while (CONSP (elt)
16024 && --limit > 0
16025 && (precision <= 0 || n < precision))
16027 n += display_mode_element (it, depth, field_width - n,
16028 precision - n, XCAR (elt),
16029 props, risky);
16030 elt = XCDR (elt);
16034 break;
16036 default:
16037 invalid:
16038 elt = build_string ("*invalid*");
16039 goto tail_recurse;
16042 /* Pad to FIELD_WIDTH. */
16043 if (field_width > 0 && n < field_width)
16045 if (frame_title_ptr)
16046 n += store_frame_title ("", field_width - n, 0);
16047 else if (!NILP (mode_line_string_list))
16048 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16049 else
16050 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16051 0, 0, 0);
16054 return n;
16057 /* Store a mode-line string element in mode_line_string_list.
16059 If STRING is non-null, display that C string. Otherwise, the Lisp
16060 string LISP_STRING is displayed.
16062 FIELD_WIDTH is the minimum number of output glyphs to produce.
16063 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16064 with spaces. FIELD_WIDTH <= 0 means don't pad.
16066 PRECISION is the maximum number of characters to output from
16067 STRING. PRECISION <= 0 means don't truncate the string.
16069 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16070 properties to the string.
16072 PROPS are the properties to add to the string.
16073 The mode_line_string_face face property is always added to the string.
16076 static int
16077 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16078 char *string;
16079 Lisp_Object lisp_string;
16080 int copy_string;
16081 int field_width;
16082 int precision;
16083 Lisp_Object props;
16085 int len;
16086 int n = 0;
16088 if (string != NULL)
16090 len = strlen (string);
16091 if (precision > 0 && len > precision)
16092 len = precision;
16093 lisp_string = make_string (string, len);
16094 if (NILP (props))
16095 props = mode_line_string_face_prop;
16096 else if (!NILP (mode_line_string_face))
16098 Lisp_Object face = Fplist_get (props, Qface);
16099 props = Fcopy_sequence (props);
16100 if (NILP (face))
16101 face = mode_line_string_face;
16102 else
16103 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16104 props = Fplist_put (props, Qface, face);
16106 Fadd_text_properties (make_number (0), make_number (len),
16107 props, lisp_string);
16109 else
16111 len = XFASTINT (Flength (lisp_string));
16112 if (precision > 0 && len > precision)
16114 len = precision;
16115 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16116 precision = -1;
16118 if (!NILP (mode_line_string_face))
16120 Lisp_Object face;
16121 if (NILP (props))
16122 props = Ftext_properties_at (make_number (0), lisp_string);
16123 face = Fplist_get (props, Qface);
16124 if (NILP (face))
16125 face = mode_line_string_face;
16126 else
16127 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16128 props = Fcons (Qface, Fcons (face, Qnil));
16129 if (copy_string)
16130 lisp_string = Fcopy_sequence (lisp_string);
16132 if (!NILP (props))
16133 Fadd_text_properties (make_number (0), make_number (len),
16134 props, lisp_string);
16137 if (len > 0)
16139 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16140 n += len;
16143 if (field_width > len)
16145 field_width -= len;
16146 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16147 if (!NILP (props))
16148 Fadd_text_properties (make_number (0), make_number (field_width),
16149 props, lisp_string);
16150 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16151 n += field_width;
16154 return n;
16158 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16159 1, 4, 0,
16160 doc: /* Format a string out of a mode line format specification.
16161 First arg FORMAT specifies the mode line format (see `mode-line-format'
16162 for details) to use.
16164 Optional second arg FACE specifies the face property to put
16165 on all characters for which no face is specified.
16166 t means whatever face the window's mode line currently uses
16167 \(either `mode-line' or `mode-line-inactive', depending).
16168 nil means the default is no face property.
16169 If FACE is an integer, the value string has no text properties.
16171 Optional third and fourth args WINDOW and BUFFER specify the window
16172 and buffer to use as the context for the formatting (defaults
16173 are the selected window and the window's buffer). */)
16174 (format, face, window, buffer)
16175 Lisp_Object format, face, window, buffer;
16177 struct it it;
16178 int len;
16179 struct window *w;
16180 struct buffer *old_buffer = NULL;
16181 int face_id = -1;
16182 int no_props = INTEGERP (face);
16184 if (NILP (window))
16185 window = selected_window;
16186 CHECK_WINDOW (window);
16187 w = XWINDOW (window);
16189 if (NILP (buffer))
16190 buffer = w->buffer;
16191 CHECK_BUFFER (buffer);
16193 if (NILP (format))
16194 return build_string ("");
16196 if (no_props)
16197 face = Qnil;
16199 if (!NILP (face))
16201 if (EQ (face, Qt))
16202 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16203 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16206 if (face_id < 0)
16207 face_id = DEFAULT_FACE_ID;
16209 if (XBUFFER (buffer) != current_buffer)
16211 old_buffer = current_buffer;
16212 set_buffer_internal_1 (XBUFFER (buffer));
16215 init_iterator (&it, w, -1, -1, NULL, face_id);
16217 if (!no_props)
16219 mode_line_string_face = face;
16220 mode_line_string_face_prop
16221 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16223 /* We need a dummy last element in mode_line_string_list to
16224 indicate we are building the propertized mode-line string.
16225 Using mode_line_string_face_prop here GC protects it. */
16226 mode_line_string_list
16227 = Fcons (mode_line_string_face_prop, Qnil);
16228 frame_title_ptr = NULL;
16230 else
16232 mode_line_string_face_prop = Qnil;
16233 mode_line_string_list = Qnil;
16234 frame_title_ptr = frame_title_buf;
16237 push_frame_kboard (it.f);
16238 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16239 pop_frame_kboard ();
16241 if (old_buffer)
16242 set_buffer_internal_1 (old_buffer);
16244 if (!no_props)
16246 Lisp_Object str;
16247 mode_line_string_list = Fnreverse (mode_line_string_list);
16248 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16249 make_string ("", 0));
16250 mode_line_string_face_prop = Qnil;
16251 mode_line_string_list = Qnil;
16252 return str;
16255 len = frame_title_ptr - frame_title_buf;
16256 if (len > 0 && frame_title_ptr[-1] == '-')
16258 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16259 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16261 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16262 if (len > frame_title_ptr - frame_title_buf)
16263 len = frame_title_ptr - frame_title_buf;
16266 frame_title_ptr = NULL;
16267 return make_string (frame_title_buf, len);
16270 /* Write a null-terminated, right justified decimal representation of
16271 the positive integer D to BUF using a minimal field width WIDTH. */
16273 static void
16274 pint2str (buf, width, d)
16275 register char *buf;
16276 register int width;
16277 register int d;
16279 register char *p = buf;
16281 if (d <= 0)
16282 *p++ = '0';
16283 else
16285 while (d > 0)
16287 *p++ = d % 10 + '0';
16288 d /= 10;
16292 for (width -= (int) (p - buf); width > 0; --width)
16293 *p++ = ' ';
16294 *p-- = '\0';
16295 while (p > buf)
16297 d = *buf;
16298 *buf++ = *p;
16299 *p-- = d;
16303 /* Write a null-terminated, right justified decimal and "human
16304 readable" representation of the nonnegative integer D to BUF using
16305 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16307 static const char power_letter[] =
16309 0, /* not used */
16310 'k', /* kilo */
16311 'M', /* mega */
16312 'G', /* giga */
16313 'T', /* tera */
16314 'P', /* peta */
16315 'E', /* exa */
16316 'Z', /* zetta */
16317 'Y' /* yotta */
16320 static void
16321 pint2hrstr (buf, width, d)
16322 char *buf;
16323 int width;
16324 int d;
16326 /* We aim to represent the nonnegative integer D as
16327 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16328 int quotient = d;
16329 int remainder = 0;
16330 /* -1 means: do not use TENTHS. */
16331 int tenths = -1;
16332 int exponent = 0;
16334 /* Length of QUOTIENT.TENTHS as a string. */
16335 int length;
16337 char * psuffix;
16338 char * p;
16340 if (1000 <= quotient)
16342 /* Scale to the appropriate EXPONENT. */
16345 remainder = quotient % 1000;
16346 quotient /= 1000;
16347 exponent++;
16349 while (1000 <= quotient);
16351 /* Round to nearest and decide whether to use TENTHS or not. */
16352 if (quotient <= 9)
16354 tenths = remainder / 100;
16355 if (50 <= remainder % 100)
16357 if (tenths < 9)
16358 tenths++;
16359 else
16361 quotient++;
16362 if (quotient == 10)
16363 tenths = -1;
16364 else
16365 tenths = 0;
16369 else
16370 if (500 <= remainder)
16372 if (quotient < 999)
16373 quotient++;
16374 else
16376 quotient = 1;
16377 exponent++;
16378 tenths = 0;
16383 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16384 if (tenths == -1 && quotient <= 99)
16385 if (quotient <= 9)
16386 length = 1;
16387 else
16388 length = 2;
16389 else
16390 length = 3;
16391 p = psuffix = buf + max (width, length);
16393 /* Print EXPONENT. */
16394 if (exponent)
16395 *psuffix++ = power_letter[exponent];
16396 *psuffix = '\0';
16398 /* Print TENTHS. */
16399 if (tenths >= 0)
16401 *--p = '0' + tenths;
16402 *--p = '.';
16405 /* Print QUOTIENT. */
16408 int digit = quotient % 10;
16409 *--p = '0' + digit;
16411 while ((quotient /= 10) != 0);
16413 /* Print leading spaces. */
16414 while (buf < p)
16415 *--p = ' ';
16418 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16419 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16420 type of CODING_SYSTEM. Return updated pointer into BUF. */
16422 static unsigned char invalid_eol_type[] = "(*invalid*)";
16424 static char *
16425 decode_mode_spec_coding (coding_system, buf, eol_flag)
16426 Lisp_Object coding_system;
16427 register char *buf;
16428 int eol_flag;
16430 Lisp_Object val;
16431 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16432 const unsigned char *eol_str;
16433 int eol_str_len;
16434 /* The EOL conversion we are using. */
16435 Lisp_Object eoltype;
16437 val = Fget (coding_system, Qcoding_system);
16438 eoltype = Qnil;
16440 if (!VECTORP (val)) /* Not yet decided. */
16442 if (multibyte)
16443 *buf++ = '-';
16444 if (eol_flag)
16445 eoltype = eol_mnemonic_undecided;
16446 /* Don't mention EOL conversion if it isn't decided. */
16448 else
16450 Lisp_Object eolvalue;
16452 eolvalue = Fget (coding_system, Qeol_type);
16454 if (multibyte)
16455 *buf++ = XFASTINT (AREF (val, 1));
16457 if (eol_flag)
16459 /* The EOL conversion that is normal on this system. */
16461 if (NILP (eolvalue)) /* Not yet decided. */
16462 eoltype = eol_mnemonic_undecided;
16463 else if (VECTORP (eolvalue)) /* Not yet decided. */
16464 eoltype = eol_mnemonic_undecided;
16465 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16466 eoltype = (XFASTINT (eolvalue) == 0
16467 ? eol_mnemonic_unix
16468 : (XFASTINT (eolvalue) == 1
16469 ? eol_mnemonic_dos : eol_mnemonic_mac));
16473 if (eol_flag)
16475 /* Mention the EOL conversion if it is not the usual one. */
16476 if (STRINGP (eoltype))
16478 eol_str = SDATA (eoltype);
16479 eol_str_len = SBYTES (eoltype);
16481 else if (INTEGERP (eoltype)
16482 && CHAR_VALID_P (XINT (eoltype), 0))
16484 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16485 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16486 eol_str = tmp;
16488 else
16490 eol_str = invalid_eol_type;
16491 eol_str_len = sizeof (invalid_eol_type) - 1;
16493 bcopy (eol_str, buf, eol_str_len);
16494 buf += eol_str_len;
16497 return buf;
16500 /* Return a string for the output of a mode line %-spec for window W,
16501 generated by character C. PRECISION >= 0 means don't return a
16502 string longer than that value. FIELD_WIDTH > 0 means pad the
16503 string returned with spaces to that value. Return 1 in *MULTIBYTE
16504 if the result is multibyte text.
16506 Note we operate on the current buffer for most purposes,
16507 the exception being w->base_line_pos. */
16509 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16511 static char *
16512 decode_mode_spec (w, c, field_width, precision, multibyte)
16513 struct window *w;
16514 register int c;
16515 int field_width, precision;
16516 int *multibyte;
16518 Lisp_Object obj;
16519 struct frame *f = XFRAME (WINDOW_FRAME (w));
16520 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16521 struct buffer *b = current_buffer;
16523 obj = Qnil;
16524 *multibyte = 0;
16526 switch (c)
16528 case '*':
16529 if (!NILP (b->read_only))
16530 return "%";
16531 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16532 return "*";
16533 return "-";
16535 case '+':
16536 /* This differs from %* only for a modified read-only buffer. */
16537 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16538 return "*";
16539 if (!NILP (b->read_only))
16540 return "%";
16541 return "-";
16543 case '&':
16544 /* This differs from %* in ignoring read-only-ness. */
16545 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16546 return "*";
16547 return "-";
16549 case '%':
16550 return "%";
16552 case '[':
16554 int i;
16555 char *p;
16557 if (command_loop_level > 5)
16558 return "[[[... ";
16559 p = decode_mode_spec_buf;
16560 for (i = 0; i < command_loop_level; i++)
16561 *p++ = '[';
16562 *p = 0;
16563 return decode_mode_spec_buf;
16566 case ']':
16568 int i;
16569 char *p;
16571 if (command_loop_level > 5)
16572 return " ...]]]";
16573 p = decode_mode_spec_buf;
16574 for (i = 0; i < command_loop_level; i++)
16575 *p++ = ']';
16576 *p = 0;
16577 return decode_mode_spec_buf;
16580 case '-':
16582 register int i;
16584 /* Let lots_of_dashes be a string of infinite length. */
16585 if (!NILP (mode_line_string_list))
16586 return "--";
16587 if (field_width <= 0
16588 || field_width > sizeof (lots_of_dashes))
16590 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16591 decode_mode_spec_buf[i] = '-';
16592 decode_mode_spec_buf[i] = '\0';
16593 return decode_mode_spec_buf;
16595 else
16596 return lots_of_dashes;
16599 case 'b':
16600 obj = b->name;
16601 break;
16603 case 'c':
16605 int col = (int) current_column (); /* iftc */
16606 w->column_number_displayed = make_number (col);
16607 pint2str (decode_mode_spec_buf, field_width, col);
16608 return decode_mode_spec_buf;
16611 case 'F':
16612 /* %F displays the frame name. */
16613 if (!NILP (f->title))
16614 return (char *) SDATA (f->title);
16615 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16616 return (char *) SDATA (f->name);
16617 return "Emacs";
16619 case 'f':
16620 obj = b->filename;
16621 break;
16623 case 'i':
16625 int size = ZV - BEGV;
16626 pint2str (decode_mode_spec_buf, field_width, size);
16627 return decode_mode_spec_buf;
16630 case 'I':
16632 int size = ZV - BEGV;
16633 pint2hrstr (decode_mode_spec_buf, field_width, size);
16634 return decode_mode_spec_buf;
16637 case 'l':
16639 int startpos = XMARKER (w->start)->charpos;
16640 int startpos_byte = marker_byte_position (w->start);
16641 int line, linepos, linepos_byte, topline;
16642 int nlines, junk;
16643 int height = WINDOW_TOTAL_LINES (w);
16645 /* If we decided that this buffer isn't suitable for line numbers,
16646 don't forget that too fast. */
16647 if (EQ (w->base_line_pos, w->buffer))
16648 goto no_value;
16649 /* But do forget it, if the window shows a different buffer now. */
16650 else if (BUFFERP (w->base_line_pos))
16651 w->base_line_pos = Qnil;
16653 /* If the buffer is very big, don't waste time. */
16654 if (INTEGERP (Vline_number_display_limit)
16655 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16657 w->base_line_pos = Qnil;
16658 w->base_line_number = Qnil;
16659 goto no_value;
16662 if (!NILP (w->base_line_number)
16663 && !NILP (w->base_line_pos)
16664 && XFASTINT (w->base_line_pos) <= startpos)
16666 line = XFASTINT (w->base_line_number);
16667 linepos = XFASTINT (w->base_line_pos);
16668 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16670 else
16672 line = 1;
16673 linepos = BUF_BEGV (b);
16674 linepos_byte = BUF_BEGV_BYTE (b);
16677 /* Count lines from base line to window start position. */
16678 nlines = display_count_lines (linepos, linepos_byte,
16679 startpos_byte,
16680 startpos, &junk);
16682 topline = nlines + line;
16684 /* Determine a new base line, if the old one is too close
16685 or too far away, or if we did not have one.
16686 "Too close" means it's plausible a scroll-down would
16687 go back past it. */
16688 if (startpos == BUF_BEGV (b))
16690 w->base_line_number = make_number (topline);
16691 w->base_line_pos = make_number (BUF_BEGV (b));
16693 else if (nlines < height + 25 || nlines > height * 3 + 50
16694 || linepos == BUF_BEGV (b))
16696 int limit = BUF_BEGV (b);
16697 int limit_byte = BUF_BEGV_BYTE (b);
16698 int position;
16699 int distance = (height * 2 + 30) * line_number_display_limit_width;
16701 if (startpos - distance > limit)
16703 limit = startpos - distance;
16704 limit_byte = CHAR_TO_BYTE (limit);
16707 nlines = display_count_lines (startpos, startpos_byte,
16708 limit_byte,
16709 - (height * 2 + 30),
16710 &position);
16711 /* If we couldn't find the lines we wanted within
16712 line_number_display_limit_width chars per line,
16713 give up on line numbers for this window. */
16714 if (position == limit_byte && limit == startpos - distance)
16716 w->base_line_pos = w->buffer;
16717 w->base_line_number = Qnil;
16718 goto no_value;
16721 w->base_line_number = make_number (topline - nlines);
16722 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16725 /* Now count lines from the start pos to point. */
16726 nlines = display_count_lines (startpos, startpos_byte,
16727 PT_BYTE, PT, &junk);
16729 /* Record that we did display the line number. */
16730 line_number_displayed = 1;
16732 /* Make the string to show. */
16733 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16734 return decode_mode_spec_buf;
16735 no_value:
16737 char* p = decode_mode_spec_buf;
16738 int pad = field_width - 2;
16739 while (pad-- > 0)
16740 *p++ = ' ';
16741 *p++ = '?';
16742 *p++ = '?';
16743 *p = '\0';
16744 return decode_mode_spec_buf;
16747 break;
16749 case 'm':
16750 obj = b->mode_name;
16751 break;
16753 case 'n':
16754 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16755 return " Narrow";
16756 break;
16758 case 'p':
16760 int pos = marker_position (w->start);
16761 int total = BUF_ZV (b) - BUF_BEGV (b);
16763 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16765 if (pos <= BUF_BEGV (b))
16766 return "All";
16767 else
16768 return "Bottom";
16770 else if (pos <= BUF_BEGV (b))
16771 return "Top";
16772 else
16774 if (total > 1000000)
16775 /* Do it differently for a large value, to avoid overflow. */
16776 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16777 else
16778 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16779 /* We can't normally display a 3-digit number,
16780 so get us a 2-digit number that is close. */
16781 if (total == 100)
16782 total = 99;
16783 sprintf (decode_mode_spec_buf, "%2d%%", total);
16784 return decode_mode_spec_buf;
16788 /* Display percentage of size above the bottom of the screen. */
16789 case 'P':
16791 int toppos = marker_position (w->start);
16792 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16793 int total = BUF_ZV (b) - BUF_BEGV (b);
16795 if (botpos >= BUF_ZV (b))
16797 if (toppos <= BUF_BEGV (b))
16798 return "All";
16799 else
16800 return "Bottom";
16802 else
16804 if (total > 1000000)
16805 /* Do it differently for a large value, to avoid overflow. */
16806 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16807 else
16808 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16809 /* We can't normally display a 3-digit number,
16810 so get us a 2-digit number that is close. */
16811 if (total == 100)
16812 total = 99;
16813 if (toppos <= BUF_BEGV (b))
16814 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16815 else
16816 sprintf (decode_mode_spec_buf, "%2d%%", total);
16817 return decode_mode_spec_buf;
16821 case 's':
16822 /* status of process */
16823 obj = Fget_buffer_process (Fcurrent_buffer ());
16824 if (NILP (obj))
16825 return "no process";
16826 #ifdef subprocesses
16827 obj = Fsymbol_name (Fprocess_status (obj));
16828 #endif
16829 break;
16831 case 't': /* indicate TEXT or BINARY */
16832 #ifdef MODE_LINE_BINARY_TEXT
16833 return MODE_LINE_BINARY_TEXT (b);
16834 #else
16835 return "T";
16836 #endif
16838 case 'z':
16839 /* coding-system (not including end-of-line format) */
16840 case 'Z':
16841 /* coding-system (including end-of-line type) */
16843 int eol_flag = (c == 'Z');
16844 char *p = decode_mode_spec_buf;
16846 if (! FRAME_WINDOW_P (f))
16848 /* No need to mention EOL here--the terminal never needs
16849 to do EOL conversion. */
16850 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16851 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16853 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16854 p, eol_flag);
16856 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16857 #ifdef subprocesses
16858 obj = Fget_buffer_process (Fcurrent_buffer ());
16859 if (PROCESSP (obj))
16861 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16862 p, eol_flag);
16863 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16864 p, eol_flag);
16866 #endif /* subprocesses */
16867 #endif /* 0 */
16868 *p = 0;
16869 return decode_mode_spec_buf;
16873 if (STRINGP (obj))
16875 *multibyte = STRING_MULTIBYTE (obj);
16876 return (char *) SDATA (obj);
16878 else
16879 return "";
16883 /* Count up to COUNT lines starting from START / START_BYTE.
16884 But don't go beyond LIMIT_BYTE.
16885 Return the number of lines thus found (always nonnegative).
16887 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16889 static int
16890 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16891 int start, start_byte, limit_byte, count;
16892 int *byte_pos_ptr;
16894 register unsigned char *cursor;
16895 unsigned char *base;
16897 register int ceiling;
16898 register unsigned char *ceiling_addr;
16899 int orig_count = count;
16901 /* If we are not in selective display mode,
16902 check only for newlines. */
16903 int selective_display = (!NILP (current_buffer->selective_display)
16904 && !INTEGERP (current_buffer->selective_display));
16906 if (count > 0)
16908 while (start_byte < limit_byte)
16910 ceiling = BUFFER_CEILING_OF (start_byte);
16911 ceiling = min (limit_byte - 1, ceiling);
16912 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16913 base = (cursor = BYTE_POS_ADDR (start_byte));
16914 while (1)
16916 if (selective_display)
16917 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16919 else
16920 while (*cursor != '\n' && ++cursor != ceiling_addr)
16923 if (cursor != ceiling_addr)
16925 if (--count == 0)
16927 start_byte += cursor - base + 1;
16928 *byte_pos_ptr = start_byte;
16929 return orig_count;
16931 else
16932 if (++cursor == ceiling_addr)
16933 break;
16935 else
16936 break;
16938 start_byte += cursor - base;
16941 else
16943 while (start_byte > limit_byte)
16945 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16946 ceiling = max (limit_byte, ceiling);
16947 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16948 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16949 while (1)
16951 if (selective_display)
16952 while (--cursor != ceiling_addr
16953 && *cursor != '\n' && *cursor != 015)
16955 else
16956 while (--cursor != ceiling_addr && *cursor != '\n')
16959 if (cursor != ceiling_addr)
16961 if (++count == 0)
16963 start_byte += cursor - base + 1;
16964 *byte_pos_ptr = start_byte;
16965 /* When scanning backwards, we should
16966 not count the newline posterior to which we stop. */
16967 return - orig_count - 1;
16970 else
16971 break;
16973 /* Here we add 1 to compensate for the last decrement
16974 of CURSOR, which took it past the valid range. */
16975 start_byte += cursor - base + 1;
16979 *byte_pos_ptr = limit_byte;
16981 if (count < 0)
16982 return - orig_count + count;
16983 return orig_count - count;
16989 /***********************************************************************
16990 Displaying strings
16991 ***********************************************************************/
16993 /* Display a NUL-terminated string, starting with index START.
16995 If STRING is non-null, display that C string. Otherwise, the Lisp
16996 string LISP_STRING is displayed.
16998 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16999 FACE_STRING. Display STRING or LISP_STRING with the face at
17000 FACE_STRING_POS in FACE_STRING:
17002 Display the string in the environment given by IT, but use the
17003 standard display table, temporarily.
17005 FIELD_WIDTH is the minimum number of output glyphs to produce.
17006 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17007 with spaces. If STRING has more characters, more than FIELD_WIDTH
17008 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17010 PRECISION is the maximum number of characters to output from
17011 STRING. PRECISION < 0 means don't truncate the string.
17013 This is roughly equivalent to printf format specifiers:
17015 FIELD_WIDTH PRECISION PRINTF
17016 ----------------------------------------
17017 -1 -1 %s
17018 -1 10 %.10s
17019 10 -1 %10s
17020 20 10 %20.10s
17022 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17023 display them, and < 0 means obey the current buffer's value of
17024 enable_multibyte_characters.
17026 Value is the number of glyphs produced. */
17028 static int
17029 display_string (string, lisp_string, face_string, face_string_pos,
17030 start, it, field_width, precision, max_x, multibyte)
17031 unsigned char *string;
17032 Lisp_Object lisp_string;
17033 Lisp_Object face_string;
17034 int face_string_pos;
17035 int start;
17036 struct it *it;
17037 int field_width, precision, max_x;
17038 int multibyte;
17040 int hpos_at_start = it->hpos;
17041 int saved_face_id = it->face_id;
17042 struct glyph_row *row = it->glyph_row;
17044 /* Initialize the iterator IT for iteration over STRING beginning
17045 with index START. */
17046 reseat_to_string (it, string, lisp_string, start,
17047 precision, field_width, multibyte);
17049 /* If displaying STRING, set up the face of the iterator
17050 from LISP_STRING, if that's given. */
17051 if (STRINGP (face_string))
17053 int endptr;
17054 struct face *face;
17056 it->face_id
17057 = face_at_string_position (it->w, face_string, face_string_pos,
17058 0, it->region_beg_charpos,
17059 it->region_end_charpos,
17060 &endptr, it->base_face_id, 0);
17061 face = FACE_FROM_ID (it->f, it->face_id);
17062 it->face_box_p = face->box != FACE_NO_BOX;
17065 /* Set max_x to the maximum allowed X position. Don't let it go
17066 beyond the right edge of the window. */
17067 if (max_x <= 0)
17068 max_x = it->last_visible_x;
17069 else
17070 max_x = min (max_x, it->last_visible_x);
17072 /* Skip over display elements that are not visible. because IT->w is
17073 hscrolled. */
17074 if (it->current_x < it->first_visible_x)
17075 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17076 MOVE_TO_POS | MOVE_TO_X);
17078 row->ascent = it->max_ascent;
17079 row->height = it->max_ascent + it->max_descent;
17080 row->phys_ascent = it->max_phys_ascent;
17081 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17082 row->extra_line_spacing = it->max_extra_line_spacing;
17084 /* This condition is for the case that we are called with current_x
17085 past last_visible_x. */
17086 while (it->current_x < max_x)
17088 int x_before, x, n_glyphs_before, i, nglyphs;
17090 /* Get the next display element. */
17091 if (!get_next_display_element (it))
17092 break;
17094 /* Produce glyphs. */
17095 x_before = it->current_x;
17096 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17097 PRODUCE_GLYPHS (it);
17099 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17100 i = 0;
17101 x = x_before;
17102 while (i < nglyphs)
17104 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17106 if (!it->truncate_lines_p
17107 && x + glyph->pixel_width > max_x)
17109 /* End of continued line or max_x reached. */
17110 if (CHAR_GLYPH_PADDING_P (*glyph))
17112 /* A wide character is unbreakable. */
17113 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17114 it->current_x = x_before;
17116 else
17118 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17119 it->current_x = x;
17121 break;
17123 else if (x + glyph->pixel_width > it->first_visible_x)
17125 /* Glyph is at least partially visible. */
17126 ++it->hpos;
17127 if (x < it->first_visible_x)
17128 it->glyph_row->x = x - it->first_visible_x;
17130 else
17132 /* Glyph is off the left margin of the display area.
17133 Should not happen. */
17134 abort ();
17137 row->ascent = max (row->ascent, it->max_ascent);
17138 row->height = max (row->height, it->max_ascent + it->max_descent);
17139 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17140 row->phys_height = max (row->phys_height,
17141 it->max_phys_ascent + it->max_phys_descent);
17142 row->extra_line_spacing = max (row->extra_line_spacing,
17143 it->max_extra_line_spacing);
17144 x += glyph->pixel_width;
17145 ++i;
17148 /* Stop if max_x reached. */
17149 if (i < nglyphs)
17150 break;
17152 /* Stop at line ends. */
17153 if (ITERATOR_AT_END_OF_LINE_P (it))
17155 it->continuation_lines_width = 0;
17156 break;
17159 set_iterator_to_next (it, 1);
17161 /* Stop if truncating at the right edge. */
17162 if (it->truncate_lines_p
17163 && it->current_x >= it->last_visible_x)
17165 /* Add truncation mark, but don't do it if the line is
17166 truncated at a padding space. */
17167 if (IT_CHARPOS (*it) < it->string_nchars)
17169 if (!FRAME_WINDOW_P (it->f))
17171 int i, n;
17173 if (it->current_x > it->last_visible_x)
17175 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17176 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17177 break;
17178 for (n = row->used[TEXT_AREA]; i < n; ++i)
17180 row->used[TEXT_AREA] = i;
17181 produce_special_glyphs (it, IT_TRUNCATION);
17184 produce_special_glyphs (it, IT_TRUNCATION);
17186 it->glyph_row->truncated_on_right_p = 1;
17188 break;
17192 /* Maybe insert a truncation at the left. */
17193 if (it->first_visible_x
17194 && IT_CHARPOS (*it) > 0)
17196 if (!FRAME_WINDOW_P (it->f))
17197 insert_left_trunc_glyphs (it);
17198 it->glyph_row->truncated_on_left_p = 1;
17201 it->face_id = saved_face_id;
17203 /* Value is number of columns displayed. */
17204 return it->hpos - hpos_at_start;
17209 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17210 appears as an element of LIST or as the car of an element of LIST.
17211 If PROPVAL is a list, compare each element against LIST in that
17212 way, and return 1/2 if any element of PROPVAL is found in LIST.
17213 Otherwise return 0. This function cannot quit.
17214 The return value is 2 if the text is invisible but with an ellipsis
17215 and 1 if it's invisible and without an ellipsis. */
17218 invisible_p (propval, list)
17219 register Lisp_Object propval;
17220 Lisp_Object list;
17222 register Lisp_Object tail, proptail;
17224 for (tail = list; CONSP (tail); tail = XCDR (tail))
17226 register Lisp_Object tem;
17227 tem = XCAR (tail);
17228 if (EQ (propval, tem))
17229 return 1;
17230 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17231 return NILP (XCDR (tem)) ? 1 : 2;
17234 if (CONSP (propval))
17236 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17238 Lisp_Object propelt;
17239 propelt = XCAR (proptail);
17240 for (tail = list; CONSP (tail); tail = XCDR (tail))
17242 register Lisp_Object tem;
17243 tem = XCAR (tail);
17244 if (EQ (propelt, tem))
17245 return 1;
17246 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17247 return NILP (XCDR (tem)) ? 1 : 2;
17252 return 0;
17255 /* Calculate a width or height in pixels from a specification using
17256 the following elements:
17258 SPEC ::=
17259 NUM - a (fractional) multiple of the default font width/height
17260 (NUM) - specifies exactly NUM pixels
17261 UNIT - a fixed number of pixels, see below.
17262 ELEMENT - size of a display element in pixels, see below.
17263 (NUM . SPEC) - equals NUM * SPEC
17264 (+ SPEC SPEC ...) - add pixel values
17265 (- SPEC SPEC ...) - subtract pixel values
17266 (- SPEC) - negate pixel value
17268 NUM ::=
17269 INT or FLOAT - a number constant
17270 SYMBOL - use symbol's (buffer local) variable binding.
17272 UNIT ::=
17273 in - pixels per inch *)
17274 mm - pixels per 1/1000 meter *)
17275 cm - pixels per 1/100 meter *)
17276 width - width of current font in pixels.
17277 height - height of current font in pixels.
17279 *) using the ratio(s) defined in display-pixels-per-inch.
17281 ELEMENT ::=
17283 left-fringe - left fringe width in pixels
17284 right-fringe - right fringe width in pixels
17286 left-margin - left margin width in pixels
17287 right-margin - right margin width in pixels
17289 scroll-bar - scroll-bar area width in pixels
17291 Examples:
17293 Pixels corresponding to 5 inches:
17294 (5 . in)
17296 Total width of non-text areas on left side of window (if scroll-bar is on left):
17297 '(space :width (+ left-fringe left-margin scroll-bar))
17299 Align to first text column (in header line):
17300 '(space :align-to 0)
17302 Align to middle of text area minus half the width of variable `my-image'
17303 containing a loaded image:
17304 '(space :align-to (0.5 . (- text my-image)))
17306 Width of left margin minus width of 1 character in the default font:
17307 '(space :width (- left-margin 1))
17309 Width of left margin minus width of 2 characters in the current font:
17310 '(space :width (- left-margin (2 . width)))
17312 Center 1 character over left-margin (in header line):
17313 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17315 Different ways to express width of left fringe plus left margin minus one pixel:
17316 '(space :width (- (+ left-fringe left-margin) (1)))
17317 '(space :width (+ left-fringe left-margin (- (1))))
17318 '(space :width (+ left-fringe left-margin (-1)))
17322 #define NUMVAL(X) \
17323 ((INTEGERP (X) || FLOATP (X)) \
17324 ? XFLOATINT (X) \
17325 : - 1)
17328 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17329 double *res;
17330 struct it *it;
17331 Lisp_Object prop;
17332 void *font;
17333 int width_p, *align_to;
17335 double pixels;
17337 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17338 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17340 if (NILP (prop))
17341 return OK_PIXELS (0);
17343 if (SYMBOLP (prop))
17345 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17347 char *unit = SDATA (SYMBOL_NAME (prop));
17349 if (unit[0] == 'i' && unit[1] == 'n')
17350 pixels = 1.0;
17351 else if (unit[0] == 'm' && unit[1] == 'm')
17352 pixels = 25.4;
17353 else if (unit[0] == 'c' && unit[1] == 'm')
17354 pixels = 2.54;
17355 else
17356 pixels = 0;
17357 if (pixels > 0)
17359 double ppi;
17360 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17361 || (CONSP (Vdisplay_pixels_per_inch)
17362 && (ppi = (width_p
17363 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17364 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17365 ppi > 0)))
17366 return OK_PIXELS (ppi / pixels);
17368 return 0;
17372 #ifdef HAVE_WINDOW_SYSTEM
17373 if (EQ (prop, Qheight))
17374 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17375 if (EQ (prop, Qwidth))
17376 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17377 #else
17378 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17379 return OK_PIXELS (1);
17380 #endif
17382 if (EQ (prop, Qtext))
17383 return OK_PIXELS (width_p
17384 ? window_box_width (it->w, TEXT_AREA)
17385 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17387 if (align_to && *align_to < 0)
17389 *res = 0;
17390 if (EQ (prop, Qleft))
17391 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17392 if (EQ (prop, Qright))
17393 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17394 if (EQ (prop, Qcenter))
17395 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17396 + window_box_width (it->w, TEXT_AREA) / 2);
17397 if (EQ (prop, Qleft_fringe))
17398 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17399 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17400 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17401 if (EQ (prop, Qright_fringe))
17402 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17403 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17404 : window_box_right_offset (it->w, TEXT_AREA));
17405 if (EQ (prop, Qleft_margin))
17406 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17407 if (EQ (prop, Qright_margin))
17408 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17409 if (EQ (prop, Qscroll_bar))
17410 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17412 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17413 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17414 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17415 : 0)));
17417 else
17419 if (EQ (prop, Qleft_fringe))
17420 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17421 if (EQ (prop, Qright_fringe))
17422 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17423 if (EQ (prop, Qleft_margin))
17424 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17425 if (EQ (prop, Qright_margin))
17426 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17427 if (EQ (prop, Qscroll_bar))
17428 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17431 prop = Fbuffer_local_value (prop, it->w->buffer);
17434 if (INTEGERP (prop) || FLOATP (prop))
17436 int base_unit = (width_p
17437 ? FRAME_COLUMN_WIDTH (it->f)
17438 : FRAME_LINE_HEIGHT (it->f));
17439 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17442 if (CONSP (prop))
17444 Lisp_Object car = XCAR (prop);
17445 Lisp_Object cdr = XCDR (prop);
17447 if (SYMBOLP (car))
17449 #ifdef HAVE_WINDOW_SYSTEM
17450 if (valid_image_p (prop))
17452 int id = lookup_image (it->f, prop);
17453 struct image *img = IMAGE_FROM_ID (it->f, id);
17455 return OK_PIXELS (width_p ? img->width : img->height);
17457 #endif
17458 if (EQ (car, Qplus) || EQ (car, Qminus))
17460 int first = 1;
17461 double px;
17463 pixels = 0;
17464 while (CONSP (cdr))
17466 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17467 font, width_p, align_to))
17468 return 0;
17469 if (first)
17470 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17471 else
17472 pixels += px;
17473 cdr = XCDR (cdr);
17475 if (EQ (car, Qminus))
17476 pixels = -pixels;
17477 return OK_PIXELS (pixels);
17480 car = Fbuffer_local_value (car, it->w->buffer);
17483 if (INTEGERP (car) || FLOATP (car))
17485 double fact;
17486 pixels = XFLOATINT (car);
17487 if (NILP (cdr))
17488 return OK_PIXELS (pixels);
17489 if (calc_pixel_width_or_height (&fact, it, cdr,
17490 font, width_p, align_to))
17491 return OK_PIXELS (pixels * fact);
17492 return 0;
17495 return 0;
17498 return 0;
17502 /***********************************************************************
17503 Glyph Display
17504 ***********************************************************************/
17506 #ifdef HAVE_WINDOW_SYSTEM
17508 #if GLYPH_DEBUG
17510 void
17511 dump_glyph_string (s)
17512 struct glyph_string *s;
17514 fprintf (stderr, "glyph string\n");
17515 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17516 s->x, s->y, s->width, s->height);
17517 fprintf (stderr, " ybase = %d\n", s->ybase);
17518 fprintf (stderr, " hl = %d\n", s->hl);
17519 fprintf (stderr, " left overhang = %d, right = %d\n",
17520 s->left_overhang, s->right_overhang);
17521 fprintf (stderr, " nchars = %d\n", s->nchars);
17522 fprintf (stderr, " extends to end of line = %d\n",
17523 s->extends_to_end_of_line_p);
17524 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17525 fprintf (stderr, " bg width = %d\n", s->background_width);
17528 #endif /* GLYPH_DEBUG */
17530 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17531 of XChar2b structures for S; it can't be allocated in
17532 init_glyph_string because it must be allocated via `alloca'. W
17533 is the window on which S is drawn. ROW and AREA are the glyph row
17534 and area within the row from which S is constructed. START is the
17535 index of the first glyph structure covered by S. HL is a
17536 face-override for drawing S. */
17538 #ifdef HAVE_NTGUI
17539 #define OPTIONAL_HDC(hdc) hdc,
17540 #define DECLARE_HDC(hdc) HDC hdc;
17541 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17542 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17543 #endif
17545 #ifndef OPTIONAL_HDC
17546 #define OPTIONAL_HDC(hdc)
17547 #define DECLARE_HDC(hdc)
17548 #define ALLOCATE_HDC(hdc, f)
17549 #define RELEASE_HDC(hdc, f)
17550 #endif
17552 static void
17553 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17554 struct glyph_string *s;
17555 DECLARE_HDC (hdc)
17556 XChar2b *char2b;
17557 struct window *w;
17558 struct glyph_row *row;
17559 enum glyph_row_area area;
17560 int start;
17561 enum draw_glyphs_face hl;
17563 bzero (s, sizeof *s);
17564 s->w = w;
17565 s->f = XFRAME (w->frame);
17566 #ifdef HAVE_NTGUI
17567 s->hdc = hdc;
17568 #endif
17569 s->display = FRAME_X_DISPLAY (s->f);
17570 s->window = FRAME_X_WINDOW (s->f);
17571 s->char2b = char2b;
17572 s->hl = hl;
17573 s->row = row;
17574 s->area = area;
17575 s->first_glyph = row->glyphs[area] + start;
17576 s->height = row->height;
17577 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17579 /* Display the internal border below the tool-bar window. */
17580 if (WINDOWP (s->f->tool_bar_window)
17581 && s->w == XWINDOW (s->f->tool_bar_window))
17582 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17584 s->ybase = s->y + row->ascent;
17588 /* Append the list of glyph strings with head H and tail T to the list
17589 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17591 static INLINE void
17592 append_glyph_string_lists (head, tail, h, t)
17593 struct glyph_string **head, **tail;
17594 struct glyph_string *h, *t;
17596 if (h)
17598 if (*head)
17599 (*tail)->next = h;
17600 else
17601 *head = h;
17602 h->prev = *tail;
17603 *tail = t;
17608 /* Prepend the list of glyph strings with head H and tail T to the
17609 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17610 result. */
17612 static INLINE void
17613 prepend_glyph_string_lists (head, tail, h, t)
17614 struct glyph_string **head, **tail;
17615 struct glyph_string *h, *t;
17617 if (h)
17619 if (*head)
17620 (*head)->prev = t;
17621 else
17622 *tail = t;
17623 t->next = *head;
17624 *head = h;
17629 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17630 Set *HEAD and *TAIL to the resulting list. */
17632 static INLINE void
17633 append_glyph_string (head, tail, s)
17634 struct glyph_string **head, **tail;
17635 struct glyph_string *s;
17637 s->next = s->prev = NULL;
17638 append_glyph_string_lists (head, tail, s, s);
17642 /* Get face and two-byte form of character glyph GLYPH on frame F.
17643 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17644 a pointer to a realized face that is ready for display. */
17646 static INLINE struct face *
17647 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17648 struct frame *f;
17649 struct glyph *glyph;
17650 XChar2b *char2b;
17651 int *two_byte_p;
17653 struct face *face;
17655 xassert (glyph->type == CHAR_GLYPH);
17656 face = FACE_FROM_ID (f, glyph->face_id);
17658 if (two_byte_p)
17659 *two_byte_p = 0;
17661 if (!glyph->multibyte_p)
17663 /* Unibyte case. We don't have to encode, but we have to make
17664 sure to use a face suitable for unibyte. */
17665 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17667 else if (glyph->u.ch < 128
17668 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17670 /* Case of ASCII in a face known to fit ASCII. */
17671 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17673 else
17675 int c1, c2, charset;
17677 /* Split characters into bytes. If c2 is -1 afterwards, C is
17678 really a one-byte character so that byte1 is zero. */
17679 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17680 if (c2 > 0)
17681 STORE_XCHAR2B (char2b, c1, c2);
17682 else
17683 STORE_XCHAR2B (char2b, 0, c1);
17685 /* Maybe encode the character in *CHAR2B. */
17686 if (charset != CHARSET_ASCII)
17688 struct font_info *font_info
17689 = FONT_INFO_FROM_ID (f, face->font_info_id);
17690 if (font_info)
17691 glyph->font_type
17692 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17696 /* Make sure X resources of the face are allocated. */
17697 xassert (face != NULL);
17698 PREPARE_FACE_FOR_DISPLAY (f, face);
17699 return face;
17703 /* Fill glyph string S with composition components specified by S->cmp.
17705 FACES is an array of faces for all components of this composition.
17706 S->gidx is the index of the first component for S.
17707 OVERLAPS_P non-zero means S should draw the foreground only, and
17708 use its physical height for clipping.
17710 Value is the index of a component not in S. */
17712 static int
17713 fill_composite_glyph_string (s, faces, overlaps_p)
17714 struct glyph_string *s;
17715 struct face **faces;
17716 int overlaps_p;
17718 int i;
17720 xassert (s);
17722 s->for_overlaps_p = overlaps_p;
17724 s->face = faces[s->gidx];
17725 s->font = s->face->font;
17726 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17728 /* For all glyphs of this composition, starting at the offset
17729 S->gidx, until we reach the end of the definition or encounter a
17730 glyph that requires the different face, add it to S. */
17731 ++s->nchars;
17732 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17733 ++s->nchars;
17735 /* All glyph strings for the same composition has the same width,
17736 i.e. the width set for the first component of the composition. */
17738 s->width = s->first_glyph->pixel_width;
17740 /* If the specified font could not be loaded, use the frame's
17741 default font, but record the fact that we couldn't load it in
17742 the glyph string so that we can draw rectangles for the
17743 characters of the glyph string. */
17744 if (s->font == NULL)
17746 s->font_not_found_p = 1;
17747 s->font = FRAME_FONT (s->f);
17750 /* Adjust base line for subscript/superscript text. */
17751 s->ybase += s->first_glyph->voffset;
17753 xassert (s->face && s->face->gc);
17755 /* This glyph string must always be drawn with 16-bit functions. */
17756 s->two_byte_p = 1;
17758 return s->gidx + s->nchars;
17762 /* Fill glyph string S from a sequence of character glyphs.
17764 FACE_ID is the face id of the string. START is the index of the
17765 first glyph to consider, END is the index of the last + 1.
17766 OVERLAPS_P non-zero means S should draw the foreground only, and
17767 use its physical height for clipping.
17769 Value is the index of the first glyph not in S. */
17771 static int
17772 fill_glyph_string (s, face_id, start, end, overlaps_p)
17773 struct glyph_string *s;
17774 int face_id;
17775 int start, end, overlaps_p;
17777 struct glyph *glyph, *last;
17778 int voffset;
17779 int glyph_not_available_p;
17781 xassert (s->f == XFRAME (s->w->frame));
17782 xassert (s->nchars == 0);
17783 xassert (start >= 0 && end > start);
17785 s->for_overlaps_p = overlaps_p,
17786 glyph = s->row->glyphs[s->area] + start;
17787 last = s->row->glyphs[s->area] + end;
17788 voffset = glyph->voffset;
17790 glyph_not_available_p = glyph->glyph_not_available_p;
17792 while (glyph < last
17793 && glyph->type == CHAR_GLYPH
17794 && glyph->voffset == voffset
17795 /* Same face id implies same font, nowadays. */
17796 && glyph->face_id == face_id
17797 && glyph->glyph_not_available_p == glyph_not_available_p)
17799 int two_byte_p;
17801 s->face = get_glyph_face_and_encoding (s->f, glyph,
17802 s->char2b + s->nchars,
17803 &two_byte_p);
17804 s->two_byte_p = two_byte_p;
17805 ++s->nchars;
17806 xassert (s->nchars <= end - start);
17807 s->width += glyph->pixel_width;
17808 ++glyph;
17811 s->font = s->face->font;
17812 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17814 /* If the specified font could not be loaded, use the frame's font,
17815 but record the fact that we couldn't load it in
17816 S->font_not_found_p so that we can draw rectangles for the
17817 characters of the glyph string. */
17818 if (s->font == NULL || glyph_not_available_p)
17820 s->font_not_found_p = 1;
17821 s->font = FRAME_FONT (s->f);
17824 /* Adjust base line for subscript/superscript text. */
17825 s->ybase += voffset;
17827 xassert (s->face && s->face->gc);
17828 return glyph - s->row->glyphs[s->area];
17832 /* Fill glyph string S from image glyph S->first_glyph. */
17834 static void
17835 fill_image_glyph_string (s)
17836 struct glyph_string *s;
17838 xassert (s->first_glyph->type == IMAGE_GLYPH);
17839 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17840 xassert (s->img);
17841 s->slice = s->first_glyph->slice;
17842 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17843 s->font = s->face->font;
17844 s->width = s->first_glyph->pixel_width;
17846 /* Adjust base line for subscript/superscript text. */
17847 s->ybase += s->first_glyph->voffset;
17851 /* Fill glyph string S from a sequence of stretch glyphs.
17853 ROW is the glyph row in which the glyphs are found, AREA is the
17854 area within the row. START is the index of the first glyph to
17855 consider, END is the index of the last + 1.
17857 Value is the index of the first glyph not in S. */
17859 static int
17860 fill_stretch_glyph_string (s, row, area, start, end)
17861 struct glyph_string *s;
17862 struct glyph_row *row;
17863 enum glyph_row_area area;
17864 int start, end;
17866 struct glyph *glyph, *last;
17867 int voffset, face_id;
17869 xassert (s->first_glyph->type == STRETCH_GLYPH);
17871 glyph = s->row->glyphs[s->area] + start;
17872 last = s->row->glyphs[s->area] + end;
17873 face_id = glyph->face_id;
17874 s->face = FACE_FROM_ID (s->f, face_id);
17875 s->font = s->face->font;
17876 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17877 s->width = glyph->pixel_width;
17878 voffset = glyph->voffset;
17880 for (++glyph;
17881 (glyph < last
17882 && glyph->type == STRETCH_GLYPH
17883 && glyph->voffset == voffset
17884 && glyph->face_id == face_id);
17885 ++glyph)
17886 s->width += glyph->pixel_width;
17888 /* Adjust base line for subscript/superscript text. */
17889 s->ybase += voffset;
17891 /* The case that face->gc == 0 is handled when drawing the glyph
17892 string by calling PREPARE_FACE_FOR_DISPLAY. */
17893 xassert (s->face);
17894 return glyph - s->row->glyphs[s->area];
17898 /* EXPORT for RIF:
17899 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17900 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17901 assumed to be zero. */
17903 void
17904 x_get_glyph_overhangs (glyph, f, left, right)
17905 struct glyph *glyph;
17906 struct frame *f;
17907 int *left, *right;
17909 *left = *right = 0;
17911 if (glyph->type == CHAR_GLYPH)
17913 XFontStruct *font;
17914 struct face *face;
17915 struct font_info *font_info;
17916 XChar2b char2b;
17917 XCharStruct *pcm;
17919 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17920 font = face->font;
17921 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17922 if (font /* ++KFS: Should this be font_info ? */
17923 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17925 if (pcm->rbearing > pcm->width)
17926 *right = pcm->rbearing - pcm->width;
17927 if (pcm->lbearing < 0)
17928 *left = -pcm->lbearing;
17934 /* Return the index of the first glyph preceding glyph string S that
17935 is overwritten by S because of S's left overhang. Value is -1
17936 if no glyphs are overwritten. */
17938 static int
17939 left_overwritten (s)
17940 struct glyph_string *s;
17942 int k;
17944 if (s->left_overhang)
17946 int x = 0, i;
17947 struct glyph *glyphs = s->row->glyphs[s->area];
17948 int first = s->first_glyph - glyphs;
17950 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17951 x -= glyphs[i].pixel_width;
17953 k = i + 1;
17955 else
17956 k = -1;
17958 return k;
17962 /* Return the index of the first glyph preceding glyph string S that
17963 is overwriting S because of its right overhang. Value is -1 if no
17964 glyph in front of S overwrites S. */
17966 static int
17967 left_overwriting (s)
17968 struct glyph_string *s;
17970 int i, k, x;
17971 struct glyph *glyphs = s->row->glyphs[s->area];
17972 int first = s->first_glyph - glyphs;
17974 k = -1;
17975 x = 0;
17976 for (i = first - 1; i >= 0; --i)
17978 int left, right;
17979 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17980 if (x + right > 0)
17981 k = i;
17982 x -= glyphs[i].pixel_width;
17985 return k;
17989 /* Return the index of the last glyph following glyph string S that is
17990 not overwritten by S because of S's right overhang. Value is -1 if
17991 no such glyph is found. */
17993 static int
17994 right_overwritten (s)
17995 struct glyph_string *s;
17997 int k = -1;
17999 if (s->right_overhang)
18001 int x = 0, i;
18002 struct glyph *glyphs = s->row->glyphs[s->area];
18003 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18004 int end = s->row->used[s->area];
18006 for (i = first; i < end && s->right_overhang > x; ++i)
18007 x += glyphs[i].pixel_width;
18009 k = i;
18012 return k;
18016 /* Return the index of the last glyph following glyph string S that
18017 overwrites S because of its left overhang. Value is negative
18018 if no such glyph is found. */
18020 static int
18021 right_overwriting (s)
18022 struct glyph_string *s;
18024 int i, k, x;
18025 int end = s->row->used[s->area];
18026 struct glyph *glyphs = s->row->glyphs[s->area];
18027 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18029 k = -1;
18030 x = 0;
18031 for (i = first; i < end; ++i)
18033 int left, right;
18034 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18035 if (x - left < 0)
18036 k = i;
18037 x += glyphs[i].pixel_width;
18040 return k;
18044 /* Get face and two-byte form of character C in face FACE_ID on frame
18045 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18046 means we want to display multibyte text. DISPLAY_P non-zero means
18047 make sure that X resources for the face returned are allocated.
18048 Value is a pointer to a realized face that is ready for display if
18049 DISPLAY_P is non-zero. */
18051 static INLINE struct face *
18052 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18053 struct frame *f;
18054 int c, face_id;
18055 XChar2b *char2b;
18056 int multibyte_p, display_p;
18058 struct face *face = FACE_FROM_ID (f, face_id);
18060 if (!multibyte_p)
18062 /* Unibyte case. We don't have to encode, but we have to make
18063 sure to use a face suitable for unibyte. */
18064 STORE_XCHAR2B (char2b, 0, c);
18065 face_id = FACE_FOR_CHAR (f, face, c);
18066 face = FACE_FROM_ID (f, face_id);
18068 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18070 /* Case of ASCII in a face known to fit ASCII. */
18071 STORE_XCHAR2B (char2b, 0, c);
18073 else
18075 int c1, c2, charset;
18077 /* Split characters into bytes. If c2 is -1 afterwards, C is
18078 really a one-byte character so that byte1 is zero. */
18079 SPLIT_CHAR (c, charset, c1, c2);
18080 if (c2 > 0)
18081 STORE_XCHAR2B (char2b, c1, c2);
18082 else
18083 STORE_XCHAR2B (char2b, 0, c1);
18085 /* Maybe encode the character in *CHAR2B. */
18086 if (face->font != NULL)
18088 struct font_info *font_info
18089 = FONT_INFO_FROM_ID (f, face->font_info_id);
18090 if (font_info)
18091 rif->encode_char (c, char2b, font_info, 0);
18095 /* Make sure X resources of the face are allocated. */
18096 #ifdef HAVE_X_WINDOWS
18097 if (display_p)
18098 #endif
18100 xassert (face != NULL);
18101 PREPARE_FACE_FOR_DISPLAY (f, face);
18104 return face;
18108 /* Set background width of glyph string S. START is the index of the
18109 first glyph following S. LAST_X is the right-most x-position + 1
18110 in the drawing area. */
18112 static INLINE void
18113 set_glyph_string_background_width (s, start, last_x)
18114 struct glyph_string *s;
18115 int start;
18116 int last_x;
18118 /* If the face of this glyph string has to be drawn to the end of
18119 the drawing area, set S->extends_to_end_of_line_p. */
18120 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18122 if (start == s->row->used[s->area]
18123 && s->area == TEXT_AREA
18124 && ((s->hl == DRAW_NORMAL_TEXT
18125 && (s->row->fill_line_p
18126 || s->face->background != default_face->background
18127 || s->face->stipple != default_face->stipple
18128 || s->row->mouse_face_p))
18129 || s->hl == DRAW_MOUSE_FACE
18130 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18131 && s->row->fill_line_p)))
18132 s->extends_to_end_of_line_p = 1;
18134 /* If S extends its face to the end of the line, set its
18135 background_width to the distance to the right edge of the drawing
18136 area. */
18137 if (s->extends_to_end_of_line_p)
18138 s->background_width = last_x - s->x + 1;
18139 else
18140 s->background_width = s->width;
18144 /* Compute overhangs and x-positions for glyph string S and its
18145 predecessors, or successors. X is the starting x-position for S.
18146 BACKWARD_P non-zero means process predecessors. */
18148 static void
18149 compute_overhangs_and_x (s, x, backward_p)
18150 struct glyph_string *s;
18151 int x;
18152 int backward_p;
18154 if (backward_p)
18156 while (s)
18158 if (rif->compute_glyph_string_overhangs)
18159 rif->compute_glyph_string_overhangs (s);
18160 x -= s->width;
18161 s->x = x;
18162 s = s->prev;
18165 else
18167 while (s)
18169 if (rif->compute_glyph_string_overhangs)
18170 rif->compute_glyph_string_overhangs (s);
18171 s->x = x;
18172 x += s->width;
18173 s = s->next;
18180 /* The following macros are only called from draw_glyphs below.
18181 They reference the following parameters of that function directly:
18182 `w', `row', `area', and `overlap_p'
18183 as well as the following local variables:
18184 `s', `f', and `hdc' (in W32) */
18186 #ifdef HAVE_NTGUI
18187 /* On W32, silently add local `hdc' variable to argument list of
18188 init_glyph_string. */
18189 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18190 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18191 #else
18192 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18193 init_glyph_string (s, char2b, w, row, area, start, hl)
18194 #endif
18196 /* Add a glyph string for a stretch glyph to the list of strings
18197 between HEAD and TAIL. START is the index of the stretch glyph in
18198 row area AREA of glyph row ROW. END is the index of the last glyph
18199 in that glyph row area. X is the current output position assigned
18200 to the new glyph string constructed. HL overrides that face of the
18201 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18202 is the right-most x-position of the drawing area. */
18204 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18205 and below -- keep them on one line. */
18206 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18207 do \
18209 s = (struct glyph_string *) alloca (sizeof *s); \
18210 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18211 START = fill_stretch_glyph_string (s, row, area, START, END); \
18212 append_glyph_string (&HEAD, &TAIL, s); \
18213 s->x = (X); \
18215 while (0)
18218 /* Add a glyph string for an image glyph to the list of strings
18219 between HEAD and TAIL. START is the index of the image glyph in
18220 row area AREA of glyph row ROW. END is the index of the last glyph
18221 in that glyph row area. X is the current output position assigned
18222 to the new glyph string constructed. HL overrides that face of the
18223 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18224 is the right-most x-position of the drawing area. */
18226 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18227 do \
18229 s = (struct glyph_string *) alloca (sizeof *s); \
18230 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18231 fill_image_glyph_string (s); \
18232 append_glyph_string (&HEAD, &TAIL, s); \
18233 ++START; \
18234 s->x = (X); \
18236 while (0)
18239 /* Add a glyph string for a sequence of character glyphs to the list
18240 of strings between HEAD and TAIL. START is the index of the first
18241 glyph in row area AREA of glyph row ROW that is part of the new
18242 glyph string. END is the index of the last glyph in that glyph row
18243 area. X is the current output position assigned to the new glyph
18244 string constructed. HL overrides that face of the glyph; e.g. it
18245 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18246 right-most x-position of the drawing area. */
18248 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18249 do \
18251 int c, face_id; \
18252 XChar2b *char2b; \
18254 c = (row)->glyphs[area][START].u.ch; \
18255 face_id = (row)->glyphs[area][START].face_id; \
18257 s = (struct glyph_string *) alloca (sizeof *s); \
18258 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18259 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18260 append_glyph_string (&HEAD, &TAIL, s); \
18261 s->x = (X); \
18262 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18264 while (0)
18267 /* Add a glyph string for a composite sequence to the list of strings
18268 between HEAD and TAIL. START is the index of the first glyph in
18269 row area AREA of glyph row ROW that is part of the new glyph
18270 string. END is the index of the last glyph in that glyph row area.
18271 X is the current output position assigned to the new glyph string
18272 constructed. HL overrides that face of the glyph; e.g. it is
18273 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18274 x-position of the drawing area. */
18276 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18277 do { \
18278 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18279 int face_id = (row)->glyphs[area][START].face_id; \
18280 struct face *base_face = FACE_FROM_ID (f, face_id); \
18281 struct composition *cmp = composition_table[cmp_id]; \
18282 int glyph_len = cmp->glyph_len; \
18283 XChar2b *char2b; \
18284 struct face **faces; \
18285 struct glyph_string *first_s = NULL; \
18286 int n; \
18288 base_face = base_face->ascii_face; \
18289 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18290 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18291 /* At first, fill in `char2b' and `faces'. */ \
18292 for (n = 0; n < glyph_len; n++) \
18294 int c = COMPOSITION_GLYPH (cmp, n); \
18295 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18296 faces[n] = FACE_FROM_ID (f, this_face_id); \
18297 get_char_face_and_encoding (f, c, this_face_id, \
18298 char2b + n, 1, 1); \
18301 /* Make glyph_strings for each glyph sequence that is drawable by \
18302 the same face, and append them to HEAD/TAIL. */ \
18303 for (n = 0; n < cmp->glyph_len;) \
18305 s = (struct glyph_string *) alloca (sizeof *s); \
18306 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18307 append_glyph_string (&(HEAD), &(TAIL), s); \
18308 s->cmp = cmp; \
18309 s->gidx = n; \
18310 s->x = (X); \
18312 if (n == 0) \
18313 first_s = s; \
18315 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18318 ++START; \
18319 s = first_s; \
18320 } while (0)
18323 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18324 of AREA of glyph row ROW on window W between indices START and END.
18325 HL overrides the face for drawing glyph strings, e.g. it is
18326 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18327 x-positions of the drawing area.
18329 This is an ugly monster macro construct because we must use alloca
18330 to allocate glyph strings (because draw_glyphs can be called
18331 asynchronously). */
18333 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18334 do \
18336 HEAD = TAIL = NULL; \
18337 while (START < END) \
18339 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18340 switch (first_glyph->type) \
18342 case CHAR_GLYPH: \
18343 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18344 HL, X, LAST_X); \
18345 break; \
18347 case COMPOSITE_GLYPH: \
18348 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18349 HL, X, LAST_X); \
18350 break; \
18352 case STRETCH_GLYPH: \
18353 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18354 HL, X, LAST_X); \
18355 break; \
18357 case IMAGE_GLYPH: \
18358 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18359 HL, X, LAST_X); \
18360 break; \
18362 default: \
18363 abort (); \
18366 set_glyph_string_background_width (s, START, LAST_X); \
18367 (X) += s->width; \
18370 while (0)
18373 /* Draw glyphs between START and END in AREA of ROW on window W,
18374 starting at x-position X. X is relative to AREA in W. HL is a
18375 face-override with the following meaning:
18377 DRAW_NORMAL_TEXT draw normally
18378 DRAW_CURSOR draw in cursor face
18379 DRAW_MOUSE_FACE draw in mouse face.
18380 DRAW_INVERSE_VIDEO draw in mode line face
18381 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18382 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18384 If OVERLAPS_P is non-zero, draw only the foreground of characters
18385 and clip to the physical height of ROW.
18387 Value is the x-position reached, relative to AREA of W. */
18389 static int
18390 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18391 struct window *w;
18392 int x;
18393 struct glyph_row *row;
18394 enum glyph_row_area area;
18395 int start, end;
18396 enum draw_glyphs_face hl;
18397 int overlaps_p;
18399 struct glyph_string *head, *tail;
18400 struct glyph_string *s;
18401 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18402 int last_x, area_width;
18403 int x_reached;
18404 int i, j;
18405 struct frame *f = XFRAME (WINDOW_FRAME (w));
18406 DECLARE_HDC (hdc);
18408 ALLOCATE_HDC (hdc, f);
18410 /* Let's rather be paranoid than getting a SEGV. */
18411 end = min (end, row->used[area]);
18412 start = max (0, start);
18413 start = min (end, start);
18415 /* Translate X to frame coordinates. Set last_x to the right
18416 end of the drawing area. */
18417 if (row->full_width_p)
18419 /* X is relative to the left edge of W, without scroll bars
18420 or fringes. */
18421 x += WINDOW_LEFT_EDGE_X (w);
18422 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18424 else
18426 int area_left = window_box_left (w, area);
18427 x += area_left;
18428 area_width = window_box_width (w, area);
18429 last_x = area_left + area_width;
18432 /* Build a doubly-linked list of glyph_string structures between
18433 head and tail from what we have to draw. Note that the macro
18434 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18435 the reason we use a separate variable `i'. */
18436 i = start;
18437 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18438 if (tail)
18439 x_reached = tail->x + tail->background_width;
18440 else
18441 x_reached = x;
18443 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18444 the row, redraw some glyphs in front or following the glyph
18445 strings built above. */
18446 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18448 int dummy_x = 0;
18449 struct glyph_string *h, *t;
18451 /* Compute overhangs for all glyph strings. */
18452 if (rif->compute_glyph_string_overhangs)
18453 for (s = head; s; s = s->next)
18454 rif->compute_glyph_string_overhangs (s);
18456 /* Prepend glyph strings for glyphs in front of the first glyph
18457 string that are overwritten because of the first glyph
18458 string's left overhang. The background of all strings
18459 prepended must be drawn because the first glyph string
18460 draws over it. */
18461 i = left_overwritten (head);
18462 if (i >= 0)
18464 j = i;
18465 BUILD_GLYPH_STRINGS (j, start, h, t,
18466 DRAW_NORMAL_TEXT, dummy_x, last_x);
18467 start = i;
18468 compute_overhangs_and_x (t, head->x, 1);
18469 prepend_glyph_string_lists (&head, &tail, h, t);
18470 clip_head = head;
18473 /* Prepend glyph strings for glyphs in front of the first glyph
18474 string that overwrite that glyph string because of their
18475 right overhang. For these strings, only the foreground must
18476 be drawn, because it draws over the glyph string at `head'.
18477 The background must not be drawn because this would overwrite
18478 right overhangs of preceding glyphs for which no glyph
18479 strings exist. */
18480 i = left_overwriting (head);
18481 if (i >= 0)
18483 clip_head = head;
18484 BUILD_GLYPH_STRINGS (i, start, h, t,
18485 DRAW_NORMAL_TEXT, dummy_x, last_x);
18486 for (s = h; s; s = s->next)
18487 s->background_filled_p = 1;
18488 compute_overhangs_and_x (t, head->x, 1);
18489 prepend_glyph_string_lists (&head, &tail, h, t);
18492 /* Append glyphs strings for glyphs following the last glyph
18493 string tail that are overwritten by tail. The background of
18494 these strings has to be drawn because tail's foreground draws
18495 over it. */
18496 i = right_overwritten (tail);
18497 if (i >= 0)
18499 BUILD_GLYPH_STRINGS (end, i, h, t,
18500 DRAW_NORMAL_TEXT, x, last_x);
18501 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18502 append_glyph_string_lists (&head, &tail, h, t);
18503 clip_tail = tail;
18506 /* Append glyph strings for glyphs following the last glyph
18507 string tail that overwrite tail. The foreground of such
18508 glyphs has to be drawn because it writes into the background
18509 of tail. The background must not be drawn because it could
18510 paint over the foreground of following glyphs. */
18511 i = right_overwriting (tail);
18512 if (i >= 0)
18514 clip_tail = tail;
18515 BUILD_GLYPH_STRINGS (end, i, h, t,
18516 DRAW_NORMAL_TEXT, x, last_x);
18517 for (s = h; s; s = s->next)
18518 s->background_filled_p = 1;
18519 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18520 append_glyph_string_lists (&head, &tail, h, t);
18522 if (clip_head || clip_tail)
18523 for (s = head; s; s = s->next)
18525 s->clip_head = clip_head;
18526 s->clip_tail = clip_tail;
18530 /* Draw all strings. */
18531 for (s = head; s; s = s->next)
18532 rif->draw_glyph_string (s);
18534 if (area == TEXT_AREA
18535 && !row->full_width_p
18536 /* When drawing overlapping rows, only the glyph strings'
18537 foreground is drawn, which doesn't erase a cursor
18538 completely. */
18539 && !overlaps_p)
18541 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18542 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18543 : (tail ? tail->x + tail->background_width : x));
18545 int text_left = window_box_left (w, TEXT_AREA);
18546 x0 -= text_left;
18547 x1 -= text_left;
18549 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18550 row->y, MATRIX_ROW_BOTTOM_Y (row));
18553 /* Value is the x-position up to which drawn, relative to AREA of W.
18554 This doesn't include parts drawn because of overhangs. */
18555 if (row->full_width_p)
18556 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18557 else
18558 x_reached -= window_box_left (w, area);
18560 RELEASE_HDC (hdc, f);
18562 return x_reached;
18565 /* Expand row matrix if too narrow. Don't expand if area
18566 is not present. */
18568 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18570 if (!fonts_changed_p \
18571 && (it->glyph_row->glyphs[area] \
18572 < it->glyph_row->glyphs[area + 1])) \
18574 it->w->ncols_scale_factor++; \
18575 fonts_changed_p = 1; \
18579 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18580 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18582 static INLINE void
18583 append_glyph (it)
18584 struct it *it;
18586 struct glyph *glyph;
18587 enum glyph_row_area area = it->area;
18589 xassert (it->glyph_row);
18590 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18592 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18593 if (glyph < it->glyph_row->glyphs[area + 1])
18595 glyph->charpos = CHARPOS (it->position);
18596 glyph->object = it->object;
18597 glyph->pixel_width = it->pixel_width;
18598 glyph->ascent = it->ascent;
18599 glyph->descent = it->descent;
18600 glyph->voffset = it->voffset;
18601 glyph->type = CHAR_GLYPH;
18602 glyph->multibyte_p = it->multibyte_p;
18603 glyph->left_box_line_p = it->start_of_box_run_p;
18604 glyph->right_box_line_p = it->end_of_box_run_p;
18605 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18606 || it->phys_descent > it->descent);
18607 glyph->padding_p = 0;
18608 glyph->glyph_not_available_p = it->glyph_not_available_p;
18609 glyph->face_id = it->face_id;
18610 glyph->u.ch = it->char_to_display;
18611 glyph->slice = null_glyph_slice;
18612 glyph->font_type = FONT_TYPE_UNKNOWN;
18613 ++it->glyph_row->used[area];
18615 else
18616 IT_EXPAND_MATRIX_WIDTH (it, area);
18619 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18620 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18622 static INLINE void
18623 append_composite_glyph (it)
18624 struct it *it;
18626 struct glyph *glyph;
18627 enum glyph_row_area area = it->area;
18629 xassert (it->glyph_row);
18631 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18632 if (glyph < it->glyph_row->glyphs[area + 1])
18634 glyph->charpos = CHARPOS (it->position);
18635 glyph->object = it->object;
18636 glyph->pixel_width = it->pixel_width;
18637 glyph->ascent = it->ascent;
18638 glyph->descent = it->descent;
18639 glyph->voffset = it->voffset;
18640 glyph->type = COMPOSITE_GLYPH;
18641 glyph->multibyte_p = it->multibyte_p;
18642 glyph->left_box_line_p = it->start_of_box_run_p;
18643 glyph->right_box_line_p = it->end_of_box_run_p;
18644 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18645 || it->phys_descent > it->descent);
18646 glyph->padding_p = 0;
18647 glyph->glyph_not_available_p = 0;
18648 glyph->face_id = it->face_id;
18649 glyph->u.cmp_id = it->cmp_id;
18650 glyph->slice = null_glyph_slice;
18651 glyph->font_type = FONT_TYPE_UNKNOWN;
18652 ++it->glyph_row->used[area];
18654 else
18655 IT_EXPAND_MATRIX_WIDTH (it, area);
18659 /* Change IT->ascent and IT->height according to the setting of
18660 IT->voffset. */
18662 static INLINE void
18663 take_vertical_position_into_account (it)
18664 struct it *it;
18666 if (it->voffset)
18668 if (it->voffset < 0)
18669 /* Increase the ascent so that we can display the text higher
18670 in the line. */
18671 it->ascent -= it->voffset;
18672 else
18673 /* Increase the descent so that we can display the text lower
18674 in the line. */
18675 it->descent += it->voffset;
18680 /* Produce glyphs/get display metrics for the image IT is loaded with.
18681 See the description of struct display_iterator in dispextern.h for
18682 an overview of struct display_iterator. */
18684 static void
18685 produce_image_glyph (it)
18686 struct it *it;
18688 struct image *img;
18689 struct face *face;
18690 int glyph_ascent;
18691 struct glyph_slice slice;
18693 xassert (it->what == IT_IMAGE);
18695 face = FACE_FROM_ID (it->f, it->face_id);
18696 xassert (face);
18697 /* Make sure X resources of the face is loaded. */
18698 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18700 if (it->image_id < 0)
18702 /* Fringe bitmap. */
18703 it->ascent = it->phys_ascent = 0;
18704 it->descent = it->phys_descent = 0;
18705 it->pixel_width = 0;
18706 it->nglyphs = 0;
18707 return;
18710 img = IMAGE_FROM_ID (it->f, it->image_id);
18711 xassert (img);
18712 /* Make sure X resources of the image is loaded. */
18713 prepare_image_for_display (it->f, img);
18715 slice.x = slice.y = 0;
18716 slice.width = img->width;
18717 slice.height = img->height;
18719 if (INTEGERP (it->slice.x))
18720 slice.x = XINT (it->slice.x);
18721 else if (FLOATP (it->slice.x))
18722 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18724 if (INTEGERP (it->slice.y))
18725 slice.y = XINT (it->slice.y);
18726 else if (FLOATP (it->slice.y))
18727 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18729 if (INTEGERP (it->slice.width))
18730 slice.width = XINT (it->slice.width);
18731 else if (FLOATP (it->slice.width))
18732 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18734 if (INTEGERP (it->slice.height))
18735 slice.height = XINT (it->slice.height);
18736 else if (FLOATP (it->slice.height))
18737 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18739 if (slice.x >= img->width)
18740 slice.x = img->width;
18741 if (slice.y >= img->height)
18742 slice.y = img->height;
18743 if (slice.x + slice.width >= img->width)
18744 slice.width = img->width - slice.x;
18745 if (slice.y + slice.height > img->height)
18746 slice.height = img->height - slice.y;
18748 if (slice.width == 0 || slice.height == 0)
18749 return;
18751 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18753 it->descent = slice.height - glyph_ascent;
18754 if (slice.y == 0)
18755 it->descent += img->vmargin;
18756 if (slice.y + slice.height == img->height)
18757 it->descent += img->vmargin;
18758 it->phys_descent = it->descent;
18760 it->pixel_width = slice.width;
18761 if (slice.x == 0)
18762 it->pixel_width += img->hmargin;
18763 if (slice.x + slice.width == img->width)
18764 it->pixel_width += img->hmargin;
18766 /* It's quite possible for images to have an ascent greater than
18767 their height, so don't get confused in that case. */
18768 if (it->descent < 0)
18769 it->descent = 0;
18771 #if 0 /* this breaks image tiling */
18772 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18773 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18774 if (face_ascent > it->ascent)
18775 it->ascent = it->phys_ascent = face_ascent;
18776 #endif
18778 it->nglyphs = 1;
18780 if (face->box != FACE_NO_BOX)
18782 if (face->box_line_width > 0)
18784 if (slice.y == 0)
18785 it->ascent += face->box_line_width;
18786 if (slice.y + slice.height == img->height)
18787 it->descent += face->box_line_width;
18790 if (it->start_of_box_run_p && slice.x == 0)
18791 it->pixel_width += abs (face->box_line_width);
18792 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18793 it->pixel_width += abs (face->box_line_width);
18796 take_vertical_position_into_account (it);
18798 if (it->glyph_row)
18800 struct glyph *glyph;
18801 enum glyph_row_area area = it->area;
18803 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18804 if (glyph < it->glyph_row->glyphs[area + 1])
18806 glyph->charpos = CHARPOS (it->position);
18807 glyph->object = it->object;
18808 glyph->pixel_width = it->pixel_width;
18809 glyph->ascent = glyph_ascent;
18810 glyph->descent = it->descent;
18811 glyph->voffset = it->voffset;
18812 glyph->type = IMAGE_GLYPH;
18813 glyph->multibyte_p = it->multibyte_p;
18814 glyph->left_box_line_p = it->start_of_box_run_p;
18815 glyph->right_box_line_p = it->end_of_box_run_p;
18816 glyph->overlaps_vertically_p = 0;
18817 glyph->padding_p = 0;
18818 glyph->glyph_not_available_p = 0;
18819 glyph->face_id = it->face_id;
18820 glyph->u.img_id = img->id;
18821 glyph->slice = slice;
18822 glyph->font_type = FONT_TYPE_UNKNOWN;
18823 ++it->glyph_row->used[area];
18825 else
18826 IT_EXPAND_MATRIX_WIDTH (it, area);
18831 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18832 of the glyph, WIDTH and HEIGHT are the width and height of the
18833 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18835 static void
18836 append_stretch_glyph (it, object, width, height, ascent)
18837 struct it *it;
18838 Lisp_Object object;
18839 int width, height;
18840 int ascent;
18842 struct glyph *glyph;
18843 enum glyph_row_area area = it->area;
18845 xassert (ascent >= 0 && ascent <= height);
18847 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18848 if (glyph < it->glyph_row->glyphs[area + 1])
18850 glyph->charpos = CHARPOS (it->position);
18851 glyph->object = object;
18852 glyph->pixel_width = width;
18853 glyph->ascent = ascent;
18854 glyph->descent = height - ascent;
18855 glyph->voffset = it->voffset;
18856 glyph->type = STRETCH_GLYPH;
18857 glyph->multibyte_p = it->multibyte_p;
18858 glyph->left_box_line_p = it->start_of_box_run_p;
18859 glyph->right_box_line_p = it->end_of_box_run_p;
18860 glyph->overlaps_vertically_p = 0;
18861 glyph->padding_p = 0;
18862 glyph->glyph_not_available_p = 0;
18863 glyph->face_id = it->face_id;
18864 glyph->u.stretch.ascent = ascent;
18865 glyph->u.stretch.height = height;
18866 glyph->slice = null_glyph_slice;
18867 glyph->font_type = FONT_TYPE_UNKNOWN;
18868 ++it->glyph_row->used[area];
18870 else
18871 IT_EXPAND_MATRIX_WIDTH (it, area);
18875 /* Produce a stretch glyph for iterator IT. IT->object is the value
18876 of the glyph property displayed. The value must be a list
18877 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18878 being recognized:
18880 1. `:width WIDTH' specifies that the space should be WIDTH *
18881 canonical char width wide. WIDTH may be an integer or floating
18882 point number.
18884 2. `:relative-width FACTOR' specifies that the width of the stretch
18885 should be computed from the width of the first character having the
18886 `glyph' property, and should be FACTOR times that width.
18888 3. `:align-to HPOS' specifies that the space should be wide enough
18889 to reach HPOS, a value in canonical character units.
18891 Exactly one of the above pairs must be present.
18893 4. `:height HEIGHT' specifies that the height of the stretch produced
18894 should be HEIGHT, measured in canonical character units.
18896 5. `:relative-height FACTOR' specifies that the height of the
18897 stretch should be FACTOR times the height of the characters having
18898 the glyph property.
18900 Either none or exactly one of 4 or 5 must be present.
18902 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18903 of the stretch should be used for the ascent of the stretch.
18904 ASCENT must be in the range 0 <= ASCENT <= 100. */
18906 static void
18907 produce_stretch_glyph (it)
18908 struct it *it;
18910 /* (space :width WIDTH :height HEIGHT ...) */
18911 Lisp_Object prop, plist;
18912 int width = 0, height = 0, align_to = -1;
18913 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18914 int ascent = 0;
18915 double tem;
18916 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18917 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18919 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18921 /* List should start with `space'. */
18922 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18923 plist = XCDR (it->object);
18925 /* Compute the width of the stretch. */
18926 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
18927 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18929 /* Absolute width `:width WIDTH' specified and valid. */
18930 zero_width_ok_p = 1;
18931 width = (int)tem;
18933 else if (prop = Fplist_get (plist, QCrelative_width),
18934 NUMVAL (prop) > 0)
18936 /* Relative width `:relative-width FACTOR' specified and valid.
18937 Compute the width of the characters having the `glyph'
18938 property. */
18939 struct it it2;
18940 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18942 it2 = *it;
18943 if (it->multibyte_p)
18945 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18946 - IT_BYTEPOS (*it));
18947 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18949 else
18950 it2.c = *p, it2.len = 1;
18952 it2.glyph_row = NULL;
18953 it2.what = IT_CHARACTER;
18954 x_produce_glyphs (&it2);
18955 width = NUMVAL (prop) * it2.pixel_width;
18957 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
18958 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18960 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18961 align_to = (align_to < 0
18963 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18964 else if (align_to < 0)
18965 align_to = window_box_left_offset (it->w, TEXT_AREA);
18966 width = max (0, (int)tem + align_to - it->current_x);
18967 zero_width_ok_p = 1;
18969 else
18970 /* Nothing specified -> width defaults to canonical char width. */
18971 width = FRAME_COLUMN_WIDTH (it->f);
18973 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18974 width = 1;
18976 /* Compute height. */
18977 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
18978 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18980 height = (int)tem;
18981 zero_height_ok_p = 1;
18983 else if (prop = Fplist_get (plist, QCrelative_height),
18984 NUMVAL (prop) > 0)
18985 height = FONT_HEIGHT (font) * NUMVAL (prop);
18986 else
18987 height = FONT_HEIGHT (font);
18989 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18990 height = 1;
18992 /* Compute percentage of height used for ascent. If
18993 `:ascent ASCENT' is present and valid, use that. Otherwise,
18994 derive the ascent from the font in use. */
18995 if (prop = Fplist_get (plist, QCascent),
18996 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18997 ascent = height * NUMVAL (prop) / 100.0;
18998 else if (!NILP (prop)
18999 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19000 ascent = min (max (0, (int)tem), height);
19001 else
19002 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19004 if (width > 0 && height > 0 && it->glyph_row)
19006 Lisp_Object object = it->stack[it->sp - 1].string;
19007 if (!STRINGP (object))
19008 object = it->w->buffer;
19009 append_stretch_glyph (it, object, width, height, ascent);
19012 it->pixel_width = width;
19013 it->ascent = it->phys_ascent = ascent;
19014 it->descent = it->phys_descent = height - it->ascent;
19015 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19017 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19019 if (face->box_line_width > 0)
19021 it->ascent += face->box_line_width;
19022 it->descent += face->box_line_width;
19025 if (it->start_of_box_run_p)
19026 it->pixel_width += abs (face->box_line_width);
19027 if (it->end_of_box_run_p)
19028 it->pixel_width += abs (face->box_line_width);
19031 take_vertical_position_into_account (it);
19034 /* Get line-height and line-spacing property at point.
19035 If line-height has format (HEIGHT TOTAL), return TOTAL
19036 in TOTAL_HEIGHT. */
19038 static Lisp_Object
19039 get_line_height_property (it, prop)
19040 struct it *it;
19041 Lisp_Object prop;
19043 Lisp_Object position, val;
19045 if (STRINGP (it->object))
19046 position = make_number (IT_STRING_CHARPOS (*it));
19047 else if (BUFFERP (it->object))
19048 position = make_number (IT_CHARPOS (*it));
19049 else
19050 return Qnil;
19052 return Fget_char_property (position, prop, it->object);
19055 /* Calculate line-height and line-spacing properties.
19056 An integer value specifies explicit pixel value.
19057 A float value specifies relative value to current face height.
19058 A cons (float . face-name) specifies relative value to
19059 height of specified face font.
19061 Returns height in pixels, or nil. */
19064 static Lisp_Object
19065 calc_line_height_property (it, val, font, boff, override)
19066 struct it *it;
19067 Lisp_Object val;
19068 XFontStruct *font;
19069 int boff, override;
19071 Lisp_Object face_name = Qnil;
19072 int ascent, descent, height;
19074 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19075 return val;
19077 if (CONSP (val))
19079 face_name = XCAR (val);
19080 val = XCDR (val);
19081 if (!NUMBERP (val))
19082 val = make_number (1);
19083 if (NILP (face_name))
19085 height = it->ascent + it->descent;
19086 goto scale;
19090 if (NILP (face_name))
19092 font = FRAME_FONT (it->f);
19093 boff = FRAME_BASELINE_OFFSET (it->f);
19095 else if (EQ (face_name, Qt))
19097 override = 0;
19099 else
19101 int face_id;
19102 struct face *face;
19103 struct font_info *font_info;
19105 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19106 if (face_id < 0)
19107 return make_number (-1);
19109 face = FACE_FROM_ID (it->f, face_id);
19110 font = face->font;
19111 if (font == NULL)
19112 return make_number (-1);
19114 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19115 boff = font_info->baseline_offset;
19116 if (font_info->vertical_centering)
19117 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19120 ascent = FONT_BASE (font) + boff;
19121 descent = FONT_DESCENT (font) - boff;
19123 if (override)
19125 it->override_ascent = ascent;
19126 it->override_descent = descent;
19127 it->override_boff = boff;
19130 height = ascent + descent;
19132 scale:
19133 if (FLOATP (val))
19134 height = (int)(XFLOAT_DATA (val) * height);
19135 else if (INTEGERP (val))
19136 height *= XINT (val);
19138 return make_number (height);
19142 /* RIF:
19143 Produce glyphs/get display metrics for the display element IT is
19144 loaded with. See the description of struct display_iterator in
19145 dispextern.h for an overview of struct display_iterator. */
19147 void
19148 x_produce_glyphs (it)
19149 struct it *it;
19151 int extra_line_spacing = it->extra_line_spacing;
19153 it->glyph_not_available_p = 0;
19155 if (it->what == IT_CHARACTER)
19157 XChar2b char2b;
19158 XFontStruct *font;
19159 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19160 XCharStruct *pcm;
19161 int font_not_found_p;
19162 struct font_info *font_info;
19163 int boff; /* baseline offset */
19164 /* We may change it->multibyte_p upon unibyte<->multibyte
19165 conversion. So, save the current value now and restore it
19166 later.
19168 Note: It seems that we don't have to record multibyte_p in
19169 struct glyph because the character code itself tells if or
19170 not the character is multibyte. Thus, in the future, we must
19171 consider eliminating the field `multibyte_p' in the struct
19172 glyph. */
19173 int saved_multibyte_p = it->multibyte_p;
19175 /* Maybe translate single-byte characters to multibyte, or the
19176 other way. */
19177 it->char_to_display = it->c;
19178 if (!ASCII_BYTE_P (it->c))
19180 if (unibyte_display_via_language_environment
19181 && SINGLE_BYTE_CHAR_P (it->c)
19182 && (it->c >= 0240
19183 || !NILP (Vnonascii_translation_table)))
19185 it->char_to_display = unibyte_char_to_multibyte (it->c);
19186 it->multibyte_p = 1;
19187 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19188 face = FACE_FROM_ID (it->f, it->face_id);
19190 else if (!SINGLE_BYTE_CHAR_P (it->c)
19191 && !it->multibyte_p)
19193 it->multibyte_p = 1;
19194 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19195 face = FACE_FROM_ID (it->f, it->face_id);
19199 /* Get font to use. Encode IT->char_to_display. */
19200 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19201 &char2b, it->multibyte_p, 0);
19202 font = face->font;
19204 /* When no suitable font found, use the default font. */
19205 font_not_found_p = font == NULL;
19206 if (font_not_found_p)
19208 font = FRAME_FONT (it->f);
19209 boff = FRAME_BASELINE_OFFSET (it->f);
19210 font_info = NULL;
19212 else
19214 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19215 boff = font_info->baseline_offset;
19216 if (font_info->vertical_centering)
19217 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19220 if (it->char_to_display >= ' '
19221 && (!it->multibyte_p || it->char_to_display < 128))
19223 /* Either unibyte or ASCII. */
19224 int stretched_p;
19226 it->nglyphs = 1;
19228 pcm = rif->per_char_metric (font, &char2b,
19229 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19231 if (it->override_ascent >= 0)
19233 it->ascent = it->override_ascent;
19234 it->descent = it->override_descent;
19235 boff = it->override_boff;
19237 else
19239 it->ascent = FONT_BASE (font) + boff;
19240 it->descent = FONT_DESCENT (font) - boff;
19243 if (pcm)
19245 it->phys_ascent = pcm->ascent + boff;
19246 it->phys_descent = pcm->descent - boff;
19247 it->pixel_width = pcm->width;
19249 else
19251 it->glyph_not_available_p = 1;
19252 it->phys_ascent = it->ascent;
19253 it->phys_descent = it->descent;
19254 it->pixel_width = FONT_WIDTH (font);
19257 if (it->constrain_row_ascent_descent_p)
19259 if (it->descent > it->max_descent)
19261 it->ascent += it->descent - it->max_descent;
19262 it->descent = it->max_descent;
19264 if (it->ascent > it->max_ascent)
19266 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19267 it->ascent = it->max_ascent;
19269 it->phys_ascent = min (it->phys_ascent, it->ascent);
19270 it->phys_descent = min (it->phys_descent, it->descent);
19271 extra_line_spacing = 0;
19274 /* If this is a space inside a region of text with
19275 `space-width' property, change its width. */
19276 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19277 if (stretched_p)
19278 it->pixel_width *= XFLOATINT (it->space_width);
19280 /* If face has a box, add the box thickness to the character
19281 height. If character has a box line to the left and/or
19282 right, add the box line width to the character's width. */
19283 if (face->box != FACE_NO_BOX)
19285 int thick = face->box_line_width;
19287 if (thick > 0)
19289 it->ascent += thick;
19290 it->descent += thick;
19292 else
19293 thick = -thick;
19295 if (it->start_of_box_run_p)
19296 it->pixel_width += thick;
19297 if (it->end_of_box_run_p)
19298 it->pixel_width += thick;
19301 /* If face has an overline, add the height of the overline
19302 (1 pixel) and a 1 pixel margin to the character height. */
19303 if (face->overline_p)
19304 it->ascent += 2;
19306 if (it->constrain_row_ascent_descent_p)
19308 if (it->ascent > it->max_ascent)
19309 it->ascent = it->max_ascent;
19310 if (it->descent > it->max_descent)
19311 it->descent = it->max_descent;
19314 take_vertical_position_into_account (it);
19316 /* If we have to actually produce glyphs, do it. */
19317 if (it->glyph_row)
19319 if (stretched_p)
19321 /* Translate a space with a `space-width' property
19322 into a stretch glyph. */
19323 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19324 / FONT_HEIGHT (font));
19325 append_stretch_glyph (it, it->object, it->pixel_width,
19326 it->ascent + it->descent, ascent);
19328 else
19329 append_glyph (it);
19331 /* If characters with lbearing or rbearing are displayed
19332 in this line, record that fact in a flag of the
19333 glyph row. This is used to optimize X output code. */
19334 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19335 it->glyph_row->contains_overlapping_glyphs_p = 1;
19338 else if (it->char_to_display == '\n')
19340 /* A newline has no width but we need the height of the line.
19341 But if previous part of the line set a height, don't
19342 increase that height */
19344 Lisp_Object height;
19345 Lisp_Object total_height = Qnil;
19347 it->override_ascent = -1;
19348 it->pixel_width = 0;
19349 it->nglyphs = 0;
19351 height = get_line_height_property(it, Qline_height);
19352 /* Split (line-height total-height) list */
19353 if (CONSP (height)
19354 && CONSP (XCDR (height))
19355 && NILP (XCDR (XCDR (height))))
19357 total_height = XCAR (XCDR (height));
19358 height = XCAR (height);
19360 height = calc_line_height_property(it, height, font, boff, 1);
19362 if (it->override_ascent >= 0)
19364 it->ascent = it->override_ascent;
19365 it->descent = it->override_descent;
19366 boff = it->override_boff;
19368 else
19370 it->ascent = FONT_BASE (font) + boff;
19371 it->descent = FONT_DESCENT (font) - boff;
19374 if (EQ (height, Qt))
19376 if (it->descent > it->max_descent)
19378 it->ascent += it->descent - it->max_descent;
19379 it->descent = it->max_descent;
19381 if (it->ascent > it->max_ascent)
19383 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19384 it->ascent = it->max_ascent;
19386 it->phys_ascent = min (it->phys_ascent, it->ascent);
19387 it->phys_descent = min (it->phys_descent, it->descent);
19388 it->constrain_row_ascent_descent_p = 1;
19389 extra_line_spacing = 0;
19391 else
19393 Lisp_Object spacing;
19394 int total = 0;
19396 it->phys_ascent = it->ascent;
19397 it->phys_descent = it->descent;
19399 if ((it->max_ascent > 0 || it->max_descent > 0)
19400 && face->box != FACE_NO_BOX
19401 && face->box_line_width > 0)
19403 it->ascent += face->box_line_width;
19404 it->descent += face->box_line_width;
19406 if (!NILP (height)
19407 && XINT (height) > it->ascent + it->descent)
19408 it->ascent = XINT (height) - it->descent;
19410 if (!NILP (total_height))
19411 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19412 else
19414 spacing = get_line_height_property(it, Qline_spacing);
19415 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19417 if (INTEGERP (spacing))
19419 extra_line_spacing = XINT (spacing);
19420 if (!NILP (total_height))
19421 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19425 else if (it->char_to_display == '\t')
19427 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19428 int x = it->current_x + it->continuation_lines_width;
19429 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19431 /* If the distance from the current position to the next tab
19432 stop is less than a space character width, use the
19433 tab stop after that. */
19434 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19435 next_tab_x += tab_width;
19437 it->pixel_width = next_tab_x - x;
19438 it->nglyphs = 1;
19439 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19440 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19442 if (it->glyph_row)
19444 append_stretch_glyph (it, it->object, it->pixel_width,
19445 it->ascent + it->descent, it->ascent);
19448 else
19450 /* A multi-byte character. Assume that the display width of the
19451 character is the width of the character multiplied by the
19452 width of the font. */
19454 /* If we found a font, this font should give us the right
19455 metrics. If we didn't find a font, use the frame's
19456 default font and calculate the width of the character
19457 from the charset width; this is what old redisplay code
19458 did. */
19460 pcm = rif->per_char_metric (font, &char2b,
19461 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19463 if (font_not_found_p || !pcm)
19465 int charset = CHAR_CHARSET (it->char_to_display);
19467 it->glyph_not_available_p = 1;
19468 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19469 * CHARSET_WIDTH (charset));
19470 it->phys_ascent = FONT_BASE (font) + boff;
19471 it->phys_descent = FONT_DESCENT (font) - boff;
19473 else
19475 it->pixel_width = pcm->width;
19476 it->phys_ascent = pcm->ascent + boff;
19477 it->phys_descent = pcm->descent - boff;
19478 if (it->glyph_row
19479 && (pcm->lbearing < 0
19480 || pcm->rbearing > pcm->width))
19481 it->glyph_row->contains_overlapping_glyphs_p = 1;
19483 it->nglyphs = 1;
19484 it->ascent = FONT_BASE (font) + boff;
19485 it->descent = FONT_DESCENT (font) - boff;
19486 if (face->box != FACE_NO_BOX)
19488 int thick = face->box_line_width;
19490 if (thick > 0)
19492 it->ascent += thick;
19493 it->descent += thick;
19495 else
19496 thick = - thick;
19498 if (it->start_of_box_run_p)
19499 it->pixel_width += thick;
19500 if (it->end_of_box_run_p)
19501 it->pixel_width += thick;
19504 /* If face has an overline, add the height of the overline
19505 (1 pixel) and a 1 pixel margin to the character height. */
19506 if (face->overline_p)
19507 it->ascent += 2;
19509 take_vertical_position_into_account (it);
19511 if (it->glyph_row)
19512 append_glyph (it);
19514 it->multibyte_p = saved_multibyte_p;
19516 else if (it->what == IT_COMPOSITION)
19518 /* Note: A composition is represented as one glyph in the
19519 glyph matrix. There are no padding glyphs. */
19520 XChar2b char2b;
19521 XFontStruct *font;
19522 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19523 XCharStruct *pcm;
19524 int font_not_found_p;
19525 struct font_info *font_info;
19526 int boff; /* baseline offset */
19527 struct composition *cmp = composition_table[it->cmp_id];
19529 /* Maybe translate single-byte characters to multibyte. */
19530 it->char_to_display = it->c;
19531 if (unibyte_display_via_language_environment
19532 && SINGLE_BYTE_CHAR_P (it->c)
19533 && (it->c >= 0240
19534 || (it->c >= 0200
19535 && !NILP (Vnonascii_translation_table))))
19537 it->char_to_display = unibyte_char_to_multibyte (it->c);
19540 /* Get face and font to use. Encode IT->char_to_display. */
19541 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19542 face = FACE_FROM_ID (it->f, it->face_id);
19543 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19544 &char2b, it->multibyte_p, 0);
19545 font = face->font;
19547 /* When no suitable font found, use the default font. */
19548 font_not_found_p = font == NULL;
19549 if (font_not_found_p)
19551 font = FRAME_FONT (it->f);
19552 boff = FRAME_BASELINE_OFFSET (it->f);
19553 font_info = NULL;
19555 else
19557 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19558 boff = font_info->baseline_offset;
19559 if (font_info->vertical_centering)
19560 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19563 /* There are no padding glyphs, so there is only one glyph to
19564 produce for the composition. Important is that pixel_width,
19565 ascent and descent are the values of what is drawn by
19566 draw_glyphs (i.e. the values of the overall glyphs composed). */
19567 it->nglyphs = 1;
19569 /* If we have not yet calculated pixel size data of glyphs of
19570 the composition for the current face font, calculate them
19571 now. Theoretically, we have to check all fonts for the
19572 glyphs, but that requires much time and memory space. So,
19573 here we check only the font of the first glyph. This leads
19574 to incorrect display very rarely, and C-l (recenter) can
19575 correct the display anyway. */
19576 if (cmp->font != (void *) font)
19578 /* Ascent and descent of the font of the first character of
19579 this composition (adjusted by baseline offset). Ascent
19580 and descent of overall glyphs should not be less than
19581 them respectively. */
19582 int font_ascent = FONT_BASE (font) + boff;
19583 int font_descent = FONT_DESCENT (font) - boff;
19584 /* Bounding box of the overall glyphs. */
19585 int leftmost, rightmost, lowest, highest;
19586 int i, width, ascent, descent;
19588 cmp->font = (void *) font;
19590 /* Initialize the bounding box. */
19591 if (font_info
19592 && (pcm = rif->per_char_metric (font, &char2b,
19593 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19595 width = pcm->width;
19596 ascent = pcm->ascent;
19597 descent = pcm->descent;
19599 else
19601 width = FONT_WIDTH (font);
19602 ascent = FONT_BASE (font);
19603 descent = FONT_DESCENT (font);
19606 rightmost = width;
19607 lowest = - descent + boff;
19608 highest = ascent + boff;
19609 leftmost = 0;
19611 if (font_info
19612 && font_info->default_ascent
19613 && CHAR_TABLE_P (Vuse_default_ascent)
19614 && !NILP (Faref (Vuse_default_ascent,
19615 make_number (it->char_to_display))))
19616 highest = font_info->default_ascent + boff;
19618 /* Draw the first glyph at the normal position. It may be
19619 shifted to right later if some other glyphs are drawn at
19620 the left. */
19621 cmp->offsets[0] = 0;
19622 cmp->offsets[1] = boff;
19624 /* Set cmp->offsets for the remaining glyphs. */
19625 for (i = 1; i < cmp->glyph_len; i++)
19627 int left, right, btm, top;
19628 int ch = COMPOSITION_GLYPH (cmp, i);
19629 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19631 face = FACE_FROM_ID (it->f, face_id);
19632 get_char_face_and_encoding (it->f, ch, face->id,
19633 &char2b, it->multibyte_p, 0);
19634 font = face->font;
19635 if (font == NULL)
19637 font = FRAME_FONT (it->f);
19638 boff = FRAME_BASELINE_OFFSET (it->f);
19639 font_info = NULL;
19641 else
19643 font_info
19644 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19645 boff = font_info->baseline_offset;
19646 if (font_info->vertical_centering)
19647 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19650 if (font_info
19651 && (pcm = rif->per_char_metric (font, &char2b,
19652 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19654 width = pcm->width;
19655 ascent = pcm->ascent;
19656 descent = pcm->descent;
19658 else
19660 width = FONT_WIDTH (font);
19661 ascent = 1;
19662 descent = 0;
19665 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19667 /* Relative composition with or without
19668 alternate chars. */
19669 left = (leftmost + rightmost - width) / 2;
19670 btm = - descent + boff;
19671 if (font_info && font_info->relative_compose
19672 && (! CHAR_TABLE_P (Vignore_relative_composition)
19673 || NILP (Faref (Vignore_relative_composition,
19674 make_number (ch)))))
19677 if (- descent >= font_info->relative_compose)
19678 /* One extra pixel between two glyphs. */
19679 btm = highest + 1;
19680 else if (ascent <= 0)
19681 /* One extra pixel between two glyphs. */
19682 btm = lowest - 1 - ascent - descent;
19685 else
19687 /* A composition rule is specified by an integer
19688 value that encodes global and new reference
19689 points (GREF and NREF). GREF and NREF are
19690 specified by numbers as below:
19692 0---1---2 -- ascent
19696 9--10--11 -- center
19698 ---3---4---5--- baseline
19700 6---7---8 -- descent
19702 int rule = COMPOSITION_RULE (cmp, i);
19703 int gref, nref, grefx, grefy, nrefx, nrefy;
19705 COMPOSITION_DECODE_RULE (rule, gref, nref);
19706 grefx = gref % 3, nrefx = nref % 3;
19707 grefy = gref / 3, nrefy = nref / 3;
19709 left = (leftmost
19710 + grefx * (rightmost - leftmost) / 2
19711 - nrefx * width / 2);
19712 btm = ((grefy == 0 ? highest
19713 : grefy == 1 ? 0
19714 : grefy == 2 ? lowest
19715 : (highest + lowest) / 2)
19716 - (nrefy == 0 ? ascent + descent
19717 : nrefy == 1 ? descent - boff
19718 : nrefy == 2 ? 0
19719 : (ascent + descent) / 2));
19722 cmp->offsets[i * 2] = left;
19723 cmp->offsets[i * 2 + 1] = btm + descent;
19725 /* Update the bounding box of the overall glyphs. */
19726 right = left + width;
19727 top = btm + descent + ascent;
19728 if (left < leftmost)
19729 leftmost = left;
19730 if (right > rightmost)
19731 rightmost = right;
19732 if (top > highest)
19733 highest = top;
19734 if (btm < lowest)
19735 lowest = btm;
19738 /* If there are glyphs whose x-offsets are negative,
19739 shift all glyphs to the right and make all x-offsets
19740 non-negative. */
19741 if (leftmost < 0)
19743 for (i = 0; i < cmp->glyph_len; i++)
19744 cmp->offsets[i * 2] -= leftmost;
19745 rightmost -= leftmost;
19748 cmp->pixel_width = rightmost;
19749 cmp->ascent = highest;
19750 cmp->descent = - lowest;
19751 if (cmp->ascent < font_ascent)
19752 cmp->ascent = font_ascent;
19753 if (cmp->descent < font_descent)
19754 cmp->descent = font_descent;
19757 it->pixel_width = cmp->pixel_width;
19758 it->ascent = it->phys_ascent = cmp->ascent;
19759 it->descent = it->phys_descent = cmp->descent;
19761 if (face->box != FACE_NO_BOX)
19763 int thick = face->box_line_width;
19765 if (thick > 0)
19767 it->ascent += thick;
19768 it->descent += thick;
19770 else
19771 thick = - thick;
19773 if (it->start_of_box_run_p)
19774 it->pixel_width += thick;
19775 if (it->end_of_box_run_p)
19776 it->pixel_width += thick;
19779 /* If face has an overline, add the height of the overline
19780 (1 pixel) and a 1 pixel margin to the character height. */
19781 if (face->overline_p)
19782 it->ascent += 2;
19784 take_vertical_position_into_account (it);
19786 if (it->glyph_row)
19787 append_composite_glyph (it);
19789 else if (it->what == IT_IMAGE)
19790 produce_image_glyph (it);
19791 else if (it->what == IT_STRETCH)
19792 produce_stretch_glyph (it);
19794 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19795 because this isn't true for images with `:ascent 100'. */
19796 xassert (it->ascent >= 0 && it->descent >= 0);
19797 if (it->area == TEXT_AREA)
19798 it->current_x += it->pixel_width;
19800 if (extra_line_spacing > 0)
19802 it->descent += extra_line_spacing;
19803 if (extra_line_spacing > it->max_extra_line_spacing)
19804 it->max_extra_line_spacing = extra_line_spacing;
19807 it->max_ascent = max (it->max_ascent, it->ascent);
19808 it->max_descent = max (it->max_descent, it->descent);
19809 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19810 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19813 /* EXPORT for RIF:
19814 Output LEN glyphs starting at START at the nominal cursor position.
19815 Advance the nominal cursor over the text. The global variable
19816 updated_window contains the window being updated, updated_row is
19817 the glyph row being updated, and updated_area is the area of that
19818 row being updated. */
19820 void
19821 x_write_glyphs (start, len)
19822 struct glyph *start;
19823 int len;
19825 int x, hpos;
19827 xassert (updated_window && updated_row);
19828 BLOCK_INPUT;
19830 /* Write glyphs. */
19832 hpos = start - updated_row->glyphs[updated_area];
19833 x = draw_glyphs (updated_window, output_cursor.x,
19834 updated_row, updated_area,
19835 hpos, hpos + len,
19836 DRAW_NORMAL_TEXT, 0);
19838 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19839 if (updated_area == TEXT_AREA
19840 && updated_window->phys_cursor_on_p
19841 && updated_window->phys_cursor.vpos == output_cursor.vpos
19842 && updated_window->phys_cursor.hpos >= hpos
19843 && updated_window->phys_cursor.hpos < hpos + len)
19844 updated_window->phys_cursor_on_p = 0;
19846 UNBLOCK_INPUT;
19848 /* Advance the output cursor. */
19849 output_cursor.hpos += len;
19850 output_cursor.x = x;
19854 /* EXPORT for RIF:
19855 Insert LEN glyphs from START at the nominal cursor position. */
19857 void
19858 x_insert_glyphs (start, len)
19859 struct glyph *start;
19860 int len;
19862 struct frame *f;
19863 struct window *w;
19864 int line_height, shift_by_width, shifted_region_width;
19865 struct glyph_row *row;
19866 struct glyph *glyph;
19867 int frame_x, frame_y, hpos;
19869 xassert (updated_window && updated_row);
19870 BLOCK_INPUT;
19871 w = updated_window;
19872 f = XFRAME (WINDOW_FRAME (w));
19874 /* Get the height of the line we are in. */
19875 row = updated_row;
19876 line_height = row->height;
19878 /* Get the width of the glyphs to insert. */
19879 shift_by_width = 0;
19880 for (glyph = start; glyph < start + len; ++glyph)
19881 shift_by_width += glyph->pixel_width;
19883 /* Get the width of the region to shift right. */
19884 shifted_region_width = (window_box_width (w, updated_area)
19885 - output_cursor.x
19886 - shift_by_width);
19888 /* Shift right. */
19889 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19890 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19892 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19893 line_height, shift_by_width);
19895 /* Write the glyphs. */
19896 hpos = start - row->glyphs[updated_area];
19897 draw_glyphs (w, output_cursor.x, row, updated_area,
19898 hpos, hpos + len,
19899 DRAW_NORMAL_TEXT, 0);
19901 /* Advance the output cursor. */
19902 output_cursor.hpos += len;
19903 output_cursor.x += shift_by_width;
19904 UNBLOCK_INPUT;
19908 /* EXPORT for RIF:
19909 Erase the current text line from the nominal cursor position
19910 (inclusive) to pixel column TO_X (exclusive). The idea is that
19911 everything from TO_X onward is already erased.
19913 TO_X is a pixel position relative to updated_area of
19914 updated_window. TO_X == -1 means clear to the end of this area. */
19916 void
19917 x_clear_end_of_line (to_x)
19918 int to_x;
19920 struct frame *f;
19921 struct window *w = updated_window;
19922 int max_x, min_y, max_y;
19923 int from_x, from_y, to_y;
19925 xassert (updated_window && updated_row);
19926 f = XFRAME (w->frame);
19928 if (updated_row->full_width_p)
19929 max_x = WINDOW_TOTAL_WIDTH (w);
19930 else
19931 max_x = window_box_width (w, updated_area);
19932 max_y = window_text_bottom_y (w);
19934 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19935 of window. For TO_X > 0, truncate to end of drawing area. */
19936 if (to_x == 0)
19937 return;
19938 else if (to_x < 0)
19939 to_x = max_x;
19940 else
19941 to_x = min (to_x, max_x);
19943 to_y = min (max_y, output_cursor.y + updated_row->height);
19945 /* Notice if the cursor will be cleared by this operation. */
19946 if (!updated_row->full_width_p)
19947 notice_overwritten_cursor (w, updated_area,
19948 output_cursor.x, -1,
19949 updated_row->y,
19950 MATRIX_ROW_BOTTOM_Y (updated_row));
19952 from_x = output_cursor.x;
19954 /* Translate to frame coordinates. */
19955 if (updated_row->full_width_p)
19957 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19958 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19960 else
19962 int area_left = window_box_left (w, updated_area);
19963 from_x += area_left;
19964 to_x += area_left;
19967 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19968 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19969 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19971 /* Prevent inadvertently clearing to end of the X window. */
19972 if (to_x > from_x && to_y > from_y)
19974 BLOCK_INPUT;
19975 rif->clear_frame_area (f, from_x, from_y,
19976 to_x - from_x, to_y - from_y);
19977 UNBLOCK_INPUT;
19981 #endif /* HAVE_WINDOW_SYSTEM */
19985 /***********************************************************************
19986 Cursor types
19987 ***********************************************************************/
19989 /* Value is the internal representation of the specified cursor type
19990 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19991 of the bar cursor. */
19993 static enum text_cursor_kinds
19994 get_specified_cursor_type (arg, width)
19995 Lisp_Object arg;
19996 int *width;
19998 enum text_cursor_kinds type;
20000 if (NILP (arg))
20001 return NO_CURSOR;
20003 if (EQ (arg, Qbox))
20004 return FILLED_BOX_CURSOR;
20006 if (EQ (arg, Qhollow))
20007 return HOLLOW_BOX_CURSOR;
20009 if (EQ (arg, Qbar))
20011 *width = 2;
20012 return BAR_CURSOR;
20015 if (CONSP (arg)
20016 && EQ (XCAR (arg), Qbar)
20017 && INTEGERP (XCDR (arg))
20018 && XINT (XCDR (arg)) >= 0)
20020 *width = XINT (XCDR (arg));
20021 return BAR_CURSOR;
20024 if (EQ (arg, Qhbar))
20026 *width = 2;
20027 return HBAR_CURSOR;
20030 if (CONSP (arg)
20031 && EQ (XCAR (arg), Qhbar)
20032 && INTEGERP (XCDR (arg))
20033 && XINT (XCDR (arg)) >= 0)
20035 *width = XINT (XCDR (arg));
20036 return HBAR_CURSOR;
20039 /* Treat anything unknown as "hollow box cursor".
20040 It was bad to signal an error; people have trouble fixing
20041 .Xdefaults with Emacs, when it has something bad in it. */
20042 type = HOLLOW_BOX_CURSOR;
20044 return type;
20047 /* Set the default cursor types for specified frame. */
20048 void
20049 set_frame_cursor_types (f, arg)
20050 struct frame *f;
20051 Lisp_Object arg;
20053 int width;
20054 Lisp_Object tem;
20056 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20057 FRAME_CURSOR_WIDTH (f) = width;
20059 /* By default, set up the blink-off state depending on the on-state. */
20061 tem = Fassoc (arg, Vblink_cursor_alist);
20062 if (!NILP (tem))
20064 FRAME_BLINK_OFF_CURSOR (f)
20065 = get_specified_cursor_type (XCDR (tem), &width);
20066 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20068 else
20069 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20073 /* Return the cursor we want to be displayed in window W. Return
20074 width of bar/hbar cursor through WIDTH arg. Return with
20075 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20076 (i.e. if the `system caret' should track this cursor).
20078 In a mini-buffer window, we want the cursor only to appear if we
20079 are reading input from this window. For the selected window, we
20080 want the cursor type given by the frame parameter or buffer local
20081 setting of cursor-type. If explicitly marked off, draw no cursor.
20082 In all other cases, we want a hollow box cursor. */
20084 static enum text_cursor_kinds
20085 get_window_cursor_type (w, glyph, width, active_cursor)
20086 struct window *w;
20087 struct glyph *glyph;
20088 int *width;
20089 int *active_cursor;
20091 struct frame *f = XFRAME (w->frame);
20092 struct buffer *b = XBUFFER (w->buffer);
20093 int cursor_type = DEFAULT_CURSOR;
20094 Lisp_Object alt_cursor;
20095 int non_selected = 0;
20097 *active_cursor = 1;
20099 /* Echo area */
20100 if (cursor_in_echo_area
20101 && FRAME_HAS_MINIBUF_P (f)
20102 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20104 if (w == XWINDOW (echo_area_window))
20106 *width = FRAME_CURSOR_WIDTH (f);
20107 return FRAME_DESIRED_CURSOR (f);
20110 *active_cursor = 0;
20111 non_selected = 1;
20114 /* Nonselected window or nonselected frame. */
20115 else if (w != XWINDOW (f->selected_window)
20116 #ifdef HAVE_WINDOW_SYSTEM
20117 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20118 #endif
20121 *active_cursor = 0;
20123 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20124 return NO_CURSOR;
20126 non_selected = 1;
20129 /* Never display a cursor in a window in which cursor-type is nil. */
20130 if (NILP (b->cursor_type))
20131 return NO_CURSOR;
20133 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20134 if (non_selected)
20136 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20137 return get_specified_cursor_type (alt_cursor, width);
20140 /* Get the normal cursor type for this window. */
20141 if (EQ (b->cursor_type, Qt))
20143 cursor_type = FRAME_DESIRED_CURSOR (f);
20144 *width = FRAME_CURSOR_WIDTH (f);
20146 else
20147 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20149 /* Use normal cursor if not blinked off. */
20150 if (!w->cursor_off_p)
20152 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20153 if (cursor_type == FILLED_BOX_CURSOR)
20154 cursor_type = HOLLOW_BOX_CURSOR;
20156 return cursor_type;
20159 /* Cursor is blinked off, so determine how to "toggle" it. */
20161 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20162 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20163 return get_specified_cursor_type (XCDR (alt_cursor), width);
20165 /* Then see if frame has specified a specific blink off cursor type. */
20166 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20168 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20169 return FRAME_BLINK_OFF_CURSOR (f);
20172 #if 0
20173 /* Some people liked having a permanently visible blinking cursor,
20174 while others had very strong opinions against it. So it was
20175 decided to remove it. KFS 2003-09-03 */
20177 /* Finally perform built-in cursor blinking:
20178 filled box <-> hollow box
20179 wide [h]bar <-> narrow [h]bar
20180 narrow [h]bar <-> no cursor
20181 other type <-> no cursor */
20183 if (cursor_type == FILLED_BOX_CURSOR)
20184 return HOLLOW_BOX_CURSOR;
20186 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20188 *width = 1;
20189 return cursor_type;
20191 #endif
20193 return NO_CURSOR;
20197 #ifdef HAVE_WINDOW_SYSTEM
20199 /* Notice when the text cursor of window W has been completely
20200 overwritten by a drawing operation that outputs glyphs in AREA
20201 starting at X0 and ending at X1 in the line starting at Y0 and
20202 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20203 the rest of the line after X0 has been written. Y coordinates
20204 are window-relative. */
20206 static void
20207 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20208 struct window *w;
20209 enum glyph_row_area area;
20210 int x0, y0, x1, y1;
20212 int cx0, cx1, cy0, cy1;
20213 struct glyph_row *row;
20215 if (!w->phys_cursor_on_p)
20216 return;
20217 if (area != TEXT_AREA)
20218 return;
20220 if (w->phys_cursor.vpos < 0
20221 || w->phys_cursor.vpos >= w->current_matrix->nrows
20222 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20223 !(row->enabled_p && row->displays_text_p)))
20224 return;
20226 if (row->cursor_in_fringe_p)
20228 row->cursor_in_fringe_p = 0;
20229 draw_fringe_bitmap (w, row, 0);
20230 w->phys_cursor_on_p = 0;
20231 return;
20234 cx0 = w->phys_cursor.x;
20235 cx1 = cx0 + w->phys_cursor_width;
20236 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20237 return;
20239 /* The cursor image will be completely removed from the
20240 screen if the output area intersects the cursor area in
20241 y-direction. When we draw in [y0 y1[, and some part of
20242 the cursor is at y < y0, that part must have been drawn
20243 before. When scrolling, the cursor is erased before
20244 actually scrolling, so we don't come here. When not
20245 scrolling, the rows above the old cursor row must have
20246 changed, and in this case these rows must have written
20247 over the cursor image.
20249 Likewise if part of the cursor is below y1, with the
20250 exception of the cursor being in the first blank row at
20251 the buffer and window end because update_text_area
20252 doesn't draw that row. (Except when it does, but
20253 that's handled in update_text_area.) */
20255 cy0 = w->phys_cursor.y;
20256 cy1 = cy0 + w->phys_cursor_height;
20257 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20258 return;
20260 w->phys_cursor_on_p = 0;
20263 #endif /* HAVE_WINDOW_SYSTEM */
20266 /************************************************************************
20267 Mouse Face
20268 ************************************************************************/
20270 #ifdef HAVE_WINDOW_SYSTEM
20272 /* EXPORT for RIF:
20273 Fix the display of area AREA of overlapping row ROW in window W. */
20275 void
20276 x_fix_overlapping_area (w, row, area)
20277 struct window *w;
20278 struct glyph_row *row;
20279 enum glyph_row_area area;
20281 int i, x;
20283 BLOCK_INPUT;
20285 x = 0;
20286 for (i = 0; i < row->used[area];)
20288 if (row->glyphs[area][i].overlaps_vertically_p)
20290 int start = i, start_x = x;
20294 x += row->glyphs[area][i].pixel_width;
20295 ++i;
20297 while (i < row->used[area]
20298 && row->glyphs[area][i].overlaps_vertically_p);
20300 draw_glyphs (w, start_x, row, area,
20301 start, i,
20302 DRAW_NORMAL_TEXT, 1);
20304 else
20306 x += row->glyphs[area][i].pixel_width;
20307 ++i;
20311 UNBLOCK_INPUT;
20315 /* EXPORT:
20316 Draw the cursor glyph of window W in glyph row ROW. See the
20317 comment of draw_glyphs for the meaning of HL. */
20319 void
20320 draw_phys_cursor_glyph (w, row, hl)
20321 struct window *w;
20322 struct glyph_row *row;
20323 enum draw_glyphs_face hl;
20325 /* If cursor hpos is out of bounds, don't draw garbage. This can
20326 happen in mini-buffer windows when switching between echo area
20327 glyphs and mini-buffer. */
20328 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20330 int on_p = w->phys_cursor_on_p;
20331 int x1;
20332 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20333 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20334 hl, 0);
20335 w->phys_cursor_on_p = on_p;
20337 if (hl == DRAW_CURSOR)
20338 w->phys_cursor_width = x1 - w->phys_cursor.x;
20339 /* When we erase the cursor, and ROW is overlapped by other
20340 rows, make sure that these overlapping parts of other rows
20341 are redrawn. */
20342 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20344 if (row > w->current_matrix->rows
20345 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20346 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20348 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20349 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20350 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20356 /* EXPORT:
20357 Erase the image of a cursor of window W from the screen. */
20359 void
20360 erase_phys_cursor (w)
20361 struct window *w;
20363 struct frame *f = XFRAME (w->frame);
20364 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20365 int hpos = w->phys_cursor.hpos;
20366 int vpos = w->phys_cursor.vpos;
20367 int mouse_face_here_p = 0;
20368 struct glyph_matrix *active_glyphs = w->current_matrix;
20369 struct glyph_row *cursor_row;
20370 struct glyph *cursor_glyph;
20371 enum draw_glyphs_face hl;
20373 /* No cursor displayed or row invalidated => nothing to do on the
20374 screen. */
20375 if (w->phys_cursor_type == NO_CURSOR)
20376 goto mark_cursor_off;
20378 /* VPOS >= active_glyphs->nrows means that window has been resized.
20379 Don't bother to erase the cursor. */
20380 if (vpos >= active_glyphs->nrows)
20381 goto mark_cursor_off;
20383 /* If row containing cursor is marked invalid, there is nothing we
20384 can do. */
20385 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20386 if (!cursor_row->enabled_p)
20387 goto mark_cursor_off;
20389 /* If line spacing is > 0, old cursor may only be partially visible in
20390 window after split-window. So adjust visible height. */
20391 cursor_row->visible_height = min (cursor_row->visible_height,
20392 window_text_bottom_y (w) - cursor_row->y);
20394 /* If row is completely invisible, don't attempt to delete a cursor which
20395 isn't there. This can happen if cursor is at top of a window, and
20396 we switch to a buffer with a header line in that window. */
20397 if (cursor_row->visible_height <= 0)
20398 goto mark_cursor_off;
20400 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20401 if (cursor_row->cursor_in_fringe_p)
20403 cursor_row->cursor_in_fringe_p = 0;
20404 draw_fringe_bitmap (w, cursor_row, 0);
20405 goto mark_cursor_off;
20408 /* This can happen when the new row is shorter than the old one.
20409 In this case, either draw_glyphs or clear_end_of_line
20410 should have cleared the cursor. Note that we wouldn't be
20411 able to erase the cursor in this case because we don't have a
20412 cursor glyph at hand. */
20413 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20414 goto mark_cursor_off;
20416 /* If the cursor is in the mouse face area, redisplay that when
20417 we clear the cursor. */
20418 if (! NILP (dpyinfo->mouse_face_window)
20419 && w == XWINDOW (dpyinfo->mouse_face_window)
20420 && (vpos > dpyinfo->mouse_face_beg_row
20421 || (vpos == dpyinfo->mouse_face_beg_row
20422 && hpos >= dpyinfo->mouse_face_beg_col))
20423 && (vpos < dpyinfo->mouse_face_end_row
20424 || (vpos == dpyinfo->mouse_face_end_row
20425 && hpos < dpyinfo->mouse_face_end_col))
20426 /* Don't redraw the cursor's spot in mouse face if it is at the
20427 end of a line (on a newline). The cursor appears there, but
20428 mouse highlighting does not. */
20429 && cursor_row->used[TEXT_AREA] > hpos)
20430 mouse_face_here_p = 1;
20432 /* Maybe clear the display under the cursor. */
20433 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20435 int x, y;
20436 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20437 int width;
20439 cursor_glyph = get_phys_cursor_glyph (w);
20440 if (cursor_glyph == NULL)
20441 goto mark_cursor_off;
20443 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20444 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20445 width = min (cursor_glyph->pixel_width,
20446 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20448 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20451 /* Erase the cursor by redrawing the character underneath it. */
20452 if (mouse_face_here_p)
20453 hl = DRAW_MOUSE_FACE;
20454 else
20455 hl = DRAW_NORMAL_TEXT;
20456 draw_phys_cursor_glyph (w, cursor_row, hl);
20458 mark_cursor_off:
20459 w->phys_cursor_on_p = 0;
20460 w->phys_cursor_type = NO_CURSOR;
20464 /* EXPORT:
20465 Display or clear cursor of window W. If ON is zero, clear the
20466 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20467 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20469 void
20470 display_and_set_cursor (w, on, hpos, vpos, x, y)
20471 struct window *w;
20472 int on, hpos, vpos, x, y;
20474 struct frame *f = XFRAME (w->frame);
20475 int new_cursor_type;
20476 int new_cursor_width;
20477 int active_cursor;
20478 struct glyph_row *glyph_row;
20479 struct glyph *glyph;
20481 /* This is pointless on invisible frames, and dangerous on garbaged
20482 windows and frames; in the latter case, the frame or window may
20483 be in the midst of changing its size, and x and y may be off the
20484 window. */
20485 if (! FRAME_VISIBLE_P (f)
20486 || FRAME_GARBAGED_P (f)
20487 || vpos >= w->current_matrix->nrows
20488 || hpos >= w->current_matrix->matrix_w)
20489 return;
20491 /* If cursor is off and we want it off, return quickly. */
20492 if (!on && !w->phys_cursor_on_p)
20493 return;
20495 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20496 /* If cursor row is not enabled, we don't really know where to
20497 display the cursor. */
20498 if (!glyph_row->enabled_p)
20500 w->phys_cursor_on_p = 0;
20501 return;
20504 glyph = NULL;
20505 if (!glyph_row->exact_window_width_line_p
20506 || hpos < glyph_row->used[TEXT_AREA])
20507 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20509 xassert (interrupt_input_blocked);
20511 /* Set new_cursor_type to the cursor we want to be displayed. */
20512 new_cursor_type = get_window_cursor_type (w, glyph,
20513 &new_cursor_width, &active_cursor);
20515 /* If cursor is currently being shown and we don't want it to be or
20516 it is in the wrong place, or the cursor type is not what we want,
20517 erase it. */
20518 if (w->phys_cursor_on_p
20519 && (!on
20520 || w->phys_cursor.x != x
20521 || w->phys_cursor.y != y
20522 || new_cursor_type != w->phys_cursor_type
20523 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20524 && new_cursor_width != w->phys_cursor_width)))
20525 erase_phys_cursor (w);
20527 /* Don't check phys_cursor_on_p here because that flag is only set
20528 to zero in some cases where we know that the cursor has been
20529 completely erased, to avoid the extra work of erasing the cursor
20530 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20531 still not be visible, or it has only been partly erased. */
20532 if (on)
20534 w->phys_cursor_ascent = glyph_row->ascent;
20535 w->phys_cursor_height = glyph_row->height;
20537 /* Set phys_cursor_.* before x_draw_.* is called because some
20538 of them may need the information. */
20539 w->phys_cursor.x = x;
20540 w->phys_cursor.y = glyph_row->y;
20541 w->phys_cursor.hpos = hpos;
20542 w->phys_cursor.vpos = vpos;
20545 rif->draw_window_cursor (w, glyph_row, x, y,
20546 new_cursor_type, new_cursor_width,
20547 on, active_cursor);
20551 /* Switch the display of W's cursor on or off, according to the value
20552 of ON. */
20554 static void
20555 update_window_cursor (w, on)
20556 struct window *w;
20557 int on;
20559 /* Don't update cursor in windows whose frame is in the process
20560 of being deleted. */
20561 if (w->current_matrix)
20563 BLOCK_INPUT;
20564 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20565 w->phys_cursor.x, w->phys_cursor.y);
20566 UNBLOCK_INPUT;
20571 /* Call update_window_cursor with parameter ON_P on all leaf windows
20572 in the window tree rooted at W. */
20574 static void
20575 update_cursor_in_window_tree (w, on_p)
20576 struct window *w;
20577 int on_p;
20579 while (w)
20581 if (!NILP (w->hchild))
20582 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20583 else if (!NILP (w->vchild))
20584 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20585 else
20586 update_window_cursor (w, on_p);
20588 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20593 /* EXPORT:
20594 Display the cursor on window W, or clear it, according to ON_P.
20595 Don't change the cursor's position. */
20597 void
20598 x_update_cursor (f, on_p)
20599 struct frame *f;
20600 int on_p;
20602 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20606 /* EXPORT:
20607 Clear the cursor of window W to background color, and mark the
20608 cursor as not shown. This is used when the text where the cursor
20609 is is about to be rewritten. */
20611 void
20612 x_clear_cursor (w)
20613 struct window *w;
20615 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20616 update_window_cursor (w, 0);
20620 /* EXPORT:
20621 Display the active region described by mouse_face_* according to DRAW. */
20623 void
20624 show_mouse_face (dpyinfo, draw)
20625 Display_Info *dpyinfo;
20626 enum draw_glyphs_face draw;
20628 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20629 struct frame *f = XFRAME (WINDOW_FRAME (w));
20631 if (/* If window is in the process of being destroyed, don't bother
20632 to do anything. */
20633 w->current_matrix != NULL
20634 /* Don't update mouse highlight if hidden */
20635 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20636 /* Recognize when we are called to operate on rows that don't exist
20637 anymore. This can happen when a window is split. */
20638 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20640 int phys_cursor_on_p = w->phys_cursor_on_p;
20641 struct glyph_row *row, *first, *last;
20643 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20644 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20646 for (row = first; row <= last && row->enabled_p; ++row)
20648 int start_hpos, end_hpos, start_x;
20650 /* For all but the first row, the highlight starts at column 0. */
20651 if (row == first)
20653 start_hpos = dpyinfo->mouse_face_beg_col;
20654 start_x = dpyinfo->mouse_face_beg_x;
20656 else
20658 start_hpos = 0;
20659 start_x = 0;
20662 if (row == last)
20663 end_hpos = dpyinfo->mouse_face_end_col;
20664 else
20665 end_hpos = row->used[TEXT_AREA];
20667 if (end_hpos > start_hpos)
20669 draw_glyphs (w, start_x, row, TEXT_AREA,
20670 start_hpos, end_hpos,
20671 draw, 0);
20673 row->mouse_face_p
20674 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20678 /* When we've written over the cursor, arrange for it to
20679 be displayed again. */
20680 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20682 BLOCK_INPUT;
20683 display_and_set_cursor (w, 1,
20684 w->phys_cursor.hpos, w->phys_cursor.vpos,
20685 w->phys_cursor.x, w->phys_cursor.y);
20686 UNBLOCK_INPUT;
20690 /* Change the mouse cursor. */
20691 if (draw == DRAW_NORMAL_TEXT)
20692 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20693 else if (draw == DRAW_MOUSE_FACE)
20694 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20695 else
20696 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20699 /* EXPORT:
20700 Clear out the mouse-highlighted active region.
20701 Redraw it un-highlighted first. Value is non-zero if mouse
20702 face was actually drawn unhighlighted. */
20705 clear_mouse_face (dpyinfo)
20706 Display_Info *dpyinfo;
20708 int cleared = 0;
20710 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20712 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20713 cleared = 1;
20716 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20717 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20718 dpyinfo->mouse_face_window = Qnil;
20719 dpyinfo->mouse_face_overlay = Qnil;
20720 return cleared;
20724 /* EXPORT:
20725 Non-zero if physical cursor of window W is within mouse face. */
20728 cursor_in_mouse_face_p (w)
20729 struct window *w;
20731 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20732 int in_mouse_face = 0;
20734 if (WINDOWP (dpyinfo->mouse_face_window)
20735 && XWINDOW (dpyinfo->mouse_face_window) == w)
20737 int hpos = w->phys_cursor.hpos;
20738 int vpos = w->phys_cursor.vpos;
20740 if (vpos >= dpyinfo->mouse_face_beg_row
20741 && vpos <= dpyinfo->mouse_face_end_row
20742 && (vpos > dpyinfo->mouse_face_beg_row
20743 || hpos >= dpyinfo->mouse_face_beg_col)
20744 && (vpos < dpyinfo->mouse_face_end_row
20745 || hpos < dpyinfo->mouse_face_end_col
20746 || dpyinfo->mouse_face_past_end))
20747 in_mouse_face = 1;
20750 return in_mouse_face;
20756 /* Find the glyph matrix position of buffer position CHARPOS in window
20757 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20758 current glyphs must be up to date. If CHARPOS is above window
20759 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20760 of last line in W. In the row containing CHARPOS, stop before glyphs
20761 having STOP as object. */
20763 #if 1 /* This is a version of fast_find_position that's more correct
20764 in the presence of hscrolling, for example. I didn't install
20765 it right away because the problem fixed is minor, it failed
20766 in 20.x as well, and I think it's too risky to install
20767 so near the release of 21.1. 2001-09-25 gerd. */
20769 static int
20770 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20771 struct window *w;
20772 int charpos;
20773 int *hpos, *vpos, *x, *y;
20774 Lisp_Object stop;
20776 struct glyph_row *row, *first;
20777 struct glyph *glyph, *end;
20778 int past_end = 0;
20780 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20781 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20783 *x = first->x;
20784 *y = first->y;
20785 *hpos = 0;
20786 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20787 return 1;
20790 row = row_containing_pos (w, charpos, first, NULL, 0);
20791 if (row == NULL)
20793 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20794 past_end = 1;
20797 /* If whole rows or last part of a row came from a display overlay,
20798 row_containing_pos will skip over such rows because their end pos
20799 equals the start pos of the overlay or interval.
20801 Move back if we have a STOP object and previous row's
20802 end glyph came from STOP. */
20803 if (!NILP (stop))
20805 struct glyph_row *prev;
20806 while ((prev = row - 1, prev >= first)
20807 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20808 && prev->used[TEXT_AREA] > 0)
20810 struct glyph *beg = prev->glyphs[TEXT_AREA];
20811 glyph = beg + prev->used[TEXT_AREA];
20812 while (--glyph >= beg
20813 && INTEGERP (glyph->object));
20814 if (glyph < beg
20815 || !EQ (stop, glyph->object))
20816 break;
20817 row = prev;
20821 *x = row->x;
20822 *y = row->y;
20823 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20825 glyph = row->glyphs[TEXT_AREA];
20826 end = glyph + row->used[TEXT_AREA];
20828 /* Skip over glyphs not having an object at the start of the row.
20829 These are special glyphs like truncation marks on terminal
20830 frames. */
20831 if (row->displays_text_p)
20832 while (glyph < end
20833 && INTEGERP (glyph->object)
20834 && !EQ (stop, glyph->object)
20835 && glyph->charpos < 0)
20837 *x += glyph->pixel_width;
20838 ++glyph;
20841 while (glyph < end
20842 && !INTEGERP (glyph->object)
20843 && !EQ (stop, glyph->object)
20844 && (!BUFFERP (glyph->object)
20845 || glyph->charpos < charpos))
20847 *x += glyph->pixel_width;
20848 ++glyph;
20851 *hpos = glyph - row->glyphs[TEXT_AREA];
20852 return !past_end;
20855 #else /* not 1 */
20857 static int
20858 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20859 struct window *w;
20860 int pos;
20861 int *hpos, *vpos, *x, *y;
20862 Lisp_Object stop;
20864 int i;
20865 int lastcol;
20866 int maybe_next_line_p = 0;
20867 int line_start_position;
20868 int yb = window_text_bottom_y (w);
20869 struct glyph_row *row, *best_row;
20870 int row_vpos, best_row_vpos;
20871 int current_x;
20873 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20874 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20876 while (row->y < yb)
20878 if (row->used[TEXT_AREA])
20879 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20880 else
20881 line_start_position = 0;
20883 if (line_start_position > pos)
20884 break;
20885 /* If the position sought is the end of the buffer,
20886 don't include the blank lines at the bottom of the window. */
20887 else if (line_start_position == pos
20888 && pos == BUF_ZV (XBUFFER (w->buffer)))
20890 maybe_next_line_p = 1;
20891 break;
20893 else if (line_start_position > 0)
20895 best_row = row;
20896 best_row_vpos = row_vpos;
20899 if (row->y + row->height >= yb)
20900 break;
20902 ++row;
20903 ++row_vpos;
20906 /* Find the right column within BEST_ROW. */
20907 lastcol = 0;
20908 current_x = best_row->x;
20909 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20911 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20912 int charpos = glyph->charpos;
20914 if (BUFFERP (glyph->object))
20916 if (charpos == pos)
20918 *hpos = i;
20919 *vpos = best_row_vpos;
20920 *x = current_x;
20921 *y = best_row->y;
20922 return 1;
20924 else if (charpos > pos)
20925 break;
20927 else if (EQ (glyph->object, stop))
20928 break;
20930 if (charpos > 0)
20931 lastcol = i;
20932 current_x += glyph->pixel_width;
20935 /* If we're looking for the end of the buffer,
20936 and we didn't find it in the line we scanned,
20937 use the start of the following line. */
20938 if (maybe_next_line_p)
20940 ++best_row;
20941 ++best_row_vpos;
20942 lastcol = 0;
20943 current_x = best_row->x;
20946 *vpos = best_row_vpos;
20947 *hpos = lastcol + 1;
20948 *x = current_x;
20949 *y = best_row->y;
20950 return 0;
20953 #endif /* not 1 */
20956 /* Find the position of the glyph for position POS in OBJECT in
20957 window W's current matrix, and return in *X, *Y the pixel
20958 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20960 RIGHT_P non-zero means return the position of the right edge of the
20961 glyph, RIGHT_P zero means return the left edge position.
20963 If no glyph for POS exists in the matrix, return the position of
20964 the glyph with the next smaller position that is in the matrix, if
20965 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20966 exists in the matrix, return the position of the glyph with the
20967 next larger position in OBJECT.
20969 Value is non-zero if a glyph was found. */
20971 static int
20972 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20973 struct window *w;
20974 int pos;
20975 Lisp_Object object;
20976 int *hpos, *vpos, *x, *y;
20977 int right_p;
20979 int yb = window_text_bottom_y (w);
20980 struct glyph_row *r;
20981 struct glyph *best_glyph = NULL;
20982 struct glyph_row *best_row = NULL;
20983 int best_x = 0;
20985 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20986 r->enabled_p && r->y < yb;
20987 ++r)
20989 struct glyph *g = r->glyphs[TEXT_AREA];
20990 struct glyph *e = g + r->used[TEXT_AREA];
20991 int gx;
20993 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20994 if (EQ (g->object, object))
20996 if (g->charpos == pos)
20998 best_glyph = g;
20999 best_x = gx;
21000 best_row = r;
21001 goto found;
21003 else if (best_glyph == NULL
21004 || ((abs (g->charpos - pos)
21005 < abs (best_glyph->charpos - pos))
21006 && (right_p
21007 ? g->charpos < pos
21008 : g->charpos > pos)))
21010 best_glyph = g;
21011 best_x = gx;
21012 best_row = r;
21017 found:
21019 if (best_glyph)
21021 *x = best_x;
21022 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21024 if (right_p)
21026 *x += best_glyph->pixel_width;
21027 ++*hpos;
21030 *y = best_row->y;
21031 *vpos = best_row - w->current_matrix->rows;
21034 return best_glyph != NULL;
21038 /* See if position X, Y is within a hot-spot of an image. */
21040 static int
21041 on_hot_spot_p (hot_spot, x, y)
21042 Lisp_Object hot_spot;
21043 int x, y;
21045 if (!CONSP (hot_spot))
21046 return 0;
21048 if (EQ (XCAR (hot_spot), Qrect))
21050 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21051 Lisp_Object rect = XCDR (hot_spot);
21052 Lisp_Object tem;
21053 if (!CONSP (rect))
21054 return 0;
21055 if (!CONSP (XCAR (rect)))
21056 return 0;
21057 if (!CONSP (XCDR (rect)))
21058 return 0;
21059 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21060 return 0;
21061 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21062 return 0;
21063 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21064 return 0;
21065 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21066 return 0;
21067 return 1;
21069 else if (EQ (XCAR (hot_spot), Qcircle))
21071 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21072 Lisp_Object circ = XCDR (hot_spot);
21073 Lisp_Object lr, lx0, ly0;
21074 if (CONSP (circ)
21075 && CONSP (XCAR (circ))
21076 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21077 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21078 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21080 double r = XFLOATINT (lr);
21081 double dx = XINT (lx0) - x;
21082 double dy = XINT (ly0) - y;
21083 return (dx * dx + dy * dy <= r * r);
21086 else if (EQ (XCAR (hot_spot), Qpoly))
21088 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21089 if (VECTORP (XCDR (hot_spot)))
21091 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21092 Lisp_Object *poly = v->contents;
21093 int n = v->size;
21094 int i;
21095 int inside = 0;
21096 Lisp_Object lx, ly;
21097 int x0, y0;
21099 /* Need an even number of coordinates, and at least 3 edges. */
21100 if (n < 6 || n & 1)
21101 return 0;
21103 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21104 If count is odd, we are inside polygon. Pixels on edges
21105 may or may not be included depending on actual geometry of the
21106 polygon. */
21107 if ((lx = poly[n-2], !INTEGERP (lx))
21108 || (ly = poly[n-1], !INTEGERP (lx)))
21109 return 0;
21110 x0 = XINT (lx), y0 = XINT (ly);
21111 for (i = 0; i < n; i += 2)
21113 int x1 = x0, y1 = y0;
21114 if ((lx = poly[i], !INTEGERP (lx))
21115 || (ly = poly[i+1], !INTEGERP (ly)))
21116 return 0;
21117 x0 = XINT (lx), y0 = XINT (ly);
21119 /* Does this segment cross the X line? */
21120 if (x0 >= x)
21122 if (x1 >= x)
21123 continue;
21125 else if (x1 < x)
21126 continue;
21127 if (y > y0 && y > y1)
21128 continue;
21129 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21130 inside = !inside;
21132 return inside;
21135 /* If we don't understand the format, pretend we're not in the hot-spot. */
21136 return 0;
21139 Lisp_Object
21140 find_hot_spot (map, x, y)
21141 Lisp_Object map;
21142 int x, y;
21144 while (CONSP (map))
21146 if (CONSP (XCAR (map))
21147 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21148 return XCAR (map);
21149 map = XCDR (map);
21152 return Qnil;
21155 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21156 3, 3, 0,
21157 doc: /* Lookup in image map MAP coordinates X and Y.
21158 An image map is an alist where each element has the format (AREA ID PLIST).
21159 An AREA is specified as either a rectangle, a circle, or a polygon:
21160 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21161 pixel coordinates of the upper left and bottom right corners.
21162 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21163 and the radius of the circle; r may be a float or integer.
21164 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21165 vector describes one corner in the polygon.
21166 Returns the alist element for the first matching AREA in MAP. */)
21167 (map, x, y)
21168 Lisp_Object map;
21169 Lisp_Object x, y;
21171 if (NILP (map))
21172 return Qnil;
21174 CHECK_NUMBER (x);
21175 CHECK_NUMBER (y);
21177 return find_hot_spot (map, XINT (x), XINT (y));
21181 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21182 static void
21183 define_frame_cursor1 (f, cursor, pointer)
21184 struct frame *f;
21185 Cursor cursor;
21186 Lisp_Object pointer;
21188 /* Do not change cursor shape while dragging mouse. */
21189 if (!NILP (do_mouse_tracking))
21190 return;
21192 if (!NILP (pointer))
21194 if (EQ (pointer, Qarrow))
21195 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21196 else if (EQ (pointer, Qhand))
21197 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21198 else if (EQ (pointer, Qtext))
21199 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21200 else if (EQ (pointer, intern ("hdrag")))
21201 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21202 #ifdef HAVE_X_WINDOWS
21203 else if (EQ (pointer, intern ("vdrag")))
21204 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21205 #endif
21206 else if (EQ (pointer, intern ("hourglass")))
21207 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21208 else if (EQ (pointer, Qmodeline))
21209 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21210 else
21211 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21214 if (cursor != No_Cursor)
21215 rif->define_frame_cursor (f, cursor);
21218 /* Take proper action when mouse has moved to the mode or header line
21219 or marginal area AREA of window W, x-position X and y-position Y.
21220 X is relative to the start of the text display area of W, so the
21221 width of bitmap areas and scroll bars must be subtracted to get a
21222 position relative to the start of the mode line. */
21224 static void
21225 note_mode_line_or_margin_highlight (w, x, y, area)
21226 struct window *w;
21227 int x, y;
21228 enum window_part area;
21230 struct frame *f = XFRAME (w->frame);
21231 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21232 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21233 Lisp_Object pointer = Qnil;
21234 int charpos, dx, dy, width, height;
21235 Lisp_Object string, object = Qnil;
21236 Lisp_Object pos, help;
21238 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21239 string = mode_line_string (w, area, &x, &y, &charpos,
21240 &object, &dx, &dy, &width, &height);
21241 else
21243 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21244 string = marginal_area_string (w, area, &x, &y, &charpos,
21245 &object, &dx, &dy, &width, &height);
21248 help = Qnil;
21250 if (IMAGEP (object))
21252 Lisp_Object image_map, hotspot;
21253 if ((image_map = Fplist_get (XCDR (object), QCmap),
21254 !NILP (image_map))
21255 && (hotspot = find_hot_spot (image_map, dx, dy),
21256 CONSP (hotspot))
21257 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21259 Lisp_Object area_id, plist;
21261 area_id = XCAR (hotspot);
21262 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21263 If so, we could look for mouse-enter, mouse-leave
21264 properties in PLIST (and do something...). */
21265 hotspot = XCDR (hotspot);
21266 if (CONSP (hotspot)
21267 && (plist = XCAR (hotspot), CONSP (plist)))
21269 pointer = Fplist_get (plist, Qpointer);
21270 if (NILP (pointer))
21271 pointer = Qhand;
21272 help = Fplist_get (plist, Qhelp_echo);
21273 if (!NILP (help))
21275 help_echo_string = help;
21276 /* Is this correct? ++kfs */
21277 XSETWINDOW (help_echo_window, w);
21278 help_echo_object = w->buffer;
21279 help_echo_pos = charpos;
21283 if (NILP (pointer))
21284 pointer = Fplist_get (XCDR (object), QCpointer);
21287 if (STRINGP (string))
21289 pos = make_number (charpos);
21290 /* If we're on a string with `help-echo' text property, arrange
21291 for the help to be displayed. This is done by setting the
21292 global variable help_echo_string to the help string. */
21293 if (NILP (help))
21295 help = Fget_text_property (pos, Qhelp_echo, string);
21296 if (!NILP (help))
21298 help_echo_string = help;
21299 XSETWINDOW (help_echo_window, w);
21300 help_echo_object = string;
21301 help_echo_pos = charpos;
21305 if (NILP (pointer))
21306 pointer = Fget_text_property (pos, Qpointer, string);
21308 /* Change the mouse pointer according to what is under X/Y. */
21309 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21311 Lisp_Object map;
21312 map = Fget_text_property (pos, Qlocal_map, string);
21313 if (!KEYMAPP (map))
21314 map = Fget_text_property (pos, Qkeymap, string);
21315 if (!KEYMAPP (map))
21316 cursor = dpyinfo->vertical_scroll_bar_cursor;
21320 define_frame_cursor1 (f, cursor, pointer);
21324 /* EXPORT:
21325 Take proper action when the mouse has moved to position X, Y on
21326 frame F as regards highlighting characters that have mouse-face
21327 properties. Also de-highlighting chars where the mouse was before.
21328 X and Y can be negative or out of range. */
21330 void
21331 note_mouse_highlight (f, x, y)
21332 struct frame *f;
21333 int x, y;
21335 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21336 enum window_part part;
21337 Lisp_Object window;
21338 struct window *w;
21339 Cursor cursor = No_Cursor;
21340 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21341 struct buffer *b;
21343 /* When a menu is active, don't highlight because this looks odd. */
21344 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21345 if (popup_activated ())
21346 return;
21347 #endif
21349 if (NILP (Vmouse_highlight)
21350 || !f->glyphs_initialized_p)
21351 return;
21353 dpyinfo->mouse_face_mouse_x = x;
21354 dpyinfo->mouse_face_mouse_y = y;
21355 dpyinfo->mouse_face_mouse_frame = f;
21357 if (dpyinfo->mouse_face_defer)
21358 return;
21360 if (gc_in_progress)
21362 dpyinfo->mouse_face_deferred_gc = 1;
21363 return;
21366 /* Which window is that in? */
21367 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21369 /* If we were displaying active text in another window, clear that.
21370 Also clear if we move out of text area in same window. */
21371 if (! EQ (window, dpyinfo->mouse_face_window)
21372 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21373 clear_mouse_face (dpyinfo);
21375 /* Not on a window -> return. */
21376 if (!WINDOWP (window))
21377 return;
21379 /* Reset help_echo_string. It will get recomputed below. */
21380 help_echo_string = Qnil;
21382 /* Convert to window-relative pixel coordinates. */
21383 w = XWINDOW (window);
21384 frame_to_window_pixel_xy (w, &x, &y);
21386 /* Handle tool-bar window differently since it doesn't display a
21387 buffer. */
21388 if (EQ (window, f->tool_bar_window))
21390 note_tool_bar_highlight (f, x, y);
21391 return;
21394 /* Mouse is on the mode, header line or margin? */
21395 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21396 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21398 note_mode_line_or_margin_highlight (w, x, y, part);
21399 return;
21402 if (part == ON_VERTICAL_BORDER)
21403 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21404 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21405 || part == ON_SCROLL_BAR)
21406 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21407 else
21408 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21410 /* Are we in a window whose display is up to date?
21411 And verify the buffer's text has not changed. */
21412 b = XBUFFER (w->buffer);
21413 if (part == ON_TEXT
21414 && EQ (w->window_end_valid, w->buffer)
21415 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21416 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21418 int hpos, vpos, pos, i, dx, dy, area;
21419 struct glyph *glyph;
21420 Lisp_Object object;
21421 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21422 Lisp_Object *overlay_vec = NULL;
21423 int noverlays;
21424 struct buffer *obuf;
21425 int obegv, ozv, same_region;
21427 /* Find the glyph under X/Y. */
21428 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21430 /* Look for :pointer property on image. */
21431 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21433 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21434 if (img != NULL && IMAGEP (img->spec))
21436 Lisp_Object image_map, hotspot;
21437 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21438 !NILP (image_map))
21439 && (hotspot = find_hot_spot (image_map,
21440 glyph->slice.x + dx,
21441 glyph->slice.y + dy),
21442 CONSP (hotspot))
21443 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21445 Lisp_Object area_id, plist;
21447 area_id = XCAR (hotspot);
21448 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21449 If so, we could look for mouse-enter, mouse-leave
21450 properties in PLIST (and do something...). */
21451 hotspot = XCDR (hotspot);
21452 if (CONSP (hotspot)
21453 && (plist = XCAR (hotspot), CONSP (plist)))
21455 pointer = Fplist_get (plist, Qpointer);
21456 if (NILP (pointer))
21457 pointer = Qhand;
21458 help_echo_string = Fplist_get (plist, Qhelp_echo);
21459 if (!NILP (help_echo_string))
21461 help_echo_window = window;
21462 help_echo_object = glyph->object;
21463 help_echo_pos = glyph->charpos;
21467 if (NILP (pointer))
21468 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21472 /* Clear mouse face if X/Y not over text. */
21473 if (glyph == NULL
21474 || area != TEXT_AREA
21475 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21477 if (clear_mouse_face (dpyinfo))
21478 cursor = No_Cursor;
21479 if (NILP (pointer))
21481 if (area != TEXT_AREA)
21482 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21483 else
21484 pointer = Vvoid_text_area_pointer;
21486 goto set_cursor;
21489 pos = glyph->charpos;
21490 object = glyph->object;
21491 if (!STRINGP (object) && !BUFFERP (object))
21492 goto set_cursor;
21494 /* If we get an out-of-range value, return now; avoid an error. */
21495 if (BUFFERP (object) && pos > BUF_Z (b))
21496 goto set_cursor;
21498 /* Make the window's buffer temporarily current for
21499 overlays_at and compute_char_face. */
21500 obuf = current_buffer;
21501 current_buffer = b;
21502 obegv = BEGV;
21503 ozv = ZV;
21504 BEGV = BEG;
21505 ZV = Z;
21507 /* Is this char mouse-active or does it have help-echo? */
21508 position = make_number (pos);
21510 if (BUFFERP (object))
21512 /* Put all the overlays we want in a vector in overlay_vec. */
21513 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21514 /* Sort overlays into increasing priority order. */
21515 noverlays = sort_overlays (overlay_vec, noverlays, w);
21517 else
21518 noverlays = 0;
21520 same_region = (EQ (window, dpyinfo->mouse_face_window)
21521 && vpos >= dpyinfo->mouse_face_beg_row
21522 && vpos <= dpyinfo->mouse_face_end_row
21523 && (vpos > dpyinfo->mouse_face_beg_row
21524 || hpos >= dpyinfo->mouse_face_beg_col)
21525 && (vpos < dpyinfo->mouse_face_end_row
21526 || hpos < dpyinfo->mouse_face_end_col
21527 || dpyinfo->mouse_face_past_end));
21529 if (same_region)
21530 cursor = No_Cursor;
21532 /* Check mouse-face highlighting. */
21533 if (! same_region
21534 /* If there exists an overlay with mouse-face overlapping
21535 the one we are currently highlighting, we have to
21536 check if we enter the overlapping overlay, and then
21537 highlight only that. */
21538 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21539 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21541 /* Find the highest priority overlay that has a mouse-face
21542 property. */
21543 overlay = Qnil;
21544 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21546 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21547 if (!NILP (mouse_face))
21548 overlay = overlay_vec[i];
21551 /* If we're actually highlighting the same overlay as
21552 before, there's no need to do that again. */
21553 if (!NILP (overlay)
21554 && EQ (overlay, dpyinfo->mouse_face_overlay))
21555 goto check_help_echo;
21557 dpyinfo->mouse_face_overlay = overlay;
21559 /* Clear the display of the old active region, if any. */
21560 if (clear_mouse_face (dpyinfo))
21561 cursor = No_Cursor;
21563 /* If no overlay applies, get a text property. */
21564 if (NILP (overlay))
21565 mouse_face = Fget_text_property (position, Qmouse_face, object);
21567 /* Handle the overlay case. */
21568 if (!NILP (overlay))
21570 /* Find the range of text around this char that
21571 should be active. */
21572 Lisp_Object before, after;
21573 int ignore;
21575 before = Foverlay_start (overlay);
21576 after = Foverlay_end (overlay);
21577 /* Record this as the current active region. */
21578 fast_find_position (w, XFASTINT (before),
21579 &dpyinfo->mouse_face_beg_col,
21580 &dpyinfo->mouse_face_beg_row,
21581 &dpyinfo->mouse_face_beg_x,
21582 &dpyinfo->mouse_face_beg_y, Qnil);
21584 dpyinfo->mouse_face_past_end
21585 = !fast_find_position (w, XFASTINT (after),
21586 &dpyinfo->mouse_face_end_col,
21587 &dpyinfo->mouse_face_end_row,
21588 &dpyinfo->mouse_face_end_x,
21589 &dpyinfo->mouse_face_end_y, Qnil);
21590 dpyinfo->mouse_face_window = window;
21592 dpyinfo->mouse_face_face_id
21593 = face_at_buffer_position (w, pos, 0, 0,
21594 &ignore, pos + 1,
21595 !dpyinfo->mouse_face_hidden);
21597 /* Display it as active. */
21598 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21599 cursor = No_Cursor;
21601 /* Handle the text property case. */
21602 else if (!NILP (mouse_face) && BUFFERP (object))
21604 /* Find the range of text around this char that
21605 should be active. */
21606 Lisp_Object before, after, beginning, end;
21607 int ignore;
21609 beginning = Fmarker_position (w->start);
21610 end = make_number (BUF_Z (XBUFFER (object))
21611 - XFASTINT (w->window_end_pos));
21612 before
21613 = Fprevious_single_property_change (make_number (pos + 1),
21614 Qmouse_face,
21615 object, beginning);
21616 after
21617 = Fnext_single_property_change (position, Qmouse_face,
21618 object, end);
21620 /* Record this as the current active region. */
21621 fast_find_position (w, XFASTINT (before),
21622 &dpyinfo->mouse_face_beg_col,
21623 &dpyinfo->mouse_face_beg_row,
21624 &dpyinfo->mouse_face_beg_x,
21625 &dpyinfo->mouse_face_beg_y, Qnil);
21626 dpyinfo->mouse_face_past_end
21627 = !fast_find_position (w, XFASTINT (after),
21628 &dpyinfo->mouse_face_end_col,
21629 &dpyinfo->mouse_face_end_row,
21630 &dpyinfo->mouse_face_end_x,
21631 &dpyinfo->mouse_face_end_y, Qnil);
21632 dpyinfo->mouse_face_window = window;
21634 if (BUFFERP (object))
21635 dpyinfo->mouse_face_face_id
21636 = face_at_buffer_position (w, pos, 0, 0,
21637 &ignore, pos + 1,
21638 !dpyinfo->mouse_face_hidden);
21640 /* Display it as active. */
21641 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21642 cursor = No_Cursor;
21644 else if (!NILP (mouse_face) && STRINGP (object))
21646 Lisp_Object b, e;
21647 int ignore;
21649 b = Fprevious_single_property_change (make_number (pos + 1),
21650 Qmouse_face,
21651 object, Qnil);
21652 e = Fnext_single_property_change (position, Qmouse_face,
21653 object, Qnil);
21654 if (NILP (b))
21655 b = make_number (0);
21656 if (NILP (e))
21657 e = make_number (SCHARS (object) - 1);
21658 fast_find_string_pos (w, XINT (b), object,
21659 &dpyinfo->mouse_face_beg_col,
21660 &dpyinfo->mouse_face_beg_row,
21661 &dpyinfo->mouse_face_beg_x,
21662 &dpyinfo->mouse_face_beg_y, 0);
21663 fast_find_string_pos (w, XINT (e), object,
21664 &dpyinfo->mouse_face_end_col,
21665 &dpyinfo->mouse_face_end_row,
21666 &dpyinfo->mouse_face_end_x,
21667 &dpyinfo->mouse_face_end_y, 1);
21668 dpyinfo->mouse_face_past_end = 0;
21669 dpyinfo->mouse_face_window = window;
21670 dpyinfo->mouse_face_face_id
21671 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21672 glyph->face_id, 1);
21673 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21674 cursor = No_Cursor;
21676 else if (STRINGP (object) && NILP (mouse_face))
21678 /* A string which doesn't have mouse-face, but
21679 the text ``under'' it might have. */
21680 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21681 int start = MATRIX_ROW_START_CHARPOS (r);
21683 pos = string_buffer_position (w, object, start);
21684 if (pos > 0)
21685 mouse_face = get_char_property_and_overlay (make_number (pos),
21686 Qmouse_face,
21687 w->buffer,
21688 &overlay);
21689 if (!NILP (mouse_face) && !NILP (overlay))
21691 Lisp_Object before = Foverlay_start (overlay);
21692 Lisp_Object after = Foverlay_end (overlay);
21693 int ignore;
21695 /* Note that we might not be able to find position
21696 BEFORE in the glyph matrix if the overlay is
21697 entirely covered by a `display' property. In
21698 this case, we overshoot. So let's stop in
21699 the glyph matrix before glyphs for OBJECT. */
21700 fast_find_position (w, XFASTINT (before),
21701 &dpyinfo->mouse_face_beg_col,
21702 &dpyinfo->mouse_face_beg_row,
21703 &dpyinfo->mouse_face_beg_x,
21704 &dpyinfo->mouse_face_beg_y,
21705 object);
21707 dpyinfo->mouse_face_past_end
21708 = !fast_find_position (w, XFASTINT (after),
21709 &dpyinfo->mouse_face_end_col,
21710 &dpyinfo->mouse_face_end_row,
21711 &dpyinfo->mouse_face_end_x,
21712 &dpyinfo->mouse_face_end_y,
21713 Qnil);
21714 dpyinfo->mouse_face_window = window;
21715 dpyinfo->mouse_face_face_id
21716 = face_at_buffer_position (w, pos, 0, 0,
21717 &ignore, pos + 1,
21718 !dpyinfo->mouse_face_hidden);
21720 /* Display it as active. */
21721 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21722 cursor = No_Cursor;
21727 check_help_echo:
21729 /* Look for a `help-echo' property. */
21730 if (NILP (help_echo_string)) {
21731 Lisp_Object help, overlay;
21733 /* Check overlays first. */
21734 help = overlay = Qnil;
21735 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21737 overlay = overlay_vec[i];
21738 help = Foverlay_get (overlay, Qhelp_echo);
21741 if (!NILP (help))
21743 help_echo_string = help;
21744 help_echo_window = window;
21745 help_echo_object = overlay;
21746 help_echo_pos = pos;
21748 else
21750 Lisp_Object object = glyph->object;
21751 int charpos = glyph->charpos;
21753 /* Try text properties. */
21754 if (STRINGP (object)
21755 && charpos >= 0
21756 && charpos < SCHARS (object))
21758 help = Fget_text_property (make_number (charpos),
21759 Qhelp_echo, object);
21760 if (NILP (help))
21762 /* If the string itself doesn't specify a help-echo,
21763 see if the buffer text ``under'' it does. */
21764 struct glyph_row *r
21765 = MATRIX_ROW (w->current_matrix, vpos);
21766 int start = MATRIX_ROW_START_CHARPOS (r);
21767 int pos = string_buffer_position (w, object, start);
21768 if (pos > 0)
21770 help = Fget_char_property (make_number (pos),
21771 Qhelp_echo, w->buffer);
21772 if (!NILP (help))
21774 charpos = pos;
21775 object = w->buffer;
21780 else if (BUFFERP (object)
21781 && charpos >= BEGV
21782 && charpos < ZV)
21783 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21784 object);
21786 if (!NILP (help))
21788 help_echo_string = help;
21789 help_echo_window = window;
21790 help_echo_object = object;
21791 help_echo_pos = charpos;
21796 /* Look for a `pointer' property. */
21797 if (NILP (pointer))
21799 /* Check overlays first. */
21800 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21801 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21803 if (NILP (pointer))
21805 Lisp_Object object = glyph->object;
21806 int charpos = glyph->charpos;
21808 /* Try text properties. */
21809 if (STRINGP (object)
21810 && charpos >= 0
21811 && charpos < SCHARS (object))
21813 pointer = Fget_text_property (make_number (charpos),
21814 Qpointer, object);
21815 if (NILP (pointer))
21817 /* If the string itself doesn't specify a pointer,
21818 see if the buffer text ``under'' it does. */
21819 struct glyph_row *r
21820 = MATRIX_ROW (w->current_matrix, vpos);
21821 int start = MATRIX_ROW_START_CHARPOS (r);
21822 int pos = string_buffer_position (w, object, start);
21823 if (pos > 0)
21824 pointer = Fget_char_property (make_number (pos),
21825 Qpointer, w->buffer);
21828 else if (BUFFERP (object)
21829 && charpos >= BEGV
21830 && charpos < ZV)
21831 pointer = Fget_text_property (make_number (charpos),
21832 Qpointer, object);
21836 BEGV = obegv;
21837 ZV = ozv;
21838 current_buffer = obuf;
21841 set_cursor:
21843 define_frame_cursor1 (f, cursor, pointer);
21847 /* EXPORT for RIF:
21848 Clear any mouse-face on window W. This function is part of the
21849 redisplay interface, and is called from try_window_id and similar
21850 functions to ensure the mouse-highlight is off. */
21852 void
21853 x_clear_window_mouse_face (w)
21854 struct window *w;
21856 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21857 Lisp_Object window;
21859 BLOCK_INPUT;
21860 XSETWINDOW (window, w);
21861 if (EQ (window, dpyinfo->mouse_face_window))
21862 clear_mouse_face (dpyinfo);
21863 UNBLOCK_INPUT;
21867 /* EXPORT:
21868 Just discard the mouse face information for frame F, if any.
21869 This is used when the size of F is changed. */
21871 void
21872 cancel_mouse_face (f)
21873 struct frame *f;
21875 Lisp_Object window;
21876 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21878 window = dpyinfo->mouse_face_window;
21879 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21881 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21882 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21883 dpyinfo->mouse_face_window = Qnil;
21888 #endif /* HAVE_WINDOW_SYSTEM */
21891 /***********************************************************************
21892 Exposure Events
21893 ***********************************************************************/
21895 #ifdef HAVE_WINDOW_SYSTEM
21897 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21898 which intersects rectangle R. R is in window-relative coordinates. */
21900 static void
21901 expose_area (w, row, r, area)
21902 struct window *w;
21903 struct glyph_row *row;
21904 XRectangle *r;
21905 enum glyph_row_area area;
21907 struct glyph *first = row->glyphs[area];
21908 struct glyph *end = row->glyphs[area] + row->used[area];
21909 struct glyph *last;
21910 int first_x, start_x, x;
21912 if (area == TEXT_AREA && row->fill_line_p)
21913 /* If row extends face to end of line write the whole line. */
21914 draw_glyphs (w, 0, row, area,
21915 0, row->used[area],
21916 DRAW_NORMAL_TEXT, 0);
21917 else
21919 /* Set START_X to the window-relative start position for drawing glyphs of
21920 AREA. The first glyph of the text area can be partially visible.
21921 The first glyphs of other areas cannot. */
21922 start_x = window_box_left_offset (w, area);
21923 x = start_x;
21924 if (area == TEXT_AREA)
21925 x += row->x;
21927 /* Find the first glyph that must be redrawn. */
21928 while (first < end
21929 && x + first->pixel_width < r->x)
21931 x += first->pixel_width;
21932 ++first;
21935 /* Find the last one. */
21936 last = first;
21937 first_x = x;
21938 while (last < end
21939 && x < r->x + r->width)
21941 x += last->pixel_width;
21942 ++last;
21945 /* Repaint. */
21946 if (last > first)
21947 draw_glyphs (w, first_x - start_x, row, area,
21948 first - row->glyphs[area], last - row->glyphs[area],
21949 DRAW_NORMAL_TEXT, 0);
21954 /* Redraw the parts of the glyph row ROW on window W intersecting
21955 rectangle R. R is in window-relative coordinates. Value is
21956 non-zero if mouse-face was overwritten. */
21958 static int
21959 expose_line (w, row, r)
21960 struct window *w;
21961 struct glyph_row *row;
21962 XRectangle *r;
21964 xassert (row->enabled_p);
21966 if (row->mode_line_p || w->pseudo_window_p)
21967 draw_glyphs (w, 0, row, TEXT_AREA,
21968 0, row->used[TEXT_AREA],
21969 DRAW_NORMAL_TEXT, 0);
21970 else
21972 if (row->used[LEFT_MARGIN_AREA])
21973 expose_area (w, row, r, LEFT_MARGIN_AREA);
21974 if (row->used[TEXT_AREA])
21975 expose_area (w, row, r, TEXT_AREA);
21976 if (row->used[RIGHT_MARGIN_AREA])
21977 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21978 draw_row_fringe_bitmaps (w, row);
21981 return row->mouse_face_p;
21985 /* Redraw those parts of glyphs rows during expose event handling that
21986 overlap other rows. Redrawing of an exposed line writes over parts
21987 of lines overlapping that exposed line; this function fixes that.
21989 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21990 row in W's current matrix that is exposed and overlaps other rows.
21991 LAST_OVERLAPPING_ROW is the last such row. */
21993 static void
21994 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21995 struct window *w;
21996 struct glyph_row *first_overlapping_row;
21997 struct glyph_row *last_overlapping_row;
21999 struct glyph_row *row;
22001 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22002 if (row->overlapping_p)
22004 xassert (row->enabled_p && !row->mode_line_p);
22006 if (row->used[LEFT_MARGIN_AREA])
22007 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22009 if (row->used[TEXT_AREA])
22010 x_fix_overlapping_area (w, row, TEXT_AREA);
22012 if (row->used[RIGHT_MARGIN_AREA])
22013 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22018 /* Return non-zero if W's cursor intersects rectangle R. */
22020 static int
22021 phys_cursor_in_rect_p (w, r)
22022 struct window *w;
22023 XRectangle *r;
22025 XRectangle cr, result;
22026 struct glyph *cursor_glyph;
22028 cursor_glyph = get_phys_cursor_glyph (w);
22029 if (cursor_glyph)
22031 /* r is relative to W's box, but w->phys_cursor.x is relative
22032 to left edge of W's TEXT area. Adjust it. */
22033 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22034 cr.y = w->phys_cursor.y;
22035 cr.width = cursor_glyph->pixel_width;
22036 cr.height = w->phys_cursor_height;
22037 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22038 I assume the effect is the same -- and this is portable. */
22039 return x_intersect_rectangles (&cr, r, &result);
22041 else
22042 return 0;
22046 /* EXPORT:
22047 Draw a vertical window border to the right of window W if W doesn't
22048 have vertical scroll bars. */
22050 void
22051 x_draw_vertical_border (w)
22052 struct window *w;
22054 /* We could do better, if we knew what type of scroll-bar the adjacent
22055 windows (on either side) have... But we don't :-(
22056 However, I think this works ok. ++KFS 2003-04-25 */
22058 /* Redraw borders between horizontally adjacent windows. Don't
22059 do it for frames with vertical scroll bars because either the
22060 right scroll bar of a window, or the left scroll bar of its
22061 neighbor will suffice as a border. */
22062 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22063 return;
22065 if (!WINDOW_RIGHTMOST_P (w)
22066 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22068 int x0, x1, y0, y1;
22070 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22071 y1 -= 1;
22073 rif->draw_vertical_window_border (w, x1, y0, y1);
22075 else if (!WINDOW_LEFTMOST_P (w)
22076 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22078 int x0, x1, y0, y1;
22080 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22081 y1 -= 1;
22083 rif->draw_vertical_window_border (w, x0, y0, y1);
22088 /* Redraw the part of window W intersection rectangle FR. Pixel
22089 coordinates in FR are frame-relative. Call this function with
22090 input blocked. Value is non-zero if the exposure overwrites
22091 mouse-face. */
22093 static int
22094 expose_window (w, fr)
22095 struct window *w;
22096 XRectangle *fr;
22098 struct frame *f = XFRAME (w->frame);
22099 XRectangle wr, r;
22100 int mouse_face_overwritten_p = 0;
22102 /* If window is not yet fully initialized, do nothing. This can
22103 happen when toolkit scroll bars are used and a window is split.
22104 Reconfiguring the scroll bar will generate an expose for a newly
22105 created window. */
22106 if (w->current_matrix == NULL)
22107 return 0;
22109 /* When we're currently updating the window, display and current
22110 matrix usually don't agree. Arrange for a thorough display
22111 later. */
22112 if (w == updated_window)
22114 SET_FRAME_GARBAGED (f);
22115 return 0;
22118 /* Frame-relative pixel rectangle of W. */
22119 wr.x = WINDOW_LEFT_EDGE_X (w);
22120 wr.y = WINDOW_TOP_EDGE_Y (w);
22121 wr.width = WINDOW_TOTAL_WIDTH (w);
22122 wr.height = WINDOW_TOTAL_HEIGHT (w);
22124 if (x_intersect_rectangles (fr, &wr, &r))
22126 int yb = window_text_bottom_y (w);
22127 struct glyph_row *row;
22128 int cursor_cleared_p;
22129 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22131 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22132 r.x, r.y, r.width, r.height));
22134 /* Convert to window coordinates. */
22135 r.x -= WINDOW_LEFT_EDGE_X (w);
22136 r.y -= WINDOW_TOP_EDGE_Y (w);
22138 /* Turn off the cursor. */
22139 if (!w->pseudo_window_p
22140 && phys_cursor_in_rect_p (w, &r))
22142 x_clear_cursor (w);
22143 cursor_cleared_p = 1;
22145 else
22146 cursor_cleared_p = 0;
22148 /* Update lines intersecting rectangle R. */
22149 first_overlapping_row = last_overlapping_row = NULL;
22150 for (row = w->current_matrix->rows;
22151 row->enabled_p;
22152 ++row)
22154 int y0 = row->y;
22155 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22157 if ((y0 >= r.y && y0 < r.y + r.height)
22158 || (y1 > r.y && y1 < r.y + r.height)
22159 || (r.y >= y0 && r.y < y1)
22160 || (r.y + r.height > y0 && r.y + r.height < y1))
22162 /* A header line may be overlapping, but there is no need
22163 to fix overlapping areas for them. KFS 2005-02-12 */
22164 if (row->overlapping_p && !row->mode_line_p)
22166 if (first_overlapping_row == NULL)
22167 first_overlapping_row = row;
22168 last_overlapping_row = row;
22171 if (expose_line (w, row, &r))
22172 mouse_face_overwritten_p = 1;
22175 if (y1 >= yb)
22176 break;
22179 /* Display the mode line if there is one. */
22180 if (WINDOW_WANTS_MODELINE_P (w)
22181 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22182 row->enabled_p)
22183 && row->y < r.y + r.height)
22185 if (expose_line (w, row, &r))
22186 mouse_face_overwritten_p = 1;
22189 if (!w->pseudo_window_p)
22191 /* Fix the display of overlapping rows. */
22192 if (first_overlapping_row)
22193 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22195 /* Draw border between windows. */
22196 x_draw_vertical_border (w);
22198 /* Turn the cursor on again. */
22199 if (cursor_cleared_p)
22200 update_window_cursor (w, 1);
22204 return mouse_face_overwritten_p;
22209 /* Redraw (parts) of all windows in the window tree rooted at W that
22210 intersect R. R contains frame pixel coordinates. Value is
22211 non-zero if the exposure overwrites mouse-face. */
22213 static int
22214 expose_window_tree (w, r)
22215 struct window *w;
22216 XRectangle *r;
22218 struct frame *f = XFRAME (w->frame);
22219 int mouse_face_overwritten_p = 0;
22221 while (w && !FRAME_GARBAGED_P (f))
22223 if (!NILP (w->hchild))
22224 mouse_face_overwritten_p
22225 |= expose_window_tree (XWINDOW (w->hchild), r);
22226 else if (!NILP (w->vchild))
22227 mouse_face_overwritten_p
22228 |= expose_window_tree (XWINDOW (w->vchild), r);
22229 else
22230 mouse_face_overwritten_p |= expose_window (w, r);
22232 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22235 return mouse_face_overwritten_p;
22239 /* EXPORT:
22240 Redisplay an exposed area of frame F. X and Y are the upper-left
22241 corner of the exposed rectangle. W and H are width and height of
22242 the exposed area. All are pixel values. W or H zero means redraw
22243 the entire frame. */
22245 void
22246 expose_frame (f, x, y, w, h)
22247 struct frame *f;
22248 int x, y, w, h;
22250 XRectangle r;
22251 int mouse_face_overwritten_p = 0;
22253 TRACE ((stderr, "expose_frame "));
22255 /* No need to redraw if frame will be redrawn soon. */
22256 if (FRAME_GARBAGED_P (f))
22258 TRACE ((stderr, " garbaged\n"));
22259 return;
22262 /* If basic faces haven't been realized yet, there is no point in
22263 trying to redraw anything. This can happen when we get an expose
22264 event while Emacs is starting, e.g. by moving another window. */
22265 if (FRAME_FACE_CACHE (f) == NULL
22266 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22268 TRACE ((stderr, " no faces\n"));
22269 return;
22272 if (w == 0 || h == 0)
22274 r.x = r.y = 0;
22275 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22276 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22278 else
22280 r.x = x;
22281 r.y = y;
22282 r.width = w;
22283 r.height = h;
22286 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22287 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22289 if (WINDOWP (f->tool_bar_window))
22290 mouse_face_overwritten_p
22291 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22293 #ifdef HAVE_X_WINDOWS
22294 #ifndef MSDOS
22295 #ifndef USE_X_TOOLKIT
22296 if (WINDOWP (f->menu_bar_window))
22297 mouse_face_overwritten_p
22298 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22299 #endif /* not USE_X_TOOLKIT */
22300 #endif
22301 #endif
22303 /* Some window managers support a focus-follows-mouse style with
22304 delayed raising of frames. Imagine a partially obscured frame,
22305 and moving the mouse into partially obscured mouse-face on that
22306 frame. The visible part of the mouse-face will be highlighted,
22307 then the WM raises the obscured frame. With at least one WM, KDE
22308 2.1, Emacs is not getting any event for the raising of the frame
22309 (even tried with SubstructureRedirectMask), only Expose events.
22310 These expose events will draw text normally, i.e. not
22311 highlighted. Which means we must redo the highlight here.
22312 Subsume it under ``we love X''. --gerd 2001-08-15 */
22313 /* Included in Windows version because Windows most likely does not
22314 do the right thing if any third party tool offers
22315 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22316 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22318 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22319 if (f == dpyinfo->mouse_face_mouse_frame)
22321 int x = dpyinfo->mouse_face_mouse_x;
22322 int y = dpyinfo->mouse_face_mouse_y;
22323 clear_mouse_face (dpyinfo);
22324 note_mouse_highlight (f, x, y);
22330 /* EXPORT:
22331 Determine the intersection of two rectangles R1 and R2. Return
22332 the intersection in *RESULT. Value is non-zero if RESULT is not
22333 empty. */
22336 x_intersect_rectangles (r1, r2, result)
22337 XRectangle *r1, *r2, *result;
22339 XRectangle *left, *right;
22340 XRectangle *upper, *lower;
22341 int intersection_p = 0;
22343 /* Rearrange so that R1 is the left-most rectangle. */
22344 if (r1->x < r2->x)
22345 left = r1, right = r2;
22346 else
22347 left = r2, right = r1;
22349 /* X0 of the intersection is right.x0, if this is inside R1,
22350 otherwise there is no intersection. */
22351 if (right->x <= left->x + left->width)
22353 result->x = right->x;
22355 /* The right end of the intersection is the minimum of the
22356 the right ends of left and right. */
22357 result->width = (min (left->x + left->width, right->x + right->width)
22358 - result->x);
22360 /* Same game for Y. */
22361 if (r1->y < r2->y)
22362 upper = r1, lower = r2;
22363 else
22364 upper = r2, lower = r1;
22366 /* The upper end of the intersection is lower.y0, if this is inside
22367 of upper. Otherwise, there is no intersection. */
22368 if (lower->y <= upper->y + upper->height)
22370 result->y = lower->y;
22372 /* The lower end of the intersection is the minimum of the lower
22373 ends of upper and lower. */
22374 result->height = (min (lower->y + lower->height,
22375 upper->y + upper->height)
22376 - result->y);
22377 intersection_p = 1;
22381 return intersection_p;
22384 #endif /* HAVE_WINDOW_SYSTEM */
22387 /***********************************************************************
22388 Initialization
22389 ***********************************************************************/
22391 void
22392 syms_of_xdisp ()
22394 Vwith_echo_area_save_vector = Qnil;
22395 staticpro (&Vwith_echo_area_save_vector);
22397 Vmessage_stack = Qnil;
22398 staticpro (&Vmessage_stack);
22400 Qinhibit_redisplay = intern ("inhibit-redisplay");
22401 staticpro (&Qinhibit_redisplay);
22403 message_dolog_marker1 = Fmake_marker ();
22404 staticpro (&message_dolog_marker1);
22405 message_dolog_marker2 = Fmake_marker ();
22406 staticpro (&message_dolog_marker2);
22407 message_dolog_marker3 = Fmake_marker ();
22408 staticpro (&message_dolog_marker3);
22410 #if GLYPH_DEBUG
22411 defsubr (&Sdump_frame_glyph_matrix);
22412 defsubr (&Sdump_glyph_matrix);
22413 defsubr (&Sdump_glyph_row);
22414 defsubr (&Sdump_tool_bar_row);
22415 defsubr (&Strace_redisplay);
22416 defsubr (&Strace_to_stderr);
22417 #endif
22418 #ifdef HAVE_WINDOW_SYSTEM
22419 defsubr (&Stool_bar_lines_needed);
22420 defsubr (&Slookup_image_map);
22421 #endif
22422 defsubr (&Sformat_mode_line);
22424 staticpro (&Qmenu_bar_update_hook);
22425 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22427 staticpro (&Qoverriding_terminal_local_map);
22428 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22430 staticpro (&Qoverriding_local_map);
22431 Qoverriding_local_map = intern ("overriding-local-map");
22433 staticpro (&Qwindow_scroll_functions);
22434 Qwindow_scroll_functions = intern ("window-scroll-functions");
22436 staticpro (&Qredisplay_end_trigger_functions);
22437 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22439 staticpro (&Qinhibit_point_motion_hooks);
22440 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22442 QCdata = intern (":data");
22443 staticpro (&QCdata);
22444 Qdisplay = intern ("display");
22445 staticpro (&Qdisplay);
22446 Qspace_width = intern ("space-width");
22447 staticpro (&Qspace_width);
22448 Qraise = intern ("raise");
22449 staticpro (&Qraise);
22450 Qslice = intern ("slice");
22451 staticpro (&Qslice);
22452 Qspace = intern ("space");
22453 staticpro (&Qspace);
22454 Qmargin = intern ("margin");
22455 staticpro (&Qmargin);
22456 Qpointer = intern ("pointer");
22457 staticpro (&Qpointer);
22458 Qleft_margin = intern ("left-margin");
22459 staticpro (&Qleft_margin);
22460 Qright_margin = intern ("right-margin");
22461 staticpro (&Qright_margin);
22462 Qcenter = intern ("center");
22463 staticpro (&Qcenter);
22464 Qline_height = intern ("line-height");
22465 staticpro (&Qline_height);
22466 QCalign_to = intern (":align-to");
22467 staticpro (&QCalign_to);
22468 QCrelative_width = intern (":relative-width");
22469 staticpro (&QCrelative_width);
22470 QCrelative_height = intern (":relative-height");
22471 staticpro (&QCrelative_height);
22472 QCeval = intern (":eval");
22473 staticpro (&QCeval);
22474 QCpropertize = intern (":propertize");
22475 staticpro (&QCpropertize);
22476 QCfile = intern (":file");
22477 staticpro (&QCfile);
22478 Qfontified = intern ("fontified");
22479 staticpro (&Qfontified);
22480 Qfontification_functions = intern ("fontification-functions");
22481 staticpro (&Qfontification_functions);
22482 Qtrailing_whitespace = intern ("trailing-whitespace");
22483 staticpro (&Qtrailing_whitespace);
22484 Qescape_glyph = intern ("escape-glyph");
22485 staticpro (&Qescape_glyph);
22486 Qimage = intern ("image");
22487 staticpro (&Qimage);
22488 QCmap = intern (":map");
22489 staticpro (&QCmap);
22490 QCpointer = intern (":pointer");
22491 staticpro (&QCpointer);
22492 Qrect = intern ("rect");
22493 staticpro (&Qrect);
22494 Qcircle = intern ("circle");
22495 staticpro (&Qcircle);
22496 Qpoly = intern ("poly");
22497 staticpro (&Qpoly);
22498 Qmessage_truncate_lines = intern ("message-truncate-lines");
22499 staticpro (&Qmessage_truncate_lines);
22500 Qgrow_only = intern ("grow-only");
22501 staticpro (&Qgrow_only);
22502 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22503 staticpro (&Qinhibit_menubar_update);
22504 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22505 staticpro (&Qinhibit_eval_during_redisplay);
22506 Qposition = intern ("position");
22507 staticpro (&Qposition);
22508 Qbuffer_position = intern ("buffer-position");
22509 staticpro (&Qbuffer_position);
22510 Qobject = intern ("object");
22511 staticpro (&Qobject);
22512 Qbar = intern ("bar");
22513 staticpro (&Qbar);
22514 Qhbar = intern ("hbar");
22515 staticpro (&Qhbar);
22516 Qbox = intern ("box");
22517 staticpro (&Qbox);
22518 Qhollow = intern ("hollow");
22519 staticpro (&Qhollow);
22520 Qhand = intern ("hand");
22521 staticpro (&Qhand);
22522 Qarrow = intern ("arrow");
22523 staticpro (&Qarrow);
22524 Qtext = intern ("text");
22525 staticpro (&Qtext);
22526 Qrisky_local_variable = intern ("risky-local-variable");
22527 staticpro (&Qrisky_local_variable);
22528 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22529 staticpro (&Qinhibit_free_realized_faces);
22531 list_of_error = Fcons (Fcons (intern ("error"),
22532 Fcons (intern ("void-variable"), Qnil)),
22533 Qnil);
22534 staticpro (&list_of_error);
22536 Qlast_arrow_position = intern ("last-arrow-position");
22537 staticpro (&Qlast_arrow_position);
22538 Qlast_arrow_string = intern ("last-arrow-string");
22539 staticpro (&Qlast_arrow_string);
22541 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22542 staticpro (&Qoverlay_arrow_string);
22543 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22544 staticpro (&Qoverlay_arrow_bitmap);
22546 echo_buffer[0] = echo_buffer[1] = Qnil;
22547 staticpro (&echo_buffer[0]);
22548 staticpro (&echo_buffer[1]);
22550 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22551 staticpro (&echo_area_buffer[0]);
22552 staticpro (&echo_area_buffer[1]);
22554 Vmessages_buffer_name = build_string ("*Messages*");
22555 staticpro (&Vmessages_buffer_name);
22557 mode_line_proptrans_alist = Qnil;
22558 staticpro (&mode_line_proptrans_alist);
22560 mode_line_string_list = Qnil;
22561 staticpro (&mode_line_string_list);
22563 help_echo_string = Qnil;
22564 staticpro (&help_echo_string);
22565 help_echo_object = Qnil;
22566 staticpro (&help_echo_object);
22567 help_echo_window = Qnil;
22568 staticpro (&help_echo_window);
22569 previous_help_echo_string = Qnil;
22570 staticpro (&previous_help_echo_string);
22571 help_echo_pos = -1;
22573 #ifdef HAVE_WINDOW_SYSTEM
22574 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22575 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22576 For example, if a block cursor is over a tab, it will be drawn as
22577 wide as that tab on the display. */);
22578 x_stretch_cursor_p = 0;
22579 #endif
22581 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22582 doc: /* *Non-nil means highlight trailing whitespace.
22583 The face used for trailing whitespace is `trailing-whitespace'. */);
22584 Vshow_trailing_whitespace = Qnil;
22586 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22587 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22588 Vshow_nonbreak_escape = Qt;
22590 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22591 doc: /* *The pointer shape to show in void text areas.
22592 Nil means to show the text pointer. Other options are `arrow', `text',
22593 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22594 Vvoid_text_area_pointer = Qarrow;
22596 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22597 doc: /* Non-nil means don't actually do any redisplay.
22598 This is used for internal purposes. */);
22599 Vinhibit_redisplay = Qnil;
22601 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22602 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22603 Vglobal_mode_string = Qnil;
22605 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22606 doc: /* Marker for where to display an arrow on top of the buffer text.
22607 This must be the beginning of a line in order to work.
22608 See also `overlay-arrow-string'. */);
22609 Voverlay_arrow_position = Qnil;
22611 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22612 doc: /* String to display as an arrow in non-window frames.
22613 See also `overlay-arrow-position'. */);
22614 Voverlay_arrow_string = build_string ("=>");
22616 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22617 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22618 The symbols on this list are examined during redisplay to determine
22619 where to display overlay arrows. */);
22620 Voverlay_arrow_variable_list
22621 = Fcons (intern ("overlay-arrow-position"), Qnil);
22623 DEFVAR_INT ("scroll-step", &scroll_step,
22624 doc: /* *The number of lines to try scrolling a window by when point moves out.
22625 If that fails to bring point back on frame, point is centered instead.
22626 If this is zero, point is always centered after it moves off frame.
22627 If you want scrolling to always be a line at a time, you should set
22628 `scroll-conservatively' to a large value rather than set this to 1. */);
22630 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22631 doc: /* *Scroll up to this many lines, to bring point back on screen.
22632 A value of zero means to scroll the text to center point vertically
22633 in the window. */);
22634 scroll_conservatively = 0;
22636 DEFVAR_INT ("scroll-margin", &scroll_margin,
22637 doc: /* *Number of lines of margin at the top and bottom of a window.
22638 Recenter the window whenever point gets within this many lines
22639 of the top or bottom of the window. */);
22640 scroll_margin = 0;
22642 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22643 doc: /* Pixels per inch on current display.
22644 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22645 Vdisplay_pixels_per_inch = make_float (72.0);
22647 #if GLYPH_DEBUG
22648 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22649 #endif
22651 DEFVAR_BOOL ("truncate-partial-width-windows",
22652 &truncate_partial_width_windows,
22653 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22654 truncate_partial_width_windows = 1;
22656 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22657 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22658 Any other value means to use the appropriate face, `mode-line',
22659 `header-line', or `menu' respectively. */);
22660 mode_line_inverse_video = 1;
22662 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22663 doc: /* *Maximum buffer size for which line number should be displayed.
22664 If the buffer is bigger than this, the line number does not appear
22665 in the mode line. A value of nil means no limit. */);
22666 Vline_number_display_limit = Qnil;
22668 DEFVAR_INT ("line-number-display-limit-width",
22669 &line_number_display_limit_width,
22670 doc: /* *Maximum line width (in characters) for line number display.
22671 If the average length of the lines near point is bigger than this, then the
22672 line number may be omitted from the mode line. */);
22673 line_number_display_limit_width = 200;
22675 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22676 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22677 highlight_nonselected_windows = 0;
22679 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22680 doc: /* Non-nil if more than one frame is visible on this display.
22681 Minibuffer-only frames don't count, but iconified frames do.
22682 This variable is not guaranteed to be accurate except while processing
22683 `frame-title-format' and `icon-title-format'. */);
22685 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22686 doc: /* Template for displaying the title bar of visible frames.
22687 \(Assuming the window manager supports this feature.)
22688 This variable has the same structure as `mode-line-format' (which see),
22689 and is used only on frames for which no explicit name has been set
22690 \(see `modify-frame-parameters'). */);
22692 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22693 doc: /* Template for displaying the title bar of an iconified frame.
22694 \(Assuming the window manager supports this feature.)
22695 This variable has the same structure as `mode-line-format' (which see),
22696 and is used only on frames for which no explicit name has been set
22697 \(see `modify-frame-parameters'). */);
22698 Vicon_title_format
22699 = Vframe_title_format
22700 = Fcons (intern ("multiple-frames"),
22701 Fcons (build_string ("%b"),
22702 Fcons (Fcons (empty_string,
22703 Fcons (intern ("invocation-name"),
22704 Fcons (build_string ("@"),
22705 Fcons (intern ("system-name"),
22706 Qnil)))),
22707 Qnil)));
22709 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22710 doc: /* Maximum number of lines to keep in the message log buffer.
22711 If nil, disable message logging. If t, log messages but don't truncate
22712 the buffer when it becomes large. */);
22713 Vmessage_log_max = make_number (50);
22715 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22716 doc: /* Functions called before redisplay, if window sizes have changed.
22717 The value should be a list of functions that take one argument.
22718 Just before redisplay, for each frame, if any of its windows have changed
22719 size since the last redisplay, or have been split or deleted,
22720 all the functions in the list are called, with the frame as argument. */);
22721 Vwindow_size_change_functions = Qnil;
22723 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22724 doc: /* List of functions to call before redisplaying a window with scrolling.
22725 Each function is called with two arguments, the window
22726 and its new display-start position. Note that the value of `window-end'
22727 is not valid when these functions are called. */);
22728 Vwindow_scroll_functions = Qnil;
22730 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22731 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22732 mouse_autoselect_window = 0;
22734 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22735 doc: /* *Non-nil means automatically resize tool-bars.
22736 This increases a tool-bar's height if not all tool-bar items are visible.
22737 It decreases a tool-bar's height when it would display blank lines
22738 otherwise. */);
22739 auto_resize_tool_bars_p = 1;
22741 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22742 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22743 auto_raise_tool_bar_buttons_p = 1;
22745 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22746 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22747 make_cursor_line_fully_visible_p = 1;
22749 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22750 doc: /* *Margin around tool-bar buttons in pixels.
22751 If an integer, use that for both horizontal and vertical margins.
22752 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22753 HORZ specifying the horizontal margin, and VERT specifying the
22754 vertical margin. */);
22755 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22757 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22758 doc: /* *Relief thickness of tool-bar buttons. */);
22759 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22761 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22762 doc: /* List of functions to call to fontify regions of text.
22763 Each function is called with one argument POS. Functions must
22764 fontify a region starting at POS in the current buffer, and give
22765 fontified regions the property `fontified'. */);
22766 Vfontification_functions = Qnil;
22767 Fmake_variable_buffer_local (Qfontification_functions);
22769 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22770 &unibyte_display_via_language_environment,
22771 doc: /* *Non-nil means display unibyte text according to language environment.
22772 Specifically this means that unibyte non-ASCII characters
22773 are displayed by converting them to the equivalent multibyte characters
22774 according to the current language environment. As a result, they are
22775 displayed according to the current fontset. */);
22776 unibyte_display_via_language_environment = 0;
22778 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22779 doc: /* *Maximum height for resizing mini-windows.
22780 If a float, it specifies a fraction of the mini-window frame's height.
22781 If an integer, it specifies a number of lines. */);
22782 Vmax_mini_window_height = make_float (0.25);
22784 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22785 doc: /* *How to resize mini-windows.
22786 A value of nil means don't automatically resize mini-windows.
22787 A value of t means resize them to fit the text displayed in them.
22788 A value of `grow-only', the default, means let mini-windows grow
22789 only, until their display becomes empty, at which point the windows
22790 go back to their normal size. */);
22791 Vresize_mini_windows = Qgrow_only;
22793 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22794 doc: /* Alist specifying how to blink the cursor off.
22795 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22796 `cursor-type' frame-parameter or variable equals ON-STATE,
22797 comparing using `equal', Emacs uses OFF-STATE to specify
22798 how to blink it off. */);
22799 Vblink_cursor_alist = Qnil;
22801 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22802 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22803 automatic_hscrolling_p = 1;
22805 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22806 doc: /* *How many columns away from the window edge point is allowed to get
22807 before automatic hscrolling will horizontally scroll the window. */);
22808 hscroll_margin = 5;
22810 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22811 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22812 When point is less than `automatic-hscroll-margin' columns from the window
22813 edge, automatic hscrolling will scroll the window by the amount of columns
22814 determined by this variable. If its value is a positive integer, scroll that
22815 many columns. If it's a positive floating-point number, it specifies the
22816 fraction of the window's width to scroll. If it's nil or zero, point will be
22817 centered horizontally after the scroll. Any other value, including negative
22818 numbers, are treated as if the value were zero.
22820 Automatic hscrolling always moves point outside the scroll margin, so if
22821 point was more than scroll step columns inside the margin, the window will
22822 scroll more than the value given by the scroll step.
22824 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22825 and `scroll-right' overrides this variable's effect. */);
22826 Vhscroll_step = make_number (0);
22828 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22829 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22830 Bind this around calls to `message' to let it take effect. */);
22831 message_truncate_lines = 0;
22833 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22834 doc: /* Normal hook run to update the menu bar definitions.
22835 Redisplay runs this hook before it redisplays the menu bar.
22836 This is used to update submenus such as Buffers,
22837 whose contents depend on various data. */);
22838 Vmenu_bar_update_hook = Qnil;
22840 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22841 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22842 inhibit_menubar_update = 0;
22844 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22845 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22846 inhibit_eval_during_redisplay = 0;
22848 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22849 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22850 inhibit_free_realized_faces = 0;
22852 #if GLYPH_DEBUG
22853 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22854 doc: /* Inhibit try_window_id display optimization. */);
22855 inhibit_try_window_id = 0;
22857 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22858 doc: /* Inhibit try_window_reusing display optimization. */);
22859 inhibit_try_window_reusing = 0;
22861 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22862 doc: /* Inhibit try_cursor_movement display optimization. */);
22863 inhibit_try_cursor_movement = 0;
22864 #endif /* GLYPH_DEBUG */
22868 /* Initialize this module when Emacs starts. */
22870 void
22871 init_xdisp ()
22873 Lisp_Object root_window;
22874 struct window *mini_w;
22876 current_header_line_height = current_mode_line_height = -1;
22878 CHARPOS (this_line_start_pos) = 0;
22880 mini_w = XWINDOW (minibuf_window);
22881 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22883 if (!noninteractive)
22885 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22886 int i;
22888 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22889 set_window_height (root_window,
22890 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22892 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22893 set_window_height (minibuf_window, 1, 0);
22895 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22896 mini_w->total_cols = make_number (FRAME_COLS (f));
22898 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22899 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22900 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22902 /* The default ellipsis glyphs `...'. */
22903 for (i = 0; i < 3; ++i)
22904 default_invis_vector[i] = make_number ('.');
22908 /* Allocate the buffer for frame titles.
22909 Also used for `format-mode-line'. */
22910 int size = 100;
22911 frame_title_buf = (char *) xmalloc (size);
22912 frame_title_buf_end = frame_title_buf + size;
22913 frame_title_ptr = NULL;
22916 help_echo_showing_p = 0;
22920 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22921 (do not change this comment) */