Rename `image-library-alist' to `dynamic-library-alist'.
[emacs.git] / src / xdisp.c
blob4c007e572cecfe21490c91c81e5e4e9df52f726d
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
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.
37 The following diagram shows how redisplay code is invoked. As you
38 can see, Lisp calls redisplay and vice versa. Under window systems
39 like X, some portions of the redisplay code are also called
40 asynchronously during mouse movement or expose events. It is very
41 important that these code parts do NOT use the C library (malloc,
42 free) because many C libraries under Unix are not reentrant. They
43 may also NOT call functions of the Lisp interpreter which could
44 change the interpreter's state. If you don't follow these rules,
45 you will encounter bugs which are very hard to explain.
47 +--------------+ redisplay +----------------+
48 | Lisp machine |---------------->| Redisplay code |<--+
49 +--------------+ (xdisp.c) +----------------+ |
50 ^ | |
51 +----------------------------------+ |
52 Don't use this path when called |
53 asynchronously! |
55 expose_window (asynchronous) |
57 X expose events -----+
59 What does redisplay do? Obviously, it has to figure out somehow what
60 has been changed since the last time the display has been updated,
61 and to make these changes visible. Preferably it would do that in
62 a moderately intelligent way, i.e. fast.
64 Changes in buffer text can be deduced from window and buffer
65 structures, and from some global variables like `beg_unchanged' and
66 `end_unchanged'. The contents of the display are additionally
67 recorded in a `glyph matrix', a two-dimensional matrix of glyph
68 structures. Each row in such a matrix corresponds to a line on the
69 display, and each glyph in a row corresponds to a column displaying
70 a character, an image, or what else. This matrix is called the
71 `current glyph matrix' or `current matrix' in redisplay
72 terminology.
74 For buffer parts that have been changed since the last update, a
75 second glyph matrix is constructed, the so called `desired glyph
76 matrix' or short `desired matrix'. Current and desired matrix are
77 then compared to find a cheap way to update the display, e.g. by
78 reusing part of the display by scrolling lines.
80 You will find a lot of redisplay optimizations when you start
81 looking at the innards of redisplay. The overall goal of all these
82 optimizations is to make redisplay fast because it is done
83 frequently. Some of these optimizations are implemented by the
84 following functions:
86 . try_cursor_movement
88 This function tries to update the display if the text in the
89 window did not change and did not scroll, only point moved, and
90 it did not move off the displayed portion of the text.
92 . try_window_reusing_current_matrix
94 This function reuses the current matrix of a window when text
95 has not changed, but the window start changed (e.g., due to
96 scrolling).
98 . try_window_id
100 This function attempts to redisplay a window by reusing parts of
101 its existing display. It finds and reuses the part that was not
102 changed, and redraws the rest.
104 . try_window
106 This function performs the full redisplay of a single window
107 assuming that its fonts were not changed and that the cursor
108 will not end up in the scroll margins. (Loading fonts requires
109 re-adjustment of dimensions of glyph matrices, which makes this
110 method impossible to use.)
112 These optimizations are tried in sequence (some can be skipped if
113 it is known that they are not applicable). If none of the
114 optimizations were successful, redisplay calls redisplay_windows,
115 which performs a full redisplay of all windows.
117 Desired matrices.
119 Desired matrices are always built per Emacs window. The function
120 `display_line' is the central function to look at if you are
121 interested. It constructs one row in a desired matrix given an
122 iterator structure containing both a buffer position and a
123 description of the environment in which the text is to be
124 displayed. But this is too early, read on.
126 Characters and pixmaps displayed for a range of buffer text depend
127 on various settings of buffers and windows, on overlays and text
128 properties, on display tables, on selective display. The good news
129 is that all this hairy stuff is hidden behind a small set of
130 interface functions taking an iterator structure (struct it)
131 argument.
133 Iteration over things to be displayed is then simple. It is
134 started by initializing an iterator with a call to init_iterator.
135 Calls to get_next_display_element fill the iterator structure with
136 relevant information about the next thing to display. Calls to
137 set_iterator_to_next move the iterator to the next thing.
139 Besides this, an iterator also contains information about the
140 display environment in which glyphs for display elements are to be
141 produced. It has fields for the width and height of the display,
142 the information whether long lines are truncated or continued, a
143 current X and Y position, and lots of other stuff you can better
144 see in dispextern.h.
146 Glyphs in a desired matrix are normally constructed in a loop
147 calling get_next_display_element and then PRODUCE_GLYPHS. The call
148 to PRODUCE_GLYPHS will fill the iterator structure with pixel
149 information about the element being displayed and at the same time
150 produce glyphs for it. If the display element fits on the line
151 being displayed, set_iterator_to_next is called next, otherwise the
152 glyphs produced are discarded. The function display_line is the
153 workhorse of filling glyph rows in the desired matrix with glyphs.
154 In addition to producing glyphs, it also handles line truncation
155 and continuation, word wrap, and cursor positioning (for the
156 latter, see also set_cursor_from_row).
158 Frame matrices.
160 That just couldn't be all, could it? What about terminal types not
161 supporting operations on sub-windows of the screen? To update the
162 display on such a terminal, window-based glyph matrices are not
163 well suited. To be able to reuse part of the display (scrolling
164 lines up and down), we must instead have a view of the whole
165 screen. This is what `frame matrices' are for. They are a trick.
167 Frames on terminals like above have a glyph pool. Windows on such
168 a frame sub-allocate their glyph memory from their frame's glyph
169 pool. The frame itself is given its own glyph matrices. By
170 coincidence---or maybe something else---rows in window glyph
171 matrices are slices of corresponding rows in frame matrices. Thus
172 writing to window matrices implicitly updates a frame matrix which
173 provides us with the view of the whole screen that we originally
174 wanted to have without having to move many bytes around. To be
175 honest, there is a little bit more done, but not much more. If you
176 plan to extend that code, take a look at dispnew.c. The function
177 build_frame_matrix is a good starting point.
179 Bidirectional display.
181 Bidirectional display adds quite some hair to this already complex
182 design. The good news are that a large portion of that hairy stuff
183 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
184 reordering engine which is called by set_iterator_to_next and
185 returns the next character to display in the visual order. See
186 commentary on bidi.c for more details. As far as redisplay is
187 concerned, the effect of calling bidi_move_to_visually_next, the
188 main interface of the reordering engine, is that the iterator gets
189 magically placed on the buffer or string position that is to be
190 displayed next. In other words, a linear iteration through the
191 buffer/string is replaced with a non-linear one. All the rest of
192 the redisplay is oblivious to the bidi reordering.
194 Well, almost oblivious---there are still complications, most of
195 them due to the fact that buffer and string positions no longer
196 change monotonously with glyph indices in a glyph row. Moreover,
197 for continued lines, the buffer positions may not even be
198 monotonously changing with vertical positions. Also, accounting
199 for face changes, overlays, etc. becomes more complex because
200 non-linear iteration could potentially skip many positions with
201 changes, and then cross them again on the way back...
203 One other prominent effect of bidirectional display is that some
204 paragraphs of text need to be displayed starting at the right
205 margin of the window---the so-called right-to-left, or R2L
206 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
207 which have their reversed_p flag set. The bidi reordering engine
208 produces characters in such rows starting from the character which
209 should be the rightmost on display. PRODUCE_GLYPHS then reverses
210 the order, when it fills up the glyph row whose reversed_p flag is
211 set, by prepending each new glyph to what is already there, instead
212 of appending it. When the glyph row is complete, the function
213 extend_face_to_end_of_line fills the empty space to the left of the
214 leftmost character with special glyphs, which will display as,
215 well, empty. On text terminals, these special glyphs are simply
216 blank characters. On graphics terminals, there's a single stretch
217 glyph with suitably computed width. Both the blanks and the
218 stretch glyph are given the face of the background of the line.
219 This way, the terminal-specific back-end can still draw the glyphs
220 left to right, even for R2L lines.
222 Note one important detail mentioned above: that the bidi reordering
223 engine, driven by the iterator, produces characters in R2L rows
224 starting at the character that will be the rightmost on display.
225 As far as the iterator is concerned, the geometry of such rows is
226 still left to right, i.e. the iterator "thinks" the first character
227 is at the leftmost pixel position. The iterator does not know that
228 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
229 delivers. This is important when functions from the the move_it_*
230 family are used to get to certain screen position or to match
231 screen coordinates with buffer coordinates: these functions use the
232 iterator geometry, which is left to right even in R2L paragraphs.
233 This works well with most callers of move_it_*, because they need
234 to get to a specific column, and columns are still numbered in the
235 reading order, i.e. the rightmost character in a R2L paragraph is
236 still column zero. But some callers do not get well with this; a
237 notable example is mouse clicks that need to find the character
238 that corresponds to certain pixel coordinates. See
239 buffer_posn_from_coords in dispnew.c for how this is handled. */
241 #include <config.h>
242 #include <stdio.h>
243 #include <limits.h>
244 #include <setjmp.h>
246 #include "lisp.h"
247 #include "keyboard.h"
248 #include "frame.h"
249 #include "window.h"
250 #include "termchar.h"
251 #include "dispextern.h"
252 #include "buffer.h"
253 #include "character.h"
254 #include "charset.h"
255 #include "indent.h"
256 #include "commands.h"
257 #include "keymap.h"
258 #include "macros.h"
259 #include "disptab.h"
260 #include "termhooks.h"
261 #include "termopts.h"
262 #include "intervals.h"
263 #include "coding.h"
264 #include "process.h"
265 #include "region-cache.h"
266 #include "font.h"
267 #include "fontset.h"
268 #include "blockinput.h"
270 #ifdef HAVE_X_WINDOWS
271 #include "xterm.h"
272 #endif
273 #ifdef WINDOWSNT
274 #include "w32term.h"
275 #endif
276 #ifdef HAVE_NS
277 #include "nsterm.h"
278 #endif
279 #ifdef USE_GTK
280 #include "gtkutil.h"
281 #endif
283 #include "font.h"
285 #ifndef FRAME_X_OUTPUT
286 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
287 #endif
289 #define INFINITY 10000000
291 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
292 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
293 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
294 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
295 Lisp_Object Qinhibit_point_motion_hooks;
296 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
297 Lisp_Object Qfontified;
298 Lisp_Object Qgrow_only;
299 Lisp_Object Qinhibit_eval_during_redisplay;
300 Lisp_Object Qbuffer_position, Qposition, Qobject;
301 Lisp_Object Qright_to_left, Qleft_to_right;
303 /* Cursor shapes */
304 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
306 /* Pointer shapes */
307 Lisp_Object Qarrow, Qhand, Qtext;
309 Lisp_Object Qrisky_local_variable;
311 /* Holds the list (error). */
312 Lisp_Object list_of_error;
314 /* Functions called to fontify regions of text. */
316 Lisp_Object Vfontification_functions;
317 Lisp_Object Qfontification_functions;
319 /* Non-nil means automatically select any window when the mouse
320 cursor moves into it. */
321 Lisp_Object Vmouse_autoselect_window;
323 Lisp_Object Vwrap_prefix, Qwrap_prefix;
324 Lisp_Object Vline_prefix, Qline_prefix;
326 /* Non-zero means draw tool bar buttons raised when the mouse moves
327 over them. */
329 int auto_raise_tool_bar_buttons_p;
331 /* Non-zero means to reposition window if cursor line is only partially visible. */
333 int make_cursor_line_fully_visible_p;
335 /* Margin below tool bar in pixels. 0 or nil means no margin.
336 If value is `internal-border-width' or `border-width',
337 the corresponding frame parameter is used. */
339 Lisp_Object Vtool_bar_border;
341 /* Margin around tool bar buttons in pixels. */
343 Lisp_Object Vtool_bar_button_margin;
345 /* Thickness of shadow to draw around tool bar buttons. */
347 EMACS_INT tool_bar_button_relief;
349 /* Non-nil means automatically resize tool-bars so that all tool-bar
350 items are visible, and no blank lines remain.
352 If value is `grow-only', only make tool-bar bigger. */
354 Lisp_Object Vauto_resize_tool_bars;
356 /* Type of tool bar. Can be symbols image, text, both or both-hroiz. */
358 Lisp_Object Vtool_bar_style;
360 /* Maximum number of characters a label can have to be shown. */
362 EMACS_INT tool_bar_max_label_size;
364 /* Non-zero means draw block and hollow cursor as wide as the glyph
365 under it. For example, if a block cursor is over a tab, it will be
366 drawn as wide as that tab on the display. */
368 int x_stretch_cursor_p;
370 /* Non-nil means don't actually do any redisplay. */
372 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
374 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
376 int inhibit_eval_during_redisplay;
378 /* Names of text properties relevant for redisplay. */
380 Lisp_Object Qdisplay;
382 /* Symbols used in text property values. */
384 Lisp_Object Vdisplay_pixels_per_inch;
385 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
386 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
387 Lisp_Object Qslice;
388 Lisp_Object Qcenter;
389 Lisp_Object Qmargin, Qpointer;
390 Lisp_Object Qline_height;
392 /* Non-nil means highlight trailing whitespace. */
394 Lisp_Object Vshow_trailing_whitespace;
396 /* Non-nil means escape non-break space and hyphens. */
398 Lisp_Object Vnobreak_char_display;
400 #ifdef HAVE_WINDOW_SYSTEM
402 /* Test if overflow newline into fringe. Called with iterator IT
403 at or past right window margin, and with IT->current_x set. */
405 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
406 (!NILP (Voverflow_newline_into_fringe) \
407 && FRAME_WINDOW_P ((IT)->f) \
408 && ((IT)->bidi_it.paragraph_dir == R2L \
409 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
410 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
411 && (IT)->current_x == (IT)->last_visible_x \
412 && (IT)->line_wrap != WORD_WRAP)
414 #else /* !HAVE_WINDOW_SYSTEM */
415 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
416 #endif /* HAVE_WINDOW_SYSTEM */
418 /* Test if the display element loaded in IT is a space or tab
419 character. This is used to determine word wrapping. */
421 #define IT_DISPLAYING_WHITESPACE(it) \
422 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
424 /* Non-nil means show the text cursor in void text areas
425 i.e. in blank areas after eol and eob. This used to be
426 the default in 21.3. */
428 Lisp_Object Vvoid_text_area_pointer;
430 /* Name of the face used to highlight trailing whitespace. */
432 Lisp_Object Qtrailing_whitespace;
434 /* Name and number of the face used to highlight escape glyphs. */
436 Lisp_Object Qescape_glyph;
438 /* Name and number of the face used to highlight non-breaking spaces. */
440 Lisp_Object Qnobreak_space;
442 /* The symbol `image' which is the car of the lists used to represent
443 images in Lisp. Also a tool bar style. */
445 Lisp_Object Qimage;
447 /* The image map types. */
448 Lisp_Object QCmap, QCpointer;
449 Lisp_Object Qrect, Qcircle, Qpoly;
451 /* Tool bar styles */
452 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
454 /* Non-zero means print newline to stdout before next mini-buffer
455 message. */
457 int noninteractive_need_newline;
459 /* Non-zero means print newline to message log before next message. */
461 static int message_log_need_newline;
463 /* Three markers that message_dolog uses.
464 It could allocate them itself, but that causes trouble
465 in handling memory-full errors. */
466 static Lisp_Object message_dolog_marker1;
467 static Lisp_Object message_dolog_marker2;
468 static Lisp_Object message_dolog_marker3;
470 /* The buffer position of the first character appearing entirely or
471 partially on the line of the selected window which contains the
472 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
473 redisplay optimization in redisplay_internal. */
475 static struct text_pos this_line_start_pos;
477 /* Number of characters past the end of the line above, including the
478 terminating newline. */
480 static struct text_pos this_line_end_pos;
482 /* The vertical positions and the height of this line. */
484 static int this_line_vpos;
485 static int this_line_y;
486 static int this_line_pixel_height;
488 /* X position at which this display line starts. Usually zero;
489 negative if first character is partially visible. */
491 static int this_line_start_x;
493 /* Buffer that this_line_.* variables are referring to. */
495 static struct buffer *this_line_buffer;
497 /* Nonzero means truncate lines in all windows less wide than the
498 frame. */
500 Lisp_Object Vtruncate_partial_width_windows;
502 /* A flag to control how to display unibyte 8-bit character. */
504 int unibyte_display_via_language_environment;
506 /* Nonzero means we have more than one non-mini-buffer-only frame.
507 Not guaranteed to be accurate except while parsing
508 frame-title-format. */
510 int multiple_frames;
512 Lisp_Object Vglobal_mode_string;
515 /* List of variables (symbols) which hold markers for overlay arrows.
516 The symbols on this list are examined during redisplay to determine
517 where to display overlay arrows. */
519 Lisp_Object Voverlay_arrow_variable_list;
521 /* Marker for where to display an arrow on top of the buffer text. */
523 Lisp_Object Voverlay_arrow_position;
525 /* String to display for the arrow. Only used on terminal frames. */
527 Lisp_Object Voverlay_arrow_string;
529 /* Values of those variables at last redisplay are stored as
530 properties on `overlay-arrow-position' symbol. However, if
531 Voverlay_arrow_position is a marker, last-arrow-position is its
532 numerical position. */
534 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
536 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
537 properties on a symbol in overlay-arrow-variable-list. */
539 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
541 /* Like mode-line-format, but for the title bar on a visible frame. */
543 Lisp_Object Vframe_title_format;
545 /* Like mode-line-format, but for the title bar on an iconified frame. */
547 Lisp_Object Vicon_title_format;
549 /* List of functions to call when a window's size changes. These
550 functions get one arg, a frame on which one or more windows' sizes
551 have changed. */
553 static Lisp_Object Vwindow_size_change_functions;
555 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
557 /* Nonzero if an overlay arrow has been displayed in this window. */
559 static int overlay_arrow_seen;
561 /* Nonzero means highlight the region even in nonselected windows. */
563 int highlight_nonselected_windows;
565 /* If cursor motion alone moves point off frame, try scrolling this
566 many lines up or down if that will bring it back. */
568 static EMACS_INT scroll_step;
570 /* Nonzero means scroll just far enough to bring point back on the
571 screen, when appropriate. */
573 static EMACS_INT scroll_conservatively;
575 /* Recenter the window whenever point gets within this many lines of
576 the top or bottom of the window. This value is translated into a
577 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
578 that there is really a fixed pixel height scroll margin. */
580 EMACS_INT scroll_margin;
582 /* Number of windows showing the buffer of the selected window (or
583 another buffer with the same base buffer). keyboard.c refers to
584 this. */
586 int buffer_shared;
588 /* Vector containing glyphs for an ellipsis `...'. */
590 static Lisp_Object default_invis_vector[3];
592 /* Zero means display the mode-line/header-line/menu-bar in the default face
593 (this slightly odd definition is for compatibility with previous versions
594 of emacs), non-zero means display them using their respective faces.
596 This variable is deprecated. */
598 int mode_line_inverse_video;
600 /* Prompt to display in front of the mini-buffer contents. */
602 Lisp_Object minibuf_prompt;
604 /* Width of current mini-buffer prompt. Only set after display_line
605 of the line that contains the prompt. */
607 int minibuf_prompt_width;
609 /* This is the window where the echo area message was displayed. It
610 is always a mini-buffer window, but it may not be the same window
611 currently active as a mini-buffer. */
613 Lisp_Object echo_area_window;
615 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
616 pushes the current message and the value of
617 message_enable_multibyte on the stack, the function restore_message
618 pops the stack and displays MESSAGE again. */
620 Lisp_Object Vmessage_stack;
622 /* Nonzero means multibyte characters were enabled when the echo area
623 message was specified. */
625 int message_enable_multibyte;
627 /* Nonzero if we should redraw the mode lines on the next redisplay. */
629 int update_mode_lines;
631 /* Nonzero if window sizes or contents have changed since last
632 redisplay that finished. */
634 int windows_or_buffers_changed;
636 /* Nonzero means a frame's cursor type has been changed. */
638 int cursor_type_changed;
640 /* Nonzero after display_mode_line if %l was used and it displayed a
641 line number. */
643 int line_number_displayed;
645 /* Maximum buffer size for which to display line numbers. */
647 Lisp_Object Vline_number_display_limit;
649 /* Line width to consider when repositioning for line number display. */
651 static EMACS_INT line_number_display_limit_width;
653 /* Number of lines to keep in the message log buffer. t means
654 infinite. nil means don't log at all. */
656 Lisp_Object Vmessage_log_max;
658 /* The name of the *Messages* buffer, a string. */
660 static Lisp_Object Vmessages_buffer_name;
662 /* Current, index 0, and last displayed echo area message. Either
663 buffers from echo_buffers, or nil to indicate no message. */
665 Lisp_Object echo_area_buffer[2];
667 /* The buffers referenced from echo_area_buffer. */
669 static Lisp_Object echo_buffer[2];
671 /* A vector saved used in with_area_buffer to reduce consing. */
673 static Lisp_Object Vwith_echo_area_save_vector;
675 /* Non-zero means display_echo_area should display the last echo area
676 message again. Set by redisplay_preserve_echo_area. */
678 static int display_last_displayed_message_p;
680 /* Nonzero if echo area is being used by print; zero if being used by
681 message. */
683 int message_buf_print;
685 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
687 Lisp_Object Qinhibit_menubar_update;
688 int inhibit_menubar_update;
690 /* When evaluating expressions from menu bar items (enable conditions,
691 for instance), this is the frame they are being processed for. */
693 Lisp_Object Vmenu_updating_frame;
695 /* Maximum height for resizing mini-windows. Either a float
696 specifying a fraction of the available height, or an integer
697 specifying a number of lines. */
699 Lisp_Object Vmax_mini_window_height;
701 /* Non-zero means messages should be displayed with truncated
702 lines instead of being continued. */
704 int message_truncate_lines;
705 Lisp_Object Qmessage_truncate_lines;
707 /* Set to 1 in clear_message to make redisplay_internal aware
708 of an emptied echo area. */
710 static int message_cleared_p;
712 /* How to blink the default frame cursor off. */
713 Lisp_Object Vblink_cursor_alist;
715 /* A scratch glyph row with contents used for generating truncation
716 glyphs. Also used in direct_output_for_insert. */
718 #define MAX_SCRATCH_GLYPHS 100
719 struct glyph_row scratch_glyph_row;
720 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
722 /* Ascent and height of the last line processed by move_it_to. */
724 static int last_max_ascent, last_height;
726 /* Non-zero if there's a help-echo in the echo area. */
728 int help_echo_showing_p;
730 /* If >= 0, computed, exact values of mode-line and header-line height
731 to use in the macros CURRENT_MODE_LINE_HEIGHT and
732 CURRENT_HEADER_LINE_HEIGHT. */
734 int current_mode_line_height, current_header_line_height;
736 /* The maximum distance to look ahead for text properties. Values
737 that are too small let us call compute_char_face and similar
738 functions too often which is expensive. Values that are too large
739 let us call compute_char_face and alike too often because we
740 might not be interested in text properties that far away. */
742 #define TEXT_PROP_DISTANCE_LIMIT 100
744 #if GLYPH_DEBUG
746 /* Variables to turn off display optimizations from Lisp. */
748 int inhibit_try_window_id, inhibit_try_window_reusing;
749 int inhibit_try_cursor_movement;
751 /* Non-zero means print traces of redisplay if compiled with
752 GLYPH_DEBUG != 0. */
754 int trace_redisplay_p;
756 #endif /* GLYPH_DEBUG */
758 #ifdef DEBUG_TRACE_MOVE
759 /* Non-zero means trace with TRACE_MOVE to stderr. */
760 int trace_move;
762 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
763 #else
764 #define TRACE_MOVE(x) (void) 0
765 #endif
767 /* Non-zero means automatically scroll windows horizontally to make
768 point visible. */
770 int automatic_hscrolling_p;
771 Lisp_Object Qauto_hscroll_mode;
773 /* How close to the margin can point get before the window is scrolled
774 horizontally. */
775 EMACS_INT hscroll_margin;
777 /* How much to scroll horizontally when point is inside the above margin. */
778 Lisp_Object Vhscroll_step;
780 /* The variable `resize-mini-windows'. If nil, don't resize
781 mini-windows. If t, always resize them to fit the text they
782 display. If `grow-only', let mini-windows grow only until they
783 become empty. */
785 Lisp_Object Vresize_mini_windows;
787 /* Buffer being redisplayed -- for redisplay_window_error. */
789 struct buffer *displayed_buffer;
791 /* Space between overline and text. */
793 EMACS_INT overline_margin;
795 /* Require underline to be at least this many screen pixels below baseline
796 This to avoid underline "merging" with the base of letters at small
797 font sizes, particularly when x_use_underline_position_properties is on. */
799 EMACS_INT underline_minimum_offset;
801 /* Value returned from text property handlers (see below). */
803 enum prop_handled
805 HANDLED_NORMALLY,
806 HANDLED_RECOMPUTE_PROPS,
807 HANDLED_OVERLAY_STRING_CONSUMED,
808 HANDLED_RETURN
811 /* A description of text properties that redisplay is interested
812 in. */
814 struct props
816 /* The name of the property. */
817 Lisp_Object *name;
819 /* A unique index for the property. */
820 enum prop_idx idx;
822 /* A handler function called to set up iterator IT from the property
823 at IT's current position. Value is used to steer handle_stop. */
824 enum prop_handled (*handler) (struct it *it);
827 static enum prop_handled handle_face_prop (struct it *);
828 static enum prop_handled handle_invisible_prop (struct it *);
829 static enum prop_handled handle_display_prop (struct it *);
830 static enum prop_handled handle_composition_prop (struct it *);
831 static enum prop_handled handle_overlay_change (struct it *);
832 static enum prop_handled handle_fontified_prop (struct it *);
834 /* Properties handled by iterators. */
836 static struct props it_props[] =
838 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
839 /* Handle `face' before `display' because some sub-properties of
840 `display' need to know the face. */
841 {&Qface, FACE_PROP_IDX, handle_face_prop},
842 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
843 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
844 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
845 {NULL, 0, NULL}
848 /* Value is the position described by X. If X is a marker, value is
849 the marker_position of X. Otherwise, value is X. */
851 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
853 /* Enumeration returned by some move_it_.* functions internally. */
855 enum move_it_result
857 /* Not used. Undefined value. */
858 MOVE_UNDEFINED,
860 /* Move ended at the requested buffer position or ZV. */
861 MOVE_POS_MATCH_OR_ZV,
863 /* Move ended at the requested X pixel position. */
864 MOVE_X_REACHED,
866 /* Move within a line ended at the end of a line that must be
867 continued. */
868 MOVE_LINE_CONTINUED,
870 /* Move within a line ended at the end of a line that would
871 be displayed truncated. */
872 MOVE_LINE_TRUNCATED,
874 /* Move within a line ended at a line end. */
875 MOVE_NEWLINE_OR_CR
878 /* This counter is used to clear the face cache every once in a while
879 in redisplay_internal. It is incremented for each redisplay.
880 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
881 cleared. */
883 #define CLEAR_FACE_CACHE_COUNT 500
884 static int clear_face_cache_count;
886 /* Similarly for the image cache. */
888 #ifdef HAVE_WINDOW_SYSTEM
889 #define CLEAR_IMAGE_CACHE_COUNT 101
890 static int clear_image_cache_count;
891 #endif
893 /* Non-zero while redisplay_internal is in progress. */
895 int redisplaying_p;
897 /* Non-zero means don't free realized faces. Bound while freeing
898 realized faces is dangerous because glyph matrices might still
899 reference them. */
901 int inhibit_free_realized_faces;
902 Lisp_Object Qinhibit_free_realized_faces;
904 /* If a string, XTread_socket generates an event to display that string.
905 (The display is done in read_char.) */
907 Lisp_Object help_echo_string;
908 Lisp_Object help_echo_window;
909 Lisp_Object help_echo_object;
910 EMACS_INT help_echo_pos;
912 /* Temporary variable for XTread_socket. */
914 Lisp_Object previous_help_echo_string;
916 /* Null glyph slice */
918 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
920 /* Platform-independent portion of hourglass implementation. */
922 /* Non-zero means we're allowed to display a hourglass pointer. */
923 int display_hourglass_p;
925 /* Non-zero means an hourglass cursor is currently shown. */
926 int hourglass_shown_p;
928 /* If non-null, an asynchronous timer that, when it expires, displays
929 an hourglass cursor on all frames. */
930 struct atimer *hourglass_atimer;
932 /* Number of seconds to wait before displaying an hourglass cursor. */
933 Lisp_Object Vhourglass_delay;
935 /* Default number of seconds to wait before displaying an hourglass
936 cursor. */
937 #define DEFAULT_HOURGLASS_DELAY 1
940 /* Function prototypes. */
942 static void setup_for_ellipsis (struct it *, int);
943 static void mark_window_display_accurate_1 (struct window *, int);
944 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
945 static int display_prop_string_p (Lisp_Object, Lisp_Object);
946 static int cursor_row_p (struct window *, struct glyph_row *);
947 static int redisplay_mode_lines (Lisp_Object, int);
948 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
950 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
952 static void handle_line_prefix (struct it *);
954 static void pint2str (char *, int, int);
955 static void pint2hrstr (char *, int, int);
956 static struct text_pos run_window_scroll_functions (Lisp_Object,
957 struct text_pos);
958 static void reconsider_clip_changes (struct window *, struct buffer *);
959 static int text_outside_line_unchanged_p (struct window *,
960 EMACS_INT, EMACS_INT);
961 static void store_mode_line_noprop_char (char);
962 static int store_mode_line_noprop (const unsigned char *, int, int);
963 static void x_consider_frame_title (Lisp_Object);
964 static void handle_stop (struct it *);
965 static void handle_stop_backwards (struct it *, EMACS_INT);
966 static int tool_bar_lines_needed (struct frame *, int *);
967 static int single_display_spec_intangible_p (Lisp_Object);
968 static void ensure_echo_area_buffers (void);
969 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
970 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
971 static int with_echo_area_buffer (struct window *, int,
972 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
973 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
974 static void clear_garbaged_frames (void);
975 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
976 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
977 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
978 static int display_echo_area (struct window *);
979 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
980 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
981 static Lisp_Object unwind_redisplay (Lisp_Object);
982 static int string_char_and_length (const unsigned char *, int *);
983 static struct text_pos display_prop_end (struct it *, Lisp_Object,
984 struct text_pos);
985 static int compute_window_start_on_continuation_line (struct window *);
986 static Lisp_Object safe_eval_handler (Lisp_Object);
987 static void insert_left_trunc_glyphs (struct it *);
988 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
989 Lisp_Object);
990 static void extend_face_to_end_of_line (struct it *);
991 static int append_space_for_newline (struct it *, int);
992 static int cursor_row_fully_visible_p (struct window *, int, int);
993 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
994 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
995 static int trailing_whitespace_p (EMACS_INT);
996 static int message_log_check_duplicate (EMACS_INT, EMACS_INT,
997 EMACS_INT, EMACS_INT);
998 static void push_it (struct it *);
999 static void pop_it (struct it *);
1000 static void sync_frame_with_window_matrix_rows (struct window *);
1001 static void select_frame_for_redisplay (Lisp_Object);
1002 static void redisplay_internal (int);
1003 static int echo_area_display (int);
1004 static void redisplay_windows (Lisp_Object);
1005 static void redisplay_window (Lisp_Object, int);
1006 static Lisp_Object redisplay_window_error (Lisp_Object);
1007 static Lisp_Object redisplay_window_0 (Lisp_Object);
1008 static Lisp_Object redisplay_window_1 (Lisp_Object);
1009 static int update_menu_bar (struct frame *, int, int);
1010 static int try_window_reusing_current_matrix (struct window *);
1011 static int try_window_id (struct window *);
1012 static int display_line (struct it *);
1013 static int display_mode_lines (struct window *);
1014 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
1015 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
1016 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
1017 static const char *decode_mode_spec (struct window *, int, int, int,
1018 Lisp_Object *);
1019 static void display_menu_bar (struct window *);
1020 static int display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT, int,
1021 EMACS_INT *);
1022 static int display_string (const unsigned char *, Lisp_Object, Lisp_Object,
1023 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
1024 static void compute_line_metrics (struct it *);
1025 static void run_redisplay_end_trigger_hook (struct it *);
1026 static int get_overlay_strings (struct it *, EMACS_INT);
1027 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
1028 static void next_overlay_string (struct it *);
1029 static void reseat (struct it *, struct text_pos, int);
1030 static void reseat_1 (struct it *, struct text_pos, int);
1031 static void back_to_previous_visible_line_start (struct it *);
1032 void reseat_at_previous_visible_line_start (struct it *);
1033 static void reseat_at_next_visible_line_start (struct it *, int);
1034 static int next_element_from_ellipsis (struct it *);
1035 static int next_element_from_display_vector (struct it *);
1036 static int next_element_from_string (struct it *);
1037 static int next_element_from_c_string (struct it *);
1038 static int next_element_from_buffer (struct it *);
1039 static int next_element_from_composition (struct it *);
1040 static int next_element_from_image (struct it *);
1041 static int next_element_from_stretch (struct it *);
1042 static void load_overlay_strings (struct it *, EMACS_INT);
1043 static int init_from_display_pos (struct it *, struct window *,
1044 struct display_pos *);
1045 static void reseat_to_string (struct it *, const unsigned char *,
1046 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
1047 static enum move_it_result
1048 move_it_in_display_line_to (struct it *, EMACS_INT, int,
1049 enum move_operation_enum);
1050 void move_it_vertically_backward (struct it *, int);
1051 static void init_to_row_start (struct it *, struct window *,
1052 struct glyph_row *);
1053 static int init_to_row_end (struct it *, struct window *,
1054 struct glyph_row *);
1055 static void back_to_previous_line_start (struct it *);
1056 static int forward_to_next_line_start (struct it *, int *);
1057 static struct text_pos string_pos_nchars_ahead (struct text_pos,
1058 Lisp_Object, EMACS_INT);
1059 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
1060 static struct text_pos c_string_pos (EMACS_INT, const unsigned char *, int);
1061 static EMACS_INT number_of_chars (const unsigned char *, int);
1062 static void compute_stop_pos (struct it *);
1063 static void compute_string_pos (struct text_pos *, struct text_pos,
1064 Lisp_Object);
1065 static int face_before_or_after_it_pos (struct it *, int);
1066 static EMACS_INT next_overlay_change (EMACS_INT);
1067 static int handle_single_display_spec (struct it *, Lisp_Object,
1068 Lisp_Object, Lisp_Object,
1069 struct text_pos *, int);
1070 static int underlying_face_id (struct it *);
1071 static int in_ellipses_for_invisible_text_p (struct display_pos *,
1072 struct window *);
1074 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1075 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1077 #ifdef HAVE_WINDOW_SYSTEM
1079 static void update_tool_bar (struct frame *, int);
1080 static void build_desired_tool_bar_string (struct frame *f);
1081 static int redisplay_tool_bar (struct frame *);
1082 static void display_tool_bar_line (struct it *, int);
1083 static void notice_overwritten_cursor (struct window *,
1084 enum glyph_row_area,
1085 int, int, int, int);
1086 static void append_stretch_glyph (struct it *, Lisp_Object,
1087 int, int, int);
1091 #endif /* HAVE_WINDOW_SYSTEM */
1094 /***********************************************************************
1095 Window display dimensions
1096 ***********************************************************************/
1098 /* Return the bottom boundary y-position for text lines in window W.
1099 This is the first y position at which a line cannot start.
1100 It is relative to the top of the window.
1102 This is the height of W minus the height of a mode line, if any. */
1104 INLINE int
1105 window_text_bottom_y (struct window *w)
1107 int height = WINDOW_TOTAL_HEIGHT (w);
1109 if (WINDOW_WANTS_MODELINE_P (w))
1110 height -= CURRENT_MODE_LINE_HEIGHT (w);
1111 return height;
1114 /* Return the pixel width of display area AREA of window W. AREA < 0
1115 means return the total width of W, not including fringes to
1116 the left and right of the window. */
1118 INLINE int
1119 window_box_width (struct window *w, int area)
1121 int cols = XFASTINT (w->total_cols);
1122 int pixels = 0;
1124 if (!w->pseudo_window_p)
1126 cols -= WINDOW_SCROLL_BAR_COLS (w);
1128 if (area == TEXT_AREA)
1130 if (INTEGERP (w->left_margin_cols))
1131 cols -= XFASTINT (w->left_margin_cols);
1132 if (INTEGERP (w->right_margin_cols))
1133 cols -= XFASTINT (w->right_margin_cols);
1134 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1136 else if (area == LEFT_MARGIN_AREA)
1138 cols = (INTEGERP (w->left_margin_cols)
1139 ? XFASTINT (w->left_margin_cols) : 0);
1140 pixels = 0;
1142 else if (area == RIGHT_MARGIN_AREA)
1144 cols = (INTEGERP (w->right_margin_cols)
1145 ? XFASTINT (w->right_margin_cols) : 0);
1146 pixels = 0;
1150 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1154 /* Return the pixel height of the display area of window W, not
1155 including mode lines of W, if any. */
1157 INLINE int
1158 window_box_height (struct window *w)
1160 struct frame *f = XFRAME (w->frame);
1161 int height = WINDOW_TOTAL_HEIGHT (w);
1163 xassert (height >= 0);
1165 /* Note: the code below that determines the mode-line/header-line
1166 height is essentially the same as that contained in the macro
1167 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1168 the appropriate glyph row has its `mode_line_p' flag set,
1169 and if it doesn't, uses estimate_mode_line_height instead. */
1171 if (WINDOW_WANTS_MODELINE_P (w))
1173 struct glyph_row *ml_row
1174 = (w->current_matrix && w->current_matrix->rows
1175 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1176 : 0);
1177 if (ml_row && ml_row->mode_line_p)
1178 height -= ml_row->height;
1179 else
1180 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1183 if (WINDOW_WANTS_HEADER_LINE_P (w))
1185 struct glyph_row *hl_row
1186 = (w->current_matrix && w->current_matrix->rows
1187 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1188 : 0);
1189 if (hl_row && hl_row->mode_line_p)
1190 height -= hl_row->height;
1191 else
1192 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1195 /* With a very small font and a mode-line that's taller than
1196 default, we might end up with a negative height. */
1197 return max (0, height);
1200 /* Return the window-relative coordinate of the left edge of display
1201 area AREA of window W. AREA < 0 means return the left edge of the
1202 whole window, to the right of the left fringe of W. */
1204 INLINE int
1205 window_box_left_offset (struct window *w, int area)
1207 int x;
1209 if (w->pseudo_window_p)
1210 return 0;
1212 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1214 if (area == TEXT_AREA)
1215 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1216 + window_box_width (w, LEFT_MARGIN_AREA));
1217 else if (area == RIGHT_MARGIN_AREA)
1218 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1219 + window_box_width (w, LEFT_MARGIN_AREA)
1220 + window_box_width (w, TEXT_AREA)
1221 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1223 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1224 else if (area == LEFT_MARGIN_AREA
1225 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1226 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1228 return x;
1232 /* Return the window-relative coordinate of the right edge of display
1233 area AREA of window W. AREA < 0 means return the right edge of the
1234 whole window, to the left of the right fringe of W. */
1236 INLINE int
1237 window_box_right_offset (struct window *w, int area)
1239 return window_box_left_offset (w, area) + window_box_width (w, area);
1242 /* Return the frame-relative coordinate of the left edge of display
1243 area AREA of window W. AREA < 0 means return the left edge of the
1244 whole window, to the right of the left fringe of W. */
1246 INLINE int
1247 window_box_left (struct window *w, int area)
1249 struct frame *f = XFRAME (w->frame);
1250 int x;
1252 if (w->pseudo_window_p)
1253 return FRAME_INTERNAL_BORDER_WIDTH (f);
1255 x = (WINDOW_LEFT_EDGE_X (w)
1256 + window_box_left_offset (w, area));
1258 return x;
1262 /* Return the frame-relative coordinate of the right edge of display
1263 area AREA of window W. AREA < 0 means return the right edge of the
1264 whole window, to the left of the right fringe of W. */
1266 INLINE int
1267 window_box_right (struct window *w, int area)
1269 return window_box_left (w, area) + window_box_width (w, area);
1272 /* Get the bounding box of the display area AREA of window W, without
1273 mode lines, in frame-relative coordinates. AREA < 0 means the
1274 whole window, not including the left and right fringes of
1275 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1276 coordinates of the upper-left corner of the box. Return in
1277 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1279 INLINE void
1280 window_box (struct window *w, int area, int *box_x, int *box_y,
1281 int *box_width, int *box_height)
1283 if (box_width)
1284 *box_width = window_box_width (w, area);
1285 if (box_height)
1286 *box_height = window_box_height (w);
1287 if (box_x)
1288 *box_x = window_box_left (w, area);
1289 if (box_y)
1291 *box_y = WINDOW_TOP_EDGE_Y (w);
1292 if (WINDOW_WANTS_HEADER_LINE_P (w))
1293 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1298 /* Get the bounding box of the display area AREA of window W, without
1299 mode lines. AREA < 0 means the whole window, not including the
1300 left and right fringe of the window. Return in *TOP_LEFT_X
1301 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1302 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1303 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1304 box. */
1306 INLINE void
1307 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1308 int *bottom_right_x, int *bottom_right_y)
1310 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1311 bottom_right_y);
1312 *bottom_right_x += *top_left_x;
1313 *bottom_right_y += *top_left_y;
1318 /***********************************************************************
1319 Utilities
1320 ***********************************************************************/
1322 /* Return the bottom y-position of the line the iterator IT is in.
1323 This can modify IT's settings. */
1326 line_bottom_y (struct it *it)
1328 int line_height = it->max_ascent + it->max_descent;
1329 int line_top_y = it->current_y;
1331 if (line_height == 0)
1333 if (last_height)
1334 line_height = last_height;
1335 else if (IT_CHARPOS (*it) < ZV)
1337 move_it_by_lines (it, 1, 1);
1338 line_height = (it->max_ascent || it->max_descent
1339 ? it->max_ascent + it->max_descent
1340 : last_height);
1342 else
1344 struct glyph_row *row = it->glyph_row;
1346 /* Use the default character height. */
1347 it->glyph_row = NULL;
1348 it->what = IT_CHARACTER;
1349 it->c = ' ';
1350 it->len = 1;
1351 PRODUCE_GLYPHS (it);
1352 line_height = it->ascent + it->descent;
1353 it->glyph_row = row;
1357 return line_top_y + line_height;
1361 /* Return 1 if position CHARPOS is visible in window W.
1362 CHARPOS < 0 means return info about WINDOW_END position.
1363 If visible, set *X and *Y to pixel coordinates of top left corner.
1364 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1365 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1368 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1369 int *rtop, int *rbot, int *rowh, int *vpos)
1371 struct it it;
1372 struct text_pos top;
1373 int visible_p = 0;
1374 struct buffer *old_buffer = NULL;
1376 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1377 return visible_p;
1379 if (XBUFFER (w->buffer) != current_buffer)
1381 old_buffer = current_buffer;
1382 set_buffer_internal_1 (XBUFFER (w->buffer));
1385 SET_TEXT_POS_FROM_MARKER (top, w->start);
1387 /* Compute exact mode line heights. */
1388 if (WINDOW_WANTS_MODELINE_P (w))
1389 current_mode_line_height
1390 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1391 current_buffer->mode_line_format);
1393 if (WINDOW_WANTS_HEADER_LINE_P (w))
1394 current_header_line_height
1395 = display_mode_line (w, HEADER_LINE_FACE_ID,
1396 current_buffer->header_line_format);
1398 start_display (&it, w, top);
1399 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1400 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1402 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1404 /* We have reached CHARPOS, or passed it. How the call to
1405 move_it_to can overshoot: (i) If CHARPOS is on invisible
1406 text, move_it_to stops at the end of the invisible text,
1407 after CHARPOS. (ii) If CHARPOS is in a display vector,
1408 move_it_to stops on its last glyph. */
1409 int top_x = it.current_x;
1410 int top_y = it.current_y;
1411 enum it_method it_method = it.method;
1412 /* Calling line_bottom_y may change it.method, it.position, etc. */
1413 int bottom_y = (last_height = 0, line_bottom_y (&it));
1414 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1416 if (top_y < window_top_y)
1417 visible_p = bottom_y > window_top_y;
1418 else if (top_y < it.last_visible_y)
1419 visible_p = 1;
1420 if (visible_p)
1422 if (it_method == GET_FROM_DISPLAY_VECTOR)
1424 /* We stopped on the last glyph of a display vector.
1425 Try and recompute. Hack alert! */
1426 if (charpos < 2 || top.charpos >= charpos)
1427 top_x = it.glyph_row->x;
1428 else
1430 struct it it2;
1431 start_display (&it2, w, top);
1432 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1433 get_next_display_element (&it2);
1434 PRODUCE_GLYPHS (&it2);
1435 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1436 || it2.current_x > it2.last_visible_x)
1437 top_x = it.glyph_row->x;
1438 else
1440 top_x = it2.current_x;
1441 top_y = it2.current_y;
1446 *x = top_x;
1447 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1448 *rtop = max (0, window_top_y - top_y);
1449 *rbot = max (0, bottom_y - it.last_visible_y);
1450 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1451 - max (top_y, window_top_y)));
1452 *vpos = it.vpos;
1455 else
1457 struct it it2;
1459 it2 = it;
1460 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1461 move_it_by_lines (&it, 1, 0);
1462 if (charpos < IT_CHARPOS (it)
1463 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1465 visible_p = 1;
1466 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1467 *x = it2.current_x;
1468 *y = it2.current_y + it2.max_ascent - it2.ascent;
1469 *rtop = max (0, -it2.current_y);
1470 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1471 - it.last_visible_y));
1472 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1473 it.last_visible_y)
1474 - max (it2.current_y,
1475 WINDOW_HEADER_LINE_HEIGHT (w))));
1476 *vpos = it2.vpos;
1480 if (old_buffer)
1481 set_buffer_internal_1 (old_buffer);
1483 current_header_line_height = current_mode_line_height = -1;
1485 if (visible_p && XFASTINT (w->hscroll) > 0)
1486 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1488 #if 0
1489 /* Debugging code. */
1490 if (visible_p)
1491 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1492 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1493 else
1494 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1495 #endif
1497 return visible_p;
1501 /* Return the next character from STR which is MAXLEN bytes long.
1502 Return in *LEN the length of the character. This is like
1503 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1504 we find one, we return a `?', but with the length of the invalid
1505 character. */
1507 static INLINE int
1508 string_char_and_length (const unsigned char *str, int *len)
1510 int c;
1512 c = STRING_CHAR_AND_LENGTH (str, *len);
1513 if (!CHAR_VALID_P (c, 1))
1514 /* We may not change the length here because other places in Emacs
1515 don't use this function, i.e. they silently accept invalid
1516 characters. */
1517 c = '?';
1519 return c;
1524 /* Given a position POS containing a valid character and byte position
1525 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1527 static struct text_pos
1528 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1530 xassert (STRINGP (string) && nchars >= 0);
1532 if (STRING_MULTIBYTE (string))
1534 EMACS_INT rest = SBYTES (string) - BYTEPOS (pos);
1535 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1536 int len;
1538 while (nchars--)
1540 string_char_and_length (p, &len);
1541 p += len, rest -= len;
1542 xassert (rest >= 0);
1543 CHARPOS (pos) += 1;
1544 BYTEPOS (pos) += len;
1547 else
1548 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1550 return pos;
1554 /* Value is the text position, i.e. character and byte position,
1555 for character position CHARPOS in STRING. */
1557 static INLINE struct text_pos
1558 string_pos (EMACS_INT charpos, Lisp_Object string)
1560 struct text_pos pos;
1561 xassert (STRINGP (string));
1562 xassert (charpos >= 0);
1563 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1564 return pos;
1568 /* Value is a text position, i.e. character and byte position, for
1569 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1570 means recognize multibyte characters. */
1572 static struct text_pos
1573 c_string_pos (EMACS_INT charpos, const unsigned char *s, int multibyte_p)
1575 struct text_pos pos;
1577 xassert (s != NULL);
1578 xassert (charpos >= 0);
1580 if (multibyte_p)
1582 EMACS_INT rest = strlen (s);
1583 int len;
1585 SET_TEXT_POS (pos, 0, 0);
1586 while (charpos--)
1588 string_char_and_length (s, &len);
1589 s += len, rest -= len;
1590 xassert (rest >= 0);
1591 CHARPOS (pos) += 1;
1592 BYTEPOS (pos) += len;
1595 else
1596 SET_TEXT_POS (pos, charpos, charpos);
1598 return pos;
1602 /* Value is the number of characters in C string S. MULTIBYTE_P
1603 non-zero means recognize multibyte characters. */
1605 static EMACS_INT
1606 number_of_chars (const unsigned char *s, int multibyte_p)
1608 EMACS_INT nchars;
1610 if (multibyte_p)
1612 EMACS_INT rest = strlen (s);
1613 int len;
1614 unsigned char *p = (unsigned char *) s;
1616 for (nchars = 0; rest > 0; ++nchars)
1618 string_char_and_length (p, &len);
1619 rest -= len, p += len;
1622 else
1623 nchars = strlen (s);
1625 return nchars;
1629 /* Compute byte position NEWPOS->bytepos corresponding to
1630 NEWPOS->charpos. POS is a known position in string STRING.
1631 NEWPOS->charpos must be >= POS.charpos. */
1633 static void
1634 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1636 xassert (STRINGP (string));
1637 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1639 if (STRING_MULTIBYTE (string))
1640 *newpos = string_pos_nchars_ahead (pos, string,
1641 CHARPOS (*newpos) - CHARPOS (pos));
1642 else
1643 BYTEPOS (*newpos) = CHARPOS (*newpos);
1646 /* EXPORT:
1647 Return an estimation of the pixel height of mode or header lines on
1648 frame F. FACE_ID specifies what line's height to estimate. */
1651 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1653 #ifdef HAVE_WINDOW_SYSTEM
1654 if (FRAME_WINDOW_P (f))
1656 int height = FONT_HEIGHT (FRAME_FONT (f));
1658 /* This function is called so early when Emacs starts that the face
1659 cache and mode line face are not yet initialized. */
1660 if (FRAME_FACE_CACHE (f))
1662 struct face *face = FACE_FROM_ID (f, face_id);
1663 if (face)
1665 if (face->font)
1666 height = FONT_HEIGHT (face->font);
1667 if (face->box_line_width > 0)
1668 height += 2 * face->box_line_width;
1672 return height;
1674 #endif
1676 return 1;
1679 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1680 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1681 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1682 not force the value into range. */
1684 void
1685 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1686 int *x, int *y, NativeRectangle *bounds, int noclip)
1689 #ifdef HAVE_WINDOW_SYSTEM
1690 if (FRAME_WINDOW_P (f))
1692 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1693 even for negative values. */
1694 if (pix_x < 0)
1695 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1696 if (pix_y < 0)
1697 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1699 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1700 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1702 if (bounds)
1703 STORE_NATIVE_RECT (*bounds,
1704 FRAME_COL_TO_PIXEL_X (f, pix_x),
1705 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1706 FRAME_COLUMN_WIDTH (f) - 1,
1707 FRAME_LINE_HEIGHT (f) - 1);
1709 if (!noclip)
1711 if (pix_x < 0)
1712 pix_x = 0;
1713 else if (pix_x > FRAME_TOTAL_COLS (f))
1714 pix_x = FRAME_TOTAL_COLS (f);
1716 if (pix_y < 0)
1717 pix_y = 0;
1718 else if (pix_y > FRAME_LINES (f))
1719 pix_y = FRAME_LINES (f);
1722 #endif
1724 *x = pix_x;
1725 *y = pix_y;
1729 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1730 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1731 can't tell the positions because W's display is not up to date,
1732 return 0. */
1735 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1736 int *frame_x, int *frame_y)
1738 #ifdef HAVE_WINDOW_SYSTEM
1739 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1741 int success_p;
1743 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1744 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1746 if (display_completed)
1748 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1749 struct glyph *glyph = row->glyphs[TEXT_AREA];
1750 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1752 hpos = row->x;
1753 vpos = row->y;
1754 while (glyph < end)
1756 hpos += glyph->pixel_width;
1757 ++glyph;
1760 /* If first glyph is partially visible, its first visible position is still 0. */
1761 if (hpos < 0)
1762 hpos = 0;
1764 success_p = 1;
1766 else
1768 hpos = vpos = 0;
1769 success_p = 0;
1772 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1773 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1774 return success_p;
1776 #endif
1778 *frame_x = hpos;
1779 *frame_y = vpos;
1780 return 1;
1784 #ifdef HAVE_WINDOW_SYSTEM
1786 /* Find the glyph under window-relative coordinates X/Y in window W.
1787 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1788 strings. Return in *HPOS and *VPOS the row and column number of
1789 the glyph found. Return in *AREA the glyph area containing X.
1790 Value is a pointer to the glyph found or null if X/Y is not on
1791 text, or we can't tell because W's current matrix is not up to
1792 date. */
1794 static
1795 struct glyph *
1796 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1797 int *dx, int *dy, int *area)
1799 struct glyph *glyph, *end;
1800 struct glyph_row *row = NULL;
1801 int x0, i;
1803 /* Find row containing Y. Give up if some row is not enabled. */
1804 for (i = 0; i < w->current_matrix->nrows; ++i)
1806 row = MATRIX_ROW (w->current_matrix, i);
1807 if (!row->enabled_p)
1808 return NULL;
1809 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1810 break;
1813 *vpos = i;
1814 *hpos = 0;
1816 /* Give up if Y is not in the window. */
1817 if (i == w->current_matrix->nrows)
1818 return NULL;
1820 /* Get the glyph area containing X. */
1821 if (w->pseudo_window_p)
1823 *area = TEXT_AREA;
1824 x0 = 0;
1826 else
1828 if (x < window_box_left_offset (w, TEXT_AREA))
1830 *area = LEFT_MARGIN_AREA;
1831 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1833 else if (x < window_box_right_offset (w, TEXT_AREA))
1835 *area = TEXT_AREA;
1836 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1838 else
1840 *area = RIGHT_MARGIN_AREA;
1841 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1845 /* Find glyph containing X. */
1846 glyph = row->glyphs[*area];
1847 end = glyph + row->used[*area];
1848 x -= x0;
1849 while (glyph < end && x >= glyph->pixel_width)
1851 x -= glyph->pixel_width;
1852 ++glyph;
1855 if (glyph == end)
1856 return NULL;
1858 if (dx)
1860 *dx = x;
1861 *dy = y - (row->y + row->ascent - glyph->ascent);
1864 *hpos = glyph - row->glyphs[*area];
1865 return glyph;
1869 /* EXPORT:
1870 Convert frame-relative x/y to coordinates relative to window W.
1871 Takes pseudo-windows into account. */
1873 void
1874 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1876 if (w->pseudo_window_p)
1878 /* A pseudo-window is always full-width, and starts at the
1879 left edge of the frame, plus a frame border. */
1880 struct frame *f = XFRAME (w->frame);
1881 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1882 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1884 else
1886 *x -= WINDOW_LEFT_EDGE_X (w);
1887 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1891 /* EXPORT:
1892 Return in RECTS[] at most N clipping rectangles for glyph string S.
1893 Return the number of stored rectangles. */
1896 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1898 XRectangle r;
1900 if (n <= 0)
1901 return 0;
1903 if (s->row->full_width_p)
1905 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1906 r.x = WINDOW_LEFT_EDGE_X (s->w);
1907 r.width = WINDOW_TOTAL_WIDTH (s->w);
1909 /* Unless displaying a mode or menu bar line, which are always
1910 fully visible, clip to the visible part of the row. */
1911 if (s->w->pseudo_window_p)
1912 r.height = s->row->visible_height;
1913 else
1914 r.height = s->height;
1916 else
1918 /* This is a text line that may be partially visible. */
1919 r.x = window_box_left (s->w, s->area);
1920 r.width = window_box_width (s->w, s->area);
1921 r.height = s->row->visible_height;
1924 if (s->clip_head)
1925 if (r.x < s->clip_head->x)
1927 if (r.width >= s->clip_head->x - r.x)
1928 r.width -= s->clip_head->x - r.x;
1929 else
1930 r.width = 0;
1931 r.x = s->clip_head->x;
1933 if (s->clip_tail)
1934 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1936 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1937 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1938 else
1939 r.width = 0;
1942 /* If S draws overlapping rows, it's sufficient to use the top and
1943 bottom of the window for clipping because this glyph string
1944 intentionally draws over other lines. */
1945 if (s->for_overlaps)
1947 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1948 r.height = window_text_bottom_y (s->w) - r.y;
1950 /* Alas, the above simple strategy does not work for the
1951 environments with anti-aliased text: if the same text is
1952 drawn onto the same place multiple times, it gets thicker.
1953 If the overlap we are processing is for the erased cursor, we
1954 take the intersection with the rectagle of the cursor. */
1955 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1957 XRectangle rc, r_save = r;
1959 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1960 rc.y = s->w->phys_cursor.y;
1961 rc.width = s->w->phys_cursor_width;
1962 rc.height = s->w->phys_cursor_height;
1964 x_intersect_rectangles (&r_save, &rc, &r);
1967 else
1969 /* Don't use S->y for clipping because it doesn't take partially
1970 visible lines into account. For example, it can be negative for
1971 partially visible lines at the top of a window. */
1972 if (!s->row->full_width_p
1973 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1974 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1975 else
1976 r.y = max (0, s->row->y);
1979 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1981 /* If drawing the cursor, don't let glyph draw outside its
1982 advertised boundaries. Cleartype does this under some circumstances. */
1983 if (s->hl == DRAW_CURSOR)
1985 struct glyph *glyph = s->first_glyph;
1986 int height, max_y;
1988 if (s->x > r.x)
1990 r.width -= s->x - r.x;
1991 r.x = s->x;
1993 r.width = min (r.width, glyph->pixel_width);
1995 /* If r.y is below window bottom, ensure that we still see a cursor. */
1996 height = min (glyph->ascent + glyph->descent,
1997 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1998 max_y = window_text_bottom_y (s->w) - height;
1999 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2000 if (s->ybase - glyph->ascent > max_y)
2002 r.y = max_y;
2003 r.height = height;
2005 else
2007 /* Don't draw cursor glyph taller than our actual glyph. */
2008 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2009 if (height < r.height)
2011 max_y = r.y + r.height;
2012 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2013 r.height = min (max_y - r.y, height);
2018 if (s->row->clip)
2020 XRectangle r_save = r;
2022 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2023 r.width = 0;
2026 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2027 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2029 #ifdef CONVERT_FROM_XRECT
2030 CONVERT_FROM_XRECT (r, *rects);
2031 #else
2032 *rects = r;
2033 #endif
2034 return 1;
2036 else
2038 /* If we are processing overlapping and allowed to return
2039 multiple clipping rectangles, we exclude the row of the glyph
2040 string from the clipping rectangle. This is to avoid drawing
2041 the same text on the environment with anti-aliasing. */
2042 #ifdef CONVERT_FROM_XRECT
2043 XRectangle rs[2];
2044 #else
2045 XRectangle *rs = rects;
2046 #endif
2047 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2049 if (s->for_overlaps & OVERLAPS_PRED)
2051 rs[i] = r;
2052 if (r.y + r.height > row_y)
2054 if (r.y < row_y)
2055 rs[i].height = row_y - r.y;
2056 else
2057 rs[i].height = 0;
2059 i++;
2061 if (s->for_overlaps & OVERLAPS_SUCC)
2063 rs[i] = r;
2064 if (r.y < row_y + s->row->visible_height)
2066 if (r.y + r.height > row_y + s->row->visible_height)
2068 rs[i].y = row_y + s->row->visible_height;
2069 rs[i].height = r.y + r.height - rs[i].y;
2071 else
2072 rs[i].height = 0;
2074 i++;
2077 n = i;
2078 #ifdef CONVERT_FROM_XRECT
2079 for (i = 0; i < n; i++)
2080 CONVERT_FROM_XRECT (rs[i], rects[i]);
2081 #endif
2082 return n;
2086 /* EXPORT:
2087 Return in *NR the clipping rectangle for glyph string S. */
2089 void
2090 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2092 get_glyph_string_clip_rects (s, nr, 1);
2096 /* EXPORT:
2097 Return the position and height of the phys cursor in window W.
2098 Set w->phys_cursor_width to width of phys cursor.
2101 void
2102 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2103 struct glyph *glyph, int *xp, int *yp, int *heightp)
2105 struct frame *f = XFRAME (WINDOW_FRAME (w));
2106 int x, y, wd, h, h0, y0;
2108 /* Compute the width of the rectangle to draw. If on a stretch
2109 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2110 rectangle as wide as the glyph, but use a canonical character
2111 width instead. */
2112 wd = glyph->pixel_width - 1;
2113 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2114 wd++; /* Why? */
2115 #endif
2117 x = w->phys_cursor.x;
2118 if (x < 0)
2120 wd += x;
2121 x = 0;
2124 if (glyph->type == STRETCH_GLYPH
2125 && !x_stretch_cursor_p)
2126 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2127 w->phys_cursor_width = wd;
2129 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2131 /* If y is below window bottom, ensure that we still see a cursor. */
2132 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2134 h = max (h0, glyph->ascent + glyph->descent);
2135 h0 = min (h0, glyph->ascent + glyph->descent);
2137 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2138 if (y < y0)
2140 h = max (h - (y0 - y) + 1, h0);
2141 y = y0 - 1;
2143 else
2145 y0 = window_text_bottom_y (w) - h0;
2146 if (y > y0)
2148 h += y - y0;
2149 y = y0;
2153 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2154 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2155 *heightp = h;
2159 * Remember which glyph the mouse is over.
2162 void
2163 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2165 Lisp_Object window;
2166 struct window *w;
2167 struct glyph_row *r, *gr, *end_row;
2168 enum window_part part;
2169 enum glyph_row_area area;
2170 int x, y, width, height;
2172 /* Try to determine frame pixel position and size of the glyph under
2173 frame pixel coordinates X/Y on frame F. */
2175 if (!f->glyphs_initialized_p
2176 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2177 NILP (window)))
2179 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2180 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2181 goto virtual_glyph;
2184 w = XWINDOW (window);
2185 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2186 height = WINDOW_FRAME_LINE_HEIGHT (w);
2188 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2189 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2191 if (w->pseudo_window_p)
2193 area = TEXT_AREA;
2194 part = ON_MODE_LINE; /* Don't adjust margin. */
2195 goto text_glyph;
2198 switch (part)
2200 case ON_LEFT_MARGIN:
2201 area = LEFT_MARGIN_AREA;
2202 goto text_glyph;
2204 case ON_RIGHT_MARGIN:
2205 area = RIGHT_MARGIN_AREA;
2206 goto text_glyph;
2208 case ON_HEADER_LINE:
2209 case ON_MODE_LINE:
2210 gr = (part == ON_HEADER_LINE
2211 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2212 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2213 gy = gr->y;
2214 area = TEXT_AREA;
2215 goto text_glyph_row_found;
2217 case ON_TEXT:
2218 area = TEXT_AREA;
2220 text_glyph:
2221 gr = 0; gy = 0;
2222 for (; r <= end_row && r->enabled_p; ++r)
2223 if (r->y + r->height > y)
2225 gr = r; gy = r->y;
2226 break;
2229 text_glyph_row_found:
2230 if (gr && gy <= y)
2232 struct glyph *g = gr->glyphs[area];
2233 struct glyph *end = g + gr->used[area];
2235 height = gr->height;
2236 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2237 if (gx + g->pixel_width > x)
2238 break;
2240 if (g < end)
2242 if (g->type == IMAGE_GLYPH)
2244 /* Don't remember when mouse is over image, as
2245 image may have hot-spots. */
2246 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2247 return;
2249 width = g->pixel_width;
2251 else
2253 /* Use nominal char spacing at end of line. */
2254 x -= gx;
2255 gx += (x / width) * width;
2258 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2259 gx += window_box_left_offset (w, area);
2261 else
2263 /* Use nominal line height at end of window. */
2264 gx = (x / width) * width;
2265 y -= gy;
2266 gy += (y / height) * height;
2268 break;
2270 case ON_LEFT_FRINGE:
2271 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2272 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2273 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2274 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2275 goto row_glyph;
2277 case ON_RIGHT_FRINGE:
2278 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2279 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2280 : window_box_right_offset (w, TEXT_AREA));
2281 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2282 goto row_glyph;
2284 case ON_SCROLL_BAR:
2285 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2287 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2288 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2289 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2290 : 0)));
2291 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2293 row_glyph:
2294 gr = 0, gy = 0;
2295 for (; r <= end_row && r->enabled_p; ++r)
2296 if (r->y + r->height > y)
2298 gr = r; gy = r->y;
2299 break;
2302 if (gr && gy <= y)
2303 height = gr->height;
2304 else
2306 /* Use nominal line height at end of window. */
2307 y -= gy;
2308 gy += (y / height) * height;
2310 break;
2312 default:
2314 virtual_glyph:
2315 /* If there is no glyph under the mouse, then we divide the screen
2316 into a grid of the smallest glyph in the frame, and use that
2317 as our "glyph". */
2319 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2320 round down even for negative values. */
2321 if (gx < 0)
2322 gx -= width - 1;
2323 if (gy < 0)
2324 gy -= height - 1;
2326 gx = (gx / width) * width;
2327 gy = (gy / height) * height;
2329 goto store_rect;
2332 gx += WINDOW_LEFT_EDGE_X (w);
2333 gy += WINDOW_TOP_EDGE_Y (w);
2335 store_rect:
2336 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2338 /* Visible feedback for debugging. */
2339 #if 0
2340 #if HAVE_X_WINDOWS
2341 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2342 f->output_data.x->normal_gc,
2343 gx, gy, width, height);
2344 #endif
2345 #endif
2349 #endif /* HAVE_WINDOW_SYSTEM */
2352 /***********************************************************************
2353 Lisp form evaluation
2354 ***********************************************************************/
2356 /* Error handler for safe_eval and safe_call. */
2358 static Lisp_Object
2359 safe_eval_handler (Lisp_Object arg)
2361 add_to_log ("Error during redisplay: %s", arg, Qnil);
2362 return Qnil;
2366 /* Evaluate SEXPR and return the result, or nil if something went
2367 wrong. Prevent redisplay during the evaluation. */
2369 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2370 Return the result, or nil if something went wrong. Prevent
2371 redisplay during the evaluation. */
2373 Lisp_Object
2374 safe_call (int nargs, Lisp_Object *args)
2376 Lisp_Object val;
2378 if (inhibit_eval_during_redisplay)
2379 val = Qnil;
2380 else
2382 int count = SPECPDL_INDEX ();
2383 struct gcpro gcpro1;
2385 GCPRO1 (args[0]);
2386 gcpro1.nvars = nargs;
2387 specbind (Qinhibit_redisplay, Qt);
2388 /* Use Qt to ensure debugger does not run,
2389 so there is no possibility of wanting to redisplay. */
2390 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2391 safe_eval_handler);
2392 UNGCPRO;
2393 val = unbind_to (count, val);
2396 return val;
2400 /* Call function FN with one argument ARG.
2401 Return the result, or nil if something went wrong. */
2403 Lisp_Object
2404 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2406 Lisp_Object args[2];
2407 args[0] = fn;
2408 args[1] = arg;
2409 return safe_call (2, args);
2412 static Lisp_Object Qeval;
2414 Lisp_Object
2415 safe_eval (Lisp_Object sexpr)
2417 return safe_call1 (Qeval, sexpr);
2420 /* Call function FN with one argument ARG.
2421 Return the result, or nil if something went wrong. */
2423 Lisp_Object
2424 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2426 Lisp_Object args[3];
2427 args[0] = fn;
2428 args[1] = arg1;
2429 args[2] = arg2;
2430 return safe_call (3, args);
2435 /***********************************************************************
2436 Debugging
2437 ***********************************************************************/
2439 #if 0
2441 /* Define CHECK_IT to perform sanity checks on iterators.
2442 This is for debugging. It is too slow to do unconditionally. */
2444 static void
2445 check_it (it)
2446 struct it *it;
2448 if (it->method == GET_FROM_STRING)
2450 xassert (STRINGP (it->string));
2451 xassert (IT_STRING_CHARPOS (*it) >= 0);
2453 else
2455 xassert (IT_STRING_CHARPOS (*it) < 0);
2456 if (it->method == GET_FROM_BUFFER)
2458 /* Check that character and byte positions agree. */
2459 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2463 if (it->dpvec)
2464 xassert (it->current.dpvec_index >= 0);
2465 else
2466 xassert (it->current.dpvec_index < 0);
2469 #define CHECK_IT(IT) check_it ((IT))
2471 #else /* not 0 */
2473 #define CHECK_IT(IT) (void) 0
2475 #endif /* not 0 */
2478 #if GLYPH_DEBUG
2480 /* Check that the window end of window W is what we expect it
2481 to be---the last row in the current matrix displaying text. */
2483 static void
2484 check_window_end (w)
2485 struct window *w;
2487 if (!MINI_WINDOW_P (w)
2488 && !NILP (w->window_end_valid))
2490 struct glyph_row *row;
2491 xassert ((row = MATRIX_ROW (w->current_matrix,
2492 XFASTINT (w->window_end_vpos)),
2493 !row->enabled_p
2494 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2495 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2499 #define CHECK_WINDOW_END(W) check_window_end ((W))
2501 #else /* not GLYPH_DEBUG */
2503 #define CHECK_WINDOW_END(W) (void) 0
2505 #endif /* not GLYPH_DEBUG */
2509 /***********************************************************************
2510 Iterator initialization
2511 ***********************************************************************/
2513 /* Initialize IT for displaying current_buffer in window W, starting
2514 at character position CHARPOS. CHARPOS < 0 means that no buffer
2515 position is specified which is useful when the iterator is assigned
2516 a position later. BYTEPOS is the byte position corresponding to
2517 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2519 If ROW is not null, calls to produce_glyphs with IT as parameter
2520 will produce glyphs in that row.
2522 BASE_FACE_ID is the id of a base face to use. It must be one of
2523 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2524 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2525 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2527 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2528 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2529 will be initialized to use the corresponding mode line glyph row of
2530 the desired matrix of W. */
2532 void
2533 init_iterator (struct it *it, struct window *w,
2534 EMACS_INT charpos, EMACS_INT bytepos,
2535 struct glyph_row *row, enum face_id base_face_id)
2537 int highlight_region_p;
2538 enum face_id remapped_base_face_id = base_face_id;
2540 /* Some precondition checks. */
2541 xassert (w != NULL && it != NULL);
2542 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2543 && charpos <= ZV));
2545 /* If face attributes have been changed since the last redisplay,
2546 free realized faces now because they depend on face definitions
2547 that might have changed. Don't free faces while there might be
2548 desired matrices pending which reference these faces. */
2549 if (face_change_count && !inhibit_free_realized_faces)
2551 face_change_count = 0;
2552 free_all_realized_faces (Qnil);
2555 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2556 if (! NILP (Vface_remapping_alist))
2557 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2559 /* Use one of the mode line rows of W's desired matrix if
2560 appropriate. */
2561 if (row == NULL)
2563 if (base_face_id == MODE_LINE_FACE_ID
2564 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2565 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2566 else if (base_face_id == HEADER_LINE_FACE_ID)
2567 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2570 /* Clear IT. */
2571 memset (it, 0, sizeof *it);
2572 it->current.overlay_string_index = -1;
2573 it->current.dpvec_index = -1;
2574 it->base_face_id = remapped_base_face_id;
2575 it->string = Qnil;
2576 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2578 /* The window in which we iterate over current_buffer: */
2579 XSETWINDOW (it->window, w);
2580 it->w = w;
2581 it->f = XFRAME (w->frame);
2583 it->cmp_it.id = -1;
2585 /* Extra space between lines (on window systems only). */
2586 if (base_face_id == DEFAULT_FACE_ID
2587 && FRAME_WINDOW_P (it->f))
2589 if (NATNUMP (current_buffer->extra_line_spacing))
2590 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2591 else if (FLOATP (current_buffer->extra_line_spacing))
2592 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2593 * FRAME_LINE_HEIGHT (it->f));
2594 else if (it->f->extra_line_spacing > 0)
2595 it->extra_line_spacing = it->f->extra_line_spacing;
2596 it->max_extra_line_spacing = 0;
2599 /* If realized faces have been removed, e.g. because of face
2600 attribute changes of named faces, recompute them. When running
2601 in batch mode, the face cache of the initial frame is null. If
2602 we happen to get called, make a dummy face cache. */
2603 if (FRAME_FACE_CACHE (it->f) == NULL)
2604 init_frame_faces (it->f);
2605 if (FRAME_FACE_CACHE (it->f)->used == 0)
2606 recompute_basic_faces (it->f);
2608 /* Current value of the `slice', `space-width', and 'height' properties. */
2609 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2610 it->space_width = Qnil;
2611 it->font_height = Qnil;
2612 it->override_ascent = -1;
2614 /* Are control characters displayed as `^C'? */
2615 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2617 /* -1 means everything between a CR and the following line end
2618 is invisible. >0 means lines indented more than this value are
2619 invisible. */
2620 it->selective = (INTEGERP (current_buffer->selective_display)
2621 ? XFASTINT (current_buffer->selective_display)
2622 : (!NILP (current_buffer->selective_display)
2623 ? -1 : 0));
2624 it->selective_display_ellipsis_p
2625 = !NILP (current_buffer->selective_display_ellipses);
2627 /* Display table to use. */
2628 it->dp = window_display_table (w);
2630 /* Are multibyte characters enabled in current_buffer? */
2631 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2633 /* Do we need to reorder bidirectional text? Not if this is a
2634 unibyte buffer: by definition, none of the single-byte characters
2635 are strong R2L, so no reordering is needed. And bidi.c doesn't
2636 support unibyte buffers anyway. */
2637 it->bidi_p
2638 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2640 /* Non-zero if we should highlight the region. */
2641 highlight_region_p
2642 = (!NILP (Vtransient_mark_mode)
2643 && !NILP (current_buffer->mark_active)
2644 && XMARKER (current_buffer->mark)->buffer != 0);
2646 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2647 start and end of a visible region in window IT->w. Set both to
2648 -1 to indicate no region. */
2649 if (highlight_region_p
2650 /* Maybe highlight only in selected window. */
2651 && (/* Either show region everywhere. */
2652 highlight_nonselected_windows
2653 /* Or show region in the selected window. */
2654 || w == XWINDOW (selected_window)
2655 /* Or show the region if we are in the mini-buffer and W is
2656 the window the mini-buffer refers to. */
2657 || (MINI_WINDOW_P (XWINDOW (selected_window))
2658 && WINDOWP (minibuf_selected_window)
2659 && w == XWINDOW (minibuf_selected_window))))
2661 EMACS_INT charpos = marker_position (current_buffer->mark);
2662 it->region_beg_charpos = min (PT, charpos);
2663 it->region_end_charpos = max (PT, charpos);
2665 else
2666 it->region_beg_charpos = it->region_end_charpos = -1;
2668 /* Get the position at which the redisplay_end_trigger hook should
2669 be run, if it is to be run at all. */
2670 if (MARKERP (w->redisplay_end_trigger)
2671 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2672 it->redisplay_end_trigger_charpos
2673 = marker_position (w->redisplay_end_trigger);
2674 else if (INTEGERP (w->redisplay_end_trigger))
2675 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2677 /* Correct bogus values of tab_width. */
2678 it->tab_width = XINT (current_buffer->tab_width);
2679 if (it->tab_width <= 0 || it->tab_width > 1000)
2680 it->tab_width = 8;
2682 /* Are lines in the display truncated? */
2683 if (base_face_id != DEFAULT_FACE_ID
2684 || XINT (it->w->hscroll)
2685 || (! WINDOW_FULL_WIDTH_P (it->w)
2686 && ((!NILP (Vtruncate_partial_width_windows)
2687 && !INTEGERP (Vtruncate_partial_width_windows))
2688 || (INTEGERP (Vtruncate_partial_width_windows)
2689 && (WINDOW_TOTAL_COLS (it->w)
2690 < XINT (Vtruncate_partial_width_windows))))))
2691 it->line_wrap = TRUNCATE;
2692 else if (NILP (current_buffer->truncate_lines))
2693 it->line_wrap = NILP (current_buffer->word_wrap)
2694 ? WINDOW_WRAP : WORD_WRAP;
2695 else
2696 it->line_wrap = TRUNCATE;
2698 /* Get dimensions of truncation and continuation glyphs. These are
2699 displayed as fringe bitmaps under X, so we don't need them for such
2700 frames. */
2701 if (!FRAME_WINDOW_P (it->f))
2703 if (it->line_wrap == TRUNCATE)
2705 /* We will need the truncation glyph. */
2706 xassert (it->glyph_row == NULL);
2707 produce_special_glyphs (it, IT_TRUNCATION);
2708 it->truncation_pixel_width = it->pixel_width;
2710 else
2712 /* We will need the continuation glyph. */
2713 xassert (it->glyph_row == NULL);
2714 produce_special_glyphs (it, IT_CONTINUATION);
2715 it->continuation_pixel_width = it->pixel_width;
2718 /* Reset these values to zero because the produce_special_glyphs
2719 above has changed them. */
2720 it->pixel_width = it->ascent = it->descent = 0;
2721 it->phys_ascent = it->phys_descent = 0;
2724 /* Set this after getting the dimensions of truncation and
2725 continuation glyphs, so that we don't produce glyphs when calling
2726 produce_special_glyphs, above. */
2727 it->glyph_row = row;
2728 it->area = TEXT_AREA;
2730 /* Forget any previous info about this row being reversed. */
2731 if (it->glyph_row)
2732 it->glyph_row->reversed_p = 0;
2734 /* Get the dimensions of the display area. The display area
2735 consists of the visible window area plus a horizontally scrolled
2736 part to the left of the window. All x-values are relative to the
2737 start of this total display area. */
2738 if (base_face_id != DEFAULT_FACE_ID)
2740 /* Mode lines, menu bar in terminal frames. */
2741 it->first_visible_x = 0;
2742 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2744 else
2746 it->first_visible_x
2747 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2748 it->last_visible_x = (it->first_visible_x
2749 + window_box_width (w, TEXT_AREA));
2751 /* If we truncate lines, leave room for the truncator glyph(s) at
2752 the right margin. Otherwise, leave room for the continuation
2753 glyph(s). Truncation and continuation glyphs are not inserted
2754 for window-based redisplay. */
2755 if (!FRAME_WINDOW_P (it->f))
2757 if (it->line_wrap == TRUNCATE)
2758 it->last_visible_x -= it->truncation_pixel_width;
2759 else
2760 it->last_visible_x -= it->continuation_pixel_width;
2763 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2764 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2767 /* Leave room for a border glyph. */
2768 if (!FRAME_WINDOW_P (it->f)
2769 && !WINDOW_RIGHTMOST_P (it->w))
2770 it->last_visible_x -= 1;
2772 it->last_visible_y = window_text_bottom_y (w);
2774 /* For mode lines and alike, arrange for the first glyph having a
2775 left box line if the face specifies a box. */
2776 if (base_face_id != DEFAULT_FACE_ID)
2778 struct face *face;
2780 it->face_id = remapped_base_face_id;
2782 /* If we have a boxed mode line, make the first character appear
2783 with a left box line. */
2784 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2785 if (face->box != FACE_NO_BOX)
2786 it->start_of_box_run_p = 1;
2789 /* If we are to reorder bidirectional text, init the bidi
2790 iterator. */
2791 if (it->bidi_p)
2793 /* Note the paragraph direction that this buffer wants to
2794 use. */
2795 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2796 it->paragraph_embedding = L2R;
2797 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2798 it->paragraph_embedding = R2L;
2799 else
2800 it->paragraph_embedding = NEUTRAL_DIR;
2801 bidi_init_it (charpos, bytepos, &it->bidi_it);
2804 /* If a buffer position was specified, set the iterator there,
2805 getting overlays and face properties from that position. */
2806 if (charpos >= BUF_BEG (current_buffer))
2808 it->end_charpos = ZV;
2809 it->face_id = -1;
2810 IT_CHARPOS (*it) = charpos;
2812 /* Compute byte position if not specified. */
2813 if (bytepos < charpos)
2814 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2815 else
2816 IT_BYTEPOS (*it) = bytepos;
2818 it->start = it->current;
2820 /* Compute faces etc. */
2821 reseat (it, it->current.pos, 1);
2824 CHECK_IT (it);
2828 /* Initialize IT for the display of window W with window start POS. */
2830 void
2831 start_display (struct it *it, struct window *w, struct text_pos pos)
2833 struct glyph_row *row;
2834 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2836 row = w->desired_matrix->rows + first_vpos;
2837 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2838 it->first_vpos = first_vpos;
2840 /* Don't reseat to previous visible line start if current start
2841 position is in a string or image. */
2842 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2844 int start_at_line_beg_p;
2845 int first_y = it->current_y;
2847 /* If window start is not at a line start, skip forward to POS to
2848 get the correct continuation lines width. */
2849 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2850 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2851 if (!start_at_line_beg_p)
2853 int new_x;
2855 reseat_at_previous_visible_line_start (it);
2856 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2858 new_x = it->current_x + it->pixel_width;
2860 /* If lines are continued, this line may end in the middle
2861 of a multi-glyph character (e.g. a control character
2862 displayed as \003, or in the middle of an overlay
2863 string). In this case move_it_to above will not have
2864 taken us to the start of the continuation line but to the
2865 end of the continued line. */
2866 if (it->current_x > 0
2867 && it->line_wrap != TRUNCATE /* Lines are continued. */
2868 && (/* And glyph doesn't fit on the line. */
2869 new_x > it->last_visible_x
2870 /* Or it fits exactly and we're on a window
2871 system frame. */
2872 || (new_x == it->last_visible_x
2873 && FRAME_WINDOW_P (it->f))))
2875 if (it->current.dpvec_index >= 0
2876 || it->current.overlay_string_index >= 0)
2878 set_iterator_to_next (it, 1);
2879 move_it_in_display_line_to (it, -1, -1, 0);
2882 it->continuation_lines_width += it->current_x;
2885 /* We're starting a new display line, not affected by the
2886 height of the continued line, so clear the appropriate
2887 fields in the iterator structure. */
2888 it->max_ascent = it->max_descent = 0;
2889 it->max_phys_ascent = it->max_phys_descent = 0;
2891 it->current_y = first_y;
2892 it->vpos = 0;
2893 it->current_x = it->hpos = 0;
2899 /* Return 1 if POS is a position in ellipses displayed for invisible
2900 text. W is the window we display, for text property lookup. */
2902 static int
2903 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2905 Lisp_Object prop, window;
2906 int ellipses_p = 0;
2907 EMACS_INT charpos = CHARPOS (pos->pos);
2909 /* If POS specifies a position in a display vector, this might
2910 be for an ellipsis displayed for invisible text. We won't
2911 get the iterator set up for delivering that ellipsis unless
2912 we make sure that it gets aware of the invisible text. */
2913 if (pos->dpvec_index >= 0
2914 && pos->overlay_string_index < 0
2915 && CHARPOS (pos->string_pos) < 0
2916 && charpos > BEGV
2917 && (XSETWINDOW (window, w),
2918 prop = Fget_char_property (make_number (charpos),
2919 Qinvisible, window),
2920 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2922 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2923 window);
2924 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2927 return ellipses_p;
2931 /* Initialize IT for stepping through current_buffer in window W,
2932 starting at position POS that includes overlay string and display
2933 vector/ control character translation position information. Value
2934 is zero if there are overlay strings with newlines at POS. */
2936 static int
2937 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2939 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2940 int i, overlay_strings_with_newlines = 0;
2942 /* If POS specifies a position in a display vector, this might
2943 be for an ellipsis displayed for invisible text. We won't
2944 get the iterator set up for delivering that ellipsis unless
2945 we make sure that it gets aware of the invisible text. */
2946 if (in_ellipses_for_invisible_text_p (pos, w))
2948 --charpos;
2949 bytepos = 0;
2952 /* Keep in mind: the call to reseat in init_iterator skips invisible
2953 text, so we might end up at a position different from POS. This
2954 is only a problem when POS is a row start after a newline and an
2955 overlay starts there with an after-string, and the overlay has an
2956 invisible property. Since we don't skip invisible text in
2957 display_line and elsewhere immediately after consuming the
2958 newline before the row start, such a POS will not be in a string,
2959 but the call to init_iterator below will move us to the
2960 after-string. */
2961 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2963 /* This only scans the current chunk -- it should scan all chunks.
2964 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2965 to 16 in 22.1 to make this a lesser problem. */
2966 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2968 const char *s = SDATA (it->overlay_strings[i]);
2969 const char *e = s + SBYTES (it->overlay_strings[i]);
2971 while (s < e && *s != '\n')
2972 ++s;
2974 if (s < e)
2976 overlay_strings_with_newlines = 1;
2977 break;
2981 /* If position is within an overlay string, set up IT to the right
2982 overlay string. */
2983 if (pos->overlay_string_index >= 0)
2985 int relative_index;
2987 /* If the first overlay string happens to have a `display'
2988 property for an image, the iterator will be set up for that
2989 image, and we have to undo that setup first before we can
2990 correct the overlay string index. */
2991 if (it->method == GET_FROM_IMAGE)
2992 pop_it (it);
2994 /* We already have the first chunk of overlay strings in
2995 IT->overlay_strings. Load more until the one for
2996 pos->overlay_string_index is in IT->overlay_strings. */
2997 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2999 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3000 it->current.overlay_string_index = 0;
3001 while (n--)
3003 load_overlay_strings (it, 0);
3004 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3008 it->current.overlay_string_index = pos->overlay_string_index;
3009 relative_index = (it->current.overlay_string_index
3010 % OVERLAY_STRING_CHUNK_SIZE);
3011 it->string = it->overlay_strings[relative_index];
3012 xassert (STRINGP (it->string));
3013 it->current.string_pos = pos->string_pos;
3014 it->method = GET_FROM_STRING;
3017 if (CHARPOS (pos->string_pos) >= 0)
3019 /* Recorded position is not in an overlay string, but in another
3020 string. This can only be a string from a `display' property.
3021 IT should already be filled with that string. */
3022 it->current.string_pos = pos->string_pos;
3023 xassert (STRINGP (it->string));
3026 /* Restore position in display vector translations, control
3027 character translations or ellipses. */
3028 if (pos->dpvec_index >= 0)
3030 if (it->dpvec == NULL)
3031 get_next_display_element (it);
3032 xassert (it->dpvec && it->current.dpvec_index == 0);
3033 it->current.dpvec_index = pos->dpvec_index;
3036 CHECK_IT (it);
3037 return !overlay_strings_with_newlines;
3041 /* Initialize IT for stepping through current_buffer in window W
3042 starting at ROW->start. */
3044 static void
3045 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3047 init_from_display_pos (it, w, &row->start);
3048 it->start = row->start;
3049 it->continuation_lines_width = row->continuation_lines_width;
3050 CHECK_IT (it);
3054 /* Initialize IT for stepping through current_buffer in window W
3055 starting in the line following ROW, i.e. starting at ROW->end.
3056 Value is zero if there are overlay strings with newlines at ROW's
3057 end position. */
3059 static int
3060 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3062 int success = 0;
3064 if (init_from_display_pos (it, w, &row->end))
3066 if (row->continued_p)
3067 it->continuation_lines_width
3068 = row->continuation_lines_width + row->pixel_width;
3069 CHECK_IT (it);
3070 success = 1;
3073 return success;
3079 /***********************************************************************
3080 Text properties
3081 ***********************************************************************/
3083 /* Called when IT reaches IT->stop_charpos. Handle text property and
3084 overlay changes. Set IT->stop_charpos to the next position where
3085 to stop. */
3087 static void
3088 handle_stop (struct it *it)
3090 enum prop_handled handled;
3091 int handle_overlay_change_p;
3092 struct props *p;
3094 it->dpvec = NULL;
3095 it->current.dpvec_index = -1;
3096 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3097 it->ignore_overlay_strings_at_pos_p = 0;
3098 it->ellipsis_p = 0;
3100 /* Use face of preceding text for ellipsis (if invisible) */
3101 if (it->selective_display_ellipsis_p)
3102 it->saved_face_id = it->face_id;
3106 handled = HANDLED_NORMALLY;
3108 /* Call text property handlers. */
3109 for (p = it_props; p->handler; ++p)
3111 handled = p->handler (it);
3113 if (handled == HANDLED_RECOMPUTE_PROPS)
3114 break;
3115 else if (handled == HANDLED_RETURN)
3117 /* We still want to show before and after strings from
3118 overlays even if the actual buffer text is replaced. */
3119 if (!handle_overlay_change_p
3120 || it->sp > 1
3121 || !get_overlay_strings_1 (it, 0, 0))
3123 if (it->ellipsis_p)
3124 setup_for_ellipsis (it, 0);
3125 /* When handling a display spec, we might load an
3126 empty string. In that case, discard it here. We
3127 used to discard it in handle_single_display_spec,
3128 but that causes get_overlay_strings_1, above, to
3129 ignore overlay strings that we must check. */
3130 if (STRINGP (it->string) && !SCHARS (it->string))
3131 pop_it (it);
3132 return;
3134 else if (STRINGP (it->string) && !SCHARS (it->string))
3135 pop_it (it);
3136 else
3138 it->ignore_overlay_strings_at_pos_p = 1;
3139 it->string_from_display_prop_p = 0;
3140 handle_overlay_change_p = 0;
3142 handled = HANDLED_RECOMPUTE_PROPS;
3143 break;
3145 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3146 handle_overlay_change_p = 0;
3149 if (handled != HANDLED_RECOMPUTE_PROPS)
3151 /* Don't check for overlay strings below when set to deliver
3152 characters from a display vector. */
3153 if (it->method == GET_FROM_DISPLAY_VECTOR)
3154 handle_overlay_change_p = 0;
3156 /* Handle overlay changes.
3157 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3158 if it finds overlays. */
3159 if (handle_overlay_change_p)
3160 handled = handle_overlay_change (it);
3163 if (it->ellipsis_p)
3165 setup_for_ellipsis (it, 0);
3166 break;
3169 while (handled == HANDLED_RECOMPUTE_PROPS);
3171 /* Determine where to stop next. */
3172 if (handled == HANDLED_NORMALLY)
3173 compute_stop_pos (it);
3177 /* Compute IT->stop_charpos from text property and overlay change
3178 information for IT's current position. */
3180 static void
3181 compute_stop_pos (struct it *it)
3183 register INTERVAL iv, next_iv;
3184 Lisp_Object object, limit, position;
3185 EMACS_INT charpos, bytepos;
3187 /* If nowhere else, stop at the end. */
3188 it->stop_charpos = it->end_charpos;
3190 if (STRINGP (it->string))
3192 /* Strings are usually short, so don't limit the search for
3193 properties. */
3194 object = it->string;
3195 limit = Qnil;
3196 charpos = IT_STRING_CHARPOS (*it);
3197 bytepos = IT_STRING_BYTEPOS (*it);
3199 else
3201 EMACS_INT pos;
3203 /* If next overlay change is in front of the current stop pos
3204 (which is IT->end_charpos), stop there. Note: value of
3205 next_overlay_change is point-max if no overlay change
3206 follows. */
3207 charpos = IT_CHARPOS (*it);
3208 bytepos = IT_BYTEPOS (*it);
3209 pos = next_overlay_change (charpos);
3210 if (pos < it->stop_charpos)
3211 it->stop_charpos = pos;
3213 /* If showing the region, we have to stop at the region
3214 start or end because the face might change there. */
3215 if (it->region_beg_charpos > 0)
3217 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3218 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3219 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3220 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3223 /* Set up variables for computing the stop position from text
3224 property changes. */
3225 XSETBUFFER (object, current_buffer);
3226 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3229 /* Get the interval containing IT's position. Value is a null
3230 interval if there isn't such an interval. */
3231 position = make_number (charpos);
3232 iv = validate_interval_range (object, &position, &position, 0);
3233 if (!NULL_INTERVAL_P (iv))
3235 Lisp_Object values_here[LAST_PROP_IDX];
3236 struct props *p;
3238 /* Get properties here. */
3239 for (p = it_props; p->handler; ++p)
3240 values_here[p->idx] = textget (iv->plist, *p->name);
3242 /* Look for an interval following iv that has different
3243 properties. */
3244 for (next_iv = next_interval (iv);
3245 (!NULL_INTERVAL_P (next_iv)
3246 && (NILP (limit)
3247 || XFASTINT (limit) > next_iv->position));
3248 next_iv = next_interval (next_iv))
3250 for (p = it_props; p->handler; ++p)
3252 Lisp_Object new_value;
3254 new_value = textget (next_iv->plist, *p->name);
3255 if (!EQ (values_here[p->idx], new_value))
3256 break;
3259 if (p->handler)
3260 break;
3263 if (!NULL_INTERVAL_P (next_iv))
3265 if (INTEGERP (limit)
3266 && next_iv->position >= XFASTINT (limit))
3267 /* No text property change up to limit. */
3268 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3269 else
3270 /* Text properties change in next_iv. */
3271 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3275 if (it->cmp_it.id < 0)
3277 EMACS_INT stoppos = it->end_charpos;
3279 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3280 stoppos = -1;
3281 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3282 stoppos, it->string);
3285 xassert (STRINGP (it->string)
3286 || (it->stop_charpos >= BEGV
3287 && it->stop_charpos >= IT_CHARPOS (*it)));
3291 /* Return the position of the next overlay change after POS in
3292 current_buffer. Value is point-max if no overlay change
3293 follows. This is like `next-overlay-change' but doesn't use
3294 xmalloc. */
3296 static EMACS_INT
3297 next_overlay_change (EMACS_INT pos)
3299 int noverlays;
3300 EMACS_INT endpos;
3301 Lisp_Object *overlays;
3302 int i;
3304 /* Get all overlays at the given position. */
3305 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3307 /* If any of these overlays ends before endpos,
3308 use its ending point instead. */
3309 for (i = 0; i < noverlays; ++i)
3311 Lisp_Object oend;
3312 EMACS_INT oendpos;
3314 oend = OVERLAY_END (overlays[i]);
3315 oendpos = OVERLAY_POSITION (oend);
3316 endpos = min (endpos, oendpos);
3319 return endpos;
3324 /***********************************************************************
3325 Fontification
3326 ***********************************************************************/
3328 /* Handle changes in the `fontified' property of the current buffer by
3329 calling hook functions from Qfontification_functions to fontify
3330 regions of text. */
3332 static enum prop_handled
3333 handle_fontified_prop (struct it *it)
3335 Lisp_Object prop, pos;
3336 enum prop_handled handled = HANDLED_NORMALLY;
3338 if (!NILP (Vmemory_full))
3339 return handled;
3341 /* Get the value of the `fontified' property at IT's current buffer
3342 position. (The `fontified' property doesn't have a special
3343 meaning in strings.) If the value is nil, call functions from
3344 Qfontification_functions. */
3345 if (!STRINGP (it->string)
3346 && it->s == NULL
3347 && !NILP (Vfontification_functions)
3348 && !NILP (Vrun_hooks)
3349 && (pos = make_number (IT_CHARPOS (*it)),
3350 prop = Fget_char_property (pos, Qfontified, Qnil),
3351 /* Ignore the special cased nil value always present at EOB since
3352 no amount of fontifying will be able to change it. */
3353 NILP (prop) && IT_CHARPOS (*it) < Z))
3355 int count = SPECPDL_INDEX ();
3356 Lisp_Object val;
3358 val = Vfontification_functions;
3359 specbind (Qfontification_functions, Qnil);
3361 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3362 safe_call1 (val, pos);
3363 else
3365 Lisp_Object globals, fn;
3366 struct gcpro gcpro1, gcpro2;
3368 globals = Qnil;
3369 GCPRO2 (val, globals);
3371 for (; CONSP (val); val = XCDR (val))
3373 fn = XCAR (val);
3375 if (EQ (fn, Qt))
3377 /* A value of t indicates this hook has a local
3378 binding; it means to run the global binding too.
3379 In a global value, t should not occur. If it
3380 does, we must ignore it to avoid an endless
3381 loop. */
3382 for (globals = Fdefault_value (Qfontification_functions);
3383 CONSP (globals);
3384 globals = XCDR (globals))
3386 fn = XCAR (globals);
3387 if (!EQ (fn, Qt))
3388 safe_call1 (fn, pos);
3391 else
3392 safe_call1 (fn, pos);
3395 UNGCPRO;
3398 unbind_to (count, Qnil);
3400 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3401 something. This avoids an endless loop if they failed to
3402 fontify the text for which reason ever. */
3403 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3404 handled = HANDLED_RECOMPUTE_PROPS;
3407 return handled;
3412 /***********************************************************************
3413 Faces
3414 ***********************************************************************/
3416 /* Set up iterator IT from face properties at its current position.
3417 Called from handle_stop. */
3419 static enum prop_handled
3420 handle_face_prop (struct it *it)
3422 int new_face_id;
3423 EMACS_INT next_stop;
3425 if (!STRINGP (it->string))
3427 new_face_id
3428 = face_at_buffer_position (it->w,
3429 IT_CHARPOS (*it),
3430 it->region_beg_charpos,
3431 it->region_end_charpos,
3432 &next_stop,
3433 (IT_CHARPOS (*it)
3434 + TEXT_PROP_DISTANCE_LIMIT),
3435 0, it->base_face_id);
3437 /* Is this a start of a run of characters with box face?
3438 Caveat: this can be called for a freshly initialized
3439 iterator; face_id is -1 in this case. We know that the new
3440 face will not change until limit, i.e. if the new face has a
3441 box, all characters up to limit will have one. But, as
3442 usual, we don't know whether limit is really the end. */
3443 if (new_face_id != it->face_id)
3445 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3447 /* If new face has a box but old face has not, this is
3448 the start of a run of characters with box, i.e. it has
3449 a shadow on the left side. The value of face_id of the
3450 iterator will be -1 if this is the initial call that gets
3451 the face. In this case, we have to look in front of IT's
3452 position and see whether there is a face != new_face_id. */
3453 it->start_of_box_run_p
3454 = (new_face->box != FACE_NO_BOX
3455 && (it->face_id >= 0
3456 || IT_CHARPOS (*it) == BEG
3457 || new_face_id != face_before_it_pos (it)));
3458 it->face_box_p = new_face->box != FACE_NO_BOX;
3461 else
3463 int base_face_id;
3464 EMACS_INT bufpos;
3465 int i;
3466 Lisp_Object from_overlay
3467 = (it->current.overlay_string_index >= 0
3468 ? it->string_overlays[it->current.overlay_string_index]
3469 : Qnil);
3471 /* See if we got to this string directly or indirectly from
3472 an overlay property. That includes the before-string or
3473 after-string of an overlay, strings in display properties
3474 provided by an overlay, their text properties, etc.
3476 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3477 if (! NILP (from_overlay))
3478 for (i = it->sp - 1; i >= 0; i--)
3480 if (it->stack[i].current.overlay_string_index >= 0)
3481 from_overlay
3482 = it->string_overlays[it->stack[i].current.overlay_string_index];
3483 else if (! NILP (it->stack[i].from_overlay))
3484 from_overlay = it->stack[i].from_overlay;
3486 if (!NILP (from_overlay))
3487 break;
3490 if (! NILP (from_overlay))
3492 bufpos = IT_CHARPOS (*it);
3493 /* For a string from an overlay, the base face depends
3494 only on text properties and ignores overlays. */
3495 base_face_id
3496 = face_for_overlay_string (it->w,
3497 IT_CHARPOS (*it),
3498 it->region_beg_charpos,
3499 it->region_end_charpos,
3500 &next_stop,
3501 (IT_CHARPOS (*it)
3502 + TEXT_PROP_DISTANCE_LIMIT),
3504 from_overlay);
3506 else
3508 bufpos = 0;
3510 /* For strings from a `display' property, use the face at
3511 IT's current buffer position as the base face to merge
3512 with, so that overlay strings appear in the same face as
3513 surrounding text, unless they specify their own
3514 faces. */
3515 base_face_id = underlying_face_id (it);
3518 new_face_id = face_at_string_position (it->w,
3519 it->string,
3520 IT_STRING_CHARPOS (*it),
3521 bufpos,
3522 it->region_beg_charpos,
3523 it->region_end_charpos,
3524 &next_stop,
3525 base_face_id, 0);
3527 /* Is this a start of a run of characters with box? Caveat:
3528 this can be called for a freshly allocated iterator; face_id
3529 is -1 is this case. We know that the new face will not
3530 change until the next check pos, i.e. if the new face has a
3531 box, all characters up to that position will have a
3532 box. But, as usual, we don't know whether that position
3533 is really the end. */
3534 if (new_face_id != it->face_id)
3536 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3537 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3539 /* If new face has a box but old face hasn't, this is the
3540 start of a run of characters with box, i.e. it has a
3541 shadow on the left side. */
3542 it->start_of_box_run_p
3543 = new_face->box && (old_face == NULL || !old_face->box);
3544 it->face_box_p = new_face->box != FACE_NO_BOX;
3548 it->face_id = new_face_id;
3549 return HANDLED_NORMALLY;
3553 /* Return the ID of the face ``underlying'' IT's current position,
3554 which is in a string. If the iterator is associated with a
3555 buffer, return the face at IT's current buffer position.
3556 Otherwise, use the iterator's base_face_id. */
3558 static int
3559 underlying_face_id (struct it *it)
3561 int face_id = it->base_face_id, i;
3563 xassert (STRINGP (it->string));
3565 for (i = it->sp - 1; i >= 0; --i)
3566 if (NILP (it->stack[i].string))
3567 face_id = it->stack[i].face_id;
3569 return face_id;
3573 /* Compute the face one character before or after the current position
3574 of IT. BEFORE_P non-zero means get the face in front of IT's
3575 position. Value is the id of the face. */
3577 static int
3578 face_before_or_after_it_pos (struct it *it, int before_p)
3580 int face_id, limit;
3581 EMACS_INT next_check_charpos;
3582 struct text_pos pos;
3584 xassert (it->s == NULL);
3586 if (STRINGP (it->string))
3588 EMACS_INT bufpos;
3589 int base_face_id;
3591 /* No face change past the end of the string (for the case
3592 we are padding with spaces). No face change before the
3593 string start. */
3594 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3595 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3596 return it->face_id;
3598 /* Set pos to the position before or after IT's current position. */
3599 if (before_p)
3600 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3601 else
3602 /* For composition, we must check the character after the
3603 composition. */
3604 pos = (it->what == IT_COMPOSITION
3605 ? string_pos (IT_STRING_CHARPOS (*it)
3606 + it->cmp_it.nchars, it->string)
3607 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3609 if (it->current.overlay_string_index >= 0)
3610 bufpos = IT_CHARPOS (*it);
3611 else
3612 bufpos = 0;
3614 base_face_id = underlying_face_id (it);
3616 /* Get the face for ASCII, or unibyte. */
3617 face_id = face_at_string_position (it->w,
3618 it->string,
3619 CHARPOS (pos),
3620 bufpos,
3621 it->region_beg_charpos,
3622 it->region_end_charpos,
3623 &next_check_charpos,
3624 base_face_id, 0);
3626 /* Correct the face for charsets different from ASCII. Do it
3627 for the multibyte case only. The face returned above is
3628 suitable for unibyte text if IT->string is unibyte. */
3629 if (STRING_MULTIBYTE (it->string))
3631 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3632 int c, len;
3633 struct face *face = FACE_FROM_ID (it->f, face_id);
3635 c = string_char_and_length (p, &len);
3636 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3639 else
3641 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3642 || (IT_CHARPOS (*it) <= BEGV && before_p))
3643 return it->face_id;
3645 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3646 pos = it->current.pos;
3648 if (before_p)
3649 DEC_TEXT_POS (pos, it->multibyte_p);
3650 else
3652 if (it->what == IT_COMPOSITION)
3653 /* For composition, we must check the position after the
3654 composition. */
3655 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3656 else
3657 INC_TEXT_POS (pos, it->multibyte_p);
3660 /* Determine face for CHARSET_ASCII, or unibyte. */
3661 face_id = face_at_buffer_position (it->w,
3662 CHARPOS (pos),
3663 it->region_beg_charpos,
3664 it->region_end_charpos,
3665 &next_check_charpos,
3666 limit, 0, -1);
3668 /* Correct the face for charsets different from ASCII. Do it
3669 for the multibyte case only. The face returned above is
3670 suitable for unibyte text if current_buffer is unibyte. */
3671 if (it->multibyte_p)
3673 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3674 struct face *face = FACE_FROM_ID (it->f, face_id);
3675 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3679 return face_id;
3684 /***********************************************************************
3685 Invisible text
3686 ***********************************************************************/
3688 /* Set up iterator IT from invisible properties at its current
3689 position. Called from handle_stop. */
3691 static enum prop_handled
3692 handle_invisible_prop (struct it *it)
3694 enum prop_handled handled = HANDLED_NORMALLY;
3696 if (STRINGP (it->string))
3698 Lisp_Object prop, end_charpos, limit, charpos;
3700 /* Get the value of the invisible text property at the
3701 current position. Value will be nil if there is no such
3702 property. */
3703 charpos = make_number (IT_STRING_CHARPOS (*it));
3704 prop = Fget_text_property (charpos, Qinvisible, it->string);
3706 if (!NILP (prop)
3707 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3709 handled = HANDLED_RECOMPUTE_PROPS;
3711 /* Get the position at which the next change of the
3712 invisible text property can be found in IT->string.
3713 Value will be nil if the property value is the same for
3714 all the rest of IT->string. */
3715 XSETINT (limit, SCHARS (it->string));
3716 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3717 it->string, limit);
3719 /* Text at current position is invisible. The next
3720 change in the property is at position end_charpos.
3721 Move IT's current position to that position. */
3722 if (INTEGERP (end_charpos)
3723 && XFASTINT (end_charpos) < XFASTINT (limit))
3725 struct text_pos old;
3726 old = it->current.string_pos;
3727 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3728 compute_string_pos (&it->current.string_pos, old, it->string);
3730 else
3732 /* The rest of the string is invisible. If this is an
3733 overlay string, proceed with the next overlay string
3734 or whatever comes and return a character from there. */
3735 if (it->current.overlay_string_index >= 0)
3737 next_overlay_string (it);
3738 /* Don't check for overlay strings when we just
3739 finished processing them. */
3740 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3742 else
3744 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3745 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3750 else
3752 int invis_p;
3753 EMACS_INT newpos, next_stop, start_charpos, tem;
3754 Lisp_Object pos, prop, overlay;
3756 /* First of all, is there invisible text at this position? */
3757 tem = start_charpos = IT_CHARPOS (*it);
3758 pos = make_number (tem);
3759 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3760 &overlay);
3761 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3763 /* If we are on invisible text, skip over it. */
3764 if (invis_p && start_charpos < it->end_charpos)
3766 /* Record whether we have to display an ellipsis for the
3767 invisible text. */
3768 int display_ellipsis_p = invis_p == 2;
3770 handled = HANDLED_RECOMPUTE_PROPS;
3772 /* Loop skipping over invisible text. The loop is left at
3773 ZV or with IT on the first char being visible again. */
3776 /* Try to skip some invisible text. Return value is the
3777 position reached which can be equal to where we start
3778 if there is nothing invisible there. This skips both
3779 over invisible text properties and overlays with
3780 invisible property. */
3781 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3783 /* If we skipped nothing at all we weren't at invisible
3784 text in the first place. If everything to the end of
3785 the buffer was skipped, end the loop. */
3786 if (newpos == tem || newpos >= ZV)
3787 invis_p = 0;
3788 else
3790 /* We skipped some characters but not necessarily
3791 all there are. Check if we ended up on visible
3792 text. Fget_char_property returns the property of
3793 the char before the given position, i.e. if we
3794 get invis_p = 0, this means that the char at
3795 newpos is visible. */
3796 pos = make_number (newpos);
3797 prop = Fget_char_property (pos, Qinvisible, it->window);
3798 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3801 /* If we ended up on invisible text, proceed to
3802 skip starting with next_stop. */
3803 if (invis_p)
3804 tem = next_stop;
3806 /* If there are adjacent invisible texts, don't lose the
3807 second one's ellipsis. */
3808 if (invis_p == 2)
3809 display_ellipsis_p = 1;
3811 while (invis_p);
3813 /* The position newpos is now either ZV or on visible text. */
3814 if (it->bidi_p && newpos < ZV)
3816 /* With bidi iteration, the region of invisible text
3817 could start and/or end in the middle of a non-base
3818 embedding level. Therefore, we need to skip
3819 invisible text using the bidi iterator, starting at
3820 IT's current position, until we find ourselves
3821 outside the invisible text. Skipping invisible text
3822 _after_ bidi iteration avoids affecting the visual
3823 order of the displayed text when invisible properties
3824 are added or removed. */
3825 if (it->bidi_it.first_elt)
3827 /* If we were `reseat'ed to a new paragraph,
3828 determine the paragraph base direction. We need
3829 to do it now because next_element_from_buffer may
3830 not have a chance to do it, if we are going to
3831 skip any text at the beginning, which resets the
3832 FIRST_ELT flag. */
3833 bidi_paragraph_init (it->paragraph_embedding,
3834 &it->bidi_it, 1);
3838 bidi_move_to_visually_next (&it->bidi_it);
3840 while (it->stop_charpos <= it->bidi_it.charpos
3841 && it->bidi_it.charpos < newpos);
3842 IT_CHARPOS (*it) = it->bidi_it.charpos;
3843 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3844 /* If we overstepped NEWPOS, record its position in the
3845 iterator, so that we skip invisible text if later the
3846 bidi iteration lands us in the invisible region
3847 again. */
3848 if (IT_CHARPOS (*it) >= newpos)
3849 it->prev_stop = newpos;
3851 else
3853 IT_CHARPOS (*it) = newpos;
3854 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3857 /* If there are before-strings at the start of invisible
3858 text, and the text is invisible because of a text
3859 property, arrange to show before-strings because 20.x did
3860 it that way. (If the text is invisible because of an
3861 overlay property instead of a text property, this is
3862 already handled in the overlay code.) */
3863 if (NILP (overlay)
3864 && get_overlay_strings (it, it->stop_charpos))
3866 handled = HANDLED_RECOMPUTE_PROPS;
3867 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3869 else if (display_ellipsis_p)
3871 /* Make sure that the glyphs of the ellipsis will get
3872 correct `charpos' values. If we would not update
3873 it->position here, the glyphs would belong to the
3874 last visible character _before_ the invisible
3875 text, which confuses `set_cursor_from_row'.
3877 We use the last invisible position instead of the
3878 first because this way the cursor is always drawn on
3879 the first "." of the ellipsis, whenever PT is inside
3880 the invisible text. Otherwise the cursor would be
3881 placed _after_ the ellipsis when the point is after the
3882 first invisible character. */
3883 if (!STRINGP (it->object))
3885 it->position.charpos = newpos - 1;
3886 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3888 it->ellipsis_p = 1;
3889 /* Let the ellipsis display before
3890 considering any properties of the following char.
3891 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3892 handled = HANDLED_RETURN;
3897 return handled;
3901 /* Make iterator IT return `...' next.
3902 Replaces LEN characters from buffer. */
3904 static void
3905 setup_for_ellipsis (struct it *it, int len)
3907 /* Use the display table definition for `...'. Invalid glyphs
3908 will be handled by the method returning elements from dpvec. */
3909 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3911 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3912 it->dpvec = v->contents;
3913 it->dpend = v->contents + v->size;
3915 else
3917 /* Default `...'. */
3918 it->dpvec = default_invis_vector;
3919 it->dpend = default_invis_vector + 3;
3922 it->dpvec_char_len = len;
3923 it->current.dpvec_index = 0;
3924 it->dpvec_face_id = -1;
3926 /* Remember the current face id in case glyphs specify faces.
3927 IT's face is restored in set_iterator_to_next.
3928 saved_face_id was set to preceding char's face in handle_stop. */
3929 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3930 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3932 it->method = GET_FROM_DISPLAY_VECTOR;
3933 it->ellipsis_p = 1;
3938 /***********************************************************************
3939 'display' property
3940 ***********************************************************************/
3942 /* Set up iterator IT from `display' property at its current position.
3943 Called from handle_stop.
3944 We return HANDLED_RETURN if some part of the display property
3945 overrides the display of the buffer text itself.
3946 Otherwise we return HANDLED_NORMALLY. */
3948 static enum prop_handled
3949 handle_display_prop (struct it *it)
3951 Lisp_Object prop, object, overlay;
3952 struct text_pos *position;
3953 /* Nonzero if some property replaces the display of the text itself. */
3954 int display_replaced_p = 0;
3956 if (STRINGP (it->string))
3958 object = it->string;
3959 position = &it->current.string_pos;
3961 else
3963 XSETWINDOW (object, it->w);
3964 position = &it->current.pos;
3967 /* Reset those iterator values set from display property values. */
3968 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3969 it->space_width = Qnil;
3970 it->font_height = Qnil;
3971 it->voffset = 0;
3973 /* We don't support recursive `display' properties, i.e. string
3974 values that have a string `display' property, that have a string
3975 `display' property etc. */
3976 if (!it->string_from_display_prop_p)
3977 it->area = TEXT_AREA;
3979 prop = get_char_property_and_overlay (make_number (position->charpos),
3980 Qdisplay, object, &overlay);
3981 if (NILP (prop))
3982 return HANDLED_NORMALLY;
3983 /* Now OVERLAY is the overlay that gave us this property, or nil
3984 if it was a text property. */
3986 if (!STRINGP (it->string))
3987 object = it->w->buffer;
3989 if (CONSP (prop)
3990 /* Simple properties. */
3991 && !EQ (XCAR (prop), Qimage)
3992 && !EQ (XCAR (prop), Qspace)
3993 && !EQ (XCAR (prop), Qwhen)
3994 && !EQ (XCAR (prop), Qslice)
3995 && !EQ (XCAR (prop), Qspace_width)
3996 && !EQ (XCAR (prop), Qheight)
3997 && !EQ (XCAR (prop), Qraise)
3998 /* Marginal area specifications. */
3999 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4000 && !EQ (XCAR (prop), Qleft_fringe)
4001 && !EQ (XCAR (prop), Qright_fringe)
4002 && !NILP (XCAR (prop)))
4004 for (; CONSP (prop); prop = XCDR (prop))
4006 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4007 position, display_replaced_p))
4009 display_replaced_p = 1;
4010 /* If some text in a string is replaced, `position' no
4011 longer points to the position of `object'. */
4012 if (STRINGP (object))
4013 break;
4017 else if (VECTORP (prop))
4019 int i;
4020 for (i = 0; i < ASIZE (prop); ++i)
4021 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4022 position, display_replaced_p))
4024 display_replaced_p = 1;
4025 /* If some text in a string is replaced, `position' no
4026 longer points to the position of `object'. */
4027 if (STRINGP (object))
4028 break;
4031 else
4033 if (handle_single_display_spec (it, prop, object, overlay,
4034 position, 0))
4035 display_replaced_p = 1;
4038 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4042 /* Value is the position of the end of the `display' property starting
4043 at START_POS in OBJECT. */
4045 static struct text_pos
4046 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4048 Lisp_Object end;
4049 struct text_pos end_pos;
4051 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4052 Qdisplay, object, Qnil);
4053 CHARPOS (end_pos) = XFASTINT (end);
4054 if (STRINGP (object))
4055 compute_string_pos (&end_pos, start_pos, it->string);
4056 else
4057 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4059 return end_pos;
4063 /* Set up IT from a single `display' specification PROP. OBJECT
4064 is the object in which the `display' property was found. *POSITION
4065 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4066 means that we previously saw a display specification which already
4067 replaced text display with something else, for example an image;
4068 we ignore such properties after the first one has been processed.
4070 OVERLAY is the overlay this `display' property came from,
4071 or nil if it was a text property.
4073 If PROP is a `space' or `image' specification, and in some other
4074 cases too, set *POSITION to the position where the `display'
4075 property ends.
4077 Value is non-zero if something was found which replaces the display
4078 of buffer or string text. */
4080 static int
4081 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4082 Lisp_Object overlay, struct text_pos *position,
4083 int display_replaced_before_p)
4085 Lisp_Object form;
4086 Lisp_Object location, value;
4087 struct text_pos start_pos, save_pos;
4088 int valid_p;
4090 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4091 If the result is non-nil, use VALUE instead of SPEC. */
4092 form = Qt;
4093 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4095 spec = XCDR (spec);
4096 if (!CONSP (spec))
4097 return 0;
4098 form = XCAR (spec);
4099 spec = XCDR (spec);
4102 if (!NILP (form) && !EQ (form, Qt))
4104 int count = SPECPDL_INDEX ();
4105 struct gcpro gcpro1;
4107 /* Bind `object' to the object having the `display' property, a
4108 buffer or string. Bind `position' to the position in the
4109 object where the property was found, and `buffer-position'
4110 to the current position in the buffer. */
4111 specbind (Qobject, object);
4112 specbind (Qposition, make_number (CHARPOS (*position)));
4113 specbind (Qbuffer_position,
4114 make_number (STRINGP (object)
4115 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4116 GCPRO1 (form);
4117 form = safe_eval (form);
4118 UNGCPRO;
4119 unbind_to (count, Qnil);
4122 if (NILP (form))
4123 return 0;
4125 /* Handle `(height HEIGHT)' specifications. */
4126 if (CONSP (spec)
4127 && EQ (XCAR (spec), Qheight)
4128 && CONSP (XCDR (spec)))
4130 if (!FRAME_WINDOW_P (it->f))
4131 return 0;
4133 it->font_height = XCAR (XCDR (spec));
4134 if (!NILP (it->font_height))
4136 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4137 int new_height = -1;
4139 if (CONSP (it->font_height)
4140 && (EQ (XCAR (it->font_height), Qplus)
4141 || EQ (XCAR (it->font_height), Qminus))
4142 && CONSP (XCDR (it->font_height))
4143 && INTEGERP (XCAR (XCDR (it->font_height))))
4145 /* `(+ N)' or `(- N)' where N is an integer. */
4146 int steps = XINT (XCAR (XCDR (it->font_height)));
4147 if (EQ (XCAR (it->font_height), Qplus))
4148 steps = - steps;
4149 it->face_id = smaller_face (it->f, it->face_id, steps);
4151 else if (FUNCTIONP (it->font_height))
4153 /* Call function with current height as argument.
4154 Value is the new height. */
4155 Lisp_Object height;
4156 height = safe_call1 (it->font_height,
4157 face->lface[LFACE_HEIGHT_INDEX]);
4158 if (NUMBERP (height))
4159 new_height = XFLOATINT (height);
4161 else if (NUMBERP (it->font_height))
4163 /* Value is a multiple of the canonical char height. */
4164 struct face *face;
4166 face = FACE_FROM_ID (it->f,
4167 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4168 new_height = (XFLOATINT (it->font_height)
4169 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4171 else
4173 /* Evaluate IT->font_height with `height' bound to the
4174 current specified height to get the new height. */
4175 int count = SPECPDL_INDEX ();
4177 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4178 value = safe_eval (it->font_height);
4179 unbind_to (count, Qnil);
4181 if (NUMBERP (value))
4182 new_height = XFLOATINT (value);
4185 if (new_height > 0)
4186 it->face_id = face_with_height (it->f, it->face_id, new_height);
4189 return 0;
4192 /* Handle `(space-width WIDTH)'. */
4193 if (CONSP (spec)
4194 && EQ (XCAR (spec), Qspace_width)
4195 && CONSP (XCDR (spec)))
4197 if (!FRAME_WINDOW_P (it->f))
4198 return 0;
4200 value = XCAR (XCDR (spec));
4201 if (NUMBERP (value) && XFLOATINT (value) > 0)
4202 it->space_width = value;
4204 return 0;
4207 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4208 if (CONSP (spec)
4209 && EQ (XCAR (spec), Qslice))
4211 Lisp_Object tem;
4213 if (!FRAME_WINDOW_P (it->f))
4214 return 0;
4216 if (tem = XCDR (spec), CONSP (tem))
4218 it->slice.x = XCAR (tem);
4219 if (tem = XCDR (tem), CONSP (tem))
4221 it->slice.y = XCAR (tem);
4222 if (tem = XCDR (tem), CONSP (tem))
4224 it->slice.width = XCAR (tem);
4225 if (tem = XCDR (tem), CONSP (tem))
4226 it->slice.height = XCAR (tem);
4231 return 0;
4234 /* Handle `(raise FACTOR)'. */
4235 if (CONSP (spec)
4236 && EQ (XCAR (spec), Qraise)
4237 && CONSP (XCDR (spec)))
4239 if (!FRAME_WINDOW_P (it->f))
4240 return 0;
4242 #ifdef HAVE_WINDOW_SYSTEM
4243 value = XCAR (XCDR (spec));
4244 if (NUMBERP (value))
4246 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4247 it->voffset = - (XFLOATINT (value)
4248 * (FONT_HEIGHT (face->font)));
4250 #endif /* HAVE_WINDOW_SYSTEM */
4252 return 0;
4255 /* Don't handle the other kinds of display specifications
4256 inside a string that we got from a `display' property. */
4257 if (it->string_from_display_prop_p)
4258 return 0;
4260 /* Characters having this form of property are not displayed, so
4261 we have to find the end of the property. */
4262 start_pos = *position;
4263 *position = display_prop_end (it, object, start_pos);
4264 value = Qnil;
4266 /* Stop the scan at that end position--we assume that all
4267 text properties change there. */
4268 it->stop_charpos = position->charpos;
4270 /* Handle `(left-fringe BITMAP [FACE])'
4271 and `(right-fringe BITMAP [FACE])'. */
4272 if (CONSP (spec)
4273 && (EQ (XCAR (spec), Qleft_fringe)
4274 || EQ (XCAR (spec), Qright_fringe))
4275 && CONSP (XCDR (spec)))
4277 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4278 int fringe_bitmap;
4280 if (!FRAME_WINDOW_P (it->f))
4281 /* If we return here, POSITION has been advanced
4282 across the text with this property. */
4283 return 0;
4285 #ifdef HAVE_WINDOW_SYSTEM
4286 value = XCAR (XCDR (spec));
4287 if (!SYMBOLP (value)
4288 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4289 /* If we return here, POSITION has been advanced
4290 across the text with this property. */
4291 return 0;
4293 if (CONSP (XCDR (XCDR (spec))))
4295 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4296 int face_id2 = lookup_derived_face (it->f, face_name,
4297 FRINGE_FACE_ID, 0);
4298 if (face_id2 >= 0)
4299 face_id = face_id2;
4302 /* Save current settings of IT so that we can restore them
4303 when we are finished with the glyph property value. */
4305 save_pos = it->position;
4306 it->position = *position;
4307 push_it (it);
4308 it->position = save_pos;
4310 it->area = TEXT_AREA;
4311 it->what = IT_IMAGE;
4312 it->image_id = -1; /* no image */
4313 it->position = start_pos;
4314 it->object = NILP (object) ? it->w->buffer : object;
4315 it->method = GET_FROM_IMAGE;
4316 it->from_overlay = Qnil;
4317 it->face_id = face_id;
4319 /* Say that we haven't consumed the characters with
4320 `display' property yet. The call to pop_it in
4321 set_iterator_to_next will clean this up. */
4322 *position = start_pos;
4324 if (EQ (XCAR (spec), Qleft_fringe))
4326 it->left_user_fringe_bitmap = fringe_bitmap;
4327 it->left_user_fringe_face_id = face_id;
4329 else
4331 it->right_user_fringe_bitmap = fringe_bitmap;
4332 it->right_user_fringe_face_id = face_id;
4334 #endif /* HAVE_WINDOW_SYSTEM */
4335 return 1;
4338 /* Prepare to handle `((margin left-margin) ...)',
4339 `((margin right-margin) ...)' and `((margin nil) ...)'
4340 prefixes for display specifications. */
4341 location = Qunbound;
4342 if (CONSP (spec) && CONSP (XCAR (spec)))
4344 Lisp_Object tem;
4346 value = XCDR (spec);
4347 if (CONSP (value))
4348 value = XCAR (value);
4350 tem = XCAR (spec);
4351 if (EQ (XCAR (tem), Qmargin)
4352 && (tem = XCDR (tem),
4353 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4354 (NILP (tem)
4355 || EQ (tem, Qleft_margin)
4356 || EQ (tem, Qright_margin))))
4357 location = tem;
4360 if (EQ (location, Qunbound))
4362 location = Qnil;
4363 value = spec;
4366 /* After this point, VALUE is the property after any
4367 margin prefix has been stripped. It must be a string,
4368 an image specification, or `(space ...)'.
4370 LOCATION specifies where to display: `left-margin',
4371 `right-margin' or nil. */
4373 valid_p = (STRINGP (value)
4374 #ifdef HAVE_WINDOW_SYSTEM
4375 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4376 #endif /* not HAVE_WINDOW_SYSTEM */
4377 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4379 if (valid_p && !display_replaced_before_p)
4381 /* Save current settings of IT so that we can restore them
4382 when we are finished with the glyph property value. */
4383 save_pos = it->position;
4384 it->position = *position;
4385 push_it (it);
4386 it->position = save_pos;
4387 it->from_overlay = overlay;
4389 if (NILP (location))
4390 it->area = TEXT_AREA;
4391 else if (EQ (location, Qleft_margin))
4392 it->area = LEFT_MARGIN_AREA;
4393 else
4394 it->area = RIGHT_MARGIN_AREA;
4396 if (STRINGP (value))
4398 it->string = value;
4399 it->multibyte_p = STRING_MULTIBYTE (it->string);
4400 it->current.overlay_string_index = -1;
4401 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4402 it->end_charpos = it->string_nchars = SCHARS (it->string);
4403 it->method = GET_FROM_STRING;
4404 it->stop_charpos = 0;
4405 it->string_from_display_prop_p = 1;
4406 /* Say that we haven't consumed the characters with
4407 `display' property yet. The call to pop_it in
4408 set_iterator_to_next will clean this up. */
4409 if (BUFFERP (object))
4410 *position = start_pos;
4412 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4414 it->method = GET_FROM_STRETCH;
4415 it->object = value;
4416 *position = it->position = start_pos;
4418 #ifdef HAVE_WINDOW_SYSTEM
4419 else
4421 it->what = IT_IMAGE;
4422 it->image_id = lookup_image (it->f, value);
4423 it->position = start_pos;
4424 it->object = NILP (object) ? it->w->buffer : object;
4425 it->method = GET_FROM_IMAGE;
4427 /* Say that we haven't consumed the characters with
4428 `display' property yet. The call to pop_it in
4429 set_iterator_to_next will clean this up. */
4430 *position = start_pos;
4432 #endif /* HAVE_WINDOW_SYSTEM */
4434 return 1;
4437 /* Invalid property or property not supported. Restore
4438 POSITION to what it was before. */
4439 *position = start_pos;
4440 return 0;
4444 /* Check if SPEC is a display sub-property value whose text should be
4445 treated as intangible. */
4447 static int
4448 single_display_spec_intangible_p (Lisp_Object prop)
4450 /* Skip over `when FORM'. */
4451 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4453 prop = XCDR (prop);
4454 if (!CONSP (prop))
4455 return 0;
4456 prop = XCDR (prop);
4459 if (STRINGP (prop))
4460 return 1;
4462 if (!CONSP (prop))
4463 return 0;
4465 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4466 we don't need to treat text as intangible. */
4467 if (EQ (XCAR (prop), Qmargin))
4469 prop = XCDR (prop);
4470 if (!CONSP (prop))
4471 return 0;
4473 prop = XCDR (prop);
4474 if (!CONSP (prop)
4475 || EQ (XCAR (prop), Qleft_margin)
4476 || EQ (XCAR (prop), Qright_margin))
4477 return 0;
4480 return (CONSP (prop)
4481 && (EQ (XCAR (prop), Qimage)
4482 || EQ (XCAR (prop), Qspace)));
4486 /* Check if PROP is a display property value whose text should be
4487 treated as intangible. */
4490 display_prop_intangible_p (Lisp_Object prop)
4492 if (CONSP (prop)
4493 && CONSP (XCAR (prop))
4494 && !EQ (Qmargin, XCAR (XCAR (prop))))
4496 /* A list of sub-properties. */
4497 while (CONSP (prop))
4499 if (single_display_spec_intangible_p (XCAR (prop)))
4500 return 1;
4501 prop = XCDR (prop);
4504 else if (VECTORP (prop))
4506 /* A vector of sub-properties. */
4507 int i;
4508 for (i = 0; i < ASIZE (prop); ++i)
4509 if (single_display_spec_intangible_p (AREF (prop, i)))
4510 return 1;
4512 else
4513 return single_display_spec_intangible_p (prop);
4515 return 0;
4519 /* Return 1 if PROP is a display sub-property value containing STRING. */
4521 static int
4522 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4524 if (EQ (string, prop))
4525 return 1;
4527 /* Skip over `when FORM'. */
4528 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4530 prop = XCDR (prop);
4531 if (!CONSP (prop))
4532 return 0;
4533 prop = XCDR (prop);
4536 if (CONSP (prop))
4537 /* Skip over `margin LOCATION'. */
4538 if (EQ (XCAR (prop), Qmargin))
4540 prop = XCDR (prop);
4541 if (!CONSP (prop))
4542 return 0;
4544 prop = XCDR (prop);
4545 if (!CONSP (prop))
4546 return 0;
4549 return CONSP (prop) && EQ (XCAR (prop), string);
4553 /* Return 1 if STRING appears in the `display' property PROP. */
4555 static int
4556 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4558 if (CONSP (prop)
4559 && CONSP (XCAR (prop))
4560 && !EQ (Qmargin, XCAR (XCAR (prop))))
4562 /* A list of sub-properties. */
4563 while (CONSP (prop))
4565 if (single_display_spec_string_p (XCAR (prop), string))
4566 return 1;
4567 prop = XCDR (prop);
4570 else if (VECTORP (prop))
4572 /* A vector of sub-properties. */
4573 int i;
4574 for (i = 0; i < ASIZE (prop); ++i)
4575 if (single_display_spec_string_p (AREF (prop, i), string))
4576 return 1;
4578 else
4579 return single_display_spec_string_p (prop, string);
4581 return 0;
4584 /* Look for STRING in overlays and text properties in W's buffer,
4585 between character positions FROM and TO (excluding TO).
4586 BACK_P non-zero means look back (in this case, TO is supposed to be
4587 less than FROM).
4588 Value is the first character position where STRING was found, or
4589 zero if it wasn't found before hitting TO.
4591 W's buffer must be current.
4593 This function may only use code that doesn't eval because it is
4594 called asynchronously from note_mouse_highlight. */
4596 static EMACS_INT
4597 string_buffer_position_lim (struct window *w, Lisp_Object string,
4598 EMACS_INT from, EMACS_INT to, int back_p)
4600 Lisp_Object limit, prop, pos;
4601 int found = 0;
4603 pos = make_number (from);
4605 if (!back_p) /* looking forward */
4607 limit = make_number (min (to, ZV));
4608 while (!found && !EQ (pos, limit))
4610 prop = Fget_char_property (pos, Qdisplay, Qnil);
4611 if (!NILP (prop) && display_prop_string_p (prop, string))
4612 found = 1;
4613 else
4614 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4615 limit);
4618 else /* looking back */
4620 limit = make_number (max (to, BEGV));
4621 while (!found && !EQ (pos, limit))
4623 prop = Fget_char_property (pos, Qdisplay, Qnil);
4624 if (!NILP (prop) && display_prop_string_p (prop, string))
4625 found = 1;
4626 else
4627 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4628 limit);
4632 return found ? XINT (pos) : 0;
4635 /* Determine which buffer position in W's buffer STRING comes from.
4636 AROUND_CHARPOS is an approximate position where it could come from.
4637 Value is the buffer position or 0 if it couldn't be determined.
4639 W's buffer must be current.
4641 This function is necessary because we don't record buffer positions
4642 in glyphs generated from strings (to keep struct glyph small).
4643 This function may only use code that doesn't eval because it is
4644 called asynchronously from note_mouse_highlight. */
4646 EMACS_INT
4647 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4649 const int MAX_DISTANCE = 1000;
4650 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4651 around_charpos + MAX_DISTANCE,
4654 if (!found)
4655 found = string_buffer_position_lim (w, string, around_charpos,
4656 around_charpos - MAX_DISTANCE, 1);
4657 return found;
4662 /***********************************************************************
4663 `composition' property
4664 ***********************************************************************/
4666 /* Set up iterator IT from `composition' property at its current
4667 position. Called from handle_stop. */
4669 static enum prop_handled
4670 handle_composition_prop (struct it *it)
4672 Lisp_Object prop, string;
4673 EMACS_INT pos, pos_byte, start, end;
4675 if (STRINGP (it->string))
4677 unsigned char *s;
4679 pos = IT_STRING_CHARPOS (*it);
4680 pos_byte = IT_STRING_BYTEPOS (*it);
4681 string = it->string;
4682 s = SDATA (string) + pos_byte;
4683 it->c = STRING_CHAR (s);
4685 else
4687 pos = IT_CHARPOS (*it);
4688 pos_byte = IT_BYTEPOS (*it);
4689 string = Qnil;
4690 it->c = FETCH_CHAR (pos_byte);
4693 /* If there's a valid composition and point is not inside of the
4694 composition (in the case that the composition is from the current
4695 buffer), draw a glyph composed from the composition components. */
4696 if (find_composition (pos, -1, &start, &end, &prop, string)
4697 && COMPOSITION_VALID_P (start, end, prop)
4698 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4700 if (start != pos)
4702 if (STRINGP (it->string))
4703 pos_byte = string_char_to_byte (it->string, start);
4704 else
4705 pos_byte = CHAR_TO_BYTE (start);
4707 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4708 prop, string);
4710 if (it->cmp_it.id >= 0)
4712 it->cmp_it.ch = -1;
4713 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4714 it->cmp_it.nglyphs = -1;
4718 return HANDLED_NORMALLY;
4723 /***********************************************************************
4724 Overlay strings
4725 ***********************************************************************/
4727 /* The following structure is used to record overlay strings for
4728 later sorting in load_overlay_strings. */
4730 struct overlay_entry
4732 Lisp_Object overlay;
4733 Lisp_Object string;
4734 int priority;
4735 int after_string_p;
4739 /* Set up iterator IT from overlay strings at its current position.
4740 Called from handle_stop. */
4742 static enum prop_handled
4743 handle_overlay_change (struct it *it)
4745 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4746 return HANDLED_RECOMPUTE_PROPS;
4747 else
4748 return HANDLED_NORMALLY;
4752 /* Set up the next overlay string for delivery by IT, if there is an
4753 overlay string to deliver. Called by set_iterator_to_next when the
4754 end of the current overlay string is reached. If there are more
4755 overlay strings to display, IT->string and
4756 IT->current.overlay_string_index are set appropriately here.
4757 Otherwise IT->string is set to nil. */
4759 static void
4760 next_overlay_string (struct it *it)
4762 ++it->current.overlay_string_index;
4763 if (it->current.overlay_string_index == it->n_overlay_strings)
4765 /* No more overlay strings. Restore IT's settings to what
4766 they were before overlay strings were processed, and
4767 continue to deliver from current_buffer. */
4769 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4770 pop_it (it);
4771 xassert (it->sp > 0
4772 || (NILP (it->string)
4773 && it->method == GET_FROM_BUFFER
4774 && it->stop_charpos >= BEGV
4775 && it->stop_charpos <= it->end_charpos));
4776 it->current.overlay_string_index = -1;
4777 it->n_overlay_strings = 0;
4779 /* If we're at the end of the buffer, record that we have
4780 processed the overlay strings there already, so that
4781 next_element_from_buffer doesn't try it again. */
4782 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4783 it->overlay_strings_at_end_processed_p = 1;
4785 else
4787 /* There are more overlay strings to process. If
4788 IT->current.overlay_string_index has advanced to a position
4789 where we must load IT->overlay_strings with more strings, do
4790 it. */
4791 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4793 if (it->current.overlay_string_index && i == 0)
4794 load_overlay_strings (it, 0);
4796 /* Initialize IT to deliver display elements from the overlay
4797 string. */
4798 it->string = it->overlay_strings[i];
4799 it->multibyte_p = STRING_MULTIBYTE (it->string);
4800 SET_TEXT_POS (it->current.string_pos, 0, 0);
4801 it->method = GET_FROM_STRING;
4802 it->stop_charpos = 0;
4803 if (it->cmp_it.stop_pos >= 0)
4804 it->cmp_it.stop_pos = 0;
4807 CHECK_IT (it);
4811 /* Compare two overlay_entry structures E1 and E2. Used as a
4812 comparison function for qsort in load_overlay_strings. Overlay
4813 strings for the same position are sorted so that
4815 1. All after-strings come in front of before-strings, except
4816 when they come from the same overlay.
4818 2. Within after-strings, strings are sorted so that overlay strings
4819 from overlays with higher priorities come first.
4821 2. Within before-strings, strings are sorted so that overlay
4822 strings from overlays with higher priorities come last.
4824 Value is analogous to strcmp. */
4827 static int
4828 compare_overlay_entries (const void *e1, const void *e2)
4830 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4831 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4832 int result;
4834 if (entry1->after_string_p != entry2->after_string_p)
4836 /* Let after-strings appear in front of before-strings if
4837 they come from different overlays. */
4838 if (EQ (entry1->overlay, entry2->overlay))
4839 result = entry1->after_string_p ? 1 : -1;
4840 else
4841 result = entry1->after_string_p ? -1 : 1;
4843 else if (entry1->after_string_p)
4844 /* After-strings sorted in order of decreasing priority. */
4845 result = entry2->priority - entry1->priority;
4846 else
4847 /* Before-strings sorted in order of increasing priority. */
4848 result = entry1->priority - entry2->priority;
4850 return result;
4854 /* Load the vector IT->overlay_strings with overlay strings from IT's
4855 current buffer position, or from CHARPOS if that is > 0. Set
4856 IT->n_overlays to the total number of overlay strings found.
4858 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4859 a time. On entry into load_overlay_strings,
4860 IT->current.overlay_string_index gives the number of overlay
4861 strings that have already been loaded by previous calls to this
4862 function.
4864 IT->add_overlay_start contains an additional overlay start
4865 position to consider for taking overlay strings from, if non-zero.
4866 This position comes into play when the overlay has an `invisible'
4867 property, and both before and after-strings. When we've skipped to
4868 the end of the overlay, because of its `invisible' property, we
4869 nevertheless want its before-string to appear.
4870 IT->add_overlay_start will contain the overlay start position
4871 in this case.
4873 Overlay strings are sorted so that after-string strings come in
4874 front of before-string strings. Within before and after-strings,
4875 strings are sorted by overlay priority. See also function
4876 compare_overlay_entries. */
4878 static void
4879 load_overlay_strings (struct it *it, EMACS_INT charpos)
4881 Lisp_Object overlay, window, str, invisible;
4882 struct Lisp_Overlay *ov;
4883 EMACS_INT start, end;
4884 int size = 20;
4885 int n = 0, i, j, invis_p;
4886 struct overlay_entry *entries
4887 = (struct overlay_entry *) alloca (size * sizeof *entries);
4889 if (charpos <= 0)
4890 charpos = IT_CHARPOS (*it);
4892 /* Append the overlay string STRING of overlay OVERLAY to vector
4893 `entries' which has size `size' and currently contains `n'
4894 elements. AFTER_P non-zero means STRING is an after-string of
4895 OVERLAY. */
4896 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4897 do \
4899 Lisp_Object priority; \
4901 if (n == size) \
4903 int new_size = 2 * size; \
4904 struct overlay_entry *old = entries; \
4905 entries = \
4906 (struct overlay_entry *) alloca (new_size \
4907 * sizeof *entries); \
4908 memcpy (entries, old, size * sizeof *entries); \
4909 size = new_size; \
4912 entries[n].string = (STRING); \
4913 entries[n].overlay = (OVERLAY); \
4914 priority = Foverlay_get ((OVERLAY), Qpriority); \
4915 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4916 entries[n].after_string_p = (AFTER_P); \
4917 ++n; \
4919 while (0)
4921 /* Process overlay before the overlay center. */
4922 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4924 XSETMISC (overlay, ov);
4925 xassert (OVERLAYP (overlay));
4926 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4927 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4929 if (end < charpos)
4930 break;
4932 /* Skip this overlay if it doesn't start or end at IT's current
4933 position. */
4934 if (end != charpos && start != charpos)
4935 continue;
4937 /* Skip this overlay if it doesn't apply to IT->w. */
4938 window = Foverlay_get (overlay, Qwindow);
4939 if (WINDOWP (window) && XWINDOW (window) != it->w)
4940 continue;
4942 /* If the text ``under'' the overlay is invisible, both before-
4943 and after-strings from this overlay are visible; start and
4944 end position are indistinguishable. */
4945 invisible = Foverlay_get (overlay, Qinvisible);
4946 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4948 /* If overlay has a non-empty before-string, record it. */
4949 if ((start == charpos || (end == charpos && invis_p))
4950 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4951 && SCHARS (str))
4952 RECORD_OVERLAY_STRING (overlay, str, 0);
4954 /* If overlay has a non-empty after-string, record it. */
4955 if ((end == charpos || (start == charpos && invis_p))
4956 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4957 && SCHARS (str))
4958 RECORD_OVERLAY_STRING (overlay, str, 1);
4961 /* Process overlays after the overlay center. */
4962 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4964 XSETMISC (overlay, ov);
4965 xassert (OVERLAYP (overlay));
4966 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4967 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4969 if (start > charpos)
4970 break;
4972 /* Skip this overlay if it doesn't start or end at IT's current
4973 position. */
4974 if (end != charpos && start != charpos)
4975 continue;
4977 /* Skip this overlay if it doesn't apply to IT->w. */
4978 window = Foverlay_get (overlay, Qwindow);
4979 if (WINDOWP (window) && XWINDOW (window) != it->w)
4980 continue;
4982 /* If the text ``under'' the overlay is invisible, it has a zero
4983 dimension, and both before- and after-strings apply. */
4984 invisible = Foverlay_get (overlay, Qinvisible);
4985 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4987 /* If overlay has a non-empty before-string, record it. */
4988 if ((start == charpos || (end == charpos && invis_p))
4989 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4990 && SCHARS (str))
4991 RECORD_OVERLAY_STRING (overlay, str, 0);
4993 /* If overlay has a non-empty after-string, record it. */
4994 if ((end == charpos || (start == charpos && invis_p))
4995 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4996 && SCHARS (str))
4997 RECORD_OVERLAY_STRING (overlay, str, 1);
5000 #undef RECORD_OVERLAY_STRING
5002 /* Sort entries. */
5003 if (n > 1)
5004 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5006 /* Record the total number of strings to process. */
5007 it->n_overlay_strings = n;
5009 /* IT->current.overlay_string_index is the number of overlay strings
5010 that have already been consumed by IT. Copy some of the
5011 remaining overlay strings to IT->overlay_strings. */
5012 i = 0;
5013 j = it->current.overlay_string_index;
5014 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5016 it->overlay_strings[i] = entries[j].string;
5017 it->string_overlays[i++] = entries[j++].overlay;
5020 CHECK_IT (it);
5024 /* Get the first chunk of overlay strings at IT's current buffer
5025 position, or at CHARPOS if that is > 0. Value is non-zero if at
5026 least one overlay string was found. */
5028 static int
5029 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5031 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5032 process. This fills IT->overlay_strings with strings, and sets
5033 IT->n_overlay_strings to the total number of strings to process.
5034 IT->pos.overlay_string_index has to be set temporarily to zero
5035 because load_overlay_strings needs this; it must be set to -1
5036 when no overlay strings are found because a zero value would
5037 indicate a position in the first overlay string. */
5038 it->current.overlay_string_index = 0;
5039 load_overlay_strings (it, charpos);
5041 /* If we found overlay strings, set up IT to deliver display
5042 elements from the first one. Otherwise set up IT to deliver
5043 from current_buffer. */
5044 if (it->n_overlay_strings)
5046 /* Make sure we know settings in current_buffer, so that we can
5047 restore meaningful values when we're done with the overlay
5048 strings. */
5049 if (compute_stop_p)
5050 compute_stop_pos (it);
5051 xassert (it->face_id >= 0);
5053 /* Save IT's settings. They are restored after all overlay
5054 strings have been processed. */
5055 xassert (!compute_stop_p || it->sp == 0);
5057 /* When called from handle_stop, there might be an empty display
5058 string loaded. In that case, don't bother saving it. */
5059 if (!STRINGP (it->string) || SCHARS (it->string))
5060 push_it (it);
5062 /* Set up IT to deliver display elements from the first overlay
5063 string. */
5064 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5065 it->string = it->overlay_strings[0];
5066 it->from_overlay = Qnil;
5067 it->stop_charpos = 0;
5068 xassert (STRINGP (it->string));
5069 it->end_charpos = SCHARS (it->string);
5070 it->multibyte_p = STRING_MULTIBYTE (it->string);
5071 it->method = GET_FROM_STRING;
5072 return 1;
5075 it->current.overlay_string_index = -1;
5076 return 0;
5079 static int
5080 get_overlay_strings (struct it *it, EMACS_INT charpos)
5082 it->string = Qnil;
5083 it->method = GET_FROM_BUFFER;
5085 (void) get_overlay_strings_1 (it, charpos, 1);
5087 CHECK_IT (it);
5089 /* Value is non-zero if we found at least one overlay string. */
5090 return STRINGP (it->string);
5095 /***********************************************************************
5096 Saving and restoring state
5097 ***********************************************************************/
5099 /* Save current settings of IT on IT->stack. Called, for example,
5100 before setting up IT for an overlay string, to be able to restore
5101 IT's settings to what they were after the overlay string has been
5102 processed. */
5104 static void
5105 push_it (struct it *it)
5107 struct iterator_stack_entry *p;
5109 xassert (it->sp < IT_STACK_SIZE);
5110 p = it->stack + it->sp;
5112 p->stop_charpos = it->stop_charpos;
5113 p->prev_stop = it->prev_stop;
5114 p->base_level_stop = it->base_level_stop;
5115 p->cmp_it = it->cmp_it;
5116 xassert (it->face_id >= 0);
5117 p->face_id = it->face_id;
5118 p->string = it->string;
5119 p->method = it->method;
5120 p->from_overlay = it->from_overlay;
5121 switch (p->method)
5123 case GET_FROM_IMAGE:
5124 p->u.image.object = it->object;
5125 p->u.image.image_id = it->image_id;
5126 p->u.image.slice = it->slice;
5127 break;
5128 case GET_FROM_STRETCH:
5129 p->u.stretch.object = it->object;
5130 break;
5132 p->position = it->position;
5133 p->current = it->current;
5134 p->end_charpos = it->end_charpos;
5135 p->string_nchars = it->string_nchars;
5136 p->area = it->area;
5137 p->multibyte_p = it->multibyte_p;
5138 p->avoid_cursor_p = it->avoid_cursor_p;
5139 p->space_width = it->space_width;
5140 p->font_height = it->font_height;
5141 p->voffset = it->voffset;
5142 p->string_from_display_prop_p = it->string_from_display_prop_p;
5143 p->display_ellipsis_p = 0;
5144 p->line_wrap = it->line_wrap;
5145 ++it->sp;
5148 static void
5149 iterate_out_of_display_property (struct it *it)
5151 /* Maybe initialize paragraph direction. If we are at the beginning
5152 of a new paragraph, next_element_from_buffer may not have a
5153 chance to do that. */
5154 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5155 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5156 /* prev_stop can be zero, so check against BEGV as well. */
5157 while (it->bidi_it.charpos >= BEGV
5158 && it->prev_stop <= it->bidi_it.charpos
5159 && it->bidi_it.charpos < CHARPOS (it->position))
5160 bidi_move_to_visually_next (&it->bidi_it);
5161 /* Record the stop_pos we just crossed, for when we cross it
5162 back, maybe. */
5163 if (it->bidi_it.charpos > CHARPOS (it->position))
5164 it->prev_stop = CHARPOS (it->position);
5165 /* If we ended up not where pop_it put us, resync IT's
5166 positional members with the bidi iterator. */
5167 if (it->bidi_it.charpos != CHARPOS (it->position))
5169 SET_TEXT_POS (it->position,
5170 it->bidi_it.charpos, it->bidi_it.bytepos);
5171 it->current.pos = it->position;
5175 /* Restore IT's settings from IT->stack. Called, for example, when no
5176 more overlay strings must be processed, and we return to delivering
5177 display elements from a buffer, or when the end of a string from a
5178 `display' property is reached and we return to delivering display
5179 elements from an overlay string, or from a buffer. */
5181 static void
5182 pop_it (struct it *it)
5184 struct iterator_stack_entry *p;
5186 xassert (it->sp > 0);
5187 --it->sp;
5188 p = it->stack + it->sp;
5189 it->stop_charpos = p->stop_charpos;
5190 it->prev_stop = p->prev_stop;
5191 it->base_level_stop = p->base_level_stop;
5192 it->cmp_it = p->cmp_it;
5193 it->face_id = p->face_id;
5194 it->current = p->current;
5195 it->position = p->position;
5196 it->string = p->string;
5197 it->from_overlay = p->from_overlay;
5198 if (NILP (it->string))
5199 SET_TEXT_POS (it->current.string_pos, -1, -1);
5200 it->method = p->method;
5201 switch (it->method)
5203 case GET_FROM_IMAGE:
5204 it->image_id = p->u.image.image_id;
5205 it->object = p->u.image.object;
5206 it->slice = p->u.image.slice;
5207 break;
5208 case GET_FROM_STRETCH:
5209 it->object = p->u.comp.object;
5210 break;
5211 case GET_FROM_BUFFER:
5212 it->object = it->w->buffer;
5213 if (it->bidi_p)
5215 /* Bidi-iterate until we get out of the portion of text, if
5216 any, covered by a `display' text property or an overlay
5217 with `display' property. (We cannot just jump there,
5218 because the internal coherency of the bidi iterator state
5219 can not be preserved across such jumps.) We also must
5220 determine the paragraph base direction if the overlay we
5221 just processed is at the beginning of a new
5222 paragraph. */
5223 iterate_out_of_display_property (it);
5225 break;
5226 case GET_FROM_STRING:
5227 it->object = it->string;
5228 break;
5229 case GET_FROM_DISPLAY_VECTOR:
5230 if (it->s)
5231 it->method = GET_FROM_C_STRING;
5232 else if (STRINGP (it->string))
5233 it->method = GET_FROM_STRING;
5234 else
5236 it->method = GET_FROM_BUFFER;
5237 it->object = it->w->buffer;
5240 it->end_charpos = p->end_charpos;
5241 it->string_nchars = p->string_nchars;
5242 it->area = p->area;
5243 it->multibyte_p = p->multibyte_p;
5244 it->avoid_cursor_p = p->avoid_cursor_p;
5245 it->space_width = p->space_width;
5246 it->font_height = p->font_height;
5247 it->voffset = p->voffset;
5248 it->string_from_display_prop_p = p->string_from_display_prop_p;
5249 it->line_wrap = p->line_wrap;
5254 /***********************************************************************
5255 Moving over lines
5256 ***********************************************************************/
5258 /* Set IT's current position to the previous line start. */
5260 static void
5261 back_to_previous_line_start (struct it *it)
5263 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5264 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5268 /* Move IT to the next line start.
5270 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5271 we skipped over part of the text (as opposed to moving the iterator
5272 continuously over the text). Otherwise, don't change the value
5273 of *SKIPPED_P.
5275 Newlines may come from buffer text, overlay strings, or strings
5276 displayed via the `display' property. That's the reason we can't
5277 simply use find_next_newline_no_quit.
5279 Note that this function may not skip over invisible text that is so
5280 because of text properties and immediately follows a newline. If
5281 it would, function reseat_at_next_visible_line_start, when called
5282 from set_iterator_to_next, would effectively make invisible
5283 characters following a newline part of the wrong glyph row, which
5284 leads to wrong cursor motion. */
5286 static int
5287 forward_to_next_line_start (struct it *it, int *skipped_p)
5289 int old_selective, newline_found_p, n;
5290 const int MAX_NEWLINE_DISTANCE = 500;
5292 /* If already on a newline, just consume it to avoid unintended
5293 skipping over invisible text below. */
5294 if (it->what == IT_CHARACTER
5295 && it->c == '\n'
5296 && CHARPOS (it->position) == IT_CHARPOS (*it))
5298 set_iterator_to_next (it, 0);
5299 it->c = 0;
5300 return 1;
5303 /* Don't handle selective display in the following. It's (a)
5304 unnecessary because it's done by the caller, and (b) leads to an
5305 infinite recursion because next_element_from_ellipsis indirectly
5306 calls this function. */
5307 old_selective = it->selective;
5308 it->selective = 0;
5310 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5311 from buffer text. */
5312 for (n = newline_found_p = 0;
5313 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5314 n += STRINGP (it->string) ? 0 : 1)
5316 if (!get_next_display_element (it))
5317 return 0;
5318 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5319 set_iterator_to_next (it, 0);
5322 /* If we didn't find a newline near enough, see if we can use a
5323 short-cut. */
5324 if (!newline_found_p)
5326 EMACS_INT start = IT_CHARPOS (*it);
5327 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5328 Lisp_Object pos;
5330 xassert (!STRINGP (it->string));
5332 /* If there isn't any `display' property in sight, and no
5333 overlays, we can just use the position of the newline in
5334 buffer text. */
5335 if (it->stop_charpos >= limit
5336 || ((pos = Fnext_single_property_change (make_number (start),
5337 Qdisplay,
5338 Qnil, make_number (limit)),
5339 NILP (pos))
5340 && next_overlay_change (start) == ZV))
5342 IT_CHARPOS (*it) = limit;
5343 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5344 *skipped_p = newline_found_p = 1;
5346 else
5348 while (get_next_display_element (it)
5349 && !newline_found_p)
5351 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5352 set_iterator_to_next (it, 0);
5357 it->selective = old_selective;
5358 return newline_found_p;
5362 /* Set IT's current position to the previous visible line start. Skip
5363 invisible text that is so either due to text properties or due to
5364 selective display. Caution: this does not change IT->current_x and
5365 IT->hpos. */
5367 static void
5368 back_to_previous_visible_line_start (struct it *it)
5370 while (IT_CHARPOS (*it) > BEGV)
5372 back_to_previous_line_start (it);
5374 if (IT_CHARPOS (*it) <= BEGV)
5375 break;
5377 /* If selective > 0, then lines indented more than its value are
5378 invisible. */
5379 if (it->selective > 0
5380 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5381 (double) it->selective)) /* iftc */
5382 continue;
5384 /* Check the newline before point for invisibility. */
5386 Lisp_Object prop;
5387 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5388 Qinvisible, it->window);
5389 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5390 continue;
5393 if (IT_CHARPOS (*it) <= BEGV)
5394 break;
5397 struct it it2;
5398 EMACS_INT pos;
5399 EMACS_INT beg, end;
5400 Lisp_Object val, overlay;
5402 /* If newline is part of a composition, continue from start of composition */
5403 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5404 && beg < IT_CHARPOS (*it))
5405 goto replaced;
5407 /* If newline is replaced by a display property, find start of overlay
5408 or interval and continue search from that point. */
5409 it2 = *it;
5410 pos = --IT_CHARPOS (it2);
5411 --IT_BYTEPOS (it2);
5412 it2.sp = 0;
5413 it2.string_from_display_prop_p = 0;
5414 if (handle_display_prop (&it2) == HANDLED_RETURN
5415 && !NILP (val = get_char_property_and_overlay
5416 (make_number (pos), Qdisplay, Qnil, &overlay))
5417 && (OVERLAYP (overlay)
5418 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5419 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5420 goto replaced;
5422 /* Newline is not replaced by anything -- so we are done. */
5423 break;
5425 replaced:
5426 if (beg < BEGV)
5427 beg = BEGV;
5428 IT_CHARPOS (*it) = beg;
5429 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5433 it->continuation_lines_width = 0;
5435 xassert (IT_CHARPOS (*it) >= BEGV);
5436 xassert (IT_CHARPOS (*it) == BEGV
5437 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5438 CHECK_IT (it);
5442 /* Reseat iterator IT at the previous visible line start. Skip
5443 invisible text that is so either due to text properties or due to
5444 selective display. At the end, update IT's overlay information,
5445 face information etc. */
5447 void
5448 reseat_at_previous_visible_line_start (struct it *it)
5450 back_to_previous_visible_line_start (it);
5451 reseat (it, it->current.pos, 1);
5452 CHECK_IT (it);
5456 /* Reseat iterator IT on the next visible line start in the current
5457 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5458 preceding the line start. Skip over invisible text that is so
5459 because of selective display. Compute faces, overlays etc at the
5460 new position. Note that this function does not skip over text that
5461 is invisible because of text properties. */
5463 static void
5464 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5466 int newline_found_p, skipped_p = 0;
5468 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5470 /* Skip over lines that are invisible because they are indented
5471 more than the value of IT->selective. */
5472 if (it->selective > 0)
5473 while (IT_CHARPOS (*it) < ZV
5474 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5475 (double) it->selective)) /* iftc */
5477 xassert (IT_BYTEPOS (*it) == BEGV
5478 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5479 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5482 /* Position on the newline if that's what's requested. */
5483 if (on_newline_p && newline_found_p)
5485 if (STRINGP (it->string))
5487 if (IT_STRING_CHARPOS (*it) > 0)
5489 --IT_STRING_CHARPOS (*it);
5490 --IT_STRING_BYTEPOS (*it);
5493 else if (IT_CHARPOS (*it) > BEGV)
5495 --IT_CHARPOS (*it);
5496 --IT_BYTEPOS (*it);
5497 reseat (it, it->current.pos, 0);
5500 else if (skipped_p)
5501 reseat (it, it->current.pos, 0);
5503 CHECK_IT (it);
5508 /***********************************************************************
5509 Changing an iterator's position
5510 ***********************************************************************/
5512 /* Change IT's current position to POS in current_buffer. If FORCE_P
5513 is non-zero, always check for text properties at the new position.
5514 Otherwise, text properties are only looked up if POS >=
5515 IT->check_charpos of a property. */
5517 static void
5518 reseat (struct it *it, struct text_pos pos, int force_p)
5520 EMACS_INT original_pos = IT_CHARPOS (*it);
5522 reseat_1 (it, pos, 0);
5524 /* Determine where to check text properties. Avoid doing it
5525 where possible because text property lookup is very expensive. */
5526 if (force_p
5527 || CHARPOS (pos) > it->stop_charpos
5528 || CHARPOS (pos) < original_pos)
5530 if (it->bidi_p)
5532 /* For bidi iteration, we need to prime prev_stop and
5533 base_level_stop with our best estimations. */
5534 if (CHARPOS (pos) < it->prev_stop)
5536 handle_stop_backwards (it, BEGV);
5537 if (CHARPOS (pos) < it->base_level_stop)
5538 it->base_level_stop = 0;
5540 else if (CHARPOS (pos) > it->stop_charpos
5541 && it->stop_charpos >= BEGV)
5542 handle_stop_backwards (it, it->stop_charpos);
5543 else /* force_p */
5544 handle_stop (it);
5546 else
5548 handle_stop (it);
5549 it->prev_stop = it->base_level_stop = 0;
5554 CHECK_IT (it);
5558 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5559 IT->stop_pos to POS, also. */
5561 static void
5562 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5564 /* Don't call this function when scanning a C string. */
5565 xassert (it->s == NULL);
5567 /* POS must be a reasonable value. */
5568 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5570 it->current.pos = it->position = pos;
5571 it->end_charpos = ZV;
5572 it->dpvec = NULL;
5573 it->current.dpvec_index = -1;
5574 it->current.overlay_string_index = -1;
5575 IT_STRING_CHARPOS (*it) = -1;
5576 IT_STRING_BYTEPOS (*it) = -1;
5577 it->string = Qnil;
5578 it->string_from_display_prop_p = 0;
5579 it->method = GET_FROM_BUFFER;
5580 it->object = it->w->buffer;
5581 it->area = TEXT_AREA;
5582 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5583 it->sp = 0;
5584 it->string_from_display_prop_p = 0;
5585 it->face_before_selective_p = 0;
5586 if (it->bidi_p)
5588 it->bidi_it.first_elt = 1;
5589 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5592 if (set_stop_p)
5594 it->stop_charpos = CHARPOS (pos);
5595 it->base_level_stop = CHARPOS (pos);
5600 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5601 If S is non-null, it is a C string to iterate over. Otherwise,
5602 STRING gives a Lisp string to iterate over.
5604 If PRECISION > 0, don't return more then PRECISION number of
5605 characters from the string.
5607 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5608 characters have been returned. FIELD_WIDTH < 0 means an infinite
5609 field width.
5611 MULTIBYTE = 0 means disable processing of multibyte characters,
5612 MULTIBYTE > 0 means enable it,
5613 MULTIBYTE < 0 means use IT->multibyte_p.
5615 IT must be initialized via a prior call to init_iterator before
5616 calling this function. */
5618 static void
5619 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5620 EMACS_INT charpos, EMACS_INT precision, int field_width,
5621 int multibyte)
5623 /* No region in strings. */
5624 it->region_beg_charpos = it->region_end_charpos = -1;
5626 /* No text property checks performed by default, but see below. */
5627 it->stop_charpos = -1;
5629 /* Set iterator position and end position. */
5630 memset (&it->current, 0, sizeof it->current);
5631 it->current.overlay_string_index = -1;
5632 it->current.dpvec_index = -1;
5633 xassert (charpos >= 0);
5635 /* If STRING is specified, use its multibyteness, otherwise use the
5636 setting of MULTIBYTE, if specified. */
5637 if (multibyte >= 0)
5638 it->multibyte_p = multibyte > 0;
5640 if (s == NULL)
5642 xassert (STRINGP (string));
5643 it->string = string;
5644 it->s = NULL;
5645 it->end_charpos = it->string_nchars = SCHARS (string);
5646 it->method = GET_FROM_STRING;
5647 it->current.string_pos = string_pos (charpos, string);
5649 else
5651 it->s = s;
5652 it->string = Qnil;
5654 /* Note that we use IT->current.pos, not it->current.string_pos,
5655 for displaying C strings. */
5656 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5657 if (it->multibyte_p)
5659 it->current.pos = c_string_pos (charpos, s, 1);
5660 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5662 else
5664 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5665 it->end_charpos = it->string_nchars = strlen (s);
5668 it->method = GET_FROM_C_STRING;
5671 /* PRECISION > 0 means don't return more than PRECISION characters
5672 from the string. */
5673 if (precision > 0 && it->end_charpos - charpos > precision)
5674 it->end_charpos = it->string_nchars = charpos + precision;
5676 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5677 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5678 FIELD_WIDTH < 0 means infinite field width. This is useful for
5679 padding with `-' at the end of a mode line. */
5680 if (field_width < 0)
5681 field_width = INFINITY;
5682 if (field_width > it->end_charpos - charpos)
5683 it->end_charpos = charpos + field_width;
5685 /* Use the standard display table for displaying strings. */
5686 if (DISP_TABLE_P (Vstandard_display_table))
5687 it->dp = XCHAR_TABLE (Vstandard_display_table);
5689 it->stop_charpos = charpos;
5690 if (s == NULL && it->multibyte_p)
5692 EMACS_INT endpos = SCHARS (it->string);
5693 if (endpos > it->end_charpos)
5694 endpos = it->end_charpos;
5695 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5696 it->string);
5698 CHECK_IT (it);
5703 /***********************************************************************
5704 Iteration
5705 ***********************************************************************/
5707 /* Map enum it_method value to corresponding next_element_from_* function. */
5709 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5711 next_element_from_buffer,
5712 next_element_from_display_vector,
5713 next_element_from_string,
5714 next_element_from_c_string,
5715 next_element_from_image,
5716 next_element_from_stretch
5719 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5722 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5723 (possibly with the following characters). */
5725 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5726 ((IT)->cmp_it.id >= 0 \
5727 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5728 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5729 END_CHARPOS, (IT)->w, \
5730 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5731 (IT)->string)))
5734 /* Load IT's display element fields with information about the next
5735 display element from the current position of IT. Value is zero if
5736 end of buffer (or C string) is reached. */
5738 static struct frame *last_escape_glyph_frame = NULL;
5739 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5740 static int last_escape_glyph_merged_face_id = 0;
5743 get_next_display_element (struct it *it)
5745 /* Non-zero means that we found a display element. Zero means that
5746 we hit the end of what we iterate over. Performance note: the
5747 function pointer `method' used here turns out to be faster than
5748 using a sequence of if-statements. */
5749 int success_p;
5751 get_next:
5752 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5754 if (it->what == IT_CHARACTER)
5756 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5757 and only if (a) the resolved directionality of that character
5758 is R..." */
5759 /* FIXME: Do we need an exception for characters from display
5760 tables? */
5761 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5762 it->c = bidi_mirror_char (it->c);
5763 /* Map via display table or translate control characters.
5764 IT->c, IT->len etc. have been set to the next character by
5765 the function call above. If we have a display table, and it
5766 contains an entry for IT->c, translate it. Don't do this if
5767 IT->c itself comes from a display table, otherwise we could
5768 end up in an infinite recursion. (An alternative could be to
5769 count the recursion depth of this function and signal an
5770 error when a certain maximum depth is reached.) Is it worth
5771 it? */
5772 if (success_p && it->dpvec == NULL)
5774 Lisp_Object dv;
5775 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5776 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5777 nbsp_or_shy = char_is_other;
5778 int c = it->c; /* This is the character to display. */
5780 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5782 xassert (SINGLE_BYTE_CHAR_P (c));
5783 if (unibyte_display_via_language_environment)
5785 c = DECODE_CHAR (unibyte, c);
5786 if (c < 0)
5787 c = BYTE8_TO_CHAR (it->c);
5789 else
5790 c = BYTE8_TO_CHAR (it->c);
5793 if (it->dp
5794 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5795 VECTORP (dv)))
5797 struct Lisp_Vector *v = XVECTOR (dv);
5799 /* Return the first character from the display table
5800 entry, if not empty. If empty, don't display the
5801 current character. */
5802 if (v->size)
5804 it->dpvec_char_len = it->len;
5805 it->dpvec = v->contents;
5806 it->dpend = v->contents + v->size;
5807 it->current.dpvec_index = 0;
5808 it->dpvec_face_id = -1;
5809 it->saved_face_id = it->face_id;
5810 it->method = GET_FROM_DISPLAY_VECTOR;
5811 it->ellipsis_p = 0;
5813 else
5815 set_iterator_to_next (it, 0);
5817 goto get_next;
5820 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5821 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5822 : c == 0xAD ? char_is_soft_hyphen
5823 : char_is_other);
5825 /* Translate control characters into `\003' or `^C' form.
5826 Control characters coming from a display table entry are
5827 currently not translated because we use IT->dpvec to hold
5828 the translation. This could easily be changed but I
5829 don't believe that it is worth doing.
5831 NBSP and SOFT-HYPEN are property translated too.
5833 Non-printable characters and raw-byte characters are also
5834 translated to octal form. */
5835 if (((c < ' ' || c == 127) /* ASCII control chars */
5836 ? (it->area != TEXT_AREA
5837 /* In mode line, treat \n, \t like other crl chars. */
5838 || (c != '\t'
5839 && it->glyph_row
5840 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5841 || (c != '\n' && c != '\t'))
5842 : (nbsp_or_shy
5843 || CHAR_BYTE8_P (c)
5844 || ! CHAR_PRINTABLE_P (c))))
5846 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5847 or a non-printable character which must be displayed
5848 either as '\003' or as `^C' where the '\\' and '^'
5849 can be defined in the display table. Fill
5850 IT->ctl_chars with glyphs for what we have to
5851 display. Then, set IT->dpvec to these glyphs. */
5852 Lisp_Object gc;
5853 int ctl_len;
5854 int face_id, lface_id = 0 ;
5855 int escape_glyph;
5857 /* Handle control characters with ^. */
5859 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5861 int g;
5863 g = '^'; /* default glyph for Control */
5864 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5865 if (it->dp
5866 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5867 && GLYPH_CODE_CHAR_VALID_P (gc))
5869 g = GLYPH_CODE_CHAR (gc);
5870 lface_id = GLYPH_CODE_FACE (gc);
5872 if (lface_id)
5874 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5876 else if (it->f == last_escape_glyph_frame
5877 && it->face_id == last_escape_glyph_face_id)
5879 face_id = last_escape_glyph_merged_face_id;
5881 else
5883 /* Merge the escape-glyph face into the current face. */
5884 face_id = merge_faces (it->f, Qescape_glyph, 0,
5885 it->face_id);
5886 last_escape_glyph_frame = it->f;
5887 last_escape_glyph_face_id = it->face_id;
5888 last_escape_glyph_merged_face_id = face_id;
5891 XSETINT (it->ctl_chars[0], g);
5892 XSETINT (it->ctl_chars[1], c ^ 0100);
5893 ctl_len = 2;
5894 goto display_control;
5897 /* Handle non-break space in the mode where it only gets
5898 highlighting. */
5900 if (EQ (Vnobreak_char_display, Qt)
5901 && nbsp_or_shy == char_is_nbsp)
5903 /* Merge the no-break-space face into the current face. */
5904 face_id = merge_faces (it->f, Qnobreak_space, 0,
5905 it->face_id);
5907 c = ' ';
5908 XSETINT (it->ctl_chars[0], ' ');
5909 ctl_len = 1;
5910 goto display_control;
5913 /* Handle sequences that start with the "escape glyph". */
5915 /* the default escape glyph is \. */
5916 escape_glyph = '\\';
5918 if (it->dp
5919 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5920 && GLYPH_CODE_CHAR_VALID_P (gc))
5922 escape_glyph = GLYPH_CODE_CHAR (gc);
5923 lface_id = GLYPH_CODE_FACE (gc);
5925 if (lface_id)
5927 /* The display table specified a face.
5928 Merge it into face_id and also into escape_glyph. */
5929 face_id = merge_faces (it->f, Qt, lface_id,
5930 it->face_id);
5932 else if (it->f == last_escape_glyph_frame
5933 && it->face_id == last_escape_glyph_face_id)
5935 face_id = last_escape_glyph_merged_face_id;
5937 else
5939 /* Merge the escape-glyph face into the current face. */
5940 face_id = merge_faces (it->f, Qescape_glyph, 0,
5941 it->face_id);
5942 last_escape_glyph_frame = it->f;
5943 last_escape_glyph_face_id = it->face_id;
5944 last_escape_glyph_merged_face_id = face_id;
5947 /* Handle soft hyphens in the mode where they only get
5948 highlighting. */
5950 if (EQ (Vnobreak_char_display, Qt)
5951 && nbsp_or_shy == char_is_soft_hyphen)
5953 XSETINT (it->ctl_chars[0], '-');
5954 ctl_len = 1;
5955 goto display_control;
5958 /* Handle non-break space and soft hyphen
5959 with the escape glyph. */
5961 if (nbsp_or_shy)
5963 XSETINT (it->ctl_chars[0], escape_glyph);
5964 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5965 XSETINT (it->ctl_chars[1], c);
5966 ctl_len = 2;
5967 goto display_control;
5971 char str[10];
5972 int len, i;
5974 if (CHAR_BYTE8_P (c))
5975 /* Display \200 instead of \17777600. */
5976 c = CHAR_TO_BYTE8 (c);
5977 len = sprintf (str, "%03o", c);
5979 XSETINT (it->ctl_chars[0], escape_glyph);
5980 for (i = 0; i < len; i++)
5981 XSETINT (it->ctl_chars[i + 1], str[i]);
5982 ctl_len = len + 1;
5985 display_control:
5986 /* Set up IT->dpvec and return first character from it. */
5987 it->dpvec_char_len = it->len;
5988 it->dpvec = it->ctl_chars;
5989 it->dpend = it->dpvec + ctl_len;
5990 it->current.dpvec_index = 0;
5991 it->dpvec_face_id = face_id;
5992 it->saved_face_id = it->face_id;
5993 it->method = GET_FROM_DISPLAY_VECTOR;
5994 it->ellipsis_p = 0;
5995 goto get_next;
5997 it->char_to_display = c;
5999 else if (success_p)
6001 it->char_to_display = it->c;
6005 #ifdef HAVE_WINDOW_SYSTEM
6006 /* Adjust face id for a multibyte character. There are no multibyte
6007 character in unibyte text. */
6008 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6009 && it->multibyte_p
6010 && success_p
6011 && FRAME_WINDOW_P (it->f))
6013 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6015 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6017 /* Automatic composition with glyph-string. */
6018 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6020 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6022 else
6024 EMACS_INT pos = (it->s ? -1
6025 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6026 : IT_CHARPOS (*it));
6028 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6029 it->string);
6032 #endif
6034 /* Is this character the last one of a run of characters with
6035 box? If yes, set IT->end_of_box_run_p to 1. */
6036 if (it->face_box_p
6037 && it->s == NULL)
6039 if (it->method == GET_FROM_STRING && it->sp)
6041 int face_id = underlying_face_id (it);
6042 struct face *face = FACE_FROM_ID (it->f, face_id);
6044 if (face)
6046 if (face->box == FACE_NO_BOX)
6048 /* If the box comes from face properties in a
6049 display string, check faces in that string. */
6050 int string_face_id = face_after_it_pos (it);
6051 it->end_of_box_run_p
6052 = (FACE_FROM_ID (it->f, string_face_id)->box
6053 == FACE_NO_BOX);
6055 /* Otherwise, the box comes from the underlying face.
6056 If this is the last string character displayed, check
6057 the next buffer location. */
6058 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6059 && (it->current.overlay_string_index
6060 == it->n_overlay_strings - 1))
6062 EMACS_INT ignore;
6063 int next_face_id;
6064 struct text_pos pos = it->current.pos;
6065 INC_TEXT_POS (pos, it->multibyte_p);
6067 next_face_id = face_at_buffer_position
6068 (it->w, CHARPOS (pos), it->region_beg_charpos,
6069 it->region_end_charpos, &ignore,
6070 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6071 -1);
6072 it->end_of_box_run_p
6073 = (FACE_FROM_ID (it->f, next_face_id)->box
6074 == FACE_NO_BOX);
6078 else
6080 int face_id = face_after_it_pos (it);
6081 it->end_of_box_run_p
6082 = (face_id != it->face_id
6083 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6087 /* Value is 0 if end of buffer or string reached. */
6088 return success_p;
6092 /* Move IT to the next display element.
6094 RESEAT_P non-zero means if called on a newline in buffer text,
6095 skip to the next visible line start.
6097 Functions get_next_display_element and set_iterator_to_next are
6098 separate because I find this arrangement easier to handle than a
6099 get_next_display_element function that also increments IT's
6100 position. The way it is we can first look at an iterator's current
6101 display element, decide whether it fits on a line, and if it does,
6102 increment the iterator position. The other way around we probably
6103 would either need a flag indicating whether the iterator has to be
6104 incremented the next time, or we would have to implement a
6105 decrement position function which would not be easy to write. */
6107 void
6108 set_iterator_to_next (struct it *it, int reseat_p)
6110 /* Reset flags indicating start and end of a sequence of characters
6111 with box. Reset them at the start of this function because
6112 moving the iterator to a new position might set them. */
6113 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6115 switch (it->method)
6117 case GET_FROM_BUFFER:
6118 /* The current display element of IT is a character from
6119 current_buffer. Advance in the buffer, and maybe skip over
6120 invisible lines that are so because of selective display. */
6121 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6122 reseat_at_next_visible_line_start (it, 0);
6123 else if (it->cmp_it.id >= 0)
6125 /* We are currently getting glyphs from a composition. */
6126 int i;
6128 if (! it->bidi_p)
6130 IT_CHARPOS (*it) += it->cmp_it.nchars;
6131 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6132 if (it->cmp_it.to < it->cmp_it.nglyphs)
6134 it->cmp_it.from = it->cmp_it.to;
6136 else
6138 it->cmp_it.id = -1;
6139 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6140 IT_BYTEPOS (*it),
6141 it->end_charpos, Qnil);
6144 else if (! it->cmp_it.reversed_p)
6146 /* Composition created while scanning forward. */
6147 /* Update IT's char/byte positions to point to the first
6148 character of the next grapheme cluster, or to the
6149 character visually after the current composition. */
6150 for (i = 0; i < it->cmp_it.nchars; i++)
6151 bidi_move_to_visually_next (&it->bidi_it);
6152 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6153 IT_CHARPOS (*it) = it->bidi_it.charpos;
6155 if (it->cmp_it.to < it->cmp_it.nglyphs)
6157 /* Proceed to the next grapheme cluster. */
6158 it->cmp_it.from = it->cmp_it.to;
6160 else
6162 /* No more grapheme clusters in this composition.
6163 Find the next stop position. */
6164 EMACS_INT stop = it->end_charpos;
6165 if (it->bidi_it.scan_dir < 0)
6166 /* Now we are scanning backward and don't know
6167 where to stop. */
6168 stop = -1;
6169 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6170 IT_BYTEPOS (*it), stop, Qnil);
6173 else
6175 /* Composition created while scanning backward. */
6176 /* Update IT's char/byte positions to point to the last
6177 character of the previous grapheme cluster, or the
6178 character visually after the current composition. */
6179 for (i = 0; i < it->cmp_it.nchars; i++)
6180 bidi_move_to_visually_next (&it->bidi_it);
6181 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6182 IT_CHARPOS (*it) = it->bidi_it.charpos;
6183 if (it->cmp_it.from > 0)
6185 /* Proceed to the previous grapheme cluster. */
6186 it->cmp_it.to = it->cmp_it.from;
6188 else
6190 /* No more grapheme clusters in this composition.
6191 Find the next stop position. */
6192 EMACS_INT stop = it->end_charpos;
6193 if (it->bidi_it.scan_dir < 0)
6194 /* Now we are scanning backward and don't know
6195 where to stop. */
6196 stop = -1;
6197 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6198 IT_BYTEPOS (*it), stop, Qnil);
6202 else
6204 xassert (it->len != 0);
6206 if (!it->bidi_p)
6208 IT_BYTEPOS (*it) += it->len;
6209 IT_CHARPOS (*it) += 1;
6211 else
6213 int prev_scan_dir = it->bidi_it.scan_dir;
6214 /* If this is a new paragraph, determine its base
6215 direction (a.k.a. its base embedding level). */
6216 if (it->bidi_it.new_paragraph)
6217 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6218 bidi_move_to_visually_next (&it->bidi_it);
6219 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6220 IT_CHARPOS (*it) = it->bidi_it.charpos;
6221 if (prev_scan_dir != it->bidi_it.scan_dir)
6223 /* As the scan direction was changed, we must
6224 re-compute the stop position for composition. */
6225 EMACS_INT stop = it->end_charpos;
6226 if (it->bidi_it.scan_dir < 0)
6227 stop = -1;
6228 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6229 IT_BYTEPOS (*it), stop, Qnil);
6232 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6234 break;
6236 case GET_FROM_C_STRING:
6237 /* Current display element of IT is from a C string. */
6238 IT_BYTEPOS (*it) += it->len;
6239 IT_CHARPOS (*it) += 1;
6240 break;
6242 case GET_FROM_DISPLAY_VECTOR:
6243 /* Current display element of IT is from a display table entry.
6244 Advance in the display table definition. Reset it to null if
6245 end reached, and continue with characters from buffers/
6246 strings. */
6247 ++it->current.dpvec_index;
6249 /* Restore face of the iterator to what they were before the
6250 display vector entry (these entries may contain faces). */
6251 it->face_id = it->saved_face_id;
6253 if (it->dpvec + it->current.dpvec_index == it->dpend)
6255 int recheck_faces = it->ellipsis_p;
6257 if (it->s)
6258 it->method = GET_FROM_C_STRING;
6259 else if (STRINGP (it->string))
6260 it->method = GET_FROM_STRING;
6261 else
6263 it->method = GET_FROM_BUFFER;
6264 it->object = it->w->buffer;
6267 it->dpvec = NULL;
6268 it->current.dpvec_index = -1;
6270 /* Skip over characters which were displayed via IT->dpvec. */
6271 if (it->dpvec_char_len < 0)
6272 reseat_at_next_visible_line_start (it, 1);
6273 else if (it->dpvec_char_len > 0)
6275 if (it->method == GET_FROM_STRING
6276 && it->n_overlay_strings > 0)
6277 it->ignore_overlay_strings_at_pos_p = 1;
6278 it->len = it->dpvec_char_len;
6279 set_iterator_to_next (it, reseat_p);
6282 /* Maybe recheck faces after display vector */
6283 if (recheck_faces)
6284 it->stop_charpos = IT_CHARPOS (*it);
6286 break;
6288 case GET_FROM_STRING:
6289 /* Current display element is a character from a Lisp string. */
6290 xassert (it->s == NULL && STRINGP (it->string));
6291 if (it->cmp_it.id >= 0)
6293 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6294 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6295 if (it->cmp_it.to < it->cmp_it.nglyphs)
6296 it->cmp_it.from = it->cmp_it.to;
6297 else
6299 it->cmp_it.id = -1;
6300 composition_compute_stop_pos (&it->cmp_it,
6301 IT_STRING_CHARPOS (*it),
6302 IT_STRING_BYTEPOS (*it),
6303 it->end_charpos, it->string);
6306 else
6308 IT_STRING_BYTEPOS (*it) += it->len;
6309 IT_STRING_CHARPOS (*it) += 1;
6312 consider_string_end:
6314 if (it->current.overlay_string_index >= 0)
6316 /* IT->string is an overlay string. Advance to the
6317 next, if there is one. */
6318 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6320 it->ellipsis_p = 0;
6321 next_overlay_string (it);
6322 if (it->ellipsis_p)
6323 setup_for_ellipsis (it, 0);
6326 else
6328 /* IT->string is not an overlay string. If we reached
6329 its end, and there is something on IT->stack, proceed
6330 with what is on the stack. This can be either another
6331 string, this time an overlay string, or a buffer. */
6332 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6333 && it->sp > 0)
6335 pop_it (it);
6336 if (it->method == GET_FROM_STRING)
6337 goto consider_string_end;
6340 break;
6342 case GET_FROM_IMAGE:
6343 case GET_FROM_STRETCH:
6344 /* The position etc with which we have to proceed are on
6345 the stack. The position may be at the end of a string,
6346 if the `display' property takes up the whole string. */
6347 xassert (it->sp > 0);
6348 pop_it (it);
6349 if (it->method == GET_FROM_STRING)
6350 goto consider_string_end;
6351 break;
6353 default:
6354 /* There are no other methods defined, so this should be a bug. */
6355 abort ();
6358 xassert (it->method != GET_FROM_STRING
6359 || (STRINGP (it->string)
6360 && IT_STRING_CHARPOS (*it) >= 0));
6363 /* Load IT's display element fields with information about the next
6364 display element which comes from a display table entry or from the
6365 result of translating a control character to one of the forms `^C'
6366 or `\003'.
6368 IT->dpvec holds the glyphs to return as characters.
6369 IT->saved_face_id holds the face id before the display vector--it
6370 is restored into IT->face_id in set_iterator_to_next. */
6372 static int
6373 next_element_from_display_vector (struct it *it)
6375 Lisp_Object gc;
6377 /* Precondition. */
6378 xassert (it->dpvec && it->current.dpvec_index >= 0);
6380 it->face_id = it->saved_face_id;
6382 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6383 That seemed totally bogus - so I changed it... */
6384 gc = it->dpvec[it->current.dpvec_index];
6386 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6388 it->c = GLYPH_CODE_CHAR (gc);
6389 it->len = CHAR_BYTES (it->c);
6391 /* The entry may contain a face id to use. Such a face id is
6392 the id of a Lisp face, not a realized face. A face id of
6393 zero means no face is specified. */
6394 if (it->dpvec_face_id >= 0)
6395 it->face_id = it->dpvec_face_id;
6396 else
6398 int lface_id = GLYPH_CODE_FACE (gc);
6399 if (lface_id > 0)
6400 it->face_id = merge_faces (it->f, Qt, lface_id,
6401 it->saved_face_id);
6404 else
6405 /* Display table entry is invalid. Return a space. */
6406 it->c = ' ', it->len = 1;
6408 /* Don't change position and object of the iterator here. They are
6409 still the values of the character that had this display table
6410 entry or was translated, and that's what we want. */
6411 it->what = IT_CHARACTER;
6412 return 1;
6416 /* Load IT with the next display element from Lisp string IT->string.
6417 IT->current.string_pos is the current position within the string.
6418 If IT->current.overlay_string_index >= 0, the Lisp string is an
6419 overlay string. */
6421 static int
6422 next_element_from_string (struct it *it)
6424 struct text_pos position;
6426 xassert (STRINGP (it->string));
6427 xassert (IT_STRING_CHARPOS (*it) >= 0);
6428 position = it->current.string_pos;
6430 /* Time to check for invisible text? */
6431 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6432 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6434 handle_stop (it);
6436 /* Since a handler may have changed IT->method, we must
6437 recurse here. */
6438 return GET_NEXT_DISPLAY_ELEMENT (it);
6441 if (it->current.overlay_string_index >= 0)
6443 /* Get the next character from an overlay string. In overlay
6444 strings, There is no field width or padding with spaces to
6445 do. */
6446 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6448 it->what = IT_EOB;
6449 return 0;
6451 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6452 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6453 && next_element_from_composition (it))
6455 return 1;
6457 else if (STRING_MULTIBYTE (it->string))
6459 const unsigned char *s = (SDATA (it->string)
6460 + IT_STRING_BYTEPOS (*it));
6461 it->c = string_char_and_length (s, &it->len);
6463 else
6465 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6466 it->len = 1;
6469 else
6471 /* Get the next character from a Lisp string that is not an
6472 overlay string. Such strings come from the mode line, for
6473 example. We may have to pad with spaces, or truncate the
6474 string. See also next_element_from_c_string. */
6475 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6477 it->what = IT_EOB;
6478 return 0;
6480 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6482 /* Pad with spaces. */
6483 it->c = ' ', it->len = 1;
6484 CHARPOS (position) = BYTEPOS (position) = -1;
6486 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6487 IT_STRING_BYTEPOS (*it), it->string_nchars)
6488 && next_element_from_composition (it))
6490 return 1;
6492 else if (STRING_MULTIBYTE (it->string))
6494 const unsigned char *s = (SDATA (it->string)
6495 + IT_STRING_BYTEPOS (*it));
6496 it->c = string_char_and_length (s, &it->len);
6498 else
6500 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6501 it->len = 1;
6505 /* Record what we have and where it came from. */
6506 it->what = IT_CHARACTER;
6507 it->object = it->string;
6508 it->position = position;
6509 return 1;
6513 /* Load IT with next display element from C string IT->s.
6514 IT->string_nchars is the maximum number of characters to return
6515 from the string. IT->end_charpos may be greater than
6516 IT->string_nchars when this function is called, in which case we
6517 may have to return padding spaces. Value is zero if end of string
6518 reached, including padding spaces. */
6520 static int
6521 next_element_from_c_string (struct it *it)
6523 int success_p = 1;
6525 xassert (it->s);
6526 it->what = IT_CHARACTER;
6527 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6528 it->object = Qnil;
6530 /* IT's position can be greater IT->string_nchars in case a field
6531 width or precision has been specified when the iterator was
6532 initialized. */
6533 if (IT_CHARPOS (*it) >= it->end_charpos)
6535 /* End of the game. */
6536 it->what = IT_EOB;
6537 success_p = 0;
6539 else if (IT_CHARPOS (*it) >= it->string_nchars)
6541 /* Pad with spaces. */
6542 it->c = ' ', it->len = 1;
6543 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6545 else if (it->multibyte_p)
6546 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6547 else
6548 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6550 return success_p;
6554 /* Set up IT to return characters from an ellipsis, if appropriate.
6555 The definition of the ellipsis glyphs may come from a display table
6556 entry. This function fills IT with the first glyph from the
6557 ellipsis if an ellipsis is to be displayed. */
6559 static int
6560 next_element_from_ellipsis (struct it *it)
6562 if (it->selective_display_ellipsis_p)
6563 setup_for_ellipsis (it, it->len);
6564 else
6566 /* The face at the current position may be different from the
6567 face we find after the invisible text. Remember what it
6568 was in IT->saved_face_id, and signal that it's there by
6569 setting face_before_selective_p. */
6570 it->saved_face_id = it->face_id;
6571 it->method = GET_FROM_BUFFER;
6572 it->object = it->w->buffer;
6573 reseat_at_next_visible_line_start (it, 1);
6574 it->face_before_selective_p = 1;
6577 return GET_NEXT_DISPLAY_ELEMENT (it);
6581 /* Deliver an image display element. The iterator IT is already
6582 filled with image information (done in handle_display_prop). Value
6583 is always 1. */
6586 static int
6587 next_element_from_image (struct it *it)
6589 it->what = IT_IMAGE;
6590 it->ignore_overlay_strings_at_pos_p = 0;
6591 return 1;
6595 /* Fill iterator IT with next display element from a stretch glyph
6596 property. IT->object is the value of the text property. Value is
6597 always 1. */
6599 static int
6600 next_element_from_stretch (struct it *it)
6602 it->what = IT_STRETCH;
6603 return 1;
6606 /* Scan forward from CHARPOS in the current buffer, until we find a
6607 stop position > current IT's position. Then handle the stop
6608 position before that. This is called when we bump into a stop
6609 position while reordering bidirectional text. CHARPOS should be
6610 the last previously processed stop_pos (or BEGV, if none were
6611 processed yet) whose position is less that IT's current
6612 position. */
6614 static void
6615 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6617 EMACS_INT where_we_are = IT_CHARPOS (*it);
6618 struct display_pos save_current = it->current;
6619 struct text_pos save_position = it->position;
6620 struct text_pos pos1;
6621 EMACS_INT next_stop;
6623 /* Scan in strict logical order. */
6624 it->bidi_p = 0;
6627 it->prev_stop = charpos;
6628 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6629 reseat_1 (it, pos1, 0);
6630 compute_stop_pos (it);
6631 /* We must advance forward, right? */
6632 if (it->stop_charpos <= it->prev_stop)
6633 abort ();
6634 charpos = it->stop_charpos;
6636 while (charpos <= where_we_are);
6638 next_stop = it->stop_charpos;
6639 it->stop_charpos = it->prev_stop;
6640 it->bidi_p = 1;
6641 it->current = save_current;
6642 it->position = save_position;
6643 handle_stop (it);
6644 it->stop_charpos = next_stop;
6647 /* Load IT with the next display element from current_buffer. Value
6648 is zero if end of buffer reached. IT->stop_charpos is the next
6649 position at which to stop and check for text properties or buffer
6650 end. */
6652 static int
6653 next_element_from_buffer (struct it *it)
6655 int success_p = 1;
6657 xassert (IT_CHARPOS (*it) >= BEGV);
6659 /* With bidi reordering, the character to display might not be the
6660 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6661 we were reseat()ed to a new buffer position, which is potentially
6662 a different paragraph. */
6663 if (it->bidi_p && it->bidi_it.first_elt)
6665 it->bidi_it.charpos = IT_CHARPOS (*it);
6666 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6667 if (it->bidi_it.bytepos == ZV_BYTE)
6669 /* Nothing to do, but reset the FIRST_ELT flag, like
6670 bidi_paragraph_init does, because we are not going to
6671 call it. */
6672 it->bidi_it.first_elt = 0;
6674 else if (it->bidi_it.bytepos == BEGV_BYTE
6675 /* FIXME: Should support all Unicode line separators. */
6676 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6677 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6679 /* If we are at the beginning of a line, we can produce the
6680 next element right away. */
6681 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6682 bidi_move_to_visually_next (&it->bidi_it);
6684 else
6686 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6688 /* We need to prime the bidi iterator starting at the line's
6689 beginning, before we will be able to produce the next
6690 element. */
6691 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6692 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6693 it->bidi_it.charpos = IT_CHARPOS (*it);
6694 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6695 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6698 /* Now return to buffer position where we were asked to
6699 get the next display element, and produce that. */
6700 bidi_move_to_visually_next (&it->bidi_it);
6702 while (it->bidi_it.bytepos != orig_bytepos
6703 && it->bidi_it.bytepos < ZV_BYTE);
6706 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6707 /* Adjust IT's position information to where we ended up. */
6708 IT_CHARPOS (*it) = it->bidi_it.charpos;
6709 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6710 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6712 EMACS_INT stop = it->end_charpos;
6713 if (it->bidi_it.scan_dir < 0)
6714 stop = -1;
6715 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6716 IT_BYTEPOS (*it), stop, Qnil);
6720 if (IT_CHARPOS (*it) >= it->stop_charpos)
6722 if (IT_CHARPOS (*it) >= it->end_charpos)
6724 int overlay_strings_follow_p;
6726 /* End of the game, except when overlay strings follow that
6727 haven't been returned yet. */
6728 if (it->overlay_strings_at_end_processed_p)
6729 overlay_strings_follow_p = 0;
6730 else
6732 it->overlay_strings_at_end_processed_p = 1;
6733 overlay_strings_follow_p = get_overlay_strings (it, 0);
6736 if (overlay_strings_follow_p)
6737 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6738 else
6740 it->what = IT_EOB;
6741 it->position = it->current.pos;
6742 success_p = 0;
6745 else if (!(!it->bidi_p
6746 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6747 || IT_CHARPOS (*it) == it->stop_charpos))
6749 /* With bidi non-linear iteration, we could find ourselves
6750 far beyond the last computed stop_charpos, with several
6751 other stop positions in between that we missed. Scan
6752 them all now, in buffer's logical order, until we find
6753 and handle the last stop_charpos that precedes our
6754 current position. */
6755 handle_stop_backwards (it, it->stop_charpos);
6756 return GET_NEXT_DISPLAY_ELEMENT (it);
6758 else
6760 if (it->bidi_p)
6762 /* Take note of the stop position we just moved across,
6763 for when we will move back across it. */
6764 it->prev_stop = it->stop_charpos;
6765 /* If we are at base paragraph embedding level, take
6766 note of the last stop position seen at this
6767 level. */
6768 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6769 it->base_level_stop = it->stop_charpos;
6771 handle_stop (it);
6772 return GET_NEXT_DISPLAY_ELEMENT (it);
6775 else if (it->bidi_p
6776 /* We can sometimes back up for reasons that have nothing
6777 to do with bidi reordering. E.g., compositions. The
6778 code below is only needed when we are above the base
6779 embedding level, so test for that explicitly. */
6780 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6781 && IT_CHARPOS (*it) < it->prev_stop)
6783 if (it->base_level_stop <= 0)
6784 it->base_level_stop = BEGV;
6785 if (IT_CHARPOS (*it) < it->base_level_stop)
6786 abort ();
6787 handle_stop_backwards (it, it->base_level_stop);
6788 return GET_NEXT_DISPLAY_ELEMENT (it);
6790 else
6792 /* No face changes, overlays etc. in sight, so just return a
6793 character from current_buffer. */
6794 unsigned char *p;
6795 EMACS_INT stop;
6797 /* Maybe run the redisplay end trigger hook. Performance note:
6798 This doesn't seem to cost measurable time. */
6799 if (it->redisplay_end_trigger_charpos
6800 && it->glyph_row
6801 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6802 run_redisplay_end_trigger_hook (it);
6804 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6805 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6806 stop)
6807 && next_element_from_composition (it))
6809 return 1;
6812 /* Get the next character, maybe multibyte. */
6813 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6814 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6815 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6816 else
6817 it->c = *p, it->len = 1;
6819 /* Record what we have and where it came from. */
6820 it->what = IT_CHARACTER;
6821 it->object = it->w->buffer;
6822 it->position = it->current.pos;
6824 /* Normally we return the character found above, except when we
6825 really want to return an ellipsis for selective display. */
6826 if (it->selective)
6828 if (it->c == '\n')
6830 /* A value of selective > 0 means hide lines indented more
6831 than that number of columns. */
6832 if (it->selective > 0
6833 && IT_CHARPOS (*it) + 1 < ZV
6834 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6835 IT_BYTEPOS (*it) + 1,
6836 (double) it->selective)) /* iftc */
6838 success_p = next_element_from_ellipsis (it);
6839 it->dpvec_char_len = -1;
6842 else if (it->c == '\r' && it->selective == -1)
6844 /* A value of selective == -1 means that everything from the
6845 CR to the end of the line is invisible, with maybe an
6846 ellipsis displayed for it. */
6847 success_p = next_element_from_ellipsis (it);
6848 it->dpvec_char_len = -1;
6853 /* Value is zero if end of buffer reached. */
6854 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6855 return success_p;
6859 /* Run the redisplay end trigger hook for IT. */
6861 static void
6862 run_redisplay_end_trigger_hook (struct it *it)
6864 Lisp_Object args[3];
6866 /* IT->glyph_row should be non-null, i.e. we should be actually
6867 displaying something, or otherwise we should not run the hook. */
6868 xassert (it->glyph_row);
6870 /* Set up hook arguments. */
6871 args[0] = Qredisplay_end_trigger_functions;
6872 args[1] = it->window;
6873 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6874 it->redisplay_end_trigger_charpos = 0;
6876 /* Since we are *trying* to run these functions, don't try to run
6877 them again, even if they get an error. */
6878 it->w->redisplay_end_trigger = Qnil;
6879 Frun_hook_with_args (3, args);
6881 /* Notice if it changed the face of the character we are on. */
6882 handle_face_prop (it);
6886 /* Deliver a composition display element. Unlike the other
6887 next_element_from_XXX, this function is not registered in the array
6888 get_next_element[]. It is called from next_element_from_buffer and
6889 next_element_from_string when necessary. */
6891 static int
6892 next_element_from_composition (struct it *it)
6894 it->what = IT_COMPOSITION;
6895 it->len = it->cmp_it.nbytes;
6896 if (STRINGP (it->string))
6898 if (it->c < 0)
6900 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6901 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6902 return 0;
6904 it->position = it->current.string_pos;
6905 it->object = it->string;
6906 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6907 IT_STRING_BYTEPOS (*it), it->string);
6909 else
6911 if (it->c < 0)
6913 IT_CHARPOS (*it) += it->cmp_it.nchars;
6914 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6915 if (it->bidi_p)
6917 if (it->bidi_it.new_paragraph)
6918 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6919 /* Resync the bidi iterator with IT's new position.
6920 FIXME: this doesn't support bidirectional text. */
6921 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6922 bidi_move_to_visually_next (&it->bidi_it);
6924 return 0;
6926 it->position = it->current.pos;
6927 it->object = it->w->buffer;
6928 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6929 IT_BYTEPOS (*it), Qnil);
6931 return 1;
6936 /***********************************************************************
6937 Moving an iterator without producing glyphs
6938 ***********************************************************************/
6940 /* Check if iterator is at a position corresponding to a valid buffer
6941 position after some move_it_ call. */
6943 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6944 ((it)->method == GET_FROM_STRING \
6945 ? IT_STRING_CHARPOS (*it) == 0 \
6946 : 1)
6949 /* Move iterator IT to a specified buffer or X position within one
6950 line on the display without producing glyphs.
6952 OP should be a bit mask including some or all of these bits:
6953 MOVE_TO_X: Stop upon reaching x-position TO_X.
6954 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6955 Regardless of OP's value, stop upon reaching the end of the display line.
6957 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6958 This means, in particular, that TO_X includes window's horizontal
6959 scroll amount.
6961 The return value has several possible values that
6962 say what condition caused the scan to stop:
6964 MOVE_POS_MATCH_OR_ZV
6965 - when TO_POS or ZV was reached.
6967 MOVE_X_REACHED
6968 -when TO_X was reached before TO_POS or ZV were reached.
6970 MOVE_LINE_CONTINUED
6971 - when we reached the end of the display area and the line must
6972 be continued.
6974 MOVE_LINE_TRUNCATED
6975 - when we reached the end of the display area and the line is
6976 truncated.
6978 MOVE_NEWLINE_OR_CR
6979 - when we stopped at a line end, i.e. a newline or a CR and selective
6980 display is on. */
6982 static enum move_it_result
6983 move_it_in_display_line_to (struct it *it,
6984 EMACS_INT to_charpos, int to_x,
6985 enum move_operation_enum op)
6987 enum move_it_result result = MOVE_UNDEFINED;
6988 struct glyph_row *saved_glyph_row;
6989 struct it wrap_it, atpos_it, atx_it;
6990 int may_wrap = 0;
6991 enum it_method prev_method = it->method;
6992 EMACS_INT prev_pos = IT_CHARPOS (*it);
6994 /* Don't produce glyphs in produce_glyphs. */
6995 saved_glyph_row = it->glyph_row;
6996 it->glyph_row = NULL;
6998 /* Use wrap_it to save a copy of IT wherever a word wrap could
6999 occur. Use atpos_it to save a copy of IT at the desired buffer
7000 position, if found, so that we can scan ahead and check if the
7001 word later overshoots the window edge. Use atx_it similarly, for
7002 pixel positions. */
7003 wrap_it.sp = -1;
7004 atpos_it.sp = -1;
7005 atx_it.sp = -1;
7007 #define BUFFER_POS_REACHED_P() \
7008 ((op & MOVE_TO_POS) != 0 \
7009 && BUFFERP (it->object) \
7010 && (IT_CHARPOS (*it) == to_charpos \
7011 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7012 && (it->method == GET_FROM_BUFFER \
7013 || (it->method == GET_FROM_DISPLAY_VECTOR \
7014 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7016 /* If there's a line-/wrap-prefix, handle it. */
7017 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7018 && it->current_y < it->last_visible_y)
7019 handle_line_prefix (it);
7021 while (1)
7023 int x, i, ascent = 0, descent = 0;
7025 /* Utility macro to reset an iterator with x, ascent, and descent. */
7026 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7027 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7028 (IT)->max_descent = descent)
7030 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7031 glyph). */
7032 if ((op & MOVE_TO_POS) != 0
7033 && BUFFERP (it->object)
7034 && it->method == GET_FROM_BUFFER
7035 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7036 || (it->bidi_p
7037 && (prev_method == GET_FROM_IMAGE
7038 || prev_method == GET_FROM_STRETCH)
7039 /* Passed TO_CHARPOS from left to right. */
7040 && ((prev_pos < to_charpos
7041 && IT_CHARPOS (*it) > to_charpos)
7042 /* Passed TO_CHARPOS from right to left. */
7043 || (prev_pos > to_charpos
7044 && IT_CHARPOS (*it) < to_charpos)))))
7046 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7048 result = MOVE_POS_MATCH_OR_ZV;
7049 break;
7051 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7052 /* If wrap_it is valid, the current position might be in a
7053 word that is wrapped. So, save the iterator in
7054 atpos_it and continue to see if wrapping happens. */
7055 atpos_it = *it;
7058 prev_method = it->method;
7059 if (it->method == GET_FROM_BUFFER)
7060 prev_pos = IT_CHARPOS (*it);
7061 /* Stop when ZV reached.
7062 We used to stop here when TO_CHARPOS reached as well, but that is
7063 too soon if this glyph does not fit on this line. So we handle it
7064 explicitly below. */
7065 if (!get_next_display_element (it))
7067 result = MOVE_POS_MATCH_OR_ZV;
7068 break;
7071 if (it->line_wrap == TRUNCATE)
7073 if (BUFFER_POS_REACHED_P ())
7075 result = MOVE_POS_MATCH_OR_ZV;
7076 break;
7079 else
7081 if (it->line_wrap == WORD_WRAP)
7083 if (IT_DISPLAYING_WHITESPACE (it))
7084 may_wrap = 1;
7085 else if (may_wrap)
7087 /* We have reached a glyph that follows one or more
7088 whitespace characters. If the position is
7089 already found, we are done. */
7090 if (atpos_it.sp >= 0)
7092 *it = atpos_it;
7093 result = MOVE_POS_MATCH_OR_ZV;
7094 goto done;
7096 if (atx_it.sp >= 0)
7098 *it = atx_it;
7099 result = MOVE_X_REACHED;
7100 goto done;
7102 /* Otherwise, we can wrap here. */
7103 wrap_it = *it;
7104 may_wrap = 0;
7109 /* Remember the line height for the current line, in case
7110 the next element doesn't fit on the line. */
7111 ascent = it->max_ascent;
7112 descent = it->max_descent;
7114 /* The call to produce_glyphs will get the metrics of the
7115 display element IT is loaded with. Record the x-position
7116 before this display element, in case it doesn't fit on the
7117 line. */
7118 x = it->current_x;
7120 PRODUCE_GLYPHS (it);
7122 if (it->area != TEXT_AREA)
7124 set_iterator_to_next (it, 1);
7125 continue;
7128 /* The number of glyphs we get back in IT->nglyphs will normally
7129 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7130 character on a terminal frame, or (iii) a line end. For the
7131 second case, IT->nglyphs - 1 padding glyphs will be present.
7132 (On X frames, there is only one glyph produced for a
7133 composite character.)
7135 The behavior implemented below means, for continuation lines,
7136 that as many spaces of a TAB as fit on the current line are
7137 displayed there. For terminal frames, as many glyphs of a
7138 multi-glyph character are displayed in the current line, too.
7139 This is what the old redisplay code did, and we keep it that
7140 way. Under X, the whole shape of a complex character must
7141 fit on the line or it will be completely displayed in the
7142 next line.
7144 Note that both for tabs and padding glyphs, all glyphs have
7145 the same width. */
7146 if (it->nglyphs)
7148 /* More than one glyph or glyph doesn't fit on line. All
7149 glyphs have the same width. */
7150 int single_glyph_width = it->pixel_width / it->nglyphs;
7151 int new_x;
7152 int x_before_this_char = x;
7153 int hpos_before_this_char = it->hpos;
7155 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7157 new_x = x + single_glyph_width;
7159 /* We want to leave anything reaching TO_X to the caller. */
7160 if ((op & MOVE_TO_X) && new_x > to_x)
7162 if (BUFFER_POS_REACHED_P ())
7164 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7165 goto buffer_pos_reached;
7166 if (atpos_it.sp < 0)
7168 atpos_it = *it;
7169 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7172 else
7174 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7176 it->current_x = x;
7177 result = MOVE_X_REACHED;
7178 break;
7180 if (atx_it.sp < 0)
7182 atx_it = *it;
7183 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7188 if (/* Lines are continued. */
7189 it->line_wrap != TRUNCATE
7190 && (/* And glyph doesn't fit on the line. */
7191 new_x > it->last_visible_x
7192 /* Or it fits exactly and we're on a window
7193 system frame. */
7194 || (new_x == it->last_visible_x
7195 && FRAME_WINDOW_P (it->f))))
7197 if (/* IT->hpos == 0 means the very first glyph
7198 doesn't fit on the line, e.g. a wide image. */
7199 it->hpos == 0
7200 || (new_x == it->last_visible_x
7201 && FRAME_WINDOW_P (it->f)))
7203 ++it->hpos;
7204 it->current_x = new_x;
7206 /* The character's last glyph just barely fits
7207 in this row. */
7208 if (i == it->nglyphs - 1)
7210 /* If this is the destination position,
7211 return a position *before* it in this row,
7212 now that we know it fits in this row. */
7213 if (BUFFER_POS_REACHED_P ())
7215 if (it->line_wrap != WORD_WRAP
7216 || wrap_it.sp < 0)
7218 it->hpos = hpos_before_this_char;
7219 it->current_x = x_before_this_char;
7220 result = MOVE_POS_MATCH_OR_ZV;
7221 break;
7223 if (it->line_wrap == WORD_WRAP
7224 && atpos_it.sp < 0)
7226 atpos_it = *it;
7227 atpos_it.current_x = x_before_this_char;
7228 atpos_it.hpos = hpos_before_this_char;
7232 set_iterator_to_next (it, 1);
7233 /* On graphical terminals, newlines may
7234 "overflow" into the fringe if
7235 overflow-newline-into-fringe is non-nil.
7236 On text-only terminals, newlines may
7237 overflow into the last glyph on the
7238 display line.*/
7239 if (!FRAME_WINDOW_P (it->f)
7240 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7242 if (!get_next_display_element (it))
7244 result = MOVE_POS_MATCH_OR_ZV;
7245 break;
7247 if (BUFFER_POS_REACHED_P ())
7249 if (ITERATOR_AT_END_OF_LINE_P (it))
7250 result = MOVE_POS_MATCH_OR_ZV;
7251 else
7252 result = MOVE_LINE_CONTINUED;
7253 break;
7255 if (ITERATOR_AT_END_OF_LINE_P (it))
7257 result = MOVE_NEWLINE_OR_CR;
7258 break;
7263 else
7264 IT_RESET_X_ASCENT_DESCENT (it);
7266 if (wrap_it.sp >= 0)
7268 *it = wrap_it;
7269 atpos_it.sp = -1;
7270 atx_it.sp = -1;
7273 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7274 IT_CHARPOS (*it)));
7275 result = MOVE_LINE_CONTINUED;
7276 break;
7279 if (BUFFER_POS_REACHED_P ())
7281 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7282 goto buffer_pos_reached;
7283 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7285 atpos_it = *it;
7286 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7290 if (new_x > it->first_visible_x)
7292 /* Glyph is visible. Increment number of glyphs that
7293 would be displayed. */
7294 ++it->hpos;
7298 if (result != MOVE_UNDEFINED)
7299 break;
7301 else if (BUFFER_POS_REACHED_P ())
7303 buffer_pos_reached:
7304 IT_RESET_X_ASCENT_DESCENT (it);
7305 result = MOVE_POS_MATCH_OR_ZV;
7306 break;
7308 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7310 /* Stop when TO_X specified and reached. This check is
7311 necessary here because of lines consisting of a line end,
7312 only. The line end will not produce any glyphs and we
7313 would never get MOVE_X_REACHED. */
7314 xassert (it->nglyphs == 0);
7315 result = MOVE_X_REACHED;
7316 break;
7319 /* Is this a line end? If yes, we're done. */
7320 if (ITERATOR_AT_END_OF_LINE_P (it))
7322 result = MOVE_NEWLINE_OR_CR;
7323 break;
7326 if (it->method == GET_FROM_BUFFER)
7327 prev_pos = IT_CHARPOS (*it);
7328 /* The current display element has been consumed. Advance
7329 to the next. */
7330 set_iterator_to_next (it, 1);
7332 /* Stop if lines are truncated and IT's current x-position is
7333 past the right edge of the window now. */
7334 if (it->line_wrap == TRUNCATE
7335 && it->current_x >= it->last_visible_x)
7337 if (!FRAME_WINDOW_P (it->f)
7338 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7340 if (!get_next_display_element (it)
7341 || BUFFER_POS_REACHED_P ())
7343 result = MOVE_POS_MATCH_OR_ZV;
7344 break;
7346 if (ITERATOR_AT_END_OF_LINE_P (it))
7348 result = MOVE_NEWLINE_OR_CR;
7349 break;
7352 result = MOVE_LINE_TRUNCATED;
7353 break;
7355 #undef IT_RESET_X_ASCENT_DESCENT
7358 #undef BUFFER_POS_REACHED_P
7360 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7361 restore the saved iterator. */
7362 if (atpos_it.sp >= 0)
7363 *it = atpos_it;
7364 else if (atx_it.sp >= 0)
7365 *it = atx_it;
7367 done:
7369 /* Restore the iterator settings altered at the beginning of this
7370 function. */
7371 it->glyph_row = saved_glyph_row;
7372 return result;
7375 /* For external use. */
7376 void
7377 move_it_in_display_line (struct it *it,
7378 EMACS_INT to_charpos, int to_x,
7379 enum move_operation_enum op)
7381 if (it->line_wrap == WORD_WRAP
7382 && (op & MOVE_TO_X))
7384 struct it save_it = *it;
7385 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7386 /* When word-wrap is on, TO_X may lie past the end
7387 of a wrapped line. Then it->current is the
7388 character on the next line, so backtrack to the
7389 space before the wrap point. */
7390 if (skip == MOVE_LINE_CONTINUED)
7392 int prev_x = max (it->current_x - 1, 0);
7393 *it = save_it;
7394 move_it_in_display_line_to
7395 (it, -1, prev_x, MOVE_TO_X);
7398 else
7399 move_it_in_display_line_to (it, to_charpos, to_x, op);
7403 /* Move IT forward until it satisfies one or more of the criteria in
7404 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7406 OP is a bit-mask that specifies where to stop, and in particular,
7407 which of those four position arguments makes a difference. See the
7408 description of enum move_operation_enum.
7410 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7411 screen line, this function will set IT to the next position >
7412 TO_CHARPOS. */
7414 void
7415 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7417 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7418 int line_height, line_start_x = 0, reached = 0;
7420 for (;;)
7422 if (op & MOVE_TO_VPOS)
7424 /* If no TO_CHARPOS and no TO_X specified, stop at the
7425 start of the line TO_VPOS. */
7426 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7428 if (it->vpos == to_vpos)
7430 reached = 1;
7431 break;
7433 else
7434 skip = move_it_in_display_line_to (it, -1, -1, 0);
7436 else
7438 /* TO_VPOS >= 0 means stop at TO_X in the line at
7439 TO_VPOS, or at TO_POS, whichever comes first. */
7440 if (it->vpos == to_vpos)
7442 reached = 2;
7443 break;
7446 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7448 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7450 reached = 3;
7451 break;
7453 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7455 /* We have reached TO_X but not in the line we want. */
7456 skip = move_it_in_display_line_to (it, to_charpos,
7457 -1, MOVE_TO_POS);
7458 if (skip == MOVE_POS_MATCH_OR_ZV)
7460 reached = 4;
7461 break;
7466 else if (op & MOVE_TO_Y)
7468 struct it it_backup;
7470 if (it->line_wrap == WORD_WRAP)
7471 it_backup = *it;
7473 /* TO_Y specified means stop at TO_X in the line containing
7474 TO_Y---or at TO_CHARPOS if this is reached first. The
7475 problem is that we can't really tell whether the line
7476 contains TO_Y before we have completely scanned it, and
7477 this may skip past TO_X. What we do is to first scan to
7478 TO_X.
7480 If TO_X is not specified, use a TO_X of zero. The reason
7481 is to make the outcome of this function more predictable.
7482 If we didn't use TO_X == 0, we would stop at the end of
7483 the line which is probably not what a caller would expect
7484 to happen. */
7485 skip = move_it_in_display_line_to
7486 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7487 (MOVE_TO_X | (op & MOVE_TO_POS)));
7489 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7490 if (skip == MOVE_POS_MATCH_OR_ZV)
7491 reached = 5;
7492 else if (skip == MOVE_X_REACHED)
7494 /* If TO_X was reached, we want to know whether TO_Y is
7495 in the line. We know this is the case if the already
7496 scanned glyphs make the line tall enough. Otherwise,
7497 we must check by scanning the rest of the line. */
7498 line_height = it->max_ascent + it->max_descent;
7499 if (to_y >= it->current_y
7500 && to_y < it->current_y + line_height)
7502 reached = 6;
7503 break;
7505 it_backup = *it;
7506 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7507 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7508 op & MOVE_TO_POS);
7509 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7510 line_height = it->max_ascent + it->max_descent;
7511 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7513 if (to_y >= it->current_y
7514 && to_y < it->current_y + line_height)
7516 /* If TO_Y is in this line and TO_X was reached
7517 above, we scanned too far. We have to restore
7518 IT's settings to the ones before skipping. */
7519 *it = it_backup;
7520 reached = 6;
7522 else
7524 skip = skip2;
7525 if (skip == MOVE_POS_MATCH_OR_ZV)
7526 reached = 7;
7529 else
7531 /* Check whether TO_Y is in this line. */
7532 line_height = it->max_ascent + it->max_descent;
7533 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7535 if (to_y >= it->current_y
7536 && to_y < it->current_y + line_height)
7538 /* When word-wrap is on, TO_X may lie past the end
7539 of a wrapped line. Then it->current is the
7540 character on the next line, so backtrack to the
7541 space before the wrap point. */
7542 if (skip == MOVE_LINE_CONTINUED
7543 && it->line_wrap == WORD_WRAP)
7545 int prev_x = max (it->current_x - 1, 0);
7546 *it = it_backup;
7547 skip = move_it_in_display_line_to
7548 (it, -1, prev_x, MOVE_TO_X);
7550 reached = 6;
7554 if (reached)
7555 break;
7557 else if (BUFFERP (it->object)
7558 && (it->method == GET_FROM_BUFFER
7559 || it->method == GET_FROM_STRETCH)
7560 && IT_CHARPOS (*it) >= to_charpos)
7561 skip = MOVE_POS_MATCH_OR_ZV;
7562 else
7563 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7565 switch (skip)
7567 case MOVE_POS_MATCH_OR_ZV:
7568 reached = 8;
7569 goto out;
7571 case MOVE_NEWLINE_OR_CR:
7572 set_iterator_to_next (it, 1);
7573 it->continuation_lines_width = 0;
7574 break;
7576 case MOVE_LINE_TRUNCATED:
7577 it->continuation_lines_width = 0;
7578 reseat_at_next_visible_line_start (it, 0);
7579 if ((op & MOVE_TO_POS) != 0
7580 && IT_CHARPOS (*it) > to_charpos)
7582 reached = 9;
7583 goto out;
7585 break;
7587 case MOVE_LINE_CONTINUED:
7588 /* For continued lines ending in a tab, some of the glyphs
7589 associated with the tab are displayed on the current
7590 line. Since it->current_x does not include these glyphs,
7591 we use it->last_visible_x instead. */
7592 if (it->c == '\t')
7594 it->continuation_lines_width += it->last_visible_x;
7595 /* When moving by vpos, ensure that the iterator really
7596 advances to the next line (bug#847, bug#969). Fixme:
7597 do we need to do this in other circumstances? */
7598 if (it->current_x != it->last_visible_x
7599 && (op & MOVE_TO_VPOS)
7600 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7602 line_start_x = it->current_x + it->pixel_width
7603 - it->last_visible_x;
7604 set_iterator_to_next (it, 0);
7607 else
7608 it->continuation_lines_width += it->current_x;
7609 break;
7611 default:
7612 abort ();
7615 /* Reset/increment for the next run. */
7616 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7617 it->current_x = line_start_x;
7618 line_start_x = 0;
7619 it->hpos = 0;
7620 it->current_y += it->max_ascent + it->max_descent;
7621 ++it->vpos;
7622 last_height = it->max_ascent + it->max_descent;
7623 last_max_ascent = it->max_ascent;
7624 it->max_ascent = it->max_descent = 0;
7627 out:
7629 /* On text terminals, we may stop at the end of a line in the middle
7630 of a multi-character glyph. If the glyph itself is continued,
7631 i.e. it is actually displayed on the next line, don't treat this
7632 stopping point as valid; move to the next line instead (unless
7633 that brings us offscreen). */
7634 if (!FRAME_WINDOW_P (it->f)
7635 && op & MOVE_TO_POS
7636 && IT_CHARPOS (*it) == to_charpos
7637 && it->what == IT_CHARACTER
7638 && it->nglyphs > 1
7639 && it->line_wrap == WINDOW_WRAP
7640 && it->current_x == it->last_visible_x - 1
7641 && it->c != '\n'
7642 && it->c != '\t'
7643 && it->vpos < XFASTINT (it->w->window_end_vpos))
7645 it->continuation_lines_width += it->current_x;
7646 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7647 it->current_y += it->max_ascent + it->max_descent;
7648 ++it->vpos;
7649 last_height = it->max_ascent + it->max_descent;
7650 last_max_ascent = it->max_ascent;
7653 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7657 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7659 If DY > 0, move IT backward at least that many pixels. DY = 0
7660 means move IT backward to the preceding line start or BEGV. This
7661 function may move over more than DY pixels if IT->current_y - DY
7662 ends up in the middle of a line; in this case IT->current_y will be
7663 set to the top of the line moved to. */
7665 void
7666 move_it_vertically_backward (struct it *it, int dy)
7668 int nlines, h;
7669 struct it it2, it3;
7670 EMACS_INT start_pos;
7672 move_further_back:
7673 xassert (dy >= 0);
7675 start_pos = IT_CHARPOS (*it);
7677 /* Estimate how many newlines we must move back. */
7678 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7680 /* Set the iterator's position that many lines back. */
7681 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7682 back_to_previous_visible_line_start (it);
7684 /* Reseat the iterator here. When moving backward, we don't want
7685 reseat to skip forward over invisible text, set up the iterator
7686 to deliver from overlay strings at the new position etc. So,
7687 use reseat_1 here. */
7688 reseat_1 (it, it->current.pos, 1);
7690 /* We are now surely at a line start. */
7691 it->current_x = it->hpos = 0;
7692 it->continuation_lines_width = 0;
7694 /* Move forward and see what y-distance we moved. First move to the
7695 start of the next line so that we get its height. We need this
7696 height to be able to tell whether we reached the specified
7697 y-distance. */
7698 it2 = *it;
7699 it2.max_ascent = it2.max_descent = 0;
7702 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7703 MOVE_TO_POS | MOVE_TO_VPOS);
7705 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7706 xassert (IT_CHARPOS (*it) >= BEGV);
7707 it3 = it2;
7709 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7710 xassert (IT_CHARPOS (*it) >= BEGV);
7711 /* H is the actual vertical distance from the position in *IT
7712 and the starting position. */
7713 h = it2.current_y - it->current_y;
7714 /* NLINES is the distance in number of lines. */
7715 nlines = it2.vpos - it->vpos;
7717 /* Correct IT's y and vpos position
7718 so that they are relative to the starting point. */
7719 it->vpos -= nlines;
7720 it->current_y -= h;
7722 if (dy == 0)
7724 /* DY == 0 means move to the start of the screen line. The
7725 value of nlines is > 0 if continuation lines were involved. */
7726 if (nlines > 0)
7727 move_it_by_lines (it, nlines, 1);
7729 else
7731 /* The y-position we try to reach, relative to *IT.
7732 Note that H has been subtracted in front of the if-statement. */
7733 int target_y = it->current_y + h - dy;
7734 int y0 = it3.current_y;
7735 int y1 = line_bottom_y (&it3);
7736 int line_height = y1 - y0;
7738 /* If we did not reach target_y, try to move further backward if
7739 we can. If we moved too far backward, try to move forward. */
7740 if (target_y < it->current_y
7741 /* This is heuristic. In a window that's 3 lines high, with
7742 a line height of 13 pixels each, recentering with point
7743 on the bottom line will try to move -39/2 = 19 pixels
7744 backward. Try to avoid moving into the first line. */
7745 && (it->current_y - target_y
7746 > min (window_box_height (it->w), line_height * 2 / 3))
7747 && IT_CHARPOS (*it) > BEGV)
7749 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7750 target_y - it->current_y));
7751 dy = it->current_y - target_y;
7752 goto move_further_back;
7754 else if (target_y >= it->current_y + line_height
7755 && IT_CHARPOS (*it) < ZV)
7757 /* Should move forward by at least one line, maybe more.
7759 Note: Calling move_it_by_lines can be expensive on
7760 terminal frames, where compute_motion is used (via
7761 vmotion) to do the job, when there are very long lines
7762 and truncate-lines is nil. That's the reason for
7763 treating terminal frames specially here. */
7765 if (!FRAME_WINDOW_P (it->f))
7766 move_it_vertically (it, target_y - (it->current_y + line_height));
7767 else
7771 move_it_by_lines (it, 1, 1);
7773 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7780 /* Move IT by a specified amount of pixel lines DY. DY negative means
7781 move backwards. DY = 0 means move to start of screen line. At the
7782 end, IT will be on the start of a screen line. */
7784 void
7785 move_it_vertically (struct it *it, int dy)
7787 if (dy <= 0)
7788 move_it_vertically_backward (it, -dy);
7789 else
7791 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7792 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7793 MOVE_TO_POS | MOVE_TO_Y);
7794 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7796 /* If buffer ends in ZV without a newline, move to the start of
7797 the line to satisfy the post-condition. */
7798 if (IT_CHARPOS (*it) == ZV
7799 && ZV > BEGV
7800 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7801 move_it_by_lines (it, 0, 0);
7806 /* Move iterator IT past the end of the text line it is in. */
7808 void
7809 move_it_past_eol (struct it *it)
7811 enum move_it_result rc;
7813 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7814 if (rc == MOVE_NEWLINE_OR_CR)
7815 set_iterator_to_next (it, 0);
7819 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7820 negative means move up. DVPOS == 0 means move to the start of the
7821 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7822 NEED_Y_P is zero, IT->current_y will be left unchanged.
7824 Further optimization ideas: If we would know that IT->f doesn't use
7825 a face with proportional font, we could be faster for
7826 truncate-lines nil. */
7828 void
7829 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7832 /* The commented-out optimization uses vmotion on terminals. This
7833 gives bad results, because elements like it->what, on which
7834 callers such as pos_visible_p rely, aren't updated. */
7835 /* struct position pos;
7836 if (!FRAME_WINDOW_P (it->f))
7838 struct text_pos textpos;
7840 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7841 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7842 reseat (it, textpos, 1);
7843 it->vpos += pos.vpos;
7844 it->current_y += pos.vpos;
7846 else */
7848 if (dvpos == 0)
7850 /* DVPOS == 0 means move to the start of the screen line. */
7851 move_it_vertically_backward (it, 0);
7852 xassert (it->current_x == 0 && it->hpos == 0);
7853 /* Let next call to line_bottom_y calculate real line height */
7854 last_height = 0;
7856 else if (dvpos > 0)
7858 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7859 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7860 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7862 else
7864 struct it it2;
7865 EMACS_INT start_charpos, i;
7867 /* Start at the beginning of the screen line containing IT's
7868 position. This may actually move vertically backwards,
7869 in case of overlays, so adjust dvpos accordingly. */
7870 dvpos += it->vpos;
7871 move_it_vertically_backward (it, 0);
7872 dvpos -= it->vpos;
7874 /* Go back -DVPOS visible lines and reseat the iterator there. */
7875 start_charpos = IT_CHARPOS (*it);
7876 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7877 back_to_previous_visible_line_start (it);
7878 reseat (it, it->current.pos, 1);
7880 /* Move further back if we end up in a string or an image. */
7881 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7883 /* First try to move to start of display line. */
7884 dvpos += it->vpos;
7885 move_it_vertically_backward (it, 0);
7886 dvpos -= it->vpos;
7887 if (IT_POS_VALID_AFTER_MOVE_P (it))
7888 break;
7889 /* If start of line is still in string or image,
7890 move further back. */
7891 back_to_previous_visible_line_start (it);
7892 reseat (it, it->current.pos, 1);
7893 dvpos--;
7896 it->current_x = it->hpos = 0;
7898 /* Above call may have moved too far if continuation lines
7899 are involved. Scan forward and see if it did. */
7900 it2 = *it;
7901 it2.vpos = it2.current_y = 0;
7902 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7903 it->vpos -= it2.vpos;
7904 it->current_y -= it2.current_y;
7905 it->current_x = it->hpos = 0;
7907 /* If we moved too far back, move IT some lines forward. */
7908 if (it2.vpos > -dvpos)
7910 int delta = it2.vpos + dvpos;
7911 it2 = *it;
7912 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7913 /* Move back again if we got too far ahead. */
7914 if (IT_CHARPOS (*it) >= start_charpos)
7915 *it = it2;
7920 /* Return 1 if IT points into the middle of a display vector. */
7923 in_display_vector_p (struct it *it)
7925 return (it->method == GET_FROM_DISPLAY_VECTOR
7926 && it->current.dpvec_index > 0
7927 && it->dpvec + it->current.dpvec_index != it->dpend);
7931 /***********************************************************************
7932 Messages
7933 ***********************************************************************/
7936 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7937 to *Messages*. */
7939 void
7940 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
7942 Lisp_Object args[3];
7943 Lisp_Object msg, fmt;
7944 char *buffer;
7945 EMACS_INT len;
7946 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7947 USE_SAFE_ALLOCA;
7949 /* Do nothing if called asynchronously. Inserting text into
7950 a buffer may call after-change-functions and alike and
7951 that would means running Lisp asynchronously. */
7952 if (handling_signal)
7953 return;
7955 fmt = msg = Qnil;
7956 GCPRO4 (fmt, msg, arg1, arg2);
7958 args[0] = fmt = build_string (format);
7959 args[1] = arg1;
7960 args[2] = arg2;
7961 msg = Fformat (3, args);
7963 len = SBYTES (msg) + 1;
7964 SAFE_ALLOCA (buffer, char *, len);
7965 memcpy (buffer, SDATA (msg), len);
7967 message_dolog (buffer, len - 1, 1, 0);
7968 SAFE_FREE ();
7970 UNGCPRO;
7974 /* Output a newline in the *Messages* buffer if "needs" one. */
7976 void
7977 message_log_maybe_newline (void)
7979 if (message_log_need_newline)
7980 message_dolog ("", 0, 1, 0);
7984 /* Add a string M of length NBYTES to the message log, optionally
7985 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7986 nonzero, means interpret the contents of M as multibyte. This
7987 function calls low-level routines in order to bypass text property
7988 hooks, etc. which might not be safe to run.
7990 This may GC (insert may run before/after change hooks),
7991 so the buffer M must NOT point to a Lisp string. */
7993 void
7994 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
7996 if (!NILP (Vmemory_full))
7997 return;
7999 if (!NILP (Vmessage_log_max))
8001 struct buffer *oldbuf;
8002 Lisp_Object oldpoint, oldbegv, oldzv;
8003 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8004 EMACS_INT point_at_end = 0;
8005 EMACS_INT zv_at_end = 0;
8006 Lisp_Object old_deactivate_mark, tem;
8007 struct gcpro gcpro1;
8009 old_deactivate_mark = Vdeactivate_mark;
8010 oldbuf = current_buffer;
8011 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8012 current_buffer->undo_list = Qt;
8014 oldpoint = message_dolog_marker1;
8015 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8016 oldbegv = message_dolog_marker2;
8017 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8018 oldzv = message_dolog_marker3;
8019 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8020 GCPRO1 (old_deactivate_mark);
8022 if (PT == Z)
8023 point_at_end = 1;
8024 if (ZV == Z)
8025 zv_at_end = 1;
8027 BEGV = BEG;
8028 BEGV_BYTE = BEG_BYTE;
8029 ZV = Z;
8030 ZV_BYTE = Z_BYTE;
8031 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8033 /* Insert the string--maybe converting multibyte to single byte
8034 or vice versa, so that all the text fits the buffer. */
8035 if (multibyte
8036 && NILP (current_buffer->enable_multibyte_characters))
8038 EMACS_INT i;
8039 int c, char_bytes;
8040 unsigned char work[1];
8042 /* Convert a multibyte string to single-byte
8043 for the *Message* buffer. */
8044 for (i = 0; i < nbytes; i += char_bytes)
8046 c = string_char_and_length (m + i, &char_bytes);
8047 work[0] = (ASCII_CHAR_P (c)
8049 : multibyte_char_to_unibyte (c, Qnil));
8050 insert_1_both (work, 1, 1, 1, 0, 0);
8053 else if (! multibyte
8054 && ! NILP (current_buffer->enable_multibyte_characters))
8056 EMACS_INT i;
8057 int c, char_bytes;
8058 unsigned char *msg = (unsigned char *) m;
8059 unsigned char str[MAX_MULTIBYTE_LENGTH];
8060 /* Convert a single-byte string to multibyte
8061 for the *Message* buffer. */
8062 for (i = 0; i < nbytes; i++)
8064 c = msg[i];
8065 MAKE_CHAR_MULTIBYTE (c);
8066 char_bytes = CHAR_STRING (c, str);
8067 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8070 else if (nbytes)
8071 insert_1 (m, nbytes, 1, 0, 0);
8073 if (nlflag)
8075 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8076 int dup;
8077 insert_1 ("\n", 1, 1, 0, 0);
8079 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8080 this_bol = PT;
8081 this_bol_byte = PT_BYTE;
8083 /* See if this line duplicates the previous one.
8084 If so, combine duplicates. */
8085 if (this_bol > BEG)
8087 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8088 prev_bol = PT;
8089 prev_bol_byte = PT_BYTE;
8091 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8092 this_bol, this_bol_byte);
8093 if (dup)
8095 del_range_both (prev_bol, prev_bol_byte,
8096 this_bol, this_bol_byte, 0);
8097 if (dup > 1)
8099 char dupstr[40];
8100 int duplen;
8102 /* If you change this format, don't forget to also
8103 change message_log_check_duplicate. */
8104 sprintf (dupstr, " [%d times]", dup);
8105 duplen = strlen (dupstr);
8106 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8107 insert_1 (dupstr, duplen, 1, 0, 1);
8112 /* If we have more than the desired maximum number of lines
8113 in the *Messages* buffer now, delete the oldest ones.
8114 This is safe because we don't have undo in this buffer. */
8116 if (NATNUMP (Vmessage_log_max))
8118 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8119 -XFASTINT (Vmessage_log_max) - 1, 0);
8120 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8123 BEGV = XMARKER (oldbegv)->charpos;
8124 BEGV_BYTE = marker_byte_position (oldbegv);
8126 if (zv_at_end)
8128 ZV = Z;
8129 ZV_BYTE = Z_BYTE;
8131 else
8133 ZV = XMARKER (oldzv)->charpos;
8134 ZV_BYTE = marker_byte_position (oldzv);
8137 if (point_at_end)
8138 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8139 else
8140 /* We can't do Fgoto_char (oldpoint) because it will run some
8141 Lisp code. */
8142 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8143 XMARKER (oldpoint)->bytepos);
8145 UNGCPRO;
8146 unchain_marker (XMARKER (oldpoint));
8147 unchain_marker (XMARKER (oldbegv));
8148 unchain_marker (XMARKER (oldzv));
8150 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8151 set_buffer_internal (oldbuf);
8152 if (NILP (tem))
8153 windows_or_buffers_changed = old_windows_or_buffers_changed;
8154 message_log_need_newline = !nlflag;
8155 Vdeactivate_mark = old_deactivate_mark;
8160 /* We are at the end of the buffer after just having inserted a newline.
8161 (Note: We depend on the fact we won't be crossing the gap.)
8162 Check to see if the most recent message looks a lot like the previous one.
8163 Return 0 if different, 1 if the new one should just replace it, or a
8164 value N > 1 if we should also append " [N times]". */
8166 static int
8167 message_log_check_duplicate (EMACS_INT prev_bol, EMACS_INT prev_bol_byte,
8168 EMACS_INT this_bol, EMACS_INT this_bol_byte)
8170 EMACS_INT i;
8171 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8172 int seen_dots = 0;
8173 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8174 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8176 for (i = 0; i < len; i++)
8178 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8179 seen_dots = 1;
8180 if (p1[i] != p2[i])
8181 return seen_dots;
8183 p1 += len;
8184 if (*p1 == '\n')
8185 return 2;
8186 if (*p1++ == ' ' && *p1++ == '[')
8188 int n = 0;
8189 while (*p1 >= '0' && *p1 <= '9')
8190 n = n * 10 + *p1++ - '0';
8191 if (strncmp (p1, " times]\n", 8) == 0)
8192 return n+1;
8194 return 0;
8198 /* Display an echo area message M with a specified length of NBYTES
8199 bytes. The string may include null characters. If M is 0, clear
8200 out any existing message, and let the mini-buffer text show
8201 through.
8203 This may GC, so the buffer M must NOT point to a Lisp string. */
8205 void
8206 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8208 /* First flush out any partial line written with print. */
8209 message_log_maybe_newline ();
8210 if (m)
8211 message_dolog (m, nbytes, 1, multibyte);
8212 message2_nolog (m, nbytes, multibyte);
8216 /* The non-logging counterpart of message2. */
8218 void
8219 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8221 struct frame *sf = SELECTED_FRAME ();
8222 message_enable_multibyte = multibyte;
8224 if (FRAME_INITIAL_P (sf))
8226 if (noninteractive_need_newline)
8227 putc ('\n', stderr);
8228 noninteractive_need_newline = 0;
8229 if (m)
8230 fwrite (m, nbytes, 1, stderr);
8231 if (cursor_in_echo_area == 0)
8232 fprintf (stderr, "\n");
8233 fflush (stderr);
8235 /* A null message buffer means that the frame hasn't really been
8236 initialized yet. Error messages get reported properly by
8237 cmd_error, so this must be just an informative message; toss it. */
8238 else if (INTERACTIVE
8239 && sf->glyphs_initialized_p
8240 && FRAME_MESSAGE_BUF (sf))
8242 Lisp_Object mini_window;
8243 struct frame *f;
8245 /* Get the frame containing the mini-buffer
8246 that the selected frame is using. */
8247 mini_window = FRAME_MINIBUF_WINDOW (sf);
8248 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8250 FRAME_SAMPLE_VISIBILITY (f);
8251 if (FRAME_VISIBLE_P (sf)
8252 && ! FRAME_VISIBLE_P (f))
8253 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8255 if (m)
8257 set_message (m, Qnil, nbytes, multibyte);
8258 if (minibuffer_auto_raise)
8259 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8261 else
8262 clear_message (1, 1);
8264 do_pending_window_change (0);
8265 echo_area_display (1);
8266 do_pending_window_change (0);
8267 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8268 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8273 /* Display an echo area message M with a specified length of NBYTES
8274 bytes. The string may include null characters. If M is not a
8275 string, clear out any existing message, and let the mini-buffer
8276 text show through.
8278 This function cancels echoing. */
8280 void
8281 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8283 struct gcpro gcpro1;
8285 GCPRO1 (m);
8286 clear_message (1,1);
8287 cancel_echoing ();
8289 /* First flush out any partial line written with print. */
8290 message_log_maybe_newline ();
8291 if (STRINGP (m))
8293 char *buffer;
8294 USE_SAFE_ALLOCA;
8296 SAFE_ALLOCA (buffer, char *, nbytes);
8297 memcpy (buffer, SDATA (m), nbytes);
8298 message_dolog (buffer, nbytes, 1, multibyte);
8299 SAFE_FREE ();
8301 message3_nolog (m, nbytes, multibyte);
8303 UNGCPRO;
8307 /* The non-logging version of message3.
8308 This does not cancel echoing, because it is used for echoing.
8309 Perhaps we need to make a separate function for echoing
8310 and make this cancel echoing. */
8312 void
8313 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8315 struct frame *sf = SELECTED_FRAME ();
8316 message_enable_multibyte = multibyte;
8318 if (FRAME_INITIAL_P (sf))
8320 if (noninteractive_need_newline)
8321 putc ('\n', stderr);
8322 noninteractive_need_newline = 0;
8323 if (STRINGP (m))
8324 fwrite (SDATA (m), nbytes, 1, stderr);
8325 if (cursor_in_echo_area == 0)
8326 fprintf (stderr, "\n");
8327 fflush (stderr);
8329 /* A null message buffer means that the frame hasn't really been
8330 initialized yet. Error messages get reported properly by
8331 cmd_error, so this must be just an informative message; toss it. */
8332 else if (INTERACTIVE
8333 && sf->glyphs_initialized_p
8334 && FRAME_MESSAGE_BUF (sf))
8336 Lisp_Object mini_window;
8337 Lisp_Object frame;
8338 struct frame *f;
8340 /* Get the frame containing the mini-buffer
8341 that the selected frame is using. */
8342 mini_window = FRAME_MINIBUF_WINDOW (sf);
8343 frame = XWINDOW (mini_window)->frame;
8344 f = XFRAME (frame);
8346 FRAME_SAMPLE_VISIBILITY (f);
8347 if (FRAME_VISIBLE_P (sf)
8348 && !FRAME_VISIBLE_P (f))
8349 Fmake_frame_visible (frame);
8351 if (STRINGP (m) && SCHARS (m) > 0)
8353 set_message (NULL, m, nbytes, multibyte);
8354 if (minibuffer_auto_raise)
8355 Fraise_frame (frame);
8356 /* Assume we are not echoing.
8357 (If we are, echo_now will override this.) */
8358 echo_message_buffer = Qnil;
8360 else
8361 clear_message (1, 1);
8363 do_pending_window_change (0);
8364 echo_area_display (1);
8365 do_pending_window_change (0);
8366 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8367 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8372 /* Display a null-terminated echo area message M. If M is 0, clear
8373 out any existing message, and let the mini-buffer text show through.
8375 The buffer M must continue to exist until after the echo area gets
8376 cleared or some other message gets displayed there. Do not pass
8377 text that is stored in a Lisp string. Do not pass text in a buffer
8378 that was alloca'd. */
8380 void
8381 message1 (const char *m)
8383 message2 (m, (m ? strlen (m) : 0), 0);
8387 /* The non-logging counterpart of message1. */
8389 void
8390 message1_nolog (const char *m)
8392 message2_nolog (m, (m ? strlen (m) : 0), 0);
8395 /* Display a message M which contains a single %s
8396 which gets replaced with STRING. */
8398 void
8399 message_with_string (const char *m, Lisp_Object string, int log)
8401 CHECK_STRING (string);
8403 if (noninteractive)
8405 if (m)
8407 if (noninteractive_need_newline)
8408 putc ('\n', stderr);
8409 noninteractive_need_newline = 0;
8410 fprintf (stderr, m, SDATA (string));
8411 if (!cursor_in_echo_area)
8412 fprintf (stderr, "\n");
8413 fflush (stderr);
8416 else if (INTERACTIVE)
8418 /* The frame whose minibuffer we're going to display the message on.
8419 It may be larger than the selected frame, so we need
8420 to use its buffer, not the selected frame's buffer. */
8421 Lisp_Object mini_window;
8422 struct frame *f, *sf = SELECTED_FRAME ();
8424 /* Get the frame containing the minibuffer
8425 that the selected frame is using. */
8426 mini_window = FRAME_MINIBUF_WINDOW (sf);
8427 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8429 /* A null message buffer means that the frame hasn't really been
8430 initialized yet. Error messages get reported properly by
8431 cmd_error, so this must be just an informative message; toss it. */
8432 if (FRAME_MESSAGE_BUF (f))
8434 Lisp_Object args[2], message;
8435 struct gcpro gcpro1, gcpro2;
8437 args[0] = build_string (m);
8438 args[1] = message = string;
8439 GCPRO2 (args[0], message);
8440 gcpro1.nvars = 2;
8442 message = Fformat (2, args);
8444 if (log)
8445 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8446 else
8447 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8449 UNGCPRO;
8451 /* Print should start at the beginning of the message
8452 buffer next time. */
8453 message_buf_print = 0;
8459 /* Dump an informative message to the minibuf. If M is 0, clear out
8460 any existing message, and let the mini-buffer text show through. */
8462 static void
8463 vmessage (const char *m, va_list ap)
8465 if (noninteractive)
8467 if (m)
8469 if (noninteractive_need_newline)
8470 putc ('\n', stderr);
8471 noninteractive_need_newline = 0;
8472 vfprintf (stderr, m, ap);
8473 if (cursor_in_echo_area == 0)
8474 fprintf (stderr, "\n");
8475 fflush (stderr);
8478 else if (INTERACTIVE)
8480 /* The frame whose mini-buffer we're going to display the message
8481 on. It may be larger than the selected frame, so we need to
8482 use its buffer, not the selected frame's buffer. */
8483 Lisp_Object mini_window;
8484 struct frame *f, *sf = SELECTED_FRAME ();
8486 /* Get the frame containing the mini-buffer
8487 that the selected frame is using. */
8488 mini_window = FRAME_MINIBUF_WINDOW (sf);
8489 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8491 /* A null message buffer means that the frame hasn't really been
8492 initialized yet. Error messages get reported properly by
8493 cmd_error, so this must be just an informative message; toss
8494 it. */
8495 if (FRAME_MESSAGE_BUF (f))
8497 if (m)
8499 EMACS_INT len;
8501 len = doprnt (FRAME_MESSAGE_BUF (f),
8502 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8504 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8506 else
8507 message1 (0);
8509 /* Print should start at the beginning of the message
8510 buffer next time. */
8511 message_buf_print = 0;
8516 void
8517 message (const char *m, ...)
8519 va_list ap;
8520 va_start (ap, m);
8521 vmessage (m, ap);
8522 va_end (ap);
8526 /* The non-logging version of message. */
8528 void
8529 message_nolog (const char *m, ...)
8531 Lisp_Object old_log_max;
8532 va_list ap;
8533 va_start (ap, m);
8534 old_log_max = Vmessage_log_max;
8535 Vmessage_log_max = Qnil;
8536 vmessage (m, ap);
8537 Vmessage_log_max = old_log_max;
8538 va_end (ap);
8542 /* Display the current message in the current mini-buffer. This is
8543 only called from error handlers in process.c, and is not time
8544 critical. */
8546 void
8547 update_echo_area (void)
8549 if (!NILP (echo_area_buffer[0]))
8551 Lisp_Object string;
8552 string = Fcurrent_message ();
8553 message3 (string, SBYTES (string),
8554 !NILP (current_buffer->enable_multibyte_characters));
8559 /* Make sure echo area buffers in `echo_buffers' are live.
8560 If they aren't, make new ones. */
8562 static void
8563 ensure_echo_area_buffers (void)
8565 int i;
8567 for (i = 0; i < 2; ++i)
8568 if (!BUFFERP (echo_buffer[i])
8569 || NILP (XBUFFER (echo_buffer[i])->name))
8571 char name[30];
8572 Lisp_Object old_buffer;
8573 int j;
8575 old_buffer = echo_buffer[i];
8576 sprintf (name, " *Echo Area %d*", i);
8577 echo_buffer[i] = Fget_buffer_create (build_string (name));
8578 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8579 /* to force word wrap in echo area -
8580 it was decided to postpone this*/
8581 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8583 for (j = 0; j < 2; ++j)
8584 if (EQ (old_buffer, echo_area_buffer[j]))
8585 echo_area_buffer[j] = echo_buffer[i];
8590 /* Call FN with args A1..A4 with either the current or last displayed
8591 echo_area_buffer as current buffer.
8593 WHICH zero means use the current message buffer
8594 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8595 from echo_buffer[] and clear it.
8597 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8598 suitable buffer from echo_buffer[] and clear it.
8600 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8601 that the current message becomes the last displayed one, make
8602 choose a suitable buffer for echo_area_buffer[0], and clear it.
8604 Value is what FN returns. */
8606 static int
8607 with_echo_area_buffer (struct window *w, int which,
8608 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8609 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8611 Lisp_Object buffer;
8612 int this_one, the_other, clear_buffer_p, rc;
8613 int count = SPECPDL_INDEX ();
8615 /* If buffers aren't live, make new ones. */
8616 ensure_echo_area_buffers ();
8618 clear_buffer_p = 0;
8620 if (which == 0)
8621 this_one = 0, the_other = 1;
8622 else if (which > 0)
8623 this_one = 1, the_other = 0;
8624 else
8626 this_one = 0, the_other = 1;
8627 clear_buffer_p = 1;
8629 /* We need a fresh one in case the current echo buffer equals
8630 the one containing the last displayed echo area message. */
8631 if (!NILP (echo_area_buffer[this_one])
8632 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8633 echo_area_buffer[this_one] = Qnil;
8636 /* Choose a suitable buffer from echo_buffer[] is we don't
8637 have one. */
8638 if (NILP (echo_area_buffer[this_one]))
8640 echo_area_buffer[this_one]
8641 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8642 ? echo_buffer[the_other]
8643 : echo_buffer[this_one]);
8644 clear_buffer_p = 1;
8647 buffer = echo_area_buffer[this_one];
8649 /* Don't get confused by reusing the buffer used for echoing
8650 for a different purpose. */
8651 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8652 cancel_echoing ();
8654 record_unwind_protect (unwind_with_echo_area_buffer,
8655 with_echo_area_buffer_unwind_data (w));
8657 /* Make the echo area buffer current. Note that for display
8658 purposes, it is not necessary that the displayed window's buffer
8659 == current_buffer, except for text property lookup. So, let's
8660 only set that buffer temporarily here without doing a full
8661 Fset_window_buffer. We must also change w->pointm, though,
8662 because otherwise an assertions in unshow_buffer fails, and Emacs
8663 aborts. */
8664 set_buffer_internal_1 (XBUFFER (buffer));
8665 if (w)
8667 w->buffer = buffer;
8668 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8671 current_buffer->undo_list = Qt;
8672 current_buffer->read_only = Qnil;
8673 specbind (Qinhibit_read_only, Qt);
8674 specbind (Qinhibit_modification_hooks, Qt);
8676 if (clear_buffer_p && Z > BEG)
8677 del_range (BEG, Z);
8679 xassert (BEGV >= BEG);
8680 xassert (ZV <= Z && ZV >= BEGV);
8682 rc = fn (a1, a2, a3, a4);
8684 xassert (BEGV >= BEG);
8685 xassert (ZV <= Z && ZV >= BEGV);
8687 unbind_to (count, Qnil);
8688 return rc;
8692 /* Save state that should be preserved around the call to the function
8693 FN called in with_echo_area_buffer. */
8695 static Lisp_Object
8696 with_echo_area_buffer_unwind_data (struct window *w)
8698 int i = 0;
8699 Lisp_Object vector, tmp;
8701 /* Reduce consing by keeping one vector in
8702 Vwith_echo_area_save_vector. */
8703 vector = Vwith_echo_area_save_vector;
8704 Vwith_echo_area_save_vector = Qnil;
8706 if (NILP (vector))
8707 vector = Fmake_vector (make_number (7), Qnil);
8709 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8710 ASET (vector, i, Vdeactivate_mark); ++i;
8711 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8713 if (w)
8715 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8716 ASET (vector, i, w->buffer); ++i;
8717 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8718 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8720 else
8722 int end = i + 4;
8723 for (; i < end; ++i)
8724 ASET (vector, i, Qnil);
8727 xassert (i == ASIZE (vector));
8728 return vector;
8732 /* Restore global state from VECTOR which was created by
8733 with_echo_area_buffer_unwind_data. */
8735 static Lisp_Object
8736 unwind_with_echo_area_buffer (Lisp_Object vector)
8738 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8739 Vdeactivate_mark = AREF (vector, 1);
8740 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8742 if (WINDOWP (AREF (vector, 3)))
8744 struct window *w;
8745 Lisp_Object buffer, charpos, bytepos;
8747 w = XWINDOW (AREF (vector, 3));
8748 buffer = AREF (vector, 4);
8749 charpos = AREF (vector, 5);
8750 bytepos = AREF (vector, 6);
8752 w->buffer = buffer;
8753 set_marker_both (w->pointm, buffer,
8754 XFASTINT (charpos), XFASTINT (bytepos));
8757 Vwith_echo_area_save_vector = vector;
8758 return Qnil;
8762 /* Set up the echo area for use by print functions. MULTIBYTE_P
8763 non-zero means we will print multibyte. */
8765 void
8766 setup_echo_area_for_printing (int multibyte_p)
8768 /* If we can't find an echo area any more, exit. */
8769 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8770 Fkill_emacs (Qnil);
8772 ensure_echo_area_buffers ();
8774 if (!message_buf_print)
8776 /* A message has been output since the last time we printed.
8777 Choose a fresh echo area buffer. */
8778 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8779 echo_area_buffer[0] = echo_buffer[1];
8780 else
8781 echo_area_buffer[0] = echo_buffer[0];
8783 /* Switch to that buffer and clear it. */
8784 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8785 current_buffer->truncate_lines = Qnil;
8787 if (Z > BEG)
8789 int count = SPECPDL_INDEX ();
8790 specbind (Qinhibit_read_only, Qt);
8791 /* Note that undo recording is always disabled. */
8792 del_range (BEG, Z);
8793 unbind_to (count, Qnil);
8795 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8797 /* Set up the buffer for the multibyteness we need. */
8798 if (multibyte_p
8799 != !NILP (current_buffer->enable_multibyte_characters))
8800 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8802 /* Raise the frame containing the echo area. */
8803 if (minibuffer_auto_raise)
8805 struct frame *sf = SELECTED_FRAME ();
8806 Lisp_Object mini_window;
8807 mini_window = FRAME_MINIBUF_WINDOW (sf);
8808 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8811 message_log_maybe_newline ();
8812 message_buf_print = 1;
8814 else
8816 if (NILP (echo_area_buffer[0]))
8818 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8819 echo_area_buffer[0] = echo_buffer[1];
8820 else
8821 echo_area_buffer[0] = echo_buffer[0];
8824 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8826 /* Someone switched buffers between print requests. */
8827 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8828 current_buffer->truncate_lines = Qnil;
8834 /* Display an echo area message in window W. Value is non-zero if W's
8835 height is changed. If display_last_displayed_message_p is
8836 non-zero, display the message that was last displayed, otherwise
8837 display the current message. */
8839 static int
8840 display_echo_area (struct window *w)
8842 int i, no_message_p, window_height_changed_p, count;
8844 /* Temporarily disable garbage collections while displaying the echo
8845 area. This is done because a GC can print a message itself.
8846 That message would modify the echo area buffer's contents while a
8847 redisplay of the buffer is going on, and seriously confuse
8848 redisplay. */
8849 count = inhibit_garbage_collection ();
8851 /* If there is no message, we must call display_echo_area_1
8852 nevertheless because it resizes the window. But we will have to
8853 reset the echo_area_buffer in question to nil at the end because
8854 with_echo_area_buffer will sets it to an empty buffer. */
8855 i = display_last_displayed_message_p ? 1 : 0;
8856 no_message_p = NILP (echo_area_buffer[i]);
8858 window_height_changed_p
8859 = with_echo_area_buffer (w, display_last_displayed_message_p,
8860 display_echo_area_1,
8861 (EMACS_INT) w, Qnil, 0, 0);
8863 if (no_message_p)
8864 echo_area_buffer[i] = Qnil;
8866 unbind_to (count, Qnil);
8867 return window_height_changed_p;
8871 /* Helper for display_echo_area. Display the current buffer which
8872 contains the current echo area message in window W, a mini-window,
8873 a pointer to which is passed in A1. A2..A4 are currently not used.
8874 Change the height of W so that all of the message is displayed.
8875 Value is non-zero if height of W was changed. */
8877 static int
8878 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8880 struct window *w = (struct window *) a1;
8881 Lisp_Object window;
8882 struct text_pos start;
8883 int window_height_changed_p = 0;
8885 /* Do this before displaying, so that we have a large enough glyph
8886 matrix for the display. If we can't get enough space for the
8887 whole text, display the last N lines. That works by setting w->start. */
8888 window_height_changed_p = resize_mini_window (w, 0);
8890 /* Use the starting position chosen by resize_mini_window. */
8891 SET_TEXT_POS_FROM_MARKER (start, w->start);
8893 /* Display. */
8894 clear_glyph_matrix (w->desired_matrix);
8895 XSETWINDOW (window, w);
8896 try_window (window, start, 0);
8898 return window_height_changed_p;
8902 /* Resize the echo area window to exactly the size needed for the
8903 currently displayed message, if there is one. If a mini-buffer
8904 is active, don't shrink it. */
8906 void
8907 resize_echo_area_exactly (void)
8909 if (BUFFERP (echo_area_buffer[0])
8910 && WINDOWP (echo_area_window))
8912 struct window *w = XWINDOW (echo_area_window);
8913 int resized_p;
8914 Lisp_Object resize_exactly;
8916 if (minibuf_level == 0)
8917 resize_exactly = Qt;
8918 else
8919 resize_exactly = Qnil;
8921 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8922 (EMACS_INT) w, resize_exactly, 0, 0);
8923 if (resized_p)
8925 ++windows_or_buffers_changed;
8926 ++update_mode_lines;
8927 redisplay_internal (0);
8933 /* Callback function for with_echo_area_buffer, when used from
8934 resize_echo_area_exactly. A1 contains a pointer to the window to
8935 resize, EXACTLY non-nil means resize the mini-window exactly to the
8936 size of the text displayed. A3 and A4 are not used. Value is what
8937 resize_mini_window returns. */
8939 static int
8940 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
8942 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8946 /* Resize mini-window W to fit the size of its contents. EXACT_P
8947 means size the window exactly to the size needed. Otherwise, it's
8948 only enlarged until W's buffer is empty.
8950 Set W->start to the right place to begin display. If the whole
8951 contents fit, start at the beginning. Otherwise, start so as
8952 to make the end of the contents appear. This is particularly
8953 important for y-or-n-p, but seems desirable generally.
8955 Value is non-zero if the window height has been changed. */
8958 resize_mini_window (struct window *w, int exact_p)
8960 struct frame *f = XFRAME (w->frame);
8961 int window_height_changed_p = 0;
8963 xassert (MINI_WINDOW_P (w));
8965 /* By default, start display at the beginning. */
8966 set_marker_both (w->start, w->buffer,
8967 BUF_BEGV (XBUFFER (w->buffer)),
8968 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8970 /* Don't resize windows while redisplaying a window; it would
8971 confuse redisplay functions when the size of the window they are
8972 displaying changes from under them. Such a resizing can happen,
8973 for instance, when which-func prints a long message while
8974 we are running fontification-functions. We're running these
8975 functions with safe_call which binds inhibit-redisplay to t. */
8976 if (!NILP (Vinhibit_redisplay))
8977 return 0;
8979 /* Nil means don't try to resize. */
8980 if (NILP (Vresize_mini_windows)
8981 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8982 return 0;
8984 if (!FRAME_MINIBUF_ONLY_P (f))
8986 struct it it;
8987 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8988 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8989 int height, max_height;
8990 int unit = FRAME_LINE_HEIGHT (f);
8991 struct text_pos start;
8992 struct buffer *old_current_buffer = NULL;
8994 if (current_buffer != XBUFFER (w->buffer))
8996 old_current_buffer = current_buffer;
8997 set_buffer_internal (XBUFFER (w->buffer));
9000 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9002 /* Compute the max. number of lines specified by the user. */
9003 if (FLOATP (Vmax_mini_window_height))
9004 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9005 else if (INTEGERP (Vmax_mini_window_height))
9006 max_height = XINT (Vmax_mini_window_height);
9007 else
9008 max_height = total_height / 4;
9010 /* Correct that max. height if it's bogus. */
9011 max_height = max (1, max_height);
9012 max_height = min (total_height, max_height);
9014 /* Find out the height of the text in the window. */
9015 if (it.line_wrap == TRUNCATE)
9016 height = 1;
9017 else
9019 last_height = 0;
9020 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9021 if (it.max_ascent == 0 && it.max_descent == 0)
9022 height = it.current_y + last_height;
9023 else
9024 height = it.current_y + it.max_ascent + it.max_descent;
9025 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9026 height = (height + unit - 1) / unit;
9029 /* Compute a suitable window start. */
9030 if (height > max_height)
9032 height = max_height;
9033 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9034 move_it_vertically_backward (&it, (height - 1) * unit);
9035 start = it.current.pos;
9037 else
9038 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9039 SET_MARKER_FROM_TEXT_POS (w->start, start);
9041 if (EQ (Vresize_mini_windows, Qgrow_only))
9043 /* Let it grow only, until we display an empty message, in which
9044 case the window shrinks again. */
9045 if (height > WINDOW_TOTAL_LINES (w))
9047 int old_height = WINDOW_TOTAL_LINES (w);
9048 freeze_window_starts (f, 1);
9049 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9050 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9052 else if (height < WINDOW_TOTAL_LINES (w)
9053 && (exact_p || BEGV == ZV))
9055 int old_height = WINDOW_TOTAL_LINES (w);
9056 freeze_window_starts (f, 0);
9057 shrink_mini_window (w);
9058 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9061 else
9063 /* Always resize to exact size needed. */
9064 if (height > WINDOW_TOTAL_LINES (w))
9066 int old_height = WINDOW_TOTAL_LINES (w);
9067 freeze_window_starts (f, 1);
9068 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9069 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9071 else if (height < WINDOW_TOTAL_LINES (w))
9073 int old_height = WINDOW_TOTAL_LINES (w);
9074 freeze_window_starts (f, 0);
9075 shrink_mini_window (w);
9077 if (height)
9079 freeze_window_starts (f, 1);
9080 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9083 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9087 if (old_current_buffer)
9088 set_buffer_internal (old_current_buffer);
9091 return window_height_changed_p;
9095 /* Value is the current message, a string, or nil if there is no
9096 current message. */
9098 Lisp_Object
9099 current_message (void)
9101 Lisp_Object msg;
9103 if (!BUFFERP (echo_area_buffer[0]))
9104 msg = Qnil;
9105 else
9107 with_echo_area_buffer (0, 0, current_message_1,
9108 (EMACS_INT) &msg, Qnil, 0, 0);
9109 if (NILP (msg))
9110 echo_area_buffer[0] = Qnil;
9113 return msg;
9117 static int
9118 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9120 Lisp_Object *msg = (Lisp_Object *) a1;
9122 if (Z > BEG)
9123 *msg = make_buffer_string (BEG, Z, 1);
9124 else
9125 *msg = Qnil;
9126 return 0;
9130 /* Push the current message on Vmessage_stack for later restauration
9131 by restore_message. Value is non-zero if the current message isn't
9132 empty. This is a relatively infrequent operation, so it's not
9133 worth optimizing. */
9136 push_message (void)
9138 Lisp_Object msg;
9139 msg = current_message ();
9140 Vmessage_stack = Fcons (msg, Vmessage_stack);
9141 return STRINGP (msg);
9145 /* Restore message display from the top of Vmessage_stack. */
9147 void
9148 restore_message (void)
9150 Lisp_Object msg;
9152 xassert (CONSP (Vmessage_stack));
9153 msg = XCAR (Vmessage_stack);
9154 if (STRINGP (msg))
9155 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9156 else
9157 message3_nolog (msg, 0, 0);
9161 /* Handler for record_unwind_protect calling pop_message. */
9163 Lisp_Object
9164 pop_message_unwind (Lisp_Object dummy)
9166 pop_message ();
9167 return Qnil;
9170 /* Pop the top-most entry off Vmessage_stack. */
9172 void
9173 pop_message (void)
9175 xassert (CONSP (Vmessage_stack));
9176 Vmessage_stack = XCDR (Vmessage_stack);
9180 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9181 exits. If the stack is not empty, we have a missing pop_message
9182 somewhere. */
9184 void
9185 check_message_stack (void)
9187 if (!NILP (Vmessage_stack))
9188 abort ();
9192 /* Truncate to NCHARS what will be displayed in the echo area the next
9193 time we display it---but don't redisplay it now. */
9195 void
9196 truncate_echo_area (EMACS_INT nchars)
9198 if (nchars == 0)
9199 echo_area_buffer[0] = Qnil;
9200 /* A null message buffer means that the frame hasn't really been
9201 initialized yet. Error messages get reported properly by
9202 cmd_error, so this must be just an informative message; toss it. */
9203 else if (!noninteractive
9204 && INTERACTIVE
9205 && !NILP (echo_area_buffer[0]))
9207 struct frame *sf = SELECTED_FRAME ();
9208 if (FRAME_MESSAGE_BUF (sf))
9209 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9214 /* Helper function for truncate_echo_area. Truncate the current
9215 message to at most NCHARS characters. */
9217 static int
9218 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9220 if (BEG + nchars < Z)
9221 del_range (BEG + nchars, Z);
9222 if (Z == BEG)
9223 echo_area_buffer[0] = Qnil;
9224 return 0;
9228 /* Set the current message to a substring of S or STRING.
9230 If STRING is a Lisp string, set the message to the first NBYTES
9231 bytes from STRING. NBYTES zero means use the whole string. If
9232 STRING is multibyte, the message will be displayed multibyte.
9234 If S is not null, set the message to the first LEN bytes of S. LEN
9235 zero means use the whole string. MULTIBYTE_P non-zero means S is
9236 multibyte. Display the message multibyte in that case.
9238 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9239 to t before calling set_message_1 (which calls insert).
9242 void
9243 set_message (const char *s, Lisp_Object string,
9244 EMACS_INT nbytes, int multibyte_p)
9246 message_enable_multibyte
9247 = ((s && multibyte_p)
9248 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9250 with_echo_area_buffer (0, -1, set_message_1,
9251 (EMACS_INT) s, string, nbytes, multibyte_p);
9252 message_buf_print = 0;
9253 help_echo_showing_p = 0;
9257 /* Helper function for set_message. Arguments have the same meaning
9258 as there, with A1 corresponding to S and A2 corresponding to STRING
9259 This function is called with the echo area buffer being
9260 current. */
9262 static int
9263 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9265 const char *s = (const char *) a1;
9266 Lisp_Object string = a2;
9268 /* Change multibyteness of the echo buffer appropriately. */
9269 if (message_enable_multibyte
9270 != !NILP (current_buffer->enable_multibyte_characters))
9271 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9273 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9275 /* Insert new message at BEG. */
9276 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9278 if (STRINGP (string))
9280 EMACS_INT nchars;
9282 if (nbytes == 0)
9283 nbytes = SBYTES (string);
9284 nchars = string_byte_to_char (string, nbytes);
9286 /* This function takes care of single/multibyte conversion. We
9287 just have to ensure that the echo area buffer has the right
9288 setting of enable_multibyte_characters. */
9289 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9291 else if (s)
9293 if (nbytes == 0)
9294 nbytes = strlen (s);
9296 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9298 /* Convert from multi-byte to single-byte. */
9299 EMACS_INT i;
9300 int c, n;
9301 unsigned char work[1];
9303 /* Convert a multibyte string to single-byte. */
9304 for (i = 0; i < nbytes; i += n)
9306 c = string_char_and_length (s + i, &n);
9307 work[0] = (ASCII_CHAR_P (c)
9309 : multibyte_char_to_unibyte (c, Qnil));
9310 insert_1_both (work, 1, 1, 1, 0, 0);
9313 else if (!multibyte_p
9314 && !NILP (current_buffer->enable_multibyte_characters))
9316 /* Convert from single-byte to multi-byte. */
9317 EMACS_INT i;
9318 int c, n;
9319 const unsigned char *msg = (const unsigned char *) s;
9320 unsigned char str[MAX_MULTIBYTE_LENGTH];
9322 /* Convert a single-byte string to multibyte. */
9323 for (i = 0; i < nbytes; i++)
9325 c = msg[i];
9326 MAKE_CHAR_MULTIBYTE (c);
9327 n = CHAR_STRING (c, str);
9328 insert_1_both (str, 1, n, 1, 0, 0);
9331 else
9332 insert_1 (s, nbytes, 1, 0, 0);
9335 return 0;
9339 /* Clear messages. CURRENT_P non-zero means clear the current
9340 message. LAST_DISPLAYED_P non-zero means clear the message
9341 last displayed. */
9343 void
9344 clear_message (int current_p, int last_displayed_p)
9346 if (current_p)
9348 echo_area_buffer[0] = Qnil;
9349 message_cleared_p = 1;
9352 if (last_displayed_p)
9353 echo_area_buffer[1] = Qnil;
9355 message_buf_print = 0;
9358 /* Clear garbaged frames.
9360 This function is used where the old redisplay called
9361 redraw_garbaged_frames which in turn called redraw_frame which in
9362 turn called clear_frame. The call to clear_frame was a source of
9363 flickering. I believe a clear_frame is not necessary. It should
9364 suffice in the new redisplay to invalidate all current matrices,
9365 and ensure a complete redisplay of all windows. */
9367 static void
9368 clear_garbaged_frames (void)
9370 if (frame_garbaged)
9372 Lisp_Object tail, frame;
9373 int changed_count = 0;
9375 FOR_EACH_FRAME (tail, frame)
9377 struct frame *f = XFRAME (frame);
9379 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9381 if (f->resized_p)
9383 Fredraw_frame (frame);
9384 f->force_flush_display_p = 1;
9386 clear_current_matrices (f);
9387 changed_count++;
9388 f->garbaged = 0;
9389 f->resized_p = 0;
9393 frame_garbaged = 0;
9394 if (changed_count)
9395 ++windows_or_buffers_changed;
9400 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9401 is non-zero update selected_frame. Value is non-zero if the
9402 mini-windows height has been changed. */
9404 static int
9405 echo_area_display (int update_frame_p)
9407 Lisp_Object mini_window;
9408 struct window *w;
9409 struct frame *f;
9410 int window_height_changed_p = 0;
9411 struct frame *sf = SELECTED_FRAME ();
9413 mini_window = FRAME_MINIBUF_WINDOW (sf);
9414 w = XWINDOW (mini_window);
9415 f = XFRAME (WINDOW_FRAME (w));
9417 /* Don't display if frame is invisible or not yet initialized. */
9418 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9419 return 0;
9421 #ifdef HAVE_WINDOW_SYSTEM
9422 /* When Emacs starts, selected_frame may be the initial terminal
9423 frame. If we let this through, a message would be displayed on
9424 the terminal. */
9425 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9426 return 0;
9427 #endif /* HAVE_WINDOW_SYSTEM */
9429 /* Redraw garbaged frames. */
9430 if (frame_garbaged)
9431 clear_garbaged_frames ();
9433 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9435 echo_area_window = mini_window;
9436 window_height_changed_p = display_echo_area (w);
9437 w->must_be_updated_p = 1;
9439 /* Update the display, unless called from redisplay_internal.
9440 Also don't update the screen during redisplay itself. The
9441 update will happen at the end of redisplay, and an update
9442 here could cause confusion. */
9443 if (update_frame_p && !redisplaying_p)
9445 int n = 0;
9447 /* If the display update has been interrupted by pending
9448 input, update mode lines in the frame. Due to the
9449 pending input, it might have been that redisplay hasn't
9450 been called, so that mode lines above the echo area are
9451 garbaged. This looks odd, so we prevent it here. */
9452 if (!display_completed)
9453 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9455 if (window_height_changed_p
9456 /* Don't do this if Emacs is shutting down. Redisplay
9457 needs to run hooks. */
9458 && !NILP (Vrun_hooks))
9460 /* Must update other windows. Likewise as in other
9461 cases, don't let this update be interrupted by
9462 pending input. */
9463 int count = SPECPDL_INDEX ();
9464 specbind (Qredisplay_dont_pause, Qt);
9465 windows_or_buffers_changed = 1;
9466 redisplay_internal (0);
9467 unbind_to (count, Qnil);
9469 else if (FRAME_WINDOW_P (f) && n == 0)
9471 /* Window configuration is the same as before.
9472 Can do with a display update of the echo area,
9473 unless we displayed some mode lines. */
9474 update_single_window (w, 1);
9475 FRAME_RIF (f)->flush_display (f);
9477 else
9478 update_frame (f, 1, 1);
9480 /* If cursor is in the echo area, make sure that the next
9481 redisplay displays the minibuffer, so that the cursor will
9482 be replaced with what the minibuffer wants. */
9483 if (cursor_in_echo_area)
9484 ++windows_or_buffers_changed;
9487 else if (!EQ (mini_window, selected_window))
9488 windows_or_buffers_changed++;
9490 /* Last displayed message is now the current message. */
9491 echo_area_buffer[1] = echo_area_buffer[0];
9492 /* Inform read_char that we're not echoing. */
9493 echo_message_buffer = Qnil;
9495 /* Prevent redisplay optimization in redisplay_internal by resetting
9496 this_line_start_pos. This is done because the mini-buffer now
9497 displays the message instead of its buffer text. */
9498 if (EQ (mini_window, selected_window))
9499 CHARPOS (this_line_start_pos) = 0;
9501 return window_height_changed_p;
9506 /***********************************************************************
9507 Mode Lines and Frame Titles
9508 ***********************************************************************/
9510 /* A buffer for constructing non-propertized mode-line strings and
9511 frame titles in it; allocated from the heap in init_xdisp and
9512 resized as needed in store_mode_line_noprop_char. */
9514 static char *mode_line_noprop_buf;
9516 /* The buffer's end, and a current output position in it. */
9518 static char *mode_line_noprop_buf_end;
9519 static char *mode_line_noprop_ptr;
9521 #define MODE_LINE_NOPROP_LEN(start) \
9522 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9524 static enum {
9525 MODE_LINE_DISPLAY = 0,
9526 MODE_LINE_TITLE,
9527 MODE_LINE_NOPROP,
9528 MODE_LINE_STRING
9529 } mode_line_target;
9531 /* Alist that caches the results of :propertize.
9532 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9533 static Lisp_Object mode_line_proptrans_alist;
9535 /* List of strings making up the mode-line. */
9536 static Lisp_Object mode_line_string_list;
9538 /* Base face property when building propertized mode line string. */
9539 static Lisp_Object mode_line_string_face;
9540 static Lisp_Object mode_line_string_face_prop;
9543 /* Unwind data for mode line strings */
9545 static Lisp_Object Vmode_line_unwind_vector;
9547 static Lisp_Object
9548 format_mode_line_unwind_data (struct buffer *obuf,
9549 Lisp_Object owin,
9550 int save_proptrans)
9552 Lisp_Object vector, tmp;
9554 /* Reduce consing by keeping one vector in
9555 Vwith_echo_area_save_vector. */
9556 vector = Vmode_line_unwind_vector;
9557 Vmode_line_unwind_vector = Qnil;
9559 if (NILP (vector))
9560 vector = Fmake_vector (make_number (8), Qnil);
9562 ASET (vector, 0, make_number (mode_line_target));
9563 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9564 ASET (vector, 2, mode_line_string_list);
9565 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9566 ASET (vector, 4, mode_line_string_face);
9567 ASET (vector, 5, mode_line_string_face_prop);
9569 if (obuf)
9570 XSETBUFFER (tmp, obuf);
9571 else
9572 tmp = Qnil;
9573 ASET (vector, 6, tmp);
9574 ASET (vector, 7, owin);
9576 return vector;
9579 static Lisp_Object
9580 unwind_format_mode_line (Lisp_Object vector)
9582 mode_line_target = XINT (AREF (vector, 0));
9583 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9584 mode_line_string_list = AREF (vector, 2);
9585 if (! EQ (AREF (vector, 3), Qt))
9586 mode_line_proptrans_alist = AREF (vector, 3);
9587 mode_line_string_face = AREF (vector, 4);
9588 mode_line_string_face_prop = AREF (vector, 5);
9590 if (!NILP (AREF (vector, 7)))
9591 /* Select window before buffer, since it may change the buffer. */
9592 Fselect_window (AREF (vector, 7), Qt);
9594 if (!NILP (AREF (vector, 6)))
9596 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9597 ASET (vector, 6, Qnil);
9600 Vmode_line_unwind_vector = vector;
9601 return Qnil;
9605 /* Store a single character C for the frame title in mode_line_noprop_buf.
9606 Re-allocate mode_line_noprop_buf if necessary. */
9608 static void
9609 store_mode_line_noprop_char (char c)
9611 /* If output position has reached the end of the allocated buffer,
9612 double the buffer's size. */
9613 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9615 int len = MODE_LINE_NOPROP_LEN (0);
9616 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9617 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9618 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9619 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9622 *mode_line_noprop_ptr++ = c;
9626 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9627 mode_line_noprop_ptr. STR is the string to store. Do not copy
9628 characters that yield more columns than PRECISION; PRECISION <= 0
9629 means copy the whole string. Pad with spaces until FIELD_WIDTH
9630 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9631 pad. Called from display_mode_element when it is used to build a
9632 frame title. */
9634 static int
9635 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9637 int n = 0;
9638 EMACS_INT dummy, nbytes;
9640 /* Copy at most PRECISION chars from STR. */
9641 nbytes = strlen (str);
9642 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9643 while (nbytes--)
9644 store_mode_line_noprop_char (*str++);
9646 /* Fill up with spaces until FIELD_WIDTH reached. */
9647 while (field_width > 0
9648 && n < field_width)
9650 store_mode_line_noprop_char (' ');
9651 ++n;
9654 return n;
9657 /***********************************************************************
9658 Frame Titles
9659 ***********************************************************************/
9661 #ifdef HAVE_WINDOW_SYSTEM
9663 /* Set the title of FRAME, if it has changed. The title format is
9664 Vicon_title_format if FRAME is iconified, otherwise it is
9665 frame_title_format. */
9667 static void
9668 x_consider_frame_title (Lisp_Object frame)
9670 struct frame *f = XFRAME (frame);
9672 if (FRAME_WINDOW_P (f)
9673 || FRAME_MINIBUF_ONLY_P (f)
9674 || f->explicit_name)
9676 /* Do we have more than one visible frame on this X display? */
9677 Lisp_Object tail;
9678 Lisp_Object fmt;
9679 int title_start;
9680 char *title;
9681 int len;
9682 struct it it;
9683 int count = SPECPDL_INDEX ();
9685 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9687 Lisp_Object other_frame = XCAR (tail);
9688 struct frame *tf = XFRAME (other_frame);
9690 if (tf != f
9691 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9692 && !FRAME_MINIBUF_ONLY_P (tf)
9693 && !EQ (other_frame, tip_frame)
9694 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9695 break;
9698 /* Set global variable indicating that multiple frames exist. */
9699 multiple_frames = CONSP (tail);
9701 /* Switch to the buffer of selected window of the frame. Set up
9702 mode_line_target so that display_mode_element will output into
9703 mode_line_noprop_buf; then display the title. */
9704 record_unwind_protect (unwind_format_mode_line,
9705 format_mode_line_unwind_data
9706 (current_buffer, selected_window, 0));
9708 Fselect_window (f->selected_window, Qt);
9709 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9710 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9712 mode_line_target = MODE_LINE_TITLE;
9713 title_start = MODE_LINE_NOPROP_LEN (0);
9714 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9715 NULL, DEFAULT_FACE_ID);
9716 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9717 len = MODE_LINE_NOPROP_LEN (title_start);
9718 title = mode_line_noprop_buf + title_start;
9719 unbind_to (count, Qnil);
9721 /* Set the title only if it's changed. This avoids consing in
9722 the common case where it hasn't. (If it turns out that we've
9723 already wasted too much time by walking through the list with
9724 display_mode_element, then we might need to optimize at a
9725 higher level than this.) */
9726 if (! STRINGP (f->name)
9727 || SBYTES (f->name) != len
9728 || memcmp (title, SDATA (f->name), len) != 0)
9729 x_implicitly_set_name (f, make_string (title, len), Qnil);
9733 #endif /* not HAVE_WINDOW_SYSTEM */
9738 /***********************************************************************
9739 Menu Bars
9740 ***********************************************************************/
9743 /* Prepare for redisplay by updating menu-bar item lists when
9744 appropriate. This can call eval. */
9746 void
9747 prepare_menu_bars (void)
9749 int all_windows;
9750 struct gcpro gcpro1, gcpro2;
9751 struct frame *f;
9752 Lisp_Object tooltip_frame;
9754 #ifdef HAVE_WINDOW_SYSTEM
9755 tooltip_frame = tip_frame;
9756 #else
9757 tooltip_frame = Qnil;
9758 #endif
9760 /* Update all frame titles based on their buffer names, etc. We do
9761 this before the menu bars so that the buffer-menu will show the
9762 up-to-date frame titles. */
9763 #ifdef HAVE_WINDOW_SYSTEM
9764 if (windows_or_buffers_changed || update_mode_lines)
9766 Lisp_Object tail, frame;
9768 FOR_EACH_FRAME (tail, frame)
9770 f = XFRAME (frame);
9771 if (!EQ (frame, tooltip_frame)
9772 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9773 x_consider_frame_title (frame);
9776 #endif /* HAVE_WINDOW_SYSTEM */
9778 /* Update the menu bar item lists, if appropriate. This has to be
9779 done before any actual redisplay or generation of display lines. */
9780 all_windows = (update_mode_lines
9781 || buffer_shared > 1
9782 || windows_or_buffers_changed);
9783 if (all_windows)
9785 Lisp_Object tail, frame;
9786 int count = SPECPDL_INDEX ();
9787 /* 1 means that update_menu_bar has run its hooks
9788 so any further calls to update_menu_bar shouldn't do so again. */
9789 int menu_bar_hooks_run = 0;
9791 record_unwind_save_match_data ();
9793 FOR_EACH_FRAME (tail, frame)
9795 f = XFRAME (frame);
9797 /* Ignore tooltip frame. */
9798 if (EQ (frame, tooltip_frame))
9799 continue;
9801 /* If a window on this frame changed size, report that to
9802 the user and clear the size-change flag. */
9803 if (FRAME_WINDOW_SIZES_CHANGED (f))
9805 Lisp_Object functions;
9807 /* Clear flag first in case we get an error below. */
9808 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9809 functions = Vwindow_size_change_functions;
9810 GCPRO2 (tail, functions);
9812 while (CONSP (functions))
9814 if (!EQ (XCAR (functions), Qt))
9815 call1 (XCAR (functions), frame);
9816 functions = XCDR (functions);
9818 UNGCPRO;
9821 GCPRO1 (tail);
9822 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9823 #ifdef HAVE_WINDOW_SYSTEM
9824 update_tool_bar (f, 0);
9825 #endif
9826 #ifdef HAVE_NS
9827 if (windows_or_buffers_changed
9828 && FRAME_NS_P (f))
9829 ns_set_doc_edited (f, Fbuffer_modified_p
9830 (XWINDOW (f->selected_window)->buffer));
9831 #endif
9832 UNGCPRO;
9835 unbind_to (count, Qnil);
9837 else
9839 struct frame *sf = SELECTED_FRAME ();
9840 update_menu_bar (sf, 1, 0);
9841 #ifdef HAVE_WINDOW_SYSTEM
9842 update_tool_bar (sf, 1);
9843 #endif
9848 /* Update the menu bar item list for frame F. This has to be done
9849 before we start to fill in any display lines, because it can call
9850 eval.
9852 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9854 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9855 already ran the menu bar hooks for this redisplay, so there
9856 is no need to run them again. The return value is the
9857 updated value of this flag, to pass to the next call. */
9859 static int
9860 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9862 Lisp_Object window;
9863 register struct window *w;
9865 /* If called recursively during a menu update, do nothing. This can
9866 happen when, for instance, an activate-menubar-hook causes a
9867 redisplay. */
9868 if (inhibit_menubar_update)
9869 return hooks_run;
9871 window = FRAME_SELECTED_WINDOW (f);
9872 w = XWINDOW (window);
9874 if (FRAME_WINDOW_P (f)
9876 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9877 || defined (HAVE_NS) || defined (USE_GTK)
9878 FRAME_EXTERNAL_MENU_BAR (f)
9879 #else
9880 FRAME_MENU_BAR_LINES (f) > 0
9881 #endif
9882 : FRAME_MENU_BAR_LINES (f) > 0)
9884 /* If the user has switched buffers or windows, we need to
9885 recompute to reflect the new bindings. But we'll
9886 recompute when update_mode_lines is set too; that means
9887 that people can use force-mode-line-update to request
9888 that the menu bar be recomputed. The adverse effect on
9889 the rest of the redisplay algorithm is about the same as
9890 windows_or_buffers_changed anyway. */
9891 if (windows_or_buffers_changed
9892 /* This used to test w->update_mode_line, but we believe
9893 there is no need to recompute the menu in that case. */
9894 || update_mode_lines
9895 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9896 < BUF_MODIFF (XBUFFER (w->buffer)))
9897 != !NILP (w->last_had_star))
9898 || ((!NILP (Vtransient_mark_mode)
9899 && !NILP (XBUFFER (w->buffer)->mark_active))
9900 != !NILP (w->region_showing)))
9902 struct buffer *prev = current_buffer;
9903 int count = SPECPDL_INDEX ();
9905 specbind (Qinhibit_menubar_update, Qt);
9907 set_buffer_internal_1 (XBUFFER (w->buffer));
9908 if (save_match_data)
9909 record_unwind_save_match_data ();
9910 if (NILP (Voverriding_local_map_menu_flag))
9912 specbind (Qoverriding_terminal_local_map, Qnil);
9913 specbind (Qoverriding_local_map, Qnil);
9916 if (!hooks_run)
9918 /* Run the Lucid hook. */
9919 safe_run_hooks (Qactivate_menubar_hook);
9921 /* If it has changed current-menubar from previous value,
9922 really recompute the menu-bar from the value. */
9923 if (! NILP (Vlucid_menu_bar_dirty_flag))
9924 call0 (Qrecompute_lucid_menubar);
9926 safe_run_hooks (Qmenu_bar_update_hook);
9928 hooks_run = 1;
9931 XSETFRAME (Vmenu_updating_frame, f);
9932 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9934 /* Redisplay the menu bar in case we changed it. */
9935 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9936 || defined (HAVE_NS) || defined (USE_GTK)
9937 if (FRAME_WINDOW_P (f))
9939 #if defined (HAVE_NS)
9940 /* All frames on Mac OS share the same menubar. So only
9941 the selected frame should be allowed to set it. */
9942 if (f == SELECTED_FRAME ())
9943 #endif
9944 set_frame_menubar (f, 0, 0);
9946 else
9947 /* On a terminal screen, the menu bar is an ordinary screen
9948 line, and this makes it get updated. */
9949 w->update_mode_line = Qt;
9950 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9951 /* In the non-toolkit version, the menu bar is an ordinary screen
9952 line, and this makes it get updated. */
9953 w->update_mode_line = Qt;
9954 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9956 unbind_to (count, Qnil);
9957 set_buffer_internal_1 (prev);
9961 return hooks_run;
9966 /***********************************************************************
9967 Output Cursor
9968 ***********************************************************************/
9970 #ifdef HAVE_WINDOW_SYSTEM
9972 /* EXPORT:
9973 Nominal cursor position -- where to draw output.
9974 HPOS and VPOS are window relative glyph matrix coordinates.
9975 X and Y are window relative pixel coordinates. */
9977 struct cursor_pos output_cursor;
9980 /* EXPORT:
9981 Set the global variable output_cursor to CURSOR. All cursor
9982 positions are relative to updated_window. */
9984 void
9985 set_output_cursor (struct cursor_pos *cursor)
9987 output_cursor.hpos = cursor->hpos;
9988 output_cursor.vpos = cursor->vpos;
9989 output_cursor.x = cursor->x;
9990 output_cursor.y = cursor->y;
9994 /* EXPORT for RIF:
9995 Set a nominal cursor position.
9997 HPOS and VPOS are column/row positions in a window glyph matrix. X
9998 and Y are window text area relative pixel positions.
10000 If this is done during an update, updated_window will contain the
10001 window that is being updated and the position is the future output
10002 cursor position for that window. If updated_window is null, use
10003 selected_window and display the cursor at the given position. */
10005 void
10006 x_cursor_to (int vpos, int hpos, int y, int x)
10008 struct window *w;
10010 /* If updated_window is not set, work on selected_window. */
10011 if (updated_window)
10012 w = updated_window;
10013 else
10014 w = XWINDOW (selected_window);
10016 /* Set the output cursor. */
10017 output_cursor.hpos = hpos;
10018 output_cursor.vpos = vpos;
10019 output_cursor.x = x;
10020 output_cursor.y = y;
10022 /* If not called as part of an update, really display the cursor.
10023 This will also set the cursor position of W. */
10024 if (updated_window == NULL)
10026 BLOCK_INPUT;
10027 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10028 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10029 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10030 UNBLOCK_INPUT;
10034 #endif /* HAVE_WINDOW_SYSTEM */
10037 /***********************************************************************
10038 Tool-bars
10039 ***********************************************************************/
10041 #ifdef HAVE_WINDOW_SYSTEM
10043 /* Where the mouse was last time we reported a mouse event. */
10045 FRAME_PTR last_mouse_frame;
10047 /* Tool-bar item index of the item on which a mouse button was pressed
10048 or -1. */
10050 int last_tool_bar_item;
10053 static Lisp_Object
10054 update_tool_bar_unwind (Lisp_Object frame)
10056 selected_frame = frame;
10057 return Qnil;
10060 /* Update the tool-bar item list for frame F. This has to be done
10061 before we start to fill in any display lines. Called from
10062 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10063 and restore it here. */
10065 static void
10066 update_tool_bar (struct frame *f, int save_match_data)
10068 #if defined (USE_GTK) || defined (HAVE_NS)
10069 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10070 #else
10071 int do_update = WINDOWP (f->tool_bar_window)
10072 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10073 #endif
10075 if (do_update)
10077 Lisp_Object window;
10078 struct window *w;
10080 window = FRAME_SELECTED_WINDOW (f);
10081 w = XWINDOW (window);
10083 /* If the user has switched buffers or windows, we need to
10084 recompute to reflect the new bindings. But we'll
10085 recompute when update_mode_lines is set too; that means
10086 that people can use force-mode-line-update to request
10087 that the menu bar be recomputed. The adverse effect on
10088 the rest of the redisplay algorithm is about the same as
10089 windows_or_buffers_changed anyway. */
10090 if (windows_or_buffers_changed
10091 || !NILP (w->update_mode_line)
10092 || update_mode_lines
10093 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10094 < BUF_MODIFF (XBUFFER (w->buffer)))
10095 != !NILP (w->last_had_star))
10096 || ((!NILP (Vtransient_mark_mode)
10097 && !NILP (XBUFFER (w->buffer)->mark_active))
10098 != !NILP (w->region_showing)))
10100 struct buffer *prev = current_buffer;
10101 int count = SPECPDL_INDEX ();
10102 Lisp_Object frame, new_tool_bar;
10103 int new_n_tool_bar;
10104 struct gcpro gcpro1;
10106 /* Set current_buffer to the buffer of the selected
10107 window of the frame, so that we get the right local
10108 keymaps. */
10109 set_buffer_internal_1 (XBUFFER (w->buffer));
10111 /* Save match data, if we must. */
10112 if (save_match_data)
10113 record_unwind_save_match_data ();
10115 /* Make sure that we don't accidentally use bogus keymaps. */
10116 if (NILP (Voverriding_local_map_menu_flag))
10118 specbind (Qoverriding_terminal_local_map, Qnil);
10119 specbind (Qoverriding_local_map, Qnil);
10122 GCPRO1 (new_tool_bar);
10124 /* We must temporarily set the selected frame to this frame
10125 before calling tool_bar_items, because the calculation of
10126 the tool-bar keymap uses the selected frame (see
10127 `tool-bar-make-keymap' in tool-bar.el). */
10128 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10129 XSETFRAME (frame, f);
10130 selected_frame = frame;
10132 /* Build desired tool-bar items from keymaps. */
10133 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10134 &new_n_tool_bar);
10136 /* Redisplay the tool-bar if we changed it. */
10137 if (new_n_tool_bar != f->n_tool_bar_items
10138 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10140 /* Redisplay that happens asynchronously due to an expose event
10141 may access f->tool_bar_items. Make sure we update both
10142 variables within BLOCK_INPUT so no such event interrupts. */
10143 BLOCK_INPUT;
10144 f->tool_bar_items = new_tool_bar;
10145 f->n_tool_bar_items = new_n_tool_bar;
10146 w->update_mode_line = Qt;
10147 UNBLOCK_INPUT;
10150 UNGCPRO;
10152 unbind_to (count, Qnil);
10153 set_buffer_internal_1 (prev);
10159 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10160 F's desired tool-bar contents. F->tool_bar_items must have
10161 been set up previously by calling prepare_menu_bars. */
10163 static void
10164 build_desired_tool_bar_string (struct frame *f)
10166 int i, size, size_needed;
10167 struct gcpro gcpro1, gcpro2, gcpro3;
10168 Lisp_Object image, plist, props;
10170 image = plist = props = Qnil;
10171 GCPRO3 (image, plist, props);
10173 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10174 Otherwise, make a new string. */
10176 /* The size of the string we might be able to reuse. */
10177 size = (STRINGP (f->desired_tool_bar_string)
10178 ? SCHARS (f->desired_tool_bar_string)
10179 : 0);
10181 /* We need one space in the string for each image. */
10182 size_needed = f->n_tool_bar_items;
10184 /* Reuse f->desired_tool_bar_string, if possible. */
10185 if (size < size_needed || NILP (f->desired_tool_bar_string))
10186 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10187 make_number (' '));
10188 else
10190 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10191 Fremove_text_properties (make_number (0), make_number (size),
10192 props, f->desired_tool_bar_string);
10195 /* Put a `display' property on the string for the images to display,
10196 put a `menu_item' property on tool-bar items with a value that
10197 is the index of the item in F's tool-bar item vector. */
10198 for (i = 0; i < f->n_tool_bar_items; ++i)
10200 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10202 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10203 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10204 int hmargin, vmargin, relief, idx, end;
10206 /* If image is a vector, choose the image according to the
10207 button state. */
10208 image = PROP (TOOL_BAR_ITEM_IMAGES);
10209 if (VECTORP (image))
10211 if (enabled_p)
10212 idx = (selected_p
10213 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10214 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10215 else
10216 idx = (selected_p
10217 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10218 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10220 xassert (ASIZE (image) >= idx);
10221 image = AREF (image, idx);
10223 else
10224 idx = -1;
10226 /* Ignore invalid image specifications. */
10227 if (!valid_image_p (image))
10228 continue;
10230 /* Display the tool-bar button pressed, or depressed. */
10231 plist = Fcopy_sequence (XCDR (image));
10233 /* Compute margin and relief to draw. */
10234 relief = (tool_bar_button_relief >= 0
10235 ? tool_bar_button_relief
10236 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10237 hmargin = vmargin = relief;
10239 if (INTEGERP (Vtool_bar_button_margin)
10240 && XINT (Vtool_bar_button_margin) > 0)
10242 hmargin += XFASTINT (Vtool_bar_button_margin);
10243 vmargin += XFASTINT (Vtool_bar_button_margin);
10245 else if (CONSP (Vtool_bar_button_margin))
10247 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10248 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10249 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10251 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10252 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10253 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10256 if (auto_raise_tool_bar_buttons_p)
10258 /* Add a `:relief' property to the image spec if the item is
10259 selected. */
10260 if (selected_p)
10262 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10263 hmargin -= relief;
10264 vmargin -= relief;
10267 else
10269 /* If image is selected, display it pressed, i.e. with a
10270 negative relief. If it's not selected, display it with a
10271 raised relief. */
10272 plist = Fplist_put (plist, QCrelief,
10273 (selected_p
10274 ? make_number (-relief)
10275 : make_number (relief)));
10276 hmargin -= relief;
10277 vmargin -= relief;
10280 /* Put a margin around the image. */
10281 if (hmargin || vmargin)
10283 if (hmargin == vmargin)
10284 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10285 else
10286 plist = Fplist_put (plist, QCmargin,
10287 Fcons (make_number (hmargin),
10288 make_number (vmargin)));
10291 /* If button is not enabled, and we don't have special images
10292 for the disabled state, make the image appear disabled by
10293 applying an appropriate algorithm to it. */
10294 if (!enabled_p && idx < 0)
10295 plist = Fplist_put (plist, QCconversion, Qdisabled);
10297 /* Put a `display' text property on the string for the image to
10298 display. Put a `menu-item' property on the string that gives
10299 the start of this item's properties in the tool-bar items
10300 vector. */
10301 image = Fcons (Qimage, plist);
10302 props = list4 (Qdisplay, image,
10303 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10305 /* Let the last image hide all remaining spaces in the tool bar
10306 string. The string can be longer than needed when we reuse a
10307 previous string. */
10308 if (i + 1 == f->n_tool_bar_items)
10309 end = SCHARS (f->desired_tool_bar_string);
10310 else
10311 end = i + 1;
10312 Fadd_text_properties (make_number (i), make_number (end),
10313 props, f->desired_tool_bar_string);
10314 #undef PROP
10317 UNGCPRO;
10321 /* Display one line of the tool-bar of frame IT->f.
10323 HEIGHT specifies the desired height of the tool-bar line.
10324 If the actual height of the glyph row is less than HEIGHT, the
10325 row's height is increased to HEIGHT, and the icons are centered
10326 vertically in the new height.
10328 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10329 count a final empty row in case the tool-bar width exactly matches
10330 the window width.
10333 static void
10334 display_tool_bar_line (struct it *it, int height)
10336 struct glyph_row *row = it->glyph_row;
10337 int max_x = it->last_visible_x;
10338 struct glyph *last;
10340 prepare_desired_row (row);
10341 row->y = it->current_y;
10343 /* Note that this isn't made use of if the face hasn't a box,
10344 so there's no need to check the face here. */
10345 it->start_of_box_run_p = 1;
10347 while (it->current_x < max_x)
10349 int x, n_glyphs_before, i, nglyphs;
10350 struct it it_before;
10352 /* Get the next display element. */
10353 if (!get_next_display_element (it))
10355 /* Don't count empty row if we are counting needed tool-bar lines. */
10356 if (height < 0 && !it->hpos)
10357 return;
10358 break;
10361 /* Produce glyphs. */
10362 n_glyphs_before = row->used[TEXT_AREA];
10363 it_before = *it;
10365 PRODUCE_GLYPHS (it);
10367 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10368 i = 0;
10369 x = it_before.current_x;
10370 while (i < nglyphs)
10372 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10374 if (x + glyph->pixel_width > max_x)
10376 /* Glyph doesn't fit on line. Backtrack. */
10377 row->used[TEXT_AREA] = n_glyphs_before;
10378 *it = it_before;
10379 /* If this is the only glyph on this line, it will never fit on the
10380 toolbar, so skip it. But ensure there is at least one glyph,
10381 so we don't accidentally disable the tool-bar. */
10382 if (n_glyphs_before == 0
10383 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10384 break;
10385 goto out;
10388 ++it->hpos;
10389 x += glyph->pixel_width;
10390 ++i;
10393 /* Stop at line ends. */
10394 if (ITERATOR_AT_END_OF_LINE_P (it))
10395 break;
10397 set_iterator_to_next (it, 1);
10400 out:;
10402 row->displays_text_p = row->used[TEXT_AREA] != 0;
10404 /* Use default face for the border below the tool bar.
10406 FIXME: When auto-resize-tool-bars is grow-only, there is
10407 no additional border below the possibly empty tool-bar lines.
10408 So to make the extra empty lines look "normal", we have to
10409 use the tool-bar face for the border too. */
10410 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10411 it->face_id = DEFAULT_FACE_ID;
10413 extend_face_to_end_of_line (it);
10414 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10415 last->right_box_line_p = 1;
10416 if (last == row->glyphs[TEXT_AREA])
10417 last->left_box_line_p = 1;
10419 /* Make line the desired height and center it vertically. */
10420 if ((height -= it->max_ascent + it->max_descent) > 0)
10422 /* Don't add more than one line height. */
10423 height %= FRAME_LINE_HEIGHT (it->f);
10424 it->max_ascent += height / 2;
10425 it->max_descent += (height + 1) / 2;
10428 compute_line_metrics (it);
10430 /* If line is empty, make it occupy the rest of the tool-bar. */
10431 if (!row->displays_text_p)
10433 row->height = row->phys_height = it->last_visible_y - row->y;
10434 row->visible_height = row->height;
10435 row->ascent = row->phys_ascent = 0;
10436 row->extra_line_spacing = 0;
10439 row->full_width_p = 1;
10440 row->continued_p = 0;
10441 row->truncated_on_left_p = 0;
10442 row->truncated_on_right_p = 0;
10444 it->current_x = it->hpos = 0;
10445 it->current_y += row->height;
10446 ++it->vpos;
10447 ++it->glyph_row;
10451 /* Max tool-bar height. */
10453 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10454 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10456 /* Value is the number of screen lines needed to make all tool-bar
10457 items of frame F visible. The number of actual rows needed is
10458 returned in *N_ROWS if non-NULL. */
10460 static int
10461 tool_bar_lines_needed (struct frame *f, int *n_rows)
10463 struct window *w = XWINDOW (f->tool_bar_window);
10464 struct it it;
10465 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10466 the desired matrix, so use (unused) mode-line row as temporary row to
10467 avoid destroying the first tool-bar row. */
10468 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10470 /* Initialize an iterator for iteration over
10471 F->desired_tool_bar_string in the tool-bar window of frame F. */
10472 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10473 it.first_visible_x = 0;
10474 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10475 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10477 while (!ITERATOR_AT_END_P (&it))
10479 clear_glyph_row (temp_row);
10480 it.glyph_row = temp_row;
10481 display_tool_bar_line (&it, -1);
10483 clear_glyph_row (temp_row);
10485 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10486 if (n_rows)
10487 *n_rows = it.vpos > 0 ? it.vpos : -1;
10489 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10493 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10494 0, 1, 0,
10495 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10496 (Lisp_Object frame)
10498 struct frame *f;
10499 struct window *w;
10500 int nlines = 0;
10502 if (NILP (frame))
10503 frame = selected_frame;
10504 else
10505 CHECK_FRAME (frame);
10506 f = XFRAME (frame);
10508 if (WINDOWP (f->tool_bar_window)
10509 || (w = XWINDOW (f->tool_bar_window),
10510 WINDOW_TOTAL_LINES (w) > 0))
10512 update_tool_bar (f, 1);
10513 if (f->n_tool_bar_items)
10515 build_desired_tool_bar_string (f);
10516 nlines = tool_bar_lines_needed (f, NULL);
10520 return make_number (nlines);
10524 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10525 height should be changed. */
10527 static int
10528 redisplay_tool_bar (struct frame *f)
10530 struct window *w;
10531 struct it it;
10532 struct glyph_row *row;
10534 #if defined (USE_GTK) || defined (HAVE_NS)
10535 if (FRAME_EXTERNAL_TOOL_BAR (f))
10536 update_frame_tool_bar (f);
10537 return 0;
10538 #endif
10540 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10541 do anything. This means you must start with tool-bar-lines
10542 non-zero to get the auto-sizing effect. Or in other words, you
10543 can turn off tool-bars by specifying tool-bar-lines zero. */
10544 if (!WINDOWP (f->tool_bar_window)
10545 || (w = XWINDOW (f->tool_bar_window),
10546 WINDOW_TOTAL_LINES (w) == 0))
10547 return 0;
10549 /* Set up an iterator for the tool-bar window. */
10550 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10551 it.first_visible_x = 0;
10552 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10553 row = it.glyph_row;
10555 /* Build a string that represents the contents of the tool-bar. */
10556 build_desired_tool_bar_string (f);
10557 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10559 if (f->n_tool_bar_rows == 0)
10561 int nlines;
10563 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10564 nlines != WINDOW_TOTAL_LINES (w)))
10566 Lisp_Object frame;
10567 int old_height = WINDOW_TOTAL_LINES (w);
10569 XSETFRAME (frame, f);
10570 Fmodify_frame_parameters (frame,
10571 Fcons (Fcons (Qtool_bar_lines,
10572 make_number (nlines)),
10573 Qnil));
10574 if (WINDOW_TOTAL_LINES (w) != old_height)
10576 clear_glyph_matrix (w->desired_matrix);
10577 fonts_changed_p = 1;
10578 return 1;
10583 /* Display as many lines as needed to display all tool-bar items. */
10585 if (f->n_tool_bar_rows > 0)
10587 int border, rows, height, extra;
10589 if (INTEGERP (Vtool_bar_border))
10590 border = XINT (Vtool_bar_border);
10591 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10592 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10593 else if (EQ (Vtool_bar_border, Qborder_width))
10594 border = f->border_width;
10595 else
10596 border = 0;
10597 if (border < 0)
10598 border = 0;
10600 rows = f->n_tool_bar_rows;
10601 height = max (1, (it.last_visible_y - border) / rows);
10602 extra = it.last_visible_y - border - height * rows;
10604 while (it.current_y < it.last_visible_y)
10606 int h = 0;
10607 if (extra > 0 && rows-- > 0)
10609 h = (extra + rows - 1) / rows;
10610 extra -= h;
10612 display_tool_bar_line (&it, height + h);
10615 else
10617 while (it.current_y < it.last_visible_y)
10618 display_tool_bar_line (&it, 0);
10621 /* It doesn't make much sense to try scrolling in the tool-bar
10622 window, so don't do it. */
10623 w->desired_matrix->no_scrolling_p = 1;
10624 w->must_be_updated_p = 1;
10626 if (!NILP (Vauto_resize_tool_bars))
10628 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10629 int change_height_p = 0;
10631 /* If we couldn't display everything, change the tool-bar's
10632 height if there is room for more. */
10633 if (IT_STRING_CHARPOS (it) < it.end_charpos
10634 && it.current_y < max_tool_bar_height)
10635 change_height_p = 1;
10637 row = it.glyph_row - 1;
10639 /* If there are blank lines at the end, except for a partially
10640 visible blank line at the end that is smaller than
10641 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10642 if (!row->displays_text_p
10643 && row->height >= FRAME_LINE_HEIGHT (f))
10644 change_height_p = 1;
10646 /* If row displays tool-bar items, but is partially visible,
10647 change the tool-bar's height. */
10648 if (row->displays_text_p
10649 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10650 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10651 change_height_p = 1;
10653 /* Resize windows as needed by changing the `tool-bar-lines'
10654 frame parameter. */
10655 if (change_height_p)
10657 Lisp_Object frame;
10658 int old_height = WINDOW_TOTAL_LINES (w);
10659 int nrows;
10660 int nlines = tool_bar_lines_needed (f, &nrows);
10662 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10663 && !f->minimize_tool_bar_window_p)
10664 ? (nlines > old_height)
10665 : (nlines != old_height));
10666 f->minimize_tool_bar_window_p = 0;
10668 if (change_height_p)
10670 XSETFRAME (frame, f);
10671 Fmodify_frame_parameters (frame,
10672 Fcons (Fcons (Qtool_bar_lines,
10673 make_number (nlines)),
10674 Qnil));
10675 if (WINDOW_TOTAL_LINES (w) != old_height)
10677 clear_glyph_matrix (w->desired_matrix);
10678 f->n_tool_bar_rows = nrows;
10679 fonts_changed_p = 1;
10680 return 1;
10686 f->minimize_tool_bar_window_p = 0;
10687 return 0;
10691 /* Get information about the tool-bar item which is displayed in GLYPH
10692 on frame F. Return in *PROP_IDX the index where tool-bar item
10693 properties start in F->tool_bar_items. Value is zero if
10694 GLYPH doesn't display a tool-bar item. */
10696 static int
10697 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10699 Lisp_Object prop;
10700 int success_p;
10701 int charpos;
10703 /* This function can be called asynchronously, which means we must
10704 exclude any possibility that Fget_text_property signals an
10705 error. */
10706 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10707 charpos = max (0, charpos);
10709 /* Get the text property `menu-item' at pos. The value of that
10710 property is the start index of this item's properties in
10711 F->tool_bar_items. */
10712 prop = Fget_text_property (make_number (charpos),
10713 Qmenu_item, f->current_tool_bar_string);
10714 if (INTEGERP (prop))
10716 *prop_idx = XINT (prop);
10717 success_p = 1;
10719 else
10720 success_p = 0;
10722 return success_p;
10726 /* Get information about the tool-bar item at position X/Y on frame F.
10727 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10728 the current matrix of the tool-bar window of F, or NULL if not
10729 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10730 item in F->tool_bar_items. Value is
10732 -1 if X/Y is not on a tool-bar item
10733 0 if X/Y is on the same item that was highlighted before.
10734 1 otherwise. */
10736 static int
10737 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10738 int *hpos, int *vpos, int *prop_idx)
10740 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10741 struct window *w = XWINDOW (f->tool_bar_window);
10742 int area;
10744 /* Find the glyph under X/Y. */
10745 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10746 if (*glyph == NULL)
10747 return -1;
10749 /* Get the start of this tool-bar item's properties in
10750 f->tool_bar_items. */
10751 if (!tool_bar_item_info (f, *glyph, prop_idx))
10752 return -1;
10754 /* Is mouse on the highlighted item? */
10755 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10756 && *vpos >= dpyinfo->mouse_face_beg_row
10757 && *vpos <= dpyinfo->mouse_face_end_row
10758 && (*vpos > dpyinfo->mouse_face_beg_row
10759 || *hpos >= dpyinfo->mouse_face_beg_col)
10760 && (*vpos < dpyinfo->mouse_face_end_row
10761 || *hpos < dpyinfo->mouse_face_end_col
10762 || dpyinfo->mouse_face_past_end))
10763 return 0;
10765 return 1;
10769 /* EXPORT:
10770 Handle mouse button event on the tool-bar of frame F, at
10771 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10772 0 for button release. MODIFIERS is event modifiers for button
10773 release. */
10775 void
10776 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10777 unsigned int modifiers)
10779 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10780 struct window *w = XWINDOW (f->tool_bar_window);
10781 int hpos, vpos, prop_idx;
10782 struct glyph *glyph;
10783 Lisp_Object enabled_p;
10785 /* If not on the highlighted tool-bar item, return. */
10786 frame_to_window_pixel_xy (w, &x, &y);
10787 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10788 return;
10790 /* If item is disabled, do nothing. */
10791 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10792 if (NILP (enabled_p))
10793 return;
10795 if (down_p)
10797 /* Show item in pressed state. */
10798 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10799 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10800 last_tool_bar_item = prop_idx;
10802 else
10804 Lisp_Object key, frame;
10805 struct input_event event;
10806 EVENT_INIT (event);
10808 /* Show item in released state. */
10809 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10810 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10812 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10814 XSETFRAME (frame, f);
10815 event.kind = TOOL_BAR_EVENT;
10816 event.frame_or_window = frame;
10817 event.arg = frame;
10818 kbd_buffer_store_event (&event);
10820 event.kind = TOOL_BAR_EVENT;
10821 event.frame_or_window = frame;
10822 event.arg = key;
10823 event.modifiers = modifiers;
10824 kbd_buffer_store_event (&event);
10825 last_tool_bar_item = -1;
10830 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10831 tool-bar window-relative coordinates X/Y. Called from
10832 note_mouse_highlight. */
10834 static void
10835 note_tool_bar_highlight (struct frame *f, int x, int y)
10837 Lisp_Object window = f->tool_bar_window;
10838 struct window *w = XWINDOW (window);
10839 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10840 int hpos, vpos;
10841 struct glyph *glyph;
10842 struct glyph_row *row;
10843 int i;
10844 Lisp_Object enabled_p;
10845 int prop_idx;
10846 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10847 int mouse_down_p, rc;
10849 /* Function note_mouse_highlight is called with negative X/Y
10850 values when mouse moves outside of the frame. */
10851 if (x <= 0 || y <= 0)
10853 clear_mouse_face (dpyinfo);
10854 return;
10857 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10858 if (rc < 0)
10860 /* Not on tool-bar item. */
10861 clear_mouse_face (dpyinfo);
10862 return;
10864 else if (rc == 0)
10865 /* On same tool-bar item as before. */
10866 goto set_help_echo;
10868 clear_mouse_face (dpyinfo);
10870 /* Mouse is down, but on different tool-bar item? */
10871 mouse_down_p = (dpyinfo->grabbed
10872 && f == last_mouse_frame
10873 && FRAME_LIVE_P (f));
10874 if (mouse_down_p
10875 && last_tool_bar_item != prop_idx)
10876 return;
10878 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10879 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10881 /* If tool-bar item is not enabled, don't highlight it. */
10882 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10883 if (!NILP (enabled_p))
10885 /* Compute the x-position of the glyph. In front and past the
10886 image is a space. We include this in the highlighted area. */
10887 row = MATRIX_ROW (w->current_matrix, vpos);
10888 for (i = x = 0; i < hpos; ++i)
10889 x += row->glyphs[TEXT_AREA][i].pixel_width;
10891 /* Record this as the current active region. */
10892 dpyinfo->mouse_face_beg_col = hpos;
10893 dpyinfo->mouse_face_beg_row = vpos;
10894 dpyinfo->mouse_face_beg_x = x;
10895 dpyinfo->mouse_face_beg_y = row->y;
10896 dpyinfo->mouse_face_past_end = 0;
10898 dpyinfo->mouse_face_end_col = hpos + 1;
10899 dpyinfo->mouse_face_end_row = vpos;
10900 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10901 dpyinfo->mouse_face_end_y = row->y;
10902 dpyinfo->mouse_face_window = window;
10903 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10905 /* Display it as active. */
10906 show_mouse_face (dpyinfo, draw);
10907 dpyinfo->mouse_face_image_state = draw;
10910 set_help_echo:
10912 /* Set help_echo_string to a help string to display for this tool-bar item.
10913 XTread_socket does the rest. */
10914 help_echo_object = help_echo_window = Qnil;
10915 help_echo_pos = -1;
10916 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10917 if (NILP (help_echo_string))
10918 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10921 #endif /* HAVE_WINDOW_SYSTEM */
10925 /************************************************************************
10926 Horizontal scrolling
10927 ************************************************************************/
10929 static int hscroll_window_tree (Lisp_Object);
10930 static int hscroll_windows (Lisp_Object);
10932 /* For all leaf windows in the window tree rooted at WINDOW, set their
10933 hscroll value so that PT is (i) visible in the window, and (ii) so
10934 that it is not within a certain margin at the window's left and
10935 right border. Value is non-zero if any window's hscroll has been
10936 changed. */
10938 static int
10939 hscroll_window_tree (Lisp_Object window)
10941 int hscrolled_p = 0;
10942 int hscroll_relative_p = FLOATP (Vhscroll_step);
10943 int hscroll_step_abs = 0;
10944 double hscroll_step_rel = 0;
10946 if (hscroll_relative_p)
10948 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10949 if (hscroll_step_rel < 0)
10951 hscroll_relative_p = 0;
10952 hscroll_step_abs = 0;
10955 else if (INTEGERP (Vhscroll_step))
10957 hscroll_step_abs = XINT (Vhscroll_step);
10958 if (hscroll_step_abs < 0)
10959 hscroll_step_abs = 0;
10961 else
10962 hscroll_step_abs = 0;
10964 while (WINDOWP (window))
10966 struct window *w = XWINDOW (window);
10968 if (WINDOWP (w->hchild))
10969 hscrolled_p |= hscroll_window_tree (w->hchild);
10970 else if (WINDOWP (w->vchild))
10971 hscrolled_p |= hscroll_window_tree (w->vchild);
10972 else if (w->cursor.vpos >= 0)
10974 int h_margin;
10975 int text_area_width;
10976 struct glyph_row *current_cursor_row
10977 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10978 struct glyph_row *desired_cursor_row
10979 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10980 struct glyph_row *cursor_row
10981 = (desired_cursor_row->enabled_p
10982 ? desired_cursor_row
10983 : current_cursor_row);
10985 text_area_width = window_box_width (w, TEXT_AREA);
10987 /* Scroll when cursor is inside this scroll margin. */
10988 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10990 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10991 && ((XFASTINT (w->hscroll)
10992 && w->cursor.x <= h_margin)
10993 || (cursor_row->enabled_p
10994 && cursor_row->truncated_on_right_p
10995 && (w->cursor.x >= text_area_width - h_margin))))
10997 struct it it;
10998 int hscroll;
10999 struct buffer *saved_current_buffer;
11000 EMACS_INT pt;
11001 int wanted_x;
11003 /* Find point in a display of infinite width. */
11004 saved_current_buffer = current_buffer;
11005 current_buffer = XBUFFER (w->buffer);
11007 if (w == XWINDOW (selected_window))
11008 pt = BUF_PT (current_buffer);
11009 else
11011 pt = marker_position (w->pointm);
11012 pt = max (BEGV, pt);
11013 pt = min (ZV, pt);
11016 /* Move iterator to pt starting at cursor_row->start in
11017 a line with infinite width. */
11018 init_to_row_start (&it, w, cursor_row);
11019 it.last_visible_x = INFINITY;
11020 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11021 current_buffer = saved_current_buffer;
11023 /* Position cursor in window. */
11024 if (!hscroll_relative_p && hscroll_step_abs == 0)
11025 hscroll = max (0, (it.current_x
11026 - (ITERATOR_AT_END_OF_LINE_P (&it)
11027 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11028 : (text_area_width / 2))))
11029 / FRAME_COLUMN_WIDTH (it.f);
11030 else if (w->cursor.x >= text_area_width - h_margin)
11032 if (hscroll_relative_p)
11033 wanted_x = text_area_width * (1 - hscroll_step_rel)
11034 - h_margin;
11035 else
11036 wanted_x = text_area_width
11037 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11038 - h_margin;
11039 hscroll
11040 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11042 else
11044 if (hscroll_relative_p)
11045 wanted_x = text_area_width * hscroll_step_rel
11046 + h_margin;
11047 else
11048 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11049 + h_margin;
11050 hscroll
11051 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11053 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11055 /* Don't call Fset_window_hscroll if value hasn't
11056 changed because it will prevent redisplay
11057 optimizations. */
11058 if (XFASTINT (w->hscroll) != hscroll)
11060 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11061 w->hscroll = make_number (hscroll);
11062 hscrolled_p = 1;
11067 window = w->next;
11070 /* Value is non-zero if hscroll of any leaf window has been changed. */
11071 return hscrolled_p;
11075 /* Set hscroll so that cursor is visible and not inside horizontal
11076 scroll margins for all windows in the tree rooted at WINDOW. See
11077 also hscroll_window_tree above. Value is non-zero if any window's
11078 hscroll has been changed. If it has, desired matrices on the frame
11079 of WINDOW are cleared. */
11081 static int
11082 hscroll_windows (Lisp_Object window)
11084 int hscrolled_p = hscroll_window_tree (window);
11085 if (hscrolled_p)
11086 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11087 return hscrolled_p;
11092 /************************************************************************
11093 Redisplay
11094 ************************************************************************/
11096 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11097 to a non-zero value. This is sometimes handy to have in a debugger
11098 session. */
11100 #if GLYPH_DEBUG
11102 /* First and last unchanged row for try_window_id. */
11104 int debug_first_unchanged_at_end_vpos;
11105 int debug_last_unchanged_at_beg_vpos;
11107 /* Delta vpos and y. */
11109 int debug_dvpos, debug_dy;
11111 /* Delta in characters and bytes for try_window_id. */
11113 EMACS_INT debug_delta, debug_delta_bytes;
11115 /* Values of window_end_pos and window_end_vpos at the end of
11116 try_window_id. */
11118 EMACS_INT debug_end_pos, debug_end_vpos;
11120 /* Append a string to W->desired_matrix->method. FMT is a printf
11121 format string. A1...A9 are a supplement for a variable-length
11122 argument list. If trace_redisplay_p is non-zero also printf the
11123 resulting string to stderr. */
11125 static void
11126 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11127 struct window *w;
11128 char *fmt;
11129 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11131 char buffer[512];
11132 char *method = w->desired_matrix->method;
11133 int len = strlen (method);
11134 int size = sizeof w->desired_matrix->method;
11135 int remaining = size - len - 1;
11137 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11138 if (len && remaining)
11140 method[len] = '|';
11141 --remaining, ++len;
11144 strncpy (method + len, buffer, remaining);
11146 if (trace_redisplay_p)
11147 fprintf (stderr, "%p (%s): %s\n",
11149 ((BUFFERP (w->buffer)
11150 && STRINGP (XBUFFER (w->buffer)->name))
11151 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11152 : "no buffer"),
11153 buffer);
11156 #endif /* GLYPH_DEBUG */
11159 /* Value is non-zero if all changes in window W, which displays
11160 current_buffer, are in the text between START and END. START is a
11161 buffer position, END is given as a distance from Z. Used in
11162 redisplay_internal for display optimization. */
11164 static INLINE int
11165 text_outside_line_unchanged_p (struct window *w,
11166 EMACS_INT start, EMACS_INT end)
11168 int unchanged_p = 1;
11170 /* If text or overlays have changed, see where. */
11171 if (XFASTINT (w->last_modified) < MODIFF
11172 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11174 /* Gap in the line? */
11175 if (GPT < start || Z - GPT < end)
11176 unchanged_p = 0;
11178 /* Changes start in front of the line, or end after it? */
11179 if (unchanged_p
11180 && (BEG_UNCHANGED < start - 1
11181 || END_UNCHANGED < end))
11182 unchanged_p = 0;
11184 /* If selective display, can't optimize if changes start at the
11185 beginning of the line. */
11186 if (unchanged_p
11187 && INTEGERP (current_buffer->selective_display)
11188 && XINT (current_buffer->selective_display) > 0
11189 && (BEG_UNCHANGED < start || GPT <= start))
11190 unchanged_p = 0;
11192 /* If there are overlays at the start or end of the line, these
11193 may have overlay strings with newlines in them. A change at
11194 START, for instance, may actually concern the display of such
11195 overlay strings as well, and they are displayed on different
11196 lines. So, quickly rule out this case. (For the future, it
11197 might be desirable to implement something more telling than
11198 just BEG/END_UNCHANGED.) */
11199 if (unchanged_p)
11201 if (BEG + BEG_UNCHANGED == start
11202 && overlay_touches_p (start))
11203 unchanged_p = 0;
11204 if (END_UNCHANGED == end
11205 && overlay_touches_p (Z - end))
11206 unchanged_p = 0;
11209 /* Under bidi reordering, adding or deleting a character in the
11210 beginning of a paragraph, before the first strong directional
11211 character, can change the base direction of the paragraph (unless
11212 the buffer specifies a fixed paragraph direction), which will
11213 require to redisplay the whole paragraph. It might be worthwhile
11214 to find the paragraph limits and widen the range of redisplayed
11215 lines to that, but for now just give up this optimization. */
11216 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11217 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11218 unchanged_p = 0;
11221 return unchanged_p;
11225 /* Do a frame update, taking possible shortcuts into account. This is
11226 the main external entry point for redisplay.
11228 If the last redisplay displayed an echo area message and that message
11229 is no longer requested, we clear the echo area or bring back the
11230 mini-buffer if that is in use. */
11232 void
11233 redisplay (void)
11235 redisplay_internal (0);
11239 static Lisp_Object
11240 overlay_arrow_string_or_property (Lisp_Object var)
11242 Lisp_Object val;
11244 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11245 return val;
11247 return Voverlay_arrow_string;
11250 /* Return 1 if there are any overlay-arrows in current_buffer. */
11251 static int
11252 overlay_arrow_in_current_buffer_p (void)
11254 Lisp_Object vlist;
11256 for (vlist = Voverlay_arrow_variable_list;
11257 CONSP (vlist);
11258 vlist = XCDR (vlist))
11260 Lisp_Object var = XCAR (vlist);
11261 Lisp_Object val;
11263 if (!SYMBOLP (var))
11264 continue;
11265 val = find_symbol_value (var);
11266 if (MARKERP (val)
11267 && current_buffer == XMARKER (val)->buffer)
11268 return 1;
11270 return 0;
11274 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11275 has changed. */
11277 static int
11278 overlay_arrows_changed_p (void)
11280 Lisp_Object vlist;
11282 for (vlist = Voverlay_arrow_variable_list;
11283 CONSP (vlist);
11284 vlist = XCDR (vlist))
11286 Lisp_Object var = XCAR (vlist);
11287 Lisp_Object val, pstr;
11289 if (!SYMBOLP (var))
11290 continue;
11291 val = find_symbol_value (var);
11292 if (!MARKERP (val))
11293 continue;
11294 if (! EQ (COERCE_MARKER (val),
11295 Fget (var, Qlast_arrow_position))
11296 || ! (pstr = overlay_arrow_string_or_property (var),
11297 EQ (pstr, Fget (var, Qlast_arrow_string))))
11298 return 1;
11300 return 0;
11303 /* Mark overlay arrows to be updated on next redisplay. */
11305 static void
11306 update_overlay_arrows (int up_to_date)
11308 Lisp_Object vlist;
11310 for (vlist = Voverlay_arrow_variable_list;
11311 CONSP (vlist);
11312 vlist = XCDR (vlist))
11314 Lisp_Object var = XCAR (vlist);
11316 if (!SYMBOLP (var))
11317 continue;
11319 if (up_to_date > 0)
11321 Lisp_Object val = find_symbol_value (var);
11322 Fput (var, Qlast_arrow_position,
11323 COERCE_MARKER (val));
11324 Fput (var, Qlast_arrow_string,
11325 overlay_arrow_string_or_property (var));
11327 else if (up_to_date < 0
11328 || !NILP (Fget (var, Qlast_arrow_position)))
11330 Fput (var, Qlast_arrow_position, Qt);
11331 Fput (var, Qlast_arrow_string, Qt);
11337 /* Return overlay arrow string to display at row.
11338 Return integer (bitmap number) for arrow bitmap in left fringe.
11339 Return nil if no overlay arrow. */
11341 static Lisp_Object
11342 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11344 Lisp_Object vlist;
11346 for (vlist = Voverlay_arrow_variable_list;
11347 CONSP (vlist);
11348 vlist = XCDR (vlist))
11350 Lisp_Object var = XCAR (vlist);
11351 Lisp_Object val;
11353 if (!SYMBOLP (var))
11354 continue;
11356 val = find_symbol_value (var);
11358 if (MARKERP (val)
11359 && current_buffer == XMARKER (val)->buffer
11360 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11362 if (FRAME_WINDOW_P (it->f)
11363 /* FIXME: if ROW->reversed_p is set, this should test
11364 the right fringe, not the left one. */
11365 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11367 #ifdef HAVE_WINDOW_SYSTEM
11368 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11370 int fringe_bitmap;
11371 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11372 return make_number (fringe_bitmap);
11374 #endif
11375 return make_number (-1); /* Use default arrow bitmap */
11377 return overlay_arrow_string_or_property (var);
11381 return Qnil;
11384 /* Return 1 if point moved out of or into a composition. Otherwise
11385 return 0. PREV_BUF and PREV_PT are the last point buffer and
11386 position. BUF and PT are the current point buffer and position. */
11389 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11390 struct buffer *buf, EMACS_INT pt)
11392 EMACS_INT start, end;
11393 Lisp_Object prop;
11394 Lisp_Object buffer;
11396 XSETBUFFER (buffer, buf);
11397 /* Check a composition at the last point if point moved within the
11398 same buffer. */
11399 if (prev_buf == buf)
11401 if (prev_pt == pt)
11402 /* Point didn't move. */
11403 return 0;
11405 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11406 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11407 && COMPOSITION_VALID_P (start, end, prop)
11408 && start < prev_pt && end > prev_pt)
11409 /* The last point was within the composition. Return 1 iff
11410 point moved out of the composition. */
11411 return (pt <= start || pt >= end);
11414 /* Check a composition at the current point. */
11415 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11416 && find_composition (pt, -1, &start, &end, &prop, buffer)
11417 && COMPOSITION_VALID_P (start, end, prop)
11418 && start < pt && end > pt);
11422 /* Reconsider the setting of B->clip_changed which is displayed
11423 in window W. */
11425 static INLINE void
11426 reconsider_clip_changes (struct window *w, struct buffer *b)
11428 if (b->clip_changed
11429 && !NILP (w->window_end_valid)
11430 && w->current_matrix->buffer == b
11431 && w->current_matrix->zv == BUF_ZV (b)
11432 && w->current_matrix->begv == BUF_BEGV (b))
11433 b->clip_changed = 0;
11435 /* If display wasn't paused, and W is not a tool bar window, see if
11436 point has been moved into or out of a composition. In that case,
11437 we set b->clip_changed to 1 to force updating the screen. If
11438 b->clip_changed has already been set to 1, we can skip this
11439 check. */
11440 if (!b->clip_changed
11441 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11443 EMACS_INT pt;
11445 if (w == XWINDOW (selected_window))
11446 pt = BUF_PT (current_buffer);
11447 else
11448 pt = marker_position (w->pointm);
11450 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11451 || pt != XINT (w->last_point))
11452 && check_point_in_composition (w->current_matrix->buffer,
11453 XINT (w->last_point),
11454 XBUFFER (w->buffer), pt))
11455 b->clip_changed = 1;
11460 /* Select FRAME to forward the values of frame-local variables into C
11461 variables so that the redisplay routines can access those values
11462 directly. */
11464 static void
11465 select_frame_for_redisplay (Lisp_Object frame)
11467 Lisp_Object tail, tem;
11468 Lisp_Object old = selected_frame;
11469 struct Lisp_Symbol *sym;
11471 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11473 selected_frame = frame;
11475 do {
11476 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11477 if (CONSP (XCAR (tail))
11478 && (tem = XCAR (XCAR (tail)),
11479 SYMBOLP (tem))
11480 && (sym = indirect_variable (XSYMBOL (tem)),
11481 sym->redirect == SYMBOL_LOCALIZED)
11482 && sym->val.blv->frame_local)
11483 /* Use find_symbol_value rather than Fsymbol_value
11484 to avoid an error if it is void. */
11485 find_symbol_value (tem);
11486 } while (!EQ (frame, old) && (frame = old, 1));
11490 #define STOP_POLLING \
11491 do { if (! polling_stopped_here) stop_polling (); \
11492 polling_stopped_here = 1; } while (0)
11494 #define RESUME_POLLING \
11495 do { if (polling_stopped_here) start_polling (); \
11496 polling_stopped_here = 0; } while (0)
11499 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11500 response to any user action; therefore, we should preserve the echo
11501 area. (Actually, our caller does that job.) Perhaps in the future
11502 avoid recentering windows if it is not necessary; currently that
11503 causes some problems. */
11505 static void
11506 redisplay_internal (int preserve_echo_area)
11508 struct window *w = XWINDOW (selected_window);
11509 struct frame *f;
11510 int pause;
11511 int must_finish = 0;
11512 struct text_pos tlbufpos, tlendpos;
11513 int number_of_visible_frames;
11514 int count, count1;
11515 struct frame *sf;
11516 int polling_stopped_here = 0;
11517 Lisp_Object old_frame = selected_frame;
11519 /* Non-zero means redisplay has to consider all windows on all
11520 frames. Zero means, only selected_window is considered. */
11521 int consider_all_windows_p;
11523 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11525 /* No redisplay if running in batch mode or frame is not yet fully
11526 initialized, or redisplay is explicitly turned off by setting
11527 Vinhibit_redisplay. */
11528 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11529 || !NILP (Vinhibit_redisplay))
11530 return;
11532 /* Don't examine these until after testing Vinhibit_redisplay.
11533 When Emacs is shutting down, perhaps because its connection to
11534 X has dropped, we should not look at them at all. */
11535 f = XFRAME (w->frame);
11536 sf = SELECTED_FRAME ();
11538 if (!f->glyphs_initialized_p)
11539 return;
11541 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11542 if (popup_activated ())
11543 return;
11544 #endif
11546 /* I don't think this happens but let's be paranoid. */
11547 if (redisplaying_p)
11548 return;
11550 /* Record a function that resets redisplaying_p to its old value
11551 when we leave this function. */
11552 count = SPECPDL_INDEX ();
11553 record_unwind_protect (unwind_redisplay,
11554 Fcons (make_number (redisplaying_p), selected_frame));
11555 ++redisplaying_p;
11556 specbind (Qinhibit_free_realized_faces, Qnil);
11559 Lisp_Object tail, frame;
11561 FOR_EACH_FRAME (tail, frame)
11563 struct frame *f = XFRAME (frame);
11564 f->already_hscrolled_p = 0;
11568 retry:
11569 if (!EQ (old_frame, selected_frame)
11570 && FRAME_LIVE_P (XFRAME (old_frame)))
11571 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11572 selected_frame and selected_window to be temporarily out-of-sync so
11573 when we come back here via `goto retry', we need to resync because we
11574 may need to run Elisp code (via prepare_menu_bars). */
11575 select_frame_for_redisplay (old_frame);
11577 pause = 0;
11578 reconsider_clip_changes (w, current_buffer);
11579 last_escape_glyph_frame = NULL;
11580 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11582 /* If new fonts have been loaded that make a glyph matrix adjustment
11583 necessary, do it. */
11584 if (fonts_changed_p)
11586 adjust_glyphs (NULL);
11587 ++windows_or_buffers_changed;
11588 fonts_changed_p = 0;
11591 /* If face_change_count is non-zero, init_iterator will free all
11592 realized faces, which includes the faces referenced from current
11593 matrices. So, we can't reuse current matrices in this case. */
11594 if (face_change_count)
11595 ++windows_or_buffers_changed;
11597 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11598 && FRAME_TTY (sf)->previous_frame != sf)
11600 /* Since frames on a single ASCII terminal share the same
11601 display area, displaying a different frame means redisplay
11602 the whole thing. */
11603 windows_or_buffers_changed++;
11604 SET_FRAME_GARBAGED (sf);
11605 #ifndef DOS_NT
11606 set_tty_color_mode (FRAME_TTY (sf), sf);
11607 #endif
11608 FRAME_TTY (sf)->previous_frame = sf;
11611 /* Set the visible flags for all frames. Do this before checking
11612 for resized or garbaged frames; they want to know if their frames
11613 are visible. See the comment in frame.h for
11614 FRAME_SAMPLE_VISIBILITY. */
11616 Lisp_Object tail, frame;
11618 number_of_visible_frames = 0;
11620 FOR_EACH_FRAME (tail, frame)
11622 struct frame *f = XFRAME (frame);
11624 FRAME_SAMPLE_VISIBILITY (f);
11625 if (FRAME_VISIBLE_P (f))
11626 ++number_of_visible_frames;
11627 clear_desired_matrices (f);
11631 /* Notice any pending interrupt request to change frame size. */
11632 do_pending_window_change (1);
11634 /* Clear frames marked as garbaged. */
11635 if (frame_garbaged)
11636 clear_garbaged_frames ();
11638 /* Build menubar and tool-bar items. */
11639 if (NILP (Vmemory_full))
11640 prepare_menu_bars ();
11642 if (windows_or_buffers_changed)
11643 update_mode_lines++;
11645 /* Detect case that we need to write or remove a star in the mode line. */
11646 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11648 w->update_mode_line = Qt;
11649 if (buffer_shared > 1)
11650 update_mode_lines++;
11653 /* Avoid invocation of point motion hooks by `current_column' below. */
11654 count1 = SPECPDL_INDEX ();
11655 specbind (Qinhibit_point_motion_hooks, Qt);
11657 /* If %c is in the mode line, update it if needed. */
11658 if (!NILP (w->column_number_displayed)
11659 /* This alternative quickly identifies a common case
11660 where no change is needed. */
11661 && !(PT == XFASTINT (w->last_point)
11662 && XFASTINT (w->last_modified) >= MODIFF
11663 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11664 && (XFASTINT (w->column_number_displayed)
11665 != (int) current_column ())) /* iftc */
11666 w->update_mode_line = Qt;
11668 unbind_to (count1, Qnil);
11670 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11672 /* The variable buffer_shared is set in redisplay_window and
11673 indicates that we redisplay a buffer in different windows. See
11674 there. */
11675 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11676 || cursor_type_changed);
11678 /* If specs for an arrow have changed, do thorough redisplay
11679 to ensure we remove any arrow that should no longer exist. */
11680 if (overlay_arrows_changed_p ())
11681 consider_all_windows_p = windows_or_buffers_changed = 1;
11683 /* Normally the message* functions will have already displayed and
11684 updated the echo area, but the frame may have been trashed, or
11685 the update may have been preempted, so display the echo area
11686 again here. Checking message_cleared_p captures the case that
11687 the echo area should be cleared. */
11688 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11689 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11690 || (message_cleared_p
11691 && minibuf_level == 0
11692 /* If the mini-window is currently selected, this means the
11693 echo-area doesn't show through. */
11694 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11696 int window_height_changed_p = echo_area_display (0);
11697 must_finish = 1;
11699 /* If we don't display the current message, don't clear the
11700 message_cleared_p flag, because, if we did, we wouldn't clear
11701 the echo area in the next redisplay which doesn't preserve
11702 the echo area. */
11703 if (!display_last_displayed_message_p)
11704 message_cleared_p = 0;
11706 if (fonts_changed_p)
11707 goto retry;
11708 else if (window_height_changed_p)
11710 consider_all_windows_p = 1;
11711 ++update_mode_lines;
11712 ++windows_or_buffers_changed;
11714 /* If window configuration was changed, frames may have been
11715 marked garbaged. Clear them or we will experience
11716 surprises wrt scrolling. */
11717 if (frame_garbaged)
11718 clear_garbaged_frames ();
11721 else if (EQ (selected_window, minibuf_window)
11722 && (current_buffer->clip_changed
11723 || XFASTINT (w->last_modified) < MODIFF
11724 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11725 && resize_mini_window (w, 0))
11727 /* Resized active mini-window to fit the size of what it is
11728 showing if its contents might have changed. */
11729 must_finish = 1;
11730 /* FIXME: this causes all frames to be updated, which seems unnecessary
11731 since only the current frame needs to be considered. This function needs
11732 to be rewritten with two variables, consider_all_windows and
11733 consider_all_frames. */
11734 consider_all_windows_p = 1;
11735 ++windows_or_buffers_changed;
11736 ++update_mode_lines;
11738 /* If window configuration was changed, frames may have been
11739 marked garbaged. Clear them or we will experience
11740 surprises wrt scrolling. */
11741 if (frame_garbaged)
11742 clear_garbaged_frames ();
11746 /* If showing the region, and mark has changed, we must redisplay
11747 the whole window. The assignment to this_line_start_pos prevents
11748 the optimization directly below this if-statement. */
11749 if (((!NILP (Vtransient_mark_mode)
11750 && !NILP (XBUFFER (w->buffer)->mark_active))
11751 != !NILP (w->region_showing))
11752 || (!NILP (w->region_showing)
11753 && !EQ (w->region_showing,
11754 Fmarker_position (XBUFFER (w->buffer)->mark))))
11755 CHARPOS (this_line_start_pos) = 0;
11757 /* Optimize the case that only the line containing the cursor in the
11758 selected window has changed. Variables starting with this_ are
11759 set in display_line and record information about the line
11760 containing the cursor. */
11761 tlbufpos = this_line_start_pos;
11762 tlendpos = this_line_end_pos;
11763 if (!consider_all_windows_p
11764 && CHARPOS (tlbufpos) > 0
11765 && NILP (w->update_mode_line)
11766 && !current_buffer->clip_changed
11767 && !current_buffer->prevent_redisplay_optimizations_p
11768 && FRAME_VISIBLE_P (XFRAME (w->frame))
11769 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11770 /* Make sure recorded data applies to current buffer, etc. */
11771 && this_line_buffer == current_buffer
11772 && current_buffer == XBUFFER (w->buffer)
11773 && NILP (w->force_start)
11774 && NILP (w->optional_new_start)
11775 /* Point must be on the line that we have info recorded about. */
11776 && PT >= CHARPOS (tlbufpos)
11777 && PT <= Z - CHARPOS (tlendpos)
11778 /* All text outside that line, including its final newline,
11779 must be unchanged. */
11780 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11781 CHARPOS (tlendpos)))
11783 if (CHARPOS (tlbufpos) > BEGV
11784 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11785 && (CHARPOS (tlbufpos) == ZV
11786 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11787 /* Former continuation line has disappeared by becoming empty. */
11788 goto cancel;
11789 else if (XFASTINT (w->last_modified) < MODIFF
11790 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11791 || MINI_WINDOW_P (w))
11793 /* We have to handle the case of continuation around a
11794 wide-column character (see the comment in indent.c around
11795 line 1340).
11797 For instance, in the following case:
11799 -------- Insert --------
11800 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11801 J_I_ ==> J_I_ `^^' are cursors.
11802 ^^ ^^
11803 -------- --------
11805 As we have to redraw the line above, we cannot use this
11806 optimization. */
11808 struct it it;
11809 int line_height_before = this_line_pixel_height;
11811 /* Note that start_display will handle the case that the
11812 line starting at tlbufpos is a continuation line. */
11813 start_display (&it, w, tlbufpos);
11815 /* Implementation note: It this still necessary? */
11816 if (it.current_x != this_line_start_x)
11817 goto cancel;
11819 TRACE ((stderr, "trying display optimization 1\n"));
11820 w->cursor.vpos = -1;
11821 overlay_arrow_seen = 0;
11822 it.vpos = this_line_vpos;
11823 it.current_y = this_line_y;
11824 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11825 display_line (&it);
11827 /* If line contains point, is not continued,
11828 and ends at same distance from eob as before, we win. */
11829 if (w->cursor.vpos >= 0
11830 /* Line is not continued, otherwise this_line_start_pos
11831 would have been set to 0 in display_line. */
11832 && CHARPOS (this_line_start_pos)
11833 /* Line ends as before. */
11834 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11835 /* Line has same height as before. Otherwise other lines
11836 would have to be shifted up or down. */
11837 && this_line_pixel_height == line_height_before)
11839 /* If this is not the window's last line, we must adjust
11840 the charstarts of the lines below. */
11841 if (it.current_y < it.last_visible_y)
11843 struct glyph_row *row
11844 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11845 EMACS_INT delta, delta_bytes;
11847 /* We used to distinguish between two cases here,
11848 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11849 when the line ends in a newline or the end of the
11850 buffer's accessible portion. But both cases did
11851 the same, so they were collapsed. */
11852 delta = (Z
11853 - CHARPOS (tlendpos)
11854 - MATRIX_ROW_START_CHARPOS (row));
11855 delta_bytes = (Z_BYTE
11856 - BYTEPOS (tlendpos)
11857 - MATRIX_ROW_START_BYTEPOS (row));
11859 increment_matrix_positions (w->current_matrix,
11860 this_line_vpos + 1,
11861 w->current_matrix->nrows,
11862 delta, delta_bytes);
11865 /* If this row displays text now but previously didn't,
11866 or vice versa, w->window_end_vpos may have to be
11867 adjusted. */
11868 if ((it.glyph_row - 1)->displays_text_p)
11870 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11871 XSETINT (w->window_end_vpos, this_line_vpos);
11873 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11874 && this_line_vpos > 0)
11875 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11876 w->window_end_valid = Qnil;
11878 /* Update hint: No need to try to scroll in update_window. */
11879 w->desired_matrix->no_scrolling_p = 1;
11881 #if GLYPH_DEBUG
11882 *w->desired_matrix->method = 0;
11883 debug_method_add (w, "optimization 1");
11884 #endif
11885 #ifdef HAVE_WINDOW_SYSTEM
11886 update_window_fringes (w, 0);
11887 #endif
11888 goto update;
11890 else
11891 goto cancel;
11893 else if (/* Cursor position hasn't changed. */
11894 PT == XFASTINT (w->last_point)
11895 /* Make sure the cursor was last displayed
11896 in this window. Otherwise we have to reposition it. */
11897 && 0 <= w->cursor.vpos
11898 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11900 if (!must_finish)
11902 do_pending_window_change (1);
11904 /* We used to always goto end_of_redisplay here, but this
11905 isn't enough if we have a blinking cursor. */
11906 if (w->cursor_off_p == w->last_cursor_off_p)
11907 goto end_of_redisplay;
11909 goto update;
11911 /* If highlighting the region, or if the cursor is in the echo area,
11912 then we can't just move the cursor. */
11913 else if (! (!NILP (Vtransient_mark_mode)
11914 && !NILP (current_buffer->mark_active))
11915 && (EQ (selected_window, current_buffer->last_selected_window)
11916 || highlight_nonselected_windows)
11917 && NILP (w->region_showing)
11918 && NILP (Vshow_trailing_whitespace)
11919 && !cursor_in_echo_area)
11921 struct it it;
11922 struct glyph_row *row;
11924 /* Skip from tlbufpos to PT and see where it is. Note that
11925 PT may be in invisible text. If so, we will end at the
11926 next visible position. */
11927 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11928 NULL, DEFAULT_FACE_ID);
11929 it.current_x = this_line_start_x;
11930 it.current_y = this_line_y;
11931 it.vpos = this_line_vpos;
11933 /* The call to move_it_to stops in front of PT, but
11934 moves over before-strings. */
11935 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11937 if (it.vpos == this_line_vpos
11938 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11939 row->enabled_p))
11941 xassert (this_line_vpos == it.vpos);
11942 xassert (this_line_y == it.current_y);
11943 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11944 #if GLYPH_DEBUG
11945 *w->desired_matrix->method = 0;
11946 debug_method_add (w, "optimization 3");
11947 #endif
11948 goto update;
11950 else
11951 goto cancel;
11954 cancel:
11955 /* Text changed drastically or point moved off of line. */
11956 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11959 CHARPOS (this_line_start_pos) = 0;
11960 consider_all_windows_p |= buffer_shared > 1;
11961 ++clear_face_cache_count;
11962 #ifdef HAVE_WINDOW_SYSTEM
11963 ++clear_image_cache_count;
11964 #endif
11966 /* Build desired matrices, and update the display. If
11967 consider_all_windows_p is non-zero, do it for all windows on all
11968 frames. Otherwise do it for selected_window, only. */
11970 if (consider_all_windows_p)
11972 Lisp_Object tail, frame;
11974 FOR_EACH_FRAME (tail, frame)
11975 XFRAME (frame)->updated_p = 0;
11977 /* Recompute # windows showing selected buffer. This will be
11978 incremented each time such a window is displayed. */
11979 buffer_shared = 0;
11981 FOR_EACH_FRAME (tail, frame)
11983 struct frame *f = XFRAME (frame);
11985 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11987 if (! EQ (frame, selected_frame))
11988 /* Select the frame, for the sake of frame-local
11989 variables. */
11990 select_frame_for_redisplay (frame);
11992 /* Mark all the scroll bars to be removed; we'll redeem
11993 the ones we want when we redisplay their windows. */
11994 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11995 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11997 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11998 redisplay_windows (FRAME_ROOT_WINDOW (f));
12000 /* The X error handler may have deleted that frame. */
12001 if (!FRAME_LIVE_P (f))
12002 continue;
12004 /* Any scroll bars which redisplay_windows should have
12005 nuked should now go away. */
12006 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12007 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12009 /* If fonts changed, display again. */
12010 /* ??? rms: I suspect it is a mistake to jump all the way
12011 back to retry here. It should just retry this frame. */
12012 if (fonts_changed_p)
12013 goto retry;
12015 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12017 /* See if we have to hscroll. */
12018 if (!f->already_hscrolled_p)
12020 f->already_hscrolled_p = 1;
12021 if (hscroll_windows (f->root_window))
12022 goto retry;
12025 /* Prevent various kinds of signals during display
12026 update. stdio is not robust about handling
12027 signals, which can cause an apparent I/O
12028 error. */
12029 if (interrupt_input)
12030 unrequest_sigio ();
12031 STOP_POLLING;
12033 /* Update the display. */
12034 set_window_update_flags (XWINDOW (f->root_window), 1);
12035 pause |= update_frame (f, 0, 0);
12036 f->updated_p = 1;
12041 if (!EQ (old_frame, selected_frame)
12042 && FRAME_LIVE_P (XFRAME (old_frame)))
12043 /* We played a bit fast-and-loose above and allowed selected_frame
12044 and selected_window to be temporarily out-of-sync but let's make
12045 sure this stays contained. */
12046 select_frame_for_redisplay (old_frame);
12047 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12049 if (!pause)
12051 /* Do the mark_window_display_accurate after all windows have
12052 been redisplayed because this call resets flags in buffers
12053 which are needed for proper redisplay. */
12054 FOR_EACH_FRAME (tail, frame)
12056 struct frame *f = XFRAME (frame);
12057 if (f->updated_p)
12059 mark_window_display_accurate (f->root_window, 1);
12060 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12061 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12066 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12068 Lisp_Object mini_window;
12069 struct frame *mini_frame;
12071 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12072 /* Use list_of_error, not Qerror, so that
12073 we catch only errors and don't run the debugger. */
12074 internal_condition_case_1 (redisplay_window_1, selected_window,
12075 list_of_error,
12076 redisplay_window_error);
12078 /* Compare desired and current matrices, perform output. */
12080 update:
12081 /* If fonts changed, display again. */
12082 if (fonts_changed_p)
12083 goto retry;
12085 /* Prevent various kinds of signals during display update.
12086 stdio is not robust about handling signals,
12087 which can cause an apparent I/O error. */
12088 if (interrupt_input)
12089 unrequest_sigio ();
12090 STOP_POLLING;
12092 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12094 if (hscroll_windows (selected_window))
12095 goto retry;
12097 XWINDOW (selected_window)->must_be_updated_p = 1;
12098 pause = update_frame (sf, 0, 0);
12101 /* We may have called echo_area_display at the top of this
12102 function. If the echo area is on another frame, that may
12103 have put text on a frame other than the selected one, so the
12104 above call to update_frame would not have caught it. Catch
12105 it here. */
12106 mini_window = FRAME_MINIBUF_WINDOW (sf);
12107 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12109 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12111 XWINDOW (mini_window)->must_be_updated_p = 1;
12112 pause |= update_frame (mini_frame, 0, 0);
12113 if (!pause && hscroll_windows (mini_window))
12114 goto retry;
12118 /* If display was paused because of pending input, make sure we do a
12119 thorough update the next time. */
12120 if (pause)
12122 /* Prevent the optimization at the beginning of
12123 redisplay_internal that tries a single-line update of the
12124 line containing the cursor in the selected window. */
12125 CHARPOS (this_line_start_pos) = 0;
12127 /* Let the overlay arrow be updated the next time. */
12128 update_overlay_arrows (0);
12130 /* If we pause after scrolling, some rows in the current
12131 matrices of some windows are not valid. */
12132 if (!WINDOW_FULL_WIDTH_P (w)
12133 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12134 update_mode_lines = 1;
12136 else
12138 if (!consider_all_windows_p)
12140 /* This has already been done above if
12141 consider_all_windows_p is set. */
12142 mark_window_display_accurate_1 (w, 1);
12144 /* Say overlay arrows are up to date. */
12145 update_overlay_arrows (1);
12147 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12148 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12151 update_mode_lines = 0;
12152 windows_or_buffers_changed = 0;
12153 cursor_type_changed = 0;
12156 /* Start SIGIO interrupts coming again. Having them off during the
12157 code above makes it less likely one will discard output, but not
12158 impossible, since there might be stuff in the system buffer here.
12159 But it is much hairier to try to do anything about that. */
12160 if (interrupt_input)
12161 request_sigio ();
12162 RESUME_POLLING;
12164 /* If a frame has become visible which was not before, redisplay
12165 again, so that we display it. Expose events for such a frame
12166 (which it gets when becoming visible) don't call the parts of
12167 redisplay constructing glyphs, so simply exposing a frame won't
12168 display anything in this case. So, we have to display these
12169 frames here explicitly. */
12170 if (!pause)
12172 Lisp_Object tail, frame;
12173 int new_count = 0;
12175 FOR_EACH_FRAME (tail, frame)
12177 int this_is_visible = 0;
12179 if (XFRAME (frame)->visible)
12180 this_is_visible = 1;
12181 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12182 if (XFRAME (frame)->visible)
12183 this_is_visible = 1;
12185 if (this_is_visible)
12186 new_count++;
12189 if (new_count != number_of_visible_frames)
12190 windows_or_buffers_changed++;
12193 /* Change frame size now if a change is pending. */
12194 do_pending_window_change (1);
12196 /* If we just did a pending size change, or have additional
12197 visible frames, redisplay again. */
12198 if (windows_or_buffers_changed && !pause)
12199 goto retry;
12201 /* Clear the face and image caches.
12203 We used to do this only if consider_all_windows_p. But the cache
12204 needs to be cleared if a timer creates images in the current
12205 buffer (e.g. the test case in Bug#6230). */
12207 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12209 clear_face_cache (0);
12210 clear_face_cache_count = 0;
12213 #ifdef HAVE_WINDOW_SYSTEM
12214 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12216 clear_image_caches (Qnil);
12217 clear_image_cache_count = 0;
12219 #endif /* HAVE_WINDOW_SYSTEM */
12221 end_of_redisplay:
12222 unbind_to (count, Qnil);
12223 RESUME_POLLING;
12227 /* Redisplay, but leave alone any recent echo area message unless
12228 another message has been requested in its place.
12230 This is useful in situations where you need to redisplay but no
12231 user action has occurred, making it inappropriate for the message
12232 area to be cleared. See tracking_off and
12233 wait_reading_process_output for examples of these situations.
12235 FROM_WHERE is an integer saying from where this function was
12236 called. This is useful for debugging. */
12238 void
12239 redisplay_preserve_echo_area (int from_where)
12241 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12243 if (!NILP (echo_area_buffer[1]))
12245 /* We have a previously displayed message, but no current
12246 message. Redisplay the previous message. */
12247 display_last_displayed_message_p = 1;
12248 redisplay_internal (1);
12249 display_last_displayed_message_p = 0;
12251 else
12252 redisplay_internal (1);
12254 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12255 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12256 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12260 /* Function registered with record_unwind_protect in
12261 redisplay_internal. Reset redisplaying_p to the value it had
12262 before redisplay_internal was called, and clear
12263 prevent_freeing_realized_faces_p. It also selects the previously
12264 selected frame, unless it has been deleted (by an X connection
12265 failure during redisplay, for example). */
12267 static Lisp_Object
12268 unwind_redisplay (Lisp_Object val)
12270 Lisp_Object old_redisplaying_p, old_frame;
12272 old_redisplaying_p = XCAR (val);
12273 redisplaying_p = XFASTINT (old_redisplaying_p);
12274 old_frame = XCDR (val);
12275 if (! EQ (old_frame, selected_frame)
12276 && FRAME_LIVE_P (XFRAME (old_frame)))
12277 select_frame_for_redisplay (old_frame);
12278 return Qnil;
12282 /* Mark the display of window W as accurate or inaccurate. If
12283 ACCURATE_P is non-zero mark display of W as accurate. If
12284 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12285 redisplay_internal is called. */
12287 static void
12288 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12290 if (BUFFERP (w->buffer))
12292 struct buffer *b = XBUFFER (w->buffer);
12294 w->last_modified
12295 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12296 w->last_overlay_modified
12297 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12298 w->last_had_star
12299 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12301 if (accurate_p)
12303 b->clip_changed = 0;
12304 b->prevent_redisplay_optimizations_p = 0;
12306 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12307 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12308 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12309 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12311 w->current_matrix->buffer = b;
12312 w->current_matrix->begv = BUF_BEGV (b);
12313 w->current_matrix->zv = BUF_ZV (b);
12315 w->last_cursor = w->cursor;
12316 w->last_cursor_off_p = w->cursor_off_p;
12318 if (w == XWINDOW (selected_window))
12319 w->last_point = make_number (BUF_PT (b));
12320 else
12321 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12325 if (accurate_p)
12327 w->window_end_valid = w->buffer;
12328 w->update_mode_line = Qnil;
12333 /* Mark the display of windows in the window tree rooted at WINDOW as
12334 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12335 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12336 be redisplayed the next time redisplay_internal is called. */
12338 void
12339 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12341 struct window *w;
12343 for (; !NILP (window); window = w->next)
12345 w = XWINDOW (window);
12346 mark_window_display_accurate_1 (w, accurate_p);
12348 if (!NILP (w->vchild))
12349 mark_window_display_accurate (w->vchild, accurate_p);
12350 if (!NILP (w->hchild))
12351 mark_window_display_accurate (w->hchild, accurate_p);
12354 if (accurate_p)
12356 update_overlay_arrows (1);
12358 else
12360 /* Force a thorough redisplay the next time by setting
12361 last_arrow_position and last_arrow_string to t, which is
12362 unequal to any useful value of Voverlay_arrow_... */
12363 update_overlay_arrows (-1);
12368 /* Return value in display table DP (Lisp_Char_Table *) for character
12369 C. Since a display table doesn't have any parent, we don't have to
12370 follow parent. Do not call this function directly but use the
12371 macro DISP_CHAR_VECTOR. */
12373 Lisp_Object
12374 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12376 Lisp_Object val;
12378 if (ASCII_CHAR_P (c))
12380 val = dp->ascii;
12381 if (SUB_CHAR_TABLE_P (val))
12382 val = XSUB_CHAR_TABLE (val)->contents[c];
12384 else
12386 Lisp_Object table;
12388 XSETCHAR_TABLE (table, dp);
12389 val = char_table_ref (table, c);
12391 if (NILP (val))
12392 val = dp->defalt;
12393 return val;
12398 /***********************************************************************
12399 Window Redisplay
12400 ***********************************************************************/
12402 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12404 static void
12405 redisplay_windows (Lisp_Object window)
12407 while (!NILP (window))
12409 struct window *w = XWINDOW (window);
12411 if (!NILP (w->hchild))
12412 redisplay_windows (w->hchild);
12413 else if (!NILP (w->vchild))
12414 redisplay_windows (w->vchild);
12415 else if (!NILP (w->buffer))
12417 displayed_buffer = XBUFFER (w->buffer);
12418 /* Use list_of_error, not Qerror, so that
12419 we catch only errors and don't run the debugger. */
12420 internal_condition_case_1 (redisplay_window_0, window,
12421 list_of_error,
12422 redisplay_window_error);
12425 window = w->next;
12429 static Lisp_Object
12430 redisplay_window_error (Lisp_Object ignore)
12432 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12433 return Qnil;
12436 static Lisp_Object
12437 redisplay_window_0 (Lisp_Object window)
12439 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12440 redisplay_window (window, 0);
12441 return Qnil;
12444 static Lisp_Object
12445 redisplay_window_1 (Lisp_Object window)
12447 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12448 redisplay_window (window, 1);
12449 return Qnil;
12453 /* Increment GLYPH until it reaches END or CONDITION fails while
12454 adding (GLYPH)->pixel_width to X. */
12456 #define SKIP_GLYPHS(glyph, end, x, condition) \
12457 do \
12459 (x) += (glyph)->pixel_width; \
12460 ++(glyph); \
12462 while ((glyph) < (end) && (condition))
12465 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12466 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12467 which positions recorded in ROW differ from current buffer
12468 positions.
12470 Return 0 if cursor is not on this row, 1 otherwise. */
12473 set_cursor_from_row (struct window *w, struct glyph_row *row,
12474 struct glyph_matrix *matrix,
12475 EMACS_INT delta, EMACS_INT delta_bytes,
12476 int dy, int dvpos)
12478 struct glyph *glyph = row->glyphs[TEXT_AREA];
12479 struct glyph *end = glyph + row->used[TEXT_AREA];
12480 struct glyph *cursor = NULL;
12481 /* The last known character position in row. */
12482 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12483 int x = row->x;
12484 EMACS_INT pt_old = PT - delta;
12485 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12486 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12487 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12488 /* A glyph beyond the edge of TEXT_AREA which we should never
12489 touch. */
12490 struct glyph *glyphs_end = end;
12491 /* Non-zero means we've found a match for cursor position, but that
12492 glyph has the avoid_cursor_p flag set. */
12493 int match_with_avoid_cursor = 0;
12494 /* Non-zero means we've seen at least one glyph that came from a
12495 display string. */
12496 int string_seen = 0;
12497 /* Largest and smalles buffer positions seen so far during scan of
12498 glyph row. */
12499 EMACS_INT bpos_max = pos_before;
12500 EMACS_INT bpos_min = pos_after;
12501 /* Last buffer position covered by an overlay string with an integer
12502 `cursor' property. */
12503 EMACS_INT bpos_covered = 0;
12505 /* Skip over glyphs not having an object at the start and the end of
12506 the row. These are special glyphs like truncation marks on
12507 terminal frames. */
12508 if (row->displays_text_p)
12510 if (!row->reversed_p)
12512 while (glyph < end
12513 && INTEGERP (glyph->object)
12514 && glyph->charpos < 0)
12516 x += glyph->pixel_width;
12517 ++glyph;
12519 while (end > glyph
12520 && INTEGERP ((end - 1)->object)
12521 /* CHARPOS is zero for blanks and stretch glyphs
12522 inserted by extend_face_to_end_of_line. */
12523 && (end - 1)->charpos <= 0)
12524 --end;
12525 glyph_before = glyph - 1;
12526 glyph_after = end;
12528 else
12530 struct glyph *g;
12532 /* If the glyph row is reversed, we need to process it from back
12533 to front, so swap the edge pointers. */
12534 glyphs_end = end = glyph - 1;
12535 glyph += row->used[TEXT_AREA] - 1;
12537 while (glyph > end + 1
12538 && INTEGERP (glyph->object)
12539 && glyph->charpos < 0)
12541 --glyph;
12542 x -= glyph->pixel_width;
12544 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12545 --glyph;
12546 /* By default, in reversed rows we put the cursor on the
12547 rightmost (first in the reading order) glyph. */
12548 for (g = end + 1; g < glyph; g++)
12549 x += g->pixel_width;
12550 while (end < glyph
12551 && INTEGERP ((end + 1)->object)
12552 && (end + 1)->charpos <= 0)
12553 ++end;
12554 glyph_before = glyph + 1;
12555 glyph_after = end;
12558 else if (row->reversed_p)
12560 /* In R2L rows that don't display text, put the cursor on the
12561 rightmost glyph. Case in point: an empty last line that is
12562 part of an R2L paragraph. */
12563 cursor = end - 1;
12564 /* Avoid placing the cursor on the last glyph of the row, where
12565 on terminal frames we hold the vertical border between
12566 adjacent windows. */
12567 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12568 && !WINDOW_RIGHTMOST_P (w)
12569 && cursor == row->glyphs[LAST_AREA] - 1)
12570 cursor--;
12571 x = -1; /* will be computed below, at label compute_x */
12574 /* Step 1: Try to find the glyph whose character position
12575 corresponds to point. If that's not possible, find 2 glyphs
12576 whose character positions are the closest to point, one before
12577 point, the other after it. */
12578 if (!row->reversed_p)
12579 while (/* not marched to end of glyph row */
12580 glyph < end
12581 /* glyph was not inserted by redisplay for internal purposes */
12582 && !INTEGERP (glyph->object))
12584 if (BUFFERP (glyph->object))
12586 EMACS_INT dpos = glyph->charpos - pt_old;
12588 if (glyph->charpos > bpos_max)
12589 bpos_max = glyph->charpos;
12590 if (glyph->charpos < bpos_min)
12591 bpos_min = glyph->charpos;
12592 if (!glyph->avoid_cursor_p)
12594 /* If we hit point, we've found the glyph on which to
12595 display the cursor. */
12596 if (dpos == 0)
12598 match_with_avoid_cursor = 0;
12599 break;
12601 /* See if we've found a better approximation to
12602 POS_BEFORE or to POS_AFTER. Note that we want the
12603 first (leftmost) glyph of all those that are the
12604 closest from below, and the last (rightmost) of all
12605 those from above. */
12606 if (0 > dpos && dpos > pos_before - pt_old)
12608 pos_before = glyph->charpos;
12609 glyph_before = glyph;
12611 else if (0 < dpos && dpos <= pos_after - pt_old)
12613 pos_after = glyph->charpos;
12614 glyph_after = glyph;
12617 else if (dpos == 0)
12618 match_with_avoid_cursor = 1;
12620 else if (STRINGP (glyph->object))
12622 Lisp_Object chprop;
12623 EMACS_INT glyph_pos = glyph->charpos;
12625 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12626 glyph->object);
12627 if (INTEGERP (chprop))
12629 bpos_covered = bpos_max + XINT (chprop);
12630 /* If the `cursor' property covers buffer positions up
12631 to and including point, we should display cursor on
12632 this glyph. Note that overlays and text properties
12633 with string values stop bidi reordering, so every
12634 buffer position to the left of the string is always
12635 smaller than any position to the right of the
12636 string. Therefore, if a `cursor' property on one
12637 of the string's characters has an integer value, we
12638 will break out of the loop below _before_ we get to
12639 the position match above. IOW, integer values of
12640 the `cursor' property override the "exact match for
12641 point" strategy of positioning the cursor. */
12642 /* Implementation note: bpos_max == pt_old when, e.g.,
12643 we are in an empty line, where bpos_max is set to
12644 MATRIX_ROW_START_CHARPOS, see above. */
12645 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12647 cursor = glyph;
12648 break;
12652 string_seen = 1;
12654 x += glyph->pixel_width;
12655 ++glyph;
12657 else if (glyph > end) /* row is reversed */
12658 while (!INTEGERP (glyph->object))
12660 if (BUFFERP (glyph->object))
12662 EMACS_INT dpos = glyph->charpos - pt_old;
12664 if (glyph->charpos > bpos_max)
12665 bpos_max = glyph->charpos;
12666 if (glyph->charpos < bpos_min)
12667 bpos_min = glyph->charpos;
12668 if (!glyph->avoid_cursor_p)
12670 if (dpos == 0)
12672 match_with_avoid_cursor = 0;
12673 break;
12675 if (0 > dpos && dpos > pos_before - pt_old)
12677 pos_before = glyph->charpos;
12678 glyph_before = glyph;
12680 else if (0 < dpos && dpos <= pos_after - pt_old)
12682 pos_after = glyph->charpos;
12683 glyph_after = glyph;
12686 else if (dpos == 0)
12687 match_with_avoid_cursor = 1;
12689 else if (STRINGP (glyph->object))
12691 Lisp_Object chprop;
12692 EMACS_INT glyph_pos = glyph->charpos;
12694 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12695 glyph->object);
12696 if (INTEGERP (chprop))
12698 bpos_covered = bpos_max + XINT (chprop);
12699 /* If the `cursor' property covers buffer positions up
12700 to and including point, we should display cursor on
12701 this glyph. */
12702 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12704 cursor = glyph;
12705 break;
12708 string_seen = 1;
12710 --glyph;
12711 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12713 x--; /* can't use any pixel_width */
12714 break;
12716 x -= glyph->pixel_width;
12719 /* Step 2: If we didn't find an exact match for point, we need to
12720 look for a proper place to put the cursor among glyphs between
12721 GLYPH_BEFORE and GLYPH_AFTER. */
12722 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12723 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12724 && bpos_covered < pt_old)
12726 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12728 EMACS_INT ellipsis_pos;
12730 /* Scan back over the ellipsis glyphs. */
12731 if (!row->reversed_p)
12733 ellipsis_pos = (glyph - 1)->charpos;
12734 while (glyph > row->glyphs[TEXT_AREA]
12735 && (glyph - 1)->charpos == ellipsis_pos)
12736 glyph--, x -= glyph->pixel_width;
12737 /* That loop always goes one position too far, including
12738 the glyph before the ellipsis. So scan forward over
12739 that one. */
12740 x += glyph->pixel_width;
12741 glyph++;
12743 else /* row is reversed */
12745 ellipsis_pos = (glyph + 1)->charpos;
12746 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12747 && (glyph + 1)->charpos == ellipsis_pos)
12748 glyph++, x += glyph->pixel_width;
12749 x -= glyph->pixel_width;
12750 glyph--;
12753 else if (match_with_avoid_cursor
12754 /* A truncated row may not include PT among its
12755 character positions. Setting the cursor inside the
12756 scroll margin will trigger recalculation of hscroll
12757 in hscroll_window_tree. */
12758 || (row->truncated_on_left_p && pt_old < bpos_min)
12759 || (row->truncated_on_right_p && pt_old > bpos_max)
12760 /* Zero-width characters produce no glyphs. */
12761 || ((row->reversed_p
12762 ? glyph_after > glyphs_end
12763 : glyph_after < glyphs_end)
12764 && eabs (glyph_after - glyph_before) == 1))
12766 cursor = glyph_after;
12767 x = -1;
12769 else if (string_seen)
12771 int incr = row->reversed_p ? -1 : +1;
12773 /* Need to find the glyph that came out of a string which is
12774 present at point. That glyph is somewhere between
12775 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12776 positioned between POS_BEFORE and POS_AFTER in the
12777 buffer. */
12778 struct glyph *stop = glyph_after;
12779 EMACS_INT pos = pos_before;
12781 x = -1;
12782 for (glyph = glyph_before + incr;
12783 row->reversed_p ? glyph > stop : glyph < stop; )
12786 /* Any glyphs that come from the buffer are here because
12787 of bidi reordering. Skip them, and only pay
12788 attention to glyphs that came from some string. */
12789 if (STRINGP (glyph->object))
12791 Lisp_Object str;
12792 EMACS_INT tem;
12794 str = glyph->object;
12795 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12796 if (tem == 0 /* from overlay */
12797 || pos <= tem)
12799 /* If the string from which this glyph came is
12800 found in the buffer at point, then we've
12801 found the glyph we've been looking for. If
12802 it comes from an overlay (tem == 0), and it
12803 has the `cursor' property on one of its
12804 glyphs, record that glyph as a candidate for
12805 displaying the cursor. (As in the
12806 unidirectional version, we will display the
12807 cursor on the last candidate we find.) */
12808 if (tem == 0 || tem == pt_old)
12810 /* The glyphs from this string could have
12811 been reordered. Find the one with the
12812 smallest string position. Or there could
12813 be a character in the string with the
12814 `cursor' property, which means display
12815 cursor on that character's glyph. */
12816 EMACS_INT strpos = glyph->charpos;
12818 cursor = glyph;
12819 for (glyph += incr;
12820 (row->reversed_p ? glyph > stop : glyph < stop)
12821 && EQ (glyph->object, str);
12822 glyph += incr)
12824 Lisp_Object cprop;
12825 EMACS_INT gpos = glyph->charpos;
12827 cprop = Fget_char_property (make_number (gpos),
12828 Qcursor,
12829 glyph->object);
12830 if (!NILP (cprop))
12832 cursor = glyph;
12833 break;
12835 if (glyph->charpos < strpos)
12837 strpos = glyph->charpos;
12838 cursor = glyph;
12842 if (tem == pt_old)
12843 goto compute_x;
12845 if (tem)
12846 pos = tem + 1; /* don't find previous instances */
12848 /* This string is not what we want; skip all of the
12849 glyphs that came from it. */
12851 glyph += incr;
12852 while ((row->reversed_p ? glyph > stop : glyph < stop)
12853 && EQ (glyph->object, str));
12855 else
12856 glyph += incr;
12859 /* If we reached the end of the line, and END was from a string,
12860 the cursor is not on this line. */
12861 if (cursor == NULL
12862 && (row->reversed_p ? glyph <= end : glyph >= end)
12863 && STRINGP (end->object)
12864 && row->continued_p)
12865 return 0;
12869 compute_x:
12870 if (cursor != NULL)
12871 glyph = cursor;
12872 if (x < 0)
12874 struct glyph *g;
12876 /* Need to compute x that corresponds to GLYPH. */
12877 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12879 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12880 abort ();
12881 x += g->pixel_width;
12885 /* ROW could be part of a continued line, which, under bidi
12886 reordering, might have other rows whose start and end charpos
12887 occlude point. Only set w->cursor if we found a better
12888 approximation to the cursor position than we have from previously
12889 examined candidate rows belonging to the same continued line. */
12890 if (/* we already have a candidate row */
12891 w->cursor.vpos >= 0
12892 /* that candidate is not the row we are processing */
12893 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12894 /* the row we are processing is part of a continued line */
12895 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12896 /* Make sure cursor.vpos specifies a row whose start and end
12897 charpos occlude point. This is because some callers of this
12898 function leave cursor.vpos at the row where the cursor was
12899 displayed during the last redisplay cycle. */
12900 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12901 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12903 struct glyph *g1 =
12904 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12906 /* Don't consider glyphs that are outside TEXT_AREA. */
12907 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12908 return 0;
12909 /* Keep the candidate whose buffer position is the closest to
12910 point. */
12911 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12912 w->cursor.hpos >= 0
12913 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12914 && BUFFERP (g1->object)
12915 && (g1->charpos == pt_old /* an exact match always wins */
12916 || (BUFFERP (glyph->object)
12917 && eabs (g1->charpos - pt_old)
12918 < eabs (glyph->charpos - pt_old))))
12919 return 0;
12920 /* If this candidate gives an exact match, use that. */
12921 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12922 /* Otherwise, keep the candidate that comes from a row
12923 spanning less buffer positions. This may win when one or
12924 both candidate positions are on glyphs that came from
12925 display strings, for which we cannot compare buffer
12926 positions. */
12927 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12928 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12929 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12930 return 0;
12932 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12933 w->cursor.x = x;
12934 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12935 w->cursor.y = row->y + dy;
12937 if (w == XWINDOW (selected_window))
12939 if (!row->continued_p
12940 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12941 && row->x == 0)
12943 this_line_buffer = XBUFFER (w->buffer);
12945 CHARPOS (this_line_start_pos)
12946 = MATRIX_ROW_START_CHARPOS (row) + delta;
12947 BYTEPOS (this_line_start_pos)
12948 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12950 CHARPOS (this_line_end_pos)
12951 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12952 BYTEPOS (this_line_end_pos)
12953 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12955 this_line_y = w->cursor.y;
12956 this_line_pixel_height = row->height;
12957 this_line_vpos = w->cursor.vpos;
12958 this_line_start_x = row->x;
12960 else
12961 CHARPOS (this_line_start_pos) = 0;
12964 return 1;
12968 /* Run window scroll functions, if any, for WINDOW with new window
12969 start STARTP. Sets the window start of WINDOW to that position.
12971 We assume that the window's buffer is really current. */
12973 static INLINE struct text_pos
12974 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
12976 struct window *w = XWINDOW (window);
12977 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12979 if (current_buffer != XBUFFER (w->buffer))
12980 abort ();
12982 if (!NILP (Vwindow_scroll_functions))
12984 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12985 make_number (CHARPOS (startp)));
12986 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12987 /* In case the hook functions switch buffers. */
12988 if (current_buffer != XBUFFER (w->buffer))
12989 set_buffer_internal_1 (XBUFFER (w->buffer));
12992 return startp;
12996 /* Make sure the line containing the cursor is fully visible.
12997 A value of 1 means there is nothing to be done.
12998 (Either the line is fully visible, or it cannot be made so,
12999 or we cannot tell.)
13001 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13002 is higher than window.
13004 A value of 0 means the caller should do scrolling
13005 as if point had gone off the screen. */
13007 static int
13008 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13010 struct glyph_matrix *matrix;
13011 struct glyph_row *row;
13012 int window_height;
13014 if (!make_cursor_line_fully_visible_p)
13015 return 1;
13017 /* It's not always possible to find the cursor, e.g, when a window
13018 is full of overlay strings. Don't do anything in that case. */
13019 if (w->cursor.vpos < 0)
13020 return 1;
13022 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13023 row = MATRIX_ROW (matrix, w->cursor.vpos);
13025 /* If the cursor row is not partially visible, there's nothing to do. */
13026 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13027 return 1;
13029 /* If the row the cursor is in is taller than the window's height,
13030 it's not clear what to do, so do nothing. */
13031 window_height = window_box_height (w);
13032 if (row->height >= window_height)
13034 if (!force_p || MINI_WINDOW_P (w)
13035 || w->vscroll || w->cursor.vpos == 0)
13036 return 1;
13038 return 0;
13042 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13043 non-zero means only WINDOW is redisplayed in redisplay_internal.
13044 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13045 in redisplay_window to bring a partially visible line into view in
13046 the case that only the cursor has moved.
13048 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13049 last screen line's vertical height extends past the end of the screen.
13051 Value is
13053 1 if scrolling succeeded
13055 0 if scrolling didn't find point.
13057 -1 if new fonts have been loaded so that we must interrupt
13058 redisplay, adjust glyph matrices, and try again. */
13060 enum
13062 SCROLLING_SUCCESS,
13063 SCROLLING_FAILED,
13064 SCROLLING_NEED_LARGER_MATRICES
13067 static int
13068 try_scrolling (Lisp_Object window, int just_this_one_p,
13069 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13070 int temp_scroll_step, int last_line_misfit)
13072 struct window *w = XWINDOW (window);
13073 struct frame *f = XFRAME (w->frame);
13074 struct text_pos pos, startp;
13075 struct it it;
13076 int this_scroll_margin, scroll_max, rc, height;
13077 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13078 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13079 Lisp_Object aggressive;
13080 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13082 #if GLYPH_DEBUG
13083 debug_method_add (w, "try_scrolling");
13084 #endif
13086 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13088 /* Compute scroll margin height in pixels. We scroll when point is
13089 within this distance from the top or bottom of the window. */
13090 if (scroll_margin > 0)
13091 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13092 * FRAME_LINE_HEIGHT (f);
13093 else
13094 this_scroll_margin = 0;
13096 /* Force scroll_conservatively to have a reasonable value, to avoid
13097 overflow while computing how much to scroll. Note that the user
13098 can supply scroll-conservatively equal to `most-positive-fixnum',
13099 which can be larger than INT_MAX. */
13100 if (scroll_conservatively > scroll_limit)
13102 scroll_conservatively = scroll_limit;
13103 scroll_max = INT_MAX;
13105 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13106 /* Compute how much we should try to scroll maximally to bring
13107 point into view. */
13108 scroll_max = (max (scroll_step,
13109 max (scroll_conservatively, temp_scroll_step))
13110 * FRAME_LINE_HEIGHT (f));
13111 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13112 || NUMBERP (current_buffer->scroll_up_aggressively))
13113 /* We're trying to scroll because of aggressive scrolling but no
13114 scroll_step is set. Choose an arbitrary one. */
13115 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13116 else
13117 scroll_max = 0;
13119 too_near_end:
13121 /* Decide whether to scroll down. */
13122 if (PT > CHARPOS (startp))
13124 int scroll_margin_y;
13126 /* Compute the pixel ypos of the scroll margin, then move it to
13127 either that ypos or PT, whichever comes first. */
13128 start_display (&it, w, startp);
13129 scroll_margin_y = it.last_visible_y - this_scroll_margin
13130 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13131 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13132 (MOVE_TO_POS | MOVE_TO_Y));
13134 if (PT > CHARPOS (it.current.pos))
13136 int y0 = line_bottom_y (&it);
13137 /* Compute how many pixels below window bottom to stop searching
13138 for PT. This avoids costly search for PT that is far away if
13139 the user limited scrolling by a small number of lines, but
13140 always finds PT if scroll_conservatively is set to a large
13141 number, such as most-positive-fixnum. */
13142 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13143 int y_to_move =
13144 slack >= INT_MAX - it.last_visible_y
13145 ? INT_MAX
13146 : it.last_visible_y + slack;
13148 /* Compute the distance from the scroll margin to PT or to
13149 the scroll limit, whichever comes first. This should
13150 include the height of the cursor line, to make that line
13151 fully visible. */
13152 move_it_to (&it, PT, -1, y_to_move,
13153 -1, MOVE_TO_POS | MOVE_TO_Y);
13154 dy = line_bottom_y (&it) - y0;
13156 if (dy > scroll_max)
13157 return SCROLLING_FAILED;
13159 scroll_down_p = 1;
13163 if (scroll_down_p)
13165 /* Point is in or below the bottom scroll margin, so move the
13166 window start down. If scrolling conservatively, move it just
13167 enough down to make point visible. If scroll_step is set,
13168 move it down by scroll_step. */
13169 if (scroll_conservatively)
13170 amount_to_scroll
13171 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13172 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13173 else if (scroll_step || temp_scroll_step)
13174 amount_to_scroll = scroll_max;
13175 else
13177 aggressive = current_buffer->scroll_up_aggressively;
13178 height = WINDOW_BOX_TEXT_HEIGHT (w);
13179 if (NUMBERP (aggressive))
13181 double float_amount = XFLOATINT (aggressive) * height;
13182 amount_to_scroll = float_amount;
13183 if (amount_to_scroll == 0 && float_amount > 0)
13184 amount_to_scroll = 1;
13188 if (amount_to_scroll <= 0)
13189 return SCROLLING_FAILED;
13191 start_display (&it, w, startp);
13192 if (scroll_max < INT_MAX)
13193 move_it_vertically (&it, amount_to_scroll);
13194 else
13196 /* Extra precision for users who set scroll-conservatively
13197 to most-positive-fixnum: make sure the amount we scroll
13198 the window start is never less than amount_to_scroll,
13199 which was computed as distance from window bottom to
13200 point. This matters when lines at window top and lines
13201 below window bottom have different height. */
13202 struct it it1 = it;
13203 /* We use a temporary it1 because line_bottom_y can modify
13204 its argument, if it moves one line down; see there. */
13205 int start_y = line_bottom_y (&it1);
13207 do {
13208 move_it_by_lines (&it, 1, 1);
13209 it1 = it;
13210 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13213 /* If STARTP is unchanged, move it down another screen line. */
13214 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13215 move_it_by_lines (&it, 1, 1);
13216 startp = it.current.pos;
13218 else
13220 struct text_pos scroll_margin_pos = startp;
13222 /* See if point is inside the scroll margin at the top of the
13223 window. */
13224 if (this_scroll_margin)
13226 start_display (&it, w, startp);
13227 move_it_vertically (&it, this_scroll_margin);
13228 scroll_margin_pos = it.current.pos;
13231 if (PT < CHARPOS (scroll_margin_pos))
13233 /* Point is in the scroll margin at the top of the window or
13234 above what is displayed in the window. */
13235 int y0;
13237 /* Compute the vertical distance from PT to the scroll
13238 margin position. Give up if distance is greater than
13239 scroll_max. */
13240 SET_TEXT_POS (pos, PT, PT_BYTE);
13241 start_display (&it, w, pos);
13242 y0 = it.current_y;
13243 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13244 it.last_visible_y, -1,
13245 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13246 dy = it.current_y - y0;
13247 if (dy > scroll_max)
13248 return SCROLLING_FAILED;
13250 /* Compute new window start. */
13251 start_display (&it, w, startp);
13253 if (scroll_conservatively)
13254 amount_to_scroll
13255 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13256 else if (scroll_step || temp_scroll_step)
13257 amount_to_scroll = scroll_max;
13258 else
13260 aggressive = current_buffer->scroll_down_aggressively;
13261 height = WINDOW_BOX_TEXT_HEIGHT (w);
13262 if (NUMBERP (aggressive))
13264 double float_amount = XFLOATINT (aggressive) * height;
13265 amount_to_scroll = float_amount;
13266 if (amount_to_scroll == 0 && float_amount > 0)
13267 amount_to_scroll = 1;
13271 if (amount_to_scroll <= 0)
13272 return SCROLLING_FAILED;
13274 move_it_vertically_backward (&it, amount_to_scroll);
13275 startp = it.current.pos;
13279 /* Run window scroll functions. */
13280 startp = run_window_scroll_functions (window, startp);
13282 /* Display the window. Give up if new fonts are loaded, or if point
13283 doesn't appear. */
13284 if (!try_window (window, startp, 0))
13285 rc = SCROLLING_NEED_LARGER_MATRICES;
13286 else if (w->cursor.vpos < 0)
13288 clear_glyph_matrix (w->desired_matrix);
13289 rc = SCROLLING_FAILED;
13291 else
13293 /* Maybe forget recorded base line for line number display. */
13294 if (!just_this_one_p
13295 || current_buffer->clip_changed
13296 || BEG_UNCHANGED < CHARPOS (startp))
13297 w->base_line_number = Qnil;
13299 /* If cursor ends up on a partially visible line,
13300 treat that as being off the bottom of the screen. */
13301 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13303 clear_glyph_matrix (w->desired_matrix);
13304 ++extra_scroll_margin_lines;
13305 goto too_near_end;
13307 rc = SCROLLING_SUCCESS;
13310 return rc;
13314 /* Compute a suitable window start for window W if display of W starts
13315 on a continuation line. Value is non-zero if a new window start
13316 was computed.
13318 The new window start will be computed, based on W's width, starting
13319 from the start of the continued line. It is the start of the
13320 screen line with the minimum distance from the old start W->start. */
13322 static int
13323 compute_window_start_on_continuation_line (struct window *w)
13325 struct text_pos pos, start_pos;
13326 int window_start_changed_p = 0;
13328 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13330 /* If window start is on a continuation line... Window start may be
13331 < BEGV in case there's invisible text at the start of the
13332 buffer (M-x rmail, for example). */
13333 if (CHARPOS (start_pos) > BEGV
13334 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13336 struct it it;
13337 struct glyph_row *row;
13339 /* Handle the case that the window start is out of range. */
13340 if (CHARPOS (start_pos) < BEGV)
13341 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13342 else if (CHARPOS (start_pos) > ZV)
13343 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13345 /* Find the start of the continued line. This should be fast
13346 because scan_buffer is fast (newline cache). */
13347 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13348 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13349 row, DEFAULT_FACE_ID);
13350 reseat_at_previous_visible_line_start (&it);
13352 /* If the line start is "too far" away from the window start,
13353 say it takes too much time to compute a new window start. */
13354 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13355 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13357 int min_distance, distance;
13359 /* Move forward by display lines to find the new window
13360 start. If window width was enlarged, the new start can
13361 be expected to be > the old start. If window width was
13362 decreased, the new window start will be < the old start.
13363 So, we're looking for the display line start with the
13364 minimum distance from the old window start. */
13365 pos = it.current.pos;
13366 min_distance = INFINITY;
13367 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13368 distance < min_distance)
13370 min_distance = distance;
13371 pos = it.current.pos;
13372 move_it_by_lines (&it, 1, 0);
13375 /* Set the window start there. */
13376 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13377 window_start_changed_p = 1;
13381 return window_start_changed_p;
13385 /* Try cursor movement in case text has not changed in window WINDOW,
13386 with window start STARTP. Value is
13388 CURSOR_MOVEMENT_SUCCESS if successful
13390 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13392 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13393 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13394 we want to scroll as if scroll-step were set to 1. See the code.
13396 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13397 which case we have to abort this redisplay, and adjust matrices
13398 first. */
13400 enum
13402 CURSOR_MOVEMENT_SUCCESS,
13403 CURSOR_MOVEMENT_CANNOT_BE_USED,
13404 CURSOR_MOVEMENT_MUST_SCROLL,
13405 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13408 static int
13409 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13411 struct window *w = XWINDOW (window);
13412 struct frame *f = XFRAME (w->frame);
13413 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13415 #if GLYPH_DEBUG
13416 if (inhibit_try_cursor_movement)
13417 return rc;
13418 #endif
13420 /* Handle case where text has not changed, only point, and it has
13421 not moved off the frame. */
13422 if (/* Point may be in this window. */
13423 PT >= CHARPOS (startp)
13424 /* Selective display hasn't changed. */
13425 && !current_buffer->clip_changed
13426 /* Function force-mode-line-update is used to force a thorough
13427 redisplay. It sets either windows_or_buffers_changed or
13428 update_mode_lines. So don't take a shortcut here for these
13429 cases. */
13430 && !update_mode_lines
13431 && !windows_or_buffers_changed
13432 && !cursor_type_changed
13433 /* Can't use this case if highlighting a region. When a
13434 region exists, cursor movement has to do more than just
13435 set the cursor. */
13436 && !(!NILP (Vtransient_mark_mode)
13437 && !NILP (current_buffer->mark_active))
13438 && NILP (w->region_showing)
13439 && NILP (Vshow_trailing_whitespace)
13440 /* Right after splitting windows, last_point may be nil. */
13441 && INTEGERP (w->last_point)
13442 /* This code is not used for mini-buffer for the sake of the case
13443 of redisplaying to replace an echo area message; since in
13444 that case the mini-buffer contents per se are usually
13445 unchanged. This code is of no real use in the mini-buffer
13446 since the handling of this_line_start_pos, etc., in redisplay
13447 handles the same cases. */
13448 && !EQ (window, minibuf_window)
13449 /* When splitting windows or for new windows, it happens that
13450 redisplay is called with a nil window_end_vpos or one being
13451 larger than the window. This should really be fixed in
13452 window.c. I don't have this on my list, now, so we do
13453 approximately the same as the old redisplay code. --gerd. */
13454 && INTEGERP (w->window_end_vpos)
13455 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13456 && (FRAME_WINDOW_P (f)
13457 || !overlay_arrow_in_current_buffer_p ()))
13459 int this_scroll_margin, top_scroll_margin;
13460 struct glyph_row *row = NULL;
13462 #if GLYPH_DEBUG
13463 debug_method_add (w, "cursor movement");
13464 #endif
13466 /* Scroll if point within this distance from the top or bottom
13467 of the window. This is a pixel value. */
13468 if (scroll_margin > 0)
13470 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13471 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13473 else
13474 this_scroll_margin = 0;
13476 top_scroll_margin = this_scroll_margin;
13477 if (WINDOW_WANTS_HEADER_LINE_P (w))
13478 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13480 /* Start with the row the cursor was displayed during the last
13481 not paused redisplay. Give up if that row is not valid. */
13482 if (w->last_cursor.vpos < 0
13483 || w->last_cursor.vpos >= w->current_matrix->nrows)
13484 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13485 else
13487 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13488 if (row->mode_line_p)
13489 ++row;
13490 if (!row->enabled_p)
13491 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13494 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13496 int scroll_p = 0, must_scroll = 0;
13497 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13499 if (PT > XFASTINT (w->last_point))
13501 /* Point has moved forward. */
13502 while (MATRIX_ROW_END_CHARPOS (row) < PT
13503 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13505 xassert (row->enabled_p);
13506 ++row;
13509 /* If the end position of a row equals the start
13510 position of the next row, and PT is at that position,
13511 we would rather display cursor in the next line. */
13512 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13513 && MATRIX_ROW_END_CHARPOS (row) == PT
13514 && row < w->current_matrix->rows
13515 + w->current_matrix->nrows - 1
13516 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13517 && !cursor_row_p (w, row))
13518 ++row;
13520 /* If within the scroll margin, scroll. Note that
13521 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13522 the next line would be drawn, and that
13523 this_scroll_margin can be zero. */
13524 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13525 || PT > MATRIX_ROW_END_CHARPOS (row)
13526 /* Line is completely visible last line in window
13527 and PT is to be set in the next line. */
13528 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13529 && PT == MATRIX_ROW_END_CHARPOS (row)
13530 && !row->ends_at_zv_p
13531 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13532 scroll_p = 1;
13534 else if (PT < XFASTINT (w->last_point))
13536 /* Cursor has to be moved backward. Note that PT >=
13537 CHARPOS (startp) because of the outer if-statement. */
13538 while (!row->mode_line_p
13539 && (MATRIX_ROW_START_CHARPOS (row) > PT
13540 || (MATRIX_ROW_START_CHARPOS (row) == PT
13541 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13542 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13543 row > w->current_matrix->rows
13544 && (row-1)->ends_in_newline_from_string_p))))
13545 && (row->y > top_scroll_margin
13546 || CHARPOS (startp) == BEGV))
13548 xassert (row->enabled_p);
13549 --row;
13552 /* Consider the following case: Window starts at BEGV,
13553 there is invisible, intangible text at BEGV, so that
13554 display starts at some point START > BEGV. It can
13555 happen that we are called with PT somewhere between
13556 BEGV and START. Try to handle that case. */
13557 if (row < w->current_matrix->rows
13558 || row->mode_line_p)
13560 row = w->current_matrix->rows;
13561 if (row->mode_line_p)
13562 ++row;
13565 /* Due to newlines in overlay strings, we may have to
13566 skip forward over overlay strings. */
13567 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13568 && MATRIX_ROW_END_CHARPOS (row) == PT
13569 && !cursor_row_p (w, row))
13570 ++row;
13572 /* If within the scroll margin, scroll. */
13573 if (row->y < top_scroll_margin
13574 && CHARPOS (startp) != BEGV)
13575 scroll_p = 1;
13577 else
13579 /* Cursor did not move. So don't scroll even if cursor line
13580 is partially visible, as it was so before. */
13581 rc = CURSOR_MOVEMENT_SUCCESS;
13584 if (PT < MATRIX_ROW_START_CHARPOS (row)
13585 || PT > MATRIX_ROW_END_CHARPOS (row))
13587 /* if PT is not in the glyph row, give up. */
13588 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13589 must_scroll = 1;
13591 else if (rc != CURSOR_MOVEMENT_SUCCESS
13592 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13594 /* If rows are bidi-reordered and point moved, back up
13595 until we find a row that does not belong to a
13596 continuation line. This is because we must consider
13597 all rows of a continued line as candidates for the
13598 new cursor positioning, since row start and end
13599 positions change non-linearly with vertical position
13600 in such rows. */
13601 /* FIXME: Revisit this when glyph ``spilling'' in
13602 continuation lines' rows is implemented for
13603 bidi-reordered rows. */
13604 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13606 xassert (row->enabled_p);
13607 --row;
13608 /* If we hit the beginning of the displayed portion
13609 without finding the first row of a continued
13610 line, give up. */
13611 if (row <= w->current_matrix->rows)
13613 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13614 break;
13619 if (must_scroll)
13621 else if (rc != CURSOR_MOVEMENT_SUCCESS
13622 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13623 && make_cursor_line_fully_visible_p)
13625 if (PT == MATRIX_ROW_END_CHARPOS (row)
13626 && !row->ends_at_zv_p
13627 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13628 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13629 else if (row->height > window_box_height (w))
13631 /* If we end up in a partially visible line, let's
13632 make it fully visible, except when it's taller
13633 than the window, in which case we can't do much
13634 about it. */
13635 *scroll_step = 1;
13636 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13638 else
13640 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13641 if (!cursor_row_fully_visible_p (w, 0, 1))
13642 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13643 else
13644 rc = CURSOR_MOVEMENT_SUCCESS;
13647 else if (scroll_p)
13648 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13649 else if (rc != CURSOR_MOVEMENT_SUCCESS
13650 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13652 /* With bidi-reordered rows, there could be more than
13653 one candidate row whose start and end positions
13654 occlude point. We need to let set_cursor_from_row
13655 find the best candidate. */
13656 /* FIXME: Revisit this when glyph ``spilling'' in
13657 continuation lines' rows is implemented for
13658 bidi-reordered rows. */
13659 int rv = 0;
13663 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13664 && PT <= MATRIX_ROW_END_CHARPOS (row)
13665 && cursor_row_p (w, row))
13666 rv |= set_cursor_from_row (w, row, w->current_matrix,
13667 0, 0, 0, 0);
13668 /* As soon as we've found the first suitable row
13669 whose ends_at_zv_p flag is set, we are done. */
13670 if (rv
13671 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13673 rc = CURSOR_MOVEMENT_SUCCESS;
13674 break;
13676 ++row;
13678 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13679 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13680 || (MATRIX_ROW_START_CHARPOS (row) == PT
13681 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13682 /* If we didn't find any candidate rows, or exited the
13683 loop before all the candidates were examined, signal
13684 to the caller that this method failed. */
13685 if (rc != CURSOR_MOVEMENT_SUCCESS
13686 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13687 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13688 else if (rv)
13689 rc = CURSOR_MOVEMENT_SUCCESS;
13691 else
13695 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13697 rc = CURSOR_MOVEMENT_SUCCESS;
13698 break;
13700 ++row;
13702 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13703 && MATRIX_ROW_START_CHARPOS (row) == PT
13704 && cursor_row_p (w, row));
13709 return rc;
13712 void
13713 set_vertical_scroll_bar (struct window *w)
13715 EMACS_INT start, end, whole;
13717 /* Calculate the start and end positions for the current window.
13718 At some point, it would be nice to choose between scrollbars
13719 which reflect the whole buffer size, with special markers
13720 indicating narrowing, and scrollbars which reflect only the
13721 visible region.
13723 Note that mini-buffers sometimes aren't displaying any text. */
13724 if (!MINI_WINDOW_P (w)
13725 || (w == XWINDOW (minibuf_window)
13726 && NILP (echo_area_buffer[0])))
13728 struct buffer *buf = XBUFFER (w->buffer);
13729 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13730 start = marker_position (w->start) - BUF_BEGV (buf);
13731 /* I don't think this is guaranteed to be right. For the
13732 moment, we'll pretend it is. */
13733 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13735 if (end < start)
13736 end = start;
13737 if (whole < (end - start))
13738 whole = end - start;
13740 else
13741 start = end = whole = 0;
13743 /* Indicate what this scroll bar ought to be displaying now. */
13744 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13745 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13746 (w, end - start, whole, start);
13750 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13751 selected_window is redisplayed.
13753 We can return without actually redisplaying the window if
13754 fonts_changed_p is nonzero. In that case, redisplay_internal will
13755 retry. */
13757 static void
13758 redisplay_window (Lisp_Object window, int just_this_one_p)
13760 struct window *w = XWINDOW (window);
13761 struct frame *f = XFRAME (w->frame);
13762 struct buffer *buffer = XBUFFER (w->buffer);
13763 struct buffer *old = current_buffer;
13764 struct text_pos lpoint, opoint, startp;
13765 int update_mode_line;
13766 int tem;
13767 struct it it;
13768 /* Record it now because it's overwritten. */
13769 int current_matrix_up_to_date_p = 0;
13770 int used_current_matrix_p = 0;
13771 /* This is less strict than current_matrix_up_to_date_p.
13772 It indictes that the buffer contents and narrowing are unchanged. */
13773 int buffer_unchanged_p = 0;
13774 int temp_scroll_step = 0;
13775 int count = SPECPDL_INDEX ();
13776 int rc;
13777 int centering_position = -1;
13778 int last_line_misfit = 0;
13779 EMACS_INT beg_unchanged, end_unchanged;
13781 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13782 opoint = lpoint;
13784 /* W must be a leaf window here. */
13785 xassert (!NILP (w->buffer));
13786 #if GLYPH_DEBUG
13787 *w->desired_matrix->method = 0;
13788 #endif
13790 restart:
13791 reconsider_clip_changes (w, buffer);
13793 /* Has the mode line to be updated? */
13794 update_mode_line = (!NILP (w->update_mode_line)
13795 || update_mode_lines
13796 || buffer->clip_changed
13797 || buffer->prevent_redisplay_optimizations_p);
13799 if (MINI_WINDOW_P (w))
13801 if (w == XWINDOW (echo_area_window)
13802 && !NILP (echo_area_buffer[0]))
13804 if (update_mode_line)
13805 /* We may have to update a tty frame's menu bar or a
13806 tool-bar. Example `M-x C-h C-h C-g'. */
13807 goto finish_menu_bars;
13808 else
13809 /* We've already displayed the echo area glyphs in this window. */
13810 goto finish_scroll_bars;
13812 else if ((w != XWINDOW (minibuf_window)
13813 || minibuf_level == 0)
13814 /* When buffer is nonempty, redisplay window normally. */
13815 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13816 /* Quail displays non-mini buffers in minibuffer window.
13817 In that case, redisplay the window normally. */
13818 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13820 /* W is a mini-buffer window, but it's not active, so clear
13821 it. */
13822 int yb = window_text_bottom_y (w);
13823 struct glyph_row *row;
13824 int y;
13826 for (y = 0, row = w->desired_matrix->rows;
13827 y < yb;
13828 y += row->height, ++row)
13829 blank_row (w, row, y);
13830 goto finish_scroll_bars;
13833 clear_glyph_matrix (w->desired_matrix);
13836 /* Otherwise set up data on this window; select its buffer and point
13837 value. */
13838 /* Really select the buffer, for the sake of buffer-local
13839 variables. */
13840 set_buffer_internal_1 (XBUFFER (w->buffer));
13842 current_matrix_up_to_date_p
13843 = (!NILP (w->window_end_valid)
13844 && !current_buffer->clip_changed
13845 && !current_buffer->prevent_redisplay_optimizations_p
13846 && XFASTINT (w->last_modified) >= MODIFF
13847 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13849 /* Run the window-bottom-change-functions
13850 if it is possible that the text on the screen has changed
13851 (either due to modification of the text, or any other reason). */
13852 if (!current_matrix_up_to_date_p
13853 && !NILP (Vwindow_text_change_functions))
13855 safe_run_hooks (Qwindow_text_change_functions);
13856 goto restart;
13859 beg_unchanged = BEG_UNCHANGED;
13860 end_unchanged = END_UNCHANGED;
13862 SET_TEXT_POS (opoint, PT, PT_BYTE);
13864 specbind (Qinhibit_point_motion_hooks, Qt);
13866 buffer_unchanged_p
13867 = (!NILP (w->window_end_valid)
13868 && !current_buffer->clip_changed
13869 && XFASTINT (w->last_modified) >= MODIFF
13870 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13872 /* When windows_or_buffers_changed is non-zero, we can't rely on
13873 the window end being valid, so set it to nil there. */
13874 if (windows_or_buffers_changed)
13876 /* If window starts on a continuation line, maybe adjust the
13877 window start in case the window's width changed. */
13878 if (XMARKER (w->start)->buffer == current_buffer)
13879 compute_window_start_on_continuation_line (w);
13881 w->window_end_valid = Qnil;
13884 /* Some sanity checks. */
13885 CHECK_WINDOW_END (w);
13886 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13887 abort ();
13888 if (BYTEPOS (opoint) < CHARPOS (opoint))
13889 abort ();
13891 /* If %c is in mode line, update it if needed. */
13892 if (!NILP (w->column_number_displayed)
13893 /* This alternative quickly identifies a common case
13894 where no change is needed. */
13895 && !(PT == XFASTINT (w->last_point)
13896 && XFASTINT (w->last_modified) >= MODIFF
13897 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13898 && (XFASTINT (w->column_number_displayed)
13899 != (int) current_column ())) /* iftc */
13900 update_mode_line = 1;
13902 /* Count number of windows showing the selected buffer. An indirect
13903 buffer counts as its base buffer. */
13904 if (!just_this_one_p)
13906 struct buffer *current_base, *window_base;
13907 current_base = current_buffer;
13908 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13909 if (current_base->base_buffer)
13910 current_base = current_base->base_buffer;
13911 if (window_base->base_buffer)
13912 window_base = window_base->base_buffer;
13913 if (current_base == window_base)
13914 buffer_shared++;
13917 /* Point refers normally to the selected window. For any other
13918 window, set up appropriate value. */
13919 if (!EQ (window, selected_window))
13921 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
13922 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
13923 if (new_pt < BEGV)
13925 new_pt = BEGV;
13926 new_pt_byte = BEGV_BYTE;
13927 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13929 else if (new_pt > (ZV - 1))
13931 new_pt = ZV;
13932 new_pt_byte = ZV_BYTE;
13933 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13936 /* We don't use SET_PT so that the point-motion hooks don't run. */
13937 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13940 /* If any of the character widths specified in the display table
13941 have changed, invalidate the width run cache. It's true that
13942 this may be a bit late to catch such changes, but the rest of
13943 redisplay goes (non-fatally) haywire when the display table is
13944 changed, so why should we worry about doing any better? */
13945 if (current_buffer->width_run_cache)
13947 struct Lisp_Char_Table *disptab = buffer_display_table ();
13949 if (! disptab_matches_widthtab (disptab,
13950 XVECTOR (current_buffer->width_table)))
13952 invalidate_region_cache (current_buffer,
13953 current_buffer->width_run_cache,
13954 BEG, Z);
13955 recompute_width_table (current_buffer, disptab);
13959 /* If window-start is screwed up, choose a new one. */
13960 if (XMARKER (w->start)->buffer != current_buffer)
13961 goto recenter;
13963 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13965 /* If someone specified a new starting point but did not insist,
13966 check whether it can be used. */
13967 if (!NILP (w->optional_new_start)
13968 && CHARPOS (startp) >= BEGV
13969 && CHARPOS (startp) <= ZV)
13971 w->optional_new_start = Qnil;
13972 start_display (&it, w, startp);
13973 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13974 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13975 if (IT_CHARPOS (it) == PT)
13976 w->force_start = Qt;
13977 /* IT may overshoot PT if text at PT is invisible. */
13978 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13979 w->force_start = Qt;
13982 force_start:
13984 /* Handle case where place to start displaying has been specified,
13985 unless the specified location is outside the accessible range. */
13986 if (!NILP (w->force_start)
13987 || w->frozen_window_start_p)
13989 /* We set this later on if we have to adjust point. */
13990 int new_vpos = -1;
13992 w->force_start = Qnil;
13993 w->vscroll = 0;
13994 w->window_end_valid = Qnil;
13996 /* Forget any recorded base line for line number display. */
13997 if (!buffer_unchanged_p)
13998 w->base_line_number = Qnil;
14000 /* Redisplay the mode line. Select the buffer properly for that.
14001 Also, run the hook window-scroll-functions
14002 because we have scrolled. */
14003 /* Note, we do this after clearing force_start because
14004 if there's an error, it is better to forget about force_start
14005 than to get into an infinite loop calling the hook functions
14006 and having them get more errors. */
14007 if (!update_mode_line
14008 || ! NILP (Vwindow_scroll_functions))
14010 update_mode_line = 1;
14011 w->update_mode_line = Qt;
14012 startp = run_window_scroll_functions (window, startp);
14015 w->last_modified = make_number (0);
14016 w->last_overlay_modified = make_number (0);
14017 if (CHARPOS (startp) < BEGV)
14018 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14019 else if (CHARPOS (startp) > ZV)
14020 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14022 /* Redisplay, then check if cursor has been set during the
14023 redisplay. Give up if new fonts were loaded. */
14024 /* We used to issue a CHECK_MARGINS argument to try_window here,
14025 but this causes scrolling to fail when point begins inside
14026 the scroll margin (bug#148) -- cyd */
14027 if (!try_window (window, startp, 0))
14029 w->force_start = Qt;
14030 clear_glyph_matrix (w->desired_matrix);
14031 goto need_larger_matrices;
14034 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14036 /* If point does not appear, try to move point so it does
14037 appear. The desired matrix has been built above, so we
14038 can use it here. */
14039 new_vpos = window_box_height (w) / 2;
14042 if (!cursor_row_fully_visible_p (w, 0, 0))
14044 /* Point does appear, but on a line partly visible at end of window.
14045 Move it back to a fully-visible line. */
14046 new_vpos = window_box_height (w);
14049 /* If we need to move point for either of the above reasons,
14050 now actually do it. */
14051 if (new_vpos >= 0)
14053 struct glyph_row *row;
14055 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14056 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14057 ++row;
14059 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14060 MATRIX_ROW_START_BYTEPOS (row));
14062 if (w != XWINDOW (selected_window))
14063 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14064 else if (current_buffer == old)
14065 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14067 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14069 /* If we are highlighting the region, then we just changed
14070 the region, so redisplay to show it. */
14071 if (!NILP (Vtransient_mark_mode)
14072 && !NILP (current_buffer->mark_active))
14074 clear_glyph_matrix (w->desired_matrix);
14075 if (!try_window (window, startp, 0))
14076 goto need_larger_matrices;
14080 #if GLYPH_DEBUG
14081 debug_method_add (w, "forced window start");
14082 #endif
14083 goto done;
14086 /* Handle case where text has not changed, only point, and it has
14087 not moved off the frame, and we are not retrying after hscroll.
14088 (current_matrix_up_to_date_p is nonzero when retrying.) */
14089 if (current_matrix_up_to_date_p
14090 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14091 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14093 switch (rc)
14095 case CURSOR_MOVEMENT_SUCCESS:
14096 used_current_matrix_p = 1;
14097 goto done;
14099 case CURSOR_MOVEMENT_MUST_SCROLL:
14100 goto try_to_scroll;
14102 default:
14103 abort ();
14106 /* If current starting point was originally the beginning of a line
14107 but no longer is, find a new starting point. */
14108 else if (!NILP (w->start_at_line_beg)
14109 && !(CHARPOS (startp) <= BEGV
14110 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14112 #if GLYPH_DEBUG
14113 debug_method_add (w, "recenter 1");
14114 #endif
14115 goto recenter;
14118 /* Try scrolling with try_window_id. Value is > 0 if update has
14119 been done, it is -1 if we know that the same window start will
14120 not work. It is 0 if unsuccessful for some other reason. */
14121 else if ((tem = try_window_id (w)) != 0)
14123 #if GLYPH_DEBUG
14124 debug_method_add (w, "try_window_id %d", tem);
14125 #endif
14127 if (fonts_changed_p)
14128 goto need_larger_matrices;
14129 if (tem > 0)
14130 goto done;
14132 /* Otherwise try_window_id has returned -1 which means that we
14133 don't want the alternative below this comment to execute. */
14135 else if (CHARPOS (startp) >= BEGV
14136 && CHARPOS (startp) <= ZV
14137 && PT >= CHARPOS (startp)
14138 && (CHARPOS (startp) < ZV
14139 /* Avoid starting at end of buffer. */
14140 || CHARPOS (startp) == BEGV
14141 || (XFASTINT (w->last_modified) >= MODIFF
14142 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14145 /* If first window line is a continuation line, and window start
14146 is inside the modified region, but the first change is before
14147 current window start, we must select a new window start.
14149 However, if this is the result of a down-mouse event (e.g. by
14150 extending the mouse-drag-overlay), we don't want to select a
14151 new window start, since that would change the position under
14152 the mouse, resulting in an unwanted mouse-movement rather
14153 than a simple mouse-click. */
14154 if (NILP (w->start_at_line_beg)
14155 && NILP (do_mouse_tracking)
14156 && CHARPOS (startp) > BEGV
14157 && CHARPOS (startp) > BEG + beg_unchanged
14158 && CHARPOS (startp) <= Z - end_unchanged
14159 /* Even if w->start_at_line_beg is nil, a new window may
14160 start at a line_beg, since that's how set_buffer_window
14161 sets it. So, we need to check the return value of
14162 compute_window_start_on_continuation_line. (See also
14163 bug#197). */
14164 && XMARKER (w->start)->buffer == current_buffer
14165 && compute_window_start_on_continuation_line (w))
14167 w->force_start = Qt;
14168 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14169 goto force_start;
14172 #if GLYPH_DEBUG
14173 debug_method_add (w, "same window start");
14174 #endif
14176 /* Try to redisplay starting at same place as before.
14177 If point has not moved off frame, accept the results. */
14178 if (!current_matrix_up_to_date_p
14179 /* Don't use try_window_reusing_current_matrix in this case
14180 because a window scroll function can have changed the
14181 buffer. */
14182 || !NILP (Vwindow_scroll_functions)
14183 || MINI_WINDOW_P (w)
14184 || !(used_current_matrix_p
14185 = try_window_reusing_current_matrix (w)))
14187 IF_DEBUG (debug_method_add (w, "1"));
14188 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14189 /* -1 means we need to scroll.
14190 0 means we need new matrices, but fonts_changed_p
14191 is set in that case, so we will detect it below. */
14192 goto try_to_scroll;
14195 if (fonts_changed_p)
14196 goto need_larger_matrices;
14198 if (w->cursor.vpos >= 0)
14200 if (!just_this_one_p
14201 || current_buffer->clip_changed
14202 || BEG_UNCHANGED < CHARPOS (startp))
14203 /* Forget any recorded base line for line number display. */
14204 w->base_line_number = Qnil;
14206 if (!cursor_row_fully_visible_p (w, 1, 0))
14208 clear_glyph_matrix (w->desired_matrix);
14209 last_line_misfit = 1;
14211 /* Drop through and scroll. */
14212 else
14213 goto done;
14215 else
14216 clear_glyph_matrix (w->desired_matrix);
14219 try_to_scroll:
14221 w->last_modified = make_number (0);
14222 w->last_overlay_modified = make_number (0);
14224 /* Redisplay the mode line. Select the buffer properly for that. */
14225 if (!update_mode_line)
14227 update_mode_line = 1;
14228 w->update_mode_line = Qt;
14231 /* Try to scroll by specified few lines. */
14232 if ((scroll_conservatively
14233 || scroll_step
14234 || temp_scroll_step
14235 || NUMBERP (current_buffer->scroll_up_aggressively)
14236 || NUMBERP (current_buffer->scroll_down_aggressively))
14237 && !current_buffer->clip_changed
14238 && CHARPOS (startp) >= BEGV
14239 && CHARPOS (startp) <= ZV)
14241 /* The function returns -1 if new fonts were loaded, 1 if
14242 successful, 0 if not successful. */
14243 int rc = try_scrolling (window, just_this_one_p,
14244 scroll_conservatively,
14245 scroll_step,
14246 temp_scroll_step, last_line_misfit);
14247 switch (rc)
14249 case SCROLLING_SUCCESS:
14250 goto done;
14252 case SCROLLING_NEED_LARGER_MATRICES:
14253 goto need_larger_matrices;
14255 case SCROLLING_FAILED:
14256 break;
14258 default:
14259 abort ();
14263 /* Finally, just choose place to start which centers point */
14265 recenter:
14266 if (centering_position < 0)
14267 centering_position = window_box_height (w) / 2;
14269 #if GLYPH_DEBUG
14270 debug_method_add (w, "recenter");
14271 #endif
14273 /* w->vscroll = 0; */
14275 /* Forget any previously recorded base line for line number display. */
14276 if (!buffer_unchanged_p)
14277 w->base_line_number = Qnil;
14279 /* Move backward half the height of the window. */
14280 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14281 it.current_y = it.last_visible_y;
14282 move_it_vertically_backward (&it, centering_position);
14283 xassert (IT_CHARPOS (it) >= BEGV);
14285 /* The function move_it_vertically_backward may move over more
14286 than the specified y-distance. If it->w is small, e.g. a
14287 mini-buffer window, we may end up in front of the window's
14288 display area. Start displaying at the start of the line
14289 containing PT in this case. */
14290 if (it.current_y <= 0)
14292 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14293 move_it_vertically_backward (&it, 0);
14294 it.current_y = 0;
14297 it.current_x = it.hpos = 0;
14299 /* Set startp here explicitly in case that helps avoid an infinite loop
14300 in case the window-scroll-functions functions get errors. */
14301 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14303 /* Run scroll hooks. */
14304 startp = run_window_scroll_functions (window, it.current.pos);
14306 /* Redisplay the window. */
14307 if (!current_matrix_up_to_date_p
14308 || windows_or_buffers_changed
14309 || cursor_type_changed
14310 /* Don't use try_window_reusing_current_matrix in this case
14311 because it can have changed the buffer. */
14312 || !NILP (Vwindow_scroll_functions)
14313 || !just_this_one_p
14314 || MINI_WINDOW_P (w)
14315 || !(used_current_matrix_p
14316 = try_window_reusing_current_matrix (w)))
14317 try_window (window, startp, 0);
14319 /* If new fonts have been loaded (due to fontsets), give up. We
14320 have to start a new redisplay since we need to re-adjust glyph
14321 matrices. */
14322 if (fonts_changed_p)
14323 goto need_larger_matrices;
14325 /* If cursor did not appear assume that the middle of the window is
14326 in the first line of the window. Do it again with the next line.
14327 (Imagine a window of height 100, displaying two lines of height
14328 60. Moving back 50 from it->last_visible_y will end in the first
14329 line.) */
14330 if (w->cursor.vpos < 0)
14332 if (!NILP (w->window_end_valid)
14333 && PT >= Z - XFASTINT (w->window_end_pos))
14335 clear_glyph_matrix (w->desired_matrix);
14336 move_it_by_lines (&it, 1, 0);
14337 try_window (window, it.current.pos, 0);
14339 else if (PT < IT_CHARPOS (it))
14341 clear_glyph_matrix (w->desired_matrix);
14342 move_it_by_lines (&it, -1, 0);
14343 try_window (window, it.current.pos, 0);
14345 else
14347 /* Not much we can do about it. */
14351 /* Consider the following case: Window starts at BEGV, there is
14352 invisible, intangible text at BEGV, so that display starts at
14353 some point START > BEGV. It can happen that we are called with
14354 PT somewhere between BEGV and START. Try to handle that case. */
14355 if (w->cursor.vpos < 0)
14357 struct glyph_row *row = w->current_matrix->rows;
14358 if (row->mode_line_p)
14359 ++row;
14360 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14363 if (!cursor_row_fully_visible_p (w, 0, 0))
14365 /* If vscroll is enabled, disable it and try again. */
14366 if (w->vscroll)
14368 w->vscroll = 0;
14369 clear_glyph_matrix (w->desired_matrix);
14370 goto recenter;
14373 /* If centering point failed to make the whole line visible,
14374 put point at the top instead. That has to make the whole line
14375 visible, if it can be done. */
14376 if (centering_position == 0)
14377 goto done;
14379 clear_glyph_matrix (w->desired_matrix);
14380 centering_position = 0;
14381 goto recenter;
14384 done:
14386 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14387 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14388 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14389 ? Qt : Qnil);
14391 /* Display the mode line, if we must. */
14392 if ((update_mode_line
14393 /* If window not full width, must redo its mode line
14394 if (a) the window to its side is being redone and
14395 (b) we do a frame-based redisplay. This is a consequence
14396 of how inverted lines are drawn in frame-based redisplay. */
14397 || (!just_this_one_p
14398 && !FRAME_WINDOW_P (f)
14399 && !WINDOW_FULL_WIDTH_P (w))
14400 /* Line number to display. */
14401 || INTEGERP (w->base_line_pos)
14402 /* Column number is displayed and different from the one displayed. */
14403 || (!NILP (w->column_number_displayed)
14404 && (XFASTINT (w->column_number_displayed)
14405 != (int) current_column ()))) /* iftc */
14406 /* This means that the window has a mode line. */
14407 && (WINDOW_WANTS_MODELINE_P (w)
14408 || WINDOW_WANTS_HEADER_LINE_P (w)))
14410 display_mode_lines (w);
14412 /* If mode line height has changed, arrange for a thorough
14413 immediate redisplay using the correct mode line height. */
14414 if (WINDOW_WANTS_MODELINE_P (w)
14415 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14417 fonts_changed_p = 1;
14418 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14419 = DESIRED_MODE_LINE_HEIGHT (w);
14422 /* If header line height has changed, arrange for a thorough
14423 immediate redisplay using the correct header line height. */
14424 if (WINDOW_WANTS_HEADER_LINE_P (w)
14425 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14427 fonts_changed_p = 1;
14428 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14429 = DESIRED_HEADER_LINE_HEIGHT (w);
14432 if (fonts_changed_p)
14433 goto need_larger_matrices;
14436 if (!line_number_displayed
14437 && !BUFFERP (w->base_line_pos))
14439 w->base_line_pos = Qnil;
14440 w->base_line_number = Qnil;
14443 finish_menu_bars:
14445 /* When we reach a frame's selected window, redo the frame's menu bar. */
14446 if (update_mode_line
14447 && EQ (FRAME_SELECTED_WINDOW (f), window))
14449 int redisplay_menu_p = 0;
14450 int redisplay_tool_bar_p = 0;
14452 if (FRAME_WINDOW_P (f))
14454 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14455 || defined (HAVE_NS) || defined (USE_GTK)
14456 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14457 #else
14458 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14459 #endif
14461 else
14462 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14464 if (redisplay_menu_p)
14465 display_menu_bar (w);
14467 #ifdef HAVE_WINDOW_SYSTEM
14468 if (FRAME_WINDOW_P (f))
14470 #if defined (USE_GTK) || defined (HAVE_NS)
14471 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14472 #else
14473 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14474 && (FRAME_TOOL_BAR_LINES (f) > 0
14475 || !NILP (Vauto_resize_tool_bars));
14476 #endif
14478 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14480 ignore_mouse_drag_p = 1;
14483 #endif
14486 #ifdef HAVE_WINDOW_SYSTEM
14487 if (FRAME_WINDOW_P (f)
14488 && update_window_fringes (w, (just_this_one_p
14489 || (!used_current_matrix_p && !overlay_arrow_seen)
14490 || w->pseudo_window_p)))
14492 update_begin (f);
14493 BLOCK_INPUT;
14494 if (draw_window_fringes (w, 1))
14495 x_draw_vertical_border (w);
14496 UNBLOCK_INPUT;
14497 update_end (f);
14499 #endif /* HAVE_WINDOW_SYSTEM */
14501 /* We go to this label, with fonts_changed_p nonzero,
14502 if it is necessary to try again using larger glyph matrices.
14503 We have to redeem the scroll bar even in this case,
14504 because the loop in redisplay_internal expects that. */
14505 need_larger_matrices:
14507 finish_scroll_bars:
14509 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14511 /* Set the thumb's position and size. */
14512 set_vertical_scroll_bar (w);
14514 /* Note that we actually used the scroll bar attached to this
14515 window, so it shouldn't be deleted at the end of redisplay. */
14516 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14517 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14520 /* Restore current_buffer and value of point in it. The window
14521 update may have changed the buffer, so first make sure `opoint'
14522 is still valid (Bug#6177). */
14523 if (CHARPOS (opoint) < BEGV)
14524 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14525 else if (CHARPOS (opoint) > ZV)
14526 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14527 else
14528 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14530 set_buffer_internal_1 (old);
14531 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14532 shorter. This can be caused by log truncation in *Messages*. */
14533 if (CHARPOS (lpoint) <= ZV)
14534 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14536 unbind_to (count, Qnil);
14540 /* Build the complete desired matrix of WINDOW with a window start
14541 buffer position POS.
14543 Value is 1 if successful. It is zero if fonts were loaded during
14544 redisplay which makes re-adjusting glyph matrices necessary, and -1
14545 if point would appear in the scroll margins.
14546 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14547 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14548 set in FLAGS.) */
14551 try_window (Lisp_Object window, struct text_pos pos, int flags)
14553 struct window *w = XWINDOW (window);
14554 struct it it;
14555 struct glyph_row *last_text_row = NULL;
14556 struct frame *f = XFRAME (w->frame);
14558 /* Make POS the new window start. */
14559 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14561 /* Mark cursor position as unknown. No overlay arrow seen. */
14562 w->cursor.vpos = -1;
14563 overlay_arrow_seen = 0;
14565 /* Initialize iterator and info to start at POS. */
14566 start_display (&it, w, pos);
14568 /* Display all lines of W. */
14569 while (it.current_y < it.last_visible_y)
14571 if (display_line (&it))
14572 last_text_row = it.glyph_row - 1;
14573 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14574 return 0;
14577 /* Don't let the cursor end in the scroll margins. */
14578 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14579 && !MINI_WINDOW_P (w))
14581 int this_scroll_margin;
14583 if (scroll_margin > 0)
14585 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14586 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14588 else
14589 this_scroll_margin = 0;
14591 if ((w->cursor.y >= 0 /* not vscrolled */
14592 && w->cursor.y < this_scroll_margin
14593 && CHARPOS (pos) > BEGV
14594 && IT_CHARPOS (it) < ZV)
14595 /* rms: considering make_cursor_line_fully_visible_p here
14596 seems to give wrong results. We don't want to recenter
14597 when the last line is partly visible, we want to allow
14598 that case to be handled in the usual way. */
14599 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14601 w->cursor.vpos = -1;
14602 clear_glyph_matrix (w->desired_matrix);
14603 return -1;
14607 /* If bottom moved off end of frame, change mode line percentage. */
14608 if (XFASTINT (w->window_end_pos) <= 0
14609 && Z != IT_CHARPOS (it))
14610 w->update_mode_line = Qt;
14612 /* Set window_end_pos to the offset of the last character displayed
14613 on the window from the end of current_buffer. Set
14614 window_end_vpos to its row number. */
14615 if (last_text_row)
14617 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14618 w->window_end_bytepos
14619 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14620 w->window_end_pos
14621 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14622 w->window_end_vpos
14623 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14624 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14625 ->displays_text_p);
14627 else
14629 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14630 w->window_end_pos = make_number (Z - ZV);
14631 w->window_end_vpos = make_number (0);
14634 /* But that is not valid info until redisplay finishes. */
14635 w->window_end_valid = Qnil;
14636 return 1;
14641 /************************************************************************
14642 Window redisplay reusing current matrix when buffer has not changed
14643 ************************************************************************/
14645 /* Try redisplay of window W showing an unchanged buffer with a
14646 different window start than the last time it was displayed by
14647 reusing its current matrix. Value is non-zero if successful.
14648 W->start is the new window start. */
14650 static int
14651 try_window_reusing_current_matrix (struct window *w)
14653 struct frame *f = XFRAME (w->frame);
14654 struct glyph_row *row, *bottom_row;
14655 struct it it;
14656 struct run run;
14657 struct text_pos start, new_start;
14658 int nrows_scrolled, i;
14659 struct glyph_row *last_text_row;
14660 struct glyph_row *last_reused_text_row;
14661 struct glyph_row *start_row;
14662 int start_vpos, min_y, max_y;
14664 #if GLYPH_DEBUG
14665 if (inhibit_try_window_reusing)
14666 return 0;
14667 #endif
14669 if (/* This function doesn't handle terminal frames. */
14670 !FRAME_WINDOW_P (f)
14671 /* Don't try to reuse the display if windows have been split
14672 or such. */
14673 || windows_or_buffers_changed
14674 || cursor_type_changed)
14675 return 0;
14677 /* Can't do this if region may have changed. */
14678 if ((!NILP (Vtransient_mark_mode)
14679 && !NILP (current_buffer->mark_active))
14680 || !NILP (w->region_showing)
14681 || !NILP (Vshow_trailing_whitespace))
14682 return 0;
14684 /* If top-line visibility has changed, give up. */
14685 if (WINDOW_WANTS_HEADER_LINE_P (w)
14686 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14687 return 0;
14689 /* Give up if old or new display is scrolled vertically. We could
14690 make this function handle this, but right now it doesn't. */
14691 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14692 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14693 return 0;
14695 /* The variable new_start now holds the new window start. The old
14696 start `start' can be determined from the current matrix. */
14697 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14698 start = start_row->minpos;
14699 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14701 /* Clear the desired matrix for the display below. */
14702 clear_glyph_matrix (w->desired_matrix);
14704 if (CHARPOS (new_start) <= CHARPOS (start))
14706 int first_row_y;
14708 /* Don't use this method if the display starts with an ellipsis
14709 displayed for invisible text. It's not easy to handle that case
14710 below, and it's certainly not worth the effort since this is
14711 not a frequent case. */
14712 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14713 return 0;
14715 IF_DEBUG (debug_method_add (w, "twu1"));
14717 /* Display up to a row that can be reused. The variable
14718 last_text_row is set to the last row displayed that displays
14719 text. Note that it.vpos == 0 if or if not there is a
14720 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14721 start_display (&it, w, new_start);
14722 first_row_y = it.current_y;
14723 w->cursor.vpos = -1;
14724 last_text_row = last_reused_text_row = NULL;
14726 while (it.current_y < it.last_visible_y
14727 && !fonts_changed_p)
14729 /* If we have reached into the characters in the START row,
14730 that means the line boundaries have changed. So we
14731 can't start copying with the row START. Maybe it will
14732 work to start copying with the following row. */
14733 while (IT_CHARPOS (it) > CHARPOS (start))
14735 /* Advance to the next row as the "start". */
14736 start_row++;
14737 start = start_row->minpos;
14738 /* If there are no more rows to try, or just one, give up. */
14739 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14740 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14741 || CHARPOS (start) == ZV)
14743 clear_glyph_matrix (w->desired_matrix);
14744 return 0;
14747 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14749 /* If we have reached alignment,
14750 we can copy the rest of the rows. */
14751 if (IT_CHARPOS (it) == CHARPOS (start))
14752 break;
14754 if (display_line (&it))
14755 last_text_row = it.glyph_row - 1;
14758 /* A value of current_y < last_visible_y means that we stopped
14759 at the previous window start, which in turn means that we
14760 have at least one reusable row. */
14761 if (it.current_y < it.last_visible_y)
14763 /* IT.vpos always starts from 0; it counts text lines. */
14764 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14766 /* Find PT if not already found in the lines displayed. */
14767 if (w->cursor.vpos < 0)
14769 int dy = it.current_y - start_row->y;
14771 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14772 row = row_containing_pos (w, PT, row, NULL, dy);
14773 if (row)
14774 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14775 dy, nrows_scrolled);
14776 else
14778 clear_glyph_matrix (w->desired_matrix);
14779 return 0;
14783 /* Scroll the display. Do it before the current matrix is
14784 changed. The problem here is that update has not yet
14785 run, i.e. part of the current matrix is not up to date.
14786 scroll_run_hook will clear the cursor, and use the
14787 current matrix to get the height of the row the cursor is
14788 in. */
14789 run.current_y = start_row->y;
14790 run.desired_y = it.current_y;
14791 run.height = it.last_visible_y - it.current_y;
14793 if (run.height > 0 && run.current_y != run.desired_y)
14795 update_begin (f);
14796 FRAME_RIF (f)->update_window_begin_hook (w);
14797 FRAME_RIF (f)->clear_window_mouse_face (w);
14798 FRAME_RIF (f)->scroll_run_hook (w, &run);
14799 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14800 update_end (f);
14803 /* Shift current matrix down by nrows_scrolled lines. */
14804 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14805 rotate_matrix (w->current_matrix,
14806 start_vpos,
14807 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14808 nrows_scrolled);
14810 /* Disable lines that must be updated. */
14811 for (i = 0; i < nrows_scrolled; ++i)
14812 (start_row + i)->enabled_p = 0;
14814 /* Re-compute Y positions. */
14815 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14816 max_y = it.last_visible_y;
14817 for (row = start_row + nrows_scrolled;
14818 row < bottom_row;
14819 ++row)
14821 row->y = it.current_y;
14822 row->visible_height = row->height;
14824 if (row->y < min_y)
14825 row->visible_height -= min_y - row->y;
14826 if (row->y + row->height > max_y)
14827 row->visible_height -= row->y + row->height - max_y;
14828 row->redraw_fringe_bitmaps_p = 1;
14830 it.current_y += row->height;
14832 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14833 last_reused_text_row = row;
14834 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14835 break;
14838 /* Disable lines in the current matrix which are now
14839 below the window. */
14840 for (++row; row < bottom_row; ++row)
14841 row->enabled_p = row->mode_line_p = 0;
14844 /* Update window_end_pos etc.; last_reused_text_row is the last
14845 reused row from the current matrix containing text, if any.
14846 The value of last_text_row is the last displayed line
14847 containing text. */
14848 if (last_reused_text_row)
14850 w->window_end_bytepos
14851 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14852 w->window_end_pos
14853 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14854 w->window_end_vpos
14855 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14856 w->current_matrix));
14858 else if (last_text_row)
14860 w->window_end_bytepos
14861 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14862 w->window_end_pos
14863 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14864 w->window_end_vpos
14865 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14867 else
14869 /* This window must be completely empty. */
14870 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14871 w->window_end_pos = make_number (Z - ZV);
14872 w->window_end_vpos = make_number (0);
14874 w->window_end_valid = Qnil;
14876 /* Update hint: don't try scrolling again in update_window. */
14877 w->desired_matrix->no_scrolling_p = 1;
14879 #if GLYPH_DEBUG
14880 debug_method_add (w, "try_window_reusing_current_matrix 1");
14881 #endif
14882 return 1;
14884 else if (CHARPOS (new_start) > CHARPOS (start))
14886 struct glyph_row *pt_row, *row;
14887 struct glyph_row *first_reusable_row;
14888 struct glyph_row *first_row_to_display;
14889 int dy;
14890 int yb = window_text_bottom_y (w);
14892 /* Find the row starting at new_start, if there is one. Don't
14893 reuse a partially visible line at the end. */
14894 first_reusable_row = start_row;
14895 while (first_reusable_row->enabled_p
14896 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14897 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14898 < CHARPOS (new_start)))
14899 ++first_reusable_row;
14901 /* Give up if there is no row to reuse. */
14902 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14903 || !first_reusable_row->enabled_p
14904 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14905 != CHARPOS (new_start)))
14906 return 0;
14908 /* We can reuse fully visible rows beginning with
14909 first_reusable_row to the end of the window. Set
14910 first_row_to_display to the first row that cannot be reused.
14911 Set pt_row to the row containing point, if there is any. */
14912 pt_row = NULL;
14913 for (first_row_to_display = first_reusable_row;
14914 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14915 ++first_row_to_display)
14917 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14918 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14919 pt_row = first_row_to_display;
14922 /* Start displaying at the start of first_row_to_display. */
14923 xassert (first_row_to_display->y < yb);
14924 init_to_row_start (&it, w, first_row_to_display);
14926 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14927 - start_vpos);
14928 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14929 - nrows_scrolled);
14930 it.current_y = (first_row_to_display->y - first_reusable_row->y
14931 + WINDOW_HEADER_LINE_HEIGHT (w));
14933 /* Display lines beginning with first_row_to_display in the
14934 desired matrix. Set last_text_row to the last row displayed
14935 that displays text. */
14936 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14937 if (pt_row == NULL)
14938 w->cursor.vpos = -1;
14939 last_text_row = NULL;
14940 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14941 if (display_line (&it))
14942 last_text_row = it.glyph_row - 1;
14944 /* If point is in a reused row, adjust y and vpos of the cursor
14945 position. */
14946 if (pt_row)
14948 w->cursor.vpos -= nrows_scrolled;
14949 w->cursor.y -= first_reusable_row->y - start_row->y;
14952 /* Give up if point isn't in a row displayed or reused. (This
14953 also handles the case where w->cursor.vpos < nrows_scrolled
14954 after the calls to display_line, which can happen with scroll
14955 margins. See bug#1295.) */
14956 if (w->cursor.vpos < 0)
14958 clear_glyph_matrix (w->desired_matrix);
14959 return 0;
14962 /* Scroll the display. */
14963 run.current_y = first_reusable_row->y;
14964 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14965 run.height = it.last_visible_y - run.current_y;
14966 dy = run.current_y - run.desired_y;
14968 if (run.height)
14970 update_begin (f);
14971 FRAME_RIF (f)->update_window_begin_hook (w);
14972 FRAME_RIF (f)->clear_window_mouse_face (w);
14973 FRAME_RIF (f)->scroll_run_hook (w, &run);
14974 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14975 update_end (f);
14978 /* Adjust Y positions of reused rows. */
14979 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14980 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14981 max_y = it.last_visible_y;
14982 for (row = first_reusable_row; row < first_row_to_display; ++row)
14984 row->y -= dy;
14985 row->visible_height = row->height;
14986 if (row->y < min_y)
14987 row->visible_height -= min_y - row->y;
14988 if (row->y + row->height > max_y)
14989 row->visible_height -= row->y + row->height - max_y;
14990 row->redraw_fringe_bitmaps_p = 1;
14993 /* Scroll the current matrix. */
14994 xassert (nrows_scrolled > 0);
14995 rotate_matrix (w->current_matrix,
14996 start_vpos,
14997 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14998 -nrows_scrolled);
15000 /* Disable rows not reused. */
15001 for (row -= nrows_scrolled; row < bottom_row; ++row)
15002 row->enabled_p = 0;
15004 /* Point may have moved to a different line, so we cannot assume that
15005 the previous cursor position is valid; locate the correct row. */
15006 if (pt_row)
15008 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15009 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15010 row++)
15012 w->cursor.vpos++;
15013 w->cursor.y = row->y;
15015 if (row < bottom_row)
15017 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15018 struct glyph *end = glyph + row->used[TEXT_AREA];
15020 /* Can't use this optimization with bidi-reordered glyph
15021 rows, unless cursor is already at point. */
15022 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15024 if (!(w->cursor.hpos >= 0
15025 && w->cursor.hpos < row->used[TEXT_AREA]
15026 && BUFFERP (glyph->object)
15027 && glyph->charpos == PT))
15028 return 0;
15030 else
15031 for (; glyph < end
15032 && (!BUFFERP (glyph->object)
15033 || glyph->charpos < PT);
15034 glyph++)
15036 w->cursor.hpos++;
15037 w->cursor.x += glyph->pixel_width;
15042 /* Adjust window end. A null value of last_text_row means that
15043 the window end is in reused rows which in turn means that
15044 only its vpos can have changed. */
15045 if (last_text_row)
15047 w->window_end_bytepos
15048 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15049 w->window_end_pos
15050 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15051 w->window_end_vpos
15052 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15054 else
15056 w->window_end_vpos
15057 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15060 w->window_end_valid = Qnil;
15061 w->desired_matrix->no_scrolling_p = 1;
15063 #if GLYPH_DEBUG
15064 debug_method_add (w, "try_window_reusing_current_matrix 2");
15065 #endif
15066 return 1;
15069 return 0;
15074 /************************************************************************
15075 Window redisplay reusing current matrix when buffer has changed
15076 ************************************************************************/
15078 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15079 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15080 EMACS_INT *, EMACS_INT *);
15081 static struct glyph_row *
15082 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15083 struct glyph_row *);
15086 /* Return the last row in MATRIX displaying text. If row START is
15087 non-null, start searching with that row. IT gives the dimensions
15088 of the display. Value is null if matrix is empty; otherwise it is
15089 a pointer to the row found. */
15091 static struct glyph_row *
15092 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15093 struct glyph_row *start)
15095 struct glyph_row *row, *row_found;
15097 /* Set row_found to the last row in IT->w's current matrix
15098 displaying text. The loop looks funny but think of partially
15099 visible lines. */
15100 row_found = NULL;
15101 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15102 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15104 xassert (row->enabled_p);
15105 row_found = row;
15106 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15107 break;
15108 ++row;
15111 return row_found;
15115 /* Return the last row in the current matrix of W that is not affected
15116 by changes at the start of current_buffer that occurred since W's
15117 current matrix was built. Value is null if no such row exists.
15119 BEG_UNCHANGED us the number of characters unchanged at the start of
15120 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15121 first changed character in current_buffer. Characters at positions <
15122 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15123 when the current matrix was built. */
15125 static struct glyph_row *
15126 find_last_unchanged_at_beg_row (struct window *w)
15128 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15129 struct glyph_row *row;
15130 struct glyph_row *row_found = NULL;
15131 int yb = window_text_bottom_y (w);
15133 /* Find the last row displaying unchanged text. */
15134 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15135 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15136 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15137 ++row)
15139 if (/* If row ends before first_changed_pos, it is unchanged,
15140 except in some case. */
15141 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15142 /* When row ends in ZV and we write at ZV it is not
15143 unchanged. */
15144 && !row->ends_at_zv_p
15145 /* When first_changed_pos is the end of a continued line,
15146 row is not unchanged because it may be no longer
15147 continued. */
15148 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15149 && (row->continued_p
15150 || row->exact_window_width_line_p)))
15151 row_found = row;
15153 /* Stop if last visible row. */
15154 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15155 break;
15158 return row_found;
15162 /* Find the first glyph row in the current matrix of W that is not
15163 affected by changes at the end of current_buffer since the
15164 time W's current matrix was built.
15166 Return in *DELTA the number of chars by which buffer positions in
15167 unchanged text at the end of current_buffer must be adjusted.
15169 Return in *DELTA_BYTES the corresponding number of bytes.
15171 Value is null if no such row exists, i.e. all rows are affected by
15172 changes. */
15174 static struct glyph_row *
15175 find_first_unchanged_at_end_row (struct window *w,
15176 EMACS_INT *delta, EMACS_INT *delta_bytes)
15178 struct glyph_row *row;
15179 struct glyph_row *row_found = NULL;
15181 *delta = *delta_bytes = 0;
15183 /* Display must not have been paused, otherwise the current matrix
15184 is not up to date. */
15185 eassert (!NILP (w->window_end_valid));
15187 /* A value of window_end_pos >= END_UNCHANGED means that the window
15188 end is in the range of changed text. If so, there is no
15189 unchanged row at the end of W's current matrix. */
15190 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15191 return NULL;
15193 /* Set row to the last row in W's current matrix displaying text. */
15194 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15196 /* If matrix is entirely empty, no unchanged row exists. */
15197 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15199 /* The value of row is the last glyph row in the matrix having a
15200 meaningful buffer position in it. The end position of row
15201 corresponds to window_end_pos. This allows us to translate
15202 buffer positions in the current matrix to current buffer
15203 positions for characters not in changed text. */
15204 EMACS_INT Z_old =
15205 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15206 EMACS_INT Z_BYTE_old =
15207 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15208 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15209 struct glyph_row *first_text_row
15210 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15212 *delta = Z - Z_old;
15213 *delta_bytes = Z_BYTE - Z_BYTE_old;
15215 /* Set last_unchanged_pos to the buffer position of the last
15216 character in the buffer that has not been changed. Z is the
15217 index + 1 of the last character in current_buffer, i.e. by
15218 subtracting END_UNCHANGED we get the index of the last
15219 unchanged character, and we have to add BEG to get its buffer
15220 position. */
15221 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15222 last_unchanged_pos_old = last_unchanged_pos - *delta;
15224 /* Search backward from ROW for a row displaying a line that
15225 starts at a minimum position >= last_unchanged_pos_old. */
15226 for (; row > first_text_row; --row)
15228 /* This used to abort, but it can happen.
15229 It is ok to just stop the search instead here. KFS. */
15230 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15231 break;
15233 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15234 row_found = row;
15238 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15240 return row_found;
15244 /* Make sure that glyph rows in the current matrix of window W
15245 reference the same glyph memory as corresponding rows in the
15246 frame's frame matrix. This function is called after scrolling W's
15247 current matrix on a terminal frame in try_window_id and
15248 try_window_reusing_current_matrix. */
15250 static void
15251 sync_frame_with_window_matrix_rows (struct window *w)
15253 struct frame *f = XFRAME (w->frame);
15254 struct glyph_row *window_row, *window_row_end, *frame_row;
15256 /* Preconditions: W must be a leaf window and full-width. Its frame
15257 must have a frame matrix. */
15258 xassert (NILP (w->hchild) && NILP (w->vchild));
15259 xassert (WINDOW_FULL_WIDTH_P (w));
15260 xassert (!FRAME_WINDOW_P (f));
15262 /* If W is a full-width window, glyph pointers in W's current matrix
15263 have, by definition, to be the same as glyph pointers in the
15264 corresponding frame matrix. Note that frame matrices have no
15265 marginal areas (see build_frame_matrix). */
15266 window_row = w->current_matrix->rows;
15267 window_row_end = window_row + w->current_matrix->nrows;
15268 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15269 while (window_row < window_row_end)
15271 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15272 struct glyph *end = window_row->glyphs[LAST_AREA];
15274 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15275 frame_row->glyphs[TEXT_AREA] = start;
15276 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15277 frame_row->glyphs[LAST_AREA] = end;
15279 /* Disable frame rows whose corresponding window rows have
15280 been disabled in try_window_id. */
15281 if (!window_row->enabled_p)
15282 frame_row->enabled_p = 0;
15284 ++window_row, ++frame_row;
15289 /* Find the glyph row in window W containing CHARPOS. Consider all
15290 rows between START and END (not inclusive). END null means search
15291 all rows to the end of the display area of W. Value is the row
15292 containing CHARPOS or null. */
15294 struct glyph_row *
15295 row_containing_pos (struct window *w, EMACS_INT charpos,
15296 struct glyph_row *start, struct glyph_row *end, int dy)
15298 struct glyph_row *row = start;
15299 struct glyph_row *best_row = NULL;
15300 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15301 int last_y;
15303 /* If we happen to start on a header-line, skip that. */
15304 if (row->mode_line_p)
15305 ++row;
15307 if ((end && row >= end) || !row->enabled_p)
15308 return NULL;
15310 last_y = window_text_bottom_y (w) - dy;
15312 while (1)
15314 /* Give up if we have gone too far. */
15315 if (end && row >= end)
15316 return NULL;
15317 /* This formerly returned if they were equal.
15318 I think that both quantities are of a "last plus one" type;
15319 if so, when they are equal, the row is within the screen. -- rms. */
15320 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15321 return NULL;
15323 /* If it is in this row, return this row. */
15324 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15325 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15326 /* The end position of a row equals the start
15327 position of the next row. If CHARPOS is there, we
15328 would rather display it in the next line, except
15329 when this line ends in ZV. */
15330 && !row->ends_at_zv_p
15331 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15332 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15334 struct glyph *g;
15336 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15337 return row;
15338 /* In bidi-reordered rows, there could be several rows
15339 occluding point. We need to find the one which fits
15340 CHARPOS the best. */
15341 for (g = row->glyphs[TEXT_AREA];
15342 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15343 g++)
15345 if (!STRINGP (g->object))
15347 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15349 mindif = eabs (g->charpos - charpos);
15350 best_row = row;
15355 else if (best_row)
15356 return best_row;
15357 ++row;
15362 /* Try to redisplay window W by reusing its existing display. W's
15363 current matrix must be up to date when this function is called,
15364 i.e. window_end_valid must not be nil.
15366 Value is
15368 1 if display has been updated
15369 0 if otherwise unsuccessful
15370 -1 if redisplay with same window start is known not to succeed
15372 The following steps are performed:
15374 1. Find the last row in the current matrix of W that is not
15375 affected by changes at the start of current_buffer. If no such row
15376 is found, give up.
15378 2. Find the first row in W's current matrix that is not affected by
15379 changes at the end of current_buffer. Maybe there is no such row.
15381 3. Display lines beginning with the row + 1 found in step 1 to the
15382 row found in step 2 or, if step 2 didn't find a row, to the end of
15383 the window.
15385 4. If cursor is not known to appear on the window, give up.
15387 5. If display stopped at the row found in step 2, scroll the
15388 display and current matrix as needed.
15390 6. Maybe display some lines at the end of W, if we must. This can
15391 happen under various circumstances, like a partially visible line
15392 becoming fully visible, or because newly displayed lines are displayed
15393 in smaller font sizes.
15395 7. Update W's window end information. */
15397 static int
15398 try_window_id (struct window *w)
15400 struct frame *f = XFRAME (w->frame);
15401 struct glyph_matrix *current_matrix = w->current_matrix;
15402 struct glyph_matrix *desired_matrix = w->desired_matrix;
15403 struct glyph_row *last_unchanged_at_beg_row;
15404 struct glyph_row *first_unchanged_at_end_row;
15405 struct glyph_row *row;
15406 struct glyph_row *bottom_row;
15407 int bottom_vpos;
15408 struct it it;
15409 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15410 int dvpos, dy;
15411 struct text_pos start_pos;
15412 struct run run;
15413 int first_unchanged_at_end_vpos = 0;
15414 struct glyph_row *last_text_row, *last_text_row_at_end;
15415 struct text_pos start;
15416 EMACS_INT first_changed_charpos, last_changed_charpos;
15418 #if GLYPH_DEBUG
15419 if (inhibit_try_window_id)
15420 return 0;
15421 #endif
15423 /* This is handy for debugging. */
15424 #if 0
15425 #define GIVE_UP(X) \
15426 do { \
15427 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15428 return 0; \
15429 } while (0)
15430 #else
15431 #define GIVE_UP(X) return 0
15432 #endif
15434 SET_TEXT_POS_FROM_MARKER (start, w->start);
15436 /* Don't use this for mini-windows because these can show
15437 messages and mini-buffers, and we don't handle that here. */
15438 if (MINI_WINDOW_P (w))
15439 GIVE_UP (1);
15441 /* This flag is used to prevent redisplay optimizations. */
15442 if (windows_or_buffers_changed || cursor_type_changed)
15443 GIVE_UP (2);
15445 /* Verify that narrowing has not changed.
15446 Also verify that we were not told to prevent redisplay optimizations.
15447 It would be nice to further
15448 reduce the number of cases where this prevents try_window_id. */
15449 if (current_buffer->clip_changed
15450 || current_buffer->prevent_redisplay_optimizations_p)
15451 GIVE_UP (3);
15453 /* Window must either use window-based redisplay or be full width. */
15454 if (!FRAME_WINDOW_P (f)
15455 && (!FRAME_LINE_INS_DEL_OK (f)
15456 || !WINDOW_FULL_WIDTH_P (w)))
15457 GIVE_UP (4);
15459 /* Give up if point is known NOT to appear in W. */
15460 if (PT < CHARPOS (start))
15461 GIVE_UP (5);
15463 /* Another way to prevent redisplay optimizations. */
15464 if (XFASTINT (w->last_modified) == 0)
15465 GIVE_UP (6);
15467 /* Verify that window is not hscrolled. */
15468 if (XFASTINT (w->hscroll) != 0)
15469 GIVE_UP (7);
15471 /* Verify that display wasn't paused. */
15472 if (NILP (w->window_end_valid))
15473 GIVE_UP (8);
15475 /* Can't use this if highlighting a region because a cursor movement
15476 will do more than just set the cursor. */
15477 if (!NILP (Vtransient_mark_mode)
15478 && !NILP (current_buffer->mark_active))
15479 GIVE_UP (9);
15481 /* Likewise if highlighting trailing whitespace. */
15482 if (!NILP (Vshow_trailing_whitespace))
15483 GIVE_UP (11);
15485 /* Likewise if showing a region. */
15486 if (!NILP (w->region_showing))
15487 GIVE_UP (10);
15489 /* Can't use this if overlay arrow position and/or string have
15490 changed. */
15491 if (overlay_arrows_changed_p ())
15492 GIVE_UP (12);
15494 /* When word-wrap is on, adding a space to the first word of a
15495 wrapped line can change the wrap position, altering the line
15496 above it. It might be worthwhile to handle this more
15497 intelligently, but for now just redisplay from scratch. */
15498 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15499 GIVE_UP (21);
15501 /* Under bidi reordering, adding or deleting a character in the
15502 beginning of a paragraph, before the first strong directional
15503 character, can change the base direction of the paragraph (unless
15504 the buffer specifies a fixed paragraph direction), which will
15505 require to redisplay the whole paragraph. It might be worthwhile
15506 to find the paragraph limits and widen the range of redisplayed
15507 lines to that, but for now just give up this optimization and
15508 redisplay from scratch. */
15509 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15510 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15511 GIVE_UP (22);
15513 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15514 only if buffer has really changed. The reason is that the gap is
15515 initially at Z for freshly visited files. The code below would
15516 set end_unchanged to 0 in that case. */
15517 if (MODIFF > SAVE_MODIFF
15518 /* This seems to happen sometimes after saving a buffer. */
15519 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15521 if (GPT - BEG < BEG_UNCHANGED)
15522 BEG_UNCHANGED = GPT - BEG;
15523 if (Z - GPT < END_UNCHANGED)
15524 END_UNCHANGED = Z - GPT;
15527 /* The position of the first and last character that has been changed. */
15528 first_changed_charpos = BEG + BEG_UNCHANGED;
15529 last_changed_charpos = Z - END_UNCHANGED;
15531 /* If window starts after a line end, and the last change is in
15532 front of that newline, then changes don't affect the display.
15533 This case happens with stealth-fontification. Note that although
15534 the display is unchanged, glyph positions in the matrix have to
15535 be adjusted, of course. */
15536 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15537 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15538 && ((last_changed_charpos < CHARPOS (start)
15539 && CHARPOS (start) == BEGV)
15540 || (last_changed_charpos < CHARPOS (start) - 1
15541 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15543 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15544 struct glyph_row *r0;
15546 /* Compute how many chars/bytes have been added to or removed
15547 from the buffer. */
15548 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15549 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15550 delta = Z - Z_old;
15551 delta_bytes = Z_BYTE - Z_BYTE_old;
15553 /* Give up if PT is not in the window. Note that it already has
15554 been checked at the start of try_window_id that PT is not in
15555 front of the window start. */
15556 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15557 GIVE_UP (13);
15559 /* If window start is unchanged, we can reuse the whole matrix
15560 as is, after adjusting glyph positions. No need to compute
15561 the window end again, since its offset from Z hasn't changed. */
15562 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15563 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15564 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15565 /* PT must not be in a partially visible line. */
15566 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15567 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15569 /* Adjust positions in the glyph matrix. */
15570 if (delta || delta_bytes)
15572 struct glyph_row *r1
15573 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15574 increment_matrix_positions (w->current_matrix,
15575 MATRIX_ROW_VPOS (r0, current_matrix),
15576 MATRIX_ROW_VPOS (r1, current_matrix),
15577 delta, delta_bytes);
15580 /* Set the cursor. */
15581 row = row_containing_pos (w, PT, r0, NULL, 0);
15582 if (row)
15583 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15584 else
15585 abort ();
15586 return 1;
15590 /* Handle the case that changes are all below what is displayed in
15591 the window, and that PT is in the window. This shortcut cannot
15592 be taken if ZV is visible in the window, and text has been added
15593 there that is visible in the window. */
15594 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15595 /* ZV is not visible in the window, or there are no
15596 changes at ZV, actually. */
15597 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15598 || first_changed_charpos == last_changed_charpos))
15600 struct glyph_row *r0;
15602 /* Give up if PT is not in the window. Note that it already has
15603 been checked at the start of try_window_id that PT is not in
15604 front of the window start. */
15605 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15606 GIVE_UP (14);
15608 /* If window start is unchanged, we can reuse the whole matrix
15609 as is, without changing glyph positions since no text has
15610 been added/removed in front of the window end. */
15611 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15612 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15613 /* PT must not be in a partially visible line. */
15614 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15615 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15617 /* We have to compute the window end anew since text
15618 could have been added/removed after it. */
15619 w->window_end_pos
15620 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15621 w->window_end_bytepos
15622 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15624 /* Set the cursor. */
15625 row = row_containing_pos (w, PT, r0, NULL, 0);
15626 if (row)
15627 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15628 else
15629 abort ();
15630 return 2;
15634 /* Give up if window start is in the changed area.
15636 The condition used to read
15638 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15640 but why that was tested escapes me at the moment. */
15641 if (CHARPOS (start) >= first_changed_charpos
15642 && CHARPOS (start) <= last_changed_charpos)
15643 GIVE_UP (15);
15645 /* Check that window start agrees with the start of the first glyph
15646 row in its current matrix. Check this after we know the window
15647 start is not in changed text, otherwise positions would not be
15648 comparable. */
15649 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15650 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15651 GIVE_UP (16);
15653 /* Give up if the window ends in strings. Overlay strings
15654 at the end are difficult to handle, so don't try. */
15655 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15656 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15657 GIVE_UP (20);
15659 /* Compute the position at which we have to start displaying new
15660 lines. Some of the lines at the top of the window might be
15661 reusable because they are not displaying changed text. Find the
15662 last row in W's current matrix not affected by changes at the
15663 start of current_buffer. Value is null if changes start in the
15664 first line of window. */
15665 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15666 if (last_unchanged_at_beg_row)
15668 /* Avoid starting to display in the moddle of a character, a TAB
15669 for instance. This is easier than to set up the iterator
15670 exactly, and it's not a frequent case, so the additional
15671 effort wouldn't really pay off. */
15672 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15673 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15674 && last_unchanged_at_beg_row > w->current_matrix->rows)
15675 --last_unchanged_at_beg_row;
15677 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15678 GIVE_UP (17);
15680 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15681 GIVE_UP (18);
15682 start_pos = it.current.pos;
15684 /* Start displaying new lines in the desired matrix at the same
15685 vpos we would use in the current matrix, i.e. below
15686 last_unchanged_at_beg_row. */
15687 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15688 current_matrix);
15689 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15690 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15692 xassert (it.hpos == 0 && it.current_x == 0);
15694 else
15696 /* There are no reusable lines at the start of the window.
15697 Start displaying in the first text line. */
15698 start_display (&it, w, start);
15699 it.vpos = it.first_vpos;
15700 start_pos = it.current.pos;
15703 /* Find the first row that is not affected by changes at the end of
15704 the buffer. Value will be null if there is no unchanged row, in
15705 which case we must redisplay to the end of the window. delta
15706 will be set to the value by which buffer positions beginning with
15707 first_unchanged_at_end_row have to be adjusted due to text
15708 changes. */
15709 first_unchanged_at_end_row
15710 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15711 IF_DEBUG (debug_delta = delta);
15712 IF_DEBUG (debug_delta_bytes = delta_bytes);
15714 /* Set stop_pos to the buffer position up to which we will have to
15715 display new lines. If first_unchanged_at_end_row != NULL, this
15716 is the buffer position of the start of the line displayed in that
15717 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15718 that we don't stop at a buffer position. */
15719 stop_pos = 0;
15720 if (first_unchanged_at_end_row)
15722 xassert (last_unchanged_at_beg_row == NULL
15723 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15725 /* If this is a continuation line, move forward to the next one
15726 that isn't. Changes in lines above affect this line.
15727 Caution: this may move first_unchanged_at_end_row to a row
15728 not displaying text. */
15729 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15730 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15731 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15732 < it.last_visible_y))
15733 ++first_unchanged_at_end_row;
15735 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15736 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15737 >= it.last_visible_y))
15738 first_unchanged_at_end_row = NULL;
15739 else
15741 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15742 + delta);
15743 first_unchanged_at_end_vpos
15744 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15745 xassert (stop_pos >= Z - END_UNCHANGED);
15748 else if (last_unchanged_at_beg_row == NULL)
15749 GIVE_UP (19);
15752 #if GLYPH_DEBUG
15754 /* Either there is no unchanged row at the end, or the one we have
15755 now displays text. This is a necessary condition for the window
15756 end pos calculation at the end of this function. */
15757 xassert (first_unchanged_at_end_row == NULL
15758 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15760 debug_last_unchanged_at_beg_vpos
15761 = (last_unchanged_at_beg_row
15762 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15763 : -1);
15764 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15766 #endif /* GLYPH_DEBUG != 0 */
15769 /* Display new lines. Set last_text_row to the last new line
15770 displayed which has text on it, i.e. might end up as being the
15771 line where the window_end_vpos is. */
15772 w->cursor.vpos = -1;
15773 last_text_row = NULL;
15774 overlay_arrow_seen = 0;
15775 while (it.current_y < it.last_visible_y
15776 && !fonts_changed_p
15777 && (first_unchanged_at_end_row == NULL
15778 || IT_CHARPOS (it) < stop_pos))
15780 if (display_line (&it))
15781 last_text_row = it.glyph_row - 1;
15784 if (fonts_changed_p)
15785 return -1;
15788 /* Compute differences in buffer positions, y-positions etc. for
15789 lines reused at the bottom of the window. Compute what we can
15790 scroll. */
15791 if (first_unchanged_at_end_row
15792 /* No lines reused because we displayed everything up to the
15793 bottom of the window. */
15794 && it.current_y < it.last_visible_y)
15796 dvpos = (it.vpos
15797 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15798 current_matrix));
15799 dy = it.current_y - first_unchanged_at_end_row->y;
15800 run.current_y = first_unchanged_at_end_row->y;
15801 run.desired_y = run.current_y + dy;
15802 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15804 else
15806 delta = delta_bytes = dvpos = dy
15807 = run.current_y = run.desired_y = run.height = 0;
15808 first_unchanged_at_end_row = NULL;
15810 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15813 /* Find the cursor if not already found. We have to decide whether
15814 PT will appear on this window (it sometimes doesn't, but this is
15815 not a very frequent case.) This decision has to be made before
15816 the current matrix is altered. A value of cursor.vpos < 0 means
15817 that PT is either in one of the lines beginning at
15818 first_unchanged_at_end_row or below the window. Don't care for
15819 lines that might be displayed later at the window end; as
15820 mentioned, this is not a frequent case. */
15821 if (w->cursor.vpos < 0)
15823 /* Cursor in unchanged rows at the top? */
15824 if (PT < CHARPOS (start_pos)
15825 && last_unchanged_at_beg_row)
15827 row = row_containing_pos (w, PT,
15828 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15829 last_unchanged_at_beg_row + 1, 0);
15830 if (row)
15831 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15834 /* Start from first_unchanged_at_end_row looking for PT. */
15835 else if (first_unchanged_at_end_row)
15837 row = row_containing_pos (w, PT - delta,
15838 first_unchanged_at_end_row, NULL, 0);
15839 if (row)
15840 set_cursor_from_row (w, row, w->current_matrix, delta,
15841 delta_bytes, dy, dvpos);
15844 /* Give up if cursor was not found. */
15845 if (w->cursor.vpos < 0)
15847 clear_glyph_matrix (w->desired_matrix);
15848 return -1;
15852 /* Don't let the cursor end in the scroll margins. */
15854 int this_scroll_margin, cursor_height;
15856 this_scroll_margin = max (0, scroll_margin);
15857 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15858 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15859 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15861 if ((w->cursor.y < this_scroll_margin
15862 && CHARPOS (start) > BEGV)
15863 /* Old redisplay didn't take scroll margin into account at the bottom,
15864 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15865 || (w->cursor.y + (make_cursor_line_fully_visible_p
15866 ? cursor_height + this_scroll_margin
15867 : 1)) > it.last_visible_y)
15869 w->cursor.vpos = -1;
15870 clear_glyph_matrix (w->desired_matrix);
15871 return -1;
15875 /* Scroll the display. Do it before changing the current matrix so
15876 that xterm.c doesn't get confused about where the cursor glyph is
15877 found. */
15878 if (dy && run.height)
15880 update_begin (f);
15882 if (FRAME_WINDOW_P (f))
15884 FRAME_RIF (f)->update_window_begin_hook (w);
15885 FRAME_RIF (f)->clear_window_mouse_face (w);
15886 FRAME_RIF (f)->scroll_run_hook (w, &run);
15887 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15889 else
15891 /* Terminal frame. In this case, dvpos gives the number of
15892 lines to scroll by; dvpos < 0 means scroll up. */
15893 int first_unchanged_at_end_vpos
15894 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15895 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15896 int end = (WINDOW_TOP_EDGE_LINE (w)
15897 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15898 + window_internal_height (w));
15900 /* Perform the operation on the screen. */
15901 if (dvpos > 0)
15903 /* Scroll last_unchanged_at_beg_row to the end of the
15904 window down dvpos lines. */
15905 set_terminal_window (f, end);
15907 /* On dumb terminals delete dvpos lines at the end
15908 before inserting dvpos empty lines. */
15909 if (!FRAME_SCROLL_REGION_OK (f))
15910 ins_del_lines (f, end - dvpos, -dvpos);
15912 /* Insert dvpos empty lines in front of
15913 last_unchanged_at_beg_row. */
15914 ins_del_lines (f, from, dvpos);
15916 else if (dvpos < 0)
15918 /* Scroll up last_unchanged_at_beg_vpos to the end of
15919 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15920 set_terminal_window (f, end);
15922 /* Delete dvpos lines in front of
15923 last_unchanged_at_beg_vpos. ins_del_lines will set
15924 the cursor to the given vpos and emit |dvpos| delete
15925 line sequences. */
15926 ins_del_lines (f, from + dvpos, dvpos);
15928 /* On a dumb terminal insert dvpos empty lines at the
15929 end. */
15930 if (!FRAME_SCROLL_REGION_OK (f))
15931 ins_del_lines (f, end + dvpos, -dvpos);
15934 set_terminal_window (f, 0);
15937 update_end (f);
15940 /* Shift reused rows of the current matrix to the right position.
15941 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15942 text. */
15943 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15944 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15945 if (dvpos < 0)
15947 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15948 bottom_vpos, dvpos);
15949 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15950 bottom_vpos, 0);
15952 else if (dvpos > 0)
15954 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15955 bottom_vpos, dvpos);
15956 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15957 first_unchanged_at_end_vpos + dvpos, 0);
15960 /* For frame-based redisplay, make sure that current frame and window
15961 matrix are in sync with respect to glyph memory. */
15962 if (!FRAME_WINDOW_P (f))
15963 sync_frame_with_window_matrix_rows (w);
15965 /* Adjust buffer positions in reused rows. */
15966 if (delta || delta_bytes)
15967 increment_matrix_positions (current_matrix,
15968 first_unchanged_at_end_vpos + dvpos,
15969 bottom_vpos, delta, delta_bytes);
15971 /* Adjust Y positions. */
15972 if (dy)
15973 shift_glyph_matrix (w, current_matrix,
15974 first_unchanged_at_end_vpos + dvpos,
15975 bottom_vpos, dy);
15977 if (first_unchanged_at_end_row)
15979 first_unchanged_at_end_row += dvpos;
15980 if (first_unchanged_at_end_row->y >= it.last_visible_y
15981 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15982 first_unchanged_at_end_row = NULL;
15985 /* If scrolling up, there may be some lines to display at the end of
15986 the window. */
15987 last_text_row_at_end = NULL;
15988 if (dy < 0)
15990 /* Scrolling up can leave for example a partially visible line
15991 at the end of the window to be redisplayed. */
15992 /* Set last_row to the glyph row in the current matrix where the
15993 window end line is found. It has been moved up or down in
15994 the matrix by dvpos. */
15995 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15996 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15998 /* If last_row is the window end line, it should display text. */
15999 xassert (last_row->displays_text_p);
16001 /* If window end line was partially visible before, begin
16002 displaying at that line. Otherwise begin displaying with the
16003 line following it. */
16004 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16006 init_to_row_start (&it, w, last_row);
16007 it.vpos = last_vpos;
16008 it.current_y = last_row->y;
16010 else
16012 init_to_row_end (&it, w, last_row);
16013 it.vpos = 1 + last_vpos;
16014 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16015 ++last_row;
16018 /* We may start in a continuation line. If so, we have to
16019 get the right continuation_lines_width and current_x. */
16020 it.continuation_lines_width = last_row->continuation_lines_width;
16021 it.hpos = it.current_x = 0;
16023 /* Display the rest of the lines at the window end. */
16024 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16025 while (it.current_y < it.last_visible_y
16026 && !fonts_changed_p)
16028 /* Is it always sure that the display agrees with lines in
16029 the current matrix? I don't think so, so we mark rows
16030 displayed invalid in the current matrix by setting their
16031 enabled_p flag to zero. */
16032 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16033 if (display_line (&it))
16034 last_text_row_at_end = it.glyph_row - 1;
16038 /* Update window_end_pos and window_end_vpos. */
16039 if (first_unchanged_at_end_row
16040 && !last_text_row_at_end)
16042 /* Window end line if one of the preserved rows from the current
16043 matrix. Set row to the last row displaying text in current
16044 matrix starting at first_unchanged_at_end_row, after
16045 scrolling. */
16046 xassert (first_unchanged_at_end_row->displays_text_p);
16047 row = find_last_row_displaying_text (w->current_matrix, &it,
16048 first_unchanged_at_end_row);
16049 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16051 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16052 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16053 w->window_end_vpos
16054 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16055 xassert (w->window_end_bytepos >= 0);
16056 IF_DEBUG (debug_method_add (w, "A"));
16058 else if (last_text_row_at_end)
16060 w->window_end_pos
16061 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16062 w->window_end_bytepos
16063 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16064 w->window_end_vpos
16065 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16066 xassert (w->window_end_bytepos >= 0);
16067 IF_DEBUG (debug_method_add (w, "B"));
16069 else if (last_text_row)
16071 /* We have displayed either to the end of the window or at the
16072 end of the window, i.e. the last row with text is to be found
16073 in the desired matrix. */
16074 w->window_end_pos
16075 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16076 w->window_end_bytepos
16077 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16078 w->window_end_vpos
16079 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16080 xassert (w->window_end_bytepos >= 0);
16082 else if (first_unchanged_at_end_row == NULL
16083 && last_text_row == NULL
16084 && last_text_row_at_end == NULL)
16086 /* Displayed to end of window, but no line containing text was
16087 displayed. Lines were deleted at the end of the window. */
16088 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16089 int vpos = XFASTINT (w->window_end_vpos);
16090 struct glyph_row *current_row = current_matrix->rows + vpos;
16091 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16093 for (row = NULL;
16094 row == NULL && vpos >= first_vpos;
16095 --vpos, --current_row, --desired_row)
16097 if (desired_row->enabled_p)
16099 if (desired_row->displays_text_p)
16100 row = desired_row;
16102 else if (current_row->displays_text_p)
16103 row = current_row;
16106 xassert (row != NULL);
16107 w->window_end_vpos = make_number (vpos + 1);
16108 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16109 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16110 xassert (w->window_end_bytepos >= 0);
16111 IF_DEBUG (debug_method_add (w, "C"));
16113 else
16114 abort ();
16116 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16117 debug_end_vpos = XFASTINT (w->window_end_vpos));
16119 /* Record that display has not been completed. */
16120 w->window_end_valid = Qnil;
16121 w->desired_matrix->no_scrolling_p = 1;
16122 return 3;
16124 #undef GIVE_UP
16129 /***********************************************************************
16130 More debugging support
16131 ***********************************************************************/
16133 #if GLYPH_DEBUG
16135 void dump_glyph_row (struct glyph_row *, int, int);
16136 void dump_glyph_matrix (struct glyph_matrix *, int);
16137 void dump_glyph (struct glyph_row *, struct glyph *, int);
16140 /* Dump the contents of glyph matrix MATRIX on stderr.
16142 GLYPHS 0 means don't show glyph contents.
16143 GLYPHS 1 means show glyphs in short form
16144 GLYPHS > 1 means show glyphs in long form. */
16146 void
16147 dump_glyph_matrix (matrix, glyphs)
16148 struct glyph_matrix *matrix;
16149 int glyphs;
16151 int i;
16152 for (i = 0; i < matrix->nrows; ++i)
16153 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16157 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16158 the glyph row and area where the glyph comes from. */
16160 void
16161 dump_glyph (row, glyph, area)
16162 struct glyph_row *row;
16163 struct glyph *glyph;
16164 int area;
16166 if (glyph->type == CHAR_GLYPH)
16168 fprintf (stderr,
16169 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16170 glyph - row->glyphs[TEXT_AREA],
16171 'C',
16172 glyph->charpos,
16173 (BUFFERP (glyph->object)
16174 ? 'B'
16175 : (STRINGP (glyph->object)
16176 ? 'S'
16177 : '-')),
16178 glyph->pixel_width,
16179 glyph->u.ch,
16180 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16181 ? glyph->u.ch
16182 : '.'),
16183 glyph->face_id,
16184 glyph->left_box_line_p,
16185 glyph->right_box_line_p);
16187 else if (glyph->type == STRETCH_GLYPH)
16189 fprintf (stderr,
16190 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16191 glyph - row->glyphs[TEXT_AREA],
16192 'S',
16193 glyph->charpos,
16194 (BUFFERP (glyph->object)
16195 ? 'B'
16196 : (STRINGP (glyph->object)
16197 ? 'S'
16198 : '-')),
16199 glyph->pixel_width,
16201 '.',
16202 glyph->face_id,
16203 glyph->left_box_line_p,
16204 glyph->right_box_line_p);
16206 else if (glyph->type == IMAGE_GLYPH)
16208 fprintf (stderr,
16209 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16210 glyph - row->glyphs[TEXT_AREA],
16211 'I',
16212 glyph->charpos,
16213 (BUFFERP (glyph->object)
16214 ? 'B'
16215 : (STRINGP (glyph->object)
16216 ? 'S'
16217 : '-')),
16218 glyph->pixel_width,
16219 glyph->u.img_id,
16220 '.',
16221 glyph->face_id,
16222 glyph->left_box_line_p,
16223 glyph->right_box_line_p);
16225 else if (glyph->type == COMPOSITE_GLYPH)
16227 fprintf (stderr,
16228 " %5d %4c %6d %c %3d 0x%05x",
16229 glyph - row->glyphs[TEXT_AREA],
16230 '+',
16231 glyph->charpos,
16232 (BUFFERP (glyph->object)
16233 ? 'B'
16234 : (STRINGP (glyph->object)
16235 ? 'S'
16236 : '-')),
16237 glyph->pixel_width,
16238 glyph->u.cmp.id);
16239 if (glyph->u.cmp.automatic)
16240 fprintf (stderr,
16241 "[%d-%d]",
16242 glyph->slice.cmp.from, glyph->slice.cmp.to);
16243 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16244 glyph->face_id,
16245 glyph->left_box_line_p,
16246 glyph->right_box_line_p);
16251 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16252 GLYPHS 0 means don't show glyph contents.
16253 GLYPHS 1 means show glyphs in short form
16254 GLYPHS > 1 means show glyphs in long form. */
16256 void
16257 dump_glyph_row (row, vpos, glyphs)
16258 struct glyph_row *row;
16259 int vpos, glyphs;
16261 if (glyphs != 1)
16263 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16264 fprintf (stderr, "======================================================================\n");
16266 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16267 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16268 vpos,
16269 MATRIX_ROW_START_CHARPOS (row),
16270 MATRIX_ROW_END_CHARPOS (row),
16271 row->used[TEXT_AREA],
16272 row->contains_overlapping_glyphs_p,
16273 row->enabled_p,
16274 row->truncated_on_left_p,
16275 row->truncated_on_right_p,
16276 row->continued_p,
16277 MATRIX_ROW_CONTINUATION_LINE_P (row),
16278 row->displays_text_p,
16279 row->ends_at_zv_p,
16280 row->fill_line_p,
16281 row->ends_in_middle_of_char_p,
16282 row->starts_in_middle_of_char_p,
16283 row->mouse_face_p,
16284 row->x,
16285 row->y,
16286 row->pixel_width,
16287 row->height,
16288 row->visible_height,
16289 row->ascent,
16290 row->phys_ascent);
16291 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16292 row->end.overlay_string_index,
16293 row->continuation_lines_width);
16294 fprintf (stderr, "%9d %5d\n",
16295 CHARPOS (row->start.string_pos),
16296 CHARPOS (row->end.string_pos));
16297 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16298 row->end.dpvec_index);
16301 if (glyphs > 1)
16303 int area;
16305 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16307 struct glyph *glyph = row->glyphs[area];
16308 struct glyph *glyph_end = glyph + row->used[area];
16310 /* Glyph for a line end in text. */
16311 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16312 ++glyph_end;
16314 if (glyph < glyph_end)
16315 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16317 for (; glyph < glyph_end; ++glyph)
16318 dump_glyph (row, glyph, area);
16321 else if (glyphs == 1)
16323 int area;
16325 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16327 char *s = (char *) alloca (row->used[area] + 1);
16328 int i;
16330 for (i = 0; i < row->used[area]; ++i)
16332 struct glyph *glyph = row->glyphs[area] + i;
16333 if (glyph->type == CHAR_GLYPH
16334 && glyph->u.ch < 0x80
16335 && glyph->u.ch >= ' ')
16336 s[i] = glyph->u.ch;
16337 else
16338 s[i] = '.';
16341 s[i] = '\0';
16342 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16348 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16349 Sdump_glyph_matrix, 0, 1, "p",
16350 doc: /* Dump the current matrix of the selected window to stderr.
16351 Shows contents of glyph row structures. With non-nil
16352 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16353 glyphs in short form, otherwise show glyphs in long form. */)
16354 (Lisp_Object glyphs)
16356 struct window *w = XWINDOW (selected_window);
16357 struct buffer *buffer = XBUFFER (w->buffer);
16359 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16360 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16361 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16362 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16363 fprintf (stderr, "=============================================\n");
16364 dump_glyph_matrix (w->current_matrix,
16365 NILP (glyphs) ? 0 : XINT (glyphs));
16366 return Qnil;
16370 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16371 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16372 (void)
16374 struct frame *f = XFRAME (selected_frame);
16375 dump_glyph_matrix (f->current_matrix, 1);
16376 return Qnil;
16380 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16381 doc: /* Dump glyph row ROW to stderr.
16382 GLYPH 0 means don't dump glyphs.
16383 GLYPH 1 means dump glyphs in short form.
16384 GLYPH > 1 or omitted means dump glyphs in long form. */)
16385 (Lisp_Object row, Lisp_Object glyphs)
16387 struct glyph_matrix *matrix;
16388 int vpos;
16390 CHECK_NUMBER (row);
16391 matrix = XWINDOW (selected_window)->current_matrix;
16392 vpos = XINT (row);
16393 if (vpos >= 0 && vpos < matrix->nrows)
16394 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16395 vpos,
16396 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16397 return Qnil;
16401 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16402 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16403 GLYPH 0 means don't dump glyphs.
16404 GLYPH 1 means dump glyphs in short form.
16405 GLYPH > 1 or omitted means dump glyphs in long form. */)
16406 (Lisp_Object row, Lisp_Object glyphs)
16408 struct frame *sf = SELECTED_FRAME ();
16409 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16410 int vpos;
16412 CHECK_NUMBER (row);
16413 vpos = XINT (row);
16414 if (vpos >= 0 && vpos < m->nrows)
16415 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16416 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16417 return Qnil;
16421 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16422 doc: /* Toggle tracing of redisplay.
16423 With ARG, turn tracing on if and only if ARG is positive. */)
16424 (Lisp_Object arg)
16426 if (NILP (arg))
16427 trace_redisplay_p = !trace_redisplay_p;
16428 else
16430 arg = Fprefix_numeric_value (arg);
16431 trace_redisplay_p = XINT (arg) > 0;
16434 return Qnil;
16438 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16439 doc: /* Like `format', but print result to stderr.
16440 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16441 (int nargs, Lisp_Object *args)
16443 Lisp_Object s = Fformat (nargs, args);
16444 fprintf (stderr, "%s", SDATA (s));
16445 return Qnil;
16448 #endif /* GLYPH_DEBUG */
16452 /***********************************************************************
16453 Building Desired Matrix Rows
16454 ***********************************************************************/
16456 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16457 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16459 static struct glyph_row *
16460 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16462 struct frame *f = XFRAME (WINDOW_FRAME (w));
16463 struct buffer *buffer = XBUFFER (w->buffer);
16464 struct buffer *old = current_buffer;
16465 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16466 int arrow_len = SCHARS (overlay_arrow_string);
16467 const unsigned char *arrow_end = arrow_string + arrow_len;
16468 const unsigned char *p;
16469 struct it it;
16470 int multibyte_p;
16471 int n_glyphs_before;
16473 set_buffer_temp (buffer);
16474 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16475 it.glyph_row->used[TEXT_AREA] = 0;
16476 SET_TEXT_POS (it.position, 0, 0);
16478 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16479 p = arrow_string;
16480 while (p < arrow_end)
16482 Lisp_Object face, ilisp;
16484 /* Get the next character. */
16485 if (multibyte_p)
16486 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16487 else
16489 it.c = it.char_to_display = *p, it.len = 1;
16490 if (! ASCII_CHAR_P (it.c))
16491 it.char_to_display = BYTE8_TO_CHAR (it.c);
16493 p += it.len;
16495 /* Get its face. */
16496 ilisp = make_number (p - arrow_string);
16497 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16498 it.face_id = compute_char_face (f, it.char_to_display, face);
16500 /* Compute its width, get its glyphs. */
16501 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16502 SET_TEXT_POS (it.position, -1, -1);
16503 PRODUCE_GLYPHS (&it);
16505 /* If this character doesn't fit any more in the line, we have
16506 to remove some glyphs. */
16507 if (it.current_x > it.last_visible_x)
16509 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16510 break;
16514 set_buffer_temp (old);
16515 return it.glyph_row;
16519 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16520 glyphs are only inserted for terminal frames since we can't really
16521 win with truncation glyphs when partially visible glyphs are
16522 involved. Which glyphs to insert is determined by
16523 produce_special_glyphs. */
16525 static void
16526 insert_left_trunc_glyphs (struct it *it)
16528 struct it truncate_it;
16529 struct glyph *from, *end, *to, *toend;
16531 xassert (!FRAME_WINDOW_P (it->f));
16533 /* Get the truncation glyphs. */
16534 truncate_it = *it;
16535 truncate_it.current_x = 0;
16536 truncate_it.face_id = DEFAULT_FACE_ID;
16537 truncate_it.glyph_row = &scratch_glyph_row;
16538 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16539 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16540 truncate_it.object = make_number (0);
16541 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16543 /* Overwrite glyphs from IT with truncation glyphs. */
16544 if (!it->glyph_row->reversed_p)
16546 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16547 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16548 to = it->glyph_row->glyphs[TEXT_AREA];
16549 toend = to + it->glyph_row->used[TEXT_AREA];
16551 while (from < end)
16552 *to++ = *from++;
16554 /* There may be padding glyphs left over. Overwrite them too. */
16555 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16557 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16558 while (from < end)
16559 *to++ = *from++;
16562 if (to > toend)
16563 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16565 else
16567 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16568 that back to front. */
16569 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16570 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16571 toend = it->glyph_row->glyphs[TEXT_AREA];
16572 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16574 while (from >= end && to >= toend)
16575 *to-- = *from--;
16576 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16578 from =
16579 truncate_it.glyph_row->glyphs[TEXT_AREA]
16580 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16581 while (from >= end && to >= toend)
16582 *to-- = *from--;
16584 if (from >= end)
16586 /* Need to free some room before prepending additional
16587 glyphs. */
16588 int move_by = from - end + 1;
16589 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16590 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16592 for ( ; g >= g0; g--)
16593 g[move_by] = *g;
16594 while (from >= end)
16595 *to-- = *from--;
16596 it->glyph_row->used[TEXT_AREA] += move_by;
16602 /* Compute the pixel height and width of IT->glyph_row.
16604 Most of the time, ascent and height of a display line will be equal
16605 to the max_ascent and max_height values of the display iterator
16606 structure. This is not the case if
16608 1. We hit ZV without displaying anything. In this case, max_ascent
16609 and max_height will be zero.
16611 2. We have some glyphs that don't contribute to the line height.
16612 (The glyph row flag contributes_to_line_height_p is for future
16613 pixmap extensions).
16615 The first case is easily covered by using default values because in
16616 these cases, the line height does not really matter, except that it
16617 must not be zero. */
16619 static void
16620 compute_line_metrics (struct it *it)
16622 struct glyph_row *row = it->glyph_row;
16623 int area, i;
16625 if (FRAME_WINDOW_P (it->f))
16627 int i, min_y, max_y;
16629 /* The line may consist of one space only, that was added to
16630 place the cursor on it. If so, the row's height hasn't been
16631 computed yet. */
16632 if (row->height == 0)
16634 if (it->max_ascent + it->max_descent == 0)
16635 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16636 row->ascent = it->max_ascent;
16637 row->height = it->max_ascent + it->max_descent;
16638 row->phys_ascent = it->max_phys_ascent;
16639 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16640 row->extra_line_spacing = it->max_extra_line_spacing;
16643 /* Compute the width of this line. */
16644 row->pixel_width = row->x;
16645 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16646 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16648 xassert (row->pixel_width >= 0);
16649 xassert (row->ascent >= 0 && row->height > 0);
16651 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16652 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16654 /* If first line's physical ascent is larger than its logical
16655 ascent, use the physical ascent, and make the row taller.
16656 This makes accented characters fully visible. */
16657 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16658 && row->phys_ascent > row->ascent)
16660 row->height += row->phys_ascent - row->ascent;
16661 row->ascent = row->phys_ascent;
16664 /* Compute how much of the line is visible. */
16665 row->visible_height = row->height;
16667 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16668 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16670 if (row->y < min_y)
16671 row->visible_height -= min_y - row->y;
16672 if (row->y + row->height > max_y)
16673 row->visible_height -= row->y + row->height - max_y;
16675 else
16677 row->pixel_width = row->used[TEXT_AREA];
16678 if (row->continued_p)
16679 row->pixel_width -= it->continuation_pixel_width;
16680 else if (row->truncated_on_right_p)
16681 row->pixel_width -= it->truncation_pixel_width;
16682 row->ascent = row->phys_ascent = 0;
16683 row->height = row->phys_height = row->visible_height = 1;
16684 row->extra_line_spacing = 0;
16687 /* Compute a hash code for this row. */
16688 row->hash = 0;
16689 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16690 for (i = 0; i < row->used[area]; ++i)
16691 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16692 + row->glyphs[area][i].u.val
16693 + row->glyphs[area][i].face_id
16694 + row->glyphs[area][i].padding_p
16695 + (row->glyphs[area][i].type << 2));
16697 it->max_ascent = it->max_descent = 0;
16698 it->max_phys_ascent = it->max_phys_descent = 0;
16702 /* Append one space to the glyph row of iterator IT if doing a
16703 window-based redisplay. The space has the same face as
16704 IT->face_id. Value is non-zero if a space was added.
16706 This function is called to make sure that there is always one glyph
16707 at the end of a glyph row that the cursor can be set on under
16708 window-systems. (If there weren't such a glyph we would not know
16709 how wide and tall a box cursor should be displayed).
16711 At the same time this space let's a nicely handle clearing to the
16712 end of the line if the row ends in italic text. */
16714 static int
16715 append_space_for_newline (struct it *it, int default_face_p)
16717 if (FRAME_WINDOW_P (it->f))
16719 int n = it->glyph_row->used[TEXT_AREA];
16721 if (it->glyph_row->glyphs[TEXT_AREA] + n
16722 < it->glyph_row->glyphs[1 + TEXT_AREA])
16724 /* Save some values that must not be changed.
16725 Must save IT->c and IT->len because otherwise
16726 ITERATOR_AT_END_P wouldn't work anymore after
16727 append_space_for_newline has been called. */
16728 enum display_element_type saved_what = it->what;
16729 int saved_c = it->c, saved_len = it->len;
16730 int saved_char_to_display = it->char_to_display;
16731 int saved_x = it->current_x;
16732 int saved_face_id = it->face_id;
16733 struct text_pos saved_pos;
16734 Lisp_Object saved_object;
16735 struct face *face;
16737 saved_object = it->object;
16738 saved_pos = it->position;
16740 it->what = IT_CHARACTER;
16741 memset (&it->position, 0, sizeof it->position);
16742 it->object = make_number (0);
16743 it->c = it->char_to_display = ' ';
16744 it->len = 1;
16746 if (default_face_p)
16747 it->face_id = DEFAULT_FACE_ID;
16748 else if (it->face_before_selective_p)
16749 it->face_id = it->saved_face_id;
16750 face = FACE_FROM_ID (it->f, it->face_id);
16751 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16753 PRODUCE_GLYPHS (it);
16755 it->override_ascent = -1;
16756 it->constrain_row_ascent_descent_p = 0;
16757 it->current_x = saved_x;
16758 it->object = saved_object;
16759 it->position = saved_pos;
16760 it->what = saved_what;
16761 it->face_id = saved_face_id;
16762 it->len = saved_len;
16763 it->c = saved_c;
16764 it->char_to_display = saved_char_to_display;
16765 return 1;
16769 return 0;
16773 /* Extend the face of the last glyph in the text area of IT->glyph_row
16774 to the end of the display line. Called from display_line. If the
16775 glyph row is empty, add a space glyph to it so that we know the
16776 face to draw. Set the glyph row flag fill_line_p. If the glyph
16777 row is R2L, prepend a stretch glyph to cover the empty space to the
16778 left of the leftmost glyph. */
16780 static void
16781 extend_face_to_end_of_line (struct it *it)
16783 struct face *face;
16784 struct frame *f = it->f;
16786 /* If line is already filled, do nothing. Non window-system frames
16787 get a grace of one more ``pixel'' because their characters are
16788 1-``pixel'' wide, so they hit the equality too early. This grace
16789 is needed only for R2L rows that are not continued, to produce
16790 one extra blank where we could display the cursor. */
16791 if (it->current_x >= it->last_visible_x
16792 + (!FRAME_WINDOW_P (f)
16793 && it->glyph_row->reversed_p
16794 && !it->glyph_row->continued_p))
16795 return;
16797 /* Face extension extends the background and box of IT->face_id
16798 to the end of the line. If the background equals the background
16799 of the frame, we don't have to do anything. */
16800 if (it->face_before_selective_p)
16801 face = FACE_FROM_ID (f, it->saved_face_id);
16802 else
16803 face = FACE_FROM_ID (f, it->face_id);
16805 if (FRAME_WINDOW_P (f)
16806 && it->glyph_row->displays_text_p
16807 && face->box == FACE_NO_BOX
16808 && face->background == FRAME_BACKGROUND_PIXEL (f)
16809 && !face->stipple
16810 && !it->glyph_row->reversed_p)
16811 return;
16813 /* Set the glyph row flag indicating that the face of the last glyph
16814 in the text area has to be drawn to the end of the text area. */
16815 it->glyph_row->fill_line_p = 1;
16817 /* If current character of IT is not ASCII, make sure we have the
16818 ASCII face. This will be automatically undone the next time
16819 get_next_display_element returns a multibyte character. Note
16820 that the character will always be single byte in unibyte
16821 text. */
16822 if (!ASCII_CHAR_P (it->c))
16824 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16827 if (FRAME_WINDOW_P (f))
16829 /* If the row is empty, add a space with the current face of IT,
16830 so that we know which face to draw. */
16831 if (it->glyph_row->used[TEXT_AREA] == 0)
16833 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16834 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16835 it->glyph_row->used[TEXT_AREA] = 1;
16837 #ifdef HAVE_WINDOW_SYSTEM
16838 if (it->glyph_row->reversed_p)
16840 /* Prepend a stretch glyph to the row, such that the
16841 rightmost glyph will be drawn flushed all the way to the
16842 right margin of the window. The stretch glyph that will
16843 occupy the empty space, if any, to the left of the
16844 glyphs. */
16845 struct font *font = face->font ? face->font : FRAME_FONT (f);
16846 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16847 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16848 struct glyph *g;
16849 int row_width, stretch_ascent, stretch_width;
16850 struct text_pos saved_pos;
16851 int saved_face_id, saved_avoid_cursor;
16853 for (row_width = 0, g = row_start; g < row_end; g++)
16854 row_width += g->pixel_width;
16855 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16856 if (stretch_width > 0)
16858 stretch_ascent =
16859 (((it->ascent + it->descent)
16860 * FONT_BASE (font)) / FONT_HEIGHT (font));
16861 saved_pos = it->position;
16862 memset (&it->position, 0, sizeof it->position);
16863 saved_avoid_cursor = it->avoid_cursor_p;
16864 it->avoid_cursor_p = 1;
16865 saved_face_id = it->face_id;
16866 /* The last row's stretch glyph should get the default
16867 face, to avoid painting the rest of the window with
16868 the region face, if the region ends at ZV. */
16869 if (it->glyph_row->ends_at_zv_p)
16870 it->face_id = DEFAULT_FACE_ID;
16871 else
16872 it->face_id = face->id;
16873 append_stretch_glyph (it, make_number (0), stretch_width,
16874 it->ascent + it->descent, stretch_ascent);
16875 it->position = saved_pos;
16876 it->avoid_cursor_p = saved_avoid_cursor;
16877 it->face_id = saved_face_id;
16880 #endif /* HAVE_WINDOW_SYSTEM */
16882 else
16884 /* Save some values that must not be changed. */
16885 int saved_x = it->current_x;
16886 struct text_pos saved_pos;
16887 Lisp_Object saved_object;
16888 enum display_element_type saved_what = it->what;
16889 int saved_face_id = it->face_id;
16891 saved_object = it->object;
16892 saved_pos = it->position;
16894 it->what = IT_CHARACTER;
16895 memset (&it->position, 0, sizeof it->position);
16896 it->object = make_number (0);
16897 it->c = it->char_to_display = ' ';
16898 it->len = 1;
16899 /* The last row's blank glyphs should get the default face, to
16900 avoid painting the rest of the window with the region face,
16901 if the region ends at ZV. */
16902 if (it->glyph_row->ends_at_zv_p)
16903 it->face_id = DEFAULT_FACE_ID;
16904 else
16905 it->face_id = face->id;
16907 PRODUCE_GLYPHS (it);
16909 while (it->current_x <= it->last_visible_x)
16910 PRODUCE_GLYPHS (it);
16912 /* Don't count these blanks really. It would let us insert a left
16913 truncation glyph below and make us set the cursor on them, maybe. */
16914 it->current_x = saved_x;
16915 it->object = saved_object;
16916 it->position = saved_pos;
16917 it->what = saved_what;
16918 it->face_id = saved_face_id;
16923 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16924 trailing whitespace. */
16926 static int
16927 trailing_whitespace_p (EMACS_INT charpos)
16929 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
16930 int c = 0;
16932 while (bytepos < ZV_BYTE
16933 && (c = FETCH_CHAR (bytepos),
16934 c == ' ' || c == '\t'))
16935 ++bytepos;
16937 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16939 if (bytepos != PT_BYTE)
16940 return 1;
16942 return 0;
16946 /* Highlight trailing whitespace, if any, in ROW. */
16948 void
16949 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
16951 int used = row->used[TEXT_AREA];
16953 if (used)
16955 struct glyph *start = row->glyphs[TEXT_AREA];
16956 struct glyph *glyph = start + used - 1;
16958 if (row->reversed_p)
16960 /* Right-to-left rows need to be processed in the opposite
16961 direction, so swap the edge pointers. */
16962 glyph = start;
16963 start = row->glyphs[TEXT_AREA] + used - 1;
16966 /* Skip over glyphs inserted to display the cursor at the
16967 end of a line, for extending the face of the last glyph
16968 to the end of the line on terminals, and for truncation
16969 and continuation glyphs. */
16970 if (!row->reversed_p)
16972 while (glyph >= start
16973 && glyph->type == CHAR_GLYPH
16974 && INTEGERP (glyph->object))
16975 --glyph;
16977 else
16979 while (glyph <= start
16980 && glyph->type == CHAR_GLYPH
16981 && INTEGERP (glyph->object))
16982 ++glyph;
16985 /* If last glyph is a space or stretch, and it's trailing
16986 whitespace, set the face of all trailing whitespace glyphs in
16987 IT->glyph_row to `trailing-whitespace'. */
16988 if ((row->reversed_p ? glyph <= start : glyph >= start)
16989 && BUFFERP (glyph->object)
16990 && (glyph->type == STRETCH_GLYPH
16991 || (glyph->type == CHAR_GLYPH
16992 && glyph->u.ch == ' '))
16993 && trailing_whitespace_p (glyph->charpos))
16995 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16996 if (face_id < 0)
16997 return;
16999 if (!row->reversed_p)
17001 while (glyph >= start
17002 && BUFFERP (glyph->object)
17003 && (glyph->type == STRETCH_GLYPH
17004 || (glyph->type == CHAR_GLYPH
17005 && glyph->u.ch == ' ')))
17006 (glyph--)->face_id = face_id;
17008 else
17010 while (glyph <= start
17011 && BUFFERP (glyph->object)
17012 && (glyph->type == STRETCH_GLYPH
17013 || (glyph->type == CHAR_GLYPH
17014 && glyph->u.ch == ' ')))
17015 (glyph++)->face_id = face_id;
17022 /* Value is non-zero if glyph row ROW in window W should be
17023 used to hold the cursor. */
17025 static int
17026 cursor_row_p (struct window *w, struct glyph_row *row)
17028 int cursor_row_p = 1;
17030 if (PT == CHARPOS (row->end.pos))
17032 /* Suppose the row ends on a string.
17033 Unless the row is continued, that means it ends on a newline
17034 in the string. If it's anything other than a display string
17035 (e.g. a before-string from an overlay), we don't want the
17036 cursor there. (This heuristic seems to give the optimal
17037 behavior for the various types of multi-line strings.) */
17038 if (CHARPOS (row->end.string_pos) >= 0)
17040 if (row->continued_p)
17041 cursor_row_p = 1;
17042 else
17044 /* Check for `display' property. */
17045 struct glyph *beg = row->glyphs[TEXT_AREA];
17046 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17047 struct glyph *glyph;
17049 cursor_row_p = 0;
17050 for (glyph = end; glyph >= beg; --glyph)
17051 if (STRINGP (glyph->object))
17053 Lisp_Object prop
17054 = Fget_char_property (make_number (PT),
17055 Qdisplay, Qnil);
17056 cursor_row_p =
17057 (!NILP (prop)
17058 && display_prop_string_p (prop, glyph->object));
17059 break;
17063 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17065 /* If the row ends in middle of a real character,
17066 and the line is continued, we want the cursor here.
17067 That's because CHARPOS (ROW->end.pos) would equal
17068 PT if PT is before the character. */
17069 if (!row->ends_in_ellipsis_p)
17070 cursor_row_p = row->continued_p;
17071 else
17072 /* If the row ends in an ellipsis, then
17073 CHARPOS (ROW->end.pos) will equal point after the
17074 invisible text. We want that position to be displayed
17075 after the ellipsis. */
17076 cursor_row_p = 0;
17078 /* If the row ends at ZV, display the cursor at the end of that
17079 row instead of at the start of the row below. */
17080 else if (row->ends_at_zv_p)
17081 cursor_row_p = 1;
17082 else
17083 cursor_row_p = 0;
17086 return cursor_row_p;
17091 /* Push the display property PROP so that it will be rendered at the
17092 current position in IT. Return 1 if PROP was successfully pushed,
17093 0 otherwise. */
17095 static int
17096 push_display_prop (struct it *it, Lisp_Object prop)
17098 push_it (it);
17100 if (STRINGP (prop))
17102 if (SCHARS (prop) == 0)
17104 pop_it (it);
17105 return 0;
17108 it->string = prop;
17109 it->multibyte_p = STRING_MULTIBYTE (it->string);
17110 it->current.overlay_string_index = -1;
17111 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17112 it->end_charpos = it->string_nchars = SCHARS (it->string);
17113 it->method = GET_FROM_STRING;
17114 it->stop_charpos = 0;
17116 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17118 it->method = GET_FROM_STRETCH;
17119 it->object = prop;
17121 #ifdef HAVE_WINDOW_SYSTEM
17122 else if (IMAGEP (prop))
17124 it->what = IT_IMAGE;
17125 it->image_id = lookup_image (it->f, prop);
17126 it->method = GET_FROM_IMAGE;
17128 #endif /* HAVE_WINDOW_SYSTEM */
17129 else
17131 pop_it (it); /* bogus display property, give up */
17132 return 0;
17135 return 1;
17138 /* Return the character-property PROP at the current position in IT. */
17140 static Lisp_Object
17141 get_it_property (struct it *it, Lisp_Object prop)
17143 Lisp_Object position;
17145 if (STRINGP (it->object))
17146 position = make_number (IT_STRING_CHARPOS (*it));
17147 else if (BUFFERP (it->object))
17148 position = make_number (IT_CHARPOS (*it));
17149 else
17150 return Qnil;
17152 return Fget_char_property (position, prop, it->object);
17155 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17157 static void
17158 handle_line_prefix (struct it *it)
17160 Lisp_Object prefix;
17161 if (it->continuation_lines_width > 0)
17163 prefix = get_it_property (it, Qwrap_prefix);
17164 if (NILP (prefix))
17165 prefix = Vwrap_prefix;
17167 else
17169 prefix = get_it_property (it, Qline_prefix);
17170 if (NILP (prefix))
17171 prefix = Vline_prefix;
17173 if (! NILP (prefix) && push_display_prop (it, prefix))
17175 /* If the prefix is wider than the window, and we try to wrap
17176 it, it would acquire its own wrap prefix, and so on till the
17177 iterator stack overflows. So, don't wrap the prefix. */
17178 it->line_wrap = TRUNCATE;
17179 it->avoid_cursor_p = 1;
17185 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17186 only for R2L lines from display_line, when it decides that too many
17187 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17188 continued. */
17189 static void
17190 unproduce_glyphs (struct it *it, int n)
17192 struct glyph *glyph, *end;
17194 xassert (it->glyph_row);
17195 xassert (it->glyph_row->reversed_p);
17196 xassert (it->area == TEXT_AREA);
17197 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17199 if (n > it->glyph_row->used[TEXT_AREA])
17200 n = it->glyph_row->used[TEXT_AREA];
17201 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17202 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17203 for ( ; glyph < end; glyph++)
17204 glyph[-n] = *glyph;
17207 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17208 and ROW->maxpos. */
17209 static void
17210 find_row_edges (struct it *it, struct glyph_row *row,
17211 EMACS_INT min_pos, EMACS_INT min_bpos,
17212 EMACS_INT max_pos, EMACS_INT max_bpos)
17214 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17215 lines' rows is implemented for bidi-reordered rows. */
17217 /* ROW->minpos is the value of min_pos, the minimal buffer position
17218 we have in ROW. */
17219 if (min_pos <= ZV)
17220 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17221 else
17223 /* We didn't find _any_ valid buffer positions in any of the
17224 glyphs, so we must trust the iterator's computed
17225 positions. */
17226 row->minpos = row->start.pos;
17227 max_pos = CHARPOS (it->current.pos);
17228 max_bpos = BYTEPOS (it->current.pos);
17231 if (!max_pos)
17232 abort ();
17234 /* Here are the various use-cases for ending the row, and the
17235 corresponding values for ROW->maxpos:
17237 Line ends in a newline from buffer eol_pos + 1
17238 Line is continued from buffer max_pos + 1
17239 Line is truncated on right it->current.pos
17240 Line ends in a newline from string max_pos
17241 Line is continued from string max_pos
17242 Line is continued from display vector max_pos
17243 Line is entirely from a string min_pos == max_pos
17244 Line is entirely from a display vector min_pos == max_pos
17245 Line that ends at ZV ZV
17247 If you discover other use-cases, please add them here as
17248 appropriate. */
17249 if (row->ends_at_zv_p)
17250 row->maxpos = it->current.pos;
17251 else if (row->used[TEXT_AREA])
17253 if (row->ends_in_newline_from_string_p)
17254 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17255 else if (CHARPOS (it->eol_pos) > 0)
17256 SET_TEXT_POS (row->maxpos,
17257 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17258 else if (row->continued_p)
17260 /* If max_pos is different from IT's current position, it
17261 means IT->method does not belong to the display element
17262 at max_pos. However, it also means that the display
17263 element at max_pos was displayed in its entirety on this
17264 line, which is equivalent to saying that the next line
17265 starts at the next buffer position. */
17266 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17267 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17268 else
17270 INC_BOTH (max_pos, max_bpos);
17271 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17274 else if (row->truncated_on_right_p)
17275 /* display_line already called reseat_at_next_visible_line_start,
17276 which puts the iterator at the beginning of the next line, in
17277 the logical order. */
17278 row->maxpos = it->current.pos;
17279 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17280 /* A line that is entirely from a string/image/stretch... */
17281 row->maxpos = row->minpos;
17282 else
17283 abort ();
17285 else
17286 row->maxpos = it->current.pos;
17289 /* Construct the glyph row IT->glyph_row in the desired matrix of
17290 IT->w from text at the current position of IT. See dispextern.h
17291 for an overview of struct it. Value is non-zero if
17292 IT->glyph_row displays text, as opposed to a line displaying ZV
17293 only. */
17295 static int
17296 display_line (struct it *it)
17298 struct glyph_row *row = it->glyph_row;
17299 Lisp_Object overlay_arrow_string;
17300 struct it wrap_it;
17301 int may_wrap = 0, wrap_x;
17302 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17303 int wrap_row_phys_ascent, wrap_row_phys_height;
17304 int wrap_row_extra_line_spacing;
17305 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17306 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17307 int cvpos;
17308 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17310 /* We always start displaying at hpos zero even if hscrolled. */
17311 xassert (it->hpos == 0 && it->current_x == 0);
17313 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17314 >= it->w->desired_matrix->nrows)
17316 it->w->nrows_scale_factor++;
17317 fonts_changed_p = 1;
17318 return 0;
17321 /* Is IT->w showing the region? */
17322 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17324 /* Clear the result glyph row and enable it. */
17325 prepare_desired_row (row);
17327 row->y = it->current_y;
17328 row->start = it->start;
17329 row->continuation_lines_width = it->continuation_lines_width;
17330 row->displays_text_p = 1;
17331 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17332 it->starts_in_middle_of_char_p = 0;
17334 /* Arrange the overlays nicely for our purposes. Usually, we call
17335 display_line on only one line at a time, in which case this
17336 can't really hurt too much, or we call it on lines which appear
17337 one after another in the buffer, in which case all calls to
17338 recenter_overlay_lists but the first will be pretty cheap. */
17339 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17341 /* Move over display elements that are not visible because we are
17342 hscrolled. This may stop at an x-position < IT->first_visible_x
17343 if the first glyph is partially visible or if we hit a line end. */
17344 if (it->current_x < it->first_visible_x)
17346 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17347 MOVE_TO_POS | MOVE_TO_X);
17349 else
17351 /* We only do this when not calling `move_it_in_display_line_to'
17352 above, because move_it_in_display_line_to calls
17353 handle_line_prefix itself. */
17354 handle_line_prefix (it);
17357 /* Get the initial row height. This is either the height of the
17358 text hscrolled, if there is any, or zero. */
17359 row->ascent = it->max_ascent;
17360 row->height = it->max_ascent + it->max_descent;
17361 row->phys_ascent = it->max_phys_ascent;
17362 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17363 row->extra_line_spacing = it->max_extra_line_spacing;
17365 /* Utility macro to record max and min buffer positions seen until now. */
17366 #define RECORD_MAX_MIN_POS(IT) \
17367 do \
17369 if (IT_CHARPOS (*(IT)) < min_pos) \
17371 min_pos = IT_CHARPOS (*(IT)); \
17372 min_bpos = IT_BYTEPOS (*(IT)); \
17374 if (IT_CHARPOS (*(IT)) > max_pos) \
17376 max_pos = IT_CHARPOS (*(IT)); \
17377 max_bpos = IT_BYTEPOS (*(IT)); \
17380 while (0)
17382 /* Loop generating characters. The loop is left with IT on the next
17383 character to display. */
17384 while (1)
17386 int n_glyphs_before, hpos_before, x_before;
17387 int x, i, nglyphs;
17388 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17390 /* Retrieve the next thing to display. Value is zero if end of
17391 buffer reached. */
17392 if (!get_next_display_element (it))
17394 /* Maybe add a space at the end of this line that is used to
17395 display the cursor there under X. Set the charpos of the
17396 first glyph of blank lines not corresponding to any text
17397 to -1. */
17398 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17399 row->exact_window_width_line_p = 1;
17400 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17401 || row->used[TEXT_AREA] == 0)
17403 row->glyphs[TEXT_AREA]->charpos = -1;
17404 row->displays_text_p = 0;
17406 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17407 && (!MINI_WINDOW_P (it->w)
17408 || (minibuf_level && EQ (it->window, minibuf_window))))
17409 row->indicate_empty_line_p = 1;
17412 it->continuation_lines_width = 0;
17413 row->ends_at_zv_p = 1;
17414 /* A row that displays right-to-left text must always have
17415 its last face extended all the way to the end of line,
17416 even if this row ends in ZV, because we still write to
17417 the screen left to right. */
17418 if (row->reversed_p)
17419 extend_face_to_end_of_line (it);
17420 break;
17423 /* Now, get the metrics of what we want to display. This also
17424 generates glyphs in `row' (which is IT->glyph_row). */
17425 n_glyphs_before = row->used[TEXT_AREA];
17426 x = it->current_x;
17428 /* Remember the line height so far in case the next element doesn't
17429 fit on the line. */
17430 if (it->line_wrap != TRUNCATE)
17432 ascent = it->max_ascent;
17433 descent = it->max_descent;
17434 phys_ascent = it->max_phys_ascent;
17435 phys_descent = it->max_phys_descent;
17437 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17439 if (IT_DISPLAYING_WHITESPACE (it))
17440 may_wrap = 1;
17441 else if (may_wrap)
17443 wrap_it = *it;
17444 wrap_x = x;
17445 wrap_row_used = row->used[TEXT_AREA];
17446 wrap_row_ascent = row->ascent;
17447 wrap_row_height = row->height;
17448 wrap_row_phys_ascent = row->phys_ascent;
17449 wrap_row_phys_height = row->phys_height;
17450 wrap_row_extra_line_spacing = row->extra_line_spacing;
17451 wrap_row_min_pos = min_pos;
17452 wrap_row_min_bpos = min_bpos;
17453 wrap_row_max_pos = max_pos;
17454 wrap_row_max_bpos = max_bpos;
17455 may_wrap = 0;
17460 PRODUCE_GLYPHS (it);
17462 /* If this display element was in marginal areas, continue with
17463 the next one. */
17464 if (it->area != TEXT_AREA)
17466 row->ascent = max (row->ascent, it->max_ascent);
17467 row->height = max (row->height, it->max_ascent + it->max_descent);
17468 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17469 row->phys_height = max (row->phys_height,
17470 it->max_phys_ascent + it->max_phys_descent);
17471 row->extra_line_spacing = max (row->extra_line_spacing,
17472 it->max_extra_line_spacing);
17473 set_iterator_to_next (it, 1);
17474 continue;
17477 /* Does the display element fit on the line? If we truncate
17478 lines, we should draw past the right edge of the window. If
17479 we don't truncate, we want to stop so that we can display the
17480 continuation glyph before the right margin. If lines are
17481 continued, there are two possible strategies for characters
17482 resulting in more than 1 glyph (e.g. tabs): Display as many
17483 glyphs as possible in this line and leave the rest for the
17484 continuation line, or display the whole element in the next
17485 line. Original redisplay did the former, so we do it also. */
17486 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17487 hpos_before = it->hpos;
17488 x_before = x;
17490 if (/* Not a newline. */
17491 nglyphs > 0
17492 /* Glyphs produced fit entirely in the line. */
17493 && it->current_x < it->last_visible_x)
17495 it->hpos += nglyphs;
17496 row->ascent = max (row->ascent, it->max_ascent);
17497 row->height = max (row->height, it->max_ascent + it->max_descent);
17498 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17499 row->phys_height = max (row->phys_height,
17500 it->max_phys_ascent + it->max_phys_descent);
17501 row->extra_line_spacing = max (row->extra_line_spacing,
17502 it->max_extra_line_spacing);
17503 if (it->current_x - it->pixel_width < it->first_visible_x)
17504 row->x = x - it->first_visible_x;
17505 /* Record the maximum and minimum buffer positions seen so
17506 far in glyphs that will be displayed by this row. */
17507 if (it->bidi_p)
17508 RECORD_MAX_MIN_POS (it);
17510 else
17512 int new_x;
17513 struct glyph *glyph;
17515 for (i = 0; i < nglyphs; ++i, x = new_x)
17517 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17518 new_x = x + glyph->pixel_width;
17520 if (/* Lines are continued. */
17521 it->line_wrap != TRUNCATE
17522 && (/* Glyph doesn't fit on the line. */
17523 new_x > it->last_visible_x
17524 /* Or it fits exactly on a window system frame. */
17525 || (new_x == it->last_visible_x
17526 && FRAME_WINDOW_P (it->f))))
17528 /* End of a continued line. */
17530 if (it->hpos == 0
17531 || (new_x == it->last_visible_x
17532 && FRAME_WINDOW_P (it->f)))
17534 /* Current glyph is the only one on the line or
17535 fits exactly on the line. We must continue
17536 the line because we can't draw the cursor
17537 after the glyph. */
17538 row->continued_p = 1;
17539 it->current_x = new_x;
17540 it->continuation_lines_width += new_x;
17541 ++it->hpos;
17542 /* Record the maximum and minimum buffer
17543 positions seen so far in glyphs that will be
17544 displayed by this row. */
17545 if (it->bidi_p)
17546 RECORD_MAX_MIN_POS (it);
17547 if (i == nglyphs - 1)
17549 /* If line-wrap is on, check if a previous
17550 wrap point was found. */
17551 if (wrap_row_used > 0
17552 /* Even if there is a previous wrap
17553 point, continue the line here as
17554 usual, if (i) the previous character
17555 was a space or tab AND (ii) the
17556 current character is not. */
17557 && (!may_wrap
17558 || IT_DISPLAYING_WHITESPACE (it)))
17559 goto back_to_wrap;
17561 set_iterator_to_next (it, 1);
17562 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17564 if (!get_next_display_element (it))
17566 row->exact_window_width_line_p = 1;
17567 it->continuation_lines_width = 0;
17568 row->continued_p = 0;
17569 row->ends_at_zv_p = 1;
17571 else if (ITERATOR_AT_END_OF_LINE_P (it))
17573 row->continued_p = 0;
17574 row->exact_window_width_line_p = 1;
17579 else if (CHAR_GLYPH_PADDING_P (*glyph)
17580 && !FRAME_WINDOW_P (it->f))
17582 /* A padding glyph that doesn't fit on this line.
17583 This means the whole character doesn't fit
17584 on the line. */
17585 if (row->reversed_p)
17586 unproduce_glyphs (it, row->used[TEXT_AREA]
17587 - n_glyphs_before);
17588 row->used[TEXT_AREA] = n_glyphs_before;
17590 /* Fill the rest of the row with continuation
17591 glyphs like in 20.x. */
17592 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17593 < row->glyphs[1 + TEXT_AREA])
17594 produce_special_glyphs (it, IT_CONTINUATION);
17596 row->continued_p = 1;
17597 it->current_x = x_before;
17598 it->continuation_lines_width += x_before;
17600 /* Restore the height to what it was before the
17601 element not fitting on the line. */
17602 it->max_ascent = ascent;
17603 it->max_descent = descent;
17604 it->max_phys_ascent = phys_ascent;
17605 it->max_phys_descent = phys_descent;
17607 else if (wrap_row_used > 0)
17609 back_to_wrap:
17610 if (row->reversed_p)
17611 unproduce_glyphs (it,
17612 row->used[TEXT_AREA] - wrap_row_used);
17613 *it = wrap_it;
17614 it->continuation_lines_width += wrap_x;
17615 row->used[TEXT_AREA] = wrap_row_used;
17616 row->ascent = wrap_row_ascent;
17617 row->height = wrap_row_height;
17618 row->phys_ascent = wrap_row_phys_ascent;
17619 row->phys_height = wrap_row_phys_height;
17620 row->extra_line_spacing = wrap_row_extra_line_spacing;
17621 min_pos = wrap_row_min_pos;
17622 min_bpos = wrap_row_min_bpos;
17623 max_pos = wrap_row_max_pos;
17624 max_bpos = wrap_row_max_bpos;
17625 row->continued_p = 1;
17626 row->ends_at_zv_p = 0;
17627 row->exact_window_width_line_p = 0;
17628 it->continuation_lines_width += x;
17630 /* Make sure that a non-default face is extended
17631 up to the right margin of the window. */
17632 extend_face_to_end_of_line (it);
17634 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17636 /* A TAB that extends past the right edge of the
17637 window. This produces a single glyph on
17638 window system frames. We leave the glyph in
17639 this row and let it fill the row, but don't
17640 consume the TAB. */
17641 it->continuation_lines_width += it->last_visible_x;
17642 row->ends_in_middle_of_char_p = 1;
17643 row->continued_p = 1;
17644 glyph->pixel_width = it->last_visible_x - x;
17645 it->starts_in_middle_of_char_p = 1;
17647 else
17649 /* Something other than a TAB that draws past
17650 the right edge of the window. Restore
17651 positions to values before the element. */
17652 if (row->reversed_p)
17653 unproduce_glyphs (it, row->used[TEXT_AREA]
17654 - (n_glyphs_before + i));
17655 row->used[TEXT_AREA] = n_glyphs_before + i;
17657 /* Display continuation glyphs. */
17658 if (!FRAME_WINDOW_P (it->f))
17659 produce_special_glyphs (it, IT_CONTINUATION);
17660 row->continued_p = 1;
17662 it->current_x = x_before;
17663 it->continuation_lines_width += x;
17664 extend_face_to_end_of_line (it);
17666 if (nglyphs > 1 && i > 0)
17668 row->ends_in_middle_of_char_p = 1;
17669 it->starts_in_middle_of_char_p = 1;
17672 /* Restore the height to what it was before the
17673 element not fitting on the line. */
17674 it->max_ascent = ascent;
17675 it->max_descent = descent;
17676 it->max_phys_ascent = phys_ascent;
17677 it->max_phys_descent = phys_descent;
17680 break;
17682 else if (new_x > it->first_visible_x)
17684 /* Increment number of glyphs actually displayed. */
17685 ++it->hpos;
17687 /* Record the maximum and minimum buffer positions
17688 seen so far in glyphs that will be displayed by
17689 this row. */
17690 if (it->bidi_p)
17691 RECORD_MAX_MIN_POS (it);
17693 if (x < it->first_visible_x)
17694 /* Glyph is partially visible, i.e. row starts at
17695 negative X position. */
17696 row->x = x - it->first_visible_x;
17698 else
17700 /* Glyph is completely off the left margin of the
17701 window. This should not happen because of the
17702 move_it_in_display_line at the start of this
17703 function, unless the text display area of the
17704 window is empty. */
17705 xassert (it->first_visible_x <= it->last_visible_x);
17709 row->ascent = max (row->ascent, it->max_ascent);
17710 row->height = max (row->height, it->max_ascent + it->max_descent);
17711 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17712 row->phys_height = max (row->phys_height,
17713 it->max_phys_ascent + it->max_phys_descent);
17714 row->extra_line_spacing = max (row->extra_line_spacing,
17715 it->max_extra_line_spacing);
17717 /* End of this display line if row is continued. */
17718 if (row->continued_p || row->ends_at_zv_p)
17719 break;
17722 at_end_of_line:
17723 /* Is this a line end? If yes, we're also done, after making
17724 sure that a non-default face is extended up to the right
17725 margin of the window. */
17726 if (ITERATOR_AT_END_OF_LINE_P (it))
17728 int used_before = row->used[TEXT_AREA];
17730 row->ends_in_newline_from_string_p = STRINGP (it->object);
17732 /* Add a space at the end of the line that is used to
17733 display the cursor there. */
17734 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17735 append_space_for_newline (it, 0);
17737 /* Extend the face to the end of the line. */
17738 extend_face_to_end_of_line (it);
17740 /* Make sure we have the position. */
17741 if (used_before == 0)
17742 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17744 /* Record the position of the newline, for use in
17745 find_row_edges. */
17746 it->eol_pos = it->current.pos;
17748 /* Consume the line end. This skips over invisible lines. */
17749 set_iterator_to_next (it, 1);
17750 it->continuation_lines_width = 0;
17751 break;
17754 /* Proceed with next display element. Note that this skips
17755 over lines invisible because of selective display. */
17756 set_iterator_to_next (it, 1);
17758 /* If we truncate lines, we are done when the last displayed
17759 glyphs reach past the right margin of the window. */
17760 if (it->line_wrap == TRUNCATE
17761 && (FRAME_WINDOW_P (it->f)
17762 ? (it->current_x >= it->last_visible_x)
17763 : (it->current_x > it->last_visible_x)))
17765 /* Maybe add truncation glyphs. */
17766 if (!FRAME_WINDOW_P (it->f))
17768 int i, n;
17770 if (!row->reversed_p)
17772 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17773 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17774 break;
17776 else
17778 for (i = 0; i < row->used[TEXT_AREA]; i++)
17779 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17780 break;
17781 /* Remove any padding glyphs at the front of ROW, to
17782 make room for the truncation glyphs we will be
17783 adding below. The loop below always inserts at
17784 least one truncation glyph, so also remove the
17785 last glyph added to ROW. */
17786 unproduce_glyphs (it, i + 1);
17787 /* Adjust i for the loop below. */
17788 i = row->used[TEXT_AREA] - (i + 1);
17791 for (n = row->used[TEXT_AREA]; i < n; ++i)
17793 row->used[TEXT_AREA] = i;
17794 produce_special_glyphs (it, IT_TRUNCATION);
17797 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17799 /* Don't truncate if we can overflow newline into fringe. */
17800 if (!get_next_display_element (it))
17802 it->continuation_lines_width = 0;
17803 row->ends_at_zv_p = 1;
17804 row->exact_window_width_line_p = 1;
17805 break;
17807 if (ITERATOR_AT_END_OF_LINE_P (it))
17809 row->exact_window_width_line_p = 1;
17810 goto at_end_of_line;
17814 row->truncated_on_right_p = 1;
17815 it->continuation_lines_width = 0;
17816 reseat_at_next_visible_line_start (it, 0);
17817 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17818 it->hpos = hpos_before;
17819 it->current_x = x_before;
17820 break;
17824 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17825 at the left window margin. */
17826 if (it->first_visible_x
17827 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17829 if (!FRAME_WINDOW_P (it->f))
17830 insert_left_trunc_glyphs (it);
17831 row->truncated_on_left_p = 1;
17834 /* Remember the position at which this line ends.
17836 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17837 cannot be before the call to find_row_edges below, since that is
17838 where these positions are determined. */
17839 row->end = it->current;
17840 if (!it->bidi_p)
17842 row->minpos = row->start.pos;
17843 row->maxpos = row->end.pos;
17845 else
17847 /* ROW->minpos and ROW->maxpos must be the smallest and
17848 `1 + the largest' buffer positions in ROW. But if ROW was
17849 bidi-reordered, these two positions can be anywhere in the
17850 row, so we must determine them now. */
17851 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17854 /* If the start of this line is the overlay arrow-position, then
17855 mark this glyph row as the one containing the overlay arrow.
17856 This is clearly a mess with variable size fonts. It would be
17857 better to let it be displayed like cursors under X. */
17858 if ((row->displays_text_p || !overlay_arrow_seen)
17859 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17860 !NILP (overlay_arrow_string)))
17862 /* Overlay arrow in window redisplay is a fringe bitmap. */
17863 if (STRINGP (overlay_arrow_string))
17865 struct glyph_row *arrow_row
17866 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17867 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17868 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17869 struct glyph *p = row->glyphs[TEXT_AREA];
17870 struct glyph *p2, *end;
17872 /* Copy the arrow glyphs. */
17873 while (glyph < arrow_end)
17874 *p++ = *glyph++;
17876 /* Throw away padding glyphs. */
17877 p2 = p;
17878 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17879 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17880 ++p2;
17881 if (p2 > p)
17883 while (p2 < end)
17884 *p++ = *p2++;
17885 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17888 else
17890 xassert (INTEGERP (overlay_arrow_string));
17891 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17893 overlay_arrow_seen = 1;
17896 /* Compute pixel dimensions of this line. */
17897 compute_line_metrics (it);
17899 /* Record whether this row ends inside an ellipsis. */
17900 row->ends_in_ellipsis_p
17901 = (it->method == GET_FROM_DISPLAY_VECTOR
17902 && it->ellipsis_p);
17904 /* Save fringe bitmaps in this row. */
17905 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17906 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17907 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17908 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17910 it->left_user_fringe_bitmap = 0;
17911 it->left_user_fringe_face_id = 0;
17912 it->right_user_fringe_bitmap = 0;
17913 it->right_user_fringe_face_id = 0;
17915 /* Maybe set the cursor. */
17916 cvpos = it->w->cursor.vpos;
17917 if ((cvpos < 0
17918 /* In bidi-reordered rows, keep checking for proper cursor
17919 position even if one has been found already, because buffer
17920 positions in such rows change non-linearly with ROW->VPOS,
17921 when a line is continued. One exception: when we are at ZV,
17922 display cursor on the first suitable glyph row, since all
17923 the empty rows after that also have their position set to ZV. */
17924 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17925 lines' rows is implemented for bidi-reordered rows. */
17926 || (it->bidi_p
17927 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17928 && PT >= MATRIX_ROW_START_CHARPOS (row)
17929 && PT <= MATRIX_ROW_END_CHARPOS (row)
17930 && cursor_row_p (it->w, row))
17931 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17933 /* Highlight trailing whitespace. */
17934 if (!NILP (Vshow_trailing_whitespace))
17935 highlight_trailing_whitespace (it->f, it->glyph_row);
17937 /* Prepare for the next line. This line starts horizontally at (X
17938 HPOS) = (0 0). Vertical positions are incremented. As a
17939 convenience for the caller, IT->glyph_row is set to the next
17940 row to be used. */
17941 it->current_x = it->hpos = 0;
17942 it->current_y += row->height;
17943 SET_TEXT_POS (it->eol_pos, 0, 0);
17944 ++it->vpos;
17945 ++it->glyph_row;
17946 /* The next row should by default use the same value of the
17947 reversed_p flag as this one. set_iterator_to_next decides when
17948 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
17949 the flag accordingly. */
17950 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
17951 it->glyph_row->reversed_p = row->reversed_p;
17952 it->start = row->end;
17953 return row->displays_text_p;
17955 #undef RECORD_MAX_MIN_POS
17958 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
17959 Scurrent_bidi_paragraph_direction, 0, 1, 0,
17960 doc: /* Return paragraph direction at point in BUFFER.
17961 Value is either `left-to-right' or `right-to-left'.
17962 If BUFFER is omitted or nil, it defaults to the current buffer.
17964 Paragraph direction determines how the text in the paragraph is displayed.
17965 In left-to-right paragraphs, text begins at the left margin of the window
17966 and the reading direction is generally left to right. In right-to-left
17967 paragraphs, text begins at the right margin and is read from right to left.
17969 See also `bidi-paragraph-direction'. */)
17970 (Lisp_Object buffer)
17972 struct buffer *buf;
17973 struct buffer *old;
17975 if (NILP (buffer))
17976 buf = current_buffer;
17977 else
17979 CHECK_BUFFER (buffer);
17980 buf = XBUFFER (buffer);
17981 old = current_buffer;
17984 if (NILP (buf->bidi_display_reordering))
17985 return Qleft_to_right;
17986 else if (!NILP (buf->bidi_paragraph_direction))
17987 return buf->bidi_paragraph_direction;
17988 else
17990 /* Determine the direction from buffer text. We could try to
17991 use current_matrix if it is up to date, but this seems fast
17992 enough as it is. */
17993 struct bidi_it itb;
17994 EMACS_INT pos = BUF_PT (buf);
17995 EMACS_INT bytepos = BUF_PT_BYTE (buf);
17996 int c;
17998 if (buf != current_buffer)
17999 set_buffer_temp (buf);
18000 /* bidi_paragraph_init finds the base direction of the paragraph
18001 by searching forward from paragraph start. We need the base
18002 direction of the current or _previous_ paragraph, so we need
18003 to make sure we are within that paragraph. To that end, find
18004 the previous non-empty line. */
18005 if (pos >= ZV && pos > BEGV)
18007 pos--;
18008 bytepos = CHAR_TO_BYTE (pos);
18010 while ((c = FETCH_BYTE (bytepos)) == '\n'
18011 || c == ' ' || c == '\t' || c == '\f')
18013 if (bytepos <= BEGV_BYTE)
18014 break;
18015 bytepos--;
18016 pos--;
18018 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18019 bytepos--;
18020 itb.charpos = pos;
18021 itb.bytepos = bytepos;
18022 itb.first_elt = 1;
18023 itb.separator_limit = -1;
18024 itb.paragraph_dir = NEUTRAL_DIR;
18026 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18027 if (buf != current_buffer)
18028 set_buffer_temp (old);
18029 switch (itb.paragraph_dir)
18031 case L2R:
18032 return Qleft_to_right;
18033 break;
18034 case R2L:
18035 return Qright_to_left;
18036 break;
18037 default:
18038 abort ();
18045 /***********************************************************************
18046 Menu Bar
18047 ***********************************************************************/
18049 /* Redisplay the menu bar in the frame for window W.
18051 The menu bar of X frames that don't have X toolkit support is
18052 displayed in a special window W->frame->menu_bar_window.
18054 The menu bar of terminal frames is treated specially as far as
18055 glyph matrices are concerned. Menu bar lines are not part of
18056 windows, so the update is done directly on the frame matrix rows
18057 for the menu bar. */
18059 static void
18060 display_menu_bar (struct window *w)
18062 struct frame *f = XFRAME (WINDOW_FRAME (w));
18063 struct it it;
18064 Lisp_Object items;
18065 int i;
18067 /* Don't do all this for graphical frames. */
18068 #ifdef HAVE_NTGUI
18069 if (FRAME_W32_P (f))
18070 return;
18071 #endif
18072 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18073 if (FRAME_X_P (f))
18074 return;
18075 #endif
18077 #ifdef HAVE_NS
18078 if (FRAME_NS_P (f))
18079 return;
18080 #endif /* HAVE_NS */
18082 #ifdef USE_X_TOOLKIT
18083 xassert (!FRAME_WINDOW_P (f));
18084 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18085 it.first_visible_x = 0;
18086 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18087 #else /* not USE_X_TOOLKIT */
18088 if (FRAME_WINDOW_P (f))
18090 /* Menu bar lines are displayed in the desired matrix of the
18091 dummy window menu_bar_window. */
18092 struct window *menu_w;
18093 xassert (WINDOWP (f->menu_bar_window));
18094 menu_w = XWINDOW (f->menu_bar_window);
18095 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18096 MENU_FACE_ID);
18097 it.first_visible_x = 0;
18098 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18100 else
18102 /* This is a TTY frame, i.e. character hpos/vpos are used as
18103 pixel x/y. */
18104 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18105 MENU_FACE_ID);
18106 it.first_visible_x = 0;
18107 it.last_visible_x = FRAME_COLS (f);
18109 #endif /* not USE_X_TOOLKIT */
18111 if (! mode_line_inverse_video)
18112 /* Force the menu-bar to be displayed in the default face. */
18113 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18115 /* Clear all rows of the menu bar. */
18116 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18118 struct glyph_row *row = it.glyph_row + i;
18119 clear_glyph_row (row);
18120 row->enabled_p = 1;
18121 row->full_width_p = 1;
18124 /* Display all items of the menu bar. */
18125 items = FRAME_MENU_BAR_ITEMS (it.f);
18126 for (i = 0; i < XVECTOR (items)->size; i += 4)
18128 Lisp_Object string;
18130 /* Stop at nil string. */
18131 string = AREF (items, i + 1);
18132 if (NILP (string))
18133 break;
18135 /* Remember where item was displayed. */
18136 ASET (items, i + 3, make_number (it.hpos));
18138 /* Display the item, pad with one space. */
18139 if (it.current_x < it.last_visible_x)
18140 display_string (NULL, string, Qnil, 0, 0, &it,
18141 SCHARS (string) + 1, 0, 0, -1);
18144 /* Fill out the line with spaces. */
18145 if (it.current_x < it.last_visible_x)
18146 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18148 /* Compute the total height of the lines. */
18149 compute_line_metrics (&it);
18154 /***********************************************************************
18155 Mode Line
18156 ***********************************************************************/
18158 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18159 FORCE is non-zero, redisplay mode lines unconditionally.
18160 Otherwise, redisplay only mode lines that are garbaged. Value is
18161 the number of windows whose mode lines were redisplayed. */
18163 static int
18164 redisplay_mode_lines (Lisp_Object window, int force)
18166 int nwindows = 0;
18168 while (!NILP (window))
18170 struct window *w = XWINDOW (window);
18172 if (WINDOWP (w->hchild))
18173 nwindows += redisplay_mode_lines (w->hchild, force);
18174 else if (WINDOWP (w->vchild))
18175 nwindows += redisplay_mode_lines (w->vchild, force);
18176 else if (force
18177 || FRAME_GARBAGED_P (XFRAME (w->frame))
18178 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18180 struct text_pos lpoint;
18181 struct buffer *old = current_buffer;
18183 /* Set the window's buffer for the mode line display. */
18184 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18185 set_buffer_internal_1 (XBUFFER (w->buffer));
18187 /* Point refers normally to the selected window. For any
18188 other window, set up appropriate value. */
18189 if (!EQ (window, selected_window))
18191 struct text_pos pt;
18193 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18194 if (CHARPOS (pt) < BEGV)
18195 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18196 else if (CHARPOS (pt) > (ZV - 1))
18197 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18198 else
18199 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18202 /* Display mode lines. */
18203 clear_glyph_matrix (w->desired_matrix);
18204 if (display_mode_lines (w))
18206 ++nwindows;
18207 w->must_be_updated_p = 1;
18210 /* Restore old settings. */
18211 set_buffer_internal_1 (old);
18212 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18215 window = w->next;
18218 return nwindows;
18222 /* Display the mode and/or header line of window W. Value is the
18223 sum number of mode lines and header lines displayed. */
18225 static int
18226 display_mode_lines (struct window *w)
18228 Lisp_Object old_selected_window, old_selected_frame;
18229 int n = 0;
18231 old_selected_frame = selected_frame;
18232 selected_frame = w->frame;
18233 old_selected_window = selected_window;
18234 XSETWINDOW (selected_window, w);
18236 /* These will be set while the mode line specs are processed. */
18237 line_number_displayed = 0;
18238 w->column_number_displayed = Qnil;
18240 if (WINDOW_WANTS_MODELINE_P (w))
18242 struct window *sel_w = XWINDOW (old_selected_window);
18244 /* Select mode line face based on the real selected window. */
18245 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18246 current_buffer->mode_line_format);
18247 ++n;
18250 if (WINDOW_WANTS_HEADER_LINE_P (w))
18252 display_mode_line (w, HEADER_LINE_FACE_ID,
18253 current_buffer->header_line_format);
18254 ++n;
18257 selected_frame = old_selected_frame;
18258 selected_window = old_selected_window;
18259 return n;
18263 /* Display mode or header line of window W. FACE_ID specifies which
18264 line to display; it is either MODE_LINE_FACE_ID or
18265 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18266 display. Value is the pixel height of the mode/header line
18267 displayed. */
18269 static int
18270 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18272 struct it it;
18273 struct face *face;
18274 int count = SPECPDL_INDEX ();
18276 init_iterator (&it, w, -1, -1, NULL, face_id);
18277 /* Don't extend on a previously drawn mode-line.
18278 This may happen if called from pos_visible_p. */
18279 it.glyph_row->enabled_p = 0;
18280 prepare_desired_row (it.glyph_row);
18282 it.glyph_row->mode_line_p = 1;
18284 if (! mode_line_inverse_video)
18285 /* Force the mode-line to be displayed in the default face. */
18286 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18288 record_unwind_protect (unwind_format_mode_line,
18289 format_mode_line_unwind_data (NULL, Qnil, 0));
18291 mode_line_target = MODE_LINE_DISPLAY;
18293 /* Temporarily make frame's keyboard the current kboard so that
18294 kboard-local variables in the mode_line_format will get the right
18295 values. */
18296 push_kboard (FRAME_KBOARD (it.f));
18297 record_unwind_save_match_data ();
18298 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18299 pop_kboard ();
18301 unbind_to (count, Qnil);
18303 /* Fill up with spaces. */
18304 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18306 compute_line_metrics (&it);
18307 it.glyph_row->full_width_p = 1;
18308 it.glyph_row->continued_p = 0;
18309 it.glyph_row->truncated_on_left_p = 0;
18310 it.glyph_row->truncated_on_right_p = 0;
18312 /* Make a 3D mode-line have a shadow at its right end. */
18313 face = FACE_FROM_ID (it.f, face_id);
18314 extend_face_to_end_of_line (&it);
18315 if (face->box != FACE_NO_BOX)
18317 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18318 + it.glyph_row->used[TEXT_AREA] - 1);
18319 last->right_box_line_p = 1;
18322 return it.glyph_row->height;
18325 /* Move element ELT in LIST to the front of LIST.
18326 Return the updated list. */
18328 static Lisp_Object
18329 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18331 register Lisp_Object tail, prev;
18332 register Lisp_Object tem;
18334 tail = list;
18335 prev = Qnil;
18336 while (CONSP (tail))
18338 tem = XCAR (tail);
18340 if (EQ (elt, tem))
18342 /* Splice out the link TAIL. */
18343 if (NILP (prev))
18344 list = XCDR (tail);
18345 else
18346 Fsetcdr (prev, XCDR (tail));
18348 /* Now make it the first. */
18349 Fsetcdr (tail, list);
18350 return tail;
18352 else
18353 prev = tail;
18354 tail = XCDR (tail);
18355 QUIT;
18358 /* Not found--return unchanged LIST. */
18359 return list;
18362 /* Contribute ELT to the mode line for window IT->w. How it
18363 translates into text depends on its data type.
18365 IT describes the display environment in which we display, as usual.
18367 DEPTH is the depth in recursion. It is used to prevent
18368 infinite recursion here.
18370 FIELD_WIDTH is the number of characters the display of ELT should
18371 occupy in the mode line, and PRECISION is the maximum number of
18372 characters to display from ELT's representation. See
18373 display_string for details.
18375 Returns the hpos of the end of the text generated by ELT.
18377 PROPS is a property list to add to any string we encounter.
18379 If RISKY is nonzero, remove (disregard) any properties in any string
18380 we encounter, and ignore :eval and :propertize.
18382 The global variable `mode_line_target' determines whether the
18383 output is passed to `store_mode_line_noprop',
18384 `store_mode_line_string', or `display_string'. */
18386 static int
18387 display_mode_element (struct it *it, int depth, int field_width, int precision,
18388 Lisp_Object elt, Lisp_Object props, int risky)
18390 int n = 0, field, prec;
18391 int literal = 0;
18393 tail_recurse:
18394 if (depth > 100)
18395 elt = build_string ("*too-deep*");
18397 depth++;
18399 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18401 case Lisp_String:
18403 /* A string: output it and check for %-constructs within it. */
18404 unsigned char c;
18405 EMACS_INT offset = 0;
18407 if (SCHARS (elt) > 0
18408 && (!NILP (props) || risky))
18410 Lisp_Object oprops, aelt;
18411 oprops = Ftext_properties_at (make_number (0), elt);
18413 /* If the starting string's properties are not what
18414 we want, translate the string. Also, if the string
18415 is risky, do that anyway. */
18417 if (NILP (Fequal (props, oprops)) || risky)
18419 /* If the starting string has properties,
18420 merge the specified ones onto the existing ones. */
18421 if (! NILP (oprops) && !risky)
18423 Lisp_Object tem;
18425 oprops = Fcopy_sequence (oprops);
18426 tem = props;
18427 while (CONSP (tem))
18429 oprops = Fplist_put (oprops, XCAR (tem),
18430 XCAR (XCDR (tem)));
18431 tem = XCDR (XCDR (tem));
18433 props = oprops;
18436 aelt = Fassoc (elt, mode_line_proptrans_alist);
18437 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18439 /* AELT is what we want. Move it to the front
18440 without consing. */
18441 elt = XCAR (aelt);
18442 mode_line_proptrans_alist
18443 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18445 else
18447 Lisp_Object tem;
18449 /* If AELT has the wrong props, it is useless.
18450 so get rid of it. */
18451 if (! NILP (aelt))
18452 mode_line_proptrans_alist
18453 = Fdelq (aelt, mode_line_proptrans_alist);
18455 elt = Fcopy_sequence (elt);
18456 Fset_text_properties (make_number (0), Flength (elt),
18457 props, elt);
18458 /* Add this item to mode_line_proptrans_alist. */
18459 mode_line_proptrans_alist
18460 = Fcons (Fcons (elt, props),
18461 mode_line_proptrans_alist);
18462 /* Truncate mode_line_proptrans_alist
18463 to at most 50 elements. */
18464 tem = Fnthcdr (make_number (50),
18465 mode_line_proptrans_alist);
18466 if (! NILP (tem))
18467 XSETCDR (tem, Qnil);
18472 offset = 0;
18474 if (literal)
18476 prec = precision - n;
18477 switch (mode_line_target)
18479 case MODE_LINE_NOPROP:
18480 case MODE_LINE_TITLE:
18481 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18482 break;
18483 case MODE_LINE_STRING:
18484 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18485 break;
18486 case MODE_LINE_DISPLAY:
18487 n += display_string (NULL, elt, Qnil, 0, 0, it,
18488 0, prec, 0, STRING_MULTIBYTE (elt));
18489 break;
18492 break;
18495 /* Handle the non-literal case. */
18497 while ((precision <= 0 || n < precision)
18498 && SREF (elt, offset) != 0
18499 && (mode_line_target != MODE_LINE_DISPLAY
18500 || it->current_x < it->last_visible_x))
18502 EMACS_INT last_offset = offset;
18504 /* Advance to end of string or next format specifier. */
18505 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18508 if (offset - 1 != last_offset)
18510 EMACS_INT nchars, nbytes;
18512 /* Output to end of string or up to '%'. Field width
18513 is length of string. Don't output more than
18514 PRECISION allows us. */
18515 offset--;
18517 prec = c_string_width (SDATA (elt) + last_offset,
18518 offset - last_offset, precision - n,
18519 &nchars, &nbytes);
18521 switch (mode_line_target)
18523 case MODE_LINE_NOPROP:
18524 case MODE_LINE_TITLE:
18525 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18526 break;
18527 case MODE_LINE_STRING:
18529 EMACS_INT bytepos = last_offset;
18530 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18531 EMACS_INT endpos = (precision <= 0
18532 ? string_byte_to_char (elt, offset)
18533 : charpos + nchars);
18535 n += store_mode_line_string (NULL,
18536 Fsubstring (elt, make_number (charpos),
18537 make_number (endpos)),
18538 0, 0, 0, Qnil);
18540 break;
18541 case MODE_LINE_DISPLAY:
18543 EMACS_INT bytepos = last_offset;
18544 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18546 if (precision <= 0)
18547 nchars = string_byte_to_char (elt, offset) - charpos;
18548 n += display_string (NULL, elt, Qnil, 0, charpos,
18549 it, 0, nchars, 0,
18550 STRING_MULTIBYTE (elt));
18552 break;
18555 else /* c == '%' */
18557 EMACS_INT percent_position = offset;
18559 /* Get the specified minimum width. Zero means
18560 don't pad. */
18561 field = 0;
18562 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18563 field = field * 10 + c - '0';
18565 /* Don't pad beyond the total padding allowed. */
18566 if (field_width - n > 0 && field > field_width - n)
18567 field = field_width - n;
18569 /* Note that either PRECISION <= 0 or N < PRECISION. */
18570 prec = precision - n;
18572 if (c == 'M')
18573 n += display_mode_element (it, depth, field, prec,
18574 Vglobal_mode_string, props,
18575 risky);
18576 else if (c != 0)
18578 int multibyte;
18579 EMACS_INT bytepos, charpos;
18580 const unsigned char *spec;
18581 Lisp_Object string;
18583 bytepos = percent_position;
18584 charpos = (STRING_MULTIBYTE (elt)
18585 ? string_byte_to_char (elt, bytepos)
18586 : bytepos);
18587 spec = decode_mode_spec (it->w, c, field, prec, &string);
18588 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18590 switch (mode_line_target)
18592 case MODE_LINE_NOPROP:
18593 case MODE_LINE_TITLE:
18594 n += store_mode_line_noprop (spec, field, prec);
18595 break;
18596 case MODE_LINE_STRING:
18598 int len = strlen (spec);
18599 Lisp_Object tem = make_string (spec, len);
18600 props = Ftext_properties_at (make_number (charpos), elt);
18601 /* Should only keep face property in props */
18602 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18604 break;
18605 case MODE_LINE_DISPLAY:
18607 int nglyphs_before, nwritten;
18609 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18610 nwritten = display_string (spec, string, elt,
18611 charpos, 0, it,
18612 field, prec, 0,
18613 multibyte);
18615 /* Assign to the glyphs written above the
18616 string where the `%x' came from, position
18617 of the `%'. */
18618 if (nwritten > 0)
18620 struct glyph *glyph
18621 = (it->glyph_row->glyphs[TEXT_AREA]
18622 + nglyphs_before);
18623 int i;
18625 for (i = 0; i < nwritten; ++i)
18627 glyph[i].object = elt;
18628 glyph[i].charpos = charpos;
18631 n += nwritten;
18634 break;
18637 else /* c == 0 */
18638 break;
18642 break;
18644 case Lisp_Symbol:
18645 /* A symbol: process the value of the symbol recursively
18646 as if it appeared here directly. Avoid error if symbol void.
18647 Special case: if value of symbol is a string, output the string
18648 literally. */
18650 register Lisp_Object tem;
18652 /* If the variable is not marked as risky to set
18653 then its contents are risky to use. */
18654 if (NILP (Fget (elt, Qrisky_local_variable)))
18655 risky = 1;
18657 tem = Fboundp (elt);
18658 if (!NILP (tem))
18660 tem = Fsymbol_value (elt);
18661 /* If value is a string, output that string literally:
18662 don't check for % within it. */
18663 if (STRINGP (tem))
18664 literal = 1;
18666 if (!EQ (tem, elt))
18668 /* Give up right away for nil or t. */
18669 elt = tem;
18670 goto tail_recurse;
18674 break;
18676 case Lisp_Cons:
18678 register Lisp_Object car, tem;
18680 /* A cons cell: five distinct cases.
18681 If first element is :eval or :propertize, do something special.
18682 If first element is a string or a cons, process all the elements
18683 and effectively concatenate them.
18684 If first element is a negative number, truncate displaying cdr to
18685 at most that many characters. If positive, pad (with spaces)
18686 to at least that many characters.
18687 If first element is a symbol, process the cadr or caddr recursively
18688 according to whether the symbol's value is non-nil or nil. */
18689 car = XCAR (elt);
18690 if (EQ (car, QCeval))
18692 /* An element of the form (:eval FORM) means evaluate FORM
18693 and use the result as mode line elements. */
18695 if (risky)
18696 break;
18698 if (CONSP (XCDR (elt)))
18700 Lisp_Object spec;
18701 spec = safe_eval (XCAR (XCDR (elt)));
18702 n += display_mode_element (it, depth, field_width - n,
18703 precision - n, spec, props,
18704 risky);
18707 else if (EQ (car, QCpropertize))
18709 /* An element of the form (:propertize ELT PROPS...)
18710 means display ELT but applying properties PROPS. */
18712 if (risky)
18713 break;
18715 if (CONSP (XCDR (elt)))
18716 n += display_mode_element (it, depth, field_width - n,
18717 precision - n, XCAR (XCDR (elt)),
18718 XCDR (XCDR (elt)), risky);
18720 else if (SYMBOLP (car))
18722 tem = Fboundp (car);
18723 elt = XCDR (elt);
18724 if (!CONSP (elt))
18725 goto invalid;
18726 /* elt is now the cdr, and we know it is a cons cell.
18727 Use its car if CAR has a non-nil value. */
18728 if (!NILP (tem))
18730 tem = Fsymbol_value (car);
18731 if (!NILP (tem))
18733 elt = XCAR (elt);
18734 goto tail_recurse;
18737 /* Symbol's value is nil (or symbol is unbound)
18738 Get the cddr of the original list
18739 and if possible find the caddr and use that. */
18740 elt = XCDR (elt);
18741 if (NILP (elt))
18742 break;
18743 else if (!CONSP (elt))
18744 goto invalid;
18745 elt = XCAR (elt);
18746 goto tail_recurse;
18748 else if (INTEGERP (car))
18750 register int lim = XINT (car);
18751 elt = XCDR (elt);
18752 if (lim < 0)
18754 /* Negative int means reduce maximum width. */
18755 if (precision <= 0)
18756 precision = -lim;
18757 else
18758 precision = min (precision, -lim);
18760 else if (lim > 0)
18762 /* Padding specified. Don't let it be more than
18763 current maximum. */
18764 if (precision > 0)
18765 lim = min (precision, lim);
18767 /* If that's more padding than already wanted, queue it.
18768 But don't reduce padding already specified even if
18769 that is beyond the current truncation point. */
18770 field_width = max (lim, field_width);
18772 goto tail_recurse;
18774 else if (STRINGP (car) || CONSP (car))
18776 Lisp_Object halftail = elt;
18777 int len = 0;
18779 while (CONSP (elt)
18780 && (precision <= 0 || n < precision))
18782 n += display_mode_element (it, depth,
18783 /* Do padding only after the last
18784 element in the list. */
18785 (! CONSP (XCDR (elt))
18786 ? field_width - n
18787 : 0),
18788 precision - n, XCAR (elt),
18789 props, risky);
18790 elt = XCDR (elt);
18791 len++;
18792 if ((len & 1) == 0)
18793 halftail = XCDR (halftail);
18794 /* Check for cycle. */
18795 if (EQ (halftail, elt))
18796 break;
18800 break;
18802 default:
18803 invalid:
18804 elt = build_string ("*invalid*");
18805 goto tail_recurse;
18808 /* Pad to FIELD_WIDTH. */
18809 if (field_width > 0 && n < field_width)
18811 switch (mode_line_target)
18813 case MODE_LINE_NOPROP:
18814 case MODE_LINE_TITLE:
18815 n += store_mode_line_noprop ("", field_width - n, 0);
18816 break;
18817 case MODE_LINE_STRING:
18818 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18819 break;
18820 case MODE_LINE_DISPLAY:
18821 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18822 0, 0, 0);
18823 break;
18827 return n;
18830 /* Store a mode-line string element in mode_line_string_list.
18832 If STRING is non-null, display that C string. Otherwise, the Lisp
18833 string LISP_STRING is displayed.
18835 FIELD_WIDTH is the minimum number of output glyphs to produce.
18836 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18837 with spaces. FIELD_WIDTH <= 0 means don't pad.
18839 PRECISION is the maximum number of characters to output from
18840 STRING. PRECISION <= 0 means don't truncate the string.
18842 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18843 properties to the string.
18845 PROPS are the properties to add to the string.
18846 The mode_line_string_face face property is always added to the string.
18849 static int
18850 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18851 int field_width, int precision, Lisp_Object props)
18853 EMACS_INT len;
18854 int n = 0;
18856 if (string != NULL)
18858 len = strlen (string);
18859 if (precision > 0 && len > precision)
18860 len = precision;
18861 lisp_string = make_string (string, len);
18862 if (NILP (props))
18863 props = mode_line_string_face_prop;
18864 else if (!NILP (mode_line_string_face))
18866 Lisp_Object face = Fplist_get (props, Qface);
18867 props = Fcopy_sequence (props);
18868 if (NILP (face))
18869 face = mode_line_string_face;
18870 else
18871 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18872 props = Fplist_put (props, Qface, face);
18874 Fadd_text_properties (make_number (0), make_number (len),
18875 props, lisp_string);
18877 else
18879 len = XFASTINT (Flength (lisp_string));
18880 if (precision > 0 && len > precision)
18882 len = precision;
18883 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18884 precision = -1;
18886 if (!NILP (mode_line_string_face))
18888 Lisp_Object face;
18889 if (NILP (props))
18890 props = Ftext_properties_at (make_number (0), lisp_string);
18891 face = Fplist_get (props, Qface);
18892 if (NILP (face))
18893 face = mode_line_string_face;
18894 else
18895 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18896 props = Fcons (Qface, Fcons (face, Qnil));
18897 if (copy_string)
18898 lisp_string = Fcopy_sequence (lisp_string);
18900 if (!NILP (props))
18901 Fadd_text_properties (make_number (0), make_number (len),
18902 props, lisp_string);
18905 if (len > 0)
18907 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18908 n += len;
18911 if (field_width > len)
18913 field_width -= len;
18914 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18915 if (!NILP (props))
18916 Fadd_text_properties (make_number (0), make_number (field_width),
18917 props, lisp_string);
18918 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18919 n += field_width;
18922 return n;
18926 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18927 1, 4, 0,
18928 doc: /* Format a string out of a mode line format specification.
18929 First arg FORMAT specifies the mode line format (see `mode-line-format'
18930 for details) to use.
18932 Optional second arg FACE specifies the face property to put
18933 on all characters for which no face is specified.
18934 The value t means whatever face the window's mode line currently uses
18935 \(either `mode-line' or `mode-line-inactive', depending).
18936 A value of nil means the default is no face property.
18937 If FACE is an integer, the value string has no text properties.
18939 Optional third and fourth args WINDOW and BUFFER specify the window
18940 and buffer to use as the context for the formatting (defaults
18941 are the selected window and the window's buffer). */)
18942 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
18944 struct it it;
18945 int len;
18946 struct window *w;
18947 struct buffer *old_buffer = NULL;
18948 int face_id = -1;
18949 int no_props = INTEGERP (face);
18950 int count = SPECPDL_INDEX ();
18951 Lisp_Object str;
18952 int string_start = 0;
18954 if (NILP (window))
18955 window = selected_window;
18956 CHECK_WINDOW (window);
18957 w = XWINDOW (window);
18959 if (NILP (buffer))
18960 buffer = w->buffer;
18961 CHECK_BUFFER (buffer);
18963 /* Make formatting the modeline a non-op when noninteractive, otherwise
18964 there will be problems later caused by a partially initialized frame. */
18965 if (NILP (format) || noninteractive)
18966 return empty_unibyte_string;
18968 if (no_props)
18969 face = Qnil;
18971 if (!NILP (face))
18973 if (EQ (face, Qt))
18974 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18975 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18978 if (face_id < 0)
18979 face_id = DEFAULT_FACE_ID;
18981 if (XBUFFER (buffer) != current_buffer)
18982 old_buffer = current_buffer;
18984 /* Save things including mode_line_proptrans_alist,
18985 and set that to nil so that we don't alter the outer value. */
18986 record_unwind_protect (unwind_format_mode_line,
18987 format_mode_line_unwind_data
18988 (old_buffer, selected_window, 1));
18989 mode_line_proptrans_alist = Qnil;
18991 Fselect_window (window, Qt);
18992 if (old_buffer)
18993 set_buffer_internal_1 (XBUFFER (buffer));
18995 init_iterator (&it, w, -1, -1, NULL, face_id);
18997 if (no_props)
18999 mode_line_target = MODE_LINE_NOPROP;
19000 mode_line_string_face_prop = Qnil;
19001 mode_line_string_list = Qnil;
19002 string_start = MODE_LINE_NOPROP_LEN (0);
19004 else
19006 mode_line_target = MODE_LINE_STRING;
19007 mode_line_string_list = Qnil;
19008 mode_line_string_face = face;
19009 mode_line_string_face_prop
19010 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19013 push_kboard (FRAME_KBOARD (it.f));
19014 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19015 pop_kboard ();
19017 if (no_props)
19019 len = MODE_LINE_NOPROP_LEN (string_start);
19020 str = make_string (mode_line_noprop_buf + string_start, len);
19022 else
19024 mode_line_string_list = Fnreverse (mode_line_string_list);
19025 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19026 empty_unibyte_string);
19029 unbind_to (count, Qnil);
19030 return str;
19033 /* Write a null-terminated, right justified decimal representation of
19034 the positive integer D to BUF using a minimal field width WIDTH. */
19036 static void
19037 pint2str (register char *buf, register int width, register int d)
19039 register char *p = buf;
19041 if (d <= 0)
19042 *p++ = '0';
19043 else
19045 while (d > 0)
19047 *p++ = d % 10 + '0';
19048 d /= 10;
19052 for (width -= (int) (p - buf); width > 0; --width)
19053 *p++ = ' ';
19054 *p-- = '\0';
19055 while (p > buf)
19057 d = *buf;
19058 *buf++ = *p;
19059 *p-- = d;
19063 /* Write a null-terminated, right justified decimal and "human
19064 readable" representation of the nonnegative integer D to BUF using
19065 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19067 static const char power_letter[] =
19069 0, /* not used */
19070 'k', /* kilo */
19071 'M', /* mega */
19072 'G', /* giga */
19073 'T', /* tera */
19074 'P', /* peta */
19075 'E', /* exa */
19076 'Z', /* zetta */
19077 'Y' /* yotta */
19080 static void
19081 pint2hrstr (char *buf, int width, int d)
19083 /* We aim to represent the nonnegative integer D as
19084 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19085 int quotient = d;
19086 int remainder = 0;
19087 /* -1 means: do not use TENTHS. */
19088 int tenths = -1;
19089 int exponent = 0;
19091 /* Length of QUOTIENT.TENTHS as a string. */
19092 int length;
19094 char * psuffix;
19095 char * p;
19097 if (1000 <= quotient)
19099 /* Scale to the appropriate EXPONENT. */
19102 remainder = quotient % 1000;
19103 quotient /= 1000;
19104 exponent++;
19106 while (1000 <= quotient);
19108 /* Round to nearest and decide whether to use TENTHS or not. */
19109 if (quotient <= 9)
19111 tenths = remainder / 100;
19112 if (50 <= remainder % 100)
19114 if (tenths < 9)
19115 tenths++;
19116 else
19118 quotient++;
19119 if (quotient == 10)
19120 tenths = -1;
19121 else
19122 tenths = 0;
19126 else
19127 if (500 <= remainder)
19129 if (quotient < 999)
19130 quotient++;
19131 else
19133 quotient = 1;
19134 exponent++;
19135 tenths = 0;
19140 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19141 if (tenths == -1 && quotient <= 99)
19142 if (quotient <= 9)
19143 length = 1;
19144 else
19145 length = 2;
19146 else
19147 length = 3;
19148 p = psuffix = buf + max (width, length);
19150 /* Print EXPONENT. */
19151 if (exponent)
19152 *psuffix++ = power_letter[exponent];
19153 *psuffix = '\0';
19155 /* Print TENTHS. */
19156 if (tenths >= 0)
19158 *--p = '0' + tenths;
19159 *--p = '.';
19162 /* Print QUOTIENT. */
19165 int digit = quotient % 10;
19166 *--p = '0' + digit;
19168 while ((quotient /= 10) != 0);
19170 /* Print leading spaces. */
19171 while (buf < p)
19172 *--p = ' ';
19175 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19176 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19177 type of CODING_SYSTEM. Return updated pointer into BUF. */
19179 static unsigned char invalid_eol_type[] = "(*invalid*)";
19181 static char *
19182 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19184 Lisp_Object val;
19185 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19186 const unsigned char *eol_str;
19187 int eol_str_len;
19188 /* The EOL conversion we are using. */
19189 Lisp_Object eoltype;
19191 val = CODING_SYSTEM_SPEC (coding_system);
19192 eoltype = Qnil;
19194 if (!VECTORP (val)) /* Not yet decided. */
19196 if (multibyte)
19197 *buf++ = '-';
19198 if (eol_flag)
19199 eoltype = eol_mnemonic_undecided;
19200 /* Don't mention EOL conversion if it isn't decided. */
19202 else
19204 Lisp_Object attrs;
19205 Lisp_Object eolvalue;
19207 attrs = AREF (val, 0);
19208 eolvalue = AREF (val, 2);
19210 if (multibyte)
19211 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19213 if (eol_flag)
19215 /* The EOL conversion that is normal on this system. */
19217 if (NILP (eolvalue)) /* Not yet decided. */
19218 eoltype = eol_mnemonic_undecided;
19219 else if (VECTORP (eolvalue)) /* Not yet decided. */
19220 eoltype = eol_mnemonic_undecided;
19221 else /* eolvalue is Qunix, Qdos, or Qmac. */
19222 eoltype = (EQ (eolvalue, Qunix)
19223 ? eol_mnemonic_unix
19224 : (EQ (eolvalue, Qdos) == 1
19225 ? eol_mnemonic_dos : eol_mnemonic_mac));
19229 if (eol_flag)
19231 /* Mention the EOL conversion if it is not the usual one. */
19232 if (STRINGP (eoltype))
19234 eol_str = SDATA (eoltype);
19235 eol_str_len = SBYTES (eoltype);
19237 else if (CHARACTERP (eoltype))
19239 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19240 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19241 eol_str = tmp;
19243 else
19245 eol_str = invalid_eol_type;
19246 eol_str_len = sizeof (invalid_eol_type) - 1;
19248 memcpy (buf, eol_str, eol_str_len);
19249 buf += eol_str_len;
19252 return buf;
19255 /* Return a string for the output of a mode line %-spec for window W,
19256 generated by character C. PRECISION >= 0 means don't return a
19257 string longer than that value. FIELD_WIDTH > 0 means pad the
19258 string returned with spaces to that value. Return a Lisp string in
19259 *STRING if the resulting string is taken from that Lisp string.
19261 Note we operate on the current buffer for most purposes,
19262 the exception being w->base_line_pos. */
19264 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19266 static const char *
19267 decode_mode_spec (struct window *w, register int c, int field_width,
19268 int precision, Lisp_Object *string)
19270 Lisp_Object obj;
19271 struct frame *f = XFRAME (WINDOW_FRAME (w));
19272 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19273 struct buffer *b = current_buffer;
19275 obj = Qnil;
19276 *string = Qnil;
19278 switch (c)
19280 case '*':
19281 if (!NILP (b->read_only))
19282 return "%";
19283 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19284 return "*";
19285 return "-";
19287 case '+':
19288 /* This differs from %* only for a modified read-only buffer. */
19289 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19290 return "*";
19291 if (!NILP (b->read_only))
19292 return "%";
19293 return "-";
19295 case '&':
19296 /* This differs from %* in ignoring read-only-ness. */
19297 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19298 return "*";
19299 return "-";
19301 case '%':
19302 return "%";
19304 case '[':
19306 int i;
19307 char *p;
19309 if (command_loop_level > 5)
19310 return "[[[... ";
19311 p = decode_mode_spec_buf;
19312 for (i = 0; i < command_loop_level; i++)
19313 *p++ = '[';
19314 *p = 0;
19315 return decode_mode_spec_buf;
19318 case ']':
19320 int i;
19321 char *p;
19323 if (command_loop_level > 5)
19324 return " ...]]]";
19325 p = decode_mode_spec_buf;
19326 for (i = 0; i < command_loop_level; i++)
19327 *p++ = ']';
19328 *p = 0;
19329 return decode_mode_spec_buf;
19332 case '-':
19334 register int i;
19336 /* Let lots_of_dashes be a string of infinite length. */
19337 if (mode_line_target == MODE_LINE_NOPROP ||
19338 mode_line_target == MODE_LINE_STRING)
19339 return "--";
19340 if (field_width <= 0
19341 || field_width > sizeof (lots_of_dashes))
19343 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19344 decode_mode_spec_buf[i] = '-';
19345 decode_mode_spec_buf[i] = '\0';
19346 return decode_mode_spec_buf;
19348 else
19349 return lots_of_dashes;
19352 case 'b':
19353 obj = b->name;
19354 break;
19356 case 'c':
19357 /* %c and %l are ignored in `frame-title-format'.
19358 (In redisplay_internal, the frame title is drawn _before_ the
19359 windows are updated, so the stuff which depends on actual
19360 window contents (such as %l) may fail to render properly, or
19361 even crash emacs.) */
19362 if (mode_line_target == MODE_LINE_TITLE)
19363 return "";
19364 else
19366 int col = (int) current_column (); /* iftc */
19367 w->column_number_displayed = make_number (col);
19368 pint2str (decode_mode_spec_buf, field_width, col);
19369 return decode_mode_spec_buf;
19372 case 'e':
19373 #ifndef SYSTEM_MALLOC
19375 if (NILP (Vmemory_full))
19376 return "";
19377 else
19378 return "!MEM FULL! ";
19380 #else
19381 return "";
19382 #endif
19384 case 'F':
19385 /* %F displays the frame name. */
19386 if (!NILP (f->title))
19387 return (char *) SDATA (f->title);
19388 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19389 return (char *) SDATA (f->name);
19390 return "Emacs";
19392 case 'f':
19393 obj = b->filename;
19394 break;
19396 case 'i':
19398 EMACS_INT size = ZV - BEGV;
19399 pint2str (decode_mode_spec_buf, field_width, size);
19400 return decode_mode_spec_buf;
19403 case 'I':
19405 EMACS_INT size = ZV - BEGV;
19406 pint2hrstr (decode_mode_spec_buf, field_width, size);
19407 return decode_mode_spec_buf;
19410 case 'l':
19412 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19413 int topline, nlines, height;
19414 EMACS_INT junk;
19416 /* %c and %l are ignored in `frame-title-format'. */
19417 if (mode_line_target == MODE_LINE_TITLE)
19418 return "";
19420 startpos = XMARKER (w->start)->charpos;
19421 startpos_byte = marker_byte_position (w->start);
19422 height = WINDOW_TOTAL_LINES (w);
19424 /* If we decided that this buffer isn't suitable for line numbers,
19425 don't forget that too fast. */
19426 if (EQ (w->base_line_pos, w->buffer))
19427 goto no_value;
19428 /* But do forget it, if the window shows a different buffer now. */
19429 else if (BUFFERP (w->base_line_pos))
19430 w->base_line_pos = Qnil;
19432 /* If the buffer is very big, don't waste time. */
19433 if (INTEGERP (Vline_number_display_limit)
19434 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19436 w->base_line_pos = Qnil;
19437 w->base_line_number = Qnil;
19438 goto no_value;
19441 if (INTEGERP (w->base_line_number)
19442 && INTEGERP (w->base_line_pos)
19443 && XFASTINT (w->base_line_pos) <= startpos)
19445 line = XFASTINT (w->base_line_number);
19446 linepos = XFASTINT (w->base_line_pos);
19447 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19449 else
19451 line = 1;
19452 linepos = BUF_BEGV (b);
19453 linepos_byte = BUF_BEGV_BYTE (b);
19456 /* Count lines from base line to window start position. */
19457 nlines = display_count_lines (linepos, linepos_byte,
19458 startpos_byte,
19459 startpos, &junk);
19461 topline = nlines + line;
19463 /* Determine a new base line, if the old one is too close
19464 or too far away, or if we did not have one.
19465 "Too close" means it's plausible a scroll-down would
19466 go back past it. */
19467 if (startpos == BUF_BEGV (b))
19469 w->base_line_number = make_number (topline);
19470 w->base_line_pos = make_number (BUF_BEGV (b));
19472 else if (nlines < height + 25 || nlines > height * 3 + 50
19473 || linepos == BUF_BEGV (b))
19475 EMACS_INT limit = BUF_BEGV (b);
19476 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19477 EMACS_INT position;
19478 int distance = (height * 2 + 30) * line_number_display_limit_width;
19480 if (startpos - distance > limit)
19482 limit = startpos - distance;
19483 limit_byte = CHAR_TO_BYTE (limit);
19486 nlines = display_count_lines (startpos, startpos_byte,
19487 limit_byte,
19488 - (height * 2 + 30),
19489 &position);
19490 /* If we couldn't find the lines we wanted within
19491 line_number_display_limit_width chars per line,
19492 give up on line numbers for this window. */
19493 if (position == limit_byte && limit == startpos - distance)
19495 w->base_line_pos = w->buffer;
19496 w->base_line_number = Qnil;
19497 goto no_value;
19500 w->base_line_number = make_number (topline - nlines);
19501 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19504 /* Now count lines from the start pos to point. */
19505 nlines = display_count_lines (startpos, startpos_byte,
19506 PT_BYTE, PT, &junk);
19508 /* Record that we did display the line number. */
19509 line_number_displayed = 1;
19511 /* Make the string to show. */
19512 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19513 return decode_mode_spec_buf;
19514 no_value:
19516 char* p = decode_mode_spec_buf;
19517 int pad = field_width - 2;
19518 while (pad-- > 0)
19519 *p++ = ' ';
19520 *p++ = '?';
19521 *p++ = '?';
19522 *p = '\0';
19523 return decode_mode_spec_buf;
19526 break;
19528 case 'm':
19529 obj = b->mode_name;
19530 break;
19532 case 'n':
19533 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19534 return " Narrow";
19535 break;
19537 case 'p':
19539 EMACS_INT pos = marker_position (w->start);
19540 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19542 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19544 if (pos <= BUF_BEGV (b))
19545 return "All";
19546 else
19547 return "Bottom";
19549 else if (pos <= BUF_BEGV (b))
19550 return "Top";
19551 else
19553 if (total > 1000000)
19554 /* Do it differently for a large value, to avoid overflow. */
19555 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19556 else
19557 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19558 /* We can't normally display a 3-digit number,
19559 so get us a 2-digit number that is close. */
19560 if (total == 100)
19561 total = 99;
19562 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19563 return decode_mode_spec_buf;
19567 /* Display percentage of size above the bottom of the screen. */
19568 case 'P':
19570 EMACS_INT toppos = marker_position (w->start);
19571 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19572 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19574 if (botpos >= BUF_ZV (b))
19576 if (toppos <= BUF_BEGV (b))
19577 return "All";
19578 else
19579 return "Bottom";
19581 else
19583 if (total > 1000000)
19584 /* Do it differently for a large value, to avoid overflow. */
19585 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19586 else
19587 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19588 /* We can't normally display a 3-digit number,
19589 so get us a 2-digit number that is close. */
19590 if (total == 100)
19591 total = 99;
19592 if (toppos <= BUF_BEGV (b))
19593 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19594 else
19595 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19596 return decode_mode_spec_buf;
19600 case 's':
19601 /* status of process */
19602 obj = Fget_buffer_process (Fcurrent_buffer ());
19603 if (NILP (obj))
19604 return "no process";
19605 #ifndef MSDOS
19606 obj = Fsymbol_name (Fprocess_status (obj));
19607 #endif
19608 break;
19610 case '@':
19612 int count = inhibit_garbage_collection ();
19613 Lisp_Object val = call1 (intern ("file-remote-p"),
19614 current_buffer->directory);
19615 unbind_to (count, Qnil);
19617 if (NILP (val))
19618 return "-";
19619 else
19620 return "@";
19623 case 't': /* indicate TEXT or BINARY */
19624 #ifdef MODE_LINE_BINARY_TEXT
19625 return MODE_LINE_BINARY_TEXT (b);
19626 #else
19627 return "T";
19628 #endif
19630 case 'z':
19631 /* coding-system (not including end-of-line format) */
19632 case 'Z':
19633 /* coding-system (including end-of-line type) */
19635 int eol_flag = (c == 'Z');
19636 char *p = decode_mode_spec_buf;
19638 if (! FRAME_WINDOW_P (f))
19640 /* No need to mention EOL here--the terminal never needs
19641 to do EOL conversion. */
19642 p = decode_mode_spec_coding (CODING_ID_NAME
19643 (FRAME_KEYBOARD_CODING (f)->id),
19644 p, 0);
19645 p = decode_mode_spec_coding (CODING_ID_NAME
19646 (FRAME_TERMINAL_CODING (f)->id),
19647 p, 0);
19649 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19650 p, eol_flag);
19652 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19653 #ifdef subprocesses
19654 obj = Fget_buffer_process (Fcurrent_buffer ());
19655 if (PROCESSP (obj))
19657 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19658 p, eol_flag);
19659 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19660 p, eol_flag);
19662 #endif /* subprocesses */
19663 #endif /* 0 */
19664 *p = 0;
19665 return decode_mode_spec_buf;
19669 if (STRINGP (obj))
19671 *string = obj;
19672 return (char *) SDATA (obj);
19674 else
19675 return "";
19679 /* Count up to COUNT lines starting from START / START_BYTE.
19680 But don't go beyond LIMIT_BYTE.
19681 Return the number of lines thus found (always nonnegative).
19683 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19685 static int
19686 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19687 EMACS_INT limit_byte, int count,
19688 EMACS_INT *byte_pos_ptr)
19690 register unsigned char *cursor;
19691 unsigned char *base;
19693 register int ceiling;
19694 register unsigned char *ceiling_addr;
19695 int orig_count = count;
19697 /* If we are not in selective display mode,
19698 check only for newlines. */
19699 int selective_display = (!NILP (current_buffer->selective_display)
19700 && !INTEGERP (current_buffer->selective_display));
19702 if (count > 0)
19704 while (start_byte < limit_byte)
19706 ceiling = BUFFER_CEILING_OF (start_byte);
19707 ceiling = min (limit_byte - 1, ceiling);
19708 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19709 base = (cursor = BYTE_POS_ADDR (start_byte));
19710 while (1)
19712 if (selective_display)
19713 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19715 else
19716 while (*cursor != '\n' && ++cursor != ceiling_addr)
19719 if (cursor != ceiling_addr)
19721 if (--count == 0)
19723 start_byte += cursor - base + 1;
19724 *byte_pos_ptr = start_byte;
19725 return orig_count;
19727 else
19728 if (++cursor == ceiling_addr)
19729 break;
19731 else
19732 break;
19734 start_byte += cursor - base;
19737 else
19739 while (start_byte > limit_byte)
19741 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19742 ceiling = max (limit_byte, ceiling);
19743 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19744 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19745 while (1)
19747 if (selective_display)
19748 while (--cursor != ceiling_addr
19749 && *cursor != '\n' && *cursor != 015)
19751 else
19752 while (--cursor != ceiling_addr && *cursor != '\n')
19755 if (cursor != ceiling_addr)
19757 if (++count == 0)
19759 start_byte += cursor - base + 1;
19760 *byte_pos_ptr = start_byte;
19761 /* When scanning backwards, we should
19762 not count the newline posterior to which we stop. */
19763 return - orig_count - 1;
19766 else
19767 break;
19769 /* Here we add 1 to compensate for the last decrement
19770 of CURSOR, which took it past the valid range. */
19771 start_byte += cursor - base + 1;
19775 *byte_pos_ptr = limit_byte;
19777 if (count < 0)
19778 return - orig_count + count;
19779 return orig_count - count;
19785 /***********************************************************************
19786 Displaying strings
19787 ***********************************************************************/
19789 /* Display a NUL-terminated string, starting with index START.
19791 If STRING is non-null, display that C string. Otherwise, the Lisp
19792 string LISP_STRING is displayed. There's a case that STRING is
19793 non-null and LISP_STRING is not nil. It means STRING is a string
19794 data of LISP_STRING. In that case, we display LISP_STRING while
19795 ignoring its text properties.
19797 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19798 FACE_STRING. Display STRING or LISP_STRING with the face at
19799 FACE_STRING_POS in FACE_STRING:
19801 Display the string in the environment given by IT, but use the
19802 standard display table, temporarily.
19804 FIELD_WIDTH is the minimum number of output glyphs to produce.
19805 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19806 with spaces. If STRING has more characters, more than FIELD_WIDTH
19807 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19809 PRECISION is the maximum number of characters to output from
19810 STRING. PRECISION < 0 means don't truncate the string.
19812 This is roughly equivalent to printf format specifiers:
19814 FIELD_WIDTH PRECISION PRINTF
19815 ----------------------------------------
19816 -1 -1 %s
19817 -1 10 %.10s
19818 10 -1 %10s
19819 20 10 %20.10s
19821 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19822 display them, and < 0 means obey the current buffer's value of
19823 enable_multibyte_characters.
19825 Value is the number of columns displayed. */
19827 static int
19828 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19829 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19830 int field_width, int precision, int max_x, int multibyte)
19832 int hpos_at_start = it->hpos;
19833 int saved_face_id = it->face_id;
19834 struct glyph_row *row = it->glyph_row;
19836 /* Initialize the iterator IT for iteration over STRING beginning
19837 with index START. */
19838 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19839 precision, field_width, multibyte);
19840 if (string && STRINGP (lisp_string))
19841 /* LISP_STRING is the one returned by decode_mode_spec. We should
19842 ignore its text properties. */
19843 it->stop_charpos = -1;
19845 /* If displaying STRING, set up the face of the iterator
19846 from LISP_STRING, if that's given. */
19847 if (STRINGP (face_string))
19849 EMACS_INT endptr;
19850 struct face *face;
19852 it->face_id
19853 = face_at_string_position (it->w, face_string, face_string_pos,
19854 0, it->region_beg_charpos,
19855 it->region_end_charpos,
19856 &endptr, it->base_face_id, 0);
19857 face = FACE_FROM_ID (it->f, it->face_id);
19858 it->face_box_p = face->box != FACE_NO_BOX;
19861 /* Set max_x to the maximum allowed X position. Don't let it go
19862 beyond the right edge of the window. */
19863 if (max_x <= 0)
19864 max_x = it->last_visible_x;
19865 else
19866 max_x = min (max_x, it->last_visible_x);
19868 /* Skip over display elements that are not visible. because IT->w is
19869 hscrolled. */
19870 if (it->current_x < it->first_visible_x)
19871 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19872 MOVE_TO_POS | MOVE_TO_X);
19874 row->ascent = it->max_ascent;
19875 row->height = it->max_ascent + it->max_descent;
19876 row->phys_ascent = it->max_phys_ascent;
19877 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19878 row->extra_line_spacing = it->max_extra_line_spacing;
19880 /* This condition is for the case that we are called with current_x
19881 past last_visible_x. */
19882 while (it->current_x < max_x)
19884 int x_before, x, n_glyphs_before, i, nglyphs;
19886 /* Get the next display element. */
19887 if (!get_next_display_element (it))
19888 break;
19890 /* Produce glyphs. */
19891 x_before = it->current_x;
19892 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19893 PRODUCE_GLYPHS (it);
19895 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19896 i = 0;
19897 x = x_before;
19898 while (i < nglyphs)
19900 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19902 if (it->line_wrap != TRUNCATE
19903 && x + glyph->pixel_width > max_x)
19905 /* End of continued line or max_x reached. */
19906 if (CHAR_GLYPH_PADDING_P (*glyph))
19908 /* A wide character is unbreakable. */
19909 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19910 it->current_x = x_before;
19912 else
19914 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19915 it->current_x = x;
19917 break;
19919 else if (x + glyph->pixel_width >= it->first_visible_x)
19921 /* Glyph is at least partially visible. */
19922 ++it->hpos;
19923 if (x < it->first_visible_x)
19924 it->glyph_row->x = x - it->first_visible_x;
19926 else
19928 /* Glyph is off the left margin of the display area.
19929 Should not happen. */
19930 abort ();
19933 row->ascent = max (row->ascent, it->max_ascent);
19934 row->height = max (row->height, it->max_ascent + it->max_descent);
19935 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19936 row->phys_height = max (row->phys_height,
19937 it->max_phys_ascent + it->max_phys_descent);
19938 row->extra_line_spacing = max (row->extra_line_spacing,
19939 it->max_extra_line_spacing);
19940 x += glyph->pixel_width;
19941 ++i;
19944 /* Stop if max_x reached. */
19945 if (i < nglyphs)
19946 break;
19948 /* Stop at line ends. */
19949 if (ITERATOR_AT_END_OF_LINE_P (it))
19951 it->continuation_lines_width = 0;
19952 break;
19955 set_iterator_to_next (it, 1);
19957 /* Stop if truncating at the right edge. */
19958 if (it->line_wrap == TRUNCATE
19959 && it->current_x >= it->last_visible_x)
19961 /* Add truncation mark, but don't do it if the line is
19962 truncated at a padding space. */
19963 if (IT_CHARPOS (*it) < it->string_nchars)
19965 if (!FRAME_WINDOW_P (it->f))
19967 int i, n;
19969 if (it->current_x > it->last_visible_x)
19971 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19972 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19973 break;
19974 for (n = row->used[TEXT_AREA]; i < n; ++i)
19976 row->used[TEXT_AREA] = i;
19977 produce_special_glyphs (it, IT_TRUNCATION);
19980 produce_special_glyphs (it, IT_TRUNCATION);
19982 it->glyph_row->truncated_on_right_p = 1;
19984 break;
19988 /* Maybe insert a truncation at the left. */
19989 if (it->first_visible_x
19990 && IT_CHARPOS (*it) > 0)
19992 if (!FRAME_WINDOW_P (it->f))
19993 insert_left_trunc_glyphs (it);
19994 it->glyph_row->truncated_on_left_p = 1;
19997 it->face_id = saved_face_id;
19999 /* Value is number of columns displayed. */
20000 return it->hpos - hpos_at_start;
20005 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20006 appears as an element of LIST or as the car of an element of LIST.
20007 If PROPVAL is a list, compare each element against LIST in that
20008 way, and return 1/2 if any element of PROPVAL is found in LIST.
20009 Otherwise return 0. This function cannot quit.
20010 The return value is 2 if the text is invisible but with an ellipsis
20011 and 1 if it's invisible and without an ellipsis. */
20014 invisible_p (register Lisp_Object propval, Lisp_Object list)
20016 register Lisp_Object tail, proptail;
20018 for (tail = list; CONSP (tail); tail = XCDR (tail))
20020 register Lisp_Object tem;
20021 tem = XCAR (tail);
20022 if (EQ (propval, tem))
20023 return 1;
20024 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20025 return NILP (XCDR (tem)) ? 1 : 2;
20028 if (CONSP (propval))
20030 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20032 Lisp_Object propelt;
20033 propelt = XCAR (proptail);
20034 for (tail = list; CONSP (tail); tail = XCDR (tail))
20036 register Lisp_Object tem;
20037 tem = XCAR (tail);
20038 if (EQ (propelt, tem))
20039 return 1;
20040 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20041 return NILP (XCDR (tem)) ? 1 : 2;
20046 return 0;
20049 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20050 doc: /* Non-nil if the property makes the text invisible.
20051 POS-OR-PROP can be a marker or number, in which case it is taken to be
20052 a position in the current buffer and the value of the `invisible' property
20053 is checked; or it can be some other value, which is then presumed to be the
20054 value of the `invisible' property of the text of interest.
20055 The non-nil value returned can be t for truly invisible text or something
20056 else if the text is replaced by an ellipsis. */)
20057 (Lisp_Object pos_or_prop)
20059 Lisp_Object prop
20060 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20061 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20062 : pos_or_prop);
20063 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20064 return (invis == 0 ? Qnil
20065 : invis == 1 ? Qt
20066 : make_number (invis));
20069 /* Calculate a width or height in pixels from a specification using
20070 the following elements:
20072 SPEC ::=
20073 NUM - a (fractional) multiple of the default font width/height
20074 (NUM) - specifies exactly NUM pixels
20075 UNIT - a fixed number of pixels, see below.
20076 ELEMENT - size of a display element in pixels, see below.
20077 (NUM . SPEC) - equals NUM * SPEC
20078 (+ SPEC SPEC ...) - add pixel values
20079 (- SPEC SPEC ...) - subtract pixel values
20080 (- SPEC) - negate pixel value
20082 NUM ::=
20083 INT or FLOAT - a number constant
20084 SYMBOL - use symbol's (buffer local) variable binding.
20086 UNIT ::=
20087 in - pixels per inch *)
20088 mm - pixels per 1/1000 meter *)
20089 cm - pixels per 1/100 meter *)
20090 width - width of current font in pixels.
20091 height - height of current font in pixels.
20093 *) using the ratio(s) defined in display-pixels-per-inch.
20095 ELEMENT ::=
20097 left-fringe - left fringe width in pixels
20098 right-fringe - right fringe width in pixels
20100 left-margin - left margin width in pixels
20101 right-margin - right margin width in pixels
20103 scroll-bar - scroll-bar area width in pixels
20105 Examples:
20107 Pixels corresponding to 5 inches:
20108 (5 . in)
20110 Total width of non-text areas on left side of window (if scroll-bar is on left):
20111 '(space :width (+ left-fringe left-margin scroll-bar))
20113 Align to first text column (in header line):
20114 '(space :align-to 0)
20116 Align to middle of text area minus half the width of variable `my-image'
20117 containing a loaded image:
20118 '(space :align-to (0.5 . (- text my-image)))
20120 Width of left margin minus width of 1 character in the default font:
20121 '(space :width (- left-margin 1))
20123 Width of left margin minus width of 2 characters in the current font:
20124 '(space :width (- left-margin (2 . width)))
20126 Center 1 character over left-margin (in header line):
20127 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20129 Different ways to express width of left fringe plus left margin minus one pixel:
20130 '(space :width (- (+ left-fringe left-margin) (1)))
20131 '(space :width (+ left-fringe left-margin (- (1))))
20132 '(space :width (+ left-fringe left-margin (-1)))
20136 #define NUMVAL(X) \
20137 ((INTEGERP (X) || FLOATP (X)) \
20138 ? XFLOATINT (X) \
20139 : - 1)
20142 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20143 struct font *font, int width_p, int *align_to)
20145 double pixels;
20147 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20148 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20150 if (NILP (prop))
20151 return OK_PIXELS (0);
20153 xassert (FRAME_LIVE_P (it->f));
20155 if (SYMBOLP (prop))
20157 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20159 char *unit = SDATA (SYMBOL_NAME (prop));
20161 if (unit[0] == 'i' && unit[1] == 'n')
20162 pixels = 1.0;
20163 else if (unit[0] == 'm' && unit[1] == 'm')
20164 pixels = 25.4;
20165 else if (unit[0] == 'c' && unit[1] == 'm')
20166 pixels = 2.54;
20167 else
20168 pixels = 0;
20169 if (pixels > 0)
20171 double ppi;
20172 #ifdef HAVE_WINDOW_SYSTEM
20173 if (FRAME_WINDOW_P (it->f)
20174 && (ppi = (width_p
20175 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20176 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20177 ppi > 0))
20178 return OK_PIXELS (ppi / pixels);
20179 #endif
20181 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20182 || (CONSP (Vdisplay_pixels_per_inch)
20183 && (ppi = (width_p
20184 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20185 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20186 ppi > 0)))
20187 return OK_PIXELS (ppi / pixels);
20189 return 0;
20193 #ifdef HAVE_WINDOW_SYSTEM
20194 if (EQ (prop, Qheight))
20195 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20196 if (EQ (prop, Qwidth))
20197 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20198 #else
20199 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20200 return OK_PIXELS (1);
20201 #endif
20203 if (EQ (prop, Qtext))
20204 return OK_PIXELS (width_p
20205 ? window_box_width (it->w, TEXT_AREA)
20206 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20208 if (align_to && *align_to < 0)
20210 *res = 0;
20211 if (EQ (prop, Qleft))
20212 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20213 if (EQ (prop, Qright))
20214 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20215 if (EQ (prop, Qcenter))
20216 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20217 + window_box_width (it->w, TEXT_AREA) / 2);
20218 if (EQ (prop, Qleft_fringe))
20219 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20220 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20221 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20222 if (EQ (prop, Qright_fringe))
20223 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20224 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20225 : window_box_right_offset (it->w, TEXT_AREA));
20226 if (EQ (prop, Qleft_margin))
20227 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20228 if (EQ (prop, Qright_margin))
20229 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20230 if (EQ (prop, Qscroll_bar))
20231 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20233 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20234 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20235 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20236 : 0)));
20238 else
20240 if (EQ (prop, Qleft_fringe))
20241 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20242 if (EQ (prop, Qright_fringe))
20243 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20244 if (EQ (prop, Qleft_margin))
20245 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20246 if (EQ (prop, Qright_margin))
20247 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20248 if (EQ (prop, Qscroll_bar))
20249 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20252 prop = Fbuffer_local_value (prop, it->w->buffer);
20255 if (INTEGERP (prop) || FLOATP (prop))
20257 int base_unit = (width_p
20258 ? FRAME_COLUMN_WIDTH (it->f)
20259 : FRAME_LINE_HEIGHT (it->f));
20260 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20263 if (CONSP (prop))
20265 Lisp_Object car = XCAR (prop);
20266 Lisp_Object cdr = XCDR (prop);
20268 if (SYMBOLP (car))
20270 #ifdef HAVE_WINDOW_SYSTEM
20271 if (FRAME_WINDOW_P (it->f)
20272 && valid_image_p (prop))
20274 int id = lookup_image (it->f, prop);
20275 struct image *img = IMAGE_FROM_ID (it->f, id);
20277 return OK_PIXELS (width_p ? img->width : img->height);
20279 #endif
20280 if (EQ (car, Qplus) || EQ (car, Qminus))
20282 int first = 1;
20283 double px;
20285 pixels = 0;
20286 while (CONSP (cdr))
20288 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20289 font, width_p, align_to))
20290 return 0;
20291 if (first)
20292 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20293 else
20294 pixels += px;
20295 cdr = XCDR (cdr);
20297 if (EQ (car, Qminus))
20298 pixels = -pixels;
20299 return OK_PIXELS (pixels);
20302 car = Fbuffer_local_value (car, it->w->buffer);
20305 if (INTEGERP (car) || FLOATP (car))
20307 double fact;
20308 pixels = XFLOATINT (car);
20309 if (NILP (cdr))
20310 return OK_PIXELS (pixels);
20311 if (calc_pixel_width_or_height (&fact, it, cdr,
20312 font, width_p, align_to))
20313 return OK_PIXELS (pixels * fact);
20314 return 0;
20317 return 0;
20320 return 0;
20324 /***********************************************************************
20325 Glyph Display
20326 ***********************************************************************/
20328 #ifdef HAVE_WINDOW_SYSTEM
20330 #if GLYPH_DEBUG
20332 void
20333 dump_glyph_string (s)
20334 struct glyph_string *s;
20336 fprintf (stderr, "glyph string\n");
20337 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20338 s->x, s->y, s->width, s->height);
20339 fprintf (stderr, " ybase = %d\n", s->ybase);
20340 fprintf (stderr, " hl = %d\n", s->hl);
20341 fprintf (stderr, " left overhang = %d, right = %d\n",
20342 s->left_overhang, s->right_overhang);
20343 fprintf (stderr, " nchars = %d\n", s->nchars);
20344 fprintf (stderr, " extends to end of line = %d\n",
20345 s->extends_to_end_of_line_p);
20346 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20347 fprintf (stderr, " bg width = %d\n", s->background_width);
20350 #endif /* GLYPH_DEBUG */
20352 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20353 of XChar2b structures for S; it can't be allocated in
20354 init_glyph_string because it must be allocated via `alloca'. W
20355 is the window on which S is drawn. ROW and AREA are the glyph row
20356 and area within the row from which S is constructed. START is the
20357 index of the first glyph structure covered by S. HL is a
20358 face-override for drawing S. */
20360 #ifdef HAVE_NTGUI
20361 #define OPTIONAL_HDC(hdc) HDC hdc,
20362 #define DECLARE_HDC(hdc) HDC hdc;
20363 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20364 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20365 #endif
20367 #ifndef OPTIONAL_HDC
20368 #define OPTIONAL_HDC(hdc)
20369 #define DECLARE_HDC(hdc)
20370 #define ALLOCATE_HDC(hdc, f)
20371 #define RELEASE_HDC(hdc, f)
20372 #endif
20374 static void
20375 init_glyph_string (struct glyph_string *s,
20376 OPTIONAL_HDC (hdc)
20377 XChar2b *char2b, struct window *w, struct glyph_row *row,
20378 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20380 memset (s, 0, sizeof *s);
20381 s->w = w;
20382 s->f = XFRAME (w->frame);
20383 #ifdef HAVE_NTGUI
20384 s->hdc = hdc;
20385 #endif
20386 s->display = FRAME_X_DISPLAY (s->f);
20387 s->window = FRAME_X_WINDOW (s->f);
20388 s->char2b = char2b;
20389 s->hl = hl;
20390 s->row = row;
20391 s->area = area;
20392 s->first_glyph = row->glyphs[area] + start;
20393 s->height = row->height;
20394 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20395 s->ybase = s->y + row->ascent;
20399 /* Append the list of glyph strings with head H and tail T to the list
20400 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20402 static INLINE void
20403 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20404 struct glyph_string *h, struct glyph_string *t)
20406 if (h)
20408 if (*head)
20409 (*tail)->next = h;
20410 else
20411 *head = h;
20412 h->prev = *tail;
20413 *tail = t;
20418 /* Prepend the list of glyph strings with head H and tail T to the
20419 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20420 result. */
20422 static INLINE void
20423 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20424 struct glyph_string *h, struct glyph_string *t)
20426 if (h)
20428 if (*head)
20429 (*head)->prev = t;
20430 else
20431 *tail = t;
20432 t->next = *head;
20433 *head = h;
20438 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20439 Set *HEAD and *TAIL to the resulting list. */
20441 static INLINE void
20442 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20443 struct glyph_string *s)
20445 s->next = s->prev = NULL;
20446 append_glyph_string_lists (head, tail, s, s);
20450 /* Get face and two-byte form of character C in face FACE_ID on frame
20451 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20452 means we want to display multibyte text. DISPLAY_P non-zero means
20453 make sure that X resources for the face returned are allocated.
20454 Value is a pointer to a realized face that is ready for display if
20455 DISPLAY_P is non-zero. */
20457 static INLINE struct face *
20458 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20459 XChar2b *char2b, int multibyte_p, int display_p)
20461 struct face *face = FACE_FROM_ID (f, face_id);
20463 if (face->font)
20465 unsigned code = face->font->driver->encode_char (face->font, c);
20467 if (code != FONT_INVALID_CODE)
20468 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20469 else
20470 STORE_XCHAR2B (char2b, 0, 0);
20473 /* Make sure X resources of the face are allocated. */
20474 #ifdef HAVE_X_WINDOWS
20475 if (display_p)
20476 #endif
20478 xassert (face != NULL);
20479 PREPARE_FACE_FOR_DISPLAY (f, face);
20482 return face;
20486 /* Get face and two-byte form of character glyph GLYPH on frame F.
20487 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20488 a pointer to a realized face that is ready for display. */
20490 static INLINE struct face *
20491 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20492 XChar2b *char2b, int *two_byte_p)
20494 struct face *face;
20496 xassert (glyph->type == CHAR_GLYPH);
20497 face = FACE_FROM_ID (f, glyph->face_id);
20499 if (two_byte_p)
20500 *two_byte_p = 0;
20502 if (face->font)
20504 unsigned code;
20506 if (CHAR_BYTE8_P (glyph->u.ch))
20507 code = CHAR_TO_BYTE8 (glyph->u.ch);
20508 else
20509 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20511 if (code != FONT_INVALID_CODE)
20512 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20513 else
20514 STORE_XCHAR2B (char2b, 0, 0);
20517 /* Make sure X resources of the face are allocated. */
20518 xassert (face != NULL);
20519 PREPARE_FACE_FOR_DISPLAY (f, face);
20520 return face;
20524 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20525 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20527 static INLINE int
20528 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20530 unsigned code;
20532 if (CHAR_BYTE8_P (c))
20533 code = CHAR_TO_BYTE8 (c);
20534 else
20535 code = font->driver->encode_char (font, c);
20537 if (code == FONT_INVALID_CODE)
20538 return 0;
20539 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20540 return 1;
20544 /* Fill glyph string S with composition components specified by S->cmp.
20546 BASE_FACE is the base face of the composition.
20547 S->cmp_from is the index of the first component for S.
20549 OVERLAPS non-zero means S should draw the foreground only, and use
20550 its physical height for clipping. See also draw_glyphs.
20552 Value is the index of a component not in S. */
20554 static int
20555 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20556 int overlaps)
20558 int i;
20559 /* For all glyphs of this composition, starting at the offset
20560 S->cmp_from, until we reach the end of the definition or encounter a
20561 glyph that requires the different face, add it to S. */
20562 struct face *face;
20564 xassert (s);
20566 s->for_overlaps = overlaps;
20567 s->face = NULL;
20568 s->font = NULL;
20569 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20571 int c = COMPOSITION_GLYPH (s->cmp, i);
20573 if (c != '\t')
20575 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20576 -1, Qnil);
20578 face = get_char_face_and_encoding (s->f, c, face_id,
20579 s->char2b + i, 1, 1);
20580 if (face)
20582 if (! s->face)
20584 s->face = face;
20585 s->font = s->face->font;
20587 else if (s->face != face)
20588 break;
20591 ++s->nchars;
20593 s->cmp_to = i;
20595 /* All glyph strings for the same composition has the same width,
20596 i.e. the width set for the first component of the composition. */
20597 s->width = s->first_glyph->pixel_width;
20599 /* If the specified font could not be loaded, use the frame's
20600 default font, but record the fact that we couldn't load it in
20601 the glyph string so that we can draw rectangles for the
20602 characters of the glyph string. */
20603 if (s->font == NULL)
20605 s->font_not_found_p = 1;
20606 s->font = FRAME_FONT (s->f);
20609 /* Adjust base line for subscript/superscript text. */
20610 s->ybase += s->first_glyph->voffset;
20612 /* This glyph string must always be drawn with 16-bit functions. */
20613 s->two_byte_p = 1;
20615 return s->cmp_to;
20618 static int
20619 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20620 int start, int end, int overlaps)
20622 struct glyph *glyph, *last;
20623 Lisp_Object lgstring;
20624 int i;
20626 s->for_overlaps = overlaps;
20627 glyph = s->row->glyphs[s->area] + start;
20628 last = s->row->glyphs[s->area] + end;
20629 s->cmp_id = glyph->u.cmp.id;
20630 s->cmp_from = glyph->slice.cmp.from;
20631 s->cmp_to = glyph->slice.cmp.to + 1;
20632 s->face = FACE_FROM_ID (s->f, face_id);
20633 lgstring = composition_gstring_from_id (s->cmp_id);
20634 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20635 glyph++;
20636 while (glyph < last
20637 && glyph->u.cmp.automatic
20638 && glyph->u.cmp.id == s->cmp_id
20639 && s->cmp_to == glyph->slice.cmp.from)
20640 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20642 for (i = s->cmp_from; i < s->cmp_to; i++)
20644 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20645 unsigned code = LGLYPH_CODE (lglyph);
20647 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20649 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20650 return glyph - s->row->glyphs[s->area];
20654 /* Fill glyph string S from a sequence of character glyphs.
20656 FACE_ID is the face id of the string. START is the index of the
20657 first glyph to consider, END is the index of the last + 1.
20658 OVERLAPS non-zero means S should draw the foreground only, and use
20659 its physical height for clipping. See also draw_glyphs.
20661 Value is the index of the first glyph not in S. */
20663 static int
20664 fill_glyph_string (struct glyph_string *s, int face_id,
20665 int start, int end, int overlaps)
20667 struct glyph *glyph, *last;
20668 int voffset;
20669 int glyph_not_available_p;
20671 xassert (s->f == XFRAME (s->w->frame));
20672 xassert (s->nchars == 0);
20673 xassert (start >= 0 && end > start);
20675 s->for_overlaps = overlaps;
20676 glyph = s->row->glyphs[s->area] + start;
20677 last = s->row->glyphs[s->area] + end;
20678 voffset = glyph->voffset;
20679 s->padding_p = glyph->padding_p;
20680 glyph_not_available_p = glyph->glyph_not_available_p;
20682 while (glyph < last
20683 && glyph->type == CHAR_GLYPH
20684 && glyph->voffset == voffset
20685 /* Same face id implies same font, nowadays. */
20686 && glyph->face_id == face_id
20687 && glyph->glyph_not_available_p == glyph_not_available_p)
20689 int two_byte_p;
20691 s->face = get_glyph_face_and_encoding (s->f, glyph,
20692 s->char2b + s->nchars,
20693 &two_byte_p);
20694 s->two_byte_p = two_byte_p;
20695 ++s->nchars;
20696 xassert (s->nchars <= end - start);
20697 s->width += glyph->pixel_width;
20698 if (glyph++->padding_p != s->padding_p)
20699 break;
20702 s->font = s->face->font;
20704 /* If the specified font could not be loaded, use the frame's font,
20705 but record the fact that we couldn't load it in
20706 S->font_not_found_p so that we can draw rectangles for the
20707 characters of the glyph string. */
20708 if (s->font == NULL || glyph_not_available_p)
20710 s->font_not_found_p = 1;
20711 s->font = FRAME_FONT (s->f);
20714 /* Adjust base line for subscript/superscript text. */
20715 s->ybase += voffset;
20717 xassert (s->face && s->face->gc);
20718 return glyph - s->row->glyphs[s->area];
20722 /* Fill glyph string S from image glyph S->first_glyph. */
20724 static void
20725 fill_image_glyph_string (struct glyph_string *s)
20727 xassert (s->first_glyph->type == IMAGE_GLYPH);
20728 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20729 xassert (s->img);
20730 s->slice = s->first_glyph->slice.img;
20731 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20732 s->font = s->face->font;
20733 s->width = s->first_glyph->pixel_width;
20735 /* Adjust base line for subscript/superscript text. */
20736 s->ybase += s->first_glyph->voffset;
20740 /* Fill glyph string S from a sequence of stretch glyphs.
20742 ROW is the glyph row in which the glyphs are found, AREA is the
20743 area within the row. START is the index of the first glyph to
20744 consider, END is the index of the last + 1.
20746 Value is the index of the first glyph not in S. */
20748 static int
20749 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20750 enum glyph_row_area area, int start, int end)
20752 struct glyph *glyph, *last;
20753 int voffset, face_id;
20755 xassert (s->first_glyph->type == STRETCH_GLYPH);
20757 glyph = s->row->glyphs[s->area] + start;
20758 last = s->row->glyphs[s->area] + end;
20759 face_id = glyph->face_id;
20760 s->face = FACE_FROM_ID (s->f, face_id);
20761 s->font = s->face->font;
20762 s->width = glyph->pixel_width;
20763 s->nchars = 1;
20764 voffset = glyph->voffset;
20766 for (++glyph;
20767 (glyph < last
20768 && glyph->type == STRETCH_GLYPH
20769 && glyph->voffset == voffset
20770 && glyph->face_id == face_id);
20771 ++glyph)
20772 s->width += glyph->pixel_width;
20774 /* Adjust base line for subscript/superscript text. */
20775 s->ybase += voffset;
20777 /* The case that face->gc == 0 is handled when drawing the glyph
20778 string by calling PREPARE_FACE_FOR_DISPLAY. */
20779 xassert (s->face);
20780 return glyph - s->row->glyphs[s->area];
20783 static struct font_metrics *
20784 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20786 static struct font_metrics metrics;
20787 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20789 if (! font || code == FONT_INVALID_CODE)
20790 return NULL;
20791 font->driver->text_extents (font, &code, 1, &metrics);
20792 return &metrics;
20795 /* EXPORT for RIF:
20796 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20797 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20798 assumed to be zero. */
20800 void
20801 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20803 *left = *right = 0;
20805 if (glyph->type == CHAR_GLYPH)
20807 struct face *face;
20808 XChar2b char2b;
20809 struct font_metrics *pcm;
20811 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20812 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20814 if (pcm->rbearing > pcm->width)
20815 *right = pcm->rbearing - pcm->width;
20816 if (pcm->lbearing < 0)
20817 *left = -pcm->lbearing;
20820 else if (glyph->type == COMPOSITE_GLYPH)
20822 if (! glyph->u.cmp.automatic)
20824 struct composition *cmp = composition_table[glyph->u.cmp.id];
20826 if (cmp->rbearing > cmp->pixel_width)
20827 *right = cmp->rbearing - cmp->pixel_width;
20828 if (cmp->lbearing < 0)
20829 *left = - cmp->lbearing;
20831 else
20833 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20834 struct font_metrics metrics;
20836 composition_gstring_width (gstring, glyph->slice.cmp.from,
20837 glyph->slice.cmp.to + 1, &metrics);
20838 if (metrics.rbearing > metrics.width)
20839 *right = metrics.rbearing - metrics.width;
20840 if (metrics.lbearing < 0)
20841 *left = - metrics.lbearing;
20847 /* Return the index of the first glyph preceding glyph string S that
20848 is overwritten by S because of S's left overhang. Value is -1
20849 if no glyphs are overwritten. */
20851 static int
20852 left_overwritten (struct glyph_string *s)
20854 int k;
20856 if (s->left_overhang)
20858 int x = 0, i;
20859 struct glyph *glyphs = s->row->glyphs[s->area];
20860 int first = s->first_glyph - glyphs;
20862 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20863 x -= glyphs[i].pixel_width;
20865 k = i + 1;
20867 else
20868 k = -1;
20870 return k;
20874 /* Return the index of the first glyph preceding glyph string S that
20875 is overwriting S because of its right overhang. Value is -1 if no
20876 glyph in front of S overwrites S. */
20878 static int
20879 left_overwriting (struct glyph_string *s)
20881 int i, k, x;
20882 struct glyph *glyphs = s->row->glyphs[s->area];
20883 int first = s->first_glyph - glyphs;
20885 k = -1;
20886 x = 0;
20887 for (i = first - 1; i >= 0; --i)
20889 int left, right;
20890 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20891 if (x + right > 0)
20892 k = i;
20893 x -= glyphs[i].pixel_width;
20896 return k;
20900 /* Return the index of the last glyph following glyph string S that is
20901 overwritten by S because of S's right overhang. Value is -1 if
20902 no such glyph is found. */
20904 static int
20905 right_overwritten (struct glyph_string *s)
20907 int k = -1;
20909 if (s->right_overhang)
20911 int x = 0, i;
20912 struct glyph *glyphs = s->row->glyphs[s->area];
20913 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20914 int end = s->row->used[s->area];
20916 for (i = first; i < end && s->right_overhang > x; ++i)
20917 x += glyphs[i].pixel_width;
20919 k = i;
20922 return k;
20926 /* Return the index of the last glyph following glyph string S that
20927 overwrites S because of its left overhang. Value is negative
20928 if no such glyph is found. */
20930 static int
20931 right_overwriting (struct glyph_string *s)
20933 int i, k, x;
20934 int end = s->row->used[s->area];
20935 struct glyph *glyphs = s->row->glyphs[s->area];
20936 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20938 k = -1;
20939 x = 0;
20940 for (i = first; i < end; ++i)
20942 int left, right;
20943 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20944 if (x - left < 0)
20945 k = i;
20946 x += glyphs[i].pixel_width;
20949 return k;
20953 /* Set background width of glyph string S. START is the index of the
20954 first glyph following S. LAST_X is the right-most x-position + 1
20955 in the drawing area. */
20957 static INLINE void
20958 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
20960 /* If the face of this glyph string has to be drawn to the end of
20961 the drawing area, set S->extends_to_end_of_line_p. */
20963 if (start == s->row->used[s->area]
20964 && s->area == TEXT_AREA
20965 && ((s->row->fill_line_p
20966 && (s->hl == DRAW_NORMAL_TEXT
20967 || s->hl == DRAW_IMAGE_RAISED
20968 || s->hl == DRAW_IMAGE_SUNKEN))
20969 || s->hl == DRAW_MOUSE_FACE))
20970 s->extends_to_end_of_line_p = 1;
20972 /* If S extends its face to the end of the line, set its
20973 background_width to the distance to the right edge of the drawing
20974 area. */
20975 if (s->extends_to_end_of_line_p)
20976 s->background_width = last_x - s->x + 1;
20977 else
20978 s->background_width = s->width;
20982 /* Compute overhangs and x-positions for glyph string S and its
20983 predecessors, or successors. X is the starting x-position for S.
20984 BACKWARD_P non-zero means process predecessors. */
20986 static void
20987 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
20989 if (backward_p)
20991 while (s)
20993 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20994 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20995 x -= s->width;
20996 s->x = x;
20997 s = s->prev;
21000 else
21002 while (s)
21004 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21005 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21006 s->x = x;
21007 x += s->width;
21008 s = s->next;
21015 /* The following macros are only called from draw_glyphs below.
21016 They reference the following parameters of that function directly:
21017 `w', `row', `area', and `overlap_p'
21018 as well as the following local variables:
21019 `s', `f', and `hdc' (in W32) */
21021 #ifdef HAVE_NTGUI
21022 /* On W32, silently add local `hdc' variable to argument list of
21023 init_glyph_string. */
21024 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21025 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21026 #else
21027 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21028 init_glyph_string (s, char2b, w, row, area, start, hl)
21029 #endif
21031 /* Add a glyph string for a stretch glyph to the list of strings
21032 between HEAD and TAIL. START is the index of the stretch glyph in
21033 row area AREA of glyph row ROW. END is the index of the last glyph
21034 in that glyph row area. X is the current output position assigned
21035 to the new glyph string constructed. HL overrides that face of the
21036 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21037 is the right-most x-position of the drawing area. */
21039 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21040 and below -- keep them on one line. */
21041 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21042 do \
21044 s = (struct glyph_string *) alloca (sizeof *s); \
21045 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21046 START = fill_stretch_glyph_string (s, row, area, START, END); \
21047 append_glyph_string (&HEAD, &TAIL, s); \
21048 s->x = (X); \
21050 while (0)
21053 /* Add a glyph string for an image glyph to the list of strings
21054 between HEAD and TAIL. START is the index of the image glyph in
21055 row area AREA of glyph row ROW. END is the index of the last glyph
21056 in that glyph row area. X is the current output position assigned
21057 to the new glyph string constructed. HL overrides that face of the
21058 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21059 is the right-most x-position of the drawing area. */
21061 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21062 do \
21064 s = (struct glyph_string *) alloca (sizeof *s); \
21065 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21066 fill_image_glyph_string (s); \
21067 append_glyph_string (&HEAD, &TAIL, s); \
21068 ++START; \
21069 s->x = (X); \
21071 while (0)
21074 /* Add a glyph string for a sequence of character glyphs to the list
21075 of strings between HEAD and TAIL. START is the index of the first
21076 glyph in row area AREA of glyph row ROW that is part of the new
21077 glyph string. END is the index of the last glyph in that glyph row
21078 area. X is the current output position assigned to the new glyph
21079 string constructed. HL overrides that face of the glyph; e.g. it
21080 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21081 right-most x-position of the drawing area. */
21083 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21084 do \
21086 int face_id; \
21087 XChar2b *char2b; \
21089 face_id = (row)->glyphs[area][START].face_id; \
21091 s = (struct glyph_string *) alloca (sizeof *s); \
21092 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21093 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21094 append_glyph_string (&HEAD, &TAIL, s); \
21095 s->x = (X); \
21096 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21098 while (0)
21101 /* Add a glyph string for a composite sequence to the list of strings
21102 between HEAD and TAIL. START is the index of the first glyph in
21103 row area AREA of glyph row ROW that is part of the new glyph
21104 string. END is the index of the last glyph in that glyph row area.
21105 X is the current output position assigned to the new glyph string
21106 constructed. HL overrides that face of the glyph; e.g. it is
21107 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21108 x-position of the drawing area. */
21110 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21111 do { \
21112 int face_id = (row)->glyphs[area][START].face_id; \
21113 struct face *base_face = FACE_FROM_ID (f, face_id); \
21114 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21115 struct composition *cmp = composition_table[cmp_id]; \
21116 XChar2b *char2b; \
21117 struct glyph_string *first_s; \
21118 int n; \
21120 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21122 /* Make glyph_strings for each glyph sequence that is drawable by \
21123 the same face, and append them to HEAD/TAIL. */ \
21124 for (n = 0; n < cmp->glyph_len;) \
21126 s = (struct glyph_string *) alloca (sizeof *s); \
21127 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21128 append_glyph_string (&(HEAD), &(TAIL), s); \
21129 s->cmp = cmp; \
21130 s->cmp_from = n; \
21131 s->x = (X); \
21132 if (n == 0) \
21133 first_s = s; \
21134 n = fill_composite_glyph_string (s, base_face, overlaps); \
21137 ++START; \
21138 s = first_s; \
21139 } while (0)
21142 /* Add a glyph string for a glyph-string sequence to the list of strings
21143 between HEAD and TAIL. */
21145 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21146 do { \
21147 int face_id; \
21148 XChar2b *char2b; \
21149 Lisp_Object gstring; \
21151 face_id = (row)->glyphs[area][START].face_id; \
21152 gstring = (composition_gstring_from_id \
21153 ((row)->glyphs[area][START].u.cmp.id)); \
21154 s = (struct glyph_string *) alloca (sizeof *s); \
21155 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21156 * LGSTRING_GLYPH_LEN (gstring)); \
21157 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21158 append_glyph_string (&(HEAD), &(TAIL), s); \
21159 s->x = (X); \
21160 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21161 } while (0)
21164 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21165 of AREA of glyph row ROW on window W between indices START and END.
21166 HL overrides the face for drawing glyph strings, e.g. it is
21167 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21168 x-positions of the drawing area.
21170 This is an ugly monster macro construct because we must use alloca
21171 to allocate glyph strings (because draw_glyphs can be called
21172 asynchronously). */
21174 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21175 do \
21177 HEAD = TAIL = NULL; \
21178 while (START < END) \
21180 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21181 switch (first_glyph->type) \
21183 case CHAR_GLYPH: \
21184 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21185 HL, X, LAST_X); \
21186 break; \
21188 case COMPOSITE_GLYPH: \
21189 if (first_glyph->u.cmp.automatic) \
21190 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21191 HL, X, LAST_X); \
21192 else \
21193 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21194 HL, X, LAST_X); \
21195 break; \
21197 case STRETCH_GLYPH: \
21198 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21199 HL, X, LAST_X); \
21200 break; \
21202 case IMAGE_GLYPH: \
21203 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21204 HL, X, LAST_X); \
21205 break; \
21207 default: \
21208 abort (); \
21211 if (s) \
21213 set_glyph_string_background_width (s, START, LAST_X); \
21214 (X) += s->width; \
21217 } while (0)
21220 /* Draw glyphs between START and END in AREA of ROW on window W,
21221 starting at x-position X. X is relative to AREA in W. HL is a
21222 face-override with the following meaning:
21224 DRAW_NORMAL_TEXT draw normally
21225 DRAW_CURSOR draw in cursor face
21226 DRAW_MOUSE_FACE draw in mouse face.
21227 DRAW_INVERSE_VIDEO draw in mode line face
21228 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21229 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21231 If OVERLAPS is non-zero, draw only the foreground of characters and
21232 clip to the physical height of ROW. Non-zero value also defines
21233 the overlapping part to be drawn:
21235 OVERLAPS_PRED overlap with preceding rows
21236 OVERLAPS_SUCC overlap with succeeding rows
21237 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21238 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21240 Value is the x-position reached, relative to AREA of W. */
21242 static int
21243 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21244 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21245 enum draw_glyphs_face hl, int overlaps)
21247 struct glyph_string *head, *tail;
21248 struct glyph_string *s;
21249 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21250 int i, j, x_reached, last_x, area_left = 0;
21251 struct frame *f = XFRAME (WINDOW_FRAME (w));
21252 DECLARE_HDC (hdc);
21254 ALLOCATE_HDC (hdc, f);
21256 /* Let's rather be paranoid than getting a SEGV. */
21257 end = min (end, row->used[area]);
21258 start = max (0, start);
21259 start = min (end, start);
21261 /* Translate X to frame coordinates. Set last_x to the right
21262 end of the drawing area. */
21263 if (row->full_width_p)
21265 /* X is relative to the left edge of W, without scroll bars
21266 or fringes. */
21267 area_left = WINDOW_LEFT_EDGE_X (w);
21268 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21270 else
21272 area_left = window_box_left (w, area);
21273 last_x = area_left + window_box_width (w, area);
21275 x += area_left;
21277 /* Build a doubly-linked list of glyph_string structures between
21278 head and tail from what we have to draw. Note that the macro
21279 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21280 the reason we use a separate variable `i'. */
21281 i = start;
21282 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21283 if (tail)
21284 x_reached = tail->x + tail->background_width;
21285 else
21286 x_reached = x;
21288 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21289 the row, redraw some glyphs in front or following the glyph
21290 strings built above. */
21291 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21293 struct glyph_string *h, *t;
21294 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21295 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21296 int dummy_x = 0;
21298 /* If mouse highlighting is on, we may need to draw adjacent
21299 glyphs using mouse-face highlighting. */
21300 if (area == TEXT_AREA && row->mouse_face_p)
21302 struct glyph_row *mouse_beg_row, *mouse_end_row;
21304 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21305 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21307 if (row >= mouse_beg_row && row <= mouse_end_row)
21309 check_mouse_face = 1;
21310 mouse_beg_col = (row == mouse_beg_row)
21311 ? dpyinfo->mouse_face_beg_col : 0;
21312 mouse_end_col = (row == mouse_end_row)
21313 ? dpyinfo->mouse_face_end_col
21314 : row->used[TEXT_AREA];
21318 /* Compute overhangs for all glyph strings. */
21319 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21320 for (s = head; s; s = s->next)
21321 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21323 /* Prepend glyph strings for glyphs in front of the first glyph
21324 string that are overwritten because of the first glyph
21325 string's left overhang. The background of all strings
21326 prepended must be drawn because the first glyph string
21327 draws over it. */
21328 i = left_overwritten (head);
21329 if (i >= 0)
21331 enum draw_glyphs_face overlap_hl;
21333 /* If this row contains mouse highlighting, attempt to draw
21334 the overlapped glyphs with the correct highlight. This
21335 code fails if the overlap encompasses more than one glyph
21336 and mouse-highlight spans only some of these glyphs.
21337 However, making it work perfectly involves a lot more
21338 code, and I don't know if the pathological case occurs in
21339 practice, so we'll stick to this for now. --- cyd */
21340 if (check_mouse_face
21341 && mouse_beg_col < start && mouse_end_col > i)
21342 overlap_hl = DRAW_MOUSE_FACE;
21343 else
21344 overlap_hl = DRAW_NORMAL_TEXT;
21346 j = i;
21347 BUILD_GLYPH_STRINGS (j, start, h, t,
21348 overlap_hl, dummy_x, last_x);
21349 start = i;
21350 compute_overhangs_and_x (t, head->x, 1);
21351 prepend_glyph_string_lists (&head, &tail, h, t);
21352 clip_head = head;
21355 /* Prepend glyph strings for glyphs in front of the first glyph
21356 string that overwrite that glyph string because of their
21357 right overhang. For these strings, only the foreground must
21358 be drawn, because it draws over the glyph string at `head'.
21359 The background must not be drawn because this would overwrite
21360 right overhangs of preceding glyphs for which no glyph
21361 strings exist. */
21362 i = left_overwriting (head);
21363 if (i >= 0)
21365 enum draw_glyphs_face overlap_hl;
21367 if (check_mouse_face
21368 && mouse_beg_col < start && mouse_end_col > i)
21369 overlap_hl = DRAW_MOUSE_FACE;
21370 else
21371 overlap_hl = DRAW_NORMAL_TEXT;
21373 clip_head = head;
21374 BUILD_GLYPH_STRINGS (i, start, h, t,
21375 overlap_hl, dummy_x, last_x);
21376 for (s = h; s; s = s->next)
21377 s->background_filled_p = 1;
21378 compute_overhangs_and_x (t, head->x, 1);
21379 prepend_glyph_string_lists (&head, &tail, h, t);
21382 /* Append glyphs strings for glyphs following the last glyph
21383 string tail that are overwritten by tail. The background of
21384 these strings has to be drawn because tail's foreground draws
21385 over it. */
21386 i = right_overwritten (tail);
21387 if (i >= 0)
21389 enum draw_glyphs_face overlap_hl;
21391 if (check_mouse_face
21392 && mouse_beg_col < i && mouse_end_col > end)
21393 overlap_hl = DRAW_MOUSE_FACE;
21394 else
21395 overlap_hl = DRAW_NORMAL_TEXT;
21397 BUILD_GLYPH_STRINGS (end, i, h, t,
21398 overlap_hl, x, last_x);
21399 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21400 we don't have `end = i;' here. */
21401 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21402 append_glyph_string_lists (&head, &tail, h, t);
21403 clip_tail = tail;
21406 /* Append glyph strings for glyphs following the last glyph
21407 string tail that overwrite tail. The foreground of such
21408 glyphs has to be drawn because it writes into the background
21409 of tail. The background must not be drawn because it could
21410 paint over the foreground of following glyphs. */
21411 i = right_overwriting (tail);
21412 if (i >= 0)
21414 enum draw_glyphs_face overlap_hl;
21415 if (check_mouse_face
21416 && mouse_beg_col < i && mouse_end_col > end)
21417 overlap_hl = DRAW_MOUSE_FACE;
21418 else
21419 overlap_hl = DRAW_NORMAL_TEXT;
21421 clip_tail = tail;
21422 i++; /* We must include the Ith glyph. */
21423 BUILD_GLYPH_STRINGS (end, i, h, t,
21424 overlap_hl, x, last_x);
21425 for (s = h; s; s = s->next)
21426 s->background_filled_p = 1;
21427 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21428 append_glyph_string_lists (&head, &tail, h, t);
21430 if (clip_head || clip_tail)
21431 for (s = head; s; s = s->next)
21433 s->clip_head = clip_head;
21434 s->clip_tail = clip_tail;
21438 /* Draw all strings. */
21439 for (s = head; s; s = s->next)
21440 FRAME_RIF (f)->draw_glyph_string (s);
21442 #ifndef HAVE_NS
21443 /* When focus a sole frame and move horizontally, this sets on_p to 0
21444 causing a failure to erase prev cursor position. */
21445 if (area == TEXT_AREA
21446 && !row->full_width_p
21447 /* When drawing overlapping rows, only the glyph strings'
21448 foreground is drawn, which doesn't erase a cursor
21449 completely. */
21450 && !overlaps)
21452 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21453 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21454 : (tail ? tail->x + tail->background_width : x));
21455 x0 -= area_left;
21456 x1 -= area_left;
21458 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21459 row->y, MATRIX_ROW_BOTTOM_Y (row));
21461 #endif
21463 /* Value is the x-position up to which drawn, relative to AREA of W.
21464 This doesn't include parts drawn because of overhangs. */
21465 if (row->full_width_p)
21466 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21467 else
21468 x_reached -= area_left;
21470 RELEASE_HDC (hdc, f);
21472 return x_reached;
21475 /* Expand row matrix if too narrow. Don't expand if area
21476 is not present. */
21478 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21480 if (!fonts_changed_p \
21481 && (it->glyph_row->glyphs[area] \
21482 < it->glyph_row->glyphs[area + 1])) \
21484 it->w->ncols_scale_factor++; \
21485 fonts_changed_p = 1; \
21489 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21490 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21492 static INLINE void
21493 append_glyph (struct it *it)
21495 struct glyph *glyph;
21496 enum glyph_row_area area = it->area;
21498 xassert (it->glyph_row);
21499 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21501 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21502 if (glyph < it->glyph_row->glyphs[area + 1])
21504 /* If the glyph row is reversed, we need to prepend the glyph
21505 rather than append it. */
21506 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21508 struct glyph *g;
21510 /* Make room for the additional glyph. */
21511 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21512 g[1] = *g;
21513 glyph = it->glyph_row->glyphs[area];
21515 glyph->charpos = CHARPOS (it->position);
21516 glyph->object = it->object;
21517 if (it->pixel_width > 0)
21519 glyph->pixel_width = it->pixel_width;
21520 glyph->padding_p = 0;
21522 else
21524 /* Assure at least 1-pixel width. Otherwise, cursor can't
21525 be displayed correctly. */
21526 glyph->pixel_width = 1;
21527 glyph->padding_p = 1;
21529 glyph->ascent = it->ascent;
21530 glyph->descent = it->descent;
21531 glyph->voffset = it->voffset;
21532 glyph->type = CHAR_GLYPH;
21533 glyph->avoid_cursor_p = it->avoid_cursor_p;
21534 glyph->multibyte_p = it->multibyte_p;
21535 glyph->left_box_line_p = it->start_of_box_run_p;
21536 glyph->right_box_line_p = it->end_of_box_run_p;
21537 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21538 || it->phys_descent > it->descent);
21539 glyph->glyph_not_available_p = it->glyph_not_available_p;
21540 glyph->face_id = it->face_id;
21541 glyph->u.ch = it->char_to_display;
21542 glyph->slice.img = null_glyph_slice;
21543 glyph->font_type = FONT_TYPE_UNKNOWN;
21544 if (it->bidi_p)
21546 glyph->resolved_level = it->bidi_it.resolved_level;
21547 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21548 abort ();
21549 glyph->bidi_type = it->bidi_it.type;
21551 else
21553 glyph->resolved_level = 0;
21554 glyph->bidi_type = UNKNOWN_BT;
21556 ++it->glyph_row->used[area];
21558 else
21559 IT_EXPAND_MATRIX_WIDTH (it, area);
21562 /* Store one glyph for the composition IT->cmp_it.id in
21563 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21564 non-null. */
21566 static INLINE void
21567 append_composite_glyph (struct it *it)
21569 struct glyph *glyph;
21570 enum glyph_row_area area = it->area;
21572 xassert (it->glyph_row);
21574 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21575 if (glyph < it->glyph_row->glyphs[area + 1])
21577 /* If the glyph row is reversed, we need to prepend the glyph
21578 rather than append it. */
21579 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21581 struct glyph *g;
21583 /* Make room for the new glyph. */
21584 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21585 g[1] = *g;
21586 glyph = it->glyph_row->glyphs[it->area];
21588 glyph->charpos = it->cmp_it.charpos;
21589 glyph->object = it->object;
21590 glyph->pixel_width = it->pixel_width;
21591 glyph->ascent = it->ascent;
21592 glyph->descent = it->descent;
21593 glyph->voffset = it->voffset;
21594 glyph->type = COMPOSITE_GLYPH;
21595 if (it->cmp_it.ch < 0)
21597 glyph->u.cmp.automatic = 0;
21598 glyph->u.cmp.id = it->cmp_it.id;
21599 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21601 else
21603 glyph->u.cmp.automatic = 1;
21604 glyph->u.cmp.id = it->cmp_it.id;
21605 glyph->slice.cmp.from = it->cmp_it.from;
21606 glyph->slice.cmp.to = it->cmp_it.to - 1;
21608 glyph->avoid_cursor_p = it->avoid_cursor_p;
21609 glyph->multibyte_p = it->multibyte_p;
21610 glyph->left_box_line_p = it->start_of_box_run_p;
21611 glyph->right_box_line_p = it->end_of_box_run_p;
21612 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21613 || it->phys_descent > it->descent);
21614 glyph->padding_p = 0;
21615 glyph->glyph_not_available_p = 0;
21616 glyph->face_id = it->face_id;
21617 glyph->font_type = FONT_TYPE_UNKNOWN;
21618 if (it->bidi_p)
21620 glyph->resolved_level = it->bidi_it.resolved_level;
21621 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21622 abort ();
21623 glyph->bidi_type = it->bidi_it.type;
21625 ++it->glyph_row->used[area];
21627 else
21628 IT_EXPAND_MATRIX_WIDTH (it, area);
21632 /* Change IT->ascent and IT->height according to the setting of
21633 IT->voffset. */
21635 static INLINE void
21636 take_vertical_position_into_account (struct it *it)
21638 if (it->voffset)
21640 if (it->voffset < 0)
21641 /* Increase the ascent so that we can display the text higher
21642 in the line. */
21643 it->ascent -= it->voffset;
21644 else
21645 /* Increase the descent so that we can display the text lower
21646 in the line. */
21647 it->descent += it->voffset;
21652 /* Produce glyphs/get display metrics for the image IT is loaded with.
21653 See the description of struct display_iterator in dispextern.h for
21654 an overview of struct display_iterator. */
21656 static void
21657 produce_image_glyph (struct it *it)
21659 struct image *img;
21660 struct face *face;
21661 int glyph_ascent, crop;
21662 struct glyph_slice slice;
21664 xassert (it->what == IT_IMAGE);
21666 face = FACE_FROM_ID (it->f, it->face_id);
21667 xassert (face);
21668 /* Make sure X resources of the face is loaded. */
21669 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21671 if (it->image_id < 0)
21673 /* Fringe bitmap. */
21674 it->ascent = it->phys_ascent = 0;
21675 it->descent = it->phys_descent = 0;
21676 it->pixel_width = 0;
21677 it->nglyphs = 0;
21678 return;
21681 img = IMAGE_FROM_ID (it->f, it->image_id);
21682 xassert (img);
21683 /* Make sure X resources of the image is loaded. */
21684 prepare_image_for_display (it->f, img);
21686 slice.x = slice.y = 0;
21687 slice.width = img->width;
21688 slice.height = img->height;
21690 if (INTEGERP (it->slice.x))
21691 slice.x = XINT (it->slice.x);
21692 else if (FLOATP (it->slice.x))
21693 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21695 if (INTEGERP (it->slice.y))
21696 slice.y = XINT (it->slice.y);
21697 else if (FLOATP (it->slice.y))
21698 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21700 if (INTEGERP (it->slice.width))
21701 slice.width = XINT (it->slice.width);
21702 else if (FLOATP (it->slice.width))
21703 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21705 if (INTEGERP (it->slice.height))
21706 slice.height = XINT (it->slice.height);
21707 else if (FLOATP (it->slice.height))
21708 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21710 if (slice.x >= img->width)
21711 slice.x = img->width;
21712 if (slice.y >= img->height)
21713 slice.y = img->height;
21714 if (slice.x + slice.width >= img->width)
21715 slice.width = img->width - slice.x;
21716 if (slice.y + slice.height > img->height)
21717 slice.height = img->height - slice.y;
21719 if (slice.width == 0 || slice.height == 0)
21720 return;
21722 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21724 it->descent = slice.height - glyph_ascent;
21725 if (slice.y == 0)
21726 it->descent += img->vmargin;
21727 if (slice.y + slice.height == img->height)
21728 it->descent += img->vmargin;
21729 it->phys_descent = it->descent;
21731 it->pixel_width = slice.width;
21732 if (slice.x == 0)
21733 it->pixel_width += img->hmargin;
21734 if (slice.x + slice.width == img->width)
21735 it->pixel_width += img->hmargin;
21737 /* It's quite possible for images to have an ascent greater than
21738 their height, so don't get confused in that case. */
21739 if (it->descent < 0)
21740 it->descent = 0;
21742 it->nglyphs = 1;
21744 if (face->box != FACE_NO_BOX)
21746 if (face->box_line_width > 0)
21748 if (slice.y == 0)
21749 it->ascent += face->box_line_width;
21750 if (slice.y + slice.height == img->height)
21751 it->descent += face->box_line_width;
21754 if (it->start_of_box_run_p && slice.x == 0)
21755 it->pixel_width += eabs (face->box_line_width);
21756 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21757 it->pixel_width += eabs (face->box_line_width);
21760 take_vertical_position_into_account (it);
21762 /* Automatically crop wide image glyphs at right edge so we can
21763 draw the cursor on same display row. */
21764 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21765 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21767 it->pixel_width -= crop;
21768 slice.width -= crop;
21771 if (it->glyph_row)
21773 struct glyph *glyph;
21774 enum glyph_row_area area = it->area;
21776 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21777 if (glyph < it->glyph_row->glyphs[area + 1])
21779 glyph->charpos = CHARPOS (it->position);
21780 glyph->object = it->object;
21781 glyph->pixel_width = it->pixel_width;
21782 glyph->ascent = glyph_ascent;
21783 glyph->descent = it->descent;
21784 glyph->voffset = it->voffset;
21785 glyph->type = IMAGE_GLYPH;
21786 glyph->avoid_cursor_p = it->avoid_cursor_p;
21787 glyph->multibyte_p = it->multibyte_p;
21788 glyph->left_box_line_p = it->start_of_box_run_p;
21789 glyph->right_box_line_p = it->end_of_box_run_p;
21790 glyph->overlaps_vertically_p = 0;
21791 glyph->padding_p = 0;
21792 glyph->glyph_not_available_p = 0;
21793 glyph->face_id = it->face_id;
21794 glyph->u.img_id = img->id;
21795 glyph->slice.img = slice;
21796 glyph->font_type = FONT_TYPE_UNKNOWN;
21797 if (it->bidi_p)
21799 glyph->resolved_level = it->bidi_it.resolved_level;
21800 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21801 abort ();
21802 glyph->bidi_type = it->bidi_it.type;
21804 ++it->glyph_row->used[area];
21806 else
21807 IT_EXPAND_MATRIX_WIDTH (it, area);
21812 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21813 of the glyph, WIDTH and HEIGHT are the width and height of the
21814 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21816 static void
21817 append_stretch_glyph (struct it *it, Lisp_Object object,
21818 int width, int height, int ascent)
21820 struct glyph *glyph;
21821 enum glyph_row_area area = it->area;
21823 xassert (ascent >= 0 && ascent <= height);
21825 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21826 if (glyph < it->glyph_row->glyphs[area + 1])
21828 /* If the glyph row is reversed, we need to prepend the glyph
21829 rather than append it. */
21830 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21832 struct glyph *g;
21834 /* Make room for the additional glyph. */
21835 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21836 g[1] = *g;
21837 glyph = it->glyph_row->glyphs[area];
21839 glyph->charpos = CHARPOS (it->position);
21840 glyph->object = object;
21841 glyph->pixel_width = width;
21842 glyph->ascent = ascent;
21843 glyph->descent = height - ascent;
21844 glyph->voffset = it->voffset;
21845 glyph->type = STRETCH_GLYPH;
21846 glyph->avoid_cursor_p = it->avoid_cursor_p;
21847 glyph->multibyte_p = it->multibyte_p;
21848 glyph->left_box_line_p = it->start_of_box_run_p;
21849 glyph->right_box_line_p = it->end_of_box_run_p;
21850 glyph->overlaps_vertically_p = 0;
21851 glyph->padding_p = 0;
21852 glyph->glyph_not_available_p = 0;
21853 glyph->face_id = it->face_id;
21854 glyph->u.stretch.ascent = ascent;
21855 glyph->u.stretch.height = height;
21856 glyph->slice.img = null_glyph_slice;
21857 glyph->font_type = FONT_TYPE_UNKNOWN;
21858 if (it->bidi_p)
21860 glyph->resolved_level = it->bidi_it.resolved_level;
21861 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21862 abort ();
21863 glyph->bidi_type = it->bidi_it.type;
21865 else
21867 glyph->resolved_level = 0;
21868 glyph->bidi_type = UNKNOWN_BT;
21870 ++it->glyph_row->used[area];
21872 else
21873 IT_EXPAND_MATRIX_WIDTH (it, area);
21877 /* Produce a stretch glyph for iterator IT. IT->object is the value
21878 of the glyph property displayed. The value must be a list
21879 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21880 being recognized:
21882 1. `:width WIDTH' specifies that the space should be WIDTH *
21883 canonical char width wide. WIDTH may be an integer or floating
21884 point number.
21886 2. `:relative-width FACTOR' specifies that the width of the stretch
21887 should be computed from the width of the first character having the
21888 `glyph' property, and should be FACTOR times that width.
21890 3. `:align-to HPOS' specifies that the space should be wide enough
21891 to reach HPOS, a value in canonical character units.
21893 Exactly one of the above pairs must be present.
21895 4. `:height HEIGHT' specifies that the height of the stretch produced
21896 should be HEIGHT, measured in canonical character units.
21898 5. `:relative-height FACTOR' specifies that the height of the
21899 stretch should be FACTOR times the height of the characters having
21900 the glyph property.
21902 Either none or exactly one of 4 or 5 must be present.
21904 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21905 of the stretch should be used for the ascent of the stretch.
21906 ASCENT must be in the range 0 <= ASCENT <= 100. */
21908 static void
21909 produce_stretch_glyph (struct it *it)
21911 /* (space :width WIDTH :height HEIGHT ...) */
21912 Lisp_Object prop, plist;
21913 int width = 0, height = 0, align_to = -1;
21914 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21915 int ascent = 0;
21916 double tem;
21917 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21918 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21920 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21922 /* List should start with `space'. */
21923 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21924 plist = XCDR (it->object);
21926 /* Compute the width of the stretch. */
21927 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21928 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21930 /* Absolute width `:width WIDTH' specified and valid. */
21931 zero_width_ok_p = 1;
21932 width = (int)tem;
21934 else if (prop = Fplist_get (plist, QCrelative_width),
21935 NUMVAL (prop) > 0)
21937 /* Relative width `:relative-width FACTOR' specified and valid.
21938 Compute the width of the characters having the `glyph'
21939 property. */
21940 struct it it2;
21941 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21943 it2 = *it;
21944 if (it->multibyte_p)
21945 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
21946 else
21948 it2.c = it2.char_to_display = *p, it2.len = 1;
21949 if (! ASCII_CHAR_P (it2.c))
21950 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
21953 it2.glyph_row = NULL;
21954 it2.what = IT_CHARACTER;
21955 x_produce_glyphs (&it2);
21956 width = NUMVAL (prop) * it2.pixel_width;
21958 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21959 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21961 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21962 align_to = (align_to < 0
21964 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21965 else if (align_to < 0)
21966 align_to = window_box_left_offset (it->w, TEXT_AREA);
21967 width = max (0, (int)tem + align_to - it->current_x);
21968 zero_width_ok_p = 1;
21970 else
21971 /* Nothing specified -> width defaults to canonical char width. */
21972 width = FRAME_COLUMN_WIDTH (it->f);
21974 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21975 width = 1;
21977 /* Compute height. */
21978 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21979 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21981 height = (int)tem;
21982 zero_height_ok_p = 1;
21984 else if (prop = Fplist_get (plist, QCrelative_height),
21985 NUMVAL (prop) > 0)
21986 height = FONT_HEIGHT (font) * NUMVAL (prop);
21987 else
21988 height = FONT_HEIGHT (font);
21990 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21991 height = 1;
21993 /* Compute percentage of height used for ascent. If
21994 `:ascent ASCENT' is present and valid, use that. Otherwise,
21995 derive the ascent from the font in use. */
21996 if (prop = Fplist_get (plist, QCascent),
21997 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21998 ascent = height * NUMVAL (prop) / 100.0;
21999 else if (!NILP (prop)
22000 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22001 ascent = min (max (0, (int)tem), height);
22002 else
22003 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22005 if (width > 0 && it->line_wrap != TRUNCATE
22006 && it->current_x + width > it->last_visible_x)
22007 width = it->last_visible_x - it->current_x - 1;
22009 if (width > 0 && height > 0 && it->glyph_row)
22011 Lisp_Object object = it->stack[it->sp - 1].string;
22012 if (!STRINGP (object))
22013 object = it->w->buffer;
22014 append_stretch_glyph (it, object, width, height, ascent);
22017 it->pixel_width = width;
22018 it->ascent = it->phys_ascent = ascent;
22019 it->descent = it->phys_descent = height - it->ascent;
22020 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22022 take_vertical_position_into_account (it);
22025 /* Calculate line-height and line-spacing properties.
22026 An integer value specifies explicit pixel value.
22027 A float value specifies relative value to current face height.
22028 A cons (float . face-name) specifies relative value to
22029 height of specified face font.
22031 Returns height in pixels, or nil. */
22034 static Lisp_Object
22035 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22036 int boff, int override)
22038 Lisp_Object face_name = Qnil;
22039 int ascent, descent, height;
22041 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22042 return val;
22044 if (CONSP (val))
22046 face_name = XCAR (val);
22047 val = XCDR (val);
22048 if (!NUMBERP (val))
22049 val = make_number (1);
22050 if (NILP (face_name))
22052 height = it->ascent + it->descent;
22053 goto scale;
22057 if (NILP (face_name))
22059 font = FRAME_FONT (it->f);
22060 boff = FRAME_BASELINE_OFFSET (it->f);
22062 else if (EQ (face_name, Qt))
22064 override = 0;
22066 else
22068 int face_id;
22069 struct face *face;
22071 face_id = lookup_named_face (it->f, face_name, 0);
22072 if (face_id < 0)
22073 return make_number (-1);
22075 face = FACE_FROM_ID (it->f, face_id);
22076 font = face->font;
22077 if (font == NULL)
22078 return make_number (-1);
22079 boff = font->baseline_offset;
22080 if (font->vertical_centering)
22081 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22084 ascent = FONT_BASE (font) + boff;
22085 descent = FONT_DESCENT (font) - boff;
22087 if (override)
22089 it->override_ascent = ascent;
22090 it->override_descent = descent;
22091 it->override_boff = boff;
22094 height = ascent + descent;
22096 scale:
22097 if (FLOATP (val))
22098 height = (int)(XFLOAT_DATA (val) * height);
22099 else if (INTEGERP (val))
22100 height *= XINT (val);
22102 return make_number (height);
22106 /* RIF:
22107 Produce glyphs/get display metrics for the display element IT is
22108 loaded with. See the description of struct it in dispextern.h
22109 for an overview of struct it. */
22111 void
22112 x_produce_glyphs (struct it *it)
22114 int extra_line_spacing = it->extra_line_spacing;
22116 it->glyph_not_available_p = 0;
22118 if (it->what == IT_CHARACTER)
22120 XChar2b char2b;
22121 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22122 struct font *font = face->font;
22123 int font_not_found_p = font == NULL;
22124 struct font_metrics *pcm = NULL;
22125 int boff; /* baseline offset */
22127 if (font_not_found_p)
22129 /* When no suitable font found, display an empty box based
22130 on the metrics of the font of the default face (or what
22131 remapped). */
22132 struct face *no_font_face
22133 = FACE_FROM_ID (it->f,
22134 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
22135 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
22136 font = no_font_face->font;
22137 boff = font->baseline_offset;
22139 else
22141 boff = font->baseline_offset;
22142 if (font->vertical_centering)
22143 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22146 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22148 int stretched_p;
22150 it->nglyphs = 1;
22152 if (it->override_ascent >= 0)
22154 it->ascent = it->override_ascent;
22155 it->descent = it->override_descent;
22156 boff = it->override_boff;
22158 else
22160 it->ascent = FONT_BASE (font) + boff;
22161 it->descent = FONT_DESCENT (font) - boff;
22164 if (! font_not_found_p
22165 && get_char_glyph_code (it->char_to_display, font, &char2b))
22167 pcm = get_per_char_metric (it->f, font, &char2b);
22168 if (pcm->width == 0
22169 && pcm->rbearing == 0 && pcm->lbearing == 0)
22170 pcm = NULL;
22173 if (pcm)
22175 it->phys_ascent = pcm->ascent + boff;
22176 it->phys_descent = pcm->descent - boff;
22177 it->pixel_width = pcm->width;
22179 else
22181 it->glyph_not_available_p = 1;
22182 it->phys_ascent = it->ascent;
22183 it->phys_descent = it->descent;
22184 it->pixel_width = font->space_width;
22187 if (it->constrain_row_ascent_descent_p)
22189 if (it->descent > it->max_descent)
22191 it->ascent += it->descent - it->max_descent;
22192 it->descent = it->max_descent;
22194 if (it->ascent > it->max_ascent)
22196 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22197 it->ascent = it->max_ascent;
22199 it->phys_ascent = min (it->phys_ascent, it->ascent);
22200 it->phys_descent = min (it->phys_descent, it->descent);
22201 extra_line_spacing = 0;
22204 /* If this is a space inside a region of text with
22205 `space-width' property, change its width. */
22206 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22207 if (stretched_p)
22208 it->pixel_width *= XFLOATINT (it->space_width);
22210 /* If face has a box, add the box thickness to the character
22211 height. If character has a box line to the left and/or
22212 right, add the box line width to the character's width. */
22213 if (face->box != FACE_NO_BOX)
22215 int thick = face->box_line_width;
22217 if (thick > 0)
22219 it->ascent += thick;
22220 it->descent += thick;
22222 else
22223 thick = -thick;
22225 if (it->start_of_box_run_p)
22226 it->pixel_width += thick;
22227 if (it->end_of_box_run_p)
22228 it->pixel_width += thick;
22231 /* If face has an overline, add the height of the overline
22232 (1 pixel) and a 1 pixel margin to the character height. */
22233 if (face->overline_p)
22234 it->ascent += overline_margin;
22236 if (it->constrain_row_ascent_descent_p)
22238 if (it->ascent > it->max_ascent)
22239 it->ascent = it->max_ascent;
22240 if (it->descent > it->max_descent)
22241 it->descent = it->max_descent;
22244 take_vertical_position_into_account (it);
22246 /* If we have to actually produce glyphs, do it. */
22247 if (it->glyph_row)
22249 if (stretched_p)
22251 /* Translate a space with a `space-width' property
22252 into a stretch glyph. */
22253 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22254 / FONT_HEIGHT (font));
22255 append_stretch_glyph (it, it->object, it->pixel_width,
22256 it->ascent + it->descent, ascent);
22258 else
22259 append_glyph (it);
22261 /* If characters with lbearing or rbearing are displayed
22262 in this line, record that fact in a flag of the
22263 glyph row. This is used to optimize X output code. */
22264 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22265 it->glyph_row->contains_overlapping_glyphs_p = 1;
22267 if (! stretched_p && it->pixel_width == 0)
22268 /* We assure that all visible glyphs have at least 1-pixel
22269 width. */
22270 it->pixel_width = 1;
22272 else if (it->char_to_display == '\n')
22274 /* A newline has no width, but we need the height of the
22275 line. But if previous part of the line sets a height,
22276 don't increase that height */
22278 Lisp_Object height;
22279 Lisp_Object total_height = Qnil;
22281 it->override_ascent = -1;
22282 it->pixel_width = 0;
22283 it->nglyphs = 0;
22285 height = get_it_property (it, Qline_height);
22286 /* Split (line-height total-height) list */
22287 if (CONSP (height)
22288 && CONSP (XCDR (height))
22289 && NILP (XCDR (XCDR (height))))
22291 total_height = XCAR (XCDR (height));
22292 height = XCAR (height);
22294 height = calc_line_height_property (it, height, font, boff, 1);
22296 if (it->override_ascent >= 0)
22298 it->ascent = it->override_ascent;
22299 it->descent = it->override_descent;
22300 boff = it->override_boff;
22302 else
22304 it->ascent = FONT_BASE (font) + boff;
22305 it->descent = FONT_DESCENT (font) - boff;
22308 if (EQ (height, Qt))
22310 if (it->descent > it->max_descent)
22312 it->ascent += it->descent - it->max_descent;
22313 it->descent = it->max_descent;
22315 if (it->ascent > it->max_ascent)
22317 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22318 it->ascent = it->max_ascent;
22320 it->phys_ascent = min (it->phys_ascent, it->ascent);
22321 it->phys_descent = min (it->phys_descent, it->descent);
22322 it->constrain_row_ascent_descent_p = 1;
22323 extra_line_spacing = 0;
22325 else
22327 Lisp_Object spacing;
22329 it->phys_ascent = it->ascent;
22330 it->phys_descent = it->descent;
22332 if ((it->max_ascent > 0 || it->max_descent > 0)
22333 && face->box != FACE_NO_BOX
22334 && face->box_line_width > 0)
22336 it->ascent += face->box_line_width;
22337 it->descent += face->box_line_width;
22339 if (!NILP (height)
22340 && XINT (height) > it->ascent + it->descent)
22341 it->ascent = XINT (height) - it->descent;
22343 if (!NILP (total_height))
22344 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22345 else
22347 spacing = get_it_property (it, Qline_spacing);
22348 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22350 if (INTEGERP (spacing))
22352 extra_line_spacing = XINT (spacing);
22353 if (!NILP (total_height))
22354 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22358 else /* i.e. (it->char_to_display == '\t') */
22360 if (font->space_width > 0)
22362 int tab_width = it->tab_width * font->space_width;
22363 int x = it->current_x + it->continuation_lines_width;
22364 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22366 /* If the distance from the current position to the next tab
22367 stop is less than a space character width, use the
22368 tab stop after that. */
22369 if (next_tab_x - x < font->space_width)
22370 next_tab_x += tab_width;
22372 it->pixel_width = next_tab_x - x;
22373 it->nglyphs = 1;
22374 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22375 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22377 if (it->glyph_row)
22379 append_stretch_glyph (it, it->object, it->pixel_width,
22380 it->ascent + it->descent, it->ascent);
22383 else
22385 it->pixel_width = 0;
22386 it->nglyphs = 1;
22390 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22392 /* A static composition.
22394 Note: A composition is represented as one glyph in the
22395 glyph matrix. There are no padding glyphs.
22397 Important note: pixel_width, ascent, and descent are the
22398 values of what is drawn by draw_glyphs (i.e. the values of
22399 the overall glyphs composed). */
22400 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22401 int boff; /* baseline offset */
22402 struct composition *cmp = composition_table[it->cmp_it.id];
22403 int glyph_len = cmp->glyph_len;
22404 struct font *font = face->font;
22406 it->nglyphs = 1;
22408 /* If we have not yet calculated pixel size data of glyphs of
22409 the composition for the current face font, calculate them
22410 now. Theoretically, we have to check all fonts for the
22411 glyphs, but that requires much time and memory space. So,
22412 here we check only the font of the first glyph. This may
22413 lead to incorrect display, but it's very rare, and C-l
22414 (recenter-top-bottom) can correct the display anyway. */
22415 if (! cmp->font || cmp->font != font)
22417 /* Ascent and descent of the font of the first character
22418 of this composition (adjusted by baseline offset).
22419 Ascent and descent of overall glyphs should not be less
22420 than these, respectively. */
22421 int font_ascent, font_descent, font_height;
22422 /* Bounding box of the overall glyphs. */
22423 int leftmost, rightmost, lowest, highest;
22424 int lbearing, rbearing;
22425 int i, width, ascent, descent;
22426 int left_padded = 0, right_padded = 0;
22427 int c;
22428 XChar2b char2b;
22429 struct font_metrics *pcm;
22430 int font_not_found_p;
22431 EMACS_INT pos;
22433 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22434 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22435 break;
22436 if (glyph_len < cmp->glyph_len)
22437 right_padded = 1;
22438 for (i = 0; i < glyph_len; i++)
22440 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22441 break;
22442 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22444 if (i > 0)
22445 left_padded = 1;
22447 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22448 : IT_CHARPOS (*it));
22449 /* If no suitable font is found, use the default font. */
22450 font_not_found_p = font == NULL;
22451 if (font_not_found_p)
22453 face = face->ascii_face;
22454 font = face->font;
22456 boff = font->baseline_offset;
22457 if (font->vertical_centering)
22458 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22459 font_ascent = FONT_BASE (font) + boff;
22460 font_descent = FONT_DESCENT (font) - boff;
22461 font_height = FONT_HEIGHT (font);
22463 cmp->font = (void *) font;
22465 pcm = NULL;
22466 if (! font_not_found_p)
22468 get_char_face_and_encoding (it->f, c, it->face_id,
22469 &char2b, it->multibyte_p, 0);
22470 pcm = get_per_char_metric (it->f, font, &char2b);
22473 /* Initialize the bounding box. */
22474 if (pcm)
22476 width = pcm->width;
22477 ascent = pcm->ascent;
22478 descent = pcm->descent;
22479 lbearing = pcm->lbearing;
22480 rbearing = pcm->rbearing;
22482 else
22484 width = font->space_width;
22485 ascent = FONT_BASE (font);
22486 descent = FONT_DESCENT (font);
22487 lbearing = 0;
22488 rbearing = width;
22491 rightmost = width;
22492 leftmost = 0;
22493 lowest = - descent + boff;
22494 highest = ascent + boff;
22496 if (! font_not_found_p
22497 && font->default_ascent
22498 && CHAR_TABLE_P (Vuse_default_ascent)
22499 && !NILP (Faref (Vuse_default_ascent,
22500 make_number (it->char_to_display))))
22501 highest = font->default_ascent + boff;
22503 /* Draw the first glyph at the normal position. It may be
22504 shifted to right later if some other glyphs are drawn
22505 at the left. */
22506 cmp->offsets[i * 2] = 0;
22507 cmp->offsets[i * 2 + 1] = boff;
22508 cmp->lbearing = lbearing;
22509 cmp->rbearing = rbearing;
22511 /* Set cmp->offsets for the remaining glyphs. */
22512 for (i++; i < glyph_len; i++)
22514 int left, right, btm, top;
22515 int ch = COMPOSITION_GLYPH (cmp, i);
22516 int face_id;
22517 struct face *this_face;
22518 int this_boff;
22520 if (ch == '\t')
22521 ch = ' ';
22522 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22523 this_face = FACE_FROM_ID (it->f, face_id);
22524 font = this_face->font;
22526 if (font == NULL)
22527 pcm = NULL;
22528 else
22530 this_boff = font->baseline_offset;
22531 if (font->vertical_centering)
22532 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22533 get_char_face_and_encoding (it->f, ch, face_id,
22534 &char2b, it->multibyte_p, 0);
22535 pcm = get_per_char_metric (it->f, font, &char2b);
22537 if (! pcm)
22538 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22539 else
22541 width = pcm->width;
22542 ascent = pcm->ascent;
22543 descent = pcm->descent;
22544 lbearing = pcm->lbearing;
22545 rbearing = pcm->rbearing;
22546 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22548 /* Relative composition with or without
22549 alternate chars. */
22550 left = (leftmost + rightmost - width) / 2;
22551 btm = - descent + boff;
22552 if (font->relative_compose
22553 && (! CHAR_TABLE_P (Vignore_relative_composition)
22554 || NILP (Faref (Vignore_relative_composition,
22555 make_number (ch)))))
22558 if (- descent >= font->relative_compose)
22559 /* One extra pixel between two glyphs. */
22560 btm = highest + 1;
22561 else if (ascent <= 0)
22562 /* One extra pixel between two glyphs. */
22563 btm = lowest - 1 - ascent - descent;
22566 else
22568 /* A composition rule is specified by an integer
22569 value that encodes global and new reference
22570 points (GREF and NREF). GREF and NREF are
22571 specified by numbers as below:
22573 0---1---2 -- ascent
22577 9--10--11 -- center
22579 ---3---4---5--- baseline
22581 6---7---8 -- descent
22583 int rule = COMPOSITION_RULE (cmp, i);
22584 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22586 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22587 grefx = gref % 3, nrefx = nref % 3;
22588 grefy = gref / 3, nrefy = nref / 3;
22589 if (xoff)
22590 xoff = font_height * (xoff - 128) / 256;
22591 if (yoff)
22592 yoff = font_height * (yoff - 128) / 256;
22594 left = (leftmost
22595 + grefx * (rightmost - leftmost) / 2
22596 - nrefx * width / 2
22597 + xoff);
22599 btm = ((grefy == 0 ? highest
22600 : grefy == 1 ? 0
22601 : grefy == 2 ? lowest
22602 : (highest + lowest) / 2)
22603 - (nrefy == 0 ? ascent + descent
22604 : nrefy == 1 ? descent - boff
22605 : nrefy == 2 ? 0
22606 : (ascent + descent) / 2)
22607 + yoff);
22610 cmp->offsets[i * 2] = left;
22611 cmp->offsets[i * 2 + 1] = btm + descent;
22613 /* Update the bounding box of the overall glyphs. */
22614 if (width > 0)
22616 right = left + width;
22617 if (left < leftmost)
22618 leftmost = left;
22619 if (right > rightmost)
22620 rightmost = right;
22622 top = btm + descent + ascent;
22623 if (top > highest)
22624 highest = top;
22625 if (btm < lowest)
22626 lowest = btm;
22628 if (cmp->lbearing > left + lbearing)
22629 cmp->lbearing = left + lbearing;
22630 if (cmp->rbearing < left + rbearing)
22631 cmp->rbearing = left + rbearing;
22635 /* If there are glyphs whose x-offsets are negative,
22636 shift all glyphs to the right and make all x-offsets
22637 non-negative. */
22638 if (leftmost < 0)
22640 for (i = 0; i < cmp->glyph_len; i++)
22641 cmp->offsets[i * 2] -= leftmost;
22642 rightmost -= leftmost;
22643 cmp->lbearing -= leftmost;
22644 cmp->rbearing -= leftmost;
22647 if (left_padded && cmp->lbearing < 0)
22649 for (i = 0; i < cmp->glyph_len; i++)
22650 cmp->offsets[i * 2] -= cmp->lbearing;
22651 rightmost -= cmp->lbearing;
22652 cmp->rbearing -= cmp->lbearing;
22653 cmp->lbearing = 0;
22655 if (right_padded && rightmost < cmp->rbearing)
22657 rightmost = cmp->rbearing;
22660 cmp->pixel_width = rightmost;
22661 cmp->ascent = highest;
22662 cmp->descent = - lowest;
22663 if (cmp->ascent < font_ascent)
22664 cmp->ascent = font_ascent;
22665 if (cmp->descent < font_descent)
22666 cmp->descent = font_descent;
22669 if (it->glyph_row
22670 && (cmp->lbearing < 0
22671 || cmp->rbearing > cmp->pixel_width))
22672 it->glyph_row->contains_overlapping_glyphs_p = 1;
22674 it->pixel_width = cmp->pixel_width;
22675 it->ascent = it->phys_ascent = cmp->ascent;
22676 it->descent = it->phys_descent = cmp->descent;
22677 if (face->box != FACE_NO_BOX)
22679 int thick = face->box_line_width;
22681 if (thick > 0)
22683 it->ascent += thick;
22684 it->descent += thick;
22686 else
22687 thick = - thick;
22689 if (it->start_of_box_run_p)
22690 it->pixel_width += thick;
22691 if (it->end_of_box_run_p)
22692 it->pixel_width += thick;
22695 /* If face has an overline, add the height of the overline
22696 (1 pixel) and a 1 pixel margin to the character height. */
22697 if (face->overline_p)
22698 it->ascent += overline_margin;
22700 take_vertical_position_into_account (it);
22701 if (it->ascent < 0)
22702 it->ascent = 0;
22703 if (it->descent < 0)
22704 it->descent = 0;
22706 if (it->glyph_row)
22707 append_composite_glyph (it);
22709 else if (it->what == IT_COMPOSITION)
22711 /* A dynamic (automatic) composition. */
22712 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22713 Lisp_Object gstring;
22714 struct font_metrics metrics;
22716 gstring = composition_gstring_from_id (it->cmp_it.id);
22717 it->pixel_width
22718 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22719 &metrics);
22720 if (it->glyph_row
22721 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22722 it->glyph_row->contains_overlapping_glyphs_p = 1;
22723 it->ascent = it->phys_ascent = metrics.ascent;
22724 it->descent = it->phys_descent = metrics.descent;
22725 if (face->box != FACE_NO_BOX)
22727 int thick = face->box_line_width;
22729 if (thick > 0)
22731 it->ascent += thick;
22732 it->descent += thick;
22734 else
22735 thick = - thick;
22737 if (it->start_of_box_run_p)
22738 it->pixel_width += thick;
22739 if (it->end_of_box_run_p)
22740 it->pixel_width += thick;
22742 /* If face has an overline, add the height of the overline
22743 (1 pixel) and a 1 pixel margin to the character height. */
22744 if (face->overline_p)
22745 it->ascent += overline_margin;
22746 take_vertical_position_into_account (it);
22747 if (it->ascent < 0)
22748 it->ascent = 0;
22749 if (it->descent < 0)
22750 it->descent = 0;
22752 if (it->glyph_row)
22753 append_composite_glyph (it);
22755 else if (it->what == IT_IMAGE)
22756 produce_image_glyph (it);
22757 else if (it->what == IT_STRETCH)
22758 produce_stretch_glyph (it);
22760 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22761 because this isn't true for images with `:ascent 100'. */
22762 xassert (it->ascent >= 0 && it->descent >= 0);
22763 if (it->area == TEXT_AREA)
22764 it->current_x += it->pixel_width;
22766 if (extra_line_spacing > 0)
22768 it->descent += extra_line_spacing;
22769 if (extra_line_spacing > it->max_extra_line_spacing)
22770 it->max_extra_line_spacing = extra_line_spacing;
22773 it->max_ascent = max (it->max_ascent, it->ascent);
22774 it->max_descent = max (it->max_descent, it->descent);
22775 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22776 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22779 /* EXPORT for RIF:
22780 Output LEN glyphs starting at START at the nominal cursor position.
22781 Advance the nominal cursor over the text. The global variable
22782 updated_window contains the window being updated, updated_row is
22783 the glyph row being updated, and updated_area is the area of that
22784 row being updated. */
22786 void
22787 x_write_glyphs (struct glyph *start, int len)
22789 int x, hpos;
22791 xassert (updated_window && updated_row);
22792 BLOCK_INPUT;
22794 /* Write glyphs. */
22796 hpos = start - updated_row->glyphs[updated_area];
22797 x = draw_glyphs (updated_window, output_cursor.x,
22798 updated_row, updated_area,
22799 hpos, hpos + len,
22800 DRAW_NORMAL_TEXT, 0);
22802 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22803 if (updated_area == TEXT_AREA
22804 && updated_window->phys_cursor_on_p
22805 && updated_window->phys_cursor.vpos == output_cursor.vpos
22806 && updated_window->phys_cursor.hpos >= hpos
22807 && updated_window->phys_cursor.hpos < hpos + len)
22808 updated_window->phys_cursor_on_p = 0;
22810 UNBLOCK_INPUT;
22812 /* Advance the output cursor. */
22813 output_cursor.hpos += len;
22814 output_cursor.x = x;
22818 /* EXPORT for RIF:
22819 Insert LEN glyphs from START at the nominal cursor position. */
22821 void
22822 x_insert_glyphs (struct glyph *start, int len)
22824 struct frame *f;
22825 struct window *w;
22826 int line_height, shift_by_width, shifted_region_width;
22827 struct glyph_row *row;
22828 struct glyph *glyph;
22829 int frame_x, frame_y;
22830 EMACS_INT hpos;
22832 xassert (updated_window && updated_row);
22833 BLOCK_INPUT;
22834 w = updated_window;
22835 f = XFRAME (WINDOW_FRAME (w));
22837 /* Get the height of the line we are in. */
22838 row = updated_row;
22839 line_height = row->height;
22841 /* Get the width of the glyphs to insert. */
22842 shift_by_width = 0;
22843 for (glyph = start; glyph < start + len; ++glyph)
22844 shift_by_width += glyph->pixel_width;
22846 /* Get the width of the region to shift right. */
22847 shifted_region_width = (window_box_width (w, updated_area)
22848 - output_cursor.x
22849 - shift_by_width);
22851 /* Shift right. */
22852 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22853 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22855 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22856 line_height, shift_by_width);
22858 /* Write the glyphs. */
22859 hpos = start - row->glyphs[updated_area];
22860 draw_glyphs (w, output_cursor.x, row, updated_area,
22861 hpos, hpos + len,
22862 DRAW_NORMAL_TEXT, 0);
22864 /* Advance the output cursor. */
22865 output_cursor.hpos += len;
22866 output_cursor.x += shift_by_width;
22867 UNBLOCK_INPUT;
22871 /* EXPORT for RIF:
22872 Erase the current text line from the nominal cursor position
22873 (inclusive) to pixel column TO_X (exclusive). The idea is that
22874 everything from TO_X onward is already erased.
22876 TO_X is a pixel position relative to updated_area of
22877 updated_window. TO_X == -1 means clear to the end of this area. */
22879 void
22880 x_clear_end_of_line (int to_x)
22882 struct frame *f;
22883 struct window *w = updated_window;
22884 int max_x, min_y, max_y;
22885 int from_x, from_y, to_y;
22887 xassert (updated_window && updated_row);
22888 f = XFRAME (w->frame);
22890 if (updated_row->full_width_p)
22891 max_x = WINDOW_TOTAL_WIDTH (w);
22892 else
22893 max_x = window_box_width (w, updated_area);
22894 max_y = window_text_bottom_y (w);
22896 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22897 of window. For TO_X > 0, truncate to end of drawing area. */
22898 if (to_x == 0)
22899 return;
22900 else if (to_x < 0)
22901 to_x = max_x;
22902 else
22903 to_x = min (to_x, max_x);
22905 to_y = min (max_y, output_cursor.y + updated_row->height);
22907 /* Notice if the cursor will be cleared by this operation. */
22908 if (!updated_row->full_width_p)
22909 notice_overwritten_cursor (w, updated_area,
22910 output_cursor.x, -1,
22911 updated_row->y,
22912 MATRIX_ROW_BOTTOM_Y (updated_row));
22914 from_x = output_cursor.x;
22916 /* Translate to frame coordinates. */
22917 if (updated_row->full_width_p)
22919 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22920 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22922 else
22924 int area_left = window_box_left (w, updated_area);
22925 from_x += area_left;
22926 to_x += area_left;
22929 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22930 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22931 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22933 /* Prevent inadvertently clearing to end of the X window. */
22934 if (to_x > from_x && to_y > from_y)
22936 BLOCK_INPUT;
22937 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22938 to_x - from_x, to_y - from_y);
22939 UNBLOCK_INPUT;
22943 #endif /* HAVE_WINDOW_SYSTEM */
22947 /***********************************************************************
22948 Cursor types
22949 ***********************************************************************/
22951 /* Value is the internal representation of the specified cursor type
22952 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22953 of the bar cursor. */
22955 static enum text_cursor_kinds
22956 get_specified_cursor_type (Lisp_Object arg, int *width)
22958 enum text_cursor_kinds type;
22960 if (NILP (arg))
22961 return NO_CURSOR;
22963 if (EQ (arg, Qbox))
22964 return FILLED_BOX_CURSOR;
22966 if (EQ (arg, Qhollow))
22967 return HOLLOW_BOX_CURSOR;
22969 if (EQ (arg, Qbar))
22971 *width = 2;
22972 return BAR_CURSOR;
22975 if (CONSP (arg)
22976 && EQ (XCAR (arg), Qbar)
22977 && INTEGERP (XCDR (arg))
22978 && XINT (XCDR (arg)) >= 0)
22980 *width = XINT (XCDR (arg));
22981 return BAR_CURSOR;
22984 if (EQ (arg, Qhbar))
22986 *width = 2;
22987 return HBAR_CURSOR;
22990 if (CONSP (arg)
22991 && EQ (XCAR (arg), Qhbar)
22992 && INTEGERP (XCDR (arg))
22993 && XINT (XCDR (arg)) >= 0)
22995 *width = XINT (XCDR (arg));
22996 return HBAR_CURSOR;
22999 /* Treat anything unknown as "hollow box cursor".
23000 It was bad to signal an error; people have trouble fixing
23001 .Xdefaults with Emacs, when it has something bad in it. */
23002 type = HOLLOW_BOX_CURSOR;
23004 return type;
23007 /* Set the default cursor types for specified frame. */
23008 void
23009 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23011 int width;
23012 Lisp_Object tem;
23014 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23015 FRAME_CURSOR_WIDTH (f) = width;
23017 /* By default, set up the blink-off state depending on the on-state. */
23019 tem = Fassoc (arg, Vblink_cursor_alist);
23020 if (!NILP (tem))
23022 FRAME_BLINK_OFF_CURSOR (f)
23023 = get_specified_cursor_type (XCDR (tem), &width);
23024 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23026 else
23027 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23031 /* Return the cursor we want to be displayed in window W. Return
23032 width of bar/hbar cursor through WIDTH arg. Return with
23033 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23034 (i.e. if the `system caret' should track this cursor).
23036 In a mini-buffer window, we want the cursor only to appear if we
23037 are reading input from this window. For the selected window, we
23038 want the cursor type given by the frame parameter or buffer local
23039 setting of cursor-type. If explicitly marked off, draw no cursor.
23040 In all other cases, we want a hollow box cursor. */
23042 static enum text_cursor_kinds
23043 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23044 int *active_cursor)
23046 struct frame *f = XFRAME (w->frame);
23047 struct buffer *b = XBUFFER (w->buffer);
23048 int cursor_type = DEFAULT_CURSOR;
23049 Lisp_Object alt_cursor;
23050 int non_selected = 0;
23052 *active_cursor = 1;
23054 /* Echo area */
23055 if (cursor_in_echo_area
23056 && FRAME_HAS_MINIBUF_P (f)
23057 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23059 if (w == XWINDOW (echo_area_window))
23061 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23063 *width = FRAME_CURSOR_WIDTH (f);
23064 return FRAME_DESIRED_CURSOR (f);
23066 else
23067 return get_specified_cursor_type (b->cursor_type, width);
23070 *active_cursor = 0;
23071 non_selected = 1;
23074 /* Detect a nonselected window or nonselected frame. */
23075 else if (w != XWINDOW (f->selected_window)
23076 #ifdef HAVE_WINDOW_SYSTEM
23077 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
23078 #endif
23081 *active_cursor = 0;
23083 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23084 return NO_CURSOR;
23086 non_selected = 1;
23089 /* Never display a cursor in a window in which cursor-type is nil. */
23090 if (NILP (b->cursor_type))
23091 return NO_CURSOR;
23093 /* Get the normal cursor type for this window. */
23094 if (EQ (b->cursor_type, Qt))
23096 cursor_type = FRAME_DESIRED_CURSOR (f);
23097 *width = FRAME_CURSOR_WIDTH (f);
23099 else
23100 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23102 /* Use cursor-in-non-selected-windows instead
23103 for non-selected window or frame. */
23104 if (non_selected)
23106 alt_cursor = b->cursor_in_non_selected_windows;
23107 if (!EQ (Qt, alt_cursor))
23108 return get_specified_cursor_type (alt_cursor, width);
23109 /* t means modify the normal cursor type. */
23110 if (cursor_type == FILLED_BOX_CURSOR)
23111 cursor_type = HOLLOW_BOX_CURSOR;
23112 else if (cursor_type == BAR_CURSOR && *width > 1)
23113 --*width;
23114 return cursor_type;
23117 /* Use normal cursor if not blinked off. */
23118 if (!w->cursor_off_p)
23120 #ifdef HAVE_WINDOW_SYSTEM
23121 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23123 if (cursor_type == FILLED_BOX_CURSOR)
23125 /* Using a block cursor on large images can be very annoying.
23126 So use a hollow cursor for "large" images.
23127 If image is not transparent (no mask), also use hollow cursor. */
23128 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23129 if (img != NULL && IMAGEP (img->spec))
23131 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23132 where N = size of default frame font size.
23133 This should cover most of the "tiny" icons people may use. */
23134 if (!img->mask
23135 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23136 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23137 cursor_type = HOLLOW_BOX_CURSOR;
23140 else if (cursor_type != NO_CURSOR)
23142 /* Display current only supports BOX and HOLLOW cursors for images.
23143 So for now, unconditionally use a HOLLOW cursor when cursor is
23144 not a solid box cursor. */
23145 cursor_type = HOLLOW_BOX_CURSOR;
23148 #endif
23149 return cursor_type;
23152 /* Cursor is blinked off, so determine how to "toggle" it. */
23154 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23155 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23156 return get_specified_cursor_type (XCDR (alt_cursor), width);
23158 /* Then see if frame has specified a specific blink off cursor type. */
23159 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23161 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23162 return FRAME_BLINK_OFF_CURSOR (f);
23165 #if 0
23166 /* Some people liked having a permanently visible blinking cursor,
23167 while others had very strong opinions against it. So it was
23168 decided to remove it. KFS 2003-09-03 */
23170 /* Finally perform built-in cursor blinking:
23171 filled box <-> hollow box
23172 wide [h]bar <-> narrow [h]bar
23173 narrow [h]bar <-> no cursor
23174 other type <-> no cursor */
23176 if (cursor_type == FILLED_BOX_CURSOR)
23177 return HOLLOW_BOX_CURSOR;
23179 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23181 *width = 1;
23182 return cursor_type;
23184 #endif
23186 return NO_CURSOR;
23190 #ifdef HAVE_WINDOW_SYSTEM
23192 /* Notice when the text cursor of window W has been completely
23193 overwritten by a drawing operation that outputs glyphs in AREA
23194 starting at X0 and ending at X1 in the line starting at Y0 and
23195 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23196 the rest of the line after X0 has been written. Y coordinates
23197 are window-relative. */
23199 static void
23200 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23201 int x0, int x1, int y0, int y1)
23203 int cx0, cx1, cy0, cy1;
23204 struct glyph_row *row;
23206 if (!w->phys_cursor_on_p)
23207 return;
23208 if (area != TEXT_AREA)
23209 return;
23211 if (w->phys_cursor.vpos < 0
23212 || w->phys_cursor.vpos >= w->current_matrix->nrows
23213 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23214 !(row->enabled_p && row->displays_text_p)))
23215 return;
23217 if (row->cursor_in_fringe_p)
23219 row->cursor_in_fringe_p = 0;
23220 draw_fringe_bitmap (w, row, row->reversed_p);
23221 w->phys_cursor_on_p = 0;
23222 return;
23225 cx0 = w->phys_cursor.x;
23226 cx1 = cx0 + w->phys_cursor_width;
23227 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23228 return;
23230 /* The cursor image will be completely removed from the
23231 screen if the output area intersects the cursor area in
23232 y-direction. When we draw in [y0 y1[, and some part of
23233 the cursor is at y < y0, that part must have been drawn
23234 before. When scrolling, the cursor is erased before
23235 actually scrolling, so we don't come here. When not
23236 scrolling, the rows above the old cursor row must have
23237 changed, and in this case these rows must have written
23238 over the cursor image.
23240 Likewise if part of the cursor is below y1, with the
23241 exception of the cursor being in the first blank row at
23242 the buffer and window end because update_text_area
23243 doesn't draw that row. (Except when it does, but
23244 that's handled in update_text_area.) */
23246 cy0 = w->phys_cursor.y;
23247 cy1 = cy0 + w->phys_cursor_height;
23248 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23249 return;
23251 w->phys_cursor_on_p = 0;
23254 #endif /* HAVE_WINDOW_SYSTEM */
23257 /************************************************************************
23258 Mouse Face
23259 ************************************************************************/
23261 #ifdef HAVE_WINDOW_SYSTEM
23263 /* EXPORT for RIF:
23264 Fix the display of area AREA of overlapping row ROW in window W
23265 with respect to the overlapping part OVERLAPS. */
23267 void
23268 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23269 enum glyph_row_area area, int overlaps)
23271 int i, x;
23273 BLOCK_INPUT;
23275 x = 0;
23276 for (i = 0; i < row->used[area];)
23278 if (row->glyphs[area][i].overlaps_vertically_p)
23280 int start = i, start_x = x;
23284 x += row->glyphs[area][i].pixel_width;
23285 ++i;
23287 while (i < row->used[area]
23288 && row->glyphs[area][i].overlaps_vertically_p);
23290 draw_glyphs (w, start_x, row, area,
23291 start, i,
23292 DRAW_NORMAL_TEXT, overlaps);
23294 else
23296 x += row->glyphs[area][i].pixel_width;
23297 ++i;
23301 UNBLOCK_INPUT;
23305 /* EXPORT:
23306 Draw the cursor glyph of window W in glyph row ROW. See the
23307 comment of draw_glyphs for the meaning of HL. */
23309 void
23310 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23311 enum draw_glyphs_face hl)
23313 /* If cursor hpos is out of bounds, don't draw garbage. This can
23314 happen in mini-buffer windows when switching between echo area
23315 glyphs and mini-buffer. */
23316 if ((row->reversed_p
23317 ? (w->phys_cursor.hpos >= 0)
23318 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23320 int on_p = w->phys_cursor_on_p;
23321 int x1;
23322 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23323 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23324 hl, 0);
23325 w->phys_cursor_on_p = on_p;
23327 if (hl == DRAW_CURSOR)
23328 w->phys_cursor_width = x1 - w->phys_cursor.x;
23329 /* When we erase the cursor, and ROW is overlapped by other
23330 rows, make sure that these overlapping parts of other rows
23331 are redrawn. */
23332 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23334 w->phys_cursor_width = x1 - w->phys_cursor.x;
23336 if (row > w->current_matrix->rows
23337 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23338 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23339 OVERLAPS_ERASED_CURSOR);
23341 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23342 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23343 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23344 OVERLAPS_ERASED_CURSOR);
23350 /* EXPORT:
23351 Erase the image of a cursor of window W from the screen. */
23353 void
23354 erase_phys_cursor (struct window *w)
23356 struct frame *f = XFRAME (w->frame);
23357 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23358 int hpos = w->phys_cursor.hpos;
23359 int vpos = w->phys_cursor.vpos;
23360 int mouse_face_here_p = 0;
23361 struct glyph_matrix *active_glyphs = w->current_matrix;
23362 struct glyph_row *cursor_row;
23363 struct glyph *cursor_glyph;
23364 enum draw_glyphs_face hl;
23366 /* No cursor displayed or row invalidated => nothing to do on the
23367 screen. */
23368 if (w->phys_cursor_type == NO_CURSOR)
23369 goto mark_cursor_off;
23371 /* VPOS >= active_glyphs->nrows means that window has been resized.
23372 Don't bother to erase the cursor. */
23373 if (vpos >= active_glyphs->nrows)
23374 goto mark_cursor_off;
23376 /* If row containing cursor is marked invalid, there is nothing we
23377 can do. */
23378 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23379 if (!cursor_row->enabled_p)
23380 goto mark_cursor_off;
23382 /* If line spacing is > 0, old cursor may only be partially visible in
23383 window after split-window. So adjust visible height. */
23384 cursor_row->visible_height = min (cursor_row->visible_height,
23385 window_text_bottom_y (w) - cursor_row->y);
23387 /* If row is completely invisible, don't attempt to delete a cursor which
23388 isn't there. This can happen if cursor is at top of a window, and
23389 we switch to a buffer with a header line in that window. */
23390 if (cursor_row->visible_height <= 0)
23391 goto mark_cursor_off;
23393 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23394 if (cursor_row->cursor_in_fringe_p)
23396 cursor_row->cursor_in_fringe_p = 0;
23397 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23398 goto mark_cursor_off;
23401 /* This can happen when the new row is shorter than the old one.
23402 In this case, either draw_glyphs or clear_end_of_line
23403 should have cleared the cursor. Note that we wouldn't be
23404 able to erase the cursor in this case because we don't have a
23405 cursor glyph at hand. */
23406 if ((cursor_row->reversed_p
23407 ? (w->phys_cursor.hpos < 0)
23408 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23409 goto mark_cursor_off;
23411 /* If the cursor is in the mouse face area, redisplay that when
23412 we clear the cursor. */
23413 if (! NILP (dpyinfo->mouse_face_window)
23414 && w == XWINDOW (dpyinfo->mouse_face_window)
23415 && (vpos > dpyinfo->mouse_face_beg_row
23416 || (vpos == dpyinfo->mouse_face_beg_row
23417 && hpos >= dpyinfo->mouse_face_beg_col))
23418 && (vpos < dpyinfo->mouse_face_end_row
23419 || (vpos == dpyinfo->mouse_face_end_row
23420 && hpos < dpyinfo->mouse_face_end_col))
23421 /* Don't redraw the cursor's spot in mouse face if it is at the
23422 end of a line (on a newline). The cursor appears there, but
23423 mouse highlighting does not. */
23424 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23425 mouse_face_here_p = 1;
23427 /* Maybe clear the display under the cursor. */
23428 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23430 int x, y, left_x;
23431 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23432 int width;
23434 cursor_glyph = get_phys_cursor_glyph (w);
23435 if (cursor_glyph == NULL)
23436 goto mark_cursor_off;
23438 width = cursor_glyph->pixel_width;
23439 left_x = window_box_left_offset (w, TEXT_AREA);
23440 x = w->phys_cursor.x;
23441 if (x < left_x)
23442 width -= left_x - x;
23443 width = min (width, window_box_width (w, TEXT_AREA) - x);
23444 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23445 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23447 if (width > 0)
23448 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23451 /* Erase the cursor by redrawing the character underneath it. */
23452 if (mouse_face_here_p)
23453 hl = DRAW_MOUSE_FACE;
23454 else
23455 hl = DRAW_NORMAL_TEXT;
23456 draw_phys_cursor_glyph (w, cursor_row, hl);
23458 mark_cursor_off:
23459 w->phys_cursor_on_p = 0;
23460 w->phys_cursor_type = NO_CURSOR;
23464 /* EXPORT:
23465 Display or clear cursor of window W. If ON is zero, clear the
23466 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23467 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23469 void
23470 display_and_set_cursor (struct window *w, int on,
23471 int hpos, int vpos, int x, int y)
23473 struct frame *f = XFRAME (w->frame);
23474 int new_cursor_type;
23475 int new_cursor_width;
23476 int active_cursor;
23477 struct glyph_row *glyph_row;
23478 struct glyph *glyph;
23480 /* This is pointless on invisible frames, and dangerous on garbaged
23481 windows and frames; in the latter case, the frame or window may
23482 be in the midst of changing its size, and x and y may be off the
23483 window. */
23484 if (! FRAME_VISIBLE_P (f)
23485 || FRAME_GARBAGED_P (f)
23486 || vpos >= w->current_matrix->nrows
23487 || hpos >= w->current_matrix->matrix_w)
23488 return;
23490 /* If cursor is off and we want it off, return quickly. */
23491 if (!on && !w->phys_cursor_on_p)
23492 return;
23494 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23495 /* If cursor row is not enabled, we don't really know where to
23496 display the cursor. */
23497 if (!glyph_row->enabled_p)
23499 w->phys_cursor_on_p = 0;
23500 return;
23503 glyph = NULL;
23504 if (!glyph_row->exact_window_width_line_p
23505 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23506 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23508 xassert (interrupt_input_blocked);
23510 /* Set new_cursor_type to the cursor we want to be displayed. */
23511 new_cursor_type = get_window_cursor_type (w, glyph,
23512 &new_cursor_width, &active_cursor);
23514 /* If cursor is currently being shown and we don't want it to be or
23515 it is in the wrong place, or the cursor type is not what we want,
23516 erase it. */
23517 if (w->phys_cursor_on_p
23518 && (!on
23519 || w->phys_cursor.x != x
23520 || w->phys_cursor.y != y
23521 || new_cursor_type != w->phys_cursor_type
23522 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23523 && new_cursor_width != w->phys_cursor_width)))
23524 erase_phys_cursor (w);
23526 /* Don't check phys_cursor_on_p here because that flag is only set
23527 to zero in some cases where we know that the cursor has been
23528 completely erased, to avoid the extra work of erasing the cursor
23529 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23530 still not be visible, or it has only been partly erased. */
23531 if (on)
23533 w->phys_cursor_ascent = glyph_row->ascent;
23534 w->phys_cursor_height = glyph_row->height;
23536 /* Set phys_cursor_.* before x_draw_.* is called because some
23537 of them may need the information. */
23538 w->phys_cursor.x = x;
23539 w->phys_cursor.y = glyph_row->y;
23540 w->phys_cursor.hpos = hpos;
23541 w->phys_cursor.vpos = vpos;
23544 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23545 new_cursor_type, new_cursor_width,
23546 on, active_cursor);
23550 /* Switch the display of W's cursor on or off, according to the value
23551 of ON. */
23553 void
23554 update_window_cursor (struct window *w, int on)
23556 /* Don't update cursor in windows whose frame is in the process
23557 of being deleted. */
23558 if (w->current_matrix)
23560 BLOCK_INPUT;
23561 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23562 w->phys_cursor.x, w->phys_cursor.y);
23563 UNBLOCK_INPUT;
23568 /* Call update_window_cursor with parameter ON_P on all leaf windows
23569 in the window tree rooted at W. */
23571 static void
23572 update_cursor_in_window_tree (struct window *w, int on_p)
23574 while (w)
23576 if (!NILP (w->hchild))
23577 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23578 else if (!NILP (w->vchild))
23579 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23580 else
23581 update_window_cursor (w, on_p);
23583 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23588 /* EXPORT:
23589 Display the cursor on window W, or clear it, according to ON_P.
23590 Don't change the cursor's position. */
23592 void
23593 x_update_cursor (struct frame *f, int on_p)
23595 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23599 /* EXPORT:
23600 Clear the cursor of window W to background color, and mark the
23601 cursor as not shown. This is used when the text where the cursor
23602 is about to be rewritten. */
23604 void
23605 x_clear_cursor (struct window *w)
23607 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23608 update_window_cursor (w, 0);
23612 /* EXPORT:
23613 Display the active region described by mouse_face_* according to DRAW. */
23615 void
23616 show_mouse_face (Display_Info *dpyinfo, enum draw_glyphs_face draw)
23618 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23619 struct frame *f = XFRAME (WINDOW_FRAME (w));
23621 if (/* If window is in the process of being destroyed, don't bother
23622 to do anything. */
23623 w->current_matrix != NULL
23624 /* Don't update mouse highlight if hidden */
23625 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23626 /* Recognize when we are called to operate on rows that don't exist
23627 anymore. This can happen when a window is split. */
23628 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23630 int phys_cursor_on_p = w->phys_cursor_on_p;
23631 struct glyph_row *row, *first, *last;
23633 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23634 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23636 for (row = first; row <= last && row->enabled_p; ++row)
23638 int start_hpos, end_hpos, start_x;
23640 /* For all but the first row, the highlight starts at column 0. */
23641 if (row == first)
23643 start_hpos = dpyinfo->mouse_face_beg_col;
23644 start_x = dpyinfo->mouse_face_beg_x;
23646 else
23648 start_hpos = 0;
23649 start_x = 0;
23652 if (row == last)
23653 end_hpos = dpyinfo->mouse_face_end_col;
23654 else
23656 end_hpos = row->used[TEXT_AREA];
23657 if (draw == DRAW_NORMAL_TEXT)
23658 row->fill_line_p = 1; /* Clear to end of line */
23661 if (end_hpos > start_hpos)
23663 draw_glyphs (w, start_x, row, TEXT_AREA,
23664 start_hpos, end_hpos,
23665 draw, 0);
23667 row->mouse_face_p
23668 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23672 /* When we've written over the cursor, arrange for it to
23673 be displayed again. */
23674 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23676 BLOCK_INPUT;
23677 display_and_set_cursor (w, 1,
23678 w->phys_cursor.hpos, w->phys_cursor.vpos,
23679 w->phys_cursor.x, w->phys_cursor.y);
23680 UNBLOCK_INPUT;
23684 /* Change the mouse cursor. */
23685 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23686 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23687 else if (draw == DRAW_MOUSE_FACE)
23688 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23689 else
23690 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23693 /* EXPORT:
23694 Clear out the mouse-highlighted active region.
23695 Redraw it un-highlighted first. Value is non-zero if mouse
23696 face was actually drawn unhighlighted. */
23699 clear_mouse_face (Display_Info *dpyinfo)
23701 int cleared = 0;
23703 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23705 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23706 cleared = 1;
23709 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23710 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23711 dpyinfo->mouse_face_window = Qnil;
23712 dpyinfo->mouse_face_overlay = Qnil;
23713 return cleared;
23717 /* EXPORT:
23718 Non-zero if physical cursor of window W is within mouse face. */
23721 cursor_in_mouse_face_p (struct window *w)
23723 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23724 int in_mouse_face = 0;
23726 if (WINDOWP (dpyinfo->mouse_face_window)
23727 && XWINDOW (dpyinfo->mouse_face_window) == w)
23729 int hpos = w->phys_cursor.hpos;
23730 int vpos = w->phys_cursor.vpos;
23732 if (vpos >= dpyinfo->mouse_face_beg_row
23733 && vpos <= dpyinfo->mouse_face_end_row
23734 && (vpos > dpyinfo->mouse_face_beg_row
23735 || hpos >= dpyinfo->mouse_face_beg_col)
23736 && (vpos < dpyinfo->mouse_face_end_row
23737 || hpos < dpyinfo->mouse_face_end_col
23738 || dpyinfo->mouse_face_past_end))
23739 in_mouse_face = 1;
23742 return in_mouse_face;
23748 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23749 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23750 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23751 for the overlay or run of text properties specifying the mouse
23752 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23753 before-string and after-string that must also be highlighted.
23754 DISPLAY_STRING, if non-nil, is a display string that may cover some
23755 or all of the highlighted text. */
23757 static void
23758 mouse_face_from_buffer_pos (Lisp_Object window,
23759 Display_Info *dpyinfo,
23760 EMACS_INT mouse_charpos,
23761 EMACS_INT start_charpos,
23762 EMACS_INT end_charpos,
23763 Lisp_Object before_string,
23764 Lisp_Object after_string,
23765 Lisp_Object display_string)
23767 struct window *w = XWINDOW (window);
23768 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23769 struct glyph_row *row;
23770 struct glyph *glyph, *end;
23771 EMACS_INT ignore;
23772 int x;
23774 xassert (NILP (display_string) || STRINGP (display_string));
23775 xassert (NILP (before_string) || STRINGP (before_string));
23776 xassert (NILP (after_string) || STRINGP (after_string));
23778 /* Find the first highlighted glyph. */
23779 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23781 dpyinfo->mouse_face_beg_col = 0;
23782 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23783 dpyinfo->mouse_face_beg_x = first->x;
23784 dpyinfo->mouse_face_beg_y = first->y;
23786 else
23788 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23789 if (row == NULL)
23790 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23792 /* If the before-string or display-string contains newlines,
23793 row_containing_pos skips to its last row. Move back. */
23794 if (!NILP (before_string) || !NILP (display_string))
23796 struct glyph_row *prev;
23797 while ((prev = row - 1, prev >= first)
23798 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23799 && prev->used[TEXT_AREA] > 0)
23801 struct glyph *beg = prev->glyphs[TEXT_AREA];
23802 glyph = beg + prev->used[TEXT_AREA];
23803 while (--glyph >= beg && INTEGERP (glyph->object));
23804 if (glyph < beg
23805 || !(EQ (glyph->object, before_string)
23806 || EQ (glyph->object, display_string)))
23807 break;
23808 row = prev;
23812 glyph = row->glyphs[TEXT_AREA];
23813 end = glyph + row->used[TEXT_AREA];
23814 x = row->x;
23815 dpyinfo->mouse_face_beg_y = row->y;
23816 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23818 /* Skip truncation glyphs at the start of the glyph row. */
23819 if (row->displays_text_p)
23820 for (; glyph < end
23821 && INTEGERP (glyph->object)
23822 && glyph->charpos < 0;
23823 ++glyph)
23824 x += glyph->pixel_width;
23826 /* Scan the glyph row, stopping before BEFORE_STRING or
23827 DISPLAY_STRING or START_CHARPOS. */
23828 for (; glyph < end
23829 && !INTEGERP (glyph->object)
23830 && !EQ (glyph->object, before_string)
23831 && !EQ (glyph->object, display_string)
23832 && !(BUFFERP (glyph->object)
23833 && glyph->charpos >= start_charpos);
23834 ++glyph)
23835 x += glyph->pixel_width;
23837 dpyinfo->mouse_face_beg_x = x;
23838 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23841 /* Find the last highlighted glyph. */
23842 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23843 if (row == NULL)
23845 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23846 dpyinfo->mouse_face_past_end = 1;
23848 else if (!NILP (after_string))
23850 /* If the after-string has newlines, advance to its last row. */
23851 struct glyph_row *next;
23852 struct glyph_row *last
23853 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23855 for (next = row + 1;
23856 next <= last
23857 && next->used[TEXT_AREA] > 0
23858 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23859 ++next)
23860 row = next;
23863 glyph = row->glyphs[TEXT_AREA];
23864 end = glyph + row->used[TEXT_AREA];
23865 x = row->x;
23866 dpyinfo->mouse_face_end_y = row->y;
23867 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23869 /* Skip truncation glyphs at the start of the row. */
23870 if (row->displays_text_p)
23871 for (; glyph < end
23872 && INTEGERP (glyph->object)
23873 && glyph->charpos < 0;
23874 ++glyph)
23875 x += glyph->pixel_width;
23877 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23878 AFTER_STRING. */
23879 for (; glyph < end
23880 && !INTEGERP (glyph->object)
23881 && !EQ (glyph->object, after_string)
23882 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23883 ++glyph)
23884 x += glyph->pixel_width;
23886 /* If we found AFTER_STRING, consume it and stop. */
23887 if (EQ (glyph->object, after_string))
23889 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23890 x += glyph->pixel_width;
23892 else
23894 /* If there's no after-string, we must check if we overshot,
23895 which might be the case if we stopped after a string glyph.
23896 That glyph may belong to a before-string or display-string
23897 associated with the end position, which must not be
23898 highlighted. */
23899 Lisp_Object prev_object;
23900 EMACS_INT pos;
23902 while (glyph > row->glyphs[TEXT_AREA])
23904 prev_object = (glyph - 1)->object;
23905 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23906 break;
23908 pos = string_buffer_position (w, prev_object, end_charpos);
23909 if (pos && pos < end_charpos)
23910 break;
23912 for (; glyph > row->glyphs[TEXT_AREA]
23913 && EQ ((glyph - 1)->object, prev_object);
23914 --glyph)
23915 x -= (glyph - 1)->pixel_width;
23919 dpyinfo->mouse_face_end_x = x;
23920 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23921 dpyinfo->mouse_face_window = window;
23922 dpyinfo->mouse_face_face_id
23923 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23924 mouse_charpos + 1,
23925 !dpyinfo->mouse_face_hidden, -1);
23926 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23930 /* Find the position of the glyph for position POS in OBJECT in
23931 window W's current matrix, and return in *X, *Y the pixel
23932 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23934 RIGHT_P non-zero means return the position of the right edge of the
23935 glyph, RIGHT_P zero means return the left edge position.
23937 If no glyph for POS exists in the matrix, return the position of
23938 the glyph with the next smaller position that is in the matrix, if
23939 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23940 exists in the matrix, return the position of the glyph with the
23941 next larger position in OBJECT.
23943 Value is non-zero if a glyph was found. */
23945 static int
23946 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
23947 int *hpos, int *vpos, int *x, int *y, int right_p)
23949 int yb = window_text_bottom_y (w);
23950 struct glyph_row *r;
23951 struct glyph *best_glyph = NULL;
23952 struct glyph_row *best_row = NULL;
23953 int best_x = 0;
23955 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23956 r->enabled_p && r->y < yb;
23957 ++r)
23959 struct glyph *g = r->glyphs[TEXT_AREA];
23960 struct glyph *e = g + r->used[TEXT_AREA];
23961 int gx;
23963 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23964 if (EQ (g->object, object))
23966 if (g->charpos == pos)
23968 best_glyph = g;
23969 best_x = gx;
23970 best_row = r;
23971 goto found;
23973 else if (best_glyph == NULL
23974 || ((eabs (g->charpos - pos)
23975 < eabs (best_glyph->charpos - pos))
23976 && (right_p
23977 ? g->charpos < pos
23978 : g->charpos > pos)))
23980 best_glyph = g;
23981 best_x = gx;
23982 best_row = r;
23987 found:
23989 if (best_glyph)
23991 *x = best_x;
23992 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23994 if (right_p)
23996 *x += best_glyph->pixel_width;
23997 ++*hpos;
24000 *y = best_row->y;
24001 *vpos = best_row - w->current_matrix->rows;
24004 return best_glyph != NULL;
24008 /* See if position X, Y is within a hot-spot of an image. */
24010 static int
24011 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24013 if (!CONSP (hot_spot))
24014 return 0;
24016 if (EQ (XCAR (hot_spot), Qrect))
24018 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24019 Lisp_Object rect = XCDR (hot_spot);
24020 Lisp_Object tem;
24021 if (!CONSP (rect))
24022 return 0;
24023 if (!CONSP (XCAR (rect)))
24024 return 0;
24025 if (!CONSP (XCDR (rect)))
24026 return 0;
24027 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24028 return 0;
24029 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24030 return 0;
24031 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24032 return 0;
24033 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24034 return 0;
24035 return 1;
24037 else if (EQ (XCAR (hot_spot), Qcircle))
24039 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24040 Lisp_Object circ = XCDR (hot_spot);
24041 Lisp_Object lr, lx0, ly0;
24042 if (CONSP (circ)
24043 && CONSP (XCAR (circ))
24044 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24045 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24046 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24048 double r = XFLOATINT (lr);
24049 double dx = XINT (lx0) - x;
24050 double dy = XINT (ly0) - y;
24051 return (dx * dx + dy * dy <= r * r);
24054 else if (EQ (XCAR (hot_spot), Qpoly))
24056 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24057 if (VECTORP (XCDR (hot_spot)))
24059 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24060 Lisp_Object *poly = v->contents;
24061 int n = v->size;
24062 int i;
24063 int inside = 0;
24064 Lisp_Object lx, ly;
24065 int x0, y0;
24067 /* Need an even number of coordinates, and at least 3 edges. */
24068 if (n < 6 || n & 1)
24069 return 0;
24071 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24072 If count is odd, we are inside polygon. Pixels on edges
24073 may or may not be included depending on actual geometry of the
24074 polygon. */
24075 if ((lx = poly[n-2], !INTEGERP (lx))
24076 || (ly = poly[n-1], !INTEGERP (lx)))
24077 return 0;
24078 x0 = XINT (lx), y0 = XINT (ly);
24079 for (i = 0; i < n; i += 2)
24081 int x1 = x0, y1 = y0;
24082 if ((lx = poly[i], !INTEGERP (lx))
24083 || (ly = poly[i+1], !INTEGERP (ly)))
24084 return 0;
24085 x0 = XINT (lx), y0 = XINT (ly);
24087 /* Does this segment cross the X line? */
24088 if (x0 >= x)
24090 if (x1 >= x)
24091 continue;
24093 else if (x1 < x)
24094 continue;
24095 if (y > y0 && y > y1)
24096 continue;
24097 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24098 inside = !inside;
24100 return inside;
24103 return 0;
24106 Lisp_Object
24107 find_hot_spot (Lisp_Object map, int x, int y)
24109 while (CONSP (map))
24111 if (CONSP (XCAR (map))
24112 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24113 return XCAR (map);
24114 map = XCDR (map);
24117 return Qnil;
24120 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24121 3, 3, 0,
24122 doc: /* Lookup in image map MAP coordinates X and Y.
24123 An image map is an alist where each element has the format (AREA ID PLIST).
24124 An AREA is specified as either a rectangle, a circle, or a polygon:
24125 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24126 pixel coordinates of the upper left and bottom right corners.
24127 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24128 and the radius of the circle; r may be a float or integer.
24129 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24130 vector describes one corner in the polygon.
24131 Returns the alist element for the first matching AREA in MAP. */)
24132 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
24134 if (NILP (map))
24135 return Qnil;
24137 CHECK_NUMBER (x);
24138 CHECK_NUMBER (y);
24140 return find_hot_spot (map, XINT (x), XINT (y));
24144 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24145 static void
24146 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
24148 /* Do not change cursor shape while dragging mouse. */
24149 if (!NILP (do_mouse_tracking))
24150 return;
24152 if (!NILP (pointer))
24154 if (EQ (pointer, Qarrow))
24155 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24156 else if (EQ (pointer, Qhand))
24157 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24158 else if (EQ (pointer, Qtext))
24159 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24160 else if (EQ (pointer, intern ("hdrag")))
24161 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24162 #ifdef HAVE_X_WINDOWS
24163 else if (EQ (pointer, intern ("vdrag")))
24164 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24165 #endif
24166 else if (EQ (pointer, intern ("hourglass")))
24167 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24168 else if (EQ (pointer, Qmodeline))
24169 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24170 else
24171 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24174 if (cursor != No_Cursor)
24175 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24178 /* Take proper action when mouse has moved to the mode or header line
24179 or marginal area AREA of window W, x-position X and y-position Y.
24180 X is relative to the start of the text display area of W, so the
24181 width of bitmap areas and scroll bars must be subtracted to get a
24182 position relative to the start of the mode line. */
24184 static void
24185 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
24186 enum window_part area)
24188 struct window *w = XWINDOW (window);
24189 struct frame *f = XFRAME (w->frame);
24190 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24191 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24192 Lisp_Object pointer = Qnil;
24193 int dx, dy, width, height;
24194 EMACS_INT charpos;
24195 Lisp_Object string, object = Qnil;
24196 Lisp_Object pos, help;
24198 Lisp_Object mouse_face;
24199 int original_x_pixel = x;
24200 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24201 struct glyph_row *row;
24203 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24205 int x0;
24206 struct glyph *end;
24208 string = mode_line_string (w, area, &x, &y, &charpos,
24209 &object, &dx, &dy, &width, &height);
24211 row = (area == ON_MODE_LINE
24212 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24213 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24215 /* Find glyph */
24216 if (row->mode_line_p && row->enabled_p)
24218 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24219 end = glyph + row->used[TEXT_AREA];
24221 for (x0 = original_x_pixel;
24222 glyph < end && x0 >= glyph->pixel_width;
24223 ++glyph)
24224 x0 -= glyph->pixel_width;
24226 if (glyph >= end)
24227 glyph = NULL;
24230 else
24232 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24233 string = marginal_area_string (w, area, &x, &y, &charpos,
24234 &object, &dx, &dy, &width, &height);
24237 help = Qnil;
24239 if (IMAGEP (object))
24241 Lisp_Object image_map, hotspot;
24242 if ((image_map = Fplist_get (XCDR (object), QCmap),
24243 !NILP (image_map))
24244 && (hotspot = find_hot_spot (image_map, dx, dy),
24245 CONSP (hotspot))
24246 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24248 Lisp_Object area_id, plist;
24250 area_id = XCAR (hotspot);
24251 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24252 If so, we could look for mouse-enter, mouse-leave
24253 properties in PLIST (and do something...). */
24254 hotspot = XCDR (hotspot);
24255 if (CONSP (hotspot)
24256 && (plist = XCAR (hotspot), CONSP (plist)))
24258 pointer = Fplist_get (plist, Qpointer);
24259 if (NILP (pointer))
24260 pointer = Qhand;
24261 help = Fplist_get (plist, Qhelp_echo);
24262 if (!NILP (help))
24264 help_echo_string = help;
24265 /* Is this correct? ++kfs */
24266 XSETWINDOW (help_echo_window, w);
24267 help_echo_object = w->buffer;
24268 help_echo_pos = charpos;
24272 if (NILP (pointer))
24273 pointer = Fplist_get (XCDR (object), QCpointer);
24276 if (STRINGP (string))
24278 pos = make_number (charpos);
24279 /* If we're on a string with `help-echo' text property, arrange
24280 for the help to be displayed. This is done by setting the
24281 global variable help_echo_string to the help string. */
24282 if (NILP (help))
24284 help = Fget_text_property (pos, Qhelp_echo, string);
24285 if (!NILP (help))
24287 help_echo_string = help;
24288 XSETWINDOW (help_echo_window, w);
24289 help_echo_object = string;
24290 help_echo_pos = charpos;
24294 if (NILP (pointer))
24295 pointer = Fget_text_property (pos, Qpointer, string);
24297 /* Change the mouse pointer according to what is under X/Y. */
24298 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24300 Lisp_Object map;
24301 map = Fget_text_property (pos, Qlocal_map, string);
24302 if (!KEYMAPP (map))
24303 map = Fget_text_property (pos, Qkeymap, string);
24304 if (!KEYMAPP (map))
24305 cursor = dpyinfo->vertical_scroll_bar_cursor;
24308 /* Change the mouse face according to what is under X/Y. */
24309 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24310 if (!NILP (mouse_face)
24311 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24312 && glyph)
24314 Lisp_Object b, e;
24316 struct glyph * tmp_glyph;
24318 int gpos;
24319 int gseq_length;
24320 int total_pixel_width;
24321 EMACS_INT ignore;
24323 int vpos, hpos;
24325 b = Fprevious_single_property_change (make_number (charpos + 1),
24326 Qmouse_face, string, Qnil);
24327 if (NILP (b))
24328 b = make_number (0);
24330 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24331 if (NILP (e))
24332 e = make_number (SCHARS (string));
24334 /* Calculate the position(glyph position: GPOS) of GLYPH in
24335 displayed string. GPOS is different from CHARPOS.
24337 CHARPOS is the position of glyph in internal string
24338 object. A mode line string format has structures which
24339 is converted to a flatten by emacs lisp interpreter.
24340 The internal string is an element of the structures.
24341 The displayed string is the flatten string. */
24342 gpos = 0;
24343 if (glyph > row_start_glyph)
24345 tmp_glyph = glyph - 1;
24346 while (tmp_glyph >= row_start_glyph
24347 && tmp_glyph->charpos >= XINT (b)
24348 && EQ (tmp_glyph->object, glyph->object))
24350 tmp_glyph--;
24351 gpos++;
24355 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24356 displayed string holding GLYPH.
24358 GSEQ_LENGTH is different from SCHARS (STRING).
24359 SCHARS (STRING) returns the length of the internal string. */
24360 for (tmp_glyph = glyph, gseq_length = gpos;
24361 tmp_glyph->charpos < XINT (e);
24362 tmp_glyph++, gseq_length++)
24364 if (!EQ (tmp_glyph->object, glyph->object))
24365 break;
24368 total_pixel_width = 0;
24369 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24370 total_pixel_width += tmp_glyph->pixel_width;
24372 /* Pre calculation of re-rendering position */
24373 vpos = (x - gpos);
24374 hpos = (area == ON_MODE_LINE
24375 ? (w->current_matrix)->nrows - 1
24376 : 0);
24378 /* If the re-rendering position is included in the last
24379 re-rendering area, we should do nothing. */
24380 if ( EQ (window, dpyinfo->mouse_face_window)
24381 && dpyinfo->mouse_face_beg_col <= vpos
24382 && vpos < dpyinfo->mouse_face_end_col
24383 && dpyinfo->mouse_face_beg_row == hpos )
24384 return;
24386 if (clear_mouse_face (dpyinfo))
24387 cursor = No_Cursor;
24389 dpyinfo->mouse_face_beg_col = vpos;
24390 dpyinfo->mouse_face_beg_row = hpos;
24392 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24393 dpyinfo->mouse_face_beg_y = 0;
24395 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24396 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24398 dpyinfo->mouse_face_end_x = 0;
24399 dpyinfo->mouse_face_end_y = 0;
24401 dpyinfo->mouse_face_past_end = 0;
24402 dpyinfo->mouse_face_window = window;
24404 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24405 charpos,
24406 0, 0, 0, &ignore,
24407 glyph->face_id, 1);
24408 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24410 if (NILP (pointer))
24411 pointer = Qhand;
24413 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24414 clear_mouse_face (dpyinfo);
24416 define_frame_cursor1 (f, cursor, pointer);
24420 /* EXPORT:
24421 Take proper action when the mouse has moved to position X, Y on
24422 frame F as regards highlighting characters that have mouse-face
24423 properties. Also de-highlighting chars where the mouse was before.
24424 X and Y can be negative or out of range. */
24426 void
24427 note_mouse_highlight (struct frame *f, int x, int y)
24429 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24430 enum window_part part;
24431 Lisp_Object window;
24432 struct window *w;
24433 Cursor cursor = No_Cursor;
24434 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24435 struct buffer *b;
24437 /* When a menu is active, don't highlight because this looks odd. */
24438 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24439 if (popup_activated ())
24440 return;
24441 #endif
24443 if (NILP (Vmouse_highlight)
24444 || !f->glyphs_initialized_p
24445 || f->pointer_invisible)
24446 return;
24448 dpyinfo->mouse_face_mouse_x = x;
24449 dpyinfo->mouse_face_mouse_y = y;
24450 dpyinfo->mouse_face_mouse_frame = f;
24452 if (dpyinfo->mouse_face_defer)
24453 return;
24455 if (gc_in_progress)
24457 dpyinfo->mouse_face_deferred_gc = 1;
24458 return;
24461 /* Which window is that in? */
24462 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24464 /* If we were displaying active text in another window, clear that.
24465 Also clear if we move out of text area in same window. */
24466 if (! EQ (window, dpyinfo->mouse_face_window)
24467 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24468 && !NILP (dpyinfo->mouse_face_window)))
24469 clear_mouse_face (dpyinfo);
24471 /* Not on a window -> return. */
24472 if (!WINDOWP (window))
24473 return;
24475 /* Reset help_echo_string. It will get recomputed below. */
24476 help_echo_string = Qnil;
24478 /* Convert to window-relative pixel coordinates. */
24479 w = XWINDOW (window);
24480 frame_to_window_pixel_xy (w, &x, &y);
24482 /* Handle tool-bar window differently since it doesn't display a
24483 buffer. */
24484 if (EQ (window, f->tool_bar_window))
24486 note_tool_bar_highlight (f, x, y);
24487 return;
24490 /* Mouse is on the mode, header line or margin? */
24491 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24492 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24494 note_mode_line_or_margin_highlight (window, x, y, part);
24495 return;
24498 if (part == ON_VERTICAL_BORDER)
24500 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24501 help_echo_string = build_string ("drag-mouse-1: resize");
24503 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24504 || part == ON_SCROLL_BAR)
24505 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24506 else
24507 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24509 /* Are we in a window whose display is up to date?
24510 And verify the buffer's text has not changed. */
24511 b = XBUFFER (w->buffer);
24512 if (part == ON_TEXT
24513 && EQ (w->window_end_valid, w->buffer)
24514 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24515 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24517 int hpos, vpos, i, dx, dy, area;
24518 EMACS_INT pos;
24519 struct glyph *glyph;
24520 Lisp_Object object;
24521 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24522 Lisp_Object *overlay_vec = NULL;
24523 int noverlays;
24524 struct buffer *obuf;
24525 EMACS_INT obegv, ozv;
24526 int same_region;
24528 /* Find the glyph under X/Y. */
24529 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24531 /* Look for :pointer property on image. */
24532 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24534 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24535 if (img != NULL && IMAGEP (img->spec))
24537 Lisp_Object image_map, hotspot;
24538 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24539 !NILP (image_map))
24540 && (hotspot = find_hot_spot (image_map,
24541 glyph->slice.img.x + dx,
24542 glyph->slice.img.y + dy),
24543 CONSP (hotspot))
24544 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24546 Lisp_Object area_id, plist;
24548 area_id = XCAR (hotspot);
24549 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24550 If so, we could look for mouse-enter, mouse-leave
24551 properties in PLIST (and do something...). */
24552 hotspot = XCDR (hotspot);
24553 if (CONSP (hotspot)
24554 && (plist = XCAR (hotspot), CONSP (plist)))
24556 pointer = Fplist_get (plist, Qpointer);
24557 if (NILP (pointer))
24558 pointer = Qhand;
24559 help_echo_string = Fplist_get (plist, Qhelp_echo);
24560 if (!NILP (help_echo_string))
24562 help_echo_window = window;
24563 help_echo_object = glyph->object;
24564 help_echo_pos = glyph->charpos;
24568 if (NILP (pointer))
24569 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24573 /* Clear mouse face if X/Y not over text. */
24574 if (glyph == NULL
24575 || area != TEXT_AREA
24576 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24578 if (clear_mouse_face (dpyinfo))
24579 cursor = No_Cursor;
24580 if (NILP (pointer))
24582 if (area != TEXT_AREA)
24583 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24584 else
24585 pointer = Vvoid_text_area_pointer;
24587 goto set_cursor;
24590 pos = glyph->charpos;
24591 object = glyph->object;
24592 if (!STRINGP (object) && !BUFFERP (object))
24593 goto set_cursor;
24595 /* If we get an out-of-range value, return now; avoid an error. */
24596 if (BUFFERP (object) && pos > BUF_Z (b))
24597 goto set_cursor;
24599 /* Make the window's buffer temporarily current for
24600 overlays_at and compute_char_face. */
24601 obuf = current_buffer;
24602 current_buffer = b;
24603 obegv = BEGV;
24604 ozv = ZV;
24605 BEGV = BEG;
24606 ZV = Z;
24608 /* Is this char mouse-active or does it have help-echo? */
24609 position = make_number (pos);
24611 if (BUFFERP (object))
24613 /* Put all the overlays we want in a vector in overlay_vec. */
24614 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24615 /* Sort overlays into increasing priority order. */
24616 noverlays = sort_overlays (overlay_vec, noverlays, w);
24618 else
24619 noverlays = 0;
24621 same_region = (EQ (window, dpyinfo->mouse_face_window)
24622 && vpos >= dpyinfo->mouse_face_beg_row
24623 && vpos <= dpyinfo->mouse_face_end_row
24624 && (vpos > dpyinfo->mouse_face_beg_row
24625 || hpos >= dpyinfo->mouse_face_beg_col)
24626 && (vpos < dpyinfo->mouse_face_end_row
24627 || hpos < dpyinfo->mouse_face_end_col
24628 || dpyinfo->mouse_face_past_end));
24630 if (same_region)
24631 cursor = No_Cursor;
24633 /* Check mouse-face highlighting. */
24634 if (! same_region
24635 /* If there exists an overlay with mouse-face overlapping
24636 the one we are currently highlighting, we have to
24637 check if we enter the overlapping overlay, and then
24638 highlight only that. */
24639 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24640 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24642 /* Find the highest priority overlay with a mouse-face. */
24643 overlay = Qnil;
24644 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24646 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24647 if (!NILP (mouse_face))
24648 overlay = overlay_vec[i];
24651 /* If we're highlighting the same overlay as before, there's
24652 no need to do that again. */
24653 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24654 goto check_help_echo;
24655 dpyinfo->mouse_face_overlay = overlay;
24657 /* Clear the display of the old active region, if any. */
24658 if (clear_mouse_face (dpyinfo))
24659 cursor = No_Cursor;
24661 /* If no overlay applies, get a text property. */
24662 if (NILP (overlay))
24663 mouse_face = Fget_text_property (position, Qmouse_face, object);
24665 /* Next, compute the bounds of the mouse highlighting and
24666 display it. */
24667 if (!NILP (mouse_face) && STRINGP (object))
24669 /* The mouse-highlighting comes from a display string
24670 with a mouse-face. */
24671 Lisp_Object b, e;
24672 EMACS_INT ignore;
24674 b = Fprevious_single_property_change
24675 (make_number (pos + 1), Qmouse_face, object, Qnil);
24676 e = Fnext_single_property_change
24677 (position, Qmouse_face, object, Qnil);
24678 if (NILP (b))
24679 b = make_number (0);
24680 if (NILP (e))
24681 e = make_number (SCHARS (object) - 1);
24683 fast_find_string_pos (w, XINT (b), object,
24684 &dpyinfo->mouse_face_beg_col,
24685 &dpyinfo->mouse_face_beg_row,
24686 &dpyinfo->mouse_face_beg_x,
24687 &dpyinfo->mouse_face_beg_y, 0);
24688 fast_find_string_pos (w, XINT (e), object,
24689 &dpyinfo->mouse_face_end_col,
24690 &dpyinfo->mouse_face_end_row,
24691 &dpyinfo->mouse_face_end_x,
24692 &dpyinfo->mouse_face_end_y, 1);
24693 dpyinfo->mouse_face_past_end = 0;
24694 dpyinfo->mouse_face_window = window;
24695 dpyinfo->mouse_face_face_id
24696 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24697 glyph->face_id, 1);
24698 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24699 cursor = No_Cursor;
24701 else
24703 /* The mouse-highlighting, if any, comes from an overlay
24704 or text property in the buffer. */
24705 Lisp_Object buffer, display_string;
24707 if (STRINGP (object))
24709 /* If we are on a display string with no mouse-face,
24710 check if the text under it has one. */
24711 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24712 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
24713 pos = string_buffer_position (w, object, start);
24714 if (pos > 0)
24716 mouse_face = get_char_property_and_overlay
24717 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24718 buffer = w->buffer;
24719 display_string = object;
24722 else
24724 buffer = object;
24725 display_string = Qnil;
24728 if (!NILP (mouse_face))
24730 Lisp_Object before, after;
24731 Lisp_Object before_string, after_string;
24733 if (NILP (overlay))
24735 /* Handle the text property case. */
24736 before = Fprevious_single_property_change
24737 (make_number (pos + 1), Qmouse_face, buffer,
24738 Fmarker_position (w->start));
24739 after = Fnext_single_property_change
24740 (make_number (pos), Qmouse_face, buffer,
24741 make_number (BUF_Z (XBUFFER (buffer))
24742 - XFASTINT (w->window_end_pos)));
24743 before_string = after_string = Qnil;
24745 else
24747 /* Handle the overlay case. */
24748 before = Foverlay_start (overlay);
24749 after = Foverlay_end (overlay);
24750 before_string = Foverlay_get (overlay, Qbefore_string);
24751 after_string = Foverlay_get (overlay, Qafter_string);
24753 if (!STRINGP (before_string)) before_string = Qnil;
24754 if (!STRINGP (after_string)) after_string = Qnil;
24757 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24758 XFASTINT (before),
24759 XFASTINT (after),
24760 before_string, after_string,
24761 display_string);
24762 cursor = No_Cursor;
24767 check_help_echo:
24769 /* Look for a `help-echo' property. */
24770 if (NILP (help_echo_string)) {
24771 Lisp_Object help, overlay;
24773 /* Check overlays first. */
24774 help = overlay = Qnil;
24775 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24777 overlay = overlay_vec[i];
24778 help = Foverlay_get (overlay, Qhelp_echo);
24781 if (!NILP (help))
24783 help_echo_string = help;
24784 help_echo_window = window;
24785 help_echo_object = overlay;
24786 help_echo_pos = pos;
24788 else
24790 Lisp_Object object = glyph->object;
24791 EMACS_INT charpos = glyph->charpos;
24793 /* Try text properties. */
24794 if (STRINGP (object)
24795 && charpos >= 0
24796 && charpos < SCHARS (object))
24798 help = Fget_text_property (make_number (charpos),
24799 Qhelp_echo, object);
24800 if (NILP (help))
24802 /* If the string itself doesn't specify a help-echo,
24803 see if the buffer text ``under'' it does. */
24804 struct glyph_row *r
24805 = MATRIX_ROW (w->current_matrix, vpos);
24806 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
24807 EMACS_INT pos = string_buffer_position (w, object, start);
24808 if (pos > 0)
24810 help = Fget_char_property (make_number (pos),
24811 Qhelp_echo, w->buffer);
24812 if (!NILP (help))
24814 charpos = pos;
24815 object = w->buffer;
24820 else if (BUFFERP (object)
24821 && charpos >= BEGV
24822 && charpos < ZV)
24823 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24824 object);
24826 if (!NILP (help))
24828 help_echo_string = help;
24829 help_echo_window = window;
24830 help_echo_object = object;
24831 help_echo_pos = charpos;
24836 /* Look for a `pointer' property. */
24837 if (NILP (pointer))
24839 /* Check overlays first. */
24840 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24841 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24843 if (NILP (pointer))
24845 Lisp_Object object = glyph->object;
24846 EMACS_INT charpos = glyph->charpos;
24848 /* Try text properties. */
24849 if (STRINGP (object)
24850 && charpos >= 0
24851 && charpos < SCHARS (object))
24853 pointer = Fget_text_property (make_number (charpos),
24854 Qpointer, object);
24855 if (NILP (pointer))
24857 /* If the string itself doesn't specify a pointer,
24858 see if the buffer text ``under'' it does. */
24859 struct glyph_row *r
24860 = MATRIX_ROW (w->current_matrix, vpos);
24861 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
24862 EMACS_INT pos = string_buffer_position (w, object,
24863 start);
24864 if (pos > 0)
24865 pointer = Fget_char_property (make_number (pos),
24866 Qpointer, w->buffer);
24869 else if (BUFFERP (object)
24870 && charpos >= BEGV
24871 && charpos < ZV)
24872 pointer = Fget_text_property (make_number (charpos),
24873 Qpointer, object);
24877 BEGV = obegv;
24878 ZV = ozv;
24879 current_buffer = obuf;
24882 set_cursor:
24884 define_frame_cursor1 (f, cursor, pointer);
24888 /* EXPORT for RIF:
24889 Clear any mouse-face on window W. This function is part of the
24890 redisplay interface, and is called from try_window_id and similar
24891 functions to ensure the mouse-highlight is off. */
24893 void
24894 x_clear_window_mouse_face (struct window *w)
24896 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24897 Lisp_Object window;
24899 BLOCK_INPUT;
24900 XSETWINDOW (window, w);
24901 if (EQ (window, dpyinfo->mouse_face_window))
24902 clear_mouse_face (dpyinfo);
24903 UNBLOCK_INPUT;
24907 /* EXPORT:
24908 Just discard the mouse face information for frame F, if any.
24909 This is used when the size of F is changed. */
24911 void
24912 cancel_mouse_face (struct frame *f)
24914 Lisp_Object window;
24915 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24917 window = dpyinfo->mouse_face_window;
24918 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24920 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24921 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24922 dpyinfo->mouse_face_window = Qnil;
24927 #endif /* HAVE_WINDOW_SYSTEM */
24930 /***********************************************************************
24931 Exposure Events
24932 ***********************************************************************/
24934 #ifdef HAVE_WINDOW_SYSTEM
24936 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24937 which intersects rectangle R. R is in window-relative coordinates. */
24939 static void
24940 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
24941 enum glyph_row_area area)
24943 struct glyph *first = row->glyphs[area];
24944 struct glyph *end = row->glyphs[area] + row->used[area];
24945 struct glyph *last;
24946 int first_x, start_x, x;
24948 if (area == TEXT_AREA && row->fill_line_p)
24949 /* If row extends face to end of line write the whole line. */
24950 draw_glyphs (w, 0, row, area,
24951 0, row->used[area],
24952 DRAW_NORMAL_TEXT, 0);
24953 else
24955 /* Set START_X to the window-relative start position for drawing glyphs of
24956 AREA. The first glyph of the text area can be partially visible.
24957 The first glyphs of other areas cannot. */
24958 start_x = window_box_left_offset (w, area);
24959 x = start_x;
24960 if (area == TEXT_AREA)
24961 x += row->x;
24963 /* Find the first glyph that must be redrawn. */
24964 while (first < end
24965 && x + first->pixel_width < r->x)
24967 x += first->pixel_width;
24968 ++first;
24971 /* Find the last one. */
24972 last = first;
24973 first_x = x;
24974 while (last < end
24975 && x < r->x + r->width)
24977 x += last->pixel_width;
24978 ++last;
24981 /* Repaint. */
24982 if (last > first)
24983 draw_glyphs (w, first_x - start_x, row, area,
24984 first - row->glyphs[area], last - row->glyphs[area],
24985 DRAW_NORMAL_TEXT, 0);
24990 /* Redraw the parts of the glyph row ROW on window W intersecting
24991 rectangle R. R is in window-relative coordinates. Value is
24992 non-zero if mouse-face was overwritten. */
24994 static int
24995 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
24997 xassert (row->enabled_p);
24999 if (row->mode_line_p || w->pseudo_window_p)
25000 draw_glyphs (w, 0, row, TEXT_AREA,
25001 0, row->used[TEXT_AREA],
25002 DRAW_NORMAL_TEXT, 0);
25003 else
25005 if (row->used[LEFT_MARGIN_AREA])
25006 expose_area (w, row, r, LEFT_MARGIN_AREA);
25007 if (row->used[TEXT_AREA])
25008 expose_area (w, row, r, TEXT_AREA);
25009 if (row->used[RIGHT_MARGIN_AREA])
25010 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25011 draw_row_fringe_bitmaps (w, row);
25014 return row->mouse_face_p;
25018 /* Redraw those parts of glyphs rows during expose event handling that
25019 overlap other rows. Redrawing of an exposed line writes over parts
25020 of lines overlapping that exposed line; this function fixes that.
25022 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25023 row in W's current matrix that is exposed and overlaps other rows.
25024 LAST_OVERLAPPING_ROW is the last such row. */
25026 static void
25027 expose_overlaps (struct window *w,
25028 struct glyph_row *first_overlapping_row,
25029 struct glyph_row *last_overlapping_row,
25030 XRectangle *r)
25032 struct glyph_row *row;
25034 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25035 if (row->overlapping_p)
25037 xassert (row->enabled_p && !row->mode_line_p);
25039 row->clip = r;
25040 if (row->used[LEFT_MARGIN_AREA])
25041 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25043 if (row->used[TEXT_AREA])
25044 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25046 if (row->used[RIGHT_MARGIN_AREA])
25047 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25048 row->clip = NULL;
25053 /* Return non-zero if W's cursor intersects rectangle R. */
25055 static int
25056 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
25058 XRectangle cr, result;
25059 struct glyph *cursor_glyph;
25060 struct glyph_row *row;
25062 if (w->phys_cursor.vpos >= 0
25063 && w->phys_cursor.vpos < w->current_matrix->nrows
25064 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25065 row->enabled_p)
25066 && row->cursor_in_fringe_p)
25068 /* Cursor is in the fringe. */
25069 cr.x = window_box_right_offset (w,
25070 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25071 ? RIGHT_MARGIN_AREA
25072 : TEXT_AREA));
25073 cr.y = row->y;
25074 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25075 cr.height = row->height;
25076 return x_intersect_rectangles (&cr, r, &result);
25079 cursor_glyph = get_phys_cursor_glyph (w);
25080 if (cursor_glyph)
25082 /* r is relative to W's box, but w->phys_cursor.x is relative
25083 to left edge of W's TEXT area. Adjust it. */
25084 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25085 cr.y = w->phys_cursor.y;
25086 cr.width = cursor_glyph->pixel_width;
25087 cr.height = w->phys_cursor_height;
25088 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25089 I assume the effect is the same -- and this is portable. */
25090 return x_intersect_rectangles (&cr, r, &result);
25092 /* If we don't understand the format, pretend we're not in the hot-spot. */
25093 return 0;
25097 /* EXPORT:
25098 Draw a vertical window border to the right of window W if W doesn't
25099 have vertical scroll bars. */
25101 void
25102 x_draw_vertical_border (struct window *w)
25104 struct frame *f = XFRAME (WINDOW_FRAME (w));
25106 /* We could do better, if we knew what type of scroll-bar the adjacent
25107 windows (on either side) have... But we don't :-(
25108 However, I think this works ok. ++KFS 2003-04-25 */
25110 /* Redraw borders between horizontally adjacent windows. Don't
25111 do it for frames with vertical scroll bars because either the
25112 right scroll bar of a window, or the left scroll bar of its
25113 neighbor will suffice as a border. */
25114 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25115 return;
25117 if (!WINDOW_RIGHTMOST_P (w)
25118 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25120 int x0, x1, y0, y1;
25122 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25123 y1 -= 1;
25125 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25126 x1 -= 1;
25128 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25130 else if (!WINDOW_LEFTMOST_P (w)
25131 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25133 int x0, x1, y0, y1;
25135 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25136 y1 -= 1;
25138 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25139 x0 -= 1;
25141 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25146 /* Redraw the part of window W intersection rectangle FR. Pixel
25147 coordinates in FR are frame-relative. Call this function with
25148 input blocked. Value is non-zero if the exposure overwrites
25149 mouse-face. */
25151 static int
25152 expose_window (struct window *w, XRectangle *fr)
25154 struct frame *f = XFRAME (w->frame);
25155 XRectangle wr, r;
25156 int mouse_face_overwritten_p = 0;
25158 /* If window is not yet fully initialized, do nothing. This can
25159 happen when toolkit scroll bars are used and a window is split.
25160 Reconfiguring the scroll bar will generate an expose for a newly
25161 created window. */
25162 if (w->current_matrix == NULL)
25163 return 0;
25165 /* When we're currently updating the window, display and current
25166 matrix usually don't agree. Arrange for a thorough display
25167 later. */
25168 if (w == updated_window)
25170 SET_FRAME_GARBAGED (f);
25171 return 0;
25174 /* Frame-relative pixel rectangle of W. */
25175 wr.x = WINDOW_LEFT_EDGE_X (w);
25176 wr.y = WINDOW_TOP_EDGE_Y (w);
25177 wr.width = WINDOW_TOTAL_WIDTH (w);
25178 wr.height = WINDOW_TOTAL_HEIGHT (w);
25180 if (x_intersect_rectangles (fr, &wr, &r))
25182 int yb = window_text_bottom_y (w);
25183 struct glyph_row *row;
25184 int cursor_cleared_p;
25185 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25187 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25188 r.x, r.y, r.width, r.height));
25190 /* Convert to window coordinates. */
25191 r.x -= WINDOW_LEFT_EDGE_X (w);
25192 r.y -= WINDOW_TOP_EDGE_Y (w);
25194 /* Turn off the cursor. */
25195 if (!w->pseudo_window_p
25196 && phys_cursor_in_rect_p (w, &r))
25198 x_clear_cursor (w);
25199 cursor_cleared_p = 1;
25201 else
25202 cursor_cleared_p = 0;
25204 /* Update lines intersecting rectangle R. */
25205 first_overlapping_row = last_overlapping_row = NULL;
25206 for (row = w->current_matrix->rows;
25207 row->enabled_p;
25208 ++row)
25210 int y0 = row->y;
25211 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25213 if ((y0 >= r.y && y0 < r.y + r.height)
25214 || (y1 > r.y && y1 < r.y + r.height)
25215 || (r.y >= y0 && r.y < y1)
25216 || (r.y + r.height > y0 && r.y + r.height < y1))
25218 /* A header line may be overlapping, but there is no need
25219 to fix overlapping areas for them. KFS 2005-02-12 */
25220 if (row->overlapping_p && !row->mode_line_p)
25222 if (first_overlapping_row == NULL)
25223 first_overlapping_row = row;
25224 last_overlapping_row = row;
25227 row->clip = fr;
25228 if (expose_line (w, row, &r))
25229 mouse_face_overwritten_p = 1;
25230 row->clip = NULL;
25232 else if (row->overlapping_p)
25234 /* We must redraw a row overlapping the exposed area. */
25235 if (y0 < r.y
25236 ? y0 + row->phys_height > r.y
25237 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25239 if (first_overlapping_row == NULL)
25240 first_overlapping_row = row;
25241 last_overlapping_row = row;
25245 if (y1 >= yb)
25246 break;
25249 /* Display the mode line if there is one. */
25250 if (WINDOW_WANTS_MODELINE_P (w)
25251 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25252 row->enabled_p)
25253 && row->y < r.y + r.height)
25255 if (expose_line (w, row, &r))
25256 mouse_face_overwritten_p = 1;
25259 if (!w->pseudo_window_p)
25261 /* Fix the display of overlapping rows. */
25262 if (first_overlapping_row)
25263 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25264 fr);
25266 /* Draw border between windows. */
25267 x_draw_vertical_border (w);
25269 /* Turn the cursor on again. */
25270 if (cursor_cleared_p)
25271 update_window_cursor (w, 1);
25275 return mouse_face_overwritten_p;
25280 /* Redraw (parts) of all windows in the window tree rooted at W that
25281 intersect R. R contains frame pixel coordinates. Value is
25282 non-zero if the exposure overwrites mouse-face. */
25284 static int
25285 expose_window_tree (struct window *w, XRectangle *r)
25287 struct frame *f = XFRAME (w->frame);
25288 int mouse_face_overwritten_p = 0;
25290 while (w && !FRAME_GARBAGED_P (f))
25292 if (!NILP (w->hchild))
25293 mouse_face_overwritten_p
25294 |= expose_window_tree (XWINDOW (w->hchild), r);
25295 else if (!NILP (w->vchild))
25296 mouse_face_overwritten_p
25297 |= expose_window_tree (XWINDOW (w->vchild), r);
25298 else
25299 mouse_face_overwritten_p |= expose_window (w, r);
25301 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25304 return mouse_face_overwritten_p;
25308 /* EXPORT:
25309 Redisplay an exposed area of frame F. X and Y are the upper-left
25310 corner of the exposed rectangle. W and H are width and height of
25311 the exposed area. All are pixel values. W or H zero means redraw
25312 the entire frame. */
25314 void
25315 expose_frame (struct frame *f, int x, int y, int w, int h)
25317 XRectangle r;
25318 int mouse_face_overwritten_p = 0;
25320 TRACE ((stderr, "expose_frame "));
25322 /* No need to redraw if frame will be redrawn soon. */
25323 if (FRAME_GARBAGED_P (f))
25325 TRACE ((stderr, " garbaged\n"));
25326 return;
25329 /* If basic faces haven't been realized yet, there is no point in
25330 trying to redraw anything. This can happen when we get an expose
25331 event while Emacs is starting, e.g. by moving another window. */
25332 if (FRAME_FACE_CACHE (f) == NULL
25333 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25335 TRACE ((stderr, " no faces\n"));
25336 return;
25339 if (w == 0 || h == 0)
25341 r.x = r.y = 0;
25342 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25343 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25345 else
25347 r.x = x;
25348 r.y = y;
25349 r.width = w;
25350 r.height = h;
25353 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25354 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25356 if (WINDOWP (f->tool_bar_window))
25357 mouse_face_overwritten_p
25358 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25360 #ifdef HAVE_X_WINDOWS
25361 #ifndef MSDOS
25362 #ifndef USE_X_TOOLKIT
25363 if (WINDOWP (f->menu_bar_window))
25364 mouse_face_overwritten_p
25365 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25366 #endif /* not USE_X_TOOLKIT */
25367 #endif
25368 #endif
25370 /* Some window managers support a focus-follows-mouse style with
25371 delayed raising of frames. Imagine a partially obscured frame,
25372 and moving the mouse into partially obscured mouse-face on that
25373 frame. The visible part of the mouse-face will be highlighted,
25374 then the WM raises the obscured frame. With at least one WM, KDE
25375 2.1, Emacs is not getting any event for the raising of the frame
25376 (even tried with SubstructureRedirectMask), only Expose events.
25377 These expose events will draw text normally, i.e. not
25378 highlighted. Which means we must redo the highlight here.
25379 Subsume it under ``we love X''. --gerd 2001-08-15 */
25380 /* Included in Windows version because Windows most likely does not
25381 do the right thing if any third party tool offers
25382 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25383 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25385 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25386 if (f == dpyinfo->mouse_face_mouse_frame)
25388 int x = dpyinfo->mouse_face_mouse_x;
25389 int y = dpyinfo->mouse_face_mouse_y;
25390 clear_mouse_face (dpyinfo);
25391 note_mouse_highlight (f, x, y);
25397 /* EXPORT:
25398 Determine the intersection of two rectangles R1 and R2. Return
25399 the intersection in *RESULT. Value is non-zero if RESULT is not
25400 empty. */
25403 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
25405 XRectangle *left, *right;
25406 XRectangle *upper, *lower;
25407 int intersection_p = 0;
25409 /* Rearrange so that R1 is the left-most rectangle. */
25410 if (r1->x < r2->x)
25411 left = r1, right = r2;
25412 else
25413 left = r2, right = r1;
25415 /* X0 of the intersection is right.x0, if this is inside R1,
25416 otherwise there is no intersection. */
25417 if (right->x <= left->x + left->width)
25419 result->x = right->x;
25421 /* The right end of the intersection is the minimum of the
25422 the right ends of left and right. */
25423 result->width = (min (left->x + left->width, right->x + right->width)
25424 - result->x);
25426 /* Same game for Y. */
25427 if (r1->y < r2->y)
25428 upper = r1, lower = r2;
25429 else
25430 upper = r2, lower = r1;
25432 /* The upper end of the intersection is lower.y0, if this is inside
25433 of upper. Otherwise, there is no intersection. */
25434 if (lower->y <= upper->y + upper->height)
25436 result->y = lower->y;
25438 /* The lower end of the intersection is the minimum of the lower
25439 ends of upper and lower. */
25440 result->height = (min (lower->y + lower->height,
25441 upper->y + upper->height)
25442 - result->y);
25443 intersection_p = 1;
25447 return intersection_p;
25450 #endif /* HAVE_WINDOW_SYSTEM */
25453 /***********************************************************************
25454 Initialization
25455 ***********************************************************************/
25457 void
25458 syms_of_xdisp (void)
25460 Vwith_echo_area_save_vector = Qnil;
25461 staticpro (&Vwith_echo_area_save_vector);
25463 Vmessage_stack = Qnil;
25464 staticpro (&Vmessage_stack);
25466 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25467 staticpro (&Qinhibit_redisplay);
25469 message_dolog_marker1 = Fmake_marker ();
25470 staticpro (&message_dolog_marker1);
25471 message_dolog_marker2 = Fmake_marker ();
25472 staticpro (&message_dolog_marker2);
25473 message_dolog_marker3 = Fmake_marker ();
25474 staticpro (&message_dolog_marker3);
25476 #if GLYPH_DEBUG
25477 defsubr (&Sdump_frame_glyph_matrix);
25478 defsubr (&Sdump_glyph_matrix);
25479 defsubr (&Sdump_glyph_row);
25480 defsubr (&Sdump_tool_bar_row);
25481 defsubr (&Strace_redisplay);
25482 defsubr (&Strace_to_stderr);
25483 #endif
25484 #ifdef HAVE_WINDOW_SYSTEM
25485 defsubr (&Stool_bar_lines_needed);
25486 defsubr (&Slookup_image_map);
25487 #endif
25488 defsubr (&Sformat_mode_line);
25489 defsubr (&Sinvisible_p);
25490 defsubr (&Scurrent_bidi_paragraph_direction);
25492 staticpro (&Qmenu_bar_update_hook);
25493 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25495 staticpro (&Qoverriding_terminal_local_map);
25496 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25498 staticpro (&Qoverriding_local_map);
25499 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25501 staticpro (&Qwindow_scroll_functions);
25502 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25504 staticpro (&Qwindow_text_change_functions);
25505 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25507 staticpro (&Qredisplay_end_trigger_functions);
25508 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25510 staticpro (&Qinhibit_point_motion_hooks);
25511 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25513 Qeval = intern_c_string ("eval");
25514 staticpro (&Qeval);
25516 QCdata = intern_c_string (":data");
25517 staticpro (&QCdata);
25518 Qdisplay = intern_c_string ("display");
25519 staticpro (&Qdisplay);
25520 Qspace_width = intern_c_string ("space-width");
25521 staticpro (&Qspace_width);
25522 Qraise = intern_c_string ("raise");
25523 staticpro (&Qraise);
25524 Qslice = intern_c_string ("slice");
25525 staticpro (&Qslice);
25526 Qspace = intern_c_string ("space");
25527 staticpro (&Qspace);
25528 Qmargin = intern_c_string ("margin");
25529 staticpro (&Qmargin);
25530 Qpointer = intern_c_string ("pointer");
25531 staticpro (&Qpointer);
25532 Qleft_margin = intern_c_string ("left-margin");
25533 staticpro (&Qleft_margin);
25534 Qright_margin = intern_c_string ("right-margin");
25535 staticpro (&Qright_margin);
25536 Qcenter = intern_c_string ("center");
25537 staticpro (&Qcenter);
25538 Qline_height = intern_c_string ("line-height");
25539 staticpro (&Qline_height);
25540 QCalign_to = intern_c_string (":align-to");
25541 staticpro (&QCalign_to);
25542 QCrelative_width = intern_c_string (":relative-width");
25543 staticpro (&QCrelative_width);
25544 QCrelative_height = intern_c_string (":relative-height");
25545 staticpro (&QCrelative_height);
25546 QCeval = intern_c_string (":eval");
25547 staticpro (&QCeval);
25548 QCpropertize = intern_c_string (":propertize");
25549 staticpro (&QCpropertize);
25550 QCfile = intern_c_string (":file");
25551 staticpro (&QCfile);
25552 Qfontified = intern_c_string ("fontified");
25553 staticpro (&Qfontified);
25554 Qfontification_functions = intern_c_string ("fontification-functions");
25555 staticpro (&Qfontification_functions);
25556 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25557 staticpro (&Qtrailing_whitespace);
25558 Qescape_glyph = intern_c_string ("escape-glyph");
25559 staticpro (&Qescape_glyph);
25560 Qnobreak_space = intern_c_string ("nobreak-space");
25561 staticpro (&Qnobreak_space);
25562 Qimage = intern_c_string ("image");
25563 staticpro (&Qimage);
25564 Qtext = intern_c_string ("text");
25565 staticpro (&Qtext);
25566 Qboth = intern_c_string ("both");
25567 staticpro (&Qboth);
25568 Qboth_horiz = intern_c_string ("both-horiz");
25569 staticpro (&Qboth_horiz);
25570 Qtext_image_horiz = intern_c_string ("text-image-horiz");
25571 staticpro (&Qtext_image_horiz);
25572 QCmap = intern_c_string (":map");
25573 staticpro (&QCmap);
25574 QCpointer = intern_c_string (":pointer");
25575 staticpro (&QCpointer);
25576 Qrect = intern_c_string ("rect");
25577 staticpro (&Qrect);
25578 Qcircle = intern_c_string ("circle");
25579 staticpro (&Qcircle);
25580 Qpoly = intern_c_string ("poly");
25581 staticpro (&Qpoly);
25582 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25583 staticpro (&Qmessage_truncate_lines);
25584 Qgrow_only = intern_c_string ("grow-only");
25585 staticpro (&Qgrow_only);
25586 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25587 staticpro (&Qinhibit_menubar_update);
25588 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25589 staticpro (&Qinhibit_eval_during_redisplay);
25590 Qposition = intern_c_string ("position");
25591 staticpro (&Qposition);
25592 Qbuffer_position = intern_c_string ("buffer-position");
25593 staticpro (&Qbuffer_position);
25594 Qobject = intern_c_string ("object");
25595 staticpro (&Qobject);
25596 Qbar = intern_c_string ("bar");
25597 staticpro (&Qbar);
25598 Qhbar = intern_c_string ("hbar");
25599 staticpro (&Qhbar);
25600 Qbox = intern_c_string ("box");
25601 staticpro (&Qbox);
25602 Qhollow = intern_c_string ("hollow");
25603 staticpro (&Qhollow);
25604 Qhand = intern_c_string ("hand");
25605 staticpro (&Qhand);
25606 Qarrow = intern_c_string ("arrow");
25607 staticpro (&Qarrow);
25608 Qtext = intern_c_string ("text");
25609 staticpro (&Qtext);
25610 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25611 staticpro (&Qrisky_local_variable);
25612 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25613 staticpro (&Qinhibit_free_realized_faces);
25615 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25616 Fcons (intern_c_string ("void-variable"), Qnil)),
25617 Qnil);
25618 staticpro (&list_of_error);
25620 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25621 staticpro (&Qlast_arrow_position);
25622 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25623 staticpro (&Qlast_arrow_string);
25625 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25626 staticpro (&Qoverlay_arrow_string);
25627 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25628 staticpro (&Qoverlay_arrow_bitmap);
25630 echo_buffer[0] = echo_buffer[1] = Qnil;
25631 staticpro (&echo_buffer[0]);
25632 staticpro (&echo_buffer[1]);
25634 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25635 staticpro (&echo_area_buffer[0]);
25636 staticpro (&echo_area_buffer[1]);
25638 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25639 staticpro (&Vmessages_buffer_name);
25641 mode_line_proptrans_alist = Qnil;
25642 staticpro (&mode_line_proptrans_alist);
25643 mode_line_string_list = Qnil;
25644 staticpro (&mode_line_string_list);
25645 mode_line_string_face = Qnil;
25646 staticpro (&mode_line_string_face);
25647 mode_line_string_face_prop = Qnil;
25648 staticpro (&mode_line_string_face_prop);
25649 Vmode_line_unwind_vector = Qnil;
25650 staticpro (&Vmode_line_unwind_vector);
25652 help_echo_string = Qnil;
25653 staticpro (&help_echo_string);
25654 help_echo_object = Qnil;
25655 staticpro (&help_echo_object);
25656 help_echo_window = Qnil;
25657 staticpro (&help_echo_window);
25658 previous_help_echo_string = Qnil;
25659 staticpro (&previous_help_echo_string);
25660 help_echo_pos = -1;
25662 Qright_to_left = intern_c_string ("right-to-left");
25663 staticpro (&Qright_to_left);
25664 Qleft_to_right = intern_c_string ("left-to-right");
25665 staticpro (&Qleft_to_right);
25667 #ifdef HAVE_WINDOW_SYSTEM
25668 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25669 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25670 For example, if a block cursor is over a tab, it will be drawn as
25671 wide as that tab on the display. */);
25672 x_stretch_cursor_p = 0;
25673 #endif
25675 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25676 doc: /* *Non-nil means highlight trailing whitespace.
25677 The face used for trailing whitespace is `trailing-whitespace'. */);
25678 Vshow_trailing_whitespace = Qnil;
25680 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25681 doc: /* *Control highlighting of nobreak space and soft hyphen.
25682 A value of t means highlight the character itself (for nobreak space,
25683 use face `nobreak-space').
25684 A value of nil means no highlighting.
25685 Other values mean display the escape glyph followed by an ordinary
25686 space or ordinary hyphen. */);
25687 Vnobreak_char_display = Qt;
25689 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25690 doc: /* *The pointer shape to show in void text areas.
25691 A value of nil means to show the text pointer. Other options are `arrow',
25692 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25693 Vvoid_text_area_pointer = Qarrow;
25695 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25696 doc: /* Non-nil means don't actually do any redisplay.
25697 This is used for internal purposes. */);
25698 Vinhibit_redisplay = Qnil;
25700 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25701 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25702 Vglobal_mode_string = Qnil;
25704 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25705 doc: /* Marker for where to display an arrow on top of the buffer text.
25706 This must be the beginning of a line in order to work.
25707 See also `overlay-arrow-string'. */);
25708 Voverlay_arrow_position = Qnil;
25710 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25711 doc: /* String to display as an arrow in non-window frames.
25712 See also `overlay-arrow-position'. */);
25713 Voverlay_arrow_string = make_pure_c_string ("=>");
25715 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25716 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25717 The symbols on this list are examined during redisplay to determine
25718 where to display overlay arrows. */);
25719 Voverlay_arrow_variable_list
25720 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25722 DEFVAR_INT ("scroll-step", &scroll_step,
25723 doc: /* *The number of lines to try scrolling a window by when point moves out.
25724 If that fails to bring point back on frame, point is centered instead.
25725 If this is zero, point is always centered after it moves off frame.
25726 If you want scrolling to always be a line at a time, you should set
25727 `scroll-conservatively' to a large value rather than set this to 1. */);
25729 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25730 doc: /* *Scroll up to this many lines, to bring point back on screen.
25731 If point moves off-screen, redisplay will scroll by up to
25732 `scroll-conservatively' lines in order to bring point just barely
25733 onto the screen again. If that cannot be done, then redisplay
25734 recenters point as usual.
25736 A value of zero means always recenter point if it moves off screen. */);
25737 scroll_conservatively = 0;
25739 DEFVAR_INT ("scroll-margin", &scroll_margin,
25740 doc: /* *Number of lines of margin at the top and bottom of a window.
25741 Recenter the window whenever point gets within this many lines
25742 of the top or bottom of the window. */);
25743 scroll_margin = 0;
25745 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25746 doc: /* Pixels per inch value for non-window system displays.
25747 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25748 Vdisplay_pixels_per_inch = make_float (72.0);
25750 #if GLYPH_DEBUG
25751 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25752 #endif
25754 DEFVAR_LISP ("truncate-partial-width-windows",
25755 &Vtruncate_partial_width_windows,
25756 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25757 For an integer value, truncate lines in each window narrower than the
25758 full frame width, provided the window width is less than that integer;
25759 otherwise, respect the value of `truncate-lines'.
25761 For any other non-nil value, truncate lines in all windows that do
25762 not span the full frame width.
25764 A value of nil means to respect the value of `truncate-lines'.
25766 If `word-wrap' is enabled, you might want to reduce this. */);
25767 Vtruncate_partial_width_windows = make_number (50);
25769 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25770 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25771 Any other value means to use the appropriate face, `mode-line',
25772 `header-line', or `menu' respectively. */);
25773 mode_line_inverse_video = 1;
25775 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25776 doc: /* *Maximum buffer size for which line number should be displayed.
25777 If the buffer is bigger than this, the line number does not appear
25778 in the mode line. A value of nil means no limit. */);
25779 Vline_number_display_limit = Qnil;
25781 DEFVAR_INT ("line-number-display-limit-width",
25782 &line_number_display_limit_width,
25783 doc: /* *Maximum line width (in characters) for line number display.
25784 If the average length of the lines near point is bigger than this, then the
25785 line number may be omitted from the mode line. */);
25786 line_number_display_limit_width = 200;
25788 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25789 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25790 highlight_nonselected_windows = 0;
25792 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25793 doc: /* Non-nil if more than one frame is visible on this display.
25794 Minibuffer-only frames don't count, but iconified frames do.
25795 This variable is not guaranteed to be accurate except while processing
25796 `frame-title-format' and `icon-title-format'. */);
25798 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25799 doc: /* Template for displaying the title bar of visible frames.
25800 \(Assuming the window manager supports this feature.)
25802 This variable has the same structure as `mode-line-format', except that
25803 the %c and %l constructs are ignored. It is used only on frames for
25804 which no explicit name has been set \(see `modify-frame-parameters'). */);
25806 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25807 doc: /* Template for displaying the title bar of an iconified frame.
25808 \(Assuming the window manager supports this feature.)
25809 This variable has the same structure as `mode-line-format' (which see),
25810 and is used only on frames for which no explicit name has been set
25811 \(see `modify-frame-parameters'). */);
25812 Vicon_title_format
25813 = Vframe_title_format
25814 = pure_cons (intern_c_string ("multiple-frames"),
25815 pure_cons (make_pure_c_string ("%b"),
25816 pure_cons (pure_cons (empty_unibyte_string,
25817 pure_cons (intern_c_string ("invocation-name"),
25818 pure_cons (make_pure_c_string ("@"),
25819 pure_cons (intern_c_string ("system-name"),
25820 Qnil)))),
25821 Qnil)));
25823 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25824 doc: /* Maximum number of lines to keep in the message log buffer.
25825 If nil, disable message logging. If t, log messages but don't truncate
25826 the buffer when it becomes large. */);
25827 Vmessage_log_max = make_number (100);
25829 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25830 doc: /* Functions called before redisplay, if window sizes have changed.
25831 The value should be a list of functions that take one argument.
25832 Just before redisplay, for each frame, if any of its windows have changed
25833 size since the last redisplay, or have been split or deleted,
25834 all the functions in the list are called, with the frame as argument. */);
25835 Vwindow_size_change_functions = Qnil;
25837 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25838 doc: /* List of functions to call before redisplaying a window with scrolling.
25839 Each function is called with two arguments, the window and its new
25840 display-start position. Note that these functions are also called by
25841 `set-window-buffer'. Also note that the value of `window-end' is not
25842 valid when these functions are called. */);
25843 Vwindow_scroll_functions = Qnil;
25845 DEFVAR_LISP ("window-text-change-functions",
25846 &Vwindow_text_change_functions,
25847 doc: /* Functions to call in redisplay when text in the window might change. */);
25848 Vwindow_text_change_functions = Qnil;
25850 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25851 doc: /* Functions called when redisplay of a window reaches the end trigger.
25852 Each function is called with two arguments, the window and the end trigger value.
25853 See `set-window-redisplay-end-trigger'. */);
25854 Vredisplay_end_trigger_functions = Qnil;
25856 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25857 doc: /* *Non-nil means autoselect window with mouse pointer.
25858 If nil, do not autoselect windows.
25859 A positive number means delay autoselection by that many seconds: a
25860 window is autoselected only after the mouse has remained in that
25861 window for the duration of the delay.
25862 A negative number has a similar effect, but causes windows to be
25863 autoselected only after the mouse has stopped moving. \(Because of
25864 the way Emacs compares mouse events, you will occasionally wait twice
25865 that time before the window gets selected.\)
25866 Any other value means to autoselect window instantaneously when the
25867 mouse pointer enters it.
25869 Autoselection selects the minibuffer only if it is active, and never
25870 unselects the minibuffer if it is active.
25872 When customizing this variable make sure that the actual value of
25873 `focus-follows-mouse' matches the behavior of your window manager. */);
25874 Vmouse_autoselect_window = Qnil;
25876 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25877 doc: /* *Non-nil means automatically resize tool-bars.
25878 This dynamically changes the tool-bar's height to the minimum height
25879 that is needed to make all tool-bar items visible.
25880 If value is `grow-only', the tool-bar's height is only increased
25881 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25882 Vauto_resize_tool_bars = Qt;
25884 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25885 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25886 auto_raise_tool_bar_buttons_p = 1;
25888 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25889 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25890 make_cursor_line_fully_visible_p = 1;
25892 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25893 doc: /* *Border below tool-bar in pixels.
25894 If an integer, use it as the height of the border.
25895 If it is one of `internal-border-width' or `border-width', use the
25896 value of the corresponding frame parameter.
25897 Otherwise, no border is added below the tool-bar. */);
25898 Vtool_bar_border = Qinternal_border_width;
25900 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25901 doc: /* *Margin around tool-bar buttons in pixels.
25902 If an integer, use that for both horizontal and vertical margins.
25903 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25904 HORZ specifying the horizontal margin, and VERT specifying the
25905 vertical margin. */);
25906 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25908 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25909 doc: /* *Relief thickness of tool-bar buttons. */);
25910 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25912 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
25913 doc: /* *Tool bar style to use.
25914 It can be one of
25915 image - show images only
25916 text - show text only
25917 both - show both, text below image
25918 both-horiz - show text to the right of the image
25919 text-image-horiz - show text to the left of the image
25920 any other - use system default or image if no system default. */);
25921 Vtool_bar_style = Qnil;
25923 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
25924 doc: /* *Maximum number of characters a label can have to be shown.
25925 The tool bar style must also show labels for this to have any effect, see
25926 `tool-bar-style'. */);
25927 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
25929 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25930 doc: /* List of functions to call to fontify regions of text.
25931 Each function is called with one argument POS. Functions must
25932 fontify a region starting at POS in the current buffer, and give
25933 fontified regions the property `fontified'. */);
25934 Vfontification_functions = Qnil;
25935 Fmake_variable_buffer_local (Qfontification_functions);
25937 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25938 &unibyte_display_via_language_environment,
25939 doc: /* *Non-nil means display unibyte text according to language environment.
25940 Specifically, this means that raw bytes in the range 160-255 decimal
25941 are displayed by converting them to the equivalent multibyte characters
25942 according to the current language environment. As a result, they are
25943 displayed according to the current fontset.
25945 Note that this variable affects only how these bytes are displayed,
25946 but does not change the fact they are interpreted as raw bytes. */);
25947 unibyte_display_via_language_environment = 0;
25949 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25950 doc: /* *Maximum height for resizing mini-windows.
25951 If a float, it specifies a fraction of the mini-window frame's height.
25952 If an integer, it specifies a number of lines. */);
25953 Vmax_mini_window_height = make_float (0.25);
25955 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25956 doc: /* *How to resize mini-windows.
25957 A value of nil means don't automatically resize mini-windows.
25958 A value of t means resize them to fit the text displayed in them.
25959 A value of `grow-only', the default, means let mini-windows grow
25960 only, until their display becomes empty, at which point the windows
25961 go back to their normal size. */);
25962 Vresize_mini_windows = Qgrow_only;
25964 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25965 doc: /* Alist specifying how to blink the cursor off.
25966 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25967 `cursor-type' frame-parameter or variable equals ON-STATE,
25968 comparing using `equal', Emacs uses OFF-STATE to specify
25969 how to blink it off. ON-STATE and OFF-STATE are values for
25970 the `cursor-type' frame parameter.
25972 If a frame's ON-STATE has no entry in this list,
25973 the frame's other specifications determine how to blink the cursor off. */);
25974 Vblink_cursor_alist = Qnil;
25976 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25977 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25978 automatic_hscrolling_p = 1;
25979 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25980 staticpro (&Qauto_hscroll_mode);
25982 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25983 doc: /* *How many columns away from the window edge point is allowed to get
25984 before automatic hscrolling will horizontally scroll the window. */);
25985 hscroll_margin = 5;
25987 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25988 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25989 When point is less than `hscroll-margin' columns from the window
25990 edge, automatic hscrolling will scroll the window by the amount of columns
25991 determined by this variable. If its value is a positive integer, scroll that
25992 many columns. If it's a positive floating-point number, it specifies the
25993 fraction of the window's width to scroll. If it's nil or zero, point will be
25994 centered horizontally after the scroll. Any other value, including negative
25995 numbers, are treated as if the value were zero.
25997 Automatic hscrolling always moves point outside the scroll margin, so if
25998 point was more than scroll step columns inside the margin, the window will
25999 scroll more than the value given by the scroll step.
26001 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26002 and `scroll-right' overrides this variable's effect. */);
26003 Vhscroll_step = make_number (0);
26005 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26006 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26007 Bind this around calls to `message' to let it take effect. */);
26008 message_truncate_lines = 0;
26010 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26011 doc: /* Normal hook run to update the menu bar definitions.
26012 Redisplay runs this hook before it redisplays the menu bar.
26013 This is used to update submenus such as Buffers,
26014 whose contents depend on various data. */);
26015 Vmenu_bar_update_hook = Qnil;
26017 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26018 doc: /* Frame for which we are updating a menu.
26019 The enable predicate for a menu binding should check this variable. */);
26020 Vmenu_updating_frame = Qnil;
26022 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26023 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26024 inhibit_menubar_update = 0;
26026 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26027 doc: /* Prefix prepended to all continuation lines at display time.
26028 The value may be a string, an image, or a stretch-glyph; it is
26029 interpreted in the same way as the value of a `display' text property.
26031 This variable is overridden by any `wrap-prefix' text or overlay
26032 property.
26034 To add a prefix to non-continuation lines, use `line-prefix'. */);
26035 Vwrap_prefix = Qnil;
26036 staticpro (&Qwrap_prefix);
26037 Qwrap_prefix = intern_c_string ("wrap-prefix");
26038 Fmake_variable_buffer_local (Qwrap_prefix);
26040 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26041 doc: /* Prefix prepended to all non-continuation lines at display time.
26042 The value may be a string, an image, or a stretch-glyph; it is
26043 interpreted in the same way as the value of a `display' text property.
26045 This variable is overridden by any `line-prefix' text or overlay
26046 property.
26048 To add a prefix to continuation lines, use `wrap-prefix'. */);
26049 Vline_prefix = Qnil;
26050 staticpro (&Qline_prefix);
26051 Qline_prefix = intern_c_string ("line-prefix");
26052 Fmake_variable_buffer_local (Qline_prefix);
26054 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
26055 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26056 inhibit_eval_during_redisplay = 0;
26058 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
26059 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26060 inhibit_free_realized_faces = 0;
26062 #if GLYPH_DEBUG
26063 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
26064 doc: /* Inhibit try_window_id display optimization. */);
26065 inhibit_try_window_id = 0;
26067 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
26068 doc: /* Inhibit try_window_reusing display optimization. */);
26069 inhibit_try_window_reusing = 0;
26071 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
26072 doc: /* Inhibit try_cursor_movement display optimization. */);
26073 inhibit_try_cursor_movement = 0;
26074 #endif /* GLYPH_DEBUG */
26076 DEFVAR_INT ("overline-margin", &overline_margin,
26077 doc: /* *Space between overline and text, in pixels.
26078 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26079 margin to the caracter height. */);
26080 overline_margin = 2;
26082 DEFVAR_INT ("underline-minimum-offset",
26083 &underline_minimum_offset,
26084 doc: /* Minimum distance between baseline and underline.
26085 This can improve legibility of underlined text at small font sizes,
26086 particularly when using variable `x-use-underline-position-properties'
26087 with fonts that specify an UNDERLINE_POSITION relatively close to the
26088 baseline. The default value is 1. */);
26089 underline_minimum_offset = 1;
26091 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
26092 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
26093 display_hourglass_p = 1;
26095 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26096 doc: /* *Seconds to wait before displaying an hourglass pointer.
26097 Value must be an integer or float. */);
26098 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26100 hourglass_atimer = NULL;
26101 hourglass_shown_p = 0;
26105 /* Initialize this module when Emacs starts. */
26107 void
26108 init_xdisp (void)
26110 Lisp_Object root_window;
26111 struct window *mini_w;
26113 current_header_line_height = current_mode_line_height = -1;
26115 CHARPOS (this_line_start_pos) = 0;
26117 mini_w = XWINDOW (minibuf_window);
26118 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26120 if (!noninteractive)
26122 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26123 int i;
26125 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26126 set_window_height (root_window,
26127 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26129 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26130 set_window_height (minibuf_window, 1, 0);
26132 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26133 mini_w->total_cols = make_number (FRAME_COLS (f));
26135 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26136 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26137 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26139 /* The default ellipsis glyphs `...'. */
26140 for (i = 0; i < 3; ++i)
26141 default_invis_vector[i] = make_number ('.');
26145 /* Allocate the buffer for frame titles.
26146 Also used for `format-mode-line'. */
26147 int size = 100;
26148 mode_line_noprop_buf = (char *) xmalloc (size);
26149 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26150 mode_line_noprop_ptr = mode_line_noprop_buf;
26151 mode_line_target = MODE_LINE_DISPLAY;
26154 help_echo_showing_p = 0;
26157 /* Since w32 does not support atimers, it defines its own implementation of
26158 the following three functions in w32fns.c. */
26159 #ifndef WINDOWSNT
26161 /* Platform-independent portion of hourglass implementation. */
26163 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26165 hourglass_started (void)
26167 return hourglass_shown_p || hourglass_atimer != NULL;
26170 /* Cancel a currently active hourglass timer, and start a new one. */
26171 void
26172 start_hourglass (void)
26174 #if defined (HAVE_WINDOW_SYSTEM)
26175 EMACS_TIME delay;
26176 int secs, usecs = 0;
26178 cancel_hourglass ();
26180 if (INTEGERP (Vhourglass_delay)
26181 && XINT (Vhourglass_delay) > 0)
26182 secs = XFASTINT (Vhourglass_delay);
26183 else if (FLOATP (Vhourglass_delay)
26184 && XFLOAT_DATA (Vhourglass_delay) > 0)
26186 Lisp_Object tem;
26187 tem = Ftruncate (Vhourglass_delay, Qnil);
26188 secs = XFASTINT (tem);
26189 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26191 else
26192 secs = DEFAULT_HOURGLASS_DELAY;
26194 EMACS_SET_SECS_USECS (delay, secs, usecs);
26195 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26196 show_hourglass, NULL);
26197 #endif
26201 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26202 shown. */
26203 void
26204 cancel_hourglass (void)
26206 #if defined (HAVE_WINDOW_SYSTEM)
26207 if (hourglass_atimer)
26209 cancel_atimer (hourglass_atimer);
26210 hourglass_atimer = NULL;
26213 if (hourglass_shown_p)
26214 hide_hourglass ();
26215 #endif
26217 #endif /* ! WINDOWSNT */
26219 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
26220 (do not change this comment) */