Start redesigning portable mouse highlight. Not compiled.
[emacs.git] / src / xdisp.c
blob97c2caeaeb7cca6c9d101e334023ad22da894b58
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 with suitably computed width. Both the blanks and the
218 stretch glyph are given the face of the background of the line.
219 This way, the terminal-specific back-end can still draw the glyphs
220 left to right, even for R2L lines.
222 Note one important detail mentioned above: that the bidi reordering
223 engine, driven by the iterator, produces characters in R2L rows
224 starting at the character that will be the rightmost on display.
225 As far as the iterator is concerned, the geometry of such rows is
226 still left to right, i.e. the iterator "thinks" the first character
227 is at the leftmost pixel position. The iterator does not know that
228 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
229 delivers. This is important when functions from the the move_it_*
230 family are used to get to certain screen position or to match
231 screen coordinates with buffer coordinates: these functions use the
232 iterator geometry, which is left to right even in R2L paragraphs.
233 This works well with most callers of move_it_*, because they need
234 to get to a specific column, and columns are still numbered in the
235 reading order, i.e. the rightmost character in a R2L paragraph is
236 still column zero. But some callers do not get well with this; a
237 notable example is mouse clicks that need to find the character
238 that corresponds to certain pixel coordinates. See
239 buffer_posn_from_coords in dispnew.c for how this is handled. */
241 #include <config.h>
242 #include <stdio.h>
243 #include <limits.h>
244 #include <setjmp.h>
246 #include "lisp.h"
247 #include "keyboard.h"
248 #include "frame.h"
249 #include "window.h"
250 #include "termchar.h"
251 #include "dispextern.h"
252 #include "buffer.h"
253 #include "character.h"
254 #include "charset.h"
255 #include "indent.h"
256 #include "commands.h"
257 #include "keymap.h"
258 #include "macros.h"
259 #include "disptab.h"
260 #include "termhooks.h"
261 #include "termopts.h"
262 #include "intervals.h"
263 #include "coding.h"
264 #include "process.h"
265 #include "region-cache.h"
266 #include "font.h"
267 #include "fontset.h"
268 #include "blockinput.h"
270 #ifdef HAVE_X_WINDOWS
271 #include "xterm.h"
272 #endif
273 #ifdef WINDOWSNT
274 #include "w32term.h"
275 #endif
276 #ifdef HAVE_NS
277 #include "nsterm.h"
278 #endif
279 #ifdef USE_GTK
280 #include "gtkutil.h"
281 #endif
283 #include "font.h"
285 #ifndef FRAME_X_OUTPUT
286 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
287 #endif
289 #define INFINITY 10000000
291 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
292 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
293 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
294 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
295 Lisp_Object Qinhibit_point_motion_hooks;
296 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
297 Lisp_Object Qfontified;
298 Lisp_Object Qgrow_only;
299 Lisp_Object Qinhibit_eval_during_redisplay;
300 Lisp_Object Qbuffer_position, Qposition, Qobject;
301 Lisp_Object Qright_to_left, Qleft_to_right;
303 /* Cursor shapes */
304 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
306 /* Pointer shapes */
307 Lisp_Object Qarrow, Qhand, Qtext;
309 Lisp_Object Qrisky_local_variable;
311 /* Holds the list (error). */
312 Lisp_Object list_of_error;
314 /* Functions called to fontify regions of text. */
316 Lisp_Object Vfontification_functions;
317 Lisp_Object Qfontification_functions;
319 /* Non-nil means automatically select any window when the mouse
320 cursor moves into it. */
321 Lisp_Object Vmouse_autoselect_window;
323 Lisp_Object Vwrap_prefix, Qwrap_prefix;
324 Lisp_Object Vline_prefix, Qline_prefix;
326 /* Non-zero means draw tool bar buttons raised when the mouse moves
327 over them. */
329 int auto_raise_tool_bar_buttons_p;
331 /* Non-zero means to reposition window if cursor line is only partially visible. */
333 int make_cursor_line_fully_visible_p;
335 /* Margin below tool bar in pixels. 0 or nil means no margin.
336 If value is `internal-border-width' or `border-width',
337 the corresponding frame parameter is used. */
339 Lisp_Object Vtool_bar_border;
341 /* Margin around tool bar buttons in pixels. */
343 Lisp_Object Vtool_bar_button_margin;
345 /* Thickness of shadow to draw around tool bar buttons. */
347 EMACS_INT tool_bar_button_relief;
349 /* Non-nil means automatically resize tool-bars so that all tool-bar
350 items are visible, and no blank lines remain.
352 If value is `grow-only', only make tool-bar bigger. */
354 Lisp_Object Vauto_resize_tool_bars;
356 /* Type of tool bar. Can be symbols image, text, both or both-hroiz. */
358 Lisp_Object Vtool_bar_style;
360 /* Maximum number of characters a label can have to be shown. */
362 EMACS_INT tool_bar_max_label_size;
364 /* Non-zero means draw block and hollow cursor as wide as the glyph
365 under it. For example, if a block cursor is over a tab, it will be
366 drawn as wide as that tab on the display. */
368 int x_stretch_cursor_p;
370 /* Non-nil means don't actually do any redisplay. */
372 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
374 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
376 int inhibit_eval_during_redisplay;
378 /* Names of text properties relevant for redisplay. */
380 Lisp_Object Qdisplay;
382 /* Symbols used in text property values. */
384 Lisp_Object Vdisplay_pixels_per_inch;
385 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
386 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
387 Lisp_Object Qslice;
388 Lisp_Object Qcenter;
389 Lisp_Object Qmargin, Qpointer;
390 Lisp_Object Qline_height;
392 /* Non-nil means highlight trailing whitespace. */
394 Lisp_Object Vshow_trailing_whitespace;
396 /* Non-nil means escape non-break space and hyphens. */
398 Lisp_Object Vnobreak_char_display;
400 #ifdef HAVE_WINDOW_SYSTEM
402 /* Test if overflow newline into fringe. Called with iterator IT
403 at or past right window margin, and with IT->current_x set. */
405 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
406 (!NILP (Voverflow_newline_into_fringe) \
407 && FRAME_WINDOW_P ((IT)->f) \
408 && ((IT)->bidi_it.paragraph_dir == R2L \
409 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
410 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
411 && (IT)->current_x == (IT)->last_visible_x \
412 && (IT)->line_wrap != WORD_WRAP)
414 #else /* !HAVE_WINDOW_SYSTEM */
415 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
416 #endif /* HAVE_WINDOW_SYSTEM */
418 /* Test if the display element loaded in IT is a space or tab
419 character. This is used to determine word wrapping. */
421 #define IT_DISPLAYING_WHITESPACE(it) \
422 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
424 /* Non-nil means show the text cursor in void text areas
425 i.e. in blank areas after eol and eob. This used to be
426 the default in 21.3. */
428 Lisp_Object Vvoid_text_area_pointer;
430 /* Name of the face used to highlight trailing whitespace. */
432 Lisp_Object Qtrailing_whitespace;
434 /* Name and number of the face used to highlight escape glyphs. */
436 Lisp_Object Qescape_glyph;
438 /* Name and number of the face used to highlight non-breaking spaces. */
440 Lisp_Object Qnobreak_space;
442 /* The symbol `image' which is the car of the lists used to represent
443 images in Lisp. Also a tool bar style. */
445 Lisp_Object Qimage;
447 /* The image map types. */
448 Lisp_Object QCmap, QCpointer;
449 Lisp_Object Qrect, Qcircle, Qpoly;
451 /* Tool bar styles */
452 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
454 /* Non-zero means print newline to stdout before next mini-buffer
455 message. */
457 int noninteractive_need_newline;
459 /* Non-zero means print newline to message log before next message. */
461 static int message_log_need_newline;
463 /* Three markers that message_dolog uses.
464 It could allocate them itself, but that causes trouble
465 in handling memory-full errors. */
466 static Lisp_Object message_dolog_marker1;
467 static Lisp_Object message_dolog_marker2;
468 static Lisp_Object message_dolog_marker3;
470 /* The buffer position of the first character appearing entirely or
471 partially on the line of the selected window which contains the
472 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
473 redisplay optimization in redisplay_internal. */
475 static struct text_pos this_line_start_pos;
477 /* Number of characters past the end of the line above, including the
478 terminating newline. */
480 static struct text_pos this_line_end_pos;
482 /* The vertical positions and the height of this line. */
484 static int this_line_vpos;
485 static int this_line_y;
486 static int this_line_pixel_height;
488 /* X position at which this display line starts. Usually zero;
489 negative if first character is partially visible. */
491 static int this_line_start_x;
493 /* Buffer that this_line_.* variables are referring to. */
495 static struct buffer *this_line_buffer;
497 /* Nonzero means truncate lines in all windows less wide than the
498 frame. */
500 Lisp_Object Vtruncate_partial_width_windows;
502 /* A flag to control how to display unibyte 8-bit character. */
504 int unibyte_display_via_language_environment;
506 /* Nonzero means we have more than one non-mini-buffer-only frame.
507 Not guaranteed to be accurate except while parsing
508 frame-title-format. */
510 int multiple_frames;
512 Lisp_Object Vglobal_mode_string;
515 /* List of variables (symbols) which hold markers for overlay arrows.
516 The symbols on this list are examined during redisplay to determine
517 where to display overlay arrows. */
519 Lisp_Object Voverlay_arrow_variable_list;
521 /* Marker for where to display an arrow on top of the buffer text. */
523 Lisp_Object Voverlay_arrow_position;
525 /* String to display for the arrow. Only used on terminal frames. */
527 Lisp_Object Voverlay_arrow_string;
529 /* Values of those variables at last redisplay are stored as
530 properties on `overlay-arrow-position' symbol. However, if
531 Voverlay_arrow_position is a marker, last-arrow-position is its
532 numerical position. */
534 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
536 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
537 properties on a symbol in overlay-arrow-variable-list. */
539 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
541 /* Like mode-line-format, but for the title bar on a visible frame. */
543 Lisp_Object Vframe_title_format;
545 /* Like mode-line-format, but for the title bar on an iconified frame. */
547 Lisp_Object Vicon_title_format;
549 /* List of functions to call when a window's size changes. These
550 functions get one arg, a frame on which one or more windows' sizes
551 have changed. */
553 static Lisp_Object Vwindow_size_change_functions;
555 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
557 /* Nonzero if an overlay arrow has been displayed in this window. */
559 static int overlay_arrow_seen;
561 /* Nonzero means highlight the region even in nonselected windows. */
563 int highlight_nonselected_windows;
565 /* If cursor motion alone moves point off frame, try scrolling this
566 many lines up or down if that will bring it back. */
568 static EMACS_INT scroll_step;
570 /* Nonzero means scroll just far enough to bring point back on the
571 screen, when appropriate. */
573 static EMACS_INT scroll_conservatively;
575 /* Recenter the window whenever point gets within this many lines of
576 the top or bottom of the window. This value is translated into a
577 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
578 that there is really a fixed pixel height scroll margin. */
580 EMACS_INT scroll_margin;
582 /* Number of windows showing the buffer of the selected window (or
583 another buffer with the same base buffer). keyboard.c refers to
584 this. */
586 int buffer_shared;
588 /* Vector containing glyphs for an ellipsis `...'. */
590 static Lisp_Object default_invis_vector[3];
592 /* Zero means display the mode-line/header-line/menu-bar in the default face
593 (this slightly odd definition is for compatibility with previous versions
594 of emacs), non-zero means display them using their respective faces.
596 This variable is deprecated. */
598 int mode_line_inverse_video;
600 /* Prompt to display in front of the mini-buffer contents. */
602 Lisp_Object minibuf_prompt;
604 /* Width of current mini-buffer prompt. Only set after display_line
605 of the line that contains the prompt. */
607 int minibuf_prompt_width;
609 /* This is the window where the echo area message was displayed. It
610 is always a mini-buffer window, but it may not be the same window
611 currently active as a mini-buffer. */
613 Lisp_Object echo_area_window;
615 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
616 pushes the current message and the value of
617 message_enable_multibyte on the stack, the function restore_message
618 pops the stack and displays MESSAGE again. */
620 Lisp_Object Vmessage_stack;
622 /* Nonzero means multibyte characters were enabled when the echo area
623 message was specified. */
625 int message_enable_multibyte;
627 /* Nonzero if we should redraw the mode lines on the next redisplay. */
629 int update_mode_lines;
631 /* Nonzero if window sizes or contents have changed since last
632 redisplay that finished. */
634 int windows_or_buffers_changed;
636 /* Nonzero means a frame's cursor type has been changed. */
638 int cursor_type_changed;
640 /* Nonzero after display_mode_line if %l was used and it displayed a
641 line number. */
643 int line_number_displayed;
645 /* Maximum buffer size for which to display line numbers. */
647 Lisp_Object Vline_number_display_limit;
649 /* Line width to consider when repositioning for line number display. */
651 static EMACS_INT line_number_display_limit_width;
653 /* Number of lines to keep in the message log buffer. t means
654 infinite. nil means don't log at all. */
656 Lisp_Object Vmessage_log_max;
658 /* The name of the *Messages* buffer, a string. */
660 static Lisp_Object Vmessages_buffer_name;
662 /* Current, index 0, and last displayed echo area message. Either
663 buffers from echo_buffers, or nil to indicate no message. */
665 Lisp_Object echo_area_buffer[2];
667 /* The buffers referenced from echo_area_buffer. */
669 static Lisp_Object echo_buffer[2];
671 /* A vector saved used in with_area_buffer to reduce consing. */
673 static Lisp_Object Vwith_echo_area_save_vector;
675 /* Non-zero means display_echo_area should display the last echo area
676 message again. Set by redisplay_preserve_echo_area. */
678 static int display_last_displayed_message_p;
680 /* Nonzero if echo area is being used by print; zero if being used by
681 message. */
683 int message_buf_print;
685 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
687 Lisp_Object Qinhibit_menubar_update;
688 int inhibit_menubar_update;
690 /* When evaluating expressions from menu bar items (enable conditions,
691 for instance), this is the frame they are being processed for. */
693 Lisp_Object Vmenu_updating_frame;
695 /* Maximum height for resizing mini-windows. Either a float
696 specifying a fraction of the available height, or an integer
697 specifying a number of lines. */
699 Lisp_Object Vmax_mini_window_height;
701 /* Non-zero means messages should be displayed with truncated
702 lines instead of being continued. */
704 int message_truncate_lines;
705 Lisp_Object Qmessage_truncate_lines;
707 /* Set to 1 in clear_message to make redisplay_internal aware
708 of an emptied echo area. */
710 static int message_cleared_p;
712 /* How to blink the default frame cursor off. */
713 Lisp_Object Vblink_cursor_alist;
715 /* A scratch glyph row with contents used for generating truncation
716 glyphs. Also used in direct_output_for_insert. */
718 #define MAX_SCRATCH_GLYPHS 100
719 struct glyph_row scratch_glyph_row;
720 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
722 /* Ascent and height of the last line processed by move_it_to. */
724 static int last_max_ascent, last_height;
726 /* Non-zero if there's a help-echo in the echo area. */
728 int help_echo_showing_p;
730 /* If >= 0, computed, exact values of mode-line and header-line height
731 to use in the macros CURRENT_MODE_LINE_HEIGHT and
732 CURRENT_HEADER_LINE_HEIGHT. */
734 int current_mode_line_height, current_header_line_height;
736 /* The maximum distance to look ahead for text properties. Values
737 that are too small let us call compute_char_face and similar
738 functions too often which is expensive. Values that are too large
739 let us call compute_char_face and alike too often because we
740 might not be interested in text properties that far away. */
742 #define TEXT_PROP_DISTANCE_LIMIT 100
744 #if GLYPH_DEBUG
746 /* Variables to turn off display optimizations from Lisp. */
748 int inhibit_try_window_id, inhibit_try_window_reusing;
749 int inhibit_try_cursor_movement;
751 /* Non-zero means print traces of redisplay if compiled with
752 GLYPH_DEBUG != 0. */
754 int trace_redisplay_p;
756 #endif /* GLYPH_DEBUG */
758 #ifdef DEBUG_TRACE_MOVE
759 /* Non-zero means trace with TRACE_MOVE to stderr. */
760 int trace_move;
762 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
763 #else
764 #define TRACE_MOVE(x) (void) 0
765 #endif
767 /* Non-zero means automatically scroll windows horizontally to make
768 point visible. */
770 int automatic_hscrolling_p;
771 Lisp_Object Qauto_hscroll_mode;
773 /* How close to the margin can point get before the window is scrolled
774 horizontally. */
775 EMACS_INT hscroll_margin;
777 /* How much to scroll horizontally when point is inside the above margin. */
778 Lisp_Object Vhscroll_step;
780 /* The variable `resize-mini-windows'. If nil, don't resize
781 mini-windows. If t, always resize them to fit the text they
782 display. If `grow-only', let mini-windows grow only until they
783 become empty. */
785 Lisp_Object Vresize_mini_windows;
787 /* Buffer being redisplayed -- for redisplay_window_error. */
789 struct buffer *displayed_buffer;
791 /* Space between overline and text. */
793 EMACS_INT overline_margin;
795 /* Require underline to be at least this many screen pixels below baseline
796 This to avoid underline "merging" with the base of letters at small
797 font sizes, particularly when x_use_underline_position_properties is on. */
799 EMACS_INT underline_minimum_offset;
801 /* Value returned from text property handlers (see below). */
803 enum prop_handled
805 HANDLED_NORMALLY,
806 HANDLED_RECOMPUTE_PROPS,
807 HANDLED_OVERLAY_STRING_CONSUMED,
808 HANDLED_RETURN
811 /* A description of text properties that redisplay is interested
812 in. */
814 struct props
816 /* The name of the property. */
817 Lisp_Object *name;
819 /* A unique index for the property. */
820 enum prop_idx idx;
822 /* A handler function called to set up iterator IT from the property
823 at IT's current position. Value is used to steer handle_stop. */
824 enum prop_handled (*handler) (struct it *it);
827 static enum prop_handled handle_face_prop (struct it *);
828 static enum prop_handled handle_invisible_prop (struct it *);
829 static enum prop_handled handle_display_prop (struct it *);
830 static enum prop_handled handle_composition_prop (struct it *);
831 static enum prop_handled handle_overlay_change (struct it *);
832 static enum prop_handled handle_fontified_prop (struct it *);
834 /* Properties handled by iterators. */
836 static struct props it_props[] =
838 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
839 /* Handle `face' before `display' because some sub-properties of
840 `display' need to know the face. */
841 {&Qface, FACE_PROP_IDX, handle_face_prop},
842 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
843 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
844 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
845 {NULL, 0, NULL}
848 /* Value is the position described by X. If X is a marker, value is
849 the marker_position of X. Otherwise, value is X. */
851 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
853 /* Enumeration returned by some move_it_.* functions internally. */
855 enum move_it_result
857 /* Not used. Undefined value. */
858 MOVE_UNDEFINED,
860 /* Move ended at the requested buffer position or ZV. */
861 MOVE_POS_MATCH_OR_ZV,
863 /* Move ended at the requested X pixel position. */
864 MOVE_X_REACHED,
866 /* Move within a line ended at the end of a line that must be
867 continued. */
868 MOVE_LINE_CONTINUED,
870 /* Move within a line ended at the end of a line that would
871 be displayed truncated. */
872 MOVE_LINE_TRUNCATED,
874 /* Move within a line ended at a line end. */
875 MOVE_NEWLINE_OR_CR
878 /* This counter is used to clear the face cache every once in a while
879 in redisplay_internal. It is incremented for each redisplay.
880 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
881 cleared. */
883 #define CLEAR_FACE_CACHE_COUNT 500
884 static int clear_face_cache_count;
886 /* Similarly for the image cache. */
888 #ifdef HAVE_WINDOW_SYSTEM
889 #define CLEAR_IMAGE_CACHE_COUNT 101
890 static int clear_image_cache_count;
891 #endif
893 /* Non-zero while redisplay_internal is in progress. */
895 int redisplaying_p;
897 /* Non-zero means don't free realized faces. Bound while freeing
898 realized faces is dangerous because glyph matrices might still
899 reference them. */
901 int inhibit_free_realized_faces;
902 Lisp_Object Qinhibit_free_realized_faces;
904 /* If a string, XTread_socket generates an event to display that string.
905 (The display is done in read_char.) */
907 Lisp_Object help_echo_string;
908 Lisp_Object help_echo_window;
909 Lisp_Object help_echo_object;
910 EMACS_INT help_echo_pos;
912 /* Temporary variable for XTread_socket. */
914 Lisp_Object previous_help_echo_string;
916 /* Null glyph slice */
918 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
920 /* Platform-independent portion of hourglass implementation. */
922 /* Non-zero means we're allowed to display a hourglass pointer. */
923 int display_hourglass_p;
925 /* Non-zero means an hourglass cursor is currently shown. */
926 int hourglass_shown_p;
928 /* If non-null, an asynchronous timer that, when it expires, displays
929 an hourglass cursor on all frames. */
930 struct atimer *hourglass_atimer;
932 /* Number of seconds to wait before displaying an hourglass cursor. */
933 Lisp_Object Vhourglass_delay;
935 /* Default number of seconds to wait before displaying an hourglass
936 cursor. */
937 #define DEFAULT_HOURGLASS_DELAY 1
940 /* Function prototypes. */
942 static void setup_for_ellipsis (struct it *, int);
943 static void mark_window_display_accurate_1 (struct window *, int);
944 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
945 static int display_prop_string_p (Lisp_Object, Lisp_Object);
946 static int cursor_row_p (struct window *, struct glyph_row *);
947 static int redisplay_mode_lines (Lisp_Object, int);
948 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
950 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
952 static void handle_line_prefix (struct it *);
954 static void pint2str (char *, int, int);
955 static void pint2hrstr (char *, int, int);
956 static struct text_pos run_window_scroll_functions (Lisp_Object,
957 struct text_pos);
958 static void reconsider_clip_changes (struct window *, struct buffer *);
959 static int text_outside_line_unchanged_p (struct window *,
960 EMACS_INT, EMACS_INT);
961 static void store_mode_line_noprop_char (char);
962 static int store_mode_line_noprop (const unsigned char *, int, int);
963 static void x_consider_frame_title (Lisp_Object);
964 static void handle_stop (struct it *);
965 static void handle_stop_backwards (struct it *, EMACS_INT);
966 static int tool_bar_lines_needed (struct frame *, int *);
967 static int single_display_spec_intangible_p (Lisp_Object);
968 static void ensure_echo_area_buffers (void);
969 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
970 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
971 static int with_echo_area_buffer (struct window *, int,
972 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
973 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
974 static void clear_garbaged_frames (void);
975 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
976 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
977 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
978 static int display_echo_area (struct window *);
979 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
980 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
981 static Lisp_Object unwind_redisplay (Lisp_Object);
982 static int string_char_and_length (const unsigned char *, int *);
983 static struct text_pos display_prop_end (struct it *, Lisp_Object,
984 struct text_pos);
985 static int compute_window_start_on_continuation_line (struct window *);
986 static Lisp_Object safe_eval_handler (Lisp_Object);
987 static void insert_left_trunc_glyphs (struct it *);
988 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
989 Lisp_Object);
990 static void extend_face_to_end_of_line (struct it *);
991 static int append_space_for_newline (struct it *, int);
992 static int cursor_row_fully_visible_p (struct window *, int, int);
993 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
994 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
995 static int trailing_whitespace_p (EMACS_INT);
996 static int message_log_check_duplicate (EMACS_INT, EMACS_INT,
997 EMACS_INT, EMACS_INT);
998 static void push_it (struct it *);
999 static void pop_it (struct it *);
1000 static void sync_frame_with_window_matrix_rows (struct window *);
1001 static void select_frame_for_redisplay (Lisp_Object);
1002 static void redisplay_internal (int);
1003 static int echo_area_display (int);
1004 static void redisplay_windows (Lisp_Object);
1005 static void redisplay_window (Lisp_Object, int);
1006 static Lisp_Object redisplay_window_error (Lisp_Object);
1007 static Lisp_Object redisplay_window_0 (Lisp_Object);
1008 static Lisp_Object redisplay_window_1 (Lisp_Object);
1009 static int update_menu_bar (struct frame *, int, int);
1010 static int try_window_reusing_current_matrix (struct window *);
1011 static int try_window_id (struct window *);
1012 static int display_line (struct it *);
1013 static int display_mode_lines (struct window *);
1014 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
1015 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
1016 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
1017 static const char *decode_mode_spec (struct window *, int, int, int,
1018 Lisp_Object *);
1019 static void display_menu_bar (struct window *);
1020 static int display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT, int,
1021 EMACS_INT *);
1022 static int display_string (const unsigned char *, Lisp_Object, Lisp_Object,
1023 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
1024 static void compute_line_metrics (struct it *);
1025 static void run_redisplay_end_trigger_hook (struct it *);
1026 static int get_overlay_strings (struct it *, EMACS_INT);
1027 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
1028 static void next_overlay_string (struct it *);
1029 static void reseat (struct it *, struct text_pos, int);
1030 static void reseat_1 (struct it *, struct text_pos, int);
1031 static void back_to_previous_visible_line_start (struct it *);
1032 void reseat_at_previous_visible_line_start (struct it *);
1033 static void reseat_at_next_visible_line_start (struct it *, int);
1034 static int next_element_from_ellipsis (struct it *);
1035 static int next_element_from_display_vector (struct it *);
1036 static int next_element_from_string (struct it *);
1037 static int next_element_from_c_string (struct it *);
1038 static int next_element_from_buffer (struct it *);
1039 static int next_element_from_composition (struct it *);
1040 static int next_element_from_image (struct it *);
1041 static int next_element_from_stretch (struct it *);
1042 static void load_overlay_strings (struct it *, EMACS_INT);
1043 static int init_from_display_pos (struct it *, struct window *,
1044 struct display_pos *);
1045 static void reseat_to_string (struct it *, const unsigned char *,
1046 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
1047 static enum move_it_result
1048 move_it_in_display_line_to (struct it *, EMACS_INT, int,
1049 enum move_operation_enum);
1050 void move_it_vertically_backward (struct it *, int);
1051 static void init_to_row_start (struct it *, struct window *,
1052 struct glyph_row *);
1053 static int init_to_row_end (struct it *, struct window *,
1054 struct glyph_row *);
1055 static void back_to_previous_line_start (struct it *);
1056 static int forward_to_next_line_start (struct it *, int *);
1057 static struct text_pos string_pos_nchars_ahead (struct text_pos,
1058 Lisp_Object, EMACS_INT);
1059 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
1060 static struct text_pos c_string_pos (EMACS_INT, const unsigned char *, int);
1061 static EMACS_INT number_of_chars (const unsigned char *, int);
1062 static void compute_stop_pos (struct it *);
1063 static void compute_string_pos (struct text_pos *, struct text_pos,
1064 Lisp_Object);
1065 static int face_before_or_after_it_pos (struct it *, int);
1066 static EMACS_INT next_overlay_change (EMACS_INT);
1067 static int handle_single_display_spec (struct it *, Lisp_Object,
1068 Lisp_Object, Lisp_Object,
1069 struct text_pos *, int);
1070 static int underlying_face_id (struct it *);
1071 static int in_ellipses_for_invisible_text_p (struct display_pos *,
1072 struct window *);
1074 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1075 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1077 #ifdef HAVE_WINDOW_SYSTEM
1079 static void update_tool_bar (struct frame *, int);
1080 static void build_desired_tool_bar_string (struct frame *f);
1081 static int redisplay_tool_bar (struct frame *);
1082 static void display_tool_bar_line (struct it *, int);
1083 static void notice_overwritten_cursor (struct window *,
1084 enum glyph_row_area,
1085 int, int, int, int);
1086 static void append_stretch_glyph (struct it *, Lisp_Object,
1087 int, int, int);
1090 #endif /* HAVE_WINDOW_SYSTEM */
1092 static int coords_in_mouse_face_p (struct window *, int, int);
1096 /***********************************************************************
1097 Window display dimensions
1098 ***********************************************************************/
1100 /* Return the bottom boundary y-position for text lines in window W.
1101 This is the first y position at which a line cannot start.
1102 It is relative to the top of the window.
1104 This is the height of W minus the height of a mode line, if any. */
1106 INLINE int
1107 window_text_bottom_y (struct window *w)
1109 int height = WINDOW_TOTAL_HEIGHT (w);
1111 if (WINDOW_WANTS_MODELINE_P (w))
1112 height -= CURRENT_MODE_LINE_HEIGHT (w);
1113 return height;
1116 /* Return the pixel width of display area AREA of window W. AREA < 0
1117 means return the total width of W, not including fringes to
1118 the left and right of the window. */
1120 INLINE int
1121 window_box_width (struct window *w, int area)
1123 int cols = XFASTINT (w->total_cols);
1124 int pixels = 0;
1126 if (!w->pseudo_window_p)
1128 cols -= WINDOW_SCROLL_BAR_COLS (w);
1130 if (area == TEXT_AREA)
1132 if (INTEGERP (w->left_margin_cols))
1133 cols -= XFASTINT (w->left_margin_cols);
1134 if (INTEGERP (w->right_margin_cols))
1135 cols -= XFASTINT (w->right_margin_cols);
1136 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1138 else if (area == LEFT_MARGIN_AREA)
1140 cols = (INTEGERP (w->left_margin_cols)
1141 ? XFASTINT (w->left_margin_cols) : 0);
1142 pixels = 0;
1144 else if (area == RIGHT_MARGIN_AREA)
1146 cols = (INTEGERP (w->right_margin_cols)
1147 ? XFASTINT (w->right_margin_cols) : 0);
1148 pixels = 0;
1152 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1156 /* Return the pixel height of the display area of window W, not
1157 including mode lines of W, if any. */
1159 INLINE int
1160 window_box_height (struct window *w)
1162 struct frame *f = XFRAME (w->frame);
1163 int height = WINDOW_TOTAL_HEIGHT (w);
1165 xassert (height >= 0);
1167 /* Note: the code below that determines the mode-line/header-line
1168 height is essentially the same as that contained in the macro
1169 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1170 the appropriate glyph row has its `mode_line_p' flag set,
1171 and if it doesn't, uses estimate_mode_line_height instead. */
1173 if (WINDOW_WANTS_MODELINE_P (w))
1175 struct glyph_row *ml_row
1176 = (w->current_matrix && w->current_matrix->rows
1177 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1178 : 0);
1179 if (ml_row && ml_row->mode_line_p)
1180 height -= ml_row->height;
1181 else
1182 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1185 if (WINDOW_WANTS_HEADER_LINE_P (w))
1187 struct glyph_row *hl_row
1188 = (w->current_matrix && w->current_matrix->rows
1189 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1190 : 0);
1191 if (hl_row && hl_row->mode_line_p)
1192 height -= hl_row->height;
1193 else
1194 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1197 /* With a very small font and a mode-line that's taller than
1198 default, we might end up with a negative height. */
1199 return max (0, height);
1202 /* Return the window-relative coordinate of the left edge of display
1203 area AREA of window W. AREA < 0 means return the left edge of the
1204 whole window, to the right of the left fringe of W. */
1206 INLINE int
1207 window_box_left_offset (struct window *w, int area)
1209 int x;
1211 if (w->pseudo_window_p)
1212 return 0;
1214 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1216 if (area == TEXT_AREA)
1217 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1218 + window_box_width (w, LEFT_MARGIN_AREA));
1219 else if (area == RIGHT_MARGIN_AREA)
1220 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1221 + window_box_width (w, LEFT_MARGIN_AREA)
1222 + window_box_width (w, TEXT_AREA)
1223 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1225 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1226 else if (area == LEFT_MARGIN_AREA
1227 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1228 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1230 return x;
1234 /* Return the window-relative coordinate of the right edge of display
1235 area AREA of window W. AREA < 0 means return the right edge of the
1236 whole window, to the left of the right fringe of W. */
1238 INLINE int
1239 window_box_right_offset (struct window *w, int area)
1241 return window_box_left_offset (w, area) + window_box_width (w, area);
1244 /* Return the frame-relative coordinate of the left edge of display
1245 area AREA of window W. AREA < 0 means return the left edge of the
1246 whole window, to the right of the left fringe of W. */
1248 INLINE int
1249 window_box_left (struct window *w, int area)
1251 struct frame *f = XFRAME (w->frame);
1252 int x;
1254 if (w->pseudo_window_p)
1255 return FRAME_INTERNAL_BORDER_WIDTH (f);
1257 x = (WINDOW_LEFT_EDGE_X (w)
1258 + window_box_left_offset (w, area));
1260 return x;
1264 /* Return the frame-relative coordinate of the right edge of display
1265 area AREA of window W. AREA < 0 means return the right edge of the
1266 whole window, to the left of the right fringe of W. */
1268 INLINE int
1269 window_box_right (struct window *w, int area)
1271 return window_box_left (w, area) + window_box_width (w, area);
1274 /* Get the bounding box of the display area AREA of window W, without
1275 mode lines, in frame-relative coordinates. AREA < 0 means the
1276 whole window, not including the left and right fringes of
1277 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1278 coordinates of the upper-left corner of the box. Return in
1279 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1281 INLINE void
1282 window_box (struct window *w, int area, int *box_x, int *box_y,
1283 int *box_width, int *box_height)
1285 if (box_width)
1286 *box_width = window_box_width (w, area);
1287 if (box_height)
1288 *box_height = window_box_height (w);
1289 if (box_x)
1290 *box_x = window_box_left (w, area);
1291 if (box_y)
1293 *box_y = WINDOW_TOP_EDGE_Y (w);
1294 if (WINDOW_WANTS_HEADER_LINE_P (w))
1295 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1300 /* Get the bounding box of the display area AREA of window W, without
1301 mode lines. AREA < 0 means the whole window, not including the
1302 left and right fringe of the window. Return in *TOP_LEFT_X
1303 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1304 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1305 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1306 box. */
1308 INLINE void
1309 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1310 int *bottom_right_x, int *bottom_right_y)
1312 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1313 bottom_right_y);
1314 *bottom_right_x += *top_left_x;
1315 *bottom_right_y += *top_left_y;
1320 /***********************************************************************
1321 Utilities
1322 ***********************************************************************/
1324 /* Return the bottom y-position of the line the iterator IT is in.
1325 This can modify IT's settings. */
1328 line_bottom_y (struct it *it)
1330 int line_height = it->max_ascent + it->max_descent;
1331 int line_top_y = it->current_y;
1333 if (line_height == 0)
1335 if (last_height)
1336 line_height = last_height;
1337 else if (IT_CHARPOS (*it) < ZV)
1339 move_it_by_lines (it, 1, 1);
1340 line_height = (it->max_ascent || it->max_descent
1341 ? it->max_ascent + it->max_descent
1342 : last_height);
1344 else
1346 struct glyph_row *row = it->glyph_row;
1348 /* Use the default character height. */
1349 it->glyph_row = NULL;
1350 it->what = IT_CHARACTER;
1351 it->c = ' ';
1352 it->len = 1;
1353 PRODUCE_GLYPHS (it);
1354 line_height = it->ascent + it->descent;
1355 it->glyph_row = row;
1359 return line_top_y + line_height;
1363 /* Return 1 if position CHARPOS is visible in window W.
1364 CHARPOS < 0 means return info about WINDOW_END position.
1365 If visible, set *X and *Y to pixel coordinates of top left corner.
1366 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1367 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1370 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1371 int *rtop, int *rbot, int *rowh, int *vpos)
1373 struct it it;
1374 struct text_pos top;
1375 int visible_p = 0;
1376 struct buffer *old_buffer = NULL;
1378 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1379 return visible_p;
1381 if (XBUFFER (w->buffer) != current_buffer)
1383 old_buffer = current_buffer;
1384 set_buffer_internal_1 (XBUFFER (w->buffer));
1387 SET_TEXT_POS_FROM_MARKER (top, w->start);
1389 /* Compute exact mode line heights. */
1390 if (WINDOW_WANTS_MODELINE_P (w))
1391 current_mode_line_height
1392 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1393 current_buffer->mode_line_format);
1395 if (WINDOW_WANTS_HEADER_LINE_P (w))
1396 current_header_line_height
1397 = display_mode_line (w, HEADER_LINE_FACE_ID,
1398 current_buffer->header_line_format);
1400 start_display (&it, w, top);
1401 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1402 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1404 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1406 /* We have reached CHARPOS, or passed it. How the call to
1407 move_it_to can overshoot: (i) If CHARPOS is on invisible
1408 text, move_it_to stops at the end of the invisible text,
1409 after CHARPOS. (ii) If CHARPOS is in a display vector,
1410 move_it_to stops on its last glyph. */
1411 int top_x = it.current_x;
1412 int top_y = it.current_y;
1413 enum it_method it_method = it.method;
1414 /* Calling line_bottom_y may change it.method, it.position, etc. */
1415 int bottom_y = (last_height = 0, line_bottom_y (&it));
1416 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1418 if (top_y < window_top_y)
1419 visible_p = bottom_y > window_top_y;
1420 else if (top_y < it.last_visible_y)
1421 visible_p = 1;
1422 if (visible_p)
1424 if (it_method == GET_FROM_DISPLAY_VECTOR)
1426 /* We stopped on the last glyph of a display vector.
1427 Try and recompute. Hack alert! */
1428 if (charpos < 2 || top.charpos >= charpos)
1429 top_x = it.glyph_row->x;
1430 else
1432 struct it it2;
1433 start_display (&it2, w, top);
1434 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1435 get_next_display_element (&it2);
1436 PRODUCE_GLYPHS (&it2);
1437 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1438 || it2.current_x > it2.last_visible_x)
1439 top_x = it.glyph_row->x;
1440 else
1442 top_x = it2.current_x;
1443 top_y = it2.current_y;
1448 *x = top_x;
1449 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1450 *rtop = max (0, window_top_y - top_y);
1451 *rbot = max (0, bottom_y - it.last_visible_y);
1452 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1453 - max (top_y, window_top_y)));
1454 *vpos = it.vpos;
1457 else
1459 struct it it2;
1461 it2 = it;
1462 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1463 move_it_by_lines (&it, 1, 0);
1464 if (charpos < IT_CHARPOS (it)
1465 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1467 visible_p = 1;
1468 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1469 *x = it2.current_x;
1470 *y = it2.current_y + it2.max_ascent - it2.ascent;
1471 *rtop = max (0, -it2.current_y);
1472 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1473 - it.last_visible_y));
1474 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1475 it.last_visible_y)
1476 - max (it2.current_y,
1477 WINDOW_HEADER_LINE_HEIGHT (w))));
1478 *vpos = it2.vpos;
1482 if (old_buffer)
1483 set_buffer_internal_1 (old_buffer);
1485 current_header_line_height = current_mode_line_height = -1;
1487 if (visible_p && XFASTINT (w->hscroll) > 0)
1488 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1490 #if 0
1491 /* Debugging code. */
1492 if (visible_p)
1493 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1494 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1495 else
1496 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1497 #endif
1499 return visible_p;
1503 /* Return the next character from STR which is MAXLEN bytes long.
1504 Return in *LEN the length of the character. This is like
1505 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1506 we find one, we return a `?', but with the length of the invalid
1507 character. */
1509 static INLINE int
1510 string_char_and_length (const unsigned char *str, int *len)
1512 int c;
1514 c = STRING_CHAR_AND_LENGTH (str, *len);
1515 if (!CHAR_VALID_P (c, 1))
1516 /* We may not change the length here because other places in Emacs
1517 don't use this function, i.e. they silently accept invalid
1518 characters. */
1519 c = '?';
1521 return c;
1526 /* Given a position POS containing a valid character and byte position
1527 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1529 static struct text_pos
1530 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1532 xassert (STRINGP (string) && nchars >= 0);
1534 if (STRING_MULTIBYTE (string))
1536 EMACS_INT rest = SBYTES (string) - BYTEPOS (pos);
1537 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1538 int len;
1540 while (nchars--)
1542 string_char_and_length (p, &len);
1543 p += len, rest -= len;
1544 xassert (rest >= 0);
1545 CHARPOS (pos) += 1;
1546 BYTEPOS (pos) += len;
1549 else
1550 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1552 return pos;
1556 /* Value is the text position, i.e. character and byte position,
1557 for character position CHARPOS in STRING. */
1559 static INLINE struct text_pos
1560 string_pos (EMACS_INT charpos, Lisp_Object string)
1562 struct text_pos pos;
1563 xassert (STRINGP (string));
1564 xassert (charpos >= 0);
1565 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1566 return pos;
1570 /* Value is a text position, i.e. character and byte position, for
1571 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1572 means recognize multibyte characters. */
1574 static struct text_pos
1575 c_string_pos (EMACS_INT charpos, const unsigned char *s, int multibyte_p)
1577 struct text_pos pos;
1579 xassert (s != NULL);
1580 xassert (charpos >= 0);
1582 if (multibyte_p)
1584 EMACS_INT rest = strlen (s);
1585 int len;
1587 SET_TEXT_POS (pos, 0, 0);
1588 while (charpos--)
1590 string_char_and_length (s, &len);
1591 s += len, rest -= len;
1592 xassert (rest >= 0);
1593 CHARPOS (pos) += 1;
1594 BYTEPOS (pos) += len;
1597 else
1598 SET_TEXT_POS (pos, charpos, charpos);
1600 return pos;
1604 /* Value is the number of characters in C string S. MULTIBYTE_P
1605 non-zero means recognize multibyte characters. */
1607 static EMACS_INT
1608 number_of_chars (const unsigned char *s, int multibyte_p)
1610 EMACS_INT nchars;
1612 if (multibyte_p)
1614 EMACS_INT rest = strlen (s);
1615 int len;
1616 unsigned char *p = (unsigned char *) s;
1618 for (nchars = 0; rest > 0; ++nchars)
1620 string_char_and_length (p, &len);
1621 rest -= len, p += len;
1624 else
1625 nchars = strlen (s);
1627 return nchars;
1631 /* Compute byte position NEWPOS->bytepos corresponding to
1632 NEWPOS->charpos. POS is a known position in string STRING.
1633 NEWPOS->charpos must be >= POS.charpos. */
1635 static void
1636 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1638 xassert (STRINGP (string));
1639 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1641 if (STRING_MULTIBYTE (string))
1642 *newpos = string_pos_nchars_ahead (pos, string,
1643 CHARPOS (*newpos) - CHARPOS (pos));
1644 else
1645 BYTEPOS (*newpos) = CHARPOS (*newpos);
1648 /* EXPORT:
1649 Return an estimation of the pixel height of mode or header lines on
1650 frame F. FACE_ID specifies what line's height to estimate. */
1653 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1655 #ifdef HAVE_WINDOW_SYSTEM
1656 if (FRAME_WINDOW_P (f))
1658 int height = FONT_HEIGHT (FRAME_FONT (f));
1660 /* This function is called so early when Emacs starts that the face
1661 cache and mode line face are not yet initialized. */
1662 if (FRAME_FACE_CACHE (f))
1664 struct face *face = FACE_FROM_ID (f, face_id);
1665 if (face)
1667 if (face->font)
1668 height = FONT_HEIGHT (face->font);
1669 if (face->box_line_width > 0)
1670 height += 2 * face->box_line_width;
1674 return height;
1676 #endif
1678 return 1;
1681 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1682 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1683 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1684 not force the value into range. */
1686 void
1687 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1688 int *x, int *y, NativeRectangle *bounds, int noclip)
1691 #ifdef HAVE_WINDOW_SYSTEM
1692 if (FRAME_WINDOW_P (f))
1694 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1695 even for negative values. */
1696 if (pix_x < 0)
1697 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1698 if (pix_y < 0)
1699 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1701 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1702 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1704 if (bounds)
1705 STORE_NATIVE_RECT (*bounds,
1706 FRAME_COL_TO_PIXEL_X (f, pix_x),
1707 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1708 FRAME_COLUMN_WIDTH (f) - 1,
1709 FRAME_LINE_HEIGHT (f) - 1);
1711 if (!noclip)
1713 if (pix_x < 0)
1714 pix_x = 0;
1715 else if (pix_x > FRAME_TOTAL_COLS (f))
1716 pix_x = FRAME_TOTAL_COLS (f);
1718 if (pix_y < 0)
1719 pix_y = 0;
1720 else if (pix_y > FRAME_LINES (f))
1721 pix_y = FRAME_LINES (f);
1724 #endif
1726 *x = pix_x;
1727 *y = pix_y;
1731 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1732 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1733 can't tell the positions because W's display is not up to date,
1734 return 0. */
1737 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1738 int *frame_x, int *frame_y)
1740 #ifdef HAVE_WINDOW_SYSTEM
1741 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1743 int success_p;
1745 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1746 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1748 if (display_completed)
1750 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1751 struct glyph *glyph = row->glyphs[TEXT_AREA];
1752 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1754 hpos = row->x;
1755 vpos = row->y;
1756 while (glyph < end)
1758 hpos += glyph->pixel_width;
1759 ++glyph;
1762 /* If first glyph is partially visible, its first visible position is still 0. */
1763 if (hpos < 0)
1764 hpos = 0;
1766 success_p = 1;
1768 else
1770 hpos = vpos = 0;
1771 success_p = 0;
1774 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1775 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1776 return success_p;
1778 #endif
1780 *frame_x = hpos;
1781 *frame_y = vpos;
1782 return 1;
1786 /* Find the glyph under window-relative coordinates X/Y in window W.
1787 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1788 strings. Return in *HPOS and *VPOS the row and column number of
1789 the glyph found. Return in *AREA the glyph area containing X.
1790 Value is a pointer to the glyph found or null if X/Y is not on
1791 text, or we can't tell because W's current matrix is not up to
1792 date. */
1794 static
1795 struct glyph *
1796 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1797 int *dx, int *dy, int *area)
1799 struct glyph *glyph, *end;
1800 struct glyph_row *row = NULL;
1801 int x0, i;
1803 /* Find row containing Y. Give up if some row is not enabled. */
1804 for (i = 0; i < w->current_matrix->nrows; ++i)
1806 row = MATRIX_ROW (w->current_matrix, i);
1807 if (!row->enabled_p)
1808 return NULL;
1809 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1810 break;
1813 *vpos = i;
1814 *hpos = 0;
1816 /* Give up if Y is not in the window. */
1817 if (i == w->current_matrix->nrows)
1818 return NULL;
1820 /* Get the glyph area containing X. */
1821 if (w->pseudo_window_p)
1823 *area = TEXT_AREA;
1824 x0 = 0;
1826 else
1828 if (x < window_box_left_offset (w, TEXT_AREA))
1830 *area = LEFT_MARGIN_AREA;
1831 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1833 else if (x < window_box_right_offset (w, TEXT_AREA))
1835 *area = TEXT_AREA;
1836 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1838 else
1840 *area = RIGHT_MARGIN_AREA;
1841 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1845 /* Find glyph containing X. */
1846 glyph = row->glyphs[*area];
1847 end = glyph + row->used[*area];
1848 x -= x0;
1849 while (glyph < end && x >= glyph->pixel_width)
1851 x -= glyph->pixel_width;
1852 ++glyph;
1855 if (glyph == end)
1856 return NULL;
1858 if (dx)
1860 *dx = x;
1861 *dy = y - (row->y + row->ascent - glyph->ascent);
1864 *hpos = glyph - row->glyphs[*area];
1865 return glyph;
1868 /* EXPORT:
1869 Convert frame-relative x/y to coordinates relative to window W.
1870 Takes pseudo-windows into account. */
1872 void
1873 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1875 if (w->pseudo_window_p)
1877 /* A pseudo-window is always full-width, and starts at the
1878 left edge of the frame, plus a frame border. */
1879 struct frame *f = XFRAME (w->frame);
1880 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1881 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1883 else
1885 *x -= WINDOW_LEFT_EDGE_X (w);
1886 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1890 #ifdef HAVE_WINDOW_SYSTEM
1892 /* EXPORT:
1893 Return in RECTS[] at most N clipping rectangles for glyph string S.
1894 Return the number of stored rectangles. */
1897 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1899 XRectangle r;
1901 if (n <= 0)
1902 return 0;
1904 if (s->row->full_width_p)
1906 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1907 r.x = WINDOW_LEFT_EDGE_X (s->w);
1908 r.width = WINDOW_TOTAL_WIDTH (s->w);
1910 /* Unless displaying a mode or menu bar line, which are always
1911 fully visible, clip to the visible part of the row. */
1912 if (s->w->pseudo_window_p)
1913 r.height = s->row->visible_height;
1914 else
1915 r.height = s->height;
1917 else
1919 /* This is a text line that may be partially visible. */
1920 r.x = window_box_left (s->w, s->area);
1921 r.width = window_box_width (s->w, s->area);
1922 r.height = s->row->visible_height;
1925 if (s->clip_head)
1926 if (r.x < s->clip_head->x)
1928 if (r.width >= s->clip_head->x - r.x)
1929 r.width -= s->clip_head->x - r.x;
1930 else
1931 r.width = 0;
1932 r.x = s->clip_head->x;
1934 if (s->clip_tail)
1935 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1937 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1938 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1939 else
1940 r.width = 0;
1943 /* If S draws overlapping rows, it's sufficient to use the top and
1944 bottom of the window for clipping because this glyph string
1945 intentionally draws over other lines. */
1946 if (s->for_overlaps)
1948 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1949 r.height = window_text_bottom_y (s->w) - r.y;
1951 /* Alas, the above simple strategy does not work for the
1952 environments with anti-aliased text: if the same text is
1953 drawn onto the same place multiple times, it gets thicker.
1954 If the overlap we are processing is for the erased cursor, we
1955 take the intersection with the rectagle of the cursor. */
1956 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1958 XRectangle rc, r_save = r;
1960 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1961 rc.y = s->w->phys_cursor.y;
1962 rc.width = s->w->phys_cursor_width;
1963 rc.height = s->w->phys_cursor_height;
1965 x_intersect_rectangles (&r_save, &rc, &r);
1968 else
1970 /* Don't use S->y for clipping because it doesn't take partially
1971 visible lines into account. For example, it can be negative for
1972 partially visible lines at the top of a window. */
1973 if (!s->row->full_width_p
1974 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1975 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1976 else
1977 r.y = max (0, s->row->y);
1980 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1982 /* If drawing the cursor, don't let glyph draw outside its
1983 advertised boundaries. Cleartype does this under some circumstances. */
1984 if (s->hl == DRAW_CURSOR)
1986 struct glyph *glyph = s->first_glyph;
1987 int height, max_y;
1989 if (s->x > r.x)
1991 r.width -= s->x - r.x;
1992 r.x = s->x;
1994 r.width = min (r.width, glyph->pixel_width);
1996 /* If r.y is below window bottom, ensure that we still see a cursor. */
1997 height = min (glyph->ascent + glyph->descent,
1998 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1999 max_y = window_text_bottom_y (s->w) - height;
2000 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2001 if (s->ybase - glyph->ascent > max_y)
2003 r.y = max_y;
2004 r.height = height;
2006 else
2008 /* Don't draw cursor glyph taller than our actual glyph. */
2009 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2010 if (height < r.height)
2012 max_y = r.y + r.height;
2013 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2014 r.height = min (max_y - r.y, height);
2019 if (s->row->clip)
2021 XRectangle r_save = r;
2023 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2024 r.width = 0;
2027 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2028 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2030 #ifdef CONVERT_FROM_XRECT
2031 CONVERT_FROM_XRECT (r, *rects);
2032 #else
2033 *rects = r;
2034 #endif
2035 return 1;
2037 else
2039 /* If we are processing overlapping and allowed to return
2040 multiple clipping rectangles, we exclude the row of the glyph
2041 string from the clipping rectangle. This is to avoid drawing
2042 the same text on the environment with anti-aliasing. */
2043 #ifdef CONVERT_FROM_XRECT
2044 XRectangle rs[2];
2045 #else
2046 XRectangle *rs = rects;
2047 #endif
2048 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2050 if (s->for_overlaps & OVERLAPS_PRED)
2052 rs[i] = r;
2053 if (r.y + r.height > row_y)
2055 if (r.y < row_y)
2056 rs[i].height = row_y - r.y;
2057 else
2058 rs[i].height = 0;
2060 i++;
2062 if (s->for_overlaps & OVERLAPS_SUCC)
2064 rs[i] = r;
2065 if (r.y < row_y + s->row->visible_height)
2067 if (r.y + r.height > row_y + s->row->visible_height)
2069 rs[i].y = row_y + s->row->visible_height;
2070 rs[i].height = r.y + r.height - rs[i].y;
2072 else
2073 rs[i].height = 0;
2075 i++;
2078 n = i;
2079 #ifdef CONVERT_FROM_XRECT
2080 for (i = 0; i < n; i++)
2081 CONVERT_FROM_XRECT (rs[i], rects[i]);
2082 #endif
2083 return n;
2087 /* EXPORT:
2088 Return in *NR the clipping rectangle for glyph string S. */
2090 void
2091 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2093 get_glyph_string_clip_rects (s, nr, 1);
2097 /* EXPORT:
2098 Return the position and height of the phys cursor in window W.
2099 Set w->phys_cursor_width to width of phys cursor.
2102 void
2103 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2104 struct glyph *glyph, int *xp, int *yp, int *heightp)
2106 struct frame *f = XFRAME (WINDOW_FRAME (w));
2107 int x, y, wd, h, h0, y0;
2109 /* Compute the width of the rectangle to draw. If on a stretch
2110 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2111 rectangle as wide as the glyph, but use a canonical character
2112 width instead. */
2113 wd = glyph->pixel_width - 1;
2114 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2115 wd++; /* Why? */
2116 #endif
2118 x = w->phys_cursor.x;
2119 if (x < 0)
2121 wd += x;
2122 x = 0;
2125 if (glyph->type == STRETCH_GLYPH
2126 && !x_stretch_cursor_p)
2127 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2128 w->phys_cursor_width = wd;
2130 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2132 /* If y is below window bottom, ensure that we still see a cursor. */
2133 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2135 h = max (h0, glyph->ascent + glyph->descent);
2136 h0 = min (h0, glyph->ascent + glyph->descent);
2138 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2139 if (y < y0)
2141 h = max (h - (y0 - y) + 1, h0);
2142 y = y0 - 1;
2144 else
2146 y0 = window_text_bottom_y (w) - h0;
2147 if (y > y0)
2149 h += y - y0;
2150 y = y0;
2154 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2155 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2156 *heightp = h;
2160 * Remember which glyph the mouse is over.
2163 void
2164 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2166 Lisp_Object window;
2167 struct window *w;
2168 struct glyph_row *r, *gr, *end_row;
2169 enum window_part part;
2170 enum glyph_row_area area;
2171 int x, y, width, height;
2173 /* Try to determine frame pixel position and size of the glyph under
2174 frame pixel coordinates X/Y on frame F. */
2176 if (!f->glyphs_initialized_p
2177 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2178 NILP (window)))
2180 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2181 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2182 goto virtual_glyph;
2185 w = XWINDOW (window);
2186 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2187 height = WINDOW_FRAME_LINE_HEIGHT (w);
2189 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2190 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2192 if (w->pseudo_window_p)
2194 area = TEXT_AREA;
2195 part = ON_MODE_LINE; /* Don't adjust margin. */
2196 goto text_glyph;
2199 switch (part)
2201 case ON_LEFT_MARGIN:
2202 area = LEFT_MARGIN_AREA;
2203 goto text_glyph;
2205 case ON_RIGHT_MARGIN:
2206 area = RIGHT_MARGIN_AREA;
2207 goto text_glyph;
2209 case ON_HEADER_LINE:
2210 case ON_MODE_LINE:
2211 gr = (part == ON_HEADER_LINE
2212 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2213 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2214 gy = gr->y;
2215 area = TEXT_AREA;
2216 goto text_glyph_row_found;
2218 case ON_TEXT:
2219 area = TEXT_AREA;
2221 text_glyph:
2222 gr = 0; gy = 0;
2223 for (; r <= end_row && r->enabled_p; ++r)
2224 if (r->y + r->height > y)
2226 gr = r; gy = r->y;
2227 break;
2230 text_glyph_row_found:
2231 if (gr && gy <= y)
2233 struct glyph *g = gr->glyphs[area];
2234 struct glyph *end = g + gr->used[area];
2236 height = gr->height;
2237 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2238 if (gx + g->pixel_width > x)
2239 break;
2241 if (g < end)
2243 if (g->type == IMAGE_GLYPH)
2245 /* Don't remember when mouse is over image, as
2246 image may have hot-spots. */
2247 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2248 return;
2250 width = g->pixel_width;
2252 else
2254 /* Use nominal char spacing at end of line. */
2255 x -= gx;
2256 gx += (x / width) * width;
2259 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2260 gx += window_box_left_offset (w, area);
2262 else
2264 /* Use nominal line height at end of window. */
2265 gx = (x / width) * width;
2266 y -= gy;
2267 gy += (y / height) * height;
2269 break;
2271 case ON_LEFT_FRINGE:
2272 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2273 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2274 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2275 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2276 goto row_glyph;
2278 case ON_RIGHT_FRINGE:
2279 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2280 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2281 : window_box_right_offset (w, TEXT_AREA));
2282 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2283 goto row_glyph;
2285 case ON_SCROLL_BAR:
2286 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2288 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2289 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2290 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2291 : 0)));
2292 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2294 row_glyph:
2295 gr = 0, gy = 0;
2296 for (; r <= end_row && r->enabled_p; ++r)
2297 if (r->y + r->height > y)
2299 gr = r; gy = r->y;
2300 break;
2303 if (gr && gy <= y)
2304 height = gr->height;
2305 else
2307 /* Use nominal line height at end of window. */
2308 y -= gy;
2309 gy += (y / height) * height;
2311 break;
2313 default:
2315 virtual_glyph:
2316 /* If there is no glyph under the mouse, then we divide the screen
2317 into a grid of the smallest glyph in the frame, and use that
2318 as our "glyph". */
2320 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2321 round down even for negative values. */
2322 if (gx < 0)
2323 gx -= width - 1;
2324 if (gy < 0)
2325 gy -= height - 1;
2327 gx = (gx / width) * width;
2328 gy = (gy / height) * height;
2330 goto store_rect;
2333 gx += WINDOW_LEFT_EDGE_X (w);
2334 gy += WINDOW_TOP_EDGE_Y (w);
2336 store_rect:
2337 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2339 /* Visible feedback for debugging. */
2340 #if 0
2341 #if HAVE_X_WINDOWS
2342 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2343 f->output_data.x->normal_gc,
2344 gx, gy, width, height);
2345 #endif
2346 #endif
2350 #endif /* HAVE_WINDOW_SYSTEM */
2353 /***********************************************************************
2354 Lisp form evaluation
2355 ***********************************************************************/
2357 /* Error handler for safe_eval and safe_call. */
2359 static Lisp_Object
2360 safe_eval_handler (Lisp_Object arg)
2362 add_to_log ("Error during redisplay: %s", arg, Qnil);
2363 return Qnil;
2367 /* Evaluate SEXPR and return the result, or nil if something went
2368 wrong. Prevent redisplay during the evaluation. */
2370 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2371 Return the result, or nil if something went wrong. Prevent
2372 redisplay during the evaluation. */
2374 Lisp_Object
2375 safe_call (int nargs, Lisp_Object *args)
2377 Lisp_Object val;
2379 if (inhibit_eval_during_redisplay)
2380 val = Qnil;
2381 else
2383 int count = SPECPDL_INDEX ();
2384 struct gcpro gcpro1;
2386 GCPRO1 (args[0]);
2387 gcpro1.nvars = nargs;
2388 specbind (Qinhibit_redisplay, Qt);
2389 /* Use Qt to ensure debugger does not run,
2390 so there is no possibility of wanting to redisplay. */
2391 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2392 safe_eval_handler);
2393 UNGCPRO;
2394 val = unbind_to (count, val);
2397 return val;
2401 /* Call function FN with one argument ARG.
2402 Return the result, or nil if something went wrong. */
2404 Lisp_Object
2405 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2407 Lisp_Object args[2];
2408 args[0] = fn;
2409 args[1] = arg;
2410 return safe_call (2, args);
2413 static Lisp_Object Qeval;
2415 Lisp_Object
2416 safe_eval (Lisp_Object sexpr)
2418 return safe_call1 (Qeval, sexpr);
2421 /* Call function FN with one argument ARG.
2422 Return the result, or nil if something went wrong. */
2424 Lisp_Object
2425 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2427 Lisp_Object args[3];
2428 args[0] = fn;
2429 args[1] = arg1;
2430 args[2] = arg2;
2431 return safe_call (3, args);
2436 /***********************************************************************
2437 Debugging
2438 ***********************************************************************/
2440 #if 0
2442 /* Define CHECK_IT to perform sanity checks on iterators.
2443 This is for debugging. It is too slow to do unconditionally. */
2445 static void
2446 check_it (it)
2447 struct it *it;
2449 if (it->method == GET_FROM_STRING)
2451 xassert (STRINGP (it->string));
2452 xassert (IT_STRING_CHARPOS (*it) >= 0);
2454 else
2456 xassert (IT_STRING_CHARPOS (*it) < 0);
2457 if (it->method == GET_FROM_BUFFER)
2459 /* Check that character and byte positions agree. */
2460 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2464 if (it->dpvec)
2465 xassert (it->current.dpvec_index >= 0);
2466 else
2467 xassert (it->current.dpvec_index < 0);
2470 #define CHECK_IT(IT) check_it ((IT))
2472 #else /* not 0 */
2474 #define CHECK_IT(IT) (void) 0
2476 #endif /* not 0 */
2479 #if GLYPH_DEBUG
2481 /* Check that the window end of window W is what we expect it
2482 to be---the last row in the current matrix displaying text. */
2484 static void
2485 check_window_end (w)
2486 struct window *w;
2488 if (!MINI_WINDOW_P (w)
2489 && !NILP (w->window_end_valid))
2491 struct glyph_row *row;
2492 xassert ((row = MATRIX_ROW (w->current_matrix,
2493 XFASTINT (w->window_end_vpos)),
2494 !row->enabled_p
2495 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2496 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2500 #define CHECK_WINDOW_END(W) check_window_end ((W))
2502 #else /* not GLYPH_DEBUG */
2504 #define CHECK_WINDOW_END(W) (void) 0
2506 #endif /* not GLYPH_DEBUG */
2510 /***********************************************************************
2511 Iterator initialization
2512 ***********************************************************************/
2514 /* Initialize IT for displaying current_buffer in window W, starting
2515 at character position CHARPOS. CHARPOS < 0 means that no buffer
2516 position is specified which is useful when the iterator is assigned
2517 a position later. BYTEPOS is the byte position corresponding to
2518 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2520 If ROW is not null, calls to produce_glyphs with IT as parameter
2521 will produce glyphs in that row.
2523 BASE_FACE_ID is the id of a base face to use. It must be one of
2524 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2525 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2526 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2528 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2529 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2530 will be initialized to use the corresponding mode line glyph row of
2531 the desired matrix of W. */
2533 void
2534 init_iterator (struct it *it, struct window *w,
2535 EMACS_INT charpos, EMACS_INT bytepos,
2536 struct glyph_row *row, enum face_id base_face_id)
2538 int highlight_region_p;
2539 enum face_id remapped_base_face_id = base_face_id;
2541 /* Some precondition checks. */
2542 xassert (w != NULL && it != NULL);
2543 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2544 && charpos <= ZV));
2546 /* If face attributes have been changed since the last redisplay,
2547 free realized faces now because they depend on face definitions
2548 that might have changed. Don't free faces while there might be
2549 desired matrices pending which reference these faces. */
2550 if (face_change_count && !inhibit_free_realized_faces)
2552 face_change_count = 0;
2553 free_all_realized_faces (Qnil);
2556 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2557 if (! NILP (Vface_remapping_alist))
2558 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2560 /* Use one of the mode line rows of W's desired matrix if
2561 appropriate. */
2562 if (row == NULL)
2564 if (base_face_id == MODE_LINE_FACE_ID
2565 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2566 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2567 else if (base_face_id == HEADER_LINE_FACE_ID)
2568 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2571 /* Clear IT. */
2572 memset (it, 0, sizeof *it);
2573 it->current.overlay_string_index = -1;
2574 it->current.dpvec_index = -1;
2575 it->base_face_id = remapped_base_face_id;
2576 it->string = Qnil;
2577 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2579 /* The window in which we iterate over current_buffer: */
2580 XSETWINDOW (it->window, w);
2581 it->w = w;
2582 it->f = XFRAME (w->frame);
2584 it->cmp_it.id = -1;
2586 /* Extra space between lines (on window systems only). */
2587 if (base_face_id == DEFAULT_FACE_ID
2588 && FRAME_WINDOW_P (it->f))
2590 if (NATNUMP (current_buffer->extra_line_spacing))
2591 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2592 else if (FLOATP (current_buffer->extra_line_spacing))
2593 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2594 * FRAME_LINE_HEIGHT (it->f));
2595 else if (it->f->extra_line_spacing > 0)
2596 it->extra_line_spacing = it->f->extra_line_spacing;
2597 it->max_extra_line_spacing = 0;
2600 /* If realized faces have been removed, e.g. because of face
2601 attribute changes of named faces, recompute them. When running
2602 in batch mode, the face cache of the initial frame is null. If
2603 we happen to get called, make a dummy face cache. */
2604 if (FRAME_FACE_CACHE (it->f) == NULL)
2605 init_frame_faces (it->f);
2606 if (FRAME_FACE_CACHE (it->f)->used == 0)
2607 recompute_basic_faces (it->f);
2609 /* Current value of the `slice', `space-width', and 'height' properties. */
2610 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2611 it->space_width = Qnil;
2612 it->font_height = Qnil;
2613 it->override_ascent = -1;
2615 /* Are control characters displayed as `^C'? */
2616 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2618 /* -1 means everything between a CR and the following line end
2619 is invisible. >0 means lines indented more than this value are
2620 invisible. */
2621 it->selective = (INTEGERP (current_buffer->selective_display)
2622 ? XFASTINT (current_buffer->selective_display)
2623 : (!NILP (current_buffer->selective_display)
2624 ? -1 : 0));
2625 it->selective_display_ellipsis_p
2626 = !NILP (current_buffer->selective_display_ellipses);
2628 /* Display table to use. */
2629 it->dp = window_display_table (w);
2631 /* Are multibyte characters enabled in current_buffer? */
2632 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2634 /* Do we need to reorder bidirectional text? Not if this is a
2635 unibyte buffer: by definition, none of the single-byte characters
2636 are strong R2L, so no reordering is needed. And bidi.c doesn't
2637 support unibyte buffers anyway. */
2638 it->bidi_p
2639 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2641 /* Non-zero if we should highlight the region. */
2642 highlight_region_p
2643 = (!NILP (Vtransient_mark_mode)
2644 && !NILP (current_buffer->mark_active)
2645 && XMARKER (current_buffer->mark)->buffer != 0);
2647 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2648 start and end of a visible region in window IT->w. Set both to
2649 -1 to indicate no region. */
2650 if (highlight_region_p
2651 /* Maybe highlight only in selected window. */
2652 && (/* Either show region everywhere. */
2653 highlight_nonselected_windows
2654 /* Or show region in the selected window. */
2655 || w == XWINDOW (selected_window)
2656 /* Or show the region if we are in the mini-buffer and W is
2657 the window the mini-buffer refers to. */
2658 || (MINI_WINDOW_P (XWINDOW (selected_window))
2659 && WINDOWP (minibuf_selected_window)
2660 && w == XWINDOW (minibuf_selected_window))))
2662 EMACS_INT charpos = marker_position (current_buffer->mark);
2663 it->region_beg_charpos = min (PT, charpos);
2664 it->region_end_charpos = max (PT, charpos);
2666 else
2667 it->region_beg_charpos = it->region_end_charpos = -1;
2669 /* Get the position at which the redisplay_end_trigger hook should
2670 be run, if it is to be run at all. */
2671 if (MARKERP (w->redisplay_end_trigger)
2672 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2673 it->redisplay_end_trigger_charpos
2674 = marker_position (w->redisplay_end_trigger);
2675 else if (INTEGERP (w->redisplay_end_trigger))
2676 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2678 /* Correct bogus values of tab_width. */
2679 it->tab_width = XINT (current_buffer->tab_width);
2680 if (it->tab_width <= 0 || it->tab_width > 1000)
2681 it->tab_width = 8;
2683 /* Are lines in the display truncated? */
2684 if (base_face_id != DEFAULT_FACE_ID
2685 || XINT (it->w->hscroll)
2686 || (! WINDOW_FULL_WIDTH_P (it->w)
2687 && ((!NILP (Vtruncate_partial_width_windows)
2688 && !INTEGERP (Vtruncate_partial_width_windows))
2689 || (INTEGERP (Vtruncate_partial_width_windows)
2690 && (WINDOW_TOTAL_COLS (it->w)
2691 < XINT (Vtruncate_partial_width_windows))))))
2692 it->line_wrap = TRUNCATE;
2693 else if (NILP (current_buffer->truncate_lines))
2694 it->line_wrap = NILP (current_buffer->word_wrap)
2695 ? WINDOW_WRAP : WORD_WRAP;
2696 else
2697 it->line_wrap = TRUNCATE;
2699 /* Get dimensions of truncation and continuation glyphs. These are
2700 displayed as fringe bitmaps under X, so we don't need them for such
2701 frames. */
2702 if (!FRAME_WINDOW_P (it->f))
2704 if (it->line_wrap == TRUNCATE)
2706 /* We will need the truncation glyph. */
2707 xassert (it->glyph_row == NULL);
2708 produce_special_glyphs (it, IT_TRUNCATION);
2709 it->truncation_pixel_width = it->pixel_width;
2711 else
2713 /* We will need the continuation glyph. */
2714 xassert (it->glyph_row == NULL);
2715 produce_special_glyphs (it, IT_CONTINUATION);
2716 it->continuation_pixel_width = it->pixel_width;
2719 /* Reset these values to zero because the produce_special_glyphs
2720 above has changed them. */
2721 it->pixel_width = it->ascent = it->descent = 0;
2722 it->phys_ascent = it->phys_descent = 0;
2725 /* Set this after getting the dimensions of truncation and
2726 continuation glyphs, so that we don't produce glyphs when calling
2727 produce_special_glyphs, above. */
2728 it->glyph_row = row;
2729 it->area = TEXT_AREA;
2731 /* Forget any previous info about this row being reversed. */
2732 if (it->glyph_row)
2733 it->glyph_row->reversed_p = 0;
2735 /* Get the dimensions of the display area. The display area
2736 consists of the visible window area plus a horizontally scrolled
2737 part to the left of the window. All x-values are relative to the
2738 start of this total display area. */
2739 if (base_face_id != DEFAULT_FACE_ID)
2741 /* Mode lines, menu bar in terminal frames. */
2742 it->first_visible_x = 0;
2743 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2745 else
2747 it->first_visible_x
2748 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2749 it->last_visible_x = (it->first_visible_x
2750 + window_box_width (w, TEXT_AREA));
2752 /* If we truncate lines, leave room for the truncator glyph(s) at
2753 the right margin. Otherwise, leave room for the continuation
2754 glyph(s). Truncation and continuation glyphs are not inserted
2755 for window-based redisplay. */
2756 if (!FRAME_WINDOW_P (it->f))
2758 if (it->line_wrap == TRUNCATE)
2759 it->last_visible_x -= it->truncation_pixel_width;
2760 else
2761 it->last_visible_x -= it->continuation_pixel_width;
2764 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2765 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2768 /* Leave room for a border glyph. */
2769 if (!FRAME_WINDOW_P (it->f)
2770 && !WINDOW_RIGHTMOST_P (it->w))
2771 it->last_visible_x -= 1;
2773 it->last_visible_y = window_text_bottom_y (w);
2775 /* For mode lines and alike, arrange for the first glyph having a
2776 left box line if the face specifies a box. */
2777 if (base_face_id != DEFAULT_FACE_ID)
2779 struct face *face;
2781 it->face_id = remapped_base_face_id;
2783 /* If we have a boxed mode line, make the first character appear
2784 with a left box line. */
2785 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2786 if (face->box != FACE_NO_BOX)
2787 it->start_of_box_run_p = 1;
2790 /* If we are to reorder bidirectional text, init the bidi
2791 iterator. */
2792 if (it->bidi_p)
2794 /* Note the paragraph direction that this buffer wants to
2795 use. */
2796 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2797 it->paragraph_embedding = L2R;
2798 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2799 it->paragraph_embedding = R2L;
2800 else
2801 it->paragraph_embedding = NEUTRAL_DIR;
2802 bidi_init_it (charpos, bytepos, &it->bidi_it);
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2809 it->end_charpos = ZV;
2810 it->face_id = -1;
2811 IT_CHARPOS (*it) = charpos;
2813 /* Compute byte position if not specified. */
2814 if (bytepos < charpos)
2815 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2816 else
2817 IT_BYTEPOS (*it) = bytepos;
2819 it->start = it->current;
2821 /* Compute faces etc. */
2822 reseat (it, it->current.pos, 1);
2825 CHECK_IT (it);
2829 /* Initialize IT for the display of window W with window start POS. */
2831 void
2832 start_display (struct it *it, struct window *w, struct text_pos pos)
2834 struct glyph_row *row;
2835 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2837 row = w->desired_matrix->rows + first_vpos;
2838 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2839 it->first_vpos = first_vpos;
2841 /* Don't reseat to previous visible line start if current start
2842 position is in a string or image. */
2843 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2845 int start_at_line_beg_p;
2846 int first_y = it->current_y;
2848 /* If window start is not at a line start, skip forward to POS to
2849 get the correct continuation lines width. */
2850 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2851 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2852 if (!start_at_line_beg_p)
2854 int new_x;
2856 reseat_at_previous_visible_line_start (it);
2857 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2859 new_x = it->current_x + it->pixel_width;
2861 /* If lines are continued, this line may end in the middle
2862 of a multi-glyph character (e.g. a control character
2863 displayed as \003, or in the middle of an overlay
2864 string). In this case move_it_to above will not have
2865 taken us to the start of the continuation line but to the
2866 end of the continued line. */
2867 if (it->current_x > 0
2868 && it->line_wrap != TRUNCATE /* Lines are continued. */
2869 && (/* And glyph doesn't fit on the line. */
2870 new_x > it->last_visible_x
2871 /* Or it fits exactly and we're on a window
2872 system frame. */
2873 || (new_x == it->last_visible_x
2874 && FRAME_WINDOW_P (it->f))))
2876 if (it->current.dpvec_index >= 0
2877 || it->current.overlay_string_index >= 0)
2879 set_iterator_to_next (it, 1);
2880 move_it_in_display_line_to (it, -1, -1, 0);
2883 it->continuation_lines_width += it->current_x;
2886 /* We're starting a new display line, not affected by the
2887 height of the continued line, so clear the appropriate
2888 fields in the iterator structure. */
2889 it->max_ascent = it->max_descent = 0;
2890 it->max_phys_ascent = it->max_phys_descent = 0;
2892 it->current_y = first_y;
2893 it->vpos = 0;
2894 it->current_x = it->hpos = 0;
2900 /* Return 1 if POS is a position in ellipses displayed for invisible
2901 text. W is the window we display, for text property lookup. */
2903 static int
2904 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2906 Lisp_Object prop, window;
2907 int ellipses_p = 0;
2908 EMACS_INT charpos = CHARPOS (pos->pos);
2910 /* If POS specifies a position in a display vector, this might
2911 be for an ellipsis displayed for invisible text. We won't
2912 get the iterator set up for delivering that ellipsis unless
2913 we make sure that it gets aware of the invisible text. */
2914 if (pos->dpvec_index >= 0
2915 && pos->overlay_string_index < 0
2916 && CHARPOS (pos->string_pos) < 0
2917 && charpos > BEGV
2918 && (XSETWINDOW (window, w),
2919 prop = Fget_char_property (make_number (charpos),
2920 Qinvisible, window),
2921 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2923 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2924 window);
2925 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2928 return ellipses_p;
2932 /* Initialize IT for stepping through current_buffer in window W,
2933 starting at position POS that includes overlay string and display
2934 vector/ control character translation position information. Value
2935 is zero if there are overlay strings with newlines at POS. */
2937 static int
2938 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2940 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2941 int i, overlay_strings_with_newlines = 0;
2943 /* If POS specifies a position in a display vector, this might
2944 be for an ellipsis displayed for invisible text. We won't
2945 get the iterator set up for delivering that ellipsis unless
2946 we make sure that it gets aware of the invisible text. */
2947 if (in_ellipses_for_invisible_text_p (pos, w))
2949 --charpos;
2950 bytepos = 0;
2953 /* Keep in mind: the call to reseat in init_iterator skips invisible
2954 text, so we might end up at a position different from POS. This
2955 is only a problem when POS is a row start after a newline and an
2956 overlay starts there with an after-string, and the overlay has an
2957 invisible property. Since we don't skip invisible text in
2958 display_line and elsewhere immediately after consuming the
2959 newline before the row start, such a POS will not be in a string,
2960 but the call to init_iterator below will move us to the
2961 after-string. */
2962 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2964 /* This only scans the current chunk -- it should scan all chunks.
2965 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2966 to 16 in 22.1 to make this a lesser problem. */
2967 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2969 const char *s = SDATA (it->overlay_strings[i]);
2970 const char *e = s + SBYTES (it->overlay_strings[i]);
2972 while (s < e && *s != '\n')
2973 ++s;
2975 if (s < e)
2977 overlay_strings_with_newlines = 1;
2978 break;
2982 /* If position is within an overlay string, set up IT to the right
2983 overlay string. */
2984 if (pos->overlay_string_index >= 0)
2986 int relative_index;
2988 /* If the first overlay string happens to have a `display'
2989 property for an image, the iterator will be set up for that
2990 image, and we have to undo that setup first before we can
2991 correct the overlay string index. */
2992 if (it->method == GET_FROM_IMAGE)
2993 pop_it (it);
2995 /* We already have the first chunk of overlay strings in
2996 IT->overlay_strings. Load more until the one for
2997 pos->overlay_string_index is in IT->overlay_strings. */
2998 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3000 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3001 it->current.overlay_string_index = 0;
3002 while (n--)
3004 load_overlay_strings (it, 0);
3005 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3009 it->current.overlay_string_index = pos->overlay_string_index;
3010 relative_index = (it->current.overlay_string_index
3011 % OVERLAY_STRING_CHUNK_SIZE);
3012 it->string = it->overlay_strings[relative_index];
3013 xassert (STRINGP (it->string));
3014 it->current.string_pos = pos->string_pos;
3015 it->method = GET_FROM_STRING;
3018 if (CHARPOS (pos->string_pos) >= 0)
3020 /* Recorded position is not in an overlay string, but in another
3021 string. This can only be a string from a `display' property.
3022 IT should already be filled with that string. */
3023 it->current.string_pos = pos->string_pos;
3024 xassert (STRINGP (it->string));
3027 /* Restore position in display vector translations, control
3028 character translations or ellipses. */
3029 if (pos->dpvec_index >= 0)
3031 if (it->dpvec == NULL)
3032 get_next_display_element (it);
3033 xassert (it->dpvec && it->current.dpvec_index == 0);
3034 it->current.dpvec_index = pos->dpvec_index;
3037 CHECK_IT (it);
3038 return !overlay_strings_with_newlines;
3042 /* Initialize IT for stepping through current_buffer in window W
3043 starting at ROW->start. */
3045 static void
3046 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3048 init_from_display_pos (it, w, &row->start);
3049 it->start = row->start;
3050 it->continuation_lines_width = row->continuation_lines_width;
3051 CHECK_IT (it);
3055 /* Initialize IT for stepping through current_buffer in window W
3056 starting in the line following ROW, i.e. starting at ROW->end.
3057 Value is zero if there are overlay strings with newlines at ROW's
3058 end position. */
3060 static int
3061 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3063 int success = 0;
3065 if (init_from_display_pos (it, w, &row->end))
3067 if (row->continued_p)
3068 it->continuation_lines_width
3069 = row->continuation_lines_width + row->pixel_width;
3070 CHECK_IT (it);
3071 success = 1;
3074 return success;
3080 /***********************************************************************
3081 Text properties
3082 ***********************************************************************/
3084 /* Called when IT reaches IT->stop_charpos. Handle text property and
3085 overlay changes. Set IT->stop_charpos to the next position where
3086 to stop. */
3088 static void
3089 handle_stop (struct it *it)
3091 enum prop_handled handled;
3092 int handle_overlay_change_p;
3093 struct props *p;
3095 it->dpvec = NULL;
3096 it->current.dpvec_index = -1;
3097 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3098 it->ignore_overlay_strings_at_pos_p = 0;
3099 it->ellipsis_p = 0;
3101 /* Use face of preceding text for ellipsis (if invisible) */
3102 if (it->selective_display_ellipsis_p)
3103 it->saved_face_id = it->face_id;
3107 handled = HANDLED_NORMALLY;
3109 /* Call text property handlers. */
3110 for (p = it_props; p->handler; ++p)
3112 handled = p->handler (it);
3114 if (handled == HANDLED_RECOMPUTE_PROPS)
3115 break;
3116 else if (handled == HANDLED_RETURN)
3118 /* We still want to show before and after strings from
3119 overlays even if the actual buffer text is replaced. */
3120 if (!handle_overlay_change_p
3121 || it->sp > 1
3122 || !get_overlay_strings_1 (it, 0, 0))
3124 if (it->ellipsis_p)
3125 setup_for_ellipsis (it, 0);
3126 /* When handling a display spec, we might load an
3127 empty string. In that case, discard it here. We
3128 used to discard it in handle_single_display_spec,
3129 but that causes get_overlay_strings_1, above, to
3130 ignore overlay strings that we must check. */
3131 if (STRINGP (it->string) && !SCHARS (it->string))
3132 pop_it (it);
3133 return;
3135 else if (STRINGP (it->string) && !SCHARS (it->string))
3136 pop_it (it);
3137 else
3139 it->ignore_overlay_strings_at_pos_p = 1;
3140 it->string_from_display_prop_p = 0;
3141 handle_overlay_change_p = 0;
3143 handled = HANDLED_RECOMPUTE_PROPS;
3144 break;
3146 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3147 handle_overlay_change_p = 0;
3150 if (handled != HANDLED_RECOMPUTE_PROPS)
3152 /* Don't check for overlay strings below when set to deliver
3153 characters from a display vector. */
3154 if (it->method == GET_FROM_DISPLAY_VECTOR)
3155 handle_overlay_change_p = 0;
3157 /* Handle overlay changes.
3158 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3159 if it finds overlays. */
3160 if (handle_overlay_change_p)
3161 handled = handle_overlay_change (it);
3164 if (it->ellipsis_p)
3166 setup_for_ellipsis (it, 0);
3167 break;
3170 while (handled == HANDLED_RECOMPUTE_PROPS);
3172 /* Determine where to stop next. */
3173 if (handled == HANDLED_NORMALLY)
3174 compute_stop_pos (it);
3178 /* Compute IT->stop_charpos from text property and overlay change
3179 information for IT's current position. */
3181 static void
3182 compute_stop_pos (struct it *it)
3184 register INTERVAL iv, next_iv;
3185 Lisp_Object object, limit, position;
3186 EMACS_INT charpos, bytepos;
3188 /* If nowhere else, stop at the end. */
3189 it->stop_charpos = it->end_charpos;
3191 if (STRINGP (it->string))
3193 /* Strings are usually short, so don't limit the search for
3194 properties. */
3195 object = it->string;
3196 limit = Qnil;
3197 charpos = IT_STRING_CHARPOS (*it);
3198 bytepos = IT_STRING_BYTEPOS (*it);
3200 else
3202 EMACS_INT pos;
3204 /* If next overlay change is in front of the current stop pos
3205 (which is IT->end_charpos), stop there. Note: value of
3206 next_overlay_change is point-max if no overlay change
3207 follows. */
3208 charpos = IT_CHARPOS (*it);
3209 bytepos = IT_BYTEPOS (*it);
3210 pos = next_overlay_change (charpos);
3211 if (pos < it->stop_charpos)
3212 it->stop_charpos = pos;
3214 /* If showing the region, we have to stop at the region
3215 start or end because the face might change there. */
3216 if (it->region_beg_charpos > 0)
3218 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3219 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3220 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3221 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3224 /* Set up variables for computing the stop position from text
3225 property changes. */
3226 XSETBUFFER (object, current_buffer);
3227 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3230 /* Get the interval containing IT's position. Value is a null
3231 interval if there isn't such an interval. */
3232 position = make_number (charpos);
3233 iv = validate_interval_range (object, &position, &position, 0);
3234 if (!NULL_INTERVAL_P (iv))
3236 Lisp_Object values_here[LAST_PROP_IDX];
3237 struct props *p;
3239 /* Get properties here. */
3240 for (p = it_props; p->handler; ++p)
3241 values_here[p->idx] = textget (iv->plist, *p->name);
3243 /* Look for an interval following iv that has different
3244 properties. */
3245 for (next_iv = next_interval (iv);
3246 (!NULL_INTERVAL_P (next_iv)
3247 && (NILP (limit)
3248 || XFASTINT (limit) > next_iv->position));
3249 next_iv = next_interval (next_iv))
3251 for (p = it_props; p->handler; ++p)
3253 Lisp_Object new_value;
3255 new_value = textget (next_iv->plist, *p->name);
3256 if (!EQ (values_here[p->idx], new_value))
3257 break;
3260 if (p->handler)
3261 break;
3264 if (!NULL_INTERVAL_P (next_iv))
3266 if (INTEGERP (limit)
3267 && next_iv->position >= XFASTINT (limit))
3268 /* No text property change up to limit. */
3269 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3270 else
3271 /* Text properties change in next_iv. */
3272 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3276 if (it->cmp_it.id < 0)
3278 EMACS_INT stoppos = it->end_charpos;
3280 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3281 stoppos = -1;
3282 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3283 stoppos, it->string);
3286 xassert (STRINGP (it->string)
3287 || (it->stop_charpos >= BEGV
3288 && it->stop_charpos >= IT_CHARPOS (*it)));
3292 /* Return the position of the next overlay change after POS in
3293 current_buffer. Value is point-max if no overlay change
3294 follows. This is like `next-overlay-change' but doesn't use
3295 xmalloc. */
3297 static EMACS_INT
3298 next_overlay_change (EMACS_INT pos)
3300 int noverlays;
3301 EMACS_INT endpos;
3302 Lisp_Object *overlays;
3303 int i;
3305 /* Get all overlays at the given position. */
3306 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3308 /* If any of these overlays ends before endpos,
3309 use its ending point instead. */
3310 for (i = 0; i < noverlays; ++i)
3312 Lisp_Object oend;
3313 EMACS_INT oendpos;
3315 oend = OVERLAY_END (overlays[i]);
3316 oendpos = OVERLAY_POSITION (oend);
3317 endpos = min (endpos, oendpos);
3320 return endpos;
3325 /***********************************************************************
3326 Fontification
3327 ***********************************************************************/
3329 /* Handle changes in the `fontified' property of the current buffer by
3330 calling hook functions from Qfontification_functions to fontify
3331 regions of text. */
3333 static enum prop_handled
3334 handle_fontified_prop (struct it *it)
3336 Lisp_Object prop, pos;
3337 enum prop_handled handled = HANDLED_NORMALLY;
3339 if (!NILP (Vmemory_full))
3340 return handled;
3342 /* Get the value of the `fontified' property at IT's current buffer
3343 position. (The `fontified' property doesn't have a special
3344 meaning in strings.) If the value is nil, call functions from
3345 Qfontification_functions. */
3346 if (!STRINGP (it->string)
3347 && it->s == NULL
3348 && !NILP (Vfontification_functions)
3349 && !NILP (Vrun_hooks)
3350 && (pos = make_number (IT_CHARPOS (*it)),
3351 prop = Fget_char_property (pos, Qfontified, Qnil),
3352 /* Ignore the special cased nil value always present at EOB since
3353 no amount of fontifying will be able to change it. */
3354 NILP (prop) && IT_CHARPOS (*it) < Z))
3356 int count = SPECPDL_INDEX ();
3357 Lisp_Object val;
3359 val = Vfontification_functions;
3360 specbind (Qfontification_functions, Qnil);
3362 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3363 safe_call1 (val, pos);
3364 else
3366 Lisp_Object globals, fn;
3367 struct gcpro gcpro1, gcpro2;
3369 globals = Qnil;
3370 GCPRO2 (val, globals);
3372 for (; CONSP (val); val = XCDR (val))
3374 fn = XCAR (val);
3376 if (EQ (fn, Qt))
3378 /* A value of t indicates this hook has a local
3379 binding; it means to run the global binding too.
3380 In a global value, t should not occur. If it
3381 does, we must ignore it to avoid an endless
3382 loop. */
3383 for (globals = Fdefault_value (Qfontification_functions);
3384 CONSP (globals);
3385 globals = XCDR (globals))
3387 fn = XCAR (globals);
3388 if (!EQ (fn, Qt))
3389 safe_call1 (fn, pos);
3392 else
3393 safe_call1 (fn, pos);
3396 UNGCPRO;
3399 unbind_to (count, Qnil);
3401 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3402 something. This avoids an endless loop if they failed to
3403 fontify the text for which reason ever. */
3404 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3405 handled = HANDLED_RECOMPUTE_PROPS;
3408 return handled;
3413 /***********************************************************************
3414 Faces
3415 ***********************************************************************/
3417 /* Set up iterator IT from face properties at its current position.
3418 Called from handle_stop. */
3420 static enum prop_handled
3421 handle_face_prop (struct it *it)
3423 int new_face_id;
3424 EMACS_INT next_stop;
3426 if (!STRINGP (it->string))
3428 new_face_id
3429 = face_at_buffer_position (it->w,
3430 IT_CHARPOS (*it),
3431 it->region_beg_charpos,
3432 it->region_end_charpos,
3433 &next_stop,
3434 (IT_CHARPOS (*it)
3435 + TEXT_PROP_DISTANCE_LIMIT),
3436 0, it->base_face_id);
3438 /* Is this a start of a run of characters with box face?
3439 Caveat: this can be called for a freshly initialized
3440 iterator; face_id is -1 in this case. We know that the new
3441 face will not change until limit, i.e. if the new face has a
3442 box, all characters up to limit will have one. But, as
3443 usual, we don't know whether limit is really the end. */
3444 if (new_face_id != it->face_id)
3446 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3448 /* If new face has a box but old face has not, this is
3449 the start of a run of characters with box, i.e. it has
3450 a shadow on the left side. The value of face_id of the
3451 iterator will be -1 if this is the initial call that gets
3452 the face. In this case, we have to look in front of IT's
3453 position and see whether there is a face != new_face_id. */
3454 it->start_of_box_run_p
3455 = (new_face->box != FACE_NO_BOX
3456 && (it->face_id >= 0
3457 || IT_CHARPOS (*it) == BEG
3458 || new_face_id != face_before_it_pos (it)));
3459 it->face_box_p = new_face->box != FACE_NO_BOX;
3462 else
3464 int base_face_id;
3465 EMACS_INT bufpos;
3466 int i;
3467 Lisp_Object from_overlay
3468 = (it->current.overlay_string_index >= 0
3469 ? it->string_overlays[it->current.overlay_string_index]
3470 : Qnil);
3472 /* See if we got to this string directly or indirectly from
3473 an overlay property. That includes the before-string or
3474 after-string of an overlay, strings in display properties
3475 provided by an overlay, their text properties, etc.
3477 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3478 if (! NILP (from_overlay))
3479 for (i = it->sp - 1; i >= 0; i--)
3481 if (it->stack[i].current.overlay_string_index >= 0)
3482 from_overlay
3483 = it->string_overlays[it->stack[i].current.overlay_string_index];
3484 else if (! NILP (it->stack[i].from_overlay))
3485 from_overlay = it->stack[i].from_overlay;
3487 if (!NILP (from_overlay))
3488 break;
3491 if (! NILP (from_overlay))
3493 bufpos = IT_CHARPOS (*it);
3494 /* For a string from an overlay, the base face depends
3495 only on text properties and ignores overlays. */
3496 base_face_id
3497 = face_for_overlay_string (it->w,
3498 IT_CHARPOS (*it),
3499 it->region_beg_charpos,
3500 it->region_end_charpos,
3501 &next_stop,
3502 (IT_CHARPOS (*it)
3503 + TEXT_PROP_DISTANCE_LIMIT),
3505 from_overlay);
3507 else
3509 bufpos = 0;
3511 /* For strings from a `display' property, use the face at
3512 IT's current buffer position as the base face to merge
3513 with, so that overlay strings appear in the same face as
3514 surrounding text, unless they specify their own
3515 faces. */
3516 base_face_id = underlying_face_id (it);
3519 new_face_id = face_at_string_position (it->w,
3520 it->string,
3521 IT_STRING_CHARPOS (*it),
3522 bufpos,
3523 it->region_beg_charpos,
3524 it->region_end_charpos,
3525 &next_stop,
3526 base_face_id, 0);
3528 /* Is this a start of a run of characters with box? Caveat:
3529 this can be called for a freshly allocated iterator; face_id
3530 is -1 is this case. We know that the new face will not
3531 change until the next check pos, i.e. if the new face has a
3532 box, all characters up to that position will have a
3533 box. But, as usual, we don't know whether that position
3534 is really the end. */
3535 if (new_face_id != it->face_id)
3537 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3538 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3540 /* If new face has a box but old face hasn't, this is the
3541 start of a run of characters with box, i.e. it has a
3542 shadow on the left side. */
3543 it->start_of_box_run_p
3544 = new_face->box && (old_face == NULL || !old_face->box);
3545 it->face_box_p = new_face->box != FACE_NO_BOX;
3549 it->face_id = new_face_id;
3550 return HANDLED_NORMALLY;
3554 /* Return the ID of the face ``underlying'' IT's current position,
3555 which is in a string. If the iterator is associated with a
3556 buffer, return the face at IT's current buffer position.
3557 Otherwise, use the iterator's base_face_id. */
3559 static int
3560 underlying_face_id (struct it *it)
3562 int face_id = it->base_face_id, i;
3564 xassert (STRINGP (it->string));
3566 for (i = it->sp - 1; i >= 0; --i)
3567 if (NILP (it->stack[i].string))
3568 face_id = it->stack[i].face_id;
3570 return face_id;
3574 /* Compute the face one character before or after the current position
3575 of IT. BEFORE_P non-zero means get the face in front of IT's
3576 position. Value is the id of the face. */
3578 static int
3579 face_before_or_after_it_pos (struct it *it, int before_p)
3581 int face_id, limit;
3582 EMACS_INT next_check_charpos;
3583 struct text_pos pos;
3585 xassert (it->s == NULL);
3587 if (STRINGP (it->string))
3589 EMACS_INT bufpos;
3590 int base_face_id;
3592 /* No face change past the end of the string (for the case
3593 we are padding with spaces). No face change before the
3594 string start. */
3595 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3596 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3597 return it->face_id;
3599 /* Set pos to the position before or after IT's current position. */
3600 if (before_p)
3601 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3602 else
3603 /* For composition, we must check the character after the
3604 composition. */
3605 pos = (it->what == IT_COMPOSITION
3606 ? string_pos (IT_STRING_CHARPOS (*it)
3607 + it->cmp_it.nchars, it->string)
3608 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3610 if (it->current.overlay_string_index >= 0)
3611 bufpos = IT_CHARPOS (*it);
3612 else
3613 bufpos = 0;
3615 base_face_id = underlying_face_id (it);
3617 /* Get the face for ASCII, or unibyte. */
3618 face_id = face_at_string_position (it->w,
3619 it->string,
3620 CHARPOS (pos),
3621 bufpos,
3622 it->region_beg_charpos,
3623 it->region_end_charpos,
3624 &next_check_charpos,
3625 base_face_id, 0);
3627 /* Correct the face for charsets different from ASCII. Do it
3628 for the multibyte case only. The face returned above is
3629 suitable for unibyte text if IT->string is unibyte. */
3630 if (STRING_MULTIBYTE (it->string))
3632 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3633 int c, len;
3634 struct face *face = FACE_FROM_ID (it->f, face_id);
3636 c = string_char_and_length (p, &len);
3637 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3640 else
3642 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3643 || (IT_CHARPOS (*it) <= BEGV && before_p))
3644 return it->face_id;
3646 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3647 pos = it->current.pos;
3649 if (before_p)
3650 DEC_TEXT_POS (pos, it->multibyte_p);
3651 else
3653 if (it->what == IT_COMPOSITION)
3654 /* For composition, we must check the position after the
3655 composition. */
3656 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3657 else
3658 INC_TEXT_POS (pos, it->multibyte_p);
3661 /* Determine face for CHARSET_ASCII, or unibyte. */
3662 face_id = face_at_buffer_position (it->w,
3663 CHARPOS (pos),
3664 it->region_beg_charpos,
3665 it->region_end_charpos,
3666 &next_check_charpos,
3667 limit, 0, -1);
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 current_buffer is unibyte. */
3672 if (it->multibyte_p)
3674 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3675 struct face *face = FACE_FROM_ID (it->f, face_id);
3676 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3680 return face_id;
3685 /***********************************************************************
3686 Invisible text
3687 ***********************************************************************/
3689 /* Set up iterator IT from invisible properties at its current
3690 position. Called from handle_stop. */
3692 static enum prop_handled
3693 handle_invisible_prop (struct it *it)
3695 enum prop_handled handled = HANDLED_NORMALLY;
3697 if (STRINGP (it->string))
3699 Lisp_Object prop, end_charpos, limit, charpos;
3701 /* Get the value of the invisible text property at the
3702 current position. Value will be nil if there is no such
3703 property. */
3704 charpos = make_number (IT_STRING_CHARPOS (*it));
3705 prop = Fget_text_property (charpos, Qinvisible, it->string);
3707 if (!NILP (prop)
3708 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3710 handled = HANDLED_RECOMPUTE_PROPS;
3712 /* Get the position at which the next change of the
3713 invisible text property can be found in IT->string.
3714 Value will be nil if the property value is the same for
3715 all the rest of IT->string. */
3716 XSETINT (limit, SCHARS (it->string));
3717 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3718 it->string, limit);
3720 /* Text at current position is invisible. The next
3721 change in the property is at position end_charpos.
3722 Move IT's current position to that position. */
3723 if (INTEGERP (end_charpos)
3724 && XFASTINT (end_charpos) < XFASTINT (limit))
3726 struct text_pos old;
3727 old = it->current.string_pos;
3728 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3729 compute_string_pos (&it->current.string_pos, old, it->string);
3731 else
3733 /* The rest of the string is invisible. If this is an
3734 overlay string, proceed with the next overlay string
3735 or whatever comes and return a character from there. */
3736 if (it->current.overlay_string_index >= 0)
3738 next_overlay_string (it);
3739 /* Don't check for overlay strings when we just
3740 finished processing them. */
3741 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3743 else
3745 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3746 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3751 else
3753 int invis_p;
3754 EMACS_INT newpos, next_stop, start_charpos, tem;
3755 Lisp_Object pos, prop, overlay;
3757 /* First of all, is there invisible text at this position? */
3758 tem = start_charpos = IT_CHARPOS (*it);
3759 pos = make_number (tem);
3760 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3761 &overlay);
3762 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3764 /* If we are on invisible text, skip over it. */
3765 if (invis_p && start_charpos < it->end_charpos)
3767 /* Record whether we have to display an ellipsis for the
3768 invisible text. */
3769 int display_ellipsis_p = invis_p == 2;
3771 handled = HANDLED_RECOMPUTE_PROPS;
3773 /* Loop skipping over invisible text. The loop is left at
3774 ZV or with IT on the first char being visible again. */
3777 /* Try to skip some invisible text. Return value is the
3778 position reached which can be equal to where we start
3779 if there is nothing invisible there. This skips both
3780 over invisible text properties and overlays with
3781 invisible property. */
3782 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3784 /* If we skipped nothing at all we weren't at invisible
3785 text in the first place. If everything to the end of
3786 the buffer was skipped, end the loop. */
3787 if (newpos == tem || newpos >= ZV)
3788 invis_p = 0;
3789 else
3791 /* We skipped some characters but not necessarily
3792 all there are. Check if we ended up on visible
3793 text. Fget_char_property returns the property of
3794 the char before the given position, i.e. if we
3795 get invis_p = 0, this means that the char at
3796 newpos is visible. */
3797 pos = make_number (newpos);
3798 prop = Fget_char_property (pos, Qinvisible, it->window);
3799 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3802 /* If we ended up on invisible text, proceed to
3803 skip starting with next_stop. */
3804 if (invis_p)
3805 tem = next_stop;
3807 /* If there are adjacent invisible texts, don't lose the
3808 second one's ellipsis. */
3809 if (invis_p == 2)
3810 display_ellipsis_p = 1;
3812 while (invis_p);
3814 /* The position newpos is now either ZV or on visible text. */
3815 if (it->bidi_p && newpos < ZV)
3817 /* With bidi iteration, the region of invisible text
3818 could start and/or end in the middle of a non-base
3819 embedding level. Therefore, we need to skip
3820 invisible text using the bidi iterator, starting at
3821 IT's current position, until we find ourselves
3822 outside the invisible text. Skipping invisible text
3823 _after_ bidi iteration avoids affecting the visual
3824 order of the displayed text when invisible properties
3825 are added or removed. */
3826 if (it->bidi_it.first_elt)
3828 /* If we were `reseat'ed to a new paragraph,
3829 determine the paragraph base direction. We need
3830 to do it now because next_element_from_buffer may
3831 not have a chance to do it, if we are going to
3832 skip any text at the beginning, which resets the
3833 FIRST_ELT flag. */
3834 bidi_paragraph_init (it->paragraph_embedding,
3835 &it->bidi_it, 1);
3839 bidi_move_to_visually_next (&it->bidi_it);
3841 while (it->stop_charpos <= it->bidi_it.charpos
3842 && it->bidi_it.charpos < newpos);
3843 IT_CHARPOS (*it) = it->bidi_it.charpos;
3844 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3845 /* If we overstepped NEWPOS, record its position in the
3846 iterator, so that we skip invisible text if later the
3847 bidi iteration lands us in the invisible region
3848 again. */
3849 if (IT_CHARPOS (*it) >= newpos)
3850 it->prev_stop = newpos;
3852 else
3854 IT_CHARPOS (*it) = newpos;
3855 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3858 /* If there are before-strings at the start of invisible
3859 text, and the text is invisible because of a text
3860 property, arrange to show before-strings because 20.x did
3861 it that way. (If the text is invisible because of an
3862 overlay property instead of a text property, this is
3863 already handled in the overlay code.) */
3864 if (NILP (overlay)
3865 && get_overlay_strings (it, it->stop_charpos))
3867 handled = HANDLED_RECOMPUTE_PROPS;
3868 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3870 else if (display_ellipsis_p)
3872 /* Make sure that the glyphs of the ellipsis will get
3873 correct `charpos' values. If we would not update
3874 it->position here, the glyphs would belong to the
3875 last visible character _before_ the invisible
3876 text, which confuses `set_cursor_from_row'.
3878 We use the last invisible position instead of the
3879 first because this way the cursor is always drawn on
3880 the first "." of the ellipsis, whenever PT is inside
3881 the invisible text. Otherwise the cursor would be
3882 placed _after_ the ellipsis when the point is after the
3883 first invisible character. */
3884 if (!STRINGP (it->object))
3886 it->position.charpos = newpos - 1;
3887 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3889 it->ellipsis_p = 1;
3890 /* Let the ellipsis display before
3891 considering any properties of the following char.
3892 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3893 handled = HANDLED_RETURN;
3898 return handled;
3902 /* Make iterator IT return `...' next.
3903 Replaces LEN characters from buffer. */
3905 static void
3906 setup_for_ellipsis (struct it *it, int len)
3908 /* Use the display table definition for `...'. Invalid glyphs
3909 will be handled by the method returning elements from dpvec. */
3910 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3912 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3913 it->dpvec = v->contents;
3914 it->dpend = v->contents + v->size;
3916 else
3918 /* Default `...'. */
3919 it->dpvec = default_invis_vector;
3920 it->dpend = default_invis_vector + 3;
3923 it->dpvec_char_len = len;
3924 it->current.dpvec_index = 0;
3925 it->dpvec_face_id = -1;
3927 /* Remember the current face id in case glyphs specify faces.
3928 IT's face is restored in set_iterator_to_next.
3929 saved_face_id was set to preceding char's face in handle_stop. */
3930 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3931 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3933 it->method = GET_FROM_DISPLAY_VECTOR;
3934 it->ellipsis_p = 1;
3939 /***********************************************************************
3940 'display' property
3941 ***********************************************************************/
3943 /* Set up iterator IT from `display' property at its current position.
3944 Called from handle_stop.
3945 We return HANDLED_RETURN if some part of the display property
3946 overrides the display of the buffer text itself.
3947 Otherwise we return HANDLED_NORMALLY. */
3949 static enum prop_handled
3950 handle_display_prop (struct it *it)
3952 Lisp_Object prop, object, overlay;
3953 struct text_pos *position;
3954 /* Nonzero if some property replaces the display of the text itself. */
3955 int display_replaced_p = 0;
3957 if (STRINGP (it->string))
3959 object = it->string;
3960 position = &it->current.string_pos;
3962 else
3964 XSETWINDOW (object, it->w);
3965 position = &it->current.pos;
3968 /* Reset those iterator values set from display property values. */
3969 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3970 it->space_width = Qnil;
3971 it->font_height = Qnil;
3972 it->voffset = 0;
3974 /* We don't support recursive `display' properties, i.e. string
3975 values that have a string `display' property, that have a string
3976 `display' property etc. */
3977 if (!it->string_from_display_prop_p)
3978 it->area = TEXT_AREA;
3980 prop = get_char_property_and_overlay (make_number (position->charpos),
3981 Qdisplay, object, &overlay);
3982 if (NILP (prop))
3983 return HANDLED_NORMALLY;
3984 /* Now OVERLAY is the overlay that gave us this property, or nil
3985 if it was a text property. */
3987 if (!STRINGP (it->string))
3988 object = it->w->buffer;
3990 if (CONSP (prop)
3991 /* Simple properties. */
3992 && !EQ (XCAR (prop), Qimage)
3993 && !EQ (XCAR (prop), Qspace)
3994 && !EQ (XCAR (prop), Qwhen)
3995 && !EQ (XCAR (prop), Qslice)
3996 && !EQ (XCAR (prop), Qspace_width)
3997 && !EQ (XCAR (prop), Qheight)
3998 && !EQ (XCAR (prop), Qraise)
3999 /* Marginal area specifications. */
4000 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
4001 && !EQ (XCAR (prop), Qleft_fringe)
4002 && !EQ (XCAR (prop), Qright_fringe)
4003 && !NILP (XCAR (prop)))
4005 for (; CONSP (prop); prop = XCDR (prop))
4007 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
4008 position, display_replaced_p))
4010 display_replaced_p = 1;
4011 /* If some text in a string is replaced, `position' no
4012 longer points to the position of `object'. */
4013 if (STRINGP (object))
4014 break;
4018 else if (VECTORP (prop))
4020 int i;
4021 for (i = 0; i < ASIZE (prop); ++i)
4022 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4023 position, display_replaced_p))
4025 display_replaced_p = 1;
4026 /* If some text in a string is replaced, `position' no
4027 longer points to the position of `object'. */
4028 if (STRINGP (object))
4029 break;
4032 else
4034 if (handle_single_display_spec (it, prop, object, overlay,
4035 position, 0))
4036 display_replaced_p = 1;
4039 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4043 /* Value is the position of the end of the `display' property starting
4044 at START_POS in OBJECT. */
4046 static struct text_pos
4047 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4049 Lisp_Object end;
4050 struct text_pos end_pos;
4052 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4053 Qdisplay, object, Qnil);
4054 CHARPOS (end_pos) = XFASTINT (end);
4055 if (STRINGP (object))
4056 compute_string_pos (&end_pos, start_pos, it->string);
4057 else
4058 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4060 return end_pos;
4064 /* Set up IT from a single `display' specification PROP. OBJECT
4065 is the object in which the `display' property was found. *POSITION
4066 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4067 means that we previously saw a display specification which already
4068 replaced text display with something else, for example an image;
4069 we ignore such properties after the first one has been processed.
4071 OVERLAY is the overlay this `display' property came from,
4072 or nil if it was a text property.
4074 If PROP is a `space' or `image' specification, and in some other
4075 cases too, set *POSITION to the position where the `display'
4076 property ends.
4078 Value is non-zero if something was found which replaces the display
4079 of buffer or string text. */
4081 static int
4082 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4083 Lisp_Object overlay, struct text_pos *position,
4084 int display_replaced_before_p)
4086 Lisp_Object form;
4087 Lisp_Object location, value;
4088 struct text_pos start_pos, save_pos;
4089 int valid_p;
4091 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4092 If the result is non-nil, use VALUE instead of SPEC. */
4093 form = Qt;
4094 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4096 spec = XCDR (spec);
4097 if (!CONSP (spec))
4098 return 0;
4099 form = XCAR (spec);
4100 spec = XCDR (spec);
4103 if (!NILP (form) && !EQ (form, Qt))
4105 int count = SPECPDL_INDEX ();
4106 struct gcpro gcpro1;
4108 /* Bind `object' to the object having the `display' property, a
4109 buffer or string. Bind `position' to the position in the
4110 object where the property was found, and `buffer-position'
4111 to the current position in the buffer. */
4112 specbind (Qobject, object);
4113 specbind (Qposition, make_number (CHARPOS (*position)));
4114 specbind (Qbuffer_position,
4115 make_number (STRINGP (object)
4116 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4117 GCPRO1 (form);
4118 form = safe_eval (form);
4119 UNGCPRO;
4120 unbind_to (count, Qnil);
4123 if (NILP (form))
4124 return 0;
4126 /* Handle `(height HEIGHT)' specifications. */
4127 if (CONSP (spec)
4128 && EQ (XCAR (spec), Qheight)
4129 && CONSP (XCDR (spec)))
4131 if (!FRAME_WINDOW_P (it->f))
4132 return 0;
4134 it->font_height = XCAR (XCDR (spec));
4135 if (!NILP (it->font_height))
4137 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4138 int new_height = -1;
4140 if (CONSP (it->font_height)
4141 && (EQ (XCAR (it->font_height), Qplus)
4142 || EQ (XCAR (it->font_height), Qminus))
4143 && CONSP (XCDR (it->font_height))
4144 && INTEGERP (XCAR (XCDR (it->font_height))))
4146 /* `(+ N)' or `(- N)' where N is an integer. */
4147 int steps = XINT (XCAR (XCDR (it->font_height)));
4148 if (EQ (XCAR (it->font_height), Qplus))
4149 steps = - steps;
4150 it->face_id = smaller_face (it->f, it->face_id, steps);
4152 else if (FUNCTIONP (it->font_height))
4154 /* Call function with current height as argument.
4155 Value is the new height. */
4156 Lisp_Object height;
4157 height = safe_call1 (it->font_height,
4158 face->lface[LFACE_HEIGHT_INDEX]);
4159 if (NUMBERP (height))
4160 new_height = XFLOATINT (height);
4162 else if (NUMBERP (it->font_height))
4164 /* Value is a multiple of the canonical char height. */
4165 struct face *face;
4167 face = FACE_FROM_ID (it->f,
4168 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4169 new_height = (XFLOATINT (it->font_height)
4170 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4172 else
4174 /* Evaluate IT->font_height with `height' bound to the
4175 current specified height to get the new height. */
4176 int count = SPECPDL_INDEX ();
4178 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4179 value = safe_eval (it->font_height);
4180 unbind_to (count, Qnil);
4182 if (NUMBERP (value))
4183 new_height = XFLOATINT (value);
4186 if (new_height > 0)
4187 it->face_id = face_with_height (it->f, it->face_id, new_height);
4190 return 0;
4193 /* Handle `(space-width WIDTH)'. */
4194 if (CONSP (spec)
4195 && EQ (XCAR (spec), Qspace_width)
4196 && CONSP (XCDR (spec)))
4198 if (!FRAME_WINDOW_P (it->f))
4199 return 0;
4201 value = XCAR (XCDR (spec));
4202 if (NUMBERP (value) && XFLOATINT (value) > 0)
4203 it->space_width = value;
4205 return 0;
4208 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4209 if (CONSP (spec)
4210 && EQ (XCAR (spec), Qslice))
4212 Lisp_Object tem;
4214 if (!FRAME_WINDOW_P (it->f))
4215 return 0;
4217 if (tem = XCDR (spec), CONSP (tem))
4219 it->slice.x = XCAR (tem);
4220 if (tem = XCDR (tem), CONSP (tem))
4222 it->slice.y = XCAR (tem);
4223 if (tem = XCDR (tem), CONSP (tem))
4225 it->slice.width = XCAR (tem);
4226 if (tem = XCDR (tem), CONSP (tem))
4227 it->slice.height = XCAR (tem);
4232 return 0;
4235 /* Handle `(raise FACTOR)'. */
4236 if (CONSP (spec)
4237 && EQ (XCAR (spec), Qraise)
4238 && CONSP (XCDR (spec)))
4240 if (!FRAME_WINDOW_P (it->f))
4241 return 0;
4243 #ifdef HAVE_WINDOW_SYSTEM
4244 value = XCAR (XCDR (spec));
4245 if (NUMBERP (value))
4247 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4248 it->voffset = - (XFLOATINT (value)
4249 * (FONT_HEIGHT (face->font)));
4251 #endif /* HAVE_WINDOW_SYSTEM */
4253 return 0;
4256 /* Don't handle the other kinds of display specifications
4257 inside a string that we got from a `display' property. */
4258 if (it->string_from_display_prop_p)
4259 return 0;
4261 /* Characters having this form of property are not displayed, so
4262 we have to find the end of the property. */
4263 start_pos = *position;
4264 *position = display_prop_end (it, object, start_pos);
4265 value = Qnil;
4267 /* Stop the scan at that end position--we assume that all
4268 text properties change there. */
4269 it->stop_charpos = position->charpos;
4271 /* Handle `(left-fringe BITMAP [FACE])'
4272 and `(right-fringe BITMAP [FACE])'. */
4273 if (CONSP (spec)
4274 && (EQ (XCAR (spec), Qleft_fringe)
4275 || EQ (XCAR (spec), Qright_fringe))
4276 && CONSP (XCDR (spec)))
4278 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4279 int fringe_bitmap;
4281 if (!FRAME_WINDOW_P (it->f))
4282 /* If we return here, POSITION has been advanced
4283 across the text with this property. */
4284 return 0;
4286 #ifdef HAVE_WINDOW_SYSTEM
4287 value = XCAR (XCDR (spec));
4288 if (!SYMBOLP (value)
4289 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4290 /* If we return here, POSITION has been advanced
4291 across the text with this property. */
4292 return 0;
4294 if (CONSP (XCDR (XCDR (spec))))
4296 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4297 int face_id2 = lookup_derived_face (it->f, face_name,
4298 FRINGE_FACE_ID, 0);
4299 if (face_id2 >= 0)
4300 face_id = face_id2;
4303 /* Save current settings of IT so that we can restore them
4304 when we are finished with the glyph property value. */
4306 save_pos = it->position;
4307 it->position = *position;
4308 push_it (it);
4309 it->position = save_pos;
4311 it->area = TEXT_AREA;
4312 it->what = IT_IMAGE;
4313 it->image_id = -1; /* no image */
4314 it->position = start_pos;
4315 it->object = NILP (object) ? it->w->buffer : object;
4316 it->method = GET_FROM_IMAGE;
4317 it->from_overlay = Qnil;
4318 it->face_id = face_id;
4320 /* Say that we haven't consumed the characters with
4321 `display' property yet. The call to pop_it in
4322 set_iterator_to_next will clean this up. */
4323 *position = start_pos;
4325 if (EQ (XCAR (spec), Qleft_fringe))
4327 it->left_user_fringe_bitmap = fringe_bitmap;
4328 it->left_user_fringe_face_id = face_id;
4330 else
4332 it->right_user_fringe_bitmap = fringe_bitmap;
4333 it->right_user_fringe_face_id = face_id;
4335 #endif /* HAVE_WINDOW_SYSTEM */
4336 return 1;
4339 /* Prepare to handle `((margin left-margin) ...)',
4340 `((margin right-margin) ...)' and `((margin nil) ...)'
4341 prefixes for display specifications. */
4342 location = Qunbound;
4343 if (CONSP (spec) && CONSP (XCAR (spec)))
4345 Lisp_Object tem;
4347 value = XCDR (spec);
4348 if (CONSP (value))
4349 value = XCAR (value);
4351 tem = XCAR (spec);
4352 if (EQ (XCAR (tem), Qmargin)
4353 && (tem = XCDR (tem),
4354 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4355 (NILP (tem)
4356 || EQ (tem, Qleft_margin)
4357 || EQ (tem, Qright_margin))))
4358 location = tem;
4361 if (EQ (location, Qunbound))
4363 location = Qnil;
4364 value = spec;
4367 /* After this point, VALUE is the property after any
4368 margin prefix has been stripped. It must be a string,
4369 an image specification, or `(space ...)'.
4371 LOCATION specifies where to display: `left-margin',
4372 `right-margin' or nil. */
4374 valid_p = (STRINGP (value)
4375 #ifdef HAVE_WINDOW_SYSTEM
4376 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4377 #endif /* not HAVE_WINDOW_SYSTEM */
4378 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4380 if (valid_p && !display_replaced_before_p)
4382 /* Save current settings of IT so that we can restore them
4383 when we are finished with the glyph property value. */
4384 save_pos = it->position;
4385 it->position = *position;
4386 push_it (it);
4387 it->position = save_pos;
4388 it->from_overlay = overlay;
4390 if (NILP (location))
4391 it->area = TEXT_AREA;
4392 else if (EQ (location, Qleft_margin))
4393 it->area = LEFT_MARGIN_AREA;
4394 else
4395 it->area = RIGHT_MARGIN_AREA;
4397 if (STRINGP (value))
4399 it->string = value;
4400 it->multibyte_p = STRING_MULTIBYTE (it->string);
4401 it->current.overlay_string_index = -1;
4402 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4403 it->end_charpos = it->string_nchars = SCHARS (it->string);
4404 it->method = GET_FROM_STRING;
4405 it->stop_charpos = 0;
4406 it->string_from_display_prop_p = 1;
4407 /* Say that we haven't consumed the characters with
4408 `display' property yet. The call to pop_it in
4409 set_iterator_to_next will clean this up. */
4410 if (BUFFERP (object))
4411 *position = start_pos;
4413 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4415 it->method = GET_FROM_STRETCH;
4416 it->object = value;
4417 *position = it->position = start_pos;
4419 #ifdef HAVE_WINDOW_SYSTEM
4420 else
4422 it->what = IT_IMAGE;
4423 it->image_id = lookup_image (it->f, value);
4424 it->position = start_pos;
4425 it->object = NILP (object) ? it->w->buffer : object;
4426 it->method = GET_FROM_IMAGE;
4428 /* Say that we haven't consumed the characters with
4429 `display' property yet. The call to pop_it in
4430 set_iterator_to_next will clean this up. */
4431 *position = start_pos;
4433 #endif /* HAVE_WINDOW_SYSTEM */
4435 return 1;
4438 /* Invalid property or property not supported. Restore
4439 POSITION to what it was before. */
4440 *position = start_pos;
4441 return 0;
4445 /* Check if SPEC is a display sub-property value whose text should be
4446 treated as intangible. */
4448 static int
4449 single_display_spec_intangible_p (Lisp_Object prop)
4451 /* Skip over `when FORM'. */
4452 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4454 prop = XCDR (prop);
4455 if (!CONSP (prop))
4456 return 0;
4457 prop = XCDR (prop);
4460 if (STRINGP (prop))
4461 return 1;
4463 if (!CONSP (prop))
4464 return 0;
4466 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4467 we don't need to treat text as intangible. */
4468 if (EQ (XCAR (prop), Qmargin))
4470 prop = XCDR (prop);
4471 if (!CONSP (prop))
4472 return 0;
4474 prop = XCDR (prop);
4475 if (!CONSP (prop)
4476 || EQ (XCAR (prop), Qleft_margin)
4477 || EQ (XCAR (prop), Qright_margin))
4478 return 0;
4481 return (CONSP (prop)
4482 && (EQ (XCAR (prop), Qimage)
4483 || EQ (XCAR (prop), Qspace)));
4487 /* Check if PROP is a display property value whose text should be
4488 treated as intangible. */
4491 display_prop_intangible_p (Lisp_Object prop)
4493 if (CONSP (prop)
4494 && CONSP (XCAR (prop))
4495 && !EQ (Qmargin, XCAR (XCAR (prop))))
4497 /* A list of sub-properties. */
4498 while (CONSP (prop))
4500 if (single_display_spec_intangible_p (XCAR (prop)))
4501 return 1;
4502 prop = XCDR (prop);
4505 else if (VECTORP (prop))
4507 /* A vector of sub-properties. */
4508 int i;
4509 for (i = 0; i < ASIZE (prop); ++i)
4510 if (single_display_spec_intangible_p (AREF (prop, i)))
4511 return 1;
4513 else
4514 return single_display_spec_intangible_p (prop);
4516 return 0;
4520 /* Return 1 if PROP is a display sub-property value containing STRING. */
4522 static int
4523 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4525 if (EQ (string, prop))
4526 return 1;
4528 /* Skip over `when FORM'. */
4529 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4531 prop = XCDR (prop);
4532 if (!CONSP (prop))
4533 return 0;
4534 prop = XCDR (prop);
4537 if (CONSP (prop))
4538 /* Skip over `margin LOCATION'. */
4539 if (EQ (XCAR (prop), Qmargin))
4541 prop = XCDR (prop);
4542 if (!CONSP (prop))
4543 return 0;
4545 prop = XCDR (prop);
4546 if (!CONSP (prop))
4547 return 0;
4550 return CONSP (prop) && EQ (XCAR (prop), string);
4554 /* Return 1 if STRING appears in the `display' property PROP. */
4556 static int
4557 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4559 if (CONSP (prop)
4560 && CONSP (XCAR (prop))
4561 && !EQ (Qmargin, XCAR (XCAR (prop))))
4563 /* A list of sub-properties. */
4564 while (CONSP (prop))
4566 if (single_display_spec_string_p (XCAR (prop), string))
4567 return 1;
4568 prop = XCDR (prop);
4571 else if (VECTORP (prop))
4573 /* A vector of sub-properties. */
4574 int i;
4575 for (i = 0; i < ASIZE (prop); ++i)
4576 if (single_display_spec_string_p (AREF (prop, i), string))
4577 return 1;
4579 else
4580 return single_display_spec_string_p (prop, string);
4582 return 0;
4585 /* Look for STRING in overlays and text properties in W's buffer,
4586 between character positions FROM and TO (excluding TO).
4587 BACK_P non-zero means look back (in this case, TO is supposed to be
4588 less than FROM).
4589 Value is the first character position where STRING was found, or
4590 zero if it wasn't found before hitting TO.
4592 W's buffer must be current.
4594 This function may only use code that doesn't eval because it is
4595 called asynchronously from note_mouse_highlight. */
4597 static EMACS_INT
4598 string_buffer_position_lim (struct window *w, Lisp_Object string,
4599 EMACS_INT from, EMACS_INT to, int back_p)
4601 Lisp_Object limit, prop, pos;
4602 int found = 0;
4604 pos = make_number (from);
4606 if (!back_p) /* looking forward */
4608 limit = make_number (min (to, ZV));
4609 while (!found && !EQ (pos, limit))
4611 prop = Fget_char_property (pos, Qdisplay, Qnil);
4612 if (!NILP (prop) && display_prop_string_p (prop, string))
4613 found = 1;
4614 else
4615 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4616 limit);
4619 else /* looking back */
4621 limit = make_number (max (to, BEGV));
4622 while (!found && !EQ (pos, limit))
4624 prop = Fget_char_property (pos, Qdisplay, Qnil);
4625 if (!NILP (prop) && display_prop_string_p (prop, string))
4626 found = 1;
4627 else
4628 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4629 limit);
4633 return found ? XINT (pos) : 0;
4636 /* Determine which buffer position in W's buffer STRING comes from.
4637 AROUND_CHARPOS is an approximate position where it could come from.
4638 Value is the buffer position or 0 if it couldn't be determined.
4640 W's buffer must be current.
4642 This function is necessary because we don't record buffer positions
4643 in glyphs generated from strings (to keep struct glyph small).
4644 This function may only use code that doesn't eval because it is
4645 called asynchronously from note_mouse_highlight. */
4647 EMACS_INT
4648 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4650 const int MAX_DISTANCE = 1000;
4651 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4652 around_charpos + MAX_DISTANCE,
4655 if (!found)
4656 found = string_buffer_position_lim (w, string, around_charpos,
4657 around_charpos - MAX_DISTANCE, 1);
4658 return found;
4663 /***********************************************************************
4664 `composition' property
4665 ***********************************************************************/
4667 /* Set up iterator IT from `composition' property at its current
4668 position. Called from handle_stop. */
4670 static enum prop_handled
4671 handle_composition_prop (struct it *it)
4673 Lisp_Object prop, string;
4674 EMACS_INT pos, pos_byte, start, end;
4676 if (STRINGP (it->string))
4678 unsigned char *s;
4680 pos = IT_STRING_CHARPOS (*it);
4681 pos_byte = IT_STRING_BYTEPOS (*it);
4682 string = it->string;
4683 s = SDATA (string) + pos_byte;
4684 it->c = STRING_CHAR (s);
4686 else
4688 pos = IT_CHARPOS (*it);
4689 pos_byte = IT_BYTEPOS (*it);
4690 string = Qnil;
4691 it->c = FETCH_CHAR (pos_byte);
4694 /* If there's a valid composition and point is not inside of the
4695 composition (in the case that the composition is from the current
4696 buffer), draw a glyph composed from the composition components. */
4697 if (find_composition (pos, -1, &start, &end, &prop, string)
4698 && COMPOSITION_VALID_P (start, end, prop)
4699 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4701 if (start != pos)
4703 if (STRINGP (it->string))
4704 pos_byte = string_char_to_byte (it->string, start);
4705 else
4706 pos_byte = CHAR_TO_BYTE (start);
4708 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4709 prop, string);
4711 if (it->cmp_it.id >= 0)
4713 it->cmp_it.ch = -1;
4714 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4715 it->cmp_it.nglyphs = -1;
4719 return HANDLED_NORMALLY;
4724 /***********************************************************************
4725 Overlay strings
4726 ***********************************************************************/
4728 /* The following structure is used to record overlay strings for
4729 later sorting in load_overlay_strings. */
4731 struct overlay_entry
4733 Lisp_Object overlay;
4734 Lisp_Object string;
4735 int priority;
4736 int after_string_p;
4740 /* Set up iterator IT from overlay strings at its current position.
4741 Called from handle_stop. */
4743 static enum prop_handled
4744 handle_overlay_change (struct it *it)
4746 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4747 return HANDLED_RECOMPUTE_PROPS;
4748 else
4749 return HANDLED_NORMALLY;
4753 /* Set up the next overlay string for delivery by IT, if there is an
4754 overlay string to deliver. Called by set_iterator_to_next when the
4755 end of the current overlay string is reached. If there are more
4756 overlay strings to display, IT->string and
4757 IT->current.overlay_string_index are set appropriately here.
4758 Otherwise IT->string is set to nil. */
4760 static void
4761 next_overlay_string (struct it *it)
4763 ++it->current.overlay_string_index;
4764 if (it->current.overlay_string_index == it->n_overlay_strings)
4766 /* No more overlay strings. Restore IT's settings to what
4767 they were before overlay strings were processed, and
4768 continue to deliver from current_buffer. */
4770 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4771 pop_it (it);
4772 xassert (it->sp > 0
4773 || (NILP (it->string)
4774 && it->method == GET_FROM_BUFFER
4775 && it->stop_charpos >= BEGV
4776 && it->stop_charpos <= it->end_charpos));
4777 it->current.overlay_string_index = -1;
4778 it->n_overlay_strings = 0;
4780 /* If we're at the end of the buffer, record that we have
4781 processed the overlay strings there already, so that
4782 next_element_from_buffer doesn't try it again. */
4783 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4784 it->overlay_strings_at_end_processed_p = 1;
4786 else
4788 /* There are more overlay strings to process. If
4789 IT->current.overlay_string_index has advanced to a position
4790 where we must load IT->overlay_strings with more strings, do
4791 it. */
4792 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4794 if (it->current.overlay_string_index && i == 0)
4795 load_overlay_strings (it, 0);
4797 /* Initialize IT to deliver display elements from the overlay
4798 string. */
4799 it->string = it->overlay_strings[i];
4800 it->multibyte_p = STRING_MULTIBYTE (it->string);
4801 SET_TEXT_POS (it->current.string_pos, 0, 0);
4802 it->method = GET_FROM_STRING;
4803 it->stop_charpos = 0;
4804 if (it->cmp_it.stop_pos >= 0)
4805 it->cmp_it.stop_pos = 0;
4808 CHECK_IT (it);
4812 /* Compare two overlay_entry structures E1 and E2. Used as a
4813 comparison function for qsort in load_overlay_strings. Overlay
4814 strings for the same position are sorted so that
4816 1. All after-strings come in front of before-strings, except
4817 when they come from the same overlay.
4819 2. Within after-strings, strings are sorted so that overlay strings
4820 from overlays with higher priorities come first.
4822 2. Within before-strings, strings are sorted so that overlay
4823 strings from overlays with higher priorities come last.
4825 Value is analogous to strcmp. */
4828 static int
4829 compare_overlay_entries (const void *e1, const void *e2)
4831 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4832 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4833 int result;
4835 if (entry1->after_string_p != entry2->after_string_p)
4837 /* Let after-strings appear in front of before-strings if
4838 they come from different overlays. */
4839 if (EQ (entry1->overlay, entry2->overlay))
4840 result = entry1->after_string_p ? 1 : -1;
4841 else
4842 result = entry1->after_string_p ? -1 : 1;
4844 else if (entry1->after_string_p)
4845 /* After-strings sorted in order of decreasing priority. */
4846 result = entry2->priority - entry1->priority;
4847 else
4848 /* Before-strings sorted in order of increasing priority. */
4849 result = entry1->priority - entry2->priority;
4851 return result;
4855 /* Load the vector IT->overlay_strings with overlay strings from IT's
4856 current buffer position, or from CHARPOS if that is > 0. Set
4857 IT->n_overlays to the total number of overlay strings found.
4859 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4860 a time. On entry into load_overlay_strings,
4861 IT->current.overlay_string_index gives the number of overlay
4862 strings that have already been loaded by previous calls to this
4863 function.
4865 IT->add_overlay_start contains an additional overlay start
4866 position to consider for taking overlay strings from, if non-zero.
4867 This position comes into play when the overlay has an `invisible'
4868 property, and both before and after-strings. When we've skipped to
4869 the end of the overlay, because of its `invisible' property, we
4870 nevertheless want its before-string to appear.
4871 IT->add_overlay_start will contain the overlay start position
4872 in this case.
4874 Overlay strings are sorted so that after-string strings come in
4875 front of before-string strings. Within before and after-strings,
4876 strings are sorted by overlay priority. See also function
4877 compare_overlay_entries. */
4879 static void
4880 load_overlay_strings (struct it *it, EMACS_INT charpos)
4882 Lisp_Object overlay, window, str, invisible;
4883 struct Lisp_Overlay *ov;
4884 EMACS_INT start, end;
4885 int size = 20;
4886 int n = 0, i, j, invis_p;
4887 struct overlay_entry *entries
4888 = (struct overlay_entry *) alloca (size * sizeof *entries);
4890 if (charpos <= 0)
4891 charpos = IT_CHARPOS (*it);
4893 /* Append the overlay string STRING of overlay OVERLAY to vector
4894 `entries' which has size `size' and currently contains `n'
4895 elements. AFTER_P non-zero means STRING is an after-string of
4896 OVERLAY. */
4897 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4898 do \
4900 Lisp_Object priority; \
4902 if (n == size) \
4904 int new_size = 2 * size; \
4905 struct overlay_entry *old = entries; \
4906 entries = \
4907 (struct overlay_entry *) alloca (new_size \
4908 * sizeof *entries); \
4909 memcpy (entries, old, size * sizeof *entries); \
4910 size = new_size; \
4913 entries[n].string = (STRING); \
4914 entries[n].overlay = (OVERLAY); \
4915 priority = Foverlay_get ((OVERLAY), Qpriority); \
4916 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4917 entries[n].after_string_p = (AFTER_P); \
4918 ++n; \
4920 while (0)
4922 /* Process overlay before the overlay center. */
4923 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4925 XSETMISC (overlay, ov);
4926 xassert (OVERLAYP (overlay));
4927 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4928 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4930 if (end < charpos)
4931 break;
4933 /* Skip this overlay if it doesn't start or end at IT's current
4934 position. */
4935 if (end != charpos && start != charpos)
4936 continue;
4938 /* Skip this overlay if it doesn't apply to IT->w. */
4939 window = Foverlay_get (overlay, Qwindow);
4940 if (WINDOWP (window) && XWINDOW (window) != it->w)
4941 continue;
4943 /* If the text ``under'' the overlay is invisible, both before-
4944 and after-strings from this overlay are visible; start and
4945 end position are indistinguishable. */
4946 invisible = Foverlay_get (overlay, Qinvisible);
4947 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4949 /* If overlay has a non-empty before-string, record it. */
4950 if ((start == charpos || (end == charpos && invis_p))
4951 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4952 && SCHARS (str))
4953 RECORD_OVERLAY_STRING (overlay, str, 0);
4955 /* If overlay has a non-empty after-string, record it. */
4956 if ((end == charpos || (start == charpos && invis_p))
4957 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4958 && SCHARS (str))
4959 RECORD_OVERLAY_STRING (overlay, str, 1);
4962 /* Process overlays after the overlay center. */
4963 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4965 XSETMISC (overlay, ov);
4966 xassert (OVERLAYP (overlay));
4967 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4968 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4970 if (start > charpos)
4971 break;
4973 /* Skip this overlay if it doesn't start or end at IT's current
4974 position. */
4975 if (end != charpos && start != charpos)
4976 continue;
4978 /* Skip this overlay if it doesn't apply to IT->w. */
4979 window = Foverlay_get (overlay, Qwindow);
4980 if (WINDOWP (window) && XWINDOW (window) != it->w)
4981 continue;
4983 /* If the text ``under'' the overlay is invisible, it has a zero
4984 dimension, and both before- and after-strings apply. */
4985 invisible = Foverlay_get (overlay, Qinvisible);
4986 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4988 /* If overlay has a non-empty before-string, record it. */
4989 if ((start == charpos || (end == charpos && invis_p))
4990 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4991 && SCHARS (str))
4992 RECORD_OVERLAY_STRING (overlay, str, 0);
4994 /* If overlay has a non-empty after-string, record it. */
4995 if ((end == charpos || (start == charpos && invis_p))
4996 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4997 && SCHARS (str))
4998 RECORD_OVERLAY_STRING (overlay, str, 1);
5001 #undef RECORD_OVERLAY_STRING
5003 /* Sort entries. */
5004 if (n > 1)
5005 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5007 /* Record the total number of strings to process. */
5008 it->n_overlay_strings = n;
5010 /* IT->current.overlay_string_index is the number of overlay strings
5011 that have already been consumed by IT. Copy some of the
5012 remaining overlay strings to IT->overlay_strings. */
5013 i = 0;
5014 j = it->current.overlay_string_index;
5015 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5017 it->overlay_strings[i] = entries[j].string;
5018 it->string_overlays[i++] = entries[j++].overlay;
5021 CHECK_IT (it);
5025 /* Get the first chunk of overlay strings at IT's current buffer
5026 position, or at CHARPOS if that is > 0. Value is non-zero if at
5027 least one overlay string was found. */
5029 static int
5030 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5032 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5033 process. This fills IT->overlay_strings with strings, and sets
5034 IT->n_overlay_strings to the total number of strings to process.
5035 IT->pos.overlay_string_index has to be set temporarily to zero
5036 because load_overlay_strings needs this; it must be set to -1
5037 when no overlay strings are found because a zero value would
5038 indicate a position in the first overlay string. */
5039 it->current.overlay_string_index = 0;
5040 load_overlay_strings (it, charpos);
5042 /* If we found overlay strings, set up IT to deliver display
5043 elements from the first one. Otherwise set up IT to deliver
5044 from current_buffer. */
5045 if (it->n_overlay_strings)
5047 /* Make sure we know settings in current_buffer, so that we can
5048 restore meaningful values when we're done with the overlay
5049 strings. */
5050 if (compute_stop_p)
5051 compute_stop_pos (it);
5052 xassert (it->face_id >= 0);
5054 /* Save IT's settings. They are restored after all overlay
5055 strings have been processed. */
5056 xassert (!compute_stop_p || it->sp == 0);
5058 /* When called from handle_stop, there might be an empty display
5059 string loaded. In that case, don't bother saving it. */
5060 if (!STRINGP (it->string) || SCHARS (it->string))
5061 push_it (it);
5063 /* Set up IT to deliver display elements from the first overlay
5064 string. */
5065 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5066 it->string = it->overlay_strings[0];
5067 it->from_overlay = Qnil;
5068 it->stop_charpos = 0;
5069 xassert (STRINGP (it->string));
5070 it->end_charpos = SCHARS (it->string);
5071 it->multibyte_p = STRING_MULTIBYTE (it->string);
5072 it->method = GET_FROM_STRING;
5073 return 1;
5076 it->current.overlay_string_index = -1;
5077 return 0;
5080 static int
5081 get_overlay_strings (struct it *it, EMACS_INT charpos)
5083 it->string = Qnil;
5084 it->method = GET_FROM_BUFFER;
5086 (void) get_overlay_strings_1 (it, charpos, 1);
5088 CHECK_IT (it);
5090 /* Value is non-zero if we found at least one overlay string. */
5091 return STRINGP (it->string);
5096 /***********************************************************************
5097 Saving and restoring state
5098 ***********************************************************************/
5100 /* Save current settings of IT on IT->stack. Called, for example,
5101 before setting up IT for an overlay string, to be able to restore
5102 IT's settings to what they were after the overlay string has been
5103 processed. */
5105 static void
5106 push_it (struct it *it)
5108 struct iterator_stack_entry *p;
5110 xassert (it->sp < IT_STACK_SIZE);
5111 p = it->stack + it->sp;
5113 p->stop_charpos = it->stop_charpos;
5114 p->prev_stop = it->prev_stop;
5115 p->base_level_stop = it->base_level_stop;
5116 p->cmp_it = it->cmp_it;
5117 xassert (it->face_id >= 0);
5118 p->face_id = it->face_id;
5119 p->string = it->string;
5120 p->method = it->method;
5121 p->from_overlay = it->from_overlay;
5122 switch (p->method)
5124 case GET_FROM_IMAGE:
5125 p->u.image.object = it->object;
5126 p->u.image.image_id = it->image_id;
5127 p->u.image.slice = it->slice;
5128 break;
5129 case GET_FROM_STRETCH:
5130 p->u.stretch.object = it->object;
5131 break;
5133 p->position = it->position;
5134 p->current = it->current;
5135 p->end_charpos = it->end_charpos;
5136 p->string_nchars = it->string_nchars;
5137 p->area = it->area;
5138 p->multibyte_p = it->multibyte_p;
5139 p->avoid_cursor_p = it->avoid_cursor_p;
5140 p->space_width = it->space_width;
5141 p->font_height = it->font_height;
5142 p->voffset = it->voffset;
5143 p->string_from_display_prop_p = it->string_from_display_prop_p;
5144 p->display_ellipsis_p = 0;
5145 p->line_wrap = it->line_wrap;
5146 ++it->sp;
5149 static void
5150 iterate_out_of_display_property (struct it *it)
5152 /* Maybe initialize paragraph direction. If we are at the beginning
5153 of a new paragraph, next_element_from_buffer may not have a
5154 chance to do that. */
5155 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5156 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5157 /* prev_stop can be zero, so check against BEGV as well. */
5158 while (it->bidi_it.charpos >= BEGV
5159 && it->prev_stop <= it->bidi_it.charpos
5160 && it->bidi_it.charpos < CHARPOS (it->position))
5161 bidi_move_to_visually_next (&it->bidi_it);
5162 /* Record the stop_pos we just crossed, for when we cross it
5163 back, maybe. */
5164 if (it->bidi_it.charpos > CHARPOS (it->position))
5165 it->prev_stop = CHARPOS (it->position);
5166 /* If we ended up not where pop_it put us, resync IT's
5167 positional members with the bidi iterator. */
5168 if (it->bidi_it.charpos != CHARPOS (it->position))
5170 SET_TEXT_POS (it->position,
5171 it->bidi_it.charpos, it->bidi_it.bytepos);
5172 it->current.pos = it->position;
5176 /* Restore IT's settings from IT->stack. Called, for example, when no
5177 more overlay strings must be processed, and we return to delivering
5178 display elements from a buffer, or when the end of a string from a
5179 `display' property is reached and we return to delivering display
5180 elements from an overlay string, or from a buffer. */
5182 static void
5183 pop_it (struct it *it)
5185 struct iterator_stack_entry *p;
5187 xassert (it->sp > 0);
5188 --it->sp;
5189 p = it->stack + it->sp;
5190 it->stop_charpos = p->stop_charpos;
5191 it->prev_stop = p->prev_stop;
5192 it->base_level_stop = p->base_level_stop;
5193 it->cmp_it = p->cmp_it;
5194 it->face_id = p->face_id;
5195 it->current = p->current;
5196 it->position = p->position;
5197 it->string = p->string;
5198 it->from_overlay = p->from_overlay;
5199 if (NILP (it->string))
5200 SET_TEXT_POS (it->current.string_pos, -1, -1);
5201 it->method = p->method;
5202 switch (it->method)
5204 case GET_FROM_IMAGE:
5205 it->image_id = p->u.image.image_id;
5206 it->object = p->u.image.object;
5207 it->slice = p->u.image.slice;
5208 break;
5209 case GET_FROM_STRETCH:
5210 it->object = p->u.comp.object;
5211 break;
5212 case GET_FROM_BUFFER:
5213 it->object = it->w->buffer;
5214 if (it->bidi_p)
5216 /* Bidi-iterate until we get out of the portion of text, if
5217 any, covered by a `display' text property or an overlay
5218 with `display' property. (We cannot just jump there,
5219 because the internal coherency of the bidi iterator state
5220 can not be preserved across such jumps.) We also must
5221 determine the paragraph base direction if the overlay we
5222 just processed is at the beginning of a new
5223 paragraph. */
5224 iterate_out_of_display_property (it);
5226 break;
5227 case GET_FROM_STRING:
5228 it->object = it->string;
5229 break;
5230 case GET_FROM_DISPLAY_VECTOR:
5231 if (it->s)
5232 it->method = GET_FROM_C_STRING;
5233 else if (STRINGP (it->string))
5234 it->method = GET_FROM_STRING;
5235 else
5237 it->method = GET_FROM_BUFFER;
5238 it->object = it->w->buffer;
5241 it->end_charpos = p->end_charpos;
5242 it->string_nchars = p->string_nchars;
5243 it->area = p->area;
5244 it->multibyte_p = p->multibyte_p;
5245 it->avoid_cursor_p = p->avoid_cursor_p;
5246 it->space_width = p->space_width;
5247 it->font_height = p->font_height;
5248 it->voffset = p->voffset;
5249 it->string_from_display_prop_p = p->string_from_display_prop_p;
5250 it->line_wrap = p->line_wrap;
5255 /***********************************************************************
5256 Moving over lines
5257 ***********************************************************************/
5259 /* Set IT's current position to the previous line start. */
5261 static void
5262 back_to_previous_line_start (struct it *it)
5264 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5265 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5269 /* Move IT to the next line start.
5271 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5272 we skipped over part of the text (as opposed to moving the iterator
5273 continuously over the text). Otherwise, don't change the value
5274 of *SKIPPED_P.
5276 Newlines may come from buffer text, overlay strings, or strings
5277 displayed via the `display' property. That's the reason we can't
5278 simply use find_next_newline_no_quit.
5280 Note that this function may not skip over invisible text that is so
5281 because of text properties and immediately follows a newline. If
5282 it would, function reseat_at_next_visible_line_start, when called
5283 from set_iterator_to_next, would effectively make invisible
5284 characters following a newline part of the wrong glyph row, which
5285 leads to wrong cursor motion. */
5287 static int
5288 forward_to_next_line_start (struct it *it, int *skipped_p)
5290 int old_selective, newline_found_p, n;
5291 const int MAX_NEWLINE_DISTANCE = 500;
5293 /* If already on a newline, just consume it to avoid unintended
5294 skipping over invisible text below. */
5295 if (it->what == IT_CHARACTER
5296 && it->c == '\n'
5297 && CHARPOS (it->position) == IT_CHARPOS (*it))
5299 set_iterator_to_next (it, 0);
5300 it->c = 0;
5301 return 1;
5304 /* Don't handle selective display in the following. It's (a)
5305 unnecessary because it's done by the caller, and (b) leads to an
5306 infinite recursion because next_element_from_ellipsis indirectly
5307 calls this function. */
5308 old_selective = it->selective;
5309 it->selective = 0;
5311 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5312 from buffer text. */
5313 for (n = newline_found_p = 0;
5314 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5315 n += STRINGP (it->string) ? 0 : 1)
5317 if (!get_next_display_element (it))
5318 return 0;
5319 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5320 set_iterator_to_next (it, 0);
5323 /* If we didn't find a newline near enough, see if we can use a
5324 short-cut. */
5325 if (!newline_found_p)
5327 EMACS_INT start = IT_CHARPOS (*it);
5328 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5329 Lisp_Object pos;
5331 xassert (!STRINGP (it->string));
5333 /* If there isn't any `display' property in sight, and no
5334 overlays, we can just use the position of the newline in
5335 buffer text. */
5336 if (it->stop_charpos >= limit
5337 || ((pos = Fnext_single_property_change (make_number (start),
5338 Qdisplay,
5339 Qnil, make_number (limit)),
5340 NILP (pos))
5341 && next_overlay_change (start) == ZV))
5343 IT_CHARPOS (*it) = limit;
5344 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5345 *skipped_p = newline_found_p = 1;
5347 else
5349 while (get_next_display_element (it)
5350 && !newline_found_p)
5352 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5353 set_iterator_to_next (it, 0);
5358 it->selective = old_selective;
5359 return newline_found_p;
5363 /* Set IT's current position to the previous visible line start. Skip
5364 invisible text that is so either due to text properties or due to
5365 selective display. Caution: this does not change IT->current_x and
5366 IT->hpos. */
5368 static void
5369 back_to_previous_visible_line_start (struct it *it)
5371 while (IT_CHARPOS (*it) > BEGV)
5373 back_to_previous_line_start (it);
5375 if (IT_CHARPOS (*it) <= BEGV)
5376 break;
5378 /* If selective > 0, then lines indented more than its value are
5379 invisible. */
5380 if (it->selective > 0
5381 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5382 (double) it->selective)) /* iftc */
5383 continue;
5385 /* Check the newline before point for invisibility. */
5387 Lisp_Object prop;
5388 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5389 Qinvisible, it->window);
5390 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5391 continue;
5394 if (IT_CHARPOS (*it) <= BEGV)
5395 break;
5398 struct it it2;
5399 EMACS_INT pos;
5400 EMACS_INT beg, end;
5401 Lisp_Object val, overlay;
5403 /* If newline is part of a composition, continue from start of composition */
5404 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5405 && beg < IT_CHARPOS (*it))
5406 goto replaced;
5408 /* If newline is replaced by a display property, find start of overlay
5409 or interval and continue search from that point. */
5410 it2 = *it;
5411 pos = --IT_CHARPOS (it2);
5412 --IT_BYTEPOS (it2);
5413 it2.sp = 0;
5414 it2.string_from_display_prop_p = 0;
5415 if (handle_display_prop (&it2) == HANDLED_RETURN
5416 && !NILP (val = get_char_property_and_overlay
5417 (make_number (pos), Qdisplay, Qnil, &overlay))
5418 && (OVERLAYP (overlay)
5419 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5420 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5421 goto replaced;
5423 /* Newline is not replaced by anything -- so we are done. */
5424 break;
5426 replaced:
5427 if (beg < BEGV)
5428 beg = BEGV;
5429 IT_CHARPOS (*it) = beg;
5430 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5434 it->continuation_lines_width = 0;
5436 xassert (IT_CHARPOS (*it) >= BEGV);
5437 xassert (IT_CHARPOS (*it) == BEGV
5438 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5439 CHECK_IT (it);
5443 /* Reseat iterator IT at the previous visible line start. Skip
5444 invisible text that is so either due to text properties or due to
5445 selective display. At the end, update IT's overlay information,
5446 face information etc. */
5448 void
5449 reseat_at_previous_visible_line_start (struct it *it)
5451 back_to_previous_visible_line_start (it);
5452 reseat (it, it->current.pos, 1);
5453 CHECK_IT (it);
5457 /* Reseat iterator IT on the next visible line start in the current
5458 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5459 preceding the line start. Skip over invisible text that is so
5460 because of selective display. Compute faces, overlays etc at the
5461 new position. Note that this function does not skip over text that
5462 is invisible because of text properties. */
5464 static void
5465 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5467 int newline_found_p, skipped_p = 0;
5469 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5471 /* Skip over lines that are invisible because they are indented
5472 more than the value of IT->selective. */
5473 if (it->selective > 0)
5474 while (IT_CHARPOS (*it) < ZV
5475 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5476 (double) it->selective)) /* iftc */
5478 xassert (IT_BYTEPOS (*it) == BEGV
5479 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5480 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5483 /* Position on the newline if that's what's requested. */
5484 if (on_newline_p && newline_found_p)
5486 if (STRINGP (it->string))
5488 if (IT_STRING_CHARPOS (*it) > 0)
5490 --IT_STRING_CHARPOS (*it);
5491 --IT_STRING_BYTEPOS (*it);
5494 else if (IT_CHARPOS (*it) > BEGV)
5496 --IT_CHARPOS (*it);
5497 --IT_BYTEPOS (*it);
5498 reseat (it, it->current.pos, 0);
5501 else if (skipped_p)
5502 reseat (it, it->current.pos, 0);
5504 CHECK_IT (it);
5509 /***********************************************************************
5510 Changing an iterator's position
5511 ***********************************************************************/
5513 /* Change IT's current position to POS in current_buffer. If FORCE_P
5514 is non-zero, always check for text properties at the new position.
5515 Otherwise, text properties are only looked up if POS >=
5516 IT->check_charpos of a property. */
5518 static void
5519 reseat (struct it *it, struct text_pos pos, int force_p)
5521 EMACS_INT original_pos = IT_CHARPOS (*it);
5523 reseat_1 (it, pos, 0);
5525 /* Determine where to check text properties. Avoid doing it
5526 where possible because text property lookup is very expensive. */
5527 if (force_p
5528 || CHARPOS (pos) > it->stop_charpos
5529 || CHARPOS (pos) < original_pos)
5531 if (it->bidi_p)
5533 /* For bidi iteration, we need to prime prev_stop and
5534 base_level_stop with our best estimations. */
5535 if (CHARPOS (pos) < it->prev_stop)
5537 handle_stop_backwards (it, BEGV);
5538 if (CHARPOS (pos) < it->base_level_stop)
5539 it->base_level_stop = 0;
5541 else if (CHARPOS (pos) > it->stop_charpos
5542 && it->stop_charpos >= BEGV)
5543 handle_stop_backwards (it, it->stop_charpos);
5544 else /* force_p */
5545 handle_stop (it);
5547 else
5549 handle_stop (it);
5550 it->prev_stop = it->base_level_stop = 0;
5555 CHECK_IT (it);
5559 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5560 IT->stop_pos to POS, also. */
5562 static void
5563 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5565 /* Don't call this function when scanning a C string. */
5566 xassert (it->s == NULL);
5568 /* POS must be a reasonable value. */
5569 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5571 it->current.pos = it->position = pos;
5572 it->end_charpos = ZV;
5573 it->dpvec = NULL;
5574 it->current.dpvec_index = -1;
5575 it->current.overlay_string_index = -1;
5576 IT_STRING_CHARPOS (*it) = -1;
5577 IT_STRING_BYTEPOS (*it) = -1;
5578 it->string = Qnil;
5579 it->string_from_display_prop_p = 0;
5580 it->method = GET_FROM_BUFFER;
5581 it->object = it->w->buffer;
5582 it->area = TEXT_AREA;
5583 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5584 it->sp = 0;
5585 it->string_from_display_prop_p = 0;
5586 it->face_before_selective_p = 0;
5587 if (it->bidi_p)
5589 it->bidi_it.first_elt = 1;
5590 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5593 if (set_stop_p)
5595 it->stop_charpos = CHARPOS (pos);
5596 it->base_level_stop = CHARPOS (pos);
5601 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5602 If S is non-null, it is a C string to iterate over. Otherwise,
5603 STRING gives a Lisp string to iterate over.
5605 If PRECISION > 0, don't return more then PRECISION number of
5606 characters from the string.
5608 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5609 characters have been returned. FIELD_WIDTH < 0 means an infinite
5610 field width.
5612 MULTIBYTE = 0 means disable processing of multibyte characters,
5613 MULTIBYTE > 0 means enable it,
5614 MULTIBYTE < 0 means use IT->multibyte_p.
5616 IT must be initialized via a prior call to init_iterator before
5617 calling this function. */
5619 static void
5620 reseat_to_string (struct it *it, const unsigned char *s, Lisp_Object string,
5621 EMACS_INT charpos, EMACS_INT precision, int field_width,
5622 int multibyte)
5624 /* No region in strings. */
5625 it->region_beg_charpos = it->region_end_charpos = -1;
5627 /* No text property checks performed by default, but see below. */
5628 it->stop_charpos = -1;
5630 /* Set iterator position and end position. */
5631 memset (&it->current, 0, sizeof it->current);
5632 it->current.overlay_string_index = -1;
5633 it->current.dpvec_index = -1;
5634 xassert (charpos >= 0);
5636 /* If STRING is specified, use its multibyteness, otherwise use the
5637 setting of MULTIBYTE, if specified. */
5638 if (multibyte >= 0)
5639 it->multibyte_p = multibyte > 0;
5641 if (s == NULL)
5643 xassert (STRINGP (string));
5644 it->string = string;
5645 it->s = NULL;
5646 it->end_charpos = it->string_nchars = SCHARS (string);
5647 it->method = GET_FROM_STRING;
5648 it->current.string_pos = string_pos (charpos, string);
5650 else
5652 it->s = s;
5653 it->string = Qnil;
5655 /* Note that we use IT->current.pos, not it->current.string_pos,
5656 for displaying C strings. */
5657 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5658 if (it->multibyte_p)
5660 it->current.pos = c_string_pos (charpos, s, 1);
5661 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5663 else
5665 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5666 it->end_charpos = it->string_nchars = strlen (s);
5669 it->method = GET_FROM_C_STRING;
5672 /* PRECISION > 0 means don't return more than PRECISION characters
5673 from the string. */
5674 if (precision > 0 && it->end_charpos - charpos > precision)
5675 it->end_charpos = it->string_nchars = charpos + precision;
5677 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5678 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5679 FIELD_WIDTH < 0 means infinite field width. This is useful for
5680 padding with `-' at the end of a mode line. */
5681 if (field_width < 0)
5682 field_width = INFINITY;
5683 if (field_width > it->end_charpos - charpos)
5684 it->end_charpos = charpos + field_width;
5686 /* Use the standard display table for displaying strings. */
5687 if (DISP_TABLE_P (Vstandard_display_table))
5688 it->dp = XCHAR_TABLE (Vstandard_display_table);
5690 it->stop_charpos = charpos;
5691 if (s == NULL && it->multibyte_p)
5693 EMACS_INT endpos = SCHARS (it->string);
5694 if (endpos > it->end_charpos)
5695 endpos = it->end_charpos;
5696 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5697 it->string);
5699 CHECK_IT (it);
5704 /***********************************************************************
5705 Iteration
5706 ***********************************************************************/
5708 /* Map enum it_method value to corresponding next_element_from_* function. */
5710 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5712 next_element_from_buffer,
5713 next_element_from_display_vector,
5714 next_element_from_string,
5715 next_element_from_c_string,
5716 next_element_from_image,
5717 next_element_from_stretch
5720 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5723 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5724 (possibly with the following characters). */
5726 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5727 ((IT)->cmp_it.id >= 0 \
5728 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5729 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5730 END_CHARPOS, (IT)->w, \
5731 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5732 (IT)->string)))
5735 /* Load IT's display element fields with information about the next
5736 display element from the current position of IT. Value is zero if
5737 end of buffer (or C string) is reached. */
5739 static struct frame *last_escape_glyph_frame = NULL;
5740 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5741 static int last_escape_glyph_merged_face_id = 0;
5744 get_next_display_element (struct it *it)
5746 /* Non-zero means that we found a display element. Zero means that
5747 we hit the end of what we iterate over. Performance note: the
5748 function pointer `method' used here turns out to be faster than
5749 using a sequence of if-statements. */
5750 int success_p;
5752 get_next:
5753 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5755 if (it->what == IT_CHARACTER)
5757 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5758 and only if (a) the resolved directionality of that character
5759 is R..." */
5760 /* FIXME: Do we need an exception for characters from display
5761 tables? */
5762 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5763 it->c = bidi_mirror_char (it->c);
5764 /* Map via display table or translate control characters.
5765 IT->c, IT->len etc. have been set to the next character by
5766 the function call above. If we have a display table, and it
5767 contains an entry for IT->c, translate it. Don't do this if
5768 IT->c itself comes from a display table, otherwise we could
5769 end up in an infinite recursion. (An alternative could be to
5770 count the recursion depth of this function and signal an
5771 error when a certain maximum depth is reached.) Is it worth
5772 it? */
5773 if (success_p && it->dpvec == NULL)
5775 Lisp_Object dv;
5776 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5777 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5778 nbsp_or_shy = char_is_other;
5779 int c = it->c; /* This is the character to display. */
5781 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5783 xassert (SINGLE_BYTE_CHAR_P (c));
5784 if (unibyte_display_via_language_environment)
5786 c = DECODE_CHAR (unibyte, c);
5787 if (c < 0)
5788 c = BYTE8_TO_CHAR (it->c);
5790 else
5791 c = BYTE8_TO_CHAR (it->c);
5794 if (it->dp
5795 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5796 VECTORP (dv)))
5798 struct Lisp_Vector *v = XVECTOR (dv);
5800 /* Return the first character from the display table
5801 entry, if not empty. If empty, don't display the
5802 current character. */
5803 if (v->size)
5805 it->dpvec_char_len = it->len;
5806 it->dpvec = v->contents;
5807 it->dpend = v->contents + v->size;
5808 it->current.dpvec_index = 0;
5809 it->dpvec_face_id = -1;
5810 it->saved_face_id = it->face_id;
5811 it->method = GET_FROM_DISPLAY_VECTOR;
5812 it->ellipsis_p = 0;
5814 else
5816 set_iterator_to_next (it, 0);
5818 goto get_next;
5821 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5822 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5823 : c == 0xAD ? char_is_soft_hyphen
5824 : char_is_other);
5826 /* Translate control characters into `\003' or `^C' form.
5827 Control characters coming from a display table entry are
5828 currently not translated because we use IT->dpvec to hold
5829 the translation. This could easily be changed but I
5830 don't believe that it is worth doing.
5832 NBSP and SOFT-HYPEN are property translated too.
5834 Non-printable characters and raw-byte characters are also
5835 translated to octal form. */
5836 if (((c < ' ' || c == 127) /* ASCII control chars */
5837 ? (it->area != TEXT_AREA
5838 /* In mode line, treat \n, \t like other crl chars. */
5839 || (c != '\t'
5840 && it->glyph_row
5841 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5842 || (c != '\n' && c != '\t'))
5843 : (nbsp_or_shy
5844 || CHAR_BYTE8_P (c)
5845 || ! CHAR_PRINTABLE_P (c))))
5847 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5848 or a non-printable character which must be displayed
5849 either as '\003' or as `^C' where the '\\' and '^'
5850 can be defined in the display table. Fill
5851 IT->ctl_chars with glyphs for what we have to
5852 display. Then, set IT->dpvec to these glyphs. */
5853 Lisp_Object gc;
5854 int ctl_len;
5855 int face_id, lface_id = 0 ;
5856 int escape_glyph;
5858 /* Handle control characters with ^. */
5860 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5862 int g;
5864 g = '^'; /* default glyph for Control */
5865 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5866 if (it->dp
5867 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5868 && GLYPH_CODE_CHAR_VALID_P (gc))
5870 g = GLYPH_CODE_CHAR (gc);
5871 lface_id = GLYPH_CODE_FACE (gc);
5873 if (lface_id)
5875 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5877 else if (it->f == last_escape_glyph_frame
5878 && it->face_id == last_escape_glyph_face_id)
5880 face_id = last_escape_glyph_merged_face_id;
5882 else
5884 /* Merge the escape-glyph face into the current face. */
5885 face_id = merge_faces (it->f, Qescape_glyph, 0,
5886 it->face_id);
5887 last_escape_glyph_frame = it->f;
5888 last_escape_glyph_face_id = it->face_id;
5889 last_escape_glyph_merged_face_id = face_id;
5892 XSETINT (it->ctl_chars[0], g);
5893 XSETINT (it->ctl_chars[1], c ^ 0100);
5894 ctl_len = 2;
5895 goto display_control;
5898 /* Handle non-break space in the mode where it only gets
5899 highlighting. */
5901 if (EQ (Vnobreak_char_display, Qt)
5902 && nbsp_or_shy == char_is_nbsp)
5904 /* Merge the no-break-space face into the current face. */
5905 face_id = merge_faces (it->f, Qnobreak_space, 0,
5906 it->face_id);
5908 c = ' ';
5909 XSETINT (it->ctl_chars[0], ' ');
5910 ctl_len = 1;
5911 goto display_control;
5914 /* Handle sequences that start with the "escape glyph". */
5916 /* the default escape glyph is \. */
5917 escape_glyph = '\\';
5919 if (it->dp
5920 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5921 && GLYPH_CODE_CHAR_VALID_P (gc))
5923 escape_glyph = GLYPH_CODE_CHAR (gc);
5924 lface_id = GLYPH_CODE_FACE (gc);
5926 if (lface_id)
5928 /* The display table specified a face.
5929 Merge it into face_id and also into escape_glyph. */
5930 face_id = merge_faces (it->f, Qt, lface_id,
5931 it->face_id);
5933 else if (it->f == last_escape_glyph_frame
5934 && it->face_id == last_escape_glyph_face_id)
5936 face_id = last_escape_glyph_merged_face_id;
5938 else
5940 /* Merge the escape-glyph face into the current face. */
5941 face_id = merge_faces (it->f, Qescape_glyph, 0,
5942 it->face_id);
5943 last_escape_glyph_frame = it->f;
5944 last_escape_glyph_face_id = it->face_id;
5945 last_escape_glyph_merged_face_id = face_id;
5948 /* Handle soft hyphens in the mode where they only get
5949 highlighting. */
5951 if (EQ (Vnobreak_char_display, Qt)
5952 && nbsp_or_shy == char_is_soft_hyphen)
5954 XSETINT (it->ctl_chars[0], '-');
5955 ctl_len = 1;
5956 goto display_control;
5959 /* Handle non-break space and soft hyphen
5960 with the escape glyph. */
5962 if (nbsp_or_shy)
5964 XSETINT (it->ctl_chars[0], escape_glyph);
5965 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5966 XSETINT (it->ctl_chars[1], c);
5967 ctl_len = 2;
5968 goto display_control;
5972 char str[10];
5973 int len, i;
5975 if (CHAR_BYTE8_P (c))
5976 /* Display \200 instead of \17777600. */
5977 c = CHAR_TO_BYTE8 (c);
5978 len = sprintf (str, "%03o", c);
5980 XSETINT (it->ctl_chars[0], escape_glyph);
5981 for (i = 0; i < len; i++)
5982 XSETINT (it->ctl_chars[i + 1], str[i]);
5983 ctl_len = len + 1;
5986 display_control:
5987 /* Set up IT->dpvec and return first character from it. */
5988 it->dpvec_char_len = it->len;
5989 it->dpvec = it->ctl_chars;
5990 it->dpend = it->dpvec + ctl_len;
5991 it->current.dpvec_index = 0;
5992 it->dpvec_face_id = face_id;
5993 it->saved_face_id = it->face_id;
5994 it->method = GET_FROM_DISPLAY_VECTOR;
5995 it->ellipsis_p = 0;
5996 goto get_next;
5998 it->char_to_display = c;
6000 else if (success_p)
6002 it->char_to_display = it->c;
6006 #ifdef HAVE_WINDOW_SYSTEM
6007 /* Adjust face id for a multibyte character. There are no multibyte
6008 character in unibyte text. */
6009 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6010 && it->multibyte_p
6011 && success_p
6012 && FRAME_WINDOW_P (it->f))
6014 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6016 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6018 /* Automatic composition with glyph-string. */
6019 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6021 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6023 else
6025 EMACS_INT pos = (it->s ? -1
6026 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6027 : IT_CHARPOS (*it));
6029 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6030 it->string);
6033 #endif
6035 /* Is this character the last one of a run of characters with
6036 box? If yes, set IT->end_of_box_run_p to 1. */
6037 if (it->face_box_p
6038 && it->s == NULL)
6040 if (it->method == GET_FROM_STRING && it->sp)
6042 int face_id = underlying_face_id (it);
6043 struct face *face = FACE_FROM_ID (it->f, face_id);
6045 if (face)
6047 if (face->box == FACE_NO_BOX)
6049 /* If the box comes from face properties in a
6050 display string, check faces in that string. */
6051 int string_face_id = face_after_it_pos (it);
6052 it->end_of_box_run_p
6053 = (FACE_FROM_ID (it->f, string_face_id)->box
6054 == FACE_NO_BOX);
6056 /* Otherwise, the box comes from the underlying face.
6057 If this is the last string character displayed, check
6058 the next buffer location. */
6059 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6060 && (it->current.overlay_string_index
6061 == it->n_overlay_strings - 1))
6063 EMACS_INT ignore;
6064 int next_face_id;
6065 struct text_pos pos = it->current.pos;
6066 INC_TEXT_POS (pos, it->multibyte_p);
6068 next_face_id = face_at_buffer_position
6069 (it->w, CHARPOS (pos), it->region_beg_charpos,
6070 it->region_end_charpos, &ignore,
6071 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6072 -1);
6073 it->end_of_box_run_p
6074 = (FACE_FROM_ID (it->f, next_face_id)->box
6075 == FACE_NO_BOX);
6079 else
6081 int face_id = face_after_it_pos (it);
6082 it->end_of_box_run_p
6083 = (face_id != it->face_id
6084 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6088 /* Value is 0 if end of buffer or string reached. */
6089 return success_p;
6093 /* Move IT to the next display element.
6095 RESEAT_P non-zero means if called on a newline in buffer text,
6096 skip to the next visible line start.
6098 Functions get_next_display_element and set_iterator_to_next are
6099 separate because I find this arrangement easier to handle than a
6100 get_next_display_element function that also increments IT's
6101 position. The way it is we can first look at an iterator's current
6102 display element, decide whether it fits on a line, and if it does,
6103 increment the iterator position. The other way around we probably
6104 would either need a flag indicating whether the iterator has to be
6105 incremented the next time, or we would have to implement a
6106 decrement position function which would not be easy to write. */
6108 void
6109 set_iterator_to_next (struct it *it, int reseat_p)
6111 /* Reset flags indicating start and end of a sequence of characters
6112 with box. Reset them at the start of this function because
6113 moving the iterator to a new position might set them. */
6114 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6116 switch (it->method)
6118 case GET_FROM_BUFFER:
6119 /* The current display element of IT is a character from
6120 current_buffer. Advance in the buffer, and maybe skip over
6121 invisible lines that are so because of selective display. */
6122 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6123 reseat_at_next_visible_line_start (it, 0);
6124 else if (it->cmp_it.id >= 0)
6126 /* We are currently getting glyphs from a composition. */
6127 int i;
6129 if (! it->bidi_p)
6131 IT_CHARPOS (*it) += it->cmp_it.nchars;
6132 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6133 if (it->cmp_it.to < it->cmp_it.nglyphs)
6135 it->cmp_it.from = it->cmp_it.to;
6137 else
6139 it->cmp_it.id = -1;
6140 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6141 IT_BYTEPOS (*it),
6142 it->end_charpos, Qnil);
6145 else if (! it->cmp_it.reversed_p)
6147 /* Composition created while scanning forward. */
6148 /* Update IT's char/byte positions to point to the first
6149 character of the next grapheme cluster, or to the
6150 character visually after the current composition. */
6151 for (i = 0; i < it->cmp_it.nchars; i++)
6152 bidi_move_to_visually_next (&it->bidi_it);
6153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6154 IT_CHARPOS (*it) = it->bidi_it.charpos;
6156 if (it->cmp_it.to < it->cmp_it.nglyphs)
6158 /* Proceed to the next grapheme cluster. */
6159 it->cmp_it.from = it->cmp_it.to;
6161 else
6163 /* No more grapheme clusters in this composition.
6164 Find the next stop position. */
6165 EMACS_INT stop = it->end_charpos;
6166 if (it->bidi_it.scan_dir < 0)
6167 /* Now we are scanning backward and don't know
6168 where to stop. */
6169 stop = -1;
6170 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6171 IT_BYTEPOS (*it), stop, Qnil);
6174 else
6176 /* Composition created while scanning backward. */
6177 /* Update IT's char/byte positions to point to the last
6178 character of the previous grapheme cluster, or the
6179 character visually after the current composition. */
6180 for (i = 0; i < it->cmp_it.nchars; i++)
6181 bidi_move_to_visually_next (&it->bidi_it);
6182 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6183 IT_CHARPOS (*it) = it->bidi_it.charpos;
6184 if (it->cmp_it.from > 0)
6186 /* Proceed to the previous grapheme cluster. */
6187 it->cmp_it.to = it->cmp_it.from;
6189 else
6191 /* No more grapheme clusters in this composition.
6192 Find the next stop position. */
6193 EMACS_INT stop = it->end_charpos;
6194 if (it->bidi_it.scan_dir < 0)
6195 /* Now we are scanning backward and don't know
6196 where to stop. */
6197 stop = -1;
6198 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6199 IT_BYTEPOS (*it), stop, Qnil);
6203 else
6205 xassert (it->len != 0);
6207 if (!it->bidi_p)
6209 IT_BYTEPOS (*it) += it->len;
6210 IT_CHARPOS (*it) += 1;
6212 else
6214 int prev_scan_dir = it->bidi_it.scan_dir;
6215 /* If this is a new paragraph, determine its base
6216 direction (a.k.a. its base embedding level). */
6217 if (it->bidi_it.new_paragraph)
6218 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6219 bidi_move_to_visually_next (&it->bidi_it);
6220 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6221 IT_CHARPOS (*it) = it->bidi_it.charpos;
6222 if (prev_scan_dir != it->bidi_it.scan_dir)
6224 /* As the scan direction was changed, we must
6225 re-compute the stop position for composition. */
6226 EMACS_INT stop = it->end_charpos;
6227 if (it->bidi_it.scan_dir < 0)
6228 stop = -1;
6229 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6230 IT_BYTEPOS (*it), stop, Qnil);
6233 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6235 break;
6237 case GET_FROM_C_STRING:
6238 /* Current display element of IT is from a C string. */
6239 IT_BYTEPOS (*it) += it->len;
6240 IT_CHARPOS (*it) += 1;
6241 break;
6243 case GET_FROM_DISPLAY_VECTOR:
6244 /* Current display element of IT is from a display table entry.
6245 Advance in the display table definition. Reset it to null if
6246 end reached, and continue with characters from buffers/
6247 strings. */
6248 ++it->current.dpvec_index;
6250 /* Restore face of the iterator to what they were before the
6251 display vector entry (these entries may contain faces). */
6252 it->face_id = it->saved_face_id;
6254 if (it->dpvec + it->current.dpvec_index == it->dpend)
6256 int recheck_faces = it->ellipsis_p;
6258 if (it->s)
6259 it->method = GET_FROM_C_STRING;
6260 else if (STRINGP (it->string))
6261 it->method = GET_FROM_STRING;
6262 else
6264 it->method = GET_FROM_BUFFER;
6265 it->object = it->w->buffer;
6268 it->dpvec = NULL;
6269 it->current.dpvec_index = -1;
6271 /* Skip over characters which were displayed via IT->dpvec. */
6272 if (it->dpvec_char_len < 0)
6273 reseat_at_next_visible_line_start (it, 1);
6274 else if (it->dpvec_char_len > 0)
6276 if (it->method == GET_FROM_STRING
6277 && it->n_overlay_strings > 0)
6278 it->ignore_overlay_strings_at_pos_p = 1;
6279 it->len = it->dpvec_char_len;
6280 set_iterator_to_next (it, reseat_p);
6283 /* Maybe recheck faces after display vector */
6284 if (recheck_faces)
6285 it->stop_charpos = IT_CHARPOS (*it);
6287 break;
6289 case GET_FROM_STRING:
6290 /* Current display element is a character from a Lisp string. */
6291 xassert (it->s == NULL && STRINGP (it->string));
6292 if (it->cmp_it.id >= 0)
6294 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6295 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6296 if (it->cmp_it.to < it->cmp_it.nglyphs)
6297 it->cmp_it.from = it->cmp_it.to;
6298 else
6300 it->cmp_it.id = -1;
6301 composition_compute_stop_pos (&it->cmp_it,
6302 IT_STRING_CHARPOS (*it),
6303 IT_STRING_BYTEPOS (*it),
6304 it->end_charpos, it->string);
6307 else
6309 IT_STRING_BYTEPOS (*it) += it->len;
6310 IT_STRING_CHARPOS (*it) += 1;
6313 consider_string_end:
6315 if (it->current.overlay_string_index >= 0)
6317 /* IT->string is an overlay string. Advance to the
6318 next, if there is one. */
6319 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6321 it->ellipsis_p = 0;
6322 next_overlay_string (it);
6323 if (it->ellipsis_p)
6324 setup_for_ellipsis (it, 0);
6327 else
6329 /* IT->string is not an overlay string. If we reached
6330 its end, and there is something on IT->stack, proceed
6331 with what is on the stack. This can be either another
6332 string, this time an overlay string, or a buffer. */
6333 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6334 && it->sp > 0)
6336 pop_it (it);
6337 if (it->method == GET_FROM_STRING)
6338 goto consider_string_end;
6341 break;
6343 case GET_FROM_IMAGE:
6344 case GET_FROM_STRETCH:
6345 /* The position etc with which we have to proceed are on
6346 the stack. The position may be at the end of a string,
6347 if the `display' property takes up the whole string. */
6348 xassert (it->sp > 0);
6349 pop_it (it);
6350 if (it->method == GET_FROM_STRING)
6351 goto consider_string_end;
6352 break;
6354 default:
6355 /* There are no other methods defined, so this should be a bug. */
6356 abort ();
6359 xassert (it->method != GET_FROM_STRING
6360 || (STRINGP (it->string)
6361 && IT_STRING_CHARPOS (*it) >= 0));
6364 /* Load IT's display element fields with information about the next
6365 display element which comes from a display table entry or from the
6366 result of translating a control character to one of the forms `^C'
6367 or `\003'.
6369 IT->dpvec holds the glyphs to return as characters.
6370 IT->saved_face_id holds the face id before the display vector--it
6371 is restored into IT->face_id in set_iterator_to_next. */
6373 static int
6374 next_element_from_display_vector (struct it *it)
6376 Lisp_Object gc;
6378 /* Precondition. */
6379 xassert (it->dpvec && it->current.dpvec_index >= 0);
6381 it->face_id = it->saved_face_id;
6383 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6384 That seemed totally bogus - so I changed it... */
6385 gc = it->dpvec[it->current.dpvec_index];
6387 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6389 it->c = GLYPH_CODE_CHAR (gc);
6390 it->len = CHAR_BYTES (it->c);
6392 /* The entry may contain a face id to use. Such a face id is
6393 the id of a Lisp face, not a realized face. A face id of
6394 zero means no face is specified. */
6395 if (it->dpvec_face_id >= 0)
6396 it->face_id = it->dpvec_face_id;
6397 else
6399 int lface_id = GLYPH_CODE_FACE (gc);
6400 if (lface_id > 0)
6401 it->face_id = merge_faces (it->f, Qt, lface_id,
6402 it->saved_face_id);
6405 else
6406 /* Display table entry is invalid. Return a space. */
6407 it->c = ' ', it->len = 1;
6409 /* Don't change position and object of the iterator here. They are
6410 still the values of the character that had this display table
6411 entry or was translated, and that's what we want. */
6412 it->what = IT_CHARACTER;
6413 return 1;
6417 /* Load IT with the next display element from Lisp string IT->string.
6418 IT->current.string_pos is the current position within the string.
6419 If IT->current.overlay_string_index >= 0, the Lisp string is an
6420 overlay string. */
6422 static int
6423 next_element_from_string (struct it *it)
6425 struct text_pos position;
6427 xassert (STRINGP (it->string));
6428 xassert (IT_STRING_CHARPOS (*it) >= 0);
6429 position = it->current.string_pos;
6431 /* Time to check for invisible text? */
6432 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6433 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6435 handle_stop (it);
6437 /* Since a handler may have changed IT->method, we must
6438 recurse here. */
6439 return GET_NEXT_DISPLAY_ELEMENT (it);
6442 if (it->current.overlay_string_index >= 0)
6444 /* Get the next character from an overlay string. In overlay
6445 strings, There is no field width or padding with spaces to
6446 do. */
6447 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6449 it->what = IT_EOB;
6450 return 0;
6452 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6453 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6454 && next_element_from_composition (it))
6456 return 1;
6458 else if (STRING_MULTIBYTE (it->string))
6460 const unsigned char *s = (SDATA (it->string)
6461 + IT_STRING_BYTEPOS (*it));
6462 it->c = string_char_and_length (s, &it->len);
6464 else
6466 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6467 it->len = 1;
6470 else
6472 /* Get the next character from a Lisp string that is not an
6473 overlay string. Such strings come from the mode line, for
6474 example. We may have to pad with spaces, or truncate the
6475 string. See also next_element_from_c_string. */
6476 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6478 it->what = IT_EOB;
6479 return 0;
6481 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6483 /* Pad with spaces. */
6484 it->c = ' ', it->len = 1;
6485 CHARPOS (position) = BYTEPOS (position) = -1;
6487 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6488 IT_STRING_BYTEPOS (*it), it->string_nchars)
6489 && next_element_from_composition (it))
6491 return 1;
6493 else if (STRING_MULTIBYTE (it->string))
6495 const unsigned char *s = (SDATA (it->string)
6496 + IT_STRING_BYTEPOS (*it));
6497 it->c = string_char_and_length (s, &it->len);
6499 else
6501 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6502 it->len = 1;
6506 /* Record what we have and where it came from. */
6507 it->what = IT_CHARACTER;
6508 it->object = it->string;
6509 it->position = position;
6510 return 1;
6514 /* Load IT with next display element from C string IT->s.
6515 IT->string_nchars is the maximum number of characters to return
6516 from the string. IT->end_charpos may be greater than
6517 IT->string_nchars when this function is called, in which case we
6518 may have to return padding spaces. Value is zero if end of string
6519 reached, including padding spaces. */
6521 static int
6522 next_element_from_c_string (struct it *it)
6524 int success_p = 1;
6526 xassert (it->s);
6527 it->what = IT_CHARACTER;
6528 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6529 it->object = Qnil;
6531 /* IT's position can be greater IT->string_nchars in case a field
6532 width or precision has been specified when the iterator was
6533 initialized. */
6534 if (IT_CHARPOS (*it) >= it->end_charpos)
6536 /* End of the game. */
6537 it->what = IT_EOB;
6538 success_p = 0;
6540 else if (IT_CHARPOS (*it) >= it->string_nchars)
6542 /* Pad with spaces. */
6543 it->c = ' ', it->len = 1;
6544 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6546 else if (it->multibyte_p)
6547 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6548 else
6549 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6551 return success_p;
6555 /* Set up IT to return characters from an ellipsis, if appropriate.
6556 The definition of the ellipsis glyphs may come from a display table
6557 entry. This function fills IT with the first glyph from the
6558 ellipsis if an ellipsis is to be displayed. */
6560 static int
6561 next_element_from_ellipsis (struct it *it)
6563 if (it->selective_display_ellipsis_p)
6564 setup_for_ellipsis (it, it->len);
6565 else
6567 /* The face at the current position may be different from the
6568 face we find after the invisible text. Remember what it
6569 was in IT->saved_face_id, and signal that it's there by
6570 setting face_before_selective_p. */
6571 it->saved_face_id = it->face_id;
6572 it->method = GET_FROM_BUFFER;
6573 it->object = it->w->buffer;
6574 reseat_at_next_visible_line_start (it, 1);
6575 it->face_before_selective_p = 1;
6578 return GET_NEXT_DISPLAY_ELEMENT (it);
6582 /* Deliver an image display element. The iterator IT is already
6583 filled with image information (done in handle_display_prop). Value
6584 is always 1. */
6587 static int
6588 next_element_from_image (struct it *it)
6590 it->what = IT_IMAGE;
6591 it->ignore_overlay_strings_at_pos_p = 0;
6592 return 1;
6596 /* Fill iterator IT with next display element from a stretch glyph
6597 property. IT->object is the value of the text property. Value is
6598 always 1. */
6600 static int
6601 next_element_from_stretch (struct it *it)
6603 it->what = IT_STRETCH;
6604 return 1;
6607 /* Scan forward from CHARPOS in the current buffer, until we find a
6608 stop position > current IT's position. Then handle the stop
6609 position before that. This is called when we bump into a stop
6610 position while reordering bidirectional text. CHARPOS should be
6611 the last previously processed stop_pos (or BEGV, if none were
6612 processed yet) whose position is less that IT's current
6613 position. */
6615 static void
6616 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6618 EMACS_INT where_we_are = IT_CHARPOS (*it);
6619 struct display_pos save_current = it->current;
6620 struct text_pos save_position = it->position;
6621 struct text_pos pos1;
6622 EMACS_INT next_stop;
6624 /* Scan in strict logical order. */
6625 it->bidi_p = 0;
6628 it->prev_stop = charpos;
6629 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6630 reseat_1 (it, pos1, 0);
6631 compute_stop_pos (it);
6632 /* We must advance forward, right? */
6633 if (it->stop_charpos <= it->prev_stop)
6634 abort ();
6635 charpos = it->stop_charpos;
6637 while (charpos <= where_we_are);
6639 next_stop = it->stop_charpos;
6640 it->stop_charpos = it->prev_stop;
6641 it->bidi_p = 1;
6642 it->current = save_current;
6643 it->position = save_position;
6644 handle_stop (it);
6645 it->stop_charpos = next_stop;
6648 /* Load IT with the next display element from current_buffer. Value
6649 is zero if end of buffer reached. IT->stop_charpos is the next
6650 position at which to stop and check for text properties or buffer
6651 end. */
6653 static int
6654 next_element_from_buffer (struct it *it)
6656 int success_p = 1;
6658 xassert (IT_CHARPOS (*it) >= BEGV);
6660 /* With bidi reordering, the character to display might not be the
6661 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6662 we were reseat()ed to a new buffer position, which is potentially
6663 a different paragraph. */
6664 if (it->bidi_p && it->bidi_it.first_elt)
6666 it->bidi_it.charpos = IT_CHARPOS (*it);
6667 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6668 if (it->bidi_it.bytepos == ZV_BYTE)
6670 /* Nothing to do, but reset the FIRST_ELT flag, like
6671 bidi_paragraph_init does, because we are not going to
6672 call it. */
6673 it->bidi_it.first_elt = 0;
6675 else if (it->bidi_it.bytepos == BEGV_BYTE
6676 /* FIXME: Should support all Unicode line separators. */
6677 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6678 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6680 /* If we are at the beginning of a line, we can produce the
6681 next element right away. */
6682 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6683 bidi_move_to_visually_next (&it->bidi_it);
6685 else
6687 EMACS_INT orig_bytepos = IT_BYTEPOS (*it);
6689 /* We need to prime the bidi iterator starting at the line's
6690 beginning, before we will be able to produce the next
6691 element. */
6692 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6693 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6694 it->bidi_it.charpos = IT_CHARPOS (*it);
6695 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6696 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6699 /* Now return to buffer position where we were asked to
6700 get the next display element, and produce that. */
6701 bidi_move_to_visually_next (&it->bidi_it);
6703 while (it->bidi_it.bytepos != orig_bytepos
6704 && it->bidi_it.bytepos < ZV_BYTE);
6707 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6708 /* Adjust IT's position information to where we ended up. */
6709 IT_CHARPOS (*it) = it->bidi_it.charpos;
6710 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6711 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6713 EMACS_INT stop = it->end_charpos;
6714 if (it->bidi_it.scan_dir < 0)
6715 stop = -1;
6716 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6717 IT_BYTEPOS (*it), stop, Qnil);
6721 if (IT_CHARPOS (*it) >= it->stop_charpos)
6723 if (IT_CHARPOS (*it) >= it->end_charpos)
6725 int overlay_strings_follow_p;
6727 /* End of the game, except when overlay strings follow that
6728 haven't been returned yet. */
6729 if (it->overlay_strings_at_end_processed_p)
6730 overlay_strings_follow_p = 0;
6731 else
6733 it->overlay_strings_at_end_processed_p = 1;
6734 overlay_strings_follow_p = get_overlay_strings (it, 0);
6737 if (overlay_strings_follow_p)
6738 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6739 else
6741 it->what = IT_EOB;
6742 it->position = it->current.pos;
6743 success_p = 0;
6746 else if (!(!it->bidi_p
6747 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6748 || IT_CHARPOS (*it) == it->stop_charpos))
6750 /* With bidi non-linear iteration, we could find ourselves
6751 far beyond the last computed stop_charpos, with several
6752 other stop positions in between that we missed. Scan
6753 them all now, in buffer's logical order, until we find
6754 and handle the last stop_charpos that precedes our
6755 current position. */
6756 handle_stop_backwards (it, it->stop_charpos);
6757 return GET_NEXT_DISPLAY_ELEMENT (it);
6759 else
6761 if (it->bidi_p)
6763 /* Take note of the stop position we just moved across,
6764 for when we will move back across it. */
6765 it->prev_stop = it->stop_charpos;
6766 /* If we are at base paragraph embedding level, take
6767 note of the last stop position seen at this
6768 level. */
6769 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6770 it->base_level_stop = it->stop_charpos;
6772 handle_stop (it);
6773 return GET_NEXT_DISPLAY_ELEMENT (it);
6776 else if (it->bidi_p
6777 /* We can sometimes back up for reasons that have nothing
6778 to do with bidi reordering. E.g., compositions. The
6779 code below is only needed when we are above the base
6780 embedding level, so test for that explicitly. */
6781 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6782 && IT_CHARPOS (*it) < it->prev_stop)
6784 if (it->base_level_stop <= 0)
6785 it->base_level_stop = BEGV;
6786 if (IT_CHARPOS (*it) < it->base_level_stop)
6787 abort ();
6788 handle_stop_backwards (it, it->base_level_stop);
6789 return GET_NEXT_DISPLAY_ELEMENT (it);
6791 else
6793 /* No face changes, overlays etc. in sight, so just return a
6794 character from current_buffer. */
6795 unsigned char *p;
6796 EMACS_INT stop;
6798 /* Maybe run the redisplay end trigger hook. Performance note:
6799 This doesn't seem to cost measurable time. */
6800 if (it->redisplay_end_trigger_charpos
6801 && it->glyph_row
6802 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6803 run_redisplay_end_trigger_hook (it);
6805 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6806 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6807 stop)
6808 && next_element_from_composition (it))
6810 return 1;
6813 /* Get the next character, maybe multibyte. */
6814 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6815 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6816 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6817 else
6818 it->c = *p, it->len = 1;
6820 /* Record what we have and where it came from. */
6821 it->what = IT_CHARACTER;
6822 it->object = it->w->buffer;
6823 it->position = it->current.pos;
6825 /* Normally we return the character found above, except when we
6826 really want to return an ellipsis for selective display. */
6827 if (it->selective)
6829 if (it->c == '\n')
6831 /* A value of selective > 0 means hide lines indented more
6832 than that number of columns. */
6833 if (it->selective > 0
6834 && IT_CHARPOS (*it) + 1 < ZV
6835 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6836 IT_BYTEPOS (*it) + 1,
6837 (double) it->selective)) /* iftc */
6839 success_p = next_element_from_ellipsis (it);
6840 it->dpvec_char_len = -1;
6843 else if (it->c == '\r' && it->selective == -1)
6845 /* A value of selective == -1 means that everything from the
6846 CR to the end of the line is invisible, with maybe an
6847 ellipsis displayed for it. */
6848 success_p = next_element_from_ellipsis (it);
6849 it->dpvec_char_len = -1;
6854 /* Value is zero if end of buffer reached. */
6855 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6856 return success_p;
6860 /* Run the redisplay end trigger hook for IT. */
6862 static void
6863 run_redisplay_end_trigger_hook (struct it *it)
6865 Lisp_Object args[3];
6867 /* IT->glyph_row should be non-null, i.e. we should be actually
6868 displaying something, or otherwise we should not run the hook. */
6869 xassert (it->glyph_row);
6871 /* Set up hook arguments. */
6872 args[0] = Qredisplay_end_trigger_functions;
6873 args[1] = it->window;
6874 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6875 it->redisplay_end_trigger_charpos = 0;
6877 /* Since we are *trying* to run these functions, don't try to run
6878 them again, even if they get an error. */
6879 it->w->redisplay_end_trigger = Qnil;
6880 Frun_hook_with_args (3, args);
6882 /* Notice if it changed the face of the character we are on. */
6883 handle_face_prop (it);
6887 /* Deliver a composition display element. Unlike the other
6888 next_element_from_XXX, this function is not registered in the array
6889 get_next_element[]. It is called from next_element_from_buffer and
6890 next_element_from_string when necessary. */
6892 static int
6893 next_element_from_composition (struct it *it)
6895 it->what = IT_COMPOSITION;
6896 it->len = it->cmp_it.nbytes;
6897 if (STRINGP (it->string))
6899 if (it->c < 0)
6901 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6902 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6903 return 0;
6905 it->position = it->current.string_pos;
6906 it->object = it->string;
6907 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6908 IT_STRING_BYTEPOS (*it), it->string);
6910 else
6912 if (it->c < 0)
6914 IT_CHARPOS (*it) += it->cmp_it.nchars;
6915 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6916 if (it->bidi_p)
6918 if (it->bidi_it.new_paragraph)
6919 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6920 /* Resync the bidi iterator with IT's new position.
6921 FIXME: this doesn't support bidirectional text. */
6922 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6923 bidi_move_to_visually_next (&it->bidi_it);
6925 return 0;
6927 it->position = it->current.pos;
6928 it->object = it->w->buffer;
6929 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6930 IT_BYTEPOS (*it), Qnil);
6932 return 1;
6937 /***********************************************************************
6938 Moving an iterator without producing glyphs
6939 ***********************************************************************/
6941 /* Check if iterator is at a position corresponding to a valid buffer
6942 position after some move_it_ call. */
6944 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6945 ((it)->method == GET_FROM_STRING \
6946 ? IT_STRING_CHARPOS (*it) == 0 \
6947 : 1)
6950 /* Move iterator IT to a specified buffer or X position within one
6951 line on the display without producing glyphs.
6953 OP should be a bit mask including some or all of these bits:
6954 MOVE_TO_X: Stop upon reaching x-position TO_X.
6955 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6956 Regardless of OP's value, stop upon reaching the end of the display line.
6958 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6959 This means, in particular, that TO_X includes window's horizontal
6960 scroll amount.
6962 The return value has several possible values that
6963 say what condition caused the scan to stop:
6965 MOVE_POS_MATCH_OR_ZV
6966 - when TO_POS or ZV was reached.
6968 MOVE_X_REACHED
6969 -when TO_X was reached before TO_POS or ZV were reached.
6971 MOVE_LINE_CONTINUED
6972 - when we reached the end of the display area and the line must
6973 be continued.
6975 MOVE_LINE_TRUNCATED
6976 - when we reached the end of the display area and the line is
6977 truncated.
6979 MOVE_NEWLINE_OR_CR
6980 - when we stopped at a line end, i.e. a newline or a CR and selective
6981 display is on. */
6983 static enum move_it_result
6984 move_it_in_display_line_to (struct it *it,
6985 EMACS_INT to_charpos, int to_x,
6986 enum move_operation_enum op)
6988 enum move_it_result result = MOVE_UNDEFINED;
6989 struct glyph_row *saved_glyph_row;
6990 struct it wrap_it, atpos_it, atx_it;
6991 int may_wrap = 0;
6992 enum it_method prev_method = it->method;
6993 EMACS_INT prev_pos = IT_CHARPOS (*it);
6995 /* Don't produce glyphs in produce_glyphs. */
6996 saved_glyph_row = it->glyph_row;
6997 it->glyph_row = NULL;
6999 /* Use wrap_it to save a copy of IT wherever a word wrap could
7000 occur. Use atpos_it to save a copy of IT at the desired buffer
7001 position, if found, so that we can scan ahead and check if the
7002 word later overshoots the window edge. Use atx_it similarly, for
7003 pixel positions. */
7004 wrap_it.sp = -1;
7005 atpos_it.sp = -1;
7006 atx_it.sp = -1;
7008 #define BUFFER_POS_REACHED_P() \
7009 ((op & MOVE_TO_POS) != 0 \
7010 && BUFFERP (it->object) \
7011 && (IT_CHARPOS (*it) == to_charpos \
7012 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7013 && (it->method == GET_FROM_BUFFER \
7014 || (it->method == GET_FROM_DISPLAY_VECTOR \
7015 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7017 /* If there's a line-/wrap-prefix, handle it. */
7018 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7019 && it->current_y < it->last_visible_y)
7020 handle_line_prefix (it);
7022 while (1)
7024 int x, i, ascent = 0, descent = 0;
7026 /* Utility macro to reset an iterator with x, ascent, and descent. */
7027 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7028 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7029 (IT)->max_descent = descent)
7031 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7032 glyph). */
7033 if ((op & MOVE_TO_POS) != 0
7034 && BUFFERP (it->object)
7035 && it->method == GET_FROM_BUFFER
7036 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7037 || (it->bidi_p
7038 && (prev_method == GET_FROM_IMAGE
7039 || prev_method == GET_FROM_STRETCH)
7040 /* Passed TO_CHARPOS from left to right. */
7041 && ((prev_pos < to_charpos
7042 && IT_CHARPOS (*it) > to_charpos)
7043 /* Passed TO_CHARPOS from right to left. */
7044 || (prev_pos > to_charpos
7045 && IT_CHARPOS (*it) < to_charpos)))))
7047 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7049 result = MOVE_POS_MATCH_OR_ZV;
7050 break;
7052 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7053 /* If wrap_it is valid, the current position might be in a
7054 word that is wrapped. So, save the iterator in
7055 atpos_it and continue to see if wrapping happens. */
7056 atpos_it = *it;
7059 prev_method = it->method;
7060 if (it->method == GET_FROM_BUFFER)
7061 prev_pos = IT_CHARPOS (*it);
7062 /* Stop when ZV reached.
7063 We used to stop here when TO_CHARPOS reached as well, but that is
7064 too soon if this glyph does not fit on this line. So we handle it
7065 explicitly below. */
7066 if (!get_next_display_element (it))
7068 result = MOVE_POS_MATCH_OR_ZV;
7069 break;
7072 if (it->line_wrap == TRUNCATE)
7074 if (BUFFER_POS_REACHED_P ())
7076 result = MOVE_POS_MATCH_OR_ZV;
7077 break;
7080 else
7082 if (it->line_wrap == WORD_WRAP)
7084 if (IT_DISPLAYING_WHITESPACE (it))
7085 may_wrap = 1;
7086 else if (may_wrap)
7088 /* We have reached a glyph that follows one or more
7089 whitespace characters. If the position is
7090 already found, we are done. */
7091 if (atpos_it.sp >= 0)
7093 *it = atpos_it;
7094 result = MOVE_POS_MATCH_OR_ZV;
7095 goto done;
7097 if (atx_it.sp >= 0)
7099 *it = atx_it;
7100 result = MOVE_X_REACHED;
7101 goto done;
7103 /* Otherwise, we can wrap here. */
7104 wrap_it = *it;
7105 may_wrap = 0;
7110 /* Remember the line height for the current line, in case
7111 the next element doesn't fit on the line. */
7112 ascent = it->max_ascent;
7113 descent = it->max_descent;
7115 /* The call to produce_glyphs will get the metrics of the
7116 display element IT is loaded with. Record the x-position
7117 before this display element, in case it doesn't fit on the
7118 line. */
7119 x = it->current_x;
7121 PRODUCE_GLYPHS (it);
7123 if (it->area != TEXT_AREA)
7125 set_iterator_to_next (it, 1);
7126 continue;
7129 /* The number of glyphs we get back in IT->nglyphs will normally
7130 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7131 character on a terminal frame, or (iii) a line end. For the
7132 second case, IT->nglyphs - 1 padding glyphs will be present.
7133 (On X frames, there is only one glyph produced for a
7134 composite character.)
7136 The behavior implemented below means, for continuation lines,
7137 that as many spaces of a TAB as fit on the current line are
7138 displayed there. For terminal frames, as many glyphs of a
7139 multi-glyph character are displayed in the current line, too.
7140 This is what the old redisplay code did, and we keep it that
7141 way. Under X, the whole shape of a complex character must
7142 fit on the line or it will be completely displayed in the
7143 next line.
7145 Note that both for tabs and padding glyphs, all glyphs have
7146 the same width. */
7147 if (it->nglyphs)
7149 /* More than one glyph or glyph doesn't fit on line. All
7150 glyphs have the same width. */
7151 int single_glyph_width = it->pixel_width / it->nglyphs;
7152 int new_x;
7153 int x_before_this_char = x;
7154 int hpos_before_this_char = it->hpos;
7156 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7158 new_x = x + single_glyph_width;
7160 /* We want to leave anything reaching TO_X to the caller. */
7161 if ((op & MOVE_TO_X) && new_x > to_x)
7163 if (BUFFER_POS_REACHED_P ())
7165 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7166 goto buffer_pos_reached;
7167 if (atpos_it.sp < 0)
7169 atpos_it = *it;
7170 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7173 else
7175 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7177 it->current_x = x;
7178 result = MOVE_X_REACHED;
7179 break;
7181 if (atx_it.sp < 0)
7183 atx_it = *it;
7184 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7189 if (/* Lines are continued. */
7190 it->line_wrap != TRUNCATE
7191 && (/* And glyph doesn't fit on the line. */
7192 new_x > it->last_visible_x
7193 /* Or it fits exactly and we're on a window
7194 system frame. */
7195 || (new_x == it->last_visible_x
7196 && FRAME_WINDOW_P (it->f))))
7198 if (/* IT->hpos == 0 means the very first glyph
7199 doesn't fit on the line, e.g. a wide image. */
7200 it->hpos == 0
7201 || (new_x == it->last_visible_x
7202 && FRAME_WINDOW_P (it->f)))
7204 ++it->hpos;
7205 it->current_x = new_x;
7207 /* The character's last glyph just barely fits
7208 in this row. */
7209 if (i == it->nglyphs - 1)
7211 /* If this is the destination position,
7212 return a position *before* it in this row,
7213 now that we know it fits in this row. */
7214 if (BUFFER_POS_REACHED_P ())
7216 if (it->line_wrap != WORD_WRAP
7217 || wrap_it.sp < 0)
7219 it->hpos = hpos_before_this_char;
7220 it->current_x = x_before_this_char;
7221 result = MOVE_POS_MATCH_OR_ZV;
7222 break;
7224 if (it->line_wrap == WORD_WRAP
7225 && atpos_it.sp < 0)
7227 atpos_it = *it;
7228 atpos_it.current_x = x_before_this_char;
7229 atpos_it.hpos = hpos_before_this_char;
7233 set_iterator_to_next (it, 1);
7234 /* On graphical terminals, newlines may
7235 "overflow" into the fringe if
7236 overflow-newline-into-fringe is non-nil.
7237 On text-only terminals, newlines may
7238 overflow into the last glyph on the
7239 display line.*/
7240 if (!FRAME_WINDOW_P (it->f)
7241 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7243 if (!get_next_display_element (it))
7245 result = MOVE_POS_MATCH_OR_ZV;
7246 break;
7248 if (BUFFER_POS_REACHED_P ())
7250 if (ITERATOR_AT_END_OF_LINE_P (it))
7251 result = MOVE_POS_MATCH_OR_ZV;
7252 else
7253 result = MOVE_LINE_CONTINUED;
7254 break;
7256 if (ITERATOR_AT_END_OF_LINE_P (it))
7258 result = MOVE_NEWLINE_OR_CR;
7259 break;
7264 else
7265 IT_RESET_X_ASCENT_DESCENT (it);
7267 if (wrap_it.sp >= 0)
7269 *it = wrap_it;
7270 atpos_it.sp = -1;
7271 atx_it.sp = -1;
7274 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7275 IT_CHARPOS (*it)));
7276 result = MOVE_LINE_CONTINUED;
7277 break;
7280 if (BUFFER_POS_REACHED_P ())
7282 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7283 goto buffer_pos_reached;
7284 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7286 atpos_it = *it;
7287 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7291 if (new_x > it->first_visible_x)
7293 /* Glyph is visible. Increment number of glyphs that
7294 would be displayed. */
7295 ++it->hpos;
7299 if (result != MOVE_UNDEFINED)
7300 break;
7302 else if (BUFFER_POS_REACHED_P ())
7304 buffer_pos_reached:
7305 IT_RESET_X_ASCENT_DESCENT (it);
7306 result = MOVE_POS_MATCH_OR_ZV;
7307 break;
7309 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7311 /* Stop when TO_X specified and reached. This check is
7312 necessary here because of lines consisting of a line end,
7313 only. The line end will not produce any glyphs and we
7314 would never get MOVE_X_REACHED. */
7315 xassert (it->nglyphs == 0);
7316 result = MOVE_X_REACHED;
7317 break;
7320 /* Is this a line end? If yes, we're done. */
7321 if (ITERATOR_AT_END_OF_LINE_P (it))
7323 result = MOVE_NEWLINE_OR_CR;
7324 break;
7327 if (it->method == GET_FROM_BUFFER)
7328 prev_pos = IT_CHARPOS (*it);
7329 /* The current display element has been consumed. Advance
7330 to the next. */
7331 set_iterator_to_next (it, 1);
7333 /* Stop if lines are truncated and IT's current x-position is
7334 past the right edge of the window now. */
7335 if (it->line_wrap == TRUNCATE
7336 && it->current_x >= it->last_visible_x)
7338 if (!FRAME_WINDOW_P (it->f)
7339 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7341 if (!get_next_display_element (it)
7342 || BUFFER_POS_REACHED_P ())
7344 result = MOVE_POS_MATCH_OR_ZV;
7345 break;
7347 if (ITERATOR_AT_END_OF_LINE_P (it))
7349 result = MOVE_NEWLINE_OR_CR;
7350 break;
7353 result = MOVE_LINE_TRUNCATED;
7354 break;
7356 #undef IT_RESET_X_ASCENT_DESCENT
7359 #undef BUFFER_POS_REACHED_P
7361 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7362 restore the saved iterator. */
7363 if (atpos_it.sp >= 0)
7364 *it = atpos_it;
7365 else if (atx_it.sp >= 0)
7366 *it = atx_it;
7368 done:
7370 /* Restore the iterator settings altered at the beginning of this
7371 function. */
7372 it->glyph_row = saved_glyph_row;
7373 return result;
7376 /* For external use. */
7377 void
7378 move_it_in_display_line (struct it *it,
7379 EMACS_INT to_charpos, int to_x,
7380 enum move_operation_enum op)
7382 if (it->line_wrap == WORD_WRAP
7383 && (op & MOVE_TO_X))
7385 struct it save_it = *it;
7386 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7387 /* When word-wrap is on, TO_X may lie past the end
7388 of a wrapped line. Then it->current is the
7389 character on the next line, so backtrack to the
7390 space before the wrap point. */
7391 if (skip == MOVE_LINE_CONTINUED)
7393 int prev_x = max (it->current_x - 1, 0);
7394 *it = save_it;
7395 move_it_in_display_line_to
7396 (it, -1, prev_x, MOVE_TO_X);
7399 else
7400 move_it_in_display_line_to (it, to_charpos, to_x, op);
7404 /* Move IT forward until it satisfies one or more of the criteria in
7405 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7407 OP is a bit-mask that specifies where to stop, and in particular,
7408 which of those four position arguments makes a difference. See the
7409 description of enum move_operation_enum.
7411 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7412 screen line, this function will set IT to the next position >
7413 TO_CHARPOS. */
7415 void
7416 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7418 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7419 int line_height, line_start_x = 0, reached = 0;
7421 for (;;)
7423 if (op & MOVE_TO_VPOS)
7425 /* If no TO_CHARPOS and no TO_X specified, stop at the
7426 start of the line TO_VPOS. */
7427 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7429 if (it->vpos == to_vpos)
7431 reached = 1;
7432 break;
7434 else
7435 skip = move_it_in_display_line_to (it, -1, -1, 0);
7437 else
7439 /* TO_VPOS >= 0 means stop at TO_X in the line at
7440 TO_VPOS, or at TO_POS, whichever comes first. */
7441 if (it->vpos == to_vpos)
7443 reached = 2;
7444 break;
7447 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7449 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7451 reached = 3;
7452 break;
7454 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7456 /* We have reached TO_X but not in the line we want. */
7457 skip = move_it_in_display_line_to (it, to_charpos,
7458 -1, MOVE_TO_POS);
7459 if (skip == MOVE_POS_MATCH_OR_ZV)
7461 reached = 4;
7462 break;
7467 else if (op & MOVE_TO_Y)
7469 struct it it_backup;
7471 if (it->line_wrap == WORD_WRAP)
7472 it_backup = *it;
7474 /* TO_Y specified means stop at TO_X in the line containing
7475 TO_Y---or at TO_CHARPOS if this is reached first. The
7476 problem is that we can't really tell whether the line
7477 contains TO_Y before we have completely scanned it, and
7478 this may skip past TO_X. What we do is to first scan to
7479 TO_X.
7481 If TO_X is not specified, use a TO_X of zero. The reason
7482 is to make the outcome of this function more predictable.
7483 If we didn't use TO_X == 0, we would stop at the end of
7484 the line which is probably not what a caller would expect
7485 to happen. */
7486 skip = move_it_in_display_line_to
7487 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7488 (MOVE_TO_X | (op & MOVE_TO_POS)));
7490 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7491 if (skip == MOVE_POS_MATCH_OR_ZV)
7492 reached = 5;
7493 else if (skip == MOVE_X_REACHED)
7495 /* If TO_X was reached, we want to know whether TO_Y is
7496 in the line. We know this is the case if the already
7497 scanned glyphs make the line tall enough. Otherwise,
7498 we must check by scanning the rest of the line. */
7499 line_height = it->max_ascent + it->max_descent;
7500 if (to_y >= it->current_y
7501 && to_y < it->current_y + line_height)
7503 reached = 6;
7504 break;
7506 it_backup = *it;
7507 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7508 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7509 op & MOVE_TO_POS);
7510 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7511 line_height = it->max_ascent + it->max_descent;
7512 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7514 if (to_y >= it->current_y
7515 && to_y < it->current_y + line_height)
7517 /* If TO_Y is in this line and TO_X was reached
7518 above, we scanned too far. We have to restore
7519 IT's settings to the ones before skipping. */
7520 *it = it_backup;
7521 reached = 6;
7523 else
7525 skip = skip2;
7526 if (skip == MOVE_POS_MATCH_OR_ZV)
7527 reached = 7;
7530 else
7532 /* Check whether TO_Y is in this line. */
7533 line_height = it->max_ascent + it->max_descent;
7534 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7536 if (to_y >= it->current_y
7537 && to_y < it->current_y + line_height)
7539 /* When word-wrap is on, TO_X may lie past the end
7540 of a wrapped line. Then it->current is the
7541 character on the next line, so backtrack to the
7542 space before the wrap point. */
7543 if (skip == MOVE_LINE_CONTINUED
7544 && it->line_wrap == WORD_WRAP)
7546 int prev_x = max (it->current_x - 1, 0);
7547 *it = it_backup;
7548 skip = move_it_in_display_line_to
7549 (it, -1, prev_x, MOVE_TO_X);
7551 reached = 6;
7555 if (reached)
7556 break;
7558 else if (BUFFERP (it->object)
7559 && (it->method == GET_FROM_BUFFER
7560 || it->method == GET_FROM_STRETCH)
7561 && IT_CHARPOS (*it) >= to_charpos)
7562 skip = MOVE_POS_MATCH_OR_ZV;
7563 else
7564 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7566 switch (skip)
7568 case MOVE_POS_MATCH_OR_ZV:
7569 reached = 8;
7570 goto out;
7572 case MOVE_NEWLINE_OR_CR:
7573 set_iterator_to_next (it, 1);
7574 it->continuation_lines_width = 0;
7575 break;
7577 case MOVE_LINE_TRUNCATED:
7578 it->continuation_lines_width = 0;
7579 reseat_at_next_visible_line_start (it, 0);
7580 if ((op & MOVE_TO_POS) != 0
7581 && IT_CHARPOS (*it) > to_charpos)
7583 reached = 9;
7584 goto out;
7586 break;
7588 case MOVE_LINE_CONTINUED:
7589 /* For continued lines ending in a tab, some of the glyphs
7590 associated with the tab are displayed on the current
7591 line. Since it->current_x does not include these glyphs,
7592 we use it->last_visible_x instead. */
7593 if (it->c == '\t')
7595 it->continuation_lines_width += it->last_visible_x;
7596 /* When moving by vpos, ensure that the iterator really
7597 advances to the next line (bug#847, bug#969). Fixme:
7598 do we need to do this in other circumstances? */
7599 if (it->current_x != it->last_visible_x
7600 && (op & MOVE_TO_VPOS)
7601 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7603 line_start_x = it->current_x + it->pixel_width
7604 - it->last_visible_x;
7605 set_iterator_to_next (it, 0);
7608 else
7609 it->continuation_lines_width += it->current_x;
7610 break;
7612 default:
7613 abort ();
7616 /* Reset/increment for the next run. */
7617 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7618 it->current_x = line_start_x;
7619 line_start_x = 0;
7620 it->hpos = 0;
7621 it->current_y += it->max_ascent + it->max_descent;
7622 ++it->vpos;
7623 last_height = it->max_ascent + it->max_descent;
7624 last_max_ascent = it->max_ascent;
7625 it->max_ascent = it->max_descent = 0;
7628 out:
7630 /* On text terminals, we may stop at the end of a line in the middle
7631 of a multi-character glyph. If the glyph itself is continued,
7632 i.e. it is actually displayed on the next line, don't treat this
7633 stopping point as valid; move to the next line instead (unless
7634 that brings us offscreen). */
7635 if (!FRAME_WINDOW_P (it->f)
7636 && op & MOVE_TO_POS
7637 && IT_CHARPOS (*it) == to_charpos
7638 && it->what == IT_CHARACTER
7639 && it->nglyphs > 1
7640 && it->line_wrap == WINDOW_WRAP
7641 && it->current_x == it->last_visible_x - 1
7642 && it->c != '\n'
7643 && it->c != '\t'
7644 && it->vpos < XFASTINT (it->w->window_end_vpos))
7646 it->continuation_lines_width += it->current_x;
7647 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7648 it->current_y += it->max_ascent + it->max_descent;
7649 ++it->vpos;
7650 last_height = it->max_ascent + it->max_descent;
7651 last_max_ascent = it->max_ascent;
7654 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7658 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7660 If DY > 0, move IT backward at least that many pixels. DY = 0
7661 means move IT backward to the preceding line start or BEGV. This
7662 function may move over more than DY pixels if IT->current_y - DY
7663 ends up in the middle of a line; in this case IT->current_y will be
7664 set to the top of the line moved to. */
7666 void
7667 move_it_vertically_backward (struct it *it, int dy)
7669 int nlines, h;
7670 struct it it2, it3;
7671 EMACS_INT start_pos;
7673 move_further_back:
7674 xassert (dy >= 0);
7676 start_pos = IT_CHARPOS (*it);
7678 /* Estimate how many newlines we must move back. */
7679 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7681 /* Set the iterator's position that many lines back. */
7682 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7683 back_to_previous_visible_line_start (it);
7685 /* Reseat the iterator here. When moving backward, we don't want
7686 reseat to skip forward over invisible text, set up the iterator
7687 to deliver from overlay strings at the new position etc. So,
7688 use reseat_1 here. */
7689 reseat_1 (it, it->current.pos, 1);
7691 /* We are now surely at a line start. */
7692 it->current_x = it->hpos = 0;
7693 it->continuation_lines_width = 0;
7695 /* Move forward and see what y-distance we moved. First move to the
7696 start of the next line so that we get its height. We need this
7697 height to be able to tell whether we reached the specified
7698 y-distance. */
7699 it2 = *it;
7700 it2.max_ascent = it2.max_descent = 0;
7703 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7704 MOVE_TO_POS | MOVE_TO_VPOS);
7706 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7707 xassert (IT_CHARPOS (*it) >= BEGV);
7708 it3 = it2;
7710 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7711 xassert (IT_CHARPOS (*it) >= BEGV);
7712 /* H is the actual vertical distance from the position in *IT
7713 and the starting position. */
7714 h = it2.current_y - it->current_y;
7715 /* NLINES is the distance in number of lines. */
7716 nlines = it2.vpos - it->vpos;
7718 /* Correct IT's y and vpos position
7719 so that they are relative to the starting point. */
7720 it->vpos -= nlines;
7721 it->current_y -= h;
7723 if (dy == 0)
7725 /* DY == 0 means move to the start of the screen line. The
7726 value of nlines is > 0 if continuation lines were involved. */
7727 if (nlines > 0)
7728 move_it_by_lines (it, nlines, 1);
7730 else
7732 /* The y-position we try to reach, relative to *IT.
7733 Note that H has been subtracted in front of the if-statement. */
7734 int target_y = it->current_y + h - dy;
7735 int y0 = it3.current_y;
7736 int y1 = line_bottom_y (&it3);
7737 int line_height = y1 - y0;
7739 /* If we did not reach target_y, try to move further backward if
7740 we can. If we moved too far backward, try to move forward. */
7741 if (target_y < it->current_y
7742 /* This is heuristic. In a window that's 3 lines high, with
7743 a line height of 13 pixels each, recentering with point
7744 on the bottom line will try to move -39/2 = 19 pixels
7745 backward. Try to avoid moving into the first line. */
7746 && (it->current_y - target_y
7747 > min (window_box_height (it->w), line_height * 2 / 3))
7748 && IT_CHARPOS (*it) > BEGV)
7750 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7751 target_y - it->current_y));
7752 dy = it->current_y - target_y;
7753 goto move_further_back;
7755 else if (target_y >= it->current_y + line_height
7756 && IT_CHARPOS (*it) < ZV)
7758 /* Should move forward by at least one line, maybe more.
7760 Note: Calling move_it_by_lines can be expensive on
7761 terminal frames, where compute_motion is used (via
7762 vmotion) to do the job, when there are very long lines
7763 and truncate-lines is nil. That's the reason for
7764 treating terminal frames specially here. */
7766 if (!FRAME_WINDOW_P (it->f))
7767 move_it_vertically (it, target_y - (it->current_y + line_height));
7768 else
7772 move_it_by_lines (it, 1, 1);
7774 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7781 /* Move IT by a specified amount of pixel lines DY. DY negative means
7782 move backwards. DY = 0 means move to start of screen line. At the
7783 end, IT will be on the start of a screen line. */
7785 void
7786 move_it_vertically (struct it *it, int dy)
7788 if (dy <= 0)
7789 move_it_vertically_backward (it, -dy);
7790 else
7792 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7793 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7794 MOVE_TO_POS | MOVE_TO_Y);
7795 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7797 /* If buffer ends in ZV without a newline, move to the start of
7798 the line to satisfy the post-condition. */
7799 if (IT_CHARPOS (*it) == ZV
7800 && ZV > BEGV
7801 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7802 move_it_by_lines (it, 0, 0);
7807 /* Move iterator IT past the end of the text line it is in. */
7809 void
7810 move_it_past_eol (struct it *it)
7812 enum move_it_result rc;
7814 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7815 if (rc == MOVE_NEWLINE_OR_CR)
7816 set_iterator_to_next (it, 0);
7820 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7821 negative means move up. DVPOS == 0 means move to the start of the
7822 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7823 NEED_Y_P is zero, IT->current_y will be left unchanged.
7825 Further optimization ideas: If we would know that IT->f doesn't use
7826 a face with proportional font, we could be faster for
7827 truncate-lines nil. */
7829 void
7830 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7833 /* The commented-out optimization uses vmotion on terminals. This
7834 gives bad results, because elements like it->what, on which
7835 callers such as pos_visible_p rely, aren't updated. */
7836 /* struct position pos;
7837 if (!FRAME_WINDOW_P (it->f))
7839 struct text_pos textpos;
7841 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7842 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7843 reseat (it, textpos, 1);
7844 it->vpos += pos.vpos;
7845 it->current_y += pos.vpos;
7847 else */
7849 if (dvpos == 0)
7851 /* DVPOS == 0 means move to the start of the screen line. */
7852 move_it_vertically_backward (it, 0);
7853 xassert (it->current_x == 0 && it->hpos == 0);
7854 /* Let next call to line_bottom_y calculate real line height */
7855 last_height = 0;
7857 else if (dvpos > 0)
7859 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7860 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7861 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7863 else
7865 struct it it2;
7866 EMACS_INT start_charpos, i;
7868 /* Start at the beginning of the screen line containing IT's
7869 position. This may actually move vertically backwards,
7870 in case of overlays, so adjust dvpos accordingly. */
7871 dvpos += it->vpos;
7872 move_it_vertically_backward (it, 0);
7873 dvpos -= it->vpos;
7875 /* Go back -DVPOS visible lines and reseat the iterator there. */
7876 start_charpos = IT_CHARPOS (*it);
7877 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7878 back_to_previous_visible_line_start (it);
7879 reseat (it, it->current.pos, 1);
7881 /* Move further back if we end up in a string or an image. */
7882 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7884 /* First try to move to start of display line. */
7885 dvpos += it->vpos;
7886 move_it_vertically_backward (it, 0);
7887 dvpos -= it->vpos;
7888 if (IT_POS_VALID_AFTER_MOVE_P (it))
7889 break;
7890 /* If start of line is still in string or image,
7891 move further back. */
7892 back_to_previous_visible_line_start (it);
7893 reseat (it, it->current.pos, 1);
7894 dvpos--;
7897 it->current_x = it->hpos = 0;
7899 /* Above call may have moved too far if continuation lines
7900 are involved. Scan forward and see if it did. */
7901 it2 = *it;
7902 it2.vpos = it2.current_y = 0;
7903 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7904 it->vpos -= it2.vpos;
7905 it->current_y -= it2.current_y;
7906 it->current_x = it->hpos = 0;
7908 /* If we moved too far back, move IT some lines forward. */
7909 if (it2.vpos > -dvpos)
7911 int delta = it2.vpos + dvpos;
7912 it2 = *it;
7913 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7914 /* Move back again if we got too far ahead. */
7915 if (IT_CHARPOS (*it) >= start_charpos)
7916 *it = it2;
7921 /* Return 1 if IT points into the middle of a display vector. */
7924 in_display_vector_p (struct it *it)
7926 return (it->method == GET_FROM_DISPLAY_VECTOR
7927 && it->current.dpvec_index > 0
7928 && it->dpvec + it->current.dpvec_index != it->dpend);
7932 /***********************************************************************
7933 Messages
7934 ***********************************************************************/
7937 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7938 to *Messages*. */
7940 void
7941 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
7943 Lisp_Object args[3];
7944 Lisp_Object msg, fmt;
7945 char *buffer;
7946 EMACS_INT len;
7947 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7948 USE_SAFE_ALLOCA;
7950 /* Do nothing if called asynchronously. Inserting text into
7951 a buffer may call after-change-functions and alike and
7952 that would means running Lisp asynchronously. */
7953 if (handling_signal)
7954 return;
7956 fmt = msg = Qnil;
7957 GCPRO4 (fmt, msg, arg1, arg2);
7959 args[0] = fmt = build_string (format);
7960 args[1] = arg1;
7961 args[2] = arg2;
7962 msg = Fformat (3, args);
7964 len = SBYTES (msg) + 1;
7965 SAFE_ALLOCA (buffer, char *, len);
7966 memcpy (buffer, SDATA (msg), len);
7968 message_dolog (buffer, len - 1, 1, 0);
7969 SAFE_FREE ();
7971 UNGCPRO;
7975 /* Output a newline in the *Messages* buffer if "needs" one. */
7977 void
7978 message_log_maybe_newline (void)
7980 if (message_log_need_newline)
7981 message_dolog ("", 0, 1, 0);
7985 /* Add a string M of length NBYTES to the message log, optionally
7986 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7987 nonzero, means interpret the contents of M as multibyte. This
7988 function calls low-level routines in order to bypass text property
7989 hooks, etc. which might not be safe to run.
7991 This may GC (insert may run before/after change hooks),
7992 so the buffer M must NOT point to a Lisp string. */
7994 void
7995 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
7997 if (!NILP (Vmemory_full))
7998 return;
8000 if (!NILP (Vmessage_log_max))
8002 struct buffer *oldbuf;
8003 Lisp_Object oldpoint, oldbegv, oldzv;
8004 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8005 EMACS_INT point_at_end = 0;
8006 EMACS_INT zv_at_end = 0;
8007 Lisp_Object old_deactivate_mark, tem;
8008 struct gcpro gcpro1;
8010 old_deactivate_mark = Vdeactivate_mark;
8011 oldbuf = current_buffer;
8012 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8013 current_buffer->undo_list = Qt;
8015 oldpoint = message_dolog_marker1;
8016 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8017 oldbegv = message_dolog_marker2;
8018 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8019 oldzv = message_dolog_marker3;
8020 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8021 GCPRO1 (old_deactivate_mark);
8023 if (PT == Z)
8024 point_at_end = 1;
8025 if (ZV == Z)
8026 zv_at_end = 1;
8028 BEGV = BEG;
8029 BEGV_BYTE = BEG_BYTE;
8030 ZV = Z;
8031 ZV_BYTE = Z_BYTE;
8032 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8034 /* Insert the string--maybe converting multibyte to single byte
8035 or vice versa, so that all the text fits the buffer. */
8036 if (multibyte
8037 && NILP (current_buffer->enable_multibyte_characters))
8039 EMACS_INT i;
8040 int c, char_bytes;
8041 unsigned char work[1];
8043 /* Convert a multibyte string to single-byte
8044 for the *Message* buffer. */
8045 for (i = 0; i < nbytes; i += char_bytes)
8047 c = string_char_and_length (m + i, &char_bytes);
8048 work[0] = (ASCII_CHAR_P (c)
8050 : multibyte_char_to_unibyte (c, Qnil));
8051 insert_1_both (work, 1, 1, 1, 0, 0);
8054 else if (! multibyte
8055 && ! NILP (current_buffer->enable_multibyte_characters))
8057 EMACS_INT i;
8058 int c, char_bytes;
8059 unsigned char *msg = (unsigned char *) m;
8060 unsigned char str[MAX_MULTIBYTE_LENGTH];
8061 /* Convert a single-byte string to multibyte
8062 for the *Message* buffer. */
8063 for (i = 0; i < nbytes; i++)
8065 c = msg[i];
8066 MAKE_CHAR_MULTIBYTE (c);
8067 char_bytes = CHAR_STRING (c, str);
8068 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8071 else if (nbytes)
8072 insert_1 (m, nbytes, 1, 0, 0);
8074 if (nlflag)
8076 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8077 int dup;
8078 insert_1 ("\n", 1, 1, 0, 0);
8080 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8081 this_bol = PT;
8082 this_bol_byte = PT_BYTE;
8084 /* See if this line duplicates the previous one.
8085 If so, combine duplicates. */
8086 if (this_bol > BEG)
8088 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8089 prev_bol = PT;
8090 prev_bol_byte = PT_BYTE;
8092 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8093 this_bol, this_bol_byte);
8094 if (dup)
8096 del_range_both (prev_bol, prev_bol_byte,
8097 this_bol, this_bol_byte, 0);
8098 if (dup > 1)
8100 char dupstr[40];
8101 int duplen;
8103 /* If you change this format, don't forget to also
8104 change message_log_check_duplicate. */
8105 sprintf (dupstr, " [%d times]", dup);
8106 duplen = strlen (dupstr);
8107 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8108 insert_1 (dupstr, duplen, 1, 0, 1);
8113 /* If we have more than the desired maximum number of lines
8114 in the *Messages* buffer now, delete the oldest ones.
8115 This is safe because we don't have undo in this buffer. */
8117 if (NATNUMP (Vmessage_log_max))
8119 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8120 -XFASTINT (Vmessage_log_max) - 1, 0);
8121 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8124 BEGV = XMARKER (oldbegv)->charpos;
8125 BEGV_BYTE = marker_byte_position (oldbegv);
8127 if (zv_at_end)
8129 ZV = Z;
8130 ZV_BYTE = Z_BYTE;
8132 else
8134 ZV = XMARKER (oldzv)->charpos;
8135 ZV_BYTE = marker_byte_position (oldzv);
8138 if (point_at_end)
8139 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8140 else
8141 /* We can't do Fgoto_char (oldpoint) because it will run some
8142 Lisp code. */
8143 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8144 XMARKER (oldpoint)->bytepos);
8146 UNGCPRO;
8147 unchain_marker (XMARKER (oldpoint));
8148 unchain_marker (XMARKER (oldbegv));
8149 unchain_marker (XMARKER (oldzv));
8151 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8152 set_buffer_internal (oldbuf);
8153 if (NILP (tem))
8154 windows_or_buffers_changed = old_windows_or_buffers_changed;
8155 message_log_need_newline = !nlflag;
8156 Vdeactivate_mark = old_deactivate_mark;
8161 /* We are at the end of the buffer after just having inserted a newline.
8162 (Note: We depend on the fact we won't be crossing the gap.)
8163 Check to see if the most recent message looks a lot like the previous one.
8164 Return 0 if different, 1 if the new one should just replace it, or a
8165 value N > 1 if we should also append " [N times]". */
8167 static int
8168 message_log_check_duplicate (EMACS_INT prev_bol, EMACS_INT prev_bol_byte,
8169 EMACS_INT this_bol, EMACS_INT this_bol_byte)
8171 EMACS_INT i;
8172 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8173 int seen_dots = 0;
8174 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8175 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8177 for (i = 0; i < len; i++)
8179 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8180 seen_dots = 1;
8181 if (p1[i] != p2[i])
8182 return seen_dots;
8184 p1 += len;
8185 if (*p1 == '\n')
8186 return 2;
8187 if (*p1++ == ' ' && *p1++ == '[')
8189 int n = 0;
8190 while (*p1 >= '0' && *p1 <= '9')
8191 n = n * 10 + *p1++ - '0';
8192 if (strncmp (p1, " times]\n", 8) == 0)
8193 return n+1;
8195 return 0;
8199 /* Display an echo area message M with a specified length of NBYTES
8200 bytes. The string may include null characters. If M is 0, clear
8201 out any existing message, and let the mini-buffer text show
8202 through.
8204 This may GC, so the buffer M must NOT point to a Lisp string. */
8206 void
8207 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8209 /* First flush out any partial line written with print. */
8210 message_log_maybe_newline ();
8211 if (m)
8212 message_dolog (m, nbytes, 1, multibyte);
8213 message2_nolog (m, nbytes, multibyte);
8217 /* The non-logging counterpart of message2. */
8219 void
8220 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8222 struct frame *sf = SELECTED_FRAME ();
8223 message_enable_multibyte = multibyte;
8225 if (FRAME_INITIAL_P (sf))
8227 if (noninteractive_need_newline)
8228 putc ('\n', stderr);
8229 noninteractive_need_newline = 0;
8230 if (m)
8231 fwrite (m, nbytes, 1, stderr);
8232 if (cursor_in_echo_area == 0)
8233 fprintf (stderr, "\n");
8234 fflush (stderr);
8236 /* A null message buffer means that the frame hasn't really been
8237 initialized yet. Error messages get reported properly by
8238 cmd_error, so this must be just an informative message; toss it. */
8239 else if (INTERACTIVE
8240 && sf->glyphs_initialized_p
8241 && FRAME_MESSAGE_BUF (sf))
8243 Lisp_Object mini_window;
8244 struct frame *f;
8246 /* Get the frame containing the mini-buffer
8247 that the selected frame is using. */
8248 mini_window = FRAME_MINIBUF_WINDOW (sf);
8249 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8251 FRAME_SAMPLE_VISIBILITY (f);
8252 if (FRAME_VISIBLE_P (sf)
8253 && ! FRAME_VISIBLE_P (f))
8254 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8256 if (m)
8258 set_message (m, Qnil, nbytes, multibyte);
8259 if (minibuffer_auto_raise)
8260 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8262 else
8263 clear_message (1, 1);
8265 do_pending_window_change (0);
8266 echo_area_display (1);
8267 do_pending_window_change (0);
8268 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8269 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8274 /* Display an echo area message M with a specified length of NBYTES
8275 bytes. The string may include null characters. If M is not a
8276 string, clear out any existing message, and let the mini-buffer
8277 text show through.
8279 This function cancels echoing. */
8281 void
8282 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8284 struct gcpro gcpro1;
8286 GCPRO1 (m);
8287 clear_message (1,1);
8288 cancel_echoing ();
8290 /* First flush out any partial line written with print. */
8291 message_log_maybe_newline ();
8292 if (STRINGP (m))
8294 char *buffer;
8295 USE_SAFE_ALLOCA;
8297 SAFE_ALLOCA (buffer, char *, nbytes);
8298 memcpy (buffer, SDATA (m), nbytes);
8299 message_dolog (buffer, nbytes, 1, multibyte);
8300 SAFE_FREE ();
8302 message3_nolog (m, nbytes, multibyte);
8304 UNGCPRO;
8308 /* The non-logging version of message3.
8309 This does not cancel echoing, because it is used for echoing.
8310 Perhaps we need to make a separate function for echoing
8311 and make this cancel echoing. */
8313 void
8314 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8316 struct frame *sf = SELECTED_FRAME ();
8317 message_enable_multibyte = multibyte;
8319 if (FRAME_INITIAL_P (sf))
8321 if (noninteractive_need_newline)
8322 putc ('\n', stderr);
8323 noninteractive_need_newline = 0;
8324 if (STRINGP (m))
8325 fwrite (SDATA (m), nbytes, 1, stderr);
8326 if (cursor_in_echo_area == 0)
8327 fprintf (stderr, "\n");
8328 fflush (stderr);
8330 /* A null message buffer means that the frame hasn't really been
8331 initialized yet. Error messages get reported properly by
8332 cmd_error, so this must be just an informative message; toss it. */
8333 else if (INTERACTIVE
8334 && sf->glyphs_initialized_p
8335 && FRAME_MESSAGE_BUF (sf))
8337 Lisp_Object mini_window;
8338 Lisp_Object frame;
8339 struct frame *f;
8341 /* Get the frame containing the mini-buffer
8342 that the selected frame is using. */
8343 mini_window = FRAME_MINIBUF_WINDOW (sf);
8344 frame = XWINDOW (mini_window)->frame;
8345 f = XFRAME (frame);
8347 FRAME_SAMPLE_VISIBILITY (f);
8348 if (FRAME_VISIBLE_P (sf)
8349 && !FRAME_VISIBLE_P (f))
8350 Fmake_frame_visible (frame);
8352 if (STRINGP (m) && SCHARS (m) > 0)
8354 set_message (NULL, m, nbytes, multibyte);
8355 if (minibuffer_auto_raise)
8356 Fraise_frame (frame);
8357 /* Assume we are not echoing.
8358 (If we are, echo_now will override this.) */
8359 echo_message_buffer = Qnil;
8361 else
8362 clear_message (1, 1);
8364 do_pending_window_change (0);
8365 echo_area_display (1);
8366 do_pending_window_change (0);
8367 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8368 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8373 /* Display a null-terminated echo area message M. If M is 0, clear
8374 out any existing message, and let the mini-buffer text show through.
8376 The buffer M must continue to exist until after the echo area gets
8377 cleared or some other message gets displayed there. Do not pass
8378 text that is stored in a Lisp string. Do not pass text in a buffer
8379 that was alloca'd. */
8381 void
8382 message1 (const char *m)
8384 message2 (m, (m ? strlen (m) : 0), 0);
8388 /* The non-logging counterpart of message1. */
8390 void
8391 message1_nolog (const char *m)
8393 message2_nolog (m, (m ? strlen (m) : 0), 0);
8396 /* Display a message M which contains a single %s
8397 which gets replaced with STRING. */
8399 void
8400 message_with_string (const char *m, Lisp_Object string, int log)
8402 CHECK_STRING (string);
8404 if (noninteractive)
8406 if (m)
8408 if (noninteractive_need_newline)
8409 putc ('\n', stderr);
8410 noninteractive_need_newline = 0;
8411 fprintf (stderr, m, SDATA (string));
8412 if (!cursor_in_echo_area)
8413 fprintf (stderr, "\n");
8414 fflush (stderr);
8417 else if (INTERACTIVE)
8419 /* The frame whose minibuffer we're going to display the message on.
8420 It may be larger than the selected frame, so we need
8421 to use its buffer, not the selected frame's buffer. */
8422 Lisp_Object mini_window;
8423 struct frame *f, *sf = SELECTED_FRAME ();
8425 /* Get the frame containing the minibuffer
8426 that the selected frame is using. */
8427 mini_window = FRAME_MINIBUF_WINDOW (sf);
8428 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8430 /* A null message buffer means that the frame hasn't really been
8431 initialized yet. Error messages get reported properly by
8432 cmd_error, so this must be just an informative message; toss it. */
8433 if (FRAME_MESSAGE_BUF (f))
8435 Lisp_Object args[2], message;
8436 struct gcpro gcpro1, gcpro2;
8438 args[0] = build_string (m);
8439 args[1] = message = string;
8440 GCPRO2 (args[0], message);
8441 gcpro1.nvars = 2;
8443 message = Fformat (2, args);
8445 if (log)
8446 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8447 else
8448 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8450 UNGCPRO;
8452 /* Print should start at the beginning of the message
8453 buffer next time. */
8454 message_buf_print = 0;
8460 /* Dump an informative message to the minibuf. If M is 0, clear out
8461 any existing message, and let the mini-buffer text show through. */
8463 static void
8464 vmessage (const char *m, va_list ap)
8466 if (noninteractive)
8468 if (m)
8470 if (noninteractive_need_newline)
8471 putc ('\n', stderr);
8472 noninteractive_need_newline = 0;
8473 vfprintf (stderr, m, ap);
8474 if (cursor_in_echo_area == 0)
8475 fprintf (stderr, "\n");
8476 fflush (stderr);
8479 else if (INTERACTIVE)
8481 /* The frame whose mini-buffer we're going to display the message
8482 on. It may be larger than the selected frame, so we need to
8483 use its buffer, not the selected frame's buffer. */
8484 Lisp_Object mini_window;
8485 struct frame *f, *sf = SELECTED_FRAME ();
8487 /* Get the frame containing the mini-buffer
8488 that the selected frame is using. */
8489 mini_window = FRAME_MINIBUF_WINDOW (sf);
8490 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8492 /* A null message buffer means that the frame hasn't really been
8493 initialized yet. Error messages get reported properly by
8494 cmd_error, so this must be just an informative message; toss
8495 it. */
8496 if (FRAME_MESSAGE_BUF (f))
8498 if (m)
8500 EMACS_INT len;
8502 len = doprnt (FRAME_MESSAGE_BUF (f),
8503 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8505 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8507 else
8508 message1 (0);
8510 /* Print should start at the beginning of the message
8511 buffer next time. */
8512 message_buf_print = 0;
8517 void
8518 message (const char *m, ...)
8520 va_list ap;
8521 va_start (ap, m);
8522 vmessage (m, ap);
8523 va_end (ap);
8527 /* The non-logging version of message. */
8529 void
8530 message_nolog (const char *m, ...)
8532 Lisp_Object old_log_max;
8533 va_list ap;
8534 va_start (ap, m);
8535 old_log_max = Vmessage_log_max;
8536 Vmessage_log_max = Qnil;
8537 vmessage (m, ap);
8538 Vmessage_log_max = old_log_max;
8539 va_end (ap);
8543 /* Display the current message in the current mini-buffer. This is
8544 only called from error handlers in process.c, and is not time
8545 critical. */
8547 void
8548 update_echo_area (void)
8550 if (!NILP (echo_area_buffer[0]))
8552 Lisp_Object string;
8553 string = Fcurrent_message ();
8554 message3 (string, SBYTES (string),
8555 !NILP (current_buffer->enable_multibyte_characters));
8560 /* Make sure echo area buffers in `echo_buffers' are live.
8561 If they aren't, make new ones. */
8563 static void
8564 ensure_echo_area_buffers (void)
8566 int i;
8568 for (i = 0; i < 2; ++i)
8569 if (!BUFFERP (echo_buffer[i])
8570 || NILP (XBUFFER (echo_buffer[i])->name))
8572 char name[30];
8573 Lisp_Object old_buffer;
8574 int j;
8576 old_buffer = echo_buffer[i];
8577 sprintf (name, " *Echo Area %d*", i);
8578 echo_buffer[i] = Fget_buffer_create (build_string (name));
8579 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8580 /* to force word wrap in echo area -
8581 it was decided to postpone this*/
8582 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8584 for (j = 0; j < 2; ++j)
8585 if (EQ (old_buffer, echo_area_buffer[j]))
8586 echo_area_buffer[j] = echo_buffer[i];
8591 /* Call FN with args A1..A4 with either the current or last displayed
8592 echo_area_buffer as current buffer.
8594 WHICH zero means use the current message buffer
8595 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8596 from echo_buffer[] and clear it.
8598 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8599 suitable buffer from echo_buffer[] and clear it.
8601 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8602 that the current message becomes the last displayed one, make
8603 choose a suitable buffer for echo_area_buffer[0], and clear it.
8605 Value is what FN returns. */
8607 static int
8608 with_echo_area_buffer (struct window *w, int which,
8609 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8610 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8612 Lisp_Object buffer;
8613 int this_one, the_other, clear_buffer_p, rc;
8614 int count = SPECPDL_INDEX ();
8616 /* If buffers aren't live, make new ones. */
8617 ensure_echo_area_buffers ();
8619 clear_buffer_p = 0;
8621 if (which == 0)
8622 this_one = 0, the_other = 1;
8623 else if (which > 0)
8624 this_one = 1, the_other = 0;
8625 else
8627 this_one = 0, the_other = 1;
8628 clear_buffer_p = 1;
8630 /* We need a fresh one in case the current echo buffer equals
8631 the one containing the last displayed echo area message. */
8632 if (!NILP (echo_area_buffer[this_one])
8633 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8634 echo_area_buffer[this_one] = Qnil;
8637 /* Choose a suitable buffer from echo_buffer[] is we don't
8638 have one. */
8639 if (NILP (echo_area_buffer[this_one]))
8641 echo_area_buffer[this_one]
8642 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8643 ? echo_buffer[the_other]
8644 : echo_buffer[this_one]);
8645 clear_buffer_p = 1;
8648 buffer = echo_area_buffer[this_one];
8650 /* Don't get confused by reusing the buffer used for echoing
8651 for a different purpose. */
8652 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8653 cancel_echoing ();
8655 record_unwind_protect (unwind_with_echo_area_buffer,
8656 with_echo_area_buffer_unwind_data (w));
8658 /* Make the echo area buffer current. Note that for display
8659 purposes, it is not necessary that the displayed window's buffer
8660 == current_buffer, except for text property lookup. So, let's
8661 only set that buffer temporarily here without doing a full
8662 Fset_window_buffer. We must also change w->pointm, though,
8663 because otherwise an assertions in unshow_buffer fails, and Emacs
8664 aborts. */
8665 set_buffer_internal_1 (XBUFFER (buffer));
8666 if (w)
8668 w->buffer = buffer;
8669 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8672 current_buffer->undo_list = Qt;
8673 current_buffer->read_only = Qnil;
8674 specbind (Qinhibit_read_only, Qt);
8675 specbind (Qinhibit_modification_hooks, Qt);
8677 if (clear_buffer_p && Z > BEG)
8678 del_range (BEG, Z);
8680 xassert (BEGV >= BEG);
8681 xassert (ZV <= Z && ZV >= BEGV);
8683 rc = fn (a1, a2, a3, a4);
8685 xassert (BEGV >= BEG);
8686 xassert (ZV <= Z && ZV >= BEGV);
8688 unbind_to (count, Qnil);
8689 return rc;
8693 /* Save state that should be preserved around the call to the function
8694 FN called in with_echo_area_buffer. */
8696 static Lisp_Object
8697 with_echo_area_buffer_unwind_data (struct window *w)
8699 int i = 0;
8700 Lisp_Object vector, tmp;
8702 /* Reduce consing by keeping one vector in
8703 Vwith_echo_area_save_vector. */
8704 vector = Vwith_echo_area_save_vector;
8705 Vwith_echo_area_save_vector = Qnil;
8707 if (NILP (vector))
8708 vector = Fmake_vector (make_number (7), Qnil);
8710 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8711 ASET (vector, i, Vdeactivate_mark); ++i;
8712 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8714 if (w)
8716 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8717 ASET (vector, i, w->buffer); ++i;
8718 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8719 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8721 else
8723 int end = i + 4;
8724 for (; i < end; ++i)
8725 ASET (vector, i, Qnil);
8728 xassert (i == ASIZE (vector));
8729 return vector;
8733 /* Restore global state from VECTOR which was created by
8734 with_echo_area_buffer_unwind_data. */
8736 static Lisp_Object
8737 unwind_with_echo_area_buffer (Lisp_Object vector)
8739 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8740 Vdeactivate_mark = AREF (vector, 1);
8741 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8743 if (WINDOWP (AREF (vector, 3)))
8745 struct window *w;
8746 Lisp_Object buffer, charpos, bytepos;
8748 w = XWINDOW (AREF (vector, 3));
8749 buffer = AREF (vector, 4);
8750 charpos = AREF (vector, 5);
8751 bytepos = AREF (vector, 6);
8753 w->buffer = buffer;
8754 set_marker_both (w->pointm, buffer,
8755 XFASTINT (charpos), XFASTINT (bytepos));
8758 Vwith_echo_area_save_vector = vector;
8759 return Qnil;
8763 /* Set up the echo area for use by print functions. MULTIBYTE_P
8764 non-zero means we will print multibyte. */
8766 void
8767 setup_echo_area_for_printing (int multibyte_p)
8769 /* If we can't find an echo area any more, exit. */
8770 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8771 Fkill_emacs (Qnil);
8773 ensure_echo_area_buffers ();
8775 if (!message_buf_print)
8777 /* A message has been output since the last time we printed.
8778 Choose a fresh echo area buffer. */
8779 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8780 echo_area_buffer[0] = echo_buffer[1];
8781 else
8782 echo_area_buffer[0] = echo_buffer[0];
8784 /* Switch to that buffer and clear it. */
8785 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8786 current_buffer->truncate_lines = Qnil;
8788 if (Z > BEG)
8790 int count = SPECPDL_INDEX ();
8791 specbind (Qinhibit_read_only, Qt);
8792 /* Note that undo recording is always disabled. */
8793 del_range (BEG, Z);
8794 unbind_to (count, Qnil);
8796 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8798 /* Set up the buffer for the multibyteness we need. */
8799 if (multibyte_p
8800 != !NILP (current_buffer->enable_multibyte_characters))
8801 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8803 /* Raise the frame containing the echo area. */
8804 if (minibuffer_auto_raise)
8806 struct frame *sf = SELECTED_FRAME ();
8807 Lisp_Object mini_window;
8808 mini_window = FRAME_MINIBUF_WINDOW (sf);
8809 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8812 message_log_maybe_newline ();
8813 message_buf_print = 1;
8815 else
8817 if (NILP (echo_area_buffer[0]))
8819 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8820 echo_area_buffer[0] = echo_buffer[1];
8821 else
8822 echo_area_buffer[0] = echo_buffer[0];
8825 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8827 /* Someone switched buffers between print requests. */
8828 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8829 current_buffer->truncate_lines = Qnil;
8835 /* Display an echo area message in window W. Value is non-zero if W's
8836 height is changed. If display_last_displayed_message_p is
8837 non-zero, display the message that was last displayed, otherwise
8838 display the current message. */
8840 static int
8841 display_echo_area (struct window *w)
8843 int i, no_message_p, window_height_changed_p, count;
8845 /* Temporarily disable garbage collections while displaying the echo
8846 area. This is done because a GC can print a message itself.
8847 That message would modify the echo area buffer's contents while a
8848 redisplay of the buffer is going on, and seriously confuse
8849 redisplay. */
8850 count = inhibit_garbage_collection ();
8852 /* If there is no message, we must call display_echo_area_1
8853 nevertheless because it resizes the window. But we will have to
8854 reset the echo_area_buffer in question to nil at the end because
8855 with_echo_area_buffer will sets it to an empty buffer. */
8856 i = display_last_displayed_message_p ? 1 : 0;
8857 no_message_p = NILP (echo_area_buffer[i]);
8859 window_height_changed_p
8860 = with_echo_area_buffer (w, display_last_displayed_message_p,
8861 display_echo_area_1,
8862 (EMACS_INT) w, Qnil, 0, 0);
8864 if (no_message_p)
8865 echo_area_buffer[i] = Qnil;
8867 unbind_to (count, Qnil);
8868 return window_height_changed_p;
8872 /* Helper for display_echo_area. Display the current buffer which
8873 contains the current echo area message in window W, a mini-window,
8874 a pointer to which is passed in A1. A2..A4 are currently not used.
8875 Change the height of W so that all of the message is displayed.
8876 Value is non-zero if height of W was changed. */
8878 static int
8879 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8881 struct window *w = (struct window *) a1;
8882 Lisp_Object window;
8883 struct text_pos start;
8884 int window_height_changed_p = 0;
8886 /* Do this before displaying, so that we have a large enough glyph
8887 matrix for the display. If we can't get enough space for the
8888 whole text, display the last N lines. That works by setting w->start. */
8889 window_height_changed_p = resize_mini_window (w, 0);
8891 /* Use the starting position chosen by resize_mini_window. */
8892 SET_TEXT_POS_FROM_MARKER (start, w->start);
8894 /* Display. */
8895 clear_glyph_matrix (w->desired_matrix);
8896 XSETWINDOW (window, w);
8897 try_window (window, start, 0);
8899 return window_height_changed_p;
8903 /* Resize the echo area window to exactly the size needed for the
8904 currently displayed message, if there is one. If a mini-buffer
8905 is active, don't shrink it. */
8907 void
8908 resize_echo_area_exactly (void)
8910 if (BUFFERP (echo_area_buffer[0])
8911 && WINDOWP (echo_area_window))
8913 struct window *w = XWINDOW (echo_area_window);
8914 int resized_p;
8915 Lisp_Object resize_exactly;
8917 if (minibuf_level == 0)
8918 resize_exactly = Qt;
8919 else
8920 resize_exactly = Qnil;
8922 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8923 (EMACS_INT) w, resize_exactly, 0, 0);
8924 if (resized_p)
8926 ++windows_or_buffers_changed;
8927 ++update_mode_lines;
8928 redisplay_internal (0);
8934 /* Callback function for with_echo_area_buffer, when used from
8935 resize_echo_area_exactly. A1 contains a pointer to the window to
8936 resize, EXACTLY non-nil means resize the mini-window exactly to the
8937 size of the text displayed. A3 and A4 are not used. Value is what
8938 resize_mini_window returns. */
8940 static int
8941 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
8943 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8947 /* Resize mini-window W to fit the size of its contents. EXACT_P
8948 means size the window exactly to the size needed. Otherwise, it's
8949 only enlarged until W's buffer is empty.
8951 Set W->start to the right place to begin display. If the whole
8952 contents fit, start at the beginning. Otherwise, start so as
8953 to make the end of the contents appear. This is particularly
8954 important for y-or-n-p, but seems desirable generally.
8956 Value is non-zero if the window height has been changed. */
8959 resize_mini_window (struct window *w, int exact_p)
8961 struct frame *f = XFRAME (w->frame);
8962 int window_height_changed_p = 0;
8964 xassert (MINI_WINDOW_P (w));
8966 /* By default, start display at the beginning. */
8967 set_marker_both (w->start, w->buffer,
8968 BUF_BEGV (XBUFFER (w->buffer)),
8969 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8971 /* Don't resize windows while redisplaying a window; it would
8972 confuse redisplay functions when the size of the window they are
8973 displaying changes from under them. Such a resizing can happen,
8974 for instance, when which-func prints a long message while
8975 we are running fontification-functions. We're running these
8976 functions with safe_call which binds inhibit-redisplay to t. */
8977 if (!NILP (Vinhibit_redisplay))
8978 return 0;
8980 /* Nil means don't try to resize. */
8981 if (NILP (Vresize_mini_windows)
8982 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8983 return 0;
8985 if (!FRAME_MINIBUF_ONLY_P (f))
8987 struct it it;
8988 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8989 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8990 int height, max_height;
8991 int unit = FRAME_LINE_HEIGHT (f);
8992 struct text_pos start;
8993 struct buffer *old_current_buffer = NULL;
8995 if (current_buffer != XBUFFER (w->buffer))
8997 old_current_buffer = current_buffer;
8998 set_buffer_internal (XBUFFER (w->buffer));
9001 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9003 /* Compute the max. number of lines specified by the user. */
9004 if (FLOATP (Vmax_mini_window_height))
9005 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9006 else if (INTEGERP (Vmax_mini_window_height))
9007 max_height = XINT (Vmax_mini_window_height);
9008 else
9009 max_height = total_height / 4;
9011 /* Correct that max. height if it's bogus. */
9012 max_height = max (1, max_height);
9013 max_height = min (total_height, max_height);
9015 /* Find out the height of the text in the window. */
9016 if (it.line_wrap == TRUNCATE)
9017 height = 1;
9018 else
9020 last_height = 0;
9021 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9022 if (it.max_ascent == 0 && it.max_descent == 0)
9023 height = it.current_y + last_height;
9024 else
9025 height = it.current_y + it.max_ascent + it.max_descent;
9026 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9027 height = (height + unit - 1) / unit;
9030 /* Compute a suitable window start. */
9031 if (height > max_height)
9033 height = max_height;
9034 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9035 move_it_vertically_backward (&it, (height - 1) * unit);
9036 start = it.current.pos;
9038 else
9039 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9040 SET_MARKER_FROM_TEXT_POS (w->start, start);
9042 if (EQ (Vresize_mini_windows, Qgrow_only))
9044 /* Let it grow only, until we display an empty message, in which
9045 case the window shrinks again. */
9046 if (height > WINDOW_TOTAL_LINES (w))
9048 int old_height = WINDOW_TOTAL_LINES (w);
9049 freeze_window_starts (f, 1);
9050 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9051 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9053 else if (height < WINDOW_TOTAL_LINES (w)
9054 && (exact_p || BEGV == ZV))
9056 int old_height = WINDOW_TOTAL_LINES (w);
9057 freeze_window_starts (f, 0);
9058 shrink_mini_window (w);
9059 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9062 else
9064 /* Always resize to exact size needed. */
9065 if (height > WINDOW_TOTAL_LINES (w))
9067 int old_height = WINDOW_TOTAL_LINES (w);
9068 freeze_window_starts (f, 1);
9069 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9070 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9072 else if (height < WINDOW_TOTAL_LINES (w))
9074 int old_height = WINDOW_TOTAL_LINES (w);
9075 freeze_window_starts (f, 0);
9076 shrink_mini_window (w);
9078 if (height)
9080 freeze_window_starts (f, 1);
9081 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9084 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9088 if (old_current_buffer)
9089 set_buffer_internal (old_current_buffer);
9092 return window_height_changed_p;
9096 /* Value is the current message, a string, or nil if there is no
9097 current message. */
9099 Lisp_Object
9100 current_message (void)
9102 Lisp_Object msg;
9104 if (!BUFFERP (echo_area_buffer[0]))
9105 msg = Qnil;
9106 else
9108 with_echo_area_buffer (0, 0, current_message_1,
9109 (EMACS_INT) &msg, Qnil, 0, 0);
9110 if (NILP (msg))
9111 echo_area_buffer[0] = Qnil;
9114 return msg;
9118 static int
9119 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9121 Lisp_Object *msg = (Lisp_Object *) a1;
9123 if (Z > BEG)
9124 *msg = make_buffer_string (BEG, Z, 1);
9125 else
9126 *msg = Qnil;
9127 return 0;
9131 /* Push the current message on Vmessage_stack for later restauration
9132 by restore_message. Value is non-zero if the current message isn't
9133 empty. This is a relatively infrequent operation, so it's not
9134 worth optimizing. */
9137 push_message (void)
9139 Lisp_Object msg;
9140 msg = current_message ();
9141 Vmessage_stack = Fcons (msg, Vmessage_stack);
9142 return STRINGP (msg);
9146 /* Restore message display from the top of Vmessage_stack. */
9148 void
9149 restore_message (void)
9151 Lisp_Object msg;
9153 xassert (CONSP (Vmessage_stack));
9154 msg = XCAR (Vmessage_stack);
9155 if (STRINGP (msg))
9156 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9157 else
9158 message3_nolog (msg, 0, 0);
9162 /* Handler for record_unwind_protect calling pop_message. */
9164 Lisp_Object
9165 pop_message_unwind (Lisp_Object dummy)
9167 pop_message ();
9168 return Qnil;
9171 /* Pop the top-most entry off Vmessage_stack. */
9173 void
9174 pop_message (void)
9176 xassert (CONSP (Vmessage_stack));
9177 Vmessage_stack = XCDR (Vmessage_stack);
9181 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9182 exits. If the stack is not empty, we have a missing pop_message
9183 somewhere. */
9185 void
9186 check_message_stack (void)
9188 if (!NILP (Vmessage_stack))
9189 abort ();
9193 /* Truncate to NCHARS what will be displayed in the echo area the next
9194 time we display it---but don't redisplay it now. */
9196 void
9197 truncate_echo_area (EMACS_INT nchars)
9199 if (nchars == 0)
9200 echo_area_buffer[0] = Qnil;
9201 /* A null message buffer means that the frame hasn't really been
9202 initialized yet. Error messages get reported properly by
9203 cmd_error, so this must be just an informative message; toss it. */
9204 else if (!noninteractive
9205 && INTERACTIVE
9206 && !NILP (echo_area_buffer[0]))
9208 struct frame *sf = SELECTED_FRAME ();
9209 if (FRAME_MESSAGE_BUF (sf))
9210 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9215 /* Helper function for truncate_echo_area. Truncate the current
9216 message to at most NCHARS characters. */
9218 static int
9219 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9221 if (BEG + nchars < Z)
9222 del_range (BEG + nchars, Z);
9223 if (Z == BEG)
9224 echo_area_buffer[0] = Qnil;
9225 return 0;
9229 /* Set the current message to a substring of S or STRING.
9231 If STRING is a Lisp string, set the message to the first NBYTES
9232 bytes from STRING. NBYTES zero means use the whole string. If
9233 STRING is multibyte, the message will be displayed multibyte.
9235 If S is not null, set the message to the first LEN bytes of S. LEN
9236 zero means use the whole string. MULTIBYTE_P non-zero means S is
9237 multibyte. Display the message multibyte in that case.
9239 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9240 to t before calling set_message_1 (which calls insert).
9243 void
9244 set_message (const char *s, Lisp_Object string,
9245 EMACS_INT nbytes, int multibyte_p)
9247 message_enable_multibyte
9248 = ((s && multibyte_p)
9249 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9251 with_echo_area_buffer (0, -1, set_message_1,
9252 (EMACS_INT) s, string, nbytes, multibyte_p);
9253 message_buf_print = 0;
9254 help_echo_showing_p = 0;
9258 /* Helper function for set_message. Arguments have the same meaning
9259 as there, with A1 corresponding to S and A2 corresponding to STRING
9260 This function is called with the echo area buffer being
9261 current. */
9263 static int
9264 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9266 const char *s = (const char *) a1;
9267 Lisp_Object string = a2;
9269 /* Change multibyteness of the echo buffer appropriately. */
9270 if (message_enable_multibyte
9271 != !NILP (current_buffer->enable_multibyte_characters))
9272 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9274 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9276 /* Insert new message at BEG. */
9277 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9279 if (STRINGP (string))
9281 EMACS_INT nchars;
9283 if (nbytes == 0)
9284 nbytes = SBYTES (string);
9285 nchars = string_byte_to_char (string, nbytes);
9287 /* This function takes care of single/multibyte conversion. We
9288 just have to ensure that the echo area buffer has the right
9289 setting of enable_multibyte_characters. */
9290 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9292 else if (s)
9294 if (nbytes == 0)
9295 nbytes = strlen (s);
9297 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9299 /* Convert from multi-byte to single-byte. */
9300 EMACS_INT i;
9301 int c, n;
9302 unsigned char work[1];
9304 /* Convert a multibyte string to single-byte. */
9305 for (i = 0; i < nbytes; i += n)
9307 c = string_char_and_length (s + i, &n);
9308 work[0] = (ASCII_CHAR_P (c)
9310 : multibyte_char_to_unibyte (c, Qnil));
9311 insert_1_both (work, 1, 1, 1, 0, 0);
9314 else if (!multibyte_p
9315 && !NILP (current_buffer->enable_multibyte_characters))
9317 /* Convert from single-byte to multi-byte. */
9318 EMACS_INT i;
9319 int c, n;
9320 const unsigned char *msg = (const unsigned char *) s;
9321 unsigned char str[MAX_MULTIBYTE_LENGTH];
9323 /* Convert a single-byte string to multibyte. */
9324 for (i = 0; i < nbytes; i++)
9326 c = msg[i];
9327 MAKE_CHAR_MULTIBYTE (c);
9328 n = CHAR_STRING (c, str);
9329 insert_1_both (str, 1, n, 1, 0, 0);
9332 else
9333 insert_1 (s, nbytes, 1, 0, 0);
9336 return 0;
9340 /* Clear messages. CURRENT_P non-zero means clear the current
9341 message. LAST_DISPLAYED_P non-zero means clear the message
9342 last displayed. */
9344 void
9345 clear_message (int current_p, int last_displayed_p)
9347 if (current_p)
9349 echo_area_buffer[0] = Qnil;
9350 message_cleared_p = 1;
9353 if (last_displayed_p)
9354 echo_area_buffer[1] = Qnil;
9356 message_buf_print = 0;
9359 /* Clear garbaged frames.
9361 This function is used where the old redisplay called
9362 redraw_garbaged_frames which in turn called redraw_frame which in
9363 turn called clear_frame. The call to clear_frame was a source of
9364 flickering. I believe a clear_frame is not necessary. It should
9365 suffice in the new redisplay to invalidate all current matrices,
9366 and ensure a complete redisplay of all windows. */
9368 static void
9369 clear_garbaged_frames (void)
9371 if (frame_garbaged)
9373 Lisp_Object tail, frame;
9374 int changed_count = 0;
9376 FOR_EACH_FRAME (tail, frame)
9378 struct frame *f = XFRAME (frame);
9380 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9382 if (f->resized_p)
9384 Fredraw_frame (frame);
9385 f->force_flush_display_p = 1;
9387 clear_current_matrices (f);
9388 changed_count++;
9389 f->garbaged = 0;
9390 f->resized_p = 0;
9394 frame_garbaged = 0;
9395 if (changed_count)
9396 ++windows_or_buffers_changed;
9401 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9402 is non-zero update selected_frame. Value is non-zero if the
9403 mini-windows height has been changed. */
9405 static int
9406 echo_area_display (int update_frame_p)
9408 Lisp_Object mini_window;
9409 struct window *w;
9410 struct frame *f;
9411 int window_height_changed_p = 0;
9412 struct frame *sf = SELECTED_FRAME ();
9414 mini_window = FRAME_MINIBUF_WINDOW (sf);
9415 w = XWINDOW (mini_window);
9416 f = XFRAME (WINDOW_FRAME (w));
9418 /* Don't display if frame is invisible or not yet initialized. */
9419 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9420 return 0;
9422 #ifdef HAVE_WINDOW_SYSTEM
9423 /* When Emacs starts, selected_frame may be the initial terminal
9424 frame. If we let this through, a message would be displayed on
9425 the terminal. */
9426 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9427 return 0;
9428 #endif /* HAVE_WINDOW_SYSTEM */
9430 /* Redraw garbaged frames. */
9431 if (frame_garbaged)
9432 clear_garbaged_frames ();
9434 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9436 echo_area_window = mini_window;
9437 window_height_changed_p = display_echo_area (w);
9438 w->must_be_updated_p = 1;
9440 /* Update the display, unless called from redisplay_internal.
9441 Also don't update the screen during redisplay itself. The
9442 update will happen at the end of redisplay, and an update
9443 here could cause confusion. */
9444 if (update_frame_p && !redisplaying_p)
9446 int n = 0;
9448 /* If the display update has been interrupted by pending
9449 input, update mode lines in the frame. Due to the
9450 pending input, it might have been that redisplay hasn't
9451 been called, so that mode lines above the echo area are
9452 garbaged. This looks odd, so we prevent it here. */
9453 if (!display_completed)
9454 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9456 if (window_height_changed_p
9457 /* Don't do this if Emacs is shutting down. Redisplay
9458 needs to run hooks. */
9459 && !NILP (Vrun_hooks))
9461 /* Must update other windows. Likewise as in other
9462 cases, don't let this update be interrupted by
9463 pending input. */
9464 int count = SPECPDL_INDEX ();
9465 specbind (Qredisplay_dont_pause, Qt);
9466 windows_or_buffers_changed = 1;
9467 redisplay_internal (0);
9468 unbind_to (count, Qnil);
9470 else if (FRAME_WINDOW_P (f) && n == 0)
9472 /* Window configuration is the same as before.
9473 Can do with a display update of the echo area,
9474 unless we displayed some mode lines. */
9475 update_single_window (w, 1);
9476 FRAME_RIF (f)->flush_display (f);
9478 else
9479 update_frame (f, 1, 1);
9481 /* If cursor is in the echo area, make sure that the next
9482 redisplay displays the minibuffer, so that the cursor will
9483 be replaced with what the minibuffer wants. */
9484 if (cursor_in_echo_area)
9485 ++windows_or_buffers_changed;
9488 else if (!EQ (mini_window, selected_window))
9489 windows_or_buffers_changed++;
9491 /* Last displayed message is now the current message. */
9492 echo_area_buffer[1] = echo_area_buffer[0];
9493 /* Inform read_char that we're not echoing. */
9494 echo_message_buffer = Qnil;
9496 /* Prevent redisplay optimization in redisplay_internal by resetting
9497 this_line_start_pos. This is done because the mini-buffer now
9498 displays the message instead of its buffer text. */
9499 if (EQ (mini_window, selected_window))
9500 CHARPOS (this_line_start_pos) = 0;
9502 return window_height_changed_p;
9507 /***********************************************************************
9508 Mode Lines and Frame Titles
9509 ***********************************************************************/
9511 /* A buffer for constructing non-propertized mode-line strings and
9512 frame titles in it; allocated from the heap in init_xdisp and
9513 resized as needed in store_mode_line_noprop_char. */
9515 static char *mode_line_noprop_buf;
9517 /* The buffer's end, and a current output position in it. */
9519 static char *mode_line_noprop_buf_end;
9520 static char *mode_line_noprop_ptr;
9522 #define MODE_LINE_NOPROP_LEN(start) \
9523 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9525 static enum {
9526 MODE_LINE_DISPLAY = 0,
9527 MODE_LINE_TITLE,
9528 MODE_LINE_NOPROP,
9529 MODE_LINE_STRING
9530 } mode_line_target;
9532 /* Alist that caches the results of :propertize.
9533 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9534 static Lisp_Object mode_line_proptrans_alist;
9536 /* List of strings making up the mode-line. */
9537 static Lisp_Object mode_line_string_list;
9539 /* Base face property when building propertized mode line string. */
9540 static Lisp_Object mode_line_string_face;
9541 static Lisp_Object mode_line_string_face_prop;
9544 /* Unwind data for mode line strings */
9546 static Lisp_Object Vmode_line_unwind_vector;
9548 static Lisp_Object
9549 format_mode_line_unwind_data (struct buffer *obuf,
9550 Lisp_Object owin,
9551 int save_proptrans)
9553 Lisp_Object vector, tmp;
9555 /* Reduce consing by keeping one vector in
9556 Vwith_echo_area_save_vector. */
9557 vector = Vmode_line_unwind_vector;
9558 Vmode_line_unwind_vector = Qnil;
9560 if (NILP (vector))
9561 vector = Fmake_vector (make_number (8), Qnil);
9563 ASET (vector, 0, make_number (mode_line_target));
9564 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9565 ASET (vector, 2, mode_line_string_list);
9566 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9567 ASET (vector, 4, mode_line_string_face);
9568 ASET (vector, 5, mode_line_string_face_prop);
9570 if (obuf)
9571 XSETBUFFER (tmp, obuf);
9572 else
9573 tmp = Qnil;
9574 ASET (vector, 6, tmp);
9575 ASET (vector, 7, owin);
9577 return vector;
9580 static Lisp_Object
9581 unwind_format_mode_line (Lisp_Object vector)
9583 mode_line_target = XINT (AREF (vector, 0));
9584 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9585 mode_line_string_list = AREF (vector, 2);
9586 if (! EQ (AREF (vector, 3), Qt))
9587 mode_line_proptrans_alist = AREF (vector, 3);
9588 mode_line_string_face = AREF (vector, 4);
9589 mode_line_string_face_prop = AREF (vector, 5);
9591 if (!NILP (AREF (vector, 7)))
9592 /* Select window before buffer, since it may change the buffer. */
9593 Fselect_window (AREF (vector, 7), Qt);
9595 if (!NILP (AREF (vector, 6)))
9597 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9598 ASET (vector, 6, Qnil);
9601 Vmode_line_unwind_vector = vector;
9602 return Qnil;
9606 /* Store a single character C for the frame title in mode_line_noprop_buf.
9607 Re-allocate mode_line_noprop_buf if necessary. */
9609 static void
9610 store_mode_line_noprop_char (char c)
9612 /* If output position has reached the end of the allocated buffer,
9613 double the buffer's size. */
9614 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9616 int len = MODE_LINE_NOPROP_LEN (0);
9617 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9618 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9619 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9620 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9623 *mode_line_noprop_ptr++ = c;
9627 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9628 mode_line_noprop_ptr. STR is the string to store. Do not copy
9629 characters that yield more columns than PRECISION; PRECISION <= 0
9630 means copy the whole string. Pad with spaces until FIELD_WIDTH
9631 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9632 pad. Called from display_mode_element when it is used to build a
9633 frame title. */
9635 static int
9636 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9638 int n = 0;
9639 EMACS_INT dummy, nbytes;
9641 /* Copy at most PRECISION chars from STR. */
9642 nbytes = strlen (str);
9643 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9644 while (nbytes--)
9645 store_mode_line_noprop_char (*str++);
9647 /* Fill up with spaces until FIELD_WIDTH reached. */
9648 while (field_width > 0
9649 && n < field_width)
9651 store_mode_line_noprop_char (' ');
9652 ++n;
9655 return n;
9658 /***********************************************************************
9659 Frame Titles
9660 ***********************************************************************/
9662 #ifdef HAVE_WINDOW_SYSTEM
9664 /* Set the title of FRAME, if it has changed. The title format is
9665 Vicon_title_format if FRAME is iconified, otherwise it is
9666 frame_title_format. */
9668 static void
9669 x_consider_frame_title (Lisp_Object frame)
9671 struct frame *f = XFRAME (frame);
9673 if (FRAME_WINDOW_P (f)
9674 || FRAME_MINIBUF_ONLY_P (f)
9675 || f->explicit_name)
9677 /* Do we have more than one visible frame on this X display? */
9678 Lisp_Object tail;
9679 Lisp_Object fmt;
9680 int title_start;
9681 char *title;
9682 int len;
9683 struct it it;
9684 int count = SPECPDL_INDEX ();
9686 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9688 Lisp_Object other_frame = XCAR (tail);
9689 struct frame *tf = XFRAME (other_frame);
9691 if (tf != f
9692 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9693 && !FRAME_MINIBUF_ONLY_P (tf)
9694 && !EQ (other_frame, tip_frame)
9695 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9696 break;
9699 /* Set global variable indicating that multiple frames exist. */
9700 multiple_frames = CONSP (tail);
9702 /* Switch to the buffer of selected window of the frame. Set up
9703 mode_line_target so that display_mode_element will output into
9704 mode_line_noprop_buf; then display the title. */
9705 record_unwind_protect (unwind_format_mode_line,
9706 format_mode_line_unwind_data
9707 (current_buffer, selected_window, 0));
9709 Fselect_window (f->selected_window, Qt);
9710 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9711 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9713 mode_line_target = MODE_LINE_TITLE;
9714 title_start = MODE_LINE_NOPROP_LEN (0);
9715 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9716 NULL, DEFAULT_FACE_ID);
9717 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9718 len = MODE_LINE_NOPROP_LEN (title_start);
9719 title = mode_line_noprop_buf + title_start;
9720 unbind_to (count, Qnil);
9722 /* Set the title only if it's changed. This avoids consing in
9723 the common case where it hasn't. (If it turns out that we've
9724 already wasted too much time by walking through the list with
9725 display_mode_element, then we might need to optimize at a
9726 higher level than this.) */
9727 if (! STRINGP (f->name)
9728 || SBYTES (f->name) != len
9729 || memcmp (title, SDATA (f->name), len) != 0)
9730 x_implicitly_set_name (f, make_string (title, len), Qnil);
9734 #endif /* not HAVE_WINDOW_SYSTEM */
9739 /***********************************************************************
9740 Menu Bars
9741 ***********************************************************************/
9744 /* Prepare for redisplay by updating menu-bar item lists when
9745 appropriate. This can call eval. */
9747 void
9748 prepare_menu_bars (void)
9750 int all_windows;
9751 struct gcpro gcpro1, gcpro2;
9752 struct frame *f;
9753 Lisp_Object tooltip_frame;
9755 #ifdef HAVE_WINDOW_SYSTEM
9756 tooltip_frame = tip_frame;
9757 #else
9758 tooltip_frame = Qnil;
9759 #endif
9761 /* Update all frame titles based on their buffer names, etc. We do
9762 this before the menu bars so that the buffer-menu will show the
9763 up-to-date frame titles. */
9764 #ifdef HAVE_WINDOW_SYSTEM
9765 if (windows_or_buffers_changed || update_mode_lines)
9767 Lisp_Object tail, frame;
9769 FOR_EACH_FRAME (tail, frame)
9771 f = XFRAME (frame);
9772 if (!EQ (frame, tooltip_frame)
9773 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9774 x_consider_frame_title (frame);
9777 #endif /* HAVE_WINDOW_SYSTEM */
9779 /* Update the menu bar item lists, if appropriate. This has to be
9780 done before any actual redisplay or generation of display lines. */
9781 all_windows = (update_mode_lines
9782 || buffer_shared > 1
9783 || windows_or_buffers_changed);
9784 if (all_windows)
9786 Lisp_Object tail, frame;
9787 int count = SPECPDL_INDEX ();
9788 /* 1 means that update_menu_bar has run its hooks
9789 so any further calls to update_menu_bar shouldn't do so again. */
9790 int menu_bar_hooks_run = 0;
9792 record_unwind_save_match_data ();
9794 FOR_EACH_FRAME (tail, frame)
9796 f = XFRAME (frame);
9798 /* Ignore tooltip frame. */
9799 if (EQ (frame, tooltip_frame))
9800 continue;
9802 /* If a window on this frame changed size, report that to
9803 the user and clear the size-change flag. */
9804 if (FRAME_WINDOW_SIZES_CHANGED (f))
9806 Lisp_Object functions;
9808 /* Clear flag first in case we get an error below. */
9809 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9810 functions = Vwindow_size_change_functions;
9811 GCPRO2 (tail, functions);
9813 while (CONSP (functions))
9815 if (!EQ (XCAR (functions), Qt))
9816 call1 (XCAR (functions), frame);
9817 functions = XCDR (functions);
9819 UNGCPRO;
9822 GCPRO1 (tail);
9823 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9824 #ifdef HAVE_WINDOW_SYSTEM
9825 update_tool_bar (f, 0);
9826 #endif
9827 #ifdef HAVE_NS
9828 if (windows_or_buffers_changed
9829 && FRAME_NS_P (f))
9830 ns_set_doc_edited (f, Fbuffer_modified_p
9831 (XWINDOW (f->selected_window)->buffer));
9832 #endif
9833 UNGCPRO;
9836 unbind_to (count, Qnil);
9838 else
9840 struct frame *sf = SELECTED_FRAME ();
9841 update_menu_bar (sf, 1, 0);
9842 #ifdef HAVE_WINDOW_SYSTEM
9843 update_tool_bar (sf, 1);
9844 #endif
9849 /* Update the menu bar item list for frame F. This has to be done
9850 before we start to fill in any display lines, because it can call
9851 eval.
9853 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9855 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9856 already ran the menu bar hooks for this redisplay, so there
9857 is no need to run them again. The return value is the
9858 updated value of this flag, to pass to the next call. */
9860 static int
9861 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9863 Lisp_Object window;
9864 register struct window *w;
9866 /* If called recursively during a menu update, do nothing. This can
9867 happen when, for instance, an activate-menubar-hook causes a
9868 redisplay. */
9869 if (inhibit_menubar_update)
9870 return hooks_run;
9872 window = FRAME_SELECTED_WINDOW (f);
9873 w = XWINDOW (window);
9875 if (FRAME_WINDOW_P (f)
9877 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9878 || defined (HAVE_NS) || defined (USE_GTK)
9879 FRAME_EXTERNAL_MENU_BAR (f)
9880 #else
9881 FRAME_MENU_BAR_LINES (f) > 0
9882 #endif
9883 : FRAME_MENU_BAR_LINES (f) > 0)
9885 /* If the user has switched buffers or windows, we need to
9886 recompute to reflect the new bindings. But we'll
9887 recompute when update_mode_lines is set too; that means
9888 that people can use force-mode-line-update to request
9889 that the menu bar be recomputed. The adverse effect on
9890 the rest of the redisplay algorithm is about the same as
9891 windows_or_buffers_changed anyway. */
9892 if (windows_or_buffers_changed
9893 /* This used to test w->update_mode_line, but we believe
9894 there is no need to recompute the menu in that case. */
9895 || update_mode_lines
9896 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9897 < BUF_MODIFF (XBUFFER (w->buffer)))
9898 != !NILP (w->last_had_star))
9899 || ((!NILP (Vtransient_mark_mode)
9900 && !NILP (XBUFFER (w->buffer)->mark_active))
9901 != !NILP (w->region_showing)))
9903 struct buffer *prev = current_buffer;
9904 int count = SPECPDL_INDEX ();
9906 specbind (Qinhibit_menubar_update, Qt);
9908 set_buffer_internal_1 (XBUFFER (w->buffer));
9909 if (save_match_data)
9910 record_unwind_save_match_data ();
9911 if (NILP (Voverriding_local_map_menu_flag))
9913 specbind (Qoverriding_terminal_local_map, Qnil);
9914 specbind (Qoverriding_local_map, Qnil);
9917 if (!hooks_run)
9919 /* Run the Lucid hook. */
9920 safe_run_hooks (Qactivate_menubar_hook);
9922 /* If it has changed current-menubar from previous value,
9923 really recompute the menu-bar from the value. */
9924 if (! NILP (Vlucid_menu_bar_dirty_flag))
9925 call0 (Qrecompute_lucid_menubar);
9927 safe_run_hooks (Qmenu_bar_update_hook);
9929 hooks_run = 1;
9932 XSETFRAME (Vmenu_updating_frame, f);
9933 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9935 /* Redisplay the menu bar in case we changed it. */
9936 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9937 || defined (HAVE_NS) || defined (USE_GTK)
9938 if (FRAME_WINDOW_P (f))
9940 #if defined (HAVE_NS)
9941 /* All frames on Mac OS share the same menubar. So only
9942 the selected frame should be allowed to set it. */
9943 if (f == SELECTED_FRAME ())
9944 #endif
9945 set_frame_menubar (f, 0, 0);
9947 else
9948 /* On a terminal screen, the menu bar is an ordinary screen
9949 line, and this makes it get updated. */
9950 w->update_mode_line = Qt;
9951 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9952 /* In the non-toolkit version, the menu bar is an ordinary screen
9953 line, and this makes it get updated. */
9954 w->update_mode_line = Qt;
9955 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9957 unbind_to (count, Qnil);
9958 set_buffer_internal_1 (prev);
9962 return hooks_run;
9967 /***********************************************************************
9968 Output Cursor
9969 ***********************************************************************/
9971 #ifdef HAVE_WINDOW_SYSTEM
9973 /* EXPORT:
9974 Nominal cursor position -- where to draw output.
9975 HPOS and VPOS are window relative glyph matrix coordinates.
9976 X and Y are window relative pixel coordinates. */
9978 struct cursor_pos output_cursor;
9981 /* EXPORT:
9982 Set the global variable output_cursor to CURSOR. All cursor
9983 positions are relative to updated_window. */
9985 void
9986 set_output_cursor (struct cursor_pos *cursor)
9988 output_cursor.hpos = cursor->hpos;
9989 output_cursor.vpos = cursor->vpos;
9990 output_cursor.x = cursor->x;
9991 output_cursor.y = cursor->y;
9995 /* EXPORT for RIF:
9996 Set a nominal cursor position.
9998 HPOS and VPOS are column/row positions in a window glyph matrix. X
9999 and Y are window text area relative pixel positions.
10001 If this is done during an update, updated_window will contain the
10002 window that is being updated and the position is the future output
10003 cursor position for that window. If updated_window is null, use
10004 selected_window and display the cursor at the given position. */
10006 void
10007 x_cursor_to (int vpos, int hpos, int y, int x)
10009 struct window *w;
10011 /* If updated_window is not set, work on selected_window. */
10012 if (updated_window)
10013 w = updated_window;
10014 else
10015 w = XWINDOW (selected_window);
10017 /* Set the output cursor. */
10018 output_cursor.hpos = hpos;
10019 output_cursor.vpos = vpos;
10020 output_cursor.x = x;
10021 output_cursor.y = y;
10023 /* If not called as part of an update, really display the cursor.
10024 This will also set the cursor position of W. */
10025 if (updated_window == NULL)
10027 BLOCK_INPUT;
10028 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10029 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10030 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10031 UNBLOCK_INPUT;
10035 #endif /* HAVE_WINDOW_SYSTEM */
10038 /***********************************************************************
10039 Tool-bars
10040 ***********************************************************************/
10042 #ifdef HAVE_WINDOW_SYSTEM
10044 /* Where the mouse was last time we reported a mouse event. */
10046 FRAME_PTR last_mouse_frame;
10048 /* Tool-bar item index of the item on which a mouse button was pressed
10049 or -1. */
10051 int last_tool_bar_item;
10054 static Lisp_Object
10055 update_tool_bar_unwind (Lisp_Object frame)
10057 selected_frame = frame;
10058 return Qnil;
10061 /* Update the tool-bar item list for frame F. This has to be done
10062 before we start to fill in any display lines. Called from
10063 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10064 and restore it here. */
10066 static void
10067 update_tool_bar (struct frame *f, int save_match_data)
10069 #if defined (USE_GTK) || defined (HAVE_NS)
10070 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10071 #else
10072 int do_update = WINDOWP (f->tool_bar_window)
10073 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10074 #endif
10076 if (do_update)
10078 Lisp_Object window;
10079 struct window *w;
10081 window = FRAME_SELECTED_WINDOW (f);
10082 w = XWINDOW (window);
10084 /* If the user has switched buffers or windows, we need to
10085 recompute to reflect the new bindings. But we'll
10086 recompute when update_mode_lines is set too; that means
10087 that people can use force-mode-line-update to request
10088 that the menu bar be recomputed. The adverse effect on
10089 the rest of the redisplay algorithm is about the same as
10090 windows_or_buffers_changed anyway. */
10091 if (windows_or_buffers_changed
10092 || !NILP (w->update_mode_line)
10093 || update_mode_lines
10094 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10095 < BUF_MODIFF (XBUFFER (w->buffer)))
10096 != !NILP (w->last_had_star))
10097 || ((!NILP (Vtransient_mark_mode)
10098 && !NILP (XBUFFER (w->buffer)->mark_active))
10099 != !NILP (w->region_showing)))
10101 struct buffer *prev = current_buffer;
10102 int count = SPECPDL_INDEX ();
10103 Lisp_Object frame, new_tool_bar;
10104 int new_n_tool_bar;
10105 struct gcpro gcpro1;
10107 /* Set current_buffer to the buffer of the selected
10108 window of the frame, so that we get the right local
10109 keymaps. */
10110 set_buffer_internal_1 (XBUFFER (w->buffer));
10112 /* Save match data, if we must. */
10113 if (save_match_data)
10114 record_unwind_save_match_data ();
10116 /* Make sure that we don't accidentally use bogus keymaps. */
10117 if (NILP (Voverriding_local_map_menu_flag))
10119 specbind (Qoverriding_terminal_local_map, Qnil);
10120 specbind (Qoverriding_local_map, Qnil);
10123 GCPRO1 (new_tool_bar);
10125 /* We must temporarily set the selected frame to this frame
10126 before calling tool_bar_items, because the calculation of
10127 the tool-bar keymap uses the selected frame (see
10128 `tool-bar-make-keymap' in tool-bar.el). */
10129 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10130 XSETFRAME (frame, f);
10131 selected_frame = frame;
10133 /* Build desired tool-bar items from keymaps. */
10134 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10135 &new_n_tool_bar);
10137 /* Redisplay the tool-bar if we changed it. */
10138 if (new_n_tool_bar != f->n_tool_bar_items
10139 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10141 /* Redisplay that happens asynchronously due to an expose event
10142 may access f->tool_bar_items. Make sure we update both
10143 variables within BLOCK_INPUT so no such event interrupts. */
10144 BLOCK_INPUT;
10145 f->tool_bar_items = new_tool_bar;
10146 f->n_tool_bar_items = new_n_tool_bar;
10147 w->update_mode_line = Qt;
10148 UNBLOCK_INPUT;
10151 UNGCPRO;
10153 unbind_to (count, Qnil);
10154 set_buffer_internal_1 (prev);
10160 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10161 F's desired tool-bar contents. F->tool_bar_items must have
10162 been set up previously by calling prepare_menu_bars. */
10164 static void
10165 build_desired_tool_bar_string (struct frame *f)
10167 int i, size, size_needed;
10168 struct gcpro gcpro1, gcpro2, gcpro3;
10169 Lisp_Object image, plist, props;
10171 image = plist = props = Qnil;
10172 GCPRO3 (image, plist, props);
10174 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10175 Otherwise, make a new string. */
10177 /* The size of the string we might be able to reuse. */
10178 size = (STRINGP (f->desired_tool_bar_string)
10179 ? SCHARS (f->desired_tool_bar_string)
10180 : 0);
10182 /* We need one space in the string for each image. */
10183 size_needed = f->n_tool_bar_items;
10185 /* Reuse f->desired_tool_bar_string, if possible. */
10186 if (size < size_needed || NILP (f->desired_tool_bar_string))
10187 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10188 make_number (' '));
10189 else
10191 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10192 Fremove_text_properties (make_number (0), make_number (size),
10193 props, f->desired_tool_bar_string);
10196 /* Put a `display' property on the string for the images to display,
10197 put a `menu_item' property on tool-bar items with a value that
10198 is the index of the item in F's tool-bar item vector. */
10199 for (i = 0; i < f->n_tool_bar_items; ++i)
10201 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10203 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10204 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10205 int hmargin, vmargin, relief, idx, end;
10207 /* If image is a vector, choose the image according to the
10208 button state. */
10209 image = PROP (TOOL_BAR_ITEM_IMAGES);
10210 if (VECTORP (image))
10212 if (enabled_p)
10213 idx = (selected_p
10214 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10215 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10216 else
10217 idx = (selected_p
10218 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10219 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10221 xassert (ASIZE (image) >= idx);
10222 image = AREF (image, idx);
10224 else
10225 idx = -1;
10227 /* Ignore invalid image specifications. */
10228 if (!valid_image_p (image))
10229 continue;
10231 /* Display the tool-bar button pressed, or depressed. */
10232 plist = Fcopy_sequence (XCDR (image));
10234 /* Compute margin and relief to draw. */
10235 relief = (tool_bar_button_relief >= 0
10236 ? tool_bar_button_relief
10237 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10238 hmargin = vmargin = relief;
10240 if (INTEGERP (Vtool_bar_button_margin)
10241 && XINT (Vtool_bar_button_margin) > 0)
10243 hmargin += XFASTINT (Vtool_bar_button_margin);
10244 vmargin += XFASTINT (Vtool_bar_button_margin);
10246 else if (CONSP (Vtool_bar_button_margin))
10248 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10249 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10250 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10252 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10253 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10254 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10257 if (auto_raise_tool_bar_buttons_p)
10259 /* Add a `:relief' property to the image spec if the item is
10260 selected. */
10261 if (selected_p)
10263 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10264 hmargin -= relief;
10265 vmargin -= relief;
10268 else
10270 /* If image is selected, display it pressed, i.e. with a
10271 negative relief. If it's not selected, display it with a
10272 raised relief. */
10273 plist = Fplist_put (plist, QCrelief,
10274 (selected_p
10275 ? make_number (-relief)
10276 : make_number (relief)));
10277 hmargin -= relief;
10278 vmargin -= relief;
10281 /* Put a margin around the image. */
10282 if (hmargin || vmargin)
10284 if (hmargin == vmargin)
10285 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10286 else
10287 plist = Fplist_put (plist, QCmargin,
10288 Fcons (make_number (hmargin),
10289 make_number (vmargin)));
10292 /* If button is not enabled, and we don't have special images
10293 for the disabled state, make the image appear disabled by
10294 applying an appropriate algorithm to it. */
10295 if (!enabled_p && idx < 0)
10296 plist = Fplist_put (plist, QCconversion, Qdisabled);
10298 /* Put a `display' text property on the string for the image to
10299 display. Put a `menu-item' property on the string that gives
10300 the start of this item's properties in the tool-bar items
10301 vector. */
10302 image = Fcons (Qimage, plist);
10303 props = list4 (Qdisplay, image,
10304 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10306 /* Let the last image hide all remaining spaces in the tool bar
10307 string. The string can be longer than needed when we reuse a
10308 previous string. */
10309 if (i + 1 == f->n_tool_bar_items)
10310 end = SCHARS (f->desired_tool_bar_string);
10311 else
10312 end = i + 1;
10313 Fadd_text_properties (make_number (i), make_number (end),
10314 props, f->desired_tool_bar_string);
10315 #undef PROP
10318 UNGCPRO;
10322 /* Display one line of the tool-bar of frame IT->f.
10324 HEIGHT specifies the desired height of the tool-bar line.
10325 If the actual height of the glyph row is less than HEIGHT, the
10326 row's height is increased to HEIGHT, and the icons are centered
10327 vertically in the new height.
10329 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10330 count a final empty row in case the tool-bar width exactly matches
10331 the window width.
10334 static void
10335 display_tool_bar_line (struct it *it, int height)
10337 struct glyph_row *row = it->glyph_row;
10338 int max_x = it->last_visible_x;
10339 struct glyph *last;
10341 prepare_desired_row (row);
10342 row->y = it->current_y;
10344 /* Note that this isn't made use of if the face hasn't a box,
10345 so there's no need to check the face here. */
10346 it->start_of_box_run_p = 1;
10348 while (it->current_x < max_x)
10350 int x, n_glyphs_before, i, nglyphs;
10351 struct it it_before;
10353 /* Get the next display element. */
10354 if (!get_next_display_element (it))
10356 /* Don't count empty row if we are counting needed tool-bar lines. */
10357 if (height < 0 && !it->hpos)
10358 return;
10359 break;
10362 /* Produce glyphs. */
10363 n_glyphs_before = row->used[TEXT_AREA];
10364 it_before = *it;
10366 PRODUCE_GLYPHS (it);
10368 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10369 i = 0;
10370 x = it_before.current_x;
10371 while (i < nglyphs)
10373 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10375 if (x + glyph->pixel_width > max_x)
10377 /* Glyph doesn't fit on line. Backtrack. */
10378 row->used[TEXT_AREA] = n_glyphs_before;
10379 *it = it_before;
10380 /* If this is the only glyph on this line, it will never fit on the
10381 toolbar, so skip it. But ensure there is at least one glyph,
10382 so we don't accidentally disable the tool-bar. */
10383 if (n_glyphs_before == 0
10384 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10385 break;
10386 goto out;
10389 ++it->hpos;
10390 x += glyph->pixel_width;
10391 ++i;
10394 /* Stop at line ends. */
10395 if (ITERATOR_AT_END_OF_LINE_P (it))
10396 break;
10398 set_iterator_to_next (it, 1);
10401 out:;
10403 row->displays_text_p = row->used[TEXT_AREA] != 0;
10405 /* Use default face for the border below the tool bar.
10407 FIXME: When auto-resize-tool-bars is grow-only, there is
10408 no additional border below the possibly empty tool-bar lines.
10409 So to make the extra empty lines look "normal", we have to
10410 use the tool-bar face for the border too. */
10411 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10412 it->face_id = DEFAULT_FACE_ID;
10414 extend_face_to_end_of_line (it);
10415 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10416 last->right_box_line_p = 1;
10417 if (last == row->glyphs[TEXT_AREA])
10418 last->left_box_line_p = 1;
10420 /* Make line the desired height and center it vertically. */
10421 if ((height -= it->max_ascent + it->max_descent) > 0)
10423 /* Don't add more than one line height. */
10424 height %= FRAME_LINE_HEIGHT (it->f);
10425 it->max_ascent += height / 2;
10426 it->max_descent += (height + 1) / 2;
10429 compute_line_metrics (it);
10431 /* If line is empty, make it occupy the rest of the tool-bar. */
10432 if (!row->displays_text_p)
10434 row->height = row->phys_height = it->last_visible_y - row->y;
10435 row->visible_height = row->height;
10436 row->ascent = row->phys_ascent = 0;
10437 row->extra_line_spacing = 0;
10440 row->full_width_p = 1;
10441 row->continued_p = 0;
10442 row->truncated_on_left_p = 0;
10443 row->truncated_on_right_p = 0;
10445 it->current_x = it->hpos = 0;
10446 it->current_y += row->height;
10447 ++it->vpos;
10448 ++it->glyph_row;
10452 /* Max tool-bar height. */
10454 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10455 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10457 /* Value is the number of screen lines needed to make all tool-bar
10458 items of frame F visible. The number of actual rows needed is
10459 returned in *N_ROWS if non-NULL. */
10461 static int
10462 tool_bar_lines_needed (struct frame *f, int *n_rows)
10464 struct window *w = XWINDOW (f->tool_bar_window);
10465 struct it it;
10466 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10467 the desired matrix, so use (unused) mode-line row as temporary row to
10468 avoid destroying the first tool-bar row. */
10469 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10471 /* Initialize an iterator for iteration over
10472 F->desired_tool_bar_string in the tool-bar window of frame F. */
10473 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10474 it.first_visible_x = 0;
10475 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10476 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10478 while (!ITERATOR_AT_END_P (&it))
10480 clear_glyph_row (temp_row);
10481 it.glyph_row = temp_row;
10482 display_tool_bar_line (&it, -1);
10484 clear_glyph_row (temp_row);
10486 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10487 if (n_rows)
10488 *n_rows = it.vpos > 0 ? it.vpos : -1;
10490 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10494 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10495 0, 1, 0,
10496 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10497 (Lisp_Object frame)
10499 struct frame *f;
10500 struct window *w;
10501 int nlines = 0;
10503 if (NILP (frame))
10504 frame = selected_frame;
10505 else
10506 CHECK_FRAME (frame);
10507 f = XFRAME (frame);
10509 if (WINDOWP (f->tool_bar_window)
10510 || (w = XWINDOW (f->tool_bar_window),
10511 WINDOW_TOTAL_LINES (w) > 0))
10513 update_tool_bar (f, 1);
10514 if (f->n_tool_bar_items)
10516 build_desired_tool_bar_string (f);
10517 nlines = tool_bar_lines_needed (f, NULL);
10521 return make_number (nlines);
10525 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10526 height should be changed. */
10528 static int
10529 redisplay_tool_bar (struct frame *f)
10531 struct window *w;
10532 struct it it;
10533 struct glyph_row *row;
10535 #if defined (USE_GTK) || defined (HAVE_NS)
10536 if (FRAME_EXTERNAL_TOOL_BAR (f))
10537 update_frame_tool_bar (f);
10538 return 0;
10539 #endif
10541 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10542 do anything. This means you must start with tool-bar-lines
10543 non-zero to get the auto-sizing effect. Or in other words, you
10544 can turn off tool-bars by specifying tool-bar-lines zero. */
10545 if (!WINDOWP (f->tool_bar_window)
10546 || (w = XWINDOW (f->tool_bar_window),
10547 WINDOW_TOTAL_LINES (w) == 0))
10548 return 0;
10550 /* Set up an iterator for the tool-bar window. */
10551 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10552 it.first_visible_x = 0;
10553 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10554 row = it.glyph_row;
10556 /* Build a string that represents the contents of the tool-bar. */
10557 build_desired_tool_bar_string (f);
10558 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10560 if (f->n_tool_bar_rows == 0)
10562 int nlines;
10564 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10565 nlines != WINDOW_TOTAL_LINES (w)))
10567 Lisp_Object frame;
10568 int old_height = WINDOW_TOTAL_LINES (w);
10570 XSETFRAME (frame, f);
10571 Fmodify_frame_parameters (frame,
10572 Fcons (Fcons (Qtool_bar_lines,
10573 make_number (nlines)),
10574 Qnil));
10575 if (WINDOW_TOTAL_LINES (w) != old_height)
10577 clear_glyph_matrix (w->desired_matrix);
10578 fonts_changed_p = 1;
10579 return 1;
10584 /* Display as many lines as needed to display all tool-bar items. */
10586 if (f->n_tool_bar_rows > 0)
10588 int border, rows, height, extra;
10590 if (INTEGERP (Vtool_bar_border))
10591 border = XINT (Vtool_bar_border);
10592 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10593 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10594 else if (EQ (Vtool_bar_border, Qborder_width))
10595 border = f->border_width;
10596 else
10597 border = 0;
10598 if (border < 0)
10599 border = 0;
10601 rows = f->n_tool_bar_rows;
10602 height = max (1, (it.last_visible_y - border) / rows);
10603 extra = it.last_visible_y - border - height * rows;
10605 while (it.current_y < it.last_visible_y)
10607 int h = 0;
10608 if (extra > 0 && rows-- > 0)
10610 h = (extra + rows - 1) / rows;
10611 extra -= h;
10613 display_tool_bar_line (&it, height + h);
10616 else
10618 while (it.current_y < it.last_visible_y)
10619 display_tool_bar_line (&it, 0);
10622 /* It doesn't make much sense to try scrolling in the tool-bar
10623 window, so don't do it. */
10624 w->desired_matrix->no_scrolling_p = 1;
10625 w->must_be_updated_p = 1;
10627 if (!NILP (Vauto_resize_tool_bars))
10629 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10630 int change_height_p = 0;
10632 /* If we couldn't display everything, change the tool-bar's
10633 height if there is room for more. */
10634 if (IT_STRING_CHARPOS (it) < it.end_charpos
10635 && it.current_y < max_tool_bar_height)
10636 change_height_p = 1;
10638 row = it.glyph_row - 1;
10640 /* If there are blank lines at the end, except for a partially
10641 visible blank line at the end that is smaller than
10642 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10643 if (!row->displays_text_p
10644 && row->height >= FRAME_LINE_HEIGHT (f))
10645 change_height_p = 1;
10647 /* If row displays tool-bar items, but is partially visible,
10648 change the tool-bar's height. */
10649 if (row->displays_text_p
10650 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10651 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10652 change_height_p = 1;
10654 /* Resize windows as needed by changing the `tool-bar-lines'
10655 frame parameter. */
10656 if (change_height_p)
10658 Lisp_Object frame;
10659 int old_height = WINDOW_TOTAL_LINES (w);
10660 int nrows;
10661 int nlines = tool_bar_lines_needed (f, &nrows);
10663 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10664 && !f->minimize_tool_bar_window_p)
10665 ? (nlines > old_height)
10666 : (nlines != old_height));
10667 f->minimize_tool_bar_window_p = 0;
10669 if (change_height_p)
10671 XSETFRAME (frame, f);
10672 Fmodify_frame_parameters (frame,
10673 Fcons (Fcons (Qtool_bar_lines,
10674 make_number (nlines)),
10675 Qnil));
10676 if (WINDOW_TOTAL_LINES (w) != old_height)
10678 clear_glyph_matrix (w->desired_matrix);
10679 f->n_tool_bar_rows = nrows;
10680 fonts_changed_p = 1;
10681 return 1;
10687 f->minimize_tool_bar_window_p = 0;
10688 return 0;
10692 /* Get information about the tool-bar item which is displayed in GLYPH
10693 on frame F. Return in *PROP_IDX the index where tool-bar item
10694 properties start in F->tool_bar_items. Value is zero if
10695 GLYPH doesn't display a tool-bar item. */
10697 static int
10698 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10700 Lisp_Object prop;
10701 int success_p;
10702 int charpos;
10704 /* This function can be called asynchronously, which means we must
10705 exclude any possibility that Fget_text_property signals an
10706 error. */
10707 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10708 charpos = max (0, charpos);
10710 /* Get the text property `menu-item' at pos. The value of that
10711 property is the start index of this item's properties in
10712 F->tool_bar_items. */
10713 prop = Fget_text_property (make_number (charpos),
10714 Qmenu_item, f->current_tool_bar_string);
10715 if (INTEGERP (prop))
10717 *prop_idx = XINT (prop);
10718 success_p = 1;
10720 else
10721 success_p = 0;
10723 return success_p;
10727 /* Get information about the tool-bar item at position X/Y on frame F.
10728 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10729 the current matrix of the tool-bar window of F, or NULL if not
10730 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10731 item in F->tool_bar_items. Value is
10733 -1 if X/Y is not on a tool-bar item
10734 0 if X/Y is on the same item that was highlighted before.
10735 1 otherwise. */
10737 static int
10738 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10739 int *hpos, int *vpos, int *prop_idx)
10741 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10742 struct window *w = XWINDOW (f->tool_bar_window);
10743 int area;
10745 /* Find the glyph under X/Y. */
10746 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10747 if (*glyph == NULL)
10748 return -1;
10750 /* Get the start of this tool-bar item's properties in
10751 f->tool_bar_items. */
10752 if (!tool_bar_item_info (f, *glyph, prop_idx))
10753 return -1;
10755 /* Is mouse on the highlighted item? */
10756 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
10757 && *vpos >= hlinfo->mouse_face_beg_row
10758 && *vpos <= hlinfo->mouse_face_end_row
10759 && (*vpos > hlinfo->mouse_face_beg_row
10760 || *hpos >= hlinfo->mouse_face_beg_col)
10761 && (*vpos < hlinfo->mouse_face_end_row
10762 || *hpos < hlinfo->mouse_face_end_col
10763 || hlinfo->mouse_face_past_end))
10764 return 0;
10766 return 1;
10770 /* EXPORT:
10771 Handle mouse button event on the tool-bar of frame F, at
10772 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10773 0 for button release. MODIFIERS is event modifiers for button
10774 release. */
10776 void
10777 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10778 unsigned int modifiers)
10780 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10781 struct window *w = XWINDOW (f->tool_bar_window);
10782 int hpos, vpos, prop_idx;
10783 struct glyph *glyph;
10784 Lisp_Object enabled_p;
10786 /* If not on the highlighted tool-bar item, return. */
10787 frame_to_window_pixel_xy (w, &x, &y);
10788 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10789 return;
10791 /* If item is disabled, do nothing. */
10792 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10793 if (NILP (enabled_p))
10794 return;
10796 if (down_p)
10798 /* Show item in pressed state. */
10799 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
10800 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10801 last_tool_bar_item = prop_idx;
10803 else
10805 Lisp_Object key, frame;
10806 struct input_event event;
10807 EVENT_INIT (event);
10809 /* Show item in released state. */
10810 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
10811 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10813 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10815 XSETFRAME (frame, f);
10816 event.kind = TOOL_BAR_EVENT;
10817 event.frame_or_window = frame;
10818 event.arg = frame;
10819 kbd_buffer_store_event (&event);
10821 event.kind = TOOL_BAR_EVENT;
10822 event.frame_or_window = frame;
10823 event.arg = key;
10824 event.modifiers = modifiers;
10825 kbd_buffer_store_event (&event);
10826 last_tool_bar_item = -1;
10831 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10832 tool-bar window-relative coordinates X/Y. Called from
10833 note_mouse_highlight. */
10835 static void
10836 note_tool_bar_highlight (struct frame *f, int x, int y)
10838 Lisp_Object window = f->tool_bar_window;
10839 struct window *w = XWINDOW (window);
10840 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10841 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
10842 int hpos, vpos;
10843 struct glyph *glyph;
10844 struct glyph_row *row;
10845 int i;
10846 Lisp_Object enabled_p;
10847 int prop_idx;
10848 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10849 int mouse_down_p, rc;
10851 /* Function note_mouse_highlight is called with negative X/Y
10852 values when mouse moves outside of the frame. */
10853 if (x <= 0 || y <= 0)
10855 clear_mouse_face (hlinfo);
10856 return;
10859 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10860 if (rc < 0)
10862 /* Not on tool-bar item. */
10863 clear_mouse_face (hlinfo);
10864 return;
10866 else if (rc == 0)
10867 /* On same tool-bar item as before. */
10868 goto set_help_echo;
10870 clear_mouse_face (hlinfo);
10872 /* Mouse is down, but on different tool-bar item? */
10873 mouse_down_p = (dpyinfo->grabbed
10874 && f == last_mouse_frame
10875 && FRAME_LIVE_P (f));
10876 if (mouse_down_p
10877 && last_tool_bar_item != prop_idx)
10878 return;
10880 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10881 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10883 /* If tool-bar item is not enabled, don't highlight it. */
10884 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10885 if (!NILP (enabled_p))
10887 /* Compute the x-position of the glyph. In front and past the
10888 image is a space. We include this in the highlighted area. */
10889 row = MATRIX_ROW (w->current_matrix, vpos);
10890 for (i = x = 0; i < hpos; ++i)
10891 x += row->glyphs[TEXT_AREA][i].pixel_width;
10893 /* Record this as the current active region. */
10894 hlinfo->mouse_face_beg_col = hpos;
10895 hlinfo->mouse_face_beg_row = vpos;
10896 hlinfo->mouse_face_beg_x = x;
10897 hlinfo->mouse_face_beg_y = row->y;
10898 hlinfo->mouse_face_past_end = 0;
10900 hlinfo->mouse_face_end_col = hpos + 1;
10901 hlinfo->mouse_face_end_row = vpos;
10902 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
10903 hlinfo->mouse_face_end_y = row->y;
10904 hlinfo->mouse_face_window = window;
10905 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10907 /* Display it as active. */
10908 show_mouse_face (hlinfo, draw);
10909 hlinfo->mouse_face_image_state = draw;
10912 set_help_echo:
10914 /* Set help_echo_string to a help string to display for this tool-bar item.
10915 XTread_socket does the rest. */
10916 help_echo_object = help_echo_window = Qnil;
10917 help_echo_pos = -1;
10918 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10919 if (NILP (help_echo_string))
10920 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10923 #endif /* HAVE_WINDOW_SYSTEM */
10927 /************************************************************************
10928 Horizontal scrolling
10929 ************************************************************************/
10931 static int hscroll_window_tree (Lisp_Object);
10932 static int hscroll_windows (Lisp_Object);
10934 /* For all leaf windows in the window tree rooted at WINDOW, set their
10935 hscroll value so that PT is (i) visible in the window, and (ii) so
10936 that it is not within a certain margin at the window's left and
10937 right border. Value is non-zero if any window's hscroll has been
10938 changed. */
10940 static int
10941 hscroll_window_tree (Lisp_Object window)
10943 int hscrolled_p = 0;
10944 int hscroll_relative_p = FLOATP (Vhscroll_step);
10945 int hscroll_step_abs = 0;
10946 double hscroll_step_rel = 0;
10948 if (hscroll_relative_p)
10950 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10951 if (hscroll_step_rel < 0)
10953 hscroll_relative_p = 0;
10954 hscroll_step_abs = 0;
10957 else if (INTEGERP (Vhscroll_step))
10959 hscroll_step_abs = XINT (Vhscroll_step);
10960 if (hscroll_step_abs < 0)
10961 hscroll_step_abs = 0;
10963 else
10964 hscroll_step_abs = 0;
10966 while (WINDOWP (window))
10968 struct window *w = XWINDOW (window);
10970 if (WINDOWP (w->hchild))
10971 hscrolled_p |= hscroll_window_tree (w->hchild);
10972 else if (WINDOWP (w->vchild))
10973 hscrolled_p |= hscroll_window_tree (w->vchild);
10974 else if (w->cursor.vpos >= 0)
10976 int h_margin;
10977 int text_area_width;
10978 struct glyph_row *current_cursor_row
10979 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10980 struct glyph_row *desired_cursor_row
10981 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10982 struct glyph_row *cursor_row
10983 = (desired_cursor_row->enabled_p
10984 ? desired_cursor_row
10985 : current_cursor_row);
10987 text_area_width = window_box_width (w, TEXT_AREA);
10989 /* Scroll when cursor is inside this scroll margin. */
10990 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10992 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10993 && ((XFASTINT (w->hscroll)
10994 && w->cursor.x <= h_margin)
10995 || (cursor_row->enabled_p
10996 && cursor_row->truncated_on_right_p
10997 && (w->cursor.x >= text_area_width - h_margin))))
10999 struct it it;
11000 int hscroll;
11001 struct buffer *saved_current_buffer;
11002 EMACS_INT pt;
11003 int wanted_x;
11005 /* Find point in a display of infinite width. */
11006 saved_current_buffer = current_buffer;
11007 current_buffer = XBUFFER (w->buffer);
11009 if (w == XWINDOW (selected_window))
11010 pt = BUF_PT (current_buffer);
11011 else
11013 pt = marker_position (w->pointm);
11014 pt = max (BEGV, pt);
11015 pt = min (ZV, pt);
11018 /* Move iterator to pt starting at cursor_row->start in
11019 a line with infinite width. */
11020 init_to_row_start (&it, w, cursor_row);
11021 it.last_visible_x = INFINITY;
11022 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11023 current_buffer = saved_current_buffer;
11025 /* Position cursor in window. */
11026 if (!hscroll_relative_p && hscroll_step_abs == 0)
11027 hscroll = max (0, (it.current_x
11028 - (ITERATOR_AT_END_OF_LINE_P (&it)
11029 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11030 : (text_area_width / 2))))
11031 / FRAME_COLUMN_WIDTH (it.f);
11032 else if (w->cursor.x >= text_area_width - h_margin)
11034 if (hscroll_relative_p)
11035 wanted_x = text_area_width * (1 - hscroll_step_rel)
11036 - h_margin;
11037 else
11038 wanted_x = text_area_width
11039 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11040 - h_margin;
11041 hscroll
11042 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11044 else
11046 if (hscroll_relative_p)
11047 wanted_x = text_area_width * hscroll_step_rel
11048 + h_margin;
11049 else
11050 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11051 + h_margin;
11052 hscroll
11053 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11055 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11057 /* Don't call Fset_window_hscroll if value hasn't
11058 changed because it will prevent redisplay
11059 optimizations. */
11060 if (XFASTINT (w->hscroll) != hscroll)
11062 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11063 w->hscroll = make_number (hscroll);
11064 hscrolled_p = 1;
11069 window = w->next;
11072 /* Value is non-zero if hscroll of any leaf window has been changed. */
11073 return hscrolled_p;
11077 /* Set hscroll so that cursor is visible and not inside horizontal
11078 scroll margins for all windows in the tree rooted at WINDOW. See
11079 also hscroll_window_tree above. Value is non-zero if any window's
11080 hscroll has been changed. If it has, desired matrices on the frame
11081 of WINDOW are cleared. */
11083 static int
11084 hscroll_windows (Lisp_Object window)
11086 int hscrolled_p = hscroll_window_tree (window);
11087 if (hscrolled_p)
11088 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11089 return hscrolled_p;
11094 /************************************************************************
11095 Redisplay
11096 ************************************************************************/
11098 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11099 to a non-zero value. This is sometimes handy to have in a debugger
11100 session. */
11102 #if GLYPH_DEBUG
11104 /* First and last unchanged row for try_window_id. */
11106 int debug_first_unchanged_at_end_vpos;
11107 int debug_last_unchanged_at_beg_vpos;
11109 /* Delta vpos and y. */
11111 int debug_dvpos, debug_dy;
11113 /* Delta in characters and bytes for try_window_id. */
11115 EMACS_INT debug_delta, debug_delta_bytes;
11117 /* Values of window_end_pos and window_end_vpos at the end of
11118 try_window_id. */
11120 EMACS_INT debug_end_pos, debug_end_vpos;
11122 /* Append a string to W->desired_matrix->method. FMT is a printf
11123 format string. A1...A9 are a supplement for a variable-length
11124 argument list. If trace_redisplay_p is non-zero also printf the
11125 resulting string to stderr. */
11127 static void
11128 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11129 struct window *w;
11130 char *fmt;
11131 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11133 char buffer[512];
11134 char *method = w->desired_matrix->method;
11135 int len = strlen (method);
11136 int size = sizeof w->desired_matrix->method;
11137 int remaining = size - len - 1;
11139 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11140 if (len && remaining)
11142 method[len] = '|';
11143 --remaining, ++len;
11146 strncpy (method + len, buffer, remaining);
11148 if (trace_redisplay_p)
11149 fprintf (stderr, "%p (%s): %s\n",
11151 ((BUFFERP (w->buffer)
11152 && STRINGP (XBUFFER (w->buffer)->name))
11153 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11154 : "no buffer"),
11155 buffer);
11158 #endif /* GLYPH_DEBUG */
11161 /* Value is non-zero if all changes in window W, which displays
11162 current_buffer, are in the text between START and END. START is a
11163 buffer position, END is given as a distance from Z. Used in
11164 redisplay_internal for display optimization. */
11166 static INLINE int
11167 text_outside_line_unchanged_p (struct window *w,
11168 EMACS_INT start, EMACS_INT end)
11170 int unchanged_p = 1;
11172 /* If text or overlays have changed, see where. */
11173 if (XFASTINT (w->last_modified) < MODIFF
11174 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11176 /* Gap in the line? */
11177 if (GPT < start || Z - GPT < end)
11178 unchanged_p = 0;
11180 /* Changes start in front of the line, or end after it? */
11181 if (unchanged_p
11182 && (BEG_UNCHANGED < start - 1
11183 || END_UNCHANGED < end))
11184 unchanged_p = 0;
11186 /* If selective display, can't optimize if changes start at the
11187 beginning of the line. */
11188 if (unchanged_p
11189 && INTEGERP (current_buffer->selective_display)
11190 && XINT (current_buffer->selective_display) > 0
11191 && (BEG_UNCHANGED < start || GPT <= start))
11192 unchanged_p = 0;
11194 /* If there are overlays at the start or end of the line, these
11195 may have overlay strings with newlines in them. A change at
11196 START, for instance, may actually concern the display of such
11197 overlay strings as well, and they are displayed on different
11198 lines. So, quickly rule out this case. (For the future, it
11199 might be desirable to implement something more telling than
11200 just BEG/END_UNCHANGED.) */
11201 if (unchanged_p)
11203 if (BEG + BEG_UNCHANGED == start
11204 && overlay_touches_p (start))
11205 unchanged_p = 0;
11206 if (END_UNCHANGED == end
11207 && overlay_touches_p (Z - end))
11208 unchanged_p = 0;
11211 /* Under bidi reordering, adding or deleting a character in the
11212 beginning of a paragraph, before the first strong directional
11213 character, can change the base direction of the paragraph (unless
11214 the buffer specifies a fixed paragraph direction), which will
11215 require to redisplay the whole paragraph. It might be worthwhile
11216 to find the paragraph limits and widen the range of redisplayed
11217 lines to that, but for now just give up this optimization. */
11218 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11219 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11220 unchanged_p = 0;
11223 return unchanged_p;
11227 /* Do a frame update, taking possible shortcuts into account. This is
11228 the main external entry point for redisplay.
11230 If the last redisplay displayed an echo area message and that message
11231 is no longer requested, we clear the echo area or bring back the
11232 mini-buffer if that is in use. */
11234 void
11235 redisplay (void)
11237 redisplay_internal (0);
11241 static Lisp_Object
11242 overlay_arrow_string_or_property (Lisp_Object var)
11244 Lisp_Object val;
11246 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11247 return val;
11249 return Voverlay_arrow_string;
11252 /* Return 1 if there are any overlay-arrows in current_buffer. */
11253 static int
11254 overlay_arrow_in_current_buffer_p (void)
11256 Lisp_Object vlist;
11258 for (vlist = Voverlay_arrow_variable_list;
11259 CONSP (vlist);
11260 vlist = XCDR (vlist))
11262 Lisp_Object var = XCAR (vlist);
11263 Lisp_Object val;
11265 if (!SYMBOLP (var))
11266 continue;
11267 val = find_symbol_value (var);
11268 if (MARKERP (val)
11269 && current_buffer == XMARKER (val)->buffer)
11270 return 1;
11272 return 0;
11276 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11277 has changed. */
11279 static int
11280 overlay_arrows_changed_p (void)
11282 Lisp_Object vlist;
11284 for (vlist = Voverlay_arrow_variable_list;
11285 CONSP (vlist);
11286 vlist = XCDR (vlist))
11288 Lisp_Object var = XCAR (vlist);
11289 Lisp_Object val, pstr;
11291 if (!SYMBOLP (var))
11292 continue;
11293 val = find_symbol_value (var);
11294 if (!MARKERP (val))
11295 continue;
11296 if (! EQ (COERCE_MARKER (val),
11297 Fget (var, Qlast_arrow_position))
11298 || ! (pstr = overlay_arrow_string_or_property (var),
11299 EQ (pstr, Fget (var, Qlast_arrow_string))))
11300 return 1;
11302 return 0;
11305 /* Mark overlay arrows to be updated on next redisplay. */
11307 static void
11308 update_overlay_arrows (int up_to_date)
11310 Lisp_Object vlist;
11312 for (vlist = Voverlay_arrow_variable_list;
11313 CONSP (vlist);
11314 vlist = XCDR (vlist))
11316 Lisp_Object var = XCAR (vlist);
11318 if (!SYMBOLP (var))
11319 continue;
11321 if (up_to_date > 0)
11323 Lisp_Object val = find_symbol_value (var);
11324 Fput (var, Qlast_arrow_position,
11325 COERCE_MARKER (val));
11326 Fput (var, Qlast_arrow_string,
11327 overlay_arrow_string_or_property (var));
11329 else if (up_to_date < 0
11330 || !NILP (Fget (var, Qlast_arrow_position)))
11332 Fput (var, Qlast_arrow_position, Qt);
11333 Fput (var, Qlast_arrow_string, Qt);
11339 /* Return overlay arrow string to display at row.
11340 Return integer (bitmap number) for arrow bitmap in left fringe.
11341 Return nil if no overlay arrow. */
11343 static Lisp_Object
11344 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11346 Lisp_Object vlist;
11348 for (vlist = Voverlay_arrow_variable_list;
11349 CONSP (vlist);
11350 vlist = XCDR (vlist))
11352 Lisp_Object var = XCAR (vlist);
11353 Lisp_Object val;
11355 if (!SYMBOLP (var))
11356 continue;
11358 val = find_symbol_value (var);
11360 if (MARKERP (val)
11361 && current_buffer == XMARKER (val)->buffer
11362 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11364 if (FRAME_WINDOW_P (it->f)
11365 /* FIXME: if ROW->reversed_p is set, this should test
11366 the right fringe, not the left one. */
11367 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11369 #ifdef HAVE_WINDOW_SYSTEM
11370 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11372 int fringe_bitmap;
11373 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11374 return make_number (fringe_bitmap);
11376 #endif
11377 return make_number (-1); /* Use default arrow bitmap */
11379 return overlay_arrow_string_or_property (var);
11383 return Qnil;
11386 /* Return 1 if point moved out of or into a composition. Otherwise
11387 return 0. PREV_BUF and PREV_PT are the last point buffer and
11388 position. BUF and PT are the current point buffer and position. */
11391 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11392 struct buffer *buf, EMACS_INT pt)
11394 EMACS_INT start, end;
11395 Lisp_Object prop;
11396 Lisp_Object buffer;
11398 XSETBUFFER (buffer, buf);
11399 /* Check a composition at the last point if point moved within the
11400 same buffer. */
11401 if (prev_buf == buf)
11403 if (prev_pt == pt)
11404 /* Point didn't move. */
11405 return 0;
11407 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11408 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11409 && COMPOSITION_VALID_P (start, end, prop)
11410 && start < prev_pt && end > prev_pt)
11411 /* The last point was within the composition. Return 1 iff
11412 point moved out of the composition. */
11413 return (pt <= start || pt >= end);
11416 /* Check a composition at the current point. */
11417 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11418 && find_composition (pt, -1, &start, &end, &prop, buffer)
11419 && COMPOSITION_VALID_P (start, end, prop)
11420 && start < pt && end > pt);
11424 /* Reconsider the setting of B->clip_changed which is displayed
11425 in window W. */
11427 static INLINE void
11428 reconsider_clip_changes (struct window *w, struct buffer *b)
11430 if (b->clip_changed
11431 && !NILP (w->window_end_valid)
11432 && w->current_matrix->buffer == b
11433 && w->current_matrix->zv == BUF_ZV (b)
11434 && w->current_matrix->begv == BUF_BEGV (b))
11435 b->clip_changed = 0;
11437 /* If display wasn't paused, and W is not a tool bar window, see if
11438 point has been moved into or out of a composition. In that case,
11439 we set b->clip_changed to 1 to force updating the screen. If
11440 b->clip_changed has already been set to 1, we can skip this
11441 check. */
11442 if (!b->clip_changed
11443 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11445 EMACS_INT pt;
11447 if (w == XWINDOW (selected_window))
11448 pt = BUF_PT (current_buffer);
11449 else
11450 pt = marker_position (w->pointm);
11452 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11453 || pt != XINT (w->last_point))
11454 && check_point_in_composition (w->current_matrix->buffer,
11455 XINT (w->last_point),
11456 XBUFFER (w->buffer), pt))
11457 b->clip_changed = 1;
11462 /* Select FRAME to forward the values of frame-local variables into C
11463 variables so that the redisplay routines can access those values
11464 directly. */
11466 static void
11467 select_frame_for_redisplay (Lisp_Object frame)
11469 Lisp_Object tail, tem;
11470 Lisp_Object old = selected_frame;
11471 struct Lisp_Symbol *sym;
11473 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11475 selected_frame = frame;
11477 do {
11478 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11479 if (CONSP (XCAR (tail))
11480 && (tem = XCAR (XCAR (tail)),
11481 SYMBOLP (tem))
11482 && (sym = indirect_variable (XSYMBOL (tem)),
11483 sym->redirect == SYMBOL_LOCALIZED)
11484 && sym->val.blv->frame_local)
11485 /* Use find_symbol_value rather than Fsymbol_value
11486 to avoid an error if it is void. */
11487 find_symbol_value (tem);
11488 } while (!EQ (frame, old) && (frame = old, 1));
11492 #define STOP_POLLING \
11493 do { if (! polling_stopped_here) stop_polling (); \
11494 polling_stopped_here = 1; } while (0)
11496 #define RESUME_POLLING \
11497 do { if (polling_stopped_here) start_polling (); \
11498 polling_stopped_here = 0; } while (0)
11501 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11502 response to any user action; therefore, we should preserve the echo
11503 area. (Actually, our caller does that job.) Perhaps in the future
11504 avoid recentering windows if it is not necessary; currently that
11505 causes some problems. */
11507 static void
11508 redisplay_internal (int preserve_echo_area)
11510 struct window *w = XWINDOW (selected_window);
11511 struct frame *f;
11512 int pause;
11513 int must_finish = 0;
11514 struct text_pos tlbufpos, tlendpos;
11515 int number_of_visible_frames;
11516 int count, count1;
11517 struct frame *sf;
11518 int polling_stopped_here = 0;
11519 Lisp_Object old_frame = selected_frame;
11521 /* Non-zero means redisplay has to consider all windows on all
11522 frames. Zero means, only selected_window is considered. */
11523 int consider_all_windows_p;
11525 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11527 /* No redisplay if running in batch mode or frame is not yet fully
11528 initialized, or redisplay is explicitly turned off by setting
11529 Vinhibit_redisplay. */
11530 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11531 || !NILP (Vinhibit_redisplay))
11532 return;
11534 /* Don't examine these until after testing Vinhibit_redisplay.
11535 When Emacs is shutting down, perhaps because its connection to
11536 X has dropped, we should not look at them at all. */
11537 f = XFRAME (w->frame);
11538 sf = SELECTED_FRAME ();
11540 if (!f->glyphs_initialized_p)
11541 return;
11543 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11544 if (popup_activated ())
11545 return;
11546 #endif
11548 /* I don't think this happens but let's be paranoid. */
11549 if (redisplaying_p)
11550 return;
11552 /* Record a function that resets redisplaying_p to its old value
11553 when we leave this function. */
11554 count = SPECPDL_INDEX ();
11555 record_unwind_protect (unwind_redisplay,
11556 Fcons (make_number (redisplaying_p), selected_frame));
11557 ++redisplaying_p;
11558 specbind (Qinhibit_free_realized_faces, Qnil);
11561 Lisp_Object tail, frame;
11563 FOR_EACH_FRAME (tail, frame)
11565 struct frame *f = XFRAME (frame);
11566 f->already_hscrolled_p = 0;
11570 retry:
11571 if (!EQ (old_frame, selected_frame)
11572 && FRAME_LIVE_P (XFRAME (old_frame)))
11573 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11574 selected_frame and selected_window to be temporarily out-of-sync so
11575 when we come back here via `goto retry', we need to resync because we
11576 may need to run Elisp code (via prepare_menu_bars). */
11577 select_frame_for_redisplay (old_frame);
11579 pause = 0;
11580 reconsider_clip_changes (w, current_buffer);
11581 last_escape_glyph_frame = NULL;
11582 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11584 /* If new fonts have been loaded that make a glyph matrix adjustment
11585 necessary, do it. */
11586 if (fonts_changed_p)
11588 adjust_glyphs (NULL);
11589 ++windows_or_buffers_changed;
11590 fonts_changed_p = 0;
11593 /* If face_change_count is non-zero, init_iterator will free all
11594 realized faces, which includes the faces referenced from current
11595 matrices. So, we can't reuse current matrices in this case. */
11596 if (face_change_count)
11597 ++windows_or_buffers_changed;
11599 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11600 && FRAME_TTY (sf)->previous_frame != sf)
11602 /* Since frames on a single ASCII terminal share the same
11603 display area, displaying a different frame means redisplay
11604 the whole thing. */
11605 windows_or_buffers_changed++;
11606 SET_FRAME_GARBAGED (sf);
11607 #ifndef DOS_NT
11608 set_tty_color_mode (FRAME_TTY (sf), sf);
11609 #endif
11610 FRAME_TTY (sf)->previous_frame = sf;
11613 /* Set the visible flags for all frames. Do this before checking
11614 for resized or garbaged frames; they want to know if their frames
11615 are visible. See the comment in frame.h for
11616 FRAME_SAMPLE_VISIBILITY. */
11618 Lisp_Object tail, frame;
11620 number_of_visible_frames = 0;
11622 FOR_EACH_FRAME (tail, frame)
11624 struct frame *f = XFRAME (frame);
11626 FRAME_SAMPLE_VISIBILITY (f);
11627 if (FRAME_VISIBLE_P (f))
11628 ++number_of_visible_frames;
11629 clear_desired_matrices (f);
11633 /* Notice any pending interrupt request to change frame size. */
11634 do_pending_window_change (1);
11636 /* Clear frames marked as garbaged. */
11637 if (frame_garbaged)
11638 clear_garbaged_frames ();
11640 /* Build menubar and tool-bar items. */
11641 if (NILP (Vmemory_full))
11642 prepare_menu_bars ();
11644 if (windows_or_buffers_changed)
11645 update_mode_lines++;
11647 /* Detect case that we need to write or remove a star in the mode line. */
11648 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11650 w->update_mode_line = Qt;
11651 if (buffer_shared > 1)
11652 update_mode_lines++;
11655 /* Avoid invocation of point motion hooks by `current_column' below. */
11656 count1 = SPECPDL_INDEX ();
11657 specbind (Qinhibit_point_motion_hooks, Qt);
11659 /* If %c is in the mode line, update it if needed. */
11660 if (!NILP (w->column_number_displayed)
11661 /* This alternative quickly identifies a common case
11662 where no change is needed. */
11663 && !(PT == XFASTINT (w->last_point)
11664 && XFASTINT (w->last_modified) >= MODIFF
11665 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11666 && (XFASTINT (w->column_number_displayed)
11667 != (int) current_column ())) /* iftc */
11668 w->update_mode_line = Qt;
11670 unbind_to (count1, Qnil);
11672 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11674 /* The variable buffer_shared is set in redisplay_window and
11675 indicates that we redisplay a buffer in different windows. See
11676 there. */
11677 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11678 || cursor_type_changed);
11680 /* If specs for an arrow have changed, do thorough redisplay
11681 to ensure we remove any arrow that should no longer exist. */
11682 if (overlay_arrows_changed_p ())
11683 consider_all_windows_p = windows_or_buffers_changed = 1;
11685 /* Normally the message* functions will have already displayed and
11686 updated the echo area, but the frame may have been trashed, or
11687 the update may have been preempted, so display the echo area
11688 again here. Checking message_cleared_p captures the case that
11689 the echo area should be cleared. */
11690 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11691 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11692 || (message_cleared_p
11693 && minibuf_level == 0
11694 /* If the mini-window is currently selected, this means the
11695 echo-area doesn't show through. */
11696 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11698 int window_height_changed_p = echo_area_display (0);
11699 must_finish = 1;
11701 /* If we don't display the current message, don't clear the
11702 message_cleared_p flag, because, if we did, we wouldn't clear
11703 the echo area in the next redisplay which doesn't preserve
11704 the echo area. */
11705 if (!display_last_displayed_message_p)
11706 message_cleared_p = 0;
11708 if (fonts_changed_p)
11709 goto retry;
11710 else if (window_height_changed_p)
11712 consider_all_windows_p = 1;
11713 ++update_mode_lines;
11714 ++windows_or_buffers_changed;
11716 /* If window configuration was changed, frames may have been
11717 marked garbaged. Clear them or we will experience
11718 surprises wrt scrolling. */
11719 if (frame_garbaged)
11720 clear_garbaged_frames ();
11723 else if (EQ (selected_window, minibuf_window)
11724 && (current_buffer->clip_changed
11725 || XFASTINT (w->last_modified) < MODIFF
11726 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11727 && resize_mini_window (w, 0))
11729 /* Resized active mini-window to fit the size of what it is
11730 showing if its contents might have changed. */
11731 must_finish = 1;
11732 /* FIXME: this causes all frames to be updated, which seems unnecessary
11733 since only the current frame needs to be considered. This function needs
11734 to be rewritten with two variables, consider_all_windows and
11735 consider_all_frames. */
11736 consider_all_windows_p = 1;
11737 ++windows_or_buffers_changed;
11738 ++update_mode_lines;
11740 /* If window configuration was changed, frames may have been
11741 marked garbaged. Clear them or we will experience
11742 surprises wrt scrolling. */
11743 if (frame_garbaged)
11744 clear_garbaged_frames ();
11748 /* If showing the region, and mark has changed, we must redisplay
11749 the whole window. The assignment to this_line_start_pos prevents
11750 the optimization directly below this if-statement. */
11751 if (((!NILP (Vtransient_mark_mode)
11752 && !NILP (XBUFFER (w->buffer)->mark_active))
11753 != !NILP (w->region_showing))
11754 || (!NILP (w->region_showing)
11755 && !EQ (w->region_showing,
11756 Fmarker_position (XBUFFER (w->buffer)->mark))))
11757 CHARPOS (this_line_start_pos) = 0;
11759 /* Optimize the case that only the line containing the cursor in the
11760 selected window has changed. Variables starting with this_ are
11761 set in display_line and record information about the line
11762 containing the cursor. */
11763 tlbufpos = this_line_start_pos;
11764 tlendpos = this_line_end_pos;
11765 if (!consider_all_windows_p
11766 && CHARPOS (tlbufpos) > 0
11767 && NILP (w->update_mode_line)
11768 && !current_buffer->clip_changed
11769 && !current_buffer->prevent_redisplay_optimizations_p
11770 && FRAME_VISIBLE_P (XFRAME (w->frame))
11771 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11772 /* Make sure recorded data applies to current buffer, etc. */
11773 && this_line_buffer == current_buffer
11774 && current_buffer == XBUFFER (w->buffer)
11775 && NILP (w->force_start)
11776 && NILP (w->optional_new_start)
11777 /* Point must be on the line that we have info recorded about. */
11778 && PT >= CHARPOS (tlbufpos)
11779 && PT <= Z - CHARPOS (tlendpos)
11780 /* All text outside that line, including its final newline,
11781 must be unchanged. */
11782 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11783 CHARPOS (tlendpos)))
11785 if (CHARPOS (tlbufpos) > BEGV
11786 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11787 && (CHARPOS (tlbufpos) == ZV
11788 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11789 /* Former continuation line has disappeared by becoming empty. */
11790 goto cancel;
11791 else if (XFASTINT (w->last_modified) < MODIFF
11792 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11793 || MINI_WINDOW_P (w))
11795 /* We have to handle the case of continuation around a
11796 wide-column character (see the comment in indent.c around
11797 line 1340).
11799 For instance, in the following case:
11801 -------- Insert --------
11802 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11803 J_I_ ==> J_I_ `^^' are cursors.
11804 ^^ ^^
11805 -------- --------
11807 As we have to redraw the line above, we cannot use this
11808 optimization. */
11810 struct it it;
11811 int line_height_before = this_line_pixel_height;
11813 /* Note that start_display will handle the case that the
11814 line starting at tlbufpos is a continuation line. */
11815 start_display (&it, w, tlbufpos);
11817 /* Implementation note: It this still necessary? */
11818 if (it.current_x != this_line_start_x)
11819 goto cancel;
11821 TRACE ((stderr, "trying display optimization 1\n"));
11822 w->cursor.vpos = -1;
11823 overlay_arrow_seen = 0;
11824 it.vpos = this_line_vpos;
11825 it.current_y = this_line_y;
11826 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11827 display_line (&it);
11829 /* If line contains point, is not continued,
11830 and ends at same distance from eob as before, we win. */
11831 if (w->cursor.vpos >= 0
11832 /* Line is not continued, otherwise this_line_start_pos
11833 would have been set to 0 in display_line. */
11834 && CHARPOS (this_line_start_pos)
11835 /* Line ends as before. */
11836 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11837 /* Line has same height as before. Otherwise other lines
11838 would have to be shifted up or down. */
11839 && this_line_pixel_height == line_height_before)
11841 /* If this is not the window's last line, we must adjust
11842 the charstarts of the lines below. */
11843 if (it.current_y < it.last_visible_y)
11845 struct glyph_row *row
11846 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11847 EMACS_INT delta, delta_bytes;
11849 /* We used to distinguish between two cases here,
11850 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11851 when the line ends in a newline or the end of the
11852 buffer's accessible portion. But both cases did
11853 the same, so they were collapsed. */
11854 delta = (Z
11855 - CHARPOS (tlendpos)
11856 - MATRIX_ROW_START_CHARPOS (row));
11857 delta_bytes = (Z_BYTE
11858 - BYTEPOS (tlendpos)
11859 - MATRIX_ROW_START_BYTEPOS (row));
11861 increment_matrix_positions (w->current_matrix,
11862 this_line_vpos + 1,
11863 w->current_matrix->nrows,
11864 delta, delta_bytes);
11867 /* If this row displays text now but previously didn't,
11868 or vice versa, w->window_end_vpos may have to be
11869 adjusted. */
11870 if ((it.glyph_row - 1)->displays_text_p)
11872 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11873 XSETINT (w->window_end_vpos, this_line_vpos);
11875 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11876 && this_line_vpos > 0)
11877 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11878 w->window_end_valid = Qnil;
11880 /* Update hint: No need to try to scroll in update_window. */
11881 w->desired_matrix->no_scrolling_p = 1;
11883 #if GLYPH_DEBUG
11884 *w->desired_matrix->method = 0;
11885 debug_method_add (w, "optimization 1");
11886 #endif
11887 #ifdef HAVE_WINDOW_SYSTEM
11888 update_window_fringes (w, 0);
11889 #endif
11890 goto update;
11892 else
11893 goto cancel;
11895 else if (/* Cursor position hasn't changed. */
11896 PT == XFASTINT (w->last_point)
11897 /* Make sure the cursor was last displayed
11898 in this window. Otherwise we have to reposition it. */
11899 && 0 <= w->cursor.vpos
11900 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11902 if (!must_finish)
11904 do_pending_window_change (1);
11906 /* We used to always goto end_of_redisplay here, but this
11907 isn't enough if we have a blinking cursor. */
11908 if (w->cursor_off_p == w->last_cursor_off_p)
11909 goto end_of_redisplay;
11911 goto update;
11913 /* If highlighting the region, or if the cursor is in the echo area,
11914 then we can't just move the cursor. */
11915 else if (! (!NILP (Vtransient_mark_mode)
11916 && !NILP (current_buffer->mark_active))
11917 && (EQ (selected_window, current_buffer->last_selected_window)
11918 || highlight_nonselected_windows)
11919 && NILP (w->region_showing)
11920 && NILP (Vshow_trailing_whitespace)
11921 && !cursor_in_echo_area)
11923 struct it it;
11924 struct glyph_row *row;
11926 /* Skip from tlbufpos to PT and see where it is. Note that
11927 PT may be in invisible text. If so, we will end at the
11928 next visible position. */
11929 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11930 NULL, DEFAULT_FACE_ID);
11931 it.current_x = this_line_start_x;
11932 it.current_y = this_line_y;
11933 it.vpos = this_line_vpos;
11935 /* The call to move_it_to stops in front of PT, but
11936 moves over before-strings. */
11937 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11939 if (it.vpos == this_line_vpos
11940 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11941 row->enabled_p))
11943 xassert (this_line_vpos == it.vpos);
11944 xassert (this_line_y == it.current_y);
11945 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11946 #if GLYPH_DEBUG
11947 *w->desired_matrix->method = 0;
11948 debug_method_add (w, "optimization 3");
11949 #endif
11950 goto update;
11952 else
11953 goto cancel;
11956 cancel:
11957 /* Text changed drastically or point moved off of line. */
11958 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11961 CHARPOS (this_line_start_pos) = 0;
11962 consider_all_windows_p |= buffer_shared > 1;
11963 ++clear_face_cache_count;
11964 #ifdef HAVE_WINDOW_SYSTEM
11965 ++clear_image_cache_count;
11966 #endif
11968 /* Build desired matrices, and update the display. If
11969 consider_all_windows_p is non-zero, do it for all windows on all
11970 frames. Otherwise do it for selected_window, only. */
11972 if (consider_all_windows_p)
11974 Lisp_Object tail, frame;
11976 FOR_EACH_FRAME (tail, frame)
11977 XFRAME (frame)->updated_p = 0;
11979 /* Recompute # windows showing selected buffer. This will be
11980 incremented each time such a window is displayed. */
11981 buffer_shared = 0;
11983 FOR_EACH_FRAME (tail, frame)
11985 struct frame *f = XFRAME (frame);
11987 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11989 if (! EQ (frame, selected_frame))
11990 /* Select the frame, for the sake of frame-local
11991 variables. */
11992 select_frame_for_redisplay (frame);
11994 /* Mark all the scroll bars to be removed; we'll redeem
11995 the ones we want when we redisplay their windows. */
11996 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11997 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11999 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12000 redisplay_windows (FRAME_ROOT_WINDOW (f));
12002 /* The X error handler may have deleted that frame. */
12003 if (!FRAME_LIVE_P (f))
12004 continue;
12006 /* Any scroll bars which redisplay_windows should have
12007 nuked should now go away. */
12008 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12009 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12011 /* If fonts changed, display again. */
12012 /* ??? rms: I suspect it is a mistake to jump all the way
12013 back to retry here. It should just retry this frame. */
12014 if (fonts_changed_p)
12015 goto retry;
12017 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12019 /* See if we have to hscroll. */
12020 if (!f->already_hscrolled_p)
12022 f->already_hscrolled_p = 1;
12023 if (hscroll_windows (f->root_window))
12024 goto retry;
12027 /* Prevent various kinds of signals during display
12028 update. stdio is not robust about handling
12029 signals, which can cause an apparent I/O
12030 error. */
12031 if (interrupt_input)
12032 unrequest_sigio ();
12033 STOP_POLLING;
12035 /* Update the display. */
12036 set_window_update_flags (XWINDOW (f->root_window), 1);
12037 pause |= update_frame (f, 0, 0);
12038 f->updated_p = 1;
12043 if (!EQ (old_frame, selected_frame)
12044 && FRAME_LIVE_P (XFRAME (old_frame)))
12045 /* We played a bit fast-and-loose above and allowed selected_frame
12046 and selected_window to be temporarily out-of-sync but let's make
12047 sure this stays contained. */
12048 select_frame_for_redisplay (old_frame);
12049 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12051 if (!pause)
12053 /* Do the mark_window_display_accurate after all windows have
12054 been redisplayed because this call resets flags in buffers
12055 which are needed for proper redisplay. */
12056 FOR_EACH_FRAME (tail, frame)
12058 struct frame *f = XFRAME (frame);
12059 if (f->updated_p)
12061 mark_window_display_accurate (f->root_window, 1);
12062 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12063 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12068 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12070 Lisp_Object mini_window;
12071 struct frame *mini_frame;
12073 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12074 /* Use list_of_error, not Qerror, so that
12075 we catch only errors and don't run the debugger. */
12076 internal_condition_case_1 (redisplay_window_1, selected_window,
12077 list_of_error,
12078 redisplay_window_error);
12080 /* Compare desired and current matrices, perform output. */
12082 update:
12083 /* If fonts changed, display again. */
12084 if (fonts_changed_p)
12085 goto retry;
12087 /* Prevent various kinds of signals during display update.
12088 stdio is not robust about handling signals,
12089 which can cause an apparent I/O error. */
12090 if (interrupt_input)
12091 unrequest_sigio ();
12092 STOP_POLLING;
12094 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12096 if (hscroll_windows (selected_window))
12097 goto retry;
12099 XWINDOW (selected_window)->must_be_updated_p = 1;
12100 pause = update_frame (sf, 0, 0);
12103 /* We may have called echo_area_display at the top of this
12104 function. If the echo area is on another frame, that may
12105 have put text on a frame other than the selected one, so the
12106 above call to update_frame would not have caught it. Catch
12107 it here. */
12108 mini_window = FRAME_MINIBUF_WINDOW (sf);
12109 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12111 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12113 XWINDOW (mini_window)->must_be_updated_p = 1;
12114 pause |= update_frame (mini_frame, 0, 0);
12115 if (!pause && hscroll_windows (mini_window))
12116 goto retry;
12120 /* If display was paused because of pending input, make sure we do a
12121 thorough update the next time. */
12122 if (pause)
12124 /* Prevent the optimization at the beginning of
12125 redisplay_internal that tries a single-line update of the
12126 line containing the cursor in the selected window. */
12127 CHARPOS (this_line_start_pos) = 0;
12129 /* Let the overlay arrow be updated the next time. */
12130 update_overlay_arrows (0);
12132 /* If we pause after scrolling, some rows in the current
12133 matrices of some windows are not valid. */
12134 if (!WINDOW_FULL_WIDTH_P (w)
12135 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12136 update_mode_lines = 1;
12138 else
12140 if (!consider_all_windows_p)
12142 /* This has already been done above if
12143 consider_all_windows_p is set. */
12144 mark_window_display_accurate_1 (w, 1);
12146 /* Say overlay arrows are up to date. */
12147 update_overlay_arrows (1);
12149 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12150 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12153 update_mode_lines = 0;
12154 windows_or_buffers_changed = 0;
12155 cursor_type_changed = 0;
12158 /* Start SIGIO interrupts coming again. Having them off during the
12159 code above makes it less likely one will discard output, but not
12160 impossible, since there might be stuff in the system buffer here.
12161 But it is much hairier to try to do anything about that. */
12162 if (interrupt_input)
12163 request_sigio ();
12164 RESUME_POLLING;
12166 /* If a frame has become visible which was not before, redisplay
12167 again, so that we display it. Expose events for such a frame
12168 (which it gets when becoming visible) don't call the parts of
12169 redisplay constructing glyphs, so simply exposing a frame won't
12170 display anything in this case. So, we have to display these
12171 frames here explicitly. */
12172 if (!pause)
12174 Lisp_Object tail, frame;
12175 int new_count = 0;
12177 FOR_EACH_FRAME (tail, frame)
12179 int this_is_visible = 0;
12181 if (XFRAME (frame)->visible)
12182 this_is_visible = 1;
12183 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12184 if (XFRAME (frame)->visible)
12185 this_is_visible = 1;
12187 if (this_is_visible)
12188 new_count++;
12191 if (new_count != number_of_visible_frames)
12192 windows_or_buffers_changed++;
12195 /* Change frame size now if a change is pending. */
12196 do_pending_window_change (1);
12198 /* If we just did a pending size change, or have additional
12199 visible frames, redisplay again. */
12200 if (windows_or_buffers_changed && !pause)
12201 goto retry;
12203 /* Clear the face and image caches.
12205 We used to do this only if consider_all_windows_p. But the cache
12206 needs to be cleared if a timer creates images in the current
12207 buffer (e.g. the test case in Bug#6230). */
12209 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12211 clear_face_cache (0);
12212 clear_face_cache_count = 0;
12215 #ifdef HAVE_WINDOW_SYSTEM
12216 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12218 clear_image_caches (Qnil);
12219 clear_image_cache_count = 0;
12221 #endif /* HAVE_WINDOW_SYSTEM */
12223 end_of_redisplay:
12224 unbind_to (count, Qnil);
12225 RESUME_POLLING;
12229 /* Redisplay, but leave alone any recent echo area message unless
12230 another message has been requested in its place.
12232 This is useful in situations where you need to redisplay but no
12233 user action has occurred, making it inappropriate for the message
12234 area to be cleared. See tracking_off and
12235 wait_reading_process_output for examples of these situations.
12237 FROM_WHERE is an integer saying from where this function was
12238 called. This is useful for debugging. */
12240 void
12241 redisplay_preserve_echo_area (int from_where)
12243 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12245 if (!NILP (echo_area_buffer[1]))
12247 /* We have a previously displayed message, but no current
12248 message. Redisplay the previous message. */
12249 display_last_displayed_message_p = 1;
12250 redisplay_internal (1);
12251 display_last_displayed_message_p = 0;
12253 else
12254 redisplay_internal (1);
12256 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12257 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12258 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12262 /* Function registered with record_unwind_protect in
12263 redisplay_internal. Reset redisplaying_p to the value it had
12264 before redisplay_internal was called, and clear
12265 prevent_freeing_realized_faces_p. It also selects the previously
12266 selected frame, unless it has been deleted (by an X connection
12267 failure during redisplay, for example). */
12269 static Lisp_Object
12270 unwind_redisplay (Lisp_Object val)
12272 Lisp_Object old_redisplaying_p, old_frame;
12274 old_redisplaying_p = XCAR (val);
12275 redisplaying_p = XFASTINT (old_redisplaying_p);
12276 old_frame = XCDR (val);
12277 if (! EQ (old_frame, selected_frame)
12278 && FRAME_LIVE_P (XFRAME (old_frame)))
12279 select_frame_for_redisplay (old_frame);
12280 return Qnil;
12284 /* Mark the display of window W as accurate or inaccurate. If
12285 ACCURATE_P is non-zero mark display of W as accurate. If
12286 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12287 redisplay_internal is called. */
12289 static void
12290 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12292 if (BUFFERP (w->buffer))
12294 struct buffer *b = XBUFFER (w->buffer);
12296 w->last_modified
12297 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12298 w->last_overlay_modified
12299 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12300 w->last_had_star
12301 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12303 if (accurate_p)
12305 b->clip_changed = 0;
12306 b->prevent_redisplay_optimizations_p = 0;
12308 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12309 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12310 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12311 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12313 w->current_matrix->buffer = b;
12314 w->current_matrix->begv = BUF_BEGV (b);
12315 w->current_matrix->zv = BUF_ZV (b);
12317 w->last_cursor = w->cursor;
12318 w->last_cursor_off_p = w->cursor_off_p;
12320 if (w == XWINDOW (selected_window))
12321 w->last_point = make_number (BUF_PT (b));
12322 else
12323 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12327 if (accurate_p)
12329 w->window_end_valid = w->buffer;
12330 w->update_mode_line = Qnil;
12335 /* Mark the display of windows in the window tree rooted at WINDOW as
12336 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12337 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12338 be redisplayed the next time redisplay_internal is called. */
12340 void
12341 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12343 struct window *w;
12345 for (; !NILP (window); window = w->next)
12347 w = XWINDOW (window);
12348 mark_window_display_accurate_1 (w, accurate_p);
12350 if (!NILP (w->vchild))
12351 mark_window_display_accurate (w->vchild, accurate_p);
12352 if (!NILP (w->hchild))
12353 mark_window_display_accurate (w->hchild, accurate_p);
12356 if (accurate_p)
12358 update_overlay_arrows (1);
12360 else
12362 /* Force a thorough redisplay the next time by setting
12363 last_arrow_position and last_arrow_string to t, which is
12364 unequal to any useful value of Voverlay_arrow_... */
12365 update_overlay_arrows (-1);
12370 /* Return value in display table DP (Lisp_Char_Table *) for character
12371 C. Since a display table doesn't have any parent, we don't have to
12372 follow parent. Do not call this function directly but use the
12373 macro DISP_CHAR_VECTOR. */
12375 Lisp_Object
12376 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12378 Lisp_Object val;
12380 if (ASCII_CHAR_P (c))
12382 val = dp->ascii;
12383 if (SUB_CHAR_TABLE_P (val))
12384 val = XSUB_CHAR_TABLE (val)->contents[c];
12386 else
12388 Lisp_Object table;
12390 XSETCHAR_TABLE (table, dp);
12391 val = char_table_ref (table, c);
12393 if (NILP (val))
12394 val = dp->defalt;
12395 return val;
12400 /***********************************************************************
12401 Window Redisplay
12402 ***********************************************************************/
12404 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12406 static void
12407 redisplay_windows (Lisp_Object window)
12409 while (!NILP (window))
12411 struct window *w = XWINDOW (window);
12413 if (!NILP (w->hchild))
12414 redisplay_windows (w->hchild);
12415 else if (!NILP (w->vchild))
12416 redisplay_windows (w->vchild);
12417 else if (!NILP (w->buffer))
12419 displayed_buffer = XBUFFER (w->buffer);
12420 /* Use list_of_error, not Qerror, so that
12421 we catch only errors and don't run the debugger. */
12422 internal_condition_case_1 (redisplay_window_0, window,
12423 list_of_error,
12424 redisplay_window_error);
12427 window = w->next;
12431 static Lisp_Object
12432 redisplay_window_error (Lisp_Object ignore)
12434 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12435 return Qnil;
12438 static Lisp_Object
12439 redisplay_window_0 (Lisp_Object window)
12441 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12442 redisplay_window (window, 0);
12443 return Qnil;
12446 static Lisp_Object
12447 redisplay_window_1 (Lisp_Object window)
12449 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12450 redisplay_window (window, 1);
12451 return Qnil;
12455 /* Increment GLYPH until it reaches END or CONDITION fails while
12456 adding (GLYPH)->pixel_width to X. */
12458 #define SKIP_GLYPHS(glyph, end, x, condition) \
12459 do \
12461 (x) += (glyph)->pixel_width; \
12462 ++(glyph); \
12464 while ((glyph) < (end) && (condition))
12467 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12468 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12469 which positions recorded in ROW differ from current buffer
12470 positions.
12472 Return 0 if cursor is not on this row, 1 otherwise. */
12475 set_cursor_from_row (struct window *w, struct glyph_row *row,
12476 struct glyph_matrix *matrix,
12477 EMACS_INT delta, EMACS_INT delta_bytes,
12478 int dy, int dvpos)
12480 struct glyph *glyph = row->glyphs[TEXT_AREA];
12481 struct glyph *end = glyph + row->used[TEXT_AREA];
12482 struct glyph *cursor = NULL;
12483 /* The last known character position in row. */
12484 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12485 int x = row->x;
12486 EMACS_INT pt_old = PT - delta;
12487 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12488 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12489 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12490 /* A glyph beyond the edge of TEXT_AREA which we should never
12491 touch. */
12492 struct glyph *glyphs_end = end;
12493 /* Non-zero means we've found a match for cursor position, but that
12494 glyph has the avoid_cursor_p flag set. */
12495 int match_with_avoid_cursor = 0;
12496 /* Non-zero means we've seen at least one glyph that came from a
12497 display string. */
12498 int string_seen = 0;
12499 /* Largest and smalles buffer positions seen so far during scan of
12500 glyph row. */
12501 EMACS_INT bpos_max = pos_before;
12502 EMACS_INT bpos_min = pos_after;
12503 /* Last buffer position covered by an overlay string with an integer
12504 `cursor' property. */
12505 EMACS_INT bpos_covered = 0;
12507 /* Skip over glyphs not having an object at the start and the end of
12508 the row. These are special glyphs like truncation marks on
12509 terminal frames. */
12510 if (row->displays_text_p)
12512 if (!row->reversed_p)
12514 while (glyph < end
12515 && INTEGERP (glyph->object)
12516 && glyph->charpos < 0)
12518 x += glyph->pixel_width;
12519 ++glyph;
12521 while (end > glyph
12522 && INTEGERP ((end - 1)->object)
12523 /* CHARPOS is zero for blanks and stretch glyphs
12524 inserted by extend_face_to_end_of_line. */
12525 && (end - 1)->charpos <= 0)
12526 --end;
12527 glyph_before = glyph - 1;
12528 glyph_after = end;
12530 else
12532 struct glyph *g;
12534 /* If the glyph row is reversed, we need to process it from back
12535 to front, so swap the edge pointers. */
12536 glyphs_end = end = glyph - 1;
12537 glyph += row->used[TEXT_AREA] - 1;
12539 while (glyph > end + 1
12540 && INTEGERP (glyph->object)
12541 && glyph->charpos < 0)
12543 --glyph;
12544 x -= glyph->pixel_width;
12546 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12547 --glyph;
12548 /* By default, in reversed rows we put the cursor on the
12549 rightmost (first in the reading order) glyph. */
12550 for (g = end + 1; g < glyph; g++)
12551 x += g->pixel_width;
12552 while (end < glyph
12553 && INTEGERP ((end + 1)->object)
12554 && (end + 1)->charpos <= 0)
12555 ++end;
12556 glyph_before = glyph + 1;
12557 glyph_after = end;
12560 else if (row->reversed_p)
12562 /* In R2L rows that don't display text, put the cursor on the
12563 rightmost glyph. Case in point: an empty last line that is
12564 part of an R2L paragraph. */
12565 cursor = end - 1;
12566 /* Avoid placing the cursor on the last glyph of the row, where
12567 on terminal frames we hold the vertical border between
12568 adjacent windows. */
12569 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12570 && !WINDOW_RIGHTMOST_P (w)
12571 && cursor == row->glyphs[LAST_AREA] - 1)
12572 cursor--;
12573 x = -1; /* will be computed below, at label compute_x */
12576 /* Step 1: Try to find the glyph whose character position
12577 corresponds to point. If that's not possible, find 2 glyphs
12578 whose character positions are the closest to point, one before
12579 point, the other after it. */
12580 if (!row->reversed_p)
12581 while (/* not marched to end of glyph row */
12582 glyph < end
12583 /* glyph was not inserted by redisplay for internal purposes */
12584 && !INTEGERP (glyph->object))
12586 if (BUFFERP (glyph->object))
12588 EMACS_INT dpos = glyph->charpos - pt_old;
12590 if (glyph->charpos > bpos_max)
12591 bpos_max = glyph->charpos;
12592 if (glyph->charpos < bpos_min)
12593 bpos_min = glyph->charpos;
12594 if (!glyph->avoid_cursor_p)
12596 /* If we hit point, we've found the glyph on which to
12597 display the cursor. */
12598 if (dpos == 0)
12600 match_with_avoid_cursor = 0;
12601 break;
12603 /* See if we've found a better approximation to
12604 POS_BEFORE or to POS_AFTER. Note that we want the
12605 first (leftmost) glyph of all those that are the
12606 closest from below, and the last (rightmost) of all
12607 those from above. */
12608 if (0 > dpos && dpos > pos_before - pt_old)
12610 pos_before = glyph->charpos;
12611 glyph_before = glyph;
12613 else if (0 < dpos && dpos <= pos_after - pt_old)
12615 pos_after = glyph->charpos;
12616 glyph_after = glyph;
12619 else if (dpos == 0)
12620 match_with_avoid_cursor = 1;
12622 else if (STRINGP (glyph->object))
12624 Lisp_Object chprop;
12625 EMACS_INT glyph_pos = glyph->charpos;
12627 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12628 glyph->object);
12629 if (INTEGERP (chprop))
12631 bpos_covered = bpos_max + XINT (chprop);
12632 /* If the `cursor' property covers buffer positions up
12633 to and including point, we should display cursor on
12634 this glyph. Note that overlays and text properties
12635 with string values stop bidi reordering, so every
12636 buffer position to the left of the string is always
12637 smaller than any position to the right of the
12638 string. Therefore, if a `cursor' property on one
12639 of the string's characters has an integer value, we
12640 will break out of the loop below _before_ we get to
12641 the position match above. IOW, integer values of
12642 the `cursor' property override the "exact match for
12643 point" strategy of positioning the cursor. */
12644 /* Implementation note: bpos_max == pt_old when, e.g.,
12645 we are in an empty line, where bpos_max is set to
12646 MATRIX_ROW_START_CHARPOS, see above. */
12647 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12649 cursor = glyph;
12650 break;
12654 string_seen = 1;
12656 x += glyph->pixel_width;
12657 ++glyph;
12659 else if (glyph > end) /* row is reversed */
12660 while (!INTEGERP (glyph->object))
12662 if (BUFFERP (glyph->object))
12664 EMACS_INT dpos = glyph->charpos - pt_old;
12666 if (glyph->charpos > bpos_max)
12667 bpos_max = glyph->charpos;
12668 if (glyph->charpos < bpos_min)
12669 bpos_min = glyph->charpos;
12670 if (!glyph->avoid_cursor_p)
12672 if (dpos == 0)
12674 match_with_avoid_cursor = 0;
12675 break;
12677 if (0 > dpos && dpos > pos_before - pt_old)
12679 pos_before = glyph->charpos;
12680 glyph_before = glyph;
12682 else if (0 < dpos && dpos <= pos_after - pt_old)
12684 pos_after = glyph->charpos;
12685 glyph_after = glyph;
12688 else if (dpos == 0)
12689 match_with_avoid_cursor = 1;
12691 else if (STRINGP (glyph->object))
12693 Lisp_Object chprop;
12694 EMACS_INT glyph_pos = glyph->charpos;
12696 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12697 glyph->object);
12698 if (INTEGERP (chprop))
12700 bpos_covered = bpos_max + XINT (chprop);
12701 /* If the `cursor' property covers buffer positions up
12702 to and including point, we should display cursor on
12703 this glyph. */
12704 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12706 cursor = glyph;
12707 break;
12710 string_seen = 1;
12712 --glyph;
12713 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12715 x--; /* can't use any pixel_width */
12716 break;
12718 x -= glyph->pixel_width;
12721 /* Step 2: If we didn't find an exact match for point, we need to
12722 look for a proper place to put the cursor among glyphs between
12723 GLYPH_BEFORE and GLYPH_AFTER. */
12724 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12725 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12726 && bpos_covered < pt_old)
12728 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12730 EMACS_INT ellipsis_pos;
12732 /* Scan back over the ellipsis glyphs. */
12733 if (!row->reversed_p)
12735 ellipsis_pos = (glyph - 1)->charpos;
12736 while (glyph > row->glyphs[TEXT_AREA]
12737 && (glyph - 1)->charpos == ellipsis_pos)
12738 glyph--, x -= glyph->pixel_width;
12739 /* That loop always goes one position too far, including
12740 the glyph before the ellipsis. So scan forward over
12741 that one. */
12742 x += glyph->pixel_width;
12743 glyph++;
12745 else /* row is reversed */
12747 ellipsis_pos = (glyph + 1)->charpos;
12748 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12749 && (glyph + 1)->charpos == ellipsis_pos)
12750 glyph++, x += glyph->pixel_width;
12751 x -= glyph->pixel_width;
12752 glyph--;
12755 else if (match_with_avoid_cursor
12756 /* A truncated row may not include PT among its
12757 character positions. Setting the cursor inside the
12758 scroll margin will trigger recalculation of hscroll
12759 in hscroll_window_tree. */
12760 || (row->truncated_on_left_p && pt_old < bpos_min)
12761 || (row->truncated_on_right_p && pt_old > bpos_max)
12762 /* Zero-width characters produce no glyphs. */
12763 || ((row->reversed_p
12764 ? glyph_after > glyphs_end
12765 : glyph_after < glyphs_end)
12766 && eabs (glyph_after - glyph_before) == 1))
12768 cursor = glyph_after;
12769 x = -1;
12771 else if (string_seen)
12773 int incr = row->reversed_p ? -1 : +1;
12775 /* Need to find the glyph that came out of a string which is
12776 present at point. That glyph is somewhere between
12777 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12778 positioned between POS_BEFORE and POS_AFTER in the
12779 buffer. */
12780 struct glyph *stop = glyph_after;
12781 EMACS_INT pos = pos_before;
12783 x = -1;
12784 for (glyph = glyph_before + incr;
12785 row->reversed_p ? glyph > stop : glyph < stop; )
12788 /* Any glyphs that come from the buffer are here because
12789 of bidi reordering. Skip them, and only pay
12790 attention to glyphs that came from some string. */
12791 if (STRINGP (glyph->object))
12793 Lisp_Object str;
12794 EMACS_INT tem;
12796 str = glyph->object;
12797 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12798 if (tem == 0 /* from overlay */
12799 || pos <= tem)
12801 /* If the string from which this glyph came is
12802 found in the buffer at point, then we've
12803 found the glyph we've been looking for. If
12804 it comes from an overlay (tem == 0), and it
12805 has the `cursor' property on one of its
12806 glyphs, record that glyph as a candidate for
12807 displaying the cursor. (As in the
12808 unidirectional version, we will display the
12809 cursor on the last candidate we find.) */
12810 if (tem == 0 || tem == pt_old)
12812 /* The glyphs from this string could have
12813 been reordered. Find the one with the
12814 smallest string position. Or there could
12815 be a character in the string with the
12816 `cursor' property, which means display
12817 cursor on that character's glyph. */
12818 EMACS_INT strpos = glyph->charpos;
12820 cursor = glyph;
12821 for (glyph += incr;
12822 (row->reversed_p ? glyph > stop : glyph < stop)
12823 && EQ (glyph->object, str);
12824 glyph += incr)
12826 Lisp_Object cprop;
12827 EMACS_INT gpos = glyph->charpos;
12829 cprop = Fget_char_property (make_number (gpos),
12830 Qcursor,
12831 glyph->object);
12832 if (!NILP (cprop))
12834 cursor = glyph;
12835 break;
12837 if (glyph->charpos < strpos)
12839 strpos = glyph->charpos;
12840 cursor = glyph;
12844 if (tem == pt_old)
12845 goto compute_x;
12847 if (tem)
12848 pos = tem + 1; /* don't find previous instances */
12850 /* This string is not what we want; skip all of the
12851 glyphs that came from it. */
12853 glyph += incr;
12854 while ((row->reversed_p ? glyph > stop : glyph < stop)
12855 && EQ (glyph->object, str));
12857 else
12858 glyph += incr;
12861 /* If we reached the end of the line, and END was from a string,
12862 the cursor is not on this line. */
12863 if (cursor == NULL
12864 && (row->reversed_p ? glyph <= end : glyph >= end)
12865 && STRINGP (end->object)
12866 && row->continued_p)
12867 return 0;
12871 compute_x:
12872 if (cursor != NULL)
12873 glyph = cursor;
12874 if (x < 0)
12876 struct glyph *g;
12878 /* Need to compute x that corresponds to GLYPH. */
12879 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12881 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12882 abort ();
12883 x += g->pixel_width;
12887 /* ROW could be part of a continued line, which, under bidi
12888 reordering, might have other rows whose start and end charpos
12889 occlude point. Only set w->cursor if we found a better
12890 approximation to the cursor position than we have from previously
12891 examined candidate rows belonging to the same continued line. */
12892 if (/* we already have a candidate row */
12893 w->cursor.vpos >= 0
12894 /* that candidate is not the row we are processing */
12895 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12896 /* the row we are processing is part of a continued line */
12897 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12898 /* Make sure cursor.vpos specifies a row whose start and end
12899 charpos occlude point. This is because some callers of this
12900 function leave cursor.vpos at the row where the cursor was
12901 displayed during the last redisplay cycle. */
12902 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12903 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12905 struct glyph *g1 =
12906 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12908 /* Don't consider glyphs that are outside TEXT_AREA. */
12909 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12910 return 0;
12911 /* Keep the candidate whose buffer position is the closest to
12912 point. */
12913 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12914 w->cursor.hpos >= 0
12915 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12916 && BUFFERP (g1->object)
12917 && (g1->charpos == pt_old /* an exact match always wins */
12918 || (BUFFERP (glyph->object)
12919 && eabs (g1->charpos - pt_old)
12920 < eabs (glyph->charpos - pt_old))))
12921 return 0;
12922 /* If this candidate gives an exact match, use that. */
12923 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12924 /* Otherwise, keep the candidate that comes from a row
12925 spanning less buffer positions. This may win when one or
12926 both candidate positions are on glyphs that came from
12927 display strings, for which we cannot compare buffer
12928 positions. */
12929 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12930 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12931 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12932 return 0;
12934 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12935 w->cursor.x = x;
12936 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12937 w->cursor.y = row->y + dy;
12939 if (w == XWINDOW (selected_window))
12941 if (!row->continued_p
12942 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12943 && row->x == 0)
12945 this_line_buffer = XBUFFER (w->buffer);
12947 CHARPOS (this_line_start_pos)
12948 = MATRIX_ROW_START_CHARPOS (row) + delta;
12949 BYTEPOS (this_line_start_pos)
12950 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12952 CHARPOS (this_line_end_pos)
12953 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12954 BYTEPOS (this_line_end_pos)
12955 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12957 this_line_y = w->cursor.y;
12958 this_line_pixel_height = row->height;
12959 this_line_vpos = w->cursor.vpos;
12960 this_line_start_x = row->x;
12962 else
12963 CHARPOS (this_line_start_pos) = 0;
12966 return 1;
12970 /* Run window scroll functions, if any, for WINDOW with new window
12971 start STARTP. Sets the window start of WINDOW to that position.
12973 We assume that the window's buffer is really current. */
12975 static INLINE struct text_pos
12976 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
12978 struct window *w = XWINDOW (window);
12979 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12981 if (current_buffer != XBUFFER (w->buffer))
12982 abort ();
12984 if (!NILP (Vwindow_scroll_functions))
12986 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12987 make_number (CHARPOS (startp)));
12988 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12989 /* In case the hook functions switch buffers. */
12990 if (current_buffer != XBUFFER (w->buffer))
12991 set_buffer_internal_1 (XBUFFER (w->buffer));
12994 return startp;
12998 /* Make sure the line containing the cursor is fully visible.
12999 A value of 1 means there is nothing to be done.
13000 (Either the line is fully visible, or it cannot be made so,
13001 or we cannot tell.)
13003 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13004 is higher than window.
13006 A value of 0 means the caller should do scrolling
13007 as if point had gone off the screen. */
13009 static int
13010 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13012 struct glyph_matrix *matrix;
13013 struct glyph_row *row;
13014 int window_height;
13016 if (!make_cursor_line_fully_visible_p)
13017 return 1;
13019 /* It's not always possible to find the cursor, e.g, when a window
13020 is full of overlay strings. Don't do anything in that case. */
13021 if (w->cursor.vpos < 0)
13022 return 1;
13024 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13025 row = MATRIX_ROW (matrix, w->cursor.vpos);
13027 /* If the cursor row is not partially visible, there's nothing to do. */
13028 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13029 return 1;
13031 /* If the row the cursor is in is taller than the window's height,
13032 it's not clear what to do, so do nothing. */
13033 window_height = window_box_height (w);
13034 if (row->height >= window_height)
13036 if (!force_p || MINI_WINDOW_P (w)
13037 || w->vscroll || w->cursor.vpos == 0)
13038 return 1;
13040 return 0;
13044 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13045 non-zero means only WINDOW is redisplayed in redisplay_internal.
13046 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13047 in redisplay_window to bring a partially visible line into view in
13048 the case that only the cursor has moved.
13050 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13051 last screen line's vertical height extends past the end of the screen.
13053 Value is
13055 1 if scrolling succeeded
13057 0 if scrolling didn't find point.
13059 -1 if new fonts have been loaded so that we must interrupt
13060 redisplay, adjust glyph matrices, and try again. */
13062 enum
13064 SCROLLING_SUCCESS,
13065 SCROLLING_FAILED,
13066 SCROLLING_NEED_LARGER_MATRICES
13069 static int
13070 try_scrolling (Lisp_Object window, int just_this_one_p,
13071 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13072 int temp_scroll_step, int last_line_misfit)
13074 struct window *w = XWINDOW (window);
13075 struct frame *f = XFRAME (w->frame);
13076 struct text_pos pos, startp;
13077 struct it it;
13078 int this_scroll_margin, scroll_max, rc, height;
13079 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13080 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13081 Lisp_Object aggressive;
13082 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13084 #if GLYPH_DEBUG
13085 debug_method_add (w, "try_scrolling");
13086 #endif
13088 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13090 /* Compute scroll margin height in pixels. We scroll when point is
13091 within this distance from the top or bottom of the window. */
13092 if (scroll_margin > 0)
13093 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13094 * FRAME_LINE_HEIGHT (f);
13095 else
13096 this_scroll_margin = 0;
13098 /* Force scroll_conservatively to have a reasonable value, to avoid
13099 overflow while computing how much to scroll. Note that the user
13100 can supply scroll-conservatively equal to `most-positive-fixnum',
13101 which can be larger than INT_MAX. */
13102 if (scroll_conservatively > scroll_limit)
13104 scroll_conservatively = scroll_limit;
13105 scroll_max = INT_MAX;
13107 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13108 /* Compute how much we should try to scroll maximally to bring
13109 point into view. */
13110 scroll_max = (max (scroll_step,
13111 max (scroll_conservatively, temp_scroll_step))
13112 * FRAME_LINE_HEIGHT (f));
13113 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13114 || NUMBERP (current_buffer->scroll_up_aggressively))
13115 /* We're trying to scroll because of aggressive scrolling but no
13116 scroll_step is set. Choose an arbitrary one. */
13117 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13118 else
13119 scroll_max = 0;
13121 too_near_end:
13123 /* Decide whether to scroll down. */
13124 if (PT > CHARPOS (startp))
13126 int scroll_margin_y;
13128 /* Compute the pixel ypos of the scroll margin, then move it to
13129 either that ypos or PT, whichever comes first. */
13130 start_display (&it, w, startp);
13131 scroll_margin_y = it.last_visible_y - this_scroll_margin
13132 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13133 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13134 (MOVE_TO_POS | MOVE_TO_Y));
13136 if (PT > CHARPOS (it.current.pos))
13138 int y0 = line_bottom_y (&it);
13139 /* Compute how many pixels below window bottom to stop searching
13140 for PT. This avoids costly search for PT that is far away if
13141 the user limited scrolling by a small number of lines, but
13142 always finds PT if scroll_conservatively is set to a large
13143 number, such as most-positive-fixnum. */
13144 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13145 int y_to_move =
13146 slack >= INT_MAX - it.last_visible_y
13147 ? INT_MAX
13148 : it.last_visible_y + slack;
13150 /* Compute the distance from the scroll margin to PT or to
13151 the scroll limit, whichever comes first. This should
13152 include the height of the cursor line, to make that line
13153 fully visible. */
13154 move_it_to (&it, PT, -1, y_to_move,
13155 -1, MOVE_TO_POS | MOVE_TO_Y);
13156 dy = line_bottom_y (&it) - y0;
13158 if (dy > scroll_max)
13159 return SCROLLING_FAILED;
13161 scroll_down_p = 1;
13165 if (scroll_down_p)
13167 /* Point is in or below the bottom scroll margin, so move the
13168 window start down. If scrolling conservatively, move it just
13169 enough down to make point visible. If scroll_step is set,
13170 move it down by scroll_step. */
13171 if (scroll_conservatively)
13172 amount_to_scroll
13173 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13174 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13175 else if (scroll_step || temp_scroll_step)
13176 amount_to_scroll = scroll_max;
13177 else
13179 aggressive = current_buffer->scroll_up_aggressively;
13180 height = WINDOW_BOX_TEXT_HEIGHT (w);
13181 if (NUMBERP (aggressive))
13183 double float_amount = XFLOATINT (aggressive) * height;
13184 amount_to_scroll = float_amount;
13185 if (amount_to_scroll == 0 && float_amount > 0)
13186 amount_to_scroll = 1;
13190 if (amount_to_scroll <= 0)
13191 return SCROLLING_FAILED;
13193 start_display (&it, w, startp);
13194 if (scroll_max < INT_MAX)
13195 move_it_vertically (&it, amount_to_scroll);
13196 else
13198 /* Extra precision for users who set scroll-conservatively
13199 to most-positive-fixnum: make sure the amount we scroll
13200 the window start is never less than amount_to_scroll,
13201 which was computed as distance from window bottom to
13202 point. This matters when lines at window top and lines
13203 below window bottom have different height. */
13204 struct it it1 = it;
13205 /* We use a temporary it1 because line_bottom_y can modify
13206 its argument, if it moves one line down; see there. */
13207 int start_y = line_bottom_y (&it1);
13209 do {
13210 move_it_by_lines (&it, 1, 1);
13211 it1 = it;
13212 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13215 /* If STARTP is unchanged, move it down another screen line. */
13216 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13217 move_it_by_lines (&it, 1, 1);
13218 startp = it.current.pos;
13220 else
13222 struct text_pos scroll_margin_pos = startp;
13224 /* See if point is inside the scroll margin at the top of the
13225 window. */
13226 if (this_scroll_margin)
13228 start_display (&it, w, startp);
13229 move_it_vertically (&it, this_scroll_margin);
13230 scroll_margin_pos = it.current.pos;
13233 if (PT < CHARPOS (scroll_margin_pos))
13235 /* Point is in the scroll margin at the top of the window or
13236 above what is displayed in the window. */
13237 int y0;
13239 /* Compute the vertical distance from PT to the scroll
13240 margin position. Give up if distance is greater than
13241 scroll_max. */
13242 SET_TEXT_POS (pos, PT, PT_BYTE);
13243 start_display (&it, w, pos);
13244 y0 = it.current_y;
13245 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13246 it.last_visible_y, -1,
13247 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13248 dy = it.current_y - y0;
13249 if (dy > scroll_max)
13250 return SCROLLING_FAILED;
13252 /* Compute new window start. */
13253 start_display (&it, w, startp);
13255 if (scroll_conservatively)
13256 amount_to_scroll
13257 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13258 else if (scroll_step || temp_scroll_step)
13259 amount_to_scroll = scroll_max;
13260 else
13262 aggressive = current_buffer->scroll_down_aggressively;
13263 height = WINDOW_BOX_TEXT_HEIGHT (w);
13264 if (NUMBERP (aggressive))
13266 double float_amount = XFLOATINT (aggressive) * height;
13267 amount_to_scroll = float_amount;
13268 if (amount_to_scroll == 0 && float_amount > 0)
13269 amount_to_scroll = 1;
13273 if (amount_to_scroll <= 0)
13274 return SCROLLING_FAILED;
13276 move_it_vertically_backward (&it, amount_to_scroll);
13277 startp = it.current.pos;
13281 /* Run window scroll functions. */
13282 startp = run_window_scroll_functions (window, startp);
13284 /* Display the window. Give up if new fonts are loaded, or if point
13285 doesn't appear. */
13286 if (!try_window (window, startp, 0))
13287 rc = SCROLLING_NEED_LARGER_MATRICES;
13288 else if (w->cursor.vpos < 0)
13290 clear_glyph_matrix (w->desired_matrix);
13291 rc = SCROLLING_FAILED;
13293 else
13295 /* Maybe forget recorded base line for line number display. */
13296 if (!just_this_one_p
13297 || current_buffer->clip_changed
13298 || BEG_UNCHANGED < CHARPOS (startp))
13299 w->base_line_number = Qnil;
13301 /* If cursor ends up on a partially visible line,
13302 treat that as being off the bottom of the screen. */
13303 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13305 clear_glyph_matrix (w->desired_matrix);
13306 ++extra_scroll_margin_lines;
13307 goto too_near_end;
13309 rc = SCROLLING_SUCCESS;
13312 return rc;
13316 /* Compute a suitable window start for window W if display of W starts
13317 on a continuation line. Value is non-zero if a new window start
13318 was computed.
13320 The new window start will be computed, based on W's width, starting
13321 from the start of the continued line. It is the start of the
13322 screen line with the minimum distance from the old start W->start. */
13324 static int
13325 compute_window_start_on_continuation_line (struct window *w)
13327 struct text_pos pos, start_pos;
13328 int window_start_changed_p = 0;
13330 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13332 /* If window start is on a continuation line... Window start may be
13333 < BEGV in case there's invisible text at the start of the
13334 buffer (M-x rmail, for example). */
13335 if (CHARPOS (start_pos) > BEGV
13336 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13338 struct it it;
13339 struct glyph_row *row;
13341 /* Handle the case that the window start is out of range. */
13342 if (CHARPOS (start_pos) < BEGV)
13343 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13344 else if (CHARPOS (start_pos) > ZV)
13345 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13347 /* Find the start of the continued line. This should be fast
13348 because scan_buffer is fast (newline cache). */
13349 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13350 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13351 row, DEFAULT_FACE_ID);
13352 reseat_at_previous_visible_line_start (&it);
13354 /* If the line start is "too far" away from the window start,
13355 say it takes too much time to compute a new window start. */
13356 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13357 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13359 int min_distance, distance;
13361 /* Move forward by display lines to find the new window
13362 start. If window width was enlarged, the new start can
13363 be expected to be > the old start. If window width was
13364 decreased, the new window start will be < the old start.
13365 So, we're looking for the display line start with the
13366 minimum distance from the old window start. */
13367 pos = it.current.pos;
13368 min_distance = INFINITY;
13369 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13370 distance < min_distance)
13372 min_distance = distance;
13373 pos = it.current.pos;
13374 move_it_by_lines (&it, 1, 0);
13377 /* Set the window start there. */
13378 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13379 window_start_changed_p = 1;
13383 return window_start_changed_p;
13387 /* Try cursor movement in case text has not changed in window WINDOW,
13388 with window start STARTP. Value is
13390 CURSOR_MOVEMENT_SUCCESS if successful
13392 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13394 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13395 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13396 we want to scroll as if scroll-step were set to 1. See the code.
13398 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13399 which case we have to abort this redisplay, and adjust matrices
13400 first. */
13402 enum
13404 CURSOR_MOVEMENT_SUCCESS,
13405 CURSOR_MOVEMENT_CANNOT_BE_USED,
13406 CURSOR_MOVEMENT_MUST_SCROLL,
13407 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13410 static int
13411 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13413 struct window *w = XWINDOW (window);
13414 struct frame *f = XFRAME (w->frame);
13415 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13417 #if GLYPH_DEBUG
13418 if (inhibit_try_cursor_movement)
13419 return rc;
13420 #endif
13422 /* Handle case where text has not changed, only point, and it has
13423 not moved off the frame. */
13424 if (/* Point may be in this window. */
13425 PT >= CHARPOS (startp)
13426 /* Selective display hasn't changed. */
13427 && !current_buffer->clip_changed
13428 /* Function force-mode-line-update is used to force a thorough
13429 redisplay. It sets either windows_or_buffers_changed or
13430 update_mode_lines. So don't take a shortcut here for these
13431 cases. */
13432 && !update_mode_lines
13433 && !windows_or_buffers_changed
13434 && !cursor_type_changed
13435 /* Can't use this case if highlighting a region. When a
13436 region exists, cursor movement has to do more than just
13437 set the cursor. */
13438 && !(!NILP (Vtransient_mark_mode)
13439 && !NILP (current_buffer->mark_active))
13440 && NILP (w->region_showing)
13441 && NILP (Vshow_trailing_whitespace)
13442 /* Right after splitting windows, last_point may be nil. */
13443 && INTEGERP (w->last_point)
13444 /* This code is not used for mini-buffer for the sake of the case
13445 of redisplaying to replace an echo area message; since in
13446 that case the mini-buffer contents per se are usually
13447 unchanged. This code is of no real use in the mini-buffer
13448 since the handling of this_line_start_pos, etc., in redisplay
13449 handles the same cases. */
13450 && !EQ (window, minibuf_window)
13451 /* When splitting windows or for new windows, it happens that
13452 redisplay is called with a nil window_end_vpos or one being
13453 larger than the window. This should really be fixed in
13454 window.c. I don't have this on my list, now, so we do
13455 approximately the same as the old redisplay code. --gerd. */
13456 && INTEGERP (w->window_end_vpos)
13457 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13458 && (FRAME_WINDOW_P (f)
13459 || !overlay_arrow_in_current_buffer_p ()))
13461 int this_scroll_margin, top_scroll_margin;
13462 struct glyph_row *row = NULL;
13464 #if GLYPH_DEBUG
13465 debug_method_add (w, "cursor movement");
13466 #endif
13468 /* Scroll if point within this distance from the top or bottom
13469 of the window. This is a pixel value. */
13470 if (scroll_margin > 0)
13472 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13473 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13475 else
13476 this_scroll_margin = 0;
13478 top_scroll_margin = this_scroll_margin;
13479 if (WINDOW_WANTS_HEADER_LINE_P (w))
13480 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13482 /* Start with the row the cursor was displayed during the last
13483 not paused redisplay. Give up if that row is not valid. */
13484 if (w->last_cursor.vpos < 0
13485 || w->last_cursor.vpos >= w->current_matrix->nrows)
13486 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13487 else
13489 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13490 if (row->mode_line_p)
13491 ++row;
13492 if (!row->enabled_p)
13493 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13496 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13498 int scroll_p = 0, must_scroll = 0;
13499 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13501 if (PT > XFASTINT (w->last_point))
13503 /* Point has moved forward. */
13504 while (MATRIX_ROW_END_CHARPOS (row) < PT
13505 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13507 xassert (row->enabled_p);
13508 ++row;
13511 /* If the end position of a row equals the start
13512 position of the next row, and PT is at that position,
13513 we would rather display cursor in the next line. */
13514 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13515 && MATRIX_ROW_END_CHARPOS (row) == PT
13516 && row < w->current_matrix->rows
13517 + w->current_matrix->nrows - 1
13518 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13519 && !cursor_row_p (w, row))
13520 ++row;
13522 /* If within the scroll margin, scroll. Note that
13523 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13524 the next line would be drawn, and that
13525 this_scroll_margin can be zero. */
13526 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13527 || PT > MATRIX_ROW_END_CHARPOS (row)
13528 /* Line is completely visible last line in window
13529 and PT is to be set in the next line. */
13530 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13531 && PT == MATRIX_ROW_END_CHARPOS (row)
13532 && !row->ends_at_zv_p
13533 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13534 scroll_p = 1;
13536 else if (PT < XFASTINT (w->last_point))
13538 /* Cursor has to be moved backward. Note that PT >=
13539 CHARPOS (startp) because of the outer if-statement. */
13540 while (!row->mode_line_p
13541 && (MATRIX_ROW_START_CHARPOS (row) > PT
13542 || (MATRIX_ROW_START_CHARPOS (row) == PT
13543 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13544 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13545 row > w->current_matrix->rows
13546 && (row-1)->ends_in_newline_from_string_p))))
13547 && (row->y > top_scroll_margin
13548 || CHARPOS (startp) == BEGV))
13550 xassert (row->enabled_p);
13551 --row;
13554 /* Consider the following case: Window starts at BEGV,
13555 there is invisible, intangible text at BEGV, so that
13556 display starts at some point START > BEGV. It can
13557 happen that we are called with PT somewhere between
13558 BEGV and START. Try to handle that case. */
13559 if (row < w->current_matrix->rows
13560 || row->mode_line_p)
13562 row = w->current_matrix->rows;
13563 if (row->mode_line_p)
13564 ++row;
13567 /* Due to newlines in overlay strings, we may have to
13568 skip forward over overlay strings. */
13569 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13570 && MATRIX_ROW_END_CHARPOS (row) == PT
13571 && !cursor_row_p (w, row))
13572 ++row;
13574 /* If within the scroll margin, scroll. */
13575 if (row->y < top_scroll_margin
13576 && CHARPOS (startp) != BEGV)
13577 scroll_p = 1;
13579 else
13581 /* Cursor did not move. So don't scroll even if cursor line
13582 is partially visible, as it was so before. */
13583 rc = CURSOR_MOVEMENT_SUCCESS;
13586 if (PT < MATRIX_ROW_START_CHARPOS (row)
13587 || PT > MATRIX_ROW_END_CHARPOS (row))
13589 /* if PT is not in the glyph row, give up. */
13590 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13591 must_scroll = 1;
13593 else if (rc != CURSOR_MOVEMENT_SUCCESS
13594 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13596 /* If rows are bidi-reordered and point moved, back up
13597 until we find a row that does not belong to a
13598 continuation line. This is because we must consider
13599 all rows of a continued line as candidates for the
13600 new cursor positioning, since row start and end
13601 positions change non-linearly with vertical position
13602 in such rows. */
13603 /* FIXME: Revisit this when glyph ``spilling'' in
13604 continuation lines' rows is implemented for
13605 bidi-reordered rows. */
13606 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13608 xassert (row->enabled_p);
13609 --row;
13610 /* If we hit the beginning of the displayed portion
13611 without finding the first row of a continued
13612 line, give up. */
13613 if (row <= w->current_matrix->rows)
13615 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13616 break;
13621 if (must_scroll)
13623 else if (rc != CURSOR_MOVEMENT_SUCCESS
13624 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13625 && make_cursor_line_fully_visible_p)
13627 if (PT == MATRIX_ROW_END_CHARPOS (row)
13628 && !row->ends_at_zv_p
13629 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13630 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13631 else if (row->height > window_box_height (w))
13633 /* If we end up in a partially visible line, let's
13634 make it fully visible, except when it's taller
13635 than the window, in which case we can't do much
13636 about it. */
13637 *scroll_step = 1;
13638 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13640 else
13642 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13643 if (!cursor_row_fully_visible_p (w, 0, 1))
13644 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13645 else
13646 rc = CURSOR_MOVEMENT_SUCCESS;
13649 else if (scroll_p)
13650 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13651 else if (rc != CURSOR_MOVEMENT_SUCCESS
13652 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13654 /* With bidi-reordered rows, there could be more than
13655 one candidate row whose start and end positions
13656 occlude point. We need to let set_cursor_from_row
13657 find the best candidate. */
13658 /* FIXME: Revisit this when glyph ``spilling'' in
13659 continuation lines' rows is implemented for
13660 bidi-reordered rows. */
13661 int rv = 0;
13665 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13666 && PT <= MATRIX_ROW_END_CHARPOS (row)
13667 && cursor_row_p (w, row))
13668 rv |= set_cursor_from_row (w, row, w->current_matrix,
13669 0, 0, 0, 0);
13670 /* As soon as we've found the first suitable row
13671 whose ends_at_zv_p flag is set, we are done. */
13672 if (rv
13673 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13675 rc = CURSOR_MOVEMENT_SUCCESS;
13676 break;
13678 ++row;
13680 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13681 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13682 || (MATRIX_ROW_START_CHARPOS (row) == PT
13683 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13684 /* If we didn't find any candidate rows, or exited the
13685 loop before all the candidates were examined, signal
13686 to the caller that this method failed. */
13687 if (rc != CURSOR_MOVEMENT_SUCCESS
13688 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13689 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13690 else if (rv)
13691 rc = CURSOR_MOVEMENT_SUCCESS;
13693 else
13697 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13699 rc = CURSOR_MOVEMENT_SUCCESS;
13700 break;
13702 ++row;
13704 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13705 && MATRIX_ROW_START_CHARPOS (row) == PT
13706 && cursor_row_p (w, row));
13711 return rc;
13714 void
13715 set_vertical_scroll_bar (struct window *w)
13717 EMACS_INT start, end, whole;
13719 /* Calculate the start and end positions for the current window.
13720 At some point, it would be nice to choose between scrollbars
13721 which reflect the whole buffer size, with special markers
13722 indicating narrowing, and scrollbars which reflect only the
13723 visible region.
13725 Note that mini-buffers sometimes aren't displaying any text. */
13726 if (!MINI_WINDOW_P (w)
13727 || (w == XWINDOW (minibuf_window)
13728 && NILP (echo_area_buffer[0])))
13730 struct buffer *buf = XBUFFER (w->buffer);
13731 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13732 start = marker_position (w->start) - BUF_BEGV (buf);
13733 /* I don't think this is guaranteed to be right. For the
13734 moment, we'll pretend it is. */
13735 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13737 if (end < start)
13738 end = start;
13739 if (whole < (end - start))
13740 whole = end - start;
13742 else
13743 start = end = whole = 0;
13745 /* Indicate what this scroll bar ought to be displaying now. */
13746 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13747 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13748 (w, end - start, whole, start);
13752 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13753 selected_window is redisplayed.
13755 We can return without actually redisplaying the window if
13756 fonts_changed_p is nonzero. In that case, redisplay_internal will
13757 retry. */
13759 static void
13760 redisplay_window (Lisp_Object window, int just_this_one_p)
13762 struct window *w = XWINDOW (window);
13763 struct frame *f = XFRAME (w->frame);
13764 struct buffer *buffer = XBUFFER (w->buffer);
13765 struct buffer *old = current_buffer;
13766 struct text_pos lpoint, opoint, startp;
13767 int update_mode_line;
13768 int tem;
13769 struct it it;
13770 /* Record it now because it's overwritten. */
13771 int current_matrix_up_to_date_p = 0;
13772 int used_current_matrix_p = 0;
13773 /* This is less strict than current_matrix_up_to_date_p.
13774 It indictes that the buffer contents and narrowing are unchanged. */
13775 int buffer_unchanged_p = 0;
13776 int temp_scroll_step = 0;
13777 int count = SPECPDL_INDEX ();
13778 int rc;
13779 int centering_position = -1;
13780 int last_line_misfit = 0;
13781 EMACS_INT beg_unchanged, end_unchanged;
13783 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13784 opoint = lpoint;
13786 /* W must be a leaf window here. */
13787 xassert (!NILP (w->buffer));
13788 #if GLYPH_DEBUG
13789 *w->desired_matrix->method = 0;
13790 #endif
13792 restart:
13793 reconsider_clip_changes (w, buffer);
13795 /* Has the mode line to be updated? */
13796 update_mode_line = (!NILP (w->update_mode_line)
13797 || update_mode_lines
13798 || buffer->clip_changed
13799 || buffer->prevent_redisplay_optimizations_p);
13801 if (MINI_WINDOW_P (w))
13803 if (w == XWINDOW (echo_area_window)
13804 && !NILP (echo_area_buffer[0]))
13806 if (update_mode_line)
13807 /* We may have to update a tty frame's menu bar or a
13808 tool-bar. Example `M-x C-h C-h C-g'. */
13809 goto finish_menu_bars;
13810 else
13811 /* We've already displayed the echo area glyphs in this window. */
13812 goto finish_scroll_bars;
13814 else if ((w != XWINDOW (minibuf_window)
13815 || minibuf_level == 0)
13816 /* When buffer is nonempty, redisplay window normally. */
13817 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13818 /* Quail displays non-mini buffers in minibuffer window.
13819 In that case, redisplay the window normally. */
13820 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13822 /* W is a mini-buffer window, but it's not active, so clear
13823 it. */
13824 int yb = window_text_bottom_y (w);
13825 struct glyph_row *row;
13826 int y;
13828 for (y = 0, row = w->desired_matrix->rows;
13829 y < yb;
13830 y += row->height, ++row)
13831 blank_row (w, row, y);
13832 goto finish_scroll_bars;
13835 clear_glyph_matrix (w->desired_matrix);
13838 /* Otherwise set up data on this window; select its buffer and point
13839 value. */
13840 /* Really select the buffer, for the sake of buffer-local
13841 variables. */
13842 set_buffer_internal_1 (XBUFFER (w->buffer));
13844 current_matrix_up_to_date_p
13845 = (!NILP (w->window_end_valid)
13846 && !current_buffer->clip_changed
13847 && !current_buffer->prevent_redisplay_optimizations_p
13848 && XFASTINT (w->last_modified) >= MODIFF
13849 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13851 /* Run the window-bottom-change-functions
13852 if it is possible that the text on the screen has changed
13853 (either due to modification of the text, or any other reason). */
13854 if (!current_matrix_up_to_date_p
13855 && !NILP (Vwindow_text_change_functions))
13857 safe_run_hooks (Qwindow_text_change_functions);
13858 goto restart;
13861 beg_unchanged = BEG_UNCHANGED;
13862 end_unchanged = END_UNCHANGED;
13864 SET_TEXT_POS (opoint, PT, PT_BYTE);
13866 specbind (Qinhibit_point_motion_hooks, Qt);
13868 buffer_unchanged_p
13869 = (!NILP (w->window_end_valid)
13870 && !current_buffer->clip_changed
13871 && XFASTINT (w->last_modified) >= MODIFF
13872 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13874 /* When windows_or_buffers_changed is non-zero, we can't rely on
13875 the window end being valid, so set it to nil there. */
13876 if (windows_or_buffers_changed)
13878 /* If window starts on a continuation line, maybe adjust the
13879 window start in case the window's width changed. */
13880 if (XMARKER (w->start)->buffer == current_buffer)
13881 compute_window_start_on_continuation_line (w);
13883 w->window_end_valid = Qnil;
13886 /* Some sanity checks. */
13887 CHECK_WINDOW_END (w);
13888 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13889 abort ();
13890 if (BYTEPOS (opoint) < CHARPOS (opoint))
13891 abort ();
13893 /* If %c is in mode line, update it if needed. */
13894 if (!NILP (w->column_number_displayed)
13895 /* This alternative quickly identifies a common case
13896 where no change is needed. */
13897 && !(PT == XFASTINT (w->last_point)
13898 && XFASTINT (w->last_modified) >= MODIFF
13899 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13900 && (XFASTINT (w->column_number_displayed)
13901 != (int) current_column ())) /* iftc */
13902 update_mode_line = 1;
13904 /* Count number of windows showing the selected buffer. An indirect
13905 buffer counts as its base buffer. */
13906 if (!just_this_one_p)
13908 struct buffer *current_base, *window_base;
13909 current_base = current_buffer;
13910 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13911 if (current_base->base_buffer)
13912 current_base = current_base->base_buffer;
13913 if (window_base->base_buffer)
13914 window_base = window_base->base_buffer;
13915 if (current_base == window_base)
13916 buffer_shared++;
13919 /* Point refers normally to the selected window. For any other
13920 window, set up appropriate value. */
13921 if (!EQ (window, selected_window))
13923 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
13924 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
13925 if (new_pt < BEGV)
13927 new_pt = BEGV;
13928 new_pt_byte = BEGV_BYTE;
13929 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13931 else if (new_pt > (ZV - 1))
13933 new_pt = ZV;
13934 new_pt_byte = ZV_BYTE;
13935 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13938 /* We don't use SET_PT so that the point-motion hooks don't run. */
13939 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13942 /* If any of the character widths specified in the display table
13943 have changed, invalidate the width run cache. It's true that
13944 this may be a bit late to catch such changes, but the rest of
13945 redisplay goes (non-fatally) haywire when the display table is
13946 changed, so why should we worry about doing any better? */
13947 if (current_buffer->width_run_cache)
13949 struct Lisp_Char_Table *disptab = buffer_display_table ();
13951 if (! disptab_matches_widthtab (disptab,
13952 XVECTOR (current_buffer->width_table)))
13954 invalidate_region_cache (current_buffer,
13955 current_buffer->width_run_cache,
13956 BEG, Z);
13957 recompute_width_table (current_buffer, disptab);
13961 /* If window-start is screwed up, choose a new one. */
13962 if (XMARKER (w->start)->buffer != current_buffer)
13963 goto recenter;
13965 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13967 /* If someone specified a new starting point but did not insist,
13968 check whether it can be used. */
13969 if (!NILP (w->optional_new_start)
13970 && CHARPOS (startp) >= BEGV
13971 && CHARPOS (startp) <= ZV)
13973 w->optional_new_start = Qnil;
13974 start_display (&it, w, startp);
13975 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13976 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13977 if (IT_CHARPOS (it) == PT)
13978 w->force_start = Qt;
13979 /* IT may overshoot PT if text at PT is invisible. */
13980 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13981 w->force_start = Qt;
13984 force_start:
13986 /* Handle case where place to start displaying has been specified,
13987 unless the specified location is outside the accessible range. */
13988 if (!NILP (w->force_start)
13989 || w->frozen_window_start_p)
13991 /* We set this later on if we have to adjust point. */
13992 int new_vpos = -1;
13994 w->force_start = Qnil;
13995 w->vscroll = 0;
13996 w->window_end_valid = Qnil;
13998 /* Forget any recorded base line for line number display. */
13999 if (!buffer_unchanged_p)
14000 w->base_line_number = Qnil;
14002 /* Redisplay the mode line. Select the buffer properly for that.
14003 Also, run the hook window-scroll-functions
14004 because we have scrolled. */
14005 /* Note, we do this after clearing force_start because
14006 if there's an error, it is better to forget about force_start
14007 than to get into an infinite loop calling the hook functions
14008 and having them get more errors. */
14009 if (!update_mode_line
14010 || ! NILP (Vwindow_scroll_functions))
14012 update_mode_line = 1;
14013 w->update_mode_line = Qt;
14014 startp = run_window_scroll_functions (window, startp);
14017 w->last_modified = make_number (0);
14018 w->last_overlay_modified = make_number (0);
14019 if (CHARPOS (startp) < BEGV)
14020 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14021 else if (CHARPOS (startp) > ZV)
14022 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14024 /* Redisplay, then check if cursor has been set during the
14025 redisplay. Give up if new fonts were loaded. */
14026 /* We used to issue a CHECK_MARGINS argument to try_window here,
14027 but this causes scrolling to fail when point begins inside
14028 the scroll margin (bug#148) -- cyd */
14029 if (!try_window (window, startp, 0))
14031 w->force_start = Qt;
14032 clear_glyph_matrix (w->desired_matrix);
14033 goto need_larger_matrices;
14036 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14038 /* If point does not appear, try to move point so it does
14039 appear. The desired matrix has been built above, so we
14040 can use it here. */
14041 new_vpos = window_box_height (w) / 2;
14044 if (!cursor_row_fully_visible_p (w, 0, 0))
14046 /* Point does appear, but on a line partly visible at end of window.
14047 Move it back to a fully-visible line. */
14048 new_vpos = window_box_height (w);
14051 /* If we need to move point for either of the above reasons,
14052 now actually do it. */
14053 if (new_vpos >= 0)
14055 struct glyph_row *row;
14057 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14058 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14059 ++row;
14061 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14062 MATRIX_ROW_START_BYTEPOS (row));
14064 if (w != XWINDOW (selected_window))
14065 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14066 else if (current_buffer == old)
14067 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14069 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14071 /* If we are highlighting the region, then we just changed
14072 the region, so redisplay to show it. */
14073 if (!NILP (Vtransient_mark_mode)
14074 && !NILP (current_buffer->mark_active))
14076 clear_glyph_matrix (w->desired_matrix);
14077 if (!try_window (window, startp, 0))
14078 goto need_larger_matrices;
14082 #if GLYPH_DEBUG
14083 debug_method_add (w, "forced window start");
14084 #endif
14085 goto done;
14088 /* Handle case where text has not changed, only point, and it has
14089 not moved off the frame, and we are not retrying after hscroll.
14090 (current_matrix_up_to_date_p is nonzero when retrying.) */
14091 if (current_matrix_up_to_date_p
14092 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14093 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14095 switch (rc)
14097 case CURSOR_MOVEMENT_SUCCESS:
14098 used_current_matrix_p = 1;
14099 goto done;
14101 case CURSOR_MOVEMENT_MUST_SCROLL:
14102 goto try_to_scroll;
14104 default:
14105 abort ();
14108 /* If current starting point was originally the beginning of a line
14109 but no longer is, find a new starting point. */
14110 else if (!NILP (w->start_at_line_beg)
14111 && !(CHARPOS (startp) <= BEGV
14112 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14114 #if GLYPH_DEBUG
14115 debug_method_add (w, "recenter 1");
14116 #endif
14117 goto recenter;
14120 /* Try scrolling with try_window_id. Value is > 0 if update has
14121 been done, it is -1 if we know that the same window start will
14122 not work. It is 0 if unsuccessful for some other reason. */
14123 else if ((tem = try_window_id (w)) != 0)
14125 #if GLYPH_DEBUG
14126 debug_method_add (w, "try_window_id %d", tem);
14127 #endif
14129 if (fonts_changed_p)
14130 goto need_larger_matrices;
14131 if (tem > 0)
14132 goto done;
14134 /* Otherwise try_window_id has returned -1 which means that we
14135 don't want the alternative below this comment to execute. */
14137 else if (CHARPOS (startp) >= BEGV
14138 && CHARPOS (startp) <= ZV
14139 && PT >= CHARPOS (startp)
14140 && (CHARPOS (startp) < ZV
14141 /* Avoid starting at end of buffer. */
14142 || CHARPOS (startp) == BEGV
14143 || (XFASTINT (w->last_modified) >= MODIFF
14144 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14147 /* If first window line is a continuation line, and window start
14148 is inside the modified region, but the first change is before
14149 current window start, we must select a new window start.
14151 However, if this is the result of a down-mouse event (e.g. by
14152 extending the mouse-drag-overlay), we don't want to select a
14153 new window start, since that would change the position under
14154 the mouse, resulting in an unwanted mouse-movement rather
14155 than a simple mouse-click. */
14156 if (NILP (w->start_at_line_beg)
14157 && NILP (do_mouse_tracking)
14158 && CHARPOS (startp) > BEGV
14159 && CHARPOS (startp) > BEG + beg_unchanged
14160 && CHARPOS (startp) <= Z - end_unchanged
14161 /* Even if w->start_at_line_beg is nil, a new window may
14162 start at a line_beg, since that's how set_buffer_window
14163 sets it. So, we need to check the return value of
14164 compute_window_start_on_continuation_line. (See also
14165 bug#197). */
14166 && XMARKER (w->start)->buffer == current_buffer
14167 && compute_window_start_on_continuation_line (w))
14169 w->force_start = Qt;
14170 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14171 goto force_start;
14174 #if GLYPH_DEBUG
14175 debug_method_add (w, "same window start");
14176 #endif
14178 /* Try to redisplay starting at same place as before.
14179 If point has not moved off frame, accept the results. */
14180 if (!current_matrix_up_to_date_p
14181 /* Don't use try_window_reusing_current_matrix in this case
14182 because a window scroll function can have changed the
14183 buffer. */
14184 || !NILP (Vwindow_scroll_functions)
14185 || MINI_WINDOW_P (w)
14186 || !(used_current_matrix_p
14187 = try_window_reusing_current_matrix (w)))
14189 IF_DEBUG (debug_method_add (w, "1"));
14190 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14191 /* -1 means we need to scroll.
14192 0 means we need new matrices, but fonts_changed_p
14193 is set in that case, so we will detect it below. */
14194 goto try_to_scroll;
14197 if (fonts_changed_p)
14198 goto need_larger_matrices;
14200 if (w->cursor.vpos >= 0)
14202 if (!just_this_one_p
14203 || current_buffer->clip_changed
14204 || BEG_UNCHANGED < CHARPOS (startp))
14205 /* Forget any recorded base line for line number display. */
14206 w->base_line_number = Qnil;
14208 if (!cursor_row_fully_visible_p (w, 1, 0))
14210 clear_glyph_matrix (w->desired_matrix);
14211 last_line_misfit = 1;
14213 /* Drop through and scroll. */
14214 else
14215 goto done;
14217 else
14218 clear_glyph_matrix (w->desired_matrix);
14221 try_to_scroll:
14223 w->last_modified = make_number (0);
14224 w->last_overlay_modified = make_number (0);
14226 /* Redisplay the mode line. Select the buffer properly for that. */
14227 if (!update_mode_line)
14229 update_mode_line = 1;
14230 w->update_mode_line = Qt;
14233 /* Try to scroll by specified few lines. */
14234 if ((scroll_conservatively
14235 || scroll_step
14236 || temp_scroll_step
14237 || NUMBERP (current_buffer->scroll_up_aggressively)
14238 || NUMBERP (current_buffer->scroll_down_aggressively))
14239 && !current_buffer->clip_changed
14240 && CHARPOS (startp) >= BEGV
14241 && CHARPOS (startp) <= ZV)
14243 /* The function returns -1 if new fonts were loaded, 1 if
14244 successful, 0 if not successful. */
14245 int rc = try_scrolling (window, just_this_one_p,
14246 scroll_conservatively,
14247 scroll_step,
14248 temp_scroll_step, last_line_misfit);
14249 switch (rc)
14251 case SCROLLING_SUCCESS:
14252 goto done;
14254 case SCROLLING_NEED_LARGER_MATRICES:
14255 goto need_larger_matrices;
14257 case SCROLLING_FAILED:
14258 break;
14260 default:
14261 abort ();
14265 /* Finally, just choose place to start which centers point */
14267 recenter:
14268 if (centering_position < 0)
14269 centering_position = window_box_height (w) / 2;
14271 #if GLYPH_DEBUG
14272 debug_method_add (w, "recenter");
14273 #endif
14275 /* w->vscroll = 0; */
14277 /* Forget any previously recorded base line for line number display. */
14278 if (!buffer_unchanged_p)
14279 w->base_line_number = Qnil;
14281 /* Move backward half the height of the window. */
14282 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14283 it.current_y = it.last_visible_y;
14284 move_it_vertically_backward (&it, centering_position);
14285 xassert (IT_CHARPOS (it) >= BEGV);
14287 /* The function move_it_vertically_backward may move over more
14288 than the specified y-distance. If it->w is small, e.g. a
14289 mini-buffer window, we may end up in front of the window's
14290 display area. Start displaying at the start of the line
14291 containing PT in this case. */
14292 if (it.current_y <= 0)
14294 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14295 move_it_vertically_backward (&it, 0);
14296 it.current_y = 0;
14299 it.current_x = it.hpos = 0;
14301 /* Set startp here explicitly in case that helps avoid an infinite loop
14302 in case the window-scroll-functions functions get errors. */
14303 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14305 /* Run scroll hooks. */
14306 startp = run_window_scroll_functions (window, it.current.pos);
14308 /* Redisplay the window. */
14309 if (!current_matrix_up_to_date_p
14310 || windows_or_buffers_changed
14311 || cursor_type_changed
14312 /* Don't use try_window_reusing_current_matrix in this case
14313 because it can have changed the buffer. */
14314 || !NILP (Vwindow_scroll_functions)
14315 || !just_this_one_p
14316 || MINI_WINDOW_P (w)
14317 || !(used_current_matrix_p
14318 = try_window_reusing_current_matrix (w)))
14319 try_window (window, startp, 0);
14321 /* If new fonts have been loaded (due to fontsets), give up. We
14322 have to start a new redisplay since we need to re-adjust glyph
14323 matrices. */
14324 if (fonts_changed_p)
14325 goto need_larger_matrices;
14327 /* If cursor did not appear assume that the middle of the window is
14328 in the first line of the window. Do it again with the next line.
14329 (Imagine a window of height 100, displaying two lines of height
14330 60. Moving back 50 from it->last_visible_y will end in the first
14331 line.) */
14332 if (w->cursor.vpos < 0)
14334 if (!NILP (w->window_end_valid)
14335 && PT >= Z - XFASTINT (w->window_end_pos))
14337 clear_glyph_matrix (w->desired_matrix);
14338 move_it_by_lines (&it, 1, 0);
14339 try_window (window, it.current.pos, 0);
14341 else if (PT < IT_CHARPOS (it))
14343 clear_glyph_matrix (w->desired_matrix);
14344 move_it_by_lines (&it, -1, 0);
14345 try_window (window, it.current.pos, 0);
14347 else
14349 /* Not much we can do about it. */
14353 /* Consider the following case: Window starts at BEGV, there is
14354 invisible, intangible text at BEGV, so that display starts at
14355 some point START > BEGV. It can happen that we are called with
14356 PT somewhere between BEGV and START. Try to handle that case. */
14357 if (w->cursor.vpos < 0)
14359 struct glyph_row *row = w->current_matrix->rows;
14360 if (row->mode_line_p)
14361 ++row;
14362 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14365 if (!cursor_row_fully_visible_p (w, 0, 0))
14367 /* If vscroll is enabled, disable it and try again. */
14368 if (w->vscroll)
14370 w->vscroll = 0;
14371 clear_glyph_matrix (w->desired_matrix);
14372 goto recenter;
14375 /* If centering point failed to make the whole line visible,
14376 put point at the top instead. That has to make the whole line
14377 visible, if it can be done. */
14378 if (centering_position == 0)
14379 goto done;
14381 clear_glyph_matrix (w->desired_matrix);
14382 centering_position = 0;
14383 goto recenter;
14386 done:
14388 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14389 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14390 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14391 ? Qt : Qnil);
14393 /* Display the mode line, if we must. */
14394 if ((update_mode_line
14395 /* If window not full width, must redo its mode line
14396 if (a) the window to its side is being redone and
14397 (b) we do a frame-based redisplay. This is a consequence
14398 of how inverted lines are drawn in frame-based redisplay. */
14399 || (!just_this_one_p
14400 && !FRAME_WINDOW_P (f)
14401 && !WINDOW_FULL_WIDTH_P (w))
14402 /* Line number to display. */
14403 || INTEGERP (w->base_line_pos)
14404 /* Column number is displayed and different from the one displayed. */
14405 || (!NILP (w->column_number_displayed)
14406 && (XFASTINT (w->column_number_displayed)
14407 != (int) current_column ()))) /* iftc */
14408 /* This means that the window has a mode line. */
14409 && (WINDOW_WANTS_MODELINE_P (w)
14410 || WINDOW_WANTS_HEADER_LINE_P (w)))
14412 display_mode_lines (w);
14414 /* If mode line height has changed, arrange for a thorough
14415 immediate redisplay using the correct mode line height. */
14416 if (WINDOW_WANTS_MODELINE_P (w)
14417 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14419 fonts_changed_p = 1;
14420 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14421 = DESIRED_MODE_LINE_HEIGHT (w);
14424 /* If header line height has changed, arrange for a thorough
14425 immediate redisplay using the correct header line height. */
14426 if (WINDOW_WANTS_HEADER_LINE_P (w)
14427 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14429 fonts_changed_p = 1;
14430 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14431 = DESIRED_HEADER_LINE_HEIGHT (w);
14434 if (fonts_changed_p)
14435 goto need_larger_matrices;
14438 if (!line_number_displayed
14439 && !BUFFERP (w->base_line_pos))
14441 w->base_line_pos = Qnil;
14442 w->base_line_number = Qnil;
14445 finish_menu_bars:
14447 /* When we reach a frame's selected window, redo the frame's menu bar. */
14448 if (update_mode_line
14449 && EQ (FRAME_SELECTED_WINDOW (f), window))
14451 int redisplay_menu_p = 0;
14452 int redisplay_tool_bar_p = 0;
14454 if (FRAME_WINDOW_P (f))
14456 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14457 || defined (HAVE_NS) || defined (USE_GTK)
14458 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14459 #else
14460 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14461 #endif
14463 else
14464 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14466 if (redisplay_menu_p)
14467 display_menu_bar (w);
14469 #ifdef HAVE_WINDOW_SYSTEM
14470 if (FRAME_WINDOW_P (f))
14472 #if defined (USE_GTK) || defined (HAVE_NS)
14473 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14474 #else
14475 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14476 && (FRAME_TOOL_BAR_LINES (f) > 0
14477 || !NILP (Vauto_resize_tool_bars));
14478 #endif
14480 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14482 ignore_mouse_drag_p = 1;
14485 #endif
14488 #ifdef HAVE_WINDOW_SYSTEM
14489 if (FRAME_WINDOW_P (f)
14490 && update_window_fringes (w, (just_this_one_p
14491 || (!used_current_matrix_p && !overlay_arrow_seen)
14492 || w->pseudo_window_p)))
14494 update_begin (f);
14495 BLOCK_INPUT;
14496 if (draw_window_fringes (w, 1))
14497 x_draw_vertical_border (w);
14498 UNBLOCK_INPUT;
14499 update_end (f);
14501 #endif /* HAVE_WINDOW_SYSTEM */
14503 /* We go to this label, with fonts_changed_p nonzero,
14504 if it is necessary to try again using larger glyph matrices.
14505 We have to redeem the scroll bar even in this case,
14506 because the loop in redisplay_internal expects that. */
14507 need_larger_matrices:
14509 finish_scroll_bars:
14511 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14513 /* Set the thumb's position and size. */
14514 set_vertical_scroll_bar (w);
14516 /* Note that we actually used the scroll bar attached to this
14517 window, so it shouldn't be deleted at the end of redisplay. */
14518 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14519 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14522 /* Restore current_buffer and value of point in it. The window
14523 update may have changed the buffer, so first make sure `opoint'
14524 is still valid (Bug#6177). */
14525 if (CHARPOS (opoint) < BEGV)
14526 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14527 else if (CHARPOS (opoint) > ZV)
14528 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14529 else
14530 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14532 set_buffer_internal_1 (old);
14533 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14534 shorter. This can be caused by log truncation in *Messages*. */
14535 if (CHARPOS (lpoint) <= ZV)
14536 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14538 unbind_to (count, Qnil);
14542 /* Build the complete desired matrix of WINDOW with a window start
14543 buffer position POS.
14545 Value is 1 if successful. It is zero if fonts were loaded during
14546 redisplay which makes re-adjusting glyph matrices necessary, and -1
14547 if point would appear in the scroll margins.
14548 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14549 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14550 set in FLAGS.) */
14553 try_window (Lisp_Object window, struct text_pos pos, int flags)
14555 struct window *w = XWINDOW (window);
14556 struct it it;
14557 struct glyph_row *last_text_row = NULL;
14558 struct frame *f = XFRAME (w->frame);
14560 /* Make POS the new window start. */
14561 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14563 /* Mark cursor position as unknown. No overlay arrow seen. */
14564 w->cursor.vpos = -1;
14565 overlay_arrow_seen = 0;
14567 /* Initialize iterator and info to start at POS. */
14568 start_display (&it, w, pos);
14570 /* Display all lines of W. */
14571 while (it.current_y < it.last_visible_y)
14573 if (display_line (&it))
14574 last_text_row = it.glyph_row - 1;
14575 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14576 return 0;
14579 /* Don't let the cursor end in the scroll margins. */
14580 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14581 && !MINI_WINDOW_P (w))
14583 int this_scroll_margin;
14585 if (scroll_margin > 0)
14587 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14588 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14590 else
14591 this_scroll_margin = 0;
14593 if ((w->cursor.y >= 0 /* not vscrolled */
14594 && w->cursor.y < this_scroll_margin
14595 && CHARPOS (pos) > BEGV
14596 && IT_CHARPOS (it) < ZV)
14597 /* rms: considering make_cursor_line_fully_visible_p here
14598 seems to give wrong results. We don't want to recenter
14599 when the last line is partly visible, we want to allow
14600 that case to be handled in the usual way. */
14601 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14603 w->cursor.vpos = -1;
14604 clear_glyph_matrix (w->desired_matrix);
14605 return -1;
14609 /* If bottom moved off end of frame, change mode line percentage. */
14610 if (XFASTINT (w->window_end_pos) <= 0
14611 && Z != IT_CHARPOS (it))
14612 w->update_mode_line = Qt;
14614 /* Set window_end_pos to the offset of the last character displayed
14615 on the window from the end of current_buffer. Set
14616 window_end_vpos to its row number. */
14617 if (last_text_row)
14619 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14620 w->window_end_bytepos
14621 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14622 w->window_end_pos
14623 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14624 w->window_end_vpos
14625 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14626 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14627 ->displays_text_p);
14629 else
14631 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14632 w->window_end_pos = make_number (Z - ZV);
14633 w->window_end_vpos = make_number (0);
14636 /* But that is not valid info until redisplay finishes. */
14637 w->window_end_valid = Qnil;
14638 return 1;
14643 /************************************************************************
14644 Window redisplay reusing current matrix when buffer has not changed
14645 ************************************************************************/
14647 /* Try redisplay of window W showing an unchanged buffer with a
14648 different window start than the last time it was displayed by
14649 reusing its current matrix. Value is non-zero if successful.
14650 W->start is the new window start. */
14652 static int
14653 try_window_reusing_current_matrix (struct window *w)
14655 struct frame *f = XFRAME (w->frame);
14656 struct glyph_row *row, *bottom_row;
14657 struct it it;
14658 struct run run;
14659 struct text_pos start, new_start;
14660 int nrows_scrolled, i;
14661 struct glyph_row *last_text_row;
14662 struct glyph_row *last_reused_text_row;
14663 struct glyph_row *start_row;
14664 int start_vpos, min_y, max_y;
14666 #if GLYPH_DEBUG
14667 if (inhibit_try_window_reusing)
14668 return 0;
14669 #endif
14671 if (/* This function doesn't handle terminal frames. */
14672 !FRAME_WINDOW_P (f)
14673 /* Don't try to reuse the display if windows have been split
14674 or such. */
14675 || windows_or_buffers_changed
14676 || cursor_type_changed)
14677 return 0;
14679 /* Can't do this if region may have changed. */
14680 if ((!NILP (Vtransient_mark_mode)
14681 && !NILP (current_buffer->mark_active))
14682 || !NILP (w->region_showing)
14683 || !NILP (Vshow_trailing_whitespace))
14684 return 0;
14686 /* If top-line visibility has changed, give up. */
14687 if (WINDOW_WANTS_HEADER_LINE_P (w)
14688 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14689 return 0;
14691 /* Give up if old or new display is scrolled vertically. We could
14692 make this function handle this, but right now it doesn't. */
14693 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14694 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14695 return 0;
14697 /* The variable new_start now holds the new window start. The old
14698 start `start' can be determined from the current matrix. */
14699 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14700 start = start_row->minpos;
14701 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14703 /* Clear the desired matrix for the display below. */
14704 clear_glyph_matrix (w->desired_matrix);
14706 if (CHARPOS (new_start) <= CHARPOS (start))
14708 int first_row_y;
14710 /* Don't use this method if the display starts with an ellipsis
14711 displayed for invisible text. It's not easy to handle that case
14712 below, and it's certainly not worth the effort since this is
14713 not a frequent case. */
14714 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14715 return 0;
14717 IF_DEBUG (debug_method_add (w, "twu1"));
14719 /* Display up to a row that can be reused. The variable
14720 last_text_row is set to the last row displayed that displays
14721 text. Note that it.vpos == 0 if or if not there is a
14722 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14723 start_display (&it, w, new_start);
14724 first_row_y = it.current_y;
14725 w->cursor.vpos = -1;
14726 last_text_row = last_reused_text_row = NULL;
14728 while (it.current_y < it.last_visible_y
14729 && !fonts_changed_p)
14731 /* If we have reached into the characters in the START row,
14732 that means the line boundaries have changed. So we
14733 can't start copying with the row START. Maybe it will
14734 work to start copying with the following row. */
14735 while (IT_CHARPOS (it) > CHARPOS (start))
14737 /* Advance to the next row as the "start". */
14738 start_row++;
14739 start = start_row->minpos;
14740 /* If there are no more rows to try, or just one, give up. */
14741 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14742 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14743 || CHARPOS (start) == ZV)
14745 clear_glyph_matrix (w->desired_matrix);
14746 return 0;
14749 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14751 /* If we have reached alignment,
14752 we can copy the rest of the rows. */
14753 if (IT_CHARPOS (it) == CHARPOS (start))
14754 break;
14756 if (display_line (&it))
14757 last_text_row = it.glyph_row - 1;
14760 /* A value of current_y < last_visible_y means that we stopped
14761 at the previous window start, which in turn means that we
14762 have at least one reusable row. */
14763 if (it.current_y < it.last_visible_y)
14765 /* IT.vpos always starts from 0; it counts text lines. */
14766 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14768 /* Find PT if not already found in the lines displayed. */
14769 if (w->cursor.vpos < 0)
14771 int dy = it.current_y - start_row->y;
14773 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14774 row = row_containing_pos (w, PT, row, NULL, dy);
14775 if (row)
14776 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14777 dy, nrows_scrolled);
14778 else
14780 clear_glyph_matrix (w->desired_matrix);
14781 return 0;
14785 /* Scroll the display. Do it before the current matrix is
14786 changed. The problem here is that update has not yet
14787 run, i.e. part of the current matrix is not up to date.
14788 scroll_run_hook will clear the cursor, and use the
14789 current matrix to get the height of the row the cursor is
14790 in. */
14791 run.current_y = start_row->y;
14792 run.desired_y = it.current_y;
14793 run.height = it.last_visible_y - it.current_y;
14795 if (run.height > 0 && run.current_y != run.desired_y)
14797 update_begin (f);
14798 FRAME_RIF (f)->update_window_begin_hook (w);
14799 FRAME_RIF (f)->clear_window_mouse_face (w);
14800 FRAME_RIF (f)->scroll_run_hook (w, &run);
14801 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14802 update_end (f);
14805 /* Shift current matrix down by nrows_scrolled lines. */
14806 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14807 rotate_matrix (w->current_matrix,
14808 start_vpos,
14809 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14810 nrows_scrolled);
14812 /* Disable lines that must be updated. */
14813 for (i = 0; i < nrows_scrolled; ++i)
14814 (start_row + i)->enabled_p = 0;
14816 /* Re-compute Y positions. */
14817 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14818 max_y = it.last_visible_y;
14819 for (row = start_row + nrows_scrolled;
14820 row < bottom_row;
14821 ++row)
14823 row->y = it.current_y;
14824 row->visible_height = row->height;
14826 if (row->y < min_y)
14827 row->visible_height -= min_y - row->y;
14828 if (row->y + row->height > max_y)
14829 row->visible_height -= row->y + row->height - max_y;
14830 row->redraw_fringe_bitmaps_p = 1;
14832 it.current_y += row->height;
14834 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14835 last_reused_text_row = row;
14836 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14837 break;
14840 /* Disable lines in the current matrix which are now
14841 below the window. */
14842 for (++row; row < bottom_row; ++row)
14843 row->enabled_p = row->mode_line_p = 0;
14846 /* Update window_end_pos etc.; last_reused_text_row is the last
14847 reused row from the current matrix containing text, if any.
14848 The value of last_text_row is the last displayed line
14849 containing text. */
14850 if (last_reused_text_row)
14852 w->window_end_bytepos
14853 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14854 w->window_end_pos
14855 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14856 w->window_end_vpos
14857 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14858 w->current_matrix));
14860 else if (last_text_row)
14862 w->window_end_bytepos
14863 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14864 w->window_end_pos
14865 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14866 w->window_end_vpos
14867 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14869 else
14871 /* This window must be completely empty. */
14872 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14873 w->window_end_pos = make_number (Z - ZV);
14874 w->window_end_vpos = make_number (0);
14876 w->window_end_valid = Qnil;
14878 /* Update hint: don't try scrolling again in update_window. */
14879 w->desired_matrix->no_scrolling_p = 1;
14881 #if GLYPH_DEBUG
14882 debug_method_add (w, "try_window_reusing_current_matrix 1");
14883 #endif
14884 return 1;
14886 else if (CHARPOS (new_start) > CHARPOS (start))
14888 struct glyph_row *pt_row, *row;
14889 struct glyph_row *first_reusable_row;
14890 struct glyph_row *first_row_to_display;
14891 int dy;
14892 int yb = window_text_bottom_y (w);
14894 /* Find the row starting at new_start, if there is one. Don't
14895 reuse a partially visible line at the end. */
14896 first_reusable_row = start_row;
14897 while (first_reusable_row->enabled_p
14898 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14899 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14900 < CHARPOS (new_start)))
14901 ++first_reusable_row;
14903 /* Give up if there is no row to reuse. */
14904 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14905 || !first_reusable_row->enabled_p
14906 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14907 != CHARPOS (new_start)))
14908 return 0;
14910 /* We can reuse fully visible rows beginning with
14911 first_reusable_row to the end of the window. Set
14912 first_row_to_display to the first row that cannot be reused.
14913 Set pt_row to the row containing point, if there is any. */
14914 pt_row = NULL;
14915 for (first_row_to_display = first_reusable_row;
14916 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14917 ++first_row_to_display)
14919 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14920 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14921 pt_row = first_row_to_display;
14924 /* Start displaying at the start of first_row_to_display. */
14925 xassert (first_row_to_display->y < yb);
14926 init_to_row_start (&it, w, first_row_to_display);
14928 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14929 - start_vpos);
14930 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14931 - nrows_scrolled);
14932 it.current_y = (first_row_to_display->y - first_reusable_row->y
14933 + WINDOW_HEADER_LINE_HEIGHT (w));
14935 /* Display lines beginning with first_row_to_display in the
14936 desired matrix. Set last_text_row to the last row displayed
14937 that displays text. */
14938 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14939 if (pt_row == NULL)
14940 w->cursor.vpos = -1;
14941 last_text_row = NULL;
14942 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14943 if (display_line (&it))
14944 last_text_row = it.glyph_row - 1;
14946 /* If point is in a reused row, adjust y and vpos of the cursor
14947 position. */
14948 if (pt_row)
14950 w->cursor.vpos -= nrows_scrolled;
14951 w->cursor.y -= first_reusable_row->y - start_row->y;
14954 /* Give up if point isn't in a row displayed or reused. (This
14955 also handles the case where w->cursor.vpos < nrows_scrolled
14956 after the calls to display_line, which can happen with scroll
14957 margins. See bug#1295.) */
14958 if (w->cursor.vpos < 0)
14960 clear_glyph_matrix (w->desired_matrix);
14961 return 0;
14964 /* Scroll the display. */
14965 run.current_y = first_reusable_row->y;
14966 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14967 run.height = it.last_visible_y - run.current_y;
14968 dy = run.current_y - run.desired_y;
14970 if (run.height)
14972 update_begin (f);
14973 FRAME_RIF (f)->update_window_begin_hook (w);
14974 FRAME_RIF (f)->clear_window_mouse_face (w);
14975 FRAME_RIF (f)->scroll_run_hook (w, &run);
14976 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14977 update_end (f);
14980 /* Adjust Y positions of reused rows. */
14981 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14982 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14983 max_y = it.last_visible_y;
14984 for (row = first_reusable_row; row < first_row_to_display; ++row)
14986 row->y -= dy;
14987 row->visible_height = row->height;
14988 if (row->y < min_y)
14989 row->visible_height -= min_y - row->y;
14990 if (row->y + row->height > max_y)
14991 row->visible_height -= row->y + row->height - max_y;
14992 row->redraw_fringe_bitmaps_p = 1;
14995 /* Scroll the current matrix. */
14996 xassert (nrows_scrolled > 0);
14997 rotate_matrix (w->current_matrix,
14998 start_vpos,
14999 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15000 -nrows_scrolled);
15002 /* Disable rows not reused. */
15003 for (row -= nrows_scrolled; row < bottom_row; ++row)
15004 row->enabled_p = 0;
15006 /* Point may have moved to a different line, so we cannot assume that
15007 the previous cursor position is valid; locate the correct row. */
15008 if (pt_row)
15010 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15011 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15012 row++)
15014 w->cursor.vpos++;
15015 w->cursor.y = row->y;
15017 if (row < bottom_row)
15019 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15020 struct glyph *end = glyph + row->used[TEXT_AREA];
15022 /* Can't use this optimization with bidi-reordered glyph
15023 rows, unless cursor is already at point. */
15024 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15026 if (!(w->cursor.hpos >= 0
15027 && w->cursor.hpos < row->used[TEXT_AREA]
15028 && BUFFERP (glyph->object)
15029 && glyph->charpos == PT))
15030 return 0;
15032 else
15033 for (; glyph < end
15034 && (!BUFFERP (glyph->object)
15035 || glyph->charpos < PT);
15036 glyph++)
15038 w->cursor.hpos++;
15039 w->cursor.x += glyph->pixel_width;
15044 /* Adjust window end. A null value of last_text_row means that
15045 the window end is in reused rows which in turn means that
15046 only its vpos can have changed. */
15047 if (last_text_row)
15049 w->window_end_bytepos
15050 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15051 w->window_end_pos
15052 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15053 w->window_end_vpos
15054 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15056 else
15058 w->window_end_vpos
15059 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15062 w->window_end_valid = Qnil;
15063 w->desired_matrix->no_scrolling_p = 1;
15065 #if GLYPH_DEBUG
15066 debug_method_add (w, "try_window_reusing_current_matrix 2");
15067 #endif
15068 return 1;
15071 return 0;
15076 /************************************************************************
15077 Window redisplay reusing current matrix when buffer has changed
15078 ************************************************************************/
15080 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15081 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15082 EMACS_INT *, EMACS_INT *);
15083 static struct glyph_row *
15084 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15085 struct glyph_row *);
15088 /* Return the last row in MATRIX displaying text. If row START is
15089 non-null, start searching with that row. IT gives the dimensions
15090 of the display. Value is null if matrix is empty; otherwise it is
15091 a pointer to the row found. */
15093 static struct glyph_row *
15094 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15095 struct glyph_row *start)
15097 struct glyph_row *row, *row_found;
15099 /* Set row_found to the last row in IT->w's current matrix
15100 displaying text. The loop looks funny but think of partially
15101 visible lines. */
15102 row_found = NULL;
15103 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15104 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15106 xassert (row->enabled_p);
15107 row_found = row;
15108 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15109 break;
15110 ++row;
15113 return row_found;
15117 /* Return the last row in the current matrix of W that is not affected
15118 by changes at the start of current_buffer that occurred since W's
15119 current matrix was built. Value is null if no such row exists.
15121 BEG_UNCHANGED us the number of characters unchanged at the start of
15122 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15123 first changed character in current_buffer. Characters at positions <
15124 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15125 when the current matrix was built. */
15127 static struct glyph_row *
15128 find_last_unchanged_at_beg_row (struct window *w)
15130 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15131 struct glyph_row *row;
15132 struct glyph_row *row_found = NULL;
15133 int yb = window_text_bottom_y (w);
15135 /* Find the last row displaying unchanged text. */
15136 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15137 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15138 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15139 ++row)
15141 if (/* If row ends before first_changed_pos, it is unchanged,
15142 except in some case. */
15143 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15144 /* When row ends in ZV and we write at ZV it is not
15145 unchanged. */
15146 && !row->ends_at_zv_p
15147 /* When first_changed_pos is the end of a continued line,
15148 row is not unchanged because it may be no longer
15149 continued. */
15150 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15151 && (row->continued_p
15152 || row->exact_window_width_line_p)))
15153 row_found = row;
15155 /* Stop if last visible row. */
15156 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15157 break;
15160 return row_found;
15164 /* Find the first glyph row in the current matrix of W that is not
15165 affected by changes at the end of current_buffer since the
15166 time W's current matrix was built.
15168 Return in *DELTA the number of chars by which buffer positions in
15169 unchanged text at the end of current_buffer must be adjusted.
15171 Return in *DELTA_BYTES the corresponding number of bytes.
15173 Value is null if no such row exists, i.e. all rows are affected by
15174 changes. */
15176 static struct glyph_row *
15177 find_first_unchanged_at_end_row (struct window *w,
15178 EMACS_INT *delta, EMACS_INT *delta_bytes)
15180 struct glyph_row *row;
15181 struct glyph_row *row_found = NULL;
15183 *delta = *delta_bytes = 0;
15185 /* Display must not have been paused, otherwise the current matrix
15186 is not up to date. */
15187 eassert (!NILP (w->window_end_valid));
15189 /* A value of window_end_pos >= END_UNCHANGED means that the window
15190 end is in the range of changed text. If so, there is no
15191 unchanged row at the end of W's current matrix. */
15192 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15193 return NULL;
15195 /* Set row to the last row in W's current matrix displaying text. */
15196 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15198 /* If matrix is entirely empty, no unchanged row exists. */
15199 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15201 /* The value of row is the last glyph row in the matrix having a
15202 meaningful buffer position in it. The end position of row
15203 corresponds to window_end_pos. This allows us to translate
15204 buffer positions in the current matrix to current buffer
15205 positions for characters not in changed text. */
15206 EMACS_INT Z_old =
15207 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15208 EMACS_INT Z_BYTE_old =
15209 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15210 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15211 struct glyph_row *first_text_row
15212 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15214 *delta = Z - Z_old;
15215 *delta_bytes = Z_BYTE - Z_BYTE_old;
15217 /* Set last_unchanged_pos to the buffer position of the last
15218 character in the buffer that has not been changed. Z is the
15219 index + 1 of the last character in current_buffer, i.e. by
15220 subtracting END_UNCHANGED we get the index of the last
15221 unchanged character, and we have to add BEG to get its buffer
15222 position. */
15223 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15224 last_unchanged_pos_old = last_unchanged_pos - *delta;
15226 /* Search backward from ROW for a row displaying a line that
15227 starts at a minimum position >= last_unchanged_pos_old. */
15228 for (; row > first_text_row; --row)
15230 /* This used to abort, but it can happen.
15231 It is ok to just stop the search instead here. KFS. */
15232 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15233 break;
15235 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15236 row_found = row;
15240 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15242 return row_found;
15246 /* Make sure that glyph rows in the current matrix of window W
15247 reference the same glyph memory as corresponding rows in the
15248 frame's frame matrix. This function is called after scrolling W's
15249 current matrix on a terminal frame in try_window_id and
15250 try_window_reusing_current_matrix. */
15252 static void
15253 sync_frame_with_window_matrix_rows (struct window *w)
15255 struct frame *f = XFRAME (w->frame);
15256 struct glyph_row *window_row, *window_row_end, *frame_row;
15258 /* Preconditions: W must be a leaf window and full-width. Its frame
15259 must have a frame matrix. */
15260 xassert (NILP (w->hchild) && NILP (w->vchild));
15261 xassert (WINDOW_FULL_WIDTH_P (w));
15262 xassert (!FRAME_WINDOW_P (f));
15264 /* If W is a full-width window, glyph pointers in W's current matrix
15265 have, by definition, to be the same as glyph pointers in the
15266 corresponding frame matrix. Note that frame matrices have no
15267 marginal areas (see build_frame_matrix). */
15268 window_row = w->current_matrix->rows;
15269 window_row_end = window_row + w->current_matrix->nrows;
15270 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15271 while (window_row < window_row_end)
15273 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15274 struct glyph *end = window_row->glyphs[LAST_AREA];
15276 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15277 frame_row->glyphs[TEXT_AREA] = start;
15278 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15279 frame_row->glyphs[LAST_AREA] = end;
15281 /* Disable frame rows whose corresponding window rows have
15282 been disabled in try_window_id. */
15283 if (!window_row->enabled_p)
15284 frame_row->enabled_p = 0;
15286 ++window_row, ++frame_row;
15291 /* Find the glyph row in window W containing CHARPOS. Consider all
15292 rows between START and END (not inclusive). END null means search
15293 all rows to the end of the display area of W. Value is the row
15294 containing CHARPOS or null. */
15296 struct glyph_row *
15297 row_containing_pos (struct window *w, EMACS_INT charpos,
15298 struct glyph_row *start, struct glyph_row *end, int dy)
15300 struct glyph_row *row = start;
15301 struct glyph_row *best_row = NULL;
15302 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15303 int last_y;
15305 /* If we happen to start on a header-line, skip that. */
15306 if (row->mode_line_p)
15307 ++row;
15309 if ((end && row >= end) || !row->enabled_p)
15310 return NULL;
15312 last_y = window_text_bottom_y (w) - dy;
15314 while (1)
15316 /* Give up if we have gone too far. */
15317 if (end && row >= end)
15318 return NULL;
15319 /* This formerly returned if they were equal.
15320 I think that both quantities are of a "last plus one" type;
15321 if so, when they are equal, the row is within the screen. -- rms. */
15322 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15323 return NULL;
15325 /* If it is in this row, return this row. */
15326 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15327 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15328 /* The end position of a row equals the start
15329 position of the next row. If CHARPOS is there, we
15330 would rather display it in the next line, except
15331 when this line ends in ZV. */
15332 && !row->ends_at_zv_p
15333 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15334 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15336 struct glyph *g;
15338 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15339 || (!best_row && !row->continued_p))
15340 return row;
15341 /* In bidi-reordered rows, there could be several rows
15342 occluding point, all of them belonging to the same
15343 continued line. We need to find the row which fits
15344 CHARPOS the best. */
15345 for (g = row->glyphs[TEXT_AREA];
15346 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15347 g++)
15349 if (!STRINGP (g->object))
15351 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15353 mindif = eabs (g->charpos - charpos);
15354 best_row = row;
15355 /* Exact match always wins. */
15356 if (mindif == 0)
15357 return best_row;
15362 else if (best_row && !row->continued_p)
15363 return best_row;
15364 ++row;
15369 /* Try to redisplay window W by reusing its existing display. W's
15370 current matrix must be up to date when this function is called,
15371 i.e. window_end_valid must not be nil.
15373 Value is
15375 1 if display has been updated
15376 0 if otherwise unsuccessful
15377 -1 if redisplay with same window start is known not to succeed
15379 The following steps are performed:
15381 1. Find the last row in the current matrix of W that is not
15382 affected by changes at the start of current_buffer. If no such row
15383 is found, give up.
15385 2. Find the first row in W's current matrix that is not affected by
15386 changes at the end of current_buffer. Maybe there is no such row.
15388 3. Display lines beginning with the row + 1 found in step 1 to the
15389 row found in step 2 or, if step 2 didn't find a row, to the end of
15390 the window.
15392 4. If cursor is not known to appear on the window, give up.
15394 5. If display stopped at the row found in step 2, scroll the
15395 display and current matrix as needed.
15397 6. Maybe display some lines at the end of W, if we must. This can
15398 happen under various circumstances, like a partially visible line
15399 becoming fully visible, or because newly displayed lines are displayed
15400 in smaller font sizes.
15402 7. Update W's window end information. */
15404 static int
15405 try_window_id (struct window *w)
15407 struct frame *f = XFRAME (w->frame);
15408 struct glyph_matrix *current_matrix = w->current_matrix;
15409 struct glyph_matrix *desired_matrix = w->desired_matrix;
15410 struct glyph_row *last_unchanged_at_beg_row;
15411 struct glyph_row *first_unchanged_at_end_row;
15412 struct glyph_row *row;
15413 struct glyph_row *bottom_row;
15414 int bottom_vpos;
15415 struct it it;
15416 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
15417 int dvpos, dy;
15418 struct text_pos start_pos;
15419 struct run run;
15420 int first_unchanged_at_end_vpos = 0;
15421 struct glyph_row *last_text_row, *last_text_row_at_end;
15422 struct text_pos start;
15423 EMACS_INT first_changed_charpos, last_changed_charpos;
15425 #if GLYPH_DEBUG
15426 if (inhibit_try_window_id)
15427 return 0;
15428 #endif
15430 /* This is handy for debugging. */
15431 #if 0
15432 #define GIVE_UP(X) \
15433 do { \
15434 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15435 return 0; \
15436 } while (0)
15437 #else
15438 #define GIVE_UP(X) return 0
15439 #endif
15441 SET_TEXT_POS_FROM_MARKER (start, w->start);
15443 /* Don't use this for mini-windows because these can show
15444 messages and mini-buffers, and we don't handle that here. */
15445 if (MINI_WINDOW_P (w))
15446 GIVE_UP (1);
15448 /* This flag is used to prevent redisplay optimizations. */
15449 if (windows_or_buffers_changed || cursor_type_changed)
15450 GIVE_UP (2);
15452 /* Verify that narrowing has not changed.
15453 Also verify that we were not told to prevent redisplay optimizations.
15454 It would be nice to further
15455 reduce the number of cases where this prevents try_window_id. */
15456 if (current_buffer->clip_changed
15457 || current_buffer->prevent_redisplay_optimizations_p)
15458 GIVE_UP (3);
15460 /* Window must either use window-based redisplay or be full width. */
15461 if (!FRAME_WINDOW_P (f)
15462 && (!FRAME_LINE_INS_DEL_OK (f)
15463 || !WINDOW_FULL_WIDTH_P (w)))
15464 GIVE_UP (4);
15466 /* Give up if point is known NOT to appear in W. */
15467 if (PT < CHARPOS (start))
15468 GIVE_UP (5);
15470 /* Another way to prevent redisplay optimizations. */
15471 if (XFASTINT (w->last_modified) == 0)
15472 GIVE_UP (6);
15474 /* Verify that window is not hscrolled. */
15475 if (XFASTINT (w->hscroll) != 0)
15476 GIVE_UP (7);
15478 /* Verify that display wasn't paused. */
15479 if (NILP (w->window_end_valid))
15480 GIVE_UP (8);
15482 /* Can't use this if highlighting a region because a cursor movement
15483 will do more than just set the cursor. */
15484 if (!NILP (Vtransient_mark_mode)
15485 && !NILP (current_buffer->mark_active))
15486 GIVE_UP (9);
15488 /* Likewise if highlighting trailing whitespace. */
15489 if (!NILP (Vshow_trailing_whitespace))
15490 GIVE_UP (11);
15492 /* Likewise if showing a region. */
15493 if (!NILP (w->region_showing))
15494 GIVE_UP (10);
15496 /* Can't use this if overlay arrow position and/or string have
15497 changed. */
15498 if (overlay_arrows_changed_p ())
15499 GIVE_UP (12);
15501 /* When word-wrap is on, adding a space to the first word of a
15502 wrapped line can change the wrap position, altering the line
15503 above it. It might be worthwhile to handle this more
15504 intelligently, but for now just redisplay from scratch. */
15505 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15506 GIVE_UP (21);
15508 /* Under bidi reordering, adding or deleting a character in the
15509 beginning of a paragraph, before the first strong directional
15510 character, can change the base direction of the paragraph (unless
15511 the buffer specifies a fixed paragraph direction), which will
15512 require to redisplay the whole paragraph. It might be worthwhile
15513 to find the paragraph limits and widen the range of redisplayed
15514 lines to that, but for now just give up this optimization and
15515 redisplay from scratch. */
15516 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15517 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15518 GIVE_UP (22);
15520 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15521 only if buffer has really changed. The reason is that the gap is
15522 initially at Z for freshly visited files. The code below would
15523 set end_unchanged to 0 in that case. */
15524 if (MODIFF > SAVE_MODIFF
15525 /* This seems to happen sometimes after saving a buffer. */
15526 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15528 if (GPT - BEG < BEG_UNCHANGED)
15529 BEG_UNCHANGED = GPT - BEG;
15530 if (Z - GPT < END_UNCHANGED)
15531 END_UNCHANGED = Z - GPT;
15534 /* The position of the first and last character that has been changed. */
15535 first_changed_charpos = BEG + BEG_UNCHANGED;
15536 last_changed_charpos = Z - END_UNCHANGED;
15538 /* If window starts after a line end, and the last change is in
15539 front of that newline, then changes don't affect the display.
15540 This case happens with stealth-fontification. Note that although
15541 the display is unchanged, glyph positions in the matrix have to
15542 be adjusted, of course. */
15543 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15544 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15545 && ((last_changed_charpos < CHARPOS (start)
15546 && CHARPOS (start) == BEGV)
15547 || (last_changed_charpos < CHARPOS (start) - 1
15548 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15550 EMACS_INT Z_old, delta, Z_BYTE_old, delta_bytes;
15551 struct glyph_row *r0;
15553 /* Compute how many chars/bytes have been added to or removed
15554 from the buffer. */
15555 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15556 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15557 delta = Z - Z_old;
15558 delta_bytes = Z_BYTE - Z_BYTE_old;
15560 /* Give up if PT is not in the window. Note that it already has
15561 been checked at the start of try_window_id that PT is not in
15562 front of the window start. */
15563 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15564 GIVE_UP (13);
15566 /* If window start is unchanged, we can reuse the whole matrix
15567 as is, after adjusting glyph positions. No need to compute
15568 the window end again, since its offset from Z hasn't changed. */
15569 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15570 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15571 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15572 /* PT must not be in a partially visible line. */
15573 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15574 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15576 /* Adjust positions in the glyph matrix. */
15577 if (delta || delta_bytes)
15579 struct glyph_row *r1
15580 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15581 increment_matrix_positions (w->current_matrix,
15582 MATRIX_ROW_VPOS (r0, current_matrix),
15583 MATRIX_ROW_VPOS (r1, current_matrix),
15584 delta, delta_bytes);
15587 /* Set the cursor. */
15588 row = row_containing_pos (w, PT, r0, NULL, 0);
15589 if (row)
15590 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15591 else
15592 abort ();
15593 return 1;
15597 /* Handle the case that changes are all below what is displayed in
15598 the window, and that PT is in the window. This shortcut cannot
15599 be taken if ZV is visible in the window, and text has been added
15600 there that is visible in the window. */
15601 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15602 /* ZV is not visible in the window, or there are no
15603 changes at ZV, actually. */
15604 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15605 || first_changed_charpos == last_changed_charpos))
15607 struct glyph_row *r0;
15609 /* Give up if PT is not in the window. Note that it already has
15610 been checked at the start of try_window_id that PT is not in
15611 front of the window start. */
15612 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15613 GIVE_UP (14);
15615 /* If window start is unchanged, we can reuse the whole matrix
15616 as is, without changing glyph positions since no text has
15617 been added/removed in front of the window end. */
15618 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15619 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15620 /* PT must not be in a partially visible line. */
15621 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15622 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15624 /* We have to compute the window end anew since text
15625 could have been added/removed after it. */
15626 w->window_end_pos
15627 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15628 w->window_end_bytepos
15629 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15631 /* Set the cursor. */
15632 row = row_containing_pos (w, PT, r0, NULL, 0);
15633 if (row)
15634 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15635 else
15636 abort ();
15637 return 2;
15641 /* Give up if window start is in the changed area.
15643 The condition used to read
15645 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15647 but why that was tested escapes me at the moment. */
15648 if (CHARPOS (start) >= first_changed_charpos
15649 && CHARPOS (start) <= last_changed_charpos)
15650 GIVE_UP (15);
15652 /* Check that window start agrees with the start of the first glyph
15653 row in its current matrix. Check this after we know the window
15654 start is not in changed text, otherwise positions would not be
15655 comparable. */
15656 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15657 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15658 GIVE_UP (16);
15660 /* Give up if the window ends in strings. Overlay strings
15661 at the end are difficult to handle, so don't try. */
15662 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15663 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15664 GIVE_UP (20);
15666 /* Compute the position at which we have to start displaying new
15667 lines. Some of the lines at the top of the window might be
15668 reusable because they are not displaying changed text. Find the
15669 last row in W's current matrix not affected by changes at the
15670 start of current_buffer. Value is null if changes start in the
15671 first line of window. */
15672 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15673 if (last_unchanged_at_beg_row)
15675 /* Avoid starting to display in the moddle of a character, a TAB
15676 for instance. This is easier than to set up the iterator
15677 exactly, and it's not a frequent case, so the additional
15678 effort wouldn't really pay off. */
15679 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15680 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15681 && last_unchanged_at_beg_row > w->current_matrix->rows)
15682 --last_unchanged_at_beg_row;
15684 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15685 GIVE_UP (17);
15687 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15688 GIVE_UP (18);
15689 start_pos = it.current.pos;
15691 /* Start displaying new lines in the desired matrix at the same
15692 vpos we would use in the current matrix, i.e. below
15693 last_unchanged_at_beg_row. */
15694 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15695 current_matrix);
15696 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15697 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15699 xassert (it.hpos == 0 && it.current_x == 0);
15701 else
15703 /* There are no reusable lines at the start of the window.
15704 Start displaying in the first text line. */
15705 start_display (&it, w, start);
15706 it.vpos = it.first_vpos;
15707 start_pos = it.current.pos;
15710 /* Find the first row that is not affected by changes at the end of
15711 the buffer. Value will be null if there is no unchanged row, in
15712 which case we must redisplay to the end of the window. delta
15713 will be set to the value by which buffer positions beginning with
15714 first_unchanged_at_end_row have to be adjusted due to text
15715 changes. */
15716 first_unchanged_at_end_row
15717 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15718 IF_DEBUG (debug_delta = delta);
15719 IF_DEBUG (debug_delta_bytes = delta_bytes);
15721 /* Set stop_pos to the buffer position up to which we will have to
15722 display new lines. If first_unchanged_at_end_row != NULL, this
15723 is the buffer position of the start of the line displayed in that
15724 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15725 that we don't stop at a buffer position. */
15726 stop_pos = 0;
15727 if (first_unchanged_at_end_row)
15729 xassert (last_unchanged_at_beg_row == NULL
15730 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15732 /* If this is a continuation line, move forward to the next one
15733 that isn't. Changes in lines above affect this line.
15734 Caution: this may move first_unchanged_at_end_row to a row
15735 not displaying text. */
15736 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15737 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15738 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15739 < it.last_visible_y))
15740 ++first_unchanged_at_end_row;
15742 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15743 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15744 >= it.last_visible_y))
15745 first_unchanged_at_end_row = NULL;
15746 else
15748 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15749 + delta);
15750 first_unchanged_at_end_vpos
15751 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15752 xassert (stop_pos >= Z - END_UNCHANGED);
15755 else if (last_unchanged_at_beg_row == NULL)
15756 GIVE_UP (19);
15759 #if GLYPH_DEBUG
15761 /* Either there is no unchanged row at the end, or the one we have
15762 now displays text. This is a necessary condition for the window
15763 end pos calculation at the end of this function. */
15764 xassert (first_unchanged_at_end_row == NULL
15765 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15767 debug_last_unchanged_at_beg_vpos
15768 = (last_unchanged_at_beg_row
15769 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15770 : -1);
15771 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15773 #endif /* GLYPH_DEBUG != 0 */
15776 /* Display new lines. Set last_text_row to the last new line
15777 displayed which has text on it, i.e. might end up as being the
15778 line where the window_end_vpos is. */
15779 w->cursor.vpos = -1;
15780 last_text_row = NULL;
15781 overlay_arrow_seen = 0;
15782 while (it.current_y < it.last_visible_y
15783 && !fonts_changed_p
15784 && (first_unchanged_at_end_row == NULL
15785 || IT_CHARPOS (it) < stop_pos))
15787 if (display_line (&it))
15788 last_text_row = it.glyph_row - 1;
15791 if (fonts_changed_p)
15792 return -1;
15795 /* Compute differences in buffer positions, y-positions etc. for
15796 lines reused at the bottom of the window. Compute what we can
15797 scroll. */
15798 if (first_unchanged_at_end_row
15799 /* No lines reused because we displayed everything up to the
15800 bottom of the window. */
15801 && it.current_y < it.last_visible_y)
15803 dvpos = (it.vpos
15804 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15805 current_matrix));
15806 dy = it.current_y - first_unchanged_at_end_row->y;
15807 run.current_y = first_unchanged_at_end_row->y;
15808 run.desired_y = run.current_y + dy;
15809 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15811 else
15813 delta = delta_bytes = dvpos = dy
15814 = run.current_y = run.desired_y = run.height = 0;
15815 first_unchanged_at_end_row = NULL;
15817 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15820 /* Find the cursor if not already found. We have to decide whether
15821 PT will appear on this window (it sometimes doesn't, but this is
15822 not a very frequent case.) This decision has to be made before
15823 the current matrix is altered. A value of cursor.vpos < 0 means
15824 that PT is either in one of the lines beginning at
15825 first_unchanged_at_end_row or below the window. Don't care for
15826 lines that might be displayed later at the window end; as
15827 mentioned, this is not a frequent case. */
15828 if (w->cursor.vpos < 0)
15830 /* Cursor in unchanged rows at the top? */
15831 if (PT < CHARPOS (start_pos)
15832 && last_unchanged_at_beg_row)
15834 row = row_containing_pos (w, PT,
15835 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15836 last_unchanged_at_beg_row + 1, 0);
15837 if (row)
15838 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15841 /* Start from first_unchanged_at_end_row looking for PT. */
15842 else if (first_unchanged_at_end_row)
15844 row = row_containing_pos (w, PT - delta,
15845 first_unchanged_at_end_row, NULL, 0);
15846 if (row)
15847 set_cursor_from_row (w, row, w->current_matrix, delta,
15848 delta_bytes, dy, dvpos);
15851 /* Give up if cursor was not found. */
15852 if (w->cursor.vpos < 0)
15854 clear_glyph_matrix (w->desired_matrix);
15855 return -1;
15859 /* Don't let the cursor end in the scroll margins. */
15861 int this_scroll_margin, cursor_height;
15863 this_scroll_margin = max (0, scroll_margin);
15864 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15865 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15866 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15868 if ((w->cursor.y < this_scroll_margin
15869 && CHARPOS (start) > BEGV)
15870 /* Old redisplay didn't take scroll margin into account at the bottom,
15871 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15872 || (w->cursor.y + (make_cursor_line_fully_visible_p
15873 ? cursor_height + this_scroll_margin
15874 : 1)) > it.last_visible_y)
15876 w->cursor.vpos = -1;
15877 clear_glyph_matrix (w->desired_matrix);
15878 return -1;
15882 /* Scroll the display. Do it before changing the current matrix so
15883 that xterm.c doesn't get confused about where the cursor glyph is
15884 found. */
15885 if (dy && run.height)
15887 update_begin (f);
15889 if (FRAME_WINDOW_P (f))
15891 FRAME_RIF (f)->update_window_begin_hook (w);
15892 FRAME_RIF (f)->clear_window_mouse_face (w);
15893 FRAME_RIF (f)->scroll_run_hook (w, &run);
15894 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15896 else
15898 /* Terminal frame. In this case, dvpos gives the number of
15899 lines to scroll by; dvpos < 0 means scroll up. */
15900 int first_unchanged_at_end_vpos
15901 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15902 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15903 int end = (WINDOW_TOP_EDGE_LINE (w)
15904 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15905 + window_internal_height (w));
15907 #if defined (HAVE_GPM) || defined (MSDOS)
15908 x_clear_window_mouse_face (w);
15909 #endif
15910 /* Perform the operation on the screen. */
15911 if (dvpos > 0)
15913 /* Scroll last_unchanged_at_beg_row to the end of the
15914 window down dvpos lines. */
15915 set_terminal_window (f, end);
15917 /* On dumb terminals delete dvpos lines at the end
15918 before inserting dvpos empty lines. */
15919 if (!FRAME_SCROLL_REGION_OK (f))
15920 ins_del_lines (f, end - dvpos, -dvpos);
15922 /* Insert dvpos empty lines in front of
15923 last_unchanged_at_beg_row. */
15924 ins_del_lines (f, from, dvpos);
15926 else if (dvpos < 0)
15928 /* Scroll up last_unchanged_at_beg_vpos to the end of
15929 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15930 set_terminal_window (f, end);
15932 /* Delete dvpos lines in front of
15933 last_unchanged_at_beg_vpos. ins_del_lines will set
15934 the cursor to the given vpos and emit |dvpos| delete
15935 line sequences. */
15936 ins_del_lines (f, from + dvpos, dvpos);
15938 /* On a dumb terminal insert dvpos empty lines at the
15939 end. */
15940 if (!FRAME_SCROLL_REGION_OK (f))
15941 ins_del_lines (f, end + dvpos, -dvpos);
15944 set_terminal_window (f, 0);
15947 update_end (f);
15950 /* Shift reused rows of the current matrix to the right position.
15951 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15952 text. */
15953 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15954 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15955 if (dvpos < 0)
15957 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15958 bottom_vpos, dvpos);
15959 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15960 bottom_vpos, 0);
15962 else if (dvpos > 0)
15964 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15965 bottom_vpos, dvpos);
15966 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15967 first_unchanged_at_end_vpos + dvpos, 0);
15970 /* For frame-based redisplay, make sure that current frame and window
15971 matrix are in sync with respect to glyph memory. */
15972 if (!FRAME_WINDOW_P (f))
15973 sync_frame_with_window_matrix_rows (w);
15975 /* Adjust buffer positions in reused rows. */
15976 if (delta || delta_bytes)
15977 increment_matrix_positions (current_matrix,
15978 first_unchanged_at_end_vpos + dvpos,
15979 bottom_vpos, delta, delta_bytes);
15981 /* Adjust Y positions. */
15982 if (dy)
15983 shift_glyph_matrix (w, current_matrix,
15984 first_unchanged_at_end_vpos + dvpos,
15985 bottom_vpos, dy);
15987 if (first_unchanged_at_end_row)
15989 first_unchanged_at_end_row += dvpos;
15990 if (first_unchanged_at_end_row->y >= it.last_visible_y
15991 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15992 first_unchanged_at_end_row = NULL;
15995 /* If scrolling up, there may be some lines to display at the end of
15996 the window. */
15997 last_text_row_at_end = NULL;
15998 if (dy < 0)
16000 /* Scrolling up can leave for example a partially visible line
16001 at the end of the window to be redisplayed. */
16002 /* Set last_row to the glyph row in the current matrix where the
16003 window end line is found. It has been moved up or down in
16004 the matrix by dvpos. */
16005 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16006 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16008 /* If last_row is the window end line, it should display text. */
16009 xassert (last_row->displays_text_p);
16011 /* If window end line was partially visible before, begin
16012 displaying at that line. Otherwise begin displaying with the
16013 line following it. */
16014 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16016 init_to_row_start (&it, w, last_row);
16017 it.vpos = last_vpos;
16018 it.current_y = last_row->y;
16020 else
16022 init_to_row_end (&it, w, last_row);
16023 it.vpos = 1 + last_vpos;
16024 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16025 ++last_row;
16028 /* We may start in a continuation line. If so, we have to
16029 get the right continuation_lines_width and current_x. */
16030 it.continuation_lines_width = last_row->continuation_lines_width;
16031 it.hpos = it.current_x = 0;
16033 /* Display the rest of the lines at the window end. */
16034 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16035 while (it.current_y < it.last_visible_y
16036 && !fonts_changed_p)
16038 /* Is it always sure that the display agrees with lines in
16039 the current matrix? I don't think so, so we mark rows
16040 displayed invalid in the current matrix by setting their
16041 enabled_p flag to zero. */
16042 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16043 if (display_line (&it))
16044 last_text_row_at_end = it.glyph_row - 1;
16048 /* Update window_end_pos and window_end_vpos. */
16049 if (first_unchanged_at_end_row
16050 && !last_text_row_at_end)
16052 /* Window end line if one of the preserved rows from the current
16053 matrix. Set row to the last row displaying text in current
16054 matrix starting at first_unchanged_at_end_row, after
16055 scrolling. */
16056 xassert (first_unchanged_at_end_row->displays_text_p);
16057 row = find_last_row_displaying_text (w->current_matrix, &it,
16058 first_unchanged_at_end_row);
16059 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16061 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16062 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16063 w->window_end_vpos
16064 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16065 xassert (w->window_end_bytepos >= 0);
16066 IF_DEBUG (debug_method_add (w, "A"));
16068 else if (last_text_row_at_end)
16070 w->window_end_pos
16071 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16072 w->window_end_bytepos
16073 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16074 w->window_end_vpos
16075 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16076 xassert (w->window_end_bytepos >= 0);
16077 IF_DEBUG (debug_method_add (w, "B"));
16079 else if (last_text_row)
16081 /* We have displayed either to the end of the window or at the
16082 end of the window, i.e. the last row with text is to be found
16083 in the desired matrix. */
16084 w->window_end_pos
16085 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16086 w->window_end_bytepos
16087 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16088 w->window_end_vpos
16089 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16090 xassert (w->window_end_bytepos >= 0);
16092 else if (first_unchanged_at_end_row == NULL
16093 && last_text_row == NULL
16094 && last_text_row_at_end == NULL)
16096 /* Displayed to end of window, but no line containing text was
16097 displayed. Lines were deleted at the end of the window. */
16098 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16099 int vpos = XFASTINT (w->window_end_vpos);
16100 struct glyph_row *current_row = current_matrix->rows + vpos;
16101 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16103 for (row = NULL;
16104 row == NULL && vpos >= first_vpos;
16105 --vpos, --current_row, --desired_row)
16107 if (desired_row->enabled_p)
16109 if (desired_row->displays_text_p)
16110 row = desired_row;
16112 else if (current_row->displays_text_p)
16113 row = current_row;
16116 xassert (row != NULL);
16117 w->window_end_vpos = make_number (vpos + 1);
16118 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16119 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16120 xassert (w->window_end_bytepos >= 0);
16121 IF_DEBUG (debug_method_add (w, "C"));
16123 else
16124 abort ();
16126 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16127 debug_end_vpos = XFASTINT (w->window_end_vpos));
16129 /* Record that display has not been completed. */
16130 w->window_end_valid = Qnil;
16131 w->desired_matrix->no_scrolling_p = 1;
16132 return 3;
16134 #undef GIVE_UP
16139 /***********************************************************************
16140 More debugging support
16141 ***********************************************************************/
16143 #if GLYPH_DEBUG
16145 void dump_glyph_row (struct glyph_row *, int, int);
16146 void dump_glyph_matrix (struct glyph_matrix *, int);
16147 void dump_glyph (struct glyph_row *, struct glyph *, int);
16150 /* Dump the contents of glyph matrix MATRIX on stderr.
16152 GLYPHS 0 means don't show glyph contents.
16153 GLYPHS 1 means show glyphs in short form
16154 GLYPHS > 1 means show glyphs in long form. */
16156 void
16157 dump_glyph_matrix (matrix, glyphs)
16158 struct glyph_matrix *matrix;
16159 int glyphs;
16161 int i;
16162 for (i = 0; i < matrix->nrows; ++i)
16163 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16167 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16168 the glyph row and area where the glyph comes from. */
16170 void
16171 dump_glyph (row, glyph, area)
16172 struct glyph_row *row;
16173 struct glyph *glyph;
16174 int area;
16176 if (glyph->type == CHAR_GLYPH)
16178 fprintf (stderr,
16179 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16180 glyph - row->glyphs[TEXT_AREA],
16181 'C',
16182 glyph->charpos,
16183 (BUFFERP (glyph->object)
16184 ? 'B'
16185 : (STRINGP (glyph->object)
16186 ? 'S'
16187 : '-')),
16188 glyph->pixel_width,
16189 glyph->u.ch,
16190 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16191 ? glyph->u.ch
16192 : '.'),
16193 glyph->face_id,
16194 glyph->left_box_line_p,
16195 glyph->right_box_line_p);
16197 else if (glyph->type == STRETCH_GLYPH)
16199 fprintf (stderr,
16200 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16201 glyph - row->glyphs[TEXT_AREA],
16202 'S',
16203 glyph->charpos,
16204 (BUFFERP (glyph->object)
16205 ? 'B'
16206 : (STRINGP (glyph->object)
16207 ? 'S'
16208 : '-')),
16209 glyph->pixel_width,
16211 '.',
16212 glyph->face_id,
16213 glyph->left_box_line_p,
16214 glyph->right_box_line_p);
16216 else if (glyph->type == IMAGE_GLYPH)
16218 fprintf (stderr,
16219 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16220 glyph - row->glyphs[TEXT_AREA],
16221 'I',
16222 glyph->charpos,
16223 (BUFFERP (glyph->object)
16224 ? 'B'
16225 : (STRINGP (glyph->object)
16226 ? 'S'
16227 : '-')),
16228 glyph->pixel_width,
16229 glyph->u.img_id,
16230 '.',
16231 glyph->face_id,
16232 glyph->left_box_line_p,
16233 glyph->right_box_line_p);
16235 else if (glyph->type == COMPOSITE_GLYPH)
16237 fprintf (stderr,
16238 " %5d %4c %6d %c %3d 0x%05x",
16239 glyph - row->glyphs[TEXT_AREA],
16240 '+',
16241 glyph->charpos,
16242 (BUFFERP (glyph->object)
16243 ? 'B'
16244 : (STRINGP (glyph->object)
16245 ? 'S'
16246 : '-')),
16247 glyph->pixel_width,
16248 glyph->u.cmp.id);
16249 if (glyph->u.cmp.automatic)
16250 fprintf (stderr,
16251 "[%d-%d]",
16252 glyph->slice.cmp.from, glyph->slice.cmp.to);
16253 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16254 glyph->face_id,
16255 glyph->left_box_line_p,
16256 glyph->right_box_line_p);
16261 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16262 GLYPHS 0 means don't show glyph contents.
16263 GLYPHS 1 means show glyphs in short form
16264 GLYPHS > 1 means show glyphs in long form. */
16266 void
16267 dump_glyph_row (row, vpos, glyphs)
16268 struct glyph_row *row;
16269 int vpos, glyphs;
16271 if (glyphs != 1)
16273 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16274 fprintf (stderr, "======================================================================\n");
16276 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16277 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16278 vpos,
16279 MATRIX_ROW_START_CHARPOS (row),
16280 MATRIX_ROW_END_CHARPOS (row),
16281 row->used[TEXT_AREA],
16282 row->contains_overlapping_glyphs_p,
16283 row->enabled_p,
16284 row->truncated_on_left_p,
16285 row->truncated_on_right_p,
16286 row->continued_p,
16287 MATRIX_ROW_CONTINUATION_LINE_P (row),
16288 row->displays_text_p,
16289 row->ends_at_zv_p,
16290 row->fill_line_p,
16291 row->ends_in_middle_of_char_p,
16292 row->starts_in_middle_of_char_p,
16293 row->mouse_face_p,
16294 row->x,
16295 row->y,
16296 row->pixel_width,
16297 row->height,
16298 row->visible_height,
16299 row->ascent,
16300 row->phys_ascent);
16301 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16302 row->end.overlay_string_index,
16303 row->continuation_lines_width);
16304 fprintf (stderr, "%9d %5d\n",
16305 CHARPOS (row->start.string_pos),
16306 CHARPOS (row->end.string_pos));
16307 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16308 row->end.dpvec_index);
16311 if (glyphs > 1)
16313 int area;
16315 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16317 struct glyph *glyph = row->glyphs[area];
16318 struct glyph *glyph_end = glyph + row->used[area];
16320 /* Glyph for a line end in text. */
16321 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16322 ++glyph_end;
16324 if (glyph < glyph_end)
16325 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16327 for (; glyph < glyph_end; ++glyph)
16328 dump_glyph (row, glyph, area);
16331 else if (glyphs == 1)
16333 int area;
16335 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16337 char *s = (char *) alloca (row->used[area] + 1);
16338 int i;
16340 for (i = 0; i < row->used[area]; ++i)
16342 struct glyph *glyph = row->glyphs[area] + i;
16343 if (glyph->type == CHAR_GLYPH
16344 && glyph->u.ch < 0x80
16345 && glyph->u.ch >= ' ')
16346 s[i] = glyph->u.ch;
16347 else
16348 s[i] = '.';
16351 s[i] = '\0';
16352 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16358 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16359 Sdump_glyph_matrix, 0, 1, "p",
16360 doc: /* Dump the current matrix of the selected window to stderr.
16361 Shows contents of glyph row structures. With non-nil
16362 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16363 glyphs in short form, otherwise show glyphs in long form. */)
16364 (Lisp_Object glyphs)
16366 struct window *w = XWINDOW (selected_window);
16367 struct buffer *buffer = XBUFFER (w->buffer);
16369 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16370 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16371 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16372 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16373 fprintf (stderr, "=============================================\n");
16374 dump_glyph_matrix (w->current_matrix,
16375 NILP (glyphs) ? 0 : XINT (glyphs));
16376 return Qnil;
16380 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16381 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16382 (void)
16384 struct frame *f = XFRAME (selected_frame);
16385 dump_glyph_matrix (f->current_matrix, 1);
16386 return Qnil;
16390 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16391 doc: /* Dump glyph row ROW to stderr.
16392 GLYPH 0 means don't dump glyphs.
16393 GLYPH 1 means dump glyphs in short form.
16394 GLYPH > 1 or omitted means dump glyphs in long form. */)
16395 (Lisp_Object row, Lisp_Object glyphs)
16397 struct glyph_matrix *matrix;
16398 int vpos;
16400 CHECK_NUMBER (row);
16401 matrix = XWINDOW (selected_window)->current_matrix;
16402 vpos = XINT (row);
16403 if (vpos >= 0 && vpos < matrix->nrows)
16404 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16405 vpos,
16406 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16407 return Qnil;
16411 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16412 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16413 GLYPH 0 means don't dump glyphs.
16414 GLYPH 1 means dump glyphs in short form.
16415 GLYPH > 1 or omitted means dump glyphs in long form. */)
16416 (Lisp_Object row, Lisp_Object glyphs)
16418 struct frame *sf = SELECTED_FRAME ();
16419 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16420 int vpos;
16422 CHECK_NUMBER (row);
16423 vpos = XINT (row);
16424 if (vpos >= 0 && vpos < m->nrows)
16425 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16426 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16427 return Qnil;
16431 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16432 doc: /* Toggle tracing of redisplay.
16433 With ARG, turn tracing on if and only if ARG is positive. */)
16434 (Lisp_Object arg)
16436 if (NILP (arg))
16437 trace_redisplay_p = !trace_redisplay_p;
16438 else
16440 arg = Fprefix_numeric_value (arg);
16441 trace_redisplay_p = XINT (arg) > 0;
16444 return Qnil;
16448 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16449 doc: /* Like `format', but print result to stderr.
16450 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16451 (int nargs, Lisp_Object *args)
16453 Lisp_Object s = Fformat (nargs, args);
16454 fprintf (stderr, "%s", SDATA (s));
16455 return Qnil;
16458 #endif /* GLYPH_DEBUG */
16462 /***********************************************************************
16463 Building Desired Matrix Rows
16464 ***********************************************************************/
16466 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16467 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16469 static struct glyph_row *
16470 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16472 struct frame *f = XFRAME (WINDOW_FRAME (w));
16473 struct buffer *buffer = XBUFFER (w->buffer);
16474 struct buffer *old = current_buffer;
16475 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16476 int arrow_len = SCHARS (overlay_arrow_string);
16477 const unsigned char *arrow_end = arrow_string + arrow_len;
16478 const unsigned char *p;
16479 struct it it;
16480 int multibyte_p;
16481 int n_glyphs_before;
16483 set_buffer_temp (buffer);
16484 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16485 it.glyph_row->used[TEXT_AREA] = 0;
16486 SET_TEXT_POS (it.position, 0, 0);
16488 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16489 p = arrow_string;
16490 while (p < arrow_end)
16492 Lisp_Object face, ilisp;
16494 /* Get the next character. */
16495 if (multibyte_p)
16496 it.c = it.char_to_display = string_char_and_length (p, &it.len);
16497 else
16499 it.c = it.char_to_display = *p, it.len = 1;
16500 if (! ASCII_CHAR_P (it.c))
16501 it.char_to_display = BYTE8_TO_CHAR (it.c);
16503 p += it.len;
16505 /* Get its face. */
16506 ilisp = make_number (p - arrow_string);
16507 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16508 it.face_id = compute_char_face (f, it.char_to_display, face);
16510 /* Compute its width, get its glyphs. */
16511 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16512 SET_TEXT_POS (it.position, -1, -1);
16513 PRODUCE_GLYPHS (&it);
16515 /* If this character doesn't fit any more in the line, we have
16516 to remove some glyphs. */
16517 if (it.current_x > it.last_visible_x)
16519 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16520 break;
16524 set_buffer_temp (old);
16525 return it.glyph_row;
16529 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16530 glyphs are only inserted for terminal frames since we can't really
16531 win with truncation glyphs when partially visible glyphs are
16532 involved. Which glyphs to insert is determined by
16533 produce_special_glyphs. */
16535 static void
16536 insert_left_trunc_glyphs (struct it *it)
16538 struct it truncate_it;
16539 struct glyph *from, *end, *to, *toend;
16541 xassert (!FRAME_WINDOW_P (it->f));
16543 /* Get the truncation glyphs. */
16544 truncate_it = *it;
16545 truncate_it.current_x = 0;
16546 truncate_it.face_id = DEFAULT_FACE_ID;
16547 truncate_it.glyph_row = &scratch_glyph_row;
16548 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16549 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16550 truncate_it.object = make_number (0);
16551 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16553 /* Overwrite glyphs from IT with truncation glyphs. */
16554 if (!it->glyph_row->reversed_p)
16556 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16557 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16558 to = it->glyph_row->glyphs[TEXT_AREA];
16559 toend = to + it->glyph_row->used[TEXT_AREA];
16561 while (from < end)
16562 *to++ = *from++;
16564 /* There may be padding glyphs left over. Overwrite them too. */
16565 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16567 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16568 while (from < end)
16569 *to++ = *from++;
16572 if (to > toend)
16573 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16575 else
16577 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16578 that back to front. */
16579 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16580 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16581 toend = it->glyph_row->glyphs[TEXT_AREA];
16582 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16584 while (from >= end && to >= toend)
16585 *to-- = *from--;
16586 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16588 from =
16589 truncate_it.glyph_row->glyphs[TEXT_AREA]
16590 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16591 while (from >= end && to >= toend)
16592 *to-- = *from--;
16594 if (from >= end)
16596 /* Need to free some room before prepending additional
16597 glyphs. */
16598 int move_by = from - end + 1;
16599 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16600 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16602 for ( ; g >= g0; g--)
16603 g[move_by] = *g;
16604 while (from >= end)
16605 *to-- = *from--;
16606 it->glyph_row->used[TEXT_AREA] += move_by;
16612 /* Compute the pixel height and width of IT->glyph_row.
16614 Most of the time, ascent and height of a display line will be equal
16615 to the max_ascent and max_height values of the display iterator
16616 structure. This is not the case if
16618 1. We hit ZV without displaying anything. In this case, max_ascent
16619 and max_height will be zero.
16621 2. We have some glyphs that don't contribute to the line height.
16622 (The glyph row flag contributes_to_line_height_p is for future
16623 pixmap extensions).
16625 The first case is easily covered by using default values because in
16626 these cases, the line height does not really matter, except that it
16627 must not be zero. */
16629 static void
16630 compute_line_metrics (struct it *it)
16632 struct glyph_row *row = it->glyph_row;
16633 int area, i;
16635 if (FRAME_WINDOW_P (it->f))
16637 int i, min_y, max_y;
16639 /* The line may consist of one space only, that was added to
16640 place the cursor on it. If so, the row's height hasn't been
16641 computed yet. */
16642 if (row->height == 0)
16644 if (it->max_ascent + it->max_descent == 0)
16645 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16646 row->ascent = it->max_ascent;
16647 row->height = it->max_ascent + it->max_descent;
16648 row->phys_ascent = it->max_phys_ascent;
16649 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16650 row->extra_line_spacing = it->max_extra_line_spacing;
16653 /* Compute the width of this line. */
16654 row->pixel_width = row->x;
16655 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16656 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16658 xassert (row->pixel_width >= 0);
16659 xassert (row->ascent >= 0 && row->height > 0);
16661 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16662 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16664 /* If first line's physical ascent is larger than its logical
16665 ascent, use the physical ascent, and make the row taller.
16666 This makes accented characters fully visible. */
16667 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16668 && row->phys_ascent > row->ascent)
16670 row->height += row->phys_ascent - row->ascent;
16671 row->ascent = row->phys_ascent;
16674 /* Compute how much of the line is visible. */
16675 row->visible_height = row->height;
16677 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16678 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16680 if (row->y < min_y)
16681 row->visible_height -= min_y - row->y;
16682 if (row->y + row->height > max_y)
16683 row->visible_height -= row->y + row->height - max_y;
16685 else
16687 row->pixel_width = row->used[TEXT_AREA];
16688 if (row->continued_p)
16689 row->pixel_width -= it->continuation_pixel_width;
16690 else if (row->truncated_on_right_p)
16691 row->pixel_width -= it->truncation_pixel_width;
16692 row->ascent = row->phys_ascent = 0;
16693 row->height = row->phys_height = row->visible_height = 1;
16694 row->extra_line_spacing = 0;
16697 /* Compute a hash code for this row. */
16698 row->hash = 0;
16699 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16700 for (i = 0; i < row->used[area]; ++i)
16701 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16702 + row->glyphs[area][i].u.val
16703 + row->glyphs[area][i].face_id
16704 + row->glyphs[area][i].padding_p
16705 + (row->glyphs[area][i].type << 2));
16707 it->max_ascent = it->max_descent = 0;
16708 it->max_phys_ascent = it->max_phys_descent = 0;
16712 /* Append one space to the glyph row of iterator IT if doing a
16713 window-based redisplay. The space has the same face as
16714 IT->face_id. Value is non-zero if a space was added.
16716 This function is called to make sure that there is always one glyph
16717 at the end of a glyph row that the cursor can be set on under
16718 window-systems. (If there weren't such a glyph we would not know
16719 how wide and tall a box cursor should be displayed).
16721 At the same time this space let's a nicely handle clearing to the
16722 end of the line if the row ends in italic text. */
16724 static int
16725 append_space_for_newline (struct it *it, int default_face_p)
16727 if (FRAME_WINDOW_P (it->f))
16729 int n = it->glyph_row->used[TEXT_AREA];
16731 if (it->glyph_row->glyphs[TEXT_AREA] + n
16732 < it->glyph_row->glyphs[1 + TEXT_AREA])
16734 /* Save some values that must not be changed.
16735 Must save IT->c and IT->len because otherwise
16736 ITERATOR_AT_END_P wouldn't work anymore after
16737 append_space_for_newline has been called. */
16738 enum display_element_type saved_what = it->what;
16739 int saved_c = it->c, saved_len = it->len;
16740 int saved_char_to_display = it->char_to_display;
16741 int saved_x = it->current_x;
16742 int saved_face_id = it->face_id;
16743 struct text_pos saved_pos;
16744 Lisp_Object saved_object;
16745 struct face *face;
16747 saved_object = it->object;
16748 saved_pos = it->position;
16750 it->what = IT_CHARACTER;
16751 memset (&it->position, 0, sizeof it->position);
16752 it->object = make_number (0);
16753 it->c = it->char_to_display = ' ';
16754 it->len = 1;
16756 if (default_face_p)
16757 it->face_id = DEFAULT_FACE_ID;
16758 else if (it->face_before_selective_p)
16759 it->face_id = it->saved_face_id;
16760 face = FACE_FROM_ID (it->f, it->face_id);
16761 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16763 PRODUCE_GLYPHS (it);
16765 it->override_ascent = -1;
16766 it->constrain_row_ascent_descent_p = 0;
16767 it->current_x = saved_x;
16768 it->object = saved_object;
16769 it->position = saved_pos;
16770 it->what = saved_what;
16771 it->face_id = saved_face_id;
16772 it->len = saved_len;
16773 it->c = saved_c;
16774 it->char_to_display = saved_char_to_display;
16775 return 1;
16779 return 0;
16783 /* Extend the face of the last glyph in the text area of IT->glyph_row
16784 to the end of the display line. Called from display_line. If the
16785 glyph row is empty, add a space glyph to it so that we know the
16786 face to draw. Set the glyph row flag fill_line_p. If the glyph
16787 row is R2L, prepend a stretch glyph to cover the empty space to the
16788 left of the leftmost glyph. */
16790 static void
16791 extend_face_to_end_of_line (struct it *it)
16793 struct face *face;
16794 struct frame *f = it->f;
16796 /* If line is already filled, do nothing. Non window-system frames
16797 get a grace of one more ``pixel'' because their characters are
16798 1-``pixel'' wide, so they hit the equality too early. This grace
16799 is needed only for R2L rows that are not continued, to produce
16800 one extra blank where we could display the cursor. */
16801 if (it->current_x >= it->last_visible_x
16802 + (!FRAME_WINDOW_P (f)
16803 && it->glyph_row->reversed_p
16804 && !it->glyph_row->continued_p))
16805 return;
16807 /* Face extension extends the background and box of IT->face_id
16808 to the end of the line. If the background equals the background
16809 of the frame, we don't have to do anything. */
16810 if (it->face_before_selective_p)
16811 face = FACE_FROM_ID (f, it->saved_face_id);
16812 else
16813 face = FACE_FROM_ID (f, it->face_id);
16815 if (FRAME_WINDOW_P (f)
16816 && it->glyph_row->displays_text_p
16817 && face->box == FACE_NO_BOX
16818 && face->background == FRAME_BACKGROUND_PIXEL (f)
16819 && !face->stipple
16820 && !it->glyph_row->reversed_p)
16821 return;
16823 /* Set the glyph row flag indicating that the face of the last glyph
16824 in the text area has to be drawn to the end of the text area. */
16825 it->glyph_row->fill_line_p = 1;
16827 /* If current character of IT is not ASCII, make sure we have the
16828 ASCII face. This will be automatically undone the next time
16829 get_next_display_element returns a multibyte character. Note
16830 that the character will always be single byte in unibyte
16831 text. */
16832 if (!ASCII_CHAR_P (it->c))
16834 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16837 if (FRAME_WINDOW_P (f))
16839 /* If the row is empty, add a space with the current face of IT,
16840 so that we know which face to draw. */
16841 if (it->glyph_row->used[TEXT_AREA] == 0)
16843 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16844 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16845 it->glyph_row->used[TEXT_AREA] = 1;
16847 #ifdef HAVE_WINDOW_SYSTEM
16848 if (it->glyph_row->reversed_p)
16850 /* Prepend a stretch glyph to the row, such that the
16851 rightmost glyph will be drawn flushed all the way to the
16852 right margin of the window. The stretch glyph that will
16853 occupy the empty space, if any, to the left of the
16854 glyphs. */
16855 struct font *font = face->font ? face->font : FRAME_FONT (f);
16856 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16857 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16858 struct glyph *g;
16859 int row_width, stretch_ascent, stretch_width;
16860 struct text_pos saved_pos;
16861 int saved_face_id, saved_avoid_cursor;
16863 for (row_width = 0, g = row_start; g < row_end; g++)
16864 row_width += g->pixel_width;
16865 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16866 if (stretch_width > 0)
16868 stretch_ascent =
16869 (((it->ascent + it->descent)
16870 * FONT_BASE (font)) / FONT_HEIGHT (font));
16871 saved_pos = it->position;
16872 memset (&it->position, 0, sizeof it->position);
16873 saved_avoid_cursor = it->avoid_cursor_p;
16874 it->avoid_cursor_p = 1;
16875 saved_face_id = it->face_id;
16876 /* The last row's stretch glyph should get the default
16877 face, to avoid painting the rest of the window with
16878 the region face, if the region ends at ZV. */
16879 if (it->glyph_row->ends_at_zv_p)
16880 it->face_id = DEFAULT_FACE_ID;
16881 else
16882 it->face_id = face->id;
16883 append_stretch_glyph (it, make_number (0), stretch_width,
16884 it->ascent + it->descent, stretch_ascent);
16885 it->position = saved_pos;
16886 it->avoid_cursor_p = saved_avoid_cursor;
16887 it->face_id = saved_face_id;
16890 #endif /* HAVE_WINDOW_SYSTEM */
16892 else
16894 /* Save some values that must not be changed. */
16895 int saved_x = it->current_x;
16896 struct text_pos saved_pos;
16897 Lisp_Object saved_object;
16898 enum display_element_type saved_what = it->what;
16899 int saved_face_id = it->face_id;
16901 saved_object = it->object;
16902 saved_pos = it->position;
16904 it->what = IT_CHARACTER;
16905 memset (&it->position, 0, sizeof it->position);
16906 it->object = make_number (0);
16907 it->c = it->char_to_display = ' ';
16908 it->len = 1;
16909 /* The last row's blank glyphs should get the default face, to
16910 avoid painting the rest of the window with the region face,
16911 if the region ends at ZV. */
16912 if (it->glyph_row->ends_at_zv_p)
16913 it->face_id = DEFAULT_FACE_ID;
16914 else
16915 it->face_id = face->id;
16917 PRODUCE_GLYPHS (it);
16919 while (it->current_x <= it->last_visible_x)
16920 PRODUCE_GLYPHS (it);
16922 /* Don't count these blanks really. It would let us insert a left
16923 truncation glyph below and make us set the cursor on them, maybe. */
16924 it->current_x = saved_x;
16925 it->object = saved_object;
16926 it->position = saved_pos;
16927 it->what = saved_what;
16928 it->face_id = saved_face_id;
16933 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16934 trailing whitespace. */
16936 static int
16937 trailing_whitespace_p (EMACS_INT charpos)
16939 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
16940 int c = 0;
16942 while (bytepos < ZV_BYTE
16943 && (c = FETCH_CHAR (bytepos),
16944 c == ' ' || c == '\t'))
16945 ++bytepos;
16947 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16949 if (bytepos != PT_BYTE)
16950 return 1;
16952 return 0;
16956 /* Highlight trailing whitespace, if any, in ROW. */
16958 void
16959 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
16961 int used = row->used[TEXT_AREA];
16963 if (used)
16965 struct glyph *start = row->glyphs[TEXT_AREA];
16966 struct glyph *glyph = start + used - 1;
16968 if (row->reversed_p)
16970 /* Right-to-left rows need to be processed in the opposite
16971 direction, so swap the edge pointers. */
16972 glyph = start;
16973 start = row->glyphs[TEXT_AREA] + used - 1;
16976 /* Skip over glyphs inserted to display the cursor at the
16977 end of a line, for extending the face of the last glyph
16978 to the end of the line on terminals, and for truncation
16979 and continuation glyphs. */
16980 if (!row->reversed_p)
16982 while (glyph >= start
16983 && glyph->type == CHAR_GLYPH
16984 && INTEGERP (glyph->object))
16985 --glyph;
16987 else
16989 while (glyph <= start
16990 && glyph->type == CHAR_GLYPH
16991 && INTEGERP (glyph->object))
16992 ++glyph;
16995 /* If last glyph is a space or stretch, and it's trailing
16996 whitespace, set the face of all trailing whitespace glyphs in
16997 IT->glyph_row to `trailing-whitespace'. */
16998 if ((row->reversed_p ? glyph <= start : glyph >= start)
16999 && BUFFERP (glyph->object)
17000 && (glyph->type == STRETCH_GLYPH
17001 || (glyph->type == CHAR_GLYPH
17002 && glyph->u.ch == ' '))
17003 && trailing_whitespace_p (glyph->charpos))
17005 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17006 if (face_id < 0)
17007 return;
17009 if (!row->reversed_p)
17011 while (glyph >= start
17012 && BUFFERP (glyph->object)
17013 && (glyph->type == STRETCH_GLYPH
17014 || (glyph->type == CHAR_GLYPH
17015 && glyph->u.ch == ' ')))
17016 (glyph--)->face_id = face_id;
17018 else
17020 while (glyph <= start
17021 && BUFFERP (glyph->object)
17022 && (glyph->type == STRETCH_GLYPH
17023 || (glyph->type == CHAR_GLYPH
17024 && glyph->u.ch == ' ')))
17025 (glyph++)->face_id = face_id;
17032 /* Value is non-zero if glyph row ROW in window W should be
17033 used to hold the cursor. */
17035 static int
17036 cursor_row_p (struct window *w, struct glyph_row *row)
17038 int cursor_row_p = 1;
17040 if (PT == CHARPOS (row->end.pos))
17042 /* Suppose the row ends on a string.
17043 Unless the row is continued, that means it ends on a newline
17044 in the string. If it's anything other than a display string
17045 (e.g. a before-string from an overlay), we don't want the
17046 cursor there. (This heuristic seems to give the optimal
17047 behavior for the various types of multi-line strings.) */
17048 if (CHARPOS (row->end.string_pos) >= 0)
17050 if (row->continued_p)
17051 cursor_row_p = 1;
17052 else
17054 /* Check for `display' property. */
17055 struct glyph *beg = row->glyphs[TEXT_AREA];
17056 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17057 struct glyph *glyph;
17059 cursor_row_p = 0;
17060 for (glyph = end; glyph >= beg; --glyph)
17061 if (STRINGP (glyph->object))
17063 Lisp_Object prop
17064 = Fget_char_property (make_number (PT),
17065 Qdisplay, Qnil);
17066 cursor_row_p =
17067 (!NILP (prop)
17068 && display_prop_string_p (prop, glyph->object));
17069 break;
17073 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17075 /* If the row ends in middle of a real character,
17076 and the line is continued, we want the cursor here.
17077 That's because CHARPOS (ROW->end.pos) would equal
17078 PT if PT is before the character. */
17079 if (!row->ends_in_ellipsis_p)
17080 cursor_row_p = row->continued_p;
17081 else
17082 /* If the row ends in an ellipsis, then
17083 CHARPOS (ROW->end.pos) will equal point after the
17084 invisible text. We want that position to be displayed
17085 after the ellipsis. */
17086 cursor_row_p = 0;
17088 /* If the row ends at ZV, display the cursor at the end of that
17089 row instead of at the start of the row below. */
17090 else if (row->ends_at_zv_p)
17091 cursor_row_p = 1;
17092 else
17093 cursor_row_p = 0;
17096 return cursor_row_p;
17101 /* Push the display property PROP so that it will be rendered at the
17102 current position in IT. Return 1 if PROP was successfully pushed,
17103 0 otherwise. */
17105 static int
17106 push_display_prop (struct it *it, Lisp_Object prop)
17108 push_it (it);
17110 if (STRINGP (prop))
17112 if (SCHARS (prop) == 0)
17114 pop_it (it);
17115 return 0;
17118 it->string = prop;
17119 it->multibyte_p = STRING_MULTIBYTE (it->string);
17120 it->current.overlay_string_index = -1;
17121 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17122 it->end_charpos = it->string_nchars = SCHARS (it->string);
17123 it->method = GET_FROM_STRING;
17124 it->stop_charpos = 0;
17126 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17128 it->method = GET_FROM_STRETCH;
17129 it->object = prop;
17131 #ifdef HAVE_WINDOW_SYSTEM
17132 else if (IMAGEP (prop))
17134 it->what = IT_IMAGE;
17135 it->image_id = lookup_image (it->f, prop);
17136 it->method = GET_FROM_IMAGE;
17138 #endif /* HAVE_WINDOW_SYSTEM */
17139 else
17141 pop_it (it); /* bogus display property, give up */
17142 return 0;
17145 return 1;
17148 /* Return the character-property PROP at the current position in IT. */
17150 static Lisp_Object
17151 get_it_property (struct it *it, Lisp_Object prop)
17153 Lisp_Object position;
17155 if (STRINGP (it->object))
17156 position = make_number (IT_STRING_CHARPOS (*it));
17157 else if (BUFFERP (it->object))
17158 position = make_number (IT_CHARPOS (*it));
17159 else
17160 return Qnil;
17162 return Fget_char_property (position, prop, it->object);
17165 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17167 static void
17168 handle_line_prefix (struct it *it)
17170 Lisp_Object prefix;
17171 if (it->continuation_lines_width > 0)
17173 prefix = get_it_property (it, Qwrap_prefix);
17174 if (NILP (prefix))
17175 prefix = Vwrap_prefix;
17177 else
17179 prefix = get_it_property (it, Qline_prefix);
17180 if (NILP (prefix))
17181 prefix = Vline_prefix;
17183 if (! NILP (prefix) && push_display_prop (it, prefix))
17185 /* If the prefix is wider than the window, and we try to wrap
17186 it, it would acquire its own wrap prefix, and so on till the
17187 iterator stack overflows. So, don't wrap the prefix. */
17188 it->line_wrap = TRUNCATE;
17189 it->avoid_cursor_p = 1;
17195 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17196 only for R2L lines from display_line, when it decides that too many
17197 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17198 continued. */
17199 static void
17200 unproduce_glyphs (struct it *it, int n)
17202 struct glyph *glyph, *end;
17204 xassert (it->glyph_row);
17205 xassert (it->glyph_row->reversed_p);
17206 xassert (it->area == TEXT_AREA);
17207 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17209 if (n > it->glyph_row->used[TEXT_AREA])
17210 n = it->glyph_row->used[TEXT_AREA];
17211 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17212 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17213 for ( ; glyph < end; glyph++)
17214 glyph[-n] = *glyph;
17217 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17218 and ROW->maxpos. */
17219 static void
17220 find_row_edges (struct it *it, struct glyph_row *row,
17221 EMACS_INT min_pos, EMACS_INT min_bpos,
17222 EMACS_INT max_pos, EMACS_INT max_bpos)
17224 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17225 lines' rows is implemented for bidi-reordered rows. */
17227 /* ROW->minpos is the value of min_pos, the minimal buffer position
17228 we have in ROW. */
17229 if (min_pos <= ZV)
17230 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17231 else
17233 /* We didn't find _any_ valid buffer positions in any of the
17234 glyphs, so we must trust the iterator's computed
17235 positions. */
17236 row->minpos = row->start.pos;
17237 max_pos = CHARPOS (it->current.pos);
17238 max_bpos = BYTEPOS (it->current.pos);
17241 if (!max_pos)
17242 abort ();
17244 /* Here are the various use-cases for ending the row, and the
17245 corresponding values for ROW->maxpos:
17247 Line ends in a newline from buffer eol_pos + 1
17248 Line is continued from buffer max_pos + 1
17249 Line is truncated on right it->current.pos
17250 Line ends in a newline from string max_pos
17251 Line is continued from string max_pos
17252 Line is continued from display vector max_pos
17253 Line is entirely from a string min_pos == max_pos
17254 Line is entirely from a display vector min_pos == max_pos
17255 Line that ends at ZV ZV
17257 If you discover other use-cases, please add them here as
17258 appropriate. */
17259 if (row->ends_at_zv_p)
17260 row->maxpos = it->current.pos;
17261 else if (row->used[TEXT_AREA])
17263 if (row->ends_in_newline_from_string_p)
17264 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17265 else if (CHARPOS (it->eol_pos) > 0)
17266 SET_TEXT_POS (row->maxpos,
17267 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17268 else if (row->continued_p)
17270 /* If max_pos is different from IT's current position, it
17271 means IT->method does not belong to the display element
17272 at max_pos. However, it also means that the display
17273 element at max_pos was displayed in its entirety on this
17274 line, which is equivalent to saying that the next line
17275 starts at the next buffer position. */
17276 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17277 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17278 else
17280 INC_BOTH (max_pos, max_bpos);
17281 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17284 else if (row->truncated_on_right_p)
17285 /* display_line already called reseat_at_next_visible_line_start,
17286 which puts the iterator at the beginning of the next line, in
17287 the logical order. */
17288 row->maxpos = it->current.pos;
17289 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17290 /* A line that is entirely from a string/image/stretch... */
17291 row->maxpos = row->minpos;
17292 else
17293 abort ();
17295 else
17296 row->maxpos = it->current.pos;
17299 /* Construct the glyph row IT->glyph_row in the desired matrix of
17300 IT->w from text at the current position of IT. See dispextern.h
17301 for an overview of struct it. Value is non-zero if
17302 IT->glyph_row displays text, as opposed to a line displaying ZV
17303 only. */
17305 static int
17306 display_line (struct it *it)
17308 struct glyph_row *row = it->glyph_row;
17309 Lisp_Object overlay_arrow_string;
17310 struct it wrap_it;
17311 int may_wrap = 0, wrap_x;
17312 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17313 int wrap_row_phys_ascent, wrap_row_phys_height;
17314 int wrap_row_extra_line_spacing;
17315 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17316 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17317 int cvpos;
17318 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17320 /* We always start displaying at hpos zero even if hscrolled. */
17321 xassert (it->hpos == 0 && it->current_x == 0);
17323 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17324 >= it->w->desired_matrix->nrows)
17326 it->w->nrows_scale_factor++;
17327 fonts_changed_p = 1;
17328 return 0;
17331 /* Is IT->w showing the region? */
17332 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17334 /* Clear the result glyph row and enable it. */
17335 prepare_desired_row (row);
17337 row->y = it->current_y;
17338 row->start = it->start;
17339 row->continuation_lines_width = it->continuation_lines_width;
17340 row->displays_text_p = 1;
17341 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17342 it->starts_in_middle_of_char_p = 0;
17344 /* Arrange the overlays nicely for our purposes. Usually, we call
17345 display_line on only one line at a time, in which case this
17346 can't really hurt too much, or we call it on lines which appear
17347 one after another in the buffer, in which case all calls to
17348 recenter_overlay_lists but the first will be pretty cheap. */
17349 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17351 /* Move over display elements that are not visible because we are
17352 hscrolled. This may stop at an x-position < IT->first_visible_x
17353 if the first glyph is partially visible or if we hit a line end. */
17354 if (it->current_x < it->first_visible_x)
17356 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17357 MOVE_TO_POS | MOVE_TO_X);
17359 else
17361 /* We only do this when not calling `move_it_in_display_line_to'
17362 above, because move_it_in_display_line_to calls
17363 handle_line_prefix itself. */
17364 handle_line_prefix (it);
17367 /* Get the initial row height. This is either the height of the
17368 text hscrolled, if there is any, or zero. */
17369 row->ascent = it->max_ascent;
17370 row->height = it->max_ascent + it->max_descent;
17371 row->phys_ascent = it->max_phys_ascent;
17372 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17373 row->extra_line_spacing = it->max_extra_line_spacing;
17375 /* Utility macro to record max and min buffer positions seen until now. */
17376 #define RECORD_MAX_MIN_POS(IT) \
17377 do \
17379 if (IT_CHARPOS (*(IT)) < min_pos) \
17381 min_pos = IT_CHARPOS (*(IT)); \
17382 min_bpos = IT_BYTEPOS (*(IT)); \
17384 if (IT_CHARPOS (*(IT)) > max_pos) \
17386 max_pos = IT_CHARPOS (*(IT)); \
17387 max_bpos = IT_BYTEPOS (*(IT)); \
17390 while (0)
17392 /* Loop generating characters. The loop is left with IT on the next
17393 character to display. */
17394 while (1)
17396 int n_glyphs_before, hpos_before, x_before;
17397 int x, i, nglyphs;
17398 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17400 /* Retrieve the next thing to display. Value is zero if end of
17401 buffer reached. */
17402 if (!get_next_display_element (it))
17404 /* Maybe add a space at the end of this line that is used to
17405 display the cursor there under X. Set the charpos of the
17406 first glyph of blank lines not corresponding to any text
17407 to -1. */
17408 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17409 row->exact_window_width_line_p = 1;
17410 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17411 || row->used[TEXT_AREA] == 0)
17413 row->glyphs[TEXT_AREA]->charpos = -1;
17414 row->displays_text_p = 0;
17416 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17417 && (!MINI_WINDOW_P (it->w)
17418 || (minibuf_level && EQ (it->window, minibuf_window))))
17419 row->indicate_empty_line_p = 1;
17422 it->continuation_lines_width = 0;
17423 row->ends_at_zv_p = 1;
17424 /* A row that displays right-to-left text must always have
17425 its last face extended all the way to the end of line,
17426 even if this row ends in ZV, because we still write to
17427 the screen left to right. */
17428 if (row->reversed_p)
17429 extend_face_to_end_of_line (it);
17430 break;
17433 /* Now, get the metrics of what we want to display. This also
17434 generates glyphs in `row' (which is IT->glyph_row). */
17435 n_glyphs_before = row->used[TEXT_AREA];
17436 x = it->current_x;
17438 /* Remember the line height so far in case the next element doesn't
17439 fit on the line. */
17440 if (it->line_wrap != TRUNCATE)
17442 ascent = it->max_ascent;
17443 descent = it->max_descent;
17444 phys_ascent = it->max_phys_ascent;
17445 phys_descent = it->max_phys_descent;
17447 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17449 if (IT_DISPLAYING_WHITESPACE (it))
17450 may_wrap = 1;
17451 else if (may_wrap)
17453 wrap_it = *it;
17454 wrap_x = x;
17455 wrap_row_used = row->used[TEXT_AREA];
17456 wrap_row_ascent = row->ascent;
17457 wrap_row_height = row->height;
17458 wrap_row_phys_ascent = row->phys_ascent;
17459 wrap_row_phys_height = row->phys_height;
17460 wrap_row_extra_line_spacing = row->extra_line_spacing;
17461 wrap_row_min_pos = min_pos;
17462 wrap_row_min_bpos = min_bpos;
17463 wrap_row_max_pos = max_pos;
17464 wrap_row_max_bpos = max_bpos;
17465 may_wrap = 0;
17470 PRODUCE_GLYPHS (it);
17472 /* If this display element was in marginal areas, continue with
17473 the next one. */
17474 if (it->area != TEXT_AREA)
17476 row->ascent = max (row->ascent, it->max_ascent);
17477 row->height = max (row->height, it->max_ascent + it->max_descent);
17478 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17479 row->phys_height = max (row->phys_height,
17480 it->max_phys_ascent + it->max_phys_descent);
17481 row->extra_line_spacing = max (row->extra_line_spacing,
17482 it->max_extra_line_spacing);
17483 set_iterator_to_next (it, 1);
17484 continue;
17487 /* Does the display element fit on the line? If we truncate
17488 lines, we should draw past the right edge of the window. If
17489 we don't truncate, we want to stop so that we can display the
17490 continuation glyph before the right margin. If lines are
17491 continued, there are two possible strategies for characters
17492 resulting in more than 1 glyph (e.g. tabs): Display as many
17493 glyphs as possible in this line and leave the rest for the
17494 continuation line, or display the whole element in the next
17495 line. Original redisplay did the former, so we do it also. */
17496 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17497 hpos_before = it->hpos;
17498 x_before = x;
17500 if (/* Not a newline. */
17501 nglyphs > 0
17502 /* Glyphs produced fit entirely in the line. */
17503 && it->current_x < it->last_visible_x)
17505 it->hpos += nglyphs;
17506 row->ascent = max (row->ascent, it->max_ascent);
17507 row->height = max (row->height, it->max_ascent + it->max_descent);
17508 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17509 row->phys_height = max (row->phys_height,
17510 it->max_phys_ascent + it->max_phys_descent);
17511 row->extra_line_spacing = max (row->extra_line_spacing,
17512 it->max_extra_line_spacing);
17513 if (it->current_x - it->pixel_width < it->first_visible_x)
17514 row->x = x - it->first_visible_x;
17515 /* Record the maximum and minimum buffer positions seen so
17516 far in glyphs that will be displayed by this row. */
17517 if (it->bidi_p)
17518 RECORD_MAX_MIN_POS (it);
17520 else
17522 int new_x;
17523 struct glyph *glyph;
17525 for (i = 0; i < nglyphs; ++i, x = new_x)
17527 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17528 new_x = x + glyph->pixel_width;
17530 if (/* Lines are continued. */
17531 it->line_wrap != TRUNCATE
17532 && (/* Glyph doesn't fit on the line. */
17533 new_x > it->last_visible_x
17534 /* Or it fits exactly on a window system frame. */
17535 || (new_x == it->last_visible_x
17536 && FRAME_WINDOW_P (it->f))))
17538 /* End of a continued line. */
17540 if (it->hpos == 0
17541 || (new_x == it->last_visible_x
17542 && FRAME_WINDOW_P (it->f)))
17544 /* Current glyph is the only one on the line or
17545 fits exactly on the line. We must continue
17546 the line because we can't draw the cursor
17547 after the glyph. */
17548 row->continued_p = 1;
17549 it->current_x = new_x;
17550 it->continuation_lines_width += new_x;
17551 ++it->hpos;
17552 /* Record the maximum and minimum buffer
17553 positions seen so far in glyphs that will be
17554 displayed by this row. */
17555 if (it->bidi_p)
17556 RECORD_MAX_MIN_POS (it);
17557 if (i == nglyphs - 1)
17559 /* If line-wrap is on, check if a previous
17560 wrap point was found. */
17561 if (wrap_row_used > 0
17562 /* Even if there is a previous wrap
17563 point, continue the line here as
17564 usual, if (i) the previous character
17565 was a space or tab AND (ii) the
17566 current character is not. */
17567 && (!may_wrap
17568 || IT_DISPLAYING_WHITESPACE (it)))
17569 goto back_to_wrap;
17571 set_iterator_to_next (it, 1);
17572 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17574 if (!get_next_display_element (it))
17576 row->exact_window_width_line_p = 1;
17577 it->continuation_lines_width = 0;
17578 row->continued_p = 0;
17579 row->ends_at_zv_p = 1;
17581 else if (ITERATOR_AT_END_OF_LINE_P (it))
17583 row->continued_p = 0;
17584 row->exact_window_width_line_p = 1;
17589 else if (CHAR_GLYPH_PADDING_P (*glyph)
17590 && !FRAME_WINDOW_P (it->f))
17592 /* A padding glyph that doesn't fit on this line.
17593 This means the whole character doesn't fit
17594 on the line. */
17595 if (row->reversed_p)
17596 unproduce_glyphs (it, row->used[TEXT_AREA]
17597 - n_glyphs_before);
17598 row->used[TEXT_AREA] = n_glyphs_before;
17600 /* Fill the rest of the row with continuation
17601 glyphs like in 20.x. */
17602 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17603 < row->glyphs[1 + TEXT_AREA])
17604 produce_special_glyphs (it, IT_CONTINUATION);
17606 row->continued_p = 1;
17607 it->current_x = x_before;
17608 it->continuation_lines_width += x_before;
17610 /* Restore the height to what it was before the
17611 element not fitting on the line. */
17612 it->max_ascent = ascent;
17613 it->max_descent = descent;
17614 it->max_phys_ascent = phys_ascent;
17615 it->max_phys_descent = phys_descent;
17617 else if (wrap_row_used > 0)
17619 back_to_wrap:
17620 if (row->reversed_p)
17621 unproduce_glyphs (it,
17622 row->used[TEXT_AREA] - wrap_row_used);
17623 *it = wrap_it;
17624 it->continuation_lines_width += wrap_x;
17625 row->used[TEXT_AREA] = wrap_row_used;
17626 row->ascent = wrap_row_ascent;
17627 row->height = wrap_row_height;
17628 row->phys_ascent = wrap_row_phys_ascent;
17629 row->phys_height = wrap_row_phys_height;
17630 row->extra_line_spacing = wrap_row_extra_line_spacing;
17631 min_pos = wrap_row_min_pos;
17632 min_bpos = wrap_row_min_bpos;
17633 max_pos = wrap_row_max_pos;
17634 max_bpos = wrap_row_max_bpos;
17635 row->continued_p = 1;
17636 row->ends_at_zv_p = 0;
17637 row->exact_window_width_line_p = 0;
17638 it->continuation_lines_width += x;
17640 /* Make sure that a non-default face is extended
17641 up to the right margin of the window. */
17642 extend_face_to_end_of_line (it);
17644 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17646 /* A TAB that extends past the right edge of the
17647 window. This produces a single glyph on
17648 window system frames. We leave the glyph in
17649 this row and let it fill the row, but don't
17650 consume the TAB. */
17651 it->continuation_lines_width += it->last_visible_x;
17652 row->ends_in_middle_of_char_p = 1;
17653 row->continued_p = 1;
17654 glyph->pixel_width = it->last_visible_x - x;
17655 it->starts_in_middle_of_char_p = 1;
17657 else
17659 /* Something other than a TAB that draws past
17660 the right edge of the window. Restore
17661 positions to values before the element. */
17662 if (row->reversed_p)
17663 unproduce_glyphs (it, row->used[TEXT_AREA]
17664 - (n_glyphs_before + i));
17665 row->used[TEXT_AREA] = n_glyphs_before + i;
17667 /* Display continuation glyphs. */
17668 if (!FRAME_WINDOW_P (it->f))
17669 produce_special_glyphs (it, IT_CONTINUATION);
17670 row->continued_p = 1;
17672 it->current_x = x_before;
17673 it->continuation_lines_width += x;
17674 extend_face_to_end_of_line (it);
17676 if (nglyphs > 1 && i > 0)
17678 row->ends_in_middle_of_char_p = 1;
17679 it->starts_in_middle_of_char_p = 1;
17682 /* Restore the height to what it was before the
17683 element not fitting on the line. */
17684 it->max_ascent = ascent;
17685 it->max_descent = descent;
17686 it->max_phys_ascent = phys_ascent;
17687 it->max_phys_descent = phys_descent;
17690 break;
17692 else if (new_x > it->first_visible_x)
17694 /* Increment number of glyphs actually displayed. */
17695 ++it->hpos;
17697 /* Record the maximum and minimum buffer positions
17698 seen so far in glyphs that will be displayed by
17699 this row. */
17700 if (it->bidi_p)
17701 RECORD_MAX_MIN_POS (it);
17703 if (x < it->first_visible_x)
17704 /* Glyph is partially visible, i.e. row starts at
17705 negative X position. */
17706 row->x = x - it->first_visible_x;
17708 else
17710 /* Glyph is completely off the left margin of the
17711 window. This should not happen because of the
17712 move_it_in_display_line at the start of this
17713 function, unless the text display area of the
17714 window is empty. */
17715 xassert (it->first_visible_x <= it->last_visible_x);
17719 row->ascent = max (row->ascent, it->max_ascent);
17720 row->height = max (row->height, it->max_ascent + it->max_descent);
17721 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17722 row->phys_height = max (row->phys_height,
17723 it->max_phys_ascent + it->max_phys_descent);
17724 row->extra_line_spacing = max (row->extra_line_spacing,
17725 it->max_extra_line_spacing);
17727 /* End of this display line if row is continued. */
17728 if (row->continued_p || row->ends_at_zv_p)
17729 break;
17732 at_end_of_line:
17733 /* Is this a line end? If yes, we're also done, after making
17734 sure that a non-default face is extended up to the right
17735 margin of the window. */
17736 if (ITERATOR_AT_END_OF_LINE_P (it))
17738 int used_before = row->used[TEXT_AREA];
17740 row->ends_in_newline_from_string_p = STRINGP (it->object);
17742 /* Add a space at the end of the line that is used to
17743 display the cursor there. */
17744 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17745 append_space_for_newline (it, 0);
17747 /* Extend the face to the end of the line. */
17748 extend_face_to_end_of_line (it);
17750 /* Make sure we have the position. */
17751 if (used_before == 0)
17752 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17754 /* Record the position of the newline, for use in
17755 find_row_edges. */
17756 it->eol_pos = it->current.pos;
17758 /* Consume the line end. This skips over invisible lines. */
17759 set_iterator_to_next (it, 1);
17760 it->continuation_lines_width = 0;
17761 break;
17764 /* Proceed with next display element. Note that this skips
17765 over lines invisible because of selective display. */
17766 set_iterator_to_next (it, 1);
17768 /* If we truncate lines, we are done when the last displayed
17769 glyphs reach past the right margin of the window. */
17770 if (it->line_wrap == TRUNCATE
17771 && (FRAME_WINDOW_P (it->f)
17772 ? (it->current_x >= it->last_visible_x)
17773 : (it->current_x > it->last_visible_x)))
17775 /* Maybe add truncation glyphs. */
17776 if (!FRAME_WINDOW_P (it->f))
17778 int i, n;
17780 if (!row->reversed_p)
17782 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17783 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17784 break;
17786 else
17788 for (i = 0; i < row->used[TEXT_AREA]; i++)
17789 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17790 break;
17791 /* Remove any padding glyphs at the front of ROW, to
17792 make room for the truncation glyphs we will be
17793 adding below. The loop below always inserts at
17794 least one truncation glyph, so also remove the
17795 last glyph added to ROW. */
17796 unproduce_glyphs (it, i + 1);
17797 /* Adjust i for the loop below. */
17798 i = row->used[TEXT_AREA] - (i + 1);
17801 for (n = row->used[TEXT_AREA]; i < n; ++i)
17803 row->used[TEXT_AREA] = i;
17804 produce_special_glyphs (it, IT_TRUNCATION);
17807 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17809 /* Don't truncate if we can overflow newline into fringe. */
17810 if (!get_next_display_element (it))
17812 it->continuation_lines_width = 0;
17813 row->ends_at_zv_p = 1;
17814 row->exact_window_width_line_p = 1;
17815 break;
17817 if (ITERATOR_AT_END_OF_LINE_P (it))
17819 row->exact_window_width_line_p = 1;
17820 goto at_end_of_line;
17824 row->truncated_on_right_p = 1;
17825 it->continuation_lines_width = 0;
17826 reseat_at_next_visible_line_start (it, 0);
17827 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17828 it->hpos = hpos_before;
17829 it->current_x = x_before;
17830 break;
17834 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17835 at the left window margin. */
17836 if (it->first_visible_x
17837 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17839 if (!FRAME_WINDOW_P (it->f))
17840 insert_left_trunc_glyphs (it);
17841 row->truncated_on_left_p = 1;
17844 /* Remember the position at which this line ends.
17846 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
17847 cannot be before the call to find_row_edges below, since that is
17848 where these positions are determined. */
17849 row->end = it->current;
17850 if (!it->bidi_p)
17852 row->minpos = row->start.pos;
17853 row->maxpos = row->end.pos;
17855 else
17857 /* ROW->minpos and ROW->maxpos must be the smallest and
17858 `1 + the largest' buffer positions in ROW. But if ROW was
17859 bidi-reordered, these two positions can be anywhere in the
17860 row, so we must determine them now. */
17861 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17864 /* If the start of this line is the overlay arrow-position, then
17865 mark this glyph row as the one containing the overlay arrow.
17866 This is clearly a mess with variable size fonts. It would be
17867 better to let it be displayed like cursors under X. */
17868 if ((row->displays_text_p || !overlay_arrow_seen)
17869 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17870 !NILP (overlay_arrow_string)))
17872 /* Overlay arrow in window redisplay is a fringe bitmap. */
17873 if (STRINGP (overlay_arrow_string))
17875 struct glyph_row *arrow_row
17876 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17877 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17878 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17879 struct glyph *p = row->glyphs[TEXT_AREA];
17880 struct glyph *p2, *end;
17882 /* Copy the arrow glyphs. */
17883 while (glyph < arrow_end)
17884 *p++ = *glyph++;
17886 /* Throw away padding glyphs. */
17887 p2 = p;
17888 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17889 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17890 ++p2;
17891 if (p2 > p)
17893 while (p2 < end)
17894 *p++ = *p2++;
17895 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17898 else
17900 xassert (INTEGERP (overlay_arrow_string));
17901 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17903 overlay_arrow_seen = 1;
17906 /* Compute pixel dimensions of this line. */
17907 compute_line_metrics (it);
17909 /* Record whether this row ends inside an ellipsis. */
17910 row->ends_in_ellipsis_p
17911 = (it->method == GET_FROM_DISPLAY_VECTOR
17912 && it->ellipsis_p);
17914 /* Save fringe bitmaps in this row. */
17915 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17916 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17917 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17918 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17920 it->left_user_fringe_bitmap = 0;
17921 it->left_user_fringe_face_id = 0;
17922 it->right_user_fringe_bitmap = 0;
17923 it->right_user_fringe_face_id = 0;
17925 /* Maybe set the cursor. */
17926 cvpos = it->w->cursor.vpos;
17927 if ((cvpos < 0
17928 /* In bidi-reordered rows, keep checking for proper cursor
17929 position even if one has been found already, because buffer
17930 positions in such rows change non-linearly with ROW->VPOS,
17931 when a line is continued. One exception: when we are at ZV,
17932 display cursor on the first suitable glyph row, since all
17933 the empty rows after that also have their position set to ZV. */
17934 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17935 lines' rows is implemented for bidi-reordered rows. */
17936 || (it->bidi_p
17937 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17938 && PT >= MATRIX_ROW_START_CHARPOS (row)
17939 && PT <= MATRIX_ROW_END_CHARPOS (row)
17940 && cursor_row_p (it->w, row))
17941 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17943 /* Highlight trailing whitespace. */
17944 if (!NILP (Vshow_trailing_whitespace))
17945 highlight_trailing_whitespace (it->f, it->glyph_row);
17947 /* Prepare for the next line. This line starts horizontally at (X
17948 HPOS) = (0 0). Vertical positions are incremented. As a
17949 convenience for the caller, IT->glyph_row is set to the next
17950 row to be used. */
17951 it->current_x = it->hpos = 0;
17952 it->current_y += row->height;
17953 SET_TEXT_POS (it->eol_pos, 0, 0);
17954 ++it->vpos;
17955 ++it->glyph_row;
17956 /* The next row should by default use the same value of the
17957 reversed_p flag as this one. set_iterator_to_next decides when
17958 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
17959 the flag accordingly. */
17960 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
17961 it->glyph_row->reversed_p = row->reversed_p;
17962 it->start = row->end;
17963 return row->displays_text_p;
17965 #undef RECORD_MAX_MIN_POS
17968 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
17969 Scurrent_bidi_paragraph_direction, 0, 1, 0,
17970 doc: /* Return paragraph direction at point in BUFFER.
17971 Value is either `left-to-right' or `right-to-left'.
17972 If BUFFER is omitted or nil, it defaults to the current buffer.
17974 Paragraph direction determines how the text in the paragraph is displayed.
17975 In left-to-right paragraphs, text begins at the left margin of the window
17976 and the reading direction is generally left to right. In right-to-left
17977 paragraphs, text begins at the right margin and is read from right to left.
17979 See also `bidi-paragraph-direction'. */)
17980 (Lisp_Object buffer)
17982 struct buffer *buf;
17983 struct buffer *old;
17985 if (NILP (buffer))
17986 buf = current_buffer;
17987 else
17989 CHECK_BUFFER (buffer);
17990 buf = XBUFFER (buffer);
17991 old = current_buffer;
17994 if (NILP (buf->bidi_display_reordering))
17995 return Qleft_to_right;
17996 else if (!NILP (buf->bidi_paragraph_direction))
17997 return buf->bidi_paragraph_direction;
17998 else
18000 /* Determine the direction from buffer text. We could try to
18001 use current_matrix if it is up to date, but this seems fast
18002 enough as it is. */
18003 struct bidi_it itb;
18004 EMACS_INT pos = BUF_PT (buf);
18005 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18006 int c;
18008 if (buf != current_buffer)
18009 set_buffer_temp (buf);
18010 /* bidi_paragraph_init finds the base direction of the paragraph
18011 by searching forward from paragraph start. We need the base
18012 direction of the current or _previous_ paragraph, so we need
18013 to make sure we are within that paragraph. To that end, find
18014 the previous non-empty line. */
18015 if (pos >= ZV && pos > BEGV)
18017 pos--;
18018 bytepos = CHAR_TO_BYTE (pos);
18020 while ((c = FETCH_BYTE (bytepos)) == '\n'
18021 || c == ' ' || c == '\t' || c == '\f')
18023 if (bytepos <= BEGV_BYTE)
18024 break;
18025 bytepos--;
18026 pos--;
18028 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18029 bytepos--;
18030 itb.charpos = pos;
18031 itb.bytepos = bytepos;
18032 itb.first_elt = 1;
18033 itb.separator_limit = -1;
18034 itb.paragraph_dir = NEUTRAL_DIR;
18036 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18037 if (buf != current_buffer)
18038 set_buffer_temp (old);
18039 switch (itb.paragraph_dir)
18041 case L2R:
18042 return Qleft_to_right;
18043 break;
18044 case R2L:
18045 return Qright_to_left;
18046 break;
18047 default:
18048 abort ();
18055 /***********************************************************************
18056 Menu Bar
18057 ***********************************************************************/
18059 /* Redisplay the menu bar in the frame for window W.
18061 The menu bar of X frames that don't have X toolkit support is
18062 displayed in a special window W->frame->menu_bar_window.
18064 The menu bar of terminal frames is treated specially as far as
18065 glyph matrices are concerned. Menu bar lines are not part of
18066 windows, so the update is done directly on the frame matrix rows
18067 for the menu bar. */
18069 static void
18070 display_menu_bar (struct window *w)
18072 struct frame *f = XFRAME (WINDOW_FRAME (w));
18073 struct it it;
18074 Lisp_Object items;
18075 int i;
18077 /* Don't do all this for graphical frames. */
18078 #ifdef HAVE_NTGUI
18079 if (FRAME_W32_P (f))
18080 return;
18081 #endif
18082 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18083 if (FRAME_X_P (f))
18084 return;
18085 #endif
18087 #ifdef HAVE_NS
18088 if (FRAME_NS_P (f))
18089 return;
18090 #endif /* HAVE_NS */
18092 #ifdef USE_X_TOOLKIT
18093 xassert (!FRAME_WINDOW_P (f));
18094 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18095 it.first_visible_x = 0;
18096 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18097 #else /* not USE_X_TOOLKIT */
18098 if (FRAME_WINDOW_P (f))
18100 /* Menu bar lines are displayed in the desired matrix of the
18101 dummy window menu_bar_window. */
18102 struct window *menu_w;
18103 xassert (WINDOWP (f->menu_bar_window));
18104 menu_w = XWINDOW (f->menu_bar_window);
18105 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18106 MENU_FACE_ID);
18107 it.first_visible_x = 0;
18108 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18110 else
18112 /* This is a TTY frame, i.e. character hpos/vpos are used as
18113 pixel x/y. */
18114 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18115 MENU_FACE_ID);
18116 it.first_visible_x = 0;
18117 it.last_visible_x = FRAME_COLS (f);
18119 #endif /* not USE_X_TOOLKIT */
18121 if (! mode_line_inverse_video)
18122 /* Force the menu-bar to be displayed in the default face. */
18123 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18125 /* Clear all rows of the menu bar. */
18126 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18128 struct glyph_row *row = it.glyph_row + i;
18129 clear_glyph_row (row);
18130 row->enabled_p = 1;
18131 row->full_width_p = 1;
18134 /* Display all items of the menu bar. */
18135 items = FRAME_MENU_BAR_ITEMS (it.f);
18136 for (i = 0; i < XVECTOR (items)->size; i += 4)
18138 Lisp_Object string;
18140 /* Stop at nil string. */
18141 string = AREF (items, i + 1);
18142 if (NILP (string))
18143 break;
18145 /* Remember where item was displayed. */
18146 ASET (items, i + 3, make_number (it.hpos));
18148 /* Display the item, pad with one space. */
18149 if (it.current_x < it.last_visible_x)
18150 display_string (NULL, string, Qnil, 0, 0, &it,
18151 SCHARS (string) + 1, 0, 0, -1);
18154 /* Fill out the line with spaces. */
18155 if (it.current_x < it.last_visible_x)
18156 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18158 /* Compute the total height of the lines. */
18159 compute_line_metrics (&it);
18164 /***********************************************************************
18165 Mode Line
18166 ***********************************************************************/
18168 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18169 FORCE is non-zero, redisplay mode lines unconditionally.
18170 Otherwise, redisplay only mode lines that are garbaged. Value is
18171 the number of windows whose mode lines were redisplayed. */
18173 static int
18174 redisplay_mode_lines (Lisp_Object window, int force)
18176 int nwindows = 0;
18178 while (!NILP (window))
18180 struct window *w = XWINDOW (window);
18182 if (WINDOWP (w->hchild))
18183 nwindows += redisplay_mode_lines (w->hchild, force);
18184 else if (WINDOWP (w->vchild))
18185 nwindows += redisplay_mode_lines (w->vchild, force);
18186 else if (force
18187 || FRAME_GARBAGED_P (XFRAME (w->frame))
18188 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18190 struct text_pos lpoint;
18191 struct buffer *old = current_buffer;
18193 /* Set the window's buffer for the mode line display. */
18194 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18195 set_buffer_internal_1 (XBUFFER (w->buffer));
18197 /* Point refers normally to the selected window. For any
18198 other window, set up appropriate value. */
18199 if (!EQ (window, selected_window))
18201 struct text_pos pt;
18203 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18204 if (CHARPOS (pt) < BEGV)
18205 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18206 else if (CHARPOS (pt) > (ZV - 1))
18207 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18208 else
18209 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18212 /* Display mode lines. */
18213 clear_glyph_matrix (w->desired_matrix);
18214 if (display_mode_lines (w))
18216 ++nwindows;
18217 w->must_be_updated_p = 1;
18220 /* Restore old settings. */
18221 set_buffer_internal_1 (old);
18222 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18225 window = w->next;
18228 return nwindows;
18232 /* Display the mode and/or header line of window W. Value is the
18233 sum number of mode lines and header lines displayed. */
18235 static int
18236 display_mode_lines (struct window *w)
18238 Lisp_Object old_selected_window, old_selected_frame;
18239 int n = 0;
18241 old_selected_frame = selected_frame;
18242 selected_frame = w->frame;
18243 old_selected_window = selected_window;
18244 XSETWINDOW (selected_window, w);
18246 /* These will be set while the mode line specs are processed. */
18247 line_number_displayed = 0;
18248 w->column_number_displayed = Qnil;
18250 if (WINDOW_WANTS_MODELINE_P (w))
18252 struct window *sel_w = XWINDOW (old_selected_window);
18254 /* Select mode line face based on the real selected window. */
18255 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18256 current_buffer->mode_line_format);
18257 ++n;
18260 if (WINDOW_WANTS_HEADER_LINE_P (w))
18262 display_mode_line (w, HEADER_LINE_FACE_ID,
18263 current_buffer->header_line_format);
18264 ++n;
18267 selected_frame = old_selected_frame;
18268 selected_window = old_selected_window;
18269 return n;
18273 /* Display mode or header line of window W. FACE_ID specifies which
18274 line to display; it is either MODE_LINE_FACE_ID or
18275 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18276 display. Value is the pixel height of the mode/header line
18277 displayed. */
18279 static int
18280 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18282 struct it it;
18283 struct face *face;
18284 int count = SPECPDL_INDEX ();
18286 init_iterator (&it, w, -1, -1, NULL, face_id);
18287 /* Don't extend on a previously drawn mode-line.
18288 This may happen if called from pos_visible_p. */
18289 it.glyph_row->enabled_p = 0;
18290 prepare_desired_row (it.glyph_row);
18292 it.glyph_row->mode_line_p = 1;
18294 if (! mode_line_inverse_video)
18295 /* Force the mode-line to be displayed in the default face. */
18296 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18298 record_unwind_protect (unwind_format_mode_line,
18299 format_mode_line_unwind_data (NULL, Qnil, 0));
18301 mode_line_target = MODE_LINE_DISPLAY;
18303 /* Temporarily make frame's keyboard the current kboard so that
18304 kboard-local variables in the mode_line_format will get the right
18305 values. */
18306 push_kboard (FRAME_KBOARD (it.f));
18307 record_unwind_save_match_data ();
18308 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18309 pop_kboard ();
18311 unbind_to (count, Qnil);
18313 /* Fill up with spaces. */
18314 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18316 compute_line_metrics (&it);
18317 it.glyph_row->full_width_p = 1;
18318 it.glyph_row->continued_p = 0;
18319 it.glyph_row->truncated_on_left_p = 0;
18320 it.glyph_row->truncated_on_right_p = 0;
18322 /* Make a 3D mode-line have a shadow at its right end. */
18323 face = FACE_FROM_ID (it.f, face_id);
18324 extend_face_to_end_of_line (&it);
18325 if (face->box != FACE_NO_BOX)
18327 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18328 + it.glyph_row->used[TEXT_AREA] - 1);
18329 last->right_box_line_p = 1;
18332 return it.glyph_row->height;
18335 /* Move element ELT in LIST to the front of LIST.
18336 Return the updated list. */
18338 static Lisp_Object
18339 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18341 register Lisp_Object tail, prev;
18342 register Lisp_Object tem;
18344 tail = list;
18345 prev = Qnil;
18346 while (CONSP (tail))
18348 tem = XCAR (tail);
18350 if (EQ (elt, tem))
18352 /* Splice out the link TAIL. */
18353 if (NILP (prev))
18354 list = XCDR (tail);
18355 else
18356 Fsetcdr (prev, XCDR (tail));
18358 /* Now make it the first. */
18359 Fsetcdr (tail, list);
18360 return tail;
18362 else
18363 prev = tail;
18364 tail = XCDR (tail);
18365 QUIT;
18368 /* Not found--return unchanged LIST. */
18369 return list;
18372 /* Contribute ELT to the mode line for window IT->w. How it
18373 translates into text depends on its data type.
18375 IT describes the display environment in which we display, as usual.
18377 DEPTH is the depth in recursion. It is used to prevent
18378 infinite recursion here.
18380 FIELD_WIDTH is the number of characters the display of ELT should
18381 occupy in the mode line, and PRECISION is the maximum number of
18382 characters to display from ELT's representation. See
18383 display_string for details.
18385 Returns the hpos of the end of the text generated by ELT.
18387 PROPS is a property list to add to any string we encounter.
18389 If RISKY is nonzero, remove (disregard) any properties in any string
18390 we encounter, and ignore :eval and :propertize.
18392 The global variable `mode_line_target' determines whether the
18393 output is passed to `store_mode_line_noprop',
18394 `store_mode_line_string', or `display_string'. */
18396 static int
18397 display_mode_element (struct it *it, int depth, int field_width, int precision,
18398 Lisp_Object elt, Lisp_Object props, int risky)
18400 int n = 0, field, prec;
18401 int literal = 0;
18403 tail_recurse:
18404 if (depth > 100)
18405 elt = build_string ("*too-deep*");
18407 depth++;
18409 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18411 case Lisp_String:
18413 /* A string: output it and check for %-constructs within it. */
18414 unsigned char c;
18415 EMACS_INT offset = 0;
18417 if (SCHARS (elt) > 0
18418 && (!NILP (props) || risky))
18420 Lisp_Object oprops, aelt;
18421 oprops = Ftext_properties_at (make_number (0), elt);
18423 /* If the starting string's properties are not what
18424 we want, translate the string. Also, if the string
18425 is risky, do that anyway. */
18427 if (NILP (Fequal (props, oprops)) || risky)
18429 /* If the starting string has properties,
18430 merge the specified ones onto the existing ones. */
18431 if (! NILP (oprops) && !risky)
18433 Lisp_Object tem;
18435 oprops = Fcopy_sequence (oprops);
18436 tem = props;
18437 while (CONSP (tem))
18439 oprops = Fplist_put (oprops, XCAR (tem),
18440 XCAR (XCDR (tem)));
18441 tem = XCDR (XCDR (tem));
18443 props = oprops;
18446 aelt = Fassoc (elt, mode_line_proptrans_alist);
18447 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18449 /* AELT is what we want. Move it to the front
18450 without consing. */
18451 elt = XCAR (aelt);
18452 mode_line_proptrans_alist
18453 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18455 else
18457 Lisp_Object tem;
18459 /* If AELT has the wrong props, it is useless.
18460 so get rid of it. */
18461 if (! NILP (aelt))
18462 mode_line_proptrans_alist
18463 = Fdelq (aelt, mode_line_proptrans_alist);
18465 elt = Fcopy_sequence (elt);
18466 Fset_text_properties (make_number (0), Flength (elt),
18467 props, elt);
18468 /* Add this item to mode_line_proptrans_alist. */
18469 mode_line_proptrans_alist
18470 = Fcons (Fcons (elt, props),
18471 mode_line_proptrans_alist);
18472 /* Truncate mode_line_proptrans_alist
18473 to at most 50 elements. */
18474 tem = Fnthcdr (make_number (50),
18475 mode_line_proptrans_alist);
18476 if (! NILP (tem))
18477 XSETCDR (tem, Qnil);
18482 offset = 0;
18484 if (literal)
18486 prec = precision - n;
18487 switch (mode_line_target)
18489 case MODE_LINE_NOPROP:
18490 case MODE_LINE_TITLE:
18491 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18492 break;
18493 case MODE_LINE_STRING:
18494 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18495 break;
18496 case MODE_LINE_DISPLAY:
18497 n += display_string (NULL, elt, Qnil, 0, 0, it,
18498 0, prec, 0, STRING_MULTIBYTE (elt));
18499 break;
18502 break;
18505 /* Handle the non-literal case. */
18507 while ((precision <= 0 || n < precision)
18508 && SREF (elt, offset) != 0
18509 && (mode_line_target != MODE_LINE_DISPLAY
18510 || it->current_x < it->last_visible_x))
18512 EMACS_INT last_offset = offset;
18514 /* Advance to end of string or next format specifier. */
18515 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18518 if (offset - 1 != last_offset)
18520 EMACS_INT nchars, nbytes;
18522 /* Output to end of string or up to '%'. Field width
18523 is length of string. Don't output more than
18524 PRECISION allows us. */
18525 offset--;
18527 prec = c_string_width (SDATA (elt) + last_offset,
18528 offset - last_offset, precision - n,
18529 &nchars, &nbytes);
18531 switch (mode_line_target)
18533 case MODE_LINE_NOPROP:
18534 case MODE_LINE_TITLE:
18535 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18536 break;
18537 case MODE_LINE_STRING:
18539 EMACS_INT bytepos = last_offset;
18540 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18541 EMACS_INT endpos = (precision <= 0
18542 ? string_byte_to_char (elt, offset)
18543 : charpos + nchars);
18545 n += store_mode_line_string (NULL,
18546 Fsubstring (elt, make_number (charpos),
18547 make_number (endpos)),
18548 0, 0, 0, Qnil);
18550 break;
18551 case MODE_LINE_DISPLAY:
18553 EMACS_INT bytepos = last_offset;
18554 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
18556 if (precision <= 0)
18557 nchars = string_byte_to_char (elt, offset) - charpos;
18558 n += display_string (NULL, elt, Qnil, 0, charpos,
18559 it, 0, nchars, 0,
18560 STRING_MULTIBYTE (elt));
18562 break;
18565 else /* c == '%' */
18567 EMACS_INT percent_position = offset;
18569 /* Get the specified minimum width. Zero means
18570 don't pad. */
18571 field = 0;
18572 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18573 field = field * 10 + c - '0';
18575 /* Don't pad beyond the total padding allowed. */
18576 if (field_width - n > 0 && field > field_width - n)
18577 field = field_width - n;
18579 /* Note that either PRECISION <= 0 or N < PRECISION. */
18580 prec = precision - n;
18582 if (c == 'M')
18583 n += display_mode_element (it, depth, field, prec,
18584 Vglobal_mode_string, props,
18585 risky);
18586 else if (c != 0)
18588 int multibyte;
18589 EMACS_INT bytepos, charpos;
18590 const unsigned char *spec;
18591 Lisp_Object string;
18593 bytepos = percent_position;
18594 charpos = (STRING_MULTIBYTE (elt)
18595 ? string_byte_to_char (elt, bytepos)
18596 : bytepos);
18597 spec = decode_mode_spec (it->w, c, field, prec, &string);
18598 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18600 switch (mode_line_target)
18602 case MODE_LINE_NOPROP:
18603 case MODE_LINE_TITLE:
18604 n += store_mode_line_noprop (spec, field, prec);
18605 break;
18606 case MODE_LINE_STRING:
18608 int len = strlen (spec);
18609 Lisp_Object tem = make_string (spec, len);
18610 props = Ftext_properties_at (make_number (charpos), elt);
18611 /* Should only keep face property in props */
18612 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18614 break;
18615 case MODE_LINE_DISPLAY:
18617 int nglyphs_before, nwritten;
18619 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18620 nwritten = display_string (spec, string, elt,
18621 charpos, 0, it,
18622 field, prec, 0,
18623 multibyte);
18625 /* Assign to the glyphs written above the
18626 string where the `%x' came from, position
18627 of the `%'. */
18628 if (nwritten > 0)
18630 struct glyph *glyph
18631 = (it->glyph_row->glyphs[TEXT_AREA]
18632 + nglyphs_before);
18633 int i;
18635 for (i = 0; i < nwritten; ++i)
18637 glyph[i].object = elt;
18638 glyph[i].charpos = charpos;
18641 n += nwritten;
18644 break;
18647 else /* c == 0 */
18648 break;
18652 break;
18654 case Lisp_Symbol:
18655 /* A symbol: process the value of the symbol recursively
18656 as if it appeared here directly. Avoid error if symbol void.
18657 Special case: if value of symbol is a string, output the string
18658 literally. */
18660 register Lisp_Object tem;
18662 /* If the variable is not marked as risky to set
18663 then its contents are risky to use. */
18664 if (NILP (Fget (elt, Qrisky_local_variable)))
18665 risky = 1;
18667 tem = Fboundp (elt);
18668 if (!NILP (tem))
18670 tem = Fsymbol_value (elt);
18671 /* If value is a string, output that string literally:
18672 don't check for % within it. */
18673 if (STRINGP (tem))
18674 literal = 1;
18676 if (!EQ (tem, elt))
18678 /* Give up right away for nil or t. */
18679 elt = tem;
18680 goto tail_recurse;
18684 break;
18686 case Lisp_Cons:
18688 register Lisp_Object car, tem;
18690 /* A cons cell: five distinct cases.
18691 If first element is :eval or :propertize, do something special.
18692 If first element is a string or a cons, process all the elements
18693 and effectively concatenate them.
18694 If first element is a negative number, truncate displaying cdr to
18695 at most that many characters. If positive, pad (with spaces)
18696 to at least that many characters.
18697 If first element is a symbol, process the cadr or caddr recursively
18698 according to whether the symbol's value is non-nil or nil. */
18699 car = XCAR (elt);
18700 if (EQ (car, QCeval))
18702 /* An element of the form (:eval FORM) means evaluate FORM
18703 and use the result as mode line elements. */
18705 if (risky)
18706 break;
18708 if (CONSP (XCDR (elt)))
18710 Lisp_Object spec;
18711 spec = safe_eval (XCAR (XCDR (elt)));
18712 n += display_mode_element (it, depth, field_width - n,
18713 precision - n, spec, props,
18714 risky);
18717 else if (EQ (car, QCpropertize))
18719 /* An element of the form (:propertize ELT PROPS...)
18720 means display ELT but applying properties PROPS. */
18722 if (risky)
18723 break;
18725 if (CONSP (XCDR (elt)))
18726 n += display_mode_element (it, depth, field_width - n,
18727 precision - n, XCAR (XCDR (elt)),
18728 XCDR (XCDR (elt)), risky);
18730 else if (SYMBOLP (car))
18732 tem = Fboundp (car);
18733 elt = XCDR (elt);
18734 if (!CONSP (elt))
18735 goto invalid;
18736 /* elt is now the cdr, and we know it is a cons cell.
18737 Use its car if CAR has a non-nil value. */
18738 if (!NILP (tem))
18740 tem = Fsymbol_value (car);
18741 if (!NILP (tem))
18743 elt = XCAR (elt);
18744 goto tail_recurse;
18747 /* Symbol's value is nil (or symbol is unbound)
18748 Get the cddr of the original list
18749 and if possible find the caddr and use that. */
18750 elt = XCDR (elt);
18751 if (NILP (elt))
18752 break;
18753 else if (!CONSP (elt))
18754 goto invalid;
18755 elt = XCAR (elt);
18756 goto tail_recurse;
18758 else if (INTEGERP (car))
18760 register int lim = XINT (car);
18761 elt = XCDR (elt);
18762 if (lim < 0)
18764 /* Negative int means reduce maximum width. */
18765 if (precision <= 0)
18766 precision = -lim;
18767 else
18768 precision = min (precision, -lim);
18770 else if (lim > 0)
18772 /* Padding specified. Don't let it be more than
18773 current maximum. */
18774 if (precision > 0)
18775 lim = min (precision, lim);
18777 /* If that's more padding than already wanted, queue it.
18778 But don't reduce padding already specified even if
18779 that is beyond the current truncation point. */
18780 field_width = max (lim, field_width);
18782 goto tail_recurse;
18784 else if (STRINGP (car) || CONSP (car))
18786 Lisp_Object halftail = elt;
18787 int len = 0;
18789 while (CONSP (elt)
18790 && (precision <= 0 || n < precision))
18792 n += display_mode_element (it, depth,
18793 /* Do padding only after the last
18794 element in the list. */
18795 (! CONSP (XCDR (elt))
18796 ? field_width - n
18797 : 0),
18798 precision - n, XCAR (elt),
18799 props, risky);
18800 elt = XCDR (elt);
18801 len++;
18802 if ((len & 1) == 0)
18803 halftail = XCDR (halftail);
18804 /* Check for cycle. */
18805 if (EQ (halftail, elt))
18806 break;
18810 break;
18812 default:
18813 invalid:
18814 elt = build_string ("*invalid*");
18815 goto tail_recurse;
18818 /* Pad to FIELD_WIDTH. */
18819 if (field_width > 0 && n < field_width)
18821 switch (mode_line_target)
18823 case MODE_LINE_NOPROP:
18824 case MODE_LINE_TITLE:
18825 n += store_mode_line_noprop ("", field_width - n, 0);
18826 break;
18827 case MODE_LINE_STRING:
18828 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18829 break;
18830 case MODE_LINE_DISPLAY:
18831 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18832 0, 0, 0);
18833 break;
18837 return n;
18840 /* Store a mode-line string element in mode_line_string_list.
18842 If STRING is non-null, display that C string. Otherwise, the Lisp
18843 string LISP_STRING is displayed.
18845 FIELD_WIDTH is the minimum number of output glyphs to produce.
18846 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18847 with spaces. FIELD_WIDTH <= 0 means don't pad.
18849 PRECISION is the maximum number of characters to output from
18850 STRING. PRECISION <= 0 means don't truncate the string.
18852 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18853 properties to the string.
18855 PROPS are the properties to add to the string.
18856 The mode_line_string_face face property is always added to the string.
18859 static int
18860 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
18861 int field_width, int precision, Lisp_Object props)
18863 EMACS_INT len;
18864 int n = 0;
18866 if (string != NULL)
18868 len = strlen (string);
18869 if (precision > 0 && len > precision)
18870 len = precision;
18871 lisp_string = make_string (string, len);
18872 if (NILP (props))
18873 props = mode_line_string_face_prop;
18874 else if (!NILP (mode_line_string_face))
18876 Lisp_Object face = Fplist_get (props, Qface);
18877 props = Fcopy_sequence (props);
18878 if (NILP (face))
18879 face = mode_line_string_face;
18880 else
18881 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18882 props = Fplist_put (props, Qface, face);
18884 Fadd_text_properties (make_number (0), make_number (len),
18885 props, lisp_string);
18887 else
18889 len = XFASTINT (Flength (lisp_string));
18890 if (precision > 0 && len > precision)
18892 len = precision;
18893 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18894 precision = -1;
18896 if (!NILP (mode_line_string_face))
18898 Lisp_Object face;
18899 if (NILP (props))
18900 props = Ftext_properties_at (make_number (0), lisp_string);
18901 face = Fplist_get (props, Qface);
18902 if (NILP (face))
18903 face = mode_line_string_face;
18904 else
18905 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18906 props = Fcons (Qface, Fcons (face, Qnil));
18907 if (copy_string)
18908 lisp_string = Fcopy_sequence (lisp_string);
18910 if (!NILP (props))
18911 Fadd_text_properties (make_number (0), make_number (len),
18912 props, lisp_string);
18915 if (len > 0)
18917 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18918 n += len;
18921 if (field_width > len)
18923 field_width -= len;
18924 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18925 if (!NILP (props))
18926 Fadd_text_properties (make_number (0), make_number (field_width),
18927 props, lisp_string);
18928 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18929 n += field_width;
18932 return n;
18936 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18937 1, 4, 0,
18938 doc: /* Format a string out of a mode line format specification.
18939 First arg FORMAT specifies the mode line format (see `mode-line-format'
18940 for details) to use.
18942 Optional second arg FACE specifies the face property to put
18943 on all characters for which no face is specified.
18944 The value t means whatever face the window's mode line currently uses
18945 \(either `mode-line' or `mode-line-inactive', depending).
18946 A value of nil means the default is no face property.
18947 If FACE is an integer, the value string has no text properties.
18949 Optional third and fourth args WINDOW and BUFFER specify the window
18950 and buffer to use as the context for the formatting (defaults
18951 are the selected window and the window's buffer). */)
18952 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
18954 struct it it;
18955 int len;
18956 struct window *w;
18957 struct buffer *old_buffer = NULL;
18958 int face_id = -1;
18959 int no_props = INTEGERP (face);
18960 int count = SPECPDL_INDEX ();
18961 Lisp_Object str;
18962 int string_start = 0;
18964 if (NILP (window))
18965 window = selected_window;
18966 CHECK_WINDOW (window);
18967 w = XWINDOW (window);
18969 if (NILP (buffer))
18970 buffer = w->buffer;
18971 CHECK_BUFFER (buffer);
18973 /* Make formatting the modeline a non-op when noninteractive, otherwise
18974 there will be problems later caused by a partially initialized frame. */
18975 if (NILP (format) || noninteractive)
18976 return empty_unibyte_string;
18978 if (no_props)
18979 face = Qnil;
18981 if (!NILP (face))
18983 if (EQ (face, Qt))
18984 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18985 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18988 if (face_id < 0)
18989 face_id = DEFAULT_FACE_ID;
18991 if (XBUFFER (buffer) != current_buffer)
18992 old_buffer = current_buffer;
18994 /* Save things including mode_line_proptrans_alist,
18995 and set that to nil so that we don't alter the outer value. */
18996 record_unwind_protect (unwind_format_mode_line,
18997 format_mode_line_unwind_data
18998 (old_buffer, selected_window, 1));
18999 mode_line_proptrans_alist = Qnil;
19001 Fselect_window (window, Qt);
19002 if (old_buffer)
19003 set_buffer_internal_1 (XBUFFER (buffer));
19005 init_iterator (&it, w, -1, -1, NULL, face_id);
19007 if (no_props)
19009 mode_line_target = MODE_LINE_NOPROP;
19010 mode_line_string_face_prop = Qnil;
19011 mode_line_string_list = Qnil;
19012 string_start = MODE_LINE_NOPROP_LEN (0);
19014 else
19016 mode_line_target = MODE_LINE_STRING;
19017 mode_line_string_list = Qnil;
19018 mode_line_string_face = face;
19019 mode_line_string_face_prop
19020 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19023 push_kboard (FRAME_KBOARD (it.f));
19024 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19025 pop_kboard ();
19027 if (no_props)
19029 len = MODE_LINE_NOPROP_LEN (string_start);
19030 str = make_string (mode_line_noprop_buf + string_start, len);
19032 else
19034 mode_line_string_list = Fnreverse (mode_line_string_list);
19035 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19036 empty_unibyte_string);
19039 unbind_to (count, Qnil);
19040 return str;
19043 /* Write a null-terminated, right justified decimal representation of
19044 the positive integer D to BUF using a minimal field width WIDTH. */
19046 static void
19047 pint2str (register char *buf, register int width, register int d)
19049 register char *p = buf;
19051 if (d <= 0)
19052 *p++ = '0';
19053 else
19055 while (d > 0)
19057 *p++ = d % 10 + '0';
19058 d /= 10;
19062 for (width -= (int) (p - buf); width > 0; --width)
19063 *p++ = ' ';
19064 *p-- = '\0';
19065 while (p > buf)
19067 d = *buf;
19068 *buf++ = *p;
19069 *p-- = d;
19073 /* Write a null-terminated, right justified decimal and "human
19074 readable" representation of the nonnegative integer D to BUF using
19075 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19077 static const char power_letter[] =
19079 0, /* not used */
19080 'k', /* kilo */
19081 'M', /* mega */
19082 'G', /* giga */
19083 'T', /* tera */
19084 'P', /* peta */
19085 'E', /* exa */
19086 'Z', /* zetta */
19087 'Y' /* yotta */
19090 static void
19091 pint2hrstr (char *buf, int width, int d)
19093 /* We aim to represent the nonnegative integer D as
19094 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19095 int quotient = d;
19096 int remainder = 0;
19097 /* -1 means: do not use TENTHS. */
19098 int tenths = -1;
19099 int exponent = 0;
19101 /* Length of QUOTIENT.TENTHS as a string. */
19102 int length;
19104 char * psuffix;
19105 char * p;
19107 if (1000 <= quotient)
19109 /* Scale to the appropriate EXPONENT. */
19112 remainder = quotient % 1000;
19113 quotient /= 1000;
19114 exponent++;
19116 while (1000 <= quotient);
19118 /* Round to nearest and decide whether to use TENTHS or not. */
19119 if (quotient <= 9)
19121 tenths = remainder / 100;
19122 if (50 <= remainder % 100)
19124 if (tenths < 9)
19125 tenths++;
19126 else
19128 quotient++;
19129 if (quotient == 10)
19130 tenths = -1;
19131 else
19132 tenths = 0;
19136 else
19137 if (500 <= remainder)
19139 if (quotient < 999)
19140 quotient++;
19141 else
19143 quotient = 1;
19144 exponent++;
19145 tenths = 0;
19150 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19151 if (tenths == -1 && quotient <= 99)
19152 if (quotient <= 9)
19153 length = 1;
19154 else
19155 length = 2;
19156 else
19157 length = 3;
19158 p = psuffix = buf + max (width, length);
19160 /* Print EXPONENT. */
19161 if (exponent)
19162 *psuffix++ = power_letter[exponent];
19163 *psuffix = '\0';
19165 /* Print TENTHS. */
19166 if (tenths >= 0)
19168 *--p = '0' + tenths;
19169 *--p = '.';
19172 /* Print QUOTIENT. */
19175 int digit = quotient % 10;
19176 *--p = '0' + digit;
19178 while ((quotient /= 10) != 0);
19180 /* Print leading spaces. */
19181 while (buf < p)
19182 *--p = ' ';
19185 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19186 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19187 type of CODING_SYSTEM. Return updated pointer into BUF. */
19189 static unsigned char invalid_eol_type[] = "(*invalid*)";
19191 static char *
19192 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19194 Lisp_Object val;
19195 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19196 const unsigned char *eol_str;
19197 int eol_str_len;
19198 /* The EOL conversion we are using. */
19199 Lisp_Object eoltype;
19201 val = CODING_SYSTEM_SPEC (coding_system);
19202 eoltype = Qnil;
19204 if (!VECTORP (val)) /* Not yet decided. */
19206 if (multibyte)
19207 *buf++ = '-';
19208 if (eol_flag)
19209 eoltype = eol_mnemonic_undecided;
19210 /* Don't mention EOL conversion if it isn't decided. */
19212 else
19214 Lisp_Object attrs;
19215 Lisp_Object eolvalue;
19217 attrs = AREF (val, 0);
19218 eolvalue = AREF (val, 2);
19220 if (multibyte)
19221 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19223 if (eol_flag)
19225 /* The EOL conversion that is normal on this system. */
19227 if (NILP (eolvalue)) /* Not yet decided. */
19228 eoltype = eol_mnemonic_undecided;
19229 else if (VECTORP (eolvalue)) /* Not yet decided. */
19230 eoltype = eol_mnemonic_undecided;
19231 else /* eolvalue is Qunix, Qdos, or Qmac. */
19232 eoltype = (EQ (eolvalue, Qunix)
19233 ? eol_mnemonic_unix
19234 : (EQ (eolvalue, Qdos) == 1
19235 ? eol_mnemonic_dos : eol_mnemonic_mac));
19239 if (eol_flag)
19241 /* Mention the EOL conversion if it is not the usual one. */
19242 if (STRINGP (eoltype))
19244 eol_str = SDATA (eoltype);
19245 eol_str_len = SBYTES (eoltype);
19247 else if (CHARACTERP (eoltype))
19249 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19250 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19251 eol_str = tmp;
19253 else
19255 eol_str = invalid_eol_type;
19256 eol_str_len = sizeof (invalid_eol_type) - 1;
19258 memcpy (buf, eol_str, eol_str_len);
19259 buf += eol_str_len;
19262 return buf;
19265 /* Return a string for the output of a mode line %-spec for window W,
19266 generated by character C. PRECISION >= 0 means don't return a
19267 string longer than that value. FIELD_WIDTH > 0 means pad the
19268 string returned with spaces to that value. Return a Lisp string in
19269 *STRING if the resulting string is taken from that Lisp string.
19271 Note we operate on the current buffer for most purposes,
19272 the exception being w->base_line_pos. */
19274 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19276 static const char *
19277 decode_mode_spec (struct window *w, register int c, int field_width,
19278 int precision, Lisp_Object *string)
19280 Lisp_Object obj;
19281 struct frame *f = XFRAME (WINDOW_FRAME (w));
19282 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19283 struct buffer *b = current_buffer;
19285 obj = Qnil;
19286 *string = Qnil;
19288 switch (c)
19290 case '*':
19291 if (!NILP (b->read_only))
19292 return "%";
19293 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19294 return "*";
19295 return "-";
19297 case '+':
19298 /* This differs from %* only for a modified read-only buffer. */
19299 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19300 return "*";
19301 if (!NILP (b->read_only))
19302 return "%";
19303 return "-";
19305 case '&':
19306 /* This differs from %* in ignoring read-only-ness. */
19307 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19308 return "*";
19309 return "-";
19311 case '%':
19312 return "%";
19314 case '[':
19316 int i;
19317 char *p;
19319 if (command_loop_level > 5)
19320 return "[[[... ";
19321 p = decode_mode_spec_buf;
19322 for (i = 0; i < command_loop_level; i++)
19323 *p++ = '[';
19324 *p = 0;
19325 return decode_mode_spec_buf;
19328 case ']':
19330 int i;
19331 char *p;
19333 if (command_loop_level > 5)
19334 return " ...]]]";
19335 p = decode_mode_spec_buf;
19336 for (i = 0; i < command_loop_level; i++)
19337 *p++ = ']';
19338 *p = 0;
19339 return decode_mode_spec_buf;
19342 case '-':
19344 register int i;
19346 /* Let lots_of_dashes be a string of infinite length. */
19347 if (mode_line_target == MODE_LINE_NOPROP ||
19348 mode_line_target == MODE_LINE_STRING)
19349 return "--";
19350 if (field_width <= 0
19351 || field_width > sizeof (lots_of_dashes))
19353 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19354 decode_mode_spec_buf[i] = '-';
19355 decode_mode_spec_buf[i] = '\0';
19356 return decode_mode_spec_buf;
19358 else
19359 return lots_of_dashes;
19362 case 'b':
19363 obj = b->name;
19364 break;
19366 case 'c':
19367 /* %c and %l are ignored in `frame-title-format'.
19368 (In redisplay_internal, the frame title is drawn _before_ the
19369 windows are updated, so the stuff which depends on actual
19370 window contents (such as %l) may fail to render properly, or
19371 even crash emacs.) */
19372 if (mode_line_target == MODE_LINE_TITLE)
19373 return "";
19374 else
19376 int col = (int) current_column (); /* iftc */
19377 w->column_number_displayed = make_number (col);
19378 pint2str (decode_mode_spec_buf, field_width, col);
19379 return decode_mode_spec_buf;
19382 case 'e':
19383 #ifndef SYSTEM_MALLOC
19385 if (NILP (Vmemory_full))
19386 return "";
19387 else
19388 return "!MEM FULL! ";
19390 #else
19391 return "";
19392 #endif
19394 case 'F':
19395 /* %F displays the frame name. */
19396 if (!NILP (f->title))
19397 return (char *) SDATA (f->title);
19398 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19399 return (char *) SDATA (f->name);
19400 return "Emacs";
19402 case 'f':
19403 obj = b->filename;
19404 break;
19406 case 'i':
19408 EMACS_INT size = ZV - BEGV;
19409 pint2str (decode_mode_spec_buf, field_width, size);
19410 return decode_mode_spec_buf;
19413 case 'I':
19415 EMACS_INT size = ZV - BEGV;
19416 pint2hrstr (decode_mode_spec_buf, field_width, size);
19417 return decode_mode_spec_buf;
19420 case 'l':
19422 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
19423 int topline, nlines, height;
19424 EMACS_INT junk;
19426 /* %c and %l are ignored in `frame-title-format'. */
19427 if (mode_line_target == MODE_LINE_TITLE)
19428 return "";
19430 startpos = XMARKER (w->start)->charpos;
19431 startpos_byte = marker_byte_position (w->start);
19432 height = WINDOW_TOTAL_LINES (w);
19434 /* If we decided that this buffer isn't suitable for line numbers,
19435 don't forget that too fast. */
19436 if (EQ (w->base_line_pos, w->buffer))
19437 goto no_value;
19438 /* But do forget it, if the window shows a different buffer now. */
19439 else if (BUFFERP (w->base_line_pos))
19440 w->base_line_pos = Qnil;
19442 /* If the buffer is very big, don't waste time. */
19443 if (INTEGERP (Vline_number_display_limit)
19444 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19446 w->base_line_pos = Qnil;
19447 w->base_line_number = Qnil;
19448 goto no_value;
19451 if (INTEGERP (w->base_line_number)
19452 && INTEGERP (w->base_line_pos)
19453 && XFASTINT (w->base_line_pos) <= startpos)
19455 line = XFASTINT (w->base_line_number);
19456 linepos = XFASTINT (w->base_line_pos);
19457 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19459 else
19461 line = 1;
19462 linepos = BUF_BEGV (b);
19463 linepos_byte = BUF_BEGV_BYTE (b);
19466 /* Count lines from base line to window start position. */
19467 nlines = display_count_lines (linepos, linepos_byte,
19468 startpos_byte,
19469 startpos, &junk);
19471 topline = nlines + line;
19473 /* Determine a new base line, if the old one is too close
19474 or too far away, or if we did not have one.
19475 "Too close" means it's plausible a scroll-down would
19476 go back past it. */
19477 if (startpos == BUF_BEGV (b))
19479 w->base_line_number = make_number (topline);
19480 w->base_line_pos = make_number (BUF_BEGV (b));
19482 else if (nlines < height + 25 || nlines > height * 3 + 50
19483 || linepos == BUF_BEGV (b))
19485 EMACS_INT limit = BUF_BEGV (b);
19486 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
19487 EMACS_INT position;
19488 int distance = (height * 2 + 30) * line_number_display_limit_width;
19490 if (startpos - distance > limit)
19492 limit = startpos - distance;
19493 limit_byte = CHAR_TO_BYTE (limit);
19496 nlines = display_count_lines (startpos, startpos_byte,
19497 limit_byte,
19498 - (height * 2 + 30),
19499 &position);
19500 /* If we couldn't find the lines we wanted within
19501 line_number_display_limit_width chars per line,
19502 give up on line numbers for this window. */
19503 if (position == limit_byte && limit == startpos - distance)
19505 w->base_line_pos = w->buffer;
19506 w->base_line_number = Qnil;
19507 goto no_value;
19510 w->base_line_number = make_number (topline - nlines);
19511 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19514 /* Now count lines from the start pos to point. */
19515 nlines = display_count_lines (startpos, startpos_byte,
19516 PT_BYTE, PT, &junk);
19518 /* Record that we did display the line number. */
19519 line_number_displayed = 1;
19521 /* Make the string to show. */
19522 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19523 return decode_mode_spec_buf;
19524 no_value:
19526 char* p = decode_mode_spec_buf;
19527 int pad = field_width - 2;
19528 while (pad-- > 0)
19529 *p++ = ' ';
19530 *p++ = '?';
19531 *p++ = '?';
19532 *p = '\0';
19533 return decode_mode_spec_buf;
19536 break;
19538 case 'm':
19539 obj = b->mode_name;
19540 break;
19542 case 'n':
19543 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19544 return " Narrow";
19545 break;
19547 case 'p':
19549 EMACS_INT pos = marker_position (w->start);
19550 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19552 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19554 if (pos <= BUF_BEGV (b))
19555 return "All";
19556 else
19557 return "Bottom";
19559 else if (pos <= BUF_BEGV (b))
19560 return "Top";
19561 else
19563 if (total > 1000000)
19564 /* Do it differently for a large value, to avoid overflow. */
19565 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19566 else
19567 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19568 /* We can't normally display a 3-digit number,
19569 so get us a 2-digit number that is close. */
19570 if (total == 100)
19571 total = 99;
19572 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19573 return decode_mode_spec_buf;
19577 /* Display percentage of size above the bottom of the screen. */
19578 case 'P':
19580 EMACS_INT toppos = marker_position (w->start);
19581 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19582 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
19584 if (botpos >= BUF_ZV (b))
19586 if (toppos <= BUF_BEGV (b))
19587 return "All";
19588 else
19589 return "Bottom";
19591 else
19593 if (total > 1000000)
19594 /* Do it differently for a large value, to avoid overflow. */
19595 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19596 else
19597 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19598 /* We can't normally display a 3-digit number,
19599 so get us a 2-digit number that is close. */
19600 if (total == 100)
19601 total = 99;
19602 if (toppos <= BUF_BEGV (b))
19603 sprintf (decode_mode_spec_buf, "Top%2ld%%", (long)total);
19604 else
19605 sprintf (decode_mode_spec_buf, "%2ld%%", (long)total);
19606 return decode_mode_spec_buf;
19610 case 's':
19611 /* status of process */
19612 obj = Fget_buffer_process (Fcurrent_buffer ());
19613 if (NILP (obj))
19614 return "no process";
19615 #ifndef MSDOS
19616 obj = Fsymbol_name (Fprocess_status (obj));
19617 #endif
19618 break;
19620 case '@':
19622 int count = inhibit_garbage_collection ();
19623 Lisp_Object val = call1 (intern ("file-remote-p"),
19624 current_buffer->directory);
19625 unbind_to (count, Qnil);
19627 if (NILP (val))
19628 return "-";
19629 else
19630 return "@";
19633 case 't': /* indicate TEXT or BINARY */
19634 #ifdef MODE_LINE_BINARY_TEXT
19635 return MODE_LINE_BINARY_TEXT (b);
19636 #else
19637 return "T";
19638 #endif
19640 case 'z':
19641 /* coding-system (not including end-of-line format) */
19642 case 'Z':
19643 /* coding-system (including end-of-line type) */
19645 int eol_flag = (c == 'Z');
19646 char *p = decode_mode_spec_buf;
19648 if (! FRAME_WINDOW_P (f))
19650 /* No need to mention EOL here--the terminal never needs
19651 to do EOL conversion. */
19652 p = decode_mode_spec_coding (CODING_ID_NAME
19653 (FRAME_KEYBOARD_CODING (f)->id),
19654 p, 0);
19655 p = decode_mode_spec_coding (CODING_ID_NAME
19656 (FRAME_TERMINAL_CODING (f)->id),
19657 p, 0);
19659 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19660 p, eol_flag);
19662 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19663 #ifdef subprocesses
19664 obj = Fget_buffer_process (Fcurrent_buffer ());
19665 if (PROCESSP (obj))
19667 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19668 p, eol_flag);
19669 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19670 p, eol_flag);
19672 #endif /* subprocesses */
19673 #endif /* 0 */
19674 *p = 0;
19675 return decode_mode_spec_buf;
19679 if (STRINGP (obj))
19681 *string = obj;
19682 return (char *) SDATA (obj);
19684 else
19685 return "";
19689 /* Count up to COUNT lines starting from START / START_BYTE.
19690 But don't go beyond LIMIT_BYTE.
19691 Return the number of lines thus found (always nonnegative).
19693 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19695 static int
19696 display_count_lines (EMACS_INT start, EMACS_INT start_byte,
19697 EMACS_INT limit_byte, int count,
19698 EMACS_INT *byte_pos_ptr)
19700 register unsigned char *cursor;
19701 unsigned char *base;
19703 register int ceiling;
19704 register unsigned char *ceiling_addr;
19705 int orig_count = count;
19707 /* If we are not in selective display mode,
19708 check only for newlines. */
19709 int selective_display = (!NILP (current_buffer->selective_display)
19710 && !INTEGERP (current_buffer->selective_display));
19712 if (count > 0)
19714 while (start_byte < limit_byte)
19716 ceiling = BUFFER_CEILING_OF (start_byte);
19717 ceiling = min (limit_byte - 1, ceiling);
19718 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19719 base = (cursor = BYTE_POS_ADDR (start_byte));
19720 while (1)
19722 if (selective_display)
19723 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19725 else
19726 while (*cursor != '\n' && ++cursor != ceiling_addr)
19729 if (cursor != ceiling_addr)
19731 if (--count == 0)
19733 start_byte += cursor - base + 1;
19734 *byte_pos_ptr = start_byte;
19735 return orig_count;
19737 else
19738 if (++cursor == ceiling_addr)
19739 break;
19741 else
19742 break;
19744 start_byte += cursor - base;
19747 else
19749 while (start_byte > limit_byte)
19751 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19752 ceiling = max (limit_byte, ceiling);
19753 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19754 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19755 while (1)
19757 if (selective_display)
19758 while (--cursor != ceiling_addr
19759 && *cursor != '\n' && *cursor != 015)
19761 else
19762 while (--cursor != ceiling_addr && *cursor != '\n')
19765 if (cursor != ceiling_addr)
19767 if (++count == 0)
19769 start_byte += cursor - base + 1;
19770 *byte_pos_ptr = start_byte;
19771 /* When scanning backwards, we should
19772 not count the newline posterior to which we stop. */
19773 return - orig_count - 1;
19776 else
19777 break;
19779 /* Here we add 1 to compensate for the last decrement
19780 of CURSOR, which took it past the valid range. */
19781 start_byte += cursor - base + 1;
19785 *byte_pos_ptr = limit_byte;
19787 if (count < 0)
19788 return - orig_count + count;
19789 return orig_count - count;
19795 /***********************************************************************
19796 Displaying strings
19797 ***********************************************************************/
19799 /* Display a NUL-terminated string, starting with index START.
19801 If STRING is non-null, display that C string. Otherwise, the Lisp
19802 string LISP_STRING is displayed. There's a case that STRING is
19803 non-null and LISP_STRING is not nil. It means STRING is a string
19804 data of LISP_STRING. In that case, we display LISP_STRING while
19805 ignoring its text properties.
19807 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19808 FACE_STRING. Display STRING or LISP_STRING with the face at
19809 FACE_STRING_POS in FACE_STRING:
19811 Display the string in the environment given by IT, but use the
19812 standard display table, temporarily.
19814 FIELD_WIDTH is the minimum number of output glyphs to produce.
19815 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19816 with spaces. If STRING has more characters, more than FIELD_WIDTH
19817 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19819 PRECISION is the maximum number of characters to output from
19820 STRING. PRECISION < 0 means don't truncate the string.
19822 This is roughly equivalent to printf format specifiers:
19824 FIELD_WIDTH PRECISION PRINTF
19825 ----------------------------------------
19826 -1 -1 %s
19827 -1 10 %.10s
19828 10 -1 %10s
19829 20 10 %20.10s
19831 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19832 display them, and < 0 means obey the current buffer's value of
19833 enable_multibyte_characters.
19835 Value is the number of columns displayed. */
19837 static int
19838 display_string (const unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19839 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19840 int field_width, int precision, int max_x, int multibyte)
19842 int hpos_at_start = it->hpos;
19843 int saved_face_id = it->face_id;
19844 struct glyph_row *row = it->glyph_row;
19846 /* Initialize the iterator IT for iteration over STRING beginning
19847 with index START. */
19848 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19849 precision, field_width, multibyte);
19850 if (string && STRINGP (lisp_string))
19851 /* LISP_STRING is the one returned by decode_mode_spec. We should
19852 ignore its text properties. */
19853 it->stop_charpos = -1;
19855 /* If displaying STRING, set up the face of the iterator
19856 from LISP_STRING, if that's given. */
19857 if (STRINGP (face_string))
19859 EMACS_INT endptr;
19860 struct face *face;
19862 it->face_id
19863 = face_at_string_position (it->w, face_string, face_string_pos,
19864 0, it->region_beg_charpos,
19865 it->region_end_charpos,
19866 &endptr, it->base_face_id, 0);
19867 face = FACE_FROM_ID (it->f, it->face_id);
19868 it->face_box_p = face->box != FACE_NO_BOX;
19871 /* Set max_x to the maximum allowed X position. Don't let it go
19872 beyond the right edge of the window. */
19873 if (max_x <= 0)
19874 max_x = it->last_visible_x;
19875 else
19876 max_x = min (max_x, it->last_visible_x);
19878 /* Skip over display elements that are not visible. because IT->w is
19879 hscrolled. */
19880 if (it->current_x < it->first_visible_x)
19881 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19882 MOVE_TO_POS | MOVE_TO_X);
19884 row->ascent = it->max_ascent;
19885 row->height = it->max_ascent + it->max_descent;
19886 row->phys_ascent = it->max_phys_ascent;
19887 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19888 row->extra_line_spacing = it->max_extra_line_spacing;
19890 /* This condition is for the case that we are called with current_x
19891 past last_visible_x. */
19892 while (it->current_x < max_x)
19894 int x_before, x, n_glyphs_before, i, nglyphs;
19896 /* Get the next display element. */
19897 if (!get_next_display_element (it))
19898 break;
19900 /* Produce glyphs. */
19901 x_before = it->current_x;
19902 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19903 PRODUCE_GLYPHS (it);
19905 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19906 i = 0;
19907 x = x_before;
19908 while (i < nglyphs)
19910 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19912 if (it->line_wrap != TRUNCATE
19913 && x + glyph->pixel_width > max_x)
19915 /* End of continued line or max_x reached. */
19916 if (CHAR_GLYPH_PADDING_P (*glyph))
19918 /* A wide character is unbreakable. */
19919 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19920 it->current_x = x_before;
19922 else
19924 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19925 it->current_x = x;
19927 break;
19929 else if (x + glyph->pixel_width >= it->first_visible_x)
19931 /* Glyph is at least partially visible. */
19932 ++it->hpos;
19933 if (x < it->first_visible_x)
19934 it->glyph_row->x = x - it->first_visible_x;
19936 else
19938 /* Glyph is off the left margin of the display area.
19939 Should not happen. */
19940 abort ();
19943 row->ascent = max (row->ascent, it->max_ascent);
19944 row->height = max (row->height, it->max_ascent + it->max_descent);
19945 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19946 row->phys_height = max (row->phys_height,
19947 it->max_phys_ascent + it->max_phys_descent);
19948 row->extra_line_spacing = max (row->extra_line_spacing,
19949 it->max_extra_line_spacing);
19950 x += glyph->pixel_width;
19951 ++i;
19954 /* Stop if max_x reached. */
19955 if (i < nglyphs)
19956 break;
19958 /* Stop at line ends. */
19959 if (ITERATOR_AT_END_OF_LINE_P (it))
19961 it->continuation_lines_width = 0;
19962 break;
19965 set_iterator_to_next (it, 1);
19967 /* Stop if truncating at the right edge. */
19968 if (it->line_wrap == TRUNCATE
19969 && it->current_x >= it->last_visible_x)
19971 /* Add truncation mark, but don't do it if the line is
19972 truncated at a padding space. */
19973 if (IT_CHARPOS (*it) < it->string_nchars)
19975 if (!FRAME_WINDOW_P (it->f))
19977 int i, n;
19979 if (it->current_x > it->last_visible_x)
19981 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19982 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19983 break;
19984 for (n = row->used[TEXT_AREA]; i < n; ++i)
19986 row->used[TEXT_AREA] = i;
19987 produce_special_glyphs (it, IT_TRUNCATION);
19990 produce_special_glyphs (it, IT_TRUNCATION);
19992 it->glyph_row->truncated_on_right_p = 1;
19994 break;
19998 /* Maybe insert a truncation at the left. */
19999 if (it->first_visible_x
20000 && IT_CHARPOS (*it) > 0)
20002 if (!FRAME_WINDOW_P (it->f))
20003 insert_left_trunc_glyphs (it);
20004 it->glyph_row->truncated_on_left_p = 1;
20007 it->face_id = saved_face_id;
20009 /* Value is number of columns displayed. */
20010 return it->hpos - hpos_at_start;
20015 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20016 appears as an element of LIST or as the car of an element of LIST.
20017 If PROPVAL is a list, compare each element against LIST in that
20018 way, and return 1/2 if any element of PROPVAL is found in LIST.
20019 Otherwise return 0. This function cannot quit.
20020 The return value is 2 if the text is invisible but with an ellipsis
20021 and 1 if it's invisible and without an ellipsis. */
20024 invisible_p (register Lisp_Object propval, Lisp_Object list)
20026 register Lisp_Object tail, proptail;
20028 for (tail = list; CONSP (tail); tail = XCDR (tail))
20030 register Lisp_Object tem;
20031 tem = XCAR (tail);
20032 if (EQ (propval, tem))
20033 return 1;
20034 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20035 return NILP (XCDR (tem)) ? 1 : 2;
20038 if (CONSP (propval))
20040 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20042 Lisp_Object propelt;
20043 propelt = XCAR (proptail);
20044 for (tail = list; CONSP (tail); tail = XCDR (tail))
20046 register Lisp_Object tem;
20047 tem = XCAR (tail);
20048 if (EQ (propelt, tem))
20049 return 1;
20050 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20051 return NILP (XCDR (tem)) ? 1 : 2;
20056 return 0;
20059 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20060 doc: /* Non-nil if the property makes the text invisible.
20061 POS-OR-PROP can be a marker or number, in which case it is taken to be
20062 a position in the current buffer and the value of the `invisible' property
20063 is checked; or it can be some other value, which is then presumed to be the
20064 value of the `invisible' property of the text of interest.
20065 The non-nil value returned can be t for truly invisible text or something
20066 else if the text is replaced by an ellipsis. */)
20067 (Lisp_Object pos_or_prop)
20069 Lisp_Object prop
20070 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20071 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20072 : pos_or_prop);
20073 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20074 return (invis == 0 ? Qnil
20075 : invis == 1 ? Qt
20076 : make_number (invis));
20079 /* Calculate a width or height in pixels from a specification using
20080 the following elements:
20082 SPEC ::=
20083 NUM - a (fractional) multiple of the default font width/height
20084 (NUM) - specifies exactly NUM pixels
20085 UNIT - a fixed number of pixels, see below.
20086 ELEMENT - size of a display element in pixels, see below.
20087 (NUM . SPEC) - equals NUM * SPEC
20088 (+ SPEC SPEC ...) - add pixel values
20089 (- SPEC SPEC ...) - subtract pixel values
20090 (- SPEC) - negate pixel value
20092 NUM ::=
20093 INT or FLOAT - a number constant
20094 SYMBOL - use symbol's (buffer local) variable binding.
20096 UNIT ::=
20097 in - pixels per inch *)
20098 mm - pixels per 1/1000 meter *)
20099 cm - pixels per 1/100 meter *)
20100 width - width of current font in pixels.
20101 height - height of current font in pixels.
20103 *) using the ratio(s) defined in display-pixels-per-inch.
20105 ELEMENT ::=
20107 left-fringe - left fringe width in pixels
20108 right-fringe - right fringe width in pixels
20110 left-margin - left margin width in pixels
20111 right-margin - right margin width in pixels
20113 scroll-bar - scroll-bar area width in pixels
20115 Examples:
20117 Pixels corresponding to 5 inches:
20118 (5 . in)
20120 Total width of non-text areas on left side of window (if scroll-bar is on left):
20121 '(space :width (+ left-fringe left-margin scroll-bar))
20123 Align to first text column (in header line):
20124 '(space :align-to 0)
20126 Align to middle of text area minus half the width of variable `my-image'
20127 containing a loaded image:
20128 '(space :align-to (0.5 . (- text my-image)))
20130 Width of left margin minus width of 1 character in the default font:
20131 '(space :width (- left-margin 1))
20133 Width of left margin minus width of 2 characters in the current font:
20134 '(space :width (- left-margin (2 . width)))
20136 Center 1 character over left-margin (in header line):
20137 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20139 Different ways to express width of left fringe plus left margin minus one pixel:
20140 '(space :width (- (+ left-fringe left-margin) (1)))
20141 '(space :width (+ left-fringe left-margin (- (1))))
20142 '(space :width (+ left-fringe left-margin (-1)))
20146 #define NUMVAL(X) \
20147 ((INTEGERP (X) || FLOATP (X)) \
20148 ? XFLOATINT (X) \
20149 : - 1)
20152 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20153 struct font *font, int width_p, int *align_to)
20155 double pixels;
20157 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20158 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20160 if (NILP (prop))
20161 return OK_PIXELS (0);
20163 xassert (FRAME_LIVE_P (it->f));
20165 if (SYMBOLP (prop))
20167 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20169 char *unit = SDATA (SYMBOL_NAME (prop));
20171 if (unit[0] == 'i' && unit[1] == 'n')
20172 pixels = 1.0;
20173 else if (unit[0] == 'm' && unit[1] == 'm')
20174 pixels = 25.4;
20175 else if (unit[0] == 'c' && unit[1] == 'm')
20176 pixels = 2.54;
20177 else
20178 pixels = 0;
20179 if (pixels > 0)
20181 double ppi;
20182 #ifdef HAVE_WINDOW_SYSTEM
20183 if (FRAME_WINDOW_P (it->f)
20184 && (ppi = (width_p
20185 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20186 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20187 ppi > 0))
20188 return OK_PIXELS (ppi / pixels);
20189 #endif
20191 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20192 || (CONSP (Vdisplay_pixels_per_inch)
20193 && (ppi = (width_p
20194 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20195 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20196 ppi > 0)))
20197 return OK_PIXELS (ppi / pixels);
20199 return 0;
20203 #ifdef HAVE_WINDOW_SYSTEM
20204 if (EQ (prop, Qheight))
20205 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20206 if (EQ (prop, Qwidth))
20207 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20208 #else
20209 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20210 return OK_PIXELS (1);
20211 #endif
20213 if (EQ (prop, Qtext))
20214 return OK_PIXELS (width_p
20215 ? window_box_width (it->w, TEXT_AREA)
20216 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20218 if (align_to && *align_to < 0)
20220 *res = 0;
20221 if (EQ (prop, Qleft))
20222 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20223 if (EQ (prop, Qright))
20224 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20225 if (EQ (prop, Qcenter))
20226 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20227 + window_box_width (it->w, TEXT_AREA) / 2);
20228 if (EQ (prop, Qleft_fringe))
20229 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20230 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20231 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20232 if (EQ (prop, Qright_fringe))
20233 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20234 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20235 : window_box_right_offset (it->w, TEXT_AREA));
20236 if (EQ (prop, Qleft_margin))
20237 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20238 if (EQ (prop, Qright_margin))
20239 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20240 if (EQ (prop, Qscroll_bar))
20241 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20243 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20244 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20245 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20246 : 0)));
20248 else
20250 if (EQ (prop, Qleft_fringe))
20251 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20252 if (EQ (prop, Qright_fringe))
20253 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20254 if (EQ (prop, Qleft_margin))
20255 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20256 if (EQ (prop, Qright_margin))
20257 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20258 if (EQ (prop, Qscroll_bar))
20259 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20262 prop = Fbuffer_local_value (prop, it->w->buffer);
20265 if (INTEGERP (prop) || FLOATP (prop))
20267 int base_unit = (width_p
20268 ? FRAME_COLUMN_WIDTH (it->f)
20269 : FRAME_LINE_HEIGHT (it->f));
20270 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20273 if (CONSP (prop))
20275 Lisp_Object car = XCAR (prop);
20276 Lisp_Object cdr = XCDR (prop);
20278 if (SYMBOLP (car))
20280 #ifdef HAVE_WINDOW_SYSTEM
20281 if (FRAME_WINDOW_P (it->f)
20282 && valid_image_p (prop))
20284 int id = lookup_image (it->f, prop);
20285 struct image *img = IMAGE_FROM_ID (it->f, id);
20287 return OK_PIXELS (width_p ? img->width : img->height);
20289 #endif
20290 if (EQ (car, Qplus) || EQ (car, Qminus))
20292 int first = 1;
20293 double px;
20295 pixels = 0;
20296 while (CONSP (cdr))
20298 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20299 font, width_p, align_to))
20300 return 0;
20301 if (first)
20302 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20303 else
20304 pixels += px;
20305 cdr = XCDR (cdr);
20307 if (EQ (car, Qminus))
20308 pixels = -pixels;
20309 return OK_PIXELS (pixels);
20312 car = Fbuffer_local_value (car, it->w->buffer);
20315 if (INTEGERP (car) || FLOATP (car))
20317 double fact;
20318 pixels = XFLOATINT (car);
20319 if (NILP (cdr))
20320 return OK_PIXELS (pixels);
20321 if (calc_pixel_width_or_height (&fact, it, cdr,
20322 font, width_p, align_to))
20323 return OK_PIXELS (pixels * fact);
20324 return 0;
20327 return 0;
20330 return 0;
20334 /***********************************************************************
20335 Glyph Display
20336 ***********************************************************************/
20338 #ifdef HAVE_WINDOW_SYSTEM
20340 #if GLYPH_DEBUG
20342 void
20343 dump_glyph_string (s)
20344 struct glyph_string *s;
20346 fprintf (stderr, "glyph string\n");
20347 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20348 s->x, s->y, s->width, s->height);
20349 fprintf (stderr, " ybase = %d\n", s->ybase);
20350 fprintf (stderr, " hl = %d\n", s->hl);
20351 fprintf (stderr, " left overhang = %d, right = %d\n",
20352 s->left_overhang, s->right_overhang);
20353 fprintf (stderr, " nchars = %d\n", s->nchars);
20354 fprintf (stderr, " extends to end of line = %d\n",
20355 s->extends_to_end_of_line_p);
20356 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20357 fprintf (stderr, " bg width = %d\n", s->background_width);
20360 #endif /* GLYPH_DEBUG */
20362 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20363 of XChar2b structures for S; it can't be allocated in
20364 init_glyph_string because it must be allocated via `alloca'. W
20365 is the window on which S is drawn. ROW and AREA are the glyph row
20366 and area within the row from which S is constructed. START is the
20367 index of the first glyph structure covered by S. HL is a
20368 face-override for drawing S. */
20370 #ifdef HAVE_NTGUI
20371 #define OPTIONAL_HDC(hdc) HDC hdc,
20372 #define DECLARE_HDC(hdc) HDC hdc;
20373 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20374 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20375 #endif
20377 #ifndef OPTIONAL_HDC
20378 #define OPTIONAL_HDC(hdc)
20379 #define DECLARE_HDC(hdc)
20380 #define ALLOCATE_HDC(hdc, f)
20381 #define RELEASE_HDC(hdc, f)
20382 #endif
20384 static void
20385 init_glyph_string (struct glyph_string *s,
20386 OPTIONAL_HDC (hdc)
20387 XChar2b *char2b, struct window *w, struct glyph_row *row,
20388 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20390 memset (s, 0, sizeof *s);
20391 s->w = w;
20392 s->f = XFRAME (w->frame);
20393 #ifdef HAVE_NTGUI
20394 s->hdc = hdc;
20395 #endif
20396 s->display = FRAME_X_DISPLAY (s->f);
20397 s->window = FRAME_X_WINDOW (s->f);
20398 s->char2b = char2b;
20399 s->hl = hl;
20400 s->row = row;
20401 s->area = area;
20402 s->first_glyph = row->glyphs[area] + start;
20403 s->height = row->height;
20404 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20405 s->ybase = s->y + row->ascent;
20409 /* Append the list of glyph strings with head H and tail T to the list
20410 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20412 static INLINE void
20413 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20414 struct glyph_string *h, struct glyph_string *t)
20416 if (h)
20418 if (*head)
20419 (*tail)->next = h;
20420 else
20421 *head = h;
20422 h->prev = *tail;
20423 *tail = t;
20428 /* Prepend the list of glyph strings with head H and tail T to the
20429 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20430 result. */
20432 static INLINE void
20433 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20434 struct glyph_string *h, struct glyph_string *t)
20436 if (h)
20438 if (*head)
20439 (*head)->prev = t;
20440 else
20441 *tail = t;
20442 t->next = *head;
20443 *head = h;
20448 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20449 Set *HEAD and *TAIL to the resulting list. */
20451 static INLINE void
20452 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20453 struct glyph_string *s)
20455 s->next = s->prev = NULL;
20456 append_glyph_string_lists (head, tail, s, s);
20460 /* Get face and two-byte form of character C in face FACE_ID on frame
20461 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20462 means we want to display multibyte text. DISPLAY_P non-zero means
20463 make sure that X resources for the face returned are allocated.
20464 Value is a pointer to a realized face that is ready for display if
20465 DISPLAY_P is non-zero. */
20467 static INLINE struct face *
20468 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20469 XChar2b *char2b, int multibyte_p, int display_p)
20471 struct face *face = FACE_FROM_ID (f, face_id);
20473 if (face->font)
20475 unsigned code = face->font->driver->encode_char (face->font, c);
20477 if (code != FONT_INVALID_CODE)
20478 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20479 else
20480 STORE_XCHAR2B (char2b, 0, 0);
20483 /* Make sure X resources of the face are allocated. */
20484 #ifdef HAVE_X_WINDOWS
20485 if (display_p)
20486 #endif
20488 xassert (face != NULL);
20489 PREPARE_FACE_FOR_DISPLAY (f, face);
20492 return face;
20496 /* Get face and two-byte form of character glyph GLYPH on frame F.
20497 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20498 a pointer to a realized face that is ready for display. */
20500 static INLINE struct face *
20501 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20502 XChar2b *char2b, int *two_byte_p)
20504 struct face *face;
20506 xassert (glyph->type == CHAR_GLYPH);
20507 face = FACE_FROM_ID (f, glyph->face_id);
20509 if (two_byte_p)
20510 *two_byte_p = 0;
20512 if (face->font)
20514 unsigned code;
20516 if (CHAR_BYTE8_P (glyph->u.ch))
20517 code = CHAR_TO_BYTE8 (glyph->u.ch);
20518 else
20519 code = face->font->driver->encode_char (face->font, glyph->u.ch);
20521 if (code != FONT_INVALID_CODE)
20522 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20523 else
20524 STORE_XCHAR2B (char2b, 0, 0);
20527 /* Make sure X resources of the face are allocated. */
20528 xassert (face != NULL);
20529 PREPARE_FACE_FOR_DISPLAY (f, face);
20530 return face;
20534 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
20535 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
20537 static INLINE int
20538 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
20540 unsigned code;
20542 if (CHAR_BYTE8_P (c))
20543 code = CHAR_TO_BYTE8 (c);
20544 else
20545 code = font->driver->encode_char (font, c);
20547 if (code == FONT_INVALID_CODE)
20548 return 0;
20549 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20550 return 1;
20554 /* Fill glyph string S with composition components specified by S->cmp.
20556 BASE_FACE is the base face of the composition.
20557 S->cmp_from is the index of the first component for S.
20559 OVERLAPS non-zero means S should draw the foreground only, and use
20560 its physical height for clipping. See also draw_glyphs.
20562 Value is the index of a component not in S. */
20564 static int
20565 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20566 int overlaps)
20568 int i;
20569 /* For all glyphs of this composition, starting at the offset
20570 S->cmp_from, until we reach the end of the definition or encounter a
20571 glyph that requires the different face, add it to S. */
20572 struct face *face;
20574 xassert (s);
20576 s->for_overlaps = overlaps;
20577 s->face = NULL;
20578 s->font = NULL;
20579 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20581 int c = COMPOSITION_GLYPH (s->cmp, i);
20583 if (c != '\t')
20585 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20586 -1, Qnil);
20588 face = get_char_face_and_encoding (s->f, c, face_id,
20589 s->char2b + i, 1, 1);
20590 if (face)
20592 if (! s->face)
20594 s->face = face;
20595 s->font = s->face->font;
20597 else if (s->face != face)
20598 break;
20601 ++s->nchars;
20603 s->cmp_to = i;
20605 /* All glyph strings for the same composition has the same width,
20606 i.e. the width set for the first component of the composition. */
20607 s->width = s->first_glyph->pixel_width;
20609 /* If the specified font could not be loaded, use the frame's
20610 default font, but record the fact that we couldn't load it in
20611 the glyph string so that we can draw rectangles for the
20612 characters of the glyph string. */
20613 if (s->font == NULL)
20615 s->font_not_found_p = 1;
20616 s->font = FRAME_FONT (s->f);
20619 /* Adjust base line for subscript/superscript text. */
20620 s->ybase += s->first_glyph->voffset;
20622 /* This glyph string must always be drawn with 16-bit functions. */
20623 s->two_byte_p = 1;
20625 return s->cmp_to;
20628 static int
20629 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20630 int start, int end, int overlaps)
20632 struct glyph *glyph, *last;
20633 Lisp_Object lgstring;
20634 int i;
20636 s->for_overlaps = overlaps;
20637 glyph = s->row->glyphs[s->area] + start;
20638 last = s->row->glyphs[s->area] + end;
20639 s->cmp_id = glyph->u.cmp.id;
20640 s->cmp_from = glyph->slice.cmp.from;
20641 s->cmp_to = glyph->slice.cmp.to + 1;
20642 s->face = FACE_FROM_ID (s->f, face_id);
20643 lgstring = composition_gstring_from_id (s->cmp_id);
20644 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20645 glyph++;
20646 while (glyph < last
20647 && glyph->u.cmp.automatic
20648 && glyph->u.cmp.id == s->cmp_id
20649 && s->cmp_to == glyph->slice.cmp.from)
20650 s->cmp_to = (glyph++)->slice.cmp.to + 1;
20652 for (i = s->cmp_from; i < s->cmp_to; i++)
20654 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20655 unsigned code = LGLYPH_CODE (lglyph);
20657 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20659 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20660 return glyph - s->row->glyphs[s->area];
20664 /* Fill glyph string S from a sequence of character glyphs.
20666 FACE_ID is the face id of the string. START is the index of the
20667 first glyph to consider, END is the index of the last + 1.
20668 OVERLAPS non-zero means S should draw the foreground only, and use
20669 its physical height for clipping. See also draw_glyphs.
20671 Value is the index of the first glyph not in S. */
20673 static int
20674 fill_glyph_string (struct glyph_string *s, int face_id,
20675 int start, int end, int overlaps)
20677 struct glyph *glyph, *last;
20678 int voffset;
20679 int glyph_not_available_p;
20681 xassert (s->f == XFRAME (s->w->frame));
20682 xassert (s->nchars == 0);
20683 xassert (start >= 0 && end > start);
20685 s->for_overlaps = overlaps;
20686 glyph = s->row->glyphs[s->area] + start;
20687 last = s->row->glyphs[s->area] + end;
20688 voffset = glyph->voffset;
20689 s->padding_p = glyph->padding_p;
20690 glyph_not_available_p = glyph->glyph_not_available_p;
20692 while (glyph < last
20693 && glyph->type == CHAR_GLYPH
20694 && glyph->voffset == voffset
20695 /* Same face id implies same font, nowadays. */
20696 && glyph->face_id == face_id
20697 && glyph->glyph_not_available_p == glyph_not_available_p)
20699 int two_byte_p;
20701 s->face = get_glyph_face_and_encoding (s->f, glyph,
20702 s->char2b + s->nchars,
20703 &two_byte_p);
20704 s->two_byte_p = two_byte_p;
20705 ++s->nchars;
20706 xassert (s->nchars <= end - start);
20707 s->width += glyph->pixel_width;
20708 if (glyph++->padding_p != s->padding_p)
20709 break;
20712 s->font = s->face->font;
20714 /* If the specified font could not be loaded, use the frame's font,
20715 but record the fact that we couldn't load it in
20716 S->font_not_found_p so that we can draw rectangles for the
20717 characters of the glyph string. */
20718 if (s->font == NULL || glyph_not_available_p)
20720 s->font_not_found_p = 1;
20721 s->font = FRAME_FONT (s->f);
20724 /* Adjust base line for subscript/superscript text. */
20725 s->ybase += voffset;
20727 xassert (s->face && s->face->gc);
20728 return glyph - s->row->glyphs[s->area];
20732 /* Fill glyph string S from image glyph S->first_glyph. */
20734 static void
20735 fill_image_glyph_string (struct glyph_string *s)
20737 xassert (s->first_glyph->type == IMAGE_GLYPH);
20738 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20739 xassert (s->img);
20740 s->slice = s->first_glyph->slice.img;
20741 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20742 s->font = s->face->font;
20743 s->width = s->first_glyph->pixel_width;
20745 /* Adjust base line for subscript/superscript text. */
20746 s->ybase += s->first_glyph->voffset;
20750 /* Fill glyph string S from a sequence of stretch glyphs.
20752 ROW is the glyph row in which the glyphs are found, AREA is the
20753 area within the row. START is the index of the first glyph to
20754 consider, END is the index of the last + 1.
20756 Value is the index of the first glyph not in S. */
20758 static int
20759 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20760 enum glyph_row_area area, int start, int end)
20762 struct glyph *glyph, *last;
20763 int voffset, face_id;
20765 xassert (s->first_glyph->type == STRETCH_GLYPH);
20767 glyph = s->row->glyphs[s->area] + start;
20768 last = s->row->glyphs[s->area] + end;
20769 face_id = glyph->face_id;
20770 s->face = FACE_FROM_ID (s->f, face_id);
20771 s->font = s->face->font;
20772 s->width = glyph->pixel_width;
20773 s->nchars = 1;
20774 voffset = glyph->voffset;
20776 for (++glyph;
20777 (glyph < last
20778 && glyph->type == STRETCH_GLYPH
20779 && glyph->voffset == voffset
20780 && glyph->face_id == face_id);
20781 ++glyph)
20782 s->width += glyph->pixel_width;
20784 /* Adjust base line for subscript/superscript text. */
20785 s->ybase += voffset;
20787 /* The case that face->gc == 0 is handled when drawing the glyph
20788 string by calling PREPARE_FACE_FOR_DISPLAY. */
20789 xassert (s->face);
20790 return glyph - s->row->glyphs[s->area];
20793 static struct font_metrics *
20794 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20796 static struct font_metrics metrics;
20797 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20799 if (! font || code == FONT_INVALID_CODE)
20800 return NULL;
20801 font->driver->text_extents (font, &code, 1, &metrics);
20802 return &metrics;
20805 /* EXPORT for RIF:
20806 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20807 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20808 assumed to be zero. */
20810 void
20811 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20813 *left = *right = 0;
20815 if (glyph->type == CHAR_GLYPH)
20817 struct face *face;
20818 XChar2b char2b;
20819 struct font_metrics *pcm;
20821 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20822 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20824 if (pcm->rbearing > pcm->width)
20825 *right = pcm->rbearing - pcm->width;
20826 if (pcm->lbearing < 0)
20827 *left = -pcm->lbearing;
20830 else if (glyph->type == COMPOSITE_GLYPH)
20832 if (! glyph->u.cmp.automatic)
20834 struct composition *cmp = composition_table[glyph->u.cmp.id];
20836 if (cmp->rbearing > cmp->pixel_width)
20837 *right = cmp->rbearing - cmp->pixel_width;
20838 if (cmp->lbearing < 0)
20839 *left = - cmp->lbearing;
20841 else
20843 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20844 struct font_metrics metrics;
20846 composition_gstring_width (gstring, glyph->slice.cmp.from,
20847 glyph->slice.cmp.to + 1, &metrics);
20848 if (metrics.rbearing > metrics.width)
20849 *right = metrics.rbearing - metrics.width;
20850 if (metrics.lbearing < 0)
20851 *left = - metrics.lbearing;
20857 /* Return the index of the first glyph preceding glyph string S that
20858 is overwritten by S because of S's left overhang. Value is -1
20859 if no glyphs are overwritten. */
20861 static int
20862 left_overwritten (struct glyph_string *s)
20864 int k;
20866 if (s->left_overhang)
20868 int x = 0, i;
20869 struct glyph *glyphs = s->row->glyphs[s->area];
20870 int first = s->first_glyph - glyphs;
20872 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20873 x -= glyphs[i].pixel_width;
20875 k = i + 1;
20877 else
20878 k = -1;
20880 return k;
20884 /* Return the index of the first glyph preceding glyph string S that
20885 is overwriting S because of its right overhang. Value is -1 if no
20886 glyph in front of S overwrites S. */
20888 static int
20889 left_overwriting (struct glyph_string *s)
20891 int i, k, x;
20892 struct glyph *glyphs = s->row->glyphs[s->area];
20893 int first = s->first_glyph - glyphs;
20895 k = -1;
20896 x = 0;
20897 for (i = first - 1; i >= 0; --i)
20899 int left, right;
20900 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20901 if (x + right > 0)
20902 k = i;
20903 x -= glyphs[i].pixel_width;
20906 return k;
20910 /* Return the index of the last glyph following glyph string S that is
20911 overwritten by S because of S's right overhang. Value is -1 if
20912 no such glyph is found. */
20914 static int
20915 right_overwritten (struct glyph_string *s)
20917 int k = -1;
20919 if (s->right_overhang)
20921 int x = 0, i;
20922 struct glyph *glyphs = s->row->glyphs[s->area];
20923 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20924 int end = s->row->used[s->area];
20926 for (i = first; i < end && s->right_overhang > x; ++i)
20927 x += glyphs[i].pixel_width;
20929 k = i;
20932 return k;
20936 /* Return the index of the last glyph following glyph string S that
20937 overwrites S because of its left overhang. Value is negative
20938 if no such glyph is found. */
20940 static int
20941 right_overwriting (struct glyph_string *s)
20943 int i, k, x;
20944 int end = s->row->used[s->area];
20945 struct glyph *glyphs = s->row->glyphs[s->area];
20946 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20948 k = -1;
20949 x = 0;
20950 for (i = first; i < end; ++i)
20952 int left, right;
20953 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20954 if (x - left < 0)
20955 k = i;
20956 x += glyphs[i].pixel_width;
20959 return k;
20963 /* Set background width of glyph string S. START is the index of the
20964 first glyph following S. LAST_X is the right-most x-position + 1
20965 in the drawing area. */
20967 static INLINE void
20968 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
20970 /* If the face of this glyph string has to be drawn to the end of
20971 the drawing area, set S->extends_to_end_of_line_p. */
20973 if (start == s->row->used[s->area]
20974 && s->area == TEXT_AREA
20975 && ((s->row->fill_line_p
20976 && (s->hl == DRAW_NORMAL_TEXT
20977 || s->hl == DRAW_IMAGE_RAISED
20978 || s->hl == DRAW_IMAGE_SUNKEN))
20979 || s->hl == DRAW_MOUSE_FACE))
20980 s->extends_to_end_of_line_p = 1;
20982 /* If S extends its face to the end of the line, set its
20983 background_width to the distance to the right edge of the drawing
20984 area. */
20985 if (s->extends_to_end_of_line_p)
20986 s->background_width = last_x - s->x + 1;
20987 else
20988 s->background_width = s->width;
20992 /* Compute overhangs and x-positions for glyph string S and its
20993 predecessors, or successors. X is the starting x-position for S.
20994 BACKWARD_P non-zero means process predecessors. */
20996 static void
20997 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
20999 if (backward_p)
21001 while (s)
21003 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21004 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21005 x -= s->width;
21006 s->x = x;
21007 s = s->prev;
21010 else
21012 while (s)
21014 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21015 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21016 s->x = x;
21017 x += s->width;
21018 s = s->next;
21025 /* The following macros are only called from draw_glyphs below.
21026 They reference the following parameters of that function directly:
21027 `w', `row', `area', and `overlap_p'
21028 as well as the following local variables:
21029 `s', `f', and `hdc' (in W32) */
21031 #ifdef HAVE_NTGUI
21032 /* On W32, silently add local `hdc' variable to argument list of
21033 init_glyph_string. */
21034 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21035 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21036 #else
21037 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21038 init_glyph_string (s, char2b, w, row, area, start, hl)
21039 #endif
21041 /* Add a glyph string for a stretch glyph to the list of strings
21042 between HEAD and TAIL. START is the index of the stretch glyph in
21043 row area AREA of glyph row ROW. END is the index of the last glyph
21044 in that glyph row area. X is the current output position assigned
21045 to the new glyph string constructed. HL overrides that face of the
21046 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21047 is the right-most x-position of the drawing area. */
21049 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21050 and below -- keep them on one line. */
21051 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21052 do \
21054 s = (struct glyph_string *) alloca (sizeof *s); \
21055 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21056 START = fill_stretch_glyph_string (s, row, area, START, END); \
21057 append_glyph_string (&HEAD, &TAIL, s); \
21058 s->x = (X); \
21060 while (0)
21063 /* Add a glyph string for an image glyph to the list of strings
21064 between HEAD and TAIL. START is the index of the image glyph in
21065 row area AREA of glyph row ROW. END is the index of the last glyph
21066 in that glyph row area. X is the current output position assigned
21067 to the new glyph string constructed. HL overrides that face of the
21068 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21069 is the right-most x-position of the drawing area. */
21071 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21072 do \
21074 s = (struct glyph_string *) alloca (sizeof *s); \
21075 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21076 fill_image_glyph_string (s); \
21077 append_glyph_string (&HEAD, &TAIL, s); \
21078 ++START; \
21079 s->x = (X); \
21081 while (0)
21084 /* Add a glyph string for a sequence of character glyphs to the list
21085 of strings between HEAD and TAIL. START is the index of the first
21086 glyph in row area AREA of glyph row ROW that is part of the new
21087 glyph string. END is the index of the last glyph in that glyph row
21088 area. X is the current output position assigned to the new glyph
21089 string constructed. HL overrides that face of the glyph; e.g. it
21090 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21091 right-most x-position of the drawing area. */
21093 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21094 do \
21096 int face_id; \
21097 XChar2b *char2b; \
21099 face_id = (row)->glyphs[area][START].face_id; \
21101 s = (struct glyph_string *) alloca (sizeof *s); \
21102 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21103 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21104 append_glyph_string (&HEAD, &TAIL, s); \
21105 s->x = (X); \
21106 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21108 while (0)
21111 /* Add a glyph string for a composite sequence to the list of strings
21112 between HEAD and TAIL. START is the index of the first glyph in
21113 row area AREA of glyph row ROW that is part of the new glyph
21114 string. END is the index of the last glyph in that glyph row area.
21115 X is the current output position assigned to the new glyph string
21116 constructed. HL overrides that face of the glyph; e.g. it is
21117 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21118 x-position of the drawing area. */
21120 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21121 do { \
21122 int face_id = (row)->glyphs[area][START].face_id; \
21123 struct face *base_face = FACE_FROM_ID (f, face_id); \
21124 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21125 struct composition *cmp = composition_table[cmp_id]; \
21126 XChar2b *char2b; \
21127 struct glyph_string *first_s; \
21128 int n; \
21130 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21132 /* Make glyph_strings for each glyph sequence that is drawable by \
21133 the same face, and append them to HEAD/TAIL. */ \
21134 for (n = 0; n < cmp->glyph_len;) \
21136 s = (struct glyph_string *) alloca (sizeof *s); \
21137 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21138 append_glyph_string (&(HEAD), &(TAIL), s); \
21139 s->cmp = cmp; \
21140 s->cmp_from = n; \
21141 s->x = (X); \
21142 if (n == 0) \
21143 first_s = s; \
21144 n = fill_composite_glyph_string (s, base_face, overlaps); \
21147 ++START; \
21148 s = first_s; \
21149 } while (0)
21152 /* Add a glyph string for a glyph-string sequence to the list of strings
21153 between HEAD and TAIL. */
21155 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21156 do { \
21157 int face_id; \
21158 XChar2b *char2b; \
21159 Lisp_Object gstring; \
21161 face_id = (row)->glyphs[area][START].face_id; \
21162 gstring = (composition_gstring_from_id \
21163 ((row)->glyphs[area][START].u.cmp.id)); \
21164 s = (struct glyph_string *) alloca (sizeof *s); \
21165 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21166 * LGSTRING_GLYPH_LEN (gstring)); \
21167 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21168 append_glyph_string (&(HEAD), &(TAIL), s); \
21169 s->x = (X); \
21170 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21171 } while (0)
21174 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21175 of AREA of glyph row ROW on window W between indices START and END.
21176 HL overrides the face for drawing glyph strings, e.g. it is
21177 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21178 x-positions of the drawing area.
21180 This is an ugly monster macro construct because we must use alloca
21181 to allocate glyph strings (because draw_glyphs can be called
21182 asynchronously). */
21184 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21185 do \
21187 HEAD = TAIL = NULL; \
21188 while (START < END) \
21190 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21191 switch (first_glyph->type) \
21193 case CHAR_GLYPH: \
21194 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21195 HL, X, LAST_X); \
21196 break; \
21198 case COMPOSITE_GLYPH: \
21199 if (first_glyph->u.cmp.automatic) \
21200 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21201 HL, X, LAST_X); \
21202 else \
21203 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21204 HL, X, LAST_X); \
21205 break; \
21207 case STRETCH_GLYPH: \
21208 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21209 HL, X, LAST_X); \
21210 break; \
21212 case IMAGE_GLYPH: \
21213 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21214 HL, X, LAST_X); \
21215 break; \
21217 default: \
21218 abort (); \
21221 if (s) \
21223 set_glyph_string_background_width (s, START, LAST_X); \
21224 (X) += s->width; \
21227 } while (0)
21230 /* Draw glyphs between START and END in AREA of ROW on window W,
21231 starting at x-position X. X is relative to AREA in W. HL is a
21232 face-override with the following meaning:
21234 DRAW_NORMAL_TEXT draw normally
21235 DRAW_CURSOR draw in cursor face
21236 DRAW_MOUSE_FACE draw in mouse face.
21237 DRAW_INVERSE_VIDEO draw in mode line face
21238 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21239 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21241 If OVERLAPS is non-zero, draw only the foreground of characters and
21242 clip to the physical height of ROW. Non-zero value also defines
21243 the overlapping part to be drawn:
21245 OVERLAPS_PRED overlap with preceding rows
21246 OVERLAPS_SUCC overlap with succeeding rows
21247 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21248 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21250 Value is the x-position reached, relative to AREA of W. */
21252 static int
21253 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21254 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21255 enum draw_glyphs_face hl, int overlaps)
21257 struct glyph_string *head, *tail;
21258 struct glyph_string *s;
21259 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21260 int i, j, x_reached, last_x, area_left = 0;
21261 struct frame *f = XFRAME (WINDOW_FRAME (w));
21262 DECLARE_HDC (hdc);
21264 ALLOCATE_HDC (hdc, f);
21266 /* Let's rather be paranoid than getting a SEGV. */
21267 end = min (end, row->used[area]);
21268 start = max (0, start);
21269 start = min (end, start);
21271 /* Translate X to frame coordinates. Set last_x to the right
21272 end of the drawing area. */
21273 if (row->full_width_p)
21275 /* X is relative to the left edge of W, without scroll bars
21276 or fringes. */
21277 area_left = WINDOW_LEFT_EDGE_X (w);
21278 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21280 else
21282 area_left = window_box_left (w, area);
21283 last_x = area_left + window_box_width (w, area);
21285 x += area_left;
21287 /* Build a doubly-linked list of glyph_string structures between
21288 head and tail from what we have to draw. Note that the macro
21289 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21290 the reason we use a separate variable `i'. */
21291 i = start;
21292 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21293 if (tail)
21294 x_reached = tail->x + tail->background_width;
21295 else
21296 x_reached = x;
21298 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21299 the row, redraw some glyphs in front or following the glyph
21300 strings built above. */
21301 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21303 struct glyph_string *h, *t;
21304 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
21305 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21306 int dummy_x = 0;
21308 /* If mouse highlighting is on, we may need to draw adjacent
21309 glyphs using mouse-face highlighting. */
21310 if (area == TEXT_AREA && row->mouse_face_p)
21312 struct glyph_row *mouse_beg_row, *mouse_end_row;
21314 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
21315 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
21317 if (row >= mouse_beg_row && row <= mouse_end_row)
21319 check_mouse_face = 1;
21320 mouse_beg_col = (row == mouse_beg_row)
21321 ? hlinfo->mouse_face_beg_col : 0;
21322 mouse_end_col = (row == mouse_end_row)
21323 ? hlinfo->mouse_face_end_col
21324 : row->used[TEXT_AREA];
21328 /* Compute overhangs for all glyph strings. */
21329 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21330 for (s = head; s; s = s->next)
21331 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21333 /* Prepend glyph strings for glyphs in front of the first glyph
21334 string that are overwritten because of the first glyph
21335 string's left overhang. The background of all strings
21336 prepended must be drawn because the first glyph string
21337 draws over it. */
21338 i = left_overwritten (head);
21339 if (i >= 0)
21341 enum draw_glyphs_face overlap_hl;
21343 /* If this row contains mouse highlighting, attempt to draw
21344 the overlapped glyphs with the correct highlight. This
21345 code fails if the overlap encompasses more than one glyph
21346 and mouse-highlight spans only some of these glyphs.
21347 However, making it work perfectly involves a lot more
21348 code, and I don't know if the pathological case occurs in
21349 practice, so we'll stick to this for now. --- cyd */
21350 if (check_mouse_face
21351 && mouse_beg_col < start && mouse_end_col > i)
21352 overlap_hl = DRAW_MOUSE_FACE;
21353 else
21354 overlap_hl = DRAW_NORMAL_TEXT;
21356 j = i;
21357 BUILD_GLYPH_STRINGS (j, start, h, t,
21358 overlap_hl, dummy_x, last_x);
21359 start = i;
21360 compute_overhangs_and_x (t, head->x, 1);
21361 prepend_glyph_string_lists (&head, &tail, h, t);
21362 clip_head = head;
21365 /* Prepend glyph strings for glyphs in front of the first glyph
21366 string that overwrite that glyph string because of their
21367 right overhang. For these strings, only the foreground must
21368 be drawn, because it draws over the glyph string at `head'.
21369 The background must not be drawn because this would overwrite
21370 right overhangs of preceding glyphs for which no glyph
21371 strings exist. */
21372 i = left_overwriting (head);
21373 if (i >= 0)
21375 enum draw_glyphs_face overlap_hl;
21377 if (check_mouse_face
21378 && mouse_beg_col < start && mouse_end_col > i)
21379 overlap_hl = DRAW_MOUSE_FACE;
21380 else
21381 overlap_hl = DRAW_NORMAL_TEXT;
21383 clip_head = head;
21384 BUILD_GLYPH_STRINGS (i, start, h, t,
21385 overlap_hl, dummy_x, last_x);
21386 for (s = h; s; s = s->next)
21387 s->background_filled_p = 1;
21388 compute_overhangs_and_x (t, head->x, 1);
21389 prepend_glyph_string_lists (&head, &tail, h, t);
21392 /* Append glyphs strings for glyphs following the last glyph
21393 string tail that are overwritten by tail. The background of
21394 these strings has to be drawn because tail's foreground draws
21395 over it. */
21396 i = right_overwritten (tail);
21397 if (i >= 0)
21399 enum draw_glyphs_face overlap_hl;
21401 if (check_mouse_face
21402 && mouse_beg_col < i && mouse_end_col > end)
21403 overlap_hl = DRAW_MOUSE_FACE;
21404 else
21405 overlap_hl = DRAW_NORMAL_TEXT;
21407 BUILD_GLYPH_STRINGS (end, i, h, t,
21408 overlap_hl, x, last_x);
21409 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21410 we don't have `end = i;' here. */
21411 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21412 append_glyph_string_lists (&head, &tail, h, t);
21413 clip_tail = tail;
21416 /* Append glyph strings for glyphs following the last glyph
21417 string tail that overwrite tail. The foreground of such
21418 glyphs has to be drawn because it writes into the background
21419 of tail. The background must not be drawn because it could
21420 paint over the foreground of following glyphs. */
21421 i = right_overwriting (tail);
21422 if (i >= 0)
21424 enum draw_glyphs_face overlap_hl;
21425 if (check_mouse_face
21426 && mouse_beg_col < i && mouse_end_col > end)
21427 overlap_hl = DRAW_MOUSE_FACE;
21428 else
21429 overlap_hl = DRAW_NORMAL_TEXT;
21431 clip_tail = tail;
21432 i++; /* We must include the Ith glyph. */
21433 BUILD_GLYPH_STRINGS (end, i, h, t,
21434 overlap_hl, x, last_x);
21435 for (s = h; s; s = s->next)
21436 s->background_filled_p = 1;
21437 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21438 append_glyph_string_lists (&head, &tail, h, t);
21440 if (clip_head || clip_tail)
21441 for (s = head; s; s = s->next)
21443 s->clip_head = clip_head;
21444 s->clip_tail = clip_tail;
21448 /* Draw all strings. */
21449 for (s = head; s; s = s->next)
21450 FRAME_RIF (f)->draw_glyph_string (s);
21452 #ifndef HAVE_NS
21453 /* When focus a sole frame and move horizontally, this sets on_p to 0
21454 causing a failure to erase prev cursor position. */
21455 if (area == TEXT_AREA
21456 && !row->full_width_p
21457 /* When drawing overlapping rows, only the glyph strings'
21458 foreground is drawn, which doesn't erase a cursor
21459 completely. */
21460 && !overlaps)
21462 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21463 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21464 : (tail ? tail->x + tail->background_width : x));
21465 x0 -= area_left;
21466 x1 -= area_left;
21468 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21469 row->y, MATRIX_ROW_BOTTOM_Y (row));
21471 #endif
21473 /* Value is the x-position up to which drawn, relative to AREA of W.
21474 This doesn't include parts drawn because of overhangs. */
21475 if (row->full_width_p)
21476 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21477 else
21478 x_reached -= area_left;
21480 RELEASE_HDC (hdc, f);
21482 return x_reached;
21485 /* Expand row matrix if too narrow. Don't expand if area
21486 is not present. */
21488 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21490 if (!fonts_changed_p \
21491 && (it->glyph_row->glyphs[area] \
21492 < it->glyph_row->glyphs[area + 1])) \
21494 it->w->ncols_scale_factor++; \
21495 fonts_changed_p = 1; \
21499 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21500 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21502 static INLINE void
21503 append_glyph (struct it *it)
21505 struct glyph *glyph;
21506 enum glyph_row_area area = it->area;
21508 xassert (it->glyph_row);
21509 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21511 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21512 if (glyph < it->glyph_row->glyphs[area + 1])
21514 /* If the glyph row is reversed, we need to prepend the glyph
21515 rather than append it. */
21516 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21518 struct glyph *g;
21520 /* Make room for the additional glyph. */
21521 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21522 g[1] = *g;
21523 glyph = it->glyph_row->glyphs[area];
21525 glyph->charpos = CHARPOS (it->position);
21526 glyph->object = it->object;
21527 if (it->pixel_width > 0)
21529 glyph->pixel_width = it->pixel_width;
21530 glyph->padding_p = 0;
21532 else
21534 /* Assure at least 1-pixel width. Otherwise, cursor can't
21535 be displayed correctly. */
21536 glyph->pixel_width = 1;
21537 glyph->padding_p = 1;
21539 glyph->ascent = it->ascent;
21540 glyph->descent = it->descent;
21541 glyph->voffset = it->voffset;
21542 glyph->type = CHAR_GLYPH;
21543 glyph->avoid_cursor_p = it->avoid_cursor_p;
21544 glyph->multibyte_p = it->multibyte_p;
21545 glyph->left_box_line_p = it->start_of_box_run_p;
21546 glyph->right_box_line_p = it->end_of_box_run_p;
21547 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21548 || it->phys_descent > it->descent);
21549 glyph->glyph_not_available_p = it->glyph_not_available_p;
21550 glyph->face_id = it->face_id;
21551 glyph->u.ch = it->char_to_display;
21552 glyph->slice.img = null_glyph_slice;
21553 glyph->font_type = FONT_TYPE_UNKNOWN;
21554 if (it->bidi_p)
21556 glyph->resolved_level = it->bidi_it.resolved_level;
21557 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21558 abort ();
21559 glyph->bidi_type = it->bidi_it.type;
21561 else
21563 glyph->resolved_level = 0;
21564 glyph->bidi_type = UNKNOWN_BT;
21566 ++it->glyph_row->used[area];
21568 else
21569 IT_EXPAND_MATRIX_WIDTH (it, area);
21572 /* Store one glyph for the composition IT->cmp_it.id in
21573 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21574 non-null. */
21576 static INLINE void
21577 append_composite_glyph (struct it *it)
21579 struct glyph *glyph;
21580 enum glyph_row_area area = it->area;
21582 xassert (it->glyph_row);
21584 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21585 if (glyph < it->glyph_row->glyphs[area + 1])
21587 /* If the glyph row is reversed, we need to prepend the glyph
21588 rather than append it. */
21589 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21591 struct glyph *g;
21593 /* Make room for the new glyph. */
21594 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21595 g[1] = *g;
21596 glyph = it->glyph_row->glyphs[it->area];
21598 glyph->charpos = it->cmp_it.charpos;
21599 glyph->object = it->object;
21600 glyph->pixel_width = it->pixel_width;
21601 glyph->ascent = it->ascent;
21602 glyph->descent = it->descent;
21603 glyph->voffset = it->voffset;
21604 glyph->type = COMPOSITE_GLYPH;
21605 if (it->cmp_it.ch < 0)
21607 glyph->u.cmp.automatic = 0;
21608 glyph->u.cmp.id = it->cmp_it.id;
21609 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
21611 else
21613 glyph->u.cmp.automatic = 1;
21614 glyph->u.cmp.id = it->cmp_it.id;
21615 glyph->slice.cmp.from = it->cmp_it.from;
21616 glyph->slice.cmp.to = it->cmp_it.to - 1;
21618 glyph->avoid_cursor_p = it->avoid_cursor_p;
21619 glyph->multibyte_p = it->multibyte_p;
21620 glyph->left_box_line_p = it->start_of_box_run_p;
21621 glyph->right_box_line_p = it->end_of_box_run_p;
21622 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21623 || it->phys_descent > it->descent);
21624 glyph->padding_p = 0;
21625 glyph->glyph_not_available_p = 0;
21626 glyph->face_id = it->face_id;
21627 glyph->font_type = FONT_TYPE_UNKNOWN;
21628 if (it->bidi_p)
21630 glyph->resolved_level = it->bidi_it.resolved_level;
21631 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21632 abort ();
21633 glyph->bidi_type = it->bidi_it.type;
21635 ++it->glyph_row->used[area];
21637 else
21638 IT_EXPAND_MATRIX_WIDTH (it, area);
21642 /* Change IT->ascent and IT->height according to the setting of
21643 IT->voffset. */
21645 static INLINE void
21646 take_vertical_position_into_account (struct it *it)
21648 if (it->voffset)
21650 if (it->voffset < 0)
21651 /* Increase the ascent so that we can display the text higher
21652 in the line. */
21653 it->ascent -= it->voffset;
21654 else
21655 /* Increase the descent so that we can display the text lower
21656 in the line. */
21657 it->descent += it->voffset;
21662 /* Produce glyphs/get display metrics for the image IT is loaded with.
21663 See the description of struct display_iterator in dispextern.h for
21664 an overview of struct display_iterator. */
21666 static void
21667 produce_image_glyph (struct it *it)
21669 struct image *img;
21670 struct face *face;
21671 int glyph_ascent, crop;
21672 struct glyph_slice slice;
21674 xassert (it->what == IT_IMAGE);
21676 face = FACE_FROM_ID (it->f, it->face_id);
21677 xassert (face);
21678 /* Make sure X resources of the face is loaded. */
21679 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21681 if (it->image_id < 0)
21683 /* Fringe bitmap. */
21684 it->ascent = it->phys_ascent = 0;
21685 it->descent = it->phys_descent = 0;
21686 it->pixel_width = 0;
21687 it->nglyphs = 0;
21688 return;
21691 img = IMAGE_FROM_ID (it->f, it->image_id);
21692 xassert (img);
21693 /* Make sure X resources of the image is loaded. */
21694 prepare_image_for_display (it->f, img);
21696 slice.x = slice.y = 0;
21697 slice.width = img->width;
21698 slice.height = img->height;
21700 if (INTEGERP (it->slice.x))
21701 slice.x = XINT (it->slice.x);
21702 else if (FLOATP (it->slice.x))
21703 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21705 if (INTEGERP (it->slice.y))
21706 slice.y = XINT (it->slice.y);
21707 else if (FLOATP (it->slice.y))
21708 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21710 if (INTEGERP (it->slice.width))
21711 slice.width = XINT (it->slice.width);
21712 else if (FLOATP (it->slice.width))
21713 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21715 if (INTEGERP (it->slice.height))
21716 slice.height = XINT (it->slice.height);
21717 else if (FLOATP (it->slice.height))
21718 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21720 if (slice.x >= img->width)
21721 slice.x = img->width;
21722 if (slice.y >= img->height)
21723 slice.y = img->height;
21724 if (slice.x + slice.width >= img->width)
21725 slice.width = img->width - slice.x;
21726 if (slice.y + slice.height > img->height)
21727 slice.height = img->height - slice.y;
21729 if (slice.width == 0 || slice.height == 0)
21730 return;
21732 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21734 it->descent = slice.height - glyph_ascent;
21735 if (slice.y == 0)
21736 it->descent += img->vmargin;
21737 if (slice.y + slice.height == img->height)
21738 it->descent += img->vmargin;
21739 it->phys_descent = it->descent;
21741 it->pixel_width = slice.width;
21742 if (slice.x == 0)
21743 it->pixel_width += img->hmargin;
21744 if (slice.x + slice.width == img->width)
21745 it->pixel_width += img->hmargin;
21747 /* It's quite possible for images to have an ascent greater than
21748 their height, so don't get confused in that case. */
21749 if (it->descent < 0)
21750 it->descent = 0;
21752 it->nglyphs = 1;
21754 if (face->box != FACE_NO_BOX)
21756 if (face->box_line_width > 0)
21758 if (slice.y == 0)
21759 it->ascent += face->box_line_width;
21760 if (slice.y + slice.height == img->height)
21761 it->descent += face->box_line_width;
21764 if (it->start_of_box_run_p && slice.x == 0)
21765 it->pixel_width += eabs (face->box_line_width);
21766 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21767 it->pixel_width += eabs (face->box_line_width);
21770 take_vertical_position_into_account (it);
21772 /* Automatically crop wide image glyphs at right edge so we can
21773 draw the cursor on same display row. */
21774 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21775 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21777 it->pixel_width -= crop;
21778 slice.width -= crop;
21781 if (it->glyph_row)
21783 struct glyph *glyph;
21784 enum glyph_row_area area = it->area;
21786 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21787 if (glyph < it->glyph_row->glyphs[area + 1])
21789 glyph->charpos = CHARPOS (it->position);
21790 glyph->object = it->object;
21791 glyph->pixel_width = it->pixel_width;
21792 glyph->ascent = glyph_ascent;
21793 glyph->descent = it->descent;
21794 glyph->voffset = it->voffset;
21795 glyph->type = IMAGE_GLYPH;
21796 glyph->avoid_cursor_p = it->avoid_cursor_p;
21797 glyph->multibyte_p = it->multibyte_p;
21798 glyph->left_box_line_p = it->start_of_box_run_p;
21799 glyph->right_box_line_p = it->end_of_box_run_p;
21800 glyph->overlaps_vertically_p = 0;
21801 glyph->padding_p = 0;
21802 glyph->glyph_not_available_p = 0;
21803 glyph->face_id = it->face_id;
21804 glyph->u.img_id = img->id;
21805 glyph->slice.img = slice;
21806 glyph->font_type = FONT_TYPE_UNKNOWN;
21807 if (it->bidi_p)
21809 glyph->resolved_level = it->bidi_it.resolved_level;
21810 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21811 abort ();
21812 glyph->bidi_type = it->bidi_it.type;
21814 ++it->glyph_row->used[area];
21816 else
21817 IT_EXPAND_MATRIX_WIDTH (it, area);
21822 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21823 of the glyph, WIDTH and HEIGHT are the width and height of the
21824 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21826 static void
21827 append_stretch_glyph (struct it *it, Lisp_Object object,
21828 int width, int height, int ascent)
21830 struct glyph *glyph;
21831 enum glyph_row_area area = it->area;
21833 xassert (ascent >= 0 && ascent <= height);
21835 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21836 if (glyph < it->glyph_row->glyphs[area + 1])
21838 /* If the glyph row is reversed, we need to prepend the glyph
21839 rather than append it. */
21840 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21842 struct glyph *g;
21844 /* Make room for the additional glyph. */
21845 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21846 g[1] = *g;
21847 glyph = it->glyph_row->glyphs[area];
21849 glyph->charpos = CHARPOS (it->position);
21850 glyph->object = object;
21851 glyph->pixel_width = width;
21852 glyph->ascent = ascent;
21853 glyph->descent = height - ascent;
21854 glyph->voffset = it->voffset;
21855 glyph->type = STRETCH_GLYPH;
21856 glyph->avoid_cursor_p = it->avoid_cursor_p;
21857 glyph->multibyte_p = it->multibyte_p;
21858 glyph->left_box_line_p = it->start_of_box_run_p;
21859 glyph->right_box_line_p = it->end_of_box_run_p;
21860 glyph->overlaps_vertically_p = 0;
21861 glyph->padding_p = 0;
21862 glyph->glyph_not_available_p = 0;
21863 glyph->face_id = it->face_id;
21864 glyph->u.stretch.ascent = ascent;
21865 glyph->u.stretch.height = height;
21866 glyph->slice.img = null_glyph_slice;
21867 glyph->font_type = FONT_TYPE_UNKNOWN;
21868 if (it->bidi_p)
21870 glyph->resolved_level = it->bidi_it.resolved_level;
21871 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21872 abort ();
21873 glyph->bidi_type = it->bidi_it.type;
21875 else
21877 glyph->resolved_level = 0;
21878 glyph->bidi_type = UNKNOWN_BT;
21880 ++it->glyph_row->used[area];
21882 else
21883 IT_EXPAND_MATRIX_WIDTH (it, area);
21887 /* Produce a stretch glyph for iterator IT. IT->object is the value
21888 of the glyph property displayed. The value must be a list
21889 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21890 being recognized:
21892 1. `:width WIDTH' specifies that the space should be WIDTH *
21893 canonical char width wide. WIDTH may be an integer or floating
21894 point number.
21896 2. `:relative-width FACTOR' specifies that the width of the stretch
21897 should be computed from the width of the first character having the
21898 `glyph' property, and should be FACTOR times that width.
21900 3. `:align-to HPOS' specifies that the space should be wide enough
21901 to reach HPOS, a value in canonical character units.
21903 Exactly one of the above pairs must be present.
21905 4. `:height HEIGHT' specifies that the height of the stretch produced
21906 should be HEIGHT, measured in canonical character units.
21908 5. `:relative-height FACTOR' specifies that the height of the
21909 stretch should be FACTOR times the height of the characters having
21910 the glyph property.
21912 Either none or exactly one of 4 or 5 must be present.
21914 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21915 of the stretch should be used for the ascent of the stretch.
21916 ASCENT must be in the range 0 <= ASCENT <= 100. */
21918 static void
21919 produce_stretch_glyph (struct it *it)
21921 /* (space :width WIDTH :height HEIGHT ...) */
21922 Lisp_Object prop, plist;
21923 int width = 0, height = 0, align_to = -1;
21924 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21925 int ascent = 0;
21926 double tem;
21927 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21928 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21930 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21932 /* List should start with `space'. */
21933 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21934 plist = XCDR (it->object);
21936 /* Compute the width of the stretch. */
21937 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21938 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21940 /* Absolute width `:width WIDTH' specified and valid. */
21941 zero_width_ok_p = 1;
21942 width = (int)tem;
21944 else if (prop = Fplist_get (plist, QCrelative_width),
21945 NUMVAL (prop) > 0)
21947 /* Relative width `:relative-width FACTOR' specified and valid.
21948 Compute the width of the characters having the `glyph'
21949 property. */
21950 struct it it2;
21951 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21953 it2 = *it;
21954 if (it->multibyte_p)
21955 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
21956 else
21958 it2.c = it2.char_to_display = *p, it2.len = 1;
21959 if (! ASCII_CHAR_P (it2.c))
21960 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
21963 it2.glyph_row = NULL;
21964 it2.what = IT_CHARACTER;
21965 x_produce_glyphs (&it2);
21966 width = NUMVAL (prop) * it2.pixel_width;
21968 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21969 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21971 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21972 align_to = (align_to < 0
21974 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21975 else if (align_to < 0)
21976 align_to = window_box_left_offset (it->w, TEXT_AREA);
21977 width = max (0, (int)tem + align_to - it->current_x);
21978 zero_width_ok_p = 1;
21980 else
21981 /* Nothing specified -> width defaults to canonical char width. */
21982 width = FRAME_COLUMN_WIDTH (it->f);
21984 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21985 width = 1;
21987 /* Compute height. */
21988 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21989 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21991 height = (int)tem;
21992 zero_height_ok_p = 1;
21994 else if (prop = Fplist_get (plist, QCrelative_height),
21995 NUMVAL (prop) > 0)
21996 height = FONT_HEIGHT (font) * NUMVAL (prop);
21997 else
21998 height = FONT_HEIGHT (font);
22000 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22001 height = 1;
22003 /* Compute percentage of height used for ascent. If
22004 `:ascent ASCENT' is present and valid, use that. Otherwise,
22005 derive the ascent from the font in use. */
22006 if (prop = Fplist_get (plist, QCascent),
22007 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22008 ascent = height * NUMVAL (prop) / 100.0;
22009 else if (!NILP (prop)
22010 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22011 ascent = min (max (0, (int)tem), height);
22012 else
22013 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22015 if (width > 0 && it->line_wrap != TRUNCATE
22016 && it->current_x + width > it->last_visible_x)
22017 width = it->last_visible_x - it->current_x - 1;
22019 if (width > 0 && height > 0 && it->glyph_row)
22021 Lisp_Object object = it->stack[it->sp - 1].string;
22022 if (!STRINGP (object))
22023 object = it->w->buffer;
22024 append_stretch_glyph (it, object, width, height, ascent);
22027 it->pixel_width = width;
22028 it->ascent = it->phys_ascent = ascent;
22029 it->descent = it->phys_descent = height - it->ascent;
22030 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22032 take_vertical_position_into_account (it);
22035 /* Calculate line-height and line-spacing properties.
22036 An integer value specifies explicit pixel value.
22037 A float value specifies relative value to current face height.
22038 A cons (float . face-name) specifies relative value to
22039 height of specified face font.
22041 Returns height in pixels, or nil. */
22044 static Lisp_Object
22045 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22046 int boff, int override)
22048 Lisp_Object face_name = Qnil;
22049 int ascent, descent, height;
22051 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22052 return val;
22054 if (CONSP (val))
22056 face_name = XCAR (val);
22057 val = XCDR (val);
22058 if (!NUMBERP (val))
22059 val = make_number (1);
22060 if (NILP (face_name))
22062 height = it->ascent + it->descent;
22063 goto scale;
22067 if (NILP (face_name))
22069 font = FRAME_FONT (it->f);
22070 boff = FRAME_BASELINE_OFFSET (it->f);
22072 else if (EQ (face_name, Qt))
22074 override = 0;
22076 else
22078 int face_id;
22079 struct face *face;
22081 face_id = lookup_named_face (it->f, face_name, 0);
22082 if (face_id < 0)
22083 return make_number (-1);
22085 face = FACE_FROM_ID (it->f, face_id);
22086 font = face->font;
22087 if (font == NULL)
22088 return make_number (-1);
22089 boff = font->baseline_offset;
22090 if (font->vertical_centering)
22091 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22094 ascent = FONT_BASE (font) + boff;
22095 descent = FONT_DESCENT (font) - boff;
22097 if (override)
22099 it->override_ascent = ascent;
22100 it->override_descent = descent;
22101 it->override_boff = boff;
22104 height = ascent + descent;
22106 scale:
22107 if (FLOATP (val))
22108 height = (int)(XFLOAT_DATA (val) * height);
22109 else if (INTEGERP (val))
22110 height *= XINT (val);
22112 return make_number (height);
22116 /* RIF:
22117 Produce glyphs/get display metrics for the display element IT is
22118 loaded with. See the description of struct it in dispextern.h
22119 for an overview of struct it. */
22121 void
22122 x_produce_glyphs (struct it *it)
22124 int extra_line_spacing = it->extra_line_spacing;
22126 it->glyph_not_available_p = 0;
22128 if (it->what == IT_CHARACTER)
22130 XChar2b char2b;
22131 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22132 struct font *font = face->font;
22133 int font_not_found_p = font == NULL;
22134 struct font_metrics *pcm = NULL;
22135 int boff; /* baseline offset */
22137 if (font_not_found_p)
22139 /* When no suitable font found, display an empty box based
22140 on the metrics of the font of the default face (or what
22141 remapped). */
22142 struct face *no_font_face
22143 = FACE_FROM_ID (it->f,
22144 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
22145 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
22146 font = no_font_face->font;
22147 boff = font->baseline_offset;
22149 else
22151 boff = font->baseline_offset;
22152 if (font->vertical_centering)
22153 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22156 if (it->char_to_display != '\n' && it->char_to_display != '\t')
22158 int stretched_p;
22160 it->nglyphs = 1;
22162 if (it->override_ascent >= 0)
22164 it->ascent = it->override_ascent;
22165 it->descent = it->override_descent;
22166 boff = it->override_boff;
22168 else
22170 it->ascent = FONT_BASE (font) + boff;
22171 it->descent = FONT_DESCENT (font) - boff;
22174 if (! font_not_found_p
22175 && get_char_glyph_code (it->char_to_display, font, &char2b))
22177 pcm = get_per_char_metric (it->f, font, &char2b);
22178 if (pcm->width == 0
22179 && pcm->rbearing == 0 && pcm->lbearing == 0)
22180 pcm = NULL;
22183 if (pcm)
22185 it->phys_ascent = pcm->ascent + boff;
22186 it->phys_descent = pcm->descent - boff;
22187 it->pixel_width = pcm->width;
22189 else
22191 it->glyph_not_available_p = 1;
22192 it->phys_ascent = it->ascent;
22193 it->phys_descent = it->descent;
22194 it->pixel_width = font->space_width;
22197 if (it->constrain_row_ascent_descent_p)
22199 if (it->descent > it->max_descent)
22201 it->ascent += it->descent - it->max_descent;
22202 it->descent = it->max_descent;
22204 if (it->ascent > it->max_ascent)
22206 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22207 it->ascent = it->max_ascent;
22209 it->phys_ascent = min (it->phys_ascent, it->ascent);
22210 it->phys_descent = min (it->phys_descent, it->descent);
22211 extra_line_spacing = 0;
22214 /* If this is a space inside a region of text with
22215 `space-width' property, change its width. */
22216 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22217 if (stretched_p)
22218 it->pixel_width *= XFLOATINT (it->space_width);
22220 /* If face has a box, add the box thickness to the character
22221 height. If character has a box line to the left and/or
22222 right, add the box line width to the character's width. */
22223 if (face->box != FACE_NO_BOX)
22225 int thick = face->box_line_width;
22227 if (thick > 0)
22229 it->ascent += thick;
22230 it->descent += thick;
22232 else
22233 thick = -thick;
22235 if (it->start_of_box_run_p)
22236 it->pixel_width += thick;
22237 if (it->end_of_box_run_p)
22238 it->pixel_width += thick;
22241 /* If face has an overline, add the height of the overline
22242 (1 pixel) and a 1 pixel margin to the character height. */
22243 if (face->overline_p)
22244 it->ascent += overline_margin;
22246 if (it->constrain_row_ascent_descent_p)
22248 if (it->ascent > it->max_ascent)
22249 it->ascent = it->max_ascent;
22250 if (it->descent > it->max_descent)
22251 it->descent = it->max_descent;
22254 take_vertical_position_into_account (it);
22256 /* If we have to actually produce glyphs, do it. */
22257 if (it->glyph_row)
22259 if (stretched_p)
22261 /* Translate a space with a `space-width' property
22262 into a stretch glyph. */
22263 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22264 / FONT_HEIGHT (font));
22265 append_stretch_glyph (it, it->object, it->pixel_width,
22266 it->ascent + it->descent, ascent);
22268 else
22269 append_glyph (it);
22271 /* If characters with lbearing or rbearing are displayed
22272 in this line, record that fact in a flag of the
22273 glyph row. This is used to optimize X output code. */
22274 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22275 it->glyph_row->contains_overlapping_glyphs_p = 1;
22277 if (! stretched_p && it->pixel_width == 0)
22278 /* We assure that all visible glyphs have at least 1-pixel
22279 width. */
22280 it->pixel_width = 1;
22282 else if (it->char_to_display == '\n')
22284 /* A newline has no width, but we need the height of the
22285 line. But if previous part of the line sets a height,
22286 don't increase that height */
22288 Lisp_Object height;
22289 Lisp_Object total_height = Qnil;
22291 it->override_ascent = -1;
22292 it->pixel_width = 0;
22293 it->nglyphs = 0;
22295 height = get_it_property (it, Qline_height);
22296 /* Split (line-height total-height) list */
22297 if (CONSP (height)
22298 && CONSP (XCDR (height))
22299 && NILP (XCDR (XCDR (height))))
22301 total_height = XCAR (XCDR (height));
22302 height = XCAR (height);
22304 height = calc_line_height_property (it, height, font, boff, 1);
22306 if (it->override_ascent >= 0)
22308 it->ascent = it->override_ascent;
22309 it->descent = it->override_descent;
22310 boff = it->override_boff;
22312 else
22314 it->ascent = FONT_BASE (font) + boff;
22315 it->descent = FONT_DESCENT (font) - boff;
22318 if (EQ (height, Qt))
22320 if (it->descent > it->max_descent)
22322 it->ascent += it->descent - it->max_descent;
22323 it->descent = it->max_descent;
22325 if (it->ascent > it->max_ascent)
22327 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22328 it->ascent = it->max_ascent;
22330 it->phys_ascent = min (it->phys_ascent, it->ascent);
22331 it->phys_descent = min (it->phys_descent, it->descent);
22332 it->constrain_row_ascent_descent_p = 1;
22333 extra_line_spacing = 0;
22335 else
22337 Lisp_Object spacing;
22339 it->phys_ascent = it->ascent;
22340 it->phys_descent = it->descent;
22342 if ((it->max_ascent > 0 || it->max_descent > 0)
22343 && face->box != FACE_NO_BOX
22344 && face->box_line_width > 0)
22346 it->ascent += face->box_line_width;
22347 it->descent += face->box_line_width;
22349 if (!NILP (height)
22350 && XINT (height) > it->ascent + it->descent)
22351 it->ascent = XINT (height) - it->descent;
22353 if (!NILP (total_height))
22354 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22355 else
22357 spacing = get_it_property (it, Qline_spacing);
22358 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22360 if (INTEGERP (spacing))
22362 extra_line_spacing = XINT (spacing);
22363 if (!NILP (total_height))
22364 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22368 else /* i.e. (it->char_to_display == '\t') */
22370 if (font->space_width > 0)
22372 int tab_width = it->tab_width * font->space_width;
22373 int x = it->current_x + it->continuation_lines_width;
22374 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22376 /* If the distance from the current position to the next tab
22377 stop is less than a space character width, use the
22378 tab stop after that. */
22379 if (next_tab_x - x < font->space_width)
22380 next_tab_x += tab_width;
22382 it->pixel_width = next_tab_x - x;
22383 it->nglyphs = 1;
22384 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22385 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22387 if (it->glyph_row)
22389 append_stretch_glyph (it, it->object, it->pixel_width,
22390 it->ascent + it->descent, it->ascent);
22393 else
22395 it->pixel_width = 0;
22396 it->nglyphs = 1;
22400 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22402 /* A static composition.
22404 Note: A composition is represented as one glyph in the
22405 glyph matrix. There are no padding glyphs.
22407 Important note: pixel_width, ascent, and descent are the
22408 values of what is drawn by draw_glyphs (i.e. the values of
22409 the overall glyphs composed). */
22410 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22411 int boff; /* baseline offset */
22412 struct composition *cmp = composition_table[it->cmp_it.id];
22413 int glyph_len = cmp->glyph_len;
22414 struct font *font = face->font;
22416 it->nglyphs = 1;
22418 /* If we have not yet calculated pixel size data of glyphs of
22419 the composition for the current face font, calculate them
22420 now. Theoretically, we have to check all fonts for the
22421 glyphs, but that requires much time and memory space. So,
22422 here we check only the font of the first glyph. This may
22423 lead to incorrect display, but it's very rare, and C-l
22424 (recenter-top-bottom) can correct the display anyway. */
22425 if (! cmp->font || cmp->font != font)
22427 /* Ascent and descent of the font of the first character
22428 of this composition (adjusted by baseline offset).
22429 Ascent and descent of overall glyphs should not be less
22430 than these, respectively. */
22431 int font_ascent, font_descent, font_height;
22432 /* Bounding box of the overall glyphs. */
22433 int leftmost, rightmost, lowest, highest;
22434 int lbearing, rbearing;
22435 int i, width, ascent, descent;
22436 int left_padded = 0, right_padded = 0;
22437 int c;
22438 XChar2b char2b;
22439 struct font_metrics *pcm;
22440 int font_not_found_p;
22441 EMACS_INT pos;
22443 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22444 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22445 break;
22446 if (glyph_len < cmp->glyph_len)
22447 right_padded = 1;
22448 for (i = 0; i < glyph_len; i++)
22450 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22451 break;
22452 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22454 if (i > 0)
22455 left_padded = 1;
22457 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22458 : IT_CHARPOS (*it));
22459 /* If no suitable font is found, use the default font. */
22460 font_not_found_p = font == NULL;
22461 if (font_not_found_p)
22463 face = face->ascii_face;
22464 font = face->font;
22466 boff = font->baseline_offset;
22467 if (font->vertical_centering)
22468 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22469 font_ascent = FONT_BASE (font) + boff;
22470 font_descent = FONT_DESCENT (font) - boff;
22471 font_height = FONT_HEIGHT (font);
22473 cmp->font = (void *) font;
22475 pcm = NULL;
22476 if (! font_not_found_p)
22478 get_char_face_and_encoding (it->f, c, it->face_id,
22479 &char2b, it->multibyte_p, 0);
22480 pcm = get_per_char_metric (it->f, font, &char2b);
22483 /* Initialize the bounding box. */
22484 if (pcm)
22486 width = pcm->width;
22487 ascent = pcm->ascent;
22488 descent = pcm->descent;
22489 lbearing = pcm->lbearing;
22490 rbearing = pcm->rbearing;
22492 else
22494 width = font->space_width;
22495 ascent = FONT_BASE (font);
22496 descent = FONT_DESCENT (font);
22497 lbearing = 0;
22498 rbearing = width;
22501 rightmost = width;
22502 leftmost = 0;
22503 lowest = - descent + boff;
22504 highest = ascent + boff;
22506 if (! font_not_found_p
22507 && font->default_ascent
22508 && CHAR_TABLE_P (Vuse_default_ascent)
22509 && !NILP (Faref (Vuse_default_ascent,
22510 make_number (it->char_to_display))))
22511 highest = font->default_ascent + boff;
22513 /* Draw the first glyph at the normal position. It may be
22514 shifted to right later if some other glyphs are drawn
22515 at the left. */
22516 cmp->offsets[i * 2] = 0;
22517 cmp->offsets[i * 2 + 1] = boff;
22518 cmp->lbearing = lbearing;
22519 cmp->rbearing = rbearing;
22521 /* Set cmp->offsets for the remaining glyphs. */
22522 for (i++; i < glyph_len; i++)
22524 int left, right, btm, top;
22525 int ch = COMPOSITION_GLYPH (cmp, i);
22526 int face_id;
22527 struct face *this_face;
22528 int this_boff;
22530 if (ch == '\t')
22531 ch = ' ';
22532 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22533 this_face = FACE_FROM_ID (it->f, face_id);
22534 font = this_face->font;
22536 if (font == NULL)
22537 pcm = NULL;
22538 else
22540 this_boff = font->baseline_offset;
22541 if (font->vertical_centering)
22542 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22543 get_char_face_and_encoding (it->f, ch, face_id,
22544 &char2b, it->multibyte_p, 0);
22545 pcm = get_per_char_metric (it->f, font, &char2b);
22547 if (! pcm)
22548 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22549 else
22551 width = pcm->width;
22552 ascent = pcm->ascent;
22553 descent = pcm->descent;
22554 lbearing = pcm->lbearing;
22555 rbearing = pcm->rbearing;
22556 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22558 /* Relative composition with or without
22559 alternate chars. */
22560 left = (leftmost + rightmost - width) / 2;
22561 btm = - descent + boff;
22562 if (font->relative_compose
22563 && (! CHAR_TABLE_P (Vignore_relative_composition)
22564 || NILP (Faref (Vignore_relative_composition,
22565 make_number (ch)))))
22568 if (- descent >= font->relative_compose)
22569 /* One extra pixel between two glyphs. */
22570 btm = highest + 1;
22571 else if (ascent <= 0)
22572 /* One extra pixel between two glyphs. */
22573 btm = lowest - 1 - ascent - descent;
22576 else
22578 /* A composition rule is specified by an integer
22579 value that encodes global and new reference
22580 points (GREF and NREF). GREF and NREF are
22581 specified by numbers as below:
22583 0---1---2 -- ascent
22587 9--10--11 -- center
22589 ---3---4---5--- baseline
22591 6---7---8 -- descent
22593 int rule = COMPOSITION_RULE (cmp, i);
22594 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22596 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22597 grefx = gref % 3, nrefx = nref % 3;
22598 grefy = gref / 3, nrefy = nref / 3;
22599 if (xoff)
22600 xoff = font_height * (xoff - 128) / 256;
22601 if (yoff)
22602 yoff = font_height * (yoff - 128) / 256;
22604 left = (leftmost
22605 + grefx * (rightmost - leftmost) / 2
22606 - nrefx * width / 2
22607 + xoff);
22609 btm = ((grefy == 0 ? highest
22610 : grefy == 1 ? 0
22611 : grefy == 2 ? lowest
22612 : (highest + lowest) / 2)
22613 - (nrefy == 0 ? ascent + descent
22614 : nrefy == 1 ? descent - boff
22615 : nrefy == 2 ? 0
22616 : (ascent + descent) / 2)
22617 + yoff);
22620 cmp->offsets[i * 2] = left;
22621 cmp->offsets[i * 2 + 1] = btm + descent;
22623 /* Update the bounding box of the overall glyphs. */
22624 if (width > 0)
22626 right = left + width;
22627 if (left < leftmost)
22628 leftmost = left;
22629 if (right > rightmost)
22630 rightmost = right;
22632 top = btm + descent + ascent;
22633 if (top > highest)
22634 highest = top;
22635 if (btm < lowest)
22636 lowest = btm;
22638 if (cmp->lbearing > left + lbearing)
22639 cmp->lbearing = left + lbearing;
22640 if (cmp->rbearing < left + rbearing)
22641 cmp->rbearing = left + rbearing;
22645 /* If there are glyphs whose x-offsets are negative,
22646 shift all glyphs to the right and make all x-offsets
22647 non-negative. */
22648 if (leftmost < 0)
22650 for (i = 0; i < cmp->glyph_len; i++)
22651 cmp->offsets[i * 2] -= leftmost;
22652 rightmost -= leftmost;
22653 cmp->lbearing -= leftmost;
22654 cmp->rbearing -= leftmost;
22657 if (left_padded && cmp->lbearing < 0)
22659 for (i = 0; i < cmp->glyph_len; i++)
22660 cmp->offsets[i * 2] -= cmp->lbearing;
22661 rightmost -= cmp->lbearing;
22662 cmp->rbearing -= cmp->lbearing;
22663 cmp->lbearing = 0;
22665 if (right_padded && rightmost < cmp->rbearing)
22667 rightmost = cmp->rbearing;
22670 cmp->pixel_width = rightmost;
22671 cmp->ascent = highest;
22672 cmp->descent = - lowest;
22673 if (cmp->ascent < font_ascent)
22674 cmp->ascent = font_ascent;
22675 if (cmp->descent < font_descent)
22676 cmp->descent = font_descent;
22679 if (it->glyph_row
22680 && (cmp->lbearing < 0
22681 || cmp->rbearing > cmp->pixel_width))
22682 it->glyph_row->contains_overlapping_glyphs_p = 1;
22684 it->pixel_width = cmp->pixel_width;
22685 it->ascent = it->phys_ascent = cmp->ascent;
22686 it->descent = it->phys_descent = cmp->descent;
22687 if (face->box != FACE_NO_BOX)
22689 int thick = face->box_line_width;
22691 if (thick > 0)
22693 it->ascent += thick;
22694 it->descent += thick;
22696 else
22697 thick = - thick;
22699 if (it->start_of_box_run_p)
22700 it->pixel_width += thick;
22701 if (it->end_of_box_run_p)
22702 it->pixel_width += thick;
22705 /* If face has an overline, add the height of the overline
22706 (1 pixel) and a 1 pixel margin to the character height. */
22707 if (face->overline_p)
22708 it->ascent += overline_margin;
22710 take_vertical_position_into_account (it);
22711 if (it->ascent < 0)
22712 it->ascent = 0;
22713 if (it->descent < 0)
22714 it->descent = 0;
22716 if (it->glyph_row)
22717 append_composite_glyph (it);
22719 else if (it->what == IT_COMPOSITION)
22721 /* A dynamic (automatic) composition. */
22722 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22723 Lisp_Object gstring;
22724 struct font_metrics metrics;
22726 gstring = composition_gstring_from_id (it->cmp_it.id);
22727 it->pixel_width
22728 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22729 &metrics);
22730 if (it->glyph_row
22731 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22732 it->glyph_row->contains_overlapping_glyphs_p = 1;
22733 it->ascent = it->phys_ascent = metrics.ascent;
22734 it->descent = it->phys_descent = metrics.descent;
22735 if (face->box != FACE_NO_BOX)
22737 int thick = face->box_line_width;
22739 if (thick > 0)
22741 it->ascent += thick;
22742 it->descent += thick;
22744 else
22745 thick = - thick;
22747 if (it->start_of_box_run_p)
22748 it->pixel_width += thick;
22749 if (it->end_of_box_run_p)
22750 it->pixel_width += thick;
22752 /* If face has an overline, add the height of the overline
22753 (1 pixel) and a 1 pixel margin to the character height. */
22754 if (face->overline_p)
22755 it->ascent += overline_margin;
22756 take_vertical_position_into_account (it);
22757 if (it->ascent < 0)
22758 it->ascent = 0;
22759 if (it->descent < 0)
22760 it->descent = 0;
22762 if (it->glyph_row)
22763 append_composite_glyph (it);
22765 else if (it->what == IT_IMAGE)
22766 produce_image_glyph (it);
22767 else if (it->what == IT_STRETCH)
22768 produce_stretch_glyph (it);
22770 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22771 because this isn't true for images with `:ascent 100'. */
22772 xassert (it->ascent >= 0 && it->descent >= 0);
22773 if (it->area == TEXT_AREA)
22774 it->current_x += it->pixel_width;
22776 if (extra_line_spacing > 0)
22778 it->descent += extra_line_spacing;
22779 if (extra_line_spacing > it->max_extra_line_spacing)
22780 it->max_extra_line_spacing = extra_line_spacing;
22783 it->max_ascent = max (it->max_ascent, it->ascent);
22784 it->max_descent = max (it->max_descent, it->descent);
22785 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22786 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22789 /* EXPORT for RIF:
22790 Output LEN glyphs starting at START at the nominal cursor position.
22791 Advance the nominal cursor over the text. The global variable
22792 updated_window contains the window being updated, updated_row is
22793 the glyph row being updated, and updated_area is the area of that
22794 row being updated. */
22796 void
22797 x_write_glyphs (struct glyph *start, int len)
22799 int x, hpos;
22801 xassert (updated_window && updated_row);
22802 BLOCK_INPUT;
22804 /* Write glyphs. */
22806 hpos = start - updated_row->glyphs[updated_area];
22807 x = draw_glyphs (updated_window, output_cursor.x,
22808 updated_row, updated_area,
22809 hpos, hpos + len,
22810 DRAW_NORMAL_TEXT, 0);
22812 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22813 if (updated_area == TEXT_AREA
22814 && updated_window->phys_cursor_on_p
22815 && updated_window->phys_cursor.vpos == output_cursor.vpos
22816 && updated_window->phys_cursor.hpos >= hpos
22817 && updated_window->phys_cursor.hpos < hpos + len)
22818 updated_window->phys_cursor_on_p = 0;
22820 UNBLOCK_INPUT;
22822 /* Advance the output cursor. */
22823 output_cursor.hpos += len;
22824 output_cursor.x = x;
22828 /* EXPORT for RIF:
22829 Insert LEN glyphs from START at the nominal cursor position. */
22831 void
22832 x_insert_glyphs (struct glyph *start, int len)
22834 struct frame *f;
22835 struct window *w;
22836 int line_height, shift_by_width, shifted_region_width;
22837 struct glyph_row *row;
22838 struct glyph *glyph;
22839 int frame_x, frame_y;
22840 EMACS_INT hpos;
22842 xassert (updated_window && updated_row);
22843 BLOCK_INPUT;
22844 w = updated_window;
22845 f = XFRAME (WINDOW_FRAME (w));
22847 /* Get the height of the line we are in. */
22848 row = updated_row;
22849 line_height = row->height;
22851 /* Get the width of the glyphs to insert. */
22852 shift_by_width = 0;
22853 for (glyph = start; glyph < start + len; ++glyph)
22854 shift_by_width += glyph->pixel_width;
22856 /* Get the width of the region to shift right. */
22857 shifted_region_width = (window_box_width (w, updated_area)
22858 - output_cursor.x
22859 - shift_by_width);
22861 /* Shift right. */
22862 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22863 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22865 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22866 line_height, shift_by_width);
22868 /* Write the glyphs. */
22869 hpos = start - row->glyphs[updated_area];
22870 draw_glyphs (w, output_cursor.x, row, updated_area,
22871 hpos, hpos + len,
22872 DRAW_NORMAL_TEXT, 0);
22874 /* Advance the output cursor. */
22875 output_cursor.hpos += len;
22876 output_cursor.x += shift_by_width;
22877 UNBLOCK_INPUT;
22881 /* EXPORT for RIF:
22882 Erase the current text line from the nominal cursor position
22883 (inclusive) to pixel column TO_X (exclusive). The idea is that
22884 everything from TO_X onward is already erased.
22886 TO_X is a pixel position relative to updated_area of
22887 updated_window. TO_X == -1 means clear to the end of this area. */
22889 void
22890 x_clear_end_of_line (int to_x)
22892 struct frame *f;
22893 struct window *w = updated_window;
22894 int max_x, min_y, max_y;
22895 int from_x, from_y, to_y;
22897 xassert (updated_window && updated_row);
22898 f = XFRAME (w->frame);
22900 if (updated_row->full_width_p)
22901 max_x = WINDOW_TOTAL_WIDTH (w);
22902 else
22903 max_x = window_box_width (w, updated_area);
22904 max_y = window_text_bottom_y (w);
22906 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22907 of window. For TO_X > 0, truncate to end of drawing area. */
22908 if (to_x == 0)
22909 return;
22910 else if (to_x < 0)
22911 to_x = max_x;
22912 else
22913 to_x = min (to_x, max_x);
22915 to_y = min (max_y, output_cursor.y + updated_row->height);
22917 /* Notice if the cursor will be cleared by this operation. */
22918 if (!updated_row->full_width_p)
22919 notice_overwritten_cursor (w, updated_area,
22920 output_cursor.x, -1,
22921 updated_row->y,
22922 MATRIX_ROW_BOTTOM_Y (updated_row));
22924 from_x = output_cursor.x;
22926 /* Translate to frame coordinates. */
22927 if (updated_row->full_width_p)
22929 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22930 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22932 else
22934 int area_left = window_box_left (w, updated_area);
22935 from_x += area_left;
22936 to_x += area_left;
22939 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22940 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22941 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22943 /* Prevent inadvertently clearing to end of the X window. */
22944 if (to_x > from_x && to_y > from_y)
22946 BLOCK_INPUT;
22947 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22948 to_x - from_x, to_y - from_y);
22949 UNBLOCK_INPUT;
22953 #endif /* HAVE_WINDOW_SYSTEM */
22957 /***********************************************************************
22958 Cursor types
22959 ***********************************************************************/
22961 /* Value is the internal representation of the specified cursor type
22962 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22963 of the bar cursor. */
22965 static enum text_cursor_kinds
22966 get_specified_cursor_type (Lisp_Object arg, int *width)
22968 enum text_cursor_kinds type;
22970 if (NILP (arg))
22971 return NO_CURSOR;
22973 if (EQ (arg, Qbox))
22974 return FILLED_BOX_CURSOR;
22976 if (EQ (arg, Qhollow))
22977 return HOLLOW_BOX_CURSOR;
22979 if (EQ (arg, Qbar))
22981 *width = 2;
22982 return BAR_CURSOR;
22985 if (CONSP (arg)
22986 && EQ (XCAR (arg), Qbar)
22987 && INTEGERP (XCDR (arg))
22988 && XINT (XCDR (arg)) >= 0)
22990 *width = XINT (XCDR (arg));
22991 return BAR_CURSOR;
22994 if (EQ (arg, Qhbar))
22996 *width = 2;
22997 return HBAR_CURSOR;
23000 if (CONSP (arg)
23001 && EQ (XCAR (arg), Qhbar)
23002 && INTEGERP (XCDR (arg))
23003 && XINT (XCDR (arg)) >= 0)
23005 *width = XINT (XCDR (arg));
23006 return HBAR_CURSOR;
23009 /* Treat anything unknown as "hollow box cursor".
23010 It was bad to signal an error; people have trouble fixing
23011 .Xdefaults with Emacs, when it has something bad in it. */
23012 type = HOLLOW_BOX_CURSOR;
23014 return type;
23017 /* Set the default cursor types for specified frame. */
23018 void
23019 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23021 int width;
23022 Lisp_Object tem;
23024 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23025 FRAME_CURSOR_WIDTH (f) = width;
23027 /* By default, set up the blink-off state depending on the on-state. */
23029 tem = Fassoc (arg, Vblink_cursor_alist);
23030 if (!NILP (tem))
23032 FRAME_BLINK_OFF_CURSOR (f)
23033 = get_specified_cursor_type (XCDR (tem), &width);
23034 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23036 else
23037 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23041 /* Return the cursor we want to be displayed in window W. Return
23042 width of bar/hbar cursor through WIDTH arg. Return with
23043 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23044 (i.e. if the `system caret' should track this cursor).
23046 In a mini-buffer window, we want the cursor only to appear if we
23047 are reading input from this window. For the selected window, we
23048 want the cursor type given by the frame parameter or buffer local
23049 setting of cursor-type. If explicitly marked off, draw no cursor.
23050 In all other cases, we want a hollow box cursor. */
23052 static enum text_cursor_kinds
23053 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23054 int *active_cursor)
23056 struct frame *f = XFRAME (w->frame);
23057 struct buffer *b = XBUFFER (w->buffer);
23058 int cursor_type = DEFAULT_CURSOR;
23059 Lisp_Object alt_cursor;
23060 int non_selected = 0;
23062 *active_cursor = 1;
23064 /* Echo area */
23065 if (cursor_in_echo_area
23066 && FRAME_HAS_MINIBUF_P (f)
23067 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23069 if (w == XWINDOW (echo_area_window))
23071 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23073 *width = FRAME_CURSOR_WIDTH (f);
23074 return FRAME_DESIRED_CURSOR (f);
23076 else
23077 return get_specified_cursor_type (b->cursor_type, width);
23080 *active_cursor = 0;
23081 non_selected = 1;
23084 /* Detect a nonselected window or nonselected frame. */
23085 else if (w != XWINDOW (f->selected_window)
23086 #ifdef HAVE_WINDOW_SYSTEM
23087 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
23088 #endif
23091 *active_cursor = 0;
23093 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23094 return NO_CURSOR;
23096 non_selected = 1;
23099 /* Never display a cursor in a window in which cursor-type is nil. */
23100 if (NILP (b->cursor_type))
23101 return NO_CURSOR;
23103 /* Get the normal cursor type for this window. */
23104 if (EQ (b->cursor_type, Qt))
23106 cursor_type = FRAME_DESIRED_CURSOR (f);
23107 *width = FRAME_CURSOR_WIDTH (f);
23109 else
23110 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23112 /* Use cursor-in-non-selected-windows instead
23113 for non-selected window or frame. */
23114 if (non_selected)
23116 alt_cursor = b->cursor_in_non_selected_windows;
23117 if (!EQ (Qt, alt_cursor))
23118 return get_specified_cursor_type (alt_cursor, width);
23119 /* t means modify the normal cursor type. */
23120 if (cursor_type == FILLED_BOX_CURSOR)
23121 cursor_type = HOLLOW_BOX_CURSOR;
23122 else if (cursor_type == BAR_CURSOR && *width > 1)
23123 --*width;
23124 return cursor_type;
23127 /* Use normal cursor if not blinked off. */
23128 if (!w->cursor_off_p)
23130 #ifdef HAVE_WINDOW_SYSTEM
23131 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23133 if (cursor_type == FILLED_BOX_CURSOR)
23135 /* Using a block cursor on large images can be very annoying.
23136 So use a hollow cursor for "large" images.
23137 If image is not transparent (no mask), also use hollow cursor. */
23138 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23139 if (img != NULL && IMAGEP (img->spec))
23141 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23142 where N = size of default frame font size.
23143 This should cover most of the "tiny" icons people may use. */
23144 if (!img->mask
23145 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23146 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23147 cursor_type = HOLLOW_BOX_CURSOR;
23150 else if (cursor_type != NO_CURSOR)
23152 /* Display current only supports BOX and HOLLOW cursors for images.
23153 So for now, unconditionally use a HOLLOW cursor when cursor is
23154 not a solid box cursor. */
23155 cursor_type = HOLLOW_BOX_CURSOR;
23158 #endif
23159 return cursor_type;
23162 /* Cursor is blinked off, so determine how to "toggle" it. */
23164 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23165 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23166 return get_specified_cursor_type (XCDR (alt_cursor), width);
23168 /* Then see if frame has specified a specific blink off cursor type. */
23169 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23171 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23172 return FRAME_BLINK_OFF_CURSOR (f);
23175 #if 0
23176 /* Some people liked having a permanently visible blinking cursor,
23177 while others had very strong opinions against it. So it was
23178 decided to remove it. KFS 2003-09-03 */
23180 /* Finally perform built-in cursor blinking:
23181 filled box <-> hollow box
23182 wide [h]bar <-> narrow [h]bar
23183 narrow [h]bar <-> no cursor
23184 other type <-> no cursor */
23186 if (cursor_type == FILLED_BOX_CURSOR)
23187 return HOLLOW_BOX_CURSOR;
23189 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23191 *width = 1;
23192 return cursor_type;
23194 #endif
23196 return NO_CURSOR;
23200 #ifdef HAVE_WINDOW_SYSTEM
23202 /* Notice when the text cursor of window W has been completely
23203 overwritten by a drawing operation that outputs glyphs in AREA
23204 starting at X0 and ending at X1 in the line starting at Y0 and
23205 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23206 the rest of the line after X0 has been written. Y coordinates
23207 are window-relative. */
23209 static void
23210 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23211 int x0, int x1, int y0, int y1)
23213 int cx0, cx1, cy0, cy1;
23214 struct glyph_row *row;
23216 if (!w->phys_cursor_on_p)
23217 return;
23218 if (area != TEXT_AREA)
23219 return;
23221 if (w->phys_cursor.vpos < 0
23222 || w->phys_cursor.vpos >= w->current_matrix->nrows
23223 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23224 !(row->enabled_p && row->displays_text_p)))
23225 return;
23227 if (row->cursor_in_fringe_p)
23229 row->cursor_in_fringe_p = 0;
23230 draw_fringe_bitmap (w, row, row->reversed_p);
23231 w->phys_cursor_on_p = 0;
23232 return;
23235 cx0 = w->phys_cursor.x;
23236 cx1 = cx0 + w->phys_cursor_width;
23237 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23238 return;
23240 /* The cursor image will be completely removed from the
23241 screen if the output area intersects the cursor area in
23242 y-direction. When we draw in [y0 y1[, and some part of
23243 the cursor is at y < y0, that part must have been drawn
23244 before. When scrolling, the cursor is erased before
23245 actually scrolling, so we don't come here. When not
23246 scrolling, the rows above the old cursor row must have
23247 changed, and in this case these rows must have written
23248 over the cursor image.
23250 Likewise if part of the cursor is below y1, with the
23251 exception of the cursor being in the first blank row at
23252 the buffer and window end because update_text_area
23253 doesn't draw that row. (Except when it does, but
23254 that's handled in update_text_area.) */
23256 cy0 = w->phys_cursor.y;
23257 cy1 = cy0 + w->phys_cursor_height;
23258 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23259 return;
23261 w->phys_cursor_on_p = 0;
23264 #endif /* HAVE_WINDOW_SYSTEM */
23267 /************************************************************************
23268 Mouse Face
23269 ************************************************************************/
23271 #ifdef HAVE_WINDOW_SYSTEM
23273 /* EXPORT for RIF:
23274 Fix the display of area AREA of overlapping row ROW in window W
23275 with respect to the overlapping part OVERLAPS. */
23277 void
23278 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23279 enum glyph_row_area area, int overlaps)
23281 int i, x;
23283 BLOCK_INPUT;
23285 x = 0;
23286 for (i = 0; i < row->used[area];)
23288 if (row->glyphs[area][i].overlaps_vertically_p)
23290 int start = i, start_x = x;
23294 x += row->glyphs[area][i].pixel_width;
23295 ++i;
23297 while (i < row->used[area]
23298 && row->glyphs[area][i].overlaps_vertically_p);
23300 draw_glyphs (w, start_x, row, area,
23301 start, i,
23302 DRAW_NORMAL_TEXT, overlaps);
23304 else
23306 x += row->glyphs[area][i].pixel_width;
23307 ++i;
23311 UNBLOCK_INPUT;
23315 /* EXPORT:
23316 Draw the cursor glyph of window W in glyph row ROW. See the
23317 comment of draw_glyphs for the meaning of HL. */
23319 void
23320 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23321 enum draw_glyphs_face hl)
23323 /* If cursor hpos is out of bounds, don't draw garbage. This can
23324 happen in mini-buffer windows when switching between echo area
23325 glyphs and mini-buffer. */
23326 if ((row->reversed_p
23327 ? (w->phys_cursor.hpos >= 0)
23328 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23330 int on_p = w->phys_cursor_on_p;
23331 int x1;
23332 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23333 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23334 hl, 0);
23335 w->phys_cursor_on_p = on_p;
23337 if (hl == DRAW_CURSOR)
23338 w->phys_cursor_width = x1 - w->phys_cursor.x;
23339 /* When we erase the cursor, and ROW is overlapped by other
23340 rows, make sure that these overlapping parts of other rows
23341 are redrawn. */
23342 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23344 w->phys_cursor_width = x1 - w->phys_cursor.x;
23346 if (row > w->current_matrix->rows
23347 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23348 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23349 OVERLAPS_ERASED_CURSOR);
23351 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23352 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23353 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23354 OVERLAPS_ERASED_CURSOR);
23360 /* EXPORT:
23361 Erase the image of a cursor of window W from the screen. */
23363 void
23364 erase_phys_cursor (struct window *w)
23366 struct frame *f = XFRAME (w->frame);
23367 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23368 int hpos = w->phys_cursor.hpos;
23369 int vpos = w->phys_cursor.vpos;
23370 int mouse_face_here_p = 0;
23371 struct glyph_matrix *active_glyphs = w->current_matrix;
23372 struct glyph_row *cursor_row;
23373 struct glyph *cursor_glyph;
23374 enum draw_glyphs_face hl;
23376 /* No cursor displayed or row invalidated => nothing to do on the
23377 screen. */
23378 if (w->phys_cursor_type == NO_CURSOR)
23379 goto mark_cursor_off;
23381 /* VPOS >= active_glyphs->nrows means that window has been resized.
23382 Don't bother to erase the cursor. */
23383 if (vpos >= active_glyphs->nrows)
23384 goto mark_cursor_off;
23386 /* If row containing cursor is marked invalid, there is nothing we
23387 can do. */
23388 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23389 if (!cursor_row->enabled_p)
23390 goto mark_cursor_off;
23392 /* If line spacing is > 0, old cursor may only be partially visible in
23393 window after split-window. So adjust visible height. */
23394 cursor_row->visible_height = min (cursor_row->visible_height,
23395 window_text_bottom_y (w) - cursor_row->y);
23397 /* If row is completely invisible, don't attempt to delete a cursor which
23398 isn't there. This can happen if cursor is at top of a window, and
23399 we switch to a buffer with a header line in that window. */
23400 if (cursor_row->visible_height <= 0)
23401 goto mark_cursor_off;
23403 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23404 if (cursor_row->cursor_in_fringe_p)
23406 cursor_row->cursor_in_fringe_p = 0;
23407 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23408 goto mark_cursor_off;
23411 /* This can happen when the new row is shorter than the old one.
23412 In this case, either draw_glyphs or clear_end_of_line
23413 should have cleared the cursor. Note that we wouldn't be
23414 able to erase the cursor in this case because we don't have a
23415 cursor glyph at hand. */
23416 if ((cursor_row->reversed_p
23417 ? (w->phys_cursor.hpos < 0)
23418 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23419 goto mark_cursor_off;
23421 /* If the cursor is in the mouse face area, redisplay that when
23422 we clear the cursor. */
23423 if (! NILP (hlinfo->mouse_face_window)
23424 && coords_in_mouse_face_p (w, hpos, vpos)
23425 /* Don't redraw the cursor's spot in mouse face if it is at the
23426 end of a line (on a newline). The cursor appears there, but
23427 mouse highlighting does not. */
23428 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23429 mouse_face_here_p = 1;
23431 /* Maybe clear the display under the cursor. */
23432 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23434 int x, y, left_x;
23435 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23436 int width;
23438 cursor_glyph = get_phys_cursor_glyph (w);
23439 if (cursor_glyph == NULL)
23440 goto mark_cursor_off;
23442 width = cursor_glyph->pixel_width;
23443 left_x = window_box_left_offset (w, TEXT_AREA);
23444 x = w->phys_cursor.x;
23445 if (x < left_x)
23446 width -= left_x - x;
23447 width = min (width, window_box_width (w, TEXT_AREA) - x);
23448 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23449 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23451 if (width > 0)
23452 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23455 /* Erase the cursor by redrawing the character underneath it. */
23456 if (mouse_face_here_p)
23457 hl = DRAW_MOUSE_FACE;
23458 else
23459 hl = DRAW_NORMAL_TEXT;
23460 draw_phys_cursor_glyph (w, cursor_row, hl);
23462 mark_cursor_off:
23463 w->phys_cursor_on_p = 0;
23464 w->phys_cursor_type = NO_CURSOR;
23468 /* EXPORT:
23469 Display or clear cursor of window W. If ON is zero, clear the
23470 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23471 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23473 void
23474 display_and_set_cursor (struct window *w, int on,
23475 int hpos, int vpos, int x, int y)
23477 struct frame *f = XFRAME (w->frame);
23478 int new_cursor_type;
23479 int new_cursor_width;
23480 int active_cursor;
23481 struct glyph_row *glyph_row;
23482 struct glyph *glyph;
23484 /* This is pointless on invisible frames, and dangerous on garbaged
23485 windows and frames; in the latter case, the frame or window may
23486 be in the midst of changing its size, and x and y may be off the
23487 window. */
23488 if (! FRAME_VISIBLE_P (f)
23489 || FRAME_GARBAGED_P (f)
23490 || vpos >= w->current_matrix->nrows
23491 || hpos >= w->current_matrix->matrix_w)
23492 return;
23494 /* If cursor is off and we want it off, return quickly. */
23495 if (!on && !w->phys_cursor_on_p)
23496 return;
23498 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23499 /* If cursor row is not enabled, we don't really know where to
23500 display the cursor. */
23501 if (!glyph_row->enabled_p)
23503 w->phys_cursor_on_p = 0;
23504 return;
23507 glyph = NULL;
23508 if (!glyph_row->exact_window_width_line_p
23509 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23510 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23512 xassert (interrupt_input_blocked);
23514 /* Set new_cursor_type to the cursor we want to be displayed. */
23515 new_cursor_type = get_window_cursor_type (w, glyph,
23516 &new_cursor_width, &active_cursor);
23518 /* If cursor is currently being shown and we don't want it to be or
23519 it is in the wrong place, or the cursor type is not what we want,
23520 erase it. */
23521 if (w->phys_cursor_on_p
23522 && (!on
23523 || w->phys_cursor.x != x
23524 || w->phys_cursor.y != y
23525 || new_cursor_type != w->phys_cursor_type
23526 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23527 && new_cursor_width != w->phys_cursor_width)))
23528 erase_phys_cursor (w);
23530 /* Don't check phys_cursor_on_p here because that flag is only set
23531 to zero in some cases where we know that the cursor has been
23532 completely erased, to avoid the extra work of erasing the cursor
23533 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23534 still not be visible, or it has only been partly erased. */
23535 if (on)
23537 w->phys_cursor_ascent = glyph_row->ascent;
23538 w->phys_cursor_height = glyph_row->height;
23540 /* Set phys_cursor_.* before x_draw_.* is called because some
23541 of them may need the information. */
23542 w->phys_cursor.x = x;
23543 w->phys_cursor.y = glyph_row->y;
23544 w->phys_cursor.hpos = hpos;
23545 w->phys_cursor.vpos = vpos;
23548 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23549 new_cursor_type, new_cursor_width,
23550 on, active_cursor);
23554 /* Switch the display of W's cursor on or off, according to the value
23555 of ON. */
23557 void
23558 update_window_cursor (struct window *w, int on)
23560 /* Don't update cursor in windows whose frame is in the process
23561 of being deleted. */
23562 if (w->current_matrix)
23564 BLOCK_INPUT;
23565 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23566 w->phys_cursor.x, w->phys_cursor.y);
23567 UNBLOCK_INPUT;
23572 /* Call update_window_cursor with parameter ON_P on all leaf windows
23573 in the window tree rooted at W. */
23575 static void
23576 update_cursor_in_window_tree (struct window *w, int on_p)
23578 while (w)
23580 if (!NILP (w->hchild))
23581 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23582 else if (!NILP (w->vchild))
23583 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23584 else
23585 update_window_cursor (w, on_p);
23587 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23592 /* EXPORT:
23593 Display the cursor on window W, or clear it, according to ON_P.
23594 Don't change the cursor's position. */
23596 void
23597 x_update_cursor (struct frame *f, int on_p)
23599 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23603 /* EXPORT:
23604 Clear the cursor of window W to background color, and mark the
23605 cursor as not shown. This is used when the text where the cursor
23606 is about to be rewritten. */
23608 void
23609 x_clear_cursor (struct window *w)
23611 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23612 update_window_cursor (w, 0);
23615 #endif /* HAVE_WINDOW_SYSTEM */
23617 /* Implementation of draw_row_with_mouse_face for GUI sessions and
23618 GPM. MSDOS has its own implementation on msdos.c. */
23619 #ifndef MSDOS
23620 void
23621 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
23622 int start_hpos, int end_hpos,
23623 enum draw_glyphs_face draw)
23625 #ifdef HAVE_WINDOW_SYSTEM
23626 if (FRAME_WINDOW_P (XFRAME (w->frame)))
23628 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
23629 return;
23631 #endif
23632 #ifdef HAVE_GPM
23633 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
23634 #endif
23636 #endif /* not MSDOS */
23638 /* EXPORT:
23639 Display the active region described by mouse_face_* according to DRAW. */
23641 void
23642 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
23644 struct window *w = XWINDOW (hlinfo->mouse_face_window);
23645 struct frame *f = XFRAME (WINDOW_FRAME (w));
23647 if (/* If window is in the process of being destroyed, don't bother
23648 to do anything. */
23649 w->current_matrix != NULL
23650 /* Don't update mouse highlight if hidden */
23651 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
23652 /* Recognize when we are called to operate on rows that don't exist
23653 anymore. This can happen when a window is split. */
23654 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
23656 int phys_cursor_on_p = w->phys_cursor_on_p;
23657 struct glyph_row *row, *first, *last;
23659 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23660 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23662 for (row = first; row <= last && row->enabled_p; ++row)
23664 int start_hpos, end_hpos, start_x;
23666 /* For all but the first row, the highlight starts at column 0. */
23667 if (row == first)
23669 /* R2L rows have BEG and END in reversed order, but the
23670 screen drawing geometry is always left to right. So
23671 we need to mirror the beginning and end of the
23672 highlighted area in R2L rows. */
23673 if (!row->reversed_p)
23675 start_hpos = hlinfo->mouse_face_beg_col;
23676 start_x = hlinfo->mouse_face_beg_x;
23678 else if (row == last)
23680 start_hpos = hlinfo->mouse_face_end_col;
23681 start_x = hlinfo->mouse_face_end_x;
23683 else
23685 start_hpos = 0;
23686 start_x = 0;
23689 else if (row->reversed_p && row == last)
23691 start_hpos = hlinfo->mouse_face_end_col;
23692 start_x = hlinfo->mouse_face_end_x;
23694 else
23696 start_hpos = 0;
23697 start_x = 0;
23700 if (row == last)
23702 if (!row->reversed_p)
23703 end_hpos = hlinfo->mouse_face_end_col;
23704 else if (row == first)
23705 end_hpos = hlinfo->mouse_face_beg_col;
23706 else
23708 end_hpos = row->used[TEXT_AREA];
23709 if (draw == DRAW_NORMAL_TEXT)
23710 row->fill_line_p = 1; /* Clear to end of line */
23713 else if (row->reversed_p && row == first)
23714 end_hpos = hlinfo->mouse_face_beg_col;
23715 else
23717 end_hpos = row->used[TEXT_AREA];
23718 if (draw == DRAW_NORMAL_TEXT)
23719 row->fill_line_p = 1; /* Clear to end of line */
23722 if (end_hpos > start_hpos)
23724 draw_row_with_mouse_face (w, start_x, row,
23725 start_hpos, end_hpos, draw);
23727 row->mouse_face_p
23728 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
23732 #ifdef HAVE_WINDOW_SYSTEM
23733 /* When we've written over the cursor, arrange for it to
23734 be displayed again. */
23735 if (phys_cursor_on_p && !w->phys_cursor_on_p)
23737 BLOCK_INPUT;
23738 display_and_set_cursor (w, 1,
23739 w->phys_cursor.hpos, w->phys_cursor.vpos,
23740 w->phys_cursor.x, w->phys_cursor.y);
23741 UNBLOCK_INPUT;
23743 #endif /* HAVE_WINDOW_SYSTEM */
23746 #ifdef HAVE_WINDOW_SYSTEM
23747 /* Change the mouse cursor. */
23748 if (draw == DRAW_NORMAL_TEXT && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
23749 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23750 else if (draw == DRAW_MOUSE_FACE)
23751 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23752 else
23753 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23755 #endif /* HAVE_WINDOW_SYSTEM */
23758 /* EXPORT:
23759 Clear out the mouse-highlighted active region.
23760 Redraw it un-highlighted first. Value is non-zero if mouse
23761 face was actually drawn unhighlighted. */
23764 clear_mouse_face (Mouse_HLInfo *hlinfo)
23766 int cleared = 0;
23768 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
23770 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
23771 cleared = 1;
23774 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
23775 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
23776 hlinfo->mouse_face_window = Qnil;
23777 hlinfo->mouse_face_overlay = Qnil;
23778 return cleared;
23781 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
23782 within the mouse face on that window. */
23783 static int
23784 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
23786 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
23788 /* Quickly resolve the easy cases. */
23789 if (!(WINDOWP (hlinfo->mouse_face_window)
23790 && XWINDOW (hlinfo->mouse_face_window) == w))
23791 return 0;
23792 if (vpos < hlinfo->mouse_face_beg_row
23793 || vpos > hlinfo->mouse_face_end_row)
23794 return 0;
23795 if (vpos > hlinfo->mouse_face_beg_row
23796 && vpos < hlinfo->mouse_face_end_row)
23797 return 1;
23799 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
23801 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
23803 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
23804 return 1;
23806 else if ((vpos == hlinfo->mouse_face_beg_row
23807 && hpos >= hlinfo->mouse_face_beg_col)
23808 || (vpos == hlinfo->mouse_face_end_row
23809 && hpos < hlinfo->mouse_face_end_col))
23810 return 1;
23812 else
23814 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
23816 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
23817 return 1;
23819 else if ((vpos == hlinfo->mouse_face_beg_row
23820 && hpos <= hlinfo->mouse_face_beg_col)
23821 || (vpos == hlinfo->mouse_face_end_row
23822 && hpos > hlinfo->mouse_face_end_col))
23823 return 1;
23825 return 0;
23829 /* EXPORT:
23830 Non-zero if physical cursor of window W is within mouse face. */
23833 cursor_in_mouse_face_p (struct window *w)
23835 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
23840 /* Find the glyph rows START_ROW and END_ROW of window W that display
23841 characters between buffer positions START_CHARPOS and END_CHARPOS
23842 (excluding END_CHARPOS). This is similar to row_containing_pos,
23843 but is more accurate when bidi reordering makes buffer positions
23844 change non-linearly with glyph rows. */
23845 static void
23846 rows_from_pos_range (struct window *w,
23847 EMACS_INT start_charpos, EMACS_INT end_charpos,
23848 struct glyph_row **start, struct glyph_row **end)
23850 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23851 int last_y = window_text_bottom_y (w);
23852 struct glyph_row *row;
23854 *start = NULL;
23855 *end = NULL;
23857 while (!first->enabled_p
23858 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
23859 first++;
23861 /* Find the START row. */
23862 for (row = first;
23863 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
23864 row++)
23866 /* A row can potentially be the START row if the range of the
23867 characters it displays intersects the range
23868 [START_CHARPOS..END_CHARPOS). */
23869 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
23870 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
23871 /* See the commentary in row_containing_pos, for the
23872 explanation of the complicated way to check whether
23873 some position is beyond the end of the characters
23874 displayed by a row. */
23875 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
23876 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
23877 && !row->ends_at_zv_p
23878 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
23879 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
23880 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
23881 && !row->ends_at_zv_p
23882 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
23884 /* Found a candidate row. Now make sure at least one of the
23885 glyphs it displays has a charpos from the range
23886 [START_CHARPOS..END_CHARPOS).
23888 This is not obvious because bidi reordering could make
23889 buffer positions of a row be 1,2,3,102,101,100, and if we
23890 want to highlight characters in [50..60), we don't want
23891 this row, even though [50..60) does intersect [1..103),
23892 the range of character positions given by the row's start
23893 and end positions. */
23894 struct glyph *g = row->glyphs[TEXT_AREA];
23895 struct glyph *e = g + row->used[TEXT_AREA];
23897 while (g < e)
23899 if (BUFFERP (g->object)
23900 && start_charpos <= g->charpos && g->charpos < end_charpos)
23901 *start = row;
23902 g++;
23904 if (*start)
23905 break;
23909 /* Find the END row. */
23910 if (!*start
23911 /* If the last row is partially visible, start looking for END
23912 from that row, instead of starting from FIRST. */
23913 && !(row->enabled_p
23914 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
23915 row = first;
23916 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
23918 struct glyph_row *next = row + 1;
23920 if (!next->enabled_p
23921 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
23922 /* The first row >= START whose range of displayed characters
23923 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
23924 is the row END + 1. */
23925 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
23926 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
23927 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
23928 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
23929 && !next->ends_at_zv_p
23930 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
23931 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
23932 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
23933 && !next->ends_at_zv_p
23934 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
23936 *end = row;
23937 break;
23939 else
23941 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
23942 but none of the characters it displays are in the range, it is
23943 also END + 1. */
23944 struct glyph *g = next->glyphs[TEXT_AREA];
23945 struct glyph *e = g + next->used[TEXT_AREA];
23947 while (g < e)
23949 if (BUFFERP (g->object)
23950 && start_charpos <= g->charpos && g->charpos < end_charpos)
23951 break;
23952 g++;
23954 if (g == e)
23956 *end = row;
23957 break;
23963 /* This function sets the mouse_face_* elements of HLINFO, assuming
23964 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23965 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23966 for the overlay or run of text properties specifying the mouse
23967 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23968 before-string and after-string that must also be highlighted.
23969 DISPLAY_STRING, if non-nil, is a display string that may cover some
23970 or all of the highlighted text. */
23972 static void
23973 mouse_face_from_buffer_pos (Lisp_Object window,
23974 Mouse_HLInfo *hlinfo,
23975 EMACS_INT mouse_charpos,
23976 EMACS_INT start_charpos,
23977 EMACS_INT end_charpos,
23978 Lisp_Object before_string,
23979 Lisp_Object after_string,
23980 Lisp_Object display_string)
23982 struct window *w = XWINDOW (window);
23983 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23984 struct glyph_row *r1, *r2;
23985 struct glyph *glyph, *end;
23986 EMACS_INT ignore, pos;
23987 int x;
23989 xassert (NILP (display_string) || STRINGP (display_string));
23990 xassert (NILP (before_string) || STRINGP (before_string));
23991 xassert (NILP (after_string) || STRINGP (after_string));
23993 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
23994 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
23995 if (r1 == NULL)
23996 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23997 /* If the before-string or display-string contains newlines,
23998 rows_from_pos_range skips to its last row. Move back. */
23999 if (!NILP (before_string) || !NILP (display_string))
24001 struct glyph_row *prev;
24002 while ((prev = r1 - 1, prev >= first)
24003 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24004 && prev->used[TEXT_AREA] > 0)
24006 struct glyph *beg = prev->glyphs[TEXT_AREA];
24007 glyph = beg + prev->used[TEXT_AREA];
24008 while (--glyph >= beg && INTEGERP (glyph->object));
24009 if (glyph < beg
24010 || !(EQ (glyph->object, before_string)
24011 || EQ (glyph->object, display_string)))
24012 break;
24013 r1 = prev;
24016 if (r2 == NULL)
24018 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24019 hlinfo->mouse_face_past_end = 1;
24021 else if (!NILP (after_string))
24023 /* If the after-string has newlines, advance to its last row. */
24024 struct glyph_row *next;
24025 struct glyph_row *last
24026 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24028 for (next = r2 + 1;
24029 next <= last
24030 && next->used[TEXT_AREA] > 0
24031 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
24032 ++next)
24033 r2 = next;
24035 /* The rest of the display engine assumes that mouse_face_beg_row is
24036 either above below mouse_face_end_row or identical to it. But
24037 with bidi-reordered continued lines, the row for START_CHARPOS
24038 could be below the row for END_CHARPOS. If so, swap the rows and
24039 store them in correct order. */
24040 if (r1->y > r2->y)
24042 struct glyph_row *tem = r2;
24044 r2 = r1;
24045 r1 = tem;
24048 hlinfo->mouse_face_beg_y = r1->y;
24049 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
24050 hlinfo->mouse_face_end_y = r2->y;
24051 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
24053 /* For a bidi-reordered row, the positions of BEFORE_STRING,
24054 AFTER_STRING, DISPLAY_STRING, START_CHARPOS, and END_CHARPOS
24055 could be anywhere in the row and in any order. The strategy
24056 below is to find the leftmost and the rightmost glyph that
24057 belongs to either of these 3 strings, or whose position is
24058 between START_CHARPOS and END_CHARPOS, and highlight all the
24059 glyphs between those two. This may cover more than just the text
24060 between START_CHARPOS and END_CHARPOS if the range of characters
24061 strides the bidi level boundary, e.g. if the beginning is in R2L
24062 text while the end is in L2R text or vice versa. */
24063 if (!r1->reversed_p)
24065 /* This row is in a left to right paragraph. Scan it left to
24066 right. */
24067 glyph = r1->glyphs[TEXT_AREA];
24068 end = glyph + r1->used[TEXT_AREA];
24069 x = r1->x;
24071 /* Skip truncation glyphs at the start of the glyph row. */
24072 if (r1->displays_text_p)
24073 for (; glyph < end
24074 && INTEGERP (glyph->object)
24075 && glyph->charpos < 0;
24076 ++glyph)
24077 x += glyph->pixel_width;
24079 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24080 or DISPLAY_STRING, and the first glyph from buffer whose
24081 position is between START_CHARPOS and END_CHARPOS. */
24082 for (; glyph < end
24083 && !INTEGERP (glyph->object)
24084 && !EQ (glyph->object, display_string)
24085 && !(BUFFERP (glyph->object)
24086 && (glyph->charpos >= start_charpos
24087 && glyph->charpos < end_charpos));
24088 ++glyph)
24090 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24091 are present at buffer positions between START_CHARPOS and
24092 END_CHARPOS, or if they come from an overlay. */
24093 if (EQ (glyph->object, before_string))
24095 pos = string_buffer_position (w, before_string,
24096 start_charpos);
24097 /* If pos == 0, it means before_string came from an
24098 overlay, not from a buffer position. */
24099 if (!pos || (pos >= start_charpos && pos < end_charpos))
24100 break;
24102 else if (EQ (glyph->object, after_string))
24104 pos = string_buffer_position (w, after_string, end_charpos);
24105 if (!pos || (pos >= start_charpos && pos < end_charpos))
24106 break;
24108 x += glyph->pixel_width;
24110 hlinfo->mouse_face_beg_x = x;
24111 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24113 else
24115 /* This row is in a right to left paragraph. Scan it right to
24116 left. */
24117 struct glyph *g;
24119 end = r1->glyphs[TEXT_AREA] - 1;
24120 glyph = end + r1->used[TEXT_AREA];
24122 /* Skip truncation glyphs at the start of the glyph row. */
24123 if (r1->displays_text_p)
24124 for (; glyph > end
24125 && INTEGERP (glyph->object)
24126 && glyph->charpos < 0;
24127 --glyph)
24130 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
24131 or DISPLAY_STRING, and the first glyph from buffer whose
24132 position is between START_CHARPOS and END_CHARPOS. */
24133 for (; glyph > end
24134 && !INTEGERP (glyph->object)
24135 && !EQ (glyph->object, display_string)
24136 && !(BUFFERP (glyph->object)
24137 && (glyph->charpos >= start_charpos
24138 && glyph->charpos < end_charpos));
24139 --glyph)
24141 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24142 are present at buffer positions between START_CHARPOS and
24143 END_CHARPOS, or if they come from an overlay. */
24144 if (EQ (glyph->object, before_string))
24146 pos = string_buffer_position (w, before_string, start_charpos);
24147 /* If pos == 0, it means before_string came from an
24148 overlay, not from a buffer position. */
24149 if (!pos || (pos >= start_charpos && pos < end_charpos))
24150 break;
24152 else if (EQ (glyph->object, after_string))
24154 pos = string_buffer_position (w, after_string, end_charpos);
24155 if (!pos || (pos >= start_charpos && pos < end_charpos))
24156 break;
24160 glyph++; /* first glyph to the right of the highlighted area */
24161 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
24162 x += g->pixel_width;
24163 hlinfo->mouse_face_beg_x = x;
24164 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
24167 /* If the highlight ends in a different row, compute GLYPH and END
24168 for the end row. Otherwise, reuse the values computed above for
24169 the row where the highlight begins. */
24170 if (r2 != r1)
24172 if (!r2->reversed_p)
24174 glyph = r2->glyphs[TEXT_AREA];
24175 end = glyph + r2->used[TEXT_AREA];
24176 x = r2->x;
24178 else
24180 end = r2->glyphs[TEXT_AREA] - 1;
24181 glyph = end + r2->used[TEXT_AREA];
24185 if (!r2->reversed_p)
24187 /* Skip truncation and continuation glyphs near the end of the
24188 row, and also blanks and stretch glyphs inserted by
24189 extend_face_to_end_of_line. */
24190 while (end > glyph
24191 && INTEGERP ((end - 1)->object)
24192 && (end - 1)->charpos <= 0)
24193 --end;
24194 /* Scan the rest of the glyph row from the end, looking for the
24195 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24196 DISPLAY_STRING, or whose position is between START_CHARPOS
24197 and END_CHARPOS */
24198 for (--end;
24199 end > glyph
24200 && !INTEGERP (end->object)
24201 && !EQ (end->object, display_string)
24202 && !(BUFFERP (end->object)
24203 && (end->charpos >= start_charpos
24204 && end->charpos < end_charpos));
24205 --end)
24207 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24208 are present at buffer positions between START_CHARPOS and
24209 END_CHARPOS, or if they come from an overlay. */
24210 if (EQ (end->object, before_string))
24212 pos = string_buffer_position (w, before_string, start_charpos);
24213 if (!pos || (pos >= start_charpos && pos < end_charpos))
24214 break;
24216 else if (EQ (end->object, after_string))
24218 pos = string_buffer_position (w, after_string, end_charpos);
24219 if (!pos || (pos >= start_charpos && pos < end_charpos))
24220 break;
24223 /* Find the X coordinate of the last glyph to be highlighted. */
24224 for (; glyph <= end; ++glyph)
24225 x += glyph->pixel_width;
24227 hlinfo->mouse_face_end_x = x;
24228 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
24230 else
24232 /* Skip truncation and continuation glyphs near the end of the
24233 row, and also blanks and stretch glyphs inserted by
24234 extend_face_to_end_of_line. */
24235 x = r2->x;
24236 end++;
24237 while (end < glyph
24238 && INTEGERP (end->object)
24239 && end->charpos <= 0)
24241 x += end->pixel_width;
24242 ++end;
24244 /* Scan the rest of the glyph row from the end, looking for the
24245 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
24246 DISPLAY_STRING, or whose position is between START_CHARPOS
24247 and END_CHARPOS */
24248 for ( ;
24249 end < glyph
24250 && !INTEGERP (end->object)
24251 && !EQ (end->object, display_string)
24252 && !(BUFFERP (end->object)
24253 && (end->charpos >= start_charpos
24254 && end->charpos < end_charpos));
24255 ++end)
24257 /* BEFORE_STRING or AFTER_STRING are only relevant if they
24258 are present at buffer positions between START_CHARPOS and
24259 END_CHARPOS, or if they come from an overlay. */
24260 if (EQ (end->object, before_string))
24262 pos = string_buffer_position (w, before_string, start_charpos);
24263 if (!pos || (pos >= start_charpos && pos < end_charpos))
24264 break;
24266 else if (EQ (end->object, after_string))
24268 pos = string_buffer_position (w, after_string, end_charpos);
24269 if (!pos || (pos >= start_charpos && pos < end_charpos))
24270 break;
24272 x += end->pixel_width;
24274 hlinfo->mouse_face_end_x = x;
24275 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
24278 hlinfo->mouse_face_window = window;
24279 hlinfo->mouse_face_face_id
24280 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
24281 mouse_charpos + 1,
24282 !hlinfo->mouse_face_hidden, -1);
24283 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24286 /* The following function is not used anymore (replaced with
24287 mouse_face_from_string_pos), but I leave it here for the time
24288 being, in case someone would. */
24290 #if 0 /* not used */
24292 /* Find the position of the glyph for position POS in OBJECT in
24293 window W's current matrix, and return in *X, *Y the pixel
24294 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
24296 RIGHT_P non-zero means return the position of the right edge of the
24297 glyph, RIGHT_P zero means return the left edge position.
24299 If no glyph for POS exists in the matrix, return the position of
24300 the glyph with the next smaller position that is in the matrix, if
24301 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24302 exists in the matrix, return the position of the glyph with the
24303 next larger position in OBJECT.
24305 Value is non-zero if a glyph was found. */
24307 static int
24308 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24309 int *hpos, int *vpos, int *x, int *y, int right_p)
24311 int yb = window_text_bottom_y (w);
24312 struct glyph_row *r;
24313 struct glyph *best_glyph = NULL;
24314 struct glyph_row *best_row = NULL;
24315 int best_x = 0;
24317 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24318 r->enabled_p && r->y < yb;
24319 ++r)
24321 struct glyph *g = r->glyphs[TEXT_AREA];
24322 struct glyph *e = g + r->used[TEXT_AREA];
24323 int gx;
24325 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24326 if (EQ (g->object, object))
24328 if (g->charpos == pos)
24330 best_glyph = g;
24331 best_x = gx;
24332 best_row = r;
24333 goto found;
24335 else if (best_glyph == NULL
24336 || ((eabs (g->charpos - pos)
24337 < eabs (best_glyph->charpos - pos))
24338 && (right_p
24339 ? g->charpos < pos
24340 : g->charpos > pos)))
24342 best_glyph = g;
24343 best_x = gx;
24344 best_row = r;
24349 found:
24351 if (best_glyph)
24353 *x = best_x;
24354 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24356 if (right_p)
24358 *x += best_glyph->pixel_width;
24359 ++*hpos;
24362 *y = best_row->y;
24363 *vpos = best_row - w->current_matrix->rows;
24366 return best_glyph != NULL;
24368 #endif /* not used */
24370 /* Find the positions of the first and the last glyphs in window W's
24371 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
24372 (assumed to be a string), and return in HLINFO's mouse_face_*
24373 members the pixel and column/row coordinates of those glyphs. */
24375 static void
24376 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
24377 Lisp_Object object,
24378 EMACS_INT startpos, EMACS_INT endpos)
24380 int yb = window_text_bottom_y (w);
24381 struct glyph_row *r;
24382 struct glyph *g, *e;
24383 int gx;
24384 int found = 0;
24386 /* Find the glyph row with at least one position in the range
24387 [STARTPOS..ENDPOS], and the first glyph in that row whose
24388 position belongs to that range. */
24389 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24390 r->enabled_p && r->y < yb;
24391 ++r)
24393 if (!r->reversed_p)
24395 g = r->glyphs[TEXT_AREA];
24396 e = g + r->used[TEXT_AREA];
24397 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24398 if (EQ (g->object, object)
24399 && startpos <= g->charpos && g->charpos <= endpos)
24401 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24402 hlinfo->mouse_face_beg_y = r->y;
24403 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24404 hlinfo->mouse_face_beg_x = gx;
24405 found = 1;
24406 break;
24409 else
24411 struct glyph *g1;
24413 e = r->glyphs[TEXT_AREA];
24414 g = e + r->used[TEXT_AREA];
24415 for ( ; g > e; --g)
24416 if (EQ ((g-1)->object, object)
24417 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
24419 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
24420 hlinfo->mouse_face_beg_y = r->y;
24421 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
24422 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
24423 gx += g1->pixel_width;
24424 hlinfo->mouse_face_beg_x = gx;
24425 found = 1;
24426 break;
24429 if (found)
24430 break;
24433 if (!found)
24434 return;
24436 /* Starting with the next row, look for the first row which does NOT
24437 include any glyphs whose positions are in the range. */
24438 for (++r; r->enabled_p && r->y < yb; ++r)
24440 g = r->glyphs[TEXT_AREA];
24441 e = g + r->used[TEXT_AREA];
24442 found = 0;
24443 for ( ; g < e; ++g)
24444 if (EQ (g->object, object)
24445 && startpos <= g->charpos && g->charpos <= endpos)
24447 found = 1;
24448 break;
24450 if (!found)
24451 break;
24454 /* The highlighted region ends on the previous row. */
24455 r--;
24457 /* Set the end row and its vertical pixel coordinate. */
24458 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
24459 hlinfo->mouse_face_end_y = r->y;
24461 /* Compute and set the end column and the end column's horizontal
24462 pixel coordinate. */
24463 if (!r->reversed_p)
24465 g = r->glyphs[TEXT_AREA];
24466 e = g + r->used[TEXT_AREA];
24467 for ( ; e > g; --e)
24468 if (EQ ((e-1)->object, object)
24469 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
24470 break;
24471 hlinfo->mouse_face_end_col = e - g;
24473 for (gx = r->x; g < e; ++g)
24474 gx += g->pixel_width;
24475 hlinfo->mouse_face_end_x = gx;
24477 else
24479 e = r->glyphs[TEXT_AREA];
24480 g = e + r->used[TEXT_AREA];
24481 for (gx = r->x ; e < g; ++e)
24483 if (EQ (e->object, object)
24484 && startpos <= e->charpos && e->charpos <= endpos)
24485 break;
24486 gx += e->pixel_width;
24488 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
24489 hlinfo->mouse_face_end_x = gx;
24493 #ifdef HAVE_WINDOW_SYSTEM
24495 /* See if position X, Y is within a hot-spot of an image. */
24497 static int
24498 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24500 if (!CONSP (hot_spot))
24501 return 0;
24503 if (EQ (XCAR (hot_spot), Qrect))
24505 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24506 Lisp_Object rect = XCDR (hot_spot);
24507 Lisp_Object tem;
24508 if (!CONSP (rect))
24509 return 0;
24510 if (!CONSP (XCAR (rect)))
24511 return 0;
24512 if (!CONSP (XCDR (rect)))
24513 return 0;
24514 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24515 return 0;
24516 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24517 return 0;
24518 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24519 return 0;
24520 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24521 return 0;
24522 return 1;
24524 else if (EQ (XCAR (hot_spot), Qcircle))
24526 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24527 Lisp_Object circ = XCDR (hot_spot);
24528 Lisp_Object lr, lx0, ly0;
24529 if (CONSP (circ)
24530 && CONSP (XCAR (circ))
24531 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24532 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24533 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24535 double r = XFLOATINT (lr);
24536 double dx = XINT (lx0) - x;
24537 double dy = XINT (ly0) - y;
24538 return (dx * dx + dy * dy <= r * r);
24541 else if (EQ (XCAR (hot_spot), Qpoly))
24543 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24544 if (VECTORP (XCDR (hot_spot)))
24546 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24547 Lisp_Object *poly = v->contents;
24548 int n = v->size;
24549 int i;
24550 int inside = 0;
24551 Lisp_Object lx, ly;
24552 int x0, y0;
24554 /* Need an even number of coordinates, and at least 3 edges. */
24555 if (n < 6 || n & 1)
24556 return 0;
24558 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24559 If count is odd, we are inside polygon. Pixels on edges
24560 may or may not be included depending on actual geometry of the
24561 polygon. */
24562 if ((lx = poly[n-2], !INTEGERP (lx))
24563 || (ly = poly[n-1], !INTEGERP (lx)))
24564 return 0;
24565 x0 = XINT (lx), y0 = XINT (ly);
24566 for (i = 0; i < n; i += 2)
24568 int x1 = x0, y1 = y0;
24569 if ((lx = poly[i], !INTEGERP (lx))
24570 || (ly = poly[i+1], !INTEGERP (ly)))
24571 return 0;
24572 x0 = XINT (lx), y0 = XINT (ly);
24574 /* Does this segment cross the X line? */
24575 if (x0 >= x)
24577 if (x1 >= x)
24578 continue;
24580 else if (x1 < x)
24581 continue;
24582 if (y > y0 && y > y1)
24583 continue;
24584 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24585 inside = !inside;
24587 return inside;
24590 return 0;
24593 Lisp_Object
24594 find_hot_spot (Lisp_Object map, int x, int y)
24596 while (CONSP (map))
24598 if (CONSP (XCAR (map))
24599 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24600 return XCAR (map);
24601 map = XCDR (map);
24604 return Qnil;
24607 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24608 3, 3, 0,
24609 doc: /* Lookup in image map MAP coordinates X and Y.
24610 An image map is an alist where each element has the format (AREA ID PLIST).
24611 An AREA is specified as either a rectangle, a circle, or a polygon:
24612 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24613 pixel coordinates of the upper left and bottom right corners.
24614 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24615 and the radius of the circle; r may be a float or integer.
24616 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24617 vector describes one corner in the polygon.
24618 Returns the alist element for the first matching AREA in MAP. */)
24619 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
24621 if (NILP (map))
24622 return Qnil;
24624 CHECK_NUMBER (x);
24625 CHECK_NUMBER (y);
24627 return find_hot_spot (map, XINT (x), XINT (y));
24631 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24632 static void
24633 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
24635 /* Do not change cursor shape while dragging mouse. */
24636 if (!NILP (do_mouse_tracking))
24637 return;
24639 if (!NILP (pointer))
24641 if (EQ (pointer, Qarrow))
24642 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24643 else if (EQ (pointer, Qhand))
24644 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24645 else if (EQ (pointer, Qtext))
24646 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24647 else if (EQ (pointer, intern ("hdrag")))
24648 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24649 #ifdef HAVE_X_WINDOWS
24650 else if (EQ (pointer, intern ("vdrag")))
24651 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24652 #endif
24653 else if (EQ (pointer, intern ("hourglass")))
24654 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24655 else if (EQ (pointer, Qmodeline))
24656 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24657 else
24658 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24661 if (cursor != No_Cursor)
24662 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24665 #endif /* HAVE_WINDOW_SYSTEM */
24667 /* Take proper action when mouse has moved to the mode or header line
24668 or marginal area AREA of window W, x-position X and y-position Y.
24669 X is relative to the start of the text display area of W, so the
24670 width of bitmap areas and scroll bars must be subtracted to get a
24671 position relative to the start of the mode line. */
24673 static void
24674 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
24675 enum window_part area)
24677 struct window *w = XWINDOW (window);
24678 struct frame *f = XFRAME (w->frame);
24679 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24680 #ifdef HAVE_WINDOW_SYSTEM
24681 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24682 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24683 #else
24684 Cursor cursor = No_Cursor;
24685 #endif
24686 Lisp_Object pointer = Qnil;
24687 int dx, dy, width, height;
24688 EMACS_INT charpos;
24689 Lisp_Object string, object = Qnil;
24690 Lisp_Object pos, help;
24692 Lisp_Object mouse_face;
24693 int original_x_pixel = x;
24694 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24695 struct glyph_row *row;
24697 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24699 int x0;
24700 struct glyph *end;
24702 /* Kludge alert: mode_line_string takes X/Y in pixels, but
24703 returns them in row/column units! */
24704 string = mode_line_string (w, area, &x, &y, &charpos,
24705 &object, &dx, &dy, &width, &height);
24707 row = (area == ON_MODE_LINE
24708 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24709 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24711 /* Find the glyph under the mouse pointer. */
24712 if (row->mode_line_p && row->enabled_p)
24714 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24715 end = glyph + row->used[TEXT_AREA];
24717 for (x0 = original_x_pixel;
24718 glyph < end && x0 >= glyph->pixel_width;
24719 ++glyph)
24720 x0 -= glyph->pixel_width;
24722 if (glyph >= end)
24723 glyph = NULL;
24726 else
24728 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24729 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
24730 returns them in row/column units! */
24731 string = marginal_area_string (w, area, &x, &y, &charpos,
24732 &object, &dx, &dy, &width, &height);
24735 help = Qnil;
24737 #ifdef HAVE_WINDOW_SYSTEM
24738 if (IMAGEP (object))
24740 Lisp_Object image_map, hotspot;
24741 if ((image_map = Fplist_get (XCDR (object), QCmap),
24742 !NILP (image_map))
24743 && (hotspot = find_hot_spot (image_map, dx, dy),
24744 CONSP (hotspot))
24745 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24747 Lisp_Object area_id, plist;
24749 area_id = XCAR (hotspot);
24750 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24751 If so, we could look for mouse-enter, mouse-leave
24752 properties in PLIST (and do something...). */
24753 hotspot = XCDR (hotspot);
24754 if (CONSP (hotspot)
24755 && (plist = XCAR (hotspot), CONSP (plist)))
24757 pointer = Fplist_get (plist, Qpointer);
24758 if (NILP (pointer))
24759 pointer = Qhand;
24760 help = Fplist_get (plist, Qhelp_echo);
24761 if (!NILP (help))
24763 help_echo_string = help;
24764 /* Is this correct? ++kfs */
24765 XSETWINDOW (help_echo_window, w);
24766 help_echo_object = w->buffer;
24767 help_echo_pos = charpos;
24771 if (NILP (pointer))
24772 pointer = Fplist_get (XCDR (object), QCpointer);
24774 #endif /* HAVE_WINDOW_SYSTEM */
24776 if (STRINGP (string))
24778 pos = make_number (charpos);
24779 /* If we're on a string with `help-echo' text property, arrange
24780 for the help to be displayed. This is done by setting the
24781 global variable help_echo_string to the help string. */
24782 if (NILP (help))
24784 help = Fget_text_property (pos, Qhelp_echo, string);
24785 if (!NILP (help))
24787 help_echo_string = help;
24788 XSETWINDOW (help_echo_window, w);
24789 help_echo_object = string;
24790 help_echo_pos = charpos;
24794 #ifdef HAVE_WINDOW_SYSTEM
24795 if (NILP (pointer))
24796 pointer = Fget_text_property (pos, Qpointer, string);
24798 /* Change the mouse pointer according to what is under X/Y. */
24799 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24801 Lisp_Object map;
24802 map = Fget_text_property (pos, Qlocal_map, string);
24803 if (!KEYMAPP (map))
24804 map = Fget_text_property (pos, Qkeymap, string);
24805 if (!KEYMAPP (map))
24806 cursor = dpyinfo->vertical_scroll_bar_cursor;
24808 #endif
24810 /* Change the mouse face according to what is under X/Y. */
24811 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24812 if (!NILP (mouse_face)
24813 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24814 && glyph)
24816 Lisp_Object b, e;
24818 struct glyph * tmp_glyph;
24820 int gpos;
24821 int gseq_length;
24822 int total_pixel_width;
24823 EMACS_INT begpos, endpos, ignore;
24825 int vpos, hpos;
24827 b = Fprevious_single_property_change (make_number (charpos + 1),
24828 Qmouse_face, string, Qnil);
24829 if (NILP (b))
24830 begpos = 0;
24831 else
24832 begpos = XINT (b);
24834 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24835 if (NILP (e))
24836 endpos = SCHARS (string);
24837 else
24838 endpos = XINT (e);
24840 /* Calculate the glyph position GPOS of GLYPH in the
24841 displayed string, relative to the beginning of the
24842 highlighted part of the string.
24844 Note: GPOS is different from CHARPOS. CHARPOS is the
24845 position of GLYPH in the internal string object. A mode
24846 line string format has structures which are converted to
24847 a flattened string by the Emacs Lisp interpreter. The
24848 internal string is an element of those structures. The
24849 displayed string is the flattened string. */
24850 tmp_glyph = row_start_glyph;
24851 while (tmp_glyph < glyph
24852 && (!(EQ (tmp_glyph->object, glyph->object)
24853 && begpos <= tmp_glyph->charpos
24854 && tmp_glyph->charpos < endpos)))
24855 tmp_glyph++;
24856 gpos = glyph - tmp_glyph;
24858 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
24859 the highlighted part of the displayed string to which
24860 GLYPH belongs. Note: GSEQ_LENGTH is different from
24861 SCHARS (STRING), because the latter returns the length of
24862 the internal string. */
24863 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
24864 tmp_glyph > glyph
24865 && (!(EQ (tmp_glyph->object, glyph->object)
24866 && begpos <= tmp_glyph->charpos
24867 && tmp_glyph->charpos < endpos));
24868 tmp_glyph--)
24870 gseq_length = gpos + (tmp_glyph - glyph) + 1;
24872 /* Calculate the total pixel width of all the glyphs between
24873 the beginning of the highlighted area and GLYPH. */
24874 total_pixel_width = 0;
24875 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24876 total_pixel_width += tmp_glyph->pixel_width;
24878 /* Pre calculation of re-rendering position. Note: X is in
24879 column units here, after the call to mode_line_string or
24880 marginal_area_string. */
24881 hpos = x - gpos;
24882 vpos = (area == ON_MODE_LINE
24883 ? (w->current_matrix)->nrows - 1
24884 : 0);
24886 /* If GLYPH's position is included in the region that is
24887 already drawn in mouse face, we have nothing to do. */
24888 if ( EQ (window, hlinfo->mouse_face_window)
24889 && (!row->reversed_p
24890 ? (hlinfo->mouse_face_beg_col <= hpos
24891 && hpos < hlinfo->mouse_face_end_col)
24892 /* In R2L rows we swap BEG and END, see below. */
24893 : (hlinfo->mouse_face_end_col <= hpos
24894 && hpos < hlinfo->mouse_face_beg_col))
24895 && hlinfo->mouse_face_beg_row == vpos )
24896 return;
24898 if (clear_mouse_face (hlinfo))
24899 cursor = No_Cursor;
24901 if (!row->reversed_p)
24903 hlinfo->mouse_face_beg_col = hpos;
24904 hlinfo->mouse_face_beg_x = original_x_pixel
24905 - (total_pixel_width + dx);
24906 hlinfo->mouse_face_end_col = hpos + gseq_length;
24907 hlinfo->mouse_face_end_x = 0;
24909 else
24911 /* In R2L rows, show_mouse_face expects BEG and END
24912 coordinates to be swapped. */
24913 hlinfo->mouse_face_end_col = hpos;
24914 hlinfo->mouse_face_end_x = original_x_pixel
24915 - (total_pixel_width + dx);
24916 hlinfo->mouse_face_beg_col = hpos + gseq_length;
24917 hlinfo->mouse_face_beg_x = 0;
24920 hlinfo->mouse_face_beg_row = vpos;
24921 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
24922 hlinfo->mouse_face_beg_y = 0;
24923 hlinfo->mouse_face_end_y = 0;
24924 hlinfo->mouse_face_past_end = 0;
24925 hlinfo->mouse_face_window = window;
24927 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
24928 charpos,
24929 0, 0, 0,
24930 &ignore,
24931 glyph->face_id,
24933 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
24935 if (NILP (pointer))
24936 pointer = Qhand;
24938 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24939 clear_mouse_face (hlinfo);
24941 #ifdef HAVE_WINDOW_SYSTEM
24942 define_frame_cursor1 (f, cursor, pointer);
24943 #endif
24947 /* EXPORT:
24948 Take proper action when the mouse has moved to position X, Y on
24949 frame F as regards highlighting characters that have mouse-face
24950 properties. Also de-highlighting chars where the mouse was before.
24951 X and Y can be negative or out of range. */
24953 void
24954 note_mouse_highlight (struct frame *f, int x, int y)
24956 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24957 enum window_part part;
24958 Lisp_Object window;
24959 struct window *w;
24960 Cursor cursor = No_Cursor;
24961 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24962 struct buffer *b;
24964 /* When a menu is active, don't highlight because this looks odd. */
24965 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
24966 if (popup_activated ())
24967 return;
24968 #endif
24970 if (NILP (Vmouse_highlight)
24971 || !f->glyphs_initialized_p
24972 || f->pointer_invisible)
24973 return;
24975 hlinfo->mouse_face_mouse_x = x;
24976 hlinfo->mouse_face_mouse_y = y;
24977 hlinfo->mouse_face_mouse_frame = f;
24979 if (hlinfo->mouse_face_defer)
24980 return;
24982 if (gc_in_progress)
24984 hlinfo->mouse_face_deferred_gc = 1;
24985 return;
24988 /* Which window is that in? */
24989 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24991 /* If we were displaying active text in another window, clear that.
24992 Also clear if we move out of text area in same window. */
24993 if (! EQ (window, hlinfo->mouse_face_window)
24994 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24995 && !NILP (hlinfo->mouse_face_window)))
24996 clear_mouse_face (hlinfo);
24998 /* Not on a window -> return. */
24999 if (!WINDOWP (window))
25000 return;
25002 /* Reset help_echo_string. It will get recomputed below. */
25003 help_echo_string = Qnil;
25005 /* Convert to window-relative pixel coordinates. */
25006 w = XWINDOW (window);
25007 frame_to_window_pixel_xy (w, &x, &y);
25009 #ifdef HAVE_WINDOW_SYSTEM
25010 /* Handle tool-bar window differently since it doesn't display a
25011 buffer. */
25012 if (EQ (window, f->tool_bar_window))
25014 note_tool_bar_highlight (f, x, y);
25015 return;
25017 #endif
25019 /* Mouse is on the mode, header line or margin? */
25020 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
25021 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
25023 note_mode_line_or_margin_highlight (window, x, y, part);
25024 return;
25027 #ifdef HAVE_WINDOW_SYSTEM
25028 if (part == ON_VERTICAL_BORDER)
25030 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25031 help_echo_string = build_string ("drag-mouse-1: resize");
25033 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
25034 || part == ON_SCROLL_BAR)
25035 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25036 else
25037 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25038 #endif
25040 /* Are we in a window whose display is up to date?
25041 And verify the buffer's text has not changed. */
25042 b = XBUFFER (w->buffer);
25043 if (part == ON_TEXT
25044 && EQ (w->window_end_valid, w->buffer)
25045 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
25046 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
25048 int hpos, vpos, i, dx, dy, area;
25049 EMACS_INT pos;
25050 struct glyph *glyph;
25051 Lisp_Object object;
25052 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
25053 Lisp_Object *overlay_vec = NULL;
25054 int noverlays;
25055 struct buffer *obuf;
25056 EMACS_INT obegv, ozv;
25057 int same_region;
25059 /* Find the glyph under X/Y. */
25060 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
25062 #ifdef HAVE_WINDOW_SYSTEM
25063 /* Look for :pointer property on image. */
25064 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25066 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25067 if (img != NULL && IMAGEP (img->spec))
25069 Lisp_Object image_map, hotspot;
25070 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
25071 !NILP (image_map))
25072 && (hotspot = find_hot_spot (image_map,
25073 glyph->slice.img.x + dx,
25074 glyph->slice.img.y + dy),
25075 CONSP (hotspot))
25076 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25078 Lisp_Object area_id, plist;
25080 area_id = XCAR (hotspot);
25081 /* Could check AREA_ID to see if we enter/leave this hot-spot.
25082 If so, we could look for mouse-enter, mouse-leave
25083 properties in PLIST (and do something...). */
25084 hotspot = XCDR (hotspot);
25085 if (CONSP (hotspot)
25086 && (plist = XCAR (hotspot), CONSP (plist)))
25088 pointer = Fplist_get (plist, Qpointer);
25089 if (NILP (pointer))
25090 pointer = Qhand;
25091 help_echo_string = Fplist_get (plist, Qhelp_echo);
25092 if (!NILP (help_echo_string))
25094 help_echo_window = window;
25095 help_echo_object = glyph->object;
25096 help_echo_pos = glyph->charpos;
25100 if (NILP (pointer))
25101 pointer = Fplist_get (XCDR (img->spec), QCpointer);
25104 #endif /* HAVE_WINDOW_SYSTEM */
25106 /* Clear mouse face if X/Y not over text. */
25107 if (glyph == NULL
25108 || area != TEXT_AREA
25109 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
25110 /* Glyph's OBJECT is an integer for glyphs inserted by the
25111 display engine for its internal purposes, like truncation
25112 and continuation glyphs and blanks beyond the end of
25113 line's text on text terminals. If we are over such a
25114 glyph, we are not over any text. */
25115 || INTEGERP (glyph->object)
25116 /* R2L rows have a stretch glyph at their front, which
25117 stands for no text, whereas L2R rows have no glyphs at
25118 all beyond the end of text. Treat such stretch glyphs
25119 like we do with NULL glyphs in L2R rows. */
25120 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
25121 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
25122 && glyph->type == STRETCH_GLYPH
25123 && glyph->avoid_cursor_p))
25125 if (clear_mouse_face (hlinfo))
25126 cursor = No_Cursor;
25127 #ifdef HAVE_WINDOW_SYSTEM
25128 if (NILP (pointer))
25130 if (area != TEXT_AREA)
25131 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25132 else
25133 pointer = Vvoid_text_area_pointer;
25135 #endif
25136 goto set_cursor;
25139 pos = glyph->charpos;
25140 object = glyph->object;
25141 if (!STRINGP (object) && !BUFFERP (object))
25142 goto set_cursor;
25144 /* If we get an out-of-range value, return now; avoid an error. */
25145 if (BUFFERP (object) && pos > BUF_Z (b))
25146 goto set_cursor;
25148 /* Make the window's buffer temporarily current for
25149 overlays_at and compute_char_face. */
25150 obuf = current_buffer;
25151 current_buffer = b;
25152 obegv = BEGV;
25153 ozv = ZV;
25154 BEGV = BEG;
25155 ZV = Z;
25157 /* Is this char mouse-active or does it have help-echo? */
25158 position = make_number (pos);
25160 if (BUFFERP (object))
25162 /* Put all the overlays we want in a vector in overlay_vec. */
25163 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
25164 /* Sort overlays into increasing priority order. */
25165 noverlays = sort_overlays (overlay_vec, noverlays, w);
25167 else
25168 noverlays = 0;
25170 same_region = coords_in_mouse_face_p (w, hpos, vpos);
25172 if (same_region)
25173 cursor = No_Cursor;
25175 /* Check mouse-face highlighting. */
25176 if (! same_region
25177 /* If there exists an overlay with mouse-face overlapping
25178 the one we are currently highlighting, we have to
25179 check if we enter the overlapping overlay, and then
25180 highlight only that. */
25181 || (OVERLAYP (hlinfo->mouse_face_overlay)
25182 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
25184 /* Find the highest priority overlay with a mouse-face. */
25185 overlay = Qnil;
25186 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
25188 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
25189 if (!NILP (mouse_face))
25190 overlay = overlay_vec[i];
25193 /* If we're highlighting the same overlay as before, there's
25194 no need to do that again. */
25195 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
25196 goto check_help_echo;
25197 hlinfo->mouse_face_overlay = overlay;
25199 /* Clear the display of the old active region, if any. */
25200 if (clear_mouse_face (hlinfo))
25201 cursor = No_Cursor;
25203 /* If no overlay applies, get a text property. */
25204 if (NILP (overlay))
25205 mouse_face = Fget_text_property (position, Qmouse_face, object);
25207 /* Next, compute the bounds of the mouse highlighting and
25208 display it. */
25209 if (!NILP (mouse_face) && STRINGP (object))
25211 /* The mouse-highlighting comes from a display string
25212 with a mouse-face. */
25213 Lisp_Object b, e;
25214 EMACS_INT ignore;
25216 b = Fprevious_single_property_change
25217 (make_number (pos + 1), Qmouse_face, object, Qnil);
25218 e = Fnext_single_property_change
25219 (position, Qmouse_face, object, Qnil);
25220 if (NILP (b))
25221 b = make_number (0);
25222 if (NILP (e))
25223 e = make_number (SCHARS (object) - 1);
25224 mouse_face_from_string_pos (w, hlinfo, object,
25225 XINT (b), XINT (e));
25226 hlinfo->mouse_face_past_end = 0;
25227 hlinfo->mouse_face_window = window;
25228 hlinfo->mouse_face_face_id
25229 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
25230 glyph->face_id, 1);
25231 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25232 cursor = No_Cursor;
25234 else
25236 /* The mouse-highlighting, if any, comes from an overlay
25237 or text property in the buffer. */
25238 Lisp_Object buffer, display_string;
25240 if (STRINGP (object))
25242 /* If we are on a display string with no mouse-face,
25243 check if the text under it has one. */
25244 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
25245 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25246 pos = string_buffer_position (w, object, start);
25247 if (pos > 0)
25249 mouse_face = get_char_property_and_overlay
25250 (make_number (pos), Qmouse_face, w->buffer, &overlay);
25251 buffer = w->buffer;
25252 display_string = object;
25255 else
25257 buffer = object;
25258 display_string = Qnil;
25261 if (!NILP (mouse_face))
25263 Lisp_Object before, after;
25264 Lisp_Object before_string, after_string;
25265 /* To correctly find the limits of mouse highlight
25266 in a bidi-reordered buffer, we must not use the
25267 optimization of limiting the search in
25268 previous-single-property-change and
25269 next-single-property-change, because
25270 rows_from_pos_range needs the real start and end
25271 positions to DTRT in this case. That's because
25272 the first row visible in a window does not
25273 necessarily display the character whose position
25274 is the smallest. */
25275 Lisp_Object lim1 =
25276 NILP (XBUFFER (buffer)->bidi_display_reordering)
25277 ? Fmarker_position (w->start)
25278 : Qnil;
25279 Lisp_Object lim2 =
25280 NILP (XBUFFER (buffer)->bidi_display_reordering)
25281 ? make_number (BUF_Z (XBUFFER (buffer))
25282 - XFASTINT (w->window_end_pos))
25283 : Qnil;
25285 if (NILP (overlay))
25287 /* Handle the text property case. */
25288 before = Fprevious_single_property_change
25289 (make_number (pos + 1), Qmouse_face, buffer, lim1);
25290 after = Fnext_single_property_change
25291 (make_number (pos), Qmouse_face, buffer, lim2);
25292 before_string = after_string = Qnil;
25294 else
25296 /* Handle the overlay case. */
25297 before = Foverlay_start (overlay);
25298 after = Foverlay_end (overlay);
25299 before_string = Foverlay_get (overlay, Qbefore_string);
25300 after_string = Foverlay_get (overlay, Qafter_string);
25302 if (!STRINGP (before_string)) before_string = Qnil;
25303 if (!STRINGP (after_string)) after_string = Qnil;
25306 mouse_face_from_buffer_pos (window, hlinfo, pos,
25307 XFASTINT (before),
25308 XFASTINT (after),
25309 before_string, after_string,
25310 display_string);
25311 cursor = No_Cursor;
25316 check_help_echo:
25318 /* Look for a `help-echo' property. */
25319 if (NILP (help_echo_string)) {
25320 Lisp_Object help, overlay;
25322 /* Check overlays first. */
25323 help = overlay = Qnil;
25324 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
25326 overlay = overlay_vec[i];
25327 help = Foverlay_get (overlay, Qhelp_echo);
25330 if (!NILP (help))
25332 help_echo_string = help;
25333 help_echo_window = window;
25334 help_echo_object = overlay;
25335 help_echo_pos = pos;
25337 else
25339 Lisp_Object object = glyph->object;
25340 EMACS_INT charpos = glyph->charpos;
25342 /* Try text properties. */
25343 if (STRINGP (object)
25344 && charpos >= 0
25345 && charpos < SCHARS (object))
25347 help = Fget_text_property (make_number (charpos),
25348 Qhelp_echo, object);
25349 if (NILP (help))
25351 /* If the string itself doesn't specify a help-echo,
25352 see if the buffer text ``under'' it does. */
25353 struct glyph_row *r
25354 = MATRIX_ROW (w->current_matrix, vpos);
25355 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25356 EMACS_INT pos = string_buffer_position (w, object, start);
25357 if (pos > 0)
25359 help = Fget_char_property (make_number (pos),
25360 Qhelp_echo, w->buffer);
25361 if (!NILP (help))
25363 charpos = pos;
25364 object = w->buffer;
25369 else if (BUFFERP (object)
25370 && charpos >= BEGV
25371 && charpos < ZV)
25372 help = Fget_text_property (make_number (charpos), Qhelp_echo,
25373 object);
25375 if (!NILP (help))
25377 help_echo_string = help;
25378 help_echo_window = window;
25379 help_echo_object = object;
25380 help_echo_pos = charpos;
25385 #ifdef HAVE_WINDOW_SYSTEM
25386 /* Look for a `pointer' property. */
25387 if (NILP (pointer))
25389 /* Check overlays first. */
25390 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
25391 pointer = Foverlay_get (overlay_vec[i], Qpointer);
25393 if (NILP (pointer))
25395 Lisp_Object object = glyph->object;
25396 EMACS_INT charpos = glyph->charpos;
25398 /* Try text properties. */
25399 if (STRINGP (object)
25400 && charpos >= 0
25401 && charpos < SCHARS (object))
25403 pointer = Fget_text_property (make_number (charpos),
25404 Qpointer, object);
25405 if (NILP (pointer))
25407 /* If the string itself doesn't specify a pointer,
25408 see if the buffer text ``under'' it does. */
25409 struct glyph_row *r
25410 = MATRIX_ROW (w->current_matrix, vpos);
25411 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
25412 EMACS_INT pos = string_buffer_position (w, object,
25413 start);
25414 if (pos > 0)
25415 pointer = Fget_char_property (make_number (pos),
25416 Qpointer, w->buffer);
25419 else if (BUFFERP (object)
25420 && charpos >= BEGV
25421 && charpos < ZV)
25422 pointer = Fget_text_property (make_number (charpos),
25423 Qpointer, object);
25426 #endif /* HAVE_WINDOW_SYSTEM */
25428 BEGV = obegv;
25429 ZV = ozv;
25430 current_buffer = obuf;
25433 set_cursor:
25435 #ifdef HAVE_WINDOW_SYSTEM
25436 define_frame_cursor1 (f, cursor, pointer);
25437 #else
25438 /* This is here to prevent a compiler error, due to "label at end of
25439 compound statement". */
25440 return;
25441 #endif
25445 /* EXPORT for RIF:
25446 Clear any mouse-face on window W. This function is part of the
25447 redisplay interface, and is called from try_window_id and similar
25448 functions to ensure the mouse-highlight is off. */
25450 void
25451 x_clear_window_mouse_face (struct window *w)
25453 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25454 Lisp_Object window;
25456 BLOCK_INPUT;
25457 XSETWINDOW (window, w);
25458 if (EQ (window, hlinfo->mouse_face_window))
25459 clear_mouse_face (hlinfo);
25460 UNBLOCK_INPUT;
25464 /* EXPORT:
25465 Just discard the mouse face information for frame F, if any.
25466 This is used when the size of F is changed. */
25468 void
25469 cancel_mouse_face (struct frame *f)
25471 Lisp_Object window;
25472 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25474 window = hlinfo->mouse_face_window;
25475 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
25477 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25478 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25479 hlinfo->mouse_face_window = Qnil;
25485 /***********************************************************************
25486 Exposure Events
25487 ***********************************************************************/
25489 #ifdef HAVE_WINDOW_SYSTEM
25491 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
25492 which intersects rectangle R. R is in window-relative coordinates. */
25494 static void
25495 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25496 enum glyph_row_area area)
25498 struct glyph *first = row->glyphs[area];
25499 struct glyph *end = row->glyphs[area] + row->used[area];
25500 struct glyph *last;
25501 int first_x, start_x, x;
25503 if (area == TEXT_AREA && row->fill_line_p)
25504 /* If row extends face to end of line write the whole line. */
25505 draw_glyphs (w, 0, row, area,
25506 0, row->used[area],
25507 DRAW_NORMAL_TEXT, 0);
25508 else
25510 /* Set START_X to the window-relative start position for drawing glyphs of
25511 AREA. The first glyph of the text area can be partially visible.
25512 The first glyphs of other areas cannot. */
25513 start_x = window_box_left_offset (w, area);
25514 x = start_x;
25515 if (area == TEXT_AREA)
25516 x += row->x;
25518 /* Find the first glyph that must be redrawn. */
25519 while (first < end
25520 && x + first->pixel_width < r->x)
25522 x += first->pixel_width;
25523 ++first;
25526 /* Find the last one. */
25527 last = first;
25528 first_x = x;
25529 while (last < end
25530 && x < r->x + r->width)
25532 x += last->pixel_width;
25533 ++last;
25536 /* Repaint. */
25537 if (last > first)
25538 draw_glyphs (w, first_x - start_x, row, area,
25539 first - row->glyphs[area], last - row->glyphs[area],
25540 DRAW_NORMAL_TEXT, 0);
25545 /* Redraw the parts of the glyph row ROW on window W intersecting
25546 rectangle R. R is in window-relative coordinates. Value is
25547 non-zero if mouse-face was overwritten. */
25549 static int
25550 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25552 xassert (row->enabled_p);
25554 if (row->mode_line_p || w->pseudo_window_p)
25555 draw_glyphs (w, 0, row, TEXT_AREA,
25556 0, row->used[TEXT_AREA],
25557 DRAW_NORMAL_TEXT, 0);
25558 else
25560 if (row->used[LEFT_MARGIN_AREA])
25561 expose_area (w, row, r, LEFT_MARGIN_AREA);
25562 if (row->used[TEXT_AREA])
25563 expose_area (w, row, r, TEXT_AREA);
25564 if (row->used[RIGHT_MARGIN_AREA])
25565 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25566 draw_row_fringe_bitmaps (w, row);
25569 return row->mouse_face_p;
25573 /* Redraw those parts of glyphs rows during expose event handling that
25574 overlap other rows. Redrawing of an exposed line writes over parts
25575 of lines overlapping that exposed line; this function fixes that.
25577 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25578 row in W's current matrix that is exposed and overlaps other rows.
25579 LAST_OVERLAPPING_ROW is the last such row. */
25581 static void
25582 expose_overlaps (struct window *w,
25583 struct glyph_row *first_overlapping_row,
25584 struct glyph_row *last_overlapping_row,
25585 XRectangle *r)
25587 struct glyph_row *row;
25589 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25590 if (row->overlapping_p)
25592 xassert (row->enabled_p && !row->mode_line_p);
25594 row->clip = r;
25595 if (row->used[LEFT_MARGIN_AREA])
25596 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25598 if (row->used[TEXT_AREA])
25599 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25601 if (row->used[RIGHT_MARGIN_AREA])
25602 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25603 row->clip = NULL;
25608 /* Return non-zero if W's cursor intersects rectangle R. */
25610 static int
25611 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
25613 XRectangle cr, result;
25614 struct glyph *cursor_glyph;
25615 struct glyph_row *row;
25617 if (w->phys_cursor.vpos >= 0
25618 && w->phys_cursor.vpos < w->current_matrix->nrows
25619 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25620 row->enabled_p)
25621 && row->cursor_in_fringe_p)
25623 /* Cursor is in the fringe. */
25624 cr.x = window_box_right_offset (w,
25625 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25626 ? RIGHT_MARGIN_AREA
25627 : TEXT_AREA));
25628 cr.y = row->y;
25629 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25630 cr.height = row->height;
25631 return x_intersect_rectangles (&cr, r, &result);
25634 cursor_glyph = get_phys_cursor_glyph (w);
25635 if (cursor_glyph)
25637 /* r is relative to W's box, but w->phys_cursor.x is relative
25638 to left edge of W's TEXT area. Adjust it. */
25639 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25640 cr.y = w->phys_cursor.y;
25641 cr.width = cursor_glyph->pixel_width;
25642 cr.height = w->phys_cursor_height;
25643 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25644 I assume the effect is the same -- and this is portable. */
25645 return x_intersect_rectangles (&cr, r, &result);
25647 /* If we don't understand the format, pretend we're not in the hot-spot. */
25648 return 0;
25652 /* EXPORT:
25653 Draw a vertical window border to the right of window W if W doesn't
25654 have vertical scroll bars. */
25656 void
25657 x_draw_vertical_border (struct window *w)
25659 struct frame *f = XFRAME (WINDOW_FRAME (w));
25661 /* We could do better, if we knew what type of scroll-bar the adjacent
25662 windows (on either side) have... But we don't :-(
25663 However, I think this works ok. ++KFS 2003-04-25 */
25665 /* Redraw borders between horizontally adjacent windows. Don't
25666 do it for frames with vertical scroll bars because either the
25667 right scroll bar of a window, or the left scroll bar of its
25668 neighbor will suffice as a border. */
25669 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25670 return;
25672 if (!WINDOW_RIGHTMOST_P (w)
25673 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25675 int x0, x1, y0, y1;
25677 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25678 y1 -= 1;
25680 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25681 x1 -= 1;
25683 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25685 else if (!WINDOW_LEFTMOST_P (w)
25686 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25688 int x0, x1, y0, y1;
25690 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25691 y1 -= 1;
25693 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25694 x0 -= 1;
25696 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25701 /* Redraw the part of window W intersection rectangle FR. Pixel
25702 coordinates in FR are frame-relative. Call this function with
25703 input blocked. Value is non-zero if the exposure overwrites
25704 mouse-face. */
25706 static int
25707 expose_window (struct window *w, XRectangle *fr)
25709 struct frame *f = XFRAME (w->frame);
25710 XRectangle wr, r;
25711 int mouse_face_overwritten_p = 0;
25713 /* If window is not yet fully initialized, do nothing. This can
25714 happen when toolkit scroll bars are used and a window is split.
25715 Reconfiguring the scroll bar will generate an expose for a newly
25716 created window. */
25717 if (w->current_matrix == NULL)
25718 return 0;
25720 /* When we're currently updating the window, display and current
25721 matrix usually don't agree. Arrange for a thorough display
25722 later. */
25723 if (w == updated_window)
25725 SET_FRAME_GARBAGED (f);
25726 return 0;
25729 /* Frame-relative pixel rectangle of W. */
25730 wr.x = WINDOW_LEFT_EDGE_X (w);
25731 wr.y = WINDOW_TOP_EDGE_Y (w);
25732 wr.width = WINDOW_TOTAL_WIDTH (w);
25733 wr.height = WINDOW_TOTAL_HEIGHT (w);
25735 if (x_intersect_rectangles (fr, &wr, &r))
25737 int yb = window_text_bottom_y (w);
25738 struct glyph_row *row;
25739 int cursor_cleared_p;
25740 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25742 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25743 r.x, r.y, r.width, r.height));
25745 /* Convert to window coordinates. */
25746 r.x -= WINDOW_LEFT_EDGE_X (w);
25747 r.y -= WINDOW_TOP_EDGE_Y (w);
25749 /* Turn off the cursor. */
25750 if (!w->pseudo_window_p
25751 && phys_cursor_in_rect_p (w, &r))
25753 x_clear_cursor (w);
25754 cursor_cleared_p = 1;
25756 else
25757 cursor_cleared_p = 0;
25759 /* Update lines intersecting rectangle R. */
25760 first_overlapping_row = last_overlapping_row = NULL;
25761 for (row = w->current_matrix->rows;
25762 row->enabled_p;
25763 ++row)
25765 int y0 = row->y;
25766 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25768 if ((y0 >= r.y && y0 < r.y + r.height)
25769 || (y1 > r.y && y1 < r.y + r.height)
25770 || (r.y >= y0 && r.y < y1)
25771 || (r.y + r.height > y0 && r.y + r.height < y1))
25773 /* A header line may be overlapping, but there is no need
25774 to fix overlapping areas for them. KFS 2005-02-12 */
25775 if (row->overlapping_p && !row->mode_line_p)
25777 if (first_overlapping_row == NULL)
25778 first_overlapping_row = row;
25779 last_overlapping_row = row;
25782 row->clip = fr;
25783 if (expose_line (w, row, &r))
25784 mouse_face_overwritten_p = 1;
25785 row->clip = NULL;
25787 else if (row->overlapping_p)
25789 /* We must redraw a row overlapping the exposed area. */
25790 if (y0 < r.y
25791 ? y0 + row->phys_height > r.y
25792 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25794 if (first_overlapping_row == NULL)
25795 first_overlapping_row = row;
25796 last_overlapping_row = row;
25800 if (y1 >= yb)
25801 break;
25804 /* Display the mode line if there is one. */
25805 if (WINDOW_WANTS_MODELINE_P (w)
25806 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25807 row->enabled_p)
25808 && row->y < r.y + r.height)
25810 if (expose_line (w, row, &r))
25811 mouse_face_overwritten_p = 1;
25814 if (!w->pseudo_window_p)
25816 /* Fix the display of overlapping rows. */
25817 if (first_overlapping_row)
25818 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25819 fr);
25821 /* Draw border between windows. */
25822 x_draw_vertical_border (w);
25824 /* Turn the cursor on again. */
25825 if (cursor_cleared_p)
25826 update_window_cursor (w, 1);
25830 return mouse_face_overwritten_p;
25835 /* Redraw (parts) of all windows in the window tree rooted at W that
25836 intersect R. R contains frame pixel coordinates. Value is
25837 non-zero if the exposure overwrites mouse-face. */
25839 static int
25840 expose_window_tree (struct window *w, XRectangle *r)
25842 struct frame *f = XFRAME (w->frame);
25843 int mouse_face_overwritten_p = 0;
25845 while (w && !FRAME_GARBAGED_P (f))
25847 if (!NILP (w->hchild))
25848 mouse_face_overwritten_p
25849 |= expose_window_tree (XWINDOW (w->hchild), r);
25850 else if (!NILP (w->vchild))
25851 mouse_face_overwritten_p
25852 |= expose_window_tree (XWINDOW (w->vchild), r);
25853 else
25854 mouse_face_overwritten_p |= expose_window (w, r);
25856 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25859 return mouse_face_overwritten_p;
25863 /* EXPORT:
25864 Redisplay an exposed area of frame F. X and Y are the upper-left
25865 corner of the exposed rectangle. W and H are width and height of
25866 the exposed area. All are pixel values. W or H zero means redraw
25867 the entire frame. */
25869 void
25870 expose_frame (struct frame *f, int x, int y, int w, int h)
25872 XRectangle r;
25873 int mouse_face_overwritten_p = 0;
25875 TRACE ((stderr, "expose_frame "));
25877 /* No need to redraw if frame will be redrawn soon. */
25878 if (FRAME_GARBAGED_P (f))
25880 TRACE ((stderr, " garbaged\n"));
25881 return;
25884 /* If basic faces haven't been realized yet, there is no point in
25885 trying to redraw anything. This can happen when we get an expose
25886 event while Emacs is starting, e.g. by moving another window. */
25887 if (FRAME_FACE_CACHE (f) == NULL
25888 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25890 TRACE ((stderr, " no faces\n"));
25891 return;
25894 if (w == 0 || h == 0)
25896 r.x = r.y = 0;
25897 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25898 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25900 else
25902 r.x = x;
25903 r.y = y;
25904 r.width = w;
25905 r.height = h;
25908 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25909 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25911 if (WINDOWP (f->tool_bar_window))
25912 mouse_face_overwritten_p
25913 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25915 #ifdef HAVE_X_WINDOWS
25916 #ifndef MSDOS
25917 #ifndef USE_X_TOOLKIT
25918 if (WINDOWP (f->menu_bar_window))
25919 mouse_face_overwritten_p
25920 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25921 #endif /* not USE_X_TOOLKIT */
25922 #endif
25923 #endif
25925 /* Some window managers support a focus-follows-mouse style with
25926 delayed raising of frames. Imagine a partially obscured frame,
25927 and moving the mouse into partially obscured mouse-face on that
25928 frame. The visible part of the mouse-face will be highlighted,
25929 then the WM raises the obscured frame. With at least one WM, KDE
25930 2.1, Emacs is not getting any event for the raising of the frame
25931 (even tried with SubstructureRedirectMask), only Expose events.
25932 These expose events will draw text normally, i.e. not
25933 highlighted. Which means we must redo the highlight here.
25934 Subsume it under ``we love X''. --gerd 2001-08-15 */
25935 /* Included in Windows version because Windows most likely does not
25936 do the right thing if any third party tool offers
25937 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25938 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25940 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25941 if (f == hlinfo->mouse_face_mouse_frame)
25943 int x = hlinfo->mouse_face_mouse_x;
25944 int y = hlinfo->mouse_face_mouse_y;
25945 clear_mouse_face (hlinfo);
25946 note_mouse_highlight (f, x, y);
25952 /* EXPORT:
25953 Determine the intersection of two rectangles R1 and R2. Return
25954 the intersection in *RESULT. Value is non-zero if RESULT is not
25955 empty. */
25958 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
25960 XRectangle *left, *right;
25961 XRectangle *upper, *lower;
25962 int intersection_p = 0;
25964 /* Rearrange so that R1 is the left-most rectangle. */
25965 if (r1->x < r2->x)
25966 left = r1, right = r2;
25967 else
25968 left = r2, right = r1;
25970 /* X0 of the intersection is right.x0, if this is inside R1,
25971 otherwise there is no intersection. */
25972 if (right->x <= left->x + left->width)
25974 result->x = right->x;
25976 /* The right end of the intersection is the minimum of the
25977 the right ends of left and right. */
25978 result->width = (min (left->x + left->width, right->x + right->width)
25979 - result->x);
25981 /* Same game for Y. */
25982 if (r1->y < r2->y)
25983 upper = r1, lower = r2;
25984 else
25985 upper = r2, lower = r1;
25987 /* The upper end of the intersection is lower.y0, if this is inside
25988 of upper. Otherwise, there is no intersection. */
25989 if (lower->y <= upper->y + upper->height)
25991 result->y = lower->y;
25993 /* The lower end of the intersection is the minimum of the lower
25994 ends of upper and lower. */
25995 result->height = (min (lower->y + lower->height,
25996 upper->y + upper->height)
25997 - result->y);
25998 intersection_p = 1;
26002 return intersection_p;
26005 #endif /* HAVE_WINDOW_SYSTEM */
26008 /***********************************************************************
26009 Initialization
26010 ***********************************************************************/
26012 void
26013 syms_of_xdisp (void)
26015 Vwith_echo_area_save_vector = Qnil;
26016 staticpro (&Vwith_echo_area_save_vector);
26018 Vmessage_stack = Qnil;
26019 staticpro (&Vmessage_stack);
26021 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
26022 staticpro (&Qinhibit_redisplay);
26024 message_dolog_marker1 = Fmake_marker ();
26025 staticpro (&message_dolog_marker1);
26026 message_dolog_marker2 = Fmake_marker ();
26027 staticpro (&message_dolog_marker2);
26028 message_dolog_marker3 = Fmake_marker ();
26029 staticpro (&message_dolog_marker3);
26031 #if GLYPH_DEBUG
26032 defsubr (&Sdump_frame_glyph_matrix);
26033 defsubr (&Sdump_glyph_matrix);
26034 defsubr (&Sdump_glyph_row);
26035 defsubr (&Sdump_tool_bar_row);
26036 defsubr (&Strace_redisplay);
26037 defsubr (&Strace_to_stderr);
26038 #endif
26039 #ifdef HAVE_WINDOW_SYSTEM
26040 defsubr (&Stool_bar_lines_needed);
26041 defsubr (&Slookup_image_map);
26042 #endif
26043 defsubr (&Sformat_mode_line);
26044 defsubr (&Sinvisible_p);
26045 defsubr (&Scurrent_bidi_paragraph_direction);
26047 staticpro (&Qmenu_bar_update_hook);
26048 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
26050 staticpro (&Qoverriding_terminal_local_map);
26051 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
26053 staticpro (&Qoverriding_local_map);
26054 Qoverriding_local_map = intern_c_string ("overriding-local-map");
26056 staticpro (&Qwindow_scroll_functions);
26057 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
26059 staticpro (&Qwindow_text_change_functions);
26060 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
26062 staticpro (&Qredisplay_end_trigger_functions);
26063 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
26065 staticpro (&Qinhibit_point_motion_hooks);
26066 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
26068 Qeval = intern_c_string ("eval");
26069 staticpro (&Qeval);
26071 QCdata = intern_c_string (":data");
26072 staticpro (&QCdata);
26073 Qdisplay = intern_c_string ("display");
26074 staticpro (&Qdisplay);
26075 Qspace_width = intern_c_string ("space-width");
26076 staticpro (&Qspace_width);
26077 Qraise = intern_c_string ("raise");
26078 staticpro (&Qraise);
26079 Qslice = intern_c_string ("slice");
26080 staticpro (&Qslice);
26081 Qspace = intern_c_string ("space");
26082 staticpro (&Qspace);
26083 Qmargin = intern_c_string ("margin");
26084 staticpro (&Qmargin);
26085 Qpointer = intern_c_string ("pointer");
26086 staticpro (&Qpointer);
26087 Qleft_margin = intern_c_string ("left-margin");
26088 staticpro (&Qleft_margin);
26089 Qright_margin = intern_c_string ("right-margin");
26090 staticpro (&Qright_margin);
26091 Qcenter = intern_c_string ("center");
26092 staticpro (&Qcenter);
26093 Qline_height = intern_c_string ("line-height");
26094 staticpro (&Qline_height);
26095 QCalign_to = intern_c_string (":align-to");
26096 staticpro (&QCalign_to);
26097 QCrelative_width = intern_c_string (":relative-width");
26098 staticpro (&QCrelative_width);
26099 QCrelative_height = intern_c_string (":relative-height");
26100 staticpro (&QCrelative_height);
26101 QCeval = intern_c_string (":eval");
26102 staticpro (&QCeval);
26103 QCpropertize = intern_c_string (":propertize");
26104 staticpro (&QCpropertize);
26105 QCfile = intern_c_string (":file");
26106 staticpro (&QCfile);
26107 Qfontified = intern_c_string ("fontified");
26108 staticpro (&Qfontified);
26109 Qfontification_functions = intern_c_string ("fontification-functions");
26110 staticpro (&Qfontification_functions);
26111 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
26112 staticpro (&Qtrailing_whitespace);
26113 Qescape_glyph = intern_c_string ("escape-glyph");
26114 staticpro (&Qescape_glyph);
26115 Qnobreak_space = intern_c_string ("nobreak-space");
26116 staticpro (&Qnobreak_space);
26117 Qimage = intern_c_string ("image");
26118 staticpro (&Qimage);
26119 Qtext = intern_c_string ("text");
26120 staticpro (&Qtext);
26121 Qboth = intern_c_string ("both");
26122 staticpro (&Qboth);
26123 Qboth_horiz = intern_c_string ("both-horiz");
26124 staticpro (&Qboth_horiz);
26125 Qtext_image_horiz = intern_c_string ("text-image-horiz");
26126 staticpro (&Qtext_image_horiz);
26127 QCmap = intern_c_string (":map");
26128 staticpro (&QCmap);
26129 QCpointer = intern_c_string (":pointer");
26130 staticpro (&QCpointer);
26131 Qrect = intern_c_string ("rect");
26132 staticpro (&Qrect);
26133 Qcircle = intern_c_string ("circle");
26134 staticpro (&Qcircle);
26135 Qpoly = intern_c_string ("poly");
26136 staticpro (&Qpoly);
26137 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
26138 staticpro (&Qmessage_truncate_lines);
26139 Qgrow_only = intern_c_string ("grow-only");
26140 staticpro (&Qgrow_only);
26141 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
26142 staticpro (&Qinhibit_menubar_update);
26143 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
26144 staticpro (&Qinhibit_eval_during_redisplay);
26145 Qposition = intern_c_string ("position");
26146 staticpro (&Qposition);
26147 Qbuffer_position = intern_c_string ("buffer-position");
26148 staticpro (&Qbuffer_position);
26149 Qobject = intern_c_string ("object");
26150 staticpro (&Qobject);
26151 Qbar = intern_c_string ("bar");
26152 staticpro (&Qbar);
26153 Qhbar = intern_c_string ("hbar");
26154 staticpro (&Qhbar);
26155 Qbox = intern_c_string ("box");
26156 staticpro (&Qbox);
26157 Qhollow = intern_c_string ("hollow");
26158 staticpro (&Qhollow);
26159 Qhand = intern_c_string ("hand");
26160 staticpro (&Qhand);
26161 Qarrow = intern_c_string ("arrow");
26162 staticpro (&Qarrow);
26163 Qtext = intern_c_string ("text");
26164 staticpro (&Qtext);
26165 Qrisky_local_variable = intern_c_string ("risky-local-variable");
26166 staticpro (&Qrisky_local_variable);
26167 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
26168 staticpro (&Qinhibit_free_realized_faces);
26170 list_of_error = Fcons (Fcons (intern_c_string ("error"),
26171 Fcons (intern_c_string ("void-variable"), Qnil)),
26172 Qnil);
26173 staticpro (&list_of_error);
26175 Qlast_arrow_position = intern_c_string ("last-arrow-position");
26176 staticpro (&Qlast_arrow_position);
26177 Qlast_arrow_string = intern_c_string ("last-arrow-string");
26178 staticpro (&Qlast_arrow_string);
26180 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
26181 staticpro (&Qoverlay_arrow_string);
26182 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
26183 staticpro (&Qoverlay_arrow_bitmap);
26185 echo_buffer[0] = echo_buffer[1] = Qnil;
26186 staticpro (&echo_buffer[0]);
26187 staticpro (&echo_buffer[1]);
26189 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
26190 staticpro (&echo_area_buffer[0]);
26191 staticpro (&echo_area_buffer[1]);
26193 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
26194 staticpro (&Vmessages_buffer_name);
26196 mode_line_proptrans_alist = Qnil;
26197 staticpro (&mode_line_proptrans_alist);
26198 mode_line_string_list = Qnil;
26199 staticpro (&mode_line_string_list);
26200 mode_line_string_face = Qnil;
26201 staticpro (&mode_line_string_face);
26202 mode_line_string_face_prop = Qnil;
26203 staticpro (&mode_line_string_face_prop);
26204 Vmode_line_unwind_vector = Qnil;
26205 staticpro (&Vmode_line_unwind_vector);
26207 help_echo_string = Qnil;
26208 staticpro (&help_echo_string);
26209 help_echo_object = Qnil;
26210 staticpro (&help_echo_object);
26211 help_echo_window = Qnil;
26212 staticpro (&help_echo_window);
26213 previous_help_echo_string = Qnil;
26214 staticpro (&previous_help_echo_string);
26215 help_echo_pos = -1;
26217 Qright_to_left = intern_c_string ("right-to-left");
26218 staticpro (&Qright_to_left);
26219 Qleft_to_right = intern_c_string ("left-to-right");
26220 staticpro (&Qleft_to_right);
26222 #ifdef HAVE_WINDOW_SYSTEM
26223 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
26224 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
26225 For example, if a block cursor is over a tab, it will be drawn as
26226 wide as that tab on the display. */);
26227 x_stretch_cursor_p = 0;
26228 #endif
26230 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
26231 doc: /* *Non-nil means highlight trailing whitespace.
26232 The face used for trailing whitespace is `trailing-whitespace'. */);
26233 Vshow_trailing_whitespace = Qnil;
26235 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
26236 doc: /* *Control highlighting of nobreak space and soft hyphen.
26237 A value of t means highlight the character itself (for nobreak space,
26238 use face `nobreak-space').
26239 A value of nil means no highlighting.
26240 Other values mean display the escape glyph followed by an ordinary
26241 space or ordinary hyphen. */);
26242 Vnobreak_char_display = Qt;
26244 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
26245 doc: /* *The pointer shape to show in void text areas.
26246 A value of nil means to show the text pointer. Other options are `arrow',
26247 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
26248 Vvoid_text_area_pointer = Qarrow;
26250 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
26251 doc: /* Non-nil means don't actually do any redisplay.
26252 This is used for internal purposes. */);
26253 Vinhibit_redisplay = Qnil;
26255 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
26256 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
26257 Vglobal_mode_string = Qnil;
26259 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
26260 doc: /* Marker for where to display an arrow on top of the buffer text.
26261 This must be the beginning of a line in order to work.
26262 See also `overlay-arrow-string'. */);
26263 Voverlay_arrow_position = Qnil;
26265 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
26266 doc: /* String to display as an arrow in non-window frames.
26267 See also `overlay-arrow-position'. */);
26268 Voverlay_arrow_string = make_pure_c_string ("=>");
26270 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
26271 doc: /* List of variables (symbols) which hold markers for overlay arrows.
26272 The symbols on this list are examined during redisplay to determine
26273 where to display overlay arrows. */);
26274 Voverlay_arrow_variable_list
26275 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
26277 DEFVAR_INT ("scroll-step", &scroll_step,
26278 doc: /* *The number of lines to try scrolling a window by when point moves out.
26279 If that fails to bring point back on frame, point is centered instead.
26280 If this is zero, point is always centered after it moves off frame.
26281 If you want scrolling to always be a line at a time, you should set
26282 `scroll-conservatively' to a large value rather than set this to 1. */);
26284 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
26285 doc: /* *Scroll up to this many lines, to bring point back on screen.
26286 If point moves off-screen, redisplay will scroll by up to
26287 `scroll-conservatively' lines in order to bring point just barely
26288 onto the screen again. If that cannot be done, then redisplay
26289 recenters point as usual.
26291 A value of zero means always recenter point if it moves off screen. */);
26292 scroll_conservatively = 0;
26294 DEFVAR_INT ("scroll-margin", &scroll_margin,
26295 doc: /* *Number of lines of margin at the top and bottom of a window.
26296 Recenter the window whenever point gets within this many lines
26297 of the top or bottom of the window. */);
26298 scroll_margin = 0;
26300 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
26301 doc: /* Pixels per inch value for non-window system displays.
26302 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
26303 Vdisplay_pixels_per_inch = make_float (72.0);
26305 #if GLYPH_DEBUG
26306 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
26307 #endif
26309 DEFVAR_LISP ("truncate-partial-width-windows",
26310 &Vtruncate_partial_width_windows,
26311 doc: /* Non-nil means truncate lines in windows narrower than the frame.
26312 For an integer value, truncate lines in each window narrower than the
26313 full frame width, provided the window width is less than that integer;
26314 otherwise, respect the value of `truncate-lines'.
26316 For any other non-nil value, truncate lines in all windows that do
26317 not span the full frame width.
26319 A value of nil means to respect the value of `truncate-lines'.
26321 If `word-wrap' is enabled, you might want to reduce this. */);
26322 Vtruncate_partial_width_windows = make_number (50);
26324 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
26325 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
26326 Any other value means to use the appropriate face, `mode-line',
26327 `header-line', or `menu' respectively. */);
26328 mode_line_inverse_video = 1;
26330 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
26331 doc: /* *Maximum buffer size for which line number should be displayed.
26332 If the buffer is bigger than this, the line number does not appear
26333 in the mode line. A value of nil means no limit. */);
26334 Vline_number_display_limit = Qnil;
26336 DEFVAR_INT ("line-number-display-limit-width",
26337 &line_number_display_limit_width,
26338 doc: /* *Maximum line width (in characters) for line number display.
26339 If the average length of the lines near point is bigger than this, then the
26340 line number may be omitted from the mode line. */);
26341 line_number_display_limit_width = 200;
26343 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
26344 doc: /* *Non-nil means highlight region even in nonselected windows. */);
26345 highlight_nonselected_windows = 0;
26347 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
26348 doc: /* Non-nil if more than one frame is visible on this display.
26349 Minibuffer-only frames don't count, but iconified frames do.
26350 This variable is not guaranteed to be accurate except while processing
26351 `frame-title-format' and `icon-title-format'. */);
26353 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
26354 doc: /* Template for displaying the title bar of visible frames.
26355 \(Assuming the window manager supports this feature.)
26357 This variable has the same structure as `mode-line-format', except that
26358 the %c and %l constructs are ignored. It is used only on frames for
26359 which no explicit name has been set \(see `modify-frame-parameters'). */);
26361 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
26362 doc: /* Template for displaying the title bar of an iconified frame.
26363 \(Assuming the window manager supports this feature.)
26364 This variable has the same structure as `mode-line-format' (which see),
26365 and is used only on frames for which no explicit name has been set
26366 \(see `modify-frame-parameters'). */);
26367 Vicon_title_format
26368 = Vframe_title_format
26369 = pure_cons (intern_c_string ("multiple-frames"),
26370 pure_cons (make_pure_c_string ("%b"),
26371 pure_cons (pure_cons (empty_unibyte_string,
26372 pure_cons (intern_c_string ("invocation-name"),
26373 pure_cons (make_pure_c_string ("@"),
26374 pure_cons (intern_c_string ("system-name"),
26375 Qnil)))),
26376 Qnil)));
26378 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
26379 doc: /* Maximum number of lines to keep in the message log buffer.
26380 If nil, disable message logging. If t, log messages but don't truncate
26381 the buffer when it becomes large. */);
26382 Vmessage_log_max = make_number (100);
26384 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
26385 doc: /* Functions called before redisplay, if window sizes have changed.
26386 The value should be a list of functions that take one argument.
26387 Just before redisplay, for each frame, if any of its windows have changed
26388 size since the last redisplay, or have been split or deleted,
26389 all the functions in the list are called, with the frame as argument. */);
26390 Vwindow_size_change_functions = Qnil;
26392 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
26393 doc: /* List of functions to call before redisplaying a window with scrolling.
26394 Each function is called with two arguments, the window and its new
26395 display-start position. Note that these functions are also called by
26396 `set-window-buffer'. Also note that the value of `window-end' is not
26397 valid when these functions are called. */);
26398 Vwindow_scroll_functions = Qnil;
26400 DEFVAR_LISP ("window-text-change-functions",
26401 &Vwindow_text_change_functions,
26402 doc: /* Functions to call in redisplay when text in the window might change. */);
26403 Vwindow_text_change_functions = Qnil;
26405 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
26406 doc: /* Functions called when redisplay of a window reaches the end trigger.
26407 Each function is called with two arguments, the window and the end trigger value.
26408 See `set-window-redisplay-end-trigger'. */);
26409 Vredisplay_end_trigger_functions = Qnil;
26411 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
26412 doc: /* *Non-nil means autoselect window with mouse pointer.
26413 If nil, do not autoselect windows.
26414 A positive number means delay autoselection by that many seconds: a
26415 window is autoselected only after the mouse has remained in that
26416 window for the duration of the delay.
26417 A negative number has a similar effect, but causes windows to be
26418 autoselected only after the mouse has stopped moving. \(Because of
26419 the way Emacs compares mouse events, you will occasionally wait twice
26420 that time before the window gets selected.\)
26421 Any other value means to autoselect window instantaneously when the
26422 mouse pointer enters it.
26424 Autoselection selects the minibuffer only if it is active, and never
26425 unselects the minibuffer if it is active.
26427 When customizing this variable make sure that the actual value of
26428 `focus-follows-mouse' matches the behavior of your window manager. */);
26429 Vmouse_autoselect_window = Qnil;
26431 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
26432 doc: /* *Non-nil means automatically resize tool-bars.
26433 This dynamically changes the tool-bar's height to the minimum height
26434 that is needed to make all tool-bar items visible.
26435 If value is `grow-only', the tool-bar's height is only increased
26436 automatically; to decrease the tool-bar height, use \\[recenter]. */);
26437 Vauto_resize_tool_bars = Qt;
26439 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
26440 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
26441 auto_raise_tool_bar_buttons_p = 1;
26443 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
26444 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
26445 make_cursor_line_fully_visible_p = 1;
26447 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
26448 doc: /* *Border below tool-bar in pixels.
26449 If an integer, use it as the height of the border.
26450 If it is one of `internal-border-width' or `border-width', use the
26451 value of the corresponding frame parameter.
26452 Otherwise, no border is added below the tool-bar. */);
26453 Vtool_bar_border = Qinternal_border_width;
26455 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
26456 doc: /* *Margin around tool-bar buttons in pixels.
26457 If an integer, use that for both horizontal and vertical margins.
26458 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
26459 HORZ specifying the horizontal margin, and VERT specifying the
26460 vertical margin. */);
26461 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
26463 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
26464 doc: /* *Relief thickness of tool-bar buttons. */);
26465 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
26467 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
26468 doc: /* *Tool bar style to use.
26469 It can be one of
26470 image - show images only
26471 text - show text only
26472 both - show both, text below image
26473 both-horiz - show text to the right of the image
26474 text-image-horiz - show text to the left of the image
26475 any other - use system default or image if no system default. */);
26476 Vtool_bar_style = Qnil;
26478 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
26479 doc: /* *Maximum number of characters a label can have to be shown.
26480 The tool bar style must also show labels for this to have any effect, see
26481 `tool-bar-style'. */);
26482 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
26484 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
26485 doc: /* List of functions to call to fontify regions of text.
26486 Each function is called with one argument POS. Functions must
26487 fontify a region starting at POS in the current buffer, and give
26488 fontified regions the property `fontified'. */);
26489 Vfontification_functions = Qnil;
26490 Fmake_variable_buffer_local (Qfontification_functions);
26492 DEFVAR_BOOL ("unibyte-display-via-language-environment",
26493 &unibyte_display_via_language_environment,
26494 doc: /* *Non-nil means display unibyte text according to language environment.
26495 Specifically, this means that raw bytes in the range 160-255 decimal
26496 are displayed by converting them to the equivalent multibyte characters
26497 according to the current language environment. As a result, they are
26498 displayed according to the current fontset.
26500 Note that this variable affects only how these bytes are displayed,
26501 but does not change the fact they are interpreted as raw bytes. */);
26502 unibyte_display_via_language_environment = 0;
26504 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26505 doc: /* *Maximum height for resizing mini-windows.
26506 If a float, it specifies a fraction of the mini-window frame's height.
26507 If an integer, it specifies a number of lines. */);
26508 Vmax_mini_window_height = make_float (0.25);
26510 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26511 doc: /* *How to resize mini-windows.
26512 A value of nil means don't automatically resize mini-windows.
26513 A value of t means resize them to fit the text displayed in them.
26514 A value of `grow-only', the default, means let mini-windows grow
26515 only, until their display becomes empty, at which point the windows
26516 go back to their normal size. */);
26517 Vresize_mini_windows = Qgrow_only;
26519 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26520 doc: /* Alist specifying how to blink the cursor off.
26521 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26522 `cursor-type' frame-parameter or variable equals ON-STATE,
26523 comparing using `equal', Emacs uses OFF-STATE to specify
26524 how to blink it off. ON-STATE and OFF-STATE are values for
26525 the `cursor-type' frame parameter.
26527 If a frame's ON-STATE has no entry in this list,
26528 the frame's other specifications determine how to blink the cursor off. */);
26529 Vblink_cursor_alist = Qnil;
26531 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26532 doc: /* Allow or disallow automatic horizontal scrolling of windows.
26533 If non-nil, windows are automatically scrolled horizontally to make
26534 point visible. */);
26535 automatic_hscrolling_p = 1;
26536 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26537 staticpro (&Qauto_hscroll_mode);
26539 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26540 doc: /* *How many columns away from the window edge point is allowed to get
26541 before automatic hscrolling will horizontally scroll the window. */);
26542 hscroll_margin = 5;
26544 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26545 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26546 When point is less than `hscroll-margin' columns from the window
26547 edge, automatic hscrolling will scroll the window by the amount of columns
26548 determined by this variable. If its value is a positive integer, scroll that
26549 many columns. If it's a positive floating-point number, it specifies the
26550 fraction of the window's width to scroll. If it's nil or zero, point will be
26551 centered horizontally after the scroll. Any other value, including negative
26552 numbers, are treated as if the value were zero.
26554 Automatic hscrolling always moves point outside the scroll margin, so if
26555 point was more than scroll step columns inside the margin, the window will
26556 scroll more than the value given by the scroll step.
26558 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26559 and `scroll-right' overrides this variable's effect. */);
26560 Vhscroll_step = make_number (0);
26562 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26563 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26564 Bind this around calls to `message' to let it take effect. */);
26565 message_truncate_lines = 0;
26567 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26568 doc: /* Normal hook run to update the menu bar definitions.
26569 Redisplay runs this hook before it redisplays the menu bar.
26570 This is used to update submenus such as Buffers,
26571 whose contents depend on various data. */);
26572 Vmenu_bar_update_hook = Qnil;
26574 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26575 doc: /* Frame for which we are updating a menu.
26576 The enable predicate for a menu binding should check this variable. */);
26577 Vmenu_updating_frame = Qnil;
26579 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26580 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26581 inhibit_menubar_update = 0;
26583 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26584 doc: /* Prefix prepended to all continuation lines at display time.
26585 The value may be a string, an image, or a stretch-glyph; it is
26586 interpreted in the same way as the value of a `display' text property.
26588 This variable is overridden by any `wrap-prefix' text or overlay
26589 property.
26591 To add a prefix to non-continuation lines, use `line-prefix'. */);
26592 Vwrap_prefix = Qnil;
26593 staticpro (&Qwrap_prefix);
26594 Qwrap_prefix = intern_c_string ("wrap-prefix");
26595 Fmake_variable_buffer_local (Qwrap_prefix);
26597 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26598 doc: /* Prefix prepended to all non-continuation lines at display time.
26599 The value may be a string, an image, or a stretch-glyph; it is
26600 interpreted in the same way as the value of a `display' text property.
26602 This variable is overridden by any `line-prefix' text or overlay
26603 property.
26605 To add a prefix to continuation lines, use `wrap-prefix'. */);
26606 Vline_prefix = Qnil;
26607 staticpro (&Qline_prefix);
26608 Qline_prefix = intern_c_string ("line-prefix");
26609 Fmake_variable_buffer_local (Qline_prefix);
26611 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
26612 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26613 inhibit_eval_during_redisplay = 0;
26615 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
26616 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26617 inhibit_free_realized_faces = 0;
26619 #if GLYPH_DEBUG
26620 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
26621 doc: /* Inhibit try_window_id display optimization. */);
26622 inhibit_try_window_id = 0;
26624 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
26625 doc: /* Inhibit try_window_reusing display optimization. */);
26626 inhibit_try_window_reusing = 0;
26628 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
26629 doc: /* Inhibit try_cursor_movement display optimization. */);
26630 inhibit_try_cursor_movement = 0;
26631 #endif /* GLYPH_DEBUG */
26633 DEFVAR_INT ("overline-margin", &overline_margin,
26634 doc: /* *Space between overline and text, in pixels.
26635 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26636 margin to the caracter height. */);
26637 overline_margin = 2;
26639 DEFVAR_INT ("underline-minimum-offset",
26640 &underline_minimum_offset,
26641 doc: /* Minimum distance between baseline and underline.
26642 This can improve legibility of underlined text at small font sizes,
26643 particularly when using variable `x-use-underline-position-properties'
26644 with fonts that specify an UNDERLINE_POSITION relatively close to the
26645 baseline. The default value is 1. */);
26646 underline_minimum_offset = 1;
26648 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
26649 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
26650 This feature only works when on a window system that can change
26651 cursor shapes. */);
26652 display_hourglass_p = 1;
26654 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26655 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
26656 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26658 hourglass_atimer = NULL;
26659 hourglass_shown_p = 0;
26663 /* Initialize this module when Emacs starts. */
26665 void
26666 init_xdisp (void)
26668 Lisp_Object root_window;
26669 struct window *mini_w;
26671 current_header_line_height = current_mode_line_height = -1;
26673 CHARPOS (this_line_start_pos) = 0;
26675 mini_w = XWINDOW (minibuf_window);
26676 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26678 if (!noninteractive)
26680 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26681 int i;
26683 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26684 set_window_height (root_window,
26685 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26687 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26688 set_window_height (minibuf_window, 1, 0);
26690 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26691 mini_w->total_cols = make_number (FRAME_COLS (f));
26693 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26694 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26695 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26697 /* The default ellipsis glyphs `...'. */
26698 for (i = 0; i < 3; ++i)
26699 default_invis_vector[i] = make_number ('.');
26703 /* Allocate the buffer for frame titles.
26704 Also used for `format-mode-line'. */
26705 int size = 100;
26706 mode_line_noprop_buf = (char *) xmalloc (size);
26707 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26708 mode_line_noprop_ptr = mode_line_noprop_buf;
26709 mode_line_target = MODE_LINE_DISPLAY;
26712 help_echo_showing_p = 0;
26715 /* Since w32 does not support atimers, it defines its own implementation of
26716 the following three functions in w32fns.c. */
26717 #ifndef WINDOWSNT
26719 /* Platform-independent portion of hourglass implementation. */
26721 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26723 hourglass_started (void)
26725 return hourglass_shown_p || hourglass_atimer != NULL;
26728 /* Cancel a currently active hourglass timer, and start a new one. */
26729 void
26730 start_hourglass (void)
26732 #if defined (HAVE_WINDOW_SYSTEM)
26733 EMACS_TIME delay;
26734 int secs, usecs = 0;
26736 cancel_hourglass ();
26738 if (INTEGERP (Vhourglass_delay)
26739 && XINT (Vhourglass_delay) > 0)
26740 secs = XFASTINT (Vhourglass_delay);
26741 else if (FLOATP (Vhourglass_delay)
26742 && XFLOAT_DATA (Vhourglass_delay) > 0)
26744 Lisp_Object tem;
26745 tem = Ftruncate (Vhourglass_delay, Qnil);
26746 secs = XFASTINT (tem);
26747 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26749 else
26750 secs = DEFAULT_HOURGLASS_DELAY;
26752 EMACS_SET_SECS_USECS (delay, secs, usecs);
26753 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26754 show_hourglass, NULL);
26755 #endif
26759 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26760 shown. */
26761 void
26762 cancel_hourglass (void)
26764 #if defined (HAVE_WINDOW_SYSTEM)
26765 if (hourglass_atimer)
26767 cancel_atimer (hourglass_atimer);
26768 hourglass_atimer = NULL;
26771 if (hourglass_shown_p)
26772 hide_hourglass ();
26773 #endif
26775 #endif /* ! WINDOWSNT */