Cleanup of window coordinate positioning code.
[emacs.git] / src / xdisp.c
blobd3ebc1a4a8ade9c6648eac1d74d5bc3280aad598
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 2010 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 of a 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 Bidirectional display and character compositions
224 Some scripts cannot be displayed by drawing each character
225 individually, because adjacent characters change each other's shape
226 on display. For example, Arabic and Indic scripts belong to this
227 category.
229 Emacs display supports this by providing "character compositions",
230 most of which is implemented in composite.c. During the buffer
231 scan that delivers characters to PRODUCE_GLYPHS, if the next
232 character to be delivered is a composed character, the iteration
233 calls composition_reseat_it and next_element_from_composition. If
234 they succeed to compose the character with one or more of the
235 following characters, the whole sequence of characters that where
236 composed is recorded in the `struct composition_it' object that is
237 part of the buffer iterator. The composed sequence could produce
238 one or more font glyphs (called "grapheme clusters") on the screen.
239 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
240 in the direction corresponding to the current bidi scan direction
241 (recorded in the scan_dir member of the `struct bidi_it' object
242 that is part of the buffer iterator). In particular, if the bidi
243 iterator currently scans the buffer backwards, the grapheme
244 clusters are delivered back to front. This reorders the grapheme
245 clusters as appropriate for the current bidi context. Note that
246 this means that the grapheme clusters are always stored in the
247 LGSTRING object (see composite.c) in the logical order.
249 Moving an iterator in bidirectional text
250 without producing glyphs
252 Note one important detail mentioned above: that the bidi reordering
253 engine, driven by the iterator, produces characters in R2L rows
254 starting at the character that will be the rightmost on display.
255 As far as the iterator is concerned, the geometry of such rows is
256 still left to right, i.e. the iterator "thinks" the first character
257 is at the leftmost pixel position. The iterator does not know that
258 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
259 delivers. This is important when functions from the the move_it_*
260 family are used to get to certain screen position or to match
261 screen coordinates with buffer coordinates: these functions use the
262 iterator geometry, which is left to right even in R2L paragraphs.
263 This works well with most callers of move_it_*, because they need
264 to get to a specific column, and columns are still numbered in the
265 reading order, i.e. the rightmost character in a R2L paragraph is
266 still column zero. But some callers do not get well with this; a
267 notable example is mouse clicks that need to find the character
268 that corresponds to certain pixel coordinates. See
269 buffer_posn_from_coords in dispnew.c for how this is handled. */
271 #include <config.h>
272 #include <stdio.h>
273 #include <limits.h>
274 #include <setjmp.h>
276 #include "lisp.h"
277 #include "keyboard.h"
278 #include "frame.h"
279 #include "window.h"
280 #include "termchar.h"
281 #include "dispextern.h"
282 #include "buffer.h"
283 #include "character.h"
284 #include "charset.h"
285 #include "indent.h"
286 #include "commands.h"
287 #include "keymap.h"
288 #include "macros.h"
289 #include "disptab.h"
290 #include "termhooks.h"
291 #include "termopts.h"
292 #include "intervals.h"
293 #include "coding.h"
294 #include "process.h"
295 #include "region-cache.h"
296 #include "font.h"
297 #include "fontset.h"
298 #include "blockinput.h"
300 #ifdef HAVE_X_WINDOWS
301 #include "xterm.h"
302 #endif
303 #ifdef WINDOWSNT
304 #include "w32term.h"
305 #endif
306 #ifdef HAVE_NS
307 #include "nsterm.h"
308 #endif
309 #ifdef USE_GTK
310 #include "gtkutil.h"
311 #endif
313 #include "font.h"
315 #ifndef FRAME_X_OUTPUT
316 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
317 #endif
319 #define INFINITY 10000000
321 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
322 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
323 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
324 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
325 Lisp_Object Qinhibit_point_motion_hooks;
326 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
327 Lisp_Object Qfontified;
328 Lisp_Object Qgrow_only;
329 Lisp_Object Qinhibit_eval_during_redisplay;
330 Lisp_Object Qbuffer_position, Qposition, Qobject;
331 Lisp_Object Qright_to_left, Qleft_to_right;
333 /* Cursor shapes */
334 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
336 /* Pointer shapes */
337 Lisp_Object Qarrow, Qhand, Qtext;
339 Lisp_Object Qrisky_local_variable;
341 /* Holds the list (error). */
342 Lisp_Object list_of_error;
344 /* Functions called to fontify regions of text. */
346 Lisp_Object Vfontification_functions;
347 Lisp_Object Qfontification_functions;
349 /* Non-nil means automatically select any window when the mouse
350 cursor moves into it. */
351 Lisp_Object Vmouse_autoselect_window;
353 Lisp_Object Vwrap_prefix, Qwrap_prefix;
354 Lisp_Object Vline_prefix, Qline_prefix;
356 /* Non-zero means draw tool bar buttons raised when the mouse moves
357 over them. */
359 int auto_raise_tool_bar_buttons_p;
361 /* Non-zero means to reposition window if cursor line is only partially visible. */
363 int make_cursor_line_fully_visible_p;
365 /* Margin below tool bar in pixels. 0 or nil means no margin.
366 If value is `internal-border-width' or `border-width',
367 the corresponding frame parameter is used. */
369 Lisp_Object Vtool_bar_border;
371 /* Margin around tool bar buttons in pixels. */
373 Lisp_Object Vtool_bar_button_margin;
375 /* Thickness of shadow to draw around tool bar buttons. */
377 EMACS_INT tool_bar_button_relief;
379 /* Non-nil means automatically resize tool-bars so that all tool-bar
380 items are visible, and no blank lines remain.
382 If value is `grow-only', only make tool-bar bigger. */
384 Lisp_Object Vauto_resize_tool_bars;
386 /* Type of tool bar. Can be symbols image, text, both or both-hroiz. */
388 Lisp_Object Vtool_bar_style;
390 /* Maximum number of characters a label can have to be shown. */
392 EMACS_INT tool_bar_max_label_size;
394 /* Non-zero means draw block and hollow cursor as wide as the glyph
395 under it. For example, if a block cursor is over a tab, it will be
396 drawn as wide as that tab on the display. */
398 int x_stretch_cursor_p;
400 /* Non-nil means don't actually do any redisplay. */
402 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
404 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
406 int inhibit_eval_during_redisplay;
408 /* Names of text properties relevant for redisplay. */
410 Lisp_Object Qdisplay;
412 /* Symbols used in text property values. */
414 Lisp_Object Vdisplay_pixels_per_inch;
415 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
416 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
417 Lisp_Object Qslice;
418 Lisp_Object Qcenter;
419 Lisp_Object Qmargin, Qpointer;
420 Lisp_Object Qline_height;
422 /* Non-nil means highlight trailing whitespace. */
424 Lisp_Object Vshow_trailing_whitespace;
426 /* Non-nil means escape non-break space and hyphens. */
428 Lisp_Object Vnobreak_char_display;
430 #ifdef HAVE_WINDOW_SYSTEM
432 /* Test if overflow newline into fringe. Called with iterator IT
433 at or past right window margin, and with IT->current_x set. */
435 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
436 (!NILP (Voverflow_newline_into_fringe) \
437 && FRAME_WINDOW_P ((IT)->f) \
438 && ((IT)->bidi_it.paragraph_dir == R2L \
439 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
440 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
441 && (IT)->current_x == (IT)->last_visible_x \
442 && (IT)->line_wrap != WORD_WRAP)
444 #else /* !HAVE_WINDOW_SYSTEM */
445 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
446 #endif /* HAVE_WINDOW_SYSTEM */
448 /* Test if the display element loaded in IT is a space or tab
449 character. This is used to determine word wrapping. */
451 #define IT_DISPLAYING_WHITESPACE(it) \
452 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
454 /* Non-nil means show the text cursor in void text areas
455 i.e. in blank areas after eol and eob. This used to be
456 the default in 21.3. */
458 Lisp_Object Vvoid_text_area_pointer;
460 /* Name of the face used to highlight trailing whitespace. */
462 Lisp_Object Qtrailing_whitespace;
464 /* Name and number of the face used to highlight escape glyphs. */
466 Lisp_Object Qescape_glyph;
468 /* Name and number of the face used to highlight non-breaking spaces. */
470 Lisp_Object Qnobreak_space;
472 /* The symbol `image' which is the car of the lists used to represent
473 images in Lisp. Also a tool bar style. */
475 Lisp_Object Qimage;
477 /* The image map types. */
478 Lisp_Object QCmap, QCpointer;
479 Lisp_Object Qrect, Qcircle, Qpoly;
481 /* Tool bar styles */
482 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
484 /* Non-zero means print newline to stdout before next mini-buffer
485 message. */
487 int noninteractive_need_newline;
489 /* Non-zero means print newline to message log before next message. */
491 static int message_log_need_newline;
493 /* Three markers that message_dolog uses.
494 It could allocate them itself, but that causes trouble
495 in handling memory-full errors. */
496 static Lisp_Object message_dolog_marker1;
497 static Lisp_Object message_dolog_marker2;
498 static Lisp_Object message_dolog_marker3;
500 /* The buffer position of the first character appearing entirely or
501 partially on the line of the selected window which contains the
502 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
503 redisplay optimization in redisplay_internal. */
505 static struct text_pos this_line_start_pos;
507 /* Number of characters past the end of the line above, including the
508 terminating newline. */
510 static struct text_pos this_line_end_pos;
512 /* The vertical positions and the height of this line. */
514 static int this_line_vpos;
515 static int this_line_y;
516 static int this_line_pixel_height;
518 /* X position at which this display line starts. Usually zero;
519 negative if first character is partially visible. */
521 static int this_line_start_x;
523 /* Buffer that this_line_.* variables are referring to. */
525 static struct buffer *this_line_buffer;
527 /* Nonzero means truncate lines in all windows less wide than the
528 frame. */
530 Lisp_Object Vtruncate_partial_width_windows;
532 /* A flag to control how to display unibyte 8-bit character. */
534 int unibyte_display_via_language_environment;
536 /* Nonzero means we have more than one non-mini-buffer-only frame.
537 Not guaranteed to be accurate except while parsing
538 frame-title-format. */
540 int multiple_frames;
542 Lisp_Object Vglobal_mode_string;
545 /* List of variables (symbols) which hold markers for overlay arrows.
546 The symbols on this list are examined during redisplay to determine
547 where to display overlay arrows. */
549 Lisp_Object Voverlay_arrow_variable_list;
551 /* Marker for where to display an arrow on top of the buffer text. */
553 Lisp_Object Voverlay_arrow_position;
555 /* String to display for the arrow. Only used on terminal frames. */
557 Lisp_Object Voverlay_arrow_string;
559 /* Values of those variables at last redisplay are stored as
560 properties on `overlay-arrow-position' symbol. However, if
561 Voverlay_arrow_position is a marker, last-arrow-position is its
562 numerical position. */
564 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
566 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
567 properties on a symbol in overlay-arrow-variable-list. */
569 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
571 /* Like mode-line-format, but for the title bar on a visible frame. */
573 Lisp_Object Vframe_title_format;
575 /* Like mode-line-format, but for the title bar on an iconified frame. */
577 Lisp_Object Vicon_title_format;
579 /* List of functions to call when a window's size changes. These
580 functions get one arg, a frame on which one or more windows' sizes
581 have changed. */
583 static Lisp_Object Vwindow_size_change_functions;
585 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
587 /* Nonzero if an overlay arrow has been displayed in this window. */
589 static int overlay_arrow_seen;
591 /* Nonzero means highlight the region even in nonselected windows. */
593 int highlight_nonselected_windows;
595 /* If cursor motion alone moves point off frame, try scrolling this
596 many lines up or down if that will bring it back. */
598 static EMACS_INT scroll_step;
600 /* Nonzero means scroll just far enough to bring point back on the
601 screen, when appropriate. */
603 static EMACS_INT scroll_conservatively;
605 /* Recenter the window whenever point gets within this many lines of
606 the top or bottom of the window. This value is translated into a
607 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
608 that there is really a fixed pixel height scroll margin. */
610 EMACS_INT scroll_margin;
612 /* Number of windows showing the buffer of the selected window (or
613 another buffer with the same base buffer). keyboard.c refers to
614 this. */
616 int buffer_shared;
618 /* Vector containing glyphs for an ellipsis `...'. */
620 static Lisp_Object default_invis_vector[3];
622 /* Zero means display the mode-line/header-line/menu-bar in the default face
623 (this slightly odd definition is for compatibility with previous versions
624 of emacs), non-zero means display them using their respective faces.
626 This variable is deprecated. */
628 int mode_line_inverse_video;
630 /* Prompt to display in front of the mini-buffer contents. */
632 Lisp_Object minibuf_prompt;
634 /* Width of current mini-buffer prompt. Only set after display_line
635 of the line that contains the prompt. */
637 int minibuf_prompt_width;
639 /* This is the window where the echo area message was displayed. It
640 is always a mini-buffer window, but it may not be the same window
641 currently active as a mini-buffer. */
643 Lisp_Object echo_area_window;
645 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
646 pushes the current message and the value of
647 message_enable_multibyte on the stack, the function restore_message
648 pops the stack and displays MESSAGE again. */
650 Lisp_Object Vmessage_stack;
652 /* Nonzero means multibyte characters were enabled when the echo area
653 message was specified. */
655 int message_enable_multibyte;
657 /* Nonzero if we should redraw the mode lines on the next redisplay. */
659 int update_mode_lines;
661 /* Nonzero if window sizes or contents have changed since last
662 redisplay that finished. */
664 int windows_or_buffers_changed;
666 /* Nonzero means a frame's cursor type has been changed. */
668 int cursor_type_changed;
670 /* Nonzero after display_mode_line if %l was used and it displayed a
671 line number. */
673 int line_number_displayed;
675 /* Maximum buffer size for which to display line numbers. */
677 Lisp_Object Vline_number_display_limit;
679 /* Line width to consider when repositioning for line number display. */
681 static EMACS_INT line_number_display_limit_width;
683 /* Number of lines to keep in the message log buffer. t means
684 infinite. nil means don't log at all. */
686 Lisp_Object Vmessage_log_max;
688 /* The name of the *Messages* buffer, a string. */
690 static Lisp_Object Vmessages_buffer_name;
692 /* Current, index 0, and last displayed echo area message. Either
693 buffers from echo_buffers, or nil to indicate no message. */
695 Lisp_Object echo_area_buffer[2];
697 /* The buffers referenced from echo_area_buffer. */
699 static Lisp_Object echo_buffer[2];
701 /* A vector saved used in with_area_buffer to reduce consing. */
703 static Lisp_Object Vwith_echo_area_save_vector;
705 /* Non-zero means display_echo_area should display the last echo area
706 message again. Set by redisplay_preserve_echo_area. */
708 static int display_last_displayed_message_p;
710 /* Nonzero if echo area is being used by print; zero if being used by
711 message. */
713 int message_buf_print;
715 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
717 Lisp_Object Qinhibit_menubar_update;
718 int inhibit_menubar_update;
720 /* When evaluating expressions from menu bar items (enable conditions,
721 for instance), this is the frame they are being processed for. */
723 Lisp_Object Vmenu_updating_frame;
725 /* Maximum height for resizing mini-windows. Either a float
726 specifying a fraction of the available height, or an integer
727 specifying a number of lines. */
729 Lisp_Object Vmax_mini_window_height;
731 /* Non-zero means messages should be displayed with truncated
732 lines instead of being continued. */
734 int message_truncate_lines;
735 Lisp_Object Qmessage_truncate_lines;
737 /* Set to 1 in clear_message to make redisplay_internal aware
738 of an emptied echo area. */
740 static int message_cleared_p;
742 /* How to blink the default frame cursor off. */
743 Lisp_Object Vblink_cursor_alist;
745 /* A scratch glyph row with contents used for generating truncation
746 glyphs. Also used in direct_output_for_insert. */
748 #define MAX_SCRATCH_GLYPHS 100
749 struct glyph_row scratch_glyph_row;
750 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
752 /* Ascent and height of the last line processed by move_it_to. */
754 static int last_max_ascent, last_height;
756 /* Non-zero if there's a help-echo in the echo area. */
758 int help_echo_showing_p;
760 /* If >= 0, computed, exact values of mode-line and header-line height
761 to use in the macros CURRENT_MODE_LINE_HEIGHT and
762 CURRENT_HEADER_LINE_HEIGHT. */
764 int current_mode_line_height, current_header_line_height;
766 /* The maximum distance to look ahead for text properties. Values
767 that are too small let us call compute_char_face and similar
768 functions too often which is expensive. Values that are too large
769 let us call compute_char_face and alike too often because we
770 might not be interested in text properties that far away. */
772 #define TEXT_PROP_DISTANCE_LIMIT 100
774 #if GLYPH_DEBUG
776 /* Variables to turn off display optimizations from Lisp. */
778 int inhibit_try_window_id, inhibit_try_window_reusing;
779 int inhibit_try_cursor_movement;
781 /* Non-zero means print traces of redisplay if compiled with
782 GLYPH_DEBUG != 0. */
784 int trace_redisplay_p;
786 #endif /* GLYPH_DEBUG */
788 #ifdef DEBUG_TRACE_MOVE
789 /* Non-zero means trace with TRACE_MOVE to stderr. */
790 int trace_move;
792 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
793 #else
794 #define TRACE_MOVE(x) (void) 0
795 #endif
797 /* Non-zero means automatically scroll windows horizontally to make
798 point visible. */
800 int automatic_hscrolling_p;
801 Lisp_Object Qauto_hscroll_mode;
803 /* How close to the margin can point get before the window is scrolled
804 horizontally. */
805 EMACS_INT hscroll_margin;
807 /* How much to scroll horizontally when point is inside the above margin. */
808 Lisp_Object Vhscroll_step;
810 /* The variable `resize-mini-windows'. If nil, don't resize
811 mini-windows. If t, always resize them to fit the text they
812 display. If `grow-only', let mini-windows grow only until they
813 become empty. */
815 Lisp_Object Vresize_mini_windows;
817 /* Buffer being redisplayed -- for redisplay_window_error. */
819 struct buffer *displayed_buffer;
821 /* Space between overline and text. */
823 EMACS_INT overline_margin;
825 /* Require underline to be at least this many screen pixels below baseline
826 This to avoid underline "merging" with the base of letters at small
827 font sizes, particularly when x_use_underline_position_properties is on. */
829 EMACS_INT underline_minimum_offset;
831 /* Value returned from text property handlers (see below). */
833 enum prop_handled
835 HANDLED_NORMALLY,
836 HANDLED_RECOMPUTE_PROPS,
837 HANDLED_OVERLAY_STRING_CONSUMED,
838 HANDLED_RETURN
841 /* A description of text properties that redisplay is interested
842 in. */
844 struct props
846 /* The name of the property. */
847 Lisp_Object *name;
849 /* A unique index for the property. */
850 enum prop_idx idx;
852 /* A handler function called to set up iterator IT from the property
853 at IT's current position. Value is used to steer handle_stop. */
854 enum prop_handled (*handler) (struct it *it);
857 static enum prop_handled handle_face_prop (struct it *);
858 static enum prop_handled handle_invisible_prop (struct it *);
859 static enum prop_handled handle_display_prop (struct it *);
860 static enum prop_handled handle_composition_prop (struct it *);
861 static enum prop_handled handle_overlay_change (struct it *);
862 static enum prop_handled handle_fontified_prop (struct it *);
864 /* Properties handled by iterators. */
866 static struct props it_props[] =
868 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
869 /* Handle `face' before `display' because some sub-properties of
870 `display' need to know the face. */
871 {&Qface, FACE_PROP_IDX, handle_face_prop},
872 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
873 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
874 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
875 {NULL, 0, NULL}
878 /* Value is the position described by X. If X is a marker, value is
879 the marker_position of X. Otherwise, value is X. */
881 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
883 /* Enumeration returned by some move_it_.* functions internally. */
885 enum move_it_result
887 /* Not used. Undefined value. */
888 MOVE_UNDEFINED,
890 /* Move ended at the requested buffer position or ZV. */
891 MOVE_POS_MATCH_OR_ZV,
893 /* Move ended at the requested X pixel position. */
894 MOVE_X_REACHED,
896 /* Move within a line ended at the end of a line that must be
897 continued. */
898 MOVE_LINE_CONTINUED,
900 /* Move within a line ended at the end of a line that would
901 be displayed truncated. */
902 MOVE_LINE_TRUNCATED,
904 /* Move within a line ended at a line end. */
905 MOVE_NEWLINE_OR_CR
908 /* This counter is used to clear the face cache every once in a while
909 in redisplay_internal. It is incremented for each redisplay.
910 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
911 cleared. */
913 #define CLEAR_FACE_CACHE_COUNT 500
914 static int clear_face_cache_count;
916 /* Similarly for the image cache. */
918 #ifdef HAVE_WINDOW_SYSTEM
919 #define CLEAR_IMAGE_CACHE_COUNT 101
920 static int clear_image_cache_count;
922 /* Null glyph slice */
923 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
924 #endif
926 /* Non-zero while redisplay_internal is in progress. */
928 int redisplaying_p;
930 /* Non-zero means don't free realized faces. Bound while freeing
931 realized faces is dangerous because glyph matrices might still
932 reference them. */
934 int inhibit_free_realized_faces;
935 Lisp_Object Qinhibit_free_realized_faces;
937 /* If a string, XTread_socket generates an event to display that string.
938 (The display is done in read_char.) */
940 Lisp_Object help_echo_string;
941 Lisp_Object help_echo_window;
942 Lisp_Object help_echo_object;
943 EMACS_INT help_echo_pos;
945 /* Temporary variable for XTread_socket. */
947 Lisp_Object previous_help_echo_string;
949 /* Platform-independent portion of hourglass implementation. */
951 /* Non-zero means we're allowed to display a hourglass pointer. */
952 int display_hourglass_p;
954 /* Non-zero means an hourglass cursor is currently shown. */
955 int hourglass_shown_p;
957 /* If non-null, an asynchronous timer that, when it expires, displays
958 an hourglass cursor on all frames. */
959 struct atimer *hourglass_atimer;
961 /* Number of seconds to wait before displaying an hourglass cursor. */
962 Lisp_Object Vhourglass_delay;
964 /* Name of the face used to display glyphless characters. */
965 Lisp_Object Qglyphless_char;
967 /* Char-table to control the display of glyphless characters. */
968 Lisp_Object Vglyphless_char_display;
970 /* Symbol for the purpose of Vglyphless_char_display. */
971 Lisp_Object Qglyphless_char_display;
973 /* Method symbols for Vglyphless_char_display. */
974 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
976 /* Default pixel width of `thin-space' display method. */
977 #define THIN_SPACE_WIDTH 1
979 /* Default number of seconds to wait before displaying an hourglass
980 cursor. */
981 #define DEFAULT_HOURGLASS_DELAY 1
984 /* Function prototypes. */
986 static void setup_for_ellipsis (struct it *, int);
987 static void mark_window_display_accurate_1 (struct window *, int);
988 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
989 static int display_prop_string_p (Lisp_Object, Lisp_Object);
990 static int cursor_row_p (struct window *, struct glyph_row *);
991 static int redisplay_mode_lines (Lisp_Object, int);
992 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
994 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
996 static void handle_line_prefix (struct it *);
998 static void pint2str (char *, int, int);
999 static void pint2hrstr (char *, int, int);
1000 static struct text_pos run_window_scroll_functions (Lisp_Object,
1001 struct text_pos);
1002 static void reconsider_clip_changes (struct window *, struct buffer *);
1003 static int text_outside_line_unchanged_p (struct window *,
1004 EMACS_INT, EMACS_INT);
1005 static void store_mode_line_noprop_char (char);
1006 static int store_mode_line_noprop (const unsigned char *, int, int);
1007 static void handle_stop (struct it *);
1008 static void handle_stop_backwards (struct it *, EMACS_INT);
1009 static int single_display_spec_intangible_p (Lisp_Object);
1010 static void ensure_echo_area_buffers (void);
1011 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
1012 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
1013 static int with_echo_area_buffer (struct window *, int,
1014 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
1015 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1016 static void clear_garbaged_frames (void);
1017 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1018 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1019 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1020 static int display_echo_area (struct window *);
1021 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1022 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
1023 static Lisp_Object unwind_redisplay (Lisp_Object);
1024 static int string_char_and_length (const unsigned char *, int *);
1025 static struct text_pos display_prop_end (struct it *, Lisp_Object,
1026 struct text_pos);
1027 static int compute_window_start_on_continuation_line (struct window *);
1028 static Lisp_Object safe_eval_handler (Lisp_Object);
1029 static void insert_left_trunc_glyphs (struct it *);
1030 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
1031 Lisp_Object);
1032 static void extend_face_to_end_of_line (struct it *);
1033 static int append_space_for_newline (struct it *, int);
1034 static int cursor_row_fully_visible_p (struct window *, int, int);
1035 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
1036 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
1037 static int trailing_whitespace_p (EMACS_INT);
1038 static int message_log_check_duplicate (EMACS_INT, EMACS_INT,
1039 EMACS_INT, EMACS_INT);
1040 static void push_it (struct it *);
1041 static void pop_it (struct it *);
1042 static void sync_frame_with_window_matrix_rows (struct window *);
1043 static void select_frame_for_redisplay (Lisp_Object);
1044 static void redisplay_internal (int);
1045 static int echo_area_display (int);
1046 static void redisplay_windows (Lisp_Object);
1047 static void redisplay_window (Lisp_Object, int);
1048 static Lisp_Object redisplay_window_error (Lisp_Object);
1049 static Lisp_Object redisplay_window_0 (Lisp_Object);
1050 static Lisp_Object redisplay_window_1 (Lisp_Object);
1051 static int update_menu_bar (struct frame *, int, int);
1052 static int try_window_reusing_current_matrix (struct window *);
1053 static int try_window_id (struct window *);
1054 static int display_line (struct it *);
1055 static int display_mode_lines (struct window *);
1056 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
1057 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
1058 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
1059 static const char *decode_mode_spec (struct window *, int, int, int,
1060 Lisp_Object *);
1061 static void display_menu_bar (struct window *);
1062 static int display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT, int,
1063 EMACS_INT *);
1064 static int display_string (const unsigned char *, Lisp_Object, Lisp_Object,
1065 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
1066 static void compute_line_metrics (struct it *);
1067 static void run_redisplay_end_trigger_hook (struct it *);
1068 static int get_overlay_strings (struct it *, EMACS_INT);
1069 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
1070 static void next_overlay_string (struct it *);
1071 static void reseat (struct it *, struct text_pos, int);
1072 static void reseat_1 (struct it *, struct text_pos, int);
1073 static void back_to_previous_visible_line_start (struct it *);
1074 void reseat_at_previous_visible_line_start (struct it *);
1075 static void reseat_at_next_visible_line_start (struct it *, int);
1076 static int next_element_from_ellipsis (struct it *);
1077 static int next_element_from_display_vector (struct it *);
1078 static int next_element_from_string (struct it *);
1079 static int next_element_from_c_string (struct it *);
1080 static int next_element_from_buffer (struct it *);
1081 static int next_element_from_composition (struct it *);
1082 static int next_element_from_image (struct it *);
1083 static int next_element_from_stretch (struct it *);
1084 static void load_overlay_strings (struct it *, EMACS_INT);
1085 static int init_from_display_pos (struct it *, struct window *,
1086 struct display_pos *);
1087 static void reseat_to_string (struct it *, const unsigned char *,
1088 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
1089 static enum move_it_result
1090 move_it_in_display_line_to (struct it *, EMACS_INT, int,
1091 enum move_operation_enum);
1092 void move_it_vertically_backward (struct it *, int);
1093 static void init_to_row_start (struct it *, struct window *,
1094 struct glyph_row *);
1095 static int init_to_row_end (struct it *, struct window *,
1096 struct glyph_row *);
1097 static void back_to_previous_line_start (struct it *);
1098 static int forward_to_next_line_start (struct it *, int *);
1099 static struct text_pos string_pos_nchars_ahead (struct text_pos,
1100 Lisp_Object, EMACS_INT);
1101 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
1102 static struct text_pos c_string_pos (EMACS_INT, const unsigned char *, int);
1103 static EMACS_INT number_of_chars (const unsigned char *, int);
1104 static void compute_stop_pos (struct it *);
1105 static void compute_string_pos (struct text_pos *, struct text_pos,
1106 Lisp_Object);
1107 static int face_before_or_after_it_pos (struct it *, int);
1108 static EMACS_INT next_overlay_change (EMACS_INT);
1109 static int handle_single_display_spec (struct it *, Lisp_Object,
1110 Lisp_Object, Lisp_Object,
1111 struct text_pos *, int);
1112 static int underlying_face_id (struct it *);
1113 static int in_ellipses_for_invisible_text_p (struct display_pos *,
1114 struct window *);
1116 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1117 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1119 #ifdef HAVE_WINDOW_SYSTEM
1121 static void x_consider_frame_title (Lisp_Object);
1122 static int tool_bar_lines_needed (struct frame *, int *);
1123 static void update_tool_bar (struct frame *, int);
1124 static void build_desired_tool_bar_string (struct frame *f);
1125 static int redisplay_tool_bar (struct frame *);
1126 static void display_tool_bar_line (struct it *, int);
1127 static void notice_overwritten_cursor (struct window *,
1128 enum glyph_row_area,
1129 int, int, int, int);
1130 static void append_stretch_glyph (struct it *, Lisp_Object,
1131 int, int, int);
1134 #endif /* HAVE_WINDOW_SYSTEM */
1136 static int coords_in_mouse_face_p (struct window *, int, int);
1140 /***********************************************************************
1141 Window display dimensions
1142 ***********************************************************************/
1144 /* Return the bottom boundary y-position for text lines in window W.
1145 This is the first y position at which a line cannot start.
1146 It is relative to the top of the window.
1148 This is the height of W minus the height of a mode line, if any. */
1150 INLINE int
1151 window_text_bottom_y (struct window *w)
1153 int height = WINDOW_TOTAL_HEIGHT (w);
1155 if (WINDOW_WANTS_MODELINE_P (w))
1156 height -= CURRENT_MODE_LINE_HEIGHT (w);
1157 return height;
1160 /* Return the pixel width of display area AREA of window W. AREA < 0
1161 means return the total width of W, not including fringes to
1162 the left and right of the window. */
1164 INLINE int
1165 window_box_width (struct window *w, int area)
1167 int cols = XFASTINT (w->total_cols);
1168 int pixels = 0;
1170 if (!w->pseudo_window_p)
1172 cols -= WINDOW_SCROLL_BAR_COLS (w);
1174 if (area == TEXT_AREA)
1176 if (INTEGERP (w->left_margin_cols))
1177 cols -= XFASTINT (w->left_margin_cols);
1178 if (INTEGERP (w->right_margin_cols))
1179 cols -= XFASTINT (w->right_margin_cols);
1180 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1182 else if (area == LEFT_MARGIN_AREA)
1184 cols = (INTEGERP (w->left_margin_cols)
1185 ? XFASTINT (w->left_margin_cols) : 0);
1186 pixels = 0;
1188 else if (area == RIGHT_MARGIN_AREA)
1190 cols = (INTEGERP (w->right_margin_cols)
1191 ? XFASTINT (w->right_margin_cols) : 0);
1192 pixels = 0;
1196 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1200 /* Return the pixel height of the display area of window W, not
1201 including mode lines of W, if any. */
1203 INLINE int
1204 window_box_height (struct window *w)
1206 struct frame *f = XFRAME (w->frame);
1207 int height = WINDOW_TOTAL_HEIGHT (w);
1209 xassert (height >= 0);
1211 /* Note: the code below that determines the mode-line/header-line
1212 height is essentially the same as that contained in the macro
1213 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1214 the appropriate glyph row has its `mode_line_p' flag set,
1215 and if it doesn't, uses estimate_mode_line_height instead. */
1217 if (WINDOW_WANTS_MODELINE_P (w))
1219 struct glyph_row *ml_row
1220 = (w->current_matrix && w->current_matrix->rows
1221 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1222 : 0);
1223 if (ml_row && ml_row->mode_line_p)
1224 height -= ml_row->height;
1225 else
1226 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1229 if (WINDOW_WANTS_HEADER_LINE_P (w))
1231 struct glyph_row *hl_row
1232 = (w->current_matrix && w->current_matrix->rows
1233 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1234 : 0);
1235 if (hl_row && hl_row->mode_line_p)
1236 height -= hl_row->height;
1237 else
1238 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1241 /* With a very small font and a mode-line that's taller than
1242 default, we might end up with a negative height. */
1243 return max (0, height);
1246 /* Return the window-relative coordinate of the left edge of display
1247 area AREA of window W. AREA < 0 means return the left edge of the
1248 whole window, to the right of the left fringe of W. */
1250 INLINE int
1251 window_box_left_offset (struct window *w, int area)
1253 int x;
1255 if (w->pseudo_window_p)
1256 return 0;
1258 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1260 if (area == TEXT_AREA)
1261 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1262 + window_box_width (w, LEFT_MARGIN_AREA));
1263 else if (area == RIGHT_MARGIN_AREA)
1264 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1265 + window_box_width (w, LEFT_MARGIN_AREA)
1266 + window_box_width (w, TEXT_AREA)
1267 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1269 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1270 else if (area == LEFT_MARGIN_AREA
1271 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1272 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1274 return x;
1278 /* Return the window-relative coordinate of the right edge of display
1279 area AREA of window W. AREA < 0 means return the right edge of the
1280 whole window, to the left of the right fringe of W. */
1282 INLINE int
1283 window_box_right_offset (struct window *w, int area)
1285 return window_box_left_offset (w, area) + window_box_width (w, area);
1288 /* Return the frame-relative coordinate of the left edge of display
1289 area AREA of window W. AREA < 0 means return the left edge of the
1290 whole window, to the right of the left fringe of W. */
1292 INLINE int
1293 window_box_left (struct window *w, int area)
1295 struct frame *f = XFRAME (w->frame);
1296 int x;
1298 if (w->pseudo_window_p)
1299 return FRAME_INTERNAL_BORDER_WIDTH (f);
1301 x = (WINDOW_LEFT_EDGE_X (w)
1302 + window_box_left_offset (w, area));
1304 return x;
1308 /* Return the frame-relative coordinate of the right edge of display
1309 area AREA of window W. AREA < 0 means return the right edge of the
1310 whole window, to the left of the right fringe of W. */
1312 INLINE int
1313 window_box_right (struct window *w, int area)
1315 return window_box_left (w, area) + window_box_width (w, area);
1318 /* Get the bounding box of the display area AREA of window W, without
1319 mode lines, in frame-relative coordinates. AREA < 0 means the
1320 whole window, not including the left and right fringes of
1321 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1322 coordinates of the upper-left corner of the box. Return in
1323 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1325 INLINE void
1326 window_box (struct window *w, int area, int *box_x, int *box_y,
1327 int *box_width, int *box_height)
1329 if (box_width)
1330 *box_width = window_box_width (w, area);
1331 if (box_height)
1332 *box_height = window_box_height (w);
1333 if (box_x)
1334 *box_x = window_box_left (w, area);
1335 if (box_y)
1337 *box_y = WINDOW_TOP_EDGE_Y (w);
1338 if (WINDOW_WANTS_HEADER_LINE_P (w))
1339 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1344 /* Get the bounding box of the display area AREA of window W, without
1345 mode lines. AREA < 0 means the whole window, not including the
1346 left and right fringe of the window. Return in *TOP_LEFT_X
1347 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1348 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1349 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1350 box. */
1352 INLINE void
1353 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1354 int *bottom_right_x, int *bottom_right_y)
1356 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1357 bottom_right_y);
1358 *bottom_right_x += *top_left_x;
1359 *bottom_right_y += *top_left_y;
1364 /***********************************************************************
1365 Utilities
1366 ***********************************************************************/
1368 /* Return the bottom y-position of the line the iterator IT is in.
1369 This can modify IT's settings. */
1372 line_bottom_y (struct it *it)
1374 int line_height = it->max_ascent + it->max_descent;
1375 int line_top_y = it->current_y;
1377 if (line_height == 0)
1379 if (last_height)
1380 line_height = last_height;
1381 else if (IT_CHARPOS (*it) < ZV)
1383 move_it_by_lines (it, 1, 1);
1384 line_height = (it->max_ascent || it->max_descent
1385 ? it->max_ascent + it->max_descent
1386 : last_height);
1388 else
1390 struct glyph_row *row = it->glyph_row;
1392 /* Use the default character height. */
1393 it->glyph_row = NULL;
1394 it->what = IT_CHARACTER;
1395 it->c = ' ';
1396 it->len = 1;
1397 PRODUCE_GLYPHS (it);
1398 line_height = it->ascent + it->descent;
1399 it->glyph_row = row;
1403 return line_top_y + line_height;
1407 /* Return 1 if position CHARPOS is visible in window W.
1408 CHARPOS < 0 means return info about WINDOW_END position.
1409 If visible, set *X and *Y to pixel coordinates of top left corner.
1410 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1411 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1414 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1415 int *rtop, int *rbot, int *rowh, int *vpos)
1417 struct it it;
1418 struct text_pos top;
1419 int visible_p = 0;
1420 struct buffer *old_buffer = NULL;
1422 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1423 return visible_p;
1425 if (XBUFFER (w->buffer) != current_buffer)
1427 old_buffer = current_buffer;
1428 set_buffer_internal_1 (XBUFFER (w->buffer));
1431 SET_TEXT_POS_FROM_MARKER (top, w->start);
1433 /* Compute exact mode line heights. */
1434 if (WINDOW_WANTS_MODELINE_P (w))
1435 current_mode_line_height
1436 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1437 current_buffer->mode_line_format);
1439 if (WINDOW_WANTS_HEADER_LINE_P (w))
1440 current_header_line_height
1441 = display_mode_line (w, HEADER_LINE_FACE_ID,
1442 current_buffer->header_line_format);
1444 start_display (&it, w, top);
1445 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1446 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1448 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1450 /* We have reached CHARPOS, or passed it. How the call to
1451 move_it_to can overshoot: (i) If CHARPOS is on invisible
1452 text, move_it_to stops at the end of the invisible text,
1453 after CHARPOS. (ii) If CHARPOS is in a display vector,
1454 move_it_to stops on its last glyph. */
1455 int top_x = it.current_x;
1456 int top_y = it.current_y;
1457 enum it_method it_method = it.method;
1458 /* Calling line_bottom_y may change it.method, it.position, etc. */
1459 int bottom_y = (last_height = 0, line_bottom_y (&it));
1460 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1462 if (top_y < window_top_y)
1463 visible_p = bottom_y > window_top_y;
1464 else if (top_y < it.last_visible_y)
1465 visible_p = 1;
1466 if (visible_p)
1468 if (it_method == GET_FROM_DISPLAY_VECTOR)
1470 /* We stopped on the last glyph of a display vector.
1471 Try and recompute. Hack alert! */
1472 if (charpos < 2 || top.charpos >= charpos)
1473 top_x = it.glyph_row->x;
1474 else
1476 struct it it2;
1477 start_display (&it2, w, top);
1478 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1479 get_next_display_element (&it2);
1480 PRODUCE_GLYPHS (&it2);
1481 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1482 || it2.current_x > it2.last_visible_x)
1483 top_x = it.glyph_row->x;
1484 else
1486 top_x = it2.current_x;
1487 top_y = it2.current_y;
1492 *x = top_x;
1493 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1494 *rtop = max (0, window_top_y - top_y);
1495 *rbot = max (0, bottom_y - it.last_visible_y);
1496 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1497 - max (top_y, window_top_y)));
1498 *vpos = it.vpos;
1501 else
1503 struct it it2;
1505 it2 = it;
1506 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1507 move_it_by_lines (&it, 1, 0);
1508 if (charpos < IT_CHARPOS (it)
1509 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1511 visible_p = 1;
1512 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1513 *x = it2.current_x;
1514 *y = it2.current_y + it2.max_ascent - it2.ascent;
1515 *rtop = max (0, -it2.current_y);
1516 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1517 - it.last_visible_y));
1518 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1519 it.last_visible_y)
1520 - max (it2.current_y,
1521 WINDOW_HEADER_LINE_HEIGHT (w))));
1522 *vpos = it2.vpos;
1526 if (old_buffer)
1527 set_buffer_internal_1 (old_buffer);
1529 current_header_line_height = current_mode_line_height = -1;
1531 if (visible_p && XFASTINT (w->hscroll) > 0)
1532 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1534 #if 0
1535 /* Debugging code. */
1536 if (visible_p)
1537 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1538 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1539 else
1540 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1541 #endif
1543 return visible_p;
1547 /* Return the next character from STR which is MAXLEN bytes long.
1548 Return in *LEN the length of the character. This is like
1549 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1550 we find one, we return a `?', but with the length of the invalid
1551 character. */
1553 static INLINE int
1554 string_char_and_length (const unsigned char *str, int *len)
1556 int c;
1558 c = STRING_CHAR_AND_LENGTH (str, *len);
1559 if (!CHAR_VALID_P (c, 1))
1560 /* We may not change the length here because other places in Emacs
1561 don't use this function, i.e. they silently accept invalid
1562 characters. */
1563 c = '?';
1565 return c;
1570 /* Given a position POS containing a valid character and byte position
1571 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1573 static struct text_pos
1574 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1576 xassert (STRINGP (string) && nchars >= 0);
1578 if (STRING_MULTIBYTE (string))
1580 EMACS_INT rest = SBYTES (string) - BYTEPOS (pos);
1581 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1582 int len;
1584 while (nchars--)
1586 string_char_and_length (p, &len);
1587 p += len, rest -= len;
1588 xassert (rest >= 0);
1589 CHARPOS (pos) += 1;
1590 BYTEPOS (pos) += len;
1593 else
1594 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1596 return pos;
1600 /* Value is the text position, i.e. character and byte position,
1601 for character position CHARPOS in STRING. */
1603 static INLINE struct text_pos
1604 string_pos (EMACS_INT charpos, Lisp_Object string)
1606 struct text_pos pos;
1607 xassert (STRINGP (string));
1608 xassert (charpos >= 0);
1609 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1610 return pos;
1614 /* Value is a text position, i.e. character and byte position, for
1615 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1616 means recognize multibyte characters. */
1618 static struct text_pos
1619 c_string_pos (EMACS_INT charpos, const unsigned char *s, int multibyte_p)
1621 struct text_pos pos;
1623 xassert (s != NULL);
1624 xassert (charpos >= 0);
1626 if (multibyte_p)
1628 EMACS_INT rest = strlen (s);
1629 int len;
1631 SET_TEXT_POS (pos, 0, 0);
1632 while (charpos--)
1634 string_char_and_length (s, &len);
1635 s += len, rest -= len;
1636 xassert (rest >= 0);
1637 CHARPOS (pos) += 1;
1638 BYTEPOS (pos) += len;
1641 else
1642 SET_TEXT_POS (pos, charpos, charpos);
1644 return pos;
1648 /* Value is the number of characters in C string S. MULTIBYTE_P
1649 non-zero means recognize multibyte characters. */
1651 static EMACS_INT
1652 number_of_chars (const unsigned char *s, int multibyte_p)
1654 EMACS_INT nchars;
1656 if (multibyte_p)
1658 EMACS_INT rest = strlen (s);
1659 int len;
1660 unsigned char *p = (unsigned char *) s;
1662 for (nchars = 0; rest > 0; ++nchars)
1664 string_char_and_length (p, &len);
1665 rest -= len, p += len;
1668 else
1669 nchars = strlen (s);
1671 return nchars;
1675 /* Compute byte position NEWPOS->bytepos corresponding to
1676 NEWPOS->charpos. POS is a known position in string STRING.
1677 NEWPOS->charpos must be >= POS.charpos. */
1679 static void
1680 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1682 xassert (STRINGP (string));
1683 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1685 if (STRING_MULTIBYTE (string))
1686 *newpos = string_pos_nchars_ahead (pos, string,
1687 CHARPOS (*newpos) - CHARPOS (pos));
1688 else
1689 BYTEPOS (*newpos) = CHARPOS (*newpos);
1692 /* EXPORT:
1693 Return an estimation of the pixel height of mode or header lines on
1694 frame F. FACE_ID specifies what line's height to estimate. */
1697 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1699 #ifdef HAVE_WINDOW_SYSTEM
1700 if (FRAME_WINDOW_P (f))
1702 int height = FONT_HEIGHT (FRAME_FONT (f));
1704 /* This function is called so early when Emacs starts that the face
1705 cache and mode line face are not yet initialized. */
1706 if (FRAME_FACE_CACHE (f))
1708 struct face *face = FACE_FROM_ID (f, face_id);
1709 if (face)
1711 if (face->font)
1712 height = FONT_HEIGHT (face->font);
1713 if (face->box_line_width > 0)
1714 height += 2 * face->box_line_width;
1718 return height;
1720 #endif
1722 return 1;
1725 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1726 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1727 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1728 not force the value into range. */
1730 void
1731 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1732 int *x, int *y, NativeRectangle *bounds, int noclip)
1735 #ifdef HAVE_WINDOW_SYSTEM
1736 if (FRAME_WINDOW_P (f))
1738 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1739 even for negative values. */
1740 if (pix_x < 0)
1741 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1742 if (pix_y < 0)
1743 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1745 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1746 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1748 if (bounds)
1749 STORE_NATIVE_RECT (*bounds,
1750 FRAME_COL_TO_PIXEL_X (f, pix_x),
1751 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1752 FRAME_COLUMN_WIDTH (f) - 1,
1753 FRAME_LINE_HEIGHT (f) - 1);
1755 if (!noclip)
1757 if (pix_x < 0)
1758 pix_x = 0;
1759 else if (pix_x > FRAME_TOTAL_COLS (f))
1760 pix_x = FRAME_TOTAL_COLS (f);
1762 if (pix_y < 0)
1763 pix_y = 0;
1764 else if (pix_y > FRAME_LINES (f))
1765 pix_y = FRAME_LINES (f);
1768 #endif
1770 *x = pix_x;
1771 *y = pix_y;
1775 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1776 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1777 can't tell the positions because W's display is not up to date,
1778 return 0. */
1781 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1782 int *frame_x, int *frame_y)
1784 #ifdef HAVE_WINDOW_SYSTEM
1785 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1787 int success_p;
1789 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1790 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1792 if (display_completed)
1794 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1795 struct glyph *glyph = row->glyphs[TEXT_AREA];
1796 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1798 hpos = row->x;
1799 vpos = row->y;
1800 while (glyph < end)
1802 hpos += glyph->pixel_width;
1803 ++glyph;
1806 /* If first glyph is partially visible, its first visible position is still 0. */
1807 if (hpos < 0)
1808 hpos = 0;
1810 success_p = 1;
1812 else
1814 hpos = vpos = 0;
1815 success_p = 0;
1818 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1819 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1820 return success_p;
1822 #endif
1824 *frame_x = hpos;
1825 *frame_y = vpos;
1826 return 1;
1830 /* Find the glyph under window-relative coordinates X/Y in window W.
1831 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1832 strings. Return in *HPOS and *VPOS the row and column number of
1833 the glyph found. Return in *AREA the glyph area containing X.
1834 Value is a pointer to the glyph found or null if X/Y is not on
1835 text, or we can't tell because W's current matrix is not up to
1836 date. */
1838 static
1839 struct glyph *
1840 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1841 int *dx, int *dy, int *area)
1843 struct glyph *glyph, *end;
1844 struct glyph_row *row = NULL;
1845 int x0, i;
1847 /* Find row containing Y. Give up if some row is not enabled. */
1848 for (i = 0; i < w->current_matrix->nrows; ++i)
1850 row = MATRIX_ROW (w->current_matrix, i);
1851 if (!row->enabled_p)
1852 return NULL;
1853 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1854 break;
1857 *vpos = i;
1858 *hpos = 0;
1860 /* Give up if Y is not in the window. */
1861 if (i == w->current_matrix->nrows)
1862 return NULL;
1864 /* Get the glyph area containing X. */
1865 if (w->pseudo_window_p)
1867 *area = TEXT_AREA;
1868 x0 = 0;
1870 else
1872 if (x < window_box_left_offset (w, TEXT_AREA))
1874 *area = LEFT_MARGIN_AREA;
1875 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1877 else if (x < window_box_right_offset (w, TEXT_AREA))
1879 *area = TEXT_AREA;
1880 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1882 else
1884 *area = RIGHT_MARGIN_AREA;
1885 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1889 /* Find glyph containing X. */
1890 glyph = row->glyphs[*area];
1891 end = glyph + row->used[*area];
1892 x -= x0;
1893 while (glyph < end && x >= glyph->pixel_width)
1895 x -= glyph->pixel_width;
1896 ++glyph;
1899 if (glyph == end)
1900 return NULL;
1902 if (dx)
1904 *dx = x;
1905 *dy = y - (row->y + row->ascent - glyph->ascent);
1908 *hpos = glyph - row->glyphs[*area];
1909 return glyph;
1912 /* EXPORT:
1913 Convert frame-relative x/y to coordinates relative to window W.
1914 Takes pseudo-windows into account. */
1916 void
1917 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1919 if (w->pseudo_window_p)
1921 /* A pseudo-window is always full-width, and starts at the
1922 left edge of the frame, plus a frame border. */
1923 struct frame *f = XFRAME (w->frame);
1924 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1925 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1927 else
1929 *x -= WINDOW_LEFT_EDGE_X (w);
1930 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1934 #ifdef HAVE_WINDOW_SYSTEM
1936 /* EXPORT:
1937 Return in RECTS[] at most N clipping rectangles for glyph string S.
1938 Return the number of stored rectangles. */
1941 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1943 XRectangle r;
1945 if (n <= 0)
1946 return 0;
1948 if (s->row->full_width_p)
1950 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1951 r.x = WINDOW_LEFT_EDGE_X (s->w);
1952 r.width = WINDOW_TOTAL_WIDTH (s->w);
1954 /* Unless displaying a mode or menu bar line, which are always
1955 fully visible, clip to the visible part of the row. */
1956 if (s->w->pseudo_window_p)
1957 r.height = s->row->visible_height;
1958 else
1959 r.height = s->height;
1961 else
1963 /* This is a text line that may be partially visible. */
1964 r.x = window_box_left (s->w, s->area);
1965 r.width = window_box_width (s->w, s->area);
1966 r.height = s->row->visible_height;
1969 if (s->clip_head)
1970 if (r.x < s->clip_head->x)
1972 if (r.width >= s->clip_head->x - r.x)
1973 r.width -= s->clip_head->x - r.x;
1974 else
1975 r.width = 0;
1976 r.x = s->clip_head->x;
1978 if (s->clip_tail)
1979 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1981 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1982 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1983 else
1984 r.width = 0;
1987 /* If S draws overlapping rows, it's sufficient to use the top and
1988 bottom of the window for clipping because this glyph string
1989 intentionally draws over other lines. */
1990 if (s->for_overlaps)
1992 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1993 r.height = window_text_bottom_y (s->w) - r.y;
1995 /* Alas, the above simple strategy does not work for the
1996 environments with anti-aliased text: if the same text is
1997 drawn onto the same place multiple times, it gets thicker.
1998 If the overlap we are processing is for the erased cursor, we
1999 take the intersection with the rectagle of the cursor. */
2000 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2002 XRectangle rc, r_save = r;
2004 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2005 rc.y = s->w->phys_cursor.y;
2006 rc.width = s->w->phys_cursor_width;
2007 rc.height = s->w->phys_cursor_height;
2009 x_intersect_rectangles (&r_save, &rc, &r);
2012 else
2014 /* Don't use S->y for clipping because it doesn't take partially
2015 visible lines into account. For example, it can be negative for
2016 partially visible lines at the top of a window. */
2017 if (!s->row->full_width_p
2018 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2019 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2020 else
2021 r.y = max (0, s->row->y);
2024 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2026 /* If drawing the cursor, don't let glyph draw outside its
2027 advertised boundaries. Cleartype does this under some circumstances. */
2028 if (s->hl == DRAW_CURSOR)
2030 struct glyph *glyph = s->first_glyph;
2031 int height, max_y;
2033 if (s->x > r.x)
2035 r.width -= s->x - r.x;
2036 r.x = s->x;
2038 r.width = min (r.width, glyph->pixel_width);
2040 /* If r.y is below window bottom, ensure that we still see a cursor. */
2041 height = min (glyph->ascent + glyph->descent,
2042 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2043 max_y = window_text_bottom_y (s->w) - height;
2044 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2045 if (s->ybase - glyph->ascent > max_y)
2047 r.y = max_y;
2048 r.height = height;
2050 else
2052 /* Don't draw cursor glyph taller than our actual glyph. */
2053 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2054 if (height < r.height)
2056 max_y = r.y + r.height;
2057 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2058 r.height = min (max_y - r.y, height);
2063 if (s->row->clip)
2065 XRectangle r_save = r;
2067 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2068 r.width = 0;
2071 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2072 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2074 #ifdef CONVERT_FROM_XRECT
2075 CONVERT_FROM_XRECT (r, *rects);
2076 #else
2077 *rects = r;
2078 #endif
2079 return 1;
2081 else
2083 /* If we are processing overlapping and allowed to return
2084 multiple clipping rectangles, we exclude the row of the glyph
2085 string from the clipping rectangle. This is to avoid drawing
2086 the same text on the environment with anti-aliasing. */
2087 #ifdef CONVERT_FROM_XRECT
2088 XRectangle rs[2];
2089 #else
2090 XRectangle *rs = rects;
2091 #endif
2092 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2094 if (s->for_overlaps & OVERLAPS_PRED)
2096 rs[i] = r;
2097 if (r.y + r.height > row_y)
2099 if (r.y < row_y)
2100 rs[i].height = row_y - r.y;
2101 else
2102 rs[i].height = 0;
2104 i++;
2106 if (s->for_overlaps & OVERLAPS_SUCC)
2108 rs[i] = r;
2109 if (r.y < row_y + s->row->visible_height)
2111 if (r.y + r.height > row_y + s->row->visible_height)
2113 rs[i].y = row_y + s->row->visible_height;
2114 rs[i].height = r.y + r.height - rs[i].y;
2116 else
2117 rs[i].height = 0;
2119 i++;
2122 n = i;
2123 #ifdef CONVERT_FROM_XRECT
2124 for (i = 0; i < n; i++)
2125 CONVERT_FROM_XRECT (rs[i], rects[i]);
2126 #endif
2127 return n;
2131 /* EXPORT:
2132 Return in *NR the clipping rectangle for glyph string S. */
2134 void
2135 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2137 get_glyph_string_clip_rects (s, nr, 1);
2141 /* EXPORT:
2142 Return the position and height of the phys cursor in window W.
2143 Set w->phys_cursor_width to width of phys cursor.
2146 void
2147 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2148 struct glyph *glyph, int *xp, int *yp, int *heightp)
2150 struct frame *f = XFRAME (WINDOW_FRAME (w));
2151 int x, y, wd, h, h0, y0;
2153 /* Compute the width of the rectangle to draw. If on a stretch
2154 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2155 rectangle as wide as the glyph, but use a canonical character
2156 width instead. */
2157 wd = glyph->pixel_width - 1;
2158 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2159 wd++; /* Why? */
2160 #endif
2162 x = w->phys_cursor.x;
2163 if (x < 0)
2165 wd += x;
2166 x = 0;
2169 if (glyph->type == STRETCH_GLYPH
2170 && !x_stretch_cursor_p)
2171 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2172 w->phys_cursor_width = wd;
2174 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2176 /* If y is below window bottom, ensure that we still see a cursor. */
2177 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2179 h = max (h0, glyph->ascent + glyph->descent);
2180 h0 = min (h0, glyph->ascent + glyph->descent);
2182 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2183 if (y < y0)
2185 h = max (h - (y0 - y) + 1, h0);
2186 y = y0 - 1;
2188 else
2190 y0 = window_text_bottom_y (w) - h0;
2191 if (y > y0)
2193 h += y - y0;
2194 y = y0;
2198 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2199 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2200 *heightp = h;
2204 * Remember which glyph the mouse is over.
2207 void
2208 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2210 Lisp_Object window;
2211 struct window *w;
2212 struct glyph_row *r, *gr, *end_row;
2213 enum window_part part;
2214 enum glyph_row_area area;
2215 int x, y, width, height;
2217 /* Try to determine frame pixel position and size of the glyph under
2218 frame pixel coordinates X/Y on frame F. */
2220 if (!f->glyphs_initialized_p
2221 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2222 NILP (window)))
2224 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2225 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2226 goto virtual_glyph;
2229 w = XWINDOW (window);
2230 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2231 height = WINDOW_FRAME_LINE_HEIGHT (w);
2233 x = window_relative_x_coord (w, part, gx);
2234 y = gy - WINDOW_TOP_EDGE_Y (w);
2236 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2237 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2239 if (w->pseudo_window_p)
2241 area = TEXT_AREA;
2242 part = ON_MODE_LINE; /* Don't adjust margin. */
2243 goto text_glyph;
2246 switch (part)
2248 case ON_LEFT_MARGIN:
2249 area = LEFT_MARGIN_AREA;
2250 goto text_glyph;
2252 case ON_RIGHT_MARGIN:
2253 area = RIGHT_MARGIN_AREA;
2254 goto text_glyph;
2256 case ON_HEADER_LINE:
2257 case ON_MODE_LINE:
2258 gr = (part == ON_HEADER_LINE
2259 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2260 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2261 gy = gr->y;
2262 area = TEXT_AREA;
2263 goto text_glyph_row_found;
2265 case ON_TEXT:
2266 area = TEXT_AREA;
2268 text_glyph:
2269 gr = 0; gy = 0;
2270 for (; r <= end_row && r->enabled_p; ++r)
2271 if (r->y + r->height > y)
2273 gr = r; gy = r->y;
2274 break;
2277 text_glyph_row_found:
2278 if (gr && gy <= y)
2280 struct glyph *g = gr->glyphs[area];
2281 struct glyph *end = g + gr->used[area];
2283 height = gr->height;
2284 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2285 if (gx + g->pixel_width > x)
2286 break;
2288 if (g < end)
2290 if (g->type == IMAGE_GLYPH)
2292 /* Don't remember when mouse is over image, as
2293 image may have hot-spots. */
2294 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2295 return;
2297 width = g->pixel_width;
2299 else
2301 /* Use nominal char spacing at end of line. */
2302 x -= gx;
2303 gx += (x / width) * width;
2306 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2307 gx += window_box_left_offset (w, area);
2309 else
2311 /* Use nominal line height at end of window. */
2312 gx = (x / width) * width;
2313 y -= gy;
2314 gy += (y / height) * height;
2316 break;
2318 case ON_LEFT_FRINGE:
2319 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2320 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2321 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2322 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2323 goto row_glyph;
2325 case ON_RIGHT_FRINGE:
2326 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2327 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2328 : window_box_right_offset (w, TEXT_AREA));
2329 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2330 goto row_glyph;
2332 case ON_SCROLL_BAR:
2333 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2335 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2336 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2337 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2338 : 0)));
2339 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2341 row_glyph:
2342 gr = 0, gy = 0;
2343 for (; r <= end_row && r->enabled_p; ++r)
2344 if (r->y + r->height > y)
2346 gr = r; gy = r->y;
2347 break;
2350 if (gr && gy <= y)
2351 height = gr->height;
2352 else
2354 /* Use nominal line height at end of window. */
2355 y -= gy;
2356 gy += (y / height) * height;
2358 break;
2360 default:
2362 virtual_glyph:
2363 /* If there is no glyph under the mouse, then we divide the screen
2364 into a grid of the smallest glyph in the frame, and use that
2365 as our "glyph". */
2367 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2368 round down even for negative values. */
2369 if (gx < 0)
2370 gx -= width - 1;
2371 if (gy < 0)
2372 gy -= height - 1;
2374 gx = (gx / width) * width;
2375 gy = (gy / height) * height;
2377 goto store_rect;
2380 gx += WINDOW_LEFT_EDGE_X (w);
2381 gy += WINDOW_TOP_EDGE_Y (w);
2383 store_rect:
2384 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2386 /* Visible feedback for debugging. */
2387 #if 0
2388 #if HAVE_X_WINDOWS
2389 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2390 f->output_data.x->normal_gc,
2391 gx, gy, width, height);
2392 #endif
2393 #endif
2397 #endif /* HAVE_WINDOW_SYSTEM */
2400 /***********************************************************************
2401 Lisp form evaluation
2402 ***********************************************************************/
2404 /* Error handler for safe_eval and safe_call. */
2406 static Lisp_Object
2407 safe_eval_handler (Lisp_Object arg)
2409 add_to_log ("Error during redisplay: %s", arg, Qnil);
2410 return Qnil;
2414 /* Evaluate SEXPR and return the result, or nil if something went
2415 wrong. Prevent redisplay during the evaluation. */
2417 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2418 Return the result, or nil if something went wrong. Prevent
2419 redisplay during the evaluation. */
2421 Lisp_Object
2422 safe_call (int nargs, Lisp_Object *args)
2424 Lisp_Object val;
2426 if (inhibit_eval_during_redisplay)
2427 val = Qnil;
2428 else
2430 int count = SPECPDL_INDEX ();
2431 struct gcpro gcpro1;
2433 GCPRO1 (args[0]);
2434 gcpro1.nvars = nargs;
2435 specbind (Qinhibit_redisplay, Qt);
2436 /* Use Qt to ensure debugger does not run,
2437 so there is no possibility of wanting to redisplay. */
2438 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2439 safe_eval_handler);
2440 UNGCPRO;
2441 val = unbind_to (count, val);
2444 return val;
2448 /* Call function FN with one argument ARG.
2449 Return the result, or nil if something went wrong. */
2451 Lisp_Object
2452 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2454 Lisp_Object args[2];
2455 args[0] = fn;
2456 args[1] = arg;
2457 return safe_call (2, args);
2460 static Lisp_Object Qeval;
2462 Lisp_Object
2463 safe_eval (Lisp_Object sexpr)
2465 return safe_call1 (Qeval, sexpr);
2468 /* Call function FN with one argument ARG.
2469 Return the result, or nil if something went wrong. */
2471 Lisp_Object
2472 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2474 Lisp_Object args[3];
2475 args[0] = fn;
2476 args[1] = arg1;
2477 args[2] = arg2;
2478 return safe_call (3, args);
2483 /***********************************************************************
2484 Debugging
2485 ***********************************************************************/
2487 #if 0
2489 /* Define CHECK_IT to perform sanity checks on iterators.
2490 This is for debugging. It is too slow to do unconditionally. */
2492 static void
2493 check_it (it)
2494 struct it *it;
2496 if (it->method == GET_FROM_STRING)
2498 xassert (STRINGP (it->string));
2499 xassert (IT_STRING_CHARPOS (*it) >= 0);
2501 else
2503 xassert (IT_STRING_CHARPOS (*it) < 0);
2504 if (it->method == GET_FROM_BUFFER)
2506 /* Check that character and byte positions agree. */
2507 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2511 if (it->dpvec)
2512 xassert (it->current.dpvec_index >= 0);
2513 else
2514 xassert (it->current.dpvec_index < 0);
2517 #define CHECK_IT(IT) check_it ((IT))
2519 #else /* not 0 */
2521 #define CHECK_IT(IT) (void) 0
2523 #endif /* not 0 */
2526 #if GLYPH_DEBUG
2528 /* Check that the window end of window W is what we expect it
2529 to be---the last row in the current matrix displaying text. */
2531 static void
2532 check_window_end (w)
2533 struct window *w;
2535 if (!MINI_WINDOW_P (w)
2536 && !NILP (w->window_end_valid))
2538 struct glyph_row *row;
2539 xassert ((row = MATRIX_ROW (w->current_matrix,
2540 XFASTINT (w->window_end_vpos)),
2541 !row->enabled_p
2542 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2543 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2547 #define CHECK_WINDOW_END(W) check_window_end ((W))
2549 #else /* not GLYPH_DEBUG */
2551 #define CHECK_WINDOW_END(W) (void) 0
2553 #endif /* not GLYPH_DEBUG */
2557 /***********************************************************************
2558 Iterator initialization
2559 ***********************************************************************/
2561 /* Initialize IT for displaying current_buffer in window W, starting
2562 at character position CHARPOS. CHARPOS < 0 means that no buffer
2563 position is specified which is useful when the iterator is assigned
2564 a position later. BYTEPOS is the byte position corresponding to
2565 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2567 If ROW is not null, calls to produce_glyphs with IT as parameter
2568 will produce glyphs in that row.
2570 BASE_FACE_ID is the id of a base face to use. It must be one of
2571 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2572 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2573 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2575 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2576 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2577 will be initialized to use the corresponding mode line glyph row of
2578 the desired matrix of W. */
2580 void
2581 init_iterator (struct it *it, struct window *w,
2582 EMACS_INT charpos, EMACS_INT bytepos,
2583 struct glyph_row *row, enum face_id base_face_id)
2585 int highlight_region_p;
2586 enum face_id remapped_base_face_id = base_face_id;
2588 /* Some precondition checks. */
2589 xassert (w != NULL && it != NULL);
2590 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2591 && charpos <= ZV));
2593 /* If face attributes have been changed since the last redisplay,
2594 free realized faces now because they depend on face definitions
2595 that might have changed. Don't free faces while there might be
2596 desired matrices pending which reference these faces. */
2597 if (face_change_count && !inhibit_free_realized_faces)
2599 face_change_count = 0;
2600 free_all_realized_faces (Qnil);
2603 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2604 if (! NILP (Vface_remapping_alist))
2605 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2607 /* Use one of the mode line rows of W's desired matrix if
2608 appropriate. */
2609 if (row == NULL)
2611 if (base_face_id == MODE_LINE_FACE_ID
2612 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2613 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2614 else if (base_face_id == HEADER_LINE_FACE_ID)
2615 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2618 /* Clear IT. */
2619 memset (it, 0, sizeof *it);
2620 it->current.overlay_string_index = -1;
2621 it->current.dpvec_index = -1;
2622 it->base_face_id = remapped_base_face_id;
2623 it->string = Qnil;
2624 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2626 /* The window in which we iterate over current_buffer: */
2627 XSETWINDOW (it->window, w);
2628 it->w = w;
2629 it->f = XFRAME (w->frame);
2631 it->cmp_it.id = -1;
2633 /* Extra space between lines (on window systems only). */
2634 if (base_face_id == DEFAULT_FACE_ID
2635 && FRAME_WINDOW_P (it->f))
2637 if (NATNUMP (current_buffer->extra_line_spacing))
2638 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2639 else if (FLOATP (current_buffer->extra_line_spacing))
2640 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2641 * FRAME_LINE_HEIGHT (it->f));
2642 else if (it->f->extra_line_spacing > 0)
2643 it->extra_line_spacing = it->f->extra_line_spacing;
2644 it->max_extra_line_spacing = 0;
2647 /* If realized faces have been removed, e.g. because of face
2648 attribute changes of named faces, recompute them. When running
2649 in batch mode, the face cache of the initial frame is null. If
2650 we happen to get called, make a dummy face cache. */
2651 if (FRAME_FACE_CACHE (it->f) == NULL)
2652 init_frame_faces (it->f);
2653 if (FRAME_FACE_CACHE (it->f)->used == 0)
2654 recompute_basic_faces (it->f);
2656 /* Current value of the `slice', `space-width', and 'height' properties. */
2657 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2658 it->space_width = Qnil;
2659 it->font_height = Qnil;
2660 it->override_ascent = -1;
2662 /* Are control characters displayed as `^C'? */
2663 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2665 /* -1 means everything between a CR and the following line end
2666 is invisible. >0 means lines indented more than this value are
2667 invisible. */
2668 it->selective = (INTEGERP (current_buffer->selective_display)
2669 ? XFASTINT (current_buffer->selective_display)
2670 : (!NILP (current_buffer->selective_display)
2671 ? -1 : 0));
2672 it->selective_display_ellipsis_p
2673 = !NILP (current_buffer->selective_display_ellipses);
2675 /* Display table to use. */
2676 it->dp = window_display_table (w);
2678 /* Are multibyte characters enabled in current_buffer? */
2679 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2681 /* Do we need to reorder bidirectional text? Not if this is a
2682 unibyte buffer: by definition, none of the single-byte characters
2683 are strong R2L, so no reordering is needed. And bidi.c doesn't
2684 support unibyte buffers anyway. */
2685 it->bidi_p
2686 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2688 /* Non-zero if we should highlight the region. */
2689 highlight_region_p
2690 = (!NILP (Vtransient_mark_mode)
2691 && !NILP (current_buffer->mark_active)
2692 && XMARKER (current_buffer->mark)->buffer != 0);
2694 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2695 start and end of a visible region in window IT->w. Set both to
2696 -1 to indicate no region. */
2697 if (highlight_region_p
2698 /* Maybe highlight only in selected window. */
2699 && (/* Either show region everywhere. */
2700 highlight_nonselected_windows
2701 /* Or show region in the selected window. */
2702 || w == XWINDOW (selected_window)
2703 /* Or show the region if we are in the mini-buffer and W is
2704 the window the mini-buffer refers to. */
2705 || (MINI_WINDOW_P (XWINDOW (selected_window))
2706 && WINDOWP (minibuf_selected_window)
2707 && w == XWINDOW (minibuf_selected_window))))
2709 EMACS_INT charpos = marker_position (current_buffer->mark);
2710 it->region_beg_charpos = min (PT, charpos);
2711 it->region_end_charpos = max (PT, charpos);
2713 else
2714 it->region_beg_charpos = it->region_end_charpos = -1;
2716 /* Get the position at which the redisplay_end_trigger hook should
2717 be run, if it is to be run at all. */
2718 if (MARKERP (w->redisplay_end_trigger)
2719 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2720 it->redisplay_end_trigger_charpos
2721 = marker_position (w->redisplay_end_trigger);
2722 else if (INTEGERP (w->redisplay_end_trigger))
2723 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2725 /* Correct bogus values of tab_width. */
2726 it->tab_width = XINT (current_buffer->tab_width);
2727 if (it->tab_width <= 0 || it->tab_width > 1000)
2728 it->tab_width = 8;
2730 /* Are lines in the display truncated? */
2731 if (base_face_id != DEFAULT_FACE_ID
2732 || XINT (it->w->hscroll)
2733 || (! WINDOW_FULL_WIDTH_P (it->w)
2734 && ((!NILP (Vtruncate_partial_width_windows)
2735 && !INTEGERP (Vtruncate_partial_width_windows))
2736 || (INTEGERP (Vtruncate_partial_width_windows)
2737 && (WINDOW_TOTAL_COLS (it->w)
2738 < XINT (Vtruncate_partial_width_windows))))))
2739 it->line_wrap = TRUNCATE;
2740 else if (NILP (current_buffer->truncate_lines))
2741 it->line_wrap = NILP (current_buffer->word_wrap)
2742 ? WINDOW_WRAP : WORD_WRAP;
2743 else
2744 it->line_wrap = TRUNCATE;
2746 /* Get dimensions of truncation and continuation glyphs. These are
2747 displayed as fringe bitmaps under X, so we don't need them for such
2748 frames. */
2749 if (!FRAME_WINDOW_P (it->f))
2751 if (it->line_wrap == TRUNCATE)
2753 /* We will need the truncation glyph. */
2754 xassert (it->glyph_row == NULL);
2755 produce_special_glyphs (it, IT_TRUNCATION);
2756 it->truncation_pixel_width = it->pixel_width;
2758 else
2760 /* We will need the continuation glyph. */
2761 xassert (it->glyph_row == NULL);
2762 produce_special_glyphs (it, IT_CONTINUATION);
2763 it->continuation_pixel_width = it->pixel_width;
2766 /* Reset these values to zero because the produce_special_glyphs
2767 above has changed them. */
2768 it->pixel_width = it->ascent = it->descent = 0;
2769 it->phys_ascent = it->phys_descent = 0;
2772 /* Set this after getting the dimensions of truncation and
2773 continuation glyphs, so that we don't produce glyphs when calling
2774 produce_special_glyphs, above. */
2775 it->glyph_row = row;
2776 it->area = TEXT_AREA;
2778 /* Forget any previous info about this row being reversed. */
2779 if (it->glyph_row)
2780 it->glyph_row->reversed_p = 0;
2782 /* Get the dimensions of the display area. The display area
2783 consists of the visible window area plus a horizontally scrolled
2784 part to the left of the window. All x-values are relative to the
2785 start of this total display area. */
2786 if (base_face_id != DEFAULT_FACE_ID)
2788 /* Mode lines, menu bar in terminal frames. */
2789 it->first_visible_x = 0;
2790 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2792 else
2794 it->first_visible_x
2795 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2796 it->last_visible_x = (it->first_visible_x
2797 + window_box_width (w, TEXT_AREA));
2799 /* If we truncate lines, leave room for the truncator glyph(s) at
2800 the right margin. Otherwise, leave room for the continuation
2801 glyph(s). Truncation and continuation glyphs are not inserted
2802 for window-based redisplay. */
2803 if (!FRAME_WINDOW_P (it->f))
2805 if (it->line_wrap == TRUNCATE)
2806 it->last_visible_x -= it->truncation_pixel_width;
2807 else
2808 it->last_visible_x -= it->continuation_pixel_width;
2811 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2812 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2815 /* Leave room for a border glyph. */
2816 if (!FRAME_WINDOW_P (it->f)
2817 && !WINDOW_RIGHTMOST_P (it->w))
2818 it->last_visible_x -= 1;
2820 it->last_visible_y = window_text_bottom_y (w);
2822 /* For mode lines and alike, arrange for the first glyph having a
2823 left box line if the face specifies a box. */
2824 if (base_face_id != DEFAULT_FACE_ID)
2826 struct face *face;
2828 it->face_id = remapped_base_face_id;
2830 /* If we have a boxed mode line, make the first character appear
2831 with a left box line. */
2832 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2833 if (face->box != FACE_NO_BOX)
2834 it->start_of_box_run_p = 1;
2837 /* If we are to reorder bidirectional text, init the bidi
2838 iterator. */
2839 if (it->bidi_p)
2841 /* Note the paragraph direction that this buffer wants to
2842 use. */
2843 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2844 it->paragraph_embedding = L2R;
2845 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2846 it->paragraph_embedding = R2L;
2847 else
2848 it->paragraph_embedding = NEUTRAL_DIR;
2849 bidi_init_it (charpos, bytepos, &it->bidi_it);
2852 /* If a buffer position was specified, set the iterator there,
2853 getting overlays and face properties from that position. */
2854 if (charpos >= BUF_BEG (current_buffer))
2856 it->end_charpos = ZV;
2857 it->face_id = -1;
2858 IT_CHARPOS (*it) = charpos;
2860 /* Compute byte position if not specified. */
2861 if (bytepos < charpos)
2862 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2863 else
2864 IT_BYTEPOS (*it) = bytepos;
2866 it->start = it->current;
2868 /* Compute faces etc. */
2869 reseat (it, it->current.pos, 1);
2872 CHECK_IT (it);
2876 /* Initialize IT for the display of window W with window start POS. */
2878 void
2879 start_display (struct it *it, struct window *w, struct text_pos pos)
2881 struct glyph_row *row;
2882 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2884 row = w->desired_matrix->rows + first_vpos;
2885 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2886 it->first_vpos = first_vpos;
2888 /* Don't reseat to previous visible line start if current start
2889 position is in a string or image. */
2890 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2892 int start_at_line_beg_p;
2893 int first_y = it->current_y;
2895 /* If window start is not at a line start, skip forward to POS to
2896 get the correct continuation lines width. */
2897 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2898 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2899 if (!start_at_line_beg_p)
2901 int new_x;
2903 reseat_at_previous_visible_line_start (it);
2904 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2906 new_x = it->current_x + it->pixel_width;
2908 /* If lines are continued, this line may end in the middle
2909 of a multi-glyph character (e.g. a control character
2910 displayed as \003, or in the middle of an overlay
2911 string). In this case move_it_to above will not have
2912 taken us to the start of the continuation line but to the
2913 end of the continued line. */
2914 if (it->current_x > 0
2915 && it->line_wrap != TRUNCATE /* Lines are continued. */
2916 && (/* And glyph doesn't fit on the line. */
2917 new_x > it->last_visible_x
2918 /* Or it fits exactly and we're on a window
2919 system frame. */
2920 || (new_x == it->last_visible_x
2921 && FRAME_WINDOW_P (it->f))))
2923 if (it->current.dpvec_index >= 0
2924 || it->current.overlay_string_index >= 0)
2926 set_iterator_to_next (it, 1);
2927 move_it_in_display_line_to (it, -1, -1, 0);
2930 it->continuation_lines_width += it->current_x;
2933 /* We're starting a new display line, not affected by the
2934 height of the continued line, so clear the appropriate
2935 fields in the iterator structure. */
2936 it->max_ascent = it->max_descent = 0;
2937 it->max_phys_ascent = it->max_phys_descent = 0;
2939 it->current_y = first_y;
2940 it->vpos = 0;
2941 it->current_x = it->hpos = 0;
2947 /* Return 1 if POS is a position in ellipses displayed for invisible
2948 text. W is the window we display, for text property lookup. */
2950 static int
2951 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2953 Lisp_Object prop, window;
2954 int ellipses_p = 0;
2955 EMACS_INT charpos = CHARPOS (pos->pos);
2957 /* If POS specifies a position in a display vector, this might
2958 be for an ellipsis displayed for invisible text. We won't
2959 get the iterator set up for delivering that ellipsis unless
2960 we make sure that it gets aware of the invisible text. */
2961 if (pos->dpvec_index >= 0
2962 && pos->overlay_string_index < 0
2963 && CHARPOS (pos->string_pos) < 0
2964 && charpos > BEGV
2965 && (XSETWINDOW (window, w),
2966 prop = Fget_char_property (make_number (charpos),
2967 Qinvisible, window),
2968 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2970 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2971 window);
2972 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2975 return ellipses_p;
2979 /* Initialize IT for stepping through current_buffer in window W,
2980 starting at position POS that includes overlay string and display
2981 vector/ control character translation position information. Value
2982 is zero if there are overlay strings with newlines at POS. */
2984 static int
2985 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2987 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2988 int i, overlay_strings_with_newlines = 0;
2990 /* If POS specifies a position in a display vector, this might
2991 be for an ellipsis displayed for invisible text. We won't
2992 get the iterator set up for delivering that ellipsis unless
2993 we make sure that it gets aware of the invisible text. */
2994 if (in_ellipses_for_invisible_text_p (pos, w))
2996 --charpos;
2997 bytepos = 0;
3000 /* Keep in mind: the call to reseat in init_iterator skips invisible
3001 text, so we might end up at a position different from POS. This
3002 is only a problem when POS is a row start after a newline and an
3003 overlay starts there with an after-string, and the overlay has an
3004 invisible property. Since we don't skip invisible text in
3005 display_line and elsewhere immediately after consuming the
3006 newline before the row start, such a POS will not be in a string,
3007 but the call to init_iterator below will move us to the
3008 after-string. */
3009 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3011 /* This only scans the current chunk -- it should scan all chunks.
3012 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3013 to 16 in 22.1 to make this a lesser problem. */
3014 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3016 const char *s = SDATA (it->overlay_strings[i]);
3017 const char *e = s + SBYTES (it->overlay_strings[i]);
3019 while (s < e && *s != '\n')
3020 ++s;
3022 if (s < e)
3024 overlay_strings_with_newlines = 1;
3025 break;
3029 /* If position is within an overlay string, set up IT to the right
3030 overlay string. */
3031 if (pos->overlay_string_index >= 0)
3033 int relative_index;
3035 /* If the first overlay string happens to have a `display'
3036 property for an image, the iterator will be set up for that
3037 image, and we have to undo that setup first before we can
3038 correct the overlay string index. */
3039 if (it->method == GET_FROM_IMAGE)
3040 pop_it (it);
3042 /* We already have the first chunk of overlay strings in
3043 IT->overlay_strings. Load more until the one for
3044 pos->overlay_string_index is in IT->overlay_strings. */
3045 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3047 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3048 it->current.overlay_string_index = 0;
3049 while (n--)
3051 load_overlay_strings (it, 0);
3052 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3056 it->current.overlay_string_index = pos->overlay_string_index;
3057 relative_index = (it->current.overlay_string_index
3058 % OVERLAY_STRING_CHUNK_SIZE);
3059 it->string = it->overlay_strings[relative_index];
3060 xassert (STRINGP (it->string));
3061 it->current.string_pos = pos->string_pos;
3062 it->method = GET_FROM_STRING;
3065 if (CHARPOS (pos->string_pos) >= 0)
3067 /* Recorded position is not in an overlay string, but in another
3068 string. This can only be a string from a `display' property.
3069 IT should already be filled with that string. */
3070 it->current.string_pos = pos->string_pos;
3071 xassert (STRINGP (it->string));
3074 /* Restore position in display vector translations, control
3075 character translations or ellipses. */
3076 if (pos->dpvec_index >= 0)
3078 if (it->dpvec == NULL)
3079 get_next_display_element (it);
3080 xassert (it->dpvec && it->current.dpvec_index == 0);
3081 it->current.dpvec_index = pos->dpvec_index;
3084 CHECK_IT (it);
3085 return !overlay_strings_with_newlines;
3089 /* Initialize IT for stepping through current_buffer in window W
3090 starting at ROW->start. */
3092 static void
3093 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3095 init_from_display_pos (it, w, &row->start);
3096 it->start = row->start;
3097 it->continuation_lines_width = row->continuation_lines_width;
3098 CHECK_IT (it);
3102 /* Initialize IT for stepping through current_buffer in window W
3103 starting in the line following ROW, i.e. starting at ROW->end.
3104 Value is zero if there are overlay strings with newlines at ROW's
3105 end position. */
3107 static int
3108 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3110 int success = 0;
3112 if (init_from_display_pos (it, w, &row->end))
3114 if (row->continued_p)
3115 it->continuation_lines_width
3116 = row->continuation_lines_width + row->pixel_width;
3117 CHECK_IT (it);
3118 success = 1;
3121 return success;
3127 /***********************************************************************
3128 Text properties
3129 ***********************************************************************/
3131 /* Called when IT reaches IT->stop_charpos. Handle text property and
3132 overlay changes. Set IT->stop_charpos to the next position where
3133 to stop. */
3135 static void
3136 handle_stop (struct it *it)
3138 enum prop_handled handled;
3139 int handle_overlay_change_p;
3140 struct props *p;
3142 it->dpvec = NULL;
3143 it->current.dpvec_index = -1;
3144 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3145 it->ignore_overlay_strings_at_pos_p = 0;
3146 it->ellipsis_p = 0;
3148 /* Use face of preceding text for ellipsis (if invisible) */
3149 if (it->selective_display_ellipsis_p)
3150 it->saved_face_id = it->face_id;
3154 handled = HANDLED_NORMALLY;
3156 /* Call text property handlers. */
3157 for (p = it_props; p->handler; ++p)
3159 handled = p->handler (it);
3161 if (handled == HANDLED_RECOMPUTE_PROPS)
3162 break;
3163 else if (handled == HANDLED_RETURN)
3165 /* We still want to show before and after strings from
3166 overlays even if the actual buffer text is replaced. */
3167 if (!handle_overlay_change_p
3168 || it->sp > 1
3169 || !get_overlay_strings_1 (it, 0, 0))
3171 if (it->ellipsis_p)
3172 setup_for_ellipsis (it, 0);
3173 /* When handling a display spec, we might load an
3174 empty string. In that case, discard it here. We
3175 used to discard it in handle_single_display_spec,
3176 but that causes get_overlay_strings_1, above, to
3177 ignore overlay strings that we must check. */
3178 if (STRINGP (it->string) && !SCHARS (it->string))
3179 pop_it (it);
3180 return;
3182 else if (STRINGP (it->string) && !SCHARS (it->string))
3183 pop_it (it);
3184 else
3186 it->ignore_overlay_strings_at_pos_p = 1;
3187 it->string_from_display_prop_p = 0;
3188 handle_overlay_change_p = 0;
3190 handled = HANDLED_RECOMPUTE_PROPS;
3191 break;
3193 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3194 handle_overlay_change_p = 0;
3197 if (handled != HANDLED_RECOMPUTE_PROPS)
3199 /* Don't check for overlay strings below when set to deliver
3200 characters from a display vector. */
3201 if (it->method == GET_FROM_DISPLAY_VECTOR)
3202 handle_overlay_change_p = 0;
3204 /* Handle overlay changes.
3205 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3206 if it finds overlays. */
3207 if (handle_overlay_change_p)
3208 handled = handle_overlay_change (it);
3211 if (it->ellipsis_p)
3213 setup_for_ellipsis (it, 0);
3214 break;
3217 while (handled == HANDLED_RECOMPUTE_PROPS);
3219 /* Determine where to stop next. */
3220 if (handled == HANDLED_NORMALLY)
3221 compute_stop_pos (it);
3225 /* Compute IT->stop_charpos from text property and overlay change
3226 information for IT's current position. */
3228 static void
3229 compute_stop_pos (struct it *it)
3231 register INTERVAL iv, next_iv;
3232 Lisp_Object object, limit, position;
3233 EMACS_INT charpos, bytepos;
3235 /* If nowhere else, stop at the end. */
3236 it->stop_charpos = it->end_charpos;
3238 if (STRINGP (it->string))
3240 /* Strings are usually short, so don't limit the search for
3241 properties. */
3242 object = it->string;
3243 limit = Qnil;
3244 charpos = IT_STRING_CHARPOS (*it);
3245 bytepos = IT_STRING_BYTEPOS (*it);
3247 else
3249 EMACS_INT pos;
3251 /* If next overlay change is in front of the current stop pos
3252 (which is IT->end_charpos), stop there. Note: value of
3253 next_overlay_change is point-max if no overlay change
3254 follows. */
3255 charpos = IT_CHARPOS (*it);
3256 bytepos = IT_BYTEPOS (*it);
3257 pos = next_overlay_change (charpos);
3258 if (pos < it->stop_charpos)
3259 it->stop_charpos = pos;
3261 /* If showing the region, we have to stop at the region
3262 start or end because the face might change there. */
3263 if (it->region_beg_charpos > 0)
3265 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3266 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3267 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3268 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3271 /* Set up variables for computing the stop position from text
3272 property changes. */
3273 XSETBUFFER (object, current_buffer);
3274 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3277 /* Get the interval containing IT's position. Value is a null
3278 interval if there isn't such an interval. */
3279 position = make_number (charpos);
3280 iv = validate_interval_range (object, &position, &position, 0);
3281 if (!NULL_INTERVAL_P (iv))
3283 Lisp_Object values_here[LAST_PROP_IDX];
3284 struct props *p;
3286 /* Get properties here. */
3287 for (p = it_props; p->handler; ++p)
3288 values_here[p->idx] = textget (iv->plist, *p->name);
3290 /* Look for an interval following iv that has different
3291 properties. */
3292 for (next_iv = next_interval (iv);
3293 (!NULL_INTERVAL_P (next_iv)
3294 && (NILP (limit)
3295 || XFASTINT (limit) > next_iv->position));
3296 next_iv = next_interval (next_iv))
3298 for (p = it_props; p->handler; ++p)
3300 Lisp_Object new_value;
3302 new_value = textget (next_iv->plist, *p->name);
3303 if (!EQ (values_here[p->idx], new_value))
3304 break;
3307 if (p->handler)
3308 break;
3311 if (!NULL_INTERVAL_P (next_iv))
3313 if (INTEGERP (limit)
3314 && next_iv->position >= XFASTINT (limit))
3315 /* No text property change up to limit. */
3316 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3317 else
3318 /* Text properties change in next_iv. */
3319 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3323 if (it->cmp_it.id < 0)
3325 EMACS_INT stoppos = it->end_charpos;
3327 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3328 stoppos = -1;
3329 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3330 stoppos, it->string);
3333 xassert (STRINGP (it->string)
3334 || (it->stop_charpos >= BEGV
3335 && it->stop_charpos >= IT_CHARPOS (*it)));
3339 /* Return the position of the next overlay change after POS in
3340 current_buffer. Value is point-max if no overlay change
3341 follows. This is like `next-overlay-change' but doesn't use
3342 xmalloc. */
3344 static EMACS_INT
3345 next_overlay_change (EMACS_INT pos)
3347 int noverlays;
3348 EMACS_INT endpos;
3349 Lisp_Object *overlays;
3350 int i;
3352 /* Get all overlays at the given position. */
3353 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3355 /* If any of these overlays ends before endpos,
3356 use its ending point instead. */
3357 for (i = 0; i < noverlays; ++i)
3359 Lisp_Object oend;
3360 EMACS_INT oendpos;
3362 oend = OVERLAY_END (overlays[i]);
3363 oendpos = OVERLAY_POSITION (oend);
3364 endpos = min (endpos, oendpos);
3367 return endpos;
3372 /***********************************************************************
3373 Fontification
3374 ***********************************************************************/
3376 /* Handle changes in the `fontified' property of the current buffer by
3377 calling hook functions from Qfontification_functions to fontify
3378 regions of text. */
3380 static enum prop_handled
3381 handle_fontified_prop (struct it *it)
3383 Lisp_Object prop, pos;
3384 enum prop_handled handled = HANDLED_NORMALLY;
3386 if (!NILP (Vmemory_full))
3387 return handled;
3389 /* Get the value of the `fontified' property at IT's current buffer
3390 position. (The `fontified' property doesn't have a special
3391 meaning in strings.) If the value is nil, call functions from
3392 Qfontification_functions. */
3393 if (!STRINGP (it->string)
3394 && it->s == NULL
3395 && !NILP (Vfontification_functions)
3396 && !NILP (Vrun_hooks)
3397 && (pos = make_number (IT_CHARPOS (*it)),
3398 prop = Fget_char_property (pos, Qfontified, Qnil),
3399 /* Ignore the special cased nil value always present at EOB since
3400 no amount of fontifying will be able to change it. */
3401 NILP (prop) && IT_CHARPOS (*it) < Z))
3403 int count = SPECPDL_INDEX ();
3404 Lisp_Object val;
3406 val = Vfontification_functions;
3407 specbind (Qfontification_functions, Qnil);
3409 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3410 safe_call1 (val, pos);
3411 else
3413 Lisp_Object globals, fn;
3414 struct gcpro gcpro1, gcpro2;
3416 globals = Qnil;
3417 GCPRO2 (val, globals);
3419 for (; CONSP (val); val = XCDR (val))
3421 fn = XCAR (val);
3423 if (EQ (fn, Qt))
3425 /* A value of t indicates this hook has a local
3426 binding; it means to run the global binding too.
3427 In a global value, t should not occur. If it
3428 does, we must ignore it to avoid an endless
3429 loop. */
3430 for (globals = Fdefault_value (Qfontification_functions);
3431 CONSP (globals);
3432 globals = XCDR (globals))
3434 fn = XCAR (globals);
3435 if (!EQ (fn, Qt))
3436 safe_call1 (fn, pos);
3439 else
3440 safe_call1 (fn, pos);
3443 UNGCPRO;
3446 unbind_to (count, Qnil);
3448 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3449 something. This avoids an endless loop if they failed to
3450 fontify the text for which reason ever. */
3451 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3452 handled = HANDLED_RECOMPUTE_PROPS;
3455 return handled;
3460 /***********************************************************************
3461 Faces
3462 ***********************************************************************/
3464 /* Set up iterator IT from face properties at its current position.
3465 Called from handle_stop. */
3467 static enum prop_handled
3468 handle_face_prop (struct it *it)
3470 int new_face_id;
3471 EMACS_INT next_stop;
3473 if (!STRINGP (it->string))
3475 new_face_id
3476 = face_at_buffer_position (it->w,
3477 IT_CHARPOS (*it),
3478 it->region_beg_charpos,
3479 it->region_end_charpos,
3480 &next_stop,
3481 (IT_CHARPOS (*it)
3482 + TEXT_PROP_DISTANCE_LIMIT),
3483 0, it->base_face_id);
3485 /* Is this a start of a run of characters with box face?
3486 Caveat: this can be called for a freshly initialized
3487 iterator; face_id is -1 in this case. We know that the new
3488 face will not change until limit, i.e. if the new face has a
3489 box, all characters up to limit will have one. But, as
3490 usual, we don't know whether limit is really the end. */
3491 if (new_face_id != it->face_id)
3493 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3495 /* If new face has a box but old face has not, this is
3496 the start of a run of characters with box, i.e. it has
3497 a shadow on the left side. The value of face_id of the
3498 iterator will be -1 if this is the initial call that gets
3499 the face. In this case, we have to look in front of IT's
3500 position and see whether there is a face != new_face_id. */
3501 it->start_of_box_run_p
3502 = (new_face->box != FACE_NO_BOX
3503 && (it->face_id >= 0
3504 || IT_CHARPOS (*it) == BEG
3505 || new_face_id != face_before_it_pos (it)));
3506 it->face_box_p = new_face->box != FACE_NO_BOX;
3509 else
3511 int base_face_id;
3512 EMACS_INT bufpos;
3513 int i;
3514 Lisp_Object from_overlay
3515 = (it->current.overlay_string_index >= 0
3516 ? it->string_overlays[it->current.overlay_string_index]
3517 : Qnil);
3519 /* See if we got to this string directly or indirectly from
3520 an overlay property. That includes the before-string or
3521 after-string of an overlay, strings in display properties
3522 provided by an overlay, their text properties, etc.
3524 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3525 if (! NILP (from_overlay))
3526 for (i = it->sp - 1; i >= 0; i--)
3528 if (it->stack[i].current.overlay_string_index >= 0)
3529 from_overlay
3530 = it->string_overlays[it->stack[i].current.overlay_string_index];
3531 else if (! NILP (it->stack[i].from_overlay))
3532 from_overlay = it->stack[i].from_overlay;
3534 if (!NILP (from_overlay))
3535 break;
3538 if (! NILP (from_overlay))
3540 bufpos = IT_CHARPOS (*it);
3541 /* For a string from an overlay, the base face depends
3542 only on text properties and ignores overlays. */
3543 base_face_id
3544 = face_for_overlay_string (it->w,
3545 IT_CHARPOS (*it),
3546 it->region_beg_charpos,
3547 it->region_end_charpos,
3548 &next_stop,
3549 (IT_CHARPOS (*it)
3550 + TEXT_PROP_DISTANCE_LIMIT),
3552 from_overlay);
3554 else
3556 bufpos = 0;
3558 /* For strings from a `display' property, use the face at
3559 IT's current buffer position as the base face to merge
3560 with, so that overlay strings appear in the same face as
3561 surrounding text, unless they specify their own
3562 faces. */
3563 base_face_id = underlying_face_id (it);
3566 new_face_id = face_at_string_position (it->w,
3567 it->string,
3568 IT_STRING_CHARPOS (*it),
3569 bufpos,
3570 it->region_beg_charpos,
3571 it->region_end_charpos,
3572 &next_stop,
3573 base_face_id, 0);
3575 /* Is this a start of a run of characters with box? Caveat:
3576 this can be called for a freshly allocated iterator; face_id
3577 is -1 is this case. We know that the new face will not
3578 change until the next check pos, i.e. if the new face has a
3579 box, all characters up to that position will have a
3580 box. But, as usual, we don't know whether that position
3581 is really the end. */
3582 if (new_face_id != it->face_id)
3584 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3585 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3587 /* If new face has a box but old face hasn't, this is the
3588 start of a run of characters with box, i.e. it has a
3589 shadow on the left side. */
3590 it->start_of_box_run_p
3591 = new_face->box && (old_face == NULL || !old_face->box);
3592 it->face_box_p = new_face->box != FACE_NO_BOX;
3596 it->face_id = new_face_id;
3597 return HANDLED_NORMALLY;
3601 /* Return the ID of the face ``underlying'' IT's current position,
3602 which is in a string. If the iterator is associated with a
3603 buffer, return the face at IT's current buffer position.
3604 Otherwise, use the iterator's base_face_id. */
3606 static int
3607 underlying_face_id (struct it *it)
3609 int face_id = it->base_face_id, i;
3611 xassert (STRINGP (it->string));
3613 for (i = it->sp - 1; i >= 0; --i)
3614 if (NILP (it->stack[i].string))
3615 face_id = it->stack[i].face_id;
3617 return face_id;
3621 /* Compute the face one character before or after the current position
3622 of IT. BEFORE_P non-zero means get the face in front of IT's
3623 position. Value is the id of the face. */
3625 static int
3626 face_before_or_after_it_pos (struct it *it, int before_p)
3628 int face_id, limit;
3629 EMACS_INT next_check_charpos;
3630 struct text_pos pos;
3632 xassert (it->s == NULL);
3634 if (STRINGP (it->string))
3636 EMACS_INT bufpos;
3637 int base_face_id;
3639 /* No face change past the end of the string (for the case
3640 we are padding with spaces). No face change before the
3641 string start. */
3642 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3643 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3644 return it->face_id;
3646 /* Set pos to the position before or after IT's current position. */
3647 if (before_p)
3648 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3649 else
3650 /* For composition, we must check the character after the
3651 composition. */
3652 pos = (it->what == IT_COMPOSITION
3653 ? string_pos (IT_STRING_CHARPOS (*it)
3654 + it->cmp_it.nchars, it->string)
3655 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3657 if (it->current.overlay_string_index >= 0)
3658 bufpos = IT_CHARPOS (*it);
3659 else
3660 bufpos = 0;
3662 base_face_id = underlying_face_id (it);
3664 /* Get the face for ASCII, or unibyte. */
3665 face_id = face_at_string_position (it->w,
3666 it->string,
3667 CHARPOS (pos),
3668 bufpos,
3669 it->region_beg_charpos,
3670 it->region_end_charpos,
3671 &next_check_charpos,
3672 base_face_id, 0);
3674 /* Correct the face for charsets different from ASCII. Do it
3675 for the multibyte case only. The face returned above is
3676 suitable for unibyte text if IT->string is unibyte. */
3677 if (STRING_MULTIBYTE (it->string))
3679 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3680 int c, len;
3681 struct face *face = FACE_FROM_ID (it->f, face_id);
3683 c = string_char_and_length (p, &len);
3684 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3687 else
3689 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3690 || (IT_CHARPOS (*it) <= BEGV && before_p))
3691 return it->face_id;
3693 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3694 pos = it->current.pos;
3696 if (before_p)
3697 DEC_TEXT_POS (pos, it->multibyte_p);
3698 else
3700 if (it->what == IT_COMPOSITION)
3701 /* For composition, we must check the position after the
3702 composition. */
3703 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3704 else
3705 INC_TEXT_POS (pos, it->multibyte_p);
3708 /* Determine face for CHARSET_ASCII, or unibyte. */
3709 face_id = face_at_buffer_position (it->w,
3710 CHARPOS (pos),
3711 it->region_beg_charpos,
3712 it->region_end_charpos,
3713 &next_check_charpos,
3714 limit, 0, -1);
3716 /* Correct the face for charsets different from ASCII. Do it
3717 for the multibyte case only. The face returned above is
3718 suitable for unibyte text if current_buffer is unibyte. */
3719 if (it->multibyte_p)
3721 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3722 struct face *face = FACE_FROM_ID (it->f, face_id);
3723 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3727 return face_id;
3732 /***********************************************************************
3733 Invisible text
3734 ***********************************************************************/
3736 /* Set up iterator IT from invisible properties at its current
3737 position. Called from handle_stop. */
3739 static enum prop_handled
3740 handle_invisible_prop (struct it *it)
3742 enum prop_handled handled = HANDLED_NORMALLY;
3744 if (STRINGP (it->string))
3746 Lisp_Object prop, end_charpos, limit, charpos;
3748 /* Get the value of the invisible text property at the
3749 current position. Value will be nil if there is no such
3750 property. */
3751 charpos = make_number (IT_STRING_CHARPOS (*it));
3752 prop = Fget_text_property (charpos, Qinvisible, it->string);
3754 if (!NILP (prop)
3755 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3757 handled = HANDLED_RECOMPUTE_PROPS;
3759 /* Get the position at which the next change of the
3760 invisible text property can be found in IT->string.
3761 Value will be nil if the property value is the same for
3762 all the rest of IT->string. */
3763 XSETINT (limit, SCHARS (it->string));
3764 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3765 it->string, limit);
3767 /* Text at current position is invisible. The next
3768 change in the property is at position end_charpos.
3769 Move IT's current position to that position. */
3770 if (INTEGERP (end_charpos)
3771 && XFASTINT (end_charpos) < XFASTINT (limit))
3773 struct text_pos old;
3774 old = it->current.string_pos;
3775 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3776 compute_string_pos (&it->current.string_pos, old, it->string);
3778 else
3780 /* The rest of the string is invisible. If this is an
3781 overlay string, proceed with the next overlay string
3782 or whatever comes and return a character from there. */
3783 if (it->current.overlay_string_index >= 0)
3785 next_overlay_string (it);
3786 /* Don't check for overlay strings when we just
3787 finished processing them. */
3788 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3790 else
3792 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3793 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3798 else
3800 int invis_p;
3801 EMACS_INT newpos, next_stop, start_charpos, tem;
3802 Lisp_Object pos, prop, overlay;
3804 /* First of all, is there invisible text at this position? */
3805 tem = start_charpos = IT_CHARPOS (*it);
3806 pos = make_number (tem);
3807 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3808 &overlay);
3809 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3811 /* If we are on invisible text, skip over it. */
3812 if (invis_p && start_charpos < it->end_charpos)
3814 /* Record whether we have to display an ellipsis for the
3815 invisible text. */
3816 int display_ellipsis_p = invis_p == 2;
3818 handled = HANDLED_RECOMPUTE_PROPS;
3820 /* Loop skipping over invisible text. The loop is left at
3821 ZV or with IT on the first char being visible again. */
3824 /* Try to skip some invisible text. Return value is the
3825 position reached which can be equal to where we start
3826 if there is nothing invisible there. This skips both
3827 over invisible text properties and overlays with
3828 invisible property. */
3829 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3831 /* If we skipped nothing at all we weren't at invisible
3832 text in the first place. If everything to the end of
3833 the buffer was skipped, end the loop. */
3834 if (newpos == tem || newpos >= ZV)
3835 invis_p = 0;
3836 else
3838 /* We skipped some characters but not necessarily
3839 all there are. Check if we ended up on visible
3840 text. Fget_char_property returns the property of
3841 the char before the given position, i.e. if we
3842 get invis_p = 0, this means that the char at
3843 newpos is visible. */
3844 pos = make_number (newpos);
3845 prop = Fget_char_property (pos, Qinvisible, it->window);
3846 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3849 /* If we ended up on invisible text, proceed to
3850 skip starting with next_stop. */
3851 if (invis_p)
3852 tem = next_stop;
3854 /* If there are adjacent invisible texts, don't lose the
3855 second one's ellipsis. */
3856 if (invis_p == 2)
3857 display_ellipsis_p = 1;
3859 while (invis_p);
3861 /* The position newpos is now either ZV or on visible text. */
3862 if (it->bidi_p && newpos < ZV)
3864 /* With bidi iteration, the region of invisible text
3865 could start and/or end in the middle of a non-base
3866 embedding level. Therefore, we need to skip
3867 invisible text using the bidi iterator, starting at
3868 IT's current position, until we find ourselves
3869 outside the invisible text. Skipping invisible text
3870 _after_ bidi iteration avoids affecting the visual
3871 order of the displayed text when invisible properties
3872 are added or removed. */
3873 if (it->bidi_it.first_elt)
3875 /* If we were `reseat'ed to a new paragraph,
3876 determine the paragraph base direction. We need
3877 to do it now because next_element_from_buffer may
3878 not have a chance to do it, if we are going to
3879 skip any text at the beginning, which resets the
3880 FIRST_ELT flag. */
3881 bidi_paragraph_init (it->paragraph_embedding,
3882 &it->bidi_it, 1);
3886 bidi_move_to_visually_next (&it->bidi_it);
3888 while (it->stop_charpos <= it->bidi_it.charpos
3889 && it->bidi_it.charpos < newpos);
3890 IT_CHARPOS (*it) = it->bidi_it.charpos;
3891 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3892 /* If we overstepped NEWPOS, record its position in the
3893 iterator, so that we skip invisible text if later the
3894 bidi iteration lands us in the invisible region
3895 again. */
3896 if (IT_CHARPOS (*it) >= newpos)
3897 it->prev_stop = newpos;
3899 else
3901 IT_CHARPOS (*it) = newpos;
3902 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3905 /* If there are before-strings at the start of invisible
3906 text, and the text is invisible because of a text
3907 property, arrange to show before-strings because 20.x did
3908 it that way. (If the text is invisible because of an
3909 overlay property instead of a text property, this is
3910 already handled in the overlay code.) */
3911 if (NILP (overlay)
3912 && get_overlay_strings (it, it->stop_charpos))
3914 handled = HANDLED_RECOMPUTE_PROPS;
3915 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3917 else if (display_ellipsis_p)
3919 /* Make sure that the glyphs of the ellipsis will get
3920 correct `charpos' values. If we would not update
3921 it->position here, the glyphs would belong to the
3922 last visible character _before_ the invisible
3923 text, which confuses `set_cursor_from_row'.
3925 We use the last invisible position instead of the
3926 first because this way the cursor is always drawn on
3927 the first "." of the ellipsis, whenever PT is inside
3928 the invisible text. Otherwise the cursor would be
3929 placed _after_ the ellipsis when the point is after the
3930 first invisible character. */
3931 if (!STRINGP (it->object))
3933 it->position.charpos = newpos - 1;
3934 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3936 it->ellipsis_p = 1;
3937 /* Let the ellipsis display before
3938 considering any properties of the following char.
3939 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3940 handled = HANDLED_RETURN;
3945 return handled;
3949 /* Make iterator IT return `...' next.
3950 Replaces LEN characters from buffer. */
3952 static void
3953 setup_for_ellipsis (struct it *it, int len)
3955 /* Use the display table definition for `...'. Invalid glyphs
3956 will be handled by the method returning elements from dpvec. */
3957 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3959 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3960 it->dpvec = v->contents;
3961 it->dpend = v->contents + v->size;
3963 else
3965 /* Default `...'. */
3966 it->dpvec = default_invis_vector;
3967 it->dpend = default_invis_vector + 3;
3970 it->dpvec_char_len = len;
3971 it->current.dpvec_index = 0;
3972 it->dpvec_face_id = -1;
3974 /* Remember the current face id in case glyphs specify faces.
3975 IT's face is restored in set_iterator_to_next.
3976 saved_face_id was set to preceding char's face in handle_stop. */
3977 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3978 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3980 it->method = GET_FROM_DISPLAY_VECTOR;
3981 it->ellipsis_p = 1;
3986 /***********************************************************************
3987 'display' property
3988 ***********************************************************************/
3990 /* Set up iterator IT from `display' property at its current position.
3991 Called from handle_stop.
3992 We return HANDLED_RETURN if some part of the display property
3993 overrides the display of the buffer text itself.
3994 Otherwise we return HANDLED_NORMALLY. */
3996 static enum prop_handled
3997 handle_display_prop (struct it *it)
3999 Lisp_Object prop, object, overlay;
4000 struct text_pos *position;
4001 /* Nonzero if some property replaces the display of the text itself. */
4002 int display_replaced_p = 0;
4004 if (STRINGP (it->string))
4006 object = it->string;
4007 position = &it->current.string_pos;
4009 else
4011 XSETWINDOW (object, it->w);
4012 position = &it->current.pos;
4015 /* Reset those iterator values set from display property values. */
4016 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4017 it->space_width = Qnil;
4018 it->font_height = Qnil;
4019 it->voffset = 0;
4021 /* We don't support recursive `display' properties, i.e. string
4022 values that have a string `display' property, that have a string
4023 `display' property etc. */
4024 if (!it->string_from_display_prop_p)
4025 it->area = TEXT_AREA;
4027 prop = get_char_property_and_overlay (make_number (position->charpos),
4028 Qdisplay, object, &overlay);
4029 if (NILP (prop))
4030 return HANDLED_NORMALLY;
4031 /* Now OVERLAY is the overlay that gave us this property, or nil
4032 if it was a text property. */
4034 if (!STRINGP (it->string))
4035 object = it->w->buffer;
4037 if (CONSP (prop)
4038 /* Simple properties. */
4039 && !EQ (XCAR (prop), Qimage)
4040 && !EQ (XCAR (prop), Qspace)
4041 && !EQ (XCAR (prop), Qwhen)
4042 && !EQ (XCAR (prop), Qslice)
4043 && !EQ (XCAR (prop), Qspace_width)
4044 && !EQ (XCAR (prop), Qheight)
4045 && !EQ (XCAR (prop), Qraise)
4046 /* Marginal area specifications. */
4047 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4048 && !EQ (XCAR (prop), Qleft_fringe)
4049 && !EQ (XCAR (prop), Qright_fringe)
4050 && !NILP (XCAR (prop)))
4052 for (; CONSP (prop); prop = XCDR (prop))
4054 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4055 position, display_replaced_p))
4057 display_replaced_p = 1;
4058 /* If some text in a string is replaced, `position' no
4059 longer points to the position of `object'. */
4060 if (STRINGP (object))
4061 break;
4065 else if (VECTORP (prop))
4067 int i;
4068 for (i = 0; i < ASIZE (prop); ++i)
4069 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4070 position, display_replaced_p))
4072 display_replaced_p = 1;
4073 /* If some text in a string is replaced, `position' no
4074 longer points to the position of `object'. */
4075 if (STRINGP (object))
4076 break;
4079 else
4081 if (handle_single_display_spec (it, prop, object, overlay,
4082 position, 0))
4083 display_replaced_p = 1;
4086 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4090 /* Value is the position of the end of the `display' property starting
4091 at START_POS in OBJECT. */
4093 static struct text_pos
4094 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4096 Lisp_Object end;
4097 struct text_pos end_pos;
4099 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4100 Qdisplay, object, Qnil);
4101 CHARPOS (end_pos) = XFASTINT (end);
4102 if (STRINGP (object))
4103 compute_string_pos (&end_pos, start_pos, it->string);
4104 else
4105 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4107 return end_pos;
4111 /* Set up IT from a single `display' specification PROP. OBJECT
4112 is the object in which the `display' property was found. *POSITION
4113 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4114 means that we previously saw a display specification which already
4115 replaced text display with something else, for example an image;
4116 we ignore such properties after the first one has been processed.
4118 OVERLAY is the overlay this `display' property came from,
4119 or nil if it was a text property.
4121 If PROP is a `space' or `image' specification, and in some other
4122 cases too, set *POSITION to the position where the `display'
4123 property ends.
4125 Value is non-zero if something was found which replaces the display
4126 of buffer or string text. */
4128 static int
4129 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4130 Lisp_Object overlay, struct text_pos *position,
4131 int display_replaced_before_p)
4133 Lisp_Object form;
4134 Lisp_Object location, value;
4135 struct text_pos start_pos, save_pos;
4136 int valid_p;
4138 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4139 If the result is non-nil, use VALUE instead of SPEC. */
4140 form = Qt;
4141 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4143 spec = XCDR (spec);
4144 if (!CONSP (spec))
4145 return 0;
4146 form = XCAR (spec);
4147 spec = XCDR (spec);
4150 if (!NILP (form) && !EQ (form, Qt))
4152 int count = SPECPDL_INDEX ();
4153 struct gcpro gcpro1;
4155 /* Bind `object' to the object having the `display' property, a
4156 buffer or string. Bind `position' to the position in the
4157 object where the property was found, and `buffer-position'
4158 to the current position in the buffer. */
4159 specbind (Qobject, object);
4160 specbind (Qposition, make_number (CHARPOS (*position)));
4161 specbind (Qbuffer_position,
4162 make_number (STRINGP (object)
4163 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4164 GCPRO1 (form);
4165 form = safe_eval (form);
4166 UNGCPRO;
4167 unbind_to (count, Qnil);
4170 if (NILP (form))
4171 return 0;
4173 /* Handle `(height HEIGHT)' specifications. */
4174 if (CONSP (spec)
4175 && EQ (XCAR (spec), Qheight)
4176 && CONSP (XCDR (spec)))
4178 if (!FRAME_WINDOW_P (it->f))
4179 return 0;
4181 it->font_height = XCAR (XCDR (spec));
4182 if (!NILP (it->font_height))
4184 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4185 int new_height = -1;
4187 if (CONSP (it->font_height)
4188 && (EQ (XCAR (it->font_height), Qplus)
4189 || EQ (XCAR (it->font_height), Qminus))
4190 && CONSP (XCDR (it->font_height))
4191 && INTEGERP (XCAR (XCDR (it->font_height))))
4193 /* `(+ N)' or `(- N)' where N is an integer. */
4194 int steps = XINT (XCAR (XCDR (it->font_height)));
4195 if (EQ (XCAR (it->font_height), Qplus))
4196 steps = - steps;
4197 it->face_id = smaller_face (it->f, it->face_id, steps);
4199 else if (FUNCTIONP (it->font_height))
4201 /* Call function with current height as argument.
4202 Value is the new height. */
4203 Lisp_Object height;
4204 height = safe_call1 (it->font_height,
4205 face->lface[LFACE_HEIGHT_INDEX]);
4206 if (NUMBERP (height))
4207 new_height = XFLOATINT (height);
4209 else if (NUMBERP (it->font_height))
4211 /* Value is a multiple of the canonical char height. */
4212 struct face *face;
4214 face = FACE_FROM_ID (it->f,
4215 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4216 new_height = (XFLOATINT (it->font_height)
4217 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4219 else
4221 /* Evaluate IT->font_height with `height' bound to the
4222 current specified height to get the new height. */
4223 int count = SPECPDL_INDEX ();
4225 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4226 value = safe_eval (it->font_height);
4227 unbind_to (count, Qnil);
4229 if (NUMBERP (value))
4230 new_height = XFLOATINT (value);
4233 if (new_height > 0)
4234 it->face_id = face_with_height (it->f, it->face_id, new_height);
4237 return 0;
4240 /* Handle `(space-width WIDTH)'. */
4241 if (CONSP (spec)
4242 && EQ (XCAR (spec), Qspace_width)
4243 && CONSP (XCDR (spec)))
4245 if (!FRAME_WINDOW_P (it->f))
4246 return 0;
4248 value = XCAR (XCDR (spec));
4249 if (NUMBERP (value) && XFLOATINT (value) > 0)
4250 it->space_width = value;
4252 return 0;
4255 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4256 if (CONSP (spec)
4257 && EQ (XCAR (spec), Qslice))
4259 Lisp_Object tem;
4261 if (!FRAME_WINDOW_P (it->f))
4262 return 0;
4264 if (tem = XCDR (spec), CONSP (tem))
4266 it->slice.x = XCAR (tem);
4267 if (tem = XCDR (tem), CONSP (tem))
4269 it->slice.y = XCAR (tem);
4270 if (tem = XCDR (tem), CONSP (tem))
4272 it->slice.width = XCAR (tem);
4273 if (tem = XCDR (tem), CONSP (tem))
4274 it->slice.height = XCAR (tem);
4279 return 0;
4282 /* Handle `(raise FACTOR)'. */
4283 if (CONSP (spec)
4284 && EQ (XCAR (spec), Qraise)
4285 && CONSP (XCDR (spec)))
4287 if (!FRAME_WINDOW_P (it->f))
4288 return 0;
4290 #ifdef HAVE_WINDOW_SYSTEM
4291 value = XCAR (XCDR (spec));
4292 if (NUMBERP (value))
4294 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4295 it->voffset = - (XFLOATINT (value)
4296 * (FONT_HEIGHT (face->font)));
4298 #endif /* HAVE_WINDOW_SYSTEM */
4300 return 0;
4303 /* Don't handle the other kinds of display specifications
4304 inside a string that we got from a `display' property. */
4305 if (it->string_from_display_prop_p)
4306 return 0;
4308 /* Characters having this form of property are not displayed, so
4309 we have to find the end of the property. */
4310 start_pos = *position;
4311 *position = display_prop_end (it, object, start_pos);
4312 value = Qnil;
4314 /* Stop the scan at that end position--we assume that all
4315 text properties change there. */
4316 it->stop_charpos = position->charpos;
4318 /* Handle `(left-fringe BITMAP [FACE])'
4319 and `(right-fringe BITMAP [FACE])'. */
4320 if (CONSP (spec)
4321 && (EQ (XCAR (spec), Qleft_fringe)
4322 || EQ (XCAR (spec), Qright_fringe))
4323 && CONSP (XCDR (spec)))
4325 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4326 int fringe_bitmap;
4328 if (!FRAME_WINDOW_P (it->f))
4329 /* If we return here, POSITION has been advanced
4330 across the text with this property. */
4331 return 0;
4333 #ifdef HAVE_WINDOW_SYSTEM
4334 value = XCAR (XCDR (spec));
4335 if (!SYMBOLP (value)
4336 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4337 /* If we return here, POSITION has been advanced
4338 across the text with this property. */
4339 return 0;
4341 if (CONSP (XCDR (XCDR (spec))))
4343 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4344 int face_id2 = lookup_derived_face (it->f, face_name,
4345 FRINGE_FACE_ID, 0);
4346 if (face_id2 >= 0)
4347 face_id = face_id2;
4350 /* Save current settings of IT so that we can restore them
4351 when we are finished with the glyph property value. */
4353 save_pos = it->position;
4354 it->position = *position;
4355 push_it (it);
4356 it->position = save_pos;
4358 it->area = TEXT_AREA;
4359 it->what = IT_IMAGE;
4360 it->image_id = -1; /* no image */
4361 it->position = start_pos;
4362 it->object = NILP (object) ? it->w->buffer : object;
4363 it->method = GET_FROM_IMAGE;
4364 it->from_overlay = Qnil;
4365 it->face_id = face_id;
4367 /* Say that we haven't consumed the characters with
4368 `display' property yet. The call to pop_it in
4369 set_iterator_to_next will clean this up. */
4370 *position = start_pos;
4372 if (EQ (XCAR (spec), Qleft_fringe))
4374 it->left_user_fringe_bitmap = fringe_bitmap;
4375 it->left_user_fringe_face_id = face_id;
4377 else
4379 it->right_user_fringe_bitmap = fringe_bitmap;
4380 it->right_user_fringe_face_id = face_id;
4382 #endif /* HAVE_WINDOW_SYSTEM */
4383 return 1;
4386 /* Prepare to handle `((margin left-margin) ...)',
4387 `((margin right-margin) ...)' and `((margin nil) ...)'
4388 prefixes for display specifications. */
4389 location = Qunbound;
4390 if (CONSP (spec) && CONSP (XCAR (spec)))
4392 Lisp_Object tem;
4394 value = XCDR (spec);
4395 if (CONSP (value))
4396 value = XCAR (value);
4398 tem = XCAR (spec);
4399 if (EQ (XCAR (tem), Qmargin)
4400 && (tem = XCDR (tem),
4401 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4402 (NILP (tem)
4403 || EQ (tem, Qleft_margin)
4404 || EQ (tem, Qright_margin))))
4405 location = tem;
4408 if (EQ (location, Qunbound))
4410 location = Qnil;
4411 value = spec;
4414 /* After this point, VALUE is the property after any
4415 margin prefix has been stripped. It must be a string,
4416 an image specification, or `(space ...)'.
4418 LOCATION specifies where to display: `left-margin',
4419 `right-margin' or nil. */
4421 valid_p = (STRINGP (value)
4422 #ifdef HAVE_WINDOW_SYSTEM
4423 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4424 #endif /* not HAVE_WINDOW_SYSTEM */
4425 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4427 if (valid_p && !display_replaced_before_p)
4429 /* Save current settings of IT so that we can restore them
4430 when we are finished with the glyph property value. */
4431 save_pos = it->position;
4432 it->position = *position;
4433 push_it (it);
4434 it->position = save_pos;
4435 it->from_overlay = overlay;
4437 if (NILP (location))
4438 it->area = TEXT_AREA;
4439 else if (EQ (location, Qleft_margin))
4440 it->area = LEFT_MARGIN_AREA;
4441 else
4442 it->area = RIGHT_MARGIN_AREA;
4444 if (STRINGP (value))
4446 it->string = value;
4447 it->multibyte_p = STRING_MULTIBYTE (it->string);
4448 it->current.overlay_string_index = -1;
4449 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4450 it->end_charpos = it->string_nchars = SCHARS (it->string);
4451 it->method = GET_FROM_STRING;
4452 it->stop_charpos = 0;
4453 it->string_from_display_prop_p = 1;
4454 /* Say that we haven't consumed the characters with
4455 `display' property yet. The call to pop_it in
4456 set_iterator_to_next will clean this up. */
4457 if (BUFFERP (object))
4458 *position = start_pos;
4460 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4462 it->method = GET_FROM_STRETCH;
4463 it->object = value;
4464 *position = it->position = start_pos;
4466 #ifdef HAVE_WINDOW_SYSTEM
4467 else
4469 it->what = IT_IMAGE;
4470 it->image_id = lookup_image (it->f, value);
4471 it->position = start_pos;
4472 it->object = NILP (object) ? it->w->buffer : object;
4473 it->method = GET_FROM_IMAGE;
4475 /* Say that we haven't consumed the characters with
4476 `display' property yet. The call to pop_it in
4477 set_iterator_to_next will clean this up. */
4478 *position = start_pos;
4480 #endif /* HAVE_WINDOW_SYSTEM */
4482 return 1;
4485 /* Invalid property or property not supported. Restore
4486 POSITION to what it was before. */
4487 *position = start_pos;
4488 return 0;
4492 /* Check if SPEC is a display sub-property value whose text should be
4493 treated as intangible. */
4495 static int
4496 single_display_spec_intangible_p (Lisp_Object prop)
4498 /* Skip over `when FORM'. */
4499 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4501 prop = XCDR (prop);
4502 if (!CONSP (prop))
4503 return 0;
4504 prop = XCDR (prop);
4507 if (STRINGP (prop))
4508 return 1;
4510 if (!CONSP (prop))
4511 return 0;
4513 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4514 we don't need to treat text as intangible. */
4515 if (EQ (XCAR (prop), Qmargin))
4517 prop = XCDR (prop);
4518 if (!CONSP (prop))
4519 return 0;
4521 prop = XCDR (prop);
4522 if (!CONSP (prop)
4523 || EQ (XCAR (prop), Qleft_margin)
4524 || EQ (XCAR (prop), Qright_margin))
4525 return 0;
4528 return (CONSP (prop)
4529 && (EQ (XCAR (prop), Qimage)
4530 || EQ (XCAR (prop), Qspace)));
4534 /* Check if PROP is a display property value whose text should be
4535 treated as intangible. */
4538 display_prop_intangible_p (Lisp_Object prop)
4540 if (CONSP (prop)
4541 && CONSP (XCAR (prop))
4542 && !EQ (Qmargin, XCAR (XCAR (prop))))
4544 /* A list of sub-properties. */
4545 while (CONSP (prop))
4547 if (single_display_spec_intangible_p (XCAR (prop)))
4548 return 1;
4549 prop = XCDR (prop);
4552 else if (VECTORP (prop))
4554 /* A vector of sub-properties. */
4555 int i;
4556 for (i = 0; i < ASIZE (prop); ++i)
4557 if (single_display_spec_intangible_p (AREF (prop, i)))
4558 return 1;
4560 else
4561 return single_display_spec_intangible_p (prop);
4563 return 0;
4567 /* Return 1 if PROP is a display sub-property value containing STRING. */
4569 static int
4570 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4572 if (EQ (string, prop))
4573 return 1;
4575 /* Skip over `when FORM'. */
4576 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4578 prop = XCDR (prop);
4579 if (!CONSP (prop))
4580 return 0;
4581 prop = XCDR (prop);
4584 if (CONSP (prop))
4585 /* Skip over `margin LOCATION'. */
4586 if (EQ (XCAR (prop), Qmargin))
4588 prop = XCDR (prop);
4589 if (!CONSP (prop))
4590 return 0;
4592 prop = XCDR (prop);
4593 if (!CONSP (prop))
4594 return 0;
4597 return CONSP (prop) && EQ (XCAR (prop), string);
4601 /* Return 1 if STRING appears in the `display' property PROP. */
4603 static int
4604 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4606 if (CONSP (prop)
4607 && CONSP (XCAR (prop))
4608 && !EQ (Qmargin, XCAR (XCAR (prop))))
4610 /* A list of sub-properties. */
4611 while (CONSP (prop))
4613 if (single_display_spec_string_p (XCAR (prop), string))
4614 return 1;
4615 prop = XCDR (prop);
4618 else if (VECTORP (prop))
4620 /* A vector of sub-properties. */
4621 int i;
4622 for (i = 0; i < ASIZE (prop); ++i)
4623 if (single_display_spec_string_p (AREF (prop, i), string))
4624 return 1;
4626 else
4627 return single_display_spec_string_p (prop, string);
4629 return 0;
4632 /* Look for STRING in overlays and text properties in W's buffer,
4633 between character positions FROM and TO (excluding TO).
4634 BACK_P non-zero means look back (in this case, TO is supposed to be
4635 less than FROM).
4636 Value is the first character position where STRING was found, or
4637 zero if it wasn't found before hitting TO.
4639 W's buffer must be current.
4641 This function may only use code that doesn't eval because it is
4642 called asynchronously from note_mouse_highlight. */
4644 static EMACS_INT
4645 string_buffer_position_lim (struct window *w, Lisp_Object string,
4646 EMACS_INT from, EMACS_INT to, int back_p)
4648 Lisp_Object limit, prop, pos;
4649 int found = 0;
4651 pos = make_number (from);
4653 if (!back_p) /* looking forward */
4655 limit = make_number (min (to, ZV));
4656 while (!found && !EQ (pos, limit))
4658 prop = Fget_char_property (pos, Qdisplay, Qnil);
4659 if (!NILP (prop) && display_prop_string_p (prop, string))
4660 found = 1;
4661 else
4662 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4663 limit);
4666 else /* looking back */
4668 limit = make_number (max (to, BEGV));
4669 while (!found && !EQ (pos, limit))
4671 prop = Fget_char_property (pos, Qdisplay, Qnil);
4672 if (!NILP (prop) && display_prop_string_p (prop, string))
4673 found = 1;
4674 else
4675 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4676 limit);
4680 return found ? XINT (pos) : 0;
4683 /* Determine which buffer position in W's buffer STRING comes from.
4684 AROUND_CHARPOS is an approximate position where it could come from.
4685 Value is the buffer position or 0 if it couldn't be determined.
4687 W's buffer must be current.
4689 This function is necessary because we don't record buffer positions
4690 in glyphs generated from strings (to keep struct glyph small).
4691 This function may only use code that doesn't eval because it is
4692 called asynchronously from note_mouse_highlight. */
4694 EMACS_INT
4695 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4697 const int MAX_DISTANCE = 1000;
4698 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4699 around_charpos + MAX_DISTANCE,
4702 if (!found)
4703 found = string_buffer_position_lim (w, string, around_charpos,
4704 around_charpos - MAX_DISTANCE, 1);
4705 return found;
4710 /***********************************************************************
4711 `composition' property
4712 ***********************************************************************/
4714 /* Set up iterator IT from `composition' property at its current
4715 position. Called from handle_stop. */
4717 static enum prop_handled
4718 handle_composition_prop (struct it *it)
4720 Lisp_Object prop, string;
4721 EMACS_INT pos, pos_byte, start, end;
4723 if (STRINGP (it->string))
4725 unsigned char *s;
4727 pos = IT_STRING_CHARPOS (*it);
4728 pos_byte = IT_STRING_BYTEPOS (*it);
4729 string = it->string;
4730 s = SDATA (string) + pos_byte;
4731 it->c = STRING_CHAR (s);
4733 else
4735 pos = IT_CHARPOS (*it);
4736 pos_byte = IT_BYTEPOS (*it);
4737 string = Qnil;
4738 it->c = FETCH_CHAR (pos_byte);
4741 /* If there's a valid composition and point is not inside of the
4742 composition (in the case that the composition is from the current
4743 buffer), draw a glyph composed from the composition components. */
4744 if (find_composition (pos, -1, &start, &end, &prop, string)
4745 && COMPOSITION_VALID_P (start, end, prop)
4746 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4748 if (start != pos)
4750 if (STRINGP (it->string))
4751 pos_byte = string_char_to_byte (it->string, start);
4752 else
4753 pos_byte = CHAR_TO_BYTE (start);
4755 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4756 prop, string);
4758 if (it->cmp_it.id >= 0)
4760 it->cmp_it.ch = -1;
4761 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4762 it->cmp_it.nglyphs = -1;
4766 return HANDLED_NORMALLY;
4771 /***********************************************************************
4772 Overlay strings
4773 ***********************************************************************/
4775 /* The following structure is used to record overlay strings for
4776 later sorting in load_overlay_strings. */
4778 struct overlay_entry
4780 Lisp_Object overlay;
4781 Lisp_Object string;
4782 int priority;
4783 int after_string_p;
4787 /* Set up iterator IT from overlay strings at its current position.
4788 Called from handle_stop. */
4790 static enum prop_handled
4791 handle_overlay_change (struct it *it)
4793 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4794 return HANDLED_RECOMPUTE_PROPS;
4795 else
4796 return HANDLED_NORMALLY;
4800 /* Set up the next overlay string for delivery by IT, if there is an
4801 overlay string to deliver. Called by set_iterator_to_next when the
4802 end of the current overlay string is reached. If there are more
4803 overlay strings to display, IT->string and
4804 IT->current.overlay_string_index are set appropriately here.
4805 Otherwise IT->string is set to nil. */
4807 static void
4808 next_overlay_string (struct it *it)
4810 ++it->current.overlay_string_index;
4811 if (it->current.overlay_string_index == it->n_overlay_strings)
4813 /* No more overlay strings. Restore IT's settings to what
4814 they were before overlay strings were processed, and
4815 continue to deliver from current_buffer. */
4817 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4818 pop_it (it);
4819 xassert (it->sp > 0
4820 || (NILP (it->string)
4821 && it->method == GET_FROM_BUFFER
4822 && it->stop_charpos >= BEGV
4823 && it->stop_charpos <= it->end_charpos));
4824 it->current.overlay_string_index = -1;
4825 it->n_overlay_strings = 0;
4827 /* If we're at the end of the buffer, record that we have
4828 processed the overlay strings there already, so that
4829 next_element_from_buffer doesn't try it again. */
4830 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4831 it->overlay_strings_at_end_processed_p = 1;
4833 else
4835 /* There are more overlay strings to process. If
4836 IT->current.overlay_string_index has advanced to a position
4837 where we must load IT->overlay_strings with more strings, do
4838 it. */
4839 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4841 if (it->current.overlay_string_index && i == 0)
4842 load_overlay_strings (it, 0);
4844 /* Initialize IT to deliver display elements from the overlay
4845 string. */
4846 it->string = it->overlay_strings[i];
4847 it->multibyte_p = STRING_MULTIBYTE (it->string);
4848 SET_TEXT_POS (it->current.string_pos, 0, 0);
4849 it->method = GET_FROM_STRING;
4850 it->stop_charpos = 0;
4851 if (it->cmp_it.stop_pos >= 0)
4852 it->cmp_it.stop_pos = 0;
4855 CHECK_IT (it);
4859 /* Compare two overlay_entry structures E1 and E2. Used as a
4860 comparison function for qsort in load_overlay_strings. Overlay
4861 strings for the same position are sorted so that
4863 1. All after-strings come in front of before-strings, except
4864 when they come from the same overlay.
4866 2. Within after-strings, strings are sorted so that overlay strings
4867 from overlays with higher priorities come first.
4869 2. Within before-strings, strings are sorted so that overlay
4870 strings from overlays with higher priorities come last.
4872 Value is analogous to strcmp. */
4875 static int
4876 compare_overlay_entries (const void *e1, const void *e2)
4878 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4879 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4880 int result;
4882 if (entry1->after_string_p != entry2->after_string_p)
4884 /* Let after-strings appear in front of before-strings if
4885 they come from different overlays. */
4886 if (EQ (entry1->overlay, entry2->overlay))
4887 result = entry1->after_string_p ? 1 : -1;
4888 else
4889 result = entry1->after_string_p ? -1 : 1;
4891 else if (entry1->after_string_p)
4892 /* After-strings sorted in order of decreasing priority. */
4893 result = entry2->priority - entry1->priority;
4894 else
4895 /* Before-strings sorted in order of increasing priority. */
4896 result = entry1->priority - entry2->priority;
4898 return result;
4902 /* Load the vector IT->overlay_strings with overlay strings from IT's
4903 current buffer position, or from CHARPOS if that is > 0. Set
4904 IT->n_overlays to the total number of overlay strings found.
4906 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4907 a time. On entry into load_overlay_strings,
4908 IT->current.overlay_string_index gives the number of overlay
4909 strings that have already been loaded by previous calls to this
4910 function.
4912 IT->add_overlay_start contains an additional overlay start
4913 position to consider for taking overlay strings from, if non-zero.
4914 This position comes into play when the overlay has an `invisible'
4915 property, and both before and after-strings. When we've skipped to
4916 the end of the overlay, because of its `invisible' property, we
4917 nevertheless want its before-string to appear.
4918 IT->add_overlay_start will contain the overlay start position
4919 in this case.
4921 Overlay strings are sorted so that after-string strings come in
4922 front of before-string strings. Within before and after-strings,
4923 strings are sorted by overlay priority. See also function
4924 compare_overlay_entries. */
4926 static void
4927 load_overlay_strings (struct it *it, EMACS_INT charpos)
4929 Lisp_Object overlay, window, str, invisible;
4930 struct Lisp_Overlay *ov;
4931 EMACS_INT start, end;
4932 int size = 20;
4933 int n = 0, i, j, invis_p;
4934 struct overlay_entry *entries
4935 = (struct overlay_entry *) alloca (size * sizeof *entries);
4937 if (charpos <= 0)
4938 charpos = IT_CHARPOS (*it);
4940 /* Append the overlay string STRING of overlay OVERLAY to vector
4941 `entries' which has size `size' and currently contains `n'
4942 elements. AFTER_P non-zero means STRING is an after-string of
4943 OVERLAY. */
4944 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4945 do \
4947 Lisp_Object priority; \
4949 if (n == size) \
4951 int new_size = 2 * size; \
4952 struct overlay_entry *old = entries; \
4953 entries = \
4954 (struct overlay_entry *) alloca (new_size \
4955 * sizeof *entries); \
4956 memcpy (entries, old, size * sizeof *entries); \
4957 size = new_size; \
4960 entries[n].string = (STRING); \
4961 entries[n].overlay = (OVERLAY); \
4962 priority = Foverlay_get ((OVERLAY), Qpriority); \
4963 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4964 entries[n].after_string_p = (AFTER_P); \
4965 ++n; \
4967 while (0)
4969 /* Process overlay before the overlay center. */
4970 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4972 XSETMISC (overlay, ov);
4973 xassert (OVERLAYP (overlay));
4974 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4975 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4977 if (end < charpos)
4978 break;
4980 /* Skip this overlay if it doesn't start or end at IT's current
4981 position. */
4982 if (end != charpos && start != charpos)
4983 continue;
4985 /* Skip this overlay if it doesn't apply to IT->w. */
4986 window = Foverlay_get (overlay, Qwindow);
4987 if (WINDOWP (window) && XWINDOW (window) != it->w)
4988 continue;
4990 /* If the text ``under'' the overlay is invisible, both before-
4991 and after-strings from this overlay are visible; start and
4992 end position are indistinguishable. */
4993 invisible = Foverlay_get (overlay, Qinvisible);
4994 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4996 /* If overlay has a non-empty before-string, record it. */
4997 if ((start == charpos || (end == charpos && invis_p))
4998 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4999 && SCHARS (str))
5000 RECORD_OVERLAY_STRING (overlay, str, 0);
5002 /* If overlay has a non-empty after-string, record it. */
5003 if ((end == charpos || (start == charpos && invis_p))
5004 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5005 && SCHARS (str))
5006 RECORD_OVERLAY_STRING (overlay, str, 1);
5009 /* Process overlays after the overlay center. */
5010 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5012 XSETMISC (overlay, ov);
5013 xassert (OVERLAYP (overlay));
5014 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5015 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5017 if (start > charpos)
5018 break;
5020 /* Skip this overlay if it doesn't start or end at IT's current
5021 position. */
5022 if (end != charpos && start != charpos)
5023 continue;
5025 /* Skip this overlay if it doesn't apply to IT->w. */
5026 window = Foverlay_get (overlay, Qwindow);
5027 if (WINDOWP (window) && XWINDOW (window) != it->w)
5028 continue;
5030 /* If the text ``under'' the overlay is invisible, it has a zero
5031 dimension, and both before- and after-strings apply. */
5032 invisible = Foverlay_get (overlay, Qinvisible);
5033 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5035 /* If overlay has a non-empty before-string, record it. */
5036 if ((start == charpos || (end == charpos && invis_p))
5037 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5038 && SCHARS (str))
5039 RECORD_OVERLAY_STRING (overlay, str, 0);
5041 /* If overlay has a non-empty after-string, record it. */
5042 if ((end == charpos || (start == charpos && invis_p))
5043 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5044 && SCHARS (str))
5045 RECORD_OVERLAY_STRING (overlay, str, 1);
5048 #undef RECORD_OVERLAY_STRING
5050 /* Sort entries. */
5051 if (n > 1)
5052 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5054 /* Record the total number of strings to process. */
5055 it->n_overlay_strings = n;
5057 /* IT->current.overlay_string_index is the number of overlay strings
5058 that have already been consumed by IT. Copy some of the
5059 remaining overlay strings to IT->overlay_strings. */
5060 i = 0;
5061 j = it->current.overlay_string_index;
5062 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5064 it->overlay_strings[i] = entries[j].string;
5065 it->string_overlays[i++] = entries[j++].overlay;
5068 CHECK_IT (it);
5072 /* Get the first chunk of overlay strings at IT's current buffer
5073 position, or at CHARPOS if that is > 0. Value is non-zero if at
5074 least one overlay string was found. */
5076 static int
5077 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5079 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5080 process. This fills IT->overlay_strings with strings, and sets
5081 IT->n_overlay_strings to the total number of strings to process.
5082 IT->pos.overlay_string_index has to be set temporarily to zero
5083 because load_overlay_strings needs this; it must be set to -1
5084 when no overlay strings are found because a zero value would
5085 indicate a position in the first overlay string. */
5086 it->current.overlay_string_index = 0;
5087 load_overlay_strings (it, charpos);
5089 /* If we found overlay strings, set up IT to deliver display
5090 elements from the first one. Otherwise set up IT to deliver
5091 from current_buffer. */
5092 if (it->n_overlay_strings)
5094 /* Make sure we know settings in current_buffer, so that we can
5095 restore meaningful values when we're done with the overlay
5096 strings. */
5097 if (compute_stop_p)
5098 compute_stop_pos (it);
5099 xassert (it->face_id >= 0);
5101 /* Save IT's settings. They are restored after all overlay
5102 strings have been processed. */
5103 xassert (!compute_stop_p || it->sp == 0);
5105 /* When called from handle_stop, there might be an empty display
5106 string loaded. In that case, don't bother saving it. */
5107 if (!STRINGP (it->string) || SCHARS (it->string))
5108 push_it (it);
5110 /* Set up IT to deliver display elements from the first overlay
5111 string. */
5112 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5113 it->string = it->overlay_strings[0];
5114 it->from_overlay = Qnil;
5115 it->stop_charpos = 0;
5116 xassert (STRINGP (it->string));
5117 it->end_charpos = SCHARS (it->string);
5118 it->multibyte_p = STRING_MULTIBYTE (it->string);
5119 it->method = GET_FROM_STRING;
5120 return 1;
5123 it->current.overlay_string_index = -1;
5124 return 0;
5127 static int
5128 get_overlay_strings (struct it *it, EMACS_INT charpos)
5130 it->string = Qnil;
5131 it->method = GET_FROM_BUFFER;
5133 (void) get_overlay_strings_1 (it, charpos, 1);
5135 CHECK_IT (it);
5137 /* Value is non-zero if we found at least one overlay string. */
5138 return STRINGP (it->string);
5143 /***********************************************************************
5144 Saving and restoring state
5145 ***********************************************************************/
5147 /* Save current settings of IT on IT->stack. Called, for example,
5148 before setting up IT for an overlay string, to be able to restore
5149 IT's settings to what they were after the overlay string has been
5150 processed. */
5152 static void
5153 push_it (struct it *it)
5155 struct iterator_stack_entry *p;
5157 xassert (it->sp < IT_STACK_SIZE);
5158 p = it->stack + it->sp;
5160 p->stop_charpos = it->stop_charpos;
5161 p->prev_stop = it->prev_stop;
5162 p->base_level_stop = it->base_level_stop;
5163 p->cmp_it = it->cmp_it;
5164 xassert (it->face_id >= 0);
5165 p->face_id = it->face_id;
5166 p->string = it->string;
5167 p->method = it->method;
5168 p->from_overlay = it->from_overlay;
5169 switch (p->method)
5171 case GET_FROM_IMAGE:
5172 p->u.image.object = it->object;
5173 p->u.image.image_id = it->image_id;
5174 p->u.image.slice = it->slice;
5175 break;
5176 case GET_FROM_STRETCH:
5177 p->u.stretch.object = it->object;
5178 break;
5180 p->position = it->position;
5181 p->current = it->current;
5182 p->end_charpos = it->end_charpos;
5183 p->string_nchars = it->string_nchars;
5184 p->area = it->area;
5185 p->multibyte_p = it->multibyte_p;
5186 p->avoid_cursor_p = it->avoid_cursor_p;
5187 p->space_width = it->space_width;
5188 p->font_height = it->font_height;
5189 p->voffset = it->voffset;
5190 p->string_from_display_prop_p = it->string_from_display_prop_p;
5191 p->display_ellipsis_p = 0;
5192 p->line_wrap = it->line_wrap;
5193 ++it->sp;
5196 static void
5197 iterate_out_of_display_property (struct it *it)
5199 /* Maybe initialize paragraph direction. If we are at the beginning
5200 of a new paragraph, next_element_from_buffer may not have a
5201 chance to do that. */
5202 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5203 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5204 /* prev_stop can be zero, so check against BEGV as well. */
5205 while (it->bidi_it.charpos >= BEGV
5206 && it->prev_stop <= it->bidi_it.charpos
5207 && it->bidi_it.charpos < CHARPOS (it->position))
5208 bidi_move_to_visually_next (&it->bidi_it);
5209 /* Record the stop_pos we just crossed, for when we cross it
5210 back, maybe. */
5211 if (it->bidi_it.charpos > CHARPOS (it->position))
5212 it->prev_stop = CHARPOS (it->position);
5213 /* If we ended up not where pop_it put us, resync IT's
5214 positional members with the bidi iterator. */
5215 if (it->bidi_it.charpos != CHARPOS (it->position))
5217 SET_TEXT_POS (it->position,
5218 it->bidi_it.charpos, it->bidi_it.bytepos);
5219 it->current.pos = it->position;
5223 /* Restore IT's settings from IT->stack. Called, for example, when no
5224 more overlay strings must be processed, and we return to delivering
5225 display elements from a buffer, or when the end of a string from a
5226 `display' property is reached and we return to delivering display
5227 elements from an overlay string, or from a buffer. */
5229 static void
5230 pop_it (struct it *it)
5232 struct iterator_stack_entry *p;
5234 xassert (it->sp > 0);
5235 --it->sp;
5236 p = it->stack + it->sp;
5237 it->stop_charpos = p->stop_charpos;
5238 it->prev_stop = p->prev_stop;
5239 it->base_level_stop = p->base_level_stop;
5240 it->cmp_it = p->cmp_it;
5241 it->face_id = p->face_id;
5242 it->current = p->current;
5243 it->position = p->position;
5244 it->string = p->string;
5245 it->from_overlay = p->from_overlay;
5246 if (NILP (it->string))
5247 SET_TEXT_POS (it->current.string_pos, -1, -1);
5248 it->method = p->method;
5249 switch (it->method)
5251 case GET_FROM_IMAGE:
5252 it->image_id = p->u.image.image_id;
5253 it->object = p->u.image.object;
5254 it->slice = p->u.image.slice;
5255 break;
5256 case GET_FROM_STRETCH:
5257 it->object = p->u.comp.object;
5258 break;
5259 case GET_FROM_BUFFER:
5260 it->object = it->w->buffer;
5261 if (it->bidi_p)
5263 /* Bidi-iterate until we get out of the portion of text, if
5264 any, covered by a `display' text property or an overlay
5265 with `display' property. (We cannot just jump there,
5266 because the internal coherency of the bidi iterator state
5267 can not be preserved across such jumps.) We also must
5268 determine the paragraph base direction if the overlay we
5269 just processed is at the beginning of a new
5270 paragraph. */
5271 iterate_out_of_display_property (it);
5273 break;
5274 case GET_FROM_STRING:
5275 it->object = it->string;
5276 break;
5277 case GET_FROM_DISPLAY_VECTOR:
5278 if (it->s)
5279 it->method = GET_FROM_C_STRING;
5280 else if (STRINGP (it->string))
5281 it->method = GET_FROM_STRING;
5282 else
5284 it->method = GET_FROM_BUFFER;
5285 it->object = it->w->buffer;
5288 it->end_charpos = p->end_charpos;
5289 it->string_nchars = p->string_nchars;
5290 it->area = p->area;
5291 it->multibyte_p = p->multibyte_p;
5292 it->avoid_cursor_p = p->avoid_cursor_p;
5293 it->space_width = p->space_width;
5294 it->font_height = p->font_height;
5295 it->voffset = p->voffset;
5296 it->string_from_display_prop_p = p->string_from_display_prop_p;
5297 it->line_wrap = p->line_wrap;
5302 /***********************************************************************
5303 Moving over lines
5304 ***********************************************************************/
5306 /* Set IT's current position to the previous line start. */
5308 static void
5309 back_to_previous_line_start (struct it *it)
5311 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5312 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5316 /* Move IT to the next line start.
5318 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5319 we skipped over part of the text (as opposed to moving the iterator
5320 continuously over the text). Otherwise, don't change the value
5321 of *SKIPPED_P.
5323 Newlines may come from buffer text, overlay strings, or strings
5324 displayed via the `display' property. That's the reason we can't
5325 simply use find_next_newline_no_quit.
5327 Note that this function may not skip over invisible text that is so
5328 because of text properties and immediately follows a newline. If
5329 it would, function reseat_at_next_visible_line_start, when called
5330 from set_iterator_to_next, would effectively make invisible
5331 characters following a newline part of the wrong glyph row, which
5332 leads to wrong cursor motion. */
5334 static int
5335 forward_to_next_line_start (struct it *it, int *skipped_p)
5337 int old_selective, newline_found_p, n;
5338 const int MAX_NEWLINE_DISTANCE = 500;
5340 /* If already on a newline, just consume it to avoid unintended
5341 skipping over invisible text below. */
5342 if (it->what == IT_CHARACTER
5343 && it->c == '\n'
5344 && CHARPOS (it->position) == IT_CHARPOS (*it))
5346 set_iterator_to_next (it, 0);
5347 it->c = 0;
5348 return 1;
5351 /* Don't handle selective display in the following. It's (a)
5352 unnecessary because it's done by the caller, and (b) leads to an
5353 infinite recursion because next_element_from_ellipsis indirectly
5354 calls this function. */
5355 old_selective = it->selective;
5356 it->selective = 0;
5358 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5359 from buffer text. */
5360 for (n = newline_found_p = 0;
5361 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5362 n += STRINGP (it->string) ? 0 : 1)
5364 if (!get_next_display_element (it))
5365 return 0;
5366 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5367 set_iterator_to_next (it, 0);
5370 /* If we didn't find a newline near enough, see if we can use a
5371 short-cut. */
5372 if (!newline_found_p)
5374 EMACS_INT start = IT_CHARPOS (*it);
5375 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5376 Lisp_Object pos;
5378 xassert (!STRINGP (it->string));
5380 /* If there isn't any `display' property in sight, and no
5381 overlays, we can just use the position of the newline in
5382 buffer text. */
5383 if (it->stop_charpos >= limit
5384 || ((pos = Fnext_single_property_change (make_number (start),
5385 Qdisplay,
5386 Qnil, make_number (limit)),
5387 NILP (pos))
5388 && next_overlay_change (start) == ZV))
5390 IT_CHARPOS (*it) = limit;
5391 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5392 *skipped_p = newline_found_p = 1;
5394 else
5396 while (get_next_display_element (it)
5397 && !newline_found_p)
5399 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5400 set_iterator_to_next (it, 0);
5405 it->selective = old_selective;
5406 return newline_found_p;
5410 /* Set IT's current position to the previous visible line start. Skip
5411 invisible text that is so either due to text properties or due to
5412 selective display. Caution: this does not change IT->current_x and
5413 IT->hpos. */
5415 static void
5416 back_to_previous_visible_line_start (struct it *it)
5418 while (IT_CHARPOS (*it) > BEGV)
5420 back_to_previous_line_start (it);
5422 if (IT_CHARPOS (*it) <= BEGV)
5423 break;
5425 /* If selective > 0, then lines indented more than its value are
5426 invisible. */
5427 if (it->selective > 0
5428 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5429 (double) it->selective)) /* iftc */
5430 continue;
5432 /* Check the newline before point for invisibility. */
5434 Lisp_Object prop;
5435 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5436 Qinvisible, it->window);
5437 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5438 continue;
5441 if (IT_CHARPOS (*it) <= BEGV)
5442 break;
5445 struct it it2;
5446 EMACS_INT pos;
5447 EMACS_INT beg, end;
5448 Lisp_Object val, overlay;
5450 /* If newline is part of a composition, continue from start of composition */
5451 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5452 && beg < IT_CHARPOS (*it))
5453 goto replaced;
5455 /* If newline is replaced by a display property, find start of overlay
5456 or interval and continue search from that point. */
5457 it2 = *it;
5458 pos = --IT_CHARPOS (it2);
5459 --IT_BYTEPOS (it2);
5460 it2.sp = 0;
5461 it2.string_from_display_prop_p = 0;
5462 if (handle_display_prop (&it2) == HANDLED_RETURN
5463 && !NILP (val = get_char_property_and_overlay
5464 (make_number (pos), Qdisplay, Qnil, &overlay))
5465 && (OVERLAYP (overlay)
5466 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5467 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5468 goto replaced;
5470 /* Newline is not replaced by anything -- so we are done. */
5471 break;
5473 replaced:
5474 if (beg < BEGV)
5475 beg = BEGV;
5476 IT_CHARPOS (*it) = beg;
5477 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5481 it->continuation_lines_width = 0;
5483 xassert (IT_CHARPOS (*it) >= BEGV);
5484 xassert (IT_CHARPOS (*it) == BEGV
5485 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5486 CHECK_IT (it);
5490 /* Reseat iterator IT at the previous visible line start. Skip
5491 invisible text that is so either due to text properties or due to
5492 selective display. At the end, update IT's overlay information,
5493 face information etc. */
5495 void
5496 reseat_at_previous_visible_line_start (struct it *it)
5498 back_to_previous_visible_line_start (it);
5499 reseat (it, it->current.pos, 1);
5500 CHECK_IT (it);
5504 /* Reseat iterator IT on the next visible line start in the current
5505 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5506 preceding the line start. Skip over invisible text that is so
5507 because of selective display. Compute faces, overlays etc at the
5508 new position. Note that this function does not skip over text that
5509 is invisible because of text properties. */
5511 static void
5512 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5514 int newline_found_p, skipped_p = 0;
5516 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5518 /* Skip over lines that are invisible because they are indented
5519 more than the value of IT->selective. */
5520 if (it->selective > 0)
5521 while (IT_CHARPOS (*it) < ZV
5522 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5523 (double) it->selective)) /* iftc */
5525 xassert (IT_BYTEPOS (*it) == BEGV
5526 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5527 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5530 /* Position on the newline if that's what's requested. */
5531 if (on_newline_p && newline_found_p)
5533 if (STRINGP (it->string))
5535 if (IT_STRING_CHARPOS (*it) > 0)
5537 --IT_STRING_CHARPOS (*it);
5538 --IT_STRING_BYTEPOS (*it);
5541 else if (IT_CHARPOS (*it) > BEGV)
5543 --IT_CHARPOS (*it);
5544 --IT_BYTEPOS (*it);
5545 reseat (it, it->current.pos, 0);
5548 else if (skipped_p)
5549 reseat (it, it->current.pos, 0);
5551 CHECK_IT (it);
5556 /***********************************************************************
5557 Changing an iterator's position
5558 ***********************************************************************/
5560 /* Change IT's current position to POS in current_buffer. If FORCE_P
5561 is non-zero, always check for text properties at the new position.
5562 Otherwise, text properties are only looked up if POS >=
5563 IT->check_charpos of a property. */
5565 static void
5566 reseat (struct it *it, struct text_pos pos, int force_p)
5568 EMACS_INT original_pos = IT_CHARPOS (*it);
5570 reseat_1 (it, pos, 0);
5572 /* Determine where to check text properties. Avoid doing it
5573 where possible because text property lookup is very expensive. */
5574 if (force_p
5575 || CHARPOS (pos) > it->stop_charpos
5576 || CHARPOS (pos) < original_pos)
5578 if (it->bidi_p)
5580 /* For bidi iteration, we need to prime prev_stop and
5581 base_level_stop with our best estimations. */
5582 if (CHARPOS (pos) < it->prev_stop)
5584 handle_stop_backwards (it, BEGV);
5585 if (CHARPOS (pos) < it->base_level_stop)
5586 it->base_level_stop = 0;
5588 else if (CHARPOS (pos) > it->stop_charpos
5589 && it->stop_charpos >= BEGV)
5590 handle_stop_backwards (it, it->stop_charpos);
5591 else /* force_p */
5592 handle_stop (it);
5594 else
5596 handle_stop (it);
5597 it->prev_stop = it->base_level_stop = 0;
5602 CHECK_IT (it);
5606 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5607 IT->stop_pos to POS, also. */
5609 static void
5610 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5612 /* Don't call this function when scanning a C string. */
5613 xassert (it->s == NULL);
5615 /* POS must be a reasonable value. */
5616 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5618 it->current.pos = it->position = pos;
5619 it->end_charpos = ZV;
5620 it->dpvec = NULL;
5621 it->current.dpvec_index = -1;
5622 it->current.overlay_string_index = -1;
5623 IT_STRING_CHARPOS (*it) = -1;
5624 IT_STRING_BYTEPOS (*it) = -1;
5625 it->string = Qnil;
5626 it->string_from_display_prop_p = 0;
5627 it->method = GET_FROM_BUFFER;
5628 it->object = it->w->buffer;
5629 it->area = TEXT_AREA;
5630 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5631 it->sp = 0;
5632 it->string_from_display_prop_p = 0;
5633 it->face_before_selective_p = 0;
5634 if (it->bidi_p)
5636 it->bidi_it.first_elt = 1;
5637 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5640 if (set_stop_p)
5642 it->stop_charpos = CHARPOS (pos);
5643 it->base_level_stop = CHARPOS (pos);
5648 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5649 If S is non-null, it is a C string to iterate over. Otherwise,
5650 STRING gives a Lisp string to iterate over.
5652 If PRECISION > 0, don't return more then PRECISION number of
5653 characters from the string.
5655 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5656 characters have been returned. FIELD_WIDTH < 0 means an infinite
5657 field width.
5659 MULTIBYTE = 0 means disable processing of multibyte characters,
5660 MULTIBYTE > 0 means enable it,
5661 MULTIBYTE < 0 means use IT->multibyte_p.
5663 IT must be initialized via a prior call to init_iterator before
5664 calling this function. */
5666 static void
5667 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5668 EMACS_INT charpos, EMACS_INT precision, int field_width,
5669 int multibyte)
5671 /* No region in strings. */
5672 it->region_beg_charpos = it->region_end_charpos = -1;
5674 /* No text property checks performed by default, but see below. */
5675 it->stop_charpos = -1;
5677 /* Set iterator position and end position. */
5678 memset (&it->current, 0, sizeof it->current);
5679 it->current.overlay_string_index = -1;
5680 it->current.dpvec_index = -1;
5681 xassert (charpos >= 0);
5683 /* If STRING is specified, use its multibyteness, otherwise use the
5684 setting of MULTIBYTE, if specified. */
5685 if (multibyte >= 0)
5686 it->multibyte_p = multibyte > 0;
5688 if (s == NULL)
5690 xassert (STRINGP (string));
5691 it->string = string;
5692 it->s = NULL;
5693 it->end_charpos = it->string_nchars = SCHARS (string);
5694 it->method = GET_FROM_STRING;
5695 it->current.string_pos = string_pos (charpos, string);
5697 else
5699 it->s = s;
5700 it->string = Qnil;
5702 /* Note that we use IT->current.pos, not it->current.string_pos,
5703 for displaying C strings. */
5704 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5705 if (it->multibyte_p)
5707 it->current.pos = c_string_pos (charpos, s, 1);
5708 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5710 else
5712 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5713 it->end_charpos = it->string_nchars = strlen (s);
5716 it->method = GET_FROM_C_STRING;
5719 /* PRECISION > 0 means don't return more than PRECISION characters
5720 from the string. */
5721 if (precision > 0 && it->end_charpos - charpos > precision)
5722 it->end_charpos = it->string_nchars = charpos + precision;
5724 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5725 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5726 FIELD_WIDTH < 0 means infinite field width. This is useful for
5727 padding with `-' at the end of a mode line. */
5728 if (field_width < 0)
5729 field_width = INFINITY;
5730 if (field_width > it->end_charpos - charpos)
5731 it->end_charpos = charpos + field_width;
5733 /* Use the standard display table for displaying strings. */
5734 if (DISP_TABLE_P (Vstandard_display_table))
5735 it->dp = XCHAR_TABLE (Vstandard_display_table);
5737 it->stop_charpos = charpos;
5738 if (s == NULL && it->multibyte_p)
5740 EMACS_INT endpos = SCHARS (it->string);
5741 if (endpos > it->end_charpos)
5742 endpos = it->end_charpos;
5743 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5744 it->string);
5746 CHECK_IT (it);
5751 /***********************************************************************
5752 Iteration
5753 ***********************************************************************/
5755 /* Map enum it_method value to corresponding next_element_from_* function. */
5757 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5759 next_element_from_buffer,
5760 next_element_from_display_vector,
5761 next_element_from_string,
5762 next_element_from_c_string,
5763 next_element_from_image,
5764 next_element_from_stretch
5767 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5770 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5771 (possibly with the following characters). */
5773 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5774 ((IT)->cmp_it.id >= 0 \
5775 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5776 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5777 END_CHARPOS, (IT)->w, \
5778 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5779 (IT)->string)))
5782 /* Lookup the char-table Vglyphless_char_display for character C (-1
5783 if we want information for no-font case), and return the display
5784 method symbol. By side-effect, update it->what and
5785 it->glyphless_method. This function is called from
5786 get_next_display_element for each character element, and from
5787 x_produce_glyphs when no suitable font was found. */
5789 Lisp_Object
5790 lookup_glyphless_char_display (int c, struct it *it)
5792 Lisp_Object glyphless_method = Qnil;
5794 if (CHAR_TABLE_P (Vglyphless_char_display)
5795 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5796 glyphless_method = (c >= 0
5797 ? CHAR_TABLE_REF (Vglyphless_char_display, c)
5798 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
5799 retry:
5800 if (NILP (glyphless_method))
5802 if (c >= 0)
5803 /* The default is to display the character by a proper font. */
5804 return Qnil;
5805 /* The default for the no-font case is to display an empty box. */
5806 glyphless_method = Qempty_box;
5808 if (EQ (glyphless_method, Qzero_width))
5810 if (c >= 0)
5811 return glyphless_method;
5812 /* This method can't be used for the no-font case. */
5813 glyphless_method = Qempty_box;
5815 if (EQ (glyphless_method, Qthin_space))
5816 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5817 else if (EQ (glyphless_method, Qempty_box))
5818 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5819 else if (EQ (glyphless_method, Qhex_code))
5820 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5821 else if (STRINGP (glyphless_method))
5822 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5823 else
5825 /* Invalid value. We use the default method. */
5826 glyphless_method = Qnil;
5827 goto retry;
5829 it->what = IT_GLYPHLESS;
5830 return glyphless_method;
5833 /* Load IT's display element fields with information about the next
5834 display element from the current position of IT. Value is zero if
5835 end of buffer (or C string) is reached. */
5837 static struct frame *last_escape_glyph_frame = NULL;
5838 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5839 static int last_escape_glyph_merged_face_id = 0;
5841 struct frame *last_glyphless_glyph_frame = NULL;
5842 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5843 int last_glyphless_glyph_merged_face_id = 0;
5846 get_next_display_element (struct it *it)
5848 /* Non-zero means that we found a display element. Zero means that
5849 we hit the end of what we iterate over. Performance note: the
5850 function pointer `method' used here turns out to be faster than
5851 using a sequence of if-statements. */
5852 int success_p;
5854 get_next:
5855 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5857 if (it->what == IT_CHARACTER)
5859 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5860 and only if (a) the resolved directionality of that character
5861 is R..." */
5862 /* FIXME: Do we need an exception for characters from display
5863 tables? */
5864 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5865 it->c = bidi_mirror_char (it->c);
5866 /* Map via display table or translate control characters.
5867 IT->c, IT->len etc. have been set to the next character by
5868 the function call above. If we have a display table, and it
5869 contains an entry for IT->c, translate it. Don't do this if
5870 IT->c itself comes from a display table, otherwise we could
5871 end up in an infinite recursion. (An alternative could be to
5872 count the recursion depth of this function and signal an
5873 error when a certain maximum depth is reached.) Is it worth
5874 it? */
5875 if (success_p && it->dpvec == NULL)
5877 Lisp_Object dv;
5878 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5879 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5880 nbsp_or_shy = char_is_other;
5881 int c = it->c; /* This is the character to display. */
5883 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5885 xassert (SINGLE_BYTE_CHAR_P (c));
5886 if (unibyte_display_via_language_environment)
5888 c = DECODE_CHAR (unibyte, c);
5889 if (c < 0)
5890 c = BYTE8_TO_CHAR (it->c);
5892 else
5893 c = BYTE8_TO_CHAR (it->c);
5896 if (it->dp
5897 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5898 VECTORP (dv)))
5900 struct Lisp_Vector *v = XVECTOR (dv);
5902 /* Return the first character from the display table
5903 entry, if not empty. If empty, don't display the
5904 current character. */
5905 if (v->size)
5907 it->dpvec_char_len = it->len;
5908 it->dpvec = v->contents;
5909 it->dpend = v->contents + v->size;
5910 it->current.dpvec_index = 0;
5911 it->dpvec_face_id = -1;
5912 it->saved_face_id = it->face_id;
5913 it->method = GET_FROM_DISPLAY_VECTOR;
5914 it->ellipsis_p = 0;
5916 else
5918 set_iterator_to_next (it, 0);
5920 goto get_next;
5923 if (! NILP (lookup_glyphless_char_display (c, it)))
5925 if (it->what == IT_GLYPHLESS)
5926 goto done;
5927 /* Don't display this character. */
5928 set_iterator_to_next (it, 0);
5929 goto get_next;
5932 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5933 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5934 : c == 0xAD ? char_is_soft_hyphen
5935 : char_is_other);
5937 /* Translate control characters into `\003' or `^C' form.
5938 Control characters coming from a display table entry are
5939 currently not translated because we use IT->dpvec to hold
5940 the translation. This could easily be changed but I
5941 don't believe that it is worth doing.
5943 NBSP and SOFT-HYPEN are property translated too.
5945 Non-printable characters and raw-byte characters are also
5946 translated to octal form. */
5947 if (((c < ' ' || c == 127) /* ASCII control chars */
5948 ? (it->area != TEXT_AREA
5949 /* In mode line, treat \n, \t like other crl chars. */
5950 || (c != '\t'
5951 && it->glyph_row
5952 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5953 || (c != '\n' && c != '\t'))
5954 : (nbsp_or_shy
5955 || CHAR_BYTE8_P (c)
5956 || ! CHAR_PRINTABLE_P (c))))
5958 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5959 or a non-printable character which must be displayed
5960 either as '\003' or as `^C' where the '\\' and '^'
5961 can be defined in the display table. Fill
5962 IT->ctl_chars with glyphs for what we have to
5963 display. Then, set IT->dpvec to these glyphs. */
5964 Lisp_Object gc;
5965 int ctl_len;
5966 int face_id, lface_id = 0 ;
5967 int escape_glyph;
5969 /* Handle control characters with ^. */
5971 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5973 int g;
5975 g = '^'; /* default glyph for Control */
5976 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5977 if (it->dp
5978 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5979 && GLYPH_CODE_CHAR_VALID_P (gc))
5981 g = GLYPH_CODE_CHAR (gc);
5982 lface_id = GLYPH_CODE_FACE (gc);
5984 if (lface_id)
5986 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5988 else if (it->f == last_escape_glyph_frame
5989 && it->face_id == last_escape_glyph_face_id)
5991 face_id = last_escape_glyph_merged_face_id;
5993 else
5995 /* Merge the escape-glyph face into the current face. */
5996 face_id = merge_faces (it->f, Qescape_glyph, 0,
5997 it->face_id);
5998 last_escape_glyph_frame = it->f;
5999 last_escape_glyph_face_id = it->face_id;
6000 last_escape_glyph_merged_face_id = face_id;
6003 XSETINT (it->ctl_chars[0], g);
6004 XSETINT (it->ctl_chars[1], c ^ 0100);
6005 ctl_len = 2;
6006 goto display_control;
6009 /* Handle non-break space in the mode where it only gets
6010 highlighting. */
6012 if (EQ (Vnobreak_char_display, Qt)
6013 && nbsp_or_shy == char_is_nbsp)
6015 /* Merge the no-break-space face into the current face. */
6016 face_id = merge_faces (it->f, Qnobreak_space, 0,
6017 it->face_id);
6019 c = ' ';
6020 XSETINT (it->ctl_chars[0], ' ');
6021 ctl_len = 1;
6022 goto display_control;
6025 /* Handle sequences that start with the "escape glyph". */
6027 /* the default escape glyph is \. */
6028 escape_glyph = '\\';
6030 if (it->dp
6031 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6032 && GLYPH_CODE_CHAR_VALID_P (gc))
6034 escape_glyph = GLYPH_CODE_CHAR (gc);
6035 lface_id = GLYPH_CODE_FACE (gc);
6037 if (lface_id)
6039 /* The display table specified a face.
6040 Merge it into face_id and also into escape_glyph. */
6041 face_id = merge_faces (it->f, Qt, lface_id,
6042 it->face_id);
6044 else if (it->f == last_escape_glyph_frame
6045 && it->face_id == last_escape_glyph_face_id)
6047 face_id = last_escape_glyph_merged_face_id;
6049 else
6051 /* Merge the escape-glyph face into the current face. */
6052 face_id = merge_faces (it->f, Qescape_glyph, 0,
6053 it->face_id);
6054 last_escape_glyph_frame = it->f;
6055 last_escape_glyph_face_id = it->face_id;
6056 last_escape_glyph_merged_face_id = face_id;
6059 /* Handle soft hyphens in the mode where they only get
6060 highlighting. */
6062 if (EQ (Vnobreak_char_display, Qt)
6063 && nbsp_or_shy == char_is_soft_hyphen)
6065 XSETINT (it->ctl_chars[0], '-');
6066 ctl_len = 1;
6067 goto display_control;
6070 /* Handle non-break space and soft hyphen
6071 with the escape glyph. */
6073 if (nbsp_or_shy)
6075 XSETINT (it->ctl_chars[0], escape_glyph);
6076 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6077 XSETINT (it->ctl_chars[1], c);
6078 ctl_len = 2;
6079 goto display_control;
6083 char str[10];
6084 int len, i;
6086 if (CHAR_BYTE8_P (c))
6087 /* Display \200 instead of \17777600. */
6088 c = CHAR_TO_BYTE8 (c);
6089 len = sprintf (str, "%03o", c);
6091 XSETINT (it->ctl_chars[0], escape_glyph);
6092 for (i = 0; i < len; i++)
6093 XSETINT (it->ctl_chars[i + 1], str[i]);
6094 ctl_len = len + 1;
6097 display_control:
6098 /* Set up IT->dpvec and return first character from it. */
6099 it->dpvec_char_len = it->len;
6100 it->dpvec = it->ctl_chars;
6101 it->dpend = it->dpvec + ctl_len;
6102 it->current.dpvec_index = 0;
6103 it->dpvec_face_id = face_id;
6104 it->saved_face_id = it->face_id;
6105 it->method = GET_FROM_DISPLAY_VECTOR;
6106 it->ellipsis_p = 0;
6107 goto get_next;
6109 it->char_to_display = c;
6111 else if (success_p)
6113 it->char_to_display = it->c;
6117 #ifdef HAVE_WINDOW_SYSTEM
6118 /* Adjust face id for a multibyte character. There are no multibyte
6119 character in unibyte text. */
6120 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6121 && it->multibyte_p
6122 && success_p
6123 && FRAME_WINDOW_P (it->f))
6125 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6127 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6129 /* Automatic composition with glyph-string. */
6130 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6132 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6134 else
6136 EMACS_INT pos = (it->s ? -1
6137 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6138 : IT_CHARPOS (*it));
6140 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6141 it->string);
6144 #endif
6146 done:
6147 /* Is this character the last one of a run of characters with
6148 box? If yes, set IT->end_of_box_run_p to 1. */
6149 if (it->face_box_p
6150 && it->s == NULL)
6152 if (it->method == GET_FROM_STRING && it->sp)
6154 int face_id = underlying_face_id (it);
6155 struct face *face = FACE_FROM_ID (it->f, face_id);
6157 if (face)
6159 if (face->box == FACE_NO_BOX)
6161 /* If the box comes from face properties in a
6162 display string, check faces in that string. */
6163 int string_face_id = face_after_it_pos (it);
6164 it->end_of_box_run_p
6165 = (FACE_FROM_ID (it->f, string_face_id)->box
6166 == FACE_NO_BOX);
6168 /* Otherwise, the box comes from the underlying face.
6169 If this is the last string character displayed, check
6170 the next buffer location. */
6171 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6172 && (it->current.overlay_string_index
6173 == it->n_overlay_strings - 1))
6175 EMACS_INT ignore;
6176 int next_face_id;
6177 struct text_pos pos = it->current.pos;
6178 INC_TEXT_POS (pos, it->multibyte_p);
6180 next_face_id = face_at_buffer_position
6181 (it->w, CHARPOS (pos), it->region_beg_charpos,
6182 it->region_end_charpos, &ignore,
6183 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6184 -1);
6185 it->end_of_box_run_p
6186 = (FACE_FROM_ID (it->f, next_face_id)->box
6187 == FACE_NO_BOX);
6191 else
6193 int face_id = face_after_it_pos (it);
6194 it->end_of_box_run_p
6195 = (face_id != it->face_id
6196 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6200 /* Value is 0 if end of buffer or string reached. */
6201 return success_p;
6205 /* Move IT to the next display element.
6207 RESEAT_P non-zero means if called on a newline in buffer text,
6208 skip to the next visible line start.
6210 Functions get_next_display_element and set_iterator_to_next are
6211 separate because I find this arrangement easier to handle than a
6212 get_next_display_element function that also increments IT's
6213 position. The way it is we can first look at an iterator's current
6214 display element, decide whether it fits on a line, and if it does,
6215 increment the iterator position. The other way around we probably
6216 would either need a flag indicating whether the iterator has to be
6217 incremented the next time, or we would have to implement a
6218 decrement position function which would not be easy to write. */
6220 void
6221 set_iterator_to_next (struct it *it, int reseat_p)
6223 /* Reset flags indicating start and end of a sequence of characters
6224 with box. Reset them at the start of this function because
6225 moving the iterator to a new position might set them. */
6226 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6228 switch (it->method)
6230 case GET_FROM_BUFFER:
6231 /* The current display element of IT is a character from
6232 current_buffer. Advance in the buffer, and maybe skip over
6233 invisible lines that are so because of selective display. */
6234 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6235 reseat_at_next_visible_line_start (it, 0);
6236 else if (it->cmp_it.id >= 0)
6238 /* We are currently getting glyphs from a composition. */
6239 int i;
6241 if (! it->bidi_p)
6243 IT_CHARPOS (*it) += it->cmp_it.nchars;
6244 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6245 if (it->cmp_it.to < it->cmp_it.nglyphs)
6247 it->cmp_it.from = it->cmp_it.to;
6249 else
6251 it->cmp_it.id = -1;
6252 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6253 IT_BYTEPOS (*it),
6254 it->end_charpos, Qnil);
6257 else if (! it->cmp_it.reversed_p)
6259 /* Composition created while scanning forward. */
6260 /* Update IT's char/byte positions to point to the first
6261 character of the next grapheme cluster, or to the
6262 character visually after the current composition. */
6263 for (i = 0; i < it->cmp_it.nchars; i++)
6264 bidi_move_to_visually_next (&it->bidi_it);
6265 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6266 IT_CHARPOS (*it) = it->bidi_it.charpos;
6268 if (it->cmp_it.to < it->cmp_it.nglyphs)
6270 /* Proceed to the next grapheme cluster. */
6271 it->cmp_it.from = it->cmp_it.to;
6273 else
6275 /* No more grapheme clusters in this composition.
6276 Find the next stop position. */
6277 EMACS_INT stop = it->end_charpos;
6278 if (it->bidi_it.scan_dir < 0)
6279 /* Now we are scanning backward and don't know
6280 where to stop. */
6281 stop = -1;
6282 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6283 IT_BYTEPOS (*it), stop, Qnil);
6286 else
6288 /* Composition created while scanning backward. */
6289 /* Update IT's char/byte positions to point to the last
6290 character of the previous grapheme cluster, or the
6291 character visually after the current composition. */
6292 for (i = 0; i < it->cmp_it.nchars; i++)
6293 bidi_move_to_visually_next (&it->bidi_it);
6294 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6295 IT_CHARPOS (*it) = it->bidi_it.charpos;
6296 if (it->cmp_it.from > 0)
6298 /* Proceed to the previous grapheme cluster. */
6299 it->cmp_it.to = it->cmp_it.from;
6301 else
6303 /* No more grapheme clusters in this composition.
6304 Find the next stop position. */
6305 EMACS_INT stop = it->end_charpos;
6306 if (it->bidi_it.scan_dir < 0)
6307 /* Now we are scanning backward and don't know
6308 where to stop. */
6309 stop = -1;
6310 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6311 IT_BYTEPOS (*it), stop, Qnil);
6315 else
6317 xassert (it->len != 0);
6319 if (!it->bidi_p)
6321 IT_BYTEPOS (*it) += it->len;
6322 IT_CHARPOS (*it) += 1;
6324 else
6326 int prev_scan_dir = it->bidi_it.scan_dir;
6327 /* If this is a new paragraph, determine its base
6328 direction (a.k.a. its base embedding level). */
6329 if (it->bidi_it.new_paragraph)
6330 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6331 bidi_move_to_visually_next (&it->bidi_it);
6332 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6333 IT_CHARPOS (*it) = it->bidi_it.charpos;
6334 if (prev_scan_dir != it->bidi_it.scan_dir)
6336 /* As the scan direction was changed, we must
6337 re-compute the stop position for composition. */
6338 EMACS_INT stop = it->end_charpos;
6339 if (it->bidi_it.scan_dir < 0)
6340 stop = -1;
6341 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6342 IT_BYTEPOS (*it), stop, Qnil);
6345 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6347 break;
6349 case GET_FROM_C_STRING:
6350 /* Current display element of IT is from a C string. */
6351 IT_BYTEPOS (*it) += it->len;
6352 IT_CHARPOS (*it) += 1;
6353 break;
6355 case GET_FROM_DISPLAY_VECTOR:
6356 /* Current display element of IT is from a display table entry.
6357 Advance in the display table definition. Reset it to null if
6358 end reached, and continue with characters from buffers/
6359 strings. */
6360 ++it->current.dpvec_index;
6362 /* Restore face of the iterator to what they were before the
6363 display vector entry (these entries may contain faces). */
6364 it->face_id = it->saved_face_id;
6366 if (it->dpvec + it->current.dpvec_index == it->dpend)
6368 int recheck_faces = it->ellipsis_p;
6370 if (it->s)
6371 it->method = GET_FROM_C_STRING;
6372 else if (STRINGP (it->string))
6373 it->method = GET_FROM_STRING;
6374 else
6376 it->method = GET_FROM_BUFFER;
6377 it->object = it->w->buffer;
6380 it->dpvec = NULL;
6381 it->current.dpvec_index = -1;
6383 /* Skip over characters which were displayed via IT->dpvec. */
6384 if (it->dpvec_char_len < 0)
6385 reseat_at_next_visible_line_start (it, 1);
6386 else if (it->dpvec_char_len > 0)
6388 if (it->method == GET_FROM_STRING
6389 && it->n_overlay_strings > 0)
6390 it->ignore_overlay_strings_at_pos_p = 1;
6391 it->len = it->dpvec_char_len;
6392 set_iterator_to_next (it, reseat_p);
6395 /* Maybe recheck faces after display vector */
6396 if (recheck_faces)
6397 it->stop_charpos = IT_CHARPOS (*it);
6399 break;
6401 case GET_FROM_STRING:
6402 /* Current display element is a character from a Lisp string. */
6403 xassert (it->s == NULL && STRINGP (it->string));
6404 if (it->cmp_it.id >= 0)
6406 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6407 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6408 if (it->cmp_it.to < it->cmp_it.nglyphs)
6409 it->cmp_it.from = it->cmp_it.to;
6410 else
6412 it->cmp_it.id = -1;
6413 composition_compute_stop_pos (&it->cmp_it,
6414 IT_STRING_CHARPOS (*it),
6415 IT_STRING_BYTEPOS (*it),
6416 it->end_charpos, it->string);
6419 else
6421 IT_STRING_BYTEPOS (*it) += it->len;
6422 IT_STRING_CHARPOS (*it) += 1;
6425 consider_string_end:
6427 if (it->current.overlay_string_index >= 0)
6429 /* IT->string is an overlay string. Advance to the
6430 next, if there is one. */
6431 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6433 it->ellipsis_p = 0;
6434 next_overlay_string (it);
6435 if (it->ellipsis_p)
6436 setup_for_ellipsis (it, 0);
6439 else
6441 /* IT->string is not an overlay string. If we reached
6442 its end, and there is something on IT->stack, proceed
6443 with what is on the stack. This can be either another
6444 string, this time an overlay string, or a buffer. */
6445 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6446 && it->sp > 0)
6448 pop_it (it);
6449 if (it->method == GET_FROM_STRING)
6450 goto consider_string_end;
6453 break;
6455 case GET_FROM_IMAGE:
6456 case GET_FROM_STRETCH:
6457 /* The position etc with which we have to proceed are on
6458 the stack. The position may be at the end of a string,
6459 if the `display' property takes up the whole string. */
6460 xassert (it->sp > 0);
6461 pop_it (it);
6462 if (it->method == GET_FROM_STRING)
6463 goto consider_string_end;
6464 break;
6466 default:
6467 /* There are no other methods defined, so this should be a bug. */
6468 abort ();
6471 xassert (it->method != GET_FROM_STRING
6472 || (STRINGP (it->string)
6473 && IT_STRING_CHARPOS (*it) >= 0));
6476 /* Load IT's display element fields with information about the next
6477 display element which comes from a display table entry or from the
6478 result of translating a control character to one of the forms `^C'
6479 or `\003'.
6481 IT->dpvec holds the glyphs to return as characters.
6482 IT->saved_face_id holds the face id before the display vector--it
6483 is restored into IT->face_id in set_iterator_to_next. */
6485 static int
6486 next_element_from_display_vector (struct it *it)
6488 Lisp_Object gc;
6490 /* Precondition. */
6491 xassert (it->dpvec && it->current.dpvec_index >= 0);
6493 it->face_id = it->saved_face_id;
6495 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6496 That seemed totally bogus - so I changed it... */
6497 gc = it->dpvec[it->current.dpvec_index];
6499 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6501 it->c = GLYPH_CODE_CHAR (gc);
6502 it->len = CHAR_BYTES (it->c);
6504 /* The entry may contain a face id to use. Such a face id is
6505 the id of a Lisp face, not a realized face. A face id of
6506 zero means no face is specified. */
6507 if (it->dpvec_face_id >= 0)
6508 it->face_id = it->dpvec_face_id;
6509 else
6511 int lface_id = GLYPH_CODE_FACE (gc);
6512 if (lface_id > 0)
6513 it->face_id = merge_faces (it->f, Qt, lface_id,
6514 it->saved_face_id);
6517 else
6518 /* Display table entry is invalid. Return a space. */
6519 it->c = ' ', it->len = 1;
6521 /* Don't change position and object of the iterator here. They are
6522 still the values of the character that had this display table
6523 entry or was translated, and that's what we want. */
6524 it->what = IT_CHARACTER;
6525 return 1;
6529 /* Load IT with the next display element from Lisp string IT->string.
6530 IT->current.string_pos is the current position within the string.
6531 If IT->current.overlay_string_index >= 0, the Lisp string is an
6532 overlay string. */
6534 static int
6535 next_element_from_string (struct it *it)
6537 struct text_pos position;
6539 xassert (STRINGP (it->string));
6540 xassert (IT_STRING_CHARPOS (*it) >= 0);
6541 position = it->current.string_pos;
6543 /* Time to check for invisible text? */
6544 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6545 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6547 handle_stop (it);
6549 /* Since a handler may have changed IT->method, we must
6550 recurse here. */
6551 return GET_NEXT_DISPLAY_ELEMENT (it);
6554 if (it->current.overlay_string_index >= 0)
6556 /* Get the next character from an overlay string. In overlay
6557 strings, There is no field width or padding with spaces to
6558 do. */
6559 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6561 it->what = IT_EOB;
6562 return 0;
6564 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6565 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6566 && next_element_from_composition (it))
6568 return 1;
6570 else if (STRING_MULTIBYTE (it->string))
6572 const unsigned char *s = (SDATA (it->string)
6573 + IT_STRING_BYTEPOS (*it));
6574 it->c = string_char_and_length (s, &it->len);
6576 else
6578 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6579 it->len = 1;
6582 else
6584 /* Get the next character from a Lisp string that is not an
6585 overlay string. Such strings come from the mode line, for
6586 example. We may have to pad with spaces, or truncate the
6587 string. See also next_element_from_c_string. */
6588 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6590 it->what = IT_EOB;
6591 return 0;
6593 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6595 /* Pad with spaces. */
6596 it->c = ' ', it->len = 1;
6597 CHARPOS (position) = BYTEPOS (position) = -1;
6599 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6600 IT_STRING_BYTEPOS (*it), it->string_nchars)
6601 && next_element_from_composition (it))
6603 return 1;
6605 else if (STRING_MULTIBYTE (it->string))
6607 const unsigned char *s = (SDATA (it->string)
6608 + IT_STRING_BYTEPOS (*it));
6609 it->c = string_char_and_length (s, &it->len);
6611 else
6613 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6614 it->len = 1;
6618 /* Record what we have and where it came from. */
6619 it->what = IT_CHARACTER;
6620 it->object = it->string;
6621 it->position = position;
6622 return 1;
6626 /* Load IT with next display element from C string IT->s.
6627 IT->string_nchars is the maximum number of characters to return
6628 from the string. IT->end_charpos may be greater than
6629 IT->string_nchars when this function is called, in which case we
6630 may have to return padding spaces. Value is zero if end of string
6631 reached, including padding spaces. */
6633 static int
6634 next_element_from_c_string (struct it *it)
6636 int success_p = 1;
6638 xassert (it->s);
6639 it->what = IT_CHARACTER;
6640 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6641 it->object = Qnil;
6643 /* IT's position can be greater IT->string_nchars in case a field
6644 width or precision has been specified when the iterator was
6645 initialized. */
6646 if (IT_CHARPOS (*it) >= it->end_charpos)
6648 /* End of the game. */
6649 it->what = IT_EOB;
6650 success_p = 0;
6652 else if (IT_CHARPOS (*it) >= it->string_nchars)
6654 /* Pad with spaces. */
6655 it->c = ' ', it->len = 1;
6656 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6658 else if (it->multibyte_p)
6659 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6660 else
6661 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6663 return success_p;
6667 /* Set up IT to return characters from an ellipsis, if appropriate.
6668 The definition of the ellipsis glyphs may come from a display table
6669 entry. This function fills IT with the first glyph from the
6670 ellipsis if an ellipsis is to be displayed. */
6672 static int
6673 next_element_from_ellipsis (struct it *it)
6675 if (it->selective_display_ellipsis_p)
6676 setup_for_ellipsis (it, it->len);
6677 else
6679 /* The face at the current position may be different from the
6680 face we find after the invisible text. Remember what it
6681 was in IT->saved_face_id, and signal that it's there by
6682 setting face_before_selective_p. */
6683 it->saved_face_id = it->face_id;
6684 it->method = GET_FROM_BUFFER;
6685 it->object = it->w->buffer;
6686 reseat_at_next_visible_line_start (it, 1);
6687 it->face_before_selective_p = 1;
6690 return GET_NEXT_DISPLAY_ELEMENT (it);
6694 /* Deliver an image display element. The iterator IT is already
6695 filled with image information (done in handle_display_prop). Value
6696 is always 1. */
6699 static int
6700 next_element_from_image (struct it *it)
6702 it->what = IT_IMAGE;
6703 it->ignore_overlay_strings_at_pos_p = 0;
6704 return 1;
6708 /* Fill iterator IT with next display element from a stretch glyph
6709 property. IT->object is the value of the text property. Value is
6710 always 1. */
6712 static int
6713 next_element_from_stretch (struct it *it)
6715 it->what = IT_STRETCH;
6716 return 1;
6719 /* Scan forward from CHARPOS in the current buffer, until we find a
6720 stop position > current IT's position. Then handle the stop
6721 position before that. This is called when we bump into a stop
6722 position while reordering bidirectional text. CHARPOS should be
6723 the last previously processed stop_pos (or BEGV, if none were
6724 processed yet) whose position is less that IT's current
6725 position. */
6727 static void
6728 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6730 EMACS_INT where_we_are = IT_CHARPOS (*it);
6731 struct display_pos save_current = it->current;
6732 struct text_pos save_position = it->position;
6733 struct text_pos pos1;
6734 EMACS_INT next_stop;
6736 /* Scan in strict logical order. */
6737 it->bidi_p = 0;
6740 it->prev_stop = charpos;
6741 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6742 reseat_1 (it, pos1, 0);
6743 compute_stop_pos (it);
6744 /* We must advance forward, right? */
6745 if (it->stop_charpos <= it->prev_stop)
6746 abort ();
6747 charpos = it->stop_charpos;
6749 while (charpos <= where_we_are);
6751 next_stop = it->stop_charpos;
6752 it->stop_charpos = it->prev_stop;
6753 it->bidi_p = 1;
6754 it->current = save_current;
6755 it->position = save_position;
6756 handle_stop (it);
6757 it->stop_charpos = next_stop;
6760 /* Load IT with the next display element from current_buffer. Value
6761 is zero if end of buffer reached. IT->stop_charpos is the next
6762 position at which to stop and check for text properties or buffer
6763 end. */
6765 static int
6766 next_element_from_buffer (struct it *it)
6768 int success_p = 1;
6770 xassert (IT_CHARPOS (*it) >= BEGV);
6772 /* With bidi reordering, the character to display might not be the
6773 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6774 we were reseat()ed to a new buffer position, which is potentially
6775 a different paragraph. */
6776 if (it->bidi_p && it->bidi_it.first_elt)
6778 it->bidi_it.charpos = IT_CHARPOS (*it);
6779 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6780 if (it->bidi_it.bytepos == ZV_BYTE)
6782 /* Nothing to do, but reset the FIRST_ELT flag, like
6783 bidi_paragraph_init does, because we are not going to
6784 call it. */
6785 it->bidi_it.first_elt = 0;
6787 else if (it->bidi_it.bytepos == BEGV_BYTE
6788 /* FIXME: Should support all Unicode line separators. */
6789 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6790 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6792 /* If we are at the beginning of a line, we can produce the
6793 next element right away. */
6794 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6795 bidi_move_to_visually_next (&it->bidi_it);
6797 else
6799 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6801 /* We need to prime the bidi iterator starting at the line's
6802 beginning, before we will be able to produce the next
6803 element. */
6804 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6805 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6806 it->bidi_it.charpos = IT_CHARPOS (*it);
6807 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6808 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6811 /* Now return to buffer position where we were asked to
6812 get the next display element, and produce that. */
6813 bidi_move_to_visually_next (&it->bidi_it);
6815 while (it->bidi_it.bytepos != orig_bytepos
6816 && it->bidi_it.bytepos < ZV_BYTE);
6819 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6820 /* Adjust IT's position information to where we ended up. */
6821 IT_CHARPOS (*it) = it->bidi_it.charpos;
6822 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6823 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6825 EMACS_INT stop = it->end_charpos;
6826 if (it->bidi_it.scan_dir < 0)
6827 stop = -1;
6828 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6829 IT_BYTEPOS (*it), stop, Qnil);
6833 if (IT_CHARPOS (*it) >= it->stop_charpos)
6835 if (IT_CHARPOS (*it) >= it->end_charpos)
6837 int overlay_strings_follow_p;
6839 /* End of the game, except when overlay strings follow that
6840 haven't been returned yet. */
6841 if (it->overlay_strings_at_end_processed_p)
6842 overlay_strings_follow_p = 0;
6843 else
6845 it->overlay_strings_at_end_processed_p = 1;
6846 overlay_strings_follow_p = get_overlay_strings (it, 0);
6849 if (overlay_strings_follow_p)
6850 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6851 else
6853 it->what = IT_EOB;
6854 it->position = it->current.pos;
6855 success_p = 0;
6858 else if (!(!it->bidi_p
6859 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6860 || IT_CHARPOS (*it) == it->stop_charpos))
6862 /* With bidi non-linear iteration, we could find ourselves
6863 far beyond the last computed stop_charpos, with several
6864 other stop positions in between that we missed. Scan
6865 them all now, in buffer's logical order, until we find
6866 and handle the last stop_charpos that precedes our
6867 current position. */
6868 handle_stop_backwards (it, it->stop_charpos);
6869 return GET_NEXT_DISPLAY_ELEMENT (it);
6871 else
6873 if (it->bidi_p)
6875 /* Take note of the stop position we just moved across,
6876 for when we will move back across it. */
6877 it->prev_stop = it->stop_charpos;
6878 /* If we are at base paragraph embedding level, take
6879 note of the last stop position seen at this
6880 level. */
6881 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6882 it->base_level_stop = it->stop_charpos;
6884 handle_stop (it);
6885 return GET_NEXT_DISPLAY_ELEMENT (it);
6888 else if (it->bidi_p
6889 /* We can sometimes back up for reasons that have nothing
6890 to do with bidi reordering. E.g., compositions. The
6891 code below is only needed when we are above the base
6892 embedding level, so test for that explicitly. */
6893 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6894 && IT_CHARPOS (*it) < it->prev_stop)
6896 if (it->base_level_stop <= 0)
6897 it->base_level_stop = BEGV;
6898 if (IT_CHARPOS (*it) < it->base_level_stop)
6899 abort ();
6900 handle_stop_backwards (it, it->base_level_stop);
6901 return GET_NEXT_DISPLAY_ELEMENT (it);
6903 else
6905 /* No face changes, overlays etc. in sight, so just return a
6906 character from current_buffer. */
6907 unsigned char *p;
6908 EMACS_INT stop;
6910 /* Maybe run the redisplay end trigger hook. Performance note:
6911 This doesn't seem to cost measurable time. */
6912 if (it->redisplay_end_trigger_charpos
6913 && it->glyph_row
6914 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6915 run_redisplay_end_trigger_hook (it);
6917 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6918 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6919 stop)
6920 && next_element_from_composition (it))
6922 return 1;
6925 /* Get the next character, maybe multibyte. */
6926 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6927 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6928 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6929 else
6930 it->c = *p, it->len = 1;
6932 /* Record what we have and where it came from. */
6933 it->what = IT_CHARACTER;
6934 it->object = it->w->buffer;
6935 it->position = it->current.pos;
6937 /* Normally we return the character found above, except when we
6938 really want to return an ellipsis for selective display. */
6939 if (it->selective)
6941 if (it->c == '\n')
6943 /* A value of selective > 0 means hide lines indented more
6944 than that number of columns. */
6945 if (it->selective > 0
6946 && IT_CHARPOS (*it) + 1 < ZV
6947 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6948 IT_BYTEPOS (*it) + 1,
6949 (double) it->selective)) /* iftc */
6951 success_p = next_element_from_ellipsis (it);
6952 it->dpvec_char_len = -1;
6955 else if (it->c == '\r' && it->selective == -1)
6957 /* A value of selective == -1 means that everything from the
6958 CR to the end of the line is invisible, with maybe an
6959 ellipsis displayed for it. */
6960 success_p = next_element_from_ellipsis (it);
6961 it->dpvec_char_len = -1;
6966 /* Value is zero if end of buffer reached. */
6967 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6968 return success_p;
6972 /* Run the redisplay end trigger hook for IT. */
6974 static void
6975 run_redisplay_end_trigger_hook (struct it *it)
6977 Lisp_Object args[3];
6979 /* IT->glyph_row should be non-null, i.e. we should be actually
6980 displaying something, or otherwise we should not run the hook. */
6981 xassert (it->glyph_row);
6983 /* Set up hook arguments. */
6984 args[0] = Qredisplay_end_trigger_functions;
6985 args[1] = it->window;
6986 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6987 it->redisplay_end_trigger_charpos = 0;
6989 /* Since we are *trying* to run these functions, don't try to run
6990 them again, even if they get an error. */
6991 it->w->redisplay_end_trigger = Qnil;
6992 Frun_hook_with_args (3, args);
6994 /* Notice if it changed the face of the character we are on. */
6995 handle_face_prop (it);
6999 /* Deliver a composition display element. Unlike the other
7000 next_element_from_XXX, this function is not registered in the array
7001 get_next_element[]. It is called from next_element_from_buffer and
7002 next_element_from_string when necessary. */
7004 static int
7005 next_element_from_composition (struct it *it)
7007 it->what = IT_COMPOSITION;
7008 it->len = it->cmp_it.nbytes;
7009 if (STRINGP (it->string))
7011 if (it->c < 0)
7013 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7014 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7015 return 0;
7017 it->position = it->current.string_pos;
7018 it->object = it->string;
7019 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7020 IT_STRING_BYTEPOS (*it), it->string);
7022 else
7024 if (it->c < 0)
7026 IT_CHARPOS (*it) += it->cmp_it.nchars;
7027 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7028 if (it->bidi_p)
7030 if (it->bidi_it.new_paragraph)
7031 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7032 /* Resync the bidi iterator with IT's new position.
7033 FIXME: this doesn't support bidirectional text. */
7034 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7035 bidi_move_to_visually_next (&it->bidi_it);
7037 return 0;
7039 it->position = it->current.pos;
7040 it->object = it->w->buffer;
7041 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7042 IT_BYTEPOS (*it), Qnil);
7044 return 1;
7049 /***********************************************************************
7050 Moving an iterator without producing glyphs
7051 ***********************************************************************/
7053 /* Check if iterator is at a position corresponding to a valid buffer
7054 position after some move_it_ call. */
7056 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7057 ((it)->method == GET_FROM_STRING \
7058 ? IT_STRING_CHARPOS (*it) == 0 \
7059 : 1)
7062 /* Move iterator IT to a specified buffer or X position within one
7063 line on the display without producing glyphs.
7065 OP should be a bit mask including some or all of these bits:
7066 MOVE_TO_X: Stop upon reaching x-position TO_X.
7067 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7068 Regardless of OP's value, stop upon reaching the end of the display line.
7070 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7071 This means, in particular, that TO_X includes window's horizontal
7072 scroll amount.
7074 The return value has several possible values that
7075 say what condition caused the scan to stop:
7077 MOVE_POS_MATCH_OR_ZV
7078 - when TO_POS or ZV was reached.
7080 MOVE_X_REACHED
7081 -when TO_X was reached before TO_POS or ZV were reached.
7083 MOVE_LINE_CONTINUED
7084 - when we reached the end of the display area and the line must
7085 be continued.
7087 MOVE_LINE_TRUNCATED
7088 - when we reached the end of the display area and the line is
7089 truncated.
7091 MOVE_NEWLINE_OR_CR
7092 - when we stopped at a line end, i.e. a newline or a CR and selective
7093 display is on. */
7095 static enum move_it_result
7096 move_it_in_display_line_to (struct it *it,
7097 EMACS_INT to_charpos, int to_x,
7098 enum move_operation_enum op)
7100 enum move_it_result result = MOVE_UNDEFINED;
7101 struct glyph_row *saved_glyph_row;
7102 struct it wrap_it, atpos_it, atx_it;
7103 int may_wrap = 0;
7104 enum it_method prev_method = it->method;
7105 EMACS_INT prev_pos = IT_CHARPOS (*it);
7107 /* Don't produce glyphs in produce_glyphs. */
7108 saved_glyph_row = it->glyph_row;
7109 it->glyph_row = NULL;
7111 /* Use wrap_it to save a copy of IT wherever a word wrap could
7112 occur. Use atpos_it to save a copy of IT at the desired buffer
7113 position, if found, so that we can scan ahead and check if the
7114 word later overshoots the window edge. Use atx_it similarly, for
7115 pixel positions. */
7116 wrap_it.sp = -1;
7117 atpos_it.sp = -1;
7118 atx_it.sp = -1;
7120 #define BUFFER_POS_REACHED_P() \
7121 ((op & MOVE_TO_POS) != 0 \
7122 && BUFFERP (it->object) \
7123 && (IT_CHARPOS (*it) == to_charpos \
7124 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7125 && (it->method == GET_FROM_BUFFER \
7126 || (it->method == GET_FROM_DISPLAY_VECTOR \
7127 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7129 /* If there's a line-/wrap-prefix, handle it. */
7130 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7131 && it->current_y < it->last_visible_y)
7132 handle_line_prefix (it);
7134 while (1)
7136 int x, i, ascent = 0, descent = 0;
7138 /* Utility macro to reset an iterator with x, ascent, and descent. */
7139 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7140 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7141 (IT)->max_descent = descent)
7143 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7144 glyph). */
7145 if ((op & MOVE_TO_POS) != 0
7146 && BUFFERP (it->object)
7147 && it->method == GET_FROM_BUFFER
7148 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7149 || (it->bidi_p
7150 && (prev_method == GET_FROM_IMAGE
7151 || prev_method == GET_FROM_STRETCH)
7152 /* Passed TO_CHARPOS from left to right. */
7153 && ((prev_pos < to_charpos
7154 && IT_CHARPOS (*it) > to_charpos)
7155 /* Passed TO_CHARPOS from right to left. */
7156 || (prev_pos > to_charpos
7157 && IT_CHARPOS (*it) < to_charpos)))))
7159 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7161 result = MOVE_POS_MATCH_OR_ZV;
7162 break;
7164 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7165 /* If wrap_it is valid, the current position might be in a
7166 word that is wrapped. So, save the iterator in
7167 atpos_it and continue to see if wrapping happens. */
7168 atpos_it = *it;
7171 prev_method = it->method;
7172 if (it->method == GET_FROM_BUFFER)
7173 prev_pos = IT_CHARPOS (*it);
7174 /* Stop when ZV reached.
7175 We used to stop here when TO_CHARPOS reached as well, but that is
7176 too soon if this glyph does not fit on this line. So we handle it
7177 explicitly below. */
7178 if (!get_next_display_element (it))
7180 result = MOVE_POS_MATCH_OR_ZV;
7181 break;
7184 if (it->line_wrap == TRUNCATE)
7186 if (BUFFER_POS_REACHED_P ())
7188 result = MOVE_POS_MATCH_OR_ZV;
7189 break;
7192 else
7194 if (it->line_wrap == WORD_WRAP)
7196 if (IT_DISPLAYING_WHITESPACE (it))
7197 may_wrap = 1;
7198 else if (may_wrap)
7200 /* We have reached a glyph that follows one or more
7201 whitespace characters. If the position is
7202 already found, we are done. */
7203 if (atpos_it.sp >= 0)
7205 *it = atpos_it;
7206 result = MOVE_POS_MATCH_OR_ZV;
7207 goto done;
7209 if (atx_it.sp >= 0)
7211 *it = atx_it;
7212 result = MOVE_X_REACHED;
7213 goto done;
7215 /* Otherwise, we can wrap here. */
7216 wrap_it = *it;
7217 may_wrap = 0;
7222 /* Remember the line height for the current line, in case
7223 the next element doesn't fit on the line. */
7224 ascent = it->max_ascent;
7225 descent = it->max_descent;
7227 /* The call to produce_glyphs will get the metrics of the
7228 display element IT is loaded with. Record the x-position
7229 before this display element, in case it doesn't fit on the
7230 line. */
7231 x = it->current_x;
7233 PRODUCE_GLYPHS (it);
7235 if (it->area != TEXT_AREA)
7237 set_iterator_to_next (it, 1);
7238 continue;
7241 /* The number of glyphs we get back in IT->nglyphs will normally
7242 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7243 character on a terminal frame, or (iii) a line end. For the
7244 second case, IT->nglyphs - 1 padding glyphs will be present.
7245 (On X frames, there is only one glyph produced for a
7246 composite character.)
7248 The behavior implemented below means, for continuation lines,
7249 that as many spaces of a TAB as fit on the current line are
7250 displayed there. For terminal frames, as many glyphs of a
7251 multi-glyph character are displayed in the current line, too.
7252 This is what the old redisplay code did, and we keep it that
7253 way. Under X, the whole shape of a complex character must
7254 fit on the line or it will be completely displayed in the
7255 next line.
7257 Note that both for tabs and padding glyphs, all glyphs have
7258 the same width. */
7259 if (it->nglyphs)
7261 /* More than one glyph or glyph doesn't fit on line. All
7262 glyphs have the same width. */
7263 int single_glyph_width = it->pixel_width / it->nglyphs;
7264 int new_x;
7265 int x_before_this_char = x;
7266 int hpos_before_this_char = it->hpos;
7268 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7270 new_x = x + single_glyph_width;
7272 /* We want to leave anything reaching TO_X to the caller. */
7273 if ((op & MOVE_TO_X) && new_x > to_x)
7275 if (BUFFER_POS_REACHED_P ())
7277 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7278 goto buffer_pos_reached;
7279 if (atpos_it.sp < 0)
7281 atpos_it = *it;
7282 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7285 else
7287 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7289 it->current_x = x;
7290 result = MOVE_X_REACHED;
7291 break;
7293 if (atx_it.sp < 0)
7295 atx_it = *it;
7296 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7301 if (/* Lines are continued. */
7302 it->line_wrap != TRUNCATE
7303 && (/* And glyph doesn't fit on the line. */
7304 new_x > it->last_visible_x
7305 /* Or it fits exactly and we're on a window
7306 system frame. */
7307 || (new_x == it->last_visible_x
7308 && FRAME_WINDOW_P (it->f))))
7310 if (/* IT->hpos == 0 means the very first glyph
7311 doesn't fit on the line, e.g. a wide image. */
7312 it->hpos == 0
7313 || (new_x == it->last_visible_x
7314 && FRAME_WINDOW_P (it->f)))
7316 ++it->hpos;
7317 it->current_x = new_x;
7319 /* The character's last glyph just barely fits
7320 in this row. */
7321 if (i == it->nglyphs - 1)
7323 /* If this is the destination position,
7324 return a position *before* it in this row,
7325 now that we know it fits in this row. */
7326 if (BUFFER_POS_REACHED_P ())
7328 if (it->line_wrap != WORD_WRAP
7329 || wrap_it.sp < 0)
7331 it->hpos = hpos_before_this_char;
7332 it->current_x = x_before_this_char;
7333 result = MOVE_POS_MATCH_OR_ZV;
7334 break;
7336 if (it->line_wrap == WORD_WRAP
7337 && atpos_it.sp < 0)
7339 atpos_it = *it;
7340 atpos_it.current_x = x_before_this_char;
7341 atpos_it.hpos = hpos_before_this_char;
7345 set_iterator_to_next (it, 1);
7346 /* On graphical terminals, newlines may
7347 "overflow" into the fringe if
7348 overflow-newline-into-fringe is non-nil.
7349 On text-only terminals, newlines may
7350 overflow into the last glyph on the
7351 display line.*/
7352 if (!FRAME_WINDOW_P (it->f)
7353 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7355 if (!get_next_display_element (it))
7357 result = MOVE_POS_MATCH_OR_ZV;
7358 break;
7360 if (BUFFER_POS_REACHED_P ())
7362 if (ITERATOR_AT_END_OF_LINE_P (it))
7363 result = MOVE_POS_MATCH_OR_ZV;
7364 else
7365 result = MOVE_LINE_CONTINUED;
7366 break;
7368 if (ITERATOR_AT_END_OF_LINE_P (it))
7370 result = MOVE_NEWLINE_OR_CR;
7371 break;
7376 else
7377 IT_RESET_X_ASCENT_DESCENT (it);
7379 if (wrap_it.sp >= 0)
7381 *it = wrap_it;
7382 atpos_it.sp = -1;
7383 atx_it.sp = -1;
7386 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7387 IT_CHARPOS (*it)));
7388 result = MOVE_LINE_CONTINUED;
7389 break;
7392 if (BUFFER_POS_REACHED_P ())
7394 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7395 goto buffer_pos_reached;
7396 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7398 atpos_it = *it;
7399 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7403 if (new_x > it->first_visible_x)
7405 /* Glyph is visible. Increment number of glyphs that
7406 would be displayed. */
7407 ++it->hpos;
7411 if (result != MOVE_UNDEFINED)
7412 break;
7414 else if (BUFFER_POS_REACHED_P ())
7416 buffer_pos_reached:
7417 IT_RESET_X_ASCENT_DESCENT (it);
7418 result = MOVE_POS_MATCH_OR_ZV;
7419 break;
7421 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7423 /* Stop when TO_X specified and reached. This check is
7424 necessary here because of lines consisting of a line end,
7425 only. The line end will not produce any glyphs and we
7426 would never get MOVE_X_REACHED. */
7427 xassert (it->nglyphs == 0);
7428 result = MOVE_X_REACHED;
7429 break;
7432 /* Is this a line end? If yes, we're done. */
7433 if (ITERATOR_AT_END_OF_LINE_P (it))
7435 result = MOVE_NEWLINE_OR_CR;
7436 break;
7439 if (it->method == GET_FROM_BUFFER)
7440 prev_pos = IT_CHARPOS (*it);
7441 /* The current display element has been consumed. Advance
7442 to the next. */
7443 set_iterator_to_next (it, 1);
7445 /* Stop if lines are truncated and IT's current x-position is
7446 past the right edge of the window now. */
7447 if (it->line_wrap == TRUNCATE
7448 && it->current_x >= it->last_visible_x)
7450 if (!FRAME_WINDOW_P (it->f)
7451 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7453 if (!get_next_display_element (it)
7454 || BUFFER_POS_REACHED_P ())
7456 result = MOVE_POS_MATCH_OR_ZV;
7457 break;
7459 if (ITERATOR_AT_END_OF_LINE_P (it))
7461 result = MOVE_NEWLINE_OR_CR;
7462 break;
7465 result = MOVE_LINE_TRUNCATED;
7466 break;
7468 #undef IT_RESET_X_ASCENT_DESCENT
7471 #undef BUFFER_POS_REACHED_P
7473 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7474 restore the saved iterator. */
7475 if (atpos_it.sp >= 0)
7476 *it = atpos_it;
7477 else if (atx_it.sp >= 0)
7478 *it = atx_it;
7480 done:
7482 /* Restore the iterator settings altered at the beginning of this
7483 function. */
7484 it->glyph_row = saved_glyph_row;
7485 return result;
7488 /* For external use. */
7489 void
7490 move_it_in_display_line (struct it *it,
7491 EMACS_INT to_charpos, int to_x,
7492 enum move_operation_enum op)
7494 if (it->line_wrap == WORD_WRAP
7495 && (op & MOVE_TO_X))
7497 struct it save_it = *it;
7498 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7499 /* When word-wrap is on, TO_X may lie past the end
7500 of a wrapped line. Then it->current is the
7501 character on the next line, so backtrack to the
7502 space before the wrap point. */
7503 if (skip == MOVE_LINE_CONTINUED)
7505 int prev_x = max (it->current_x - 1, 0);
7506 *it = save_it;
7507 move_it_in_display_line_to
7508 (it, -1, prev_x, MOVE_TO_X);
7511 else
7512 move_it_in_display_line_to (it, to_charpos, to_x, op);
7516 /* Move IT forward until it satisfies one or more of the criteria in
7517 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7519 OP is a bit-mask that specifies where to stop, and in particular,
7520 which of those four position arguments makes a difference. See the
7521 description of enum move_operation_enum.
7523 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7524 screen line, this function will set IT to the next position >
7525 TO_CHARPOS. */
7527 void
7528 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7530 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7531 int line_height, line_start_x = 0, reached = 0;
7533 for (;;)
7535 if (op & MOVE_TO_VPOS)
7537 /* If no TO_CHARPOS and no TO_X specified, stop at the
7538 start of the line TO_VPOS. */
7539 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7541 if (it->vpos == to_vpos)
7543 reached = 1;
7544 break;
7546 else
7547 skip = move_it_in_display_line_to (it, -1, -1, 0);
7549 else
7551 /* TO_VPOS >= 0 means stop at TO_X in the line at
7552 TO_VPOS, or at TO_POS, whichever comes first. */
7553 if (it->vpos == to_vpos)
7555 reached = 2;
7556 break;
7559 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7561 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7563 reached = 3;
7564 break;
7566 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7568 /* We have reached TO_X but not in the line we want. */
7569 skip = move_it_in_display_line_to (it, to_charpos,
7570 -1, MOVE_TO_POS);
7571 if (skip == MOVE_POS_MATCH_OR_ZV)
7573 reached = 4;
7574 break;
7579 else if (op & MOVE_TO_Y)
7581 struct it it_backup;
7583 if (it->line_wrap == WORD_WRAP)
7584 it_backup = *it;
7586 /* TO_Y specified means stop at TO_X in the line containing
7587 TO_Y---or at TO_CHARPOS if this is reached first. The
7588 problem is that we can't really tell whether the line
7589 contains TO_Y before we have completely scanned it, and
7590 this may skip past TO_X. What we do is to first scan to
7591 TO_X.
7593 If TO_X is not specified, use a TO_X of zero. The reason
7594 is to make the outcome of this function more predictable.
7595 If we didn't use TO_X == 0, we would stop at the end of
7596 the line which is probably not what a caller would expect
7597 to happen. */
7598 skip = move_it_in_display_line_to
7599 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7600 (MOVE_TO_X | (op & MOVE_TO_POS)));
7602 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7603 if (skip == MOVE_POS_MATCH_OR_ZV)
7604 reached = 5;
7605 else if (skip == MOVE_X_REACHED)
7607 /* If TO_X was reached, we want to know whether TO_Y is
7608 in the line. We know this is the case if the already
7609 scanned glyphs make the line tall enough. Otherwise,
7610 we must check by scanning the rest of the line. */
7611 line_height = it->max_ascent + it->max_descent;
7612 if (to_y >= it->current_y
7613 && to_y < it->current_y + line_height)
7615 reached = 6;
7616 break;
7618 it_backup = *it;
7619 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7620 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7621 op & MOVE_TO_POS);
7622 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7623 line_height = it->max_ascent + it->max_descent;
7624 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7626 if (to_y >= it->current_y
7627 && to_y < it->current_y + line_height)
7629 /* If TO_Y is in this line and TO_X was reached
7630 above, we scanned too far. We have to restore
7631 IT's settings to the ones before skipping. */
7632 *it = it_backup;
7633 reached = 6;
7635 else
7637 skip = skip2;
7638 if (skip == MOVE_POS_MATCH_OR_ZV)
7639 reached = 7;
7642 else
7644 /* Check whether TO_Y is in this line. */
7645 line_height = it->max_ascent + it->max_descent;
7646 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7648 if (to_y >= it->current_y
7649 && to_y < it->current_y + line_height)
7651 /* When word-wrap is on, TO_X may lie past the end
7652 of a wrapped line. Then it->current is the
7653 character on the next line, so backtrack to the
7654 space before the wrap point. */
7655 if (skip == MOVE_LINE_CONTINUED
7656 && it->line_wrap == WORD_WRAP)
7658 int prev_x = max (it->current_x - 1, 0);
7659 *it = it_backup;
7660 skip = move_it_in_display_line_to
7661 (it, -1, prev_x, MOVE_TO_X);
7663 reached = 6;
7667 if (reached)
7668 break;
7670 else if (BUFFERP (it->object)
7671 && (it->method == GET_FROM_BUFFER
7672 || it->method == GET_FROM_STRETCH)
7673 && IT_CHARPOS (*it) >= to_charpos)
7674 skip = MOVE_POS_MATCH_OR_ZV;
7675 else
7676 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7678 switch (skip)
7680 case MOVE_POS_MATCH_OR_ZV:
7681 reached = 8;
7682 goto out;
7684 case MOVE_NEWLINE_OR_CR:
7685 set_iterator_to_next (it, 1);
7686 it->continuation_lines_width = 0;
7687 break;
7689 case MOVE_LINE_TRUNCATED:
7690 it->continuation_lines_width = 0;
7691 reseat_at_next_visible_line_start (it, 0);
7692 if ((op & MOVE_TO_POS) != 0
7693 && IT_CHARPOS (*it) > to_charpos)
7695 reached = 9;
7696 goto out;
7698 break;
7700 case MOVE_LINE_CONTINUED:
7701 /* For continued lines ending in a tab, some of the glyphs
7702 associated with the tab are displayed on the current
7703 line. Since it->current_x does not include these glyphs,
7704 we use it->last_visible_x instead. */
7705 if (it->c == '\t')
7707 it->continuation_lines_width += it->last_visible_x;
7708 /* When moving by vpos, ensure that the iterator really
7709 advances to the next line (bug#847, bug#969). Fixme:
7710 do we need to do this in other circumstances? */
7711 if (it->current_x != it->last_visible_x
7712 && (op & MOVE_TO_VPOS)
7713 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7715 line_start_x = it->current_x + it->pixel_width
7716 - it->last_visible_x;
7717 set_iterator_to_next (it, 0);
7720 else
7721 it->continuation_lines_width += it->current_x;
7722 break;
7724 default:
7725 abort ();
7728 /* Reset/increment for the next run. */
7729 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7730 it->current_x = line_start_x;
7731 line_start_x = 0;
7732 it->hpos = 0;
7733 it->current_y += it->max_ascent + it->max_descent;
7734 ++it->vpos;
7735 last_height = it->max_ascent + it->max_descent;
7736 last_max_ascent = it->max_ascent;
7737 it->max_ascent = it->max_descent = 0;
7740 out:
7742 /* On text terminals, we may stop at the end of a line in the middle
7743 of a multi-character glyph. If the glyph itself is continued,
7744 i.e. it is actually displayed on the next line, don't treat this
7745 stopping point as valid; move to the next line instead (unless
7746 that brings us offscreen). */
7747 if (!FRAME_WINDOW_P (it->f)
7748 && op & MOVE_TO_POS
7749 && IT_CHARPOS (*it) == to_charpos
7750 && it->what == IT_CHARACTER
7751 && it->nglyphs > 1
7752 && it->line_wrap == WINDOW_WRAP
7753 && it->current_x == it->last_visible_x - 1
7754 && it->c != '\n'
7755 && it->c != '\t'
7756 && it->vpos < XFASTINT (it->w->window_end_vpos))
7758 it->continuation_lines_width += it->current_x;
7759 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7760 it->current_y += it->max_ascent + it->max_descent;
7761 ++it->vpos;
7762 last_height = it->max_ascent + it->max_descent;
7763 last_max_ascent = it->max_ascent;
7766 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7770 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7772 If DY > 0, move IT backward at least that many pixels. DY = 0
7773 means move IT backward to the preceding line start or BEGV. This
7774 function may move over more than DY pixels if IT->current_y - DY
7775 ends up in the middle of a line; in this case IT->current_y will be
7776 set to the top of the line moved to. */
7778 void
7779 move_it_vertically_backward (struct it *it, int dy)
7781 int nlines, h;
7782 struct it it2, it3;
7783 EMACS_INT start_pos;
7785 move_further_back:
7786 xassert (dy >= 0);
7788 start_pos = IT_CHARPOS (*it);
7790 /* Estimate how many newlines we must move back. */
7791 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7793 /* Set the iterator's position that many lines back. */
7794 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7795 back_to_previous_visible_line_start (it);
7797 /* Reseat the iterator here. When moving backward, we don't want
7798 reseat to skip forward over invisible text, set up the iterator
7799 to deliver from overlay strings at the new position etc. So,
7800 use reseat_1 here. */
7801 reseat_1 (it, it->current.pos, 1);
7803 /* We are now surely at a line start. */
7804 it->current_x = it->hpos = 0;
7805 it->continuation_lines_width = 0;
7807 /* Move forward and see what y-distance we moved. First move to the
7808 start of the next line so that we get its height. We need this
7809 height to be able to tell whether we reached the specified
7810 y-distance. */
7811 it2 = *it;
7812 it2.max_ascent = it2.max_descent = 0;
7815 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7816 MOVE_TO_POS | MOVE_TO_VPOS);
7818 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7819 xassert (IT_CHARPOS (*it) >= BEGV);
7820 it3 = it2;
7822 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7823 xassert (IT_CHARPOS (*it) >= BEGV);
7824 /* H is the actual vertical distance from the position in *IT
7825 and the starting position. */
7826 h = it2.current_y - it->current_y;
7827 /* NLINES is the distance in number of lines. */
7828 nlines = it2.vpos - it->vpos;
7830 /* Correct IT's y and vpos position
7831 so that they are relative to the starting point. */
7832 it->vpos -= nlines;
7833 it->current_y -= h;
7835 if (dy == 0)
7837 /* DY == 0 means move to the start of the screen line. The
7838 value of nlines is > 0 if continuation lines were involved. */
7839 if (nlines > 0)
7840 move_it_by_lines (it, nlines, 1);
7842 else
7844 /* The y-position we try to reach, relative to *IT.
7845 Note that H has been subtracted in front of the if-statement. */
7846 int target_y = it->current_y + h - dy;
7847 int y0 = it3.current_y;
7848 int y1 = line_bottom_y (&it3);
7849 int line_height = y1 - y0;
7851 /* If we did not reach target_y, try to move further backward if
7852 we can. If we moved too far backward, try to move forward. */
7853 if (target_y < it->current_y
7854 /* This is heuristic. In a window that's 3 lines high, with
7855 a line height of 13 pixels each, recentering with point
7856 on the bottom line will try to move -39/2 = 19 pixels
7857 backward. Try to avoid moving into the first line. */
7858 && (it->current_y - target_y
7859 > min (window_box_height (it->w), line_height * 2 / 3))
7860 && IT_CHARPOS (*it) > BEGV)
7862 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7863 target_y - it->current_y));
7864 dy = it->current_y - target_y;
7865 goto move_further_back;
7867 else if (target_y >= it->current_y + line_height
7868 && IT_CHARPOS (*it) < ZV)
7870 /* Should move forward by at least one line, maybe more.
7872 Note: Calling move_it_by_lines can be expensive on
7873 terminal frames, where compute_motion is used (via
7874 vmotion) to do the job, when there are very long lines
7875 and truncate-lines is nil. That's the reason for
7876 treating terminal frames specially here. */
7878 if (!FRAME_WINDOW_P (it->f))
7879 move_it_vertically (it, target_y - (it->current_y + line_height));
7880 else
7884 move_it_by_lines (it, 1, 1);
7886 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7893 /* Move IT by a specified amount of pixel lines DY. DY negative means
7894 move backwards. DY = 0 means move to start of screen line. At the
7895 end, IT will be on the start of a screen line. */
7897 void
7898 move_it_vertically (struct it *it, int dy)
7900 if (dy <= 0)
7901 move_it_vertically_backward (it, -dy);
7902 else
7904 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7905 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7906 MOVE_TO_POS | MOVE_TO_Y);
7907 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7909 /* If buffer ends in ZV without a newline, move to the start of
7910 the line to satisfy the post-condition. */
7911 if (IT_CHARPOS (*it) == ZV
7912 && ZV > BEGV
7913 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7914 move_it_by_lines (it, 0, 0);
7919 /* Move iterator IT past the end of the text line it is in. */
7921 void
7922 move_it_past_eol (struct it *it)
7924 enum move_it_result rc;
7926 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7927 if (rc == MOVE_NEWLINE_OR_CR)
7928 set_iterator_to_next (it, 0);
7932 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7933 negative means move up. DVPOS == 0 means move to the start of the
7934 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7935 NEED_Y_P is zero, IT->current_y will be left unchanged.
7937 Further optimization ideas: If we would know that IT->f doesn't use
7938 a face with proportional font, we could be faster for
7939 truncate-lines nil. */
7941 void
7942 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7945 /* The commented-out optimization uses vmotion on terminals. This
7946 gives bad results, because elements like it->what, on which
7947 callers such as pos_visible_p rely, aren't updated. */
7948 /* struct position pos;
7949 if (!FRAME_WINDOW_P (it->f))
7951 struct text_pos textpos;
7953 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7954 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7955 reseat (it, textpos, 1);
7956 it->vpos += pos.vpos;
7957 it->current_y += pos.vpos;
7959 else */
7961 if (dvpos == 0)
7963 /* DVPOS == 0 means move to the start of the screen line. */
7964 move_it_vertically_backward (it, 0);
7965 xassert (it->current_x == 0 && it->hpos == 0);
7966 /* Let next call to line_bottom_y calculate real line height */
7967 last_height = 0;
7969 else if (dvpos > 0)
7971 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7972 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7973 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7975 else
7977 struct it it2;
7978 EMACS_INT start_charpos, i;
7980 /* Start at the beginning of the screen line containing IT's
7981 position. This may actually move vertically backwards,
7982 in case of overlays, so adjust dvpos accordingly. */
7983 dvpos += it->vpos;
7984 move_it_vertically_backward (it, 0);
7985 dvpos -= it->vpos;
7987 /* Go back -DVPOS visible lines and reseat the iterator there. */
7988 start_charpos = IT_CHARPOS (*it);
7989 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7990 back_to_previous_visible_line_start (it);
7991 reseat (it, it->current.pos, 1);
7993 /* Move further back if we end up in a string or an image. */
7994 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7996 /* First try to move to start of display line. */
7997 dvpos += it->vpos;
7998 move_it_vertically_backward (it, 0);
7999 dvpos -= it->vpos;
8000 if (IT_POS_VALID_AFTER_MOVE_P (it))
8001 break;
8002 /* If start of line is still in string or image,
8003 move further back. */
8004 back_to_previous_visible_line_start (it);
8005 reseat (it, it->current.pos, 1);
8006 dvpos--;
8009 it->current_x = it->hpos = 0;
8011 /* Above call may have moved too far if continuation lines
8012 are involved. Scan forward and see if it did. */
8013 it2 = *it;
8014 it2.vpos = it2.current_y = 0;
8015 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8016 it->vpos -= it2.vpos;
8017 it->current_y -= it2.current_y;
8018 it->current_x = it->hpos = 0;
8020 /* If we moved too far back, move IT some lines forward. */
8021 if (it2.vpos > -dvpos)
8023 int delta = it2.vpos + dvpos;
8024 it2 = *it;
8025 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8026 /* Move back again if we got too far ahead. */
8027 if (IT_CHARPOS (*it) >= start_charpos)
8028 *it = it2;
8033 /* Return 1 if IT points into the middle of a display vector. */
8036 in_display_vector_p (struct it *it)
8038 return (it->method == GET_FROM_DISPLAY_VECTOR
8039 && it->current.dpvec_index > 0
8040 && it->dpvec + it->current.dpvec_index != it->dpend);
8044 /***********************************************************************
8045 Messages
8046 ***********************************************************************/
8049 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8050 to *Messages*. */
8052 void
8053 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8055 Lisp_Object args[3];
8056 Lisp_Object msg, fmt;
8057 char *buffer;
8058 EMACS_INT len;
8059 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8060 USE_SAFE_ALLOCA;
8062 /* Do nothing if called asynchronously. Inserting text into
8063 a buffer may call after-change-functions and alike and
8064 that would means running Lisp asynchronously. */
8065 if (handling_signal)
8066 return;
8068 fmt = msg = Qnil;
8069 GCPRO4 (fmt, msg, arg1, arg2);
8071 args[0] = fmt = build_string (format);
8072 args[1] = arg1;
8073 args[2] = arg2;
8074 msg = Fformat (3, args);
8076 len = SBYTES (msg) + 1;
8077 SAFE_ALLOCA (buffer, char *, len);
8078 memcpy (buffer, SDATA (msg), len);
8080 message_dolog (buffer, len - 1, 1, 0);
8081 SAFE_FREE ();
8083 UNGCPRO;
8087 /* Output a newline in the *Messages* buffer if "needs" one. */
8089 void
8090 message_log_maybe_newline (void)
8092 if (message_log_need_newline)
8093 message_dolog ("", 0, 1, 0);
8097 /* Add a string M of length NBYTES to the message log, optionally
8098 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8099 nonzero, means interpret the contents of M as multibyte. This
8100 function calls low-level routines in order to bypass text property
8101 hooks, etc. which might not be safe to run.
8103 This may GC (insert may run before/after change hooks),
8104 so the buffer M must NOT point to a Lisp string. */
8106 void
8107 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8109 if (!NILP (Vmemory_full))
8110 return;
8112 if (!NILP (Vmessage_log_max))
8114 struct buffer *oldbuf;
8115 Lisp_Object oldpoint, oldbegv, oldzv;
8116 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8117 EMACS_INT point_at_end = 0;
8118 EMACS_INT zv_at_end = 0;
8119 Lisp_Object old_deactivate_mark, tem;
8120 struct gcpro gcpro1;
8122 old_deactivate_mark = Vdeactivate_mark;
8123 oldbuf = current_buffer;
8124 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8125 current_buffer->undo_list = Qt;
8127 oldpoint = message_dolog_marker1;
8128 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8129 oldbegv = message_dolog_marker2;
8130 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8131 oldzv = message_dolog_marker3;
8132 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8133 GCPRO1 (old_deactivate_mark);
8135 if (PT == Z)
8136 point_at_end = 1;
8137 if (ZV == Z)
8138 zv_at_end = 1;
8140 BEGV = BEG;
8141 BEGV_BYTE = BEG_BYTE;
8142 ZV = Z;
8143 ZV_BYTE = Z_BYTE;
8144 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8146 /* Insert the string--maybe converting multibyte to single byte
8147 or vice versa, so that all the text fits the buffer. */
8148 if (multibyte
8149 && NILP (current_buffer->enable_multibyte_characters))
8151 EMACS_INT i;
8152 int c, char_bytes;
8153 unsigned char work[1];
8155 /* Convert a multibyte string to single-byte
8156 for the *Message* buffer. */
8157 for (i = 0; i < nbytes; i += char_bytes)
8159 c = string_char_and_length (m + i, &char_bytes);
8160 work[0] = (ASCII_CHAR_P (c)
8162 : multibyte_char_to_unibyte (c, Qnil));
8163 insert_1_both (work, 1, 1, 1, 0, 0);
8166 else if (! multibyte
8167 && ! NILP (current_buffer->enable_multibyte_characters))
8169 EMACS_INT i;
8170 int c, char_bytes;
8171 unsigned char *msg = (unsigned char *) m;
8172 unsigned char str[MAX_MULTIBYTE_LENGTH];
8173 /* Convert a single-byte string to multibyte
8174 for the *Message* buffer. */
8175 for (i = 0; i < nbytes; i++)
8177 c = msg[i];
8178 MAKE_CHAR_MULTIBYTE (c);
8179 char_bytes = CHAR_STRING (c, str);
8180 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8183 else if (nbytes)
8184 insert_1 (m, nbytes, 1, 0, 0);
8186 if (nlflag)
8188 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8189 int dup;
8190 insert_1 ("\n", 1, 1, 0, 0);
8192 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8193 this_bol = PT;
8194 this_bol_byte = PT_BYTE;
8196 /* See if this line duplicates the previous one.
8197 If so, combine duplicates. */
8198 if (this_bol > BEG)
8200 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8201 prev_bol = PT;
8202 prev_bol_byte = PT_BYTE;
8204 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8205 this_bol, this_bol_byte);
8206 if (dup)
8208 del_range_both (prev_bol, prev_bol_byte,
8209 this_bol, this_bol_byte, 0);
8210 if (dup > 1)
8212 char dupstr[40];
8213 int duplen;
8215 /* If you change this format, don't forget to also
8216 change message_log_check_duplicate. */
8217 sprintf (dupstr, " [%d times]", dup);
8218 duplen = strlen (dupstr);
8219 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8220 insert_1 (dupstr, duplen, 1, 0, 1);
8225 /* If we have more than the desired maximum number of lines
8226 in the *Messages* buffer now, delete the oldest ones.
8227 This is safe because we don't have undo in this buffer. */
8229 if (NATNUMP (Vmessage_log_max))
8231 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8232 -XFASTINT (Vmessage_log_max) - 1, 0);
8233 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8236 BEGV = XMARKER (oldbegv)->charpos;
8237 BEGV_BYTE = marker_byte_position (oldbegv);
8239 if (zv_at_end)
8241 ZV = Z;
8242 ZV_BYTE = Z_BYTE;
8244 else
8246 ZV = XMARKER (oldzv)->charpos;
8247 ZV_BYTE = marker_byte_position (oldzv);
8250 if (point_at_end)
8251 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8252 else
8253 /* We can't do Fgoto_char (oldpoint) because it will run some
8254 Lisp code. */
8255 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8256 XMARKER (oldpoint)->bytepos);
8258 UNGCPRO;
8259 unchain_marker (XMARKER (oldpoint));
8260 unchain_marker (XMARKER (oldbegv));
8261 unchain_marker (XMARKER (oldzv));
8263 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8264 set_buffer_internal (oldbuf);
8265 if (NILP (tem))
8266 windows_or_buffers_changed = old_windows_or_buffers_changed;
8267 message_log_need_newline = !nlflag;
8268 Vdeactivate_mark = old_deactivate_mark;
8273 /* We are at the end of the buffer after just having inserted a newline.
8274 (Note: We depend on the fact we won't be crossing the gap.)
8275 Check to see if the most recent message looks a lot like the previous one.
8276 Return 0 if different, 1 if the new one should just replace it, or a
8277 value N > 1 if we should also append " [N times]". */
8279 static int
8280 message_log_check_duplicate (EMACS_INT prev_bol, EMACS_INT prev_bol_byte,
8281 EMACS_INT this_bol, EMACS_INT this_bol_byte)
8283 EMACS_INT i;
8284 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8285 int seen_dots = 0;
8286 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8287 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8289 for (i = 0; i < len; i++)
8291 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8292 seen_dots = 1;
8293 if (p1[i] != p2[i])
8294 return seen_dots;
8296 p1 += len;
8297 if (*p1 == '\n')
8298 return 2;
8299 if (*p1++ == ' ' && *p1++ == '[')
8301 int n = 0;
8302 while (*p1 >= '0' && *p1 <= '9')
8303 n = n * 10 + *p1++ - '0';
8304 if (strncmp (p1, " times]\n", 8) == 0)
8305 return n+1;
8307 return 0;
8311 /* Display an echo area message M with a specified length of NBYTES
8312 bytes. The string may include null characters. If M is 0, clear
8313 out any existing message, and let the mini-buffer text show
8314 through.
8316 This may GC, so the buffer M must NOT point to a Lisp string. */
8318 void
8319 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8321 /* First flush out any partial line written with print. */
8322 message_log_maybe_newline ();
8323 if (m)
8324 message_dolog (m, nbytes, 1, multibyte);
8325 message2_nolog (m, nbytes, multibyte);
8329 /* The non-logging counterpart of message2. */
8331 void
8332 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8334 struct frame *sf = SELECTED_FRAME ();
8335 message_enable_multibyte = multibyte;
8337 if (FRAME_INITIAL_P (sf))
8339 if (noninteractive_need_newline)
8340 putc ('\n', stderr);
8341 noninteractive_need_newline = 0;
8342 if (m)
8343 fwrite (m, nbytes, 1, stderr);
8344 if (cursor_in_echo_area == 0)
8345 fprintf (stderr, "\n");
8346 fflush (stderr);
8348 /* A null message buffer means that the frame hasn't really been
8349 initialized yet. Error messages get reported properly by
8350 cmd_error, so this must be just an informative message; toss it. */
8351 else if (INTERACTIVE
8352 && sf->glyphs_initialized_p
8353 && FRAME_MESSAGE_BUF (sf))
8355 Lisp_Object mini_window;
8356 struct frame *f;
8358 /* Get the frame containing the mini-buffer
8359 that the selected frame is using. */
8360 mini_window = FRAME_MINIBUF_WINDOW (sf);
8361 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8363 FRAME_SAMPLE_VISIBILITY (f);
8364 if (FRAME_VISIBLE_P (sf)
8365 && ! FRAME_VISIBLE_P (f))
8366 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8368 if (m)
8370 set_message (m, Qnil, nbytes, multibyte);
8371 if (minibuffer_auto_raise)
8372 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8374 else
8375 clear_message (1, 1);
8377 do_pending_window_change (0);
8378 echo_area_display (1);
8379 do_pending_window_change (0);
8380 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8381 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8386 /* Display an echo area message M with a specified length of NBYTES
8387 bytes. The string may include null characters. If M is not a
8388 string, clear out any existing message, and let the mini-buffer
8389 text show through.
8391 This function cancels echoing. */
8393 void
8394 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8396 struct gcpro gcpro1;
8398 GCPRO1 (m);
8399 clear_message (1,1);
8400 cancel_echoing ();
8402 /* First flush out any partial line written with print. */
8403 message_log_maybe_newline ();
8404 if (STRINGP (m))
8406 char *buffer;
8407 USE_SAFE_ALLOCA;
8409 SAFE_ALLOCA (buffer, char *, nbytes);
8410 memcpy (buffer, SDATA (m), nbytes);
8411 message_dolog (buffer, nbytes, 1, multibyte);
8412 SAFE_FREE ();
8414 message3_nolog (m, nbytes, multibyte);
8416 UNGCPRO;
8420 /* The non-logging version of message3.
8421 This does not cancel echoing, because it is used for echoing.
8422 Perhaps we need to make a separate function for echoing
8423 and make this cancel echoing. */
8425 void
8426 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8428 struct frame *sf = SELECTED_FRAME ();
8429 message_enable_multibyte = multibyte;
8431 if (FRAME_INITIAL_P (sf))
8433 if (noninteractive_need_newline)
8434 putc ('\n', stderr);
8435 noninteractive_need_newline = 0;
8436 if (STRINGP (m))
8437 fwrite (SDATA (m), nbytes, 1, stderr);
8438 if (cursor_in_echo_area == 0)
8439 fprintf (stderr, "\n");
8440 fflush (stderr);
8442 /* A null message buffer means that the frame hasn't really been
8443 initialized yet. Error messages get reported properly by
8444 cmd_error, so this must be just an informative message; toss it. */
8445 else if (INTERACTIVE
8446 && sf->glyphs_initialized_p
8447 && FRAME_MESSAGE_BUF (sf))
8449 Lisp_Object mini_window;
8450 Lisp_Object frame;
8451 struct frame *f;
8453 /* Get the frame containing the mini-buffer
8454 that the selected frame is using. */
8455 mini_window = FRAME_MINIBUF_WINDOW (sf);
8456 frame = XWINDOW (mini_window)->frame;
8457 f = XFRAME (frame);
8459 FRAME_SAMPLE_VISIBILITY (f);
8460 if (FRAME_VISIBLE_P (sf)
8461 && !FRAME_VISIBLE_P (f))
8462 Fmake_frame_visible (frame);
8464 if (STRINGP (m) && SCHARS (m) > 0)
8466 set_message (NULL, m, nbytes, multibyte);
8467 if (minibuffer_auto_raise)
8468 Fraise_frame (frame);
8469 /* Assume we are not echoing.
8470 (If we are, echo_now will override this.) */
8471 echo_message_buffer = Qnil;
8473 else
8474 clear_message (1, 1);
8476 do_pending_window_change (0);
8477 echo_area_display (1);
8478 do_pending_window_change (0);
8479 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8480 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8485 /* Display a null-terminated echo area message M. If M is 0, clear
8486 out any existing message, and let the mini-buffer text show through.
8488 The buffer M must continue to exist until after the echo area gets
8489 cleared or some other message gets displayed there. Do not pass
8490 text that is stored in a Lisp string. Do not pass text in a buffer
8491 that was alloca'd. */
8493 void
8494 message1 (const char *m)
8496 message2 (m, (m ? strlen (m) : 0), 0);
8500 /* The non-logging counterpart of message1. */
8502 void
8503 message1_nolog (const char *m)
8505 message2_nolog (m, (m ? strlen (m) : 0), 0);
8508 /* Display a message M which contains a single %s
8509 which gets replaced with STRING. */
8511 void
8512 message_with_string (const char *m, Lisp_Object string, int log)
8514 CHECK_STRING (string);
8516 if (noninteractive)
8518 if (m)
8520 if (noninteractive_need_newline)
8521 putc ('\n', stderr);
8522 noninteractive_need_newline = 0;
8523 fprintf (stderr, m, SDATA (string));
8524 if (!cursor_in_echo_area)
8525 fprintf (stderr, "\n");
8526 fflush (stderr);
8529 else if (INTERACTIVE)
8531 /* The frame whose minibuffer we're going to display the message on.
8532 It may be larger than the selected frame, so we need
8533 to use its buffer, not the selected frame's buffer. */
8534 Lisp_Object mini_window;
8535 struct frame *f, *sf = SELECTED_FRAME ();
8537 /* Get the frame containing the minibuffer
8538 that the selected frame is using. */
8539 mini_window = FRAME_MINIBUF_WINDOW (sf);
8540 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8542 /* A null message buffer means that the frame hasn't really been
8543 initialized yet. Error messages get reported properly by
8544 cmd_error, so this must be just an informative message; toss it. */
8545 if (FRAME_MESSAGE_BUF (f))
8547 Lisp_Object args[2], message;
8548 struct gcpro gcpro1, gcpro2;
8550 args[0] = build_string (m);
8551 args[1] = message = string;
8552 GCPRO2 (args[0], message);
8553 gcpro1.nvars = 2;
8555 message = Fformat (2, args);
8557 if (log)
8558 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8559 else
8560 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8562 UNGCPRO;
8564 /* Print should start at the beginning of the message
8565 buffer next time. */
8566 message_buf_print = 0;
8572 /* Dump an informative message to the minibuf. If M is 0, clear out
8573 any existing message, and let the mini-buffer text show through. */
8575 static void
8576 vmessage (const char *m, va_list ap)
8578 if (noninteractive)
8580 if (m)
8582 if (noninteractive_need_newline)
8583 putc ('\n', stderr);
8584 noninteractive_need_newline = 0;
8585 vfprintf (stderr, m, ap);
8586 if (cursor_in_echo_area == 0)
8587 fprintf (stderr, "\n");
8588 fflush (stderr);
8591 else if (INTERACTIVE)
8593 /* The frame whose mini-buffer we're going to display the message
8594 on. It may be larger than the selected frame, so we need to
8595 use its buffer, not the selected frame's buffer. */
8596 Lisp_Object mini_window;
8597 struct frame *f, *sf = SELECTED_FRAME ();
8599 /* Get the frame containing the mini-buffer
8600 that the selected frame is using. */
8601 mini_window = FRAME_MINIBUF_WINDOW (sf);
8602 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8604 /* A null message buffer means that the frame hasn't really been
8605 initialized yet. Error messages get reported properly by
8606 cmd_error, so this must be just an informative message; toss
8607 it. */
8608 if (FRAME_MESSAGE_BUF (f))
8610 if (m)
8612 EMACS_INT len;
8614 len = doprnt (FRAME_MESSAGE_BUF (f),
8615 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8617 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8619 else
8620 message1 (0);
8622 /* Print should start at the beginning of the message
8623 buffer next time. */
8624 message_buf_print = 0;
8629 void
8630 message (const char *m, ...)
8632 va_list ap;
8633 va_start (ap, m);
8634 vmessage (m, ap);
8635 va_end (ap);
8639 /* The non-logging version of message. */
8641 void
8642 message_nolog (const char *m, ...)
8644 Lisp_Object old_log_max;
8645 va_list ap;
8646 va_start (ap, m);
8647 old_log_max = Vmessage_log_max;
8648 Vmessage_log_max = Qnil;
8649 vmessage (m, ap);
8650 Vmessage_log_max = old_log_max;
8651 va_end (ap);
8655 /* Display the current message in the current mini-buffer. This is
8656 only called from error handlers in process.c, and is not time
8657 critical. */
8659 void
8660 update_echo_area (void)
8662 if (!NILP (echo_area_buffer[0]))
8664 Lisp_Object string;
8665 string = Fcurrent_message ();
8666 message3 (string, SBYTES (string),
8667 !NILP (current_buffer->enable_multibyte_characters));
8672 /* Make sure echo area buffers in `echo_buffers' are live.
8673 If they aren't, make new ones. */
8675 static void
8676 ensure_echo_area_buffers (void)
8678 int i;
8680 for (i = 0; i < 2; ++i)
8681 if (!BUFFERP (echo_buffer[i])
8682 || NILP (XBUFFER (echo_buffer[i])->name))
8684 char name[30];
8685 Lisp_Object old_buffer;
8686 int j;
8688 old_buffer = echo_buffer[i];
8689 sprintf (name, " *Echo Area %d*", i);
8690 echo_buffer[i] = Fget_buffer_create (build_string (name));
8691 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8692 /* to force word wrap in echo area -
8693 it was decided to postpone this*/
8694 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8696 for (j = 0; j < 2; ++j)
8697 if (EQ (old_buffer, echo_area_buffer[j]))
8698 echo_area_buffer[j] = echo_buffer[i];
8703 /* Call FN with args A1..A4 with either the current or last displayed
8704 echo_area_buffer as current buffer.
8706 WHICH zero means use the current message buffer
8707 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8708 from echo_buffer[] and clear it.
8710 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8711 suitable buffer from echo_buffer[] and clear it.
8713 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8714 that the current message becomes the last displayed one, make
8715 choose a suitable buffer for echo_area_buffer[0], and clear it.
8717 Value is what FN returns. */
8719 static int
8720 with_echo_area_buffer (struct window *w, int which,
8721 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8722 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8724 Lisp_Object buffer;
8725 int this_one, the_other, clear_buffer_p, rc;
8726 int count = SPECPDL_INDEX ();
8728 /* If buffers aren't live, make new ones. */
8729 ensure_echo_area_buffers ();
8731 clear_buffer_p = 0;
8733 if (which == 0)
8734 this_one = 0, the_other = 1;
8735 else if (which > 0)
8736 this_one = 1, the_other = 0;
8737 else
8739 this_one = 0, the_other = 1;
8740 clear_buffer_p = 1;
8742 /* We need a fresh one in case the current echo buffer equals
8743 the one containing the last displayed echo area message. */
8744 if (!NILP (echo_area_buffer[this_one])
8745 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8746 echo_area_buffer[this_one] = Qnil;
8749 /* Choose a suitable buffer from echo_buffer[] is we don't
8750 have one. */
8751 if (NILP (echo_area_buffer[this_one]))
8753 echo_area_buffer[this_one]
8754 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8755 ? echo_buffer[the_other]
8756 : echo_buffer[this_one]);
8757 clear_buffer_p = 1;
8760 buffer = echo_area_buffer[this_one];
8762 /* Don't get confused by reusing the buffer used for echoing
8763 for a different purpose. */
8764 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8765 cancel_echoing ();
8767 record_unwind_protect (unwind_with_echo_area_buffer,
8768 with_echo_area_buffer_unwind_data (w));
8770 /* Make the echo area buffer current. Note that for display
8771 purposes, it is not necessary that the displayed window's buffer
8772 == current_buffer, except for text property lookup. So, let's
8773 only set that buffer temporarily here without doing a full
8774 Fset_window_buffer. We must also change w->pointm, though,
8775 because otherwise an assertions in unshow_buffer fails, and Emacs
8776 aborts. */
8777 set_buffer_internal_1 (XBUFFER (buffer));
8778 if (w)
8780 w->buffer = buffer;
8781 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8784 current_buffer->undo_list = Qt;
8785 current_buffer->read_only = Qnil;
8786 specbind (Qinhibit_read_only, Qt);
8787 specbind (Qinhibit_modification_hooks, Qt);
8789 if (clear_buffer_p && Z > BEG)
8790 del_range (BEG, Z);
8792 xassert (BEGV >= BEG);
8793 xassert (ZV <= Z && ZV >= BEGV);
8795 rc = fn (a1, a2, a3, a4);
8797 xassert (BEGV >= BEG);
8798 xassert (ZV <= Z && ZV >= BEGV);
8800 unbind_to (count, Qnil);
8801 return rc;
8805 /* Save state that should be preserved around the call to the function
8806 FN called in with_echo_area_buffer. */
8808 static Lisp_Object
8809 with_echo_area_buffer_unwind_data (struct window *w)
8811 int i = 0;
8812 Lisp_Object vector, tmp;
8814 /* Reduce consing by keeping one vector in
8815 Vwith_echo_area_save_vector. */
8816 vector = Vwith_echo_area_save_vector;
8817 Vwith_echo_area_save_vector = Qnil;
8819 if (NILP (vector))
8820 vector = Fmake_vector (make_number (7), Qnil);
8822 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8823 ASET (vector, i, Vdeactivate_mark); ++i;
8824 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8826 if (w)
8828 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8829 ASET (vector, i, w->buffer); ++i;
8830 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8831 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8833 else
8835 int end = i + 4;
8836 for (; i < end; ++i)
8837 ASET (vector, i, Qnil);
8840 xassert (i == ASIZE (vector));
8841 return vector;
8845 /* Restore global state from VECTOR which was created by
8846 with_echo_area_buffer_unwind_data. */
8848 static Lisp_Object
8849 unwind_with_echo_area_buffer (Lisp_Object vector)
8851 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8852 Vdeactivate_mark = AREF (vector, 1);
8853 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8855 if (WINDOWP (AREF (vector, 3)))
8857 struct window *w;
8858 Lisp_Object buffer, charpos, bytepos;
8860 w = XWINDOW (AREF (vector, 3));
8861 buffer = AREF (vector, 4);
8862 charpos = AREF (vector, 5);
8863 bytepos = AREF (vector, 6);
8865 w->buffer = buffer;
8866 set_marker_both (w->pointm, buffer,
8867 XFASTINT (charpos), XFASTINT (bytepos));
8870 Vwith_echo_area_save_vector = vector;
8871 return Qnil;
8875 /* Set up the echo area for use by print functions. MULTIBYTE_P
8876 non-zero means we will print multibyte. */
8878 void
8879 setup_echo_area_for_printing (int multibyte_p)
8881 /* If we can't find an echo area any more, exit. */
8882 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8883 Fkill_emacs (Qnil);
8885 ensure_echo_area_buffers ();
8887 if (!message_buf_print)
8889 /* A message has been output since the last time we printed.
8890 Choose a fresh echo area buffer. */
8891 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8892 echo_area_buffer[0] = echo_buffer[1];
8893 else
8894 echo_area_buffer[0] = echo_buffer[0];
8896 /* Switch to that buffer and clear it. */
8897 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8898 current_buffer->truncate_lines = Qnil;
8900 if (Z > BEG)
8902 int count = SPECPDL_INDEX ();
8903 specbind (Qinhibit_read_only, Qt);
8904 /* Note that undo recording is always disabled. */
8905 del_range (BEG, Z);
8906 unbind_to (count, Qnil);
8908 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8910 /* Set up the buffer for the multibyteness we need. */
8911 if (multibyte_p
8912 != !NILP (current_buffer->enable_multibyte_characters))
8913 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8915 /* Raise the frame containing the echo area. */
8916 if (minibuffer_auto_raise)
8918 struct frame *sf = SELECTED_FRAME ();
8919 Lisp_Object mini_window;
8920 mini_window = FRAME_MINIBUF_WINDOW (sf);
8921 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8924 message_log_maybe_newline ();
8925 message_buf_print = 1;
8927 else
8929 if (NILP (echo_area_buffer[0]))
8931 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8932 echo_area_buffer[0] = echo_buffer[1];
8933 else
8934 echo_area_buffer[0] = echo_buffer[0];
8937 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8939 /* Someone switched buffers between print requests. */
8940 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8941 current_buffer->truncate_lines = Qnil;
8947 /* Display an echo area message in window W. Value is non-zero if W's
8948 height is changed. If display_last_displayed_message_p is
8949 non-zero, display the message that was last displayed, otherwise
8950 display the current message. */
8952 static int
8953 display_echo_area (struct window *w)
8955 int i, no_message_p, window_height_changed_p, count;
8957 /* Temporarily disable garbage collections while displaying the echo
8958 area. This is done because a GC can print a message itself.
8959 That message would modify the echo area buffer's contents while a
8960 redisplay of the buffer is going on, and seriously confuse
8961 redisplay. */
8962 count = inhibit_garbage_collection ();
8964 /* If there is no message, we must call display_echo_area_1
8965 nevertheless because it resizes the window. But we will have to
8966 reset the echo_area_buffer in question to nil at the end because
8967 with_echo_area_buffer will sets it to an empty buffer. */
8968 i = display_last_displayed_message_p ? 1 : 0;
8969 no_message_p = NILP (echo_area_buffer[i]);
8971 window_height_changed_p
8972 = with_echo_area_buffer (w, display_last_displayed_message_p,
8973 display_echo_area_1,
8974 (EMACS_INT) w, Qnil, 0, 0);
8976 if (no_message_p)
8977 echo_area_buffer[i] = Qnil;
8979 unbind_to (count, Qnil);
8980 return window_height_changed_p;
8984 /* Helper for display_echo_area. Display the current buffer which
8985 contains the current echo area message in window W, a mini-window,
8986 a pointer to which is passed in A1. A2..A4 are currently not used.
8987 Change the height of W so that all of the message is displayed.
8988 Value is non-zero if height of W was changed. */
8990 static int
8991 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8993 struct window *w = (struct window *) a1;
8994 Lisp_Object window;
8995 struct text_pos start;
8996 int window_height_changed_p = 0;
8998 /* Do this before displaying, so that we have a large enough glyph
8999 matrix for the display. If we can't get enough space for the
9000 whole text, display the last N lines. That works by setting w->start. */
9001 window_height_changed_p = resize_mini_window (w, 0);
9003 /* Use the starting position chosen by resize_mini_window. */
9004 SET_TEXT_POS_FROM_MARKER (start, w->start);
9006 /* Display. */
9007 clear_glyph_matrix (w->desired_matrix);
9008 XSETWINDOW (window, w);
9009 try_window (window, start, 0);
9011 return window_height_changed_p;
9015 /* Resize the echo area window to exactly the size needed for the
9016 currently displayed message, if there is one. If a mini-buffer
9017 is active, don't shrink it. */
9019 void
9020 resize_echo_area_exactly (void)
9022 if (BUFFERP (echo_area_buffer[0])
9023 && WINDOWP (echo_area_window))
9025 struct window *w = XWINDOW (echo_area_window);
9026 int resized_p;
9027 Lisp_Object resize_exactly;
9029 if (minibuf_level == 0)
9030 resize_exactly = Qt;
9031 else
9032 resize_exactly = Qnil;
9034 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9035 (EMACS_INT) w, resize_exactly, 0, 0);
9036 if (resized_p)
9038 ++windows_or_buffers_changed;
9039 ++update_mode_lines;
9040 redisplay_internal (0);
9046 /* Callback function for with_echo_area_buffer, when used from
9047 resize_echo_area_exactly. A1 contains a pointer to the window to
9048 resize, EXACTLY non-nil means resize the mini-window exactly to the
9049 size of the text displayed. A3 and A4 are not used. Value is what
9050 resize_mini_window returns. */
9052 static int
9053 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9055 return resize_mini_window ((struct window *) a1, !NILP (exactly));
9059 /* Resize mini-window W to fit the size of its contents. EXACT_P
9060 means size the window exactly to the size needed. Otherwise, it's
9061 only enlarged until W's buffer is empty.
9063 Set W->start to the right place to begin display. If the whole
9064 contents fit, start at the beginning. Otherwise, start so as
9065 to make the end of the contents appear. This is particularly
9066 important for y-or-n-p, but seems desirable generally.
9068 Value is non-zero if the window height has been changed. */
9071 resize_mini_window (struct window *w, int exact_p)
9073 struct frame *f = XFRAME (w->frame);
9074 int window_height_changed_p = 0;
9076 xassert (MINI_WINDOW_P (w));
9078 /* By default, start display at the beginning. */
9079 set_marker_both (w->start, w->buffer,
9080 BUF_BEGV (XBUFFER (w->buffer)),
9081 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9083 /* Don't resize windows while redisplaying a window; it would
9084 confuse redisplay functions when the size of the window they are
9085 displaying changes from under them. Such a resizing can happen,
9086 for instance, when which-func prints a long message while
9087 we are running fontification-functions. We're running these
9088 functions with safe_call which binds inhibit-redisplay to t. */
9089 if (!NILP (Vinhibit_redisplay))
9090 return 0;
9092 /* Nil means don't try to resize. */
9093 if (NILP (Vresize_mini_windows)
9094 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9095 return 0;
9097 if (!FRAME_MINIBUF_ONLY_P (f))
9099 struct it it;
9100 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9101 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9102 int height, max_height;
9103 int unit = FRAME_LINE_HEIGHT (f);
9104 struct text_pos start;
9105 struct buffer *old_current_buffer = NULL;
9107 if (current_buffer != XBUFFER (w->buffer))
9109 old_current_buffer = current_buffer;
9110 set_buffer_internal (XBUFFER (w->buffer));
9113 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9115 /* Compute the max. number of lines specified by the user. */
9116 if (FLOATP (Vmax_mini_window_height))
9117 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9118 else if (INTEGERP (Vmax_mini_window_height))
9119 max_height = XINT (Vmax_mini_window_height);
9120 else
9121 max_height = total_height / 4;
9123 /* Correct that max. height if it's bogus. */
9124 max_height = max (1, max_height);
9125 max_height = min (total_height, max_height);
9127 /* Find out the height of the text in the window. */
9128 if (it.line_wrap == TRUNCATE)
9129 height = 1;
9130 else
9132 last_height = 0;
9133 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9134 if (it.max_ascent == 0 && it.max_descent == 0)
9135 height = it.current_y + last_height;
9136 else
9137 height = it.current_y + it.max_ascent + it.max_descent;
9138 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9139 height = (height + unit - 1) / unit;
9142 /* Compute a suitable window start. */
9143 if (height > max_height)
9145 height = max_height;
9146 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9147 move_it_vertically_backward (&it, (height - 1) * unit);
9148 start = it.current.pos;
9150 else
9151 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9152 SET_MARKER_FROM_TEXT_POS (w->start, start);
9154 if (EQ (Vresize_mini_windows, Qgrow_only))
9156 /* Let it grow only, until we display an empty message, in which
9157 case the window shrinks again. */
9158 if (height > WINDOW_TOTAL_LINES (w))
9160 int old_height = WINDOW_TOTAL_LINES (w);
9161 freeze_window_starts (f, 1);
9162 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9163 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9165 else if (height < WINDOW_TOTAL_LINES (w)
9166 && (exact_p || BEGV == ZV))
9168 int old_height = WINDOW_TOTAL_LINES (w);
9169 freeze_window_starts (f, 0);
9170 shrink_mini_window (w);
9171 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9174 else
9176 /* Always resize to exact size needed. */
9177 if (height > WINDOW_TOTAL_LINES (w))
9179 int old_height = WINDOW_TOTAL_LINES (w);
9180 freeze_window_starts (f, 1);
9181 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9182 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9184 else if (height < WINDOW_TOTAL_LINES (w))
9186 int old_height = WINDOW_TOTAL_LINES (w);
9187 freeze_window_starts (f, 0);
9188 shrink_mini_window (w);
9190 if (height)
9192 freeze_window_starts (f, 1);
9193 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9196 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9200 if (old_current_buffer)
9201 set_buffer_internal (old_current_buffer);
9204 return window_height_changed_p;
9208 /* Value is the current message, a string, or nil if there is no
9209 current message. */
9211 Lisp_Object
9212 current_message (void)
9214 Lisp_Object msg;
9216 if (!BUFFERP (echo_area_buffer[0]))
9217 msg = Qnil;
9218 else
9220 with_echo_area_buffer (0, 0, current_message_1,
9221 (EMACS_INT) &msg, Qnil, 0, 0);
9222 if (NILP (msg))
9223 echo_area_buffer[0] = Qnil;
9226 return msg;
9230 static int
9231 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9233 Lisp_Object *msg = (Lisp_Object *) a1;
9235 if (Z > BEG)
9236 *msg = make_buffer_string (BEG, Z, 1);
9237 else
9238 *msg = Qnil;
9239 return 0;
9243 /* Push the current message on Vmessage_stack for later restauration
9244 by restore_message. Value is non-zero if the current message isn't
9245 empty. This is a relatively infrequent operation, so it's not
9246 worth optimizing. */
9249 push_message (void)
9251 Lisp_Object msg;
9252 msg = current_message ();
9253 Vmessage_stack = Fcons (msg, Vmessage_stack);
9254 return STRINGP (msg);
9258 /* Restore message display from the top of Vmessage_stack. */
9260 void
9261 restore_message (void)
9263 Lisp_Object msg;
9265 xassert (CONSP (Vmessage_stack));
9266 msg = XCAR (Vmessage_stack);
9267 if (STRINGP (msg))
9268 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9269 else
9270 message3_nolog (msg, 0, 0);
9274 /* Handler for record_unwind_protect calling pop_message. */
9276 Lisp_Object
9277 pop_message_unwind (Lisp_Object dummy)
9279 pop_message ();
9280 return Qnil;
9283 /* Pop the top-most entry off Vmessage_stack. */
9285 void
9286 pop_message (void)
9288 xassert (CONSP (Vmessage_stack));
9289 Vmessage_stack = XCDR (Vmessage_stack);
9293 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9294 exits. If the stack is not empty, we have a missing pop_message
9295 somewhere. */
9297 void
9298 check_message_stack (void)
9300 if (!NILP (Vmessage_stack))
9301 abort ();
9305 /* Truncate to NCHARS what will be displayed in the echo area the next
9306 time we display it---but don't redisplay it now. */
9308 void
9309 truncate_echo_area (EMACS_INT nchars)
9311 if (nchars == 0)
9312 echo_area_buffer[0] = Qnil;
9313 /* A null message buffer means that the frame hasn't really been
9314 initialized yet. Error messages get reported properly by
9315 cmd_error, so this must be just an informative message; toss it. */
9316 else if (!noninteractive
9317 && INTERACTIVE
9318 && !NILP (echo_area_buffer[0]))
9320 struct frame *sf = SELECTED_FRAME ();
9321 if (FRAME_MESSAGE_BUF (sf))
9322 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9327 /* Helper function for truncate_echo_area. Truncate the current
9328 message to at most NCHARS characters. */
9330 static int
9331 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9333 if (BEG + nchars < Z)
9334 del_range (BEG + nchars, Z);
9335 if (Z == BEG)
9336 echo_area_buffer[0] = Qnil;
9337 return 0;
9341 /* Set the current message to a substring of S or STRING.
9343 If STRING is a Lisp string, set the message to the first NBYTES
9344 bytes from STRING. NBYTES zero means use the whole string. If
9345 STRING is multibyte, the message will be displayed multibyte.
9347 If S is not null, set the message to the first LEN bytes of S. LEN
9348 zero means use the whole string. MULTIBYTE_P non-zero means S is
9349 multibyte. Display the message multibyte in that case.
9351 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9352 to t before calling set_message_1 (which calls insert).
9355 void
9356 set_message (const char *s, Lisp_Object string,
9357 EMACS_INT nbytes, int multibyte_p)
9359 message_enable_multibyte
9360 = ((s && multibyte_p)
9361 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9363 with_echo_area_buffer (0, -1, set_message_1,
9364 (EMACS_INT) s, string, nbytes, multibyte_p);
9365 message_buf_print = 0;
9366 help_echo_showing_p = 0;
9370 /* Helper function for set_message. Arguments have the same meaning
9371 as there, with A1 corresponding to S and A2 corresponding to STRING
9372 This function is called with the echo area buffer being
9373 current. */
9375 static int
9376 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9378 const char *s = (const char *) a1;
9379 Lisp_Object string = a2;
9381 /* Change multibyteness of the echo buffer appropriately. */
9382 if (message_enable_multibyte
9383 != !NILP (current_buffer->enable_multibyte_characters))
9384 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9386 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9388 /* Insert new message at BEG. */
9389 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9391 if (STRINGP (string))
9393 EMACS_INT nchars;
9395 if (nbytes == 0)
9396 nbytes = SBYTES (string);
9397 nchars = string_byte_to_char (string, nbytes);
9399 /* This function takes care of single/multibyte conversion. We
9400 just have to ensure that the echo area buffer has the right
9401 setting of enable_multibyte_characters. */
9402 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9404 else if (s)
9406 if (nbytes == 0)
9407 nbytes = strlen (s);
9409 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9411 /* Convert from multi-byte to single-byte. */
9412 EMACS_INT i;
9413 int c, n;
9414 unsigned char work[1];
9416 /* Convert a multibyte string to single-byte. */
9417 for (i = 0; i < nbytes; i += n)
9419 c = string_char_and_length (s + i, &n);
9420 work[0] = (ASCII_CHAR_P (c)
9422 : multibyte_char_to_unibyte (c, Qnil));
9423 insert_1_both (work, 1, 1, 1, 0, 0);
9426 else if (!multibyte_p
9427 && !NILP (current_buffer->enable_multibyte_characters))
9429 /* Convert from single-byte to multi-byte. */
9430 EMACS_INT i;
9431 int c, n;
9432 const unsigned char *msg = (const unsigned char *) s;
9433 unsigned char str[MAX_MULTIBYTE_LENGTH];
9435 /* Convert a single-byte string to multibyte. */
9436 for (i = 0; i < nbytes; i++)
9438 c = msg[i];
9439 MAKE_CHAR_MULTIBYTE (c);
9440 n = CHAR_STRING (c, str);
9441 insert_1_both (str, 1, n, 1, 0, 0);
9444 else
9445 insert_1 (s, nbytes, 1, 0, 0);
9448 return 0;
9452 /* Clear messages. CURRENT_P non-zero means clear the current
9453 message. LAST_DISPLAYED_P non-zero means clear the message
9454 last displayed. */
9456 void
9457 clear_message (int current_p, int last_displayed_p)
9459 if (current_p)
9461 echo_area_buffer[0] = Qnil;
9462 message_cleared_p = 1;
9465 if (last_displayed_p)
9466 echo_area_buffer[1] = Qnil;
9468 message_buf_print = 0;
9471 /* Clear garbaged frames.
9473 This function is used where the old redisplay called
9474 redraw_garbaged_frames which in turn called redraw_frame which in
9475 turn called clear_frame. The call to clear_frame was a source of
9476 flickering. I believe a clear_frame is not necessary. It should
9477 suffice in the new redisplay to invalidate all current matrices,
9478 and ensure a complete redisplay of all windows. */
9480 static void
9481 clear_garbaged_frames (void)
9483 if (frame_garbaged)
9485 Lisp_Object tail, frame;
9486 int changed_count = 0;
9488 FOR_EACH_FRAME (tail, frame)
9490 struct frame *f = XFRAME (frame);
9492 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9494 if (f->resized_p)
9496 Fredraw_frame (frame);
9497 f->force_flush_display_p = 1;
9499 clear_current_matrices (f);
9500 changed_count++;
9501 f->garbaged = 0;
9502 f->resized_p = 0;
9506 frame_garbaged = 0;
9507 if (changed_count)
9508 ++windows_or_buffers_changed;
9513 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9514 is non-zero update selected_frame. Value is non-zero if the
9515 mini-windows height has been changed. */
9517 static int
9518 echo_area_display (int update_frame_p)
9520 Lisp_Object mini_window;
9521 struct window *w;
9522 struct frame *f;
9523 int window_height_changed_p = 0;
9524 struct frame *sf = SELECTED_FRAME ();
9526 mini_window = FRAME_MINIBUF_WINDOW (sf);
9527 w = XWINDOW (mini_window);
9528 f = XFRAME (WINDOW_FRAME (w));
9530 /* Don't display if frame is invisible or not yet initialized. */
9531 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9532 return 0;
9534 #ifdef HAVE_WINDOW_SYSTEM
9535 /* When Emacs starts, selected_frame may be the initial terminal
9536 frame. If we let this through, a message would be displayed on
9537 the terminal. */
9538 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9539 return 0;
9540 #endif /* HAVE_WINDOW_SYSTEM */
9542 /* Redraw garbaged frames. */
9543 if (frame_garbaged)
9544 clear_garbaged_frames ();
9546 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9548 echo_area_window = mini_window;
9549 window_height_changed_p = display_echo_area (w);
9550 w->must_be_updated_p = 1;
9552 /* Update the display, unless called from redisplay_internal.
9553 Also don't update the screen during redisplay itself. The
9554 update will happen at the end of redisplay, and an update
9555 here could cause confusion. */
9556 if (update_frame_p && !redisplaying_p)
9558 int n = 0;
9560 /* If the display update has been interrupted by pending
9561 input, update mode lines in the frame. Due to the
9562 pending input, it might have been that redisplay hasn't
9563 been called, so that mode lines above the echo area are
9564 garbaged. This looks odd, so we prevent it here. */
9565 if (!display_completed)
9566 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9568 if (window_height_changed_p
9569 /* Don't do this if Emacs is shutting down. Redisplay
9570 needs to run hooks. */
9571 && !NILP (Vrun_hooks))
9573 /* Must update other windows. Likewise as in other
9574 cases, don't let this update be interrupted by
9575 pending input. */
9576 int count = SPECPDL_INDEX ();
9577 specbind (Qredisplay_dont_pause, Qt);
9578 windows_or_buffers_changed = 1;
9579 redisplay_internal (0);
9580 unbind_to (count, Qnil);
9582 else if (FRAME_WINDOW_P (f) && n == 0)
9584 /* Window configuration is the same as before.
9585 Can do with a display update of the echo area,
9586 unless we displayed some mode lines. */
9587 update_single_window (w, 1);
9588 FRAME_RIF (f)->flush_display (f);
9590 else
9591 update_frame (f, 1, 1);
9593 /* If cursor is in the echo area, make sure that the next
9594 redisplay displays the minibuffer, so that the cursor will
9595 be replaced with what the minibuffer wants. */
9596 if (cursor_in_echo_area)
9597 ++windows_or_buffers_changed;
9600 else if (!EQ (mini_window, selected_window))
9601 windows_or_buffers_changed++;
9603 /* Last displayed message is now the current message. */
9604 echo_area_buffer[1] = echo_area_buffer[0];
9605 /* Inform read_char that we're not echoing. */
9606 echo_message_buffer = Qnil;
9608 /* Prevent redisplay optimization in redisplay_internal by resetting
9609 this_line_start_pos. This is done because the mini-buffer now
9610 displays the message instead of its buffer text. */
9611 if (EQ (mini_window, selected_window))
9612 CHARPOS (this_line_start_pos) = 0;
9614 return window_height_changed_p;
9619 /***********************************************************************
9620 Mode Lines and Frame Titles
9621 ***********************************************************************/
9623 /* A buffer for constructing non-propertized mode-line strings and
9624 frame titles in it; allocated from the heap in init_xdisp and
9625 resized as needed in store_mode_line_noprop_char. */
9627 static char *mode_line_noprop_buf;
9629 /* The buffer's end, and a current output position in it. */
9631 static char *mode_line_noprop_buf_end;
9632 static char *mode_line_noprop_ptr;
9634 #define MODE_LINE_NOPROP_LEN(start) \
9635 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9637 static enum {
9638 MODE_LINE_DISPLAY = 0,
9639 MODE_LINE_TITLE,
9640 MODE_LINE_NOPROP,
9641 MODE_LINE_STRING
9642 } mode_line_target;
9644 /* Alist that caches the results of :propertize.
9645 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9646 static Lisp_Object mode_line_proptrans_alist;
9648 /* List of strings making up the mode-line. */
9649 static Lisp_Object mode_line_string_list;
9651 /* Base face property when building propertized mode line string. */
9652 static Lisp_Object mode_line_string_face;
9653 static Lisp_Object mode_line_string_face_prop;
9656 /* Unwind data for mode line strings */
9658 static Lisp_Object Vmode_line_unwind_vector;
9660 static Lisp_Object
9661 format_mode_line_unwind_data (struct buffer *obuf,
9662 Lisp_Object owin,
9663 int save_proptrans)
9665 Lisp_Object vector, tmp;
9667 /* Reduce consing by keeping one vector in
9668 Vwith_echo_area_save_vector. */
9669 vector = Vmode_line_unwind_vector;
9670 Vmode_line_unwind_vector = Qnil;
9672 if (NILP (vector))
9673 vector = Fmake_vector (make_number (8), Qnil);
9675 ASET (vector, 0, make_number (mode_line_target));
9676 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9677 ASET (vector, 2, mode_line_string_list);
9678 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9679 ASET (vector, 4, mode_line_string_face);
9680 ASET (vector, 5, mode_line_string_face_prop);
9682 if (obuf)
9683 XSETBUFFER (tmp, obuf);
9684 else
9685 tmp = Qnil;
9686 ASET (vector, 6, tmp);
9687 ASET (vector, 7, owin);
9689 return vector;
9692 static Lisp_Object
9693 unwind_format_mode_line (Lisp_Object vector)
9695 mode_line_target = XINT (AREF (vector, 0));
9696 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9697 mode_line_string_list = AREF (vector, 2);
9698 if (! EQ (AREF (vector, 3), Qt))
9699 mode_line_proptrans_alist = AREF (vector, 3);
9700 mode_line_string_face = AREF (vector, 4);
9701 mode_line_string_face_prop = AREF (vector, 5);
9703 if (!NILP (AREF (vector, 7)))
9704 /* Select window before buffer, since it may change the buffer. */
9705 Fselect_window (AREF (vector, 7), Qt);
9707 if (!NILP (AREF (vector, 6)))
9709 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9710 ASET (vector, 6, Qnil);
9713 Vmode_line_unwind_vector = vector;
9714 return Qnil;
9718 /* Store a single character C for the frame title in mode_line_noprop_buf.
9719 Re-allocate mode_line_noprop_buf if necessary. */
9721 static void
9722 store_mode_line_noprop_char (char c)
9724 /* If output position has reached the end of the allocated buffer,
9725 double the buffer's size. */
9726 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9728 int len = MODE_LINE_NOPROP_LEN (0);
9729 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9730 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9731 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9732 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9735 *mode_line_noprop_ptr++ = c;
9739 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9740 mode_line_noprop_ptr. STR is the string to store. Do not copy
9741 characters that yield more columns than PRECISION; PRECISION <= 0
9742 means copy the whole string. Pad with spaces until FIELD_WIDTH
9743 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9744 pad. Called from display_mode_element when it is used to build a
9745 frame title. */
9747 static int
9748 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9750 int n = 0;
9751 EMACS_INT dummy, nbytes;
9753 /* Copy at most PRECISION chars from STR. */
9754 nbytes = strlen (str);
9755 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9756 while (nbytes--)
9757 store_mode_line_noprop_char (*str++);
9759 /* Fill up with spaces until FIELD_WIDTH reached. */
9760 while (field_width > 0
9761 && n < field_width)
9763 store_mode_line_noprop_char (' ');
9764 ++n;
9767 return n;
9770 /***********************************************************************
9771 Frame Titles
9772 ***********************************************************************/
9774 #ifdef HAVE_WINDOW_SYSTEM
9776 /* Set the title of FRAME, if it has changed. The title format is
9777 Vicon_title_format if FRAME is iconified, otherwise it is
9778 frame_title_format. */
9780 static void
9781 x_consider_frame_title (Lisp_Object frame)
9783 struct frame *f = XFRAME (frame);
9785 if (FRAME_WINDOW_P (f)
9786 || FRAME_MINIBUF_ONLY_P (f)
9787 || f->explicit_name)
9789 /* Do we have more than one visible frame on this X display? */
9790 Lisp_Object tail;
9791 Lisp_Object fmt;
9792 int title_start;
9793 char *title;
9794 int len;
9795 struct it it;
9796 int count = SPECPDL_INDEX ();
9798 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9800 Lisp_Object other_frame = XCAR (tail);
9801 struct frame *tf = XFRAME (other_frame);
9803 if (tf != f
9804 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9805 && !FRAME_MINIBUF_ONLY_P (tf)
9806 && !EQ (other_frame, tip_frame)
9807 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9808 break;
9811 /* Set global variable indicating that multiple frames exist. */
9812 multiple_frames = CONSP (tail);
9814 /* Switch to the buffer of selected window of the frame. Set up
9815 mode_line_target so that display_mode_element will output into
9816 mode_line_noprop_buf; then display the title. */
9817 record_unwind_protect (unwind_format_mode_line,
9818 format_mode_line_unwind_data
9819 (current_buffer, selected_window, 0));
9821 Fselect_window (f->selected_window, Qt);
9822 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9823 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9825 mode_line_target = MODE_LINE_TITLE;
9826 title_start = MODE_LINE_NOPROP_LEN (0);
9827 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9828 NULL, DEFAULT_FACE_ID);
9829 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9830 len = MODE_LINE_NOPROP_LEN (title_start);
9831 title = mode_line_noprop_buf + title_start;
9832 unbind_to (count, Qnil);
9834 /* Set the title only if it's changed. This avoids consing in
9835 the common case where it hasn't. (If it turns out that we've
9836 already wasted too much time by walking through the list with
9837 display_mode_element, then we might need to optimize at a
9838 higher level than this.) */
9839 if (! STRINGP (f->name)
9840 || SBYTES (f->name) != len
9841 || memcmp (title, SDATA (f->name), len) != 0)
9842 x_implicitly_set_name (f, make_string (title, len), Qnil);
9846 #endif /* not HAVE_WINDOW_SYSTEM */
9851 /***********************************************************************
9852 Menu Bars
9853 ***********************************************************************/
9856 /* Prepare for redisplay by updating menu-bar item lists when
9857 appropriate. This can call eval. */
9859 void
9860 prepare_menu_bars (void)
9862 int all_windows;
9863 struct gcpro gcpro1, gcpro2;
9864 struct frame *f;
9865 Lisp_Object tooltip_frame;
9867 #ifdef HAVE_WINDOW_SYSTEM
9868 tooltip_frame = tip_frame;
9869 #else
9870 tooltip_frame = Qnil;
9871 #endif
9873 /* Update all frame titles based on their buffer names, etc. We do
9874 this before the menu bars so that the buffer-menu will show the
9875 up-to-date frame titles. */
9876 #ifdef HAVE_WINDOW_SYSTEM
9877 if (windows_or_buffers_changed || update_mode_lines)
9879 Lisp_Object tail, frame;
9881 FOR_EACH_FRAME (tail, frame)
9883 f = XFRAME (frame);
9884 if (!EQ (frame, tooltip_frame)
9885 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9886 x_consider_frame_title (frame);
9889 #endif /* HAVE_WINDOW_SYSTEM */
9891 /* Update the menu bar item lists, if appropriate. This has to be
9892 done before any actual redisplay or generation of display lines. */
9893 all_windows = (update_mode_lines
9894 || buffer_shared > 1
9895 || windows_or_buffers_changed);
9896 if (all_windows)
9898 Lisp_Object tail, frame;
9899 int count = SPECPDL_INDEX ();
9900 /* 1 means that update_menu_bar has run its hooks
9901 so any further calls to update_menu_bar shouldn't do so again. */
9902 int menu_bar_hooks_run = 0;
9904 record_unwind_save_match_data ();
9906 FOR_EACH_FRAME (tail, frame)
9908 f = XFRAME (frame);
9910 /* Ignore tooltip frame. */
9911 if (EQ (frame, tooltip_frame))
9912 continue;
9914 /* If a window on this frame changed size, report that to
9915 the user and clear the size-change flag. */
9916 if (FRAME_WINDOW_SIZES_CHANGED (f))
9918 Lisp_Object functions;
9920 /* Clear flag first in case we get an error below. */
9921 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9922 functions = Vwindow_size_change_functions;
9923 GCPRO2 (tail, functions);
9925 while (CONSP (functions))
9927 if (!EQ (XCAR (functions), Qt))
9928 call1 (XCAR (functions), frame);
9929 functions = XCDR (functions);
9931 UNGCPRO;
9934 GCPRO1 (tail);
9935 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9936 #ifdef HAVE_WINDOW_SYSTEM
9937 update_tool_bar (f, 0);
9938 #endif
9939 #ifdef HAVE_NS
9940 if (windows_or_buffers_changed
9941 && FRAME_NS_P (f))
9942 ns_set_doc_edited (f, Fbuffer_modified_p
9943 (XWINDOW (f->selected_window)->buffer));
9944 #endif
9945 UNGCPRO;
9948 unbind_to (count, Qnil);
9950 else
9952 struct frame *sf = SELECTED_FRAME ();
9953 update_menu_bar (sf, 1, 0);
9954 #ifdef HAVE_WINDOW_SYSTEM
9955 update_tool_bar (sf, 1);
9956 #endif
9961 /* Update the menu bar item list for frame F. This has to be done
9962 before we start to fill in any display lines, because it can call
9963 eval.
9965 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9967 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9968 already ran the menu bar hooks for this redisplay, so there
9969 is no need to run them again. The return value is the
9970 updated value of this flag, to pass to the next call. */
9972 static int
9973 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9975 Lisp_Object window;
9976 register struct window *w;
9978 /* If called recursively during a menu update, do nothing. This can
9979 happen when, for instance, an activate-menubar-hook causes a
9980 redisplay. */
9981 if (inhibit_menubar_update)
9982 return hooks_run;
9984 window = FRAME_SELECTED_WINDOW (f);
9985 w = XWINDOW (window);
9987 if (FRAME_WINDOW_P (f)
9989 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9990 || defined (HAVE_NS) || defined (USE_GTK)
9991 FRAME_EXTERNAL_MENU_BAR (f)
9992 #else
9993 FRAME_MENU_BAR_LINES (f) > 0
9994 #endif
9995 : FRAME_MENU_BAR_LINES (f) > 0)
9997 /* If the user has switched buffers or windows, we need to
9998 recompute to reflect the new bindings. But we'll
9999 recompute when update_mode_lines is set too; that means
10000 that people can use force-mode-line-update to request
10001 that the menu bar be recomputed. The adverse effect on
10002 the rest of the redisplay algorithm is about the same as
10003 windows_or_buffers_changed anyway. */
10004 if (windows_or_buffers_changed
10005 /* This used to test w->update_mode_line, but we believe
10006 there is no need to recompute the menu in that case. */
10007 || update_mode_lines
10008 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10009 < BUF_MODIFF (XBUFFER (w->buffer)))
10010 != !NILP (w->last_had_star))
10011 || ((!NILP (Vtransient_mark_mode)
10012 && !NILP (XBUFFER (w->buffer)->mark_active))
10013 != !NILP (w->region_showing)))
10015 struct buffer *prev = current_buffer;
10016 int count = SPECPDL_INDEX ();
10018 specbind (Qinhibit_menubar_update, Qt);
10020 set_buffer_internal_1 (XBUFFER (w->buffer));
10021 if (save_match_data)
10022 record_unwind_save_match_data ();
10023 if (NILP (Voverriding_local_map_menu_flag))
10025 specbind (Qoverriding_terminal_local_map, Qnil);
10026 specbind (Qoverriding_local_map, Qnil);
10029 if (!hooks_run)
10031 /* Run the Lucid hook. */
10032 safe_run_hooks (Qactivate_menubar_hook);
10034 /* If it has changed current-menubar from previous value,
10035 really recompute the menu-bar from the value. */
10036 if (! NILP (Vlucid_menu_bar_dirty_flag))
10037 call0 (Qrecompute_lucid_menubar);
10039 safe_run_hooks (Qmenu_bar_update_hook);
10041 hooks_run = 1;
10044 XSETFRAME (Vmenu_updating_frame, f);
10045 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10047 /* Redisplay the menu bar in case we changed it. */
10048 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10049 || defined (HAVE_NS) || defined (USE_GTK)
10050 if (FRAME_WINDOW_P (f))
10052 #if defined (HAVE_NS)
10053 /* All frames on Mac OS share the same menubar. So only
10054 the selected frame should be allowed to set it. */
10055 if (f == SELECTED_FRAME ())
10056 #endif
10057 set_frame_menubar (f, 0, 0);
10059 else
10060 /* On a terminal screen, the menu bar is an ordinary screen
10061 line, and this makes it get updated. */
10062 w->update_mode_line = Qt;
10063 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10064 /* In the non-toolkit version, the menu bar is an ordinary screen
10065 line, and this makes it get updated. */
10066 w->update_mode_line = Qt;
10067 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10069 unbind_to (count, Qnil);
10070 set_buffer_internal_1 (prev);
10074 return hooks_run;
10079 /***********************************************************************
10080 Output Cursor
10081 ***********************************************************************/
10083 #ifdef HAVE_WINDOW_SYSTEM
10085 /* EXPORT:
10086 Nominal cursor position -- where to draw output.
10087 HPOS and VPOS are window relative glyph matrix coordinates.
10088 X and Y are window relative pixel coordinates. */
10090 struct cursor_pos output_cursor;
10093 /* EXPORT:
10094 Set the global variable output_cursor to CURSOR. All cursor
10095 positions are relative to updated_window. */
10097 void
10098 set_output_cursor (struct cursor_pos *cursor)
10100 output_cursor.hpos = cursor->hpos;
10101 output_cursor.vpos = cursor->vpos;
10102 output_cursor.x = cursor->x;
10103 output_cursor.y = cursor->y;
10107 /* EXPORT for RIF:
10108 Set a nominal cursor position.
10110 HPOS and VPOS are column/row positions in a window glyph matrix. X
10111 and Y are window text area relative pixel positions.
10113 If this is done during an update, updated_window will contain the
10114 window that is being updated and the position is the future output
10115 cursor position for that window. If updated_window is null, use
10116 selected_window and display the cursor at the given position. */
10118 void
10119 x_cursor_to (int vpos, int hpos, int y, int x)
10121 struct window *w;
10123 /* If updated_window is not set, work on selected_window. */
10124 if (updated_window)
10125 w = updated_window;
10126 else
10127 w = XWINDOW (selected_window);
10129 /* Set the output cursor. */
10130 output_cursor.hpos = hpos;
10131 output_cursor.vpos = vpos;
10132 output_cursor.x = x;
10133 output_cursor.y = y;
10135 /* If not called as part of an update, really display the cursor.
10136 This will also set the cursor position of W. */
10137 if (updated_window == NULL)
10139 BLOCK_INPUT;
10140 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10141 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10142 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10143 UNBLOCK_INPUT;
10147 #endif /* HAVE_WINDOW_SYSTEM */
10150 /***********************************************************************
10151 Tool-bars
10152 ***********************************************************************/
10154 #ifdef HAVE_WINDOW_SYSTEM
10156 /* Where the mouse was last time we reported a mouse event. */
10158 FRAME_PTR last_mouse_frame;
10160 /* Tool-bar item index of the item on which a mouse button was pressed
10161 or -1. */
10163 int last_tool_bar_item;
10166 static Lisp_Object
10167 update_tool_bar_unwind (Lisp_Object frame)
10169 selected_frame = frame;
10170 return Qnil;
10173 /* Update the tool-bar item list for frame F. This has to be done
10174 before we start to fill in any display lines. Called from
10175 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10176 and restore it here. */
10178 static void
10179 update_tool_bar (struct frame *f, int save_match_data)
10181 #if defined (USE_GTK) || defined (HAVE_NS)
10182 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10183 #else
10184 int do_update = WINDOWP (f->tool_bar_window)
10185 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10186 #endif
10188 if (do_update)
10190 Lisp_Object window;
10191 struct window *w;
10193 window = FRAME_SELECTED_WINDOW (f);
10194 w = XWINDOW (window);
10196 /* If the user has switched buffers or windows, we need to
10197 recompute to reflect the new bindings. But we'll
10198 recompute when update_mode_lines is set too; that means
10199 that people can use force-mode-line-update to request
10200 that the menu bar be recomputed. The adverse effect on
10201 the rest of the redisplay algorithm is about the same as
10202 windows_or_buffers_changed anyway. */
10203 if (windows_or_buffers_changed
10204 || !NILP (w->update_mode_line)
10205 || update_mode_lines
10206 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10207 < BUF_MODIFF (XBUFFER (w->buffer)))
10208 != !NILP (w->last_had_star))
10209 || ((!NILP (Vtransient_mark_mode)
10210 && !NILP (XBUFFER (w->buffer)->mark_active))
10211 != !NILP (w->region_showing)))
10213 struct buffer *prev = current_buffer;
10214 int count = SPECPDL_INDEX ();
10215 Lisp_Object frame, new_tool_bar;
10216 int new_n_tool_bar;
10217 struct gcpro gcpro1;
10219 /* Set current_buffer to the buffer of the selected
10220 window of the frame, so that we get the right local
10221 keymaps. */
10222 set_buffer_internal_1 (XBUFFER (w->buffer));
10224 /* Save match data, if we must. */
10225 if (save_match_data)
10226 record_unwind_save_match_data ();
10228 /* Make sure that we don't accidentally use bogus keymaps. */
10229 if (NILP (Voverriding_local_map_menu_flag))
10231 specbind (Qoverriding_terminal_local_map, Qnil);
10232 specbind (Qoverriding_local_map, Qnil);
10235 GCPRO1 (new_tool_bar);
10237 /* We must temporarily set the selected frame to this frame
10238 before calling tool_bar_items, because the calculation of
10239 the tool-bar keymap uses the selected frame (see
10240 `tool-bar-make-keymap' in tool-bar.el). */
10241 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10242 XSETFRAME (frame, f);
10243 selected_frame = frame;
10245 /* Build desired tool-bar items from keymaps. */
10246 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10247 &new_n_tool_bar);
10249 /* Redisplay the tool-bar if we changed it. */
10250 if (new_n_tool_bar != f->n_tool_bar_items
10251 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10253 /* Redisplay that happens asynchronously due to an expose event
10254 may access f->tool_bar_items. Make sure we update both
10255 variables within BLOCK_INPUT so no such event interrupts. */
10256 BLOCK_INPUT;
10257 f->tool_bar_items = new_tool_bar;
10258 f->n_tool_bar_items = new_n_tool_bar;
10259 w->update_mode_line = Qt;
10260 UNBLOCK_INPUT;
10263 UNGCPRO;
10265 unbind_to (count, Qnil);
10266 set_buffer_internal_1 (prev);
10272 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10273 F's desired tool-bar contents. F->tool_bar_items must have
10274 been set up previously by calling prepare_menu_bars. */
10276 static void
10277 build_desired_tool_bar_string (struct frame *f)
10279 int i, size, size_needed;
10280 struct gcpro gcpro1, gcpro2, gcpro3;
10281 Lisp_Object image, plist, props;
10283 image = plist = props = Qnil;
10284 GCPRO3 (image, plist, props);
10286 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10287 Otherwise, make a new string. */
10289 /* The size of the string we might be able to reuse. */
10290 size = (STRINGP (f->desired_tool_bar_string)
10291 ? SCHARS (f->desired_tool_bar_string)
10292 : 0);
10294 /* We need one space in the string for each image. */
10295 size_needed = f->n_tool_bar_items;
10297 /* Reuse f->desired_tool_bar_string, if possible. */
10298 if (size < size_needed || NILP (f->desired_tool_bar_string))
10299 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10300 make_number (' '));
10301 else
10303 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10304 Fremove_text_properties (make_number (0), make_number (size),
10305 props, f->desired_tool_bar_string);
10308 /* Put a `display' property on the string for the images to display,
10309 put a `menu_item' property on tool-bar items with a value that
10310 is the index of the item in F's tool-bar item vector. */
10311 for (i = 0; i < f->n_tool_bar_items; ++i)
10313 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10315 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10316 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10317 int hmargin, vmargin, relief, idx, end;
10319 /* If image is a vector, choose the image according to the
10320 button state. */
10321 image = PROP (TOOL_BAR_ITEM_IMAGES);
10322 if (VECTORP (image))
10324 if (enabled_p)
10325 idx = (selected_p
10326 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10327 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10328 else
10329 idx = (selected_p
10330 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10331 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10333 xassert (ASIZE (image) >= idx);
10334 image = AREF (image, idx);
10336 else
10337 idx = -1;
10339 /* Ignore invalid image specifications. */
10340 if (!valid_image_p (image))
10341 continue;
10343 /* Display the tool-bar button pressed, or depressed. */
10344 plist = Fcopy_sequence (XCDR (image));
10346 /* Compute margin and relief to draw. */
10347 relief = (tool_bar_button_relief >= 0
10348 ? tool_bar_button_relief
10349 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10350 hmargin = vmargin = relief;
10352 if (INTEGERP (Vtool_bar_button_margin)
10353 && XINT (Vtool_bar_button_margin) > 0)
10355 hmargin += XFASTINT (Vtool_bar_button_margin);
10356 vmargin += XFASTINT (Vtool_bar_button_margin);
10358 else if (CONSP (Vtool_bar_button_margin))
10360 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10361 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10362 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10364 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10365 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10366 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10369 if (auto_raise_tool_bar_buttons_p)
10371 /* Add a `:relief' property to the image spec if the item is
10372 selected. */
10373 if (selected_p)
10375 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10376 hmargin -= relief;
10377 vmargin -= relief;
10380 else
10382 /* If image is selected, display it pressed, i.e. with a
10383 negative relief. If it's not selected, display it with a
10384 raised relief. */
10385 plist = Fplist_put (plist, QCrelief,
10386 (selected_p
10387 ? make_number (-relief)
10388 : make_number (relief)));
10389 hmargin -= relief;
10390 vmargin -= relief;
10393 /* Put a margin around the image. */
10394 if (hmargin || vmargin)
10396 if (hmargin == vmargin)
10397 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10398 else
10399 plist = Fplist_put (plist, QCmargin,
10400 Fcons (make_number (hmargin),
10401 make_number (vmargin)));
10404 /* If button is not enabled, and we don't have special images
10405 for the disabled state, make the image appear disabled by
10406 applying an appropriate algorithm to it. */
10407 if (!enabled_p && idx < 0)
10408 plist = Fplist_put (plist, QCconversion, Qdisabled);
10410 /* Put a `display' text property on the string for the image to
10411 display. Put a `menu-item' property on the string that gives
10412 the start of this item's properties in the tool-bar items
10413 vector. */
10414 image = Fcons (Qimage, plist);
10415 props = list4 (Qdisplay, image,
10416 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10418 /* Let the last image hide all remaining spaces in the tool bar
10419 string. The string can be longer than needed when we reuse a
10420 previous string. */
10421 if (i + 1 == f->n_tool_bar_items)
10422 end = SCHARS (f->desired_tool_bar_string);
10423 else
10424 end = i + 1;
10425 Fadd_text_properties (make_number (i), make_number (end),
10426 props, f->desired_tool_bar_string);
10427 #undef PROP
10430 UNGCPRO;
10434 /* Display one line of the tool-bar of frame IT->f.
10436 HEIGHT specifies the desired height of the tool-bar line.
10437 If the actual height of the glyph row is less than HEIGHT, the
10438 row's height is increased to HEIGHT, and the icons are centered
10439 vertically in the new height.
10441 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10442 count a final empty row in case the tool-bar width exactly matches
10443 the window width.
10446 static void
10447 display_tool_bar_line (struct it *it, int height)
10449 struct glyph_row *row = it->glyph_row;
10450 int max_x = it->last_visible_x;
10451 struct glyph *last;
10453 prepare_desired_row (row);
10454 row->y = it->current_y;
10456 /* Note that this isn't made use of if the face hasn't a box,
10457 so there's no need to check the face here. */
10458 it->start_of_box_run_p = 1;
10460 while (it->current_x < max_x)
10462 int x, n_glyphs_before, i, nglyphs;
10463 struct it it_before;
10465 /* Get the next display element. */
10466 if (!get_next_display_element (it))
10468 /* Don't count empty row if we are counting needed tool-bar lines. */
10469 if (height < 0 && !it->hpos)
10470 return;
10471 break;
10474 /* Produce glyphs. */
10475 n_glyphs_before = row->used[TEXT_AREA];
10476 it_before = *it;
10478 PRODUCE_GLYPHS (it);
10480 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10481 i = 0;
10482 x = it_before.current_x;
10483 while (i < nglyphs)
10485 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10487 if (x + glyph->pixel_width > max_x)
10489 /* Glyph doesn't fit on line. Backtrack. */
10490 row->used[TEXT_AREA] = n_glyphs_before;
10491 *it = it_before;
10492 /* If this is the only glyph on this line, it will never fit on the
10493 toolbar, so skip it. But ensure there is at least one glyph,
10494 so we don't accidentally disable the tool-bar. */
10495 if (n_glyphs_before == 0
10496 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10497 break;
10498 goto out;
10501 ++it->hpos;
10502 x += glyph->pixel_width;
10503 ++i;
10506 /* Stop at line ends. */
10507 if (ITERATOR_AT_END_OF_LINE_P (it))
10508 break;
10510 set_iterator_to_next (it, 1);
10513 out:;
10515 row->displays_text_p = row->used[TEXT_AREA] != 0;
10517 /* Use default face for the border below the tool bar.
10519 FIXME: When auto-resize-tool-bars is grow-only, there is
10520 no additional border below the possibly empty tool-bar lines.
10521 So to make the extra empty lines look "normal", we have to
10522 use the tool-bar face for the border too. */
10523 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10524 it->face_id = DEFAULT_FACE_ID;
10526 extend_face_to_end_of_line (it);
10527 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10528 last->right_box_line_p = 1;
10529 if (last == row->glyphs[TEXT_AREA])
10530 last->left_box_line_p = 1;
10532 /* Make line the desired height and center it vertically. */
10533 if ((height -= it->max_ascent + it->max_descent) > 0)
10535 /* Don't add more than one line height. */
10536 height %= FRAME_LINE_HEIGHT (it->f);
10537 it->max_ascent += height / 2;
10538 it->max_descent += (height + 1) / 2;
10541 compute_line_metrics (it);
10543 /* If line is empty, make it occupy the rest of the tool-bar. */
10544 if (!row->displays_text_p)
10546 row->height = row->phys_height = it->last_visible_y - row->y;
10547 row->visible_height = row->height;
10548 row->ascent = row->phys_ascent = 0;
10549 row->extra_line_spacing = 0;
10552 row->full_width_p = 1;
10553 row->continued_p = 0;
10554 row->truncated_on_left_p = 0;
10555 row->truncated_on_right_p = 0;
10557 it->current_x = it->hpos = 0;
10558 it->current_y += row->height;
10559 ++it->vpos;
10560 ++it->glyph_row;
10564 /* Max tool-bar height. */
10566 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10567 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10569 /* Value is the number of screen lines needed to make all tool-bar
10570 items of frame F visible. The number of actual rows needed is
10571 returned in *N_ROWS if non-NULL. */
10573 static int
10574 tool_bar_lines_needed (struct frame *f, int *n_rows)
10576 struct window *w = XWINDOW (f->tool_bar_window);
10577 struct it it;
10578 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10579 the desired matrix, so use (unused) mode-line row as temporary row to
10580 avoid destroying the first tool-bar row. */
10581 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10583 /* Initialize an iterator for iteration over
10584 F->desired_tool_bar_string in the tool-bar window of frame F. */
10585 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10586 it.first_visible_x = 0;
10587 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10588 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10590 while (!ITERATOR_AT_END_P (&it))
10592 clear_glyph_row (temp_row);
10593 it.glyph_row = temp_row;
10594 display_tool_bar_line (&it, -1);
10596 clear_glyph_row (temp_row);
10598 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10599 if (n_rows)
10600 *n_rows = it.vpos > 0 ? it.vpos : -1;
10602 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10606 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10607 0, 1, 0,
10608 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10609 (Lisp_Object frame)
10611 struct frame *f;
10612 struct window *w;
10613 int nlines = 0;
10615 if (NILP (frame))
10616 frame = selected_frame;
10617 else
10618 CHECK_FRAME (frame);
10619 f = XFRAME (frame);
10621 if (WINDOWP (f->tool_bar_window)
10622 || (w = XWINDOW (f->tool_bar_window),
10623 WINDOW_TOTAL_LINES (w) > 0))
10625 update_tool_bar (f, 1);
10626 if (f->n_tool_bar_items)
10628 build_desired_tool_bar_string (f);
10629 nlines = tool_bar_lines_needed (f, NULL);
10633 return make_number (nlines);
10637 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10638 height should be changed. */
10640 static int
10641 redisplay_tool_bar (struct frame *f)
10643 struct window *w;
10644 struct it it;
10645 struct glyph_row *row;
10647 #if defined (USE_GTK) || defined (HAVE_NS)
10648 if (FRAME_EXTERNAL_TOOL_BAR (f))
10649 update_frame_tool_bar (f);
10650 return 0;
10651 #endif
10653 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10654 do anything. This means you must start with tool-bar-lines
10655 non-zero to get the auto-sizing effect. Or in other words, you
10656 can turn off tool-bars by specifying tool-bar-lines zero. */
10657 if (!WINDOWP (f->tool_bar_window)
10658 || (w = XWINDOW (f->tool_bar_window),
10659 WINDOW_TOTAL_LINES (w) == 0))
10660 return 0;
10662 /* Set up an iterator for the tool-bar window. */
10663 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10664 it.first_visible_x = 0;
10665 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10666 row = it.glyph_row;
10668 /* Build a string that represents the contents of the tool-bar. */
10669 build_desired_tool_bar_string (f);
10670 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10672 if (f->n_tool_bar_rows == 0)
10674 int nlines;
10676 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10677 nlines != WINDOW_TOTAL_LINES (w)))
10679 Lisp_Object frame;
10680 int old_height = WINDOW_TOTAL_LINES (w);
10682 XSETFRAME (frame, f);
10683 Fmodify_frame_parameters (frame,
10684 Fcons (Fcons (Qtool_bar_lines,
10685 make_number (nlines)),
10686 Qnil));
10687 if (WINDOW_TOTAL_LINES (w) != old_height)
10689 clear_glyph_matrix (w->desired_matrix);
10690 fonts_changed_p = 1;
10691 return 1;
10696 /* Display as many lines as needed to display all tool-bar items. */
10698 if (f->n_tool_bar_rows > 0)
10700 int border, rows, height, extra;
10702 if (INTEGERP (Vtool_bar_border))
10703 border = XINT (Vtool_bar_border);
10704 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10705 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10706 else if (EQ (Vtool_bar_border, Qborder_width))
10707 border = f->border_width;
10708 else
10709 border = 0;
10710 if (border < 0)
10711 border = 0;
10713 rows = f->n_tool_bar_rows;
10714 height = max (1, (it.last_visible_y - border) / rows);
10715 extra = it.last_visible_y - border - height * rows;
10717 while (it.current_y < it.last_visible_y)
10719 int h = 0;
10720 if (extra > 0 && rows-- > 0)
10722 h = (extra + rows - 1) / rows;
10723 extra -= h;
10725 display_tool_bar_line (&it, height + h);
10728 else
10730 while (it.current_y < it.last_visible_y)
10731 display_tool_bar_line (&it, 0);
10734 /* It doesn't make much sense to try scrolling in the tool-bar
10735 window, so don't do it. */
10736 w->desired_matrix->no_scrolling_p = 1;
10737 w->must_be_updated_p = 1;
10739 if (!NILP (Vauto_resize_tool_bars))
10741 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10742 int change_height_p = 0;
10744 /* If we couldn't display everything, change the tool-bar's
10745 height if there is room for more. */
10746 if (IT_STRING_CHARPOS (it) < it.end_charpos
10747 && it.current_y < max_tool_bar_height)
10748 change_height_p = 1;
10750 row = it.glyph_row - 1;
10752 /* If there are blank lines at the end, except for a partially
10753 visible blank line at the end that is smaller than
10754 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10755 if (!row->displays_text_p
10756 && row->height >= FRAME_LINE_HEIGHT (f))
10757 change_height_p = 1;
10759 /* If row displays tool-bar items, but is partially visible,
10760 change the tool-bar's height. */
10761 if (row->displays_text_p
10762 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10763 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10764 change_height_p = 1;
10766 /* Resize windows as needed by changing the `tool-bar-lines'
10767 frame parameter. */
10768 if (change_height_p)
10770 Lisp_Object frame;
10771 int old_height = WINDOW_TOTAL_LINES (w);
10772 int nrows;
10773 int nlines = tool_bar_lines_needed (f, &nrows);
10775 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10776 && !f->minimize_tool_bar_window_p)
10777 ? (nlines > old_height)
10778 : (nlines != old_height));
10779 f->minimize_tool_bar_window_p = 0;
10781 if (change_height_p)
10783 XSETFRAME (frame, f);
10784 Fmodify_frame_parameters (frame,
10785 Fcons (Fcons (Qtool_bar_lines,
10786 make_number (nlines)),
10787 Qnil));
10788 if (WINDOW_TOTAL_LINES (w) != old_height)
10790 clear_glyph_matrix (w->desired_matrix);
10791 f->n_tool_bar_rows = nrows;
10792 fonts_changed_p = 1;
10793 return 1;
10799 f->minimize_tool_bar_window_p = 0;
10800 return 0;
10804 /* Get information about the tool-bar item which is displayed in GLYPH
10805 on frame F. Return in *PROP_IDX the index where tool-bar item
10806 properties start in F->tool_bar_items. Value is zero if
10807 GLYPH doesn't display a tool-bar item. */
10809 static int
10810 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10812 Lisp_Object prop;
10813 int success_p;
10814 int charpos;
10816 /* This function can be called asynchronously, which means we must
10817 exclude any possibility that Fget_text_property signals an
10818 error. */
10819 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10820 charpos = max (0, charpos);
10822 /* Get the text property `menu-item' at pos. The value of that
10823 property is the start index of this item's properties in
10824 F->tool_bar_items. */
10825 prop = Fget_text_property (make_number (charpos),
10826 Qmenu_item, f->current_tool_bar_string);
10827 if (INTEGERP (prop))
10829 *prop_idx = XINT (prop);
10830 success_p = 1;
10832 else
10833 success_p = 0;
10835 return success_p;
10839 /* Get information about the tool-bar item at position X/Y on frame F.
10840 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10841 the current matrix of the tool-bar window of F, or NULL if not
10842 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10843 item in F->tool_bar_items. Value is
10845 -1 if X/Y is not on a tool-bar item
10846 0 if X/Y is on the same item that was highlighted before.
10847 1 otherwise. */
10849 static int
10850 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10851 int *hpos, int *vpos, int *prop_idx)
10853 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10854 struct window *w = XWINDOW (f->tool_bar_window);
10855 int area;
10857 /* Find the glyph under X/Y. */
10858 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10859 if (*glyph == NULL)
10860 return -1;
10862 /* Get the start of this tool-bar item's properties in
10863 f->tool_bar_items. */
10864 if (!tool_bar_item_info (f, *glyph, prop_idx))
10865 return -1;
10867 /* Is mouse on the highlighted item? */
10868 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10869 && *vpos >= hlinfo->mouse_face_beg_row
10870 && *vpos <= hlinfo->mouse_face_end_row
10871 && (*vpos > hlinfo->mouse_face_beg_row
10872 || *hpos >= hlinfo->mouse_face_beg_col)
10873 && (*vpos < hlinfo->mouse_face_end_row
10874 || *hpos < hlinfo->mouse_face_end_col
10875 || hlinfo->mouse_face_past_end))
10876 return 0;
10878 return 1;
10882 /* EXPORT:
10883 Handle mouse button event on the tool-bar of frame F, at
10884 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10885 0 for button release. MODIFIERS is event modifiers for button
10886 release. */
10888 void
10889 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10890 unsigned int modifiers)
10892 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10893 struct window *w = XWINDOW (f->tool_bar_window);
10894 int hpos, vpos, prop_idx;
10895 struct glyph *glyph;
10896 Lisp_Object enabled_p;
10898 /* If not on the highlighted tool-bar item, return. */
10899 frame_to_window_pixel_xy (w, &x, &y);
10900 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10901 return;
10903 /* If item is disabled, do nothing. */
10904 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10905 if (NILP (enabled_p))
10906 return;
10908 if (down_p)
10910 /* Show item in pressed state. */
10911 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10912 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10913 last_tool_bar_item = prop_idx;
10915 else
10917 Lisp_Object key, frame;
10918 struct input_event event;
10919 EVENT_INIT (event);
10921 /* Show item in released state. */
10922 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10923 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10925 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10927 XSETFRAME (frame, f);
10928 event.kind = TOOL_BAR_EVENT;
10929 event.frame_or_window = frame;
10930 event.arg = frame;
10931 kbd_buffer_store_event (&event);
10933 event.kind = TOOL_BAR_EVENT;
10934 event.frame_or_window = frame;
10935 event.arg = key;
10936 event.modifiers = modifiers;
10937 kbd_buffer_store_event (&event);
10938 last_tool_bar_item = -1;
10943 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10944 tool-bar window-relative coordinates X/Y. Called from
10945 note_mouse_highlight. */
10947 static void
10948 note_tool_bar_highlight (struct frame *f, int x, int y)
10950 Lisp_Object window = f->tool_bar_window;
10951 struct window *w = XWINDOW (window);
10952 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10953 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10954 int hpos, vpos;
10955 struct glyph *glyph;
10956 struct glyph_row *row;
10957 int i;
10958 Lisp_Object enabled_p;
10959 int prop_idx;
10960 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10961 int mouse_down_p, rc;
10963 /* Function note_mouse_highlight is called with negative X/Y
10964 values when mouse moves outside of the frame. */
10965 if (x <= 0 || y <= 0)
10967 clear_mouse_face (hlinfo);
10968 return;
10971 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10972 if (rc < 0)
10974 /* Not on tool-bar item. */
10975 clear_mouse_face (hlinfo);
10976 return;
10978 else if (rc == 0)
10979 /* On same tool-bar item as before. */
10980 goto set_help_echo;
10982 clear_mouse_face (hlinfo);
10984 /* Mouse is down, but on different tool-bar item? */
10985 mouse_down_p = (dpyinfo->grabbed
10986 && f == last_mouse_frame
10987 && FRAME_LIVE_P (f));
10988 if (mouse_down_p
10989 && last_tool_bar_item != prop_idx)
10990 return;
10992 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10993 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10995 /* If tool-bar item is not enabled, don't highlight it. */
10996 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10997 if (!NILP (enabled_p))
10999 /* Compute the x-position of the glyph. In front and past the
11000 image is a space. We include this in the highlighted area. */
11001 row = MATRIX_ROW (w->current_matrix, vpos);
11002 for (i = x = 0; i < hpos; ++i)
11003 x += row->glyphs[TEXT_AREA][i].pixel_width;
11005 /* Record this as the current active region. */
11006 hlinfo->mouse_face_beg_col = hpos;
11007 hlinfo->mouse_face_beg_row = vpos;
11008 hlinfo->mouse_face_beg_x = x;
11009 hlinfo->mouse_face_beg_y = row->y;
11010 hlinfo->mouse_face_past_end = 0;
11012 hlinfo->mouse_face_end_col = hpos + 1;
11013 hlinfo->mouse_face_end_row = vpos;
11014 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11015 hlinfo->mouse_face_end_y = row->y;
11016 hlinfo->mouse_face_window = window;
11017 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11019 /* Display it as active. */
11020 show_mouse_face (hlinfo, draw);
11021 hlinfo->mouse_face_image_state = draw;
11024 set_help_echo:
11026 /* Set help_echo_string to a help string to display for this tool-bar item.
11027 XTread_socket does the rest. */
11028 help_echo_object = help_echo_window = Qnil;
11029 help_echo_pos = -1;
11030 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11031 if (NILP (help_echo_string))
11032 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11035 #endif /* HAVE_WINDOW_SYSTEM */
11039 /************************************************************************
11040 Horizontal scrolling
11041 ************************************************************************/
11043 static int hscroll_window_tree (Lisp_Object);
11044 static int hscroll_windows (Lisp_Object);
11046 /* For all leaf windows in the window tree rooted at WINDOW, set their
11047 hscroll value so that PT is (i) visible in the window, and (ii) so
11048 that it is not within a certain margin at the window's left and
11049 right border. Value is non-zero if any window's hscroll has been
11050 changed. */
11052 static int
11053 hscroll_window_tree (Lisp_Object window)
11055 int hscrolled_p = 0;
11056 int hscroll_relative_p = FLOATP (Vhscroll_step);
11057 int hscroll_step_abs = 0;
11058 double hscroll_step_rel = 0;
11060 if (hscroll_relative_p)
11062 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11063 if (hscroll_step_rel < 0)
11065 hscroll_relative_p = 0;
11066 hscroll_step_abs = 0;
11069 else if (INTEGERP (Vhscroll_step))
11071 hscroll_step_abs = XINT (Vhscroll_step);
11072 if (hscroll_step_abs < 0)
11073 hscroll_step_abs = 0;
11075 else
11076 hscroll_step_abs = 0;
11078 while (WINDOWP (window))
11080 struct window *w = XWINDOW (window);
11082 if (WINDOWP (w->hchild))
11083 hscrolled_p |= hscroll_window_tree (w->hchild);
11084 else if (WINDOWP (w->vchild))
11085 hscrolled_p |= hscroll_window_tree (w->vchild);
11086 else if (w->cursor.vpos >= 0)
11088 int h_margin;
11089 int text_area_width;
11090 struct glyph_row *current_cursor_row
11091 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11092 struct glyph_row *desired_cursor_row
11093 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11094 struct glyph_row *cursor_row
11095 = (desired_cursor_row->enabled_p
11096 ? desired_cursor_row
11097 : current_cursor_row);
11099 text_area_width = window_box_width (w, TEXT_AREA);
11101 /* Scroll when cursor is inside this scroll margin. */
11102 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11104 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11105 && ((XFASTINT (w->hscroll)
11106 && w->cursor.x <= h_margin)
11107 || (cursor_row->enabled_p
11108 && cursor_row->truncated_on_right_p
11109 && (w->cursor.x >= text_area_width - h_margin))))
11111 struct it it;
11112 int hscroll;
11113 struct buffer *saved_current_buffer;
11114 EMACS_INT pt;
11115 int wanted_x;
11117 /* Find point in a display of infinite width. */
11118 saved_current_buffer = current_buffer;
11119 current_buffer = XBUFFER (w->buffer);
11121 if (w == XWINDOW (selected_window))
11122 pt = BUF_PT (current_buffer);
11123 else
11125 pt = marker_position (w->pointm);
11126 pt = max (BEGV, pt);
11127 pt = min (ZV, pt);
11130 /* Move iterator to pt starting at cursor_row->start in
11131 a line with infinite width. */
11132 init_to_row_start (&it, w, cursor_row);
11133 it.last_visible_x = INFINITY;
11134 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11135 current_buffer = saved_current_buffer;
11137 /* Position cursor in window. */
11138 if (!hscroll_relative_p && hscroll_step_abs == 0)
11139 hscroll = max (0, (it.current_x
11140 - (ITERATOR_AT_END_OF_LINE_P (&it)
11141 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11142 : (text_area_width / 2))))
11143 / FRAME_COLUMN_WIDTH (it.f);
11144 else if (w->cursor.x >= text_area_width - h_margin)
11146 if (hscroll_relative_p)
11147 wanted_x = text_area_width * (1 - hscroll_step_rel)
11148 - h_margin;
11149 else
11150 wanted_x = text_area_width
11151 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11152 - h_margin;
11153 hscroll
11154 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11156 else
11158 if (hscroll_relative_p)
11159 wanted_x = text_area_width * hscroll_step_rel
11160 + h_margin;
11161 else
11162 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11163 + h_margin;
11164 hscroll
11165 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11167 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11169 /* Don't call Fset_window_hscroll if value hasn't
11170 changed because it will prevent redisplay
11171 optimizations. */
11172 if (XFASTINT (w->hscroll) != hscroll)
11174 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11175 w->hscroll = make_number (hscroll);
11176 hscrolled_p = 1;
11181 window = w->next;
11184 /* Value is non-zero if hscroll of any leaf window has been changed. */
11185 return hscrolled_p;
11189 /* Set hscroll so that cursor is visible and not inside horizontal
11190 scroll margins for all windows in the tree rooted at WINDOW. See
11191 also hscroll_window_tree above. Value is non-zero if any window's
11192 hscroll has been changed. If it has, desired matrices on the frame
11193 of WINDOW are cleared. */
11195 static int
11196 hscroll_windows (Lisp_Object window)
11198 int hscrolled_p = hscroll_window_tree (window);
11199 if (hscrolled_p)
11200 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11201 return hscrolled_p;
11206 /************************************************************************
11207 Redisplay
11208 ************************************************************************/
11210 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11211 to a non-zero value. This is sometimes handy to have in a debugger
11212 session. */
11214 #if GLYPH_DEBUG
11216 /* First and last unchanged row for try_window_id. */
11218 int debug_first_unchanged_at_end_vpos;
11219 int debug_last_unchanged_at_beg_vpos;
11221 /* Delta vpos and y. */
11223 int debug_dvpos, debug_dy;
11225 /* Delta in characters and bytes for try_window_id. */
11227 EMACS_INT debug_delta, debug_delta_bytes;
11229 /* Values of window_end_pos and window_end_vpos at the end of
11230 try_window_id. */
11232 EMACS_INT debug_end_pos, debug_end_vpos;
11234 /* Append a string to W->desired_matrix->method. FMT is a printf
11235 format string. A1...A9 are a supplement for a variable-length
11236 argument list. If trace_redisplay_p is non-zero also printf the
11237 resulting string to stderr. */
11239 static void
11240 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11241 struct window *w;
11242 char *fmt;
11243 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11245 char buffer[512];
11246 char *method = w->desired_matrix->method;
11247 int len = strlen (method);
11248 int size = sizeof w->desired_matrix->method;
11249 int remaining = size - len - 1;
11251 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11252 if (len && remaining)
11254 method[len] = '|';
11255 --remaining, ++len;
11258 strncpy (method + len, buffer, remaining);
11260 if (trace_redisplay_p)
11261 fprintf (stderr, "%p (%s): %s\n",
11263 ((BUFFERP (w->buffer)
11264 && STRINGP (XBUFFER (w->buffer)->name))
11265 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11266 : "no buffer"),
11267 buffer);
11270 #endif /* GLYPH_DEBUG */
11273 /* Value is non-zero if all changes in window W, which displays
11274 current_buffer, are in the text between START and END. START is a
11275 buffer position, END is given as a distance from Z. Used in
11276 redisplay_internal for display optimization. */
11278 static INLINE int
11279 text_outside_line_unchanged_p (struct window *w,
11280 EMACS_INT start, EMACS_INT end)
11282 int unchanged_p = 1;
11284 /* If text or overlays have changed, see where. */
11285 if (XFASTINT (w->last_modified) < MODIFF
11286 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11288 /* Gap in the line? */
11289 if (GPT < start || Z - GPT < end)
11290 unchanged_p = 0;
11292 /* Changes start in front of the line, or end after it? */
11293 if (unchanged_p
11294 && (BEG_UNCHANGED < start - 1
11295 || END_UNCHANGED < end))
11296 unchanged_p = 0;
11298 /* If selective display, can't optimize if changes start at the
11299 beginning of the line. */
11300 if (unchanged_p
11301 && INTEGERP (current_buffer->selective_display)
11302 && XINT (current_buffer->selective_display) > 0
11303 && (BEG_UNCHANGED < start || GPT <= start))
11304 unchanged_p = 0;
11306 /* If there are overlays at the start or end of the line, these
11307 may have overlay strings with newlines in them. A change at
11308 START, for instance, may actually concern the display of such
11309 overlay strings as well, and they are displayed on different
11310 lines. So, quickly rule out this case. (For the future, it
11311 might be desirable to implement something more telling than
11312 just BEG/END_UNCHANGED.) */
11313 if (unchanged_p)
11315 if (BEG + BEG_UNCHANGED == start
11316 && overlay_touches_p (start))
11317 unchanged_p = 0;
11318 if (END_UNCHANGED == end
11319 && overlay_touches_p (Z - end))
11320 unchanged_p = 0;
11323 /* Under bidi reordering, adding or deleting a character in the
11324 beginning of a paragraph, before the first strong directional
11325 character, can change the base direction of the paragraph (unless
11326 the buffer specifies a fixed paragraph direction), which will
11327 require to redisplay the whole paragraph. It might be worthwhile
11328 to find the paragraph limits and widen the range of redisplayed
11329 lines to that, but for now just give up this optimization. */
11330 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11331 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11332 unchanged_p = 0;
11335 return unchanged_p;
11339 /* Do a frame update, taking possible shortcuts into account. This is
11340 the main external entry point for redisplay.
11342 If the last redisplay displayed an echo area message and that message
11343 is no longer requested, we clear the echo area or bring back the
11344 mini-buffer if that is in use. */
11346 void
11347 redisplay (void)
11349 redisplay_internal (0);
11353 static Lisp_Object
11354 overlay_arrow_string_or_property (Lisp_Object var)
11356 Lisp_Object val;
11358 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11359 return val;
11361 return Voverlay_arrow_string;
11364 /* Return 1 if there are any overlay-arrows in current_buffer. */
11365 static int
11366 overlay_arrow_in_current_buffer_p (void)
11368 Lisp_Object vlist;
11370 for (vlist = Voverlay_arrow_variable_list;
11371 CONSP (vlist);
11372 vlist = XCDR (vlist))
11374 Lisp_Object var = XCAR (vlist);
11375 Lisp_Object val;
11377 if (!SYMBOLP (var))
11378 continue;
11379 val = find_symbol_value (var);
11380 if (MARKERP (val)
11381 && current_buffer == XMARKER (val)->buffer)
11382 return 1;
11384 return 0;
11388 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11389 has changed. */
11391 static int
11392 overlay_arrows_changed_p (void)
11394 Lisp_Object vlist;
11396 for (vlist = Voverlay_arrow_variable_list;
11397 CONSP (vlist);
11398 vlist = XCDR (vlist))
11400 Lisp_Object var = XCAR (vlist);
11401 Lisp_Object val, pstr;
11403 if (!SYMBOLP (var))
11404 continue;
11405 val = find_symbol_value (var);
11406 if (!MARKERP (val))
11407 continue;
11408 if (! EQ (COERCE_MARKER (val),
11409 Fget (var, Qlast_arrow_position))
11410 || ! (pstr = overlay_arrow_string_or_property (var),
11411 EQ (pstr, Fget (var, Qlast_arrow_string))))
11412 return 1;
11414 return 0;
11417 /* Mark overlay arrows to be updated on next redisplay. */
11419 static void
11420 update_overlay_arrows (int up_to_date)
11422 Lisp_Object vlist;
11424 for (vlist = Voverlay_arrow_variable_list;
11425 CONSP (vlist);
11426 vlist = XCDR (vlist))
11428 Lisp_Object var = XCAR (vlist);
11430 if (!SYMBOLP (var))
11431 continue;
11433 if (up_to_date > 0)
11435 Lisp_Object val = find_symbol_value (var);
11436 Fput (var, Qlast_arrow_position,
11437 COERCE_MARKER (val));
11438 Fput (var, Qlast_arrow_string,
11439 overlay_arrow_string_or_property (var));
11441 else if (up_to_date < 0
11442 || !NILP (Fget (var, Qlast_arrow_position)))
11444 Fput (var, Qlast_arrow_position, Qt);
11445 Fput (var, Qlast_arrow_string, Qt);
11451 /* Return overlay arrow string to display at row.
11452 Return integer (bitmap number) for arrow bitmap in left fringe.
11453 Return nil if no overlay arrow. */
11455 static Lisp_Object
11456 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11458 Lisp_Object vlist;
11460 for (vlist = Voverlay_arrow_variable_list;
11461 CONSP (vlist);
11462 vlist = XCDR (vlist))
11464 Lisp_Object var = XCAR (vlist);
11465 Lisp_Object val;
11467 if (!SYMBOLP (var))
11468 continue;
11470 val = find_symbol_value (var);
11472 if (MARKERP (val)
11473 && current_buffer == XMARKER (val)->buffer
11474 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11476 if (FRAME_WINDOW_P (it->f)
11477 /* FIXME: if ROW->reversed_p is set, this should test
11478 the right fringe, not the left one. */
11479 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11481 #ifdef HAVE_WINDOW_SYSTEM
11482 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11484 int fringe_bitmap;
11485 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11486 return make_number (fringe_bitmap);
11488 #endif
11489 return make_number (-1); /* Use default arrow bitmap */
11491 return overlay_arrow_string_or_property (var);
11495 return Qnil;
11498 /* Return 1 if point moved out of or into a composition. Otherwise
11499 return 0. PREV_BUF and PREV_PT are the last point buffer and
11500 position. BUF and PT are the current point buffer and position. */
11503 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11504 struct buffer *buf, EMACS_INT pt)
11506 EMACS_INT start, end;
11507 Lisp_Object prop;
11508 Lisp_Object buffer;
11510 XSETBUFFER (buffer, buf);
11511 /* Check a composition at the last point if point moved within the
11512 same buffer. */
11513 if (prev_buf == buf)
11515 if (prev_pt == pt)
11516 /* Point didn't move. */
11517 return 0;
11519 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11520 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11521 && COMPOSITION_VALID_P (start, end, prop)
11522 && start < prev_pt && end > prev_pt)
11523 /* The last point was within the composition. Return 1 iff
11524 point moved out of the composition. */
11525 return (pt <= start || pt >= end);
11528 /* Check a composition at the current point. */
11529 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11530 && find_composition (pt, -1, &start, &end, &prop, buffer)
11531 && COMPOSITION_VALID_P (start, end, prop)
11532 && start < pt && end > pt);
11536 /* Reconsider the setting of B->clip_changed which is displayed
11537 in window W. */
11539 static INLINE void
11540 reconsider_clip_changes (struct window *w, struct buffer *b)
11542 if (b->clip_changed
11543 && !NILP (w->window_end_valid)
11544 && w->current_matrix->buffer == b
11545 && w->current_matrix->zv == BUF_ZV (b)
11546 && w->current_matrix->begv == BUF_BEGV (b))
11547 b->clip_changed = 0;
11549 /* If display wasn't paused, and W is not a tool bar window, see if
11550 point has been moved into or out of a composition. In that case,
11551 we set b->clip_changed to 1 to force updating the screen. If
11552 b->clip_changed has already been set to 1, we can skip this
11553 check. */
11554 if (!b->clip_changed
11555 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11557 EMACS_INT pt;
11559 if (w == XWINDOW (selected_window))
11560 pt = BUF_PT (current_buffer);
11561 else
11562 pt = marker_position (w->pointm);
11564 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11565 || pt != XINT (w->last_point))
11566 && check_point_in_composition (w->current_matrix->buffer,
11567 XINT (w->last_point),
11568 XBUFFER (w->buffer), pt))
11569 b->clip_changed = 1;
11574 /* Select FRAME to forward the values of frame-local variables into C
11575 variables so that the redisplay routines can access those values
11576 directly. */
11578 static void
11579 select_frame_for_redisplay (Lisp_Object frame)
11581 Lisp_Object tail, tem;
11582 Lisp_Object old = selected_frame;
11583 struct Lisp_Symbol *sym;
11585 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11587 selected_frame = frame;
11589 do {
11590 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11591 if (CONSP (XCAR (tail))
11592 && (tem = XCAR (XCAR (tail)),
11593 SYMBOLP (tem))
11594 && (sym = indirect_variable (XSYMBOL (tem)),
11595 sym->redirect == SYMBOL_LOCALIZED)
11596 && sym->val.blv->frame_local)
11597 /* Use find_symbol_value rather than Fsymbol_value
11598 to avoid an error if it is void. */
11599 find_symbol_value (tem);
11600 } while (!EQ (frame, old) && (frame = old, 1));
11604 #define STOP_POLLING \
11605 do { if (! polling_stopped_here) stop_polling (); \
11606 polling_stopped_here = 1; } while (0)
11608 #define RESUME_POLLING \
11609 do { if (polling_stopped_here) start_polling (); \
11610 polling_stopped_here = 0; } while (0)
11613 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11614 response to any user action; therefore, we should preserve the echo
11615 area. (Actually, our caller does that job.) Perhaps in the future
11616 avoid recentering windows if it is not necessary; currently that
11617 causes some problems. */
11619 static void
11620 redisplay_internal (int preserve_echo_area)
11622 struct window *w = XWINDOW (selected_window);
11623 struct frame *f;
11624 int pause;
11625 int must_finish = 0;
11626 struct text_pos tlbufpos, tlendpos;
11627 int number_of_visible_frames;
11628 int count, count1;
11629 struct frame *sf;
11630 int polling_stopped_here = 0;
11631 Lisp_Object old_frame = selected_frame;
11633 /* Non-zero means redisplay has to consider all windows on all
11634 frames. Zero means, only selected_window is considered. */
11635 int consider_all_windows_p;
11637 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11639 /* No redisplay if running in batch mode or frame is not yet fully
11640 initialized, or redisplay is explicitly turned off by setting
11641 Vinhibit_redisplay. */
11642 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11643 || !NILP (Vinhibit_redisplay))
11644 return;
11646 /* Don't examine these until after testing Vinhibit_redisplay.
11647 When Emacs is shutting down, perhaps because its connection to
11648 X has dropped, we should not look at them at all. */
11649 f = XFRAME (w->frame);
11650 sf = SELECTED_FRAME ();
11652 if (!f->glyphs_initialized_p)
11653 return;
11655 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11656 if (popup_activated ())
11657 return;
11658 #endif
11660 /* I don't think this happens but let's be paranoid. */
11661 if (redisplaying_p)
11662 return;
11664 /* Record a function that resets redisplaying_p to its old value
11665 when we leave this function. */
11666 count = SPECPDL_INDEX ();
11667 record_unwind_protect (unwind_redisplay,
11668 Fcons (make_number (redisplaying_p), selected_frame));
11669 ++redisplaying_p;
11670 specbind (Qinhibit_free_realized_faces, Qnil);
11673 Lisp_Object tail, frame;
11675 FOR_EACH_FRAME (tail, frame)
11677 struct frame *f = XFRAME (frame);
11678 f->already_hscrolled_p = 0;
11682 retry:
11683 if (!EQ (old_frame, selected_frame)
11684 && FRAME_LIVE_P (XFRAME (old_frame)))
11685 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11686 selected_frame and selected_window to be temporarily out-of-sync so
11687 when we come back here via `goto retry', we need to resync because we
11688 may need to run Elisp code (via prepare_menu_bars). */
11689 select_frame_for_redisplay (old_frame);
11691 pause = 0;
11692 reconsider_clip_changes (w, current_buffer);
11693 last_escape_glyph_frame = NULL;
11694 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11695 last_glyphless_glyph_frame = NULL;
11696 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11698 /* If new fonts have been loaded that make a glyph matrix adjustment
11699 necessary, do it. */
11700 if (fonts_changed_p)
11702 adjust_glyphs (NULL);
11703 ++windows_or_buffers_changed;
11704 fonts_changed_p = 0;
11707 /* If face_change_count is non-zero, init_iterator will free all
11708 realized faces, which includes the faces referenced from current
11709 matrices. So, we can't reuse current matrices in this case. */
11710 if (face_change_count)
11711 ++windows_or_buffers_changed;
11713 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11714 && FRAME_TTY (sf)->previous_frame != sf)
11716 /* Since frames on a single ASCII terminal share the same
11717 display area, displaying a different frame means redisplay
11718 the whole thing. */
11719 windows_or_buffers_changed++;
11720 SET_FRAME_GARBAGED (sf);
11721 #ifndef DOS_NT
11722 set_tty_color_mode (FRAME_TTY (sf), sf);
11723 #endif
11724 FRAME_TTY (sf)->previous_frame = sf;
11727 /* Set the visible flags for all frames. Do this before checking
11728 for resized or garbaged frames; they want to know if their frames
11729 are visible. See the comment in frame.h for
11730 FRAME_SAMPLE_VISIBILITY. */
11732 Lisp_Object tail, frame;
11734 number_of_visible_frames = 0;
11736 FOR_EACH_FRAME (tail, frame)
11738 struct frame *f = XFRAME (frame);
11740 FRAME_SAMPLE_VISIBILITY (f);
11741 if (FRAME_VISIBLE_P (f))
11742 ++number_of_visible_frames;
11743 clear_desired_matrices (f);
11747 /* Notice any pending interrupt request to change frame size. */
11748 do_pending_window_change (1);
11750 /* Clear frames marked as garbaged. */
11751 if (frame_garbaged)
11752 clear_garbaged_frames ();
11754 /* Build menubar and tool-bar items. */
11755 if (NILP (Vmemory_full))
11756 prepare_menu_bars ();
11758 if (windows_or_buffers_changed)
11759 update_mode_lines++;
11761 /* Detect case that we need to write or remove a star in the mode line. */
11762 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11764 w->update_mode_line = Qt;
11765 if (buffer_shared > 1)
11766 update_mode_lines++;
11769 /* Avoid invocation of point motion hooks by `current_column' below. */
11770 count1 = SPECPDL_INDEX ();
11771 specbind (Qinhibit_point_motion_hooks, Qt);
11773 /* If %c is in the mode line, update it if needed. */
11774 if (!NILP (w->column_number_displayed)
11775 /* This alternative quickly identifies a common case
11776 where no change is needed. */
11777 && !(PT == XFASTINT (w->last_point)
11778 && XFASTINT (w->last_modified) >= MODIFF
11779 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11780 && (XFASTINT (w->column_number_displayed)
11781 != (int) current_column ())) /* iftc */
11782 w->update_mode_line = Qt;
11784 unbind_to (count1, Qnil);
11786 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11788 /* The variable buffer_shared is set in redisplay_window and
11789 indicates that we redisplay a buffer in different windows. See
11790 there. */
11791 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11792 || cursor_type_changed);
11794 /* If specs for an arrow have changed, do thorough redisplay
11795 to ensure we remove any arrow that should no longer exist. */
11796 if (overlay_arrows_changed_p ())
11797 consider_all_windows_p = windows_or_buffers_changed = 1;
11799 /* Normally the message* functions will have already displayed and
11800 updated the echo area, but the frame may have been trashed, or
11801 the update may have been preempted, so display the echo area
11802 again here. Checking message_cleared_p captures the case that
11803 the echo area should be cleared. */
11804 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11805 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11806 || (message_cleared_p
11807 && minibuf_level == 0
11808 /* If the mini-window is currently selected, this means the
11809 echo-area doesn't show through. */
11810 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11812 int window_height_changed_p = echo_area_display (0);
11813 must_finish = 1;
11815 /* If we don't display the current message, don't clear the
11816 message_cleared_p flag, because, if we did, we wouldn't clear
11817 the echo area in the next redisplay which doesn't preserve
11818 the echo area. */
11819 if (!display_last_displayed_message_p)
11820 message_cleared_p = 0;
11822 if (fonts_changed_p)
11823 goto retry;
11824 else if (window_height_changed_p)
11826 consider_all_windows_p = 1;
11827 ++update_mode_lines;
11828 ++windows_or_buffers_changed;
11830 /* If window configuration was changed, frames may have been
11831 marked garbaged. Clear them or we will experience
11832 surprises wrt scrolling. */
11833 if (frame_garbaged)
11834 clear_garbaged_frames ();
11837 else if (EQ (selected_window, minibuf_window)
11838 && (current_buffer->clip_changed
11839 || XFASTINT (w->last_modified) < MODIFF
11840 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11841 && resize_mini_window (w, 0))
11843 /* Resized active mini-window to fit the size of what it is
11844 showing if its contents might have changed. */
11845 must_finish = 1;
11846 /* FIXME: this causes all frames to be updated, which seems unnecessary
11847 since only the current frame needs to be considered. This function needs
11848 to be rewritten with two variables, consider_all_windows and
11849 consider_all_frames. */
11850 consider_all_windows_p = 1;
11851 ++windows_or_buffers_changed;
11852 ++update_mode_lines;
11854 /* If window configuration was changed, frames may have been
11855 marked garbaged. Clear them or we will experience
11856 surprises wrt scrolling. */
11857 if (frame_garbaged)
11858 clear_garbaged_frames ();
11862 /* If showing the region, and mark has changed, we must redisplay
11863 the whole window. The assignment to this_line_start_pos prevents
11864 the optimization directly below this if-statement. */
11865 if (((!NILP (Vtransient_mark_mode)
11866 && !NILP (XBUFFER (w->buffer)->mark_active))
11867 != !NILP (w->region_showing))
11868 || (!NILP (w->region_showing)
11869 && !EQ (w->region_showing,
11870 Fmarker_position (XBUFFER (w->buffer)->mark))))
11871 CHARPOS (this_line_start_pos) = 0;
11873 /* Optimize the case that only the line containing the cursor in the
11874 selected window has changed. Variables starting with this_ are
11875 set in display_line and record information about the line
11876 containing the cursor. */
11877 tlbufpos = this_line_start_pos;
11878 tlendpos = this_line_end_pos;
11879 if (!consider_all_windows_p
11880 && CHARPOS (tlbufpos) > 0
11881 && NILP (w->update_mode_line)
11882 && !current_buffer->clip_changed
11883 && !current_buffer->prevent_redisplay_optimizations_p
11884 && FRAME_VISIBLE_P (XFRAME (w->frame))
11885 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11886 /* Make sure recorded data applies to current buffer, etc. */
11887 && this_line_buffer == current_buffer
11888 && current_buffer == XBUFFER (w->buffer)
11889 && NILP (w->force_start)
11890 && NILP (w->optional_new_start)
11891 /* Point must be on the line that we have info recorded about. */
11892 && PT >= CHARPOS (tlbufpos)
11893 && PT <= Z - CHARPOS (tlendpos)
11894 /* All text outside that line, including its final newline,
11895 must be unchanged. */
11896 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11897 CHARPOS (tlendpos)))
11899 if (CHARPOS (tlbufpos) > BEGV
11900 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11901 && (CHARPOS (tlbufpos) == ZV
11902 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11903 /* Former continuation line has disappeared by becoming empty. */
11904 goto cancel;
11905 else if (XFASTINT (w->last_modified) < MODIFF
11906 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11907 || MINI_WINDOW_P (w))
11909 /* We have to handle the case of continuation around a
11910 wide-column character (see the comment in indent.c around
11911 line 1340).
11913 For instance, in the following case:
11915 -------- Insert --------
11916 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11917 J_I_ ==> J_I_ `^^' are cursors.
11918 ^^ ^^
11919 -------- --------
11921 As we have to redraw the line above, we cannot use this
11922 optimization. */
11924 struct it it;
11925 int line_height_before = this_line_pixel_height;
11927 /* Note that start_display will handle the case that the
11928 line starting at tlbufpos is a continuation line. */
11929 start_display (&it, w, tlbufpos);
11931 /* Implementation note: It this still necessary? */
11932 if (it.current_x != this_line_start_x)
11933 goto cancel;
11935 TRACE ((stderr, "trying display optimization 1\n"));
11936 w->cursor.vpos = -1;
11937 overlay_arrow_seen = 0;
11938 it.vpos = this_line_vpos;
11939 it.current_y = this_line_y;
11940 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11941 display_line (&it);
11943 /* If line contains point, is not continued,
11944 and ends at same distance from eob as before, we win. */
11945 if (w->cursor.vpos >= 0
11946 /* Line is not continued, otherwise this_line_start_pos
11947 would have been set to 0 in display_line. */
11948 && CHARPOS (this_line_start_pos)
11949 /* Line ends as before. */
11950 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11951 /* Line has same height as before. Otherwise other lines
11952 would have to be shifted up or down. */
11953 && this_line_pixel_height == line_height_before)
11955 /* If this is not the window's last line, we must adjust
11956 the charstarts of the lines below. */
11957 if (it.current_y < it.last_visible_y)
11959 struct glyph_row *row
11960 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11961 EMACS_INT delta, delta_bytes;
11963 /* We used to distinguish between two cases here,
11964 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11965 when the line ends in a newline or the end of the
11966 buffer's accessible portion. But both cases did
11967 the same, so they were collapsed. */
11968 delta = (Z
11969 - CHARPOS (tlendpos)
11970 - MATRIX_ROW_START_CHARPOS (row));
11971 delta_bytes = (Z_BYTE
11972 - BYTEPOS (tlendpos)
11973 - MATRIX_ROW_START_BYTEPOS (row));
11975 increment_matrix_positions (w->current_matrix,
11976 this_line_vpos + 1,
11977 w->current_matrix->nrows,
11978 delta, delta_bytes);
11981 /* If this row displays text now but previously didn't,
11982 or vice versa, w->window_end_vpos may have to be
11983 adjusted. */
11984 if ((it.glyph_row - 1)->displays_text_p)
11986 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11987 XSETINT (w->window_end_vpos, this_line_vpos);
11989 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11990 && this_line_vpos > 0)
11991 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11992 w->window_end_valid = Qnil;
11994 /* Update hint: No need to try to scroll in update_window. */
11995 w->desired_matrix->no_scrolling_p = 1;
11997 #if GLYPH_DEBUG
11998 *w->desired_matrix->method = 0;
11999 debug_method_add (w, "optimization 1");
12000 #endif
12001 #ifdef HAVE_WINDOW_SYSTEM
12002 update_window_fringes (w, 0);
12003 #endif
12004 goto update;
12006 else
12007 goto cancel;
12009 else if (/* Cursor position hasn't changed. */
12010 PT == XFASTINT (w->last_point)
12011 /* Make sure the cursor was last displayed
12012 in this window. Otherwise we have to reposition it. */
12013 && 0 <= w->cursor.vpos
12014 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12016 if (!must_finish)
12018 do_pending_window_change (1);
12020 /* We used to always goto end_of_redisplay here, but this
12021 isn't enough if we have a blinking cursor. */
12022 if (w->cursor_off_p == w->last_cursor_off_p)
12023 goto end_of_redisplay;
12025 goto update;
12027 /* If highlighting the region, or if the cursor is in the echo area,
12028 then we can't just move the cursor. */
12029 else if (! (!NILP (Vtransient_mark_mode)
12030 && !NILP (current_buffer->mark_active))
12031 && (EQ (selected_window, current_buffer->last_selected_window)
12032 || highlight_nonselected_windows)
12033 && NILP (w->region_showing)
12034 && NILP (Vshow_trailing_whitespace)
12035 && !cursor_in_echo_area)
12037 struct it it;
12038 struct glyph_row *row;
12040 /* Skip from tlbufpos to PT and see where it is. Note that
12041 PT may be in invisible text. If so, we will end at the
12042 next visible position. */
12043 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12044 NULL, DEFAULT_FACE_ID);
12045 it.current_x = this_line_start_x;
12046 it.current_y = this_line_y;
12047 it.vpos = this_line_vpos;
12049 /* The call to move_it_to stops in front of PT, but
12050 moves over before-strings. */
12051 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12053 if (it.vpos == this_line_vpos
12054 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12055 row->enabled_p))
12057 xassert (this_line_vpos == it.vpos);
12058 xassert (this_line_y == it.current_y);
12059 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12060 #if GLYPH_DEBUG
12061 *w->desired_matrix->method = 0;
12062 debug_method_add (w, "optimization 3");
12063 #endif
12064 goto update;
12066 else
12067 goto cancel;
12070 cancel:
12071 /* Text changed drastically or point moved off of line. */
12072 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12075 CHARPOS (this_line_start_pos) = 0;
12076 consider_all_windows_p |= buffer_shared > 1;
12077 ++clear_face_cache_count;
12078 #ifdef HAVE_WINDOW_SYSTEM
12079 ++clear_image_cache_count;
12080 #endif
12082 /* Build desired matrices, and update the display. If
12083 consider_all_windows_p is non-zero, do it for all windows on all
12084 frames. Otherwise do it for selected_window, only. */
12086 if (consider_all_windows_p)
12088 Lisp_Object tail, frame;
12090 FOR_EACH_FRAME (tail, frame)
12091 XFRAME (frame)->updated_p = 0;
12093 /* Recompute # windows showing selected buffer. This will be
12094 incremented each time such a window is displayed. */
12095 buffer_shared = 0;
12097 FOR_EACH_FRAME (tail, frame)
12099 struct frame *f = XFRAME (frame);
12101 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12103 if (! EQ (frame, selected_frame))
12104 /* Select the frame, for the sake of frame-local
12105 variables. */
12106 select_frame_for_redisplay (frame);
12108 /* Mark all the scroll bars to be removed; we'll redeem
12109 the ones we want when we redisplay their windows. */
12110 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12111 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12113 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12114 redisplay_windows (FRAME_ROOT_WINDOW (f));
12116 /* The X error handler may have deleted that frame. */
12117 if (!FRAME_LIVE_P (f))
12118 continue;
12120 /* Any scroll bars which redisplay_windows should have
12121 nuked should now go away. */
12122 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12123 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12125 /* If fonts changed, display again. */
12126 /* ??? rms: I suspect it is a mistake to jump all the way
12127 back to retry here. It should just retry this frame. */
12128 if (fonts_changed_p)
12129 goto retry;
12131 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12133 /* See if we have to hscroll. */
12134 if (!f->already_hscrolled_p)
12136 f->already_hscrolled_p = 1;
12137 if (hscroll_windows (f->root_window))
12138 goto retry;
12141 /* Prevent various kinds of signals during display
12142 update. stdio is not robust about handling
12143 signals, which can cause an apparent I/O
12144 error. */
12145 if (interrupt_input)
12146 unrequest_sigio ();
12147 STOP_POLLING;
12149 /* Update the display. */
12150 set_window_update_flags (XWINDOW (f->root_window), 1);
12151 pause |= update_frame (f, 0, 0);
12152 f->updated_p = 1;
12157 if (!EQ (old_frame, selected_frame)
12158 && FRAME_LIVE_P (XFRAME (old_frame)))
12159 /* We played a bit fast-and-loose above and allowed selected_frame
12160 and selected_window to be temporarily out-of-sync but let's make
12161 sure this stays contained. */
12162 select_frame_for_redisplay (old_frame);
12163 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12165 if (!pause)
12167 /* Do the mark_window_display_accurate after all windows have
12168 been redisplayed because this call resets flags in buffers
12169 which are needed for proper redisplay. */
12170 FOR_EACH_FRAME (tail, frame)
12172 struct frame *f = XFRAME (frame);
12173 if (f->updated_p)
12175 mark_window_display_accurate (f->root_window, 1);
12176 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12177 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12182 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12184 Lisp_Object mini_window;
12185 struct frame *mini_frame;
12187 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12188 /* Use list_of_error, not Qerror, so that
12189 we catch only errors and don't run the debugger. */
12190 internal_condition_case_1 (redisplay_window_1, selected_window,
12191 list_of_error,
12192 redisplay_window_error);
12194 /* Compare desired and current matrices, perform output. */
12196 update:
12197 /* If fonts changed, display again. */
12198 if (fonts_changed_p)
12199 goto retry;
12201 /* Prevent various kinds of signals during display update.
12202 stdio is not robust about handling signals,
12203 which can cause an apparent I/O error. */
12204 if (interrupt_input)
12205 unrequest_sigio ();
12206 STOP_POLLING;
12208 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12210 if (hscroll_windows (selected_window))
12211 goto retry;
12213 XWINDOW (selected_window)->must_be_updated_p = 1;
12214 pause = update_frame (sf, 0, 0);
12217 /* We may have called echo_area_display at the top of this
12218 function. If the echo area is on another frame, that may
12219 have put text on a frame other than the selected one, so the
12220 above call to update_frame would not have caught it. Catch
12221 it here. */
12222 mini_window = FRAME_MINIBUF_WINDOW (sf);
12223 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12225 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12227 XWINDOW (mini_window)->must_be_updated_p = 1;
12228 pause |= update_frame (mini_frame, 0, 0);
12229 if (!pause && hscroll_windows (mini_window))
12230 goto retry;
12234 /* If display was paused because of pending input, make sure we do a
12235 thorough update the next time. */
12236 if (pause)
12238 /* Prevent the optimization at the beginning of
12239 redisplay_internal that tries a single-line update of the
12240 line containing the cursor in the selected window. */
12241 CHARPOS (this_line_start_pos) = 0;
12243 /* Let the overlay arrow be updated the next time. */
12244 update_overlay_arrows (0);
12246 /* If we pause after scrolling, some rows in the current
12247 matrices of some windows are not valid. */
12248 if (!WINDOW_FULL_WIDTH_P (w)
12249 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12250 update_mode_lines = 1;
12252 else
12254 if (!consider_all_windows_p)
12256 /* This has already been done above if
12257 consider_all_windows_p is set. */
12258 mark_window_display_accurate_1 (w, 1);
12260 /* Say overlay arrows are up to date. */
12261 update_overlay_arrows (1);
12263 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12264 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12267 update_mode_lines = 0;
12268 windows_or_buffers_changed = 0;
12269 cursor_type_changed = 0;
12272 /* Start SIGIO interrupts coming again. Having them off during the
12273 code above makes it less likely one will discard output, but not
12274 impossible, since there might be stuff in the system buffer here.
12275 But it is much hairier to try to do anything about that. */
12276 if (interrupt_input)
12277 request_sigio ();
12278 RESUME_POLLING;
12280 /* If a frame has become visible which was not before, redisplay
12281 again, so that we display it. Expose events for such a frame
12282 (which it gets when becoming visible) don't call the parts of
12283 redisplay constructing glyphs, so simply exposing a frame won't
12284 display anything in this case. So, we have to display these
12285 frames here explicitly. */
12286 if (!pause)
12288 Lisp_Object tail, frame;
12289 int new_count = 0;
12291 FOR_EACH_FRAME (tail, frame)
12293 int this_is_visible = 0;
12295 if (XFRAME (frame)->visible)
12296 this_is_visible = 1;
12297 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12298 if (XFRAME (frame)->visible)
12299 this_is_visible = 1;
12301 if (this_is_visible)
12302 new_count++;
12305 if (new_count != number_of_visible_frames)
12306 windows_or_buffers_changed++;
12309 /* Change frame size now if a change is pending. */
12310 do_pending_window_change (1);
12312 /* If we just did a pending size change, or have additional
12313 visible frames, redisplay again. */
12314 if (windows_or_buffers_changed && !pause)
12315 goto retry;
12317 /* Clear the face and image caches.
12319 We used to do this only if consider_all_windows_p. But the cache
12320 needs to be cleared if a timer creates images in the current
12321 buffer (e.g. the test case in Bug#6230). */
12323 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12325 clear_face_cache (0);
12326 clear_face_cache_count = 0;
12329 #ifdef HAVE_WINDOW_SYSTEM
12330 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12332 clear_image_caches (Qnil);
12333 clear_image_cache_count = 0;
12335 #endif /* HAVE_WINDOW_SYSTEM */
12337 end_of_redisplay:
12338 unbind_to (count, Qnil);
12339 RESUME_POLLING;
12343 /* Redisplay, but leave alone any recent echo area message unless
12344 another message has been requested in its place.
12346 This is useful in situations where you need to redisplay but no
12347 user action has occurred, making it inappropriate for the message
12348 area to be cleared. See tracking_off and
12349 wait_reading_process_output for examples of these situations.
12351 FROM_WHERE is an integer saying from where this function was
12352 called. This is useful for debugging. */
12354 void
12355 redisplay_preserve_echo_area (int from_where)
12357 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12359 if (!NILP (echo_area_buffer[1]))
12361 /* We have a previously displayed message, but no current
12362 message. Redisplay the previous message. */
12363 display_last_displayed_message_p = 1;
12364 redisplay_internal (1);
12365 display_last_displayed_message_p = 0;
12367 else
12368 redisplay_internal (1);
12370 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12371 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12372 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12376 /* Function registered with record_unwind_protect in
12377 redisplay_internal. Reset redisplaying_p to the value it had
12378 before redisplay_internal was called, and clear
12379 prevent_freeing_realized_faces_p. It also selects the previously
12380 selected frame, unless it has been deleted (by an X connection
12381 failure during redisplay, for example). */
12383 static Lisp_Object
12384 unwind_redisplay (Lisp_Object val)
12386 Lisp_Object old_redisplaying_p, old_frame;
12388 old_redisplaying_p = XCAR (val);
12389 redisplaying_p = XFASTINT (old_redisplaying_p);
12390 old_frame = XCDR (val);
12391 if (! EQ (old_frame, selected_frame)
12392 && FRAME_LIVE_P (XFRAME (old_frame)))
12393 select_frame_for_redisplay (old_frame);
12394 return Qnil;
12398 /* Mark the display of window W as accurate or inaccurate. If
12399 ACCURATE_P is non-zero mark display of W as accurate. If
12400 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12401 redisplay_internal is called. */
12403 static void
12404 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12406 if (BUFFERP (w->buffer))
12408 struct buffer *b = XBUFFER (w->buffer);
12410 w->last_modified
12411 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12412 w->last_overlay_modified
12413 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12414 w->last_had_star
12415 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12417 if (accurate_p)
12419 b->clip_changed = 0;
12420 b->prevent_redisplay_optimizations_p = 0;
12422 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12423 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12424 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12425 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12427 w->current_matrix->buffer = b;
12428 w->current_matrix->begv = BUF_BEGV (b);
12429 w->current_matrix->zv = BUF_ZV (b);
12431 w->last_cursor = w->cursor;
12432 w->last_cursor_off_p = w->cursor_off_p;
12434 if (w == XWINDOW (selected_window))
12435 w->last_point = make_number (BUF_PT (b));
12436 else
12437 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12441 if (accurate_p)
12443 w->window_end_valid = w->buffer;
12444 w->update_mode_line = Qnil;
12449 /* Mark the display of windows in the window tree rooted at WINDOW as
12450 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12451 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12452 be redisplayed the next time redisplay_internal is called. */
12454 void
12455 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12457 struct window *w;
12459 for (; !NILP (window); window = w->next)
12461 w = XWINDOW (window);
12462 mark_window_display_accurate_1 (w, accurate_p);
12464 if (!NILP (w->vchild))
12465 mark_window_display_accurate (w->vchild, accurate_p);
12466 if (!NILP (w->hchild))
12467 mark_window_display_accurate (w->hchild, accurate_p);
12470 if (accurate_p)
12472 update_overlay_arrows (1);
12474 else
12476 /* Force a thorough redisplay the next time by setting
12477 last_arrow_position and last_arrow_string to t, which is
12478 unequal to any useful value of Voverlay_arrow_... */
12479 update_overlay_arrows (-1);
12484 /* Return value in display table DP (Lisp_Char_Table *) for character
12485 C. Since a display table doesn't have any parent, we don't have to
12486 follow parent. Do not call this function directly but use the
12487 macro DISP_CHAR_VECTOR. */
12489 Lisp_Object
12490 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12492 Lisp_Object val;
12494 if (ASCII_CHAR_P (c))
12496 val = dp->ascii;
12497 if (SUB_CHAR_TABLE_P (val))
12498 val = XSUB_CHAR_TABLE (val)->contents[c];
12500 else
12502 Lisp_Object table;
12504 XSETCHAR_TABLE (table, dp);
12505 val = char_table_ref (table, c);
12507 if (NILP (val))
12508 val = dp->defalt;
12509 return val;
12514 /***********************************************************************
12515 Window Redisplay
12516 ***********************************************************************/
12518 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12520 static void
12521 redisplay_windows (Lisp_Object window)
12523 while (!NILP (window))
12525 struct window *w = XWINDOW (window);
12527 if (!NILP (w->hchild))
12528 redisplay_windows (w->hchild);
12529 else if (!NILP (w->vchild))
12530 redisplay_windows (w->vchild);
12531 else if (!NILP (w->buffer))
12533 displayed_buffer = XBUFFER (w->buffer);
12534 /* Use list_of_error, not Qerror, so that
12535 we catch only errors and don't run the debugger. */
12536 internal_condition_case_1 (redisplay_window_0, window,
12537 list_of_error,
12538 redisplay_window_error);
12541 window = w->next;
12545 static Lisp_Object
12546 redisplay_window_error (Lisp_Object ignore)
12548 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12549 return Qnil;
12552 static Lisp_Object
12553 redisplay_window_0 (Lisp_Object window)
12555 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12556 redisplay_window (window, 0);
12557 return Qnil;
12560 static Lisp_Object
12561 redisplay_window_1 (Lisp_Object window)
12563 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12564 redisplay_window (window, 1);
12565 return Qnil;
12569 /* Increment GLYPH until it reaches END or CONDITION fails while
12570 adding (GLYPH)->pixel_width to X. */
12572 #define SKIP_GLYPHS(glyph, end, x, condition) \
12573 do \
12575 (x) += (glyph)->pixel_width; \
12576 ++(glyph); \
12578 while ((glyph) < (end) && (condition))
12581 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12582 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12583 which positions recorded in ROW differ from current buffer
12584 positions.
12586 Return 0 if cursor is not on this row, 1 otherwise. */
12589 set_cursor_from_row (struct window *w, struct glyph_row *row,
12590 struct glyph_matrix *matrix,
12591 EMACS_INT delta, EMACS_INT delta_bytes,
12592 int dy, int dvpos)
12594 struct glyph *glyph = row->glyphs[TEXT_AREA];
12595 struct glyph *end = glyph + row->used[TEXT_AREA];
12596 struct glyph *cursor = NULL;
12597 /* The last known character position in row. */
12598 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12599 int x = row->x;
12600 EMACS_INT pt_old = PT - delta;
12601 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12602 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12603 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12604 /* A glyph beyond the edge of TEXT_AREA which we should never
12605 touch. */
12606 struct glyph *glyphs_end = end;
12607 /* Non-zero means we've found a match for cursor position, but that
12608 glyph has the avoid_cursor_p flag set. */
12609 int match_with_avoid_cursor = 0;
12610 /* Non-zero means we've seen at least one glyph that came from a
12611 display string. */
12612 int string_seen = 0;
12613 /* Largest and smalles buffer positions seen so far during scan of
12614 glyph row. */
12615 EMACS_INT bpos_max = pos_before;
12616 EMACS_INT bpos_min = pos_after;
12617 /* Last buffer position covered by an overlay string with an integer
12618 `cursor' property. */
12619 EMACS_INT bpos_covered = 0;
12621 /* Skip over glyphs not having an object at the start and the end of
12622 the row. These are special glyphs like truncation marks on
12623 terminal frames. */
12624 if (row->displays_text_p)
12626 if (!row->reversed_p)
12628 while (glyph < end
12629 && INTEGERP (glyph->object)
12630 && glyph->charpos < 0)
12632 x += glyph->pixel_width;
12633 ++glyph;
12635 while (end > glyph
12636 && INTEGERP ((end - 1)->object)
12637 /* CHARPOS is zero for blanks and stretch glyphs
12638 inserted by extend_face_to_end_of_line. */
12639 && (end - 1)->charpos <= 0)
12640 --end;
12641 glyph_before = glyph - 1;
12642 glyph_after = end;
12644 else
12646 struct glyph *g;
12648 /* If the glyph row is reversed, we need to process it from back
12649 to front, so swap the edge pointers. */
12650 glyphs_end = end = glyph - 1;
12651 glyph += row->used[TEXT_AREA] - 1;
12653 while (glyph > end + 1
12654 && INTEGERP (glyph->object)
12655 && glyph->charpos < 0)
12657 --glyph;
12658 x -= glyph->pixel_width;
12660 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12661 --glyph;
12662 /* By default, in reversed rows we put the cursor on the
12663 rightmost (first in the reading order) glyph. */
12664 for (g = end + 1; g < glyph; g++)
12665 x += g->pixel_width;
12666 while (end < glyph
12667 && INTEGERP ((end + 1)->object)
12668 && (end + 1)->charpos <= 0)
12669 ++end;
12670 glyph_before = glyph + 1;
12671 glyph_after = end;
12674 else if (row->reversed_p)
12676 /* In R2L rows that don't display text, put the cursor on the
12677 rightmost glyph. Case in point: an empty last line that is
12678 part of an R2L paragraph. */
12679 cursor = end - 1;
12680 /* Avoid placing the cursor on the last glyph of the row, where
12681 on terminal frames we hold the vertical border between
12682 adjacent windows. */
12683 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12684 && !WINDOW_RIGHTMOST_P (w)
12685 && cursor == row->glyphs[LAST_AREA] - 1)
12686 cursor--;
12687 x = -1; /* will be computed below, at label compute_x */
12690 /* Step 1: Try to find the glyph whose character position
12691 corresponds to point. If that's not possible, find 2 glyphs
12692 whose character positions are the closest to point, one before
12693 point, the other after it. */
12694 if (!row->reversed_p)
12695 while (/* not marched to end of glyph row */
12696 glyph < end
12697 /* glyph was not inserted by redisplay for internal purposes */
12698 && !INTEGERP (glyph->object))
12700 if (BUFFERP (glyph->object))
12702 EMACS_INT dpos = glyph->charpos - pt_old;
12704 if (glyph->charpos > bpos_max)
12705 bpos_max = glyph->charpos;
12706 if (glyph->charpos < bpos_min)
12707 bpos_min = glyph->charpos;
12708 if (!glyph->avoid_cursor_p)
12710 /* If we hit point, we've found the glyph on which to
12711 display the cursor. */
12712 if (dpos == 0)
12714 match_with_avoid_cursor = 0;
12715 break;
12717 /* See if we've found a better approximation to
12718 POS_BEFORE or to POS_AFTER. Note that we want the
12719 first (leftmost) glyph of all those that are the
12720 closest from below, and the last (rightmost) of all
12721 those from above. */
12722 if (0 > dpos && dpos > pos_before - pt_old)
12724 pos_before = glyph->charpos;
12725 glyph_before = glyph;
12727 else if (0 < dpos && dpos <= pos_after - pt_old)
12729 pos_after = glyph->charpos;
12730 glyph_after = glyph;
12733 else if (dpos == 0)
12734 match_with_avoid_cursor = 1;
12736 else if (STRINGP (glyph->object))
12738 Lisp_Object chprop;
12739 EMACS_INT glyph_pos = glyph->charpos;
12741 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12742 glyph->object);
12743 if (INTEGERP (chprop))
12745 bpos_covered = bpos_max + XINT (chprop);
12746 /* If the `cursor' property covers buffer positions up
12747 to and including point, we should display cursor on
12748 this glyph. Note that overlays and text properties
12749 with string values stop bidi reordering, so every
12750 buffer position to the left of the string is always
12751 smaller than any position to the right of the
12752 string. Therefore, if a `cursor' property on one
12753 of the string's characters has an integer value, we
12754 will break out of the loop below _before_ we get to
12755 the position match above. IOW, integer values of
12756 the `cursor' property override the "exact match for
12757 point" strategy of positioning the cursor. */
12758 /* Implementation note: bpos_max == pt_old when, e.g.,
12759 we are in an empty line, where bpos_max is set to
12760 MATRIX_ROW_START_CHARPOS, see above. */
12761 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12763 cursor = glyph;
12764 break;
12768 string_seen = 1;
12770 x += glyph->pixel_width;
12771 ++glyph;
12773 else if (glyph > end) /* row is reversed */
12774 while (!INTEGERP (glyph->object))
12776 if (BUFFERP (glyph->object))
12778 EMACS_INT dpos = glyph->charpos - pt_old;
12780 if (glyph->charpos > bpos_max)
12781 bpos_max = glyph->charpos;
12782 if (glyph->charpos < bpos_min)
12783 bpos_min = glyph->charpos;
12784 if (!glyph->avoid_cursor_p)
12786 if (dpos == 0)
12788 match_with_avoid_cursor = 0;
12789 break;
12791 if (0 > dpos && dpos > pos_before - pt_old)
12793 pos_before = glyph->charpos;
12794 glyph_before = glyph;
12796 else if (0 < dpos && dpos <= pos_after - pt_old)
12798 pos_after = glyph->charpos;
12799 glyph_after = glyph;
12802 else if (dpos == 0)
12803 match_with_avoid_cursor = 1;
12805 else if (STRINGP (glyph->object))
12807 Lisp_Object chprop;
12808 EMACS_INT glyph_pos = glyph->charpos;
12810 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12811 glyph->object);
12812 if (INTEGERP (chprop))
12814 bpos_covered = bpos_max + XINT (chprop);
12815 /* If the `cursor' property covers buffer positions up
12816 to and including point, we should display cursor on
12817 this glyph. */
12818 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12820 cursor = glyph;
12821 break;
12824 string_seen = 1;
12826 --glyph;
12827 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12829 x--; /* can't use any pixel_width */
12830 break;
12832 x -= glyph->pixel_width;
12835 /* Step 2: If we didn't find an exact match for point, we need to
12836 look for a proper place to put the cursor among glyphs between
12837 GLYPH_BEFORE and GLYPH_AFTER. */
12838 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12839 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12840 && bpos_covered < pt_old)
12842 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12844 EMACS_INT ellipsis_pos;
12846 /* Scan back over the ellipsis glyphs. */
12847 if (!row->reversed_p)
12849 ellipsis_pos = (glyph - 1)->charpos;
12850 while (glyph > row->glyphs[TEXT_AREA]
12851 && (glyph - 1)->charpos == ellipsis_pos)
12852 glyph--, x -= glyph->pixel_width;
12853 /* That loop always goes one position too far, including
12854 the glyph before the ellipsis. So scan forward over
12855 that one. */
12856 x += glyph->pixel_width;
12857 glyph++;
12859 else /* row is reversed */
12861 ellipsis_pos = (glyph + 1)->charpos;
12862 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12863 && (glyph + 1)->charpos == ellipsis_pos)
12864 glyph++, x += glyph->pixel_width;
12865 x -= glyph->pixel_width;
12866 glyph--;
12869 else if (match_with_avoid_cursor
12870 /* A truncated row may not include PT among its
12871 character positions. Setting the cursor inside the
12872 scroll margin will trigger recalculation of hscroll
12873 in hscroll_window_tree. */
12874 || (row->truncated_on_left_p && pt_old < bpos_min)
12875 || (row->truncated_on_right_p && pt_old > bpos_max)
12876 /* Zero-width characters produce no glyphs. */
12877 || (!string_seen
12878 && (row->reversed_p
12879 ? glyph_after > glyphs_end
12880 : glyph_after < glyphs_end)))
12882 cursor = glyph_after;
12883 x = -1;
12885 else if (string_seen)
12887 int incr = row->reversed_p ? -1 : +1;
12889 /* Need to find the glyph that came out of a string which is
12890 present at point. That glyph is somewhere between
12891 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12892 positioned between POS_BEFORE and POS_AFTER in the
12893 buffer. */
12894 struct glyph *stop = glyph_after;
12895 EMACS_INT pos = pos_before;
12897 x = -1;
12898 for (glyph = glyph_before + incr;
12899 row->reversed_p ? glyph > stop : glyph < stop; )
12902 /* Any glyphs that come from the buffer are here because
12903 of bidi reordering. Skip them, and only pay
12904 attention to glyphs that came from some string. */
12905 if (STRINGP (glyph->object))
12907 Lisp_Object str;
12908 EMACS_INT tem;
12910 str = glyph->object;
12911 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12912 if (tem == 0 /* from overlay */
12913 || pos <= tem)
12915 /* If the string from which this glyph came is
12916 found in the buffer at point, then we've
12917 found the glyph we've been looking for. If
12918 it comes from an overlay (tem == 0), and it
12919 has the `cursor' property on one of its
12920 glyphs, record that glyph as a candidate for
12921 displaying the cursor. (As in the
12922 unidirectional version, we will display the
12923 cursor on the last candidate we find.) */
12924 if (tem == 0 || tem == pt_old)
12926 /* The glyphs from this string could have
12927 been reordered. Find the one with the
12928 smallest string position. Or there could
12929 be a character in the string with the
12930 `cursor' property, which means display
12931 cursor on that character's glyph. */
12932 EMACS_INT strpos = glyph->charpos;
12934 cursor = glyph;
12935 for (glyph += incr;
12936 (row->reversed_p ? glyph > stop : glyph < stop)
12937 && EQ (glyph->object, str);
12938 glyph += incr)
12940 Lisp_Object cprop;
12941 EMACS_INT gpos = glyph->charpos;
12943 cprop = Fget_char_property (make_number (gpos),
12944 Qcursor,
12945 glyph->object);
12946 if (!NILP (cprop))
12948 cursor = glyph;
12949 break;
12951 if (glyph->charpos < strpos)
12953 strpos = glyph->charpos;
12954 cursor = glyph;
12958 if (tem == pt_old)
12959 goto compute_x;
12961 if (tem)
12962 pos = tem + 1; /* don't find previous instances */
12964 /* This string is not what we want; skip all of the
12965 glyphs that came from it. */
12967 glyph += incr;
12968 while ((row->reversed_p ? glyph > stop : glyph < stop)
12969 && EQ (glyph->object, str));
12971 else
12972 glyph += incr;
12975 /* If we reached the end of the line, and END was from a string,
12976 the cursor is not on this line. */
12977 if (cursor == NULL
12978 && (row->reversed_p ? glyph <= end : glyph >= end)
12979 && STRINGP (end->object)
12980 && row->continued_p)
12981 return 0;
12985 compute_x:
12986 if (cursor != NULL)
12987 glyph = cursor;
12988 if (x < 0)
12990 struct glyph *g;
12992 /* Need to compute x that corresponds to GLYPH. */
12993 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12995 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12996 abort ();
12997 x += g->pixel_width;
13001 /* ROW could be part of a continued line, which, under bidi
13002 reordering, might have other rows whose start and end charpos
13003 occlude point. Only set w->cursor if we found a better
13004 approximation to the cursor position than we have from previously
13005 examined candidate rows belonging to the same continued line. */
13006 if (/* we already have a candidate row */
13007 w->cursor.vpos >= 0
13008 /* that candidate is not the row we are processing */
13009 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13010 /* the row we are processing is part of a continued line */
13011 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13012 /* Make sure cursor.vpos specifies a row whose start and end
13013 charpos occlude point. This is because some callers of this
13014 function leave cursor.vpos at the row where the cursor was
13015 displayed during the last redisplay cycle. */
13016 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13017 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13019 struct glyph *g1 =
13020 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13022 /* Don't consider glyphs that are outside TEXT_AREA. */
13023 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13024 return 0;
13025 /* Keep the candidate whose buffer position is the closest to
13026 point. */
13027 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13028 w->cursor.hpos >= 0
13029 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13030 && BUFFERP (g1->object)
13031 && (g1->charpos == pt_old /* an exact match always wins */
13032 || (BUFFERP (glyph->object)
13033 && eabs (g1->charpos - pt_old)
13034 < eabs (glyph->charpos - pt_old))))
13035 return 0;
13036 /* If this candidate gives an exact match, use that. */
13037 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13038 /* Otherwise, keep the candidate that comes from a row
13039 spanning less buffer positions. This may win when one or
13040 both candidate positions are on glyphs that came from
13041 display strings, for which we cannot compare buffer
13042 positions. */
13043 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13044 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13045 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13046 return 0;
13048 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13049 w->cursor.x = x;
13050 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13051 w->cursor.y = row->y + dy;
13053 if (w == XWINDOW (selected_window))
13055 if (!row->continued_p
13056 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13057 && row->x == 0)
13059 this_line_buffer = XBUFFER (w->buffer);
13061 CHARPOS (this_line_start_pos)
13062 = MATRIX_ROW_START_CHARPOS (row) + delta;
13063 BYTEPOS (this_line_start_pos)
13064 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13066 CHARPOS (this_line_end_pos)
13067 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13068 BYTEPOS (this_line_end_pos)
13069 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13071 this_line_y = w->cursor.y;
13072 this_line_pixel_height = row->height;
13073 this_line_vpos = w->cursor.vpos;
13074 this_line_start_x = row->x;
13076 else
13077 CHARPOS (this_line_start_pos) = 0;
13080 return 1;
13084 /* Run window scroll functions, if any, for WINDOW with new window
13085 start STARTP. Sets the window start of WINDOW to that position.
13087 We assume that the window's buffer is really current. */
13089 static INLINE struct text_pos
13090 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13092 struct window *w = XWINDOW (window);
13093 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13095 if (current_buffer != XBUFFER (w->buffer))
13096 abort ();
13098 if (!NILP (Vwindow_scroll_functions))
13100 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13101 make_number (CHARPOS (startp)));
13102 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13103 /* In case the hook functions switch buffers. */
13104 if (current_buffer != XBUFFER (w->buffer))
13105 set_buffer_internal_1 (XBUFFER (w->buffer));
13108 return startp;
13112 /* Make sure the line containing the cursor is fully visible.
13113 A value of 1 means there is nothing to be done.
13114 (Either the line is fully visible, or it cannot be made so,
13115 or we cannot tell.)
13117 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13118 is higher than window.
13120 A value of 0 means the caller should do scrolling
13121 as if point had gone off the screen. */
13123 static int
13124 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13126 struct glyph_matrix *matrix;
13127 struct glyph_row *row;
13128 int window_height;
13130 if (!make_cursor_line_fully_visible_p)
13131 return 1;
13133 /* It's not always possible to find the cursor, e.g, when a window
13134 is full of overlay strings. Don't do anything in that case. */
13135 if (w->cursor.vpos < 0)
13136 return 1;
13138 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13139 row = MATRIX_ROW (matrix, w->cursor.vpos);
13141 /* If the cursor row is not partially visible, there's nothing to do. */
13142 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13143 return 1;
13145 /* If the row the cursor is in is taller than the window's height,
13146 it's not clear what to do, so do nothing. */
13147 window_height = window_box_height (w);
13148 if (row->height >= window_height)
13150 if (!force_p || MINI_WINDOW_P (w)
13151 || w->vscroll || w->cursor.vpos == 0)
13152 return 1;
13154 return 0;
13158 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13159 non-zero means only WINDOW is redisplayed in redisplay_internal.
13160 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13161 in redisplay_window to bring a partially visible line into view in
13162 the case that only the cursor has moved.
13164 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13165 last screen line's vertical height extends past the end of the screen.
13167 Value is
13169 1 if scrolling succeeded
13171 0 if scrolling didn't find point.
13173 -1 if new fonts have been loaded so that we must interrupt
13174 redisplay, adjust glyph matrices, and try again. */
13176 enum
13178 SCROLLING_SUCCESS,
13179 SCROLLING_FAILED,
13180 SCROLLING_NEED_LARGER_MATRICES
13183 static int
13184 try_scrolling (Lisp_Object window, int just_this_one_p,
13185 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13186 int temp_scroll_step, int last_line_misfit)
13188 struct window *w = XWINDOW (window);
13189 struct frame *f = XFRAME (w->frame);
13190 struct text_pos pos, startp;
13191 struct it it;
13192 int this_scroll_margin, scroll_max, rc, height;
13193 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13194 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13195 Lisp_Object aggressive;
13196 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13198 #if GLYPH_DEBUG
13199 debug_method_add (w, "try_scrolling");
13200 #endif
13202 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13204 /* Compute scroll margin height in pixels. We scroll when point is
13205 within this distance from the top or bottom of the window. */
13206 if (scroll_margin > 0)
13207 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13208 * FRAME_LINE_HEIGHT (f);
13209 else
13210 this_scroll_margin = 0;
13212 /* Force scroll_conservatively to have a reasonable value, to avoid
13213 overflow while computing how much to scroll. Note that the user
13214 can supply scroll-conservatively equal to `most-positive-fixnum',
13215 which can be larger than INT_MAX. */
13216 if (scroll_conservatively > scroll_limit)
13218 scroll_conservatively = scroll_limit;
13219 scroll_max = INT_MAX;
13221 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13222 /* Compute how much we should try to scroll maximally to bring
13223 point into view. */
13224 scroll_max = (max (scroll_step,
13225 max (scroll_conservatively, temp_scroll_step))
13226 * FRAME_LINE_HEIGHT (f));
13227 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13228 || NUMBERP (current_buffer->scroll_up_aggressively))
13229 /* We're trying to scroll because of aggressive scrolling but no
13230 scroll_step is set. Choose an arbitrary one. */
13231 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13232 else
13233 scroll_max = 0;
13235 too_near_end:
13237 /* Decide whether to scroll down. */
13238 if (PT > CHARPOS (startp))
13240 int scroll_margin_y;
13242 /* Compute the pixel ypos of the scroll margin, then move it to
13243 either that ypos or PT, whichever comes first. */
13244 start_display (&it, w, startp);
13245 scroll_margin_y = it.last_visible_y - this_scroll_margin
13246 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13247 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13248 (MOVE_TO_POS | MOVE_TO_Y));
13250 if (PT > CHARPOS (it.current.pos))
13252 int y0 = line_bottom_y (&it);
13253 /* Compute how many pixels below window bottom to stop searching
13254 for PT. This avoids costly search for PT that is far away if
13255 the user limited scrolling by a small number of lines, but
13256 always finds PT if scroll_conservatively is set to a large
13257 number, such as most-positive-fixnum. */
13258 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13259 int y_to_move =
13260 slack >= INT_MAX - it.last_visible_y
13261 ? INT_MAX
13262 : it.last_visible_y + slack;
13264 /* Compute the distance from the scroll margin to PT or to
13265 the scroll limit, whichever comes first. This should
13266 include the height of the cursor line, to make that line
13267 fully visible. */
13268 move_it_to (&it, PT, -1, y_to_move,
13269 -1, MOVE_TO_POS | MOVE_TO_Y);
13270 dy = line_bottom_y (&it) - y0;
13272 if (dy > scroll_max)
13273 return SCROLLING_FAILED;
13275 scroll_down_p = 1;
13279 if (scroll_down_p)
13281 /* Point is in or below the bottom scroll margin, so move the
13282 window start down. If scrolling conservatively, move it just
13283 enough down to make point visible. If scroll_step is set,
13284 move it down by scroll_step. */
13285 if (scroll_conservatively)
13286 amount_to_scroll
13287 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13288 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13289 else if (scroll_step || temp_scroll_step)
13290 amount_to_scroll = scroll_max;
13291 else
13293 aggressive = current_buffer->scroll_up_aggressively;
13294 height = WINDOW_BOX_TEXT_HEIGHT (w);
13295 if (NUMBERP (aggressive))
13297 double float_amount = XFLOATINT (aggressive) * height;
13298 amount_to_scroll = float_amount;
13299 if (amount_to_scroll == 0 && float_amount > 0)
13300 amount_to_scroll = 1;
13304 if (amount_to_scroll <= 0)
13305 return SCROLLING_FAILED;
13307 start_display (&it, w, startp);
13308 if (scroll_max < INT_MAX)
13309 move_it_vertically (&it, amount_to_scroll);
13310 else
13312 /* Extra precision for users who set scroll-conservatively
13313 to most-positive-fixnum: make sure the amount we scroll
13314 the window start is never less than amount_to_scroll,
13315 which was computed as distance from window bottom to
13316 point. This matters when lines at window top and lines
13317 below window bottom have different height. */
13318 struct it it1 = it;
13319 /* We use a temporary it1 because line_bottom_y can modify
13320 its argument, if it moves one line down; see there. */
13321 int start_y = line_bottom_y (&it1);
13323 do {
13324 move_it_by_lines (&it, 1, 1);
13325 it1 = it;
13326 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13329 /* If STARTP is unchanged, move it down another screen line. */
13330 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13331 move_it_by_lines (&it, 1, 1);
13332 startp = it.current.pos;
13334 else
13336 struct text_pos scroll_margin_pos = startp;
13338 /* See if point is inside the scroll margin at the top of the
13339 window. */
13340 if (this_scroll_margin)
13342 start_display (&it, w, startp);
13343 move_it_vertically (&it, this_scroll_margin);
13344 scroll_margin_pos = it.current.pos;
13347 if (PT < CHARPOS (scroll_margin_pos))
13349 /* Point is in the scroll margin at the top of the window or
13350 above what is displayed in the window. */
13351 int y0;
13353 /* Compute the vertical distance from PT to the scroll
13354 margin position. Give up if distance is greater than
13355 scroll_max. */
13356 SET_TEXT_POS (pos, PT, PT_BYTE);
13357 start_display (&it, w, pos);
13358 y0 = it.current_y;
13359 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13360 it.last_visible_y, -1,
13361 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13362 dy = it.current_y - y0;
13363 if (dy > scroll_max)
13364 return SCROLLING_FAILED;
13366 /* Compute new window start. */
13367 start_display (&it, w, startp);
13369 if (scroll_conservatively)
13370 amount_to_scroll
13371 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13372 else if (scroll_step || temp_scroll_step)
13373 amount_to_scroll = scroll_max;
13374 else
13376 aggressive = current_buffer->scroll_down_aggressively;
13377 height = WINDOW_BOX_TEXT_HEIGHT (w);
13378 if (NUMBERP (aggressive))
13380 double float_amount = XFLOATINT (aggressive) * height;
13381 amount_to_scroll = float_amount;
13382 if (amount_to_scroll == 0 && float_amount > 0)
13383 amount_to_scroll = 1;
13387 if (amount_to_scroll <= 0)
13388 return SCROLLING_FAILED;
13390 move_it_vertically_backward (&it, amount_to_scroll);
13391 startp = it.current.pos;
13395 /* Run window scroll functions. */
13396 startp = run_window_scroll_functions (window, startp);
13398 /* Display the window. Give up if new fonts are loaded, or if point
13399 doesn't appear. */
13400 if (!try_window (window, startp, 0))
13401 rc = SCROLLING_NEED_LARGER_MATRICES;
13402 else if (w->cursor.vpos < 0)
13404 clear_glyph_matrix (w->desired_matrix);
13405 rc = SCROLLING_FAILED;
13407 else
13409 /* Maybe forget recorded base line for line number display. */
13410 if (!just_this_one_p
13411 || current_buffer->clip_changed
13412 || BEG_UNCHANGED < CHARPOS (startp))
13413 w->base_line_number = Qnil;
13415 /* If cursor ends up on a partially visible line,
13416 treat that as being off the bottom of the screen. */
13417 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13419 clear_glyph_matrix (w->desired_matrix);
13420 ++extra_scroll_margin_lines;
13421 goto too_near_end;
13423 rc = SCROLLING_SUCCESS;
13426 return rc;
13430 /* Compute a suitable window start for window W if display of W starts
13431 on a continuation line. Value is non-zero if a new window start
13432 was computed.
13434 The new window start will be computed, based on W's width, starting
13435 from the start of the continued line. It is the start of the
13436 screen line with the minimum distance from the old start W->start. */
13438 static int
13439 compute_window_start_on_continuation_line (struct window *w)
13441 struct text_pos pos, start_pos;
13442 int window_start_changed_p = 0;
13444 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13446 /* If window start is on a continuation line... Window start may be
13447 < BEGV in case there's invisible text at the start of the
13448 buffer (M-x rmail, for example). */
13449 if (CHARPOS (start_pos) > BEGV
13450 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13452 struct it it;
13453 struct glyph_row *row;
13455 /* Handle the case that the window start is out of range. */
13456 if (CHARPOS (start_pos) < BEGV)
13457 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13458 else if (CHARPOS (start_pos) > ZV)
13459 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13461 /* Find the start of the continued line. This should be fast
13462 because scan_buffer is fast (newline cache). */
13463 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13464 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13465 row, DEFAULT_FACE_ID);
13466 reseat_at_previous_visible_line_start (&it);
13468 /* If the line start is "too far" away from the window start,
13469 say it takes too much time to compute a new window start. */
13470 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13471 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13473 int min_distance, distance;
13475 /* Move forward by display lines to find the new window
13476 start. If window width was enlarged, the new start can
13477 be expected to be > the old start. If window width was
13478 decreased, the new window start will be < the old start.
13479 So, we're looking for the display line start with the
13480 minimum distance from the old window start. */
13481 pos = it.current.pos;
13482 min_distance = INFINITY;
13483 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13484 distance < min_distance)
13486 min_distance = distance;
13487 pos = it.current.pos;
13488 move_it_by_lines (&it, 1, 0);
13491 /* Set the window start there. */
13492 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13493 window_start_changed_p = 1;
13497 return window_start_changed_p;
13501 /* Try cursor movement in case text has not changed in window WINDOW,
13502 with window start STARTP. Value is
13504 CURSOR_MOVEMENT_SUCCESS if successful
13506 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13508 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13509 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13510 we want to scroll as if scroll-step were set to 1. See the code.
13512 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13513 which case we have to abort this redisplay, and adjust matrices
13514 first. */
13516 enum
13518 CURSOR_MOVEMENT_SUCCESS,
13519 CURSOR_MOVEMENT_CANNOT_BE_USED,
13520 CURSOR_MOVEMENT_MUST_SCROLL,
13521 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13524 static int
13525 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13527 struct window *w = XWINDOW (window);
13528 struct frame *f = XFRAME (w->frame);
13529 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13531 #if GLYPH_DEBUG
13532 if (inhibit_try_cursor_movement)
13533 return rc;
13534 #endif
13536 /* Handle case where text has not changed, only point, and it has
13537 not moved off the frame. */
13538 if (/* Point may be in this window. */
13539 PT >= CHARPOS (startp)
13540 /* Selective display hasn't changed. */
13541 && !current_buffer->clip_changed
13542 /* Function force-mode-line-update is used to force a thorough
13543 redisplay. It sets either windows_or_buffers_changed or
13544 update_mode_lines. So don't take a shortcut here for these
13545 cases. */
13546 && !update_mode_lines
13547 && !windows_or_buffers_changed
13548 && !cursor_type_changed
13549 /* Can't use this case if highlighting a region. When a
13550 region exists, cursor movement has to do more than just
13551 set the cursor. */
13552 && !(!NILP (Vtransient_mark_mode)
13553 && !NILP (current_buffer->mark_active))
13554 && NILP (w->region_showing)
13555 && NILP (Vshow_trailing_whitespace)
13556 /* Right after splitting windows, last_point may be nil. */
13557 && INTEGERP (w->last_point)
13558 /* This code is not used for mini-buffer for the sake of the case
13559 of redisplaying to replace an echo area message; since in
13560 that case the mini-buffer contents per se are usually
13561 unchanged. This code is of no real use in the mini-buffer
13562 since the handling of this_line_start_pos, etc., in redisplay
13563 handles the same cases. */
13564 && !EQ (window, minibuf_window)
13565 /* When splitting windows or for new windows, it happens that
13566 redisplay is called with a nil window_end_vpos or one being
13567 larger than the window. This should really be fixed in
13568 window.c. I don't have this on my list, now, so we do
13569 approximately the same as the old redisplay code. --gerd. */
13570 && INTEGERP (w->window_end_vpos)
13571 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13572 && (FRAME_WINDOW_P (f)
13573 || !overlay_arrow_in_current_buffer_p ()))
13575 int this_scroll_margin, top_scroll_margin;
13576 struct glyph_row *row = NULL;
13578 #if GLYPH_DEBUG
13579 debug_method_add (w, "cursor movement");
13580 #endif
13582 /* Scroll if point within this distance from the top or bottom
13583 of the window. This is a pixel value. */
13584 if (scroll_margin > 0)
13586 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13587 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13589 else
13590 this_scroll_margin = 0;
13592 top_scroll_margin = this_scroll_margin;
13593 if (WINDOW_WANTS_HEADER_LINE_P (w))
13594 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13596 /* Start with the row the cursor was displayed during the last
13597 not paused redisplay. Give up if that row is not valid. */
13598 if (w->last_cursor.vpos < 0
13599 || w->last_cursor.vpos >= w->current_matrix->nrows)
13600 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13601 else
13603 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13604 if (row->mode_line_p)
13605 ++row;
13606 if (!row->enabled_p)
13607 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13610 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13612 int scroll_p = 0, must_scroll = 0;
13613 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13615 if (PT > XFASTINT (w->last_point))
13617 /* Point has moved forward. */
13618 while (MATRIX_ROW_END_CHARPOS (row) < PT
13619 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13621 xassert (row->enabled_p);
13622 ++row;
13625 /* If the end position of a row equals the start
13626 position of the next row, and PT is at that position,
13627 we would rather display cursor in the next line. */
13628 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13629 && MATRIX_ROW_END_CHARPOS (row) == PT
13630 && row < w->current_matrix->rows
13631 + w->current_matrix->nrows - 1
13632 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13633 && !cursor_row_p (w, row))
13634 ++row;
13636 /* If within the scroll margin, scroll. Note that
13637 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13638 the next line would be drawn, and that
13639 this_scroll_margin can be zero. */
13640 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13641 || PT > MATRIX_ROW_END_CHARPOS (row)
13642 /* Line is completely visible last line in window
13643 and PT is to be set in the next line. */
13644 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13645 && PT == MATRIX_ROW_END_CHARPOS (row)
13646 && !row->ends_at_zv_p
13647 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13648 scroll_p = 1;
13650 else if (PT < XFASTINT (w->last_point))
13652 /* Cursor has to be moved backward. Note that PT >=
13653 CHARPOS (startp) because of the outer if-statement. */
13654 while (!row->mode_line_p
13655 && (MATRIX_ROW_START_CHARPOS (row) > PT
13656 || (MATRIX_ROW_START_CHARPOS (row) == PT
13657 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13658 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13659 row > w->current_matrix->rows
13660 && (row-1)->ends_in_newline_from_string_p))))
13661 && (row->y > top_scroll_margin
13662 || CHARPOS (startp) == BEGV))
13664 xassert (row->enabled_p);
13665 --row;
13668 /* Consider the following case: Window starts at BEGV,
13669 there is invisible, intangible text at BEGV, so that
13670 display starts at some point START > BEGV. It can
13671 happen that we are called with PT somewhere between
13672 BEGV and START. Try to handle that case. */
13673 if (row < w->current_matrix->rows
13674 || row->mode_line_p)
13676 row = w->current_matrix->rows;
13677 if (row->mode_line_p)
13678 ++row;
13681 /* Due to newlines in overlay strings, we may have to
13682 skip forward over overlay strings. */
13683 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13684 && MATRIX_ROW_END_CHARPOS (row) == PT
13685 && !cursor_row_p (w, row))
13686 ++row;
13688 /* If within the scroll margin, scroll. */
13689 if (row->y < top_scroll_margin
13690 && CHARPOS (startp) != BEGV)
13691 scroll_p = 1;
13693 else
13695 /* Cursor did not move. So don't scroll even if cursor line
13696 is partially visible, as it was so before. */
13697 rc = CURSOR_MOVEMENT_SUCCESS;
13700 if (PT < MATRIX_ROW_START_CHARPOS (row)
13701 || PT > MATRIX_ROW_END_CHARPOS (row))
13703 /* if PT is not in the glyph row, give up. */
13704 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13705 must_scroll = 1;
13707 else if (rc != CURSOR_MOVEMENT_SUCCESS
13708 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13710 /* If rows are bidi-reordered and point moved, back up
13711 until we find a row that does not belong to a
13712 continuation line. This is because we must consider
13713 all rows of a continued line as candidates for the
13714 new cursor positioning, since row start and end
13715 positions change non-linearly with vertical position
13716 in such rows. */
13717 /* FIXME: Revisit this when glyph ``spilling'' in
13718 continuation lines' rows is implemented for
13719 bidi-reordered rows. */
13720 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13722 xassert (row->enabled_p);
13723 --row;
13724 /* If we hit the beginning of the displayed portion
13725 without finding the first row of a continued
13726 line, give up. */
13727 if (row <= w->current_matrix->rows)
13729 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13730 break;
13735 if (must_scroll)
13737 else if (rc != CURSOR_MOVEMENT_SUCCESS
13738 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13739 && make_cursor_line_fully_visible_p)
13741 if (PT == MATRIX_ROW_END_CHARPOS (row)
13742 && !row->ends_at_zv_p
13743 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13744 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13745 else if (row->height > window_box_height (w))
13747 /* If we end up in a partially visible line, let's
13748 make it fully visible, except when it's taller
13749 than the window, in which case we can't do much
13750 about it. */
13751 *scroll_step = 1;
13752 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13754 else
13756 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13757 if (!cursor_row_fully_visible_p (w, 0, 1))
13758 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13759 else
13760 rc = CURSOR_MOVEMENT_SUCCESS;
13763 else if (scroll_p)
13764 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13765 else if (rc != CURSOR_MOVEMENT_SUCCESS
13766 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13768 /* With bidi-reordered rows, there could be more than
13769 one candidate row whose start and end positions
13770 occlude point. We need to let set_cursor_from_row
13771 find the best candidate. */
13772 /* FIXME: Revisit this when glyph ``spilling'' in
13773 continuation lines' rows is implemented for
13774 bidi-reordered rows. */
13775 int rv = 0;
13779 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13780 && PT <= MATRIX_ROW_END_CHARPOS (row)
13781 && cursor_row_p (w, row))
13782 rv |= set_cursor_from_row (w, row, w->current_matrix,
13783 0, 0, 0, 0);
13784 /* As soon as we've found the first suitable row
13785 whose ends_at_zv_p flag is set, we are done. */
13786 if (rv
13787 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13789 rc = CURSOR_MOVEMENT_SUCCESS;
13790 break;
13792 ++row;
13794 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13795 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13796 || (MATRIX_ROW_START_CHARPOS (row) == PT
13797 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13798 /* If we didn't find any candidate rows, or exited the
13799 loop before all the candidates were examined, signal
13800 to the caller that this method failed. */
13801 if (rc != CURSOR_MOVEMENT_SUCCESS
13802 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13803 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13804 else if (rv)
13805 rc = CURSOR_MOVEMENT_SUCCESS;
13807 else
13811 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13813 rc = CURSOR_MOVEMENT_SUCCESS;
13814 break;
13816 ++row;
13818 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13819 && MATRIX_ROW_START_CHARPOS (row) == PT
13820 && cursor_row_p (w, row));
13825 return rc;
13828 void
13829 set_vertical_scroll_bar (struct window *w)
13831 EMACS_INT start, end, whole;
13833 /* Calculate the start and end positions for the current window.
13834 At some point, it would be nice to choose between scrollbars
13835 which reflect the whole buffer size, with special markers
13836 indicating narrowing, and scrollbars which reflect only the
13837 visible region.
13839 Note that mini-buffers sometimes aren't displaying any text. */
13840 if (!MINI_WINDOW_P (w)
13841 || (w == XWINDOW (minibuf_window)
13842 && NILP (echo_area_buffer[0])))
13844 struct buffer *buf = XBUFFER (w->buffer);
13845 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13846 start = marker_position (w->start) - BUF_BEGV (buf);
13847 /* I don't think this is guaranteed to be right. For the
13848 moment, we'll pretend it is. */
13849 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13851 if (end < start)
13852 end = start;
13853 if (whole < (end - start))
13854 whole = end - start;
13856 else
13857 start = end = whole = 0;
13859 /* Indicate what this scroll bar ought to be displaying now. */
13860 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13861 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13862 (w, end - start, whole, start);
13866 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13867 selected_window is redisplayed.
13869 We can return without actually redisplaying the window if
13870 fonts_changed_p is nonzero. In that case, redisplay_internal will
13871 retry. */
13873 static void
13874 redisplay_window (Lisp_Object window, int just_this_one_p)
13876 struct window *w = XWINDOW (window);
13877 struct frame *f = XFRAME (w->frame);
13878 struct buffer *buffer = XBUFFER (w->buffer);
13879 struct buffer *old = current_buffer;
13880 struct text_pos lpoint, opoint, startp;
13881 int update_mode_line;
13882 int tem;
13883 struct it it;
13884 /* Record it now because it's overwritten. */
13885 int current_matrix_up_to_date_p = 0;
13886 int used_current_matrix_p = 0;
13887 /* This is less strict than current_matrix_up_to_date_p.
13888 It indictes that the buffer contents and narrowing are unchanged. */
13889 int buffer_unchanged_p = 0;
13890 int temp_scroll_step = 0;
13891 int count = SPECPDL_INDEX ();
13892 int rc;
13893 int centering_position = -1;
13894 int last_line_misfit = 0;
13895 EMACS_INT beg_unchanged, end_unchanged;
13897 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13898 opoint = lpoint;
13900 /* W must be a leaf window here. */
13901 xassert (!NILP (w->buffer));
13902 #if GLYPH_DEBUG
13903 *w->desired_matrix->method = 0;
13904 #endif
13906 restart:
13907 reconsider_clip_changes (w, buffer);
13909 /* Has the mode line to be updated? */
13910 update_mode_line = (!NILP (w->update_mode_line)
13911 || update_mode_lines
13912 || buffer->clip_changed
13913 || buffer->prevent_redisplay_optimizations_p);
13915 if (MINI_WINDOW_P (w))
13917 if (w == XWINDOW (echo_area_window)
13918 && !NILP (echo_area_buffer[0]))
13920 if (update_mode_line)
13921 /* We may have to update a tty frame's menu bar or a
13922 tool-bar. Example `M-x C-h C-h C-g'. */
13923 goto finish_menu_bars;
13924 else
13925 /* We've already displayed the echo area glyphs in this window. */
13926 goto finish_scroll_bars;
13928 else if ((w != XWINDOW (minibuf_window)
13929 || minibuf_level == 0)
13930 /* When buffer is nonempty, redisplay window normally. */
13931 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13932 /* Quail displays non-mini buffers in minibuffer window.
13933 In that case, redisplay the window normally. */
13934 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13936 /* W is a mini-buffer window, but it's not active, so clear
13937 it. */
13938 int yb = window_text_bottom_y (w);
13939 struct glyph_row *row;
13940 int y;
13942 for (y = 0, row = w->desired_matrix->rows;
13943 y < yb;
13944 y += row->height, ++row)
13945 blank_row (w, row, y);
13946 goto finish_scroll_bars;
13949 clear_glyph_matrix (w->desired_matrix);
13952 /* Otherwise set up data on this window; select its buffer and point
13953 value. */
13954 /* Really select the buffer, for the sake of buffer-local
13955 variables. */
13956 set_buffer_internal_1 (XBUFFER (w->buffer));
13958 current_matrix_up_to_date_p
13959 = (!NILP (w->window_end_valid)
13960 && !current_buffer->clip_changed
13961 && !current_buffer->prevent_redisplay_optimizations_p
13962 && XFASTINT (w->last_modified) >= MODIFF
13963 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13965 /* Run the window-bottom-change-functions
13966 if it is possible that the text on the screen has changed
13967 (either due to modification of the text, or any other reason). */
13968 if (!current_matrix_up_to_date_p
13969 && !NILP (Vwindow_text_change_functions))
13971 safe_run_hooks (Qwindow_text_change_functions);
13972 goto restart;
13975 beg_unchanged = BEG_UNCHANGED;
13976 end_unchanged = END_UNCHANGED;
13978 SET_TEXT_POS (opoint, PT, PT_BYTE);
13980 specbind (Qinhibit_point_motion_hooks, Qt);
13982 buffer_unchanged_p
13983 = (!NILP (w->window_end_valid)
13984 && !current_buffer->clip_changed
13985 && XFASTINT (w->last_modified) >= MODIFF
13986 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13988 /* When windows_or_buffers_changed is non-zero, we can't rely on
13989 the window end being valid, so set it to nil there. */
13990 if (windows_or_buffers_changed)
13992 /* If window starts on a continuation line, maybe adjust the
13993 window start in case the window's width changed. */
13994 if (XMARKER (w->start)->buffer == current_buffer)
13995 compute_window_start_on_continuation_line (w);
13997 w->window_end_valid = Qnil;
14000 /* Some sanity checks. */
14001 CHECK_WINDOW_END (w);
14002 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14003 abort ();
14004 if (BYTEPOS (opoint) < CHARPOS (opoint))
14005 abort ();
14007 /* If %c is in mode line, update it if needed. */
14008 if (!NILP (w->column_number_displayed)
14009 /* This alternative quickly identifies a common case
14010 where no change is needed. */
14011 && !(PT == XFASTINT (w->last_point)
14012 && XFASTINT (w->last_modified) >= MODIFF
14013 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14014 && (XFASTINT (w->column_number_displayed)
14015 != (int) current_column ())) /* iftc */
14016 update_mode_line = 1;
14018 /* Count number of windows showing the selected buffer. An indirect
14019 buffer counts as its base buffer. */
14020 if (!just_this_one_p)
14022 struct buffer *current_base, *window_base;
14023 current_base = current_buffer;
14024 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14025 if (current_base->base_buffer)
14026 current_base = current_base->base_buffer;
14027 if (window_base->base_buffer)
14028 window_base = window_base->base_buffer;
14029 if (current_base == window_base)
14030 buffer_shared++;
14033 /* Point refers normally to the selected window. For any other
14034 window, set up appropriate value. */
14035 if (!EQ (window, selected_window))
14037 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14038 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14039 if (new_pt < BEGV)
14041 new_pt = BEGV;
14042 new_pt_byte = BEGV_BYTE;
14043 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14045 else if (new_pt > (ZV - 1))
14047 new_pt = ZV;
14048 new_pt_byte = ZV_BYTE;
14049 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14052 /* We don't use SET_PT so that the point-motion hooks don't run. */
14053 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14056 /* If any of the character widths specified in the display table
14057 have changed, invalidate the width run cache. It's true that
14058 this may be a bit late to catch such changes, but the rest of
14059 redisplay goes (non-fatally) haywire when the display table is
14060 changed, so why should we worry about doing any better? */
14061 if (current_buffer->width_run_cache)
14063 struct Lisp_Char_Table *disptab = buffer_display_table ();
14065 if (! disptab_matches_widthtab (disptab,
14066 XVECTOR (current_buffer->width_table)))
14068 invalidate_region_cache (current_buffer,
14069 current_buffer->width_run_cache,
14070 BEG, Z);
14071 recompute_width_table (current_buffer, disptab);
14075 /* If window-start is screwed up, choose a new one. */
14076 if (XMARKER (w->start)->buffer != current_buffer)
14077 goto recenter;
14079 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14081 /* If someone specified a new starting point but did not insist,
14082 check whether it can be used. */
14083 if (!NILP (w->optional_new_start)
14084 && CHARPOS (startp) >= BEGV
14085 && CHARPOS (startp) <= ZV)
14087 w->optional_new_start = Qnil;
14088 start_display (&it, w, startp);
14089 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14090 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14091 if (IT_CHARPOS (it) == PT)
14092 w->force_start = Qt;
14093 /* IT may overshoot PT if text at PT is invisible. */
14094 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14095 w->force_start = Qt;
14098 force_start:
14100 /* Handle case where place to start displaying has been specified,
14101 unless the specified location is outside the accessible range. */
14102 if (!NILP (w->force_start)
14103 || w->frozen_window_start_p)
14105 /* We set this later on if we have to adjust point. */
14106 int new_vpos = -1;
14108 w->force_start = Qnil;
14109 w->vscroll = 0;
14110 w->window_end_valid = Qnil;
14112 /* Forget any recorded base line for line number display. */
14113 if (!buffer_unchanged_p)
14114 w->base_line_number = Qnil;
14116 /* Redisplay the mode line. Select the buffer properly for that.
14117 Also, run the hook window-scroll-functions
14118 because we have scrolled. */
14119 /* Note, we do this after clearing force_start because
14120 if there's an error, it is better to forget about force_start
14121 than to get into an infinite loop calling the hook functions
14122 and having them get more errors. */
14123 if (!update_mode_line
14124 || ! NILP (Vwindow_scroll_functions))
14126 update_mode_line = 1;
14127 w->update_mode_line = Qt;
14128 startp = run_window_scroll_functions (window, startp);
14131 w->last_modified = make_number (0);
14132 w->last_overlay_modified = make_number (0);
14133 if (CHARPOS (startp) < BEGV)
14134 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14135 else if (CHARPOS (startp) > ZV)
14136 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14138 /* Redisplay, then check if cursor has been set during the
14139 redisplay. Give up if new fonts were loaded. */
14140 /* We used to issue a CHECK_MARGINS argument to try_window here,
14141 but this causes scrolling to fail when point begins inside
14142 the scroll margin (bug#148) -- cyd */
14143 if (!try_window (window, startp, 0))
14145 w->force_start = Qt;
14146 clear_glyph_matrix (w->desired_matrix);
14147 goto need_larger_matrices;
14150 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14152 /* If point does not appear, try to move point so it does
14153 appear. The desired matrix has been built above, so we
14154 can use it here. */
14155 new_vpos = window_box_height (w) / 2;
14158 if (!cursor_row_fully_visible_p (w, 0, 0))
14160 /* Point does appear, but on a line partly visible at end of window.
14161 Move it back to a fully-visible line. */
14162 new_vpos = window_box_height (w);
14165 /* If we need to move point for either of the above reasons,
14166 now actually do it. */
14167 if (new_vpos >= 0)
14169 struct glyph_row *row;
14171 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14172 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14173 ++row;
14175 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14176 MATRIX_ROW_START_BYTEPOS (row));
14178 if (w != XWINDOW (selected_window))
14179 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14180 else if (current_buffer == old)
14181 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14183 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14185 /* If we are highlighting the region, then we just changed
14186 the region, so redisplay to show it. */
14187 if (!NILP (Vtransient_mark_mode)
14188 && !NILP (current_buffer->mark_active))
14190 clear_glyph_matrix (w->desired_matrix);
14191 if (!try_window (window, startp, 0))
14192 goto need_larger_matrices;
14196 #if GLYPH_DEBUG
14197 debug_method_add (w, "forced window start");
14198 #endif
14199 goto done;
14202 /* Handle case where text has not changed, only point, and it has
14203 not moved off the frame, and we are not retrying after hscroll.
14204 (current_matrix_up_to_date_p is nonzero when retrying.) */
14205 if (current_matrix_up_to_date_p
14206 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14207 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14209 switch (rc)
14211 case CURSOR_MOVEMENT_SUCCESS:
14212 used_current_matrix_p = 1;
14213 goto done;
14215 case CURSOR_MOVEMENT_MUST_SCROLL:
14216 goto try_to_scroll;
14218 default:
14219 abort ();
14222 /* If current starting point was originally the beginning of a line
14223 but no longer is, find a new starting point. */
14224 else if (!NILP (w->start_at_line_beg)
14225 && !(CHARPOS (startp) <= BEGV
14226 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14228 #if GLYPH_DEBUG
14229 debug_method_add (w, "recenter 1");
14230 #endif
14231 goto recenter;
14234 /* Try scrolling with try_window_id. Value is > 0 if update has
14235 been done, it is -1 if we know that the same window start will
14236 not work. It is 0 if unsuccessful for some other reason. */
14237 else if ((tem = try_window_id (w)) != 0)
14239 #if GLYPH_DEBUG
14240 debug_method_add (w, "try_window_id %d", tem);
14241 #endif
14243 if (fonts_changed_p)
14244 goto need_larger_matrices;
14245 if (tem > 0)
14246 goto done;
14248 /* Otherwise try_window_id has returned -1 which means that we
14249 don't want the alternative below this comment to execute. */
14251 else if (CHARPOS (startp) >= BEGV
14252 && CHARPOS (startp) <= ZV
14253 && PT >= CHARPOS (startp)
14254 && (CHARPOS (startp) < ZV
14255 /* Avoid starting at end of buffer. */
14256 || CHARPOS (startp) == BEGV
14257 || (XFASTINT (w->last_modified) >= MODIFF
14258 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14261 /* If first window line is a continuation line, and window start
14262 is inside the modified region, but the first change is before
14263 current window start, we must select a new window start.
14265 However, if this is the result of a down-mouse event (e.g. by
14266 extending the mouse-drag-overlay), we don't want to select a
14267 new window start, since that would change the position under
14268 the mouse, resulting in an unwanted mouse-movement rather
14269 than a simple mouse-click. */
14270 if (NILP (w->start_at_line_beg)
14271 && NILP (do_mouse_tracking)
14272 && CHARPOS (startp) > BEGV
14273 && CHARPOS (startp) > BEG + beg_unchanged
14274 && CHARPOS (startp) <= Z - end_unchanged
14275 /* Even if w->start_at_line_beg is nil, a new window may
14276 start at a line_beg, since that's how set_buffer_window
14277 sets it. So, we need to check the return value of
14278 compute_window_start_on_continuation_line. (See also
14279 bug#197). */
14280 && XMARKER (w->start)->buffer == current_buffer
14281 && compute_window_start_on_continuation_line (w))
14283 w->force_start = Qt;
14284 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14285 goto force_start;
14288 #if GLYPH_DEBUG
14289 debug_method_add (w, "same window start");
14290 #endif
14292 /* Try to redisplay starting at same place as before.
14293 If point has not moved off frame, accept the results. */
14294 if (!current_matrix_up_to_date_p
14295 /* Don't use try_window_reusing_current_matrix in this case
14296 because a window scroll function can have changed the
14297 buffer. */
14298 || !NILP (Vwindow_scroll_functions)
14299 || MINI_WINDOW_P (w)
14300 || !(used_current_matrix_p
14301 = try_window_reusing_current_matrix (w)))
14303 IF_DEBUG (debug_method_add (w, "1"));
14304 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14305 /* -1 means we need to scroll.
14306 0 means we need new matrices, but fonts_changed_p
14307 is set in that case, so we will detect it below. */
14308 goto try_to_scroll;
14311 if (fonts_changed_p)
14312 goto need_larger_matrices;
14314 if (w->cursor.vpos >= 0)
14316 if (!just_this_one_p
14317 || current_buffer->clip_changed
14318 || BEG_UNCHANGED < CHARPOS (startp))
14319 /* Forget any recorded base line for line number display. */
14320 w->base_line_number = Qnil;
14322 if (!cursor_row_fully_visible_p (w, 1, 0))
14324 clear_glyph_matrix (w->desired_matrix);
14325 last_line_misfit = 1;
14327 /* Drop through and scroll. */
14328 else
14329 goto done;
14331 else
14332 clear_glyph_matrix (w->desired_matrix);
14335 try_to_scroll:
14337 w->last_modified = make_number (0);
14338 w->last_overlay_modified = make_number (0);
14340 /* Redisplay the mode line. Select the buffer properly for that. */
14341 if (!update_mode_line)
14343 update_mode_line = 1;
14344 w->update_mode_line = Qt;
14347 /* Try to scroll by specified few lines. */
14348 if ((scroll_conservatively
14349 || scroll_step
14350 || temp_scroll_step
14351 || NUMBERP (current_buffer->scroll_up_aggressively)
14352 || NUMBERP (current_buffer->scroll_down_aggressively))
14353 && !current_buffer->clip_changed
14354 && CHARPOS (startp) >= BEGV
14355 && CHARPOS (startp) <= ZV)
14357 /* The function returns -1 if new fonts were loaded, 1 if
14358 successful, 0 if not successful. */
14359 int rc = try_scrolling (window, just_this_one_p,
14360 scroll_conservatively,
14361 scroll_step,
14362 temp_scroll_step, last_line_misfit);
14363 switch (rc)
14365 case SCROLLING_SUCCESS:
14366 goto done;
14368 case SCROLLING_NEED_LARGER_MATRICES:
14369 goto need_larger_matrices;
14371 case SCROLLING_FAILED:
14372 break;
14374 default:
14375 abort ();
14379 /* Finally, just choose place to start which centers point */
14381 recenter:
14382 if (centering_position < 0)
14383 centering_position = window_box_height (w) / 2;
14385 #if GLYPH_DEBUG
14386 debug_method_add (w, "recenter");
14387 #endif
14389 /* w->vscroll = 0; */
14391 /* Forget any previously recorded base line for line number display. */
14392 if (!buffer_unchanged_p)
14393 w->base_line_number = Qnil;
14395 /* Move backward half the height of the window. */
14396 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14397 it.current_y = it.last_visible_y;
14398 move_it_vertically_backward (&it, centering_position);
14399 xassert (IT_CHARPOS (it) >= BEGV);
14401 /* The function move_it_vertically_backward may move over more
14402 than the specified y-distance. If it->w is small, e.g. a
14403 mini-buffer window, we may end up in front of the window's
14404 display area. Start displaying at the start of the line
14405 containing PT in this case. */
14406 if (it.current_y <= 0)
14408 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14409 move_it_vertically_backward (&it, 0);
14410 it.current_y = 0;
14413 it.current_x = it.hpos = 0;
14415 /* Set startp here explicitly in case that helps avoid an infinite loop
14416 in case the window-scroll-functions functions get errors. */
14417 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14419 /* Run scroll hooks. */
14420 startp = run_window_scroll_functions (window, it.current.pos);
14422 /* Redisplay the window. */
14423 if (!current_matrix_up_to_date_p
14424 || windows_or_buffers_changed
14425 || cursor_type_changed
14426 /* Don't use try_window_reusing_current_matrix in this case
14427 because it can have changed the buffer. */
14428 || !NILP (Vwindow_scroll_functions)
14429 || !just_this_one_p
14430 || MINI_WINDOW_P (w)
14431 || !(used_current_matrix_p
14432 = try_window_reusing_current_matrix (w)))
14433 try_window (window, startp, 0);
14435 /* If new fonts have been loaded (due to fontsets), give up. We
14436 have to start a new redisplay since we need to re-adjust glyph
14437 matrices. */
14438 if (fonts_changed_p)
14439 goto need_larger_matrices;
14441 /* If cursor did not appear assume that the middle of the window is
14442 in the first line of the window. Do it again with the next line.
14443 (Imagine a window of height 100, displaying two lines of height
14444 60. Moving back 50 from it->last_visible_y will end in the first
14445 line.) */
14446 if (w->cursor.vpos < 0)
14448 if (!NILP (w->window_end_valid)
14449 && PT >= Z - XFASTINT (w->window_end_pos))
14451 clear_glyph_matrix (w->desired_matrix);
14452 move_it_by_lines (&it, 1, 0);
14453 try_window (window, it.current.pos, 0);
14455 else if (PT < IT_CHARPOS (it))
14457 clear_glyph_matrix (w->desired_matrix);
14458 move_it_by_lines (&it, -1, 0);
14459 try_window (window, it.current.pos, 0);
14461 else
14463 /* Not much we can do about it. */
14467 /* Consider the following case: Window starts at BEGV, there is
14468 invisible, intangible text at BEGV, so that display starts at
14469 some point START > BEGV. It can happen that we are called with
14470 PT somewhere between BEGV and START. Try to handle that case. */
14471 if (w->cursor.vpos < 0)
14473 struct glyph_row *row = w->current_matrix->rows;
14474 if (row->mode_line_p)
14475 ++row;
14476 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14479 if (!cursor_row_fully_visible_p (w, 0, 0))
14481 /* If vscroll is enabled, disable it and try again. */
14482 if (w->vscroll)
14484 w->vscroll = 0;
14485 clear_glyph_matrix (w->desired_matrix);
14486 goto recenter;
14489 /* If centering point failed to make the whole line visible,
14490 put point at the top instead. That has to make the whole line
14491 visible, if it can be done. */
14492 if (centering_position == 0)
14493 goto done;
14495 clear_glyph_matrix (w->desired_matrix);
14496 centering_position = 0;
14497 goto recenter;
14500 done:
14502 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14503 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14504 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14505 ? Qt : Qnil);
14507 /* Display the mode line, if we must. */
14508 if ((update_mode_line
14509 /* If window not full width, must redo its mode line
14510 if (a) the window to its side is being redone and
14511 (b) we do a frame-based redisplay. This is a consequence
14512 of how inverted lines are drawn in frame-based redisplay. */
14513 || (!just_this_one_p
14514 && !FRAME_WINDOW_P (f)
14515 && !WINDOW_FULL_WIDTH_P (w))
14516 /* Line number to display. */
14517 || INTEGERP (w->base_line_pos)
14518 /* Column number is displayed and different from the one displayed. */
14519 || (!NILP (w->column_number_displayed)
14520 && (XFASTINT (w->column_number_displayed)
14521 != (int) current_column ()))) /* iftc */
14522 /* This means that the window has a mode line. */
14523 && (WINDOW_WANTS_MODELINE_P (w)
14524 || WINDOW_WANTS_HEADER_LINE_P (w)))
14526 display_mode_lines (w);
14528 /* If mode line height has changed, arrange for a thorough
14529 immediate redisplay using the correct mode line height. */
14530 if (WINDOW_WANTS_MODELINE_P (w)
14531 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14533 fonts_changed_p = 1;
14534 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14535 = DESIRED_MODE_LINE_HEIGHT (w);
14538 /* If header line height has changed, arrange for a thorough
14539 immediate redisplay using the correct header line height. */
14540 if (WINDOW_WANTS_HEADER_LINE_P (w)
14541 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14543 fonts_changed_p = 1;
14544 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14545 = DESIRED_HEADER_LINE_HEIGHT (w);
14548 if (fonts_changed_p)
14549 goto need_larger_matrices;
14552 if (!line_number_displayed
14553 && !BUFFERP (w->base_line_pos))
14555 w->base_line_pos = Qnil;
14556 w->base_line_number = Qnil;
14559 finish_menu_bars:
14561 /* When we reach a frame's selected window, redo the frame's menu bar. */
14562 if (update_mode_line
14563 && EQ (FRAME_SELECTED_WINDOW (f), window))
14565 int redisplay_menu_p = 0;
14566 int redisplay_tool_bar_p = 0;
14568 if (FRAME_WINDOW_P (f))
14570 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14571 || defined (HAVE_NS) || defined (USE_GTK)
14572 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14573 #else
14574 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14575 #endif
14577 else
14578 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14580 if (redisplay_menu_p)
14581 display_menu_bar (w);
14583 #ifdef HAVE_WINDOW_SYSTEM
14584 if (FRAME_WINDOW_P (f))
14586 #if defined (USE_GTK) || defined (HAVE_NS)
14587 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14588 #else
14589 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14590 && (FRAME_TOOL_BAR_LINES (f) > 0
14591 || !NILP (Vauto_resize_tool_bars));
14592 #endif
14594 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14596 ignore_mouse_drag_p = 1;
14599 #endif
14602 #ifdef HAVE_WINDOW_SYSTEM
14603 if (FRAME_WINDOW_P (f)
14604 && update_window_fringes (w, (just_this_one_p
14605 || (!used_current_matrix_p && !overlay_arrow_seen)
14606 || w->pseudo_window_p)))
14608 update_begin (f);
14609 BLOCK_INPUT;
14610 if (draw_window_fringes (w, 1))
14611 x_draw_vertical_border (w);
14612 UNBLOCK_INPUT;
14613 update_end (f);
14615 #endif /* HAVE_WINDOW_SYSTEM */
14617 /* We go to this label, with fonts_changed_p nonzero,
14618 if it is necessary to try again using larger glyph matrices.
14619 We have to redeem the scroll bar even in this case,
14620 because the loop in redisplay_internal expects that. */
14621 need_larger_matrices:
14623 finish_scroll_bars:
14625 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14627 /* Set the thumb's position and size. */
14628 set_vertical_scroll_bar (w);
14630 /* Note that we actually used the scroll bar attached to this
14631 window, so it shouldn't be deleted at the end of redisplay. */
14632 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14633 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14636 /* Restore current_buffer and value of point in it. The window
14637 update may have changed the buffer, so first make sure `opoint'
14638 is still valid (Bug#6177). */
14639 if (CHARPOS (opoint) < BEGV)
14640 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14641 else if (CHARPOS (opoint) > ZV)
14642 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14643 else
14644 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14646 set_buffer_internal_1 (old);
14647 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14648 shorter. This can be caused by log truncation in *Messages*. */
14649 if (CHARPOS (lpoint) <= ZV)
14650 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14652 unbind_to (count, Qnil);
14656 /* Build the complete desired matrix of WINDOW with a window start
14657 buffer position POS.
14659 Value is 1 if successful. It is zero if fonts were loaded during
14660 redisplay which makes re-adjusting glyph matrices necessary, and -1
14661 if point would appear in the scroll margins.
14662 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14663 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14664 set in FLAGS.) */
14667 try_window (Lisp_Object window, struct text_pos pos, int flags)
14669 struct window *w = XWINDOW (window);
14670 struct it it;
14671 struct glyph_row *last_text_row = NULL;
14672 struct frame *f = XFRAME (w->frame);
14674 /* Make POS the new window start. */
14675 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14677 /* Mark cursor position as unknown. No overlay arrow seen. */
14678 w->cursor.vpos = -1;
14679 overlay_arrow_seen = 0;
14681 /* Initialize iterator and info to start at POS. */
14682 start_display (&it, w, pos);
14684 /* Display all lines of W. */
14685 while (it.current_y < it.last_visible_y)
14687 if (display_line (&it))
14688 last_text_row = it.glyph_row - 1;
14689 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14690 return 0;
14693 /* Don't let the cursor end in the scroll margins. */
14694 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14695 && !MINI_WINDOW_P (w))
14697 int this_scroll_margin;
14699 if (scroll_margin > 0)
14701 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14702 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14704 else
14705 this_scroll_margin = 0;
14707 if ((w->cursor.y >= 0 /* not vscrolled */
14708 && w->cursor.y < this_scroll_margin
14709 && CHARPOS (pos) > BEGV
14710 && IT_CHARPOS (it) < ZV)
14711 /* rms: considering make_cursor_line_fully_visible_p here
14712 seems to give wrong results. We don't want to recenter
14713 when the last line is partly visible, we want to allow
14714 that case to be handled in the usual way. */
14715 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14717 w->cursor.vpos = -1;
14718 clear_glyph_matrix (w->desired_matrix);
14719 return -1;
14723 /* If bottom moved off end of frame, change mode line percentage. */
14724 if (XFASTINT (w->window_end_pos) <= 0
14725 && Z != IT_CHARPOS (it))
14726 w->update_mode_line = Qt;
14728 /* Set window_end_pos to the offset of the last character displayed
14729 on the window from the end of current_buffer. Set
14730 window_end_vpos to its row number. */
14731 if (last_text_row)
14733 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14734 w->window_end_bytepos
14735 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14736 w->window_end_pos
14737 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14738 w->window_end_vpos
14739 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14740 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14741 ->displays_text_p);
14743 else
14745 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14746 w->window_end_pos = make_number (Z - ZV);
14747 w->window_end_vpos = make_number (0);
14750 /* But that is not valid info until redisplay finishes. */
14751 w->window_end_valid = Qnil;
14752 return 1;
14757 /************************************************************************
14758 Window redisplay reusing current matrix when buffer has not changed
14759 ************************************************************************/
14761 /* Try redisplay of window W showing an unchanged buffer with a
14762 different window start than the last time it was displayed by
14763 reusing its current matrix. Value is non-zero if successful.
14764 W->start is the new window start. */
14766 static int
14767 try_window_reusing_current_matrix (struct window *w)
14769 struct frame *f = XFRAME (w->frame);
14770 struct glyph_row *row, *bottom_row;
14771 struct it it;
14772 struct run run;
14773 struct text_pos start, new_start;
14774 int nrows_scrolled, i;
14775 struct glyph_row *last_text_row;
14776 struct glyph_row *last_reused_text_row;
14777 struct glyph_row *start_row;
14778 int start_vpos, min_y, max_y;
14780 #if GLYPH_DEBUG
14781 if (inhibit_try_window_reusing)
14782 return 0;
14783 #endif
14785 if (/* This function doesn't handle terminal frames. */
14786 !FRAME_WINDOW_P (f)
14787 /* Don't try to reuse the display if windows have been split
14788 or such. */
14789 || windows_or_buffers_changed
14790 || cursor_type_changed)
14791 return 0;
14793 /* Can't do this if region may have changed. */
14794 if ((!NILP (Vtransient_mark_mode)
14795 && !NILP (current_buffer->mark_active))
14796 || !NILP (w->region_showing)
14797 || !NILP (Vshow_trailing_whitespace))
14798 return 0;
14800 /* If top-line visibility has changed, give up. */
14801 if (WINDOW_WANTS_HEADER_LINE_P (w)
14802 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14803 return 0;
14805 /* Give up if old or new display is scrolled vertically. We could
14806 make this function handle this, but right now it doesn't. */
14807 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14808 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14809 return 0;
14811 /* The variable new_start now holds the new window start. The old
14812 start `start' can be determined from the current matrix. */
14813 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14814 start = start_row->minpos;
14815 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14817 /* Clear the desired matrix for the display below. */
14818 clear_glyph_matrix (w->desired_matrix);
14820 if (CHARPOS (new_start) <= CHARPOS (start))
14822 int first_row_y;
14824 /* Don't use this method if the display starts with an ellipsis
14825 displayed for invisible text. It's not easy to handle that case
14826 below, and it's certainly not worth the effort since this is
14827 not a frequent case. */
14828 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14829 return 0;
14831 IF_DEBUG (debug_method_add (w, "twu1"));
14833 /* Display up to a row that can be reused. The variable
14834 last_text_row is set to the last row displayed that displays
14835 text. Note that it.vpos == 0 if or if not there is a
14836 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14837 start_display (&it, w, new_start);
14838 first_row_y = it.current_y;
14839 w->cursor.vpos = -1;
14840 last_text_row = last_reused_text_row = NULL;
14842 while (it.current_y < it.last_visible_y
14843 && !fonts_changed_p)
14845 /* If we have reached into the characters in the START row,
14846 that means the line boundaries have changed. So we
14847 can't start copying with the row START. Maybe it will
14848 work to start copying with the following row. */
14849 while (IT_CHARPOS (it) > CHARPOS (start))
14851 /* Advance to the next row as the "start". */
14852 start_row++;
14853 start = start_row->minpos;
14854 /* If there are no more rows to try, or just one, give up. */
14855 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14856 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14857 || CHARPOS (start) == ZV)
14859 clear_glyph_matrix (w->desired_matrix);
14860 return 0;
14863 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14865 /* If we have reached alignment,
14866 we can copy the rest of the rows. */
14867 if (IT_CHARPOS (it) == CHARPOS (start))
14868 break;
14870 if (display_line (&it))
14871 last_text_row = it.glyph_row - 1;
14874 /* A value of current_y < last_visible_y means that we stopped
14875 at the previous window start, which in turn means that we
14876 have at least one reusable row. */
14877 if (it.current_y < it.last_visible_y)
14879 /* IT.vpos always starts from 0; it counts text lines. */
14880 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14882 /* Find PT if not already found in the lines displayed. */
14883 if (w->cursor.vpos < 0)
14885 int dy = it.current_y - start_row->y;
14887 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14888 row = row_containing_pos (w, PT, row, NULL, dy);
14889 if (row)
14890 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14891 dy, nrows_scrolled);
14892 else
14894 clear_glyph_matrix (w->desired_matrix);
14895 return 0;
14899 /* Scroll the display. Do it before the current matrix is
14900 changed. The problem here is that update has not yet
14901 run, i.e. part of the current matrix is not up to date.
14902 scroll_run_hook will clear the cursor, and use the
14903 current matrix to get the height of the row the cursor is
14904 in. */
14905 run.current_y = start_row->y;
14906 run.desired_y = it.current_y;
14907 run.height = it.last_visible_y - it.current_y;
14909 if (run.height > 0 && run.current_y != run.desired_y)
14911 update_begin (f);
14912 FRAME_RIF (f)->update_window_begin_hook (w);
14913 FRAME_RIF (f)->clear_window_mouse_face (w);
14914 FRAME_RIF (f)->scroll_run_hook (w, &run);
14915 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14916 update_end (f);
14919 /* Shift current matrix down by nrows_scrolled lines. */
14920 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14921 rotate_matrix (w->current_matrix,
14922 start_vpos,
14923 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14924 nrows_scrolled);
14926 /* Disable lines that must be updated. */
14927 for (i = 0; i < nrows_scrolled; ++i)
14928 (start_row + i)->enabled_p = 0;
14930 /* Re-compute Y positions. */
14931 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14932 max_y = it.last_visible_y;
14933 for (row = start_row + nrows_scrolled;
14934 row < bottom_row;
14935 ++row)
14937 row->y = it.current_y;
14938 row->visible_height = row->height;
14940 if (row->y < min_y)
14941 row->visible_height -= min_y - row->y;
14942 if (row->y + row->height > max_y)
14943 row->visible_height -= row->y + row->height - max_y;
14944 row->redraw_fringe_bitmaps_p = 1;
14946 it.current_y += row->height;
14948 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14949 last_reused_text_row = row;
14950 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14951 break;
14954 /* Disable lines in the current matrix which are now
14955 below the window. */
14956 for (++row; row < bottom_row; ++row)
14957 row->enabled_p = row->mode_line_p = 0;
14960 /* Update window_end_pos etc.; last_reused_text_row is the last
14961 reused row from the current matrix containing text, if any.
14962 The value of last_text_row is the last displayed line
14963 containing text. */
14964 if (last_reused_text_row)
14966 w->window_end_bytepos
14967 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14968 w->window_end_pos
14969 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14970 w->window_end_vpos
14971 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14972 w->current_matrix));
14974 else if (last_text_row)
14976 w->window_end_bytepos
14977 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14978 w->window_end_pos
14979 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14980 w->window_end_vpos
14981 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14983 else
14985 /* This window must be completely empty. */
14986 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14987 w->window_end_pos = make_number (Z - ZV);
14988 w->window_end_vpos = make_number (0);
14990 w->window_end_valid = Qnil;
14992 /* Update hint: don't try scrolling again in update_window. */
14993 w->desired_matrix->no_scrolling_p = 1;
14995 #if GLYPH_DEBUG
14996 debug_method_add (w, "try_window_reusing_current_matrix 1");
14997 #endif
14998 return 1;
15000 else if (CHARPOS (new_start) > CHARPOS (start))
15002 struct glyph_row *pt_row, *row;
15003 struct glyph_row *first_reusable_row;
15004 struct glyph_row *first_row_to_display;
15005 int dy;
15006 int yb = window_text_bottom_y (w);
15008 /* Find the row starting at new_start, if there is one. Don't
15009 reuse a partially visible line at the end. */
15010 first_reusable_row = start_row;
15011 while (first_reusable_row->enabled_p
15012 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15013 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15014 < CHARPOS (new_start)))
15015 ++first_reusable_row;
15017 /* Give up if there is no row to reuse. */
15018 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15019 || !first_reusable_row->enabled_p
15020 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15021 != CHARPOS (new_start)))
15022 return 0;
15024 /* We can reuse fully visible rows beginning with
15025 first_reusable_row to the end of the window. Set
15026 first_row_to_display to the first row that cannot be reused.
15027 Set pt_row to the row containing point, if there is any. */
15028 pt_row = NULL;
15029 for (first_row_to_display = first_reusable_row;
15030 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15031 ++first_row_to_display)
15033 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15034 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15035 pt_row = first_row_to_display;
15038 /* Start displaying at the start of first_row_to_display. */
15039 xassert (first_row_to_display->y < yb);
15040 init_to_row_start (&it, w, first_row_to_display);
15042 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15043 - start_vpos);
15044 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15045 - nrows_scrolled);
15046 it.current_y = (first_row_to_display->y - first_reusable_row->y
15047 + WINDOW_HEADER_LINE_HEIGHT (w));
15049 /* Display lines beginning with first_row_to_display in the
15050 desired matrix. Set last_text_row to the last row displayed
15051 that displays text. */
15052 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15053 if (pt_row == NULL)
15054 w->cursor.vpos = -1;
15055 last_text_row = NULL;
15056 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15057 if (display_line (&it))
15058 last_text_row = it.glyph_row - 1;
15060 /* If point is in a reused row, adjust y and vpos of the cursor
15061 position. */
15062 if (pt_row)
15064 w->cursor.vpos -= nrows_scrolled;
15065 w->cursor.y -= first_reusable_row->y - start_row->y;
15068 /* Give up if point isn't in a row displayed or reused. (This
15069 also handles the case where w->cursor.vpos < nrows_scrolled
15070 after the calls to display_line, which can happen with scroll
15071 margins. See bug#1295.) */
15072 if (w->cursor.vpos < 0)
15074 clear_glyph_matrix (w->desired_matrix);
15075 return 0;
15078 /* Scroll the display. */
15079 run.current_y = first_reusable_row->y;
15080 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15081 run.height = it.last_visible_y - run.current_y;
15082 dy = run.current_y - run.desired_y;
15084 if (run.height)
15086 update_begin (f);
15087 FRAME_RIF (f)->update_window_begin_hook (w);
15088 FRAME_RIF (f)->clear_window_mouse_face (w);
15089 FRAME_RIF (f)->scroll_run_hook (w, &run);
15090 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15091 update_end (f);
15094 /* Adjust Y positions of reused rows. */
15095 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15096 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15097 max_y = it.last_visible_y;
15098 for (row = first_reusable_row; row < first_row_to_display; ++row)
15100 row->y -= dy;
15101 row->visible_height = row->height;
15102 if (row->y < min_y)
15103 row->visible_height -= min_y - row->y;
15104 if (row->y + row->height > max_y)
15105 row->visible_height -= row->y + row->height - max_y;
15106 row->redraw_fringe_bitmaps_p = 1;
15109 /* Scroll the current matrix. */
15110 xassert (nrows_scrolled > 0);
15111 rotate_matrix (w->current_matrix,
15112 start_vpos,
15113 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15114 -nrows_scrolled);
15116 /* Disable rows not reused. */
15117 for (row -= nrows_scrolled; row < bottom_row; ++row)
15118 row->enabled_p = 0;
15120 /* Point may have moved to a different line, so we cannot assume that
15121 the previous cursor position is valid; locate the correct row. */
15122 if (pt_row)
15124 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15125 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15126 row++)
15128 w->cursor.vpos++;
15129 w->cursor.y = row->y;
15131 if (row < bottom_row)
15133 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15134 struct glyph *end = glyph + row->used[TEXT_AREA];
15136 /* Can't use this optimization with bidi-reordered glyph
15137 rows, unless cursor is already at point. */
15138 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15140 if (!(w->cursor.hpos >= 0
15141 && w->cursor.hpos < row->used[TEXT_AREA]
15142 && BUFFERP (glyph->object)
15143 && glyph->charpos == PT))
15144 return 0;
15146 else
15147 for (; glyph < end
15148 && (!BUFFERP (glyph->object)
15149 || glyph->charpos < PT);
15150 glyph++)
15152 w->cursor.hpos++;
15153 w->cursor.x += glyph->pixel_width;
15158 /* Adjust window end. A null value of last_text_row means that
15159 the window end is in reused rows which in turn means that
15160 only its vpos can have changed. */
15161 if (last_text_row)
15163 w->window_end_bytepos
15164 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15165 w->window_end_pos
15166 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15167 w->window_end_vpos
15168 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15170 else
15172 w->window_end_vpos
15173 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15176 w->window_end_valid = Qnil;
15177 w->desired_matrix->no_scrolling_p = 1;
15179 #if GLYPH_DEBUG
15180 debug_method_add (w, "try_window_reusing_current_matrix 2");
15181 #endif
15182 return 1;
15185 return 0;
15190 /************************************************************************
15191 Window redisplay reusing current matrix when buffer has changed
15192 ************************************************************************/
15194 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15195 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15196 EMACS_INT *, EMACS_INT *);
15197 static struct glyph_row *
15198 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15199 struct glyph_row *);
15202 /* Return the last row in MATRIX displaying text. If row START is
15203 non-null, start searching with that row. IT gives the dimensions
15204 of the display. Value is null if matrix is empty; otherwise it is
15205 a pointer to the row found. */
15207 static struct glyph_row *
15208 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15209 struct glyph_row *start)
15211 struct glyph_row *row, *row_found;
15213 /* Set row_found to the last row in IT->w's current matrix
15214 displaying text. The loop looks funny but think of partially
15215 visible lines. */
15216 row_found = NULL;
15217 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15218 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15220 xassert (row->enabled_p);
15221 row_found = row;
15222 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15223 break;
15224 ++row;
15227 return row_found;
15231 /* Return the last row in the current matrix of W that is not affected
15232 by changes at the start of current_buffer that occurred since W's
15233 current matrix was built. Value is null if no such row exists.
15235 BEG_UNCHANGED us the number of characters unchanged at the start of
15236 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15237 first changed character in current_buffer. Characters at positions <
15238 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15239 when the current matrix was built. */
15241 static struct glyph_row *
15242 find_last_unchanged_at_beg_row (struct window *w)
15244 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15245 struct glyph_row *row;
15246 struct glyph_row *row_found = NULL;
15247 int yb = window_text_bottom_y (w);
15249 /* Find the last row displaying unchanged text. */
15250 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15251 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15252 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15253 ++row)
15255 if (/* If row ends before first_changed_pos, it is unchanged,
15256 except in some case. */
15257 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15258 /* When row ends in ZV and we write at ZV it is not
15259 unchanged. */
15260 && !row->ends_at_zv_p
15261 /* When first_changed_pos is the end of a continued line,
15262 row is not unchanged because it may be no longer
15263 continued. */
15264 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15265 && (row->continued_p
15266 || row->exact_window_width_line_p)))
15267 row_found = row;
15269 /* Stop if last visible row. */
15270 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15271 break;
15274 return row_found;
15278 /* Find the first glyph row in the current matrix of W that is not
15279 affected by changes at the end of current_buffer since the
15280 time W's current matrix was built.
15282 Return in *DELTA the number of chars by which buffer positions in
15283 unchanged text at the end of current_buffer must be adjusted.
15285 Return in *DELTA_BYTES the corresponding number of bytes.
15287 Value is null if no such row exists, i.e. all rows are affected by
15288 changes. */
15290 static struct glyph_row *
15291 find_first_unchanged_at_end_row (struct window *w,
15292 EMACS_INT *delta, EMACS_INT *delta_bytes)
15294 struct glyph_row *row;
15295 struct glyph_row *row_found = NULL;
15297 *delta = *delta_bytes = 0;
15299 /* Display must not have been paused, otherwise the current matrix
15300 is not up to date. */
15301 eassert (!NILP (w->window_end_valid));
15303 /* A value of window_end_pos >= END_UNCHANGED means that the window
15304 end is in the range of changed text. If so, there is no
15305 unchanged row at the end of W's current matrix. */
15306 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15307 return NULL;
15309 /* Set row to the last row in W's current matrix displaying text. */
15310 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15312 /* If matrix is entirely empty, no unchanged row exists. */
15313 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15315 /* The value of row is the last glyph row in the matrix having a
15316 meaningful buffer position in it. The end position of row
15317 corresponds to window_end_pos. This allows us to translate
15318 buffer positions in the current matrix to current buffer
15319 positions for characters not in changed text. */
15320 EMACS_INT Z_old =
15321 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15322 EMACS_INT Z_BYTE_old =
15323 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15324 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15325 struct glyph_row *first_text_row
15326 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15328 *delta = Z - Z_old;
15329 *delta_bytes = Z_BYTE - Z_BYTE_old;
15331 /* Set last_unchanged_pos to the buffer position of the last
15332 character in the buffer that has not been changed. Z is the
15333 index + 1 of the last character in current_buffer, i.e. by
15334 subtracting END_UNCHANGED we get the index of the last
15335 unchanged character, and we have to add BEG to get its buffer
15336 position. */
15337 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15338 last_unchanged_pos_old = last_unchanged_pos - *delta;
15340 /* Search backward from ROW for a row displaying a line that
15341 starts at a minimum position >= last_unchanged_pos_old. */
15342 for (; row > first_text_row; --row)
15344 /* This used to abort, but it can happen.
15345 It is ok to just stop the search instead here. KFS. */
15346 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15347 break;
15349 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15350 row_found = row;
15354 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15356 return row_found;
15360 /* Make sure that glyph rows in the current matrix of window W
15361 reference the same glyph memory as corresponding rows in the
15362 frame's frame matrix. This function is called after scrolling W's
15363 current matrix on a terminal frame in try_window_id and
15364 try_window_reusing_current_matrix. */
15366 static void
15367 sync_frame_with_window_matrix_rows (struct window *w)
15369 struct frame *f = XFRAME (w->frame);
15370 struct glyph_row *window_row, *window_row_end, *frame_row;
15372 /* Preconditions: W must be a leaf window and full-width. Its frame
15373 must have a frame matrix. */
15374 xassert (NILP (w->hchild) && NILP (w->vchild));
15375 xassert (WINDOW_FULL_WIDTH_P (w));
15376 xassert (!FRAME_WINDOW_P (f));
15378 /* If W is a full-width window, glyph pointers in W's current matrix
15379 have, by definition, to be the same as glyph pointers in the
15380 corresponding frame matrix. Note that frame matrices have no
15381 marginal areas (see build_frame_matrix). */
15382 window_row = w->current_matrix->rows;
15383 window_row_end = window_row + w->current_matrix->nrows;
15384 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15385 while (window_row < window_row_end)
15387 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15388 struct glyph *end = window_row->glyphs[LAST_AREA];
15390 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15391 frame_row->glyphs[TEXT_AREA] = start;
15392 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15393 frame_row->glyphs[LAST_AREA] = end;
15395 /* Disable frame rows whose corresponding window rows have
15396 been disabled in try_window_id. */
15397 if (!window_row->enabled_p)
15398 frame_row->enabled_p = 0;
15400 ++window_row, ++frame_row;
15405 /* Find the glyph row in window W containing CHARPOS. Consider all
15406 rows between START and END (not inclusive). END null means search
15407 all rows to the end of the display area of W. Value is the row
15408 containing CHARPOS or null. */
15410 struct glyph_row *
15411 row_containing_pos (struct window *w, EMACS_INT charpos,
15412 struct glyph_row *start, struct glyph_row *end, int dy)
15414 struct glyph_row *row = start;
15415 struct glyph_row *best_row = NULL;
15416 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15417 int last_y;
15419 /* If we happen to start on a header-line, skip that. */
15420 if (row->mode_line_p)
15421 ++row;
15423 if ((end && row >= end) || !row->enabled_p)
15424 return NULL;
15426 last_y = window_text_bottom_y (w) - dy;
15428 while (1)
15430 /* Give up if we have gone too far. */
15431 if (end && row >= end)
15432 return NULL;
15433 /* This formerly returned if they were equal.
15434 I think that both quantities are of a "last plus one" type;
15435 if so, when they are equal, the row is within the screen. -- rms. */
15436 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15437 return NULL;
15439 /* If it is in this row, return this row. */
15440 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15441 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15442 /* The end position of a row equals the start
15443 position of the next row. If CHARPOS is there, we
15444 would rather display it in the next line, except
15445 when this line ends in ZV. */
15446 && !row->ends_at_zv_p
15447 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15448 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15450 struct glyph *g;
15452 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15453 || (!best_row && !row->continued_p))
15454 return row;
15455 /* In bidi-reordered rows, there could be several rows
15456 occluding point, all of them belonging to the same
15457 continued line. We need to find the row which fits
15458 CHARPOS the best. */
15459 for (g = row->glyphs[TEXT_AREA];
15460 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15461 g++)
15463 if (!STRINGP (g->object))
15465 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15467 mindif = eabs (g->charpos - charpos);
15468 best_row = row;
15469 /* Exact match always wins. */
15470 if (mindif == 0)
15471 return best_row;
15476 else if (best_row && !row->continued_p)
15477 return best_row;
15478 ++row;
15483 /* Try to redisplay window W by reusing its existing display. W's
15484 current matrix must be up to date when this function is called,
15485 i.e. window_end_valid must not be nil.
15487 Value is
15489 1 if display has been updated
15490 0 if otherwise unsuccessful
15491 -1 if redisplay with same window start is known not to succeed
15493 The following steps are performed:
15495 1. Find the last row in the current matrix of W that is not
15496 affected by changes at the start of current_buffer. If no such row
15497 is found, give up.
15499 2. Find the first row in W's current matrix that is not affected by
15500 changes at the end of current_buffer. Maybe there is no such row.
15502 3. Display lines beginning with the row + 1 found in step 1 to the
15503 row found in step 2 or, if step 2 didn't find a row, to the end of
15504 the window.
15506 4. If cursor is not known to appear on the window, give up.
15508 5. If display stopped at the row found in step 2, scroll the
15509 display and current matrix as needed.
15511 6. Maybe display some lines at the end of W, if we must. This can
15512 happen under various circumstances, like a partially visible line
15513 becoming fully visible, or because newly displayed lines are displayed
15514 in smaller font sizes.
15516 7. Update W's window end information. */
15518 static int
15519 try_window_id (struct window *w)
15521 struct frame *f = XFRAME (w->frame);
15522 struct glyph_matrix *current_matrix = w->current_matrix;
15523 struct glyph_matrix *desired_matrix = w->desired_matrix;
15524 struct glyph_row *last_unchanged_at_beg_row;
15525 struct glyph_row *first_unchanged_at_end_row;
15526 struct glyph_row *row;
15527 struct glyph_row *bottom_row;
15528 int bottom_vpos;
15529 struct it it;
15530 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15531 int dvpos, dy;
15532 struct text_pos start_pos;
15533 struct run run;
15534 int first_unchanged_at_end_vpos = 0;
15535 struct glyph_row *last_text_row, *last_text_row_at_end;
15536 struct text_pos start;
15537 EMACS_INT first_changed_charpos, last_changed_charpos;
15539 #if GLYPH_DEBUG
15540 if (inhibit_try_window_id)
15541 return 0;
15542 #endif
15544 /* This is handy for debugging. */
15545 #if 0
15546 #define GIVE_UP(X) \
15547 do { \
15548 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15549 return 0; \
15550 } while (0)
15551 #else
15552 #define GIVE_UP(X) return 0
15553 #endif
15555 SET_TEXT_POS_FROM_MARKER (start, w->start);
15557 /* Don't use this for mini-windows because these can show
15558 messages and mini-buffers, and we don't handle that here. */
15559 if (MINI_WINDOW_P (w))
15560 GIVE_UP (1);
15562 /* This flag is used to prevent redisplay optimizations. */
15563 if (windows_or_buffers_changed || cursor_type_changed)
15564 GIVE_UP (2);
15566 /* Verify that narrowing has not changed.
15567 Also verify that we were not told to prevent redisplay optimizations.
15568 It would be nice to further
15569 reduce the number of cases where this prevents try_window_id. */
15570 if (current_buffer->clip_changed
15571 || current_buffer->prevent_redisplay_optimizations_p)
15572 GIVE_UP (3);
15574 /* Window must either use window-based redisplay or be full width. */
15575 if (!FRAME_WINDOW_P (f)
15576 && (!FRAME_LINE_INS_DEL_OK (f)
15577 || !WINDOW_FULL_WIDTH_P (w)))
15578 GIVE_UP (4);
15580 /* Give up if point is known NOT to appear in W. */
15581 if (PT < CHARPOS (start))
15582 GIVE_UP (5);
15584 /* Another way to prevent redisplay optimizations. */
15585 if (XFASTINT (w->last_modified) == 0)
15586 GIVE_UP (6);
15588 /* Verify that window is not hscrolled. */
15589 if (XFASTINT (w->hscroll) != 0)
15590 GIVE_UP (7);
15592 /* Verify that display wasn't paused. */
15593 if (NILP (w->window_end_valid))
15594 GIVE_UP (8);
15596 /* Can't use this if highlighting a region because a cursor movement
15597 will do more than just set the cursor. */
15598 if (!NILP (Vtransient_mark_mode)
15599 && !NILP (current_buffer->mark_active))
15600 GIVE_UP (9);
15602 /* Likewise if highlighting trailing whitespace. */
15603 if (!NILP (Vshow_trailing_whitespace))
15604 GIVE_UP (11);
15606 /* Likewise if showing a region. */
15607 if (!NILP (w->region_showing))
15608 GIVE_UP (10);
15610 /* Can't use this if overlay arrow position and/or string have
15611 changed. */
15612 if (overlay_arrows_changed_p ())
15613 GIVE_UP (12);
15615 /* When word-wrap is on, adding a space to the first word of a
15616 wrapped line can change the wrap position, altering the line
15617 above it. It might be worthwhile to handle this more
15618 intelligently, but for now just redisplay from scratch. */
15619 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15620 GIVE_UP (21);
15622 /* Under bidi reordering, adding or deleting a character in the
15623 beginning of a paragraph, before the first strong directional
15624 character, can change the base direction of the paragraph (unless
15625 the buffer specifies a fixed paragraph direction), which will
15626 require to redisplay the whole paragraph. It might be worthwhile
15627 to find the paragraph limits and widen the range of redisplayed
15628 lines to that, but for now just give up this optimization and
15629 redisplay from scratch. */
15630 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15631 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15632 GIVE_UP (22);
15634 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15635 only if buffer has really changed. The reason is that the gap is
15636 initially at Z for freshly visited files. The code below would
15637 set end_unchanged to 0 in that case. */
15638 if (MODIFF > SAVE_MODIFF
15639 /* This seems to happen sometimes after saving a buffer. */
15640 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15642 if (GPT - BEG < BEG_UNCHANGED)
15643 BEG_UNCHANGED = GPT - BEG;
15644 if (Z - GPT < END_UNCHANGED)
15645 END_UNCHANGED = Z - GPT;
15648 /* The position of the first and last character that has been changed. */
15649 first_changed_charpos = BEG + BEG_UNCHANGED;
15650 last_changed_charpos = Z - END_UNCHANGED;
15652 /* If window starts after a line end, and the last change is in
15653 front of that newline, then changes don't affect the display.
15654 This case happens with stealth-fontification. Note that although
15655 the display is unchanged, glyph positions in the matrix have to
15656 be adjusted, of course. */
15657 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15658 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15659 && ((last_changed_charpos < CHARPOS (start)
15660 && CHARPOS (start) == BEGV)
15661 || (last_changed_charpos < CHARPOS (start) - 1
15662 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15664 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15665 struct glyph_row *r0;
15667 /* Compute how many chars/bytes have been added to or removed
15668 from the buffer. */
15669 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15670 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15671 delta = Z - Z_old;
15672 delta_bytes = Z_BYTE - Z_BYTE_old;
15674 /* Give up if PT is not in the window. Note that it already has
15675 been checked at the start of try_window_id that PT is not in
15676 front of the window start. */
15677 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15678 GIVE_UP (13);
15680 /* If window start is unchanged, we can reuse the whole matrix
15681 as is, after adjusting glyph positions. No need to compute
15682 the window end again, since its offset from Z hasn't changed. */
15683 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15684 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15685 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15686 /* PT must not be in a partially visible line. */
15687 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15688 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15690 /* Adjust positions in the glyph matrix. */
15691 if (delta || delta_bytes)
15693 struct glyph_row *r1
15694 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15695 increment_matrix_positions (w->current_matrix,
15696 MATRIX_ROW_VPOS (r0, current_matrix),
15697 MATRIX_ROW_VPOS (r1, current_matrix),
15698 delta, delta_bytes);
15701 /* Set the cursor. */
15702 row = row_containing_pos (w, PT, r0, NULL, 0);
15703 if (row)
15704 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15705 else
15706 abort ();
15707 return 1;
15711 /* Handle the case that changes are all below what is displayed in
15712 the window, and that PT is in the window. This shortcut cannot
15713 be taken if ZV is visible in the window, and text has been added
15714 there that is visible in the window. */
15715 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15716 /* ZV is not visible in the window, or there are no
15717 changes at ZV, actually. */
15718 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15719 || first_changed_charpos == last_changed_charpos))
15721 struct glyph_row *r0;
15723 /* Give up if PT is not in the window. Note that it already has
15724 been checked at the start of try_window_id that PT is not in
15725 front of the window start. */
15726 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15727 GIVE_UP (14);
15729 /* If window start is unchanged, we can reuse the whole matrix
15730 as is, without changing glyph positions since no text has
15731 been added/removed in front of the window end. */
15732 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15733 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15734 /* PT must not be in a partially visible line. */
15735 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15736 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15738 /* We have to compute the window end anew since text
15739 could have been added/removed after it. */
15740 w->window_end_pos
15741 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15742 w->window_end_bytepos
15743 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15745 /* Set the cursor. */
15746 row = row_containing_pos (w, PT, r0, NULL, 0);
15747 if (row)
15748 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15749 else
15750 abort ();
15751 return 2;
15755 /* Give up if window start is in the changed area.
15757 The condition used to read
15759 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15761 but why that was tested escapes me at the moment. */
15762 if (CHARPOS (start) >= first_changed_charpos
15763 && CHARPOS (start) <= last_changed_charpos)
15764 GIVE_UP (15);
15766 /* Check that window start agrees with the start of the first glyph
15767 row in its current matrix. Check this after we know the window
15768 start is not in changed text, otherwise positions would not be
15769 comparable. */
15770 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15771 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15772 GIVE_UP (16);
15774 /* Give up if the window ends in strings. Overlay strings
15775 at the end are difficult to handle, so don't try. */
15776 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15777 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15778 GIVE_UP (20);
15780 /* Compute the position at which we have to start displaying new
15781 lines. Some of the lines at the top of the window might be
15782 reusable because they are not displaying changed text. Find the
15783 last row in W's current matrix not affected by changes at the
15784 start of current_buffer. Value is null if changes start in the
15785 first line of window. */
15786 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15787 if (last_unchanged_at_beg_row)
15789 /* Avoid starting to display in the moddle of a character, a TAB
15790 for instance. This is easier than to set up the iterator
15791 exactly, and it's not a frequent case, so the additional
15792 effort wouldn't really pay off. */
15793 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15794 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15795 && last_unchanged_at_beg_row > w->current_matrix->rows)
15796 --last_unchanged_at_beg_row;
15798 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15799 GIVE_UP (17);
15801 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15802 GIVE_UP (18);
15803 start_pos = it.current.pos;
15805 /* Start displaying new lines in the desired matrix at the same
15806 vpos we would use in the current matrix, i.e. below
15807 last_unchanged_at_beg_row. */
15808 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15809 current_matrix);
15810 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15811 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15813 xassert (it.hpos == 0 && it.current_x == 0);
15815 else
15817 /* There are no reusable lines at the start of the window.
15818 Start displaying in the first text line. */
15819 start_display (&it, w, start);
15820 it.vpos = it.first_vpos;
15821 start_pos = it.current.pos;
15824 /* Find the first row that is not affected by changes at the end of
15825 the buffer. Value will be null if there is no unchanged row, in
15826 which case we must redisplay to the end of the window. delta
15827 will be set to the value by which buffer positions beginning with
15828 first_unchanged_at_end_row have to be adjusted due to text
15829 changes. */
15830 first_unchanged_at_end_row
15831 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15832 IF_DEBUG (debug_delta = delta);
15833 IF_DEBUG (debug_delta_bytes = delta_bytes);
15835 /* Set stop_pos to the buffer position up to which we will have to
15836 display new lines. If first_unchanged_at_end_row != NULL, this
15837 is the buffer position of the start of the line displayed in that
15838 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15839 that we don't stop at a buffer position. */
15840 stop_pos = 0;
15841 if (first_unchanged_at_end_row)
15843 xassert (last_unchanged_at_beg_row == NULL
15844 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15846 /* If this is a continuation line, move forward to the next one
15847 that isn't. Changes in lines above affect this line.
15848 Caution: this may move first_unchanged_at_end_row to a row
15849 not displaying text. */
15850 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15851 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15852 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15853 < it.last_visible_y))
15854 ++first_unchanged_at_end_row;
15856 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15857 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15858 >= it.last_visible_y))
15859 first_unchanged_at_end_row = NULL;
15860 else
15862 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15863 + delta);
15864 first_unchanged_at_end_vpos
15865 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15866 xassert (stop_pos >= Z - END_UNCHANGED);
15869 else if (last_unchanged_at_beg_row == NULL)
15870 GIVE_UP (19);
15873 #if GLYPH_DEBUG
15875 /* Either there is no unchanged row at the end, or the one we have
15876 now displays text. This is a necessary condition for the window
15877 end pos calculation at the end of this function. */
15878 xassert (first_unchanged_at_end_row == NULL
15879 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15881 debug_last_unchanged_at_beg_vpos
15882 = (last_unchanged_at_beg_row
15883 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15884 : -1);
15885 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15887 #endif /* GLYPH_DEBUG != 0 */
15890 /* Display new lines. Set last_text_row to the last new line
15891 displayed which has text on it, i.e. might end up as being the
15892 line where the window_end_vpos is. */
15893 w->cursor.vpos = -1;
15894 last_text_row = NULL;
15895 overlay_arrow_seen = 0;
15896 while (it.current_y < it.last_visible_y
15897 && !fonts_changed_p
15898 && (first_unchanged_at_end_row == NULL
15899 || IT_CHARPOS (it) < stop_pos))
15901 if (display_line (&it))
15902 last_text_row = it.glyph_row - 1;
15905 if (fonts_changed_p)
15906 return -1;
15909 /* Compute differences in buffer positions, y-positions etc. for
15910 lines reused at the bottom of the window. Compute what we can
15911 scroll. */
15912 if (first_unchanged_at_end_row
15913 /* No lines reused because we displayed everything up to the
15914 bottom of the window. */
15915 && it.current_y < it.last_visible_y)
15917 dvpos = (it.vpos
15918 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15919 current_matrix));
15920 dy = it.current_y - first_unchanged_at_end_row->y;
15921 run.current_y = first_unchanged_at_end_row->y;
15922 run.desired_y = run.current_y + dy;
15923 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15925 else
15927 delta = delta_bytes = dvpos = dy
15928 = run.current_y = run.desired_y = run.height = 0;
15929 first_unchanged_at_end_row = NULL;
15931 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15934 /* Find the cursor if not already found. We have to decide whether
15935 PT will appear on this window (it sometimes doesn't, but this is
15936 not a very frequent case.) This decision has to be made before
15937 the current matrix is altered. A value of cursor.vpos < 0 means
15938 that PT is either in one of the lines beginning at
15939 first_unchanged_at_end_row or below the window. Don't care for
15940 lines that might be displayed later at the window end; as
15941 mentioned, this is not a frequent case. */
15942 if (w->cursor.vpos < 0)
15944 /* Cursor in unchanged rows at the top? */
15945 if (PT < CHARPOS (start_pos)
15946 && last_unchanged_at_beg_row)
15948 row = row_containing_pos (w, PT,
15949 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15950 last_unchanged_at_beg_row + 1, 0);
15951 if (row)
15952 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15955 /* Start from first_unchanged_at_end_row looking for PT. */
15956 else if (first_unchanged_at_end_row)
15958 row = row_containing_pos (w, PT - delta,
15959 first_unchanged_at_end_row, NULL, 0);
15960 if (row)
15961 set_cursor_from_row (w, row, w->current_matrix, delta,
15962 delta_bytes, dy, dvpos);
15965 /* Give up if cursor was not found. */
15966 if (w->cursor.vpos < 0)
15968 clear_glyph_matrix (w->desired_matrix);
15969 return -1;
15973 /* Don't let the cursor end in the scroll margins. */
15975 int this_scroll_margin, cursor_height;
15977 this_scroll_margin = max (0, scroll_margin);
15978 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15979 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15980 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15982 if ((w->cursor.y < this_scroll_margin
15983 && CHARPOS (start) > BEGV)
15984 /* Old redisplay didn't take scroll margin into account at the bottom,
15985 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15986 || (w->cursor.y + (make_cursor_line_fully_visible_p
15987 ? cursor_height + this_scroll_margin
15988 : 1)) > it.last_visible_y)
15990 w->cursor.vpos = -1;
15991 clear_glyph_matrix (w->desired_matrix);
15992 return -1;
15996 /* Scroll the display. Do it before changing the current matrix so
15997 that xterm.c doesn't get confused about where the cursor glyph is
15998 found. */
15999 if (dy && run.height)
16001 update_begin (f);
16003 if (FRAME_WINDOW_P (f))
16005 FRAME_RIF (f)->update_window_begin_hook (w);
16006 FRAME_RIF (f)->clear_window_mouse_face (w);
16007 FRAME_RIF (f)->scroll_run_hook (w, &run);
16008 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16010 else
16012 /* Terminal frame. In this case, dvpos gives the number of
16013 lines to scroll by; dvpos < 0 means scroll up. */
16014 int first_unchanged_at_end_vpos
16015 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16016 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
16017 int end = (WINDOW_TOP_EDGE_LINE (w)
16018 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16019 + window_internal_height (w));
16021 #if defined (HAVE_GPM) || defined (MSDOS)
16022 x_clear_window_mouse_face (w);
16023 #endif
16024 /* Perform the operation on the screen. */
16025 if (dvpos > 0)
16027 /* Scroll last_unchanged_at_beg_row to the end of the
16028 window down dvpos lines. */
16029 set_terminal_window (f, end);
16031 /* On dumb terminals delete dvpos lines at the end
16032 before inserting dvpos empty lines. */
16033 if (!FRAME_SCROLL_REGION_OK (f))
16034 ins_del_lines (f, end - dvpos, -dvpos);
16036 /* Insert dvpos empty lines in front of
16037 last_unchanged_at_beg_row. */
16038 ins_del_lines (f, from, dvpos);
16040 else if (dvpos < 0)
16042 /* Scroll up last_unchanged_at_beg_vpos to the end of
16043 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16044 set_terminal_window (f, end);
16046 /* Delete dvpos lines in front of
16047 last_unchanged_at_beg_vpos. ins_del_lines will set
16048 the cursor to the given vpos and emit |dvpos| delete
16049 line sequences. */
16050 ins_del_lines (f, from + dvpos, dvpos);
16052 /* On a dumb terminal insert dvpos empty lines at the
16053 end. */
16054 if (!FRAME_SCROLL_REGION_OK (f))
16055 ins_del_lines (f, end + dvpos, -dvpos);
16058 set_terminal_window (f, 0);
16061 update_end (f);
16064 /* Shift reused rows of the current matrix to the right position.
16065 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16066 text. */
16067 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16068 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16069 if (dvpos < 0)
16071 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16072 bottom_vpos, dvpos);
16073 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16074 bottom_vpos, 0);
16076 else if (dvpos > 0)
16078 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16079 bottom_vpos, dvpos);
16080 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16081 first_unchanged_at_end_vpos + dvpos, 0);
16084 /* For frame-based redisplay, make sure that current frame and window
16085 matrix are in sync with respect to glyph memory. */
16086 if (!FRAME_WINDOW_P (f))
16087 sync_frame_with_window_matrix_rows (w);
16089 /* Adjust buffer positions in reused rows. */
16090 if (delta || delta_bytes)
16091 increment_matrix_positions (current_matrix,
16092 first_unchanged_at_end_vpos + dvpos,
16093 bottom_vpos, delta, delta_bytes);
16095 /* Adjust Y positions. */
16096 if (dy)
16097 shift_glyph_matrix (w, current_matrix,
16098 first_unchanged_at_end_vpos + dvpos,
16099 bottom_vpos, dy);
16101 if (first_unchanged_at_end_row)
16103 first_unchanged_at_end_row += dvpos;
16104 if (first_unchanged_at_end_row->y >= it.last_visible_y
16105 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16106 first_unchanged_at_end_row = NULL;
16109 /* If scrolling up, there may be some lines to display at the end of
16110 the window. */
16111 last_text_row_at_end = NULL;
16112 if (dy < 0)
16114 /* Scrolling up can leave for example a partially visible line
16115 at the end of the window to be redisplayed. */
16116 /* Set last_row to the glyph row in the current matrix where the
16117 window end line is found. It has been moved up or down in
16118 the matrix by dvpos. */
16119 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16120 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16122 /* If last_row is the window end line, it should display text. */
16123 xassert (last_row->displays_text_p);
16125 /* If window end line was partially visible before, begin
16126 displaying at that line. Otherwise begin displaying with the
16127 line following it. */
16128 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16130 init_to_row_start (&it, w, last_row);
16131 it.vpos = last_vpos;
16132 it.current_y = last_row->y;
16134 else
16136 init_to_row_end (&it, w, last_row);
16137 it.vpos = 1 + last_vpos;
16138 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16139 ++last_row;
16142 /* We may start in a continuation line. If so, we have to
16143 get the right continuation_lines_width and current_x. */
16144 it.continuation_lines_width = last_row->continuation_lines_width;
16145 it.hpos = it.current_x = 0;
16147 /* Display the rest of the lines at the window end. */
16148 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16149 while (it.current_y < it.last_visible_y
16150 && !fonts_changed_p)
16152 /* Is it always sure that the display agrees with lines in
16153 the current matrix? I don't think so, so we mark rows
16154 displayed invalid in the current matrix by setting their
16155 enabled_p flag to zero. */
16156 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16157 if (display_line (&it))
16158 last_text_row_at_end = it.glyph_row - 1;
16162 /* Update window_end_pos and window_end_vpos. */
16163 if (first_unchanged_at_end_row
16164 && !last_text_row_at_end)
16166 /* Window end line if one of the preserved rows from the current
16167 matrix. Set row to the last row displaying text in current
16168 matrix starting at first_unchanged_at_end_row, after
16169 scrolling. */
16170 xassert (first_unchanged_at_end_row->displays_text_p);
16171 row = find_last_row_displaying_text (w->current_matrix, &it,
16172 first_unchanged_at_end_row);
16173 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16175 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16176 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16177 w->window_end_vpos
16178 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16179 xassert (w->window_end_bytepos >= 0);
16180 IF_DEBUG (debug_method_add (w, "A"));
16182 else if (last_text_row_at_end)
16184 w->window_end_pos
16185 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16186 w->window_end_bytepos
16187 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16188 w->window_end_vpos
16189 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16190 xassert (w->window_end_bytepos >= 0);
16191 IF_DEBUG (debug_method_add (w, "B"));
16193 else if (last_text_row)
16195 /* We have displayed either to the end of the window or at the
16196 end of the window, i.e. the last row with text is to be found
16197 in the desired matrix. */
16198 w->window_end_pos
16199 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16200 w->window_end_bytepos
16201 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16202 w->window_end_vpos
16203 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16204 xassert (w->window_end_bytepos >= 0);
16206 else if (first_unchanged_at_end_row == NULL
16207 && last_text_row == NULL
16208 && last_text_row_at_end == NULL)
16210 /* Displayed to end of window, but no line containing text was
16211 displayed. Lines were deleted at the end of the window. */
16212 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16213 int vpos = XFASTINT (w->window_end_vpos);
16214 struct glyph_row *current_row = current_matrix->rows + vpos;
16215 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16217 for (row = NULL;
16218 row == NULL && vpos >= first_vpos;
16219 --vpos, --current_row, --desired_row)
16221 if (desired_row->enabled_p)
16223 if (desired_row->displays_text_p)
16224 row = desired_row;
16226 else if (current_row->displays_text_p)
16227 row = current_row;
16230 xassert (row != NULL);
16231 w->window_end_vpos = make_number (vpos + 1);
16232 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16233 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16234 xassert (w->window_end_bytepos >= 0);
16235 IF_DEBUG (debug_method_add (w, "C"));
16237 else
16238 abort ();
16240 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16241 debug_end_vpos = XFASTINT (w->window_end_vpos));
16243 /* Record that display has not been completed. */
16244 w->window_end_valid = Qnil;
16245 w->desired_matrix->no_scrolling_p = 1;
16246 return 3;
16248 #undef GIVE_UP
16253 /***********************************************************************
16254 More debugging support
16255 ***********************************************************************/
16257 #if GLYPH_DEBUG
16259 void dump_glyph_row (struct glyph_row *, int, int);
16260 void dump_glyph_matrix (struct glyph_matrix *, int);
16261 void dump_glyph (struct glyph_row *, struct glyph *, int);
16264 /* Dump the contents of glyph matrix MATRIX on stderr.
16266 GLYPHS 0 means don't show glyph contents.
16267 GLYPHS 1 means show glyphs in short form
16268 GLYPHS > 1 means show glyphs in long form. */
16270 void
16271 dump_glyph_matrix (matrix, glyphs)
16272 struct glyph_matrix *matrix;
16273 int glyphs;
16275 int i;
16276 for (i = 0; i < matrix->nrows; ++i)
16277 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16281 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16282 the glyph row and area where the glyph comes from. */
16284 void
16285 dump_glyph (row, glyph, area)
16286 struct glyph_row *row;
16287 struct glyph *glyph;
16288 int area;
16290 if (glyph->type == CHAR_GLYPH)
16292 fprintf (stderr,
16293 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16294 glyph - row->glyphs[TEXT_AREA],
16295 'C',
16296 glyph->charpos,
16297 (BUFFERP (glyph->object)
16298 ? 'B'
16299 : (STRINGP (glyph->object)
16300 ? 'S'
16301 : '-')),
16302 glyph->pixel_width,
16303 glyph->u.ch,
16304 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16305 ? glyph->u.ch
16306 : '.'),
16307 glyph->face_id,
16308 glyph->left_box_line_p,
16309 glyph->right_box_line_p);
16311 else if (glyph->type == STRETCH_GLYPH)
16313 fprintf (stderr,
16314 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16315 glyph - row->glyphs[TEXT_AREA],
16316 'S',
16317 glyph->charpos,
16318 (BUFFERP (glyph->object)
16319 ? 'B'
16320 : (STRINGP (glyph->object)
16321 ? 'S'
16322 : '-')),
16323 glyph->pixel_width,
16325 '.',
16326 glyph->face_id,
16327 glyph->left_box_line_p,
16328 glyph->right_box_line_p);
16330 else if (glyph->type == IMAGE_GLYPH)
16332 fprintf (stderr,
16333 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16334 glyph - row->glyphs[TEXT_AREA],
16335 'I',
16336 glyph->charpos,
16337 (BUFFERP (glyph->object)
16338 ? 'B'
16339 : (STRINGP (glyph->object)
16340 ? 'S'
16341 : '-')),
16342 glyph->pixel_width,
16343 glyph->u.img_id,
16344 '.',
16345 glyph->face_id,
16346 glyph->left_box_line_p,
16347 glyph->right_box_line_p);
16349 else if (glyph->type == COMPOSITE_GLYPH)
16351 fprintf (stderr,
16352 " %5d %4c %6d %c %3d 0x%05x",
16353 glyph - row->glyphs[TEXT_AREA],
16354 '+',
16355 glyph->charpos,
16356 (BUFFERP (glyph->object)
16357 ? 'B'
16358 : (STRINGP (glyph->object)
16359 ? 'S'
16360 : '-')),
16361 glyph->pixel_width,
16362 glyph->u.cmp.id);
16363 if (glyph->u.cmp.automatic)
16364 fprintf (stderr,
16365 "[%d-%d]",
16366 glyph->slice.cmp.from, glyph->slice.cmp.to);
16367 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16368 glyph->face_id,
16369 glyph->left_box_line_p,
16370 glyph->right_box_line_p);
16375 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16376 GLYPHS 0 means don't show glyph contents.
16377 GLYPHS 1 means show glyphs in short form
16378 GLYPHS > 1 means show glyphs in long form. */
16380 void
16381 dump_glyph_row (row, vpos, glyphs)
16382 struct glyph_row *row;
16383 int vpos, glyphs;
16385 if (glyphs != 1)
16387 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16388 fprintf (stderr, "======================================================================\n");
16390 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16391 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16392 vpos,
16393 MATRIX_ROW_START_CHARPOS (row),
16394 MATRIX_ROW_END_CHARPOS (row),
16395 row->used[TEXT_AREA],
16396 row->contains_overlapping_glyphs_p,
16397 row->enabled_p,
16398 row->truncated_on_left_p,
16399 row->truncated_on_right_p,
16400 row->continued_p,
16401 MATRIX_ROW_CONTINUATION_LINE_P (row),
16402 row->displays_text_p,
16403 row->ends_at_zv_p,
16404 row->fill_line_p,
16405 row->ends_in_middle_of_char_p,
16406 row->starts_in_middle_of_char_p,
16407 row->mouse_face_p,
16408 row->x,
16409 row->y,
16410 row->pixel_width,
16411 row->height,
16412 row->visible_height,
16413 row->ascent,
16414 row->phys_ascent);
16415 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16416 row->end.overlay_string_index,
16417 row->continuation_lines_width);
16418 fprintf (stderr, "%9d %5d\n",
16419 CHARPOS (row->start.string_pos),
16420 CHARPOS (row->end.string_pos));
16421 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16422 row->end.dpvec_index);
16425 if (glyphs > 1)
16427 int area;
16429 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16431 struct glyph *glyph = row->glyphs[area];
16432 struct glyph *glyph_end = glyph + row->used[area];
16434 /* Glyph for a line end in text. */
16435 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16436 ++glyph_end;
16438 if (glyph < glyph_end)
16439 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16441 for (; glyph < glyph_end; ++glyph)
16442 dump_glyph (row, glyph, area);
16445 else if (glyphs == 1)
16447 int area;
16449 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16451 char *s = (char *) alloca (row->used[area] + 1);
16452 int i;
16454 for (i = 0; i < row->used[area]; ++i)
16456 struct glyph *glyph = row->glyphs[area] + i;
16457 if (glyph->type == CHAR_GLYPH
16458 && glyph->u.ch < 0x80
16459 && glyph->u.ch >= ' ')
16460 s[i] = glyph->u.ch;
16461 else
16462 s[i] = '.';
16465 s[i] = '\0';
16466 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16472 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16473 Sdump_glyph_matrix, 0, 1, "p",
16474 doc: /* Dump the current matrix of the selected window to stderr.
16475 Shows contents of glyph row structures. With non-nil
16476 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16477 glyphs in short form, otherwise show glyphs in long form. */)
16478 (Lisp_Object glyphs)
16480 struct window *w = XWINDOW (selected_window);
16481 struct buffer *buffer = XBUFFER (w->buffer);
16483 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16484 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16485 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16486 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16487 fprintf (stderr, "=============================================\n");
16488 dump_glyph_matrix (w->current_matrix,
16489 NILP (glyphs) ? 0 : XINT (glyphs));
16490 return Qnil;
16494 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16495 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16496 (void)
16498 struct frame *f = XFRAME (selected_frame);
16499 dump_glyph_matrix (f->current_matrix, 1);
16500 return Qnil;
16504 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16505 doc: /* Dump glyph row ROW to stderr.
16506 GLYPH 0 means don't dump glyphs.
16507 GLYPH 1 means dump glyphs in short form.
16508 GLYPH > 1 or omitted means dump glyphs in long form. */)
16509 (Lisp_Object row, Lisp_Object glyphs)
16511 struct glyph_matrix *matrix;
16512 int vpos;
16514 CHECK_NUMBER (row);
16515 matrix = XWINDOW (selected_window)->current_matrix;
16516 vpos = XINT (row);
16517 if (vpos >= 0 && vpos < matrix->nrows)
16518 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16519 vpos,
16520 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16521 return Qnil;
16525 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16526 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16527 GLYPH 0 means don't dump glyphs.
16528 GLYPH 1 means dump glyphs in short form.
16529 GLYPH > 1 or omitted means dump glyphs in long form. */)
16530 (Lisp_Object row, Lisp_Object glyphs)
16532 struct frame *sf = SELECTED_FRAME ();
16533 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16534 int vpos;
16536 CHECK_NUMBER (row);
16537 vpos = XINT (row);
16538 if (vpos >= 0 && vpos < m->nrows)
16539 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16540 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16541 return Qnil;
16545 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16546 doc: /* Toggle tracing of redisplay.
16547 With ARG, turn tracing on if and only if ARG is positive. */)
16548 (Lisp_Object arg)
16550 if (NILP (arg))
16551 trace_redisplay_p = !trace_redisplay_p;
16552 else
16554 arg = Fprefix_numeric_value (arg);
16555 trace_redisplay_p = XINT (arg) > 0;
16558 return Qnil;
16562 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16563 doc: /* Like `format', but print result to stderr.
16564 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16565 (int nargs, Lisp_Object *args)
16567 Lisp_Object s = Fformat (nargs, args);
16568 fprintf (stderr, "%s", SDATA (s));
16569 return Qnil;
16572 #endif /* GLYPH_DEBUG */
16576 /***********************************************************************
16577 Building Desired Matrix Rows
16578 ***********************************************************************/
16580 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16581 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16583 static struct glyph_row *
16584 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16586 struct frame *f = XFRAME (WINDOW_FRAME (w));
16587 struct buffer *buffer = XBUFFER (w->buffer);
16588 struct buffer *old = current_buffer;
16589 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16590 int arrow_len = SCHARS (overlay_arrow_string);
16591 const unsigned char *arrow_end = arrow_string + arrow_len;
16592 const unsigned char *p;
16593 struct it it;
16594 int multibyte_p;
16595 int n_glyphs_before;
16597 set_buffer_temp (buffer);
16598 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16599 it.glyph_row->used[TEXT_AREA] = 0;
16600 SET_TEXT_POS (it.position, 0, 0);
16602 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16603 p = arrow_string;
16604 while (p < arrow_end)
16606 Lisp_Object face, ilisp;
16608 /* Get the next character. */
16609 if (multibyte_p)
16610 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16611 else
16613 it.c = it.char_to_display = *p, it.len = 1;
16614 if (! ASCII_CHAR_P (it.c))
16615 it.char_to_display = BYTE8_TO_CHAR (it.c);
16617 p += it.len;
16619 /* Get its face. */
16620 ilisp = make_number (p - arrow_string);
16621 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16622 it.face_id = compute_char_face (f, it.char_to_display, face);
16624 /* Compute its width, get its glyphs. */
16625 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16626 SET_TEXT_POS (it.position, -1, -1);
16627 PRODUCE_GLYPHS (&it);
16629 /* If this character doesn't fit any more in the line, we have
16630 to remove some glyphs. */
16631 if (it.current_x > it.last_visible_x)
16633 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16634 break;
16638 set_buffer_temp (old);
16639 return it.glyph_row;
16643 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16644 glyphs are only inserted for terminal frames since we can't really
16645 win with truncation glyphs when partially visible glyphs are
16646 involved. Which glyphs to insert is determined by
16647 produce_special_glyphs. */
16649 static void
16650 insert_left_trunc_glyphs (struct it *it)
16652 struct it truncate_it;
16653 struct glyph *from, *end, *to, *toend;
16655 xassert (!FRAME_WINDOW_P (it->f));
16657 /* Get the truncation glyphs. */
16658 truncate_it = *it;
16659 truncate_it.current_x = 0;
16660 truncate_it.face_id = DEFAULT_FACE_ID;
16661 truncate_it.glyph_row = &scratch_glyph_row;
16662 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16663 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16664 truncate_it.object = make_number (0);
16665 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16667 /* Overwrite glyphs from IT with truncation glyphs. */
16668 if (!it->glyph_row->reversed_p)
16670 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16671 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16672 to = it->glyph_row->glyphs[TEXT_AREA];
16673 toend = to + it->glyph_row->used[TEXT_AREA];
16675 while (from < end)
16676 *to++ = *from++;
16678 /* There may be padding glyphs left over. Overwrite them too. */
16679 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16681 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16682 while (from < end)
16683 *to++ = *from++;
16686 if (to > toend)
16687 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16689 else
16691 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16692 that back to front. */
16693 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16694 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16695 toend = it->glyph_row->glyphs[TEXT_AREA];
16696 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16698 while (from >= end && to >= toend)
16699 *to-- = *from--;
16700 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16702 from =
16703 truncate_it.glyph_row->glyphs[TEXT_AREA]
16704 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16705 while (from >= end && to >= toend)
16706 *to-- = *from--;
16708 if (from >= end)
16710 /* Need to free some room before prepending additional
16711 glyphs. */
16712 int move_by = from - end + 1;
16713 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16714 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16716 for ( ; g >= g0; g--)
16717 g[move_by] = *g;
16718 while (from >= end)
16719 *to-- = *from--;
16720 it->glyph_row->used[TEXT_AREA] += move_by;
16726 /* Compute the pixel height and width of IT->glyph_row.
16728 Most of the time, ascent and height of a display line will be equal
16729 to the max_ascent and max_height values of the display iterator
16730 structure. This is not the case if
16732 1. We hit ZV without displaying anything. In this case, max_ascent
16733 and max_height will be zero.
16735 2. We have some glyphs that don't contribute to the line height.
16736 (The glyph row flag contributes_to_line_height_p is for future
16737 pixmap extensions).
16739 The first case is easily covered by using default values because in
16740 these cases, the line height does not really matter, except that it
16741 must not be zero. */
16743 static void
16744 compute_line_metrics (struct it *it)
16746 struct glyph_row *row = it->glyph_row;
16747 int area, i;
16749 if (FRAME_WINDOW_P (it->f))
16751 int i, min_y, max_y;
16753 /* The line may consist of one space only, that was added to
16754 place the cursor on it. If so, the row's height hasn't been
16755 computed yet. */
16756 if (row->height == 0)
16758 if (it->max_ascent + it->max_descent == 0)
16759 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16760 row->ascent = it->max_ascent;
16761 row->height = it->max_ascent + it->max_descent;
16762 row->phys_ascent = it->max_phys_ascent;
16763 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16764 row->extra_line_spacing = it->max_extra_line_spacing;
16767 /* Compute the width of this line. */
16768 row->pixel_width = row->x;
16769 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16770 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16772 xassert (row->pixel_width >= 0);
16773 xassert (row->ascent >= 0 && row->height > 0);
16775 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16776 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16778 /* If first line's physical ascent is larger than its logical
16779 ascent, use the physical ascent, and make the row taller.
16780 This makes accented characters fully visible. */
16781 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16782 && row->phys_ascent > row->ascent)
16784 row->height += row->phys_ascent - row->ascent;
16785 row->ascent = row->phys_ascent;
16788 /* Compute how much of the line is visible. */
16789 row->visible_height = row->height;
16791 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16792 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16794 if (row->y < min_y)
16795 row->visible_height -= min_y - row->y;
16796 if (row->y + row->height > max_y)
16797 row->visible_height -= row->y + row->height - max_y;
16799 else
16801 row->pixel_width = row->used[TEXT_AREA];
16802 if (row->continued_p)
16803 row->pixel_width -= it->continuation_pixel_width;
16804 else if (row->truncated_on_right_p)
16805 row->pixel_width -= it->truncation_pixel_width;
16806 row->ascent = row->phys_ascent = 0;
16807 row->height = row->phys_height = row->visible_height = 1;
16808 row->extra_line_spacing = 0;
16811 /* Compute a hash code for this row. */
16812 row->hash = 0;
16813 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16814 for (i = 0; i < row->used[area]; ++i)
16815 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16816 + row->glyphs[area][i].u.val
16817 + row->glyphs[area][i].face_id
16818 + row->glyphs[area][i].padding_p
16819 + (row->glyphs[area][i].type << 2));
16821 it->max_ascent = it->max_descent = 0;
16822 it->max_phys_ascent = it->max_phys_descent = 0;
16826 /* Append one space to the glyph row of iterator IT if doing a
16827 window-based redisplay. The space has the same face as
16828 IT->face_id. Value is non-zero if a space was added.
16830 This function is called to make sure that there is always one glyph
16831 at the end of a glyph row that the cursor can be set on under
16832 window-systems. (If there weren't such a glyph we would not know
16833 how wide and tall a box cursor should be displayed).
16835 At the same time this space let's a nicely handle clearing to the
16836 end of the line if the row ends in italic text. */
16838 static int
16839 append_space_for_newline (struct it *it, int default_face_p)
16841 if (FRAME_WINDOW_P (it->f))
16843 int n = it->glyph_row->used[TEXT_AREA];
16845 if (it->glyph_row->glyphs[TEXT_AREA] + n
16846 < it->glyph_row->glyphs[1 + TEXT_AREA])
16848 /* Save some values that must not be changed.
16849 Must save IT->c and IT->len because otherwise
16850 ITERATOR_AT_END_P wouldn't work anymore after
16851 append_space_for_newline has been called. */
16852 enum display_element_type saved_what = it->what;
16853 int saved_c = it->c, saved_len = it->len;
16854 int saved_char_to_display = it->char_to_display;
16855 int saved_x = it->current_x;
16856 int saved_face_id = it->face_id;
16857 struct text_pos saved_pos;
16858 Lisp_Object saved_object;
16859 struct face *face;
16861 saved_object = it->object;
16862 saved_pos = it->position;
16864 it->what = IT_CHARACTER;
16865 memset (&it->position, 0, sizeof it->position);
16866 it->object = make_number (0);
16867 it->c = it->char_to_display = ' ';
16868 it->len = 1;
16870 if (default_face_p)
16871 it->face_id = DEFAULT_FACE_ID;
16872 else if (it->face_before_selective_p)
16873 it->face_id = it->saved_face_id;
16874 face = FACE_FROM_ID (it->f, it->face_id);
16875 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16877 PRODUCE_GLYPHS (it);
16879 it->override_ascent = -1;
16880 it->constrain_row_ascent_descent_p = 0;
16881 it->current_x = saved_x;
16882 it->object = saved_object;
16883 it->position = saved_pos;
16884 it->what = saved_what;
16885 it->face_id = saved_face_id;
16886 it->len = saved_len;
16887 it->c = saved_c;
16888 it->char_to_display = saved_char_to_display;
16889 return 1;
16893 return 0;
16897 /* Extend the face of the last glyph in the text area of IT->glyph_row
16898 to the end of the display line. Called from display_line. If the
16899 glyph row is empty, add a space glyph to it so that we know the
16900 face to draw. Set the glyph row flag fill_line_p. If the glyph
16901 row is R2L, prepend a stretch glyph to cover the empty space to the
16902 left of the leftmost glyph. */
16904 static void
16905 extend_face_to_end_of_line (struct it *it)
16907 struct face *face;
16908 struct frame *f = it->f;
16910 /* If line is already filled, do nothing. Non window-system frames
16911 get a grace of one more ``pixel'' because their characters are
16912 1-``pixel'' wide, so they hit the equality too early. This grace
16913 is needed only for R2L rows that are not continued, to produce
16914 one extra blank where we could display the cursor. */
16915 if (it->current_x >= it->last_visible_x
16916 + (!FRAME_WINDOW_P (f)
16917 && it->glyph_row->reversed_p
16918 && !it->glyph_row->continued_p))
16919 return;
16921 /* Face extension extends the background and box of IT->face_id
16922 to the end of the line. If the background equals the background
16923 of the frame, we don't have to do anything. */
16924 if (it->face_before_selective_p)
16925 face = FACE_FROM_ID (f, it->saved_face_id);
16926 else
16927 face = FACE_FROM_ID (f, it->face_id);
16929 if (FRAME_WINDOW_P (f)
16930 && it->glyph_row->displays_text_p
16931 && face->box == FACE_NO_BOX
16932 && face->background == FRAME_BACKGROUND_PIXEL (f)
16933 && !face->stipple
16934 && !it->glyph_row->reversed_p)
16935 return;
16937 /* Set the glyph row flag indicating that the face of the last glyph
16938 in the text area has to be drawn to the end of the text area. */
16939 it->glyph_row->fill_line_p = 1;
16941 /* If current character of IT is not ASCII, make sure we have the
16942 ASCII face. This will be automatically undone the next time
16943 get_next_display_element returns a multibyte character. Note
16944 that the character will always be single byte in unibyte
16945 text. */
16946 if (!ASCII_CHAR_P (it->c))
16948 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16951 if (FRAME_WINDOW_P (f))
16953 /* If the row is empty, add a space with the current face of IT,
16954 so that we know which face to draw. */
16955 if (it->glyph_row->used[TEXT_AREA] == 0)
16957 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16958 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16959 it->glyph_row->used[TEXT_AREA] = 1;
16961 #ifdef HAVE_WINDOW_SYSTEM
16962 if (it->glyph_row->reversed_p)
16964 /* Prepend a stretch glyph to the row, such that the
16965 rightmost glyph will be drawn flushed all the way to the
16966 right margin of the window. The stretch glyph that will
16967 occupy the empty space, if any, to the left of the
16968 glyphs. */
16969 struct font *font = face->font ? face->font : FRAME_FONT (f);
16970 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16971 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16972 struct glyph *g;
16973 int row_width, stretch_ascent, stretch_width;
16974 struct text_pos saved_pos;
16975 int saved_face_id, saved_avoid_cursor;
16977 for (row_width = 0, g = row_start; g < row_end; g++)
16978 row_width += g->pixel_width;
16979 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16980 if (stretch_width > 0)
16982 stretch_ascent =
16983 (((it->ascent + it->descent)
16984 * FONT_BASE (font)) / FONT_HEIGHT (font));
16985 saved_pos = it->position;
16986 memset (&it->position, 0, sizeof it->position);
16987 saved_avoid_cursor = it->avoid_cursor_p;
16988 it->avoid_cursor_p = 1;
16989 saved_face_id = it->face_id;
16990 /* The last row's stretch glyph should get the default
16991 face, to avoid painting the rest of the window with
16992 the region face, if the region ends at ZV. */
16993 if (it->glyph_row->ends_at_zv_p)
16994 it->face_id = DEFAULT_FACE_ID;
16995 else
16996 it->face_id = face->id;
16997 append_stretch_glyph (it, make_number (0), stretch_width,
16998 it->ascent + it->descent, stretch_ascent);
16999 it->position = saved_pos;
17000 it->avoid_cursor_p = saved_avoid_cursor;
17001 it->face_id = saved_face_id;
17004 #endif /* HAVE_WINDOW_SYSTEM */
17006 else
17008 /* Save some values that must not be changed. */
17009 int saved_x = it->current_x;
17010 struct text_pos saved_pos;
17011 Lisp_Object saved_object;
17012 enum display_element_type saved_what = it->what;
17013 int saved_face_id = it->face_id;
17015 saved_object = it->object;
17016 saved_pos = it->position;
17018 it->what = IT_CHARACTER;
17019 memset (&it->position, 0, sizeof it->position);
17020 it->object = make_number (0);
17021 it->c = it->char_to_display = ' ';
17022 it->len = 1;
17023 /* The last row's blank glyphs should get the default face, to
17024 avoid painting the rest of the window with the region face,
17025 if the region ends at ZV. */
17026 if (it->glyph_row->ends_at_zv_p)
17027 it->face_id = DEFAULT_FACE_ID;
17028 else
17029 it->face_id = face->id;
17031 PRODUCE_GLYPHS (it);
17033 while (it->current_x <= it->last_visible_x)
17034 PRODUCE_GLYPHS (it);
17036 /* Don't count these blanks really. It would let us insert a left
17037 truncation glyph below and make us set the cursor on them, maybe. */
17038 it->current_x = saved_x;
17039 it->object = saved_object;
17040 it->position = saved_pos;
17041 it->what = saved_what;
17042 it->face_id = saved_face_id;
17047 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17048 trailing whitespace. */
17050 static int
17051 trailing_whitespace_p (EMACS_INT charpos)
17053 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17054 int c = 0;
17056 while (bytepos < ZV_BYTE
17057 && (c = FETCH_CHAR (bytepos),
17058 c == ' ' || c == '\t'))
17059 ++bytepos;
17061 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17063 if (bytepos != PT_BYTE)
17064 return 1;
17066 return 0;
17070 /* Highlight trailing whitespace, if any, in ROW. */
17072 void
17073 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17075 int used = row->used[TEXT_AREA];
17077 if (used)
17079 struct glyph *start = row->glyphs[TEXT_AREA];
17080 struct glyph *glyph = start + used - 1;
17082 if (row->reversed_p)
17084 /* Right-to-left rows need to be processed in the opposite
17085 direction, so swap the edge pointers. */
17086 glyph = start;
17087 start = row->glyphs[TEXT_AREA] + used - 1;
17090 /* Skip over glyphs inserted to display the cursor at the
17091 end of a line, for extending the face of the last glyph
17092 to the end of the line on terminals, and for truncation
17093 and continuation glyphs. */
17094 if (!row->reversed_p)
17096 while (glyph >= start
17097 && glyph->type == CHAR_GLYPH
17098 && INTEGERP (glyph->object))
17099 --glyph;
17101 else
17103 while (glyph <= start
17104 && glyph->type == CHAR_GLYPH
17105 && INTEGERP (glyph->object))
17106 ++glyph;
17109 /* If last glyph is a space or stretch, and it's trailing
17110 whitespace, set the face of all trailing whitespace glyphs in
17111 IT->glyph_row to `trailing-whitespace'. */
17112 if ((row->reversed_p ? glyph <= start : glyph >= start)
17113 && BUFFERP (glyph->object)
17114 && (glyph->type == STRETCH_GLYPH
17115 || (glyph->type == CHAR_GLYPH
17116 && glyph->u.ch == ' '))
17117 && trailing_whitespace_p (glyph->charpos))
17119 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17120 if (face_id < 0)
17121 return;
17123 if (!row->reversed_p)
17125 while (glyph >= start
17126 && BUFFERP (glyph->object)
17127 && (glyph->type == STRETCH_GLYPH
17128 || (glyph->type == CHAR_GLYPH
17129 && glyph->u.ch == ' ')))
17130 (glyph--)->face_id = face_id;
17132 else
17134 while (glyph <= start
17135 && BUFFERP (glyph->object)
17136 && (glyph->type == STRETCH_GLYPH
17137 || (glyph->type == CHAR_GLYPH
17138 && glyph->u.ch == ' ')))
17139 (glyph++)->face_id = face_id;
17146 /* Value is non-zero if glyph row ROW in window W should be
17147 used to hold the cursor. */
17149 static int
17150 cursor_row_p (struct window *w, struct glyph_row *row)
17152 int cursor_row_p = 1;
17154 if (PT == CHARPOS (row->end.pos))
17156 /* Suppose the row ends on a string.
17157 Unless the row is continued, that means it ends on a newline
17158 in the string. If it's anything other than a display string
17159 (e.g. a before-string from an overlay), we don't want the
17160 cursor there. (This heuristic seems to give the optimal
17161 behavior for the various types of multi-line strings.) */
17162 if (CHARPOS (row->end.string_pos) >= 0)
17164 if (row->continued_p)
17165 cursor_row_p = 1;
17166 else
17168 /* Check for `display' property. */
17169 struct glyph *beg = row->glyphs[TEXT_AREA];
17170 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17171 struct glyph *glyph;
17173 cursor_row_p = 0;
17174 for (glyph = end; glyph >= beg; --glyph)
17175 if (STRINGP (glyph->object))
17177 Lisp_Object prop
17178 = Fget_char_property (make_number (PT),
17179 Qdisplay, Qnil);
17180 cursor_row_p =
17181 (!NILP (prop)
17182 && display_prop_string_p (prop, glyph->object));
17183 break;
17187 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17189 /* If the row ends in middle of a real character,
17190 and the line is continued, we want the cursor here.
17191 That's because CHARPOS (ROW->end.pos) would equal
17192 PT if PT is before the character. */
17193 if (!row->ends_in_ellipsis_p)
17194 cursor_row_p = row->continued_p;
17195 else
17196 /* If the row ends in an ellipsis, then
17197 CHARPOS (ROW->end.pos) will equal point after the
17198 invisible text. We want that position to be displayed
17199 after the ellipsis. */
17200 cursor_row_p = 0;
17202 /* If the row ends at ZV, display the cursor at the end of that
17203 row instead of at the start of the row below. */
17204 else if (row->ends_at_zv_p)
17205 cursor_row_p = 1;
17206 else
17207 cursor_row_p = 0;
17210 return cursor_row_p;
17215 /* Push the display property PROP so that it will be rendered at the
17216 current position in IT. Return 1 if PROP was successfully pushed,
17217 0 otherwise. */
17219 static int
17220 push_display_prop (struct it *it, Lisp_Object prop)
17222 push_it (it);
17224 if (STRINGP (prop))
17226 if (SCHARS (prop) == 0)
17228 pop_it (it);
17229 return 0;
17232 it->string = prop;
17233 it->multibyte_p = STRING_MULTIBYTE (it->string);
17234 it->current.overlay_string_index = -1;
17235 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17236 it->end_charpos = it->string_nchars = SCHARS (it->string);
17237 it->method = GET_FROM_STRING;
17238 it->stop_charpos = 0;
17240 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17242 it->method = GET_FROM_STRETCH;
17243 it->object = prop;
17245 #ifdef HAVE_WINDOW_SYSTEM
17246 else if (IMAGEP (prop))
17248 it->what = IT_IMAGE;
17249 it->image_id = lookup_image (it->f, prop);
17250 it->method = GET_FROM_IMAGE;
17252 #endif /* HAVE_WINDOW_SYSTEM */
17253 else
17255 pop_it (it); /* bogus display property, give up */
17256 return 0;
17259 return 1;
17262 /* Return the character-property PROP at the current position in IT. */
17264 static Lisp_Object
17265 get_it_property (struct it *it, Lisp_Object prop)
17267 Lisp_Object position;
17269 if (STRINGP (it->object))
17270 position = make_number (IT_STRING_CHARPOS (*it));
17271 else if (BUFFERP (it->object))
17272 position = make_number (IT_CHARPOS (*it));
17273 else
17274 return Qnil;
17276 return Fget_char_property (position, prop, it->object);
17279 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17281 static void
17282 handle_line_prefix (struct it *it)
17284 Lisp_Object prefix;
17285 if (it->continuation_lines_width > 0)
17287 prefix = get_it_property (it, Qwrap_prefix);
17288 if (NILP (prefix))
17289 prefix = Vwrap_prefix;
17291 else
17293 prefix = get_it_property (it, Qline_prefix);
17294 if (NILP (prefix))
17295 prefix = Vline_prefix;
17297 if (! NILP (prefix) && push_display_prop (it, prefix))
17299 /* If the prefix is wider than the window, and we try to wrap
17300 it, it would acquire its own wrap prefix, and so on till the
17301 iterator stack overflows. So, don't wrap the prefix. */
17302 it->line_wrap = TRUNCATE;
17303 it->avoid_cursor_p = 1;
17309 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17310 only for R2L lines from display_line, when it decides that too many
17311 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17312 continued. */
17313 static void
17314 unproduce_glyphs (struct it *it, int n)
17316 struct glyph *glyph, *end;
17318 xassert (it->glyph_row);
17319 xassert (it->glyph_row->reversed_p);
17320 xassert (it->area == TEXT_AREA);
17321 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17323 if (n > it->glyph_row->used[TEXT_AREA])
17324 n = it->glyph_row->used[TEXT_AREA];
17325 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17326 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17327 for ( ; glyph < end; glyph++)
17328 glyph[-n] = *glyph;
17331 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17332 and ROW->maxpos. */
17333 static void
17334 find_row_edges (struct it *it, struct glyph_row *row,
17335 EMACS_INT min_pos, EMACS_INT min_bpos,
17336 EMACS_INT max_pos, EMACS_INT max_bpos)
17338 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17339 lines' rows is implemented for bidi-reordered rows. */
17341 /* ROW->minpos is the value of min_pos, the minimal buffer position
17342 we have in ROW. */
17343 if (min_pos <= ZV)
17344 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17345 else
17347 /* We didn't find _any_ valid buffer positions in any of the
17348 glyphs, so we must trust the iterator's computed
17349 positions. */
17350 row->minpos = row->start.pos;
17351 max_pos = CHARPOS (it->current.pos);
17352 max_bpos = BYTEPOS (it->current.pos);
17355 if (!max_pos)
17356 abort ();
17358 /* Here are the various use-cases for ending the row, and the
17359 corresponding values for ROW->maxpos:
17361 Line ends in a newline from buffer eol_pos + 1
17362 Line is continued from buffer max_pos + 1
17363 Line is truncated on right it->current.pos
17364 Line ends in a newline from string max_pos
17365 Line is continued from string max_pos
17366 Line is continued from display vector max_pos
17367 Line is entirely from a string min_pos == max_pos
17368 Line is entirely from a display vector min_pos == max_pos
17369 Line that ends at ZV ZV
17371 If you discover other use-cases, please add them here as
17372 appropriate. */
17373 if (row->ends_at_zv_p)
17374 row->maxpos = it->current.pos;
17375 else if (row->used[TEXT_AREA])
17377 if (row->ends_in_newline_from_string_p)
17378 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17379 else if (CHARPOS (it->eol_pos) > 0)
17380 SET_TEXT_POS (row->maxpos,
17381 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17382 else if (row->continued_p)
17384 /* If max_pos is different from IT's current position, it
17385 means IT->method does not belong to the display element
17386 at max_pos. However, it also means that the display
17387 element at max_pos was displayed in its entirety on this
17388 line, which is equivalent to saying that the next line
17389 starts at the next buffer position. */
17390 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17391 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17392 else
17394 INC_BOTH (max_pos, max_bpos);
17395 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17398 else if (row->truncated_on_right_p)
17399 /* display_line already called reseat_at_next_visible_line_start,
17400 which puts the iterator at the beginning of the next line, in
17401 the logical order. */
17402 row->maxpos = it->current.pos;
17403 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17404 /* A line that is entirely from a string/image/stretch... */
17405 row->maxpos = row->minpos;
17406 else
17407 abort ();
17409 else
17410 row->maxpos = it->current.pos;
17413 /* Construct the glyph row IT->glyph_row in the desired matrix of
17414 IT->w from text at the current position of IT. See dispextern.h
17415 for an overview of struct it. Value is non-zero if
17416 IT->glyph_row displays text, as opposed to a line displaying ZV
17417 only. */
17419 static int
17420 display_line (struct it *it)
17422 struct glyph_row *row = it->glyph_row;
17423 Lisp_Object overlay_arrow_string;
17424 struct it wrap_it;
17425 int may_wrap = 0, wrap_x;
17426 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17427 int wrap_row_phys_ascent, wrap_row_phys_height;
17428 int wrap_row_extra_line_spacing;
17429 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17430 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17431 int cvpos;
17432 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17434 /* We always start displaying at hpos zero even if hscrolled. */
17435 xassert (it->hpos == 0 && it->current_x == 0);
17437 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17438 >= it->w->desired_matrix->nrows)
17440 it->w->nrows_scale_factor++;
17441 fonts_changed_p = 1;
17442 return 0;
17445 /* Is IT->w showing the region? */
17446 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17448 /* Clear the result glyph row and enable it. */
17449 prepare_desired_row (row);
17451 row->y = it->current_y;
17452 row->start = it->start;
17453 row->continuation_lines_width = it->continuation_lines_width;
17454 row->displays_text_p = 1;
17455 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17456 it->starts_in_middle_of_char_p = 0;
17458 /* Arrange the overlays nicely for our purposes. Usually, we call
17459 display_line on only one line at a time, in which case this
17460 can't really hurt too much, or we call it on lines which appear
17461 one after another in the buffer, in which case all calls to
17462 recenter_overlay_lists but the first will be pretty cheap. */
17463 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17465 /* Move over display elements that are not visible because we are
17466 hscrolled. This may stop at an x-position < IT->first_visible_x
17467 if the first glyph is partially visible or if we hit a line end. */
17468 if (it->current_x < it->first_visible_x)
17470 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17471 MOVE_TO_POS | MOVE_TO_X);
17473 else
17475 /* We only do this when not calling `move_it_in_display_line_to'
17476 above, because move_it_in_display_line_to calls
17477 handle_line_prefix itself. */
17478 handle_line_prefix (it);
17481 /* Get the initial row height. This is either the height of the
17482 text hscrolled, if there is any, or zero. */
17483 row->ascent = it->max_ascent;
17484 row->height = it->max_ascent + it->max_descent;
17485 row->phys_ascent = it->max_phys_ascent;
17486 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17487 row->extra_line_spacing = it->max_extra_line_spacing;
17489 /* Utility macro to record max and min buffer positions seen until now. */
17490 #define RECORD_MAX_MIN_POS(IT) \
17491 do \
17493 if (IT_CHARPOS (*(IT)) < min_pos) \
17495 min_pos = IT_CHARPOS (*(IT)); \
17496 min_bpos = IT_BYTEPOS (*(IT)); \
17498 if (IT_CHARPOS (*(IT)) > max_pos) \
17500 max_pos = IT_CHARPOS (*(IT)); \
17501 max_bpos = IT_BYTEPOS (*(IT)); \
17504 while (0)
17506 /* Loop generating characters. The loop is left with IT on the next
17507 character to display. */
17508 while (1)
17510 int n_glyphs_before, hpos_before, x_before;
17511 int x, i, nglyphs;
17512 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17514 /* Retrieve the next thing to display. Value is zero if end of
17515 buffer reached. */
17516 if (!get_next_display_element (it))
17518 /* Maybe add a space at the end of this line that is used to
17519 display the cursor there under X. Set the charpos of the
17520 first glyph of blank lines not corresponding to any text
17521 to -1. */
17522 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17523 row->exact_window_width_line_p = 1;
17524 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17525 || row->used[TEXT_AREA] == 0)
17527 row->glyphs[TEXT_AREA]->charpos = -1;
17528 row->displays_text_p = 0;
17530 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17531 && (!MINI_WINDOW_P (it->w)
17532 || (minibuf_level && EQ (it->window, minibuf_window))))
17533 row->indicate_empty_line_p = 1;
17536 it->continuation_lines_width = 0;
17537 row->ends_at_zv_p = 1;
17538 /* A row that displays right-to-left text must always have
17539 its last face extended all the way to the end of line,
17540 even if this row ends in ZV, because we still write to
17541 the screen left to right. */
17542 if (row->reversed_p)
17543 extend_face_to_end_of_line (it);
17544 break;
17547 /* Now, get the metrics of what we want to display. This also
17548 generates glyphs in `row' (which is IT->glyph_row). */
17549 n_glyphs_before = row->used[TEXT_AREA];
17550 x = it->current_x;
17552 /* Remember the line height so far in case the next element doesn't
17553 fit on the line. */
17554 if (it->line_wrap != TRUNCATE)
17556 ascent = it->max_ascent;
17557 descent = it->max_descent;
17558 phys_ascent = it->max_phys_ascent;
17559 phys_descent = it->max_phys_descent;
17561 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17563 if (IT_DISPLAYING_WHITESPACE (it))
17564 may_wrap = 1;
17565 else if (may_wrap)
17567 wrap_it = *it;
17568 wrap_x = x;
17569 wrap_row_used = row->used[TEXT_AREA];
17570 wrap_row_ascent = row->ascent;
17571 wrap_row_height = row->height;
17572 wrap_row_phys_ascent = row->phys_ascent;
17573 wrap_row_phys_height = row->phys_height;
17574 wrap_row_extra_line_spacing = row->extra_line_spacing;
17575 wrap_row_min_pos = min_pos;
17576 wrap_row_min_bpos = min_bpos;
17577 wrap_row_max_pos = max_pos;
17578 wrap_row_max_bpos = max_bpos;
17579 may_wrap = 0;
17584 PRODUCE_GLYPHS (it);
17586 /* If this display element was in marginal areas, continue with
17587 the next one. */
17588 if (it->area != TEXT_AREA)
17590 row->ascent = max (row->ascent, it->max_ascent);
17591 row->height = max (row->height, it->max_ascent + it->max_descent);
17592 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17593 row->phys_height = max (row->phys_height,
17594 it->max_phys_ascent + it->max_phys_descent);
17595 row->extra_line_spacing = max (row->extra_line_spacing,
17596 it->max_extra_line_spacing);
17597 set_iterator_to_next (it, 1);
17598 continue;
17601 /* Does the display element fit on the line? If we truncate
17602 lines, we should draw past the right edge of the window. If
17603 we don't truncate, we want to stop so that we can display the
17604 continuation glyph before the right margin. If lines are
17605 continued, there are two possible strategies for characters
17606 resulting in more than 1 glyph (e.g. tabs): Display as many
17607 glyphs as possible in this line and leave the rest for the
17608 continuation line, or display the whole element in the next
17609 line. Original redisplay did the former, so we do it also. */
17610 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17611 hpos_before = it->hpos;
17612 x_before = x;
17614 if (/* Not a newline. */
17615 nglyphs > 0
17616 /* Glyphs produced fit entirely in the line. */
17617 && it->current_x < it->last_visible_x)
17619 it->hpos += nglyphs;
17620 row->ascent = max (row->ascent, it->max_ascent);
17621 row->height = max (row->height, it->max_ascent + it->max_descent);
17622 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17623 row->phys_height = max (row->phys_height,
17624 it->max_phys_ascent + it->max_phys_descent);
17625 row->extra_line_spacing = max (row->extra_line_spacing,
17626 it->max_extra_line_spacing);
17627 if (it->current_x - it->pixel_width < it->first_visible_x)
17628 row->x = x - it->first_visible_x;
17629 /* Record the maximum and minimum buffer positions seen so
17630 far in glyphs that will be displayed by this row. */
17631 if (it->bidi_p)
17632 RECORD_MAX_MIN_POS (it);
17634 else
17636 int new_x;
17637 struct glyph *glyph;
17639 for (i = 0; i < nglyphs; ++i, x = new_x)
17641 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17642 new_x = x + glyph->pixel_width;
17644 if (/* Lines are continued. */
17645 it->line_wrap != TRUNCATE
17646 && (/* Glyph doesn't fit on the line. */
17647 new_x > it->last_visible_x
17648 /* Or it fits exactly on a window system frame. */
17649 || (new_x == it->last_visible_x
17650 && FRAME_WINDOW_P (it->f))))
17652 /* End of a continued line. */
17654 if (it->hpos == 0
17655 || (new_x == it->last_visible_x
17656 && FRAME_WINDOW_P (it->f)))
17658 /* Current glyph is the only one on the line or
17659 fits exactly on the line. We must continue
17660 the line because we can't draw the cursor
17661 after the glyph. */
17662 row->continued_p = 1;
17663 it->current_x = new_x;
17664 it->continuation_lines_width += new_x;
17665 ++it->hpos;
17666 /* Record the maximum and minimum buffer
17667 positions seen so far in glyphs that will be
17668 displayed by this row. */
17669 if (it->bidi_p)
17670 RECORD_MAX_MIN_POS (it);
17671 if (i == nglyphs - 1)
17673 /* If line-wrap is on, check if a previous
17674 wrap point was found. */
17675 if (wrap_row_used > 0
17676 /* Even if there is a previous wrap
17677 point, continue the line here as
17678 usual, if (i) the previous character
17679 was a space or tab AND (ii) the
17680 current character is not. */
17681 && (!may_wrap
17682 || IT_DISPLAYING_WHITESPACE (it)))
17683 goto back_to_wrap;
17685 set_iterator_to_next (it, 1);
17686 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17688 if (!get_next_display_element (it))
17690 row->exact_window_width_line_p = 1;
17691 it->continuation_lines_width = 0;
17692 row->continued_p = 0;
17693 row->ends_at_zv_p = 1;
17695 else if (ITERATOR_AT_END_OF_LINE_P (it))
17697 row->continued_p = 0;
17698 row->exact_window_width_line_p = 1;
17703 else if (CHAR_GLYPH_PADDING_P (*glyph)
17704 && !FRAME_WINDOW_P (it->f))
17706 /* A padding glyph that doesn't fit on this line.
17707 This means the whole character doesn't fit
17708 on the line. */
17709 if (row->reversed_p)
17710 unproduce_glyphs (it, row->used[TEXT_AREA]
17711 - n_glyphs_before);
17712 row->used[TEXT_AREA] = n_glyphs_before;
17714 /* Fill the rest of the row with continuation
17715 glyphs like in 20.x. */
17716 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17717 < row->glyphs[1 + TEXT_AREA])
17718 produce_special_glyphs (it, IT_CONTINUATION);
17720 row->continued_p = 1;
17721 it->current_x = x_before;
17722 it->continuation_lines_width += x_before;
17724 /* Restore the height to what it was before the
17725 element not fitting on the line. */
17726 it->max_ascent = ascent;
17727 it->max_descent = descent;
17728 it->max_phys_ascent = phys_ascent;
17729 it->max_phys_descent = phys_descent;
17731 else if (wrap_row_used > 0)
17733 back_to_wrap:
17734 if (row->reversed_p)
17735 unproduce_glyphs (it,
17736 row->used[TEXT_AREA] - wrap_row_used);
17737 *it = wrap_it;
17738 it->continuation_lines_width += wrap_x;
17739 row->used[TEXT_AREA] = wrap_row_used;
17740 row->ascent = wrap_row_ascent;
17741 row->height = wrap_row_height;
17742 row->phys_ascent = wrap_row_phys_ascent;
17743 row->phys_height = wrap_row_phys_height;
17744 row->extra_line_spacing = wrap_row_extra_line_spacing;
17745 min_pos = wrap_row_min_pos;
17746 min_bpos = wrap_row_min_bpos;
17747 max_pos = wrap_row_max_pos;
17748 max_bpos = wrap_row_max_bpos;
17749 row->continued_p = 1;
17750 row->ends_at_zv_p = 0;
17751 row->exact_window_width_line_p = 0;
17752 it->continuation_lines_width += x;
17754 /* Make sure that a non-default face is extended
17755 up to the right margin of the window. */
17756 extend_face_to_end_of_line (it);
17758 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17760 /* A TAB that extends past the right edge of the
17761 window. This produces a single glyph on
17762 window system frames. We leave the glyph in
17763 this row and let it fill the row, but don't
17764 consume the TAB. */
17765 it->continuation_lines_width += it->last_visible_x;
17766 row->ends_in_middle_of_char_p = 1;
17767 row->continued_p = 1;
17768 glyph->pixel_width = it->last_visible_x - x;
17769 it->starts_in_middle_of_char_p = 1;
17771 else
17773 /* Something other than a TAB that draws past
17774 the right edge of the window. Restore
17775 positions to values before the element. */
17776 if (row->reversed_p)
17777 unproduce_glyphs (it, row->used[TEXT_AREA]
17778 - (n_glyphs_before + i));
17779 row->used[TEXT_AREA] = n_glyphs_before + i;
17781 /* Display continuation glyphs. */
17782 if (!FRAME_WINDOW_P (it->f))
17783 produce_special_glyphs (it, IT_CONTINUATION);
17784 row->continued_p = 1;
17786 it->current_x = x_before;
17787 it->continuation_lines_width += x;
17788 extend_face_to_end_of_line (it);
17790 if (nglyphs > 1 && i > 0)
17792 row->ends_in_middle_of_char_p = 1;
17793 it->starts_in_middle_of_char_p = 1;
17796 /* Restore the height to what it was before the
17797 element not fitting on the line. */
17798 it->max_ascent = ascent;
17799 it->max_descent = descent;
17800 it->max_phys_ascent = phys_ascent;
17801 it->max_phys_descent = phys_descent;
17804 break;
17806 else if (new_x > it->first_visible_x)
17808 /* Increment number of glyphs actually displayed. */
17809 ++it->hpos;
17811 /* Record the maximum and minimum buffer positions
17812 seen so far in glyphs that will be displayed by
17813 this row. */
17814 if (it->bidi_p)
17815 RECORD_MAX_MIN_POS (it);
17817 if (x < it->first_visible_x)
17818 /* Glyph is partially visible, i.e. row starts at
17819 negative X position. */
17820 row->x = x - it->first_visible_x;
17822 else
17824 /* Glyph is completely off the left margin of the
17825 window. This should not happen because of the
17826 move_it_in_display_line at the start of this
17827 function, unless the text display area of the
17828 window is empty. */
17829 xassert (it->first_visible_x <= it->last_visible_x);
17833 row->ascent = max (row->ascent, it->max_ascent);
17834 row->height = max (row->height, it->max_ascent + it->max_descent);
17835 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17836 row->phys_height = max (row->phys_height,
17837 it->max_phys_ascent + it->max_phys_descent);
17838 row->extra_line_spacing = max (row->extra_line_spacing,
17839 it->max_extra_line_spacing);
17841 /* End of this display line if row is continued. */
17842 if (row->continued_p || row->ends_at_zv_p)
17843 break;
17846 at_end_of_line:
17847 /* Is this a line end? If yes, we're also done, after making
17848 sure that a non-default face is extended up to the right
17849 margin of the window. */
17850 if (ITERATOR_AT_END_OF_LINE_P (it))
17852 int used_before = row->used[TEXT_AREA];
17854 row->ends_in_newline_from_string_p = STRINGP (it->object);
17856 /* Add a space at the end of the line that is used to
17857 display the cursor there. */
17858 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17859 append_space_for_newline (it, 0);
17861 /* Extend the face to the end of the line. */
17862 extend_face_to_end_of_line (it);
17864 /* Make sure we have the position. */
17865 if (used_before == 0)
17866 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17868 /* Record the position of the newline, for use in
17869 find_row_edges. */
17870 it->eol_pos = it->current.pos;
17872 /* Consume the line end. This skips over invisible lines. */
17873 set_iterator_to_next (it, 1);
17874 it->continuation_lines_width = 0;
17875 break;
17878 /* Proceed with next display element. Note that this skips
17879 over lines invisible because of selective display. */
17880 set_iterator_to_next (it, 1);
17882 /* If we truncate lines, we are done when the last displayed
17883 glyphs reach past the right margin of the window. */
17884 if (it->line_wrap == TRUNCATE
17885 && (FRAME_WINDOW_P (it->f)
17886 ? (it->current_x >= it->last_visible_x)
17887 : (it->current_x > it->last_visible_x)))
17889 /* Maybe add truncation glyphs. */
17890 if (!FRAME_WINDOW_P (it->f))
17892 int i, n;
17894 if (!row->reversed_p)
17896 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17897 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17898 break;
17900 else
17902 for (i = 0; i < row->used[TEXT_AREA]; i++)
17903 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17904 break;
17905 /* Remove any padding glyphs at the front of ROW, to
17906 make room for the truncation glyphs we will be
17907 adding below. The loop below always inserts at
17908 least one truncation glyph, so also remove the
17909 last glyph added to ROW. */
17910 unproduce_glyphs (it, i + 1);
17911 /* Adjust i for the loop below. */
17912 i = row->used[TEXT_AREA] - (i + 1);
17915 for (n = row->used[TEXT_AREA]; i < n; ++i)
17917 row->used[TEXT_AREA] = i;
17918 produce_special_glyphs (it, IT_TRUNCATION);
17921 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17923 /* Don't truncate if we can overflow newline into fringe. */
17924 if (!get_next_display_element (it))
17926 it->continuation_lines_width = 0;
17927 row->ends_at_zv_p = 1;
17928 row->exact_window_width_line_p = 1;
17929 break;
17931 if (ITERATOR_AT_END_OF_LINE_P (it))
17933 row->exact_window_width_line_p = 1;
17934 goto at_end_of_line;
17938 row->truncated_on_right_p = 1;
17939 it->continuation_lines_width = 0;
17940 reseat_at_next_visible_line_start (it, 0);
17941 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17942 it->hpos = hpos_before;
17943 it->current_x = x_before;
17944 break;
17948 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17949 at the left window margin. */
17950 if (it->first_visible_x
17951 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17953 if (!FRAME_WINDOW_P (it->f))
17954 insert_left_trunc_glyphs (it);
17955 row->truncated_on_left_p = 1;
17958 /* Remember the position at which this line ends.
17960 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17961 cannot be before the call to find_row_edges below, since that is
17962 where these positions are determined. */
17963 row->end = it->current;
17964 if (!it->bidi_p)
17966 row->minpos = row->start.pos;
17967 row->maxpos = row->end.pos;
17969 else
17971 /* ROW->minpos and ROW->maxpos must be the smallest and
17972 `1 + the largest' buffer positions in ROW. But if ROW was
17973 bidi-reordered, these two positions can be anywhere in the
17974 row, so we must determine them now. */
17975 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17978 /* If the start of this line is the overlay arrow-position, then
17979 mark this glyph row as the one containing the overlay arrow.
17980 This is clearly a mess with variable size fonts. It would be
17981 better to let it be displayed like cursors under X. */
17982 if ((row->displays_text_p || !overlay_arrow_seen)
17983 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17984 !NILP (overlay_arrow_string)))
17986 /* Overlay arrow in window redisplay is a fringe bitmap. */
17987 if (STRINGP (overlay_arrow_string))
17989 struct glyph_row *arrow_row
17990 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17991 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17992 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17993 struct glyph *p = row->glyphs[TEXT_AREA];
17994 struct glyph *p2, *end;
17996 /* Copy the arrow glyphs. */
17997 while (glyph < arrow_end)
17998 *p++ = *glyph++;
18000 /* Throw away padding glyphs. */
18001 p2 = p;
18002 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18003 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18004 ++p2;
18005 if (p2 > p)
18007 while (p2 < end)
18008 *p++ = *p2++;
18009 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18012 else
18014 xassert (INTEGERP (overlay_arrow_string));
18015 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18017 overlay_arrow_seen = 1;
18020 /* Compute pixel dimensions of this line. */
18021 compute_line_metrics (it);
18023 /* Record whether this row ends inside an ellipsis. */
18024 row->ends_in_ellipsis_p
18025 = (it->method == GET_FROM_DISPLAY_VECTOR
18026 && it->ellipsis_p);
18028 /* Save fringe bitmaps in this row. */
18029 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18030 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18031 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18032 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18034 it->left_user_fringe_bitmap = 0;
18035 it->left_user_fringe_face_id = 0;
18036 it->right_user_fringe_bitmap = 0;
18037 it->right_user_fringe_face_id = 0;
18039 /* Maybe set the cursor. */
18040 cvpos = it->w->cursor.vpos;
18041 if ((cvpos < 0
18042 /* In bidi-reordered rows, keep checking for proper cursor
18043 position even if one has been found already, because buffer
18044 positions in such rows change non-linearly with ROW->VPOS,
18045 when a line is continued. One exception: when we are at ZV,
18046 display cursor on the first suitable glyph row, since all
18047 the empty rows after that also have their position set to ZV. */
18048 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18049 lines' rows is implemented for bidi-reordered rows. */
18050 || (it->bidi_p
18051 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18052 && PT >= MATRIX_ROW_START_CHARPOS (row)
18053 && PT <= MATRIX_ROW_END_CHARPOS (row)
18054 && cursor_row_p (it->w, row))
18055 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18057 /* Highlight trailing whitespace. */
18058 if (!NILP (Vshow_trailing_whitespace))
18059 highlight_trailing_whitespace (it->f, it->glyph_row);
18061 /* Prepare for the next line. This line starts horizontally at (X
18062 HPOS) = (0 0). Vertical positions are incremented. As a
18063 convenience for the caller, IT->glyph_row is set to the next
18064 row to be used. */
18065 it->current_x = it->hpos = 0;
18066 it->current_y += row->height;
18067 SET_TEXT_POS (it->eol_pos, 0, 0);
18068 ++it->vpos;
18069 ++it->glyph_row;
18070 /* The next row should by default use the same value of the
18071 reversed_p flag as this one. set_iterator_to_next decides when
18072 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18073 the flag accordingly. */
18074 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18075 it->glyph_row->reversed_p = row->reversed_p;
18076 it->start = row->end;
18077 return row->displays_text_p;
18079 #undef RECORD_MAX_MIN_POS
18082 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18083 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18084 doc: /* Return paragraph direction at point in BUFFER.
18085 Value is either `left-to-right' or `right-to-left'.
18086 If BUFFER is omitted or nil, it defaults to the current buffer.
18088 Paragraph direction determines how the text in the paragraph is displayed.
18089 In left-to-right paragraphs, text begins at the left margin of the window
18090 and the reading direction is generally left to right. In right-to-left
18091 paragraphs, text begins at the right margin and is read from right to left.
18093 See also `bidi-paragraph-direction'. */)
18094 (Lisp_Object buffer)
18096 struct buffer *buf;
18097 struct buffer *old;
18099 if (NILP (buffer))
18100 buf = current_buffer;
18101 else
18103 CHECK_BUFFER (buffer);
18104 buf = XBUFFER (buffer);
18105 old = current_buffer;
18108 if (NILP (buf->bidi_display_reordering))
18109 return Qleft_to_right;
18110 else if (!NILP (buf->bidi_paragraph_direction))
18111 return buf->bidi_paragraph_direction;
18112 else
18114 /* Determine the direction from buffer text. We could try to
18115 use current_matrix if it is up to date, but this seems fast
18116 enough as it is. */
18117 struct bidi_it itb;
18118 EMACS_INT pos = BUF_PT (buf);
18119 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18120 int c;
18122 if (buf != current_buffer)
18123 set_buffer_temp (buf);
18124 /* bidi_paragraph_init finds the base direction of the paragraph
18125 by searching forward from paragraph start. We need the base
18126 direction of the current or _previous_ paragraph, so we need
18127 to make sure we are within that paragraph. To that end, find
18128 the previous non-empty line. */
18129 if (pos >= ZV && pos > BEGV)
18131 pos--;
18132 bytepos = CHAR_TO_BYTE (pos);
18134 while ((c = FETCH_BYTE (bytepos)) == '\n'
18135 || c == ' ' || c == '\t' || c == '\f')
18137 if (bytepos <= BEGV_BYTE)
18138 break;
18139 bytepos--;
18140 pos--;
18142 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18143 bytepos--;
18144 itb.charpos = pos;
18145 itb.bytepos = bytepos;
18146 itb.first_elt = 1;
18147 itb.separator_limit = -1;
18148 itb.paragraph_dir = NEUTRAL_DIR;
18150 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18151 if (buf != current_buffer)
18152 set_buffer_temp (old);
18153 switch (itb.paragraph_dir)
18155 case L2R:
18156 return Qleft_to_right;
18157 break;
18158 case R2L:
18159 return Qright_to_left;
18160 break;
18161 default:
18162 abort ();
18169 /***********************************************************************
18170 Menu Bar
18171 ***********************************************************************/
18173 /* Redisplay the menu bar in the frame for window W.
18175 The menu bar of X frames that don't have X toolkit support is
18176 displayed in a special window W->frame->menu_bar_window.
18178 The menu bar of terminal frames is treated specially as far as
18179 glyph matrices are concerned. Menu bar lines are not part of
18180 windows, so the update is done directly on the frame matrix rows
18181 for the menu bar. */
18183 static void
18184 display_menu_bar (struct window *w)
18186 struct frame *f = XFRAME (WINDOW_FRAME (w));
18187 struct it it;
18188 Lisp_Object items;
18189 int i;
18191 /* Don't do all this for graphical frames. */
18192 #ifdef HAVE_NTGUI
18193 if (FRAME_W32_P (f))
18194 return;
18195 #endif
18196 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18197 if (FRAME_X_P (f))
18198 return;
18199 #endif
18201 #ifdef HAVE_NS
18202 if (FRAME_NS_P (f))
18203 return;
18204 #endif /* HAVE_NS */
18206 #ifdef USE_X_TOOLKIT
18207 xassert (!FRAME_WINDOW_P (f));
18208 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18209 it.first_visible_x = 0;
18210 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18211 #else /* not USE_X_TOOLKIT */
18212 if (FRAME_WINDOW_P (f))
18214 /* Menu bar lines are displayed in the desired matrix of the
18215 dummy window menu_bar_window. */
18216 struct window *menu_w;
18217 xassert (WINDOWP (f->menu_bar_window));
18218 menu_w = XWINDOW (f->menu_bar_window);
18219 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18220 MENU_FACE_ID);
18221 it.first_visible_x = 0;
18222 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18224 else
18226 /* This is a TTY frame, i.e. character hpos/vpos are used as
18227 pixel x/y. */
18228 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18229 MENU_FACE_ID);
18230 it.first_visible_x = 0;
18231 it.last_visible_x = FRAME_COLS (f);
18233 #endif /* not USE_X_TOOLKIT */
18235 if (! mode_line_inverse_video)
18236 /* Force the menu-bar to be displayed in the default face. */
18237 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18239 /* Clear all rows of the menu bar. */
18240 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18242 struct glyph_row *row = it.glyph_row + i;
18243 clear_glyph_row (row);
18244 row->enabled_p = 1;
18245 row->full_width_p = 1;
18248 /* Display all items of the menu bar. */
18249 items = FRAME_MENU_BAR_ITEMS (it.f);
18250 for (i = 0; i < XVECTOR (items)->size; i += 4)
18252 Lisp_Object string;
18254 /* Stop at nil string. */
18255 string = AREF (items, i + 1);
18256 if (NILP (string))
18257 break;
18259 /* Remember where item was displayed. */
18260 ASET (items, i + 3, make_number (it.hpos));
18262 /* Display the item, pad with one space. */
18263 if (it.current_x < it.last_visible_x)
18264 display_string (NULL, string, Qnil, 0, 0, &it,
18265 SCHARS (string) + 1, 0, 0, -1);
18268 /* Fill out the line with spaces. */
18269 if (it.current_x < it.last_visible_x)
18270 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18272 /* Compute the total height of the lines. */
18273 compute_line_metrics (&it);
18278 /***********************************************************************
18279 Mode Line
18280 ***********************************************************************/
18282 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18283 FORCE is non-zero, redisplay mode lines unconditionally.
18284 Otherwise, redisplay only mode lines that are garbaged. Value is
18285 the number of windows whose mode lines were redisplayed. */
18287 static int
18288 redisplay_mode_lines (Lisp_Object window, int force)
18290 int nwindows = 0;
18292 while (!NILP (window))
18294 struct window *w = XWINDOW (window);
18296 if (WINDOWP (w->hchild))
18297 nwindows += redisplay_mode_lines (w->hchild, force);
18298 else if (WINDOWP (w->vchild))
18299 nwindows += redisplay_mode_lines (w->vchild, force);
18300 else if (force
18301 || FRAME_GARBAGED_P (XFRAME (w->frame))
18302 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18304 struct text_pos lpoint;
18305 struct buffer *old = current_buffer;
18307 /* Set the window's buffer for the mode line display. */
18308 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18309 set_buffer_internal_1 (XBUFFER (w->buffer));
18311 /* Point refers normally to the selected window. For any
18312 other window, set up appropriate value. */
18313 if (!EQ (window, selected_window))
18315 struct text_pos pt;
18317 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18318 if (CHARPOS (pt) < BEGV)
18319 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18320 else if (CHARPOS (pt) > (ZV - 1))
18321 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18322 else
18323 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18326 /* Display mode lines. */
18327 clear_glyph_matrix (w->desired_matrix);
18328 if (display_mode_lines (w))
18330 ++nwindows;
18331 w->must_be_updated_p = 1;
18334 /* Restore old settings. */
18335 set_buffer_internal_1 (old);
18336 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18339 window = w->next;
18342 return nwindows;
18346 /* Display the mode and/or header line of window W. Value is the
18347 sum number of mode lines and header lines displayed. */
18349 static int
18350 display_mode_lines (struct window *w)
18352 Lisp_Object old_selected_window, old_selected_frame;
18353 int n = 0;
18355 old_selected_frame = selected_frame;
18356 selected_frame = w->frame;
18357 old_selected_window = selected_window;
18358 XSETWINDOW (selected_window, w);
18360 /* These will be set while the mode line specs are processed. */
18361 line_number_displayed = 0;
18362 w->column_number_displayed = Qnil;
18364 if (WINDOW_WANTS_MODELINE_P (w))
18366 struct window *sel_w = XWINDOW (old_selected_window);
18368 /* Select mode line face based on the real selected window. */
18369 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18370 current_buffer->mode_line_format);
18371 ++n;
18374 if (WINDOW_WANTS_HEADER_LINE_P (w))
18376 display_mode_line (w, HEADER_LINE_FACE_ID,
18377 current_buffer->header_line_format);
18378 ++n;
18381 selected_frame = old_selected_frame;
18382 selected_window = old_selected_window;
18383 return n;
18387 /* Display mode or header line of window W. FACE_ID specifies which
18388 line to display; it is either MODE_LINE_FACE_ID or
18389 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18390 display. Value is the pixel height of the mode/header line
18391 displayed. */
18393 static int
18394 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18396 struct it it;
18397 struct face *face;
18398 int count = SPECPDL_INDEX ();
18400 init_iterator (&it, w, -1, -1, NULL, face_id);
18401 /* Don't extend on a previously drawn mode-line.
18402 This may happen if called from pos_visible_p. */
18403 it.glyph_row->enabled_p = 0;
18404 prepare_desired_row (it.glyph_row);
18406 it.glyph_row->mode_line_p = 1;
18408 if (! mode_line_inverse_video)
18409 /* Force the mode-line to be displayed in the default face. */
18410 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18412 record_unwind_protect (unwind_format_mode_line,
18413 format_mode_line_unwind_data (NULL, Qnil, 0));
18415 mode_line_target = MODE_LINE_DISPLAY;
18417 /* Temporarily make frame's keyboard the current kboard so that
18418 kboard-local variables in the mode_line_format will get the right
18419 values. */
18420 push_kboard (FRAME_KBOARD (it.f));
18421 record_unwind_save_match_data ();
18422 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18423 pop_kboard ();
18425 unbind_to (count, Qnil);
18427 /* Fill up with spaces. */
18428 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18430 compute_line_metrics (&it);
18431 it.glyph_row->full_width_p = 1;
18432 it.glyph_row->continued_p = 0;
18433 it.glyph_row->truncated_on_left_p = 0;
18434 it.glyph_row->truncated_on_right_p = 0;
18436 /* Make a 3D mode-line have a shadow at its right end. */
18437 face = FACE_FROM_ID (it.f, face_id);
18438 extend_face_to_end_of_line (&it);
18439 if (face->box != FACE_NO_BOX)
18441 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18442 + it.glyph_row->used[TEXT_AREA] - 1);
18443 last->right_box_line_p = 1;
18446 return it.glyph_row->height;
18449 /* Move element ELT in LIST to the front of LIST.
18450 Return the updated list. */
18452 static Lisp_Object
18453 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18455 register Lisp_Object tail, prev;
18456 register Lisp_Object tem;
18458 tail = list;
18459 prev = Qnil;
18460 while (CONSP (tail))
18462 tem = XCAR (tail);
18464 if (EQ (elt, tem))
18466 /* Splice out the link TAIL. */
18467 if (NILP (prev))
18468 list = XCDR (tail);
18469 else
18470 Fsetcdr (prev, XCDR (tail));
18472 /* Now make it the first. */
18473 Fsetcdr (tail, list);
18474 return tail;
18476 else
18477 prev = tail;
18478 tail = XCDR (tail);
18479 QUIT;
18482 /* Not found--return unchanged LIST. */
18483 return list;
18486 /* Contribute ELT to the mode line for window IT->w. How it
18487 translates into text depends on its data type.
18489 IT describes the display environment in which we display, as usual.
18491 DEPTH is the depth in recursion. It is used to prevent
18492 infinite recursion here.
18494 FIELD_WIDTH is the number of characters the display of ELT should
18495 occupy in the mode line, and PRECISION is the maximum number of
18496 characters to display from ELT's representation. See
18497 display_string for details.
18499 Returns the hpos of the end of the text generated by ELT.
18501 PROPS is a property list to add to any string we encounter.
18503 If RISKY is nonzero, remove (disregard) any properties in any string
18504 we encounter, and ignore :eval and :propertize.
18506 The global variable `mode_line_target' determines whether the
18507 output is passed to `store_mode_line_noprop',
18508 `store_mode_line_string', or `display_string'. */
18510 static int
18511 display_mode_element (struct it *it, int depth, int field_width, int precision,
18512 Lisp_Object elt, Lisp_Object props, int risky)
18514 int n = 0, field, prec;
18515 int literal = 0;
18517 tail_recurse:
18518 if (depth > 100)
18519 elt = build_string ("*too-deep*");
18521 depth++;
18523 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18525 case Lisp_String:
18527 /* A string: output it and check for %-constructs within it. */
18528 unsigned char c;
18529 EMACS_INT offset = 0;
18531 if (SCHARS (elt) > 0
18532 && (!NILP (props) || risky))
18534 Lisp_Object oprops, aelt;
18535 oprops = Ftext_properties_at (make_number (0), elt);
18537 /* If the starting string's properties are not what
18538 we want, translate the string. Also, if the string
18539 is risky, do that anyway. */
18541 if (NILP (Fequal (props, oprops)) || risky)
18543 /* If the starting string has properties,
18544 merge the specified ones onto the existing ones. */
18545 if (! NILP (oprops) && !risky)
18547 Lisp_Object tem;
18549 oprops = Fcopy_sequence (oprops);
18550 tem = props;
18551 while (CONSP (tem))
18553 oprops = Fplist_put (oprops, XCAR (tem),
18554 XCAR (XCDR (tem)));
18555 tem = XCDR (XCDR (tem));
18557 props = oprops;
18560 aelt = Fassoc (elt, mode_line_proptrans_alist);
18561 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18563 /* AELT is what we want. Move it to the front
18564 without consing. */
18565 elt = XCAR (aelt);
18566 mode_line_proptrans_alist
18567 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18569 else
18571 Lisp_Object tem;
18573 /* If AELT has the wrong props, it is useless.
18574 so get rid of it. */
18575 if (! NILP (aelt))
18576 mode_line_proptrans_alist
18577 = Fdelq (aelt, mode_line_proptrans_alist);
18579 elt = Fcopy_sequence (elt);
18580 Fset_text_properties (make_number (0), Flength (elt),
18581 props, elt);
18582 /* Add this item to mode_line_proptrans_alist. */
18583 mode_line_proptrans_alist
18584 = Fcons (Fcons (elt, props),
18585 mode_line_proptrans_alist);
18586 /* Truncate mode_line_proptrans_alist
18587 to at most 50 elements. */
18588 tem = Fnthcdr (make_number (50),
18589 mode_line_proptrans_alist);
18590 if (! NILP (tem))
18591 XSETCDR (tem, Qnil);
18596 offset = 0;
18598 if (literal)
18600 prec = precision - n;
18601 switch (mode_line_target)
18603 case MODE_LINE_NOPROP:
18604 case MODE_LINE_TITLE:
18605 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18606 break;
18607 case MODE_LINE_STRING:
18608 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18609 break;
18610 case MODE_LINE_DISPLAY:
18611 n += display_string (NULL, elt, Qnil, 0, 0, it,
18612 0, prec, 0, STRING_MULTIBYTE (elt));
18613 break;
18616 break;
18619 /* Handle the non-literal case. */
18621 while ((precision <= 0 || n < precision)
18622 && SREF (elt, offset) != 0
18623 && (mode_line_target != MODE_LINE_DISPLAY
18624 || it->current_x < it->last_visible_x))
18626 EMACS_INT last_offset = offset;
18628 /* Advance to end of string or next format specifier. */
18629 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18632 if (offset - 1 != last_offset)
18634 EMACS_INT nchars, nbytes;
18636 /* Output to end of string or up to '%'. Field width
18637 is length of string. Don't output more than
18638 PRECISION allows us. */
18639 offset--;
18641 prec = c_string_width (SDATA (elt) + last_offset,
18642 offset - last_offset, precision - n,
18643 &nchars, &nbytes);
18645 switch (mode_line_target)
18647 case MODE_LINE_NOPROP:
18648 case MODE_LINE_TITLE:
18649 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18650 break;
18651 case MODE_LINE_STRING:
18653 EMACS_INT bytepos = last_offset;
18654 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18655 EMACS_INT endpos = (precision <= 0
18656 ? string_byte_to_char (elt, offset)
18657 : charpos + nchars);
18659 n += store_mode_line_string (NULL,
18660 Fsubstring (elt, make_number (charpos),
18661 make_number (endpos)),
18662 0, 0, 0, Qnil);
18664 break;
18665 case MODE_LINE_DISPLAY:
18667 EMACS_INT bytepos = last_offset;
18668 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18670 if (precision <= 0)
18671 nchars = string_byte_to_char (elt, offset) - charpos;
18672 n += display_string (NULL, elt, Qnil, 0, charpos,
18673 it, 0, nchars, 0,
18674 STRING_MULTIBYTE (elt));
18676 break;
18679 else /* c == '%' */
18681 EMACS_INT percent_position = offset;
18683 /* Get the specified minimum width. Zero means
18684 don't pad. */
18685 field = 0;
18686 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18687 field = field * 10 + c - '0';
18689 /* Don't pad beyond the total padding allowed. */
18690 if (field_width - n > 0 && field > field_width - n)
18691 field = field_width - n;
18693 /* Note that either PRECISION <= 0 or N < PRECISION. */
18694 prec = precision - n;
18696 if (c == 'M')
18697 n += display_mode_element (it, depth, field, prec,
18698 Vglobal_mode_string, props,
18699 risky);
18700 else if (c != 0)
18702 int multibyte;
18703 EMACS_INT bytepos, charpos;
18704 const unsigned char *spec;
18705 Lisp_Object string;
18707 bytepos = percent_position;
18708 charpos = (STRING_MULTIBYTE (elt)
18709 ? string_byte_to_char (elt, bytepos)
18710 : bytepos);
18711 spec = decode_mode_spec (it->w, c, field, prec, &string);
18712 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18714 switch (mode_line_target)
18716 case MODE_LINE_NOPROP:
18717 case MODE_LINE_TITLE:
18718 n += store_mode_line_noprop (spec, field, prec);
18719 break;
18720 case MODE_LINE_STRING:
18722 int len = strlen (spec);
18723 Lisp_Object tem = make_string (spec, len);
18724 props = Ftext_properties_at (make_number (charpos), elt);
18725 /* Should only keep face property in props */
18726 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18728 break;
18729 case MODE_LINE_DISPLAY:
18731 int nglyphs_before, nwritten;
18733 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18734 nwritten = display_string (spec, string, elt,
18735 charpos, 0, it,
18736 field, prec, 0,
18737 multibyte);
18739 /* Assign to the glyphs written above the
18740 string where the `%x' came from, position
18741 of the `%'. */
18742 if (nwritten > 0)
18744 struct glyph *glyph
18745 = (it->glyph_row->glyphs[TEXT_AREA]
18746 + nglyphs_before);
18747 int i;
18749 for (i = 0; i < nwritten; ++i)
18751 glyph[i].object = elt;
18752 glyph[i].charpos = charpos;
18755 n += nwritten;
18758 break;
18761 else /* c == 0 */
18762 break;
18766 break;
18768 case Lisp_Symbol:
18769 /* A symbol: process the value of the symbol recursively
18770 as if it appeared here directly. Avoid error if symbol void.
18771 Special case: if value of symbol is a string, output the string
18772 literally. */
18774 register Lisp_Object tem;
18776 /* If the variable is not marked as risky to set
18777 then its contents are risky to use. */
18778 if (NILP (Fget (elt, Qrisky_local_variable)))
18779 risky = 1;
18781 tem = Fboundp (elt);
18782 if (!NILP (tem))
18784 tem = Fsymbol_value (elt);
18785 /* If value is a string, output that string literally:
18786 don't check for % within it. */
18787 if (STRINGP (tem))
18788 literal = 1;
18790 if (!EQ (tem, elt))
18792 /* Give up right away for nil or t. */
18793 elt = tem;
18794 goto tail_recurse;
18798 break;
18800 case Lisp_Cons:
18802 register Lisp_Object car, tem;
18804 /* A cons cell: five distinct cases.
18805 If first element is :eval or :propertize, do something special.
18806 If first element is a string or a cons, process all the elements
18807 and effectively concatenate them.
18808 If first element is a negative number, truncate displaying cdr to
18809 at most that many characters. If positive, pad (with spaces)
18810 to at least that many characters.
18811 If first element is a symbol, process the cadr or caddr recursively
18812 according to whether the symbol's value is non-nil or nil. */
18813 car = XCAR (elt);
18814 if (EQ (car, QCeval))
18816 /* An element of the form (:eval FORM) means evaluate FORM
18817 and use the result as mode line elements. */
18819 if (risky)
18820 break;
18822 if (CONSP (XCDR (elt)))
18824 Lisp_Object spec;
18825 spec = safe_eval (XCAR (XCDR (elt)));
18826 n += display_mode_element (it, depth, field_width - n,
18827 precision - n, spec, props,
18828 risky);
18831 else if (EQ (car, QCpropertize))
18833 /* An element of the form (:propertize ELT PROPS...)
18834 means display ELT but applying properties PROPS. */
18836 if (risky)
18837 break;
18839 if (CONSP (XCDR (elt)))
18840 n += display_mode_element (it, depth, field_width - n,
18841 precision - n, XCAR (XCDR (elt)),
18842 XCDR (XCDR (elt)), risky);
18844 else if (SYMBOLP (car))
18846 tem = Fboundp (car);
18847 elt = XCDR (elt);
18848 if (!CONSP (elt))
18849 goto invalid;
18850 /* elt is now the cdr, and we know it is a cons cell.
18851 Use its car if CAR has a non-nil value. */
18852 if (!NILP (tem))
18854 tem = Fsymbol_value (car);
18855 if (!NILP (tem))
18857 elt = XCAR (elt);
18858 goto tail_recurse;
18861 /* Symbol's value is nil (or symbol is unbound)
18862 Get the cddr of the original list
18863 and if possible find the caddr and use that. */
18864 elt = XCDR (elt);
18865 if (NILP (elt))
18866 break;
18867 else if (!CONSP (elt))
18868 goto invalid;
18869 elt = XCAR (elt);
18870 goto tail_recurse;
18872 else if (INTEGERP (car))
18874 register int lim = XINT (car);
18875 elt = XCDR (elt);
18876 if (lim < 0)
18878 /* Negative int means reduce maximum width. */
18879 if (precision <= 0)
18880 precision = -lim;
18881 else
18882 precision = min (precision, -lim);
18884 else if (lim > 0)
18886 /* Padding specified. Don't let it be more than
18887 current maximum. */
18888 if (precision > 0)
18889 lim = min (precision, lim);
18891 /* If that's more padding than already wanted, queue it.
18892 But don't reduce padding already specified even if
18893 that is beyond the current truncation point. */
18894 field_width = max (lim, field_width);
18896 goto tail_recurse;
18898 else if (STRINGP (car) || CONSP (car))
18900 Lisp_Object halftail = elt;
18901 int len = 0;
18903 while (CONSP (elt)
18904 && (precision <= 0 || n < precision))
18906 n += display_mode_element (it, depth,
18907 /* Do padding only after the last
18908 element in the list. */
18909 (! CONSP (XCDR (elt))
18910 ? field_width - n
18911 : 0),
18912 precision - n, XCAR (elt),
18913 props, risky);
18914 elt = XCDR (elt);
18915 len++;
18916 if ((len & 1) == 0)
18917 halftail = XCDR (halftail);
18918 /* Check for cycle. */
18919 if (EQ (halftail, elt))
18920 break;
18924 break;
18926 default:
18927 invalid:
18928 elt = build_string ("*invalid*");
18929 goto tail_recurse;
18932 /* Pad to FIELD_WIDTH. */
18933 if (field_width > 0 && n < field_width)
18935 switch (mode_line_target)
18937 case MODE_LINE_NOPROP:
18938 case MODE_LINE_TITLE:
18939 n += store_mode_line_noprop ("", field_width - n, 0);
18940 break;
18941 case MODE_LINE_STRING:
18942 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18943 break;
18944 case MODE_LINE_DISPLAY:
18945 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18946 0, 0, 0);
18947 break;
18951 return n;
18954 /* Store a mode-line string element in mode_line_string_list.
18956 If STRING is non-null, display that C string. Otherwise, the Lisp
18957 string LISP_STRING is displayed.
18959 FIELD_WIDTH is the minimum number of output glyphs to produce.
18960 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18961 with spaces. FIELD_WIDTH <= 0 means don't pad.
18963 PRECISION is the maximum number of characters to output from
18964 STRING. PRECISION <= 0 means don't truncate the string.
18966 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18967 properties to the string.
18969 PROPS are the properties to add to the string.
18970 The mode_line_string_face face property is always added to the string.
18973 static int
18974 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18975 int field_width, int precision, Lisp_Object props)
18977 EMACS_INT len;
18978 int n = 0;
18980 if (string != NULL)
18982 len = strlen (string);
18983 if (precision > 0 && len > precision)
18984 len = precision;
18985 lisp_string = make_string (string, len);
18986 if (NILP (props))
18987 props = mode_line_string_face_prop;
18988 else if (!NILP (mode_line_string_face))
18990 Lisp_Object face = Fplist_get (props, Qface);
18991 props = Fcopy_sequence (props);
18992 if (NILP (face))
18993 face = mode_line_string_face;
18994 else
18995 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18996 props = Fplist_put (props, Qface, face);
18998 Fadd_text_properties (make_number (0), make_number (len),
18999 props, lisp_string);
19001 else
19003 len = XFASTINT (Flength (lisp_string));
19004 if (precision > 0 && len > precision)
19006 len = precision;
19007 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19008 precision = -1;
19010 if (!NILP (mode_line_string_face))
19012 Lisp_Object face;
19013 if (NILP (props))
19014 props = Ftext_properties_at (make_number (0), lisp_string);
19015 face = Fplist_get (props, Qface);
19016 if (NILP (face))
19017 face = mode_line_string_face;
19018 else
19019 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19020 props = Fcons (Qface, Fcons (face, Qnil));
19021 if (copy_string)
19022 lisp_string = Fcopy_sequence (lisp_string);
19024 if (!NILP (props))
19025 Fadd_text_properties (make_number (0), make_number (len),
19026 props, lisp_string);
19029 if (len > 0)
19031 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19032 n += len;
19035 if (field_width > len)
19037 field_width -= len;
19038 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19039 if (!NILP (props))
19040 Fadd_text_properties (make_number (0), make_number (field_width),
19041 props, lisp_string);
19042 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19043 n += field_width;
19046 return n;
19050 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19051 1, 4, 0,
19052 doc: /* Format a string out of a mode line format specification.
19053 First arg FORMAT specifies the mode line format (see `mode-line-format'
19054 for details) to use.
19056 Optional second arg FACE specifies the face property to put
19057 on all characters for which no face is specified.
19058 The value t means whatever face the window's mode line currently uses
19059 \(either `mode-line' or `mode-line-inactive', depending).
19060 A value of nil means the default is no face property.
19061 If FACE is an integer, the value string has no text properties.
19063 Optional third and fourth args WINDOW and BUFFER specify the window
19064 and buffer to use as the context for the formatting (defaults
19065 are the selected window and the window's buffer). */)
19066 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
19068 struct it it;
19069 int len;
19070 struct window *w;
19071 struct buffer *old_buffer = NULL;
19072 int face_id = -1;
19073 int no_props = INTEGERP (face);
19074 int count = SPECPDL_INDEX ();
19075 Lisp_Object str;
19076 int string_start = 0;
19078 if (NILP (window))
19079 window = selected_window;
19080 CHECK_WINDOW (window);
19081 w = XWINDOW (window);
19083 if (NILP (buffer))
19084 buffer = w->buffer;
19085 CHECK_BUFFER (buffer);
19087 /* Make formatting the modeline a non-op when noninteractive, otherwise
19088 there will be problems later caused by a partially initialized frame. */
19089 if (NILP (format) || noninteractive)
19090 return empty_unibyte_string;
19092 if (no_props)
19093 face = Qnil;
19095 if (!NILP (face))
19097 if (EQ (face, Qt))
19098 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
19099 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
19102 if (face_id < 0)
19103 face_id = DEFAULT_FACE_ID;
19105 if (XBUFFER (buffer) != current_buffer)
19106 old_buffer = current_buffer;
19108 /* Save things including mode_line_proptrans_alist,
19109 and set that to nil so that we don't alter the outer value. */
19110 record_unwind_protect (unwind_format_mode_line,
19111 format_mode_line_unwind_data
19112 (old_buffer, selected_window, 1));
19113 mode_line_proptrans_alist = Qnil;
19115 Fselect_window (window, Qt);
19116 if (old_buffer)
19117 set_buffer_internal_1 (XBUFFER (buffer));
19119 init_iterator (&it, w, -1, -1, NULL, face_id);
19121 if (no_props)
19123 mode_line_target = MODE_LINE_NOPROP;
19124 mode_line_string_face_prop = Qnil;
19125 mode_line_string_list = Qnil;
19126 string_start = MODE_LINE_NOPROP_LEN (0);
19128 else
19130 mode_line_target = MODE_LINE_STRING;
19131 mode_line_string_list = Qnil;
19132 mode_line_string_face = face;
19133 mode_line_string_face_prop
19134 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19137 push_kboard (FRAME_KBOARD (it.f));
19138 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19139 pop_kboard ();
19141 if (no_props)
19143 len = MODE_LINE_NOPROP_LEN (string_start);
19144 str = make_string (mode_line_noprop_buf + string_start, len);
19146 else
19148 mode_line_string_list = Fnreverse (mode_line_string_list);
19149 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19150 empty_unibyte_string);
19153 unbind_to (count, Qnil);
19154 return str;
19157 /* Write a null-terminated, right justified decimal representation of
19158 the positive integer D to BUF using a minimal field width WIDTH. */
19160 static void
19161 pint2str (register char *buf, register int width, register int d)
19163 register char *p = buf;
19165 if (d <= 0)
19166 *p++ = '0';
19167 else
19169 while (d > 0)
19171 *p++ = d % 10 + '0';
19172 d /= 10;
19176 for (width -= (int) (p - buf); width > 0; --width)
19177 *p++ = ' ';
19178 *p-- = '\0';
19179 while (p > buf)
19181 d = *buf;
19182 *buf++ = *p;
19183 *p-- = d;
19187 /* Write a null-terminated, right justified decimal and "human
19188 readable" representation of the nonnegative integer D to BUF using
19189 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19191 static const char power_letter[] =
19193 0, /* not used */
19194 'k', /* kilo */
19195 'M', /* mega */
19196 'G', /* giga */
19197 'T', /* tera */
19198 'P', /* peta */
19199 'E', /* exa */
19200 'Z', /* zetta */
19201 'Y' /* yotta */
19204 static void
19205 pint2hrstr (char *buf, int width, int d)
19207 /* We aim to represent the nonnegative integer D as
19208 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19209 int quotient = d;
19210 int remainder = 0;
19211 /* -1 means: do not use TENTHS. */
19212 int tenths = -1;
19213 int exponent = 0;
19215 /* Length of QUOTIENT.TENTHS as a string. */
19216 int length;
19218 char * psuffix;
19219 char * p;
19221 if (1000 <= quotient)
19223 /* Scale to the appropriate EXPONENT. */
19226 remainder = quotient % 1000;
19227 quotient /= 1000;
19228 exponent++;
19230 while (1000 <= quotient);
19232 /* Round to nearest and decide whether to use TENTHS or not. */
19233 if (quotient <= 9)
19235 tenths = remainder / 100;
19236 if (50 <= remainder % 100)
19238 if (tenths < 9)
19239 tenths++;
19240 else
19242 quotient++;
19243 if (quotient == 10)
19244 tenths = -1;
19245 else
19246 tenths = 0;
19250 else
19251 if (500 <= remainder)
19253 if (quotient < 999)
19254 quotient++;
19255 else
19257 quotient = 1;
19258 exponent++;
19259 tenths = 0;
19264 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19265 if (tenths == -1 && quotient <= 99)
19266 if (quotient <= 9)
19267 length = 1;
19268 else
19269 length = 2;
19270 else
19271 length = 3;
19272 p = psuffix = buf + max (width, length);
19274 /* Print EXPONENT. */
19275 if (exponent)
19276 *psuffix++ = power_letter[exponent];
19277 *psuffix = '\0';
19279 /* Print TENTHS. */
19280 if (tenths >= 0)
19282 *--p = '0' + tenths;
19283 *--p = '.';
19286 /* Print QUOTIENT. */
19289 int digit = quotient % 10;
19290 *--p = '0' + digit;
19292 while ((quotient /= 10) != 0);
19294 /* Print leading spaces. */
19295 while (buf < p)
19296 *--p = ' ';
19299 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19300 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19301 type of CODING_SYSTEM. Return updated pointer into BUF. */
19303 static unsigned char invalid_eol_type[] = "(*invalid*)";
19305 static char *
19306 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19308 Lisp_Object val;
19309 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19310 const unsigned char *eol_str;
19311 int eol_str_len;
19312 /* The EOL conversion we are using. */
19313 Lisp_Object eoltype;
19315 val = CODING_SYSTEM_SPEC (coding_system);
19316 eoltype = Qnil;
19318 if (!VECTORP (val)) /* Not yet decided. */
19320 if (multibyte)
19321 *buf++ = '-';
19322 if (eol_flag)
19323 eoltype = eol_mnemonic_undecided;
19324 /* Don't mention EOL conversion if it isn't decided. */
19326 else
19328 Lisp_Object attrs;
19329 Lisp_Object eolvalue;
19331 attrs = AREF (val, 0);
19332 eolvalue = AREF (val, 2);
19334 if (multibyte)
19335 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19337 if (eol_flag)
19339 /* The EOL conversion that is normal on this system. */
19341 if (NILP (eolvalue)) /* Not yet decided. */
19342 eoltype = eol_mnemonic_undecided;
19343 else if (VECTORP (eolvalue)) /* Not yet decided. */
19344 eoltype = eol_mnemonic_undecided;
19345 else /* eolvalue is Qunix, Qdos, or Qmac. */
19346 eoltype = (EQ (eolvalue, Qunix)
19347 ? eol_mnemonic_unix
19348 : (EQ (eolvalue, Qdos) == 1
19349 ? eol_mnemonic_dos : eol_mnemonic_mac));
19353 if (eol_flag)
19355 /* Mention the EOL conversion if it is not the usual one. */
19356 if (STRINGP (eoltype))
19358 eol_str = SDATA (eoltype);
19359 eol_str_len = SBYTES (eoltype);
19361 else if (CHARACTERP (eoltype))
19363 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19364 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19365 eol_str = tmp;
19367 else
19369 eol_str = invalid_eol_type;
19370 eol_str_len = sizeof (invalid_eol_type) - 1;
19372 memcpy (buf, eol_str, eol_str_len);
19373 buf += eol_str_len;
19376 return buf;
19379 /* Return a string for the output of a mode line %-spec for window W,
19380 generated by character C. PRECISION >= 0 means don't return a
19381 string longer than that value. FIELD_WIDTH > 0 means pad the
19382 string returned with spaces to that value. Return a Lisp string in
19383 *STRING if the resulting string is taken from that Lisp string.
19385 Note we operate on the current buffer for most purposes,
19386 the exception being w->base_line_pos. */
19388 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19390 static const char *
19391 decode_mode_spec (struct window *w, register int c, int field_width,
19392 int precision, Lisp_Object *string)
19394 Lisp_Object obj;
19395 struct frame *f = XFRAME (WINDOW_FRAME (w));
19396 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19397 struct buffer *b = current_buffer;
19399 obj = Qnil;
19400 *string = Qnil;
19402 switch (c)
19404 case '*':
19405 if (!NILP (b->read_only))
19406 return "%";
19407 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19408 return "*";
19409 return "-";
19411 case '+':
19412 /* This differs from %* only for a modified read-only buffer. */
19413 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19414 return "*";
19415 if (!NILP (b->read_only))
19416 return "%";
19417 return "-";
19419 case '&':
19420 /* This differs from %* in ignoring read-only-ness. */
19421 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19422 return "*";
19423 return "-";
19425 case '%':
19426 return "%";
19428 case '[':
19430 int i;
19431 char *p;
19433 if (command_loop_level > 5)
19434 return "[[[... ";
19435 p = decode_mode_spec_buf;
19436 for (i = 0; i < command_loop_level; i++)
19437 *p++ = '[';
19438 *p = 0;
19439 return decode_mode_spec_buf;
19442 case ']':
19444 int i;
19445 char *p;
19447 if (command_loop_level > 5)
19448 return " ...]]]";
19449 p = decode_mode_spec_buf;
19450 for (i = 0; i < command_loop_level; i++)
19451 *p++ = ']';
19452 *p = 0;
19453 return decode_mode_spec_buf;
19456 case '-':
19458 register int i;
19460 /* Let lots_of_dashes be a string of infinite length. */
19461 if (mode_line_target == MODE_LINE_NOPROP ||
19462 mode_line_target == MODE_LINE_STRING)
19463 return "--";
19464 if (field_width <= 0
19465 || field_width > sizeof (lots_of_dashes))
19467 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19468 decode_mode_spec_buf[i] = '-';
19469 decode_mode_spec_buf[i] = '\0';
19470 return decode_mode_spec_buf;
19472 else
19473 return lots_of_dashes;
19476 case 'b':
19477 obj = b->name;
19478 break;
19480 case 'c':
19481 /* %c and %l are ignored in `frame-title-format'.
19482 (In redisplay_internal, the frame title is drawn _before_ the
19483 windows are updated, so the stuff which depends on actual
19484 window contents (such as %l) may fail to render properly, or
19485 even crash emacs.) */
19486 if (mode_line_target == MODE_LINE_TITLE)
19487 return "";
19488 else
19490 int col = (int) current_column (); /* iftc */
19491 w->column_number_displayed = make_number (col);
19492 pint2str (decode_mode_spec_buf, field_width, col);
19493 return decode_mode_spec_buf;
19496 case 'e':
19497 #ifndef SYSTEM_MALLOC
19499 if (NILP (Vmemory_full))
19500 return "";
19501 else
19502 return "!MEM FULL! ";
19504 #else
19505 return "";
19506 #endif
19508 case 'F':
19509 /* %F displays the frame name. */
19510 if (!NILP (f->title))
19511 return (char *) SDATA (f->title);
19512 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19513 return (char *) SDATA (f->name);
19514 return "Emacs";
19516 case 'f':
19517 obj = b->filename;
19518 break;
19520 case 'i':
19522 EMACS_INT size = ZV - BEGV;
19523 pint2str (decode_mode_spec_buf, field_width, size);
19524 return decode_mode_spec_buf;
19527 case 'I':
19529 EMACS_INT size = ZV - BEGV;
19530 pint2hrstr (decode_mode_spec_buf, field_width, size);
19531 return decode_mode_spec_buf;
19534 case 'l':
19536 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19537 int topline, nlines, height;
19538 EMACS_INT junk;
19540 /* %c and %l are ignored in `frame-title-format'. */
19541 if (mode_line_target == MODE_LINE_TITLE)
19542 return "";
19544 startpos = XMARKER (w->start)->charpos;
19545 startpos_byte = marker_byte_position (w->start);
19546 height = WINDOW_TOTAL_LINES (w);
19548 /* If we decided that this buffer isn't suitable for line numbers,
19549 don't forget that too fast. */
19550 if (EQ (w->base_line_pos, w->buffer))
19551 goto no_value;
19552 /* But do forget it, if the window shows a different buffer now. */
19553 else if (BUFFERP (w->base_line_pos))
19554 w->base_line_pos = Qnil;
19556 /* If the buffer is very big, don't waste time. */
19557 if (INTEGERP (Vline_number_display_limit)
19558 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19560 w->base_line_pos = Qnil;
19561 w->base_line_number = Qnil;
19562 goto no_value;
19565 if (INTEGERP (w->base_line_number)
19566 && INTEGERP (w->base_line_pos)
19567 && XFASTINT (w->base_line_pos) <= startpos)
19569 line = XFASTINT (w->base_line_number);
19570 linepos = XFASTINT (w->base_line_pos);
19571 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19573 else
19575 line = 1;
19576 linepos = BUF_BEGV (b);
19577 linepos_byte = BUF_BEGV_BYTE (b);
19580 /* Count lines from base line to window start position. */
19581 nlines = display_count_lines (linepos, linepos_byte,
19582 startpos_byte,
19583 startpos, &junk);
19585 topline = nlines + line;
19587 /* Determine a new base line, if the old one is too close
19588 or too far away, or if we did not have one.
19589 "Too close" means it's plausible a scroll-down would
19590 go back past it. */
19591 if (startpos == BUF_BEGV (b))
19593 w->base_line_number = make_number (topline);
19594 w->base_line_pos = make_number (BUF_BEGV (b));
19596 else if (nlines < height + 25 || nlines > height * 3 + 50
19597 || linepos == BUF_BEGV (b))
19599 EMACS_INT limit = BUF_BEGV (b);
19600 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19601 EMACS_INT position;
19602 int distance = (height * 2 + 30) * line_number_display_limit_width;
19604 if (startpos - distance > limit)
19606 limit = startpos - distance;
19607 limit_byte = CHAR_TO_BYTE (limit);
19610 nlines = display_count_lines (startpos, startpos_byte,
19611 limit_byte,
19612 - (height * 2 + 30),
19613 &position);
19614 /* If we couldn't find the lines we wanted within
19615 line_number_display_limit_width chars per line,
19616 give up on line numbers for this window. */
19617 if (position == limit_byte && limit == startpos - distance)
19619 w->base_line_pos = w->buffer;
19620 w->base_line_number = Qnil;
19621 goto no_value;
19624 w->base_line_number = make_number (topline - nlines);
19625 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19628 /* Now count lines from the start pos to point. */
19629 nlines = display_count_lines (startpos, startpos_byte,
19630 PT_BYTE, PT, &junk);
19632 /* Record that we did display the line number. */
19633 line_number_displayed = 1;
19635 /* Make the string to show. */
19636 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19637 return decode_mode_spec_buf;
19638 no_value:
19640 char* p = decode_mode_spec_buf;
19641 int pad = field_width - 2;
19642 while (pad-- > 0)
19643 *p++ = ' ';
19644 *p++ = '?';
19645 *p++ = '?';
19646 *p = '\0';
19647 return decode_mode_spec_buf;
19650 break;
19652 case 'm':
19653 obj = b->mode_name;
19654 break;
19656 case 'n':
19657 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19658 return " Narrow";
19659 break;
19661 case 'p':
19663 EMACS_INT pos = marker_position (w->start);
19664 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19666 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19668 if (pos <= BUF_BEGV (b))
19669 return "All";
19670 else
19671 return "Bottom";
19673 else if (pos <= BUF_BEGV (b))
19674 return "Top";
19675 else
19677 if (total > 1000000)
19678 /* Do it differently for a large value, to avoid overflow. */
19679 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19680 else
19681 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19682 /* We can't normally display a 3-digit number,
19683 so get us a 2-digit number that is close. */
19684 if (total == 100)
19685 total = 99;
19686 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19687 return decode_mode_spec_buf;
19691 /* Display percentage of size above the bottom of the screen. */
19692 case 'P':
19694 EMACS_INT toppos = marker_position (w->start);
19695 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19696 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19698 if (botpos >= BUF_ZV (b))
19700 if (toppos <= BUF_BEGV (b))
19701 return "All";
19702 else
19703 return "Bottom";
19705 else
19707 if (total > 1000000)
19708 /* Do it differently for a large value, to avoid overflow. */
19709 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19710 else
19711 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19712 /* We can't normally display a 3-digit number,
19713 so get us a 2-digit number that is close. */
19714 if (total == 100)
19715 total = 99;
19716 if (toppos <= BUF_BEGV (b))
19717 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19718 else
19719 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19720 return decode_mode_spec_buf;
19724 case 's':
19725 /* status of process */
19726 obj = Fget_buffer_process (Fcurrent_buffer ());
19727 if (NILP (obj))
19728 return "no process";
19729 #ifndef MSDOS
19730 obj = Fsymbol_name (Fprocess_status (obj));
19731 #endif
19732 break;
19734 case '@':
19736 int count = inhibit_garbage_collection ();
19737 Lisp_Object val = call1 (intern ("file-remote-p"),
19738 current_buffer->directory);
19739 unbind_to (count, Qnil);
19741 if (NILP (val))
19742 return "-";
19743 else
19744 return "@";
19747 case 't': /* indicate TEXT or BINARY */
19748 #ifdef MODE_LINE_BINARY_TEXT
19749 return MODE_LINE_BINARY_TEXT (b);
19750 #else
19751 return "T";
19752 #endif
19754 case 'z':
19755 /* coding-system (not including end-of-line format) */
19756 case 'Z':
19757 /* coding-system (including end-of-line type) */
19759 int eol_flag = (c == 'Z');
19760 char *p = decode_mode_spec_buf;
19762 if (! FRAME_WINDOW_P (f))
19764 /* No need to mention EOL here--the terminal never needs
19765 to do EOL conversion. */
19766 p = decode_mode_spec_coding (CODING_ID_NAME
19767 (FRAME_KEYBOARD_CODING (f)->id),
19768 p, 0);
19769 p = decode_mode_spec_coding (CODING_ID_NAME
19770 (FRAME_TERMINAL_CODING (f)->id),
19771 p, 0);
19773 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19774 p, eol_flag);
19776 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19777 #ifdef subprocesses
19778 obj = Fget_buffer_process (Fcurrent_buffer ());
19779 if (PROCESSP (obj))
19781 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19782 p, eol_flag);
19783 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19784 p, eol_flag);
19786 #endif /* subprocesses */
19787 #endif /* 0 */
19788 *p = 0;
19789 return decode_mode_spec_buf;
19793 if (STRINGP (obj))
19795 *string = obj;
19796 return (char *) SDATA (obj);
19798 else
19799 return "";
19803 /* Count up to COUNT lines starting from START / START_BYTE.
19804 But don't go beyond LIMIT_BYTE.
19805 Return the number of lines thus found (always nonnegative).
19807 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19809 static int
19810 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19811 EMACS_INT limit_byte, int count,
19812 EMACS_INT *byte_pos_ptr)
19814 register unsigned char *cursor;
19815 unsigned char *base;
19817 register int ceiling;
19818 register unsigned char *ceiling_addr;
19819 int orig_count = count;
19821 /* If we are not in selective display mode,
19822 check only for newlines. */
19823 int selective_display = (!NILP (current_buffer->selective_display)
19824 && !INTEGERP (current_buffer->selective_display));
19826 if (count > 0)
19828 while (start_byte < limit_byte)
19830 ceiling = BUFFER_CEILING_OF (start_byte);
19831 ceiling = min (limit_byte - 1, ceiling);
19832 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19833 base = (cursor = BYTE_POS_ADDR (start_byte));
19834 while (1)
19836 if (selective_display)
19837 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19839 else
19840 while (*cursor != '\n' && ++cursor != ceiling_addr)
19843 if (cursor != ceiling_addr)
19845 if (--count == 0)
19847 start_byte += cursor - base + 1;
19848 *byte_pos_ptr = start_byte;
19849 return orig_count;
19851 else
19852 if (++cursor == ceiling_addr)
19853 break;
19855 else
19856 break;
19858 start_byte += cursor - base;
19861 else
19863 while (start_byte > limit_byte)
19865 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19866 ceiling = max (limit_byte, ceiling);
19867 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19868 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19869 while (1)
19871 if (selective_display)
19872 while (--cursor != ceiling_addr
19873 && *cursor != '\n' && *cursor != 015)
19875 else
19876 while (--cursor != ceiling_addr && *cursor != '\n')
19879 if (cursor != ceiling_addr)
19881 if (++count == 0)
19883 start_byte += cursor - base + 1;
19884 *byte_pos_ptr = start_byte;
19885 /* When scanning backwards, we should
19886 not count the newline posterior to which we stop. */
19887 return - orig_count - 1;
19890 else
19891 break;
19893 /* Here we add 1 to compensate for the last decrement
19894 of CURSOR, which took it past the valid range. */
19895 start_byte += cursor - base + 1;
19899 *byte_pos_ptr = limit_byte;
19901 if (count < 0)
19902 return - orig_count + count;
19903 return orig_count - count;
19909 /***********************************************************************
19910 Displaying strings
19911 ***********************************************************************/
19913 /* Display a NUL-terminated string, starting with index START.
19915 If STRING is non-null, display that C string. Otherwise, the Lisp
19916 string LISP_STRING is displayed. There's a case that STRING is
19917 non-null and LISP_STRING is not nil. It means STRING is a string
19918 data of LISP_STRING. In that case, we display LISP_STRING while
19919 ignoring its text properties.
19921 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19922 FACE_STRING. Display STRING or LISP_STRING with the face at
19923 FACE_STRING_POS in FACE_STRING:
19925 Display the string in the environment given by IT, but use the
19926 standard display table, temporarily.
19928 FIELD_WIDTH is the minimum number of output glyphs to produce.
19929 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19930 with spaces. If STRING has more characters, more than FIELD_WIDTH
19931 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19933 PRECISION is the maximum number of characters to output from
19934 STRING. PRECISION < 0 means don't truncate the string.
19936 This is roughly equivalent to printf format specifiers:
19938 FIELD_WIDTH PRECISION PRINTF
19939 ----------------------------------------
19940 -1 -1 %s
19941 -1 10 %.10s
19942 10 -1 %10s
19943 20 10 %20.10s
19945 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19946 display them, and < 0 means obey the current buffer's value of
19947 enable_multibyte_characters.
19949 Value is the number of columns displayed. */
19951 static int
19952 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19953 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19954 int field_width, int precision, int max_x, int multibyte)
19956 int hpos_at_start = it->hpos;
19957 int saved_face_id = it->face_id;
19958 struct glyph_row *row = it->glyph_row;
19960 /* Initialize the iterator IT for iteration over STRING beginning
19961 with index START. */
19962 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19963 precision, field_width, multibyte);
19964 if (string && STRINGP (lisp_string))
19965 /* LISP_STRING is the one returned by decode_mode_spec. We should
19966 ignore its text properties. */
19967 it->stop_charpos = -1;
19969 /* If displaying STRING, set up the face of the iterator
19970 from LISP_STRING, if that's given. */
19971 if (STRINGP (face_string))
19973 EMACS_INT endptr;
19974 struct face *face;
19976 it->face_id
19977 = face_at_string_position (it->w, face_string, face_string_pos,
19978 0, it->region_beg_charpos,
19979 it->region_end_charpos,
19980 &endptr, it->base_face_id, 0);
19981 face = FACE_FROM_ID (it->f, it->face_id);
19982 it->face_box_p = face->box != FACE_NO_BOX;
19985 /* Set max_x to the maximum allowed X position. Don't let it go
19986 beyond the right edge of the window. */
19987 if (max_x <= 0)
19988 max_x = it->last_visible_x;
19989 else
19990 max_x = min (max_x, it->last_visible_x);
19992 /* Skip over display elements that are not visible. because IT->w is
19993 hscrolled. */
19994 if (it->current_x < it->first_visible_x)
19995 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19996 MOVE_TO_POS | MOVE_TO_X);
19998 row->ascent = it->max_ascent;
19999 row->height = it->max_ascent + it->max_descent;
20000 row->phys_ascent = it->max_phys_ascent;
20001 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20002 row->extra_line_spacing = it->max_extra_line_spacing;
20004 /* This condition is for the case that we are called with current_x
20005 past last_visible_x. */
20006 while (it->current_x < max_x)
20008 int x_before, x, n_glyphs_before, i, nglyphs;
20010 /* Get the next display element. */
20011 if (!get_next_display_element (it))
20012 break;
20014 /* Produce glyphs. */
20015 x_before = it->current_x;
20016 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
20017 PRODUCE_GLYPHS (it);
20019 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
20020 i = 0;
20021 x = x_before;
20022 while (i < nglyphs)
20024 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20026 if (it->line_wrap != TRUNCATE
20027 && x + glyph->pixel_width > max_x)
20029 /* End of continued line or max_x reached. */
20030 if (CHAR_GLYPH_PADDING_P (*glyph))
20032 /* A wide character is unbreakable. */
20033 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
20034 it->current_x = x_before;
20036 else
20038 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
20039 it->current_x = x;
20041 break;
20043 else if (x + glyph->pixel_width >= it->first_visible_x)
20045 /* Glyph is at least partially visible. */
20046 ++it->hpos;
20047 if (x < it->first_visible_x)
20048 it->glyph_row->x = x - it->first_visible_x;
20050 else
20052 /* Glyph is off the left margin of the display area.
20053 Should not happen. */
20054 abort ();
20057 row->ascent = max (row->ascent, it->max_ascent);
20058 row->height = max (row->height, it->max_ascent + it->max_descent);
20059 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20060 row->phys_height = max (row->phys_height,
20061 it->max_phys_ascent + it->max_phys_descent);
20062 row->extra_line_spacing = max (row->extra_line_spacing,
20063 it->max_extra_line_spacing);
20064 x += glyph->pixel_width;
20065 ++i;
20068 /* Stop if max_x reached. */
20069 if (i < nglyphs)
20070 break;
20072 /* Stop at line ends. */
20073 if (ITERATOR_AT_END_OF_LINE_P (it))
20075 it->continuation_lines_width = 0;
20076 break;
20079 set_iterator_to_next (it, 1);
20081 /* Stop if truncating at the right edge. */
20082 if (it->line_wrap == TRUNCATE
20083 && it->current_x >= it->last_visible_x)
20085 /* Add truncation mark, but don't do it if the line is
20086 truncated at a padding space. */
20087 if (IT_CHARPOS (*it) < it->string_nchars)
20089 if (!FRAME_WINDOW_P (it->f))
20091 int i, n;
20093 if (it->current_x > it->last_visible_x)
20095 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20096 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20097 break;
20098 for (n = row->used[TEXT_AREA]; i < n; ++i)
20100 row->used[TEXT_AREA] = i;
20101 produce_special_glyphs (it, IT_TRUNCATION);
20104 produce_special_glyphs (it, IT_TRUNCATION);
20106 it->glyph_row->truncated_on_right_p = 1;
20108 break;
20112 /* Maybe insert a truncation at the left. */
20113 if (it->first_visible_x
20114 && IT_CHARPOS (*it) > 0)
20116 if (!FRAME_WINDOW_P (it->f))
20117 insert_left_trunc_glyphs (it);
20118 it->glyph_row->truncated_on_left_p = 1;
20121 it->face_id = saved_face_id;
20123 /* Value is number of columns displayed. */
20124 return it->hpos - hpos_at_start;
20129 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20130 appears as an element of LIST or as the car of an element of LIST.
20131 If PROPVAL is a list, compare each element against LIST in that
20132 way, and return 1/2 if any element of PROPVAL is found in LIST.
20133 Otherwise return 0. This function cannot quit.
20134 The return value is 2 if the text is invisible but with an ellipsis
20135 and 1 if it's invisible and without an ellipsis. */
20138 invisible_p (register Lisp_Object propval, Lisp_Object list)
20140 register Lisp_Object tail, proptail;
20142 for (tail = list; CONSP (tail); tail = XCDR (tail))
20144 register Lisp_Object tem;
20145 tem = XCAR (tail);
20146 if (EQ (propval, tem))
20147 return 1;
20148 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20149 return NILP (XCDR (tem)) ? 1 : 2;
20152 if (CONSP (propval))
20154 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20156 Lisp_Object propelt;
20157 propelt = XCAR (proptail);
20158 for (tail = list; CONSP (tail); tail = XCDR (tail))
20160 register Lisp_Object tem;
20161 tem = XCAR (tail);
20162 if (EQ (propelt, tem))
20163 return 1;
20164 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20165 return NILP (XCDR (tem)) ? 1 : 2;
20170 return 0;
20173 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20174 doc: /* Non-nil if the property makes the text invisible.
20175 POS-OR-PROP can be a marker or number, in which case it is taken to be
20176 a position in the current buffer and the value of the `invisible' property
20177 is checked; or it can be some other value, which is then presumed to be the
20178 value of the `invisible' property of the text of interest.
20179 The non-nil value returned can be t for truly invisible text or something
20180 else if the text is replaced by an ellipsis. */)
20181 (Lisp_Object pos_or_prop)
20183 Lisp_Object prop
20184 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20185 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20186 : pos_or_prop);
20187 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20188 return (invis == 0 ? Qnil
20189 : invis == 1 ? Qt
20190 : make_number (invis));
20193 /* Calculate a width or height in pixels from a specification using
20194 the following elements:
20196 SPEC ::=
20197 NUM - a (fractional) multiple of the default font width/height
20198 (NUM) - specifies exactly NUM pixels
20199 UNIT - a fixed number of pixels, see below.
20200 ELEMENT - size of a display element in pixels, see below.
20201 (NUM . SPEC) - equals NUM * SPEC
20202 (+ SPEC SPEC ...) - add pixel values
20203 (- SPEC SPEC ...) - subtract pixel values
20204 (- SPEC) - negate pixel value
20206 NUM ::=
20207 INT or FLOAT - a number constant
20208 SYMBOL - use symbol's (buffer local) variable binding.
20210 UNIT ::=
20211 in - pixels per inch *)
20212 mm - pixels per 1/1000 meter *)
20213 cm - pixels per 1/100 meter *)
20214 width - width of current font in pixels.
20215 height - height of current font in pixels.
20217 *) using the ratio(s) defined in display-pixels-per-inch.
20219 ELEMENT ::=
20221 left-fringe - left fringe width in pixels
20222 right-fringe - right fringe width in pixels
20224 left-margin - left margin width in pixels
20225 right-margin - right margin width in pixels
20227 scroll-bar - scroll-bar area width in pixels
20229 Examples:
20231 Pixels corresponding to 5 inches:
20232 (5 . in)
20234 Total width of non-text areas on left side of window (if scroll-bar is on left):
20235 '(space :width (+ left-fringe left-margin scroll-bar))
20237 Align to first text column (in header line):
20238 '(space :align-to 0)
20240 Align to middle of text area minus half the width of variable `my-image'
20241 containing a loaded image:
20242 '(space :align-to (0.5 . (- text my-image)))
20244 Width of left margin minus width of 1 character in the default font:
20245 '(space :width (- left-margin 1))
20247 Width of left margin minus width of 2 characters in the current font:
20248 '(space :width (- left-margin (2 . width)))
20250 Center 1 character over left-margin (in header line):
20251 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20253 Different ways to express width of left fringe plus left margin minus one pixel:
20254 '(space :width (- (+ left-fringe left-margin) (1)))
20255 '(space :width (+ left-fringe left-margin (- (1))))
20256 '(space :width (+ left-fringe left-margin (-1)))
20260 #define NUMVAL(X) \
20261 ((INTEGERP (X) || FLOATP (X)) \
20262 ? XFLOATINT (X) \
20263 : - 1)
20266 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20267 struct font *font, int width_p, int *align_to)
20269 double pixels;
20271 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20272 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20274 if (NILP (prop))
20275 return OK_PIXELS (0);
20277 xassert (FRAME_LIVE_P (it->f));
20279 if (SYMBOLP (prop))
20281 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20283 char *unit = SDATA (SYMBOL_NAME (prop));
20285 if (unit[0] == 'i' && unit[1] == 'n')
20286 pixels = 1.0;
20287 else if (unit[0] == 'm' && unit[1] == 'm')
20288 pixels = 25.4;
20289 else if (unit[0] == 'c' && unit[1] == 'm')
20290 pixels = 2.54;
20291 else
20292 pixels = 0;
20293 if (pixels > 0)
20295 double ppi;
20296 #ifdef HAVE_WINDOW_SYSTEM
20297 if (FRAME_WINDOW_P (it->f)
20298 && (ppi = (width_p
20299 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20300 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20301 ppi > 0))
20302 return OK_PIXELS (ppi / pixels);
20303 #endif
20305 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20306 || (CONSP (Vdisplay_pixels_per_inch)
20307 && (ppi = (width_p
20308 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20309 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20310 ppi > 0)))
20311 return OK_PIXELS (ppi / pixels);
20313 return 0;
20317 #ifdef HAVE_WINDOW_SYSTEM
20318 if (EQ (prop, Qheight))
20319 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20320 if (EQ (prop, Qwidth))
20321 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20322 #else
20323 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20324 return OK_PIXELS (1);
20325 #endif
20327 if (EQ (prop, Qtext))
20328 return OK_PIXELS (width_p
20329 ? window_box_width (it->w, TEXT_AREA)
20330 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20332 if (align_to && *align_to < 0)
20334 *res = 0;
20335 if (EQ (prop, Qleft))
20336 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20337 if (EQ (prop, Qright))
20338 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20339 if (EQ (prop, Qcenter))
20340 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20341 + window_box_width (it->w, TEXT_AREA) / 2);
20342 if (EQ (prop, Qleft_fringe))
20343 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20344 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20345 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20346 if (EQ (prop, Qright_fringe))
20347 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20348 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20349 : window_box_right_offset (it->w, TEXT_AREA));
20350 if (EQ (prop, Qleft_margin))
20351 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20352 if (EQ (prop, Qright_margin))
20353 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20354 if (EQ (prop, Qscroll_bar))
20355 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20357 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20358 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20359 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20360 : 0)));
20362 else
20364 if (EQ (prop, Qleft_fringe))
20365 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20366 if (EQ (prop, Qright_fringe))
20367 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20368 if (EQ (prop, Qleft_margin))
20369 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20370 if (EQ (prop, Qright_margin))
20371 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20372 if (EQ (prop, Qscroll_bar))
20373 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20376 prop = Fbuffer_local_value (prop, it->w->buffer);
20379 if (INTEGERP (prop) || FLOATP (prop))
20381 int base_unit = (width_p
20382 ? FRAME_COLUMN_WIDTH (it->f)
20383 : FRAME_LINE_HEIGHT (it->f));
20384 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20387 if (CONSP (prop))
20389 Lisp_Object car = XCAR (prop);
20390 Lisp_Object cdr = XCDR (prop);
20392 if (SYMBOLP (car))
20394 #ifdef HAVE_WINDOW_SYSTEM
20395 if (FRAME_WINDOW_P (it->f)
20396 && valid_image_p (prop))
20398 int id = lookup_image (it->f, prop);
20399 struct image *img = IMAGE_FROM_ID (it->f, id);
20401 return OK_PIXELS (width_p ? img->width : img->height);
20403 #endif
20404 if (EQ (car, Qplus) || EQ (car, Qminus))
20406 int first = 1;
20407 double px;
20409 pixels = 0;
20410 while (CONSP (cdr))
20412 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20413 font, width_p, align_to))
20414 return 0;
20415 if (first)
20416 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20417 else
20418 pixels += px;
20419 cdr = XCDR (cdr);
20421 if (EQ (car, Qminus))
20422 pixels = -pixels;
20423 return OK_PIXELS (pixels);
20426 car = Fbuffer_local_value (car, it->w->buffer);
20429 if (INTEGERP (car) || FLOATP (car))
20431 double fact;
20432 pixels = XFLOATINT (car);
20433 if (NILP (cdr))
20434 return OK_PIXELS (pixels);
20435 if (calc_pixel_width_or_height (&fact, it, cdr,
20436 font, width_p, align_to))
20437 return OK_PIXELS (pixels * fact);
20438 return 0;
20441 return 0;
20444 return 0;
20448 /***********************************************************************
20449 Glyph Display
20450 ***********************************************************************/
20452 #ifdef HAVE_WINDOW_SYSTEM
20454 #if GLYPH_DEBUG
20456 void
20457 dump_glyph_string (s)
20458 struct glyph_string *s;
20460 fprintf (stderr, "glyph string\n");
20461 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20462 s->x, s->y, s->width, s->height);
20463 fprintf (stderr, " ybase = %d\n", s->ybase);
20464 fprintf (stderr, " hl = %d\n", s->hl);
20465 fprintf (stderr, " left overhang = %d, right = %d\n",
20466 s->left_overhang, s->right_overhang);
20467 fprintf (stderr, " nchars = %d\n", s->nchars);
20468 fprintf (stderr, " extends to end of line = %d\n",
20469 s->extends_to_end_of_line_p);
20470 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20471 fprintf (stderr, " bg width = %d\n", s->background_width);
20474 #endif /* GLYPH_DEBUG */
20476 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20477 of XChar2b structures for S; it can't be allocated in
20478 init_glyph_string because it must be allocated via `alloca'. W
20479 is the window on which S is drawn. ROW and AREA are the glyph row
20480 and area within the row from which S is constructed. START is the
20481 index of the first glyph structure covered by S. HL is a
20482 face-override for drawing S. */
20484 #ifdef HAVE_NTGUI
20485 #define OPTIONAL_HDC(hdc) HDC hdc,
20486 #define DECLARE_HDC(hdc) HDC hdc;
20487 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20488 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20489 #endif
20491 #ifndef OPTIONAL_HDC
20492 #define OPTIONAL_HDC(hdc)
20493 #define DECLARE_HDC(hdc)
20494 #define ALLOCATE_HDC(hdc, f)
20495 #define RELEASE_HDC(hdc, f)
20496 #endif
20498 static void
20499 init_glyph_string (struct glyph_string *s,
20500 OPTIONAL_HDC (hdc)
20501 XChar2b *char2b, struct window *w, struct glyph_row *row,
20502 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20504 memset (s, 0, sizeof *s);
20505 s->w = w;
20506 s->f = XFRAME (w->frame);
20507 #ifdef HAVE_NTGUI
20508 s->hdc = hdc;
20509 #endif
20510 s->display = FRAME_X_DISPLAY (s->f);
20511 s->window = FRAME_X_WINDOW (s->f);
20512 s->char2b = char2b;
20513 s->hl = hl;
20514 s->row = row;
20515 s->area = area;
20516 s->first_glyph = row->glyphs[area] + start;
20517 s->height = row->height;
20518 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20519 s->ybase = s->y + row->ascent;
20523 /* Append the list of glyph strings with head H and tail T to the list
20524 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20526 static INLINE void
20527 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20528 struct glyph_string *h, struct glyph_string *t)
20530 if (h)
20532 if (*head)
20533 (*tail)->next = h;
20534 else
20535 *head = h;
20536 h->prev = *tail;
20537 *tail = t;
20542 /* Prepend the list of glyph strings with head H and tail T to the
20543 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20544 result. */
20546 static INLINE void
20547 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20548 struct glyph_string *h, struct glyph_string *t)
20550 if (h)
20552 if (*head)
20553 (*head)->prev = t;
20554 else
20555 *tail = t;
20556 t->next = *head;
20557 *head = h;
20562 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20563 Set *HEAD and *TAIL to the resulting list. */
20565 static INLINE void
20566 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20567 struct glyph_string *s)
20569 s->next = s->prev = NULL;
20570 append_glyph_string_lists (head, tail, s, s);
20574 /* Get face and two-byte form of character C in face FACE_ID on frame
20575 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20576 means we want to display multibyte text. DISPLAY_P non-zero means
20577 make sure that X resources for the face returned are allocated.
20578 Value is a pointer to a realized face that is ready for display if
20579 DISPLAY_P is non-zero. */
20581 static INLINE struct face *
20582 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20583 XChar2b *char2b, int multibyte_p, int display_p)
20585 struct face *face = FACE_FROM_ID (f, face_id);
20587 if (face->font)
20589 unsigned code = face->font->driver->encode_char (face->font, c);
20591 if (code != FONT_INVALID_CODE)
20592 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20593 else
20594 STORE_XCHAR2B (char2b, 0, 0);
20597 /* Make sure X resources of the face are allocated. */
20598 #ifdef HAVE_X_WINDOWS
20599 if (display_p)
20600 #endif
20602 xassert (face != NULL);
20603 PREPARE_FACE_FOR_DISPLAY (f, face);
20606 return face;
20610 /* Get face and two-byte form of character glyph GLYPH on frame F.
20611 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20612 a pointer to a realized face that is ready for display. */
20614 static INLINE struct face *
20615 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20616 XChar2b *char2b, int *two_byte_p)
20618 struct face *face;
20620 xassert (glyph->type == CHAR_GLYPH);
20621 face = FACE_FROM_ID (f, glyph->face_id);
20623 if (two_byte_p)
20624 *two_byte_p = 0;
20626 if (face->font)
20628 unsigned code;
20630 if (CHAR_BYTE8_P (glyph->u.ch))
20631 code = CHAR_TO_BYTE8 (glyph->u.ch);
20632 else
20633 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20635 if (code != FONT_INVALID_CODE)
20636 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20637 else
20638 STORE_XCHAR2B (char2b, 0, 0);
20641 /* Make sure X resources of the face are allocated. */
20642 xassert (face != NULL);
20643 PREPARE_FACE_FOR_DISPLAY (f, face);
20644 return face;
20648 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20649 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20651 static INLINE int
20652 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20654 unsigned code;
20656 if (CHAR_BYTE8_P (c))
20657 code = CHAR_TO_BYTE8 (c);
20658 else
20659 code = font->driver->encode_char (font, c);
20661 if (code == FONT_INVALID_CODE)
20662 return 0;
20663 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20664 return 1;
20668 /* Fill glyph string S with composition components specified by S->cmp.
20670 BASE_FACE is the base face of the composition.
20671 S->cmp_from is the index of the first component for S.
20673 OVERLAPS non-zero means S should draw the foreground only, and use
20674 its physical height for clipping. See also draw_glyphs.
20676 Value is the index of a component not in S. */
20678 static int
20679 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20680 int overlaps)
20682 int i;
20683 /* For all glyphs of this composition, starting at the offset
20684 S->cmp_from, until we reach the end of the definition or encounter a
20685 glyph that requires the different face, add it to S. */
20686 struct face *face;
20688 xassert (s);
20690 s->for_overlaps = overlaps;
20691 s->face = NULL;
20692 s->font = NULL;
20693 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20695 int c = COMPOSITION_GLYPH (s->cmp, i);
20697 if (c != '\t')
20699 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20700 -1, Qnil);
20702 face = get_char_face_and_encoding (s->f, c, face_id,
20703 s->char2b + i, 1, 1);
20704 if (face)
20706 if (! s->face)
20708 s->face = face;
20709 s->font = s->face->font;
20711 else if (s->face != face)
20712 break;
20715 ++s->nchars;
20717 s->cmp_to = i;
20719 /* All glyph strings for the same composition has the same width,
20720 i.e. the width set for the first component of the composition. */
20721 s->width = s->first_glyph->pixel_width;
20723 /* If the specified font could not be loaded, use the frame's
20724 default font, but record the fact that we couldn't load it in
20725 the glyph string so that we can draw rectangles for the
20726 characters of the glyph string. */
20727 if (s->font == NULL)
20729 s->font_not_found_p = 1;
20730 s->font = FRAME_FONT (s->f);
20733 /* Adjust base line for subscript/superscript text. */
20734 s->ybase += s->first_glyph->voffset;
20736 /* This glyph string must always be drawn with 16-bit functions. */
20737 s->two_byte_p = 1;
20739 return s->cmp_to;
20742 static int
20743 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20744 int start, int end, int overlaps)
20746 struct glyph *glyph, *last;
20747 Lisp_Object lgstring;
20748 int i;
20750 s->for_overlaps = overlaps;
20751 glyph = s->row->glyphs[s->area] + start;
20752 last = s->row->glyphs[s->area] + end;
20753 s->cmp_id = glyph->u.cmp.id;
20754 s->cmp_from = glyph->slice.cmp.from;
20755 s->cmp_to = glyph->slice.cmp.to + 1;
20756 s->face = FACE_FROM_ID (s->f, face_id);
20757 lgstring = composition_gstring_from_id (s->cmp_id);
20758 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20759 glyph++;
20760 while (glyph < last
20761 && glyph->u.cmp.automatic
20762 && glyph->u.cmp.id == s->cmp_id
20763 && s->cmp_to == glyph->slice.cmp.from)
20764 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20766 for (i = s->cmp_from; i < s->cmp_to; i++)
20768 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20769 unsigned code = LGLYPH_CODE (lglyph);
20771 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20773 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20774 return glyph - s->row->glyphs[s->area];
20778 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20779 See the comment of fill_glyph_string for arguments.
20780 Value is the index of the first glyph not in S. */
20783 static int
20784 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20785 int start, int end, int overlaps)
20787 struct glyph *glyph, *last;
20788 int voffset;
20790 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20791 s->for_overlaps = overlaps;
20792 glyph = s->row->glyphs[s->area] + start;
20793 last = s->row->glyphs[s->area] + end;
20794 voffset = glyph->voffset;
20795 s->face = FACE_FROM_ID (s->f, face_id);
20796 s->font = s->face->font;
20797 s->nchars = 1;
20798 s->width = glyph->pixel_width;
20799 glyph++;
20800 while (glyph < last
20801 && glyph->type == GLYPHLESS_GLYPH
20802 && glyph->voffset == voffset
20803 && glyph->face_id == face_id)
20805 s->nchars++;
20806 s->width += glyph->pixel_width;
20807 glyph++;
20809 s->ybase += voffset;
20810 return glyph - s->row->glyphs[s->area];
20814 /* Fill glyph string S from a sequence of character glyphs.
20816 FACE_ID is the face id of the string. START is the index of the
20817 first glyph to consider, END is the index of the last + 1.
20818 OVERLAPS non-zero means S should draw the foreground only, and use
20819 its physical height for clipping. See also draw_glyphs.
20821 Value is the index of the first glyph not in S. */
20823 static int
20824 fill_glyph_string (struct glyph_string *s, int face_id,
20825 int start, int end, int overlaps)
20827 struct glyph *glyph, *last;
20828 int voffset;
20829 int glyph_not_available_p;
20831 xassert (s->f == XFRAME (s->w->frame));
20832 xassert (s->nchars == 0);
20833 xassert (start >= 0 && end > start);
20835 s->for_overlaps = overlaps;
20836 glyph = s->row->glyphs[s->area] + start;
20837 last = s->row->glyphs[s->area] + end;
20838 voffset = glyph->voffset;
20839 s->padding_p = glyph->padding_p;
20840 glyph_not_available_p = glyph->glyph_not_available_p;
20842 while (glyph < last
20843 && glyph->type == CHAR_GLYPH
20844 && glyph->voffset == voffset
20845 /* Same face id implies same font, nowadays. */
20846 && glyph->face_id == face_id
20847 && glyph->glyph_not_available_p == glyph_not_available_p)
20849 int two_byte_p;
20851 s->face = get_glyph_face_and_encoding (s->f, glyph,
20852 s->char2b + s->nchars,
20853 &two_byte_p);
20854 s->two_byte_p = two_byte_p;
20855 ++s->nchars;
20856 xassert (s->nchars <= end - start);
20857 s->width += glyph->pixel_width;
20858 if (glyph++->padding_p != s->padding_p)
20859 break;
20862 s->font = s->face->font;
20864 /* If the specified font could not be loaded, use the frame's font,
20865 but record the fact that we couldn't load it in
20866 S->font_not_found_p so that we can draw rectangles for the
20867 characters of the glyph string. */
20868 if (s->font == NULL || glyph_not_available_p)
20870 s->font_not_found_p = 1;
20871 s->font = FRAME_FONT (s->f);
20874 /* Adjust base line for subscript/superscript text. */
20875 s->ybase += voffset;
20877 xassert (s->face && s->face->gc);
20878 return glyph - s->row->glyphs[s->area];
20882 /* Fill glyph string S from image glyph S->first_glyph. */
20884 static void
20885 fill_image_glyph_string (struct glyph_string *s)
20887 xassert (s->first_glyph->type == IMAGE_GLYPH);
20888 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20889 xassert (s->img);
20890 s->slice = s->first_glyph->slice.img;
20891 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20892 s->font = s->face->font;
20893 s->width = s->first_glyph->pixel_width;
20895 /* Adjust base line for subscript/superscript text. */
20896 s->ybase += s->first_glyph->voffset;
20900 /* Fill glyph string S from a sequence of stretch glyphs.
20902 ROW is the glyph row in which the glyphs are found, AREA is the
20903 area within the row. START is the index of the first glyph to
20904 consider, END is the index of the last + 1.
20906 Value is the index of the first glyph not in S. */
20908 static int
20909 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20910 enum glyph_row_area area, int start, int end)
20912 struct glyph *glyph, *last;
20913 int voffset, face_id;
20915 xassert (s->first_glyph->type == STRETCH_GLYPH);
20917 glyph = s->row->glyphs[s->area] + start;
20918 last = s->row->glyphs[s->area] + end;
20919 face_id = glyph->face_id;
20920 s->face = FACE_FROM_ID (s->f, face_id);
20921 s->font = s->face->font;
20922 s->width = glyph->pixel_width;
20923 s->nchars = 1;
20924 voffset = glyph->voffset;
20926 for (++glyph;
20927 (glyph < last
20928 && glyph->type == STRETCH_GLYPH
20929 && glyph->voffset == voffset
20930 && glyph->face_id == face_id);
20931 ++glyph)
20932 s->width += glyph->pixel_width;
20934 /* Adjust base line for subscript/superscript text. */
20935 s->ybase += voffset;
20937 /* The case that face->gc == 0 is handled when drawing the glyph
20938 string by calling PREPARE_FACE_FOR_DISPLAY. */
20939 xassert (s->face);
20940 return glyph - s->row->glyphs[s->area];
20943 static struct font_metrics *
20944 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20946 static struct font_metrics metrics;
20947 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20949 if (! font || code == FONT_INVALID_CODE)
20950 return NULL;
20951 font->driver->text_extents (font, &code, 1, &metrics);
20952 return &metrics;
20955 /* EXPORT for RIF:
20956 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20957 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20958 assumed to be zero. */
20960 void
20961 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20963 *left = *right = 0;
20965 if (glyph->type == CHAR_GLYPH)
20967 struct face *face;
20968 XChar2b char2b;
20969 struct font_metrics *pcm;
20971 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20972 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20974 if (pcm->rbearing > pcm->width)
20975 *right = pcm->rbearing - pcm->width;
20976 if (pcm->lbearing < 0)
20977 *left = -pcm->lbearing;
20980 else if (glyph->type == COMPOSITE_GLYPH)
20982 if (! glyph->u.cmp.automatic)
20984 struct composition *cmp = composition_table[glyph->u.cmp.id];
20986 if (cmp->rbearing > cmp->pixel_width)
20987 *right = cmp->rbearing - cmp->pixel_width;
20988 if (cmp->lbearing < 0)
20989 *left = - cmp->lbearing;
20991 else
20993 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20994 struct font_metrics metrics;
20996 composition_gstring_width (gstring, glyph->slice.cmp.from,
20997 glyph->slice.cmp.to + 1, &metrics);
20998 if (metrics.rbearing > metrics.width)
20999 *right = metrics.rbearing - metrics.width;
21000 if (metrics.lbearing < 0)
21001 *left = - metrics.lbearing;
21007 /* Return the index of the first glyph preceding glyph string S that
21008 is overwritten by S because of S's left overhang. Value is -1
21009 if no glyphs are overwritten. */
21011 static int
21012 left_overwritten (struct glyph_string *s)
21014 int k;
21016 if (s->left_overhang)
21018 int x = 0, i;
21019 struct glyph *glyphs = s->row->glyphs[s->area];
21020 int first = s->first_glyph - glyphs;
21022 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21023 x -= glyphs[i].pixel_width;
21025 k = i + 1;
21027 else
21028 k = -1;
21030 return k;
21034 /* Return the index of the first glyph preceding glyph string S that
21035 is overwriting S because of its right overhang. Value is -1 if no
21036 glyph in front of S overwrites S. */
21038 static int
21039 left_overwriting (struct glyph_string *s)
21041 int i, k, x;
21042 struct glyph *glyphs = s->row->glyphs[s->area];
21043 int first = s->first_glyph - glyphs;
21045 k = -1;
21046 x = 0;
21047 for (i = first - 1; i >= 0; --i)
21049 int left, right;
21050 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21051 if (x + right > 0)
21052 k = i;
21053 x -= glyphs[i].pixel_width;
21056 return k;
21060 /* Return the index of the last glyph following glyph string S that is
21061 overwritten by S because of S's right overhang. Value is -1 if
21062 no such glyph is found. */
21064 static int
21065 right_overwritten (struct glyph_string *s)
21067 int k = -1;
21069 if (s->right_overhang)
21071 int x = 0, i;
21072 struct glyph *glyphs = s->row->glyphs[s->area];
21073 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21074 int end = s->row->used[s->area];
21076 for (i = first; i < end && s->right_overhang > x; ++i)
21077 x += glyphs[i].pixel_width;
21079 k = i;
21082 return k;
21086 /* Return the index of the last glyph following glyph string S that
21087 overwrites S because of its left overhang. Value is negative
21088 if no such glyph is found. */
21090 static int
21091 right_overwriting (struct glyph_string *s)
21093 int i, k, x;
21094 int end = s->row->used[s->area];
21095 struct glyph *glyphs = s->row->glyphs[s->area];
21096 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21098 k = -1;
21099 x = 0;
21100 for (i = first; i < end; ++i)
21102 int left, right;
21103 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21104 if (x - left < 0)
21105 k = i;
21106 x += glyphs[i].pixel_width;
21109 return k;
21113 /* Set background width of glyph string S. START is the index of the
21114 first glyph following S. LAST_X is the right-most x-position + 1
21115 in the drawing area. */
21117 static INLINE void
21118 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21120 /* If the face of this glyph string has to be drawn to the end of
21121 the drawing area, set S->extends_to_end_of_line_p. */
21123 if (start == s->row->used[s->area]
21124 && s->area == TEXT_AREA
21125 && ((s->row->fill_line_p
21126 && (s->hl == DRAW_NORMAL_TEXT
21127 || s->hl == DRAW_IMAGE_RAISED
21128 || s->hl == DRAW_IMAGE_SUNKEN))
21129 || s->hl == DRAW_MOUSE_FACE))
21130 s->extends_to_end_of_line_p = 1;
21132 /* If S extends its face to the end of the line, set its
21133 background_width to the distance to the right edge of the drawing
21134 area. */
21135 if (s->extends_to_end_of_line_p)
21136 s->background_width = last_x - s->x + 1;
21137 else
21138 s->background_width = s->width;
21142 /* Compute overhangs and x-positions for glyph string S and its
21143 predecessors, or successors. X is the starting x-position for S.
21144 BACKWARD_P non-zero means process predecessors. */
21146 static void
21147 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21149 if (backward_p)
21151 while (s)
21153 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21154 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21155 x -= s->width;
21156 s->x = x;
21157 s = s->prev;
21160 else
21162 while (s)
21164 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21165 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21166 s->x = x;
21167 x += s->width;
21168 s = s->next;
21175 /* The following macros are only called from draw_glyphs below.
21176 They reference the following parameters of that function directly:
21177 `w', `row', `area', and `overlap_p'
21178 as well as the following local variables:
21179 `s', `f', and `hdc' (in W32) */
21181 #ifdef HAVE_NTGUI
21182 /* On W32, silently add local `hdc' variable to argument list of
21183 init_glyph_string. */
21184 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21185 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21186 #else
21187 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21188 init_glyph_string (s, char2b, w, row, area, start, hl)
21189 #endif
21191 /* Add a glyph string for a stretch glyph to the list of strings
21192 between HEAD and TAIL. START is the index of the stretch glyph in
21193 row area AREA of glyph row ROW. END is the index of the last glyph
21194 in that glyph row area. X is the current output position assigned
21195 to the new glyph string constructed. HL overrides that face of the
21196 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21197 is the right-most x-position of the drawing area. */
21199 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21200 and below -- keep them on one line. */
21201 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21202 do \
21204 s = (struct glyph_string *) alloca (sizeof *s); \
21205 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21206 START = fill_stretch_glyph_string (s, row, area, START, END); \
21207 append_glyph_string (&HEAD, &TAIL, s); \
21208 s->x = (X); \
21210 while (0)
21213 /* Add a glyph string for an image glyph to the list of strings
21214 between HEAD and TAIL. START is the index of the image glyph in
21215 row area AREA of glyph row ROW. END is the index of the last glyph
21216 in that glyph row area. X is the current output position assigned
21217 to the new glyph string constructed. HL overrides that face of the
21218 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21219 is the right-most x-position of the drawing area. */
21221 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21222 do \
21224 s = (struct glyph_string *) alloca (sizeof *s); \
21225 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21226 fill_image_glyph_string (s); \
21227 append_glyph_string (&HEAD, &TAIL, s); \
21228 ++START; \
21229 s->x = (X); \
21231 while (0)
21234 /* Add a glyph string for a sequence of character glyphs to the list
21235 of strings between HEAD and TAIL. START is the index of the first
21236 glyph in row area AREA of glyph row ROW that is part of the new
21237 glyph string. END is the index of the last glyph in that glyph row
21238 area. X is the current output position assigned to the new glyph
21239 string constructed. HL overrides that face of the glyph; e.g. it
21240 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21241 right-most x-position of the drawing area. */
21243 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21244 do \
21246 int face_id; \
21247 XChar2b *char2b; \
21249 face_id = (row)->glyphs[area][START].face_id; \
21251 s = (struct glyph_string *) alloca (sizeof *s); \
21252 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21253 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21254 append_glyph_string (&HEAD, &TAIL, s); \
21255 s->x = (X); \
21256 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21258 while (0)
21261 /* Add a glyph string for a composite sequence to the list of strings
21262 between HEAD and TAIL. START is the index of the first glyph in
21263 row area AREA of glyph row ROW that is part of the new glyph
21264 string. END is the index of the last glyph in that glyph row area.
21265 X is the current output position assigned to the new glyph string
21266 constructed. HL overrides that face of the glyph; e.g. it is
21267 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21268 x-position of the drawing area. */
21270 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21271 do { \
21272 int face_id = (row)->glyphs[area][START].face_id; \
21273 struct face *base_face = FACE_FROM_ID (f, face_id); \
21274 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21275 struct composition *cmp = composition_table[cmp_id]; \
21276 XChar2b *char2b; \
21277 struct glyph_string *first_s; \
21278 int n; \
21280 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21282 /* Make glyph_strings for each glyph sequence that is drawable by \
21283 the same face, and append them to HEAD/TAIL. */ \
21284 for (n = 0; n < cmp->glyph_len;) \
21286 s = (struct glyph_string *) alloca (sizeof *s); \
21287 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21288 append_glyph_string (&(HEAD), &(TAIL), s); \
21289 s->cmp = cmp; \
21290 s->cmp_from = n; \
21291 s->x = (X); \
21292 if (n == 0) \
21293 first_s = s; \
21294 n = fill_composite_glyph_string (s, base_face, overlaps); \
21297 ++START; \
21298 s = first_s; \
21299 } while (0)
21302 /* Add a glyph string for a glyph-string sequence to the list of strings
21303 between HEAD and TAIL. */
21305 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21306 do { \
21307 int face_id; \
21308 XChar2b *char2b; \
21309 Lisp_Object gstring; \
21311 face_id = (row)->glyphs[area][START].face_id; \
21312 gstring = (composition_gstring_from_id \
21313 ((row)->glyphs[area][START].u.cmp.id)); \
21314 s = (struct glyph_string *) alloca (sizeof *s); \
21315 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21316 * LGSTRING_GLYPH_LEN (gstring)); \
21317 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21318 append_glyph_string (&(HEAD), &(TAIL), s); \
21319 s->x = (X); \
21320 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21321 } while (0)
21324 /* Add a glyph string for a sequence of glyphless character's glyphs
21325 to the list of strings between HEAD and TAIL. The meanings of
21326 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21328 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21329 do \
21331 int face_id; \
21332 XChar2b *char2b; \
21334 face_id = (row)->glyphs[area][START].face_id; \
21336 s = (struct glyph_string *) alloca (sizeof *s); \
21337 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21338 append_glyph_string (&HEAD, &TAIL, s); \
21339 s->x = (X); \
21340 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21341 overlaps); \
21343 while (0)
21346 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21347 of AREA of glyph row ROW on window W between indices START and END.
21348 HL overrides the face for drawing glyph strings, e.g. it is
21349 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21350 x-positions of the drawing area.
21352 This is an ugly monster macro construct because we must use alloca
21353 to allocate glyph strings (because draw_glyphs can be called
21354 asynchronously). */
21356 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21357 do \
21359 HEAD = TAIL = NULL; \
21360 while (START < END) \
21362 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21363 switch (first_glyph->type) \
21365 case CHAR_GLYPH: \
21366 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21367 HL, X, LAST_X); \
21368 break; \
21370 case COMPOSITE_GLYPH: \
21371 if (first_glyph->u.cmp.automatic) \
21372 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21373 HL, X, LAST_X); \
21374 else \
21375 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21376 HL, X, LAST_X); \
21377 break; \
21379 case STRETCH_GLYPH: \
21380 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21381 HL, X, LAST_X); \
21382 break; \
21384 case IMAGE_GLYPH: \
21385 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21386 HL, X, LAST_X); \
21387 break; \
21389 case GLYPHLESS_GLYPH: \
21390 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21391 HL, X, LAST_X); \
21392 break; \
21394 default: \
21395 abort (); \
21398 if (s) \
21400 set_glyph_string_background_width (s, START, LAST_X); \
21401 (X) += s->width; \
21404 } while (0)
21407 /* Draw glyphs between START and END in AREA of ROW on window W,
21408 starting at x-position X. X is relative to AREA in W. HL is a
21409 face-override with the following meaning:
21411 DRAW_NORMAL_TEXT draw normally
21412 DRAW_CURSOR draw in cursor face
21413 DRAW_MOUSE_FACE draw in mouse face.
21414 DRAW_INVERSE_VIDEO draw in mode line face
21415 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21416 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21418 If OVERLAPS is non-zero, draw only the foreground of characters and
21419 clip to the physical height of ROW. Non-zero value also defines
21420 the overlapping part to be drawn:
21422 OVERLAPS_PRED overlap with preceding rows
21423 OVERLAPS_SUCC overlap with succeeding rows
21424 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21425 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21427 Value is the x-position reached, relative to AREA of W. */
21429 static int
21430 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21431 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21432 enum draw_glyphs_face hl, int overlaps)
21434 struct glyph_string *head, *tail;
21435 struct glyph_string *s;
21436 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21437 int i, j, x_reached, last_x, area_left = 0;
21438 struct frame *f = XFRAME (WINDOW_FRAME (w));
21439 DECLARE_HDC (hdc);
21441 ALLOCATE_HDC (hdc, f);
21443 /* Let's rather be paranoid than getting a SEGV. */
21444 end = min (end, row->used[area]);
21445 start = max (0, start);
21446 start = min (end, start);
21448 /* Translate X to frame coordinates. Set last_x to the right
21449 end of the drawing area. */
21450 if (row->full_width_p)
21452 /* X is relative to the left edge of W, without scroll bars
21453 or fringes. */
21454 area_left = WINDOW_LEFT_EDGE_X (w);
21455 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21457 else
21459 area_left = window_box_left (w, area);
21460 last_x = area_left + window_box_width (w, area);
21462 x += area_left;
21464 /* Build a doubly-linked list of glyph_string structures between
21465 head and tail from what we have to draw. Note that the macro
21466 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21467 the reason we use a separate variable `i'. */
21468 i = start;
21469 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21470 if (tail)
21471 x_reached = tail->x + tail->background_width;
21472 else
21473 x_reached = x;
21475 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21476 the row, redraw some glyphs in front or following the glyph
21477 strings built above. */
21478 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21480 struct glyph_string *h, *t;
21481 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21482 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21483 int dummy_x = 0;
21485 /* If mouse highlighting is on, we may need to draw adjacent
21486 glyphs using mouse-face highlighting. */
21487 if (area == TEXT_AREA && row->mouse_face_p)
21489 struct glyph_row *mouse_beg_row, *mouse_end_row;
21491 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21492 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21494 if (row >= mouse_beg_row && row <= mouse_end_row)
21496 check_mouse_face = 1;
21497 mouse_beg_col = (row == mouse_beg_row)
21498 ? hlinfo->mouse_face_beg_col : 0;
21499 mouse_end_col = (row == mouse_end_row)
21500 ? hlinfo->mouse_face_end_col
21501 : row->used[TEXT_AREA];
21505 /* Compute overhangs for all glyph strings. */
21506 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21507 for (s = head; s; s = s->next)
21508 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21510 /* Prepend glyph strings for glyphs in front of the first glyph
21511 string that are overwritten because of the first glyph
21512 string's left overhang. The background of all strings
21513 prepended must be drawn because the first glyph string
21514 draws over it. */
21515 i = left_overwritten (head);
21516 if (i >= 0)
21518 enum draw_glyphs_face overlap_hl;
21520 /* If this row contains mouse highlighting, attempt to draw
21521 the overlapped glyphs with the correct highlight. This
21522 code fails if the overlap encompasses more than one glyph
21523 and mouse-highlight spans only some of these glyphs.
21524 However, making it work perfectly involves a lot more
21525 code, and I don't know if the pathological case occurs in
21526 practice, so we'll stick to this for now. --- cyd */
21527 if (check_mouse_face
21528 && mouse_beg_col < start && mouse_end_col > i)
21529 overlap_hl = DRAW_MOUSE_FACE;
21530 else
21531 overlap_hl = DRAW_NORMAL_TEXT;
21533 j = i;
21534 BUILD_GLYPH_STRINGS (j, start, h, t,
21535 overlap_hl, dummy_x, last_x);
21536 start = i;
21537 compute_overhangs_and_x (t, head->x, 1);
21538 prepend_glyph_string_lists (&head, &tail, h, t);
21539 clip_head = head;
21542 /* Prepend glyph strings for glyphs in front of the first glyph
21543 string that overwrite that glyph string because of their
21544 right overhang. For these strings, only the foreground must
21545 be drawn, because it draws over the glyph string at `head'.
21546 The background must not be drawn because this would overwrite
21547 right overhangs of preceding glyphs for which no glyph
21548 strings exist. */
21549 i = left_overwriting (head);
21550 if (i >= 0)
21552 enum draw_glyphs_face overlap_hl;
21554 if (check_mouse_face
21555 && mouse_beg_col < start && mouse_end_col > i)
21556 overlap_hl = DRAW_MOUSE_FACE;
21557 else
21558 overlap_hl = DRAW_NORMAL_TEXT;
21560 clip_head = head;
21561 BUILD_GLYPH_STRINGS (i, start, h, t,
21562 overlap_hl, dummy_x, last_x);
21563 for (s = h; s; s = s->next)
21564 s->background_filled_p = 1;
21565 compute_overhangs_and_x (t, head->x, 1);
21566 prepend_glyph_string_lists (&head, &tail, h, t);
21569 /* Append glyphs strings for glyphs following the last glyph
21570 string tail that are overwritten by tail. The background of
21571 these strings has to be drawn because tail's foreground draws
21572 over it. */
21573 i = right_overwritten (tail);
21574 if (i >= 0)
21576 enum draw_glyphs_face overlap_hl;
21578 if (check_mouse_face
21579 && mouse_beg_col < i && mouse_end_col > end)
21580 overlap_hl = DRAW_MOUSE_FACE;
21581 else
21582 overlap_hl = DRAW_NORMAL_TEXT;
21584 BUILD_GLYPH_STRINGS (end, i, h, t,
21585 overlap_hl, x, last_x);
21586 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21587 we don't have `end = i;' here. */
21588 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21589 append_glyph_string_lists (&head, &tail, h, t);
21590 clip_tail = tail;
21593 /* Append glyph strings for glyphs following the last glyph
21594 string tail that overwrite tail. The foreground of such
21595 glyphs has to be drawn because it writes into the background
21596 of tail. The background must not be drawn because it could
21597 paint over the foreground of following glyphs. */
21598 i = right_overwriting (tail);
21599 if (i >= 0)
21601 enum draw_glyphs_face overlap_hl;
21602 if (check_mouse_face
21603 && mouse_beg_col < i && mouse_end_col > end)
21604 overlap_hl = DRAW_MOUSE_FACE;
21605 else
21606 overlap_hl = DRAW_NORMAL_TEXT;
21608 clip_tail = tail;
21609 i++; /* We must include the Ith glyph. */
21610 BUILD_GLYPH_STRINGS (end, i, h, t,
21611 overlap_hl, x, last_x);
21612 for (s = h; s; s = s->next)
21613 s->background_filled_p = 1;
21614 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21615 append_glyph_string_lists (&head, &tail, h, t);
21617 if (clip_head || clip_tail)
21618 for (s = head; s; s = s->next)
21620 s->clip_head = clip_head;
21621 s->clip_tail = clip_tail;
21625 /* Draw all strings. */
21626 for (s = head; s; s = s->next)
21627 FRAME_RIF (f)->draw_glyph_string (s);
21629 #ifndef HAVE_NS
21630 /* When focus a sole frame and move horizontally, this sets on_p to 0
21631 causing a failure to erase prev cursor position. */
21632 if (area == TEXT_AREA
21633 && !row->full_width_p
21634 /* When drawing overlapping rows, only the glyph strings'
21635 foreground is drawn, which doesn't erase a cursor
21636 completely. */
21637 && !overlaps)
21639 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21640 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21641 : (tail ? tail->x + tail->background_width : x));
21642 x0 -= area_left;
21643 x1 -= area_left;
21645 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21646 row->y, MATRIX_ROW_BOTTOM_Y (row));
21648 #endif
21650 /* Value is the x-position up to which drawn, relative to AREA of W.
21651 This doesn't include parts drawn because of overhangs. */
21652 if (row->full_width_p)
21653 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21654 else
21655 x_reached -= area_left;
21657 RELEASE_HDC (hdc, f);
21659 return x_reached;
21662 /* Expand row matrix if too narrow. Don't expand if area
21663 is not present. */
21665 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21667 if (!fonts_changed_p \
21668 && (it->glyph_row->glyphs[area] \
21669 < it->glyph_row->glyphs[area + 1])) \
21671 it->w->ncols_scale_factor++; \
21672 fonts_changed_p = 1; \
21676 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21677 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21679 static INLINE void
21680 append_glyph (struct it *it)
21682 struct glyph *glyph;
21683 enum glyph_row_area area = it->area;
21685 xassert (it->glyph_row);
21686 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21688 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21689 if (glyph < it->glyph_row->glyphs[area + 1])
21691 /* If the glyph row is reversed, we need to prepend the glyph
21692 rather than append it. */
21693 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21695 struct glyph *g;
21697 /* Make room for the additional glyph. */
21698 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21699 g[1] = *g;
21700 glyph = it->glyph_row->glyphs[area];
21702 glyph->charpos = CHARPOS (it->position);
21703 glyph->object = it->object;
21704 if (it->pixel_width > 0)
21706 glyph->pixel_width = it->pixel_width;
21707 glyph->padding_p = 0;
21709 else
21711 /* Assure at least 1-pixel width. Otherwise, cursor can't
21712 be displayed correctly. */
21713 glyph->pixel_width = 1;
21714 glyph->padding_p = 1;
21716 glyph->ascent = it->ascent;
21717 glyph->descent = it->descent;
21718 glyph->voffset = it->voffset;
21719 glyph->type = CHAR_GLYPH;
21720 glyph->avoid_cursor_p = it->avoid_cursor_p;
21721 glyph->multibyte_p = it->multibyte_p;
21722 glyph->left_box_line_p = it->start_of_box_run_p;
21723 glyph->right_box_line_p = it->end_of_box_run_p;
21724 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21725 || it->phys_descent > it->descent);
21726 glyph->glyph_not_available_p = it->glyph_not_available_p;
21727 glyph->face_id = it->face_id;
21728 glyph->u.ch = it->char_to_display;
21729 glyph->slice.img = null_glyph_slice;
21730 glyph->font_type = FONT_TYPE_UNKNOWN;
21731 if (it->bidi_p)
21733 glyph->resolved_level = it->bidi_it.resolved_level;
21734 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21735 abort ();
21736 glyph->bidi_type = it->bidi_it.type;
21738 else
21740 glyph->resolved_level = 0;
21741 glyph->bidi_type = UNKNOWN_BT;
21743 ++it->glyph_row->used[area];
21745 else
21746 IT_EXPAND_MATRIX_WIDTH (it, area);
21749 /* Store one glyph for the composition IT->cmp_it.id in
21750 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21751 non-null. */
21753 static INLINE void
21754 append_composite_glyph (struct it *it)
21756 struct glyph *glyph;
21757 enum glyph_row_area area = it->area;
21759 xassert (it->glyph_row);
21761 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21762 if (glyph < it->glyph_row->glyphs[area + 1])
21764 /* If the glyph row is reversed, we need to prepend the glyph
21765 rather than append it. */
21766 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21768 struct glyph *g;
21770 /* Make room for the new glyph. */
21771 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21772 g[1] = *g;
21773 glyph = it->glyph_row->glyphs[it->area];
21775 glyph->charpos = it->cmp_it.charpos;
21776 glyph->object = it->object;
21777 glyph->pixel_width = it->pixel_width;
21778 glyph->ascent = it->ascent;
21779 glyph->descent = it->descent;
21780 glyph->voffset = it->voffset;
21781 glyph->type = COMPOSITE_GLYPH;
21782 if (it->cmp_it.ch < 0)
21784 glyph->u.cmp.automatic = 0;
21785 glyph->u.cmp.id = it->cmp_it.id;
21786 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21788 else
21790 glyph->u.cmp.automatic = 1;
21791 glyph->u.cmp.id = it->cmp_it.id;
21792 glyph->slice.cmp.from = it->cmp_it.from;
21793 glyph->slice.cmp.to = it->cmp_it.to - 1;
21795 glyph->avoid_cursor_p = it->avoid_cursor_p;
21796 glyph->multibyte_p = it->multibyte_p;
21797 glyph->left_box_line_p = it->start_of_box_run_p;
21798 glyph->right_box_line_p = it->end_of_box_run_p;
21799 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21800 || it->phys_descent > it->descent);
21801 glyph->padding_p = 0;
21802 glyph->glyph_not_available_p = 0;
21803 glyph->face_id = it->face_id;
21804 glyph->font_type = FONT_TYPE_UNKNOWN;
21805 if (it->bidi_p)
21807 glyph->resolved_level = it->bidi_it.resolved_level;
21808 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21809 abort ();
21810 glyph->bidi_type = it->bidi_it.type;
21812 ++it->glyph_row->used[area];
21814 else
21815 IT_EXPAND_MATRIX_WIDTH (it, area);
21819 /* Change IT->ascent and IT->height according to the setting of
21820 IT->voffset. */
21822 static INLINE void
21823 take_vertical_position_into_account (struct it *it)
21825 if (it->voffset)
21827 if (it->voffset < 0)
21828 /* Increase the ascent so that we can display the text higher
21829 in the line. */
21830 it->ascent -= it->voffset;
21831 else
21832 /* Increase the descent so that we can display the text lower
21833 in the line. */
21834 it->descent += it->voffset;
21839 /* Produce glyphs/get display metrics for the image IT is loaded with.
21840 See the description of struct display_iterator in dispextern.h for
21841 an overview of struct display_iterator. */
21843 static void
21844 produce_image_glyph (struct it *it)
21846 struct image *img;
21847 struct face *face;
21848 int glyph_ascent, crop;
21849 struct glyph_slice slice;
21851 xassert (it->what == IT_IMAGE);
21853 face = FACE_FROM_ID (it->f, it->face_id);
21854 xassert (face);
21855 /* Make sure X resources of the face is loaded. */
21856 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21858 if (it->image_id < 0)
21860 /* Fringe bitmap. */
21861 it->ascent = it->phys_ascent = 0;
21862 it->descent = it->phys_descent = 0;
21863 it->pixel_width = 0;
21864 it->nglyphs = 0;
21865 return;
21868 img = IMAGE_FROM_ID (it->f, it->image_id);
21869 xassert (img);
21870 /* Make sure X resources of the image is loaded. */
21871 prepare_image_for_display (it->f, img);
21873 slice.x = slice.y = 0;
21874 slice.width = img->width;
21875 slice.height = img->height;
21877 if (INTEGERP (it->slice.x))
21878 slice.x = XINT (it->slice.x);
21879 else if (FLOATP (it->slice.x))
21880 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21882 if (INTEGERP (it->slice.y))
21883 slice.y = XINT (it->slice.y);
21884 else if (FLOATP (it->slice.y))
21885 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21887 if (INTEGERP (it->slice.width))
21888 slice.width = XINT (it->slice.width);
21889 else if (FLOATP (it->slice.width))
21890 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21892 if (INTEGERP (it->slice.height))
21893 slice.height = XINT (it->slice.height);
21894 else if (FLOATP (it->slice.height))
21895 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21897 if (slice.x >= img->width)
21898 slice.x = img->width;
21899 if (slice.y >= img->height)
21900 slice.y = img->height;
21901 if (slice.x + slice.width >= img->width)
21902 slice.width = img->width - slice.x;
21903 if (slice.y + slice.height > img->height)
21904 slice.height = img->height - slice.y;
21906 if (slice.width == 0 || slice.height == 0)
21907 return;
21909 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21911 it->descent = slice.height - glyph_ascent;
21912 if (slice.y == 0)
21913 it->descent += img->vmargin;
21914 if (slice.y + slice.height == img->height)
21915 it->descent += img->vmargin;
21916 it->phys_descent = it->descent;
21918 it->pixel_width = slice.width;
21919 if (slice.x == 0)
21920 it->pixel_width += img->hmargin;
21921 if (slice.x + slice.width == img->width)
21922 it->pixel_width += img->hmargin;
21924 /* It's quite possible for images to have an ascent greater than
21925 their height, so don't get confused in that case. */
21926 if (it->descent < 0)
21927 it->descent = 0;
21929 it->nglyphs = 1;
21931 if (face->box != FACE_NO_BOX)
21933 if (face->box_line_width > 0)
21935 if (slice.y == 0)
21936 it->ascent += face->box_line_width;
21937 if (slice.y + slice.height == img->height)
21938 it->descent += face->box_line_width;
21941 if (it->start_of_box_run_p && slice.x == 0)
21942 it->pixel_width += eabs (face->box_line_width);
21943 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21944 it->pixel_width += eabs (face->box_line_width);
21947 take_vertical_position_into_account (it);
21949 /* Automatically crop wide image glyphs at right edge so we can
21950 draw the cursor on same display row. */
21951 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21952 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21954 it->pixel_width -= crop;
21955 slice.width -= crop;
21958 if (it->glyph_row)
21960 struct glyph *glyph;
21961 enum glyph_row_area area = it->area;
21963 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21964 if (glyph < it->glyph_row->glyphs[area + 1])
21966 glyph->charpos = CHARPOS (it->position);
21967 glyph->object = it->object;
21968 glyph->pixel_width = it->pixel_width;
21969 glyph->ascent = glyph_ascent;
21970 glyph->descent = it->descent;
21971 glyph->voffset = it->voffset;
21972 glyph->type = IMAGE_GLYPH;
21973 glyph->avoid_cursor_p = it->avoid_cursor_p;
21974 glyph->multibyte_p = it->multibyte_p;
21975 glyph->left_box_line_p = it->start_of_box_run_p;
21976 glyph->right_box_line_p = it->end_of_box_run_p;
21977 glyph->overlaps_vertically_p = 0;
21978 glyph->padding_p = 0;
21979 glyph->glyph_not_available_p = 0;
21980 glyph->face_id = it->face_id;
21981 glyph->u.img_id = img->id;
21982 glyph->slice.img = slice;
21983 glyph->font_type = FONT_TYPE_UNKNOWN;
21984 if (it->bidi_p)
21986 glyph->resolved_level = it->bidi_it.resolved_level;
21987 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21988 abort ();
21989 glyph->bidi_type = it->bidi_it.type;
21991 ++it->glyph_row->used[area];
21993 else
21994 IT_EXPAND_MATRIX_WIDTH (it, area);
21999 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22000 of the glyph, WIDTH and HEIGHT are the width and height of the
22001 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22003 static void
22004 append_stretch_glyph (struct it *it, Lisp_Object object,
22005 int width, int height, int ascent)
22007 struct glyph *glyph;
22008 enum glyph_row_area area = it->area;
22010 xassert (ascent >= 0 && ascent <= height);
22012 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22013 if (glyph < it->glyph_row->glyphs[area + 1])
22015 /* If the glyph row is reversed, we need to prepend the glyph
22016 rather than append it. */
22017 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22019 struct glyph *g;
22021 /* Make room for the additional glyph. */
22022 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22023 g[1] = *g;
22024 glyph = it->glyph_row->glyphs[area];
22026 glyph->charpos = CHARPOS (it->position);
22027 glyph->object = object;
22028 glyph->pixel_width = width;
22029 glyph->ascent = ascent;
22030 glyph->descent = height - ascent;
22031 glyph->voffset = it->voffset;
22032 glyph->type = STRETCH_GLYPH;
22033 glyph->avoid_cursor_p = it->avoid_cursor_p;
22034 glyph->multibyte_p = it->multibyte_p;
22035 glyph->left_box_line_p = it->start_of_box_run_p;
22036 glyph->right_box_line_p = it->end_of_box_run_p;
22037 glyph->overlaps_vertically_p = 0;
22038 glyph->padding_p = 0;
22039 glyph->glyph_not_available_p = 0;
22040 glyph->face_id = it->face_id;
22041 glyph->u.stretch.ascent = ascent;
22042 glyph->u.stretch.height = height;
22043 glyph->slice.img = null_glyph_slice;
22044 glyph->font_type = FONT_TYPE_UNKNOWN;
22045 if (it->bidi_p)
22047 glyph->resolved_level = it->bidi_it.resolved_level;
22048 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22049 abort ();
22050 glyph->bidi_type = it->bidi_it.type;
22052 else
22054 glyph->resolved_level = 0;
22055 glyph->bidi_type = UNKNOWN_BT;
22057 ++it->glyph_row->used[area];
22059 else
22060 IT_EXPAND_MATRIX_WIDTH (it, area);
22064 /* Produce a stretch glyph for iterator IT. IT->object is the value
22065 of the glyph property displayed. The value must be a list
22066 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22067 being recognized:
22069 1. `:width WIDTH' specifies that the space should be WIDTH *
22070 canonical char width wide. WIDTH may be an integer or floating
22071 point number.
22073 2. `:relative-width FACTOR' specifies that the width of the stretch
22074 should be computed from the width of the first character having the
22075 `glyph' property, and should be FACTOR times that width.
22077 3. `:align-to HPOS' specifies that the space should be wide enough
22078 to reach HPOS, a value in canonical character units.
22080 Exactly one of the above pairs must be present.
22082 4. `:height HEIGHT' specifies that the height of the stretch produced
22083 should be HEIGHT, measured in canonical character units.
22085 5. `:relative-height FACTOR' specifies that the height of the
22086 stretch should be FACTOR times the height of the characters having
22087 the glyph property.
22089 Either none or exactly one of 4 or 5 must be present.
22091 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22092 of the stretch should be used for the ascent of the stretch.
22093 ASCENT must be in the range 0 <= ASCENT <= 100. */
22095 static void
22096 produce_stretch_glyph (struct it *it)
22098 /* (space :width WIDTH :height HEIGHT ...) */
22099 Lisp_Object prop, plist;
22100 int width = 0, height = 0, align_to = -1;
22101 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22102 int ascent = 0;
22103 double tem;
22104 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22105 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22107 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22109 /* List should start with `space'. */
22110 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22111 plist = XCDR (it->object);
22113 /* Compute the width of the stretch. */
22114 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22115 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22117 /* Absolute width `:width WIDTH' specified and valid. */
22118 zero_width_ok_p = 1;
22119 width = (int)tem;
22121 else if (prop = Fplist_get (plist, QCrelative_width),
22122 NUMVAL (prop) > 0)
22124 /* Relative width `:relative-width FACTOR' specified and valid.
22125 Compute the width of the characters having the `glyph'
22126 property. */
22127 struct it it2;
22128 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22130 it2 = *it;
22131 if (it->multibyte_p)
22132 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22133 else
22135 it2.c = it2.char_to_display = *p, it2.len = 1;
22136 if (! ASCII_CHAR_P (it2.c))
22137 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22140 it2.glyph_row = NULL;
22141 it2.what = IT_CHARACTER;
22142 x_produce_glyphs (&it2);
22143 width = NUMVAL (prop) * it2.pixel_width;
22145 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22146 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22148 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22149 align_to = (align_to < 0
22151 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22152 else if (align_to < 0)
22153 align_to = window_box_left_offset (it->w, TEXT_AREA);
22154 width = max (0, (int)tem + align_to - it->current_x);
22155 zero_width_ok_p = 1;
22157 else
22158 /* Nothing specified -> width defaults to canonical char width. */
22159 width = FRAME_COLUMN_WIDTH (it->f);
22161 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22162 width = 1;
22164 /* Compute height. */
22165 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22166 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22168 height = (int)tem;
22169 zero_height_ok_p = 1;
22171 else if (prop = Fplist_get (plist, QCrelative_height),
22172 NUMVAL (prop) > 0)
22173 height = FONT_HEIGHT (font) * NUMVAL (prop);
22174 else
22175 height = FONT_HEIGHT (font);
22177 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22178 height = 1;
22180 /* Compute percentage of height used for ascent. If
22181 `:ascent ASCENT' is present and valid, use that. Otherwise,
22182 derive the ascent from the font in use. */
22183 if (prop = Fplist_get (plist, QCascent),
22184 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22185 ascent = height * NUMVAL (prop) / 100.0;
22186 else if (!NILP (prop)
22187 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22188 ascent = min (max (0, (int)tem), height);
22189 else
22190 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22192 if (width > 0 && it->line_wrap != TRUNCATE
22193 && it->current_x + width > it->last_visible_x)
22194 width = it->last_visible_x - it->current_x - 1;
22196 if (width > 0 && height > 0 && it->glyph_row)
22198 Lisp_Object object = it->stack[it->sp - 1].string;
22199 if (!STRINGP (object))
22200 object = it->w->buffer;
22201 append_stretch_glyph (it, object, width, height, ascent);
22204 it->pixel_width = width;
22205 it->ascent = it->phys_ascent = ascent;
22206 it->descent = it->phys_descent = height - it->ascent;
22207 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22209 take_vertical_position_into_account (it);
22212 /* Calculate line-height and line-spacing properties.
22213 An integer value specifies explicit pixel value.
22214 A float value specifies relative value to current face height.
22215 A cons (float . face-name) specifies relative value to
22216 height of specified face font.
22218 Returns height in pixels, or nil. */
22221 static Lisp_Object
22222 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22223 int boff, int override)
22225 Lisp_Object face_name = Qnil;
22226 int ascent, descent, height;
22228 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22229 return val;
22231 if (CONSP (val))
22233 face_name = XCAR (val);
22234 val = XCDR (val);
22235 if (!NUMBERP (val))
22236 val = make_number (1);
22237 if (NILP (face_name))
22239 height = it->ascent + it->descent;
22240 goto scale;
22244 if (NILP (face_name))
22246 font = FRAME_FONT (it->f);
22247 boff = FRAME_BASELINE_OFFSET (it->f);
22249 else if (EQ (face_name, Qt))
22251 override = 0;
22253 else
22255 int face_id;
22256 struct face *face;
22258 face_id = lookup_named_face (it->f, face_name, 0);
22259 if (face_id < 0)
22260 return make_number (-1);
22262 face = FACE_FROM_ID (it->f, face_id);
22263 font = face->font;
22264 if (font == NULL)
22265 return make_number (-1);
22266 boff = font->baseline_offset;
22267 if (font->vertical_centering)
22268 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22271 ascent = FONT_BASE (font) + boff;
22272 descent = FONT_DESCENT (font) - boff;
22274 if (override)
22276 it->override_ascent = ascent;
22277 it->override_descent = descent;
22278 it->override_boff = boff;
22281 height = ascent + descent;
22283 scale:
22284 if (FLOATP (val))
22285 height = (int)(XFLOAT_DATA (val) * height);
22286 else if (INTEGERP (val))
22287 height *= XINT (val);
22289 return make_number (height);
22293 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22294 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22295 and only if this is for a character for which no font was found.
22297 If the display method (it->glyphless_method) is
22298 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22299 length of the acronym or the hexadecimal string, UPPER_XOFF and
22300 UPPER_YOFF are pixel offsets for the upper part of the string,
22301 LOWER_XOFF and LOWER_YOFF are for the lower part.
22303 For the other display methods, LEN through LOWER_YOFF are zero. */
22305 static void
22306 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22307 short upper_xoff, short upper_yoff,
22308 short lower_xoff, short lower_yoff)
22310 struct glyph *glyph;
22311 enum glyph_row_area area = it->area;
22313 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22314 if (glyph < it->glyph_row->glyphs[area + 1])
22316 /* If the glyph row is reversed, we need to prepend the glyph
22317 rather than append it. */
22318 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22320 struct glyph *g;
22322 /* Make room for the additional glyph. */
22323 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22324 g[1] = *g;
22325 glyph = it->glyph_row->glyphs[area];
22327 glyph->charpos = CHARPOS (it->position);
22328 glyph->object = it->object;
22329 glyph->pixel_width = it->pixel_width;
22330 glyph->ascent = it->ascent;
22331 glyph->descent = it->descent;
22332 glyph->voffset = it->voffset;
22333 glyph->type = GLYPHLESS_GLYPH;
22334 glyph->u.glyphless.method = it->glyphless_method;
22335 glyph->u.glyphless.for_no_font = for_no_font;
22336 glyph->u.glyphless.len = len;
22337 glyph->u.glyphless.ch = it->c;
22338 glyph->slice.glyphless.upper_xoff = upper_xoff;
22339 glyph->slice.glyphless.upper_yoff = upper_yoff;
22340 glyph->slice.glyphless.lower_xoff = lower_xoff;
22341 glyph->slice.glyphless.lower_yoff = lower_yoff;
22342 glyph->avoid_cursor_p = it->avoid_cursor_p;
22343 glyph->multibyte_p = it->multibyte_p;
22344 glyph->left_box_line_p = it->start_of_box_run_p;
22345 glyph->right_box_line_p = it->end_of_box_run_p;
22346 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22347 || it->phys_descent > it->descent);
22348 glyph->padding_p = 0;
22349 glyph->glyph_not_available_p = 0;
22350 glyph->face_id = face_id;
22351 glyph->font_type = FONT_TYPE_UNKNOWN;
22352 if (it->bidi_p)
22354 glyph->resolved_level = it->bidi_it.resolved_level;
22355 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22356 abort ();
22357 glyph->bidi_type = it->bidi_it.type;
22359 ++it->glyph_row->used[area];
22361 else
22362 IT_EXPAND_MATRIX_WIDTH (it, area);
22366 /* Produce a glyph for a glyphless character for iterator IT.
22367 IT->glyphless_method specifies which method to use for displaying
22368 the character. See the description of enum
22369 glyphless_display_method in dispextern.h for the detail.
22371 FOR_NO_FONT is nonzero if and only if this is for a character for
22372 which no font was found. ACRONYM, if non-nil, is an acronym string
22373 for the character. */
22375 static void
22376 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22378 int face_id;
22379 struct face *face;
22380 struct font *font;
22381 int base_width, base_height, width, height;
22382 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22383 int len;
22385 /* Get the metrics of the base font. We always refer to the current
22386 ASCII face. */
22387 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22388 font = face->font ? face->font : FRAME_FONT (it->f);
22389 it->ascent = FONT_BASE (font) + font->baseline_offset;
22390 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22391 base_height = it->ascent + it->descent;
22392 base_width = font->average_width;
22394 /* Get a face ID for the glyph by utilizing a cache (the same way as
22395 doen for `escape-glyph' in get_next_display_element). */
22396 if (it->f == last_glyphless_glyph_frame
22397 && it->face_id == last_glyphless_glyph_face_id)
22399 face_id = last_glyphless_glyph_merged_face_id;
22401 else
22403 /* Merge the `glyphless-char' face into the current face. */
22404 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22405 last_glyphless_glyph_frame = it->f;
22406 last_glyphless_glyph_face_id = it->face_id;
22407 last_glyphless_glyph_merged_face_id = face_id;
22410 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22412 it->pixel_width = THIN_SPACE_WIDTH;
22413 len = 0;
22414 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22416 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22418 width = CHAR_WIDTH (it->c);
22419 if (width == 0)
22420 width = 1;
22421 else if (width > 4)
22422 width = 4;
22423 it->pixel_width = base_width * width;
22424 len = 0;
22425 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22427 else
22429 char buf[7], *str;
22430 unsigned int code[6];
22431 int upper_len;
22432 int ascent, descent;
22433 struct font_metrics metrics_upper, metrics_lower;
22435 face = FACE_FROM_ID (it->f, face_id);
22436 font = face->font ? face->font : FRAME_FONT (it->f);
22437 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22439 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22441 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22442 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22443 str = STRINGP (acronym) ? (char *) SDATA (acronym) : "";
22445 else
22447 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22448 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22449 str = buf;
22451 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22452 code[len] = font->driver->encode_char (font, str[len]);
22453 upper_len = (len + 1) / 2;
22454 font->driver->text_extents (font, code, upper_len,
22455 &metrics_upper);
22456 font->driver->text_extents (font, code + upper_len, len - upper_len,
22457 &metrics_lower);
22461 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22462 width = max (metrics_upper.width, metrics_lower.width) + 4;
22463 upper_xoff = upper_yoff = 2; /* the typical case */
22464 if (base_width >= width)
22466 /* Align the upper to the left, the lower to the right. */
22467 it->pixel_width = base_width;
22468 lower_xoff = base_width - 2 - metrics_lower.width;
22470 else
22472 /* Center the shorter one. */
22473 it->pixel_width = width;
22474 if (metrics_upper.width >= metrics_lower.width)
22475 lower_xoff = (width - metrics_lower.width) / 2;
22476 else
22477 upper_xoff = (width - metrics_upper.width) / 2;
22480 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22481 top, bottom, and between upper and lower strings. */
22482 height = (metrics_upper.ascent + metrics_upper.descent
22483 + metrics_lower.ascent + metrics_lower.descent) + 5;
22484 /* Center vertically.
22485 H:base_height, D:base_descent
22486 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22488 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22489 descent = D - H/2 + h/2;
22490 lower_yoff = descent - 2 - ld;
22491 upper_yoff = lower_yoff - la - 1 - ud; */
22492 ascent = - (it->descent - (base_height + height + 1) / 2);
22493 descent = it->descent - (base_height - height) / 2;
22494 lower_yoff = descent - 2 - metrics_lower.descent;
22495 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22496 - metrics_upper.descent);
22497 /* Don't make the height shorter than the base height. */
22498 if (height > base_height)
22500 it->ascent = ascent;
22501 it->descent = descent;
22505 it->phys_ascent = it->ascent;
22506 it->phys_descent = it->descent;
22507 if (it->glyph_row)
22508 append_glyphless_glyph (it, face_id, for_no_font, len,
22509 upper_xoff, upper_yoff,
22510 lower_xoff, lower_yoff);
22511 it->nglyphs = 1;
22512 take_vertical_position_into_account (it);
22516 /* RIF:
22517 Produce glyphs/get display metrics for the display element IT is
22518 loaded with. See the description of struct it in dispextern.h
22519 for an overview of struct it. */
22521 void
22522 x_produce_glyphs (struct it *it)
22524 int extra_line_spacing = it->extra_line_spacing;
22526 it->glyph_not_available_p = 0;
22528 if (it->what == IT_CHARACTER)
22530 XChar2b char2b;
22531 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22532 struct font *font = face->font;
22533 struct font_metrics *pcm = NULL;
22534 int boff; /* baseline offset */
22536 if (font == NULL)
22538 /* When no suitable font is found, display this character by
22539 the method specified in the first extra slot of
22540 Vglyphless_char_display. */
22541 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22543 xassert (it->what == IT_GLYPHLESS);
22544 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22545 goto done;
22548 boff = font->baseline_offset;
22549 if (font->vertical_centering)
22550 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22552 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22554 int stretched_p;
22556 it->nglyphs = 1;
22558 if (it->override_ascent >= 0)
22560 it->ascent = it->override_ascent;
22561 it->descent = it->override_descent;
22562 boff = it->override_boff;
22564 else
22566 it->ascent = FONT_BASE (font) + boff;
22567 it->descent = FONT_DESCENT (font) - boff;
22570 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22572 pcm = get_per_char_metric (it->f, font, &char2b);
22573 if (pcm->width == 0
22574 && pcm->rbearing == 0 && pcm->lbearing == 0)
22575 pcm = NULL;
22578 if (pcm)
22580 it->phys_ascent = pcm->ascent + boff;
22581 it->phys_descent = pcm->descent - boff;
22582 it->pixel_width = pcm->width;
22584 else
22586 it->glyph_not_available_p = 1;
22587 it->phys_ascent = it->ascent;
22588 it->phys_descent = it->descent;
22589 it->pixel_width = font->space_width;
22592 if (it->constrain_row_ascent_descent_p)
22594 if (it->descent > it->max_descent)
22596 it->ascent += it->descent - it->max_descent;
22597 it->descent = it->max_descent;
22599 if (it->ascent > it->max_ascent)
22601 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22602 it->ascent = it->max_ascent;
22604 it->phys_ascent = min (it->phys_ascent, it->ascent);
22605 it->phys_descent = min (it->phys_descent, it->descent);
22606 extra_line_spacing = 0;
22609 /* If this is a space inside a region of text with
22610 `space-width' property, change its width. */
22611 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22612 if (stretched_p)
22613 it->pixel_width *= XFLOATINT (it->space_width);
22615 /* If face has a box, add the box thickness to the character
22616 height. If character has a box line to the left and/or
22617 right, add the box line width to the character's width. */
22618 if (face->box != FACE_NO_BOX)
22620 int thick = face->box_line_width;
22622 if (thick > 0)
22624 it->ascent += thick;
22625 it->descent += thick;
22627 else
22628 thick = -thick;
22630 if (it->start_of_box_run_p)
22631 it->pixel_width += thick;
22632 if (it->end_of_box_run_p)
22633 it->pixel_width += thick;
22636 /* If face has an overline, add the height of the overline
22637 (1 pixel) and a 1 pixel margin to the character height. */
22638 if (face->overline_p)
22639 it->ascent += overline_margin;
22641 if (it->constrain_row_ascent_descent_p)
22643 if (it->ascent > it->max_ascent)
22644 it->ascent = it->max_ascent;
22645 if (it->descent > it->max_descent)
22646 it->descent = it->max_descent;
22649 take_vertical_position_into_account (it);
22651 /* If we have to actually produce glyphs, do it. */
22652 if (it->glyph_row)
22654 if (stretched_p)
22656 /* Translate a space with a `space-width' property
22657 into a stretch glyph. */
22658 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22659 / FONT_HEIGHT (font));
22660 append_stretch_glyph (it, it->object, it->pixel_width,
22661 it->ascent + it->descent, ascent);
22663 else
22664 append_glyph (it);
22666 /* If characters with lbearing or rbearing are displayed
22667 in this line, record that fact in a flag of the
22668 glyph row. This is used to optimize X output code. */
22669 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22670 it->glyph_row->contains_overlapping_glyphs_p = 1;
22672 if (! stretched_p && it->pixel_width == 0)
22673 /* We assure that all visible glyphs have at least 1-pixel
22674 width. */
22675 it->pixel_width = 1;
22677 else if (it->char_to_display == '\n')
22679 /* A newline has no width, but we need the height of the
22680 line. But if previous part of the line sets a height,
22681 don't increase that height */
22683 Lisp_Object height;
22684 Lisp_Object total_height = Qnil;
22686 it->override_ascent = -1;
22687 it->pixel_width = 0;
22688 it->nglyphs = 0;
22690 height = get_it_property (it, Qline_height);
22691 /* Split (line-height total-height) list */
22692 if (CONSP (height)
22693 && CONSP (XCDR (height))
22694 && NILP (XCDR (XCDR (height))))
22696 total_height = XCAR (XCDR (height));
22697 height = XCAR (height);
22699 height = calc_line_height_property (it, height, font, boff, 1);
22701 if (it->override_ascent >= 0)
22703 it->ascent = it->override_ascent;
22704 it->descent = it->override_descent;
22705 boff = it->override_boff;
22707 else
22709 it->ascent = FONT_BASE (font) + boff;
22710 it->descent = FONT_DESCENT (font) - boff;
22713 if (EQ (height, Qt))
22715 if (it->descent > it->max_descent)
22717 it->ascent += it->descent - it->max_descent;
22718 it->descent = it->max_descent;
22720 if (it->ascent > it->max_ascent)
22722 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22723 it->ascent = it->max_ascent;
22725 it->phys_ascent = min (it->phys_ascent, it->ascent);
22726 it->phys_descent = min (it->phys_descent, it->descent);
22727 it->constrain_row_ascent_descent_p = 1;
22728 extra_line_spacing = 0;
22730 else
22732 Lisp_Object spacing;
22734 it->phys_ascent = it->ascent;
22735 it->phys_descent = it->descent;
22737 if ((it->max_ascent > 0 || it->max_descent > 0)
22738 && face->box != FACE_NO_BOX
22739 && face->box_line_width > 0)
22741 it->ascent += face->box_line_width;
22742 it->descent += face->box_line_width;
22744 if (!NILP (height)
22745 && XINT (height) > it->ascent + it->descent)
22746 it->ascent = XINT (height) - it->descent;
22748 if (!NILP (total_height))
22749 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22750 else
22752 spacing = get_it_property (it, Qline_spacing);
22753 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22755 if (INTEGERP (spacing))
22757 extra_line_spacing = XINT (spacing);
22758 if (!NILP (total_height))
22759 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22763 else /* i.e. (it->char_to_display == '\t') */
22765 if (font->space_width > 0)
22767 int tab_width = it->tab_width * font->space_width;
22768 int x = it->current_x + it->continuation_lines_width;
22769 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22771 /* If the distance from the current position to the next tab
22772 stop is less than a space character width, use the
22773 tab stop after that. */
22774 if (next_tab_x - x < font->space_width)
22775 next_tab_x += tab_width;
22777 it->pixel_width = next_tab_x - x;
22778 it->nglyphs = 1;
22779 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22780 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22782 if (it->glyph_row)
22784 append_stretch_glyph (it, it->object, it->pixel_width,
22785 it->ascent + it->descent, it->ascent);
22788 else
22790 it->pixel_width = 0;
22791 it->nglyphs = 1;
22795 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22797 /* A static composition.
22799 Note: A composition is represented as one glyph in the
22800 glyph matrix. There are no padding glyphs.
22802 Important note: pixel_width, ascent, and descent are the
22803 values of what is drawn by draw_glyphs (i.e. the values of
22804 the overall glyphs composed). */
22805 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22806 int boff; /* baseline offset */
22807 struct composition *cmp = composition_table[it->cmp_it.id];
22808 int glyph_len = cmp->glyph_len;
22809 struct font *font = face->font;
22811 it->nglyphs = 1;
22813 /* If we have not yet calculated pixel size data of glyphs of
22814 the composition for the current face font, calculate them
22815 now. Theoretically, we have to check all fonts for the
22816 glyphs, but that requires much time and memory space. So,
22817 here we check only the font of the first glyph. This may
22818 lead to incorrect display, but it's very rare, and C-l
22819 (recenter-top-bottom) can correct the display anyway. */
22820 if (! cmp->font || cmp->font != font)
22822 /* Ascent and descent of the font of the first character
22823 of this composition (adjusted by baseline offset).
22824 Ascent and descent of overall glyphs should not be less
22825 than these, respectively. */
22826 int font_ascent, font_descent, font_height;
22827 /* Bounding box of the overall glyphs. */
22828 int leftmost, rightmost, lowest, highest;
22829 int lbearing, rbearing;
22830 int i, width, ascent, descent;
22831 int left_padded = 0, right_padded = 0;
22832 int c;
22833 XChar2b char2b;
22834 struct font_metrics *pcm;
22835 int font_not_found_p;
22836 EMACS_INT pos;
22838 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22839 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22840 break;
22841 if (glyph_len < cmp->glyph_len)
22842 right_padded = 1;
22843 for (i = 0; i < glyph_len; i++)
22845 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22846 break;
22847 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22849 if (i > 0)
22850 left_padded = 1;
22852 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22853 : IT_CHARPOS (*it));
22854 /* If no suitable font is found, use the default font. */
22855 font_not_found_p = font == NULL;
22856 if (font_not_found_p)
22858 face = face->ascii_face;
22859 font = face->font;
22861 boff = font->baseline_offset;
22862 if (font->vertical_centering)
22863 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22864 font_ascent = FONT_BASE (font) + boff;
22865 font_descent = FONT_DESCENT (font) - boff;
22866 font_height = FONT_HEIGHT (font);
22868 cmp->font = (void *) font;
22870 pcm = NULL;
22871 if (! font_not_found_p)
22873 get_char_face_and_encoding (it->f, c, it->face_id,
22874 &char2b, it->multibyte_p, 0);
22875 pcm = get_per_char_metric (it->f, font, &char2b);
22878 /* Initialize the bounding box. */
22879 if (pcm)
22881 width = pcm->width;
22882 ascent = pcm->ascent;
22883 descent = pcm->descent;
22884 lbearing = pcm->lbearing;
22885 rbearing = pcm->rbearing;
22887 else
22889 width = font->space_width;
22890 ascent = FONT_BASE (font);
22891 descent = FONT_DESCENT (font);
22892 lbearing = 0;
22893 rbearing = width;
22896 rightmost = width;
22897 leftmost = 0;
22898 lowest = - descent + boff;
22899 highest = ascent + boff;
22901 if (! font_not_found_p
22902 && font->default_ascent
22903 && CHAR_TABLE_P (Vuse_default_ascent)
22904 && !NILP (Faref (Vuse_default_ascent,
22905 make_number (it->char_to_display))))
22906 highest = font->default_ascent + boff;
22908 /* Draw the first glyph at the normal position. It may be
22909 shifted to right later if some other glyphs are drawn
22910 at the left. */
22911 cmp->offsets[i * 2] = 0;
22912 cmp->offsets[i * 2 + 1] = boff;
22913 cmp->lbearing = lbearing;
22914 cmp->rbearing = rbearing;
22916 /* Set cmp->offsets for the remaining glyphs. */
22917 for (i++; i < glyph_len; i++)
22919 int left, right, btm, top;
22920 int ch = COMPOSITION_GLYPH (cmp, i);
22921 int face_id;
22922 struct face *this_face;
22923 int this_boff;
22925 if (ch == '\t')
22926 ch = ' ';
22927 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22928 this_face = FACE_FROM_ID (it->f, face_id);
22929 font = this_face->font;
22931 if (font == NULL)
22932 pcm = NULL;
22933 else
22935 this_boff = font->baseline_offset;
22936 if (font->vertical_centering)
22937 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22938 get_char_face_and_encoding (it->f, ch, face_id,
22939 &char2b, it->multibyte_p, 0);
22940 pcm = get_per_char_metric (it->f, font, &char2b);
22942 if (! pcm)
22943 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22944 else
22946 width = pcm->width;
22947 ascent = pcm->ascent;
22948 descent = pcm->descent;
22949 lbearing = pcm->lbearing;
22950 rbearing = pcm->rbearing;
22951 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22953 /* Relative composition with or without
22954 alternate chars. */
22955 left = (leftmost + rightmost - width) / 2;
22956 btm = - descent + boff;
22957 if (font->relative_compose
22958 && (! CHAR_TABLE_P (Vignore_relative_composition)
22959 || NILP (Faref (Vignore_relative_composition,
22960 make_number (ch)))))
22963 if (- descent >= font->relative_compose)
22964 /* One extra pixel between two glyphs. */
22965 btm = highest + 1;
22966 else if (ascent <= 0)
22967 /* One extra pixel between two glyphs. */
22968 btm = lowest - 1 - ascent - descent;
22971 else
22973 /* A composition rule is specified by an integer
22974 value that encodes global and new reference
22975 points (GREF and NREF). GREF and NREF are
22976 specified by numbers as below:
22978 0---1---2 -- ascent
22982 9--10--11 -- center
22984 ---3---4---5--- baseline
22986 6---7---8 -- descent
22988 int rule = COMPOSITION_RULE (cmp, i);
22989 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22991 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22992 grefx = gref % 3, nrefx = nref % 3;
22993 grefy = gref / 3, nrefy = nref / 3;
22994 if (xoff)
22995 xoff = font_height * (xoff - 128) / 256;
22996 if (yoff)
22997 yoff = font_height * (yoff - 128) / 256;
22999 left = (leftmost
23000 + grefx * (rightmost - leftmost) / 2
23001 - nrefx * width / 2
23002 + xoff);
23004 btm = ((grefy == 0 ? highest
23005 : grefy == 1 ? 0
23006 : grefy == 2 ? lowest
23007 : (highest + lowest) / 2)
23008 - (nrefy == 0 ? ascent + descent
23009 : nrefy == 1 ? descent - boff
23010 : nrefy == 2 ? 0
23011 : (ascent + descent) / 2)
23012 + yoff);
23015 cmp->offsets[i * 2] = left;
23016 cmp->offsets[i * 2 + 1] = btm + descent;
23018 /* Update the bounding box of the overall glyphs. */
23019 if (width > 0)
23021 right = left + width;
23022 if (left < leftmost)
23023 leftmost = left;
23024 if (right > rightmost)
23025 rightmost = right;
23027 top = btm + descent + ascent;
23028 if (top > highest)
23029 highest = top;
23030 if (btm < lowest)
23031 lowest = btm;
23033 if (cmp->lbearing > left + lbearing)
23034 cmp->lbearing = left + lbearing;
23035 if (cmp->rbearing < left + rbearing)
23036 cmp->rbearing = left + rbearing;
23040 /* If there are glyphs whose x-offsets are negative,
23041 shift all glyphs to the right and make all x-offsets
23042 non-negative. */
23043 if (leftmost < 0)
23045 for (i = 0; i < cmp->glyph_len; i++)
23046 cmp->offsets[i * 2] -= leftmost;
23047 rightmost -= leftmost;
23048 cmp->lbearing -= leftmost;
23049 cmp->rbearing -= leftmost;
23052 if (left_padded && cmp->lbearing < 0)
23054 for (i = 0; i < cmp->glyph_len; i++)
23055 cmp->offsets[i * 2] -= cmp->lbearing;
23056 rightmost -= cmp->lbearing;
23057 cmp->rbearing -= cmp->lbearing;
23058 cmp->lbearing = 0;
23060 if (right_padded && rightmost < cmp->rbearing)
23062 rightmost = cmp->rbearing;
23065 cmp->pixel_width = rightmost;
23066 cmp->ascent = highest;
23067 cmp->descent = - lowest;
23068 if (cmp->ascent < font_ascent)
23069 cmp->ascent = font_ascent;
23070 if (cmp->descent < font_descent)
23071 cmp->descent = font_descent;
23074 if (it->glyph_row
23075 && (cmp->lbearing < 0
23076 || cmp->rbearing > cmp->pixel_width))
23077 it->glyph_row->contains_overlapping_glyphs_p = 1;
23079 it->pixel_width = cmp->pixel_width;
23080 it->ascent = it->phys_ascent = cmp->ascent;
23081 it->descent = it->phys_descent = cmp->descent;
23082 if (face->box != FACE_NO_BOX)
23084 int thick = face->box_line_width;
23086 if (thick > 0)
23088 it->ascent += thick;
23089 it->descent += thick;
23091 else
23092 thick = - thick;
23094 if (it->start_of_box_run_p)
23095 it->pixel_width += thick;
23096 if (it->end_of_box_run_p)
23097 it->pixel_width += thick;
23100 /* If face has an overline, add the height of the overline
23101 (1 pixel) and a 1 pixel margin to the character height. */
23102 if (face->overline_p)
23103 it->ascent += overline_margin;
23105 take_vertical_position_into_account (it);
23106 if (it->ascent < 0)
23107 it->ascent = 0;
23108 if (it->descent < 0)
23109 it->descent = 0;
23111 if (it->glyph_row)
23112 append_composite_glyph (it);
23114 else if (it->what == IT_COMPOSITION)
23116 /* A dynamic (automatic) composition. */
23117 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23118 Lisp_Object gstring;
23119 struct font_metrics metrics;
23121 gstring = composition_gstring_from_id (it->cmp_it.id);
23122 it->pixel_width
23123 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23124 &metrics);
23125 if (it->glyph_row
23126 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23127 it->glyph_row->contains_overlapping_glyphs_p = 1;
23128 it->ascent = it->phys_ascent = metrics.ascent;
23129 it->descent = it->phys_descent = metrics.descent;
23130 if (face->box != FACE_NO_BOX)
23132 int thick = face->box_line_width;
23134 if (thick > 0)
23136 it->ascent += thick;
23137 it->descent += thick;
23139 else
23140 thick = - thick;
23142 if (it->start_of_box_run_p)
23143 it->pixel_width += thick;
23144 if (it->end_of_box_run_p)
23145 it->pixel_width += thick;
23147 /* If face has an overline, add the height of the overline
23148 (1 pixel) and a 1 pixel margin to the character height. */
23149 if (face->overline_p)
23150 it->ascent += overline_margin;
23151 take_vertical_position_into_account (it);
23152 if (it->ascent < 0)
23153 it->ascent = 0;
23154 if (it->descent < 0)
23155 it->descent = 0;
23157 if (it->glyph_row)
23158 append_composite_glyph (it);
23160 else if (it->what == IT_GLYPHLESS)
23161 produce_glyphless_glyph (it, 0, Qnil);
23162 else if (it->what == IT_IMAGE)
23163 produce_image_glyph (it);
23164 else if (it->what == IT_STRETCH)
23165 produce_stretch_glyph (it);
23167 done:
23168 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23169 because this isn't true for images with `:ascent 100'. */
23170 xassert (it->ascent >= 0 && it->descent >= 0);
23171 if (it->area == TEXT_AREA)
23172 it->current_x += it->pixel_width;
23174 if (extra_line_spacing > 0)
23176 it->descent += extra_line_spacing;
23177 if (extra_line_spacing > it->max_extra_line_spacing)
23178 it->max_extra_line_spacing = extra_line_spacing;
23181 it->max_ascent = max (it->max_ascent, it->ascent);
23182 it->max_descent = max (it->max_descent, it->descent);
23183 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23184 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23187 /* EXPORT for RIF:
23188 Output LEN glyphs starting at START at the nominal cursor position.
23189 Advance the nominal cursor over the text. The global variable
23190 updated_window contains the window being updated, updated_row is
23191 the glyph row being updated, and updated_area is the area of that
23192 row being updated. */
23194 void
23195 x_write_glyphs (struct glyph *start, int len)
23197 int x, hpos;
23199 xassert (updated_window && updated_row);
23200 BLOCK_INPUT;
23202 /* Write glyphs. */
23204 hpos = start - updated_row->glyphs[updated_area];
23205 x = draw_glyphs (updated_window, output_cursor.x,
23206 updated_row, updated_area,
23207 hpos, hpos + len,
23208 DRAW_NORMAL_TEXT, 0);
23210 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23211 if (updated_area == TEXT_AREA
23212 && updated_window->phys_cursor_on_p
23213 && updated_window->phys_cursor.vpos == output_cursor.vpos
23214 && updated_window->phys_cursor.hpos >= hpos
23215 && updated_window->phys_cursor.hpos < hpos + len)
23216 updated_window->phys_cursor_on_p = 0;
23218 UNBLOCK_INPUT;
23220 /* Advance the output cursor. */
23221 output_cursor.hpos += len;
23222 output_cursor.x = x;
23226 /* EXPORT for RIF:
23227 Insert LEN glyphs from START at the nominal cursor position. */
23229 void
23230 x_insert_glyphs (struct glyph *start, int len)
23232 struct frame *f;
23233 struct window *w;
23234 int line_height, shift_by_width, shifted_region_width;
23235 struct glyph_row *row;
23236 struct glyph *glyph;
23237 int frame_x, frame_y;
23238 EMACS_INT hpos;
23240 xassert (updated_window && updated_row);
23241 BLOCK_INPUT;
23242 w = updated_window;
23243 f = XFRAME (WINDOW_FRAME (w));
23245 /* Get the height of the line we are in. */
23246 row = updated_row;
23247 line_height = row->height;
23249 /* Get the width of the glyphs to insert. */
23250 shift_by_width = 0;
23251 for (glyph = start; glyph < start + len; ++glyph)
23252 shift_by_width += glyph->pixel_width;
23254 /* Get the width of the region to shift right. */
23255 shifted_region_width = (window_box_width (w, updated_area)
23256 - output_cursor.x
23257 - shift_by_width);
23259 /* Shift right. */
23260 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23261 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23263 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23264 line_height, shift_by_width);
23266 /* Write the glyphs. */
23267 hpos = start - row->glyphs[updated_area];
23268 draw_glyphs (w, output_cursor.x, row, updated_area,
23269 hpos, hpos + len,
23270 DRAW_NORMAL_TEXT, 0);
23272 /* Advance the output cursor. */
23273 output_cursor.hpos += len;
23274 output_cursor.x += shift_by_width;
23275 UNBLOCK_INPUT;
23279 /* EXPORT for RIF:
23280 Erase the current text line from the nominal cursor position
23281 (inclusive) to pixel column TO_X (exclusive). The idea is that
23282 everything from TO_X onward is already erased.
23284 TO_X is a pixel position relative to updated_area of
23285 updated_window. TO_X == -1 means clear to the end of this area. */
23287 void
23288 x_clear_end_of_line (int to_x)
23290 struct frame *f;
23291 struct window *w = updated_window;
23292 int max_x, min_y, max_y;
23293 int from_x, from_y, to_y;
23295 xassert (updated_window && updated_row);
23296 f = XFRAME (w->frame);
23298 if (updated_row->full_width_p)
23299 max_x = WINDOW_TOTAL_WIDTH (w);
23300 else
23301 max_x = window_box_width (w, updated_area);
23302 max_y = window_text_bottom_y (w);
23304 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23305 of window. For TO_X > 0, truncate to end of drawing area. */
23306 if (to_x == 0)
23307 return;
23308 else if (to_x < 0)
23309 to_x = max_x;
23310 else
23311 to_x = min (to_x, max_x);
23313 to_y = min (max_y, output_cursor.y + updated_row->height);
23315 /* Notice if the cursor will be cleared by this operation. */
23316 if (!updated_row->full_width_p)
23317 notice_overwritten_cursor (w, updated_area,
23318 output_cursor.x, -1,
23319 updated_row->y,
23320 MATRIX_ROW_BOTTOM_Y (updated_row));
23322 from_x = output_cursor.x;
23324 /* Translate to frame coordinates. */
23325 if (updated_row->full_width_p)
23327 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23328 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23330 else
23332 int area_left = window_box_left (w, updated_area);
23333 from_x += area_left;
23334 to_x += area_left;
23337 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23338 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23339 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23341 /* Prevent inadvertently clearing to end of the X window. */
23342 if (to_x > from_x && to_y > from_y)
23344 BLOCK_INPUT;
23345 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23346 to_x - from_x, to_y - from_y);
23347 UNBLOCK_INPUT;
23351 #endif /* HAVE_WINDOW_SYSTEM */
23355 /***********************************************************************
23356 Cursor types
23357 ***********************************************************************/
23359 /* Value is the internal representation of the specified cursor type
23360 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23361 of the bar cursor. */
23363 static enum text_cursor_kinds
23364 get_specified_cursor_type (Lisp_Object arg, int *width)
23366 enum text_cursor_kinds type;
23368 if (NILP (arg))
23369 return NO_CURSOR;
23371 if (EQ (arg, Qbox))
23372 return FILLED_BOX_CURSOR;
23374 if (EQ (arg, Qhollow))
23375 return HOLLOW_BOX_CURSOR;
23377 if (EQ (arg, Qbar))
23379 *width = 2;
23380 return BAR_CURSOR;
23383 if (CONSP (arg)
23384 && EQ (XCAR (arg), Qbar)
23385 && INTEGERP (XCDR (arg))
23386 && XINT (XCDR (arg)) >= 0)
23388 *width = XINT (XCDR (arg));
23389 return BAR_CURSOR;
23392 if (EQ (arg, Qhbar))
23394 *width = 2;
23395 return HBAR_CURSOR;
23398 if (CONSP (arg)
23399 && EQ (XCAR (arg), Qhbar)
23400 && INTEGERP (XCDR (arg))
23401 && XINT (XCDR (arg)) >= 0)
23403 *width = XINT (XCDR (arg));
23404 return HBAR_CURSOR;
23407 /* Treat anything unknown as "hollow box cursor".
23408 It was bad to signal an error; people have trouble fixing
23409 .Xdefaults with Emacs, when it has something bad in it. */
23410 type = HOLLOW_BOX_CURSOR;
23412 return type;
23415 /* Set the default cursor types for specified frame. */
23416 void
23417 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23419 int width;
23420 Lisp_Object tem;
23422 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23423 FRAME_CURSOR_WIDTH (f) = width;
23425 /* By default, set up the blink-off state depending on the on-state. */
23427 tem = Fassoc (arg, Vblink_cursor_alist);
23428 if (!NILP (tem))
23430 FRAME_BLINK_OFF_CURSOR (f)
23431 = get_specified_cursor_type (XCDR (tem), &width);
23432 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23434 else
23435 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23439 #ifdef HAVE_WINDOW_SYSTEM
23441 /* Return the cursor we want to be displayed in window W. Return
23442 width of bar/hbar cursor through WIDTH arg. Return with
23443 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23444 (i.e. if the `system caret' should track this cursor).
23446 In a mini-buffer window, we want the cursor only to appear if we
23447 are reading input from this window. For the selected window, we
23448 want the cursor type given by the frame parameter or buffer local
23449 setting of cursor-type. If explicitly marked off, draw no cursor.
23450 In all other cases, we want a hollow box cursor. */
23452 static enum text_cursor_kinds
23453 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23454 int *active_cursor)
23456 struct frame *f = XFRAME (w->frame);
23457 struct buffer *b = XBUFFER (w->buffer);
23458 int cursor_type = DEFAULT_CURSOR;
23459 Lisp_Object alt_cursor;
23460 int non_selected = 0;
23462 *active_cursor = 1;
23464 /* Echo area */
23465 if (cursor_in_echo_area
23466 && FRAME_HAS_MINIBUF_P (f)
23467 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23469 if (w == XWINDOW (echo_area_window))
23471 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23473 *width = FRAME_CURSOR_WIDTH (f);
23474 return FRAME_DESIRED_CURSOR (f);
23476 else
23477 return get_specified_cursor_type (b->cursor_type, width);
23480 *active_cursor = 0;
23481 non_selected = 1;
23484 /* Detect a nonselected window or nonselected frame. */
23485 else if (w != XWINDOW (f->selected_window)
23486 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23488 *active_cursor = 0;
23490 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23491 return NO_CURSOR;
23493 non_selected = 1;
23496 /* Never display a cursor in a window in which cursor-type is nil. */
23497 if (NILP (b->cursor_type))
23498 return NO_CURSOR;
23500 /* Get the normal cursor type for this window. */
23501 if (EQ (b->cursor_type, Qt))
23503 cursor_type = FRAME_DESIRED_CURSOR (f);
23504 *width = FRAME_CURSOR_WIDTH (f);
23506 else
23507 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23509 /* Use cursor-in-non-selected-windows instead
23510 for non-selected window or frame. */
23511 if (non_selected)
23513 alt_cursor = b->cursor_in_non_selected_windows;
23514 if (!EQ (Qt, alt_cursor))
23515 return get_specified_cursor_type (alt_cursor, width);
23516 /* t means modify the normal cursor type. */
23517 if (cursor_type == FILLED_BOX_CURSOR)
23518 cursor_type = HOLLOW_BOX_CURSOR;
23519 else if (cursor_type == BAR_CURSOR && *width > 1)
23520 --*width;
23521 return cursor_type;
23524 /* Use normal cursor if not blinked off. */
23525 if (!w->cursor_off_p)
23527 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23529 if (cursor_type == FILLED_BOX_CURSOR)
23531 /* Using a block cursor on large images can be very annoying.
23532 So use a hollow cursor for "large" images.
23533 If image is not transparent (no mask), also use hollow cursor. */
23534 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23535 if (img != NULL && IMAGEP (img->spec))
23537 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23538 where N = size of default frame font size.
23539 This should cover most of the "tiny" icons people may use. */
23540 if (!img->mask
23541 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23542 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23543 cursor_type = HOLLOW_BOX_CURSOR;
23546 else if (cursor_type != NO_CURSOR)
23548 /* Display current only supports BOX and HOLLOW cursors for images.
23549 So for now, unconditionally use a HOLLOW cursor when cursor is
23550 not a solid box cursor. */
23551 cursor_type = HOLLOW_BOX_CURSOR;
23554 return cursor_type;
23557 /* Cursor is blinked off, so determine how to "toggle" it. */
23559 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23560 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23561 return get_specified_cursor_type (XCDR (alt_cursor), width);
23563 /* Then see if frame has specified a specific blink off cursor type. */
23564 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23566 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23567 return FRAME_BLINK_OFF_CURSOR (f);
23570 #if 0
23571 /* Some people liked having a permanently visible blinking cursor,
23572 while others had very strong opinions against it. So it was
23573 decided to remove it. KFS 2003-09-03 */
23575 /* Finally perform built-in cursor blinking:
23576 filled box <-> hollow box
23577 wide [h]bar <-> narrow [h]bar
23578 narrow [h]bar <-> no cursor
23579 other type <-> no cursor */
23581 if (cursor_type == FILLED_BOX_CURSOR)
23582 return HOLLOW_BOX_CURSOR;
23584 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23586 *width = 1;
23587 return cursor_type;
23589 #endif
23591 return NO_CURSOR;
23595 /* Notice when the text cursor of window W has been completely
23596 overwritten by a drawing operation that outputs glyphs in AREA
23597 starting at X0 and ending at X1 in the line starting at Y0 and
23598 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23599 the rest of the line after X0 has been written. Y coordinates
23600 are window-relative. */
23602 static void
23603 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23604 int x0, int x1, int y0, int y1)
23606 int cx0, cx1, cy0, cy1;
23607 struct glyph_row *row;
23609 if (!w->phys_cursor_on_p)
23610 return;
23611 if (area != TEXT_AREA)
23612 return;
23614 if (w->phys_cursor.vpos < 0
23615 || w->phys_cursor.vpos >= w->current_matrix->nrows
23616 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23617 !(row->enabled_p && row->displays_text_p)))
23618 return;
23620 if (row->cursor_in_fringe_p)
23622 row->cursor_in_fringe_p = 0;
23623 draw_fringe_bitmap (w, row, row->reversed_p);
23624 w->phys_cursor_on_p = 0;
23625 return;
23628 cx0 = w->phys_cursor.x;
23629 cx1 = cx0 + w->phys_cursor_width;
23630 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23631 return;
23633 /* The cursor image will be completely removed from the
23634 screen if the output area intersects the cursor area in
23635 y-direction. When we draw in [y0 y1[, and some part of
23636 the cursor is at y < y0, that part must have been drawn
23637 before. When scrolling, the cursor is erased before
23638 actually scrolling, so we don't come here. When not
23639 scrolling, the rows above the old cursor row must have
23640 changed, and in this case these rows must have written
23641 over the cursor image.
23643 Likewise if part of the cursor is below y1, with the
23644 exception of the cursor being in the first blank row at
23645 the buffer and window end because update_text_area
23646 doesn't draw that row. (Except when it does, but
23647 that's handled in update_text_area.) */
23649 cy0 = w->phys_cursor.y;
23650 cy1 = cy0 + w->phys_cursor_height;
23651 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23652 return;
23654 w->phys_cursor_on_p = 0;
23657 #endif /* HAVE_WINDOW_SYSTEM */
23660 /************************************************************************
23661 Mouse Face
23662 ************************************************************************/
23664 #ifdef HAVE_WINDOW_SYSTEM
23666 /* EXPORT for RIF:
23667 Fix the display of area AREA of overlapping row ROW in window W
23668 with respect to the overlapping part OVERLAPS. */
23670 void
23671 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23672 enum glyph_row_area area, int overlaps)
23674 int i, x;
23676 BLOCK_INPUT;
23678 x = 0;
23679 for (i = 0; i < row->used[area];)
23681 if (row->glyphs[area][i].overlaps_vertically_p)
23683 int start = i, start_x = x;
23687 x += row->glyphs[area][i].pixel_width;
23688 ++i;
23690 while (i < row->used[area]
23691 && row->glyphs[area][i].overlaps_vertically_p);
23693 draw_glyphs (w, start_x, row, area,
23694 start, i,
23695 DRAW_NORMAL_TEXT, overlaps);
23697 else
23699 x += row->glyphs[area][i].pixel_width;
23700 ++i;
23704 UNBLOCK_INPUT;
23708 /* EXPORT:
23709 Draw the cursor glyph of window W in glyph row ROW. See the
23710 comment of draw_glyphs for the meaning of HL. */
23712 void
23713 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23714 enum draw_glyphs_face hl)
23716 /* If cursor hpos is out of bounds, don't draw garbage. This can
23717 happen in mini-buffer windows when switching between echo area
23718 glyphs and mini-buffer. */
23719 if ((row->reversed_p
23720 ? (w->phys_cursor.hpos >= 0)
23721 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23723 int on_p = w->phys_cursor_on_p;
23724 int x1;
23725 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23726 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23727 hl, 0);
23728 w->phys_cursor_on_p = on_p;
23730 if (hl == DRAW_CURSOR)
23731 w->phys_cursor_width = x1 - w->phys_cursor.x;
23732 /* When we erase the cursor, and ROW is overlapped by other
23733 rows, make sure that these overlapping parts of other rows
23734 are redrawn. */
23735 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23737 w->phys_cursor_width = x1 - w->phys_cursor.x;
23739 if (row > w->current_matrix->rows
23740 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23741 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23742 OVERLAPS_ERASED_CURSOR);
23744 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23745 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23746 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23747 OVERLAPS_ERASED_CURSOR);
23753 /* EXPORT:
23754 Erase the image of a cursor of window W from the screen. */
23756 void
23757 erase_phys_cursor (struct window *w)
23759 struct frame *f = XFRAME (w->frame);
23760 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23761 int hpos = w->phys_cursor.hpos;
23762 int vpos = w->phys_cursor.vpos;
23763 int mouse_face_here_p = 0;
23764 struct glyph_matrix *active_glyphs = w->current_matrix;
23765 struct glyph_row *cursor_row;
23766 struct glyph *cursor_glyph;
23767 enum draw_glyphs_face hl;
23769 /* No cursor displayed or row invalidated => nothing to do on the
23770 screen. */
23771 if (w->phys_cursor_type == NO_CURSOR)
23772 goto mark_cursor_off;
23774 /* VPOS >= active_glyphs->nrows means that window has been resized.
23775 Don't bother to erase the cursor. */
23776 if (vpos >= active_glyphs->nrows)
23777 goto mark_cursor_off;
23779 /* If row containing cursor is marked invalid, there is nothing we
23780 can do. */
23781 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23782 if (!cursor_row->enabled_p)
23783 goto mark_cursor_off;
23785 /* If line spacing is > 0, old cursor may only be partially visible in
23786 window after split-window. So adjust visible height. */
23787 cursor_row->visible_height = min (cursor_row->visible_height,
23788 window_text_bottom_y (w) - cursor_row->y);
23790 /* If row is completely invisible, don't attempt to delete a cursor which
23791 isn't there. This can happen if cursor is at top of a window, and
23792 we switch to a buffer with a header line in that window. */
23793 if (cursor_row->visible_height <= 0)
23794 goto mark_cursor_off;
23796 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23797 if (cursor_row->cursor_in_fringe_p)
23799 cursor_row->cursor_in_fringe_p = 0;
23800 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23801 goto mark_cursor_off;
23804 /* This can happen when the new row is shorter than the old one.
23805 In this case, either draw_glyphs or clear_end_of_line
23806 should have cleared the cursor. Note that we wouldn't be
23807 able to erase the cursor in this case because we don't have a
23808 cursor glyph at hand. */
23809 if ((cursor_row->reversed_p
23810 ? (w->phys_cursor.hpos < 0)
23811 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23812 goto mark_cursor_off;
23814 /* If the cursor is in the mouse face area, redisplay that when
23815 we clear the cursor. */
23816 if (! NILP (hlinfo->mouse_face_window)
23817 && coords_in_mouse_face_p (w, hpos, vpos)
23818 /* Don't redraw the cursor's spot in mouse face if it is at the
23819 end of a line (on a newline). The cursor appears there, but
23820 mouse highlighting does not. */
23821 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23822 mouse_face_here_p = 1;
23824 /* Maybe clear the display under the cursor. */
23825 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23827 int x, y, left_x;
23828 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23829 int width;
23831 cursor_glyph = get_phys_cursor_glyph (w);
23832 if (cursor_glyph == NULL)
23833 goto mark_cursor_off;
23835 width = cursor_glyph->pixel_width;
23836 left_x = window_box_left_offset (w, TEXT_AREA);
23837 x = w->phys_cursor.x;
23838 if (x < left_x)
23839 width -= left_x - x;
23840 width = min (width, window_box_width (w, TEXT_AREA) - x);
23841 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23842 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23844 if (width > 0)
23845 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23848 /* Erase the cursor by redrawing the character underneath it. */
23849 if (mouse_face_here_p)
23850 hl = DRAW_MOUSE_FACE;
23851 else
23852 hl = DRAW_NORMAL_TEXT;
23853 draw_phys_cursor_glyph (w, cursor_row, hl);
23855 mark_cursor_off:
23856 w->phys_cursor_on_p = 0;
23857 w->phys_cursor_type = NO_CURSOR;
23861 /* EXPORT:
23862 Display or clear cursor of window W. If ON is zero, clear the
23863 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23864 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23866 void
23867 display_and_set_cursor (struct window *w, int on,
23868 int hpos, int vpos, int x, int y)
23870 struct frame *f = XFRAME (w->frame);
23871 int new_cursor_type;
23872 int new_cursor_width;
23873 int active_cursor;
23874 struct glyph_row *glyph_row;
23875 struct glyph *glyph;
23877 /* This is pointless on invisible frames, and dangerous on garbaged
23878 windows and frames; in the latter case, the frame or window may
23879 be in the midst of changing its size, and x and y may be off the
23880 window. */
23881 if (! FRAME_VISIBLE_P (f)
23882 || FRAME_GARBAGED_P (f)
23883 || vpos >= w->current_matrix->nrows
23884 || hpos >= w->current_matrix->matrix_w)
23885 return;
23887 /* If cursor is off and we want it off, return quickly. */
23888 if (!on && !w->phys_cursor_on_p)
23889 return;
23891 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23892 /* If cursor row is not enabled, we don't really know where to
23893 display the cursor. */
23894 if (!glyph_row->enabled_p)
23896 w->phys_cursor_on_p = 0;
23897 return;
23900 glyph = NULL;
23901 if (!glyph_row->exact_window_width_line_p
23902 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23903 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23905 xassert (interrupt_input_blocked);
23907 /* Set new_cursor_type to the cursor we want to be displayed. */
23908 new_cursor_type = get_window_cursor_type (w, glyph,
23909 &new_cursor_width, &active_cursor);
23911 /* If cursor is currently being shown and we don't want it to be or
23912 it is in the wrong place, or the cursor type is not what we want,
23913 erase it. */
23914 if (w->phys_cursor_on_p
23915 && (!on
23916 || w->phys_cursor.x != x
23917 || w->phys_cursor.y != y
23918 || new_cursor_type != w->phys_cursor_type
23919 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23920 && new_cursor_width != w->phys_cursor_width)))
23921 erase_phys_cursor (w);
23923 /* Don't check phys_cursor_on_p here because that flag is only set
23924 to zero in some cases where we know that the cursor has been
23925 completely erased, to avoid the extra work of erasing the cursor
23926 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23927 still not be visible, or it has only been partly erased. */
23928 if (on)
23930 w->phys_cursor_ascent = glyph_row->ascent;
23931 w->phys_cursor_height = glyph_row->height;
23933 /* Set phys_cursor_.* before x_draw_.* is called because some
23934 of them may need the information. */
23935 w->phys_cursor.x = x;
23936 w->phys_cursor.y = glyph_row->y;
23937 w->phys_cursor.hpos = hpos;
23938 w->phys_cursor.vpos = vpos;
23941 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23942 new_cursor_type, new_cursor_width,
23943 on, active_cursor);
23947 /* Switch the display of W's cursor on or off, according to the value
23948 of ON. */
23950 void
23951 update_window_cursor (struct window *w, int on)
23953 /* Don't update cursor in windows whose frame is in the process
23954 of being deleted. */
23955 if (w->current_matrix)
23957 BLOCK_INPUT;
23958 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23959 w->phys_cursor.x, w->phys_cursor.y);
23960 UNBLOCK_INPUT;
23965 /* Call update_window_cursor with parameter ON_P on all leaf windows
23966 in the window tree rooted at W. */
23968 static void
23969 update_cursor_in_window_tree (struct window *w, int on_p)
23971 while (w)
23973 if (!NILP (w->hchild))
23974 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23975 else if (!NILP (w->vchild))
23976 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23977 else
23978 update_window_cursor (w, on_p);
23980 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23985 /* EXPORT:
23986 Display the cursor on window W, or clear it, according to ON_P.
23987 Don't change the cursor's position. */
23989 void
23990 x_update_cursor (struct frame *f, int on_p)
23992 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23996 /* EXPORT:
23997 Clear the cursor of window W to background color, and mark the
23998 cursor as not shown. This is used when the text where the cursor
23999 is about to be rewritten. */
24001 void
24002 x_clear_cursor (struct window *w)
24004 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24005 update_window_cursor (w, 0);
24008 #endif /* HAVE_WINDOW_SYSTEM */
24010 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24011 and MSDOS. */
24012 void
24013 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24014 int start_hpos, int end_hpos,
24015 enum draw_glyphs_face draw)
24017 #ifdef HAVE_WINDOW_SYSTEM
24018 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24020 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24021 return;
24023 #endif
24024 #if defined (HAVE_GPM) || defined (MSDOS)
24025 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24026 #endif
24029 /* EXPORT:
24030 Display the active region described by mouse_face_* according to DRAW. */
24032 void
24033 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24035 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24036 struct frame *f = XFRAME (WINDOW_FRAME (w));
24038 if (/* If window is in the process of being destroyed, don't bother
24039 to do anything. */
24040 w->current_matrix != NULL
24041 /* Don't update mouse highlight if hidden */
24042 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24043 /* Recognize when we are called to operate on rows that don't exist
24044 anymore. This can happen when a window is split. */
24045 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24047 int phys_cursor_on_p = w->phys_cursor_on_p;
24048 struct glyph_row *row, *first, *last;
24050 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24051 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24053 for (row = first; row <= last && row->enabled_p; ++row)
24055 int start_hpos, end_hpos, start_x;
24057 /* For all but the first row, the highlight starts at column 0. */
24058 if (row == first)
24060 /* R2L rows have BEG and END in reversed order, but the
24061 screen drawing geometry is always left to right. So
24062 we need to mirror the beginning and end of the
24063 highlighted area in R2L rows. */
24064 if (!row->reversed_p)
24066 start_hpos = hlinfo->mouse_face_beg_col;
24067 start_x = hlinfo->mouse_face_beg_x;
24069 else if (row == last)
24071 start_hpos = hlinfo->mouse_face_end_col;
24072 start_x = hlinfo->mouse_face_end_x;
24074 else
24076 start_hpos = 0;
24077 start_x = 0;
24080 else if (row->reversed_p && row == last)
24082 start_hpos = hlinfo->mouse_face_end_col;
24083 start_x = hlinfo->mouse_face_end_x;
24085 else
24087 start_hpos = 0;
24088 start_x = 0;
24091 if (row == last)
24093 if (!row->reversed_p)
24094 end_hpos = hlinfo->mouse_face_end_col;
24095 else if (row == first)
24096 end_hpos = hlinfo->mouse_face_beg_col;
24097 else
24099 end_hpos = row->used[TEXT_AREA];
24100 if (draw == DRAW_NORMAL_TEXT)
24101 row->fill_line_p = 1; /* Clear to end of line */
24104 else if (row->reversed_p && row == first)
24105 end_hpos = hlinfo->mouse_face_beg_col;
24106 else
24108 end_hpos = row->used[TEXT_AREA];
24109 if (draw == DRAW_NORMAL_TEXT)
24110 row->fill_line_p = 1; /* Clear to end of line */
24113 if (end_hpos > start_hpos)
24115 draw_row_with_mouse_face (w, start_x, row,
24116 start_hpos, end_hpos, draw);
24118 row->mouse_face_p
24119 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24123 #ifdef HAVE_WINDOW_SYSTEM
24124 /* When we've written over the cursor, arrange for it to
24125 be displayed again. */
24126 if (FRAME_WINDOW_P (f)
24127 && phys_cursor_on_p && !w->phys_cursor_on_p)
24129 BLOCK_INPUT;
24130 display_and_set_cursor (w, 1,
24131 w->phys_cursor.hpos, w->phys_cursor.vpos,
24132 w->phys_cursor.x, w->phys_cursor.y);
24133 UNBLOCK_INPUT;
24135 #endif /* HAVE_WINDOW_SYSTEM */
24138 #ifdef HAVE_WINDOW_SYSTEM
24139 /* Change the mouse cursor. */
24140 if (FRAME_WINDOW_P (f))
24142 if (draw == DRAW_NORMAL_TEXT
24143 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24144 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24145 else if (draw == DRAW_MOUSE_FACE)
24146 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24147 else
24148 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24150 #endif /* HAVE_WINDOW_SYSTEM */
24153 /* EXPORT:
24154 Clear out the mouse-highlighted active region.
24155 Redraw it un-highlighted first. Value is non-zero if mouse
24156 face was actually drawn unhighlighted. */
24159 clear_mouse_face (Mouse_HLInfo *hlinfo)
24161 int cleared = 0;
24163 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24165 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24166 cleared = 1;
24169 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24170 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24171 hlinfo->mouse_face_window = Qnil;
24172 hlinfo->mouse_face_overlay = Qnil;
24173 return cleared;
24176 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24177 within the mouse face on that window. */
24178 static int
24179 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24181 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24183 /* Quickly resolve the easy cases. */
24184 if (!(WINDOWP (hlinfo->mouse_face_window)
24185 && XWINDOW (hlinfo->mouse_face_window) == w))
24186 return 0;
24187 if (vpos < hlinfo->mouse_face_beg_row
24188 || vpos > hlinfo->mouse_face_end_row)
24189 return 0;
24190 if (vpos > hlinfo->mouse_face_beg_row
24191 && vpos < hlinfo->mouse_face_end_row)
24192 return 1;
24194 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24196 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24198 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24199 return 1;
24201 else if ((vpos == hlinfo->mouse_face_beg_row
24202 && hpos >= hlinfo->mouse_face_beg_col)
24203 || (vpos == hlinfo->mouse_face_end_row
24204 && hpos < hlinfo->mouse_face_end_col))
24205 return 1;
24207 else
24209 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24211 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24212 return 1;
24214 else if ((vpos == hlinfo->mouse_face_beg_row
24215 && hpos <= hlinfo->mouse_face_beg_col)
24216 || (vpos == hlinfo->mouse_face_end_row
24217 && hpos > hlinfo->mouse_face_end_col))
24218 return 1;
24220 return 0;
24224 /* EXPORT:
24225 Non-zero if physical cursor of window W is within mouse face. */
24228 cursor_in_mouse_face_p (struct window *w)
24230 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24235 /* Find the glyph rows START_ROW and END_ROW of window W that display
24236 characters between buffer positions START_CHARPOS and END_CHARPOS
24237 (excluding END_CHARPOS). This is similar to row_containing_pos,
24238 but is more accurate when bidi reordering makes buffer positions
24239 change non-linearly with glyph rows. */
24240 static void
24241 rows_from_pos_range (struct window *w,
24242 EMACS_INT start_charpos, EMACS_INT end_charpos,
24243 struct glyph_row **start, struct glyph_row **end)
24245 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24246 int last_y = window_text_bottom_y (w);
24247 struct glyph_row *row;
24249 *start = NULL;
24250 *end = NULL;
24252 while (!first->enabled_p
24253 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24254 first++;
24256 /* Find the START row. */
24257 for (row = first;
24258 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24259 row++)
24261 /* A row can potentially be the START row if the range of the
24262 characters it displays intersects the range
24263 [START_CHARPOS..END_CHARPOS). */
24264 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24265 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24266 /* See the commentary in row_containing_pos, for the
24267 explanation of the complicated way to check whether
24268 some position is beyond the end of the characters
24269 displayed by a row. */
24270 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24271 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24272 && !row->ends_at_zv_p
24273 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24274 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24275 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24276 && !row->ends_at_zv_p
24277 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24279 /* Found a candidate row. Now make sure at least one of the
24280 glyphs it displays has a charpos from the range
24281 [START_CHARPOS..END_CHARPOS).
24283 This is not obvious because bidi reordering could make
24284 buffer positions of a row be 1,2,3,102,101,100, and if we
24285 want to highlight characters in [50..60), we don't want
24286 this row, even though [50..60) does intersect [1..103),
24287 the range of character positions given by the row's start
24288 and end positions. */
24289 struct glyph *g = row->glyphs[TEXT_AREA];
24290 struct glyph *e = g + row->used[TEXT_AREA];
24292 while (g < e)
24294 if (BUFFERP (g->object)
24295 && start_charpos <= g->charpos && g->charpos < end_charpos)
24296 *start = row;
24297 g++;
24299 if (*start)
24300 break;
24304 /* Find the END row. */
24305 if (!*start
24306 /* If the last row is partially visible, start looking for END
24307 from that row, instead of starting from FIRST. */
24308 && !(row->enabled_p
24309 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24310 row = first;
24311 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24313 struct glyph_row *next = row + 1;
24315 if (!next->enabled_p
24316 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24317 /* The first row >= START whose range of displayed characters
24318 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24319 is the row END + 1. */
24320 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24321 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24322 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24323 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24324 && !next->ends_at_zv_p
24325 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24326 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24327 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24328 && !next->ends_at_zv_p
24329 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24331 *end = row;
24332 break;
24334 else
24336 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24337 but none of the characters it displays are in the range, it is
24338 also END + 1. */
24339 struct glyph *g = next->glyphs[TEXT_AREA];
24340 struct glyph *e = g + next->used[TEXT_AREA];
24342 while (g < e)
24344 if (BUFFERP (g->object)
24345 && start_charpos <= g->charpos && g->charpos < end_charpos)
24346 break;
24347 g++;
24349 if (g == e)
24351 *end = row;
24352 break;
24358 /* This function sets the mouse_face_* elements of HLINFO, assuming
24359 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24360 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24361 for the overlay or run of text properties specifying the mouse
24362 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24363 before-string and after-string that must also be highlighted.
24364 DISPLAY_STRING, if non-nil, is a display string that may cover some
24365 or all of the highlighted text. */
24367 static void
24368 mouse_face_from_buffer_pos (Lisp_Object window,
24369 Mouse_HLInfo *hlinfo,
24370 EMACS_INT mouse_charpos,
24371 EMACS_INT start_charpos,
24372 EMACS_INT end_charpos,
24373 Lisp_Object before_string,
24374 Lisp_Object after_string,
24375 Lisp_Object display_string)
24377 struct window *w = XWINDOW (window);
24378 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24379 struct glyph_row *r1, *r2;
24380 struct glyph *glyph, *end;
24381 EMACS_INT ignore, pos;
24382 int x;
24384 xassert (NILP (display_string) || STRINGP (display_string));
24385 xassert (NILP (before_string) || STRINGP (before_string));
24386 xassert (NILP (after_string) || STRINGP (after_string));
24388 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24389 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24390 if (r1 == NULL)
24391 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24392 /* If the before-string or display-string contains newlines,
24393 rows_from_pos_range skips to its last row. Move back. */
24394 if (!NILP (before_string) || !NILP (display_string))
24396 struct glyph_row *prev;
24397 while ((prev = r1 - 1, prev >= first)
24398 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24399 && prev->used[TEXT_AREA] > 0)
24401 struct glyph *beg = prev->glyphs[TEXT_AREA];
24402 glyph = beg + prev->used[TEXT_AREA];
24403 while (--glyph >= beg && INTEGERP (glyph->object));
24404 if (glyph < beg
24405 || !(EQ (glyph->object, before_string)
24406 || EQ (glyph->object, display_string)))
24407 break;
24408 r1 = prev;
24411 if (r2 == NULL)
24413 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24414 hlinfo->mouse_face_past_end = 1;
24416 else if (!NILP (after_string))
24418 /* If the after-string has newlines, advance to its last row. */
24419 struct glyph_row *next;
24420 struct glyph_row *last
24421 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24423 for (next = r2 + 1;
24424 next <= last
24425 && next->used[TEXT_AREA] > 0
24426 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24427 ++next)
24428 r2 = next;
24430 /* The rest of the display engine assumes that mouse_face_beg_row is
24431 either above below mouse_face_end_row or identical to it. But
24432 with bidi-reordered continued lines, the row for START_CHARPOS
24433 could be below the row for END_CHARPOS. If so, swap the rows and
24434 store them in correct order. */
24435 if (r1->y > r2->y)
24437 struct glyph_row *tem = r2;
24439 r2 = r1;
24440 r1 = tem;
24443 hlinfo->mouse_face_beg_y = r1->y;
24444 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24445 hlinfo->mouse_face_end_y = r2->y;
24446 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24448 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24449 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
24450 could be anywhere in the row and in any order. The strategy
24451 below is to find the leftmost and the rightmost glyph that
24452 belongs to either of these 3 strings, or whose position is
24453 between START_CHARPOS and END_CHARPOS, and highlight all the
24454 glyphs between those two. This may cover more than just the text
24455 between START_CHARPOS and END_CHARPOS if the range of characters
24456 strides the bidi level boundary, e.g. if the beginning is in R2L
24457 text while the end is in L2R text or vice versa. */
24458 if (!r1->reversed_p)
24460 /* This row is in a left to right paragraph. Scan it left to
24461 right. */
24462 glyph = r1->glyphs[TEXT_AREA];
24463 end = glyph + r1->used[TEXT_AREA];
24464 x = r1->x;
24466 /* Skip truncation glyphs at the start of the glyph row. */
24467 if (r1->displays_text_p)
24468 for (; glyph < end
24469 && INTEGERP (glyph->object)
24470 && glyph->charpos < 0;
24471 ++glyph)
24472 x += glyph->pixel_width;
24474 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24475 or DISPLAY_STRING, and the first glyph from buffer whose
24476 position is between START_CHARPOS and END_CHARPOS. */
24477 for (; glyph < end
24478 && !INTEGERP (glyph->object)
24479 && !EQ (glyph->object, display_string)
24480 && !(BUFFERP (glyph->object)
24481 && (glyph->charpos >= start_charpos
24482 && glyph->charpos < end_charpos));
24483 ++glyph)
24485 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24486 are present at buffer positions between START_CHARPOS and
24487 END_CHARPOS, or if they come from an overlay. */
24488 if (EQ (glyph->object, before_string))
24490 pos = string_buffer_position (w, before_string,
24491 start_charpos);
24492 /* If pos == 0, it means before_string came from an
24493 overlay, not from a buffer position. */
24494 if (!pos || (pos >= start_charpos && pos < end_charpos))
24495 break;
24497 else if (EQ (glyph->object, after_string))
24499 pos = string_buffer_position (w, after_string, end_charpos);
24500 if (!pos || (pos >= start_charpos && pos < end_charpos))
24501 break;
24503 x += glyph->pixel_width;
24505 hlinfo->mouse_face_beg_x = x;
24506 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24508 else
24510 /* This row is in a right to left paragraph. Scan it right to
24511 left. */
24512 struct glyph *g;
24514 end = r1->glyphs[TEXT_AREA] - 1;
24515 glyph = end + r1->used[TEXT_AREA];
24517 /* Skip truncation glyphs at the start of the glyph row. */
24518 if (r1->displays_text_p)
24519 for (; glyph > end
24520 && INTEGERP (glyph->object)
24521 && glyph->charpos < 0;
24522 --glyph)
24525 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24526 or DISPLAY_STRING, and the first glyph from buffer whose
24527 position is between START_CHARPOS and END_CHARPOS. */
24528 for (; glyph > end
24529 && !INTEGERP (glyph->object)
24530 && !EQ (glyph->object, display_string)
24531 && !(BUFFERP (glyph->object)
24532 && (glyph->charpos >= start_charpos
24533 && glyph->charpos < end_charpos));
24534 --glyph)
24536 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24537 are present at buffer positions between START_CHARPOS and
24538 END_CHARPOS, or if they come from an overlay. */
24539 if (EQ (glyph->object, before_string))
24541 pos = string_buffer_position (w, before_string, start_charpos);
24542 /* If pos == 0, it means before_string came from an
24543 overlay, not from a buffer position. */
24544 if (!pos || (pos >= start_charpos && pos < end_charpos))
24545 break;
24547 else if (EQ (glyph->object, after_string))
24549 pos = string_buffer_position (w, after_string, end_charpos);
24550 if (!pos || (pos >= start_charpos && pos < end_charpos))
24551 break;
24555 glyph++; /* first glyph to the right of the highlighted area */
24556 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24557 x += g->pixel_width;
24558 hlinfo->mouse_face_beg_x = x;
24559 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24562 /* If the highlight ends in a different row, compute GLYPH and END
24563 for the end row. Otherwise, reuse the values computed above for
24564 the row where the highlight begins. */
24565 if (r2 != r1)
24567 if (!r2->reversed_p)
24569 glyph = r2->glyphs[TEXT_AREA];
24570 end = glyph + r2->used[TEXT_AREA];
24571 x = r2->x;
24573 else
24575 end = r2->glyphs[TEXT_AREA] - 1;
24576 glyph = end + r2->used[TEXT_AREA];
24580 if (!r2->reversed_p)
24582 /* Skip truncation and continuation glyphs near the end of the
24583 row, and also blanks and stretch glyphs inserted by
24584 extend_face_to_end_of_line. */
24585 while (end > glyph
24586 && INTEGERP ((end - 1)->object)
24587 && (end - 1)->charpos <= 0)
24588 --end;
24589 /* Scan the rest of the glyph row from the end, looking for the
24590 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24591 DISPLAY_STRING, or whose position is between START_CHARPOS
24592 and END_CHARPOS */
24593 for (--end;
24594 end > glyph
24595 && !INTEGERP (end->object)
24596 && !EQ (end->object, display_string)
24597 && !(BUFFERP (end->object)
24598 && (end->charpos >= start_charpos
24599 && end->charpos < end_charpos));
24600 --end)
24602 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24603 are present at buffer positions between START_CHARPOS and
24604 END_CHARPOS, or if they come from an overlay. */
24605 if (EQ (end->object, before_string))
24607 pos = string_buffer_position (w, before_string, start_charpos);
24608 if (!pos || (pos >= start_charpos && pos < end_charpos))
24609 break;
24611 else if (EQ (end->object, after_string))
24613 pos = string_buffer_position (w, after_string, end_charpos);
24614 if (!pos || (pos >= start_charpos && pos < end_charpos))
24615 break;
24618 /* Find the X coordinate of the last glyph to be highlighted. */
24619 for (; glyph <= end; ++glyph)
24620 x += glyph->pixel_width;
24622 hlinfo->mouse_face_end_x = x;
24623 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24625 else
24627 /* Skip truncation and continuation glyphs near the end of the
24628 row, and also blanks and stretch glyphs inserted by
24629 extend_face_to_end_of_line. */
24630 x = r2->x;
24631 end++;
24632 while (end < glyph
24633 && INTEGERP (end->object)
24634 && end->charpos <= 0)
24636 x += end->pixel_width;
24637 ++end;
24639 /* Scan the rest of the glyph row from the end, looking for the
24640 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24641 DISPLAY_STRING, or whose position is between START_CHARPOS
24642 and END_CHARPOS */
24643 for ( ;
24644 end < glyph
24645 && !INTEGERP (end->object)
24646 && !EQ (end->object, display_string)
24647 && !(BUFFERP (end->object)
24648 && (end->charpos >= start_charpos
24649 && end->charpos < end_charpos));
24650 ++end)
24652 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24653 are present at buffer positions between START_CHARPOS and
24654 END_CHARPOS, or if they come from an overlay. */
24655 if (EQ (end->object, before_string))
24657 pos = string_buffer_position (w, before_string, start_charpos);
24658 if (!pos || (pos >= start_charpos && pos < end_charpos))
24659 break;
24661 else if (EQ (end->object, after_string))
24663 pos = string_buffer_position (w, after_string, end_charpos);
24664 if (!pos || (pos >= start_charpos && pos < end_charpos))
24665 break;
24667 x += end->pixel_width;
24669 hlinfo->mouse_face_end_x = x;
24670 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24673 hlinfo->mouse_face_window = window;
24674 hlinfo->mouse_face_face_id
24675 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24676 mouse_charpos + 1,
24677 !hlinfo->mouse_face_hidden, -1);
24678 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24681 /* The following function is not used anymore (replaced with
24682 mouse_face_from_string_pos), but I leave it here for the time
24683 being, in case someone would. */
24685 #if 0 /* not used */
24687 /* Find the position of the glyph for position POS in OBJECT in
24688 window W's current matrix, and return in *X, *Y the pixel
24689 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24691 RIGHT_P non-zero means return the position of the right edge of the
24692 glyph, RIGHT_P zero means return the left edge position.
24694 If no glyph for POS exists in the matrix, return the position of
24695 the glyph with the next smaller position that is in the matrix, if
24696 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24697 exists in the matrix, return the position of the glyph with the
24698 next larger position in OBJECT.
24700 Value is non-zero if a glyph was found. */
24702 static int
24703 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24704 int *hpos, int *vpos, int *x, int *y, int right_p)
24706 int yb = window_text_bottom_y (w);
24707 struct glyph_row *r;
24708 struct glyph *best_glyph = NULL;
24709 struct glyph_row *best_row = NULL;
24710 int best_x = 0;
24712 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24713 r->enabled_p && r->y < yb;
24714 ++r)
24716 struct glyph *g = r->glyphs[TEXT_AREA];
24717 struct glyph *e = g + r->used[TEXT_AREA];
24718 int gx;
24720 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24721 if (EQ (g->object, object))
24723 if (g->charpos == pos)
24725 best_glyph = g;
24726 best_x = gx;
24727 best_row = r;
24728 goto found;
24730 else if (best_glyph == NULL
24731 || ((eabs (g->charpos - pos)
24732 < eabs (best_glyph->charpos - pos))
24733 && (right_p
24734 ? g->charpos < pos
24735 : g->charpos > pos)))
24737 best_glyph = g;
24738 best_x = gx;
24739 best_row = r;
24744 found:
24746 if (best_glyph)
24748 *x = best_x;
24749 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24751 if (right_p)
24753 *x += best_glyph->pixel_width;
24754 ++*hpos;
24757 *y = best_row->y;
24758 *vpos = best_row - w->current_matrix->rows;
24761 return best_glyph != NULL;
24763 #endif /* not used */
24765 /* Find the positions of the first and the last glyphs in window W's
24766 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24767 (assumed to be a string), and return in HLINFO's mouse_face_*
24768 members the pixel and column/row coordinates of those glyphs. */
24770 static void
24771 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24772 Lisp_Object object,
24773 EMACS_INT startpos, EMACS_INT endpos)
24775 int yb = window_text_bottom_y (w);
24776 struct glyph_row *r;
24777 struct glyph *g, *e;
24778 int gx;
24779 int found = 0;
24781 /* Find the glyph row with at least one position in the range
24782 [STARTPOS..ENDPOS], and the first glyph in that row whose
24783 position belongs to that range. */
24784 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24785 r->enabled_p && r->y < yb;
24786 ++r)
24788 if (!r->reversed_p)
24790 g = r->glyphs[TEXT_AREA];
24791 e = g + r->used[TEXT_AREA];
24792 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24793 if (EQ (g->object, object)
24794 && startpos <= g->charpos && g->charpos <= endpos)
24796 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24797 hlinfo->mouse_face_beg_y = r->y;
24798 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24799 hlinfo->mouse_face_beg_x = gx;
24800 found = 1;
24801 break;
24804 else
24806 struct glyph *g1;
24808 e = r->glyphs[TEXT_AREA];
24809 g = e + r->used[TEXT_AREA];
24810 for ( ; g > e; --g)
24811 if (EQ ((g-1)->object, object)
24812 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24814 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24815 hlinfo->mouse_face_beg_y = r->y;
24816 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24817 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24818 gx += g1->pixel_width;
24819 hlinfo->mouse_face_beg_x = gx;
24820 found = 1;
24821 break;
24824 if (found)
24825 break;
24828 if (!found)
24829 return;
24831 /* Starting with the next row, look for the first row which does NOT
24832 include any glyphs whose positions are in the range. */
24833 for (++r; r->enabled_p && r->y < yb; ++r)
24835 g = r->glyphs[TEXT_AREA];
24836 e = g + r->used[TEXT_AREA];
24837 found = 0;
24838 for ( ; g < e; ++g)
24839 if (EQ (g->object, object)
24840 && startpos <= g->charpos && g->charpos <= endpos)
24842 found = 1;
24843 break;
24845 if (!found)
24846 break;
24849 /* The highlighted region ends on the previous row. */
24850 r--;
24852 /* Set the end row and its vertical pixel coordinate. */
24853 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24854 hlinfo->mouse_face_end_y = r->y;
24856 /* Compute and set the end column and the end column's horizontal
24857 pixel coordinate. */
24858 if (!r->reversed_p)
24860 g = r->glyphs[TEXT_AREA];
24861 e = g + r->used[TEXT_AREA];
24862 for ( ; e > g; --e)
24863 if (EQ ((e-1)->object, object)
24864 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24865 break;
24866 hlinfo->mouse_face_end_col = e - g;
24868 for (gx = r->x; g < e; ++g)
24869 gx += g->pixel_width;
24870 hlinfo->mouse_face_end_x = gx;
24872 else
24874 e = r->glyphs[TEXT_AREA];
24875 g = e + r->used[TEXT_AREA];
24876 for (gx = r->x ; e < g; ++e)
24878 if (EQ (e->object, object)
24879 && startpos <= e->charpos && e->charpos <= endpos)
24880 break;
24881 gx += e->pixel_width;
24883 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24884 hlinfo->mouse_face_end_x = gx;
24888 #ifdef HAVE_WINDOW_SYSTEM
24890 /* See if position X, Y is within a hot-spot of an image. */
24892 static int
24893 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24895 if (!CONSP (hot_spot))
24896 return 0;
24898 if (EQ (XCAR (hot_spot), Qrect))
24900 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24901 Lisp_Object rect = XCDR (hot_spot);
24902 Lisp_Object tem;
24903 if (!CONSP (rect))
24904 return 0;
24905 if (!CONSP (XCAR (rect)))
24906 return 0;
24907 if (!CONSP (XCDR (rect)))
24908 return 0;
24909 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24910 return 0;
24911 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24912 return 0;
24913 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24914 return 0;
24915 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24916 return 0;
24917 return 1;
24919 else if (EQ (XCAR (hot_spot), Qcircle))
24921 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24922 Lisp_Object circ = XCDR (hot_spot);
24923 Lisp_Object lr, lx0, ly0;
24924 if (CONSP (circ)
24925 && CONSP (XCAR (circ))
24926 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24927 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24928 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24930 double r = XFLOATINT (lr);
24931 double dx = XINT (lx0) - x;
24932 double dy = XINT (ly0) - y;
24933 return (dx * dx + dy * dy <= r * r);
24936 else if (EQ (XCAR (hot_spot), Qpoly))
24938 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24939 if (VECTORP (XCDR (hot_spot)))
24941 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24942 Lisp_Object *poly = v->contents;
24943 int n = v->size;
24944 int i;
24945 int inside = 0;
24946 Lisp_Object lx, ly;
24947 int x0, y0;
24949 /* Need an even number of coordinates, and at least 3 edges. */
24950 if (n < 6 || n & 1)
24951 return 0;
24953 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24954 If count is odd, we are inside polygon. Pixels on edges
24955 may or may not be included depending on actual geometry of the
24956 polygon. */
24957 if ((lx = poly[n-2], !INTEGERP (lx))
24958 || (ly = poly[n-1], !INTEGERP (lx)))
24959 return 0;
24960 x0 = XINT (lx), y0 = XINT (ly);
24961 for (i = 0; i < n; i += 2)
24963 int x1 = x0, y1 = y0;
24964 if ((lx = poly[i], !INTEGERP (lx))
24965 || (ly = poly[i+1], !INTEGERP (ly)))
24966 return 0;
24967 x0 = XINT (lx), y0 = XINT (ly);
24969 /* Does this segment cross the X line? */
24970 if (x0 >= x)
24972 if (x1 >= x)
24973 continue;
24975 else if (x1 < x)
24976 continue;
24977 if (y > y0 && y > y1)
24978 continue;
24979 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24980 inside = !inside;
24982 return inside;
24985 return 0;
24988 Lisp_Object
24989 find_hot_spot (Lisp_Object map, int x, int y)
24991 while (CONSP (map))
24993 if (CONSP (XCAR (map))
24994 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24995 return XCAR (map);
24996 map = XCDR (map);
24999 return Qnil;
25002 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25003 3, 3, 0,
25004 doc: /* Lookup in image map MAP coordinates X and Y.
25005 An image map is an alist where each element has the format (AREA ID PLIST).
25006 An AREA is specified as either a rectangle, a circle, or a polygon:
25007 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25008 pixel coordinates of the upper left and bottom right corners.
25009 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25010 and the radius of the circle; r may be a float or integer.
25011 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25012 vector describes one corner in the polygon.
25013 Returns the alist element for the first matching AREA in MAP. */)
25014 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25016 if (NILP (map))
25017 return Qnil;
25019 CHECK_NUMBER (x);
25020 CHECK_NUMBER (y);
25022 return find_hot_spot (map, XINT (x), XINT (y));
25026 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25027 static void
25028 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25030 /* Do not change cursor shape while dragging mouse. */
25031 if (!NILP (do_mouse_tracking))
25032 return;
25034 if (!NILP (pointer))
25036 if (EQ (pointer, Qarrow))
25037 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25038 else if (EQ (pointer, Qhand))
25039 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25040 else if (EQ (pointer, Qtext))
25041 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25042 else if (EQ (pointer, intern ("hdrag")))
25043 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25044 #ifdef HAVE_X_WINDOWS
25045 else if (EQ (pointer, intern ("vdrag")))
25046 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25047 #endif
25048 else if (EQ (pointer, intern ("hourglass")))
25049 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25050 else if (EQ (pointer, Qmodeline))
25051 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25052 else
25053 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25056 if (cursor != No_Cursor)
25057 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25060 #endif /* HAVE_WINDOW_SYSTEM */
25062 /* Take proper action when mouse has moved to the mode or header line
25063 or marginal area AREA of window W, x-position X and y-position Y.
25064 X is relative to the start of the text display area of W, so the
25065 width of bitmap areas and scroll bars must be subtracted to get a
25066 position relative to the start of the mode line. */
25068 static void
25069 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25070 enum window_part area)
25072 struct window *w = XWINDOW (window);
25073 struct frame *f = XFRAME (w->frame);
25074 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25075 #ifdef HAVE_WINDOW_SYSTEM
25076 Display_Info *dpyinfo;
25077 #endif
25078 Cursor cursor = No_Cursor;
25079 Lisp_Object pointer = Qnil;
25080 int dx, dy, width, height;
25081 EMACS_INT charpos;
25082 Lisp_Object string, object = Qnil;
25083 Lisp_Object pos, help;
25085 Lisp_Object mouse_face;
25086 int original_x_pixel = x;
25087 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25088 struct glyph_row *row;
25090 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25092 int x0;
25093 struct glyph *end;
25095 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25096 returns them in row/column units! */
25097 string = mode_line_string (w, area, &x, &y, &charpos,
25098 &object, &dx, &dy, &width, &height);
25100 row = (area == ON_MODE_LINE
25101 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25102 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25104 /* Find the glyph under the mouse pointer. */
25105 if (row->mode_line_p && row->enabled_p)
25107 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25108 end = glyph + row->used[TEXT_AREA];
25110 for (x0 = original_x_pixel;
25111 glyph < end && x0 >= glyph->pixel_width;
25112 ++glyph)
25113 x0 -= glyph->pixel_width;
25115 if (glyph >= end)
25116 glyph = NULL;
25119 else
25121 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25122 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25123 returns them in row/column units! */
25124 string = marginal_area_string (w, area, &x, &y, &charpos,
25125 &object, &dx, &dy, &width, &height);
25128 help = Qnil;
25130 #ifdef HAVE_WINDOW_SYSTEM
25131 if (IMAGEP (object))
25133 Lisp_Object image_map, hotspot;
25134 if ((image_map = Fplist_get (XCDR (object), QCmap),
25135 !NILP (image_map))
25136 && (hotspot = find_hot_spot (image_map, dx, dy),
25137 CONSP (hotspot))
25138 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25140 Lisp_Object area_id, plist;
25142 area_id = XCAR (hotspot);
25143 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25144 If so, we could look for mouse-enter, mouse-leave
25145 properties in PLIST (and do something...). */
25146 hotspot = XCDR (hotspot);
25147 if (CONSP (hotspot)
25148 && (plist = XCAR (hotspot), CONSP (plist)))
25150 pointer = Fplist_get (plist, Qpointer);
25151 if (NILP (pointer))
25152 pointer = Qhand;
25153 help = Fplist_get (plist, Qhelp_echo);
25154 if (!NILP (help))
25156 help_echo_string = help;
25157 /* Is this correct? ++kfs */
25158 XSETWINDOW (help_echo_window, w);
25159 help_echo_object = w->buffer;
25160 help_echo_pos = charpos;
25164 if (NILP (pointer))
25165 pointer = Fplist_get (XCDR (object), QCpointer);
25167 #endif /* HAVE_WINDOW_SYSTEM */
25169 if (STRINGP (string))
25171 pos = make_number (charpos);
25172 /* If we're on a string with `help-echo' text property, arrange
25173 for the help to be displayed. This is done by setting the
25174 global variable help_echo_string to the help string. */
25175 if (NILP (help))
25177 help = Fget_text_property (pos, Qhelp_echo, string);
25178 if (!NILP (help))
25180 help_echo_string = help;
25181 XSETWINDOW (help_echo_window, w);
25182 help_echo_object = string;
25183 help_echo_pos = charpos;
25187 #ifdef HAVE_WINDOW_SYSTEM
25188 if (FRAME_WINDOW_P (f))
25190 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25191 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25192 if (NILP (pointer))
25193 pointer = Fget_text_property (pos, Qpointer, string);
25195 /* Change the mouse pointer according to what is under X/Y. */
25196 if (NILP (pointer)
25197 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25199 Lisp_Object map;
25200 map = Fget_text_property (pos, Qlocal_map, string);
25201 if (!KEYMAPP (map))
25202 map = Fget_text_property (pos, Qkeymap, string);
25203 if (!KEYMAPP (map))
25204 cursor = dpyinfo->vertical_scroll_bar_cursor;
25207 #endif
25209 /* Change the mouse face according to what is under X/Y. */
25210 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25211 if (!NILP (mouse_face)
25212 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25213 && glyph)
25215 Lisp_Object b, e;
25217 struct glyph * tmp_glyph;
25219 int gpos;
25220 int gseq_length;
25221 int total_pixel_width;
25222 EMACS_INT begpos, endpos, ignore;
25224 int vpos, hpos;
25226 b = Fprevious_single_property_change (make_number (charpos + 1),
25227 Qmouse_face, string, Qnil);
25228 if (NILP (b))
25229 begpos = 0;
25230 else
25231 begpos = XINT (b);
25233 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25234 if (NILP (e))
25235 endpos = SCHARS (string);
25236 else
25237 endpos = XINT (e);
25239 /* Calculate the glyph position GPOS of GLYPH in the
25240 displayed string, relative to the beginning of the
25241 highlighted part of the string.
25243 Note: GPOS is different from CHARPOS. CHARPOS is the
25244 position of GLYPH in the internal string object. A mode
25245 line string format has structures which are converted to
25246 a flattened string by the Emacs Lisp interpreter. The
25247 internal string is an element of those structures. The
25248 displayed string is the flattened string. */
25249 tmp_glyph = row_start_glyph;
25250 while (tmp_glyph < glyph
25251 && (!(EQ (tmp_glyph->object, glyph->object)
25252 && begpos <= tmp_glyph->charpos
25253 && tmp_glyph->charpos < endpos)))
25254 tmp_glyph++;
25255 gpos = glyph - tmp_glyph;
25257 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25258 the highlighted part of the displayed string to which
25259 GLYPH belongs. Note: GSEQ_LENGTH is different from
25260 SCHARS (STRING), because the latter returns the length of
25261 the internal string. */
25262 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25263 tmp_glyph > glyph
25264 && (!(EQ (tmp_glyph->object, glyph->object)
25265 && begpos <= tmp_glyph->charpos
25266 && tmp_glyph->charpos < endpos));
25267 tmp_glyph--)
25269 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25271 /* Calculate the total pixel width of all the glyphs between
25272 the beginning of the highlighted area and GLYPH. */
25273 total_pixel_width = 0;
25274 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25275 total_pixel_width += tmp_glyph->pixel_width;
25277 /* Pre calculation of re-rendering position. Note: X is in
25278 column units here, after the call to mode_line_string or
25279 marginal_area_string. */
25280 hpos = x - gpos;
25281 vpos = (area == ON_MODE_LINE
25282 ? (w->current_matrix)->nrows - 1
25283 : 0);
25285 /* If GLYPH's position is included in the region that is
25286 already drawn in mouse face, we have nothing to do. */
25287 if ( EQ (window, hlinfo->mouse_face_window)
25288 && (!row->reversed_p
25289 ? (hlinfo->mouse_face_beg_col <= hpos
25290 && hpos < hlinfo->mouse_face_end_col)
25291 /* In R2L rows we swap BEG and END, see below. */
25292 : (hlinfo->mouse_face_end_col <= hpos
25293 && hpos < hlinfo->mouse_face_beg_col))
25294 && hlinfo->mouse_face_beg_row == vpos )
25295 return;
25297 if (clear_mouse_face (hlinfo))
25298 cursor = No_Cursor;
25300 if (!row->reversed_p)
25302 hlinfo->mouse_face_beg_col = hpos;
25303 hlinfo->mouse_face_beg_x = original_x_pixel
25304 - (total_pixel_width + dx);
25305 hlinfo->mouse_face_end_col = hpos + gseq_length;
25306 hlinfo->mouse_face_end_x = 0;
25308 else
25310 /* In R2L rows, show_mouse_face expects BEG and END
25311 coordinates to be swapped. */
25312 hlinfo->mouse_face_end_col = hpos;
25313 hlinfo->mouse_face_end_x = original_x_pixel
25314 - (total_pixel_width + dx);
25315 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25316 hlinfo->mouse_face_beg_x = 0;
25319 hlinfo->mouse_face_beg_row = vpos;
25320 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25321 hlinfo->mouse_face_beg_y = 0;
25322 hlinfo->mouse_face_end_y = 0;
25323 hlinfo->mouse_face_past_end = 0;
25324 hlinfo->mouse_face_window = window;
25326 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25327 charpos,
25328 0, 0, 0,
25329 &ignore,
25330 glyph->face_id,
25332 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25334 if (NILP (pointer))
25335 pointer = Qhand;
25337 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25338 clear_mouse_face (hlinfo);
25340 #ifdef HAVE_WINDOW_SYSTEM
25341 if (FRAME_WINDOW_P (f))
25342 define_frame_cursor1 (f, cursor, pointer);
25343 #endif
25347 /* EXPORT:
25348 Take proper action when the mouse has moved to position X, Y on
25349 frame F as regards highlighting characters that have mouse-face
25350 properties. Also de-highlighting chars where the mouse was before.
25351 X and Y can be negative or out of range. */
25353 void
25354 note_mouse_highlight (struct frame *f, int x, int y)
25356 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25357 enum window_part part;
25358 Lisp_Object window;
25359 struct window *w;
25360 Cursor cursor = No_Cursor;
25361 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25362 struct buffer *b;
25364 /* When a menu is active, don't highlight because this looks odd. */
25365 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25366 if (popup_activated ())
25367 return;
25368 #endif
25370 if (NILP (Vmouse_highlight)
25371 || !f->glyphs_initialized_p
25372 || f->pointer_invisible)
25373 return;
25375 hlinfo->mouse_face_mouse_x = x;
25376 hlinfo->mouse_face_mouse_y = y;
25377 hlinfo->mouse_face_mouse_frame = f;
25379 if (hlinfo->mouse_face_defer)
25380 return;
25382 if (gc_in_progress)
25384 hlinfo->mouse_face_deferred_gc = 1;
25385 return;
25388 /* Which window is that in? */
25389 window = window_from_coordinates (f, x, y, &part, 1);
25391 /* If we were displaying active text in another window, clear that.
25392 Also clear if we move out of text area in same window. */
25393 if (! EQ (window, hlinfo->mouse_face_window)
25394 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25395 && !NILP (hlinfo->mouse_face_window)))
25396 clear_mouse_face (hlinfo);
25398 /* Not on a window -> return. */
25399 if (!WINDOWP (window))
25400 return;
25402 /* Reset help_echo_string. It will get recomputed below. */
25403 help_echo_string = Qnil;
25405 /* Convert to window-relative pixel coordinates. */
25406 w = XWINDOW (window);
25407 frame_to_window_pixel_xy (w, &x, &y);
25409 #ifdef HAVE_WINDOW_SYSTEM
25410 /* Handle tool-bar window differently since it doesn't display a
25411 buffer. */
25412 if (EQ (window, f->tool_bar_window))
25414 note_tool_bar_highlight (f, x, y);
25415 return;
25417 #endif
25419 /* Mouse is on the mode, header line or margin? */
25420 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25421 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25423 note_mode_line_or_margin_highlight (window, x, y, part);
25424 return;
25427 #ifdef HAVE_WINDOW_SYSTEM
25428 if (part == ON_VERTICAL_BORDER)
25430 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25431 help_echo_string = build_string ("drag-mouse-1: resize");
25433 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25434 || part == ON_SCROLL_BAR)
25435 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25436 else
25437 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25438 #endif
25440 /* Are we in a window whose display is up to date?
25441 And verify the buffer's text has not changed. */
25442 b = XBUFFER (w->buffer);
25443 if (part == ON_TEXT
25444 && EQ (w->window_end_valid, w->buffer)
25445 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25446 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25448 int hpos, vpos, i, dx, dy, area;
25449 EMACS_INT pos;
25450 struct glyph *glyph;
25451 Lisp_Object object;
25452 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
25453 Lisp_Object *overlay_vec = NULL;
25454 int noverlays;
25455 struct buffer *obuf;
25456 EMACS_INT obegv, ozv;
25457 int same_region;
25459 /* Find the glyph under X/Y. */
25460 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25462 #ifdef HAVE_WINDOW_SYSTEM
25463 /* Look for :pointer property on image. */
25464 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25466 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25467 if (img != NULL && IMAGEP (img->spec))
25469 Lisp_Object image_map, hotspot;
25470 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25471 !NILP (image_map))
25472 && (hotspot = find_hot_spot (image_map,
25473 glyph->slice.img.x + dx,
25474 glyph->slice.img.y + dy),
25475 CONSP (hotspot))
25476 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25478 Lisp_Object area_id, plist;
25480 area_id = XCAR (hotspot);
25481 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25482 If so, we could look for mouse-enter, mouse-leave
25483 properties in PLIST (and do something...). */
25484 hotspot = XCDR (hotspot);
25485 if (CONSP (hotspot)
25486 && (plist = XCAR (hotspot), CONSP (plist)))
25488 pointer = Fplist_get (plist, Qpointer);
25489 if (NILP (pointer))
25490 pointer = Qhand;
25491 help_echo_string = Fplist_get (plist, Qhelp_echo);
25492 if (!NILP (help_echo_string))
25494 help_echo_window = window;
25495 help_echo_object = glyph->object;
25496 help_echo_pos = glyph->charpos;
25500 if (NILP (pointer))
25501 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25504 #endif /* HAVE_WINDOW_SYSTEM */
25506 /* Clear mouse face if X/Y not over text. */
25507 if (glyph == NULL
25508 || area != TEXT_AREA
25509 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25510 /* Glyph's OBJECT is an integer for glyphs inserted by the
25511 display engine for its internal purposes, like truncation
25512 and continuation glyphs and blanks beyond the end of
25513 line's text on text terminals. If we are over such a
25514 glyph, we are not over any text. */
25515 || INTEGERP (glyph->object)
25516 /* R2L rows have a stretch glyph at their front, which
25517 stands for no text, whereas L2R rows have no glyphs at
25518 all beyond the end of text. Treat such stretch glyphs
25519 like we do with NULL glyphs in L2R rows. */
25520 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25521 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25522 && glyph->type == STRETCH_GLYPH
25523 && glyph->avoid_cursor_p))
25525 if (clear_mouse_face (hlinfo))
25526 cursor = No_Cursor;
25527 #ifdef HAVE_WINDOW_SYSTEM
25528 if (FRAME_WINDOW_P (f) && NILP (pointer))
25530 if (area != TEXT_AREA)
25531 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25532 else
25533 pointer = Vvoid_text_area_pointer;
25535 #endif
25536 goto set_cursor;
25539 pos = glyph->charpos;
25540 object = glyph->object;
25541 if (!STRINGP (object) && !BUFFERP (object))
25542 goto set_cursor;
25544 /* If we get an out-of-range value, return now; avoid an error. */
25545 if (BUFFERP (object) && pos > BUF_Z (b))
25546 goto set_cursor;
25548 /* Make the window's buffer temporarily current for
25549 overlays_at and compute_char_face. */
25550 obuf = current_buffer;
25551 current_buffer = b;
25552 obegv = BEGV;
25553 ozv = ZV;
25554 BEGV = BEG;
25555 ZV = Z;
25557 /* Is this char mouse-active or does it have help-echo? */
25558 position = make_number (pos);
25560 if (BUFFERP (object))
25562 /* Put all the overlays we want in a vector in overlay_vec. */
25563 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25564 /* Sort overlays into increasing priority order. */
25565 noverlays = sort_overlays (overlay_vec, noverlays, w);
25567 else
25568 noverlays = 0;
25570 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25572 if (same_region)
25573 cursor = No_Cursor;
25575 /* Check mouse-face highlighting. */
25576 if (! same_region
25577 /* If there exists an overlay with mouse-face overlapping
25578 the one we are currently highlighting, we have to
25579 check if we enter the overlapping overlay, and then
25580 highlight only that. */
25581 || (OVERLAYP (hlinfo->mouse_face_overlay)
25582 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25584 /* Find the highest priority overlay with a mouse-face. */
25585 overlay = Qnil;
25586 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25588 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25589 if (!NILP (mouse_face))
25590 overlay = overlay_vec[i];
25593 /* If we're highlighting the same overlay as before, there's
25594 no need to do that again. */
25595 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25596 goto check_help_echo;
25597 hlinfo->mouse_face_overlay = overlay;
25599 /* Clear the display of the old active region, if any. */
25600 if (clear_mouse_face (hlinfo))
25601 cursor = No_Cursor;
25603 /* If no overlay applies, get a text property. */
25604 if (NILP (overlay))
25605 mouse_face = Fget_text_property (position, Qmouse_face, object);
25607 /* Next, compute the bounds of the mouse highlighting and
25608 display it. */
25609 if (!NILP (mouse_face) && STRINGP (object))
25611 /* The mouse-highlighting comes from a display string
25612 with a mouse-face. */
25613 Lisp_Object b, e;
25614 EMACS_INT ignore;
25616 b = Fprevious_single_property_change
25617 (make_number (pos + 1), Qmouse_face, object, Qnil);
25618 e = Fnext_single_property_change
25619 (position, Qmouse_face, object, Qnil);
25620 if (NILP (b))
25621 b = make_number (0);
25622 if (NILP (e))
25623 e = make_number (SCHARS (object) - 1);
25624 mouse_face_from_string_pos (w, hlinfo, object,
25625 XINT (b), XINT (e));
25626 hlinfo->mouse_face_past_end = 0;
25627 hlinfo->mouse_face_window = window;
25628 hlinfo->mouse_face_face_id
25629 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25630 glyph->face_id, 1);
25631 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25632 cursor = No_Cursor;
25634 else
25636 /* The mouse-highlighting, if any, comes from an overlay
25637 or text property in the buffer. */
25638 Lisp_Object buffer, display_string;
25640 if (STRINGP (object))
25642 /* If we are on a display string with no mouse-face,
25643 check if the text under it has one. */
25644 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25645 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25646 pos = string_buffer_position (w, object, start);
25647 if (pos > 0)
25649 mouse_face = get_char_property_and_overlay
25650 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25651 buffer = w->buffer;
25652 display_string = object;
25655 else
25657 buffer = object;
25658 display_string = Qnil;
25661 if (!NILP (mouse_face))
25663 Lisp_Object before, after;
25664 Lisp_Object before_string, after_string;
25665 /* To correctly find the limits of mouse highlight
25666 in a bidi-reordered buffer, we must not use the
25667 optimization of limiting the search in
25668 previous-single-property-change and
25669 next-single-property-change, because
25670 rows_from_pos_range needs the real start and end
25671 positions to DTRT in this case. That's because
25672 the first row visible in a window does not
25673 necessarily display the character whose position
25674 is the smallest. */
25675 Lisp_Object lim1 =
25676 NILP (XBUFFER (buffer)->bidi_display_reordering)
25677 ? Fmarker_position (w->start)
25678 : Qnil;
25679 Lisp_Object lim2 =
25680 NILP (XBUFFER (buffer)->bidi_display_reordering)
25681 ? make_number (BUF_Z (XBUFFER (buffer))
25682 - XFASTINT (w->window_end_pos))
25683 : Qnil;
25685 if (NILP (overlay))
25687 /* Handle the text property case. */
25688 before = Fprevious_single_property_change
25689 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25690 after = Fnext_single_property_change
25691 (make_number (pos), Qmouse_face, buffer, lim2);
25692 before_string = after_string = Qnil;
25694 else
25696 /* Handle the overlay case. */
25697 before = Foverlay_start (overlay);
25698 after = Foverlay_end (overlay);
25699 before_string = Foverlay_get (overlay, Qbefore_string);
25700 after_string = Foverlay_get (overlay, Qafter_string);
25702 if (!STRINGP (before_string)) before_string = Qnil;
25703 if (!STRINGP (after_string)) after_string = Qnil;
25706 mouse_face_from_buffer_pos (window, hlinfo, pos,
25707 XFASTINT (before),
25708 XFASTINT (after),
25709 before_string, after_string,
25710 display_string);
25711 cursor = No_Cursor;
25716 check_help_echo:
25718 /* Look for a `help-echo' property. */
25719 if (NILP (help_echo_string)) {
25720 Lisp_Object help, overlay;
25722 /* Check overlays first. */
25723 help = overlay = Qnil;
25724 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25726 overlay = overlay_vec[i];
25727 help = Foverlay_get (overlay, Qhelp_echo);
25730 if (!NILP (help))
25732 help_echo_string = help;
25733 help_echo_window = window;
25734 help_echo_object = overlay;
25735 help_echo_pos = pos;
25737 else
25739 Lisp_Object object = glyph->object;
25740 EMACS_INT charpos = glyph->charpos;
25742 /* Try text properties. */
25743 if (STRINGP (object)
25744 && charpos >= 0
25745 && charpos < SCHARS (object))
25747 help = Fget_text_property (make_number (charpos),
25748 Qhelp_echo, object);
25749 if (NILP (help))
25751 /* If the string itself doesn't specify a help-echo,
25752 see if the buffer text ``under'' it does. */
25753 struct glyph_row *r
25754 = MATRIX_ROW (w->current_matrix, vpos);
25755 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25756 EMACS_INT pos = string_buffer_position (w, object, start);
25757 if (pos > 0)
25759 help = Fget_char_property (make_number (pos),
25760 Qhelp_echo, w->buffer);
25761 if (!NILP (help))
25763 charpos = pos;
25764 object = w->buffer;
25769 else if (BUFFERP (object)
25770 && charpos >= BEGV
25771 && charpos < ZV)
25772 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25773 object);
25775 if (!NILP (help))
25777 help_echo_string = help;
25778 help_echo_window = window;
25779 help_echo_object = object;
25780 help_echo_pos = charpos;
25785 #ifdef HAVE_WINDOW_SYSTEM
25786 /* Look for a `pointer' property. */
25787 if (FRAME_WINDOW_P (f) && NILP (pointer))
25789 /* Check overlays first. */
25790 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25791 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25793 if (NILP (pointer))
25795 Lisp_Object object = glyph->object;
25796 EMACS_INT charpos = glyph->charpos;
25798 /* Try text properties. */
25799 if (STRINGP (object)
25800 && charpos >= 0
25801 && charpos < SCHARS (object))
25803 pointer = Fget_text_property (make_number (charpos),
25804 Qpointer, object);
25805 if (NILP (pointer))
25807 /* If the string itself doesn't specify a pointer,
25808 see if the buffer text ``under'' it does. */
25809 struct glyph_row *r
25810 = MATRIX_ROW (w->current_matrix, vpos);
25811 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25812 EMACS_INT pos = string_buffer_position (w, object,
25813 start);
25814 if (pos > 0)
25815 pointer = Fget_char_property (make_number (pos),
25816 Qpointer, w->buffer);
25819 else if (BUFFERP (object)
25820 && charpos >= BEGV
25821 && charpos < ZV)
25822 pointer = Fget_text_property (make_number (charpos),
25823 Qpointer, object);
25826 #endif /* HAVE_WINDOW_SYSTEM */
25828 BEGV = obegv;
25829 ZV = ozv;
25830 current_buffer = obuf;
25833 set_cursor:
25835 #ifdef HAVE_WINDOW_SYSTEM
25836 if (FRAME_WINDOW_P (f))
25837 define_frame_cursor1 (f, cursor, pointer);
25838 #else
25839 /* This is here to prevent a compiler error, about "label at end of
25840 compound statement". */
25841 return;
25842 #endif
25846 /* EXPORT for RIF:
25847 Clear any mouse-face on window W. This function is part of the
25848 redisplay interface, and is called from try_window_id and similar
25849 functions to ensure the mouse-highlight is off. */
25851 void
25852 x_clear_window_mouse_face (struct window *w)
25854 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25855 Lisp_Object window;
25857 BLOCK_INPUT;
25858 XSETWINDOW (window, w);
25859 if (EQ (window, hlinfo->mouse_face_window))
25860 clear_mouse_face (hlinfo);
25861 UNBLOCK_INPUT;
25865 /* EXPORT:
25866 Just discard the mouse face information for frame F, if any.
25867 This is used when the size of F is changed. */
25869 void
25870 cancel_mouse_face (struct frame *f)
25872 Lisp_Object window;
25873 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25875 window = hlinfo->mouse_face_window;
25876 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25878 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25879 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25880 hlinfo->mouse_face_window = Qnil;
25886 /***********************************************************************
25887 Exposure Events
25888 ***********************************************************************/
25890 #ifdef HAVE_WINDOW_SYSTEM
25892 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25893 which intersects rectangle R. R is in window-relative coordinates. */
25895 static void
25896 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25897 enum glyph_row_area area)
25899 struct glyph *first = row->glyphs[area];
25900 struct glyph *end = row->glyphs[area] + row->used[area];
25901 struct glyph *last;
25902 int first_x, start_x, x;
25904 if (area == TEXT_AREA && row->fill_line_p)
25905 /* If row extends face to end of line write the whole line. */
25906 draw_glyphs (w, 0, row, area,
25907 0, row->used[area],
25908 DRAW_NORMAL_TEXT, 0);
25909 else
25911 /* Set START_X to the window-relative start position for drawing glyphs of
25912 AREA. The first glyph of the text area can be partially visible.
25913 The first glyphs of other areas cannot. */
25914 start_x = window_box_left_offset (w, area);
25915 x = start_x;
25916 if (area == TEXT_AREA)
25917 x += row->x;
25919 /* Find the first glyph that must be redrawn. */
25920 while (first < end
25921 && x + first->pixel_width < r->x)
25923 x += first->pixel_width;
25924 ++first;
25927 /* Find the last one. */
25928 last = first;
25929 first_x = x;
25930 while (last < end
25931 && x < r->x + r->width)
25933 x += last->pixel_width;
25934 ++last;
25937 /* Repaint. */
25938 if (last > first)
25939 draw_glyphs (w, first_x - start_x, row, area,
25940 first - row->glyphs[area], last - row->glyphs[area],
25941 DRAW_NORMAL_TEXT, 0);
25946 /* Redraw the parts of the glyph row ROW on window W intersecting
25947 rectangle R. R is in window-relative coordinates. Value is
25948 non-zero if mouse-face was overwritten. */
25950 static int
25951 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25953 xassert (row->enabled_p);
25955 if (row->mode_line_p || w->pseudo_window_p)
25956 draw_glyphs (w, 0, row, TEXT_AREA,
25957 0, row->used[TEXT_AREA],
25958 DRAW_NORMAL_TEXT, 0);
25959 else
25961 if (row->used[LEFT_MARGIN_AREA])
25962 expose_area (w, row, r, LEFT_MARGIN_AREA);
25963 if (row->used[TEXT_AREA])
25964 expose_area (w, row, r, TEXT_AREA);
25965 if (row->used[RIGHT_MARGIN_AREA])
25966 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25967 draw_row_fringe_bitmaps (w, row);
25970 return row->mouse_face_p;
25974 /* Redraw those parts of glyphs rows during expose event handling that
25975 overlap other rows. Redrawing of an exposed line writes over parts
25976 of lines overlapping that exposed line; this function fixes that.
25978 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25979 row in W's current matrix that is exposed and overlaps other rows.
25980 LAST_OVERLAPPING_ROW is the last such row. */
25982 static void
25983 expose_overlaps (struct window *w,
25984 struct glyph_row *first_overlapping_row,
25985 struct glyph_row *last_overlapping_row,
25986 XRectangle *r)
25988 struct glyph_row *row;
25990 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25991 if (row->overlapping_p)
25993 xassert (row->enabled_p && !row->mode_line_p);
25995 row->clip = r;
25996 if (row->used[LEFT_MARGIN_AREA])
25997 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25999 if (row->used[TEXT_AREA])
26000 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26002 if (row->used[RIGHT_MARGIN_AREA])
26003 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26004 row->clip = NULL;
26009 /* Return non-zero if W's cursor intersects rectangle R. */
26011 static int
26012 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26014 XRectangle cr, result;
26015 struct glyph *cursor_glyph;
26016 struct glyph_row *row;
26018 if (w->phys_cursor.vpos >= 0
26019 && w->phys_cursor.vpos < w->current_matrix->nrows
26020 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26021 row->enabled_p)
26022 && row->cursor_in_fringe_p)
26024 /* Cursor is in the fringe. */
26025 cr.x = window_box_right_offset (w,
26026 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26027 ? RIGHT_MARGIN_AREA
26028 : TEXT_AREA));
26029 cr.y = row->y;
26030 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26031 cr.height = row->height;
26032 return x_intersect_rectangles (&cr, r, &result);
26035 cursor_glyph = get_phys_cursor_glyph (w);
26036 if (cursor_glyph)
26038 /* r is relative to W's box, but w->phys_cursor.x is relative
26039 to left edge of W's TEXT area. Adjust it. */
26040 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26041 cr.y = w->phys_cursor.y;
26042 cr.width = cursor_glyph->pixel_width;
26043 cr.height = w->phys_cursor_height;
26044 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26045 I assume the effect is the same -- and this is portable. */
26046 return x_intersect_rectangles (&cr, r, &result);
26048 /* If we don't understand the format, pretend we're not in the hot-spot. */
26049 return 0;
26053 /* EXPORT:
26054 Draw a vertical window border to the right of window W if W doesn't
26055 have vertical scroll bars. */
26057 void
26058 x_draw_vertical_border (struct window *w)
26060 struct frame *f = XFRAME (WINDOW_FRAME (w));
26062 /* We could do better, if we knew what type of scroll-bar the adjacent
26063 windows (on either side) have... But we don't :-(
26064 However, I think this works ok. ++KFS 2003-04-25 */
26066 /* Redraw borders between horizontally adjacent windows. Don't
26067 do it for frames with vertical scroll bars because either the
26068 right scroll bar of a window, or the left scroll bar of its
26069 neighbor will suffice as a border. */
26070 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26071 return;
26073 if (!WINDOW_RIGHTMOST_P (w)
26074 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26076 int x0, x1, y0, y1;
26078 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26079 y1 -= 1;
26081 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26082 x1 -= 1;
26084 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26086 else if (!WINDOW_LEFTMOST_P (w)
26087 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26089 int x0, x1, y0, y1;
26091 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26092 y1 -= 1;
26094 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26095 x0 -= 1;
26097 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26102 /* Redraw the part of window W intersection rectangle FR. Pixel
26103 coordinates in FR are frame-relative. Call this function with
26104 input blocked. Value is non-zero if the exposure overwrites
26105 mouse-face. */
26107 static int
26108 expose_window (struct window *w, XRectangle *fr)
26110 struct frame *f = XFRAME (w->frame);
26111 XRectangle wr, r;
26112 int mouse_face_overwritten_p = 0;
26114 /* If window is not yet fully initialized, do nothing. This can
26115 happen when toolkit scroll bars are used and a window is split.
26116 Reconfiguring the scroll bar will generate an expose for a newly
26117 created window. */
26118 if (w->current_matrix == NULL)
26119 return 0;
26121 /* When we're currently updating the window, display and current
26122 matrix usually don't agree. Arrange for a thorough display
26123 later. */
26124 if (w == updated_window)
26126 SET_FRAME_GARBAGED (f);
26127 return 0;
26130 /* Frame-relative pixel rectangle of W. */
26131 wr.x = WINDOW_LEFT_EDGE_X (w);
26132 wr.y = WINDOW_TOP_EDGE_Y (w);
26133 wr.width = WINDOW_TOTAL_WIDTH (w);
26134 wr.height = WINDOW_TOTAL_HEIGHT (w);
26136 if (x_intersect_rectangles (fr, &wr, &r))
26138 int yb = window_text_bottom_y (w);
26139 struct glyph_row *row;
26140 int cursor_cleared_p;
26141 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26143 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26144 r.x, r.y, r.width, r.height));
26146 /* Convert to window coordinates. */
26147 r.x -= WINDOW_LEFT_EDGE_X (w);
26148 r.y -= WINDOW_TOP_EDGE_Y (w);
26150 /* Turn off the cursor. */
26151 if (!w->pseudo_window_p
26152 && phys_cursor_in_rect_p (w, &r))
26154 x_clear_cursor (w);
26155 cursor_cleared_p = 1;
26157 else
26158 cursor_cleared_p = 0;
26160 /* Update lines intersecting rectangle R. */
26161 first_overlapping_row = last_overlapping_row = NULL;
26162 for (row = w->current_matrix->rows;
26163 row->enabled_p;
26164 ++row)
26166 int y0 = row->y;
26167 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26169 if ((y0 >= r.y && y0 < r.y + r.height)
26170 || (y1 > r.y && y1 < r.y + r.height)
26171 || (r.y >= y0 && r.y < y1)
26172 || (r.y + r.height > y0 && r.y + r.height < y1))
26174 /* A header line may be overlapping, but there is no need
26175 to fix overlapping areas for them. KFS 2005-02-12 */
26176 if (row->overlapping_p && !row->mode_line_p)
26178 if (first_overlapping_row == NULL)
26179 first_overlapping_row = row;
26180 last_overlapping_row = row;
26183 row->clip = fr;
26184 if (expose_line (w, row, &r))
26185 mouse_face_overwritten_p = 1;
26186 row->clip = NULL;
26188 else if (row->overlapping_p)
26190 /* We must redraw a row overlapping the exposed area. */
26191 if (y0 < r.y
26192 ? y0 + row->phys_height > r.y
26193 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26195 if (first_overlapping_row == NULL)
26196 first_overlapping_row = row;
26197 last_overlapping_row = row;
26201 if (y1 >= yb)
26202 break;
26205 /* Display the mode line if there is one. */
26206 if (WINDOW_WANTS_MODELINE_P (w)
26207 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26208 row->enabled_p)
26209 && row->y < r.y + r.height)
26211 if (expose_line (w, row, &r))
26212 mouse_face_overwritten_p = 1;
26215 if (!w->pseudo_window_p)
26217 /* Fix the display of overlapping rows. */
26218 if (first_overlapping_row)
26219 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26220 fr);
26222 /* Draw border between windows. */
26223 x_draw_vertical_border (w);
26225 /* Turn the cursor on again. */
26226 if (cursor_cleared_p)
26227 update_window_cursor (w, 1);
26231 return mouse_face_overwritten_p;
26236 /* Redraw (parts) of all windows in the window tree rooted at W that
26237 intersect R. R contains frame pixel coordinates. Value is
26238 non-zero if the exposure overwrites mouse-face. */
26240 static int
26241 expose_window_tree (struct window *w, XRectangle *r)
26243 struct frame *f = XFRAME (w->frame);
26244 int mouse_face_overwritten_p = 0;
26246 while (w && !FRAME_GARBAGED_P (f))
26248 if (!NILP (w->hchild))
26249 mouse_face_overwritten_p
26250 |= expose_window_tree (XWINDOW (w->hchild), r);
26251 else if (!NILP (w->vchild))
26252 mouse_face_overwritten_p
26253 |= expose_window_tree (XWINDOW (w->vchild), r);
26254 else
26255 mouse_face_overwritten_p |= expose_window (w, r);
26257 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26260 return mouse_face_overwritten_p;
26264 /* EXPORT:
26265 Redisplay an exposed area of frame F. X and Y are the upper-left
26266 corner of the exposed rectangle. W and H are width and height of
26267 the exposed area. All are pixel values. W or H zero means redraw
26268 the entire frame. */
26270 void
26271 expose_frame (struct frame *f, int x, int y, int w, int h)
26273 XRectangle r;
26274 int mouse_face_overwritten_p = 0;
26276 TRACE ((stderr, "expose_frame "));
26278 /* No need to redraw if frame will be redrawn soon. */
26279 if (FRAME_GARBAGED_P (f))
26281 TRACE ((stderr, " garbaged\n"));
26282 return;
26285 /* If basic faces haven't been realized yet, there is no point in
26286 trying to redraw anything. This can happen when we get an expose
26287 event while Emacs is starting, e.g. by moving another window. */
26288 if (FRAME_FACE_CACHE (f) == NULL
26289 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26291 TRACE ((stderr, " no faces\n"));
26292 return;
26295 if (w == 0 || h == 0)
26297 r.x = r.y = 0;
26298 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26299 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26301 else
26303 r.x = x;
26304 r.y = y;
26305 r.width = w;
26306 r.height = h;
26309 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26310 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26312 if (WINDOWP (f->tool_bar_window))
26313 mouse_face_overwritten_p
26314 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26316 #ifdef HAVE_X_WINDOWS
26317 #ifndef MSDOS
26318 #ifndef USE_X_TOOLKIT
26319 if (WINDOWP (f->menu_bar_window))
26320 mouse_face_overwritten_p
26321 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26322 #endif /* not USE_X_TOOLKIT */
26323 #endif
26324 #endif
26326 /* Some window managers support a focus-follows-mouse style with
26327 delayed raising of frames. Imagine a partially obscured frame,
26328 and moving the mouse into partially obscured mouse-face on that
26329 frame. The visible part of the mouse-face will be highlighted,
26330 then the WM raises the obscured frame. With at least one WM, KDE
26331 2.1, Emacs is not getting any event for the raising of the frame
26332 (even tried with SubstructureRedirectMask), only Expose events.
26333 These expose events will draw text normally, i.e. not
26334 highlighted. Which means we must redo the highlight here.
26335 Subsume it under ``we love X''. --gerd 2001-08-15 */
26336 /* Included in Windows version because Windows most likely does not
26337 do the right thing if any third party tool offers
26338 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26339 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26341 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26342 if (f == hlinfo->mouse_face_mouse_frame)
26344 int x = hlinfo->mouse_face_mouse_x;
26345 int y = hlinfo->mouse_face_mouse_y;
26346 clear_mouse_face (hlinfo);
26347 note_mouse_highlight (f, x, y);
26353 /* EXPORT:
26354 Determine the intersection of two rectangles R1 and R2. Return
26355 the intersection in *RESULT. Value is non-zero if RESULT is not
26356 empty. */
26359 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26361 XRectangle *left, *right;
26362 XRectangle *upper, *lower;
26363 int intersection_p = 0;
26365 /* Rearrange so that R1 is the left-most rectangle. */
26366 if (r1->x < r2->x)
26367 left = r1, right = r2;
26368 else
26369 left = r2, right = r1;
26371 /* X0 of the intersection is right.x0, if this is inside R1,
26372 otherwise there is no intersection. */
26373 if (right->x <= left->x + left->width)
26375 result->x = right->x;
26377 /* The right end of the intersection is the minimum of the
26378 the right ends of left and right. */
26379 result->width = (min (left->x + left->width, right->x + right->width)
26380 - result->x);
26382 /* Same game for Y. */
26383 if (r1->y < r2->y)
26384 upper = r1, lower = r2;
26385 else
26386 upper = r2, lower = r1;
26388 /* The upper end of the intersection is lower.y0, if this is inside
26389 of upper. Otherwise, there is no intersection. */
26390 if (lower->y <= upper->y + upper->height)
26392 result->y = lower->y;
26394 /* The lower end of the intersection is the minimum of the lower
26395 ends of upper and lower. */
26396 result->height = (min (lower->y + lower->height,
26397 upper->y + upper->height)
26398 - result->y);
26399 intersection_p = 1;
26403 return intersection_p;
26406 #endif /* HAVE_WINDOW_SYSTEM */
26409 /***********************************************************************
26410 Initialization
26411 ***********************************************************************/
26413 void
26414 syms_of_xdisp (void)
26416 Vwith_echo_area_save_vector = Qnil;
26417 staticpro (&Vwith_echo_area_save_vector);
26419 Vmessage_stack = Qnil;
26420 staticpro (&Vmessage_stack);
26422 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26423 staticpro (&Qinhibit_redisplay);
26425 message_dolog_marker1 = Fmake_marker ();
26426 staticpro (&message_dolog_marker1);
26427 message_dolog_marker2 = Fmake_marker ();
26428 staticpro (&message_dolog_marker2);
26429 message_dolog_marker3 = Fmake_marker ();
26430 staticpro (&message_dolog_marker3);
26432 #if GLYPH_DEBUG
26433 defsubr (&Sdump_frame_glyph_matrix);
26434 defsubr (&Sdump_glyph_matrix);
26435 defsubr (&Sdump_glyph_row);
26436 defsubr (&Sdump_tool_bar_row);
26437 defsubr (&Strace_redisplay);
26438 defsubr (&Strace_to_stderr);
26439 #endif
26440 #ifdef HAVE_WINDOW_SYSTEM
26441 defsubr (&Stool_bar_lines_needed);
26442 defsubr (&Slookup_image_map);
26443 #endif
26444 defsubr (&Sformat_mode_line);
26445 defsubr (&Sinvisible_p);
26446 defsubr (&Scurrent_bidi_paragraph_direction);
26448 staticpro (&Qmenu_bar_update_hook);
26449 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26451 staticpro (&Qoverriding_terminal_local_map);
26452 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26454 staticpro (&Qoverriding_local_map);
26455 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26457 staticpro (&Qwindow_scroll_functions);
26458 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26460 staticpro (&Qwindow_text_change_functions);
26461 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26463 staticpro (&Qredisplay_end_trigger_functions);
26464 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26466 staticpro (&Qinhibit_point_motion_hooks);
26467 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26469 Qeval = intern_c_string ("eval");
26470 staticpro (&Qeval);
26472 QCdata = intern_c_string (":data");
26473 staticpro (&QCdata);
26474 Qdisplay = intern_c_string ("display");
26475 staticpro (&Qdisplay);
26476 Qspace_width = intern_c_string ("space-width");
26477 staticpro (&Qspace_width);
26478 Qraise = intern_c_string ("raise");
26479 staticpro (&Qraise);
26480 Qslice = intern_c_string ("slice");
26481 staticpro (&Qslice);
26482 Qspace = intern_c_string ("space");
26483 staticpro (&Qspace);
26484 Qmargin = intern_c_string ("margin");
26485 staticpro (&Qmargin);
26486 Qpointer = intern_c_string ("pointer");
26487 staticpro (&Qpointer);
26488 Qleft_margin = intern_c_string ("left-margin");
26489 staticpro (&Qleft_margin);
26490 Qright_margin = intern_c_string ("right-margin");
26491 staticpro (&Qright_margin);
26492 Qcenter = intern_c_string ("center");
26493 staticpro (&Qcenter);
26494 Qline_height = intern_c_string ("line-height");
26495 staticpro (&Qline_height);
26496 QCalign_to = intern_c_string (":align-to");
26497 staticpro (&QCalign_to);
26498 QCrelative_width = intern_c_string (":relative-width");
26499 staticpro (&QCrelative_width);
26500 QCrelative_height = intern_c_string (":relative-height");
26501 staticpro (&QCrelative_height);
26502 QCeval = intern_c_string (":eval");
26503 staticpro (&QCeval);
26504 QCpropertize = intern_c_string (":propertize");
26505 staticpro (&QCpropertize);
26506 QCfile = intern_c_string (":file");
26507 staticpro (&QCfile);
26508 Qfontified = intern_c_string ("fontified");
26509 staticpro (&Qfontified);
26510 Qfontification_functions = intern_c_string ("fontification-functions");
26511 staticpro (&Qfontification_functions);
26512 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26513 staticpro (&Qtrailing_whitespace);
26514 Qescape_glyph = intern_c_string ("escape-glyph");
26515 staticpro (&Qescape_glyph);
26516 Qnobreak_space = intern_c_string ("nobreak-space");
26517 staticpro (&Qnobreak_space);
26518 Qimage = intern_c_string ("image");
26519 staticpro (&Qimage);
26520 Qtext = intern_c_string ("text");
26521 staticpro (&Qtext);
26522 Qboth = intern_c_string ("both");
26523 staticpro (&Qboth);
26524 Qboth_horiz = intern_c_string ("both-horiz");
26525 staticpro (&Qboth_horiz);
26526 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26527 staticpro (&Qtext_image_horiz);
26528 QCmap = intern_c_string (":map");
26529 staticpro (&QCmap);
26530 QCpointer = intern_c_string (":pointer");
26531 staticpro (&QCpointer);
26532 Qrect = intern_c_string ("rect");
26533 staticpro (&Qrect);
26534 Qcircle = intern_c_string ("circle");
26535 staticpro (&Qcircle);
26536 Qpoly = intern_c_string ("poly");
26537 staticpro (&Qpoly);
26538 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26539 staticpro (&Qmessage_truncate_lines);
26540 Qgrow_only = intern_c_string ("grow-only");
26541 staticpro (&Qgrow_only);
26542 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26543 staticpro (&Qinhibit_menubar_update);
26544 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26545 staticpro (&Qinhibit_eval_during_redisplay);
26546 Qposition = intern_c_string ("position");
26547 staticpro (&Qposition);
26548 Qbuffer_position = intern_c_string ("buffer-position");
26549 staticpro (&Qbuffer_position);
26550 Qobject = intern_c_string ("object");
26551 staticpro (&Qobject);
26552 Qbar = intern_c_string ("bar");
26553 staticpro (&Qbar);
26554 Qhbar = intern_c_string ("hbar");
26555 staticpro (&Qhbar);
26556 Qbox = intern_c_string ("box");
26557 staticpro (&Qbox);
26558 Qhollow = intern_c_string ("hollow");
26559 staticpro (&Qhollow);
26560 Qhand = intern_c_string ("hand");
26561 staticpro (&Qhand);
26562 Qarrow = intern_c_string ("arrow");
26563 staticpro (&Qarrow);
26564 Qtext = intern_c_string ("text");
26565 staticpro (&Qtext);
26566 Qrisky_local_variable = intern_c_string ("risky-local-variable");
26567 staticpro (&Qrisky_local_variable);
26568 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26569 staticpro (&Qinhibit_free_realized_faces);
26571 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26572 Fcons (intern_c_string ("void-variable"), Qnil)),
26573 Qnil);
26574 staticpro (&list_of_error);
26576 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26577 staticpro (&Qlast_arrow_position);
26578 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26579 staticpro (&Qlast_arrow_string);
26581 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26582 staticpro (&Qoverlay_arrow_string);
26583 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26584 staticpro (&Qoverlay_arrow_bitmap);
26586 echo_buffer[0] = echo_buffer[1] = Qnil;
26587 staticpro (&echo_buffer[0]);
26588 staticpro (&echo_buffer[1]);
26590 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26591 staticpro (&echo_area_buffer[0]);
26592 staticpro (&echo_area_buffer[1]);
26594 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26595 staticpro (&Vmessages_buffer_name);
26597 mode_line_proptrans_alist = Qnil;
26598 staticpro (&mode_line_proptrans_alist);
26599 mode_line_string_list = Qnil;
26600 staticpro (&mode_line_string_list);
26601 mode_line_string_face = Qnil;
26602 staticpro (&mode_line_string_face);
26603 mode_line_string_face_prop = Qnil;
26604 staticpro (&mode_line_string_face_prop);
26605 Vmode_line_unwind_vector = Qnil;
26606 staticpro (&Vmode_line_unwind_vector);
26608 help_echo_string = Qnil;
26609 staticpro (&help_echo_string);
26610 help_echo_object = Qnil;
26611 staticpro (&help_echo_object);
26612 help_echo_window = Qnil;
26613 staticpro (&help_echo_window);
26614 previous_help_echo_string = Qnil;
26615 staticpro (&previous_help_echo_string);
26616 help_echo_pos = -1;
26618 Qright_to_left = intern_c_string ("right-to-left");
26619 staticpro (&Qright_to_left);
26620 Qleft_to_right = intern_c_string ("left-to-right");
26621 staticpro (&Qleft_to_right);
26623 #ifdef HAVE_WINDOW_SYSTEM
26624 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
26625 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26626 For example, if a block cursor is over a tab, it will be drawn as
26627 wide as that tab on the display. */);
26628 x_stretch_cursor_p = 0;
26629 #endif
26631 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
26632 doc: /* *Non-nil means highlight trailing whitespace.
26633 The face used for trailing whitespace is `trailing-whitespace'. */);
26634 Vshow_trailing_whitespace = Qnil;
26636 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
26637 doc: /* *Control highlighting of nobreak space and soft hyphen.
26638 A value of t means highlight the character itself (for nobreak space,
26639 use face `nobreak-space').
26640 A value of nil means no highlighting.
26641 Other values mean display the escape glyph followed by an ordinary
26642 space or ordinary hyphen. */);
26643 Vnobreak_char_display = Qt;
26645 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
26646 doc: /* *The pointer shape to show in void text areas.
26647 A value of nil means to show the text pointer. Other options are `arrow',
26648 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26649 Vvoid_text_area_pointer = Qarrow;
26651 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
26652 doc: /* Non-nil means don't actually do any redisplay.
26653 This is used for internal purposes. */);
26654 Vinhibit_redisplay = Qnil;
26656 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
26657 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26658 Vglobal_mode_string = Qnil;
26660 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
26661 doc: /* Marker for where to display an arrow on top of the buffer text.
26662 This must be the beginning of a line in order to work.
26663 See also `overlay-arrow-string'. */);
26664 Voverlay_arrow_position = Qnil;
26666 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
26667 doc: /* String to display as an arrow in non-window frames.
26668 See also `overlay-arrow-position'. */);
26669 Voverlay_arrow_string = make_pure_c_string ("=>");
26671 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
26672 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26673 The symbols on this list are examined during redisplay to determine
26674 where to display overlay arrows. */);
26675 Voverlay_arrow_variable_list
26676 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26678 DEFVAR_INT ("scroll-step", &scroll_step,
26679 doc: /* *The number of lines to try scrolling a window by when point moves out.
26680 If that fails to bring point back on frame, point is centered instead.
26681 If this is zero, point is always centered after it moves off frame.
26682 If you want scrolling to always be a line at a time, you should set
26683 `scroll-conservatively' to a large value rather than set this to 1. */);
26685 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
26686 doc: /* *Scroll up to this many lines, to bring point back on screen.
26687 If point moves off-screen, redisplay will scroll by up to
26688 `scroll-conservatively' lines in order to bring point just barely
26689 onto the screen again. If that cannot be done, then redisplay
26690 recenters point as usual.
26692 A value of zero means always recenter point if it moves off screen. */);
26693 scroll_conservatively = 0;
26695 DEFVAR_INT ("scroll-margin", &scroll_margin,
26696 doc: /* *Number of lines of margin at the top and bottom of a window.
26697 Recenter the window whenever point gets within this many lines
26698 of the top or bottom of the window. */);
26699 scroll_margin = 0;
26701 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
26702 doc: /* Pixels per inch value for non-window system displays.
26703 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26704 Vdisplay_pixels_per_inch = make_float (72.0);
26706 #if GLYPH_DEBUG
26707 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
26708 #endif
26710 DEFVAR_LISP ("truncate-partial-width-windows",
26711 &Vtruncate_partial_width_windows,
26712 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26713 For an integer value, truncate lines in each window narrower than the
26714 full frame width, provided the window width is less than that integer;
26715 otherwise, respect the value of `truncate-lines'.
26717 For any other non-nil value, truncate lines in all windows that do
26718 not span the full frame width.
26720 A value of nil means to respect the value of `truncate-lines'.
26722 If `word-wrap' is enabled, you might want to reduce this. */);
26723 Vtruncate_partial_width_windows = make_number (50);
26725 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
26726 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26727 Any other value means to use the appropriate face, `mode-line',
26728 `header-line', or `menu' respectively. */);
26729 mode_line_inverse_video = 1;
26731 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
26732 doc: /* *Maximum buffer size for which line number should be displayed.
26733 If the buffer is bigger than this, the line number does not appear
26734 in the mode line. A value of nil means no limit. */);
26735 Vline_number_display_limit = Qnil;
26737 DEFVAR_INT ("line-number-display-limit-width",
26738 &line_number_display_limit_width,
26739 doc: /* *Maximum line width (in characters) for line number display.
26740 If the average length of the lines near point is bigger than this, then the
26741 line number may be omitted from the mode line. */);
26742 line_number_display_limit_width = 200;
26744 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
26745 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26746 highlight_nonselected_windows = 0;
26748 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
26749 doc: /* Non-nil if more than one frame is visible on this display.
26750 Minibuffer-only frames don't count, but iconified frames do.
26751 This variable is not guaranteed to be accurate except while processing
26752 `frame-title-format' and `icon-title-format'. */);
26754 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
26755 doc: /* Template for displaying the title bar of visible frames.
26756 \(Assuming the window manager supports this feature.)
26758 This variable has the same structure as `mode-line-format', except that
26759 the %c and %l constructs are ignored. It is used only on frames for
26760 which no explicit name has been set \(see `modify-frame-parameters'). */);
26762 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
26763 doc: /* Template for displaying the title bar of an iconified frame.
26764 \(Assuming the window manager supports this feature.)
26765 This variable has the same structure as `mode-line-format' (which see),
26766 and is used only on frames for which no explicit name has been set
26767 \(see `modify-frame-parameters'). */);
26768 Vicon_title_format
26769 = Vframe_title_format
26770 = pure_cons (intern_c_string ("multiple-frames"),
26771 pure_cons (make_pure_c_string ("%b"),
26772 pure_cons (pure_cons (empty_unibyte_string,
26773 pure_cons (intern_c_string ("invocation-name"),
26774 pure_cons (make_pure_c_string ("@"),
26775 pure_cons (intern_c_string ("system-name"),
26776 Qnil)))),
26777 Qnil)));
26779 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
26780 doc: /* Maximum number of lines to keep in the message log buffer.
26781 If nil, disable message logging. If t, log messages but don't truncate
26782 the buffer when it becomes large. */);
26783 Vmessage_log_max = make_number (100);
26785 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
26786 doc: /* Functions called before redisplay, if window sizes have changed.
26787 The value should be a list of functions that take one argument.
26788 Just before redisplay, for each frame, if any of its windows have changed
26789 size since the last redisplay, or have been split or deleted,
26790 all the functions in the list are called, with the frame as argument. */);
26791 Vwindow_size_change_functions = Qnil;
26793 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
26794 doc: /* List of functions to call before redisplaying a window with scrolling.
26795 Each function is called with two arguments, the window and its new
26796 display-start position. Note that these functions are also called by
26797 `set-window-buffer'. Also note that the value of `window-end' is not
26798 valid when these functions are called. */);
26799 Vwindow_scroll_functions = Qnil;
26801 DEFVAR_LISP ("window-text-change-functions",
26802 &Vwindow_text_change_functions,
26803 doc: /* Functions to call in redisplay when text in the window might change. */);
26804 Vwindow_text_change_functions = Qnil;
26806 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
26807 doc: /* Functions called when redisplay of a window reaches the end trigger.
26808 Each function is called with two arguments, the window and the end trigger value.
26809 See `set-window-redisplay-end-trigger'. */);
26810 Vredisplay_end_trigger_functions = Qnil;
26812 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
26813 doc: /* *Non-nil means autoselect window with mouse pointer.
26814 If nil, do not autoselect windows.
26815 A positive number means delay autoselection by that many seconds: a
26816 window is autoselected only after the mouse has remained in that
26817 window for the duration of the delay.
26818 A negative number has a similar effect, but causes windows to be
26819 autoselected only after the mouse has stopped moving. \(Because of
26820 the way Emacs compares mouse events, you will occasionally wait twice
26821 that time before the window gets selected.\)
26822 Any other value means to autoselect window instantaneously when the
26823 mouse pointer enters it.
26825 Autoselection selects the minibuffer only if it is active, and never
26826 unselects the minibuffer if it is active.
26828 When customizing this variable make sure that the actual value of
26829 `focus-follows-mouse' matches the behavior of your window manager. */);
26830 Vmouse_autoselect_window = Qnil;
26832 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
26833 doc: /* *Non-nil means automatically resize tool-bars.
26834 This dynamically changes the tool-bar's height to the minimum height
26835 that is needed to make all tool-bar items visible.
26836 If value is `grow-only', the tool-bar's height is only increased
26837 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26838 Vauto_resize_tool_bars = Qt;
26840 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
26841 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26842 auto_raise_tool_bar_buttons_p = 1;
26844 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
26845 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26846 make_cursor_line_fully_visible_p = 1;
26848 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26849 doc: /* *Border below tool-bar in pixels.
26850 If an integer, use it as the height of the border.
26851 If it is one of `internal-border-width' or `border-width', use the
26852 value of the corresponding frame parameter.
26853 Otherwise, no border is added below the tool-bar. */);
26854 Vtool_bar_border = Qinternal_border_width;
26856 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26857 doc: /* *Margin around tool-bar buttons in pixels.
26858 If an integer, use that for both horizontal and vertical margins.
26859 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26860 HORZ specifying the horizontal margin, and VERT specifying the
26861 vertical margin. */);
26862 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26864 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26865 doc: /* *Relief thickness of tool-bar buttons. */);
26866 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26868 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26869 doc: /* *Tool bar style to use.
26870 It can be one of
26871 image - show images only
26872 text - show text only
26873 both - show both, text below image
26874 both-horiz - show text to the right of the image
26875 text-image-horiz - show text to the left of the image
26876 any other - use system default or image if no system default. */);
26877 Vtool_bar_style = Qnil;
26879 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26880 doc: /* *Maximum number of characters a label can have to be shown.
26881 The tool bar style must also show labels for this to have any effect, see
26882 `tool-bar-style'. */);
26883 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26885 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26886 doc: /* List of functions to call to fontify regions of text.
26887 Each function is called with one argument POS. Functions must
26888 fontify a region starting at POS in the current buffer, and give
26889 fontified regions the property `fontified'. */);
26890 Vfontification_functions = Qnil;
26891 Fmake_variable_buffer_local (Qfontification_functions);
26893 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26894 &unibyte_display_via_language_environment,
26895 doc: /* *Non-nil means display unibyte text according to language environment.
26896 Specifically, this means that raw bytes in the range 160-255 decimal
26897 are displayed by converting them to the equivalent multibyte characters
26898 according to the current language environment. As a result, they are
26899 displayed according to the current fontset.
26901 Note that this variable affects only how these bytes are displayed,
26902 but does not change the fact they are interpreted as raw bytes. */);
26903 unibyte_display_via_language_environment = 0;
26905 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26906 doc: /* *Maximum height for resizing mini-windows.
26907 If a float, it specifies a fraction of the mini-window frame's height.
26908 If an integer, it specifies a number of lines. */);
26909 Vmax_mini_window_height = make_float (0.25);
26911 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26912 doc: /* *How to resize mini-windows.
26913 A value of nil means don't automatically resize mini-windows.
26914 A value of t means resize them to fit the text displayed in them.
26915 A value of `grow-only', the default, means let mini-windows grow
26916 only, until their display becomes empty, at which point the windows
26917 go back to their normal size. */);
26918 Vresize_mini_windows = Qgrow_only;
26920 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26921 doc: /* Alist specifying how to blink the cursor off.
26922 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26923 `cursor-type' frame-parameter or variable equals ON-STATE,
26924 comparing using `equal', Emacs uses OFF-STATE to specify
26925 how to blink it off. ON-STATE and OFF-STATE are values for
26926 the `cursor-type' frame parameter.
26928 If a frame's ON-STATE has no entry in this list,
26929 the frame's other specifications determine how to blink the cursor off. */);
26930 Vblink_cursor_alist = Qnil;
26932 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26933 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26934 If non-nil, windows are automatically scrolled horizontally to make
26935 point visible. */);
26936 automatic_hscrolling_p = 1;
26937 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26938 staticpro (&Qauto_hscroll_mode);
26940 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26941 doc: /* *How many columns away from the window edge point is allowed to get
26942 before automatic hscrolling will horizontally scroll the window. */);
26943 hscroll_margin = 5;
26945 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26946 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26947 When point is less than `hscroll-margin' columns from the window
26948 edge, automatic hscrolling will scroll the window by the amount of columns
26949 determined by this variable. If its value is a positive integer, scroll that
26950 many columns. If it's a positive floating-point number, it specifies the
26951 fraction of the window's width to scroll. If it's nil or zero, point will be
26952 centered horizontally after the scroll. Any other value, including negative
26953 numbers, are treated as if the value were zero.
26955 Automatic hscrolling always moves point outside the scroll margin, so if
26956 point was more than scroll step columns inside the margin, the window will
26957 scroll more than the value given by the scroll step.
26959 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26960 and `scroll-right' overrides this variable's effect. */);
26961 Vhscroll_step = make_number (0);
26963 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26964 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26965 Bind this around calls to `message' to let it take effect. */);
26966 message_truncate_lines = 0;
26968 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26969 doc: /* Normal hook run to update the menu bar definitions.
26970 Redisplay runs this hook before it redisplays the menu bar.
26971 This is used to update submenus such as Buffers,
26972 whose contents depend on various data. */);
26973 Vmenu_bar_update_hook = Qnil;
26975 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26976 doc: /* Frame for which we are updating a menu.
26977 The enable predicate for a menu binding should check this variable. */);
26978 Vmenu_updating_frame = Qnil;
26980 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26981 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26982 inhibit_menubar_update = 0;
26984 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26985 doc: /* Prefix prepended to all continuation lines at display time.
26986 The value may be a string, an image, or a stretch-glyph; it is
26987 interpreted in the same way as the value of a `display' text property.
26989 This variable is overridden by any `wrap-prefix' text or overlay
26990 property.
26992 To add a prefix to non-continuation lines, use `line-prefix'. */);
26993 Vwrap_prefix = Qnil;
26994 staticpro (&Qwrap_prefix);
26995 Qwrap_prefix = intern_c_string ("wrap-prefix");
26996 Fmake_variable_buffer_local (Qwrap_prefix);
26998 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26999 doc: /* Prefix prepended to all non-continuation lines at display time.
27000 The value may be a string, an image, or a stretch-glyph; it is
27001 interpreted in the same way as the value of a `display' text property.
27003 This variable is overridden by any `line-prefix' text or overlay
27004 property.
27006 To add a prefix to continuation lines, use `wrap-prefix'. */);
27007 Vline_prefix = Qnil;
27008 staticpro (&Qline_prefix);
27009 Qline_prefix = intern_c_string ("line-prefix");
27010 Fmake_variable_buffer_local (Qline_prefix);
27012 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
27013 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27014 inhibit_eval_during_redisplay = 0;
27016 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
27017 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27018 inhibit_free_realized_faces = 0;
27020 #if GLYPH_DEBUG
27021 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
27022 doc: /* Inhibit try_window_id display optimization. */);
27023 inhibit_try_window_id = 0;
27025 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
27026 doc: /* Inhibit try_window_reusing display optimization. */);
27027 inhibit_try_window_reusing = 0;
27029 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
27030 doc: /* Inhibit try_cursor_movement display optimization. */);
27031 inhibit_try_cursor_movement = 0;
27032 #endif /* GLYPH_DEBUG */
27034 DEFVAR_INT ("overline-margin", &overline_margin,
27035 doc: /* *Space between overline and text, in pixels.
27036 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27037 margin to the caracter height. */);
27038 overline_margin = 2;
27040 DEFVAR_INT ("underline-minimum-offset",
27041 &underline_minimum_offset,
27042 doc: /* Minimum distance between baseline and underline.
27043 This can improve legibility of underlined text at small font sizes,
27044 particularly when using variable `x-use-underline-position-properties'
27045 with fonts that specify an UNDERLINE_POSITION relatively close to the
27046 baseline. The default value is 1. */);
27047 underline_minimum_offset = 1;
27049 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
27050 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27051 This feature only works when on a window system that can change
27052 cursor shapes. */);
27053 display_hourglass_p = 1;
27055 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
27056 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27057 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27059 hourglass_atimer = NULL;
27060 hourglass_shown_p = 0;
27062 DEFSYM (Qglyphless_char, "glyphless-char");
27063 DEFSYM (Qhex_code, "hex-code");
27064 DEFSYM (Qempty_box, "empty-box");
27065 DEFSYM (Qthin_space, "thin-space");
27066 DEFSYM (Qzero_width, "zero-width");
27068 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27069 /* Intern this now in case it isn't already done.
27070 Setting this variable twice is harmless.
27071 But don't staticpro it here--that is done in alloc.c. */
27072 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27073 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27075 DEFVAR_LISP ("glyphless-char-display", &Vglyphless_char_display,
27076 doc: /* Char-table to control displaying of glyphless characters.
27077 Each element, if non-nil, is an ASCII acronym string (displayed in a box)
27078 or one of these symbols:
27079 hex-code: display the hexadecimal code of a character in a box
27080 empty-box: display as an empty box
27081 thin-space: display as 1-pixel width space
27082 zero-width: don't display
27084 It has one extra slot to control the display of a character for which
27085 no font is found. The value of the slot is `hex-code' or `empty-box'.
27086 The default is `empty-box'. */);
27087 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27088 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27089 Qempty_box);
27093 /* Initialize this module when Emacs starts. */
27095 void
27096 init_xdisp (void)
27098 Lisp_Object root_window;
27099 struct window *mini_w;
27101 current_header_line_height = current_mode_line_height = -1;
27103 CHARPOS (this_line_start_pos) = 0;
27105 mini_w = XWINDOW (minibuf_window);
27106 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27108 if (!noninteractive)
27110 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27111 int i;
27113 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27114 set_window_height (root_window,
27115 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27117 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27118 set_window_height (minibuf_window, 1, 0);
27120 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27121 mini_w->total_cols = make_number (FRAME_COLS (f));
27123 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27124 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27125 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27127 /* The default ellipsis glyphs `...'. */
27128 for (i = 0; i < 3; ++i)
27129 default_invis_vector[i] = make_number ('.');
27133 /* Allocate the buffer for frame titles.
27134 Also used for `format-mode-line'. */
27135 int size = 100;
27136 mode_line_noprop_buf = (char *) xmalloc (size);
27137 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27138 mode_line_noprop_ptr = mode_line_noprop_buf;
27139 mode_line_target = MODE_LINE_DISPLAY;
27142 help_echo_showing_p = 0;
27145 /* Since w32 does not support atimers, it defines its own implementation of
27146 the following three functions in w32fns.c. */
27147 #ifndef WINDOWSNT
27149 /* Platform-independent portion of hourglass implementation. */
27151 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27153 hourglass_started (void)
27155 return hourglass_shown_p || hourglass_atimer != NULL;
27158 /* Cancel a currently active hourglass timer, and start a new one. */
27159 void
27160 start_hourglass (void)
27162 #if defined (HAVE_WINDOW_SYSTEM)
27163 EMACS_TIME delay;
27164 int secs, usecs = 0;
27166 cancel_hourglass ();
27168 if (INTEGERP (Vhourglass_delay)
27169 && XINT (Vhourglass_delay) > 0)
27170 secs = XFASTINT (Vhourglass_delay);
27171 else if (FLOATP (Vhourglass_delay)
27172 && XFLOAT_DATA (Vhourglass_delay) > 0)
27174 Lisp_Object tem;
27175 tem = Ftruncate (Vhourglass_delay, Qnil);
27176 secs = XFASTINT (tem);
27177 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27179 else
27180 secs = DEFAULT_HOURGLASS_DELAY;
27182 EMACS_SET_SECS_USECS (delay, secs, usecs);
27183 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27184 show_hourglass, NULL);
27185 #endif
27189 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27190 shown. */
27191 void
27192 cancel_hourglass (void)
27194 #if defined (HAVE_WINDOW_SYSTEM)
27195 if (hourglass_atimer)
27197 cancel_atimer (hourglass_atimer);
27198 hourglass_atimer = NULL;
27201 if (hourglass_shown_p)
27202 hide_hourglass ();
27203 #endif
27205 #endif /* ! WINDOWSNT */