Support for menu separators in the GTK tool-bar.
[emacs.git] / src / xdisp.c
blob41204e0a5b4ae012d72368b047219f28c78bffe6
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. Return in *LEN the length of
1548 the character. This is like STRING_CHAR_AND_LENGTH but never
1549 returns an invalid character. If we find one, we return a `?', but
1550 with the length of the invalid character. */
1552 static INLINE int
1553 string_char_and_length (const unsigned char *str, int *len)
1555 int c;
1557 c = STRING_CHAR_AND_LENGTH (str, *len);
1558 if (!CHAR_VALID_P (c, 1))
1559 /* We may not change the length here because other places in Emacs
1560 don't use this function, i.e. they silently accept invalid
1561 characters. */
1562 c = '?';
1564 return c;
1569 /* Given a position POS containing a valid character and byte position
1570 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1572 static struct text_pos
1573 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1575 xassert (STRINGP (string) && nchars >= 0);
1577 if (STRING_MULTIBYTE (string))
1579 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1580 int len;
1582 while (nchars--)
1584 string_char_and_length (p, &len);
1585 p += len;
1586 CHARPOS (pos) += 1;
1587 BYTEPOS (pos) += len;
1590 else
1591 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1593 return pos;
1597 /* Value is the text position, i.e. character and byte position,
1598 for character position CHARPOS in STRING. */
1600 static INLINE struct text_pos
1601 string_pos (EMACS_INT charpos, Lisp_Object string)
1603 struct text_pos pos;
1604 xassert (STRINGP (string));
1605 xassert (charpos >= 0);
1606 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1607 return pos;
1611 /* Value is a text position, i.e. character and byte position, for
1612 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1613 means recognize multibyte characters. */
1615 static struct text_pos
1616 c_string_pos (EMACS_INT charpos, const unsigned char *s, int multibyte_p)
1618 struct text_pos pos;
1620 xassert (s != NULL);
1621 xassert (charpos >= 0);
1623 if (multibyte_p)
1625 int len;
1627 SET_TEXT_POS (pos, 0, 0);
1628 while (charpos--)
1630 string_char_and_length (s, &len);
1631 s += len;
1632 CHARPOS (pos) += 1;
1633 BYTEPOS (pos) += len;
1636 else
1637 SET_TEXT_POS (pos, charpos, charpos);
1639 return pos;
1643 /* Value is the number of characters in C string S. MULTIBYTE_P
1644 non-zero means recognize multibyte characters. */
1646 static EMACS_INT
1647 number_of_chars (const unsigned char *s, int multibyte_p)
1649 EMACS_INT nchars;
1651 if (multibyte_p)
1653 EMACS_INT rest = strlen (s);
1654 int len;
1655 unsigned char *p = (unsigned char *) s;
1657 for (nchars = 0; rest > 0; ++nchars)
1659 string_char_and_length (p, &len);
1660 rest -= len, p += len;
1663 else
1664 nchars = strlen (s);
1666 return nchars;
1670 /* Compute byte position NEWPOS->bytepos corresponding to
1671 NEWPOS->charpos. POS is a known position in string STRING.
1672 NEWPOS->charpos must be >= POS.charpos. */
1674 static void
1675 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1677 xassert (STRINGP (string));
1678 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1680 if (STRING_MULTIBYTE (string))
1681 *newpos = string_pos_nchars_ahead (pos, string,
1682 CHARPOS (*newpos) - CHARPOS (pos));
1683 else
1684 BYTEPOS (*newpos) = CHARPOS (*newpos);
1687 /* EXPORT:
1688 Return an estimation of the pixel height of mode or header lines on
1689 frame F. FACE_ID specifies what line's height to estimate. */
1692 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1694 #ifdef HAVE_WINDOW_SYSTEM
1695 if (FRAME_WINDOW_P (f))
1697 int height = FONT_HEIGHT (FRAME_FONT (f));
1699 /* This function is called so early when Emacs starts that the face
1700 cache and mode line face are not yet initialized. */
1701 if (FRAME_FACE_CACHE (f))
1703 struct face *face = FACE_FROM_ID (f, face_id);
1704 if (face)
1706 if (face->font)
1707 height = FONT_HEIGHT (face->font);
1708 if (face->box_line_width > 0)
1709 height += 2 * face->box_line_width;
1713 return height;
1715 #endif
1717 return 1;
1720 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1721 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1722 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1723 not force the value into range. */
1725 void
1726 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1727 int *x, int *y, NativeRectangle *bounds, int noclip)
1730 #ifdef HAVE_WINDOW_SYSTEM
1731 if (FRAME_WINDOW_P (f))
1733 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1734 even for negative values. */
1735 if (pix_x < 0)
1736 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1737 if (pix_y < 0)
1738 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1740 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1741 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1743 if (bounds)
1744 STORE_NATIVE_RECT (*bounds,
1745 FRAME_COL_TO_PIXEL_X (f, pix_x),
1746 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1747 FRAME_COLUMN_WIDTH (f) - 1,
1748 FRAME_LINE_HEIGHT (f) - 1);
1750 if (!noclip)
1752 if (pix_x < 0)
1753 pix_x = 0;
1754 else if (pix_x > FRAME_TOTAL_COLS (f))
1755 pix_x = FRAME_TOTAL_COLS (f);
1757 if (pix_y < 0)
1758 pix_y = 0;
1759 else if (pix_y > FRAME_LINES (f))
1760 pix_y = FRAME_LINES (f);
1763 #endif
1765 *x = pix_x;
1766 *y = pix_y;
1770 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1771 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1772 can't tell the positions because W's display is not up to date,
1773 return 0. */
1776 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1777 int *frame_x, int *frame_y)
1779 #ifdef HAVE_WINDOW_SYSTEM
1780 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1782 int success_p;
1784 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1785 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1787 if (display_completed)
1789 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1790 struct glyph *glyph = row->glyphs[TEXT_AREA];
1791 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1793 hpos = row->x;
1794 vpos = row->y;
1795 while (glyph < end)
1797 hpos += glyph->pixel_width;
1798 ++glyph;
1801 /* If first glyph is partially visible, its first visible position is still 0. */
1802 if (hpos < 0)
1803 hpos = 0;
1805 success_p = 1;
1807 else
1809 hpos = vpos = 0;
1810 success_p = 0;
1813 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1814 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1815 return success_p;
1817 #endif
1819 *frame_x = hpos;
1820 *frame_y = vpos;
1821 return 1;
1825 /* Find the glyph under window-relative coordinates X/Y in window W.
1826 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1827 strings. Return in *HPOS and *VPOS the row and column number of
1828 the glyph found. Return in *AREA the glyph area containing X.
1829 Value is a pointer to the glyph found or null if X/Y is not on
1830 text, or we can't tell because W's current matrix is not up to
1831 date. */
1833 static
1834 struct glyph *
1835 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1836 int *dx, int *dy, int *area)
1838 struct glyph *glyph, *end;
1839 struct glyph_row *row = NULL;
1840 int x0, i;
1842 /* Find row containing Y. Give up if some row is not enabled. */
1843 for (i = 0; i < w->current_matrix->nrows; ++i)
1845 row = MATRIX_ROW (w->current_matrix, i);
1846 if (!row->enabled_p)
1847 return NULL;
1848 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1849 break;
1852 *vpos = i;
1853 *hpos = 0;
1855 /* Give up if Y is not in the window. */
1856 if (i == w->current_matrix->nrows)
1857 return NULL;
1859 /* Get the glyph area containing X. */
1860 if (w->pseudo_window_p)
1862 *area = TEXT_AREA;
1863 x0 = 0;
1865 else
1867 if (x < window_box_left_offset (w, TEXT_AREA))
1869 *area = LEFT_MARGIN_AREA;
1870 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1872 else if (x < window_box_right_offset (w, TEXT_AREA))
1874 *area = TEXT_AREA;
1875 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1877 else
1879 *area = RIGHT_MARGIN_AREA;
1880 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1884 /* Find glyph containing X. */
1885 glyph = row->glyphs[*area];
1886 end = glyph + row->used[*area];
1887 x -= x0;
1888 while (glyph < end && x >= glyph->pixel_width)
1890 x -= glyph->pixel_width;
1891 ++glyph;
1894 if (glyph == end)
1895 return NULL;
1897 if (dx)
1899 *dx = x;
1900 *dy = y - (row->y + row->ascent - glyph->ascent);
1903 *hpos = glyph - row->glyphs[*area];
1904 return glyph;
1907 /* EXPORT:
1908 Convert frame-relative x/y to coordinates relative to window W.
1909 Takes pseudo-windows into account. */
1911 void
1912 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1914 if (w->pseudo_window_p)
1916 /* A pseudo-window is always full-width, and starts at the
1917 left edge of the frame, plus a frame border. */
1918 struct frame *f = XFRAME (w->frame);
1919 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1920 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1922 else
1924 *x -= WINDOW_LEFT_EDGE_X (w);
1925 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1929 #ifdef HAVE_WINDOW_SYSTEM
1931 /* EXPORT:
1932 Return in RECTS[] at most N clipping rectangles for glyph string S.
1933 Return the number of stored rectangles. */
1936 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1938 XRectangle r;
1940 if (n <= 0)
1941 return 0;
1943 if (s->row->full_width_p)
1945 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1946 r.x = WINDOW_LEFT_EDGE_X (s->w);
1947 r.width = WINDOW_TOTAL_WIDTH (s->w);
1949 /* Unless displaying a mode or menu bar line, which are always
1950 fully visible, clip to the visible part of the row. */
1951 if (s->w->pseudo_window_p)
1952 r.height = s->row->visible_height;
1953 else
1954 r.height = s->height;
1956 else
1958 /* This is a text line that may be partially visible. */
1959 r.x = window_box_left (s->w, s->area);
1960 r.width = window_box_width (s->w, s->area);
1961 r.height = s->row->visible_height;
1964 if (s->clip_head)
1965 if (r.x < s->clip_head->x)
1967 if (r.width >= s->clip_head->x - r.x)
1968 r.width -= s->clip_head->x - r.x;
1969 else
1970 r.width = 0;
1971 r.x = s->clip_head->x;
1973 if (s->clip_tail)
1974 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1976 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1977 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1978 else
1979 r.width = 0;
1982 /* If S draws overlapping rows, it's sufficient to use the top and
1983 bottom of the window for clipping because this glyph string
1984 intentionally draws over other lines. */
1985 if (s->for_overlaps)
1987 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1988 r.height = window_text_bottom_y (s->w) - r.y;
1990 /* Alas, the above simple strategy does not work for the
1991 environments with anti-aliased text: if the same text is
1992 drawn onto the same place multiple times, it gets thicker.
1993 If the overlap we are processing is for the erased cursor, we
1994 take the intersection with the rectagle of the cursor. */
1995 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1997 XRectangle rc, r_save = r;
1999 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2000 rc.y = s->w->phys_cursor.y;
2001 rc.width = s->w->phys_cursor_width;
2002 rc.height = s->w->phys_cursor_height;
2004 x_intersect_rectangles (&r_save, &rc, &r);
2007 else
2009 /* Don't use S->y for clipping because it doesn't take partially
2010 visible lines into account. For example, it can be negative for
2011 partially visible lines at the top of a window. */
2012 if (!s->row->full_width_p
2013 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2014 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2015 else
2016 r.y = max (0, s->row->y);
2019 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2021 /* If drawing the cursor, don't let glyph draw outside its
2022 advertised boundaries. Cleartype does this under some circumstances. */
2023 if (s->hl == DRAW_CURSOR)
2025 struct glyph *glyph = s->first_glyph;
2026 int height, max_y;
2028 if (s->x > r.x)
2030 r.width -= s->x - r.x;
2031 r.x = s->x;
2033 r.width = min (r.width, glyph->pixel_width);
2035 /* If r.y is below window bottom, ensure that we still see a cursor. */
2036 height = min (glyph->ascent + glyph->descent,
2037 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2038 max_y = window_text_bottom_y (s->w) - height;
2039 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2040 if (s->ybase - glyph->ascent > max_y)
2042 r.y = max_y;
2043 r.height = height;
2045 else
2047 /* Don't draw cursor glyph taller than our actual glyph. */
2048 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2049 if (height < r.height)
2051 max_y = r.y + r.height;
2052 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2053 r.height = min (max_y - r.y, height);
2058 if (s->row->clip)
2060 XRectangle r_save = r;
2062 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2063 r.width = 0;
2066 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2067 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2069 #ifdef CONVERT_FROM_XRECT
2070 CONVERT_FROM_XRECT (r, *rects);
2071 #else
2072 *rects = r;
2073 #endif
2074 return 1;
2076 else
2078 /* If we are processing overlapping and allowed to return
2079 multiple clipping rectangles, we exclude the row of the glyph
2080 string from the clipping rectangle. This is to avoid drawing
2081 the same text on the environment with anti-aliasing. */
2082 #ifdef CONVERT_FROM_XRECT
2083 XRectangle rs[2];
2084 #else
2085 XRectangle *rs = rects;
2086 #endif
2087 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2089 if (s->for_overlaps & OVERLAPS_PRED)
2091 rs[i] = r;
2092 if (r.y + r.height > row_y)
2094 if (r.y < row_y)
2095 rs[i].height = row_y - r.y;
2096 else
2097 rs[i].height = 0;
2099 i++;
2101 if (s->for_overlaps & OVERLAPS_SUCC)
2103 rs[i] = r;
2104 if (r.y < row_y + s->row->visible_height)
2106 if (r.y + r.height > row_y + s->row->visible_height)
2108 rs[i].y = row_y + s->row->visible_height;
2109 rs[i].height = r.y + r.height - rs[i].y;
2111 else
2112 rs[i].height = 0;
2114 i++;
2117 n = i;
2118 #ifdef CONVERT_FROM_XRECT
2119 for (i = 0; i < n; i++)
2120 CONVERT_FROM_XRECT (rs[i], rects[i]);
2121 #endif
2122 return n;
2126 /* EXPORT:
2127 Return in *NR the clipping rectangle for glyph string S. */
2129 void
2130 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2132 get_glyph_string_clip_rects (s, nr, 1);
2136 /* EXPORT:
2137 Return the position and height of the phys cursor in window W.
2138 Set w->phys_cursor_width to width of phys cursor.
2141 void
2142 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2143 struct glyph *glyph, int *xp, int *yp, int *heightp)
2145 struct frame *f = XFRAME (WINDOW_FRAME (w));
2146 int x, y, wd, h, h0, y0;
2148 /* Compute the width of the rectangle to draw. If on a stretch
2149 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2150 rectangle as wide as the glyph, but use a canonical character
2151 width instead. */
2152 wd = glyph->pixel_width - 1;
2153 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2154 wd++; /* Why? */
2155 #endif
2157 x = w->phys_cursor.x;
2158 if (x < 0)
2160 wd += x;
2161 x = 0;
2164 if (glyph->type == STRETCH_GLYPH
2165 && !x_stretch_cursor_p)
2166 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2167 w->phys_cursor_width = wd;
2169 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2171 /* If y is below window bottom, ensure that we still see a cursor. */
2172 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2174 h = max (h0, glyph->ascent + glyph->descent);
2175 h0 = min (h0, glyph->ascent + glyph->descent);
2177 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2178 if (y < y0)
2180 h = max (h - (y0 - y) + 1, h0);
2181 y = y0 - 1;
2183 else
2185 y0 = window_text_bottom_y (w) - h0;
2186 if (y > y0)
2188 h += y - y0;
2189 y = y0;
2193 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2194 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2195 *heightp = h;
2199 * Remember which glyph the mouse is over.
2202 void
2203 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2205 Lisp_Object window;
2206 struct window *w;
2207 struct glyph_row *r, *gr, *end_row;
2208 enum window_part part;
2209 enum glyph_row_area area;
2210 int x, y, width, height;
2212 /* Try to determine frame pixel position and size of the glyph under
2213 frame pixel coordinates X/Y on frame F. */
2215 if (!f->glyphs_initialized_p
2216 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2217 NILP (window)))
2219 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2220 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2221 goto virtual_glyph;
2224 w = XWINDOW (window);
2225 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2226 height = WINDOW_FRAME_LINE_HEIGHT (w);
2228 x = window_relative_x_coord (w, part, gx);
2229 y = gy - WINDOW_TOP_EDGE_Y (w);
2231 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2232 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2234 if (w->pseudo_window_p)
2236 area = TEXT_AREA;
2237 part = ON_MODE_LINE; /* Don't adjust margin. */
2238 goto text_glyph;
2241 switch (part)
2243 case ON_LEFT_MARGIN:
2244 area = LEFT_MARGIN_AREA;
2245 goto text_glyph;
2247 case ON_RIGHT_MARGIN:
2248 area = RIGHT_MARGIN_AREA;
2249 goto text_glyph;
2251 case ON_HEADER_LINE:
2252 case ON_MODE_LINE:
2253 gr = (part == ON_HEADER_LINE
2254 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2255 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2256 gy = gr->y;
2257 area = TEXT_AREA;
2258 goto text_glyph_row_found;
2260 case ON_TEXT:
2261 area = TEXT_AREA;
2263 text_glyph:
2264 gr = 0; gy = 0;
2265 for (; r <= end_row && r->enabled_p; ++r)
2266 if (r->y + r->height > y)
2268 gr = r; gy = r->y;
2269 break;
2272 text_glyph_row_found:
2273 if (gr && gy <= y)
2275 struct glyph *g = gr->glyphs[area];
2276 struct glyph *end = g + gr->used[area];
2278 height = gr->height;
2279 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2280 if (gx + g->pixel_width > x)
2281 break;
2283 if (g < end)
2285 if (g->type == IMAGE_GLYPH)
2287 /* Don't remember when mouse is over image, as
2288 image may have hot-spots. */
2289 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2290 return;
2292 width = g->pixel_width;
2294 else
2296 /* Use nominal char spacing at end of line. */
2297 x -= gx;
2298 gx += (x / width) * width;
2301 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2302 gx += window_box_left_offset (w, area);
2304 else
2306 /* Use nominal line height at end of window. */
2307 gx = (x / width) * width;
2308 y -= gy;
2309 gy += (y / height) * height;
2311 break;
2313 case ON_LEFT_FRINGE:
2314 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2315 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2316 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2317 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2318 goto row_glyph;
2320 case ON_RIGHT_FRINGE:
2321 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2322 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2323 : window_box_right_offset (w, TEXT_AREA));
2324 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2325 goto row_glyph;
2327 case ON_SCROLL_BAR:
2328 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2330 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2331 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2332 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2333 : 0)));
2334 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2336 row_glyph:
2337 gr = 0, gy = 0;
2338 for (; r <= end_row && r->enabled_p; ++r)
2339 if (r->y + r->height > y)
2341 gr = r; gy = r->y;
2342 break;
2345 if (gr && gy <= y)
2346 height = gr->height;
2347 else
2349 /* Use nominal line height at end of window. */
2350 y -= gy;
2351 gy += (y / height) * height;
2353 break;
2355 default:
2357 virtual_glyph:
2358 /* If there is no glyph under the mouse, then we divide the screen
2359 into a grid of the smallest glyph in the frame, and use that
2360 as our "glyph". */
2362 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2363 round down even for negative values. */
2364 if (gx < 0)
2365 gx -= width - 1;
2366 if (gy < 0)
2367 gy -= height - 1;
2369 gx = (gx / width) * width;
2370 gy = (gy / height) * height;
2372 goto store_rect;
2375 gx += WINDOW_LEFT_EDGE_X (w);
2376 gy += WINDOW_TOP_EDGE_Y (w);
2378 store_rect:
2379 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2381 /* Visible feedback for debugging. */
2382 #if 0
2383 #if HAVE_X_WINDOWS
2384 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2385 f->output_data.x->normal_gc,
2386 gx, gy, width, height);
2387 #endif
2388 #endif
2392 #endif /* HAVE_WINDOW_SYSTEM */
2395 /***********************************************************************
2396 Lisp form evaluation
2397 ***********************************************************************/
2399 /* Error handler for safe_eval and safe_call. */
2401 static Lisp_Object
2402 safe_eval_handler (Lisp_Object arg)
2404 add_to_log ("Error during redisplay: %s", arg, Qnil);
2405 return Qnil;
2409 /* Evaluate SEXPR and return the result, or nil if something went
2410 wrong. Prevent redisplay during the evaluation. */
2412 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2413 Return the result, or nil if something went wrong. Prevent
2414 redisplay during the evaluation. */
2416 Lisp_Object
2417 safe_call (int nargs, Lisp_Object *args)
2419 Lisp_Object val;
2421 if (inhibit_eval_during_redisplay)
2422 val = Qnil;
2423 else
2425 int count = SPECPDL_INDEX ();
2426 struct gcpro gcpro1;
2428 GCPRO1 (args[0]);
2429 gcpro1.nvars = nargs;
2430 specbind (Qinhibit_redisplay, Qt);
2431 /* Use Qt to ensure debugger does not run,
2432 so there is no possibility of wanting to redisplay. */
2433 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2434 safe_eval_handler);
2435 UNGCPRO;
2436 val = unbind_to (count, val);
2439 return val;
2443 /* Call function FN with one argument ARG.
2444 Return the result, or nil if something went wrong. */
2446 Lisp_Object
2447 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2449 Lisp_Object args[2];
2450 args[0] = fn;
2451 args[1] = arg;
2452 return safe_call (2, args);
2455 static Lisp_Object Qeval;
2457 Lisp_Object
2458 safe_eval (Lisp_Object sexpr)
2460 return safe_call1 (Qeval, sexpr);
2463 /* Call function FN with one argument ARG.
2464 Return the result, or nil if something went wrong. */
2466 Lisp_Object
2467 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2469 Lisp_Object args[3];
2470 args[0] = fn;
2471 args[1] = arg1;
2472 args[2] = arg2;
2473 return safe_call (3, args);
2478 /***********************************************************************
2479 Debugging
2480 ***********************************************************************/
2482 #if 0
2484 /* Define CHECK_IT to perform sanity checks on iterators.
2485 This is for debugging. It is too slow to do unconditionally. */
2487 static void
2488 check_it (it)
2489 struct it *it;
2491 if (it->method == GET_FROM_STRING)
2493 xassert (STRINGP (it->string));
2494 xassert (IT_STRING_CHARPOS (*it) >= 0);
2496 else
2498 xassert (IT_STRING_CHARPOS (*it) < 0);
2499 if (it->method == GET_FROM_BUFFER)
2501 /* Check that character and byte positions agree. */
2502 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2506 if (it->dpvec)
2507 xassert (it->current.dpvec_index >= 0);
2508 else
2509 xassert (it->current.dpvec_index < 0);
2512 #define CHECK_IT(IT) check_it ((IT))
2514 #else /* not 0 */
2516 #define CHECK_IT(IT) (void) 0
2518 #endif /* not 0 */
2521 #if GLYPH_DEBUG
2523 /* Check that the window end of window W is what we expect it
2524 to be---the last row in the current matrix displaying text. */
2526 static void
2527 check_window_end (w)
2528 struct window *w;
2530 if (!MINI_WINDOW_P (w)
2531 && !NILP (w->window_end_valid))
2533 struct glyph_row *row;
2534 xassert ((row = MATRIX_ROW (w->current_matrix,
2535 XFASTINT (w->window_end_vpos)),
2536 !row->enabled_p
2537 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2538 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2542 #define CHECK_WINDOW_END(W) check_window_end ((W))
2544 #else /* not GLYPH_DEBUG */
2546 #define CHECK_WINDOW_END(W) (void) 0
2548 #endif /* not GLYPH_DEBUG */
2552 /***********************************************************************
2553 Iterator initialization
2554 ***********************************************************************/
2556 /* Initialize IT for displaying current_buffer in window W, starting
2557 at character position CHARPOS. CHARPOS < 0 means that no buffer
2558 position is specified which is useful when the iterator is assigned
2559 a position later. BYTEPOS is the byte position corresponding to
2560 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2562 If ROW is not null, calls to produce_glyphs with IT as parameter
2563 will produce glyphs in that row.
2565 BASE_FACE_ID is the id of a base face to use. It must be one of
2566 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2567 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2568 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2570 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2571 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2572 will be initialized to use the corresponding mode line glyph row of
2573 the desired matrix of W. */
2575 void
2576 init_iterator (struct it *it, struct window *w,
2577 EMACS_INT charpos, EMACS_INT bytepos,
2578 struct glyph_row *row, enum face_id base_face_id)
2580 int highlight_region_p;
2581 enum face_id remapped_base_face_id = base_face_id;
2583 /* Some precondition checks. */
2584 xassert (w != NULL && it != NULL);
2585 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2586 && charpos <= ZV));
2588 /* If face attributes have been changed since the last redisplay,
2589 free realized faces now because they depend on face definitions
2590 that might have changed. Don't free faces while there might be
2591 desired matrices pending which reference these faces. */
2592 if (face_change_count && !inhibit_free_realized_faces)
2594 face_change_count = 0;
2595 free_all_realized_faces (Qnil);
2598 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2599 if (! NILP (Vface_remapping_alist))
2600 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2602 /* Use one of the mode line rows of W's desired matrix if
2603 appropriate. */
2604 if (row == NULL)
2606 if (base_face_id == MODE_LINE_FACE_ID
2607 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2608 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2609 else if (base_face_id == HEADER_LINE_FACE_ID)
2610 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2613 /* Clear IT. */
2614 memset (it, 0, sizeof *it);
2615 it->current.overlay_string_index = -1;
2616 it->current.dpvec_index = -1;
2617 it->base_face_id = remapped_base_face_id;
2618 it->string = Qnil;
2619 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2621 /* The window in which we iterate over current_buffer: */
2622 XSETWINDOW (it->window, w);
2623 it->w = w;
2624 it->f = XFRAME (w->frame);
2626 it->cmp_it.id = -1;
2628 /* Extra space between lines (on window systems only). */
2629 if (base_face_id == DEFAULT_FACE_ID
2630 && FRAME_WINDOW_P (it->f))
2632 if (NATNUMP (current_buffer->extra_line_spacing))
2633 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2634 else if (FLOATP (current_buffer->extra_line_spacing))
2635 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2636 * FRAME_LINE_HEIGHT (it->f));
2637 else if (it->f->extra_line_spacing > 0)
2638 it->extra_line_spacing = it->f->extra_line_spacing;
2639 it->max_extra_line_spacing = 0;
2642 /* If realized faces have been removed, e.g. because of face
2643 attribute changes of named faces, recompute them. When running
2644 in batch mode, the face cache of the initial frame is null. If
2645 we happen to get called, make a dummy face cache. */
2646 if (FRAME_FACE_CACHE (it->f) == NULL)
2647 init_frame_faces (it->f);
2648 if (FRAME_FACE_CACHE (it->f)->used == 0)
2649 recompute_basic_faces (it->f);
2651 /* Current value of the `slice', `space-width', and 'height' properties. */
2652 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2653 it->space_width = Qnil;
2654 it->font_height = Qnil;
2655 it->override_ascent = -1;
2657 /* Are control characters displayed as `^C'? */
2658 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2660 /* -1 means everything between a CR and the following line end
2661 is invisible. >0 means lines indented more than this value are
2662 invisible. */
2663 it->selective = (INTEGERP (current_buffer->selective_display)
2664 ? XFASTINT (current_buffer->selective_display)
2665 : (!NILP (current_buffer->selective_display)
2666 ? -1 : 0));
2667 it->selective_display_ellipsis_p
2668 = !NILP (current_buffer->selective_display_ellipses);
2670 /* Display table to use. */
2671 it->dp = window_display_table (w);
2673 /* Are multibyte characters enabled in current_buffer? */
2674 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2676 /* Do we need to reorder bidirectional text? Not if this is a
2677 unibyte buffer: by definition, none of the single-byte characters
2678 are strong R2L, so no reordering is needed. And bidi.c doesn't
2679 support unibyte buffers anyway. */
2680 it->bidi_p
2681 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2683 /* Non-zero if we should highlight the region. */
2684 highlight_region_p
2685 = (!NILP (Vtransient_mark_mode)
2686 && !NILP (current_buffer->mark_active)
2687 && XMARKER (current_buffer->mark)->buffer != 0);
2689 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2690 start and end of a visible region in window IT->w. Set both to
2691 -1 to indicate no region. */
2692 if (highlight_region_p
2693 /* Maybe highlight only in selected window. */
2694 && (/* Either show region everywhere. */
2695 highlight_nonselected_windows
2696 /* Or show region in the selected window. */
2697 || w == XWINDOW (selected_window)
2698 /* Or show the region if we are in the mini-buffer and W is
2699 the window the mini-buffer refers to. */
2700 || (MINI_WINDOW_P (XWINDOW (selected_window))
2701 && WINDOWP (minibuf_selected_window)
2702 && w == XWINDOW (minibuf_selected_window))))
2704 EMACS_INT charpos = marker_position (current_buffer->mark);
2705 it->region_beg_charpos = min (PT, charpos);
2706 it->region_end_charpos = max (PT, charpos);
2708 else
2709 it->region_beg_charpos = it->region_end_charpos = -1;
2711 /* Get the position at which the redisplay_end_trigger hook should
2712 be run, if it is to be run at all. */
2713 if (MARKERP (w->redisplay_end_trigger)
2714 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2715 it->redisplay_end_trigger_charpos
2716 = marker_position (w->redisplay_end_trigger);
2717 else if (INTEGERP (w->redisplay_end_trigger))
2718 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2720 /* Correct bogus values of tab_width. */
2721 it->tab_width = XINT (current_buffer->tab_width);
2722 if (it->tab_width <= 0 || it->tab_width > 1000)
2723 it->tab_width = 8;
2725 /* Are lines in the display truncated? */
2726 if (base_face_id != DEFAULT_FACE_ID
2727 || XINT (it->w->hscroll)
2728 || (! WINDOW_FULL_WIDTH_P (it->w)
2729 && ((!NILP (Vtruncate_partial_width_windows)
2730 && !INTEGERP (Vtruncate_partial_width_windows))
2731 || (INTEGERP (Vtruncate_partial_width_windows)
2732 && (WINDOW_TOTAL_COLS (it->w)
2733 < XINT (Vtruncate_partial_width_windows))))))
2734 it->line_wrap = TRUNCATE;
2735 else if (NILP (current_buffer->truncate_lines))
2736 it->line_wrap = NILP (current_buffer->word_wrap)
2737 ? WINDOW_WRAP : WORD_WRAP;
2738 else
2739 it->line_wrap = TRUNCATE;
2741 /* Get dimensions of truncation and continuation glyphs. These are
2742 displayed as fringe bitmaps under X, so we don't need them for such
2743 frames. */
2744 if (!FRAME_WINDOW_P (it->f))
2746 if (it->line_wrap == TRUNCATE)
2748 /* We will need the truncation glyph. */
2749 xassert (it->glyph_row == NULL);
2750 produce_special_glyphs (it, IT_TRUNCATION);
2751 it->truncation_pixel_width = it->pixel_width;
2753 else
2755 /* We will need the continuation glyph. */
2756 xassert (it->glyph_row == NULL);
2757 produce_special_glyphs (it, IT_CONTINUATION);
2758 it->continuation_pixel_width = it->pixel_width;
2761 /* Reset these values to zero because the produce_special_glyphs
2762 above has changed them. */
2763 it->pixel_width = it->ascent = it->descent = 0;
2764 it->phys_ascent = it->phys_descent = 0;
2767 /* Set this after getting the dimensions of truncation and
2768 continuation glyphs, so that we don't produce glyphs when calling
2769 produce_special_glyphs, above. */
2770 it->glyph_row = row;
2771 it->area = TEXT_AREA;
2773 /* Forget any previous info about this row being reversed. */
2774 if (it->glyph_row)
2775 it->glyph_row->reversed_p = 0;
2777 /* Get the dimensions of the display area. The display area
2778 consists of the visible window area plus a horizontally scrolled
2779 part to the left of the window. All x-values are relative to the
2780 start of this total display area. */
2781 if (base_face_id != DEFAULT_FACE_ID)
2783 /* Mode lines, menu bar in terminal frames. */
2784 it->first_visible_x = 0;
2785 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2787 else
2789 it->first_visible_x
2790 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2791 it->last_visible_x = (it->first_visible_x
2792 + window_box_width (w, TEXT_AREA));
2794 /* If we truncate lines, leave room for the truncator glyph(s) at
2795 the right margin. Otherwise, leave room for the continuation
2796 glyph(s). Truncation and continuation glyphs are not inserted
2797 for window-based redisplay. */
2798 if (!FRAME_WINDOW_P (it->f))
2800 if (it->line_wrap == TRUNCATE)
2801 it->last_visible_x -= it->truncation_pixel_width;
2802 else
2803 it->last_visible_x -= it->continuation_pixel_width;
2806 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2807 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2810 /* Leave room for a border glyph. */
2811 if (!FRAME_WINDOW_P (it->f)
2812 && !WINDOW_RIGHTMOST_P (it->w))
2813 it->last_visible_x -= 1;
2815 it->last_visible_y = window_text_bottom_y (w);
2817 /* For mode lines and alike, arrange for the first glyph having a
2818 left box line if the face specifies a box. */
2819 if (base_face_id != DEFAULT_FACE_ID)
2821 struct face *face;
2823 it->face_id = remapped_base_face_id;
2825 /* If we have a boxed mode line, make the first character appear
2826 with a left box line. */
2827 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2828 if (face->box != FACE_NO_BOX)
2829 it->start_of_box_run_p = 1;
2832 /* If we are to reorder bidirectional text, init the bidi
2833 iterator. */
2834 if (it->bidi_p)
2836 /* Note the paragraph direction that this buffer wants to
2837 use. */
2838 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2839 it->paragraph_embedding = L2R;
2840 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2841 it->paragraph_embedding = R2L;
2842 else
2843 it->paragraph_embedding = NEUTRAL_DIR;
2844 bidi_init_it (charpos, bytepos, &it->bidi_it);
2847 /* If a buffer position was specified, set the iterator there,
2848 getting overlays and face properties from that position. */
2849 if (charpos >= BUF_BEG (current_buffer))
2851 it->end_charpos = ZV;
2852 it->face_id = -1;
2853 IT_CHARPOS (*it) = charpos;
2855 /* Compute byte position if not specified. */
2856 if (bytepos < charpos)
2857 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2858 else
2859 IT_BYTEPOS (*it) = bytepos;
2861 it->start = it->current;
2863 /* Compute faces etc. */
2864 reseat (it, it->current.pos, 1);
2867 CHECK_IT (it);
2871 /* Initialize IT for the display of window W with window start POS. */
2873 void
2874 start_display (struct it *it, struct window *w, struct text_pos pos)
2876 struct glyph_row *row;
2877 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2879 row = w->desired_matrix->rows + first_vpos;
2880 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2881 it->first_vpos = first_vpos;
2883 /* Don't reseat to previous visible line start if current start
2884 position is in a string or image. */
2885 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2887 int start_at_line_beg_p;
2888 int first_y = it->current_y;
2890 /* If window start is not at a line start, skip forward to POS to
2891 get the correct continuation lines width. */
2892 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2893 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2894 if (!start_at_line_beg_p)
2896 int new_x;
2898 reseat_at_previous_visible_line_start (it);
2899 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2901 new_x = it->current_x + it->pixel_width;
2903 /* If lines are continued, this line may end in the middle
2904 of a multi-glyph character (e.g. a control character
2905 displayed as \003, or in the middle of an overlay
2906 string). In this case move_it_to above will not have
2907 taken us to the start of the continuation line but to the
2908 end of the continued line. */
2909 if (it->current_x > 0
2910 && it->line_wrap != TRUNCATE /* Lines are continued. */
2911 && (/* And glyph doesn't fit on the line. */
2912 new_x > it->last_visible_x
2913 /* Or it fits exactly and we're on a window
2914 system frame. */
2915 || (new_x == it->last_visible_x
2916 && FRAME_WINDOW_P (it->f))))
2918 if (it->current.dpvec_index >= 0
2919 || it->current.overlay_string_index >= 0)
2921 set_iterator_to_next (it, 1);
2922 move_it_in_display_line_to (it, -1, -1, 0);
2925 it->continuation_lines_width += it->current_x;
2928 /* We're starting a new display line, not affected by the
2929 height of the continued line, so clear the appropriate
2930 fields in the iterator structure. */
2931 it->max_ascent = it->max_descent = 0;
2932 it->max_phys_ascent = it->max_phys_descent = 0;
2934 it->current_y = first_y;
2935 it->vpos = 0;
2936 it->current_x = it->hpos = 0;
2942 /* Return 1 if POS is a position in ellipses displayed for invisible
2943 text. W is the window we display, for text property lookup. */
2945 static int
2946 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2948 Lisp_Object prop, window;
2949 int ellipses_p = 0;
2950 EMACS_INT charpos = CHARPOS (pos->pos);
2952 /* If POS specifies a position in a display vector, this might
2953 be for an ellipsis displayed for invisible text. We won't
2954 get the iterator set up for delivering that ellipsis unless
2955 we make sure that it gets aware of the invisible text. */
2956 if (pos->dpvec_index >= 0
2957 && pos->overlay_string_index < 0
2958 && CHARPOS (pos->string_pos) < 0
2959 && charpos > BEGV
2960 && (XSETWINDOW (window, w),
2961 prop = Fget_char_property (make_number (charpos),
2962 Qinvisible, window),
2963 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2965 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2966 window);
2967 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2970 return ellipses_p;
2974 /* Initialize IT for stepping through current_buffer in window W,
2975 starting at position POS that includes overlay string and display
2976 vector/ control character translation position information. Value
2977 is zero if there are overlay strings with newlines at POS. */
2979 static int
2980 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2982 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2983 int i, overlay_strings_with_newlines = 0;
2985 /* If POS specifies a position in a display vector, this might
2986 be for an ellipsis displayed for invisible text. We won't
2987 get the iterator set up for delivering that ellipsis unless
2988 we make sure that it gets aware of the invisible text. */
2989 if (in_ellipses_for_invisible_text_p (pos, w))
2991 --charpos;
2992 bytepos = 0;
2995 /* Keep in mind: the call to reseat in init_iterator skips invisible
2996 text, so we might end up at a position different from POS. This
2997 is only a problem when POS is a row start after a newline and an
2998 overlay starts there with an after-string, and the overlay has an
2999 invisible property. Since we don't skip invisible text in
3000 display_line and elsewhere immediately after consuming the
3001 newline before the row start, such a POS will not be in a string,
3002 but the call to init_iterator below will move us to the
3003 after-string. */
3004 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3006 /* This only scans the current chunk -- it should scan all chunks.
3007 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3008 to 16 in 22.1 to make this a lesser problem. */
3009 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3011 const char *s = SDATA (it->overlay_strings[i]);
3012 const char *e = s + SBYTES (it->overlay_strings[i]);
3014 while (s < e && *s != '\n')
3015 ++s;
3017 if (s < e)
3019 overlay_strings_with_newlines = 1;
3020 break;
3024 /* If position is within an overlay string, set up IT to the right
3025 overlay string. */
3026 if (pos->overlay_string_index >= 0)
3028 int relative_index;
3030 /* If the first overlay string happens to have a `display'
3031 property for an image, the iterator will be set up for that
3032 image, and we have to undo that setup first before we can
3033 correct the overlay string index. */
3034 if (it->method == GET_FROM_IMAGE)
3035 pop_it (it);
3037 /* We already have the first chunk of overlay strings in
3038 IT->overlay_strings. Load more until the one for
3039 pos->overlay_string_index is in IT->overlay_strings. */
3040 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3042 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3043 it->current.overlay_string_index = 0;
3044 while (n--)
3046 load_overlay_strings (it, 0);
3047 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3051 it->current.overlay_string_index = pos->overlay_string_index;
3052 relative_index = (it->current.overlay_string_index
3053 % OVERLAY_STRING_CHUNK_SIZE);
3054 it->string = it->overlay_strings[relative_index];
3055 xassert (STRINGP (it->string));
3056 it->current.string_pos = pos->string_pos;
3057 it->method = GET_FROM_STRING;
3060 if (CHARPOS (pos->string_pos) >= 0)
3062 /* Recorded position is not in an overlay string, but in another
3063 string. This can only be a string from a `display' property.
3064 IT should already be filled with that string. */
3065 it->current.string_pos = pos->string_pos;
3066 xassert (STRINGP (it->string));
3069 /* Restore position in display vector translations, control
3070 character translations or ellipses. */
3071 if (pos->dpvec_index >= 0)
3073 if (it->dpvec == NULL)
3074 get_next_display_element (it);
3075 xassert (it->dpvec && it->current.dpvec_index == 0);
3076 it->current.dpvec_index = pos->dpvec_index;
3079 CHECK_IT (it);
3080 return !overlay_strings_with_newlines;
3084 /* Initialize IT for stepping through current_buffer in window W
3085 starting at ROW->start. */
3087 static void
3088 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3090 init_from_display_pos (it, w, &row->start);
3091 it->start = row->start;
3092 it->continuation_lines_width = row->continuation_lines_width;
3093 CHECK_IT (it);
3097 /* Initialize IT for stepping through current_buffer in window W
3098 starting in the line following ROW, i.e. starting at ROW->end.
3099 Value is zero if there are overlay strings with newlines at ROW's
3100 end position. */
3102 static int
3103 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3105 int success = 0;
3107 if (init_from_display_pos (it, w, &row->end))
3109 if (row->continued_p)
3110 it->continuation_lines_width
3111 = row->continuation_lines_width + row->pixel_width;
3112 CHECK_IT (it);
3113 success = 1;
3116 return success;
3122 /***********************************************************************
3123 Text properties
3124 ***********************************************************************/
3126 /* Called when IT reaches IT->stop_charpos. Handle text property and
3127 overlay changes. Set IT->stop_charpos to the next position where
3128 to stop. */
3130 static void
3131 handle_stop (struct it *it)
3133 enum prop_handled handled;
3134 int handle_overlay_change_p;
3135 struct props *p;
3137 it->dpvec = NULL;
3138 it->current.dpvec_index = -1;
3139 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3140 it->ignore_overlay_strings_at_pos_p = 0;
3141 it->ellipsis_p = 0;
3143 /* Use face of preceding text for ellipsis (if invisible) */
3144 if (it->selective_display_ellipsis_p)
3145 it->saved_face_id = it->face_id;
3149 handled = HANDLED_NORMALLY;
3151 /* Call text property handlers. */
3152 for (p = it_props; p->handler; ++p)
3154 handled = p->handler (it);
3156 if (handled == HANDLED_RECOMPUTE_PROPS)
3157 break;
3158 else if (handled == HANDLED_RETURN)
3160 /* We still want to show before and after strings from
3161 overlays even if the actual buffer text is replaced. */
3162 if (!handle_overlay_change_p
3163 || it->sp > 1
3164 || !get_overlay_strings_1 (it, 0, 0))
3166 if (it->ellipsis_p)
3167 setup_for_ellipsis (it, 0);
3168 /* When handling a display spec, we might load an
3169 empty string. In that case, discard it here. We
3170 used to discard it in handle_single_display_spec,
3171 but that causes get_overlay_strings_1, above, to
3172 ignore overlay strings that we must check. */
3173 if (STRINGP (it->string) && !SCHARS (it->string))
3174 pop_it (it);
3175 return;
3177 else if (STRINGP (it->string) && !SCHARS (it->string))
3178 pop_it (it);
3179 else
3181 it->ignore_overlay_strings_at_pos_p = 1;
3182 it->string_from_display_prop_p = 0;
3183 handle_overlay_change_p = 0;
3185 handled = HANDLED_RECOMPUTE_PROPS;
3186 break;
3188 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3189 handle_overlay_change_p = 0;
3192 if (handled != HANDLED_RECOMPUTE_PROPS)
3194 /* Don't check for overlay strings below when set to deliver
3195 characters from a display vector. */
3196 if (it->method == GET_FROM_DISPLAY_VECTOR)
3197 handle_overlay_change_p = 0;
3199 /* Handle overlay changes.
3200 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3201 if it finds overlays. */
3202 if (handle_overlay_change_p)
3203 handled = handle_overlay_change (it);
3206 if (it->ellipsis_p)
3208 setup_for_ellipsis (it, 0);
3209 break;
3212 while (handled == HANDLED_RECOMPUTE_PROPS);
3214 /* Determine where to stop next. */
3215 if (handled == HANDLED_NORMALLY)
3216 compute_stop_pos (it);
3220 /* Compute IT->stop_charpos from text property and overlay change
3221 information for IT's current position. */
3223 static void
3224 compute_stop_pos (struct it *it)
3226 register INTERVAL iv, next_iv;
3227 Lisp_Object object, limit, position;
3228 EMACS_INT charpos, bytepos;
3230 /* If nowhere else, stop at the end. */
3231 it->stop_charpos = it->end_charpos;
3233 if (STRINGP (it->string))
3235 /* Strings are usually short, so don't limit the search for
3236 properties. */
3237 object = it->string;
3238 limit = Qnil;
3239 charpos = IT_STRING_CHARPOS (*it);
3240 bytepos = IT_STRING_BYTEPOS (*it);
3242 else
3244 EMACS_INT pos;
3246 /* If next overlay change is in front of the current stop pos
3247 (which is IT->end_charpos), stop there. Note: value of
3248 next_overlay_change is point-max if no overlay change
3249 follows. */
3250 charpos = IT_CHARPOS (*it);
3251 bytepos = IT_BYTEPOS (*it);
3252 pos = next_overlay_change (charpos);
3253 if (pos < it->stop_charpos)
3254 it->stop_charpos = pos;
3256 /* If showing the region, we have to stop at the region
3257 start or end because the face might change there. */
3258 if (it->region_beg_charpos > 0)
3260 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3261 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3262 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3263 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3266 /* Set up variables for computing the stop position from text
3267 property changes. */
3268 XSETBUFFER (object, current_buffer);
3269 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3272 /* Get the interval containing IT's position. Value is a null
3273 interval if there isn't such an interval. */
3274 position = make_number (charpos);
3275 iv = validate_interval_range (object, &position, &position, 0);
3276 if (!NULL_INTERVAL_P (iv))
3278 Lisp_Object values_here[LAST_PROP_IDX];
3279 struct props *p;
3281 /* Get properties here. */
3282 for (p = it_props; p->handler; ++p)
3283 values_here[p->idx] = textget (iv->plist, *p->name);
3285 /* Look for an interval following iv that has different
3286 properties. */
3287 for (next_iv = next_interval (iv);
3288 (!NULL_INTERVAL_P (next_iv)
3289 && (NILP (limit)
3290 || XFASTINT (limit) > next_iv->position));
3291 next_iv = next_interval (next_iv))
3293 for (p = it_props; p->handler; ++p)
3295 Lisp_Object new_value;
3297 new_value = textget (next_iv->plist, *p->name);
3298 if (!EQ (values_here[p->idx], new_value))
3299 break;
3302 if (p->handler)
3303 break;
3306 if (!NULL_INTERVAL_P (next_iv))
3308 if (INTEGERP (limit)
3309 && next_iv->position >= XFASTINT (limit))
3310 /* No text property change up to limit. */
3311 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3312 else
3313 /* Text properties change in next_iv. */
3314 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3318 if (it->cmp_it.id < 0)
3320 EMACS_INT stoppos = it->end_charpos;
3322 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3323 stoppos = -1;
3324 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3325 stoppos, it->string);
3328 xassert (STRINGP (it->string)
3329 || (it->stop_charpos >= BEGV
3330 && it->stop_charpos >= IT_CHARPOS (*it)));
3334 /* Return the position of the next overlay change after POS in
3335 current_buffer. Value is point-max if no overlay change
3336 follows. This is like `next-overlay-change' but doesn't use
3337 xmalloc. */
3339 static EMACS_INT
3340 next_overlay_change (EMACS_INT pos)
3342 int noverlays;
3343 EMACS_INT endpos;
3344 Lisp_Object *overlays;
3345 int i;
3347 /* Get all overlays at the given position. */
3348 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3350 /* If any of these overlays ends before endpos,
3351 use its ending point instead. */
3352 for (i = 0; i < noverlays; ++i)
3354 Lisp_Object oend;
3355 EMACS_INT oendpos;
3357 oend = OVERLAY_END (overlays[i]);
3358 oendpos = OVERLAY_POSITION (oend);
3359 endpos = min (endpos, oendpos);
3362 return endpos;
3367 /***********************************************************************
3368 Fontification
3369 ***********************************************************************/
3371 /* Handle changes in the `fontified' property of the current buffer by
3372 calling hook functions from Qfontification_functions to fontify
3373 regions of text. */
3375 static enum prop_handled
3376 handle_fontified_prop (struct it *it)
3378 Lisp_Object prop, pos;
3379 enum prop_handled handled = HANDLED_NORMALLY;
3381 if (!NILP (Vmemory_full))
3382 return handled;
3384 /* Get the value of the `fontified' property at IT's current buffer
3385 position. (The `fontified' property doesn't have a special
3386 meaning in strings.) If the value is nil, call functions from
3387 Qfontification_functions. */
3388 if (!STRINGP (it->string)
3389 && it->s == NULL
3390 && !NILP (Vfontification_functions)
3391 && !NILP (Vrun_hooks)
3392 && (pos = make_number (IT_CHARPOS (*it)),
3393 prop = Fget_char_property (pos, Qfontified, Qnil),
3394 /* Ignore the special cased nil value always present at EOB since
3395 no amount of fontifying will be able to change it. */
3396 NILP (prop) && IT_CHARPOS (*it) < Z))
3398 int count = SPECPDL_INDEX ();
3399 Lisp_Object val;
3401 val = Vfontification_functions;
3402 specbind (Qfontification_functions, Qnil);
3404 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3405 safe_call1 (val, pos);
3406 else
3408 Lisp_Object globals, fn;
3409 struct gcpro gcpro1, gcpro2;
3411 globals = Qnil;
3412 GCPRO2 (val, globals);
3414 for (; CONSP (val); val = XCDR (val))
3416 fn = XCAR (val);
3418 if (EQ (fn, Qt))
3420 /* A value of t indicates this hook has a local
3421 binding; it means to run the global binding too.
3422 In a global value, t should not occur. If it
3423 does, we must ignore it to avoid an endless
3424 loop. */
3425 for (globals = Fdefault_value (Qfontification_functions);
3426 CONSP (globals);
3427 globals = XCDR (globals))
3429 fn = XCAR (globals);
3430 if (!EQ (fn, Qt))
3431 safe_call1 (fn, pos);
3434 else
3435 safe_call1 (fn, pos);
3438 UNGCPRO;
3441 unbind_to (count, Qnil);
3443 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3444 something. This avoids an endless loop if they failed to
3445 fontify the text for which reason ever. */
3446 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3447 handled = HANDLED_RECOMPUTE_PROPS;
3450 return handled;
3455 /***********************************************************************
3456 Faces
3457 ***********************************************************************/
3459 /* Set up iterator IT from face properties at its current position.
3460 Called from handle_stop. */
3462 static enum prop_handled
3463 handle_face_prop (struct it *it)
3465 int new_face_id;
3466 EMACS_INT next_stop;
3468 if (!STRINGP (it->string))
3470 new_face_id
3471 = face_at_buffer_position (it->w,
3472 IT_CHARPOS (*it),
3473 it->region_beg_charpos,
3474 it->region_end_charpos,
3475 &next_stop,
3476 (IT_CHARPOS (*it)
3477 + TEXT_PROP_DISTANCE_LIMIT),
3478 0, it->base_face_id);
3480 /* Is this a start of a run of characters with box face?
3481 Caveat: this can be called for a freshly initialized
3482 iterator; face_id is -1 in this case. We know that the new
3483 face will not change until limit, i.e. if the new face has a
3484 box, all characters up to limit will have one. But, as
3485 usual, we don't know whether limit is really the end. */
3486 if (new_face_id != it->face_id)
3488 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3490 /* If new face has a box but old face has not, this is
3491 the start of a run of characters with box, i.e. it has
3492 a shadow on the left side. The value of face_id of the
3493 iterator will be -1 if this is the initial call that gets
3494 the face. In this case, we have to look in front of IT's
3495 position and see whether there is a face != new_face_id. */
3496 it->start_of_box_run_p
3497 = (new_face->box != FACE_NO_BOX
3498 && (it->face_id >= 0
3499 || IT_CHARPOS (*it) == BEG
3500 || new_face_id != face_before_it_pos (it)));
3501 it->face_box_p = new_face->box != FACE_NO_BOX;
3504 else
3506 int base_face_id;
3507 EMACS_INT bufpos;
3508 int i;
3509 Lisp_Object from_overlay
3510 = (it->current.overlay_string_index >= 0
3511 ? it->string_overlays[it->current.overlay_string_index]
3512 : Qnil);
3514 /* See if we got to this string directly or indirectly from
3515 an overlay property. That includes the before-string or
3516 after-string of an overlay, strings in display properties
3517 provided by an overlay, their text properties, etc.
3519 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3520 if (! NILP (from_overlay))
3521 for (i = it->sp - 1; i >= 0; i--)
3523 if (it->stack[i].current.overlay_string_index >= 0)
3524 from_overlay
3525 = it->string_overlays[it->stack[i].current.overlay_string_index];
3526 else if (! NILP (it->stack[i].from_overlay))
3527 from_overlay = it->stack[i].from_overlay;
3529 if (!NILP (from_overlay))
3530 break;
3533 if (! NILP (from_overlay))
3535 bufpos = IT_CHARPOS (*it);
3536 /* For a string from an overlay, the base face depends
3537 only on text properties and ignores overlays. */
3538 base_face_id
3539 = face_for_overlay_string (it->w,
3540 IT_CHARPOS (*it),
3541 it->region_beg_charpos,
3542 it->region_end_charpos,
3543 &next_stop,
3544 (IT_CHARPOS (*it)
3545 + TEXT_PROP_DISTANCE_LIMIT),
3547 from_overlay);
3549 else
3551 bufpos = 0;
3553 /* For strings from a `display' property, use the face at
3554 IT's current buffer position as the base face to merge
3555 with, so that overlay strings appear in the same face as
3556 surrounding text, unless they specify their own
3557 faces. */
3558 base_face_id = underlying_face_id (it);
3561 new_face_id = face_at_string_position (it->w,
3562 it->string,
3563 IT_STRING_CHARPOS (*it),
3564 bufpos,
3565 it->region_beg_charpos,
3566 it->region_end_charpos,
3567 &next_stop,
3568 base_face_id, 0);
3570 /* Is this a start of a run of characters with box? Caveat:
3571 this can be called for a freshly allocated iterator; face_id
3572 is -1 is this case. We know that the new face will not
3573 change until the next check pos, i.e. if the new face has a
3574 box, all characters up to that position will have a
3575 box. But, as usual, we don't know whether that position
3576 is really the end. */
3577 if (new_face_id != it->face_id)
3579 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3580 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3582 /* If new face has a box but old face hasn't, this is the
3583 start of a run of characters with box, i.e. it has a
3584 shadow on the left side. */
3585 it->start_of_box_run_p
3586 = new_face->box && (old_face == NULL || !old_face->box);
3587 it->face_box_p = new_face->box != FACE_NO_BOX;
3591 it->face_id = new_face_id;
3592 return HANDLED_NORMALLY;
3596 /* Return the ID of the face ``underlying'' IT's current position,
3597 which is in a string. If the iterator is associated with a
3598 buffer, return the face at IT's current buffer position.
3599 Otherwise, use the iterator's base_face_id. */
3601 static int
3602 underlying_face_id (struct it *it)
3604 int face_id = it->base_face_id, i;
3606 xassert (STRINGP (it->string));
3608 for (i = it->sp - 1; i >= 0; --i)
3609 if (NILP (it->stack[i].string))
3610 face_id = it->stack[i].face_id;
3612 return face_id;
3616 /* Compute the face one character before or after the current position
3617 of IT. BEFORE_P non-zero means get the face in front of IT's
3618 position. Value is the id of the face. */
3620 static int
3621 face_before_or_after_it_pos (struct it *it, int before_p)
3623 int face_id, limit;
3624 EMACS_INT next_check_charpos;
3625 struct text_pos pos;
3627 xassert (it->s == NULL);
3629 if (STRINGP (it->string))
3631 EMACS_INT bufpos;
3632 int base_face_id;
3634 /* No face change past the end of the string (for the case
3635 we are padding with spaces). No face change before the
3636 string start. */
3637 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3638 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3639 return it->face_id;
3641 /* Set pos to the position before or after IT's current position. */
3642 if (before_p)
3643 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3644 else
3645 /* For composition, we must check the character after the
3646 composition. */
3647 pos = (it->what == IT_COMPOSITION
3648 ? string_pos (IT_STRING_CHARPOS (*it)
3649 + it->cmp_it.nchars, it->string)
3650 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3652 if (it->current.overlay_string_index >= 0)
3653 bufpos = IT_CHARPOS (*it);
3654 else
3655 bufpos = 0;
3657 base_face_id = underlying_face_id (it);
3659 /* Get the face for ASCII, or unibyte. */
3660 face_id = face_at_string_position (it->w,
3661 it->string,
3662 CHARPOS (pos),
3663 bufpos,
3664 it->region_beg_charpos,
3665 it->region_end_charpos,
3666 &next_check_charpos,
3667 base_face_id, 0);
3669 /* Correct the face for charsets different from ASCII. Do it
3670 for the multibyte case only. The face returned above is
3671 suitable for unibyte text if IT->string is unibyte. */
3672 if (STRING_MULTIBYTE (it->string))
3674 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3675 int c, len;
3676 struct face *face = FACE_FROM_ID (it->f, face_id);
3678 c = string_char_and_length (p, &len);
3679 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3682 else
3684 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3685 || (IT_CHARPOS (*it) <= BEGV && before_p))
3686 return it->face_id;
3688 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3689 pos = it->current.pos;
3691 if (before_p)
3692 DEC_TEXT_POS (pos, it->multibyte_p);
3693 else
3695 if (it->what == IT_COMPOSITION)
3696 /* For composition, we must check the position after the
3697 composition. */
3698 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3699 else
3700 INC_TEXT_POS (pos, it->multibyte_p);
3703 /* Determine face for CHARSET_ASCII, or unibyte. */
3704 face_id = face_at_buffer_position (it->w,
3705 CHARPOS (pos),
3706 it->region_beg_charpos,
3707 it->region_end_charpos,
3708 &next_check_charpos,
3709 limit, 0, -1);
3711 /* Correct the face for charsets different from ASCII. Do it
3712 for the multibyte case only. The face returned above is
3713 suitable for unibyte text if current_buffer is unibyte. */
3714 if (it->multibyte_p)
3716 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3717 struct face *face = FACE_FROM_ID (it->f, face_id);
3718 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3722 return face_id;
3727 /***********************************************************************
3728 Invisible text
3729 ***********************************************************************/
3731 /* Set up iterator IT from invisible properties at its current
3732 position. Called from handle_stop. */
3734 static enum prop_handled
3735 handle_invisible_prop (struct it *it)
3737 enum prop_handled handled = HANDLED_NORMALLY;
3739 if (STRINGP (it->string))
3741 Lisp_Object prop, end_charpos, limit, charpos;
3743 /* Get the value of the invisible text property at the
3744 current position. Value will be nil if there is no such
3745 property. */
3746 charpos = make_number (IT_STRING_CHARPOS (*it));
3747 prop = Fget_text_property (charpos, Qinvisible, it->string);
3749 if (!NILP (prop)
3750 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3752 handled = HANDLED_RECOMPUTE_PROPS;
3754 /* Get the position at which the next change of the
3755 invisible text property can be found in IT->string.
3756 Value will be nil if the property value is the same for
3757 all the rest of IT->string. */
3758 XSETINT (limit, SCHARS (it->string));
3759 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3760 it->string, limit);
3762 /* Text at current position is invisible. The next
3763 change in the property is at position end_charpos.
3764 Move IT's current position to that position. */
3765 if (INTEGERP (end_charpos)
3766 && XFASTINT (end_charpos) < XFASTINT (limit))
3768 struct text_pos old;
3769 old = it->current.string_pos;
3770 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3771 compute_string_pos (&it->current.string_pos, old, it->string);
3773 else
3775 /* The rest of the string is invisible. If this is an
3776 overlay string, proceed with the next overlay string
3777 or whatever comes and return a character from there. */
3778 if (it->current.overlay_string_index >= 0)
3780 next_overlay_string (it);
3781 /* Don't check for overlay strings when we just
3782 finished processing them. */
3783 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3785 else
3787 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3788 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3793 else
3795 int invis_p;
3796 EMACS_INT newpos, next_stop, start_charpos, tem;
3797 Lisp_Object pos, prop, overlay;
3799 /* First of all, is there invisible text at this position? */
3800 tem = start_charpos = IT_CHARPOS (*it);
3801 pos = make_number (tem);
3802 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3803 &overlay);
3804 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3806 /* If we are on invisible text, skip over it. */
3807 if (invis_p && start_charpos < it->end_charpos)
3809 /* Record whether we have to display an ellipsis for the
3810 invisible text. */
3811 int display_ellipsis_p = invis_p == 2;
3813 handled = HANDLED_RECOMPUTE_PROPS;
3815 /* Loop skipping over invisible text. The loop is left at
3816 ZV or with IT on the first char being visible again. */
3819 /* Try to skip some invisible text. Return value is the
3820 position reached which can be equal to where we start
3821 if there is nothing invisible there. This skips both
3822 over invisible text properties and overlays with
3823 invisible property. */
3824 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3826 /* If we skipped nothing at all we weren't at invisible
3827 text in the first place. If everything to the end of
3828 the buffer was skipped, end the loop. */
3829 if (newpos == tem || newpos >= ZV)
3830 invis_p = 0;
3831 else
3833 /* We skipped some characters but not necessarily
3834 all there are. Check if we ended up on visible
3835 text. Fget_char_property returns the property of
3836 the char before the given position, i.e. if we
3837 get invis_p = 0, this means that the char at
3838 newpos is visible. */
3839 pos = make_number (newpos);
3840 prop = Fget_char_property (pos, Qinvisible, it->window);
3841 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3844 /* If we ended up on invisible text, proceed to
3845 skip starting with next_stop. */
3846 if (invis_p)
3847 tem = next_stop;
3849 /* If there are adjacent invisible texts, don't lose the
3850 second one's ellipsis. */
3851 if (invis_p == 2)
3852 display_ellipsis_p = 1;
3854 while (invis_p);
3856 /* The position newpos is now either ZV or on visible text. */
3857 if (it->bidi_p && newpos < ZV)
3859 /* With bidi iteration, the region of invisible text
3860 could start and/or end in the middle of a non-base
3861 embedding level. Therefore, we need to skip
3862 invisible text using the bidi iterator, starting at
3863 IT's current position, until we find ourselves
3864 outside the invisible text. Skipping invisible text
3865 _after_ bidi iteration avoids affecting the visual
3866 order of the displayed text when invisible properties
3867 are added or removed. */
3868 if (it->bidi_it.first_elt)
3870 /* If we were `reseat'ed to a new paragraph,
3871 determine the paragraph base direction. We need
3872 to do it now because next_element_from_buffer may
3873 not have a chance to do it, if we are going to
3874 skip any text at the beginning, which resets the
3875 FIRST_ELT flag. */
3876 bidi_paragraph_init (it->paragraph_embedding,
3877 &it->bidi_it, 1);
3881 bidi_move_to_visually_next (&it->bidi_it);
3883 while (it->stop_charpos <= it->bidi_it.charpos
3884 && it->bidi_it.charpos < newpos);
3885 IT_CHARPOS (*it) = it->bidi_it.charpos;
3886 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3887 /* If we overstepped NEWPOS, record its position in the
3888 iterator, so that we skip invisible text if later the
3889 bidi iteration lands us in the invisible region
3890 again. */
3891 if (IT_CHARPOS (*it) >= newpos)
3892 it->prev_stop = newpos;
3894 else
3896 IT_CHARPOS (*it) = newpos;
3897 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3900 /* If there are before-strings at the start of invisible
3901 text, and the text is invisible because of a text
3902 property, arrange to show before-strings because 20.x did
3903 it that way. (If the text is invisible because of an
3904 overlay property instead of a text property, this is
3905 already handled in the overlay code.) */
3906 if (NILP (overlay)
3907 && get_overlay_strings (it, it->stop_charpos))
3909 handled = HANDLED_RECOMPUTE_PROPS;
3910 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3912 else if (display_ellipsis_p)
3914 /* Make sure that the glyphs of the ellipsis will get
3915 correct `charpos' values. If we would not update
3916 it->position here, the glyphs would belong to the
3917 last visible character _before_ the invisible
3918 text, which confuses `set_cursor_from_row'.
3920 We use the last invisible position instead of the
3921 first because this way the cursor is always drawn on
3922 the first "." of the ellipsis, whenever PT is inside
3923 the invisible text. Otherwise the cursor would be
3924 placed _after_ the ellipsis when the point is after the
3925 first invisible character. */
3926 if (!STRINGP (it->object))
3928 it->position.charpos = newpos - 1;
3929 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3931 it->ellipsis_p = 1;
3932 /* Let the ellipsis display before
3933 considering any properties of the following char.
3934 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3935 handled = HANDLED_RETURN;
3940 return handled;
3944 /* Make iterator IT return `...' next.
3945 Replaces LEN characters from buffer. */
3947 static void
3948 setup_for_ellipsis (struct it *it, int len)
3950 /* Use the display table definition for `...'. Invalid glyphs
3951 will be handled by the method returning elements from dpvec. */
3952 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3954 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3955 it->dpvec = v->contents;
3956 it->dpend = v->contents + v->size;
3958 else
3960 /* Default `...'. */
3961 it->dpvec = default_invis_vector;
3962 it->dpend = default_invis_vector + 3;
3965 it->dpvec_char_len = len;
3966 it->current.dpvec_index = 0;
3967 it->dpvec_face_id = -1;
3969 /* Remember the current face id in case glyphs specify faces.
3970 IT's face is restored in set_iterator_to_next.
3971 saved_face_id was set to preceding char's face in handle_stop. */
3972 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3973 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3975 it->method = GET_FROM_DISPLAY_VECTOR;
3976 it->ellipsis_p = 1;
3981 /***********************************************************************
3982 'display' property
3983 ***********************************************************************/
3985 /* Set up iterator IT from `display' property at its current position.
3986 Called from handle_stop.
3987 We return HANDLED_RETURN if some part of the display property
3988 overrides the display of the buffer text itself.
3989 Otherwise we return HANDLED_NORMALLY. */
3991 static enum prop_handled
3992 handle_display_prop (struct it *it)
3994 Lisp_Object prop, object, overlay;
3995 struct text_pos *position;
3996 /* Nonzero if some property replaces the display of the text itself. */
3997 int display_replaced_p = 0;
3999 if (STRINGP (it->string))
4001 object = it->string;
4002 position = &it->current.string_pos;
4004 else
4006 XSETWINDOW (object, it->w);
4007 position = &it->current.pos;
4010 /* Reset those iterator values set from display property values. */
4011 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4012 it->space_width = Qnil;
4013 it->font_height = Qnil;
4014 it->voffset = 0;
4016 /* We don't support recursive `display' properties, i.e. string
4017 values that have a string `display' property, that have a string
4018 `display' property etc. */
4019 if (!it->string_from_display_prop_p)
4020 it->area = TEXT_AREA;
4022 prop = get_char_property_and_overlay (make_number (position->charpos),
4023 Qdisplay, object, &overlay);
4024 if (NILP (prop))
4025 return HANDLED_NORMALLY;
4026 /* Now OVERLAY is the overlay that gave us this property, or nil
4027 if it was a text property. */
4029 if (!STRINGP (it->string))
4030 object = it->w->buffer;
4032 if (CONSP (prop)
4033 /* Simple properties. */
4034 && !EQ (XCAR (prop), Qimage)
4035 && !EQ (XCAR (prop), Qspace)
4036 && !EQ (XCAR (prop), Qwhen)
4037 && !EQ (XCAR (prop), Qslice)
4038 && !EQ (XCAR (prop), Qspace_width)
4039 && !EQ (XCAR (prop), Qheight)
4040 && !EQ (XCAR (prop), Qraise)
4041 /* Marginal area specifications. */
4042 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4043 && !EQ (XCAR (prop), Qleft_fringe)
4044 && !EQ (XCAR (prop), Qright_fringe)
4045 && !NILP (XCAR (prop)))
4047 for (; CONSP (prop); prop = XCDR (prop))
4049 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4050 position, display_replaced_p))
4052 display_replaced_p = 1;
4053 /* If some text in a string is replaced, `position' no
4054 longer points to the position of `object'. */
4055 if (STRINGP (object))
4056 break;
4060 else if (VECTORP (prop))
4062 int i;
4063 for (i = 0; i < ASIZE (prop); ++i)
4064 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4065 position, display_replaced_p))
4067 display_replaced_p = 1;
4068 /* If some text in a string is replaced, `position' no
4069 longer points to the position of `object'. */
4070 if (STRINGP (object))
4071 break;
4074 else
4076 if (handle_single_display_spec (it, prop, object, overlay,
4077 position, 0))
4078 display_replaced_p = 1;
4081 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4085 /* Value is the position of the end of the `display' property starting
4086 at START_POS in OBJECT. */
4088 static struct text_pos
4089 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4091 Lisp_Object end;
4092 struct text_pos end_pos;
4094 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4095 Qdisplay, object, Qnil);
4096 CHARPOS (end_pos) = XFASTINT (end);
4097 if (STRINGP (object))
4098 compute_string_pos (&end_pos, start_pos, it->string);
4099 else
4100 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4102 return end_pos;
4106 /* Set up IT from a single `display' specification PROP. OBJECT
4107 is the object in which the `display' property was found. *POSITION
4108 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4109 means that we previously saw a display specification which already
4110 replaced text display with something else, for example an image;
4111 we ignore such properties after the first one has been processed.
4113 OVERLAY is the overlay this `display' property came from,
4114 or nil if it was a text property.
4116 If PROP is a `space' or `image' specification, and in some other
4117 cases too, set *POSITION to the position where the `display'
4118 property ends.
4120 Value is non-zero if something was found which replaces the display
4121 of buffer or string text. */
4123 static int
4124 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4125 Lisp_Object overlay, struct text_pos *position,
4126 int display_replaced_before_p)
4128 Lisp_Object form;
4129 Lisp_Object location, value;
4130 struct text_pos start_pos, save_pos;
4131 int valid_p;
4133 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4134 If the result is non-nil, use VALUE instead of SPEC. */
4135 form = Qt;
4136 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4138 spec = XCDR (spec);
4139 if (!CONSP (spec))
4140 return 0;
4141 form = XCAR (spec);
4142 spec = XCDR (spec);
4145 if (!NILP (form) && !EQ (form, Qt))
4147 int count = SPECPDL_INDEX ();
4148 struct gcpro gcpro1;
4150 /* Bind `object' to the object having the `display' property, a
4151 buffer or string. Bind `position' to the position in the
4152 object where the property was found, and `buffer-position'
4153 to the current position in the buffer. */
4154 specbind (Qobject, object);
4155 specbind (Qposition, make_number (CHARPOS (*position)));
4156 specbind (Qbuffer_position,
4157 make_number (STRINGP (object)
4158 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4159 GCPRO1 (form);
4160 form = safe_eval (form);
4161 UNGCPRO;
4162 unbind_to (count, Qnil);
4165 if (NILP (form))
4166 return 0;
4168 /* Handle `(height HEIGHT)' specifications. */
4169 if (CONSP (spec)
4170 && EQ (XCAR (spec), Qheight)
4171 && CONSP (XCDR (spec)))
4173 if (!FRAME_WINDOW_P (it->f))
4174 return 0;
4176 it->font_height = XCAR (XCDR (spec));
4177 if (!NILP (it->font_height))
4179 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4180 int new_height = -1;
4182 if (CONSP (it->font_height)
4183 && (EQ (XCAR (it->font_height), Qplus)
4184 || EQ (XCAR (it->font_height), Qminus))
4185 && CONSP (XCDR (it->font_height))
4186 && INTEGERP (XCAR (XCDR (it->font_height))))
4188 /* `(+ N)' or `(- N)' where N is an integer. */
4189 int steps = XINT (XCAR (XCDR (it->font_height)));
4190 if (EQ (XCAR (it->font_height), Qplus))
4191 steps = - steps;
4192 it->face_id = smaller_face (it->f, it->face_id, steps);
4194 else if (FUNCTIONP (it->font_height))
4196 /* Call function with current height as argument.
4197 Value is the new height. */
4198 Lisp_Object height;
4199 height = safe_call1 (it->font_height,
4200 face->lface[LFACE_HEIGHT_INDEX]);
4201 if (NUMBERP (height))
4202 new_height = XFLOATINT (height);
4204 else if (NUMBERP (it->font_height))
4206 /* Value is a multiple of the canonical char height. */
4207 struct face *face;
4209 face = FACE_FROM_ID (it->f,
4210 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4211 new_height = (XFLOATINT (it->font_height)
4212 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4214 else
4216 /* Evaluate IT->font_height with `height' bound to the
4217 current specified height to get the new height. */
4218 int count = SPECPDL_INDEX ();
4220 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4221 value = safe_eval (it->font_height);
4222 unbind_to (count, Qnil);
4224 if (NUMBERP (value))
4225 new_height = XFLOATINT (value);
4228 if (new_height > 0)
4229 it->face_id = face_with_height (it->f, it->face_id, new_height);
4232 return 0;
4235 /* Handle `(space-width WIDTH)'. */
4236 if (CONSP (spec)
4237 && EQ (XCAR (spec), Qspace_width)
4238 && CONSP (XCDR (spec)))
4240 if (!FRAME_WINDOW_P (it->f))
4241 return 0;
4243 value = XCAR (XCDR (spec));
4244 if (NUMBERP (value) && XFLOATINT (value) > 0)
4245 it->space_width = value;
4247 return 0;
4250 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4251 if (CONSP (spec)
4252 && EQ (XCAR (spec), Qslice))
4254 Lisp_Object tem;
4256 if (!FRAME_WINDOW_P (it->f))
4257 return 0;
4259 if (tem = XCDR (spec), CONSP (tem))
4261 it->slice.x = XCAR (tem);
4262 if (tem = XCDR (tem), CONSP (tem))
4264 it->slice.y = XCAR (tem);
4265 if (tem = XCDR (tem), CONSP (tem))
4267 it->slice.width = XCAR (tem);
4268 if (tem = XCDR (tem), CONSP (tem))
4269 it->slice.height = XCAR (tem);
4274 return 0;
4277 /* Handle `(raise FACTOR)'. */
4278 if (CONSP (spec)
4279 && EQ (XCAR (spec), Qraise)
4280 && CONSP (XCDR (spec)))
4282 if (!FRAME_WINDOW_P (it->f))
4283 return 0;
4285 #ifdef HAVE_WINDOW_SYSTEM
4286 value = XCAR (XCDR (spec));
4287 if (NUMBERP (value))
4289 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4290 it->voffset = - (XFLOATINT (value)
4291 * (FONT_HEIGHT (face->font)));
4293 #endif /* HAVE_WINDOW_SYSTEM */
4295 return 0;
4298 /* Don't handle the other kinds of display specifications
4299 inside a string that we got from a `display' property. */
4300 if (it->string_from_display_prop_p)
4301 return 0;
4303 /* Characters having this form of property are not displayed, so
4304 we have to find the end of the property. */
4305 start_pos = *position;
4306 *position = display_prop_end (it, object, start_pos);
4307 value = Qnil;
4309 /* Stop the scan at that end position--we assume that all
4310 text properties change there. */
4311 it->stop_charpos = position->charpos;
4313 /* Handle `(left-fringe BITMAP [FACE])'
4314 and `(right-fringe BITMAP [FACE])'. */
4315 if (CONSP (spec)
4316 && (EQ (XCAR (spec), Qleft_fringe)
4317 || EQ (XCAR (spec), Qright_fringe))
4318 && CONSP (XCDR (spec)))
4320 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4321 int fringe_bitmap;
4323 if (!FRAME_WINDOW_P (it->f))
4324 /* If we return here, POSITION has been advanced
4325 across the text with this property. */
4326 return 0;
4328 #ifdef HAVE_WINDOW_SYSTEM
4329 value = XCAR (XCDR (spec));
4330 if (!SYMBOLP (value)
4331 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4332 /* If we return here, POSITION has been advanced
4333 across the text with this property. */
4334 return 0;
4336 if (CONSP (XCDR (XCDR (spec))))
4338 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4339 int face_id2 = lookup_derived_face (it->f, face_name,
4340 FRINGE_FACE_ID, 0);
4341 if (face_id2 >= 0)
4342 face_id = face_id2;
4345 /* Save current settings of IT so that we can restore them
4346 when we are finished with the glyph property value. */
4348 save_pos = it->position;
4349 it->position = *position;
4350 push_it (it);
4351 it->position = save_pos;
4353 it->area = TEXT_AREA;
4354 it->what = IT_IMAGE;
4355 it->image_id = -1; /* no image */
4356 it->position = start_pos;
4357 it->object = NILP (object) ? it->w->buffer : object;
4358 it->method = GET_FROM_IMAGE;
4359 it->from_overlay = Qnil;
4360 it->face_id = face_id;
4362 /* Say that we haven't consumed the characters with
4363 `display' property yet. The call to pop_it in
4364 set_iterator_to_next will clean this up. */
4365 *position = start_pos;
4367 if (EQ (XCAR (spec), Qleft_fringe))
4369 it->left_user_fringe_bitmap = fringe_bitmap;
4370 it->left_user_fringe_face_id = face_id;
4372 else
4374 it->right_user_fringe_bitmap = fringe_bitmap;
4375 it->right_user_fringe_face_id = face_id;
4377 #endif /* HAVE_WINDOW_SYSTEM */
4378 return 1;
4381 /* Prepare to handle `((margin left-margin) ...)',
4382 `((margin right-margin) ...)' and `((margin nil) ...)'
4383 prefixes for display specifications. */
4384 location = Qunbound;
4385 if (CONSP (spec) && CONSP (XCAR (spec)))
4387 Lisp_Object tem;
4389 value = XCDR (spec);
4390 if (CONSP (value))
4391 value = XCAR (value);
4393 tem = XCAR (spec);
4394 if (EQ (XCAR (tem), Qmargin)
4395 && (tem = XCDR (tem),
4396 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4397 (NILP (tem)
4398 || EQ (tem, Qleft_margin)
4399 || EQ (tem, Qright_margin))))
4400 location = tem;
4403 if (EQ (location, Qunbound))
4405 location = Qnil;
4406 value = spec;
4409 /* After this point, VALUE is the property after any
4410 margin prefix has been stripped. It must be a string,
4411 an image specification, or `(space ...)'.
4413 LOCATION specifies where to display: `left-margin',
4414 `right-margin' or nil. */
4416 valid_p = (STRINGP (value)
4417 #ifdef HAVE_WINDOW_SYSTEM
4418 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4419 #endif /* not HAVE_WINDOW_SYSTEM */
4420 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4422 if (valid_p && !display_replaced_before_p)
4424 /* Save current settings of IT so that we can restore them
4425 when we are finished with the glyph property value. */
4426 save_pos = it->position;
4427 it->position = *position;
4428 push_it (it);
4429 it->position = save_pos;
4430 it->from_overlay = overlay;
4432 if (NILP (location))
4433 it->area = TEXT_AREA;
4434 else if (EQ (location, Qleft_margin))
4435 it->area = LEFT_MARGIN_AREA;
4436 else
4437 it->area = RIGHT_MARGIN_AREA;
4439 if (STRINGP (value))
4441 it->string = value;
4442 it->multibyte_p = STRING_MULTIBYTE (it->string);
4443 it->current.overlay_string_index = -1;
4444 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4445 it->end_charpos = it->string_nchars = SCHARS (it->string);
4446 it->method = GET_FROM_STRING;
4447 it->stop_charpos = 0;
4448 it->string_from_display_prop_p = 1;
4449 /* Say that we haven't consumed the characters with
4450 `display' property yet. The call to pop_it in
4451 set_iterator_to_next will clean this up. */
4452 if (BUFFERP (object))
4453 *position = start_pos;
4455 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4457 it->method = GET_FROM_STRETCH;
4458 it->object = value;
4459 *position = it->position = start_pos;
4461 #ifdef HAVE_WINDOW_SYSTEM
4462 else
4464 it->what = IT_IMAGE;
4465 it->image_id = lookup_image (it->f, value);
4466 it->position = start_pos;
4467 it->object = NILP (object) ? it->w->buffer : object;
4468 it->method = GET_FROM_IMAGE;
4470 /* Say that we haven't consumed the characters with
4471 `display' property yet. The call to pop_it in
4472 set_iterator_to_next will clean this up. */
4473 *position = start_pos;
4475 #endif /* HAVE_WINDOW_SYSTEM */
4477 return 1;
4480 /* Invalid property or property not supported. Restore
4481 POSITION to what it was before. */
4482 *position = start_pos;
4483 return 0;
4487 /* Check if SPEC is a display sub-property value whose text should be
4488 treated as intangible. */
4490 static int
4491 single_display_spec_intangible_p (Lisp_Object prop)
4493 /* Skip over `when FORM'. */
4494 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4496 prop = XCDR (prop);
4497 if (!CONSP (prop))
4498 return 0;
4499 prop = XCDR (prop);
4502 if (STRINGP (prop))
4503 return 1;
4505 if (!CONSP (prop))
4506 return 0;
4508 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4509 we don't need to treat text as intangible. */
4510 if (EQ (XCAR (prop), Qmargin))
4512 prop = XCDR (prop);
4513 if (!CONSP (prop))
4514 return 0;
4516 prop = XCDR (prop);
4517 if (!CONSP (prop)
4518 || EQ (XCAR (prop), Qleft_margin)
4519 || EQ (XCAR (prop), Qright_margin))
4520 return 0;
4523 return (CONSP (prop)
4524 && (EQ (XCAR (prop), Qimage)
4525 || EQ (XCAR (prop), Qspace)));
4529 /* Check if PROP is a display property value whose text should be
4530 treated as intangible. */
4533 display_prop_intangible_p (Lisp_Object prop)
4535 if (CONSP (prop)
4536 && CONSP (XCAR (prop))
4537 && !EQ (Qmargin, XCAR (XCAR (prop))))
4539 /* A list of sub-properties. */
4540 while (CONSP (prop))
4542 if (single_display_spec_intangible_p (XCAR (prop)))
4543 return 1;
4544 prop = XCDR (prop);
4547 else if (VECTORP (prop))
4549 /* A vector of sub-properties. */
4550 int i;
4551 for (i = 0; i < ASIZE (prop); ++i)
4552 if (single_display_spec_intangible_p (AREF (prop, i)))
4553 return 1;
4555 else
4556 return single_display_spec_intangible_p (prop);
4558 return 0;
4562 /* Return 1 if PROP is a display sub-property value containing STRING. */
4564 static int
4565 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4567 if (EQ (string, prop))
4568 return 1;
4570 /* Skip over `when FORM'. */
4571 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4573 prop = XCDR (prop);
4574 if (!CONSP (prop))
4575 return 0;
4576 prop = XCDR (prop);
4579 if (CONSP (prop))
4580 /* Skip over `margin LOCATION'. */
4581 if (EQ (XCAR (prop), Qmargin))
4583 prop = XCDR (prop);
4584 if (!CONSP (prop))
4585 return 0;
4587 prop = XCDR (prop);
4588 if (!CONSP (prop))
4589 return 0;
4592 return CONSP (prop) && EQ (XCAR (prop), string);
4596 /* Return 1 if STRING appears in the `display' property PROP. */
4598 static int
4599 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4601 if (CONSP (prop)
4602 && CONSP (XCAR (prop))
4603 && !EQ (Qmargin, XCAR (XCAR (prop))))
4605 /* A list of sub-properties. */
4606 while (CONSP (prop))
4608 if (single_display_spec_string_p (XCAR (prop), string))
4609 return 1;
4610 prop = XCDR (prop);
4613 else if (VECTORP (prop))
4615 /* A vector of sub-properties. */
4616 int i;
4617 for (i = 0; i < ASIZE (prop); ++i)
4618 if (single_display_spec_string_p (AREF (prop, i), string))
4619 return 1;
4621 else
4622 return single_display_spec_string_p (prop, string);
4624 return 0;
4627 /* Look for STRING in overlays and text properties in W's buffer,
4628 between character positions FROM and TO (excluding TO).
4629 BACK_P non-zero means look back (in this case, TO is supposed to be
4630 less than FROM).
4631 Value is the first character position where STRING was found, or
4632 zero if it wasn't found before hitting TO.
4634 W's buffer must be current.
4636 This function may only use code that doesn't eval because it is
4637 called asynchronously from note_mouse_highlight. */
4639 static EMACS_INT
4640 string_buffer_position_lim (struct window *w, Lisp_Object string,
4641 EMACS_INT from, EMACS_INT to, int back_p)
4643 Lisp_Object limit, prop, pos;
4644 int found = 0;
4646 pos = make_number (from);
4648 if (!back_p) /* looking forward */
4650 limit = make_number (min (to, ZV));
4651 while (!found && !EQ (pos, limit))
4653 prop = Fget_char_property (pos, Qdisplay, Qnil);
4654 if (!NILP (prop) && display_prop_string_p (prop, string))
4655 found = 1;
4656 else
4657 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4658 limit);
4661 else /* looking back */
4663 limit = make_number (max (to, BEGV));
4664 while (!found && !EQ (pos, limit))
4666 prop = Fget_char_property (pos, Qdisplay, Qnil);
4667 if (!NILP (prop) && display_prop_string_p (prop, string))
4668 found = 1;
4669 else
4670 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4671 limit);
4675 return found ? XINT (pos) : 0;
4678 /* Determine which buffer position in W's buffer STRING comes from.
4679 AROUND_CHARPOS is an approximate position where it could come from.
4680 Value is the buffer position or 0 if it couldn't be determined.
4682 W's buffer must be current.
4684 This function is necessary because we don't record buffer positions
4685 in glyphs generated from strings (to keep struct glyph small).
4686 This function may only use code that doesn't eval because it is
4687 called asynchronously from note_mouse_highlight. */
4689 EMACS_INT
4690 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4692 const int MAX_DISTANCE = 1000;
4693 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4694 around_charpos + MAX_DISTANCE,
4697 if (!found)
4698 found = string_buffer_position_lim (w, string, around_charpos,
4699 around_charpos - MAX_DISTANCE, 1);
4700 return found;
4705 /***********************************************************************
4706 `composition' property
4707 ***********************************************************************/
4709 /* Set up iterator IT from `composition' property at its current
4710 position. Called from handle_stop. */
4712 static enum prop_handled
4713 handle_composition_prop (struct it *it)
4715 Lisp_Object prop, string;
4716 EMACS_INT pos, pos_byte, start, end;
4718 if (STRINGP (it->string))
4720 unsigned char *s;
4722 pos = IT_STRING_CHARPOS (*it);
4723 pos_byte = IT_STRING_BYTEPOS (*it);
4724 string = it->string;
4725 s = SDATA (string) + pos_byte;
4726 it->c = STRING_CHAR (s);
4728 else
4730 pos = IT_CHARPOS (*it);
4731 pos_byte = IT_BYTEPOS (*it);
4732 string = Qnil;
4733 it->c = FETCH_CHAR (pos_byte);
4736 /* If there's a valid composition and point is not inside of the
4737 composition (in the case that the composition is from the current
4738 buffer), draw a glyph composed from the composition components. */
4739 if (find_composition (pos, -1, &start, &end, &prop, string)
4740 && COMPOSITION_VALID_P (start, end, prop)
4741 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4743 if (start != pos)
4745 if (STRINGP (it->string))
4746 pos_byte = string_char_to_byte (it->string, start);
4747 else
4748 pos_byte = CHAR_TO_BYTE (start);
4750 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4751 prop, string);
4753 if (it->cmp_it.id >= 0)
4755 it->cmp_it.ch = -1;
4756 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4757 it->cmp_it.nglyphs = -1;
4761 return HANDLED_NORMALLY;
4766 /***********************************************************************
4767 Overlay strings
4768 ***********************************************************************/
4770 /* The following structure is used to record overlay strings for
4771 later sorting in load_overlay_strings. */
4773 struct overlay_entry
4775 Lisp_Object overlay;
4776 Lisp_Object string;
4777 int priority;
4778 int after_string_p;
4782 /* Set up iterator IT from overlay strings at its current position.
4783 Called from handle_stop. */
4785 static enum prop_handled
4786 handle_overlay_change (struct it *it)
4788 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4789 return HANDLED_RECOMPUTE_PROPS;
4790 else
4791 return HANDLED_NORMALLY;
4795 /* Set up the next overlay string for delivery by IT, if there is an
4796 overlay string to deliver. Called by set_iterator_to_next when the
4797 end of the current overlay string is reached. If there are more
4798 overlay strings to display, IT->string and
4799 IT->current.overlay_string_index are set appropriately here.
4800 Otherwise IT->string is set to nil. */
4802 static void
4803 next_overlay_string (struct it *it)
4805 ++it->current.overlay_string_index;
4806 if (it->current.overlay_string_index == it->n_overlay_strings)
4808 /* No more overlay strings. Restore IT's settings to what
4809 they were before overlay strings were processed, and
4810 continue to deliver from current_buffer. */
4812 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4813 pop_it (it);
4814 xassert (it->sp > 0
4815 || (NILP (it->string)
4816 && it->method == GET_FROM_BUFFER
4817 && it->stop_charpos >= BEGV
4818 && it->stop_charpos <= it->end_charpos));
4819 it->current.overlay_string_index = -1;
4820 it->n_overlay_strings = 0;
4821 it->overlay_strings_charpos = -1;
4823 /* If we're at the end of the buffer, record that we have
4824 processed the overlay strings there already, so that
4825 next_element_from_buffer doesn't try it again. */
4826 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4827 it->overlay_strings_at_end_processed_p = 1;
4829 else
4831 /* There are more overlay strings to process. If
4832 IT->current.overlay_string_index has advanced to a position
4833 where we must load IT->overlay_strings with more strings, do
4834 it. We must load at the IT->overlay_strings_charpos where
4835 IT->n_overlay_strings was originally computed; when invisible
4836 text is present, this might not be IT_CHARPOS (Bug#7016). */
4837 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4839 if (it->current.overlay_string_index && i == 0)
4840 load_overlay_strings (it, it->overlay_strings_charpos);
4842 /* Initialize IT to deliver display elements from the overlay
4843 string. */
4844 it->string = it->overlay_strings[i];
4845 it->multibyte_p = STRING_MULTIBYTE (it->string);
4846 SET_TEXT_POS (it->current.string_pos, 0, 0);
4847 it->method = GET_FROM_STRING;
4848 it->stop_charpos = 0;
4849 if (it->cmp_it.stop_pos >= 0)
4850 it->cmp_it.stop_pos = 0;
4853 CHECK_IT (it);
4857 /* Compare two overlay_entry structures E1 and E2. Used as a
4858 comparison function for qsort in load_overlay_strings. Overlay
4859 strings for the same position are sorted so that
4861 1. All after-strings come in front of before-strings, except
4862 when they come from the same overlay.
4864 2. Within after-strings, strings are sorted so that overlay strings
4865 from overlays with higher priorities come first.
4867 2. Within before-strings, strings are sorted so that overlay
4868 strings from overlays with higher priorities come last.
4870 Value is analogous to strcmp. */
4873 static int
4874 compare_overlay_entries (const void *e1, const void *e2)
4876 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4877 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4878 int result;
4880 if (entry1->after_string_p != entry2->after_string_p)
4882 /* Let after-strings appear in front of before-strings if
4883 they come from different overlays. */
4884 if (EQ (entry1->overlay, entry2->overlay))
4885 result = entry1->after_string_p ? 1 : -1;
4886 else
4887 result = entry1->after_string_p ? -1 : 1;
4889 else if (entry1->after_string_p)
4890 /* After-strings sorted in order of decreasing priority. */
4891 result = entry2->priority - entry1->priority;
4892 else
4893 /* Before-strings sorted in order of increasing priority. */
4894 result = entry1->priority - entry2->priority;
4896 return result;
4900 /* Load the vector IT->overlay_strings with overlay strings from IT's
4901 current buffer position, or from CHARPOS if that is > 0. Set
4902 IT->n_overlays to the total number of overlay strings found.
4904 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4905 a time. On entry into load_overlay_strings,
4906 IT->current.overlay_string_index gives the number of overlay
4907 strings that have already been loaded by previous calls to this
4908 function.
4910 IT->add_overlay_start contains an additional overlay start
4911 position to consider for taking overlay strings from, if non-zero.
4912 This position comes into play when the overlay has an `invisible'
4913 property, and both before and after-strings. When we've skipped to
4914 the end of the overlay, because of its `invisible' property, we
4915 nevertheless want its before-string to appear.
4916 IT->add_overlay_start will contain the overlay start position
4917 in this case.
4919 Overlay strings are sorted so that after-string strings come in
4920 front of before-string strings. Within before and after-strings,
4921 strings are sorted by overlay priority. See also function
4922 compare_overlay_entries. */
4924 static void
4925 load_overlay_strings (struct it *it, EMACS_INT charpos)
4927 Lisp_Object overlay, window, str, invisible;
4928 struct Lisp_Overlay *ov;
4929 EMACS_INT start, end;
4930 int size = 20;
4931 int n = 0, i, j, invis_p;
4932 struct overlay_entry *entries
4933 = (struct overlay_entry *) alloca (size * sizeof *entries);
4935 if (charpos <= 0)
4936 charpos = IT_CHARPOS (*it);
4938 /* Append the overlay string STRING of overlay OVERLAY to vector
4939 `entries' which has size `size' and currently contains `n'
4940 elements. AFTER_P non-zero means STRING is an after-string of
4941 OVERLAY. */
4942 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4943 do \
4945 Lisp_Object priority; \
4947 if (n == size) \
4949 int new_size = 2 * size; \
4950 struct overlay_entry *old = entries; \
4951 entries = \
4952 (struct overlay_entry *) alloca (new_size \
4953 * sizeof *entries); \
4954 memcpy (entries, old, size * sizeof *entries); \
4955 size = new_size; \
4958 entries[n].string = (STRING); \
4959 entries[n].overlay = (OVERLAY); \
4960 priority = Foverlay_get ((OVERLAY), Qpriority); \
4961 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4962 entries[n].after_string_p = (AFTER_P); \
4963 ++n; \
4965 while (0)
4967 /* Process overlay before the overlay center. */
4968 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4970 XSETMISC (overlay, ov);
4971 xassert (OVERLAYP (overlay));
4972 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4973 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4975 if (end < charpos)
4976 break;
4978 /* Skip this overlay if it doesn't start or end at IT's current
4979 position. */
4980 if (end != charpos && start != charpos)
4981 continue;
4983 /* Skip this overlay if it doesn't apply to IT->w. */
4984 window = Foverlay_get (overlay, Qwindow);
4985 if (WINDOWP (window) && XWINDOW (window) != it->w)
4986 continue;
4988 /* If the text ``under'' the overlay is invisible, both before-
4989 and after-strings from this overlay are visible; start and
4990 end position are indistinguishable. */
4991 invisible = Foverlay_get (overlay, Qinvisible);
4992 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4994 /* If overlay has a non-empty before-string, record it. */
4995 if ((start == charpos || (end == charpos && invis_p))
4996 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4997 && SCHARS (str))
4998 RECORD_OVERLAY_STRING (overlay, str, 0);
5000 /* If overlay has a non-empty after-string, record it. */
5001 if ((end == charpos || (start == charpos && invis_p))
5002 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5003 && SCHARS (str))
5004 RECORD_OVERLAY_STRING (overlay, str, 1);
5007 /* Process overlays after the overlay center. */
5008 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5010 XSETMISC (overlay, ov);
5011 xassert (OVERLAYP (overlay));
5012 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5013 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5015 if (start > charpos)
5016 break;
5018 /* Skip this overlay if it doesn't start or end at IT's current
5019 position. */
5020 if (end != charpos && start != charpos)
5021 continue;
5023 /* Skip this overlay if it doesn't apply to IT->w. */
5024 window = Foverlay_get (overlay, Qwindow);
5025 if (WINDOWP (window) && XWINDOW (window) != it->w)
5026 continue;
5028 /* If the text ``under'' the overlay is invisible, it has a zero
5029 dimension, and both before- and after-strings apply. */
5030 invisible = Foverlay_get (overlay, Qinvisible);
5031 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5033 /* If overlay has a non-empty before-string, record it. */
5034 if ((start == charpos || (end == charpos && invis_p))
5035 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5036 && SCHARS (str))
5037 RECORD_OVERLAY_STRING (overlay, str, 0);
5039 /* If overlay has a non-empty after-string, record it. */
5040 if ((end == charpos || (start == charpos && invis_p))
5041 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5042 && SCHARS (str))
5043 RECORD_OVERLAY_STRING (overlay, str, 1);
5046 #undef RECORD_OVERLAY_STRING
5048 /* Sort entries. */
5049 if (n > 1)
5050 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5052 /* Record number of overlay strings, and where we computed it. */
5053 it->n_overlay_strings = n;
5054 it->overlay_strings_charpos = charpos;
5056 /* IT->current.overlay_string_index is the number of overlay strings
5057 that have already been consumed by IT. Copy some of the
5058 remaining overlay strings to IT->overlay_strings. */
5059 i = 0;
5060 j = it->current.overlay_string_index;
5061 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5063 it->overlay_strings[i] = entries[j].string;
5064 it->string_overlays[i++] = entries[j++].overlay;
5067 CHECK_IT (it);
5071 /* Get the first chunk of overlay strings at IT's current buffer
5072 position, or at CHARPOS if that is > 0. Value is non-zero if at
5073 least one overlay string was found. */
5075 static int
5076 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5078 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5079 process. This fills IT->overlay_strings with strings, and sets
5080 IT->n_overlay_strings to the total number of strings to process.
5081 IT->pos.overlay_string_index has to be set temporarily to zero
5082 because load_overlay_strings needs this; it must be set to -1
5083 when no overlay strings are found because a zero value would
5084 indicate a position in the first overlay string. */
5085 it->current.overlay_string_index = 0;
5086 load_overlay_strings (it, charpos);
5088 /* If we found overlay strings, set up IT to deliver display
5089 elements from the first one. Otherwise set up IT to deliver
5090 from current_buffer. */
5091 if (it->n_overlay_strings)
5093 /* Make sure we know settings in current_buffer, so that we can
5094 restore meaningful values when we're done with the overlay
5095 strings. */
5096 if (compute_stop_p)
5097 compute_stop_pos (it);
5098 xassert (it->face_id >= 0);
5100 /* Save IT's settings. They are restored after all overlay
5101 strings have been processed. */
5102 xassert (!compute_stop_p || it->sp == 0);
5104 /* When called from handle_stop, there might be an empty display
5105 string loaded. In that case, don't bother saving it. */
5106 if (!STRINGP (it->string) || SCHARS (it->string))
5107 push_it (it);
5109 /* Set up IT to deliver display elements from the first overlay
5110 string. */
5111 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5112 it->string = it->overlay_strings[0];
5113 it->from_overlay = Qnil;
5114 it->stop_charpos = 0;
5115 xassert (STRINGP (it->string));
5116 it->end_charpos = SCHARS (it->string);
5117 it->multibyte_p = STRING_MULTIBYTE (it->string);
5118 it->method = GET_FROM_STRING;
5119 return 1;
5122 it->current.overlay_string_index = -1;
5123 return 0;
5126 static int
5127 get_overlay_strings (struct it *it, EMACS_INT charpos)
5129 it->string = Qnil;
5130 it->method = GET_FROM_BUFFER;
5132 (void) get_overlay_strings_1 (it, charpos, 1);
5134 CHECK_IT (it);
5136 /* Value is non-zero if we found at least one overlay string. */
5137 return STRINGP (it->string);
5142 /***********************************************************************
5143 Saving and restoring state
5144 ***********************************************************************/
5146 /* Save current settings of IT on IT->stack. Called, for example,
5147 before setting up IT for an overlay string, to be able to restore
5148 IT's settings to what they were after the overlay string has been
5149 processed. */
5151 static void
5152 push_it (struct it *it)
5154 struct iterator_stack_entry *p;
5156 xassert (it->sp < IT_STACK_SIZE);
5157 p = it->stack + it->sp;
5159 p->stop_charpos = it->stop_charpos;
5160 p->prev_stop = it->prev_stop;
5161 p->base_level_stop = it->base_level_stop;
5162 p->cmp_it = it->cmp_it;
5163 xassert (it->face_id >= 0);
5164 p->face_id = it->face_id;
5165 p->string = it->string;
5166 p->method = it->method;
5167 p->from_overlay = it->from_overlay;
5168 switch (p->method)
5170 case GET_FROM_IMAGE:
5171 p->u.image.object = it->object;
5172 p->u.image.image_id = it->image_id;
5173 p->u.image.slice = it->slice;
5174 break;
5175 case GET_FROM_STRETCH:
5176 p->u.stretch.object = it->object;
5177 break;
5179 p->position = it->position;
5180 p->current = it->current;
5181 p->end_charpos = it->end_charpos;
5182 p->string_nchars = it->string_nchars;
5183 p->area = it->area;
5184 p->multibyte_p = it->multibyte_p;
5185 p->avoid_cursor_p = it->avoid_cursor_p;
5186 p->space_width = it->space_width;
5187 p->font_height = it->font_height;
5188 p->voffset = it->voffset;
5189 p->string_from_display_prop_p = it->string_from_display_prop_p;
5190 p->display_ellipsis_p = 0;
5191 p->line_wrap = it->line_wrap;
5192 ++it->sp;
5195 static void
5196 iterate_out_of_display_property (struct it *it)
5198 /* Maybe initialize paragraph direction. If we are at the beginning
5199 of a new paragraph, next_element_from_buffer may not have a
5200 chance to do that. */
5201 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5202 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5203 /* prev_stop can be zero, so check against BEGV as well. */
5204 while (it->bidi_it.charpos >= BEGV
5205 && it->prev_stop <= it->bidi_it.charpos
5206 && it->bidi_it.charpos < CHARPOS (it->position))
5207 bidi_move_to_visually_next (&it->bidi_it);
5208 /* Record the stop_pos we just crossed, for when we cross it
5209 back, maybe. */
5210 if (it->bidi_it.charpos > CHARPOS (it->position))
5211 it->prev_stop = CHARPOS (it->position);
5212 /* If we ended up not where pop_it put us, resync IT's
5213 positional members with the bidi iterator. */
5214 if (it->bidi_it.charpos != CHARPOS (it->position))
5216 SET_TEXT_POS (it->position,
5217 it->bidi_it.charpos, it->bidi_it.bytepos);
5218 it->current.pos = it->position;
5222 /* Restore IT's settings from IT->stack. Called, for example, when no
5223 more overlay strings must be processed, and we return to delivering
5224 display elements from a buffer, or when the end of a string from a
5225 `display' property is reached and we return to delivering display
5226 elements from an overlay string, or from a buffer. */
5228 static void
5229 pop_it (struct it *it)
5231 struct iterator_stack_entry *p;
5233 xassert (it->sp > 0);
5234 --it->sp;
5235 p = it->stack + it->sp;
5236 it->stop_charpos = p->stop_charpos;
5237 it->prev_stop = p->prev_stop;
5238 it->base_level_stop = p->base_level_stop;
5239 it->cmp_it = p->cmp_it;
5240 it->face_id = p->face_id;
5241 it->current = p->current;
5242 it->position = p->position;
5243 it->string = p->string;
5244 it->from_overlay = p->from_overlay;
5245 if (NILP (it->string))
5246 SET_TEXT_POS (it->current.string_pos, -1, -1);
5247 it->method = p->method;
5248 switch (it->method)
5250 case GET_FROM_IMAGE:
5251 it->image_id = p->u.image.image_id;
5252 it->object = p->u.image.object;
5253 it->slice = p->u.image.slice;
5254 break;
5255 case GET_FROM_STRETCH:
5256 it->object = p->u.comp.object;
5257 break;
5258 case GET_FROM_BUFFER:
5259 it->object = it->w->buffer;
5260 if (it->bidi_p)
5262 /* Bidi-iterate until we get out of the portion of text, if
5263 any, covered by a `display' text property or an overlay
5264 with `display' property. (We cannot just jump there,
5265 because the internal coherency of the bidi iterator state
5266 can not be preserved across such jumps.) We also must
5267 determine the paragraph base direction if the overlay we
5268 just processed is at the beginning of a new
5269 paragraph. */
5270 iterate_out_of_display_property (it);
5272 break;
5273 case GET_FROM_STRING:
5274 it->object = it->string;
5275 break;
5276 case GET_FROM_DISPLAY_VECTOR:
5277 if (it->s)
5278 it->method = GET_FROM_C_STRING;
5279 else if (STRINGP (it->string))
5280 it->method = GET_FROM_STRING;
5281 else
5283 it->method = GET_FROM_BUFFER;
5284 it->object = it->w->buffer;
5287 it->end_charpos = p->end_charpos;
5288 it->string_nchars = p->string_nchars;
5289 it->area = p->area;
5290 it->multibyte_p = p->multibyte_p;
5291 it->avoid_cursor_p = p->avoid_cursor_p;
5292 it->space_width = p->space_width;
5293 it->font_height = p->font_height;
5294 it->voffset = p->voffset;
5295 it->string_from_display_prop_p = p->string_from_display_prop_p;
5296 it->line_wrap = p->line_wrap;
5301 /***********************************************************************
5302 Moving over lines
5303 ***********************************************************************/
5305 /* Set IT's current position to the previous line start. */
5307 static void
5308 back_to_previous_line_start (struct it *it)
5310 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5311 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5315 /* Move IT to the next line start.
5317 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5318 we skipped over part of the text (as opposed to moving the iterator
5319 continuously over the text). Otherwise, don't change the value
5320 of *SKIPPED_P.
5322 Newlines may come from buffer text, overlay strings, or strings
5323 displayed via the `display' property. That's the reason we can't
5324 simply use find_next_newline_no_quit.
5326 Note that this function may not skip over invisible text that is so
5327 because of text properties and immediately follows a newline. If
5328 it would, function reseat_at_next_visible_line_start, when called
5329 from set_iterator_to_next, would effectively make invisible
5330 characters following a newline part of the wrong glyph row, which
5331 leads to wrong cursor motion. */
5333 static int
5334 forward_to_next_line_start (struct it *it, int *skipped_p)
5336 int old_selective, newline_found_p, n;
5337 const int MAX_NEWLINE_DISTANCE = 500;
5339 /* If already on a newline, just consume it to avoid unintended
5340 skipping over invisible text below. */
5341 if (it->what == IT_CHARACTER
5342 && it->c == '\n'
5343 && CHARPOS (it->position) == IT_CHARPOS (*it))
5345 set_iterator_to_next (it, 0);
5346 it->c = 0;
5347 return 1;
5350 /* Don't handle selective display in the following. It's (a)
5351 unnecessary because it's done by the caller, and (b) leads to an
5352 infinite recursion because next_element_from_ellipsis indirectly
5353 calls this function. */
5354 old_selective = it->selective;
5355 it->selective = 0;
5357 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5358 from buffer text. */
5359 for (n = newline_found_p = 0;
5360 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5361 n += STRINGP (it->string) ? 0 : 1)
5363 if (!get_next_display_element (it))
5364 return 0;
5365 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5366 set_iterator_to_next (it, 0);
5369 /* If we didn't find a newline near enough, see if we can use a
5370 short-cut. */
5371 if (!newline_found_p)
5373 EMACS_INT start = IT_CHARPOS (*it);
5374 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5375 Lisp_Object pos;
5377 xassert (!STRINGP (it->string));
5379 /* If there isn't any `display' property in sight, and no
5380 overlays, we can just use the position of the newline in
5381 buffer text. */
5382 if (it->stop_charpos >= limit
5383 || ((pos = Fnext_single_property_change (make_number (start),
5384 Qdisplay,
5385 Qnil, make_number (limit)),
5386 NILP (pos))
5387 && next_overlay_change (start) == ZV))
5389 IT_CHARPOS (*it) = limit;
5390 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5391 *skipped_p = newline_found_p = 1;
5393 else
5395 while (get_next_display_element (it)
5396 && !newline_found_p)
5398 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5399 set_iterator_to_next (it, 0);
5404 it->selective = old_selective;
5405 return newline_found_p;
5409 /* Set IT's current position to the previous visible line start. Skip
5410 invisible text that is so either due to text properties or due to
5411 selective display. Caution: this does not change IT->current_x and
5412 IT->hpos. */
5414 static void
5415 back_to_previous_visible_line_start (struct it *it)
5417 while (IT_CHARPOS (*it) > BEGV)
5419 back_to_previous_line_start (it);
5421 if (IT_CHARPOS (*it) <= BEGV)
5422 break;
5424 /* If selective > 0, then lines indented more than its value are
5425 invisible. */
5426 if (it->selective > 0
5427 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5428 (double) it->selective)) /* iftc */
5429 continue;
5431 /* Check the newline before point for invisibility. */
5433 Lisp_Object prop;
5434 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5435 Qinvisible, it->window);
5436 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5437 continue;
5440 if (IT_CHARPOS (*it) <= BEGV)
5441 break;
5444 struct it it2;
5445 EMACS_INT pos;
5446 EMACS_INT beg, end;
5447 Lisp_Object val, overlay;
5449 /* If newline is part of a composition, continue from start of composition */
5450 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5451 && beg < IT_CHARPOS (*it))
5452 goto replaced;
5454 /* If newline is replaced by a display property, find start of overlay
5455 or interval and continue search from that point. */
5456 it2 = *it;
5457 pos = --IT_CHARPOS (it2);
5458 --IT_BYTEPOS (it2);
5459 it2.sp = 0;
5460 it2.string_from_display_prop_p = 0;
5461 if (handle_display_prop (&it2) == HANDLED_RETURN
5462 && !NILP (val = get_char_property_and_overlay
5463 (make_number (pos), Qdisplay, Qnil, &overlay))
5464 && (OVERLAYP (overlay)
5465 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5466 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5467 goto replaced;
5469 /* Newline is not replaced by anything -- so we are done. */
5470 break;
5472 replaced:
5473 if (beg < BEGV)
5474 beg = BEGV;
5475 IT_CHARPOS (*it) = beg;
5476 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5480 it->continuation_lines_width = 0;
5482 xassert (IT_CHARPOS (*it) >= BEGV);
5483 xassert (IT_CHARPOS (*it) == BEGV
5484 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5485 CHECK_IT (it);
5489 /* Reseat iterator IT at the previous visible line start. Skip
5490 invisible text that is so either due to text properties or due to
5491 selective display. At the end, update IT's overlay information,
5492 face information etc. */
5494 void
5495 reseat_at_previous_visible_line_start (struct it *it)
5497 back_to_previous_visible_line_start (it);
5498 reseat (it, it->current.pos, 1);
5499 CHECK_IT (it);
5503 /* Reseat iterator IT on the next visible line start in the current
5504 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5505 preceding the line start. Skip over invisible text that is so
5506 because of selective display. Compute faces, overlays etc at the
5507 new position. Note that this function does not skip over text that
5508 is invisible because of text properties. */
5510 static void
5511 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5513 int newline_found_p, skipped_p = 0;
5515 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5517 /* Skip over lines that are invisible because they are indented
5518 more than the value of IT->selective. */
5519 if (it->selective > 0)
5520 while (IT_CHARPOS (*it) < ZV
5521 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5522 (double) it->selective)) /* iftc */
5524 xassert (IT_BYTEPOS (*it) == BEGV
5525 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5526 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5529 /* Position on the newline if that's what's requested. */
5530 if (on_newline_p && newline_found_p)
5532 if (STRINGP (it->string))
5534 if (IT_STRING_CHARPOS (*it) > 0)
5536 --IT_STRING_CHARPOS (*it);
5537 --IT_STRING_BYTEPOS (*it);
5540 else if (IT_CHARPOS (*it) > BEGV)
5542 --IT_CHARPOS (*it);
5543 --IT_BYTEPOS (*it);
5544 reseat (it, it->current.pos, 0);
5547 else if (skipped_p)
5548 reseat (it, it->current.pos, 0);
5550 CHECK_IT (it);
5555 /***********************************************************************
5556 Changing an iterator's position
5557 ***********************************************************************/
5559 /* Change IT's current position to POS in current_buffer. If FORCE_P
5560 is non-zero, always check for text properties at the new position.
5561 Otherwise, text properties are only looked up if POS >=
5562 IT->check_charpos of a property. */
5564 static void
5565 reseat (struct it *it, struct text_pos pos, int force_p)
5567 EMACS_INT original_pos = IT_CHARPOS (*it);
5569 reseat_1 (it, pos, 0);
5571 /* Determine where to check text properties. Avoid doing it
5572 where possible because text property lookup is very expensive. */
5573 if (force_p
5574 || CHARPOS (pos) > it->stop_charpos
5575 || CHARPOS (pos) < original_pos)
5577 if (it->bidi_p)
5579 /* For bidi iteration, we need to prime prev_stop and
5580 base_level_stop with our best estimations. */
5581 if (CHARPOS (pos) < it->prev_stop)
5583 handle_stop_backwards (it, BEGV);
5584 if (CHARPOS (pos) < it->base_level_stop)
5585 it->base_level_stop = 0;
5587 else if (CHARPOS (pos) > it->stop_charpos
5588 && it->stop_charpos >= BEGV)
5589 handle_stop_backwards (it, it->stop_charpos);
5590 else /* force_p */
5591 handle_stop (it);
5593 else
5595 handle_stop (it);
5596 it->prev_stop = it->base_level_stop = 0;
5601 CHECK_IT (it);
5605 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5606 IT->stop_pos to POS, also. */
5608 static void
5609 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5611 /* Don't call this function when scanning a C string. */
5612 xassert (it->s == NULL);
5614 /* POS must be a reasonable value. */
5615 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5617 it->current.pos = it->position = pos;
5618 it->end_charpos = ZV;
5619 it->dpvec = NULL;
5620 it->current.dpvec_index = -1;
5621 it->current.overlay_string_index = -1;
5622 IT_STRING_CHARPOS (*it) = -1;
5623 IT_STRING_BYTEPOS (*it) = -1;
5624 it->string = Qnil;
5625 it->string_from_display_prop_p = 0;
5626 it->method = GET_FROM_BUFFER;
5627 it->object = it->w->buffer;
5628 it->area = TEXT_AREA;
5629 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5630 it->sp = 0;
5631 it->string_from_display_prop_p = 0;
5632 it->face_before_selective_p = 0;
5633 if (it->bidi_p)
5635 it->bidi_it.first_elt = 1;
5636 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5639 if (set_stop_p)
5641 it->stop_charpos = CHARPOS (pos);
5642 it->base_level_stop = CHARPOS (pos);
5647 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5648 If S is non-null, it is a C string to iterate over. Otherwise,
5649 STRING gives a Lisp string to iterate over.
5651 If PRECISION > 0, don't return more then PRECISION number of
5652 characters from the string.
5654 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5655 characters have been returned. FIELD_WIDTH < 0 means an infinite
5656 field width.
5658 MULTIBYTE = 0 means disable processing of multibyte characters,
5659 MULTIBYTE > 0 means enable it,
5660 MULTIBYTE < 0 means use IT->multibyte_p.
5662 IT must be initialized via a prior call to init_iterator before
5663 calling this function. */
5665 static void
5666 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5667 EMACS_INT charpos, EMACS_INT precision, int field_width,
5668 int multibyte)
5670 /* No region in strings. */
5671 it->region_beg_charpos = it->region_end_charpos = -1;
5673 /* No text property checks performed by default, but see below. */
5674 it->stop_charpos = -1;
5676 /* Set iterator position and end position. */
5677 memset (&it->current, 0, sizeof it->current);
5678 it->current.overlay_string_index = -1;
5679 it->current.dpvec_index = -1;
5680 xassert (charpos >= 0);
5682 /* If STRING is specified, use its multibyteness, otherwise use the
5683 setting of MULTIBYTE, if specified. */
5684 if (multibyte >= 0)
5685 it->multibyte_p = multibyte > 0;
5687 if (s == NULL)
5689 xassert (STRINGP (string));
5690 it->string = string;
5691 it->s = NULL;
5692 it->end_charpos = it->string_nchars = SCHARS (string);
5693 it->method = GET_FROM_STRING;
5694 it->current.string_pos = string_pos (charpos, string);
5696 else
5698 it->s = s;
5699 it->string = Qnil;
5701 /* Note that we use IT->current.pos, not it->current.string_pos,
5702 for displaying C strings. */
5703 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5704 if (it->multibyte_p)
5706 it->current.pos = c_string_pos (charpos, s, 1);
5707 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5709 else
5711 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5712 it->end_charpos = it->string_nchars = strlen (s);
5715 it->method = GET_FROM_C_STRING;
5718 /* PRECISION > 0 means don't return more than PRECISION characters
5719 from the string. */
5720 if (precision > 0 && it->end_charpos - charpos > precision)
5721 it->end_charpos = it->string_nchars = charpos + precision;
5723 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5724 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5725 FIELD_WIDTH < 0 means infinite field width. This is useful for
5726 padding with `-' at the end of a mode line. */
5727 if (field_width < 0)
5728 field_width = INFINITY;
5729 if (field_width > it->end_charpos - charpos)
5730 it->end_charpos = charpos + field_width;
5732 /* Use the standard display table for displaying strings. */
5733 if (DISP_TABLE_P (Vstandard_display_table))
5734 it->dp = XCHAR_TABLE (Vstandard_display_table);
5736 it->stop_charpos = charpos;
5737 if (s == NULL && it->multibyte_p)
5739 EMACS_INT endpos = SCHARS (it->string);
5740 if (endpos > it->end_charpos)
5741 endpos = it->end_charpos;
5742 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5743 it->string);
5745 CHECK_IT (it);
5750 /***********************************************************************
5751 Iteration
5752 ***********************************************************************/
5754 /* Map enum it_method value to corresponding next_element_from_* function. */
5756 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5758 next_element_from_buffer,
5759 next_element_from_display_vector,
5760 next_element_from_string,
5761 next_element_from_c_string,
5762 next_element_from_image,
5763 next_element_from_stretch
5766 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5769 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5770 (possibly with the following characters). */
5772 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5773 ((IT)->cmp_it.id >= 0 \
5774 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5775 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5776 END_CHARPOS, (IT)->w, \
5777 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5778 (IT)->string)))
5781 /* Lookup the char-table Vglyphless_char_display for character C (-1
5782 if we want information for no-font case), and return the display
5783 method symbol. By side-effect, update it->what and
5784 it->glyphless_method. This function is called from
5785 get_next_display_element for each character element, and from
5786 x_produce_glyphs when no suitable font was found. */
5788 Lisp_Object
5789 lookup_glyphless_char_display (int c, struct it *it)
5791 Lisp_Object glyphless_method = Qnil;
5793 if (CHAR_TABLE_P (Vglyphless_char_display)
5794 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5795 glyphless_method = (c >= 0
5796 ? CHAR_TABLE_REF (Vglyphless_char_display, c)
5797 : XCHAR_TABLE (Vglyphless_char_display)->extras[0]);
5798 retry:
5799 if (NILP (glyphless_method))
5801 if (c >= 0)
5802 /* The default is to display the character by a proper font. */
5803 return Qnil;
5804 /* The default for the no-font case is to display an empty box. */
5805 glyphless_method = Qempty_box;
5807 if (EQ (glyphless_method, Qzero_width))
5809 if (c >= 0)
5810 return glyphless_method;
5811 /* This method can't be used for the no-font case. */
5812 glyphless_method = Qempty_box;
5814 if (EQ (glyphless_method, Qthin_space))
5815 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5816 else if (EQ (glyphless_method, Qempty_box))
5817 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5818 else if (EQ (glyphless_method, Qhex_code))
5819 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5820 else if (STRINGP (glyphless_method))
5821 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5822 else
5824 /* Invalid value. We use the default method. */
5825 glyphless_method = Qnil;
5826 goto retry;
5828 it->what = IT_GLYPHLESS;
5829 return glyphless_method;
5832 /* Load IT's display element fields with information about the next
5833 display element from the current position of IT. Value is zero if
5834 end of buffer (or C string) is reached. */
5836 static struct frame *last_escape_glyph_frame = NULL;
5837 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5838 static int last_escape_glyph_merged_face_id = 0;
5840 struct frame *last_glyphless_glyph_frame = NULL;
5841 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5842 int last_glyphless_glyph_merged_face_id = 0;
5845 get_next_display_element (struct it *it)
5847 /* Non-zero means that we found a display element. Zero means that
5848 we hit the end of what we iterate over. Performance note: the
5849 function pointer `method' used here turns out to be faster than
5850 using a sequence of if-statements. */
5851 int success_p;
5853 get_next:
5854 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5856 if (it->what == IT_CHARACTER)
5858 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5859 and only if (a) the resolved directionality of that character
5860 is R..." */
5861 /* FIXME: Do we need an exception for characters from display
5862 tables? */
5863 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5864 it->c = bidi_mirror_char (it->c);
5865 /* Map via display table or translate control characters.
5866 IT->c, IT->len etc. have been set to the next character by
5867 the function call above. If we have a display table, and it
5868 contains an entry for IT->c, translate it. Don't do this if
5869 IT->c itself comes from a display table, otherwise we could
5870 end up in an infinite recursion. (An alternative could be to
5871 count the recursion depth of this function and signal an
5872 error when a certain maximum depth is reached.) Is it worth
5873 it? */
5874 if (success_p && it->dpvec == NULL)
5876 Lisp_Object dv;
5877 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5878 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5879 nbsp_or_shy = char_is_other;
5880 int c = it->c; /* This is the character to display. */
5882 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5884 xassert (SINGLE_BYTE_CHAR_P (c));
5885 if (unibyte_display_via_language_environment)
5887 c = DECODE_CHAR (unibyte, c);
5888 if (c < 0)
5889 c = BYTE8_TO_CHAR (it->c);
5891 else
5892 c = BYTE8_TO_CHAR (it->c);
5895 if (it->dp
5896 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5897 VECTORP (dv)))
5899 struct Lisp_Vector *v = XVECTOR (dv);
5901 /* Return the first character from the display table
5902 entry, if not empty. If empty, don't display the
5903 current character. */
5904 if (v->size)
5906 it->dpvec_char_len = it->len;
5907 it->dpvec = v->contents;
5908 it->dpend = v->contents + v->size;
5909 it->current.dpvec_index = 0;
5910 it->dpvec_face_id = -1;
5911 it->saved_face_id = it->face_id;
5912 it->method = GET_FROM_DISPLAY_VECTOR;
5913 it->ellipsis_p = 0;
5915 else
5917 set_iterator_to_next (it, 0);
5919 goto get_next;
5922 if (! NILP (lookup_glyphless_char_display (c, it)))
5924 if (it->what == IT_GLYPHLESS)
5925 goto done;
5926 /* Don't display this character. */
5927 set_iterator_to_next (it, 0);
5928 goto get_next;
5931 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5932 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5933 : c == 0xAD ? char_is_soft_hyphen
5934 : char_is_other);
5936 /* Translate control characters into `\003' or `^C' form.
5937 Control characters coming from a display table entry are
5938 currently not translated because we use IT->dpvec to hold
5939 the translation. This could easily be changed but I
5940 don't believe that it is worth doing.
5942 NBSP and SOFT-HYPEN are property translated too.
5944 Non-printable characters and raw-byte characters are also
5945 translated to octal form. */
5946 if (((c < ' ' || c == 127) /* ASCII control chars */
5947 ? (it->area != TEXT_AREA
5948 /* In mode line, treat \n, \t like other crl chars. */
5949 || (c != '\t'
5950 && it->glyph_row
5951 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5952 || (c != '\n' && c != '\t'))
5953 : (nbsp_or_shy
5954 || CHAR_BYTE8_P (c)
5955 || ! CHAR_PRINTABLE_P (c))))
5957 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5958 or a non-printable character which must be displayed
5959 either as '\003' or as `^C' where the '\\' and '^'
5960 can be defined in the display table. Fill
5961 IT->ctl_chars with glyphs for what we have to
5962 display. Then, set IT->dpvec to these glyphs. */
5963 Lisp_Object gc;
5964 int ctl_len;
5965 int face_id, lface_id = 0 ;
5966 int escape_glyph;
5968 /* Handle control characters with ^. */
5970 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5972 int g;
5974 g = '^'; /* default glyph for Control */
5975 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5976 if (it->dp
5977 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5978 && GLYPH_CODE_CHAR_VALID_P (gc))
5980 g = GLYPH_CODE_CHAR (gc);
5981 lface_id = GLYPH_CODE_FACE (gc);
5983 if (lface_id)
5985 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5987 else if (it->f == last_escape_glyph_frame
5988 && it->face_id == last_escape_glyph_face_id)
5990 face_id = last_escape_glyph_merged_face_id;
5992 else
5994 /* Merge the escape-glyph face into the current face. */
5995 face_id = merge_faces (it->f, Qescape_glyph, 0,
5996 it->face_id);
5997 last_escape_glyph_frame = it->f;
5998 last_escape_glyph_face_id = it->face_id;
5999 last_escape_glyph_merged_face_id = face_id;
6002 XSETINT (it->ctl_chars[0], g);
6003 XSETINT (it->ctl_chars[1], c ^ 0100);
6004 ctl_len = 2;
6005 goto display_control;
6008 /* Handle non-break space in the mode where it only gets
6009 highlighting. */
6011 if (EQ (Vnobreak_char_display, Qt)
6012 && nbsp_or_shy == char_is_nbsp)
6014 /* Merge the no-break-space face into the current face. */
6015 face_id = merge_faces (it->f, Qnobreak_space, 0,
6016 it->face_id);
6018 c = ' ';
6019 XSETINT (it->ctl_chars[0], ' ');
6020 ctl_len = 1;
6021 goto display_control;
6024 /* Handle sequences that start with the "escape glyph". */
6026 /* the default escape glyph is \. */
6027 escape_glyph = '\\';
6029 if (it->dp
6030 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6031 && GLYPH_CODE_CHAR_VALID_P (gc))
6033 escape_glyph = GLYPH_CODE_CHAR (gc);
6034 lface_id = GLYPH_CODE_FACE (gc);
6036 if (lface_id)
6038 /* The display table specified a face.
6039 Merge it into face_id and also into escape_glyph. */
6040 face_id = merge_faces (it->f, Qt, lface_id,
6041 it->face_id);
6043 else if (it->f == last_escape_glyph_frame
6044 && it->face_id == last_escape_glyph_face_id)
6046 face_id = last_escape_glyph_merged_face_id;
6048 else
6050 /* Merge the escape-glyph face into the current face. */
6051 face_id = merge_faces (it->f, Qescape_glyph, 0,
6052 it->face_id);
6053 last_escape_glyph_frame = it->f;
6054 last_escape_glyph_face_id = it->face_id;
6055 last_escape_glyph_merged_face_id = face_id;
6058 /* Handle soft hyphens in the mode where they only get
6059 highlighting. */
6061 if (EQ (Vnobreak_char_display, Qt)
6062 && nbsp_or_shy == char_is_soft_hyphen)
6064 XSETINT (it->ctl_chars[0], '-');
6065 ctl_len = 1;
6066 goto display_control;
6069 /* Handle non-break space and soft hyphen
6070 with the escape glyph. */
6072 if (nbsp_or_shy)
6074 XSETINT (it->ctl_chars[0], escape_glyph);
6075 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6076 XSETINT (it->ctl_chars[1], c);
6077 ctl_len = 2;
6078 goto display_control;
6082 char str[10];
6083 int len, i;
6085 if (CHAR_BYTE8_P (c))
6086 /* Display \200 instead of \17777600. */
6087 c = CHAR_TO_BYTE8 (c);
6088 len = sprintf (str, "%03o", c);
6090 XSETINT (it->ctl_chars[0], escape_glyph);
6091 for (i = 0; i < len; i++)
6092 XSETINT (it->ctl_chars[i + 1], str[i]);
6093 ctl_len = len + 1;
6096 display_control:
6097 /* Set up IT->dpvec and return first character from it. */
6098 it->dpvec_char_len = it->len;
6099 it->dpvec = it->ctl_chars;
6100 it->dpend = it->dpvec + ctl_len;
6101 it->current.dpvec_index = 0;
6102 it->dpvec_face_id = face_id;
6103 it->saved_face_id = it->face_id;
6104 it->method = GET_FROM_DISPLAY_VECTOR;
6105 it->ellipsis_p = 0;
6106 goto get_next;
6108 it->char_to_display = c;
6110 else if (success_p)
6112 it->char_to_display = it->c;
6116 #ifdef HAVE_WINDOW_SYSTEM
6117 /* Adjust face id for a multibyte character. There are no multibyte
6118 character in unibyte text. */
6119 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6120 && it->multibyte_p
6121 && success_p
6122 && FRAME_WINDOW_P (it->f))
6124 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6126 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6128 /* Automatic composition with glyph-string. */
6129 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6131 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6133 else
6135 EMACS_INT pos = (it->s ? -1
6136 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6137 : IT_CHARPOS (*it));
6139 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6140 it->string);
6143 #endif
6145 done:
6146 /* Is this character the last one of a run of characters with
6147 box? If yes, set IT->end_of_box_run_p to 1. */
6148 if (it->face_box_p
6149 && it->s == NULL)
6151 if (it->method == GET_FROM_STRING && it->sp)
6153 int face_id = underlying_face_id (it);
6154 struct face *face = FACE_FROM_ID (it->f, face_id);
6156 if (face)
6158 if (face->box == FACE_NO_BOX)
6160 /* If the box comes from face properties in a
6161 display string, check faces in that string. */
6162 int string_face_id = face_after_it_pos (it);
6163 it->end_of_box_run_p
6164 = (FACE_FROM_ID (it->f, string_face_id)->box
6165 == FACE_NO_BOX);
6167 /* Otherwise, the box comes from the underlying face.
6168 If this is the last string character displayed, check
6169 the next buffer location. */
6170 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6171 && (it->current.overlay_string_index
6172 == it->n_overlay_strings - 1))
6174 EMACS_INT ignore;
6175 int next_face_id;
6176 struct text_pos pos = it->current.pos;
6177 INC_TEXT_POS (pos, it->multibyte_p);
6179 next_face_id = face_at_buffer_position
6180 (it->w, CHARPOS (pos), it->region_beg_charpos,
6181 it->region_end_charpos, &ignore,
6182 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6183 -1);
6184 it->end_of_box_run_p
6185 = (FACE_FROM_ID (it->f, next_face_id)->box
6186 == FACE_NO_BOX);
6190 else
6192 int face_id = face_after_it_pos (it);
6193 it->end_of_box_run_p
6194 = (face_id != it->face_id
6195 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6199 /* Value is 0 if end of buffer or string reached. */
6200 return success_p;
6204 /* Move IT to the next display element.
6206 RESEAT_P non-zero means if called on a newline in buffer text,
6207 skip to the next visible line start.
6209 Functions get_next_display_element and set_iterator_to_next are
6210 separate because I find this arrangement easier to handle than a
6211 get_next_display_element function that also increments IT's
6212 position. The way it is we can first look at an iterator's current
6213 display element, decide whether it fits on a line, and if it does,
6214 increment the iterator position. The other way around we probably
6215 would either need a flag indicating whether the iterator has to be
6216 incremented the next time, or we would have to implement a
6217 decrement position function which would not be easy to write. */
6219 void
6220 set_iterator_to_next (struct it *it, int reseat_p)
6222 /* Reset flags indicating start and end of a sequence of characters
6223 with box. Reset them at the start of this function because
6224 moving the iterator to a new position might set them. */
6225 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6227 switch (it->method)
6229 case GET_FROM_BUFFER:
6230 /* The current display element of IT is a character from
6231 current_buffer. Advance in the buffer, and maybe skip over
6232 invisible lines that are so because of selective display. */
6233 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6234 reseat_at_next_visible_line_start (it, 0);
6235 else if (it->cmp_it.id >= 0)
6237 /* We are currently getting glyphs from a composition. */
6238 int i;
6240 if (! it->bidi_p)
6242 IT_CHARPOS (*it) += it->cmp_it.nchars;
6243 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6244 if (it->cmp_it.to < it->cmp_it.nglyphs)
6246 it->cmp_it.from = it->cmp_it.to;
6248 else
6250 it->cmp_it.id = -1;
6251 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6252 IT_BYTEPOS (*it),
6253 it->end_charpos, Qnil);
6256 else if (! it->cmp_it.reversed_p)
6258 /* Composition created while scanning forward. */
6259 /* Update IT's char/byte positions to point to the first
6260 character of the next grapheme cluster, or to the
6261 character visually after the current composition. */
6262 for (i = 0; i < it->cmp_it.nchars; i++)
6263 bidi_move_to_visually_next (&it->bidi_it);
6264 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6265 IT_CHARPOS (*it) = it->bidi_it.charpos;
6267 if (it->cmp_it.to < it->cmp_it.nglyphs)
6269 /* Proceed to the next grapheme cluster. */
6270 it->cmp_it.from = it->cmp_it.to;
6272 else
6274 /* No more grapheme clusters in this composition.
6275 Find the next stop position. */
6276 EMACS_INT stop = it->end_charpos;
6277 if (it->bidi_it.scan_dir < 0)
6278 /* Now we are scanning backward and don't know
6279 where to stop. */
6280 stop = -1;
6281 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6282 IT_BYTEPOS (*it), stop, Qnil);
6285 else
6287 /* Composition created while scanning backward. */
6288 /* Update IT's char/byte positions to point to the last
6289 character of the previous grapheme cluster, or the
6290 character visually after the current composition. */
6291 for (i = 0; i < it->cmp_it.nchars; i++)
6292 bidi_move_to_visually_next (&it->bidi_it);
6293 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6294 IT_CHARPOS (*it) = it->bidi_it.charpos;
6295 if (it->cmp_it.from > 0)
6297 /* Proceed to the previous grapheme cluster. */
6298 it->cmp_it.to = it->cmp_it.from;
6300 else
6302 /* No more grapheme clusters in this composition.
6303 Find the next stop position. */
6304 EMACS_INT stop = it->end_charpos;
6305 if (it->bidi_it.scan_dir < 0)
6306 /* Now we are scanning backward and don't know
6307 where to stop. */
6308 stop = -1;
6309 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6310 IT_BYTEPOS (*it), stop, Qnil);
6314 else
6316 xassert (it->len != 0);
6318 if (!it->bidi_p)
6320 IT_BYTEPOS (*it) += it->len;
6321 IT_CHARPOS (*it) += 1;
6323 else
6325 int prev_scan_dir = it->bidi_it.scan_dir;
6326 /* If this is a new paragraph, determine its base
6327 direction (a.k.a. its base embedding level). */
6328 if (it->bidi_it.new_paragraph)
6329 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6330 bidi_move_to_visually_next (&it->bidi_it);
6331 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6332 IT_CHARPOS (*it) = it->bidi_it.charpos;
6333 if (prev_scan_dir != it->bidi_it.scan_dir)
6335 /* As the scan direction was changed, we must
6336 re-compute the stop position for composition. */
6337 EMACS_INT stop = it->end_charpos;
6338 if (it->bidi_it.scan_dir < 0)
6339 stop = -1;
6340 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6341 IT_BYTEPOS (*it), stop, Qnil);
6344 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6346 break;
6348 case GET_FROM_C_STRING:
6349 /* Current display element of IT is from a C string. */
6350 IT_BYTEPOS (*it) += it->len;
6351 IT_CHARPOS (*it) += 1;
6352 break;
6354 case GET_FROM_DISPLAY_VECTOR:
6355 /* Current display element of IT is from a display table entry.
6356 Advance in the display table definition. Reset it to null if
6357 end reached, and continue with characters from buffers/
6358 strings. */
6359 ++it->current.dpvec_index;
6361 /* Restore face of the iterator to what they were before the
6362 display vector entry (these entries may contain faces). */
6363 it->face_id = it->saved_face_id;
6365 if (it->dpvec + it->current.dpvec_index == it->dpend)
6367 int recheck_faces = it->ellipsis_p;
6369 if (it->s)
6370 it->method = GET_FROM_C_STRING;
6371 else if (STRINGP (it->string))
6372 it->method = GET_FROM_STRING;
6373 else
6375 it->method = GET_FROM_BUFFER;
6376 it->object = it->w->buffer;
6379 it->dpvec = NULL;
6380 it->current.dpvec_index = -1;
6382 /* Skip over characters which were displayed via IT->dpvec. */
6383 if (it->dpvec_char_len < 0)
6384 reseat_at_next_visible_line_start (it, 1);
6385 else if (it->dpvec_char_len > 0)
6387 if (it->method == GET_FROM_STRING
6388 && it->n_overlay_strings > 0)
6389 it->ignore_overlay_strings_at_pos_p = 1;
6390 it->len = it->dpvec_char_len;
6391 set_iterator_to_next (it, reseat_p);
6394 /* Maybe recheck faces after display vector */
6395 if (recheck_faces)
6396 it->stop_charpos = IT_CHARPOS (*it);
6398 break;
6400 case GET_FROM_STRING:
6401 /* Current display element is a character from a Lisp string. */
6402 xassert (it->s == NULL && STRINGP (it->string));
6403 if (it->cmp_it.id >= 0)
6405 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6406 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6407 if (it->cmp_it.to < it->cmp_it.nglyphs)
6408 it->cmp_it.from = it->cmp_it.to;
6409 else
6411 it->cmp_it.id = -1;
6412 composition_compute_stop_pos (&it->cmp_it,
6413 IT_STRING_CHARPOS (*it),
6414 IT_STRING_BYTEPOS (*it),
6415 it->end_charpos, it->string);
6418 else
6420 IT_STRING_BYTEPOS (*it) += it->len;
6421 IT_STRING_CHARPOS (*it) += 1;
6424 consider_string_end:
6426 if (it->current.overlay_string_index >= 0)
6428 /* IT->string is an overlay string. Advance to the
6429 next, if there is one. */
6430 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6432 it->ellipsis_p = 0;
6433 next_overlay_string (it);
6434 if (it->ellipsis_p)
6435 setup_for_ellipsis (it, 0);
6438 else
6440 /* IT->string is not an overlay string. If we reached
6441 its end, and there is something on IT->stack, proceed
6442 with what is on the stack. This can be either another
6443 string, this time an overlay string, or a buffer. */
6444 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6445 && it->sp > 0)
6447 pop_it (it);
6448 if (it->method == GET_FROM_STRING)
6449 goto consider_string_end;
6452 break;
6454 case GET_FROM_IMAGE:
6455 case GET_FROM_STRETCH:
6456 /* The position etc with which we have to proceed are on
6457 the stack. The position may be at the end of a string,
6458 if the `display' property takes up the whole string. */
6459 xassert (it->sp > 0);
6460 pop_it (it);
6461 if (it->method == GET_FROM_STRING)
6462 goto consider_string_end;
6463 break;
6465 default:
6466 /* There are no other methods defined, so this should be a bug. */
6467 abort ();
6470 xassert (it->method != GET_FROM_STRING
6471 || (STRINGP (it->string)
6472 && IT_STRING_CHARPOS (*it) >= 0));
6475 /* Load IT's display element fields with information about the next
6476 display element which comes from a display table entry or from the
6477 result of translating a control character to one of the forms `^C'
6478 or `\003'.
6480 IT->dpvec holds the glyphs to return as characters.
6481 IT->saved_face_id holds the face id before the display vector--it
6482 is restored into IT->face_id in set_iterator_to_next. */
6484 static int
6485 next_element_from_display_vector (struct it *it)
6487 Lisp_Object gc;
6489 /* Precondition. */
6490 xassert (it->dpvec && it->current.dpvec_index >= 0);
6492 it->face_id = it->saved_face_id;
6494 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6495 That seemed totally bogus - so I changed it... */
6496 gc = it->dpvec[it->current.dpvec_index];
6498 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6500 it->c = GLYPH_CODE_CHAR (gc);
6501 it->len = CHAR_BYTES (it->c);
6503 /* The entry may contain a face id to use. Such a face id is
6504 the id of a Lisp face, not a realized face. A face id of
6505 zero means no face is specified. */
6506 if (it->dpvec_face_id >= 0)
6507 it->face_id = it->dpvec_face_id;
6508 else
6510 int lface_id = GLYPH_CODE_FACE (gc);
6511 if (lface_id > 0)
6512 it->face_id = merge_faces (it->f, Qt, lface_id,
6513 it->saved_face_id);
6516 else
6517 /* Display table entry is invalid. Return a space. */
6518 it->c = ' ', it->len = 1;
6520 /* Don't change position and object of the iterator here. They are
6521 still the values of the character that had this display table
6522 entry or was translated, and that's what we want. */
6523 it->what = IT_CHARACTER;
6524 return 1;
6528 /* Load IT with the next display element from Lisp string IT->string.
6529 IT->current.string_pos is the current position within the string.
6530 If IT->current.overlay_string_index >= 0, the Lisp string is an
6531 overlay string. */
6533 static int
6534 next_element_from_string (struct it *it)
6536 struct text_pos position;
6538 xassert (STRINGP (it->string));
6539 xassert (IT_STRING_CHARPOS (*it) >= 0);
6540 position = it->current.string_pos;
6542 /* Time to check for invisible text? */
6543 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6544 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6546 handle_stop (it);
6548 /* Since a handler may have changed IT->method, we must
6549 recurse here. */
6550 return GET_NEXT_DISPLAY_ELEMENT (it);
6553 if (it->current.overlay_string_index >= 0)
6555 /* Get the next character from an overlay string. In overlay
6556 strings, There is no field width or padding with spaces to
6557 do. */
6558 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6560 it->what = IT_EOB;
6561 return 0;
6563 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6564 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6565 && next_element_from_composition (it))
6567 return 1;
6569 else if (STRING_MULTIBYTE (it->string))
6571 const unsigned char *s = (SDATA (it->string)
6572 + IT_STRING_BYTEPOS (*it));
6573 it->c = string_char_and_length (s, &it->len);
6575 else
6577 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6578 it->len = 1;
6581 else
6583 /* Get the next character from a Lisp string that is not an
6584 overlay string. Such strings come from the mode line, for
6585 example. We may have to pad with spaces, or truncate the
6586 string. See also next_element_from_c_string. */
6587 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6589 it->what = IT_EOB;
6590 return 0;
6592 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6594 /* Pad with spaces. */
6595 it->c = ' ', it->len = 1;
6596 CHARPOS (position) = BYTEPOS (position) = -1;
6598 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6599 IT_STRING_BYTEPOS (*it), it->string_nchars)
6600 && next_element_from_composition (it))
6602 return 1;
6604 else if (STRING_MULTIBYTE (it->string))
6606 const unsigned char *s = (SDATA (it->string)
6607 + IT_STRING_BYTEPOS (*it));
6608 it->c = string_char_and_length (s, &it->len);
6610 else
6612 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6613 it->len = 1;
6617 /* Record what we have and where it came from. */
6618 it->what = IT_CHARACTER;
6619 it->object = it->string;
6620 it->position = position;
6621 return 1;
6625 /* Load IT with next display element from C string IT->s.
6626 IT->string_nchars is the maximum number of characters to return
6627 from the string. IT->end_charpos may be greater than
6628 IT->string_nchars when this function is called, in which case we
6629 may have to return padding spaces. Value is zero if end of string
6630 reached, including padding spaces. */
6632 static int
6633 next_element_from_c_string (struct it *it)
6635 int success_p = 1;
6637 xassert (it->s);
6638 it->what = IT_CHARACTER;
6639 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6640 it->object = Qnil;
6642 /* IT's position can be greater IT->string_nchars in case a field
6643 width or precision has been specified when the iterator was
6644 initialized. */
6645 if (IT_CHARPOS (*it) >= it->end_charpos)
6647 /* End of the game. */
6648 it->what = IT_EOB;
6649 success_p = 0;
6651 else if (IT_CHARPOS (*it) >= it->string_nchars)
6653 /* Pad with spaces. */
6654 it->c = ' ', it->len = 1;
6655 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6657 else if (it->multibyte_p)
6658 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6659 else
6660 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6662 return success_p;
6666 /* Set up IT to return characters from an ellipsis, if appropriate.
6667 The definition of the ellipsis glyphs may come from a display table
6668 entry. This function fills IT with the first glyph from the
6669 ellipsis if an ellipsis is to be displayed. */
6671 static int
6672 next_element_from_ellipsis (struct it *it)
6674 if (it->selective_display_ellipsis_p)
6675 setup_for_ellipsis (it, it->len);
6676 else
6678 /* The face at the current position may be different from the
6679 face we find after the invisible text. Remember what it
6680 was in IT->saved_face_id, and signal that it's there by
6681 setting face_before_selective_p. */
6682 it->saved_face_id = it->face_id;
6683 it->method = GET_FROM_BUFFER;
6684 it->object = it->w->buffer;
6685 reseat_at_next_visible_line_start (it, 1);
6686 it->face_before_selective_p = 1;
6689 return GET_NEXT_DISPLAY_ELEMENT (it);
6693 /* Deliver an image display element. The iterator IT is already
6694 filled with image information (done in handle_display_prop). Value
6695 is always 1. */
6698 static int
6699 next_element_from_image (struct it *it)
6701 it->what = IT_IMAGE;
6702 it->ignore_overlay_strings_at_pos_p = 0;
6703 return 1;
6707 /* Fill iterator IT with next display element from a stretch glyph
6708 property. IT->object is the value of the text property. Value is
6709 always 1. */
6711 static int
6712 next_element_from_stretch (struct it *it)
6714 it->what = IT_STRETCH;
6715 return 1;
6718 /* Scan forward from CHARPOS in the current buffer, until we find a
6719 stop position > current IT's position. Then handle the stop
6720 position before that. This is called when we bump into a stop
6721 position while reordering bidirectional text. CHARPOS should be
6722 the last previously processed stop_pos (or BEGV, if none were
6723 processed yet) whose position is less that IT's current
6724 position. */
6726 static void
6727 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6729 EMACS_INT where_we_are = IT_CHARPOS (*it);
6730 struct display_pos save_current = it->current;
6731 struct text_pos save_position = it->position;
6732 struct text_pos pos1;
6733 EMACS_INT next_stop;
6735 /* Scan in strict logical order. */
6736 it->bidi_p = 0;
6739 it->prev_stop = charpos;
6740 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6741 reseat_1 (it, pos1, 0);
6742 compute_stop_pos (it);
6743 /* We must advance forward, right? */
6744 if (it->stop_charpos <= it->prev_stop)
6745 abort ();
6746 charpos = it->stop_charpos;
6748 while (charpos <= where_we_are);
6750 next_stop = it->stop_charpos;
6751 it->stop_charpos = it->prev_stop;
6752 it->bidi_p = 1;
6753 it->current = save_current;
6754 it->position = save_position;
6755 handle_stop (it);
6756 it->stop_charpos = next_stop;
6759 /* Load IT with the next display element from current_buffer. Value
6760 is zero if end of buffer reached. IT->stop_charpos is the next
6761 position at which to stop and check for text properties or buffer
6762 end. */
6764 static int
6765 next_element_from_buffer (struct it *it)
6767 int success_p = 1;
6769 xassert (IT_CHARPOS (*it) >= BEGV);
6771 /* With bidi reordering, the character to display might not be the
6772 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6773 we were reseat()ed to a new buffer position, which is potentially
6774 a different paragraph. */
6775 if (it->bidi_p && it->bidi_it.first_elt)
6777 it->bidi_it.charpos = IT_CHARPOS (*it);
6778 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6779 if (it->bidi_it.bytepos == ZV_BYTE)
6781 /* Nothing to do, but reset the FIRST_ELT flag, like
6782 bidi_paragraph_init does, because we are not going to
6783 call it. */
6784 it->bidi_it.first_elt = 0;
6786 else if (it->bidi_it.bytepos == BEGV_BYTE
6787 /* FIXME: Should support all Unicode line separators. */
6788 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6789 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6791 /* If we are at the beginning of a line, we can produce the
6792 next element right away. */
6793 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6794 bidi_move_to_visually_next (&it->bidi_it);
6796 else
6798 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6800 /* We need to prime the bidi iterator starting at the line's
6801 beginning, before we will be able to produce the next
6802 element. */
6803 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6804 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6805 it->bidi_it.charpos = IT_CHARPOS (*it);
6806 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6807 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6810 /* Now return to buffer position where we were asked to
6811 get the next display element, and produce that. */
6812 bidi_move_to_visually_next (&it->bidi_it);
6814 while (it->bidi_it.bytepos != orig_bytepos
6815 && it->bidi_it.bytepos < ZV_BYTE);
6818 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6819 /* Adjust IT's position information to where we ended up. */
6820 IT_CHARPOS (*it) = it->bidi_it.charpos;
6821 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6822 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6824 EMACS_INT stop = it->end_charpos;
6825 if (it->bidi_it.scan_dir < 0)
6826 stop = -1;
6827 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6828 IT_BYTEPOS (*it), stop, Qnil);
6832 if (IT_CHARPOS (*it) >= it->stop_charpos)
6834 if (IT_CHARPOS (*it) >= it->end_charpos)
6836 int overlay_strings_follow_p;
6838 /* End of the game, except when overlay strings follow that
6839 haven't been returned yet. */
6840 if (it->overlay_strings_at_end_processed_p)
6841 overlay_strings_follow_p = 0;
6842 else
6844 it->overlay_strings_at_end_processed_p = 1;
6845 overlay_strings_follow_p = get_overlay_strings (it, 0);
6848 if (overlay_strings_follow_p)
6849 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6850 else
6852 it->what = IT_EOB;
6853 it->position = it->current.pos;
6854 success_p = 0;
6857 else if (!(!it->bidi_p
6858 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6859 || IT_CHARPOS (*it) == it->stop_charpos))
6861 /* With bidi non-linear iteration, we could find ourselves
6862 far beyond the last computed stop_charpos, with several
6863 other stop positions in between that we missed. Scan
6864 them all now, in buffer's logical order, until we find
6865 and handle the last stop_charpos that precedes our
6866 current position. */
6867 handle_stop_backwards (it, it->stop_charpos);
6868 return GET_NEXT_DISPLAY_ELEMENT (it);
6870 else
6872 if (it->bidi_p)
6874 /* Take note of the stop position we just moved across,
6875 for when we will move back across it. */
6876 it->prev_stop = it->stop_charpos;
6877 /* If we are at base paragraph embedding level, take
6878 note of the last stop position seen at this
6879 level. */
6880 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6881 it->base_level_stop = it->stop_charpos;
6883 handle_stop (it);
6884 return GET_NEXT_DISPLAY_ELEMENT (it);
6887 else if (it->bidi_p
6888 /* We can sometimes back up for reasons that have nothing
6889 to do with bidi reordering. E.g., compositions. The
6890 code below is only needed when we are above the base
6891 embedding level, so test for that explicitly. */
6892 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6893 && IT_CHARPOS (*it) < it->prev_stop)
6895 if (it->base_level_stop <= 0)
6896 it->base_level_stop = BEGV;
6897 if (IT_CHARPOS (*it) < it->base_level_stop)
6898 abort ();
6899 handle_stop_backwards (it, it->base_level_stop);
6900 return GET_NEXT_DISPLAY_ELEMENT (it);
6902 else
6904 /* No face changes, overlays etc. in sight, so just return a
6905 character from current_buffer. */
6906 unsigned char *p;
6907 EMACS_INT stop;
6909 /* Maybe run the redisplay end trigger hook. Performance note:
6910 This doesn't seem to cost measurable time. */
6911 if (it->redisplay_end_trigger_charpos
6912 && it->glyph_row
6913 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6914 run_redisplay_end_trigger_hook (it);
6916 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6917 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6918 stop)
6919 && next_element_from_composition (it))
6921 return 1;
6924 /* Get the next character, maybe multibyte. */
6925 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6926 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6927 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6928 else
6929 it->c = *p, it->len = 1;
6931 /* Record what we have and where it came from. */
6932 it->what = IT_CHARACTER;
6933 it->object = it->w->buffer;
6934 it->position = it->current.pos;
6936 /* Normally we return the character found above, except when we
6937 really want to return an ellipsis for selective display. */
6938 if (it->selective)
6940 if (it->c == '\n')
6942 /* A value of selective > 0 means hide lines indented more
6943 than that number of columns. */
6944 if (it->selective > 0
6945 && IT_CHARPOS (*it) + 1 < ZV
6946 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6947 IT_BYTEPOS (*it) + 1,
6948 (double) it->selective)) /* iftc */
6950 success_p = next_element_from_ellipsis (it);
6951 it->dpvec_char_len = -1;
6954 else if (it->c == '\r' && it->selective == -1)
6956 /* A value of selective == -1 means that everything from the
6957 CR to the end of the line is invisible, with maybe an
6958 ellipsis displayed for it. */
6959 success_p = next_element_from_ellipsis (it);
6960 it->dpvec_char_len = -1;
6965 /* Value is zero if end of buffer reached. */
6966 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6967 return success_p;
6971 /* Run the redisplay end trigger hook for IT. */
6973 static void
6974 run_redisplay_end_trigger_hook (struct it *it)
6976 Lisp_Object args[3];
6978 /* IT->glyph_row should be non-null, i.e. we should be actually
6979 displaying something, or otherwise we should not run the hook. */
6980 xassert (it->glyph_row);
6982 /* Set up hook arguments. */
6983 args[0] = Qredisplay_end_trigger_functions;
6984 args[1] = it->window;
6985 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6986 it->redisplay_end_trigger_charpos = 0;
6988 /* Since we are *trying* to run these functions, don't try to run
6989 them again, even if they get an error. */
6990 it->w->redisplay_end_trigger = Qnil;
6991 Frun_hook_with_args (3, args);
6993 /* Notice if it changed the face of the character we are on. */
6994 handle_face_prop (it);
6998 /* Deliver a composition display element. Unlike the other
6999 next_element_from_XXX, this function is not registered in the array
7000 get_next_element[]. It is called from next_element_from_buffer and
7001 next_element_from_string when necessary. */
7003 static int
7004 next_element_from_composition (struct it *it)
7006 it->what = IT_COMPOSITION;
7007 it->len = it->cmp_it.nbytes;
7008 if (STRINGP (it->string))
7010 if (it->c < 0)
7012 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7013 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7014 return 0;
7016 it->position = it->current.string_pos;
7017 it->object = it->string;
7018 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7019 IT_STRING_BYTEPOS (*it), it->string);
7021 else
7023 if (it->c < 0)
7025 IT_CHARPOS (*it) += it->cmp_it.nchars;
7026 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7027 if (it->bidi_p)
7029 if (it->bidi_it.new_paragraph)
7030 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7031 /* Resync the bidi iterator with IT's new position.
7032 FIXME: this doesn't support bidirectional text. */
7033 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7034 bidi_move_to_visually_next (&it->bidi_it);
7036 return 0;
7038 it->position = it->current.pos;
7039 it->object = it->w->buffer;
7040 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7041 IT_BYTEPOS (*it), Qnil);
7043 return 1;
7048 /***********************************************************************
7049 Moving an iterator without producing glyphs
7050 ***********************************************************************/
7052 /* Check if iterator is at a position corresponding to a valid buffer
7053 position after some move_it_ call. */
7055 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7056 ((it)->method == GET_FROM_STRING \
7057 ? IT_STRING_CHARPOS (*it) == 0 \
7058 : 1)
7061 /* Move iterator IT to a specified buffer or X position within one
7062 line on the display without producing glyphs.
7064 OP should be a bit mask including some or all of these bits:
7065 MOVE_TO_X: Stop upon reaching x-position TO_X.
7066 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7067 Regardless of OP's value, stop upon reaching the end of the display line.
7069 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7070 This means, in particular, that TO_X includes window's horizontal
7071 scroll amount.
7073 The return value has several possible values that
7074 say what condition caused the scan to stop:
7076 MOVE_POS_MATCH_OR_ZV
7077 - when TO_POS or ZV was reached.
7079 MOVE_X_REACHED
7080 -when TO_X was reached before TO_POS or ZV were reached.
7082 MOVE_LINE_CONTINUED
7083 - when we reached the end of the display area and the line must
7084 be continued.
7086 MOVE_LINE_TRUNCATED
7087 - when we reached the end of the display area and the line is
7088 truncated.
7090 MOVE_NEWLINE_OR_CR
7091 - when we stopped at a line end, i.e. a newline or a CR and selective
7092 display is on. */
7094 static enum move_it_result
7095 move_it_in_display_line_to (struct it *it,
7096 EMACS_INT to_charpos, int to_x,
7097 enum move_operation_enum op)
7099 enum move_it_result result = MOVE_UNDEFINED;
7100 struct glyph_row *saved_glyph_row;
7101 struct it wrap_it, atpos_it, atx_it;
7102 int may_wrap = 0;
7103 enum it_method prev_method = it->method;
7104 EMACS_INT prev_pos = IT_CHARPOS (*it);
7106 /* Don't produce glyphs in produce_glyphs. */
7107 saved_glyph_row = it->glyph_row;
7108 it->glyph_row = NULL;
7110 /* Use wrap_it to save a copy of IT wherever a word wrap could
7111 occur. Use atpos_it to save a copy of IT at the desired buffer
7112 position, if found, so that we can scan ahead and check if the
7113 word later overshoots the window edge. Use atx_it similarly, for
7114 pixel positions. */
7115 wrap_it.sp = -1;
7116 atpos_it.sp = -1;
7117 atx_it.sp = -1;
7119 #define BUFFER_POS_REACHED_P() \
7120 ((op & MOVE_TO_POS) != 0 \
7121 && BUFFERP (it->object) \
7122 && (IT_CHARPOS (*it) == to_charpos \
7123 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7124 && (it->method == GET_FROM_BUFFER \
7125 || (it->method == GET_FROM_DISPLAY_VECTOR \
7126 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7128 /* If there's a line-/wrap-prefix, handle it. */
7129 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7130 && it->current_y < it->last_visible_y)
7131 handle_line_prefix (it);
7133 while (1)
7135 int x, i, ascent = 0, descent = 0;
7137 /* Utility macro to reset an iterator with x, ascent, and descent. */
7138 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7139 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7140 (IT)->max_descent = descent)
7142 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7143 glyph). */
7144 if ((op & MOVE_TO_POS) != 0
7145 && BUFFERP (it->object)
7146 && it->method == GET_FROM_BUFFER
7147 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7148 || (it->bidi_p
7149 && (prev_method == GET_FROM_IMAGE
7150 || prev_method == GET_FROM_STRETCH)
7151 /* Passed TO_CHARPOS from left to right. */
7152 && ((prev_pos < to_charpos
7153 && IT_CHARPOS (*it) > to_charpos)
7154 /* Passed TO_CHARPOS from right to left. */
7155 || (prev_pos > to_charpos
7156 && IT_CHARPOS (*it) < to_charpos)))))
7158 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7160 result = MOVE_POS_MATCH_OR_ZV;
7161 break;
7163 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7164 /* If wrap_it is valid, the current position might be in a
7165 word that is wrapped. So, save the iterator in
7166 atpos_it and continue to see if wrapping happens. */
7167 atpos_it = *it;
7170 prev_method = it->method;
7171 if (it->method == GET_FROM_BUFFER)
7172 prev_pos = IT_CHARPOS (*it);
7173 /* Stop when ZV reached.
7174 We used to stop here when TO_CHARPOS reached as well, but that is
7175 too soon if this glyph does not fit on this line. So we handle it
7176 explicitly below. */
7177 if (!get_next_display_element (it))
7179 result = MOVE_POS_MATCH_OR_ZV;
7180 break;
7183 if (it->line_wrap == TRUNCATE)
7185 if (BUFFER_POS_REACHED_P ())
7187 result = MOVE_POS_MATCH_OR_ZV;
7188 break;
7191 else
7193 if (it->line_wrap == WORD_WRAP)
7195 if (IT_DISPLAYING_WHITESPACE (it))
7196 may_wrap = 1;
7197 else if (may_wrap)
7199 /* We have reached a glyph that follows one or more
7200 whitespace characters. If the position is
7201 already found, we are done. */
7202 if (atpos_it.sp >= 0)
7204 *it = atpos_it;
7205 result = MOVE_POS_MATCH_OR_ZV;
7206 goto done;
7208 if (atx_it.sp >= 0)
7210 *it = atx_it;
7211 result = MOVE_X_REACHED;
7212 goto done;
7214 /* Otherwise, we can wrap here. */
7215 wrap_it = *it;
7216 may_wrap = 0;
7221 /* Remember the line height for the current line, in case
7222 the next element doesn't fit on the line. */
7223 ascent = it->max_ascent;
7224 descent = it->max_descent;
7226 /* The call to produce_glyphs will get the metrics of the
7227 display element IT is loaded with. Record the x-position
7228 before this display element, in case it doesn't fit on the
7229 line. */
7230 x = it->current_x;
7232 PRODUCE_GLYPHS (it);
7234 if (it->area != TEXT_AREA)
7236 set_iterator_to_next (it, 1);
7237 continue;
7240 /* The number of glyphs we get back in IT->nglyphs will normally
7241 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7242 character on a terminal frame, or (iii) a line end. For the
7243 second case, IT->nglyphs - 1 padding glyphs will be present.
7244 (On X frames, there is only one glyph produced for a
7245 composite character.)
7247 The behavior implemented below means, for continuation lines,
7248 that as many spaces of a TAB as fit on the current line are
7249 displayed there. For terminal frames, as many glyphs of a
7250 multi-glyph character are displayed in the current line, too.
7251 This is what the old redisplay code did, and we keep it that
7252 way. Under X, the whole shape of a complex character must
7253 fit on the line or it will be completely displayed in the
7254 next line.
7256 Note that both for tabs and padding glyphs, all glyphs have
7257 the same width. */
7258 if (it->nglyphs)
7260 /* More than one glyph or glyph doesn't fit on line. All
7261 glyphs have the same width. */
7262 int single_glyph_width = it->pixel_width / it->nglyphs;
7263 int new_x;
7264 int x_before_this_char = x;
7265 int hpos_before_this_char = it->hpos;
7267 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7269 new_x = x + single_glyph_width;
7271 /* We want to leave anything reaching TO_X to the caller. */
7272 if ((op & MOVE_TO_X) && new_x > to_x)
7274 if (BUFFER_POS_REACHED_P ())
7276 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7277 goto buffer_pos_reached;
7278 if (atpos_it.sp < 0)
7280 atpos_it = *it;
7281 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7284 else
7286 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7288 it->current_x = x;
7289 result = MOVE_X_REACHED;
7290 break;
7292 if (atx_it.sp < 0)
7294 atx_it = *it;
7295 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7300 if (/* Lines are continued. */
7301 it->line_wrap != TRUNCATE
7302 && (/* And glyph doesn't fit on the line. */
7303 new_x > it->last_visible_x
7304 /* Or it fits exactly and we're on a window
7305 system frame. */
7306 || (new_x == it->last_visible_x
7307 && FRAME_WINDOW_P (it->f))))
7309 if (/* IT->hpos == 0 means the very first glyph
7310 doesn't fit on the line, e.g. a wide image. */
7311 it->hpos == 0
7312 || (new_x == it->last_visible_x
7313 && FRAME_WINDOW_P (it->f)))
7315 ++it->hpos;
7316 it->current_x = new_x;
7318 /* The character's last glyph just barely fits
7319 in this row. */
7320 if (i == it->nglyphs - 1)
7322 /* If this is the destination position,
7323 return a position *before* it in this row,
7324 now that we know it fits in this row. */
7325 if (BUFFER_POS_REACHED_P ())
7327 if (it->line_wrap != WORD_WRAP
7328 || wrap_it.sp < 0)
7330 it->hpos = hpos_before_this_char;
7331 it->current_x = x_before_this_char;
7332 result = MOVE_POS_MATCH_OR_ZV;
7333 break;
7335 if (it->line_wrap == WORD_WRAP
7336 && atpos_it.sp < 0)
7338 atpos_it = *it;
7339 atpos_it.current_x = x_before_this_char;
7340 atpos_it.hpos = hpos_before_this_char;
7344 set_iterator_to_next (it, 1);
7345 /* On graphical terminals, newlines may
7346 "overflow" into the fringe if
7347 overflow-newline-into-fringe is non-nil.
7348 On text-only terminals, newlines may
7349 overflow into the last glyph on the
7350 display line.*/
7351 if (!FRAME_WINDOW_P (it->f)
7352 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7354 if (!get_next_display_element (it))
7356 result = MOVE_POS_MATCH_OR_ZV;
7357 break;
7359 if (BUFFER_POS_REACHED_P ())
7361 if (ITERATOR_AT_END_OF_LINE_P (it))
7362 result = MOVE_POS_MATCH_OR_ZV;
7363 else
7364 result = MOVE_LINE_CONTINUED;
7365 break;
7367 if (ITERATOR_AT_END_OF_LINE_P (it))
7369 result = MOVE_NEWLINE_OR_CR;
7370 break;
7375 else
7376 IT_RESET_X_ASCENT_DESCENT (it);
7378 if (wrap_it.sp >= 0)
7380 *it = wrap_it;
7381 atpos_it.sp = -1;
7382 atx_it.sp = -1;
7385 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7386 IT_CHARPOS (*it)));
7387 result = MOVE_LINE_CONTINUED;
7388 break;
7391 if (BUFFER_POS_REACHED_P ())
7393 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7394 goto buffer_pos_reached;
7395 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7397 atpos_it = *it;
7398 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7402 if (new_x > it->first_visible_x)
7404 /* Glyph is visible. Increment number of glyphs that
7405 would be displayed. */
7406 ++it->hpos;
7410 if (result != MOVE_UNDEFINED)
7411 break;
7413 else if (BUFFER_POS_REACHED_P ())
7415 buffer_pos_reached:
7416 IT_RESET_X_ASCENT_DESCENT (it);
7417 result = MOVE_POS_MATCH_OR_ZV;
7418 break;
7420 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7422 /* Stop when TO_X specified and reached. This check is
7423 necessary here because of lines consisting of a line end,
7424 only. The line end will not produce any glyphs and we
7425 would never get MOVE_X_REACHED. */
7426 xassert (it->nglyphs == 0);
7427 result = MOVE_X_REACHED;
7428 break;
7431 /* Is this a line end? If yes, we're done. */
7432 if (ITERATOR_AT_END_OF_LINE_P (it))
7434 result = MOVE_NEWLINE_OR_CR;
7435 break;
7438 if (it->method == GET_FROM_BUFFER)
7439 prev_pos = IT_CHARPOS (*it);
7440 /* The current display element has been consumed. Advance
7441 to the next. */
7442 set_iterator_to_next (it, 1);
7444 /* Stop if lines are truncated and IT's current x-position is
7445 past the right edge of the window now. */
7446 if (it->line_wrap == TRUNCATE
7447 && it->current_x >= it->last_visible_x)
7449 if (!FRAME_WINDOW_P (it->f)
7450 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7452 if (!get_next_display_element (it)
7453 || BUFFER_POS_REACHED_P ())
7455 result = MOVE_POS_MATCH_OR_ZV;
7456 break;
7458 if (ITERATOR_AT_END_OF_LINE_P (it))
7460 result = MOVE_NEWLINE_OR_CR;
7461 break;
7464 result = MOVE_LINE_TRUNCATED;
7465 break;
7467 #undef IT_RESET_X_ASCENT_DESCENT
7470 #undef BUFFER_POS_REACHED_P
7472 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7473 restore the saved iterator. */
7474 if (atpos_it.sp >= 0)
7475 *it = atpos_it;
7476 else if (atx_it.sp >= 0)
7477 *it = atx_it;
7479 done:
7481 /* Restore the iterator settings altered at the beginning of this
7482 function. */
7483 it->glyph_row = saved_glyph_row;
7484 return result;
7487 /* For external use. */
7488 void
7489 move_it_in_display_line (struct it *it,
7490 EMACS_INT to_charpos, int to_x,
7491 enum move_operation_enum op)
7493 if (it->line_wrap == WORD_WRAP
7494 && (op & MOVE_TO_X))
7496 struct it save_it = *it;
7497 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7498 /* When word-wrap is on, TO_X may lie past the end
7499 of a wrapped line. Then it->current is the
7500 character on the next line, so backtrack to the
7501 space before the wrap point. */
7502 if (skip == MOVE_LINE_CONTINUED)
7504 int prev_x = max (it->current_x - 1, 0);
7505 *it = save_it;
7506 move_it_in_display_line_to
7507 (it, -1, prev_x, MOVE_TO_X);
7510 else
7511 move_it_in_display_line_to (it, to_charpos, to_x, op);
7515 /* Move IT forward until it satisfies one or more of the criteria in
7516 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7518 OP is a bit-mask that specifies where to stop, and in particular,
7519 which of those four position arguments makes a difference. See the
7520 description of enum move_operation_enum.
7522 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7523 screen line, this function will set IT to the next position >
7524 TO_CHARPOS. */
7526 void
7527 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7529 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7530 int line_height, line_start_x = 0, reached = 0;
7532 for (;;)
7534 if (op & MOVE_TO_VPOS)
7536 /* If no TO_CHARPOS and no TO_X specified, stop at the
7537 start of the line TO_VPOS. */
7538 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7540 if (it->vpos == to_vpos)
7542 reached = 1;
7543 break;
7545 else
7546 skip = move_it_in_display_line_to (it, -1, -1, 0);
7548 else
7550 /* TO_VPOS >= 0 means stop at TO_X in the line at
7551 TO_VPOS, or at TO_POS, whichever comes first. */
7552 if (it->vpos == to_vpos)
7554 reached = 2;
7555 break;
7558 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7560 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7562 reached = 3;
7563 break;
7565 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7567 /* We have reached TO_X but not in the line we want. */
7568 skip = move_it_in_display_line_to (it, to_charpos,
7569 -1, MOVE_TO_POS);
7570 if (skip == MOVE_POS_MATCH_OR_ZV)
7572 reached = 4;
7573 break;
7578 else if (op & MOVE_TO_Y)
7580 struct it it_backup;
7582 if (it->line_wrap == WORD_WRAP)
7583 it_backup = *it;
7585 /* TO_Y specified means stop at TO_X in the line containing
7586 TO_Y---or at TO_CHARPOS if this is reached first. The
7587 problem is that we can't really tell whether the line
7588 contains TO_Y before we have completely scanned it, and
7589 this may skip past TO_X. What we do is to first scan to
7590 TO_X.
7592 If TO_X is not specified, use a TO_X of zero. The reason
7593 is to make the outcome of this function more predictable.
7594 If we didn't use TO_X == 0, we would stop at the end of
7595 the line which is probably not what a caller would expect
7596 to happen. */
7597 skip = move_it_in_display_line_to
7598 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7599 (MOVE_TO_X | (op & MOVE_TO_POS)));
7601 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7602 if (skip == MOVE_POS_MATCH_OR_ZV)
7603 reached = 5;
7604 else if (skip == MOVE_X_REACHED)
7606 /* If TO_X was reached, we want to know whether TO_Y is
7607 in the line. We know this is the case if the already
7608 scanned glyphs make the line tall enough. Otherwise,
7609 we must check by scanning the rest of the line. */
7610 line_height = it->max_ascent + it->max_descent;
7611 if (to_y >= it->current_y
7612 && to_y < it->current_y + line_height)
7614 reached = 6;
7615 break;
7617 it_backup = *it;
7618 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7619 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7620 op & MOVE_TO_POS);
7621 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7622 line_height = it->max_ascent + it->max_descent;
7623 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7625 if (to_y >= it->current_y
7626 && to_y < it->current_y + line_height)
7628 /* If TO_Y is in this line and TO_X was reached
7629 above, we scanned too far. We have to restore
7630 IT's settings to the ones before skipping. */
7631 *it = it_backup;
7632 reached = 6;
7634 else
7636 skip = skip2;
7637 if (skip == MOVE_POS_MATCH_OR_ZV)
7638 reached = 7;
7641 else
7643 /* Check whether TO_Y is in this line. */
7644 line_height = it->max_ascent + it->max_descent;
7645 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7647 if (to_y >= it->current_y
7648 && to_y < it->current_y + line_height)
7650 /* When word-wrap is on, TO_X may lie past the end
7651 of a wrapped line. Then it->current is the
7652 character on the next line, so backtrack to the
7653 space before the wrap point. */
7654 if (skip == MOVE_LINE_CONTINUED
7655 && it->line_wrap == WORD_WRAP)
7657 int prev_x = max (it->current_x - 1, 0);
7658 *it = it_backup;
7659 skip = move_it_in_display_line_to
7660 (it, -1, prev_x, MOVE_TO_X);
7662 reached = 6;
7666 if (reached)
7667 break;
7669 else if (BUFFERP (it->object)
7670 && (it->method == GET_FROM_BUFFER
7671 || it->method == GET_FROM_STRETCH)
7672 && IT_CHARPOS (*it) >= to_charpos)
7673 skip = MOVE_POS_MATCH_OR_ZV;
7674 else
7675 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7677 switch (skip)
7679 case MOVE_POS_MATCH_OR_ZV:
7680 reached = 8;
7681 goto out;
7683 case MOVE_NEWLINE_OR_CR:
7684 set_iterator_to_next (it, 1);
7685 it->continuation_lines_width = 0;
7686 break;
7688 case MOVE_LINE_TRUNCATED:
7689 it->continuation_lines_width = 0;
7690 reseat_at_next_visible_line_start (it, 0);
7691 if ((op & MOVE_TO_POS) != 0
7692 && IT_CHARPOS (*it) > to_charpos)
7694 reached = 9;
7695 goto out;
7697 break;
7699 case MOVE_LINE_CONTINUED:
7700 /* For continued lines ending in a tab, some of the glyphs
7701 associated with the tab are displayed on the current
7702 line. Since it->current_x does not include these glyphs,
7703 we use it->last_visible_x instead. */
7704 if (it->c == '\t')
7706 it->continuation_lines_width += it->last_visible_x;
7707 /* When moving by vpos, ensure that the iterator really
7708 advances to the next line (bug#847, bug#969). Fixme:
7709 do we need to do this in other circumstances? */
7710 if (it->current_x != it->last_visible_x
7711 && (op & MOVE_TO_VPOS)
7712 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7714 line_start_x = it->current_x + it->pixel_width
7715 - it->last_visible_x;
7716 set_iterator_to_next (it, 0);
7719 else
7720 it->continuation_lines_width += it->current_x;
7721 break;
7723 default:
7724 abort ();
7727 /* Reset/increment for the next run. */
7728 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7729 it->current_x = line_start_x;
7730 line_start_x = 0;
7731 it->hpos = 0;
7732 it->current_y += it->max_ascent + it->max_descent;
7733 ++it->vpos;
7734 last_height = it->max_ascent + it->max_descent;
7735 last_max_ascent = it->max_ascent;
7736 it->max_ascent = it->max_descent = 0;
7739 out:
7741 /* On text terminals, we may stop at the end of a line in the middle
7742 of a multi-character glyph. If the glyph itself is continued,
7743 i.e. it is actually displayed on the next line, don't treat this
7744 stopping point as valid; move to the next line instead (unless
7745 that brings us offscreen). */
7746 if (!FRAME_WINDOW_P (it->f)
7747 && op & MOVE_TO_POS
7748 && IT_CHARPOS (*it) == to_charpos
7749 && it->what == IT_CHARACTER
7750 && it->nglyphs > 1
7751 && it->line_wrap == WINDOW_WRAP
7752 && it->current_x == it->last_visible_x - 1
7753 && it->c != '\n'
7754 && it->c != '\t'
7755 && it->vpos < XFASTINT (it->w->window_end_vpos))
7757 it->continuation_lines_width += it->current_x;
7758 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7759 it->current_y += it->max_ascent + it->max_descent;
7760 ++it->vpos;
7761 last_height = it->max_ascent + it->max_descent;
7762 last_max_ascent = it->max_ascent;
7765 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7769 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7771 If DY > 0, move IT backward at least that many pixels. DY = 0
7772 means move IT backward to the preceding line start or BEGV. This
7773 function may move over more than DY pixels if IT->current_y - DY
7774 ends up in the middle of a line; in this case IT->current_y will be
7775 set to the top of the line moved to. */
7777 void
7778 move_it_vertically_backward (struct it *it, int dy)
7780 int nlines, h;
7781 struct it it2, it3;
7782 EMACS_INT start_pos;
7784 move_further_back:
7785 xassert (dy >= 0);
7787 start_pos = IT_CHARPOS (*it);
7789 /* Estimate how many newlines we must move back. */
7790 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7792 /* Set the iterator's position that many lines back. */
7793 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7794 back_to_previous_visible_line_start (it);
7796 /* Reseat the iterator here. When moving backward, we don't want
7797 reseat to skip forward over invisible text, set up the iterator
7798 to deliver from overlay strings at the new position etc. So,
7799 use reseat_1 here. */
7800 reseat_1 (it, it->current.pos, 1);
7802 /* We are now surely at a line start. */
7803 it->current_x = it->hpos = 0;
7804 it->continuation_lines_width = 0;
7806 /* Move forward and see what y-distance we moved. First move to the
7807 start of the next line so that we get its height. We need this
7808 height to be able to tell whether we reached the specified
7809 y-distance. */
7810 it2 = *it;
7811 it2.max_ascent = it2.max_descent = 0;
7814 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7815 MOVE_TO_POS | MOVE_TO_VPOS);
7817 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7818 xassert (IT_CHARPOS (*it) >= BEGV);
7819 it3 = it2;
7821 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7822 xassert (IT_CHARPOS (*it) >= BEGV);
7823 /* H is the actual vertical distance from the position in *IT
7824 and the starting position. */
7825 h = it2.current_y - it->current_y;
7826 /* NLINES is the distance in number of lines. */
7827 nlines = it2.vpos - it->vpos;
7829 /* Correct IT's y and vpos position
7830 so that they are relative to the starting point. */
7831 it->vpos -= nlines;
7832 it->current_y -= h;
7834 if (dy == 0)
7836 /* DY == 0 means move to the start of the screen line. The
7837 value of nlines is > 0 if continuation lines were involved. */
7838 if (nlines > 0)
7839 move_it_by_lines (it, nlines, 1);
7841 else
7843 /* The y-position we try to reach, relative to *IT.
7844 Note that H has been subtracted in front of the if-statement. */
7845 int target_y = it->current_y + h - dy;
7846 int y0 = it3.current_y;
7847 int y1 = line_bottom_y (&it3);
7848 int line_height = y1 - y0;
7850 /* If we did not reach target_y, try to move further backward if
7851 we can. If we moved too far backward, try to move forward. */
7852 if (target_y < it->current_y
7853 /* This is heuristic. In a window that's 3 lines high, with
7854 a line height of 13 pixels each, recentering with point
7855 on the bottom line will try to move -39/2 = 19 pixels
7856 backward. Try to avoid moving into the first line. */
7857 && (it->current_y - target_y
7858 > min (window_box_height (it->w), line_height * 2 / 3))
7859 && IT_CHARPOS (*it) > BEGV)
7861 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7862 target_y - it->current_y));
7863 dy = it->current_y - target_y;
7864 goto move_further_back;
7866 else if (target_y >= it->current_y + line_height
7867 && IT_CHARPOS (*it) < ZV)
7869 /* Should move forward by at least one line, maybe more.
7871 Note: Calling move_it_by_lines can be expensive on
7872 terminal frames, where compute_motion is used (via
7873 vmotion) to do the job, when there are very long lines
7874 and truncate-lines is nil. That's the reason for
7875 treating terminal frames specially here. */
7877 if (!FRAME_WINDOW_P (it->f))
7878 move_it_vertically (it, target_y - (it->current_y + line_height));
7879 else
7883 move_it_by_lines (it, 1, 1);
7885 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7892 /* Move IT by a specified amount of pixel lines DY. DY negative means
7893 move backwards. DY = 0 means move to start of screen line. At the
7894 end, IT will be on the start of a screen line. */
7896 void
7897 move_it_vertically (struct it *it, int dy)
7899 if (dy <= 0)
7900 move_it_vertically_backward (it, -dy);
7901 else
7903 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7904 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7905 MOVE_TO_POS | MOVE_TO_Y);
7906 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7908 /* If buffer ends in ZV without a newline, move to the start of
7909 the line to satisfy the post-condition. */
7910 if (IT_CHARPOS (*it) == ZV
7911 && ZV > BEGV
7912 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7913 move_it_by_lines (it, 0, 0);
7918 /* Move iterator IT past the end of the text line it is in. */
7920 void
7921 move_it_past_eol (struct it *it)
7923 enum move_it_result rc;
7925 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7926 if (rc == MOVE_NEWLINE_OR_CR)
7927 set_iterator_to_next (it, 0);
7931 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7932 negative means move up. DVPOS == 0 means move to the start of the
7933 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7934 NEED_Y_P is zero, IT->current_y will be left unchanged.
7936 Further optimization ideas: If we would know that IT->f doesn't use
7937 a face with proportional font, we could be faster for
7938 truncate-lines nil. */
7940 void
7941 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7944 /* The commented-out optimization uses vmotion on terminals. This
7945 gives bad results, because elements like it->what, on which
7946 callers such as pos_visible_p rely, aren't updated. */
7947 /* struct position pos;
7948 if (!FRAME_WINDOW_P (it->f))
7950 struct text_pos textpos;
7952 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7953 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7954 reseat (it, textpos, 1);
7955 it->vpos += pos.vpos;
7956 it->current_y += pos.vpos;
7958 else */
7960 if (dvpos == 0)
7962 /* DVPOS == 0 means move to the start of the screen line. */
7963 move_it_vertically_backward (it, 0);
7964 xassert (it->current_x == 0 && it->hpos == 0);
7965 /* Let next call to line_bottom_y calculate real line height */
7966 last_height = 0;
7968 else if (dvpos > 0)
7970 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7971 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7972 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7974 else
7976 struct it it2;
7977 EMACS_INT start_charpos, i;
7979 /* Start at the beginning of the screen line containing IT's
7980 position. This may actually move vertically backwards,
7981 in case of overlays, so adjust dvpos accordingly. */
7982 dvpos += it->vpos;
7983 move_it_vertically_backward (it, 0);
7984 dvpos -= it->vpos;
7986 /* Go back -DVPOS visible lines and reseat the iterator there. */
7987 start_charpos = IT_CHARPOS (*it);
7988 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7989 back_to_previous_visible_line_start (it);
7990 reseat (it, it->current.pos, 1);
7992 /* Move further back if we end up in a string or an image. */
7993 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7995 /* First try to move to start of display line. */
7996 dvpos += it->vpos;
7997 move_it_vertically_backward (it, 0);
7998 dvpos -= it->vpos;
7999 if (IT_POS_VALID_AFTER_MOVE_P (it))
8000 break;
8001 /* If start of line is still in string or image,
8002 move further back. */
8003 back_to_previous_visible_line_start (it);
8004 reseat (it, it->current.pos, 1);
8005 dvpos--;
8008 it->current_x = it->hpos = 0;
8010 /* Above call may have moved too far if continuation lines
8011 are involved. Scan forward and see if it did. */
8012 it2 = *it;
8013 it2.vpos = it2.current_y = 0;
8014 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8015 it->vpos -= it2.vpos;
8016 it->current_y -= it2.current_y;
8017 it->current_x = it->hpos = 0;
8019 /* If we moved too far back, move IT some lines forward. */
8020 if (it2.vpos > -dvpos)
8022 int delta = it2.vpos + dvpos;
8023 it2 = *it;
8024 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8025 /* Move back again if we got too far ahead. */
8026 if (IT_CHARPOS (*it) >= start_charpos)
8027 *it = it2;
8032 /* Return 1 if IT points into the middle of a display vector. */
8035 in_display_vector_p (struct it *it)
8037 return (it->method == GET_FROM_DISPLAY_VECTOR
8038 && it->current.dpvec_index > 0
8039 && it->dpvec + it->current.dpvec_index != it->dpend);
8043 /***********************************************************************
8044 Messages
8045 ***********************************************************************/
8048 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8049 to *Messages*. */
8051 void
8052 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8054 Lisp_Object args[3];
8055 Lisp_Object msg, fmt;
8056 char *buffer;
8057 EMACS_INT len;
8058 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8059 USE_SAFE_ALLOCA;
8061 /* Do nothing if called asynchronously. Inserting text into
8062 a buffer may call after-change-functions and alike and
8063 that would means running Lisp asynchronously. */
8064 if (handling_signal)
8065 return;
8067 fmt = msg = Qnil;
8068 GCPRO4 (fmt, msg, arg1, arg2);
8070 args[0] = fmt = build_string (format);
8071 args[1] = arg1;
8072 args[2] = arg2;
8073 msg = Fformat (3, args);
8075 len = SBYTES (msg) + 1;
8076 SAFE_ALLOCA (buffer, char *, len);
8077 memcpy (buffer, SDATA (msg), len);
8079 message_dolog (buffer, len - 1, 1, 0);
8080 SAFE_FREE ();
8082 UNGCPRO;
8086 /* Output a newline in the *Messages* buffer if "needs" one. */
8088 void
8089 message_log_maybe_newline (void)
8091 if (message_log_need_newline)
8092 message_dolog ("", 0, 1, 0);
8096 /* Add a string M of length NBYTES to the message log, optionally
8097 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8098 nonzero, means interpret the contents of M as multibyte. This
8099 function calls low-level routines in order to bypass text property
8100 hooks, etc. which might not be safe to run.
8102 This may GC (insert may run before/after change hooks),
8103 so the buffer M must NOT point to a Lisp string. */
8105 void
8106 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8108 if (!NILP (Vmemory_full))
8109 return;
8111 if (!NILP (Vmessage_log_max))
8113 struct buffer *oldbuf;
8114 Lisp_Object oldpoint, oldbegv, oldzv;
8115 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8116 EMACS_INT point_at_end = 0;
8117 EMACS_INT zv_at_end = 0;
8118 Lisp_Object old_deactivate_mark, tem;
8119 struct gcpro gcpro1;
8121 old_deactivate_mark = Vdeactivate_mark;
8122 oldbuf = current_buffer;
8123 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8124 current_buffer->undo_list = Qt;
8126 oldpoint = message_dolog_marker1;
8127 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8128 oldbegv = message_dolog_marker2;
8129 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8130 oldzv = message_dolog_marker3;
8131 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8132 GCPRO1 (old_deactivate_mark);
8134 if (PT == Z)
8135 point_at_end = 1;
8136 if (ZV == Z)
8137 zv_at_end = 1;
8139 BEGV = BEG;
8140 BEGV_BYTE = BEG_BYTE;
8141 ZV = Z;
8142 ZV_BYTE = Z_BYTE;
8143 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8145 /* Insert the string--maybe converting multibyte to single byte
8146 or vice versa, so that all the text fits the buffer. */
8147 if (multibyte
8148 && NILP (current_buffer->enable_multibyte_characters))
8150 EMACS_INT i;
8151 int c, char_bytes;
8152 unsigned char work[1];
8154 /* Convert a multibyte string to single-byte
8155 for the *Message* buffer. */
8156 for (i = 0; i < nbytes; i += char_bytes)
8158 c = string_char_and_length (m + i, &char_bytes);
8159 work[0] = (ASCII_CHAR_P (c)
8161 : multibyte_char_to_unibyte (c, Qnil));
8162 insert_1_both (work, 1, 1, 1, 0, 0);
8165 else if (! multibyte
8166 && ! NILP (current_buffer->enable_multibyte_characters))
8168 EMACS_INT i;
8169 int c, char_bytes;
8170 unsigned char *msg = (unsigned char *) m;
8171 unsigned char str[MAX_MULTIBYTE_LENGTH];
8172 /* Convert a single-byte string to multibyte
8173 for the *Message* buffer. */
8174 for (i = 0; i < nbytes; i++)
8176 c = msg[i];
8177 MAKE_CHAR_MULTIBYTE (c);
8178 char_bytes = CHAR_STRING (c, str);
8179 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8182 else if (nbytes)
8183 insert_1 (m, nbytes, 1, 0, 0);
8185 if (nlflag)
8187 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8188 int dup;
8189 insert_1 ("\n", 1, 1, 0, 0);
8191 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8192 this_bol = PT;
8193 this_bol_byte = PT_BYTE;
8195 /* See if this line duplicates the previous one.
8196 If so, combine duplicates. */
8197 if (this_bol > BEG)
8199 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8200 prev_bol = PT;
8201 prev_bol_byte = PT_BYTE;
8203 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8204 this_bol, this_bol_byte);
8205 if (dup)
8207 del_range_both (prev_bol, prev_bol_byte,
8208 this_bol, this_bol_byte, 0);
8209 if (dup > 1)
8211 char dupstr[40];
8212 int duplen;
8214 /* If you change this format, don't forget to also
8215 change message_log_check_duplicate. */
8216 sprintf (dupstr, " [%d times]", dup);
8217 duplen = strlen (dupstr);
8218 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8219 insert_1 (dupstr, duplen, 1, 0, 1);
8224 /* If we have more than the desired maximum number of lines
8225 in the *Messages* buffer now, delete the oldest ones.
8226 This is safe because we don't have undo in this buffer. */
8228 if (NATNUMP (Vmessage_log_max))
8230 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8231 -XFASTINT (Vmessage_log_max) - 1, 0);
8232 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8235 BEGV = XMARKER (oldbegv)->charpos;
8236 BEGV_BYTE = marker_byte_position (oldbegv);
8238 if (zv_at_end)
8240 ZV = Z;
8241 ZV_BYTE = Z_BYTE;
8243 else
8245 ZV = XMARKER (oldzv)->charpos;
8246 ZV_BYTE = marker_byte_position (oldzv);
8249 if (point_at_end)
8250 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8251 else
8252 /* We can't do Fgoto_char (oldpoint) because it will run some
8253 Lisp code. */
8254 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8255 XMARKER (oldpoint)->bytepos);
8257 UNGCPRO;
8258 unchain_marker (XMARKER (oldpoint));
8259 unchain_marker (XMARKER (oldbegv));
8260 unchain_marker (XMARKER (oldzv));
8262 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8263 set_buffer_internal (oldbuf);
8264 if (NILP (tem))
8265 windows_or_buffers_changed = old_windows_or_buffers_changed;
8266 message_log_need_newline = !nlflag;
8267 Vdeactivate_mark = old_deactivate_mark;
8272 /* We are at the end of the buffer after just having inserted a newline.
8273 (Note: We depend on the fact we won't be crossing the gap.)
8274 Check to see if the most recent message looks a lot like the previous one.
8275 Return 0 if different, 1 if the new one should just replace it, or a
8276 value N > 1 if we should also append " [N times]". */
8278 static int
8279 message_log_check_duplicate (EMACS_INT prev_bol, EMACS_INT prev_bol_byte,
8280 EMACS_INT this_bol, EMACS_INT this_bol_byte)
8282 EMACS_INT i;
8283 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8284 int seen_dots = 0;
8285 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8286 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8288 for (i = 0; i < len; i++)
8290 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8291 seen_dots = 1;
8292 if (p1[i] != p2[i])
8293 return seen_dots;
8295 p1 += len;
8296 if (*p1 == '\n')
8297 return 2;
8298 if (*p1++ == ' ' && *p1++ == '[')
8300 int n = 0;
8301 while (*p1 >= '0' && *p1 <= '9')
8302 n = n * 10 + *p1++ - '0';
8303 if (strncmp (p1, " times]\n", 8) == 0)
8304 return n+1;
8306 return 0;
8310 /* Display an echo area message M with a specified length of NBYTES
8311 bytes. The string may include null characters. If M is 0, clear
8312 out any existing message, and let the mini-buffer text show
8313 through.
8315 This may GC, so the buffer M must NOT point to a Lisp string. */
8317 void
8318 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8320 /* First flush out any partial line written with print. */
8321 message_log_maybe_newline ();
8322 if (m)
8323 message_dolog (m, nbytes, 1, multibyte);
8324 message2_nolog (m, nbytes, multibyte);
8328 /* The non-logging counterpart of message2. */
8330 void
8331 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8333 struct frame *sf = SELECTED_FRAME ();
8334 message_enable_multibyte = multibyte;
8336 if (FRAME_INITIAL_P (sf))
8338 if (noninteractive_need_newline)
8339 putc ('\n', stderr);
8340 noninteractive_need_newline = 0;
8341 if (m)
8342 fwrite (m, nbytes, 1, stderr);
8343 if (cursor_in_echo_area == 0)
8344 fprintf (stderr, "\n");
8345 fflush (stderr);
8347 /* A null message buffer means that the frame hasn't really been
8348 initialized yet. Error messages get reported properly by
8349 cmd_error, so this must be just an informative message; toss it. */
8350 else if (INTERACTIVE
8351 && sf->glyphs_initialized_p
8352 && FRAME_MESSAGE_BUF (sf))
8354 Lisp_Object mini_window;
8355 struct frame *f;
8357 /* Get the frame containing the mini-buffer
8358 that the selected frame is using. */
8359 mini_window = FRAME_MINIBUF_WINDOW (sf);
8360 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8362 FRAME_SAMPLE_VISIBILITY (f);
8363 if (FRAME_VISIBLE_P (sf)
8364 && ! FRAME_VISIBLE_P (f))
8365 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8367 if (m)
8369 set_message (m, Qnil, nbytes, multibyte);
8370 if (minibuffer_auto_raise)
8371 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8373 else
8374 clear_message (1, 1);
8376 do_pending_window_change (0);
8377 echo_area_display (1);
8378 do_pending_window_change (0);
8379 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8380 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8385 /* Display an echo area message M with a specified length of NBYTES
8386 bytes. The string may include null characters. If M is not a
8387 string, clear out any existing message, and let the mini-buffer
8388 text show through.
8390 This function cancels echoing. */
8392 void
8393 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8395 struct gcpro gcpro1;
8397 GCPRO1 (m);
8398 clear_message (1,1);
8399 cancel_echoing ();
8401 /* First flush out any partial line written with print. */
8402 message_log_maybe_newline ();
8403 if (STRINGP (m))
8405 char *buffer;
8406 USE_SAFE_ALLOCA;
8408 SAFE_ALLOCA (buffer, char *, nbytes);
8409 memcpy (buffer, SDATA (m), nbytes);
8410 message_dolog (buffer, nbytes, 1, multibyte);
8411 SAFE_FREE ();
8413 message3_nolog (m, nbytes, multibyte);
8415 UNGCPRO;
8419 /* The non-logging version of message3.
8420 This does not cancel echoing, because it is used for echoing.
8421 Perhaps we need to make a separate function for echoing
8422 and make this cancel echoing. */
8424 void
8425 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8427 struct frame *sf = SELECTED_FRAME ();
8428 message_enable_multibyte = multibyte;
8430 if (FRAME_INITIAL_P (sf))
8432 if (noninteractive_need_newline)
8433 putc ('\n', stderr);
8434 noninteractive_need_newline = 0;
8435 if (STRINGP (m))
8436 fwrite (SDATA (m), nbytes, 1, stderr);
8437 if (cursor_in_echo_area == 0)
8438 fprintf (stderr, "\n");
8439 fflush (stderr);
8441 /* A null message buffer means that the frame hasn't really been
8442 initialized yet. Error messages get reported properly by
8443 cmd_error, so this must be just an informative message; toss it. */
8444 else if (INTERACTIVE
8445 && sf->glyphs_initialized_p
8446 && FRAME_MESSAGE_BUF (sf))
8448 Lisp_Object mini_window;
8449 Lisp_Object frame;
8450 struct frame *f;
8452 /* Get the frame containing the mini-buffer
8453 that the selected frame is using. */
8454 mini_window = FRAME_MINIBUF_WINDOW (sf);
8455 frame = XWINDOW (mini_window)->frame;
8456 f = XFRAME (frame);
8458 FRAME_SAMPLE_VISIBILITY (f);
8459 if (FRAME_VISIBLE_P (sf)
8460 && !FRAME_VISIBLE_P (f))
8461 Fmake_frame_visible (frame);
8463 if (STRINGP (m) && SCHARS (m) > 0)
8465 set_message (NULL, m, nbytes, multibyte);
8466 if (minibuffer_auto_raise)
8467 Fraise_frame (frame);
8468 /* Assume we are not echoing.
8469 (If we are, echo_now will override this.) */
8470 echo_message_buffer = Qnil;
8472 else
8473 clear_message (1, 1);
8475 do_pending_window_change (0);
8476 echo_area_display (1);
8477 do_pending_window_change (0);
8478 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8479 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8484 /* Display a null-terminated echo area message M. If M is 0, clear
8485 out any existing message, and let the mini-buffer text show through.
8487 The buffer M must continue to exist until after the echo area gets
8488 cleared or some other message gets displayed there. Do not pass
8489 text that is stored in a Lisp string. Do not pass text in a buffer
8490 that was alloca'd. */
8492 void
8493 message1 (const char *m)
8495 message2 (m, (m ? strlen (m) : 0), 0);
8499 /* The non-logging counterpart of message1. */
8501 void
8502 message1_nolog (const char *m)
8504 message2_nolog (m, (m ? strlen (m) : 0), 0);
8507 /* Display a message M which contains a single %s
8508 which gets replaced with STRING. */
8510 void
8511 message_with_string (const char *m, Lisp_Object string, int log)
8513 CHECK_STRING (string);
8515 if (noninteractive)
8517 if (m)
8519 if (noninteractive_need_newline)
8520 putc ('\n', stderr);
8521 noninteractive_need_newline = 0;
8522 fprintf (stderr, m, SDATA (string));
8523 if (!cursor_in_echo_area)
8524 fprintf (stderr, "\n");
8525 fflush (stderr);
8528 else if (INTERACTIVE)
8530 /* The frame whose minibuffer we're going to display the message on.
8531 It may be larger than the selected frame, so we need
8532 to use its buffer, not the selected frame's buffer. */
8533 Lisp_Object mini_window;
8534 struct frame *f, *sf = SELECTED_FRAME ();
8536 /* Get the frame containing the minibuffer
8537 that the selected frame is using. */
8538 mini_window = FRAME_MINIBUF_WINDOW (sf);
8539 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8541 /* A null message buffer means that the frame hasn't really been
8542 initialized yet. Error messages get reported properly by
8543 cmd_error, so this must be just an informative message; toss it. */
8544 if (FRAME_MESSAGE_BUF (f))
8546 Lisp_Object args[2], message;
8547 struct gcpro gcpro1, gcpro2;
8549 args[0] = build_string (m);
8550 args[1] = message = string;
8551 GCPRO2 (args[0], message);
8552 gcpro1.nvars = 2;
8554 message = Fformat (2, args);
8556 if (log)
8557 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8558 else
8559 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8561 UNGCPRO;
8563 /* Print should start at the beginning of the message
8564 buffer next time. */
8565 message_buf_print = 0;
8571 /* Dump an informative message to the minibuf. If M is 0, clear out
8572 any existing message, and let the mini-buffer text show through. */
8574 static void
8575 vmessage (const char *m, va_list ap)
8577 if (noninteractive)
8579 if (m)
8581 if (noninteractive_need_newline)
8582 putc ('\n', stderr);
8583 noninteractive_need_newline = 0;
8584 vfprintf (stderr, m, ap);
8585 if (cursor_in_echo_area == 0)
8586 fprintf (stderr, "\n");
8587 fflush (stderr);
8590 else if (INTERACTIVE)
8592 /* The frame whose mini-buffer we're going to display the message
8593 on. It may be larger than the selected frame, so we need to
8594 use its buffer, not the selected frame's buffer. */
8595 Lisp_Object mini_window;
8596 struct frame *f, *sf = SELECTED_FRAME ();
8598 /* Get the frame containing the mini-buffer
8599 that the selected frame is using. */
8600 mini_window = FRAME_MINIBUF_WINDOW (sf);
8601 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8603 /* A null message buffer means that the frame hasn't really been
8604 initialized yet. Error messages get reported properly by
8605 cmd_error, so this must be just an informative message; toss
8606 it. */
8607 if (FRAME_MESSAGE_BUF (f))
8609 if (m)
8611 EMACS_INT len;
8613 len = doprnt (FRAME_MESSAGE_BUF (f),
8614 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8616 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8618 else
8619 message1 (0);
8621 /* Print should start at the beginning of the message
8622 buffer next time. */
8623 message_buf_print = 0;
8628 void
8629 message (const char *m, ...)
8631 va_list ap;
8632 va_start (ap, m);
8633 vmessage (m, ap);
8634 va_end (ap);
8638 /* The non-logging version of message. */
8640 void
8641 message_nolog (const char *m, ...)
8643 Lisp_Object old_log_max;
8644 va_list ap;
8645 va_start (ap, m);
8646 old_log_max = Vmessage_log_max;
8647 Vmessage_log_max = Qnil;
8648 vmessage (m, ap);
8649 Vmessage_log_max = old_log_max;
8650 va_end (ap);
8654 /* Display the current message in the current mini-buffer. This is
8655 only called from error handlers in process.c, and is not time
8656 critical. */
8658 void
8659 update_echo_area (void)
8661 if (!NILP (echo_area_buffer[0]))
8663 Lisp_Object string;
8664 string = Fcurrent_message ();
8665 message3 (string, SBYTES (string),
8666 !NILP (current_buffer->enable_multibyte_characters));
8671 /* Make sure echo area buffers in `echo_buffers' are live.
8672 If they aren't, make new ones. */
8674 static void
8675 ensure_echo_area_buffers (void)
8677 int i;
8679 for (i = 0; i < 2; ++i)
8680 if (!BUFFERP (echo_buffer[i])
8681 || NILP (XBUFFER (echo_buffer[i])->name))
8683 char name[30];
8684 Lisp_Object old_buffer;
8685 int j;
8687 old_buffer = echo_buffer[i];
8688 sprintf (name, " *Echo Area %d*", i);
8689 echo_buffer[i] = Fget_buffer_create (build_string (name));
8690 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8691 /* to force word wrap in echo area -
8692 it was decided to postpone this*/
8693 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8695 for (j = 0; j < 2; ++j)
8696 if (EQ (old_buffer, echo_area_buffer[j]))
8697 echo_area_buffer[j] = echo_buffer[i];
8702 /* Call FN with args A1..A4 with either the current or last displayed
8703 echo_area_buffer as current buffer.
8705 WHICH zero means use the current message buffer
8706 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8707 from echo_buffer[] and clear it.
8709 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8710 suitable buffer from echo_buffer[] and clear it.
8712 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8713 that the current message becomes the last displayed one, make
8714 choose a suitable buffer for echo_area_buffer[0], and clear it.
8716 Value is what FN returns. */
8718 static int
8719 with_echo_area_buffer (struct window *w, int which,
8720 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8721 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8723 Lisp_Object buffer;
8724 int this_one, the_other, clear_buffer_p, rc;
8725 int count = SPECPDL_INDEX ();
8727 /* If buffers aren't live, make new ones. */
8728 ensure_echo_area_buffers ();
8730 clear_buffer_p = 0;
8732 if (which == 0)
8733 this_one = 0, the_other = 1;
8734 else if (which > 0)
8735 this_one = 1, the_other = 0;
8736 else
8738 this_one = 0, the_other = 1;
8739 clear_buffer_p = 1;
8741 /* We need a fresh one in case the current echo buffer equals
8742 the one containing the last displayed echo area message. */
8743 if (!NILP (echo_area_buffer[this_one])
8744 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8745 echo_area_buffer[this_one] = Qnil;
8748 /* Choose a suitable buffer from echo_buffer[] is we don't
8749 have one. */
8750 if (NILP (echo_area_buffer[this_one]))
8752 echo_area_buffer[this_one]
8753 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8754 ? echo_buffer[the_other]
8755 : echo_buffer[this_one]);
8756 clear_buffer_p = 1;
8759 buffer = echo_area_buffer[this_one];
8761 /* Don't get confused by reusing the buffer used for echoing
8762 for a different purpose. */
8763 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8764 cancel_echoing ();
8766 record_unwind_protect (unwind_with_echo_area_buffer,
8767 with_echo_area_buffer_unwind_data (w));
8769 /* Make the echo area buffer current. Note that for display
8770 purposes, it is not necessary that the displayed window's buffer
8771 == current_buffer, except for text property lookup. So, let's
8772 only set that buffer temporarily here without doing a full
8773 Fset_window_buffer. We must also change w->pointm, though,
8774 because otherwise an assertions in unshow_buffer fails, and Emacs
8775 aborts. */
8776 set_buffer_internal_1 (XBUFFER (buffer));
8777 if (w)
8779 w->buffer = buffer;
8780 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8783 current_buffer->undo_list = Qt;
8784 current_buffer->read_only = Qnil;
8785 specbind (Qinhibit_read_only, Qt);
8786 specbind (Qinhibit_modification_hooks, Qt);
8788 if (clear_buffer_p && Z > BEG)
8789 del_range (BEG, Z);
8791 xassert (BEGV >= BEG);
8792 xassert (ZV <= Z && ZV >= BEGV);
8794 rc = fn (a1, a2, a3, a4);
8796 xassert (BEGV >= BEG);
8797 xassert (ZV <= Z && ZV >= BEGV);
8799 unbind_to (count, Qnil);
8800 return rc;
8804 /* Save state that should be preserved around the call to the function
8805 FN called in with_echo_area_buffer. */
8807 static Lisp_Object
8808 with_echo_area_buffer_unwind_data (struct window *w)
8810 int i = 0;
8811 Lisp_Object vector, tmp;
8813 /* Reduce consing by keeping one vector in
8814 Vwith_echo_area_save_vector. */
8815 vector = Vwith_echo_area_save_vector;
8816 Vwith_echo_area_save_vector = Qnil;
8818 if (NILP (vector))
8819 vector = Fmake_vector (make_number (7), Qnil);
8821 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8822 ASET (vector, i, Vdeactivate_mark); ++i;
8823 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8825 if (w)
8827 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8828 ASET (vector, i, w->buffer); ++i;
8829 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8830 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8832 else
8834 int end = i + 4;
8835 for (; i < end; ++i)
8836 ASET (vector, i, Qnil);
8839 xassert (i == ASIZE (vector));
8840 return vector;
8844 /* Restore global state from VECTOR which was created by
8845 with_echo_area_buffer_unwind_data. */
8847 static Lisp_Object
8848 unwind_with_echo_area_buffer (Lisp_Object vector)
8850 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8851 Vdeactivate_mark = AREF (vector, 1);
8852 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8854 if (WINDOWP (AREF (vector, 3)))
8856 struct window *w;
8857 Lisp_Object buffer, charpos, bytepos;
8859 w = XWINDOW (AREF (vector, 3));
8860 buffer = AREF (vector, 4);
8861 charpos = AREF (vector, 5);
8862 bytepos = AREF (vector, 6);
8864 w->buffer = buffer;
8865 set_marker_both (w->pointm, buffer,
8866 XFASTINT (charpos), XFASTINT (bytepos));
8869 Vwith_echo_area_save_vector = vector;
8870 return Qnil;
8874 /* Set up the echo area for use by print functions. MULTIBYTE_P
8875 non-zero means we will print multibyte. */
8877 void
8878 setup_echo_area_for_printing (int multibyte_p)
8880 /* If we can't find an echo area any more, exit. */
8881 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8882 Fkill_emacs (Qnil);
8884 ensure_echo_area_buffers ();
8886 if (!message_buf_print)
8888 /* A message has been output since the last time we printed.
8889 Choose a fresh echo area buffer. */
8890 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8891 echo_area_buffer[0] = echo_buffer[1];
8892 else
8893 echo_area_buffer[0] = echo_buffer[0];
8895 /* Switch to that buffer and clear it. */
8896 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8897 current_buffer->truncate_lines = Qnil;
8899 if (Z > BEG)
8901 int count = SPECPDL_INDEX ();
8902 specbind (Qinhibit_read_only, Qt);
8903 /* Note that undo recording is always disabled. */
8904 del_range (BEG, Z);
8905 unbind_to (count, Qnil);
8907 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8909 /* Set up the buffer for the multibyteness we need. */
8910 if (multibyte_p
8911 != !NILP (current_buffer->enable_multibyte_characters))
8912 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8914 /* Raise the frame containing the echo area. */
8915 if (minibuffer_auto_raise)
8917 struct frame *sf = SELECTED_FRAME ();
8918 Lisp_Object mini_window;
8919 mini_window = FRAME_MINIBUF_WINDOW (sf);
8920 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8923 message_log_maybe_newline ();
8924 message_buf_print = 1;
8926 else
8928 if (NILP (echo_area_buffer[0]))
8930 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8931 echo_area_buffer[0] = echo_buffer[1];
8932 else
8933 echo_area_buffer[0] = echo_buffer[0];
8936 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8938 /* Someone switched buffers between print requests. */
8939 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8940 current_buffer->truncate_lines = Qnil;
8946 /* Display an echo area message in window W. Value is non-zero if W's
8947 height is changed. If display_last_displayed_message_p is
8948 non-zero, display the message that was last displayed, otherwise
8949 display the current message. */
8951 static int
8952 display_echo_area (struct window *w)
8954 int i, no_message_p, window_height_changed_p, count;
8956 /* Temporarily disable garbage collections while displaying the echo
8957 area. This is done because a GC can print a message itself.
8958 That message would modify the echo area buffer's contents while a
8959 redisplay of the buffer is going on, and seriously confuse
8960 redisplay. */
8961 count = inhibit_garbage_collection ();
8963 /* If there is no message, we must call display_echo_area_1
8964 nevertheless because it resizes the window. But we will have to
8965 reset the echo_area_buffer in question to nil at the end because
8966 with_echo_area_buffer will sets it to an empty buffer. */
8967 i = display_last_displayed_message_p ? 1 : 0;
8968 no_message_p = NILP (echo_area_buffer[i]);
8970 window_height_changed_p
8971 = with_echo_area_buffer (w, display_last_displayed_message_p,
8972 display_echo_area_1,
8973 (EMACS_INT) w, Qnil, 0, 0);
8975 if (no_message_p)
8976 echo_area_buffer[i] = Qnil;
8978 unbind_to (count, Qnil);
8979 return window_height_changed_p;
8983 /* Helper for display_echo_area. Display the current buffer which
8984 contains the current echo area message in window W, a mini-window,
8985 a pointer to which is passed in A1. A2..A4 are currently not used.
8986 Change the height of W so that all of the message is displayed.
8987 Value is non-zero if height of W was changed. */
8989 static int
8990 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8992 struct window *w = (struct window *) a1;
8993 Lisp_Object window;
8994 struct text_pos start;
8995 int window_height_changed_p = 0;
8997 /* Do this before displaying, so that we have a large enough glyph
8998 matrix for the display. If we can't get enough space for the
8999 whole text, display the last N lines. That works by setting w->start. */
9000 window_height_changed_p = resize_mini_window (w, 0);
9002 /* Use the starting position chosen by resize_mini_window. */
9003 SET_TEXT_POS_FROM_MARKER (start, w->start);
9005 /* Display. */
9006 clear_glyph_matrix (w->desired_matrix);
9007 XSETWINDOW (window, w);
9008 try_window (window, start, 0);
9010 return window_height_changed_p;
9014 /* Resize the echo area window to exactly the size needed for the
9015 currently displayed message, if there is one. If a mini-buffer
9016 is active, don't shrink it. */
9018 void
9019 resize_echo_area_exactly (void)
9021 if (BUFFERP (echo_area_buffer[0])
9022 && WINDOWP (echo_area_window))
9024 struct window *w = XWINDOW (echo_area_window);
9025 int resized_p;
9026 Lisp_Object resize_exactly;
9028 if (minibuf_level == 0)
9029 resize_exactly = Qt;
9030 else
9031 resize_exactly = Qnil;
9033 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9034 (EMACS_INT) w, resize_exactly, 0, 0);
9035 if (resized_p)
9037 ++windows_or_buffers_changed;
9038 ++update_mode_lines;
9039 redisplay_internal (0);
9045 /* Callback function for with_echo_area_buffer, when used from
9046 resize_echo_area_exactly. A1 contains a pointer to the window to
9047 resize, EXACTLY non-nil means resize the mini-window exactly to the
9048 size of the text displayed. A3 and A4 are not used. Value is what
9049 resize_mini_window returns. */
9051 static int
9052 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9054 return resize_mini_window ((struct window *) a1, !NILP (exactly));
9058 /* Resize mini-window W to fit the size of its contents. EXACT_P
9059 means size the window exactly to the size needed. Otherwise, it's
9060 only enlarged until W's buffer is empty.
9062 Set W->start to the right place to begin display. If the whole
9063 contents fit, start at the beginning. Otherwise, start so as
9064 to make the end of the contents appear. This is particularly
9065 important for y-or-n-p, but seems desirable generally.
9067 Value is non-zero if the window height has been changed. */
9070 resize_mini_window (struct window *w, int exact_p)
9072 struct frame *f = XFRAME (w->frame);
9073 int window_height_changed_p = 0;
9075 xassert (MINI_WINDOW_P (w));
9077 /* By default, start display at the beginning. */
9078 set_marker_both (w->start, w->buffer,
9079 BUF_BEGV (XBUFFER (w->buffer)),
9080 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9082 /* Don't resize windows while redisplaying a window; it would
9083 confuse redisplay functions when the size of the window they are
9084 displaying changes from under them. Such a resizing can happen,
9085 for instance, when which-func prints a long message while
9086 we are running fontification-functions. We're running these
9087 functions with safe_call which binds inhibit-redisplay to t. */
9088 if (!NILP (Vinhibit_redisplay))
9089 return 0;
9091 /* Nil means don't try to resize. */
9092 if (NILP (Vresize_mini_windows)
9093 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9094 return 0;
9096 if (!FRAME_MINIBUF_ONLY_P (f))
9098 struct it it;
9099 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9100 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9101 int height, max_height;
9102 int unit = FRAME_LINE_HEIGHT (f);
9103 struct text_pos start;
9104 struct buffer *old_current_buffer = NULL;
9106 if (current_buffer != XBUFFER (w->buffer))
9108 old_current_buffer = current_buffer;
9109 set_buffer_internal (XBUFFER (w->buffer));
9112 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9114 /* Compute the max. number of lines specified by the user. */
9115 if (FLOATP (Vmax_mini_window_height))
9116 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9117 else if (INTEGERP (Vmax_mini_window_height))
9118 max_height = XINT (Vmax_mini_window_height);
9119 else
9120 max_height = total_height / 4;
9122 /* Correct that max. height if it's bogus. */
9123 max_height = max (1, max_height);
9124 max_height = min (total_height, max_height);
9126 /* Find out the height of the text in the window. */
9127 if (it.line_wrap == TRUNCATE)
9128 height = 1;
9129 else
9131 last_height = 0;
9132 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9133 if (it.max_ascent == 0 && it.max_descent == 0)
9134 height = it.current_y + last_height;
9135 else
9136 height = it.current_y + it.max_ascent + it.max_descent;
9137 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9138 height = (height + unit - 1) / unit;
9141 /* Compute a suitable window start. */
9142 if (height > max_height)
9144 height = max_height;
9145 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9146 move_it_vertically_backward (&it, (height - 1) * unit);
9147 start = it.current.pos;
9149 else
9150 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9151 SET_MARKER_FROM_TEXT_POS (w->start, start);
9153 if (EQ (Vresize_mini_windows, Qgrow_only))
9155 /* Let it grow only, until we display an empty message, in which
9156 case the window shrinks again. */
9157 if (height > WINDOW_TOTAL_LINES (w))
9159 int old_height = WINDOW_TOTAL_LINES (w);
9160 freeze_window_starts (f, 1);
9161 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9162 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9164 else if (height < WINDOW_TOTAL_LINES (w)
9165 && (exact_p || BEGV == ZV))
9167 int old_height = WINDOW_TOTAL_LINES (w);
9168 freeze_window_starts (f, 0);
9169 shrink_mini_window (w);
9170 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9173 else
9175 /* Always resize to exact size needed. */
9176 if (height > WINDOW_TOTAL_LINES (w))
9178 int old_height = WINDOW_TOTAL_LINES (w);
9179 freeze_window_starts (f, 1);
9180 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9181 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9183 else if (height < WINDOW_TOTAL_LINES (w))
9185 int old_height = WINDOW_TOTAL_LINES (w);
9186 freeze_window_starts (f, 0);
9187 shrink_mini_window (w);
9189 if (height)
9191 freeze_window_starts (f, 1);
9192 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9195 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9199 if (old_current_buffer)
9200 set_buffer_internal (old_current_buffer);
9203 return window_height_changed_p;
9207 /* Value is the current message, a string, or nil if there is no
9208 current message. */
9210 Lisp_Object
9211 current_message (void)
9213 Lisp_Object msg;
9215 if (!BUFFERP (echo_area_buffer[0]))
9216 msg = Qnil;
9217 else
9219 with_echo_area_buffer (0, 0, current_message_1,
9220 (EMACS_INT) &msg, Qnil, 0, 0);
9221 if (NILP (msg))
9222 echo_area_buffer[0] = Qnil;
9225 return msg;
9229 static int
9230 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9232 Lisp_Object *msg = (Lisp_Object *) a1;
9234 if (Z > BEG)
9235 *msg = make_buffer_string (BEG, Z, 1);
9236 else
9237 *msg = Qnil;
9238 return 0;
9242 /* Push the current message on Vmessage_stack for later restauration
9243 by restore_message. Value is non-zero if the current message isn't
9244 empty. This is a relatively infrequent operation, so it's not
9245 worth optimizing. */
9248 push_message (void)
9250 Lisp_Object msg;
9251 msg = current_message ();
9252 Vmessage_stack = Fcons (msg, Vmessage_stack);
9253 return STRINGP (msg);
9257 /* Restore message display from the top of Vmessage_stack. */
9259 void
9260 restore_message (void)
9262 Lisp_Object msg;
9264 xassert (CONSP (Vmessage_stack));
9265 msg = XCAR (Vmessage_stack);
9266 if (STRINGP (msg))
9267 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9268 else
9269 message3_nolog (msg, 0, 0);
9273 /* Handler for record_unwind_protect calling pop_message. */
9275 Lisp_Object
9276 pop_message_unwind (Lisp_Object dummy)
9278 pop_message ();
9279 return Qnil;
9282 /* Pop the top-most entry off Vmessage_stack. */
9284 void
9285 pop_message (void)
9287 xassert (CONSP (Vmessage_stack));
9288 Vmessage_stack = XCDR (Vmessage_stack);
9292 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9293 exits. If the stack is not empty, we have a missing pop_message
9294 somewhere. */
9296 void
9297 check_message_stack (void)
9299 if (!NILP (Vmessage_stack))
9300 abort ();
9304 /* Truncate to NCHARS what will be displayed in the echo area the next
9305 time we display it---but don't redisplay it now. */
9307 void
9308 truncate_echo_area (EMACS_INT nchars)
9310 if (nchars == 0)
9311 echo_area_buffer[0] = Qnil;
9312 /* A null message buffer means that the frame hasn't really been
9313 initialized yet. Error messages get reported properly by
9314 cmd_error, so this must be just an informative message; toss it. */
9315 else if (!noninteractive
9316 && INTERACTIVE
9317 && !NILP (echo_area_buffer[0]))
9319 struct frame *sf = SELECTED_FRAME ();
9320 if (FRAME_MESSAGE_BUF (sf))
9321 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9326 /* Helper function for truncate_echo_area. Truncate the current
9327 message to at most NCHARS characters. */
9329 static int
9330 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9332 if (BEG + nchars < Z)
9333 del_range (BEG + nchars, Z);
9334 if (Z == BEG)
9335 echo_area_buffer[0] = Qnil;
9336 return 0;
9340 /* Set the current message to a substring of S or STRING.
9342 If STRING is a Lisp string, set the message to the first NBYTES
9343 bytes from STRING. NBYTES zero means use the whole string. If
9344 STRING is multibyte, the message will be displayed multibyte.
9346 If S is not null, set the message to the first LEN bytes of S. LEN
9347 zero means use the whole string. MULTIBYTE_P non-zero means S is
9348 multibyte. Display the message multibyte in that case.
9350 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9351 to t before calling set_message_1 (which calls insert).
9354 void
9355 set_message (const char *s, Lisp_Object string,
9356 EMACS_INT nbytes, int multibyte_p)
9358 message_enable_multibyte
9359 = ((s && multibyte_p)
9360 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9362 with_echo_area_buffer (0, -1, set_message_1,
9363 (EMACS_INT) s, string, nbytes, multibyte_p);
9364 message_buf_print = 0;
9365 help_echo_showing_p = 0;
9369 /* Helper function for set_message. Arguments have the same meaning
9370 as there, with A1 corresponding to S and A2 corresponding to STRING
9371 This function is called with the echo area buffer being
9372 current. */
9374 static int
9375 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9377 const char *s = (const char *) a1;
9378 Lisp_Object string = a2;
9380 /* Change multibyteness of the echo buffer appropriately. */
9381 if (message_enable_multibyte
9382 != !NILP (current_buffer->enable_multibyte_characters))
9383 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9385 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9386 if (!NILP (current_buffer->bidi_display_reordering))
9387 current_buffer->bidi_paragraph_direction = Qleft_to_right;
9389 /* Insert new message at BEG. */
9390 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9392 if (STRINGP (string))
9394 EMACS_INT nchars;
9396 if (nbytes == 0)
9397 nbytes = SBYTES (string);
9398 nchars = string_byte_to_char (string, nbytes);
9400 /* This function takes care of single/multibyte conversion. We
9401 just have to ensure that the echo area buffer has the right
9402 setting of enable_multibyte_characters. */
9403 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9405 else if (s)
9407 if (nbytes == 0)
9408 nbytes = strlen (s);
9410 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9412 /* Convert from multi-byte to single-byte. */
9413 EMACS_INT i;
9414 int c, n;
9415 unsigned char work[1];
9417 /* Convert a multibyte string to single-byte. */
9418 for (i = 0; i < nbytes; i += n)
9420 c = string_char_and_length (s + i, &n);
9421 work[0] = (ASCII_CHAR_P (c)
9423 : multibyte_char_to_unibyte (c, Qnil));
9424 insert_1_both (work, 1, 1, 1, 0, 0);
9427 else if (!multibyte_p
9428 && !NILP (current_buffer->enable_multibyte_characters))
9430 /* Convert from single-byte to multi-byte. */
9431 EMACS_INT i;
9432 int c, n;
9433 const unsigned char *msg = (const unsigned char *) s;
9434 unsigned char str[MAX_MULTIBYTE_LENGTH];
9436 /* Convert a single-byte string to multibyte. */
9437 for (i = 0; i < nbytes; i++)
9439 c = msg[i];
9440 MAKE_CHAR_MULTIBYTE (c);
9441 n = CHAR_STRING (c, str);
9442 insert_1_both (str, 1, n, 1, 0, 0);
9445 else
9446 insert_1 (s, nbytes, 1, 0, 0);
9449 return 0;
9453 /* Clear messages. CURRENT_P non-zero means clear the current
9454 message. LAST_DISPLAYED_P non-zero means clear the message
9455 last displayed. */
9457 void
9458 clear_message (int current_p, int last_displayed_p)
9460 if (current_p)
9462 echo_area_buffer[0] = Qnil;
9463 message_cleared_p = 1;
9466 if (last_displayed_p)
9467 echo_area_buffer[1] = Qnil;
9469 message_buf_print = 0;
9472 /* Clear garbaged frames.
9474 This function is used where the old redisplay called
9475 redraw_garbaged_frames which in turn called redraw_frame which in
9476 turn called clear_frame. The call to clear_frame was a source of
9477 flickering. I believe a clear_frame is not necessary. It should
9478 suffice in the new redisplay to invalidate all current matrices,
9479 and ensure a complete redisplay of all windows. */
9481 static void
9482 clear_garbaged_frames (void)
9484 if (frame_garbaged)
9486 Lisp_Object tail, frame;
9487 int changed_count = 0;
9489 FOR_EACH_FRAME (tail, frame)
9491 struct frame *f = XFRAME (frame);
9493 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9495 if (f->resized_p)
9497 Fredraw_frame (frame);
9498 f->force_flush_display_p = 1;
9500 clear_current_matrices (f);
9501 changed_count++;
9502 f->garbaged = 0;
9503 f->resized_p = 0;
9507 frame_garbaged = 0;
9508 if (changed_count)
9509 ++windows_or_buffers_changed;
9514 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9515 is non-zero update selected_frame. Value is non-zero if the
9516 mini-windows height has been changed. */
9518 static int
9519 echo_area_display (int update_frame_p)
9521 Lisp_Object mini_window;
9522 struct window *w;
9523 struct frame *f;
9524 int window_height_changed_p = 0;
9525 struct frame *sf = SELECTED_FRAME ();
9527 mini_window = FRAME_MINIBUF_WINDOW (sf);
9528 w = XWINDOW (mini_window);
9529 f = XFRAME (WINDOW_FRAME (w));
9531 /* Don't display if frame is invisible or not yet initialized. */
9532 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9533 return 0;
9535 #ifdef HAVE_WINDOW_SYSTEM
9536 /* When Emacs starts, selected_frame may be the initial terminal
9537 frame. If we let this through, a message would be displayed on
9538 the terminal. */
9539 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9540 return 0;
9541 #endif /* HAVE_WINDOW_SYSTEM */
9543 /* Redraw garbaged frames. */
9544 if (frame_garbaged)
9545 clear_garbaged_frames ();
9547 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9549 echo_area_window = mini_window;
9550 window_height_changed_p = display_echo_area (w);
9551 w->must_be_updated_p = 1;
9553 /* Update the display, unless called from redisplay_internal.
9554 Also don't update the screen during redisplay itself. The
9555 update will happen at the end of redisplay, and an update
9556 here could cause confusion. */
9557 if (update_frame_p && !redisplaying_p)
9559 int n = 0;
9561 /* If the display update has been interrupted by pending
9562 input, update mode lines in the frame. Due to the
9563 pending input, it might have been that redisplay hasn't
9564 been called, so that mode lines above the echo area are
9565 garbaged. This looks odd, so we prevent it here. */
9566 if (!display_completed)
9567 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9569 if (window_height_changed_p
9570 /* Don't do this if Emacs is shutting down. Redisplay
9571 needs to run hooks. */
9572 && !NILP (Vrun_hooks))
9574 /* Must update other windows. Likewise as in other
9575 cases, don't let this update be interrupted by
9576 pending input. */
9577 int count = SPECPDL_INDEX ();
9578 specbind (Qredisplay_dont_pause, Qt);
9579 windows_or_buffers_changed = 1;
9580 redisplay_internal (0);
9581 unbind_to (count, Qnil);
9583 else if (FRAME_WINDOW_P (f) && n == 0)
9585 /* Window configuration is the same as before.
9586 Can do with a display update of the echo area,
9587 unless we displayed some mode lines. */
9588 update_single_window (w, 1);
9589 FRAME_RIF (f)->flush_display (f);
9591 else
9592 update_frame (f, 1, 1);
9594 /* If cursor is in the echo area, make sure that the next
9595 redisplay displays the minibuffer, so that the cursor will
9596 be replaced with what the minibuffer wants. */
9597 if (cursor_in_echo_area)
9598 ++windows_or_buffers_changed;
9601 else if (!EQ (mini_window, selected_window))
9602 windows_or_buffers_changed++;
9604 /* Last displayed message is now the current message. */
9605 echo_area_buffer[1] = echo_area_buffer[0];
9606 /* Inform read_char that we're not echoing. */
9607 echo_message_buffer = Qnil;
9609 /* Prevent redisplay optimization in redisplay_internal by resetting
9610 this_line_start_pos. This is done because the mini-buffer now
9611 displays the message instead of its buffer text. */
9612 if (EQ (mini_window, selected_window))
9613 CHARPOS (this_line_start_pos) = 0;
9615 return window_height_changed_p;
9620 /***********************************************************************
9621 Mode Lines and Frame Titles
9622 ***********************************************************************/
9624 /* A buffer for constructing non-propertized mode-line strings and
9625 frame titles in it; allocated from the heap in init_xdisp and
9626 resized as needed in store_mode_line_noprop_char. */
9628 static char *mode_line_noprop_buf;
9630 /* The buffer's end, and a current output position in it. */
9632 static char *mode_line_noprop_buf_end;
9633 static char *mode_line_noprop_ptr;
9635 #define MODE_LINE_NOPROP_LEN(start) \
9636 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9638 static enum {
9639 MODE_LINE_DISPLAY = 0,
9640 MODE_LINE_TITLE,
9641 MODE_LINE_NOPROP,
9642 MODE_LINE_STRING
9643 } mode_line_target;
9645 /* Alist that caches the results of :propertize.
9646 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9647 static Lisp_Object mode_line_proptrans_alist;
9649 /* List of strings making up the mode-line. */
9650 static Lisp_Object mode_line_string_list;
9652 /* Base face property when building propertized mode line string. */
9653 static Lisp_Object mode_line_string_face;
9654 static Lisp_Object mode_line_string_face_prop;
9657 /* Unwind data for mode line strings */
9659 static Lisp_Object Vmode_line_unwind_vector;
9661 static Lisp_Object
9662 format_mode_line_unwind_data (struct buffer *obuf,
9663 Lisp_Object owin,
9664 int save_proptrans)
9666 Lisp_Object vector, tmp;
9668 /* Reduce consing by keeping one vector in
9669 Vwith_echo_area_save_vector. */
9670 vector = Vmode_line_unwind_vector;
9671 Vmode_line_unwind_vector = Qnil;
9673 if (NILP (vector))
9674 vector = Fmake_vector (make_number (8), Qnil);
9676 ASET (vector, 0, make_number (mode_line_target));
9677 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9678 ASET (vector, 2, mode_line_string_list);
9679 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9680 ASET (vector, 4, mode_line_string_face);
9681 ASET (vector, 5, mode_line_string_face_prop);
9683 if (obuf)
9684 XSETBUFFER (tmp, obuf);
9685 else
9686 tmp = Qnil;
9687 ASET (vector, 6, tmp);
9688 ASET (vector, 7, owin);
9690 return vector;
9693 static Lisp_Object
9694 unwind_format_mode_line (Lisp_Object vector)
9696 mode_line_target = XINT (AREF (vector, 0));
9697 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9698 mode_line_string_list = AREF (vector, 2);
9699 if (! EQ (AREF (vector, 3), Qt))
9700 mode_line_proptrans_alist = AREF (vector, 3);
9701 mode_line_string_face = AREF (vector, 4);
9702 mode_line_string_face_prop = AREF (vector, 5);
9704 if (!NILP (AREF (vector, 7)))
9705 /* Select window before buffer, since it may change the buffer. */
9706 Fselect_window (AREF (vector, 7), Qt);
9708 if (!NILP (AREF (vector, 6)))
9710 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9711 ASET (vector, 6, Qnil);
9714 Vmode_line_unwind_vector = vector;
9715 return Qnil;
9719 /* Store a single character C for the frame title in mode_line_noprop_buf.
9720 Re-allocate mode_line_noprop_buf if necessary. */
9722 static void
9723 store_mode_line_noprop_char (char c)
9725 /* If output position has reached the end of the allocated buffer,
9726 double the buffer's size. */
9727 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9729 int len = MODE_LINE_NOPROP_LEN (0);
9730 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9731 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9732 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9733 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9736 *mode_line_noprop_ptr++ = c;
9740 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9741 mode_line_noprop_ptr. STR is the string to store. Do not copy
9742 characters that yield more columns than PRECISION; PRECISION <= 0
9743 means copy the whole string. Pad with spaces until FIELD_WIDTH
9744 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9745 pad. Called from display_mode_element when it is used to build a
9746 frame title. */
9748 static int
9749 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9751 int n = 0;
9752 EMACS_INT dummy, nbytes;
9754 /* Copy at most PRECISION chars from STR. */
9755 nbytes = strlen (str);
9756 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9757 while (nbytes--)
9758 store_mode_line_noprop_char (*str++);
9760 /* Fill up with spaces until FIELD_WIDTH reached. */
9761 while (field_width > 0
9762 && n < field_width)
9764 store_mode_line_noprop_char (' ');
9765 ++n;
9768 return n;
9771 /***********************************************************************
9772 Frame Titles
9773 ***********************************************************************/
9775 #ifdef HAVE_WINDOW_SYSTEM
9777 /* Set the title of FRAME, if it has changed. The title format is
9778 Vicon_title_format if FRAME is iconified, otherwise it is
9779 frame_title_format. */
9781 static void
9782 x_consider_frame_title (Lisp_Object frame)
9784 struct frame *f = XFRAME (frame);
9786 if (FRAME_WINDOW_P (f)
9787 || FRAME_MINIBUF_ONLY_P (f)
9788 || f->explicit_name)
9790 /* Do we have more than one visible frame on this X display? */
9791 Lisp_Object tail;
9792 Lisp_Object fmt;
9793 int title_start;
9794 char *title;
9795 int len;
9796 struct it it;
9797 int count = SPECPDL_INDEX ();
9799 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9801 Lisp_Object other_frame = XCAR (tail);
9802 struct frame *tf = XFRAME (other_frame);
9804 if (tf != f
9805 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9806 && !FRAME_MINIBUF_ONLY_P (tf)
9807 && !EQ (other_frame, tip_frame)
9808 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9809 break;
9812 /* Set global variable indicating that multiple frames exist. */
9813 multiple_frames = CONSP (tail);
9815 /* Switch to the buffer of selected window of the frame. Set up
9816 mode_line_target so that display_mode_element will output into
9817 mode_line_noprop_buf; then display the title. */
9818 record_unwind_protect (unwind_format_mode_line,
9819 format_mode_line_unwind_data
9820 (current_buffer, selected_window, 0));
9822 Fselect_window (f->selected_window, Qt);
9823 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9824 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9826 mode_line_target = MODE_LINE_TITLE;
9827 title_start = MODE_LINE_NOPROP_LEN (0);
9828 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9829 NULL, DEFAULT_FACE_ID);
9830 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9831 len = MODE_LINE_NOPROP_LEN (title_start);
9832 title = mode_line_noprop_buf + title_start;
9833 unbind_to (count, Qnil);
9835 /* Set the title only if it's changed. This avoids consing in
9836 the common case where it hasn't. (If it turns out that we've
9837 already wasted too much time by walking through the list with
9838 display_mode_element, then we might need to optimize at a
9839 higher level than this.) */
9840 if (! STRINGP (f->name)
9841 || SBYTES (f->name) != len
9842 || memcmp (title, SDATA (f->name), len) != 0)
9843 x_implicitly_set_name (f, make_string (title, len), Qnil);
9847 #endif /* not HAVE_WINDOW_SYSTEM */
9852 /***********************************************************************
9853 Menu Bars
9854 ***********************************************************************/
9857 /* Prepare for redisplay by updating menu-bar item lists when
9858 appropriate. This can call eval. */
9860 void
9861 prepare_menu_bars (void)
9863 int all_windows;
9864 struct gcpro gcpro1, gcpro2;
9865 struct frame *f;
9866 Lisp_Object tooltip_frame;
9868 #ifdef HAVE_WINDOW_SYSTEM
9869 tooltip_frame = tip_frame;
9870 #else
9871 tooltip_frame = Qnil;
9872 #endif
9874 /* Update all frame titles based on their buffer names, etc. We do
9875 this before the menu bars so that the buffer-menu will show the
9876 up-to-date frame titles. */
9877 #ifdef HAVE_WINDOW_SYSTEM
9878 if (windows_or_buffers_changed || update_mode_lines)
9880 Lisp_Object tail, frame;
9882 FOR_EACH_FRAME (tail, frame)
9884 f = XFRAME (frame);
9885 if (!EQ (frame, tooltip_frame)
9886 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9887 x_consider_frame_title (frame);
9890 #endif /* HAVE_WINDOW_SYSTEM */
9892 /* Update the menu bar item lists, if appropriate. This has to be
9893 done before any actual redisplay or generation of display lines. */
9894 all_windows = (update_mode_lines
9895 || buffer_shared > 1
9896 || windows_or_buffers_changed);
9897 if (all_windows)
9899 Lisp_Object tail, frame;
9900 int count = SPECPDL_INDEX ();
9901 /* 1 means that update_menu_bar has run its hooks
9902 so any further calls to update_menu_bar shouldn't do so again. */
9903 int menu_bar_hooks_run = 0;
9905 record_unwind_save_match_data ();
9907 FOR_EACH_FRAME (tail, frame)
9909 f = XFRAME (frame);
9911 /* Ignore tooltip frame. */
9912 if (EQ (frame, tooltip_frame))
9913 continue;
9915 /* If a window on this frame changed size, report that to
9916 the user and clear the size-change flag. */
9917 if (FRAME_WINDOW_SIZES_CHANGED (f))
9919 Lisp_Object functions;
9921 /* Clear flag first in case we get an error below. */
9922 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9923 functions = Vwindow_size_change_functions;
9924 GCPRO2 (tail, functions);
9926 while (CONSP (functions))
9928 if (!EQ (XCAR (functions), Qt))
9929 call1 (XCAR (functions), frame);
9930 functions = XCDR (functions);
9932 UNGCPRO;
9935 GCPRO1 (tail);
9936 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9937 #ifdef HAVE_WINDOW_SYSTEM
9938 update_tool_bar (f, 0);
9939 #endif
9940 #ifdef HAVE_NS
9941 if (windows_or_buffers_changed
9942 && FRAME_NS_P (f))
9943 ns_set_doc_edited (f, Fbuffer_modified_p
9944 (XWINDOW (f->selected_window)->buffer));
9945 #endif
9946 UNGCPRO;
9949 unbind_to (count, Qnil);
9951 else
9953 struct frame *sf = SELECTED_FRAME ();
9954 update_menu_bar (sf, 1, 0);
9955 #ifdef HAVE_WINDOW_SYSTEM
9956 update_tool_bar (sf, 1);
9957 #endif
9962 /* Update the menu bar item list for frame F. This has to be done
9963 before we start to fill in any display lines, because it can call
9964 eval.
9966 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9968 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9969 already ran the menu bar hooks for this redisplay, so there
9970 is no need to run them again. The return value is the
9971 updated value of this flag, to pass to the next call. */
9973 static int
9974 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9976 Lisp_Object window;
9977 register struct window *w;
9979 /* If called recursively during a menu update, do nothing. This can
9980 happen when, for instance, an activate-menubar-hook causes a
9981 redisplay. */
9982 if (inhibit_menubar_update)
9983 return hooks_run;
9985 window = FRAME_SELECTED_WINDOW (f);
9986 w = XWINDOW (window);
9988 if (FRAME_WINDOW_P (f)
9990 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9991 || defined (HAVE_NS) || defined (USE_GTK)
9992 FRAME_EXTERNAL_MENU_BAR (f)
9993 #else
9994 FRAME_MENU_BAR_LINES (f) > 0
9995 #endif
9996 : FRAME_MENU_BAR_LINES (f) > 0)
9998 /* If the user has switched buffers or windows, we need to
9999 recompute to reflect the new bindings. But we'll
10000 recompute when update_mode_lines is set too; that means
10001 that people can use force-mode-line-update to request
10002 that the menu bar be recomputed. The adverse effect on
10003 the rest of the redisplay algorithm is about the same as
10004 windows_or_buffers_changed anyway. */
10005 if (windows_or_buffers_changed
10006 /* This used to test w->update_mode_line, but we believe
10007 there is no need to recompute the menu in that case. */
10008 || update_mode_lines
10009 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10010 < BUF_MODIFF (XBUFFER (w->buffer)))
10011 != !NILP (w->last_had_star))
10012 || ((!NILP (Vtransient_mark_mode)
10013 && !NILP (XBUFFER (w->buffer)->mark_active))
10014 != !NILP (w->region_showing)))
10016 struct buffer *prev = current_buffer;
10017 int count = SPECPDL_INDEX ();
10019 specbind (Qinhibit_menubar_update, Qt);
10021 set_buffer_internal_1 (XBUFFER (w->buffer));
10022 if (save_match_data)
10023 record_unwind_save_match_data ();
10024 if (NILP (Voverriding_local_map_menu_flag))
10026 specbind (Qoverriding_terminal_local_map, Qnil);
10027 specbind (Qoverriding_local_map, Qnil);
10030 if (!hooks_run)
10032 /* Run the Lucid hook. */
10033 safe_run_hooks (Qactivate_menubar_hook);
10035 /* If it has changed current-menubar from previous value,
10036 really recompute the menu-bar from the value. */
10037 if (! NILP (Vlucid_menu_bar_dirty_flag))
10038 call0 (Qrecompute_lucid_menubar);
10040 safe_run_hooks (Qmenu_bar_update_hook);
10042 hooks_run = 1;
10045 XSETFRAME (Vmenu_updating_frame, f);
10046 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10048 /* Redisplay the menu bar in case we changed it. */
10049 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10050 || defined (HAVE_NS) || defined (USE_GTK)
10051 if (FRAME_WINDOW_P (f))
10053 #if defined (HAVE_NS)
10054 /* All frames on Mac OS share the same menubar. So only
10055 the selected frame should be allowed to set it. */
10056 if (f == SELECTED_FRAME ())
10057 #endif
10058 set_frame_menubar (f, 0, 0);
10060 else
10061 /* On a terminal screen, the menu bar is an ordinary screen
10062 line, and this makes it get updated. */
10063 w->update_mode_line = Qt;
10064 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10065 /* In the non-toolkit version, the menu bar is an ordinary screen
10066 line, and this makes it get updated. */
10067 w->update_mode_line = Qt;
10068 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10070 unbind_to (count, Qnil);
10071 set_buffer_internal_1 (prev);
10075 return hooks_run;
10080 /***********************************************************************
10081 Output Cursor
10082 ***********************************************************************/
10084 #ifdef HAVE_WINDOW_SYSTEM
10086 /* EXPORT:
10087 Nominal cursor position -- where to draw output.
10088 HPOS and VPOS are window relative glyph matrix coordinates.
10089 X and Y are window relative pixel coordinates. */
10091 struct cursor_pos output_cursor;
10094 /* EXPORT:
10095 Set the global variable output_cursor to CURSOR. All cursor
10096 positions are relative to updated_window. */
10098 void
10099 set_output_cursor (struct cursor_pos *cursor)
10101 output_cursor.hpos = cursor->hpos;
10102 output_cursor.vpos = cursor->vpos;
10103 output_cursor.x = cursor->x;
10104 output_cursor.y = cursor->y;
10108 /* EXPORT for RIF:
10109 Set a nominal cursor position.
10111 HPOS and VPOS are column/row positions in a window glyph matrix. X
10112 and Y are window text area relative pixel positions.
10114 If this is done during an update, updated_window will contain the
10115 window that is being updated and the position is the future output
10116 cursor position for that window. If updated_window is null, use
10117 selected_window and display the cursor at the given position. */
10119 void
10120 x_cursor_to (int vpos, int hpos, int y, int x)
10122 struct window *w;
10124 /* If updated_window is not set, work on selected_window. */
10125 if (updated_window)
10126 w = updated_window;
10127 else
10128 w = XWINDOW (selected_window);
10130 /* Set the output cursor. */
10131 output_cursor.hpos = hpos;
10132 output_cursor.vpos = vpos;
10133 output_cursor.x = x;
10134 output_cursor.y = y;
10136 /* If not called as part of an update, really display the cursor.
10137 This will also set the cursor position of W. */
10138 if (updated_window == NULL)
10140 BLOCK_INPUT;
10141 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10142 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10143 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10144 UNBLOCK_INPUT;
10148 #endif /* HAVE_WINDOW_SYSTEM */
10151 /***********************************************************************
10152 Tool-bars
10153 ***********************************************************************/
10155 #ifdef HAVE_WINDOW_SYSTEM
10157 /* Where the mouse was last time we reported a mouse event. */
10159 FRAME_PTR last_mouse_frame;
10161 /* Tool-bar item index of the item on which a mouse button was pressed
10162 or -1. */
10164 int last_tool_bar_item;
10167 static Lisp_Object
10168 update_tool_bar_unwind (Lisp_Object frame)
10170 selected_frame = frame;
10171 return Qnil;
10174 /* Update the tool-bar item list for frame F. This has to be done
10175 before we start to fill in any display lines. Called from
10176 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10177 and restore it here. */
10179 static void
10180 update_tool_bar (struct frame *f, int save_match_data)
10182 #if defined (USE_GTK) || defined (HAVE_NS)
10183 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10184 #else
10185 int do_update = WINDOWP (f->tool_bar_window)
10186 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10187 #endif
10189 if (do_update)
10191 Lisp_Object window;
10192 struct window *w;
10194 window = FRAME_SELECTED_WINDOW (f);
10195 w = XWINDOW (window);
10197 /* If the user has switched buffers or windows, we need to
10198 recompute to reflect the new bindings. But we'll
10199 recompute when update_mode_lines is set too; that means
10200 that people can use force-mode-line-update to request
10201 that the menu bar be recomputed. The adverse effect on
10202 the rest of the redisplay algorithm is about the same as
10203 windows_or_buffers_changed anyway. */
10204 if (windows_or_buffers_changed
10205 || !NILP (w->update_mode_line)
10206 || update_mode_lines
10207 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10208 < BUF_MODIFF (XBUFFER (w->buffer)))
10209 != !NILP (w->last_had_star))
10210 || ((!NILP (Vtransient_mark_mode)
10211 && !NILP (XBUFFER (w->buffer)->mark_active))
10212 != !NILP (w->region_showing)))
10214 struct buffer *prev = current_buffer;
10215 int count = SPECPDL_INDEX ();
10216 Lisp_Object frame, new_tool_bar;
10217 int new_n_tool_bar;
10218 struct gcpro gcpro1;
10220 /* Set current_buffer to the buffer of the selected
10221 window of the frame, so that we get the right local
10222 keymaps. */
10223 set_buffer_internal_1 (XBUFFER (w->buffer));
10225 /* Save match data, if we must. */
10226 if (save_match_data)
10227 record_unwind_save_match_data ();
10229 /* Make sure that we don't accidentally use bogus keymaps. */
10230 if (NILP (Voverriding_local_map_menu_flag))
10232 specbind (Qoverriding_terminal_local_map, Qnil);
10233 specbind (Qoverriding_local_map, Qnil);
10236 GCPRO1 (new_tool_bar);
10238 /* We must temporarily set the selected frame to this frame
10239 before calling tool_bar_items, because the calculation of
10240 the tool-bar keymap uses the selected frame (see
10241 `tool-bar-make-keymap' in tool-bar.el). */
10242 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10243 XSETFRAME (frame, f);
10244 selected_frame = frame;
10246 /* Build desired tool-bar items from keymaps. */
10247 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10248 &new_n_tool_bar);
10250 /* Redisplay the tool-bar if we changed it. */
10251 if (new_n_tool_bar != f->n_tool_bar_items
10252 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10254 /* Redisplay that happens asynchronously due to an expose event
10255 may access f->tool_bar_items. Make sure we update both
10256 variables within BLOCK_INPUT so no such event interrupts. */
10257 BLOCK_INPUT;
10258 f->tool_bar_items = new_tool_bar;
10259 f->n_tool_bar_items = new_n_tool_bar;
10260 w->update_mode_line = Qt;
10261 UNBLOCK_INPUT;
10264 UNGCPRO;
10266 unbind_to (count, Qnil);
10267 set_buffer_internal_1 (prev);
10273 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10274 F's desired tool-bar contents. F->tool_bar_items must have
10275 been set up previously by calling prepare_menu_bars. */
10277 static void
10278 build_desired_tool_bar_string (struct frame *f)
10280 int i, size, size_needed;
10281 struct gcpro gcpro1, gcpro2, gcpro3;
10282 Lisp_Object image, plist, props;
10284 image = plist = props = Qnil;
10285 GCPRO3 (image, plist, props);
10287 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10288 Otherwise, make a new string. */
10290 /* The size of the string we might be able to reuse. */
10291 size = (STRINGP (f->desired_tool_bar_string)
10292 ? SCHARS (f->desired_tool_bar_string)
10293 : 0);
10295 /* We need one space in the string for each image. */
10296 size_needed = f->n_tool_bar_items;
10298 /* Reuse f->desired_tool_bar_string, if possible. */
10299 if (size < size_needed || NILP (f->desired_tool_bar_string))
10300 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10301 make_number (' '));
10302 else
10304 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10305 Fremove_text_properties (make_number (0), make_number (size),
10306 props, f->desired_tool_bar_string);
10309 /* Put a `display' property on the string for the images to display,
10310 put a `menu_item' property on tool-bar items with a value that
10311 is the index of the item in F's tool-bar item vector. */
10312 for (i = 0; i < f->n_tool_bar_items; ++i)
10314 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10316 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10317 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10318 int hmargin, vmargin, relief, idx, end;
10320 /* Ignore separator items. */
10321 if (EQ (PROP (TOOL_BAR_ITEM_TYPE), Qt))
10322 continue;
10324 /* If image is a vector, choose the image according to the
10325 button state. */
10326 image = PROP (TOOL_BAR_ITEM_IMAGES);
10327 if (VECTORP (image))
10329 if (enabled_p)
10330 idx = (selected_p
10331 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10332 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10333 else
10334 idx = (selected_p
10335 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10336 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10338 xassert (ASIZE (image) >= idx);
10339 image = AREF (image, idx);
10341 else
10342 idx = -1;
10344 /* Ignore invalid image specifications. */
10345 if (!valid_image_p (image))
10346 continue;
10348 /* Display the tool-bar button pressed, or depressed. */
10349 plist = Fcopy_sequence (XCDR (image));
10351 /* Compute margin and relief to draw. */
10352 relief = (tool_bar_button_relief >= 0
10353 ? tool_bar_button_relief
10354 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10355 hmargin = vmargin = relief;
10357 if (INTEGERP (Vtool_bar_button_margin)
10358 && XINT (Vtool_bar_button_margin) > 0)
10360 hmargin += XFASTINT (Vtool_bar_button_margin);
10361 vmargin += XFASTINT (Vtool_bar_button_margin);
10363 else if (CONSP (Vtool_bar_button_margin))
10365 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10366 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10367 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10369 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10370 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10371 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10374 if (auto_raise_tool_bar_buttons_p)
10376 /* Add a `:relief' property to the image spec if the item is
10377 selected. */
10378 if (selected_p)
10380 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10381 hmargin -= relief;
10382 vmargin -= relief;
10385 else
10387 /* If image is selected, display it pressed, i.e. with a
10388 negative relief. If it's not selected, display it with a
10389 raised relief. */
10390 plist = Fplist_put (plist, QCrelief,
10391 (selected_p
10392 ? make_number (-relief)
10393 : make_number (relief)));
10394 hmargin -= relief;
10395 vmargin -= relief;
10398 /* Put a margin around the image. */
10399 if (hmargin || vmargin)
10401 if (hmargin == vmargin)
10402 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10403 else
10404 plist = Fplist_put (plist, QCmargin,
10405 Fcons (make_number (hmargin),
10406 make_number (vmargin)));
10409 /* If button is not enabled, and we don't have special images
10410 for the disabled state, make the image appear disabled by
10411 applying an appropriate algorithm to it. */
10412 if (!enabled_p && idx < 0)
10413 plist = Fplist_put (plist, QCconversion, Qdisabled);
10415 /* Put a `display' text property on the string for the image to
10416 display. Put a `menu-item' property on the string that gives
10417 the start of this item's properties in the tool-bar items
10418 vector. */
10419 image = Fcons (Qimage, plist);
10420 props = list4 (Qdisplay, image,
10421 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10423 /* Let the last image hide all remaining spaces in the tool bar
10424 string. The string can be longer than needed when we reuse a
10425 previous string. */
10426 if (i + 1 == f->n_tool_bar_items)
10427 end = SCHARS (f->desired_tool_bar_string);
10428 else
10429 end = i + 1;
10430 Fadd_text_properties (make_number (i), make_number (end),
10431 props, f->desired_tool_bar_string);
10432 #undef PROP
10435 UNGCPRO;
10439 /* Display one line of the tool-bar of frame IT->f.
10441 HEIGHT specifies the desired height of the tool-bar line.
10442 If the actual height of the glyph row is less than HEIGHT, the
10443 row's height is increased to HEIGHT, and the icons are centered
10444 vertically in the new height.
10446 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10447 count a final empty row in case the tool-bar width exactly matches
10448 the window width.
10451 static void
10452 display_tool_bar_line (struct it *it, int height)
10454 struct glyph_row *row = it->glyph_row;
10455 int max_x = it->last_visible_x;
10456 struct glyph *last;
10458 prepare_desired_row (row);
10459 row->y = it->current_y;
10461 /* Note that this isn't made use of if the face hasn't a box,
10462 so there's no need to check the face here. */
10463 it->start_of_box_run_p = 1;
10465 while (it->current_x < max_x)
10467 int x, n_glyphs_before, i, nglyphs;
10468 struct it it_before;
10470 /* Get the next display element. */
10471 if (!get_next_display_element (it))
10473 /* Don't count empty row if we are counting needed tool-bar lines. */
10474 if (height < 0 && !it->hpos)
10475 return;
10476 break;
10479 /* Produce glyphs. */
10480 n_glyphs_before = row->used[TEXT_AREA];
10481 it_before = *it;
10483 PRODUCE_GLYPHS (it);
10485 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10486 i = 0;
10487 x = it_before.current_x;
10488 while (i < nglyphs)
10490 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10492 if (x + glyph->pixel_width > max_x)
10494 /* Glyph doesn't fit on line. Backtrack. */
10495 row->used[TEXT_AREA] = n_glyphs_before;
10496 *it = it_before;
10497 /* If this is the only glyph on this line, it will never fit on the
10498 toolbar, so skip it. But ensure there is at least one glyph,
10499 so we don't accidentally disable the tool-bar. */
10500 if (n_glyphs_before == 0
10501 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10502 break;
10503 goto out;
10506 ++it->hpos;
10507 x += glyph->pixel_width;
10508 ++i;
10511 /* Stop at line ends. */
10512 if (ITERATOR_AT_END_OF_LINE_P (it))
10513 break;
10515 set_iterator_to_next (it, 1);
10518 out:;
10520 row->displays_text_p = row->used[TEXT_AREA] != 0;
10522 /* Use default face for the border below the tool bar.
10524 FIXME: When auto-resize-tool-bars is grow-only, there is
10525 no additional border below the possibly empty tool-bar lines.
10526 So to make the extra empty lines look "normal", we have to
10527 use the tool-bar face for the border too. */
10528 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10529 it->face_id = DEFAULT_FACE_ID;
10531 extend_face_to_end_of_line (it);
10532 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10533 last->right_box_line_p = 1;
10534 if (last == row->glyphs[TEXT_AREA])
10535 last->left_box_line_p = 1;
10537 /* Make line the desired height and center it vertically. */
10538 if ((height -= it->max_ascent + it->max_descent) > 0)
10540 /* Don't add more than one line height. */
10541 height %= FRAME_LINE_HEIGHT (it->f);
10542 it->max_ascent += height / 2;
10543 it->max_descent += (height + 1) / 2;
10546 compute_line_metrics (it);
10548 /* If line is empty, make it occupy the rest of the tool-bar. */
10549 if (!row->displays_text_p)
10551 row->height = row->phys_height = it->last_visible_y - row->y;
10552 row->visible_height = row->height;
10553 row->ascent = row->phys_ascent = 0;
10554 row->extra_line_spacing = 0;
10557 row->full_width_p = 1;
10558 row->continued_p = 0;
10559 row->truncated_on_left_p = 0;
10560 row->truncated_on_right_p = 0;
10562 it->current_x = it->hpos = 0;
10563 it->current_y += row->height;
10564 ++it->vpos;
10565 ++it->glyph_row;
10569 /* Max tool-bar height. */
10571 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10572 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10574 /* Value is the number of screen lines needed to make all tool-bar
10575 items of frame F visible. The number of actual rows needed is
10576 returned in *N_ROWS if non-NULL. */
10578 static int
10579 tool_bar_lines_needed (struct frame *f, int *n_rows)
10581 struct window *w = XWINDOW (f->tool_bar_window);
10582 struct it it;
10583 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10584 the desired matrix, so use (unused) mode-line row as temporary row to
10585 avoid destroying the first tool-bar row. */
10586 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10588 /* Initialize an iterator for iteration over
10589 F->desired_tool_bar_string in the tool-bar window of frame F. */
10590 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10591 it.first_visible_x = 0;
10592 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10593 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10595 while (!ITERATOR_AT_END_P (&it))
10597 clear_glyph_row (temp_row);
10598 it.glyph_row = temp_row;
10599 display_tool_bar_line (&it, -1);
10601 clear_glyph_row (temp_row);
10603 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10604 if (n_rows)
10605 *n_rows = it.vpos > 0 ? it.vpos : -1;
10607 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10611 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10612 0, 1, 0,
10613 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10614 (Lisp_Object frame)
10616 struct frame *f;
10617 struct window *w;
10618 int nlines = 0;
10620 if (NILP (frame))
10621 frame = selected_frame;
10622 else
10623 CHECK_FRAME (frame);
10624 f = XFRAME (frame);
10626 if (WINDOWP (f->tool_bar_window)
10627 || (w = XWINDOW (f->tool_bar_window),
10628 WINDOW_TOTAL_LINES (w) > 0))
10630 update_tool_bar (f, 1);
10631 if (f->n_tool_bar_items)
10633 build_desired_tool_bar_string (f);
10634 nlines = tool_bar_lines_needed (f, NULL);
10638 return make_number (nlines);
10642 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10643 height should be changed. */
10645 static int
10646 redisplay_tool_bar (struct frame *f)
10648 struct window *w;
10649 struct it it;
10650 struct glyph_row *row;
10652 #if defined (USE_GTK) || defined (HAVE_NS)
10653 if (FRAME_EXTERNAL_TOOL_BAR (f))
10654 update_frame_tool_bar (f);
10655 return 0;
10656 #endif
10658 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10659 do anything. This means you must start with tool-bar-lines
10660 non-zero to get the auto-sizing effect. Or in other words, you
10661 can turn off tool-bars by specifying tool-bar-lines zero. */
10662 if (!WINDOWP (f->tool_bar_window)
10663 || (w = XWINDOW (f->tool_bar_window),
10664 WINDOW_TOTAL_LINES (w) == 0))
10665 return 0;
10667 /* Set up an iterator for the tool-bar window. */
10668 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10669 it.first_visible_x = 0;
10670 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10671 row = it.glyph_row;
10673 /* Build a string that represents the contents of the tool-bar. */
10674 build_desired_tool_bar_string (f);
10675 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10677 if (f->n_tool_bar_rows == 0)
10679 int nlines;
10681 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10682 nlines != WINDOW_TOTAL_LINES (w)))
10684 Lisp_Object frame;
10685 int old_height = WINDOW_TOTAL_LINES (w);
10687 XSETFRAME (frame, f);
10688 Fmodify_frame_parameters (frame,
10689 Fcons (Fcons (Qtool_bar_lines,
10690 make_number (nlines)),
10691 Qnil));
10692 if (WINDOW_TOTAL_LINES (w) != old_height)
10694 clear_glyph_matrix (w->desired_matrix);
10695 fonts_changed_p = 1;
10696 return 1;
10701 /* Display as many lines as needed to display all tool-bar items. */
10703 if (f->n_tool_bar_rows > 0)
10705 int border, rows, height, extra;
10707 if (INTEGERP (Vtool_bar_border))
10708 border = XINT (Vtool_bar_border);
10709 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10710 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10711 else if (EQ (Vtool_bar_border, Qborder_width))
10712 border = f->border_width;
10713 else
10714 border = 0;
10715 if (border < 0)
10716 border = 0;
10718 rows = f->n_tool_bar_rows;
10719 height = max (1, (it.last_visible_y - border) / rows);
10720 extra = it.last_visible_y - border - height * rows;
10722 while (it.current_y < it.last_visible_y)
10724 int h = 0;
10725 if (extra > 0 && rows-- > 0)
10727 h = (extra + rows - 1) / rows;
10728 extra -= h;
10730 display_tool_bar_line (&it, height + h);
10733 else
10735 while (it.current_y < it.last_visible_y)
10736 display_tool_bar_line (&it, 0);
10739 /* It doesn't make much sense to try scrolling in the tool-bar
10740 window, so don't do it. */
10741 w->desired_matrix->no_scrolling_p = 1;
10742 w->must_be_updated_p = 1;
10744 if (!NILP (Vauto_resize_tool_bars))
10746 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10747 int change_height_p = 0;
10749 /* If we couldn't display everything, change the tool-bar's
10750 height if there is room for more. */
10751 if (IT_STRING_CHARPOS (it) < it.end_charpos
10752 && it.current_y < max_tool_bar_height)
10753 change_height_p = 1;
10755 row = it.glyph_row - 1;
10757 /* If there are blank lines at the end, except for a partially
10758 visible blank line at the end that is smaller than
10759 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10760 if (!row->displays_text_p
10761 && row->height >= FRAME_LINE_HEIGHT (f))
10762 change_height_p = 1;
10764 /* If row displays tool-bar items, but is partially visible,
10765 change the tool-bar's height. */
10766 if (row->displays_text_p
10767 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10768 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10769 change_height_p = 1;
10771 /* Resize windows as needed by changing the `tool-bar-lines'
10772 frame parameter. */
10773 if (change_height_p)
10775 Lisp_Object frame;
10776 int old_height = WINDOW_TOTAL_LINES (w);
10777 int nrows;
10778 int nlines = tool_bar_lines_needed (f, &nrows);
10780 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10781 && !f->minimize_tool_bar_window_p)
10782 ? (nlines > old_height)
10783 : (nlines != old_height));
10784 f->minimize_tool_bar_window_p = 0;
10786 if (change_height_p)
10788 XSETFRAME (frame, f);
10789 Fmodify_frame_parameters (frame,
10790 Fcons (Fcons (Qtool_bar_lines,
10791 make_number (nlines)),
10792 Qnil));
10793 if (WINDOW_TOTAL_LINES (w) != old_height)
10795 clear_glyph_matrix (w->desired_matrix);
10796 f->n_tool_bar_rows = nrows;
10797 fonts_changed_p = 1;
10798 return 1;
10804 f->minimize_tool_bar_window_p = 0;
10805 return 0;
10809 /* Get information about the tool-bar item which is displayed in GLYPH
10810 on frame F. Return in *PROP_IDX the index where tool-bar item
10811 properties start in F->tool_bar_items. Value is zero if
10812 GLYPH doesn't display a tool-bar item. */
10814 static int
10815 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10817 Lisp_Object prop;
10818 int success_p;
10819 int charpos;
10821 /* This function can be called asynchronously, which means we must
10822 exclude any possibility that Fget_text_property signals an
10823 error. */
10824 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10825 charpos = max (0, charpos);
10827 /* Get the text property `menu-item' at pos. The value of that
10828 property is the start index of this item's properties in
10829 F->tool_bar_items. */
10830 prop = Fget_text_property (make_number (charpos),
10831 Qmenu_item, f->current_tool_bar_string);
10832 if (INTEGERP (prop))
10834 *prop_idx = XINT (prop);
10835 success_p = 1;
10837 else
10838 success_p = 0;
10840 return success_p;
10844 /* Get information about the tool-bar item at position X/Y on frame F.
10845 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10846 the current matrix of the tool-bar window of F, or NULL if not
10847 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10848 item in F->tool_bar_items. Value is
10850 -1 if X/Y is not on a tool-bar item
10851 0 if X/Y is on the same item that was highlighted before.
10852 1 otherwise. */
10854 static int
10855 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10856 int *hpos, int *vpos, int *prop_idx)
10858 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10859 struct window *w = XWINDOW (f->tool_bar_window);
10860 int area;
10862 /* Find the glyph under X/Y. */
10863 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10864 if (*glyph == NULL)
10865 return -1;
10867 /* Get the start of this tool-bar item's properties in
10868 f->tool_bar_items. */
10869 if (!tool_bar_item_info (f, *glyph, prop_idx))
10870 return -1;
10872 /* Is mouse on the highlighted item? */
10873 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10874 && *vpos >= hlinfo->mouse_face_beg_row
10875 && *vpos <= hlinfo->mouse_face_end_row
10876 && (*vpos > hlinfo->mouse_face_beg_row
10877 || *hpos >= hlinfo->mouse_face_beg_col)
10878 && (*vpos < hlinfo->mouse_face_end_row
10879 || *hpos < hlinfo->mouse_face_end_col
10880 || hlinfo->mouse_face_past_end))
10881 return 0;
10883 return 1;
10887 /* EXPORT:
10888 Handle mouse button event on the tool-bar of frame F, at
10889 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10890 0 for button release. MODIFIERS is event modifiers for button
10891 release. */
10893 void
10894 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10895 unsigned int modifiers)
10897 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10898 struct window *w = XWINDOW (f->tool_bar_window);
10899 int hpos, vpos, prop_idx;
10900 struct glyph *glyph;
10901 Lisp_Object enabled_p;
10903 /* If not on the highlighted tool-bar item, return. */
10904 frame_to_window_pixel_xy (w, &x, &y);
10905 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10906 return;
10908 /* If item is disabled, do nothing. */
10909 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10910 if (NILP (enabled_p))
10911 return;
10913 if (down_p)
10915 /* Show item in pressed state. */
10916 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10917 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10918 last_tool_bar_item = prop_idx;
10920 else
10922 Lisp_Object key, frame;
10923 struct input_event event;
10924 EVENT_INIT (event);
10926 /* Show item in released state. */
10927 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10928 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10930 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10932 XSETFRAME (frame, f);
10933 event.kind = TOOL_BAR_EVENT;
10934 event.frame_or_window = frame;
10935 event.arg = frame;
10936 kbd_buffer_store_event (&event);
10938 event.kind = TOOL_BAR_EVENT;
10939 event.frame_or_window = frame;
10940 event.arg = key;
10941 event.modifiers = modifiers;
10942 kbd_buffer_store_event (&event);
10943 last_tool_bar_item = -1;
10948 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10949 tool-bar window-relative coordinates X/Y. Called from
10950 note_mouse_highlight. */
10952 static void
10953 note_tool_bar_highlight (struct frame *f, int x, int y)
10955 Lisp_Object window = f->tool_bar_window;
10956 struct window *w = XWINDOW (window);
10957 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10958 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10959 int hpos, vpos;
10960 struct glyph *glyph;
10961 struct glyph_row *row;
10962 int i;
10963 Lisp_Object enabled_p;
10964 int prop_idx;
10965 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10966 int mouse_down_p, rc;
10968 /* Function note_mouse_highlight is called with negative X/Y
10969 values when mouse moves outside of the frame. */
10970 if (x <= 0 || y <= 0)
10972 clear_mouse_face (hlinfo);
10973 return;
10976 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10977 if (rc < 0)
10979 /* Not on tool-bar item. */
10980 clear_mouse_face (hlinfo);
10981 return;
10983 else if (rc == 0)
10984 /* On same tool-bar item as before. */
10985 goto set_help_echo;
10987 clear_mouse_face (hlinfo);
10989 /* Mouse is down, but on different tool-bar item? */
10990 mouse_down_p = (dpyinfo->grabbed
10991 && f == last_mouse_frame
10992 && FRAME_LIVE_P (f));
10993 if (mouse_down_p
10994 && last_tool_bar_item != prop_idx)
10995 return;
10997 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10998 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11000 /* If tool-bar item is not enabled, don't highlight it. */
11001 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11002 if (!NILP (enabled_p))
11004 /* Compute the x-position of the glyph. In front and past the
11005 image is a space. We include this in the highlighted area. */
11006 row = MATRIX_ROW (w->current_matrix, vpos);
11007 for (i = x = 0; i < hpos; ++i)
11008 x += row->glyphs[TEXT_AREA][i].pixel_width;
11010 /* Record this as the current active region. */
11011 hlinfo->mouse_face_beg_col = hpos;
11012 hlinfo->mouse_face_beg_row = vpos;
11013 hlinfo->mouse_face_beg_x = x;
11014 hlinfo->mouse_face_beg_y = row->y;
11015 hlinfo->mouse_face_past_end = 0;
11017 hlinfo->mouse_face_end_col = hpos + 1;
11018 hlinfo->mouse_face_end_row = vpos;
11019 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11020 hlinfo->mouse_face_end_y = row->y;
11021 hlinfo->mouse_face_window = window;
11022 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11024 /* Display it as active. */
11025 show_mouse_face (hlinfo, draw);
11026 hlinfo->mouse_face_image_state = draw;
11029 set_help_echo:
11031 /* Set help_echo_string to a help string to display for this tool-bar item.
11032 XTread_socket does the rest. */
11033 help_echo_object = help_echo_window = Qnil;
11034 help_echo_pos = -1;
11035 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11036 if (NILP (help_echo_string))
11037 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11040 #endif /* HAVE_WINDOW_SYSTEM */
11044 /************************************************************************
11045 Horizontal scrolling
11046 ************************************************************************/
11048 static int hscroll_window_tree (Lisp_Object);
11049 static int hscroll_windows (Lisp_Object);
11051 /* For all leaf windows in the window tree rooted at WINDOW, set their
11052 hscroll value so that PT is (i) visible in the window, and (ii) so
11053 that it is not within a certain margin at the window's left and
11054 right border. Value is non-zero if any window's hscroll has been
11055 changed. */
11057 static int
11058 hscroll_window_tree (Lisp_Object window)
11060 int hscrolled_p = 0;
11061 int hscroll_relative_p = FLOATP (Vhscroll_step);
11062 int hscroll_step_abs = 0;
11063 double hscroll_step_rel = 0;
11065 if (hscroll_relative_p)
11067 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11068 if (hscroll_step_rel < 0)
11070 hscroll_relative_p = 0;
11071 hscroll_step_abs = 0;
11074 else if (INTEGERP (Vhscroll_step))
11076 hscroll_step_abs = XINT (Vhscroll_step);
11077 if (hscroll_step_abs < 0)
11078 hscroll_step_abs = 0;
11080 else
11081 hscroll_step_abs = 0;
11083 while (WINDOWP (window))
11085 struct window *w = XWINDOW (window);
11087 if (WINDOWP (w->hchild))
11088 hscrolled_p |= hscroll_window_tree (w->hchild);
11089 else if (WINDOWP (w->vchild))
11090 hscrolled_p |= hscroll_window_tree (w->vchild);
11091 else if (w->cursor.vpos >= 0)
11093 int h_margin;
11094 int text_area_width;
11095 struct glyph_row *current_cursor_row
11096 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11097 struct glyph_row *desired_cursor_row
11098 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11099 struct glyph_row *cursor_row
11100 = (desired_cursor_row->enabled_p
11101 ? desired_cursor_row
11102 : current_cursor_row);
11104 text_area_width = window_box_width (w, TEXT_AREA);
11106 /* Scroll when cursor is inside this scroll margin. */
11107 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11109 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11110 && ((XFASTINT (w->hscroll)
11111 && w->cursor.x <= h_margin)
11112 || (cursor_row->enabled_p
11113 && cursor_row->truncated_on_right_p
11114 && (w->cursor.x >= text_area_width - h_margin))))
11116 struct it it;
11117 int hscroll;
11118 struct buffer *saved_current_buffer;
11119 EMACS_INT pt;
11120 int wanted_x;
11122 /* Find point in a display of infinite width. */
11123 saved_current_buffer = current_buffer;
11124 current_buffer = XBUFFER (w->buffer);
11126 if (w == XWINDOW (selected_window))
11127 pt = BUF_PT (current_buffer);
11128 else
11130 pt = marker_position (w->pointm);
11131 pt = max (BEGV, pt);
11132 pt = min (ZV, pt);
11135 /* Move iterator to pt starting at cursor_row->start in
11136 a line with infinite width. */
11137 init_to_row_start (&it, w, cursor_row);
11138 it.last_visible_x = INFINITY;
11139 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11140 current_buffer = saved_current_buffer;
11142 /* Position cursor in window. */
11143 if (!hscroll_relative_p && hscroll_step_abs == 0)
11144 hscroll = max (0, (it.current_x
11145 - (ITERATOR_AT_END_OF_LINE_P (&it)
11146 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11147 : (text_area_width / 2))))
11148 / FRAME_COLUMN_WIDTH (it.f);
11149 else if (w->cursor.x >= text_area_width - h_margin)
11151 if (hscroll_relative_p)
11152 wanted_x = text_area_width * (1 - hscroll_step_rel)
11153 - h_margin;
11154 else
11155 wanted_x = text_area_width
11156 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11157 - h_margin;
11158 hscroll
11159 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11161 else
11163 if (hscroll_relative_p)
11164 wanted_x = text_area_width * hscroll_step_rel
11165 + h_margin;
11166 else
11167 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11168 + h_margin;
11169 hscroll
11170 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11172 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11174 /* Don't call Fset_window_hscroll if value hasn't
11175 changed because it will prevent redisplay
11176 optimizations. */
11177 if (XFASTINT (w->hscroll) != hscroll)
11179 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11180 w->hscroll = make_number (hscroll);
11181 hscrolled_p = 1;
11186 window = w->next;
11189 /* Value is non-zero if hscroll of any leaf window has been changed. */
11190 return hscrolled_p;
11194 /* Set hscroll so that cursor is visible and not inside horizontal
11195 scroll margins for all windows in the tree rooted at WINDOW. See
11196 also hscroll_window_tree above. Value is non-zero if any window's
11197 hscroll has been changed. If it has, desired matrices on the frame
11198 of WINDOW are cleared. */
11200 static int
11201 hscroll_windows (Lisp_Object window)
11203 int hscrolled_p = hscroll_window_tree (window);
11204 if (hscrolled_p)
11205 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11206 return hscrolled_p;
11211 /************************************************************************
11212 Redisplay
11213 ************************************************************************/
11215 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11216 to a non-zero value. This is sometimes handy to have in a debugger
11217 session. */
11219 #if GLYPH_DEBUG
11221 /* First and last unchanged row for try_window_id. */
11223 int debug_first_unchanged_at_end_vpos;
11224 int debug_last_unchanged_at_beg_vpos;
11226 /* Delta vpos and y. */
11228 int debug_dvpos, debug_dy;
11230 /* Delta in characters and bytes for try_window_id. */
11232 EMACS_INT debug_delta, debug_delta_bytes;
11234 /* Values of window_end_pos and window_end_vpos at the end of
11235 try_window_id. */
11237 EMACS_INT debug_end_pos, debug_end_vpos;
11239 /* Append a string to W->desired_matrix->method. FMT is a printf
11240 format string. A1...A9 are a supplement for a variable-length
11241 argument list. If trace_redisplay_p is non-zero also printf the
11242 resulting string to stderr. */
11244 static void
11245 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11246 struct window *w;
11247 char *fmt;
11248 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11250 char buffer[512];
11251 char *method = w->desired_matrix->method;
11252 int len = strlen (method);
11253 int size = sizeof w->desired_matrix->method;
11254 int remaining = size - len - 1;
11256 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11257 if (len && remaining)
11259 method[len] = '|';
11260 --remaining, ++len;
11263 strncpy (method + len, buffer, remaining);
11265 if (trace_redisplay_p)
11266 fprintf (stderr, "%p (%s): %s\n",
11268 ((BUFFERP (w->buffer)
11269 && STRINGP (XBUFFER (w->buffer)->name))
11270 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11271 : "no buffer"),
11272 buffer);
11275 #endif /* GLYPH_DEBUG */
11278 /* Value is non-zero if all changes in window W, which displays
11279 current_buffer, are in the text between START and END. START is a
11280 buffer position, END is given as a distance from Z. Used in
11281 redisplay_internal for display optimization. */
11283 static INLINE int
11284 text_outside_line_unchanged_p (struct window *w,
11285 EMACS_INT start, EMACS_INT end)
11287 int unchanged_p = 1;
11289 /* If text or overlays have changed, see where. */
11290 if (XFASTINT (w->last_modified) < MODIFF
11291 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11293 /* Gap in the line? */
11294 if (GPT < start || Z - GPT < end)
11295 unchanged_p = 0;
11297 /* Changes start in front of the line, or end after it? */
11298 if (unchanged_p
11299 && (BEG_UNCHANGED < start - 1
11300 || END_UNCHANGED < end))
11301 unchanged_p = 0;
11303 /* If selective display, can't optimize if changes start at the
11304 beginning of the line. */
11305 if (unchanged_p
11306 && INTEGERP (current_buffer->selective_display)
11307 && XINT (current_buffer->selective_display) > 0
11308 && (BEG_UNCHANGED < start || GPT <= start))
11309 unchanged_p = 0;
11311 /* If there are overlays at the start or end of the line, these
11312 may have overlay strings with newlines in them. A change at
11313 START, for instance, may actually concern the display of such
11314 overlay strings as well, and they are displayed on different
11315 lines. So, quickly rule out this case. (For the future, it
11316 might be desirable to implement something more telling than
11317 just BEG/END_UNCHANGED.) */
11318 if (unchanged_p)
11320 if (BEG + BEG_UNCHANGED == start
11321 && overlay_touches_p (start))
11322 unchanged_p = 0;
11323 if (END_UNCHANGED == end
11324 && overlay_touches_p (Z - end))
11325 unchanged_p = 0;
11328 /* Under bidi reordering, adding or deleting a character in the
11329 beginning of a paragraph, before the first strong directional
11330 character, can change the base direction of the paragraph (unless
11331 the buffer specifies a fixed paragraph direction), which will
11332 require to redisplay the whole paragraph. It might be worthwhile
11333 to find the paragraph limits and widen the range of redisplayed
11334 lines to that, but for now just give up this optimization. */
11335 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11336 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11337 unchanged_p = 0;
11340 return unchanged_p;
11344 /* Do a frame update, taking possible shortcuts into account. This is
11345 the main external entry point for redisplay.
11347 If the last redisplay displayed an echo area message and that message
11348 is no longer requested, we clear the echo area or bring back the
11349 mini-buffer if that is in use. */
11351 void
11352 redisplay (void)
11354 redisplay_internal (0);
11358 static Lisp_Object
11359 overlay_arrow_string_or_property (Lisp_Object var)
11361 Lisp_Object val;
11363 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11364 return val;
11366 return Voverlay_arrow_string;
11369 /* Return 1 if there are any overlay-arrows in current_buffer. */
11370 static int
11371 overlay_arrow_in_current_buffer_p (void)
11373 Lisp_Object vlist;
11375 for (vlist = Voverlay_arrow_variable_list;
11376 CONSP (vlist);
11377 vlist = XCDR (vlist))
11379 Lisp_Object var = XCAR (vlist);
11380 Lisp_Object val;
11382 if (!SYMBOLP (var))
11383 continue;
11384 val = find_symbol_value (var);
11385 if (MARKERP (val)
11386 && current_buffer == XMARKER (val)->buffer)
11387 return 1;
11389 return 0;
11393 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11394 has changed. */
11396 static int
11397 overlay_arrows_changed_p (void)
11399 Lisp_Object vlist;
11401 for (vlist = Voverlay_arrow_variable_list;
11402 CONSP (vlist);
11403 vlist = XCDR (vlist))
11405 Lisp_Object var = XCAR (vlist);
11406 Lisp_Object val, pstr;
11408 if (!SYMBOLP (var))
11409 continue;
11410 val = find_symbol_value (var);
11411 if (!MARKERP (val))
11412 continue;
11413 if (! EQ (COERCE_MARKER (val),
11414 Fget (var, Qlast_arrow_position))
11415 || ! (pstr = overlay_arrow_string_or_property (var),
11416 EQ (pstr, Fget (var, Qlast_arrow_string))))
11417 return 1;
11419 return 0;
11422 /* Mark overlay arrows to be updated on next redisplay. */
11424 static void
11425 update_overlay_arrows (int up_to_date)
11427 Lisp_Object vlist;
11429 for (vlist = Voverlay_arrow_variable_list;
11430 CONSP (vlist);
11431 vlist = XCDR (vlist))
11433 Lisp_Object var = XCAR (vlist);
11435 if (!SYMBOLP (var))
11436 continue;
11438 if (up_to_date > 0)
11440 Lisp_Object val = find_symbol_value (var);
11441 Fput (var, Qlast_arrow_position,
11442 COERCE_MARKER (val));
11443 Fput (var, Qlast_arrow_string,
11444 overlay_arrow_string_or_property (var));
11446 else if (up_to_date < 0
11447 || !NILP (Fget (var, Qlast_arrow_position)))
11449 Fput (var, Qlast_arrow_position, Qt);
11450 Fput (var, Qlast_arrow_string, Qt);
11456 /* Return overlay arrow string to display at row.
11457 Return integer (bitmap number) for arrow bitmap in left fringe.
11458 Return nil if no overlay arrow. */
11460 static Lisp_Object
11461 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11463 Lisp_Object vlist;
11465 for (vlist = Voverlay_arrow_variable_list;
11466 CONSP (vlist);
11467 vlist = XCDR (vlist))
11469 Lisp_Object var = XCAR (vlist);
11470 Lisp_Object val;
11472 if (!SYMBOLP (var))
11473 continue;
11475 val = find_symbol_value (var);
11477 if (MARKERP (val)
11478 && current_buffer == XMARKER (val)->buffer
11479 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11481 if (FRAME_WINDOW_P (it->f)
11482 /* FIXME: if ROW->reversed_p is set, this should test
11483 the right fringe, not the left one. */
11484 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11486 #ifdef HAVE_WINDOW_SYSTEM
11487 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11489 int fringe_bitmap;
11490 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11491 return make_number (fringe_bitmap);
11493 #endif
11494 return make_number (-1); /* Use default arrow bitmap */
11496 return overlay_arrow_string_or_property (var);
11500 return Qnil;
11503 /* Return 1 if point moved out of or into a composition. Otherwise
11504 return 0. PREV_BUF and PREV_PT are the last point buffer and
11505 position. BUF and PT are the current point buffer and position. */
11508 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11509 struct buffer *buf, EMACS_INT pt)
11511 EMACS_INT start, end;
11512 Lisp_Object prop;
11513 Lisp_Object buffer;
11515 XSETBUFFER (buffer, buf);
11516 /* Check a composition at the last point if point moved within the
11517 same buffer. */
11518 if (prev_buf == buf)
11520 if (prev_pt == pt)
11521 /* Point didn't move. */
11522 return 0;
11524 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11525 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11526 && COMPOSITION_VALID_P (start, end, prop)
11527 && start < prev_pt && end > prev_pt)
11528 /* The last point was within the composition. Return 1 iff
11529 point moved out of the composition. */
11530 return (pt <= start || pt >= end);
11533 /* Check a composition at the current point. */
11534 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11535 && find_composition (pt, -1, &start, &end, &prop, buffer)
11536 && COMPOSITION_VALID_P (start, end, prop)
11537 && start < pt && end > pt);
11541 /* Reconsider the setting of B->clip_changed which is displayed
11542 in window W. */
11544 static INLINE void
11545 reconsider_clip_changes (struct window *w, struct buffer *b)
11547 if (b->clip_changed
11548 && !NILP (w->window_end_valid)
11549 && w->current_matrix->buffer == b
11550 && w->current_matrix->zv == BUF_ZV (b)
11551 && w->current_matrix->begv == BUF_BEGV (b))
11552 b->clip_changed = 0;
11554 /* If display wasn't paused, and W is not a tool bar window, see if
11555 point has been moved into or out of a composition. In that case,
11556 we set b->clip_changed to 1 to force updating the screen. If
11557 b->clip_changed has already been set to 1, we can skip this
11558 check. */
11559 if (!b->clip_changed
11560 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11562 EMACS_INT pt;
11564 if (w == XWINDOW (selected_window))
11565 pt = BUF_PT (current_buffer);
11566 else
11567 pt = marker_position (w->pointm);
11569 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11570 || pt != XINT (w->last_point))
11571 && check_point_in_composition (w->current_matrix->buffer,
11572 XINT (w->last_point),
11573 XBUFFER (w->buffer), pt))
11574 b->clip_changed = 1;
11579 /* Select FRAME to forward the values of frame-local variables into C
11580 variables so that the redisplay routines can access those values
11581 directly. */
11583 static void
11584 select_frame_for_redisplay (Lisp_Object frame)
11586 Lisp_Object tail, tem;
11587 Lisp_Object old = selected_frame;
11588 struct Lisp_Symbol *sym;
11590 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11592 selected_frame = frame;
11594 do {
11595 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11596 if (CONSP (XCAR (tail))
11597 && (tem = XCAR (XCAR (tail)),
11598 SYMBOLP (tem))
11599 && (sym = indirect_variable (XSYMBOL (tem)),
11600 sym->redirect == SYMBOL_LOCALIZED)
11601 && sym->val.blv->frame_local)
11602 /* Use find_symbol_value rather than Fsymbol_value
11603 to avoid an error if it is void. */
11604 find_symbol_value (tem);
11605 } while (!EQ (frame, old) && (frame = old, 1));
11609 #define STOP_POLLING \
11610 do { if (! polling_stopped_here) stop_polling (); \
11611 polling_stopped_here = 1; } while (0)
11613 #define RESUME_POLLING \
11614 do { if (polling_stopped_here) start_polling (); \
11615 polling_stopped_here = 0; } while (0)
11618 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11619 response to any user action; therefore, we should preserve the echo
11620 area. (Actually, our caller does that job.) Perhaps in the future
11621 avoid recentering windows if it is not necessary; currently that
11622 causes some problems. */
11624 static void
11625 redisplay_internal (int preserve_echo_area)
11627 struct window *w = XWINDOW (selected_window);
11628 struct frame *f;
11629 int pause;
11630 int must_finish = 0;
11631 struct text_pos tlbufpos, tlendpos;
11632 int number_of_visible_frames;
11633 int count, count1;
11634 struct frame *sf;
11635 int polling_stopped_here = 0;
11636 Lisp_Object old_frame = selected_frame;
11638 /* Non-zero means redisplay has to consider all windows on all
11639 frames. Zero means, only selected_window is considered. */
11640 int consider_all_windows_p;
11642 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11644 /* No redisplay if running in batch mode or frame is not yet fully
11645 initialized, or redisplay is explicitly turned off by setting
11646 Vinhibit_redisplay. */
11647 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11648 || !NILP (Vinhibit_redisplay))
11649 return;
11651 /* Don't examine these until after testing Vinhibit_redisplay.
11652 When Emacs is shutting down, perhaps because its connection to
11653 X has dropped, we should not look at them at all. */
11654 f = XFRAME (w->frame);
11655 sf = SELECTED_FRAME ();
11657 if (!f->glyphs_initialized_p)
11658 return;
11660 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11661 if (popup_activated ())
11662 return;
11663 #endif
11665 /* I don't think this happens but let's be paranoid. */
11666 if (redisplaying_p)
11667 return;
11669 /* Record a function that resets redisplaying_p to its old value
11670 when we leave this function. */
11671 count = SPECPDL_INDEX ();
11672 record_unwind_protect (unwind_redisplay,
11673 Fcons (make_number (redisplaying_p), selected_frame));
11674 ++redisplaying_p;
11675 specbind (Qinhibit_free_realized_faces, Qnil);
11678 Lisp_Object tail, frame;
11680 FOR_EACH_FRAME (tail, frame)
11682 struct frame *f = XFRAME (frame);
11683 f->already_hscrolled_p = 0;
11687 retry:
11688 if (!EQ (old_frame, selected_frame)
11689 && FRAME_LIVE_P (XFRAME (old_frame)))
11690 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11691 selected_frame and selected_window to be temporarily out-of-sync so
11692 when we come back here via `goto retry', we need to resync because we
11693 may need to run Elisp code (via prepare_menu_bars). */
11694 select_frame_for_redisplay (old_frame);
11696 pause = 0;
11697 reconsider_clip_changes (w, current_buffer);
11698 last_escape_glyph_frame = NULL;
11699 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11700 last_glyphless_glyph_frame = NULL;
11701 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
11703 /* If new fonts have been loaded that make a glyph matrix adjustment
11704 necessary, do it. */
11705 if (fonts_changed_p)
11707 adjust_glyphs (NULL);
11708 ++windows_or_buffers_changed;
11709 fonts_changed_p = 0;
11712 /* If face_change_count is non-zero, init_iterator will free all
11713 realized faces, which includes the faces referenced from current
11714 matrices. So, we can't reuse current matrices in this case. */
11715 if (face_change_count)
11716 ++windows_or_buffers_changed;
11718 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11719 && FRAME_TTY (sf)->previous_frame != sf)
11721 /* Since frames on a single ASCII terminal share the same
11722 display area, displaying a different frame means redisplay
11723 the whole thing. */
11724 windows_or_buffers_changed++;
11725 SET_FRAME_GARBAGED (sf);
11726 #ifndef DOS_NT
11727 set_tty_color_mode (FRAME_TTY (sf), sf);
11728 #endif
11729 FRAME_TTY (sf)->previous_frame = sf;
11732 /* Set the visible flags for all frames. Do this before checking
11733 for resized or garbaged frames; they want to know if their frames
11734 are visible. See the comment in frame.h for
11735 FRAME_SAMPLE_VISIBILITY. */
11737 Lisp_Object tail, frame;
11739 number_of_visible_frames = 0;
11741 FOR_EACH_FRAME (tail, frame)
11743 struct frame *f = XFRAME (frame);
11745 FRAME_SAMPLE_VISIBILITY (f);
11746 if (FRAME_VISIBLE_P (f))
11747 ++number_of_visible_frames;
11748 clear_desired_matrices (f);
11752 /* Notice any pending interrupt request to change frame size. */
11753 do_pending_window_change (1);
11755 /* Clear frames marked as garbaged. */
11756 if (frame_garbaged)
11757 clear_garbaged_frames ();
11759 /* Build menubar and tool-bar items. */
11760 if (NILP (Vmemory_full))
11761 prepare_menu_bars ();
11763 if (windows_or_buffers_changed)
11764 update_mode_lines++;
11766 /* Detect case that we need to write or remove a star in the mode line. */
11767 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11769 w->update_mode_line = Qt;
11770 if (buffer_shared > 1)
11771 update_mode_lines++;
11774 /* Avoid invocation of point motion hooks by `current_column' below. */
11775 count1 = SPECPDL_INDEX ();
11776 specbind (Qinhibit_point_motion_hooks, Qt);
11778 /* If %c is in the mode line, update it if needed. */
11779 if (!NILP (w->column_number_displayed)
11780 /* This alternative quickly identifies a common case
11781 where no change is needed. */
11782 && !(PT == XFASTINT (w->last_point)
11783 && XFASTINT (w->last_modified) >= MODIFF
11784 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11785 && (XFASTINT (w->column_number_displayed)
11786 != (int) current_column ())) /* iftc */
11787 w->update_mode_line = Qt;
11789 unbind_to (count1, Qnil);
11791 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11793 /* The variable buffer_shared is set in redisplay_window and
11794 indicates that we redisplay a buffer in different windows. See
11795 there. */
11796 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11797 || cursor_type_changed);
11799 /* If specs for an arrow have changed, do thorough redisplay
11800 to ensure we remove any arrow that should no longer exist. */
11801 if (overlay_arrows_changed_p ())
11802 consider_all_windows_p = windows_or_buffers_changed = 1;
11804 /* Normally the message* functions will have already displayed and
11805 updated the echo area, but the frame may have been trashed, or
11806 the update may have been preempted, so display the echo area
11807 again here. Checking message_cleared_p captures the case that
11808 the echo area should be cleared. */
11809 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11810 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11811 || (message_cleared_p
11812 && minibuf_level == 0
11813 /* If the mini-window is currently selected, this means the
11814 echo-area doesn't show through. */
11815 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11817 int window_height_changed_p = echo_area_display (0);
11818 must_finish = 1;
11820 /* If we don't display the current message, don't clear the
11821 message_cleared_p flag, because, if we did, we wouldn't clear
11822 the echo area in the next redisplay which doesn't preserve
11823 the echo area. */
11824 if (!display_last_displayed_message_p)
11825 message_cleared_p = 0;
11827 if (fonts_changed_p)
11828 goto retry;
11829 else if (window_height_changed_p)
11831 consider_all_windows_p = 1;
11832 ++update_mode_lines;
11833 ++windows_or_buffers_changed;
11835 /* If window configuration was changed, frames may have been
11836 marked garbaged. Clear them or we will experience
11837 surprises wrt scrolling. */
11838 if (frame_garbaged)
11839 clear_garbaged_frames ();
11842 else if (EQ (selected_window, minibuf_window)
11843 && (current_buffer->clip_changed
11844 || XFASTINT (w->last_modified) < MODIFF
11845 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11846 && resize_mini_window (w, 0))
11848 /* Resized active mini-window to fit the size of what it is
11849 showing if its contents might have changed. */
11850 must_finish = 1;
11851 /* FIXME: this causes all frames to be updated, which seems unnecessary
11852 since only the current frame needs to be considered. This function needs
11853 to be rewritten with two variables, consider_all_windows and
11854 consider_all_frames. */
11855 consider_all_windows_p = 1;
11856 ++windows_or_buffers_changed;
11857 ++update_mode_lines;
11859 /* If window configuration was changed, frames may have been
11860 marked garbaged. Clear them or we will experience
11861 surprises wrt scrolling. */
11862 if (frame_garbaged)
11863 clear_garbaged_frames ();
11867 /* If showing the region, and mark has changed, we must redisplay
11868 the whole window. The assignment to this_line_start_pos prevents
11869 the optimization directly below this if-statement. */
11870 if (((!NILP (Vtransient_mark_mode)
11871 && !NILP (XBUFFER (w->buffer)->mark_active))
11872 != !NILP (w->region_showing))
11873 || (!NILP (w->region_showing)
11874 && !EQ (w->region_showing,
11875 Fmarker_position (XBUFFER (w->buffer)->mark))))
11876 CHARPOS (this_line_start_pos) = 0;
11878 /* Optimize the case that only the line containing the cursor in the
11879 selected window has changed. Variables starting with this_ are
11880 set in display_line and record information about the line
11881 containing the cursor. */
11882 tlbufpos = this_line_start_pos;
11883 tlendpos = this_line_end_pos;
11884 if (!consider_all_windows_p
11885 && CHARPOS (tlbufpos) > 0
11886 && NILP (w->update_mode_line)
11887 && !current_buffer->clip_changed
11888 && !current_buffer->prevent_redisplay_optimizations_p
11889 && FRAME_VISIBLE_P (XFRAME (w->frame))
11890 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11891 /* Make sure recorded data applies to current buffer, etc. */
11892 && this_line_buffer == current_buffer
11893 && current_buffer == XBUFFER (w->buffer)
11894 && NILP (w->force_start)
11895 && NILP (w->optional_new_start)
11896 /* Point must be on the line that we have info recorded about. */
11897 && PT >= CHARPOS (tlbufpos)
11898 && PT <= Z - CHARPOS (tlendpos)
11899 /* All text outside that line, including its final newline,
11900 must be unchanged. */
11901 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11902 CHARPOS (tlendpos)))
11904 if (CHARPOS (tlbufpos) > BEGV
11905 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11906 && (CHARPOS (tlbufpos) == ZV
11907 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11908 /* Former continuation line has disappeared by becoming empty. */
11909 goto cancel;
11910 else if (XFASTINT (w->last_modified) < MODIFF
11911 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11912 || MINI_WINDOW_P (w))
11914 /* We have to handle the case of continuation around a
11915 wide-column character (see the comment in indent.c around
11916 line 1340).
11918 For instance, in the following case:
11920 -------- Insert --------
11921 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11922 J_I_ ==> J_I_ `^^' are cursors.
11923 ^^ ^^
11924 -------- --------
11926 As we have to redraw the line above, we cannot use this
11927 optimization. */
11929 struct it it;
11930 int line_height_before = this_line_pixel_height;
11932 /* Note that start_display will handle the case that the
11933 line starting at tlbufpos is a continuation line. */
11934 start_display (&it, w, tlbufpos);
11936 /* Implementation note: It this still necessary? */
11937 if (it.current_x != this_line_start_x)
11938 goto cancel;
11940 TRACE ((stderr, "trying display optimization 1\n"));
11941 w->cursor.vpos = -1;
11942 overlay_arrow_seen = 0;
11943 it.vpos = this_line_vpos;
11944 it.current_y = this_line_y;
11945 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11946 display_line (&it);
11948 /* If line contains point, is not continued,
11949 and ends at same distance from eob as before, we win. */
11950 if (w->cursor.vpos >= 0
11951 /* Line is not continued, otherwise this_line_start_pos
11952 would have been set to 0 in display_line. */
11953 && CHARPOS (this_line_start_pos)
11954 /* Line ends as before. */
11955 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11956 /* Line has same height as before. Otherwise other lines
11957 would have to be shifted up or down. */
11958 && this_line_pixel_height == line_height_before)
11960 /* If this is not the window's last line, we must adjust
11961 the charstarts of the lines below. */
11962 if (it.current_y < it.last_visible_y)
11964 struct glyph_row *row
11965 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11966 EMACS_INT delta, delta_bytes;
11968 /* We used to distinguish between two cases here,
11969 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11970 when the line ends in a newline or the end of the
11971 buffer's accessible portion. But both cases did
11972 the same, so they were collapsed. */
11973 delta = (Z
11974 - CHARPOS (tlendpos)
11975 - MATRIX_ROW_START_CHARPOS (row));
11976 delta_bytes = (Z_BYTE
11977 - BYTEPOS (tlendpos)
11978 - MATRIX_ROW_START_BYTEPOS (row));
11980 increment_matrix_positions (w->current_matrix,
11981 this_line_vpos + 1,
11982 w->current_matrix->nrows,
11983 delta, delta_bytes);
11986 /* If this row displays text now but previously didn't,
11987 or vice versa, w->window_end_vpos may have to be
11988 adjusted. */
11989 if ((it.glyph_row - 1)->displays_text_p)
11991 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11992 XSETINT (w->window_end_vpos, this_line_vpos);
11994 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11995 && this_line_vpos > 0)
11996 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11997 w->window_end_valid = Qnil;
11999 /* Update hint: No need to try to scroll in update_window. */
12000 w->desired_matrix->no_scrolling_p = 1;
12002 #if GLYPH_DEBUG
12003 *w->desired_matrix->method = 0;
12004 debug_method_add (w, "optimization 1");
12005 #endif
12006 #ifdef HAVE_WINDOW_SYSTEM
12007 update_window_fringes (w, 0);
12008 #endif
12009 goto update;
12011 else
12012 goto cancel;
12014 else if (/* Cursor position hasn't changed. */
12015 PT == XFASTINT (w->last_point)
12016 /* Make sure the cursor was last displayed
12017 in this window. Otherwise we have to reposition it. */
12018 && 0 <= w->cursor.vpos
12019 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12021 if (!must_finish)
12023 do_pending_window_change (1);
12025 /* We used to always goto end_of_redisplay here, but this
12026 isn't enough if we have a blinking cursor. */
12027 if (w->cursor_off_p == w->last_cursor_off_p)
12028 goto end_of_redisplay;
12030 goto update;
12032 /* If highlighting the region, or if the cursor is in the echo area,
12033 then we can't just move the cursor. */
12034 else if (! (!NILP (Vtransient_mark_mode)
12035 && !NILP (current_buffer->mark_active))
12036 && (EQ (selected_window, current_buffer->last_selected_window)
12037 || highlight_nonselected_windows)
12038 && NILP (w->region_showing)
12039 && NILP (Vshow_trailing_whitespace)
12040 && !cursor_in_echo_area)
12042 struct it it;
12043 struct glyph_row *row;
12045 /* Skip from tlbufpos to PT and see where it is. Note that
12046 PT may be in invisible text. If so, we will end at the
12047 next visible position. */
12048 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12049 NULL, DEFAULT_FACE_ID);
12050 it.current_x = this_line_start_x;
12051 it.current_y = this_line_y;
12052 it.vpos = this_line_vpos;
12054 /* The call to move_it_to stops in front of PT, but
12055 moves over before-strings. */
12056 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12058 if (it.vpos == this_line_vpos
12059 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12060 row->enabled_p))
12062 xassert (this_line_vpos == it.vpos);
12063 xassert (this_line_y == it.current_y);
12064 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12065 #if GLYPH_DEBUG
12066 *w->desired_matrix->method = 0;
12067 debug_method_add (w, "optimization 3");
12068 #endif
12069 goto update;
12071 else
12072 goto cancel;
12075 cancel:
12076 /* Text changed drastically or point moved off of line. */
12077 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12080 CHARPOS (this_line_start_pos) = 0;
12081 consider_all_windows_p |= buffer_shared > 1;
12082 ++clear_face_cache_count;
12083 #ifdef HAVE_WINDOW_SYSTEM
12084 ++clear_image_cache_count;
12085 #endif
12087 /* Build desired matrices, and update the display. If
12088 consider_all_windows_p is non-zero, do it for all windows on all
12089 frames. Otherwise do it for selected_window, only. */
12091 if (consider_all_windows_p)
12093 Lisp_Object tail, frame;
12095 FOR_EACH_FRAME (tail, frame)
12096 XFRAME (frame)->updated_p = 0;
12098 /* Recompute # windows showing selected buffer. This will be
12099 incremented each time such a window is displayed. */
12100 buffer_shared = 0;
12102 FOR_EACH_FRAME (tail, frame)
12104 struct frame *f = XFRAME (frame);
12106 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12108 if (! EQ (frame, selected_frame))
12109 /* Select the frame, for the sake of frame-local
12110 variables. */
12111 select_frame_for_redisplay (frame);
12113 /* Mark all the scroll bars to be removed; we'll redeem
12114 the ones we want when we redisplay their windows. */
12115 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12116 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12118 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12119 redisplay_windows (FRAME_ROOT_WINDOW (f));
12121 /* The X error handler may have deleted that frame. */
12122 if (!FRAME_LIVE_P (f))
12123 continue;
12125 /* Any scroll bars which redisplay_windows should have
12126 nuked should now go away. */
12127 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12128 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12130 /* If fonts changed, display again. */
12131 /* ??? rms: I suspect it is a mistake to jump all the way
12132 back to retry here. It should just retry this frame. */
12133 if (fonts_changed_p)
12134 goto retry;
12136 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12138 /* See if we have to hscroll. */
12139 if (!f->already_hscrolled_p)
12141 f->already_hscrolled_p = 1;
12142 if (hscroll_windows (f->root_window))
12143 goto retry;
12146 /* Prevent various kinds of signals during display
12147 update. stdio is not robust about handling
12148 signals, which can cause an apparent I/O
12149 error. */
12150 if (interrupt_input)
12151 unrequest_sigio ();
12152 STOP_POLLING;
12154 /* Update the display. */
12155 set_window_update_flags (XWINDOW (f->root_window), 1);
12156 pause |= update_frame (f, 0, 0);
12157 f->updated_p = 1;
12162 if (!EQ (old_frame, selected_frame)
12163 && FRAME_LIVE_P (XFRAME (old_frame)))
12164 /* We played a bit fast-and-loose above and allowed selected_frame
12165 and selected_window to be temporarily out-of-sync but let's make
12166 sure this stays contained. */
12167 select_frame_for_redisplay (old_frame);
12168 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12170 if (!pause)
12172 /* Do the mark_window_display_accurate after all windows have
12173 been redisplayed because this call resets flags in buffers
12174 which are needed for proper redisplay. */
12175 FOR_EACH_FRAME (tail, frame)
12177 struct frame *f = XFRAME (frame);
12178 if (f->updated_p)
12180 mark_window_display_accurate (f->root_window, 1);
12181 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12182 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12187 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12189 Lisp_Object mini_window;
12190 struct frame *mini_frame;
12192 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12193 /* Use list_of_error, not Qerror, so that
12194 we catch only errors and don't run the debugger. */
12195 internal_condition_case_1 (redisplay_window_1, selected_window,
12196 list_of_error,
12197 redisplay_window_error);
12199 /* Compare desired and current matrices, perform output. */
12201 update:
12202 /* If fonts changed, display again. */
12203 if (fonts_changed_p)
12204 goto retry;
12206 /* Prevent various kinds of signals during display update.
12207 stdio is not robust about handling signals,
12208 which can cause an apparent I/O error. */
12209 if (interrupt_input)
12210 unrequest_sigio ();
12211 STOP_POLLING;
12213 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12215 if (hscroll_windows (selected_window))
12216 goto retry;
12218 XWINDOW (selected_window)->must_be_updated_p = 1;
12219 pause = update_frame (sf, 0, 0);
12222 /* We may have called echo_area_display at the top of this
12223 function. If the echo area is on another frame, that may
12224 have put text on a frame other than the selected one, so the
12225 above call to update_frame would not have caught it. Catch
12226 it here. */
12227 mini_window = FRAME_MINIBUF_WINDOW (sf);
12228 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12230 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12232 XWINDOW (mini_window)->must_be_updated_p = 1;
12233 pause |= update_frame (mini_frame, 0, 0);
12234 if (!pause && hscroll_windows (mini_window))
12235 goto retry;
12239 /* If display was paused because of pending input, make sure we do a
12240 thorough update the next time. */
12241 if (pause)
12243 /* Prevent the optimization at the beginning of
12244 redisplay_internal that tries a single-line update of the
12245 line containing the cursor in the selected window. */
12246 CHARPOS (this_line_start_pos) = 0;
12248 /* Let the overlay arrow be updated the next time. */
12249 update_overlay_arrows (0);
12251 /* If we pause after scrolling, some rows in the current
12252 matrices of some windows are not valid. */
12253 if (!WINDOW_FULL_WIDTH_P (w)
12254 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12255 update_mode_lines = 1;
12257 else
12259 if (!consider_all_windows_p)
12261 /* This has already been done above if
12262 consider_all_windows_p is set. */
12263 mark_window_display_accurate_1 (w, 1);
12265 /* Say overlay arrows are up to date. */
12266 update_overlay_arrows (1);
12268 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12269 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12272 update_mode_lines = 0;
12273 windows_or_buffers_changed = 0;
12274 cursor_type_changed = 0;
12277 /* Start SIGIO interrupts coming again. Having them off during the
12278 code above makes it less likely one will discard output, but not
12279 impossible, since there might be stuff in the system buffer here.
12280 But it is much hairier to try to do anything about that. */
12281 if (interrupt_input)
12282 request_sigio ();
12283 RESUME_POLLING;
12285 /* If a frame has become visible which was not before, redisplay
12286 again, so that we display it. Expose events for such a frame
12287 (which it gets when becoming visible) don't call the parts of
12288 redisplay constructing glyphs, so simply exposing a frame won't
12289 display anything in this case. So, we have to display these
12290 frames here explicitly. */
12291 if (!pause)
12293 Lisp_Object tail, frame;
12294 int new_count = 0;
12296 FOR_EACH_FRAME (tail, frame)
12298 int this_is_visible = 0;
12300 if (XFRAME (frame)->visible)
12301 this_is_visible = 1;
12302 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12303 if (XFRAME (frame)->visible)
12304 this_is_visible = 1;
12306 if (this_is_visible)
12307 new_count++;
12310 if (new_count != number_of_visible_frames)
12311 windows_or_buffers_changed++;
12314 /* Change frame size now if a change is pending. */
12315 do_pending_window_change (1);
12317 /* If we just did a pending size change, or have additional
12318 visible frames, redisplay again. */
12319 if (windows_or_buffers_changed && !pause)
12320 goto retry;
12322 /* Clear the face and image caches.
12324 We used to do this only if consider_all_windows_p. But the cache
12325 needs to be cleared if a timer creates images in the current
12326 buffer (e.g. the test case in Bug#6230). */
12328 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12330 clear_face_cache (0);
12331 clear_face_cache_count = 0;
12334 #ifdef HAVE_WINDOW_SYSTEM
12335 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12337 clear_image_caches (Qnil);
12338 clear_image_cache_count = 0;
12340 #endif /* HAVE_WINDOW_SYSTEM */
12342 end_of_redisplay:
12343 unbind_to (count, Qnil);
12344 RESUME_POLLING;
12348 /* Redisplay, but leave alone any recent echo area message unless
12349 another message has been requested in its place.
12351 This is useful in situations where you need to redisplay but no
12352 user action has occurred, making it inappropriate for the message
12353 area to be cleared. See tracking_off and
12354 wait_reading_process_output for examples of these situations.
12356 FROM_WHERE is an integer saying from where this function was
12357 called. This is useful for debugging. */
12359 void
12360 redisplay_preserve_echo_area (int from_where)
12362 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12364 if (!NILP (echo_area_buffer[1]))
12366 /* We have a previously displayed message, but no current
12367 message. Redisplay the previous message. */
12368 display_last_displayed_message_p = 1;
12369 redisplay_internal (1);
12370 display_last_displayed_message_p = 0;
12372 else
12373 redisplay_internal (1);
12375 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12376 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12377 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12381 /* Function registered with record_unwind_protect in
12382 redisplay_internal. Reset redisplaying_p to the value it had
12383 before redisplay_internal was called, and clear
12384 prevent_freeing_realized_faces_p. It also selects the previously
12385 selected frame, unless it has been deleted (by an X connection
12386 failure during redisplay, for example). */
12388 static Lisp_Object
12389 unwind_redisplay (Lisp_Object val)
12391 Lisp_Object old_redisplaying_p, old_frame;
12393 old_redisplaying_p = XCAR (val);
12394 redisplaying_p = XFASTINT (old_redisplaying_p);
12395 old_frame = XCDR (val);
12396 if (! EQ (old_frame, selected_frame)
12397 && FRAME_LIVE_P (XFRAME (old_frame)))
12398 select_frame_for_redisplay (old_frame);
12399 return Qnil;
12403 /* Mark the display of window W as accurate or inaccurate. If
12404 ACCURATE_P is non-zero mark display of W as accurate. If
12405 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12406 redisplay_internal is called. */
12408 static void
12409 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12411 if (BUFFERP (w->buffer))
12413 struct buffer *b = XBUFFER (w->buffer);
12415 w->last_modified
12416 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12417 w->last_overlay_modified
12418 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12419 w->last_had_star
12420 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12422 if (accurate_p)
12424 b->clip_changed = 0;
12425 b->prevent_redisplay_optimizations_p = 0;
12427 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12428 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12429 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12430 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12432 w->current_matrix->buffer = b;
12433 w->current_matrix->begv = BUF_BEGV (b);
12434 w->current_matrix->zv = BUF_ZV (b);
12436 w->last_cursor = w->cursor;
12437 w->last_cursor_off_p = w->cursor_off_p;
12439 if (w == XWINDOW (selected_window))
12440 w->last_point = make_number (BUF_PT (b));
12441 else
12442 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12446 if (accurate_p)
12448 w->window_end_valid = w->buffer;
12449 w->update_mode_line = Qnil;
12454 /* Mark the display of windows in the window tree rooted at WINDOW as
12455 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12456 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12457 be redisplayed the next time redisplay_internal is called. */
12459 void
12460 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12462 struct window *w;
12464 for (; !NILP (window); window = w->next)
12466 w = XWINDOW (window);
12467 mark_window_display_accurate_1 (w, accurate_p);
12469 if (!NILP (w->vchild))
12470 mark_window_display_accurate (w->vchild, accurate_p);
12471 if (!NILP (w->hchild))
12472 mark_window_display_accurate (w->hchild, accurate_p);
12475 if (accurate_p)
12477 update_overlay_arrows (1);
12479 else
12481 /* Force a thorough redisplay the next time by setting
12482 last_arrow_position and last_arrow_string to t, which is
12483 unequal to any useful value of Voverlay_arrow_... */
12484 update_overlay_arrows (-1);
12489 /* Return value in display table DP (Lisp_Char_Table *) for character
12490 C. Since a display table doesn't have any parent, we don't have to
12491 follow parent. Do not call this function directly but use the
12492 macro DISP_CHAR_VECTOR. */
12494 Lisp_Object
12495 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12497 Lisp_Object val;
12499 if (ASCII_CHAR_P (c))
12501 val = dp->ascii;
12502 if (SUB_CHAR_TABLE_P (val))
12503 val = XSUB_CHAR_TABLE (val)->contents[c];
12505 else
12507 Lisp_Object table;
12509 XSETCHAR_TABLE (table, dp);
12510 val = char_table_ref (table, c);
12512 if (NILP (val))
12513 val = dp->defalt;
12514 return val;
12519 /***********************************************************************
12520 Window Redisplay
12521 ***********************************************************************/
12523 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12525 static void
12526 redisplay_windows (Lisp_Object window)
12528 while (!NILP (window))
12530 struct window *w = XWINDOW (window);
12532 if (!NILP (w->hchild))
12533 redisplay_windows (w->hchild);
12534 else if (!NILP (w->vchild))
12535 redisplay_windows (w->vchild);
12536 else if (!NILP (w->buffer))
12538 displayed_buffer = XBUFFER (w->buffer);
12539 /* Use list_of_error, not Qerror, so that
12540 we catch only errors and don't run the debugger. */
12541 internal_condition_case_1 (redisplay_window_0, window,
12542 list_of_error,
12543 redisplay_window_error);
12546 window = w->next;
12550 static Lisp_Object
12551 redisplay_window_error (Lisp_Object ignore)
12553 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12554 return Qnil;
12557 static Lisp_Object
12558 redisplay_window_0 (Lisp_Object window)
12560 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12561 redisplay_window (window, 0);
12562 return Qnil;
12565 static Lisp_Object
12566 redisplay_window_1 (Lisp_Object window)
12568 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12569 redisplay_window (window, 1);
12570 return Qnil;
12574 /* Increment GLYPH until it reaches END or CONDITION fails while
12575 adding (GLYPH)->pixel_width to X. */
12577 #define SKIP_GLYPHS(glyph, end, x, condition) \
12578 do \
12580 (x) += (glyph)->pixel_width; \
12581 ++(glyph); \
12583 while ((glyph) < (end) && (condition))
12586 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12587 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12588 which positions recorded in ROW differ from current buffer
12589 positions.
12591 Return 0 if cursor is not on this row, 1 otherwise. */
12594 set_cursor_from_row (struct window *w, struct glyph_row *row,
12595 struct glyph_matrix *matrix,
12596 EMACS_INT delta, EMACS_INT delta_bytes,
12597 int dy, int dvpos)
12599 struct glyph *glyph = row->glyphs[TEXT_AREA];
12600 struct glyph *end = glyph + row->used[TEXT_AREA];
12601 struct glyph *cursor = NULL;
12602 /* The last known character position in row. */
12603 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12604 int x = row->x;
12605 EMACS_INT pt_old = PT - delta;
12606 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12607 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12608 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12609 /* A glyph beyond the edge of TEXT_AREA which we should never
12610 touch. */
12611 struct glyph *glyphs_end = end;
12612 /* Non-zero means we've found a match for cursor position, but that
12613 glyph has the avoid_cursor_p flag set. */
12614 int match_with_avoid_cursor = 0;
12615 /* Non-zero means we've seen at least one glyph that came from a
12616 display string. */
12617 int string_seen = 0;
12618 /* Largest and smalles buffer positions seen so far during scan of
12619 glyph row. */
12620 EMACS_INT bpos_max = pos_before;
12621 EMACS_INT bpos_min = pos_after;
12622 /* Last buffer position covered by an overlay string with an integer
12623 `cursor' property. */
12624 EMACS_INT bpos_covered = 0;
12626 /* Skip over glyphs not having an object at the start and the end of
12627 the row. These are special glyphs like truncation marks on
12628 terminal frames. */
12629 if (row->displays_text_p)
12631 if (!row->reversed_p)
12633 while (glyph < end
12634 && INTEGERP (glyph->object)
12635 && glyph->charpos < 0)
12637 x += glyph->pixel_width;
12638 ++glyph;
12640 while (end > glyph
12641 && INTEGERP ((end - 1)->object)
12642 /* CHARPOS is zero for blanks and stretch glyphs
12643 inserted by extend_face_to_end_of_line. */
12644 && (end - 1)->charpos <= 0)
12645 --end;
12646 glyph_before = glyph - 1;
12647 glyph_after = end;
12649 else
12651 struct glyph *g;
12653 /* If the glyph row is reversed, we need to process it from back
12654 to front, so swap the edge pointers. */
12655 glyphs_end = end = glyph - 1;
12656 glyph += row->used[TEXT_AREA] - 1;
12658 while (glyph > end + 1
12659 && INTEGERP (glyph->object)
12660 && glyph->charpos < 0)
12662 --glyph;
12663 x -= glyph->pixel_width;
12665 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12666 --glyph;
12667 /* By default, in reversed rows we put the cursor on the
12668 rightmost (first in the reading order) glyph. */
12669 for (g = end + 1; g < glyph; g++)
12670 x += g->pixel_width;
12671 while (end < glyph
12672 && INTEGERP ((end + 1)->object)
12673 && (end + 1)->charpos <= 0)
12674 ++end;
12675 glyph_before = glyph + 1;
12676 glyph_after = end;
12679 else if (row->reversed_p)
12681 /* In R2L rows that don't display text, put the cursor on the
12682 rightmost glyph. Case in point: an empty last line that is
12683 part of an R2L paragraph. */
12684 cursor = end - 1;
12685 /* Avoid placing the cursor on the last glyph of the row, where
12686 on terminal frames we hold the vertical border between
12687 adjacent windows. */
12688 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12689 && !WINDOW_RIGHTMOST_P (w)
12690 && cursor == row->glyphs[LAST_AREA] - 1)
12691 cursor--;
12692 x = -1; /* will be computed below, at label compute_x */
12695 /* Step 1: Try to find the glyph whose character position
12696 corresponds to point. If that's not possible, find 2 glyphs
12697 whose character positions are the closest to point, one before
12698 point, the other after it. */
12699 if (!row->reversed_p)
12700 while (/* not marched to end of glyph row */
12701 glyph < end
12702 /* glyph was not inserted by redisplay for internal purposes */
12703 && !INTEGERP (glyph->object))
12705 if (BUFFERP (glyph->object))
12707 EMACS_INT dpos = glyph->charpos - pt_old;
12709 if (glyph->charpos > bpos_max)
12710 bpos_max = glyph->charpos;
12711 if (glyph->charpos < bpos_min)
12712 bpos_min = glyph->charpos;
12713 if (!glyph->avoid_cursor_p)
12715 /* If we hit point, we've found the glyph on which to
12716 display the cursor. */
12717 if (dpos == 0)
12719 match_with_avoid_cursor = 0;
12720 break;
12722 /* See if we've found a better approximation to
12723 POS_BEFORE or to POS_AFTER. Note that we want the
12724 first (leftmost) glyph of all those that are the
12725 closest from below, and the last (rightmost) of all
12726 those from above. */
12727 if (0 > dpos && dpos > pos_before - pt_old)
12729 pos_before = glyph->charpos;
12730 glyph_before = glyph;
12732 else if (0 < dpos && dpos <= pos_after - pt_old)
12734 pos_after = glyph->charpos;
12735 glyph_after = glyph;
12738 else if (dpos == 0)
12739 match_with_avoid_cursor = 1;
12741 else if (STRINGP (glyph->object))
12743 Lisp_Object chprop;
12744 EMACS_INT glyph_pos = glyph->charpos;
12746 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12747 glyph->object);
12748 if (INTEGERP (chprop))
12750 bpos_covered = bpos_max + XINT (chprop);
12751 /* If the `cursor' property covers buffer positions up
12752 to and including point, we should display cursor on
12753 this glyph. Note that overlays and text properties
12754 with string values stop bidi reordering, so every
12755 buffer position to the left of the string is always
12756 smaller than any position to the right of the
12757 string. Therefore, if a `cursor' property on one
12758 of the string's characters has an integer value, we
12759 will break out of the loop below _before_ we get to
12760 the position match above. IOW, integer values of
12761 the `cursor' property override the "exact match for
12762 point" strategy of positioning the cursor. */
12763 /* Implementation note: bpos_max == pt_old when, e.g.,
12764 we are in an empty line, where bpos_max is set to
12765 MATRIX_ROW_START_CHARPOS, see above. */
12766 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12768 cursor = glyph;
12769 break;
12773 string_seen = 1;
12775 x += glyph->pixel_width;
12776 ++glyph;
12778 else if (glyph > end) /* row is reversed */
12779 while (!INTEGERP (glyph->object))
12781 if (BUFFERP (glyph->object))
12783 EMACS_INT dpos = glyph->charpos - pt_old;
12785 if (glyph->charpos > bpos_max)
12786 bpos_max = glyph->charpos;
12787 if (glyph->charpos < bpos_min)
12788 bpos_min = glyph->charpos;
12789 if (!glyph->avoid_cursor_p)
12791 if (dpos == 0)
12793 match_with_avoid_cursor = 0;
12794 break;
12796 if (0 > dpos && dpos > pos_before - pt_old)
12798 pos_before = glyph->charpos;
12799 glyph_before = glyph;
12801 else if (0 < dpos && dpos <= pos_after - pt_old)
12803 pos_after = glyph->charpos;
12804 glyph_after = glyph;
12807 else if (dpos == 0)
12808 match_with_avoid_cursor = 1;
12810 else if (STRINGP (glyph->object))
12812 Lisp_Object chprop;
12813 EMACS_INT glyph_pos = glyph->charpos;
12815 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12816 glyph->object);
12817 if (INTEGERP (chprop))
12819 bpos_covered = bpos_max + XINT (chprop);
12820 /* If the `cursor' property covers buffer positions up
12821 to and including point, we should display cursor on
12822 this glyph. */
12823 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12825 cursor = glyph;
12826 break;
12829 string_seen = 1;
12831 --glyph;
12832 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12834 x--; /* can't use any pixel_width */
12835 break;
12837 x -= glyph->pixel_width;
12840 /* Step 2: If we didn't find an exact match for point, we need to
12841 look for a proper place to put the cursor among glyphs between
12842 GLYPH_BEFORE and GLYPH_AFTER. */
12843 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12844 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12845 && bpos_covered < pt_old)
12847 /* An empty line has a single glyph whose OBJECT is zero and
12848 whose CHARPOS is the position of a newline on that line.
12849 Note that on a TTY, there are more glyphs after that, which
12850 were produced by extend_face_to_end_of_line, but their
12851 CHARPOS is zero or negative. */
12852 int empty_line_p =
12853 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12854 && INTEGERP (glyph->object) && glyph->charpos > 0;
12856 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12858 EMACS_INT ellipsis_pos;
12860 /* Scan back over the ellipsis glyphs. */
12861 if (!row->reversed_p)
12863 ellipsis_pos = (glyph - 1)->charpos;
12864 while (glyph > row->glyphs[TEXT_AREA]
12865 && (glyph - 1)->charpos == ellipsis_pos)
12866 glyph--, x -= glyph->pixel_width;
12867 /* That loop always goes one position too far, including
12868 the glyph before the ellipsis. So scan forward over
12869 that one. */
12870 x += glyph->pixel_width;
12871 glyph++;
12873 else /* row is reversed */
12875 ellipsis_pos = (glyph + 1)->charpos;
12876 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12877 && (glyph + 1)->charpos == ellipsis_pos)
12878 glyph++, x += glyph->pixel_width;
12879 x -= glyph->pixel_width;
12880 glyph--;
12883 else if (match_with_avoid_cursor
12884 /* A truncated row may not include PT among its
12885 character positions. Setting the cursor inside the
12886 scroll margin will trigger recalculation of hscroll
12887 in hscroll_window_tree. */
12888 || (row->truncated_on_left_p && pt_old < bpos_min)
12889 || (row->truncated_on_right_p && pt_old > bpos_max)
12890 /* Zero-width characters produce no glyphs. */
12891 || (!string_seen
12892 && !empty_line_p
12893 && (row->reversed_p
12894 ? glyph_after > glyphs_end
12895 : glyph_after < glyphs_end)))
12897 cursor = glyph_after;
12898 x = -1;
12900 else if (string_seen)
12902 int incr = row->reversed_p ? -1 : +1;
12904 /* Need to find the glyph that came out of a string which is
12905 present at point. That glyph is somewhere between
12906 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12907 positioned between POS_BEFORE and POS_AFTER in the
12908 buffer. */
12909 struct glyph *stop = glyph_after;
12910 EMACS_INT pos = pos_before;
12912 x = -1;
12913 for (glyph = glyph_before + incr;
12914 row->reversed_p ? glyph > stop : glyph < stop; )
12917 /* Any glyphs that come from the buffer are here because
12918 of bidi reordering. Skip them, and only pay
12919 attention to glyphs that came from some string. */
12920 if (STRINGP (glyph->object))
12922 Lisp_Object str;
12923 EMACS_INT tem;
12925 str = glyph->object;
12926 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12927 if (tem == 0 /* from overlay */
12928 || pos <= tem)
12930 /* If the string from which this glyph came is
12931 found in the buffer at point, then we've
12932 found the glyph we've been looking for. If
12933 it comes from an overlay (tem == 0), and it
12934 has the `cursor' property on one of its
12935 glyphs, record that glyph as a candidate for
12936 displaying the cursor. (As in the
12937 unidirectional version, we will display the
12938 cursor on the last candidate we find.) */
12939 if (tem == 0 || tem == pt_old)
12941 /* The glyphs from this string could have
12942 been reordered. Find the one with the
12943 smallest string position. Or there could
12944 be a character in the string with the
12945 `cursor' property, which means display
12946 cursor on that character's glyph. */
12947 EMACS_INT strpos = glyph->charpos;
12949 if (tem)
12950 cursor = glyph;
12951 for ( ;
12952 (row->reversed_p ? glyph > stop : glyph < stop)
12953 && EQ (glyph->object, str);
12954 glyph += incr)
12956 Lisp_Object cprop;
12957 EMACS_INT gpos = glyph->charpos;
12959 cprop = Fget_char_property (make_number (gpos),
12960 Qcursor,
12961 glyph->object);
12962 if (!NILP (cprop))
12964 cursor = glyph;
12965 break;
12967 if (tem && glyph->charpos < strpos)
12969 strpos = glyph->charpos;
12970 cursor = glyph;
12974 if (tem == pt_old)
12975 goto compute_x;
12977 if (tem)
12978 pos = tem + 1; /* don't find previous instances */
12980 /* This string is not what we want; skip all of the
12981 glyphs that came from it. */
12982 while ((row->reversed_p ? glyph > stop : glyph < stop)
12983 && EQ (glyph->object, str))
12984 glyph += incr;
12986 else
12987 glyph += incr;
12990 /* If we reached the end of the line, and END was from a string,
12991 the cursor is not on this line. */
12992 if (cursor == NULL
12993 && (row->reversed_p ? glyph <= end : glyph >= end)
12994 && STRINGP (end->object)
12995 && row->continued_p)
12996 return 0;
13000 compute_x:
13001 if (cursor != NULL)
13002 glyph = cursor;
13003 if (x < 0)
13005 struct glyph *g;
13007 /* Need to compute x that corresponds to GLYPH. */
13008 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13010 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13011 abort ();
13012 x += g->pixel_width;
13016 /* ROW could be part of a continued line, which, under bidi
13017 reordering, might have other rows whose start and end charpos
13018 occlude point. Only set w->cursor if we found a better
13019 approximation to the cursor position than we have from previously
13020 examined candidate rows belonging to the same continued line. */
13021 if (/* we already have a candidate row */
13022 w->cursor.vpos >= 0
13023 /* that candidate is not the row we are processing */
13024 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13025 /* the row we are processing is part of a continued line */
13026 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13027 /* Make sure cursor.vpos specifies a row whose start and end
13028 charpos occlude point. This is because some callers of this
13029 function leave cursor.vpos at the row where the cursor was
13030 displayed during the last redisplay cycle. */
13031 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13032 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13034 struct glyph *g1 =
13035 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13037 /* Don't consider glyphs that are outside TEXT_AREA. */
13038 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13039 return 0;
13040 /* Keep the candidate whose buffer position is the closest to
13041 point. */
13042 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13043 w->cursor.hpos >= 0
13044 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13045 && BUFFERP (g1->object)
13046 && (g1->charpos == pt_old /* an exact match always wins */
13047 || (BUFFERP (glyph->object)
13048 && eabs (g1->charpos - pt_old)
13049 < eabs (glyph->charpos - pt_old))))
13050 return 0;
13051 /* If this candidate gives an exact match, use that. */
13052 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13053 /* Otherwise, keep the candidate that comes from a row
13054 spanning less buffer positions. This may win when one or
13055 both candidate positions are on glyphs that came from
13056 display strings, for which we cannot compare buffer
13057 positions. */
13058 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13059 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13060 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13061 return 0;
13063 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13064 w->cursor.x = x;
13065 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13066 w->cursor.y = row->y + dy;
13068 if (w == XWINDOW (selected_window))
13070 if (!row->continued_p
13071 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13072 && row->x == 0)
13074 this_line_buffer = XBUFFER (w->buffer);
13076 CHARPOS (this_line_start_pos)
13077 = MATRIX_ROW_START_CHARPOS (row) + delta;
13078 BYTEPOS (this_line_start_pos)
13079 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13081 CHARPOS (this_line_end_pos)
13082 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13083 BYTEPOS (this_line_end_pos)
13084 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13086 this_line_y = w->cursor.y;
13087 this_line_pixel_height = row->height;
13088 this_line_vpos = w->cursor.vpos;
13089 this_line_start_x = row->x;
13091 else
13092 CHARPOS (this_line_start_pos) = 0;
13095 return 1;
13099 /* Run window scroll functions, if any, for WINDOW with new window
13100 start STARTP. Sets the window start of WINDOW to that position.
13102 We assume that the window's buffer is really current. */
13104 static INLINE struct text_pos
13105 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13107 struct window *w = XWINDOW (window);
13108 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13110 if (current_buffer != XBUFFER (w->buffer))
13111 abort ();
13113 if (!NILP (Vwindow_scroll_functions))
13115 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13116 make_number (CHARPOS (startp)));
13117 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13118 /* In case the hook functions switch buffers. */
13119 if (current_buffer != XBUFFER (w->buffer))
13120 set_buffer_internal_1 (XBUFFER (w->buffer));
13123 return startp;
13127 /* Make sure the line containing the cursor is fully visible.
13128 A value of 1 means there is nothing to be done.
13129 (Either the line is fully visible, or it cannot be made so,
13130 or we cannot tell.)
13132 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13133 is higher than window.
13135 A value of 0 means the caller should do scrolling
13136 as if point had gone off the screen. */
13138 static int
13139 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13141 struct glyph_matrix *matrix;
13142 struct glyph_row *row;
13143 int window_height;
13145 if (!make_cursor_line_fully_visible_p)
13146 return 1;
13148 /* It's not always possible to find the cursor, e.g, when a window
13149 is full of overlay strings. Don't do anything in that case. */
13150 if (w->cursor.vpos < 0)
13151 return 1;
13153 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13154 row = MATRIX_ROW (matrix, w->cursor.vpos);
13156 /* If the cursor row is not partially visible, there's nothing to do. */
13157 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13158 return 1;
13160 /* If the row the cursor is in is taller than the window's height,
13161 it's not clear what to do, so do nothing. */
13162 window_height = window_box_height (w);
13163 if (row->height >= window_height)
13165 if (!force_p || MINI_WINDOW_P (w)
13166 || w->vscroll || w->cursor.vpos == 0)
13167 return 1;
13169 return 0;
13173 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13174 non-zero means only WINDOW is redisplayed in redisplay_internal.
13175 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13176 in redisplay_window to bring a partially visible line into view in
13177 the case that only the cursor has moved.
13179 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13180 last screen line's vertical height extends past the end of the screen.
13182 Value is
13184 1 if scrolling succeeded
13186 0 if scrolling didn't find point.
13188 -1 if new fonts have been loaded so that we must interrupt
13189 redisplay, adjust glyph matrices, and try again. */
13191 enum
13193 SCROLLING_SUCCESS,
13194 SCROLLING_FAILED,
13195 SCROLLING_NEED_LARGER_MATRICES
13198 static int
13199 try_scrolling (Lisp_Object window, int just_this_one_p,
13200 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13201 int temp_scroll_step, int last_line_misfit)
13203 struct window *w = XWINDOW (window);
13204 struct frame *f = XFRAME (w->frame);
13205 struct text_pos pos, startp;
13206 struct it it;
13207 int this_scroll_margin, scroll_max, rc, height;
13208 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13209 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13210 Lisp_Object aggressive;
13211 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13213 #if GLYPH_DEBUG
13214 debug_method_add (w, "try_scrolling");
13215 #endif
13217 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13219 /* Compute scroll margin height in pixels. We scroll when point is
13220 within this distance from the top or bottom of the window. */
13221 if (scroll_margin > 0)
13222 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13223 * FRAME_LINE_HEIGHT (f);
13224 else
13225 this_scroll_margin = 0;
13227 /* Force scroll_conservatively to have a reasonable value, to avoid
13228 overflow while computing how much to scroll. Note that the user
13229 can supply scroll-conservatively equal to `most-positive-fixnum',
13230 which can be larger than INT_MAX. */
13231 if (scroll_conservatively > scroll_limit)
13233 scroll_conservatively = scroll_limit;
13234 scroll_max = INT_MAX;
13236 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13237 /* Compute how much we should try to scroll maximally to bring
13238 point into view. */
13239 scroll_max = (max (scroll_step,
13240 max (scroll_conservatively, temp_scroll_step))
13241 * FRAME_LINE_HEIGHT (f));
13242 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13243 || NUMBERP (current_buffer->scroll_up_aggressively))
13244 /* We're trying to scroll because of aggressive scrolling but no
13245 scroll_step is set. Choose an arbitrary one. */
13246 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13247 else
13248 scroll_max = 0;
13250 too_near_end:
13252 /* Decide whether to scroll down. */
13253 if (PT > CHARPOS (startp))
13255 int scroll_margin_y;
13257 /* Compute the pixel ypos of the scroll margin, then move it to
13258 either that ypos or PT, whichever comes first. */
13259 start_display (&it, w, startp);
13260 scroll_margin_y = it.last_visible_y - this_scroll_margin
13261 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13262 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13263 (MOVE_TO_POS | MOVE_TO_Y));
13265 if (PT > CHARPOS (it.current.pos))
13267 int y0 = line_bottom_y (&it);
13268 /* Compute how many pixels below window bottom to stop searching
13269 for PT. This avoids costly search for PT that is far away if
13270 the user limited scrolling by a small number of lines, but
13271 always finds PT if scroll_conservatively is set to a large
13272 number, such as most-positive-fixnum. */
13273 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13274 int y_to_move =
13275 slack >= INT_MAX - it.last_visible_y
13276 ? INT_MAX
13277 : it.last_visible_y + slack;
13279 /* Compute the distance from the scroll margin to PT or to
13280 the scroll limit, whichever comes first. This should
13281 include the height of the cursor line, to make that line
13282 fully visible. */
13283 move_it_to (&it, PT, -1, y_to_move,
13284 -1, MOVE_TO_POS | MOVE_TO_Y);
13285 dy = line_bottom_y (&it) - y0;
13287 if (dy > scroll_max)
13288 return SCROLLING_FAILED;
13290 scroll_down_p = 1;
13294 if (scroll_down_p)
13296 /* Point is in or below the bottom scroll margin, so move the
13297 window start down. If scrolling conservatively, move it just
13298 enough down to make point visible. If scroll_step is set,
13299 move it down by scroll_step. */
13300 if (scroll_conservatively)
13301 amount_to_scroll
13302 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13303 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13304 else if (scroll_step || temp_scroll_step)
13305 amount_to_scroll = scroll_max;
13306 else
13308 aggressive = current_buffer->scroll_up_aggressively;
13309 height = WINDOW_BOX_TEXT_HEIGHT (w);
13310 if (NUMBERP (aggressive))
13312 double float_amount = XFLOATINT (aggressive) * height;
13313 amount_to_scroll = float_amount;
13314 if (amount_to_scroll == 0 && float_amount > 0)
13315 amount_to_scroll = 1;
13319 if (amount_to_scroll <= 0)
13320 return SCROLLING_FAILED;
13322 start_display (&it, w, startp);
13323 if (scroll_max < INT_MAX)
13324 move_it_vertically (&it, amount_to_scroll);
13325 else
13327 /* Extra precision for users who set scroll-conservatively
13328 to most-positive-fixnum: make sure the amount we scroll
13329 the window start is never less than amount_to_scroll,
13330 which was computed as distance from window bottom to
13331 point. This matters when lines at window top and lines
13332 below window bottom have different height. */
13333 struct it it1 = it;
13334 /* We use a temporary it1 because line_bottom_y can modify
13335 its argument, if it moves one line down; see there. */
13336 int start_y = line_bottom_y (&it1);
13338 do {
13339 move_it_by_lines (&it, 1, 1);
13340 it1 = it;
13341 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13344 /* If STARTP is unchanged, move it down another screen line. */
13345 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13346 move_it_by_lines (&it, 1, 1);
13347 startp = it.current.pos;
13349 else
13351 struct text_pos scroll_margin_pos = startp;
13353 /* See if point is inside the scroll margin at the top of the
13354 window. */
13355 if (this_scroll_margin)
13357 start_display (&it, w, startp);
13358 move_it_vertically (&it, this_scroll_margin);
13359 scroll_margin_pos = it.current.pos;
13362 if (PT < CHARPOS (scroll_margin_pos))
13364 /* Point is in the scroll margin at the top of the window or
13365 above what is displayed in the window. */
13366 int y0;
13368 /* Compute the vertical distance from PT to the scroll
13369 margin position. Give up if distance is greater than
13370 scroll_max. */
13371 SET_TEXT_POS (pos, PT, PT_BYTE);
13372 start_display (&it, w, pos);
13373 y0 = it.current_y;
13374 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13375 it.last_visible_y, -1,
13376 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13377 dy = it.current_y - y0;
13378 if (dy > scroll_max)
13379 return SCROLLING_FAILED;
13381 /* Compute new window start. */
13382 start_display (&it, w, startp);
13384 if (scroll_conservatively)
13385 amount_to_scroll
13386 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13387 else if (scroll_step || temp_scroll_step)
13388 amount_to_scroll = scroll_max;
13389 else
13391 aggressive = current_buffer->scroll_down_aggressively;
13392 height = WINDOW_BOX_TEXT_HEIGHT (w);
13393 if (NUMBERP (aggressive))
13395 double float_amount = XFLOATINT (aggressive) * height;
13396 amount_to_scroll = float_amount;
13397 if (amount_to_scroll == 0 && float_amount > 0)
13398 amount_to_scroll = 1;
13402 if (amount_to_scroll <= 0)
13403 return SCROLLING_FAILED;
13405 move_it_vertically_backward (&it, amount_to_scroll);
13406 startp = it.current.pos;
13410 /* Run window scroll functions. */
13411 startp = run_window_scroll_functions (window, startp);
13413 /* Display the window. Give up if new fonts are loaded, or if point
13414 doesn't appear. */
13415 if (!try_window (window, startp, 0))
13416 rc = SCROLLING_NEED_LARGER_MATRICES;
13417 else if (w->cursor.vpos < 0)
13419 clear_glyph_matrix (w->desired_matrix);
13420 rc = SCROLLING_FAILED;
13422 else
13424 /* Maybe forget recorded base line for line number display. */
13425 if (!just_this_one_p
13426 || current_buffer->clip_changed
13427 || BEG_UNCHANGED < CHARPOS (startp))
13428 w->base_line_number = Qnil;
13430 /* If cursor ends up on a partially visible line,
13431 treat that as being off the bottom of the screen. */
13432 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13433 /* It's possible that the cursor is on the first line of the
13434 buffer, which is partially obscured due to a vscroll
13435 (Bug#7537). In that case, avoid looping forever . */
13436 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13438 clear_glyph_matrix (w->desired_matrix);
13439 ++extra_scroll_margin_lines;
13440 goto too_near_end;
13442 rc = SCROLLING_SUCCESS;
13445 return rc;
13449 /* Compute a suitable window start for window W if display of W starts
13450 on a continuation line. Value is non-zero if a new window start
13451 was computed.
13453 The new window start will be computed, based on W's width, starting
13454 from the start of the continued line. It is the start of the
13455 screen line with the minimum distance from the old start W->start. */
13457 static int
13458 compute_window_start_on_continuation_line (struct window *w)
13460 struct text_pos pos, start_pos;
13461 int window_start_changed_p = 0;
13463 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13465 /* If window start is on a continuation line... Window start may be
13466 < BEGV in case there's invisible text at the start of the
13467 buffer (M-x rmail, for example). */
13468 if (CHARPOS (start_pos) > BEGV
13469 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13471 struct it it;
13472 struct glyph_row *row;
13474 /* Handle the case that the window start is out of range. */
13475 if (CHARPOS (start_pos) < BEGV)
13476 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13477 else if (CHARPOS (start_pos) > ZV)
13478 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13480 /* Find the start of the continued line. This should be fast
13481 because scan_buffer is fast (newline cache). */
13482 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13483 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13484 row, DEFAULT_FACE_ID);
13485 reseat_at_previous_visible_line_start (&it);
13487 /* If the line start is "too far" away from the window start,
13488 say it takes too much time to compute a new window start. */
13489 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13490 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13492 int min_distance, distance;
13494 /* Move forward by display lines to find the new window
13495 start. If window width was enlarged, the new start can
13496 be expected to be > the old start. If window width was
13497 decreased, the new window start will be < the old start.
13498 So, we're looking for the display line start with the
13499 minimum distance from the old window start. */
13500 pos = it.current.pos;
13501 min_distance = INFINITY;
13502 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13503 distance < min_distance)
13505 min_distance = distance;
13506 pos = it.current.pos;
13507 move_it_by_lines (&it, 1, 0);
13510 /* Set the window start there. */
13511 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13512 window_start_changed_p = 1;
13516 return window_start_changed_p;
13520 /* Try cursor movement in case text has not changed in window WINDOW,
13521 with window start STARTP. Value is
13523 CURSOR_MOVEMENT_SUCCESS if successful
13525 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13527 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13528 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13529 we want to scroll as if scroll-step were set to 1. See the code.
13531 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13532 which case we have to abort this redisplay, and adjust matrices
13533 first. */
13535 enum
13537 CURSOR_MOVEMENT_SUCCESS,
13538 CURSOR_MOVEMENT_CANNOT_BE_USED,
13539 CURSOR_MOVEMENT_MUST_SCROLL,
13540 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13543 static int
13544 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13546 struct window *w = XWINDOW (window);
13547 struct frame *f = XFRAME (w->frame);
13548 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13550 #if GLYPH_DEBUG
13551 if (inhibit_try_cursor_movement)
13552 return rc;
13553 #endif
13555 /* Handle case where text has not changed, only point, and it has
13556 not moved off the frame. */
13557 if (/* Point may be in this window. */
13558 PT >= CHARPOS (startp)
13559 /* Selective display hasn't changed. */
13560 && !current_buffer->clip_changed
13561 /* Function force-mode-line-update is used to force a thorough
13562 redisplay. It sets either windows_or_buffers_changed or
13563 update_mode_lines. So don't take a shortcut here for these
13564 cases. */
13565 && !update_mode_lines
13566 && !windows_or_buffers_changed
13567 && !cursor_type_changed
13568 /* Can't use this case if highlighting a region. When a
13569 region exists, cursor movement has to do more than just
13570 set the cursor. */
13571 && !(!NILP (Vtransient_mark_mode)
13572 && !NILP (current_buffer->mark_active))
13573 && NILP (w->region_showing)
13574 && NILP (Vshow_trailing_whitespace)
13575 /* Right after splitting windows, last_point may be nil. */
13576 && INTEGERP (w->last_point)
13577 /* This code is not used for mini-buffer for the sake of the case
13578 of redisplaying to replace an echo area message; since in
13579 that case the mini-buffer contents per se are usually
13580 unchanged. This code is of no real use in the mini-buffer
13581 since the handling of this_line_start_pos, etc., in redisplay
13582 handles the same cases. */
13583 && !EQ (window, minibuf_window)
13584 /* When splitting windows or for new windows, it happens that
13585 redisplay is called with a nil window_end_vpos or one being
13586 larger than the window. This should really be fixed in
13587 window.c. I don't have this on my list, now, so we do
13588 approximately the same as the old redisplay code. --gerd. */
13589 && INTEGERP (w->window_end_vpos)
13590 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13591 && (FRAME_WINDOW_P (f)
13592 || !overlay_arrow_in_current_buffer_p ()))
13594 int this_scroll_margin, top_scroll_margin;
13595 struct glyph_row *row = NULL;
13597 #if GLYPH_DEBUG
13598 debug_method_add (w, "cursor movement");
13599 #endif
13601 /* Scroll if point within this distance from the top or bottom
13602 of the window. This is a pixel value. */
13603 if (scroll_margin > 0)
13605 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13606 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13608 else
13609 this_scroll_margin = 0;
13611 top_scroll_margin = this_scroll_margin;
13612 if (WINDOW_WANTS_HEADER_LINE_P (w))
13613 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13615 /* Start with the row the cursor was displayed during the last
13616 not paused redisplay. Give up if that row is not valid. */
13617 if (w->last_cursor.vpos < 0
13618 || w->last_cursor.vpos >= w->current_matrix->nrows)
13619 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13620 else
13622 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13623 if (row->mode_line_p)
13624 ++row;
13625 if (!row->enabled_p)
13626 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13629 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13631 int scroll_p = 0, must_scroll = 0;
13632 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13634 if (PT > XFASTINT (w->last_point))
13636 /* Point has moved forward. */
13637 while (MATRIX_ROW_END_CHARPOS (row) < PT
13638 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13640 xassert (row->enabled_p);
13641 ++row;
13644 /* If the end position of a row equals the start
13645 position of the next row, and PT is at that position,
13646 we would rather display cursor in the next line. */
13647 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13648 && MATRIX_ROW_END_CHARPOS (row) == PT
13649 && row < w->current_matrix->rows
13650 + w->current_matrix->nrows - 1
13651 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13652 && !cursor_row_p (w, row))
13653 ++row;
13655 /* If within the scroll margin, scroll. Note that
13656 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13657 the next line would be drawn, and that
13658 this_scroll_margin can be zero. */
13659 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13660 || PT > MATRIX_ROW_END_CHARPOS (row)
13661 /* Line is completely visible last line in window
13662 and PT is to be set in the next line. */
13663 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13664 && PT == MATRIX_ROW_END_CHARPOS (row)
13665 && !row->ends_at_zv_p
13666 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13667 scroll_p = 1;
13669 else if (PT < XFASTINT (w->last_point))
13671 /* Cursor has to be moved backward. Note that PT >=
13672 CHARPOS (startp) because of the outer if-statement. */
13673 while (!row->mode_line_p
13674 && (MATRIX_ROW_START_CHARPOS (row) > PT
13675 || (MATRIX_ROW_START_CHARPOS (row) == PT
13676 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13677 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13678 row > w->current_matrix->rows
13679 && (row-1)->ends_in_newline_from_string_p))))
13680 && (row->y > top_scroll_margin
13681 || CHARPOS (startp) == BEGV))
13683 xassert (row->enabled_p);
13684 --row;
13687 /* Consider the following case: Window starts at BEGV,
13688 there is invisible, intangible text at BEGV, so that
13689 display starts at some point START > BEGV. It can
13690 happen that we are called with PT somewhere between
13691 BEGV and START. Try to handle that case. */
13692 if (row < w->current_matrix->rows
13693 || row->mode_line_p)
13695 row = w->current_matrix->rows;
13696 if (row->mode_line_p)
13697 ++row;
13700 /* Due to newlines in overlay strings, we may have to
13701 skip forward over overlay strings. */
13702 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13703 && MATRIX_ROW_END_CHARPOS (row) == PT
13704 && !cursor_row_p (w, row))
13705 ++row;
13707 /* If within the scroll margin, scroll. */
13708 if (row->y < top_scroll_margin
13709 && CHARPOS (startp) != BEGV)
13710 scroll_p = 1;
13712 else
13714 /* Cursor did not move. So don't scroll even if cursor line
13715 is partially visible, as it was so before. */
13716 rc = CURSOR_MOVEMENT_SUCCESS;
13719 if (PT < MATRIX_ROW_START_CHARPOS (row)
13720 || PT > MATRIX_ROW_END_CHARPOS (row))
13722 /* if PT is not in the glyph row, give up. */
13723 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13724 must_scroll = 1;
13726 else if (rc != CURSOR_MOVEMENT_SUCCESS
13727 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13729 /* If rows are bidi-reordered and point moved, back up
13730 until we find a row that does not belong to a
13731 continuation line. This is because we must consider
13732 all rows of a continued line as candidates for the
13733 new cursor positioning, since row start and end
13734 positions change non-linearly with vertical position
13735 in such rows. */
13736 /* FIXME: Revisit this when glyph ``spilling'' in
13737 continuation lines' rows is implemented for
13738 bidi-reordered rows. */
13739 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13741 xassert (row->enabled_p);
13742 --row;
13743 /* If we hit the beginning of the displayed portion
13744 without finding the first row of a continued
13745 line, give up. */
13746 if (row <= w->current_matrix->rows)
13748 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13749 break;
13754 if (must_scroll)
13756 else if (rc != CURSOR_MOVEMENT_SUCCESS
13757 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13758 && make_cursor_line_fully_visible_p)
13760 if (PT == MATRIX_ROW_END_CHARPOS (row)
13761 && !row->ends_at_zv_p
13762 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13763 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13764 else if (row->height > window_box_height (w))
13766 /* If we end up in a partially visible line, let's
13767 make it fully visible, except when it's taller
13768 than the window, in which case we can't do much
13769 about it. */
13770 *scroll_step = 1;
13771 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13773 else
13775 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13776 if (!cursor_row_fully_visible_p (w, 0, 1))
13777 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13778 else
13779 rc = CURSOR_MOVEMENT_SUCCESS;
13782 else if (scroll_p)
13783 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13784 else if (rc != CURSOR_MOVEMENT_SUCCESS
13785 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13787 /* With bidi-reordered rows, there could be more than
13788 one candidate row whose start and end positions
13789 occlude point. We need to let set_cursor_from_row
13790 find the best candidate. */
13791 /* FIXME: Revisit this when glyph ``spilling'' in
13792 continuation lines' rows is implemented for
13793 bidi-reordered rows. */
13794 int rv = 0;
13798 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13799 && PT <= MATRIX_ROW_END_CHARPOS (row)
13800 && cursor_row_p (w, row))
13801 rv |= set_cursor_from_row (w, row, w->current_matrix,
13802 0, 0, 0, 0);
13803 /* As soon as we've found the first suitable row
13804 whose ends_at_zv_p flag is set, we are done. */
13805 if (rv
13806 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13808 rc = CURSOR_MOVEMENT_SUCCESS;
13809 break;
13811 ++row;
13813 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13814 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13815 || (MATRIX_ROW_START_CHARPOS (row) == PT
13816 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13817 /* If we didn't find any candidate rows, or exited the
13818 loop before all the candidates were examined, signal
13819 to the caller that this method failed. */
13820 if (rc != CURSOR_MOVEMENT_SUCCESS
13821 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13822 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13823 else if (rv)
13824 rc = CURSOR_MOVEMENT_SUCCESS;
13826 else
13830 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13832 rc = CURSOR_MOVEMENT_SUCCESS;
13833 break;
13835 ++row;
13837 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13838 && MATRIX_ROW_START_CHARPOS (row) == PT
13839 && cursor_row_p (w, row));
13844 return rc;
13847 void
13848 set_vertical_scroll_bar (struct window *w)
13850 EMACS_INT start, end, whole;
13852 /* Calculate the start and end positions for the current window.
13853 At some point, it would be nice to choose between scrollbars
13854 which reflect the whole buffer size, with special markers
13855 indicating narrowing, and scrollbars which reflect only the
13856 visible region.
13858 Note that mini-buffers sometimes aren't displaying any text. */
13859 if (!MINI_WINDOW_P (w)
13860 || (w == XWINDOW (minibuf_window)
13861 && NILP (echo_area_buffer[0])))
13863 struct buffer *buf = XBUFFER (w->buffer);
13864 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13865 start = marker_position (w->start) - BUF_BEGV (buf);
13866 /* I don't think this is guaranteed to be right. For the
13867 moment, we'll pretend it is. */
13868 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13870 if (end < start)
13871 end = start;
13872 if (whole < (end - start))
13873 whole = end - start;
13875 else
13876 start = end = whole = 0;
13878 /* Indicate what this scroll bar ought to be displaying now. */
13879 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13880 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13881 (w, end - start, whole, start);
13885 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13886 selected_window is redisplayed.
13888 We can return without actually redisplaying the window if
13889 fonts_changed_p is nonzero. In that case, redisplay_internal will
13890 retry. */
13892 static void
13893 redisplay_window (Lisp_Object window, int just_this_one_p)
13895 struct window *w = XWINDOW (window);
13896 struct frame *f = XFRAME (w->frame);
13897 struct buffer *buffer = XBUFFER (w->buffer);
13898 struct buffer *old = current_buffer;
13899 struct text_pos lpoint, opoint, startp;
13900 int update_mode_line;
13901 int tem;
13902 struct it it;
13903 /* Record it now because it's overwritten. */
13904 int current_matrix_up_to_date_p = 0;
13905 int used_current_matrix_p = 0;
13906 /* This is less strict than current_matrix_up_to_date_p.
13907 It indictes that the buffer contents and narrowing are unchanged. */
13908 int buffer_unchanged_p = 0;
13909 int temp_scroll_step = 0;
13910 int count = SPECPDL_INDEX ();
13911 int rc;
13912 int centering_position = -1;
13913 int last_line_misfit = 0;
13914 EMACS_INT beg_unchanged, end_unchanged;
13916 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13917 opoint = lpoint;
13919 /* W must be a leaf window here. */
13920 xassert (!NILP (w->buffer));
13921 #if GLYPH_DEBUG
13922 *w->desired_matrix->method = 0;
13923 #endif
13925 restart:
13926 reconsider_clip_changes (w, buffer);
13928 /* Has the mode line to be updated? */
13929 update_mode_line = (!NILP (w->update_mode_line)
13930 || update_mode_lines
13931 || buffer->clip_changed
13932 || buffer->prevent_redisplay_optimizations_p);
13934 if (MINI_WINDOW_P (w))
13936 if (w == XWINDOW (echo_area_window)
13937 && !NILP (echo_area_buffer[0]))
13939 if (update_mode_line)
13940 /* We may have to update a tty frame's menu bar or a
13941 tool-bar. Example `M-x C-h C-h C-g'. */
13942 goto finish_menu_bars;
13943 else
13944 /* We've already displayed the echo area glyphs in this window. */
13945 goto finish_scroll_bars;
13947 else if ((w != XWINDOW (minibuf_window)
13948 || minibuf_level == 0)
13949 /* When buffer is nonempty, redisplay window normally. */
13950 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13951 /* Quail displays non-mini buffers in minibuffer window.
13952 In that case, redisplay the window normally. */
13953 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13955 /* W is a mini-buffer window, but it's not active, so clear
13956 it. */
13957 int yb = window_text_bottom_y (w);
13958 struct glyph_row *row;
13959 int y;
13961 for (y = 0, row = w->desired_matrix->rows;
13962 y < yb;
13963 y += row->height, ++row)
13964 blank_row (w, row, y);
13965 goto finish_scroll_bars;
13968 clear_glyph_matrix (w->desired_matrix);
13971 /* Otherwise set up data on this window; select its buffer and point
13972 value. */
13973 /* Really select the buffer, for the sake of buffer-local
13974 variables. */
13975 set_buffer_internal_1 (XBUFFER (w->buffer));
13977 current_matrix_up_to_date_p
13978 = (!NILP (w->window_end_valid)
13979 && !current_buffer->clip_changed
13980 && !current_buffer->prevent_redisplay_optimizations_p
13981 && XFASTINT (w->last_modified) >= MODIFF
13982 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13984 /* Run the window-bottom-change-functions
13985 if it is possible that the text on the screen has changed
13986 (either due to modification of the text, or any other reason). */
13987 if (!current_matrix_up_to_date_p
13988 && !NILP (Vwindow_text_change_functions))
13990 safe_run_hooks (Qwindow_text_change_functions);
13991 goto restart;
13994 beg_unchanged = BEG_UNCHANGED;
13995 end_unchanged = END_UNCHANGED;
13997 SET_TEXT_POS (opoint, PT, PT_BYTE);
13999 specbind (Qinhibit_point_motion_hooks, Qt);
14001 buffer_unchanged_p
14002 = (!NILP (w->window_end_valid)
14003 && !current_buffer->clip_changed
14004 && XFASTINT (w->last_modified) >= MODIFF
14005 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14007 /* When windows_or_buffers_changed is non-zero, we can't rely on
14008 the window end being valid, so set it to nil there. */
14009 if (windows_or_buffers_changed)
14011 /* If window starts on a continuation line, maybe adjust the
14012 window start in case the window's width changed. */
14013 if (XMARKER (w->start)->buffer == current_buffer)
14014 compute_window_start_on_continuation_line (w);
14016 w->window_end_valid = Qnil;
14019 /* Some sanity checks. */
14020 CHECK_WINDOW_END (w);
14021 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14022 abort ();
14023 if (BYTEPOS (opoint) < CHARPOS (opoint))
14024 abort ();
14026 /* If %c is in mode line, update it if needed. */
14027 if (!NILP (w->column_number_displayed)
14028 /* This alternative quickly identifies a common case
14029 where no change is needed. */
14030 && !(PT == XFASTINT (w->last_point)
14031 && XFASTINT (w->last_modified) >= MODIFF
14032 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14033 && (XFASTINT (w->column_number_displayed)
14034 != (int) current_column ())) /* iftc */
14035 update_mode_line = 1;
14037 /* Count number of windows showing the selected buffer. An indirect
14038 buffer counts as its base buffer. */
14039 if (!just_this_one_p)
14041 struct buffer *current_base, *window_base;
14042 current_base = current_buffer;
14043 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14044 if (current_base->base_buffer)
14045 current_base = current_base->base_buffer;
14046 if (window_base->base_buffer)
14047 window_base = window_base->base_buffer;
14048 if (current_base == window_base)
14049 buffer_shared++;
14052 /* Point refers normally to the selected window. For any other
14053 window, set up appropriate value. */
14054 if (!EQ (window, selected_window))
14056 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14057 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14058 if (new_pt < BEGV)
14060 new_pt = BEGV;
14061 new_pt_byte = BEGV_BYTE;
14062 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14064 else if (new_pt > (ZV - 1))
14066 new_pt = ZV;
14067 new_pt_byte = ZV_BYTE;
14068 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14071 /* We don't use SET_PT so that the point-motion hooks don't run. */
14072 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14075 /* If any of the character widths specified in the display table
14076 have changed, invalidate the width run cache. It's true that
14077 this may be a bit late to catch such changes, but the rest of
14078 redisplay goes (non-fatally) haywire when the display table is
14079 changed, so why should we worry about doing any better? */
14080 if (current_buffer->width_run_cache)
14082 struct Lisp_Char_Table *disptab = buffer_display_table ();
14084 if (! disptab_matches_widthtab (disptab,
14085 XVECTOR (current_buffer->width_table)))
14087 invalidate_region_cache (current_buffer,
14088 current_buffer->width_run_cache,
14089 BEG, Z);
14090 recompute_width_table (current_buffer, disptab);
14094 /* If window-start is screwed up, choose a new one. */
14095 if (XMARKER (w->start)->buffer != current_buffer)
14096 goto recenter;
14098 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14100 /* If someone specified a new starting point but did not insist,
14101 check whether it can be used. */
14102 if (!NILP (w->optional_new_start)
14103 && CHARPOS (startp) >= BEGV
14104 && CHARPOS (startp) <= ZV)
14106 w->optional_new_start = Qnil;
14107 start_display (&it, w, startp);
14108 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14109 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14110 if (IT_CHARPOS (it) == PT)
14111 w->force_start = Qt;
14112 /* IT may overshoot PT if text at PT is invisible. */
14113 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14114 w->force_start = Qt;
14117 force_start:
14119 /* Handle case where place to start displaying has been specified,
14120 unless the specified location is outside the accessible range. */
14121 if (!NILP (w->force_start)
14122 || w->frozen_window_start_p)
14124 /* We set this later on if we have to adjust point. */
14125 int new_vpos = -1;
14127 w->force_start = Qnil;
14128 w->vscroll = 0;
14129 w->window_end_valid = Qnil;
14131 /* Forget any recorded base line for line number display. */
14132 if (!buffer_unchanged_p)
14133 w->base_line_number = Qnil;
14135 /* Redisplay the mode line. Select the buffer properly for that.
14136 Also, run the hook window-scroll-functions
14137 because we have scrolled. */
14138 /* Note, we do this after clearing force_start because
14139 if there's an error, it is better to forget about force_start
14140 than to get into an infinite loop calling the hook functions
14141 and having them get more errors. */
14142 if (!update_mode_line
14143 || ! NILP (Vwindow_scroll_functions))
14145 update_mode_line = 1;
14146 w->update_mode_line = Qt;
14147 startp = run_window_scroll_functions (window, startp);
14150 w->last_modified = make_number (0);
14151 w->last_overlay_modified = make_number (0);
14152 if (CHARPOS (startp) < BEGV)
14153 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14154 else if (CHARPOS (startp) > ZV)
14155 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14157 /* Redisplay, then check if cursor has been set during the
14158 redisplay. Give up if new fonts were loaded. */
14159 /* We used to issue a CHECK_MARGINS argument to try_window here,
14160 but this causes scrolling to fail when point begins inside
14161 the scroll margin (bug#148) -- cyd */
14162 if (!try_window (window, startp, 0))
14164 w->force_start = Qt;
14165 clear_glyph_matrix (w->desired_matrix);
14166 goto need_larger_matrices;
14169 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14171 /* If point does not appear, try to move point so it does
14172 appear. The desired matrix has been built above, so we
14173 can use it here. */
14174 new_vpos = window_box_height (w) / 2;
14177 if (!cursor_row_fully_visible_p (w, 0, 0))
14179 /* Point does appear, but on a line partly visible at end of window.
14180 Move it back to a fully-visible line. */
14181 new_vpos = window_box_height (w);
14184 /* If we need to move point for either of the above reasons,
14185 now actually do it. */
14186 if (new_vpos >= 0)
14188 struct glyph_row *row;
14190 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14191 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14192 ++row;
14194 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14195 MATRIX_ROW_START_BYTEPOS (row));
14197 if (w != XWINDOW (selected_window))
14198 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14199 else if (current_buffer == old)
14200 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14202 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14204 /* If we are highlighting the region, then we just changed
14205 the region, so redisplay to show it. */
14206 if (!NILP (Vtransient_mark_mode)
14207 && !NILP (current_buffer->mark_active))
14209 clear_glyph_matrix (w->desired_matrix);
14210 if (!try_window (window, startp, 0))
14211 goto need_larger_matrices;
14215 #if GLYPH_DEBUG
14216 debug_method_add (w, "forced window start");
14217 #endif
14218 goto done;
14221 /* Handle case where text has not changed, only point, and it has
14222 not moved off the frame, and we are not retrying after hscroll.
14223 (current_matrix_up_to_date_p is nonzero when retrying.) */
14224 if (current_matrix_up_to_date_p
14225 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14226 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14228 switch (rc)
14230 case CURSOR_MOVEMENT_SUCCESS:
14231 used_current_matrix_p = 1;
14232 goto done;
14234 case CURSOR_MOVEMENT_MUST_SCROLL:
14235 goto try_to_scroll;
14237 default:
14238 abort ();
14241 /* If current starting point was originally the beginning of a line
14242 but no longer is, find a new starting point. */
14243 else if (!NILP (w->start_at_line_beg)
14244 && !(CHARPOS (startp) <= BEGV
14245 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14247 #if GLYPH_DEBUG
14248 debug_method_add (w, "recenter 1");
14249 #endif
14250 goto recenter;
14253 /* Try scrolling with try_window_id. Value is > 0 if update has
14254 been done, it is -1 if we know that the same window start will
14255 not work. It is 0 if unsuccessful for some other reason. */
14256 else if ((tem = try_window_id (w)) != 0)
14258 #if GLYPH_DEBUG
14259 debug_method_add (w, "try_window_id %d", tem);
14260 #endif
14262 if (fonts_changed_p)
14263 goto need_larger_matrices;
14264 if (tem > 0)
14265 goto done;
14267 /* Otherwise try_window_id has returned -1 which means that we
14268 don't want the alternative below this comment to execute. */
14270 else if (CHARPOS (startp) >= BEGV
14271 && CHARPOS (startp) <= ZV
14272 && PT >= CHARPOS (startp)
14273 && (CHARPOS (startp) < ZV
14274 /* Avoid starting at end of buffer. */
14275 || CHARPOS (startp) == BEGV
14276 || (XFASTINT (w->last_modified) >= MODIFF
14277 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14280 /* If first window line is a continuation line, and window start
14281 is inside the modified region, but the first change is before
14282 current window start, we must select a new window start.
14284 However, if this is the result of a down-mouse event (e.g. by
14285 extending the mouse-drag-overlay), we don't want to select a
14286 new window start, since that would change the position under
14287 the mouse, resulting in an unwanted mouse-movement rather
14288 than a simple mouse-click. */
14289 if (NILP (w->start_at_line_beg)
14290 && NILP (do_mouse_tracking)
14291 && CHARPOS (startp) > BEGV
14292 && CHARPOS (startp) > BEG + beg_unchanged
14293 && CHARPOS (startp) <= Z - end_unchanged
14294 /* Even if w->start_at_line_beg is nil, a new window may
14295 start at a line_beg, since that's how set_buffer_window
14296 sets it. So, we need to check the return value of
14297 compute_window_start_on_continuation_line. (See also
14298 bug#197). */
14299 && XMARKER (w->start)->buffer == current_buffer
14300 && compute_window_start_on_continuation_line (w))
14302 w->force_start = Qt;
14303 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14304 goto force_start;
14307 #if GLYPH_DEBUG
14308 debug_method_add (w, "same window start");
14309 #endif
14311 /* Try to redisplay starting at same place as before.
14312 If point has not moved off frame, accept the results. */
14313 if (!current_matrix_up_to_date_p
14314 /* Don't use try_window_reusing_current_matrix in this case
14315 because a window scroll function can have changed the
14316 buffer. */
14317 || !NILP (Vwindow_scroll_functions)
14318 || MINI_WINDOW_P (w)
14319 || !(used_current_matrix_p
14320 = try_window_reusing_current_matrix (w)))
14322 IF_DEBUG (debug_method_add (w, "1"));
14323 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14324 /* -1 means we need to scroll.
14325 0 means we need new matrices, but fonts_changed_p
14326 is set in that case, so we will detect it below. */
14327 goto try_to_scroll;
14330 if (fonts_changed_p)
14331 goto need_larger_matrices;
14333 if (w->cursor.vpos >= 0)
14335 if (!just_this_one_p
14336 || current_buffer->clip_changed
14337 || BEG_UNCHANGED < CHARPOS (startp))
14338 /* Forget any recorded base line for line number display. */
14339 w->base_line_number = Qnil;
14341 if (!cursor_row_fully_visible_p (w, 1, 0))
14343 clear_glyph_matrix (w->desired_matrix);
14344 last_line_misfit = 1;
14346 /* Drop through and scroll. */
14347 else
14348 goto done;
14350 else
14351 clear_glyph_matrix (w->desired_matrix);
14354 try_to_scroll:
14356 w->last_modified = make_number (0);
14357 w->last_overlay_modified = make_number (0);
14359 /* Redisplay the mode line. Select the buffer properly for that. */
14360 if (!update_mode_line)
14362 update_mode_line = 1;
14363 w->update_mode_line = Qt;
14366 /* Try to scroll by specified few lines. */
14367 if ((scroll_conservatively
14368 || scroll_step
14369 || temp_scroll_step
14370 || NUMBERP (current_buffer->scroll_up_aggressively)
14371 || NUMBERP (current_buffer->scroll_down_aggressively))
14372 && !current_buffer->clip_changed
14373 && CHARPOS (startp) >= BEGV
14374 && CHARPOS (startp) <= ZV)
14376 /* The function returns -1 if new fonts were loaded, 1 if
14377 successful, 0 if not successful. */
14378 int rc = try_scrolling (window, just_this_one_p,
14379 scroll_conservatively,
14380 scroll_step,
14381 temp_scroll_step, last_line_misfit);
14382 switch (rc)
14384 case SCROLLING_SUCCESS:
14385 goto done;
14387 case SCROLLING_NEED_LARGER_MATRICES:
14388 goto need_larger_matrices;
14390 case SCROLLING_FAILED:
14391 break;
14393 default:
14394 abort ();
14398 /* Finally, just choose place to start which centers point */
14400 recenter:
14401 if (centering_position < 0)
14402 centering_position = window_box_height (w) / 2;
14404 #if GLYPH_DEBUG
14405 debug_method_add (w, "recenter");
14406 #endif
14408 /* w->vscroll = 0; */
14410 /* Forget any previously recorded base line for line number display. */
14411 if (!buffer_unchanged_p)
14412 w->base_line_number = Qnil;
14414 /* Move backward half the height of the window. */
14415 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14416 it.current_y = it.last_visible_y;
14417 move_it_vertically_backward (&it, centering_position);
14418 xassert (IT_CHARPOS (it) >= BEGV);
14420 /* The function move_it_vertically_backward may move over more
14421 than the specified y-distance. If it->w is small, e.g. a
14422 mini-buffer window, we may end up in front of the window's
14423 display area. Start displaying at the start of the line
14424 containing PT in this case. */
14425 if (it.current_y <= 0)
14427 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14428 move_it_vertically_backward (&it, 0);
14429 it.current_y = 0;
14432 it.current_x = it.hpos = 0;
14434 /* Set startp here explicitly in case that helps avoid an infinite loop
14435 in case the window-scroll-functions functions get errors. */
14436 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14438 /* Run scroll hooks. */
14439 startp = run_window_scroll_functions (window, it.current.pos);
14441 /* Redisplay the window. */
14442 if (!current_matrix_up_to_date_p
14443 || windows_or_buffers_changed
14444 || cursor_type_changed
14445 /* Don't use try_window_reusing_current_matrix in this case
14446 because it can have changed the buffer. */
14447 || !NILP (Vwindow_scroll_functions)
14448 || !just_this_one_p
14449 || MINI_WINDOW_P (w)
14450 || !(used_current_matrix_p
14451 = try_window_reusing_current_matrix (w)))
14452 try_window (window, startp, 0);
14454 /* If new fonts have been loaded (due to fontsets), give up. We
14455 have to start a new redisplay since we need to re-adjust glyph
14456 matrices. */
14457 if (fonts_changed_p)
14458 goto need_larger_matrices;
14460 /* If cursor did not appear assume that the middle of the window is
14461 in the first line of the window. Do it again with the next line.
14462 (Imagine a window of height 100, displaying two lines of height
14463 60. Moving back 50 from it->last_visible_y will end in the first
14464 line.) */
14465 if (w->cursor.vpos < 0)
14467 if (!NILP (w->window_end_valid)
14468 && PT >= Z - XFASTINT (w->window_end_pos))
14470 clear_glyph_matrix (w->desired_matrix);
14471 move_it_by_lines (&it, 1, 0);
14472 try_window (window, it.current.pos, 0);
14474 else if (PT < IT_CHARPOS (it))
14476 clear_glyph_matrix (w->desired_matrix);
14477 move_it_by_lines (&it, -1, 0);
14478 try_window (window, it.current.pos, 0);
14480 else
14482 /* Not much we can do about it. */
14486 /* Consider the following case: Window starts at BEGV, there is
14487 invisible, intangible text at BEGV, so that display starts at
14488 some point START > BEGV. It can happen that we are called with
14489 PT somewhere between BEGV and START. Try to handle that case. */
14490 if (w->cursor.vpos < 0)
14492 struct glyph_row *row = w->current_matrix->rows;
14493 if (row->mode_line_p)
14494 ++row;
14495 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14498 if (!cursor_row_fully_visible_p (w, 0, 0))
14500 /* If vscroll is enabled, disable it and try again. */
14501 if (w->vscroll)
14503 w->vscroll = 0;
14504 clear_glyph_matrix (w->desired_matrix);
14505 goto recenter;
14508 /* If centering point failed to make the whole line visible,
14509 put point at the top instead. That has to make the whole line
14510 visible, if it can be done. */
14511 if (centering_position == 0)
14512 goto done;
14514 clear_glyph_matrix (w->desired_matrix);
14515 centering_position = 0;
14516 goto recenter;
14519 done:
14521 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14522 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14523 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14524 ? Qt : Qnil);
14526 /* Display the mode line, if we must. */
14527 if ((update_mode_line
14528 /* If window not full width, must redo its mode line
14529 if (a) the window to its side is being redone and
14530 (b) we do a frame-based redisplay. This is a consequence
14531 of how inverted lines are drawn in frame-based redisplay. */
14532 || (!just_this_one_p
14533 && !FRAME_WINDOW_P (f)
14534 && !WINDOW_FULL_WIDTH_P (w))
14535 /* Line number to display. */
14536 || INTEGERP (w->base_line_pos)
14537 /* Column number is displayed and different from the one displayed. */
14538 || (!NILP (w->column_number_displayed)
14539 && (XFASTINT (w->column_number_displayed)
14540 != (int) current_column ()))) /* iftc */
14541 /* This means that the window has a mode line. */
14542 && (WINDOW_WANTS_MODELINE_P (w)
14543 || WINDOW_WANTS_HEADER_LINE_P (w)))
14545 display_mode_lines (w);
14547 /* If mode line height has changed, arrange for a thorough
14548 immediate redisplay using the correct mode line height. */
14549 if (WINDOW_WANTS_MODELINE_P (w)
14550 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14552 fonts_changed_p = 1;
14553 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14554 = DESIRED_MODE_LINE_HEIGHT (w);
14557 /* If header line height has changed, arrange for a thorough
14558 immediate redisplay using the correct header line height. */
14559 if (WINDOW_WANTS_HEADER_LINE_P (w)
14560 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14562 fonts_changed_p = 1;
14563 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14564 = DESIRED_HEADER_LINE_HEIGHT (w);
14567 if (fonts_changed_p)
14568 goto need_larger_matrices;
14571 if (!line_number_displayed
14572 && !BUFFERP (w->base_line_pos))
14574 w->base_line_pos = Qnil;
14575 w->base_line_number = Qnil;
14578 finish_menu_bars:
14580 /* When we reach a frame's selected window, redo the frame's menu bar. */
14581 if (update_mode_line
14582 && EQ (FRAME_SELECTED_WINDOW (f), window))
14584 int redisplay_menu_p = 0;
14585 int redisplay_tool_bar_p = 0;
14587 if (FRAME_WINDOW_P (f))
14589 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14590 || defined (HAVE_NS) || defined (USE_GTK)
14591 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14592 #else
14593 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14594 #endif
14596 else
14597 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14599 if (redisplay_menu_p)
14600 display_menu_bar (w);
14602 #ifdef HAVE_WINDOW_SYSTEM
14603 if (FRAME_WINDOW_P (f))
14605 #if defined (USE_GTK) || defined (HAVE_NS)
14606 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14607 #else
14608 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14609 && (FRAME_TOOL_BAR_LINES (f) > 0
14610 || !NILP (Vauto_resize_tool_bars));
14611 #endif
14613 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14615 ignore_mouse_drag_p = 1;
14618 #endif
14621 #ifdef HAVE_WINDOW_SYSTEM
14622 if (FRAME_WINDOW_P (f)
14623 && update_window_fringes (w, (just_this_one_p
14624 || (!used_current_matrix_p && !overlay_arrow_seen)
14625 || w->pseudo_window_p)))
14627 update_begin (f);
14628 BLOCK_INPUT;
14629 if (draw_window_fringes (w, 1))
14630 x_draw_vertical_border (w);
14631 UNBLOCK_INPUT;
14632 update_end (f);
14634 #endif /* HAVE_WINDOW_SYSTEM */
14636 /* We go to this label, with fonts_changed_p nonzero,
14637 if it is necessary to try again using larger glyph matrices.
14638 We have to redeem the scroll bar even in this case,
14639 because the loop in redisplay_internal expects that. */
14640 need_larger_matrices:
14642 finish_scroll_bars:
14644 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14646 /* Set the thumb's position and size. */
14647 set_vertical_scroll_bar (w);
14649 /* Note that we actually used the scroll bar attached to this
14650 window, so it shouldn't be deleted at the end of redisplay. */
14651 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14652 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14655 /* Restore current_buffer and value of point in it. The window
14656 update may have changed the buffer, so first make sure `opoint'
14657 is still valid (Bug#6177). */
14658 if (CHARPOS (opoint) < BEGV)
14659 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14660 else if (CHARPOS (opoint) > ZV)
14661 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14662 else
14663 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14665 set_buffer_internal_1 (old);
14666 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14667 shorter. This can be caused by log truncation in *Messages*. */
14668 if (CHARPOS (lpoint) <= ZV)
14669 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14671 unbind_to (count, Qnil);
14675 /* Build the complete desired matrix of WINDOW with a window start
14676 buffer position POS.
14678 Value is 1 if successful. It is zero if fonts were loaded during
14679 redisplay which makes re-adjusting glyph matrices necessary, and -1
14680 if point would appear in the scroll margins.
14681 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14682 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14683 set in FLAGS.) */
14686 try_window (Lisp_Object window, struct text_pos pos, int flags)
14688 struct window *w = XWINDOW (window);
14689 struct it it;
14690 struct glyph_row *last_text_row = NULL;
14691 struct frame *f = XFRAME (w->frame);
14693 /* Make POS the new window start. */
14694 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14696 /* Mark cursor position as unknown. No overlay arrow seen. */
14697 w->cursor.vpos = -1;
14698 overlay_arrow_seen = 0;
14700 /* Initialize iterator and info to start at POS. */
14701 start_display (&it, w, pos);
14703 /* Display all lines of W. */
14704 while (it.current_y < it.last_visible_y)
14706 if (display_line (&it))
14707 last_text_row = it.glyph_row - 1;
14708 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14709 return 0;
14712 /* Don't let the cursor end in the scroll margins. */
14713 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14714 && !MINI_WINDOW_P (w))
14716 int this_scroll_margin;
14718 if (scroll_margin > 0)
14720 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14721 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14723 else
14724 this_scroll_margin = 0;
14726 if ((w->cursor.y >= 0 /* not vscrolled */
14727 && w->cursor.y < this_scroll_margin
14728 && CHARPOS (pos) > BEGV
14729 && IT_CHARPOS (it) < ZV)
14730 /* rms: considering make_cursor_line_fully_visible_p here
14731 seems to give wrong results. We don't want to recenter
14732 when the last line is partly visible, we want to allow
14733 that case to be handled in the usual way. */
14734 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14736 w->cursor.vpos = -1;
14737 clear_glyph_matrix (w->desired_matrix);
14738 return -1;
14742 /* If bottom moved off end of frame, change mode line percentage. */
14743 if (XFASTINT (w->window_end_pos) <= 0
14744 && Z != IT_CHARPOS (it))
14745 w->update_mode_line = Qt;
14747 /* Set window_end_pos to the offset of the last character displayed
14748 on the window from the end of current_buffer. Set
14749 window_end_vpos to its row number. */
14750 if (last_text_row)
14752 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14753 w->window_end_bytepos
14754 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14755 w->window_end_pos
14756 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14757 w->window_end_vpos
14758 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14759 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14760 ->displays_text_p);
14762 else
14764 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14765 w->window_end_pos = make_number (Z - ZV);
14766 w->window_end_vpos = make_number (0);
14769 /* But that is not valid info until redisplay finishes. */
14770 w->window_end_valid = Qnil;
14771 return 1;
14776 /************************************************************************
14777 Window redisplay reusing current matrix when buffer has not changed
14778 ************************************************************************/
14780 /* Try redisplay of window W showing an unchanged buffer with a
14781 different window start than the last time it was displayed by
14782 reusing its current matrix. Value is non-zero if successful.
14783 W->start is the new window start. */
14785 static int
14786 try_window_reusing_current_matrix (struct window *w)
14788 struct frame *f = XFRAME (w->frame);
14789 struct glyph_row *row, *bottom_row;
14790 struct it it;
14791 struct run run;
14792 struct text_pos start, new_start;
14793 int nrows_scrolled, i;
14794 struct glyph_row *last_text_row;
14795 struct glyph_row *last_reused_text_row;
14796 struct glyph_row *start_row;
14797 int start_vpos, min_y, max_y;
14799 #if GLYPH_DEBUG
14800 if (inhibit_try_window_reusing)
14801 return 0;
14802 #endif
14804 if (/* This function doesn't handle terminal frames. */
14805 !FRAME_WINDOW_P (f)
14806 /* Don't try to reuse the display if windows have been split
14807 or such. */
14808 || windows_or_buffers_changed
14809 || cursor_type_changed)
14810 return 0;
14812 /* Can't do this if region may have changed. */
14813 if ((!NILP (Vtransient_mark_mode)
14814 && !NILP (current_buffer->mark_active))
14815 || !NILP (w->region_showing)
14816 || !NILP (Vshow_trailing_whitespace))
14817 return 0;
14819 /* If top-line visibility has changed, give up. */
14820 if (WINDOW_WANTS_HEADER_LINE_P (w)
14821 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14822 return 0;
14824 /* Give up if old or new display is scrolled vertically. We could
14825 make this function handle this, but right now it doesn't. */
14826 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14827 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14828 return 0;
14830 /* The variable new_start now holds the new window start. The old
14831 start `start' can be determined from the current matrix. */
14832 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14833 start = start_row->minpos;
14834 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14836 /* Clear the desired matrix for the display below. */
14837 clear_glyph_matrix (w->desired_matrix);
14839 if (CHARPOS (new_start) <= CHARPOS (start))
14841 int first_row_y;
14843 /* Don't use this method if the display starts with an ellipsis
14844 displayed for invisible text. It's not easy to handle that case
14845 below, and it's certainly not worth the effort since this is
14846 not a frequent case. */
14847 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14848 return 0;
14850 IF_DEBUG (debug_method_add (w, "twu1"));
14852 /* Display up to a row that can be reused. The variable
14853 last_text_row is set to the last row displayed that displays
14854 text. Note that it.vpos == 0 if or if not there is a
14855 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14856 start_display (&it, w, new_start);
14857 first_row_y = it.current_y;
14858 w->cursor.vpos = -1;
14859 last_text_row = last_reused_text_row = NULL;
14861 while (it.current_y < it.last_visible_y
14862 && !fonts_changed_p)
14864 /* If we have reached into the characters in the START row,
14865 that means the line boundaries have changed. So we
14866 can't start copying with the row START. Maybe it will
14867 work to start copying with the following row. */
14868 while (IT_CHARPOS (it) > CHARPOS (start))
14870 /* Advance to the next row as the "start". */
14871 start_row++;
14872 start = start_row->minpos;
14873 /* If there are no more rows to try, or just one, give up. */
14874 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14875 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14876 || CHARPOS (start) == ZV)
14878 clear_glyph_matrix (w->desired_matrix);
14879 return 0;
14882 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14884 /* If we have reached alignment,
14885 we can copy the rest of the rows. */
14886 if (IT_CHARPOS (it) == CHARPOS (start))
14887 break;
14889 if (display_line (&it))
14890 last_text_row = it.glyph_row - 1;
14893 /* A value of current_y < last_visible_y means that we stopped
14894 at the previous window start, which in turn means that we
14895 have at least one reusable row. */
14896 if (it.current_y < it.last_visible_y)
14898 /* IT.vpos always starts from 0; it counts text lines. */
14899 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14901 /* Find PT if not already found in the lines displayed. */
14902 if (w->cursor.vpos < 0)
14904 int dy = it.current_y - start_row->y;
14906 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14907 row = row_containing_pos (w, PT, row, NULL, dy);
14908 if (row)
14909 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14910 dy, nrows_scrolled);
14911 else
14913 clear_glyph_matrix (w->desired_matrix);
14914 return 0;
14918 /* Scroll the display. Do it before the current matrix is
14919 changed. The problem here is that update has not yet
14920 run, i.e. part of the current matrix is not up to date.
14921 scroll_run_hook will clear the cursor, and use the
14922 current matrix to get the height of the row the cursor is
14923 in. */
14924 run.current_y = start_row->y;
14925 run.desired_y = it.current_y;
14926 run.height = it.last_visible_y - it.current_y;
14928 if (run.height > 0 && run.current_y != run.desired_y)
14930 update_begin (f);
14931 FRAME_RIF (f)->update_window_begin_hook (w);
14932 FRAME_RIF (f)->clear_window_mouse_face (w);
14933 FRAME_RIF (f)->scroll_run_hook (w, &run);
14934 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14935 update_end (f);
14938 /* Shift current matrix down by nrows_scrolled lines. */
14939 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14940 rotate_matrix (w->current_matrix,
14941 start_vpos,
14942 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14943 nrows_scrolled);
14945 /* Disable lines that must be updated. */
14946 for (i = 0; i < nrows_scrolled; ++i)
14947 (start_row + i)->enabled_p = 0;
14949 /* Re-compute Y positions. */
14950 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14951 max_y = it.last_visible_y;
14952 for (row = start_row + nrows_scrolled;
14953 row < bottom_row;
14954 ++row)
14956 row->y = it.current_y;
14957 row->visible_height = row->height;
14959 if (row->y < min_y)
14960 row->visible_height -= min_y - row->y;
14961 if (row->y + row->height > max_y)
14962 row->visible_height -= row->y + row->height - max_y;
14963 row->redraw_fringe_bitmaps_p = 1;
14965 it.current_y += row->height;
14967 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14968 last_reused_text_row = row;
14969 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14970 break;
14973 /* Disable lines in the current matrix which are now
14974 below the window. */
14975 for (++row; row < bottom_row; ++row)
14976 row->enabled_p = row->mode_line_p = 0;
14979 /* Update window_end_pos etc.; last_reused_text_row is the last
14980 reused row from the current matrix containing text, if any.
14981 The value of last_text_row is the last displayed line
14982 containing text. */
14983 if (last_reused_text_row)
14985 w->window_end_bytepos
14986 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14987 w->window_end_pos
14988 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14989 w->window_end_vpos
14990 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14991 w->current_matrix));
14993 else if (last_text_row)
14995 w->window_end_bytepos
14996 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14997 w->window_end_pos
14998 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14999 w->window_end_vpos
15000 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15002 else
15004 /* This window must be completely empty. */
15005 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15006 w->window_end_pos = make_number (Z - ZV);
15007 w->window_end_vpos = make_number (0);
15009 w->window_end_valid = Qnil;
15011 /* Update hint: don't try scrolling again in update_window. */
15012 w->desired_matrix->no_scrolling_p = 1;
15014 #if GLYPH_DEBUG
15015 debug_method_add (w, "try_window_reusing_current_matrix 1");
15016 #endif
15017 return 1;
15019 else if (CHARPOS (new_start) > CHARPOS (start))
15021 struct glyph_row *pt_row, *row;
15022 struct glyph_row *first_reusable_row;
15023 struct glyph_row *first_row_to_display;
15024 int dy;
15025 int yb = window_text_bottom_y (w);
15027 /* Find the row starting at new_start, if there is one. Don't
15028 reuse a partially visible line at the end. */
15029 first_reusable_row = start_row;
15030 while (first_reusable_row->enabled_p
15031 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15032 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15033 < CHARPOS (new_start)))
15034 ++first_reusable_row;
15036 /* Give up if there is no row to reuse. */
15037 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15038 || !first_reusable_row->enabled_p
15039 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15040 != CHARPOS (new_start)))
15041 return 0;
15043 /* We can reuse fully visible rows beginning with
15044 first_reusable_row to the end of the window. Set
15045 first_row_to_display to the first row that cannot be reused.
15046 Set pt_row to the row containing point, if there is any. */
15047 pt_row = NULL;
15048 for (first_row_to_display = first_reusable_row;
15049 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15050 ++first_row_to_display)
15052 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15053 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15054 pt_row = first_row_to_display;
15057 /* Start displaying at the start of first_row_to_display. */
15058 xassert (first_row_to_display->y < yb);
15059 init_to_row_start (&it, w, first_row_to_display);
15061 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15062 - start_vpos);
15063 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15064 - nrows_scrolled);
15065 it.current_y = (first_row_to_display->y - first_reusable_row->y
15066 + WINDOW_HEADER_LINE_HEIGHT (w));
15068 /* Display lines beginning with first_row_to_display in the
15069 desired matrix. Set last_text_row to the last row displayed
15070 that displays text. */
15071 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15072 if (pt_row == NULL)
15073 w->cursor.vpos = -1;
15074 last_text_row = NULL;
15075 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15076 if (display_line (&it))
15077 last_text_row = it.glyph_row - 1;
15079 /* If point is in a reused row, adjust y and vpos of the cursor
15080 position. */
15081 if (pt_row)
15083 w->cursor.vpos -= nrows_scrolled;
15084 w->cursor.y -= first_reusable_row->y - start_row->y;
15087 /* Give up if point isn't in a row displayed or reused. (This
15088 also handles the case where w->cursor.vpos < nrows_scrolled
15089 after the calls to display_line, which can happen with scroll
15090 margins. See bug#1295.) */
15091 if (w->cursor.vpos < 0)
15093 clear_glyph_matrix (w->desired_matrix);
15094 return 0;
15097 /* Scroll the display. */
15098 run.current_y = first_reusable_row->y;
15099 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15100 run.height = it.last_visible_y - run.current_y;
15101 dy = run.current_y - run.desired_y;
15103 if (run.height)
15105 update_begin (f);
15106 FRAME_RIF (f)->update_window_begin_hook (w);
15107 FRAME_RIF (f)->clear_window_mouse_face (w);
15108 FRAME_RIF (f)->scroll_run_hook (w, &run);
15109 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15110 update_end (f);
15113 /* Adjust Y positions of reused rows. */
15114 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15115 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15116 max_y = it.last_visible_y;
15117 for (row = first_reusable_row; row < first_row_to_display; ++row)
15119 row->y -= dy;
15120 row->visible_height = row->height;
15121 if (row->y < min_y)
15122 row->visible_height -= min_y - row->y;
15123 if (row->y + row->height > max_y)
15124 row->visible_height -= row->y + row->height - max_y;
15125 row->redraw_fringe_bitmaps_p = 1;
15128 /* Scroll the current matrix. */
15129 xassert (nrows_scrolled > 0);
15130 rotate_matrix (w->current_matrix,
15131 start_vpos,
15132 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15133 -nrows_scrolled);
15135 /* Disable rows not reused. */
15136 for (row -= nrows_scrolled; row < bottom_row; ++row)
15137 row->enabled_p = 0;
15139 /* Point may have moved to a different line, so we cannot assume that
15140 the previous cursor position is valid; locate the correct row. */
15141 if (pt_row)
15143 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15144 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15145 row++)
15147 w->cursor.vpos++;
15148 w->cursor.y = row->y;
15150 if (row < bottom_row)
15152 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15153 struct glyph *end = glyph + row->used[TEXT_AREA];
15155 /* Can't use this optimization with bidi-reordered glyph
15156 rows, unless cursor is already at point. */
15157 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15159 if (!(w->cursor.hpos >= 0
15160 && w->cursor.hpos < row->used[TEXT_AREA]
15161 && BUFFERP (glyph->object)
15162 && glyph->charpos == PT))
15163 return 0;
15165 else
15166 for (; glyph < end
15167 && (!BUFFERP (glyph->object)
15168 || glyph->charpos < PT);
15169 glyph++)
15171 w->cursor.hpos++;
15172 w->cursor.x += glyph->pixel_width;
15177 /* Adjust window end. A null value of last_text_row means that
15178 the window end is in reused rows which in turn means that
15179 only its vpos can have changed. */
15180 if (last_text_row)
15182 w->window_end_bytepos
15183 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15184 w->window_end_pos
15185 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15186 w->window_end_vpos
15187 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15189 else
15191 w->window_end_vpos
15192 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15195 w->window_end_valid = Qnil;
15196 w->desired_matrix->no_scrolling_p = 1;
15198 #if GLYPH_DEBUG
15199 debug_method_add (w, "try_window_reusing_current_matrix 2");
15200 #endif
15201 return 1;
15204 return 0;
15209 /************************************************************************
15210 Window redisplay reusing current matrix when buffer has changed
15211 ************************************************************************/
15213 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15214 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15215 EMACS_INT *, EMACS_INT *);
15216 static struct glyph_row *
15217 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15218 struct glyph_row *);
15221 /* Return the last row in MATRIX displaying text. If row START is
15222 non-null, start searching with that row. IT gives the dimensions
15223 of the display. Value is null if matrix is empty; otherwise it is
15224 a pointer to the row found. */
15226 static struct glyph_row *
15227 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15228 struct glyph_row *start)
15230 struct glyph_row *row, *row_found;
15232 /* Set row_found to the last row in IT->w's current matrix
15233 displaying text. The loop looks funny but think of partially
15234 visible lines. */
15235 row_found = NULL;
15236 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15237 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15239 xassert (row->enabled_p);
15240 row_found = row;
15241 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15242 break;
15243 ++row;
15246 return row_found;
15250 /* Return the last row in the current matrix of W that is not affected
15251 by changes at the start of current_buffer that occurred since W's
15252 current matrix was built. Value is null if no such row exists.
15254 BEG_UNCHANGED us the number of characters unchanged at the start of
15255 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15256 first changed character in current_buffer. Characters at positions <
15257 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15258 when the current matrix was built. */
15260 static struct glyph_row *
15261 find_last_unchanged_at_beg_row (struct window *w)
15263 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15264 struct glyph_row *row;
15265 struct glyph_row *row_found = NULL;
15266 int yb = window_text_bottom_y (w);
15268 /* Find the last row displaying unchanged text. */
15269 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15270 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15271 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15272 ++row)
15274 if (/* If row ends before first_changed_pos, it is unchanged,
15275 except in some case. */
15276 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15277 /* When row ends in ZV and we write at ZV it is not
15278 unchanged. */
15279 && !row->ends_at_zv_p
15280 /* When first_changed_pos is the end of a continued line,
15281 row is not unchanged because it may be no longer
15282 continued. */
15283 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15284 && (row->continued_p
15285 || row->exact_window_width_line_p)))
15286 row_found = row;
15288 /* Stop if last visible row. */
15289 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15290 break;
15293 return row_found;
15297 /* Find the first glyph row in the current matrix of W that is not
15298 affected by changes at the end of current_buffer since the
15299 time W's current matrix was built.
15301 Return in *DELTA the number of chars by which buffer positions in
15302 unchanged text at the end of current_buffer must be adjusted.
15304 Return in *DELTA_BYTES the corresponding number of bytes.
15306 Value is null if no such row exists, i.e. all rows are affected by
15307 changes. */
15309 static struct glyph_row *
15310 find_first_unchanged_at_end_row (struct window *w,
15311 EMACS_INT *delta, EMACS_INT *delta_bytes)
15313 struct glyph_row *row;
15314 struct glyph_row *row_found = NULL;
15316 *delta = *delta_bytes = 0;
15318 /* Display must not have been paused, otherwise the current matrix
15319 is not up to date. */
15320 eassert (!NILP (w->window_end_valid));
15322 /* A value of window_end_pos >= END_UNCHANGED means that the window
15323 end is in the range of changed text. If so, there is no
15324 unchanged row at the end of W's current matrix. */
15325 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15326 return NULL;
15328 /* Set row to the last row in W's current matrix displaying text. */
15329 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15331 /* If matrix is entirely empty, no unchanged row exists. */
15332 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15334 /* The value of row is the last glyph row in the matrix having a
15335 meaningful buffer position in it. The end position of row
15336 corresponds to window_end_pos. This allows us to translate
15337 buffer positions in the current matrix to current buffer
15338 positions for characters not in changed text. */
15339 EMACS_INT Z_old =
15340 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15341 EMACS_INT Z_BYTE_old =
15342 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15343 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15344 struct glyph_row *first_text_row
15345 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15347 *delta = Z - Z_old;
15348 *delta_bytes = Z_BYTE - Z_BYTE_old;
15350 /* Set last_unchanged_pos to the buffer position of the last
15351 character in the buffer that has not been changed. Z is the
15352 index + 1 of the last character in current_buffer, i.e. by
15353 subtracting END_UNCHANGED we get the index of the last
15354 unchanged character, and we have to add BEG to get its buffer
15355 position. */
15356 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15357 last_unchanged_pos_old = last_unchanged_pos - *delta;
15359 /* Search backward from ROW for a row displaying a line that
15360 starts at a minimum position >= last_unchanged_pos_old. */
15361 for (; row > first_text_row; --row)
15363 /* This used to abort, but it can happen.
15364 It is ok to just stop the search instead here. KFS. */
15365 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15366 break;
15368 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15369 row_found = row;
15373 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15375 return row_found;
15379 /* Make sure that glyph rows in the current matrix of window W
15380 reference the same glyph memory as corresponding rows in the
15381 frame's frame matrix. This function is called after scrolling W's
15382 current matrix on a terminal frame in try_window_id and
15383 try_window_reusing_current_matrix. */
15385 static void
15386 sync_frame_with_window_matrix_rows (struct window *w)
15388 struct frame *f = XFRAME (w->frame);
15389 struct glyph_row *window_row, *window_row_end, *frame_row;
15391 /* Preconditions: W must be a leaf window and full-width. Its frame
15392 must have a frame matrix. */
15393 xassert (NILP (w->hchild) && NILP (w->vchild));
15394 xassert (WINDOW_FULL_WIDTH_P (w));
15395 xassert (!FRAME_WINDOW_P (f));
15397 /* If W is a full-width window, glyph pointers in W's current matrix
15398 have, by definition, to be the same as glyph pointers in the
15399 corresponding frame matrix. Note that frame matrices have no
15400 marginal areas (see build_frame_matrix). */
15401 window_row = w->current_matrix->rows;
15402 window_row_end = window_row + w->current_matrix->nrows;
15403 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15404 while (window_row < window_row_end)
15406 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15407 struct glyph *end = window_row->glyphs[LAST_AREA];
15409 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15410 frame_row->glyphs[TEXT_AREA] = start;
15411 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15412 frame_row->glyphs[LAST_AREA] = end;
15414 /* Disable frame rows whose corresponding window rows have
15415 been disabled in try_window_id. */
15416 if (!window_row->enabled_p)
15417 frame_row->enabled_p = 0;
15419 ++window_row, ++frame_row;
15424 /* Find the glyph row in window W containing CHARPOS. Consider all
15425 rows between START and END (not inclusive). END null means search
15426 all rows to the end of the display area of W. Value is the row
15427 containing CHARPOS or null. */
15429 struct glyph_row *
15430 row_containing_pos (struct window *w, EMACS_INT charpos,
15431 struct glyph_row *start, struct glyph_row *end, int dy)
15433 struct glyph_row *row = start;
15434 struct glyph_row *best_row = NULL;
15435 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15436 int last_y;
15438 /* If we happen to start on a header-line, skip that. */
15439 if (row->mode_line_p)
15440 ++row;
15442 if ((end && row >= end) || !row->enabled_p)
15443 return NULL;
15445 last_y = window_text_bottom_y (w) - dy;
15447 while (1)
15449 /* Give up if we have gone too far. */
15450 if (end && row >= end)
15451 return NULL;
15452 /* This formerly returned if they were equal.
15453 I think that both quantities are of a "last plus one" type;
15454 if so, when they are equal, the row is within the screen. -- rms. */
15455 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15456 return NULL;
15458 /* If it is in this row, return this row. */
15459 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15460 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15461 /* The end position of a row equals the start
15462 position of the next row. If CHARPOS is there, we
15463 would rather display it in the next line, except
15464 when this line ends in ZV. */
15465 && !row->ends_at_zv_p
15466 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15467 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15469 struct glyph *g;
15471 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15472 || (!best_row && !row->continued_p))
15473 return row;
15474 /* In bidi-reordered rows, there could be several rows
15475 occluding point, all of them belonging to the same
15476 continued line. We need to find the row which fits
15477 CHARPOS the best. */
15478 for (g = row->glyphs[TEXT_AREA];
15479 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15480 g++)
15482 if (!STRINGP (g->object))
15484 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15486 mindif = eabs (g->charpos - charpos);
15487 best_row = row;
15488 /* Exact match always wins. */
15489 if (mindif == 0)
15490 return best_row;
15495 else if (best_row && !row->continued_p)
15496 return best_row;
15497 ++row;
15502 /* Try to redisplay window W by reusing its existing display. W's
15503 current matrix must be up to date when this function is called,
15504 i.e. window_end_valid must not be nil.
15506 Value is
15508 1 if display has been updated
15509 0 if otherwise unsuccessful
15510 -1 if redisplay with same window start is known not to succeed
15512 The following steps are performed:
15514 1. Find the last row in the current matrix of W that is not
15515 affected by changes at the start of current_buffer. If no such row
15516 is found, give up.
15518 2. Find the first row in W's current matrix that is not affected by
15519 changes at the end of current_buffer. Maybe there is no such row.
15521 3. Display lines beginning with the row + 1 found in step 1 to the
15522 row found in step 2 or, if step 2 didn't find a row, to the end of
15523 the window.
15525 4. If cursor is not known to appear on the window, give up.
15527 5. If display stopped at the row found in step 2, scroll the
15528 display and current matrix as needed.
15530 6. Maybe display some lines at the end of W, if we must. This can
15531 happen under various circumstances, like a partially visible line
15532 becoming fully visible, or because newly displayed lines are displayed
15533 in smaller font sizes.
15535 7. Update W's window end information. */
15537 static int
15538 try_window_id (struct window *w)
15540 struct frame *f = XFRAME (w->frame);
15541 struct glyph_matrix *current_matrix = w->current_matrix;
15542 struct glyph_matrix *desired_matrix = w->desired_matrix;
15543 struct glyph_row *last_unchanged_at_beg_row;
15544 struct glyph_row *first_unchanged_at_end_row;
15545 struct glyph_row *row;
15546 struct glyph_row *bottom_row;
15547 int bottom_vpos;
15548 struct it it;
15549 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15550 int dvpos, dy;
15551 struct text_pos start_pos;
15552 struct run run;
15553 int first_unchanged_at_end_vpos = 0;
15554 struct glyph_row *last_text_row, *last_text_row_at_end;
15555 struct text_pos start;
15556 EMACS_INT first_changed_charpos, last_changed_charpos;
15558 #if GLYPH_DEBUG
15559 if (inhibit_try_window_id)
15560 return 0;
15561 #endif
15563 /* This is handy for debugging. */
15564 #if 0
15565 #define GIVE_UP(X) \
15566 do { \
15567 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15568 return 0; \
15569 } while (0)
15570 #else
15571 #define GIVE_UP(X) return 0
15572 #endif
15574 SET_TEXT_POS_FROM_MARKER (start, w->start);
15576 /* Don't use this for mini-windows because these can show
15577 messages and mini-buffers, and we don't handle that here. */
15578 if (MINI_WINDOW_P (w))
15579 GIVE_UP (1);
15581 /* This flag is used to prevent redisplay optimizations. */
15582 if (windows_or_buffers_changed || cursor_type_changed)
15583 GIVE_UP (2);
15585 /* Verify that narrowing has not changed.
15586 Also verify that we were not told to prevent redisplay optimizations.
15587 It would be nice to further
15588 reduce the number of cases where this prevents try_window_id. */
15589 if (current_buffer->clip_changed
15590 || current_buffer->prevent_redisplay_optimizations_p)
15591 GIVE_UP (3);
15593 /* Window must either use window-based redisplay or be full width. */
15594 if (!FRAME_WINDOW_P (f)
15595 && (!FRAME_LINE_INS_DEL_OK (f)
15596 || !WINDOW_FULL_WIDTH_P (w)))
15597 GIVE_UP (4);
15599 /* Give up if point is known NOT to appear in W. */
15600 if (PT < CHARPOS (start))
15601 GIVE_UP (5);
15603 /* Another way to prevent redisplay optimizations. */
15604 if (XFASTINT (w->last_modified) == 0)
15605 GIVE_UP (6);
15607 /* Verify that window is not hscrolled. */
15608 if (XFASTINT (w->hscroll) != 0)
15609 GIVE_UP (7);
15611 /* Verify that display wasn't paused. */
15612 if (NILP (w->window_end_valid))
15613 GIVE_UP (8);
15615 /* Can't use this if highlighting a region because a cursor movement
15616 will do more than just set the cursor. */
15617 if (!NILP (Vtransient_mark_mode)
15618 && !NILP (current_buffer->mark_active))
15619 GIVE_UP (9);
15621 /* Likewise if highlighting trailing whitespace. */
15622 if (!NILP (Vshow_trailing_whitespace))
15623 GIVE_UP (11);
15625 /* Likewise if showing a region. */
15626 if (!NILP (w->region_showing))
15627 GIVE_UP (10);
15629 /* Can't use this if overlay arrow position and/or string have
15630 changed. */
15631 if (overlay_arrows_changed_p ())
15632 GIVE_UP (12);
15634 /* When word-wrap is on, adding a space to the first word of a
15635 wrapped line can change the wrap position, altering the line
15636 above it. It might be worthwhile to handle this more
15637 intelligently, but for now just redisplay from scratch. */
15638 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15639 GIVE_UP (21);
15641 /* Under bidi reordering, adding or deleting a character in the
15642 beginning of a paragraph, before the first strong directional
15643 character, can change the base direction of the paragraph (unless
15644 the buffer specifies a fixed paragraph direction), which will
15645 require to redisplay the whole paragraph. It might be worthwhile
15646 to find the paragraph limits and widen the range of redisplayed
15647 lines to that, but for now just give up this optimization and
15648 redisplay from scratch. */
15649 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15650 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15651 GIVE_UP (22);
15653 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15654 only if buffer has really changed. The reason is that the gap is
15655 initially at Z for freshly visited files. The code below would
15656 set end_unchanged to 0 in that case. */
15657 if (MODIFF > SAVE_MODIFF
15658 /* This seems to happen sometimes after saving a buffer. */
15659 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15661 if (GPT - BEG < BEG_UNCHANGED)
15662 BEG_UNCHANGED = GPT - BEG;
15663 if (Z - GPT < END_UNCHANGED)
15664 END_UNCHANGED = Z - GPT;
15667 /* The position of the first and last character that has been changed. */
15668 first_changed_charpos = BEG + BEG_UNCHANGED;
15669 last_changed_charpos = Z - END_UNCHANGED;
15671 /* If window starts after a line end, and the last change is in
15672 front of that newline, then changes don't affect the display.
15673 This case happens with stealth-fontification. Note that although
15674 the display is unchanged, glyph positions in the matrix have to
15675 be adjusted, of course. */
15676 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15677 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15678 && ((last_changed_charpos < CHARPOS (start)
15679 && CHARPOS (start) == BEGV)
15680 || (last_changed_charpos < CHARPOS (start) - 1
15681 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15683 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15684 struct glyph_row *r0;
15686 /* Compute how many chars/bytes have been added to or removed
15687 from the buffer. */
15688 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15689 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15690 delta = Z - Z_old;
15691 delta_bytes = Z_BYTE - Z_BYTE_old;
15693 /* Give up if PT is not in the window. Note that it already has
15694 been checked at the start of try_window_id that PT is not in
15695 front of the window start. */
15696 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15697 GIVE_UP (13);
15699 /* If window start is unchanged, we can reuse the whole matrix
15700 as is, after adjusting glyph positions. No need to compute
15701 the window end again, since its offset from Z hasn't changed. */
15702 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15703 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15704 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15705 /* PT must not be in a partially visible line. */
15706 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15707 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15709 /* Adjust positions in the glyph matrix. */
15710 if (delta || delta_bytes)
15712 struct glyph_row *r1
15713 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15714 increment_matrix_positions (w->current_matrix,
15715 MATRIX_ROW_VPOS (r0, current_matrix),
15716 MATRIX_ROW_VPOS (r1, current_matrix),
15717 delta, delta_bytes);
15720 /* Set the cursor. */
15721 row = row_containing_pos (w, PT, r0, NULL, 0);
15722 if (row)
15723 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15724 else
15725 abort ();
15726 return 1;
15730 /* Handle the case that changes are all below what is displayed in
15731 the window, and that PT is in the window. This shortcut cannot
15732 be taken if ZV is visible in the window, and text has been added
15733 there that is visible in the window. */
15734 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15735 /* ZV is not visible in the window, or there are no
15736 changes at ZV, actually. */
15737 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15738 || first_changed_charpos == last_changed_charpos))
15740 struct glyph_row *r0;
15742 /* Give up if PT is not in the window. Note that it already has
15743 been checked at the start of try_window_id that PT is not in
15744 front of the window start. */
15745 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15746 GIVE_UP (14);
15748 /* If window start is unchanged, we can reuse the whole matrix
15749 as is, without changing glyph positions since no text has
15750 been added/removed in front of the window end. */
15751 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15752 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15753 /* PT must not be in a partially visible line. */
15754 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15755 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15757 /* We have to compute the window end anew since text
15758 could have been added/removed after it. */
15759 w->window_end_pos
15760 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15761 w->window_end_bytepos
15762 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15764 /* Set the cursor. */
15765 row = row_containing_pos (w, PT, r0, NULL, 0);
15766 if (row)
15767 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15768 else
15769 abort ();
15770 return 2;
15774 /* Give up if window start is in the changed area.
15776 The condition used to read
15778 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15780 but why that was tested escapes me at the moment. */
15781 if (CHARPOS (start) >= first_changed_charpos
15782 && CHARPOS (start) <= last_changed_charpos)
15783 GIVE_UP (15);
15785 /* Check that window start agrees with the start of the first glyph
15786 row in its current matrix. Check this after we know the window
15787 start is not in changed text, otherwise positions would not be
15788 comparable. */
15789 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15790 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15791 GIVE_UP (16);
15793 /* Give up if the window ends in strings. Overlay strings
15794 at the end are difficult to handle, so don't try. */
15795 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15796 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15797 GIVE_UP (20);
15799 /* Compute the position at which we have to start displaying new
15800 lines. Some of the lines at the top of the window might be
15801 reusable because they are not displaying changed text. Find the
15802 last row in W's current matrix not affected by changes at the
15803 start of current_buffer. Value is null if changes start in the
15804 first line of window. */
15805 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15806 if (last_unchanged_at_beg_row)
15808 /* Avoid starting to display in the moddle of a character, a TAB
15809 for instance. This is easier than to set up the iterator
15810 exactly, and it's not a frequent case, so the additional
15811 effort wouldn't really pay off. */
15812 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15813 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15814 && last_unchanged_at_beg_row > w->current_matrix->rows)
15815 --last_unchanged_at_beg_row;
15817 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15818 GIVE_UP (17);
15820 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15821 GIVE_UP (18);
15822 start_pos = it.current.pos;
15824 /* Start displaying new lines in the desired matrix at the same
15825 vpos we would use in the current matrix, i.e. below
15826 last_unchanged_at_beg_row. */
15827 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15828 current_matrix);
15829 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15830 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15832 xassert (it.hpos == 0 && it.current_x == 0);
15834 else
15836 /* There are no reusable lines at the start of the window.
15837 Start displaying in the first text line. */
15838 start_display (&it, w, start);
15839 it.vpos = it.first_vpos;
15840 start_pos = it.current.pos;
15843 /* Find the first row that is not affected by changes at the end of
15844 the buffer. Value will be null if there is no unchanged row, in
15845 which case we must redisplay to the end of the window. delta
15846 will be set to the value by which buffer positions beginning with
15847 first_unchanged_at_end_row have to be adjusted due to text
15848 changes. */
15849 first_unchanged_at_end_row
15850 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15851 IF_DEBUG (debug_delta = delta);
15852 IF_DEBUG (debug_delta_bytes = delta_bytes);
15854 /* Set stop_pos to the buffer position up to which we will have to
15855 display new lines. If first_unchanged_at_end_row != NULL, this
15856 is the buffer position of the start of the line displayed in that
15857 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15858 that we don't stop at a buffer position. */
15859 stop_pos = 0;
15860 if (first_unchanged_at_end_row)
15862 xassert (last_unchanged_at_beg_row == NULL
15863 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15865 /* If this is a continuation line, move forward to the next one
15866 that isn't. Changes in lines above affect this line.
15867 Caution: this may move first_unchanged_at_end_row to a row
15868 not displaying text. */
15869 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15870 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15871 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15872 < it.last_visible_y))
15873 ++first_unchanged_at_end_row;
15875 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15876 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15877 >= it.last_visible_y))
15878 first_unchanged_at_end_row = NULL;
15879 else
15881 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15882 + delta);
15883 first_unchanged_at_end_vpos
15884 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15885 xassert (stop_pos >= Z - END_UNCHANGED);
15888 else if (last_unchanged_at_beg_row == NULL)
15889 GIVE_UP (19);
15892 #if GLYPH_DEBUG
15894 /* Either there is no unchanged row at the end, or the one we have
15895 now displays text. This is a necessary condition for the window
15896 end pos calculation at the end of this function. */
15897 xassert (first_unchanged_at_end_row == NULL
15898 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15900 debug_last_unchanged_at_beg_vpos
15901 = (last_unchanged_at_beg_row
15902 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15903 : -1);
15904 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15906 #endif /* GLYPH_DEBUG != 0 */
15909 /* Display new lines. Set last_text_row to the last new line
15910 displayed which has text on it, i.e. might end up as being the
15911 line where the window_end_vpos is. */
15912 w->cursor.vpos = -1;
15913 last_text_row = NULL;
15914 overlay_arrow_seen = 0;
15915 while (it.current_y < it.last_visible_y
15916 && !fonts_changed_p
15917 && (first_unchanged_at_end_row == NULL
15918 || IT_CHARPOS (it) < stop_pos))
15920 if (display_line (&it))
15921 last_text_row = it.glyph_row - 1;
15924 if (fonts_changed_p)
15925 return -1;
15928 /* Compute differences in buffer positions, y-positions etc. for
15929 lines reused at the bottom of the window. Compute what we can
15930 scroll. */
15931 if (first_unchanged_at_end_row
15932 /* No lines reused because we displayed everything up to the
15933 bottom of the window. */
15934 && it.current_y < it.last_visible_y)
15936 dvpos = (it.vpos
15937 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15938 current_matrix));
15939 dy = it.current_y - first_unchanged_at_end_row->y;
15940 run.current_y = first_unchanged_at_end_row->y;
15941 run.desired_y = run.current_y + dy;
15942 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15944 else
15946 delta = delta_bytes = dvpos = dy
15947 = run.current_y = run.desired_y = run.height = 0;
15948 first_unchanged_at_end_row = NULL;
15950 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15953 /* Find the cursor if not already found. We have to decide whether
15954 PT will appear on this window (it sometimes doesn't, but this is
15955 not a very frequent case.) This decision has to be made before
15956 the current matrix is altered. A value of cursor.vpos < 0 means
15957 that PT is either in one of the lines beginning at
15958 first_unchanged_at_end_row or below the window. Don't care for
15959 lines that might be displayed later at the window end; as
15960 mentioned, this is not a frequent case. */
15961 if (w->cursor.vpos < 0)
15963 /* Cursor in unchanged rows at the top? */
15964 if (PT < CHARPOS (start_pos)
15965 && last_unchanged_at_beg_row)
15967 row = row_containing_pos (w, PT,
15968 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15969 last_unchanged_at_beg_row + 1, 0);
15970 if (row)
15971 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15974 /* Start from first_unchanged_at_end_row looking for PT. */
15975 else if (first_unchanged_at_end_row)
15977 row = row_containing_pos (w, PT - delta,
15978 first_unchanged_at_end_row, NULL, 0);
15979 if (row)
15980 set_cursor_from_row (w, row, w->current_matrix, delta,
15981 delta_bytes, dy, dvpos);
15984 /* Give up if cursor was not found. */
15985 if (w->cursor.vpos < 0)
15987 clear_glyph_matrix (w->desired_matrix);
15988 return -1;
15992 /* Don't let the cursor end in the scroll margins. */
15994 int this_scroll_margin, cursor_height;
15996 this_scroll_margin = max (0, scroll_margin);
15997 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15998 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15999 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16001 if ((w->cursor.y < this_scroll_margin
16002 && CHARPOS (start) > BEGV)
16003 /* Old redisplay didn't take scroll margin into account at the bottom,
16004 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16005 || (w->cursor.y + (make_cursor_line_fully_visible_p
16006 ? cursor_height + this_scroll_margin
16007 : 1)) > it.last_visible_y)
16009 w->cursor.vpos = -1;
16010 clear_glyph_matrix (w->desired_matrix);
16011 return -1;
16015 /* Scroll the display. Do it before changing the current matrix so
16016 that xterm.c doesn't get confused about where the cursor glyph is
16017 found. */
16018 if (dy && run.height)
16020 update_begin (f);
16022 if (FRAME_WINDOW_P (f))
16024 FRAME_RIF (f)->update_window_begin_hook (w);
16025 FRAME_RIF (f)->clear_window_mouse_face (w);
16026 FRAME_RIF (f)->scroll_run_hook (w, &run);
16027 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16029 else
16031 /* Terminal frame. In this case, dvpos gives the number of
16032 lines to scroll by; dvpos < 0 means scroll up. */
16033 int first_unchanged_at_end_vpos
16034 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16035 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
16036 int end = (WINDOW_TOP_EDGE_LINE (w)
16037 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16038 + window_internal_height (w));
16040 #if defined (HAVE_GPM) || defined (MSDOS)
16041 x_clear_window_mouse_face (w);
16042 #endif
16043 /* Perform the operation on the screen. */
16044 if (dvpos > 0)
16046 /* Scroll last_unchanged_at_beg_row to the end of the
16047 window down dvpos lines. */
16048 set_terminal_window (f, end);
16050 /* On dumb terminals delete dvpos lines at the end
16051 before inserting dvpos empty lines. */
16052 if (!FRAME_SCROLL_REGION_OK (f))
16053 ins_del_lines (f, end - dvpos, -dvpos);
16055 /* Insert dvpos empty lines in front of
16056 last_unchanged_at_beg_row. */
16057 ins_del_lines (f, from, dvpos);
16059 else if (dvpos < 0)
16061 /* Scroll up last_unchanged_at_beg_vpos to the end of
16062 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16063 set_terminal_window (f, end);
16065 /* Delete dvpos lines in front of
16066 last_unchanged_at_beg_vpos. ins_del_lines will set
16067 the cursor to the given vpos and emit |dvpos| delete
16068 line sequences. */
16069 ins_del_lines (f, from + dvpos, dvpos);
16071 /* On a dumb terminal insert dvpos empty lines at the
16072 end. */
16073 if (!FRAME_SCROLL_REGION_OK (f))
16074 ins_del_lines (f, end + dvpos, -dvpos);
16077 set_terminal_window (f, 0);
16080 update_end (f);
16083 /* Shift reused rows of the current matrix to the right position.
16084 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16085 text. */
16086 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16087 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16088 if (dvpos < 0)
16090 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16091 bottom_vpos, dvpos);
16092 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16093 bottom_vpos, 0);
16095 else if (dvpos > 0)
16097 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16098 bottom_vpos, dvpos);
16099 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16100 first_unchanged_at_end_vpos + dvpos, 0);
16103 /* For frame-based redisplay, make sure that current frame and window
16104 matrix are in sync with respect to glyph memory. */
16105 if (!FRAME_WINDOW_P (f))
16106 sync_frame_with_window_matrix_rows (w);
16108 /* Adjust buffer positions in reused rows. */
16109 if (delta || delta_bytes)
16110 increment_matrix_positions (current_matrix,
16111 first_unchanged_at_end_vpos + dvpos,
16112 bottom_vpos, delta, delta_bytes);
16114 /* Adjust Y positions. */
16115 if (dy)
16116 shift_glyph_matrix (w, current_matrix,
16117 first_unchanged_at_end_vpos + dvpos,
16118 bottom_vpos, dy);
16120 if (first_unchanged_at_end_row)
16122 first_unchanged_at_end_row += dvpos;
16123 if (first_unchanged_at_end_row->y >= it.last_visible_y
16124 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16125 first_unchanged_at_end_row = NULL;
16128 /* If scrolling up, there may be some lines to display at the end of
16129 the window. */
16130 last_text_row_at_end = NULL;
16131 if (dy < 0)
16133 /* Scrolling up can leave for example a partially visible line
16134 at the end of the window to be redisplayed. */
16135 /* Set last_row to the glyph row in the current matrix where the
16136 window end line is found. It has been moved up or down in
16137 the matrix by dvpos. */
16138 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16139 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16141 /* If last_row is the window end line, it should display text. */
16142 xassert (last_row->displays_text_p);
16144 /* If window end line was partially visible before, begin
16145 displaying at that line. Otherwise begin displaying with the
16146 line following it. */
16147 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16149 init_to_row_start (&it, w, last_row);
16150 it.vpos = last_vpos;
16151 it.current_y = last_row->y;
16153 else
16155 init_to_row_end (&it, w, last_row);
16156 it.vpos = 1 + last_vpos;
16157 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16158 ++last_row;
16161 /* We may start in a continuation line. If so, we have to
16162 get the right continuation_lines_width and current_x. */
16163 it.continuation_lines_width = last_row->continuation_lines_width;
16164 it.hpos = it.current_x = 0;
16166 /* Display the rest of the lines at the window end. */
16167 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16168 while (it.current_y < it.last_visible_y
16169 && !fonts_changed_p)
16171 /* Is it always sure that the display agrees with lines in
16172 the current matrix? I don't think so, so we mark rows
16173 displayed invalid in the current matrix by setting their
16174 enabled_p flag to zero. */
16175 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16176 if (display_line (&it))
16177 last_text_row_at_end = it.glyph_row - 1;
16181 /* Update window_end_pos and window_end_vpos. */
16182 if (first_unchanged_at_end_row
16183 && !last_text_row_at_end)
16185 /* Window end line if one of the preserved rows from the current
16186 matrix. Set row to the last row displaying text in current
16187 matrix starting at first_unchanged_at_end_row, after
16188 scrolling. */
16189 xassert (first_unchanged_at_end_row->displays_text_p);
16190 row = find_last_row_displaying_text (w->current_matrix, &it,
16191 first_unchanged_at_end_row);
16192 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16194 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16195 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16196 w->window_end_vpos
16197 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16198 xassert (w->window_end_bytepos >= 0);
16199 IF_DEBUG (debug_method_add (w, "A"));
16201 else if (last_text_row_at_end)
16203 w->window_end_pos
16204 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16205 w->window_end_bytepos
16206 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16207 w->window_end_vpos
16208 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16209 xassert (w->window_end_bytepos >= 0);
16210 IF_DEBUG (debug_method_add (w, "B"));
16212 else if (last_text_row)
16214 /* We have displayed either to the end of the window or at the
16215 end of the window, i.e. the last row with text is to be found
16216 in the desired matrix. */
16217 w->window_end_pos
16218 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16219 w->window_end_bytepos
16220 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16221 w->window_end_vpos
16222 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16223 xassert (w->window_end_bytepos >= 0);
16225 else if (first_unchanged_at_end_row == NULL
16226 && last_text_row == NULL
16227 && last_text_row_at_end == NULL)
16229 /* Displayed to end of window, but no line containing text was
16230 displayed. Lines were deleted at the end of the window. */
16231 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16232 int vpos = XFASTINT (w->window_end_vpos);
16233 struct glyph_row *current_row = current_matrix->rows + vpos;
16234 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16236 for (row = NULL;
16237 row == NULL && vpos >= first_vpos;
16238 --vpos, --current_row, --desired_row)
16240 if (desired_row->enabled_p)
16242 if (desired_row->displays_text_p)
16243 row = desired_row;
16245 else if (current_row->displays_text_p)
16246 row = current_row;
16249 xassert (row != NULL);
16250 w->window_end_vpos = make_number (vpos + 1);
16251 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16252 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16253 xassert (w->window_end_bytepos >= 0);
16254 IF_DEBUG (debug_method_add (w, "C"));
16256 else
16257 abort ();
16259 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16260 debug_end_vpos = XFASTINT (w->window_end_vpos));
16262 /* Record that display has not been completed. */
16263 w->window_end_valid = Qnil;
16264 w->desired_matrix->no_scrolling_p = 1;
16265 return 3;
16267 #undef GIVE_UP
16272 /***********************************************************************
16273 More debugging support
16274 ***********************************************************************/
16276 #if GLYPH_DEBUG
16278 void dump_glyph_row (struct glyph_row *, int, int);
16279 void dump_glyph_matrix (struct glyph_matrix *, int);
16280 void dump_glyph (struct glyph_row *, struct glyph *, int);
16283 /* Dump the contents of glyph matrix MATRIX on stderr.
16285 GLYPHS 0 means don't show glyph contents.
16286 GLYPHS 1 means show glyphs in short form
16287 GLYPHS > 1 means show glyphs in long form. */
16289 void
16290 dump_glyph_matrix (matrix, glyphs)
16291 struct glyph_matrix *matrix;
16292 int glyphs;
16294 int i;
16295 for (i = 0; i < matrix->nrows; ++i)
16296 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16300 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16301 the glyph row and area where the glyph comes from. */
16303 void
16304 dump_glyph (row, glyph, area)
16305 struct glyph_row *row;
16306 struct glyph *glyph;
16307 int area;
16309 if (glyph->type == CHAR_GLYPH)
16311 fprintf (stderr,
16312 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16313 glyph - row->glyphs[TEXT_AREA],
16314 'C',
16315 glyph->charpos,
16316 (BUFFERP (glyph->object)
16317 ? 'B'
16318 : (STRINGP (glyph->object)
16319 ? 'S'
16320 : '-')),
16321 glyph->pixel_width,
16322 glyph->u.ch,
16323 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16324 ? glyph->u.ch
16325 : '.'),
16326 glyph->face_id,
16327 glyph->left_box_line_p,
16328 glyph->right_box_line_p);
16330 else if (glyph->type == STRETCH_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 'S',
16336 glyph->charpos,
16337 (BUFFERP (glyph->object)
16338 ? 'B'
16339 : (STRINGP (glyph->object)
16340 ? 'S'
16341 : '-')),
16342 glyph->pixel_width,
16344 '.',
16345 glyph->face_id,
16346 glyph->left_box_line_p,
16347 glyph->right_box_line_p);
16349 else if (glyph->type == IMAGE_GLYPH)
16351 fprintf (stderr,
16352 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16353 glyph - row->glyphs[TEXT_AREA],
16354 'I',
16355 glyph->charpos,
16356 (BUFFERP (glyph->object)
16357 ? 'B'
16358 : (STRINGP (glyph->object)
16359 ? 'S'
16360 : '-')),
16361 glyph->pixel_width,
16362 glyph->u.img_id,
16363 '.',
16364 glyph->face_id,
16365 glyph->left_box_line_p,
16366 glyph->right_box_line_p);
16368 else if (glyph->type == COMPOSITE_GLYPH)
16370 fprintf (stderr,
16371 " %5d %4c %6d %c %3d 0x%05x",
16372 glyph - row->glyphs[TEXT_AREA],
16373 '+',
16374 glyph->charpos,
16375 (BUFFERP (glyph->object)
16376 ? 'B'
16377 : (STRINGP (glyph->object)
16378 ? 'S'
16379 : '-')),
16380 glyph->pixel_width,
16381 glyph->u.cmp.id);
16382 if (glyph->u.cmp.automatic)
16383 fprintf (stderr,
16384 "[%d-%d]",
16385 glyph->slice.cmp.from, glyph->slice.cmp.to);
16386 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16387 glyph->face_id,
16388 glyph->left_box_line_p,
16389 glyph->right_box_line_p);
16394 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16395 GLYPHS 0 means don't show glyph contents.
16396 GLYPHS 1 means show glyphs in short form
16397 GLYPHS > 1 means show glyphs in long form. */
16399 void
16400 dump_glyph_row (row, vpos, glyphs)
16401 struct glyph_row *row;
16402 int vpos, glyphs;
16404 if (glyphs != 1)
16406 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16407 fprintf (stderr, "======================================================================\n");
16409 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16410 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16411 vpos,
16412 MATRIX_ROW_START_CHARPOS (row),
16413 MATRIX_ROW_END_CHARPOS (row),
16414 row->used[TEXT_AREA],
16415 row->contains_overlapping_glyphs_p,
16416 row->enabled_p,
16417 row->truncated_on_left_p,
16418 row->truncated_on_right_p,
16419 row->continued_p,
16420 MATRIX_ROW_CONTINUATION_LINE_P (row),
16421 row->displays_text_p,
16422 row->ends_at_zv_p,
16423 row->fill_line_p,
16424 row->ends_in_middle_of_char_p,
16425 row->starts_in_middle_of_char_p,
16426 row->mouse_face_p,
16427 row->x,
16428 row->y,
16429 row->pixel_width,
16430 row->height,
16431 row->visible_height,
16432 row->ascent,
16433 row->phys_ascent);
16434 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16435 row->end.overlay_string_index,
16436 row->continuation_lines_width);
16437 fprintf (stderr, "%9d %5d\n",
16438 CHARPOS (row->start.string_pos),
16439 CHARPOS (row->end.string_pos));
16440 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16441 row->end.dpvec_index);
16444 if (glyphs > 1)
16446 int area;
16448 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16450 struct glyph *glyph = row->glyphs[area];
16451 struct glyph *glyph_end = glyph + row->used[area];
16453 /* Glyph for a line end in text. */
16454 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16455 ++glyph_end;
16457 if (glyph < glyph_end)
16458 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16460 for (; glyph < glyph_end; ++glyph)
16461 dump_glyph (row, glyph, area);
16464 else if (glyphs == 1)
16466 int area;
16468 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16470 char *s = (char *) alloca (row->used[area] + 1);
16471 int i;
16473 for (i = 0; i < row->used[area]; ++i)
16475 struct glyph *glyph = row->glyphs[area] + i;
16476 if (glyph->type == CHAR_GLYPH
16477 && glyph->u.ch < 0x80
16478 && glyph->u.ch >= ' ')
16479 s[i] = glyph->u.ch;
16480 else
16481 s[i] = '.';
16484 s[i] = '\0';
16485 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16491 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16492 Sdump_glyph_matrix, 0, 1, "p",
16493 doc: /* Dump the current matrix of the selected window to stderr.
16494 Shows contents of glyph row structures. With non-nil
16495 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16496 glyphs in short form, otherwise show glyphs in long form. */)
16497 (Lisp_Object glyphs)
16499 struct window *w = XWINDOW (selected_window);
16500 struct buffer *buffer = XBUFFER (w->buffer);
16502 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16503 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16504 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16505 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16506 fprintf (stderr, "=============================================\n");
16507 dump_glyph_matrix (w->current_matrix,
16508 NILP (glyphs) ? 0 : XINT (glyphs));
16509 return Qnil;
16513 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16514 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16515 (void)
16517 struct frame *f = XFRAME (selected_frame);
16518 dump_glyph_matrix (f->current_matrix, 1);
16519 return Qnil;
16523 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16524 doc: /* Dump glyph row ROW to stderr.
16525 GLYPH 0 means don't dump glyphs.
16526 GLYPH 1 means dump glyphs in short form.
16527 GLYPH > 1 or omitted means dump glyphs in long form. */)
16528 (Lisp_Object row, Lisp_Object glyphs)
16530 struct glyph_matrix *matrix;
16531 int vpos;
16533 CHECK_NUMBER (row);
16534 matrix = XWINDOW (selected_window)->current_matrix;
16535 vpos = XINT (row);
16536 if (vpos >= 0 && vpos < matrix->nrows)
16537 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16538 vpos,
16539 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16540 return Qnil;
16544 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16545 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16546 GLYPH 0 means don't dump glyphs.
16547 GLYPH 1 means dump glyphs in short form.
16548 GLYPH > 1 or omitted means dump glyphs in long form. */)
16549 (Lisp_Object row, Lisp_Object glyphs)
16551 struct frame *sf = SELECTED_FRAME ();
16552 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16553 int vpos;
16555 CHECK_NUMBER (row);
16556 vpos = XINT (row);
16557 if (vpos >= 0 && vpos < m->nrows)
16558 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16559 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16560 return Qnil;
16564 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16565 doc: /* Toggle tracing of redisplay.
16566 With ARG, turn tracing on if and only if ARG is positive. */)
16567 (Lisp_Object arg)
16569 if (NILP (arg))
16570 trace_redisplay_p = !trace_redisplay_p;
16571 else
16573 arg = Fprefix_numeric_value (arg);
16574 trace_redisplay_p = XINT (arg) > 0;
16577 return Qnil;
16581 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16582 doc: /* Like `format', but print result to stderr.
16583 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16584 (int nargs, Lisp_Object *args)
16586 Lisp_Object s = Fformat (nargs, args);
16587 fprintf (stderr, "%s", SDATA (s));
16588 return Qnil;
16591 #endif /* GLYPH_DEBUG */
16595 /***********************************************************************
16596 Building Desired Matrix Rows
16597 ***********************************************************************/
16599 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16600 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16602 static struct glyph_row *
16603 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16605 struct frame *f = XFRAME (WINDOW_FRAME (w));
16606 struct buffer *buffer = XBUFFER (w->buffer);
16607 struct buffer *old = current_buffer;
16608 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16609 int arrow_len = SCHARS (overlay_arrow_string);
16610 const unsigned char *arrow_end = arrow_string + arrow_len;
16611 const unsigned char *p;
16612 struct it it;
16613 int multibyte_p;
16614 int n_glyphs_before;
16616 set_buffer_temp (buffer);
16617 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16618 it.glyph_row->used[TEXT_AREA] = 0;
16619 SET_TEXT_POS (it.position, 0, 0);
16621 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16622 p = arrow_string;
16623 while (p < arrow_end)
16625 Lisp_Object face, ilisp;
16627 /* Get the next character. */
16628 if (multibyte_p)
16629 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16630 else
16632 it.c = it.char_to_display = *p, it.len = 1;
16633 if (! ASCII_CHAR_P (it.c))
16634 it.char_to_display = BYTE8_TO_CHAR (it.c);
16636 p += it.len;
16638 /* Get its face. */
16639 ilisp = make_number (p - arrow_string);
16640 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16641 it.face_id = compute_char_face (f, it.char_to_display, face);
16643 /* Compute its width, get its glyphs. */
16644 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16645 SET_TEXT_POS (it.position, -1, -1);
16646 PRODUCE_GLYPHS (&it);
16648 /* If this character doesn't fit any more in the line, we have
16649 to remove some glyphs. */
16650 if (it.current_x > it.last_visible_x)
16652 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16653 break;
16657 set_buffer_temp (old);
16658 return it.glyph_row;
16662 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16663 glyphs are only inserted for terminal frames since we can't really
16664 win with truncation glyphs when partially visible glyphs are
16665 involved. Which glyphs to insert is determined by
16666 produce_special_glyphs. */
16668 static void
16669 insert_left_trunc_glyphs (struct it *it)
16671 struct it truncate_it;
16672 struct glyph *from, *end, *to, *toend;
16674 xassert (!FRAME_WINDOW_P (it->f));
16676 /* Get the truncation glyphs. */
16677 truncate_it = *it;
16678 truncate_it.current_x = 0;
16679 truncate_it.face_id = DEFAULT_FACE_ID;
16680 truncate_it.glyph_row = &scratch_glyph_row;
16681 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16682 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16683 truncate_it.object = make_number (0);
16684 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16686 /* Overwrite glyphs from IT with truncation glyphs. */
16687 if (!it->glyph_row->reversed_p)
16689 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16690 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16691 to = it->glyph_row->glyphs[TEXT_AREA];
16692 toend = to + it->glyph_row->used[TEXT_AREA];
16694 while (from < end)
16695 *to++ = *from++;
16697 /* There may be padding glyphs left over. Overwrite them too. */
16698 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16700 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16701 while (from < end)
16702 *to++ = *from++;
16705 if (to > toend)
16706 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16708 else
16710 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16711 that back to front. */
16712 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16713 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16714 toend = it->glyph_row->glyphs[TEXT_AREA];
16715 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16717 while (from >= end && to >= toend)
16718 *to-- = *from--;
16719 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16721 from =
16722 truncate_it.glyph_row->glyphs[TEXT_AREA]
16723 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16724 while (from >= end && to >= toend)
16725 *to-- = *from--;
16727 if (from >= end)
16729 /* Need to free some room before prepending additional
16730 glyphs. */
16731 int move_by = from - end + 1;
16732 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16733 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16735 for ( ; g >= g0; g--)
16736 g[move_by] = *g;
16737 while (from >= end)
16738 *to-- = *from--;
16739 it->glyph_row->used[TEXT_AREA] += move_by;
16745 /* Compute the pixel height and width of IT->glyph_row.
16747 Most of the time, ascent and height of a display line will be equal
16748 to the max_ascent and max_height values of the display iterator
16749 structure. This is not the case if
16751 1. We hit ZV without displaying anything. In this case, max_ascent
16752 and max_height will be zero.
16754 2. We have some glyphs that don't contribute to the line height.
16755 (The glyph row flag contributes_to_line_height_p is for future
16756 pixmap extensions).
16758 The first case is easily covered by using default values because in
16759 these cases, the line height does not really matter, except that it
16760 must not be zero. */
16762 static void
16763 compute_line_metrics (struct it *it)
16765 struct glyph_row *row = it->glyph_row;
16766 int area, i;
16768 if (FRAME_WINDOW_P (it->f))
16770 int i, min_y, max_y;
16772 /* The line may consist of one space only, that was added to
16773 place the cursor on it. If so, the row's height hasn't been
16774 computed yet. */
16775 if (row->height == 0)
16777 if (it->max_ascent + it->max_descent == 0)
16778 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16779 row->ascent = it->max_ascent;
16780 row->height = it->max_ascent + it->max_descent;
16781 row->phys_ascent = it->max_phys_ascent;
16782 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16783 row->extra_line_spacing = it->max_extra_line_spacing;
16786 /* Compute the width of this line. */
16787 row->pixel_width = row->x;
16788 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16789 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16791 xassert (row->pixel_width >= 0);
16792 xassert (row->ascent >= 0 && row->height > 0);
16794 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16795 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16797 /* If first line's physical ascent is larger than its logical
16798 ascent, use the physical ascent, and make the row taller.
16799 This makes accented characters fully visible. */
16800 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16801 && row->phys_ascent > row->ascent)
16803 row->height += row->phys_ascent - row->ascent;
16804 row->ascent = row->phys_ascent;
16807 /* Compute how much of the line is visible. */
16808 row->visible_height = row->height;
16810 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16811 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16813 if (row->y < min_y)
16814 row->visible_height -= min_y - row->y;
16815 if (row->y + row->height > max_y)
16816 row->visible_height -= row->y + row->height - max_y;
16818 else
16820 row->pixel_width = row->used[TEXT_AREA];
16821 if (row->continued_p)
16822 row->pixel_width -= it->continuation_pixel_width;
16823 else if (row->truncated_on_right_p)
16824 row->pixel_width -= it->truncation_pixel_width;
16825 row->ascent = row->phys_ascent = 0;
16826 row->height = row->phys_height = row->visible_height = 1;
16827 row->extra_line_spacing = 0;
16830 /* Compute a hash code for this row. */
16831 row->hash = 0;
16832 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16833 for (i = 0; i < row->used[area]; ++i)
16834 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16835 + row->glyphs[area][i].u.val
16836 + row->glyphs[area][i].face_id
16837 + row->glyphs[area][i].padding_p
16838 + (row->glyphs[area][i].type << 2));
16840 it->max_ascent = it->max_descent = 0;
16841 it->max_phys_ascent = it->max_phys_descent = 0;
16845 /* Append one space to the glyph row of iterator IT if doing a
16846 window-based redisplay. The space has the same face as
16847 IT->face_id. Value is non-zero if a space was added.
16849 This function is called to make sure that there is always one glyph
16850 at the end of a glyph row that the cursor can be set on under
16851 window-systems. (If there weren't such a glyph we would not know
16852 how wide and tall a box cursor should be displayed).
16854 At the same time this space let's a nicely handle clearing to the
16855 end of the line if the row ends in italic text. */
16857 static int
16858 append_space_for_newline (struct it *it, int default_face_p)
16860 if (FRAME_WINDOW_P (it->f))
16862 int n = it->glyph_row->used[TEXT_AREA];
16864 if (it->glyph_row->glyphs[TEXT_AREA] + n
16865 < it->glyph_row->glyphs[1 + TEXT_AREA])
16867 /* Save some values that must not be changed.
16868 Must save IT->c and IT->len because otherwise
16869 ITERATOR_AT_END_P wouldn't work anymore after
16870 append_space_for_newline has been called. */
16871 enum display_element_type saved_what = it->what;
16872 int saved_c = it->c, saved_len = it->len;
16873 int saved_char_to_display = it->char_to_display;
16874 int saved_x = it->current_x;
16875 int saved_face_id = it->face_id;
16876 struct text_pos saved_pos;
16877 Lisp_Object saved_object;
16878 struct face *face;
16880 saved_object = it->object;
16881 saved_pos = it->position;
16883 it->what = IT_CHARACTER;
16884 memset (&it->position, 0, sizeof it->position);
16885 it->object = make_number (0);
16886 it->c = it->char_to_display = ' ';
16887 it->len = 1;
16889 if (default_face_p)
16890 it->face_id = DEFAULT_FACE_ID;
16891 else if (it->face_before_selective_p)
16892 it->face_id = it->saved_face_id;
16893 face = FACE_FROM_ID (it->f, it->face_id);
16894 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16896 PRODUCE_GLYPHS (it);
16898 it->override_ascent = -1;
16899 it->constrain_row_ascent_descent_p = 0;
16900 it->current_x = saved_x;
16901 it->object = saved_object;
16902 it->position = saved_pos;
16903 it->what = saved_what;
16904 it->face_id = saved_face_id;
16905 it->len = saved_len;
16906 it->c = saved_c;
16907 it->char_to_display = saved_char_to_display;
16908 return 1;
16912 return 0;
16916 /* Extend the face of the last glyph in the text area of IT->glyph_row
16917 to the end of the display line. Called from display_line. If the
16918 glyph row is empty, add a space glyph to it so that we know the
16919 face to draw. Set the glyph row flag fill_line_p. If the glyph
16920 row is R2L, prepend a stretch glyph to cover the empty space to the
16921 left of the leftmost glyph. */
16923 static void
16924 extend_face_to_end_of_line (struct it *it)
16926 struct face *face;
16927 struct frame *f = it->f;
16929 /* If line is already filled, do nothing. Non window-system frames
16930 get a grace of one more ``pixel'' because their characters are
16931 1-``pixel'' wide, so they hit the equality too early. This grace
16932 is needed only for R2L rows that are not continued, to produce
16933 one extra blank where we could display the cursor. */
16934 if (it->current_x >= it->last_visible_x
16935 + (!FRAME_WINDOW_P (f)
16936 && it->glyph_row->reversed_p
16937 && !it->glyph_row->continued_p))
16938 return;
16940 /* Face extension extends the background and box of IT->face_id
16941 to the end of the line. If the background equals the background
16942 of the frame, we don't have to do anything. */
16943 if (it->face_before_selective_p)
16944 face = FACE_FROM_ID (f, it->saved_face_id);
16945 else
16946 face = FACE_FROM_ID (f, it->face_id);
16948 if (FRAME_WINDOW_P (f)
16949 && it->glyph_row->displays_text_p
16950 && face->box == FACE_NO_BOX
16951 && face->background == FRAME_BACKGROUND_PIXEL (f)
16952 && !face->stipple
16953 && !it->glyph_row->reversed_p)
16954 return;
16956 /* Set the glyph row flag indicating that the face of the last glyph
16957 in the text area has to be drawn to the end of the text area. */
16958 it->glyph_row->fill_line_p = 1;
16960 /* If current character of IT is not ASCII, make sure we have the
16961 ASCII face. This will be automatically undone the next time
16962 get_next_display_element returns a multibyte character. Note
16963 that the character will always be single byte in unibyte
16964 text. */
16965 if (!ASCII_CHAR_P (it->c))
16967 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16970 if (FRAME_WINDOW_P (f))
16972 /* If the row is empty, add a space with the current face of IT,
16973 so that we know which face to draw. */
16974 if (it->glyph_row->used[TEXT_AREA] == 0)
16976 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16977 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16978 it->glyph_row->used[TEXT_AREA] = 1;
16980 #ifdef HAVE_WINDOW_SYSTEM
16981 if (it->glyph_row->reversed_p)
16983 /* Prepend a stretch glyph to the row, such that the
16984 rightmost glyph will be drawn flushed all the way to the
16985 right margin of the window. The stretch glyph that will
16986 occupy the empty space, if any, to the left of the
16987 glyphs. */
16988 struct font *font = face->font ? face->font : FRAME_FONT (f);
16989 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16990 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16991 struct glyph *g;
16992 int row_width, stretch_ascent, stretch_width;
16993 struct text_pos saved_pos;
16994 int saved_face_id, saved_avoid_cursor;
16996 for (row_width = 0, g = row_start; g < row_end; g++)
16997 row_width += g->pixel_width;
16998 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16999 if (stretch_width > 0)
17001 stretch_ascent =
17002 (((it->ascent + it->descent)
17003 * FONT_BASE (font)) / FONT_HEIGHT (font));
17004 saved_pos = it->position;
17005 memset (&it->position, 0, sizeof it->position);
17006 saved_avoid_cursor = it->avoid_cursor_p;
17007 it->avoid_cursor_p = 1;
17008 saved_face_id = it->face_id;
17009 /* The last row's stretch glyph should get the default
17010 face, to avoid painting the rest of the window with
17011 the region face, if the region ends at ZV. */
17012 if (it->glyph_row->ends_at_zv_p)
17013 it->face_id = DEFAULT_FACE_ID;
17014 else
17015 it->face_id = face->id;
17016 append_stretch_glyph (it, make_number (0), stretch_width,
17017 it->ascent + it->descent, stretch_ascent);
17018 it->position = saved_pos;
17019 it->avoid_cursor_p = saved_avoid_cursor;
17020 it->face_id = saved_face_id;
17023 #endif /* HAVE_WINDOW_SYSTEM */
17025 else
17027 /* Save some values that must not be changed. */
17028 int saved_x = it->current_x;
17029 struct text_pos saved_pos;
17030 Lisp_Object saved_object;
17031 enum display_element_type saved_what = it->what;
17032 int saved_face_id = it->face_id;
17034 saved_object = it->object;
17035 saved_pos = it->position;
17037 it->what = IT_CHARACTER;
17038 memset (&it->position, 0, sizeof it->position);
17039 it->object = make_number (0);
17040 it->c = it->char_to_display = ' ';
17041 it->len = 1;
17042 /* The last row's blank glyphs should get the default face, to
17043 avoid painting the rest of the window with the region face,
17044 if the region ends at ZV. */
17045 if (it->glyph_row->ends_at_zv_p)
17046 it->face_id = DEFAULT_FACE_ID;
17047 else
17048 it->face_id = face->id;
17050 PRODUCE_GLYPHS (it);
17052 while (it->current_x <= it->last_visible_x)
17053 PRODUCE_GLYPHS (it);
17055 /* Don't count these blanks really. It would let us insert a left
17056 truncation glyph below and make us set the cursor on them, maybe. */
17057 it->current_x = saved_x;
17058 it->object = saved_object;
17059 it->position = saved_pos;
17060 it->what = saved_what;
17061 it->face_id = saved_face_id;
17066 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17067 trailing whitespace. */
17069 static int
17070 trailing_whitespace_p (EMACS_INT charpos)
17072 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17073 int c = 0;
17075 while (bytepos < ZV_BYTE
17076 && (c = FETCH_CHAR (bytepos),
17077 c == ' ' || c == '\t'))
17078 ++bytepos;
17080 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17082 if (bytepos != PT_BYTE)
17083 return 1;
17085 return 0;
17089 /* Highlight trailing whitespace, if any, in ROW. */
17091 void
17092 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17094 int used = row->used[TEXT_AREA];
17096 if (used)
17098 struct glyph *start = row->glyphs[TEXT_AREA];
17099 struct glyph *glyph = start + used - 1;
17101 if (row->reversed_p)
17103 /* Right-to-left rows need to be processed in the opposite
17104 direction, so swap the edge pointers. */
17105 glyph = start;
17106 start = row->glyphs[TEXT_AREA] + used - 1;
17109 /* Skip over glyphs inserted to display the cursor at the
17110 end of a line, for extending the face of the last glyph
17111 to the end of the line on terminals, and for truncation
17112 and continuation glyphs. */
17113 if (!row->reversed_p)
17115 while (glyph >= start
17116 && glyph->type == CHAR_GLYPH
17117 && INTEGERP (glyph->object))
17118 --glyph;
17120 else
17122 while (glyph <= start
17123 && glyph->type == CHAR_GLYPH
17124 && INTEGERP (glyph->object))
17125 ++glyph;
17128 /* If last glyph is a space or stretch, and it's trailing
17129 whitespace, set the face of all trailing whitespace glyphs in
17130 IT->glyph_row to `trailing-whitespace'. */
17131 if ((row->reversed_p ? glyph <= start : glyph >= start)
17132 && BUFFERP (glyph->object)
17133 && (glyph->type == STRETCH_GLYPH
17134 || (glyph->type == CHAR_GLYPH
17135 && glyph->u.ch == ' '))
17136 && trailing_whitespace_p (glyph->charpos))
17138 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17139 if (face_id < 0)
17140 return;
17142 if (!row->reversed_p)
17144 while (glyph >= start
17145 && BUFFERP (glyph->object)
17146 && (glyph->type == STRETCH_GLYPH
17147 || (glyph->type == CHAR_GLYPH
17148 && glyph->u.ch == ' ')))
17149 (glyph--)->face_id = face_id;
17151 else
17153 while (glyph <= start
17154 && BUFFERP (glyph->object)
17155 && (glyph->type == STRETCH_GLYPH
17156 || (glyph->type == CHAR_GLYPH
17157 && glyph->u.ch == ' ')))
17158 (glyph++)->face_id = face_id;
17165 /* Value is non-zero if glyph row ROW in window W should be
17166 used to hold the cursor. */
17168 static int
17169 cursor_row_p (struct window *w, struct glyph_row *row)
17171 int cursor_row_p = 1;
17173 if (PT == CHARPOS (row->end.pos))
17175 /* Suppose the row ends on a string.
17176 Unless the row is continued, that means it ends on a newline
17177 in the string. If it's anything other than a display string
17178 (e.g. a before-string from an overlay), we don't want the
17179 cursor there. (This heuristic seems to give the optimal
17180 behavior for the various types of multi-line strings.) */
17181 if (CHARPOS (row->end.string_pos) >= 0)
17183 if (row->continued_p)
17184 cursor_row_p = 1;
17185 else
17187 /* Check for `display' property. */
17188 struct glyph *beg = row->glyphs[TEXT_AREA];
17189 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17190 struct glyph *glyph;
17192 cursor_row_p = 0;
17193 for (glyph = end; glyph >= beg; --glyph)
17194 if (STRINGP (glyph->object))
17196 Lisp_Object prop
17197 = Fget_char_property (make_number (PT),
17198 Qdisplay, Qnil);
17199 cursor_row_p =
17200 (!NILP (prop)
17201 && display_prop_string_p (prop, glyph->object));
17202 break;
17206 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17208 /* If the row ends in middle of a real character,
17209 and the line is continued, we want the cursor here.
17210 That's because CHARPOS (ROW->end.pos) would equal
17211 PT if PT is before the character. */
17212 if (!row->ends_in_ellipsis_p)
17213 cursor_row_p = row->continued_p;
17214 else
17215 /* If the row ends in an ellipsis, then
17216 CHARPOS (ROW->end.pos) will equal point after the
17217 invisible text. We want that position to be displayed
17218 after the ellipsis. */
17219 cursor_row_p = 0;
17221 /* If the row ends at ZV, display the cursor at the end of that
17222 row instead of at the start of the row below. */
17223 else if (row->ends_at_zv_p)
17224 cursor_row_p = 1;
17225 else
17226 cursor_row_p = 0;
17229 return cursor_row_p;
17234 /* Push the display property PROP so that it will be rendered at the
17235 current position in IT. Return 1 if PROP was successfully pushed,
17236 0 otherwise. */
17238 static int
17239 push_display_prop (struct it *it, Lisp_Object prop)
17241 push_it (it);
17243 if (STRINGP (prop))
17245 if (SCHARS (prop) == 0)
17247 pop_it (it);
17248 return 0;
17251 it->string = prop;
17252 it->multibyte_p = STRING_MULTIBYTE (it->string);
17253 it->current.overlay_string_index = -1;
17254 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17255 it->end_charpos = it->string_nchars = SCHARS (it->string);
17256 it->method = GET_FROM_STRING;
17257 it->stop_charpos = 0;
17259 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17261 it->method = GET_FROM_STRETCH;
17262 it->object = prop;
17264 #ifdef HAVE_WINDOW_SYSTEM
17265 else if (IMAGEP (prop))
17267 it->what = IT_IMAGE;
17268 it->image_id = lookup_image (it->f, prop);
17269 it->method = GET_FROM_IMAGE;
17271 #endif /* HAVE_WINDOW_SYSTEM */
17272 else
17274 pop_it (it); /* bogus display property, give up */
17275 return 0;
17278 return 1;
17281 /* Return the character-property PROP at the current position in IT. */
17283 static Lisp_Object
17284 get_it_property (struct it *it, Lisp_Object prop)
17286 Lisp_Object position;
17288 if (STRINGP (it->object))
17289 position = make_number (IT_STRING_CHARPOS (*it));
17290 else if (BUFFERP (it->object))
17291 position = make_number (IT_CHARPOS (*it));
17292 else
17293 return Qnil;
17295 return Fget_char_property (position, prop, it->object);
17298 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17300 static void
17301 handle_line_prefix (struct it *it)
17303 Lisp_Object prefix;
17304 if (it->continuation_lines_width > 0)
17306 prefix = get_it_property (it, Qwrap_prefix);
17307 if (NILP (prefix))
17308 prefix = Vwrap_prefix;
17310 else
17312 prefix = get_it_property (it, Qline_prefix);
17313 if (NILP (prefix))
17314 prefix = Vline_prefix;
17316 if (! NILP (prefix) && push_display_prop (it, prefix))
17318 /* If the prefix is wider than the window, and we try to wrap
17319 it, it would acquire its own wrap prefix, and so on till the
17320 iterator stack overflows. So, don't wrap the prefix. */
17321 it->line_wrap = TRUNCATE;
17322 it->avoid_cursor_p = 1;
17328 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17329 only for R2L lines from display_line, when it decides that too many
17330 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17331 continued. */
17332 static void
17333 unproduce_glyphs (struct it *it, int n)
17335 struct glyph *glyph, *end;
17337 xassert (it->glyph_row);
17338 xassert (it->glyph_row->reversed_p);
17339 xassert (it->area == TEXT_AREA);
17340 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17342 if (n > it->glyph_row->used[TEXT_AREA])
17343 n = it->glyph_row->used[TEXT_AREA];
17344 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17345 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17346 for ( ; glyph < end; glyph++)
17347 glyph[-n] = *glyph;
17350 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17351 and ROW->maxpos. */
17352 static void
17353 find_row_edges (struct it *it, struct glyph_row *row,
17354 EMACS_INT min_pos, EMACS_INT min_bpos,
17355 EMACS_INT max_pos, EMACS_INT max_bpos)
17357 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17358 lines' rows is implemented for bidi-reordered rows. */
17360 /* ROW->minpos is the value of min_pos, the minimal buffer position
17361 we have in ROW. */
17362 if (min_pos <= ZV)
17363 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17364 else
17366 /* We didn't find _any_ valid buffer positions in any of the
17367 glyphs, so we must trust the iterator's computed
17368 positions. */
17369 row->minpos = row->start.pos;
17370 max_pos = CHARPOS (it->current.pos);
17371 max_bpos = BYTEPOS (it->current.pos);
17374 if (!max_pos)
17375 abort ();
17377 /* Here are the various use-cases for ending the row, and the
17378 corresponding values for ROW->maxpos:
17380 Line ends in a newline from buffer eol_pos + 1
17381 Line is continued from buffer max_pos + 1
17382 Line is truncated on right it->current.pos
17383 Line ends in a newline from string max_pos
17384 Line is continued from string max_pos
17385 Line is continued from display vector max_pos
17386 Line is entirely from a string min_pos == max_pos
17387 Line is entirely from a display vector min_pos == max_pos
17388 Line that ends at ZV ZV
17390 If you discover other use-cases, please add them here as
17391 appropriate. */
17392 if (row->ends_at_zv_p)
17393 row->maxpos = it->current.pos;
17394 else if (row->used[TEXT_AREA])
17396 if (row->ends_in_newline_from_string_p)
17397 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17398 else if (CHARPOS (it->eol_pos) > 0)
17399 SET_TEXT_POS (row->maxpos,
17400 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17401 else if (row->continued_p)
17403 /* If max_pos is different from IT's current position, it
17404 means IT->method does not belong to the display element
17405 at max_pos. However, it also means that the display
17406 element at max_pos was displayed in its entirety on this
17407 line, which is equivalent to saying that the next line
17408 starts at the next buffer position. */
17409 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17410 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17411 else
17413 INC_BOTH (max_pos, max_bpos);
17414 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17417 else if (row->truncated_on_right_p)
17418 /* display_line already called reseat_at_next_visible_line_start,
17419 which puts the iterator at the beginning of the next line, in
17420 the logical order. */
17421 row->maxpos = it->current.pos;
17422 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17423 /* A line that is entirely from a string/image/stretch... */
17424 row->maxpos = row->minpos;
17425 else
17426 abort ();
17428 else
17429 row->maxpos = it->current.pos;
17432 /* Construct the glyph row IT->glyph_row in the desired matrix of
17433 IT->w from text at the current position of IT. See dispextern.h
17434 for an overview of struct it. Value is non-zero if
17435 IT->glyph_row displays text, as opposed to a line displaying ZV
17436 only. */
17438 static int
17439 display_line (struct it *it)
17441 struct glyph_row *row = it->glyph_row;
17442 Lisp_Object overlay_arrow_string;
17443 struct it wrap_it;
17444 int may_wrap = 0, wrap_x;
17445 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17446 int wrap_row_phys_ascent, wrap_row_phys_height;
17447 int wrap_row_extra_line_spacing;
17448 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17449 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17450 int cvpos;
17451 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17453 /* We always start displaying at hpos zero even if hscrolled. */
17454 xassert (it->hpos == 0 && it->current_x == 0);
17456 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17457 >= it->w->desired_matrix->nrows)
17459 it->w->nrows_scale_factor++;
17460 fonts_changed_p = 1;
17461 return 0;
17464 /* Is IT->w showing the region? */
17465 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17467 /* Clear the result glyph row and enable it. */
17468 prepare_desired_row (row);
17470 row->y = it->current_y;
17471 row->start = it->start;
17472 row->continuation_lines_width = it->continuation_lines_width;
17473 row->displays_text_p = 1;
17474 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17475 it->starts_in_middle_of_char_p = 0;
17477 /* Arrange the overlays nicely for our purposes. Usually, we call
17478 display_line on only one line at a time, in which case this
17479 can't really hurt too much, or we call it on lines which appear
17480 one after another in the buffer, in which case all calls to
17481 recenter_overlay_lists but the first will be pretty cheap. */
17482 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17484 /* Move over display elements that are not visible because we are
17485 hscrolled. This may stop at an x-position < IT->first_visible_x
17486 if the first glyph is partially visible or if we hit a line end. */
17487 if (it->current_x < it->first_visible_x)
17489 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17490 MOVE_TO_POS | MOVE_TO_X);
17492 else
17494 /* We only do this when not calling `move_it_in_display_line_to'
17495 above, because move_it_in_display_line_to calls
17496 handle_line_prefix itself. */
17497 handle_line_prefix (it);
17500 /* Get the initial row height. This is either the height of the
17501 text hscrolled, if there is any, or zero. */
17502 row->ascent = it->max_ascent;
17503 row->height = it->max_ascent + it->max_descent;
17504 row->phys_ascent = it->max_phys_ascent;
17505 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17506 row->extra_line_spacing = it->max_extra_line_spacing;
17508 /* Utility macro to record max and min buffer positions seen until now. */
17509 #define RECORD_MAX_MIN_POS(IT) \
17510 do \
17512 if (IT_CHARPOS (*(IT)) < min_pos) \
17514 min_pos = IT_CHARPOS (*(IT)); \
17515 min_bpos = IT_BYTEPOS (*(IT)); \
17517 if (IT_CHARPOS (*(IT)) > max_pos) \
17519 max_pos = IT_CHARPOS (*(IT)); \
17520 max_bpos = IT_BYTEPOS (*(IT)); \
17523 while (0)
17525 /* Loop generating characters. The loop is left with IT on the next
17526 character to display. */
17527 while (1)
17529 int n_glyphs_before, hpos_before, x_before;
17530 int x, i, nglyphs;
17531 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17533 /* Retrieve the next thing to display. Value is zero if end of
17534 buffer reached. */
17535 if (!get_next_display_element (it))
17537 /* Maybe add a space at the end of this line that is used to
17538 display the cursor there under X. Set the charpos of the
17539 first glyph of blank lines not corresponding to any text
17540 to -1. */
17541 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17542 row->exact_window_width_line_p = 1;
17543 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17544 || row->used[TEXT_AREA] == 0)
17546 row->glyphs[TEXT_AREA]->charpos = -1;
17547 row->displays_text_p = 0;
17549 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17550 && (!MINI_WINDOW_P (it->w)
17551 || (minibuf_level && EQ (it->window, minibuf_window))))
17552 row->indicate_empty_line_p = 1;
17555 it->continuation_lines_width = 0;
17556 row->ends_at_zv_p = 1;
17557 /* A row that displays right-to-left text must always have
17558 its last face extended all the way to the end of line,
17559 even if this row ends in ZV, because we still write to
17560 the screen left to right. */
17561 if (row->reversed_p)
17562 extend_face_to_end_of_line (it);
17563 break;
17566 /* Now, get the metrics of what we want to display. This also
17567 generates glyphs in `row' (which is IT->glyph_row). */
17568 n_glyphs_before = row->used[TEXT_AREA];
17569 x = it->current_x;
17571 /* Remember the line height so far in case the next element doesn't
17572 fit on the line. */
17573 if (it->line_wrap != TRUNCATE)
17575 ascent = it->max_ascent;
17576 descent = it->max_descent;
17577 phys_ascent = it->max_phys_ascent;
17578 phys_descent = it->max_phys_descent;
17580 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17582 if (IT_DISPLAYING_WHITESPACE (it))
17583 may_wrap = 1;
17584 else if (may_wrap)
17586 wrap_it = *it;
17587 wrap_x = x;
17588 wrap_row_used = row->used[TEXT_AREA];
17589 wrap_row_ascent = row->ascent;
17590 wrap_row_height = row->height;
17591 wrap_row_phys_ascent = row->phys_ascent;
17592 wrap_row_phys_height = row->phys_height;
17593 wrap_row_extra_line_spacing = row->extra_line_spacing;
17594 wrap_row_min_pos = min_pos;
17595 wrap_row_min_bpos = min_bpos;
17596 wrap_row_max_pos = max_pos;
17597 wrap_row_max_bpos = max_bpos;
17598 may_wrap = 0;
17603 PRODUCE_GLYPHS (it);
17605 /* If this display element was in marginal areas, continue with
17606 the next one. */
17607 if (it->area != TEXT_AREA)
17609 row->ascent = max (row->ascent, it->max_ascent);
17610 row->height = max (row->height, it->max_ascent + it->max_descent);
17611 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17612 row->phys_height = max (row->phys_height,
17613 it->max_phys_ascent + it->max_phys_descent);
17614 row->extra_line_spacing = max (row->extra_line_spacing,
17615 it->max_extra_line_spacing);
17616 set_iterator_to_next (it, 1);
17617 continue;
17620 /* Does the display element fit on the line? If we truncate
17621 lines, we should draw past the right edge of the window. If
17622 we don't truncate, we want to stop so that we can display the
17623 continuation glyph before the right margin. If lines are
17624 continued, there are two possible strategies for characters
17625 resulting in more than 1 glyph (e.g. tabs): Display as many
17626 glyphs as possible in this line and leave the rest for the
17627 continuation line, or display the whole element in the next
17628 line. Original redisplay did the former, so we do it also. */
17629 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17630 hpos_before = it->hpos;
17631 x_before = x;
17633 if (/* Not a newline. */
17634 nglyphs > 0
17635 /* Glyphs produced fit entirely in the line. */
17636 && it->current_x < it->last_visible_x)
17638 it->hpos += nglyphs;
17639 row->ascent = max (row->ascent, it->max_ascent);
17640 row->height = max (row->height, it->max_ascent + it->max_descent);
17641 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17642 row->phys_height = max (row->phys_height,
17643 it->max_phys_ascent + it->max_phys_descent);
17644 row->extra_line_spacing = max (row->extra_line_spacing,
17645 it->max_extra_line_spacing);
17646 if (it->current_x - it->pixel_width < it->first_visible_x)
17647 row->x = x - it->first_visible_x;
17648 /* Record the maximum and minimum buffer positions seen so
17649 far in glyphs that will be displayed by this row. */
17650 if (it->bidi_p)
17651 RECORD_MAX_MIN_POS (it);
17653 else
17655 int new_x;
17656 struct glyph *glyph;
17658 for (i = 0; i < nglyphs; ++i, x = new_x)
17660 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17661 new_x = x + glyph->pixel_width;
17663 if (/* Lines are continued. */
17664 it->line_wrap != TRUNCATE
17665 && (/* Glyph doesn't fit on the line. */
17666 new_x > it->last_visible_x
17667 /* Or it fits exactly on a window system frame. */
17668 || (new_x == it->last_visible_x
17669 && FRAME_WINDOW_P (it->f))))
17671 /* End of a continued line. */
17673 if (it->hpos == 0
17674 || (new_x == it->last_visible_x
17675 && FRAME_WINDOW_P (it->f)))
17677 /* Current glyph is the only one on the line or
17678 fits exactly on the line. We must continue
17679 the line because we can't draw the cursor
17680 after the glyph. */
17681 row->continued_p = 1;
17682 it->current_x = new_x;
17683 it->continuation_lines_width += new_x;
17684 ++it->hpos;
17685 /* Record the maximum and minimum buffer
17686 positions seen so far in glyphs that will be
17687 displayed by this row. */
17688 if (it->bidi_p)
17689 RECORD_MAX_MIN_POS (it);
17690 if (i == nglyphs - 1)
17692 /* If line-wrap is on, check if a previous
17693 wrap point was found. */
17694 if (wrap_row_used > 0
17695 /* Even if there is a previous wrap
17696 point, continue the line here as
17697 usual, if (i) the previous character
17698 was a space or tab AND (ii) the
17699 current character is not. */
17700 && (!may_wrap
17701 || IT_DISPLAYING_WHITESPACE (it)))
17702 goto back_to_wrap;
17704 set_iterator_to_next (it, 1);
17705 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17707 if (!get_next_display_element (it))
17709 row->exact_window_width_line_p = 1;
17710 it->continuation_lines_width = 0;
17711 row->continued_p = 0;
17712 row->ends_at_zv_p = 1;
17714 else if (ITERATOR_AT_END_OF_LINE_P (it))
17716 row->continued_p = 0;
17717 row->exact_window_width_line_p = 1;
17722 else if (CHAR_GLYPH_PADDING_P (*glyph)
17723 && !FRAME_WINDOW_P (it->f))
17725 /* A padding glyph that doesn't fit on this line.
17726 This means the whole character doesn't fit
17727 on the line. */
17728 if (row->reversed_p)
17729 unproduce_glyphs (it, row->used[TEXT_AREA]
17730 - n_glyphs_before);
17731 row->used[TEXT_AREA] = n_glyphs_before;
17733 /* Fill the rest of the row with continuation
17734 glyphs like in 20.x. */
17735 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17736 < row->glyphs[1 + TEXT_AREA])
17737 produce_special_glyphs (it, IT_CONTINUATION);
17739 row->continued_p = 1;
17740 it->current_x = x_before;
17741 it->continuation_lines_width += x_before;
17743 /* Restore the height to what it was before the
17744 element not fitting on the line. */
17745 it->max_ascent = ascent;
17746 it->max_descent = descent;
17747 it->max_phys_ascent = phys_ascent;
17748 it->max_phys_descent = phys_descent;
17750 else if (wrap_row_used > 0)
17752 back_to_wrap:
17753 if (row->reversed_p)
17754 unproduce_glyphs (it,
17755 row->used[TEXT_AREA] - wrap_row_used);
17756 *it = wrap_it;
17757 it->continuation_lines_width += wrap_x;
17758 row->used[TEXT_AREA] = wrap_row_used;
17759 row->ascent = wrap_row_ascent;
17760 row->height = wrap_row_height;
17761 row->phys_ascent = wrap_row_phys_ascent;
17762 row->phys_height = wrap_row_phys_height;
17763 row->extra_line_spacing = wrap_row_extra_line_spacing;
17764 min_pos = wrap_row_min_pos;
17765 min_bpos = wrap_row_min_bpos;
17766 max_pos = wrap_row_max_pos;
17767 max_bpos = wrap_row_max_bpos;
17768 row->continued_p = 1;
17769 row->ends_at_zv_p = 0;
17770 row->exact_window_width_line_p = 0;
17771 it->continuation_lines_width += x;
17773 /* Make sure that a non-default face is extended
17774 up to the right margin of the window. */
17775 extend_face_to_end_of_line (it);
17777 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17779 /* A TAB that extends past the right edge of the
17780 window. This produces a single glyph on
17781 window system frames. We leave the glyph in
17782 this row and let it fill the row, but don't
17783 consume the TAB. */
17784 it->continuation_lines_width += it->last_visible_x;
17785 row->ends_in_middle_of_char_p = 1;
17786 row->continued_p = 1;
17787 glyph->pixel_width = it->last_visible_x - x;
17788 it->starts_in_middle_of_char_p = 1;
17790 else
17792 /* Something other than a TAB that draws past
17793 the right edge of the window. Restore
17794 positions to values before the element. */
17795 if (row->reversed_p)
17796 unproduce_glyphs (it, row->used[TEXT_AREA]
17797 - (n_glyphs_before + i));
17798 row->used[TEXT_AREA] = n_glyphs_before + i;
17800 /* Display continuation glyphs. */
17801 if (!FRAME_WINDOW_P (it->f))
17802 produce_special_glyphs (it, IT_CONTINUATION);
17803 row->continued_p = 1;
17805 it->current_x = x_before;
17806 it->continuation_lines_width += x;
17807 extend_face_to_end_of_line (it);
17809 if (nglyphs > 1 && i > 0)
17811 row->ends_in_middle_of_char_p = 1;
17812 it->starts_in_middle_of_char_p = 1;
17815 /* Restore the height to what it was before the
17816 element not fitting on the line. */
17817 it->max_ascent = ascent;
17818 it->max_descent = descent;
17819 it->max_phys_ascent = phys_ascent;
17820 it->max_phys_descent = phys_descent;
17823 break;
17825 else if (new_x > it->first_visible_x)
17827 /* Increment number of glyphs actually displayed. */
17828 ++it->hpos;
17830 /* Record the maximum and minimum buffer positions
17831 seen so far in glyphs that will be displayed by
17832 this row. */
17833 if (it->bidi_p)
17834 RECORD_MAX_MIN_POS (it);
17836 if (x < it->first_visible_x)
17837 /* Glyph is partially visible, i.e. row starts at
17838 negative X position. */
17839 row->x = x - it->first_visible_x;
17841 else
17843 /* Glyph is completely off the left margin of the
17844 window. This should not happen because of the
17845 move_it_in_display_line at the start of this
17846 function, unless the text display area of the
17847 window is empty. */
17848 xassert (it->first_visible_x <= it->last_visible_x);
17852 row->ascent = max (row->ascent, it->max_ascent);
17853 row->height = max (row->height, it->max_ascent + it->max_descent);
17854 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17855 row->phys_height = max (row->phys_height,
17856 it->max_phys_ascent + it->max_phys_descent);
17857 row->extra_line_spacing = max (row->extra_line_spacing,
17858 it->max_extra_line_spacing);
17860 /* End of this display line if row is continued. */
17861 if (row->continued_p || row->ends_at_zv_p)
17862 break;
17865 at_end_of_line:
17866 /* Is this a line end? If yes, we're also done, after making
17867 sure that a non-default face is extended up to the right
17868 margin of the window. */
17869 if (ITERATOR_AT_END_OF_LINE_P (it))
17871 int used_before = row->used[TEXT_AREA];
17873 row->ends_in_newline_from_string_p = STRINGP (it->object);
17875 /* Add a space at the end of the line that is used to
17876 display the cursor there. */
17877 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17878 append_space_for_newline (it, 0);
17880 /* Extend the face to the end of the line. */
17881 extend_face_to_end_of_line (it);
17883 /* Make sure we have the position. */
17884 if (used_before == 0)
17885 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17887 /* Record the position of the newline, for use in
17888 find_row_edges. */
17889 it->eol_pos = it->current.pos;
17891 /* Consume the line end. This skips over invisible lines. */
17892 set_iterator_to_next (it, 1);
17893 it->continuation_lines_width = 0;
17894 break;
17897 /* Proceed with next display element. Note that this skips
17898 over lines invisible because of selective display. */
17899 set_iterator_to_next (it, 1);
17901 /* If we truncate lines, we are done when the last displayed
17902 glyphs reach past the right margin of the window. */
17903 if (it->line_wrap == TRUNCATE
17904 && (FRAME_WINDOW_P (it->f)
17905 ? (it->current_x >= it->last_visible_x)
17906 : (it->current_x > it->last_visible_x)))
17908 /* Maybe add truncation glyphs. */
17909 if (!FRAME_WINDOW_P (it->f))
17911 int i, n;
17913 if (!row->reversed_p)
17915 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17916 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17917 break;
17919 else
17921 for (i = 0; i < row->used[TEXT_AREA]; i++)
17922 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17923 break;
17924 /* Remove any padding glyphs at the front of ROW, to
17925 make room for the truncation glyphs we will be
17926 adding below. The loop below always inserts at
17927 least one truncation glyph, so also remove the
17928 last glyph added to ROW. */
17929 unproduce_glyphs (it, i + 1);
17930 /* Adjust i for the loop below. */
17931 i = row->used[TEXT_AREA] - (i + 1);
17934 for (n = row->used[TEXT_AREA]; i < n; ++i)
17936 row->used[TEXT_AREA] = i;
17937 produce_special_glyphs (it, IT_TRUNCATION);
17940 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17942 /* Don't truncate if we can overflow newline into fringe. */
17943 if (!get_next_display_element (it))
17945 it->continuation_lines_width = 0;
17946 row->ends_at_zv_p = 1;
17947 row->exact_window_width_line_p = 1;
17948 break;
17950 if (ITERATOR_AT_END_OF_LINE_P (it))
17952 row->exact_window_width_line_p = 1;
17953 goto at_end_of_line;
17957 row->truncated_on_right_p = 1;
17958 it->continuation_lines_width = 0;
17959 reseat_at_next_visible_line_start (it, 0);
17960 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17961 it->hpos = hpos_before;
17962 it->current_x = x_before;
17963 break;
17967 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17968 at the left window margin. */
17969 if (it->first_visible_x
17970 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17972 if (!FRAME_WINDOW_P (it->f))
17973 insert_left_trunc_glyphs (it);
17974 row->truncated_on_left_p = 1;
17977 /* Remember the position at which this line ends.
17979 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17980 cannot be before the call to find_row_edges below, since that is
17981 where these positions are determined. */
17982 row->end = it->current;
17983 if (!it->bidi_p)
17985 row->minpos = row->start.pos;
17986 row->maxpos = row->end.pos;
17988 else
17990 /* ROW->minpos and ROW->maxpos must be the smallest and
17991 `1 + the largest' buffer positions in ROW. But if ROW was
17992 bidi-reordered, these two positions can be anywhere in the
17993 row, so we must determine them now. */
17994 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17997 /* If the start of this line is the overlay arrow-position, then
17998 mark this glyph row as the one containing the overlay arrow.
17999 This is clearly a mess with variable size fonts. It would be
18000 better to let it be displayed like cursors under X. */
18001 if ((row->displays_text_p || !overlay_arrow_seen)
18002 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18003 !NILP (overlay_arrow_string)))
18005 /* Overlay arrow in window redisplay is a fringe bitmap. */
18006 if (STRINGP (overlay_arrow_string))
18008 struct glyph_row *arrow_row
18009 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18010 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18011 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18012 struct glyph *p = row->glyphs[TEXT_AREA];
18013 struct glyph *p2, *end;
18015 /* Copy the arrow glyphs. */
18016 while (glyph < arrow_end)
18017 *p++ = *glyph++;
18019 /* Throw away padding glyphs. */
18020 p2 = p;
18021 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18022 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18023 ++p2;
18024 if (p2 > p)
18026 while (p2 < end)
18027 *p++ = *p2++;
18028 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18031 else
18033 xassert (INTEGERP (overlay_arrow_string));
18034 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18036 overlay_arrow_seen = 1;
18039 /* Compute pixel dimensions of this line. */
18040 compute_line_metrics (it);
18042 /* Record whether this row ends inside an ellipsis. */
18043 row->ends_in_ellipsis_p
18044 = (it->method == GET_FROM_DISPLAY_VECTOR
18045 && it->ellipsis_p);
18047 /* Save fringe bitmaps in this row. */
18048 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18049 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18050 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18051 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18053 it->left_user_fringe_bitmap = 0;
18054 it->left_user_fringe_face_id = 0;
18055 it->right_user_fringe_bitmap = 0;
18056 it->right_user_fringe_face_id = 0;
18058 /* Maybe set the cursor. */
18059 cvpos = it->w->cursor.vpos;
18060 if ((cvpos < 0
18061 /* In bidi-reordered rows, keep checking for proper cursor
18062 position even if one has been found already, because buffer
18063 positions in such rows change non-linearly with ROW->VPOS,
18064 when a line is continued. One exception: when we are at ZV,
18065 display cursor on the first suitable glyph row, since all
18066 the empty rows after that also have their position set to ZV. */
18067 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18068 lines' rows is implemented for bidi-reordered rows. */
18069 || (it->bidi_p
18070 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18071 && PT >= MATRIX_ROW_START_CHARPOS (row)
18072 && PT <= MATRIX_ROW_END_CHARPOS (row)
18073 && cursor_row_p (it->w, row))
18074 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18076 /* Highlight trailing whitespace. */
18077 if (!NILP (Vshow_trailing_whitespace))
18078 highlight_trailing_whitespace (it->f, it->glyph_row);
18080 /* Prepare for the next line. This line starts horizontally at (X
18081 HPOS) = (0 0). Vertical positions are incremented. As a
18082 convenience for the caller, IT->glyph_row is set to the next
18083 row to be used. */
18084 it->current_x = it->hpos = 0;
18085 it->current_y += row->height;
18086 SET_TEXT_POS (it->eol_pos, 0, 0);
18087 ++it->vpos;
18088 ++it->glyph_row;
18089 /* The next row should by default use the same value of the
18090 reversed_p flag as this one. set_iterator_to_next decides when
18091 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18092 the flag accordingly. */
18093 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18094 it->glyph_row->reversed_p = row->reversed_p;
18095 it->start = row->end;
18096 return row->displays_text_p;
18098 #undef RECORD_MAX_MIN_POS
18101 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18102 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18103 doc: /* Return paragraph direction at point in BUFFER.
18104 Value is either `left-to-right' or `right-to-left'.
18105 If BUFFER is omitted or nil, it defaults to the current buffer.
18107 Paragraph direction determines how the text in the paragraph is displayed.
18108 In left-to-right paragraphs, text begins at the left margin of the window
18109 and the reading direction is generally left to right. In right-to-left
18110 paragraphs, text begins at the right margin and is read from right to left.
18112 See also `bidi-paragraph-direction'. */)
18113 (Lisp_Object buffer)
18115 struct buffer *buf;
18116 struct buffer *old;
18118 if (NILP (buffer))
18119 buf = current_buffer;
18120 else
18122 CHECK_BUFFER (buffer);
18123 buf = XBUFFER (buffer);
18124 old = current_buffer;
18127 if (NILP (buf->bidi_display_reordering))
18128 return Qleft_to_right;
18129 else if (!NILP (buf->bidi_paragraph_direction))
18130 return buf->bidi_paragraph_direction;
18131 else
18133 /* Determine the direction from buffer text. We could try to
18134 use current_matrix if it is up to date, but this seems fast
18135 enough as it is. */
18136 struct bidi_it itb;
18137 EMACS_INT pos = BUF_PT (buf);
18138 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18139 int c;
18141 if (buf != current_buffer)
18142 set_buffer_temp (buf);
18143 /* bidi_paragraph_init finds the base direction of the paragraph
18144 by searching forward from paragraph start. We need the base
18145 direction of the current or _previous_ paragraph, so we need
18146 to make sure we are within that paragraph. To that end, find
18147 the previous non-empty line. */
18148 if (pos >= ZV && pos > BEGV)
18150 pos--;
18151 bytepos = CHAR_TO_BYTE (pos);
18153 while ((c = FETCH_BYTE (bytepos)) == '\n'
18154 || c == ' ' || c == '\t' || c == '\f')
18156 if (bytepos <= BEGV_BYTE)
18157 break;
18158 bytepos--;
18159 pos--;
18161 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18162 bytepos--;
18163 itb.charpos = pos;
18164 itb.bytepos = bytepos;
18165 itb.first_elt = 1;
18166 itb.separator_limit = -1;
18167 itb.paragraph_dir = NEUTRAL_DIR;
18169 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18170 if (buf != current_buffer)
18171 set_buffer_temp (old);
18172 switch (itb.paragraph_dir)
18174 case L2R:
18175 return Qleft_to_right;
18176 break;
18177 case R2L:
18178 return Qright_to_left;
18179 break;
18180 default:
18181 abort ();
18188 /***********************************************************************
18189 Menu Bar
18190 ***********************************************************************/
18192 /* Redisplay the menu bar in the frame for window W.
18194 The menu bar of X frames that don't have X toolkit support is
18195 displayed in a special window W->frame->menu_bar_window.
18197 The menu bar of terminal frames is treated specially as far as
18198 glyph matrices are concerned. Menu bar lines are not part of
18199 windows, so the update is done directly on the frame matrix rows
18200 for the menu bar. */
18202 static void
18203 display_menu_bar (struct window *w)
18205 struct frame *f = XFRAME (WINDOW_FRAME (w));
18206 struct it it;
18207 Lisp_Object items;
18208 int i;
18210 /* Don't do all this for graphical frames. */
18211 #ifdef HAVE_NTGUI
18212 if (FRAME_W32_P (f))
18213 return;
18214 #endif
18215 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18216 if (FRAME_X_P (f))
18217 return;
18218 #endif
18220 #ifdef HAVE_NS
18221 if (FRAME_NS_P (f))
18222 return;
18223 #endif /* HAVE_NS */
18225 #ifdef USE_X_TOOLKIT
18226 xassert (!FRAME_WINDOW_P (f));
18227 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18228 it.first_visible_x = 0;
18229 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18230 #else /* not USE_X_TOOLKIT */
18231 if (FRAME_WINDOW_P (f))
18233 /* Menu bar lines are displayed in the desired matrix of the
18234 dummy window menu_bar_window. */
18235 struct window *menu_w;
18236 xassert (WINDOWP (f->menu_bar_window));
18237 menu_w = XWINDOW (f->menu_bar_window);
18238 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18239 MENU_FACE_ID);
18240 it.first_visible_x = 0;
18241 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18243 else
18245 /* This is a TTY frame, i.e. character hpos/vpos are used as
18246 pixel x/y. */
18247 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18248 MENU_FACE_ID);
18249 it.first_visible_x = 0;
18250 it.last_visible_x = FRAME_COLS (f);
18252 #endif /* not USE_X_TOOLKIT */
18254 if (! mode_line_inverse_video)
18255 /* Force the menu-bar to be displayed in the default face. */
18256 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18258 /* Clear all rows of the menu bar. */
18259 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18261 struct glyph_row *row = it.glyph_row + i;
18262 clear_glyph_row (row);
18263 row->enabled_p = 1;
18264 row->full_width_p = 1;
18267 /* Display all items of the menu bar. */
18268 items = FRAME_MENU_BAR_ITEMS (it.f);
18269 for (i = 0; i < XVECTOR (items)->size; i += 4)
18271 Lisp_Object string;
18273 /* Stop at nil string. */
18274 string = AREF (items, i + 1);
18275 if (NILP (string))
18276 break;
18278 /* Remember where item was displayed. */
18279 ASET (items, i + 3, make_number (it.hpos));
18281 /* Display the item, pad with one space. */
18282 if (it.current_x < it.last_visible_x)
18283 display_string (NULL, string, Qnil, 0, 0, &it,
18284 SCHARS (string) + 1, 0, 0, -1);
18287 /* Fill out the line with spaces. */
18288 if (it.current_x < it.last_visible_x)
18289 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18291 /* Compute the total height of the lines. */
18292 compute_line_metrics (&it);
18297 /***********************************************************************
18298 Mode Line
18299 ***********************************************************************/
18301 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18302 FORCE is non-zero, redisplay mode lines unconditionally.
18303 Otherwise, redisplay only mode lines that are garbaged. Value is
18304 the number of windows whose mode lines were redisplayed. */
18306 static int
18307 redisplay_mode_lines (Lisp_Object window, int force)
18309 int nwindows = 0;
18311 while (!NILP (window))
18313 struct window *w = XWINDOW (window);
18315 if (WINDOWP (w->hchild))
18316 nwindows += redisplay_mode_lines (w->hchild, force);
18317 else if (WINDOWP (w->vchild))
18318 nwindows += redisplay_mode_lines (w->vchild, force);
18319 else if (force
18320 || FRAME_GARBAGED_P (XFRAME (w->frame))
18321 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18323 struct text_pos lpoint;
18324 struct buffer *old = current_buffer;
18326 /* Set the window's buffer for the mode line display. */
18327 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18328 set_buffer_internal_1 (XBUFFER (w->buffer));
18330 /* Point refers normally to the selected window. For any
18331 other window, set up appropriate value. */
18332 if (!EQ (window, selected_window))
18334 struct text_pos pt;
18336 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18337 if (CHARPOS (pt) < BEGV)
18338 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18339 else if (CHARPOS (pt) > (ZV - 1))
18340 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18341 else
18342 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18345 /* Display mode lines. */
18346 clear_glyph_matrix (w->desired_matrix);
18347 if (display_mode_lines (w))
18349 ++nwindows;
18350 w->must_be_updated_p = 1;
18353 /* Restore old settings. */
18354 set_buffer_internal_1 (old);
18355 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18358 window = w->next;
18361 return nwindows;
18365 /* Display the mode and/or header line of window W. Value is the
18366 sum number of mode lines and header lines displayed. */
18368 static int
18369 display_mode_lines (struct window *w)
18371 Lisp_Object old_selected_window, old_selected_frame;
18372 int n = 0;
18374 old_selected_frame = selected_frame;
18375 selected_frame = w->frame;
18376 old_selected_window = selected_window;
18377 XSETWINDOW (selected_window, w);
18379 /* These will be set while the mode line specs are processed. */
18380 line_number_displayed = 0;
18381 w->column_number_displayed = Qnil;
18383 if (WINDOW_WANTS_MODELINE_P (w))
18385 struct window *sel_w = XWINDOW (old_selected_window);
18387 /* Select mode line face based on the real selected window. */
18388 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18389 current_buffer->mode_line_format);
18390 ++n;
18393 if (WINDOW_WANTS_HEADER_LINE_P (w))
18395 display_mode_line (w, HEADER_LINE_FACE_ID,
18396 current_buffer->header_line_format);
18397 ++n;
18400 selected_frame = old_selected_frame;
18401 selected_window = old_selected_window;
18402 return n;
18406 /* Display mode or header line of window W. FACE_ID specifies which
18407 line to display; it is either MODE_LINE_FACE_ID or
18408 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18409 display. Value is the pixel height of the mode/header line
18410 displayed. */
18412 static int
18413 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18415 struct it it;
18416 struct face *face;
18417 int count = SPECPDL_INDEX ();
18419 init_iterator (&it, w, -1, -1, NULL, face_id);
18420 /* Don't extend on a previously drawn mode-line.
18421 This may happen if called from pos_visible_p. */
18422 it.glyph_row->enabled_p = 0;
18423 prepare_desired_row (it.glyph_row);
18425 it.glyph_row->mode_line_p = 1;
18427 if (! mode_line_inverse_video)
18428 /* Force the mode-line to be displayed in the default face. */
18429 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18431 record_unwind_protect (unwind_format_mode_line,
18432 format_mode_line_unwind_data (NULL, Qnil, 0));
18434 mode_line_target = MODE_LINE_DISPLAY;
18436 /* Temporarily make frame's keyboard the current kboard so that
18437 kboard-local variables in the mode_line_format will get the right
18438 values. */
18439 push_kboard (FRAME_KBOARD (it.f));
18440 record_unwind_save_match_data ();
18441 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18442 pop_kboard ();
18444 unbind_to (count, Qnil);
18446 /* Fill up with spaces. */
18447 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18449 compute_line_metrics (&it);
18450 it.glyph_row->full_width_p = 1;
18451 it.glyph_row->continued_p = 0;
18452 it.glyph_row->truncated_on_left_p = 0;
18453 it.glyph_row->truncated_on_right_p = 0;
18455 /* Make a 3D mode-line have a shadow at its right end. */
18456 face = FACE_FROM_ID (it.f, face_id);
18457 extend_face_to_end_of_line (&it);
18458 if (face->box != FACE_NO_BOX)
18460 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18461 + it.glyph_row->used[TEXT_AREA] - 1);
18462 last->right_box_line_p = 1;
18465 return it.glyph_row->height;
18468 /* Move element ELT in LIST to the front of LIST.
18469 Return the updated list. */
18471 static Lisp_Object
18472 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18474 register Lisp_Object tail, prev;
18475 register Lisp_Object tem;
18477 tail = list;
18478 prev = Qnil;
18479 while (CONSP (tail))
18481 tem = XCAR (tail);
18483 if (EQ (elt, tem))
18485 /* Splice out the link TAIL. */
18486 if (NILP (prev))
18487 list = XCDR (tail);
18488 else
18489 Fsetcdr (prev, XCDR (tail));
18491 /* Now make it the first. */
18492 Fsetcdr (tail, list);
18493 return tail;
18495 else
18496 prev = tail;
18497 tail = XCDR (tail);
18498 QUIT;
18501 /* Not found--return unchanged LIST. */
18502 return list;
18505 /* Contribute ELT to the mode line for window IT->w. How it
18506 translates into text depends on its data type.
18508 IT describes the display environment in which we display, as usual.
18510 DEPTH is the depth in recursion. It is used to prevent
18511 infinite recursion here.
18513 FIELD_WIDTH is the number of characters the display of ELT should
18514 occupy in the mode line, and PRECISION is the maximum number of
18515 characters to display from ELT's representation. See
18516 display_string for details.
18518 Returns the hpos of the end of the text generated by ELT.
18520 PROPS is a property list to add to any string we encounter.
18522 If RISKY is nonzero, remove (disregard) any properties in any string
18523 we encounter, and ignore :eval and :propertize.
18525 The global variable `mode_line_target' determines whether the
18526 output is passed to `store_mode_line_noprop',
18527 `store_mode_line_string', or `display_string'. */
18529 static int
18530 display_mode_element (struct it *it, int depth, int field_width, int precision,
18531 Lisp_Object elt, Lisp_Object props, int risky)
18533 int n = 0, field, prec;
18534 int literal = 0;
18536 tail_recurse:
18537 if (depth > 100)
18538 elt = build_string ("*too-deep*");
18540 depth++;
18542 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18544 case Lisp_String:
18546 /* A string: output it and check for %-constructs within it. */
18547 unsigned char c;
18548 EMACS_INT offset = 0;
18550 if (SCHARS (elt) > 0
18551 && (!NILP (props) || risky))
18553 Lisp_Object oprops, aelt;
18554 oprops = Ftext_properties_at (make_number (0), elt);
18556 /* If the starting string's properties are not what
18557 we want, translate the string. Also, if the string
18558 is risky, do that anyway. */
18560 if (NILP (Fequal (props, oprops)) || risky)
18562 /* If the starting string has properties,
18563 merge the specified ones onto the existing ones. */
18564 if (! NILP (oprops) && !risky)
18566 Lisp_Object tem;
18568 oprops = Fcopy_sequence (oprops);
18569 tem = props;
18570 while (CONSP (tem))
18572 oprops = Fplist_put (oprops, XCAR (tem),
18573 XCAR (XCDR (tem)));
18574 tem = XCDR (XCDR (tem));
18576 props = oprops;
18579 aelt = Fassoc (elt, mode_line_proptrans_alist);
18580 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18582 /* AELT is what we want. Move it to the front
18583 without consing. */
18584 elt = XCAR (aelt);
18585 mode_line_proptrans_alist
18586 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18588 else
18590 Lisp_Object tem;
18592 /* If AELT has the wrong props, it is useless.
18593 so get rid of it. */
18594 if (! NILP (aelt))
18595 mode_line_proptrans_alist
18596 = Fdelq (aelt, mode_line_proptrans_alist);
18598 elt = Fcopy_sequence (elt);
18599 Fset_text_properties (make_number (0), Flength (elt),
18600 props, elt);
18601 /* Add this item to mode_line_proptrans_alist. */
18602 mode_line_proptrans_alist
18603 = Fcons (Fcons (elt, props),
18604 mode_line_proptrans_alist);
18605 /* Truncate mode_line_proptrans_alist
18606 to at most 50 elements. */
18607 tem = Fnthcdr (make_number (50),
18608 mode_line_proptrans_alist);
18609 if (! NILP (tem))
18610 XSETCDR (tem, Qnil);
18615 offset = 0;
18617 if (literal)
18619 prec = precision - n;
18620 switch (mode_line_target)
18622 case MODE_LINE_NOPROP:
18623 case MODE_LINE_TITLE:
18624 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18625 break;
18626 case MODE_LINE_STRING:
18627 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18628 break;
18629 case MODE_LINE_DISPLAY:
18630 n += display_string (NULL, elt, Qnil, 0, 0, it,
18631 0, prec, 0, STRING_MULTIBYTE (elt));
18632 break;
18635 break;
18638 /* Handle the non-literal case. */
18640 while ((precision <= 0 || n < precision)
18641 && SREF (elt, offset) != 0
18642 && (mode_line_target != MODE_LINE_DISPLAY
18643 || it->current_x < it->last_visible_x))
18645 EMACS_INT last_offset = offset;
18647 /* Advance to end of string or next format specifier. */
18648 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18651 if (offset - 1 != last_offset)
18653 EMACS_INT nchars, nbytes;
18655 /* Output to end of string or up to '%'. Field width
18656 is length of string. Don't output more than
18657 PRECISION allows us. */
18658 offset--;
18660 prec = c_string_width (SDATA (elt) + last_offset,
18661 offset - last_offset, precision - n,
18662 &nchars, &nbytes);
18664 switch (mode_line_target)
18666 case MODE_LINE_NOPROP:
18667 case MODE_LINE_TITLE:
18668 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18669 break;
18670 case MODE_LINE_STRING:
18672 EMACS_INT bytepos = last_offset;
18673 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18674 EMACS_INT endpos = (precision <= 0
18675 ? string_byte_to_char (elt, offset)
18676 : charpos + nchars);
18678 n += store_mode_line_string (NULL,
18679 Fsubstring (elt, make_number (charpos),
18680 make_number (endpos)),
18681 0, 0, 0, Qnil);
18683 break;
18684 case MODE_LINE_DISPLAY:
18686 EMACS_INT bytepos = last_offset;
18687 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18689 if (precision <= 0)
18690 nchars = string_byte_to_char (elt, offset) - charpos;
18691 n += display_string (NULL, elt, Qnil, 0, charpos,
18692 it, 0, nchars, 0,
18693 STRING_MULTIBYTE (elt));
18695 break;
18698 else /* c == '%' */
18700 EMACS_INT percent_position = offset;
18702 /* Get the specified minimum width. Zero means
18703 don't pad. */
18704 field = 0;
18705 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18706 field = field * 10 + c - '0';
18708 /* Don't pad beyond the total padding allowed. */
18709 if (field_width - n > 0 && field > field_width - n)
18710 field = field_width - n;
18712 /* Note that either PRECISION <= 0 or N < PRECISION. */
18713 prec = precision - n;
18715 if (c == 'M')
18716 n += display_mode_element (it, depth, field, prec,
18717 Vglobal_mode_string, props,
18718 risky);
18719 else if (c != 0)
18721 int multibyte;
18722 EMACS_INT bytepos, charpos;
18723 const unsigned char *spec;
18724 Lisp_Object string;
18726 bytepos = percent_position;
18727 charpos = (STRING_MULTIBYTE (elt)
18728 ? string_byte_to_char (elt, bytepos)
18729 : bytepos);
18730 spec = decode_mode_spec (it->w, c, field, prec, &string);
18731 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18733 switch (mode_line_target)
18735 case MODE_LINE_NOPROP:
18736 case MODE_LINE_TITLE:
18737 n += store_mode_line_noprop (spec, field, prec);
18738 break;
18739 case MODE_LINE_STRING:
18741 int len = strlen (spec);
18742 Lisp_Object tem = make_string (spec, len);
18743 props = Ftext_properties_at (make_number (charpos), elt);
18744 /* Should only keep face property in props */
18745 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18747 break;
18748 case MODE_LINE_DISPLAY:
18750 int nglyphs_before, nwritten;
18752 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18753 nwritten = display_string (spec, string, elt,
18754 charpos, 0, it,
18755 field, prec, 0,
18756 multibyte);
18758 /* Assign to the glyphs written above the
18759 string where the `%x' came from, position
18760 of the `%'. */
18761 if (nwritten > 0)
18763 struct glyph *glyph
18764 = (it->glyph_row->glyphs[TEXT_AREA]
18765 + nglyphs_before);
18766 int i;
18768 for (i = 0; i < nwritten; ++i)
18770 glyph[i].object = elt;
18771 glyph[i].charpos = charpos;
18774 n += nwritten;
18777 break;
18780 else /* c == 0 */
18781 break;
18785 break;
18787 case Lisp_Symbol:
18788 /* A symbol: process the value of the symbol recursively
18789 as if it appeared here directly. Avoid error if symbol void.
18790 Special case: if value of symbol is a string, output the string
18791 literally. */
18793 register Lisp_Object tem;
18795 /* If the variable is not marked as risky to set
18796 then its contents are risky to use. */
18797 if (NILP (Fget (elt, Qrisky_local_variable)))
18798 risky = 1;
18800 tem = Fboundp (elt);
18801 if (!NILP (tem))
18803 tem = Fsymbol_value (elt);
18804 /* If value is a string, output that string literally:
18805 don't check for % within it. */
18806 if (STRINGP (tem))
18807 literal = 1;
18809 if (!EQ (tem, elt))
18811 /* Give up right away for nil or t. */
18812 elt = tem;
18813 goto tail_recurse;
18817 break;
18819 case Lisp_Cons:
18821 register Lisp_Object car, tem;
18823 /* A cons cell: five distinct cases.
18824 If first element is :eval or :propertize, do something special.
18825 If first element is a string or a cons, process all the elements
18826 and effectively concatenate them.
18827 If first element is a negative number, truncate displaying cdr to
18828 at most that many characters. If positive, pad (with spaces)
18829 to at least that many characters.
18830 If first element is a symbol, process the cadr or caddr recursively
18831 according to whether the symbol's value is non-nil or nil. */
18832 car = XCAR (elt);
18833 if (EQ (car, QCeval))
18835 /* An element of the form (:eval FORM) means evaluate FORM
18836 and use the result as mode line elements. */
18838 if (risky)
18839 break;
18841 if (CONSP (XCDR (elt)))
18843 Lisp_Object spec;
18844 spec = safe_eval (XCAR (XCDR (elt)));
18845 n += display_mode_element (it, depth, field_width - n,
18846 precision - n, spec, props,
18847 risky);
18850 else if (EQ (car, QCpropertize))
18852 /* An element of the form (:propertize ELT PROPS...)
18853 means display ELT but applying properties PROPS. */
18855 if (risky)
18856 break;
18858 if (CONSP (XCDR (elt)))
18859 n += display_mode_element (it, depth, field_width - n,
18860 precision - n, XCAR (XCDR (elt)),
18861 XCDR (XCDR (elt)), risky);
18863 else if (SYMBOLP (car))
18865 tem = Fboundp (car);
18866 elt = XCDR (elt);
18867 if (!CONSP (elt))
18868 goto invalid;
18869 /* elt is now the cdr, and we know it is a cons cell.
18870 Use its car if CAR has a non-nil value. */
18871 if (!NILP (tem))
18873 tem = Fsymbol_value (car);
18874 if (!NILP (tem))
18876 elt = XCAR (elt);
18877 goto tail_recurse;
18880 /* Symbol's value is nil (or symbol is unbound)
18881 Get the cddr of the original list
18882 and if possible find the caddr and use that. */
18883 elt = XCDR (elt);
18884 if (NILP (elt))
18885 break;
18886 else if (!CONSP (elt))
18887 goto invalid;
18888 elt = XCAR (elt);
18889 goto tail_recurse;
18891 else if (INTEGERP (car))
18893 register int lim = XINT (car);
18894 elt = XCDR (elt);
18895 if (lim < 0)
18897 /* Negative int means reduce maximum width. */
18898 if (precision <= 0)
18899 precision = -lim;
18900 else
18901 precision = min (precision, -lim);
18903 else if (lim > 0)
18905 /* Padding specified. Don't let it be more than
18906 current maximum. */
18907 if (precision > 0)
18908 lim = min (precision, lim);
18910 /* If that's more padding than already wanted, queue it.
18911 But don't reduce padding already specified even if
18912 that is beyond the current truncation point. */
18913 field_width = max (lim, field_width);
18915 goto tail_recurse;
18917 else if (STRINGP (car) || CONSP (car))
18919 Lisp_Object halftail = elt;
18920 int len = 0;
18922 while (CONSP (elt)
18923 && (precision <= 0 || n < precision))
18925 n += display_mode_element (it, depth,
18926 /* Do padding only after the last
18927 element in the list. */
18928 (! CONSP (XCDR (elt))
18929 ? field_width - n
18930 : 0),
18931 precision - n, XCAR (elt),
18932 props, risky);
18933 elt = XCDR (elt);
18934 len++;
18935 if ((len & 1) == 0)
18936 halftail = XCDR (halftail);
18937 /* Check for cycle. */
18938 if (EQ (halftail, elt))
18939 break;
18943 break;
18945 default:
18946 invalid:
18947 elt = build_string ("*invalid*");
18948 goto tail_recurse;
18951 /* Pad to FIELD_WIDTH. */
18952 if (field_width > 0 && n < field_width)
18954 switch (mode_line_target)
18956 case MODE_LINE_NOPROP:
18957 case MODE_LINE_TITLE:
18958 n += store_mode_line_noprop ("", field_width - n, 0);
18959 break;
18960 case MODE_LINE_STRING:
18961 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18962 break;
18963 case MODE_LINE_DISPLAY:
18964 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18965 0, 0, 0);
18966 break;
18970 return n;
18973 /* Store a mode-line string element in mode_line_string_list.
18975 If STRING is non-null, display that C string. Otherwise, the Lisp
18976 string LISP_STRING is displayed.
18978 FIELD_WIDTH is the minimum number of output glyphs to produce.
18979 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18980 with spaces. FIELD_WIDTH <= 0 means don't pad.
18982 PRECISION is the maximum number of characters to output from
18983 STRING. PRECISION <= 0 means don't truncate the string.
18985 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18986 properties to the string.
18988 PROPS are the properties to add to the string.
18989 The mode_line_string_face face property is always added to the string.
18992 static int
18993 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18994 int field_width, int precision, Lisp_Object props)
18996 EMACS_INT len;
18997 int n = 0;
18999 if (string != NULL)
19001 len = strlen (string);
19002 if (precision > 0 && len > precision)
19003 len = precision;
19004 lisp_string = make_string (string, len);
19005 if (NILP (props))
19006 props = mode_line_string_face_prop;
19007 else if (!NILP (mode_line_string_face))
19009 Lisp_Object face = Fplist_get (props, Qface);
19010 props = Fcopy_sequence (props);
19011 if (NILP (face))
19012 face = mode_line_string_face;
19013 else
19014 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19015 props = Fplist_put (props, Qface, face);
19017 Fadd_text_properties (make_number (0), make_number (len),
19018 props, lisp_string);
19020 else
19022 len = XFASTINT (Flength (lisp_string));
19023 if (precision > 0 && len > precision)
19025 len = precision;
19026 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19027 precision = -1;
19029 if (!NILP (mode_line_string_face))
19031 Lisp_Object face;
19032 if (NILP (props))
19033 props = Ftext_properties_at (make_number (0), lisp_string);
19034 face = Fplist_get (props, Qface);
19035 if (NILP (face))
19036 face = mode_line_string_face;
19037 else
19038 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19039 props = Fcons (Qface, Fcons (face, Qnil));
19040 if (copy_string)
19041 lisp_string = Fcopy_sequence (lisp_string);
19043 if (!NILP (props))
19044 Fadd_text_properties (make_number (0), make_number (len),
19045 props, lisp_string);
19048 if (len > 0)
19050 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19051 n += len;
19054 if (field_width > len)
19056 field_width -= len;
19057 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19058 if (!NILP (props))
19059 Fadd_text_properties (make_number (0), make_number (field_width),
19060 props, lisp_string);
19061 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19062 n += field_width;
19065 return n;
19069 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19070 1, 4, 0,
19071 doc: /* Format a string out of a mode line format specification.
19072 First arg FORMAT specifies the mode line format (see `mode-line-format'
19073 for details) to use.
19075 Optional second arg FACE specifies the face property to put
19076 on all characters for which no face is specified.
19077 The value t means whatever face the window's mode line currently uses
19078 \(either `mode-line' or `mode-line-inactive', depending).
19079 A value of nil means the default is no face property.
19080 If FACE is an integer, the value string has no text properties.
19082 Optional third and fourth args WINDOW and BUFFER specify the window
19083 and buffer to use as the context for the formatting (defaults
19084 are the selected window and the window's buffer). */)
19085 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
19087 struct it it;
19088 int len;
19089 struct window *w;
19090 struct buffer *old_buffer = NULL;
19091 int face_id = -1;
19092 int no_props = INTEGERP (face);
19093 int count = SPECPDL_INDEX ();
19094 Lisp_Object str;
19095 int string_start = 0;
19097 if (NILP (window))
19098 window = selected_window;
19099 CHECK_WINDOW (window);
19100 w = XWINDOW (window);
19102 if (NILP (buffer))
19103 buffer = w->buffer;
19104 CHECK_BUFFER (buffer);
19106 /* Make formatting the modeline a non-op when noninteractive, otherwise
19107 there will be problems later caused by a partially initialized frame. */
19108 if (NILP (format) || noninteractive)
19109 return empty_unibyte_string;
19111 if (no_props)
19112 face = Qnil;
19114 if (!NILP (face))
19116 if (EQ (face, Qt))
19117 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
19118 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
19121 if (face_id < 0)
19122 face_id = DEFAULT_FACE_ID;
19124 if (XBUFFER (buffer) != current_buffer)
19125 old_buffer = current_buffer;
19127 /* Save things including mode_line_proptrans_alist,
19128 and set that to nil so that we don't alter the outer value. */
19129 record_unwind_protect (unwind_format_mode_line,
19130 format_mode_line_unwind_data
19131 (old_buffer, selected_window, 1));
19132 mode_line_proptrans_alist = Qnil;
19134 Fselect_window (window, Qt);
19135 if (old_buffer)
19136 set_buffer_internal_1 (XBUFFER (buffer));
19138 init_iterator (&it, w, -1, -1, NULL, face_id);
19140 if (no_props)
19142 mode_line_target = MODE_LINE_NOPROP;
19143 mode_line_string_face_prop = Qnil;
19144 mode_line_string_list = Qnil;
19145 string_start = MODE_LINE_NOPROP_LEN (0);
19147 else
19149 mode_line_target = MODE_LINE_STRING;
19150 mode_line_string_list = Qnil;
19151 mode_line_string_face = face;
19152 mode_line_string_face_prop
19153 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19156 push_kboard (FRAME_KBOARD (it.f));
19157 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19158 pop_kboard ();
19160 if (no_props)
19162 len = MODE_LINE_NOPROP_LEN (string_start);
19163 str = make_string (mode_line_noprop_buf + string_start, len);
19165 else
19167 mode_line_string_list = Fnreverse (mode_line_string_list);
19168 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19169 empty_unibyte_string);
19172 unbind_to (count, Qnil);
19173 return str;
19176 /* Write a null-terminated, right justified decimal representation of
19177 the positive integer D to BUF using a minimal field width WIDTH. */
19179 static void
19180 pint2str (register char *buf, register int width, register int d)
19182 register char *p = buf;
19184 if (d <= 0)
19185 *p++ = '0';
19186 else
19188 while (d > 0)
19190 *p++ = d % 10 + '0';
19191 d /= 10;
19195 for (width -= (int) (p - buf); width > 0; --width)
19196 *p++ = ' ';
19197 *p-- = '\0';
19198 while (p > buf)
19200 d = *buf;
19201 *buf++ = *p;
19202 *p-- = d;
19206 /* Write a null-terminated, right justified decimal and "human
19207 readable" representation of the nonnegative integer D to BUF using
19208 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19210 static const char power_letter[] =
19212 0, /* not used */
19213 'k', /* kilo */
19214 'M', /* mega */
19215 'G', /* giga */
19216 'T', /* tera */
19217 'P', /* peta */
19218 'E', /* exa */
19219 'Z', /* zetta */
19220 'Y' /* yotta */
19223 static void
19224 pint2hrstr (char *buf, int width, int d)
19226 /* We aim to represent the nonnegative integer D as
19227 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19228 int quotient = d;
19229 int remainder = 0;
19230 /* -1 means: do not use TENTHS. */
19231 int tenths = -1;
19232 int exponent = 0;
19234 /* Length of QUOTIENT.TENTHS as a string. */
19235 int length;
19237 char * psuffix;
19238 char * p;
19240 if (1000 <= quotient)
19242 /* Scale to the appropriate EXPONENT. */
19245 remainder = quotient % 1000;
19246 quotient /= 1000;
19247 exponent++;
19249 while (1000 <= quotient);
19251 /* Round to nearest and decide whether to use TENTHS or not. */
19252 if (quotient <= 9)
19254 tenths = remainder / 100;
19255 if (50 <= remainder % 100)
19257 if (tenths < 9)
19258 tenths++;
19259 else
19261 quotient++;
19262 if (quotient == 10)
19263 tenths = -1;
19264 else
19265 tenths = 0;
19269 else
19270 if (500 <= remainder)
19272 if (quotient < 999)
19273 quotient++;
19274 else
19276 quotient = 1;
19277 exponent++;
19278 tenths = 0;
19283 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19284 if (tenths == -1 && quotient <= 99)
19285 if (quotient <= 9)
19286 length = 1;
19287 else
19288 length = 2;
19289 else
19290 length = 3;
19291 p = psuffix = buf + max (width, length);
19293 /* Print EXPONENT. */
19294 if (exponent)
19295 *psuffix++ = power_letter[exponent];
19296 *psuffix = '\0';
19298 /* Print TENTHS. */
19299 if (tenths >= 0)
19301 *--p = '0' + tenths;
19302 *--p = '.';
19305 /* Print QUOTIENT. */
19308 int digit = quotient % 10;
19309 *--p = '0' + digit;
19311 while ((quotient /= 10) != 0);
19313 /* Print leading spaces. */
19314 while (buf < p)
19315 *--p = ' ';
19318 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19319 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19320 type of CODING_SYSTEM. Return updated pointer into BUF. */
19322 static unsigned char invalid_eol_type[] = "(*invalid*)";
19324 static char *
19325 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19327 Lisp_Object val;
19328 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19329 const unsigned char *eol_str;
19330 int eol_str_len;
19331 /* The EOL conversion we are using. */
19332 Lisp_Object eoltype;
19334 val = CODING_SYSTEM_SPEC (coding_system);
19335 eoltype = Qnil;
19337 if (!VECTORP (val)) /* Not yet decided. */
19339 if (multibyte)
19340 *buf++ = '-';
19341 if (eol_flag)
19342 eoltype = eol_mnemonic_undecided;
19343 /* Don't mention EOL conversion if it isn't decided. */
19345 else
19347 Lisp_Object attrs;
19348 Lisp_Object eolvalue;
19350 attrs = AREF (val, 0);
19351 eolvalue = AREF (val, 2);
19353 if (multibyte)
19354 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19356 if (eol_flag)
19358 /* The EOL conversion that is normal on this system. */
19360 if (NILP (eolvalue)) /* Not yet decided. */
19361 eoltype = eol_mnemonic_undecided;
19362 else if (VECTORP (eolvalue)) /* Not yet decided. */
19363 eoltype = eol_mnemonic_undecided;
19364 else /* eolvalue is Qunix, Qdos, or Qmac. */
19365 eoltype = (EQ (eolvalue, Qunix)
19366 ? eol_mnemonic_unix
19367 : (EQ (eolvalue, Qdos) == 1
19368 ? eol_mnemonic_dos : eol_mnemonic_mac));
19372 if (eol_flag)
19374 /* Mention the EOL conversion if it is not the usual one. */
19375 if (STRINGP (eoltype))
19377 eol_str = SDATA (eoltype);
19378 eol_str_len = SBYTES (eoltype);
19380 else if (CHARACTERP (eoltype))
19382 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19383 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19384 eol_str = tmp;
19386 else
19388 eol_str = invalid_eol_type;
19389 eol_str_len = sizeof (invalid_eol_type) - 1;
19391 memcpy (buf, eol_str, eol_str_len);
19392 buf += eol_str_len;
19395 return buf;
19398 /* Return a string for the output of a mode line %-spec for window W,
19399 generated by character C. PRECISION >= 0 means don't return a
19400 string longer than that value. FIELD_WIDTH > 0 means pad the
19401 string returned with spaces to that value. Return a Lisp string in
19402 *STRING if the resulting string is taken from that Lisp string.
19404 Note we operate on the current buffer for most purposes,
19405 the exception being w->base_line_pos. */
19407 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19409 static const char *
19410 decode_mode_spec (struct window *w, register int c, int field_width,
19411 int precision, Lisp_Object *string)
19413 Lisp_Object obj;
19414 struct frame *f = XFRAME (WINDOW_FRAME (w));
19415 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19416 struct buffer *b = current_buffer;
19418 obj = Qnil;
19419 *string = Qnil;
19421 switch (c)
19423 case '*':
19424 if (!NILP (b->read_only))
19425 return "%";
19426 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19427 return "*";
19428 return "-";
19430 case '+':
19431 /* This differs from %* only for a modified read-only buffer. */
19432 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19433 return "*";
19434 if (!NILP (b->read_only))
19435 return "%";
19436 return "-";
19438 case '&':
19439 /* This differs from %* in ignoring read-only-ness. */
19440 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19441 return "*";
19442 return "-";
19444 case '%':
19445 return "%";
19447 case '[':
19449 int i;
19450 char *p;
19452 if (command_loop_level > 5)
19453 return "[[[... ";
19454 p = decode_mode_spec_buf;
19455 for (i = 0; i < command_loop_level; i++)
19456 *p++ = '[';
19457 *p = 0;
19458 return decode_mode_spec_buf;
19461 case ']':
19463 int i;
19464 char *p;
19466 if (command_loop_level > 5)
19467 return " ...]]]";
19468 p = decode_mode_spec_buf;
19469 for (i = 0; i < command_loop_level; i++)
19470 *p++ = ']';
19471 *p = 0;
19472 return decode_mode_spec_buf;
19475 case '-':
19477 register int i;
19479 /* Let lots_of_dashes be a string of infinite length. */
19480 if (mode_line_target == MODE_LINE_NOPROP ||
19481 mode_line_target == MODE_LINE_STRING)
19482 return "--";
19483 if (field_width <= 0
19484 || field_width > sizeof (lots_of_dashes))
19486 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19487 decode_mode_spec_buf[i] = '-';
19488 decode_mode_spec_buf[i] = '\0';
19489 return decode_mode_spec_buf;
19491 else
19492 return lots_of_dashes;
19495 case 'b':
19496 obj = b->name;
19497 break;
19499 case 'c':
19500 /* %c and %l are ignored in `frame-title-format'.
19501 (In redisplay_internal, the frame title is drawn _before_ the
19502 windows are updated, so the stuff which depends on actual
19503 window contents (such as %l) may fail to render properly, or
19504 even crash emacs.) */
19505 if (mode_line_target == MODE_LINE_TITLE)
19506 return "";
19507 else
19509 int col = (int) current_column (); /* iftc */
19510 w->column_number_displayed = make_number (col);
19511 pint2str (decode_mode_spec_buf, field_width, col);
19512 return decode_mode_spec_buf;
19515 case 'e':
19516 #ifndef SYSTEM_MALLOC
19518 if (NILP (Vmemory_full))
19519 return "";
19520 else
19521 return "!MEM FULL! ";
19523 #else
19524 return "";
19525 #endif
19527 case 'F':
19528 /* %F displays the frame name. */
19529 if (!NILP (f->title))
19530 return (char *) SDATA (f->title);
19531 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19532 return (char *) SDATA (f->name);
19533 return "Emacs";
19535 case 'f':
19536 obj = b->filename;
19537 break;
19539 case 'i':
19541 EMACS_INT size = ZV - BEGV;
19542 pint2str (decode_mode_spec_buf, field_width, size);
19543 return decode_mode_spec_buf;
19546 case 'I':
19548 EMACS_INT size = ZV - BEGV;
19549 pint2hrstr (decode_mode_spec_buf, field_width, size);
19550 return decode_mode_spec_buf;
19553 case 'l':
19555 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19556 int topline, nlines, height;
19557 EMACS_INT junk;
19559 /* %c and %l are ignored in `frame-title-format'. */
19560 if (mode_line_target == MODE_LINE_TITLE)
19561 return "";
19563 startpos = XMARKER (w->start)->charpos;
19564 startpos_byte = marker_byte_position (w->start);
19565 height = WINDOW_TOTAL_LINES (w);
19567 /* If we decided that this buffer isn't suitable for line numbers,
19568 don't forget that too fast. */
19569 if (EQ (w->base_line_pos, w->buffer))
19570 goto no_value;
19571 /* But do forget it, if the window shows a different buffer now. */
19572 else if (BUFFERP (w->base_line_pos))
19573 w->base_line_pos = Qnil;
19575 /* If the buffer is very big, don't waste time. */
19576 if (INTEGERP (Vline_number_display_limit)
19577 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19579 w->base_line_pos = Qnil;
19580 w->base_line_number = Qnil;
19581 goto no_value;
19584 if (INTEGERP (w->base_line_number)
19585 && INTEGERP (w->base_line_pos)
19586 && XFASTINT (w->base_line_pos) <= startpos)
19588 line = XFASTINT (w->base_line_number);
19589 linepos = XFASTINT (w->base_line_pos);
19590 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19592 else
19594 line = 1;
19595 linepos = BUF_BEGV (b);
19596 linepos_byte = BUF_BEGV_BYTE (b);
19599 /* Count lines from base line to window start position. */
19600 nlines = display_count_lines (linepos, linepos_byte,
19601 startpos_byte,
19602 startpos, &junk);
19604 topline = nlines + line;
19606 /* Determine a new base line, if the old one is too close
19607 or too far away, or if we did not have one.
19608 "Too close" means it's plausible a scroll-down would
19609 go back past it. */
19610 if (startpos == BUF_BEGV (b))
19612 w->base_line_number = make_number (topline);
19613 w->base_line_pos = make_number (BUF_BEGV (b));
19615 else if (nlines < height + 25 || nlines > height * 3 + 50
19616 || linepos == BUF_BEGV (b))
19618 EMACS_INT limit = BUF_BEGV (b);
19619 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19620 EMACS_INT position;
19621 int distance = (height * 2 + 30) * line_number_display_limit_width;
19623 if (startpos - distance > limit)
19625 limit = startpos - distance;
19626 limit_byte = CHAR_TO_BYTE (limit);
19629 nlines = display_count_lines (startpos, startpos_byte,
19630 limit_byte,
19631 - (height * 2 + 30),
19632 &position);
19633 /* If we couldn't find the lines we wanted within
19634 line_number_display_limit_width chars per line,
19635 give up on line numbers for this window. */
19636 if (position == limit_byte && limit == startpos - distance)
19638 w->base_line_pos = w->buffer;
19639 w->base_line_number = Qnil;
19640 goto no_value;
19643 w->base_line_number = make_number (topline - nlines);
19644 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19647 /* Now count lines from the start pos to point. */
19648 nlines = display_count_lines (startpos, startpos_byte,
19649 PT_BYTE, PT, &junk);
19651 /* Record that we did display the line number. */
19652 line_number_displayed = 1;
19654 /* Make the string to show. */
19655 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19656 return decode_mode_spec_buf;
19657 no_value:
19659 char* p = decode_mode_spec_buf;
19660 int pad = field_width - 2;
19661 while (pad-- > 0)
19662 *p++ = ' ';
19663 *p++ = '?';
19664 *p++ = '?';
19665 *p = '\0';
19666 return decode_mode_spec_buf;
19669 break;
19671 case 'm':
19672 obj = b->mode_name;
19673 break;
19675 case 'n':
19676 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19677 return " Narrow";
19678 break;
19680 case 'p':
19682 EMACS_INT pos = marker_position (w->start);
19683 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19685 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19687 if (pos <= BUF_BEGV (b))
19688 return "All";
19689 else
19690 return "Bottom";
19692 else if (pos <= BUF_BEGV (b))
19693 return "Top";
19694 else
19696 if (total > 1000000)
19697 /* Do it differently for a large value, to avoid overflow. */
19698 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19699 else
19700 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19701 /* We can't normally display a 3-digit number,
19702 so get us a 2-digit number that is close. */
19703 if (total == 100)
19704 total = 99;
19705 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19706 return decode_mode_spec_buf;
19710 /* Display percentage of size above the bottom of the screen. */
19711 case 'P':
19713 EMACS_INT toppos = marker_position (w->start);
19714 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19715 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19717 if (botpos >= BUF_ZV (b))
19719 if (toppos <= BUF_BEGV (b))
19720 return "All";
19721 else
19722 return "Bottom";
19724 else
19726 if (total > 1000000)
19727 /* Do it differently for a large value, to avoid overflow. */
19728 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19729 else
19730 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19731 /* We can't normally display a 3-digit number,
19732 so get us a 2-digit number that is close. */
19733 if (total == 100)
19734 total = 99;
19735 if (toppos <= BUF_BEGV (b))
19736 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19737 else
19738 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19739 return decode_mode_spec_buf;
19743 case 's':
19744 /* status of process */
19745 obj = Fget_buffer_process (Fcurrent_buffer ());
19746 if (NILP (obj))
19747 return "no process";
19748 #ifndef MSDOS
19749 obj = Fsymbol_name (Fprocess_status (obj));
19750 #endif
19751 break;
19753 case '@':
19755 int count = inhibit_garbage_collection ();
19756 Lisp_Object val = call1 (intern ("file-remote-p"),
19757 current_buffer->directory);
19758 unbind_to (count, Qnil);
19760 if (NILP (val))
19761 return "-";
19762 else
19763 return "@";
19766 case 't': /* indicate TEXT or BINARY */
19767 #ifdef MODE_LINE_BINARY_TEXT
19768 return MODE_LINE_BINARY_TEXT (b);
19769 #else
19770 return "T";
19771 #endif
19773 case 'z':
19774 /* coding-system (not including end-of-line format) */
19775 case 'Z':
19776 /* coding-system (including end-of-line type) */
19778 int eol_flag = (c == 'Z');
19779 char *p = decode_mode_spec_buf;
19781 if (! FRAME_WINDOW_P (f))
19783 /* No need to mention EOL here--the terminal never needs
19784 to do EOL conversion. */
19785 p = decode_mode_spec_coding (CODING_ID_NAME
19786 (FRAME_KEYBOARD_CODING (f)->id),
19787 p, 0);
19788 p = decode_mode_spec_coding (CODING_ID_NAME
19789 (FRAME_TERMINAL_CODING (f)->id),
19790 p, 0);
19792 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19793 p, eol_flag);
19795 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19796 #ifdef subprocesses
19797 obj = Fget_buffer_process (Fcurrent_buffer ());
19798 if (PROCESSP (obj))
19800 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19801 p, eol_flag);
19802 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19803 p, eol_flag);
19805 #endif /* subprocesses */
19806 #endif /* 0 */
19807 *p = 0;
19808 return decode_mode_spec_buf;
19812 if (STRINGP (obj))
19814 *string = obj;
19815 return (char *) SDATA (obj);
19817 else
19818 return "";
19822 /* Count up to COUNT lines starting from START / START_BYTE.
19823 But don't go beyond LIMIT_BYTE.
19824 Return the number of lines thus found (always nonnegative).
19826 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19828 static int
19829 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19830 EMACS_INT limit_byte, int count,
19831 EMACS_INT *byte_pos_ptr)
19833 register unsigned char *cursor;
19834 unsigned char *base;
19836 register int ceiling;
19837 register unsigned char *ceiling_addr;
19838 int orig_count = count;
19840 /* If we are not in selective display mode,
19841 check only for newlines. */
19842 int selective_display = (!NILP (current_buffer->selective_display)
19843 && !INTEGERP (current_buffer->selective_display));
19845 if (count > 0)
19847 while (start_byte < limit_byte)
19849 ceiling = BUFFER_CEILING_OF (start_byte);
19850 ceiling = min (limit_byte - 1, ceiling);
19851 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19852 base = (cursor = BYTE_POS_ADDR (start_byte));
19853 while (1)
19855 if (selective_display)
19856 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19858 else
19859 while (*cursor != '\n' && ++cursor != ceiling_addr)
19862 if (cursor != ceiling_addr)
19864 if (--count == 0)
19866 start_byte += cursor - base + 1;
19867 *byte_pos_ptr = start_byte;
19868 return orig_count;
19870 else
19871 if (++cursor == ceiling_addr)
19872 break;
19874 else
19875 break;
19877 start_byte += cursor - base;
19880 else
19882 while (start_byte > limit_byte)
19884 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19885 ceiling = max (limit_byte, ceiling);
19886 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19887 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19888 while (1)
19890 if (selective_display)
19891 while (--cursor != ceiling_addr
19892 && *cursor != '\n' && *cursor != 015)
19894 else
19895 while (--cursor != ceiling_addr && *cursor != '\n')
19898 if (cursor != ceiling_addr)
19900 if (++count == 0)
19902 start_byte += cursor - base + 1;
19903 *byte_pos_ptr = start_byte;
19904 /* When scanning backwards, we should
19905 not count the newline posterior to which we stop. */
19906 return - orig_count - 1;
19909 else
19910 break;
19912 /* Here we add 1 to compensate for the last decrement
19913 of CURSOR, which took it past the valid range. */
19914 start_byte += cursor - base + 1;
19918 *byte_pos_ptr = limit_byte;
19920 if (count < 0)
19921 return - orig_count + count;
19922 return orig_count - count;
19928 /***********************************************************************
19929 Displaying strings
19930 ***********************************************************************/
19932 /* Display a NUL-terminated string, starting with index START.
19934 If STRING is non-null, display that C string. Otherwise, the Lisp
19935 string LISP_STRING is displayed. There's a case that STRING is
19936 non-null and LISP_STRING is not nil. It means STRING is a string
19937 data of LISP_STRING. In that case, we display LISP_STRING while
19938 ignoring its text properties.
19940 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19941 FACE_STRING. Display STRING or LISP_STRING with the face at
19942 FACE_STRING_POS in FACE_STRING:
19944 Display the string in the environment given by IT, but use the
19945 standard display table, temporarily.
19947 FIELD_WIDTH is the minimum number of output glyphs to produce.
19948 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19949 with spaces. If STRING has more characters, more than FIELD_WIDTH
19950 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19952 PRECISION is the maximum number of characters to output from
19953 STRING. PRECISION < 0 means don't truncate the string.
19955 This is roughly equivalent to printf format specifiers:
19957 FIELD_WIDTH PRECISION PRINTF
19958 ----------------------------------------
19959 -1 -1 %s
19960 -1 10 %.10s
19961 10 -1 %10s
19962 20 10 %20.10s
19964 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19965 display them, and < 0 means obey the current buffer's value of
19966 enable_multibyte_characters.
19968 Value is the number of columns displayed. */
19970 static int
19971 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19972 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19973 int field_width, int precision, int max_x, int multibyte)
19975 int hpos_at_start = it->hpos;
19976 int saved_face_id = it->face_id;
19977 struct glyph_row *row = it->glyph_row;
19979 /* Initialize the iterator IT for iteration over STRING beginning
19980 with index START. */
19981 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19982 precision, field_width, multibyte);
19983 if (string && STRINGP (lisp_string))
19984 /* LISP_STRING is the one returned by decode_mode_spec. We should
19985 ignore its text properties. */
19986 it->stop_charpos = -1;
19988 /* If displaying STRING, set up the face of the iterator
19989 from LISP_STRING, if that's given. */
19990 if (STRINGP (face_string))
19992 EMACS_INT endptr;
19993 struct face *face;
19995 it->face_id
19996 = face_at_string_position (it->w, face_string, face_string_pos,
19997 0, it->region_beg_charpos,
19998 it->region_end_charpos,
19999 &endptr, it->base_face_id, 0);
20000 face = FACE_FROM_ID (it->f, it->face_id);
20001 it->face_box_p = face->box != FACE_NO_BOX;
20004 /* Set max_x to the maximum allowed X position. Don't let it go
20005 beyond the right edge of the window. */
20006 if (max_x <= 0)
20007 max_x = it->last_visible_x;
20008 else
20009 max_x = min (max_x, it->last_visible_x);
20011 /* Skip over display elements that are not visible. because IT->w is
20012 hscrolled. */
20013 if (it->current_x < it->first_visible_x)
20014 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20015 MOVE_TO_POS | MOVE_TO_X);
20017 row->ascent = it->max_ascent;
20018 row->height = it->max_ascent + it->max_descent;
20019 row->phys_ascent = it->max_phys_ascent;
20020 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20021 row->extra_line_spacing = it->max_extra_line_spacing;
20023 /* This condition is for the case that we are called with current_x
20024 past last_visible_x. */
20025 while (it->current_x < max_x)
20027 int x_before, x, n_glyphs_before, i, nglyphs;
20029 /* Get the next display element. */
20030 if (!get_next_display_element (it))
20031 break;
20033 /* Produce glyphs. */
20034 x_before = it->current_x;
20035 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
20036 PRODUCE_GLYPHS (it);
20038 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
20039 i = 0;
20040 x = x_before;
20041 while (i < nglyphs)
20043 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20045 if (it->line_wrap != TRUNCATE
20046 && x + glyph->pixel_width > max_x)
20048 /* End of continued line or max_x reached. */
20049 if (CHAR_GLYPH_PADDING_P (*glyph))
20051 /* A wide character is unbreakable. */
20052 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
20053 it->current_x = x_before;
20055 else
20057 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
20058 it->current_x = x;
20060 break;
20062 else if (x + glyph->pixel_width >= it->first_visible_x)
20064 /* Glyph is at least partially visible. */
20065 ++it->hpos;
20066 if (x < it->first_visible_x)
20067 it->glyph_row->x = x - it->first_visible_x;
20069 else
20071 /* Glyph is off the left margin of the display area.
20072 Should not happen. */
20073 abort ();
20076 row->ascent = max (row->ascent, it->max_ascent);
20077 row->height = max (row->height, it->max_ascent + it->max_descent);
20078 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20079 row->phys_height = max (row->phys_height,
20080 it->max_phys_ascent + it->max_phys_descent);
20081 row->extra_line_spacing = max (row->extra_line_spacing,
20082 it->max_extra_line_spacing);
20083 x += glyph->pixel_width;
20084 ++i;
20087 /* Stop if max_x reached. */
20088 if (i < nglyphs)
20089 break;
20091 /* Stop at line ends. */
20092 if (ITERATOR_AT_END_OF_LINE_P (it))
20094 it->continuation_lines_width = 0;
20095 break;
20098 set_iterator_to_next (it, 1);
20100 /* Stop if truncating at the right edge. */
20101 if (it->line_wrap == TRUNCATE
20102 && it->current_x >= it->last_visible_x)
20104 /* Add truncation mark, but don't do it if the line is
20105 truncated at a padding space. */
20106 if (IT_CHARPOS (*it) < it->string_nchars)
20108 if (!FRAME_WINDOW_P (it->f))
20110 int i, n;
20112 if (it->current_x > it->last_visible_x)
20114 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20115 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20116 break;
20117 for (n = row->used[TEXT_AREA]; i < n; ++i)
20119 row->used[TEXT_AREA] = i;
20120 produce_special_glyphs (it, IT_TRUNCATION);
20123 produce_special_glyphs (it, IT_TRUNCATION);
20125 it->glyph_row->truncated_on_right_p = 1;
20127 break;
20131 /* Maybe insert a truncation at the left. */
20132 if (it->first_visible_x
20133 && IT_CHARPOS (*it) > 0)
20135 if (!FRAME_WINDOW_P (it->f))
20136 insert_left_trunc_glyphs (it);
20137 it->glyph_row->truncated_on_left_p = 1;
20140 it->face_id = saved_face_id;
20142 /* Value is number of columns displayed. */
20143 return it->hpos - hpos_at_start;
20148 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20149 appears as an element of LIST or as the car of an element of LIST.
20150 If PROPVAL is a list, compare each element against LIST in that
20151 way, and return 1/2 if any element of PROPVAL is found in LIST.
20152 Otherwise return 0. This function cannot quit.
20153 The return value is 2 if the text is invisible but with an ellipsis
20154 and 1 if it's invisible and without an ellipsis. */
20157 invisible_p (register Lisp_Object propval, Lisp_Object list)
20159 register Lisp_Object tail, proptail;
20161 for (tail = list; CONSP (tail); tail = XCDR (tail))
20163 register Lisp_Object tem;
20164 tem = XCAR (tail);
20165 if (EQ (propval, tem))
20166 return 1;
20167 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20168 return NILP (XCDR (tem)) ? 1 : 2;
20171 if (CONSP (propval))
20173 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20175 Lisp_Object propelt;
20176 propelt = XCAR (proptail);
20177 for (tail = list; CONSP (tail); tail = XCDR (tail))
20179 register Lisp_Object tem;
20180 tem = XCAR (tail);
20181 if (EQ (propelt, tem))
20182 return 1;
20183 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20184 return NILP (XCDR (tem)) ? 1 : 2;
20189 return 0;
20192 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20193 doc: /* Non-nil if the property makes the text invisible.
20194 POS-OR-PROP can be a marker or number, in which case it is taken to be
20195 a position in the current buffer and the value of the `invisible' property
20196 is checked; or it can be some other value, which is then presumed to be the
20197 value of the `invisible' property of the text of interest.
20198 The non-nil value returned can be t for truly invisible text or something
20199 else if the text is replaced by an ellipsis. */)
20200 (Lisp_Object pos_or_prop)
20202 Lisp_Object prop
20203 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20204 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20205 : pos_or_prop);
20206 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20207 return (invis == 0 ? Qnil
20208 : invis == 1 ? Qt
20209 : make_number (invis));
20212 /* Calculate a width or height in pixels from a specification using
20213 the following elements:
20215 SPEC ::=
20216 NUM - a (fractional) multiple of the default font width/height
20217 (NUM) - specifies exactly NUM pixels
20218 UNIT - a fixed number of pixels, see below.
20219 ELEMENT - size of a display element in pixels, see below.
20220 (NUM . SPEC) - equals NUM * SPEC
20221 (+ SPEC SPEC ...) - add pixel values
20222 (- SPEC SPEC ...) - subtract pixel values
20223 (- SPEC) - negate pixel value
20225 NUM ::=
20226 INT or FLOAT - a number constant
20227 SYMBOL - use symbol's (buffer local) variable binding.
20229 UNIT ::=
20230 in - pixels per inch *)
20231 mm - pixels per 1/1000 meter *)
20232 cm - pixels per 1/100 meter *)
20233 width - width of current font in pixels.
20234 height - height of current font in pixels.
20236 *) using the ratio(s) defined in display-pixels-per-inch.
20238 ELEMENT ::=
20240 left-fringe - left fringe width in pixels
20241 right-fringe - right fringe width in pixels
20243 left-margin - left margin width in pixels
20244 right-margin - right margin width in pixels
20246 scroll-bar - scroll-bar area width in pixels
20248 Examples:
20250 Pixels corresponding to 5 inches:
20251 (5 . in)
20253 Total width of non-text areas on left side of window (if scroll-bar is on left):
20254 '(space :width (+ left-fringe left-margin scroll-bar))
20256 Align to first text column (in header line):
20257 '(space :align-to 0)
20259 Align to middle of text area minus half the width of variable `my-image'
20260 containing a loaded image:
20261 '(space :align-to (0.5 . (- text my-image)))
20263 Width of left margin minus width of 1 character in the default font:
20264 '(space :width (- left-margin 1))
20266 Width of left margin minus width of 2 characters in the current font:
20267 '(space :width (- left-margin (2 . width)))
20269 Center 1 character over left-margin (in header line):
20270 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20272 Different ways to express width of left fringe plus left margin minus one pixel:
20273 '(space :width (- (+ left-fringe left-margin) (1)))
20274 '(space :width (+ left-fringe left-margin (- (1))))
20275 '(space :width (+ left-fringe left-margin (-1)))
20279 #define NUMVAL(X) \
20280 ((INTEGERP (X) || FLOATP (X)) \
20281 ? XFLOATINT (X) \
20282 : - 1)
20285 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20286 struct font *font, int width_p, int *align_to)
20288 double pixels;
20290 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20291 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20293 if (NILP (prop))
20294 return OK_PIXELS (0);
20296 xassert (FRAME_LIVE_P (it->f));
20298 if (SYMBOLP (prop))
20300 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20302 char *unit = SDATA (SYMBOL_NAME (prop));
20304 if (unit[0] == 'i' && unit[1] == 'n')
20305 pixels = 1.0;
20306 else if (unit[0] == 'm' && unit[1] == 'm')
20307 pixels = 25.4;
20308 else if (unit[0] == 'c' && unit[1] == 'm')
20309 pixels = 2.54;
20310 else
20311 pixels = 0;
20312 if (pixels > 0)
20314 double ppi;
20315 #ifdef HAVE_WINDOW_SYSTEM
20316 if (FRAME_WINDOW_P (it->f)
20317 && (ppi = (width_p
20318 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20319 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20320 ppi > 0))
20321 return OK_PIXELS (ppi / pixels);
20322 #endif
20324 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20325 || (CONSP (Vdisplay_pixels_per_inch)
20326 && (ppi = (width_p
20327 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20328 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20329 ppi > 0)))
20330 return OK_PIXELS (ppi / pixels);
20332 return 0;
20336 #ifdef HAVE_WINDOW_SYSTEM
20337 if (EQ (prop, Qheight))
20338 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20339 if (EQ (prop, Qwidth))
20340 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20341 #else
20342 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20343 return OK_PIXELS (1);
20344 #endif
20346 if (EQ (prop, Qtext))
20347 return OK_PIXELS (width_p
20348 ? window_box_width (it->w, TEXT_AREA)
20349 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20351 if (align_to && *align_to < 0)
20353 *res = 0;
20354 if (EQ (prop, Qleft))
20355 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20356 if (EQ (prop, Qright))
20357 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20358 if (EQ (prop, Qcenter))
20359 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20360 + window_box_width (it->w, TEXT_AREA) / 2);
20361 if (EQ (prop, Qleft_fringe))
20362 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20363 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20364 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20365 if (EQ (prop, Qright_fringe))
20366 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20367 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20368 : window_box_right_offset (it->w, TEXT_AREA));
20369 if (EQ (prop, Qleft_margin))
20370 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20371 if (EQ (prop, Qright_margin))
20372 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20373 if (EQ (prop, Qscroll_bar))
20374 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20376 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20377 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20378 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20379 : 0)));
20381 else
20383 if (EQ (prop, Qleft_fringe))
20384 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20385 if (EQ (prop, Qright_fringe))
20386 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20387 if (EQ (prop, Qleft_margin))
20388 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20389 if (EQ (prop, Qright_margin))
20390 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20391 if (EQ (prop, Qscroll_bar))
20392 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20395 prop = Fbuffer_local_value (prop, it->w->buffer);
20398 if (INTEGERP (prop) || FLOATP (prop))
20400 int base_unit = (width_p
20401 ? FRAME_COLUMN_WIDTH (it->f)
20402 : FRAME_LINE_HEIGHT (it->f));
20403 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20406 if (CONSP (prop))
20408 Lisp_Object car = XCAR (prop);
20409 Lisp_Object cdr = XCDR (prop);
20411 if (SYMBOLP (car))
20413 #ifdef HAVE_WINDOW_SYSTEM
20414 if (FRAME_WINDOW_P (it->f)
20415 && valid_image_p (prop))
20417 int id = lookup_image (it->f, prop);
20418 struct image *img = IMAGE_FROM_ID (it->f, id);
20420 return OK_PIXELS (width_p ? img->width : img->height);
20422 #endif
20423 if (EQ (car, Qplus) || EQ (car, Qminus))
20425 int first = 1;
20426 double px;
20428 pixels = 0;
20429 while (CONSP (cdr))
20431 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20432 font, width_p, align_to))
20433 return 0;
20434 if (first)
20435 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20436 else
20437 pixels += px;
20438 cdr = XCDR (cdr);
20440 if (EQ (car, Qminus))
20441 pixels = -pixels;
20442 return OK_PIXELS (pixels);
20445 car = Fbuffer_local_value (car, it->w->buffer);
20448 if (INTEGERP (car) || FLOATP (car))
20450 double fact;
20451 pixels = XFLOATINT (car);
20452 if (NILP (cdr))
20453 return OK_PIXELS (pixels);
20454 if (calc_pixel_width_or_height (&fact, it, cdr,
20455 font, width_p, align_to))
20456 return OK_PIXELS (pixels * fact);
20457 return 0;
20460 return 0;
20463 return 0;
20467 /***********************************************************************
20468 Glyph Display
20469 ***********************************************************************/
20471 #ifdef HAVE_WINDOW_SYSTEM
20473 #if GLYPH_DEBUG
20475 void
20476 dump_glyph_string (s)
20477 struct glyph_string *s;
20479 fprintf (stderr, "glyph string\n");
20480 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20481 s->x, s->y, s->width, s->height);
20482 fprintf (stderr, " ybase = %d\n", s->ybase);
20483 fprintf (stderr, " hl = %d\n", s->hl);
20484 fprintf (stderr, " left overhang = %d, right = %d\n",
20485 s->left_overhang, s->right_overhang);
20486 fprintf (stderr, " nchars = %d\n", s->nchars);
20487 fprintf (stderr, " extends to end of line = %d\n",
20488 s->extends_to_end_of_line_p);
20489 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20490 fprintf (stderr, " bg width = %d\n", s->background_width);
20493 #endif /* GLYPH_DEBUG */
20495 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20496 of XChar2b structures for S; it can't be allocated in
20497 init_glyph_string because it must be allocated via `alloca'. W
20498 is the window on which S is drawn. ROW and AREA are the glyph row
20499 and area within the row from which S is constructed. START is the
20500 index of the first glyph structure covered by S. HL is a
20501 face-override for drawing S. */
20503 #ifdef HAVE_NTGUI
20504 #define OPTIONAL_HDC(hdc) HDC hdc,
20505 #define DECLARE_HDC(hdc) HDC hdc;
20506 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20507 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20508 #endif
20510 #ifndef OPTIONAL_HDC
20511 #define OPTIONAL_HDC(hdc)
20512 #define DECLARE_HDC(hdc)
20513 #define ALLOCATE_HDC(hdc, f)
20514 #define RELEASE_HDC(hdc, f)
20515 #endif
20517 static void
20518 init_glyph_string (struct glyph_string *s,
20519 OPTIONAL_HDC (hdc)
20520 XChar2b *char2b, struct window *w, struct glyph_row *row,
20521 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20523 memset (s, 0, sizeof *s);
20524 s->w = w;
20525 s->f = XFRAME (w->frame);
20526 #ifdef HAVE_NTGUI
20527 s->hdc = hdc;
20528 #endif
20529 s->display = FRAME_X_DISPLAY (s->f);
20530 s->window = FRAME_X_WINDOW (s->f);
20531 s->char2b = char2b;
20532 s->hl = hl;
20533 s->row = row;
20534 s->area = area;
20535 s->first_glyph = row->glyphs[area] + start;
20536 s->height = row->height;
20537 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20538 s->ybase = s->y + row->ascent;
20542 /* Append the list of glyph strings with head H and tail T to the list
20543 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20545 static INLINE void
20546 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20547 struct glyph_string *h, struct glyph_string *t)
20549 if (h)
20551 if (*head)
20552 (*tail)->next = h;
20553 else
20554 *head = h;
20555 h->prev = *tail;
20556 *tail = t;
20561 /* Prepend the list of glyph strings with head H and tail T to the
20562 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20563 result. */
20565 static INLINE void
20566 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20567 struct glyph_string *h, struct glyph_string *t)
20569 if (h)
20571 if (*head)
20572 (*head)->prev = t;
20573 else
20574 *tail = t;
20575 t->next = *head;
20576 *head = h;
20581 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20582 Set *HEAD and *TAIL to the resulting list. */
20584 static INLINE void
20585 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20586 struct glyph_string *s)
20588 s->next = s->prev = NULL;
20589 append_glyph_string_lists (head, tail, s, s);
20593 /* Get face and two-byte form of character C in face FACE_ID on frame
20594 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20595 means we want to display multibyte text. DISPLAY_P non-zero means
20596 make sure that X resources for the face returned are allocated.
20597 Value is a pointer to a realized face that is ready for display if
20598 DISPLAY_P is non-zero. */
20600 static INLINE struct face *
20601 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20602 XChar2b *char2b, int multibyte_p, int display_p)
20604 struct face *face = FACE_FROM_ID (f, face_id);
20606 if (face->font)
20608 unsigned code = face->font->driver->encode_char (face->font, c);
20610 if (code != FONT_INVALID_CODE)
20611 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20612 else
20613 STORE_XCHAR2B (char2b, 0, 0);
20616 /* Make sure X resources of the face are allocated. */
20617 #ifdef HAVE_X_WINDOWS
20618 if (display_p)
20619 #endif
20621 xassert (face != NULL);
20622 PREPARE_FACE_FOR_DISPLAY (f, face);
20625 return face;
20629 /* Get face and two-byte form of character glyph GLYPH on frame F.
20630 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20631 a pointer to a realized face that is ready for display. */
20633 static INLINE struct face *
20634 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20635 XChar2b *char2b, int *two_byte_p)
20637 struct face *face;
20639 xassert (glyph->type == CHAR_GLYPH);
20640 face = FACE_FROM_ID (f, glyph->face_id);
20642 if (two_byte_p)
20643 *two_byte_p = 0;
20645 if (face->font)
20647 unsigned code;
20649 if (CHAR_BYTE8_P (glyph->u.ch))
20650 code = CHAR_TO_BYTE8 (glyph->u.ch);
20651 else
20652 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20654 if (code != FONT_INVALID_CODE)
20655 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20656 else
20657 STORE_XCHAR2B (char2b, 0, 0);
20660 /* Make sure X resources of the face are allocated. */
20661 xassert (face != NULL);
20662 PREPARE_FACE_FOR_DISPLAY (f, face);
20663 return face;
20667 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20668 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20670 static INLINE int
20671 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20673 unsigned code;
20675 if (CHAR_BYTE8_P (c))
20676 code = CHAR_TO_BYTE8 (c);
20677 else
20678 code = font->driver->encode_char (font, c);
20680 if (code == FONT_INVALID_CODE)
20681 return 0;
20682 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20683 return 1;
20687 /* Fill glyph string S with composition components specified by S->cmp.
20689 BASE_FACE is the base face of the composition.
20690 S->cmp_from is the index of the first component for S.
20692 OVERLAPS non-zero means S should draw the foreground only, and use
20693 its physical height for clipping. See also draw_glyphs.
20695 Value is the index of a component not in S. */
20697 static int
20698 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20699 int overlaps)
20701 int i;
20702 /* For all glyphs of this composition, starting at the offset
20703 S->cmp_from, until we reach the end of the definition or encounter a
20704 glyph that requires the different face, add it to S. */
20705 struct face *face;
20707 xassert (s);
20709 s->for_overlaps = overlaps;
20710 s->face = NULL;
20711 s->font = NULL;
20712 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20714 int c = COMPOSITION_GLYPH (s->cmp, i);
20716 if (c != '\t')
20718 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20719 -1, Qnil);
20721 face = get_char_face_and_encoding (s->f, c, face_id,
20722 s->char2b + i, 1, 1);
20723 if (face)
20725 if (! s->face)
20727 s->face = face;
20728 s->font = s->face->font;
20730 else if (s->face != face)
20731 break;
20734 ++s->nchars;
20736 s->cmp_to = i;
20738 /* All glyph strings for the same composition has the same width,
20739 i.e. the width set for the first component of the composition. */
20740 s->width = s->first_glyph->pixel_width;
20742 /* If the specified font could not be loaded, use the frame's
20743 default font, but record the fact that we couldn't load it in
20744 the glyph string so that we can draw rectangles for the
20745 characters of the glyph string. */
20746 if (s->font == NULL)
20748 s->font_not_found_p = 1;
20749 s->font = FRAME_FONT (s->f);
20752 /* Adjust base line for subscript/superscript text. */
20753 s->ybase += s->first_glyph->voffset;
20755 /* This glyph string must always be drawn with 16-bit functions. */
20756 s->two_byte_p = 1;
20758 return s->cmp_to;
20761 static int
20762 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20763 int start, int end, int overlaps)
20765 struct glyph *glyph, *last;
20766 Lisp_Object lgstring;
20767 int i;
20769 s->for_overlaps = overlaps;
20770 glyph = s->row->glyphs[s->area] + start;
20771 last = s->row->glyphs[s->area] + end;
20772 s->cmp_id = glyph->u.cmp.id;
20773 s->cmp_from = glyph->slice.cmp.from;
20774 s->cmp_to = glyph->slice.cmp.to + 1;
20775 s->face = FACE_FROM_ID (s->f, face_id);
20776 lgstring = composition_gstring_from_id (s->cmp_id);
20777 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20778 glyph++;
20779 while (glyph < last
20780 && glyph->u.cmp.automatic
20781 && glyph->u.cmp.id == s->cmp_id
20782 && s->cmp_to == glyph->slice.cmp.from)
20783 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20785 for (i = s->cmp_from; i < s->cmp_to; i++)
20787 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20788 unsigned code = LGLYPH_CODE (lglyph);
20790 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20792 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20793 return glyph - s->row->glyphs[s->area];
20797 /* Fill glyph string S from a sequence glyphs for glyphless characters.
20798 See the comment of fill_glyph_string for arguments.
20799 Value is the index of the first glyph not in S. */
20802 static int
20803 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
20804 int start, int end, int overlaps)
20806 struct glyph *glyph, *last;
20807 int voffset;
20809 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
20810 s->for_overlaps = overlaps;
20811 glyph = s->row->glyphs[s->area] + start;
20812 last = s->row->glyphs[s->area] + end;
20813 voffset = glyph->voffset;
20814 s->face = FACE_FROM_ID (s->f, face_id);
20815 s->font = s->face->font;
20816 s->nchars = 1;
20817 s->width = glyph->pixel_width;
20818 glyph++;
20819 while (glyph < last
20820 && glyph->type == GLYPHLESS_GLYPH
20821 && glyph->voffset == voffset
20822 && glyph->face_id == face_id)
20824 s->nchars++;
20825 s->width += glyph->pixel_width;
20826 glyph++;
20828 s->ybase += voffset;
20829 return glyph - s->row->glyphs[s->area];
20833 /* Fill glyph string S from a sequence of character glyphs.
20835 FACE_ID is the face id of the string. START is the index of the
20836 first glyph to consider, END is the index of the last + 1.
20837 OVERLAPS non-zero means S should draw the foreground only, and use
20838 its physical height for clipping. See also draw_glyphs.
20840 Value is the index of the first glyph not in S. */
20842 static int
20843 fill_glyph_string (struct glyph_string *s, int face_id,
20844 int start, int end, int overlaps)
20846 struct glyph *glyph, *last;
20847 int voffset;
20848 int glyph_not_available_p;
20850 xassert (s->f == XFRAME (s->w->frame));
20851 xassert (s->nchars == 0);
20852 xassert (start >= 0 && end > start);
20854 s->for_overlaps = overlaps;
20855 glyph = s->row->glyphs[s->area] + start;
20856 last = s->row->glyphs[s->area] + end;
20857 voffset = glyph->voffset;
20858 s->padding_p = glyph->padding_p;
20859 glyph_not_available_p = glyph->glyph_not_available_p;
20861 while (glyph < last
20862 && glyph->type == CHAR_GLYPH
20863 && glyph->voffset == voffset
20864 /* Same face id implies same font, nowadays. */
20865 && glyph->face_id == face_id
20866 && glyph->glyph_not_available_p == glyph_not_available_p)
20868 int two_byte_p;
20870 s->face = get_glyph_face_and_encoding (s->f, glyph,
20871 s->char2b + s->nchars,
20872 &two_byte_p);
20873 s->two_byte_p = two_byte_p;
20874 ++s->nchars;
20875 xassert (s->nchars <= end - start);
20876 s->width += glyph->pixel_width;
20877 if (glyph++->padding_p != s->padding_p)
20878 break;
20881 s->font = s->face->font;
20883 /* If the specified font could not be loaded, use the frame's font,
20884 but record the fact that we couldn't load it in
20885 S->font_not_found_p so that we can draw rectangles for the
20886 characters of the glyph string. */
20887 if (s->font == NULL || glyph_not_available_p)
20889 s->font_not_found_p = 1;
20890 s->font = FRAME_FONT (s->f);
20893 /* Adjust base line for subscript/superscript text. */
20894 s->ybase += voffset;
20896 xassert (s->face && s->face->gc);
20897 return glyph - s->row->glyphs[s->area];
20901 /* Fill glyph string S from image glyph S->first_glyph. */
20903 static void
20904 fill_image_glyph_string (struct glyph_string *s)
20906 xassert (s->first_glyph->type == IMAGE_GLYPH);
20907 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20908 xassert (s->img);
20909 s->slice = s->first_glyph->slice.img;
20910 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20911 s->font = s->face->font;
20912 s->width = s->first_glyph->pixel_width;
20914 /* Adjust base line for subscript/superscript text. */
20915 s->ybase += s->first_glyph->voffset;
20919 /* Fill glyph string S from a sequence of stretch glyphs.
20921 ROW is the glyph row in which the glyphs are found, AREA is the
20922 area within the row. START is the index of the first glyph to
20923 consider, END is the index of the last + 1.
20925 Value is the index of the first glyph not in S. */
20927 static int
20928 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20929 enum glyph_row_area area, int start, int end)
20931 struct glyph *glyph, *last;
20932 int voffset, face_id;
20934 xassert (s->first_glyph->type == STRETCH_GLYPH);
20936 glyph = s->row->glyphs[s->area] + start;
20937 last = s->row->glyphs[s->area] + end;
20938 face_id = glyph->face_id;
20939 s->face = FACE_FROM_ID (s->f, face_id);
20940 s->font = s->face->font;
20941 s->width = glyph->pixel_width;
20942 s->nchars = 1;
20943 voffset = glyph->voffset;
20945 for (++glyph;
20946 (glyph < last
20947 && glyph->type == STRETCH_GLYPH
20948 && glyph->voffset == voffset
20949 && glyph->face_id == face_id);
20950 ++glyph)
20951 s->width += glyph->pixel_width;
20953 /* Adjust base line for subscript/superscript text. */
20954 s->ybase += voffset;
20956 /* The case that face->gc == 0 is handled when drawing the glyph
20957 string by calling PREPARE_FACE_FOR_DISPLAY. */
20958 xassert (s->face);
20959 return glyph - s->row->glyphs[s->area];
20962 static struct font_metrics *
20963 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20965 static struct font_metrics metrics;
20966 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20968 if (! font || code == FONT_INVALID_CODE)
20969 return NULL;
20970 font->driver->text_extents (font, &code, 1, &metrics);
20971 return &metrics;
20974 /* EXPORT for RIF:
20975 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20976 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20977 assumed to be zero. */
20979 void
20980 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20982 *left = *right = 0;
20984 if (glyph->type == CHAR_GLYPH)
20986 struct face *face;
20987 XChar2b char2b;
20988 struct font_metrics *pcm;
20990 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20991 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20993 if (pcm->rbearing > pcm->width)
20994 *right = pcm->rbearing - pcm->width;
20995 if (pcm->lbearing < 0)
20996 *left = -pcm->lbearing;
20999 else if (glyph->type == COMPOSITE_GLYPH)
21001 if (! glyph->u.cmp.automatic)
21003 struct composition *cmp = composition_table[glyph->u.cmp.id];
21005 if (cmp->rbearing > cmp->pixel_width)
21006 *right = cmp->rbearing - cmp->pixel_width;
21007 if (cmp->lbearing < 0)
21008 *left = - cmp->lbearing;
21010 else
21012 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21013 struct font_metrics metrics;
21015 composition_gstring_width (gstring, glyph->slice.cmp.from,
21016 glyph->slice.cmp.to + 1, &metrics);
21017 if (metrics.rbearing > metrics.width)
21018 *right = metrics.rbearing - metrics.width;
21019 if (metrics.lbearing < 0)
21020 *left = - metrics.lbearing;
21026 /* Return the index of the first glyph preceding glyph string S that
21027 is overwritten by S because of S's left overhang. Value is -1
21028 if no glyphs are overwritten. */
21030 static int
21031 left_overwritten (struct glyph_string *s)
21033 int k;
21035 if (s->left_overhang)
21037 int x = 0, i;
21038 struct glyph *glyphs = s->row->glyphs[s->area];
21039 int first = s->first_glyph - glyphs;
21041 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21042 x -= glyphs[i].pixel_width;
21044 k = i + 1;
21046 else
21047 k = -1;
21049 return k;
21053 /* Return the index of the first glyph preceding glyph string S that
21054 is overwriting S because of its right overhang. Value is -1 if no
21055 glyph in front of S overwrites S. */
21057 static int
21058 left_overwriting (struct glyph_string *s)
21060 int i, k, x;
21061 struct glyph *glyphs = s->row->glyphs[s->area];
21062 int first = s->first_glyph - glyphs;
21064 k = -1;
21065 x = 0;
21066 for (i = first - 1; i >= 0; --i)
21068 int left, right;
21069 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21070 if (x + right > 0)
21071 k = i;
21072 x -= glyphs[i].pixel_width;
21075 return k;
21079 /* Return the index of the last glyph following glyph string S that is
21080 overwritten by S because of S's right overhang. Value is -1 if
21081 no such glyph is found. */
21083 static int
21084 right_overwritten (struct glyph_string *s)
21086 int k = -1;
21088 if (s->right_overhang)
21090 int x = 0, i;
21091 struct glyph *glyphs = s->row->glyphs[s->area];
21092 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21093 int end = s->row->used[s->area];
21095 for (i = first; i < end && s->right_overhang > x; ++i)
21096 x += glyphs[i].pixel_width;
21098 k = i;
21101 return k;
21105 /* Return the index of the last glyph following glyph string S that
21106 overwrites S because of its left overhang. Value is negative
21107 if no such glyph is found. */
21109 static int
21110 right_overwriting (struct glyph_string *s)
21112 int i, k, x;
21113 int end = s->row->used[s->area];
21114 struct glyph *glyphs = s->row->glyphs[s->area];
21115 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21117 k = -1;
21118 x = 0;
21119 for (i = first; i < end; ++i)
21121 int left, right;
21122 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21123 if (x - left < 0)
21124 k = i;
21125 x += glyphs[i].pixel_width;
21128 return k;
21132 /* Set background width of glyph string S. START is the index of the
21133 first glyph following S. LAST_X is the right-most x-position + 1
21134 in the drawing area. */
21136 static INLINE void
21137 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21139 /* If the face of this glyph string has to be drawn to the end of
21140 the drawing area, set S->extends_to_end_of_line_p. */
21142 if (start == s->row->used[s->area]
21143 && s->area == TEXT_AREA
21144 && ((s->row->fill_line_p
21145 && (s->hl == DRAW_NORMAL_TEXT
21146 || s->hl == DRAW_IMAGE_RAISED
21147 || s->hl == DRAW_IMAGE_SUNKEN))
21148 || s->hl == DRAW_MOUSE_FACE))
21149 s->extends_to_end_of_line_p = 1;
21151 /* If S extends its face to the end of the line, set its
21152 background_width to the distance to the right edge of the drawing
21153 area. */
21154 if (s->extends_to_end_of_line_p)
21155 s->background_width = last_x - s->x + 1;
21156 else
21157 s->background_width = s->width;
21161 /* Compute overhangs and x-positions for glyph string S and its
21162 predecessors, or successors. X is the starting x-position for S.
21163 BACKWARD_P non-zero means process predecessors. */
21165 static void
21166 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21168 if (backward_p)
21170 while (s)
21172 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21173 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21174 x -= s->width;
21175 s->x = x;
21176 s = s->prev;
21179 else
21181 while (s)
21183 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21184 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21185 s->x = x;
21186 x += s->width;
21187 s = s->next;
21194 /* The following macros are only called from draw_glyphs below.
21195 They reference the following parameters of that function directly:
21196 `w', `row', `area', and `overlap_p'
21197 as well as the following local variables:
21198 `s', `f', and `hdc' (in W32) */
21200 #ifdef HAVE_NTGUI
21201 /* On W32, silently add local `hdc' variable to argument list of
21202 init_glyph_string. */
21203 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21204 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21205 #else
21206 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21207 init_glyph_string (s, char2b, w, row, area, start, hl)
21208 #endif
21210 /* Add a glyph string for a stretch glyph to the list of strings
21211 between HEAD and TAIL. START is the index of the stretch glyph in
21212 row area AREA of glyph row ROW. END is the index of the last glyph
21213 in that glyph row area. X is the current output position assigned
21214 to the new glyph string constructed. HL overrides that face of the
21215 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21216 is the right-most x-position of the drawing area. */
21218 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21219 and below -- keep them on one line. */
21220 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21221 do \
21223 s = (struct glyph_string *) alloca (sizeof *s); \
21224 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21225 START = fill_stretch_glyph_string (s, row, area, START, END); \
21226 append_glyph_string (&HEAD, &TAIL, s); \
21227 s->x = (X); \
21229 while (0)
21232 /* Add a glyph string for an image glyph to the list of strings
21233 between HEAD and TAIL. START is the index of the image glyph in
21234 row area AREA of glyph row ROW. END is the index of the last glyph
21235 in that glyph row area. X is the current output position assigned
21236 to the new glyph string constructed. HL overrides that face of the
21237 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21238 is the right-most x-position of the drawing area. */
21240 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21241 do \
21243 s = (struct glyph_string *) alloca (sizeof *s); \
21244 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21245 fill_image_glyph_string (s); \
21246 append_glyph_string (&HEAD, &TAIL, s); \
21247 ++START; \
21248 s->x = (X); \
21250 while (0)
21253 /* Add a glyph string for a sequence of character glyphs to the list
21254 of strings between HEAD and TAIL. START is the index of the first
21255 glyph in row area AREA of glyph row ROW that is part of the new
21256 glyph string. END is the index of the last glyph in that glyph row
21257 area. X is the current output position assigned to the new glyph
21258 string constructed. HL overrides that face of the glyph; e.g. it
21259 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21260 right-most x-position of the drawing area. */
21262 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21263 do \
21265 int face_id; \
21266 XChar2b *char2b; \
21268 face_id = (row)->glyphs[area][START].face_id; \
21270 s = (struct glyph_string *) alloca (sizeof *s); \
21271 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21272 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21273 append_glyph_string (&HEAD, &TAIL, s); \
21274 s->x = (X); \
21275 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21277 while (0)
21280 /* Add a glyph string for a composite sequence to the list of strings
21281 between HEAD and TAIL. START is the index of the first glyph in
21282 row area AREA of glyph row ROW that is part of the new glyph
21283 string. END is the index of the last glyph in that glyph row area.
21284 X is the current output position assigned to the new glyph string
21285 constructed. HL overrides that face of the glyph; e.g. it is
21286 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21287 x-position of the drawing area. */
21289 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21290 do { \
21291 int face_id = (row)->glyphs[area][START].face_id; \
21292 struct face *base_face = FACE_FROM_ID (f, face_id); \
21293 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21294 struct composition *cmp = composition_table[cmp_id]; \
21295 XChar2b *char2b; \
21296 struct glyph_string *first_s; \
21297 int n; \
21299 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21301 /* Make glyph_strings for each glyph sequence that is drawable by \
21302 the same face, and append them to HEAD/TAIL. */ \
21303 for (n = 0; n < cmp->glyph_len;) \
21305 s = (struct glyph_string *) alloca (sizeof *s); \
21306 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21307 append_glyph_string (&(HEAD), &(TAIL), s); \
21308 s->cmp = cmp; \
21309 s->cmp_from = n; \
21310 s->x = (X); \
21311 if (n == 0) \
21312 first_s = s; \
21313 n = fill_composite_glyph_string (s, base_face, overlaps); \
21316 ++START; \
21317 s = first_s; \
21318 } while (0)
21321 /* Add a glyph string for a glyph-string sequence to the list of strings
21322 between HEAD and TAIL. */
21324 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21325 do { \
21326 int face_id; \
21327 XChar2b *char2b; \
21328 Lisp_Object gstring; \
21330 face_id = (row)->glyphs[area][START].face_id; \
21331 gstring = (composition_gstring_from_id \
21332 ((row)->glyphs[area][START].u.cmp.id)); \
21333 s = (struct glyph_string *) alloca (sizeof *s); \
21334 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21335 * LGSTRING_GLYPH_LEN (gstring)); \
21336 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21337 append_glyph_string (&(HEAD), &(TAIL), s); \
21338 s->x = (X); \
21339 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21340 } while (0)
21343 /* Add a glyph string for a sequence of glyphless character's glyphs
21344 to the list of strings between HEAD and TAIL. The meanings of
21345 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21347 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21348 do \
21350 int face_id; \
21351 XChar2b *char2b; \
21353 face_id = (row)->glyphs[area][START].face_id; \
21355 s = (struct glyph_string *) alloca (sizeof *s); \
21356 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21357 append_glyph_string (&HEAD, &TAIL, s); \
21358 s->x = (X); \
21359 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21360 overlaps); \
21362 while (0)
21365 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21366 of AREA of glyph row ROW on window W between indices START and END.
21367 HL overrides the face for drawing glyph strings, e.g. it is
21368 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21369 x-positions of the drawing area.
21371 This is an ugly monster macro construct because we must use alloca
21372 to allocate glyph strings (because draw_glyphs can be called
21373 asynchronously). */
21375 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21376 do \
21378 HEAD = TAIL = NULL; \
21379 while (START < END) \
21381 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21382 switch (first_glyph->type) \
21384 case CHAR_GLYPH: \
21385 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21386 HL, X, LAST_X); \
21387 break; \
21389 case COMPOSITE_GLYPH: \
21390 if (first_glyph->u.cmp.automatic) \
21391 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21392 HL, X, LAST_X); \
21393 else \
21394 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21395 HL, X, LAST_X); \
21396 break; \
21398 case STRETCH_GLYPH: \
21399 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21400 HL, X, LAST_X); \
21401 break; \
21403 case IMAGE_GLYPH: \
21404 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21405 HL, X, LAST_X); \
21406 break; \
21408 case GLYPHLESS_GLYPH: \
21409 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21410 HL, X, LAST_X); \
21411 break; \
21413 default: \
21414 abort (); \
21417 if (s) \
21419 set_glyph_string_background_width (s, START, LAST_X); \
21420 (X) += s->width; \
21423 } while (0)
21426 /* Draw glyphs between START and END in AREA of ROW on window W,
21427 starting at x-position X. X is relative to AREA in W. HL is a
21428 face-override with the following meaning:
21430 DRAW_NORMAL_TEXT draw normally
21431 DRAW_CURSOR draw in cursor face
21432 DRAW_MOUSE_FACE draw in mouse face.
21433 DRAW_INVERSE_VIDEO draw in mode line face
21434 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21435 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21437 If OVERLAPS is non-zero, draw only the foreground of characters and
21438 clip to the physical height of ROW. Non-zero value also defines
21439 the overlapping part to be drawn:
21441 OVERLAPS_PRED overlap with preceding rows
21442 OVERLAPS_SUCC overlap with succeeding rows
21443 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21444 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21446 Value is the x-position reached, relative to AREA of W. */
21448 static int
21449 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21450 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21451 enum draw_glyphs_face hl, int overlaps)
21453 struct glyph_string *head, *tail;
21454 struct glyph_string *s;
21455 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21456 int i, j, x_reached, last_x, area_left = 0;
21457 struct frame *f = XFRAME (WINDOW_FRAME (w));
21458 DECLARE_HDC (hdc);
21460 ALLOCATE_HDC (hdc, f);
21462 /* Let's rather be paranoid than getting a SEGV. */
21463 end = min (end, row->used[area]);
21464 start = max (0, start);
21465 start = min (end, start);
21467 /* Translate X to frame coordinates. Set last_x to the right
21468 end of the drawing area. */
21469 if (row->full_width_p)
21471 /* X is relative to the left edge of W, without scroll bars
21472 or fringes. */
21473 area_left = WINDOW_LEFT_EDGE_X (w);
21474 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21476 else
21478 area_left = window_box_left (w, area);
21479 last_x = area_left + window_box_width (w, area);
21481 x += area_left;
21483 /* Build a doubly-linked list of glyph_string structures between
21484 head and tail from what we have to draw. Note that the macro
21485 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21486 the reason we use a separate variable `i'. */
21487 i = start;
21488 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21489 if (tail)
21490 x_reached = tail->x + tail->background_width;
21491 else
21492 x_reached = x;
21494 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21495 the row, redraw some glyphs in front or following the glyph
21496 strings built above. */
21497 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21499 struct glyph_string *h, *t;
21500 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21501 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21502 int dummy_x = 0;
21504 /* If mouse highlighting is on, we may need to draw adjacent
21505 glyphs using mouse-face highlighting. */
21506 if (area == TEXT_AREA && row->mouse_face_p)
21508 struct glyph_row *mouse_beg_row, *mouse_end_row;
21510 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21511 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21513 if (row >= mouse_beg_row && row <= mouse_end_row)
21515 check_mouse_face = 1;
21516 mouse_beg_col = (row == mouse_beg_row)
21517 ? hlinfo->mouse_face_beg_col : 0;
21518 mouse_end_col = (row == mouse_end_row)
21519 ? hlinfo->mouse_face_end_col
21520 : row->used[TEXT_AREA];
21524 /* Compute overhangs for all glyph strings. */
21525 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21526 for (s = head; s; s = s->next)
21527 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21529 /* Prepend glyph strings for glyphs in front of the first glyph
21530 string that are overwritten because of the first glyph
21531 string's left overhang. The background of all strings
21532 prepended must be drawn because the first glyph string
21533 draws over it. */
21534 i = left_overwritten (head);
21535 if (i >= 0)
21537 enum draw_glyphs_face overlap_hl;
21539 /* If this row contains mouse highlighting, attempt to draw
21540 the overlapped glyphs with the correct highlight. This
21541 code fails if the overlap encompasses more than one glyph
21542 and mouse-highlight spans only some of these glyphs.
21543 However, making it work perfectly involves a lot more
21544 code, and I don't know if the pathological case occurs in
21545 practice, so we'll stick to this for now. --- cyd */
21546 if (check_mouse_face
21547 && mouse_beg_col < start && mouse_end_col > i)
21548 overlap_hl = DRAW_MOUSE_FACE;
21549 else
21550 overlap_hl = DRAW_NORMAL_TEXT;
21552 j = i;
21553 BUILD_GLYPH_STRINGS (j, start, h, t,
21554 overlap_hl, dummy_x, last_x);
21555 start = i;
21556 compute_overhangs_and_x (t, head->x, 1);
21557 prepend_glyph_string_lists (&head, &tail, h, t);
21558 clip_head = head;
21561 /* Prepend glyph strings for glyphs in front of the first glyph
21562 string that overwrite that glyph string because of their
21563 right overhang. For these strings, only the foreground must
21564 be drawn, because it draws over the glyph string at `head'.
21565 The background must not be drawn because this would overwrite
21566 right overhangs of preceding glyphs for which no glyph
21567 strings exist. */
21568 i = left_overwriting (head);
21569 if (i >= 0)
21571 enum draw_glyphs_face overlap_hl;
21573 if (check_mouse_face
21574 && mouse_beg_col < start && mouse_end_col > i)
21575 overlap_hl = DRAW_MOUSE_FACE;
21576 else
21577 overlap_hl = DRAW_NORMAL_TEXT;
21579 clip_head = head;
21580 BUILD_GLYPH_STRINGS (i, start, h, t,
21581 overlap_hl, dummy_x, last_x);
21582 for (s = h; s; s = s->next)
21583 s->background_filled_p = 1;
21584 compute_overhangs_and_x (t, head->x, 1);
21585 prepend_glyph_string_lists (&head, &tail, h, t);
21588 /* Append glyphs strings for glyphs following the last glyph
21589 string tail that are overwritten by tail. The background of
21590 these strings has to be drawn because tail's foreground draws
21591 over it. */
21592 i = right_overwritten (tail);
21593 if (i >= 0)
21595 enum draw_glyphs_face overlap_hl;
21597 if (check_mouse_face
21598 && mouse_beg_col < i && mouse_end_col > end)
21599 overlap_hl = DRAW_MOUSE_FACE;
21600 else
21601 overlap_hl = DRAW_NORMAL_TEXT;
21603 BUILD_GLYPH_STRINGS (end, i, h, t,
21604 overlap_hl, x, last_x);
21605 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21606 we don't have `end = i;' here. */
21607 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21608 append_glyph_string_lists (&head, &tail, h, t);
21609 clip_tail = tail;
21612 /* Append glyph strings for glyphs following the last glyph
21613 string tail that overwrite tail. The foreground of such
21614 glyphs has to be drawn because it writes into the background
21615 of tail. The background must not be drawn because it could
21616 paint over the foreground of following glyphs. */
21617 i = right_overwriting (tail);
21618 if (i >= 0)
21620 enum draw_glyphs_face overlap_hl;
21621 if (check_mouse_face
21622 && mouse_beg_col < i && mouse_end_col > end)
21623 overlap_hl = DRAW_MOUSE_FACE;
21624 else
21625 overlap_hl = DRAW_NORMAL_TEXT;
21627 clip_tail = tail;
21628 i++; /* We must include the Ith glyph. */
21629 BUILD_GLYPH_STRINGS (end, i, h, t,
21630 overlap_hl, x, last_x);
21631 for (s = h; s; s = s->next)
21632 s->background_filled_p = 1;
21633 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21634 append_glyph_string_lists (&head, &tail, h, t);
21636 if (clip_head || clip_tail)
21637 for (s = head; s; s = s->next)
21639 s->clip_head = clip_head;
21640 s->clip_tail = clip_tail;
21644 /* Draw all strings. */
21645 for (s = head; s; s = s->next)
21646 FRAME_RIF (f)->draw_glyph_string (s);
21648 #ifndef HAVE_NS
21649 /* When focus a sole frame and move horizontally, this sets on_p to 0
21650 causing a failure to erase prev cursor position. */
21651 if (area == TEXT_AREA
21652 && !row->full_width_p
21653 /* When drawing overlapping rows, only the glyph strings'
21654 foreground is drawn, which doesn't erase a cursor
21655 completely. */
21656 && !overlaps)
21658 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21659 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21660 : (tail ? tail->x + tail->background_width : x));
21661 x0 -= area_left;
21662 x1 -= area_left;
21664 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21665 row->y, MATRIX_ROW_BOTTOM_Y (row));
21667 #endif
21669 /* Value is the x-position up to which drawn, relative to AREA of W.
21670 This doesn't include parts drawn because of overhangs. */
21671 if (row->full_width_p)
21672 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21673 else
21674 x_reached -= area_left;
21676 RELEASE_HDC (hdc, f);
21678 return x_reached;
21681 /* Expand row matrix if too narrow. Don't expand if area
21682 is not present. */
21684 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21686 if (!fonts_changed_p \
21687 && (it->glyph_row->glyphs[area] \
21688 < it->glyph_row->glyphs[area + 1])) \
21690 it->w->ncols_scale_factor++; \
21691 fonts_changed_p = 1; \
21695 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21696 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21698 static INLINE void
21699 append_glyph (struct it *it)
21701 struct glyph *glyph;
21702 enum glyph_row_area area = it->area;
21704 xassert (it->glyph_row);
21705 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21707 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21708 if (glyph < it->glyph_row->glyphs[area + 1])
21710 /* If the glyph row is reversed, we need to prepend the glyph
21711 rather than append it. */
21712 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21714 struct glyph *g;
21716 /* Make room for the additional glyph. */
21717 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21718 g[1] = *g;
21719 glyph = it->glyph_row->glyphs[area];
21721 glyph->charpos = CHARPOS (it->position);
21722 glyph->object = it->object;
21723 if (it->pixel_width > 0)
21725 glyph->pixel_width = it->pixel_width;
21726 glyph->padding_p = 0;
21728 else
21730 /* Assure at least 1-pixel width. Otherwise, cursor can't
21731 be displayed correctly. */
21732 glyph->pixel_width = 1;
21733 glyph->padding_p = 1;
21735 glyph->ascent = it->ascent;
21736 glyph->descent = it->descent;
21737 glyph->voffset = it->voffset;
21738 glyph->type = CHAR_GLYPH;
21739 glyph->avoid_cursor_p = it->avoid_cursor_p;
21740 glyph->multibyte_p = it->multibyte_p;
21741 glyph->left_box_line_p = it->start_of_box_run_p;
21742 glyph->right_box_line_p = it->end_of_box_run_p;
21743 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21744 || it->phys_descent > it->descent);
21745 glyph->glyph_not_available_p = it->glyph_not_available_p;
21746 glyph->face_id = it->face_id;
21747 glyph->u.ch = it->char_to_display;
21748 glyph->slice.img = null_glyph_slice;
21749 glyph->font_type = FONT_TYPE_UNKNOWN;
21750 if (it->bidi_p)
21752 glyph->resolved_level = it->bidi_it.resolved_level;
21753 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21754 abort ();
21755 glyph->bidi_type = it->bidi_it.type;
21757 else
21759 glyph->resolved_level = 0;
21760 glyph->bidi_type = UNKNOWN_BT;
21762 ++it->glyph_row->used[area];
21764 else
21765 IT_EXPAND_MATRIX_WIDTH (it, area);
21768 /* Store one glyph for the composition IT->cmp_it.id in
21769 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21770 non-null. */
21772 static INLINE void
21773 append_composite_glyph (struct it *it)
21775 struct glyph *glyph;
21776 enum glyph_row_area area = it->area;
21778 xassert (it->glyph_row);
21780 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21781 if (glyph < it->glyph_row->glyphs[area + 1])
21783 /* If the glyph row is reversed, we need to prepend the glyph
21784 rather than append it. */
21785 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21787 struct glyph *g;
21789 /* Make room for the new glyph. */
21790 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21791 g[1] = *g;
21792 glyph = it->glyph_row->glyphs[it->area];
21794 glyph->charpos = it->cmp_it.charpos;
21795 glyph->object = it->object;
21796 glyph->pixel_width = it->pixel_width;
21797 glyph->ascent = it->ascent;
21798 glyph->descent = it->descent;
21799 glyph->voffset = it->voffset;
21800 glyph->type = COMPOSITE_GLYPH;
21801 if (it->cmp_it.ch < 0)
21803 glyph->u.cmp.automatic = 0;
21804 glyph->u.cmp.id = it->cmp_it.id;
21805 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21807 else
21809 glyph->u.cmp.automatic = 1;
21810 glyph->u.cmp.id = it->cmp_it.id;
21811 glyph->slice.cmp.from = it->cmp_it.from;
21812 glyph->slice.cmp.to = it->cmp_it.to - 1;
21814 glyph->avoid_cursor_p = it->avoid_cursor_p;
21815 glyph->multibyte_p = it->multibyte_p;
21816 glyph->left_box_line_p = it->start_of_box_run_p;
21817 glyph->right_box_line_p = it->end_of_box_run_p;
21818 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21819 || it->phys_descent > it->descent);
21820 glyph->padding_p = 0;
21821 glyph->glyph_not_available_p = 0;
21822 glyph->face_id = it->face_id;
21823 glyph->font_type = FONT_TYPE_UNKNOWN;
21824 if (it->bidi_p)
21826 glyph->resolved_level = it->bidi_it.resolved_level;
21827 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21828 abort ();
21829 glyph->bidi_type = it->bidi_it.type;
21831 ++it->glyph_row->used[area];
21833 else
21834 IT_EXPAND_MATRIX_WIDTH (it, area);
21838 /* Change IT->ascent and IT->height according to the setting of
21839 IT->voffset. */
21841 static INLINE void
21842 take_vertical_position_into_account (struct it *it)
21844 if (it->voffset)
21846 if (it->voffset < 0)
21847 /* Increase the ascent so that we can display the text higher
21848 in the line. */
21849 it->ascent -= it->voffset;
21850 else
21851 /* Increase the descent so that we can display the text lower
21852 in the line. */
21853 it->descent += it->voffset;
21858 /* Produce glyphs/get display metrics for the image IT is loaded with.
21859 See the description of struct display_iterator in dispextern.h for
21860 an overview of struct display_iterator. */
21862 static void
21863 produce_image_glyph (struct it *it)
21865 struct image *img;
21866 struct face *face;
21867 int glyph_ascent, crop;
21868 struct glyph_slice slice;
21870 xassert (it->what == IT_IMAGE);
21872 face = FACE_FROM_ID (it->f, it->face_id);
21873 xassert (face);
21874 /* Make sure X resources of the face is loaded. */
21875 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21877 if (it->image_id < 0)
21879 /* Fringe bitmap. */
21880 it->ascent = it->phys_ascent = 0;
21881 it->descent = it->phys_descent = 0;
21882 it->pixel_width = 0;
21883 it->nglyphs = 0;
21884 return;
21887 img = IMAGE_FROM_ID (it->f, it->image_id);
21888 xassert (img);
21889 /* Make sure X resources of the image is loaded. */
21890 prepare_image_for_display (it->f, img);
21892 slice.x = slice.y = 0;
21893 slice.width = img->width;
21894 slice.height = img->height;
21896 if (INTEGERP (it->slice.x))
21897 slice.x = XINT (it->slice.x);
21898 else if (FLOATP (it->slice.x))
21899 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21901 if (INTEGERP (it->slice.y))
21902 slice.y = XINT (it->slice.y);
21903 else if (FLOATP (it->slice.y))
21904 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21906 if (INTEGERP (it->slice.width))
21907 slice.width = XINT (it->slice.width);
21908 else if (FLOATP (it->slice.width))
21909 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21911 if (INTEGERP (it->slice.height))
21912 slice.height = XINT (it->slice.height);
21913 else if (FLOATP (it->slice.height))
21914 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21916 if (slice.x >= img->width)
21917 slice.x = img->width;
21918 if (slice.y >= img->height)
21919 slice.y = img->height;
21920 if (slice.x + slice.width >= img->width)
21921 slice.width = img->width - slice.x;
21922 if (slice.y + slice.height > img->height)
21923 slice.height = img->height - slice.y;
21925 if (slice.width == 0 || slice.height == 0)
21926 return;
21928 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21930 it->descent = slice.height - glyph_ascent;
21931 if (slice.y == 0)
21932 it->descent += img->vmargin;
21933 if (slice.y + slice.height == img->height)
21934 it->descent += img->vmargin;
21935 it->phys_descent = it->descent;
21937 it->pixel_width = slice.width;
21938 if (slice.x == 0)
21939 it->pixel_width += img->hmargin;
21940 if (slice.x + slice.width == img->width)
21941 it->pixel_width += img->hmargin;
21943 /* It's quite possible for images to have an ascent greater than
21944 their height, so don't get confused in that case. */
21945 if (it->descent < 0)
21946 it->descent = 0;
21948 it->nglyphs = 1;
21950 if (face->box != FACE_NO_BOX)
21952 if (face->box_line_width > 0)
21954 if (slice.y == 0)
21955 it->ascent += face->box_line_width;
21956 if (slice.y + slice.height == img->height)
21957 it->descent += face->box_line_width;
21960 if (it->start_of_box_run_p && slice.x == 0)
21961 it->pixel_width += eabs (face->box_line_width);
21962 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21963 it->pixel_width += eabs (face->box_line_width);
21966 take_vertical_position_into_account (it);
21968 /* Automatically crop wide image glyphs at right edge so we can
21969 draw the cursor on same display row. */
21970 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21971 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21973 it->pixel_width -= crop;
21974 slice.width -= crop;
21977 if (it->glyph_row)
21979 struct glyph *glyph;
21980 enum glyph_row_area area = it->area;
21982 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21983 if (glyph < it->glyph_row->glyphs[area + 1])
21985 glyph->charpos = CHARPOS (it->position);
21986 glyph->object = it->object;
21987 glyph->pixel_width = it->pixel_width;
21988 glyph->ascent = glyph_ascent;
21989 glyph->descent = it->descent;
21990 glyph->voffset = it->voffset;
21991 glyph->type = IMAGE_GLYPH;
21992 glyph->avoid_cursor_p = it->avoid_cursor_p;
21993 glyph->multibyte_p = it->multibyte_p;
21994 glyph->left_box_line_p = it->start_of_box_run_p;
21995 glyph->right_box_line_p = it->end_of_box_run_p;
21996 glyph->overlaps_vertically_p = 0;
21997 glyph->padding_p = 0;
21998 glyph->glyph_not_available_p = 0;
21999 glyph->face_id = it->face_id;
22000 glyph->u.img_id = img->id;
22001 glyph->slice.img = slice;
22002 glyph->font_type = FONT_TYPE_UNKNOWN;
22003 if (it->bidi_p)
22005 glyph->resolved_level = it->bidi_it.resolved_level;
22006 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22007 abort ();
22008 glyph->bidi_type = it->bidi_it.type;
22010 ++it->glyph_row->used[area];
22012 else
22013 IT_EXPAND_MATRIX_WIDTH (it, area);
22018 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22019 of the glyph, WIDTH and HEIGHT are the width and height of the
22020 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22022 static void
22023 append_stretch_glyph (struct it *it, Lisp_Object object,
22024 int width, int height, int ascent)
22026 struct glyph *glyph;
22027 enum glyph_row_area area = it->area;
22029 xassert (ascent >= 0 && ascent <= height);
22031 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22032 if (glyph < it->glyph_row->glyphs[area + 1])
22034 /* If the glyph row is reversed, we need to prepend the glyph
22035 rather than append it. */
22036 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22038 struct glyph *g;
22040 /* Make room for the additional glyph. */
22041 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22042 g[1] = *g;
22043 glyph = it->glyph_row->glyphs[area];
22045 glyph->charpos = CHARPOS (it->position);
22046 glyph->object = object;
22047 glyph->pixel_width = width;
22048 glyph->ascent = ascent;
22049 glyph->descent = height - ascent;
22050 glyph->voffset = it->voffset;
22051 glyph->type = STRETCH_GLYPH;
22052 glyph->avoid_cursor_p = it->avoid_cursor_p;
22053 glyph->multibyte_p = it->multibyte_p;
22054 glyph->left_box_line_p = it->start_of_box_run_p;
22055 glyph->right_box_line_p = it->end_of_box_run_p;
22056 glyph->overlaps_vertically_p = 0;
22057 glyph->padding_p = 0;
22058 glyph->glyph_not_available_p = 0;
22059 glyph->face_id = it->face_id;
22060 glyph->u.stretch.ascent = ascent;
22061 glyph->u.stretch.height = height;
22062 glyph->slice.img = null_glyph_slice;
22063 glyph->font_type = FONT_TYPE_UNKNOWN;
22064 if (it->bidi_p)
22066 glyph->resolved_level = it->bidi_it.resolved_level;
22067 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22068 abort ();
22069 glyph->bidi_type = it->bidi_it.type;
22071 else
22073 glyph->resolved_level = 0;
22074 glyph->bidi_type = UNKNOWN_BT;
22076 ++it->glyph_row->used[area];
22078 else
22079 IT_EXPAND_MATRIX_WIDTH (it, area);
22083 /* Produce a stretch glyph for iterator IT. IT->object is the value
22084 of the glyph property displayed. The value must be a list
22085 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22086 being recognized:
22088 1. `:width WIDTH' specifies that the space should be WIDTH *
22089 canonical char width wide. WIDTH may be an integer or floating
22090 point number.
22092 2. `:relative-width FACTOR' specifies that the width of the stretch
22093 should be computed from the width of the first character having the
22094 `glyph' property, and should be FACTOR times that width.
22096 3. `:align-to HPOS' specifies that the space should be wide enough
22097 to reach HPOS, a value in canonical character units.
22099 Exactly one of the above pairs must be present.
22101 4. `:height HEIGHT' specifies that the height of the stretch produced
22102 should be HEIGHT, measured in canonical character units.
22104 5. `:relative-height FACTOR' specifies that the height of the
22105 stretch should be FACTOR times the height of the characters having
22106 the glyph property.
22108 Either none or exactly one of 4 or 5 must be present.
22110 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22111 of the stretch should be used for the ascent of the stretch.
22112 ASCENT must be in the range 0 <= ASCENT <= 100. */
22114 static void
22115 produce_stretch_glyph (struct it *it)
22117 /* (space :width WIDTH :height HEIGHT ...) */
22118 Lisp_Object prop, plist;
22119 int width = 0, height = 0, align_to = -1;
22120 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22121 int ascent = 0;
22122 double tem;
22123 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22124 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22126 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22128 /* List should start with `space'. */
22129 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22130 plist = XCDR (it->object);
22132 /* Compute the width of the stretch. */
22133 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22134 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22136 /* Absolute width `:width WIDTH' specified and valid. */
22137 zero_width_ok_p = 1;
22138 width = (int)tem;
22140 else if (prop = Fplist_get (plist, QCrelative_width),
22141 NUMVAL (prop) > 0)
22143 /* Relative width `:relative-width FACTOR' specified and valid.
22144 Compute the width of the characters having the `glyph'
22145 property. */
22146 struct it it2;
22147 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22149 it2 = *it;
22150 if (it->multibyte_p)
22151 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22152 else
22154 it2.c = it2.char_to_display = *p, it2.len = 1;
22155 if (! ASCII_CHAR_P (it2.c))
22156 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22159 it2.glyph_row = NULL;
22160 it2.what = IT_CHARACTER;
22161 x_produce_glyphs (&it2);
22162 width = NUMVAL (prop) * it2.pixel_width;
22164 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22165 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22167 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22168 align_to = (align_to < 0
22170 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22171 else if (align_to < 0)
22172 align_to = window_box_left_offset (it->w, TEXT_AREA);
22173 width = max (0, (int)tem + align_to - it->current_x);
22174 zero_width_ok_p = 1;
22176 else
22177 /* Nothing specified -> width defaults to canonical char width. */
22178 width = FRAME_COLUMN_WIDTH (it->f);
22180 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22181 width = 1;
22183 /* Compute height. */
22184 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22185 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22187 height = (int)tem;
22188 zero_height_ok_p = 1;
22190 else if (prop = Fplist_get (plist, QCrelative_height),
22191 NUMVAL (prop) > 0)
22192 height = FONT_HEIGHT (font) * NUMVAL (prop);
22193 else
22194 height = FONT_HEIGHT (font);
22196 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22197 height = 1;
22199 /* Compute percentage of height used for ascent. If
22200 `:ascent ASCENT' is present and valid, use that. Otherwise,
22201 derive the ascent from the font in use. */
22202 if (prop = Fplist_get (plist, QCascent),
22203 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22204 ascent = height * NUMVAL (prop) / 100.0;
22205 else if (!NILP (prop)
22206 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22207 ascent = min (max (0, (int)tem), height);
22208 else
22209 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22211 if (width > 0 && it->line_wrap != TRUNCATE
22212 && it->current_x + width > it->last_visible_x)
22213 width = it->last_visible_x - it->current_x - 1;
22215 if (width > 0 && height > 0 && it->glyph_row)
22217 Lisp_Object object = it->stack[it->sp - 1].string;
22218 if (!STRINGP (object))
22219 object = it->w->buffer;
22220 append_stretch_glyph (it, object, width, height, ascent);
22223 it->pixel_width = width;
22224 it->ascent = it->phys_ascent = ascent;
22225 it->descent = it->phys_descent = height - it->ascent;
22226 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22228 take_vertical_position_into_account (it);
22231 /* Calculate line-height and line-spacing properties.
22232 An integer value specifies explicit pixel value.
22233 A float value specifies relative value to current face height.
22234 A cons (float . face-name) specifies relative value to
22235 height of specified face font.
22237 Returns height in pixels, or nil. */
22240 static Lisp_Object
22241 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22242 int boff, int override)
22244 Lisp_Object face_name = Qnil;
22245 int ascent, descent, height;
22247 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22248 return val;
22250 if (CONSP (val))
22252 face_name = XCAR (val);
22253 val = XCDR (val);
22254 if (!NUMBERP (val))
22255 val = make_number (1);
22256 if (NILP (face_name))
22258 height = it->ascent + it->descent;
22259 goto scale;
22263 if (NILP (face_name))
22265 font = FRAME_FONT (it->f);
22266 boff = FRAME_BASELINE_OFFSET (it->f);
22268 else if (EQ (face_name, Qt))
22270 override = 0;
22272 else
22274 int face_id;
22275 struct face *face;
22277 face_id = lookup_named_face (it->f, face_name, 0);
22278 if (face_id < 0)
22279 return make_number (-1);
22281 face = FACE_FROM_ID (it->f, face_id);
22282 font = face->font;
22283 if (font == NULL)
22284 return make_number (-1);
22285 boff = font->baseline_offset;
22286 if (font->vertical_centering)
22287 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22290 ascent = FONT_BASE (font) + boff;
22291 descent = FONT_DESCENT (font) - boff;
22293 if (override)
22295 it->override_ascent = ascent;
22296 it->override_descent = descent;
22297 it->override_boff = boff;
22300 height = ascent + descent;
22302 scale:
22303 if (FLOATP (val))
22304 height = (int)(XFLOAT_DATA (val) * height);
22305 else if (INTEGERP (val))
22306 height *= XINT (val);
22308 return make_number (height);
22312 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22313 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22314 and only if this is for a character for which no font was found.
22316 If the display method (it->glyphless_method) is
22317 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22318 length of the acronym or the hexadecimal string, UPPER_XOFF and
22319 UPPER_YOFF are pixel offsets for the upper part of the string,
22320 LOWER_XOFF and LOWER_YOFF are for the lower part.
22322 For the other display methods, LEN through LOWER_YOFF are zero. */
22324 static void
22325 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22326 short upper_xoff, short upper_yoff,
22327 short lower_xoff, short lower_yoff)
22329 struct glyph *glyph;
22330 enum glyph_row_area area = it->area;
22332 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22333 if (glyph < it->glyph_row->glyphs[area + 1])
22335 /* If the glyph row is reversed, we need to prepend the glyph
22336 rather than append it. */
22337 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22339 struct glyph *g;
22341 /* Make room for the additional glyph. */
22342 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22343 g[1] = *g;
22344 glyph = it->glyph_row->glyphs[area];
22346 glyph->charpos = CHARPOS (it->position);
22347 glyph->object = it->object;
22348 glyph->pixel_width = it->pixel_width;
22349 glyph->ascent = it->ascent;
22350 glyph->descent = it->descent;
22351 glyph->voffset = it->voffset;
22352 glyph->type = GLYPHLESS_GLYPH;
22353 glyph->u.glyphless.method = it->glyphless_method;
22354 glyph->u.glyphless.for_no_font = for_no_font;
22355 glyph->u.glyphless.len = len;
22356 glyph->u.glyphless.ch = it->c;
22357 glyph->slice.glyphless.upper_xoff = upper_xoff;
22358 glyph->slice.glyphless.upper_yoff = upper_yoff;
22359 glyph->slice.glyphless.lower_xoff = lower_xoff;
22360 glyph->slice.glyphless.lower_yoff = lower_yoff;
22361 glyph->avoid_cursor_p = it->avoid_cursor_p;
22362 glyph->multibyte_p = it->multibyte_p;
22363 glyph->left_box_line_p = it->start_of_box_run_p;
22364 glyph->right_box_line_p = it->end_of_box_run_p;
22365 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22366 || it->phys_descent > it->descent);
22367 glyph->padding_p = 0;
22368 glyph->glyph_not_available_p = 0;
22369 glyph->face_id = face_id;
22370 glyph->font_type = FONT_TYPE_UNKNOWN;
22371 if (it->bidi_p)
22373 glyph->resolved_level = it->bidi_it.resolved_level;
22374 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22375 abort ();
22376 glyph->bidi_type = it->bidi_it.type;
22378 ++it->glyph_row->used[area];
22380 else
22381 IT_EXPAND_MATRIX_WIDTH (it, area);
22385 /* Produce a glyph for a glyphless character for iterator IT.
22386 IT->glyphless_method specifies which method to use for displaying
22387 the character. See the description of enum
22388 glyphless_display_method in dispextern.h for the detail.
22390 FOR_NO_FONT is nonzero if and only if this is for a character for
22391 which no font was found. ACRONYM, if non-nil, is an acronym string
22392 for the character. */
22394 static void
22395 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22397 int face_id;
22398 struct face *face;
22399 struct font *font;
22400 int base_width, base_height, width, height;
22401 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22402 int len;
22404 /* Get the metrics of the base font. We always refer to the current
22405 ASCII face. */
22406 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22407 font = face->font ? face->font : FRAME_FONT (it->f);
22408 it->ascent = FONT_BASE (font) + font->baseline_offset;
22409 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22410 base_height = it->ascent + it->descent;
22411 base_width = font->average_width;
22413 /* Get a face ID for the glyph by utilizing a cache (the same way as
22414 doen for `escape-glyph' in get_next_display_element). */
22415 if (it->f == last_glyphless_glyph_frame
22416 && it->face_id == last_glyphless_glyph_face_id)
22418 face_id = last_glyphless_glyph_merged_face_id;
22420 else
22422 /* Merge the `glyphless-char' face into the current face. */
22423 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22424 last_glyphless_glyph_frame = it->f;
22425 last_glyphless_glyph_face_id = it->face_id;
22426 last_glyphless_glyph_merged_face_id = face_id;
22429 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22431 it->pixel_width = THIN_SPACE_WIDTH;
22432 len = 0;
22433 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22435 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
22437 width = CHAR_WIDTH (it->c);
22438 if (width == 0)
22439 width = 1;
22440 else if (width > 4)
22441 width = 4;
22442 it->pixel_width = base_width * width;
22443 len = 0;
22444 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
22446 else
22448 char buf[7], *str;
22449 unsigned int code[6];
22450 int upper_len;
22451 int ascent, descent;
22452 struct font_metrics metrics_upper, metrics_lower;
22454 face = FACE_FROM_ID (it->f, face_id);
22455 font = face->font ? face->font : FRAME_FONT (it->f);
22456 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22458 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
22460 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
22461 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
22462 str = STRINGP (acronym) ? (char *) SDATA (acronym) : "";
22464 else
22466 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
22467 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
22468 str = buf;
22470 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
22471 code[len] = font->driver->encode_char (font, str[len]);
22472 upper_len = (len + 1) / 2;
22473 font->driver->text_extents (font, code, upper_len,
22474 &metrics_upper);
22475 font->driver->text_extents (font, code + upper_len, len - upper_len,
22476 &metrics_lower);
22480 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
22481 width = max (metrics_upper.width, metrics_lower.width) + 4;
22482 upper_xoff = upper_yoff = 2; /* the typical case */
22483 if (base_width >= width)
22485 /* Align the upper to the left, the lower to the right. */
22486 it->pixel_width = base_width;
22487 lower_xoff = base_width - 2 - metrics_lower.width;
22489 else
22491 /* Center the shorter one. */
22492 it->pixel_width = width;
22493 if (metrics_upper.width >= metrics_lower.width)
22494 lower_xoff = (width - metrics_lower.width) / 2;
22495 else
22496 upper_xoff = (width - metrics_upper.width) / 2;
22499 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
22500 top, bottom, and between upper and lower strings. */
22501 height = (metrics_upper.ascent + metrics_upper.descent
22502 + metrics_lower.ascent + metrics_lower.descent) + 5;
22503 /* Center vertically.
22504 H:base_height, D:base_descent
22505 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
22507 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
22508 descent = D - H/2 + h/2;
22509 lower_yoff = descent - 2 - ld;
22510 upper_yoff = lower_yoff - la - 1 - ud; */
22511 ascent = - (it->descent - (base_height + height + 1) / 2);
22512 descent = it->descent - (base_height - height) / 2;
22513 lower_yoff = descent - 2 - metrics_lower.descent;
22514 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
22515 - metrics_upper.descent);
22516 /* Don't make the height shorter than the base height. */
22517 if (height > base_height)
22519 it->ascent = ascent;
22520 it->descent = descent;
22524 it->phys_ascent = it->ascent;
22525 it->phys_descent = it->descent;
22526 if (it->glyph_row)
22527 append_glyphless_glyph (it, face_id, for_no_font, len,
22528 upper_xoff, upper_yoff,
22529 lower_xoff, lower_yoff);
22530 it->nglyphs = 1;
22531 take_vertical_position_into_account (it);
22535 /* RIF:
22536 Produce glyphs/get display metrics for the display element IT is
22537 loaded with. See the description of struct it in dispextern.h
22538 for an overview of struct it. */
22540 void
22541 x_produce_glyphs (struct it *it)
22543 int extra_line_spacing = it->extra_line_spacing;
22545 it->glyph_not_available_p = 0;
22547 if (it->what == IT_CHARACTER)
22549 XChar2b char2b;
22550 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22551 struct font *font = face->font;
22552 struct font_metrics *pcm = NULL;
22553 int boff; /* baseline offset */
22555 if (font == NULL)
22557 /* When no suitable font is found, display this character by
22558 the method specified in the first extra slot of
22559 Vglyphless_char_display. */
22560 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
22562 xassert (it->what == IT_GLYPHLESS);
22563 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
22564 goto done;
22567 boff = font->baseline_offset;
22568 if (font->vertical_centering)
22569 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22571 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22573 int stretched_p;
22575 it->nglyphs = 1;
22577 if (it->override_ascent >= 0)
22579 it->ascent = it->override_ascent;
22580 it->descent = it->override_descent;
22581 boff = it->override_boff;
22583 else
22585 it->ascent = FONT_BASE (font) + boff;
22586 it->descent = FONT_DESCENT (font) - boff;
22589 if (get_char_glyph_code (it->char_to_display, font, &char2b))
22591 pcm = get_per_char_metric (it->f, font, &char2b);
22592 if (pcm->width == 0
22593 && pcm->rbearing == 0 && pcm->lbearing == 0)
22594 pcm = NULL;
22597 if (pcm)
22599 it->phys_ascent = pcm->ascent + boff;
22600 it->phys_descent = pcm->descent - boff;
22601 it->pixel_width = pcm->width;
22603 else
22605 it->glyph_not_available_p = 1;
22606 it->phys_ascent = it->ascent;
22607 it->phys_descent = it->descent;
22608 it->pixel_width = font->space_width;
22611 if (it->constrain_row_ascent_descent_p)
22613 if (it->descent > it->max_descent)
22615 it->ascent += it->descent - it->max_descent;
22616 it->descent = it->max_descent;
22618 if (it->ascent > it->max_ascent)
22620 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22621 it->ascent = it->max_ascent;
22623 it->phys_ascent = min (it->phys_ascent, it->ascent);
22624 it->phys_descent = min (it->phys_descent, it->descent);
22625 extra_line_spacing = 0;
22628 /* If this is a space inside a region of text with
22629 `space-width' property, change its width. */
22630 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22631 if (stretched_p)
22632 it->pixel_width *= XFLOATINT (it->space_width);
22634 /* If face has a box, add the box thickness to the character
22635 height. If character has a box line to the left and/or
22636 right, add the box line width to the character's width. */
22637 if (face->box != FACE_NO_BOX)
22639 int thick = face->box_line_width;
22641 if (thick > 0)
22643 it->ascent += thick;
22644 it->descent += thick;
22646 else
22647 thick = -thick;
22649 if (it->start_of_box_run_p)
22650 it->pixel_width += thick;
22651 if (it->end_of_box_run_p)
22652 it->pixel_width += thick;
22655 /* If face has an overline, add the height of the overline
22656 (1 pixel) and a 1 pixel margin to the character height. */
22657 if (face->overline_p)
22658 it->ascent += overline_margin;
22660 if (it->constrain_row_ascent_descent_p)
22662 if (it->ascent > it->max_ascent)
22663 it->ascent = it->max_ascent;
22664 if (it->descent > it->max_descent)
22665 it->descent = it->max_descent;
22668 take_vertical_position_into_account (it);
22670 /* If we have to actually produce glyphs, do it. */
22671 if (it->glyph_row)
22673 if (stretched_p)
22675 /* Translate a space with a `space-width' property
22676 into a stretch glyph. */
22677 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22678 / FONT_HEIGHT (font));
22679 append_stretch_glyph (it, it->object, it->pixel_width,
22680 it->ascent + it->descent, ascent);
22682 else
22683 append_glyph (it);
22685 /* If characters with lbearing or rbearing are displayed
22686 in this line, record that fact in a flag of the
22687 glyph row. This is used to optimize X output code. */
22688 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22689 it->glyph_row->contains_overlapping_glyphs_p = 1;
22691 if (! stretched_p && it->pixel_width == 0)
22692 /* We assure that all visible glyphs have at least 1-pixel
22693 width. */
22694 it->pixel_width = 1;
22696 else if (it->char_to_display == '\n')
22698 /* A newline has no width, but we need the height of the
22699 line. But if previous part of the line sets a height,
22700 don't increase that height */
22702 Lisp_Object height;
22703 Lisp_Object total_height = Qnil;
22705 it->override_ascent = -1;
22706 it->pixel_width = 0;
22707 it->nglyphs = 0;
22709 height = get_it_property (it, Qline_height);
22710 /* Split (line-height total-height) list */
22711 if (CONSP (height)
22712 && CONSP (XCDR (height))
22713 && NILP (XCDR (XCDR (height))))
22715 total_height = XCAR (XCDR (height));
22716 height = XCAR (height);
22718 height = calc_line_height_property (it, height, font, boff, 1);
22720 if (it->override_ascent >= 0)
22722 it->ascent = it->override_ascent;
22723 it->descent = it->override_descent;
22724 boff = it->override_boff;
22726 else
22728 it->ascent = FONT_BASE (font) + boff;
22729 it->descent = FONT_DESCENT (font) - boff;
22732 if (EQ (height, Qt))
22734 if (it->descent > it->max_descent)
22736 it->ascent += it->descent - it->max_descent;
22737 it->descent = it->max_descent;
22739 if (it->ascent > it->max_ascent)
22741 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22742 it->ascent = it->max_ascent;
22744 it->phys_ascent = min (it->phys_ascent, it->ascent);
22745 it->phys_descent = min (it->phys_descent, it->descent);
22746 it->constrain_row_ascent_descent_p = 1;
22747 extra_line_spacing = 0;
22749 else
22751 Lisp_Object spacing;
22753 it->phys_ascent = it->ascent;
22754 it->phys_descent = it->descent;
22756 if ((it->max_ascent > 0 || it->max_descent > 0)
22757 && face->box != FACE_NO_BOX
22758 && face->box_line_width > 0)
22760 it->ascent += face->box_line_width;
22761 it->descent += face->box_line_width;
22763 if (!NILP (height)
22764 && XINT (height) > it->ascent + it->descent)
22765 it->ascent = XINT (height) - it->descent;
22767 if (!NILP (total_height))
22768 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22769 else
22771 spacing = get_it_property (it, Qline_spacing);
22772 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22774 if (INTEGERP (spacing))
22776 extra_line_spacing = XINT (spacing);
22777 if (!NILP (total_height))
22778 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22782 else /* i.e. (it->char_to_display == '\t') */
22784 if (font->space_width > 0)
22786 int tab_width = it->tab_width * font->space_width;
22787 int x = it->current_x + it->continuation_lines_width;
22788 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22790 /* If the distance from the current position to the next tab
22791 stop is less than a space character width, use the
22792 tab stop after that. */
22793 if (next_tab_x - x < font->space_width)
22794 next_tab_x += tab_width;
22796 it->pixel_width = next_tab_x - x;
22797 it->nglyphs = 1;
22798 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22799 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22801 if (it->glyph_row)
22803 append_stretch_glyph (it, it->object, it->pixel_width,
22804 it->ascent + it->descent, it->ascent);
22807 else
22809 it->pixel_width = 0;
22810 it->nglyphs = 1;
22814 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22816 /* A static composition.
22818 Note: A composition is represented as one glyph in the
22819 glyph matrix. There are no padding glyphs.
22821 Important note: pixel_width, ascent, and descent are the
22822 values of what is drawn by draw_glyphs (i.e. the values of
22823 the overall glyphs composed). */
22824 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22825 int boff; /* baseline offset */
22826 struct composition *cmp = composition_table[it->cmp_it.id];
22827 int glyph_len = cmp->glyph_len;
22828 struct font *font = face->font;
22830 it->nglyphs = 1;
22832 /* If we have not yet calculated pixel size data of glyphs of
22833 the composition for the current face font, calculate them
22834 now. Theoretically, we have to check all fonts for the
22835 glyphs, but that requires much time and memory space. So,
22836 here we check only the font of the first glyph. This may
22837 lead to incorrect display, but it's very rare, and C-l
22838 (recenter-top-bottom) can correct the display anyway. */
22839 if (! cmp->font || cmp->font != font)
22841 /* Ascent and descent of the font of the first character
22842 of this composition (adjusted by baseline offset).
22843 Ascent and descent of overall glyphs should not be less
22844 than these, respectively. */
22845 int font_ascent, font_descent, font_height;
22846 /* Bounding box of the overall glyphs. */
22847 int leftmost, rightmost, lowest, highest;
22848 int lbearing, rbearing;
22849 int i, width, ascent, descent;
22850 int left_padded = 0, right_padded = 0;
22851 int c;
22852 XChar2b char2b;
22853 struct font_metrics *pcm;
22854 int font_not_found_p;
22855 EMACS_INT pos;
22857 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22858 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22859 break;
22860 if (glyph_len < cmp->glyph_len)
22861 right_padded = 1;
22862 for (i = 0; i < glyph_len; i++)
22864 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22865 break;
22866 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22868 if (i > 0)
22869 left_padded = 1;
22871 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22872 : IT_CHARPOS (*it));
22873 /* If no suitable font is found, use the default font. */
22874 font_not_found_p = font == NULL;
22875 if (font_not_found_p)
22877 face = face->ascii_face;
22878 font = face->font;
22880 boff = font->baseline_offset;
22881 if (font->vertical_centering)
22882 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22883 font_ascent = FONT_BASE (font) + boff;
22884 font_descent = FONT_DESCENT (font) - boff;
22885 font_height = FONT_HEIGHT (font);
22887 cmp->font = (void *) font;
22889 pcm = NULL;
22890 if (! font_not_found_p)
22892 get_char_face_and_encoding (it->f, c, it->face_id,
22893 &char2b, it->multibyte_p, 0);
22894 pcm = get_per_char_metric (it->f, font, &char2b);
22897 /* Initialize the bounding box. */
22898 if (pcm)
22900 width = pcm->width;
22901 ascent = pcm->ascent;
22902 descent = pcm->descent;
22903 lbearing = pcm->lbearing;
22904 rbearing = pcm->rbearing;
22906 else
22908 width = font->space_width;
22909 ascent = FONT_BASE (font);
22910 descent = FONT_DESCENT (font);
22911 lbearing = 0;
22912 rbearing = width;
22915 rightmost = width;
22916 leftmost = 0;
22917 lowest = - descent + boff;
22918 highest = ascent + boff;
22920 if (! font_not_found_p
22921 && font->default_ascent
22922 && CHAR_TABLE_P (Vuse_default_ascent)
22923 && !NILP (Faref (Vuse_default_ascent,
22924 make_number (it->char_to_display))))
22925 highest = font->default_ascent + boff;
22927 /* Draw the first glyph at the normal position. It may be
22928 shifted to right later if some other glyphs are drawn
22929 at the left. */
22930 cmp->offsets[i * 2] = 0;
22931 cmp->offsets[i * 2 + 1] = boff;
22932 cmp->lbearing = lbearing;
22933 cmp->rbearing = rbearing;
22935 /* Set cmp->offsets for the remaining glyphs. */
22936 for (i++; i < glyph_len; i++)
22938 int left, right, btm, top;
22939 int ch = COMPOSITION_GLYPH (cmp, i);
22940 int face_id;
22941 struct face *this_face;
22942 int this_boff;
22944 if (ch == '\t')
22945 ch = ' ';
22946 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22947 this_face = FACE_FROM_ID (it->f, face_id);
22948 font = this_face->font;
22950 if (font == NULL)
22951 pcm = NULL;
22952 else
22954 this_boff = font->baseline_offset;
22955 if (font->vertical_centering)
22956 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22957 get_char_face_and_encoding (it->f, ch, face_id,
22958 &char2b, it->multibyte_p, 0);
22959 pcm = get_per_char_metric (it->f, font, &char2b);
22961 if (! pcm)
22962 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22963 else
22965 width = pcm->width;
22966 ascent = pcm->ascent;
22967 descent = pcm->descent;
22968 lbearing = pcm->lbearing;
22969 rbearing = pcm->rbearing;
22970 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22972 /* Relative composition with or without
22973 alternate chars. */
22974 left = (leftmost + rightmost - width) / 2;
22975 btm = - descent + boff;
22976 if (font->relative_compose
22977 && (! CHAR_TABLE_P (Vignore_relative_composition)
22978 || NILP (Faref (Vignore_relative_composition,
22979 make_number (ch)))))
22982 if (- descent >= font->relative_compose)
22983 /* One extra pixel between two glyphs. */
22984 btm = highest + 1;
22985 else if (ascent <= 0)
22986 /* One extra pixel between two glyphs. */
22987 btm = lowest - 1 - ascent - descent;
22990 else
22992 /* A composition rule is specified by an integer
22993 value that encodes global and new reference
22994 points (GREF and NREF). GREF and NREF are
22995 specified by numbers as below:
22997 0---1---2 -- ascent
23001 9--10--11 -- center
23003 ---3---4---5--- baseline
23005 6---7---8 -- descent
23007 int rule = COMPOSITION_RULE (cmp, i);
23008 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23010 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23011 grefx = gref % 3, nrefx = nref % 3;
23012 grefy = gref / 3, nrefy = nref / 3;
23013 if (xoff)
23014 xoff = font_height * (xoff - 128) / 256;
23015 if (yoff)
23016 yoff = font_height * (yoff - 128) / 256;
23018 left = (leftmost
23019 + grefx * (rightmost - leftmost) / 2
23020 - nrefx * width / 2
23021 + xoff);
23023 btm = ((grefy == 0 ? highest
23024 : grefy == 1 ? 0
23025 : grefy == 2 ? lowest
23026 : (highest + lowest) / 2)
23027 - (nrefy == 0 ? ascent + descent
23028 : nrefy == 1 ? descent - boff
23029 : nrefy == 2 ? 0
23030 : (ascent + descent) / 2)
23031 + yoff);
23034 cmp->offsets[i * 2] = left;
23035 cmp->offsets[i * 2 + 1] = btm + descent;
23037 /* Update the bounding box of the overall glyphs. */
23038 if (width > 0)
23040 right = left + width;
23041 if (left < leftmost)
23042 leftmost = left;
23043 if (right > rightmost)
23044 rightmost = right;
23046 top = btm + descent + ascent;
23047 if (top > highest)
23048 highest = top;
23049 if (btm < lowest)
23050 lowest = btm;
23052 if (cmp->lbearing > left + lbearing)
23053 cmp->lbearing = left + lbearing;
23054 if (cmp->rbearing < left + rbearing)
23055 cmp->rbearing = left + rbearing;
23059 /* If there are glyphs whose x-offsets are negative,
23060 shift all glyphs to the right and make all x-offsets
23061 non-negative. */
23062 if (leftmost < 0)
23064 for (i = 0; i < cmp->glyph_len; i++)
23065 cmp->offsets[i * 2] -= leftmost;
23066 rightmost -= leftmost;
23067 cmp->lbearing -= leftmost;
23068 cmp->rbearing -= leftmost;
23071 if (left_padded && cmp->lbearing < 0)
23073 for (i = 0; i < cmp->glyph_len; i++)
23074 cmp->offsets[i * 2] -= cmp->lbearing;
23075 rightmost -= cmp->lbearing;
23076 cmp->rbearing -= cmp->lbearing;
23077 cmp->lbearing = 0;
23079 if (right_padded && rightmost < cmp->rbearing)
23081 rightmost = cmp->rbearing;
23084 cmp->pixel_width = rightmost;
23085 cmp->ascent = highest;
23086 cmp->descent = - lowest;
23087 if (cmp->ascent < font_ascent)
23088 cmp->ascent = font_ascent;
23089 if (cmp->descent < font_descent)
23090 cmp->descent = font_descent;
23093 if (it->glyph_row
23094 && (cmp->lbearing < 0
23095 || cmp->rbearing > cmp->pixel_width))
23096 it->glyph_row->contains_overlapping_glyphs_p = 1;
23098 it->pixel_width = cmp->pixel_width;
23099 it->ascent = it->phys_ascent = cmp->ascent;
23100 it->descent = it->phys_descent = cmp->descent;
23101 if (face->box != FACE_NO_BOX)
23103 int thick = face->box_line_width;
23105 if (thick > 0)
23107 it->ascent += thick;
23108 it->descent += thick;
23110 else
23111 thick = - thick;
23113 if (it->start_of_box_run_p)
23114 it->pixel_width += thick;
23115 if (it->end_of_box_run_p)
23116 it->pixel_width += thick;
23119 /* If face has an overline, add the height of the overline
23120 (1 pixel) and a 1 pixel margin to the character height. */
23121 if (face->overline_p)
23122 it->ascent += overline_margin;
23124 take_vertical_position_into_account (it);
23125 if (it->ascent < 0)
23126 it->ascent = 0;
23127 if (it->descent < 0)
23128 it->descent = 0;
23130 if (it->glyph_row)
23131 append_composite_glyph (it);
23133 else if (it->what == IT_COMPOSITION)
23135 /* A dynamic (automatic) composition. */
23136 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23137 Lisp_Object gstring;
23138 struct font_metrics metrics;
23140 gstring = composition_gstring_from_id (it->cmp_it.id);
23141 it->pixel_width
23142 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23143 &metrics);
23144 if (it->glyph_row
23145 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23146 it->glyph_row->contains_overlapping_glyphs_p = 1;
23147 it->ascent = it->phys_ascent = metrics.ascent;
23148 it->descent = it->phys_descent = metrics.descent;
23149 if (face->box != FACE_NO_BOX)
23151 int thick = face->box_line_width;
23153 if (thick > 0)
23155 it->ascent += thick;
23156 it->descent += thick;
23158 else
23159 thick = - thick;
23161 if (it->start_of_box_run_p)
23162 it->pixel_width += thick;
23163 if (it->end_of_box_run_p)
23164 it->pixel_width += thick;
23166 /* If face has an overline, add the height of the overline
23167 (1 pixel) and a 1 pixel margin to the character height. */
23168 if (face->overline_p)
23169 it->ascent += overline_margin;
23170 take_vertical_position_into_account (it);
23171 if (it->ascent < 0)
23172 it->ascent = 0;
23173 if (it->descent < 0)
23174 it->descent = 0;
23176 if (it->glyph_row)
23177 append_composite_glyph (it);
23179 else if (it->what == IT_GLYPHLESS)
23180 produce_glyphless_glyph (it, 0, Qnil);
23181 else if (it->what == IT_IMAGE)
23182 produce_image_glyph (it);
23183 else if (it->what == IT_STRETCH)
23184 produce_stretch_glyph (it);
23186 done:
23187 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23188 because this isn't true for images with `:ascent 100'. */
23189 xassert (it->ascent >= 0 && it->descent >= 0);
23190 if (it->area == TEXT_AREA)
23191 it->current_x += it->pixel_width;
23193 if (extra_line_spacing > 0)
23195 it->descent += extra_line_spacing;
23196 if (extra_line_spacing > it->max_extra_line_spacing)
23197 it->max_extra_line_spacing = extra_line_spacing;
23200 it->max_ascent = max (it->max_ascent, it->ascent);
23201 it->max_descent = max (it->max_descent, it->descent);
23202 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23203 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23206 /* EXPORT for RIF:
23207 Output LEN glyphs starting at START at the nominal cursor position.
23208 Advance the nominal cursor over the text. The global variable
23209 updated_window contains the window being updated, updated_row is
23210 the glyph row being updated, and updated_area is the area of that
23211 row being updated. */
23213 void
23214 x_write_glyphs (struct glyph *start, int len)
23216 int x, hpos;
23218 xassert (updated_window && updated_row);
23219 BLOCK_INPUT;
23221 /* Write glyphs. */
23223 hpos = start - updated_row->glyphs[updated_area];
23224 x = draw_glyphs (updated_window, output_cursor.x,
23225 updated_row, updated_area,
23226 hpos, hpos + len,
23227 DRAW_NORMAL_TEXT, 0);
23229 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23230 if (updated_area == TEXT_AREA
23231 && updated_window->phys_cursor_on_p
23232 && updated_window->phys_cursor.vpos == output_cursor.vpos
23233 && updated_window->phys_cursor.hpos >= hpos
23234 && updated_window->phys_cursor.hpos < hpos + len)
23235 updated_window->phys_cursor_on_p = 0;
23237 UNBLOCK_INPUT;
23239 /* Advance the output cursor. */
23240 output_cursor.hpos += len;
23241 output_cursor.x = x;
23245 /* EXPORT for RIF:
23246 Insert LEN glyphs from START at the nominal cursor position. */
23248 void
23249 x_insert_glyphs (struct glyph *start, int len)
23251 struct frame *f;
23252 struct window *w;
23253 int line_height, shift_by_width, shifted_region_width;
23254 struct glyph_row *row;
23255 struct glyph *glyph;
23256 int frame_x, frame_y;
23257 EMACS_INT hpos;
23259 xassert (updated_window && updated_row);
23260 BLOCK_INPUT;
23261 w = updated_window;
23262 f = XFRAME (WINDOW_FRAME (w));
23264 /* Get the height of the line we are in. */
23265 row = updated_row;
23266 line_height = row->height;
23268 /* Get the width of the glyphs to insert. */
23269 shift_by_width = 0;
23270 for (glyph = start; glyph < start + len; ++glyph)
23271 shift_by_width += glyph->pixel_width;
23273 /* Get the width of the region to shift right. */
23274 shifted_region_width = (window_box_width (w, updated_area)
23275 - output_cursor.x
23276 - shift_by_width);
23278 /* Shift right. */
23279 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23280 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23282 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23283 line_height, shift_by_width);
23285 /* Write the glyphs. */
23286 hpos = start - row->glyphs[updated_area];
23287 draw_glyphs (w, output_cursor.x, row, updated_area,
23288 hpos, hpos + len,
23289 DRAW_NORMAL_TEXT, 0);
23291 /* Advance the output cursor. */
23292 output_cursor.hpos += len;
23293 output_cursor.x += shift_by_width;
23294 UNBLOCK_INPUT;
23298 /* EXPORT for RIF:
23299 Erase the current text line from the nominal cursor position
23300 (inclusive) to pixel column TO_X (exclusive). The idea is that
23301 everything from TO_X onward is already erased.
23303 TO_X is a pixel position relative to updated_area of
23304 updated_window. TO_X == -1 means clear to the end of this area. */
23306 void
23307 x_clear_end_of_line (int to_x)
23309 struct frame *f;
23310 struct window *w = updated_window;
23311 int max_x, min_y, max_y;
23312 int from_x, from_y, to_y;
23314 xassert (updated_window && updated_row);
23315 f = XFRAME (w->frame);
23317 if (updated_row->full_width_p)
23318 max_x = WINDOW_TOTAL_WIDTH (w);
23319 else
23320 max_x = window_box_width (w, updated_area);
23321 max_y = window_text_bottom_y (w);
23323 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23324 of window. For TO_X > 0, truncate to end of drawing area. */
23325 if (to_x == 0)
23326 return;
23327 else if (to_x < 0)
23328 to_x = max_x;
23329 else
23330 to_x = min (to_x, max_x);
23332 to_y = min (max_y, output_cursor.y + updated_row->height);
23334 /* Notice if the cursor will be cleared by this operation. */
23335 if (!updated_row->full_width_p)
23336 notice_overwritten_cursor (w, updated_area,
23337 output_cursor.x, -1,
23338 updated_row->y,
23339 MATRIX_ROW_BOTTOM_Y (updated_row));
23341 from_x = output_cursor.x;
23343 /* Translate to frame coordinates. */
23344 if (updated_row->full_width_p)
23346 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23347 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23349 else
23351 int area_left = window_box_left (w, updated_area);
23352 from_x += area_left;
23353 to_x += area_left;
23356 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23357 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23358 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23360 /* Prevent inadvertently clearing to end of the X window. */
23361 if (to_x > from_x && to_y > from_y)
23363 BLOCK_INPUT;
23364 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23365 to_x - from_x, to_y - from_y);
23366 UNBLOCK_INPUT;
23370 #endif /* HAVE_WINDOW_SYSTEM */
23374 /***********************************************************************
23375 Cursor types
23376 ***********************************************************************/
23378 /* Value is the internal representation of the specified cursor type
23379 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23380 of the bar cursor. */
23382 static enum text_cursor_kinds
23383 get_specified_cursor_type (Lisp_Object arg, int *width)
23385 enum text_cursor_kinds type;
23387 if (NILP (arg))
23388 return NO_CURSOR;
23390 if (EQ (arg, Qbox))
23391 return FILLED_BOX_CURSOR;
23393 if (EQ (arg, Qhollow))
23394 return HOLLOW_BOX_CURSOR;
23396 if (EQ (arg, Qbar))
23398 *width = 2;
23399 return BAR_CURSOR;
23402 if (CONSP (arg)
23403 && EQ (XCAR (arg), Qbar)
23404 && INTEGERP (XCDR (arg))
23405 && XINT (XCDR (arg)) >= 0)
23407 *width = XINT (XCDR (arg));
23408 return BAR_CURSOR;
23411 if (EQ (arg, Qhbar))
23413 *width = 2;
23414 return HBAR_CURSOR;
23417 if (CONSP (arg)
23418 && EQ (XCAR (arg), Qhbar)
23419 && INTEGERP (XCDR (arg))
23420 && XINT (XCDR (arg)) >= 0)
23422 *width = XINT (XCDR (arg));
23423 return HBAR_CURSOR;
23426 /* Treat anything unknown as "hollow box cursor".
23427 It was bad to signal an error; people have trouble fixing
23428 .Xdefaults with Emacs, when it has something bad in it. */
23429 type = HOLLOW_BOX_CURSOR;
23431 return type;
23434 /* Set the default cursor types for specified frame. */
23435 void
23436 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23438 int width;
23439 Lisp_Object tem;
23441 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23442 FRAME_CURSOR_WIDTH (f) = width;
23444 /* By default, set up the blink-off state depending on the on-state. */
23446 tem = Fassoc (arg, Vblink_cursor_alist);
23447 if (!NILP (tem))
23449 FRAME_BLINK_OFF_CURSOR (f)
23450 = get_specified_cursor_type (XCDR (tem), &width);
23451 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23453 else
23454 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23458 #ifdef HAVE_WINDOW_SYSTEM
23460 /* Return the cursor we want to be displayed in window W. Return
23461 width of bar/hbar cursor through WIDTH arg. Return with
23462 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23463 (i.e. if the `system caret' should track this cursor).
23465 In a mini-buffer window, we want the cursor only to appear if we
23466 are reading input from this window. For the selected window, we
23467 want the cursor type given by the frame parameter or buffer local
23468 setting of cursor-type. If explicitly marked off, draw no cursor.
23469 In all other cases, we want a hollow box cursor. */
23471 static enum text_cursor_kinds
23472 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23473 int *active_cursor)
23475 struct frame *f = XFRAME (w->frame);
23476 struct buffer *b = XBUFFER (w->buffer);
23477 int cursor_type = DEFAULT_CURSOR;
23478 Lisp_Object alt_cursor;
23479 int non_selected = 0;
23481 *active_cursor = 1;
23483 /* Echo area */
23484 if (cursor_in_echo_area
23485 && FRAME_HAS_MINIBUF_P (f)
23486 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23488 if (w == XWINDOW (echo_area_window))
23490 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23492 *width = FRAME_CURSOR_WIDTH (f);
23493 return FRAME_DESIRED_CURSOR (f);
23495 else
23496 return get_specified_cursor_type (b->cursor_type, width);
23499 *active_cursor = 0;
23500 non_selected = 1;
23503 /* Detect a nonselected window or nonselected frame. */
23504 else if (w != XWINDOW (f->selected_window)
23505 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
23507 *active_cursor = 0;
23509 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23510 return NO_CURSOR;
23512 non_selected = 1;
23515 /* Never display a cursor in a window in which cursor-type is nil. */
23516 if (NILP (b->cursor_type))
23517 return NO_CURSOR;
23519 /* Get the normal cursor type for this window. */
23520 if (EQ (b->cursor_type, Qt))
23522 cursor_type = FRAME_DESIRED_CURSOR (f);
23523 *width = FRAME_CURSOR_WIDTH (f);
23525 else
23526 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23528 /* Use cursor-in-non-selected-windows instead
23529 for non-selected window or frame. */
23530 if (non_selected)
23532 alt_cursor = b->cursor_in_non_selected_windows;
23533 if (!EQ (Qt, alt_cursor))
23534 return get_specified_cursor_type (alt_cursor, width);
23535 /* t means modify the normal cursor type. */
23536 if (cursor_type == FILLED_BOX_CURSOR)
23537 cursor_type = HOLLOW_BOX_CURSOR;
23538 else if (cursor_type == BAR_CURSOR && *width > 1)
23539 --*width;
23540 return cursor_type;
23543 /* Use normal cursor if not blinked off. */
23544 if (!w->cursor_off_p)
23546 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23548 if (cursor_type == FILLED_BOX_CURSOR)
23550 /* Using a block cursor on large images can be very annoying.
23551 So use a hollow cursor for "large" images.
23552 If image is not transparent (no mask), also use hollow cursor. */
23553 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23554 if (img != NULL && IMAGEP (img->spec))
23556 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23557 where N = size of default frame font size.
23558 This should cover most of the "tiny" icons people may use. */
23559 if (!img->mask
23560 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23561 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23562 cursor_type = HOLLOW_BOX_CURSOR;
23565 else if (cursor_type != NO_CURSOR)
23567 /* Display current only supports BOX and HOLLOW cursors for images.
23568 So for now, unconditionally use a HOLLOW cursor when cursor is
23569 not a solid box cursor. */
23570 cursor_type = HOLLOW_BOX_CURSOR;
23573 return cursor_type;
23576 /* Cursor is blinked off, so determine how to "toggle" it. */
23578 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23579 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23580 return get_specified_cursor_type (XCDR (alt_cursor), width);
23582 /* Then see if frame has specified a specific blink off cursor type. */
23583 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23585 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23586 return FRAME_BLINK_OFF_CURSOR (f);
23589 #if 0
23590 /* Some people liked having a permanently visible blinking cursor,
23591 while others had very strong opinions against it. So it was
23592 decided to remove it. KFS 2003-09-03 */
23594 /* Finally perform built-in cursor blinking:
23595 filled box <-> hollow box
23596 wide [h]bar <-> narrow [h]bar
23597 narrow [h]bar <-> no cursor
23598 other type <-> no cursor */
23600 if (cursor_type == FILLED_BOX_CURSOR)
23601 return HOLLOW_BOX_CURSOR;
23603 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23605 *width = 1;
23606 return cursor_type;
23608 #endif
23610 return NO_CURSOR;
23614 /* Notice when the text cursor of window W has been completely
23615 overwritten by a drawing operation that outputs glyphs in AREA
23616 starting at X0 and ending at X1 in the line starting at Y0 and
23617 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23618 the rest of the line after X0 has been written. Y coordinates
23619 are window-relative. */
23621 static void
23622 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23623 int x0, int x1, int y0, int y1)
23625 int cx0, cx1, cy0, cy1;
23626 struct glyph_row *row;
23628 if (!w->phys_cursor_on_p)
23629 return;
23630 if (area != TEXT_AREA)
23631 return;
23633 if (w->phys_cursor.vpos < 0
23634 || w->phys_cursor.vpos >= w->current_matrix->nrows
23635 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23636 !(row->enabled_p && row->displays_text_p)))
23637 return;
23639 if (row->cursor_in_fringe_p)
23641 row->cursor_in_fringe_p = 0;
23642 draw_fringe_bitmap (w, row, row->reversed_p);
23643 w->phys_cursor_on_p = 0;
23644 return;
23647 cx0 = w->phys_cursor.x;
23648 cx1 = cx0 + w->phys_cursor_width;
23649 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23650 return;
23652 /* The cursor image will be completely removed from the
23653 screen if the output area intersects the cursor area in
23654 y-direction. When we draw in [y0 y1[, and some part of
23655 the cursor is at y < y0, that part must have been drawn
23656 before. When scrolling, the cursor is erased before
23657 actually scrolling, so we don't come here. When not
23658 scrolling, the rows above the old cursor row must have
23659 changed, and in this case these rows must have written
23660 over the cursor image.
23662 Likewise if part of the cursor is below y1, with the
23663 exception of the cursor being in the first blank row at
23664 the buffer and window end because update_text_area
23665 doesn't draw that row. (Except when it does, but
23666 that's handled in update_text_area.) */
23668 cy0 = w->phys_cursor.y;
23669 cy1 = cy0 + w->phys_cursor_height;
23670 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23671 return;
23673 w->phys_cursor_on_p = 0;
23676 #endif /* HAVE_WINDOW_SYSTEM */
23679 /************************************************************************
23680 Mouse Face
23681 ************************************************************************/
23683 #ifdef HAVE_WINDOW_SYSTEM
23685 /* EXPORT for RIF:
23686 Fix the display of area AREA of overlapping row ROW in window W
23687 with respect to the overlapping part OVERLAPS. */
23689 void
23690 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23691 enum glyph_row_area area, int overlaps)
23693 int i, x;
23695 BLOCK_INPUT;
23697 x = 0;
23698 for (i = 0; i < row->used[area];)
23700 if (row->glyphs[area][i].overlaps_vertically_p)
23702 int start = i, start_x = x;
23706 x += row->glyphs[area][i].pixel_width;
23707 ++i;
23709 while (i < row->used[area]
23710 && row->glyphs[area][i].overlaps_vertically_p);
23712 draw_glyphs (w, start_x, row, area,
23713 start, i,
23714 DRAW_NORMAL_TEXT, overlaps);
23716 else
23718 x += row->glyphs[area][i].pixel_width;
23719 ++i;
23723 UNBLOCK_INPUT;
23727 /* EXPORT:
23728 Draw the cursor glyph of window W in glyph row ROW. See the
23729 comment of draw_glyphs for the meaning of HL. */
23731 void
23732 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23733 enum draw_glyphs_face hl)
23735 /* If cursor hpos is out of bounds, don't draw garbage. This can
23736 happen in mini-buffer windows when switching between echo area
23737 glyphs and mini-buffer. */
23738 if ((row->reversed_p
23739 ? (w->phys_cursor.hpos >= 0)
23740 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23742 int on_p = w->phys_cursor_on_p;
23743 int x1;
23744 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23745 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23746 hl, 0);
23747 w->phys_cursor_on_p = on_p;
23749 if (hl == DRAW_CURSOR)
23750 w->phys_cursor_width = x1 - w->phys_cursor.x;
23751 /* When we erase the cursor, and ROW is overlapped by other
23752 rows, make sure that these overlapping parts of other rows
23753 are redrawn. */
23754 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23756 w->phys_cursor_width = x1 - w->phys_cursor.x;
23758 if (row > w->current_matrix->rows
23759 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23760 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23761 OVERLAPS_ERASED_CURSOR);
23763 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23764 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23765 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23766 OVERLAPS_ERASED_CURSOR);
23772 /* EXPORT:
23773 Erase the image of a cursor of window W from the screen. */
23775 void
23776 erase_phys_cursor (struct window *w)
23778 struct frame *f = XFRAME (w->frame);
23779 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23780 int hpos = w->phys_cursor.hpos;
23781 int vpos = w->phys_cursor.vpos;
23782 int mouse_face_here_p = 0;
23783 struct glyph_matrix *active_glyphs = w->current_matrix;
23784 struct glyph_row *cursor_row;
23785 struct glyph *cursor_glyph;
23786 enum draw_glyphs_face hl;
23788 /* No cursor displayed or row invalidated => nothing to do on the
23789 screen. */
23790 if (w->phys_cursor_type == NO_CURSOR)
23791 goto mark_cursor_off;
23793 /* VPOS >= active_glyphs->nrows means that window has been resized.
23794 Don't bother to erase the cursor. */
23795 if (vpos >= active_glyphs->nrows)
23796 goto mark_cursor_off;
23798 /* If row containing cursor is marked invalid, there is nothing we
23799 can do. */
23800 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23801 if (!cursor_row->enabled_p)
23802 goto mark_cursor_off;
23804 /* If line spacing is > 0, old cursor may only be partially visible in
23805 window after split-window. So adjust visible height. */
23806 cursor_row->visible_height = min (cursor_row->visible_height,
23807 window_text_bottom_y (w) - cursor_row->y);
23809 /* If row is completely invisible, don't attempt to delete a cursor which
23810 isn't there. This can happen if cursor is at top of a window, and
23811 we switch to a buffer with a header line in that window. */
23812 if (cursor_row->visible_height <= 0)
23813 goto mark_cursor_off;
23815 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23816 if (cursor_row->cursor_in_fringe_p)
23818 cursor_row->cursor_in_fringe_p = 0;
23819 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23820 goto mark_cursor_off;
23823 /* This can happen when the new row is shorter than the old one.
23824 In this case, either draw_glyphs or clear_end_of_line
23825 should have cleared the cursor. Note that we wouldn't be
23826 able to erase the cursor in this case because we don't have a
23827 cursor glyph at hand. */
23828 if ((cursor_row->reversed_p
23829 ? (w->phys_cursor.hpos < 0)
23830 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23831 goto mark_cursor_off;
23833 /* If the cursor is in the mouse face area, redisplay that when
23834 we clear the cursor. */
23835 if (! NILP (hlinfo->mouse_face_window)
23836 && coords_in_mouse_face_p (w, hpos, vpos)
23837 /* Don't redraw the cursor's spot in mouse face if it is at the
23838 end of a line (on a newline). The cursor appears there, but
23839 mouse highlighting does not. */
23840 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23841 mouse_face_here_p = 1;
23843 /* Maybe clear the display under the cursor. */
23844 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23846 int x, y, left_x;
23847 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23848 int width;
23850 cursor_glyph = get_phys_cursor_glyph (w);
23851 if (cursor_glyph == NULL)
23852 goto mark_cursor_off;
23854 width = cursor_glyph->pixel_width;
23855 left_x = window_box_left_offset (w, TEXT_AREA);
23856 x = w->phys_cursor.x;
23857 if (x < left_x)
23858 width -= left_x - x;
23859 width = min (width, window_box_width (w, TEXT_AREA) - x);
23860 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23861 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23863 if (width > 0)
23864 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23867 /* Erase the cursor by redrawing the character underneath it. */
23868 if (mouse_face_here_p)
23869 hl = DRAW_MOUSE_FACE;
23870 else
23871 hl = DRAW_NORMAL_TEXT;
23872 draw_phys_cursor_glyph (w, cursor_row, hl);
23874 mark_cursor_off:
23875 w->phys_cursor_on_p = 0;
23876 w->phys_cursor_type = NO_CURSOR;
23880 /* EXPORT:
23881 Display or clear cursor of window W. If ON is zero, clear the
23882 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23883 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23885 void
23886 display_and_set_cursor (struct window *w, int on,
23887 int hpos, int vpos, int x, int y)
23889 struct frame *f = XFRAME (w->frame);
23890 int new_cursor_type;
23891 int new_cursor_width;
23892 int active_cursor;
23893 struct glyph_row *glyph_row;
23894 struct glyph *glyph;
23896 /* This is pointless on invisible frames, and dangerous on garbaged
23897 windows and frames; in the latter case, the frame or window may
23898 be in the midst of changing its size, and x and y may be off the
23899 window. */
23900 if (! FRAME_VISIBLE_P (f)
23901 || FRAME_GARBAGED_P (f)
23902 || vpos >= w->current_matrix->nrows
23903 || hpos >= w->current_matrix->matrix_w)
23904 return;
23906 /* If cursor is off and we want it off, return quickly. */
23907 if (!on && !w->phys_cursor_on_p)
23908 return;
23910 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23911 /* If cursor row is not enabled, we don't really know where to
23912 display the cursor. */
23913 if (!glyph_row->enabled_p)
23915 w->phys_cursor_on_p = 0;
23916 return;
23919 glyph = NULL;
23920 if (!glyph_row->exact_window_width_line_p
23921 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23922 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23924 xassert (interrupt_input_blocked);
23926 /* Set new_cursor_type to the cursor we want to be displayed. */
23927 new_cursor_type = get_window_cursor_type (w, glyph,
23928 &new_cursor_width, &active_cursor);
23930 /* If cursor is currently being shown and we don't want it to be or
23931 it is in the wrong place, or the cursor type is not what we want,
23932 erase it. */
23933 if (w->phys_cursor_on_p
23934 && (!on
23935 || w->phys_cursor.x != x
23936 || w->phys_cursor.y != y
23937 || new_cursor_type != w->phys_cursor_type
23938 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23939 && new_cursor_width != w->phys_cursor_width)))
23940 erase_phys_cursor (w);
23942 /* Don't check phys_cursor_on_p here because that flag is only set
23943 to zero in some cases where we know that the cursor has been
23944 completely erased, to avoid the extra work of erasing the cursor
23945 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23946 still not be visible, or it has only been partly erased. */
23947 if (on)
23949 w->phys_cursor_ascent = glyph_row->ascent;
23950 w->phys_cursor_height = glyph_row->height;
23952 /* Set phys_cursor_.* before x_draw_.* is called because some
23953 of them may need the information. */
23954 w->phys_cursor.x = x;
23955 w->phys_cursor.y = glyph_row->y;
23956 w->phys_cursor.hpos = hpos;
23957 w->phys_cursor.vpos = vpos;
23960 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23961 new_cursor_type, new_cursor_width,
23962 on, active_cursor);
23966 /* Switch the display of W's cursor on or off, according to the value
23967 of ON. */
23969 void
23970 update_window_cursor (struct window *w, int on)
23972 /* Don't update cursor in windows whose frame is in the process
23973 of being deleted. */
23974 if (w->current_matrix)
23976 BLOCK_INPUT;
23977 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23978 w->phys_cursor.x, w->phys_cursor.y);
23979 UNBLOCK_INPUT;
23984 /* Call update_window_cursor with parameter ON_P on all leaf windows
23985 in the window tree rooted at W. */
23987 static void
23988 update_cursor_in_window_tree (struct window *w, int on_p)
23990 while (w)
23992 if (!NILP (w->hchild))
23993 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23994 else if (!NILP (w->vchild))
23995 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23996 else
23997 update_window_cursor (w, on_p);
23999 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24004 /* EXPORT:
24005 Display the cursor on window W, or clear it, according to ON_P.
24006 Don't change the cursor's position. */
24008 void
24009 x_update_cursor (struct frame *f, int on_p)
24011 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24015 /* EXPORT:
24016 Clear the cursor of window W to background color, and mark the
24017 cursor as not shown. This is used when the text where the cursor
24018 is about to be rewritten. */
24020 void
24021 x_clear_cursor (struct window *w)
24023 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24024 update_window_cursor (w, 0);
24027 #endif /* HAVE_WINDOW_SYSTEM */
24029 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24030 and MSDOS. */
24031 void
24032 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24033 int start_hpos, int end_hpos,
24034 enum draw_glyphs_face draw)
24036 #ifdef HAVE_WINDOW_SYSTEM
24037 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24039 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24040 return;
24042 #endif
24043 #if defined (HAVE_GPM) || defined (MSDOS)
24044 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24045 #endif
24048 /* EXPORT:
24049 Display the active region described by mouse_face_* according to DRAW. */
24051 void
24052 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24054 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24055 struct frame *f = XFRAME (WINDOW_FRAME (w));
24057 if (/* If window is in the process of being destroyed, don't bother
24058 to do anything. */
24059 w->current_matrix != NULL
24060 /* Don't update mouse highlight if hidden */
24061 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24062 /* Recognize when we are called to operate on rows that don't exist
24063 anymore. This can happen when a window is split. */
24064 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24066 int phys_cursor_on_p = w->phys_cursor_on_p;
24067 struct glyph_row *row, *first, *last;
24069 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24070 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24072 for (row = first; row <= last && row->enabled_p; ++row)
24074 int start_hpos, end_hpos, start_x;
24076 /* For all but the first row, the highlight starts at column 0. */
24077 if (row == first)
24079 /* R2L rows have BEG and END in reversed order, but the
24080 screen drawing geometry is always left to right. So
24081 we need to mirror the beginning and end of the
24082 highlighted area in R2L rows. */
24083 if (!row->reversed_p)
24085 start_hpos = hlinfo->mouse_face_beg_col;
24086 start_x = hlinfo->mouse_face_beg_x;
24088 else if (row == last)
24090 start_hpos = hlinfo->mouse_face_end_col;
24091 start_x = hlinfo->mouse_face_end_x;
24093 else
24095 start_hpos = 0;
24096 start_x = 0;
24099 else if (row->reversed_p && row == last)
24101 start_hpos = hlinfo->mouse_face_end_col;
24102 start_x = hlinfo->mouse_face_end_x;
24104 else
24106 start_hpos = 0;
24107 start_x = 0;
24110 if (row == last)
24112 if (!row->reversed_p)
24113 end_hpos = hlinfo->mouse_face_end_col;
24114 else if (row == first)
24115 end_hpos = hlinfo->mouse_face_beg_col;
24116 else
24118 end_hpos = row->used[TEXT_AREA];
24119 if (draw == DRAW_NORMAL_TEXT)
24120 row->fill_line_p = 1; /* Clear to end of line */
24123 else if (row->reversed_p && row == first)
24124 end_hpos = hlinfo->mouse_face_beg_col;
24125 else
24127 end_hpos = row->used[TEXT_AREA];
24128 if (draw == DRAW_NORMAL_TEXT)
24129 row->fill_line_p = 1; /* Clear to end of line */
24132 if (end_hpos > start_hpos)
24134 draw_row_with_mouse_face (w, start_x, row,
24135 start_hpos, end_hpos, draw);
24137 row->mouse_face_p
24138 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24142 #ifdef HAVE_WINDOW_SYSTEM
24143 /* When we've written over the cursor, arrange for it to
24144 be displayed again. */
24145 if (FRAME_WINDOW_P (f)
24146 && phys_cursor_on_p && !w->phys_cursor_on_p)
24148 BLOCK_INPUT;
24149 display_and_set_cursor (w, 1,
24150 w->phys_cursor.hpos, w->phys_cursor.vpos,
24151 w->phys_cursor.x, w->phys_cursor.y);
24152 UNBLOCK_INPUT;
24154 #endif /* HAVE_WINDOW_SYSTEM */
24157 #ifdef HAVE_WINDOW_SYSTEM
24158 /* Change the mouse cursor. */
24159 if (FRAME_WINDOW_P (f))
24161 if (draw == DRAW_NORMAL_TEXT
24162 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24163 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24164 else if (draw == DRAW_MOUSE_FACE)
24165 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24166 else
24167 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24169 #endif /* HAVE_WINDOW_SYSTEM */
24172 /* EXPORT:
24173 Clear out the mouse-highlighted active region.
24174 Redraw it un-highlighted first. Value is non-zero if mouse
24175 face was actually drawn unhighlighted. */
24178 clear_mouse_face (Mouse_HLInfo *hlinfo)
24180 int cleared = 0;
24182 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24184 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24185 cleared = 1;
24188 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24189 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24190 hlinfo->mouse_face_window = Qnil;
24191 hlinfo->mouse_face_overlay = Qnil;
24192 return cleared;
24195 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24196 within the mouse face on that window. */
24197 static int
24198 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24200 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24202 /* Quickly resolve the easy cases. */
24203 if (!(WINDOWP (hlinfo->mouse_face_window)
24204 && XWINDOW (hlinfo->mouse_face_window) == w))
24205 return 0;
24206 if (vpos < hlinfo->mouse_face_beg_row
24207 || vpos > hlinfo->mouse_face_end_row)
24208 return 0;
24209 if (vpos > hlinfo->mouse_face_beg_row
24210 && vpos < hlinfo->mouse_face_end_row)
24211 return 1;
24213 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24215 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24217 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24218 return 1;
24220 else if ((vpos == hlinfo->mouse_face_beg_row
24221 && hpos >= hlinfo->mouse_face_beg_col)
24222 || (vpos == hlinfo->mouse_face_end_row
24223 && hpos < hlinfo->mouse_face_end_col))
24224 return 1;
24226 else
24228 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24230 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24231 return 1;
24233 else if ((vpos == hlinfo->mouse_face_beg_row
24234 && hpos <= hlinfo->mouse_face_beg_col)
24235 || (vpos == hlinfo->mouse_face_end_row
24236 && hpos > hlinfo->mouse_face_end_col))
24237 return 1;
24239 return 0;
24243 /* EXPORT:
24244 Non-zero if physical cursor of window W is within mouse face. */
24247 cursor_in_mouse_face_p (struct window *w)
24249 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24254 /* Find the glyph rows START_ROW and END_ROW of window W that display
24255 characters between buffer positions START_CHARPOS and END_CHARPOS
24256 (excluding END_CHARPOS). This is similar to row_containing_pos,
24257 but is more accurate when bidi reordering makes buffer positions
24258 change non-linearly with glyph rows. */
24259 static void
24260 rows_from_pos_range (struct window *w,
24261 EMACS_INT start_charpos, EMACS_INT end_charpos,
24262 struct glyph_row **start, struct glyph_row **end)
24264 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24265 int last_y = window_text_bottom_y (w);
24266 struct glyph_row *row;
24268 *start = NULL;
24269 *end = NULL;
24271 while (!first->enabled_p
24272 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24273 first++;
24275 /* Find the START row. */
24276 for (row = first;
24277 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24278 row++)
24280 /* A row can potentially be the START row if the range of the
24281 characters it displays intersects the range
24282 [START_CHARPOS..END_CHARPOS). */
24283 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24284 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24285 /* See the commentary in row_containing_pos, for the
24286 explanation of the complicated way to check whether
24287 some position is beyond the end of the characters
24288 displayed by a row. */
24289 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24290 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24291 && !row->ends_at_zv_p
24292 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24293 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24294 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24295 && !row->ends_at_zv_p
24296 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24298 /* Found a candidate row. Now make sure at least one of the
24299 glyphs it displays has a charpos from the range
24300 [START_CHARPOS..END_CHARPOS).
24302 This is not obvious because bidi reordering could make
24303 buffer positions of a row be 1,2,3,102,101,100, and if we
24304 want to highlight characters in [50..60), we don't want
24305 this row, even though [50..60) does intersect [1..103),
24306 the range of character positions given by the row's start
24307 and end positions. */
24308 struct glyph *g = row->glyphs[TEXT_AREA];
24309 struct glyph *e = g + row->used[TEXT_AREA];
24311 while (g < e)
24313 if (BUFFERP (g->object)
24314 && start_charpos <= g->charpos && g->charpos < end_charpos)
24315 *start = row;
24316 g++;
24318 if (*start)
24319 break;
24323 /* Find the END row. */
24324 if (!*start
24325 /* If the last row is partially visible, start looking for END
24326 from that row, instead of starting from FIRST. */
24327 && !(row->enabled_p
24328 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24329 row = first;
24330 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24332 struct glyph_row *next = row + 1;
24334 if (!next->enabled_p
24335 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24336 /* The first row >= START whose range of displayed characters
24337 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24338 is the row END + 1. */
24339 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24340 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24341 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24342 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24343 && !next->ends_at_zv_p
24344 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24345 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24346 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24347 && !next->ends_at_zv_p
24348 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24350 *end = row;
24351 break;
24353 else
24355 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24356 but none of the characters it displays are in the range, it is
24357 also END + 1. */
24358 struct glyph *g = next->glyphs[TEXT_AREA];
24359 struct glyph *e = g + next->used[TEXT_AREA];
24361 while (g < e)
24363 if (BUFFERP (g->object)
24364 && start_charpos <= g->charpos && g->charpos < end_charpos)
24365 break;
24366 g++;
24368 if (g == e)
24370 *end = row;
24371 break;
24377 /* This function sets the mouse_face_* elements of HLINFO, assuming
24378 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24379 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24380 for the overlay or run of text properties specifying the mouse
24381 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24382 before-string and after-string that must also be highlighted.
24383 DISPLAY_STRING, if non-nil, is a display string that may cover some
24384 or all of the highlighted text. */
24386 static void
24387 mouse_face_from_buffer_pos (Lisp_Object window,
24388 Mouse_HLInfo *hlinfo,
24389 EMACS_INT mouse_charpos,
24390 EMACS_INT start_charpos,
24391 EMACS_INT end_charpos,
24392 Lisp_Object before_string,
24393 Lisp_Object after_string,
24394 Lisp_Object display_string)
24396 struct window *w = XWINDOW (window);
24397 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24398 struct glyph_row *r1, *r2;
24399 struct glyph *glyph, *end;
24400 EMACS_INT ignore, pos;
24401 int x;
24403 xassert (NILP (display_string) || STRINGP (display_string));
24404 xassert (NILP (before_string) || STRINGP (before_string));
24405 xassert (NILP (after_string) || STRINGP (after_string));
24407 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24408 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24409 if (r1 == NULL)
24410 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24411 /* If the before-string or display-string contains newlines,
24412 rows_from_pos_range skips to its last row. Move back. */
24413 if (!NILP (before_string) || !NILP (display_string))
24415 struct glyph_row *prev;
24416 while ((prev = r1 - 1, prev >= first)
24417 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24418 && prev->used[TEXT_AREA] > 0)
24420 struct glyph *beg = prev->glyphs[TEXT_AREA];
24421 glyph = beg + prev->used[TEXT_AREA];
24422 while (--glyph >= beg && INTEGERP (glyph->object));
24423 if (glyph < beg
24424 || !(EQ (glyph->object, before_string)
24425 || EQ (glyph->object, display_string)))
24426 break;
24427 r1 = prev;
24430 if (r2 == NULL)
24432 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24433 hlinfo->mouse_face_past_end = 1;
24435 else if (!NILP (after_string))
24437 /* If the after-string has newlines, advance to its last row. */
24438 struct glyph_row *next;
24439 struct glyph_row *last
24440 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24442 for (next = r2 + 1;
24443 next <= last
24444 && next->used[TEXT_AREA] > 0
24445 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24446 ++next)
24447 r2 = next;
24449 /* The rest of the display engine assumes that mouse_face_beg_row is
24450 either above below mouse_face_end_row or identical to it. But
24451 with bidi-reordered continued lines, the row for START_CHARPOS
24452 could be below the row for END_CHARPOS. If so, swap the rows and
24453 store them in correct order. */
24454 if (r1->y > r2->y)
24456 struct glyph_row *tem = r2;
24458 r2 = r1;
24459 r1 = tem;
24462 hlinfo->mouse_face_beg_y = r1->y;
24463 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24464 hlinfo->mouse_face_end_y = r2->y;
24465 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24467 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24468 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
24469 could be anywhere in the row and in any order. The strategy
24470 below is to find the leftmost and the rightmost glyph that
24471 belongs to either of these 3 strings, or whose position is
24472 between START_CHARPOS and END_CHARPOS, and highlight all the
24473 glyphs between those two. This may cover more than just the text
24474 between START_CHARPOS and END_CHARPOS if the range of characters
24475 strides the bidi level boundary, e.g. if the beginning is in R2L
24476 text while the end is in L2R text or vice versa. */
24477 if (!r1->reversed_p)
24479 /* This row is in a left to right paragraph. Scan it left to
24480 right. */
24481 glyph = r1->glyphs[TEXT_AREA];
24482 end = glyph + r1->used[TEXT_AREA];
24483 x = r1->x;
24485 /* Skip truncation glyphs at the start of the glyph row. */
24486 if (r1->displays_text_p)
24487 for (; glyph < end
24488 && INTEGERP (glyph->object)
24489 && glyph->charpos < 0;
24490 ++glyph)
24491 x += glyph->pixel_width;
24493 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24494 or DISPLAY_STRING, and the first glyph from buffer whose
24495 position is between START_CHARPOS and END_CHARPOS. */
24496 for (; glyph < end
24497 && !INTEGERP (glyph->object)
24498 && !EQ (glyph->object, display_string)
24499 && !(BUFFERP (glyph->object)
24500 && (glyph->charpos >= start_charpos
24501 && glyph->charpos < end_charpos));
24502 ++glyph)
24504 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24505 are present at buffer positions between START_CHARPOS and
24506 END_CHARPOS, or if they come from an overlay. */
24507 if (EQ (glyph->object, before_string))
24509 pos = string_buffer_position (w, before_string,
24510 start_charpos);
24511 /* If pos == 0, it means before_string came from an
24512 overlay, not from a buffer position. */
24513 if (!pos || (pos >= start_charpos && pos < end_charpos))
24514 break;
24516 else if (EQ (glyph->object, after_string))
24518 pos = string_buffer_position (w, after_string, end_charpos);
24519 if (!pos || (pos >= start_charpos && pos < end_charpos))
24520 break;
24522 x += glyph->pixel_width;
24524 hlinfo->mouse_face_beg_x = x;
24525 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24527 else
24529 /* This row is in a right to left paragraph. Scan it right to
24530 left. */
24531 struct glyph *g;
24533 end = r1->glyphs[TEXT_AREA] - 1;
24534 glyph = end + r1->used[TEXT_AREA];
24536 /* Skip truncation glyphs at the start of the glyph row. */
24537 if (r1->displays_text_p)
24538 for (; glyph > end
24539 && INTEGERP (glyph->object)
24540 && glyph->charpos < 0;
24541 --glyph)
24544 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24545 or DISPLAY_STRING, and the first glyph from buffer whose
24546 position is between START_CHARPOS and END_CHARPOS. */
24547 for (; glyph > end
24548 && !INTEGERP (glyph->object)
24549 && !EQ (glyph->object, display_string)
24550 && !(BUFFERP (glyph->object)
24551 && (glyph->charpos >= start_charpos
24552 && glyph->charpos < end_charpos));
24553 --glyph)
24555 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24556 are present at buffer positions between START_CHARPOS and
24557 END_CHARPOS, or if they come from an overlay. */
24558 if (EQ (glyph->object, before_string))
24560 pos = string_buffer_position (w, before_string, start_charpos);
24561 /* If pos == 0, it means before_string came from an
24562 overlay, not from a buffer position. */
24563 if (!pos || (pos >= start_charpos && pos < end_charpos))
24564 break;
24566 else if (EQ (glyph->object, after_string))
24568 pos = string_buffer_position (w, after_string, end_charpos);
24569 if (!pos || (pos >= start_charpos && pos < end_charpos))
24570 break;
24574 glyph++; /* first glyph to the right of the highlighted area */
24575 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24576 x += g->pixel_width;
24577 hlinfo->mouse_face_beg_x = x;
24578 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24581 /* If the highlight ends in a different row, compute GLYPH and END
24582 for the end row. Otherwise, reuse the values computed above for
24583 the row where the highlight begins. */
24584 if (r2 != r1)
24586 if (!r2->reversed_p)
24588 glyph = r2->glyphs[TEXT_AREA];
24589 end = glyph + r2->used[TEXT_AREA];
24590 x = r2->x;
24592 else
24594 end = r2->glyphs[TEXT_AREA] - 1;
24595 glyph = end + r2->used[TEXT_AREA];
24599 if (!r2->reversed_p)
24601 /* Skip truncation and continuation glyphs near the end of the
24602 row, and also blanks and stretch glyphs inserted by
24603 extend_face_to_end_of_line. */
24604 while (end > glyph
24605 && INTEGERP ((end - 1)->object)
24606 && (end - 1)->charpos <= 0)
24607 --end;
24608 /* Scan the rest of the glyph row from the end, looking for the
24609 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24610 DISPLAY_STRING, or whose position is between START_CHARPOS
24611 and END_CHARPOS */
24612 for (--end;
24613 end > glyph
24614 && !INTEGERP (end->object)
24615 && !EQ (end->object, display_string)
24616 && !(BUFFERP (end->object)
24617 && (end->charpos >= start_charpos
24618 && end->charpos < end_charpos));
24619 --end)
24621 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24622 are present at buffer positions between START_CHARPOS and
24623 END_CHARPOS, or if they come from an overlay. */
24624 if (EQ (end->object, before_string))
24626 pos = string_buffer_position (w, before_string, start_charpos);
24627 if (!pos || (pos >= start_charpos && pos < end_charpos))
24628 break;
24630 else if (EQ (end->object, after_string))
24632 pos = string_buffer_position (w, after_string, end_charpos);
24633 if (!pos || (pos >= start_charpos && pos < end_charpos))
24634 break;
24637 /* Find the X coordinate of the last glyph to be highlighted. */
24638 for (; glyph <= end; ++glyph)
24639 x += glyph->pixel_width;
24641 hlinfo->mouse_face_end_x = x;
24642 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24644 else
24646 /* Skip truncation and continuation glyphs near the end of the
24647 row, and also blanks and stretch glyphs inserted by
24648 extend_face_to_end_of_line. */
24649 x = r2->x;
24650 end++;
24651 while (end < glyph
24652 && INTEGERP (end->object)
24653 && end->charpos <= 0)
24655 x += end->pixel_width;
24656 ++end;
24658 /* Scan the rest of the glyph row from the end, looking for the
24659 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24660 DISPLAY_STRING, or whose position is between START_CHARPOS
24661 and END_CHARPOS */
24662 for ( ;
24663 end < glyph
24664 && !INTEGERP (end->object)
24665 && !EQ (end->object, display_string)
24666 && !(BUFFERP (end->object)
24667 && (end->charpos >= start_charpos
24668 && end->charpos < end_charpos));
24669 ++end)
24671 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24672 are present at buffer positions between START_CHARPOS and
24673 END_CHARPOS, or if they come from an overlay. */
24674 if (EQ (end->object, before_string))
24676 pos = string_buffer_position (w, before_string, start_charpos);
24677 if (!pos || (pos >= start_charpos && pos < end_charpos))
24678 break;
24680 else if (EQ (end->object, after_string))
24682 pos = string_buffer_position (w, after_string, end_charpos);
24683 if (!pos || (pos >= start_charpos && pos < end_charpos))
24684 break;
24686 x += end->pixel_width;
24688 hlinfo->mouse_face_end_x = x;
24689 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24692 hlinfo->mouse_face_window = window;
24693 hlinfo->mouse_face_face_id
24694 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24695 mouse_charpos + 1,
24696 !hlinfo->mouse_face_hidden, -1);
24697 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24700 /* The following function is not used anymore (replaced with
24701 mouse_face_from_string_pos), but I leave it here for the time
24702 being, in case someone would. */
24704 #if 0 /* not used */
24706 /* Find the position of the glyph for position POS in OBJECT in
24707 window W's current matrix, and return in *X, *Y the pixel
24708 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24710 RIGHT_P non-zero means return the position of the right edge of the
24711 glyph, RIGHT_P zero means return the left edge position.
24713 If no glyph for POS exists in the matrix, return the position of
24714 the glyph with the next smaller position that is in the matrix, if
24715 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24716 exists in the matrix, return the position of the glyph with the
24717 next larger position in OBJECT.
24719 Value is non-zero if a glyph was found. */
24721 static int
24722 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24723 int *hpos, int *vpos, int *x, int *y, int right_p)
24725 int yb = window_text_bottom_y (w);
24726 struct glyph_row *r;
24727 struct glyph *best_glyph = NULL;
24728 struct glyph_row *best_row = NULL;
24729 int best_x = 0;
24731 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24732 r->enabled_p && r->y < yb;
24733 ++r)
24735 struct glyph *g = r->glyphs[TEXT_AREA];
24736 struct glyph *e = g + r->used[TEXT_AREA];
24737 int gx;
24739 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24740 if (EQ (g->object, object))
24742 if (g->charpos == pos)
24744 best_glyph = g;
24745 best_x = gx;
24746 best_row = r;
24747 goto found;
24749 else if (best_glyph == NULL
24750 || ((eabs (g->charpos - pos)
24751 < eabs (best_glyph->charpos - pos))
24752 && (right_p
24753 ? g->charpos < pos
24754 : g->charpos > pos)))
24756 best_glyph = g;
24757 best_x = gx;
24758 best_row = r;
24763 found:
24765 if (best_glyph)
24767 *x = best_x;
24768 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24770 if (right_p)
24772 *x += best_glyph->pixel_width;
24773 ++*hpos;
24776 *y = best_row->y;
24777 *vpos = best_row - w->current_matrix->rows;
24780 return best_glyph != NULL;
24782 #endif /* not used */
24784 /* Find the positions of the first and the last glyphs in window W's
24785 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24786 (assumed to be a string), and return in HLINFO's mouse_face_*
24787 members the pixel and column/row coordinates of those glyphs. */
24789 static void
24790 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24791 Lisp_Object object,
24792 EMACS_INT startpos, EMACS_INT endpos)
24794 int yb = window_text_bottom_y (w);
24795 struct glyph_row *r;
24796 struct glyph *g, *e;
24797 int gx;
24798 int found = 0;
24800 /* Find the glyph row with at least one position in the range
24801 [STARTPOS..ENDPOS], and the first glyph in that row whose
24802 position belongs to that range. */
24803 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24804 r->enabled_p && r->y < yb;
24805 ++r)
24807 if (!r->reversed_p)
24809 g = r->glyphs[TEXT_AREA];
24810 e = g + r->used[TEXT_AREA];
24811 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24812 if (EQ (g->object, object)
24813 && startpos <= g->charpos && g->charpos <= endpos)
24815 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24816 hlinfo->mouse_face_beg_y = r->y;
24817 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24818 hlinfo->mouse_face_beg_x = gx;
24819 found = 1;
24820 break;
24823 else
24825 struct glyph *g1;
24827 e = r->glyphs[TEXT_AREA];
24828 g = e + r->used[TEXT_AREA];
24829 for ( ; g > e; --g)
24830 if (EQ ((g-1)->object, object)
24831 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24833 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24834 hlinfo->mouse_face_beg_y = r->y;
24835 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24836 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24837 gx += g1->pixel_width;
24838 hlinfo->mouse_face_beg_x = gx;
24839 found = 1;
24840 break;
24843 if (found)
24844 break;
24847 if (!found)
24848 return;
24850 /* Starting with the next row, look for the first row which does NOT
24851 include any glyphs whose positions are in the range. */
24852 for (++r; r->enabled_p && r->y < yb; ++r)
24854 g = r->glyphs[TEXT_AREA];
24855 e = g + r->used[TEXT_AREA];
24856 found = 0;
24857 for ( ; g < e; ++g)
24858 if (EQ (g->object, object)
24859 && startpos <= g->charpos && g->charpos <= endpos)
24861 found = 1;
24862 break;
24864 if (!found)
24865 break;
24868 /* The highlighted region ends on the previous row. */
24869 r--;
24871 /* Set the end row and its vertical pixel coordinate. */
24872 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24873 hlinfo->mouse_face_end_y = r->y;
24875 /* Compute and set the end column and the end column's horizontal
24876 pixel coordinate. */
24877 if (!r->reversed_p)
24879 g = r->glyphs[TEXT_AREA];
24880 e = g + r->used[TEXT_AREA];
24881 for ( ; e > g; --e)
24882 if (EQ ((e-1)->object, object)
24883 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24884 break;
24885 hlinfo->mouse_face_end_col = e - g;
24887 for (gx = r->x; g < e; ++g)
24888 gx += g->pixel_width;
24889 hlinfo->mouse_face_end_x = gx;
24891 else
24893 e = r->glyphs[TEXT_AREA];
24894 g = e + r->used[TEXT_AREA];
24895 for (gx = r->x ; e < g; ++e)
24897 if (EQ (e->object, object)
24898 && startpos <= e->charpos && e->charpos <= endpos)
24899 break;
24900 gx += e->pixel_width;
24902 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24903 hlinfo->mouse_face_end_x = gx;
24907 #ifdef HAVE_WINDOW_SYSTEM
24909 /* See if position X, Y is within a hot-spot of an image. */
24911 static int
24912 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24914 if (!CONSP (hot_spot))
24915 return 0;
24917 if (EQ (XCAR (hot_spot), Qrect))
24919 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24920 Lisp_Object rect = XCDR (hot_spot);
24921 Lisp_Object tem;
24922 if (!CONSP (rect))
24923 return 0;
24924 if (!CONSP (XCAR (rect)))
24925 return 0;
24926 if (!CONSP (XCDR (rect)))
24927 return 0;
24928 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24929 return 0;
24930 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24931 return 0;
24932 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24933 return 0;
24934 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24935 return 0;
24936 return 1;
24938 else if (EQ (XCAR (hot_spot), Qcircle))
24940 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24941 Lisp_Object circ = XCDR (hot_spot);
24942 Lisp_Object lr, lx0, ly0;
24943 if (CONSP (circ)
24944 && CONSP (XCAR (circ))
24945 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24946 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24947 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24949 double r = XFLOATINT (lr);
24950 double dx = XINT (lx0) - x;
24951 double dy = XINT (ly0) - y;
24952 return (dx * dx + dy * dy <= r * r);
24955 else if (EQ (XCAR (hot_spot), Qpoly))
24957 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24958 if (VECTORP (XCDR (hot_spot)))
24960 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24961 Lisp_Object *poly = v->contents;
24962 int n = v->size;
24963 int i;
24964 int inside = 0;
24965 Lisp_Object lx, ly;
24966 int x0, y0;
24968 /* Need an even number of coordinates, and at least 3 edges. */
24969 if (n < 6 || n & 1)
24970 return 0;
24972 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24973 If count is odd, we are inside polygon. Pixels on edges
24974 may or may not be included depending on actual geometry of the
24975 polygon. */
24976 if ((lx = poly[n-2], !INTEGERP (lx))
24977 || (ly = poly[n-1], !INTEGERP (lx)))
24978 return 0;
24979 x0 = XINT (lx), y0 = XINT (ly);
24980 for (i = 0; i < n; i += 2)
24982 int x1 = x0, y1 = y0;
24983 if ((lx = poly[i], !INTEGERP (lx))
24984 || (ly = poly[i+1], !INTEGERP (ly)))
24985 return 0;
24986 x0 = XINT (lx), y0 = XINT (ly);
24988 /* Does this segment cross the X line? */
24989 if (x0 >= x)
24991 if (x1 >= x)
24992 continue;
24994 else if (x1 < x)
24995 continue;
24996 if (y > y0 && y > y1)
24997 continue;
24998 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24999 inside = !inside;
25001 return inside;
25004 return 0;
25007 Lisp_Object
25008 find_hot_spot (Lisp_Object map, int x, int y)
25010 while (CONSP (map))
25012 if (CONSP (XCAR (map))
25013 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25014 return XCAR (map);
25015 map = XCDR (map);
25018 return Qnil;
25021 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25022 3, 3, 0,
25023 doc: /* Lookup in image map MAP coordinates X and Y.
25024 An image map is an alist where each element has the format (AREA ID PLIST).
25025 An AREA is specified as either a rectangle, a circle, or a polygon:
25026 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25027 pixel coordinates of the upper left and bottom right corners.
25028 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25029 and the radius of the circle; r may be a float or integer.
25030 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25031 vector describes one corner in the polygon.
25032 Returns the alist element for the first matching AREA in MAP. */)
25033 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25035 if (NILP (map))
25036 return Qnil;
25038 CHECK_NUMBER (x);
25039 CHECK_NUMBER (y);
25041 return find_hot_spot (map, XINT (x), XINT (y));
25045 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25046 static void
25047 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25049 /* Do not change cursor shape while dragging mouse. */
25050 if (!NILP (do_mouse_tracking))
25051 return;
25053 if (!NILP (pointer))
25055 if (EQ (pointer, Qarrow))
25056 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25057 else if (EQ (pointer, Qhand))
25058 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25059 else if (EQ (pointer, Qtext))
25060 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25061 else if (EQ (pointer, intern ("hdrag")))
25062 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25063 #ifdef HAVE_X_WINDOWS
25064 else if (EQ (pointer, intern ("vdrag")))
25065 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25066 #endif
25067 else if (EQ (pointer, intern ("hourglass")))
25068 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25069 else if (EQ (pointer, Qmodeline))
25070 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25071 else
25072 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25075 if (cursor != No_Cursor)
25076 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25079 #endif /* HAVE_WINDOW_SYSTEM */
25081 /* Take proper action when mouse has moved to the mode or header line
25082 or marginal area AREA of window W, x-position X and y-position Y.
25083 X is relative to the start of the text display area of W, so the
25084 width of bitmap areas and scroll bars must be subtracted to get a
25085 position relative to the start of the mode line. */
25087 static void
25088 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25089 enum window_part area)
25091 struct window *w = XWINDOW (window);
25092 struct frame *f = XFRAME (w->frame);
25093 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25094 #ifdef HAVE_WINDOW_SYSTEM
25095 Display_Info *dpyinfo;
25096 #endif
25097 Cursor cursor = No_Cursor;
25098 Lisp_Object pointer = Qnil;
25099 int dx, dy, width, height;
25100 EMACS_INT charpos;
25101 Lisp_Object string, object = Qnil;
25102 Lisp_Object pos, help;
25104 Lisp_Object mouse_face;
25105 int original_x_pixel = x;
25106 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25107 struct glyph_row *row;
25109 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25111 int x0;
25112 struct glyph *end;
25114 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25115 returns them in row/column units! */
25116 string = mode_line_string (w, area, &x, &y, &charpos,
25117 &object, &dx, &dy, &width, &height);
25119 row = (area == ON_MODE_LINE
25120 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25121 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25123 /* Find the glyph under the mouse pointer. */
25124 if (row->mode_line_p && row->enabled_p)
25126 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25127 end = glyph + row->used[TEXT_AREA];
25129 for (x0 = original_x_pixel;
25130 glyph < end && x0 >= glyph->pixel_width;
25131 ++glyph)
25132 x0 -= glyph->pixel_width;
25134 if (glyph >= end)
25135 glyph = NULL;
25138 else
25140 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25141 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25142 returns them in row/column units! */
25143 string = marginal_area_string (w, area, &x, &y, &charpos,
25144 &object, &dx, &dy, &width, &height);
25147 help = Qnil;
25149 #ifdef HAVE_WINDOW_SYSTEM
25150 if (IMAGEP (object))
25152 Lisp_Object image_map, hotspot;
25153 if ((image_map = Fplist_get (XCDR (object), QCmap),
25154 !NILP (image_map))
25155 && (hotspot = find_hot_spot (image_map, dx, dy),
25156 CONSP (hotspot))
25157 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25159 Lisp_Object area_id, plist;
25161 area_id = XCAR (hotspot);
25162 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25163 If so, we could look for mouse-enter, mouse-leave
25164 properties in PLIST (and do something...). */
25165 hotspot = XCDR (hotspot);
25166 if (CONSP (hotspot)
25167 && (plist = XCAR (hotspot), CONSP (plist)))
25169 pointer = Fplist_get (plist, Qpointer);
25170 if (NILP (pointer))
25171 pointer = Qhand;
25172 help = Fplist_get (plist, Qhelp_echo);
25173 if (!NILP (help))
25175 help_echo_string = help;
25176 /* Is this correct? ++kfs */
25177 XSETWINDOW (help_echo_window, w);
25178 help_echo_object = w->buffer;
25179 help_echo_pos = charpos;
25183 if (NILP (pointer))
25184 pointer = Fplist_get (XCDR (object), QCpointer);
25186 #endif /* HAVE_WINDOW_SYSTEM */
25188 if (STRINGP (string))
25190 pos = make_number (charpos);
25191 /* If we're on a string with `help-echo' text property, arrange
25192 for the help to be displayed. This is done by setting the
25193 global variable help_echo_string to the help string. */
25194 if (NILP (help))
25196 help = Fget_text_property (pos, Qhelp_echo, string);
25197 if (!NILP (help))
25199 help_echo_string = help;
25200 XSETWINDOW (help_echo_window, w);
25201 help_echo_object = string;
25202 help_echo_pos = charpos;
25206 #ifdef HAVE_WINDOW_SYSTEM
25207 if (FRAME_WINDOW_P (f))
25209 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25210 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25211 if (NILP (pointer))
25212 pointer = Fget_text_property (pos, Qpointer, string);
25214 /* Change the mouse pointer according to what is under X/Y. */
25215 if (NILP (pointer)
25216 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25218 Lisp_Object map;
25219 map = Fget_text_property (pos, Qlocal_map, string);
25220 if (!KEYMAPP (map))
25221 map = Fget_text_property (pos, Qkeymap, string);
25222 if (!KEYMAPP (map))
25223 cursor = dpyinfo->vertical_scroll_bar_cursor;
25226 #endif
25228 /* Change the mouse face according to what is under X/Y. */
25229 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25230 if (!NILP (mouse_face)
25231 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25232 && glyph)
25234 Lisp_Object b, e;
25236 struct glyph * tmp_glyph;
25238 int gpos;
25239 int gseq_length;
25240 int total_pixel_width;
25241 EMACS_INT begpos, endpos, ignore;
25243 int vpos, hpos;
25245 b = Fprevious_single_property_change (make_number (charpos + 1),
25246 Qmouse_face, string, Qnil);
25247 if (NILP (b))
25248 begpos = 0;
25249 else
25250 begpos = XINT (b);
25252 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25253 if (NILP (e))
25254 endpos = SCHARS (string);
25255 else
25256 endpos = XINT (e);
25258 /* Calculate the glyph position GPOS of GLYPH in the
25259 displayed string, relative to the beginning of the
25260 highlighted part of the string.
25262 Note: GPOS is different from CHARPOS. CHARPOS is the
25263 position of GLYPH in the internal string object. A mode
25264 line string format has structures which are converted to
25265 a flattened string by the Emacs Lisp interpreter. The
25266 internal string is an element of those structures. The
25267 displayed string is the flattened string. */
25268 tmp_glyph = row_start_glyph;
25269 while (tmp_glyph < glyph
25270 && (!(EQ (tmp_glyph->object, glyph->object)
25271 && begpos <= tmp_glyph->charpos
25272 && tmp_glyph->charpos < endpos)))
25273 tmp_glyph++;
25274 gpos = glyph - tmp_glyph;
25276 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25277 the highlighted part of the displayed string to which
25278 GLYPH belongs. Note: GSEQ_LENGTH is different from
25279 SCHARS (STRING), because the latter returns the length of
25280 the internal string. */
25281 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25282 tmp_glyph > glyph
25283 && (!(EQ (tmp_glyph->object, glyph->object)
25284 && begpos <= tmp_glyph->charpos
25285 && tmp_glyph->charpos < endpos));
25286 tmp_glyph--)
25288 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25290 /* Calculate the total pixel width of all the glyphs between
25291 the beginning of the highlighted area and GLYPH. */
25292 total_pixel_width = 0;
25293 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25294 total_pixel_width += tmp_glyph->pixel_width;
25296 /* Pre calculation of re-rendering position. Note: X is in
25297 column units here, after the call to mode_line_string or
25298 marginal_area_string. */
25299 hpos = x - gpos;
25300 vpos = (area == ON_MODE_LINE
25301 ? (w->current_matrix)->nrows - 1
25302 : 0);
25304 /* If GLYPH's position is included in the region that is
25305 already drawn in mouse face, we have nothing to do. */
25306 if ( EQ (window, hlinfo->mouse_face_window)
25307 && (!row->reversed_p
25308 ? (hlinfo->mouse_face_beg_col <= hpos
25309 && hpos < hlinfo->mouse_face_end_col)
25310 /* In R2L rows we swap BEG and END, see below. */
25311 : (hlinfo->mouse_face_end_col <= hpos
25312 && hpos < hlinfo->mouse_face_beg_col))
25313 && hlinfo->mouse_face_beg_row == vpos )
25314 return;
25316 if (clear_mouse_face (hlinfo))
25317 cursor = No_Cursor;
25319 if (!row->reversed_p)
25321 hlinfo->mouse_face_beg_col = hpos;
25322 hlinfo->mouse_face_beg_x = original_x_pixel
25323 - (total_pixel_width + dx);
25324 hlinfo->mouse_face_end_col = hpos + gseq_length;
25325 hlinfo->mouse_face_end_x = 0;
25327 else
25329 /* In R2L rows, show_mouse_face expects BEG and END
25330 coordinates to be swapped. */
25331 hlinfo->mouse_face_end_col = hpos;
25332 hlinfo->mouse_face_end_x = original_x_pixel
25333 - (total_pixel_width + dx);
25334 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25335 hlinfo->mouse_face_beg_x = 0;
25338 hlinfo->mouse_face_beg_row = vpos;
25339 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25340 hlinfo->mouse_face_beg_y = 0;
25341 hlinfo->mouse_face_end_y = 0;
25342 hlinfo->mouse_face_past_end = 0;
25343 hlinfo->mouse_face_window = window;
25345 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25346 charpos,
25347 0, 0, 0,
25348 &ignore,
25349 glyph->face_id,
25351 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25353 if (NILP (pointer))
25354 pointer = Qhand;
25356 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25357 clear_mouse_face (hlinfo);
25359 #ifdef HAVE_WINDOW_SYSTEM
25360 if (FRAME_WINDOW_P (f))
25361 define_frame_cursor1 (f, cursor, pointer);
25362 #endif
25366 /* EXPORT:
25367 Take proper action when the mouse has moved to position X, Y on
25368 frame F as regards highlighting characters that have mouse-face
25369 properties. Also de-highlighting chars where the mouse was before.
25370 X and Y can be negative or out of range. */
25372 void
25373 note_mouse_highlight (struct frame *f, int x, int y)
25375 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25376 enum window_part part;
25377 Lisp_Object window;
25378 struct window *w;
25379 Cursor cursor = No_Cursor;
25380 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25381 struct buffer *b;
25383 /* When a menu is active, don't highlight because this looks odd. */
25384 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25385 if (popup_activated ())
25386 return;
25387 #endif
25389 if (NILP (Vmouse_highlight)
25390 || !f->glyphs_initialized_p
25391 || f->pointer_invisible)
25392 return;
25394 hlinfo->mouse_face_mouse_x = x;
25395 hlinfo->mouse_face_mouse_y = y;
25396 hlinfo->mouse_face_mouse_frame = f;
25398 if (hlinfo->mouse_face_defer)
25399 return;
25401 if (gc_in_progress)
25403 hlinfo->mouse_face_deferred_gc = 1;
25404 return;
25407 /* Which window is that in? */
25408 window = window_from_coordinates (f, x, y, &part, 1);
25410 /* If we were displaying active text in another window, clear that.
25411 Also clear if we move out of text area in same window. */
25412 if (! EQ (window, hlinfo->mouse_face_window)
25413 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25414 && !NILP (hlinfo->mouse_face_window)))
25415 clear_mouse_face (hlinfo);
25417 /* Not on a window -> return. */
25418 if (!WINDOWP (window))
25419 return;
25421 /* Reset help_echo_string. It will get recomputed below. */
25422 help_echo_string = Qnil;
25424 /* Convert to window-relative pixel coordinates. */
25425 w = XWINDOW (window);
25426 frame_to_window_pixel_xy (w, &x, &y);
25428 #ifdef HAVE_WINDOW_SYSTEM
25429 /* Handle tool-bar window differently since it doesn't display a
25430 buffer. */
25431 if (EQ (window, f->tool_bar_window))
25433 note_tool_bar_highlight (f, x, y);
25434 return;
25436 #endif
25438 /* Mouse is on the mode, header line or margin? */
25439 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25440 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25442 note_mode_line_or_margin_highlight (window, x, y, part);
25443 return;
25446 #ifdef HAVE_WINDOW_SYSTEM
25447 if (part == ON_VERTICAL_BORDER)
25449 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25450 help_echo_string = build_string ("drag-mouse-1: resize");
25452 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25453 || part == ON_SCROLL_BAR)
25454 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25455 else
25456 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25457 #endif
25459 /* Are we in a window whose display is up to date?
25460 And verify the buffer's text has not changed. */
25461 b = XBUFFER (w->buffer);
25462 if (part == ON_TEXT
25463 && EQ (w->window_end_valid, w->buffer)
25464 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25465 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25467 int hpos, vpos, i, dx, dy, area;
25468 EMACS_INT pos;
25469 struct glyph *glyph;
25470 Lisp_Object object;
25471 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
25472 Lisp_Object *overlay_vec = NULL;
25473 int noverlays;
25474 struct buffer *obuf;
25475 EMACS_INT obegv, ozv;
25476 int same_region;
25478 /* Find the glyph under X/Y. */
25479 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25481 #ifdef HAVE_WINDOW_SYSTEM
25482 /* Look for :pointer property on image. */
25483 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25485 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25486 if (img != NULL && IMAGEP (img->spec))
25488 Lisp_Object image_map, hotspot;
25489 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25490 !NILP (image_map))
25491 && (hotspot = find_hot_spot (image_map,
25492 glyph->slice.img.x + dx,
25493 glyph->slice.img.y + dy),
25494 CONSP (hotspot))
25495 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25497 Lisp_Object area_id, plist;
25499 area_id = XCAR (hotspot);
25500 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25501 If so, we could look for mouse-enter, mouse-leave
25502 properties in PLIST (and do something...). */
25503 hotspot = XCDR (hotspot);
25504 if (CONSP (hotspot)
25505 && (plist = XCAR (hotspot), CONSP (plist)))
25507 pointer = Fplist_get (plist, Qpointer);
25508 if (NILP (pointer))
25509 pointer = Qhand;
25510 help_echo_string = Fplist_get (plist, Qhelp_echo);
25511 if (!NILP (help_echo_string))
25513 help_echo_window = window;
25514 help_echo_object = glyph->object;
25515 help_echo_pos = glyph->charpos;
25519 if (NILP (pointer))
25520 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25523 #endif /* HAVE_WINDOW_SYSTEM */
25525 /* Clear mouse face if X/Y not over text. */
25526 if (glyph == NULL
25527 || area != TEXT_AREA
25528 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25529 /* Glyph's OBJECT is an integer for glyphs inserted by the
25530 display engine for its internal purposes, like truncation
25531 and continuation glyphs and blanks beyond the end of
25532 line's text on text terminals. If we are over such a
25533 glyph, we are not over any text. */
25534 || INTEGERP (glyph->object)
25535 /* R2L rows have a stretch glyph at their front, which
25536 stands for no text, whereas L2R rows have no glyphs at
25537 all beyond the end of text. Treat such stretch glyphs
25538 like we do with NULL glyphs in L2R rows. */
25539 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25540 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25541 && glyph->type == STRETCH_GLYPH
25542 && glyph->avoid_cursor_p))
25544 if (clear_mouse_face (hlinfo))
25545 cursor = No_Cursor;
25546 #ifdef HAVE_WINDOW_SYSTEM
25547 if (FRAME_WINDOW_P (f) && NILP (pointer))
25549 if (area != TEXT_AREA)
25550 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25551 else
25552 pointer = Vvoid_text_area_pointer;
25554 #endif
25555 goto set_cursor;
25558 pos = glyph->charpos;
25559 object = glyph->object;
25560 if (!STRINGP (object) && !BUFFERP (object))
25561 goto set_cursor;
25563 /* If we get an out-of-range value, return now; avoid an error. */
25564 if (BUFFERP (object) && pos > BUF_Z (b))
25565 goto set_cursor;
25567 /* Make the window's buffer temporarily current for
25568 overlays_at and compute_char_face. */
25569 obuf = current_buffer;
25570 current_buffer = b;
25571 obegv = BEGV;
25572 ozv = ZV;
25573 BEGV = BEG;
25574 ZV = Z;
25576 /* Is this char mouse-active or does it have help-echo? */
25577 position = make_number (pos);
25579 if (BUFFERP (object))
25581 /* Put all the overlays we want in a vector in overlay_vec. */
25582 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25583 /* Sort overlays into increasing priority order. */
25584 noverlays = sort_overlays (overlay_vec, noverlays, w);
25586 else
25587 noverlays = 0;
25589 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25591 if (same_region)
25592 cursor = No_Cursor;
25594 /* Check mouse-face highlighting. */
25595 if (! same_region
25596 /* If there exists an overlay with mouse-face overlapping
25597 the one we are currently highlighting, we have to
25598 check if we enter the overlapping overlay, and then
25599 highlight only that. */
25600 || (OVERLAYP (hlinfo->mouse_face_overlay)
25601 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25603 /* Find the highest priority overlay with a mouse-face. */
25604 overlay = Qnil;
25605 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25607 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25608 if (!NILP (mouse_face))
25609 overlay = overlay_vec[i];
25612 /* If we're highlighting the same overlay as before, there's
25613 no need to do that again. */
25614 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25615 goto check_help_echo;
25616 hlinfo->mouse_face_overlay = overlay;
25618 /* Clear the display of the old active region, if any. */
25619 if (clear_mouse_face (hlinfo))
25620 cursor = No_Cursor;
25622 /* If no overlay applies, get a text property. */
25623 if (NILP (overlay))
25624 mouse_face = Fget_text_property (position, Qmouse_face, object);
25626 /* Next, compute the bounds of the mouse highlighting and
25627 display it. */
25628 if (!NILP (mouse_face) && STRINGP (object))
25630 /* The mouse-highlighting comes from a display string
25631 with a mouse-face. */
25632 Lisp_Object b, e;
25633 EMACS_INT ignore;
25635 b = Fprevious_single_property_change
25636 (make_number (pos + 1), Qmouse_face, object, Qnil);
25637 e = Fnext_single_property_change
25638 (position, Qmouse_face, object, Qnil);
25639 if (NILP (b))
25640 b = make_number (0);
25641 if (NILP (e))
25642 e = make_number (SCHARS (object) - 1);
25643 mouse_face_from_string_pos (w, hlinfo, object,
25644 XINT (b), XINT (e));
25645 hlinfo->mouse_face_past_end = 0;
25646 hlinfo->mouse_face_window = window;
25647 hlinfo->mouse_face_face_id
25648 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25649 glyph->face_id, 1);
25650 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25651 cursor = No_Cursor;
25653 else
25655 /* The mouse-highlighting, if any, comes from an overlay
25656 or text property in the buffer. */
25657 Lisp_Object buffer, display_string;
25659 if (STRINGP (object))
25661 /* If we are on a display string with no mouse-face,
25662 check if the text under it has one. */
25663 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25664 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25665 pos = string_buffer_position (w, object, start);
25666 if (pos > 0)
25668 mouse_face = get_char_property_and_overlay
25669 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25670 buffer = w->buffer;
25671 display_string = object;
25674 else
25676 buffer = object;
25677 display_string = Qnil;
25680 if (!NILP (mouse_face))
25682 Lisp_Object before, after;
25683 Lisp_Object before_string, after_string;
25684 /* To correctly find the limits of mouse highlight
25685 in a bidi-reordered buffer, we must not use the
25686 optimization of limiting the search in
25687 previous-single-property-change and
25688 next-single-property-change, because
25689 rows_from_pos_range needs the real start and end
25690 positions to DTRT in this case. That's because
25691 the first row visible in a window does not
25692 necessarily display the character whose position
25693 is the smallest. */
25694 Lisp_Object lim1 =
25695 NILP (XBUFFER (buffer)->bidi_display_reordering)
25696 ? Fmarker_position (w->start)
25697 : Qnil;
25698 Lisp_Object lim2 =
25699 NILP (XBUFFER (buffer)->bidi_display_reordering)
25700 ? make_number (BUF_Z (XBUFFER (buffer))
25701 - XFASTINT (w->window_end_pos))
25702 : Qnil;
25704 if (NILP (overlay))
25706 /* Handle the text property case. */
25707 before = Fprevious_single_property_change
25708 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25709 after = Fnext_single_property_change
25710 (make_number (pos), Qmouse_face, buffer, lim2);
25711 before_string = after_string = Qnil;
25713 else
25715 /* Handle the overlay case. */
25716 before = Foverlay_start (overlay);
25717 after = Foverlay_end (overlay);
25718 before_string = Foverlay_get (overlay, Qbefore_string);
25719 after_string = Foverlay_get (overlay, Qafter_string);
25721 if (!STRINGP (before_string)) before_string = Qnil;
25722 if (!STRINGP (after_string)) after_string = Qnil;
25725 mouse_face_from_buffer_pos (window, hlinfo, pos,
25726 XFASTINT (before),
25727 XFASTINT (after),
25728 before_string, after_string,
25729 display_string);
25730 cursor = No_Cursor;
25735 check_help_echo:
25737 /* Look for a `help-echo' property. */
25738 if (NILP (help_echo_string)) {
25739 Lisp_Object help, overlay;
25741 /* Check overlays first. */
25742 help = overlay = Qnil;
25743 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25745 overlay = overlay_vec[i];
25746 help = Foverlay_get (overlay, Qhelp_echo);
25749 if (!NILP (help))
25751 help_echo_string = help;
25752 help_echo_window = window;
25753 help_echo_object = overlay;
25754 help_echo_pos = pos;
25756 else
25758 Lisp_Object object = glyph->object;
25759 EMACS_INT charpos = glyph->charpos;
25761 /* Try text properties. */
25762 if (STRINGP (object)
25763 && charpos >= 0
25764 && charpos < SCHARS (object))
25766 help = Fget_text_property (make_number (charpos),
25767 Qhelp_echo, object);
25768 if (NILP (help))
25770 /* If the string itself doesn't specify a help-echo,
25771 see if the buffer text ``under'' it does. */
25772 struct glyph_row *r
25773 = MATRIX_ROW (w->current_matrix, vpos);
25774 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25775 EMACS_INT pos = string_buffer_position (w, object, start);
25776 if (pos > 0)
25778 help = Fget_char_property (make_number (pos),
25779 Qhelp_echo, w->buffer);
25780 if (!NILP (help))
25782 charpos = pos;
25783 object = w->buffer;
25788 else if (BUFFERP (object)
25789 && charpos >= BEGV
25790 && charpos < ZV)
25791 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25792 object);
25794 if (!NILP (help))
25796 help_echo_string = help;
25797 help_echo_window = window;
25798 help_echo_object = object;
25799 help_echo_pos = charpos;
25804 #ifdef HAVE_WINDOW_SYSTEM
25805 /* Look for a `pointer' property. */
25806 if (FRAME_WINDOW_P (f) && NILP (pointer))
25808 /* Check overlays first. */
25809 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25810 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25812 if (NILP (pointer))
25814 Lisp_Object object = glyph->object;
25815 EMACS_INT charpos = glyph->charpos;
25817 /* Try text properties. */
25818 if (STRINGP (object)
25819 && charpos >= 0
25820 && charpos < SCHARS (object))
25822 pointer = Fget_text_property (make_number (charpos),
25823 Qpointer, object);
25824 if (NILP (pointer))
25826 /* If the string itself doesn't specify a pointer,
25827 see if the buffer text ``under'' it does. */
25828 struct glyph_row *r
25829 = MATRIX_ROW (w->current_matrix, vpos);
25830 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25831 EMACS_INT pos = string_buffer_position (w, object,
25832 start);
25833 if (pos > 0)
25834 pointer = Fget_char_property (make_number (pos),
25835 Qpointer, w->buffer);
25838 else if (BUFFERP (object)
25839 && charpos >= BEGV
25840 && charpos < ZV)
25841 pointer = Fget_text_property (make_number (charpos),
25842 Qpointer, object);
25845 #endif /* HAVE_WINDOW_SYSTEM */
25847 BEGV = obegv;
25848 ZV = ozv;
25849 current_buffer = obuf;
25852 set_cursor:
25854 #ifdef HAVE_WINDOW_SYSTEM
25855 if (FRAME_WINDOW_P (f))
25856 define_frame_cursor1 (f, cursor, pointer);
25857 #else
25858 /* This is here to prevent a compiler error, about "label at end of
25859 compound statement". */
25860 return;
25861 #endif
25865 /* EXPORT for RIF:
25866 Clear any mouse-face on window W. This function is part of the
25867 redisplay interface, and is called from try_window_id and similar
25868 functions to ensure the mouse-highlight is off. */
25870 void
25871 x_clear_window_mouse_face (struct window *w)
25873 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25874 Lisp_Object window;
25876 BLOCK_INPUT;
25877 XSETWINDOW (window, w);
25878 if (EQ (window, hlinfo->mouse_face_window))
25879 clear_mouse_face (hlinfo);
25880 UNBLOCK_INPUT;
25884 /* EXPORT:
25885 Just discard the mouse face information for frame F, if any.
25886 This is used when the size of F is changed. */
25888 void
25889 cancel_mouse_face (struct frame *f)
25891 Lisp_Object window;
25892 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25894 window = hlinfo->mouse_face_window;
25895 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25897 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25898 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25899 hlinfo->mouse_face_window = Qnil;
25905 /***********************************************************************
25906 Exposure Events
25907 ***********************************************************************/
25909 #ifdef HAVE_WINDOW_SYSTEM
25911 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25912 which intersects rectangle R. R is in window-relative coordinates. */
25914 static void
25915 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25916 enum glyph_row_area area)
25918 struct glyph *first = row->glyphs[area];
25919 struct glyph *end = row->glyphs[area] + row->used[area];
25920 struct glyph *last;
25921 int first_x, start_x, x;
25923 if (area == TEXT_AREA && row->fill_line_p)
25924 /* If row extends face to end of line write the whole line. */
25925 draw_glyphs (w, 0, row, area,
25926 0, row->used[area],
25927 DRAW_NORMAL_TEXT, 0);
25928 else
25930 /* Set START_X to the window-relative start position for drawing glyphs of
25931 AREA. The first glyph of the text area can be partially visible.
25932 The first glyphs of other areas cannot. */
25933 start_x = window_box_left_offset (w, area);
25934 x = start_x;
25935 if (area == TEXT_AREA)
25936 x += row->x;
25938 /* Find the first glyph that must be redrawn. */
25939 while (first < end
25940 && x + first->pixel_width < r->x)
25942 x += first->pixel_width;
25943 ++first;
25946 /* Find the last one. */
25947 last = first;
25948 first_x = x;
25949 while (last < end
25950 && x < r->x + r->width)
25952 x += last->pixel_width;
25953 ++last;
25956 /* Repaint. */
25957 if (last > first)
25958 draw_glyphs (w, first_x - start_x, row, area,
25959 first - row->glyphs[area], last - row->glyphs[area],
25960 DRAW_NORMAL_TEXT, 0);
25965 /* Redraw the parts of the glyph row ROW on window W intersecting
25966 rectangle R. R is in window-relative coordinates. Value is
25967 non-zero if mouse-face was overwritten. */
25969 static int
25970 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25972 xassert (row->enabled_p);
25974 if (row->mode_line_p || w->pseudo_window_p)
25975 draw_glyphs (w, 0, row, TEXT_AREA,
25976 0, row->used[TEXT_AREA],
25977 DRAW_NORMAL_TEXT, 0);
25978 else
25980 if (row->used[LEFT_MARGIN_AREA])
25981 expose_area (w, row, r, LEFT_MARGIN_AREA);
25982 if (row->used[TEXT_AREA])
25983 expose_area (w, row, r, TEXT_AREA);
25984 if (row->used[RIGHT_MARGIN_AREA])
25985 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25986 draw_row_fringe_bitmaps (w, row);
25989 return row->mouse_face_p;
25993 /* Redraw those parts of glyphs rows during expose event handling that
25994 overlap other rows. Redrawing of an exposed line writes over parts
25995 of lines overlapping that exposed line; this function fixes that.
25997 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25998 row in W's current matrix that is exposed and overlaps other rows.
25999 LAST_OVERLAPPING_ROW is the last such row. */
26001 static void
26002 expose_overlaps (struct window *w,
26003 struct glyph_row *first_overlapping_row,
26004 struct glyph_row *last_overlapping_row,
26005 XRectangle *r)
26007 struct glyph_row *row;
26009 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26010 if (row->overlapping_p)
26012 xassert (row->enabled_p && !row->mode_line_p);
26014 row->clip = r;
26015 if (row->used[LEFT_MARGIN_AREA])
26016 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26018 if (row->used[TEXT_AREA])
26019 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26021 if (row->used[RIGHT_MARGIN_AREA])
26022 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26023 row->clip = NULL;
26028 /* Return non-zero if W's cursor intersects rectangle R. */
26030 static int
26031 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26033 XRectangle cr, result;
26034 struct glyph *cursor_glyph;
26035 struct glyph_row *row;
26037 if (w->phys_cursor.vpos >= 0
26038 && w->phys_cursor.vpos < w->current_matrix->nrows
26039 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26040 row->enabled_p)
26041 && row->cursor_in_fringe_p)
26043 /* Cursor is in the fringe. */
26044 cr.x = window_box_right_offset (w,
26045 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26046 ? RIGHT_MARGIN_AREA
26047 : TEXT_AREA));
26048 cr.y = row->y;
26049 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26050 cr.height = row->height;
26051 return x_intersect_rectangles (&cr, r, &result);
26054 cursor_glyph = get_phys_cursor_glyph (w);
26055 if (cursor_glyph)
26057 /* r is relative to W's box, but w->phys_cursor.x is relative
26058 to left edge of W's TEXT area. Adjust it. */
26059 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26060 cr.y = w->phys_cursor.y;
26061 cr.width = cursor_glyph->pixel_width;
26062 cr.height = w->phys_cursor_height;
26063 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26064 I assume the effect is the same -- and this is portable. */
26065 return x_intersect_rectangles (&cr, r, &result);
26067 /* If we don't understand the format, pretend we're not in the hot-spot. */
26068 return 0;
26072 /* EXPORT:
26073 Draw a vertical window border to the right of window W if W doesn't
26074 have vertical scroll bars. */
26076 void
26077 x_draw_vertical_border (struct window *w)
26079 struct frame *f = XFRAME (WINDOW_FRAME (w));
26081 /* We could do better, if we knew what type of scroll-bar the adjacent
26082 windows (on either side) have... But we don't :-(
26083 However, I think this works ok. ++KFS 2003-04-25 */
26085 /* Redraw borders between horizontally adjacent windows. Don't
26086 do it for frames with vertical scroll bars because either the
26087 right scroll bar of a window, or the left scroll bar of its
26088 neighbor will suffice as a border. */
26089 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26090 return;
26092 if (!WINDOW_RIGHTMOST_P (w)
26093 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26095 int x0, x1, y0, y1;
26097 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26098 y1 -= 1;
26100 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26101 x1 -= 1;
26103 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26105 else if (!WINDOW_LEFTMOST_P (w)
26106 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26108 int x0, x1, y0, y1;
26110 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26111 y1 -= 1;
26113 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26114 x0 -= 1;
26116 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26121 /* Redraw the part of window W intersection rectangle FR. Pixel
26122 coordinates in FR are frame-relative. Call this function with
26123 input blocked. Value is non-zero if the exposure overwrites
26124 mouse-face. */
26126 static int
26127 expose_window (struct window *w, XRectangle *fr)
26129 struct frame *f = XFRAME (w->frame);
26130 XRectangle wr, r;
26131 int mouse_face_overwritten_p = 0;
26133 /* If window is not yet fully initialized, do nothing. This can
26134 happen when toolkit scroll bars are used and a window is split.
26135 Reconfiguring the scroll bar will generate an expose for a newly
26136 created window. */
26137 if (w->current_matrix == NULL)
26138 return 0;
26140 /* When we're currently updating the window, display and current
26141 matrix usually don't agree. Arrange for a thorough display
26142 later. */
26143 if (w == updated_window)
26145 SET_FRAME_GARBAGED (f);
26146 return 0;
26149 /* Frame-relative pixel rectangle of W. */
26150 wr.x = WINDOW_LEFT_EDGE_X (w);
26151 wr.y = WINDOW_TOP_EDGE_Y (w);
26152 wr.width = WINDOW_TOTAL_WIDTH (w);
26153 wr.height = WINDOW_TOTAL_HEIGHT (w);
26155 if (x_intersect_rectangles (fr, &wr, &r))
26157 int yb = window_text_bottom_y (w);
26158 struct glyph_row *row;
26159 int cursor_cleared_p;
26160 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26162 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26163 r.x, r.y, r.width, r.height));
26165 /* Convert to window coordinates. */
26166 r.x -= WINDOW_LEFT_EDGE_X (w);
26167 r.y -= WINDOW_TOP_EDGE_Y (w);
26169 /* Turn off the cursor. */
26170 if (!w->pseudo_window_p
26171 && phys_cursor_in_rect_p (w, &r))
26173 x_clear_cursor (w);
26174 cursor_cleared_p = 1;
26176 else
26177 cursor_cleared_p = 0;
26179 /* Update lines intersecting rectangle R. */
26180 first_overlapping_row = last_overlapping_row = NULL;
26181 for (row = w->current_matrix->rows;
26182 row->enabled_p;
26183 ++row)
26185 int y0 = row->y;
26186 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26188 if ((y0 >= r.y && y0 < r.y + r.height)
26189 || (y1 > r.y && y1 < r.y + r.height)
26190 || (r.y >= y0 && r.y < y1)
26191 || (r.y + r.height > y0 && r.y + r.height < y1))
26193 /* A header line may be overlapping, but there is no need
26194 to fix overlapping areas for them. KFS 2005-02-12 */
26195 if (row->overlapping_p && !row->mode_line_p)
26197 if (first_overlapping_row == NULL)
26198 first_overlapping_row = row;
26199 last_overlapping_row = row;
26202 row->clip = fr;
26203 if (expose_line (w, row, &r))
26204 mouse_face_overwritten_p = 1;
26205 row->clip = NULL;
26207 else if (row->overlapping_p)
26209 /* We must redraw a row overlapping the exposed area. */
26210 if (y0 < r.y
26211 ? y0 + row->phys_height > r.y
26212 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26214 if (first_overlapping_row == NULL)
26215 first_overlapping_row = row;
26216 last_overlapping_row = row;
26220 if (y1 >= yb)
26221 break;
26224 /* Display the mode line if there is one. */
26225 if (WINDOW_WANTS_MODELINE_P (w)
26226 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26227 row->enabled_p)
26228 && row->y < r.y + r.height)
26230 if (expose_line (w, row, &r))
26231 mouse_face_overwritten_p = 1;
26234 if (!w->pseudo_window_p)
26236 /* Fix the display of overlapping rows. */
26237 if (first_overlapping_row)
26238 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26239 fr);
26241 /* Draw border between windows. */
26242 x_draw_vertical_border (w);
26244 /* Turn the cursor on again. */
26245 if (cursor_cleared_p)
26246 update_window_cursor (w, 1);
26250 return mouse_face_overwritten_p;
26255 /* Redraw (parts) of all windows in the window tree rooted at W that
26256 intersect R. R contains frame pixel coordinates. Value is
26257 non-zero if the exposure overwrites mouse-face. */
26259 static int
26260 expose_window_tree (struct window *w, XRectangle *r)
26262 struct frame *f = XFRAME (w->frame);
26263 int mouse_face_overwritten_p = 0;
26265 while (w && !FRAME_GARBAGED_P (f))
26267 if (!NILP (w->hchild))
26268 mouse_face_overwritten_p
26269 |= expose_window_tree (XWINDOW (w->hchild), r);
26270 else if (!NILP (w->vchild))
26271 mouse_face_overwritten_p
26272 |= expose_window_tree (XWINDOW (w->vchild), r);
26273 else
26274 mouse_face_overwritten_p |= expose_window (w, r);
26276 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26279 return mouse_face_overwritten_p;
26283 /* EXPORT:
26284 Redisplay an exposed area of frame F. X and Y are the upper-left
26285 corner of the exposed rectangle. W and H are width and height of
26286 the exposed area. All are pixel values. W or H zero means redraw
26287 the entire frame. */
26289 void
26290 expose_frame (struct frame *f, int x, int y, int w, int h)
26292 XRectangle r;
26293 int mouse_face_overwritten_p = 0;
26295 TRACE ((stderr, "expose_frame "));
26297 /* No need to redraw if frame will be redrawn soon. */
26298 if (FRAME_GARBAGED_P (f))
26300 TRACE ((stderr, " garbaged\n"));
26301 return;
26304 /* If basic faces haven't been realized yet, there is no point in
26305 trying to redraw anything. This can happen when we get an expose
26306 event while Emacs is starting, e.g. by moving another window. */
26307 if (FRAME_FACE_CACHE (f) == NULL
26308 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26310 TRACE ((stderr, " no faces\n"));
26311 return;
26314 if (w == 0 || h == 0)
26316 r.x = r.y = 0;
26317 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26318 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26320 else
26322 r.x = x;
26323 r.y = y;
26324 r.width = w;
26325 r.height = h;
26328 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26329 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26331 if (WINDOWP (f->tool_bar_window))
26332 mouse_face_overwritten_p
26333 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26335 #ifdef HAVE_X_WINDOWS
26336 #ifndef MSDOS
26337 #ifndef USE_X_TOOLKIT
26338 if (WINDOWP (f->menu_bar_window))
26339 mouse_face_overwritten_p
26340 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26341 #endif /* not USE_X_TOOLKIT */
26342 #endif
26343 #endif
26345 /* Some window managers support a focus-follows-mouse style with
26346 delayed raising of frames. Imagine a partially obscured frame,
26347 and moving the mouse into partially obscured mouse-face on that
26348 frame. The visible part of the mouse-face will be highlighted,
26349 then the WM raises the obscured frame. With at least one WM, KDE
26350 2.1, Emacs is not getting any event for the raising of the frame
26351 (even tried with SubstructureRedirectMask), only Expose events.
26352 These expose events will draw text normally, i.e. not
26353 highlighted. Which means we must redo the highlight here.
26354 Subsume it under ``we love X''. --gerd 2001-08-15 */
26355 /* Included in Windows version because Windows most likely does not
26356 do the right thing if any third party tool offers
26357 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26358 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26360 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26361 if (f == hlinfo->mouse_face_mouse_frame)
26363 int x = hlinfo->mouse_face_mouse_x;
26364 int y = hlinfo->mouse_face_mouse_y;
26365 clear_mouse_face (hlinfo);
26366 note_mouse_highlight (f, x, y);
26372 /* EXPORT:
26373 Determine the intersection of two rectangles R1 and R2. Return
26374 the intersection in *RESULT. Value is non-zero if RESULT is not
26375 empty. */
26378 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26380 XRectangle *left, *right;
26381 XRectangle *upper, *lower;
26382 int intersection_p = 0;
26384 /* Rearrange so that R1 is the left-most rectangle. */
26385 if (r1->x < r2->x)
26386 left = r1, right = r2;
26387 else
26388 left = r2, right = r1;
26390 /* X0 of the intersection is right.x0, if this is inside R1,
26391 otherwise there is no intersection. */
26392 if (right->x <= left->x + left->width)
26394 result->x = right->x;
26396 /* The right end of the intersection is the minimum of the
26397 the right ends of left and right. */
26398 result->width = (min (left->x + left->width, right->x + right->width)
26399 - result->x);
26401 /* Same game for Y. */
26402 if (r1->y < r2->y)
26403 upper = r1, lower = r2;
26404 else
26405 upper = r2, lower = r1;
26407 /* The upper end of the intersection is lower.y0, if this is inside
26408 of upper. Otherwise, there is no intersection. */
26409 if (lower->y <= upper->y + upper->height)
26411 result->y = lower->y;
26413 /* The lower end of the intersection is the minimum of the lower
26414 ends of upper and lower. */
26415 result->height = (min (lower->y + lower->height,
26416 upper->y + upper->height)
26417 - result->y);
26418 intersection_p = 1;
26422 return intersection_p;
26425 #endif /* HAVE_WINDOW_SYSTEM */
26428 /***********************************************************************
26429 Initialization
26430 ***********************************************************************/
26432 void
26433 syms_of_xdisp (void)
26435 Vwith_echo_area_save_vector = Qnil;
26436 staticpro (&Vwith_echo_area_save_vector);
26438 Vmessage_stack = Qnil;
26439 staticpro (&Vmessage_stack);
26441 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26442 staticpro (&Qinhibit_redisplay);
26444 message_dolog_marker1 = Fmake_marker ();
26445 staticpro (&message_dolog_marker1);
26446 message_dolog_marker2 = Fmake_marker ();
26447 staticpro (&message_dolog_marker2);
26448 message_dolog_marker3 = Fmake_marker ();
26449 staticpro (&message_dolog_marker3);
26451 #if GLYPH_DEBUG
26452 defsubr (&Sdump_frame_glyph_matrix);
26453 defsubr (&Sdump_glyph_matrix);
26454 defsubr (&Sdump_glyph_row);
26455 defsubr (&Sdump_tool_bar_row);
26456 defsubr (&Strace_redisplay);
26457 defsubr (&Strace_to_stderr);
26458 #endif
26459 #ifdef HAVE_WINDOW_SYSTEM
26460 defsubr (&Stool_bar_lines_needed);
26461 defsubr (&Slookup_image_map);
26462 #endif
26463 defsubr (&Sformat_mode_line);
26464 defsubr (&Sinvisible_p);
26465 defsubr (&Scurrent_bidi_paragraph_direction);
26467 staticpro (&Qmenu_bar_update_hook);
26468 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26470 staticpro (&Qoverriding_terminal_local_map);
26471 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26473 staticpro (&Qoverriding_local_map);
26474 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26476 staticpro (&Qwindow_scroll_functions);
26477 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26479 staticpro (&Qwindow_text_change_functions);
26480 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26482 staticpro (&Qredisplay_end_trigger_functions);
26483 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26485 staticpro (&Qinhibit_point_motion_hooks);
26486 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26488 Qeval = intern_c_string ("eval");
26489 staticpro (&Qeval);
26491 QCdata = intern_c_string (":data");
26492 staticpro (&QCdata);
26493 Qdisplay = intern_c_string ("display");
26494 staticpro (&Qdisplay);
26495 Qspace_width = intern_c_string ("space-width");
26496 staticpro (&Qspace_width);
26497 Qraise = intern_c_string ("raise");
26498 staticpro (&Qraise);
26499 Qslice = intern_c_string ("slice");
26500 staticpro (&Qslice);
26501 Qspace = intern_c_string ("space");
26502 staticpro (&Qspace);
26503 Qmargin = intern_c_string ("margin");
26504 staticpro (&Qmargin);
26505 Qpointer = intern_c_string ("pointer");
26506 staticpro (&Qpointer);
26507 Qleft_margin = intern_c_string ("left-margin");
26508 staticpro (&Qleft_margin);
26509 Qright_margin = intern_c_string ("right-margin");
26510 staticpro (&Qright_margin);
26511 Qcenter = intern_c_string ("center");
26512 staticpro (&Qcenter);
26513 Qline_height = intern_c_string ("line-height");
26514 staticpro (&Qline_height);
26515 QCalign_to = intern_c_string (":align-to");
26516 staticpro (&QCalign_to);
26517 QCrelative_width = intern_c_string (":relative-width");
26518 staticpro (&QCrelative_width);
26519 QCrelative_height = intern_c_string (":relative-height");
26520 staticpro (&QCrelative_height);
26521 QCeval = intern_c_string (":eval");
26522 staticpro (&QCeval);
26523 QCpropertize = intern_c_string (":propertize");
26524 staticpro (&QCpropertize);
26525 QCfile = intern_c_string (":file");
26526 staticpro (&QCfile);
26527 Qfontified = intern_c_string ("fontified");
26528 staticpro (&Qfontified);
26529 Qfontification_functions = intern_c_string ("fontification-functions");
26530 staticpro (&Qfontification_functions);
26531 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26532 staticpro (&Qtrailing_whitespace);
26533 Qescape_glyph = intern_c_string ("escape-glyph");
26534 staticpro (&Qescape_glyph);
26535 Qnobreak_space = intern_c_string ("nobreak-space");
26536 staticpro (&Qnobreak_space);
26537 Qimage = intern_c_string ("image");
26538 staticpro (&Qimage);
26539 Qtext = intern_c_string ("text");
26540 staticpro (&Qtext);
26541 Qboth = intern_c_string ("both");
26542 staticpro (&Qboth);
26543 Qboth_horiz = intern_c_string ("both-horiz");
26544 staticpro (&Qboth_horiz);
26545 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26546 staticpro (&Qtext_image_horiz);
26547 QCmap = intern_c_string (":map");
26548 staticpro (&QCmap);
26549 QCpointer = intern_c_string (":pointer");
26550 staticpro (&QCpointer);
26551 Qrect = intern_c_string ("rect");
26552 staticpro (&Qrect);
26553 Qcircle = intern_c_string ("circle");
26554 staticpro (&Qcircle);
26555 Qpoly = intern_c_string ("poly");
26556 staticpro (&Qpoly);
26557 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26558 staticpro (&Qmessage_truncate_lines);
26559 Qgrow_only = intern_c_string ("grow-only");
26560 staticpro (&Qgrow_only);
26561 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26562 staticpro (&Qinhibit_menubar_update);
26563 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26564 staticpro (&Qinhibit_eval_during_redisplay);
26565 Qposition = intern_c_string ("position");
26566 staticpro (&Qposition);
26567 Qbuffer_position = intern_c_string ("buffer-position");
26568 staticpro (&Qbuffer_position);
26569 Qobject = intern_c_string ("object");
26570 staticpro (&Qobject);
26571 Qbar = intern_c_string ("bar");
26572 staticpro (&Qbar);
26573 Qhbar = intern_c_string ("hbar");
26574 staticpro (&Qhbar);
26575 Qbox = intern_c_string ("box");
26576 staticpro (&Qbox);
26577 Qhollow = intern_c_string ("hollow");
26578 staticpro (&Qhollow);
26579 Qhand = intern_c_string ("hand");
26580 staticpro (&Qhand);
26581 Qarrow = intern_c_string ("arrow");
26582 staticpro (&Qarrow);
26583 Qtext = intern_c_string ("text");
26584 staticpro (&Qtext);
26585 Qrisky_local_variable = intern_c_string ("risky-local-variable");
26586 staticpro (&Qrisky_local_variable);
26587 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26588 staticpro (&Qinhibit_free_realized_faces);
26590 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26591 Fcons (intern_c_string ("void-variable"), Qnil)),
26592 Qnil);
26593 staticpro (&list_of_error);
26595 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26596 staticpro (&Qlast_arrow_position);
26597 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26598 staticpro (&Qlast_arrow_string);
26600 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26601 staticpro (&Qoverlay_arrow_string);
26602 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26603 staticpro (&Qoverlay_arrow_bitmap);
26605 echo_buffer[0] = echo_buffer[1] = Qnil;
26606 staticpro (&echo_buffer[0]);
26607 staticpro (&echo_buffer[1]);
26609 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26610 staticpro (&echo_area_buffer[0]);
26611 staticpro (&echo_area_buffer[1]);
26613 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26614 staticpro (&Vmessages_buffer_name);
26616 mode_line_proptrans_alist = Qnil;
26617 staticpro (&mode_line_proptrans_alist);
26618 mode_line_string_list = Qnil;
26619 staticpro (&mode_line_string_list);
26620 mode_line_string_face = Qnil;
26621 staticpro (&mode_line_string_face);
26622 mode_line_string_face_prop = Qnil;
26623 staticpro (&mode_line_string_face_prop);
26624 Vmode_line_unwind_vector = Qnil;
26625 staticpro (&Vmode_line_unwind_vector);
26627 help_echo_string = Qnil;
26628 staticpro (&help_echo_string);
26629 help_echo_object = Qnil;
26630 staticpro (&help_echo_object);
26631 help_echo_window = Qnil;
26632 staticpro (&help_echo_window);
26633 previous_help_echo_string = Qnil;
26634 staticpro (&previous_help_echo_string);
26635 help_echo_pos = -1;
26637 Qright_to_left = intern_c_string ("right-to-left");
26638 staticpro (&Qright_to_left);
26639 Qleft_to_right = intern_c_string ("left-to-right");
26640 staticpro (&Qleft_to_right);
26642 #ifdef HAVE_WINDOW_SYSTEM
26643 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
26644 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26645 For example, if a block cursor is over a tab, it will be drawn as
26646 wide as that tab on the display. */);
26647 x_stretch_cursor_p = 0;
26648 #endif
26650 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
26651 doc: /* *Non-nil means highlight trailing whitespace.
26652 The face used for trailing whitespace is `trailing-whitespace'. */);
26653 Vshow_trailing_whitespace = Qnil;
26655 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
26656 doc: /* *Control highlighting of nobreak space and soft hyphen.
26657 A value of t means highlight the character itself (for nobreak space,
26658 use face `nobreak-space').
26659 A value of nil means no highlighting.
26660 Other values mean display the escape glyph followed by an ordinary
26661 space or ordinary hyphen. */);
26662 Vnobreak_char_display = Qt;
26664 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
26665 doc: /* *The pointer shape to show in void text areas.
26666 A value of nil means to show the text pointer. Other options are `arrow',
26667 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26668 Vvoid_text_area_pointer = Qarrow;
26670 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
26671 doc: /* Non-nil means don't actually do any redisplay.
26672 This is used for internal purposes. */);
26673 Vinhibit_redisplay = Qnil;
26675 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
26676 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26677 Vglobal_mode_string = Qnil;
26679 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
26680 doc: /* Marker for where to display an arrow on top of the buffer text.
26681 This must be the beginning of a line in order to work.
26682 See also `overlay-arrow-string'. */);
26683 Voverlay_arrow_position = Qnil;
26685 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
26686 doc: /* String to display as an arrow in non-window frames.
26687 See also `overlay-arrow-position'. */);
26688 Voverlay_arrow_string = make_pure_c_string ("=>");
26690 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
26691 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26692 The symbols on this list are examined during redisplay to determine
26693 where to display overlay arrows. */);
26694 Voverlay_arrow_variable_list
26695 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26697 DEFVAR_INT ("scroll-step", &scroll_step,
26698 doc: /* *The number of lines to try scrolling a window by when point moves out.
26699 If that fails to bring point back on frame, point is centered instead.
26700 If this is zero, point is always centered after it moves off frame.
26701 If you want scrolling to always be a line at a time, you should set
26702 `scroll-conservatively' to a large value rather than set this to 1. */);
26704 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
26705 doc: /* *Scroll up to this many lines, to bring point back on screen.
26706 If point moves off-screen, redisplay will scroll by up to
26707 `scroll-conservatively' lines in order to bring point just barely
26708 onto the screen again. If that cannot be done, then redisplay
26709 recenters point as usual.
26711 A value of zero means always recenter point if it moves off screen. */);
26712 scroll_conservatively = 0;
26714 DEFVAR_INT ("scroll-margin", &scroll_margin,
26715 doc: /* *Number of lines of margin at the top and bottom of a window.
26716 Recenter the window whenever point gets within this many lines
26717 of the top or bottom of the window. */);
26718 scroll_margin = 0;
26720 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
26721 doc: /* Pixels per inch value for non-window system displays.
26722 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26723 Vdisplay_pixels_per_inch = make_float (72.0);
26725 #if GLYPH_DEBUG
26726 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
26727 #endif
26729 DEFVAR_LISP ("truncate-partial-width-windows",
26730 &Vtruncate_partial_width_windows,
26731 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26732 For an integer value, truncate lines in each window narrower than the
26733 full frame width, provided the window width is less than that integer;
26734 otherwise, respect the value of `truncate-lines'.
26736 For any other non-nil value, truncate lines in all windows that do
26737 not span the full frame width.
26739 A value of nil means to respect the value of `truncate-lines'.
26741 If `word-wrap' is enabled, you might want to reduce this. */);
26742 Vtruncate_partial_width_windows = make_number (50);
26744 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
26745 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26746 Any other value means to use the appropriate face, `mode-line',
26747 `header-line', or `menu' respectively. */);
26748 mode_line_inverse_video = 1;
26750 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
26751 doc: /* *Maximum buffer size for which line number should be displayed.
26752 If the buffer is bigger than this, the line number does not appear
26753 in the mode line. A value of nil means no limit. */);
26754 Vline_number_display_limit = Qnil;
26756 DEFVAR_INT ("line-number-display-limit-width",
26757 &line_number_display_limit_width,
26758 doc: /* *Maximum line width (in characters) for line number display.
26759 If the average length of the lines near point is bigger than this, then the
26760 line number may be omitted from the mode line. */);
26761 line_number_display_limit_width = 200;
26763 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
26764 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26765 highlight_nonselected_windows = 0;
26767 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
26768 doc: /* Non-nil if more than one frame is visible on this display.
26769 Minibuffer-only frames don't count, but iconified frames do.
26770 This variable is not guaranteed to be accurate except while processing
26771 `frame-title-format' and `icon-title-format'. */);
26773 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
26774 doc: /* Template for displaying the title bar of visible frames.
26775 \(Assuming the window manager supports this feature.)
26777 This variable has the same structure as `mode-line-format', except that
26778 the %c and %l constructs are ignored. It is used only on frames for
26779 which no explicit name has been set \(see `modify-frame-parameters'). */);
26781 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
26782 doc: /* Template for displaying the title bar of an iconified frame.
26783 \(Assuming the window manager supports this feature.)
26784 This variable has the same structure as `mode-line-format' (which see),
26785 and is used only on frames for which no explicit name has been set
26786 \(see `modify-frame-parameters'). */);
26787 Vicon_title_format
26788 = Vframe_title_format
26789 = pure_cons (intern_c_string ("multiple-frames"),
26790 pure_cons (make_pure_c_string ("%b"),
26791 pure_cons (pure_cons (empty_unibyte_string,
26792 pure_cons (intern_c_string ("invocation-name"),
26793 pure_cons (make_pure_c_string ("@"),
26794 pure_cons (intern_c_string ("system-name"),
26795 Qnil)))),
26796 Qnil)));
26798 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
26799 doc: /* Maximum number of lines to keep in the message log buffer.
26800 If nil, disable message logging. If t, log messages but don't truncate
26801 the buffer when it becomes large. */);
26802 Vmessage_log_max = make_number (100);
26804 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
26805 doc: /* Functions called before redisplay, if window sizes have changed.
26806 The value should be a list of functions that take one argument.
26807 Just before redisplay, for each frame, if any of its windows have changed
26808 size since the last redisplay, or have been split or deleted,
26809 all the functions in the list are called, with the frame as argument. */);
26810 Vwindow_size_change_functions = Qnil;
26812 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
26813 doc: /* List of functions to call before redisplaying a window with scrolling.
26814 Each function is called with two arguments, the window and its new
26815 display-start position. Note that these functions are also called by
26816 `set-window-buffer'. Also note that the value of `window-end' is not
26817 valid when these functions are called. */);
26818 Vwindow_scroll_functions = Qnil;
26820 DEFVAR_LISP ("window-text-change-functions",
26821 &Vwindow_text_change_functions,
26822 doc: /* Functions to call in redisplay when text in the window might change. */);
26823 Vwindow_text_change_functions = Qnil;
26825 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
26826 doc: /* Functions called when redisplay of a window reaches the end trigger.
26827 Each function is called with two arguments, the window and the end trigger value.
26828 See `set-window-redisplay-end-trigger'. */);
26829 Vredisplay_end_trigger_functions = Qnil;
26831 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
26832 doc: /* *Non-nil means autoselect window with mouse pointer.
26833 If nil, do not autoselect windows.
26834 A positive number means delay autoselection by that many seconds: a
26835 window is autoselected only after the mouse has remained in that
26836 window for the duration of the delay.
26837 A negative number has a similar effect, but causes windows to be
26838 autoselected only after the mouse has stopped moving. \(Because of
26839 the way Emacs compares mouse events, you will occasionally wait twice
26840 that time before the window gets selected.\)
26841 Any other value means to autoselect window instantaneously when the
26842 mouse pointer enters it.
26844 Autoselection selects the minibuffer only if it is active, and never
26845 unselects the minibuffer if it is active.
26847 When customizing this variable make sure that the actual value of
26848 `focus-follows-mouse' matches the behavior of your window manager. */);
26849 Vmouse_autoselect_window = Qnil;
26851 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
26852 doc: /* *Non-nil means automatically resize tool-bars.
26853 This dynamically changes the tool-bar's height to the minimum height
26854 that is needed to make all tool-bar items visible.
26855 If value is `grow-only', the tool-bar's height is only increased
26856 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26857 Vauto_resize_tool_bars = Qt;
26859 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
26860 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26861 auto_raise_tool_bar_buttons_p = 1;
26863 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
26864 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26865 make_cursor_line_fully_visible_p = 1;
26867 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26868 doc: /* *Border below tool-bar in pixels.
26869 If an integer, use it as the height of the border.
26870 If it is one of `internal-border-width' or `border-width', use the
26871 value of the corresponding frame parameter.
26872 Otherwise, no border is added below the tool-bar. */);
26873 Vtool_bar_border = Qinternal_border_width;
26875 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26876 doc: /* *Margin around tool-bar buttons in pixels.
26877 If an integer, use that for both horizontal and vertical margins.
26878 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26879 HORZ specifying the horizontal margin, and VERT specifying the
26880 vertical margin. */);
26881 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26883 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26884 doc: /* *Relief thickness of tool-bar buttons. */);
26885 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26887 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26888 doc: /* *Tool bar style to use.
26889 It can be one of
26890 image - show images only
26891 text - show text only
26892 both - show both, text below image
26893 both-horiz - show text to the right of the image
26894 text-image-horiz - show text to the left of the image
26895 any other - use system default or image if no system default. */);
26896 Vtool_bar_style = Qnil;
26898 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26899 doc: /* *Maximum number of characters a label can have to be shown.
26900 The tool bar style must also show labels for this to have any effect, see
26901 `tool-bar-style'. */);
26902 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26904 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26905 doc: /* List of functions to call to fontify regions of text.
26906 Each function is called with one argument POS. Functions must
26907 fontify a region starting at POS in the current buffer, and give
26908 fontified regions the property `fontified'. */);
26909 Vfontification_functions = Qnil;
26910 Fmake_variable_buffer_local (Qfontification_functions);
26912 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26913 &unibyte_display_via_language_environment,
26914 doc: /* *Non-nil means display unibyte text according to language environment.
26915 Specifically, this means that raw bytes in the range 160-255 decimal
26916 are displayed by converting them to the equivalent multibyte characters
26917 according to the current language environment. As a result, they are
26918 displayed according to the current fontset.
26920 Note that this variable affects only how these bytes are displayed,
26921 but does not change the fact they are interpreted as raw bytes. */);
26922 unibyte_display_via_language_environment = 0;
26924 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26925 doc: /* *Maximum height for resizing mini-windows.
26926 If a float, it specifies a fraction of the mini-window frame's height.
26927 If an integer, it specifies a number of lines. */);
26928 Vmax_mini_window_height = make_float (0.25);
26930 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26931 doc: /* *How to resize mini-windows.
26932 A value of nil means don't automatically resize mini-windows.
26933 A value of t means resize them to fit the text displayed in them.
26934 A value of `grow-only', the default, means let mini-windows grow
26935 only, until their display becomes empty, at which point the windows
26936 go back to their normal size. */);
26937 Vresize_mini_windows = Qgrow_only;
26939 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26940 doc: /* Alist specifying how to blink the cursor off.
26941 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26942 `cursor-type' frame-parameter or variable equals ON-STATE,
26943 comparing using `equal', Emacs uses OFF-STATE to specify
26944 how to blink it off. ON-STATE and OFF-STATE are values for
26945 the `cursor-type' frame parameter.
26947 If a frame's ON-STATE has no entry in this list,
26948 the frame's other specifications determine how to blink the cursor off. */);
26949 Vblink_cursor_alist = Qnil;
26951 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26952 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26953 If non-nil, windows are automatically scrolled horizontally to make
26954 point visible. */);
26955 automatic_hscrolling_p = 1;
26956 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26957 staticpro (&Qauto_hscroll_mode);
26959 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26960 doc: /* *How many columns away from the window edge point is allowed to get
26961 before automatic hscrolling will horizontally scroll the window. */);
26962 hscroll_margin = 5;
26964 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26965 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26966 When point is less than `hscroll-margin' columns from the window
26967 edge, automatic hscrolling will scroll the window by the amount of columns
26968 determined by this variable. If its value is a positive integer, scroll that
26969 many columns. If it's a positive floating-point number, it specifies the
26970 fraction of the window's width to scroll. If it's nil or zero, point will be
26971 centered horizontally after the scroll. Any other value, including negative
26972 numbers, are treated as if the value were zero.
26974 Automatic hscrolling always moves point outside the scroll margin, so if
26975 point was more than scroll step columns inside the margin, the window will
26976 scroll more than the value given by the scroll step.
26978 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26979 and `scroll-right' overrides this variable's effect. */);
26980 Vhscroll_step = make_number (0);
26982 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26983 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26984 Bind this around calls to `message' to let it take effect. */);
26985 message_truncate_lines = 0;
26987 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26988 doc: /* Normal hook run to update the menu bar definitions.
26989 Redisplay runs this hook before it redisplays the menu bar.
26990 This is used to update submenus such as Buffers,
26991 whose contents depend on various data. */);
26992 Vmenu_bar_update_hook = Qnil;
26994 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26995 doc: /* Frame for which we are updating a menu.
26996 The enable predicate for a menu binding should check this variable. */);
26997 Vmenu_updating_frame = Qnil;
26999 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
27000 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27001 inhibit_menubar_update = 0;
27003 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
27004 doc: /* Prefix prepended to all continuation lines at display time.
27005 The value may be a string, an image, or a stretch-glyph; it is
27006 interpreted in the same way as the value of a `display' text property.
27008 This variable is overridden by any `wrap-prefix' text or overlay
27009 property.
27011 To add a prefix to non-continuation lines, use `line-prefix'. */);
27012 Vwrap_prefix = Qnil;
27013 staticpro (&Qwrap_prefix);
27014 Qwrap_prefix = intern_c_string ("wrap-prefix");
27015 Fmake_variable_buffer_local (Qwrap_prefix);
27017 DEFVAR_LISP ("line-prefix", &Vline_prefix,
27018 doc: /* Prefix prepended to all non-continuation lines at display time.
27019 The value may be a string, an image, or a stretch-glyph; it is
27020 interpreted in the same way as the value of a `display' text property.
27022 This variable is overridden by any `line-prefix' text or overlay
27023 property.
27025 To add a prefix to continuation lines, use `wrap-prefix'. */);
27026 Vline_prefix = Qnil;
27027 staticpro (&Qline_prefix);
27028 Qline_prefix = intern_c_string ("line-prefix");
27029 Fmake_variable_buffer_local (Qline_prefix);
27031 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
27032 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27033 inhibit_eval_during_redisplay = 0;
27035 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
27036 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27037 inhibit_free_realized_faces = 0;
27039 #if GLYPH_DEBUG
27040 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
27041 doc: /* Inhibit try_window_id display optimization. */);
27042 inhibit_try_window_id = 0;
27044 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
27045 doc: /* Inhibit try_window_reusing display optimization. */);
27046 inhibit_try_window_reusing = 0;
27048 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
27049 doc: /* Inhibit try_cursor_movement display optimization. */);
27050 inhibit_try_cursor_movement = 0;
27051 #endif /* GLYPH_DEBUG */
27053 DEFVAR_INT ("overline-margin", &overline_margin,
27054 doc: /* *Space between overline and text, in pixels.
27055 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27056 margin to the caracter height. */);
27057 overline_margin = 2;
27059 DEFVAR_INT ("underline-minimum-offset",
27060 &underline_minimum_offset,
27061 doc: /* Minimum distance between baseline and underline.
27062 This can improve legibility of underlined text at small font sizes,
27063 particularly when using variable `x-use-underline-position-properties'
27064 with fonts that specify an UNDERLINE_POSITION relatively close to the
27065 baseline. The default value is 1. */);
27066 underline_minimum_offset = 1;
27068 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
27069 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27070 This feature only works when on a window system that can change
27071 cursor shapes. */);
27072 display_hourglass_p = 1;
27074 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
27075 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27076 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27078 hourglass_atimer = NULL;
27079 hourglass_shown_p = 0;
27081 DEFSYM (Qglyphless_char, "glyphless-char");
27082 DEFSYM (Qhex_code, "hex-code");
27083 DEFSYM (Qempty_box, "empty-box");
27084 DEFSYM (Qthin_space, "thin-space");
27085 DEFSYM (Qzero_width, "zero-width");
27087 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27088 /* Intern this now in case it isn't already done.
27089 Setting this variable twice is harmless.
27090 But don't staticpro it here--that is done in alloc.c. */
27091 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27092 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27094 DEFVAR_LISP ("glyphless-char-display", &Vglyphless_char_display,
27095 doc: /* Char-table to control displaying of glyphless characters.
27096 Each element, if non-nil, is an ASCII acronym string (displayed in a box)
27097 or one of these symbols:
27098 hex-code: display the hexadecimal code of a character in a box
27099 empty-box: display as an empty box
27100 thin-space: display as 1-pixel width space
27101 zero-width: don't display
27103 It has one extra slot to control the display of a character for which
27104 no font is found. The value of the slot is `hex-code' or `empty-box'.
27105 The default is `empty-box'. */);
27106 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27107 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27108 Qempty_box);
27112 /* Initialize this module when Emacs starts. */
27114 void
27115 init_xdisp (void)
27117 Lisp_Object root_window;
27118 struct window *mini_w;
27120 current_header_line_height = current_mode_line_height = -1;
27122 CHARPOS (this_line_start_pos) = 0;
27124 mini_w = XWINDOW (minibuf_window);
27125 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27127 if (!noninteractive)
27129 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27130 int i;
27132 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27133 set_window_height (root_window,
27134 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27136 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27137 set_window_height (minibuf_window, 1, 0);
27139 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27140 mini_w->total_cols = make_number (FRAME_COLS (f));
27142 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27143 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27144 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27146 /* The default ellipsis glyphs `...'. */
27147 for (i = 0; i < 3; ++i)
27148 default_invis_vector[i] = make_number ('.');
27152 /* Allocate the buffer for frame titles.
27153 Also used for `format-mode-line'. */
27154 int size = 100;
27155 mode_line_noprop_buf = (char *) xmalloc (size);
27156 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27157 mode_line_noprop_ptr = mode_line_noprop_buf;
27158 mode_line_target = MODE_LINE_DISPLAY;
27161 help_echo_showing_p = 0;
27164 /* Since w32 does not support atimers, it defines its own implementation of
27165 the following three functions in w32fns.c. */
27166 #ifndef WINDOWSNT
27168 /* Platform-independent portion of hourglass implementation. */
27170 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27172 hourglass_started (void)
27174 return hourglass_shown_p || hourglass_atimer != NULL;
27177 /* Cancel a currently active hourglass timer, and start a new one. */
27178 void
27179 start_hourglass (void)
27181 #if defined (HAVE_WINDOW_SYSTEM)
27182 EMACS_TIME delay;
27183 int secs, usecs = 0;
27185 cancel_hourglass ();
27187 if (INTEGERP (Vhourglass_delay)
27188 && XINT (Vhourglass_delay) > 0)
27189 secs = XFASTINT (Vhourglass_delay);
27190 else if (FLOATP (Vhourglass_delay)
27191 && XFLOAT_DATA (Vhourglass_delay) > 0)
27193 Lisp_Object tem;
27194 tem = Ftruncate (Vhourglass_delay, Qnil);
27195 secs = XFASTINT (tem);
27196 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27198 else
27199 secs = DEFAULT_HOURGLASS_DELAY;
27201 EMACS_SET_SECS_USECS (delay, secs, usecs);
27202 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27203 show_hourglass, NULL);
27204 #endif
27208 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27209 shown. */
27210 void
27211 cancel_hourglass (void)
27213 #if defined (HAVE_WINDOW_SYSTEM)
27214 if (hourglass_atimer)
27216 cancel_atimer (hourglass_atimer);
27217 hourglass_atimer = NULL;
27220 if (hourglass_shown_p)
27221 hide_hourglass ();
27222 #endif
27224 #endif /* ! WINDOWSNT */