* src/term.c: Remove dead code.
[emacs.git] / src / xdisp.c
blob3274a48294ff9302c8c2d0e1ce6c38de30b679d9
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24 Redisplay.
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code.
37 The following diagram shows how redisplay code is invoked. As you
38 can see, Lisp calls redisplay and vice versa. Under window systems
39 like X, some portions of the redisplay code are also called
40 asynchronously during mouse movement or expose events. It is very
41 important that these code parts do NOT use the C library (malloc,
42 free) because many C libraries under Unix are not reentrant. They
43 may also NOT call functions of the Lisp interpreter which could
44 change the interpreter's state. If you don't follow these rules,
45 you will encounter bugs which are very hard to explain.
47 +--------------+ redisplay +----------------+
48 | Lisp machine |---------------->| Redisplay code |<--+
49 +--------------+ (xdisp.c) +----------------+ |
50 ^ | |
51 +----------------------------------+ |
52 Don't use this path when called |
53 asynchronously! |
55 expose_window (asynchronous) |
57 X expose events -----+
59 What does redisplay do? Obviously, it has to figure out somehow what
60 has been changed since the last time the display has been updated,
61 and to make these changes visible. Preferably it would do that in
62 a moderately intelligent way, i.e. fast.
64 Changes in buffer text can be deduced from window and buffer
65 structures, and from some global variables like `beg_unchanged' and
66 `end_unchanged'. The contents of the display are additionally
67 recorded in a `glyph matrix', a two-dimensional matrix of glyph
68 structures. Each row in such a matrix corresponds to a line on the
69 display, and each glyph in a row corresponds to a column displaying
70 a character, an image, or what else. This matrix is called the
71 `current glyph matrix' or `current matrix' in redisplay
72 terminology.
74 For buffer parts that have been changed since the last update, a
75 second glyph matrix is constructed, the so called `desired glyph
76 matrix' or short `desired matrix'. Current and desired matrix are
77 then compared to find a cheap way to update the display, e.g. by
78 reusing part of the display by scrolling lines.
80 You will find a lot of redisplay optimizations when you start
81 looking at the innards of redisplay. The overall goal of all these
82 optimizations is to make redisplay fast because it is done
83 frequently. Some of these optimizations are implemented by the
84 following functions:
86 . try_cursor_movement
88 This function tries to update the display if the text in the
89 window did not change and did not scroll, only point moved, and
90 it did not move off the displayed portion of the text.
92 . try_window_reusing_current_matrix
94 This function reuses the current matrix of a window when text
95 has not changed, but the window start changed (e.g., due to
96 scrolling).
98 . try_window_id
100 This function attempts to redisplay a window by reusing parts of
101 its existing display. It finds and reuses the part that was not
102 changed, and redraws the rest.
104 . try_window
106 This function performs the full redisplay of a single window
107 assuming that its fonts were not changed and that the cursor
108 will not end up in the scroll margins. (Loading fonts requires
109 re-adjustment of dimensions of glyph matrices, which makes this
110 method impossible to use.)
112 These optimizations are tried in sequence (some can be skipped if
113 it is known that they are not applicable). If none of the
114 optimizations were successful, redisplay calls redisplay_windows,
115 which performs a full redisplay of all windows.
117 Desired matrices.
119 Desired matrices are always built per Emacs window. The function
120 `display_line' is the central function to look at if you are
121 interested. It constructs one row in a desired matrix given an
122 iterator structure containing both a buffer position and a
123 description of the environment in which the text is to be
124 displayed. But this is too early, read on.
126 Characters and pixmaps displayed for a range of buffer text depend
127 on various settings of buffers and windows, on overlays and text
128 properties, on display tables, on selective display. The good news
129 is that all this hairy stuff is hidden behind a small set of
130 interface functions taking an iterator structure (struct it)
131 argument.
133 Iteration over things to be displayed is then simple. It is
134 started by initializing an iterator with a call to init_iterator.
135 Calls to get_next_display_element fill the iterator structure with
136 relevant information about the next thing to display. Calls to
137 set_iterator_to_next move the iterator to the next thing.
139 Besides this, an iterator also contains information about the
140 display environment in which glyphs for display elements are to be
141 produced. It has fields for the width and height of the display,
142 the information whether long lines are truncated or continued, a
143 current X and Y position, and lots of other stuff you can better
144 see in dispextern.h.
146 Glyphs in a desired matrix are normally constructed in a loop
147 calling get_next_display_element and then PRODUCE_GLYPHS. The call
148 to PRODUCE_GLYPHS will fill the iterator structure with pixel
149 information about the element being displayed and at the same time
150 produce glyphs for it. If the display element fits on the line
151 being displayed, set_iterator_to_next is called next, otherwise the
152 glyphs produced are discarded. The function display_line is the
153 workhorse of filling glyph rows in the desired matrix with glyphs.
154 In addition to producing glyphs, it also handles line truncation
155 and continuation, word wrap, and cursor positioning (for the
156 latter, see also set_cursor_from_row).
158 Frame matrices.
160 That just couldn't be all, could it? What about terminal types not
161 supporting operations on sub-windows of the screen? To update the
162 display on such a terminal, window-based glyph matrices are not
163 well suited. To be able to reuse part of the display (scrolling
164 lines up and down), we must instead have a view of the whole
165 screen. This is what `frame matrices' are for. They are a trick.
167 Frames on terminals like above have a glyph pool. Windows on such
168 a frame sub-allocate their glyph memory from their frame's glyph
169 pool. The frame itself is given its own glyph matrices. By
170 coincidence---or maybe something else---rows in window glyph
171 matrices are slices of corresponding rows in frame matrices. Thus
172 writing to window matrices implicitly updates a frame matrix which
173 provides us with the view of the whole screen that we originally
174 wanted to have without having to move many bytes around. To be
175 honest, there is a little bit more done, but not much more. If you
176 plan to extend that code, take a look at dispnew.c. The function
177 build_frame_matrix is a good starting point.
179 Bidirectional display.
181 Bidirectional display adds quite some hair to this already complex
182 design. The good news are that a large portion of that hairy stuff
183 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
184 reordering engine which is called by set_iterator_to_next and
185 returns the next character to display in the visual order. See
186 commentary on bidi.c for more details. As far as redisplay is
187 concerned, the effect of calling bidi_move_to_visually_next, the
188 main interface of the reordering engine, is that the iterator gets
189 magically placed on the buffer or string position that is to be
190 displayed next. In other words, a linear iteration through the
191 buffer/string is replaced with a non-linear one. All the rest of
192 the redisplay is oblivious to the bidi reordering.
194 Well, almost oblivious---there are still complications, most of
195 them due to the fact that buffer and string positions no longer
196 change monotonously with glyph indices in a glyph row. Moreover,
197 for continued lines, the buffer positions may not even be
198 monotonously changing with vertical positions. Also, accounting
199 for face changes, overlays, etc. becomes more complex because
200 non-linear iteration could potentially skip many positions with
201 changes, and then cross them again on the way back...
203 One other prominent effect of bidirectional display is that some
204 paragraphs of text need to be displayed starting at the right
205 margin of the window---the so-called right-to-left, or R2L
206 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
207 which have their reversed_p flag set. The bidi reordering engine
208 produces characters in such rows starting from the character which
209 should be the rightmost on display. PRODUCE_GLYPHS then reverses
210 the order, when it fills up the glyph row whose reversed_p flag is
211 set, by prepending each new glyph to what is already there, instead
212 of appending it. When the glyph row is complete, the function
213 extend_face_to_end_of_line fills the empty space to the left of the
214 leftmost character with special glyphs, which will display as,
215 well, empty. On text terminals, these special glyphs are simply
216 blank characters. On graphics terminals, there's a single stretch
217 glyph with suitably computed width. Both the blanks and the
218 stretch glyph are given the face of the background of the line.
219 This way, the terminal-specific back-end can still draw the glyphs
220 left to right, even for R2L lines. */
222 #include <config.h>
223 #include <stdio.h>
224 #include <limits.h>
225 #include <setjmp.h>
227 #include "lisp.h"
228 #include "keyboard.h"
229 #include "frame.h"
230 #include "window.h"
231 #include "termchar.h"
232 #include "dispextern.h"
233 #include "buffer.h"
234 #include "character.h"
235 #include "charset.h"
236 #include "indent.h"
237 #include "commands.h"
238 #include "keymap.h"
239 #include "macros.h"
240 #include "disptab.h"
241 #include "termhooks.h"
242 #include "intervals.h"
243 #include "coding.h"
244 #include "process.h"
245 #include "region-cache.h"
246 #include "font.h"
247 #include "fontset.h"
248 #include "blockinput.h"
250 #ifdef HAVE_X_WINDOWS
251 #include "xterm.h"
252 #endif
253 #ifdef WINDOWSNT
254 #include "w32term.h"
255 #endif
256 #ifdef HAVE_NS
257 #include "nsterm.h"
258 #endif
259 #ifdef USE_GTK
260 #include "gtkutil.h"
261 #endif
263 #include "font.h"
265 #ifndef FRAME_X_OUTPUT
266 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
267 #endif
269 #define INFINITY 10000000
271 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
272 || defined(HAVE_NS) || defined (USE_GTK)
273 extern void set_frame_menubar (struct frame *f, int, int);
274 #endif
276 extern int interrupt_input;
277 extern int command_loop_level;
279 extern Lisp_Object Vminibuffer_list;
281 extern Lisp_Object Qface;
282 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
284 extern Lisp_Object Qwhen;
285 extern Lisp_Object Qhelp_echo;
286 extern Lisp_Object Qbefore_string, Qafter_string;
288 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
289 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
290 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
291 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
292 Lisp_Object Qinhibit_point_motion_hooks;
293 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
294 Lisp_Object Qfontified;
295 Lisp_Object Qgrow_only;
296 Lisp_Object Qinhibit_eval_during_redisplay;
297 Lisp_Object Qbuffer_position, Qposition, Qobject;
298 Lisp_Object Qright_to_left, Qleft_to_right;
300 /* Cursor shapes */
301 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
303 /* Pointer shapes */
304 Lisp_Object Qarrow, Qhand, Qtext;
306 Lisp_Object Qrisky_local_variable;
308 /* Holds the list (error). */
309 Lisp_Object list_of_error;
311 /* Functions called to fontify regions of text. */
313 Lisp_Object Vfontification_functions;
314 Lisp_Object Qfontification_functions;
316 /* Non-nil means automatically select any window when the mouse
317 cursor moves into it. */
318 Lisp_Object Vmouse_autoselect_window;
320 Lisp_Object Vwrap_prefix, Qwrap_prefix;
321 Lisp_Object Vline_prefix, Qline_prefix;
323 /* Non-zero means draw tool bar buttons raised when the mouse moves
324 over them. */
326 int auto_raise_tool_bar_buttons_p;
328 /* Non-zero means to reposition window if cursor line is only partially visible. */
330 int make_cursor_line_fully_visible_p;
332 /* Margin below tool bar in pixels. 0 or nil means no margin.
333 If value is `internal-border-width' or `border-width',
334 the corresponding frame parameter is used. */
336 Lisp_Object Vtool_bar_border;
338 /* Margin around tool bar buttons in pixels. */
340 Lisp_Object Vtool_bar_button_margin;
342 /* Thickness of shadow to draw around tool bar buttons. */
344 EMACS_INT tool_bar_button_relief;
346 /* Non-nil means automatically resize tool-bars so that all tool-bar
347 items are visible, and no blank lines remain.
349 If value is `grow-only', only make tool-bar bigger. */
351 Lisp_Object Vauto_resize_tool_bars;
353 /* Type of tool bar. Can be symbols image, text, both or both-hroiz. */
355 Lisp_Object Vtool_bar_style;
357 /* Maximum number of characters a label can have to be shown. */
359 EMACS_INT tool_bar_max_label_size;
361 /* Non-zero means draw block and hollow cursor as wide as the glyph
362 under it. For example, if a block cursor is over a tab, it will be
363 drawn as wide as that tab on the display. */
365 int x_stretch_cursor_p;
367 /* Non-nil means don't actually do any redisplay. */
369 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
371 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
373 int inhibit_eval_during_redisplay;
375 /* Names of text properties relevant for redisplay. */
377 Lisp_Object Qdisplay;
379 /* Symbols used in text property values. */
381 Lisp_Object Vdisplay_pixels_per_inch;
382 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
383 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
384 Lisp_Object Qslice;
385 Lisp_Object Qcenter;
386 Lisp_Object Qmargin, Qpointer;
387 Lisp_Object Qline_height;
388 extern Lisp_Object Qheight;
389 extern Lisp_Object QCwidth, QCheight, QCascent;
390 extern Lisp_Object Qscroll_bar;
391 extern Lisp_Object Qcursor;
393 /* Non-nil means highlight trailing whitespace. */
395 Lisp_Object Vshow_trailing_whitespace;
397 /* Non-nil means escape non-break space and hyphens. */
399 Lisp_Object Vnobreak_char_display;
401 #ifdef HAVE_WINDOW_SYSTEM
402 extern Lisp_Object Voverflow_newline_into_fringe;
404 /* Test if overflow newline into fringe. Called with iterator IT
405 at or past right window margin, and with IT->current_x set. */
407 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
408 (!NILP (Voverflow_newline_into_fringe) \
409 && FRAME_WINDOW_P ((IT)->f) \
410 && ((IT)->bidi_it.paragraph_dir == R2L \
411 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
412 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
413 && (IT)->current_x == (IT)->last_visible_x \
414 && (IT)->line_wrap != WORD_WRAP)
416 #else /* !HAVE_WINDOW_SYSTEM */
417 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
418 #endif /* HAVE_WINDOW_SYSTEM */
420 /* Test if the display element loaded in IT is a space or tab
421 character. This is used to determine word wrapping. */
423 #define IT_DISPLAYING_WHITESPACE(it) \
424 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
426 /* Non-nil means show the text cursor in void text areas
427 i.e. in blank areas after eol and eob. This used to be
428 the default in 21.3. */
430 Lisp_Object Vvoid_text_area_pointer;
432 /* Name of the face used to highlight trailing whitespace. */
434 Lisp_Object Qtrailing_whitespace;
436 /* Name and number of the face used to highlight escape glyphs. */
438 Lisp_Object Qescape_glyph;
440 /* Name and number of the face used to highlight non-breaking spaces. */
442 Lisp_Object Qnobreak_space;
444 /* The symbol `image' which is the car of the lists used to represent
445 images in Lisp. Also a tool bar style. */
447 Lisp_Object Qimage;
449 /* The image map types. */
450 Lisp_Object QCmap, QCpointer;
451 Lisp_Object Qrect, Qcircle, Qpoly;
453 /* Tool bar styles */
454 Lisp_Object Qtext, Qboth, Qboth_horiz, Qtext_image_horiz;
456 /* Non-zero means print newline to stdout before next mini-buffer
457 message. */
459 int noninteractive_need_newline;
461 /* Non-zero means print newline to message log before next message. */
463 static int message_log_need_newline;
465 /* Three markers that message_dolog uses.
466 It could allocate them itself, but that causes trouble
467 in handling memory-full errors. */
468 static Lisp_Object message_dolog_marker1;
469 static Lisp_Object message_dolog_marker2;
470 static Lisp_Object message_dolog_marker3;
472 /* The buffer position of the first character appearing entirely or
473 partially on the line of the selected window which contains the
474 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
475 redisplay optimization in redisplay_internal. */
477 static struct text_pos this_line_start_pos;
479 /* Number of characters past the end of the line above, including the
480 terminating newline. */
482 static struct text_pos this_line_end_pos;
484 /* The vertical positions and the height of this line. */
486 static int this_line_vpos;
487 static int this_line_y;
488 static int this_line_pixel_height;
490 /* X position at which this display line starts. Usually zero;
491 negative if first character is partially visible. */
493 static int this_line_start_x;
495 /* Buffer that this_line_.* variables are referring to. */
497 static struct buffer *this_line_buffer;
499 /* Nonzero means truncate lines in all windows less wide than the
500 frame. */
502 Lisp_Object Vtruncate_partial_width_windows;
504 /* A flag to control how to display unibyte 8-bit character. */
506 int unibyte_display_via_language_environment;
508 /* Nonzero means we have more than one non-mini-buffer-only frame.
509 Not guaranteed to be accurate except while parsing
510 frame-title-format. */
512 int multiple_frames;
514 Lisp_Object Vglobal_mode_string;
517 /* List of variables (symbols) which hold markers for overlay arrows.
518 The symbols on this list are examined during redisplay to determine
519 where to display overlay arrows. */
521 Lisp_Object Voverlay_arrow_variable_list;
523 /* Marker for where to display an arrow on top of the buffer text. */
525 Lisp_Object Voverlay_arrow_position;
527 /* String to display for the arrow. Only used on terminal frames. */
529 Lisp_Object Voverlay_arrow_string;
531 /* Values of those variables at last redisplay are stored as
532 properties on `overlay-arrow-position' symbol. However, if
533 Voverlay_arrow_position is a marker, last-arrow-position is its
534 numerical position. */
536 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
538 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
539 properties on a symbol in overlay-arrow-variable-list. */
541 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
543 /* Like mode-line-format, but for the title bar on a visible frame. */
545 Lisp_Object Vframe_title_format;
547 /* Like mode-line-format, but for the title bar on an iconified frame. */
549 Lisp_Object Vicon_title_format;
551 /* List of functions to call when a window's size changes. These
552 functions get one arg, a frame on which one or more windows' sizes
553 have changed. */
555 static Lisp_Object Vwindow_size_change_functions;
557 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
559 /* Nonzero if an overlay arrow has been displayed in this window. */
561 static int overlay_arrow_seen;
563 /* Nonzero means highlight the region even in nonselected windows. */
565 int highlight_nonselected_windows;
567 /* If cursor motion alone moves point off frame, try scrolling this
568 many lines up or down if that will bring it back. */
570 static EMACS_INT scroll_step;
572 /* Nonzero means scroll just far enough to bring point back on the
573 screen, when appropriate. */
575 static EMACS_INT scroll_conservatively;
577 /* Recenter the window whenever point gets within this many lines of
578 the top or bottom of the window. This value is translated into a
579 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
580 that there is really a fixed pixel height scroll margin. */
582 EMACS_INT scroll_margin;
584 /* Number of windows showing the buffer of the selected window (or
585 another buffer with the same base buffer). keyboard.c refers to
586 this. */
588 int buffer_shared;
590 /* Vector containing glyphs for an ellipsis `...'. */
592 static Lisp_Object default_invis_vector[3];
594 /* Zero means display the mode-line/header-line/menu-bar in the default face
595 (this slightly odd definition is for compatibility with previous versions
596 of emacs), non-zero means display them using their respective faces.
598 This variable is deprecated. */
600 int mode_line_inverse_video;
602 /* Prompt to display in front of the mini-buffer contents. */
604 Lisp_Object minibuf_prompt;
606 /* Width of current mini-buffer prompt. Only set after display_line
607 of the line that contains the prompt. */
609 int minibuf_prompt_width;
611 /* This is the window where the echo area message was displayed. It
612 is always a mini-buffer window, but it may not be the same window
613 currently active as a mini-buffer. */
615 Lisp_Object echo_area_window;
617 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
618 pushes the current message and the value of
619 message_enable_multibyte on the stack, the function restore_message
620 pops the stack and displays MESSAGE again. */
622 Lisp_Object Vmessage_stack;
624 /* Nonzero means multibyte characters were enabled when the echo area
625 message was specified. */
627 int message_enable_multibyte;
629 /* Nonzero if we should redraw the mode lines on the next redisplay. */
631 int update_mode_lines;
633 /* Nonzero if window sizes or contents have changed since last
634 redisplay that finished. */
636 int windows_or_buffers_changed;
638 /* Nonzero means a frame's cursor type has been changed. */
640 int cursor_type_changed;
642 /* Nonzero after display_mode_line if %l was used and it displayed a
643 line number. */
645 int line_number_displayed;
647 /* Maximum buffer size for which to display line numbers. */
649 Lisp_Object Vline_number_display_limit;
651 /* Line width to consider when repositioning for line number display. */
653 static EMACS_INT line_number_display_limit_width;
655 /* Number of lines to keep in the message log buffer. t means
656 infinite. nil means don't log at all. */
658 Lisp_Object Vmessage_log_max;
660 /* The name of the *Messages* buffer, a string. */
662 static Lisp_Object Vmessages_buffer_name;
664 /* Current, index 0, and last displayed echo area message. Either
665 buffers from echo_buffers, or nil to indicate no message. */
667 Lisp_Object echo_area_buffer[2];
669 /* The buffers referenced from echo_area_buffer. */
671 static Lisp_Object echo_buffer[2];
673 /* A vector saved used in with_area_buffer to reduce consing. */
675 static Lisp_Object Vwith_echo_area_save_vector;
677 /* Non-zero means display_echo_area should display the last echo area
678 message again. Set by redisplay_preserve_echo_area. */
680 static int display_last_displayed_message_p;
682 /* Nonzero if echo area is being used by print; zero if being used by
683 message. */
685 int message_buf_print;
687 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
689 Lisp_Object Qinhibit_menubar_update;
690 int inhibit_menubar_update;
692 /* When evaluating expressions from menu bar items (enable conditions,
693 for instance), this is the frame they are being processed for. */
695 Lisp_Object Vmenu_updating_frame;
697 /* Maximum height for resizing mini-windows. Either a float
698 specifying a fraction of the available height, or an integer
699 specifying a number of lines. */
701 Lisp_Object Vmax_mini_window_height;
703 /* Non-zero means messages should be displayed with truncated
704 lines instead of being continued. */
706 int message_truncate_lines;
707 Lisp_Object Qmessage_truncate_lines;
709 /* Set to 1 in clear_message to make redisplay_internal aware
710 of an emptied echo area. */
712 static int message_cleared_p;
714 /* How to blink the default frame cursor off. */
715 Lisp_Object Vblink_cursor_alist;
717 /* A scratch glyph row with contents used for generating truncation
718 glyphs. Also used in direct_output_for_insert. */
720 #define MAX_SCRATCH_GLYPHS 100
721 struct glyph_row scratch_glyph_row;
722 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
724 /* Ascent and height of the last line processed by move_it_to. */
726 static int last_max_ascent, last_height;
728 /* Non-zero if there's a help-echo in the echo area. */
730 int help_echo_showing_p;
732 /* If >= 0, computed, exact values of mode-line and header-line height
733 to use in the macros CURRENT_MODE_LINE_HEIGHT and
734 CURRENT_HEADER_LINE_HEIGHT. */
736 int current_mode_line_height, current_header_line_height;
738 /* The maximum distance to look ahead for text properties. Values
739 that are too small let us call compute_char_face and similar
740 functions too often which is expensive. Values that are too large
741 let us call compute_char_face and alike too often because we
742 might not be interested in text properties that far away. */
744 #define TEXT_PROP_DISTANCE_LIMIT 100
746 #if GLYPH_DEBUG
748 /* Variables to turn off display optimizations from Lisp. */
750 int inhibit_try_window_id, inhibit_try_window_reusing;
751 int inhibit_try_cursor_movement;
753 /* Non-zero means print traces of redisplay if compiled with
754 GLYPH_DEBUG != 0. */
756 int trace_redisplay_p;
758 #endif /* GLYPH_DEBUG */
760 #ifdef DEBUG_TRACE_MOVE
761 /* Non-zero means trace with TRACE_MOVE to stderr. */
762 int trace_move;
764 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
765 #else
766 #define TRACE_MOVE(x) (void) 0
767 #endif
769 /* Non-zero means automatically scroll windows horizontally to make
770 point visible. */
772 int automatic_hscrolling_p;
773 Lisp_Object Qauto_hscroll_mode;
775 /* How close to the margin can point get before the window is scrolled
776 horizontally. */
777 EMACS_INT hscroll_margin;
779 /* How much to scroll horizontally when point is inside the above margin. */
780 Lisp_Object Vhscroll_step;
782 /* The variable `resize-mini-windows'. If nil, don't resize
783 mini-windows. If t, always resize them to fit the text they
784 display. If `grow-only', let mini-windows grow only until they
785 become empty. */
787 Lisp_Object Vresize_mini_windows;
789 /* Buffer being redisplayed -- for redisplay_window_error. */
791 struct buffer *displayed_buffer;
793 /* Space between overline and text. */
795 EMACS_INT overline_margin;
797 /* Require underline to be at least this many screen pixels below baseline
798 This to avoid underline "merging" with the base of letters at small
799 font sizes, particularly when x_use_underline_position_properties is on. */
801 EMACS_INT underline_minimum_offset;
803 /* Value returned from text property handlers (see below). */
805 enum prop_handled
807 HANDLED_NORMALLY,
808 HANDLED_RECOMPUTE_PROPS,
809 HANDLED_OVERLAY_STRING_CONSUMED,
810 HANDLED_RETURN
813 /* A description of text properties that redisplay is interested
814 in. */
816 struct props
818 /* The name of the property. */
819 Lisp_Object *name;
821 /* A unique index for the property. */
822 enum prop_idx idx;
824 /* A handler function called to set up iterator IT from the property
825 at IT's current position. Value is used to steer handle_stop. */
826 enum prop_handled (*handler) (struct it *it);
829 static enum prop_handled handle_face_prop (struct it *);
830 static enum prop_handled handle_invisible_prop (struct it *);
831 static enum prop_handled handle_display_prop (struct it *);
832 static enum prop_handled handle_composition_prop (struct it *);
833 static enum prop_handled handle_overlay_change (struct it *);
834 static enum prop_handled handle_fontified_prop (struct it *);
836 /* Properties handled by iterators. */
838 static struct props it_props[] =
840 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
841 /* Handle `face' before `display' because some sub-properties of
842 `display' need to know the face. */
843 {&Qface, FACE_PROP_IDX, handle_face_prop},
844 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
845 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
846 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
847 {NULL, 0, NULL}
850 /* Value is the position described by X. If X is a marker, value is
851 the marker_position of X. Otherwise, value is X. */
853 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
855 /* Enumeration returned by some move_it_.* functions internally. */
857 enum move_it_result
859 /* Not used. Undefined value. */
860 MOVE_UNDEFINED,
862 /* Move ended at the requested buffer position or ZV. */
863 MOVE_POS_MATCH_OR_ZV,
865 /* Move ended at the requested X pixel position. */
866 MOVE_X_REACHED,
868 /* Move within a line ended at the end of a line that must be
869 continued. */
870 MOVE_LINE_CONTINUED,
872 /* Move within a line ended at the end of a line that would
873 be displayed truncated. */
874 MOVE_LINE_TRUNCATED,
876 /* Move within a line ended at a line end. */
877 MOVE_NEWLINE_OR_CR
880 /* This counter is used to clear the face cache every once in a while
881 in redisplay_internal. It is incremented for each redisplay.
882 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
883 cleared. */
885 #define CLEAR_FACE_CACHE_COUNT 500
886 static int clear_face_cache_count;
888 /* Similarly for the image cache. */
890 #ifdef HAVE_WINDOW_SYSTEM
891 #define CLEAR_IMAGE_CACHE_COUNT 101
892 static int clear_image_cache_count;
893 #endif
895 /* Non-zero while redisplay_internal is in progress. */
897 int redisplaying_p;
899 /* Non-zero means don't free realized faces. Bound while freeing
900 realized faces is dangerous because glyph matrices might still
901 reference them. */
903 int inhibit_free_realized_faces;
904 Lisp_Object Qinhibit_free_realized_faces;
906 /* If a string, XTread_socket generates an event to display that string.
907 (The display is done in read_char.) */
909 Lisp_Object help_echo_string;
910 Lisp_Object help_echo_window;
911 Lisp_Object help_echo_object;
912 int help_echo_pos;
914 /* Temporary variable for XTread_socket. */
916 Lisp_Object previous_help_echo_string;
918 /* Null glyph slice */
920 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
922 /* Platform-independent portion of hourglass implementation. */
924 /* Non-zero means we're allowed to display a hourglass pointer. */
925 int display_hourglass_p;
927 /* Non-zero means an hourglass cursor is currently shown. */
928 int hourglass_shown_p;
930 /* If non-null, an asynchronous timer that, when it expires, displays
931 an hourglass cursor on all frames. */
932 struct atimer *hourglass_atimer;
934 /* Number of seconds to wait before displaying an hourglass cursor. */
935 Lisp_Object Vhourglass_delay;
937 /* Default number of seconds to wait before displaying an hourglass
938 cursor. */
939 #define DEFAULT_HOURGLASS_DELAY 1
942 /* Function prototypes. */
944 static void setup_for_ellipsis (struct it *, int);
945 static void mark_window_display_accurate_1 (struct window *, int);
946 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
947 static int display_prop_string_p (Lisp_Object, Lisp_Object);
948 static int cursor_row_p (struct window *, struct glyph_row *);
949 static int redisplay_mode_lines (Lisp_Object, int);
950 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
952 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
954 static void handle_line_prefix (struct it *);
956 static void pint2str (char *, int, int);
957 static void pint2hrstr (char *, int, int);
958 static struct text_pos run_window_scroll_functions (Lisp_Object,
959 struct text_pos);
960 static void reconsider_clip_changes (struct window *, struct buffer *);
961 static int text_outside_line_unchanged_p (struct window *, int, int);
962 static void store_mode_line_noprop_char (char);
963 static int store_mode_line_noprop (const unsigned char *, int, int);
964 static void x_consider_frame_title (Lisp_Object);
965 static void handle_stop (struct it *);
966 static void handle_stop_backwards (struct it *, EMACS_INT);
967 static int tool_bar_lines_needed (struct frame *, int *);
968 static int single_display_spec_intangible_p (Lisp_Object);
969 static void ensure_echo_area_buffers (void);
970 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
971 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
972 static int with_echo_area_buffer (struct window *, int,
973 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
974 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
975 static void clear_garbaged_frames (void);
976 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
977 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
978 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
979 static int display_echo_area (struct window *);
980 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
981 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
982 static Lisp_Object unwind_redisplay (Lisp_Object);
983 static int string_char_and_length (const unsigned char *, int *);
984 static struct text_pos display_prop_end (struct it *, Lisp_Object,
985 struct text_pos);
986 static int compute_window_start_on_continuation_line (struct window *);
987 static Lisp_Object safe_eval_handler (Lisp_Object);
988 static void insert_left_trunc_glyphs (struct it *);
989 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
990 Lisp_Object);
991 static void extend_face_to_end_of_line (struct it *);
992 static int append_space_for_newline (struct it *, int);
993 static int cursor_row_fully_visible_p (struct window *, int, int);
994 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
995 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
996 static int trailing_whitespace_p (int);
997 static int message_log_check_duplicate (int, int, int, 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 (char *, Lisp_Object, int, int, int, Lisp_Object);
1017 static 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 (int, int, int, int, int *);
1021 static int display_string (unsigned char *, Lisp_Object, Lisp_Object,
1022 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
1023 static void compute_line_metrics (struct it *);
1024 static void run_redisplay_end_trigger_hook (struct it *);
1025 static int get_overlay_strings (struct it *, int);
1026 static int get_overlay_strings_1 (struct it *, int, int);
1027 static void next_overlay_string (struct it *);
1028 static void reseat (struct it *, struct text_pos, int);
1029 static void reseat_1 (struct it *, struct text_pos, int);
1030 static void back_to_previous_visible_line_start (struct it *);
1031 void reseat_at_previous_visible_line_start (struct it *);
1032 static void reseat_at_next_visible_line_start (struct it *, int);
1033 static int next_element_from_ellipsis (struct it *);
1034 static int next_element_from_display_vector (struct it *);
1035 static int next_element_from_string (struct it *);
1036 static int next_element_from_c_string (struct it *);
1037 static int next_element_from_buffer (struct it *);
1038 static int next_element_from_composition (struct it *);
1039 static int next_element_from_image (struct it *);
1040 static int next_element_from_stretch (struct it *);
1041 static void load_overlay_strings (struct it *, int);
1042 static int init_from_display_pos (struct it *, struct window *,
1043 struct display_pos *);
1044 static void reseat_to_string (struct it *, unsigned char *,
1045 Lisp_Object, int, int, int, int);
1046 static enum move_it_result
1047 move_it_in_display_line_to (struct it *, EMACS_INT, int,
1048 enum move_operation_enum);
1049 void move_it_vertically_backward (struct it *, int);
1050 static void init_to_row_start (struct it *, struct window *,
1051 struct glyph_row *);
1052 static int init_to_row_end (struct it *, struct window *,
1053 struct glyph_row *);
1054 static void back_to_previous_line_start (struct it *);
1055 static int forward_to_next_line_start (struct it *, int *);
1056 static struct text_pos string_pos_nchars_ahead (struct text_pos,
1057 Lisp_Object, int);
1058 static struct text_pos string_pos (int, Lisp_Object);
1059 static struct text_pos c_string_pos (int, unsigned char *, int);
1060 static int number_of_chars (unsigned char *, int);
1061 static void compute_stop_pos (struct it *);
1062 static void compute_string_pos (struct text_pos *, struct text_pos,
1063 Lisp_Object);
1064 static int face_before_or_after_it_pos (struct it *, int);
1065 static EMACS_INT next_overlay_change (EMACS_INT);
1066 static int handle_single_display_spec (struct it *, Lisp_Object,
1067 Lisp_Object, Lisp_Object,
1068 struct text_pos *, int);
1069 static int underlying_face_id (struct it *);
1070 static int in_ellipses_for_invisible_text_p (struct display_pos *,
1071 struct window *);
1073 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1074 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1076 #ifdef HAVE_WINDOW_SYSTEM
1078 static void update_tool_bar (struct frame *, int);
1079 static void build_desired_tool_bar_string (struct frame *f);
1080 static int redisplay_tool_bar (struct frame *);
1081 static void display_tool_bar_line (struct it *, int);
1082 static void notice_overwritten_cursor (struct window *,
1083 enum glyph_row_area,
1084 int, int, int, int);
1085 static void append_stretch_glyph (struct it *, Lisp_Object,
1086 int, int, int);
1090 #endif /* HAVE_WINDOW_SYSTEM */
1093 /***********************************************************************
1094 Window display dimensions
1095 ***********************************************************************/
1097 /* Return the bottom boundary y-position for text lines in window W.
1098 This is the first y position at which a line cannot start.
1099 It is relative to the top of the window.
1101 This is the height of W minus the height of a mode line, if any. */
1103 INLINE int
1104 window_text_bottom_y (struct window *w)
1106 int height = WINDOW_TOTAL_HEIGHT (w);
1108 if (WINDOW_WANTS_MODELINE_P (w))
1109 height -= CURRENT_MODE_LINE_HEIGHT (w);
1110 return height;
1113 /* Return the pixel width of display area AREA of window W. AREA < 0
1114 means return the total width of W, not including fringes to
1115 the left and right of the window. */
1117 INLINE int
1118 window_box_width (struct window *w, int area)
1120 int cols = XFASTINT (w->total_cols);
1121 int pixels = 0;
1123 if (!w->pseudo_window_p)
1125 cols -= WINDOW_SCROLL_BAR_COLS (w);
1127 if (area == TEXT_AREA)
1129 if (INTEGERP (w->left_margin_cols))
1130 cols -= XFASTINT (w->left_margin_cols);
1131 if (INTEGERP (w->right_margin_cols))
1132 cols -= XFASTINT (w->right_margin_cols);
1133 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1135 else if (area == LEFT_MARGIN_AREA)
1137 cols = (INTEGERP (w->left_margin_cols)
1138 ? XFASTINT (w->left_margin_cols) : 0);
1139 pixels = 0;
1141 else if (area == RIGHT_MARGIN_AREA)
1143 cols = (INTEGERP (w->right_margin_cols)
1144 ? XFASTINT (w->right_margin_cols) : 0);
1145 pixels = 0;
1149 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1153 /* Return the pixel height of the display area of window W, not
1154 including mode lines of W, if any. */
1156 INLINE int
1157 window_box_height (struct window *w)
1159 struct frame *f = XFRAME (w->frame);
1160 int height = WINDOW_TOTAL_HEIGHT (w);
1162 xassert (height >= 0);
1164 /* Note: the code below that determines the mode-line/header-line
1165 height is essentially the same as that contained in the macro
1166 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1167 the appropriate glyph row has its `mode_line_p' flag set,
1168 and if it doesn't, uses estimate_mode_line_height instead. */
1170 if (WINDOW_WANTS_MODELINE_P (w))
1172 struct glyph_row *ml_row
1173 = (w->current_matrix && w->current_matrix->rows
1174 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1175 : 0);
1176 if (ml_row && ml_row->mode_line_p)
1177 height -= ml_row->height;
1178 else
1179 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1184 struct glyph_row *hl_row
1185 = (w->current_matrix && w->current_matrix->rows
1186 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1187 : 0);
1188 if (hl_row && hl_row->mode_line_p)
1189 height -= hl_row->height;
1190 else
1191 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1194 /* With a very small font and a mode-line that's taller than
1195 default, we might end up with a negative height. */
1196 return max (0, height);
1199 /* Return the window-relative coordinate of the left edge of display
1200 area AREA of window W. AREA < 0 means return the left edge of the
1201 whole window, to the right of the left fringe of W. */
1203 INLINE int
1204 window_box_left_offset (struct window *w, int area)
1206 int x;
1208 if (w->pseudo_window_p)
1209 return 0;
1211 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1213 if (area == TEXT_AREA)
1214 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1215 + window_box_width (w, LEFT_MARGIN_AREA));
1216 else if (area == RIGHT_MARGIN_AREA)
1217 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1218 + window_box_width (w, LEFT_MARGIN_AREA)
1219 + window_box_width (w, TEXT_AREA)
1220 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1222 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1223 else if (area == LEFT_MARGIN_AREA
1224 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1225 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1227 return x;
1231 /* Return the window-relative coordinate of the right edge of display
1232 area AREA of window W. AREA < 0 means return the left edge of the
1233 whole window, to the left of the right fringe of W. */
1235 INLINE int
1236 window_box_right_offset (struct window *w, int area)
1238 return window_box_left_offset (w, area) + window_box_width (w, area);
1241 /* Return the frame-relative coordinate of the left edge of display
1242 area AREA of window W. AREA < 0 means return the left edge of the
1243 whole window, to the right of the left fringe of W. */
1245 INLINE int
1246 window_box_left (struct window *w, int area)
1248 struct frame *f = XFRAME (w->frame);
1249 int x;
1251 if (w->pseudo_window_p)
1252 return FRAME_INTERNAL_BORDER_WIDTH (f);
1254 x = (WINDOW_LEFT_EDGE_X (w)
1255 + window_box_left_offset (w, area));
1257 return x;
1261 /* Return the frame-relative coordinate of the right edge of display
1262 area AREA of window W. AREA < 0 means return the left edge of the
1263 whole window, to the left of the right fringe of W. */
1265 INLINE int
1266 window_box_right (struct window *w, int area)
1268 return window_box_left (w, area) + window_box_width (w, area);
1271 /* Get the bounding box of the display area AREA of window W, without
1272 mode lines, in frame-relative coordinates. AREA < 0 means the
1273 whole window, not including the left and right fringes of
1274 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1275 coordinates of the upper-left corner of the box. Return in
1276 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1278 INLINE void
1279 window_box (struct window *w, int area, int *box_x, int *box_y,
1280 int *box_width, int *box_height)
1282 if (box_width)
1283 *box_width = window_box_width (w, area);
1284 if (box_height)
1285 *box_height = window_box_height (w);
1286 if (box_x)
1287 *box_x = window_box_left (w, area);
1288 if (box_y)
1290 *box_y = WINDOW_TOP_EDGE_Y (w);
1291 if (WINDOW_WANTS_HEADER_LINE_P (w))
1292 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1297 /* Get the bounding box of the display area AREA of window W, without
1298 mode lines. AREA < 0 means the whole window, not including the
1299 left and right fringe of the window. Return in *TOP_LEFT_X
1300 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1301 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1302 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1303 box. */
1305 INLINE void
1306 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1307 int *bottom_right_x, int *bottom_right_y)
1309 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1310 bottom_right_y);
1311 *bottom_right_x += *top_left_x;
1312 *bottom_right_y += *top_left_y;
1317 /***********************************************************************
1318 Utilities
1319 ***********************************************************************/
1321 /* Return the bottom y-position of the line the iterator IT is in.
1322 This can modify IT's settings. */
1325 line_bottom_y (struct it *it)
1327 int line_height = it->max_ascent + it->max_descent;
1328 int line_top_y = it->current_y;
1330 if (line_height == 0)
1332 if (last_height)
1333 line_height = last_height;
1334 else if (IT_CHARPOS (*it) < ZV)
1336 move_it_by_lines (it, 1, 1);
1337 line_height = (it->max_ascent || it->max_descent
1338 ? it->max_ascent + it->max_descent
1339 : last_height);
1341 else
1343 struct glyph_row *row = it->glyph_row;
1345 /* Use the default character height. */
1346 it->glyph_row = NULL;
1347 it->what = IT_CHARACTER;
1348 it->c = ' ';
1349 it->len = 1;
1350 PRODUCE_GLYPHS (it);
1351 line_height = it->ascent + it->descent;
1352 it->glyph_row = row;
1356 return line_top_y + line_height;
1360 /* Return 1 if position CHARPOS is visible in window W.
1361 CHARPOS < 0 means return info about WINDOW_END position.
1362 If visible, set *X and *Y to pixel coordinates of top left corner.
1363 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1364 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1367 pos_visible_p (struct window *w, int charpos, int *x, int *y,
1368 int *rtop, int *rbot, int *rowh, int *vpos)
1370 struct it it;
1371 struct text_pos top;
1372 int visible_p = 0;
1373 struct buffer *old_buffer = NULL;
1375 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1376 return visible_p;
1378 if (XBUFFER (w->buffer) != current_buffer)
1380 old_buffer = current_buffer;
1381 set_buffer_internal_1 (XBUFFER (w->buffer));
1384 SET_TEXT_POS_FROM_MARKER (top, w->start);
1386 /* Compute exact mode line heights. */
1387 if (WINDOW_WANTS_MODELINE_P (w))
1388 current_mode_line_height
1389 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1390 current_buffer->mode_line_format);
1392 if (WINDOW_WANTS_HEADER_LINE_P (w))
1393 current_header_line_height
1394 = display_mode_line (w, HEADER_LINE_FACE_ID,
1395 current_buffer->header_line_format);
1397 start_display (&it, w, top);
1398 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1399 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1401 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1403 /* We have reached CHARPOS, or passed it. How the call to
1404 move_it_to can overshoot: (i) If CHARPOS is on invisible
1405 text, move_it_to stops at the end of the invisible text,
1406 after CHARPOS. (ii) If CHARPOS is in a display vector,
1407 move_it_to stops on its last glyph. */
1408 int top_x = it.current_x;
1409 int top_y = it.current_y;
1410 enum it_method it_method = it.method;
1411 /* Calling line_bottom_y may change it.method, it.position, etc. */
1412 int bottom_y = (last_height = 0, line_bottom_y (&it));
1413 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1415 if (top_y < window_top_y)
1416 visible_p = bottom_y > window_top_y;
1417 else if (top_y < it.last_visible_y)
1418 visible_p = 1;
1419 if (visible_p)
1421 if (it_method == GET_FROM_DISPLAY_VECTOR)
1423 /* We stopped on the last glyph of a display vector.
1424 Try and recompute. Hack alert! */
1425 if (charpos < 2 || top.charpos >= charpos)
1426 top_x = it.glyph_row->x;
1427 else
1429 struct it it2;
1430 start_display (&it2, w, top);
1431 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1432 get_next_display_element (&it2);
1433 PRODUCE_GLYPHS (&it2);
1434 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1435 || it2.current_x > it2.last_visible_x)
1436 top_x = it.glyph_row->x;
1437 else
1439 top_x = it2.current_x;
1440 top_y = it2.current_y;
1445 *x = top_x;
1446 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1447 *rtop = max (0, window_top_y - top_y);
1448 *rbot = max (0, bottom_y - it.last_visible_y);
1449 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1450 - max (top_y, window_top_y)));
1451 *vpos = it.vpos;
1454 else
1456 struct it it2;
1458 it2 = it;
1459 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1460 move_it_by_lines (&it, 1, 0);
1461 if (charpos < IT_CHARPOS (it)
1462 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1464 visible_p = 1;
1465 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1466 *x = it2.current_x;
1467 *y = it2.current_y + it2.max_ascent - it2.ascent;
1468 *rtop = max (0, -it2.current_y);
1469 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1470 - it.last_visible_y));
1471 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1472 it.last_visible_y)
1473 - max (it2.current_y,
1474 WINDOW_HEADER_LINE_HEIGHT (w))));
1475 *vpos = it2.vpos;
1479 if (old_buffer)
1480 set_buffer_internal_1 (old_buffer);
1482 current_header_line_height = current_mode_line_height = -1;
1484 if (visible_p && XFASTINT (w->hscroll) > 0)
1485 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1487 #if 0
1488 /* Debugging code. */
1489 if (visible_p)
1490 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1491 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1492 else
1493 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1494 #endif
1496 return visible_p;
1500 /* Return the next character from STR which is MAXLEN bytes long.
1501 Return in *LEN the length of the character. This is like
1502 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1503 we find one, we return a `?', but with the length of the invalid
1504 character. */
1506 static INLINE int
1507 string_char_and_length (const unsigned char *str, int *len)
1509 int c;
1511 c = STRING_CHAR_AND_LENGTH (str, *len);
1512 if (!CHAR_VALID_P (c, 1))
1513 /* We may not change the length here because other places in Emacs
1514 don't use this function, i.e. they silently accept invalid
1515 characters. */
1516 c = '?';
1518 return c;
1523 /* Given a position POS containing a valid character and byte position
1524 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1526 static struct text_pos
1527 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, int nchars)
1529 xassert (STRINGP (string) && nchars >= 0);
1531 if (STRING_MULTIBYTE (string))
1533 int rest = SBYTES (string) - BYTEPOS (pos);
1534 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1535 int len;
1537 while (nchars--)
1539 string_char_and_length (p, &len);
1540 p += len, rest -= len;
1541 xassert (rest >= 0);
1542 CHARPOS (pos) += 1;
1543 BYTEPOS (pos) += len;
1546 else
1547 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1549 return pos;
1553 /* Value is the text position, i.e. character and byte position,
1554 for character position CHARPOS in STRING. */
1556 static INLINE struct text_pos
1557 string_pos (int charpos, Lisp_Object string)
1559 struct text_pos pos;
1560 xassert (STRINGP (string));
1561 xassert (charpos >= 0);
1562 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1563 return pos;
1567 /* Value is a text position, i.e. character and byte position, for
1568 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1569 means recognize multibyte characters. */
1571 static struct text_pos
1572 c_string_pos (int charpos, unsigned char *s, int multibyte_p)
1574 struct text_pos pos;
1576 xassert (s != NULL);
1577 xassert (charpos >= 0);
1579 if (multibyte_p)
1581 int rest = strlen (s), len;
1583 SET_TEXT_POS (pos, 0, 0);
1584 while (charpos--)
1586 string_char_and_length (s, &len);
1587 s += len, rest -= len;
1588 xassert (rest >= 0);
1589 CHARPOS (pos) += 1;
1590 BYTEPOS (pos) += len;
1593 else
1594 SET_TEXT_POS (pos, charpos, charpos);
1596 return pos;
1600 /* Value is the number of characters in C string S. MULTIBYTE_P
1601 non-zero means recognize multibyte characters. */
1603 static int
1604 number_of_chars (unsigned char *s, int multibyte_p)
1606 int nchars;
1608 if (multibyte_p)
1610 int rest = strlen (s), len;
1611 unsigned char *p = (unsigned char *) s;
1613 for (nchars = 0; rest > 0; ++nchars)
1615 string_char_and_length (p, &len);
1616 rest -= len, p += len;
1619 else
1620 nchars = strlen (s);
1622 return nchars;
1626 /* Compute byte position NEWPOS->bytepos corresponding to
1627 NEWPOS->charpos. POS is a known position in string STRING.
1628 NEWPOS->charpos must be >= POS.charpos. */
1630 static void
1631 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1633 xassert (STRINGP (string));
1634 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1636 if (STRING_MULTIBYTE (string))
1637 *newpos = string_pos_nchars_ahead (pos, string,
1638 CHARPOS (*newpos) - CHARPOS (pos));
1639 else
1640 BYTEPOS (*newpos) = CHARPOS (*newpos);
1643 /* EXPORT:
1644 Return an estimation of the pixel height of mode or header lines on
1645 frame F. FACE_ID specifies what line's height to estimate. */
1648 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1650 #ifdef HAVE_WINDOW_SYSTEM
1651 if (FRAME_WINDOW_P (f))
1653 int height = FONT_HEIGHT (FRAME_FONT (f));
1655 /* This function is called so early when Emacs starts that the face
1656 cache and mode line face are not yet initialized. */
1657 if (FRAME_FACE_CACHE (f))
1659 struct face *face = FACE_FROM_ID (f, face_id);
1660 if (face)
1662 if (face->font)
1663 height = FONT_HEIGHT (face->font);
1664 if (face->box_line_width > 0)
1665 height += 2 * face->box_line_width;
1669 return height;
1671 #endif
1673 return 1;
1676 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1677 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1678 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1679 not force the value into range. */
1681 void
1682 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1683 int *x, int *y, NativeRectangle *bounds, int noclip)
1686 #ifdef HAVE_WINDOW_SYSTEM
1687 if (FRAME_WINDOW_P (f))
1689 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1690 even for negative values. */
1691 if (pix_x < 0)
1692 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1693 if (pix_y < 0)
1694 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1696 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1697 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1699 if (bounds)
1700 STORE_NATIVE_RECT (*bounds,
1701 FRAME_COL_TO_PIXEL_X (f, pix_x),
1702 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1703 FRAME_COLUMN_WIDTH (f) - 1,
1704 FRAME_LINE_HEIGHT (f) - 1);
1706 if (!noclip)
1708 if (pix_x < 0)
1709 pix_x = 0;
1710 else if (pix_x > FRAME_TOTAL_COLS (f))
1711 pix_x = FRAME_TOTAL_COLS (f);
1713 if (pix_y < 0)
1714 pix_y = 0;
1715 else if (pix_y > FRAME_LINES (f))
1716 pix_y = FRAME_LINES (f);
1719 #endif
1721 *x = pix_x;
1722 *y = pix_y;
1726 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1727 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1728 can't tell the positions because W's display is not up to date,
1729 return 0. */
1732 glyph_to_pixel_coords (struct window *w, int hpos, int vpos,
1733 int *frame_x, int *frame_y)
1735 #ifdef HAVE_WINDOW_SYSTEM
1736 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1738 int success_p;
1740 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1741 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1743 if (display_completed)
1745 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1746 struct glyph *glyph = row->glyphs[TEXT_AREA];
1747 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1749 hpos = row->x;
1750 vpos = row->y;
1751 while (glyph < end)
1753 hpos += glyph->pixel_width;
1754 ++glyph;
1757 /* If first glyph is partially visible, its first visible position is still 0. */
1758 if (hpos < 0)
1759 hpos = 0;
1761 success_p = 1;
1763 else
1765 hpos = vpos = 0;
1766 success_p = 0;
1769 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1770 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1771 return success_p;
1773 #endif
1775 *frame_x = hpos;
1776 *frame_y = vpos;
1777 return 1;
1781 #ifdef HAVE_WINDOW_SYSTEM
1783 /* Find the glyph under window-relative coordinates X/Y in window W.
1784 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1785 strings. Return in *HPOS and *VPOS the row and column number of
1786 the glyph found. Return in *AREA the glyph area containing X.
1787 Value is a pointer to the glyph found or null if X/Y is not on
1788 text, or we can't tell because W's current matrix is not up to
1789 date. */
1791 static
1792 struct glyph *
1793 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1794 int *dx, int *dy, int *area)
1796 struct glyph *glyph, *end;
1797 struct glyph_row *row = NULL;
1798 int x0, i;
1800 /* Find row containing Y. Give up if some row is not enabled. */
1801 for (i = 0; i < w->current_matrix->nrows; ++i)
1803 row = MATRIX_ROW (w->current_matrix, i);
1804 if (!row->enabled_p)
1805 return NULL;
1806 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1807 break;
1810 *vpos = i;
1811 *hpos = 0;
1813 /* Give up if Y is not in the window. */
1814 if (i == w->current_matrix->nrows)
1815 return NULL;
1817 /* Get the glyph area containing X. */
1818 if (w->pseudo_window_p)
1820 *area = TEXT_AREA;
1821 x0 = 0;
1823 else
1825 if (x < window_box_left_offset (w, TEXT_AREA))
1827 *area = LEFT_MARGIN_AREA;
1828 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1830 else if (x < window_box_right_offset (w, TEXT_AREA))
1832 *area = TEXT_AREA;
1833 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1835 else
1837 *area = RIGHT_MARGIN_AREA;
1838 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1842 /* Find glyph containing X. */
1843 glyph = row->glyphs[*area];
1844 end = glyph + row->used[*area];
1845 x -= x0;
1846 while (glyph < end && x >= glyph->pixel_width)
1848 x -= glyph->pixel_width;
1849 ++glyph;
1852 if (glyph == end)
1853 return NULL;
1855 if (dx)
1857 *dx = x;
1858 *dy = y - (row->y + row->ascent - glyph->ascent);
1861 *hpos = glyph - row->glyphs[*area];
1862 return glyph;
1866 /* EXPORT:
1867 Convert frame-relative x/y to coordinates relative to window W.
1868 Takes pseudo-windows into account. */
1870 void
1871 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1873 if (w->pseudo_window_p)
1875 /* A pseudo-window is always full-width, and starts at the
1876 left edge of the frame, plus a frame border. */
1877 struct frame *f = XFRAME (w->frame);
1878 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1879 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1881 else
1883 *x -= WINDOW_LEFT_EDGE_X (w);
1884 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1888 /* EXPORT:
1889 Return in RECTS[] at most N clipping rectangles for glyph string S.
1890 Return the number of stored rectangles. */
1893 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1895 XRectangle r;
1897 if (n <= 0)
1898 return 0;
1900 if (s->row->full_width_p)
1902 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1903 r.x = WINDOW_LEFT_EDGE_X (s->w);
1904 r.width = WINDOW_TOTAL_WIDTH (s->w);
1906 /* Unless displaying a mode or menu bar line, which are always
1907 fully visible, clip to the visible part of the row. */
1908 if (s->w->pseudo_window_p)
1909 r.height = s->row->visible_height;
1910 else
1911 r.height = s->height;
1913 else
1915 /* This is a text line that may be partially visible. */
1916 r.x = window_box_left (s->w, s->area);
1917 r.width = window_box_width (s->w, s->area);
1918 r.height = s->row->visible_height;
1921 if (s->clip_head)
1922 if (r.x < s->clip_head->x)
1924 if (r.width >= s->clip_head->x - r.x)
1925 r.width -= s->clip_head->x - r.x;
1926 else
1927 r.width = 0;
1928 r.x = s->clip_head->x;
1930 if (s->clip_tail)
1931 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1933 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1934 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1935 else
1936 r.width = 0;
1939 /* If S draws overlapping rows, it's sufficient to use the top and
1940 bottom of the window for clipping because this glyph string
1941 intentionally draws over other lines. */
1942 if (s->for_overlaps)
1944 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1945 r.height = window_text_bottom_y (s->w) - r.y;
1947 /* Alas, the above simple strategy does not work for the
1948 environments with anti-aliased text: if the same text is
1949 drawn onto the same place multiple times, it gets thicker.
1950 If the overlap we are processing is for the erased cursor, we
1951 take the intersection with the rectagle of the cursor. */
1952 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1954 XRectangle rc, r_save = r;
1956 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1957 rc.y = s->w->phys_cursor.y;
1958 rc.width = s->w->phys_cursor_width;
1959 rc.height = s->w->phys_cursor_height;
1961 x_intersect_rectangles (&r_save, &rc, &r);
1964 else
1966 /* Don't use S->y for clipping because it doesn't take partially
1967 visible lines into account. For example, it can be negative for
1968 partially visible lines at the top of a window. */
1969 if (!s->row->full_width_p
1970 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1971 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1972 else
1973 r.y = max (0, s->row->y);
1976 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1978 /* If drawing the cursor, don't let glyph draw outside its
1979 advertised boundaries. Cleartype does this under some circumstances. */
1980 if (s->hl == DRAW_CURSOR)
1982 struct glyph *glyph = s->first_glyph;
1983 int height, max_y;
1985 if (s->x > r.x)
1987 r.width -= s->x - r.x;
1988 r.x = s->x;
1990 r.width = min (r.width, glyph->pixel_width);
1992 /* If r.y is below window bottom, ensure that we still see a cursor. */
1993 height = min (glyph->ascent + glyph->descent,
1994 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1995 max_y = window_text_bottom_y (s->w) - height;
1996 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1997 if (s->ybase - glyph->ascent > max_y)
1999 r.y = max_y;
2000 r.height = height;
2002 else
2004 /* Don't draw cursor glyph taller than our actual glyph. */
2005 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2006 if (height < r.height)
2008 max_y = r.y + r.height;
2009 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2010 r.height = min (max_y - r.y, height);
2015 if (s->row->clip)
2017 XRectangle r_save = r;
2019 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2020 r.width = 0;
2023 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2024 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2026 #ifdef CONVERT_FROM_XRECT
2027 CONVERT_FROM_XRECT (r, *rects);
2028 #else
2029 *rects = r;
2030 #endif
2031 return 1;
2033 else
2035 /* If we are processing overlapping and allowed to return
2036 multiple clipping rectangles, we exclude the row of the glyph
2037 string from the clipping rectangle. This is to avoid drawing
2038 the same text on the environment with anti-aliasing. */
2039 #ifdef CONVERT_FROM_XRECT
2040 XRectangle rs[2];
2041 #else
2042 XRectangle *rs = rects;
2043 #endif
2044 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2046 if (s->for_overlaps & OVERLAPS_PRED)
2048 rs[i] = r;
2049 if (r.y + r.height > row_y)
2051 if (r.y < row_y)
2052 rs[i].height = row_y - r.y;
2053 else
2054 rs[i].height = 0;
2056 i++;
2058 if (s->for_overlaps & OVERLAPS_SUCC)
2060 rs[i] = r;
2061 if (r.y < row_y + s->row->visible_height)
2063 if (r.y + r.height > row_y + s->row->visible_height)
2065 rs[i].y = row_y + s->row->visible_height;
2066 rs[i].height = r.y + r.height - rs[i].y;
2068 else
2069 rs[i].height = 0;
2071 i++;
2074 n = i;
2075 #ifdef CONVERT_FROM_XRECT
2076 for (i = 0; i < n; i++)
2077 CONVERT_FROM_XRECT (rs[i], rects[i]);
2078 #endif
2079 return n;
2083 /* EXPORT:
2084 Return in *NR the clipping rectangle for glyph string S. */
2086 void
2087 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2089 get_glyph_string_clip_rects (s, nr, 1);
2093 /* EXPORT:
2094 Return the position and height of the phys cursor in window W.
2095 Set w->phys_cursor_width to width of phys cursor.
2098 void
2099 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2100 struct glyph *glyph, int *xp, int *yp, int *heightp)
2102 struct frame *f = XFRAME (WINDOW_FRAME (w));
2103 int x, y, wd, h, h0, y0;
2105 /* Compute the width of the rectangle to draw. If on a stretch
2106 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2107 rectangle as wide as the glyph, but use a canonical character
2108 width instead. */
2109 wd = glyph->pixel_width - 1;
2110 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2111 wd++; /* Why? */
2112 #endif
2114 x = w->phys_cursor.x;
2115 if (x < 0)
2117 wd += x;
2118 x = 0;
2121 if (glyph->type == STRETCH_GLYPH
2122 && !x_stretch_cursor_p)
2123 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2124 w->phys_cursor_width = wd;
2126 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2128 /* If y is below window bottom, ensure that we still see a cursor. */
2129 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2131 h = max (h0, glyph->ascent + glyph->descent);
2132 h0 = min (h0, glyph->ascent + glyph->descent);
2134 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2135 if (y < y0)
2137 h = max (h - (y0 - y) + 1, h0);
2138 y = y0 - 1;
2140 else
2142 y0 = window_text_bottom_y (w) - h0;
2143 if (y > y0)
2145 h += y - y0;
2146 y = y0;
2150 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2151 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2152 *heightp = h;
2156 * Remember which glyph the mouse is over.
2159 void
2160 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2162 Lisp_Object window;
2163 struct window *w;
2164 struct glyph_row *r, *gr, *end_row;
2165 enum window_part part;
2166 enum glyph_row_area area;
2167 int x, y, width, height;
2169 /* Try to determine frame pixel position and size of the glyph under
2170 frame pixel coordinates X/Y on frame F. */
2172 if (!f->glyphs_initialized_p
2173 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2174 NILP (window)))
2176 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2177 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2178 goto virtual_glyph;
2181 w = XWINDOW (window);
2182 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2183 height = WINDOW_FRAME_LINE_HEIGHT (w);
2185 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2186 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2188 if (w->pseudo_window_p)
2190 area = TEXT_AREA;
2191 part = ON_MODE_LINE; /* Don't adjust margin. */
2192 goto text_glyph;
2195 switch (part)
2197 case ON_LEFT_MARGIN:
2198 area = LEFT_MARGIN_AREA;
2199 goto text_glyph;
2201 case ON_RIGHT_MARGIN:
2202 area = RIGHT_MARGIN_AREA;
2203 goto text_glyph;
2205 case ON_HEADER_LINE:
2206 case ON_MODE_LINE:
2207 gr = (part == ON_HEADER_LINE
2208 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2209 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2210 gy = gr->y;
2211 area = TEXT_AREA;
2212 goto text_glyph_row_found;
2214 case ON_TEXT:
2215 area = TEXT_AREA;
2217 text_glyph:
2218 gr = 0; gy = 0;
2219 for (; r <= end_row && r->enabled_p; ++r)
2220 if (r->y + r->height > y)
2222 gr = r; gy = r->y;
2223 break;
2226 text_glyph_row_found:
2227 if (gr && gy <= y)
2229 struct glyph *g = gr->glyphs[area];
2230 struct glyph *end = g + gr->used[area];
2232 height = gr->height;
2233 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2234 if (gx + g->pixel_width > x)
2235 break;
2237 if (g < end)
2239 if (g->type == IMAGE_GLYPH)
2241 /* Don't remember when mouse is over image, as
2242 image may have hot-spots. */
2243 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2244 return;
2246 width = g->pixel_width;
2248 else
2250 /* Use nominal char spacing at end of line. */
2251 x -= gx;
2252 gx += (x / width) * width;
2255 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2256 gx += window_box_left_offset (w, area);
2258 else
2260 /* Use nominal line height at end of window. */
2261 gx = (x / width) * width;
2262 y -= gy;
2263 gy += (y / height) * height;
2265 break;
2267 case ON_LEFT_FRINGE:
2268 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2269 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2270 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2271 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2272 goto row_glyph;
2274 case ON_RIGHT_FRINGE:
2275 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2276 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2277 : window_box_right_offset (w, TEXT_AREA));
2278 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2279 goto row_glyph;
2281 case ON_SCROLL_BAR:
2282 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2284 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2285 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2286 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2287 : 0)));
2288 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2290 row_glyph:
2291 gr = 0, gy = 0;
2292 for (; r <= end_row && r->enabled_p; ++r)
2293 if (r->y + r->height > y)
2295 gr = r; gy = r->y;
2296 break;
2299 if (gr && gy <= y)
2300 height = gr->height;
2301 else
2303 /* Use nominal line height at end of window. */
2304 y -= gy;
2305 gy += (y / height) * height;
2307 break;
2309 default:
2311 virtual_glyph:
2312 /* If there is no glyph under the mouse, then we divide the screen
2313 into a grid of the smallest glyph in the frame, and use that
2314 as our "glyph". */
2316 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2317 round down even for negative values. */
2318 if (gx < 0)
2319 gx -= width - 1;
2320 if (gy < 0)
2321 gy -= height - 1;
2323 gx = (gx / width) * width;
2324 gy = (gy / height) * height;
2326 goto store_rect;
2329 gx += WINDOW_LEFT_EDGE_X (w);
2330 gy += WINDOW_TOP_EDGE_Y (w);
2332 store_rect:
2333 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2335 /* Visible feedback for debugging. */
2336 #if 0
2337 #if HAVE_X_WINDOWS
2338 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2339 f->output_data.x->normal_gc,
2340 gx, gy, width, height);
2341 #endif
2342 #endif
2346 #endif /* HAVE_WINDOW_SYSTEM */
2349 /***********************************************************************
2350 Lisp form evaluation
2351 ***********************************************************************/
2353 /* Error handler for safe_eval and safe_call. */
2355 static Lisp_Object
2356 safe_eval_handler (Lisp_Object arg)
2358 add_to_log ("Error during redisplay: %s", arg, Qnil);
2359 return Qnil;
2363 /* Evaluate SEXPR and return the result, or nil if something went
2364 wrong. Prevent redisplay during the evaluation. */
2366 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2367 Return the result, or nil if something went wrong. Prevent
2368 redisplay during the evaluation. */
2370 Lisp_Object
2371 safe_call (int nargs, Lisp_Object *args)
2373 Lisp_Object val;
2375 if (inhibit_eval_during_redisplay)
2376 val = Qnil;
2377 else
2379 int count = SPECPDL_INDEX ();
2380 struct gcpro gcpro1;
2382 GCPRO1 (args[0]);
2383 gcpro1.nvars = nargs;
2384 specbind (Qinhibit_redisplay, Qt);
2385 /* Use Qt to ensure debugger does not run,
2386 so there is no possibility of wanting to redisplay. */
2387 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2388 safe_eval_handler);
2389 UNGCPRO;
2390 val = unbind_to (count, val);
2393 return val;
2397 /* Call function FN with one argument ARG.
2398 Return the result, or nil if something went wrong. */
2400 Lisp_Object
2401 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2403 Lisp_Object args[2];
2404 args[0] = fn;
2405 args[1] = arg;
2406 return safe_call (2, args);
2409 static Lisp_Object Qeval;
2411 Lisp_Object
2412 safe_eval (Lisp_Object sexpr)
2414 return safe_call1 (Qeval, sexpr);
2417 /* Call function FN with one argument ARG.
2418 Return the result, or nil if something went wrong. */
2420 Lisp_Object
2421 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2423 Lisp_Object args[3];
2424 args[0] = fn;
2425 args[1] = arg1;
2426 args[2] = arg2;
2427 return safe_call (3, args);
2432 /***********************************************************************
2433 Debugging
2434 ***********************************************************************/
2436 #if 0
2438 /* Define CHECK_IT to perform sanity checks on iterators.
2439 This is for debugging. It is too slow to do unconditionally. */
2441 static void
2442 check_it (it)
2443 struct it *it;
2445 if (it->method == GET_FROM_STRING)
2447 xassert (STRINGP (it->string));
2448 xassert (IT_STRING_CHARPOS (*it) >= 0);
2450 else
2452 xassert (IT_STRING_CHARPOS (*it) < 0);
2453 if (it->method == GET_FROM_BUFFER)
2455 /* Check that character and byte positions agree. */
2456 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2460 if (it->dpvec)
2461 xassert (it->current.dpvec_index >= 0);
2462 else
2463 xassert (it->current.dpvec_index < 0);
2466 #define CHECK_IT(IT) check_it ((IT))
2468 #else /* not 0 */
2470 #define CHECK_IT(IT) (void) 0
2472 #endif /* not 0 */
2475 #if GLYPH_DEBUG
2477 /* Check that the window end of window W is what we expect it
2478 to be---the last row in the current matrix displaying text. */
2480 static void
2481 check_window_end (w)
2482 struct window *w;
2484 if (!MINI_WINDOW_P (w)
2485 && !NILP (w->window_end_valid))
2487 struct glyph_row *row;
2488 xassert ((row = MATRIX_ROW (w->current_matrix,
2489 XFASTINT (w->window_end_vpos)),
2490 !row->enabled_p
2491 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2492 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2496 #define CHECK_WINDOW_END(W) check_window_end ((W))
2498 #else /* not GLYPH_DEBUG */
2500 #define CHECK_WINDOW_END(W) (void) 0
2502 #endif /* not GLYPH_DEBUG */
2506 /***********************************************************************
2507 Iterator initialization
2508 ***********************************************************************/
2510 /* Initialize IT for displaying current_buffer in window W, starting
2511 at character position CHARPOS. CHARPOS < 0 means that no buffer
2512 position is specified which is useful when the iterator is assigned
2513 a position later. BYTEPOS is the byte position corresponding to
2514 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2516 If ROW is not null, calls to produce_glyphs with IT as parameter
2517 will produce glyphs in that row.
2519 BASE_FACE_ID is the id of a base face to use. It must be one of
2520 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2521 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2522 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2524 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2525 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2526 will be initialized to use the corresponding mode line glyph row of
2527 the desired matrix of W. */
2529 void
2530 init_iterator (struct it *it, struct window *w,
2531 EMACS_INT charpos, EMACS_INT bytepos,
2532 struct glyph_row *row, enum face_id base_face_id)
2534 int highlight_region_p;
2535 enum face_id remapped_base_face_id = base_face_id;
2537 /* Some precondition checks. */
2538 xassert (w != NULL && it != NULL);
2539 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2540 && charpos <= ZV));
2542 /* If face attributes have been changed since the last redisplay,
2543 free realized faces now because they depend on face definitions
2544 that might have changed. Don't free faces while there might be
2545 desired matrices pending which reference these faces. */
2546 if (face_change_count && !inhibit_free_realized_faces)
2548 face_change_count = 0;
2549 free_all_realized_faces (Qnil);
2552 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2553 if (! NILP (Vface_remapping_alist))
2554 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2556 /* Use one of the mode line rows of W's desired matrix if
2557 appropriate. */
2558 if (row == NULL)
2560 if (base_face_id == MODE_LINE_FACE_ID
2561 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2562 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2563 else if (base_face_id == HEADER_LINE_FACE_ID)
2564 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2567 /* Clear IT. */
2568 memset (it, 0, sizeof *it);
2569 it->current.overlay_string_index = -1;
2570 it->current.dpvec_index = -1;
2571 it->base_face_id = remapped_base_face_id;
2572 it->string = Qnil;
2573 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2575 /* The window in which we iterate over current_buffer: */
2576 XSETWINDOW (it->window, w);
2577 it->w = w;
2578 it->f = XFRAME (w->frame);
2580 it->cmp_it.id = -1;
2582 /* Extra space between lines (on window systems only). */
2583 if (base_face_id == DEFAULT_FACE_ID
2584 && FRAME_WINDOW_P (it->f))
2586 if (NATNUMP (current_buffer->extra_line_spacing))
2587 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2588 else if (FLOATP (current_buffer->extra_line_spacing))
2589 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2590 * FRAME_LINE_HEIGHT (it->f));
2591 else if (it->f->extra_line_spacing > 0)
2592 it->extra_line_spacing = it->f->extra_line_spacing;
2593 it->max_extra_line_spacing = 0;
2596 /* If realized faces have been removed, e.g. because of face
2597 attribute changes of named faces, recompute them. When running
2598 in batch mode, the face cache of the initial frame is null. If
2599 we happen to get called, make a dummy face cache. */
2600 if (FRAME_FACE_CACHE (it->f) == NULL)
2601 init_frame_faces (it->f);
2602 if (FRAME_FACE_CACHE (it->f)->used == 0)
2603 recompute_basic_faces (it->f);
2605 /* Current value of the `slice', `space-width', and 'height' properties. */
2606 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2607 it->space_width = Qnil;
2608 it->font_height = Qnil;
2609 it->override_ascent = -1;
2611 /* Are control characters displayed as `^C'? */
2612 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2614 /* -1 means everything between a CR and the following line end
2615 is invisible. >0 means lines indented more than this value are
2616 invisible. */
2617 it->selective = (INTEGERP (current_buffer->selective_display)
2618 ? XFASTINT (current_buffer->selective_display)
2619 : (!NILP (current_buffer->selective_display)
2620 ? -1 : 0));
2621 it->selective_display_ellipsis_p
2622 = !NILP (current_buffer->selective_display_ellipses);
2624 /* Display table to use. */
2625 it->dp = window_display_table (w);
2627 /* Are multibyte characters enabled in current_buffer? */
2628 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2630 /* Do we need to reorder bidirectional text? Not if this is a
2631 unibyte buffer: by definition, none of the single-byte characters
2632 are strong R2L, so no reordering is needed. And bidi.c doesn't
2633 support unibyte buffers anyway. */
2634 it->bidi_p
2635 = !NILP (current_buffer->bidi_display_reordering) && it->multibyte_p;
2637 /* Non-zero if we should highlight the region. */
2638 highlight_region_p
2639 = (!NILP (Vtransient_mark_mode)
2640 && !NILP (current_buffer->mark_active)
2641 && XMARKER (current_buffer->mark)->buffer != 0);
2643 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2644 start and end of a visible region in window IT->w. Set both to
2645 -1 to indicate no region. */
2646 if (highlight_region_p
2647 /* Maybe highlight only in selected window. */
2648 && (/* Either show region everywhere. */
2649 highlight_nonselected_windows
2650 /* Or show region in the selected window. */
2651 || w == XWINDOW (selected_window)
2652 /* Or show the region if we are in the mini-buffer and W is
2653 the window the mini-buffer refers to. */
2654 || (MINI_WINDOW_P (XWINDOW (selected_window))
2655 && WINDOWP (minibuf_selected_window)
2656 && w == XWINDOW (minibuf_selected_window))))
2658 int charpos = marker_position (current_buffer->mark);
2659 it->region_beg_charpos = min (PT, charpos);
2660 it->region_end_charpos = max (PT, charpos);
2662 else
2663 it->region_beg_charpos = it->region_end_charpos = -1;
2665 /* Get the position at which the redisplay_end_trigger hook should
2666 be run, if it is to be run at all. */
2667 if (MARKERP (w->redisplay_end_trigger)
2668 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2669 it->redisplay_end_trigger_charpos
2670 = marker_position (w->redisplay_end_trigger);
2671 else if (INTEGERP (w->redisplay_end_trigger))
2672 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2674 /* Correct bogus values of tab_width. */
2675 it->tab_width = XINT (current_buffer->tab_width);
2676 if (it->tab_width <= 0 || it->tab_width > 1000)
2677 it->tab_width = 8;
2679 /* Are lines in the display truncated? */
2680 if (base_face_id != DEFAULT_FACE_ID
2681 || XINT (it->w->hscroll)
2682 || (! WINDOW_FULL_WIDTH_P (it->w)
2683 && ((!NILP (Vtruncate_partial_width_windows)
2684 && !INTEGERP (Vtruncate_partial_width_windows))
2685 || (INTEGERP (Vtruncate_partial_width_windows)
2686 && (WINDOW_TOTAL_COLS (it->w)
2687 < XINT (Vtruncate_partial_width_windows))))))
2688 it->line_wrap = TRUNCATE;
2689 else if (NILP (current_buffer->truncate_lines))
2690 it->line_wrap = NILP (current_buffer->word_wrap)
2691 ? WINDOW_WRAP : WORD_WRAP;
2692 else
2693 it->line_wrap = TRUNCATE;
2695 /* Get dimensions of truncation and continuation glyphs. These are
2696 displayed as fringe bitmaps under X, so we don't need them for such
2697 frames. */
2698 if (!FRAME_WINDOW_P (it->f))
2700 if (it->line_wrap == TRUNCATE)
2702 /* We will need the truncation glyph. */
2703 xassert (it->glyph_row == NULL);
2704 produce_special_glyphs (it, IT_TRUNCATION);
2705 it->truncation_pixel_width = it->pixel_width;
2707 else
2709 /* We will need the continuation glyph. */
2710 xassert (it->glyph_row == NULL);
2711 produce_special_glyphs (it, IT_CONTINUATION);
2712 it->continuation_pixel_width = it->pixel_width;
2715 /* Reset these values to zero because the produce_special_glyphs
2716 above has changed them. */
2717 it->pixel_width = it->ascent = it->descent = 0;
2718 it->phys_ascent = it->phys_descent = 0;
2721 /* Set this after getting the dimensions of truncation and
2722 continuation glyphs, so that we don't produce glyphs when calling
2723 produce_special_glyphs, above. */
2724 it->glyph_row = row;
2725 it->area = TEXT_AREA;
2727 /* Forget any previous info about this row being reversed. */
2728 if (it->glyph_row)
2729 it->glyph_row->reversed_p = 0;
2731 /* Get the dimensions of the display area. The display area
2732 consists of the visible window area plus a horizontally scrolled
2733 part to the left of the window. All x-values are relative to the
2734 start of this total display area. */
2735 if (base_face_id != DEFAULT_FACE_ID)
2737 /* Mode lines, menu bar in terminal frames. */
2738 it->first_visible_x = 0;
2739 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2741 else
2743 it->first_visible_x
2744 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2745 it->last_visible_x = (it->first_visible_x
2746 + window_box_width (w, TEXT_AREA));
2748 /* If we truncate lines, leave room for the truncator glyph(s) at
2749 the right margin. Otherwise, leave room for the continuation
2750 glyph(s). Truncation and continuation glyphs are not inserted
2751 for window-based redisplay. */
2752 if (!FRAME_WINDOW_P (it->f))
2754 if (it->line_wrap == TRUNCATE)
2755 it->last_visible_x -= it->truncation_pixel_width;
2756 else
2757 it->last_visible_x -= it->continuation_pixel_width;
2760 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2761 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2764 /* Leave room for a border glyph. */
2765 if (!FRAME_WINDOW_P (it->f)
2766 && !WINDOW_RIGHTMOST_P (it->w))
2767 it->last_visible_x -= 1;
2769 it->last_visible_y = window_text_bottom_y (w);
2771 /* For mode lines and alike, arrange for the first glyph having a
2772 left box line if the face specifies a box. */
2773 if (base_face_id != DEFAULT_FACE_ID)
2775 struct face *face;
2777 it->face_id = remapped_base_face_id;
2779 /* If we have a boxed mode line, make the first character appear
2780 with a left box line. */
2781 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2782 if (face->box != FACE_NO_BOX)
2783 it->start_of_box_run_p = 1;
2786 /* If we are to reorder bidirectional text, init the bidi
2787 iterator. */
2788 if (it->bidi_p)
2790 /* Note the paragraph direction that this buffer wants to
2791 use. */
2792 if (EQ (current_buffer->bidi_paragraph_direction, Qleft_to_right))
2793 it->paragraph_embedding = L2R;
2794 else if (EQ (current_buffer->bidi_paragraph_direction, Qright_to_left))
2795 it->paragraph_embedding = R2L;
2796 else
2797 it->paragraph_embedding = NEUTRAL_DIR;
2798 bidi_init_it (charpos, bytepos, &it->bidi_it);
2801 /* If a buffer position was specified, set the iterator there,
2802 getting overlays and face properties from that position. */
2803 if (charpos >= BUF_BEG (current_buffer))
2805 it->end_charpos = ZV;
2806 it->face_id = -1;
2807 IT_CHARPOS (*it) = charpos;
2809 /* Compute byte position if not specified. */
2810 if (bytepos < charpos)
2811 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2812 else
2813 IT_BYTEPOS (*it) = bytepos;
2815 it->start = it->current;
2817 /* Compute faces etc. */
2818 reseat (it, it->current.pos, 1);
2821 CHECK_IT (it);
2825 /* Initialize IT for the display of window W with window start POS. */
2827 void
2828 start_display (struct it *it, struct window *w, struct text_pos pos)
2830 struct glyph_row *row;
2831 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2833 row = w->desired_matrix->rows + first_vpos;
2834 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2835 it->first_vpos = first_vpos;
2837 /* Don't reseat to previous visible line start if current start
2838 position is in a string or image. */
2839 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2841 int start_at_line_beg_p;
2842 int first_y = it->current_y;
2844 /* If window start is not at a line start, skip forward to POS to
2845 get the correct continuation lines width. */
2846 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2847 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2848 if (!start_at_line_beg_p)
2850 int new_x;
2852 reseat_at_previous_visible_line_start (it);
2853 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2855 new_x = it->current_x + it->pixel_width;
2857 /* If lines are continued, this line may end in the middle
2858 of a multi-glyph character (e.g. a control character
2859 displayed as \003, or in the middle of an overlay
2860 string). In this case move_it_to above will not have
2861 taken us to the start of the continuation line but to the
2862 end of the continued line. */
2863 if (it->current_x > 0
2864 && it->line_wrap != TRUNCATE /* Lines are continued. */
2865 && (/* And glyph doesn't fit on the line. */
2866 new_x > it->last_visible_x
2867 /* Or it fits exactly and we're on a window
2868 system frame. */
2869 || (new_x == it->last_visible_x
2870 && FRAME_WINDOW_P (it->f))))
2872 if (it->current.dpvec_index >= 0
2873 || it->current.overlay_string_index >= 0)
2875 set_iterator_to_next (it, 1);
2876 move_it_in_display_line_to (it, -1, -1, 0);
2879 it->continuation_lines_width += it->current_x;
2882 /* We're starting a new display line, not affected by the
2883 height of the continued line, so clear the appropriate
2884 fields in the iterator structure. */
2885 it->max_ascent = it->max_descent = 0;
2886 it->max_phys_ascent = it->max_phys_descent = 0;
2888 it->current_y = first_y;
2889 it->vpos = 0;
2890 it->current_x = it->hpos = 0;
2896 /* Return 1 if POS is a position in ellipses displayed for invisible
2897 text. W is the window we display, for text property lookup. */
2899 static int
2900 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2902 Lisp_Object prop, window;
2903 int ellipses_p = 0;
2904 int charpos = CHARPOS (pos->pos);
2906 /* If POS specifies a position in a display vector, this might
2907 be for an ellipsis displayed for invisible text. We won't
2908 get the iterator set up for delivering that ellipsis unless
2909 we make sure that it gets aware of the invisible text. */
2910 if (pos->dpvec_index >= 0
2911 && pos->overlay_string_index < 0
2912 && CHARPOS (pos->string_pos) < 0
2913 && charpos > BEGV
2914 && (XSETWINDOW (window, w),
2915 prop = Fget_char_property (make_number (charpos),
2916 Qinvisible, window),
2917 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2919 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2920 window);
2921 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2924 return ellipses_p;
2928 /* Initialize IT for stepping through current_buffer in window W,
2929 starting at position POS that includes overlay string and display
2930 vector/ control character translation position information. Value
2931 is zero if there are overlay strings with newlines at POS. */
2933 static int
2934 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2936 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2937 int i, overlay_strings_with_newlines = 0;
2939 /* If POS specifies a position in a display vector, this might
2940 be for an ellipsis displayed for invisible text. We won't
2941 get the iterator set up for delivering that ellipsis unless
2942 we make sure that it gets aware of the invisible text. */
2943 if (in_ellipses_for_invisible_text_p (pos, w))
2945 --charpos;
2946 bytepos = 0;
2949 /* Keep in mind: the call to reseat in init_iterator skips invisible
2950 text, so we might end up at a position different from POS. This
2951 is only a problem when POS is a row start after a newline and an
2952 overlay starts there with an after-string, and the overlay has an
2953 invisible property. Since we don't skip invisible text in
2954 display_line and elsewhere immediately after consuming the
2955 newline before the row start, such a POS will not be in a string,
2956 but the call to init_iterator below will move us to the
2957 after-string. */
2958 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2960 /* This only scans the current chunk -- it should scan all chunks.
2961 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2962 to 16 in 22.1 to make this a lesser problem. */
2963 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2965 const char *s = SDATA (it->overlay_strings[i]);
2966 const char *e = s + SBYTES (it->overlay_strings[i]);
2968 while (s < e && *s != '\n')
2969 ++s;
2971 if (s < e)
2973 overlay_strings_with_newlines = 1;
2974 break;
2978 /* If position is within an overlay string, set up IT to the right
2979 overlay string. */
2980 if (pos->overlay_string_index >= 0)
2982 int relative_index;
2984 /* If the first overlay string happens to have a `display'
2985 property for an image, the iterator will be set up for that
2986 image, and we have to undo that setup first before we can
2987 correct the overlay string index. */
2988 if (it->method == GET_FROM_IMAGE)
2989 pop_it (it);
2991 /* We already have the first chunk of overlay strings in
2992 IT->overlay_strings. Load more until the one for
2993 pos->overlay_string_index is in IT->overlay_strings. */
2994 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2996 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2997 it->current.overlay_string_index = 0;
2998 while (n--)
3000 load_overlay_strings (it, 0);
3001 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3005 it->current.overlay_string_index = pos->overlay_string_index;
3006 relative_index = (it->current.overlay_string_index
3007 % OVERLAY_STRING_CHUNK_SIZE);
3008 it->string = it->overlay_strings[relative_index];
3009 xassert (STRINGP (it->string));
3010 it->current.string_pos = pos->string_pos;
3011 it->method = GET_FROM_STRING;
3014 if (CHARPOS (pos->string_pos) >= 0)
3016 /* Recorded position is not in an overlay string, but in another
3017 string. This can only be a string from a `display' property.
3018 IT should already be filled with that string. */
3019 it->current.string_pos = pos->string_pos;
3020 xassert (STRINGP (it->string));
3023 /* Restore position in display vector translations, control
3024 character translations or ellipses. */
3025 if (pos->dpvec_index >= 0)
3027 if (it->dpvec == NULL)
3028 get_next_display_element (it);
3029 xassert (it->dpvec && it->current.dpvec_index == 0);
3030 it->current.dpvec_index = pos->dpvec_index;
3033 CHECK_IT (it);
3034 return !overlay_strings_with_newlines;
3038 /* Initialize IT for stepping through current_buffer in window W
3039 starting at ROW->start. */
3041 static void
3042 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3044 init_from_display_pos (it, w, &row->start);
3045 it->start = row->start;
3046 it->continuation_lines_width = row->continuation_lines_width;
3047 CHECK_IT (it);
3051 /* Initialize IT for stepping through current_buffer in window W
3052 starting in the line following ROW, i.e. starting at ROW->end.
3053 Value is zero if there are overlay strings with newlines at ROW's
3054 end position. */
3056 static int
3057 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3059 int success = 0;
3061 if (init_from_display_pos (it, w, &row->end))
3063 if (row->continued_p)
3064 it->continuation_lines_width
3065 = row->continuation_lines_width + row->pixel_width;
3066 CHECK_IT (it);
3067 success = 1;
3070 return success;
3076 /***********************************************************************
3077 Text properties
3078 ***********************************************************************/
3080 /* Called when IT reaches IT->stop_charpos. Handle text property and
3081 overlay changes. Set IT->stop_charpos to the next position where
3082 to stop. */
3084 static void
3085 handle_stop (struct it *it)
3087 enum prop_handled handled;
3088 int handle_overlay_change_p;
3089 struct props *p;
3091 it->dpvec = NULL;
3092 it->current.dpvec_index = -1;
3093 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3094 it->ignore_overlay_strings_at_pos_p = 0;
3095 it->ellipsis_p = 0;
3097 /* Use face of preceding text for ellipsis (if invisible) */
3098 if (it->selective_display_ellipsis_p)
3099 it->saved_face_id = it->face_id;
3103 handled = HANDLED_NORMALLY;
3105 /* Call text property handlers. */
3106 for (p = it_props; p->handler; ++p)
3108 handled = p->handler (it);
3110 if (handled == HANDLED_RECOMPUTE_PROPS)
3111 break;
3112 else if (handled == HANDLED_RETURN)
3114 /* We still want to show before and after strings from
3115 overlays even if the actual buffer text is replaced. */
3116 if (!handle_overlay_change_p
3117 || it->sp > 1
3118 || !get_overlay_strings_1 (it, 0, 0))
3120 if (it->ellipsis_p)
3121 setup_for_ellipsis (it, 0);
3122 /* When handling a display spec, we might load an
3123 empty string. In that case, discard it here. We
3124 used to discard it in handle_single_display_spec,
3125 but that causes get_overlay_strings_1, above, to
3126 ignore overlay strings that we must check. */
3127 if (STRINGP (it->string) && !SCHARS (it->string))
3128 pop_it (it);
3129 return;
3131 else if (STRINGP (it->string) && !SCHARS (it->string))
3132 pop_it (it);
3133 else
3135 it->ignore_overlay_strings_at_pos_p = 1;
3136 it->string_from_display_prop_p = 0;
3137 handle_overlay_change_p = 0;
3139 handled = HANDLED_RECOMPUTE_PROPS;
3140 break;
3142 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3143 handle_overlay_change_p = 0;
3146 if (handled != HANDLED_RECOMPUTE_PROPS)
3148 /* Don't check for overlay strings below when set to deliver
3149 characters from a display vector. */
3150 if (it->method == GET_FROM_DISPLAY_VECTOR)
3151 handle_overlay_change_p = 0;
3153 /* Handle overlay changes.
3154 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3155 if it finds overlays. */
3156 if (handle_overlay_change_p)
3157 handled = handle_overlay_change (it);
3160 if (it->ellipsis_p)
3162 setup_for_ellipsis (it, 0);
3163 break;
3166 while (handled == HANDLED_RECOMPUTE_PROPS);
3168 /* Determine where to stop next. */
3169 if (handled == HANDLED_NORMALLY)
3170 compute_stop_pos (it);
3174 /* Compute IT->stop_charpos from text property and overlay change
3175 information for IT's current position. */
3177 static void
3178 compute_stop_pos (struct it *it)
3180 register INTERVAL iv, next_iv;
3181 Lisp_Object object, limit, position;
3182 EMACS_INT charpos, bytepos;
3184 /* If nowhere else, stop at the end. */
3185 it->stop_charpos = it->end_charpos;
3187 if (STRINGP (it->string))
3189 /* Strings are usually short, so don't limit the search for
3190 properties. */
3191 object = it->string;
3192 limit = Qnil;
3193 charpos = IT_STRING_CHARPOS (*it);
3194 bytepos = IT_STRING_BYTEPOS (*it);
3196 else
3198 EMACS_INT pos;
3200 /* If next overlay change is in front of the current stop pos
3201 (which is IT->end_charpos), stop there. Note: value of
3202 next_overlay_change is point-max if no overlay change
3203 follows. */
3204 charpos = IT_CHARPOS (*it);
3205 bytepos = IT_BYTEPOS (*it);
3206 pos = next_overlay_change (charpos);
3207 if (pos < it->stop_charpos)
3208 it->stop_charpos = pos;
3210 /* If showing the region, we have to stop at the region
3211 start or end because the face might change there. */
3212 if (it->region_beg_charpos > 0)
3214 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3215 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3216 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3217 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3220 /* Set up variables for computing the stop position from text
3221 property changes. */
3222 XSETBUFFER (object, current_buffer);
3223 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3226 /* Get the interval containing IT's position. Value is a null
3227 interval if there isn't such an interval. */
3228 position = make_number (charpos);
3229 iv = validate_interval_range (object, &position, &position, 0);
3230 if (!NULL_INTERVAL_P (iv))
3232 Lisp_Object values_here[LAST_PROP_IDX];
3233 struct props *p;
3235 /* Get properties here. */
3236 for (p = it_props; p->handler; ++p)
3237 values_here[p->idx] = textget (iv->plist, *p->name);
3239 /* Look for an interval following iv that has different
3240 properties. */
3241 for (next_iv = next_interval (iv);
3242 (!NULL_INTERVAL_P (next_iv)
3243 && (NILP (limit)
3244 || XFASTINT (limit) > next_iv->position));
3245 next_iv = next_interval (next_iv))
3247 for (p = it_props; p->handler; ++p)
3249 Lisp_Object new_value;
3251 new_value = textget (next_iv->plist, *p->name);
3252 if (!EQ (values_here[p->idx], new_value))
3253 break;
3256 if (p->handler)
3257 break;
3260 if (!NULL_INTERVAL_P (next_iv))
3262 if (INTEGERP (limit)
3263 && next_iv->position >= XFASTINT (limit))
3264 /* No text property change up to limit. */
3265 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3266 else
3267 /* Text properties change in next_iv. */
3268 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3272 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3273 it->stop_charpos, it->string);
3275 xassert (STRINGP (it->string)
3276 || (it->stop_charpos >= BEGV
3277 && it->stop_charpos >= IT_CHARPOS (*it)));
3281 /* Return the position of the next overlay change after POS in
3282 current_buffer. Value is point-max if no overlay change
3283 follows. This is like `next-overlay-change' but doesn't use
3284 xmalloc. */
3286 static EMACS_INT
3287 next_overlay_change (EMACS_INT pos)
3289 int noverlays;
3290 EMACS_INT endpos;
3291 Lisp_Object *overlays;
3292 int i;
3294 /* Get all overlays at the given position. */
3295 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3297 /* If any of these overlays ends before endpos,
3298 use its ending point instead. */
3299 for (i = 0; i < noverlays; ++i)
3301 Lisp_Object oend;
3302 EMACS_INT oendpos;
3304 oend = OVERLAY_END (overlays[i]);
3305 oendpos = OVERLAY_POSITION (oend);
3306 endpos = min (endpos, oendpos);
3309 return endpos;
3314 /***********************************************************************
3315 Fontification
3316 ***********************************************************************/
3318 /* Handle changes in the `fontified' property of the current buffer by
3319 calling hook functions from Qfontification_functions to fontify
3320 regions of text. */
3322 static enum prop_handled
3323 handle_fontified_prop (struct it *it)
3325 Lisp_Object prop, pos;
3326 enum prop_handled handled = HANDLED_NORMALLY;
3328 if (!NILP (Vmemory_full))
3329 return handled;
3331 /* Get the value of the `fontified' property at IT's current buffer
3332 position. (The `fontified' property doesn't have a special
3333 meaning in strings.) If the value is nil, call functions from
3334 Qfontification_functions. */
3335 if (!STRINGP (it->string)
3336 && it->s == NULL
3337 && !NILP (Vfontification_functions)
3338 && !NILP (Vrun_hooks)
3339 && (pos = make_number (IT_CHARPOS (*it)),
3340 prop = Fget_char_property (pos, Qfontified, Qnil),
3341 /* Ignore the special cased nil value always present at EOB since
3342 no amount of fontifying will be able to change it. */
3343 NILP (prop) && IT_CHARPOS (*it) < Z))
3345 int count = SPECPDL_INDEX ();
3346 Lisp_Object val;
3348 val = Vfontification_functions;
3349 specbind (Qfontification_functions, Qnil);
3351 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3352 safe_call1 (val, pos);
3353 else
3355 Lisp_Object globals, fn;
3356 struct gcpro gcpro1, gcpro2;
3358 globals = Qnil;
3359 GCPRO2 (val, globals);
3361 for (; CONSP (val); val = XCDR (val))
3363 fn = XCAR (val);
3365 if (EQ (fn, Qt))
3367 /* A value of t indicates this hook has a local
3368 binding; it means to run the global binding too.
3369 In a global value, t should not occur. If it
3370 does, we must ignore it to avoid an endless
3371 loop. */
3372 for (globals = Fdefault_value (Qfontification_functions);
3373 CONSP (globals);
3374 globals = XCDR (globals))
3376 fn = XCAR (globals);
3377 if (!EQ (fn, Qt))
3378 safe_call1 (fn, pos);
3381 else
3382 safe_call1 (fn, pos);
3385 UNGCPRO;
3388 unbind_to (count, Qnil);
3390 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3391 something. This avoids an endless loop if they failed to
3392 fontify the text for which reason ever. */
3393 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3394 handled = HANDLED_RECOMPUTE_PROPS;
3397 return handled;
3402 /***********************************************************************
3403 Faces
3404 ***********************************************************************/
3406 /* Set up iterator IT from face properties at its current position.
3407 Called from handle_stop. */
3409 static enum prop_handled
3410 handle_face_prop (struct it *it)
3412 int new_face_id;
3413 EMACS_INT next_stop;
3415 if (!STRINGP (it->string))
3417 new_face_id
3418 = face_at_buffer_position (it->w,
3419 IT_CHARPOS (*it),
3420 it->region_beg_charpos,
3421 it->region_end_charpos,
3422 &next_stop,
3423 (IT_CHARPOS (*it)
3424 + TEXT_PROP_DISTANCE_LIMIT),
3425 0, it->base_face_id);
3427 /* Is this a start of a run of characters with box face?
3428 Caveat: this can be called for a freshly initialized
3429 iterator; face_id is -1 in this case. We know that the new
3430 face will not change until limit, i.e. if the new face has a
3431 box, all characters up to limit will have one. But, as
3432 usual, we don't know whether limit is really the end. */
3433 if (new_face_id != it->face_id)
3435 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3437 /* If new face has a box but old face has not, this is
3438 the start of a run of characters with box, i.e. it has
3439 a shadow on the left side. The value of face_id of the
3440 iterator will be -1 if this is the initial call that gets
3441 the face. In this case, we have to look in front of IT's
3442 position and see whether there is a face != new_face_id. */
3443 it->start_of_box_run_p
3444 = (new_face->box != FACE_NO_BOX
3445 && (it->face_id >= 0
3446 || IT_CHARPOS (*it) == BEG
3447 || new_face_id != face_before_it_pos (it)));
3448 it->face_box_p = new_face->box != FACE_NO_BOX;
3451 else
3453 int base_face_id, bufpos;
3454 int i;
3455 Lisp_Object from_overlay
3456 = (it->current.overlay_string_index >= 0
3457 ? it->string_overlays[it->current.overlay_string_index]
3458 : Qnil);
3460 /* See if we got to this string directly or indirectly from
3461 an overlay property. That includes the before-string or
3462 after-string of an overlay, strings in display properties
3463 provided by an overlay, their text properties, etc.
3465 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3466 if (! NILP (from_overlay))
3467 for (i = it->sp - 1; i >= 0; i--)
3469 if (it->stack[i].current.overlay_string_index >= 0)
3470 from_overlay
3471 = it->string_overlays[it->stack[i].current.overlay_string_index];
3472 else if (! NILP (it->stack[i].from_overlay))
3473 from_overlay = it->stack[i].from_overlay;
3475 if (!NILP (from_overlay))
3476 break;
3479 if (! NILP (from_overlay))
3481 bufpos = IT_CHARPOS (*it);
3482 /* For a string from an overlay, the base face depends
3483 only on text properties and ignores overlays. */
3484 base_face_id
3485 = face_for_overlay_string (it->w,
3486 IT_CHARPOS (*it),
3487 it->region_beg_charpos,
3488 it->region_end_charpos,
3489 &next_stop,
3490 (IT_CHARPOS (*it)
3491 + TEXT_PROP_DISTANCE_LIMIT),
3493 from_overlay);
3495 else
3497 bufpos = 0;
3499 /* For strings from a `display' property, use the face at
3500 IT's current buffer position as the base face to merge
3501 with, so that overlay strings appear in the same face as
3502 surrounding text, unless they specify their own
3503 faces. */
3504 base_face_id = underlying_face_id (it);
3507 new_face_id = face_at_string_position (it->w,
3508 it->string,
3509 IT_STRING_CHARPOS (*it),
3510 bufpos,
3511 it->region_beg_charpos,
3512 it->region_end_charpos,
3513 &next_stop,
3514 base_face_id, 0);
3516 /* Is this a start of a run of characters with box? Caveat:
3517 this can be called for a freshly allocated iterator; face_id
3518 is -1 is this case. We know that the new face will not
3519 change until the next check pos, i.e. if the new face has a
3520 box, all characters up to that position will have a
3521 box. But, as usual, we don't know whether that position
3522 is really the end. */
3523 if (new_face_id != it->face_id)
3525 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3526 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3528 /* If new face has a box but old face hasn't, this is the
3529 start of a run of characters with box, i.e. it has a
3530 shadow on the left side. */
3531 it->start_of_box_run_p
3532 = new_face->box && (old_face == NULL || !old_face->box);
3533 it->face_box_p = new_face->box != FACE_NO_BOX;
3537 it->face_id = new_face_id;
3538 return HANDLED_NORMALLY;
3542 /* Return the ID of the face ``underlying'' IT's current position,
3543 which is in a string. If the iterator is associated with a
3544 buffer, return the face at IT's current buffer position.
3545 Otherwise, use the iterator's base_face_id. */
3547 static int
3548 underlying_face_id (struct it *it)
3550 int face_id = it->base_face_id, i;
3552 xassert (STRINGP (it->string));
3554 for (i = it->sp - 1; i >= 0; --i)
3555 if (NILP (it->stack[i].string))
3556 face_id = it->stack[i].face_id;
3558 return face_id;
3562 /* Compute the face one character before or after the current position
3563 of IT. BEFORE_P non-zero means get the face in front of IT's
3564 position. Value is the id of the face. */
3566 static int
3567 face_before_or_after_it_pos (struct it *it, int before_p)
3569 int face_id, limit;
3570 EMACS_INT next_check_charpos;
3571 struct text_pos pos;
3573 xassert (it->s == NULL);
3575 if (STRINGP (it->string))
3577 int bufpos, base_face_id;
3579 /* No face change past the end of the string (for the case
3580 we are padding with spaces). No face change before the
3581 string start. */
3582 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3583 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3584 return it->face_id;
3586 /* Set pos to the position before or after IT's current position. */
3587 if (before_p)
3588 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3589 else
3590 /* For composition, we must check the character after the
3591 composition. */
3592 pos = (it->what == IT_COMPOSITION
3593 ? string_pos (IT_STRING_CHARPOS (*it)
3594 + it->cmp_it.nchars, it->string)
3595 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3597 if (it->current.overlay_string_index >= 0)
3598 bufpos = IT_CHARPOS (*it);
3599 else
3600 bufpos = 0;
3602 base_face_id = underlying_face_id (it);
3604 /* Get the face for ASCII, or unibyte. */
3605 face_id = face_at_string_position (it->w,
3606 it->string,
3607 CHARPOS (pos),
3608 bufpos,
3609 it->region_beg_charpos,
3610 it->region_end_charpos,
3611 &next_check_charpos,
3612 base_face_id, 0);
3614 /* Correct the face for charsets different from ASCII. Do it
3615 for the multibyte case only. The face returned above is
3616 suitable for unibyte text if IT->string is unibyte. */
3617 if (STRING_MULTIBYTE (it->string))
3619 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3620 int rest = SBYTES (it->string) - BYTEPOS (pos);
3621 int c, len;
3622 struct face *face = FACE_FROM_ID (it->f, face_id);
3624 c = string_char_and_length (p, &len);
3625 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3628 else
3630 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3631 || (IT_CHARPOS (*it) <= BEGV && before_p))
3632 return it->face_id;
3634 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3635 pos = it->current.pos;
3637 if (before_p)
3638 DEC_TEXT_POS (pos, it->multibyte_p);
3639 else
3641 if (it->what == IT_COMPOSITION)
3642 /* For composition, we must check the position after the
3643 composition. */
3644 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3645 else
3646 INC_TEXT_POS (pos, it->multibyte_p);
3649 /* Determine face for CHARSET_ASCII, or unibyte. */
3650 face_id = face_at_buffer_position (it->w,
3651 CHARPOS (pos),
3652 it->region_beg_charpos,
3653 it->region_end_charpos,
3654 &next_check_charpos,
3655 limit, 0, -1);
3657 /* Correct the face for charsets different from ASCII. Do it
3658 for the multibyte case only. The face returned above is
3659 suitable for unibyte text if current_buffer is unibyte. */
3660 if (it->multibyte_p)
3662 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3663 struct face *face = FACE_FROM_ID (it->f, face_id);
3664 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3668 return face_id;
3673 /***********************************************************************
3674 Invisible text
3675 ***********************************************************************/
3677 /* Set up iterator IT from invisible properties at its current
3678 position. Called from handle_stop. */
3680 static enum prop_handled
3681 handle_invisible_prop (struct it *it)
3683 enum prop_handled handled = HANDLED_NORMALLY;
3685 if (STRINGP (it->string))
3687 Lisp_Object prop, end_charpos, limit, charpos;
3689 /* Get the value of the invisible text property at the
3690 current position. Value will be nil if there is no such
3691 property. */
3692 charpos = make_number (IT_STRING_CHARPOS (*it));
3693 prop = Fget_text_property (charpos, Qinvisible, it->string);
3695 if (!NILP (prop)
3696 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3698 handled = HANDLED_RECOMPUTE_PROPS;
3700 /* Get the position at which the next change of the
3701 invisible text property can be found in IT->string.
3702 Value will be nil if the property value is the same for
3703 all the rest of IT->string. */
3704 XSETINT (limit, SCHARS (it->string));
3705 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3706 it->string, limit);
3708 /* Text at current position is invisible. The next
3709 change in the property is at position end_charpos.
3710 Move IT's current position to that position. */
3711 if (INTEGERP (end_charpos)
3712 && XFASTINT (end_charpos) < XFASTINT (limit))
3714 struct text_pos old;
3715 old = it->current.string_pos;
3716 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3717 compute_string_pos (&it->current.string_pos, old, it->string);
3719 else
3721 /* The rest of the string is invisible. If this is an
3722 overlay string, proceed with the next overlay string
3723 or whatever comes and return a character from there. */
3724 if (it->current.overlay_string_index >= 0)
3726 next_overlay_string (it);
3727 /* Don't check for overlay strings when we just
3728 finished processing them. */
3729 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3731 else
3733 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3734 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3739 else
3741 int invis_p;
3742 EMACS_INT newpos, next_stop, start_charpos, tem;
3743 Lisp_Object pos, prop, overlay;
3745 /* First of all, is there invisible text at this position? */
3746 tem = start_charpos = IT_CHARPOS (*it);
3747 pos = make_number (tem);
3748 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3749 &overlay);
3750 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3752 /* If we are on invisible text, skip over it. */
3753 if (invis_p && start_charpos < it->end_charpos)
3755 /* Record whether we have to display an ellipsis for the
3756 invisible text. */
3757 int display_ellipsis_p = invis_p == 2;
3759 handled = HANDLED_RECOMPUTE_PROPS;
3761 /* Loop skipping over invisible text. The loop is left at
3762 ZV or with IT on the first char being visible again. */
3765 /* Try to skip some invisible text. Return value is the
3766 position reached which can be equal to where we start
3767 if there is nothing invisible there. This skips both
3768 over invisible text properties and overlays with
3769 invisible property. */
3770 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3772 /* If we skipped nothing at all we weren't at invisible
3773 text in the first place. If everything to the end of
3774 the buffer was skipped, end the loop. */
3775 if (newpos == tem || newpos >= ZV)
3776 invis_p = 0;
3777 else
3779 /* We skipped some characters but not necessarily
3780 all there are. Check if we ended up on visible
3781 text. Fget_char_property returns the property of
3782 the char before the given position, i.e. if we
3783 get invis_p = 0, this means that the char at
3784 newpos is visible. */
3785 pos = make_number (newpos);
3786 prop = Fget_char_property (pos, Qinvisible, it->window);
3787 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3790 /* If we ended up on invisible text, proceed to
3791 skip starting with next_stop. */
3792 if (invis_p)
3793 tem = next_stop;
3795 /* If there are adjacent invisible texts, don't lose the
3796 second one's ellipsis. */
3797 if (invis_p == 2)
3798 display_ellipsis_p = 1;
3800 while (invis_p);
3802 /* The position newpos is now either ZV or on visible text. */
3803 if (it->bidi_p && newpos < ZV)
3805 /* With bidi iteration, the region of invisible text
3806 could start and/or end in the middle of a non-base
3807 embedding level. Therefore, we need to skip
3808 invisible text using the bidi iterator, starting at
3809 IT's current position, until we find ourselves
3810 outside the invisible text. Skipping invisible text
3811 _after_ bidi iteration avoids affecting the visual
3812 order of the displayed text when invisible properties
3813 are added or removed. */
3814 if (it->bidi_it.first_elt)
3816 /* If we were `reseat'ed to a new paragraph,
3817 determine the paragraph base direction. We need
3818 to do it now because next_element_from_buffer may
3819 not have a chance to do it, if we are going to
3820 skip any text at the beginning, which resets the
3821 FIRST_ELT flag. */
3822 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
3826 bidi_move_to_visually_next (&it->bidi_it);
3828 while (it->stop_charpos <= it->bidi_it.charpos
3829 && it->bidi_it.charpos < newpos);
3830 IT_CHARPOS (*it) = it->bidi_it.charpos;
3831 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3832 /* If we overstepped NEWPOS, record its position in the
3833 iterator, so that we skip invisible text if later the
3834 bidi iteration lands us in the invisible region
3835 again. */
3836 if (IT_CHARPOS (*it) >= newpos)
3837 it->prev_stop = newpos;
3839 else
3841 IT_CHARPOS (*it) = newpos;
3842 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3845 /* If there are before-strings at the start of invisible
3846 text, and the text is invisible because of a text
3847 property, arrange to show before-strings because 20.x did
3848 it that way. (If the text is invisible because of an
3849 overlay property instead of a text property, this is
3850 already handled in the overlay code.) */
3851 if (NILP (overlay)
3852 && get_overlay_strings (it, it->stop_charpos))
3854 handled = HANDLED_RECOMPUTE_PROPS;
3855 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3857 else if (display_ellipsis_p)
3859 /* Make sure that the glyphs of the ellipsis will get
3860 correct `charpos' values. If we would not update
3861 it->position here, the glyphs would belong to the
3862 last visible character _before_ the invisible
3863 text, which confuses `set_cursor_from_row'.
3865 We use the last invisible position instead of the
3866 first because this way the cursor is always drawn on
3867 the first "." of the ellipsis, whenever PT is inside
3868 the invisible text. Otherwise the cursor would be
3869 placed _after_ the ellipsis when the point is after the
3870 first invisible character. */
3871 if (!STRINGP (it->object))
3873 it->position.charpos = newpos - 1;
3874 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3876 it->ellipsis_p = 1;
3877 /* Let the ellipsis display before
3878 considering any properties of the following char.
3879 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3880 handled = HANDLED_RETURN;
3885 return handled;
3889 /* Make iterator IT return `...' next.
3890 Replaces LEN characters from buffer. */
3892 static void
3893 setup_for_ellipsis (struct it *it, int len)
3895 /* Use the display table definition for `...'. Invalid glyphs
3896 will be handled by the method returning elements from dpvec. */
3897 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3899 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3900 it->dpvec = v->contents;
3901 it->dpend = v->contents + v->size;
3903 else
3905 /* Default `...'. */
3906 it->dpvec = default_invis_vector;
3907 it->dpend = default_invis_vector + 3;
3910 it->dpvec_char_len = len;
3911 it->current.dpvec_index = 0;
3912 it->dpvec_face_id = -1;
3914 /* Remember the current face id in case glyphs specify faces.
3915 IT's face is restored in set_iterator_to_next.
3916 saved_face_id was set to preceding char's face in handle_stop. */
3917 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3918 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3920 it->method = GET_FROM_DISPLAY_VECTOR;
3921 it->ellipsis_p = 1;
3926 /***********************************************************************
3927 'display' property
3928 ***********************************************************************/
3930 /* Set up iterator IT from `display' property at its current position.
3931 Called from handle_stop.
3932 We return HANDLED_RETURN if some part of the display property
3933 overrides the display of the buffer text itself.
3934 Otherwise we return HANDLED_NORMALLY. */
3936 static enum prop_handled
3937 handle_display_prop (struct it *it)
3939 Lisp_Object prop, object, overlay;
3940 struct text_pos *position;
3941 /* Nonzero if some property replaces the display of the text itself. */
3942 int display_replaced_p = 0;
3944 if (STRINGP (it->string))
3946 object = it->string;
3947 position = &it->current.string_pos;
3949 else
3951 XSETWINDOW (object, it->w);
3952 position = &it->current.pos;
3955 /* Reset those iterator values set from display property values. */
3956 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3957 it->space_width = Qnil;
3958 it->font_height = Qnil;
3959 it->voffset = 0;
3961 /* We don't support recursive `display' properties, i.e. string
3962 values that have a string `display' property, that have a string
3963 `display' property etc. */
3964 if (!it->string_from_display_prop_p)
3965 it->area = TEXT_AREA;
3967 prop = get_char_property_and_overlay (make_number (position->charpos),
3968 Qdisplay, object, &overlay);
3969 if (NILP (prop))
3970 return HANDLED_NORMALLY;
3971 /* Now OVERLAY is the overlay that gave us this property, or nil
3972 if it was a text property. */
3974 if (!STRINGP (it->string))
3975 object = it->w->buffer;
3977 if (CONSP (prop)
3978 /* Simple properties. */
3979 && !EQ (XCAR (prop), Qimage)
3980 && !EQ (XCAR (prop), Qspace)
3981 && !EQ (XCAR (prop), Qwhen)
3982 && !EQ (XCAR (prop), Qslice)
3983 && !EQ (XCAR (prop), Qspace_width)
3984 && !EQ (XCAR (prop), Qheight)
3985 && !EQ (XCAR (prop), Qraise)
3986 /* Marginal area specifications. */
3987 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3988 && !EQ (XCAR (prop), Qleft_fringe)
3989 && !EQ (XCAR (prop), Qright_fringe)
3990 && !NILP (XCAR (prop)))
3992 for (; CONSP (prop); prop = XCDR (prop))
3994 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3995 position, display_replaced_p))
3997 display_replaced_p = 1;
3998 /* If some text in a string is replaced, `position' no
3999 longer points to the position of `object'. */
4000 if (STRINGP (object))
4001 break;
4005 else if (VECTORP (prop))
4007 int i;
4008 for (i = 0; i < ASIZE (prop); ++i)
4009 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4010 position, display_replaced_p))
4012 display_replaced_p = 1;
4013 /* If some text in a string is replaced, `position' no
4014 longer points to the position of `object'. */
4015 if (STRINGP (object))
4016 break;
4019 else
4021 if (handle_single_display_spec (it, prop, object, overlay,
4022 position, 0))
4023 display_replaced_p = 1;
4026 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4030 /* Value is the position of the end of the `display' property starting
4031 at START_POS in OBJECT. */
4033 static struct text_pos
4034 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4036 Lisp_Object end;
4037 struct text_pos end_pos;
4039 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4040 Qdisplay, object, Qnil);
4041 CHARPOS (end_pos) = XFASTINT (end);
4042 if (STRINGP (object))
4043 compute_string_pos (&end_pos, start_pos, it->string);
4044 else
4045 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4047 return end_pos;
4051 /* Set up IT from a single `display' specification PROP. OBJECT
4052 is the object in which the `display' property was found. *POSITION
4053 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4054 means that we previously saw a display specification which already
4055 replaced text display with something else, for example an image;
4056 we ignore such properties after the first one has been processed.
4058 OVERLAY is the overlay this `display' property came from,
4059 or nil if it was a text property.
4061 If PROP is a `space' or `image' specification, and in some other
4062 cases too, set *POSITION to the position where the `display'
4063 property ends.
4065 Value is non-zero if something was found which replaces the display
4066 of buffer or string text. */
4068 static int
4069 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4070 Lisp_Object overlay, struct text_pos *position,
4071 int display_replaced_before_p)
4073 Lisp_Object form;
4074 Lisp_Object location, value;
4075 struct text_pos start_pos, save_pos;
4076 int valid_p;
4078 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4079 If the result is non-nil, use VALUE instead of SPEC. */
4080 form = Qt;
4081 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4083 spec = XCDR (spec);
4084 if (!CONSP (spec))
4085 return 0;
4086 form = XCAR (spec);
4087 spec = XCDR (spec);
4090 if (!NILP (form) && !EQ (form, Qt))
4092 int count = SPECPDL_INDEX ();
4093 struct gcpro gcpro1;
4095 /* Bind `object' to the object having the `display' property, a
4096 buffer or string. Bind `position' to the position in the
4097 object where the property was found, and `buffer-position'
4098 to the current position in the buffer. */
4099 specbind (Qobject, object);
4100 specbind (Qposition, make_number (CHARPOS (*position)));
4101 specbind (Qbuffer_position,
4102 make_number (STRINGP (object)
4103 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4104 GCPRO1 (form);
4105 form = safe_eval (form);
4106 UNGCPRO;
4107 unbind_to (count, Qnil);
4110 if (NILP (form))
4111 return 0;
4113 /* Handle `(height HEIGHT)' specifications. */
4114 if (CONSP (spec)
4115 && EQ (XCAR (spec), Qheight)
4116 && CONSP (XCDR (spec)))
4118 if (!FRAME_WINDOW_P (it->f))
4119 return 0;
4121 it->font_height = XCAR (XCDR (spec));
4122 if (!NILP (it->font_height))
4124 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4125 int new_height = -1;
4127 if (CONSP (it->font_height)
4128 && (EQ (XCAR (it->font_height), Qplus)
4129 || EQ (XCAR (it->font_height), Qminus))
4130 && CONSP (XCDR (it->font_height))
4131 && INTEGERP (XCAR (XCDR (it->font_height))))
4133 /* `(+ N)' or `(- N)' where N is an integer. */
4134 int steps = XINT (XCAR (XCDR (it->font_height)));
4135 if (EQ (XCAR (it->font_height), Qplus))
4136 steps = - steps;
4137 it->face_id = smaller_face (it->f, it->face_id, steps);
4139 else if (FUNCTIONP (it->font_height))
4141 /* Call function with current height as argument.
4142 Value is the new height. */
4143 Lisp_Object height;
4144 height = safe_call1 (it->font_height,
4145 face->lface[LFACE_HEIGHT_INDEX]);
4146 if (NUMBERP (height))
4147 new_height = XFLOATINT (height);
4149 else if (NUMBERP (it->font_height))
4151 /* Value is a multiple of the canonical char height. */
4152 struct face *face;
4154 face = FACE_FROM_ID (it->f,
4155 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4156 new_height = (XFLOATINT (it->font_height)
4157 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4159 else
4161 /* Evaluate IT->font_height with `height' bound to the
4162 current specified height to get the new height. */
4163 int count = SPECPDL_INDEX ();
4165 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4166 value = safe_eval (it->font_height);
4167 unbind_to (count, Qnil);
4169 if (NUMBERP (value))
4170 new_height = XFLOATINT (value);
4173 if (new_height > 0)
4174 it->face_id = face_with_height (it->f, it->face_id, new_height);
4177 return 0;
4180 /* Handle `(space-width WIDTH)'. */
4181 if (CONSP (spec)
4182 && EQ (XCAR (spec), Qspace_width)
4183 && CONSP (XCDR (spec)))
4185 if (!FRAME_WINDOW_P (it->f))
4186 return 0;
4188 value = XCAR (XCDR (spec));
4189 if (NUMBERP (value) && XFLOATINT (value) > 0)
4190 it->space_width = value;
4192 return 0;
4195 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4196 if (CONSP (spec)
4197 && EQ (XCAR (spec), Qslice))
4199 Lisp_Object tem;
4201 if (!FRAME_WINDOW_P (it->f))
4202 return 0;
4204 if (tem = XCDR (spec), CONSP (tem))
4206 it->slice.x = XCAR (tem);
4207 if (tem = XCDR (tem), CONSP (tem))
4209 it->slice.y = XCAR (tem);
4210 if (tem = XCDR (tem), CONSP (tem))
4212 it->slice.width = XCAR (tem);
4213 if (tem = XCDR (tem), CONSP (tem))
4214 it->slice.height = XCAR (tem);
4219 return 0;
4222 /* Handle `(raise FACTOR)'. */
4223 if (CONSP (spec)
4224 && EQ (XCAR (spec), Qraise)
4225 && CONSP (XCDR (spec)))
4227 if (!FRAME_WINDOW_P (it->f))
4228 return 0;
4230 #ifdef HAVE_WINDOW_SYSTEM
4231 value = XCAR (XCDR (spec));
4232 if (NUMBERP (value))
4234 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4235 it->voffset = - (XFLOATINT (value)
4236 * (FONT_HEIGHT (face->font)));
4238 #endif /* HAVE_WINDOW_SYSTEM */
4240 return 0;
4243 /* Don't handle the other kinds of display specifications
4244 inside a string that we got from a `display' property. */
4245 if (it->string_from_display_prop_p)
4246 return 0;
4248 /* Characters having this form of property are not displayed, so
4249 we have to find the end of the property. */
4250 start_pos = *position;
4251 *position = display_prop_end (it, object, start_pos);
4252 value = Qnil;
4254 /* Stop the scan at that end position--we assume that all
4255 text properties change there. */
4256 it->stop_charpos = position->charpos;
4258 /* Handle `(left-fringe BITMAP [FACE])'
4259 and `(right-fringe BITMAP [FACE])'. */
4260 if (CONSP (spec)
4261 && (EQ (XCAR (spec), Qleft_fringe)
4262 || EQ (XCAR (spec), Qright_fringe))
4263 && CONSP (XCDR (spec)))
4265 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4266 int fringe_bitmap;
4268 if (!FRAME_WINDOW_P (it->f))
4269 /* If we return here, POSITION has been advanced
4270 across the text with this property. */
4271 return 0;
4273 #ifdef HAVE_WINDOW_SYSTEM
4274 value = XCAR (XCDR (spec));
4275 if (!SYMBOLP (value)
4276 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4277 /* If we return here, POSITION has been advanced
4278 across the text with this property. */
4279 return 0;
4281 if (CONSP (XCDR (XCDR (spec))))
4283 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4284 int face_id2 = lookup_derived_face (it->f, face_name,
4285 FRINGE_FACE_ID, 0);
4286 if (face_id2 >= 0)
4287 face_id = face_id2;
4290 /* Save current settings of IT so that we can restore them
4291 when we are finished with the glyph property value. */
4293 save_pos = it->position;
4294 it->position = *position;
4295 push_it (it);
4296 it->position = save_pos;
4298 it->area = TEXT_AREA;
4299 it->what = IT_IMAGE;
4300 it->image_id = -1; /* no image */
4301 it->position = start_pos;
4302 it->object = NILP (object) ? it->w->buffer : object;
4303 it->method = GET_FROM_IMAGE;
4304 it->from_overlay = Qnil;
4305 it->face_id = face_id;
4307 /* Say that we haven't consumed the characters with
4308 `display' property yet. The call to pop_it in
4309 set_iterator_to_next will clean this up. */
4310 *position = start_pos;
4312 if (EQ (XCAR (spec), Qleft_fringe))
4314 it->left_user_fringe_bitmap = fringe_bitmap;
4315 it->left_user_fringe_face_id = face_id;
4317 else
4319 it->right_user_fringe_bitmap = fringe_bitmap;
4320 it->right_user_fringe_face_id = face_id;
4322 #endif /* HAVE_WINDOW_SYSTEM */
4323 return 1;
4326 /* Prepare to handle `((margin left-margin) ...)',
4327 `((margin right-margin) ...)' and `((margin nil) ...)'
4328 prefixes for display specifications. */
4329 location = Qunbound;
4330 if (CONSP (spec) && CONSP (XCAR (spec)))
4332 Lisp_Object tem;
4334 value = XCDR (spec);
4335 if (CONSP (value))
4336 value = XCAR (value);
4338 tem = XCAR (spec);
4339 if (EQ (XCAR (tem), Qmargin)
4340 && (tem = XCDR (tem),
4341 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4342 (NILP (tem)
4343 || EQ (tem, Qleft_margin)
4344 || EQ (tem, Qright_margin))))
4345 location = tem;
4348 if (EQ (location, Qunbound))
4350 location = Qnil;
4351 value = spec;
4354 /* After this point, VALUE is the property after any
4355 margin prefix has been stripped. It must be a string,
4356 an image specification, or `(space ...)'.
4358 LOCATION specifies where to display: `left-margin',
4359 `right-margin' or nil. */
4361 valid_p = (STRINGP (value)
4362 #ifdef HAVE_WINDOW_SYSTEM
4363 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4364 #endif /* not HAVE_WINDOW_SYSTEM */
4365 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4367 if (valid_p && !display_replaced_before_p)
4369 /* Save current settings of IT so that we can restore them
4370 when we are finished with the glyph property value. */
4371 save_pos = it->position;
4372 it->position = *position;
4373 push_it (it);
4374 it->position = save_pos;
4375 it->from_overlay = overlay;
4377 if (NILP (location))
4378 it->area = TEXT_AREA;
4379 else if (EQ (location, Qleft_margin))
4380 it->area = LEFT_MARGIN_AREA;
4381 else
4382 it->area = RIGHT_MARGIN_AREA;
4384 if (STRINGP (value))
4386 it->string = value;
4387 it->multibyte_p = STRING_MULTIBYTE (it->string);
4388 it->current.overlay_string_index = -1;
4389 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4390 it->end_charpos = it->string_nchars = SCHARS (it->string);
4391 it->method = GET_FROM_STRING;
4392 it->stop_charpos = 0;
4393 it->string_from_display_prop_p = 1;
4394 /* Say that we haven't consumed the characters with
4395 `display' property yet. The call to pop_it in
4396 set_iterator_to_next will clean this up. */
4397 if (BUFFERP (object))
4398 *position = start_pos;
4400 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4402 it->method = GET_FROM_STRETCH;
4403 it->object = value;
4404 *position = it->position = start_pos;
4406 #ifdef HAVE_WINDOW_SYSTEM
4407 else
4409 it->what = IT_IMAGE;
4410 it->image_id = lookup_image (it->f, value);
4411 it->position = start_pos;
4412 it->object = NILP (object) ? it->w->buffer : object;
4413 it->method = GET_FROM_IMAGE;
4415 /* Say that we haven't consumed the characters with
4416 `display' property yet. The call to pop_it in
4417 set_iterator_to_next will clean this up. */
4418 *position = start_pos;
4420 #endif /* HAVE_WINDOW_SYSTEM */
4422 return 1;
4425 /* Invalid property or property not supported. Restore
4426 POSITION to what it was before. */
4427 *position = start_pos;
4428 return 0;
4432 /* Check if SPEC is a display sub-property value whose text should be
4433 treated as intangible. */
4435 static int
4436 single_display_spec_intangible_p (Lisp_Object prop)
4438 /* Skip over `when FORM'. */
4439 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4441 prop = XCDR (prop);
4442 if (!CONSP (prop))
4443 return 0;
4444 prop = XCDR (prop);
4447 if (STRINGP (prop))
4448 return 1;
4450 if (!CONSP (prop))
4451 return 0;
4453 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4454 we don't need to treat text as intangible. */
4455 if (EQ (XCAR (prop), Qmargin))
4457 prop = XCDR (prop);
4458 if (!CONSP (prop))
4459 return 0;
4461 prop = XCDR (prop);
4462 if (!CONSP (prop)
4463 || EQ (XCAR (prop), Qleft_margin)
4464 || EQ (XCAR (prop), Qright_margin))
4465 return 0;
4468 return (CONSP (prop)
4469 && (EQ (XCAR (prop), Qimage)
4470 || EQ (XCAR (prop), Qspace)));
4474 /* Check if PROP is a display property value whose text should be
4475 treated as intangible. */
4478 display_prop_intangible_p (Lisp_Object prop)
4480 if (CONSP (prop)
4481 && CONSP (XCAR (prop))
4482 && !EQ (Qmargin, XCAR (XCAR (prop))))
4484 /* A list of sub-properties. */
4485 while (CONSP (prop))
4487 if (single_display_spec_intangible_p (XCAR (prop)))
4488 return 1;
4489 prop = XCDR (prop);
4492 else if (VECTORP (prop))
4494 /* A vector of sub-properties. */
4495 int i;
4496 for (i = 0; i < ASIZE (prop); ++i)
4497 if (single_display_spec_intangible_p (AREF (prop, i)))
4498 return 1;
4500 else
4501 return single_display_spec_intangible_p (prop);
4503 return 0;
4507 /* Return 1 if PROP is a display sub-property value containing STRING. */
4509 static int
4510 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4512 if (EQ (string, prop))
4513 return 1;
4515 /* Skip over `when FORM'. */
4516 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4518 prop = XCDR (prop);
4519 if (!CONSP (prop))
4520 return 0;
4521 prop = XCDR (prop);
4524 if (CONSP (prop))
4525 /* Skip over `margin LOCATION'. */
4526 if (EQ (XCAR (prop), Qmargin))
4528 prop = XCDR (prop);
4529 if (!CONSP (prop))
4530 return 0;
4532 prop = XCDR (prop);
4533 if (!CONSP (prop))
4534 return 0;
4537 return CONSP (prop) && EQ (XCAR (prop), string);
4541 /* Return 1 if STRING appears in the `display' property PROP. */
4543 static int
4544 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4546 if (CONSP (prop)
4547 && CONSP (XCAR (prop))
4548 && !EQ (Qmargin, XCAR (XCAR (prop))))
4550 /* A list of sub-properties. */
4551 while (CONSP (prop))
4553 if (single_display_spec_string_p (XCAR (prop), string))
4554 return 1;
4555 prop = XCDR (prop);
4558 else if (VECTORP (prop))
4560 /* A vector of sub-properties. */
4561 int i;
4562 for (i = 0; i < ASIZE (prop); ++i)
4563 if (single_display_spec_string_p (AREF (prop, i), string))
4564 return 1;
4566 else
4567 return single_display_spec_string_p (prop, string);
4569 return 0;
4572 /* Look for STRING in overlays and text properties in W's buffer,
4573 between character positions FROM and TO (excluding TO).
4574 BACK_P non-zero means look back (in this case, TO is supposed to be
4575 less than FROM).
4576 Value is the first character position where STRING was found, or
4577 zero if it wasn't found before hitting TO.
4579 W's buffer must be current.
4581 This function may only use code that doesn't eval because it is
4582 called asynchronously from note_mouse_highlight. */
4584 static EMACS_INT
4585 string_buffer_position_lim (struct window *w, Lisp_Object string,
4586 EMACS_INT from, EMACS_INT to, int back_p)
4588 Lisp_Object limit, prop, pos;
4589 int found = 0;
4591 pos = make_number (from);
4593 if (!back_p) /* looking forward */
4595 limit = make_number (min (to, ZV));
4596 while (!found && !EQ (pos, limit))
4598 prop = Fget_char_property (pos, Qdisplay, Qnil);
4599 if (!NILP (prop) && display_prop_string_p (prop, string))
4600 found = 1;
4601 else
4602 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4603 limit);
4606 else /* looking back */
4608 limit = make_number (max (to, BEGV));
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 = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4616 limit);
4620 return found ? XINT (pos) : 0;
4623 /* Determine which buffer position in W's buffer STRING comes from.
4624 AROUND_CHARPOS is an approximate position where it could come from.
4625 Value is the buffer position or 0 if it couldn't be determined.
4627 W's buffer must be current.
4629 This function is necessary because we don't record buffer positions
4630 in glyphs generated from strings (to keep struct glyph small).
4631 This function may only use code that doesn't eval because it is
4632 called asynchronously from note_mouse_highlight. */
4634 EMACS_INT
4635 string_buffer_position (struct window *w, Lisp_Object string, EMACS_INT around_charpos)
4637 Lisp_Object limit, prop, pos;
4638 const int MAX_DISTANCE = 1000;
4639 EMACS_INT found = string_buffer_position_lim (w, string, around_charpos,
4640 around_charpos + MAX_DISTANCE,
4643 if (!found)
4644 found = string_buffer_position_lim (w, string, around_charpos,
4645 around_charpos - MAX_DISTANCE, 1);
4646 return found;
4651 /***********************************************************************
4652 `composition' property
4653 ***********************************************************************/
4655 /* Set up iterator IT from `composition' property at its current
4656 position. Called from handle_stop. */
4658 static enum prop_handled
4659 handle_composition_prop (struct it *it)
4661 Lisp_Object prop, string;
4662 EMACS_INT pos, pos_byte, start, end;
4664 if (STRINGP (it->string))
4666 unsigned char *s;
4668 pos = IT_STRING_CHARPOS (*it);
4669 pos_byte = IT_STRING_BYTEPOS (*it);
4670 string = it->string;
4671 s = SDATA (string) + pos_byte;
4672 it->c = STRING_CHAR (s);
4674 else
4676 pos = IT_CHARPOS (*it);
4677 pos_byte = IT_BYTEPOS (*it);
4678 string = Qnil;
4679 it->c = FETCH_CHAR (pos_byte);
4682 /* If there's a valid composition and point is not inside of the
4683 composition (in the case that the composition is from the current
4684 buffer), draw a glyph composed from the composition components. */
4685 if (find_composition (pos, -1, &start, &end, &prop, string)
4686 && COMPOSITION_VALID_P (start, end, prop)
4687 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4689 if (start != pos)
4691 if (STRINGP (it->string))
4692 pos_byte = string_char_to_byte (it->string, start);
4693 else
4694 pos_byte = CHAR_TO_BYTE (start);
4696 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4697 prop, string);
4699 if (it->cmp_it.id >= 0)
4701 it->cmp_it.ch = -1;
4702 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4703 it->cmp_it.nglyphs = -1;
4707 return HANDLED_NORMALLY;
4712 /***********************************************************************
4713 Overlay strings
4714 ***********************************************************************/
4716 /* The following structure is used to record overlay strings for
4717 later sorting in load_overlay_strings. */
4719 struct overlay_entry
4721 Lisp_Object overlay;
4722 Lisp_Object string;
4723 int priority;
4724 int after_string_p;
4728 /* Set up iterator IT from overlay strings at its current position.
4729 Called from handle_stop. */
4731 static enum prop_handled
4732 handle_overlay_change (struct it *it)
4734 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4735 return HANDLED_RECOMPUTE_PROPS;
4736 else
4737 return HANDLED_NORMALLY;
4741 /* Set up the next overlay string for delivery by IT, if there is an
4742 overlay string to deliver. Called by set_iterator_to_next when the
4743 end of the current overlay string is reached. If there are more
4744 overlay strings to display, IT->string and
4745 IT->current.overlay_string_index are set appropriately here.
4746 Otherwise IT->string is set to nil. */
4748 static void
4749 next_overlay_string (struct it *it)
4751 ++it->current.overlay_string_index;
4752 if (it->current.overlay_string_index == it->n_overlay_strings)
4754 /* No more overlay strings. Restore IT's settings to what
4755 they were before overlay strings were processed, and
4756 continue to deliver from current_buffer. */
4758 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4759 pop_it (it);
4760 xassert (it->sp > 0
4761 || (NILP (it->string)
4762 && it->method == GET_FROM_BUFFER
4763 && it->stop_charpos >= BEGV
4764 && it->stop_charpos <= it->end_charpos));
4765 it->current.overlay_string_index = -1;
4766 it->n_overlay_strings = 0;
4768 /* If we're at the end of the buffer, record that we have
4769 processed the overlay strings there already, so that
4770 next_element_from_buffer doesn't try it again. */
4771 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4772 it->overlay_strings_at_end_processed_p = 1;
4774 else
4776 /* There are more overlay strings to process. If
4777 IT->current.overlay_string_index has advanced to a position
4778 where we must load IT->overlay_strings with more strings, do
4779 it. */
4780 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4782 if (it->current.overlay_string_index && i == 0)
4783 load_overlay_strings (it, 0);
4785 /* Initialize IT to deliver display elements from the overlay
4786 string. */
4787 it->string = it->overlay_strings[i];
4788 it->multibyte_p = STRING_MULTIBYTE (it->string);
4789 SET_TEXT_POS (it->current.string_pos, 0, 0);
4790 it->method = GET_FROM_STRING;
4791 it->stop_charpos = 0;
4792 if (it->cmp_it.stop_pos >= 0)
4793 it->cmp_it.stop_pos = 0;
4796 CHECK_IT (it);
4800 /* Compare two overlay_entry structures E1 and E2. Used as a
4801 comparison function for qsort in load_overlay_strings. Overlay
4802 strings for the same position are sorted so that
4804 1. All after-strings come in front of before-strings, except
4805 when they come from the same overlay.
4807 2. Within after-strings, strings are sorted so that overlay strings
4808 from overlays with higher priorities come first.
4810 2. Within before-strings, strings are sorted so that overlay
4811 strings from overlays with higher priorities come last.
4813 Value is analogous to strcmp. */
4816 static int
4817 compare_overlay_entries (const void *e1, const void *e2)
4819 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4820 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4821 int result;
4823 if (entry1->after_string_p != entry2->after_string_p)
4825 /* Let after-strings appear in front of before-strings if
4826 they come from different overlays. */
4827 if (EQ (entry1->overlay, entry2->overlay))
4828 result = entry1->after_string_p ? 1 : -1;
4829 else
4830 result = entry1->after_string_p ? -1 : 1;
4832 else if (entry1->after_string_p)
4833 /* After-strings sorted in order of decreasing priority. */
4834 result = entry2->priority - entry1->priority;
4835 else
4836 /* Before-strings sorted in order of increasing priority. */
4837 result = entry1->priority - entry2->priority;
4839 return result;
4843 /* Load the vector IT->overlay_strings with overlay strings from IT's
4844 current buffer position, or from CHARPOS if that is > 0. Set
4845 IT->n_overlays to the total number of overlay strings found.
4847 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4848 a time. On entry into load_overlay_strings,
4849 IT->current.overlay_string_index gives the number of overlay
4850 strings that have already been loaded by previous calls to this
4851 function.
4853 IT->add_overlay_start contains an additional overlay start
4854 position to consider for taking overlay strings from, if non-zero.
4855 This position comes into play when the overlay has an `invisible'
4856 property, and both before and after-strings. When we've skipped to
4857 the end of the overlay, because of its `invisible' property, we
4858 nevertheless want its before-string to appear.
4859 IT->add_overlay_start will contain the overlay start position
4860 in this case.
4862 Overlay strings are sorted so that after-string strings come in
4863 front of before-string strings. Within before and after-strings,
4864 strings are sorted by overlay priority. See also function
4865 compare_overlay_entries. */
4867 static void
4868 load_overlay_strings (struct it *it, int charpos)
4870 Lisp_Object overlay, window, str, invisible;
4871 struct Lisp_Overlay *ov;
4872 int start, end;
4873 int size = 20;
4874 int n = 0, i, j, invis_p;
4875 struct overlay_entry *entries
4876 = (struct overlay_entry *) alloca (size * sizeof *entries);
4878 if (charpos <= 0)
4879 charpos = IT_CHARPOS (*it);
4881 /* Append the overlay string STRING of overlay OVERLAY to vector
4882 `entries' which has size `size' and currently contains `n'
4883 elements. AFTER_P non-zero means STRING is an after-string of
4884 OVERLAY. */
4885 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4886 do \
4888 Lisp_Object priority; \
4890 if (n == size) \
4892 int new_size = 2 * size; \
4893 struct overlay_entry *old = entries; \
4894 entries = \
4895 (struct overlay_entry *) alloca (new_size \
4896 * sizeof *entries); \
4897 memcpy (entries, old, size * sizeof *entries); \
4898 size = new_size; \
4901 entries[n].string = (STRING); \
4902 entries[n].overlay = (OVERLAY); \
4903 priority = Foverlay_get ((OVERLAY), Qpriority); \
4904 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4905 entries[n].after_string_p = (AFTER_P); \
4906 ++n; \
4908 while (0)
4910 /* Process overlay before the overlay center. */
4911 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4913 XSETMISC (overlay, ov);
4914 xassert (OVERLAYP (overlay));
4915 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4916 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4918 if (end < charpos)
4919 break;
4921 /* Skip this overlay if it doesn't start or end at IT's current
4922 position. */
4923 if (end != charpos && start != charpos)
4924 continue;
4926 /* Skip this overlay if it doesn't apply to IT->w. */
4927 window = Foverlay_get (overlay, Qwindow);
4928 if (WINDOWP (window) && XWINDOW (window) != it->w)
4929 continue;
4931 /* If the text ``under'' the overlay is invisible, both before-
4932 and after-strings from this overlay are visible; start and
4933 end position are indistinguishable. */
4934 invisible = Foverlay_get (overlay, Qinvisible);
4935 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4937 /* If overlay has a non-empty before-string, record it. */
4938 if ((start == charpos || (end == charpos && invis_p))
4939 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4940 && SCHARS (str))
4941 RECORD_OVERLAY_STRING (overlay, str, 0);
4943 /* If overlay has a non-empty after-string, record it. */
4944 if ((end == charpos || (start == charpos && invis_p))
4945 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4946 && SCHARS (str))
4947 RECORD_OVERLAY_STRING (overlay, str, 1);
4950 /* Process overlays after the overlay center. */
4951 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4953 XSETMISC (overlay, ov);
4954 xassert (OVERLAYP (overlay));
4955 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4956 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4958 if (start > charpos)
4959 break;
4961 /* Skip this overlay if it doesn't start or end at IT's current
4962 position. */
4963 if (end != charpos && start != charpos)
4964 continue;
4966 /* Skip this overlay if it doesn't apply to IT->w. */
4967 window = Foverlay_get (overlay, Qwindow);
4968 if (WINDOWP (window) && XWINDOW (window) != it->w)
4969 continue;
4971 /* If the text ``under'' the overlay is invisible, it has a zero
4972 dimension, and both before- and after-strings apply. */
4973 invisible = Foverlay_get (overlay, Qinvisible);
4974 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4976 /* If overlay has a non-empty before-string, record it. */
4977 if ((start == charpos || (end == charpos && invis_p))
4978 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4979 && SCHARS (str))
4980 RECORD_OVERLAY_STRING (overlay, str, 0);
4982 /* If overlay has a non-empty after-string, record it. */
4983 if ((end == charpos || (start == charpos && invis_p))
4984 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4985 && SCHARS (str))
4986 RECORD_OVERLAY_STRING (overlay, str, 1);
4989 #undef RECORD_OVERLAY_STRING
4991 /* Sort entries. */
4992 if (n > 1)
4993 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4995 /* Record the total number of strings to process. */
4996 it->n_overlay_strings = n;
4998 /* IT->current.overlay_string_index is the number of overlay strings
4999 that have already been consumed by IT. Copy some of the
5000 remaining overlay strings to IT->overlay_strings. */
5001 i = 0;
5002 j = it->current.overlay_string_index;
5003 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5005 it->overlay_strings[i] = entries[j].string;
5006 it->string_overlays[i++] = entries[j++].overlay;
5009 CHECK_IT (it);
5013 /* Get the first chunk of overlay strings at IT's current buffer
5014 position, or at CHARPOS if that is > 0. Value is non-zero if at
5015 least one overlay string was found. */
5017 static int
5018 get_overlay_strings_1 (struct it *it, int charpos, int compute_stop_p)
5020 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5021 process. This fills IT->overlay_strings with strings, and sets
5022 IT->n_overlay_strings to the total number of strings to process.
5023 IT->pos.overlay_string_index has to be set temporarily to zero
5024 because load_overlay_strings needs this; it must be set to -1
5025 when no overlay strings are found because a zero value would
5026 indicate a position in the first overlay string. */
5027 it->current.overlay_string_index = 0;
5028 load_overlay_strings (it, charpos);
5030 /* If we found overlay strings, set up IT to deliver display
5031 elements from the first one. Otherwise set up IT to deliver
5032 from current_buffer. */
5033 if (it->n_overlay_strings)
5035 /* Make sure we know settings in current_buffer, so that we can
5036 restore meaningful values when we're done with the overlay
5037 strings. */
5038 if (compute_stop_p)
5039 compute_stop_pos (it);
5040 xassert (it->face_id >= 0);
5042 /* Save IT's settings. They are restored after all overlay
5043 strings have been processed. */
5044 xassert (!compute_stop_p || it->sp == 0);
5046 /* When called from handle_stop, there might be an empty display
5047 string loaded. In that case, don't bother saving it. */
5048 if (!STRINGP (it->string) || SCHARS (it->string))
5049 push_it (it);
5051 /* Set up IT to deliver display elements from the first overlay
5052 string. */
5053 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5054 it->string = it->overlay_strings[0];
5055 it->from_overlay = Qnil;
5056 it->stop_charpos = 0;
5057 xassert (STRINGP (it->string));
5058 it->end_charpos = SCHARS (it->string);
5059 it->multibyte_p = STRING_MULTIBYTE (it->string);
5060 it->method = GET_FROM_STRING;
5061 return 1;
5064 it->current.overlay_string_index = -1;
5065 return 0;
5068 static int
5069 get_overlay_strings (struct it *it, int charpos)
5071 it->string = Qnil;
5072 it->method = GET_FROM_BUFFER;
5074 (void) get_overlay_strings_1 (it, charpos, 1);
5076 CHECK_IT (it);
5078 /* Value is non-zero if we found at least one overlay string. */
5079 return STRINGP (it->string);
5084 /***********************************************************************
5085 Saving and restoring state
5086 ***********************************************************************/
5088 /* Save current settings of IT on IT->stack. Called, for example,
5089 before setting up IT for an overlay string, to be able to restore
5090 IT's settings to what they were after the overlay string has been
5091 processed. */
5093 static void
5094 push_it (struct it *it)
5096 struct iterator_stack_entry *p;
5098 xassert (it->sp < IT_STACK_SIZE);
5099 p = it->stack + it->sp;
5101 p->stop_charpos = it->stop_charpos;
5102 p->prev_stop = it->prev_stop;
5103 p->base_level_stop = it->base_level_stop;
5104 p->cmp_it = it->cmp_it;
5105 xassert (it->face_id >= 0);
5106 p->face_id = it->face_id;
5107 p->string = it->string;
5108 p->method = it->method;
5109 p->from_overlay = it->from_overlay;
5110 switch (p->method)
5112 case GET_FROM_IMAGE:
5113 p->u.image.object = it->object;
5114 p->u.image.image_id = it->image_id;
5115 p->u.image.slice = it->slice;
5116 break;
5117 case GET_FROM_STRETCH:
5118 p->u.stretch.object = it->object;
5119 break;
5121 p->position = it->position;
5122 p->current = it->current;
5123 p->end_charpos = it->end_charpos;
5124 p->string_nchars = it->string_nchars;
5125 p->area = it->area;
5126 p->multibyte_p = it->multibyte_p;
5127 p->avoid_cursor_p = it->avoid_cursor_p;
5128 p->space_width = it->space_width;
5129 p->font_height = it->font_height;
5130 p->voffset = it->voffset;
5131 p->string_from_display_prop_p = it->string_from_display_prop_p;
5132 p->display_ellipsis_p = 0;
5133 p->line_wrap = it->line_wrap;
5134 ++it->sp;
5137 static void
5138 iterate_out_of_display_property (struct it *it)
5140 /* Maybe initialize paragraph direction. If we are at the beginning
5141 of a new paragraph, next_element_from_buffer may not have a
5142 chance to do that. */
5143 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
5144 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
5145 /* prev_stop can be zero, so check against BEGV as well. */
5146 while (it->bidi_it.charpos >= BEGV
5147 && it->prev_stop <= it->bidi_it.charpos
5148 && it->bidi_it.charpos < CHARPOS (it->position))
5149 bidi_move_to_visually_next (&it->bidi_it);
5150 /* Record the stop_pos we just crossed, for when we cross it
5151 back, maybe. */
5152 if (it->bidi_it.charpos > CHARPOS (it->position))
5153 it->prev_stop = CHARPOS (it->position);
5154 /* If we ended up not where pop_it put us, resync IT's
5155 positional members with the bidi iterator. */
5156 if (it->bidi_it.charpos != CHARPOS (it->position))
5158 SET_TEXT_POS (it->position,
5159 it->bidi_it.charpos, it->bidi_it.bytepos);
5160 it->current.pos = it->position;
5164 /* Restore IT's settings from IT->stack. Called, for example, when no
5165 more overlay strings must be processed, and we return to delivering
5166 display elements from a buffer, or when the end of a string from a
5167 `display' property is reached and we return to delivering display
5168 elements from an overlay string, or from a buffer. */
5170 static void
5171 pop_it (struct it *it)
5173 struct iterator_stack_entry *p;
5175 xassert (it->sp > 0);
5176 --it->sp;
5177 p = it->stack + it->sp;
5178 it->stop_charpos = p->stop_charpos;
5179 it->prev_stop = p->prev_stop;
5180 it->base_level_stop = p->base_level_stop;
5181 it->cmp_it = p->cmp_it;
5182 it->face_id = p->face_id;
5183 it->current = p->current;
5184 it->position = p->position;
5185 it->string = p->string;
5186 it->from_overlay = p->from_overlay;
5187 if (NILP (it->string))
5188 SET_TEXT_POS (it->current.string_pos, -1, -1);
5189 it->method = p->method;
5190 switch (it->method)
5192 case GET_FROM_IMAGE:
5193 it->image_id = p->u.image.image_id;
5194 it->object = p->u.image.object;
5195 it->slice = p->u.image.slice;
5196 break;
5197 case GET_FROM_STRETCH:
5198 it->object = p->u.comp.object;
5199 break;
5200 case GET_FROM_BUFFER:
5201 it->object = it->w->buffer;
5202 if (it->bidi_p)
5204 /* Bidi-iterate until we get out of the portion of text, if
5205 any, covered by a `display' text property or an overlay
5206 with `display' property. (We cannot just jump there,
5207 because the internal coherency of the bidi iterator state
5208 can not be preserved across such jumps.) We also must
5209 determine the paragraph base direction if the overlay we
5210 just processed is at the beginning of a new
5211 paragraph. */
5212 iterate_out_of_display_property (it);
5214 break;
5215 case GET_FROM_STRING:
5216 it->object = it->string;
5217 break;
5218 case GET_FROM_DISPLAY_VECTOR:
5219 if (it->s)
5220 it->method = GET_FROM_C_STRING;
5221 else if (STRINGP (it->string))
5222 it->method = GET_FROM_STRING;
5223 else
5225 it->method = GET_FROM_BUFFER;
5226 it->object = it->w->buffer;
5229 it->end_charpos = p->end_charpos;
5230 it->string_nchars = p->string_nchars;
5231 it->area = p->area;
5232 it->multibyte_p = p->multibyte_p;
5233 it->avoid_cursor_p = p->avoid_cursor_p;
5234 it->space_width = p->space_width;
5235 it->font_height = p->font_height;
5236 it->voffset = p->voffset;
5237 it->string_from_display_prop_p = p->string_from_display_prop_p;
5238 it->line_wrap = p->line_wrap;
5243 /***********************************************************************
5244 Moving over lines
5245 ***********************************************************************/
5247 /* Set IT's current position to the previous line start. */
5249 static void
5250 back_to_previous_line_start (struct it *it)
5252 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5253 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5257 /* Move IT to the next line start.
5259 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5260 we skipped over part of the text (as opposed to moving the iterator
5261 continuously over the text). Otherwise, don't change the value
5262 of *SKIPPED_P.
5264 Newlines may come from buffer text, overlay strings, or strings
5265 displayed via the `display' property. That's the reason we can't
5266 simply use find_next_newline_no_quit.
5268 Note that this function may not skip over invisible text that is so
5269 because of text properties and immediately follows a newline. If
5270 it would, function reseat_at_next_visible_line_start, when called
5271 from set_iterator_to_next, would effectively make invisible
5272 characters following a newline part of the wrong glyph row, which
5273 leads to wrong cursor motion. */
5275 static int
5276 forward_to_next_line_start (struct it *it, int *skipped_p)
5278 int old_selective, newline_found_p, n;
5279 const int MAX_NEWLINE_DISTANCE = 500;
5281 /* If already on a newline, just consume it to avoid unintended
5282 skipping over invisible text below. */
5283 if (it->what == IT_CHARACTER
5284 && it->c == '\n'
5285 && CHARPOS (it->position) == IT_CHARPOS (*it))
5287 set_iterator_to_next (it, 0);
5288 it->c = 0;
5289 return 1;
5292 /* Don't handle selective display in the following. It's (a)
5293 unnecessary because it's done by the caller, and (b) leads to an
5294 infinite recursion because next_element_from_ellipsis indirectly
5295 calls this function. */
5296 old_selective = it->selective;
5297 it->selective = 0;
5299 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5300 from buffer text. */
5301 for (n = newline_found_p = 0;
5302 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5303 n += STRINGP (it->string) ? 0 : 1)
5305 if (!get_next_display_element (it))
5306 return 0;
5307 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5308 set_iterator_to_next (it, 0);
5311 /* If we didn't find a newline near enough, see if we can use a
5312 short-cut. */
5313 if (!newline_found_p)
5315 int start = IT_CHARPOS (*it);
5316 int limit = find_next_newline_no_quit (start, 1);
5317 Lisp_Object pos;
5319 xassert (!STRINGP (it->string));
5321 /* If there isn't any `display' property in sight, and no
5322 overlays, we can just use the position of the newline in
5323 buffer text. */
5324 if (it->stop_charpos >= limit
5325 || ((pos = Fnext_single_property_change (make_number (start),
5326 Qdisplay,
5327 Qnil, make_number (limit)),
5328 NILP (pos))
5329 && next_overlay_change (start) == ZV))
5331 IT_CHARPOS (*it) = limit;
5332 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5333 *skipped_p = newline_found_p = 1;
5335 else
5337 while (get_next_display_element (it)
5338 && !newline_found_p)
5340 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5341 set_iterator_to_next (it, 0);
5346 it->selective = old_selective;
5347 return newline_found_p;
5351 /* Set IT's current position to the previous visible line start. Skip
5352 invisible text that is so either due to text properties or due to
5353 selective display. Caution: this does not change IT->current_x and
5354 IT->hpos. */
5356 static void
5357 back_to_previous_visible_line_start (struct it *it)
5359 while (IT_CHARPOS (*it) > BEGV)
5361 back_to_previous_line_start (it);
5363 if (IT_CHARPOS (*it) <= BEGV)
5364 break;
5366 /* If selective > 0, then lines indented more than its value are
5367 invisible. */
5368 if (it->selective > 0
5369 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5370 (double) it->selective)) /* iftc */
5371 continue;
5373 /* Check the newline before point for invisibility. */
5375 Lisp_Object prop;
5376 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5377 Qinvisible, it->window);
5378 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5379 continue;
5382 if (IT_CHARPOS (*it) <= BEGV)
5383 break;
5386 struct it it2;
5387 int pos;
5388 EMACS_INT beg, end;
5389 Lisp_Object val, overlay;
5391 /* If newline is part of a composition, continue from start of composition */
5392 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5393 && beg < IT_CHARPOS (*it))
5394 goto replaced;
5396 /* If newline is replaced by a display property, find start of overlay
5397 or interval and continue search from that point. */
5398 it2 = *it;
5399 pos = --IT_CHARPOS (it2);
5400 --IT_BYTEPOS (it2);
5401 it2.sp = 0;
5402 it2.string_from_display_prop_p = 0;
5403 if (handle_display_prop (&it2) == HANDLED_RETURN
5404 && !NILP (val = get_char_property_and_overlay
5405 (make_number (pos), Qdisplay, Qnil, &overlay))
5406 && (OVERLAYP (overlay)
5407 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5408 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5409 goto replaced;
5411 /* Newline is not replaced by anything -- so we are done. */
5412 break;
5414 replaced:
5415 if (beg < BEGV)
5416 beg = BEGV;
5417 IT_CHARPOS (*it) = beg;
5418 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5422 it->continuation_lines_width = 0;
5424 xassert (IT_CHARPOS (*it) >= BEGV);
5425 xassert (IT_CHARPOS (*it) == BEGV
5426 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5427 CHECK_IT (it);
5431 /* Reseat iterator IT at the previous visible line start. Skip
5432 invisible text that is so either due to text properties or due to
5433 selective display. At the end, update IT's overlay information,
5434 face information etc. */
5436 void
5437 reseat_at_previous_visible_line_start (struct it *it)
5439 back_to_previous_visible_line_start (it);
5440 reseat (it, it->current.pos, 1);
5441 CHECK_IT (it);
5445 /* Reseat iterator IT on the next visible line start in the current
5446 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5447 preceding the line start. Skip over invisible text that is so
5448 because of selective display. Compute faces, overlays etc at the
5449 new position. Note that this function does not skip over text that
5450 is invisible because of text properties. */
5452 static void
5453 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5455 int newline_found_p, skipped_p = 0;
5457 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5459 /* Skip over lines that are invisible because they are indented
5460 more than the value of IT->selective. */
5461 if (it->selective > 0)
5462 while (IT_CHARPOS (*it) < ZV
5463 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5464 (double) it->selective)) /* iftc */
5466 xassert (IT_BYTEPOS (*it) == BEGV
5467 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5468 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5471 /* Position on the newline if that's what's requested. */
5472 if (on_newline_p && newline_found_p)
5474 if (STRINGP (it->string))
5476 if (IT_STRING_CHARPOS (*it) > 0)
5478 --IT_STRING_CHARPOS (*it);
5479 --IT_STRING_BYTEPOS (*it);
5482 else if (IT_CHARPOS (*it) > BEGV)
5484 --IT_CHARPOS (*it);
5485 --IT_BYTEPOS (*it);
5486 reseat (it, it->current.pos, 0);
5489 else if (skipped_p)
5490 reseat (it, it->current.pos, 0);
5492 CHECK_IT (it);
5497 /***********************************************************************
5498 Changing an iterator's position
5499 ***********************************************************************/
5501 /* Change IT's current position to POS in current_buffer. If FORCE_P
5502 is non-zero, always check for text properties at the new position.
5503 Otherwise, text properties are only looked up if POS >=
5504 IT->check_charpos of a property. */
5506 static void
5507 reseat (struct it *it, struct text_pos pos, int force_p)
5509 int original_pos = IT_CHARPOS (*it);
5511 reseat_1 (it, pos, 0);
5513 /* Determine where to check text properties. Avoid doing it
5514 where possible because text property lookup is very expensive. */
5515 if (force_p
5516 || CHARPOS (pos) > it->stop_charpos
5517 || CHARPOS (pos) < original_pos)
5519 if (it->bidi_p)
5521 /* For bidi iteration, we need to prime prev_stop and
5522 base_level_stop with our best estimations. */
5523 if (CHARPOS (pos) < it->prev_stop)
5525 handle_stop_backwards (it, BEGV);
5526 if (CHARPOS (pos) < it->base_level_stop)
5527 it->base_level_stop = 0;
5529 else if (CHARPOS (pos) > it->stop_charpos
5530 && it->stop_charpos >= BEGV)
5531 handle_stop_backwards (it, it->stop_charpos);
5532 else /* force_p */
5533 handle_stop (it);
5535 else
5537 handle_stop (it);
5538 it->prev_stop = it->base_level_stop = 0;
5543 CHECK_IT (it);
5547 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5548 IT->stop_pos to POS, also. */
5550 static void
5551 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5553 /* Don't call this function when scanning a C string. */
5554 xassert (it->s == NULL);
5556 /* POS must be a reasonable value. */
5557 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5559 it->current.pos = it->position = pos;
5560 it->end_charpos = ZV;
5561 it->dpvec = NULL;
5562 it->current.dpvec_index = -1;
5563 it->current.overlay_string_index = -1;
5564 IT_STRING_CHARPOS (*it) = -1;
5565 IT_STRING_BYTEPOS (*it) = -1;
5566 it->string = Qnil;
5567 it->string_from_display_prop_p = 0;
5568 it->method = GET_FROM_BUFFER;
5569 it->object = it->w->buffer;
5570 it->area = TEXT_AREA;
5571 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5572 it->sp = 0;
5573 it->string_from_display_prop_p = 0;
5574 it->face_before_selective_p = 0;
5575 if (it->bidi_p)
5576 it->bidi_it.first_elt = 1;
5578 if (set_stop_p)
5580 it->stop_charpos = CHARPOS (pos);
5581 it->base_level_stop = CHARPOS (pos);
5586 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5587 If S is non-null, it is a C string to iterate over. Otherwise,
5588 STRING gives a Lisp string to iterate over.
5590 If PRECISION > 0, don't return more then PRECISION number of
5591 characters from the string.
5593 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5594 characters have been returned. FIELD_WIDTH < 0 means an infinite
5595 field width.
5597 MULTIBYTE = 0 means disable processing of multibyte characters,
5598 MULTIBYTE > 0 means enable it,
5599 MULTIBYTE < 0 means use IT->multibyte_p.
5601 IT must be initialized via a prior call to init_iterator before
5602 calling this function. */
5604 static void
5605 reseat_to_string (struct it *it, unsigned char *s, Lisp_Object string,
5606 int charpos, int precision, int field_width, int multibyte)
5608 /* No region in strings. */
5609 it->region_beg_charpos = it->region_end_charpos = -1;
5611 /* No text property checks performed by default, but see below. */
5612 it->stop_charpos = -1;
5614 /* Set iterator position and end position. */
5615 memset (&it->current, 0, sizeof it->current);
5616 it->current.overlay_string_index = -1;
5617 it->current.dpvec_index = -1;
5618 xassert (charpos >= 0);
5620 /* If STRING is specified, use its multibyteness, otherwise use the
5621 setting of MULTIBYTE, if specified. */
5622 if (multibyte >= 0)
5623 it->multibyte_p = multibyte > 0;
5625 if (s == NULL)
5627 xassert (STRINGP (string));
5628 it->string = string;
5629 it->s = NULL;
5630 it->end_charpos = it->string_nchars = SCHARS (string);
5631 it->method = GET_FROM_STRING;
5632 it->current.string_pos = string_pos (charpos, string);
5634 else
5636 it->s = s;
5637 it->string = Qnil;
5639 /* Note that we use IT->current.pos, not it->current.string_pos,
5640 for displaying C strings. */
5641 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5642 if (it->multibyte_p)
5644 it->current.pos = c_string_pos (charpos, s, 1);
5645 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5647 else
5649 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5650 it->end_charpos = it->string_nchars = strlen (s);
5653 it->method = GET_FROM_C_STRING;
5656 /* PRECISION > 0 means don't return more than PRECISION characters
5657 from the string. */
5658 if (precision > 0 && it->end_charpos - charpos > precision)
5659 it->end_charpos = it->string_nchars = charpos + precision;
5661 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5662 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5663 FIELD_WIDTH < 0 means infinite field width. This is useful for
5664 padding with `-' at the end of a mode line. */
5665 if (field_width < 0)
5666 field_width = INFINITY;
5667 if (field_width > it->end_charpos - charpos)
5668 it->end_charpos = charpos + field_width;
5670 /* Use the standard display table for displaying strings. */
5671 if (DISP_TABLE_P (Vstandard_display_table))
5672 it->dp = XCHAR_TABLE (Vstandard_display_table);
5674 it->stop_charpos = charpos;
5675 if (s == NULL && it->multibyte_p)
5677 EMACS_INT endpos = SCHARS (it->string);
5678 if (endpos > it->end_charpos)
5679 endpos = it->end_charpos;
5680 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5681 it->string);
5683 CHECK_IT (it);
5688 /***********************************************************************
5689 Iteration
5690 ***********************************************************************/
5692 /* Map enum it_method value to corresponding next_element_from_* function. */
5694 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5696 next_element_from_buffer,
5697 next_element_from_display_vector,
5698 next_element_from_string,
5699 next_element_from_c_string,
5700 next_element_from_image,
5701 next_element_from_stretch
5704 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5707 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5708 (possibly with the following characters). */
5710 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5711 ((IT)->cmp_it.id >= 0 \
5712 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5713 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5714 END_CHARPOS, (IT)->w, \
5715 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5716 (IT)->string)))
5719 /* Load IT's display element fields with information about the next
5720 display element from the current position of IT. Value is zero if
5721 end of buffer (or C string) is reached. */
5723 static struct frame *last_escape_glyph_frame = NULL;
5724 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5725 static int last_escape_glyph_merged_face_id = 0;
5728 get_next_display_element (struct it *it)
5730 /* Non-zero means that we found a display element. Zero means that
5731 we hit the end of what we iterate over. Performance note: the
5732 function pointer `method' used here turns out to be faster than
5733 using a sequence of if-statements. */
5734 int success_p;
5736 get_next:
5737 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5739 if (it->what == IT_CHARACTER)
5741 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
5742 and only if (a) the resolved directionality of that character
5743 is R..." */
5744 /* FIXME: Do we need an exception for characters from display
5745 tables? */
5746 if (it->bidi_p && it->bidi_it.type == STRONG_R)
5747 it->c = bidi_mirror_char (it->c);
5748 /* Map via display table or translate control characters.
5749 IT->c, IT->len etc. have been set to the next character by
5750 the function call above. If we have a display table, and it
5751 contains an entry for IT->c, translate it. Don't do this if
5752 IT->c itself comes from a display table, otherwise we could
5753 end up in an infinite recursion. (An alternative could be to
5754 count the recursion depth of this function and signal an
5755 error when a certain maximum depth is reached.) Is it worth
5756 it? */
5757 if (success_p && it->dpvec == NULL)
5759 Lisp_Object dv;
5760 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5761 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5762 nbsp_or_shy = char_is_other;
5763 int decoded = it->c;
5765 if (it->dp
5766 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5767 VECTORP (dv)))
5769 struct Lisp_Vector *v = XVECTOR (dv);
5771 /* Return the first character from the display table
5772 entry, if not empty. If empty, don't display the
5773 current character. */
5774 if (v->size)
5776 it->dpvec_char_len = it->len;
5777 it->dpvec = v->contents;
5778 it->dpend = v->contents + v->size;
5779 it->current.dpvec_index = 0;
5780 it->dpvec_face_id = -1;
5781 it->saved_face_id = it->face_id;
5782 it->method = GET_FROM_DISPLAY_VECTOR;
5783 it->ellipsis_p = 0;
5785 else
5787 set_iterator_to_next (it, 0);
5789 goto get_next;
5792 if (unibyte_display_via_language_environment
5793 && !ASCII_CHAR_P (it->c))
5794 decoded = DECODE_CHAR (unibyte, it->c);
5796 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5798 if (it->multibyte_p)
5799 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5800 : it->c == 0xAD ? char_is_soft_hyphen
5801 : char_is_other);
5802 else if (unibyte_display_via_language_environment)
5803 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5804 : decoded == 0xAD ? char_is_soft_hyphen
5805 : char_is_other);
5808 /* Translate control characters into `\003' or `^C' form.
5809 Control characters coming from a display table entry are
5810 currently not translated because we use IT->dpvec to hold
5811 the translation. This could easily be changed but I
5812 don't believe that it is worth doing.
5814 If it->multibyte_p is nonzero, non-printable non-ASCII
5815 characters are also translated to octal form.
5817 If it->multibyte_p is zero, eight-bit characters that
5818 don't have corresponding multibyte char code are also
5819 translated to octal form. */
5820 if ((it->c < ' '
5821 ? (it->area != TEXT_AREA
5822 /* In mode line, treat \n, \t like other crl chars. */
5823 || (it->c != '\t'
5824 && it->glyph_row
5825 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5826 || (it->c != '\n' && it->c != '\t'))
5827 : (nbsp_or_shy
5828 || (it->multibyte_p
5829 ? ! CHAR_PRINTABLE_P (it->c)
5830 : (! unibyte_display_via_language_environment
5831 ? it->c >= 0x80
5832 : (decoded >= 0x80 && decoded < 0xA0))))))
5834 /* IT->c is a control character which must be displayed
5835 either as '\003' or as `^C' where the '\\' and '^'
5836 can be defined in the display table. Fill
5837 IT->ctl_chars with glyphs for what we have to
5838 display. Then, set IT->dpvec to these glyphs. */
5839 Lisp_Object gc;
5840 int ctl_len;
5841 int face_id, lface_id = 0 ;
5842 int escape_glyph;
5844 /* Handle control characters with ^. */
5846 if (it->c < 128 && it->ctl_arrow_p)
5848 int g;
5850 g = '^'; /* default glyph for Control */
5851 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5852 if (it->dp
5853 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5854 && GLYPH_CODE_CHAR_VALID_P (gc))
5856 g = GLYPH_CODE_CHAR (gc);
5857 lface_id = GLYPH_CODE_FACE (gc);
5859 if (lface_id)
5861 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5863 else if (it->f == last_escape_glyph_frame
5864 && it->face_id == last_escape_glyph_face_id)
5866 face_id = last_escape_glyph_merged_face_id;
5868 else
5870 /* Merge the escape-glyph face into the current face. */
5871 face_id = merge_faces (it->f, Qescape_glyph, 0,
5872 it->face_id);
5873 last_escape_glyph_frame = it->f;
5874 last_escape_glyph_face_id = it->face_id;
5875 last_escape_glyph_merged_face_id = face_id;
5878 XSETINT (it->ctl_chars[0], g);
5879 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5880 ctl_len = 2;
5881 goto display_control;
5884 /* Handle non-break space in the mode where it only gets
5885 highlighting. */
5887 if (EQ (Vnobreak_char_display, Qt)
5888 && nbsp_or_shy == char_is_nbsp)
5890 /* Merge the no-break-space face into the current face. */
5891 face_id = merge_faces (it->f, Qnobreak_space, 0,
5892 it->face_id);
5894 it->c = ' ';
5895 XSETINT (it->ctl_chars[0], ' ');
5896 ctl_len = 1;
5897 goto display_control;
5900 /* Handle sequences that start with the "escape glyph". */
5902 /* the default escape glyph is \. */
5903 escape_glyph = '\\';
5905 if (it->dp
5906 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5907 && GLYPH_CODE_CHAR_VALID_P (gc))
5909 escape_glyph = GLYPH_CODE_CHAR (gc);
5910 lface_id = GLYPH_CODE_FACE (gc);
5912 if (lface_id)
5914 /* The display table specified a face.
5915 Merge it into face_id and also into escape_glyph. */
5916 face_id = merge_faces (it->f, Qt, lface_id,
5917 it->face_id);
5919 else if (it->f == last_escape_glyph_frame
5920 && it->face_id == last_escape_glyph_face_id)
5922 face_id = last_escape_glyph_merged_face_id;
5924 else
5926 /* Merge the escape-glyph face into the current face. */
5927 face_id = merge_faces (it->f, Qescape_glyph, 0,
5928 it->face_id);
5929 last_escape_glyph_frame = it->f;
5930 last_escape_glyph_face_id = it->face_id;
5931 last_escape_glyph_merged_face_id = face_id;
5934 /* Handle soft hyphens in the mode where they only get
5935 highlighting. */
5937 if (EQ (Vnobreak_char_display, Qt)
5938 && nbsp_or_shy == char_is_soft_hyphen)
5940 it->c = '-';
5941 XSETINT (it->ctl_chars[0], '-');
5942 ctl_len = 1;
5943 goto display_control;
5946 /* Handle non-break space and soft hyphen
5947 with the escape glyph. */
5949 if (nbsp_or_shy)
5951 XSETINT (it->ctl_chars[0], escape_glyph);
5952 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5953 XSETINT (it->ctl_chars[1], it->c);
5954 ctl_len = 2;
5955 goto display_control;
5959 unsigned char str[MAX_MULTIBYTE_LENGTH];
5960 int len;
5961 int i;
5963 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5964 if (CHAR_BYTE8_P (it->c))
5966 str[0] = CHAR_TO_BYTE8 (it->c);
5967 len = 1;
5969 else if (it->c < 256)
5971 str[0] = it->c;
5972 len = 1;
5974 else
5976 /* It's an invalid character, which shouldn't
5977 happen actually, but due to bugs it may
5978 happen. Let's print the char as is, there's
5979 not much meaningful we can do with it. */
5980 str[0] = it->c;
5981 str[1] = it->c >> 8;
5982 str[2] = it->c >> 16;
5983 str[3] = it->c >> 24;
5984 len = 4;
5987 for (i = 0; i < len; i++)
5989 int g;
5990 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5991 /* Insert three more glyphs into IT->ctl_chars for
5992 the octal display of the character. */
5993 g = ((str[i] >> 6) & 7) + '0';
5994 XSETINT (it->ctl_chars[i * 4 + 1], g);
5995 g = ((str[i] >> 3) & 7) + '0';
5996 XSETINT (it->ctl_chars[i * 4 + 2], g);
5997 g = (str[i] & 7) + '0';
5998 XSETINT (it->ctl_chars[i * 4 + 3], g);
6000 ctl_len = len * 4;
6003 display_control:
6004 /* Set up IT->dpvec and return first character from it. */
6005 it->dpvec_char_len = it->len;
6006 it->dpvec = it->ctl_chars;
6007 it->dpend = it->dpvec + ctl_len;
6008 it->current.dpvec_index = 0;
6009 it->dpvec_face_id = face_id;
6010 it->saved_face_id = it->face_id;
6011 it->method = GET_FROM_DISPLAY_VECTOR;
6012 it->ellipsis_p = 0;
6013 goto get_next;
6018 #ifdef HAVE_WINDOW_SYSTEM
6019 /* Adjust face id for a multibyte character. There are no multibyte
6020 character in unibyte text. */
6021 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6022 && it->multibyte_p
6023 && success_p
6024 && FRAME_WINDOW_P (it->f))
6026 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6028 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6030 /* Automatic composition with glyph-string. */
6031 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6033 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6035 else
6037 int pos = (it->s ? -1
6038 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6039 : IT_CHARPOS (*it));
6041 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6044 #endif
6046 /* Is this character the last one of a run of characters with
6047 box? If yes, set IT->end_of_box_run_p to 1. */
6048 if (it->face_box_p
6049 && it->s == NULL)
6051 if (it->method == GET_FROM_STRING && it->sp)
6053 int face_id = underlying_face_id (it);
6054 struct face *face = FACE_FROM_ID (it->f, face_id);
6056 if (face)
6058 if (face->box == FACE_NO_BOX)
6060 /* If the box comes from face properties in a
6061 display string, check faces in that string. */
6062 int string_face_id = face_after_it_pos (it);
6063 it->end_of_box_run_p
6064 = (FACE_FROM_ID (it->f, string_face_id)->box
6065 == FACE_NO_BOX);
6067 /* Otherwise, the box comes from the underlying face.
6068 If this is the last string character displayed, check
6069 the next buffer location. */
6070 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6071 && (it->current.overlay_string_index
6072 == it->n_overlay_strings - 1))
6074 EMACS_INT ignore;
6075 int next_face_id;
6076 struct text_pos pos = it->current.pos;
6077 INC_TEXT_POS (pos, it->multibyte_p);
6079 next_face_id = face_at_buffer_position
6080 (it->w, CHARPOS (pos), it->region_beg_charpos,
6081 it->region_end_charpos, &ignore,
6082 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6083 -1);
6084 it->end_of_box_run_p
6085 = (FACE_FROM_ID (it->f, next_face_id)->box
6086 == FACE_NO_BOX);
6090 else
6092 int face_id = face_after_it_pos (it);
6093 it->end_of_box_run_p
6094 = (face_id != it->face_id
6095 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6099 /* Value is 0 if end of buffer or string reached. */
6100 return success_p;
6104 /* Move IT to the next display element.
6106 RESEAT_P non-zero means if called on a newline in buffer text,
6107 skip to the next visible line start.
6109 Functions get_next_display_element and set_iterator_to_next are
6110 separate because I find this arrangement easier to handle than a
6111 get_next_display_element function that also increments IT's
6112 position. The way it is we can first look at an iterator's current
6113 display element, decide whether it fits on a line, and if it does,
6114 increment the iterator position. The other way around we probably
6115 would either need a flag indicating whether the iterator has to be
6116 incremented the next time, or we would have to implement a
6117 decrement position function which would not be easy to write. */
6119 void
6120 set_iterator_to_next (struct it *it, int reseat_p)
6122 /* Reset flags indicating start and end of a sequence of characters
6123 with box. Reset them at the start of this function because
6124 moving the iterator to a new position might set them. */
6125 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6127 switch (it->method)
6129 case GET_FROM_BUFFER:
6130 /* The current display element of IT is a character from
6131 current_buffer. Advance in the buffer, and maybe skip over
6132 invisible lines that are so because of selective display. */
6133 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6134 reseat_at_next_visible_line_start (it, 0);
6135 else if (it->cmp_it.id >= 0)
6137 /* We are currently getting glyphs from a composition. */
6138 int i;
6140 if (! it->bidi_p)
6142 IT_CHARPOS (*it) += it->cmp_it.nchars;
6143 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6144 if (it->cmp_it.to < it->cmp_it.nglyphs)
6146 it->cmp_it.from = it->cmp_it.to;
6148 else
6150 it->cmp_it.id = -1;
6151 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6152 IT_BYTEPOS (*it),
6153 it->stop_charpos, Qnil);
6156 else if (! it->cmp_it.reversed_p)
6158 /* Composition created while scanning forward. */
6159 /* Update IT's char/byte positions to point to the first
6160 character of the next grapheme cluster, or to the
6161 character visually after the current composition. */
6162 for (i = 0; i < it->cmp_it.nchars; i++)
6163 bidi_move_to_visually_next (&it->bidi_it);
6164 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6165 IT_CHARPOS (*it) = it->bidi_it.charpos;
6167 if (it->cmp_it.to < it->cmp_it.nglyphs)
6169 /* Proceed to the next grapheme cluster. */
6170 it->cmp_it.from = it->cmp_it.to;
6172 else
6174 /* No more grapheme clusters in this composition.
6175 Find the next stop position. */
6176 EMACS_INT stop = it->stop_charpos;
6177 if (it->bidi_it.scan_dir < 0)
6178 /* Now we are scanning backward and don't know
6179 where to stop. */
6180 stop = -1;
6181 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6182 IT_BYTEPOS (*it), stop, Qnil);
6185 else
6187 /* Composition created while scanning backward. */
6188 /* Update IT's char/byte positions to point to the last
6189 character of the previous grapheme cluster, or the
6190 character visually after the current composition. */
6191 for (i = 0; i < it->cmp_it.nchars; i++)
6192 bidi_move_to_visually_next (&it->bidi_it);
6193 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6194 IT_CHARPOS (*it) = it->bidi_it.charpos;
6195 if (it->cmp_it.from > 0)
6197 /* Proceed to the previous grapheme cluster. */
6198 it->cmp_it.to = it->cmp_it.from;
6200 else
6202 /* No more grapheme clusters in this composition.
6203 Find the next stop position. */
6204 EMACS_INT stop = it->stop_charpos;
6205 if (it->bidi_it.scan_dir < 0)
6206 /* Now we are scanning backward and don't know
6207 where to stop. */
6208 stop = -1;
6209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6210 IT_BYTEPOS (*it), stop, Qnil);
6214 else
6216 xassert (it->len != 0);
6218 if (!it->bidi_p)
6220 IT_BYTEPOS (*it) += it->len;
6221 IT_CHARPOS (*it) += 1;
6223 else
6225 int prev_scan_dir = it->bidi_it.scan_dir;
6226 /* If this is a new paragraph, determine its base
6227 direction (a.k.a. its base embedding level). */
6228 if (it->bidi_it.new_paragraph)
6229 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6230 bidi_move_to_visually_next (&it->bidi_it);
6231 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6232 IT_CHARPOS (*it) = it->bidi_it.charpos;
6233 if (prev_scan_dir != it->bidi_it.scan_dir)
6235 /* As the scan direction was changed, we must
6236 re-compute the stop position for composition. */
6237 EMACS_INT stop = it->stop_charpos;
6238 if (it->bidi_it.scan_dir < 0)
6239 stop = -1;
6240 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6241 IT_BYTEPOS (*it), stop, Qnil);
6244 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6246 break;
6248 case GET_FROM_C_STRING:
6249 /* Current display element of IT is from a C string. */
6250 IT_BYTEPOS (*it) += it->len;
6251 IT_CHARPOS (*it) += 1;
6252 break;
6254 case GET_FROM_DISPLAY_VECTOR:
6255 /* Current display element of IT is from a display table entry.
6256 Advance in the display table definition. Reset it to null if
6257 end reached, and continue with characters from buffers/
6258 strings. */
6259 ++it->current.dpvec_index;
6261 /* Restore face of the iterator to what they were before the
6262 display vector entry (these entries may contain faces). */
6263 it->face_id = it->saved_face_id;
6265 if (it->dpvec + it->current.dpvec_index == it->dpend)
6267 int recheck_faces = it->ellipsis_p;
6269 if (it->s)
6270 it->method = GET_FROM_C_STRING;
6271 else if (STRINGP (it->string))
6272 it->method = GET_FROM_STRING;
6273 else
6275 it->method = GET_FROM_BUFFER;
6276 it->object = it->w->buffer;
6279 it->dpvec = NULL;
6280 it->current.dpvec_index = -1;
6282 /* Skip over characters which were displayed via IT->dpvec. */
6283 if (it->dpvec_char_len < 0)
6284 reseat_at_next_visible_line_start (it, 1);
6285 else if (it->dpvec_char_len > 0)
6287 if (it->method == GET_FROM_STRING
6288 && it->n_overlay_strings > 0)
6289 it->ignore_overlay_strings_at_pos_p = 1;
6290 it->len = it->dpvec_char_len;
6291 set_iterator_to_next (it, reseat_p);
6294 /* Maybe recheck faces after display vector */
6295 if (recheck_faces)
6296 it->stop_charpos = IT_CHARPOS (*it);
6298 break;
6300 case GET_FROM_STRING:
6301 /* Current display element is a character from a Lisp string. */
6302 xassert (it->s == NULL && STRINGP (it->string));
6303 if (it->cmp_it.id >= 0)
6305 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6306 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6307 if (it->cmp_it.to < it->cmp_it.nglyphs)
6308 it->cmp_it.from = it->cmp_it.to;
6309 else
6311 it->cmp_it.id = -1;
6312 composition_compute_stop_pos (&it->cmp_it,
6313 IT_STRING_CHARPOS (*it),
6314 IT_STRING_BYTEPOS (*it),
6315 it->stop_charpos, it->string);
6318 else
6320 IT_STRING_BYTEPOS (*it) += it->len;
6321 IT_STRING_CHARPOS (*it) += 1;
6324 consider_string_end:
6326 if (it->current.overlay_string_index >= 0)
6328 /* IT->string is an overlay string. Advance to the
6329 next, if there is one. */
6330 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6332 it->ellipsis_p = 0;
6333 next_overlay_string (it);
6334 if (it->ellipsis_p)
6335 setup_for_ellipsis (it, 0);
6338 else
6340 /* IT->string is not an overlay string. If we reached
6341 its end, and there is something on IT->stack, proceed
6342 with what is on the stack. This can be either another
6343 string, this time an overlay string, or a buffer. */
6344 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6345 && it->sp > 0)
6347 pop_it (it);
6348 if (it->method == GET_FROM_STRING)
6349 goto consider_string_end;
6352 break;
6354 case GET_FROM_IMAGE:
6355 case GET_FROM_STRETCH:
6356 /* The position etc with which we have to proceed are on
6357 the stack. The position may be at the end of a string,
6358 if the `display' property takes up the whole string. */
6359 xassert (it->sp > 0);
6360 pop_it (it);
6361 if (it->method == GET_FROM_STRING)
6362 goto consider_string_end;
6363 break;
6365 default:
6366 /* There are no other methods defined, so this should be a bug. */
6367 abort ();
6370 xassert (it->method != GET_FROM_STRING
6371 || (STRINGP (it->string)
6372 && IT_STRING_CHARPOS (*it) >= 0));
6375 /* Load IT's display element fields with information about the next
6376 display element which comes from a display table entry or from the
6377 result of translating a control character to one of the forms `^C'
6378 or `\003'.
6380 IT->dpvec holds the glyphs to return as characters.
6381 IT->saved_face_id holds the face id before the display vector--it
6382 is restored into IT->face_id in set_iterator_to_next. */
6384 static int
6385 next_element_from_display_vector (struct it *it)
6387 Lisp_Object gc;
6389 /* Precondition. */
6390 xassert (it->dpvec && it->current.dpvec_index >= 0);
6392 it->face_id = it->saved_face_id;
6394 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6395 That seemed totally bogus - so I changed it... */
6396 gc = it->dpvec[it->current.dpvec_index];
6398 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6400 it->c = GLYPH_CODE_CHAR (gc);
6401 it->len = CHAR_BYTES (it->c);
6403 /* The entry may contain a face id to use. Such a face id is
6404 the id of a Lisp face, not a realized face. A face id of
6405 zero means no face is specified. */
6406 if (it->dpvec_face_id >= 0)
6407 it->face_id = it->dpvec_face_id;
6408 else
6410 int lface_id = GLYPH_CODE_FACE (gc);
6411 if (lface_id > 0)
6412 it->face_id = merge_faces (it->f, Qt, lface_id,
6413 it->saved_face_id);
6416 else
6417 /* Display table entry is invalid. Return a space. */
6418 it->c = ' ', it->len = 1;
6420 /* Don't change position and object of the iterator here. They are
6421 still the values of the character that had this display table
6422 entry or was translated, and that's what we want. */
6423 it->what = IT_CHARACTER;
6424 return 1;
6428 /* Load IT with the next display element from Lisp string IT->string.
6429 IT->current.string_pos is the current position within the string.
6430 If IT->current.overlay_string_index >= 0, the Lisp string is an
6431 overlay string. */
6433 static int
6434 next_element_from_string (struct it *it)
6436 struct text_pos position;
6438 xassert (STRINGP (it->string));
6439 xassert (IT_STRING_CHARPOS (*it) >= 0);
6440 position = it->current.string_pos;
6442 /* Time to check for invisible text? */
6443 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6444 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6446 handle_stop (it);
6448 /* Since a handler may have changed IT->method, we must
6449 recurse here. */
6450 return GET_NEXT_DISPLAY_ELEMENT (it);
6453 if (it->current.overlay_string_index >= 0)
6455 /* Get the next character from an overlay string. In overlay
6456 strings, There is no field width or padding with spaces to
6457 do. */
6458 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6460 it->what = IT_EOB;
6461 return 0;
6463 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6464 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6465 && next_element_from_composition (it))
6467 return 1;
6469 else if (STRING_MULTIBYTE (it->string))
6471 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6472 const unsigned char *s = (SDATA (it->string)
6473 + IT_STRING_BYTEPOS (*it));
6474 it->c = string_char_and_length (s, &it->len);
6476 else
6478 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6479 it->len = 1;
6482 else
6484 /* Get the next character from a Lisp string that is not an
6485 overlay string. Such strings come from the mode line, for
6486 example. We may have to pad with spaces, or truncate the
6487 string. See also next_element_from_c_string. */
6488 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6490 it->what = IT_EOB;
6491 return 0;
6493 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6495 /* Pad with spaces. */
6496 it->c = ' ', it->len = 1;
6497 CHARPOS (position) = BYTEPOS (position) = -1;
6499 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6500 IT_STRING_BYTEPOS (*it), it->string_nchars)
6501 && next_element_from_composition (it))
6503 return 1;
6505 else if (STRING_MULTIBYTE (it->string))
6507 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6508 const unsigned char *s = (SDATA (it->string)
6509 + IT_STRING_BYTEPOS (*it));
6510 it->c = string_char_and_length (s, &it->len);
6512 else
6514 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6515 it->len = 1;
6519 /* Record what we have and where it came from. */
6520 it->what = IT_CHARACTER;
6521 it->object = it->string;
6522 it->position = position;
6523 return 1;
6527 /* Load IT with next display element from C string IT->s.
6528 IT->string_nchars is the maximum number of characters to return
6529 from the string. IT->end_charpos may be greater than
6530 IT->string_nchars when this function is called, in which case we
6531 may have to return padding spaces. Value is zero if end of string
6532 reached, including padding spaces. */
6534 static int
6535 next_element_from_c_string (struct it *it)
6537 int success_p = 1;
6539 xassert (it->s);
6540 it->what = IT_CHARACTER;
6541 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6542 it->object = Qnil;
6544 /* IT's position can be greater IT->string_nchars in case a field
6545 width or precision has been specified when the iterator was
6546 initialized. */
6547 if (IT_CHARPOS (*it) >= it->end_charpos)
6549 /* End of the game. */
6550 it->what = IT_EOB;
6551 success_p = 0;
6553 else if (IT_CHARPOS (*it) >= it->string_nchars)
6555 /* Pad with spaces. */
6556 it->c = ' ', it->len = 1;
6557 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6559 else if (it->multibyte_p)
6561 /* Implementation note: The calls to strlen apparently aren't a
6562 performance problem because there is no noticeable performance
6563 difference between Emacs running in unibyte or multibyte mode. */
6564 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6565 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6567 else
6568 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6570 return success_p;
6574 /* Set up IT to return characters from an ellipsis, if appropriate.
6575 The definition of the ellipsis glyphs may come from a display table
6576 entry. This function fills IT with the first glyph from the
6577 ellipsis if an ellipsis is to be displayed. */
6579 static int
6580 next_element_from_ellipsis (struct it *it)
6582 if (it->selective_display_ellipsis_p)
6583 setup_for_ellipsis (it, it->len);
6584 else
6586 /* The face at the current position may be different from the
6587 face we find after the invisible text. Remember what it
6588 was in IT->saved_face_id, and signal that it's there by
6589 setting face_before_selective_p. */
6590 it->saved_face_id = it->face_id;
6591 it->method = GET_FROM_BUFFER;
6592 it->object = it->w->buffer;
6593 reseat_at_next_visible_line_start (it, 1);
6594 it->face_before_selective_p = 1;
6597 return GET_NEXT_DISPLAY_ELEMENT (it);
6601 /* Deliver an image display element. The iterator IT is already
6602 filled with image information (done in handle_display_prop). Value
6603 is always 1. */
6606 static int
6607 next_element_from_image (struct it *it)
6609 it->what = IT_IMAGE;
6610 it->ignore_overlay_strings_at_pos_p = 0;
6611 return 1;
6615 /* Fill iterator IT with next display element from a stretch glyph
6616 property. IT->object is the value of the text property. Value is
6617 always 1. */
6619 static int
6620 next_element_from_stretch (struct it *it)
6622 it->what = IT_STRETCH;
6623 return 1;
6626 /* Scan forward from CHARPOS in the current buffer, until we find a
6627 stop position > current IT's position. Then handle the stop
6628 position before that. This is called when we bump into a stop
6629 position while reordering bidirectional text. CHARPOS should be
6630 the last previously processed stop_pos (or BEGV, if none were
6631 processed yet) whose position is less that IT's current
6632 position. */
6634 static void
6635 handle_stop_backwards (struct it *it, EMACS_INT charpos)
6637 EMACS_INT where_we_are = IT_CHARPOS (*it);
6638 struct display_pos save_current = it->current;
6639 struct text_pos save_position = it->position;
6640 struct text_pos pos1;
6641 EMACS_INT next_stop;
6643 /* Scan in strict logical order. */
6644 it->bidi_p = 0;
6647 it->prev_stop = charpos;
6648 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
6649 reseat_1 (it, pos1, 0);
6650 compute_stop_pos (it);
6651 /* We must advance forward, right? */
6652 if (it->stop_charpos <= it->prev_stop)
6653 abort ();
6654 charpos = it->stop_charpos;
6656 while (charpos <= where_we_are);
6658 next_stop = it->stop_charpos;
6659 it->stop_charpos = it->prev_stop;
6660 it->bidi_p = 1;
6661 it->current = save_current;
6662 it->position = save_position;
6663 handle_stop (it);
6664 it->stop_charpos = next_stop;
6667 /* Load IT with the next display element from current_buffer. Value
6668 is zero if end of buffer reached. IT->stop_charpos is the next
6669 position at which to stop and check for text properties or buffer
6670 end. */
6672 static int
6673 next_element_from_buffer (struct it *it)
6675 int success_p = 1;
6677 xassert (IT_CHARPOS (*it) >= BEGV);
6679 /* With bidi reordering, the character to display might not be the
6680 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
6681 we were reseat()ed to a new buffer position, which is potentially
6682 a different paragraph. */
6683 if (it->bidi_p && it->bidi_it.first_elt)
6685 it->bidi_it.charpos = IT_CHARPOS (*it);
6686 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6687 if (it->bidi_it.bytepos == ZV_BYTE)
6689 /* Nothing to do, but reset the FIRST_ELT flag, like
6690 bidi_paragraph_init does, because we are not going to
6691 call it. */
6692 it->bidi_it.first_elt = 0;
6694 else if (it->bidi_it.bytepos == BEGV_BYTE
6695 /* FIXME: Should support all Unicode line separators. */
6696 || FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6697 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')
6699 /* If we are at the beginning of a line, we can produce the
6700 next element right away. */
6701 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6702 bidi_move_to_visually_next (&it->bidi_it);
6704 else
6706 int orig_bytepos = IT_BYTEPOS (*it);
6708 /* We need to prime the bidi iterator starting at the line's
6709 beginning, before we will be able to produce the next
6710 element. */
6711 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it), -1);
6712 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
6713 it->bidi_it.charpos = IT_CHARPOS (*it);
6714 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6715 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6718 /* Now return to buffer position where we were asked to
6719 get the next display element, and produce that. */
6720 bidi_move_to_visually_next (&it->bidi_it);
6722 while (it->bidi_it.bytepos != orig_bytepos
6723 && it->bidi_it.bytepos < ZV_BYTE);
6726 it->bidi_it.first_elt = 0; /* paranoia: bidi.c does this */
6727 /* Adjust IT's position information to where we ended up. */
6728 IT_CHARPOS (*it) = it->bidi_it.charpos;
6729 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6730 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
6732 EMACS_INT stop = it->stop_charpos;
6733 if (it->bidi_it.scan_dir < 0)
6734 stop = -1;
6735 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6736 IT_BYTEPOS (*it), stop, Qnil);
6740 if (IT_CHARPOS (*it) >= it->stop_charpos)
6742 if (IT_CHARPOS (*it) >= it->end_charpos)
6744 int overlay_strings_follow_p;
6746 /* End of the game, except when overlay strings follow that
6747 haven't been returned yet. */
6748 if (it->overlay_strings_at_end_processed_p)
6749 overlay_strings_follow_p = 0;
6750 else
6752 it->overlay_strings_at_end_processed_p = 1;
6753 overlay_strings_follow_p = get_overlay_strings (it, 0);
6756 if (overlay_strings_follow_p)
6757 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6758 else
6760 it->what = IT_EOB;
6761 it->position = it->current.pos;
6762 success_p = 0;
6765 else if (!(!it->bidi_p
6766 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6767 || IT_CHARPOS (*it) == it->stop_charpos))
6769 /* With bidi non-linear iteration, we could find ourselves
6770 far beyond the last computed stop_charpos, with several
6771 other stop positions in between that we missed. Scan
6772 them all now, in buffer's logical order, until we find
6773 and handle the last stop_charpos that precedes our
6774 current position. */
6775 handle_stop_backwards (it, it->stop_charpos);
6776 return GET_NEXT_DISPLAY_ELEMENT (it);
6778 else
6780 if (it->bidi_p)
6782 /* Take note of the stop position we just moved across,
6783 for when we will move back across it. */
6784 it->prev_stop = it->stop_charpos;
6785 /* If we are at base paragraph embedding level, take
6786 note of the last stop position seen at this
6787 level. */
6788 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6789 it->base_level_stop = it->stop_charpos;
6791 handle_stop (it);
6792 return GET_NEXT_DISPLAY_ELEMENT (it);
6795 else if (it->bidi_p
6796 /* We can sometimes back up for reasons that have nothing
6797 to do with bidi reordering. E.g., compositions. The
6798 code below is only needed when we are above the base
6799 embedding level, so test for that explicitly. */
6800 && !BIDI_AT_BASE_LEVEL (it->bidi_it)
6801 && IT_CHARPOS (*it) < it->prev_stop)
6803 if (it->base_level_stop <= 0)
6804 it->base_level_stop = BEGV;
6805 if (IT_CHARPOS (*it) < it->base_level_stop)
6806 abort ();
6807 handle_stop_backwards (it, it->base_level_stop);
6808 return GET_NEXT_DISPLAY_ELEMENT (it);
6810 else
6812 /* No face changes, overlays etc. in sight, so just return a
6813 character from current_buffer. */
6814 unsigned char *p;
6815 EMACS_INT stop;
6817 /* Maybe run the redisplay end trigger hook. Performance note:
6818 This doesn't seem to cost measurable time. */
6819 if (it->redisplay_end_trigger_charpos
6820 && it->glyph_row
6821 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6822 run_redisplay_end_trigger_hook (it);
6824 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
6825 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6826 stop)
6827 && next_element_from_composition (it))
6829 return 1;
6832 /* Get the next character, maybe multibyte. */
6833 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6834 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6835 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6836 else
6837 it->c = *p, it->len = 1;
6839 /* Record what we have and where it came from. */
6840 it->what = IT_CHARACTER;
6841 it->object = it->w->buffer;
6842 it->position = it->current.pos;
6844 /* Normally we return the character found above, except when we
6845 really want to return an ellipsis for selective display. */
6846 if (it->selective)
6848 if (it->c == '\n')
6850 /* A value of selective > 0 means hide lines indented more
6851 than that number of columns. */
6852 if (it->selective > 0
6853 && IT_CHARPOS (*it) + 1 < ZV
6854 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6855 IT_BYTEPOS (*it) + 1,
6856 (double) it->selective)) /* iftc */
6858 success_p = next_element_from_ellipsis (it);
6859 it->dpvec_char_len = -1;
6862 else if (it->c == '\r' && it->selective == -1)
6864 /* A value of selective == -1 means that everything from the
6865 CR to the end of the line is invisible, with maybe an
6866 ellipsis displayed for it. */
6867 success_p = next_element_from_ellipsis (it);
6868 it->dpvec_char_len = -1;
6873 /* Value is zero if end of buffer reached. */
6874 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6875 return success_p;
6879 /* Run the redisplay end trigger hook for IT. */
6881 static void
6882 run_redisplay_end_trigger_hook (struct it *it)
6884 Lisp_Object args[3];
6886 /* IT->glyph_row should be non-null, i.e. we should be actually
6887 displaying something, or otherwise we should not run the hook. */
6888 xassert (it->glyph_row);
6890 /* Set up hook arguments. */
6891 args[0] = Qredisplay_end_trigger_functions;
6892 args[1] = it->window;
6893 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6894 it->redisplay_end_trigger_charpos = 0;
6896 /* Since we are *trying* to run these functions, don't try to run
6897 them again, even if they get an error. */
6898 it->w->redisplay_end_trigger = Qnil;
6899 Frun_hook_with_args (3, args);
6901 /* Notice if it changed the face of the character we are on. */
6902 handle_face_prop (it);
6906 /* Deliver a composition display element. Unlike the other
6907 next_element_from_XXX, this function is not registered in the array
6908 get_next_element[]. It is called from next_element_from_buffer and
6909 next_element_from_string when necessary. */
6911 static int
6912 next_element_from_composition (struct it *it)
6914 it->what = IT_COMPOSITION;
6915 it->len = it->cmp_it.nbytes;
6916 if (STRINGP (it->string))
6918 if (it->c < 0)
6920 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6921 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6922 return 0;
6924 it->position = it->current.string_pos;
6925 it->object = it->string;
6926 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6927 IT_STRING_BYTEPOS (*it), it->string);
6929 else
6931 if (it->c < 0)
6933 IT_CHARPOS (*it) += it->cmp_it.nchars;
6934 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6935 if (it->bidi_p)
6937 if (it->bidi_it.new_paragraph)
6938 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it);
6939 /* Resync the bidi iterator with IT's new position.
6940 FIXME: this doesn't support bidirectional text. */
6941 while (it->bidi_it.charpos < IT_CHARPOS (*it))
6942 bidi_move_to_visually_next (&it->bidi_it);
6944 return 0;
6946 it->position = it->current.pos;
6947 it->object = it->w->buffer;
6948 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6949 IT_BYTEPOS (*it), Qnil);
6951 return 1;
6956 /***********************************************************************
6957 Moving an iterator without producing glyphs
6958 ***********************************************************************/
6960 /* Check if iterator is at a position corresponding to a valid buffer
6961 position after some move_it_ call. */
6963 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6964 ((it)->method == GET_FROM_STRING \
6965 ? IT_STRING_CHARPOS (*it) == 0 \
6966 : 1)
6969 /* Move iterator IT to a specified buffer or X position within one
6970 line on the display without producing glyphs.
6972 OP should be a bit mask including some or all of these bits:
6973 MOVE_TO_X: Stop upon reaching x-position TO_X.
6974 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
6975 Regardless of OP's value, stop upon reaching the end of the display line.
6977 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6978 This means, in particular, that TO_X includes window's horizontal
6979 scroll amount.
6981 The return value has several possible values that
6982 say what condition caused the scan to stop:
6984 MOVE_POS_MATCH_OR_ZV
6985 - when TO_POS or ZV was reached.
6987 MOVE_X_REACHED
6988 -when TO_X was reached before TO_POS or ZV were reached.
6990 MOVE_LINE_CONTINUED
6991 - when we reached the end of the display area and the line must
6992 be continued.
6994 MOVE_LINE_TRUNCATED
6995 - when we reached the end of the display area and the line is
6996 truncated.
6998 MOVE_NEWLINE_OR_CR
6999 - when we stopped at a line end, i.e. a newline or a CR and selective
7000 display is on. */
7002 static enum move_it_result
7003 move_it_in_display_line_to (struct it *it,
7004 EMACS_INT to_charpos, int to_x,
7005 enum move_operation_enum op)
7007 enum move_it_result result = MOVE_UNDEFINED;
7008 struct glyph_row *saved_glyph_row;
7009 struct it wrap_it, atpos_it, atx_it;
7010 int may_wrap = 0;
7011 enum it_method prev_method = it->method;
7012 EMACS_INT prev_pos = IT_CHARPOS (*it);
7014 /* Don't produce glyphs in produce_glyphs. */
7015 saved_glyph_row = it->glyph_row;
7016 it->glyph_row = NULL;
7018 /* Use wrap_it to save a copy of IT wherever a word wrap could
7019 occur. Use atpos_it to save a copy of IT at the desired buffer
7020 position, if found, so that we can scan ahead and check if the
7021 word later overshoots the window edge. Use atx_it similarly, for
7022 pixel positions. */
7023 wrap_it.sp = -1;
7024 atpos_it.sp = -1;
7025 atx_it.sp = -1;
7027 #define BUFFER_POS_REACHED_P() \
7028 ((op & MOVE_TO_POS) != 0 \
7029 && BUFFERP (it->object) \
7030 && (IT_CHARPOS (*it) == to_charpos \
7031 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7032 && (it->method == GET_FROM_BUFFER \
7033 || (it->method == GET_FROM_DISPLAY_VECTOR \
7034 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7036 /* If there's a line-/wrap-prefix, handle it. */
7037 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7038 && it->current_y < it->last_visible_y)
7039 handle_line_prefix (it);
7041 while (1)
7043 int x, i, ascent = 0, descent = 0;
7045 /* Utility macro to reset an iterator with x, ascent, and descent. */
7046 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7047 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7048 (IT)->max_descent = descent)
7050 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7051 glyph). */
7052 if ((op & MOVE_TO_POS) != 0
7053 && BUFFERP (it->object)
7054 && it->method == GET_FROM_BUFFER
7055 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7056 || (it->bidi_p
7057 && (prev_method == GET_FROM_IMAGE
7058 || prev_method == GET_FROM_STRETCH)
7059 /* Passed TO_CHARPOS from left to right. */
7060 && ((prev_pos < to_charpos
7061 && IT_CHARPOS (*it) > to_charpos)
7062 /* Passed TO_CHARPOS from right to left. */
7063 || (prev_pos > to_charpos
7064 && IT_CHARPOS (*it) < to_charpos)))))
7066 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7068 result = MOVE_POS_MATCH_OR_ZV;
7069 break;
7071 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7072 /* If wrap_it is valid, the current position might be in a
7073 word that is wrapped. So, save the iterator in
7074 atpos_it and continue to see if wrapping happens. */
7075 atpos_it = *it;
7078 prev_method = it->method;
7079 if (it->method == GET_FROM_BUFFER)
7080 prev_pos = IT_CHARPOS (*it);
7081 /* Stop when ZV reached.
7082 We used to stop here when TO_CHARPOS reached as well, but that is
7083 too soon if this glyph does not fit on this line. So we handle it
7084 explicitly below. */
7085 if (!get_next_display_element (it))
7087 result = MOVE_POS_MATCH_OR_ZV;
7088 break;
7091 if (it->line_wrap == TRUNCATE)
7093 if (BUFFER_POS_REACHED_P ())
7095 result = MOVE_POS_MATCH_OR_ZV;
7096 break;
7099 else
7101 if (it->line_wrap == WORD_WRAP)
7103 if (IT_DISPLAYING_WHITESPACE (it))
7104 may_wrap = 1;
7105 else if (may_wrap)
7107 /* We have reached a glyph that follows one or more
7108 whitespace characters. If the position is
7109 already found, we are done. */
7110 if (atpos_it.sp >= 0)
7112 *it = atpos_it;
7113 result = MOVE_POS_MATCH_OR_ZV;
7114 goto done;
7116 if (atx_it.sp >= 0)
7118 *it = atx_it;
7119 result = MOVE_X_REACHED;
7120 goto done;
7122 /* Otherwise, we can wrap here. */
7123 wrap_it = *it;
7124 may_wrap = 0;
7129 /* Remember the line height for the current line, in case
7130 the next element doesn't fit on the line. */
7131 ascent = it->max_ascent;
7132 descent = it->max_descent;
7134 /* The call to produce_glyphs will get the metrics of the
7135 display element IT is loaded with. Record the x-position
7136 before this display element, in case it doesn't fit on the
7137 line. */
7138 x = it->current_x;
7140 PRODUCE_GLYPHS (it);
7142 if (it->area != TEXT_AREA)
7144 set_iterator_to_next (it, 1);
7145 continue;
7148 /* The number of glyphs we get back in IT->nglyphs will normally
7149 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7150 character on a terminal frame, or (iii) a line end. For the
7151 second case, IT->nglyphs - 1 padding glyphs will be present.
7152 (On X frames, there is only one glyph produced for a
7153 composite character.)
7155 The behavior implemented below means, for continuation lines,
7156 that as many spaces of a TAB as fit on the current line are
7157 displayed there. For terminal frames, as many glyphs of a
7158 multi-glyph character are displayed in the current line, too.
7159 This is what the old redisplay code did, and we keep it that
7160 way. Under X, the whole shape of a complex character must
7161 fit on the line or it will be completely displayed in the
7162 next line.
7164 Note that both for tabs and padding glyphs, all glyphs have
7165 the same width. */
7166 if (it->nglyphs)
7168 /* More than one glyph or glyph doesn't fit on line. All
7169 glyphs have the same width. */
7170 int single_glyph_width = it->pixel_width / it->nglyphs;
7171 int new_x;
7172 int x_before_this_char = x;
7173 int hpos_before_this_char = it->hpos;
7175 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7177 new_x = x + single_glyph_width;
7179 /* We want to leave anything reaching TO_X to the caller. */
7180 if ((op & MOVE_TO_X) && new_x > to_x)
7182 if (BUFFER_POS_REACHED_P ())
7184 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7185 goto buffer_pos_reached;
7186 if (atpos_it.sp < 0)
7188 atpos_it = *it;
7189 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7192 else
7194 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7196 it->current_x = x;
7197 result = MOVE_X_REACHED;
7198 break;
7200 if (atx_it.sp < 0)
7202 atx_it = *it;
7203 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7208 if (/* Lines are continued. */
7209 it->line_wrap != TRUNCATE
7210 && (/* And glyph doesn't fit on the line. */
7211 new_x > it->last_visible_x
7212 /* Or it fits exactly and we're on a window
7213 system frame. */
7214 || (new_x == it->last_visible_x
7215 && FRAME_WINDOW_P (it->f))))
7217 if (/* IT->hpos == 0 means the very first glyph
7218 doesn't fit on the line, e.g. a wide image. */
7219 it->hpos == 0
7220 || (new_x == it->last_visible_x
7221 && FRAME_WINDOW_P (it->f)))
7223 ++it->hpos;
7224 it->current_x = new_x;
7226 /* The character's last glyph just barely fits
7227 in this row. */
7228 if (i == it->nglyphs - 1)
7230 /* If this is the destination position,
7231 return a position *before* it in this row,
7232 now that we know it fits in this row. */
7233 if (BUFFER_POS_REACHED_P ())
7235 if (it->line_wrap != WORD_WRAP
7236 || wrap_it.sp < 0)
7238 it->hpos = hpos_before_this_char;
7239 it->current_x = x_before_this_char;
7240 result = MOVE_POS_MATCH_OR_ZV;
7241 break;
7243 if (it->line_wrap == WORD_WRAP
7244 && atpos_it.sp < 0)
7246 atpos_it = *it;
7247 atpos_it.current_x = x_before_this_char;
7248 atpos_it.hpos = hpos_before_this_char;
7252 set_iterator_to_next (it, 1);
7253 /* On graphical terminals, newlines may
7254 "overflow" into the fringe if
7255 overflow-newline-into-fringe is non-nil.
7256 On text-only terminals, newlines may
7257 overflow into the last glyph on the
7258 display line.*/
7259 if (!FRAME_WINDOW_P (it->f)
7260 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7262 if (!get_next_display_element (it))
7264 result = MOVE_POS_MATCH_OR_ZV;
7265 break;
7267 if (BUFFER_POS_REACHED_P ())
7269 if (ITERATOR_AT_END_OF_LINE_P (it))
7270 result = MOVE_POS_MATCH_OR_ZV;
7271 else
7272 result = MOVE_LINE_CONTINUED;
7273 break;
7275 if (ITERATOR_AT_END_OF_LINE_P (it))
7277 result = MOVE_NEWLINE_OR_CR;
7278 break;
7283 else
7284 IT_RESET_X_ASCENT_DESCENT (it);
7286 if (wrap_it.sp >= 0)
7288 *it = wrap_it;
7289 atpos_it.sp = -1;
7290 atx_it.sp = -1;
7293 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7294 IT_CHARPOS (*it)));
7295 result = MOVE_LINE_CONTINUED;
7296 break;
7299 if (BUFFER_POS_REACHED_P ())
7301 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7302 goto buffer_pos_reached;
7303 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7305 atpos_it = *it;
7306 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7310 if (new_x > it->first_visible_x)
7312 /* Glyph is visible. Increment number of glyphs that
7313 would be displayed. */
7314 ++it->hpos;
7318 if (result != MOVE_UNDEFINED)
7319 break;
7321 else if (BUFFER_POS_REACHED_P ())
7323 buffer_pos_reached:
7324 IT_RESET_X_ASCENT_DESCENT (it);
7325 result = MOVE_POS_MATCH_OR_ZV;
7326 break;
7328 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7330 /* Stop when TO_X specified and reached. This check is
7331 necessary here because of lines consisting of a line end,
7332 only. The line end will not produce any glyphs and we
7333 would never get MOVE_X_REACHED. */
7334 xassert (it->nglyphs == 0);
7335 result = MOVE_X_REACHED;
7336 break;
7339 /* Is this a line end? If yes, we're done. */
7340 if (ITERATOR_AT_END_OF_LINE_P (it))
7342 result = MOVE_NEWLINE_OR_CR;
7343 break;
7346 if (it->method == GET_FROM_BUFFER)
7347 prev_pos = IT_CHARPOS (*it);
7348 /* The current display element has been consumed. Advance
7349 to the next. */
7350 set_iterator_to_next (it, 1);
7352 /* Stop if lines are truncated and IT's current x-position is
7353 past the right edge of the window now. */
7354 if (it->line_wrap == TRUNCATE
7355 && it->current_x >= it->last_visible_x)
7357 if (!FRAME_WINDOW_P (it->f)
7358 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7360 if (!get_next_display_element (it)
7361 || BUFFER_POS_REACHED_P ())
7363 result = MOVE_POS_MATCH_OR_ZV;
7364 break;
7366 if (ITERATOR_AT_END_OF_LINE_P (it))
7368 result = MOVE_NEWLINE_OR_CR;
7369 break;
7372 result = MOVE_LINE_TRUNCATED;
7373 break;
7375 #undef IT_RESET_X_ASCENT_DESCENT
7378 #undef BUFFER_POS_REACHED_P
7380 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7381 restore the saved iterator. */
7382 if (atpos_it.sp >= 0)
7383 *it = atpos_it;
7384 else if (atx_it.sp >= 0)
7385 *it = atx_it;
7387 done:
7389 /* Restore the iterator settings altered at the beginning of this
7390 function. */
7391 it->glyph_row = saved_glyph_row;
7392 return result;
7395 /* For external use. */
7396 void
7397 move_it_in_display_line (struct it *it,
7398 EMACS_INT to_charpos, int to_x,
7399 enum move_operation_enum op)
7401 if (it->line_wrap == WORD_WRAP
7402 && (op & MOVE_TO_X))
7404 struct it save_it = *it;
7405 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7406 /* When word-wrap is on, TO_X may lie past the end
7407 of a wrapped line. Then it->current is the
7408 character on the next line, so backtrack to the
7409 space before the wrap point. */
7410 if (skip == MOVE_LINE_CONTINUED)
7412 int prev_x = max (it->current_x - 1, 0);
7413 *it = save_it;
7414 move_it_in_display_line_to
7415 (it, -1, prev_x, MOVE_TO_X);
7418 else
7419 move_it_in_display_line_to (it, to_charpos, to_x, op);
7423 /* Move IT forward until it satisfies one or more of the criteria in
7424 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7426 OP is a bit-mask that specifies where to stop, and in particular,
7427 which of those four position arguments makes a difference. See the
7428 description of enum move_operation_enum.
7430 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7431 screen line, this function will set IT to the next position >
7432 TO_CHARPOS. */
7434 void
7435 move_it_to (struct it *it, int to_charpos, int to_x, int to_y, int to_vpos, int op)
7437 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7438 int line_height, line_start_x = 0, reached = 0;
7440 for (;;)
7442 if (op & MOVE_TO_VPOS)
7444 /* If no TO_CHARPOS and no TO_X specified, stop at the
7445 start of the line TO_VPOS. */
7446 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7448 if (it->vpos == to_vpos)
7450 reached = 1;
7451 break;
7453 else
7454 skip = move_it_in_display_line_to (it, -1, -1, 0);
7456 else
7458 /* TO_VPOS >= 0 means stop at TO_X in the line at
7459 TO_VPOS, or at TO_POS, whichever comes first. */
7460 if (it->vpos == to_vpos)
7462 reached = 2;
7463 break;
7466 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7468 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7470 reached = 3;
7471 break;
7473 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7475 /* We have reached TO_X but not in the line we want. */
7476 skip = move_it_in_display_line_to (it, to_charpos,
7477 -1, MOVE_TO_POS);
7478 if (skip == MOVE_POS_MATCH_OR_ZV)
7480 reached = 4;
7481 break;
7486 else if (op & MOVE_TO_Y)
7488 struct it it_backup;
7490 if (it->line_wrap == WORD_WRAP)
7491 it_backup = *it;
7493 /* TO_Y specified means stop at TO_X in the line containing
7494 TO_Y---or at TO_CHARPOS if this is reached first. The
7495 problem is that we can't really tell whether the line
7496 contains TO_Y before we have completely scanned it, and
7497 this may skip past TO_X. What we do is to first scan to
7498 TO_X.
7500 If TO_X is not specified, use a TO_X of zero. The reason
7501 is to make the outcome of this function more predictable.
7502 If we didn't use TO_X == 0, we would stop at the end of
7503 the line which is probably not what a caller would expect
7504 to happen. */
7505 skip = move_it_in_display_line_to
7506 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7507 (MOVE_TO_X | (op & MOVE_TO_POS)));
7509 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7510 if (skip == MOVE_POS_MATCH_OR_ZV)
7511 reached = 5;
7512 else if (skip == MOVE_X_REACHED)
7514 /* If TO_X was reached, we want to know whether TO_Y is
7515 in the line. We know this is the case if the already
7516 scanned glyphs make the line tall enough. Otherwise,
7517 we must check by scanning the rest of the line. */
7518 line_height = it->max_ascent + it->max_descent;
7519 if (to_y >= it->current_y
7520 && to_y < it->current_y + line_height)
7522 reached = 6;
7523 break;
7525 it_backup = *it;
7526 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7527 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7528 op & MOVE_TO_POS);
7529 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7530 line_height = it->max_ascent + it->max_descent;
7531 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7533 if (to_y >= it->current_y
7534 && to_y < it->current_y + line_height)
7536 /* If TO_Y is in this line and TO_X was reached
7537 above, we scanned too far. We have to restore
7538 IT's settings to the ones before skipping. */
7539 *it = it_backup;
7540 reached = 6;
7542 else
7544 skip = skip2;
7545 if (skip == MOVE_POS_MATCH_OR_ZV)
7546 reached = 7;
7549 else
7551 /* Check whether TO_Y is in this line. */
7552 line_height = it->max_ascent + it->max_descent;
7553 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7555 if (to_y >= it->current_y
7556 && to_y < it->current_y + line_height)
7558 /* When word-wrap is on, TO_X may lie past the end
7559 of a wrapped line. Then it->current is the
7560 character on the next line, so backtrack to the
7561 space before the wrap point. */
7562 if (skip == MOVE_LINE_CONTINUED
7563 && it->line_wrap == WORD_WRAP)
7565 int prev_x = max (it->current_x - 1, 0);
7566 *it = it_backup;
7567 skip = move_it_in_display_line_to
7568 (it, -1, prev_x, MOVE_TO_X);
7570 reached = 6;
7574 if (reached)
7575 break;
7577 else if (BUFFERP (it->object)
7578 && (it->method == GET_FROM_BUFFER
7579 || it->method == GET_FROM_STRETCH)
7580 && IT_CHARPOS (*it) >= to_charpos)
7581 skip = MOVE_POS_MATCH_OR_ZV;
7582 else
7583 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7585 switch (skip)
7587 case MOVE_POS_MATCH_OR_ZV:
7588 reached = 8;
7589 goto out;
7591 case MOVE_NEWLINE_OR_CR:
7592 set_iterator_to_next (it, 1);
7593 it->continuation_lines_width = 0;
7594 break;
7596 case MOVE_LINE_TRUNCATED:
7597 it->continuation_lines_width = 0;
7598 reseat_at_next_visible_line_start (it, 0);
7599 if ((op & MOVE_TO_POS) != 0
7600 && IT_CHARPOS (*it) > to_charpos)
7602 reached = 9;
7603 goto out;
7605 break;
7607 case MOVE_LINE_CONTINUED:
7608 /* For continued lines ending in a tab, some of the glyphs
7609 associated with the tab are displayed on the current
7610 line. Since it->current_x does not include these glyphs,
7611 we use it->last_visible_x instead. */
7612 if (it->c == '\t')
7614 it->continuation_lines_width += it->last_visible_x;
7615 /* When moving by vpos, ensure that the iterator really
7616 advances to the next line (bug#847, bug#969). Fixme:
7617 do we need to do this in other circumstances? */
7618 if (it->current_x != it->last_visible_x
7619 && (op & MOVE_TO_VPOS)
7620 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7622 line_start_x = it->current_x + it->pixel_width
7623 - it->last_visible_x;
7624 set_iterator_to_next (it, 0);
7627 else
7628 it->continuation_lines_width += it->current_x;
7629 break;
7631 default:
7632 abort ();
7635 /* Reset/increment for the next run. */
7636 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7637 it->current_x = line_start_x;
7638 line_start_x = 0;
7639 it->hpos = 0;
7640 it->current_y += it->max_ascent + it->max_descent;
7641 ++it->vpos;
7642 last_height = it->max_ascent + it->max_descent;
7643 last_max_ascent = it->max_ascent;
7644 it->max_ascent = it->max_descent = 0;
7647 out:
7649 /* On text terminals, we may stop at the end of a line in the middle
7650 of a multi-character glyph. If the glyph itself is continued,
7651 i.e. it is actually displayed on the next line, don't treat this
7652 stopping point as valid; move to the next line instead (unless
7653 that brings us offscreen). */
7654 if (!FRAME_WINDOW_P (it->f)
7655 && op & MOVE_TO_POS
7656 && IT_CHARPOS (*it) == to_charpos
7657 && it->what == IT_CHARACTER
7658 && it->nglyphs > 1
7659 && it->line_wrap == WINDOW_WRAP
7660 && it->current_x == it->last_visible_x - 1
7661 && it->c != '\n'
7662 && it->c != '\t'
7663 && it->vpos < XFASTINT (it->w->window_end_vpos))
7665 it->continuation_lines_width += it->current_x;
7666 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7667 it->current_y += it->max_ascent + it->max_descent;
7668 ++it->vpos;
7669 last_height = it->max_ascent + it->max_descent;
7670 last_max_ascent = it->max_ascent;
7673 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7677 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7679 If DY > 0, move IT backward at least that many pixels. DY = 0
7680 means move IT backward to the preceding line start or BEGV. This
7681 function may move over more than DY pixels if IT->current_y - DY
7682 ends up in the middle of a line; in this case IT->current_y will be
7683 set to the top of the line moved to. */
7685 void
7686 move_it_vertically_backward (struct it *it, int dy)
7688 int nlines, h;
7689 struct it it2, it3;
7690 int start_pos;
7692 move_further_back:
7693 xassert (dy >= 0);
7695 start_pos = IT_CHARPOS (*it);
7697 /* Estimate how many newlines we must move back. */
7698 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7700 /* Set the iterator's position that many lines back. */
7701 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7702 back_to_previous_visible_line_start (it);
7704 /* Reseat the iterator here. When moving backward, we don't want
7705 reseat to skip forward over invisible text, set up the iterator
7706 to deliver from overlay strings at the new position etc. So,
7707 use reseat_1 here. */
7708 reseat_1 (it, it->current.pos, 1);
7710 /* We are now surely at a line start. */
7711 it->current_x = it->hpos = 0;
7712 it->continuation_lines_width = 0;
7714 /* Move forward and see what y-distance we moved. First move to the
7715 start of the next line so that we get its height. We need this
7716 height to be able to tell whether we reached the specified
7717 y-distance. */
7718 it2 = *it;
7719 it2.max_ascent = it2.max_descent = 0;
7722 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7723 MOVE_TO_POS | MOVE_TO_VPOS);
7725 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7726 xassert (IT_CHARPOS (*it) >= BEGV);
7727 it3 = it2;
7729 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7730 xassert (IT_CHARPOS (*it) >= BEGV);
7731 /* H is the actual vertical distance from the position in *IT
7732 and the starting position. */
7733 h = it2.current_y - it->current_y;
7734 /* NLINES is the distance in number of lines. */
7735 nlines = it2.vpos - it->vpos;
7737 /* Correct IT's y and vpos position
7738 so that they are relative to the starting point. */
7739 it->vpos -= nlines;
7740 it->current_y -= h;
7742 if (dy == 0)
7744 /* DY == 0 means move to the start of the screen line. The
7745 value of nlines is > 0 if continuation lines were involved. */
7746 if (nlines > 0)
7747 move_it_by_lines (it, nlines, 1);
7749 else
7751 /* The y-position we try to reach, relative to *IT.
7752 Note that H has been subtracted in front of the if-statement. */
7753 int target_y = it->current_y + h - dy;
7754 int y0 = it3.current_y;
7755 int y1 = line_bottom_y (&it3);
7756 int line_height = y1 - y0;
7758 /* If we did not reach target_y, try to move further backward if
7759 we can. If we moved too far backward, try to move forward. */
7760 if (target_y < it->current_y
7761 /* This is heuristic. In a window that's 3 lines high, with
7762 a line height of 13 pixels each, recentering with point
7763 on the bottom line will try to move -39/2 = 19 pixels
7764 backward. Try to avoid moving into the first line. */
7765 && (it->current_y - target_y
7766 > min (window_box_height (it->w), line_height * 2 / 3))
7767 && IT_CHARPOS (*it) > BEGV)
7769 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7770 target_y - it->current_y));
7771 dy = it->current_y - target_y;
7772 goto move_further_back;
7774 else if (target_y >= it->current_y + line_height
7775 && IT_CHARPOS (*it) < ZV)
7777 /* Should move forward by at least one line, maybe more.
7779 Note: Calling move_it_by_lines can be expensive on
7780 terminal frames, where compute_motion is used (via
7781 vmotion) to do the job, when there are very long lines
7782 and truncate-lines is nil. That's the reason for
7783 treating terminal frames specially here. */
7785 if (!FRAME_WINDOW_P (it->f))
7786 move_it_vertically (it, target_y - (it->current_y + line_height));
7787 else
7791 move_it_by_lines (it, 1, 1);
7793 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7800 /* Move IT by a specified amount of pixel lines DY. DY negative means
7801 move backwards. DY = 0 means move to start of screen line. At the
7802 end, IT will be on the start of a screen line. */
7804 void
7805 move_it_vertically (struct it *it, int dy)
7807 if (dy <= 0)
7808 move_it_vertically_backward (it, -dy);
7809 else
7811 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7812 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7813 MOVE_TO_POS | MOVE_TO_Y);
7814 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7816 /* If buffer ends in ZV without a newline, move to the start of
7817 the line to satisfy the post-condition. */
7818 if (IT_CHARPOS (*it) == ZV
7819 && ZV > BEGV
7820 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7821 move_it_by_lines (it, 0, 0);
7826 /* Move iterator IT past the end of the text line it is in. */
7828 void
7829 move_it_past_eol (struct it *it)
7831 enum move_it_result rc;
7833 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7834 if (rc == MOVE_NEWLINE_OR_CR)
7835 set_iterator_to_next (it, 0);
7839 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7840 negative means move up. DVPOS == 0 means move to the start of the
7841 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7842 NEED_Y_P is zero, IT->current_y will be left unchanged.
7844 Further optimization ideas: If we would know that IT->f doesn't use
7845 a face with proportional font, we could be faster for
7846 truncate-lines nil. */
7848 void
7849 move_it_by_lines (struct it *it, int dvpos, int need_y_p)
7851 struct position pos;
7853 /* The commented-out optimization uses vmotion on terminals. This
7854 gives bad results, because elements like it->what, on which
7855 callers such as pos_visible_p rely, aren't updated. */
7856 /* if (!FRAME_WINDOW_P (it->f))
7858 struct text_pos textpos;
7860 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7861 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7862 reseat (it, textpos, 1);
7863 it->vpos += pos.vpos;
7864 it->current_y += pos.vpos;
7866 else */
7868 if (dvpos == 0)
7870 /* DVPOS == 0 means move to the start of the screen line. */
7871 move_it_vertically_backward (it, 0);
7872 xassert (it->current_x == 0 && it->hpos == 0);
7873 /* Let next call to line_bottom_y calculate real line height */
7874 last_height = 0;
7876 else if (dvpos > 0)
7878 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7879 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7880 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7882 else
7884 struct it it2;
7885 int start_charpos, i;
7887 /* Start at the beginning of the screen line containing IT's
7888 position. This may actually move vertically backwards,
7889 in case of overlays, so adjust dvpos accordingly. */
7890 dvpos += it->vpos;
7891 move_it_vertically_backward (it, 0);
7892 dvpos -= it->vpos;
7894 /* Go back -DVPOS visible lines and reseat the iterator there. */
7895 start_charpos = IT_CHARPOS (*it);
7896 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7897 back_to_previous_visible_line_start (it);
7898 reseat (it, it->current.pos, 1);
7900 /* Move further back if we end up in a string or an image. */
7901 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7903 /* First try to move to start of display line. */
7904 dvpos += it->vpos;
7905 move_it_vertically_backward (it, 0);
7906 dvpos -= it->vpos;
7907 if (IT_POS_VALID_AFTER_MOVE_P (it))
7908 break;
7909 /* If start of line is still in string or image,
7910 move further back. */
7911 back_to_previous_visible_line_start (it);
7912 reseat (it, it->current.pos, 1);
7913 dvpos--;
7916 it->current_x = it->hpos = 0;
7918 /* Above call may have moved too far if continuation lines
7919 are involved. Scan forward and see if it did. */
7920 it2 = *it;
7921 it2.vpos = it2.current_y = 0;
7922 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7923 it->vpos -= it2.vpos;
7924 it->current_y -= it2.current_y;
7925 it->current_x = it->hpos = 0;
7927 /* If we moved too far back, move IT some lines forward. */
7928 if (it2.vpos > -dvpos)
7930 int delta = it2.vpos + dvpos;
7931 it2 = *it;
7932 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7933 /* Move back again if we got too far ahead. */
7934 if (IT_CHARPOS (*it) >= start_charpos)
7935 *it = it2;
7940 /* Return 1 if IT points into the middle of a display vector. */
7943 in_display_vector_p (struct it *it)
7945 return (it->method == GET_FROM_DISPLAY_VECTOR
7946 && it->current.dpvec_index > 0
7947 && it->dpvec + it->current.dpvec_index != it->dpend);
7951 /***********************************************************************
7952 Messages
7953 ***********************************************************************/
7956 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7957 to *Messages*. */
7959 void
7960 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
7962 Lisp_Object args[3];
7963 Lisp_Object msg, fmt;
7964 char *buffer;
7965 int len;
7966 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7967 USE_SAFE_ALLOCA;
7969 /* Do nothing if called asynchronously. Inserting text into
7970 a buffer may call after-change-functions and alike and
7971 that would means running Lisp asynchronously. */
7972 if (handling_signal)
7973 return;
7975 fmt = msg = Qnil;
7976 GCPRO4 (fmt, msg, arg1, arg2);
7978 args[0] = fmt = build_string (format);
7979 args[1] = arg1;
7980 args[2] = arg2;
7981 msg = Fformat (3, args);
7983 len = SBYTES (msg) + 1;
7984 SAFE_ALLOCA (buffer, char *, len);
7985 memcpy (buffer, SDATA (msg), len);
7987 message_dolog (buffer, len - 1, 1, 0);
7988 SAFE_FREE ();
7990 UNGCPRO;
7994 /* Output a newline in the *Messages* buffer if "needs" one. */
7996 void
7997 message_log_maybe_newline (void)
7999 if (message_log_need_newline)
8000 message_dolog ("", 0, 1, 0);
8004 /* Add a string M of length NBYTES to the message log, optionally
8005 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8006 nonzero, means interpret the contents of M as multibyte. This
8007 function calls low-level routines in order to bypass text property
8008 hooks, etc. which might not be safe to run.
8010 This may GC (insert may run before/after change hooks),
8011 so the buffer M must NOT point to a Lisp string. */
8013 void
8014 message_dolog (const char *m, int nbytes, int nlflag, int multibyte)
8016 if (!NILP (Vmemory_full))
8017 return;
8019 if (!NILP (Vmessage_log_max))
8021 struct buffer *oldbuf;
8022 Lisp_Object oldpoint, oldbegv, oldzv;
8023 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8024 int point_at_end = 0;
8025 int zv_at_end = 0;
8026 Lisp_Object old_deactivate_mark, tem;
8027 struct gcpro gcpro1;
8029 old_deactivate_mark = Vdeactivate_mark;
8030 oldbuf = current_buffer;
8031 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8032 current_buffer->undo_list = Qt;
8034 oldpoint = message_dolog_marker1;
8035 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8036 oldbegv = message_dolog_marker2;
8037 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8038 oldzv = message_dolog_marker3;
8039 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8040 GCPRO1 (old_deactivate_mark);
8042 if (PT == Z)
8043 point_at_end = 1;
8044 if (ZV == Z)
8045 zv_at_end = 1;
8047 BEGV = BEG;
8048 BEGV_BYTE = BEG_BYTE;
8049 ZV = Z;
8050 ZV_BYTE = Z_BYTE;
8051 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8053 /* Insert the string--maybe converting multibyte to single byte
8054 or vice versa, so that all the text fits the buffer. */
8055 if (multibyte
8056 && NILP (current_buffer->enable_multibyte_characters))
8058 int i, c, char_bytes;
8059 unsigned char work[1];
8061 /* Convert a multibyte string to single-byte
8062 for the *Message* buffer. */
8063 for (i = 0; i < nbytes; i += char_bytes)
8065 c = string_char_and_length (m + i, &char_bytes);
8066 work[0] = (ASCII_CHAR_P (c)
8068 : multibyte_char_to_unibyte (c, Qnil));
8069 insert_1_both (work, 1, 1, 1, 0, 0);
8072 else if (! multibyte
8073 && ! NILP (current_buffer->enable_multibyte_characters))
8075 int i, c, char_bytes;
8076 unsigned char *msg = (unsigned char *) m;
8077 unsigned char str[MAX_MULTIBYTE_LENGTH];
8078 /* Convert a single-byte string to multibyte
8079 for the *Message* buffer. */
8080 for (i = 0; i < nbytes; i++)
8082 c = msg[i];
8083 MAKE_CHAR_MULTIBYTE (c);
8084 char_bytes = CHAR_STRING (c, str);
8085 insert_1_both (str, 1, char_bytes, 1, 0, 0);
8088 else if (nbytes)
8089 insert_1 (m, nbytes, 1, 0, 0);
8091 if (nlflag)
8093 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
8094 insert_1 ("\n", 1, 1, 0, 0);
8096 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8097 this_bol = PT;
8098 this_bol_byte = PT_BYTE;
8100 /* See if this line duplicates the previous one.
8101 If so, combine duplicates. */
8102 if (this_bol > BEG)
8104 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8105 prev_bol = PT;
8106 prev_bol_byte = PT_BYTE;
8108 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
8109 this_bol, this_bol_byte);
8110 if (dup)
8112 del_range_both (prev_bol, prev_bol_byte,
8113 this_bol, this_bol_byte, 0);
8114 if (dup > 1)
8116 char dupstr[40];
8117 int duplen;
8119 /* If you change this format, don't forget to also
8120 change message_log_check_duplicate. */
8121 sprintf (dupstr, " [%d times]", dup);
8122 duplen = strlen (dupstr);
8123 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8124 insert_1 (dupstr, duplen, 1, 0, 1);
8129 /* If we have more than the desired maximum number of lines
8130 in the *Messages* buffer now, delete the oldest ones.
8131 This is safe because we don't have undo in this buffer. */
8133 if (NATNUMP (Vmessage_log_max))
8135 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8136 -XFASTINT (Vmessage_log_max) - 1, 0);
8137 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8140 BEGV = XMARKER (oldbegv)->charpos;
8141 BEGV_BYTE = marker_byte_position (oldbegv);
8143 if (zv_at_end)
8145 ZV = Z;
8146 ZV_BYTE = Z_BYTE;
8148 else
8150 ZV = XMARKER (oldzv)->charpos;
8151 ZV_BYTE = marker_byte_position (oldzv);
8154 if (point_at_end)
8155 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8156 else
8157 /* We can't do Fgoto_char (oldpoint) because it will run some
8158 Lisp code. */
8159 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8160 XMARKER (oldpoint)->bytepos);
8162 UNGCPRO;
8163 unchain_marker (XMARKER (oldpoint));
8164 unchain_marker (XMARKER (oldbegv));
8165 unchain_marker (XMARKER (oldzv));
8167 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8168 set_buffer_internal (oldbuf);
8169 if (NILP (tem))
8170 windows_or_buffers_changed = old_windows_or_buffers_changed;
8171 message_log_need_newline = !nlflag;
8172 Vdeactivate_mark = old_deactivate_mark;
8177 /* We are at the end of the buffer after just having inserted a newline.
8178 (Note: We depend on the fact we won't be crossing the gap.)
8179 Check to see if the most recent message looks a lot like the previous one.
8180 Return 0 if different, 1 if the new one should just replace it, or a
8181 value N > 1 if we should also append " [N times]". */
8183 static int
8184 message_log_check_duplicate (int prev_bol, int prev_bol_byte,
8185 int this_bol, int this_bol_byte)
8187 int i;
8188 int len = Z_BYTE - 1 - this_bol_byte;
8189 int seen_dots = 0;
8190 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8191 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8193 for (i = 0; i < len; i++)
8195 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8196 seen_dots = 1;
8197 if (p1[i] != p2[i])
8198 return seen_dots;
8200 p1 += len;
8201 if (*p1 == '\n')
8202 return 2;
8203 if (*p1++ == ' ' && *p1++ == '[')
8205 int n = 0;
8206 while (*p1 >= '0' && *p1 <= '9')
8207 n = n * 10 + *p1++ - '0';
8208 if (strncmp (p1, " times]\n", 8) == 0)
8209 return n+1;
8211 return 0;
8215 /* Display an echo area message M with a specified length of NBYTES
8216 bytes. The string may include null characters. If M is 0, clear
8217 out any existing message, and let the mini-buffer text show
8218 through.
8220 This may GC, so the buffer M must NOT point to a Lisp string. */
8222 void
8223 message2 (const char *m, int nbytes, int multibyte)
8225 /* First flush out any partial line written with print. */
8226 message_log_maybe_newline ();
8227 if (m)
8228 message_dolog (m, nbytes, 1, multibyte);
8229 message2_nolog (m, nbytes, multibyte);
8233 /* The non-logging counterpart of message2. */
8235 void
8236 message2_nolog (const char *m, int nbytes, int multibyte)
8238 struct frame *sf = SELECTED_FRAME ();
8239 message_enable_multibyte = multibyte;
8241 if (FRAME_INITIAL_P (sf))
8243 if (noninteractive_need_newline)
8244 putc ('\n', stderr);
8245 noninteractive_need_newline = 0;
8246 if (m)
8247 fwrite (m, nbytes, 1, stderr);
8248 if (cursor_in_echo_area == 0)
8249 fprintf (stderr, "\n");
8250 fflush (stderr);
8252 /* A null message buffer means that the frame hasn't really been
8253 initialized yet. Error messages get reported properly by
8254 cmd_error, so this must be just an informative message; toss it. */
8255 else if (INTERACTIVE
8256 && sf->glyphs_initialized_p
8257 && FRAME_MESSAGE_BUF (sf))
8259 Lisp_Object mini_window;
8260 struct frame *f;
8262 /* Get the frame containing the mini-buffer
8263 that the selected frame is using. */
8264 mini_window = FRAME_MINIBUF_WINDOW (sf);
8265 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8267 FRAME_SAMPLE_VISIBILITY (f);
8268 if (FRAME_VISIBLE_P (sf)
8269 && ! FRAME_VISIBLE_P (f))
8270 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8272 if (m)
8274 set_message (m, Qnil, nbytes, multibyte);
8275 if (minibuffer_auto_raise)
8276 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8278 else
8279 clear_message (1, 1);
8281 do_pending_window_change (0);
8282 echo_area_display (1);
8283 do_pending_window_change (0);
8284 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8285 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8290 /* Display an echo area message M with a specified length of NBYTES
8291 bytes. The string may include null characters. If M is not a
8292 string, clear out any existing message, and let the mini-buffer
8293 text show through.
8295 This function cancels echoing. */
8297 void
8298 message3 (Lisp_Object m, int nbytes, int multibyte)
8300 struct gcpro gcpro1;
8302 GCPRO1 (m);
8303 clear_message (1,1);
8304 cancel_echoing ();
8306 /* First flush out any partial line written with print. */
8307 message_log_maybe_newline ();
8308 if (STRINGP (m))
8310 char *buffer;
8311 USE_SAFE_ALLOCA;
8313 SAFE_ALLOCA (buffer, char *, nbytes);
8314 memcpy (buffer, SDATA (m), nbytes);
8315 message_dolog (buffer, nbytes, 1, multibyte);
8316 SAFE_FREE ();
8318 message3_nolog (m, nbytes, multibyte);
8320 UNGCPRO;
8324 /* The non-logging version of message3.
8325 This does not cancel echoing, because it is used for echoing.
8326 Perhaps we need to make a separate function for echoing
8327 and make this cancel echoing. */
8329 void
8330 message3_nolog (Lisp_Object m, int nbytes, int multibyte)
8332 struct frame *sf = SELECTED_FRAME ();
8333 message_enable_multibyte = multibyte;
8335 if (FRAME_INITIAL_P (sf))
8337 if (noninteractive_need_newline)
8338 putc ('\n', stderr);
8339 noninteractive_need_newline = 0;
8340 if (STRINGP (m))
8341 fwrite (SDATA (m), nbytes, 1, stderr);
8342 if (cursor_in_echo_area == 0)
8343 fprintf (stderr, "\n");
8344 fflush (stderr);
8346 /* A null message buffer means that the frame hasn't really been
8347 initialized yet. Error messages get reported properly by
8348 cmd_error, so this must be just an informative message; toss it. */
8349 else if (INTERACTIVE
8350 && sf->glyphs_initialized_p
8351 && FRAME_MESSAGE_BUF (sf))
8353 Lisp_Object mini_window;
8354 Lisp_Object frame;
8355 struct frame *f;
8357 /* Get the frame containing the mini-buffer
8358 that the selected frame is using. */
8359 mini_window = FRAME_MINIBUF_WINDOW (sf);
8360 frame = XWINDOW (mini_window)->frame;
8361 f = XFRAME (frame);
8363 FRAME_SAMPLE_VISIBILITY (f);
8364 if (FRAME_VISIBLE_P (sf)
8365 && !FRAME_VISIBLE_P (f))
8366 Fmake_frame_visible (frame);
8368 if (STRINGP (m) && SCHARS (m) > 0)
8370 set_message (NULL, m, nbytes, multibyte);
8371 if (minibuffer_auto_raise)
8372 Fraise_frame (frame);
8373 /* Assume we are not echoing.
8374 (If we are, echo_now will override this.) */
8375 echo_message_buffer = Qnil;
8377 else
8378 clear_message (1, 1);
8380 do_pending_window_change (0);
8381 echo_area_display (1);
8382 do_pending_window_change (0);
8383 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8384 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8389 /* Display a null-terminated echo area message M. If M is 0, clear
8390 out any existing message, and let the mini-buffer text show through.
8392 The buffer M must continue to exist until after the echo area gets
8393 cleared or some other message gets displayed there. Do not pass
8394 text that is stored in a Lisp string. Do not pass text in a buffer
8395 that was alloca'd. */
8397 void
8398 message1 (const char *m)
8400 message2 (m, (m ? strlen (m) : 0), 0);
8404 /* The non-logging counterpart of message1. */
8406 void
8407 message1_nolog (const char *m)
8409 message2_nolog (m, (m ? strlen (m) : 0), 0);
8412 /* Display a message M which contains a single %s
8413 which gets replaced with STRING. */
8415 void
8416 message_with_string (const char *m, Lisp_Object string, int log)
8418 CHECK_STRING (string);
8420 if (noninteractive)
8422 if (m)
8424 if (noninteractive_need_newline)
8425 putc ('\n', stderr);
8426 noninteractive_need_newline = 0;
8427 fprintf (stderr, m, SDATA (string));
8428 if (!cursor_in_echo_area)
8429 fprintf (stderr, "\n");
8430 fflush (stderr);
8433 else if (INTERACTIVE)
8435 /* The frame whose minibuffer we're going to display the message on.
8436 It may be larger than the selected frame, so we need
8437 to use its buffer, not the selected frame's buffer. */
8438 Lisp_Object mini_window;
8439 struct frame *f, *sf = SELECTED_FRAME ();
8441 /* Get the frame containing the minibuffer
8442 that the selected frame is using. */
8443 mini_window = FRAME_MINIBUF_WINDOW (sf);
8444 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8446 /* A null message buffer means that the frame hasn't really been
8447 initialized yet. Error messages get reported properly by
8448 cmd_error, so this must be just an informative message; toss it. */
8449 if (FRAME_MESSAGE_BUF (f))
8451 Lisp_Object args[2], message;
8452 struct gcpro gcpro1, gcpro2;
8454 args[0] = build_string (m);
8455 args[1] = message = string;
8456 GCPRO2 (args[0], message);
8457 gcpro1.nvars = 2;
8459 message = Fformat (2, args);
8461 if (log)
8462 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8463 else
8464 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8466 UNGCPRO;
8468 /* Print should start at the beginning of the message
8469 buffer next time. */
8470 message_buf_print = 0;
8476 /* Dump an informative message to the minibuf. If M is 0, clear out
8477 any existing message, and let the mini-buffer text show through. */
8479 static void
8480 vmessage (const char *m, va_list ap)
8482 if (noninteractive)
8484 if (m)
8486 if (noninteractive_need_newline)
8487 putc ('\n', stderr);
8488 noninteractive_need_newline = 0;
8489 vfprintf (stderr, m, ap);
8490 if (cursor_in_echo_area == 0)
8491 fprintf (stderr, "\n");
8492 fflush (stderr);
8495 else if (INTERACTIVE)
8497 /* The frame whose mini-buffer we're going to display the message
8498 on. It may be larger than the selected frame, so we need to
8499 use its buffer, not the selected frame's buffer. */
8500 Lisp_Object mini_window;
8501 struct frame *f, *sf = SELECTED_FRAME ();
8503 /* Get the frame containing the mini-buffer
8504 that the selected frame is using. */
8505 mini_window = FRAME_MINIBUF_WINDOW (sf);
8506 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8508 /* A null message buffer means that the frame hasn't really been
8509 initialized yet. Error messages get reported properly by
8510 cmd_error, so this must be just an informative message; toss
8511 it. */
8512 if (FRAME_MESSAGE_BUF (f))
8514 if (m)
8516 int len;
8518 len = doprnt (FRAME_MESSAGE_BUF (f),
8519 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8521 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8523 else
8524 message1 (0);
8526 /* Print should start at the beginning of the message
8527 buffer next time. */
8528 message_buf_print = 0;
8533 void
8534 message (const char *m, ...)
8536 va_list ap;
8537 va_start (ap, m);
8538 vmessage (m, ap);
8539 va_end (ap);
8543 /* The non-logging version of message. */
8545 void
8546 message_nolog (const char *m, ...)
8548 Lisp_Object old_log_max;
8549 va_list ap;
8550 va_start (ap, m);
8551 old_log_max = Vmessage_log_max;
8552 Vmessage_log_max = Qnil;
8553 vmessage (m, ap);
8554 Vmessage_log_max = old_log_max;
8555 va_end (ap);
8559 /* Display the current message in the current mini-buffer. This is
8560 only called from error handlers in process.c, and is not time
8561 critical. */
8563 void
8564 update_echo_area (void)
8566 if (!NILP (echo_area_buffer[0]))
8568 Lisp_Object string;
8569 string = Fcurrent_message ();
8570 message3 (string, SBYTES (string),
8571 !NILP (current_buffer->enable_multibyte_characters));
8576 /* Make sure echo area buffers in `echo_buffers' are live.
8577 If they aren't, make new ones. */
8579 static void
8580 ensure_echo_area_buffers (void)
8582 int i;
8584 for (i = 0; i < 2; ++i)
8585 if (!BUFFERP (echo_buffer[i])
8586 || NILP (XBUFFER (echo_buffer[i])->name))
8588 char name[30];
8589 Lisp_Object old_buffer;
8590 int j;
8592 old_buffer = echo_buffer[i];
8593 sprintf (name, " *Echo Area %d*", i);
8594 echo_buffer[i] = Fget_buffer_create (build_string (name));
8595 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8596 /* to force word wrap in echo area -
8597 it was decided to postpone this*/
8598 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8600 for (j = 0; j < 2; ++j)
8601 if (EQ (old_buffer, echo_area_buffer[j]))
8602 echo_area_buffer[j] = echo_buffer[i];
8607 /* Call FN with args A1..A4 with either the current or last displayed
8608 echo_area_buffer as current buffer.
8610 WHICH zero means use the current message buffer
8611 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8612 from echo_buffer[] and clear it.
8614 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8615 suitable buffer from echo_buffer[] and clear it.
8617 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8618 that the current message becomes the last displayed one, make
8619 choose a suitable buffer for echo_area_buffer[0], and clear it.
8621 Value is what FN returns. */
8623 static int
8624 with_echo_area_buffer (struct window *w, int which,
8625 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
8626 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8628 Lisp_Object buffer;
8629 int this_one, the_other, clear_buffer_p, rc;
8630 int count = SPECPDL_INDEX ();
8632 /* If buffers aren't live, make new ones. */
8633 ensure_echo_area_buffers ();
8635 clear_buffer_p = 0;
8637 if (which == 0)
8638 this_one = 0, the_other = 1;
8639 else if (which > 0)
8640 this_one = 1, the_other = 0;
8641 else
8643 this_one = 0, the_other = 1;
8644 clear_buffer_p = 1;
8646 /* We need a fresh one in case the current echo buffer equals
8647 the one containing the last displayed echo area message. */
8648 if (!NILP (echo_area_buffer[this_one])
8649 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8650 echo_area_buffer[this_one] = Qnil;
8653 /* Choose a suitable buffer from echo_buffer[] is we don't
8654 have one. */
8655 if (NILP (echo_area_buffer[this_one]))
8657 echo_area_buffer[this_one]
8658 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8659 ? echo_buffer[the_other]
8660 : echo_buffer[this_one]);
8661 clear_buffer_p = 1;
8664 buffer = echo_area_buffer[this_one];
8666 /* Don't get confused by reusing the buffer used for echoing
8667 for a different purpose. */
8668 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8669 cancel_echoing ();
8671 record_unwind_protect (unwind_with_echo_area_buffer,
8672 with_echo_area_buffer_unwind_data (w));
8674 /* Make the echo area buffer current. Note that for display
8675 purposes, it is not necessary that the displayed window's buffer
8676 == current_buffer, except for text property lookup. So, let's
8677 only set that buffer temporarily here without doing a full
8678 Fset_window_buffer. We must also change w->pointm, though,
8679 because otherwise an assertions in unshow_buffer fails, and Emacs
8680 aborts. */
8681 set_buffer_internal_1 (XBUFFER (buffer));
8682 if (w)
8684 w->buffer = buffer;
8685 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8688 current_buffer->undo_list = Qt;
8689 current_buffer->read_only = Qnil;
8690 specbind (Qinhibit_read_only, Qt);
8691 specbind (Qinhibit_modification_hooks, Qt);
8693 if (clear_buffer_p && Z > BEG)
8694 del_range (BEG, Z);
8696 xassert (BEGV >= BEG);
8697 xassert (ZV <= Z && ZV >= BEGV);
8699 rc = fn (a1, a2, a3, a4);
8701 xassert (BEGV >= BEG);
8702 xassert (ZV <= Z && ZV >= BEGV);
8704 unbind_to (count, Qnil);
8705 return rc;
8709 /* Save state that should be preserved around the call to the function
8710 FN called in with_echo_area_buffer. */
8712 static Lisp_Object
8713 with_echo_area_buffer_unwind_data (struct window *w)
8715 int i = 0;
8716 Lisp_Object vector, tmp;
8718 /* Reduce consing by keeping one vector in
8719 Vwith_echo_area_save_vector. */
8720 vector = Vwith_echo_area_save_vector;
8721 Vwith_echo_area_save_vector = Qnil;
8723 if (NILP (vector))
8724 vector = Fmake_vector (make_number (7), Qnil);
8726 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8727 ASET (vector, i, Vdeactivate_mark); ++i;
8728 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8730 if (w)
8732 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8733 ASET (vector, i, w->buffer); ++i;
8734 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8735 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8737 else
8739 int end = i + 4;
8740 for (; i < end; ++i)
8741 ASET (vector, i, Qnil);
8744 xassert (i == ASIZE (vector));
8745 return vector;
8749 /* Restore global state from VECTOR which was created by
8750 with_echo_area_buffer_unwind_data. */
8752 static Lisp_Object
8753 unwind_with_echo_area_buffer (Lisp_Object vector)
8755 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8756 Vdeactivate_mark = AREF (vector, 1);
8757 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8759 if (WINDOWP (AREF (vector, 3)))
8761 struct window *w;
8762 Lisp_Object buffer, charpos, bytepos;
8764 w = XWINDOW (AREF (vector, 3));
8765 buffer = AREF (vector, 4);
8766 charpos = AREF (vector, 5);
8767 bytepos = AREF (vector, 6);
8769 w->buffer = buffer;
8770 set_marker_both (w->pointm, buffer,
8771 XFASTINT (charpos), XFASTINT (bytepos));
8774 Vwith_echo_area_save_vector = vector;
8775 return Qnil;
8779 /* Set up the echo area for use by print functions. MULTIBYTE_P
8780 non-zero means we will print multibyte. */
8782 void
8783 setup_echo_area_for_printing (int multibyte_p)
8785 /* If we can't find an echo area any more, exit. */
8786 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8787 Fkill_emacs (Qnil);
8789 ensure_echo_area_buffers ();
8791 if (!message_buf_print)
8793 /* A message has been output since the last time we printed.
8794 Choose a fresh echo area buffer. */
8795 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8796 echo_area_buffer[0] = echo_buffer[1];
8797 else
8798 echo_area_buffer[0] = echo_buffer[0];
8800 /* Switch to that buffer and clear it. */
8801 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8802 current_buffer->truncate_lines = Qnil;
8804 if (Z > BEG)
8806 int count = SPECPDL_INDEX ();
8807 specbind (Qinhibit_read_only, Qt);
8808 /* Note that undo recording is always disabled. */
8809 del_range (BEG, Z);
8810 unbind_to (count, Qnil);
8812 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8814 /* Set up the buffer for the multibyteness we need. */
8815 if (multibyte_p
8816 != !NILP (current_buffer->enable_multibyte_characters))
8817 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8819 /* Raise the frame containing the echo area. */
8820 if (minibuffer_auto_raise)
8822 struct frame *sf = SELECTED_FRAME ();
8823 Lisp_Object mini_window;
8824 mini_window = FRAME_MINIBUF_WINDOW (sf);
8825 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8828 message_log_maybe_newline ();
8829 message_buf_print = 1;
8831 else
8833 if (NILP (echo_area_buffer[0]))
8835 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8836 echo_area_buffer[0] = echo_buffer[1];
8837 else
8838 echo_area_buffer[0] = echo_buffer[0];
8841 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8843 /* Someone switched buffers between print requests. */
8844 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8845 current_buffer->truncate_lines = Qnil;
8851 /* Display an echo area message in window W. Value is non-zero if W's
8852 height is changed. If display_last_displayed_message_p is
8853 non-zero, display the message that was last displayed, otherwise
8854 display the current message. */
8856 static int
8857 display_echo_area (struct window *w)
8859 int i, no_message_p, window_height_changed_p, count;
8861 /* Temporarily disable garbage collections while displaying the echo
8862 area. This is done because a GC can print a message itself.
8863 That message would modify the echo area buffer's contents while a
8864 redisplay of the buffer is going on, and seriously confuse
8865 redisplay. */
8866 count = inhibit_garbage_collection ();
8868 /* If there is no message, we must call display_echo_area_1
8869 nevertheless because it resizes the window. But we will have to
8870 reset the echo_area_buffer in question to nil at the end because
8871 with_echo_area_buffer will sets it to an empty buffer. */
8872 i = display_last_displayed_message_p ? 1 : 0;
8873 no_message_p = NILP (echo_area_buffer[i]);
8875 window_height_changed_p
8876 = with_echo_area_buffer (w, display_last_displayed_message_p,
8877 display_echo_area_1,
8878 (EMACS_INT) w, Qnil, 0, 0);
8880 if (no_message_p)
8881 echo_area_buffer[i] = Qnil;
8883 unbind_to (count, Qnil);
8884 return window_height_changed_p;
8888 /* Helper for display_echo_area. Display the current buffer which
8889 contains the current echo area message in window W, a mini-window,
8890 a pointer to which is passed in A1. A2..A4 are currently not used.
8891 Change the height of W so that all of the message is displayed.
8892 Value is non-zero if height of W was changed. */
8894 static int
8895 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
8897 struct window *w = (struct window *) a1;
8898 Lisp_Object window;
8899 struct text_pos start;
8900 int window_height_changed_p = 0;
8902 /* Do this before displaying, so that we have a large enough glyph
8903 matrix for the display. If we can't get enough space for the
8904 whole text, display the last N lines. That works by setting w->start. */
8905 window_height_changed_p = resize_mini_window (w, 0);
8907 /* Use the starting position chosen by resize_mini_window. */
8908 SET_TEXT_POS_FROM_MARKER (start, w->start);
8910 /* Display. */
8911 clear_glyph_matrix (w->desired_matrix);
8912 XSETWINDOW (window, w);
8913 try_window (window, start, 0);
8915 return window_height_changed_p;
8919 /* Resize the echo area window to exactly the size needed for the
8920 currently displayed message, if there is one. If a mini-buffer
8921 is active, don't shrink it. */
8923 void
8924 resize_echo_area_exactly (void)
8926 if (BUFFERP (echo_area_buffer[0])
8927 && WINDOWP (echo_area_window))
8929 struct window *w = XWINDOW (echo_area_window);
8930 int resized_p;
8931 Lisp_Object resize_exactly;
8933 if (minibuf_level == 0)
8934 resize_exactly = Qt;
8935 else
8936 resize_exactly = Qnil;
8938 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8939 (EMACS_INT) w, resize_exactly, 0, 0);
8940 if (resized_p)
8942 ++windows_or_buffers_changed;
8943 ++update_mode_lines;
8944 redisplay_internal (0);
8950 /* Callback function for with_echo_area_buffer, when used from
8951 resize_echo_area_exactly. A1 contains a pointer to the window to
8952 resize, EXACTLY non-nil means resize the mini-window exactly to the
8953 size of the text displayed. A3 and A4 are not used. Value is what
8954 resize_mini_window returns. */
8956 static int
8957 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
8959 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8963 /* Resize mini-window W to fit the size of its contents. EXACT_P
8964 means size the window exactly to the size needed. Otherwise, it's
8965 only enlarged until W's buffer is empty.
8967 Set W->start to the right place to begin display. If the whole
8968 contents fit, start at the beginning. Otherwise, start so as
8969 to make the end of the contents appear. This is particularly
8970 important for y-or-n-p, but seems desirable generally.
8972 Value is non-zero if the window height has been changed. */
8975 resize_mini_window (struct window *w, int exact_p)
8977 struct frame *f = XFRAME (w->frame);
8978 int window_height_changed_p = 0;
8980 xassert (MINI_WINDOW_P (w));
8982 /* By default, start display at the beginning. */
8983 set_marker_both (w->start, w->buffer,
8984 BUF_BEGV (XBUFFER (w->buffer)),
8985 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8987 /* Don't resize windows while redisplaying a window; it would
8988 confuse redisplay functions when the size of the window they are
8989 displaying changes from under them. Such a resizing can happen,
8990 for instance, when which-func prints a long message while
8991 we are running fontification-functions. We're running these
8992 functions with safe_call which binds inhibit-redisplay to t. */
8993 if (!NILP (Vinhibit_redisplay))
8994 return 0;
8996 /* Nil means don't try to resize. */
8997 if (NILP (Vresize_mini_windows)
8998 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8999 return 0;
9001 if (!FRAME_MINIBUF_ONLY_P (f))
9003 struct it it;
9004 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9005 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9006 int height, max_height;
9007 int unit = FRAME_LINE_HEIGHT (f);
9008 struct text_pos start;
9009 struct buffer *old_current_buffer = NULL;
9011 if (current_buffer != XBUFFER (w->buffer))
9013 old_current_buffer = current_buffer;
9014 set_buffer_internal (XBUFFER (w->buffer));
9017 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9019 /* Compute the max. number of lines specified by the user. */
9020 if (FLOATP (Vmax_mini_window_height))
9021 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9022 else if (INTEGERP (Vmax_mini_window_height))
9023 max_height = XINT (Vmax_mini_window_height);
9024 else
9025 max_height = total_height / 4;
9027 /* Correct that max. height if it's bogus. */
9028 max_height = max (1, max_height);
9029 max_height = min (total_height, max_height);
9031 /* Find out the height of the text in the window. */
9032 if (it.line_wrap == TRUNCATE)
9033 height = 1;
9034 else
9036 last_height = 0;
9037 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9038 if (it.max_ascent == 0 && it.max_descent == 0)
9039 height = it.current_y + last_height;
9040 else
9041 height = it.current_y + it.max_ascent + it.max_descent;
9042 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9043 height = (height + unit - 1) / unit;
9046 /* Compute a suitable window start. */
9047 if (height > max_height)
9049 height = max_height;
9050 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9051 move_it_vertically_backward (&it, (height - 1) * unit);
9052 start = it.current.pos;
9054 else
9055 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9056 SET_MARKER_FROM_TEXT_POS (w->start, start);
9058 if (EQ (Vresize_mini_windows, Qgrow_only))
9060 /* Let it grow only, until we display an empty message, in which
9061 case the window shrinks again. */
9062 if (height > WINDOW_TOTAL_LINES (w))
9064 int old_height = WINDOW_TOTAL_LINES (w);
9065 freeze_window_starts (f, 1);
9066 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9067 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9069 else if (height < WINDOW_TOTAL_LINES (w)
9070 && (exact_p || BEGV == ZV))
9072 int old_height = WINDOW_TOTAL_LINES (w);
9073 freeze_window_starts (f, 0);
9074 shrink_mini_window (w);
9075 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9078 else
9080 /* Always resize to exact size needed. */
9081 if (height > WINDOW_TOTAL_LINES (w))
9083 int old_height = WINDOW_TOTAL_LINES (w);
9084 freeze_window_starts (f, 1);
9085 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9086 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9088 else if (height < WINDOW_TOTAL_LINES (w))
9090 int old_height = WINDOW_TOTAL_LINES (w);
9091 freeze_window_starts (f, 0);
9092 shrink_mini_window (w);
9094 if (height)
9096 freeze_window_starts (f, 1);
9097 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9100 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9104 if (old_current_buffer)
9105 set_buffer_internal (old_current_buffer);
9108 return window_height_changed_p;
9112 /* Value is the current message, a string, or nil if there is no
9113 current message. */
9115 Lisp_Object
9116 current_message (void)
9118 Lisp_Object msg;
9120 if (!BUFFERP (echo_area_buffer[0]))
9121 msg = Qnil;
9122 else
9124 with_echo_area_buffer (0, 0, current_message_1,
9125 (EMACS_INT) &msg, Qnil, 0, 0);
9126 if (NILP (msg))
9127 echo_area_buffer[0] = Qnil;
9130 return msg;
9134 static int
9135 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9137 Lisp_Object *msg = (Lisp_Object *) a1;
9139 if (Z > BEG)
9140 *msg = make_buffer_string (BEG, Z, 1);
9141 else
9142 *msg = Qnil;
9143 return 0;
9147 /* Push the current message on Vmessage_stack for later restauration
9148 by restore_message. Value is non-zero if the current message isn't
9149 empty. This is a relatively infrequent operation, so it's not
9150 worth optimizing. */
9153 push_message (void)
9155 Lisp_Object msg;
9156 msg = current_message ();
9157 Vmessage_stack = Fcons (msg, Vmessage_stack);
9158 return STRINGP (msg);
9162 /* Restore message display from the top of Vmessage_stack. */
9164 void
9165 restore_message (void)
9167 Lisp_Object msg;
9169 xassert (CONSP (Vmessage_stack));
9170 msg = XCAR (Vmessage_stack);
9171 if (STRINGP (msg))
9172 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9173 else
9174 message3_nolog (msg, 0, 0);
9178 /* Handler for record_unwind_protect calling pop_message. */
9180 Lisp_Object
9181 pop_message_unwind (Lisp_Object dummy)
9183 pop_message ();
9184 return Qnil;
9187 /* Pop the top-most entry off Vmessage_stack. */
9189 void
9190 pop_message (void)
9192 xassert (CONSP (Vmessage_stack));
9193 Vmessage_stack = XCDR (Vmessage_stack);
9197 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9198 exits. If the stack is not empty, we have a missing pop_message
9199 somewhere. */
9201 void
9202 check_message_stack (void)
9204 if (!NILP (Vmessage_stack))
9205 abort ();
9209 /* Truncate to NCHARS what will be displayed in the echo area the next
9210 time we display it---but don't redisplay it now. */
9212 void
9213 truncate_echo_area (int nchars)
9215 if (nchars == 0)
9216 echo_area_buffer[0] = Qnil;
9217 /* A null message buffer means that the frame hasn't really been
9218 initialized yet. Error messages get reported properly by
9219 cmd_error, so this must be just an informative message; toss it. */
9220 else if (!noninteractive
9221 && INTERACTIVE
9222 && !NILP (echo_area_buffer[0]))
9224 struct frame *sf = SELECTED_FRAME ();
9225 if (FRAME_MESSAGE_BUF (sf))
9226 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9231 /* Helper function for truncate_echo_area. Truncate the current
9232 message to at most NCHARS characters. */
9234 static int
9235 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9237 if (BEG + nchars < Z)
9238 del_range (BEG + nchars, Z);
9239 if (Z == BEG)
9240 echo_area_buffer[0] = Qnil;
9241 return 0;
9245 /* Set the current message to a substring of S or STRING.
9247 If STRING is a Lisp string, set the message to the first NBYTES
9248 bytes from STRING. NBYTES zero means use the whole string. If
9249 STRING is multibyte, the message will be displayed multibyte.
9251 If S is not null, set the message to the first LEN bytes of S. LEN
9252 zero means use the whole string. MULTIBYTE_P non-zero means S is
9253 multibyte. Display the message multibyte in that case.
9255 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9256 to t before calling set_message_1 (which calls insert).
9259 void
9260 set_message (const char *s, Lisp_Object string, int nbytes, int multibyte_p)
9262 message_enable_multibyte
9263 = ((s && multibyte_p)
9264 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9266 with_echo_area_buffer (0, -1, set_message_1,
9267 (EMACS_INT) s, string, nbytes, multibyte_p);
9268 message_buf_print = 0;
9269 help_echo_showing_p = 0;
9273 /* Helper function for set_message. Arguments have the same meaning
9274 as there, with A1 corresponding to S and A2 corresponding to STRING
9275 This function is called with the echo area buffer being
9276 current. */
9278 static int
9279 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9281 const char *s = (const char *) a1;
9282 Lisp_Object string = a2;
9284 /* Change multibyteness of the echo buffer appropriately. */
9285 if (message_enable_multibyte
9286 != !NILP (current_buffer->enable_multibyte_characters))
9287 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9289 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9291 /* Insert new message at BEG. */
9292 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9294 if (STRINGP (string))
9296 int nchars;
9298 if (nbytes == 0)
9299 nbytes = SBYTES (string);
9300 nchars = string_byte_to_char (string, nbytes);
9302 /* This function takes care of single/multibyte conversion. We
9303 just have to ensure that the echo area buffer has the right
9304 setting of enable_multibyte_characters. */
9305 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9307 else if (s)
9309 if (nbytes == 0)
9310 nbytes = strlen (s);
9312 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9314 /* Convert from multi-byte to single-byte. */
9315 int i, c, n;
9316 unsigned char work[1];
9318 /* Convert a multibyte string to single-byte. */
9319 for (i = 0; i < nbytes; i += n)
9321 c = string_char_and_length (s + i, &n);
9322 work[0] = (ASCII_CHAR_P (c)
9324 : multibyte_char_to_unibyte (c, Qnil));
9325 insert_1_both (work, 1, 1, 1, 0, 0);
9328 else if (!multibyte_p
9329 && !NILP (current_buffer->enable_multibyte_characters))
9331 /* Convert from single-byte to multi-byte. */
9332 int i, c, n;
9333 const unsigned char *msg = (const unsigned char *) s;
9334 unsigned char str[MAX_MULTIBYTE_LENGTH];
9336 /* Convert a single-byte string to multibyte. */
9337 for (i = 0; i < nbytes; i++)
9339 c = msg[i];
9340 MAKE_CHAR_MULTIBYTE (c);
9341 n = CHAR_STRING (c, str);
9342 insert_1_both (str, 1, n, 1, 0, 0);
9345 else
9346 insert_1 (s, nbytes, 1, 0, 0);
9349 return 0;
9353 /* Clear messages. CURRENT_P non-zero means clear the current
9354 message. LAST_DISPLAYED_P non-zero means clear the message
9355 last displayed. */
9357 void
9358 clear_message (int current_p, int last_displayed_p)
9360 if (current_p)
9362 echo_area_buffer[0] = Qnil;
9363 message_cleared_p = 1;
9366 if (last_displayed_p)
9367 echo_area_buffer[1] = Qnil;
9369 message_buf_print = 0;
9372 /* Clear garbaged frames.
9374 This function is used where the old redisplay called
9375 redraw_garbaged_frames which in turn called redraw_frame which in
9376 turn called clear_frame. The call to clear_frame was a source of
9377 flickering. I believe a clear_frame is not necessary. It should
9378 suffice in the new redisplay to invalidate all current matrices,
9379 and ensure a complete redisplay of all windows. */
9381 static void
9382 clear_garbaged_frames (void)
9384 if (frame_garbaged)
9386 Lisp_Object tail, frame;
9387 int changed_count = 0;
9389 FOR_EACH_FRAME (tail, frame)
9391 struct frame *f = XFRAME (frame);
9393 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9395 if (f->resized_p)
9397 Fredraw_frame (frame);
9398 f->force_flush_display_p = 1;
9400 clear_current_matrices (f);
9401 changed_count++;
9402 f->garbaged = 0;
9403 f->resized_p = 0;
9407 frame_garbaged = 0;
9408 if (changed_count)
9409 ++windows_or_buffers_changed;
9414 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9415 is non-zero update selected_frame. Value is non-zero if the
9416 mini-windows height has been changed. */
9418 static int
9419 echo_area_display (int update_frame_p)
9421 Lisp_Object mini_window;
9422 struct window *w;
9423 struct frame *f;
9424 int window_height_changed_p = 0;
9425 struct frame *sf = SELECTED_FRAME ();
9427 mini_window = FRAME_MINIBUF_WINDOW (sf);
9428 w = XWINDOW (mini_window);
9429 f = XFRAME (WINDOW_FRAME (w));
9431 /* Don't display if frame is invisible or not yet initialized. */
9432 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9433 return 0;
9435 #ifdef HAVE_WINDOW_SYSTEM
9436 /* When Emacs starts, selected_frame may be the initial terminal
9437 frame. If we let this through, a message would be displayed on
9438 the terminal. */
9439 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9440 return 0;
9441 #endif /* HAVE_WINDOW_SYSTEM */
9443 /* Redraw garbaged frames. */
9444 if (frame_garbaged)
9445 clear_garbaged_frames ();
9447 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9449 echo_area_window = mini_window;
9450 window_height_changed_p = display_echo_area (w);
9451 w->must_be_updated_p = 1;
9453 /* Update the display, unless called from redisplay_internal.
9454 Also don't update the screen during redisplay itself. The
9455 update will happen at the end of redisplay, and an update
9456 here could cause confusion. */
9457 if (update_frame_p && !redisplaying_p)
9459 int n = 0;
9461 /* If the display update has been interrupted by pending
9462 input, update mode lines in the frame. Due to the
9463 pending input, it might have been that redisplay hasn't
9464 been called, so that mode lines above the echo area are
9465 garbaged. This looks odd, so we prevent it here. */
9466 if (!display_completed)
9467 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9469 if (window_height_changed_p
9470 /* Don't do this if Emacs is shutting down. Redisplay
9471 needs to run hooks. */
9472 && !NILP (Vrun_hooks))
9474 /* Must update other windows. Likewise as in other
9475 cases, don't let this update be interrupted by
9476 pending input. */
9477 int count = SPECPDL_INDEX ();
9478 specbind (Qredisplay_dont_pause, Qt);
9479 windows_or_buffers_changed = 1;
9480 redisplay_internal (0);
9481 unbind_to (count, Qnil);
9483 else if (FRAME_WINDOW_P (f) && n == 0)
9485 /* Window configuration is the same as before.
9486 Can do with a display update of the echo area,
9487 unless we displayed some mode lines. */
9488 update_single_window (w, 1);
9489 FRAME_RIF (f)->flush_display (f);
9491 else
9492 update_frame (f, 1, 1);
9494 /* If cursor is in the echo area, make sure that the next
9495 redisplay displays the minibuffer, so that the cursor will
9496 be replaced with what the minibuffer wants. */
9497 if (cursor_in_echo_area)
9498 ++windows_or_buffers_changed;
9501 else if (!EQ (mini_window, selected_window))
9502 windows_or_buffers_changed++;
9504 /* Last displayed message is now the current message. */
9505 echo_area_buffer[1] = echo_area_buffer[0];
9506 /* Inform read_char that we're not echoing. */
9507 echo_message_buffer = Qnil;
9509 /* Prevent redisplay optimization in redisplay_internal by resetting
9510 this_line_start_pos. This is done because the mini-buffer now
9511 displays the message instead of its buffer text. */
9512 if (EQ (mini_window, selected_window))
9513 CHARPOS (this_line_start_pos) = 0;
9515 return window_height_changed_p;
9520 /***********************************************************************
9521 Mode Lines and Frame Titles
9522 ***********************************************************************/
9524 /* A buffer for constructing non-propertized mode-line strings and
9525 frame titles in it; allocated from the heap in init_xdisp and
9526 resized as needed in store_mode_line_noprop_char. */
9528 static char *mode_line_noprop_buf;
9530 /* The buffer's end, and a current output position in it. */
9532 static char *mode_line_noprop_buf_end;
9533 static char *mode_line_noprop_ptr;
9535 #define MODE_LINE_NOPROP_LEN(start) \
9536 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9538 static enum {
9539 MODE_LINE_DISPLAY = 0,
9540 MODE_LINE_TITLE,
9541 MODE_LINE_NOPROP,
9542 MODE_LINE_STRING
9543 } mode_line_target;
9545 /* Alist that caches the results of :propertize.
9546 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9547 static Lisp_Object mode_line_proptrans_alist;
9549 /* List of strings making up the mode-line. */
9550 static Lisp_Object mode_line_string_list;
9552 /* Base face property when building propertized mode line string. */
9553 static Lisp_Object mode_line_string_face;
9554 static Lisp_Object mode_line_string_face_prop;
9557 /* Unwind data for mode line strings */
9559 static Lisp_Object Vmode_line_unwind_vector;
9561 static Lisp_Object
9562 format_mode_line_unwind_data (struct buffer *obuf,
9563 Lisp_Object owin,
9564 int save_proptrans)
9566 Lisp_Object vector, tmp;
9568 /* Reduce consing by keeping one vector in
9569 Vwith_echo_area_save_vector. */
9570 vector = Vmode_line_unwind_vector;
9571 Vmode_line_unwind_vector = Qnil;
9573 if (NILP (vector))
9574 vector = Fmake_vector (make_number (8), Qnil);
9576 ASET (vector, 0, make_number (mode_line_target));
9577 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9578 ASET (vector, 2, mode_line_string_list);
9579 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9580 ASET (vector, 4, mode_line_string_face);
9581 ASET (vector, 5, mode_line_string_face_prop);
9583 if (obuf)
9584 XSETBUFFER (tmp, obuf);
9585 else
9586 tmp = Qnil;
9587 ASET (vector, 6, tmp);
9588 ASET (vector, 7, owin);
9590 return vector;
9593 static Lisp_Object
9594 unwind_format_mode_line (Lisp_Object vector)
9596 mode_line_target = XINT (AREF (vector, 0));
9597 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9598 mode_line_string_list = AREF (vector, 2);
9599 if (! EQ (AREF (vector, 3), Qt))
9600 mode_line_proptrans_alist = AREF (vector, 3);
9601 mode_line_string_face = AREF (vector, 4);
9602 mode_line_string_face_prop = AREF (vector, 5);
9604 if (!NILP (AREF (vector, 7)))
9605 /* Select window before buffer, since it may change the buffer. */
9606 Fselect_window (AREF (vector, 7), Qt);
9608 if (!NILP (AREF (vector, 6)))
9610 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9611 ASET (vector, 6, Qnil);
9614 Vmode_line_unwind_vector = vector;
9615 return Qnil;
9619 /* Store a single character C for the frame title in mode_line_noprop_buf.
9620 Re-allocate mode_line_noprop_buf if necessary. */
9622 static void
9623 store_mode_line_noprop_char (char c)
9625 /* If output position has reached the end of the allocated buffer,
9626 double the buffer's size. */
9627 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9629 int len = MODE_LINE_NOPROP_LEN (0);
9630 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9631 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9632 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9633 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9636 *mode_line_noprop_ptr++ = c;
9640 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9641 mode_line_noprop_ptr. STR is the string to store. Do not copy
9642 characters that yield more columns than PRECISION; PRECISION <= 0
9643 means copy the whole string. Pad with spaces until FIELD_WIDTH
9644 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9645 pad. Called from display_mode_element when it is used to build a
9646 frame title. */
9648 static int
9649 store_mode_line_noprop (const unsigned char *str, int field_width, int precision)
9651 int n = 0;
9652 int dummy, nbytes;
9654 /* Copy at most PRECISION chars from STR. */
9655 nbytes = strlen (str);
9656 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9657 while (nbytes--)
9658 store_mode_line_noprop_char (*str++);
9660 /* Fill up with spaces until FIELD_WIDTH reached. */
9661 while (field_width > 0
9662 && n < field_width)
9664 store_mode_line_noprop_char (' ');
9665 ++n;
9668 return n;
9671 /***********************************************************************
9672 Frame Titles
9673 ***********************************************************************/
9675 #ifdef HAVE_WINDOW_SYSTEM
9677 /* Set the title of FRAME, if it has changed. The title format is
9678 Vicon_title_format if FRAME is iconified, otherwise it is
9679 frame_title_format. */
9681 static void
9682 x_consider_frame_title (Lisp_Object frame)
9684 struct frame *f = XFRAME (frame);
9686 if (FRAME_WINDOW_P (f)
9687 || FRAME_MINIBUF_ONLY_P (f)
9688 || f->explicit_name)
9690 /* Do we have more than one visible frame on this X display? */
9691 Lisp_Object tail;
9692 Lisp_Object fmt;
9693 int title_start;
9694 char *title;
9695 int len;
9696 struct it it;
9697 int count = SPECPDL_INDEX ();
9699 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9701 Lisp_Object other_frame = XCAR (tail);
9702 struct frame *tf = XFRAME (other_frame);
9704 if (tf != f
9705 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9706 && !FRAME_MINIBUF_ONLY_P (tf)
9707 && !EQ (other_frame, tip_frame)
9708 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9709 break;
9712 /* Set global variable indicating that multiple frames exist. */
9713 multiple_frames = CONSP (tail);
9715 /* Switch to the buffer of selected window of the frame. Set up
9716 mode_line_target so that display_mode_element will output into
9717 mode_line_noprop_buf; then display the title. */
9718 record_unwind_protect (unwind_format_mode_line,
9719 format_mode_line_unwind_data
9720 (current_buffer, selected_window, 0));
9722 Fselect_window (f->selected_window, Qt);
9723 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9724 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9726 mode_line_target = MODE_LINE_TITLE;
9727 title_start = MODE_LINE_NOPROP_LEN (0);
9728 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9729 NULL, DEFAULT_FACE_ID);
9730 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9731 len = MODE_LINE_NOPROP_LEN (title_start);
9732 title = mode_line_noprop_buf + title_start;
9733 unbind_to (count, Qnil);
9735 /* Set the title only if it's changed. This avoids consing in
9736 the common case where it hasn't. (If it turns out that we've
9737 already wasted too much time by walking through the list with
9738 display_mode_element, then we might need to optimize at a
9739 higher level than this.) */
9740 if (! STRINGP (f->name)
9741 || SBYTES (f->name) != len
9742 || memcmp (title, SDATA (f->name), len) != 0)
9743 x_implicitly_set_name (f, make_string (title, len), Qnil);
9747 #endif /* not HAVE_WINDOW_SYSTEM */
9752 /***********************************************************************
9753 Menu Bars
9754 ***********************************************************************/
9757 /* Prepare for redisplay by updating menu-bar item lists when
9758 appropriate. This can call eval. */
9760 void
9761 prepare_menu_bars (void)
9763 int all_windows;
9764 struct gcpro gcpro1, gcpro2;
9765 struct frame *f;
9766 Lisp_Object tooltip_frame;
9768 #ifdef HAVE_WINDOW_SYSTEM
9769 tooltip_frame = tip_frame;
9770 #else
9771 tooltip_frame = Qnil;
9772 #endif
9774 /* Update all frame titles based on their buffer names, etc. We do
9775 this before the menu bars so that the buffer-menu will show the
9776 up-to-date frame titles. */
9777 #ifdef HAVE_WINDOW_SYSTEM
9778 if (windows_or_buffers_changed || update_mode_lines)
9780 Lisp_Object tail, frame;
9782 FOR_EACH_FRAME (tail, frame)
9784 f = XFRAME (frame);
9785 if (!EQ (frame, tooltip_frame)
9786 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9787 x_consider_frame_title (frame);
9790 #endif /* HAVE_WINDOW_SYSTEM */
9792 /* Update the menu bar item lists, if appropriate. This has to be
9793 done before any actual redisplay or generation of display lines. */
9794 all_windows = (update_mode_lines
9795 || buffer_shared > 1
9796 || windows_or_buffers_changed);
9797 if (all_windows)
9799 Lisp_Object tail, frame;
9800 int count = SPECPDL_INDEX ();
9801 /* 1 means that update_menu_bar has run its hooks
9802 so any further calls to update_menu_bar shouldn't do so again. */
9803 int menu_bar_hooks_run = 0;
9805 record_unwind_save_match_data ();
9807 FOR_EACH_FRAME (tail, frame)
9809 f = XFRAME (frame);
9811 /* Ignore tooltip frame. */
9812 if (EQ (frame, tooltip_frame))
9813 continue;
9815 /* If a window on this frame changed size, report that to
9816 the user and clear the size-change flag. */
9817 if (FRAME_WINDOW_SIZES_CHANGED (f))
9819 Lisp_Object functions;
9821 /* Clear flag first in case we get an error below. */
9822 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9823 functions = Vwindow_size_change_functions;
9824 GCPRO2 (tail, functions);
9826 while (CONSP (functions))
9828 if (!EQ (XCAR (functions), Qt))
9829 call1 (XCAR (functions), frame);
9830 functions = XCDR (functions);
9832 UNGCPRO;
9835 GCPRO1 (tail);
9836 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9837 #ifdef HAVE_WINDOW_SYSTEM
9838 update_tool_bar (f, 0);
9839 #endif
9840 #ifdef HAVE_NS
9841 if (windows_or_buffers_changed
9842 && FRAME_NS_P (f))
9843 ns_set_doc_edited (f, Fbuffer_modified_p
9844 (XWINDOW (f->selected_window)->buffer));
9845 #endif
9846 UNGCPRO;
9849 unbind_to (count, Qnil);
9851 else
9853 struct frame *sf = SELECTED_FRAME ();
9854 update_menu_bar (sf, 1, 0);
9855 #ifdef HAVE_WINDOW_SYSTEM
9856 update_tool_bar (sf, 1);
9857 #endif
9862 /* Update the menu bar item list for frame F. This has to be done
9863 before we start to fill in any display lines, because it can call
9864 eval.
9866 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9868 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9869 already ran the menu bar hooks for this redisplay, so there
9870 is no need to run them again. The return value is the
9871 updated value of this flag, to pass to the next call. */
9873 static int
9874 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
9876 Lisp_Object window;
9877 register struct window *w;
9879 /* If called recursively during a menu update, do nothing. This can
9880 happen when, for instance, an activate-menubar-hook causes a
9881 redisplay. */
9882 if (inhibit_menubar_update)
9883 return hooks_run;
9885 window = FRAME_SELECTED_WINDOW (f);
9886 w = XWINDOW (window);
9888 if (FRAME_WINDOW_P (f)
9890 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9891 || defined (HAVE_NS) || defined (USE_GTK)
9892 FRAME_EXTERNAL_MENU_BAR (f)
9893 #else
9894 FRAME_MENU_BAR_LINES (f) > 0
9895 #endif
9896 : FRAME_MENU_BAR_LINES (f) > 0)
9898 /* If the user has switched buffers or windows, we need to
9899 recompute to reflect the new bindings. But we'll
9900 recompute when update_mode_lines is set too; that means
9901 that people can use force-mode-line-update to request
9902 that the menu bar be recomputed. The adverse effect on
9903 the rest of the redisplay algorithm is about the same as
9904 windows_or_buffers_changed anyway. */
9905 if (windows_or_buffers_changed
9906 /* This used to test w->update_mode_line, but we believe
9907 there is no need to recompute the menu in that case. */
9908 || update_mode_lines
9909 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9910 < BUF_MODIFF (XBUFFER (w->buffer)))
9911 != !NILP (w->last_had_star))
9912 || ((!NILP (Vtransient_mark_mode)
9913 && !NILP (XBUFFER (w->buffer)->mark_active))
9914 != !NILP (w->region_showing)))
9916 struct buffer *prev = current_buffer;
9917 int count = SPECPDL_INDEX ();
9919 specbind (Qinhibit_menubar_update, Qt);
9921 set_buffer_internal_1 (XBUFFER (w->buffer));
9922 if (save_match_data)
9923 record_unwind_save_match_data ();
9924 if (NILP (Voverriding_local_map_menu_flag))
9926 specbind (Qoverriding_terminal_local_map, Qnil);
9927 specbind (Qoverriding_local_map, Qnil);
9930 if (!hooks_run)
9932 /* Run the Lucid hook. */
9933 safe_run_hooks (Qactivate_menubar_hook);
9935 /* If it has changed current-menubar from previous value,
9936 really recompute the menu-bar from the value. */
9937 if (! NILP (Vlucid_menu_bar_dirty_flag))
9938 call0 (Qrecompute_lucid_menubar);
9940 safe_run_hooks (Qmenu_bar_update_hook);
9942 hooks_run = 1;
9945 XSETFRAME (Vmenu_updating_frame, f);
9946 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9948 /* Redisplay the menu bar in case we changed it. */
9949 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9950 || defined (HAVE_NS) || defined (USE_GTK)
9951 if (FRAME_WINDOW_P (f))
9953 #if defined (HAVE_NS)
9954 /* All frames on Mac OS share the same menubar. So only
9955 the selected frame should be allowed to set it. */
9956 if (f == SELECTED_FRAME ())
9957 #endif
9958 set_frame_menubar (f, 0, 0);
9960 else
9961 /* On a terminal screen, the menu bar is an ordinary screen
9962 line, and this makes it get updated. */
9963 w->update_mode_line = Qt;
9964 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9965 /* In the non-toolkit version, the menu bar is an ordinary screen
9966 line, and this makes it get updated. */
9967 w->update_mode_line = Qt;
9968 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9970 unbind_to (count, Qnil);
9971 set_buffer_internal_1 (prev);
9975 return hooks_run;
9980 /***********************************************************************
9981 Output Cursor
9982 ***********************************************************************/
9984 #ifdef HAVE_WINDOW_SYSTEM
9986 /* EXPORT:
9987 Nominal cursor position -- where to draw output.
9988 HPOS and VPOS are window relative glyph matrix coordinates.
9989 X and Y are window relative pixel coordinates. */
9991 struct cursor_pos output_cursor;
9994 /* EXPORT:
9995 Set the global variable output_cursor to CURSOR. All cursor
9996 positions are relative to updated_window. */
9998 void
9999 set_output_cursor (struct cursor_pos *cursor)
10001 output_cursor.hpos = cursor->hpos;
10002 output_cursor.vpos = cursor->vpos;
10003 output_cursor.x = cursor->x;
10004 output_cursor.y = cursor->y;
10008 /* EXPORT for RIF:
10009 Set a nominal cursor position.
10011 HPOS and VPOS are column/row positions in a window glyph matrix. X
10012 and Y are window text area relative pixel positions.
10014 If this is done during an update, updated_window will contain the
10015 window that is being updated and the position is the future output
10016 cursor position for that window. If updated_window is null, use
10017 selected_window and display the cursor at the given position. */
10019 void
10020 x_cursor_to (int vpos, int hpos, int y, int x)
10022 struct window *w;
10024 /* If updated_window is not set, work on selected_window. */
10025 if (updated_window)
10026 w = updated_window;
10027 else
10028 w = XWINDOW (selected_window);
10030 /* Set the output cursor. */
10031 output_cursor.hpos = hpos;
10032 output_cursor.vpos = vpos;
10033 output_cursor.x = x;
10034 output_cursor.y = y;
10036 /* If not called as part of an update, really display the cursor.
10037 This will also set the cursor position of W. */
10038 if (updated_window == NULL)
10040 BLOCK_INPUT;
10041 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10042 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10043 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10044 UNBLOCK_INPUT;
10048 #endif /* HAVE_WINDOW_SYSTEM */
10051 /***********************************************************************
10052 Tool-bars
10053 ***********************************************************************/
10055 #ifdef HAVE_WINDOW_SYSTEM
10057 /* Where the mouse was last time we reported a mouse event. */
10059 FRAME_PTR last_mouse_frame;
10061 /* Tool-bar item index of the item on which a mouse button was pressed
10062 or -1. */
10064 int last_tool_bar_item;
10067 static Lisp_Object
10068 update_tool_bar_unwind (Lisp_Object frame)
10070 selected_frame = frame;
10071 return Qnil;
10074 /* Update the tool-bar item list for frame F. This has to be done
10075 before we start to fill in any display lines. Called from
10076 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10077 and restore it here. */
10079 static void
10080 update_tool_bar (struct frame *f, int save_match_data)
10082 #if defined (USE_GTK) || defined (HAVE_NS)
10083 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10084 #else
10085 int do_update = WINDOWP (f->tool_bar_window)
10086 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10087 #endif
10089 if (do_update)
10091 Lisp_Object window;
10092 struct window *w;
10094 window = FRAME_SELECTED_WINDOW (f);
10095 w = XWINDOW (window);
10097 /* If the user has switched buffers or windows, we need to
10098 recompute to reflect the new bindings. But we'll
10099 recompute when update_mode_lines is set too; that means
10100 that people can use force-mode-line-update to request
10101 that the menu bar be recomputed. The adverse effect on
10102 the rest of the redisplay algorithm is about the same as
10103 windows_or_buffers_changed anyway. */
10104 if (windows_or_buffers_changed
10105 || !NILP (w->update_mode_line)
10106 || update_mode_lines
10107 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10108 < BUF_MODIFF (XBUFFER (w->buffer)))
10109 != !NILP (w->last_had_star))
10110 || ((!NILP (Vtransient_mark_mode)
10111 && !NILP (XBUFFER (w->buffer)->mark_active))
10112 != !NILP (w->region_showing)))
10114 struct buffer *prev = current_buffer;
10115 int count = SPECPDL_INDEX ();
10116 Lisp_Object frame, new_tool_bar;
10117 int new_n_tool_bar;
10118 struct gcpro gcpro1;
10120 /* Set current_buffer to the buffer of the selected
10121 window of the frame, so that we get the right local
10122 keymaps. */
10123 set_buffer_internal_1 (XBUFFER (w->buffer));
10125 /* Save match data, if we must. */
10126 if (save_match_data)
10127 record_unwind_save_match_data ();
10129 /* Make sure that we don't accidentally use bogus keymaps. */
10130 if (NILP (Voverriding_local_map_menu_flag))
10132 specbind (Qoverriding_terminal_local_map, Qnil);
10133 specbind (Qoverriding_local_map, Qnil);
10136 GCPRO1 (new_tool_bar);
10138 /* We must temporarily set the selected frame to this frame
10139 before calling tool_bar_items, because the calculation of
10140 the tool-bar keymap uses the selected frame (see
10141 `tool-bar-make-keymap' in tool-bar.el). */
10142 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10143 XSETFRAME (frame, f);
10144 selected_frame = frame;
10146 /* Build desired tool-bar items from keymaps. */
10147 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10148 &new_n_tool_bar);
10150 /* Redisplay the tool-bar if we changed it. */
10151 if (new_n_tool_bar != f->n_tool_bar_items
10152 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10154 /* Redisplay that happens asynchronously due to an expose event
10155 may access f->tool_bar_items. Make sure we update both
10156 variables within BLOCK_INPUT so no such event interrupts. */
10157 BLOCK_INPUT;
10158 f->tool_bar_items = new_tool_bar;
10159 f->n_tool_bar_items = new_n_tool_bar;
10160 w->update_mode_line = Qt;
10161 UNBLOCK_INPUT;
10164 UNGCPRO;
10166 unbind_to (count, Qnil);
10167 set_buffer_internal_1 (prev);
10173 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10174 F's desired tool-bar contents. F->tool_bar_items must have
10175 been set up previously by calling prepare_menu_bars. */
10177 static void
10178 build_desired_tool_bar_string (struct frame *f)
10180 int i, size, size_needed;
10181 struct gcpro gcpro1, gcpro2, gcpro3;
10182 Lisp_Object image, plist, props;
10184 image = plist = props = Qnil;
10185 GCPRO3 (image, plist, props);
10187 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10188 Otherwise, make a new string. */
10190 /* The size of the string we might be able to reuse. */
10191 size = (STRINGP (f->desired_tool_bar_string)
10192 ? SCHARS (f->desired_tool_bar_string)
10193 : 0);
10195 /* We need one space in the string for each image. */
10196 size_needed = f->n_tool_bar_items;
10198 /* Reuse f->desired_tool_bar_string, if possible. */
10199 if (size < size_needed || NILP (f->desired_tool_bar_string))
10200 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10201 make_number (' '));
10202 else
10204 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10205 Fremove_text_properties (make_number (0), make_number (size),
10206 props, f->desired_tool_bar_string);
10209 /* Put a `display' property on the string for the images to display,
10210 put a `menu_item' property on tool-bar items with a value that
10211 is the index of the item in F's tool-bar item vector. */
10212 for (i = 0; i < f->n_tool_bar_items; ++i)
10214 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10216 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10217 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10218 int hmargin, vmargin, relief, idx, end;
10219 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10221 /* If image is a vector, choose the image according to the
10222 button state. */
10223 image = PROP (TOOL_BAR_ITEM_IMAGES);
10224 if (VECTORP (image))
10226 if (enabled_p)
10227 idx = (selected_p
10228 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10229 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10230 else
10231 idx = (selected_p
10232 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10233 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10235 xassert (ASIZE (image) >= idx);
10236 image = AREF (image, idx);
10238 else
10239 idx = -1;
10241 /* Ignore invalid image specifications. */
10242 if (!valid_image_p (image))
10243 continue;
10245 /* Display the tool-bar button pressed, or depressed. */
10246 plist = Fcopy_sequence (XCDR (image));
10248 /* Compute margin and relief to draw. */
10249 relief = (tool_bar_button_relief >= 0
10250 ? tool_bar_button_relief
10251 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10252 hmargin = vmargin = relief;
10254 if (INTEGERP (Vtool_bar_button_margin)
10255 && XINT (Vtool_bar_button_margin) > 0)
10257 hmargin += XFASTINT (Vtool_bar_button_margin);
10258 vmargin += XFASTINT (Vtool_bar_button_margin);
10260 else if (CONSP (Vtool_bar_button_margin))
10262 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10263 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10264 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10266 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10267 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10268 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10271 if (auto_raise_tool_bar_buttons_p)
10273 /* Add a `:relief' property to the image spec if the item is
10274 selected. */
10275 if (selected_p)
10277 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10278 hmargin -= relief;
10279 vmargin -= relief;
10282 else
10284 /* If image is selected, display it pressed, i.e. with a
10285 negative relief. If it's not selected, display it with a
10286 raised relief. */
10287 plist = Fplist_put (plist, QCrelief,
10288 (selected_p
10289 ? make_number (-relief)
10290 : make_number (relief)));
10291 hmargin -= relief;
10292 vmargin -= relief;
10295 /* Put a margin around the image. */
10296 if (hmargin || vmargin)
10298 if (hmargin == vmargin)
10299 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10300 else
10301 plist = Fplist_put (plist, QCmargin,
10302 Fcons (make_number (hmargin),
10303 make_number (vmargin)));
10306 /* If button is not enabled, and we don't have special images
10307 for the disabled state, make the image appear disabled by
10308 applying an appropriate algorithm to it. */
10309 if (!enabled_p && idx < 0)
10310 plist = Fplist_put (plist, QCconversion, Qdisabled);
10312 /* Put a `display' text property on the string for the image to
10313 display. Put a `menu-item' property on the string that gives
10314 the start of this item's properties in the tool-bar items
10315 vector. */
10316 image = Fcons (Qimage, plist);
10317 props = list4 (Qdisplay, image,
10318 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10320 /* Let the last image hide all remaining spaces in the tool bar
10321 string. The string can be longer than needed when we reuse a
10322 previous string. */
10323 if (i + 1 == f->n_tool_bar_items)
10324 end = SCHARS (f->desired_tool_bar_string);
10325 else
10326 end = i + 1;
10327 Fadd_text_properties (make_number (i), make_number (end),
10328 props, f->desired_tool_bar_string);
10329 #undef PROP
10332 UNGCPRO;
10336 /* Display one line of the tool-bar of frame IT->f.
10338 HEIGHT specifies the desired height of the tool-bar line.
10339 If the actual height of the glyph row is less than HEIGHT, the
10340 row's height is increased to HEIGHT, and the icons are centered
10341 vertically in the new height.
10343 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10344 count a final empty row in case the tool-bar width exactly matches
10345 the window width.
10348 static void
10349 display_tool_bar_line (struct it *it, int height)
10351 struct glyph_row *row = it->glyph_row;
10352 int max_x = it->last_visible_x;
10353 struct glyph *last;
10355 prepare_desired_row (row);
10356 row->y = it->current_y;
10358 /* Note that this isn't made use of if the face hasn't a box,
10359 so there's no need to check the face here. */
10360 it->start_of_box_run_p = 1;
10362 while (it->current_x < max_x)
10364 int x, n_glyphs_before, i, nglyphs;
10365 struct it it_before;
10367 /* Get the next display element. */
10368 if (!get_next_display_element (it))
10370 /* Don't count empty row if we are counting needed tool-bar lines. */
10371 if (height < 0 && !it->hpos)
10372 return;
10373 break;
10376 /* Produce glyphs. */
10377 n_glyphs_before = row->used[TEXT_AREA];
10378 it_before = *it;
10380 PRODUCE_GLYPHS (it);
10382 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10383 i = 0;
10384 x = it_before.current_x;
10385 while (i < nglyphs)
10387 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10389 if (x + glyph->pixel_width > max_x)
10391 /* Glyph doesn't fit on line. Backtrack. */
10392 row->used[TEXT_AREA] = n_glyphs_before;
10393 *it = it_before;
10394 /* If this is the only glyph on this line, it will never fit on the
10395 toolbar, so skip it. But ensure there is at least one glyph,
10396 so we don't accidentally disable the tool-bar. */
10397 if (n_glyphs_before == 0
10398 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10399 break;
10400 goto out;
10403 ++it->hpos;
10404 x += glyph->pixel_width;
10405 ++i;
10408 /* Stop at line ends. */
10409 if (ITERATOR_AT_END_OF_LINE_P (it))
10410 break;
10412 set_iterator_to_next (it, 1);
10415 out:;
10417 row->displays_text_p = row->used[TEXT_AREA] != 0;
10419 /* Use default face for the border below the tool bar.
10421 FIXME: When auto-resize-tool-bars is grow-only, there is
10422 no additional border below the possibly empty tool-bar lines.
10423 So to make the extra empty lines look "normal", we have to
10424 use the tool-bar face for the border too. */
10425 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10426 it->face_id = DEFAULT_FACE_ID;
10428 extend_face_to_end_of_line (it);
10429 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10430 last->right_box_line_p = 1;
10431 if (last == row->glyphs[TEXT_AREA])
10432 last->left_box_line_p = 1;
10434 /* Make line the desired height and center it vertically. */
10435 if ((height -= it->max_ascent + it->max_descent) > 0)
10437 /* Don't add more than one line height. */
10438 height %= FRAME_LINE_HEIGHT (it->f);
10439 it->max_ascent += height / 2;
10440 it->max_descent += (height + 1) / 2;
10443 compute_line_metrics (it);
10445 /* If line is empty, make it occupy the rest of the tool-bar. */
10446 if (!row->displays_text_p)
10448 row->height = row->phys_height = it->last_visible_y - row->y;
10449 row->visible_height = row->height;
10450 row->ascent = row->phys_ascent = 0;
10451 row->extra_line_spacing = 0;
10454 row->full_width_p = 1;
10455 row->continued_p = 0;
10456 row->truncated_on_left_p = 0;
10457 row->truncated_on_right_p = 0;
10459 it->current_x = it->hpos = 0;
10460 it->current_y += row->height;
10461 ++it->vpos;
10462 ++it->glyph_row;
10466 /* Max tool-bar height. */
10468 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10469 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10471 /* Value is the number of screen lines needed to make all tool-bar
10472 items of frame F visible. The number of actual rows needed is
10473 returned in *N_ROWS if non-NULL. */
10475 static int
10476 tool_bar_lines_needed (struct frame *f, int *n_rows)
10478 struct window *w = XWINDOW (f->tool_bar_window);
10479 struct it it;
10480 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10481 the desired matrix, so use (unused) mode-line row as temporary row to
10482 avoid destroying the first tool-bar row. */
10483 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10485 /* Initialize an iterator for iteration over
10486 F->desired_tool_bar_string in the tool-bar window of frame F. */
10487 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10488 it.first_visible_x = 0;
10489 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10490 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10492 while (!ITERATOR_AT_END_P (&it))
10494 clear_glyph_row (temp_row);
10495 it.glyph_row = temp_row;
10496 display_tool_bar_line (&it, -1);
10498 clear_glyph_row (temp_row);
10500 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10501 if (n_rows)
10502 *n_rows = it.vpos > 0 ? it.vpos : -1;
10504 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10508 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10509 0, 1, 0,
10510 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10511 (Lisp_Object frame)
10513 struct frame *f;
10514 struct window *w;
10515 int nlines = 0;
10517 if (NILP (frame))
10518 frame = selected_frame;
10519 else
10520 CHECK_FRAME (frame);
10521 f = XFRAME (frame);
10523 if (WINDOWP (f->tool_bar_window)
10524 || (w = XWINDOW (f->tool_bar_window),
10525 WINDOW_TOTAL_LINES (w) > 0))
10527 update_tool_bar (f, 1);
10528 if (f->n_tool_bar_items)
10530 build_desired_tool_bar_string (f);
10531 nlines = tool_bar_lines_needed (f, NULL);
10535 return make_number (nlines);
10539 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10540 height should be changed. */
10542 static int
10543 redisplay_tool_bar (struct frame *f)
10545 struct window *w;
10546 struct it it;
10547 struct glyph_row *row;
10549 #if defined (USE_GTK) || defined (HAVE_NS)
10550 if (FRAME_EXTERNAL_TOOL_BAR (f))
10551 update_frame_tool_bar (f);
10552 return 0;
10553 #endif
10555 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10556 do anything. This means you must start with tool-bar-lines
10557 non-zero to get the auto-sizing effect. Or in other words, you
10558 can turn off tool-bars by specifying tool-bar-lines zero. */
10559 if (!WINDOWP (f->tool_bar_window)
10560 || (w = XWINDOW (f->tool_bar_window),
10561 WINDOW_TOTAL_LINES (w) == 0))
10562 return 0;
10564 /* Set up an iterator for the tool-bar window. */
10565 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10566 it.first_visible_x = 0;
10567 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10568 row = it.glyph_row;
10570 /* Build a string that represents the contents of the tool-bar. */
10571 build_desired_tool_bar_string (f);
10572 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10574 if (f->n_tool_bar_rows == 0)
10576 int nlines;
10578 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10579 nlines != WINDOW_TOTAL_LINES (w)))
10581 Lisp_Object frame;
10582 int old_height = WINDOW_TOTAL_LINES (w);
10584 XSETFRAME (frame, f);
10585 Fmodify_frame_parameters (frame,
10586 Fcons (Fcons (Qtool_bar_lines,
10587 make_number (nlines)),
10588 Qnil));
10589 if (WINDOW_TOTAL_LINES (w) != old_height)
10591 clear_glyph_matrix (w->desired_matrix);
10592 fonts_changed_p = 1;
10593 return 1;
10598 /* Display as many lines as needed to display all tool-bar items. */
10600 if (f->n_tool_bar_rows > 0)
10602 int border, rows, height, extra;
10604 if (INTEGERP (Vtool_bar_border))
10605 border = XINT (Vtool_bar_border);
10606 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10607 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10608 else if (EQ (Vtool_bar_border, Qborder_width))
10609 border = f->border_width;
10610 else
10611 border = 0;
10612 if (border < 0)
10613 border = 0;
10615 rows = f->n_tool_bar_rows;
10616 height = max (1, (it.last_visible_y - border) / rows);
10617 extra = it.last_visible_y - border - height * rows;
10619 while (it.current_y < it.last_visible_y)
10621 int h = 0;
10622 if (extra > 0 && rows-- > 0)
10624 h = (extra + rows - 1) / rows;
10625 extra -= h;
10627 display_tool_bar_line (&it, height + h);
10630 else
10632 while (it.current_y < it.last_visible_y)
10633 display_tool_bar_line (&it, 0);
10636 /* It doesn't make much sense to try scrolling in the tool-bar
10637 window, so don't do it. */
10638 w->desired_matrix->no_scrolling_p = 1;
10639 w->must_be_updated_p = 1;
10641 if (!NILP (Vauto_resize_tool_bars))
10643 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10644 int change_height_p = 0;
10646 /* If we couldn't display everything, change the tool-bar's
10647 height if there is room for more. */
10648 if (IT_STRING_CHARPOS (it) < it.end_charpos
10649 && it.current_y < max_tool_bar_height)
10650 change_height_p = 1;
10652 row = it.glyph_row - 1;
10654 /* If there are blank lines at the end, except for a partially
10655 visible blank line at the end that is smaller than
10656 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10657 if (!row->displays_text_p
10658 && row->height >= FRAME_LINE_HEIGHT (f))
10659 change_height_p = 1;
10661 /* If row displays tool-bar items, but is partially visible,
10662 change the tool-bar's height. */
10663 if (row->displays_text_p
10664 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10665 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10666 change_height_p = 1;
10668 /* Resize windows as needed by changing the `tool-bar-lines'
10669 frame parameter. */
10670 if (change_height_p)
10672 Lisp_Object frame;
10673 int old_height = WINDOW_TOTAL_LINES (w);
10674 int nrows;
10675 int nlines = tool_bar_lines_needed (f, &nrows);
10677 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10678 && !f->minimize_tool_bar_window_p)
10679 ? (nlines > old_height)
10680 : (nlines != old_height));
10681 f->minimize_tool_bar_window_p = 0;
10683 if (change_height_p)
10685 XSETFRAME (frame, f);
10686 Fmodify_frame_parameters (frame,
10687 Fcons (Fcons (Qtool_bar_lines,
10688 make_number (nlines)),
10689 Qnil));
10690 if (WINDOW_TOTAL_LINES (w) != old_height)
10692 clear_glyph_matrix (w->desired_matrix);
10693 f->n_tool_bar_rows = nrows;
10694 fonts_changed_p = 1;
10695 return 1;
10701 f->minimize_tool_bar_window_p = 0;
10702 return 0;
10706 /* Get information about the tool-bar item which is displayed in GLYPH
10707 on frame F. Return in *PROP_IDX the index where tool-bar item
10708 properties start in F->tool_bar_items. Value is zero if
10709 GLYPH doesn't display a tool-bar item. */
10711 static int
10712 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
10714 Lisp_Object prop;
10715 int success_p;
10716 int charpos;
10718 /* This function can be called asynchronously, which means we must
10719 exclude any possibility that Fget_text_property signals an
10720 error. */
10721 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10722 charpos = max (0, charpos);
10724 /* Get the text property `menu-item' at pos. The value of that
10725 property is the start index of this item's properties in
10726 F->tool_bar_items. */
10727 prop = Fget_text_property (make_number (charpos),
10728 Qmenu_item, f->current_tool_bar_string);
10729 if (INTEGERP (prop))
10731 *prop_idx = XINT (prop);
10732 success_p = 1;
10734 else
10735 success_p = 0;
10737 return success_p;
10741 /* Get information about the tool-bar item at position X/Y on frame F.
10742 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10743 the current matrix of the tool-bar window of F, or NULL if not
10744 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10745 item in F->tool_bar_items. Value is
10747 -1 if X/Y is not on a tool-bar item
10748 0 if X/Y is on the same item that was highlighted before.
10749 1 otherwise. */
10751 static int
10752 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
10753 int *hpos, int *vpos, int *prop_idx)
10755 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10756 struct window *w = XWINDOW (f->tool_bar_window);
10757 int area;
10759 /* Find the glyph under X/Y. */
10760 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10761 if (*glyph == NULL)
10762 return -1;
10764 /* Get the start of this tool-bar item's properties in
10765 f->tool_bar_items. */
10766 if (!tool_bar_item_info (f, *glyph, prop_idx))
10767 return -1;
10769 /* Is mouse on the highlighted item? */
10770 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10771 && *vpos >= dpyinfo->mouse_face_beg_row
10772 && *vpos <= dpyinfo->mouse_face_end_row
10773 && (*vpos > dpyinfo->mouse_face_beg_row
10774 || *hpos >= dpyinfo->mouse_face_beg_col)
10775 && (*vpos < dpyinfo->mouse_face_end_row
10776 || *hpos < dpyinfo->mouse_face_end_col
10777 || dpyinfo->mouse_face_past_end))
10778 return 0;
10780 return 1;
10784 /* EXPORT:
10785 Handle mouse button event on the tool-bar of frame F, at
10786 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10787 0 for button release. MODIFIERS is event modifiers for button
10788 release. */
10790 void
10791 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
10792 unsigned int modifiers)
10794 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10795 struct window *w = XWINDOW (f->tool_bar_window);
10796 int hpos, vpos, prop_idx;
10797 struct glyph *glyph;
10798 Lisp_Object enabled_p;
10800 /* If not on the highlighted tool-bar item, return. */
10801 frame_to_window_pixel_xy (w, &x, &y);
10802 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10803 return;
10805 /* If item is disabled, do nothing. */
10806 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10807 if (NILP (enabled_p))
10808 return;
10810 if (down_p)
10812 /* Show item in pressed state. */
10813 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10814 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10815 last_tool_bar_item = prop_idx;
10817 else
10819 Lisp_Object key, frame;
10820 struct input_event event;
10821 EVENT_INIT (event);
10823 /* Show item in released state. */
10824 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10825 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10827 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10829 XSETFRAME (frame, f);
10830 event.kind = TOOL_BAR_EVENT;
10831 event.frame_or_window = frame;
10832 event.arg = frame;
10833 kbd_buffer_store_event (&event);
10835 event.kind = TOOL_BAR_EVENT;
10836 event.frame_or_window = frame;
10837 event.arg = key;
10838 event.modifiers = modifiers;
10839 kbd_buffer_store_event (&event);
10840 last_tool_bar_item = -1;
10845 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10846 tool-bar window-relative coordinates X/Y. Called from
10847 note_mouse_highlight. */
10849 static void
10850 note_tool_bar_highlight (struct frame *f, int x, int y)
10852 Lisp_Object window = f->tool_bar_window;
10853 struct window *w = XWINDOW (window);
10854 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10855 int hpos, vpos;
10856 struct glyph *glyph;
10857 struct glyph_row *row;
10858 int i;
10859 Lisp_Object enabled_p;
10860 int prop_idx;
10861 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10862 int mouse_down_p, rc;
10864 /* Function note_mouse_highlight is called with negative x(y
10865 values when mouse moves outside of the frame. */
10866 if (x <= 0 || y <= 0)
10868 clear_mouse_face (dpyinfo);
10869 return;
10872 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10873 if (rc < 0)
10875 /* Not on tool-bar item. */
10876 clear_mouse_face (dpyinfo);
10877 return;
10879 else if (rc == 0)
10880 /* On same tool-bar item as before. */
10881 goto set_help_echo;
10883 clear_mouse_face (dpyinfo);
10885 /* Mouse is down, but on different tool-bar item? */
10886 mouse_down_p = (dpyinfo->grabbed
10887 && f == last_mouse_frame
10888 && FRAME_LIVE_P (f));
10889 if (mouse_down_p
10890 && last_tool_bar_item != prop_idx)
10891 return;
10893 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10894 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10896 /* If tool-bar item is not enabled, don't highlight it. */
10897 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10898 if (!NILP (enabled_p))
10900 /* Compute the x-position of the glyph. In front and past the
10901 image is a space. We include this in the highlighted area. */
10902 row = MATRIX_ROW (w->current_matrix, vpos);
10903 for (i = x = 0; i < hpos; ++i)
10904 x += row->glyphs[TEXT_AREA][i].pixel_width;
10906 /* Record this as the current active region. */
10907 dpyinfo->mouse_face_beg_col = hpos;
10908 dpyinfo->mouse_face_beg_row = vpos;
10909 dpyinfo->mouse_face_beg_x = x;
10910 dpyinfo->mouse_face_beg_y = row->y;
10911 dpyinfo->mouse_face_past_end = 0;
10913 dpyinfo->mouse_face_end_col = hpos + 1;
10914 dpyinfo->mouse_face_end_row = vpos;
10915 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10916 dpyinfo->mouse_face_end_y = row->y;
10917 dpyinfo->mouse_face_window = window;
10918 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10920 /* Display it as active. */
10921 show_mouse_face (dpyinfo, draw);
10922 dpyinfo->mouse_face_image_state = draw;
10925 set_help_echo:
10927 /* Set help_echo_string to a help string to display for this tool-bar item.
10928 XTread_socket does the rest. */
10929 help_echo_object = help_echo_window = Qnil;
10930 help_echo_pos = -1;
10931 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10932 if (NILP (help_echo_string))
10933 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10936 #endif /* HAVE_WINDOW_SYSTEM */
10940 /************************************************************************
10941 Horizontal scrolling
10942 ************************************************************************/
10944 static int hscroll_window_tree (Lisp_Object);
10945 static int hscroll_windows (Lisp_Object);
10947 /* For all leaf windows in the window tree rooted at WINDOW, set their
10948 hscroll value so that PT is (i) visible in the window, and (ii) so
10949 that it is not within a certain margin at the window's left and
10950 right border. Value is non-zero if any window's hscroll has been
10951 changed. */
10953 static int
10954 hscroll_window_tree (Lisp_Object window)
10956 int hscrolled_p = 0;
10957 int hscroll_relative_p = FLOATP (Vhscroll_step);
10958 int hscroll_step_abs = 0;
10959 double hscroll_step_rel = 0;
10961 if (hscroll_relative_p)
10963 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10964 if (hscroll_step_rel < 0)
10966 hscroll_relative_p = 0;
10967 hscroll_step_abs = 0;
10970 else if (INTEGERP (Vhscroll_step))
10972 hscroll_step_abs = XINT (Vhscroll_step);
10973 if (hscroll_step_abs < 0)
10974 hscroll_step_abs = 0;
10976 else
10977 hscroll_step_abs = 0;
10979 while (WINDOWP (window))
10981 struct window *w = XWINDOW (window);
10983 if (WINDOWP (w->hchild))
10984 hscrolled_p |= hscroll_window_tree (w->hchild);
10985 else if (WINDOWP (w->vchild))
10986 hscrolled_p |= hscroll_window_tree (w->vchild);
10987 else if (w->cursor.vpos >= 0)
10989 int h_margin;
10990 int text_area_width;
10991 struct glyph_row *current_cursor_row
10992 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10993 struct glyph_row *desired_cursor_row
10994 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10995 struct glyph_row *cursor_row
10996 = (desired_cursor_row->enabled_p
10997 ? desired_cursor_row
10998 : current_cursor_row);
11000 text_area_width = window_box_width (w, TEXT_AREA);
11002 /* Scroll when cursor is inside this scroll margin. */
11003 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11005 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11006 && ((XFASTINT (w->hscroll)
11007 && w->cursor.x <= h_margin)
11008 || (cursor_row->enabled_p
11009 && cursor_row->truncated_on_right_p
11010 && (w->cursor.x >= text_area_width - h_margin))))
11012 struct it it;
11013 int hscroll;
11014 struct buffer *saved_current_buffer;
11015 int pt;
11016 int wanted_x;
11018 /* Find point in a display of infinite width. */
11019 saved_current_buffer = current_buffer;
11020 current_buffer = XBUFFER (w->buffer);
11022 if (w == XWINDOW (selected_window))
11023 pt = BUF_PT (current_buffer);
11024 else
11026 pt = marker_position (w->pointm);
11027 pt = max (BEGV, pt);
11028 pt = min (ZV, pt);
11031 /* Move iterator to pt starting at cursor_row->start in
11032 a line with infinite width. */
11033 init_to_row_start (&it, w, cursor_row);
11034 it.last_visible_x = INFINITY;
11035 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11036 current_buffer = saved_current_buffer;
11038 /* Position cursor in window. */
11039 if (!hscroll_relative_p && hscroll_step_abs == 0)
11040 hscroll = max (0, (it.current_x
11041 - (ITERATOR_AT_END_OF_LINE_P (&it)
11042 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11043 : (text_area_width / 2))))
11044 / FRAME_COLUMN_WIDTH (it.f);
11045 else if (w->cursor.x >= text_area_width - h_margin)
11047 if (hscroll_relative_p)
11048 wanted_x = text_area_width * (1 - hscroll_step_rel)
11049 - h_margin;
11050 else
11051 wanted_x = text_area_width
11052 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11053 - h_margin;
11054 hscroll
11055 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11057 else
11059 if (hscroll_relative_p)
11060 wanted_x = text_area_width * hscroll_step_rel
11061 + h_margin;
11062 else
11063 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11064 + h_margin;
11065 hscroll
11066 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11068 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11070 /* Don't call Fset_window_hscroll if value hasn't
11071 changed because it will prevent redisplay
11072 optimizations. */
11073 if (XFASTINT (w->hscroll) != hscroll)
11075 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11076 w->hscroll = make_number (hscroll);
11077 hscrolled_p = 1;
11082 window = w->next;
11085 /* Value is non-zero if hscroll of any leaf window has been changed. */
11086 return hscrolled_p;
11090 /* Set hscroll so that cursor is visible and not inside horizontal
11091 scroll margins for all windows in the tree rooted at WINDOW. See
11092 also hscroll_window_tree above. Value is non-zero if any window's
11093 hscroll has been changed. If it has, desired matrices on the frame
11094 of WINDOW are cleared. */
11096 static int
11097 hscroll_windows (Lisp_Object window)
11099 int hscrolled_p = hscroll_window_tree (window);
11100 if (hscrolled_p)
11101 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11102 return hscrolled_p;
11107 /************************************************************************
11108 Redisplay
11109 ************************************************************************/
11111 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11112 to a non-zero value. This is sometimes handy to have in a debugger
11113 session. */
11115 #if GLYPH_DEBUG
11117 /* First and last unchanged row for try_window_id. */
11119 int debug_first_unchanged_at_end_vpos;
11120 int debug_last_unchanged_at_beg_vpos;
11122 /* Delta vpos and y. */
11124 int debug_dvpos, debug_dy;
11126 /* Delta in characters and bytes for try_window_id. */
11128 int debug_delta, debug_delta_bytes;
11130 /* Values of window_end_pos and window_end_vpos at the end of
11131 try_window_id. */
11133 EMACS_INT debug_end_pos, debug_end_vpos;
11135 /* Append a string to W->desired_matrix->method. FMT is a printf
11136 format string. A1...A9 are a supplement for a variable-length
11137 argument list. If trace_redisplay_p is non-zero also printf the
11138 resulting string to stderr. */
11140 static void
11141 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11142 struct window *w;
11143 char *fmt;
11144 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11146 char buffer[512];
11147 char *method = w->desired_matrix->method;
11148 int len = strlen (method);
11149 int size = sizeof w->desired_matrix->method;
11150 int remaining = size - len - 1;
11152 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11153 if (len && remaining)
11155 method[len] = '|';
11156 --remaining, ++len;
11159 strncpy (method + len, buffer, remaining);
11161 if (trace_redisplay_p)
11162 fprintf (stderr, "%p (%s): %s\n",
11164 ((BUFFERP (w->buffer)
11165 && STRINGP (XBUFFER (w->buffer)->name))
11166 ? (char *) SDATA (XBUFFER (w->buffer)->name)
11167 : "no buffer"),
11168 buffer);
11171 #endif /* GLYPH_DEBUG */
11174 /* Value is non-zero if all changes in window W, which displays
11175 current_buffer, are in the text between START and END. START is a
11176 buffer position, END is given as a distance from Z. Used in
11177 redisplay_internal for display optimization. */
11179 static INLINE int
11180 text_outside_line_unchanged_p (struct window *w, int start, int end)
11182 int unchanged_p = 1;
11184 /* If text or overlays have changed, see where. */
11185 if (XFASTINT (w->last_modified) < MODIFF
11186 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11188 /* Gap in the line? */
11189 if (GPT < start || Z - GPT < end)
11190 unchanged_p = 0;
11192 /* Changes start in front of the line, or end after it? */
11193 if (unchanged_p
11194 && (BEG_UNCHANGED < start - 1
11195 || END_UNCHANGED < end))
11196 unchanged_p = 0;
11198 /* If selective display, can't optimize if changes start at the
11199 beginning of the line. */
11200 if (unchanged_p
11201 && INTEGERP (current_buffer->selective_display)
11202 && XINT (current_buffer->selective_display) > 0
11203 && (BEG_UNCHANGED < start || GPT <= start))
11204 unchanged_p = 0;
11206 /* If there are overlays at the start or end of the line, these
11207 may have overlay strings with newlines in them. A change at
11208 START, for instance, may actually concern the display of such
11209 overlay strings as well, and they are displayed on different
11210 lines. So, quickly rule out this case. (For the future, it
11211 might be desirable to implement something more telling than
11212 just BEG/END_UNCHANGED.) */
11213 if (unchanged_p)
11215 if (BEG + BEG_UNCHANGED == start
11216 && overlay_touches_p (start))
11217 unchanged_p = 0;
11218 if (END_UNCHANGED == end
11219 && overlay_touches_p (Z - end))
11220 unchanged_p = 0;
11223 /* Under bidi reordering, adding or deleting a character in the
11224 beginning of a paragraph, before the first strong directional
11225 character, can change the base direction of the paragraph (unless
11226 the buffer specifies a fixed paragraph direction), which will
11227 require to redisplay the whole paragraph. It might be worthwhile
11228 to find the paragraph limits and widen the range of redisplayed
11229 lines to that, but for now just give up this optimization. */
11230 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
11231 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
11232 unchanged_p = 0;
11235 return unchanged_p;
11239 /* Do a frame update, taking possible shortcuts into account. This is
11240 the main external entry point for redisplay.
11242 If the last redisplay displayed an echo area message and that message
11243 is no longer requested, we clear the echo area or bring back the
11244 mini-buffer if that is in use. */
11246 void
11247 redisplay (void)
11249 redisplay_internal (0);
11253 static Lisp_Object
11254 overlay_arrow_string_or_property (Lisp_Object var)
11256 Lisp_Object val;
11258 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11259 return val;
11261 return Voverlay_arrow_string;
11264 /* Return 1 if there are any overlay-arrows in current_buffer. */
11265 static int
11266 overlay_arrow_in_current_buffer_p (void)
11268 Lisp_Object vlist;
11270 for (vlist = Voverlay_arrow_variable_list;
11271 CONSP (vlist);
11272 vlist = XCDR (vlist))
11274 Lisp_Object var = XCAR (vlist);
11275 Lisp_Object val;
11277 if (!SYMBOLP (var))
11278 continue;
11279 val = find_symbol_value (var);
11280 if (MARKERP (val)
11281 && current_buffer == XMARKER (val)->buffer)
11282 return 1;
11284 return 0;
11288 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11289 has changed. */
11291 static int
11292 overlay_arrows_changed_p (void)
11294 Lisp_Object vlist;
11296 for (vlist = Voverlay_arrow_variable_list;
11297 CONSP (vlist);
11298 vlist = XCDR (vlist))
11300 Lisp_Object var = XCAR (vlist);
11301 Lisp_Object val, pstr;
11303 if (!SYMBOLP (var))
11304 continue;
11305 val = find_symbol_value (var);
11306 if (!MARKERP (val))
11307 continue;
11308 if (! EQ (COERCE_MARKER (val),
11309 Fget (var, Qlast_arrow_position))
11310 || ! (pstr = overlay_arrow_string_or_property (var),
11311 EQ (pstr, Fget (var, Qlast_arrow_string))))
11312 return 1;
11314 return 0;
11317 /* Mark overlay arrows to be updated on next redisplay. */
11319 static void
11320 update_overlay_arrows (int up_to_date)
11322 Lisp_Object vlist;
11324 for (vlist = Voverlay_arrow_variable_list;
11325 CONSP (vlist);
11326 vlist = XCDR (vlist))
11328 Lisp_Object var = XCAR (vlist);
11330 if (!SYMBOLP (var))
11331 continue;
11333 if (up_to_date > 0)
11335 Lisp_Object val = find_symbol_value (var);
11336 Fput (var, Qlast_arrow_position,
11337 COERCE_MARKER (val));
11338 Fput (var, Qlast_arrow_string,
11339 overlay_arrow_string_or_property (var));
11341 else if (up_to_date < 0
11342 || !NILP (Fget (var, Qlast_arrow_position)))
11344 Fput (var, Qlast_arrow_position, Qt);
11345 Fput (var, Qlast_arrow_string, Qt);
11351 /* Return overlay arrow string to display at row.
11352 Return integer (bitmap number) for arrow bitmap in left fringe.
11353 Return nil if no overlay arrow. */
11355 static Lisp_Object
11356 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11358 Lisp_Object vlist;
11360 for (vlist = Voverlay_arrow_variable_list;
11361 CONSP (vlist);
11362 vlist = XCDR (vlist))
11364 Lisp_Object var = XCAR (vlist);
11365 Lisp_Object val;
11367 if (!SYMBOLP (var))
11368 continue;
11370 val = find_symbol_value (var);
11372 if (MARKERP (val)
11373 && current_buffer == XMARKER (val)->buffer
11374 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11376 if (FRAME_WINDOW_P (it->f)
11377 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11379 #ifdef HAVE_WINDOW_SYSTEM
11380 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11382 int fringe_bitmap;
11383 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11384 return make_number (fringe_bitmap);
11386 #endif
11387 return make_number (-1); /* Use default arrow bitmap */
11389 return overlay_arrow_string_or_property (var);
11393 return Qnil;
11396 /* Return 1 if point moved out of or into a composition. Otherwise
11397 return 0. PREV_BUF and PREV_PT are the last point buffer and
11398 position. BUF and PT are the current point buffer and position. */
11401 check_point_in_composition (struct buffer *prev_buf, int prev_pt,
11402 struct buffer *buf, int pt)
11404 EMACS_INT start, end;
11405 Lisp_Object prop;
11406 Lisp_Object buffer;
11408 XSETBUFFER (buffer, buf);
11409 /* Check a composition at the last point if point moved within the
11410 same buffer. */
11411 if (prev_buf == buf)
11413 if (prev_pt == pt)
11414 /* Point didn't move. */
11415 return 0;
11417 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11418 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11419 && COMPOSITION_VALID_P (start, end, prop)
11420 && start < prev_pt && end > prev_pt)
11421 /* The last point was within the composition. Return 1 iff
11422 point moved out of the composition. */
11423 return (pt <= start || pt >= end);
11426 /* Check a composition at the current point. */
11427 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11428 && find_composition (pt, -1, &start, &end, &prop, buffer)
11429 && COMPOSITION_VALID_P (start, end, prop)
11430 && start < pt && end > pt);
11434 /* Reconsider the setting of B->clip_changed which is displayed
11435 in window W. */
11437 static INLINE void
11438 reconsider_clip_changes (struct window *w, struct buffer *b)
11440 if (b->clip_changed
11441 && !NILP (w->window_end_valid)
11442 && w->current_matrix->buffer == b
11443 && w->current_matrix->zv == BUF_ZV (b)
11444 && w->current_matrix->begv == BUF_BEGV (b))
11445 b->clip_changed = 0;
11447 /* If display wasn't paused, and W is not a tool bar window, see if
11448 point has been moved into or out of a composition. In that case,
11449 we set b->clip_changed to 1 to force updating the screen. If
11450 b->clip_changed has already been set to 1, we can skip this
11451 check. */
11452 if (!b->clip_changed
11453 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11455 int pt;
11457 if (w == XWINDOW (selected_window))
11458 pt = BUF_PT (current_buffer);
11459 else
11460 pt = marker_position (w->pointm);
11462 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11463 || pt != XINT (w->last_point))
11464 && check_point_in_composition (w->current_matrix->buffer,
11465 XINT (w->last_point),
11466 XBUFFER (w->buffer), pt))
11467 b->clip_changed = 1;
11472 /* Select FRAME to forward the values of frame-local variables into C
11473 variables so that the redisplay routines can access those values
11474 directly. */
11476 static void
11477 select_frame_for_redisplay (Lisp_Object frame)
11479 Lisp_Object tail, tem;
11480 Lisp_Object old = selected_frame;
11481 struct Lisp_Symbol *sym;
11483 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11485 selected_frame = frame;
11487 do {
11488 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11489 if (CONSP (XCAR (tail))
11490 && (tem = XCAR (XCAR (tail)),
11491 SYMBOLP (tem))
11492 && (sym = indirect_variable (XSYMBOL (tem)),
11493 sym->redirect == SYMBOL_LOCALIZED)
11494 && sym->val.blv->frame_local)
11495 /* Use find_symbol_value rather than Fsymbol_value
11496 to avoid an error if it is void. */
11497 find_symbol_value (tem);
11498 } while (!EQ (frame, old) && (frame = old, 1));
11502 #define STOP_POLLING \
11503 do { if (! polling_stopped_here) stop_polling (); \
11504 polling_stopped_here = 1; } while (0)
11506 #define RESUME_POLLING \
11507 do { if (polling_stopped_here) start_polling (); \
11508 polling_stopped_here = 0; } while (0)
11511 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11512 response to any user action; therefore, we should preserve the echo
11513 area. (Actually, our caller does that job.) Perhaps in the future
11514 avoid recentering windows if it is not necessary; currently that
11515 causes some problems. */
11517 static void
11518 redisplay_internal (int preserve_echo_area)
11520 struct window *w = XWINDOW (selected_window);
11521 struct frame *f;
11522 int pause;
11523 int must_finish = 0;
11524 struct text_pos tlbufpos, tlendpos;
11525 int number_of_visible_frames;
11526 int count, count1;
11527 struct frame *sf;
11528 int polling_stopped_here = 0;
11529 Lisp_Object old_frame = selected_frame;
11531 /* Non-zero means redisplay has to consider all windows on all
11532 frames. Zero means, only selected_window is considered. */
11533 int consider_all_windows_p;
11535 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11537 /* No redisplay if running in batch mode or frame is not yet fully
11538 initialized, or redisplay is explicitly turned off by setting
11539 Vinhibit_redisplay. */
11540 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11541 || !NILP (Vinhibit_redisplay))
11542 return;
11544 /* Don't examine these until after testing Vinhibit_redisplay.
11545 When Emacs is shutting down, perhaps because its connection to
11546 X has dropped, we should not look at them at all. */
11547 f = XFRAME (w->frame);
11548 sf = SELECTED_FRAME ();
11550 if (!f->glyphs_initialized_p)
11551 return;
11553 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11554 if (popup_activated ())
11555 return;
11556 #endif
11558 /* I don't think this happens but let's be paranoid. */
11559 if (redisplaying_p)
11560 return;
11562 /* Record a function that resets redisplaying_p to its old value
11563 when we leave this function. */
11564 count = SPECPDL_INDEX ();
11565 record_unwind_protect (unwind_redisplay,
11566 Fcons (make_number (redisplaying_p), selected_frame));
11567 ++redisplaying_p;
11568 specbind (Qinhibit_free_realized_faces, Qnil);
11571 Lisp_Object tail, frame;
11573 FOR_EACH_FRAME (tail, frame)
11575 struct frame *f = XFRAME (frame);
11576 f->already_hscrolled_p = 0;
11580 retry:
11581 if (!EQ (old_frame, selected_frame)
11582 && FRAME_LIVE_P (XFRAME (old_frame)))
11583 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11584 selected_frame and selected_window to be temporarily out-of-sync so
11585 when we come back here via `goto retry', we need to resync because we
11586 may need to run Elisp code (via prepare_menu_bars). */
11587 select_frame_for_redisplay (old_frame);
11589 pause = 0;
11590 reconsider_clip_changes (w, current_buffer);
11591 last_escape_glyph_frame = NULL;
11592 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11594 /* If new fonts have been loaded that make a glyph matrix adjustment
11595 necessary, do it. */
11596 if (fonts_changed_p)
11598 adjust_glyphs (NULL);
11599 ++windows_or_buffers_changed;
11600 fonts_changed_p = 0;
11603 /* If face_change_count is non-zero, init_iterator will free all
11604 realized faces, which includes the faces referenced from current
11605 matrices. So, we can't reuse current matrices in this case. */
11606 if (face_change_count)
11607 ++windows_or_buffers_changed;
11609 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11610 && FRAME_TTY (sf)->previous_frame != sf)
11612 /* Since frames on a single ASCII terminal share the same
11613 display area, displaying a different frame means redisplay
11614 the whole thing. */
11615 windows_or_buffers_changed++;
11616 SET_FRAME_GARBAGED (sf);
11617 #ifndef DOS_NT
11618 set_tty_color_mode (FRAME_TTY (sf), sf);
11619 #endif
11620 FRAME_TTY (sf)->previous_frame = sf;
11623 /* Set the visible flags for all frames. Do this before checking
11624 for resized or garbaged frames; they want to know if their frames
11625 are visible. See the comment in frame.h for
11626 FRAME_SAMPLE_VISIBILITY. */
11628 Lisp_Object tail, frame;
11630 number_of_visible_frames = 0;
11632 FOR_EACH_FRAME (tail, frame)
11634 struct frame *f = XFRAME (frame);
11636 FRAME_SAMPLE_VISIBILITY (f);
11637 if (FRAME_VISIBLE_P (f))
11638 ++number_of_visible_frames;
11639 clear_desired_matrices (f);
11643 /* Notice any pending interrupt request to change frame size. */
11644 do_pending_window_change (1);
11646 /* Clear frames marked as garbaged. */
11647 if (frame_garbaged)
11648 clear_garbaged_frames ();
11650 /* Build menubar and tool-bar items. */
11651 if (NILP (Vmemory_full))
11652 prepare_menu_bars ();
11654 if (windows_or_buffers_changed)
11655 update_mode_lines++;
11657 /* Detect case that we need to write or remove a star in the mode line. */
11658 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11660 w->update_mode_line = Qt;
11661 if (buffer_shared > 1)
11662 update_mode_lines++;
11665 /* Avoid invocation of point motion hooks by `current_column' below. */
11666 count1 = SPECPDL_INDEX ();
11667 specbind (Qinhibit_point_motion_hooks, Qt);
11669 /* If %c is in the mode line, update it if needed. */
11670 if (!NILP (w->column_number_displayed)
11671 /* This alternative quickly identifies a common case
11672 where no change is needed. */
11673 && !(PT == XFASTINT (w->last_point)
11674 && XFASTINT (w->last_modified) >= MODIFF
11675 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11676 && (XFASTINT (w->column_number_displayed)
11677 != (int) current_column ())) /* iftc */
11678 w->update_mode_line = Qt;
11680 unbind_to (count1, Qnil);
11682 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11684 /* The variable buffer_shared is set in redisplay_window and
11685 indicates that we redisplay a buffer in different windows. See
11686 there. */
11687 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11688 || cursor_type_changed);
11690 /* If specs for an arrow have changed, do thorough redisplay
11691 to ensure we remove any arrow that should no longer exist. */
11692 if (overlay_arrows_changed_p ())
11693 consider_all_windows_p = windows_or_buffers_changed = 1;
11695 /* Normally the message* functions will have already displayed and
11696 updated the echo area, but the frame may have been trashed, or
11697 the update may have been preempted, so display the echo area
11698 again here. Checking message_cleared_p captures the case that
11699 the echo area should be cleared. */
11700 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11701 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11702 || (message_cleared_p
11703 && minibuf_level == 0
11704 /* If the mini-window is currently selected, this means the
11705 echo-area doesn't show through. */
11706 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11708 int window_height_changed_p = echo_area_display (0);
11709 must_finish = 1;
11711 /* If we don't display the current message, don't clear the
11712 message_cleared_p flag, because, if we did, we wouldn't clear
11713 the echo area in the next redisplay which doesn't preserve
11714 the echo area. */
11715 if (!display_last_displayed_message_p)
11716 message_cleared_p = 0;
11718 if (fonts_changed_p)
11719 goto retry;
11720 else if (window_height_changed_p)
11722 consider_all_windows_p = 1;
11723 ++update_mode_lines;
11724 ++windows_or_buffers_changed;
11726 /* If window configuration was changed, frames may have been
11727 marked garbaged. Clear them or we will experience
11728 surprises wrt scrolling. */
11729 if (frame_garbaged)
11730 clear_garbaged_frames ();
11733 else if (EQ (selected_window, minibuf_window)
11734 && (current_buffer->clip_changed
11735 || XFASTINT (w->last_modified) < MODIFF
11736 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11737 && resize_mini_window (w, 0))
11739 /* Resized active mini-window to fit the size of what it is
11740 showing if its contents might have changed. */
11741 must_finish = 1;
11742 /* FIXME: this causes all frames to be updated, which seems unnecessary
11743 since only the current frame needs to be considered. This function needs
11744 to be rewritten with two variables, consider_all_windows and
11745 consider_all_frames. */
11746 consider_all_windows_p = 1;
11747 ++windows_or_buffers_changed;
11748 ++update_mode_lines;
11750 /* If window configuration was changed, frames may have been
11751 marked garbaged. Clear them or we will experience
11752 surprises wrt scrolling. */
11753 if (frame_garbaged)
11754 clear_garbaged_frames ();
11758 /* If showing the region, and mark has changed, we must redisplay
11759 the whole window. The assignment to this_line_start_pos prevents
11760 the optimization directly below this if-statement. */
11761 if (((!NILP (Vtransient_mark_mode)
11762 && !NILP (XBUFFER (w->buffer)->mark_active))
11763 != !NILP (w->region_showing))
11764 || (!NILP (w->region_showing)
11765 && !EQ (w->region_showing,
11766 Fmarker_position (XBUFFER (w->buffer)->mark))))
11767 CHARPOS (this_line_start_pos) = 0;
11769 /* Optimize the case that only the line containing the cursor in the
11770 selected window has changed. Variables starting with this_ are
11771 set in display_line and record information about the line
11772 containing the cursor. */
11773 tlbufpos = this_line_start_pos;
11774 tlendpos = this_line_end_pos;
11775 if (!consider_all_windows_p
11776 && CHARPOS (tlbufpos) > 0
11777 && NILP (w->update_mode_line)
11778 && !current_buffer->clip_changed
11779 && !current_buffer->prevent_redisplay_optimizations_p
11780 && FRAME_VISIBLE_P (XFRAME (w->frame))
11781 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11782 /* Make sure recorded data applies to current buffer, etc. */
11783 && this_line_buffer == current_buffer
11784 && current_buffer == XBUFFER (w->buffer)
11785 && NILP (w->force_start)
11786 && NILP (w->optional_new_start)
11787 /* Point must be on the line that we have info recorded about. */
11788 && PT >= CHARPOS (tlbufpos)
11789 && PT <= Z - CHARPOS (tlendpos)
11790 /* All text outside that line, including its final newline,
11791 must be unchanged. */
11792 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11793 CHARPOS (tlendpos)))
11795 if (CHARPOS (tlbufpos) > BEGV
11796 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11797 && (CHARPOS (tlbufpos) == ZV
11798 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11799 /* Former continuation line has disappeared by becoming empty. */
11800 goto cancel;
11801 else if (XFASTINT (w->last_modified) < MODIFF
11802 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11803 || MINI_WINDOW_P (w))
11805 /* We have to handle the case of continuation around a
11806 wide-column character (see the comment in indent.c around
11807 line 1340).
11809 For instance, in the following case:
11811 -------- Insert --------
11812 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11813 J_I_ ==> J_I_ `^^' are cursors.
11814 ^^ ^^
11815 -------- --------
11817 As we have to redraw the line above, we cannot use this
11818 optimization. */
11820 struct it it;
11821 int line_height_before = this_line_pixel_height;
11823 /* Note that start_display will handle the case that the
11824 line starting at tlbufpos is a continuation line. */
11825 start_display (&it, w, tlbufpos);
11827 /* Implementation note: It this still necessary? */
11828 if (it.current_x != this_line_start_x)
11829 goto cancel;
11831 TRACE ((stderr, "trying display optimization 1\n"));
11832 w->cursor.vpos = -1;
11833 overlay_arrow_seen = 0;
11834 it.vpos = this_line_vpos;
11835 it.current_y = this_line_y;
11836 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11837 display_line (&it);
11839 /* If line contains point, is not continued,
11840 and ends at same distance from eob as before, we win. */
11841 if (w->cursor.vpos >= 0
11842 /* Line is not continued, otherwise this_line_start_pos
11843 would have been set to 0 in display_line. */
11844 && CHARPOS (this_line_start_pos)
11845 /* Line ends as before. */
11846 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11847 /* Line has same height as before. Otherwise other lines
11848 would have to be shifted up or down. */
11849 && this_line_pixel_height == line_height_before)
11851 /* If this is not the window's last line, we must adjust
11852 the charstarts of the lines below. */
11853 if (it.current_y < it.last_visible_y)
11855 struct glyph_row *row
11856 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11857 int delta, delta_bytes;
11859 /* We used to distinguish between two cases here,
11860 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11861 when the line ends in a newline or the end of the
11862 buffer's accessible portion. But both cases did
11863 the same, so they were collapsed. */
11864 delta = (Z
11865 - CHARPOS (tlendpos)
11866 - MATRIX_ROW_START_CHARPOS (row));
11867 delta_bytes = (Z_BYTE
11868 - BYTEPOS (tlendpos)
11869 - MATRIX_ROW_START_BYTEPOS (row));
11871 increment_matrix_positions (w->current_matrix,
11872 this_line_vpos + 1,
11873 w->current_matrix->nrows,
11874 delta, delta_bytes);
11877 /* If this row displays text now but previously didn't,
11878 or vice versa, w->window_end_vpos may have to be
11879 adjusted. */
11880 if ((it.glyph_row - 1)->displays_text_p)
11882 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11883 XSETINT (w->window_end_vpos, this_line_vpos);
11885 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11886 && this_line_vpos > 0)
11887 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11888 w->window_end_valid = Qnil;
11890 /* Update hint: No need to try to scroll in update_window. */
11891 w->desired_matrix->no_scrolling_p = 1;
11893 #if GLYPH_DEBUG
11894 *w->desired_matrix->method = 0;
11895 debug_method_add (w, "optimization 1");
11896 #endif
11897 #ifdef HAVE_WINDOW_SYSTEM
11898 update_window_fringes (w, 0);
11899 #endif
11900 goto update;
11902 else
11903 goto cancel;
11905 else if (/* Cursor position hasn't changed. */
11906 PT == XFASTINT (w->last_point)
11907 /* Make sure the cursor was last displayed
11908 in this window. Otherwise we have to reposition it. */
11909 && 0 <= w->cursor.vpos
11910 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11912 if (!must_finish)
11914 do_pending_window_change (1);
11916 /* We used to always goto end_of_redisplay here, but this
11917 isn't enough if we have a blinking cursor. */
11918 if (w->cursor_off_p == w->last_cursor_off_p)
11919 goto end_of_redisplay;
11921 goto update;
11923 /* If highlighting the region, or if the cursor is in the echo area,
11924 then we can't just move the cursor. */
11925 else if (! (!NILP (Vtransient_mark_mode)
11926 && !NILP (current_buffer->mark_active))
11927 && (EQ (selected_window, current_buffer->last_selected_window)
11928 || highlight_nonselected_windows)
11929 && NILP (w->region_showing)
11930 && NILP (Vshow_trailing_whitespace)
11931 && !cursor_in_echo_area)
11933 struct it it;
11934 struct glyph_row *row;
11936 /* Skip from tlbufpos to PT and see where it is. Note that
11937 PT may be in invisible text. If so, we will end at the
11938 next visible position. */
11939 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11940 NULL, DEFAULT_FACE_ID);
11941 it.current_x = this_line_start_x;
11942 it.current_y = this_line_y;
11943 it.vpos = this_line_vpos;
11945 /* The call to move_it_to stops in front of PT, but
11946 moves over before-strings. */
11947 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11949 if (it.vpos == this_line_vpos
11950 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11951 row->enabled_p))
11953 xassert (this_line_vpos == it.vpos);
11954 xassert (this_line_y == it.current_y);
11955 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11956 #if GLYPH_DEBUG
11957 *w->desired_matrix->method = 0;
11958 debug_method_add (w, "optimization 3");
11959 #endif
11960 goto update;
11962 else
11963 goto cancel;
11966 cancel:
11967 /* Text changed drastically or point moved off of line. */
11968 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11971 CHARPOS (this_line_start_pos) = 0;
11972 consider_all_windows_p |= buffer_shared > 1;
11973 ++clear_face_cache_count;
11974 #ifdef HAVE_WINDOW_SYSTEM
11975 ++clear_image_cache_count;
11976 #endif
11978 /* Build desired matrices, and update the display. If
11979 consider_all_windows_p is non-zero, do it for all windows on all
11980 frames. Otherwise do it for selected_window, only. */
11982 if (consider_all_windows_p)
11984 Lisp_Object tail, frame;
11986 FOR_EACH_FRAME (tail, frame)
11987 XFRAME (frame)->updated_p = 0;
11989 /* Recompute # windows showing selected buffer. This will be
11990 incremented each time such a window is displayed. */
11991 buffer_shared = 0;
11993 FOR_EACH_FRAME (tail, frame)
11995 struct frame *f = XFRAME (frame);
11997 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11999 if (! EQ (frame, selected_frame))
12000 /* Select the frame, for the sake of frame-local
12001 variables. */
12002 select_frame_for_redisplay (frame);
12004 /* Mark all the scroll bars to be removed; we'll redeem
12005 the ones we want when we redisplay their windows. */
12006 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12007 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12009 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12010 redisplay_windows (FRAME_ROOT_WINDOW (f));
12012 /* The X error handler may have deleted that frame. */
12013 if (!FRAME_LIVE_P (f))
12014 continue;
12016 /* Any scroll bars which redisplay_windows should have
12017 nuked should now go away. */
12018 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12019 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12021 /* If fonts changed, display again. */
12022 /* ??? rms: I suspect it is a mistake to jump all the way
12023 back to retry here. It should just retry this frame. */
12024 if (fonts_changed_p)
12025 goto retry;
12027 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12029 /* See if we have to hscroll. */
12030 if (!f->already_hscrolled_p)
12032 f->already_hscrolled_p = 1;
12033 if (hscroll_windows (f->root_window))
12034 goto retry;
12037 /* Prevent various kinds of signals during display
12038 update. stdio is not robust about handling
12039 signals, which can cause an apparent I/O
12040 error. */
12041 if (interrupt_input)
12042 unrequest_sigio ();
12043 STOP_POLLING;
12045 /* Update the display. */
12046 set_window_update_flags (XWINDOW (f->root_window), 1);
12047 pause |= update_frame (f, 0, 0);
12048 f->updated_p = 1;
12053 if (!EQ (old_frame, selected_frame)
12054 && FRAME_LIVE_P (XFRAME (old_frame)))
12055 /* We played a bit fast-and-loose above and allowed selected_frame
12056 and selected_window to be temporarily out-of-sync but let's make
12057 sure this stays contained. */
12058 select_frame_for_redisplay (old_frame);
12059 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12061 if (!pause)
12063 /* Do the mark_window_display_accurate after all windows have
12064 been redisplayed because this call resets flags in buffers
12065 which are needed for proper redisplay. */
12066 FOR_EACH_FRAME (tail, frame)
12068 struct frame *f = XFRAME (frame);
12069 if (f->updated_p)
12071 mark_window_display_accurate (f->root_window, 1);
12072 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12073 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12078 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12080 Lisp_Object mini_window;
12081 struct frame *mini_frame;
12083 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12084 /* Use list_of_error, not Qerror, so that
12085 we catch only errors and don't run the debugger. */
12086 internal_condition_case_1 (redisplay_window_1, selected_window,
12087 list_of_error,
12088 redisplay_window_error);
12090 /* Compare desired and current matrices, perform output. */
12092 update:
12093 /* If fonts changed, display again. */
12094 if (fonts_changed_p)
12095 goto retry;
12097 /* Prevent various kinds of signals during display update.
12098 stdio is not robust about handling signals,
12099 which can cause an apparent I/O error. */
12100 if (interrupt_input)
12101 unrequest_sigio ();
12102 STOP_POLLING;
12104 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12106 if (hscroll_windows (selected_window))
12107 goto retry;
12109 XWINDOW (selected_window)->must_be_updated_p = 1;
12110 pause = update_frame (sf, 0, 0);
12113 /* We may have called echo_area_display at the top of this
12114 function. If the echo area is on another frame, that may
12115 have put text on a frame other than the selected one, so the
12116 above call to update_frame would not have caught it. Catch
12117 it here. */
12118 mini_window = FRAME_MINIBUF_WINDOW (sf);
12119 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12121 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12123 XWINDOW (mini_window)->must_be_updated_p = 1;
12124 pause |= update_frame (mini_frame, 0, 0);
12125 if (!pause && hscroll_windows (mini_window))
12126 goto retry;
12130 /* If display was paused because of pending input, make sure we do a
12131 thorough update the next time. */
12132 if (pause)
12134 /* Prevent the optimization at the beginning of
12135 redisplay_internal that tries a single-line update of the
12136 line containing the cursor in the selected window. */
12137 CHARPOS (this_line_start_pos) = 0;
12139 /* Let the overlay arrow be updated the next time. */
12140 update_overlay_arrows (0);
12142 /* If we pause after scrolling, some rows in the current
12143 matrices of some windows are not valid. */
12144 if (!WINDOW_FULL_WIDTH_P (w)
12145 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12146 update_mode_lines = 1;
12148 else
12150 if (!consider_all_windows_p)
12152 /* This has already been done above if
12153 consider_all_windows_p is set. */
12154 mark_window_display_accurate_1 (w, 1);
12156 /* Say overlay arrows are up to date. */
12157 update_overlay_arrows (1);
12159 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12160 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12163 update_mode_lines = 0;
12164 windows_or_buffers_changed = 0;
12165 cursor_type_changed = 0;
12168 /* Start SIGIO interrupts coming again. Having them off during the
12169 code above makes it less likely one will discard output, but not
12170 impossible, since there might be stuff in the system buffer here.
12171 But it is much hairier to try to do anything about that. */
12172 if (interrupt_input)
12173 request_sigio ();
12174 RESUME_POLLING;
12176 /* If a frame has become visible which was not before, redisplay
12177 again, so that we display it. Expose events for such a frame
12178 (which it gets when becoming visible) don't call the parts of
12179 redisplay constructing glyphs, so simply exposing a frame won't
12180 display anything in this case. So, we have to display these
12181 frames here explicitly. */
12182 if (!pause)
12184 Lisp_Object tail, frame;
12185 int new_count = 0;
12187 FOR_EACH_FRAME (tail, frame)
12189 int this_is_visible = 0;
12191 if (XFRAME (frame)->visible)
12192 this_is_visible = 1;
12193 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12194 if (XFRAME (frame)->visible)
12195 this_is_visible = 1;
12197 if (this_is_visible)
12198 new_count++;
12201 if (new_count != number_of_visible_frames)
12202 windows_or_buffers_changed++;
12205 /* Change frame size now if a change is pending. */
12206 do_pending_window_change (1);
12208 /* If we just did a pending size change, or have additional
12209 visible frames, redisplay again. */
12210 if (windows_or_buffers_changed && !pause)
12211 goto retry;
12213 /* Clear the face and image caches.
12215 We used to do this only if consider_all_windows_p. But the cache
12216 needs to be cleared if a timer creates images in the current
12217 buffer (e.g. the test case in Bug#6230). */
12219 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12221 clear_face_cache (0);
12222 clear_face_cache_count = 0;
12225 #ifdef HAVE_WINDOW_SYSTEM
12226 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12228 clear_image_caches (Qnil);
12229 clear_image_cache_count = 0;
12231 #endif /* HAVE_WINDOW_SYSTEM */
12233 end_of_redisplay:
12234 unbind_to (count, Qnil);
12235 RESUME_POLLING;
12239 /* Redisplay, but leave alone any recent echo area message unless
12240 another message has been requested in its place.
12242 This is useful in situations where you need to redisplay but no
12243 user action has occurred, making it inappropriate for the message
12244 area to be cleared. See tracking_off and
12245 wait_reading_process_output for examples of these situations.
12247 FROM_WHERE is an integer saying from where this function was
12248 called. This is useful for debugging. */
12250 void
12251 redisplay_preserve_echo_area (int from_where)
12253 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12255 if (!NILP (echo_area_buffer[1]))
12257 /* We have a previously displayed message, but no current
12258 message. Redisplay the previous message. */
12259 display_last_displayed_message_p = 1;
12260 redisplay_internal (1);
12261 display_last_displayed_message_p = 0;
12263 else
12264 redisplay_internal (1);
12266 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12267 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12268 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12272 /* Function registered with record_unwind_protect in
12273 redisplay_internal. Reset redisplaying_p to the value it had
12274 before redisplay_internal was called, and clear
12275 prevent_freeing_realized_faces_p. It also selects the previously
12276 selected frame, unless it has been deleted (by an X connection
12277 failure during redisplay, for example). */
12279 static Lisp_Object
12280 unwind_redisplay (Lisp_Object val)
12282 Lisp_Object old_redisplaying_p, old_frame;
12284 old_redisplaying_p = XCAR (val);
12285 redisplaying_p = XFASTINT (old_redisplaying_p);
12286 old_frame = XCDR (val);
12287 if (! EQ (old_frame, selected_frame)
12288 && FRAME_LIVE_P (XFRAME (old_frame)))
12289 select_frame_for_redisplay (old_frame);
12290 return Qnil;
12294 /* Mark the display of window W as accurate or inaccurate. If
12295 ACCURATE_P is non-zero mark display of W as accurate. If
12296 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12297 redisplay_internal is called. */
12299 static void
12300 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12302 if (BUFFERP (w->buffer))
12304 struct buffer *b = XBUFFER (w->buffer);
12306 w->last_modified
12307 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12308 w->last_overlay_modified
12309 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12310 w->last_had_star
12311 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12313 if (accurate_p)
12315 b->clip_changed = 0;
12316 b->prevent_redisplay_optimizations_p = 0;
12318 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12319 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12320 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12321 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12323 w->current_matrix->buffer = b;
12324 w->current_matrix->begv = BUF_BEGV (b);
12325 w->current_matrix->zv = BUF_ZV (b);
12327 w->last_cursor = w->cursor;
12328 w->last_cursor_off_p = w->cursor_off_p;
12330 if (w == XWINDOW (selected_window))
12331 w->last_point = make_number (BUF_PT (b));
12332 else
12333 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12337 if (accurate_p)
12339 w->window_end_valid = w->buffer;
12340 w->update_mode_line = Qnil;
12345 /* Mark the display of windows in the window tree rooted at WINDOW as
12346 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12347 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12348 be redisplayed the next time redisplay_internal is called. */
12350 void
12351 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12353 struct window *w;
12355 for (; !NILP (window); window = w->next)
12357 w = XWINDOW (window);
12358 mark_window_display_accurate_1 (w, accurate_p);
12360 if (!NILP (w->vchild))
12361 mark_window_display_accurate (w->vchild, accurate_p);
12362 if (!NILP (w->hchild))
12363 mark_window_display_accurate (w->hchild, accurate_p);
12366 if (accurate_p)
12368 update_overlay_arrows (1);
12370 else
12372 /* Force a thorough redisplay the next time by setting
12373 last_arrow_position and last_arrow_string to t, which is
12374 unequal to any useful value of Voverlay_arrow_... */
12375 update_overlay_arrows (-1);
12380 /* Return value in display table DP (Lisp_Char_Table *) for character
12381 C. Since a display table doesn't have any parent, we don't have to
12382 follow parent. Do not call this function directly but use the
12383 macro DISP_CHAR_VECTOR. */
12385 Lisp_Object
12386 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12388 Lisp_Object val;
12390 if (ASCII_CHAR_P (c))
12392 val = dp->ascii;
12393 if (SUB_CHAR_TABLE_P (val))
12394 val = XSUB_CHAR_TABLE (val)->contents[c];
12396 else
12398 Lisp_Object table;
12400 XSETCHAR_TABLE (table, dp);
12401 val = char_table_ref (table, c);
12403 if (NILP (val))
12404 val = dp->defalt;
12405 return val;
12410 /***********************************************************************
12411 Window Redisplay
12412 ***********************************************************************/
12414 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12416 static void
12417 redisplay_windows (Lisp_Object window)
12419 while (!NILP (window))
12421 struct window *w = XWINDOW (window);
12423 if (!NILP (w->hchild))
12424 redisplay_windows (w->hchild);
12425 else if (!NILP (w->vchild))
12426 redisplay_windows (w->vchild);
12427 else if (!NILP (w->buffer))
12429 displayed_buffer = XBUFFER (w->buffer);
12430 /* Use list_of_error, not Qerror, so that
12431 we catch only errors and don't run the debugger. */
12432 internal_condition_case_1 (redisplay_window_0, window,
12433 list_of_error,
12434 redisplay_window_error);
12437 window = w->next;
12441 static Lisp_Object
12442 redisplay_window_error (Lisp_Object ignore)
12444 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12445 return Qnil;
12448 static Lisp_Object
12449 redisplay_window_0 (Lisp_Object window)
12451 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12452 redisplay_window (window, 0);
12453 return Qnil;
12456 static Lisp_Object
12457 redisplay_window_1 (Lisp_Object window)
12459 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12460 redisplay_window (window, 1);
12461 return Qnil;
12465 /* Increment GLYPH until it reaches END or CONDITION fails while
12466 adding (GLYPH)->pixel_width to X. */
12468 #define SKIP_GLYPHS(glyph, end, x, condition) \
12469 do \
12471 (x) += (glyph)->pixel_width; \
12472 ++(glyph); \
12474 while ((glyph) < (end) && (condition))
12477 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12478 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12479 which positions recorded in ROW differ from current buffer
12480 positions.
12482 Return 0 if cursor is not on this row, 1 otherwise. */
12485 set_cursor_from_row (struct window *w, struct glyph_row *row,
12486 struct glyph_matrix *matrix, int delta, int delta_bytes,
12487 int dy, int dvpos)
12489 struct glyph *glyph = row->glyphs[TEXT_AREA];
12490 struct glyph *end = glyph + row->used[TEXT_AREA];
12491 struct glyph *cursor = NULL;
12492 /* The last known character position in row. */
12493 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12494 int x = row->x;
12495 EMACS_INT pt_old = PT - delta;
12496 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
12497 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
12498 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
12499 /* A glyph beyond the edge of TEXT_AREA which we should never
12500 touch. */
12501 struct glyph *glyphs_end = end;
12502 /* Non-zero means we've found a match for cursor position, but that
12503 glyph has the avoid_cursor_p flag set. */
12504 int match_with_avoid_cursor = 0;
12505 /* Non-zero means we've seen at least one glyph that came from a
12506 display string. */
12507 int string_seen = 0;
12508 /* Largest buffer position seen so far during scan of glyph row. */
12509 EMACS_INT bpos_max = last_pos;
12510 /* Last buffer position covered by an overlay string with an integer
12511 `cursor' property. */
12512 EMACS_INT bpos_covered = 0;
12514 /* Skip over glyphs not having an object at the start and the end of
12515 the row. These are special glyphs like truncation marks on
12516 terminal frames. */
12517 if (row->displays_text_p)
12519 if (!row->reversed_p)
12521 while (glyph < end
12522 && INTEGERP (glyph->object)
12523 && glyph->charpos < 0)
12525 x += glyph->pixel_width;
12526 ++glyph;
12528 while (end > glyph
12529 && INTEGERP ((end - 1)->object)
12530 /* CHARPOS is zero for blanks and stretch glyphs
12531 inserted by extend_face_to_end_of_line. */
12532 && (end - 1)->charpos <= 0)
12533 --end;
12534 glyph_before = glyph - 1;
12535 glyph_after = end;
12537 else
12539 struct glyph *g;
12541 /* If the glyph row is reversed, we need to process it from back
12542 to front, so swap the edge pointers. */
12543 glyphs_end = end = glyph - 1;
12544 glyph += row->used[TEXT_AREA] - 1;
12546 while (glyph > end + 1
12547 && INTEGERP (glyph->object)
12548 && glyph->charpos < 0)
12550 --glyph;
12551 x -= glyph->pixel_width;
12553 if (INTEGERP (glyph->object) && glyph->charpos < 0)
12554 --glyph;
12555 /* By default, in reversed rows we put the cursor on the
12556 rightmost (first in the reading order) glyph. */
12557 for (g = end + 1; g < glyph; g++)
12558 x += g->pixel_width;
12559 while (end < glyph
12560 && INTEGERP ((end + 1)->object)
12561 && (end + 1)->charpos <= 0)
12562 ++end;
12563 glyph_before = glyph + 1;
12564 glyph_after = end;
12567 else if (row->reversed_p)
12569 /* In R2L rows that don't display text, put the cursor on the
12570 rightmost glyph. Case in point: an empty last line that is
12571 part of an R2L paragraph. */
12572 cursor = end - 1;
12573 /* Avoid placing the cursor on the last glyph of the row, where
12574 on terminal frames we hold the vertical border between
12575 adjacent windows. */
12576 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
12577 && !WINDOW_RIGHTMOST_P (w)
12578 && cursor == row->glyphs[LAST_AREA] - 1)
12579 cursor--;
12580 x = -1; /* will be computed below, at label compute_x */
12583 /* Step 1: Try to find the glyph whose character position
12584 corresponds to point. If that's not possible, find 2 glyphs
12585 whose character positions are the closest to point, one before
12586 point, the other after it. */
12587 if (!row->reversed_p)
12588 while (/* not marched to end of glyph row */
12589 glyph < end
12590 /* glyph was not inserted by redisplay for internal purposes */
12591 && !INTEGERP (glyph->object))
12593 if (BUFFERP (glyph->object))
12595 EMACS_INT dpos = glyph->charpos - pt_old;
12597 if (glyph->charpos > bpos_max)
12598 bpos_max = glyph->charpos;
12599 if (!glyph->avoid_cursor_p)
12601 /* If we hit point, we've found the glyph on which to
12602 display the cursor. */
12603 if (dpos == 0)
12605 match_with_avoid_cursor = 0;
12606 break;
12608 /* See if we've found a better approximation to
12609 POS_BEFORE or to POS_AFTER. Note that we want the
12610 first (leftmost) glyph of all those that are the
12611 closest from below, and the last (rightmost) of all
12612 those from above. */
12613 if (0 > dpos && dpos > pos_before - pt_old)
12615 pos_before = glyph->charpos;
12616 glyph_before = glyph;
12618 else if (0 < dpos && dpos <= pos_after - pt_old)
12620 pos_after = glyph->charpos;
12621 glyph_after = glyph;
12624 else if (dpos == 0)
12625 match_with_avoid_cursor = 1;
12627 else if (STRINGP (glyph->object))
12629 Lisp_Object chprop;
12630 int glyph_pos = glyph->charpos;
12632 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12633 glyph->object);
12634 if (INTEGERP (chprop))
12636 bpos_covered = bpos_max + XINT (chprop);
12637 /* If the `cursor' property covers buffer positions up
12638 to and including point, we should display cursor on
12639 this glyph. Note that overlays and text properties
12640 with string values stop bidi reordering, so every
12641 buffer position to the left of the string is always
12642 smaller than any position to the right of the
12643 string. Therefore, if a `cursor' property on one
12644 of the string's characters has an integer value, we
12645 will break out of the loop below _before_ we get to
12646 the position match above. IOW, integer values of
12647 the `cursor' property override the "exact match for
12648 point" strategy of positioning the cursor. */
12649 /* Implementation note: bpos_max == pt_old when, e.g.,
12650 we are in an empty line, where bpos_max is set to
12651 MATRIX_ROW_START_CHARPOS, see above. */
12652 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12654 cursor = glyph;
12655 break;
12659 string_seen = 1;
12661 x += glyph->pixel_width;
12662 ++glyph;
12664 else if (glyph > end) /* row is reversed */
12665 while (!INTEGERP (glyph->object))
12667 if (BUFFERP (glyph->object))
12669 EMACS_INT dpos = glyph->charpos - pt_old;
12671 if (glyph->charpos > bpos_max)
12672 bpos_max = glyph->charpos;
12673 if (!glyph->avoid_cursor_p)
12675 if (dpos == 0)
12677 match_with_avoid_cursor = 0;
12678 break;
12680 if (0 > dpos && dpos > pos_before - pt_old)
12682 pos_before = glyph->charpos;
12683 glyph_before = glyph;
12685 else if (0 < dpos && dpos <= pos_after - pt_old)
12687 pos_after = glyph->charpos;
12688 glyph_after = glyph;
12691 else if (dpos == 0)
12692 match_with_avoid_cursor = 1;
12694 else if (STRINGP (glyph->object))
12696 Lisp_Object chprop;
12697 int glyph_pos = glyph->charpos;
12699 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
12700 glyph->object);
12701 if (INTEGERP (chprop))
12703 bpos_covered = bpos_max + XINT (chprop);
12704 /* If the `cursor' property covers buffer positions up
12705 to and including point, we should display cursor on
12706 this glyph. */
12707 if (bpos_max <= pt_old && bpos_covered >= pt_old)
12709 cursor = glyph;
12710 break;
12713 string_seen = 1;
12715 --glyph;
12716 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
12718 x--; /* can't use any pixel_width */
12719 break;
12721 x -= glyph->pixel_width;
12724 /* Step 2: If we didn't find an exact match for point, we need to
12725 look for a proper place to put the cursor among glyphs between
12726 GLYPH_BEFORE and GLYPH_AFTER. */
12727 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
12728 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
12729 && bpos_covered < pt_old)
12731 if (row->ends_in_ellipsis_p && pos_after == last_pos)
12733 EMACS_INT ellipsis_pos;
12735 /* Scan back over the ellipsis glyphs. */
12736 if (!row->reversed_p)
12738 ellipsis_pos = (glyph - 1)->charpos;
12739 while (glyph > row->glyphs[TEXT_AREA]
12740 && (glyph - 1)->charpos == ellipsis_pos)
12741 glyph--, x -= glyph->pixel_width;
12742 /* That loop always goes one position too far, including
12743 the glyph before the ellipsis. So scan forward over
12744 that one. */
12745 x += glyph->pixel_width;
12746 glyph++;
12748 else /* row is reversed */
12750 ellipsis_pos = (glyph + 1)->charpos;
12751 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
12752 && (glyph + 1)->charpos == ellipsis_pos)
12753 glyph++, x += glyph->pixel_width;
12754 x -= glyph->pixel_width;
12755 glyph--;
12758 else if (match_with_avoid_cursor
12759 /* zero-width characters produce no glyphs */
12760 || ((row->reversed_p
12761 ? glyph_after > glyphs_end
12762 : glyph_after < glyphs_end)
12763 && eabs (glyph_after - glyph_before) == 1))
12765 cursor = glyph_after;
12766 x = -1;
12768 else if (string_seen)
12770 int incr = row->reversed_p ? -1 : +1;
12772 /* Need to find the glyph that came out of a string which is
12773 present at point. That glyph is somewhere between
12774 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
12775 positioned between POS_BEFORE and POS_AFTER in the
12776 buffer. */
12777 struct glyph *stop = glyph_after;
12778 EMACS_INT pos = pos_before;
12780 x = -1;
12781 for (glyph = glyph_before + incr;
12782 row->reversed_p ? glyph > stop : glyph < stop; )
12785 /* Any glyphs that come from the buffer are here because
12786 of bidi reordering. Skip them, and only pay
12787 attention to glyphs that came from some string. */
12788 if (STRINGP (glyph->object))
12790 Lisp_Object str;
12791 EMACS_INT tem;
12793 str = glyph->object;
12794 tem = string_buffer_position_lim (w, str, pos, pos_after, 0);
12795 if (tem == 0 /* from overlay */
12796 || pos <= tem)
12798 /* If the string from which this glyph came is
12799 found in the buffer at point, then we've
12800 found the glyph we've been looking for. If
12801 it comes from an overlay (tem == 0), and it
12802 has the `cursor' property on one of its
12803 glyphs, record that glyph as a candidate for
12804 displaying the cursor. (As in the
12805 unidirectional version, we will display the
12806 cursor on the last candidate we find.) */
12807 if (tem == 0 || tem == pt_old)
12809 /* The glyphs from this string could have
12810 been reordered. Find the one with the
12811 smallest string position. Or there could
12812 be a character in the string with the
12813 `cursor' property, which means display
12814 cursor on that character's glyph. */
12815 int strpos = glyph->charpos;
12817 cursor = glyph;
12818 for (glyph += incr;
12819 (row->reversed_p ? glyph > stop : glyph < stop)
12820 && EQ (glyph->object, str);
12821 glyph += incr)
12823 Lisp_Object cprop;
12824 int gpos = glyph->charpos;
12826 cprop = Fget_char_property (make_number (gpos),
12827 Qcursor,
12828 glyph->object);
12829 if (!NILP (cprop))
12831 cursor = glyph;
12832 break;
12834 if (glyph->charpos < strpos)
12836 strpos = glyph->charpos;
12837 cursor = glyph;
12841 if (tem == pt_old)
12842 goto compute_x;
12844 if (tem)
12845 pos = tem + 1; /* don't find previous instances */
12847 /* This string is not what we want; skip all of the
12848 glyphs that came from it. */
12850 glyph += incr;
12851 while ((row->reversed_p ? glyph > stop : glyph < stop)
12852 && EQ (glyph->object, str));
12854 else
12855 glyph += incr;
12858 /* If we reached the end of the line, and END was from a string,
12859 the cursor is not on this line. */
12860 if (cursor == NULL
12861 && (row->reversed_p ? glyph <= end : glyph >= end)
12862 && STRINGP (end->object)
12863 && row->continued_p)
12864 return 0;
12868 compute_x:
12869 if (cursor != NULL)
12870 glyph = cursor;
12871 if (x < 0)
12873 struct glyph *g;
12875 /* Need to compute x that corresponds to GLYPH. */
12876 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
12878 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
12879 abort ();
12880 x += g->pixel_width;
12884 /* ROW could be part of a continued line, which, under bidi
12885 reordering, might have other rows whose start and end charpos
12886 occlude point. Only set w->cursor if we found a better
12887 approximation to the cursor position than we have from previously
12888 examined candidate rows belonging to the same continued line. */
12889 if (/* we already have a candidate row */
12890 w->cursor.vpos >= 0
12891 /* that candidate is not the row we are processing */
12892 && MATRIX_ROW (matrix, w->cursor.vpos) != row
12893 /* the row we are processing is part of a continued line */
12894 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
12895 /* Make sure cursor.vpos specifies a row whose start and end
12896 charpos occlude point. This is because some callers of this
12897 function leave cursor.vpos at the row where the cursor was
12898 displayed during the last redisplay cycle. */
12899 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
12900 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
12902 struct glyph *g1 =
12903 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
12905 /* Don't consider glyphs that are outside TEXT_AREA. */
12906 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
12907 return 0;
12908 /* Keep the candidate whose buffer position is the closest to
12909 point. */
12910 if (/* previous candidate is a glyph in TEXT_AREA of that row */
12911 w->cursor.hpos >= 0
12912 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
12913 && BUFFERP (g1->object)
12914 && (g1->charpos == pt_old /* an exact match always wins */
12915 || (BUFFERP (glyph->object)
12916 && eabs (g1->charpos - pt_old)
12917 < eabs (glyph->charpos - pt_old))))
12918 return 0;
12919 /* If this candidate gives an exact match, use that. */
12920 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
12921 /* Otherwise, keep the candidate that comes from a row
12922 spanning less buffer positions. This may win when one or
12923 both candidate positions are on glyphs that came from
12924 display strings, for which we cannot compare buffer
12925 positions. */
12926 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12927 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
12928 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
12929 return 0;
12931 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12932 w->cursor.x = x;
12933 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12934 w->cursor.y = row->y + dy;
12936 if (w == XWINDOW (selected_window))
12938 if (!row->continued_p
12939 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12940 && row->x == 0)
12942 this_line_buffer = XBUFFER (w->buffer);
12944 CHARPOS (this_line_start_pos)
12945 = MATRIX_ROW_START_CHARPOS (row) + delta;
12946 BYTEPOS (this_line_start_pos)
12947 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12949 CHARPOS (this_line_end_pos)
12950 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12951 BYTEPOS (this_line_end_pos)
12952 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12954 this_line_y = w->cursor.y;
12955 this_line_pixel_height = row->height;
12956 this_line_vpos = w->cursor.vpos;
12957 this_line_start_x = row->x;
12959 else
12960 CHARPOS (this_line_start_pos) = 0;
12963 return 1;
12967 /* Run window scroll functions, if any, for WINDOW with new window
12968 start STARTP. Sets the window start of WINDOW to that position.
12970 We assume that the window's buffer is really current. */
12972 static INLINE struct text_pos
12973 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
12975 struct window *w = XWINDOW (window);
12976 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12978 if (current_buffer != XBUFFER (w->buffer))
12979 abort ();
12981 if (!NILP (Vwindow_scroll_functions))
12983 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12984 make_number (CHARPOS (startp)));
12985 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12986 /* In case the hook functions switch buffers. */
12987 if (current_buffer != XBUFFER (w->buffer))
12988 set_buffer_internal_1 (XBUFFER (w->buffer));
12991 return startp;
12995 /* Make sure the line containing the cursor is fully visible.
12996 A value of 1 means there is nothing to be done.
12997 (Either the line is fully visible, or it cannot be made so,
12998 or we cannot tell.)
13000 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13001 is higher than window.
13003 A value of 0 means the caller should do scrolling
13004 as if point had gone off the screen. */
13006 static int
13007 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13009 struct glyph_matrix *matrix;
13010 struct glyph_row *row;
13011 int window_height;
13013 if (!make_cursor_line_fully_visible_p)
13014 return 1;
13016 /* It's not always possible to find the cursor, e.g, when a window
13017 is full of overlay strings. Don't do anything in that case. */
13018 if (w->cursor.vpos < 0)
13019 return 1;
13021 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13022 row = MATRIX_ROW (matrix, w->cursor.vpos);
13024 /* If the cursor row is not partially visible, there's nothing to do. */
13025 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13026 return 1;
13028 /* If the row the cursor is in is taller than the window's height,
13029 it's not clear what to do, so do nothing. */
13030 window_height = window_box_height (w);
13031 if (row->height >= window_height)
13033 if (!force_p || MINI_WINDOW_P (w)
13034 || w->vscroll || w->cursor.vpos == 0)
13035 return 1;
13037 return 0;
13041 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13042 non-zero means only WINDOW is redisplayed in redisplay_internal.
13043 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
13044 in redisplay_window to bring a partially visible line into view in
13045 the case that only the cursor has moved.
13047 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13048 last screen line's vertical height extends past the end of the screen.
13050 Value is
13052 1 if scrolling succeeded
13054 0 if scrolling didn't find point.
13056 -1 if new fonts have been loaded so that we must interrupt
13057 redisplay, adjust glyph matrices, and try again. */
13059 enum
13061 SCROLLING_SUCCESS,
13062 SCROLLING_FAILED,
13063 SCROLLING_NEED_LARGER_MATRICES
13066 static int
13067 try_scrolling (Lisp_Object window, int just_this_one_p,
13068 EMACS_INT scroll_conservatively, EMACS_INT scroll_step,
13069 int temp_scroll_step, int last_line_misfit)
13071 struct window *w = XWINDOW (window);
13072 struct frame *f = XFRAME (w->frame);
13073 struct text_pos pos, startp;
13074 struct it it;
13075 int this_scroll_margin, scroll_max, rc, height;
13076 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13077 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13078 Lisp_Object aggressive;
13079 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
13081 #if GLYPH_DEBUG
13082 debug_method_add (w, "try_scrolling");
13083 #endif
13085 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13087 /* Compute scroll margin height in pixels. We scroll when point is
13088 within this distance from the top or bottom of the window. */
13089 if (scroll_margin > 0)
13090 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13091 * FRAME_LINE_HEIGHT (f);
13092 else
13093 this_scroll_margin = 0;
13095 /* Force scroll_conservatively to have a reasonable value, to avoid
13096 overflow while computing how much to scroll. Note that the user
13097 can supply scroll-conservatively equal to `most-positive-fixnum',
13098 which can be larger than INT_MAX. */
13099 if (scroll_conservatively > scroll_limit)
13101 scroll_conservatively = scroll_limit;
13102 scroll_max = INT_MAX;
13104 else if (scroll_step || scroll_conservatively || temp_scroll_step)
13105 /* Compute how much we should try to scroll maximally to bring
13106 point into view. */
13107 scroll_max = (max (scroll_step,
13108 max (scroll_conservatively, temp_scroll_step))
13109 * FRAME_LINE_HEIGHT (f));
13110 else if (NUMBERP (current_buffer->scroll_down_aggressively)
13111 || NUMBERP (current_buffer->scroll_up_aggressively))
13112 /* We're trying to scroll because of aggressive scrolling but no
13113 scroll_step is set. Choose an arbitrary one. */
13114 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13115 else
13116 scroll_max = 0;
13118 too_near_end:
13120 /* Decide whether to scroll down. */
13121 if (PT > CHARPOS (startp))
13123 int scroll_margin_y;
13125 /* Compute the pixel ypos of the scroll margin, then move it to
13126 either that ypos or PT, whichever comes first. */
13127 start_display (&it, w, startp);
13128 scroll_margin_y = it.last_visible_y - this_scroll_margin
13129 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13130 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13131 (MOVE_TO_POS | MOVE_TO_Y));
13133 if (PT > CHARPOS (it.current.pos))
13135 int y0 = line_bottom_y (&it);
13136 /* Compute how many pixels below window bottom to stop searching
13137 for PT. This avoids costly search for PT that is far away if
13138 the user limited scrolling by a small number of lines, but
13139 always finds PT if scroll_conservatively is set to a large
13140 number, such as most-positive-fixnum. */
13141 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13142 int y_to_move =
13143 slack >= INT_MAX - it.last_visible_y
13144 ? INT_MAX
13145 : it.last_visible_y + slack;
13147 /* Compute the distance from the scroll margin to PT or to
13148 the scroll limit, whichever comes first. This should
13149 include the height of the cursor line, to make that line
13150 fully visible. */
13151 move_it_to (&it, PT, -1, y_to_move,
13152 -1, MOVE_TO_POS | MOVE_TO_Y);
13153 dy = line_bottom_y (&it) - y0;
13155 if (dy > scroll_max)
13156 return SCROLLING_FAILED;
13158 scroll_down_p = 1;
13162 if (scroll_down_p)
13164 /* Point is in or below the bottom scroll margin, so move the
13165 window start down. If scrolling conservatively, move it just
13166 enough down to make point visible. If scroll_step is set,
13167 move it down by scroll_step. */
13168 if (scroll_conservatively)
13169 amount_to_scroll
13170 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13171 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
13172 else if (scroll_step || temp_scroll_step)
13173 amount_to_scroll = scroll_max;
13174 else
13176 aggressive = current_buffer->scroll_up_aggressively;
13177 height = WINDOW_BOX_TEXT_HEIGHT (w);
13178 if (NUMBERP (aggressive))
13180 double float_amount = XFLOATINT (aggressive) * height;
13181 amount_to_scroll = float_amount;
13182 if (amount_to_scroll == 0 && float_amount > 0)
13183 amount_to_scroll = 1;
13187 if (amount_to_scroll <= 0)
13188 return SCROLLING_FAILED;
13190 start_display (&it, w, startp);
13191 if (scroll_max < INT_MAX)
13192 move_it_vertically (&it, amount_to_scroll);
13193 else
13195 /* Extra precision for users who set scroll-conservatively
13196 to most-positive-fixnum: make sure the amount we scroll
13197 the window start is never less than amount_to_scroll,
13198 which was computed as distance from window bottom to
13199 point. This matters when lines at window top and lines
13200 below window bottom have different height. */
13201 struct it it1 = it;
13202 /* We use a temporary it1 because line_bottom_y can modify
13203 its argument, if it moves one line down; see there. */
13204 int start_y = line_bottom_y (&it1);
13206 do {
13207 move_it_by_lines (&it, 1, 1);
13208 it1 = it;
13209 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13212 /* If STARTP is unchanged, move it down another screen line. */
13213 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13214 move_it_by_lines (&it, 1, 1);
13215 startp = it.current.pos;
13217 else
13219 struct text_pos scroll_margin_pos = startp;
13221 /* See if point is inside the scroll margin at the top of the
13222 window. */
13223 if (this_scroll_margin)
13225 start_display (&it, w, startp);
13226 move_it_vertically (&it, this_scroll_margin);
13227 scroll_margin_pos = it.current.pos;
13230 if (PT < CHARPOS (scroll_margin_pos))
13232 /* Point is in the scroll margin at the top of the window or
13233 above what is displayed in the window. */
13234 int y0;
13236 /* Compute the vertical distance from PT to the scroll
13237 margin position. Give up if distance is greater than
13238 scroll_max. */
13239 SET_TEXT_POS (pos, PT, PT_BYTE);
13240 start_display (&it, w, pos);
13241 y0 = it.current_y;
13242 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13243 it.last_visible_y, -1,
13244 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13245 dy = it.current_y - y0;
13246 if (dy > scroll_max)
13247 return SCROLLING_FAILED;
13249 /* Compute new window start. */
13250 start_display (&it, w, startp);
13252 if (scroll_conservatively)
13253 amount_to_scroll
13254 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
13255 else if (scroll_step || temp_scroll_step)
13256 amount_to_scroll = scroll_max;
13257 else
13259 aggressive = current_buffer->scroll_down_aggressively;
13260 height = WINDOW_BOX_TEXT_HEIGHT (w);
13261 if (NUMBERP (aggressive))
13263 double float_amount = XFLOATINT (aggressive) * height;
13264 amount_to_scroll = float_amount;
13265 if (amount_to_scroll == 0 && float_amount > 0)
13266 amount_to_scroll = 1;
13270 if (amount_to_scroll <= 0)
13271 return SCROLLING_FAILED;
13273 move_it_vertically_backward (&it, amount_to_scroll);
13274 startp = it.current.pos;
13278 /* Run window scroll functions. */
13279 startp = run_window_scroll_functions (window, startp);
13281 /* Display the window. Give up if new fonts are loaded, or if point
13282 doesn't appear. */
13283 if (!try_window (window, startp, 0))
13284 rc = SCROLLING_NEED_LARGER_MATRICES;
13285 else if (w->cursor.vpos < 0)
13287 clear_glyph_matrix (w->desired_matrix);
13288 rc = SCROLLING_FAILED;
13290 else
13292 /* Maybe forget recorded base line for line number display. */
13293 if (!just_this_one_p
13294 || current_buffer->clip_changed
13295 || BEG_UNCHANGED < CHARPOS (startp))
13296 w->base_line_number = Qnil;
13298 /* If cursor ends up on a partially visible line,
13299 treat that as being off the bottom of the screen. */
13300 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
13302 clear_glyph_matrix (w->desired_matrix);
13303 ++extra_scroll_margin_lines;
13304 goto too_near_end;
13306 rc = SCROLLING_SUCCESS;
13309 return rc;
13313 /* Compute a suitable window start for window W if display of W starts
13314 on a continuation line. Value is non-zero if a new window start
13315 was computed.
13317 The new window start will be computed, based on W's width, starting
13318 from the start of the continued line. It is the start of the
13319 screen line with the minimum distance from the old start W->start. */
13321 static int
13322 compute_window_start_on_continuation_line (struct window *w)
13324 struct text_pos pos, start_pos;
13325 int window_start_changed_p = 0;
13327 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13329 /* If window start is on a continuation line... Window start may be
13330 < BEGV in case there's invisible text at the start of the
13331 buffer (M-x rmail, for example). */
13332 if (CHARPOS (start_pos) > BEGV
13333 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13335 struct it it;
13336 struct glyph_row *row;
13338 /* Handle the case that the window start is out of range. */
13339 if (CHARPOS (start_pos) < BEGV)
13340 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13341 else if (CHARPOS (start_pos) > ZV)
13342 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13344 /* Find the start of the continued line. This should be fast
13345 because scan_buffer is fast (newline cache). */
13346 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13347 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13348 row, DEFAULT_FACE_ID);
13349 reseat_at_previous_visible_line_start (&it);
13351 /* If the line start is "too far" away from the window start,
13352 say it takes too much time to compute a new window start. */
13353 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13354 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13356 int min_distance, distance;
13358 /* Move forward by display lines to find the new window
13359 start. If window width was enlarged, the new start can
13360 be expected to be > the old start. If window width was
13361 decreased, the new window start will be < the old start.
13362 So, we're looking for the display line start with the
13363 minimum distance from the old window start. */
13364 pos = it.current.pos;
13365 min_distance = INFINITY;
13366 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13367 distance < min_distance)
13369 min_distance = distance;
13370 pos = it.current.pos;
13371 move_it_by_lines (&it, 1, 0);
13374 /* Set the window start there. */
13375 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13376 window_start_changed_p = 1;
13380 return window_start_changed_p;
13384 /* Try cursor movement in case text has not changed in window WINDOW,
13385 with window start STARTP. Value is
13387 CURSOR_MOVEMENT_SUCCESS if successful
13389 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13391 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13392 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13393 we want to scroll as if scroll-step were set to 1. See the code.
13395 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13396 which case we have to abort this redisplay, and adjust matrices
13397 first. */
13399 enum
13401 CURSOR_MOVEMENT_SUCCESS,
13402 CURSOR_MOVEMENT_CANNOT_BE_USED,
13403 CURSOR_MOVEMENT_MUST_SCROLL,
13404 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13407 static int
13408 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13410 struct window *w = XWINDOW (window);
13411 struct frame *f = XFRAME (w->frame);
13412 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13414 #if GLYPH_DEBUG
13415 if (inhibit_try_cursor_movement)
13416 return rc;
13417 #endif
13419 /* Handle case where text has not changed, only point, and it has
13420 not moved off the frame. */
13421 if (/* Point may be in this window. */
13422 PT >= CHARPOS (startp)
13423 /* Selective display hasn't changed. */
13424 && !current_buffer->clip_changed
13425 /* Function force-mode-line-update is used to force a thorough
13426 redisplay. It sets either windows_or_buffers_changed or
13427 update_mode_lines. So don't take a shortcut here for these
13428 cases. */
13429 && !update_mode_lines
13430 && !windows_or_buffers_changed
13431 && !cursor_type_changed
13432 /* Can't use this case if highlighting a region. When a
13433 region exists, cursor movement has to do more than just
13434 set the cursor. */
13435 && !(!NILP (Vtransient_mark_mode)
13436 && !NILP (current_buffer->mark_active))
13437 && NILP (w->region_showing)
13438 && NILP (Vshow_trailing_whitespace)
13439 /* Right after splitting windows, last_point may be nil. */
13440 && INTEGERP (w->last_point)
13441 /* This code is not used for mini-buffer for the sake of the case
13442 of redisplaying to replace an echo area message; since in
13443 that case the mini-buffer contents per se are usually
13444 unchanged. This code is of no real use in the mini-buffer
13445 since the handling of this_line_start_pos, etc., in redisplay
13446 handles the same cases. */
13447 && !EQ (window, minibuf_window)
13448 /* When splitting windows or for new windows, it happens that
13449 redisplay is called with a nil window_end_vpos or one being
13450 larger than the window. This should really be fixed in
13451 window.c. I don't have this on my list, now, so we do
13452 approximately the same as the old redisplay code. --gerd. */
13453 && INTEGERP (w->window_end_vpos)
13454 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
13455 && (FRAME_WINDOW_P (f)
13456 || !overlay_arrow_in_current_buffer_p ()))
13458 int this_scroll_margin, top_scroll_margin;
13459 struct glyph_row *row = NULL;
13461 #if GLYPH_DEBUG
13462 debug_method_add (w, "cursor movement");
13463 #endif
13465 /* Scroll if point within this distance from the top or bottom
13466 of the window. This is a pixel value. */
13467 if (scroll_margin > 0)
13469 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13470 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13472 else
13473 this_scroll_margin = 0;
13475 top_scroll_margin = this_scroll_margin;
13476 if (WINDOW_WANTS_HEADER_LINE_P (w))
13477 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13479 /* Start with the row the cursor was displayed during the last
13480 not paused redisplay. Give up if that row is not valid. */
13481 if (w->last_cursor.vpos < 0
13482 || w->last_cursor.vpos >= w->current_matrix->nrows)
13483 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13484 else
13486 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13487 if (row->mode_line_p)
13488 ++row;
13489 if (!row->enabled_p)
13490 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13493 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13495 int scroll_p = 0, must_scroll = 0;
13496 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13498 if (PT > XFASTINT (w->last_point))
13500 /* Point has moved forward. */
13501 while (MATRIX_ROW_END_CHARPOS (row) < PT
13502 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13504 xassert (row->enabled_p);
13505 ++row;
13508 /* If the end position of a row equals the start
13509 position of the next row, and PT is at that position,
13510 we would rather display cursor in the next line. */
13511 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13512 && MATRIX_ROW_END_CHARPOS (row) == PT
13513 && row < w->current_matrix->rows
13514 + w->current_matrix->nrows - 1
13515 && MATRIX_ROW_START_CHARPOS (row+1) == PT
13516 && !cursor_row_p (w, row))
13517 ++row;
13519 /* If within the scroll margin, scroll. Note that
13520 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13521 the next line would be drawn, and that
13522 this_scroll_margin can be zero. */
13523 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13524 || PT > MATRIX_ROW_END_CHARPOS (row)
13525 /* Line is completely visible last line in window
13526 and PT is to be set in the next line. */
13527 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13528 && PT == MATRIX_ROW_END_CHARPOS (row)
13529 && !row->ends_at_zv_p
13530 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13531 scroll_p = 1;
13533 else if (PT < XFASTINT (w->last_point))
13535 /* Cursor has to be moved backward. Note that PT >=
13536 CHARPOS (startp) because of the outer if-statement. */
13537 while (!row->mode_line_p
13538 && (MATRIX_ROW_START_CHARPOS (row) > PT
13539 || (MATRIX_ROW_START_CHARPOS (row) == PT
13540 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13541 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13542 row > w->current_matrix->rows
13543 && (row-1)->ends_in_newline_from_string_p))))
13544 && (row->y > top_scroll_margin
13545 || CHARPOS (startp) == BEGV))
13547 xassert (row->enabled_p);
13548 --row;
13551 /* Consider the following case: Window starts at BEGV,
13552 there is invisible, intangible text at BEGV, so that
13553 display starts at some point START > BEGV. It can
13554 happen that we are called with PT somewhere between
13555 BEGV and START. Try to handle that case. */
13556 if (row < w->current_matrix->rows
13557 || row->mode_line_p)
13559 row = w->current_matrix->rows;
13560 if (row->mode_line_p)
13561 ++row;
13564 /* Due to newlines in overlay strings, we may have to
13565 skip forward over overlay strings. */
13566 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13567 && MATRIX_ROW_END_CHARPOS (row) == PT
13568 && !cursor_row_p (w, row))
13569 ++row;
13571 /* If within the scroll margin, scroll. */
13572 if (row->y < top_scroll_margin
13573 && CHARPOS (startp) != BEGV)
13574 scroll_p = 1;
13576 else
13578 /* Cursor did not move. So don't scroll even if cursor line
13579 is partially visible, as it was so before. */
13580 rc = CURSOR_MOVEMENT_SUCCESS;
13583 if (PT < MATRIX_ROW_START_CHARPOS (row)
13584 || PT > MATRIX_ROW_END_CHARPOS (row))
13586 /* if PT is not in the glyph row, give up. */
13587 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13588 must_scroll = 1;
13590 else if (rc != CURSOR_MOVEMENT_SUCCESS
13591 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13593 /* If rows are bidi-reordered and point moved, back up
13594 until we find a row that does not belong to a
13595 continuation line. This is because we must consider
13596 all rows of a continued line as candidates for the
13597 new cursor positioning, since row start and end
13598 positions change non-linearly with vertical position
13599 in such rows. */
13600 /* FIXME: Revisit this when glyph ``spilling'' in
13601 continuation lines' rows is implemented for
13602 bidi-reordered rows. */
13603 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
13605 xassert (row->enabled_p);
13606 --row;
13607 /* If we hit the beginning of the displayed portion
13608 without finding the first row of a continued
13609 line, give up. */
13610 if (row <= w->current_matrix->rows)
13612 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13613 break;
13618 if (must_scroll)
13620 else if (rc != CURSOR_MOVEMENT_SUCCESS
13621 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13622 && make_cursor_line_fully_visible_p)
13624 if (PT == MATRIX_ROW_END_CHARPOS (row)
13625 && !row->ends_at_zv_p
13626 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13627 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13628 else if (row->height > window_box_height (w))
13630 /* If we end up in a partially visible line, let's
13631 make it fully visible, except when it's taller
13632 than the window, in which case we can't do much
13633 about it. */
13634 *scroll_step = 1;
13635 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13637 else
13639 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13640 if (!cursor_row_fully_visible_p (w, 0, 1))
13641 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13642 else
13643 rc = CURSOR_MOVEMENT_SUCCESS;
13646 else if (scroll_p)
13647 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13648 else if (rc != CURSOR_MOVEMENT_SUCCESS
13649 && !NILP (XBUFFER (w->buffer)->bidi_display_reordering))
13651 /* With bidi-reordered rows, there could be more than
13652 one candidate row whose start and end positions
13653 occlude point. We need to let set_cursor_from_row
13654 find the best candidate. */
13655 /* FIXME: Revisit this when glyph ``spilling'' in
13656 continuation lines' rows is implemented for
13657 bidi-reordered rows. */
13658 int rv = 0;
13662 if (MATRIX_ROW_START_CHARPOS (row) <= PT
13663 && PT <= MATRIX_ROW_END_CHARPOS (row)
13664 && cursor_row_p (w, row))
13665 rv |= set_cursor_from_row (w, row, w->current_matrix,
13666 0, 0, 0, 0);
13667 /* As soon as we've found the first suitable row
13668 whose ends_at_zv_p flag is set, we are done. */
13669 if (rv
13670 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
13672 rc = CURSOR_MOVEMENT_SUCCESS;
13673 break;
13675 ++row;
13677 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
13678 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
13679 || (MATRIX_ROW_START_CHARPOS (row) == PT
13680 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
13681 /* If we didn't find any candidate rows, or exited the
13682 loop before all the candidates were examined, signal
13683 to the caller that this method failed. */
13684 if (rc != CURSOR_MOVEMENT_SUCCESS
13685 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
13686 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13687 else if (rv)
13688 rc = CURSOR_MOVEMENT_SUCCESS;
13690 else
13694 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13696 rc = CURSOR_MOVEMENT_SUCCESS;
13697 break;
13699 ++row;
13701 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13702 && MATRIX_ROW_START_CHARPOS (row) == PT
13703 && cursor_row_p (w, row));
13708 return rc;
13711 void
13712 set_vertical_scroll_bar (struct window *w)
13714 int start, end, whole;
13716 /* Calculate the start and end positions for the current window.
13717 At some point, it would be nice to choose between scrollbars
13718 which reflect the whole buffer size, with special markers
13719 indicating narrowing, and scrollbars which reflect only the
13720 visible region.
13722 Note that mini-buffers sometimes aren't displaying any text. */
13723 if (!MINI_WINDOW_P (w)
13724 || (w == XWINDOW (minibuf_window)
13725 && NILP (echo_area_buffer[0])))
13727 struct buffer *buf = XBUFFER (w->buffer);
13728 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13729 start = marker_position (w->start) - BUF_BEGV (buf);
13730 /* I don't think this is guaranteed to be right. For the
13731 moment, we'll pretend it is. */
13732 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13734 if (end < start)
13735 end = start;
13736 if (whole < (end - start))
13737 whole = end - start;
13739 else
13740 start = end = whole = 0;
13742 /* Indicate what this scroll bar ought to be displaying now. */
13743 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13744 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13745 (w, end - start, whole, start);
13749 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13750 selected_window is redisplayed.
13752 We can return without actually redisplaying the window if
13753 fonts_changed_p is nonzero. In that case, redisplay_internal will
13754 retry. */
13756 static void
13757 redisplay_window (Lisp_Object window, int just_this_one_p)
13759 struct window *w = XWINDOW (window);
13760 struct frame *f = XFRAME (w->frame);
13761 struct buffer *buffer = XBUFFER (w->buffer);
13762 struct buffer *old = current_buffer;
13763 struct text_pos lpoint, opoint, startp;
13764 int update_mode_line;
13765 int tem;
13766 struct it it;
13767 /* Record it now because it's overwritten. */
13768 int current_matrix_up_to_date_p = 0;
13769 int used_current_matrix_p = 0;
13770 /* This is less strict than current_matrix_up_to_date_p.
13771 It indictes that the buffer contents and narrowing are unchanged. */
13772 int buffer_unchanged_p = 0;
13773 int temp_scroll_step = 0;
13774 int count = SPECPDL_INDEX ();
13775 int rc;
13776 int centering_position = -1;
13777 int last_line_misfit = 0;
13778 int beg_unchanged, end_unchanged;
13780 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13781 opoint = lpoint;
13783 /* W must be a leaf window here. */
13784 xassert (!NILP (w->buffer));
13785 #if GLYPH_DEBUG
13786 *w->desired_matrix->method = 0;
13787 #endif
13789 restart:
13790 reconsider_clip_changes (w, buffer);
13792 /* Has the mode line to be updated? */
13793 update_mode_line = (!NILP (w->update_mode_line)
13794 || update_mode_lines
13795 || buffer->clip_changed
13796 || buffer->prevent_redisplay_optimizations_p);
13798 if (MINI_WINDOW_P (w))
13800 if (w == XWINDOW (echo_area_window)
13801 && !NILP (echo_area_buffer[0]))
13803 if (update_mode_line)
13804 /* We may have to update a tty frame's menu bar or a
13805 tool-bar. Example `M-x C-h C-h C-g'. */
13806 goto finish_menu_bars;
13807 else
13808 /* We've already displayed the echo area glyphs in this window. */
13809 goto finish_scroll_bars;
13811 else if ((w != XWINDOW (minibuf_window)
13812 || minibuf_level == 0)
13813 /* When buffer is nonempty, redisplay window normally. */
13814 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13815 /* Quail displays non-mini buffers in minibuffer window.
13816 In that case, redisplay the window normally. */
13817 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13819 /* W is a mini-buffer window, but it's not active, so clear
13820 it. */
13821 int yb = window_text_bottom_y (w);
13822 struct glyph_row *row;
13823 int y;
13825 for (y = 0, row = w->desired_matrix->rows;
13826 y < yb;
13827 y += row->height, ++row)
13828 blank_row (w, row, y);
13829 goto finish_scroll_bars;
13832 clear_glyph_matrix (w->desired_matrix);
13835 /* Otherwise set up data on this window; select its buffer and point
13836 value. */
13837 /* Really select the buffer, for the sake of buffer-local
13838 variables. */
13839 set_buffer_internal_1 (XBUFFER (w->buffer));
13841 current_matrix_up_to_date_p
13842 = (!NILP (w->window_end_valid)
13843 && !current_buffer->clip_changed
13844 && !current_buffer->prevent_redisplay_optimizations_p
13845 && XFASTINT (w->last_modified) >= MODIFF
13846 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13848 /* Run the window-bottom-change-functions
13849 if it is possible that the text on the screen has changed
13850 (either due to modification of the text, or any other reason). */
13851 if (!current_matrix_up_to_date_p
13852 && !NILP (Vwindow_text_change_functions))
13854 safe_run_hooks (Qwindow_text_change_functions);
13855 goto restart;
13858 beg_unchanged = BEG_UNCHANGED;
13859 end_unchanged = END_UNCHANGED;
13861 SET_TEXT_POS (opoint, PT, PT_BYTE);
13863 specbind (Qinhibit_point_motion_hooks, Qt);
13865 buffer_unchanged_p
13866 = (!NILP (w->window_end_valid)
13867 && !current_buffer->clip_changed
13868 && XFASTINT (w->last_modified) >= MODIFF
13869 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13871 /* When windows_or_buffers_changed is non-zero, we can't rely on
13872 the window end being valid, so set it to nil there. */
13873 if (windows_or_buffers_changed)
13875 /* If window starts on a continuation line, maybe adjust the
13876 window start in case the window's width changed. */
13877 if (XMARKER (w->start)->buffer == current_buffer)
13878 compute_window_start_on_continuation_line (w);
13880 w->window_end_valid = Qnil;
13883 /* Some sanity checks. */
13884 CHECK_WINDOW_END (w);
13885 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13886 abort ();
13887 if (BYTEPOS (opoint) < CHARPOS (opoint))
13888 abort ();
13890 /* If %c is in mode line, update it if needed. */
13891 if (!NILP (w->column_number_displayed)
13892 /* This alternative quickly identifies a common case
13893 where no change is needed. */
13894 && !(PT == XFASTINT (w->last_point)
13895 && XFASTINT (w->last_modified) >= MODIFF
13896 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13897 && (XFASTINT (w->column_number_displayed)
13898 != (int) current_column ())) /* iftc */
13899 update_mode_line = 1;
13901 /* Count number of windows showing the selected buffer. An indirect
13902 buffer counts as its base buffer. */
13903 if (!just_this_one_p)
13905 struct buffer *current_base, *window_base;
13906 current_base = current_buffer;
13907 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13908 if (current_base->base_buffer)
13909 current_base = current_base->base_buffer;
13910 if (window_base->base_buffer)
13911 window_base = window_base->base_buffer;
13912 if (current_base == window_base)
13913 buffer_shared++;
13916 /* Point refers normally to the selected window. For any other
13917 window, set up appropriate value. */
13918 if (!EQ (window, selected_window))
13920 int new_pt = XMARKER (w->pointm)->charpos;
13921 int new_pt_byte = marker_byte_position (w->pointm);
13922 if (new_pt < BEGV)
13924 new_pt = BEGV;
13925 new_pt_byte = BEGV_BYTE;
13926 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13928 else if (new_pt > (ZV - 1))
13930 new_pt = ZV;
13931 new_pt_byte = ZV_BYTE;
13932 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13935 /* We don't use SET_PT so that the point-motion hooks don't run. */
13936 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13939 /* If any of the character widths specified in the display table
13940 have changed, invalidate the width run cache. It's true that
13941 this may be a bit late to catch such changes, but the rest of
13942 redisplay goes (non-fatally) haywire when the display table is
13943 changed, so why should we worry about doing any better? */
13944 if (current_buffer->width_run_cache)
13946 struct Lisp_Char_Table *disptab = buffer_display_table ();
13948 if (! disptab_matches_widthtab (disptab,
13949 XVECTOR (current_buffer->width_table)))
13951 invalidate_region_cache (current_buffer,
13952 current_buffer->width_run_cache,
13953 BEG, Z);
13954 recompute_width_table (current_buffer, disptab);
13958 /* If window-start is screwed up, choose a new one. */
13959 if (XMARKER (w->start)->buffer != current_buffer)
13960 goto recenter;
13962 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13964 /* If someone specified a new starting point but did not insist,
13965 check whether it can be used. */
13966 if (!NILP (w->optional_new_start)
13967 && CHARPOS (startp) >= BEGV
13968 && CHARPOS (startp) <= ZV)
13970 w->optional_new_start = Qnil;
13971 start_display (&it, w, startp);
13972 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13973 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13974 if (IT_CHARPOS (it) == PT)
13975 w->force_start = Qt;
13976 /* IT may overshoot PT if text at PT is invisible. */
13977 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13978 w->force_start = Qt;
13981 force_start:
13983 /* Handle case where place to start displaying has been specified,
13984 unless the specified location is outside the accessible range. */
13985 if (!NILP (w->force_start)
13986 || w->frozen_window_start_p)
13988 /* We set this later on if we have to adjust point. */
13989 int new_vpos = -1;
13991 w->force_start = Qnil;
13992 w->vscroll = 0;
13993 w->window_end_valid = Qnil;
13995 /* Forget any recorded base line for line number display. */
13996 if (!buffer_unchanged_p)
13997 w->base_line_number = Qnil;
13999 /* Redisplay the mode line. Select the buffer properly for that.
14000 Also, run the hook window-scroll-functions
14001 because we have scrolled. */
14002 /* Note, we do this after clearing force_start because
14003 if there's an error, it is better to forget about force_start
14004 than to get into an infinite loop calling the hook functions
14005 and having them get more errors. */
14006 if (!update_mode_line
14007 || ! NILP (Vwindow_scroll_functions))
14009 update_mode_line = 1;
14010 w->update_mode_line = Qt;
14011 startp = run_window_scroll_functions (window, startp);
14014 w->last_modified = make_number (0);
14015 w->last_overlay_modified = make_number (0);
14016 if (CHARPOS (startp) < BEGV)
14017 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14018 else if (CHARPOS (startp) > ZV)
14019 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14021 /* Redisplay, then check if cursor has been set during the
14022 redisplay. Give up if new fonts were loaded. */
14023 /* We used to issue a CHECK_MARGINS argument to try_window here,
14024 but this causes scrolling to fail when point begins inside
14025 the scroll margin (bug#148) -- cyd */
14026 if (!try_window (window, startp, 0))
14028 w->force_start = Qt;
14029 clear_glyph_matrix (w->desired_matrix);
14030 goto need_larger_matrices;
14033 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14035 /* If point does not appear, try to move point so it does
14036 appear. The desired matrix has been built above, so we
14037 can use it here. */
14038 new_vpos = window_box_height (w) / 2;
14041 if (!cursor_row_fully_visible_p (w, 0, 0))
14043 /* Point does appear, but on a line partly visible at end of window.
14044 Move it back to a fully-visible line. */
14045 new_vpos = window_box_height (w);
14048 /* If we need to move point for either of the above reasons,
14049 now actually do it. */
14050 if (new_vpos >= 0)
14052 struct glyph_row *row;
14054 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14055 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14056 ++row;
14058 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14059 MATRIX_ROW_START_BYTEPOS (row));
14061 if (w != XWINDOW (selected_window))
14062 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14063 else if (current_buffer == old)
14064 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14066 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14068 /* If we are highlighting the region, then we just changed
14069 the region, so redisplay to show it. */
14070 if (!NILP (Vtransient_mark_mode)
14071 && !NILP (current_buffer->mark_active))
14073 clear_glyph_matrix (w->desired_matrix);
14074 if (!try_window (window, startp, 0))
14075 goto need_larger_matrices;
14079 #if GLYPH_DEBUG
14080 debug_method_add (w, "forced window start");
14081 #endif
14082 goto done;
14085 /* Handle case where text has not changed, only point, and it has
14086 not moved off the frame, and we are not retrying after hscroll.
14087 (current_matrix_up_to_date_p is nonzero when retrying.) */
14088 if (current_matrix_up_to_date_p
14089 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14090 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14092 switch (rc)
14094 case CURSOR_MOVEMENT_SUCCESS:
14095 used_current_matrix_p = 1;
14096 goto done;
14098 case CURSOR_MOVEMENT_MUST_SCROLL:
14099 goto try_to_scroll;
14101 default:
14102 abort ();
14105 /* If current starting point was originally the beginning of a line
14106 but no longer is, find a new starting point. */
14107 else if (!NILP (w->start_at_line_beg)
14108 && !(CHARPOS (startp) <= BEGV
14109 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14111 #if GLYPH_DEBUG
14112 debug_method_add (w, "recenter 1");
14113 #endif
14114 goto recenter;
14117 /* Try scrolling with try_window_id. Value is > 0 if update has
14118 been done, it is -1 if we know that the same window start will
14119 not work. It is 0 if unsuccessful for some other reason. */
14120 else if ((tem = try_window_id (w)) != 0)
14122 #if GLYPH_DEBUG
14123 debug_method_add (w, "try_window_id %d", tem);
14124 #endif
14126 if (fonts_changed_p)
14127 goto need_larger_matrices;
14128 if (tem > 0)
14129 goto done;
14131 /* Otherwise try_window_id has returned -1 which means that we
14132 don't want the alternative below this comment to execute. */
14134 else if (CHARPOS (startp) >= BEGV
14135 && CHARPOS (startp) <= ZV
14136 && PT >= CHARPOS (startp)
14137 && (CHARPOS (startp) < ZV
14138 /* Avoid starting at end of buffer. */
14139 || CHARPOS (startp) == BEGV
14140 || (XFASTINT (w->last_modified) >= MODIFF
14141 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14144 /* If first window line is a continuation line, and window start
14145 is inside the modified region, but the first change is before
14146 current window start, we must select a new window start.
14148 However, if this is the result of a down-mouse event (e.g. by
14149 extending the mouse-drag-overlay), we don't want to select a
14150 new window start, since that would change the position under
14151 the mouse, resulting in an unwanted mouse-movement rather
14152 than a simple mouse-click. */
14153 if (NILP (w->start_at_line_beg)
14154 && NILP (do_mouse_tracking)
14155 && CHARPOS (startp) > BEGV
14156 && CHARPOS (startp) > BEG + beg_unchanged
14157 && CHARPOS (startp) <= Z - end_unchanged
14158 /* Even if w->start_at_line_beg is nil, a new window may
14159 start at a line_beg, since that's how set_buffer_window
14160 sets it. So, we need to check the return value of
14161 compute_window_start_on_continuation_line. (See also
14162 bug#197). */
14163 && XMARKER (w->start)->buffer == current_buffer
14164 && compute_window_start_on_continuation_line (w))
14166 w->force_start = Qt;
14167 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14168 goto force_start;
14171 #if GLYPH_DEBUG
14172 debug_method_add (w, "same window start");
14173 #endif
14175 /* Try to redisplay starting at same place as before.
14176 If point has not moved off frame, accept the results. */
14177 if (!current_matrix_up_to_date_p
14178 /* Don't use try_window_reusing_current_matrix in this case
14179 because a window scroll function can have changed the
14180 buffer. */
14181 || !NILP (Vwindow_scroll_functions)
14182 || MINI_WINDOW_P (w)
14183 || !(used_current_matrix_p
14184 = try_window_reusing_current_matrix (w)))
14186 IF_DEBUG (debug_method_add (w, "1"));
14187 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14188 /* -1 means we need to scroll.
14189 0 means we need new matrices, but fonts_changed_p
14190 is set in that case, so we will detect it below. */
14191 goto try_to_scroll;
14194 if (fonts_changed_p)
14195 goto need_larger_matrices;
14197 if (w->cursor.vpos >= 0)
14199 if (!just_this_one_p
14200 || current_buffer->clip_changed
14201 || BEG_UNCHANGED < CHARPOS (startp))
14202 /* Forget any recorded base line for line number display. */
14203 w->base_line_number = Qnil;
14205 if (!cursor_row_fully_visible_p (w, 1, 0))
14207 clear_glyph_matrix (w->desired_matrix);
14208 last_line_misfit = 1;
14210 /* Drop through and scroll. */
14211 else
14212 goto done;
14214 else
14215 clear_glyph_matrix (w->desired_matrix);
14218 try_to_scroll:
14220 w->last_modified = make_number (0);
14221 w->last_overlay_modified = make_number (0);
14223 /* Redisplay the mode line. Select the buffer properly for that. */
14224 if (!update_mode_line)
14226 update_mode_line = 1;
14227 w->update_mode_line = Qt;
14230 /* Try to scroll by specified few lines. */
14231 if ((scroll_conservatively
14232 || scroll_step
14233 || temp_scroll_step
14234 || NUMBERP (current_buffer->scroll_up_aggressively)
14235 || NUMBERP (current_buffer->scroll_down_aggressively))
14236 && !current_buffer->clip_changed
14237 && CHARPOS (startp) >= BEGV
14238 && CHARPOS (startp) <= ZV)
14240 /* The function returns -1 if new fonts were loaded, 1 if
14241 successful, 0 if not successful. */
14242 int rc = try_scrolling (window, just_this_one_p,
14243 scroll_conservatively,
14244 scroll_step,
14245 temp_scroll_step, last_line_misfit);
14246 switch (rc)
14248 case SCROLLING_SUCCESS:
14249 goto done;
14251 case SCROLLING_NEED_LARGER_MATRICES:
14252 goto need_larger_matrices;
14254 case SCROLLING_FAILED:
14255 break;
14257 default:
14258 abort ();
14262 /* Finally, just choose place to start which centers point */
14264 recenter:
14265 if (centering_position < 0)
14266 centering_position = window_box_height (w) / 2;
14268 #if GLYPH_DEBUG
14269 debug_method_add (w, "recenter");
14270 #endif
14272 /* w->vscroll = 0; */
14274 /* Forget any previously recorded base line for line number display. */
14275 if (!buffer_unchanged_p)
14276 w->base_line_number = Qnil;
14278 /* Move backward half the height of the window. */
14279 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14280 it.current_y = it.last_visible_y;
14281 move_it_vertically_backward (&it, centering_position);
14282 xassert (IT_CHARPOS (it) >= BEGV);
14284 /* The function move_it_vertically_backward may move over more
14285 than the specified y-distance. If it->w is small, e.g. a
14286 mini-buffer window, we may end up in front of the window's
14287 display area. Start displaying at the start of the line
14288 containing PT in this case. */
14289 if (it.current_y <= 0)
14291 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14292 move_it_vertically_backward (&it, 0);
14293 it.current_y = 0;
14296 it.current_x = it.hpos = 0;
14298 /* Set startp here explicitly in case that helps avoid an infinite loop
14299 in case the window-scroll-functions functions get errors. */
14300 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14302 /* Run scroll hooks. */
14303 startp = run_window_scroll_functions (window, it.current.pos);
14305 /* Redisplay the window. */
14306 if (!current_matrix_up_to_date_p
14307 || windows_or_buffers_changed
14308 || cursor_type_changed
14309 /* Don't use try_window_reusing_current_matrix in this case
14310 because it can have changed the buffer. */
14311 || !NILP (Vwindow_scroll_functions)
14312 || !just_this_one_p
14313 || MINI_WINDOW_P (w)
14314 || !(used_current_matrix_p
14315 = try_window_reusing_current_matrix (w)))
14316 try_window (window, startp, 0);
14318 /* If new fonts have been loaded (due to fontsets), give up. We
14319 have to start a new redisplay since we need to re-adjust glyph
14320 matrices. */
14321 if (fonts_changed_p)
14322 goto need_larger_matrices;
14324 /* If cursor did not appear assume that the middle of the window is
14325 in the first line of the window. Do it again with the next line.
14326 (Imagine a window of height 100, displaying two lines of height
14327 60. Moving back 50 from it->last_visible_y will end in the first
14328 line.) */
14329 if (w->cursor.vpos < 0)
14331 if (!NILP (w->window_end_valid)
14332 && PT >= Z - XFASTINT (w->window_end_pos))
14334 clear_glyph_matrix (w->desired_matrix);
14335 move_it_by_lines (&it, 1, 0);
14336 try_window (window, it.current.pos, 0);
14338 else if (PT < IT_CHARPOS (it))
14340 clear_glyph_matrix (w->desired_matrix);
14341 move_it_by_lines (&it, -1, 0);
14342 try_window (window, it.current.pos, 0);
14344 else
14346 /* Not much we can do about it. */
14350 /* Consider the following case: Window starts at BEGV, there is
14351 invisible, intangible text at BEGV, so that display starts at
14352 some point START > BEGV. It can happen that we are called with
14353 PT somewhere between BEGV and START. Try to handle that case. */
14354 if (w->cursor.vpos < 0)
14356 struct glyph_row *row = w->current_matrix->rows;
14357 if (row->mode_line_p)
14358 ++row;
14359 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14362 if (!cursor_row_fully_visible_p (w, 0, 0))
14364 /* If vscroll is enabled, disable it and try again. */
14365 if (w->vscroll)
14367 w->vscroll = 0;
14368 clear_glyph_matrix (w->desired_matrix);
14369 goto recenter;
14372 /* If centering point failed to make the whole line visible,
14373 put point at the top instead. That has to make the whole line
14374 visible, if it can be done. */
14375 if (centering_position == 0)
14376 goto done;
14378 clear_glyph_matrix (w->desired_matrix);
14379 centering_position = 0;
14380 goto recenter;
14383 done:
14385 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14386 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
14387 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
14388 ? Qt : Qnil);
14390 /* Display the mode line, if we must. */
14391 if ((update_mode_line
14392 /* If window not full width, must redo its mode line
14393 if (a) the window to its side is being redone and
14394 (b) we do a frame-based redisplay. This is a consequence
14395 of how inverted lines are drawn in frame-based redisplay. */
14396 || (!just_this_one_p
14397 && !FRAME_WINDOW_P (f)
14398 && !WINDOW_FULL_WIDTH_P (w))
14399 /* Line number to display. */
14400 || INTEGERP (w->base_line_pos)
14401 /* Column number is displayed and different from the one displayed. */
14402 || (!NILP (w->column_number_displayed)
14403 && (XFASTINT (w->column_number_displayed)
14404 != (int) current_column ()))) /* iftc */
14405 /* This means that the window has a mode line. */
14406 && (WINDOW_WANTS_MODELINE_P (w)
14407 || WINDOW_WANTS_HEADER_LINE_P (w)))
14409 display_mode_lines (w);
14411 /* If mode line height has changed, arrange for a thorough
14412 immediate redisplay using the correct mode line height. */
14413 if (WINDOW_WANTS_MODELINE_P (w)
14414 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
14416 fonts_changed_p = 1;
14417 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
14418 = DESIRED_MODE_LINE_HEIGHT (w);
14421 /* If header line height has changed, arrange for a thorough
14422 immediate redisplay using the correct header line height. */
14423 if (WINDOW_WANTS_HEADER_LINE_P (w)
14424 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
14426 fonts_changed_p = 1;
14427 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
14428 = DESIRED_HEADER_LINE_HEIGHT (w);
14431 if (fonts_changed_p)
14432 goto need_larger_matrices;
14435 if (!line_number_displayed
14436 && !BUFFERP (w->base_line_pos))
14438 w->base_line_pos = Qnil;
14439 w->base_line_number = Qnil;
14442 finish_menu_bars:
14444 /* When we reach a frame's selected window, redo the frame's menu bar. */
14445 if (update_mode_line
14446 && EQ (FRAME_SELECTED_WINDOW (f), window))
14448 int redisplay_menu_p = 0;
14449 int redisplay_tool_bar_p = 0;
14451 if (FRAME_WINDOW_P (f))
14453 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
14454 || defined (HAVE_NS) || defined (USE_GTK)
14455 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
14456 #else
14457 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14458 #endif
14460 else
14461 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
14463 if (redisplay_menu_p)
14464 display_menu_bar (w);
14466 #ifdef HAVE_WINDOW_SYSTEM
14467 if (FRAME_WINDOW_P (f))
14469 #if defined (USE_GTK) || defined (HAVE_NS)
14470 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
14471 #else
14472 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
14473 && (FRAME_TOOL_BAR_LINES (f) > 0
14474 || !NILP (Vauto_resize_tool_bars));
14475 #endif
14477 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
14479 ignore_mouse_drag_p = 1;
14482 #endif
14485 #ifdef HAVE_WINDOW_SYSTEM
14486 if (FRAME_WINDOW_P (f)
14487 && update_window_fringes (w, (just_this_one_p
14488 || (!used_current_matrix_p && !overlay_arrow_seen)
14489 || w->pseudo_window_p)))
14491 update_begin (f);
14492 BLOCK_INPUT;
14493 if (draw_window_fringes (w, 1))
14494 x_draw_vertical_border (w);
14495 UNBLOCK_INPUT;
14496 update_end (f);
14498 #endif /* HAVE_WINDOW_SYSTEM */
14500 /* We go to this label, with fonts_changed_p nonzero,
14501 if it is necessary to try again using larger glyph matrices.
14502 We have to redeem the scroll bar even in this case,
14503 because the loop in redisplay_internal expects that. */
14504 need_larger_matrices:
14506 finish_scroll_bars:
14508 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
14510 /* Set the thumb's position and size. */
14511 set_vertical_scroll_bar (w);
14513 /* Note that we actually used the scroll bar attached to this
14514 window, so it shouldn't be deleted at the end of redisplay. */
14515 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
14516 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
14519 /* Restore current_buffer and value of point in it. The window
14520 update may have changed the buffer, so first make sure `opoint'
14521 is still valid (Bug#6177). */
14522 if (CHARPOS (opoint) < BEGV)
14523 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
14524 else if (CHARPOS (opoint) > ZV)
14525 TEMP_SET_PT_BOTH (Z, Z_BYTE);
14526 else
14527 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
14529 set_buffer_internal_1 (old);
14530 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
14531 shorter. This can be caused by log truncation in *Messages*. */
14532 if (CHARPOS (lpoint) <= ZV)
14533 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
14535 unbind_to (count, Qnil);
14539 /* Build the complete desired matrix of WINDOW with a window start
14540 buffer position POS.
14542 Value is 1 if successful. It is zero if fonts were loaded during
14543 redisplay which makes re-adjusting glyph matrices necessary, and -1
14544 if point would appear in the scroll margins.
14545 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
14546 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
14547 set in FLAGS.) */
14550 try_window (Lisp_Object window, struct text_pos pos, int flags)
14552 struct window *w = XWINDOW (window);
14553 struct it it;
14554 struct glyph_row *last_text_row = NULL;
14555 struct frame *f = XFRAME (w->frame);
14557 /* Make POS the new window start. */
14558 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14560 /* Mark cursor position as unknown. No overlay arrow seen. */
14561 w->cursor.vpos = -1;
14562 overlay_arrow_seen = 0;
14564 /* Initialize iterator and info to start at POS. */
14565 start_display (&it, w, pos);
14567 /* Display all lines of W. */
14568 while (it.current_y < it.last_visible_y)
14570 if (display_line (&it))
14571 last_text_row = it.glyph_row - 1;
14572 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14573 return 0;
14576 /* Don't let the cursor end in the scroll margins. */
14577 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14578 && !MINI_WINDOW_P (w))
14580 int this_scroll_margin;
14582 if (scroll_margin > 0)
14584 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14585 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14587 else
14588 this_scroll_margin = 0;
14590 if ((w->cursor.y >= 0 /* not vscrolled */
14591 && w->cursor.y < this_scroll_margin
14592 && CHARPOS (pos) > BEGV
14593 && IT_CHARPOS (it) < ZV)
14594 /* rms: considering make_cursor_line_fully_visible_p here
14595 seems to give wrong results. We don't want to recenter
14596 when the last line is partly visible, we want to allow
14597 that case to be handled in the usual way. */
14598 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14600 w->cursor.vpos = -1;
14601 clear_glyph_matrix (w->desired_matrix);
14602 return -1;
14606 /* If bottom moved off end of frame, change mode line percentage. */
14607 if (XFASTINT (w->window_end_pos) <= 0
14608 && Z != IT_CHARPOS (it))
14609 w->update_mode_line = Qt;
14611 /* Set window_end_pos to the offset of the last character displayed
14612 on the window from the end of current_buffer. Set
14613 window_end_vpos to its row number. */
14614 if (last_text_row)
14616 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14617 w->window_end_bytepos
14618 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14619 w->window_end_pos
14620 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14621 w->window_end_vpos
14622 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14623 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14624 ->displays_text_p);
14626 else
14628 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14629 w->window_end_pos = make_number (Z - ZV);
14630 w->window_end_vpos = make_number (0);
14633 /* But that is not valid info until redisplay finishes. */
14634 w->window_end_valid = Qnil;
14635 return 1;
14640 /************************************************************************
14641 Window redisplay reusing current matrix when buffer has not changed
14642 ************************************************************************/
14644 /* Try redisplay of window W showing an unchanged buffer with a
14645 different window start than the last time it was displayed by
14646 reusing its current matrix. Value is non-zero if successful.
14647 W->start is the new window start. */
14649 static int
14650 try_window_reusing_current_matrix (struct window *w)
14652 struct frame *f = XFRAME (w->frame);
14653 struct glyph_row *row, *bottom_row;
14654 struct it it;
14655 struct run run;
14656 struct text_pos start, new_start;
14657 int nrows_scrolled, i;
14658 struct glyph_row *last_text_row;
14659 struct glyph_row *last_reused_text_row;
14660 struct glyph_row *start_row;
14661 int start_vpos, min_y, max_y;
14663 #if GLYPH_DEBUG
14664 if (inhibit_try_window_reusing)
14665 return 0;
14666 #endif
14668 if (/* This function doesn't handle terminal frames. */
14669 !FRAME_WINDOW_P (f)
14670 /* Don't try to reuse the display if windows have been split
14671 or such. */
14672 || windows_or_buffers_changed
14673 || cursor_type_changed)
14674 return 0;
14676 /* Can't do this if region may have changed. */
14677 if ((!NILP (Vtransient_mark_mode)
14678 && !NILP (current_buffer->mark_active))
14679 || !NILP (w->region_showing)
14680 || !NILP (Vshow_trailing_whitespace))
14681 return 0;
14683 /* If top-line visibility has changed, give up. */
14684 if (WINDOW_WANTS_HEADER_LINE_P (w)
14685 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14686 return 0;
14688 /* Give up if old or new display is scrolled vertically. We could
14689 make this function handle this, but right now it doesn't. */
14690 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14691 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14692 return 0;
14694 /* The variable new_start now holds the new window start. The old
14695 start `start' can be determined from the current matrix. */
14696 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14697 start = start_row->minpos;
14698 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14700 /* Clear the desired matrix for the display below. */
14701 clear_glyph_matrix (w->desired_matrix);
14703 if (CHARPOS (new_start) <= CHARPOS (start))
14705 int first_row_y;
14707 /* Don't use this method if the display starts with an ellipsis
14708 displayed for invisible text. It's not easy to handle that case
14709 below, and it's certainly not worth the effort since this is
14710 not a frequent case. */
14711 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14712 return 0;
14714 IF_DEBUG (debug_method_add (w, "twu1"));
14716 /* Display up to a row that can be reused. The variable
14717 last_text_row is set to the last row displayed that displays
14718 text. Note that it.vpos == 0 if or if not there is a
14719 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14720 start_display (&it, w, new_start);
14721 first_row_y = it.current_y;
14722 w->cursor.vpos = -1;
14723 last_text_row = last_reused_text_row = NULL;
14725 while (it.current_y < it.last_visible_y
14726 && !fonts_changed_p)
14728 /* If we have reached into the characters in the START row,
14729 that means the line boundaries have changed. So we
14730 can't start copying with the row START. Maybe it will
14731 work to start copying with the following row. */
14732 while (IT_CHARPOS (it) > CHARPOS (start))
14734 /* Advance to the next row as the "start". */
14735 start_row++;
14736 start = start_row->minpos;
14737 /* If there are no more rows to try, or just one, give up. */
14738 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14739 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14740 || CHARPOS (start) == ZV)
14742 clear_glyph_matrix (w->desired_matrix);
14743 return 0;
14746 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14748 /* If we have reached alignment,
14749 we can copy the rest of the rows. */
14750 if (IT_CHARPOS (it) == CHARPOS (start))
14751 break;
14753 if (display_line (&it))
14754 last_text_row = it.glyph_row - 1;
14757 /* A value of current_y < last_visible_y means that we stopped
14758 at the previous window start, which in turn means that we
14759 have at least one reusable row. */
14760 if (it.current_y < it.last_visible_y)
14762 /* IT.vpos always starts from 0; it counts text lines. */
14763 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14765 /* Find PT if not already found in the lines displayed. */
14766 if (w->cursor.vpos < 0)
14768 int dy = it.current_y - start_row->y;
14770 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14771 row = row_containing_pos (w, PT, row, NULL, dy);
14772 if (row)
14773 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14774 dy, nrows_scrolled);
14775 else
14777 clear_glyph_matrix (w->desired_matrix);
14778 return 0;
14782 /* Scroll the display. Do it before the current matrix is
14783 changed. The problem here is that update has not yet
14784 run, i.e. part of the current matrix is not up to date.
14785 scroll_run_hook will clear the cursor, and use the
14786 current matrix to get the height of the row the cursor is
14787 in. */
14788 run.current_y = start_row->y;
14789 run.desired_y = it.current_y;
14790 run.height = it.last_visible_y - it.current_y;
14792 if (run.height > 0 && run.current_y != run.desired_y)
14794 update_begin (f);
14795 FRAME_RIF (f)->update_window_begin_hook (w);
14796 FRAME_RIF (f)->clear_window_mouse_face (w);
14797 FRAME_RIF (f)->scroll_run_hook (w, &run);
14798 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14799 update_end (f);
14802 /* Shift current matrix down by nrows_scrolled lines. */
14803 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14804 rotate_matrix (w->current_matrix,
14805 start_vpos,
14806 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14807 nrows_scrolled);
14809 /* Disable lines that must be updated. */
14810 for (i = 0; i < nrows_scrolled; ++i)
14811 (start_row + i)->enabled_p = 0;
14813 /* Re-compute Y positions. */
14814 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14815 max_y = it.last_visible_y;
14816 for (row = start_row + nrows_scrolled;
14817 row < bottom_row;
14818 ++row)
14820 row->y = it.current_y;
14821 row->visible_height = row->height;
14823 if (row->y < min_y)
14824 row->visible_height -= min_y - row->y;
14825 if (row->y + row->height > max_y)
14826 row->visible_height -= row->y + row->height - max_y;
14827 row->redraw_fringe_bitmaps_p = 1;
14829 it.current_y += row->height;
14831 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14832 last_reused_text_row = row;
14833 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14834 break;
14837 /* Disable lines in the current matrix which are now
14838 below the window. */
14839 for (++row; row < bottom_row; ++row)
14840 row->enabled_p = row->mode_line_p = 0;
14843 /* Update window_end_pos etc.; last_reused_text_row is the last
14844 reused row from the current matrix containing text, if any.
14845 The value of last_text_row is the last displayed line
14846 containing text. */
14847 if (last_reused_text_row)
14849 w->window_end_bytepos
14850 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14851 w->window_end_pos
14852 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14853 w->window_end_vpos
14854 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14855 w->current_matrix));
14857 else if (last_text_row)
14859 w->window_end_bytepos
14860 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14861 w->window_end_pos
14862 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14863 w->window_end_vpos
14864 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14866 else
14868 /* This window must be completely empty. */
14869 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14870 w->window_end_pos = make_number (Z - ZV);
14871 w->window_end_vpos = make_number (0);
14873 w->window_end_valid = Qnil;
14875 /* Update hint: don't try scrolling again in update_window. */
14876 w->desired_matrix->no_scrolling_p = 1;
14878 #if GLYPH_DEBUG
14879 debug_method_add (w, "try_window_reusing_current_matrix 1");
14880 #endif
14881 return 1;
14883 else if (CHARPOS (new_start) > CHARPOS (start))
14885 struct glyph_row *pt_row, *row;
14886 struct glyph_row *first_reusable_row;
14887 struct glyph_row *first_row_to_display;
14888 int dy;
14889 int yb = window_text_bottom_y (w);
14891 /* Find the row starting at new_start, if there is one. Don't
14892 reuse a partially visible line at the end. */
14893 first_reusable_row = start_row;
14894 while (first_reusable_row->enabled_p
14895 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14896 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14897 < CHARPOS (new_start)))
14898 ++first_reusable_row;
14900 /* Give up if there is no row to reuse. */
14901 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14902 || !first_reusable_row->enabled_p
14903 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14904 != CHARPOS (new_start)))
14905 return 0;
14907 /* We can reuse fully visible rows beginning with
14908 first_reusable_row to the end of the window. Set
14909 first_row_to_display to the first row that cannot be reused.
14910 Set pt_row to the row containing point, if there is any. */
14911 pt_row = NULL;
14912 for (first_row_to_display = first_reusable_row;
14913 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14914 ++first_row_to_display)
14916 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14917 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14918 pt_row = first_row_to_display;
14921 /* Start displaying at the start of first_row_to_display. */
14922 xassert (first_row_to_display->y < yb);
14923 init_to_row_start (&it, w, first_row_to_display);
14925 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14926 - start_vpos);
14927 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14928 - nrows_scrolled);
14929 it.current_y = (first_row_to_display->y - first_reusable_row->y
14930 + WINDOW_HEADER_LINE_HEIGHT (w));
14932 /* Display lines beginning with first_row_to_display in the
14933 desired matrix. Set last_text_row to the last row displayed
14934 that displays text. */
14935 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14936 if (pt_row == NULL)
14937 w->cursor.vpos = -1;
14938 last_text_row = NULL;
14939 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14940 if (display_line (&it))
14941 last_text_row = it.glyph_row - 1;
14943 /* If point is in a reused row, adjust y and vpos of the cursor
14944 position. */
14945 if (pt_row)
14947 w->cursor.vpos -= nrows_scrolled;
14948 w->cursor.y -= first_reusable_row->y - start_row->y;
14951 /* Give up if point isn't in a row displayed or reused. (This
14952 also handles the case where w->cursor.vpos < nrows_scrolled
14953 after the calls to display_line, which can happen with scroll
14954 margins. See bug#1295.) */
14955 if (w->cursor.vpos < 0)
14957 clear_glyph_matrix (w->desired_matrix);
14958 return 0;
14961 /* Scroll the display. */
14962 run.current_y = first_reusable_row->y;
14963 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14964 run.height = it.last_visible_y - run.current_y;
14965 dy = run.current_y - run.desired_y;
14967 if (run.height)
14969 update_begin (f);
14970 FRAME_RIF (f)->update_window_begin_hook (w);
14971 FRAME_RIF (f)->clear_window_mouse_face (w);
14972 FRAME_RIF (f)->scroll_run_hook (w, &run);
14973 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14974 update_end (f);
14977 /* Adjust Y positions of reused rows. */
14978 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14979 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14980 max_y = it.last_visible_y;
14981 for (row = first_reusable_row; row < first_row_to_display; ++row)
14983 row->y -= dy;
14984 row->visible_height = row->height;
14985 if (row->y < min_y)
14986 row->visible_height -= min_y - row->y;
14987 if (row->y + row->height > max_y)
14988 row->visible_height -= row->y + row->height - max_y;
14989 row->redraw_fringe_bitmaps_p = 1;
14992 /* Scroll the current matrix. */
14993 xassert (nrows_scrolled > 0);
14994 rotate_matrix (w->current_matrix,
14995 start_vpos,
14996 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14997 -nrows_scrolled);
14999 /* Disable rows not reused. */
15000 for (row -= nrows_scrolled; row < bottom_row; ++row)
15001 row->enabled_p = 0;
15003 /* Point may have moved to a different line, so we cannot assume that
15004 the previous cursor position is valid; locate the correct row. */
15005 if (pt_row)
15007 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15008 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15009 row++)
15011 w->cursor.vpos++;
15012 w->cursor.y = row->y;
15014 if (row < bottom_row)
15016 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15017 struct glyph *end = glyph + row->used[TEXT_AREA];
15019 /* Can't use this optimization with bidi-reordered glyph
15020 rows, unless cursor is already at point. */
15021 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15023 if (!(w->cursor.hpos >= 0
15024 && w->cursor.hpos < row->used[TEXT_AREA]
15025 && BUFFERP (glyph->object)
15026 && glyph->charpos == PT))
15027 return 0;
15029 else
15030 for (; glyph < end
15031 && (!BUFFERP (glyph->object)
15032 || glyph->charpos < PT);
15033 glyph++)
15035 w->cursor.hpos++;
15036 w->cursor.x += glyph->pixel_width;
15041 /* Adjust window end. A null value of last_text_row means that
15042 the window end is in reused rows which in turn means that
15043 only its vpos can have changed. */
15044 if (last_text_row)
15046 w->window_end_bytepos
15047 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15048 w->window_end_pos
15049 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15050 w->window_end_vpos
15051 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15053 else
15055 w->window_end_vpos
15056 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15059 w->window_end_valid = Qnil;
15060 w->desired_matrix->no_scrolling_p = 1;
15062 #if GLYPH_DEBUG
15063 debug_method_add (w, "try_window_reusing_current_matrix 2");
15064 #endif
15065 return 1;
15068 return 0;
15073 /************************************************************************
15074 Window redisplay reusing current matrix when buffer has changed
15075 ************************************************************************/
15077 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15078 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15079 int *, int *);
15080 static struct glyph_row *
15081 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15082 struct glyph_row *);
15085 /* Return the last row in MATRIX displaying text. If row START is
15086 non-null, start searching with that row. IT gives the dimensions
15087 of the display. Value is null if matrix is empty; otherwise it is
15088 a pointer to the row found. */
15090 static struct glyph_row *
15091 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15092 struct glyph_row *start)
15094 struct glyph_row *row, *row_found;
15096 /* Set row_found to the last row in IT->w's current matrix
15097 displaying text. The loop looks funny but think of partially
15098 visible lines. */
15099 row_found = NULL;
15100 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15101 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15103 xassert (row->enabled_p);
15104 row_found = row;
15105 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15106 break;
15107 ++row;
15110 return row_found;
15114 /* Return the last row in the current matrix of W that is not affected
15115 by changes at the start of current_buffer that occurred since W's
15116 current matrix was built. Value is null if no such row exists.
15118 BEG_UNCHANGED us the number of characters unchanged at the start of
15119 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15120 first changed character in current_buffer. Characters at positions <
15121 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15122 when the current matrix was built. */
15124 static struct glyph_row *
15125 find_last_unchanged_at_beg_row (struct window *w)
15127 int first_changed_pos = BEG + BEG_UNCHANGED;
15128 struct glyph_row *row;
15129 struct glyph_row *row_found = NULL;
15130 int yb = window_text_bottom_y (w);
15132 /* Find the last row displaying unchanged text. */
15133 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15134 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15135 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15136 ++row)
15138 if (/* If row ends before first_changed_pos, it is unchanged,
15139 except in some case. */
15140 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15141 /* When row ends in ZV and we write at ZV it is not
15142 unchanged. */
15143 && !row->ends_at_zv_p
15144 /* When first_changed_pos is the end of a continued line,
15145 row is not unchanged because it may be no longer
15146 continued. */
15147 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15148 && (row->continued_p
15149 || row->exact_window_width_line_p)))
15150 row_found = row;
15152 /* Stop if last visible row. */
15153 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15154 break;
15157 return row_found;
15161 /* Find the first glyph row in the current matrix of W that is not
15162 affected by changes at the end of current_buffer since the
15163 time W's current matrix was built.
15165 Return in *DELTA the number of chars by which buffer positions in
15166 unchanged text at the end of current_buffer must be adjusted.
15168 Return in *DELTA_BYTES the corresponding number of bytes.
15170 Value is null if no such row exists, i.e. all rows are affected by
15171 changes. */
15173 static struct glyph_row *
15174 find_first_unchanged_at_end_row (struct window *w, int *delta, int *delta_bytes)
15176 struct glyph_row *row;
15177 struct glyph_row *row_found = NULL;
15179 *delta = *delta_bytes = 0;
15181 /* Display must not have been paused, otherwise the current matrix
15182 is not up to date. */
15183 eassert (!NILP (w->window_end_valid));
15185 /* A value of window_end_pos >= END_UNCHANGED means that the window
15186 end is in the range of changed text. If so, there is no
15187 unchanged row at the end of W's current matrix. */
15188 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15189 return NULL;
15191 /* Set row to the last row in W's current matrix displaying text. */
15192 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15194 /* If matrix is entirely empty, no unchanged row exists. */
15195 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15197 /* The value of row is the last glyph row in the matrix having a
15198 meaningful buffer position in it. The end position of row
15199 corresponds to window_end_pos. This allows us to translate
15200 buffer positions in the current matrix to current buffer
15201 positions for characters not in changed text. */
15202 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15203 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15204 int last_unchanged_pos, last_unchanged_pos_old;
15205 struct glyph_row *first_text_row
15206 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15208 *delta = Z - Z_old;
15209 *delta_bytes = Z_BYTE - Z_BYTE_old;
15211 /* Set last_unchanged_pos to the buffer position of the last
15212 character in the buffer that has not been changed. Z is the
15213 index + 1 of the last character in current_buffer, i.e. by
15214 subtracting END_UNCHANGED we get the index of the last
15215 unchanged character, and we have to add BEG to get its buffer
15216 position. */
15217 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15218 last_unchanged_pos_old = last_unchanged_pos - *delta;
15220 /* Search backward from ROW for a row displaying a line that
15221 starts at a minimum position >= last_unchanged_pos_old. */
15222 for (; row > first_text_row; --row)
15224 /* This used to abort, but it can happen.
15225 It is ok to just stop the search instead here. KFS. */
15226 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15227 break;
15229 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15230 row_found = row;
15234 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15236 return row_found;
15240 /* Make sure that glyph rows in the current matrix of window W
15241 reference the same glyph memory as corresponding rows in the
15242 frame's frame matrix. This function is called after scrolling W's
15243 current matrix on a terminal frame in try_window_id and
15244 try_window_reusing_current_matrix. */
15246 static void
15247 sync_frame_with_window_matrix_rows (struct window *w)
15249 struct frame *f = XFRAME (w->frame);
15250 struct glyph_row *window_row, *window_row_end, *frame_row;
15252 /* Preconditions: W must be a leaf window and full-width. Its frame
15253 must have a frame matrix. */
15254 xassert (NILP (w->hchild) && NILP (w->vchild));
15255 xassert (WINDOW_FULL_WIDTH_P (w));
15256 xassert (!FRAME_WINDOW_P (f));
15258 /* If W is a full-width window, glyph pointers in W's current matrix
15259 have, by definition, to be the same as glyph pointers in the
15260 corresponding frame matrix. Note that frame matrices have no
15261 marginal areas (see build_frame_matrix). */
15262 window_row = w->current_matrix->rows;
15263 window_row_end = window_row + w->current_matrix->nrows;
15264 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15265 while (window_row < window_row_end)
15267 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15268 struct glyph *end = window_row->glyphs[LAST_AREA];
15270 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15271 frame_row->glyphs[TEXT_AREA] = start;
15272 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15273 frame_row->glyphs[LAST_AREA] = end;
15275 /* Disable frame rows whose corresponding window rows have
15276 been disabled in try_window_id. */
15277 if (!window_row->enabled_p)
15278 frame_row->enabled_p = 0;
15280 ++window_row, ++frame_row;
15285 /* Find the glyph row in window W containing CHARPOS. Consider all
15286 rows between START and END (not inclusive). END null means search
15287 all rows to the end of the display area of W. Value is the row
15288 containing CHARPOS or null. */
15290 struct glyph_row *
15291 row_containing_pos (struct window *w, int charpos, struct glyph_row *start,
15292 struct glyph_row *end, int dy)
15294 struct glyph_row *row = start;
15295 struct glyph_row *best_row = NULL;
15296 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15297 int last_y;
15299 /* If we happen to start on a header-line, skip that. */
15300 if (row->mode_line_p)
15301 ++row;
15303 if ((end && row >= end) || !row->enabled_p)
15304 return NULL;
15306 last_y = window_text_bottom_y (w) - dy;
15308 while (1)
15310 /* Give up if we have gone too far. */
15311 if (end && row >= end)
15312 return NULL;
15313 /* This formerly returned if they were equal.
15314 I think that both quantities are of a "last plus one" type;
15315 if so, when they are equal, the row is within the screen. -- rms. */
15316 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15317 return NULL;
15319 /* If it is in this row, return this row. */
15320 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15321 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15322 /* The end position of a row equals the start
15323 position of the next row. If CHARPOS is there, we
15324 would rather display it in the next line, except
15325 when this line ends in ZV. */
15326 && !row->ends_at_zv_p
15327 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15328 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15330 struct glyph *g;
15332 if (NILP (XBUFFER (w->buffer)->bidi_display_reordering))
15333 return row;
15334 /* In bidi-reordered rows, there could be several rows
15335 occluding point. We need to find the one which fits
15336 CHARPOS the best. */
15337 for (g = row->glyphs[TEXT_AREA];
15338 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15339 g++)
15341 if (!STRINGP (g->object))
15343 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15345 mindif = eabs (g->charpos - charpos);
15346 best_row = row;
15351 else if (best_row)
15352 return best_row;
15353 ++row;
15358 /* Try to redisplay window W by reusing its existing display. W's
15359 current matrix must be up to date when this function is called,
15360 i.e. window_end_valid must not be nil.
15362 Value is
15364 1 if display has been updated
15365 0 if otherwise unsuccessful
15366 -1 if redisplay with same window start is known not to succeed
15368 The following steps are performed:
15370 1. Find the last row in the current matrix of W that is not
15371 affected by changes at the start of current_buffer. If no such row
15372 is found, give up.
15374 2. Find the first row in W's current matrix that is not affected by
15375 changes at the end of current_buffer. Maybe there is no such row.
15377 3. Display lines beginning with the row + 1 found in step 1 to the
15378 row found in step 2 or, if step 2 didn't find a row, to the end of
15379 the window.
15381 4. If cursor is not known to appear on the window, give up.
15383 5. If display stopped at the row found in step 2, scroll the
15384 display and current matrix as needed.
15386 6. Maybe display some lines at the end of W, if we must. This can
15387 happen under various circumstances, like a partially visible line
15388 becoming fully visible, or because newly displayed lines are displayed
15389 in smaller font sizes.
15391 7. Update W's window end information. */
15393 static int
15394 try_window_id (struct window *w)
15396 struct frame *f = XFRAME (w->frame);
15397 struct glyph_matrix *current_matrix = w->current_matrix;
15398 struct glyph_matrix *desired_matrix = w->desired_matrix;
15399 struct glyph_row *last_unchanged_at_beg_row;
15400 struct glyph_row *first_unchanged_at_end_row;
15401 struct glyph_row *row;
15402 struct glyph_row *bottom_row;
15403 int bottom_vpos;
15404 struct it it;
15405 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
15406 struct text_pos start_pos;
15407 struct run run;
15408 int first_unchanged_at_end_vpos = 0;
15409 struct glyph_row *last_text_row, *last_text_row_at_end;
15410 struct text_pos start;
15411 int first_changed_charpos, last_changed_charpos;
15413 #if GLYPH_DEBUG
15414 if (inhibit_try_window_id)
15415 return 0;
15416 #endif
15418 /* This is handy for debugging. */
15419 #if 0
15420 #define GIVE_UP(X) \
15421 do { \
15422 fprintf (stderr, "try_window_id give up %d\n", (X)); \
15423 return 0; \
15424 } while (0)
15425 #else
15426 #define GIVE_UP(X) return 0
15427 #endif
15429 SET_TEXT_POS_FROM_MARKER (start, w->start);
15431 /* Don't use this for mini-windows because these can show
15432 messages and mini-buffers, and we don't handle that here. */
15433 if (MINI_WINDOW_P (w))
15434 GIVE_UP (1);
15436 /* This flag is used to prevent redisplay optimizations. */
15437 if (windows_or_buffers_changed || cursor_type_changed)
15438 GIVE_UP (2);
15440 /* Verify that narrowing has not changed.
15441 Also verify that we were not told to prevent redisplay optimizations.
15442 It would be nice to further
15443 reduce the number of cases where this prevents try_window_id. */
15444 if (current_buffer->clip_changed
15445 || current_buffer->prevent_redisplay_optimizations_p)
15446 GIVE_UP (3);
15448 /* Window must either use window-based redisplay or be full width. */
15449 if (!FRAME_WINDOW_P (f)
15450 && (!FRAME_LINE_INS_DEL_OK (f)
15451 || !WINDOW_FULL_WIDTH_P (w)))
15452 GIVE_UP (4);
15454 /* Give up if point is known NOT to appear in W. */
15455 if (PT < CHARPOS (start))
15456 GIVE_UP (5);
15458 /* Another way to prevent redisplay optimizations. */
15459 if (XFASTINT (w->last_modified) == 0)
15460 GIVE_UP (6);
15462 /* Verify that window is not hscrolled. */
15463 if (XFASTINT (w->hscroll) != 0)
15464 GIVE_UP (7);
15466 /* Verify that display wasn't paused. */
15467 if (NILP (w->window_end_valid))
15468 GIVE_UP (8);
15470 /* Can't use this if highlighting a region because a cursor movement
15471 will do more than just set the cursor. */
15472 if (!NILP (Vtransient_mark_mode)
15473 && !NILP (current_buffer->mark_active))
15474 GIVE_UP (9);
15476 /* Likewise if highlighting trailing whitespace. */
15477 if (!NILP (Vshow_trailing_whitespace))
15478 GIVE_UP (11);
15480 /* Likewise if showing a region. */
15481 if (!NILP (w->region_showing))
15482 GIVE_UP (10);
15484 /* Can't use this if overlay arrow position and/or string have
15485 changed. */
15486 if (overlay_arrows_changed_p ())
15487 GIVE_UP (12);
15489 /* When word-wrap is on, adding a space to the first word of a
15490 wrapped line can change the wrap position, altering the line
15491 above it. It might be worthwhile to handle this more
15492 intelligently, but for now just redisplay from scratch. */
15493 if (!NILP (XBUFFER (w->buffer)->word_wrap))
15494 GIVE_UP (21);
15496 /* Under bidi reordering, adding or deleting a character in the
15497 beginning of a paragraph, before the first strong directional
15498 character, can change the base direction of the paragraph (unless
15499 the buffer specifies a fixed paragraph direction), which will
15500 require to redisplay the whole paragraph. It might be worthwhile
15501 to find the paragraph limits and widen the range of redisplayed
15502 lines to that, but for now just give up this optimization and
15503 redisplay from scratch. */
15504 if (!NILP (XBUFFER (w->buffer)->bidi_display_reordering)
15505 && NILP (XBUFFER (w->buffer)->bidi_paragraph_direction))
15506 GIVE_UP (22);
15508 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
15509 only if buffer has really changed. The reason is that the gap is
15510 initially at Z for freshly visited files. The code below would
15511 set end_unchanged to 0 in that case. */
15512 if (MODIFF > SAVE_MODIFF
15513 /* This seems to happen sometimes after saving a buffer. */
15514 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
15516 if (GPT - BEG < BEG_UNCHANGED)
15517 BEG_UNCHANGED = GPT - BEG;
15518 if (Z - GPT < END_UNCHANGED)
15519 END_UNCHANGED = Z - GPT;
15522 /* The position of the first and last character that has been changed. */
15523 first_changed_charpos = BEG + BEG_UNCHANGED;
15524 last_changed_charpos = Z - END_UNCHANGED;
15526 /* If window starts after a line end, and the last change is in
15527 front of that newline, then changes don't affect the display.
15528 This case happens with stealth-fontification. Note that although
15529 the display is unchanged, glyph positions in the matrix have to
15530 be adjusted, of course. */
15531 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15532 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
15533 && ((last_changed_charpos < CHARPOS (start)
15534 && CHARPOS (start) == BEGV)
15535 || (last_changed_charpos < CHARPOS (start) - 1
15536 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
15538 int Z_old, delta, Z_BYTE_old, delta_bytes;
15539 struct glyph_row *r0;
15541 /* Compute how many chars/bytes have been added to or removed
15542 from the buffer. */
15543 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15544 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15545 delta = Z - Z_old;
15546 delta_bytes = Z_BYTE - Z_BYTE_old;
15548 /* Give up if PT is not in the window. Note that it already has
15549 been checked at the start of try_window_id that PT is not in
15550 front of the window start. */
15551 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
15552 GIVE_UP (13);
15554 /* If window start is unchanged, we can reuse the whole matrix
15555 as is, after adjusting glyph positions. No need to compute
15556 the window end again, since its offset from Z hasn't changed. */
15557 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15558 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
15559 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
15560 /* PT must not be in a partially visible line. */
15561 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
15562 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15564 /* Adjust positions in the glyph matrix. */
15565 if (delta || delta_bytes)
15567 struct glyph_row *r1
15568 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15569 increment_matrix_positions (w->current_matrix,
15570 MATRIX_ROW_VPOS (r0, current_matrix),
15571 MATRIX_ROW_VPOS (r1, current_matrix),
15572 delta, delta_bytes);
15575 /* Set the cursor. */
15576 row = row_containing_pos (w, PT, r0, NULL, 0);
15577 if (row)
15578 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15579 else
15580 abort ();
15581 return 1;
15585 /* Handle the case that changes are all below what is displayed in
15586 the window, and that PT is in the window. This shortcut cannot
15587 be taken if ZV is visible in the window, and text has been added
15588 there that is visible in the window. */
15589 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15590 /* ZV is not visible in the window, or there are no
15591 changes at ZV, actually. */
15592 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15593 || first_changed_charpos == last_changed_charpos))
15595 struct glyph_row *r0;
15597 /* Give up if PT is not in the window. Note that it already has
15598 been checked at the start of try_window_id that PT is not in
15599 front of the window start. */
15600 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15601 GIVE_UP (14);
15603 /* If window start is unchanged, we can reuse the whole matrix
15604 as is, without changing glyph positions since no text has
15605 been added/removed in front of the window end. */
15606 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15607 if (TEXT_POS_EQUAL_P (start, r0->minpos)
15608 /* PT must not be in a partially visible line. */
15609 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15610 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15612 /* We have to compute the window end anew since text
15613 could have been added/removed after it. */
15614 w->window_end_pos
15615 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15616 w->window_end_bytepos
15617 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15619 /* Set the cursor. */
15620 row = row_containing_pos (w, PT, r0, NULL, 0);
15621 if (row)
15622 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15623 else
15624 abort ();
15625 return 2;
15629 /* Give up if window start is in the changed area.
15631 The condition used to read
15633 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15635 but why that was tested escapes me at the moment. */
15636 if (CHARPOS (start) >= first_changed_charpos
15637 && CHARPOS (start) <= last_changed_charpos)
15638 GIVE_UP (15);
15640 /* Check that window start agrees with the start of the first glyph
15641 row in its current matrix. Check this after we know the window
15642 start is not in changed text, otherwise positions would not be
15643 comparable. */
15644 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15645 if (!TEXT_POS_EQUAL_P (start, row->minpos))
15646 GIVE_UP (16);
15648 /* Give up if the window ends in strings. Overlay strings
15649 at the end are difficult to handle, so don't try. */
15650 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15651 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15652 GIVE_UP (20);
15654 /* Compute the position at which we have to start displaying new
15655 lines. Some of the lines at the top of the window might be
15656 reusable because they are not displaying changed text. Find the
15657 last row in W's current matrix not affected by changes at the
15658 start of current_buffer. Value is null if changes start in the
15659 first line of window. */
15660 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15661 if (last_unchanged_at_beg_row)
15663 /* Avoid starting to display in the moddle of a character, a TAB
15664 for instance. This is easier than to set up the iterator
15665 exactly, and it's not a frequent case, so the additional
15666 effort wouldn't really pay off. */
15667 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15668 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15669 && last_unchanged_at_beg_row > w->current_matrix->rows)
15670 --last_unchanged_at_beg_row;
15672 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15673 GIVE_UP (17);
15675 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15676 GIVE_UP (18);
15677 start_pos = it.current.pos;
15679 /* Start displaying new lines in the desired matrix at the same
15680 vpos we would use in the current matrix, i.e. below
15681 last_unchanged_at_beg_row. */
15682 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15683 current_matrix);
15684 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15685 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15687 xassert (it.hpos == 0 && it.current_x == 0);
15689 else
15691 /* There are no reusable lines at the start of the window.
15692 Start displaying in the first text line. */
15693 start_display (&it, w, start);
15694 it.vpos = it.first_vpos;
15695 start_pos = it.current.pos;
15698 /* Find the first row that is not affected by changes at the end of
15699 the buffer. Value will be null if there is no unchanged row, in
15700 which case we must redisplay to the end of the window. delta
15701 will be set to the value by which buffer positions beginning with
15702 first_unchanged_at_end_row have to be adjusted due to text
15703 changes. */
15704 first_unchanged_at_end_row
15705 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15706 IF_DEBUG (debug_delta = delta);
15707 IF_DEBUG (debug_delta_bytes = delta_bytes);
15709 /* Set stop_pos to the buffer position up to which we will have to
15710 display new lines. If first_unchanged_at_end_row != NULL, this
15711 is the buffer position of the start of the line displayed in that
15712 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15713 that we don't stop at a buffer position. */
15714 stop_pos = 0;
15715 if (first_unchanged_at_end_row)
15717 xassert (last_unchanged_at_beg_row == NULL
15718 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15720 /* If this is a continuation line, move forward to the next one
15721 that isn't. Changes in lines above affect this line.
15722 Caution: this may move first_unchanged_at_end_row to a row
15723 not displaying text. */
15724 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15725 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15726 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15727 < it.last_visible_y))
15728 ++first_unchanged_at_end_row;
15730 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15731 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15732 >= it.last_visible_y))
15733 first_unchanged_at_end_row = NULL;
15734 else
15736 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15737 + delta);
15738 first_unchanged_at_end_vpos
15739 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15740 xassert (stop_pos >= Z - END_UNCHANGED);
15743 else if (last_unchanged_at_beg_row == NULL)
15744 GIVE_UP (19);
15747 #if GLYPH_DEBUG
15749 /* Either there is no unchanged row at the end, or the one we have
15750 now displays text. This is a necessary condition for the window
15751 end pos calculation at the end of this function. */
15752 xassert (first_unchanged_at_end_row == NULL
15753 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15755 debug_last_unchanged_at_beg_vpos
15756 = (last_unchanged_at_beg_row
15757 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15758 : -1);
15759 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15761 #endif /* GLYPH_DEBUG != 0 */
15764 /* Display new lines. Set last_text_row to the last new line
15765 displayed which has text on it, i.e. might end up as being the
15766 line where the window_end_vpos is. */
15767 w->cursor.vpos = -1;
15768 last_text_row = NULL;
15769 overlay_arrow_seen = 0;
15770 while (it.current_y < it.last_visible_y
15771 && !fonts_changed_p
15772 && (first_unchanged_at_end_row == NULL
15773 || IT_CHARPOS (it) < stop_pos))
15775 if (display_line (&it))
15776 last_text_row = it.glyph_row - 1;
15779 if (fonts_changed_p)
15780 return -1;
15783 /* Compute differences in buffer positions, y-positions etc. for
15784 lines reused at the bottom of the window. Compute what we can
15785 scroll. */
15786 if (first_unchanged_at_end_row
15787 /* No lines reused because we displayed everything up to the
15788 bottom of the window. */
15789 && it.current_y < it.last_visible_y)
15791 dvpos = (it.vpos
15792 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15793 current_matrix));
15794 dy = it.current_y - first_unchanged_at_end_row->y;
15795 run.current_y = first_unchanged_at_end_row->y;
15796 run.desired_y = run.current_y + dy;
15797 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15799 else
15801 delta = delta_bytes = dvpos = dy
15802 = run.current_y = run.desired_y = run.height = 0;
15803 first_unchanged_at_end_row = NULL;
15805 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15808 /* Find the cursor if not already found. We have to decide whether
15809 PT will appear on this window (it sometimes doesn't, but this is
15810 not a very frequent case.) This decision has to be made before
15811 the current matrix is altered. A value of cursor.vpos < 0 means
15812 that PT is either in one of the lines beginning at
15813 first_unchanged_at_end_row or below the window. Don't care for
15814 lines that might be displayed later at the window end; as
15815 mentioned, this is not a frequent case. */
15816 if (w->cursor.vpos < 0)
15818 /* Cursor in unchanged rows at the top? */
15819 if (PT < CHARPOS (start_pos)
15820 && last_unchanged_at_beg_row)
15822 row = row_containing_pos (w, PT,
15823 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15824 last_unchanged_at_beg_row + 1, 0);
15825 if (row)
15826 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15829 /* Start from first_unchanged_at_end_row looking for PT. */
15830 else if (first_unchanged_at_end_row)
15832 row = row_containing_pos (w, PT - delta,
15833 first_unchanged_at_end_row, NULL, 0);
15834 if (row)
15835 set_cursor_from_row (w, row, w->current_matrix, delta,
15836 delta_bytes, dy, dvpos);
15839 /* Give up if cursor was not found. */
15840 if (w->cursor.vpos < 0)
15842 clear_glyph_matrix (w->desired_matrix);
15843 return -1;
15847 /* Don't let the cursor end in the scroll margins. */
15849 int this_scroll_margin, cursor_height;
15851 this_scroll_margin = max (0, scroll_margin);
15852 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15853 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15854 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15856 if ((w->cursor.y < this_scroll_margin
15857 && CHARPOS (start) > BEGV)
15858 /* Old redisplay didn't take scroll margin into account at the bottom,
15859 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15860 || (w->cursor.y + (make_cursor_line_fully_visible_p
15861 ? cursor_height + this_scroll_margin
15862 : 1)) > it.last_visible_y)
15864 w->cursor.vpos = -1;
15865 clear_glyph_matrix (w->desired_matrix);
15866 return -1;
15870 /* Scroll the display. Do it before changing the current matrix so
15871 that xterm.c doesn't get confused about where the cursor glyph is
15872 found. */
15873 if (dy && run.height)
15875 update_begin (f);
15877 if (FRAME_WINDOW_P (f))
15879 FRAME_RIF (f)->update_window_begin_hook (w);
15880 FRAME_RIF (f)->clear_window_mouse_face (w);
15881 FRAME_RIF (f)->scroll_run_hook (w, &run);
15882 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15884 else
15886 /* Terminal frame. In this case, dvpos gives the number of
15887 lines to scroll by; dvpos < 0 means scroll up. */
15888 int first_unchanged_at_end_vpos
15889 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15890 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15891 int end = (WINDOW_TOP_EDGE_LINE (w)
15892 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15893 + window_internal_height (w));
15895 /* Perform the operation on the screen. */
15896 if (dvpos > 0)
15898 /* Scroll last_unchanged_at_beg_row to the end of the
15899 window down dvpos lines. */
15900 set_terminal_window (f, end);
15902 /* On dumb terminals delete dvpos lines at the end
15903 before inserting dvpos empty lines. */
15904 if (!FRAME_SCROLL_REGION_OK (f))
15905 ins_del_lines (f, end - dvpos, -dvpos);
15907 /* Insert dvpos empty lines in front of
15908 last_unchanged_at_beg_row. */
15909 ins_del_lines (f, from, dvpos);
15911 else if (dvpos < 0)
15913 /* Scroll up last_unchanged_at_beg_vpos to the end of
15914 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15915 set_terminal_window (f, end);
15917 /* Delete dvpos lines in front of
15918 last_unchanged_at_beg_vpos. ins_del_lines will set
15919 the cursor to the given vpos and emit |dvpos| delete
15920 line sequences. */
15921 ins_del_lines (f, from + dvpos, dvpos);
15923 /* On a dumb terminal insert dvpos empty lines at the
15924 end. */
15925 if (!FRAME_SCROLL_REGION_OK (f))
15926 ins_del_lines (f, end + dvpos, -dvpos);
15929 set_terminal_window (f, 0);
15932 update_end (f);
15935 /* Shift reused rows of the current matrix to the right position.
15936 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15937 text. */
15938 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15939 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15940 if (dvpos < 0)
15942 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15943 bottom_vpos, dvpos);
15944 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15945 bottom_vpos, 0);
15947 else if (dvpos > 0)
15949 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15950 bottom_vpos, dvpos);
15951 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15952 first_unchanged_at_end_vpos + dvpos, 0);
15955 /* For frame-based redisplay, make sure that current frame and window
15956 matrix are in sync with respect to glyph memory. */
15957 if (!FRAME_WINDOW_P (f))
15958 sync_frame_with_window_matrix_rows (w);
15960 /* Adjust buffer positions in reused rows. */
15961 if (delta || delta_bytes)
15962 increment_matrix_positions (current_matrix,
15963 first_unchanged_at_end_vpos + dvpos,
15964 bottom_vpos, delta, delta_bytes);
15966 /* Adjust Y positions. */
15967 if (dy)
15968 shift_glyph_matrix (w, current_matrix,
15969 first_unchanged_at_end_vpos + dvpos,
15970 bottom_vpos, dy);
15972 if (first_unchanged_at_end_row)
15974 first_unchanged_at_end_row += dvpos;
15975 if (first_unchanged_at_end_row->y >= it.last_visible_y
15976 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15977 first_unchanged_at_end_row = NULL;
15980 /* If scrolling up, there may be some lines to display at the end of
15981 the window. */
15982 last_text_row_at_end = NULL;
15983 if (dy < 0)
15985 /* Scrolling up can leave for example a partially visible line
15986 at the end of the window to be redisplayed. */
15987 /* Set last_row to the glyph row in the current matrix where the
15988 window end line is found. It has been moved up or down in
15989 the matrix by dvpos. */
15990 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15991 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15993 /* If last_row is the window end line, it should display text. */
15994 xassert (last_row->displays_text_p);
15996 /* If window end line was partially visible before, begin
15997 displaying at that line. Otherwise begin displaying with the
15998 line following it. */
15999 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16001 init_to_row_start (&it, w, last_row);
16002 it.vpos = last_vpos;
16003 it.current_y = last_row->y;
16005 else
16007 init_to_row_end (&it, w, last_row);
16008 it.vpos = 1 + last_vpos;
16009 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16010 ++last_row;
16013 /* We may start in a continuation line. If so, we have to
16014 get the right continuation_lines_width and current_x. */
16015 it.continuation_lines_width = last_row->continuation_lines_width;
16016 it.hpos = it.current_x = 0;
16018 /* Display the rest of the lines at the window end. */
16019 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16020 while (it.current_y < it.last_visible_y
16021 && !fonts_changed_p)
16023 /* Is it always sure that the display agrees with lines in
16024 the current matrix? I don't think so, so we mark rows
16025 displayed invalid in the current matrix by setting their
16026 enabled_p flag to zero. */
16027 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16028 if (display_line (&it))
16029 last_text_row_at_end = it.glyph_row - 1;
16033 /* Update window_end_pos and window_end_vpos. */
16034 if (first_unchanged_at_end_row
16035 && !last_text_row_at_end)
16037 /* Window end line if one of the preserved rows from the current
16038 matrix. Set row to the last row displaying text in current
16039 matrix starting at first_unchanged_at_end_row, after
16040 scrolling. */
16041 xassert (first_unchanged_at_end_row->displays_text_p);
16042 row = find_last_row_displaying_text (w->current_matrix, &it,
16043 first_unchanged_at_end_row);
16044 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16046 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16047 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16048 w->window_end_vpos
16049 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16050 xassert (w->window_end_bytepos >= 0);
16051 IF_DEBUG (debug_method_add (w, "A"));
16053 else if (last_text_row_at_end)
16055 w->window_end_pos
16056 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16057 w->window_end_bytepos
16058 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16059 w->window_end_vpos
16060 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16061 xassert (w->window_end_bytepos >= 0);
16062 IF_DEBUG (debug_method_add (w, "B"));
16064 else if (last_text_row)
16066 /* We have displayed either to the end of the window or at the
16067 end of the window, i.e. the last row with text is to be found
16068 in the desired matrix. */
16069 w->window_end_pos
16070 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16071 w->window_end_bytepos
16072 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16073 w->window_end_vpos
16074 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16075 xassert (w->window_end_bytepos >= 0);
16077 else if (first_unchanged_at_end_row == NULL
16078 && last_text_row == NULL
16079 && last_text_row_at_end == NULL)
16081 /* Displayed to end of window, but no line containing text was
16082 displayed. Lines were deleted at the end of the window. */
16083 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16084 int vpos = XFASTINT (w->window_end_vpos);
16085 struct glyph_row *current_row = current_matrix->rows + vpos;
16086 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16088 for (row = NULL;
16089 row == NULL && vpos >= first_vpos;
16090 --vpos, --current_row, --desired_row)
16092 if (desired_row->enabled_p)
16094 if (desired_row->displays_text_p)
16095 row = desired_row;
16097 else if (current_row->displays_text_p)
16098 row = current_row;
16101 xassert (row != NULL);
16102 w->window_end_vpos = make_number (vpos + 1);
16103 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16104 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16105 xassert (w->window_end_bytepos >= 0);
16106 IF_DEBUG (debug_method_add (w, "C"));
16108 else
16109 abort ();
16111 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16112 debug_end_vpos = XFASTINT (w->window_end_vpos));
16114 /* Record that display has not been completed. */
16115 w->window_end_valid = Qnil;
16116 w->desired_matrix->no_scrolling_p = 1;
16117 return 3;
16119 #undef GIVE_UP
16124 /***********************************************************************
16125 More debugging support
16126 ***********************************************************************/
16128 #if GLYPH_DEBUG
16130 void dump_glyph_row (struct glyph_row *, int, int);
16131 void dump_glyph_matrix (struct glyph_matrix *, int);
16132 void dump_glyph (struct glyph_row *, struct glyph *, int);
16135 /* Dump the contents of glyph matrix MATRIX on stderr.
16137 GLYPHS 0 means don't show glyph contents.
16138 GLYPHS 1 means show glyphs in short form
16139 GLYPHS > 1 means show glyphs in long form. */
16141 void
16142 dump_glyph_matrix (matrix, glyphs)
16143 struct glyph_matrix *matrix;
16144 int glyphs;
16146 int i;
16147 for (i = 0; i < matrix->nrows; ++i)
16148 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16152 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16153 the glyph row and area where the glyph comes from. */
16155 void
16156 dump_glyph (row, glyph, area)
16157 struct glyph_row *row;
16158 struct glyph *glyph;
16159 int area;
16161 if (glyph->type == CHAR_GLYPH)
16163 fprintf (stderr,
16164 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16165 glyph - row->glyphs[TEXT_AREA],
16166 'C',
16167 glyph->charpos,
16168 (BUFFERP (glyph->object)
16169 ? 'B'
16170 : (STRINGP (glyph->object)
16171 ? 'S'
16172 : '-')),
16173 glyph->pixel_width,
16174 glyph->u.ch,
16175 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16176 ? glyph->u.ch
16177 : '.'),
16178 glyph->face_id,
16179 glyph->left_box_line_p,
16180 glyph->right_box_line_p);
16182 else if (glyph->type == STRETCH_GLYPH)
16184 fprintf (stderr,
16185 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16186 glyph - row->glyphs[TEXT_AREA],
16187 'S',
16188 glyph->charpos,
16189 (BUFFERP (glyph->object)
16190 ? 'B'
16191 : (STRINGP (glyph->object)
16192 ? 'S'
16193 : '-')),
16194 glyph->pixel_width,
16196 '.',
16197 glyph->face_id,
16198 glyph->left_box_line_p,
16199 glyph->right_box_line_p);
16201 else if (glyph->type == IMAGE_GLYPH)
16203 fprintf (stderr,
16204 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16205 glyph - row->glyphs[TEXT_AREA],
16206 'I',
16207 glyph->charpos,
16208 (BUFFERP (glyph->object)
16209 ? 'B'
16210 : (STRINGP (glyph->object)
16211 ? 'S'
16212 : '-')),
16213 glyph->pixel_width,
16214 glyph->u.img_id,
16215 '.',
16216 glyph->face_id,
16217 glyph->left_box_line_p,
16218 glyph->right_box_line_p);
16220 else if (glyph->type == COMPOSITE_GLYPH)
16222 fprintf (stderr,
16223 " %5d %4c %6d %c %3d 0x%05x",
16224 glyph - row->glyphs[TEXT_AREA],
16225 '+',
16226 glyph->charpos,
16227 (BUFFERP (glyph->object)
16228 ? 'B'
16229 : (STRINGP (glyph->object)
16230 ? 'S'
16231 : '-')),
16232 glyph->pixel_width,
16233 glyph->u.cmp.id);
16234 if (glyph->u.cmp.automatic)
16235 fprintf (stderr,
16236 "[%d-%d]",
16237 glyph->u.cmp.from, glyph->u.cmp.to);
16238 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16239 glyph->face_id,
16240 glyph->left_box_line_p,
16241 glyph->right_box_line_p);
16246 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16247 GLYPHS 0 means don't show glyph contents.
16248 GLYPHS 1 means show glyphs in short form
16249 GLYPHS > 1 means show glyphs in long form. */
16251 void
16252 dump_glyph_row (row, vpos, glyphs)
16253 struct glyph_row *row;
16254 int vpos, glyphs;
16256 if (glyphs != 1)
16258 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16259 fprintf (stderr, "======================================================================\n");
16261 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16262 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16263 vpos,
16264 MATRIX_ROW_START_CHARPOS (row),
16265 MATRIX_ROW_END_CHARPOS (row),
16266 row->used[TEXT_AREA],
16267 row->contains_overlapping_glyphs_p,
16268 row->enabled_p,
16269 row->truncated_on_left_p,
16270 row->truncated_on_right_p,
16271 row->continued_p,
16272 MATRIX_ROW_CONTINUATION_LINE_P (row),
16273 row->displays_text_p,
16274 row->ends_at_zv_p,
16275 row->fill_line_p,
16276 row->ends_in_middle_of_char_p,
16277 row->starts_in_middle_of_char_p,
16278 row->mouse_face_p,
16279 row->x,
16280 row->y,
16281 row->pixel_width,
16282 row->height,
16283 row->visible_height,
16284 row->ascent,
16285 row->phys_ascent);
16286 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16287 row->end.overlay_string_index,
16288 row->continuation_lines_width);
16289 fprintf (stderr, "%9d %5d\n",
16290 CHARPOS (row->start.string_pos),
16291 CHARPOS (row->end.string_pos));
16292 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16293 row->end.dpvec_index);
16296 if (glyphs > 1)
16298 int area;
16300 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16302 struct glyph *glyph = row->glyphs[area];
16303 struct glyph *glyph_end = glyph + row->used[area];
16305 /* Glyph for a line end in text. */
16306 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16307 ++glyph_end;
16309 if (glyph < glyph_end)
16310 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16312 for (; glyph < glyph_end; ++glyph)
16313 dump_glyph (row, glyph, area);
16316 else if (glyphs == 1)
16318 int area;
16320 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16322 char *s = (char *) alloca (row->used[area] + 1);
16323 int i;
16325 for (i = 0; i < row->used[area]; ++i)
16327 struct glyph *glyph = row->glyphs[area] + i;
16328 if (glyph->type == CHAR_GLYPH
16329 && glyph->u.ch < 0x80
16330 && glyph->u.ch >= ' ')
16331 s[i] = glyph->u.ch;
16332 else
16333 s[i] = '.';
16336 s[i] = '\0';
16337 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16343 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16344 Sdump_glyph_matrix, 0, 1, "p",
16345 doc: /* Dump the current matrix of the selected window to stderr.
16346 Shows contents of glyph row structures. With non-nil
16347 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16348 glyphs in short form, otherwise show glyphs in long form. */)
16349 (Lisp_Object glyphs)
16351 struct window *w = XWINDOW (selected_window);
16352 struct buffer *buffer = XBUFFER (w->buffer);
16354 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
16355 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
16356 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
16357 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
16358 fprintf (stderr, "=============================================\n");
16359 dump_glyph_matrix (w->current_matrix,
16360 NILP (glyphs) ? 0 : XINT (glyphs));
16361 return Qnil;
16365 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
16366 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
16367 (void)
16369 struct frame *f = XFRAME (selected_frame);
16370 dump_glyph_matrix (f->current_matrix, 1);
16371 return Qnil;
16375 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
16376 doc: /* Dump glyph row ROW to stderr.
16377 GLYPH 0 means don't dump glyphs.
16378 GLYPH 1 means dump glyphs in short form.
16379 GLYPH > 1 or omitted means dump glyphs in long form. */)
16380 (Lisp_Object row, Lisp_Object glyphs)
16382 struct glyph_matrix *matrix;
16383 int vpos;
16385 CHECK_NUMBER (row);
16386 matrix = XWINDOW (selected_window)->current_matrix;
16387 vpos = XINT (row);
16388 if (vpos >= 0 && vpos < matrix->nrows)
16389 dump_glyph_row (MATRIX_ROW (matrix, vpos),
16390 vpos,
16391 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16392 return Qnil;
16396 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
16397 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
16398 GLYPH 0 means don't dump glyphs.
16399 GLYPH 1 means dump glyphs in short form.
16400 GLYPH > 1 or omitted means dump glyphs in long form. */)
16401 (Lisp_Object row, Lisp_Object glyphs)
16403 struct frame *sf = SELECTED_FRAME ();
16404 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
16405 int vpos;
16407 CHECK_NUMBER (row);
16408 vpos = XINT (row);
16409 if (vpos >= 0 && vpos < m->nrows)
16410 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
16411 INTEGERP (glyphs) ? XINT (glyphs) : 2);
16412 return Qnil;
16416 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
16417 doc: /* Toggle tracing of redisplay.
16418 With ARG, turn tracing on if and only if ARG is positive. */)
16419 (Lisp_Object arg)
16421 if (NILP (arg))
16422 trace_redisplay_p = !trace_redisplay_p;
16423 else
16425 arg = Fprefix_numeric_value (arg);
16426 trace_redisplay_p = XINT (arg) > 0;
16429 return Qnil;
16433 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
16434 doc: /* Like `format', but print result to stderr.
16435 usage: (trace-to-stderr STRING &rest OBJECTS) */)
16436 (int nargs, Lisp_Object *args)
16438 Lisp_Object s = Fformat (nargs, args);
16439 fprintf (stderr, "%s", SDATA (s));
16440 return Qnil;
16443 #endif /* GLYPH_DEBUG */
16447 /***********************************************************************
16448 Building Desired Matrix Rows
16449 ***********************************************************************/
16451 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
16452 Used for non-window-redisplay windows, and for windows w/o left fringe. */
16454 static struct glyph_row *
16455 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
16457 struct frame *f = XFRAME (WINDOW_FRAME (w));
16458 struct buffer *buffer = XBUFFER (w->buffer);
16459 struct buffer *old = current_buffer;
16460 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
16461 int arrow_len = SCHARS (overlay_arrow_string);
16462 const unsigned char *arrow_end = arrow_string + arrow_len;
16463 const unsigned char *p;
16464 struct it it;
16465 int multibyte_p;
16466 int n_glyphs_before;
16468 set_buffer_temp (buffer);
16469 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
16470 it.glyph_row->used[TEXT_AREA] = 0;
16471 SET_TEXT_POS (it.position, 0, 0);
16473 multibyte_p = !NILP (buffer->enable_multibyte_characters);
16474 p = arrow_string;
16475 while (p < arrow_end)
16477 Lisp_Object face, ilisp;
16479 /* Get the next character. */
16480 if (multibyte_p)
16481 it.c = string_char_and_length (p, &it.len);
16482 else
16483 it.c = *p, it.len = 1;
16484 p += it.len;
16486 /* Get its face. */
16487 ilisp = make_number (p - arrow_string);
16488 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
16489 it.face_id = compute_char_face (f, it.c, face);
16491 /* Compute its width, get its glyphs. */
16492 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
16493 SET_TEXT_POS (it.position, -1, -1);
16494 PRODUCE_GLYPHS (&it);
16496 /* If this character doesn't fit any more in the line, we have
16497 to remove some glyphs. */
16498 if (it.current_x > it.last_visible_x)
16500 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
16501 break;
16505 set_buffer_temp (old);
16506 return it.glyph_row;
16510 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
16511 glyphs are only inserted for terminal frames since we can't really
16512 win with truncation glyphs when partially visible glyphs are
16513 involved. Which glyphs to insert is determined by
16514 produce_special_glyphs. */
16516 static void
16517 insert_left_trunc_glyphs (struct it *it)
16519 struct it truncate_it;
16520 struct glyph *from, *end, *to, *toend;
16522 xassert (!FRAME_WINDOW_P (it->f));
16524 /* Get the truncation glyphs. */
16525 truncate_it = *it;
16526 truncate_it.current_x = 0;
16527 truncate_it.face_id = DEFAULT_FACE_ID;
16528 truncate_it.glyph_row = &scratch_glyph_row;
16529 truncate_it.glyph_row->used[TEXT_AREA] = 0;
16530 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
16531 truncate_it.object = make_number (0);
16532 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
16534 /* Overwrite glyphs from IT with truncation glyphs. */
16535 if (!it->glyph_row->reversed_p)
16537 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16538 end = from + truncate_it.glyph_row->used[TEXT_AREA];
16539 to = it->glyph_row->glyphs[TEXT_AREA];
16540 toend = to + it->glyph_row->used[TEXT_AREA];
16542 while (from < end)
16543 *to++ = *from++;
16545 /* There may be padding glyphs left over. Overwrite them too. */
16546 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
16548 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
16549 while (from < end)
16550 *to++ = *from++;
16553 if (to > toend)
16554 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
16556 else
16558 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
16559 that back to front. */
16560 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
16561 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16562 toend = it->glyph_row->glyphs[TEXT_AREA];
16563 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
16565 while (from >= end && to >= toend)
16566 *to-- = *from--;
16567 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
16569 from =
16570 truncate_it.glyph_row->glyphs[TEXT_AREA]
16571 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
16572 while (from >= end && to >= toend)
16573 *to-- = *from--;
16575 if (from >= end)
16577 /* Need to free some room before prepending additional
16578 glyphs. */
16579 int move_by = from - end + 1;
16580 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
16581 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
16583 for ( ; g >= g0; g--)
16584 g[move_by] = *g;
16585 while (from >= end)
16586 *to-- = *from--;
16587 it->glyph_row->used[TEXT_AREA] += move_by;
16593 /* Compute the pixel height and width of IT->glyph_row.
16595 Most of the time, ascent and height of a display line will be equal
16596 to the max_ascent and max_height values of the display iterator
16597 structure. This is not the case if
16599 1. We hit ZV without displaying anything. In this case, max_ascent
16600 and max_height will be zero.
16602 2. We have some glyphs that don't contribute to the line height.
16603 (The glyph row flag contributes_to_line_height_p is for future
16604 pixmap extensions).
16606 The first case is easily covered by using default values because in
16607 these cases, the line height does not really matter, except that it
16608 must not be zero. */
16610 static void
16611 compute_line_metrics (struct it *it)
16613 struct glyph_row *row = it->glyph_row;
16614 int area, i;
16616 if (FRAME_WINDOW_P (it->f))
16618 int i, min_y, max_y;
16620 /* The line may consist of one space only, that was added to
16621 place the cursor on it. If so, the row's height hasn't been
16622 computed yet. */
16623 if (row->height == 0)
16625 if (it->max_ascent + it->max_descent == 0)
16626 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16627 row->ascent = it->max_ascent;
16628 row->height = it->max_ascent + it->max_descent;
16629 row->phys_ascent = it->max_phys_ascent;
16630 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16631 row->extra_line_spacing = it->max_extra_line_spacing;
16634 /* Compute the width of this line. */
16635 row->pixel_width = row->x;
16636 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16637 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16639 xassert (row->pixel_width >= 0);
16640 xassert (row->ascent >= 0 && row->height > 0);
16642 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16643 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16645 /* If first line's physical ascent is larger than its logical
16646 ascent, use the physical ascent, and make the row taller.
16647 This makes accented characters fully visible. */
16648 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16649 && row->phys_ascent > row->ascent)
16651 row->height += row->phys_ascent - row->ascent;
16652 row->ascent = row->phys_ascent;
16655 /* Compute how much of the line is visible. */
16656 row->visible_height = row->height;
16658 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16659 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16661 if (row->y < min_y)
16662 row->visible_height -= min_y - row->y;
16663 if (row->y + row->height > max_y)
16664 row->visible_height -= row->y + row->height - max_y;
16666 else
16668 row->pixel_width = row->used[TEXT_AREA];
16669 if (row->continued_p)
16670 row->pixel_width -= it->continuation_pixel_width;
16671 else if (row->truncated_on_right_p)
16672 row->pixel_width -= it->truncation_pixel_width;
16673 row->ascent = row->phys_ascent = 0;
16674 row->height = row->phys_height = row->visible_height = 1;
16675 row->extra_line_spacing = 0;
16678 /* Compute a hash code for this row. */
16679 row->hash = 0;
16680 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16681 for (i = 0; i < row->used[area]; ++i)
16682 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16683 + row->glyphs[area][i].u.val
16684 + row->glyphs[area][i].face_id
16685 + row->glyphs[area][i].padding_p
16686 + (row->glyphs[area][i].type << 2));
16688 it->max_ascent = it->max_descent = 0;
16689 it->max_phys_ascent = it->max_phys_descent = 0;
16693 /* Append one space to the glyph row of iterator IT if doing a
16694 window-based redisplay. The space has the same face as
16695 IT->face_id. Value is non-zero if a space was added.
16697 This function is called to make sure that there is always one glyph
16698 at the end of a glyph row that the cursor can be set on under
16699 window-systems. (If there weren't such a glyph we would not know
16700 how wide and tall a box cursor should be displayed).
16702 At the same time this space let's a nicely handle clearing to the
16703 end of the line if the row ends in italic text. */
16705 static int
16706 append_space_for_newline (struct it *it, int default_face_p)
16708 if (FRAME_WINDOW_P (it->f))
16710 int n = it->glyph_row->used[TEXT_AREA];
16712 if (it->glyph_row->glyphs[TEXT_AREA] + n
16713 < it->glyph_row->glyphs[1 + TEXT_AREA])
16715 /* Save some values that must not be changed.
16716 Must save IT->c and IT->len because otherwise
16717 ITERATOR_AT_END_P wouldn't work anymore after
16718 append_space_for_newline has been called. */
16719 enum display_element_type saved_what = it->what;
16720 int saved_c = it->c, saved_len = it->len;
16721 int saved_x = it->current_x;
16722 int saved_face_id = it->face_id;
16723 struct text_pos saved_pos;
16724 Lisp_Object saved_object;
16725 struct face *face;
16727 saved_object = it->object;
16728 saved_pos = it->position;
16730 it->what = IT_CHARACTER;
16731 memset (&it->position, 0, sizeof it->position);
16732 it->object = make_number (0);
16733 it->c = ' ';
16734 it->len = 1;
16736 if (default_face_p)
16737 it->face_id = DEFAULT_FACE_ID;
16738 else if (it->face_before_selective_p)
16739 it->face_id = it->saved_face_id;
16740 face = FACE_FROM_ID (it->f, it->face_id);
16741 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16743 PRODUCE_GLYPHS (it);
16745 it->override_ascent = -1;
16746 it->constrain_row_ascent_descent_p = 0;
16747 it->current_x = saved_x;
16748 it->object = saved_object;
16749 it->position = saved_pos;
16750 it->what = saved_what;
16751 it->face_id = saved_face_id;
16752 it->len = saved_len;
16753 it->c = saved_c;
16754 return 1;
16758 return 0;
16762 /* Extend the face of the last glyph in the text area of IT->glyph_row
16763 to the end of the display line. Called from display_line. If the
16764 glyph row is empty, add a space glyph to it so that we know the
16765 face to draw. Set the glyph row flag fill_line_p. If the glyph
16766 row is R2L, prepend a stretch glyph to cover the empty space to the
16767 left of the leftmost glyph. */
16769 static void
16770 extend_face_to_end_of_line (struct it *it)
16772 struct face *face;
16773 struct frame *f = it->f;
16775 /* If line is already filled, do nothing. Non window-system frames
16776 get a grace of one more ``pixel'' because their characters are
16777 1-``pixel'' wide, so they hit the equality too early. This grace
16778 is needed only for R2L rows that are not continued, to produce
16779 one extra blank where we could display the cursor. */
16780 if (it->current_x >= it->last_visible_x
16781 + (!FRAME_WINDOW_P (f)
16782 && it->glyph_row->reversed_p
16783 && !it->glyph_row->continued_p))
16784 return;
16786 /* Face extension extends the background and box of IT->face_id
16787 to the end of the line. If the background equals the background
16788 of the frame, we don't have to do anything. */
16789 if (it->face_before_selective_p)
16790 face = FACE_FROM_ID (f, it->saved_face_id);
16791 else
16792 face = FACE_FROM_ID (f, it->face_id);
16794 if (FRAME_WINDOW_P (f)
16795 && it->glyph_row->displays_text_p
16796 && face->box == FACE_NO_BOX
16797 && face->background == FRAME_BACKGROUND_PIXEL (f)
16798 && !face->stipple
16799 && !it->glyph_row->reversed_p)
16800 return;
16802 /* Set the glyph row flag indicating that the face of the last glyph
16803 in the text area has to be drawn to the end of the text area. */
16804 it->glyph_row->fill_line_p = 1;
16806 /* If current character of IT is not ASCII, make sure we have the
16807 ASCII face. This will be automatically undone the next time
16808 get_next_display_element returns a multibyte character. Note
16809 that the character will always be single byte in unibyte
16810 text. */
16811 if (!ASCII_CHAR_P (it->c))
16813 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16816 if (FRAME_WINDOW_P (f))
16818 /* If the row is empty, add a space with the current face of IT,
16819 so that we know which face to draw. */
16820 if (it->glyph_row->used[TEXT_AREA] == 0)
16822 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16823 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16824 it->glyph_row->used[TEXT_AREA] = 1;
16826 #ifdef HAVE_WINDOW_SYSTEM
16827 if (it->glyph_row->reversed_p)
16829 /* Prepend a stretch glyph to the row, such that the
16830 rightmost glyph will be drawn flushed all the way to the
16831 right margin of the window. The stretch glyph that will
16832 occupy the empty space, if any, to the left of the
16833 glyphs. */
16834 struct font *font = face->font ? face->font : FRAME_FONT (f);
16835 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
16836 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
16837 struct glyph *g;
16838 int row_width, stretch_ascent, stretch_width;
16839 struct text_pos saved_pos;
16840 int saved_face_id, saved_avoid_cursor;
16842 for (row_width = 0, g = row_start; g < row_end; g++)
16843 row_width += g->pixel_width;
16844 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
16845 if (stretch_width > 0)
16847 stretch_ascent =
16848 (((it->ascent + it->descent)
16849 * FONT_BASE (font)) / FONT_HEIGHT (font));
16850 saved_pos = it->position;
16851 memset (&it->position, 0, sizeof it->position);
16852 saved_avoid_cursor = it->avoid_cursor_p;
16853 it->avoid_cursor_p = 1;
16854 saved_face_id = it->face_id;
16855 /* The last row's stretch glyph should get the default
16856 face, to avoid painting the rest of the window with
16857 the region face, if the region ends at ZV. */
16858 if (it->glyph_row->ends_at_zv_p)
16859 it->face_id = DEFAULT_FACE_ID;
16860 else
16861 it->face_id = face->id;
16862 append_stretch_glyph (it, make_number (0), stretch_width,
16863 it->ascent + it->descent, stretch_ascent);
16864 it->position = saved_pos;
16865 it->avoid_cursor_p = saved_avoid_cursor;
16866 it->face_id = saved_face_id;
16869 #endif /* HAVE_WINDOW_SYSTEM */
16871 else
16873 /* Save some values that must not be changed. */
16874 int saved_x = it->current_x;
16875 struct text_pos saved_pos;
16876 Lisp_Object saved_object;
16877 enum display_element_type saved_what = it->what;
16878 int saved_face_id = it->face_id;
16880 saved_object = it->object;
16881 saved_pos = it->position;
16883 it->what = IT_CHARACTER;
16884 memset (&it->position, 0, sizeof it->position);
16885 it->object = make_number (0);
16886 it->c = ' ';
16887 it->len = 1;
16888 /* The last row's blank glyphs should get the default face, to
16889 avoid painting the rest of the window with the region face,
16890 if the region ends at ZV. */
16891 if (it->glyph_row->ends_at_zv_p)
16892 it->face_id = DEFAULT_FACE_ID;
16893 else
16894 it->face_id = face->id;
16896 PRODUCE_GLYPHS (it);
16898 while (it->current_x <= it->last_visible_x)
16899 PRODUCE_GLYPHS (it);
16901 /* Don't count these blanks really. It would let us insert a left
16902 truncation glyph below and make us set the cursor on them, maybe. */
16903 it->current_x = saved_x;
16904 it->object = saved_object;
16905 it->position = saved_pos;
16906 it->what = saved_what;
16907 it->face_id = saved_face_id;
16912 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16913 trailing whitespace. */
16915 static int
16916 trailing_whitespace_p (int charpos)
16918 int bytepos = CHAR_TO_BYTE (charpos);
16919 int c = 0;
16921 while (bytepos < ZV_BYTE
16922 && (c = FETCH_CHAR (bytepos),
16923 c == ' ' || c == '\t'))
16924 ++bytepos;
16926 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16928 if (bytepos != PT_BYTE)
16929 return 1;
16931 return 0;
16935 /* Highlight trailing whitespace, if any, in ROW. */
16937 void
16938 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
16940 int used = row->used[TEXT_AREA];
16942 if (used)
16944 struct glyph *start = row->glyphs[TEXT_AREA];
16945 struct glyph *glyph = start + used - 1;
16947 if (row->reversed_p)
16949 /* Right-to-left rows need to be processed in the opposite
16950 direction, so swap the edge pointers. */
16951 glyph = start;
16952 start = row->glyphs[TEXT_AREA] + used - 1;
16955 /* Skip over glyphs inserted to display the cursor at the
16956 end of a line, for extending the face of the last glyph
16957 to the end of the line on terminals, and for truncation
16958 and continuation glyphs. */
16959 if (!row->reversed_p)
16961 while (glyph >= start
16962 && glyph->type == CHAR_GLYPH
16963 && INTEGERP (glyph->object))
16964 --glyph;
16966 else
16968 while (glyph <= start
16969 && glyph->type == CHAR_GLYPH
16970 && INTEGERP (glyph->object))
16971 ++glyph;
16974 /* If last glyph is a space or stretch, and it's trailing
16975 whitespace, set the face of all trailing whitespace glyphs in
16976 IT->glyph_row to `trailing-whitespace'. */
16977 if ((row->reversed_p ? glyph <= start : glyph >= start)
16978 && BUFFERP (glyph->object)
16979 && (glyph->type == STRETCH_GLYPH
16980 || (glyph->type == CHAR_GLYPH
16981 && glyph->u.ch == ' '))
16982 && trailing_whitespace_p (glyph->charpos))
16984 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16985 if (face_id < 0)
16986 return;
16988 if (!row->reversed_p)
16990 while (glyph >= start
16991 && BUFFERP (glyph->object)
16992 && (glyph->type == STRETCH_GLYPH
16993 || (glyph->type == CHAR_GLYPH
16994 && glyph->u.ch == ' ')))
16995 (glyph--)->face_id = face_id;
16997 else
16999 while (glyph <= start
17000 && BUFFERP (glyph->object)
17001 && (glyph->type == STRETCH_GLYPH
17002 || (glyph->type == CHAR_GLYPH
17003 && glyph->u.ch == ' ')))
17004 (glyph++)->face_id = face_id;
17011 /* Value is non-zero if glyph row ROW in window W should be
17012 used to hold the cursor. */
17014 static int
17015 cursor_row_p (struct window *w, struct glyph_row *row)
17017 int cursor_row_p = 1;
17019 if (PT == CHARPOS (row->end.pos))
17021 /* Suppose the row ends on a string.
17022 Unless the row is continued, that means it ends on a newline
17023 in the string. If it's anything other than a display string
17024 (e.g. a before-string from an overlay), we don't want the
17025 cursor there. (This heuristic seems to give the optimal
17026 behavior for the various types of multi-line strings.) */
17027 if (CHARPOS (row->end.string_pos) >= 0)
17029 if (row->continued_p)
17030 cursor_row_p = 1;
17031 else
17033 /* Check for `display' property. */
17034 struct glyph *beg = row->glyphs[TEXT_AREA];
17035 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17036 struct glyph *glyph;
17038 cursor_row_p = 0;
17039 for (glyph = end; glyph >= beg; --glyph)
17040 if (STRINGP (glyph->object))
17042 Lisp_Object prop
17043 = Fget_char_property (make_number (PT),
17044 Qdisplay, Qnil);
17045 cursor_row_p =
17046 (!NILP (prop)
17047 && display_prop_string_p (prop, glyph->object));
17048 break;
17052 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17054 /* If the row ends in middle of a real character,
17055 and the line is continued, we want the cursor here.
17056 That's because CHARPOS (ROW->end.pos) would equal
17057 PT if PT is before the character. */
17058 if (!row->ends_in_ellipsis_p)
17059 cursor_row_p = row->continued_p;
17060 else
17061 /* If the row ends in an ellipsis, then
17062 CHARPOS (ROW->end.pos) will equal point after the
17063 invisible text. We want that position to be displayed
17064 after the ellipsis. */
17065 cursor_row_p = 0;
17067 /* If the row ends at ZV, display the cursor at the end of that
17068 row instead of at the start of the row below. */
17069 else if (row->ends_at_zv_p)
17070 cursor_row_p = 1;
17071 else
17072 cursor_row_p = 0;
17075 return cursor_row_p;
17080 /* Push the display property PROP so that it will be rendered at the
17081 current position in IT. Return 1 if PROP was successfully pushed,
17082 0 otherwise. */
17084 static int
17085 push_display_prop (struct it *it, Lisp_Object prop)
17087 push_it (it);
17089 if (STRINGP (prop))
17091 if (SCHARS (prop) == 0)
17093 pop_it (it);
17094 return 0;
17097 it->string = prop;
17098 it->multibyte_p = STRING_MULTIBYTE (it->string);
17099 it->current.overlay_string_index = -1;
17100 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17101 it->end_charpos = it->string_nchars = SCHARS (it->string);
17102 it->method = GET_FROM_STRING;
17103 it->stop_charpos = 0;
17105 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17107 it->method = GET_FROM_STRETCH;
17108 it->object = prop;
17110 #ifdef HAVE_WINDOW_SYSTEM
17111 else if (IMAGEP (prop))
17113 it->what = IT_IMAGE;
17114 it->image_id = lookup_image (it->f, prop);
17115 it->method = GET_FROM_IMAGE;
17117 #endif /* HAVE_WINDOW_SYSTEM */
17118 else
17120 pop_it (it); /* bogus display property, give up */
17121 return 0;
17124 return 1;
17127 /* Return the character-property PROP at the current position in IT. */
17129 static Lisp_Object
17130 get_it_property (struct it *it, Lisp_Object prop)
17132 Lisp_Object position;
17134 if (STRINGP (it->object))
17135 position = make_number (IT_STRING_CHARPOS (*it));
17136 else if (BUFFERP (it->object))
17137 position = make_number (IT_CHARPOS (*it));
17138 else
17139 return Qnil;
17141 return Fget_char_property (position, prop, it->object);
17144 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17146 static void
17147 handle_line_prefix (struct it *it)
17149 Lisp_Object prefix;
17150 if (it->continuation_lines_width > 0)
17152 prefix = get_it_property (it, Qwrap_prefix);
17153 if (NILP (prefix))
17154 prefix = Vwrap_prefix;
17156 else
17158 prefix = get_it_property (it, Qline_prefix);
17159 if (NILP (prefix))
17160 prefix = Vline_prefix;
17162 if (! NILP (prefix) && push_display_prop (it, prefix))
17164 /* If the prefix is wider than the window, and we try to wrap
17165 it, it would acquire its own wrap prefix, and so on till the
17166 iterator stack overflows. So, don't wrap the prefix. */
17167 it->line_wrap = TRUNCATE;
17168 it->avoid_cursor_p = 1;
17174 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17175 only for R2L lines from display_line, when it decides that too many
17176 glyphs were produced by PRODUCE_GLYPHS, and the line needs to be
17177 continued. */
17178 static void
17179 unproduce_glyphs (struct it *it, int n)
17181 struct glyph *glyph, *end;
17183 xassert (it->glyph_row);
17184 xassert (it->glyph_row->reversed_p);
17185 xassert (it->area == TEXT_AREA);
17186 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17188 if (n > it->glyph_row->used[TEXT_AREA])
17189 n = it->glyph_row->used[TEXT_AREA];
17190 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17191 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17192 for ( ; glyph < end; glyph++)
17193 glyph[-n] = *glyph;
17196 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17197 and ROW->maxpos. */
17198 static void
17199 find_row_edges (struct it *it, struct glyph_row *row,
17200 EMACS_INT min_pos, EMACS_INT min_bpos,
17201 EMACS_INT max_pos, EMACS_INT max_bpos)
17203 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17204 lines' rows is implemented for bidi-reordered rows. */
17206 /* ROW->minpos is the value of min_pos, the minimal buffer position
17207 we have in ROW. */
17208 if (min_pos <= ZV)
17209 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17210 else
17212 /* We didn't find _any_ valid buffer positions in any of the
17213 glyphs, so we must trust the iterator's computed
17214 positions. */
17215 row->minpos = row->start.pos;
17216 max_pos = CHARPOS (it->current.pos);
17217 max_bpos = BYTEPOS (it->current.pos);
17220 if (!max_pos)
17221 abort ();
17223 /* Here are the various use-cases for ending the row, and the
17224 corresponding values for ROW->maxpos:
17226 Line ends in a newline from buffer eol_pos + 1
17227 Line is continued from buffer max_pos + 1
17228 Line is truncated on right it->current.pos
17229 Line ends in a newline from string max_pos
17230 Line is continued from string max_pos
17231 Line is continued from display vector max_pos
17232 Line is entirely from a string min_pos == max_pos
17233 Line is entirely from a display vector min_pos == max_pos
17234 Line that ends at ZV ZV
17236 If you discover other use-cases, please add them here as
17237 appropriate. */
17238 if (row->ends_at_zv_p)
17239 row->maxpos = it->current.pos;
17240 else if (row->used[TEXT_AREA])
17242 if (row->ends_in_newline_from_string_p)
17243 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17244 else if (CHARPOS (it->eol_pos) > 0)
17245 SET_TEXT_POS (row->maxpos,
17246 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17247 else if (row->continued_p)
17249 /* If max_pos is different from IT's current position, it
17250 means IT->method does not belong to the display element
17251 at max_pos. However, it also means that the display
17252 element at max_pos was displayed in its entirety on this
17253 line, which is equivalent to saying that the next line
17254 starts at the next buffer position. */
17255 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17256 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17257 else
17259 INC_BOTH (max_pos, max_bpos);
17260 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17263 else if (row->truncated_on_right_p)
17264 /* display_line already called reseat_at_next_visible_line_start,
17265 which puts the iterator at the beginning of the next line, in
17266 the logical order. */
17267 row->maxpos = it->current.pos;
17268 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17269 /* A line that is entirely from a string/image/stretch... */
17270 row->maxpos = row->minpos;
17271 else
17272 abort ();
17274 else
17275 row->maxpos = it->current.pos;
17278 /* Construct the glyph row IT->glyph_row in the desired matrix of
17279 IT->w from text at the current position of IT. See dispextern.h
17280 for an overview of struct it. Value is non-zero if
17281 IT->glyph_row displays text, as opposed to a line displaying ZV
17282 only. */
17284 static int
17285 display_line (struct it *it)
17287 struct glyph_row *row = it->glyph_row;
17288 Lisp_Object overlay_arrow_string;
17289 struct it wrap_it;
17290 int may_wrap = 0, wrap_x;
17291 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
17292 int wrap_row_phys_ascent, wrap_row_phys_height;
17293 int wrap_row_extra_line_spacing;
17294 EMACS_INT wrap_row_min_pos, wrap_row_min_bpos;
17295 EMACS_INT wrap_row_max_pos, wrap_row_max_bpos;
17296 int cvpos;
17297 EMACS_INT min_pos = ZV + 1, min_bpos, max_pos = 0, max_bpos;
17299 /* We always start displaying at hpos zero even if hscrolled. */
17300 xassert (it->hpos == 0 && it->current_x == 0);
17302 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17303 >= it->w->desired_matrix->nrows)
17305 it->w->nrows_scale_factor++;
17306 fonts_changed_p = 1;
17307 return 0;
17310 /* Is IT->w showing the region? */
17311 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17313 /* Clear the result glyph row and enable it. */
17314 prepare_desired_row (row);
17316 row->y = it->current_y;
17317 row->start = it->start;
17318 row->continuation_lines_width = it->continuation_lines_width;
17319 row->displays_text_p = 1;
17320 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
17321 it->starts_in_middle_of_char_p = 0;
17323 /* Arrange the overlays nicely for our purposes. Usually, we call
17324 display_line on only one line at a time, in which case this
17325 can't really hurt too much, or we call it on lines which appear
17326 one after another in the buffer, in which case all calls to
17327 recenter_overlay_lists but the first will be pretty cheap. */
17328 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
17330 /* Move over display elements that are not visible because we are
17331 hscrolled. This may stop at an x-position < IT->first_visible_x
17332 if the first glyph is partially visible or if we hit a line end. */
17333 if (it->current_x < it->first_visible_x)
17335 move_it_in_display_line_to (it, ZV, it->first_visible_x,
17336 MOVE_TO_POS | MOVE_TO_X);
17338 else
17340 /* We only do this when not calling `move_it_in_display_line_to'
17341 above, because move_it_in_display_line_to calls
17342 handle_line_prefix itself. */
17343 handle_line_prefix (it);
17346 /* Get the initial row height. This is either the height of the
17347 text hscrolled, if there is any, or zero. */
17348 row->ascent = it->max_ascent;
17349 row->height = it->max_ascent + it->max_descent;
17350 row->phys_ascent = it->max_phys_ascent;
17351 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17352 row->extra_line_spacing = it->max_extra_line_spacing;
17354 /* Utility macro to record max and min buffer positions seen until now. */
17355 #define RECORD_MAX_MIN_POS(IT) \
17356 do \
17358 if (IT_CHARPOS (*(IT)) < min_pos) \
17360 min_pos = IT_CHARPOS (*(IT)); \
17361 min_bpos = IT_BYTEPOS (*(IT)); \
17363 if (IT_CHARPOS (*(IT)) > max_pos) \
17365 max_pos = IT_CHARPOS (*(IT)); \
17366 max_bpos = IT_BYTEPOS (*(IT)); \
17369 while (0)
17371 /* Loop generating characters. The loop is left with IT on the next
17372 character to display. */
17373 while (1)
17375 int n_glyphs_before, hpos_before, x_before;
17376 int x, i, nglyphs;
17377 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
17379 /* Retrieve the next thing to display. Value is zero if end of
17380 buffer reached. */
17381 if (!get_next_display_element (it))
17383 /* Maybe add a space at the end of this line that is used to
17384 display the cursor there under X. Set the charpos of the
17385 first glyph of blank lines not corresponding to any text
17386 to -1. */
17387 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17388 row->exact_window_width_line_p = 1;
17389 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
17390 || row->used[TEXT_AREA] == 0)
17392 row->glyphs[TEXT_AREA]->charpos = -1;
17393 row->displays_text_p = 0;
17395 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
17396 && (!MINI_WINDOW_P (it->w)
17397 || (minibuf_level && EQ (it->window, minibuf_window))))
17398 row->indicate_empty_line_p = 1;
17401 it->continuation_lines_width = 0;
17402 row->ends_at_zv_p = 1;
17403 /* A row that displays right-to-left text must always have
17404 its last face extended all the way to the end of line,
17405 even if this row ends in ZV, because we still write to th
17406 screen left to right. */
17407 if (row->reversed_p)
17408 extend_face_to_end_of_line (it);
17409 break;
17412 /* Now, get the metrics of what we want to display. This also
17413 generates glyphs in `row' (which is IT->glyph_row). */
17414 n_glyphs_before = row->used[TEXT_AREA];
17415 x = it->current_x;
17417 /* Remember the line height so far in case the next element doesn't
17418 fit on the line. */
17419 if (it->line_wrap != TRUNCATE)
17421 ascent = it->max_ascent;
17422 descent = it->max_descent;
17423 phys_ascent = it->max_phys_ascent;
17424 phys_descent = it->max_phys_descent;
17426 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
17428 if (IT_DISPLAYING_WHITESPACE (it))
17429 may_wrap = 1;
17430 else if (may_wrap)
17432 wrap_it = *it;
17433 wrap_x = x;
17434 wrap_row_used = row->used[TEXT_AREA];
17435 wrap_row_ascent = row->ascent;
17436 wrap_row_height = row->height;
17437 wrap_row_phys_ascent = row->phys_ascent;
17438 wrap_row_phys_height = row->phys_height;
17439 wrap_row_extra_line_spacing = row->extra_line_spacing;
17440 wrap_row_min_pos = min_pos;
17441 wrap_row_min_bpos = min_bpos;
17442 wrap_row_max_pos = max_pos;
17443 wrap_row_max_bpos = max_bpos;
17444 may_wrap = 0;
17449 PRODUCE_GLYPHS (it);
17451 /* If this display element was in marginal areas, continue with
17452 the next one. */
17453 if (it->area != TEXT_AREA)
17455 row->ascent = max (row->ascent, it->max_ascent);
17456 row->height = max (row->height, it->max_ascent + it->max_descent);
17457 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17458 row->phys_height = max (row->phys_height,
17459 it->max_phys_ascent + it->max_phys_descent);
17460 row->extra_line_spacing = max (row->extra_line_spacing,
17461 it->max_extra_line_spacing);
17462 set_iterator_to_next (it, 1);
17463 continue;
17466 /* Does the display element fit on the line? If we truncate
17467 lines, we should draw past the right edge of the window. If
17468 we don't truncate, we want to stop so that we can display the
17469 continuation glyph before the right margin. If lines are
17470 continued, there are two possible strategies for characters
17471 resulting in more than 1 glyph (e.g. tabs): Display as many
17472 glyphs as possible in this line and leave the rest for the
17473 continuation line, or display the whole element in the next
17474 line. Original redisplay did the former, so we do it also. */
17475 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
17476 hpos_before = it->hpos;
17477 x_before = x;
17479 if (/* Not a newline. */
17480 nglyphs > 0
17481 /* Glyphs produced fit entirely in the line. */
17482 && it->current_x < it->last_visible_x)
17484 it->hpos += nglyphs;
17485 row->ascent = max (row->ascent, it->max_ascent);
17486 row->height = max (row->height, it->max_ascent + it->max_descent);
17487 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17488 row->phys_height = max (row->phys_height,
17489 it->max_phys_ascent + it->max_phys_descent);
17490 row->extra_line_spacing = max (row->extra_line_spacing,
17491 it->max_extra_line_spacing);
17492 if (it->current_x - it->pixel_width < it->first_visible_x)
17493 row->x = x - it->first_visible_x;
17494 /* Record the maximum and minimum buffer positions seen so
17495 far in glyphs that will be displayed by this row. */
17496 if (it->bidi_p)
17497 RECORD_MAX_MIN_POS (it);
17499 else
17501 int new_x;
17502 struct glyph *glyph;
17504 for (i = 0; i < nglyphs; ++i, x = new_x)
17506 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17507 new_x = x + glyph->pixel_width;
17509 if (/* Lines are continued. */
17510 it->line_wrap != TRUNCATE
17511 && (/* Glyph doesn't fit on the line. */
17512 new_x > it->last_visible_x
17513 /* Or it fits exactly on a window system frame. */
17514 || (new_x == it->last_visible_x
17515 && FRAME_WINDOW_P (it->f))))
17517 /* End of a continued line. */
17519 if (it->hpos == 0
17520 || (new_x == it->last_visible_x
17521 && FRAME_WINDOW_P (it->f)))
17523 /* Current glyph is the only one on the line or
17524 fits exactly on the line. We must continue
17525 the line because we can't draw the cursor
17526 after the glyph. */
17527 row->continued_p = 1;
17528 it->current_x = new_x;
17529 it->continuation_lines_width += new_x;
17530 ++it->hpos;
17531 /* Record the maximum and minimum buffer
17532 positions seen so far in glyphs that will be
17533 displayed by this row. */
17534 if (it->bidi_p)
17535 RECORD_MAX_MIN_POS (it);
17536 if (i == nglyphs - 1)
17538 /* If line-wrap is on, check if a previous
17539 wrap point was found. */
17540 if (wrap_row_used > 0
17541 /* Even if there is a previous wrap
17542 point, continue the line here as
17543 usual, if (i) the previous character
17544 was a space or tab AND (ii) the
17545 current character is not. */
17546 && (!may_wrap
17547 || IT_DISPLAYING_WHITESPACE (it)))
17548 goto back_to_wrap;
17550 set_iterator_to_next (it, 1);
17551 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17553 if (!get_next_display_element (it))
17555 row->exact_window_width_line_p = 1;
17556 it->continuation_lines_width = 0;
17557 row->continued_p = 0;
17558 row->ends_at_zv_p = 1;
17560 else if (ITERATOR_AT_END_OF_LINE_P (it))
17562 row->continued_p = 0;
17563 row->exact_window_width_line_p = 1;
17568 else if (CHAR_GLYPH_PADDING_P (*glyph)
17569 && !FRAME_WINDOW_P (it->f))
17571 /* A padding glyph that doesn't fit on this line.
17572 This means the whole character doesn't fit
17573 on the line. */
17574 if (row->reversed_p)
17575 unproduce_glyphs (it, row->used[TEXT_AREA]
17576 - n_glyphs_before);
17577 row->used[TEXT_AREA] = n_glyphs_before;
17579 /* Fill the rest of the row with continuation
17580 glyphs like in 20.x. */
17581 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
17582 < row->glyphs[1 + TEXT_AREA])
17583 produce_special_glyphs (it, IT_CONTINUATION);
17585 row->continued_p = 1;
17586 it->current_x = x_before;
17587 it->continuation_lines_width += x_before;
17589 /* Restore the height to what it was before the
17590 element not fitting on the line. */
17591 it->max_ascent = ascent;
17592 it->max_descent = descent;
17593 it->max_phys_ascent = phys_ascent;
17594 it->max_phys_descent = phys_descent;
17596 else if (wrap_row_used > 0)
17598 back_to_wrap:
17599 if (row->reversed_p)
17600 unproduce_glyphs (it,
17601 row->used[TEXT_AREA] - wrap_row_used);
17602 *it = wrap_it;
17603 it->continuation_lines_width += wrap_x;
17604 row->used[TEXT_AREA] = wrap_row_used;
17605 row->ascent = wrap_row_ascent;
17606 row->height = wrap_row_height;
17607 row->phys_ascent = wrap_row_phys_ascent;
17608 row->phys_height = wrap_row_phys_height;
17609 row->extra_line_spacing = wrap_row_extra_line_spacing;
17610 min_pos = wrap_row_min_pos;
17611 min_bpos = wrap_row_min_bpos;
17612 max_pos = wrap_row_max_pos;
17613 max_bpos = wrap_row_max_bpos;
17614 row->continued_p = 1;
17615 row->ends_at_zv_p = 0;
17616 row->exact_window_width_line_p = 0;
17617 it->continuation_lines_width += x;
17619 /* Make sure that a non-default face is extended
17620 up to the right margin of the window. */
17621 extend_face_to_end_of_line (it);
17623 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
17625 /* A TAB that extends past the right edge of the
17626 window. This produces a single glyph on
17627 window system frames. We leave the glyph in
17628 this row and let it fill the row, but don't
17629 consume the TAB. */
17630 it->continuation_lines_width += it->last_visible_x;
17631 row->ends_in_middle_of_char_p = 1;
17632 row->continued_p = 1;
17633 glyph->pixel_width = it->last_visible_x - x;
17634 it->starts_in_middle_of_char_p = 1;
17636 else
17638 /* Something other than a TAB that draws past
17639 the right edge of the window. Restore
17640 positions to values before the element. */
17641 if (row->reversed_p)
17642 unproduce_glyphs (it, row->used[TEXT_AREA]
17643 - (n_glyphs_before + i));
17644 row->used[TEXT_AREA] = n_glyphs_before + i;
17646 /* Display continuation glyphs. */
17647 if (!FRAME_WINDOW_P (it->f))
17648 produce_special_glyphs (it, IT_CONTINUATION);
17649 row->continued_p = 1;
17651 it->current_x = x_before;
17652 it->continuation_lines_width += x;
17653 extend_face_to_end_of_line (it);
17655 if (nglyphs > 1 && i > 0)
17657 row->ends_in_middle_of_char_p = 1;
17658 it->starts_in_middle_of_char_p = 1;
17661 /* Restore the height to what it was before the
17662 element not fitting on the line. */
17663 it->max_ascent = ascent;
17664 it->max_descent = descent;
17665 it->max_phys_ascent = phys_ascent;
17666 it->max_phys_descent = phys_descent;
17669 break;
17671 else if (new_x > it->first_visible_x)
17673 /* Increment number of glyphs actually displayed. */
17674 ++it->hpos;
17676 /* Record the maximum and minimum buffer positions
17677 seen so far in glyphs that will be displayed by
17678 this row. */
17679 if (it->bidi_p)
17680 RECORD_MAX_MIN_POS (it);
17682 if (x < it->first_visible_x)
17683 /* Glyph is partially visible, i.e. row starts at
17684 negative X position. */
17685 row->x = x - it->first_visible_x;
17687 else
17689 /* Glyph is completely off the left margin of the
17690 window. This should not happen because of the
17691 move_it_in_display_line at the start of this
17692 function, unless the text display area of the
17693 window is empty. */
17694 xassert (it->first_visible_x <= it->last_visible_x);
17698 row->ascent = max (row->ascent, it->max_ascent);
17699 row->height = max (row->height, it->max_ascent + it->max_descent);
17700 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17701 row->phys_height = max (row->phys_height,
17702 it->max_phys_ascent + it->max_phys_descent);
17703 row->extra_line_spacing = max (row->extra_line_spacing,
17704 it->max_extra_line_spacing);
17706 /* End of this display line if row is continued. */
17707 if (row->continued_p || row->ends_at_zv_p)
17708 break;
17711 at_end_of_line:
17712 /* Is this a line end? If yes, we're also done, after making
17713 sure that a non-default face is extended up to the right
17714 margin of the window. */
17715 if (ITERATOR_AT_END_OF_LINE_P (it))
17717 int used_before = row->used[TEXT_AREA];
17719 row->ends_in_newline_from_string_p = STRINGP (it->object);
17721 /* Add a space at the end of the line that is used to
17722 display the cursor there. */
17723 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17724 append_space_for_newline (it, 0);
17726 /* Extend the face to the end of the line. */
17727 extend_face_to_end_of_line (it);
17729 /* Make sure we have the position. */
17730 if (used_before == 0)
17731 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
17733 /* Record the position of the newline, for use in
17734 find_row_edges. */
17735 it->eol_pos = it->current.pos;
17737 /* Consume the line end. This skips over invisible lines. */
17738 set_iterator_to_next (it, 1);
17739 it->continuation_lines_width = 0;
17740 break;
17743 /* Proceed with next display element. Note that this skips
17744 over lines invisible because of selective display. */
17745 set_iterator_to_next (it, 1);
17747 /* If we truncate lines, we are done when the last displayed
17748 glyphs reach past the right margin of the window. */
17749 if (it->line_wrap == TRUNCATE
17750 && (FRAME_WINDOW_P (it->f)
17751 ? (it->current_x >= it->last_visible_x)
17752 : (it->current_x > it->last_visible_x)))
17754 /* Maybe add truncation glyphs. */
17755 if (!FRAME_WINDOW_P (it->f))
17757 int i, n;
17759 if (!row->reversed_p)
17761 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17762 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17763 break;
17765 else
17767 for (i = 0; i < row->used[TEXT_AREA]; i++)
17768 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17769 break;
17770 /* Remove any padding glyphs at the front of ROW, to
17771 make room for the truncation glyphs we will be
17772 adding below. The loop below always inserts at
17773 least one truncation glyph, so also remove the
17774 last glyph added to ROW. */
17775 unproduce_glyphs (it, i + 1);
17776 /* Adjust i for the loop below. */
17777 i = row->used[TEXT_AREA] - (i + 1);
17780 for (n = row->used[TEXT_AREA]; i < n; ++i)
17782 row->used[TEXT_AREA] = i;
17783 produce_special_glyphs (it, IT_TRUNCATION);
17786 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
17788 /* Don't truncate if we can overflow newline into fringe. */
17789 if (!get_next_display_element (it))
17791 it->continuation_lines_width = 0;
17792 row->ends_at_zv_p = 1;
17793 row->exact_window_width_line_p = 1;
17794 break;
17796 if (ITERATOR_AT_END_OF_LINE_P (it))
17798 row->exact_window_width_line_p = 1;
17799 goto at_end_of_line;
17803 row->truncated_on_right_p = 1;
17804 it->continuation_lines_width = 0;
17805 reseat_at_next_visible_line_start (it, 0);
17806 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
17807 it->hpos = hpos_before;
17808 it->current_x = x_before;
17809 break;
17813 /* If line is not empty and hscrolled, maybe insert truncation glyphs
17814 at the left window margin. */
17815 if (it->first_visible_x
17816 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
17818 if (!FRAME_WINDOW_P (it->f))
17819 insert_left_trunc_glyphs (it);
17820 row->truncated_on_left_p = 1;
17823 /* If the start of this line is the overlay arrow-position, then
17824 mark this glyph row as the one containing the overlay arrow.
17825 This is clearly a mess with variable size fonts. It would be
17826 better to let it be displayed like cursors under X. */
17827 if ((row->displays_text_p || !overlay_arrow_seen)
17828 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
17829 !NILP (overlay_arrow_string)))
17831 /* Overlay arrow in window redisplay is a fringe bitmap. */
17832 if (STRINGP (overlay_arrow_string))
17834 struct glyph_row *arrow_row
17835 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
17836 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
17837 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
17838 struct glyph *p = row->glyphs[TEXT_AREA];
17839 struct glyph *p2, *end;
17841 /* Copy the arrow glyphs. */
17842 while (glyph < arrow_end)
17843 *p++ = *glyph++;
17845 /* Throw away padding glyphs. */
17846 p2 = p;
17847 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17848 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
17849 ++p2;
17850 if (p2 > p)
17852 while (p2 < end)
17853 *p++ = *p2++;
17854 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
17857 else
17859 xassert (INTEGERP (overlay_arrow_string));
17860 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
17862 overlay_arrow_seen = 1;
17865 /* Compute pixel dimensions of this line. */
17866 compute_line_metrics (it);
17868 /* Remember the position at which this line ends. */
17869 row->end = it->current;
17870 if (!it->bidi_p)
17872 row->minpos = row->start.pos;
17873 row->maxpos = row->end.pos;
17875 else
17877 /* ROW->minpos and ROW->maxpos must be the smallest and
17878 `1 + the largest' buffer positions in ROW. But if ROW was
17879 bidi-reordered, these two positions can be anywhere in the
17880 row, so we must determine them now. */
17881 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
17884 /* Record whether this row ends inside an ellipsis. */
17885 row->ends_in_ellipsis_p
17886 = (it->method == GET_FROM_DISPLAY_VECTOR
17887 && it->ellipsis_p);
17889 /* Save fringe bitmaps in this row. */
17890 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17891 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17892 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17893 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17895 it->left_user_fringe_bitmap = 0;
17896 it->left_user_fringe_face_id = 0;
17897 it->right_user_fringe_bitmap = 0;
17898 it->right_user_fringe_face_id = 0;
17900 /* Maybe set the cursor. */
17901 cvpos = it->w->cursor.vpos;
17902 if ((cvpos < 0
17903 /* In bidi-reordered rows, keep checking for proper cursor
17904 position even if one has been found already, because buffer
17905 positions in such rows change non-linearly with ROW->VPOS,
17906 when a line is continued. One exception: when we are at ZV,
17907 display cursor on the first suitable glyph row, since all
17908 the empty rows after that also have their position set to ZV. */
17909 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17910 lines' rows is implemented for bidi-reordered rows. */
17911 || (it->bidi_p
17912 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
17913 && PT >= MATRIX_ROW_START_CHARPOS (row)
17914 && PT <= MATRIX_ROW_END_CHARPOS (row)
17915 && cursor_row_p (it->w, row))
17916 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17918 /* Highlight trailing whitespace. */
17919 if (!NILP (Vshow_trailing_whitespace))
17920 highlight_trailing_whitespace (it->f, it->glyph_row);
17922 /* Prepare for the next line. This line starts horizontally at (X
17923 HPOS) = (0 0). Vertical positions are incremented. As a
17924 convenience for the caller, IT->glyph_row is set to the next
17925 row to be used. */
17926 it->current_x = it->hpos = 0;
17927 it->current_y += row->height;
17928 SET_TEXT_POS (it->eol_pos, 0, 0);
17929 ++it->vpos;
17930 ++it->glyph_row;
17931 /* The next row should by default use the same value of the
17932 reversed_p flag as this one. set_iterator_to_next decides when
17933 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
17934 the flag accordingly. */
17935 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
17936 it->glyph_row->reversed_p = row->reversed_p;
17937 it->start = row->end;
17938 return row->displays_text_p;
17940 #undef RECORD_MAX_MIN_POS
17943 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
17944 Scurrent_bidi_paragraph_direction, 0, 1, 0,
17945 doc: /* Return paragraph direction at point in BUFFER.
17946 Value is either `left-to-right' or `right-to-left'.
17947 If BUFFER is omitted or nil, it defaults to the current buffer.
17949 Paragraph direction determines how the text in the paragraph is displayed.
17950 In left-to-right paragraphs, text begins at the left margin of the window
17951 and the reading direction is generally left to right. In right-to-left
17952 paragraphs, text begins at the right margin and is read from right to left.
17954 See also `bidi-paragraph-direction'. */)
17955 (Lisp_Object buffer)
17957 struct buffer *buf;
17958 struct buffer *old;
17960 if (NILP (buffer))
17961 buf = current_buffer;
17962 else
17964 CHECK_BUFFER (buffer);
17965 buf = XBUFFER (buffer);
17966 old = current_buffer;
17969 if (NILP (buf->bidi_display_reordering))
17970 return Qleft_to_right;
17971 else if (!NILP (buf->bidi_paragraph_direction))
17972 return buf->bidi_paragraph_direction;
17973 else
17975 /* Determine the direction from buffer text. We could try to
17976 use current_matrix if it is up to date, but this seems fast
17977 enough as it is. */
17978 struct bidi_it itb;
17979 EMACS_INT pos = BUF_PT (buf);
17980 EMACS_INT bytepos = BUF_PT_BYTE (buf);
17982 if (buf != current_buffer)
17983 set_buffer_temp (buf);
17984 /* Find previous non-empty line. */
17985 if (pos >= ZV && pos > BEGV)
17987 pos--;
17988 bytepos = CHAR_TO_BYTE (pos);
17990 while (FETCH_BYTE (bytepos) == '\n')
17992 if (bytepos <= BEGV_BYTE)
17993 break;
17994 bytepos--;
17995 pos--;
17997 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
17998 bytepos--;
17999 itb.charpos = pos;
18000 itb.bytepos = bytepos;
18001 itb.first_elt = 1;
18003 bidi_paragraph_init (NEUTRAL_DIR, &itb);
18004 if (buf != current_buffer)
18005 set_buffer_temp (old);
18006 switch (itb.paragraph_dir)
18008 case L2R:
18009 return Qleft_to_right;
18010 break;
18011 case R2L:
18012 return Qright_to_left;
18013 break;
18014 default:
18015 abort ();
18022 /***********************************************************************
18023 Menu Bar
18024 ***********************************************************************/
18026 /* Redisplay the menu bar in the frame for window W.
18028 The menu bar of X frames that don't have X toolkit support is
18029 displayed in a special window W->frame->menu_bar_window.
18031 The menu bar of terminal frames is treated specially as far as
18032 glyph matrices are concerned. Menu bar lines are not part of
18033 windows, so the update is done directly on the frame matrix rows
18034 for the menu bar. */
18036 static void
18037 display_menu_bar (struct window *w)
18039 struct frame *f = XFRAME (WINDOW_FRAME (w));
18040 struct it it;
18041 Lisp_Object items;
18042 int i;
18044 /* Don't do all this for graphical frames. */
18045 #ifdef HAVE_NTGUI
18046 if (FRAME_W32_P (f))
18047 return;
18048 #endif
18049 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18050 if (FRAME_X_P (f))
18051 return;
18052 #endif
18054 #ifdef HAVE_NS
18055 if (FRAME_NS_P (f))
18056 return;
18057 #endif /* HAVE_NS */
18059 #ifdef USE_X_TOOLKIT
18060 xassert (!FRAME_WINDOW_P (f));
18061 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18062 it.first_visible_x = 0;
18063 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18064 #else /* not USE_X_TOOLKIT */
18065 if (FRAME_WINDOW_P (f))
18067 /* Menu bar lines are displayed in the desired matrix of the
18068 dummy window menu_bar_window. */
18069 struct window *menu_w;
18070 xassert (WINDOWP (f->menu_bar_window));
18071 menu_w = XWINDOW (f->menu_bar_window);
18072 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18073 MENU_FACE_ID);
18074 it.first_visible_x = 0;
18075 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18077 else
18079 /* This is a TTY frame, i.e. character hpos/vpos are used as
18080 pixel x/y. */
18081 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18082 MENU_FACE_ID);
18083 it.first_visible_x = 0;
18084 it.last_visible_x = FRAME_COLS (f);
18086 #endif /* not USE_X_TOOLKIT */
18088 if (! mode_line_inverse_video)
18089 /* Force the menu-bar to be displayed in the default face. */
18090 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18092 /* Clear all rows of the menu bar. */
18093 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18095 struct glyph_row *row = it.glyph_row + i;
18096 clear_glyph_row (row);
18097 row->enabled_p = 1;
18098 row->full_width_p = 1;
18101 /* Display all items of the menu bar. */
18102 items = FRAME_MENU_BAR_ITEMS (it.f);
18103 for (i = 0; i < XVECTOR (items)->size; i += 4)
18105 Lisp_Object string;
18107 /* Stop at nil string. */
18108 string = AREF (items, i + 1);
18109 if (NILP (string))
18110 break;
18112 /* Remember where item was displayed. */
18113 ASET (items, i + 3, make_number (it.hpos));
18115 /* Display the item, pad with one space. */
18116 if (it.current_x < it.last_visible_x)
18117 display_string (NULL, string, Qnil, 0, 0, &it,
18118 SCHARS (string) + 1, 0, 0, -1);
18121 /* Fill out the line with spaces. */
18122 if (it.current_x < it.last_visible_x)
18123 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18125 /* Compute the total height of the lines. */
18126 compute_line_metrics (&it);
18131 /***********************************************************************
18132 Mode Line
18133 ***********************************************************************/
18135 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18136 FORCE is non-zero, redisplay mode lines unconditionally.
18137 Otherwise, redisplay only mode lines that are garbaged. Value is
18138 the number of windows whose mode lines were redisplayed. */
18140 static int
18141 redisplay_mode_lines (Lisp_Object window, int force)
18143 int nwindows = 0;
18145 while (!NILP (window))
18147 struct window *w = XWINDOW (window);
18149 if (WINDOWP (w->hchild))
18150 nwindows += redisplay_mode_lines (w->hchild, force);
18151 else if (WINDOWP (w->vchild))
18152 nwindows += redisplay_mode_lines (w->vchild, force);
18153 else if (force
18154 || FRAME_GARBAGED_P (XFRAME (w->frame))
18155 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18157 struct text_pos lpoint;
18158 struct buffer *old = current_buffer;
18160 /* Set the window's buffer for the mode line display. */
18161 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18162 set_buffer_internal_1 (XBUFFER (w->buffer));
18164 /* Point refers normally to the selected window. For any
18165 other window, set up appropriate value. */
18166 if (!EQ (window, selected_window))
18168 struct text_pos pt;
18170 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18171 if (CHARPOS (pt) < BEGV)
18172 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18173 else if (CHARPOS (pt) > (ZV - 1))
18174 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18175 else
18176 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18179 /* Display mode lines. */
18180 clear_glyph_matrix (w->desired_matrix);
18181 if (display_mode_lines (w))
18183 ++nwindows;
18184 w->must_be_updated_p = 1;
18187 /* Restore old settings. */
18188 set_buffer_internal_1 (old);
18189 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18192 window = w->next;
18195 return nwindows;
18199 /* Display the mode and/or header line of window W. Value is the
18200 sum number of mode lines and header lines displayed. */
18202 static int
18203 display_mode_lines (struct window *w)
18205 Lisp_Object old_selected_window, old_selected_frame;
18206 int n = 0;
18208 old_selected_frame = selected_frame;
18209 selected_frame = w->frame;
18210 old_selected_window = selected_window;
18211 XSETWINDOW (selected_window, w);
18213 /* These will be set while the mode line specs are processed. */
18214 line_number_displayed = 0;
18215 w->column_number_displayed = Qnil;
18217 if (WINDOW_WANTS_MODELINE_P (w))
18219 struct window *sel_w = XWINDOW (old_selected_window);
18221 /* Select mode line face based on the real selected window. */
18222 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18223 current_buffer->mode_line_format);
18224 ++n;
18227 if (WINDOW_WANTS_HEADER_LINE_P (w))
18229 display_mode_line (w, HEADER_LINE_FACE_ID,
18230 current_buffer->header_line_format);
18231 ++n;
18234 selected_frame = old_selected_frame;
18235 selected_window = old_selected_window;
18236 return n;
18240 /* Display mode or header line of window W. FACE_ID specifies which
18241 line to display; it is either MODE_LINE_FACE_ID or
18242 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18243 display. Value is the pixel height of the mode/header line
18244 displayed. */
18246 static int
18247 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18249 struct it it;
18250 struct face *face;
18251 int count = SPECPDL_INDEX ();
18253 init_iterator (&it, w, -1, -1, NULL, face_id);
18254 /* Don't extend on a previously drawn mode-line.
18255 This may happen if called from pos_visible_p. */
18256 it.glyph_row->enabled_p = 0;
18257 prepare_desired_row (it.glyph_row);
18259 it.glyph_row->mode_line_p = 1;
18261 if (! mode_line_inverse_video)
18262 /* Force the mode-line to be displayed in the default face. */
18263 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18265 record_unwind_protect (unwind_format_mode_line,
18266 format_mode_line_unwind_data (NULL, Qnil, 0));
18268 mode_line_target = MODE_LINE_DISPLAY;
18270 /* Temporarily make frame's keyboard the current kboard so that
18271 kboard-local variables in the mode_line_format will get the right
18272 values. */
18273 push_kboard (FRAME_KBOARD (it.f));
18274 record_unwind_save_match_data ();
18275 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18276 pop_kboard ();
18278 unbind_to (count, Qnil);
18280 /* Fill up with spaces. */
18281 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18283 compute_line_metrics (&it);
18284 it.glyph_row->full_width_p = 1;
18285 it.glyph_row->continued_p = 0;
18286 it.glyph_row->truncated_on_left_p = 0;
18287 it.glyph_row->truncated_on_right_p = 0;
18289 /* Make a 3D mode-line have a shadow at its right end. */
18290 face = FACE_FROM_ID (it.f, face_id);
18291 extend_face_to_end_of_line (&it);
18292 if (face->box != FACE_NO_BOX)
18294 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
18295 + it.glyph_row->used[TEXT_AREA] - 1);
18296 last->right_box_line_p = 1;
18299 return it.glyph_row->height;
18302 /* Move element ELT in LIST to the front of LIST.
18303 Return the updated list. */
18305 static Lisp_Object
18306 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
18308 register Lisp_Object tail, prev;
18309 register Lisp_Object tem;
18311 tail = list;
18312 prev = Qnil;
18313 while (CONSP (tail))
18315 tem = XCAR (tail);
18317 if (EQ (elt, tem))
18319 /* Splice out the link TAIL. */
18320 if (NILP (prev))
18321 list = XCDR (tail);
18322 else
18323 Fsetcdr (prev, XCDR (tail));
18325 /* Now make it the first. */
18326 Fsetcdr (tail, list);
18327 return tail;
18329 else
18330 prev = tail;
18331 tail = XCDR (tail);
18332 QUIT;
18335 /* Not found--return unchanged LIST. */
18336 return list;
18339 /* Contribute ELT to the mode line for window IT->w. How it
18340 translates into text depends on its data type.
18342 IT describes the display environment in which we display, as usual.
18344 DEPTH is the depth in recursion. It is used to prevent
18345 infinite recursion here.
18347 FIELD_WIDTH is the number of characters the display of ELT should
18348 occupy in the mode line, and PRECISION is the maximum number of
18349 characters to display from ELT's representation. See
18350 display_string for details.
18352 Returns the hpos of the end of the text generated by ELT.
18354 PROPS is a property list to add to any string we encounter.
18356 If RISKY is nonzero, remove (disregard) any properties in any string
18357 we encounter, and ignore :eval and :propertize.
18359 The global variable `mode_line_target' determines whether the
18360 output is passed to `store_mode_line_noprop',
18361 `store_mode_line_string', or `display_string'. */
18363 static int
18364 display_mode_element (struct it *it, int depth, int field_width, int precision,
18365 Lisp_Object elt, Lisp_Object props, int risky)
18367 int n = 0, field, prec;
18368 int literal = 0;
18370 tail_recurse:
18371 if (depth > 100)
18372 elt = build_string ("*too-deep*");
18374 depth++;
18376 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
18378 case Lisp_String:
18380 /* A string: output it and check for %-constructs within it. */
18381 unsigned char c;
18382 int offset = 0;
18384 if (SCHARS (elt) > 0
18385 && (!NILP (props) || risky))
18387 Lisp_Object oprops, aelt;
18388 oprops = Ftext_properties_at (make_number (0), elt);
18390 /* If the starting string's properties are not what
18391 we want, translate the string. Also, if the string
18392 is risky, do that anyway. */
18394 if (NILP (Fequal (props, oprops)) || risky)
18396 /* If the starting string has properties,
18397 merge the specified ones onto the existing ones. */
18398 if (! NILP (oprops) && !risky)
18400 Lisp_Object tem;
18402 oprops = Fcopy_sequence (oprops);
18403 tem = props;
18404 while (CONSP (tem))
18406 oprops = Fplist_put (oprops, XCAR (tem),
18407 XCAR (XCDR (tem)));
18408 tem = XCDR (XCDR (tem));
18410 props = oprops;
18413 aelt = Fassoc (elt, mode_line_proptrans_alist);
18414 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
18416 /* AELT is what we want. Move it to the front
18417 without consing. */
18418 elt = XCAR (aelt);
18419 mode_line_proptrans_alist
18420 = move_elt_to_front (aelt, mode_line_proptrans_alist);
18422 else
18424 Lisp_Object tem;
18426 /* If AELT has the wrong props, it is useless.
18427 so get rid of it. */
18428 if (! NILP (aelt))
18429 mode_line_proptrans_alist
18430 = Fdelq (aelt, mode_line_proptrans_alist);
18432 elt = Fcopy_sequence (elt);
18433 Fset_text_properties (make_number (0), Flength (elt),
18434 props, elt);
18435 /* Add this item to mode_line_proptrans_alist. */
18436 mode_line_proptrans_alist
18437 = Fcons (Fcons (elt, props),
18438 mode_line_proptrans_alist);
18439 /* Truncate mode_line_proptrans_alist
18440 to at most 50 elements. */
18441 tem = Fnthcdr (make_number (50),
18442 mode_line_proptrans_alist);
18443 if (! NILP (tem))
18444 XSETCDR (tem, Qnil);
18449 offset = 0;
18451 if (literal)
18453 prec = precision - n;
18454 switch (mode_line_target)
18456 case MODE_LINE_NOPROP:
18457 case MODE_LINE_TITLE:
18458 n += store_mode_line_noprop (SDATA (elt), -1, prec);
18459 break;
18460 case MODE_LINE_STRING:
18461 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
18462 break;
18463 case MODE_LINE_DISPLAY:
18464 n += display_string (NULL, elt, Qnil, 0, 0, it,
18465 0, prec, 0, STRING_MULTIBYTE (elt));
18466 break;
18469 break;
18472 /* Handle the non-literal case. */
18474 while ((precision <= 0 || n < precision)
18475 && SREF (elt, offset) != 0
18476 && (mode_line_target != MODE_LINE_DISPLAY
18477 || it->current_x < it->last_visible_x))
18479 int last_offset = offset;
18481 /* Advance to end of string or next format specifier. */
18482 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
18485 if (offset - 1 != last_offset)
18487 int nchars, nbytes;
18489 /* Output to end of string or up to '%'. Field width
18490 is length of string. Don't output more than
18491 PRECISION allows us. */
18492 offset--;
18494 prec = c_string_width (SDATA (elt) + last_offset,
18495 offset - last_offset, precision - n,
18496 &nchars, &nbytes);
18498 switch (mode_line_target)
18500 case MODE_LINE_NOPROP:
18501 case MODE_LINE_TITLE:
18502 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
18503 break;
18504 case MODE_LINE_STRING:
18506 int bytepos = last_offset;
18507 int charpos = string_byte_to_char (elt, bytepos);
18508 int endpos = (precision <= 0
18509 ? string_byte_to_char (elt, offset)
18510 : charpos + nchars);
18512 n += store_mode_line_string (NULL,
18513 Fsubstring (elt, make_number (charpos),
18514 make_number (endpos)),
18515 0, 0, 0, Qnil);
18517 break;
18518 case MODE_LINE_DISPLAY:
18520 int bytepos = last_offset;
18521 int charpos = string_byte_to_char (elt, bytepos);
18523 if (precision <= 0)
18524 nchars = string_byte_to_char (elt, offset) - charpos;
18525 n += display_string (NULL, elt, Qnil, 0, charpos,
18526 it, 0, nchars, 0,
18527 STRING_MULTIBYTE (elt));
18529 break;
18532 else /* c == '%' */
18534 int percent_position = offset;
18536 /* Get the specified minimum width. Zero means
18537 don't pad. */
18538 field = 0;
18539 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
18540 field = field * 10 + c - '0';
18542 /* Don't pad beyond the total padding allowed. */
18543 if (field_width - n > 0 && field > field_width - n)
18544 field = field_width - n;
18546 /* Note that either PRECISION <= 0 or N < PRECISION. */
18547 prec = precision - n;
18549 if (c == 'M')
18550 n += display_mode_element (it, depth, field, prec,
18551 Vglobal_mode_string, props,
18552 risky);
18553 else if (c != 0)
18555 int multibyte;
18556 int bytepos, charpos;
18557 unsigned char *spec;
18558 Lisp_Object string;
18560 bytepos = percent_position;
18561 charpos = (STRING_MULTIBYTE (elt)
18562 ? string_byte_to_char (elt, bytepos)
18563 : bytepos);
18564 spec = decode_mode_spec (it->w, c, field, prec, &string);
18565 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
18567 switch (mode_line_target)
18569 case MODE_LINE_NOPROP:
18570 case MODE_LINE_TITLE:
18571 n += store_mode_line_noprop (spec, field, prec);
18572 break;
18573 case MODE_LINE_STRING:
18575 int len = strlen (spec);
18576 Lisp_Object tem = make_string (spec, len);
18577 props = Ftext_properties_at (make_number (charpos), elt);
18578 /* Should only keep face property in props */
18579 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
18581 break;
18582 case MODE_LINE_DISPLAY:
18584 int nglyphs_before, nwritten;
18586 nglyphs_before = it->glyph_row->used[TEXT_AREA];
18587 nwritten = display_string (spec, string, elt,
18588 charpos, 0, it,
18589 field, prec, 0,
18590 multibyte);
18592 /* Assign to the glyphs written above the
18593 string where the `%x' came from, position
18594 of the `%'. */
18595 if (nwritten > 0)
18597 struct glyph *glyph
18598 = (it->glyph_row->glyphs[TEXT_AREA]
18599 + nglyphs_before);
18600 int i;
18602 for (i = 0; i < nwritten; ++i)
18604 glyph[i].object = elt;
18605 glyph[i].charpos = charpos;
18608 n += nwritten;
18611 break;
18614 else /* c == 0 */
18615 break;
18619 break;
18621 case Lisp_Symbol:
18622 /* A symbol: process the value of the symbol recursively
18623 as if it appeared here directly. Avoid error if symbol void.
18624 Special case: if value of symbol is a string, output the string
18625 literally. */
18627 register Lisp_Object tem;
18629 /* If the variable is not marked as risky to set
18630 then its contents are risky to use. */
18631 if (NILP (Fget (elt, Qrisky_local_variable)))
18632 risky = 1;
18634 tem = Fboundp (elt);
18635 if (!NILP (tem))
18637 tem = Fsymbol_value (elt);
18638 /* If value is a string, output that string literally:
18639 don't check for % within it. */
18640 if (STRINGP (tem))
18641 literal = 1;
18643 if (!EQ (tem, elt))
18645 /* Give up right away for nil or t. */
18646 elt = tem;
18647 goto tail_recurse;
18651 break;
18653 case Lisp_Cons:
18655 register Lisp_Object car, tem;
18657 /* A cons cell: five distinct cases.
18658 If first element is :eval or :propertize, do something special.
18659 If first element is a string or a cons, process all the elements
18660 and effectively concatenate them.
18661 If first element is a negative number, truncate displaying cdr to
18662 at most that many characters. If positive, pad (with spaces)
18663 to at least that many characters.
18664 If first element is a symbol, process the cadr or caddr recursively
18665 according to whether the symbol's value is non-nil or nil. */
18666 car = XCAR (elt);
18667 if (EQ (car, QCeval))
18669 /* An element of the form (:eval FORM) means evaluate FORM
18670 and use the result as mode line elements. */
18672 if (risky)
18673 break;
18675 if (CONSP (XCDR (elt)))
18677 Lisp_Object spec;
18678 spec = safe_eval (XCAR (XCDR (elt)));
18679 n += display_mode_element (it, depth, field_width - n,
18680 precision - n, spec, props,
18681 risky);
18684 else if (EQ (car, QCpropertize))
18686 /* An element of the form (:propertize ELT PROPS...)
18687 means display ELT but applying properties PROPS. */
18689 if (risky)
18690 break;
18692 if (CONSP (XCDR (elt)))
18693 n += display_mode_element (it, depth, field_width - n,
18694 precision - n, XCAR (XCDR (elt)),
18695 XCDR (XCDR (elt)), risky);
18697 else if (SYMBOLP (car))
18699 tem = Fboundp (car);
18700 elt = XCDR (elt);
18701 if (!CONSP (elt))
18702 goto invalid;
18703 /* elt is now the cdr, and we know it is a cons cell.
18704 Use its car if CAR has a non-nil value. */
18705 if (!NILP (tem))
18707 tem = Fsymbol_value (car);
18708 if (!NILP (tem))
18710 elt = XCAR (elt);
18711 goto tail_recurse;
18714 /* Symbol's value is nil (or symbol is unbound)
18715 Get the cddr of the original list
18716 and if possible find the caddr and use that. */
18717 elt = XCDR (elt);
18718 if (NILP (elt))
18719 break;
18720 else if (!CONSP (elt))
18721 goto invalid;
18722 elt = XCAR (elt);
18723 goto tail_recurse;
18725 else if (INTEGERP (car))
18727 register int lim = XINT (car);
18728 elt = XCDR (elt);
18729 if (lim < 0)
18731 /* Negative int means reduce maximum width. */
18732 if (precision <= 0)
18733 precision = -lim;
18734 else
18735 precision = min (precision, -lim);
18737 else if (lim > 0)
18739 /* Padding specified. Don't let it be more than
18740 current maximum. */
18741 if (precision > 0)
18742 lim = min (precision, lim);
18744 /* If that's more padding than already wanted, queue it.
18745 But don't reduce padding already specified even if
18746 that is beyond the current truncation point. */
18747 field_width = max (lim, field_width);
18749 goto tail_recurse;
18751 else if (STRINGP (car) || CONSP (car))
18753 Lisp_Object halftail = elt;
18754 int len = 0;
18756 while (CONSP (elt)
18757 && (precision <= 0 || n < precision))
18759 n += display_mode_element (it, depth,
18760 /* Do padding only after the last
18761 element in the list. */
18762 (! CONSP (XCDR (elt))
18763 ? field_width - n
18764 : 0),
18765 precision - n, XCAR (elt),
18766 props, risky);
18767 elt = XCDR (elt);
18768 len++;
18769 if ((len & 1) == 0)
18770 halftail = XCDR (halftail);
18771 /* Check for cycle. */
18772 if (EQ (halftail, elt))
18773 break;
18777 break;
18779 default:
18780 invalid:
18781 elt = build_string ("*invalid*");
18782 goto tail_recurse;
18785 /* Pad to FIELD_WIDTH. */
18786 if (field_width > 0 && n < field_width)
18788 switch (mode_line_target)
18790 case MODE_LINE_NOPROP:
18791 case MODE_LINE_TITLE:
18792 n += store_mode_line_noprop ("", field_width - n, 0);
18793 break;
18794 case MODE_LINE_STRING:
18795 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
18796 break;
18797 case MODE_LINE_DISPLAY:
18798 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
18799 0, 0, 0);
18800 break;
18804 return n;
18807 /* Store a mode-line string element in mode_line_string_list.
18809 If STRING is non-null, display that C string. Otherwise, the Lisp
18810 string LISP_STRING is displayed.
18812 FIELD_WIDTH is the minimum number of output glyphs to produce.
18813 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18814 with spaces. FIELD_WIDTH <= 0 means don't pad.
18816 PRECISION is the maximum number of characters to output from
18817 STRING. PRECISION <= 0 means don't truncate the string.
18819 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
18820 properties to the string.
18822 PROPS are the properties to add to the string.
18823 The mode_line_string_face face property is always added to the string.
18826 static int
18827 store_mode_line_string (char *string, Lisp_Object lisp_string, int copy_string,
18828 int field_width, int precision, Lisp_Object props)
18830 int len;
18831 int n = 0;
18833 if (string != NULL)
18835 len = strlen (string);
18836 if (precision > 0 && len > precision)
18837 len = precision;
18838 lisp_string = make_string (string, len);
18839 if (NILP (props))
18840 props = mode_line_string_face_prop;
18841 else if (!NILP (mode_line_string_face))
18843 Lisp_Object face = Fplist_get (props, Qface);
18844 props = Fcopy_sequence (props);
18845 if (NILP (face))
18846 face = mode_line_string_face;
18847 else
18848 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18849 props = Fplist_put (props, Qface, face);
18851 Fadd_text_properties (make_number (0), make_number (len),
18852 props, lisp_string);
18854 else
18856 len = XFASTINT (Flength (lisp_string));
18857 if (precision > 0 && len > precision)
18859 len = precision;
18860 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
18861 precision = -1;
18863 if (!NILP (mode_line_string_face))
18865 Lisp_Object face;
18866 if (NILP (props))
18867 props = Ftext_properties_at (make_number (0), lisp_string);
18868 face = Fplist_get (props, Qface);
18869 if (NILP (face))
18870 face = mode_line_string_face;
18871 else
18872 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
18873 props = Fcons (Qface, Fcons (face, Qnil));
18874 if (copy_string)
18875 lisp_string = Fcopy_sequence (lisp_string);
18877 if (!NILP (props))
18878 Fadd_text_properties (make_number (0), make_number (len),
18879 props, lisp_string);
18882 if (len > 0)
18884 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18885 n += len;
18888 if (field_width > len)
18890 field_width -= len;
18891 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
18892 if (!NILP (props))
18893 Fadd_text_properties (make_number (0), make_number (field_width),
18894 props, lisp_string);
18895 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
18896 n += field_width;
18899 return n;
18903 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
18904 1, 4, 0,
18905 doc: /* Format a string out of a mode line format specification.
18906 First arg FORMAT specifies the mode line format (see `mode-line-format'
18907 for details) to use.
18909 Optional second arg FACE specifies the face property to put
18910 on all characters for which no face is specified.
18911 The value t means whatever face the window's mode line currently uses
18912 \(either `mode-line' or `mode-line-inactive', depending).
18913 A value of nil means the default is no face property.
18914 If FACE is an integer, the value string has no text properties.
18916 Optional third and fourth args WINDOW and BUFFER specify the window
18917 and buffer to use as the context for the formatting (defaults
18918 are the selected window and the window's buffer). */)
18919 (Lisp_Object format, Lisp_Object face, Lisp_Object window, Lisp_Object buffer)
18921 struct it it;
18922 int len;
18923 struct window *w;
18924 struct buffer *old_buffer = NULL;
18925 int face_id = -1;
18926 int no_props = INTEGERP (face);
18927 int count = SPECPDL_INDEX ();
18928 Lisp_Object str;
18929 int string_start = 0;
18931 if (NILP (window))
18932 window = selected_window;
18933 CHECK_WINDOW (window);
18934 w = XWINDOW (window);
18936 if (NILP (buffer))
18937 buffer = w->buffer;
18938 CHECK_BUFFER (buffer);
18940 /* Make formatting the modeline a non-op when noninteractive, otherwise
18941 there will be problems later caused by a partially initialized frame. */
18942 if (NILP (format) || noninteractive)
18943 return empty_unibyte_string;
18945 if (no_props)
18946 face = Qnil;
18948 if (!NILP (face))
18950 if (EQ (face, Qt))
18951 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
18952 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
18955 if (face_id < 0)
18956 face_id = DEFAULT_FACE_ID;
18958 if (XBUFFER (buffer) != current_buffer)
18959 old_buffer = current_buffer;
18961 /* Save things including mode_line_proptrans_alist,
18962 and set that to nil so that we don't alter the outer value. */
18963 record_unwind_protect (unwind_format_mode_line,
18964 format_mode_line_unwind_data
18965 (old_buffer, selected_window, 1));
18966 mode_line_proptrans_alist = Qnil;
18968 Fselect_window (window, Qt);
18969 if (old_buffer)
18970 set_buffer_internal_1 (XBUFFER (buffer));
18972 init_iterator (&it, w, -1, -1, NULL, face_id);
18974 if (no_props)
18976 mode_line_target = MODE_LINE_NOPROP;
18977 mode_line_string_face_prop = Qnil;
18978 mode_line_string_list = Qnil;
18979 string_start = MODE_LINE_NOPROP_LEN (0);
18981 else
18983 mode_line_target = MODE_LINE_STRING;
18984 mode_line_string_list = Qnil;
18985 mode_line_string_face = face;
18986 mode_line_string_face_prop
18987 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18990 push_kboard (FRAME_KBOARD (it.f));
18991 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18992 pop_kboard ();
18994 if (no_props)
18996 len = MODE_LINE_NOPROP_LEN (string_start);
18997 str = make_string (mode_line_noprop_buf + string_start, len);
18999 else
19001 mode_line_string_list = Fnreverse (mode_line_string_list);
19002 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19003 empty_unibyte_string);
19006 unbind_to (count, Qnil);
19007 return str;
19010 /* Write a null-terminated, right justified decimal representation of
19011 the positive integer D to BUF using a minimal field width WIDTH. */
19013 static void
19014 pint2str (register char *buf, register int width, register int d)
19016 register char *p = buf;
19018 if (d <= 0)
19019 *p++ = '0';
19020 else
19022 while (d > 0)
19024 *p++ = d % 10 + '0';
19025 d /= 10;
19029 for (width -= (int) (p - buf); width > 0; --width)
19030 *p++ = ' ';
19031 *p-- = '\0';
19032 while (p > buf)
19034 d = *buf;
19035 *buf++ = *p;
19036 *p-- = d;
19040 /* Write a null-terminated, right justified decimal and "human
19041 readable" representation of the nonnegative integer D to BUF using
19042 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19044 static const char power_letter[] =
19046 0, /* not used */
19047 'k', /* kilo */
19048 'M', /* mega */
19049 'G', /* giga */
19050 'T', /* tera */
19051 'P', /* peta */
19052 'E', /* exa */
19053 'Z', /* zetta */
19054 'Y' /* yotta */
19057 static void
19058 pint2hrstr (char *buf, int width, int d)
19060 /* We aim to represent the nonnegative integer D as
19061 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19062 int quotient = d;
19063 int remainder = 0;
19064 /* -1 means: do not use TENTHS. */
19065 int tenths = -1;
19066 int exponent = 0;
19068 /* Length of QUOTIENT.TENTHS as a string. */
19069 int length;
19071 char * psuffix;
19072 char * p;
19074 if (1000 <= quotient)
19076 /* Scale to the appropriate EXPONENT. */
19079 remainder = quotient % 1000;
19080 quotient /= 1000;
19081 exponent++;
19083 while (1000 <= quotient);
19085 /* Round to nearest and decide whether to use TENTHS or not. */
19086 if (quotient <= 9)
19088 tenths = remainder / 100;
19089 if (50 <= remainder % 100)
19091 if (tenths < 9)
19092 tenths++;
19093 else
19095 quotient++;
19096 if (quotient == 10)
19097 tenths = -1;
19098 else
19099 tenths = 0;
19103 else
19104 if (500 <= remainder)
19106 if (quotient < 999)
19107 quotient++;
19108 else
19110 quotient = 1;
19111 exponent++;
19112 tenths = 0;
19117 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19118 if (tenths == -1 && quotient <= 99)
19119 if (quotient <= 9)
19120 length = 1;
19121 else
19122 length = 2;
19123 else
19124 length = 3;
19125 p = psuffix = buf + max (width, length);
19127 /* Print EXPONENT. */
19128 if (exponent)
19129 *psuffix++ = power_letter[exponent];
19130 *psuffix = '\0';
19132 /* Print TENTHS. */
19133 if (tenths >= 0)
19135 *--p = '0' + tenths;
19136 *--p = '.';
19139 /* Print QUOTIENT. */
19142 int digit = quotient % 10;
19143 *--p = '0' + digit;
19145 while ((quotient /= 10) != 0);
19147 /* Print leading spaces. */
19148 while (buf < p)
19149 *--p = ' ';
19152 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19153 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19154 type of CODING_SYSTEM. Return updated pointer into BUF. */
19156 static unsigned char invalid_eol_type[] = "(*invalid*)";
19158 static char *
19159 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19161 Lisp_Object val;
19162 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
19163 const unsigned char *eol_str;
19164 int eol_str_len;
19165 /* The EOL conversion we are using. */
19166 Lisp_Object eoltype;
19168 val = CODING_SYSTEM_SPEC (coding_system);
19169 eoltype = Qnil;
19171 if (!VECTORP (val)) /* Not yet decided. */
19173 if (multibyte)
19174 *buf++ = '-';
19175 if (eol_flag)
19176 eoltype = eol_mnemonic_undecided;
19177 /* Don't mention EOL conversion if it isn't decided. */
19179 else
19181 Lisp_Object attrs;
19182 Lisp_Object eolvalue;
19184 attrs = AREF (val, 0);
19185 eolvalue = AREF (val, 2);
19187 if (multibyte)
19188 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19190 if (eol_flag)
19192 /* The EOL conversion that is normal on this system. */
19194 if (NILP (eolvalue)) /* Not yet decided. */
19195 eoltype = eol_mnemonic_undecided;
19196 else if (VECTORP (eolvalue)) /* Not yet decided. */
19197 eoltype = eol_mnemonic_undecided;
19198 else /* eolvalue is Qunix, Qdos, or Qmac. */
19199 eoltype = (EQ (eolvalue, Qunix)
19200 ? eol_mnemonic_unix
19201 : (EQ (eolvalue, Qdos) == 1
19202 ? eol_mnemonic_dos : eol_mnemonic_mac));
19206 if (eol_flag)
19208 /* Mention the EOL conversion if it is not the usual one. */
19209 if (STRINGP (eoltype))
19211 eol_str = SDATA (eoltype);
19212 eol_str_len = SBYTES (eoltype);
19214 else if (CHARACTERP (eoltype))
19216 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19217 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19218 eol_str = tmp;
19220 else
19222 eol_str = invalid_eol_type;
19223 eol_str_len = sizeof (invalid_eol_type) - 1;
19225 memcpy (buf, eol_str, eol_str_len);
19226 buf += eol_str_len;
19229 return buf;
19232 /* Return a string for the output of a mode line %-spec for window W,
19233 generated by character C. PRECISION >= 0 means don't return a
19234 string longer than that value. FIELD_WIDTH > 0 means pad the
19235 string returned with spaces to that value. Return a Lisp string in
19236 *STRING if the resulting string is taken from that Lisp string.
19238 Note we operate on the current buffer for most purposes,
19239 the exception being w->base_line_pos. */
19241 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19243 static char *
19244 decode_mode_spec (struct window *w, register int c, int field_width,
19245 int precision, Lisp_Object *string)
19247 Lisp_Object obj;
19248 struct frame *f = XFRAME (WINDOW_FRAME (w));
19249 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19250 struct buffer *b = current_buffer;
19252 obj = Qnil;
19253 *string = Qnil;
19255 switch (c)
19257 case '*':
19258 if (!NILP (b->read_only))
19259 return "%";
19260 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19261 return "*";
19262 return "-";
19264 case '+':
19265 /* This differs from %* only for a modified read-only buffer. */
19266 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19267 return "*";
19268 if (!NILP (b->read_only))
19269 return "%";
19270 return "-";
19272 case '&':
19273 /* This differs from %* in ignoring read-only-ness. */
19274 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19275 return "*";
19276 return "-";
19278 case '%':
19279 return "%";
19281 case '[':
19283 int i;
19284 char *p;
19286 if (command_loop_level > 5)
19287 return "[[[... ";
19288 p = decode_mode_spec_buf;
19289 for (i = 0; i < command_loop_level; i++)
19290 *p++ = '[';
19291 *p = 0;
19292 return decode_mode_spec_buf;
19295 case ']':
19297 int i;
19298 char *p;
19300 if (command_loop_level > 5)
19301 return " ...]]]";
19302 p = decode_mode_spec_buf;
19303 for (i = 0; i < command_loop_level; i++)
19304 *p++ = ']';
19305 *p = 0;
19306 return decode_mode_spec_buf;
19309 case '-':
19311 register int i;
19313 /* Let lots_of_dashes be a string of infinite length. */
19314 if (mode_line_target == MODE_LINE_NOPROP ||
19315 mode_line_target == MODE_LINE_STRING)
19316 return "--";
19317 if (field_width <= 0
19318 || field_width > sizeof (lots_of_dashes))
19320 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
19321 decode_mode_spec_buf[i] = '-';
19322 decode_mode_spec_buf[i] = '\0';
19323 return decode_mode_spec_buf;
19325 else
19326 return lots_of_dashes;
19329 case 'b':
19330 obj = b->name;
19331 break;
19333 case 'c':
19334 /* %c and %l are ignored in `frame-title-format'.
19335 (In redisplay_internal, the frame title is drawn _before_ the
19336 windows are updated, so the stuff which depends on actual
19337 window contents (such as %l) may fail to render properly, or
19338 even crash emacs.) */
19339 if (mode_line_target == MODE_LINE_TITLE)
19340 return "";
19341 else
19343 int col = (int) current_column (); /* iftc */
19344 w->column_number_displayed = make_number (col);
19345 pint2str (decode_mode_spec_buf, field_width, col);
19346 return decode_mode_spec_buf;
19349 case 'e':
19350 #ifndef SYSTEM_MALLOC
19352 if (NILP (Vmemory_full))
19353 return "";
19354 else
19355 return "!MEM FULL! ";
19357 #else
19358 return "";
19359 #endif
19361 case 'F':
19362 /* %F displays the frame name. */
19363 if (!NILP (f->title))
19364 return (char *) SDATA (f->title);
19365 if (f->explicit_name || ! FRAME_WINDOW_P (f))
19366 return (char *) SDATA (f->name);
19367 return "Emacs";
19369 case 'f':
19370 obj = b->filename;
19371 break;
19373 case 'i':
19375 int size = ZV - BEGV;
19376 pint2str (decode_mode_spec_buf, field_width, size);
19377 return decode_mode_spec_buf;
19380 case 'I':
19382 int size = ZV - BEGV;
19383 pint2hrstr (decode_mode_spec_buf, field_width, size);
19384 return decode_mode_spec_buf;
19387 case 'l':
19389 int startpos, startpos_byte, line, linepos, linepos_byte;
19390 int topline, nlines, junk, height;
19392 /* %c and %l are ignored in `frame-title-format'. */
19393 if (mode_line_target == MODE_LINE_TITLE)
19394 return "";
19396 startpos = XMARKER (w->start)->charpos;
19397 startpos_byte = marker_byte_position (w->start);
19398 height = WINDOW_TOTAL_LINES (w);
19400 /* If we decided that this buffer isn't suitable for line numbers,
19401 don't forget that too fast. */
19402 if (EQ (w->base_line_pos, w->buffer))
19403 goto no_value;
19404 /* But do forget it, if the window shows a different buffer now. */
19405 else if (BUFFERP (w->base_line_pos))
19406 w->base_line_pos = Qnil;
19408 /* If the buffer is very big, don't waste time. */
19409 if (INTEGERP (Vline_number_display_limit)
19410 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
19412 w->base_line_pos = Qnil;
19413 w->base_line_number = Qnil;
19414 goto no_value;
19417 if (INTEGERP (w->base_line_number)
19418 && INTEGERP (w->base_line_pos)
19419 && XFASTINT (w->base_line_pos) <= startpos)
19421 line = XFASTINT (w->base_line_number);
19422 linepos = XFASTINT (w->base_line_pos);
19423 linepos_byte = buf_charpos_to_bytepos (b, linepos);
19425 else
19427 line = 1;
19428 linepos = BUF_BEGV (b);
19429 linepos_byte = BUF_BEGV_BYTE (b);
19432 /* Count lines from base line to window start position. */
19433 nlines = display_count_lines (linepos, linepos_byte,
19434 startpos_byte,
19435 startpos, &junk);
19437 topline = nlines + line;
19439 /* Determine a new base line, if the old one is too close
19440 or too far away, or if we did not have one.
19441 "Too close" means it's plausible a scroll-down would
19442 go back past it. */
19443 if (startpos == BUF_BEGV (b))
19445 w->base_line_number = make_number (topline);
19446 w->base_line_pos = make_number (BUF_BEGV (b));
19448 else if (nlines < height + 25 || nlines > height * 3 + 50
19449 || linepos == BUF_BEGV (b))
19451 int limit = BUF_BEGV (b);
19452 int limit_byte = BUF_BEGV_BYTE (b);
19453 int position;
19454 int distance = (height * 2 + 30) * line_number_display_limit_width;
19456 if (startpos - distance > limit)
19458 limit = startpos - distance;
19459 limit_byte = CHAR_TO_BYTE (limit);
19462 nlines = display_count_lines (startpos, startpos_byte,
19463 limit_byte,
19464 - (height * 2 + 30),
19465 &position);
19466 /* If we couldn't find the lines we wanted within
19467 line_number_display_limit_width chars per line,
19468 give up on line numbers for this window. */
19469 if (position == limit_byte && limit == startpos - distance)
19471 w->base_line_pos = w->buffer;
19472 w->base_line_number = Qnil;
19473 goto no_value;
19476 w->base_line_number = make_number (topline - nlines);
19477 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
19480 /* Now count lines from the start pos to point. */
19481 nlines = display_count_lines (startpos, startpos_byte,
19482 PT_BYTE, PT, &junk);
19484 /* Record that we did display the line number. */
19485 line_number_displayed = 1;
19487 /* Make the string to show. */
19488 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
19489 return decode_mode_spec_buf;
19490 no_value:
19492 char* p = decode_mode_spec_buf;
19493 int pad = field_width - 2;
19494 while (pad-- > 0)
19495 *p++ = ' ';
19496 *p++ = '?';
19497 *p++ = '?';
19498 *p = '\0';
19499 return decode_mode_spec_buf;
19502 break;
19504 case 'm':
19505 obj = b->mode_name;
19506 break;
19508 case 'n':
19509 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
19510 return " Narrow";
19511 break;
19513 case 'p':
19515 int pos = marker_position (w->start);
19516 int total = BUF_ZV (b) - BUF_BEGV (b);
19518 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
19520 if (pos <= BUF_BEGV (b))
19521 return "All";
19522 else
19523 return "Bottom";
19525 else if (pos <= BUF_BEGV (b))
19526 return "Top";
19527 else
19529 if (total > 1000000)
19530 /* Do it differently for a large value, to avoid overflow. */
19531 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19532 else
19533 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
19534 /* We can't normally display a 3-digit number,
19535 so get us a 2-digit number that is close. */
19536 if (total == 100)
19537 total = 99;
19538 sprintf (decode_mode_spec_buf, "%2d%%", total);
19539 return decode_mode_spec_buf;
19543 /* Display percentage of size above the bottom of the screen. */
19544 case 'P':
19546 int toppos = marker_position (w->start);
19547 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
19548 int total = BUF_ZV (b) - BUF_BEGV (b);
19550 if (botpos >= BUF_ZV (b))
19552 if (toppos <= BUF_BEGV (b))
19553 return "All";
19554 else
19555 return "Bottom";
19557 else
19559 if (total > 1000000)
19560 /* Do it differently for a large value, to avoid overflow. */
19561 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
19562 else
19563 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
19564 /* We can't normally display a 3-digit number,
19565 so get us a 2-digit number that is close. */
19566 if (total == 100)
19567 total = 99;
19568 if (toppos <= BUF_BEGV (b))
19569 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
19570 else
19571 sprintf (decode_mode_spec_buf, "%2d%%", total);
19572 return decode_mode_spec_buf;
19576 case 's':
19577 /* status of process */
19578 obj = Fget_buffer_process (Fcurrent_buffer ());
19579 if (NILP (obj))
19580 return "no process";
19581 #ifndef MSDOS
19582 obj = Fsymbol_name (Fprocess_status (obj));
19583 #endif
19584 break;
19586 case '@':
19588 int count = inhibit_garbage_collection ();
19589 Lisp_Object val = call1 (intern ("file-remote-p"),
19590 current_buffer->directory);
19591 unbind_to (count, Qnil);
19593 if (NILP (val))
19594 return "-";
19595 else
19596 return "@";
19599 case 't': /* indicate TEXT or BINARY */
19600 #ifdef MODE_LINE_BINARY_TEXT
19601 return MODE_LINE_BINARY_TEXT (b);
19602 #else
19603 return "T";
19604 #endif
19606 case 'z':
19607 /* coding-system (not including end-of-line format) */
19608 case 'Z':
19609 /* coding-system (including end-of-line type) */
19611 int eol_flag = (c == 'Z');
19612 char *p = decode_mode_spec_buf;
19614 if (! FRAME_WINDOW_P (f))
19616 /* No need to mention EOL here--the terminal never needs
19617 to do EOL conversion. */
19618 p = decode_mode_spec_coding (CODING_ID_NAME
19619 (FRAME_KEYBOARD_CODING (f)->id),
19620 p, 0);
19621 p = decode_mode_spec_coding (CODING_ID_NAME
19622 (FRAME_TERMINAL_CODING (f)->id),
19623 p, 0);
19625 p = decode_mode_spec_coding (b->buffer_file_coding_system,
19626 p, eol_flag);
19628 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
19629 #ifdef subprocesses
19630 obj = Fget_buffer_process (Fcurrent_buffer ());
19631 if (PROCESSP (obj))
19633 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
19634 p, eol_flag);
19635 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
19636 p, eol_flag);
19638 #endif /* subprocesses */
19639 #endif /* 0 */
19640 *p = 0;
19641 return decode_mode_spec_buf;
19645 if (STRINGP (obj))
19647 *string = obj;
19648 return (char *) SDATA (obj);
19650 else
19651 return "";
19655 /* Count up to COUNT lines starting from START / START_BYTE.
19656 But don't go beyond LIMIT_BYTE.
19657 Return the number of lines thus found (always nonnegative).
19659 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
19661 static int
19662 display_count_lines (int start, int start_byte, int limit_byte, int count,
19663 int *byte_pos_ptr)
19665 register unsigned char *cursor;
19666 unsigned char *base;
19668 register int ceiling;
19669 register unsigned char *ceiling_addr;
19670 int orig_count = count;
19672 /* If we are not in selective display mode,
19673 check only for newlines. */
19674 int selective_display = (!NILP (current_buffer->selective_display)
19675 && !INTEGERP (current_buffer->selective_display));
19677 if (count > 0)
19679 while (start_byte < limit_byte)
19681 ceiling = BUFFER_CEILING_OF (start_byte);
19682 ceiling = min (limit_byte - 1, ceiling);
19683 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
19684 base = (cursor = BYTE_POS_ADDR (start_byte));
19685 while (1)
19687 if (selective_display)
19688 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
19690 else
19691 while (*cursor != '\n' && ++cursor != ceiling_addr)
19694 if (cursor != ceiling_addr)
19696 if (--count == 0)
19698 start_byte += cursor - base + 1;
19699 *byte_pos_ptr = start_byte;
19700 return orig_count;
19702 else
19703 if (++cursor == ceiling_addr)
19704 break;
19706 else
19707 break;
19709 start_byte += cursor - base;
19712 else
19714 while (start_byte > limit_byte)
19716 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
19717 ceiling = max (limit_byte, ceiling);
19718 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
19719 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
19720 while (1)
19722 if (selective_display)
19723 while (--cursor != ceiling_addr
19724 && *cursor != '\n' && *cursor != 015)
19726 else
19727 while (--cursor != ceiling_addr && *cursor != '\n')
19730 if (cursor != ceiling_addr)
19732 if (++count == 0)
19734 start_byte += cursor - base + 1;
19735 *byte_pos_ptr = start_byte;
19736 /* When scanning backwards, we should
19737 not count the newline posterior to which we stop. */
19738 return - orig_count - 1;
19741 else
19742 break;
19744 /* Here we add 1 to compensate for the last decrement
19745 of CURSOR, which took it past the valid range. */
19746 start_byte += cursor - base + 1;
19750 *byte_pos_ptr = limit_byte;
19752 if (count < 0)
19753 return - orig_count + count;
19754 return orig_count - count;
19760 /***********************************************************************
19761 Displaying strings
19762 ***********************************************************************/
19764 /* Display a NUL-terminated string, starting with index START.
19766 If STRING is non-null, display that C string. Otherwise, the Lisp
19767 string LISP_STRING is displayed. There's a case that STRING is
19768 non-null and LISP_STRING is not nil. It means STRING is a string
19769 data of LISP_STRING. In that case, we display LISP_STRING while
19770 ignoring its text properties.
19772 If FACE_STRING is not nil, FACE_STRING_POS is a position in
19773 FACE_STRING. Display STRING or LISP_STRING with the face at
19774 FACE_STRING_POS in FACE_STRING:
19776 Display the string in the environment given by IT, but use the
19777 standard display table, temporarily.
19779 FIELD_WIDTH is the minimum number of output glyphs to produce.
19780 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19781 with spaces. If STRING has more characters, more than FIELD_WIDTH
19782 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
19784 PRECISION is the maximum number of characters to output from
19785 STRING. PRECISION < 0 means don't truncate the string.
19787 This is roughly equivalent to printf format specifiers:
19789 FIELD_WIDTH PRECISION PRINTF
19790 ----------------------------------------
19791 -1 -1 %s
19792 -1 10 %.10s
19793 10 -1 %10s
19794 20 10 %20.10s
19796 MULTIBYTE zero means do not display multibyte chars, > 0 means do
19797 display them, and < 0 means obey the current buffer's value of
19798 enable_multibyte_characters.
19800 Value is the number of columns displayed. */
19802 static int
19803 display_string (unsigned char *string, Lisp_Object lisp_string, Lisp_Object face_string,
19804 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
19805 int field_width, int precision, int max_x, int multibyte)
19807 int hpos_at_start = it->hpos;
19808 int saved_face_id = it->face_id;
19809 struct glyph_row *row = it->glyph_row;
19811 /* Initialize the iterator IT for iteration over STRING beginning
19812 with index START. */
19813 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
19814 precision, field_width, multibyte);
19815 if (string && STRINGP (lisp_string))
19816 /* LISP_STRING is the one returned by decode_mode_spec. We should
19817 ignore its text properties. */
19818 it->stop_charpos = -1;
19820 /* If displaying STRING, set up the face of the iterator
19821 from LISP_STRING, if that's given. */
19822 if (STRINGP (face_string))
19824 EMACS_INT endptr;
19825 struct face *face;
19827 it->face_id
19828 = face_at_string_position (it->w, face_string, face_string_pos,
19829 0, it->region_beg_charpos,
19830 it->region_end_charpos,
19831 &endptr, it->base_face_id, 0);
19832 face = FACE_FROM_ID (it->f, it->face_id);
19833 it->face_box_p = face->box != FACE_NO_BOX;
19836 /* Set max_x to the maximum allowed X position. Don't let it go
19837 beyond the right edge of the window. */
19838 if (max_x <= 0)
19839 max_x = it->last_visible_x;
19840 else
19841 max_x = min (max_x, it->last_visible_x);
19843 /* Skip over display elements that are not visible. because IT->w is
19844 hscrolled. */
19845 if (it->current_x < it->first_visible_x)
19846 move_it_in_display_line_to (it, 100000, it->first_visible_x,
19847 MOVE_TO_POS | MOVE_TO_X);
19849 row->ascent = it->max_ascent;
19850 row->height = it->max_ascent + it->max_descent;
19851 row->phys_ascent = it->max_phys_ascent;
19852 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19853 row->extra_line_spacing = it->max_extra_line_spacing;
19855 /* This condition is for the case that we are called with current_x
19856 past last_visible_x. */
19857 while (it->current_x < max_x)
19859 int x_before, x, n_glyphs_before, i, nglyphs;
19861 /* Get the next display element. */
19862 if (!get_next_display_element (it))
19863 break;
19865 /* Produce glyphs. */
19866 x_before = it->current_x;
19867 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
19868 PRODUCE_GLYPHS (it);
19870 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
19871 i = 0;
19872 x = x_before;
19873 while (i < nglyphs)
19875 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19877 if (it->line_wrap != TRUNCATE
19878 && x + glyph->pixel_width > max_x)
19880 /* End of continued line or max_x reached. */
19881 if (CHAR_GLYPH_PADDING_P (*glyph))
19883 /* A wide character is unbreakable. */
19884 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
19885 it->current_x = x_before;
19887 else
19889 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
19890 it->current_x = x;
19892 break;
19894 else if (x + glyph->pixel_width >= it->first_visible_x)
19896 /* Glyph is at least partially visible. */
19897 ++it->hpos;
19898 if (x < it->first_visible_x)
19899 it->glyph_row->x = x - it->first_visible_x;
19901 else
19903 /* Glyph is off the left margin of the display area.
19904 Should not happen. */
19905 abort ();
19908 row->ascent = max (row->ascent, it->max_ascent);
19909 row->height = max (row->height, it->max_ascent + it->max_descent);
19910 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19911 row->phys_height = max (row->phys_height,
19912 it->max_phys_ascent + it->max_phys_descent);
19913 row->extra_line_spacing = max (row->extra_line_spacing,
19914 it->max_extra_line_spacing);
19915 x += glyph->pixel_width;
19916 ++i;
19919 /* Stop if max_x reached. */
19920 if (i < nglyphs)
19921 break;
19923 /* Stop at line ends. */
19924 if (ITERATOR_AT_END_OF_LINE_P (it))
19926 it->continuation_lines_width = 0;
19927 break;
19930 set_iterator_to_next (it, 1);
19932 /* Stop if truncating at the right edge. */
19933 if (it->line_wrap == TRUNCATE
19934 && it->current_x >= it->last_visible_x)
19936 /* Add truncation mark, but don't do it if the line is
19937 truncated at a padding space. */
19938 if (IT_CHARPOS (*it) < it->string_nchars)
19940 if (!FRAME_WINDOW_P (it->f))
19942 int i, n;
19944 if (it->current_x > it->last_visible_x)
19946 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19947 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19948 break;
19949 for (n = row->used[TEXT_AREA]; i < n; ++i)
19951 row->used[TEXT_AREA] = i;
19952 produce_special_glyphs (it, IT_TRUNCATION);
19955 produce_special_glyphs (it, IT_TRUNCATION);
19957 it->glyph_row->truncated_on_right_p = 1;
19959 break;
19963 /* Maybe insert a truncation at the left. */
19964 if (it->first_visible_x
19965 && IT_CHARPOS (*it) > 0)
19967 if (!FRAME_WINDOW_P (it->f))
19968 insert_left_trunc_glyphs (it);
19969 it->glyph_row->truncated_on_left_p = 1;
19972 it->face_id = saved_face_id;
19974 /* Value is number of columns displayed. */
19975 return it->hpos - hpos_at_start;
19980 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19981 appears as an element of LIST or as the car of an element of LIST.
19982 If PROPVAL is a list, compare each element against LIST in that
19983 way, and return 1/2 if any element of PROPVAL is found in LIST.
19984 Otherwise return 0. This function cannot quit.
19985 The return value is 2 if the text is invisible but with an ellipsis
19986 and 1 if it's invisible and without an ellipsis. */
19989 invisible_p (register Lisp_Object propval, Lisp_Object list)
19991 register Lisp_Object tail, proptail;
19993 for (tail = list; CONSP (tail); tail = XCDR (tail))
19995 register Lisp_Object tem;
19996 tem = XCAR (tail);
19997 if (EQ (propval, tem))
19998 return 1;
19999 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20000 return NILP (XCDR (tem)) ? 1 : 2;
20003 if (CONSP (propval))
20005 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20007 Lisp_Object propelt;
20008 propelt = XCAR (proptail);
20009 for (tail = list; CONSP (tail); tail = XCDR (tail))
20011 register Lisp_Object tem;
20012 tem = XCAR (tail);
20013 if (EQ (propelt, tem))
20014 return 1;
20015 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20016 return NILP (XCDR (tem)) ? 1 : 2;
20021 return 0;
20024 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20025 doc: /* Non-nil if the property makes the text invisible.
20026 POS-OR-PROP can be a marker or number, in which case it is taken to be
20027 a position in the current buffer and the value of the `invisible' property
20028 is checked; or it can be some other value, which is then presumed to be the
20029 value of the `invisible' property of the text of interest.
20030 The non-nil value returned can be t for truly invisible text or something
20031 else if the text is replaced by an ellipsis. */)
20032 (Lisp_Object pos_or_prop)
20034 Lisp_Object prop
20035 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20036 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20037 : pos_or_prop);
20038 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20039 return (invis == 0 ? Qnil
20040 : invis == 1 ? Qt
20041 : make_number (invis));
20044 /* Calculate a width or height in pixels from a specification using
20045 the following elements:
20047 SPEC ::=
20048 NUM - a (fractional) multiple of the default font width/height
20049 (NUM) - specifies exactly NUM pixels
20050 UNIT - a fixed number of pixels, see below.
20051 ELEMENT - size of a display element in pixels, see below.
20052 (NUM . SPEC) - equals NUM * SPEC
20053 (+ SPEC SPEC ...) - add pixel values
20054 (- SPEC SPEC ...) - subtract pixel values
20055 (- SPEC) - negate pixel value
20057 NUM ::=
20058 INT or FLOAT - a number constant
20059 SYMBOL - use symbol's (buffer local) variable binding.
20061 UNIT ::=
20062 in - pixels per inch *)
20063 mm - pixels per 1/1000 meter *)
20064 cm - pixels per 1/100 meter *)
20065 width - width of current font in pixels.
20066 height - height of current font in pixels.
20068 *) using the ratio(s) defined in display-pixels-per-inch.
20070 ELEMENT ::=
20072 left-fringe - left fringe width in pixels
20073 right-fringe - right fringe width in pixels
20075 left-margin - left margin width in pixels
20076 right-margin - right margin width in pixels
20078 scroll-bar - scroll-bar area width in pixels
20080 Examples:
20082 Pixels corresponding to 5 inches:
20083 (5 . in)
20085 Total width of non-text areas on left side of window (if scroll-bar is on left):
20086 '(space :width (+ left-fringe left-margin scroll-bar))
20088 Align to first text column (in header line):
20089 '(space :align-to 0)
20091 Align to middle of text area minus half the width of variable `my-image'
20092 containing a loaded image:
20093 '(space :align-to (0.5 . (- text my-image)))
20095 Width of left margin minus width of 1 character in the default font:
20096 '(space :width (- left-margin 1))
20098 Width of left margin minus width of 2 characters in the current font:
20099 '(space :width (- left-margin (2 . width)))
20101 Center 1 character over left-margin (in header line):
20102 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20104 Different ways to express width of left fringe plus left margin minus one pixel:
20105 '(space :width (- (+ left-fringe left-margin) (1)))
20106 '(space :width (+ left-fringe left-margin (- (1))))
20107 '(space :width (+ left-fringe left-margin (-1)))
20111 #define NUMVAL(X) \
20112 ((INTEGERP (X) || FLOATP (X)) \
20113 ? XFLOATINT (X) \
20114 : - 1)
20117 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20118 struct font *font, int width_p, int *align_to)
20120 double pixels;
20122 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20123 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20125 if (NILP (prop))
20126 return OK_PIXELS (0);
20128 xassert (FRAME_LIVE_P (it->f));
20130 if (SYMBOLP (prop))
20132 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20134 char *unit = SDATA (SYMBOL_NAME (prop));
20136 if (unit[0] == 'i' && unit[1] == 'n')
20137 pixels = 1.0;
20138 else if (unit[0] == 'm' && unit[1] == 'm')
20139 pixels = 25.4;
20140 else if (unit[0] == 'c' && unit[1] == 'm')
20141 pixels = 2.54;
20142 else
20143 pixels = 0;
20144 if (pixels > 0)
20146 double ppi;
20147 #ifdef HAVE_WINDOW_SYSTEM
20148 if (FRAME_WINDOW_P (it->f)
20149 && (ppi = (width_p
20150 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20151 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20152 ppi > 0))
20153 return OK_PIXELS (ppi / pixels);
20154 #endif
20156 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20157 || (CONSP (Vdisplay_pixels_per_inch)
20158 && (ppi = (width_p
20159 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20160 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20161 ppi > 0)))
20162 return OK_PIXELS (ppi / pixels);
20164 return 0;
20168 #ifdef HAVE_WINDOW_SYSTEM
20169 if (EQ (prop, Qheight))
20170 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20171 if (EQ (prop, Qwidth))
20172 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20173 #else
20174 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20175 return OK_PIXELS (1);
20176 #endif
20178 if (EQ (prop, Qtext))
20179 return OK_PIXELS (width_p
20180 ? window_box_width (it->w, TEXT_AREA)
20181 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20183 if (align_to && *align_to < 0)
20185 *res = 0;
20186 if (EQ (prop, Qleft))
20187 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20188 if (EQ (prop, Qright))
20189 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20190 if (EQ (prop, Qcenter))
20191 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20192 + window_box_width (it->w, TEXT_AREA) / 2);
20193 if (EQ (prop, Qleft_fringe))
20194 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20195 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20196 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20197 if (EQ (prop, Qright_fringe))
20198 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20199 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20200 : window_box_right_offset (it->w, TEXT_AREA));
20201 if (EQ (prop, Qleft_margin))
20202 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20203 if (EQ (prop, Qright_margin))
20204 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20205 if (EQ (prop, Qscroll_bar))
20206 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20208 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20209 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20210 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20211 : 0)));
20213 else
20215 if (EQ (prop, Qleft_fringe))
20216 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20217 if (EQ (prop, Qright_fringe))
20218 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20219 if (EQ (prop, Qleft_margin))
20220 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20221 if (EQ (prop, Qright_margin))
20222 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20223 if (EQ (prop, Qscroll_bar))
20224 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20227 prop = Fbuffer_local_value (prop, it->w->buffer);
20230 if (INTEGERP (prop) || FLOATP (prop))
20232 int base_unit = (width_p
20233 ? FRAME_COLUMN_WIDTH (it->f)
20234 : FRAME_LINE_HEIGHT (it->f));
20235 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20238 if (CONSP (prop))
20240 Lisp_Object car = XCAR (prop);
20241 Lisp_Object cdr = XCDR (prop);
20243 if (SYMBOLP (car))
20245 #ifdef HAVE_WINDOW_SYSTEM
20246 if (FRAME_WINDOW_P (it->f)
20247 && valid_image_p (prop))
20249 int id = lookup_image (it->f, prop);
20250 struct image *img = IMAGE_FROM_ID (it->f, id);
20252 return OK_PIXELS (width_p ? img->width : img->height);
20254 #endif
20255 if (EQ (car, Qplus) || EQ (car, Qminus))
20257 int first = 1;
20258 double px;
20260 pixels = 0;
20261 while (CONSP (cdr))
20263 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
20264 font, width_p, align_to))
20265 return 0;
20266 if (first)
20267 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
20268 else
20269 pixels += px;
20270 cdr = XCDR (cdr);
20272 if (EQ (car, Qminus))
20273 pixels = -pixels;
20274 return OK_PIXELS (pixels);
20277 car = Fbuffer_local_value (car, it->w->buffer);
20280 if (INTEGERP (car) || FLOATP (car))
20282 double fact;
20283 pixels = XFLOATINT (car);
20284 if (NILP (cdr))
20285 return OK_PIXELS (pixels);
20286 if (calc_pixel_width_or_height (&fact, it, cdr,
20287 font, width_p, align_to))
20288 return OK_PIXELS (pixels * fact);
20289 return 0;
20292 return 0;
20295 return 0;
20299 /***********************************************************************
20300 Glyph Display
20301 ***********************************************************************/
20303 #ifdef HAVE_WINDOW_SYSTEM
20305 #if GLYPH_DEBUG
20307 void
20308 dump_glyph_string (s)
20309 struct glyph_string *s;
20311 fprintf (stderr, "glyph string\n");
20312 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
20313 s->x, s->y, s->width, s->height);
20314 fprintf (stderr, " ybase = %d\n", s->ybase);
20315 fprintf (stderr, " hl = %d\n", s->hl);
20316 fprintf (stderr, " left overhang = %d, right = %d\n",
20317 s->left_overhang, s->right_overhang);
20318 fprintf (stderr, " nchars = %d\n", s->nchars);
20319 fprintf (stderr, " extends to end of line = %d\n",
20320 s->extends_to_end_of_line_p);
20321 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
20322 fprintf (stderr, " bg width = %d\n", s->background_width);
20325 #endif /* GLYPH_DEBUG */
20327 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
20328 of XChar2b structures for S; it can't be allocated in
20329 init_glyph_string because it must be allocated via `alloca'. W
20330 is the window on which S is drawn. ROW and AREA are the glyph row
20331 and area within the row from which S is constructed. START is the
20332 index of the first glyph structure covered by S. HL is a
20333 face-override for drawing S. */
20335 #ifdef HAVE_NTGUI
20336 #define OPTIONAL_HDC(hdc) HDC hdc,
20337 #define DECLARE_HDC(hdc) HDC hdc;
20338 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
20339 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
20340 #endif
20342 #ifndef OPTIONAL_HDC
20343 #define OPTIONAL_HDC(hdc)
20344 #define DECLARE_HDC(hdc)
20345 #define ALLOCATE_HDC(hdc, f)
20346 #define RELEASE_HDC(hdc, f)
20347 #endif
20349 static void
20350 init_glyph_string (struct glyph_string *s,
20351 OPTIONAL_HDC (hdc)
20352 XChar2b *char2b, struct window *w, struct glyph_row *row,
20353 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
20355 memset (s, 0, sizeof *s);
20356 s->w = w;
20357 s->f = XFRAME (w->frame);
20358 #ifdef HAVE_NTGUI
20359 s->hdc = hdc;
20360 #endif
20361 s->display = FRAME_X_DISPLAY (s->f);
20362 s->window = FRAME_X_WINDOW (s->f);
20363 s->char2b = char2b;
20364 s->hl = hl;
20365 s->row = row;
20366 s->area = area;
20367 s->first_glyph = row->glyphs[area] + start;
20368 s->height = row->height;
20369 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
20370 s->ybase = s->y + row->ascent;
20374 /* Append the list of glyph strings with head H and tail T to the list
20375 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
20377 static INLINE void
20378 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20379 struct glyph_string *h, struct glyph_string *t)
20381 if (h)
20383 if (*head)
20384 (*tail)->next = h;
20385 else
20386 *head = h;
20387 h->prev = *tail;
20388 *tail = t;
20393 /* Prepend the list of glyph strings with head H and tail T to the
20394 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
20395 result. */
20397 static INLINE void
20398 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
20399 struct glyph_string *h, struct glyph_string *t)
20401 if (h)
20403 if (*head)
20404 (*head)->prev = t;
20405 else
20406 *tail = t;
20407 t->next = *head;
20408 *head = h;
20413 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
20414 Set *HEAD and *TAIL to the resulting list. */
20416 static INLINE void
20417 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
20418 struct glyph_string *s)
20420 s->next = s->prev = NULL;
20421 append_glyph_string_lists (head, tail, s, s);
20425 /* Get face and two-byte form of character C in face FACE_ID on frame
20426 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
20427 means we want to display multibyte text. DISPLAY_P non-zero means
20428 make sure that X resources for the face returned are allocated.
20429 Value is a pointer to a realized face that is ready for display if
20430 DISPLAY_P is non-zero. */
20432 static INLINE struct face *
20433 get_char_face_and_encoding (struct frame *f, int c, int face_id,
20434 XChar2b *char2b, int multibyte_p, int display_p)
20436 struct face *face = FACE_FROM_ID (f, face_id);
20438 if (face->font)
20440 unsigned code = face->font->driver->encode_char (face->font, c);
20442 if (code != FONT_INVALID_CODE)
20443 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20444 else
20445 STORE_XCHAR2B (char2b, 0, 0);
20448 /* Make sure X resources of the face are allocated. */
20449 #ifdef HAVE_X_WINDOWS
20450 if (display_p)
20451 #endif
20453 xassert (face != NULL);
20454 PREPARE_FACE_FOR_DISPLAY (f, face);
20457 return face;
20461 /* Get face and two-byte form of character glyph GLYPH on frame F.
20462 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
20463 a pointer to a realized face that is ready for display. */
20465 static INLINE struct face *
20466 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
20467 XChar2b *char2b, int *two_byte_p)
20469 struct face *face;
20471 xassert (glyph->type == CHAR_GLYPH);
20472 face = FACE_FROM_ID (f, glyph->face_id);
20474 if (two_byte_p)
20475 *two_byte_p = 0;
20477 if (face->font)
20479 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
20481 if (code != FONT_INVALID_CODE)
20482 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
20483 else
20484 STORE_XCHAR2B (char2b, 0, 0);
20487 /* Make sure X resources of the face are allocated. */
20488 xassert (face != NULL);
20489 PREPARE_FACE_FOR_DISPLAY (f, face);
20490 return face;
20494 /* Fill glyph string S with composition components specified by S->cmp.
20496 BASE_FACE is the base face of the composition.
20497 S->cmp_from is the index of the first component for S.
20499 OVERLAPS non-zero means S should draw the foreground only, and use
20500 its physical height for clipping. See also draw_glyphs.
20502 Value is the index of a component not in S. */
20504 static int
20505 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
20506 int overlaps)
20508 int i;
20509 /* For all glyphs of this composition, starting at the offset
20510 S->cmp_from, until we reach the end of the definition or encounter a
20511 glyph that requires the different face, add it to S. */
20512 struct face *face;
20514 xassert (s);
20516 s->for_overlaps = overlaps;
20517 s->face = NULL;
20518 s->font = NULL;
20519 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
20521 int c = COMPOSITION_GLYPH (s->cmp, i);
20523 if (c != '\t')
20525 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
20526 -1, Qnil);
20528 face = get_char_face_and_encoding (s->f, c, face_id,
20529 s->char2b + i, 1, 1);
20530 if (face)
20532 if (! s->face)
20534 s->face = face;
20535 s->font = s->face->font;
20537 else if (s->face != face)
20538 break;
20541 ++s->nchars;
20543 s->cmp_to = i;
20545 /* All glyph strings for the same composition has the same width,
20546 i.e. the width set for the first component of the composition. */
20547 s->width = s->first_glyph->pixel_width;
20549 /* If the specified font could not be loaded, use the frame's
20550 default font, but record the fact that we couldn't load it in
20551 the glyph string so that we can draw rectangles for the
20552 characters of the glyph string. */
20553 if (s->font == NULL)
20555 s->font_not_found_p = 1;
20556 s->font = FRAME_FONT (s->f);
20559 /* Adjust base line for subscript/superscript text. */
20560 s->ybase += s->first_glyph->voffset;
20562 /* This glyph string must always be drawn with 16-bit functions. */
20563 s->two_byte_p = 1;
20565 return s->cmp_to;
20568 static int
20569 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
20570 int start, int end, int overlaps)
20572 struct glyph *glyph, *last;
20573 Lisp_Object lgstring;
20574 int i;
20576 s->for_overlaps = overlaps;
20577 glyph = s->row->glyphs[s->area] + start;
20578 last = s->row->glyphs[s->area] + end;
20579 s->cmp_id = glyph->u.cmp.id;
20580 s->cmp_from = glyph->u.cmp.from;
20581 s->cmp_to = glyph->u.cmp.to + 1;
20582 s->face = FACE_FROM_ID (s->f, face_id);
20583 lgstring = composition_gstring_from_id (s->cmp_id);
20584 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
20585 glyph++;
20586 while (glyph < last
20587 && glyph->u.cmp.automatic
20588 && glyph->u.cmp.id == s->cmp_id
20589 && s->cmp_to == glyph->u.cmp.from)
20590 s->cmp_to = (glyph++)->u.cmp.to + 1;
20592 for (i = s->cmp_from; i < s->cmp_to; i++)
20594 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
20595 unsigned code = LGLYPH_CODE (lglyph);
20597 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
20599 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
20600 return glyph - s->row->glyphs[s->area];
20604 /* Fill glyph string S from a sequence of character glyphs.
20606 FACE_ID is the face id of the string. START is the index of the
20607 first glyph to consider, END is the index of the last + 1.
20608 OVERLAPS non-zero means S should draw the foreground only, and use
20609 its physical height for clipping. See also draw_glyphs.
20611 Value is the index of the first glyph not in S. */
20613 static int
20614 fill_glyph_string (struct glyph_string *s, int face_id,
20615 int start, int end, int overlaps)
20617 struct glyph *glyph, *last;
20618 int voffset;
20619 int glyph_not_available_p;
20621 xassert (s->f == XFRAME (s->w->frame));
20622 xassert (s->nchars == 0);
20623 xassert (start >= 0 && end > start);
20625 s->for_overlaps = overlaps;
20626 glyph = s->row->glyphs[s->area] + start;
20627 last = s->row->glyphs[s->area] + end;
20628 voffset = glyph->voffset;
20629 s->padding_p = glyph->padding_p;
20630 glyph_not_available_p = glyph->glyph_not_available_p;
20632 while (glyph < last
20633 && glyph->type == CHAR_GLYPH
20634 && glyph->voffset == voffset
20635 /* Same face id implies same font, nowadays. */
20636 && glyph->face_id == face_id
20637 && glyph->glyph_not_available_p == glyph_not_available_p)
20639 int two_byte_p;
20641 s->face = get_glyph_face_and_encoding (s->f, glyph,
20642 s->char2b + s->nchars,
20643 &two_byte_p);
20644 s->two_byte_p = two_byte_p;
20645 ++s->nchars;
20646 xassert (s->nchars <= end - start);
20647 s->width += glyph->pixel_width;
20648 if (glyph++->padding_p != s->padding_p)
20649 break;
20652 s->font = s->face->font;
20654 /* If the specified font could not be loaded, use the frame's font,
20655 but record the fact that we couldn't load it in
20656 S->font_not_found_p so that we can draw rectangles for the
20657 characters of the glyph string. */
20658 if (s->font == NULL || glyph_not_available_p)
20660 s->font_not_found_p = 1;
20661 s->font = FRAME_FONT (s->f);
20664 /* Adjust base line for subscript/superscript text. */
20665 s->ybase += voffset;
20667 xassert (s->face && s->face->gc);
20668 return glyph - s->row->glyphs[s->area];
20672 /* Fill glyph string S from image glyph S->first_glyph. */
20674 static void
20675 fill_image_glyph_string (struct glyph_string *s)
20677 xassert (s->first_glyph->type == IMAGE_GLYPH);
20678 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
20679 xassert (s->img);
20680 s->slice = s->first_glyph->slice;
20681 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
20682 s->font = s->face->font;
20683 s->width = s->first_glyph->pixel_width;
20685 /* Adjust base line for subscript/superscript text. */
20686 s->ybase += s->first_glyph->voffset;
20690 /* Fill glyph string S from a sequence of stretch glyphs.
20692 ROW is the glyph row in which the glyphs are found, AREA is the
20693 area within the row. START is the index of the first glyph to
20694 consider, END is the index of the last + 1.
20696 Value is the index of the first glyph not in S. */
20698 static int
20699 fill_stretch_glyph_string (struct glyph_string *s, struct glyph_row *row,
20700 enum glyph_row_area area, int start, int end)
20702 struct glyph *glyph, *last;
20703 int voffset, face_id;
20705 xassert (s->first_glyph->type == STRETCH_GLYPH);
20707 glyph = s->row->glyphs[s->area] + start;
20708 last = s->row->glyphs[s->area] + end;
20709 face_id = glyph->face_id;
20710 s->face = FACE_FROM_ID (s->f, face_id);
20711 s->font = s->face->font;
20712 s->width = glyph->pixel_width;
20713 s->nchars = 1;
20714 voffset = glyph->voffset;
20716 for (++glyph;
20717 (glyph < last
20718 && glyph->type == STRETCH_GLYPH
20719 && glyph->voffset == voffset
20720 && glyph->face_id == face_id);
20721 ++glyph)
20722 s->width += glyph->pixel_width;
20724 /* Adjust base line for subscript/superscript text. */
20725 s->ybase += voffset;
20727 /* The case that face->gc == 0 is handled when drawing the glyph
20728 string by calling PREPARE_FACE_FOR_DISPLAY. */
20729 xassert (s->face);
20730 return glyph - s->row->glyphs[s->area];
20733 static struct font_metrics *
20734 get_per_char_metric (struct frame *f, struct font *font, XChar2b *char2b)
20736 static struct font_metrics metrics;
20737 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
20739 if (! font || code == FONT_INVALID_CODE)
20740 return NULL;
20741 font->driver->text_extents (font, &code, 1, &metrics);
20742 return &metrics;
20745 /* EXPORT for RIF:
20746 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
20747 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
20748 assumed to be zero. */
20750 void
20751 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
20753 *left = *right = 0;
20755 if (glyph->type == CHAR_GLYPH)
20757 struct face *face;
20758 XChar2b char2b;
20759 struct font_metrics *pcm;
20761 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
20762 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
20764 if (pcm->rbearing > pcm->width)
20765 *right = pcm->rbearing - pcm->width;
20766 if (pcm->lbearing < 0)
20767 *left = -pcm->lbearing;
20770 else if (glyph->type == COMPOSITE_GLYPH)
20772 if (! glyph->u.cmp.automatic)
20774 struct composition *cmp = composition_table[glyph->u.cmp.id];
20776 if (cmp->rbearing > cmp->pixel_width)
20777 *right = cmp->rbearing - cmp->pixel_width;
20778 if (cmp->lbearing < 0)
20779 *left = - cmp->lbearing;
20781 else
20783 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
20784 struct font_metrics metrics;
20786 composition_gstring_width (gstring, glyph->u.cmp.from,
20787 glyph->u.cmp.to + 1, &metrics);
20788 if (metrics.rbearing > metrics.width)
20789 *right = metrics.rbearing - metrics.width;
20790 if (metrics.lbearing < 0)
20791 *left = - metrics.lbearing;
20797 /* Return the index of the first glyph preceding glyph string S that
20798 is overwritten by S because of S's left overhang. Value is -1
20799 if no glyphs are overwritten. */
20801 static int
20802 left_overwritten (struct glyph_string *s)
20804 int k;
20806 if (s->left_overhang)
20808 int x = 0, i;
20809 struct glyph *glyphs = s->row->glyphs[s->area];
20810 int first = s->first_glyph - glyphs;
20812 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
20813 x -= glyphs[i].pixel_width;
20815 k = i + 1;
20817 else
20818 k = -1;
20820 return k;
20824 /* Return the index of the first glyph preceding glyph string S that
20825 is overwriting S because of its right overhang. Value is -1 if no
20826 glyph in front of S overwrites S. */
20828 static int
20829 left_overwriting (struct glyph_string *s)
20831 int i, k, x;
20832 struct glyph *glyphs = s->row->glyphs[s->area];
20833 int first = s->first_glyph - glyphs;
20835 k = -1;
20836 x = 0;
20837 for (i = first - 1; i >= 0; --i)
20839 int left, right;
20840 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20841 if (x + right > 0)
20842 k = i;
20843 x -= glyphs[i].pixel_width;
20846 return k;
20850 /* Return the index of the last glyph following glyph string S that is
20851 overwritten by S because of S's right overhang. Value is -1 if
20852 no such glyph is found. */
20854 static int
20855 right_overwritten (struct glyph_string *s)
20857 int k = -1;
20859 if (s->right_overhang)
20861 int x = 0, i;
20862 struct glyph *glyphs = s->row->glyphs[s->area];
20863 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20864 int end = s->row->used[s->area];
20866 for (i = first; i < end && s->right_overhang > x; ++i)
20867 x += glyphs[i].pixel_width;
20869 k = i;
20872 return k;
20876 /* Return the index of the last glyph following glyph string S that
20877 overwrites S because of its left overhang. Value is negative
20878 if no such glyph is found. */
20880 static int
20881 right_overwriting (struct glyph_string *s)
20883 int i, k, x;
20884 int end = s->row->used[s->area];
20885 struct glyph *glyphs = s->row->glyphs[s->area];
20886 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
20888 k = -1;
20889 x = 0;
20890 for (i = first; i < end; ++i)
20892 int left, right;
20893 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20894 if (x - left < 0)
20895 k = i;
20896 x += glyphs[i].pixel_width;
20899 return k;
20903 /* Set background width of glyph string S. START is the index of the
20904 first glyph following S. LAST_X is the right-most x-position + 1
20905 in the drawing area. */
20907 static INLINE void
20908 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
20910 /* If the face of this glyph string has to be drawn to the end of
20911 the drawing area, set S->extends_to_end_of_line_p. */
20913 if (start == s->row->used[s->area]
20914 && s->area == TEXT_AREA
20915 && ((s->row->fill_line_p
20916 && (s->hl == DRAW_NORMAL_TEXT
20917 || s->hl == DRAW_IMAGE_RAISED
20918 || s->hl == DRAW_IMAGE_SUNKEN))
20919 || s->hl == DRAW_MOUSE_FACE))
20920 s->extends_to_end_of_line_p = 1;
20922 /* If S extends its face to the end of the line, set its
20923 background_width to the distance to the right edge of the drawing
20924 area. */
20925 if (s->extends_to_end_of_line_p)
20926 s->background_width = last_x - s->x + 1;
20927 else
20928 s->background_width = s->width;
20932 /* Compute overhangs and x-positions for glyph string S and its
20933 predecessors, or successors. X is the starting x-position for S.
20934 BACKWARD_P non-zero means process predecessors. */
20936 static void
20937 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
20939 if (backward_p)
20941 while (s)
20943 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20944 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20945 x -= s->width;
20946 s->x = x;
20947 s = s->prev;
20950 else
20952 while (s)
20954 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20955 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20956 s->x = x;
20957 x += s->width;
20958 s = s->next;
20965 /* The following macros are only called from draw_glyphs below.
20966 They reference the following parameters of that function directly:
20967 `w', `row', `area', and `overlap_p'
20968 as well as the following local variables:
20969 `s', `f', and `hdc' (in W32) */
20971 #ifdef HAVE_NTGUI
20972 /* On W32, silently add local `hdc' variable to argument list of
20973 init_glyph_string. */
20974 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20975 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20976 #else
20977 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20978 init_glyph_string (s, char2b, w, row, area, start, hl)
20979 #endif
20981 /* Add a glyph string for a stretch glyph to the list of strings
20982 between HEAD and TAIL. START is the index of the stretch glyph in
20983 row area AREA of glyph row ROW. END is the index of the last glyph
20984 in that glyph row area. X is the current output position assigned
20985 to the new glyph string constructed. HL overrides that face of the
20986 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20987 is the right-most x-position of the drawing area. */
20989 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20990 and below -- keep them on one line. */
20991 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20992 do \
20994 s = (struct glyph_string *) alloca (sizeof *s); \
20995 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20996 START = fill_stretch_glyph_string (s, row, area, START, END); \
20997 append_glyph_string (&HEAD, &TAIL, s); \
20998 s->x = (X); \
21000 while (0)
21003 /* Add a glyph string for an image glyph to the list of strings
21004 between HEAD and TAIL. START is the index of the image glyph in
21005 row area AREA of glyph row ROW. END is the index of the last glyph
21006 in that glyph row area. X is the current output position assigned
21007 to the new glyph string constructed. HL overrides that face of the
21008 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21009 is the right-most x-position of the drawing area. */
21011 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21012 do \
21014 s = (struct glyph_string *) alloca (sizeof *s); \
21015 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21016 fill_image_glyph_string (s); \
21017 append_glyph_string (&HEAD, &TAIL, s); \
21018 ++START; \
21019 s->x = (X); \
21021 while (0)
21024 /* Add a glyph string for a sequence of character glyphs to the list
21025 of strings between HEAD and TAIL. START is the index of the first
21026 glyph in row area AREA of glyph row ROW that is part of the new
21027 glyph string. END is the index of the last glyph in that glyph row
21028 area. X is the current output position assigned to the new glyph
21029 string constructed. HL overrides that face of the glyph; e.g. it
21030 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21031 right-most x-position of the drawing area. */
21033 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21034 do \
21036 int face_id; \
21037 XChar2b *char2b; \
21039 face_id = (row)->glyphs[area][START].face_id; \
21041 s = (struct glyph_string *) alloca (sizeof *s); \
21042 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21043 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21044 append_glyph_string (&HEAD, &TAIL, s); \
21045 s->x = (X); \
21046 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21048 while (0)
21051 /* Add a glyph string for a composite sequence to the list of strings
21052 between HEAD and TAIL. START is the index of the first glyph in
21053 row area AREA of glyph row ROW that is part of the new glyph
21054 string. END is the index of the last glyph in that glyph row area.
21055 X is the current output position assigned to the new glyph string
21056 constructed. HL overrides that face of the glyph; e.g. it is
21057 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21058 x-position of the drawing area. */
21060 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21061 do { \
21062 int face_id = (row)->glyphs[area][START].face_id; \
21063 struct face *base_face = FACE_FROM_ID (f, face_id); \
21064 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21065 struct composition *cmp = composition_table[cmp_id]; \
21066 XChar2b *char2b; \
21067 struct glyph_string *first_s; \
21068 int n; \
21070 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21072 /* Make glyph_strings for each glyph sequence that is drawable by \
21073 the same face, and append them to HEAD/TAIL. */ \
21074 for (n = 0; n < cmp->glyph_len;) \
21076 s = (struct glyph_string *) alloca (sizeof *s); \
21077 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21078 append_glyph_string (&(HEAD), &(TAIL), s); \
21079 s->cmp = cmp; \
21080 s->cmp_from = n; \
21081 s->x = (X); \
21082 if (n == 0) \
21083 first_s = s; \
21084 n = fill_composite_glyph_string (s, base_face, overlaps); \
21087 ++START; \
21088 s = first_s; \
21089 } while (0)
21092 /* Add a glyph string for a glyph-string sequence to the list of strings
21093 between HEAD and TAIL. */
21095 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21096 do { \
21097 int face_id; \
21098 XChar2b *char2b; \
21099 Lisp_Object gstring; \
21101 face_id = (row)->glyphs[area][START].face_id; \
21102 gstring = (composition_gstring_from_id \
21103 ((row)->glyphs[area][START].u.cmp.id)); \
21104 s = (struct glyph_string *) alloca (sizeof *s); \
21105 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21106 * LGSTRING_GLYPH_LEN (gstring)); \
21107 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21108 append_glyph_string (&(HEAD), &(TAIL), s); \
21109 s->x = (X); \
21110 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21111 } while (0)
21114 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21115 of AREA of glyph row ROW on window W between indices START and END.
21116 HL overrides the face for drawing glyph strings, e.g. it is
21117 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21118 x-positions of the drawing area.
21120 This is an ugly monster macro construct because we must use alloca
21121 to allocate glyph strings (because draw_glyphs can be called
21122 asynchronously). */
21124 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21125 do \
21127 HEAD = TAIL = NULL; \
21128 while (START < END) \
21130 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21131 switch (first_glyph->type) \
21133 case CHAR_GLYPH: \
21134 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21135 HL, X, LAST_X); \
21136 break; \
21138 case COMPOSITE_GLYPH: \
21139 if (first_glyph->u.cmp.automatic) \
21140 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21141 HL, X, LAST_X); \
21142 else \
21143 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21144 HL, X, LAST_X); \
21145 break; \
21147 case STRETCH_GLYPH: \
21148 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21149 HL, X, LAST_X); \
21150 break; \
21152 case IMAGE_GLYPH: \
21153 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21154 HL, X, LAST_X); \
21155 break; \
21157 default: \
21158 abort (); \
21161 if (s) \
21163 set_glyph_string_background_width (s, START, LAST_X); \
21164 (X) += s->width; \
21167 } while (0)
21170 /* Draw glyphs between START and END in AREA of ROW on window W,
21171 starting at x-position X. X is relative to AREA in W. HL is a
21172 face-override with the following meaning:
21174 DRAW_NORMAL_TEXT draw normally
21175 DRAW_CURSOR draw in cursor face
21176 DRAW_MOUSE_FACE draw in mouse face.
21177 DRAW_INVERSE_VIDEO draw in mode line face
21178 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
21179 DRAW_IMAGE_RAISED draw an image with a raised relief around it
21181 If OVERLAPS is non-zero, draw only the foreground of characters and
21182 clip to the physical height of ROW. Non-zero value also defines
21183 the overlapping part to be drawn:
21185 OVERLAPS_PRED overlap with preceding rows
21186 OVERLAPS_SUCC overlap with succeeding rows
21187 OVERLAPS_BOTH overlap with both preceding/succeeding rows
21188 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
21190 Value is the x-position reached, relative to AREA of W. */
21192 static int
21193 draw_glyphs (struct window *w, int x, struct glyph_row *row,
21194 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
21195 enum draw_glyphs_face hl, int overlaps)
21197 struct glyph_string *head, *tail;
21198 struct glyph_string *s;
21199 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
21200 int i, j, x_reached, last_x, area_left = 0;
21201 struct frame *f = XFRAME (WINDOW_FRAME (w));
21202 DECLARE_HDC (hdc);
21204 ALLOCATE_HDC (hdc, f);
21206 /* Let's rather be paranoid than getting a SEGV. */
21207 end = min (end, row->used[area]);
21208 start = max (0, start);
21209 start = min (end, start);
21211 /* Translate X to frame coordinates. Set last_x to the right
21212 end of the drawing area. */
21213 if (row->full_width_p)
21215 /* X is relative to the left edge of W, without scroll bars
21216 or fringes. */
21217 area_left = WINDOW_LEFT_EDGE_X (w);
21218 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
21220 else
21222 area_left = window_box_left (w, area);
21223 last_x = area_left + window_box_width (w, area);
21225 x += area_left;
21227 /* Build a doubly-linked list of glyph_string structures between
21228 head and tail from what we have to draw. Note that the macro
21229 BUILD_GLYPH_STRINGS will modify its start parameter. That's
21230 the reason we use a separate variable `i'. */
21231 i = start;
21232 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
21233 if (tail)
21234 x_reached = tail->x + tail->background_width;
21235 else
21236 x_reached = x;
21238 /* If there are any glyphs with lbearing < 0 or rbearing > width in
21239 the row, redraw some glyphs in front or following the glyph
21240 strings built above. */
21241 if (head && !overlaps && row->contains_overlapping_glyphs_p)
21243 struct glyph_string *h, *t;
21244 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21245 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
21246 int dummy_x = 0;
21248 /* If mouse highlighting is on, we may need to draw adjacent
21249 glyphs using mouse-face highlighting. */
21250 if (area == TEXT_AREA && row->mouse_face_p)
21252 struct glyph_row *mouse_beg_row, *mouse_end_row;
21254 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21255 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21257 if (row >= mouse_beg_row && row <= mouse_end_row)
21259 check_mouse_face = 1;
21260 mouse_beg_col = (row == mouse_beg_row)
21261 ? dpyinfo->mouse_face_beg_col : 0;
21262 mouse_end_col = (row == mouse_end_row)
21263 ? dpyinfo->mouse_face_end_col
21264 : row->used[TEXT_AREA];
21268 /* Compute overhangs for all glyph strings. */
21269 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
21270 for (s = head; s; s = s->next)
21271 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
21273 /* Prepend glyph strings for glyphs in front of the first glyph
21274 string that are overwritten because of the first glyph
21275 string's left overhang. The background of all strings
21276 prepended must be drawn because the first glyph string
21277 draws over it. */
21278 i = left_overwritten (head);
21279 if (i >= 0)
21281 enum draw_glyphs_face overlap_hl;
21283 /* If this row contains mouse highlighting, attempt to draw
21284 the overlapped glyphs with the correct highlight. This
21285 code fails if the overlap encompasses more than one glyph
21286 and mouse-highlight spans only some of these glyphs.
21287 However, making it work perfectly involves a lot more
21288 code, and I don't know if the pathological case occurs in
21289 practice, so we'll stick to this for now. --- cyd */
21290 if (check_mouse_face
21291 && mouse_beg_col < start && mouse_end_col > i)
21292 overlap_hl = DRAW_MOUSE_FACE;
21293 else
21294 overlap_hl = DRAW_NORMAL_TEXT;
21296 j = i;
21297 BUILD_GLYPH_STRINGS (j, start, h, t,
21298 overlap_hl, dummy_x, last_x);
21299 start = i;
21300 compute_overhangs_and_x (t, head->x, 1);
21301 prepend_glyph_string_lists (&head, &tail, h, t);
21302 clip_head = head;
21305 /* Prepend glyph strings for glyphs in front of the first glyph
21306 string that overwrite that glyph string because of their
21307 right overhang. For these strings, only the foreground must
21308 be drawn, because it draws over the glyph string at `head'.
21309 The background must not be drawn because this would overwrite
21310 right overhangs of preceding glyphs for which no glyph
21311 strings exist. */
21312 i = left_overwriting (head);
21313 if (i >= 0)
21315 enum draw_glyphs_face overlap_hl;
21317 if (check_mouse_face
21318 && mouse_beg_col < start && mouse_end_col > i)
21319 overlap_hl = DRAW_MOUSE_FACE;
21320 else
21321 overlap_hl = DRAW_NORMAL_TEXT;
21323 clip_head = head;
21324 BUILD_GLYPH_STRINGS (i, start, h, t,
21325 overlap_hl, dummy_x, last_x);
21326 for (s = h; s; s = s->next)
21327 s->background_filled_p = 1;
21328 compute_overhangs_and_x (t, head->x, 1);
21329 prepend_glyph_string_lists (&head, &tail, h, t);
21332 /* Append glyphs strings for glyphs following the last glyph
21333 string tail that are overwritten by tail. The background of
21334 these strings has to be drawn because tail's foreground draws
21335 over it. */
21336 i = right_overwritten (tail);
21337 if (i >= 0)
21339 enum draw_glyphs_face overlap_hl;
21341 if (check_mouse_face
21342 && mouse_beg_col < i && mouse_end_col > end)
21343 overlap_hl = DRAW_MOUSE_FACE;
21344 else
21345 overlap_hl = DRAW_NORMAL_TEXT;
21347 BUILD_GLYPH_STRINGS (end, i, h, t,
21348 overlap_hl, x, last_x);
21349 /* Because BUILD_GLYPH_STRINGS updates the first argument,
21350 we don't have `end = i;' here. */
21351 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21352 append_glyph_string_lists (&head, &tail, h, t);
21353 clip_tail = tail;
21356 /* Append glyph strings for glyphs following the last glyph
21357 string tail that overwrite tail. The foreground of such
21358 glyphs has to be drawn because it writes into the background
21359 of tail. The background must not be drawn because it could
21360 paint over the foreground of following glyphs. */
21361 i = right_overwriting (tail);
21362 if (i >= 0)
21364 enum draw_glyphs_face overlap_hl;
21365 if (check_mouse_face
21366 && mouse_beg_col < i && mouse_end_col > end)
21367 overlap_hl = DRAW_MOUSE_FACE;
21368 else
21369 overlap_hl = DRAW_NORMAL_TEXT;
21371 clip_tail = tail;
21372 i++; /* We must include the Ith glyph. */
21373 BUILD_GLYPH_STRINGS (end, i, h, t,
21374 overlap_hl, x, last_x);
21375 for (s = h; s; s = s->next)
21376 s->background_filled_p = 1;
21377 compute_overhangs_and_x (h, tail->x + tail->width, 0);
21378 append_glyph_string_lists (&head, &tail, h, t);
21380 if (clip_head || clip_tail)
21381 for (s = head; s; s = s->next)
21383 s->clip_head = clip_head;
21384 s->clip_tail = clip_tail;
21388 /* Draw all strings. */
21389 for (s = head; s; s = s->next)
21390 FRAME_RIF (f)->draw_glyph_string (s);
21392 #ifndef HAVE_NS
21393 /* When focus a sole frame and move horizontally, this sets on_p to 0
21394 causing a failure to erase prev cursor position. */
21395 if (area == TEXT_AREA
21396 && !row->full_width_p
21397 /* When drawing overlapping rows, only the glyph strings'
21398 foreground is drawn, which doesn't erase a cursor
21399 completely. */
21400 && !overlaps)
21402 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
21403 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
21404 : (tail ? tail->x + tail->background_width : x));
21405 x0 -= area_left;
21406 x1 -= area_left;
21408 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
21409 row->y, MATRIX_ROW_BOTTOM_Y (row));
21411 #endif
21413 /* Value is the x-position up to which drawn, relative to AREA of W.
21414 This doesn't include parts drawn because of overhangs. */
21415 if (row->full_width_p)
21416 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
21417 else
21418 x_reached -= area_left;
21420 RELEASE_HDC (hdc, f);
21422 return x_reached;
21425 /* Expand row matrix if too narrow. Don't expand if area
21426 is not present. */
21428 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
21430 if (!fonts_changed_p \
21431 && (it->glyph_row->glyphs[area] \
21432 < it->glyph_row->glyphs[area + 1])) \
21434 it->w->ncols_scale_factor++; \
21435 fonts_changed_p = 1; \
21439 /* Store one glyph for IT->char_to_display in IT->glyph_row.
21440 Called from x_produce_glyphs when IT->glyph_row is non-null. */
21442 static INLINE void
21443 append_glyph (struct it *it)
21445 struct glyph *glyph;
21446 enum glyph_row_area area = it->area;
21448 xassert (it->glyph_row);
21449 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
21451 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21452 if (glyph < it->glyph_row->glyphs[area + 1])
21454 /* If the glyph row is reversed, we need to prepend the glyph
21455 rather than append it. */
21456 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21458 struct glyph *g;
21460 /* Make room for the additional glyph. */
21461 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21462 g[1] = *g;
21463 glyph = it->glyph_row->glyphs[area];
21465 glyph->charpos = CHARPOS (it->position);
21466 glyph->object = it->object;
21467 if (it->pixel_width > 0)
21469 glyph->pixel_width = it->pixel_width;
21470 glyph->padding_p = 0;
21472 else
21474 /* Assure at least 1-pixel width. Otherwise, cursor can't
21475 be displayed correctly. */
21476 glyph->pixel_width = 1;
21477 glyph->padding_p = 1;
21479 glyph->ascent = it->ascent;
21480 glyph->descent = it->descent;
21481 glyph->voffset = it->voffset;
21482 glyph->type = CHAR_GLYPH;
21483 glyph->avoid_cursor_p = it->avoid_cursor_p;
21484 glyph->multibyte_p = it->multibyte_p;
21485 glyph->left_box_line_p = it->start_of_box_run_p;
21486 glyph->right_box_line_p = it->end_of_box_run_p;
21487 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21488 || it->phys_descent > it->descent);
21489 glyph->glyph_not_available_p = it->glyph_not_available_p;
21490 glyph->face_id = it->face_id;
21491 glyph->u.ch = it->char_to_display;
21492 glyph->slice = null_glyph_slice;
21493 glyph->font_type = FONT_TYPE_UNKNOWN;
21494 if (it->bidi_p)
21496 glyph->resolved_level = it->bidi_it.resolved_level;
21497 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21498 abort ();
21499 glyph->bidi_type = it->bidi_it.type;
21501 else
21503 glyph->resolved_level = 0;
21504 glyph->bidi_type = UNKNOWN_BT;
21506 ++it->glyph_row->used[area];
21508 else
21509 IT_EXPAND_MATRIX_WIDTH (it, area);
21512 /* Store one glyph for the composition IT->cmp_it.id in
21513 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
21514 non-null. */
21516 static INLINE void
21517 append_composite_glyph (struct it *it)
21519 struct glyph *glyph;
21520 enum glyph_row_area area = it->area;
21522 xassert (it->glyph_row);
21524 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21525 if (glyph < it->glyph_row->glyphs[area + 1])
21527 /* If the glyph row is reversed, we need to prepend the glyph
21528 rather than append it. */
21529 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
21531 struct glyph *g;
21533 /* Make room for the new glyph. */
21534 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
21535 g[1] = *g;
21536 glyph = it->glyph_row->glyphs[it->area];
21538 glyph->charpos = it->cmp_it.charpos;
21539 glyph->object = it->object;
21540 glyph->pixel_width = it->pixel_width;
21541 glyph->ascent = it->ascent;
21542 glyph->descent = it->descent;
21543 glyph->voffset = it->voffset;
21544 glyph->type = COMPOSITE_GLYPH;
21545 if (it->cmp_it.ch < 0)
21547 glyph->u.cmp.automatic = 0;
21548 glyph->u.cmp.id = it->cmp_it.id;
21550 else
21552 glyph->u.cmp.automatic = 1;
21553 glyph->u.cmp.id = it->cmp_it.id;
21554 glyph->u.cmp.from = it->cmp_it.from;
21555 glyph->u.cmp.to = it->cmp_it.to - 1;
21557 glyph->avoid_cursor_p = it->avoid_cursor_p;
21558 glyph->multibyte_p = it->multibyte_p;
21559 glyph->left_box_line_p = it->start_of_box_run_p;
21560 glyph->right_box_line_p = it->end_of_box_run_p;
21561 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
21562 || it->phys_descent > it->descent);
21563 glyph->padding_p = 0;
21564 glyph->glyph_not_available_p = 0;
21565 glyph->face_id = it->face_id;
21566 glyph->slice = null_glyph_slice;
21567 glyph->font_type = FONT_TYPE_UNKNOWN;
21568 if (it->bidi_p)
21570 glyph->resolved_level = it->bidi_it.resolved_level;
21571 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21572 abort ();
21573 glyph->bidi_type = it->bidi_it.type;
21575 ++it->glyph_row->used[area];
21577 else
21578 IT_EXPAND_MATRIX_WIDTH (it, area);
21582 /* Change IT->ascent and IT->height according to the setting of
21583 IT->voffset. */
21585 static INLINE void
21586 take_vertical_position_into_account (struct it *it)
21588 if (it->voffset)
21590 if (it->voffset < 0)
21591 /* Increase the ascent so that we can display the text higher
21592 in the line. */
21593 it->ascent -= it->voffset;
21594 else
21595 /* Increase the descent so that we can display the text lower
21596 in the line. */
21597 it->descent += it->voffset;
21602 /* Produce glyphs/get display metrics for the image IT is loaded with.
21603 See the description of struct display_iterator in dispextern.h for
21604 an overview of struct display_iterator. */
21606 static void
21607 produce_image_glyph (struct it *it)
21609 struct image *img;
21610 struct face *face;
21611 int glyph_ascent, crop;
21612 struct glyph_slice slice;
21614 xassert (it->what == IT_IMAGE);
21616 face = FACE_FROM_ID (it->f, it->face_id);
21617 xassert (face);
21618 /* Make sure X resources of the face is loaded. */
21619 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21621 if (it->image_id < 0)
21623 /* Fringe bitmap. */
21624 it->ascent = it->phys_ascent = 0;
21625 it->descent = it->phys_descent = 0;
21626 it->pixel_width = 0;
21627 it->nglyphs = 0;
21628 return;
21631 img = IMAGE_FROM_ID (it->f, it->image_id);
21632 xassert (img);
21633 /* Make sure X resources of the image is loaded. */
21634 prepare_image_for_display (it->f, img);
21636 slice.x = slice.y = 0;
21637 slice.width = img->width;
21638 slice.height = img->height;
21640 if (INTEGERP (it->slice.x))
21641 slice.x = XINT (it->slice.x);
21642 else if (FLOATP (it->slice.x))
21643 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
21645 if (INTEGERP (it->slice.y))
21646 slice.y = XINT (it->slice.y);
21647 else if (FLOATP (it->slice.y))
21648 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
21650 if (INTEGERP (it->slice.width))
21651 slice.width = XINT (it->slice.width);
21652 else if (FLOATP (it->slice.width))
21653 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
21655 if (INTEGERP (it->slice.height))
21656 slice.height = XINT (it->slice.height);
21657 else if (FLOATP (it->slice.height))
21658 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
21660 if (slice.x >= img->width)
21661 slice.x = img->width;
21662 if (slice.y >= img->height)
21663 slice.y = img->height;
21664 if (slice.x + slice.width >= img->width)
21665 slice.width = img->width - slice.x;
21666 if (slice.y + slice.height > img->height)
21667 slice.height = img->height - slice.y;
21669 if (slice.width == 0 || slice.height == 0)
21670 return;
21672 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
21674 it->descent = slice.height - glyph_ascent;
21675 if (slice.y == 0)
21676 it->descent += img->vmargin;
21677 if (slice.y + slice.height == img->height)
21678 it->descent += img->vmargin;
21679 it->phys_descent = it->descent;
21681 it->pixel_width = slice.width;
21682 if (slice.x == 0)
21683 it->pixel_width += img->hmargin;
21684 if (slice.x + slice.width == img->width)
21685 it->pixel_width += img->hmargin;
21687 /* It's quite possible for images to have an ascent greater than
21688 their height, so don't get confused in that case. */
21689 if (it->descent < 0)
21690 it->descent = 0;
21692 it->nglyphs = 1;
21694 if (face->box != FACE_NO_BOX)
21696 if (face->box_line_width > 0)
21698 if (slice.y == 0)
21699 it->ascent += face->box_line_width;
21700 if (slice.y + slice.height == img->height)
21701 it->descent += face->box_line_width;
21704 if (it->start_of_box_run_p && slice.x == 0)
21705 it->pixel_width += eabs (face->box_line_width);
21706 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
21707 it->pixel_width += eabs (face->box_line_width);
21710 take_vertical_position_into_account (it);
21712 /* Automatically crop wide image glyphs at right edge so we can
21713 draw the cursor on same display row. */
21714 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
21715 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
21717 it->pixel_width -= crop;
21718 slice.width -= crop;
21721 if (it->glyph_row)
21723 struct glyph *glyph;
21724 enum glyph_row_area area = it->area;
21726 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21727 if (glyph < it->glyph_row->glyphs[area + 1])
21729 glyph->charpos = CHARPOS (it->position);
21730 glyph->object = it->object;
21731 glyph->pixel_width = it->pixel_width;
21732 glyph->ascent = glyph_ascent;
21733 glyph->descent = it->descent;
21734 glyph->voffset = it->voffset;
21735 glyph->type = IMAGE_GLYPH;
21736 glyph->avoid_cursor_p = it->avoid_cursor_p;
21737 glyph->multibyte_p = it->multibyte_p;
21738 glyph->left_box_line_p = it->start_of_box_run_p;
21739 glyph->right_box_line_p = it->end_of_box_run_p;
21740 glyph->overlaps_vertically_p = 0;
21741 glyph->padding_p = 0;
21742 glyph->glyph_not_available_p = 0;
21743 glyph->face_id = it->face_id;
21744 glyph->u.img_id = img->id;
21745 glyph->slice = slice;
21746 glyph->font_type = FONT_TYPE_UNKNOWN;
21747 if (it->bidi_p)
21749 glyph->resolved_level = it->bidi_it.resolved_level;
21750 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21751 abort ();
21752 glyph->bidi_type = it->bidi_it.type;
21754 ++it->glyph_row->used[area];
21756 else
21757 IT_EXPAND_MATRIX_WIDTH (it, area);
21762 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
21763 of the glyph, WIDTH and HEIGHT are the width and height of the
21764 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
21766 static void
21767 append_stretch_glyph (struct it *it, Lisp_Object object,
21768 int width, int height, int ascent)
21770 struct glyph *glyph;
21771 enum glyph_row_area area = it->area;
21773 xassert (ascent >= 0 && ascent <= height);
21775 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
21776 if (glyph < it->glyph_row->glyphs[area + 1])
21778 /* If the glyph row is reversed, we need to prepend the glyph
21779 rather than append it. */
21780 if (it->glyph_row->reversed_p && area == TEXT_AREA)
21782 struct glyph *g;
21784 /* Make room for the additional glyph. */
21785 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
21786 g[1] = *g;
21787 glyph = it->glyph_row->glyphs[area];
21789 glyph->charpos = CHARPOS (it->position);
21790 glyph->object = object;
21791 glyph->pixel_width = width;
21792 glyph->ascent = ascent;
21793 glyph->descent = height - ascent;
21794 glyph->voffset = it->voffset;
21795 glyph->type = STRETCH_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.stretch.ascent = ascent;
21805 glyph->u.stretch.height = height;
21806 glyph->slice = null_glyph_slice;
21807 glyph->font_type = FONT_TYPE_UNKNOWN;
21808 if (it->bidi_p)
21810 glyph->resolved_level = it->bidi_it.resolved_level;
21811 if ((it->bidi_it.type & 7) != it->bidi_it.type)
21812 abort ();
21813 glyph->bidi_type = it->bidi_it.type;
21815 else
21817 glyph->resolved_level = 0;
21818 glyph->bidi_type = UNKNOWN_BT;
21820 ++it->glyph_row->used[area];
21822 else
21823 IT_EXPAND_MATRIX_WIDTH (it, area);
21827 /* Produce a stretch glyph for iterator IT. IT->object is the value
21828 of the glyph property displayed. The value must be a list
21829 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
21830 being recognized:
21832 1. `:width WIDTH' specifies that the space should be WIDTH *
21833 canonical char width wide. WIDTH may be an integer or floating
21834 point number.
21836 2. `:relative-width FACTOR' specifies that the width of the stretch
21837 should be computed from the width of the first character having the
21838 `glyph' property, and should be FACTOR times that width.
21840 3. `:align-to HPOS' specifies that the space should be wide enough
21841 to reach HPOS, a value in canonical character units.
21843 Exactly one of the above pairs must be present.
21845 4. `:height HEIGHT' specifies that the height of the stretch produced
21846 should be HEIGHT, measured in canonical character units.
21848 5. `:relative-height FACTOR' specifies that the height of the
21849 stretch should be FACTOR times the height of the characters having
21850 the glyph property.
21852 Either none or exactly one of 4 or 5 must be present.
21854 6. `:ascent ASCENT' specifies that ASCENT percent of the height
21855 of the stretch should be used for the ascent of the stretch.
21856 ASCENT must be in the range 0 <= ASCENT <= 100. */
21858 static void
21859 produce_stretch_glyph (struct it *it)
21861 /* (space :width WIDTH :height HEIGHT ...) */
21862 Lisp_Object prop, plist;
21863 int width = 0, height = 0, align_to = -1;
21864 int zero_width_ok_p = 0, zero_height_ok_p = 0;
21865 int ascent = 0;
21866 double tem;
21867 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21868 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
21870 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21872 /* List should start with `space'. */
21873 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
21874 plist = XCDR (it->object);
21876 /* Compute the width of the stretch. */
21877 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
21878 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
21880 /* Absolute width `:width WIDTH' specified and valid. */
21881 zero_width_ok_p = 1;
21882 width = (int)tem;
21884 else if (prop = Fplist_get (plist, QCrelative_width),
21885 NUMVAL (prop) > 0)
21887 /* Relative width `:relative-width FACTOR' specified and valid.
21888 Compute the width of the characters having the `glyph'
21889 property. */
21890 struct it it2;
21891 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
21893 it2 = *it;
21894 if (it->multibyte_p)
21896 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
21897 - IT_BYTEPOS (*it));
21898 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
21900 else
21901 it2.c = *p, it2.len = 1;
21903 it2.glyph_row = NULL;
21904 it2.what = IT_CHARACTER;
21905 x_produce_glyphs (&it2);
21906 width = NUMVAL (prop) * it2.pixel_width;
21908 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
21909 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
21911 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
21912 align_to = (align_to < 0
21914 : align_to - window_box_left_offset (it->w, TEXT_AREA));
21915 else if (align_to < 0)
21916 align_to = window_box_left_offset (it->w, TEXT_AREA);
21917 width = max (0, (int)tem + align_to - it->current_x);
21918 zero_width_ok_p = 1;
21920 else
21921 /* Nothing specified -> width defaults to canonical char width. */
21922 width = FRAME_COLUMN_WIDTH (it->f);
21924 if (width <= 0 && (width < 0 || !zero_width_ok_p))
21925 width = 1;
21927 /* Compute height. */
21928 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
21929 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21931 height = (int)tem;
21932 zero_height_ok_p = 1;
21934 else if (prop = Fplist_get (plist, QCrelative_height),
21935 NUMVAL (prop) > 0)
21936 height = FONT_HEIGHT (font) * NUMVAL (prop);
21937 else
21938 height = FONT_HEIGHT (font);
21940 if (height <= 0 && (height < 0 || !zero_height_ok_p))
21941 height = 1;
21943 /* Compute percentage of height used for ascent. If
21944 `:ascent ASCENT' is present and valid, use that. Otherwise,
21945 derive the ascent from the font in use. */
21946 if (prop = Fplist_get (plist, QCascent),
21947 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21948 ascent = height * NUMVAL (prop) / 100.0;
21949 else if (!NILP (prop)
21950 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21951 ascent = min (max (0, (int)tem), height);
21952 else
21953 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21955 if (width > 0 && it->line_wrap != TRUNCATE
21956 && it->current_x + width > it->last_visible_x)
21957 width = it->last_visible_x - it->current_x - 1;
21959 if (width > 0 && height > 0 && it->glyph_row)
21961 Lisp_Object object = it->stack[it->sp - 1].string;
21962 if (!STRINGP (object))
21963 object = it->w->buffer;
21964 append_stretch_glyph (it, object, width, height, ascent);
21967 it->pixel_width = width;
21968 it->ascent = it->phys_ascent = ascent;
21969 it->descent = it->phys_descent = height - it->ascent;
21970 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21972 take_vertical_position_into_account (it);
21975 /* Calculate line-height and line-spacing properties.
21976 An integer value specifies explicit pixel value.
21977 A float value specifies relative value to current face height.
21978 A cons (float . face-name) specifies relative value to
21979 height of specified face font.
21981 Returns height in pixels, or nil. */
21984 static Lisp_Object
21985 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
21986 int boff, int override)
21988 Lisp_Object face_name = Qnil;
21989 int ascent, descent, height;
21991 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21992 return val;
21994 if (CONSP (val))
21996 face_name = XCAR (val);
21997 val = XCDR (val);
21998 if (!NUMBERP (val))
21999 val = make_number (1);
22000 if (NILP (face_name))
22002 height = it->ascent + it->descent;
22003 goto scale;
22007 if (NILP (face_name))
22009 font = FRAME_FONT (it->f);
22010 boff = FRAME_BASELINE_OFFSET (it->f);
22012 else if (EQ (face_name, Qt))
22014 override = 0;
22016 else
22018 int face_id;
22019 struct face *face;
22021 face_id = lookup_named_face (it->f, face_name, 0);
22022 if (face_id < 0)
22023 return make_number (-1);
22025 face = FACE_FROM_ID (it->f, face_id);
22026 font = face->font;
22027 if (font == NULL)
22028 return make_number (-1);
22029 boff = font->baseline_offset;
22030 if (font->vertical_centering)
22031 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22034 ascent = FONT_BASE (font) + boff;
22035 descent = FONT_DESCENT (font) - boff;
22037 if (override)
22039 it->override_ascent = ascent;
22040 it->override_descent = descent;
22041 it->override_boff = boff;
22044 height = ascent + descent;
22046 scale:
22047 if (FLOATP (val))
22048 height = (int)(XFLOAT_DATA (val) * height);
22049 else if (INTEGERP (val))
22050 height *= XINT (val);
22052 return make_number (height);
22056 /* RIF:
22057 Produce glyphs/get display metrics for the display element IT is
22058 loaded with. See the description of struct it in dispextern.h
22059 for an overview of struct it. */
22061 void
22062 x_produce_glyphs (struct it *it)
22064 int extra_line_spacing = it->extra_line_spacing;
22066 it->glyph_not_available_p = 0;
22068 if (it->what == IT_CHARACTER)
22070 XChar2b char2b;
22071 struct font *font;
22072 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22073 struct font_metrics *pcm;
22074 int font_not_found_p;
22075 int boff; /* baseline offset */
22076 /* We may change it->multibyte_p upon unibyte<->multibyte
22077 conversion. So, save the current value now and restore it
22078 later.
22080 Note: It seems that we don't have to record multibyte_p in
22081 struct glyph because the character code itself tells whether
22082 or not the character is multibyte. Thus, in the future, we
22083 must consider eliminating the field `multibyte_p' in the
22084 struct glyph. */
22085 int saved_multibyte_p = it->multibyte_p;
22087 /* Maybe translate single-byte characters to multibyte, or the
22088 other way. */
22089 it->char_to_display = it->c;
22090 if (!ASCII_BYTE_P (it->c)
22091 && ! it->multibyte_p)
22093 if (SINGLE_BYTE_CHAR_P (it->c)
22094 && unibyte_display_via_language_environment)
22096 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
22098 /* get_next_display_element assures that this decoding
22099 never fails. */
22100 it->char_to_display = DECODE_CHAR (unibyte, it->c);
22101 it->multibyte_p = 1;
22102 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
22103 -1, Qnil);
22104 face = FACE_FROM_ID (it->f, it->face_id);
22108 /* Get font to use. Encode IT->char_to_display. */
22109 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
22110 &char2b, it->multibyte_p, 0);
22111 font = face->font;
22113 font_not_found_p = font == NULL;
22114 if (font_not_found_p)
22116 /* When no suitable font found, display an empty box based
22117 on the metrics of the font of the default face (or what
22118 remapped). */
22119 struct face *no_font_face
22120 = FACE_FROM_ID (it->f,
22121 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
22122 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
22123 font = no_font_face->font;
22124 boff = font->baseline_offset;
22126 else
22128 boff = font->baseline_offset;
22129 if (font->vertical_centering)
22130 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22133 if (it->char_to_display >= ' '
22134 && (!it->multibyte_p || it->char_to_display < 128))
22136 /* Either unibyte or ASCII. */
22137 int stretched_p;
22139 it->nglyphs = 1;
22141 pcm = get_per_char_metric (it->f, font, &char2b);
22143 if (it->override_ascent >= 0)
22145 it->ascent = it->override_ascent;
22146 it->descent = it->override_descent;
22147 boff = it->override_boff;
22149 else
22151 it->ascent = FONT_BASE (font) + boff;
22152 it->descent = FONT_DESCENT (font) - boff;
22155 if (pcm)
22157 it->phys_ascent = pcm->ascent + boff;
22158 it->phys_descent = pcm->descent - boff;
22159 it->pixel_width = pcm->width;
22161 else
22163 it->glyph_not_available_p = 1;
22164 it->phys_ascent = it->ascent;
22165 it->phys_descent = it->descent;
22166 it->pixel_width = FONT_WIDTH (font);
22169 if (it->constrain_row_ascent_descent_p)
22171 if (it->descent > it->max_descent)
22173 it->ascent += it->descent - it->max_descent;
22174 it->descent = it->max_descent;
22176 if (it->ascent > it->max_ascent)
22178 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22179 it->ascent = it->max_ascent;
22181 it->phys_ascent = min (it->phys_ascent, it->ascent);
22182 it->phys_descent = min (it->phys_descent, it->descent);
22183 extra_line_spacing = 0;
22186 /* If this is a space inside a region of text with
22187 `space-width' property, change its width. */
22188 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
22189 if (stretched_p)
22190 it->pixel_width *= XFLOATINT (it->space_width);
22192 /* If face has a box, add the box thickness to the character
22193 height. If character has a box line to the left and/or
22194 right, add the box line width to the character's width. */
22195 if (face->box != FACE_NO_BOX)
22197 int thick = face->box_line_width;
22199 if (thick > 0)
22201 it->ascent += thick;
22202 it->descent += thick;
22204 else
22205 thick = -thick;
22207 if (it->start_of_box_run_p)
22208 it->pixel_width += thick;
22209 if (it->end_of_box_run_p)
22210 it->pixel_width += thick;
22213 /* If face has an overline, add the height of the overline
22214 (1 pixel) and a 1 pixel margin to the character height. */
22215 if (face->overline_p)
22216 it->ascent += overline_margin;
22218 if (it->constrain_row_ascent_descent_p)
22220 if (it->ascent > it->max_ascent)
22221 it->ascent = it->max_ascent;
22222 if (it->descent > it->max_descent)
22223 it->descent = it->max_descent;
22226 take_vertical_position_into_account (it);
22228 /* If we have to actually produce glyphs, do it. */
22229 if (it->glyph_row)
22231 if (stretched_p)
22233 /* Translate a space with a `space-width' property
22234 into a stretch glyph. */
22235 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
22236 / FONT_HEIGHT (font));
22237 append_stretch_glyph (it, it->object, it->pixel_width,
22238 it->ascent + it->descent, ascent);
22240 else
22241 append_glyph (it);
22243 /* If characters with lbearing or rbearing are displayed
22244 in this line, record that fact in a flag of the
22245 glyph row. This is used to optimize X output code. */
22246 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
22247 it->glyph_row->contains_overlapping_glyphs_p = 1;
22249 if (! stretched_p && it->pixel_width == 0)
22250 /* We assure that all visible glyphs have at least 1-pixel
22251 width. */
22252 it->pixel_width = 1;
22254 else if (it->char_to_display == '\n')
22256 /* A newline has no width, but we need the height of the
22257 line. But if previous part of the line sets a height,
22258 don't increase that height */
22260 Lisp_Object height;
22261 Lisp_Object total_height = Qnil;
22263 it->override_ascent = -1;
22264 it->pixel_width = 0;
22265 it->nglyphs = 0;
22267 height = get_it_property (it, Qline_height);
22268 /* Split (line-height total-height) list */
22269 if (CONSP (height)
22270 && CONSP (XCDR (height))
22271 && NILP (XCDR (XCDR (height))))
22273 total_height = XCAR (XCDR (height));
22274 height = XCAR (height);
22276 height = calc_line_height_property (it, height, font, boff, 1);
22278 if (it->override_ascent >= 0)
22280 it->ascent = it->override_ascent;
22281 it->descent = it->override_descent;
22282 boff = it->override_boff;
22284 else
22286 it->ascent = FONT_BASE (font) + boff;
22287 it->descent = FONT_DESCENT (font) - boff;
22290 if (EQ (height, Qt))
22292 if (it->descent > it->max_descent)
22294 it->ascent += it->descent - it->max_descent;
22295 it->descent = it->max_descent;
22297 if (it->ascent > it->max_ascent)
22299 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
22300 it->ascent = it->max_ascent;
22302 it->phys_ascent = min (it->phys_ascent, it->ascent);
22303 it->phys_descent = min (it->phys_descent, it->descent);
22304 it->constrain_row_ascent_descent_p = 1;
22305 extra_line_spacing = 0;
22307 else
22309 Lisp_Object spacing;
22311 it->phys_ascent = it->ascent;
22312 it->phys_descent = it->descent;
22314 if ((it->max_ascent > 0 || it->max_descent > 0)
22315 && face->box != FACE_NO_BOX
22316 && face->box_line_width > 0)
22318 it->ascent += face->box_line_width;
22319 it->descent += face->box_line_width;
22321 if (!NILP (height)
22322 && XINT (height) > it->ascent + it->descent)
22323 it->ascent = XINT (height) - it->descent;
22325 if (!NILP (total_height))
22326 spacing = calc_line_height_property (it, total_height, font, boff, 0);
22327 else
22329 spacing = get_it_property (it, Qline_spacing);
22330 spacing = calc_line_height_property (it, spacing, font, boff, 0);
22332 if (INTEGERP (spacing))
22334 extra_line_spacing = XINT (spacing);
22335 if (!NILP (total_height))
22336 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
22340 else if (it->char_to_display == '\t')
22342 if (font->space_width > 0)
22344 int tab_width = it->tab_width * font->space_width;
22345 int x = it->current_x + it->continuation_lines_width;
22346 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
22348 /* If the distance from the current position to the next tab
22349 stop is less than a space character width, use the
22350 tab stop after that. */
22351 if (next_tab_x - x < font->space_width)
22352 next_tab_x += tab_width;
22354 it->pixel_width = next_tab_x - x;
22355 it->nglyphs = 1;
22356 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
22357 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
22359 if (it->glyph_row)
22361 append_stretch_glyph (it, it->object, it->pixel_width,
22362 it->ascent + it->descent, it->ascent);
22365 else
22367 it->pixel_width = 0;
22368 it->nglyphs = 1;
22371 else
22373 /* A multi-byte character. Assume that the display width of the
22374 character is the width of the character multiplied by the
22375 width of the font. */
22377 /* If we found a font, this font should give us the right
22378 metrics. If we didn't find a font, use the frame's
22379 default font and calculate the width of the character by
22380 multiplying the width of font by the width of the
22381 character. */
22383 pcm = get_per_char_metric (it->f, font, &char2b);
22385 if (font_not_found_p || !pcm)
22387 int char_width = CHAR_WIDTH (it->char_to_display);
22389 if (char_width == 0)
22390 /* This is a non spacing character. But, as we are
22391 going to display an empty box, the box must occupy
22392 at least one column. */
22393 char_width = 1;
22394 it->glyph_not_available_p = 1;
22395 it->pixel_width = font->space_width * char_width;
22396 it->phys_ascent = FONT_BASE (font) + boff;
22397 it->phys_descent = FONT_DESCENT (font) - boff;
22399 else
22401 it->pixel_width = pcm->width;
22402 it->phys_ascent = pcm->ascent + boff;
22403 it->phys_descent = pcm->descent - boff;
22404 if (it->glyph_row
22405 && (pcm->lbearing < 0
22406 || pcm->rbearing > pcm->width))
22407 it->glyph_row->contains_overlapping_glyphs_p = 1;
22409 it->nglyphs = 1;
22410 it->ascent = FONT_BASE (font) + boff;
22411 it->descent = FONT_DESCENT (font) - boff;
22412 if (face->box != FACE_NO_BOX)
22414 int thick = face->box_line_width;
22416 if (thick > 0)
22418 it->ascent += thick;
22419 it->descent += thick;
22421 else
22422 thick = - thick;
22424 if (it->start_of_box_run_p)
22425 it->pixel_width += thick;
22426 if (it->end_of_box_run_p)
22427 it->pixel_width += thick;
22430 /* If face has an overline, add the height of the overline
22431 (1 pixel) and a 1 pixel margin to the character height. */
22432 if (face->overline_p)
22433 it->ascent += overline_margin;
22435 take_vertical_position_into_account (it);
22437 if (it->ascent < 0)
22438 it->ascent = 0;
22439 if (it->descent < 0)
22440 it->descent = 0;
22442 if (it->glyph_row)
22443 append_glyph (it);
22444 if (it->pixel_width == 0)
22445 /* We assure that all visible glyphs have at least 1-pixel
22446 width. */
22447 it->pixel_width = 1;
22449 it->multibyte_p = saved_multibyte_p;
22451 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
22453 /* A static composition.
22455 Note: A composition is represented as one glyph in the
22456 glyph matrix. There are no padding glyphs.
22458 Important note: pixel_width, ascent, and descent are the
22459 values of what is drawn by draw_glyphs (i.e. the values of
22460 the overall glyphs composed). */
22461 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22462 int boff; /* baseline offset */
22463 struct composition *cmp = composition_table[it->cmp_it.id];
22464 int glyph_len = cmp->glyph_len;
22465 struct font *font = face->font;
22467 it->nglyphs = 1;
22469 /* If we have not yet calculated pixel size data of glyphs of
22470 the composition for the current face font, calculate them
22471 now. Theoretically, we have to check all fonts for the
22472 glyphs, but that requires much time and memory space. So,
22473 here we check only the font of the first glyph. This may
22474 lead to incorrect display, but it's very rare, and C-l
22475 (recenter-top-bottom) can correct the display anyway. */
22476 if (! cmp->font || cmp->font != font)
22478 /* Ascent and descent of the font of the first character
22479 of this composition (adjusted by baseline offset).
22480 Ascent and descent of overall glyphs should not be less
22481 than these, respectively. */
22482 int font_ascent, font_descent, font_height;
22483 /* Bounding box of the overall glyphs. */
22484 int leftmost, rightmost, lowest, highest;
22485 int lbearing, rbearing;
22486 int i, width, ascent, descent;
22487 int left_padded = 0, right_padded = 0;
22488 int c;
22489 XChar2b char2b;
22490 struct font_metrics *pcm;
22491 int font_not_found_p;
22492 int pos;
22494 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
22495 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
22496 break;
22497 if (glyph_len < cmp->glyph_len)
22498 right_padded = 1;
22499 for (i = 0; i < glyph_len; i++)
22501 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
22502 break;
22503 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22505 if (i > 0)
22506 left_padded = 1;
22508 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
22509 : IT_CHARPOS (*it));
22510 /* If no suitable font is found, use the default font. */
22511 font_not_found_p = font == NULL;
22512 if (font_not_found_p)
22514 face = face->ascii_face;
22515 font = face->font;
22517 boff = font->baseline_offset;
22518 if (font->vertical_centering)
22519 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22520 font_ascent = FONT_BASE (font) + boff;
22521 font_descent = FONT_DESCENT (font) - boff;
22522 font_height = FONT_HEIGHT (font);
22524 cmp->font = (void *) font;
22526 pcm = NULL;
22527 if (! font_not_found_p)
22529 get_char_face_and_encoding (it->f, c, it->face_id,
22530 &char2b, it->multibyte_p, 0);
22531 pcm = get_per_char_metric (it->f, font, &char2b);
22534 /* Initialize the bounding box. */
22535 if (pcm)
22537 width = pcm->width;
22538 ascent = pcm->ascent;
22539 descent = pcm->descent;
22540 lbearing = pcm->lbearing;
22541 rbearing = pcm->rbearing;
22543 else
22545 width = FONT_WIDTH (font);
22546 ascent = FONT_BASE (font);
22547 descent = FONT_DESCENT (font);
22548 lbearing = 0;
22549 rbearing = width;
22552 rightmost = width;
22553 leftmost = 0;
22554 lowest = - descent + boff;
22555 highest = ascent + boff;
22557 if (! font_not_found_p
22558 && font->default_ascent
22559 && CHAR_TABLE_P (Vuse_default_ascent)
22560 && !NILP (Faref (Vuse_default_ascent,
22561 make_number (it->char_to_display))))
22562 highest = font->default_ascent + boff;
22564 /* Draw the first glyph at the normal position. It may be
22565 shifted to right later if some other glyphs are drawn
22566 at the left. */
22567 cmp->offsets[i * 2] = 0;
22568 cmp->offsets[i * 2 + 1] = boff;
22569 cmp->lbearing = lbearing;
22570 cmp->rbearing = rbearing;
22572 /* Set cmp->offsets for the remaining glyphs. */
22573 for (i++; i < glyph_len; i++)
22575 int left, right, btm, top;
22576 int ch = COMPOSITION_GLYPH (cmp, i);
22577 int face_id;
22578 struct face *this_face;
22579 int this_boff;
22581 if (ch == '\t')
22582 ch = ' ';
22583 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
22584 this_face = FACE_FROM_ID (it->f, face_id);
22585 font = this_face->font;
22587 if (font == NULL)
22588 pcm = NULL;
22589 else
22591 this_boff = font->baseline_offset;
22592 if (font->vertical_centering)
22593 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22594 get_char_face_and_encoding (it->f, ch, face_id,
22595 &char2b, it->multibyte_p, 0);
22596 pcm = get_per_char_metric (it->f, font, &char2b);
22598 if (! pcm)
22599 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
22600 else
22602 width = pcm->width;
22603 ascent = pcm->ascent;
22604 descent = pcm->descent;
22605 lbearing = pcm->lbearing;
22606 rbearing = pcm->rbearing;
22607 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
22609 /* Relative composition with or without
22610 alternate chars. */
22611 left = (leftmost + rightmost - width) / 2;
22612 btm = - descent + boff;
22613 if (font->relative_compose
22614 && (! CHAR_TABLE_P (Vignore_relative_composition)
22615 || NILP (Faref (Vignore_relative_composition,
22616 make_number (ch)))))
22619 if (- descent >= font->relative_compose)
22620 /* One extra pixel between two glyphs. */
22621 btm = highest + 1;
22622 else if (ascent <= 0)
22623 /* One extra pixel between two glyphs. */
22624 btm = lowest - 1 - ascent - descent;
22627 else
22629 /* A composition rule is specified by an integer
22630 value that encodes global and new reference
22631 points (GREF and NREF). GREF and NREF are
22632 specified by numbers as below:
22634 0---1---2 -- ascent
22638 9--10--11 -- center
22640 ---3---4---5--- baseline
22642 6---7---8 -- descent
22644 int rule = COMPOSITION_RULE (cmp, i);
22645 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
22647 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
22648 grefx = gref % 3, nrefx = nref % 3;
22649 grefy = gref / 3, nrefy = nref / 3;
22650 if (xoff)
22651 xoff = font_height * (xoff - 128) / 256;
22652 if (yoff)
22653 yoff = font_height * (yoff - 128) / 256;
22655 left = (leftmost
22656 + grefx * (rightmost - leftmost) / 2
22657 - nrefx * width / 2
22658 + xoff);
22660 btm = ((grefy == 0 ? highest
22661 : grefy == 1 ? 0
22662 : grefy == 2 ? lowest
22663 : (highest + lowest) / 2)
22664 - (nrefy == 0 ? ascent + descent
22665 : nrefy == 1 ? descent - boff
22666 : nrefy == 2 ? 0
22667 : (ascent + descent) / 2)
22668 + yoff);
22671 cmp->offsets[i * 2] = left;
22672 cmp->offsets[i * 2 + 1] = btm + descent;
22674 /* Update the bounding box of the overall glyphs. */
22675 if (width > 0)
22677 right = left + width;
22678 if (left < leftmost)
22679 leftmost = left;
22680 if (right > rightmost)
22681 rightmost = right;
22683 top = btm + descent + ascent;
22684 if (top > highest)
22685 highest = top;
22686 if (btm < lowest)
22687 lowest = btm;
22689 if (cmp->lbearing > left + lbearing)
22690 cmp->lbearing = left + lbearing;
22691 if (cmp->rbearing < left + rbearing)
22692 cmp->rbearing = left + rbearing;
22696 /* If there are glyphs whose x-offsets are negative,
22697 shift all glyphs to the right and make all x-offsets
22698 non-negative. */
22699 if (leftmost < 0)
22701 for (i = 0; i < cmp->glyph_len; i++)
22702 cmp->offsets[i * 2] -= leftmost;
22703 rightmost -= leftmost;
22704 cmp->lbearing -= leftmost;
22705 cmp->rbearing -= leftmost;
22708 if (left_padded && cmp->lbearing < 0)
22710 for (i = 0; i < cmp->glyph_len; i++)
22711 cmp->offsets[i * 2] -= cmp->lbearing;
22712 rightmost -= cmp->lbearing;
22713 cmp->rbearing -= cmp->lbearing;
22714 cmp->lbearing = 0;
22716 if (right_padded && rightmost < cmp->rbearing)
22718 rightmost = cmp->rbearing;
22721 cmp->pixel_width = rightmost;
22722 cmp->ascent = highest;
22723 cmp->descent = - lowest;
22724 if (cmp->ascent < font_ascent)
22725 cmp->ascent = font_ascent;
22726 if (cmp->descent < font_descent)
22727 cmp->descent = font_descent;
22730 if (it->glyph_row
22731 && (cmp->lbearing < 0
22732 || cmp->rbearing > cmp->pixel_width))
22733 it->glyph_row->contains_overlapping_glyphs_p = 1;
22735 it->pixel_width = cmp->pixel_width;
22736 it->ascent = it->phys_ascent = cmp->ascent;
22737 it->descent = it->phys_descent = cmp->descent;
22738 if (face->box != FACE_NO_BOX)
22740 int thick = face->box_line_width;
22742 if (thick > 0)
22744 it->ascent += thick;
22745 it->descent += thick;
22747 else
22748 thick = - thick;
22750 if (it->start_of_box_run_p)
22751 it->pixel_width += thick;
22752 if (it->end_of_box_run_p)
22753 it->pixel_width += thick;
22756 /* If face has an overline, add the height of the overline
22757 (1 pixel) and a 1 pixel margin to the character height. */
22758 if (face->overline_p)
22759 it->ascent += overline_margin;
22761 take_vertical_position_into_account (it);
22762 if (it->ascent < 0)
22763 it->ascent = 0;
22764 if (it->descent < 0)
22765 it->descent = 0;
22767 if (it->glyph_row)
22768 append_composite_glyph (it);
22770 else if (it->what == IT_COMPOSITION)
22772 /* A dynamic (automatic) composition. */
22773 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22774 Lisp_Object gstring;
22775 struct font_metrics metrics;
22777 gstring = composition_gstring_from_id (it->cmp_it.id);
22778 it->pixel_width
22779 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
22780 &metrics);
22781 if (it->glyph_row
22782 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
22783 it->glyph_row->contains_overlapping_glyphs_p = 1;
22784 it->ascent = it->phys_ascent = metrics.ascent;
22785 it->descent = it->phys_descent = metrics.descent;
22786 if (face->box != FACE_NO_BOX)
22788 int thick = face->box_line_width;
22790 if (thick > 0)
22792 it->ascent += thick;
22793 it->descent += thick;
22795 else
22796 thick = - thick;
22798 if (it->start_of_box_run_p)
22799 it->pixel_width += thick;
22800 if (it->end_of_box_run_p)
22801 it->pixel_width += thick;
22803 /* If face has an overline, add the height of the overline
22804 (1 pixel) and a 1 pixel margin to the character height. */
22805 if (face->overline_p)
22806 it->ascent += overline_margin;
22807 take_vertical_position_into_account (it);
22808 if (it->ascent < 0)
22809 it->ascent = 0;
22810 if (it->descent < 0)
22811 it->descent = 0;
22813 if (it->glyph_row)
22814 append_composite_glyph (it);
22816 else if (it->what == IT_IMAGE)
22817 produce_image_glyph (it);
22818 else if (it->what == IT_STRETCH)
22819 produce_stretch_glyph (it);
22821 /* Accumulate dimensions. Note: can't assume that it->descent > 0
22822 because this isn't true for images with `:ascent 100'. */
22823 xassert (it->ascent >= 0 && it->descent >= 0);
22824 if (it->area == TEXT_AREA)
22825 it->current_x += it->pixel_width;
22827 if (extra_line_spacing > 0)
22829 it->descent += extra_line_spacing;
22830 if (extra_line_spacing > it->max_extra_line_spacing)
22831 it->max_extra_line_spacing = extra_line_spacing;
22834 it->max_ascent = max (it->max_ascent, it->ascent);
22835 it->max_descent = max (it->max_descent, it->descent);
22836 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
22837 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
22840 /* EXPORT for RIF:
22841 Output LEN glyphs starting at START at the nominal cursor position.
22842 Advance the nominal cursor over the text. The global variable
22843 updated_window contains the window being updated, updated_row is
22844 the glyph row being updated, and updated_area is the area of that
22845 row being updated. */
22847 void
22848 x_write_glyphs (struct glyph *start, int len)
22850 int x, hpos;
22852 xassert (updated_window && updated_row);
22853 BLOCK_INPUT;
22855 /* Write glyphs. */
22857 hpos = start - updated_row->glyphs[updated_area];
22858 x = draw_glyphs (updated_window, output_cursor.x,
22859 updated_row, updated_area,
22860 hpos, hpos + len,
22861 DRAW_NORMAL_TEXT, 0);
22863 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
22864 if (updated_area == TEXT_AREA
22865 && updated_window->phys_cursor_on_p
22866 && updated_window->phys_cursor.vpos == output_cursor.vpos
22867 && updated_window->phys_cursor.hpos >= hpos
22868 && updated_window->phys_cursor.hpos < hpos + len)
22869 updated_window->phys_cursor_on_p = 0;
22871 UNBLOCK_INPUT;
22873 /* Advance the output cursor. */
22874 output_cursor.hpos += len;
22875 output_cursor.x = x;
22879 /* EXPORT for RIF:
22880 Insert LEN glyphs from START at the nominal cursor position. */
22882 void
22883 x_insert_glyphs (struct glyph *start, int len)
22885 struct frame *f;
22886 struct window *w;
22887 int line_height, shift_by_width, shifted_region_width;
22888 struct glyph_row *row;
22889 struct glyph *glyph;
22890 int frame_x, frame_y;
22891 EMACS_INT hpos;
22893 xassert (updated_window && updated_row);
22894 BLOCK_INPUT;
22895 w = updated_window;
22896 f = XFRAME (WINDOW_FRAME (w));
22898 /* Get the height of the line we are in. */
22899 row = updated_row;
22900 line_height = row->height;
22902 /* Get the width of the glyphs to insert. */
22903 shift_by_width = 0;
22904 for (glyph = start; glyph < start + len; ++glyph)
22905 shift_by_width += glyph->pixel_width;
22907 /* Get the width of the region to shift right. */
22908 shifted_region_width = (window_box_width (w, updated_area)
22909 - output_cursor.x
22910 - shift_by_width);
22912 /* Shift right. */
22913 frame_x = window_box_left (w, updated_area) + output_cursor.x;
22914 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
22916 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
22917 line_height, shift_by_width);
22919 /* Write the glyphs. */
22920 hpos = start - row->glyphs[updated_area];
22921 draw_glyphs (w, output_cursor.x, row, updated_area,
22922 hpos, hpos + len,
22923 DRAW_NORMAL_TEXT, 0);
22925 /* Advance the output cursor. */
22926 output_cursor.hpos += len;
22927 output_cursor.x += shift_by_width;
22928 UNBLOCK_INPUT;
22932 /* EXPORT for RIF:
22933 Erase the current text line from the nominal cursor position
22934 (inclusive) to pixel column TO_X (exclusive). The idea is that
22935 everything from TO_X onward is already erased.
22937 TO_X is a pixel position relative to updated_area of
22938 updated_window. TO_X == -1 means clear to the end of this area. */
22940 void
22941 x_clear_end_of_line (int to_x)
22943 struct frame *f;
22944 struct window *w = updated_window;
22945 int max_x, min_y, max_y;
22946 int from_x, from_y, to_y;
22948 xassert (updated_window && updated_row);
22949 f = XFRAME (w->frame);
22951 if (updated_row->full_width_p)
22952 max_x = WINDOW_TOTAL_WIDTH (w);
22953 else
22954 max_x = window_box_width (w, updated_area);
22955 max_y = window_text_bottom_y (w);
22957 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22958 of window. For TO_X > 0, truncate to end of drawing area. */
22959 if (to_x == 0)
22960 return;
22961 else if (to_x < 0)
22962 to_x = max_x;
22963 else
22964 to_x = min (to_x, max_x);
22966 to_y = min (max_y, output_cursor.y + updated_row->height);
22968 /* Notice if the cursor will be cleared by this operation. */
22969 if (!updated_row->full_width_p)
22970 notice_overwritten_cursor (w, updated_area,
22971 output_cursor.x, -1,
22972 updated_row->y,
22973 MATRIX_ROW_BOTTOM_Y (updated_row));
22975 from_x = output_cursor.x;
22977 /* Translate to frame coordinates. */
22978 if (updated_row->full_width_p)
22980 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22981 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22983 else
22985 int area_left = window_box_left (w, updated_area);
22986 from_x += area_left;
22987 to_x += area_left;
22990 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22991 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22992 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22994 /* Prevent inadvertently clearing to end of the X window. */
22995 if (to_x > from_x && to_y > from_y)
22997 BLOCK_INPUT;
22998 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22999 to_x - from_x, to_y - from_y);
23000 UNBLOCK_INPUT;
23004 #endif /* HAVE_WINDOW_SYSTEM */
23008 /***********************************************************************
23009 Cursor types
23010 ***********************************************************************/
23012 /* Value is the internal representation of the specified cursor type
23013 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23014 of the bar cursor. */
23016 static enum text_cursor_kinds
23017 get_specified_cursor_type (Lisp_Object arg, int *width)
23019 enum text_cursor_kinds type;
23021 if (NILP (arg))
23022 return NO_CURSOR;
23024 if (EQ (arg, Qbox))
23025 return FILLED_BOX_CURSOR;
23027 if (EQ (arg, Qhollow))
23028 return HOLLOW_BOX_CURSOR;
23030 if (EQ (arg, Qbar))
23032 *width = 2;
23033 return BAR_CURSOR;
23036 if (CONSP (arg)
23037 && EQ (XCAR (arg), Qbar)
23038 && INTEGERP (XCDR (arg))
23039 && XINT (XCDR (arg)) >= 0)
23041 *width = XINT (XCDR (arg));
23042 return BAR_CURSOR;
23045 if (EQ (arg, Qhbar))
23047 *width = 2;
23048 return HBAR_CURSOR;
23051 if (CONSP (arg)
23052 && EQ (XCAR (arg), Qhbar)
23053 && INTEGERP (XCDR (arg))
23054 && XINT (XCDR (arg)) >= 0)
23056 *width = XINT (XCDR (arg));
23057 return HBAR_CURSOR;
23060 /* Treat anything unknown as "hollow box cursor".
23061 It was bad to signal an error; people have trouble fixing
23062 .Xdefaults with Emacs, when it has something bad in it. */
23063 type = HOLLOW_BOX_CURSOR;
23065 return type;
23068 /* Set the default cursor types for specified frame. */
23069 void
23070 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
23072 int width;
23073 Lisp_Object tem;
23075 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
23076 FRAME_CURSOR_WIDTH (f) = width;
23078 /* By default, set up the blink-off state depending on the on-state. */
23080 tem = Fassoc (arg, Vblink_cursor_alist);
23081 if (!NILP (tem))
23083 FRAME_BLINK_OFF_CURSOR (f)
23084 = get_specified_cursor_type (XCDR (tem), &width);
23085 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
23087 else
23088 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
23092 /* Return the cursor we want to be displayed in window W. Return
23093 width of bar/hbar cursor through WIDTH arg. Return with
23094 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
23095 (i.e. if the `system caret' should track this cursor).
23097 In a mini-buffer window, we want the cursor only to appear if we
23098 are reading input from this window. For the selected window, we
23099 want the cursor type given by the frame parameter or buffer local
23100 setting of cursor-type. If explicitly marked off, draw no cursor.
23101 In all other cases, we want a hollow box cursor. */
23103 static enum text_cursor_kinds
23104 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
23105 int *active_cursor)
23107 struct frame *f = XFRAME (w->frame);
23108 struct buffer *b = XBUFFER (w->buffer);
23109 int cursor_type = DEFAULT_CURSOR;
23110 Lisp_Object alt_cursor;
23111 int non_selected = 0;
23113 *active_cursor = 1;
23115 /* Echo area */
23116 if (cursor_in_echo_area
23117 && FRAME_HAS_MINIBUF_P (f)
23118 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
23120 if (w == XWINDOW (echo_area_window))
23122 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
23124 *width = FRAME_CURSOR_WIDTH (f);
23125 return FRAME_DESIRED_CURSOR (f);
23127 else
23128 return get_specified_cursor_type (b->cursor_type, width);
23131 *active_cursor = 0;
23132 non_selected = 1;
23135 /* Detect a nonselected window or nonselected frame. */
23136 else if (w != XWINDOW (f->selected_window)
23137 #ifdef HAVE_WINDOW_SYSTEM
23138 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
23139 #endif
23142 *active_cursor = 0;
23144 if (MINI_WINDOW_P (w) && minibuf_level == 0)
23145 return NO_CURSOR;
23147 non_selected = 1;
23150 /* Never display a cursor in a window in which cursor-type is nil. */
23151 if (NILP (b->cursor_type))
23152 return NO_CURSOR;
23154 /* Get the normal cursor type for this window. */
23155 if (EQ (b->cursor_type, Qt))
23157 cursor_type = FRAME_DESIRED_CURSOR (f);
23158 *width = FRAME_CURSOR_WIDTH (f);
23160 else
23161 cursor_type = get_specified_cursor_type (b->cursor_type, width);
23163 /* Use cursor-in-non-selected-windows instead
23164 for non-selected window or frame. */
23165 if (non_selected)
23167 alt_cursor = b->cursor_in_non_selected_windows;
23168 if (!EQ (Qt, alt_cursor))
23169 return get_specified_cursor_type (alt_cursor, width);
23170 /* t means modify the normal cursor type. */
23171 if (cursor_type == FILLED_BOX_CURSOR)
23172 cursor_type = HOLLOW_BOX_CURSOR;
23173 else if (cursor_type == BAR_CURSOR && *width > 1)
23174 --*width;
23175 return cursor_type;
23178 /* Use normal cursor if not blinked off. */
23179 if (!w->cursor_off_p)
23181 #ifdef HAVE_WINDOW_SYSTEM
23182 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23184 if (cursor_type == FILLED_BOX_CURSOR)
23186 /* Using a block cursor on large images can be very annoying.
23187 So use a hollow cursor for "large" images.
23188 If image is not transparent (no mask), also use hollow cursor. */
23189 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23190 if (img != NULL && IMAGEP (img->spec))
23192 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
23193 where N = size of default frame font size.
23194 This should cover most of the "tiny" icons people may use. */
23195 if (!img->mask
23196 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
23197 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
23198 cursor_type = HOLLOW_BOX_CURSOR;
23201 else if (cursor_type != NO_CURSOR)
23203 /* Display current only supports BOX and HOLLOW cursors for images.
23204 So for now, unconditionally use a HOLLOW cursor when cursor is
23205 not a solid box cursor. */
23206 cursor_type = HOLLOW_BOX_CURSOR;
23209 #endif
23210 return cursor_type;
23213 /* Cursor is blinked off, so determine how to "toggle" it. */
23215 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
23216 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
23217 return get_specified_cursor_type (XCDR (alt_cursor), width);
23219 /* Then see if frame has specified a specific blink off cursor type. */
23220 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
23222 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
23223 return FRAME_BLINK_OFF_CURSOR (f);
23226 #if 0
23227 /* Some people liked having a permanently visible blinking cursor,
23228 while others had very strong opinions against it. So it was
23229 decided to remove it. KFS 2003-09-03 */
23231 /* Finally perform built-in cursor blinking:
23232 filled box <-> hollow box
23233 wide [h]bar <-> narrow [h]bar
23234 narrow [h]bar <-> no cursor
23235 other type <-> no cursor */
23237 if (cursor_type == FILLED_BOX_CURSOR)
23238 return HOLLOW_BOX_CURSOR;
23240 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
23242 *width = 1;
23243 return cursor_type;
23245 #endif
23247 return NO_CURSOR;
23251 #ifdef HAVE_WINDOW_SYSTEM
23253 /* Notice when the text cursor of window W has been completely
23254 overwritten by a drawing operation that outputs glyphs in AREA
23255 starting at X0 and ending at X1 in the line starting at Y0 and
23256 ending at Y1. X coordinates are area-relative. X1 < 0 means all
23257 the rest of the line after X0 has been written. Y coordinates
23258 are window-relative. */
23260 static void
23261 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
23262 int x0, int x1, int y0, int y1)
23264 int cx0, cx1, cy0, cy1;
23265 struct glyph_row *row;
23267 if (!w->phys_cursor_on_p)
23268 return;
23269 if (area != TEXT_AREA)
23270 return;
23272 if (w->phys_cursor.vpos < 0
23273 || w->phys_cursor.vpos >= w->current_matrix->nrows
23274 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
23275 !(row->enabled_p && row->displays_text_p)))
23276 return;
23278 if (row->cursor_in_fringe_p)
23280 row->cursor_in_fringe_p = 0;
23281 draw_fringe_bitmap (w, row, row->reversed_p);
23282 w->phys_cursor_on_p = 0;
23283 return;
23286 cx0 = w->phys_cursor.x;
23287 cx1 = cx0 + w->phys_cursor_width;
23288 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
23289 return;
23291 /* The cursor image will be completely removed from the
23292 screen if the output area intersects the cursor area in
23293 y-direction. When we draw in [y0 y1[, and some part of
23294 the cursor is at y < y0, that part must have been drawn
23295 before. When scrolling, the cursor is erased before
23296 actually scrolling, so we don't come here. When not
23297 scrolling, the rows above the old cursor row must have
23298 changed, and in this case these rows must have written
23299 over the cursor image.
23301 Likewise if part of the cursor is below y1, with the
23302 exception of the cursor being in the first blank row at
23303 the buffer and window end because update_text_area
23304 doesn't draw that row. (Except when it does, but
23305 that's handled in update_text_area.) */
23307 cy0 = w->phys_cursor.y;
23308 cy1 = cy0 + w->phys_cursor_height;
23309 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
23310 return;
23312 w->phys_cursor_on_p = 0;
23315 #endif /* HAVE_WINDOW_SYSTEM */
23318 /************************************************************************
23319 Mouse Face
23320 ************************************************************************/
23322 #ifdef HAVE_WINDOW_SYSTEM
23324 /* EXPORT for RIF:
23325 Fix the display of area AREA of overlapping row ROW in window W
23326 with respect to the overlapping part OVERLAPS. */
23328 void
23329 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
23330 enum glyph_row_area area, int overlaps)
23332 int i, x;
23334 BLOCK_INPUT;
23336 x = 0;
23337 for (i = 0; i < row->used[area];)
23339 if (row->glyphs[area][i].overlaps_vertically_p)
23341 int start = i, start_x = x;
23345 x += row->glyphs[area][i].pixel_width;
23346 ++i;
23348 while (i < row->used[area]
23349 && row->glyphs[area][i].overlaps_vertically_p);
23351 draw_glyphs (w, start_x, row, area,
23352 start, i,
23353 DRAW_NORMAL_TEXT, overlaps);
23355 else
23357 x += row->glyphs[area][i].pixel_width;
23358 ++i;
23362 UNBLOCK_INPUT;
23366 /* EXPORT:
23367 Draw the cursor glyph of window W in glyph row ROW. See the
23368 comment of draw_glyphs for the meaning of HL. */
23370 void
23371 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
23372 enum draw_glyphs_face hl)
23374 /* If cursor hpos is out of bounds, don't draw garbage. This can
23375 happen in mini-buffer windows when switching between echo area
23376 glyphs and mini-buffer. */
23377 if ((row->reversed_p
23378 ? (w->phys_cursor.hpos >= 0)
23379 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
23381 int on_p = w->phys_cursor_on_p;
23382 int x1;
23383 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
23384 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
23385 hl, 0);
23386 w->phys_cursor_on_p = on_p;
23388 if (hl == DRAW_CURSOR)
23389 w->phys_cursor_width = x1 - w->phys_cursor.x;
23390 /* When we erase the cursor, and ROW is overlapped by other
23391 rows, make sure that these overlapping parts of other rows
23392 are redrawn. */
23393 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
23395 w->phys_cursor_width = x1 - w->phys_cursor.x;
23397 if (row > w->current_matrix->rows
23398 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
23399 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
23400 OVERLAPS_ERASED_CURSOR);
23402 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
23403 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
23404 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
23405 OVERLAPS_ERASED_CURSOR);
23411 /* EXPORT:
23412 Erase the image of a cursor of window W from the screen. */
23414 void
23415 erase_phys_cursor (struct window *w)
23417 struct frame *f = XFRAME (w->frame);
23418 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23419 int hpos = w->phys_cursor.hpos;
23420 int vpos = w->phys_cursor.vpos;
23421 int mouse_face_here_p = 0;
23422 struct glyph_matrix *active_glyphs = w->current_matrix;
23423 struct glyph_row *cursor_row;
23424 struct glyph *cursor_glyph;
23425 enum draw_glyphs_face hl;
23427 /* No cursor displayed or row invalidated => nothing to do on the
23428 screen. */
23429 if (w->phys_cursor_type == NO_CURSOR)
23430 goto mark_cursor_off;
23432 /* VPOS >= active_glyphs->nrows means that window has been resized.
23433 Don't bother to erase the cursor. */
23434 if (vpos >= active_glyphs->nrows)
23435 goto mark_cursor_off;
23437 /* If row containing cursor is marked invalid, there is nothing we
23438 can do. */
23439 cursor_row = MATRIX_ROW (active_glyphs, vpos);
23440 if (!cursor_row->enabled_p)
23441 goto mark_cursor_off;
23443 /* If line spacing is > 0, old cursor may only be partially visible in
23444 window after split-window. So adjust visible height. */
23445 cursor_row->visible_height = min (cursor_row->visible_height,
23446 window_text_bottom_y (w) - cursor_row->y);
23448 /* If row is completely invisible, don't attempt to delete a cursor which
23449 isn't there. This can happen if cursor is at top of a window, and
23450 we switch to a buffer with a header line in that window. */
23451 if (cursor_row->visible_height <= 0)
23452 goto mark_cursor_off;
23454 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
23455 if (cursor_row->cursor_in_fringe_p)
23457 cursor_row->cursor_in_fringe_p = 0;
23458 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
23459 goto mark_cursor_off;
23462 /* This can happen when the new row is shorter than the old one.
23463 In this case, either draw_glyphs or clear_end_of_line
23464 should have cleared the cursor. Note that we wouldn't be
23465 able to erase the cursor in this case because we don't have a
23466 cursor glyph at hand. */
23467 if ((cursor_row->reversed_p
23468 ? (w->phys_cursor.hpos < 0)
23469 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
23470 goto mark_cursor_off;
23472 /* If the cursor is in the mouse face area, redisplay that when
23473 we clear the cursor. */
23474 if (! NILP (dpyinfo->mouse_face_window)
23475 && w == XWINDOW (dpyinfo->mouse_face_window)
23476 && (vpos > dpyinfo->mouse_face_beg_row
23477 || (vpos == dpyinfo->mouse_face_beg_row
23478 && hpos >= dpyinfo->mouse_face_beg_col))
23479 && (vpos < dpyinfo->mouse_face_end_row
23480 || (vpos == dpyinfo->mouse_face_end_row
23481 && hpos < dpyinfo->mouse_face_end_col))
23482 /* Don't redraw the cursor's spot in mouse face if it is at the
23483 end of a line (on a newline). The cursor appears there, but
23484 mouse highlighting does not. */
23485 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
23486 mouse_face_here_p = 1;
23488 /* Maybe clear the display under the cursor. */
23489 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
23491 int x, y, left_x;
23492 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
23493 int width;
23495 cursor_glyph = get_phys_cursor_glyph (w);
23496 if (cursor_glyph == NULL)
23497 goto mark_cursor_off;
23499 width = cursor_glyph->pixel_width;
23500 left_x = window_box_left_offset (w, TEXT_AREA);
23501 x = w->phys_cursor.x;
23502 if (x < left_x)
23503 width -= left_x - x;
23504 width = min (width, window_box_width (w, TEXT_AREA) - x);
23505 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
23506 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
23508 if (width > 0)
23509 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
23512 /* Erase the cursor by redrawing the character underneath it. */
23513 if (mouse_face_here_p)
23514 hl = DRAW_MOUSE_FACE;
23515 else
23516 hl = DRAW_NORMAL_TEXT;
23517 draw_phys_cursor_glyph (w, cursor_row, hl);
23519 mark_cursor_off:
23520 w->phys_cursor_on_p = 0;
23521 w->phys_cursor_type = NO_CURSOR;
23525 /* EXPORT:
23526 Display or clear cursor of window W. If ON is zero, clear the
23527 cursor. If it is non-zero, display the cursor. If ON is nonzero,
23528 where to put the cursor is specified by HPOS, VPOS, X and Y. */
23530 void
23531 display_and_set_cursor (struct window *w, int on,
23532 int hpos, int vpos, int x, int y)
23534 struct frame *f = XFRAME (w->frame);
23535 int new_cursor_type;
23536 int new_cursor_width;
23537 int active_cursor;
23538 struct glyph_row *glyph_row;
23539 struct glyph *glyph;
23541 /* This is pointless on invisible frames, and dangerous on garbaged
23542 windows and frames; in the latter case, the frame or window may
23543 be in the midst of changing its size, and x and y may be off the
23544 window. */
23545 if (! FRAME_VISIBLE_P (f)
23546 || FRAME_GARBAGED_P (f)
23547 || vpos >= w->current_matrix->nrows
23548 || hpos >= w->current_matrix->matrix_w)
23549 return;
23551 /* If cursor is off and we want it off, return quickly. */
23552 if (!on && !w->phys_cursor_on_p)
23553 return;
23555 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
23556 /* If cursor row is not enabled, we don't really know where to
23557 display the cursor. */
23558 if (!glyph_row->enabled_p)
23560 w->phys_cursor_on_p = 0;
23561 return;
23564 glyph = NULL;
23565 if (!glyph_row->exact_window_width_line_p
23566 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
23567 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
23569 xassert (interrupt_input_blocked);
23571 /* Set new_cursor_type to the cursor we want to be displayed. */
23572 new_cursor_type = get_window_cursor_type (w, glyph,
23573 &new_cursor_width, &active_cursor);
23575 /* If cursor is currently being shown and we don't want it to be or
23576 it is in the wrong place, or the cursor type is not what we want,
23577 erase it. */
23578 if (w->phys_cursor_on_p
23579 && (!on
23580 || w->phys_cursor.x != x
23581 || w->phys_cursor.y != y
23582 || new_cursor_type != w->phys_cursor_type
23583 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
23584 && new_cursor_width != w->phys_cursor_width)))
23585 erase_phys_cursor (w);
23587 /* Don't check phys_cursor_on_p here because that flag is only set
23588 to zero in some cases where we know that the cursor has been
23589 completely erased, to avoid the extra work of erasing the cursor
23590 twice. In other words, phys_cursor_on_p can be 1 and the cursor
23591 still not be visible, or it has only been partly erased. */
23592 if (on)
23594 w->phys_cursor_ascent = glyph_row->ascent;
23595 w->phys_cursor_height = glyph_row->height;
23597 /* Set phys_cursor_.* before x_draw_.* is called because some
23598 of them may need the information. */
23599 w->phys_cursor.x = x;
23600 w->phys_cursor.y = glyph_row->y;
23601 w->phys_cursor.hpos = hpos;
23602 w->phys_cursor.vpos = vpos;
23605 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
23606 new_cursor_type, new_cursor_width,
23607 on, active_cursor);
23611 /* Switch the display of W's cursor on or off, according to the value
23612 of ON. */
23614 void
23615 update_window_cursor (struct window *w, int on)
23617 /* Don't update cursor in windows whose frame is in the process
23618 of being deleted. */
23619 if (w->current_matrix)
23621 BLOCK_INPUT;
23622 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
23623 w->phys_cursor.x, w->phys_cursor.y);
23624 UNBLOCK_INPUT;
23629 /* Call update_window_cursor with parameter ON_P on all leaf windows
23630 in the window tree rooted at W. */
23632 static void
23633 update_cursor_in_window_tree (struct window *w, int on_p)
23635 while (w)
23637 if (!NILP (w->hchild))
23638 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
23639 else if (!NILP (w->vchild))
23640 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
23641 else
23642 update_window_cursor (w, on_p);
23644 w = NILP (w->next) ? 0 : XWINDOW (w->next);
23649 /* EXPORT:
23650 Display the cursor on window W, or clear it, according to ON_P.
23651 Don't change the cursor's position. */
23653 void
23654 x_update_cursor (struct frame *f, int on_p)
23656 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
23660 /* EXPORT:
23661 Clear the cursor of window W to background color, and mark the
23662 cursor as not shown. This is used when the text where the cursor
23663 is about to be rewritten. */
23665 void
23666 x_clear_cursor (struct window *w)
23668 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
23669 update_window_cursor (w, 0);
23673 /* EXPORT:
23674 Display the active region described by mouse_face_* according to DRAW. */
23676 void
23677 show_mouse_face (Display_Info *dpyinfo, enum draw_glyphs_face draw)
23679 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
23680 struct frame *f = XFRAME (WINDOW_FRAME (w));
23682 if (/* If window is in the process of being destroyed, don't bother
23683 to do anything. */
23684 w->current_matrix != NULL
23685 /* Don't update mouse highlight if hidden */
23686 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
23687 /* Recognize when we are called to operate on rows that don't exist
23688 anymore. This can happen when a window is split. */
23689 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
23691 int phys_cursor_on_p = w->phys_cursor_on_p;
23692 struct glyph_row *row, *first, *last;
23694 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
23695 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
23697 for (row = first; row <= last && row->enabled_p; ++row)
23699 int start_hpos, end_hpos, start_x;
23701 /* For all but the first row, the highlight starts at column 0. */
23702 if (row == first)
23704 start_hpos = dpyinfo->mouse_face_beg_col;
23705 start_x = dpyinfo->mouse_face_beg_x;
23707 else
23709 start_hpos = 0;
23710 start_x = 0;
23713 if (row == last)
23714 end_hpos = dpyinfo->mouse_face_end_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_glyphs (w, start_x, row, TEXT_AREA,
23725 start_hpos, end_hpos,
23726 draw, 0);
23728 row->mouse_face_p
23729 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
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;
23745 /* Change the mouse cursor. */
23746 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
23747 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
23748 else if (draw == DRAW_MOUSE_FACE)
23749 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
23750 else
23751 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
23754 /* EXPORT:
23755 Clear out the mouse-highlighted active region.
23756 Redraw it un-highlighted first. Value is non-zero if mouse
23757 face was actually drawn unhighlighted. */
23760 clear_mouse_face (Display_Info *dpyinfo)
23762 int cleared = 0;
23764 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
23766 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
23767 cleared = 1;
23770 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23771 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23772 dpyinfo->mouse_face_window = Qnil;
23773 dpyinfo->mouse_face_overlay = Qnil;
23774 return cleared;
23778 /* EXPORT:
23779 Non-zero if physical cursor of window W is within mouse face. */
23782 cursor_in_mouse_face_p (struct window *w)
23784 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23785 int in_mouse_face = 0;
23787 if (WINDOWP (dpyinfo->mouse_face_window)
23788 && XWINDOW (dpyinfo->mouse_face_window) == w)
23790 int hpos = w->phys_cursor.hpos;
23791 int vpos = w->phys_cursor.vpos;
23793 if (vpos >= dpyinfo->mouse_face_beg_row
23794 && vpos <= dpyinfo->mouse_face_end_row
23795 && (vpos > dpyinfo->mouse_face_beg_row
23796 || hpos >= dpyinfo->mouse_face_beg_col)
23797 && (vpos < dpyinfo->mouse_face_end_row
23798 || hpos < dpyinfo->mouse_face_end_col
23799 || dpyinfo->mouse_face_past_end))
23800 in_mouse_face = 1;
23803 return in_mouse_face;
23809 /* This function sets the mouse_face_* elements of DPYINFO, assuming
23810 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
23811 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
23812 for the overlay or run of text properties specifying the mouse
23813 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
23814 before-string and after-string that must also be highlighted.
23815 DISPLAY_STRING, if non-nil, is a display string that may cover some
23816 or all of the highlighted text. */
23818 static void
23819 mouse_face_from_buffer_pos (Lisp_Object window,
23820 Display_Info *dpyinfo,
23821 EMACS_INT mouse_charpos,
23822 EMACS_INT start_charpos,
23823 EMACS_INT end_charpos,
23824 Lisp_Object before_string,
23825 Lisp_Object after_string,
23826 Lisp_Object display_string)
23828 struct window *w = XWINDOW (window);
23829 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23830 struct glyph_row *row;
23831 struct glyph *glyph, *end;
23832 EMACS_INT ignore;
23833 int x;
23835 xassert (NILP (display_string) || STRINGP (display_string));
23836 xassert (NILP (before_string) || STRINGP (before_string));
23837 xassert (NILP (after_string) || STRINGP (after_string));
23839 /* Find the first highlighted glyph. */
23840 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
23842 dpyinfo->mouse_face_beg_col = 0;
23843 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
23844 dpyinfo->mouse_face_beg_x = first->x;
23845 dpyinfo->mouse_face_beg_y = first->y;
23847 else
23849 row = row_containing_pos (w, start_charpos, first, NULL, 0);
23850 if (row == NULL)
23851 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23853 /* If the before-string or display-string contains newlines,
23854 row_containing_pos skips to its last row. Move back. */
23855 if (!NILP (before_string) || !NILP (display_string))
23857 struct glyph_row *prev;
23858 while ((prev = row - 1, prev >= first)
23859 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
23860 && prev->used[TEXT_AREA] > 0)
23862 struct glyph *beg = prev->glyphs[TEXT_AREA];
23863 glyph = beg + prev->used[TEXT_AREA];
23864 while (--glyph >= beg && INTEGERP (glyph->object));
23865 if (glyph < beg
23866 || !(EQ (glyph->object, before_string)
23867 || EQ (glyph->object, display_string)))
23868 break;
23869 row = prev;
23873 glyph = row->glyphs[TEXT_AREA];
23874 end = glyph + row->used[TEXT_AREA];
23875 x = row->x;
23876 dpyinfo->mouse_face_beg_y = row->y;
23877 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23879 /* Skip truncation glyphs at the start of the glyph row. */
23880 if (row->displays_text_p)
23881 for (; glyph < end
23882 && INTEGERP (glyph->object)
23883 && glyph->charpos < 0;
23884 ++glyph)
23885 x += glyph->pixel_width;
23887 /* Scan the glyph row, stopping before BEFORE_STRING or
23888 DISPLAY_STRING or START_CHARPOS. */
23889 for (; glyph < end
23890 && !INTEGERP (glyph->object)
23891 && !EQ (glyph->object, before_string)
23892 && !EQ (glyph->object, display_string)
23893 && !(BUFFERP (glyph->object)
23894 && glyph->charpos >= start_charpos);
23895 ++glyph)
23896 x += glyph->pixel_width;
23898 dpyinfo->mouse_face_beg_x = x;
23899 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
23902 /* Find the last highlighted glyph. */
23903 row = row_containing_pos (w, end_charpos, first, NULL, 0);
23904 if (row == NULL)
23906 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23907 dpyinfo->mouse_face_past_end = 1;
23909 else if (!NILP (after_string))
23911 /* If the after-string has newlines, advance to its last row. */
23912 struct glyph_row *next;
23913 struct glyph_row *last
23914 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
23916 for (next = row + 1;
23917 next <= last
23918 && next->used[TEXT_AREA] > 0
23919 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
23920 ++next)
23921 row = next;
23924 glyph = row->glyphs[TEXT_AREA];
23925 end = glyph + row->used[TEXT_AREA];
23926 x = row->x;
23927 dpyinfo->mouse_face_end_y = row->y;
23928 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
23930 /* Skip truncation glyphs at the start of the row. */
23931 if (row->displays_text_p)
23932 for (; glyph < end
23933 && INTEGERP (glyph->object)
23934 && glyph->charpos < 0;
23935 ++glyph)
23936 x += glyph->pixel_width;
23938 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23939 AFTER_STRING. */
23940 for (; glyph < end
23941 && !INTEGERP (glyph->object)
23942 && !EQ (glyph->object, after_string)
23943 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23944 ++glyph)
23945 x += glyph->pixel_width;
23947 /* If we found AFTER_STRING, consume it and stop. */
23948 if (EQ (glyph->object, after_string))
23950 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23951 x += glyph->pixel_width;
23953 else
23955 /* If there's no after-string, we must check if we overshot,
23956 which might be the case if we stopped after a string glyph.
23957 That glyph may belong to a before-string or display-string
23958 associated with the end position, which must not be
23959 highlighted. */
23960 Lisp_Object prev_object;
23961 EMACS_INT pos;
23963 while (glyph > row->glyphs[TEXT_AREA])
23965 prev_object = (glyph - 1)->object;
23966 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23967 break;
23969 pos = string_buffer_position (w, prev_object, end_charpos);
23970 if (pos && pos < end_charpos)
23971 break;
23973 for (; glyph > row->glyphs[TEXT_AREA]
23974 && EQ ((glyph - 1)->object, prev_object);
23975 --glyph)
23976 x -= (glyph - 1)->pixel_width;
23980 dpyinfo->mouse_face_end_x = x;
23981 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23982 dpyinfo->mouse_face_window = window;
23983 dpyinfo->mouse_face_face_id
23984 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23985 mouse_charpos + 1,
23986 !dpyinfo->mouse_face_hidden, -1);
23987 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23991 /* Find the position of the glyph for position POS in OBJECT in
23992 window W's current matrix, and return in *X, *Y the pixel
23993 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23995 RIGHT_P non-zero means return the position of the right edge of the
23996 glyph, RIGHT_P zero means return the left edge position.
23998 If no glyph for POS exists in the matrix, return the position of
23999 the glyph with the next smaller position that is in the matrix, if
24000 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
24001 exists in the matrix, return the position of the glyph with the
24002 next larger position in OBJECT.
24004 Value is non-zero if a glyph was found. */
24006 static int
24007 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
24008 int *hpos, int *vpos, int *x, int *y, int right_p)
24010 int yb = window_text_bottom_y (w);
24011 struct glyph_row *r;
24012 struct glyph *best_glyph = NULL;
24013 struct glyph_row *best_row = NULL;
24014 int best_x = 0;
24016 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24017 r->enabled_p && r->y < yb;
24018 ++r)
24020 struct glyph *g = r->glyphs[TEXT_AREA];
24021 struct glyph *e = g + r->used[TEXT_AREA];
24022 int gx;
24024 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
24025 if (EQ (g->object, object))
24027 if (g->charpos == pos)
24029 best_glyph = g;
24030 best_x = gx;
24031 best_row = r;
24032 goto found;
24034 else if (best_glyph == NULL
24035 || ((eabs (g->charpos - pos)
24036 < eabs (best_glyph->charpos - pos))
24037 && (right_p
24038 ? g->charpos < pos
24039 : g->charpos > pos)))
24041 best_glyph = g;
24042 best_x = gx;
24043 best_row = r;
24048 found:
24050 if (best_glyph)
24052 *x = best_x;
24053 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
24055 if (right_p)
24057 *x += best_glyph->pixel_width;
24058 ++*hpos;
24061 *y = best_row->y;
24062 *vpos = best_row - w->current_matrix->rows;
24065 return best_glyph != NULL;
24069 /* See if position X, Y is within a hot-spot of an image. */
24071 static int
24072 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
24074 if (!CONSP (hot_spot))
24075 return 0;
24077 if (EQ (XCAR (hot_spot), Qrect))
24079 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
24080 Lisp_Object rect = XCDR (hot_spot);
24081 Lisp_Object tem;
24082 if (!CONSP (rect))
24083 return 0;
24084 if (!CONSP (XCAR (rect)))
24085 return 0;
24086 if (!CONSP (XCDR (rect)))
24087 return 0;
24088 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
24089 return 0;
24090 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
24091 return 0;
24092 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
24093 return 0;
24094 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
24095 return 0;
24096 return 1;
24098 else if (EQ (XCAR (hot_spot), Qcircle))
24100 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
24101 Lisp_Object circ = XCDR (hot_spot);
24102 Lisp_Object lr, lx0, ly0;
24103 if (CONSP (circ)
24104 && CONSP (XCAR (circ))
24105 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
24106 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
24107 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
24109 double r = XFLOATINT (lr);
24110 double dx = XINT (lx0) - x;
24111 double dy = XINT (ly0) - y;
24112 return (dx * dx + dy * dy <= r * r);
24115 else if (EQ (XCAR (hot_spot), Qpoly))
24117 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
24118 if (VECTORP (XCDR (hot_spot)))
24120 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
24121 Lisp_Object *poly = v->contents;
24122 int n = v->size;
24123 int i;
24124 int inside = 0;
24125 Lisp_Object lx, ly;
24126 int x0, y0;
24128 /* Need an even number of coordinates, and at least 3 edges. */
24129 if (n < 6 || n & 1)
24130 return 0;
24132 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
24133 If count is odd, we are inside polygon. Pixels on edges
24134 may or may not be included depending on actual geometry of the
24135 polygon. */
24136 if ((lx = poly[n-2], !INTEGERP (lx))
24137 || (ly = poly[n-1], !INTEGERP (lx)))
24138 return 0;
24139 x0 = XINT (lx), y0 = XINT (ly);
24140 for (i = 0; i < n; i += 2)
24142 int x1 = x0, y1 = y0;
24143 if ((lx = poly[i], !INTEGERP (lx))
24144 || (ly = poly[i+1], !INTEGERP (ly)))
24145 return 0;
24146 x0 = XINT (lx), y0 = XINT (ly);
24148 /* Does this segment cross the X line? */
24149 if (x0 >= x)
24151 if (x1 >= x)
24152 continue;
24154 else if (x1 < x)
24155 continue;
24156 if (y > y0 && y > y1)
24157 continue;
24158 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
24159 inside = !inside;
24161 return inside;
24164 return 0;
24167 Lisp_Object
24168 find_hot_spot (Lisp_Object map, int x, int y)
24170 while (CONSP (map))
24172 if (CONSP (XCAR (map))
24173 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
24174 return XCAR (map);
24175 map = XCDR (map);
24178 return Qnil;
24181 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
24182 3, 3, 0,
24183 doc: /* Lookup in image map MAP coordinates X and Y.
24184 An image map is an alist where each element has the format (AREA ID PLIST).
24185 An AREA is specified as either a rectangle, a circle, or a polygon:
24186 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
24187 pixel coordinates of the upper left and bottom right corners.
24188 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
24189 and the radius of the circle; r may be a float or integer.
24190 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
24191 vector describes one corner in the polygon.
24192 Returns the alist element for the first matching AREA in MAP. */)
24193 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
24195 if (NILP (map))
24196 return Qnil;
24198 CHECK_NUMBER (x);
24199 CHECK_NUMBER (y);
24201 return find_hot_spot (map, XINT (x), XINT (y));
24205 /* Display frame CURSOR, optionally using shape defined by POINTER. */
24206 static void
24207 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
24209 /* Do not change cursor shape while dragging mouse. */
24210 if (!NILP (do_mouse_tracking))
24211 return;
24213 if (!NILP (pointer))
24215 if (EQ (pointer, Qarrow))
24216 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24217 else if (EQ (pointer, Qhand))
24218 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
24219 else if (EQ (pointer, Qtext))
24220 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24221 else if (EQ (pointer, intern ("hdrag")))
24222 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24223 #ifdef HAVE_X_WINDOWS
24224 else if (EQ (pointer, intern ("vdrag")))
24225 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
24226 #endif
24227 else if (EQ (pointer, intern ("hourglass")))
24228 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
24229 else if (EQ (pointer, Qmodeline))
24230 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
24231 else
24232 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24235 if (cursor != No_Cursor)
24236 FRAME_RIF (f)->define_frame_cursor (f, cursor);
24239 /* Take proper action when mouse has moved to the mode or header line
24240 or marginal area AREA of window W, x-position X and y-position Y.
24241 X is relative to the start of the text display area of W, so the
24242 width of bitmap areas and scroll bars must be subtracted to get a
24243 position relative to the start of the mode line. */
24245 static void
24246 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
24247 enum window_part area)
24249 struct window *w = XWINDOW (window);
24250 struct frame *f = XFRAME (w->frame);
24251 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24252 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24253 Lisp_Object pointer = Qnil;
24254 int charpos, dx, dy, width, height;
24255 Lisp_Object string, object = Qnil;
24256 Lisp_Object pos, help;
24258 Lisp_Object mouse_face;
24259 int original_x_pixel = x;
24260 struct glyph * glyph = NULL, * row_start_glyph = NULL;
24261 struct glyph_row *row;
24263 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
24265 int x0;
24266 struct glyph *end;
24268 string = mode_line_string (w, area, &x, &y, &charpos,
24269 &object, &dx, &dy, &width, &height);
24271 row = (area == ON_MODE_LINE
24272 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
24273 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
24275 /* Find glyph */
24276 if (row->mode_line_p && row->enabled_p)
24278 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
24279 end = glyph + row->used[TEXT_AREA];
24281 for (x0 = original_x_pixel;
24282 glyph < end && x0 >= glyph->pixel_width;
24283 ++glyph)
24284 x0 -= glyph->pixel_width;
24286 if (glyph >= end)
24287 glyph = NULL;
24290 else
24292 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
24293 string = marginal_area_string (w, area, &x, &y, &charpos,
24294 &object, &dx, &dy, &width, &height);
24297 help = Qnil;
24299 if (IMAGEP (object))
24301 Lisp_Object image_map, hotspot;
24302 if ((image_map = Fplist_get (XCDR (object), QCmap),
24303 !NILP (image_map))
24304 && (hotspot = find_hot_spot (image_map, dx, dy),
24305 CONSP (hotspot))
24306 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24308 Lisp_Object area_id, plist;
24310 area_id = XCAR (hotspot);
24311 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24312 If so, we could look for mouse-enter, mouse-leave
24313 properties in PLIST (and do something...). */
24314 hotspot = XCDR (hotspot);
24315 if (CONSP (hotspot)
24316 && (plist = XCAR (hotspot), CONSP (plist)))
24318 pointer = Fplist_get (plist, Qpointer);
24319 if (NILP (pointer))
24320 pointer = Qhand;
24321 help = Fplist_get (plist, Qhelp_echo);
24322 if (!NILP (help))
24324 help_echo_string = help;
24325 /* Is this correct? ++kfs */
24326 XSETWINDOW (help_echo_window, w);
24327 help_echo_object = w->buffer;
24328 help_echo_pos = charpos;
24332 if (NILP (pointer))
24333 pointer = Fplist_get (XCDR (object), QCpointer);
24336 if (STRINGP (string))
24338 pos = make_number (charpos);
24339 /* If we're on a string with `help-echo' text property, arrange
24340 for the help to be displayed. This is done by setting the
24341 global variable help_echo_string to the help string. */
24342 if (NILP (help))
24344 help = Fget_text_property (pos, Qhelp_echo, string);
24345 if (!NILP (help))
24347 help_echo_string = help;
24348 XSETWINDOW (help_echo_window, w);
24349 help_echo_object = string;
24350 help_echo_pos = charpos;
24354 if (NILP (pointer))
24355 pointer = Fget_text_property (pos, Qpointer, string);
24357 /* Change the mouse pointer according to what is under X/Y. */
24358 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
24360 Lisp_Object map;
24361 map = Fget_text_property (pos, Qlocal_map, string);
24362 if (!KEYMAPP (map))
24363 map = Fget_text_property (pos, Qkeymap, string);
24364 if (!KEYMAPP (map))
24365 cursor = dpyinfo->vertical_scroll_bar_cursor;
24368 /* Change the mouse face according to what is under X/Y. */
24369 mouse_face = Fget_text_property (pos, Qmouse_face, string);
24370 if (!NILP (mouse_face)
24371 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24372 && glyph)
24374 Lisp_Object b, e;
24376 struct glyph * tmp_glyph;
24378 int gpos;
24379 int gseq_length;
24380 int total_pixel_width;
24381 EMACS_INT ignore;
24383 int vpos, hpos;
24385 b = Fprevious_single_property_change (make_number (charpos + 1),
24386 Qmouse_face, string, Qnil);
24387 if (NILP (b))
24388 b = make_number (0);
24390 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
24391 if (NILP (e))
24392 e = make_number (SCHARS (string));
24394 /* Calculate the position(glyph position: GPOS) of GLYPH in
24395 displayed string. GPOS is different from CHARPOS.
24397 CHARPOS is the position of glyph in internal string
24398 object. A mode line string format has structures which
24399 is converted to a flatten by emacs lisp interpreter.
24400 The internal string is an element of the structures.
24401 The displayed string is the flatten string. */
24402 gpos = 0;
24403 if (glyph > row_start_glyph)
24405 tmp_glyph = glyph - 1;
24406 while (tmp_glyph >= row_start_glyph
24407 && tmp_glyph->charpos >= XINT (b)
24408 && EQ (tmp_glyph->object, glyph->object))
24410 tmp_glyph--;
24411 gpos++;
24415 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
24416 displayed string holding GLYPH.
24418 GSEQ_LENGTH is different from SCHARS (STRING).
24419 SCHARS (STRING) returns the length of the internal string. */
24420 for (tmp_glyph = glyph, gseq_length = gpos;
24421 tmp_glyph->charpos < XINT (e);
24422 tmp_glyph++, gseq_length++)
24424 if (!EQ (tmp_glyph->object, glyph->object))
24425 break;
24428 total_pixel_width = 0;
24429 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
24430 total_pixel_width += tmp_glyph->pixel_width;
24432 /* Pre calculation of re-rendering position */
24433 vpos = (x - gpos);
24434 hpos = (area == ON_MODE_LINE
24435 ? (w->current_matrix)->nrows - 1
24436 : 0);
24438 /* If the re-rendering position is included in the last
24439 re-rendering area, we should do nothing. */
24440 if ( EQ (window, dpyinfo->mouse_face_window)
24441 && dpyinfo->mouse_face_beg_col <= vpos
24442 && vpos < dpyinfo->mouse_face_end_col
24443 && dpyinfo->mouse_face_beg_row == hpos )
24444 return;
24446 if (clear_mouse_face (dpyinfo))
24447 cursor = No_Cursor;
24449 dpyinfo->mouse_face_beg_col = vpos;
24450 dpyinfo->mouse_face_beg_row = hpos;
24452 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
24453 dpyinfo->mouse_face_beg_y = 0;
24455 dpyinfo->mouse_face_end_col = vpos + gseq_length;
24456 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
24458 dpyinfo->mouse_face_end_x = 0;
24459 dpyinfo->mouse_face_end_y = 0;
24461 dpyinfo->mouse_face_past_end = 0;
24462 dpyinfo->mouse_face_window = window;
24464 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
24465 charpos,
24466 0, 0, 0, &ignore,
24467 glyph->face_id, 1);
24468 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24470 if (NILP (pointer))
24471 pointer = Qhand;
24473 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
24474 clear_mouse_face (dpyinfo);
24476 define_frame_cursor1 (f, cursor, pointer);
24480 /* EXPORT:
24481 Take proper action when the mouse has moved to position X, Y on
24482 frame F as regards highlighting characters that have mouse-face
24483 properties. Also de-highlighting chars where the mouse was before.
24484 X and Y can be negative or out of range. */
24486 void
24487 note_mouse_highlight (struct frame *f, int x, int y)
24489 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24490 enum window_part part;
24491 Lisp_Object window;
24492 struct window *w;
24493 Cursor cursor = No_Cursor;
24494 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
24495 struct buffer *b;
24497 /* When a menu is active, don't highlight because this looks odd. */
24498 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
24499 if (popup_activated ())
24500 return;
24501 #endif
24503 if (NILP (Vmouse_highlight)
24504 || !f->glyphs_initialized_p
24505 || f->pointer_invisible)
24506 return;
24508 dpyinfo->mouse_face_mouse_x = x;
24509 dpyinfo->mouse_face_mouse_y = y;
24510 dpyinfo->mouse_face_mouse_frame = f;
24512 if (dpyinfo->mouse_face_defer)
24513 return;
24515 if (gc_in_progress)
24517 dpyinfo->mouse_face_deferred_gc = 1;
24518 return;
24521 /* Which window is that in? */
24522 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
24524 /* If we were displaying active text in another window, clear that.
24525 Also clear if we move out of text area in same window. */
24526 if (! EQ (window, dpyinfo->mouse_face_window)
24527 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
24528 && !NILP (dpyinfo->mouse_face_window)))
24529 clear_mouse_face (dpyinfo);
24531 /* Not on a window -> return. */
24532 if (!WINDOWP (window))
24533 return;
24535 /* Reset help_echo_string. It will get recomputed below. */
24536 help_echo_string = Qnil;
24538 /* Convert to window-relative pixel coordinates. */
24539 w = XWINDOW (window);
24540 frame_to_window_pixel_xy (w, &x, &y);
24542 /* Handle tool-bar window differently since it doesn't display a
24543 buffer. */
24544 if (EQ (window, f->tool_bar_window))
24546 note_tool_bar_highlight (f, x, y);
24547 return;
24550 /* Mouse is on the mode, header line or margin? */
24551 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
24552 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
24554 note_mode_line_or_margin_highlight (window, x, y, part);
24555 return;
24558 if (part == ON_VERTICAL_BORDER)
24560 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
24561 help_echo_string = build_string ("drag-mouse-1: resize");
24563 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
24564 || part == ON_SCROLL_BAR)
24565 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24566 else
24567 cursor = FRAME_X_OUTPUT (f)->text_cursor;
24569 /* Are we in a window whose display is up to date?
24570 And verify the buffer's text has not changed. */
24571 b = XBUFFER (w->buffer);
24572 if (part == ON_TEXT
24573 && EQ (w->window_end_valid, w->buffer)
24574 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
24575 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
24577 int hpos, vpos, i, dx, dy, area;
24578 EMACS_INT pos;
24579 struct glyph *glyph;
24580 Lisp_Object object;
24581 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
24582 Lisp_Object *overlay_vec = NULL;
24583 int noverlays;
24584 struct buffer *obuf;
24585 int obegv, ozv, same_region;
24587 /* Find the glyph under X/Y. */
24588 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
24590 /* Look for :pointer property on image. */
24591 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24593 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24594 if (img != NULL && IMAGEP (img->spec))
24596 Lisp_Object image_map, hotspot;
24597 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
24598 !NILP (image_map))
24599 && (hotspot = find_hot_spot (image_map,
24600 glyph->slice.x + dx,
24601 glyph->slice.y + dy),
24602 CONSP (hotspot))
24603 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
24605 Lisp_Object area_id, plist;
24607 area_id = XCAR (hotspot);
24608 /* Could check AREA_ID to see if we enter/leave this hot-spot.
24609 If so, we could look for mouse-enter, mouse-leave
24610 properties in PLIST (and do something...). */
24611 hotspot = XCDR (hotspot);
24612 if (CONSP (hotspot)
24613 && (plist = XCAR (hotspot), CONSP (plist)))
24615 pointer = Fplist_get (plist, Qpointer);
24616 if (NILP (pointer))
24617 pointer = Qhand;
24618 help_echo_string = Fplist_get (plist, Qhelp_echo);
24619 if (!NILP (help_echo_string))
24621 help_echo_window = window;
24622 help_echo_object = glyph->object;
24623 help_echo_pos = glyph->charpos;
24627 if (NILP (pointer))
24628 pointer = Fplist_get (XCDR (img->spec), QCpointer);
24632 /* Clear mouse face if X/Y not over text. */
24633 if (glyph == NULL
24634 || area != TEXT_AREA
24635 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
24637 if (clear_mouse_face (dpyinfo))
24638 cursor = No_Cursor;
24639 if (NILP (pointer))
24641 if (area != TEXT_AREA)
24642 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
24643 else
24644 pointer = Vvoid_text_area_pointer;
24646 goto set_cursor;
24649 pos = glyph->charpos;
24650 object = glyph->object;
24651 if (!STRINGP (object) && !BUFFERP (object))
24652 goto set_cursor;
24654 /* If we get an out-of-range value, return now; avoid an error. */
24655 if (BUFFERP (object) && pos > BUF_Z (b))
24656 goto set_cursor;
24658 /* Make the window's buffer temporarily current for
24659 overlays_at and compute_char_face. */
24660 obuf = current_buffer;
24661 current_buffer = b;
24662 obegv = BEGV;
24663 ozv = ZV;
24664 BEGV = BEG;
24665 ZV = Z;
24667 /* Is this char mouse-active or does it have help-echo? */
24668 position = make_number (pos);
24670 if (BUFFERP (object))
24672 /* Put all the overlays we want in a vector in overlay_vec. */
24673 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
24674 /* Sort overlays into increasing priority order. */
24675 noverlays = sort_overlays (overlay_vec, noverlays, w);
24677 else
24678 noverlays = 0;
24680 same_region = (EQ (window, dpyinfo->mouse_face_window)
24681 && vpos >= dpyinfo->mouse_face_beg_row
24682 && vpos <= dpyinfo->mouse_face_end_row
24683 && (vpos > dpyinfo->mouse_face_beg_row
24684 || hpos >= dpyinfo->mouse_face_beg_col)
24685 && (vpos < dpyinfo->mouse_face_end_row
24686 || hpos < dpyinfo->mouse_face_end_col
24687 || dpyinfo->mouse_face_past_end));
24689 if (same_region)
24690 cursor = No_Cursor;
24692 /* Check mouse-face highlighting. */
24693 if (! same_region
24694 /* If there exists an overlay with mouse-face overlapping
24695 the one we are currently highlighting, we have to
24696 check if we enter the overlapping overlay, and then
24697 highlight only that. */
24698 || (OVERLAYP (dpyinfo->mouse_face_overlay)
24699 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
24701 /* Find the highest priority overlay with a mouse-face. */
24702 overlay = Qnil;
24703 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
24705 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
24706 if (!NILP (mouse_face))
24707 overlay = overlay_vec[i];
24710 /* If we're highlighting the same overlay as before, there's
24711 no need to do that again. */
24712 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
24713 goto check_help_echo;
24714 dpyinfo->mouse_face_overlay = overlay;
24716 /* Clear the display of the old active region, if any. */
24717 if (clear_mouse_face (dpyinfo))
24718 cursor = No_Cursor;
24720 /* If no overlay applies, get a text property. */
24721 if (NILP (overlay))
24722 mouse_face = Fget_text_property (position, Qmouse_face, object);
24724 /* Next, compute the bounds of the mouse highlighting and
24725 display it. */
24726 if (!NILP (mouse_face) && STRINGP (object))
24728 /* The mouse-highlighting comes from a display string
24729 with a mouse-face. */
24730 Lisp_Object b, e;
24731 EMACS_INT ignore;
24733 b = Fprevious_single_property_change
24734 (make_number (pos + 1), Qmouse_face, object, Qnil);
24735 e = Fnext_single_property_change
24736 (position, Qmouse_face, object, Qnil);
24737 if (NILP (b))
24738 b = make_number (0);
24739 if (NILP (e))
24740 e = make_number (SCHARS (object) - 1);
24742 fast_find_string_pos (w, XINT (b), object,
24743 &dpyinfo->mouse_face_beg_col,
24744 &dpyinfo->mouse_face_beg_row,
24745 &dpyinfo->mouse_face_beg_x,
24746 &dpyinfo->mouse_face_beg_y, 0);
24747 fast_find_string_pos (w, XINT (e), object,
24748 &dpyinfo->mouse_face_end_col,
24749 &dpyinfo->mouse_face_end_row,
24750 &dpyinfo->mouse_face_end_x,
24751 &dpyinfo->mouse_face_end_y, 1);
24752 dpyinfo->mouse_face_past_end = 0;
24753 dpyinfo->mouse_face_window = window;
24754 dpyinfo->mouse_face_face_id
24755 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
24756 glyph->face_id, 1);
24757 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
24758 cursor = No_Cursor;
24760 else
24762 /* The mouse-highlighting, if any, comes from an overlay
24763 or text property in the buffer. */
24764 Lisp_Object buffer, display_string;
24766 if (STRINGP (object))
24768 /* If we are on a display string with no mouse-face,
24769 check if the text under it has one. */
24770 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
24771 int start = MATRIX_ROW_START_CHARPOS (r);
24772 pos = string_buffer_position (w, object, start);
24773 if (pos > 0)
24775 mouse_face = get_char_property_and_overlay
24776 (make_number (pos), Qmouse_face, w->buffer, &overlay);
24777 buffer = w->buffer;
24778 display_string = object;
24781 else
24783 buffer = object;
24784 display_string = Qnil;
24787 if (!NILP (mouse_face))
24789 Lisp_Object before, after;
24790 Lisp_Object before_string, after_string;
24792 if (NILP (overlay))
24794 /* Handle the text property case. */
24795 before = Fprevious_single_property_change
24796 (make_number (pos + 1), Qmouse_face, buffer,
24797 Fmarker_position (w->start));
24798 after = Fnext_single_property_change
24799 (make_number (pos), Qmouse_face, buffer,
24800 make_number (BUF_Z (XBUFFER (buffer))
24801 - XFASTINT (w->window_end_pos)));
24802 before_string = after_string = Qnil;
24804 else
24806 /* Handle the overlay case. */
24807 before = Foverlay_start (overlay);
24808 after = Foverlay_end (overlay);
24809 before_string = Foverlay_get (overlay, Qbefore_string);
24810 after_string = Foverlay_get (overlay, Qafter_string);
24812 if (!STRINGP (before_string)) before_string = Qnil;
24813 if (!STRINGP (after_string)) after_string = Qnil;
24816 mouse_face_from_buffer_pos (window, dpyinfo, pos,
24817 XFASTINT (before),
24818 XFASTINT (after),
24819 before_string, after_string,
24820 display_string);
24821 cursor = No_Cursor;
24826 check_help_echo:
24828 /* Look for a `help-echo' property. */
24829 if (NILP (help_echo_string)) {
24830 Lisp_Object help, overlay;
24832 /* Check overlays first. */
24833 help = overlay = Qnil;
24834 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
24836 overlay = overlay_vec[i];
24837 help = Foverlay_get (overlay, Qhelp_echo);
24840 if (!NILP (help))
24842 help_echo_string = help;
24843 help_echo_window = window;
24844 help_echo_object = overlay;
24845 help_echo_pos = pos;
24847 else
24849 Lisp_Object object = glyph->object;
24850 int charpos = glyph->charpos;
24852 /* Try text properties. */
24853 if (STRINGP (object)
24854 && charpos >= 0
24855 && charpos < SCHARS (object))
24857 help = Fget_text_property (make_number (charpos),
24858 Qhelp_echo, object);
24859 if (NILP (help))
24861 /* If the string itself doesn't specify a help-echo,
24862 see if the buffer text ``under'' it does. */
24863 struct glyph_row *r
24864 = MATRIX_ROW (w->current_matrix, vpos);
24865 int start = MATRIX_ROW_START_CHARPOS (r);
24866 EMACS_INT pos = string_buffer_position (w, object, start);
24867 if (pos > 0)
24869 help = Fget_char_property (make_number (pos),
24870 Qhelp_echo, w->buffer);
24871 if (!NILP (help))
24873 charpos = pos;
24874 object = w->buffer;
24879 else if (BUFFERP (object)
24880 && charpos >= BEGV
24881 && charpos < ZV)
24882 help = Fget_text_property (make_number (charpos), Qhelp_echo,
24883 object);
24885 if (!NILP (help))
24887 help_echo_string = help;
24888 help_echo_window = window;
24889 help_echo_object = object;
24890 help_echo_pos = charpos;
24895 /* Look for a `pointer' property. */
24896 if (NILP (pointer))
24898 /* Check overlays first. */
24899 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
24900 pointer = Foverlay_get (overlay_vec[i], Qpointer);
24902 if (NILP (pointer))
24904 Lisp_Object object = glyph->object;
24905 int charpos = glyph->charpos;
24907 /* Try text properties. */
24908 if (STRINGP (object)
24909 && charpos >= 0
24910 && charpos < SCHARS (object))
24912 pointer = Fget_text_property (make_number (charpos),
24913 Qpointer, object);
24914 if (NILP (pointer))
24916 /* If the string itself doesn't specify a pointer,
24917 see if the buffer text ``under'' it does. */
24918 struct glyph_row *r
24919 = MATRIX_ROW (w->current_matrix, vpos);
24920 int start = MATRIX_ROW_START_CHARPOS (r);
24921 EMACS_INT pos = string_buffer_position (w, object,
24922 start);
24923 if (pos > 0)
24924 pointer = Fget_char_property (make_number (pos),
24925 Qpointer, w->buffer);
24928 else if (BUFFERP (object)
24929 && charpos >= BEGV
24930 && charpos < ZV)
24931 pointer = Fget_text_property (make_number (charpos),
24932 Qpointer, object);
24936 BEGV = obegv;
24937 ZV = ozv;
24938 current_buffer = obuf;
24941 set_cursor:
24943 define_frame_cursor1 (f, cursor, pointer);
24947 /* EXPORT for RIF:
24948 Clear any mouse-face on window W. This function is part of the
24949 redisplay interface, and is called from try_window_id and similar
24950 functions to ensure the mouse-highlight is off. */
24952 void
24953 x_clear_window_mouse_face (struct window *w)
24955 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24956 Lisp_Object window;
24958 BLOCK_INPUT;
24959 XSETWINDOW (window, w);
24960 if (EQ (window, dpyinfo->mouse_face_window))
24961 clear_mouse_face (dpyinfo);
24962 UNBLOCK_INPUT;
24966 /* EXPORT:
24967 Just discard the mouse face information for frame F, if any.
24968 This is used when the size of F is changed. */
24970 void
24971 cancel_mouse_face (struct frame *f)
24973 Lisp_Object window;
24974 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24976 window = dpyinfo->mouse_face_window;
24977 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24979 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24980 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24981 dpyinfo->mouse_face_window = Qnil;
24986 #endif /* HAVE_WINDOW_SYSTEM */
24989 /***********************************************************************
24990 Exposure Events
24991 ***********************************************************************/
24993 #ifdef HAVE_WINDOW_SYSTEM
24995 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24996 which intersects rectangle R. R is in window-relative coordinates. */
24998 static void
24999 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
25000 enum glyph_row_area area)
25002 struct glyph *first = row->glyphs[area];
25003 struct glyph *end = row->glyphs[area] + row->used[area];
25004 struct glyph *last;
25005 int first_x, start_x, x;
25007 if (area == TEXT_AREA && row->fill_line_p)
25008 /* If row extends face to end of line write the whole line. */
25009 draw_glyphs (w, 0, row, area,
25010 0, row->used[area],
25011 DRAW_NORMAL_TEXT, 0);
25012 else
25014 /* Set START_X to the window-relative start position for drawing glyphs of
25015 AREA. The first glyph of the text area can be partially visible.
25016 The first glyphs of other areas cannot. */
25017 start_x = window_box_left_offset (w, area);
25018 x = start_x;
25019 if (area == TEXT_AREA)
25020 x += row->x;
25022 /* Find the first glyph that must be redrawn. */
25023 while (first < end
25024 && x + first->pixel_width < r->x)
25026 x += first->pixel_width;
25027 ++first;
25030 /* Find the last one. */
25031 last = first;
25032 first_x = x;
25033 while (last < end
25034 && x < r->x + r->width)
25036 x += last->pixel_width;
25037 ++last;
25040 /* Repaint. */
25041 if (last > first)
25042 draw_glyphs (w, first_x - start_x, row, area,
25043 first - row->glyphs[area], last - row->glyphs[area],
25044 DRAW_NORMAL_TEXT, 0);
25049 /* Redraw the parts of the glyph row ROW on window W intersecting
25050 rectangle R. R is in window-relative coordinates. Value is
25051 non-zero if mouse-face was overwritten. */
25053 static int
25054 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
25056 xassert (row->enabled_p);
25058 if (row->mode_line_p || w->pseudo_window_p)
25059 draw_glyphs (w, 0, row, TEXT_AREA,
25060 0, row->used[TEXT_AREA],
25061 DRAW_NORMAL_TEXT, 0);
25062 else
25064 if (row->used[LEFT_MARGIN_AREA])
25065 expose_area (w, row, r, LEFT_MARGIN_AREA);
25066 if (row->used[TEXT_AREA])
25067 expose_area (w, row, r, TEXT_AREA);
25068 if (row->used[RIGHT_MARGIN_AREA])
25069 expose_area (w, row, r, RIGHT_MARGIN_AREA);
25070 draw_row_fringe_bitmaps (w, row);
25073 return row->mouse_face_p;
25077 /* Redraw those parts of glyphs rows during expose event handling that
25078 overlap other rows. Redrawing of an exposed line writes over parts
25079 of lines overlapping that exposed line; this function fixes that.
25081 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
25082 row in W's current matrix that is exposed and overlaps other rows.
25083 LAST_OVERLAPPING_ROW is the last such row. */
25085 static void
25086 expose_overlaps (struct window *w,
25087 struct glyph_row *first_overlapping_row,
25088 struct glyph_row *last_overlapping_row,
25089 XRectangle *r)
25091 struct glyph_row *row;
25093 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
25094 if (row->overlapping_p)
25096 xassert (row->enabled_p && !row->mode_line_p);
25098 row->clip = r;
25099 if (row->used[LEFT_MARGIN_AREA])
25100 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
25102 if (row->used[TEXT_AREA])
25103 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
25105 if (row->used[RIGHT_MARGIN_AREA])
25106 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
25107 row->clip = NULL;
25112 /* Return non-zero if W's cursor intersects rectangle R. */
25114 static int
25115 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
25117 XRectangle cr, result;
25118 struct glyph *cursor_glyph;
25119 struct glyph_row *row;
25121 if (w->phys_cursor.vpos >= 0
25122 && w->phys_cursor.vpos < w->current_matrix->nrows
25123 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
25124 row->enabled_p)
25125 && row->cursor_in_fringe_p)
25127 /* Cursor is in the fringe. */
25128 cr.x = window_box_right_offset (w,
25129 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
25130 ? RIGHT_MARGIN_AREA
25131 : TEXT_AREA));
25132 cr.y = row->y;
25133 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
25134 cr.height = row->height;
25135 return x_intersect_rectangles (&cr, r, &result);
25138 cursor_glyph = get_phys_cursor_glyph (w);
25139 if (cursor_glyph)
25141 /* r is relative to W's box, but w->phys_cursor.x is relative
25142 to left edge of W's TEXT area. Adjust it. */
25143 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
25144 cr.y = w->phys_cursor.y;
25145 cr.width = cursor_glyph->pixel_width;
25146 cr.height = w->phys_cursor_height;
25147 /* ++KFS: W32 version used W32-specific IntersectRect here, but
25148 I assume the effect is the same -- and this is portable. */
25149 return x_intersect_rectangles (&cr, r, &result);
25151 /* If we don't understand the format, pretend we're not in the hot-spot. */
25152 return 0;
25156 /* EXPORT:
25157 Draw a vertical window border to the right of window W if W doesn't
25158 have vertical scroll bars. */
25160 void
25161 x_draw_vertical_border (struct window *w)
25163 struct frame *f = XFRAME (WINDOW_FRAME (w));
25165 /* We could do better, if we knew what type of scroll-bar the adjacent
25166 windows (on either side) have... But we don't :-(
25167 However, I think this works ok. ++KFS 2003-04-25 */
25169 /* Redraw borders between horizontally adjacent windows. Don't
25170 do it for frames with vertical scroll bars because either the
25171 right scroll bar of a window, or the left scroll bar of its
25172 neighbor will suffice as a border. */
25173 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
25174 return;
25176 if (!WINDOW_RIGHTMOST_P (w)
25177 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
25179 int x0, x1, y0, y1;
25181 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25182 y1 -= 1;
25184 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25185 x1 -= 1;
25187 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
25189 else if (!WINDOW_LEFTMOST_P (w)
25190 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
25192 int x0, x1, y0, y1;
25194 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
25195 y1 -= 1;
25197 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
25198 x0 -= 1;
25200 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
25205 /* Redraw the part of window W intersection rectangle FR. Pixel
25206 coordinates in FR are frame-relative. Call this function with
25207 input blocked. Value is non-zero if the exposure overwrites
25208 mouse-face. */
25210 static int
25211 expose_window (struct window *w, XRectangle *fr)
25213 struct frame *f = XFRAME (w->frame);
25214 XRectangle wr, r;
25215 int mouse_face_overwritten_p = 0;
25217 /* If window is not yet fully initialized, do nothing. This can
25218 happen when toolkit scroll bars are used and a window is split.
25219 Reconfiguring the scroll bar will generate an expose for a newly
25220 created window. */
25221 if (w->current_matrix == NULL)
25222 return 0;
25224 /* When we're currently updating the window, display and current
25225 matrix usually don't agree. Arrange for a thorough display
25226 later. */
25227 if (w == updated_window)
25229 SET_FRAME_GARBAGED (f);
25230 return 0;
25233 /* Frame-relative pixel rectangle of W. */
25234 wr.x = WINDOW_LEFT_EDGE_X (w);
25235 wr.y = WINDOW_TOP_EDGE_Y (w);
25236 wr.width = WINDOW_TOTAL_WIDTH (w);
25237 wr.height = WINDOW_TOTAL_HEIGHT (w);
25239 if (x_intersect_rectangles (fr, &wr, &r))
25241 int yb = window_text_bottom_y (w);
25242 struct glyph_row *row;
25243 int cursor_cleared_p;
25244 struct glyph_row *first_overlapping_row, *last_overlapping_row;
25246 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
25247 r.x, r.y, r.width, r.height));
25249 /* Convert to window coordinates. */
25250 r.x -= WINDOW_LEFT_EDGE_X (w);
25251 r.y -= WINDOW_TOP_EDGE_Y (w);
25253 /* Turn off the cursor. */
25254 if (!w->pseudo_window_p
25255 && phys_cursor_in_rect_p (w, &r))
25257 x_clear_cursor (w);
25258 cursor_cleared_p = 1;
25260 else
25261 cursor_cleared_p = 0;
25263 /* Update lines intersecting rectangle R. */
25264 first_overlapping_row = last_overlapping_row = NULL;
25265 for (row = w->current_matrix->rows;
25266 row->enabled_p;
25267 ++row)
25269 int y0 = row->y;
25270 int y1 = MATRIX_ROW_BOTTOM_Y (row);
25272 if ((y0 >= r.y && y0 < r.y + r.height)
25273 || (y1 > r.y && y1 < r.y + r.height)
25274 || (r.y >= y0 && r.y < y1)
25275 || (r.y + r.height > y0 && r.y + r.height < y1))
25277 /* A header line may be overlapping, but there is no need
25278 to fix overlapping areas for them. KFS 2005-02-12 */
25279 if (row->overlapping_p && !row->mode_line_p)
25281 if (first_overlapping_row == NULL)
25282 first_overlapping_row = row;
25283 last_overlapping_row = row;
25286 row->clip = fr;
25287 if (expose_line (w, row, &r))
25288 mouse_face_overwritten_p = 1;
25289 row->clip = NULL;
25291 else if (row->overlapping_p)
25293 /* We must redraw a row overlapping the exposed area. */
25294 if (y0 < r.y
25295 ? y0 + row->phys_height > r.y
25296 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
25298 if (first_overlapping_row == NULL)
25299 first_overlapping_row = row;
25300 last_overlapping_row = row;
25304 if (y1 >= yb)
25305 break;
25308 /* Display the mode line if there is one. */
25309 if (WINDOW_WANTS_MODELINE_P (w)
25310 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
25311 row->enabled_p)
25312 && row->y < r.y + r.height)
25314 if (expose_line (w, row, &r))
25315 mouse_face_overwritten_p = 1;
25318 if (!w->pseudo_window_p)
25320 /* Fix the display of overlapping rows. */
25321 if (first_overlapping_row)
25322 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
25323 fr);
25325 /* Draw border between windows. */
25326 x_draw_vertical_border (w);
25328 /* Turn the cursor on again. */
25329 if (cursor_cleared_p)
25330 update_window_cursor (w, 1);
25334 return mouse_face_overwritten_p;
25339 /* Redraw (parts) of all windows in the window tree rooted at W that
25340 intersect R. R contains frame pixel coordinates. Value is
25341 non-zero if the exposure overwrites mouse-face. */
25343 static int
25344 expose_window_tree (struct window *w, XRectangle *r)
25346 struct frame *f = XFRAME (w->frame);
25347 int mouse_face_overwritten_p = 0;
25349 while (w && !FRAME_GARBAGED_P (f))
25351 if (!NILP (w->hchild))
25352 mouse_face_overwritten_p
25353 |= expose_window_tree (XWINDOW (w->hchild), r);
25354 else if (!NILP (w->vchild))
25355 mouse_face_overwritten_p
25356 |= expose_window_tree (XWINDOW (w->vchild), r);
25357 else
25358 mouse_face_overwritten_p |= expose_window (w, r);
25360 w = NILP (w->next) ? NULL : XWINDOW (w->next);
25363 return mouse_face_overwritten_p;
25367 /* EXPORT:
25368 Redisplay an exposed area of frame F. X and Y are the upper-left
25369 corner of the exposed rectangle. W and H are width and height of
25370 the exposed area. All are pixel values. W or H zero means redraw
25371 the entire frame. */
25373 void
25374 expose_frame (struct frame *f, int x, int y, int w, int h)
25376 XRectangle r;
25377 int mouse_face_overwritten_p = 0;
25379 TRACE ((stderr, "expose_frame "));
25381 /* No need to redraw if frame will be redrawn soon. */
25382 if (FRAME_GARBAGED_P (f))
25384 TRACE ((stderr, " garbaged\n"));
25385 return;
25388 /* If basic faces haven't been realized yet, there is no point in
25389 trying to redraw anything. This can happen when we get an expose
25390 event while Emacs is starting, e.g. by moving another window. */
25391 if (FRAME_FACE_CACHE (f) == NULL
25392 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
25394 TRACE ((stderr, " no faces\n"));
25395 return;
25398 if (w == 0 || h == 0)
25400 r.x = r.y = 0;
25401 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
25402 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
25404 else
25406 r.x = x;
25407 r.y = y;
25408 r.width = w;
25409 r.height = h;
25412 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
25413 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
25415 if (WINDOWP (f->tool_bar_window))
25416 mouse_face_overwritten_p
25417 |= expose_window (XWINDOW (f->tool_bar_window), &r);
25419 #ifdef HAVE_X_WINDOWS
25420 #ifndef MSDOS
25421 #ifndef USE_X_TOOLKIT
25422 if (WINDOWP (f->menu_bar_window))
25423 mouse_face_overwritten_p
25424 |= expose_window (XWINDOW (f->menu_bar_window), &r);
25425 #endif /* not USE_X_TOOLKIT */
25426 #endif
25427 #endif
25429 /* Some window managers support a focus-follows-mouse style with
25430 delayed raising of frames. Imagine a partially obscured frame,
25431 and moving the mouse into partially obscured mouse-face on that
25432 frame. The visible part of the mouse-face will be highlighted,
25433 then the WM raises the obscured frame. With at least one WM, KDE
25434 2.1, Emacs is not getting any event for the raising of the frame
25435 (even tried with SubstructureRedirectMask), only Expose events.
25436 These expose events will draw text normally, i.e. not
25437 highlighted. Which means we must redo the highlight here.
25438 Subsume it under ``we love X''. --gerd 2001-08-15 */
25439 /* Included in Windows version because Windows most likely does not
25440 do the right thing if any third party tool offers
25441 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
25442 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
25444 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
25445 if (f == dpyinfo->mouse_face_mouse_frame)
25447 int x = dpyinfo->mouse_face_mouse_x;
25448 int y = dpyinfo->mouse_face_mouse_y;
25449 clear_mouse_face (dpyinfo);
25450 note_mouse_highlight (f, x, y);
25456 /* EXPORT:
25457 Determine the intersection of two rectangles R1 and R2. Return
25458 the intersection in *RESULT. Value is non-zero if RESULT is not
25459 empty. */
25462 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
25464 XRectangle *left, *right;
25465 XRectangle *upper, *lower;
25466 int intersection_p = 0;
25468 /* Rearrange so that R1 is the left-most rectangle. */
25469 if (r1->x < r2->x)
25470 left = r1, right = r2;
25471 else
25472 left = r2, right = r1;
25474 /* X0 of the intersection is right.x0, if this is inside R1,
25475 otherwise there is no intersection. */
25476 if (right->x <= left->x + left->width)
25478 result->x = right->x;
25480 /* The right end of the intersection is the minimum of the
25481 the right ends of left and right. */
25482 result->width = (min (left->x + left->width, right->x + right->width)
25483 - result->x);
25485 /* Same game for Y. */
25486 if (r1->y < r2->y)
25487 upper = r1, lower = r2;
25488 else
25489 upper = r2, lower = r1;
25491 /* The upper end of the intersection is lower.y0, if this is inside
25492 of upper. Otherwise, there is no intersection. */
25493 if (lower->y <= upper->y + upper->height)
25495 result->y = lower->y;
25497 /* The lower end of the intersection is the minimum of the lower
25498 ends of upper and lower. */
25499 result->height = (min (lower->y + lower->height,
25500 upper->y + upper->height)
25501 - result->y);
25502 intersection_p = 1;
25506 return intersection_p;
25509 #endif /* HAVE_WINDOW_SYSTEM */
25512 /***********************************************************************
25513 Initialization
25514 ***********************************************************************/
25516 void
25517 syms_of_xdisp (void)
25519 Vwith_echo_area_save_vector = Qnil;
25520 staticpro (&Vwith_echo_area_save_vector);
25522 Vmessage_stack = Qnil;
25523 staticpro (&Vmessage_stack);
25525 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
25526 staticpro (&Qinhibit_redisplay);
25528 message_dolog_marker1 = Fmake_marker ();
25529 staticpro (&message_dolog_marker1);
25530 message_dolog_marker2 = Fmake_marker ();
25531 staticpro (&message_dolog_marker2);
25532 message_dolog_marker3 = Fmake_marker ();
25533 staticpro (&message_dolog_marker3);
25535 #if GLYPH_DEBUG
25536 defsubr (&Sdump_frame_glyph_matrix);
25537 defsubr (&Sdump_glyph_matrix);
25538 defsubr (&Sdump_glyph_row);
25539 defsubr (&Sdump_tool_bar_row);
25540 defsubr (&Strace_redisplay);
25541 defsubr (&Strace_to_stderr);
25542 #endif
25543 #ifdef HAVE_WINDOW_SYSTEM
25544 defsubr (&Stool_bar_lines_needed);
25545 defsubr (&Slookup_image_map);
25546 #endif
25547 defsubr (&Sformat_mode_line);
25548 defsubr (&Sinvisible_p);
25549 defsubr (&Scurrent_bidi_paragraph_direction);
25551 staticpro (&Qmenu_bar_update_hook);
25552 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
25554 staticpro (&Qoverriding_terminal_local_map);
25555 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
25557 staticpro (&Qoverriding_local_map);
25558 Qoverriding_local_map = intern_c_string ("overriding-local-map");
25560 staticpro (&Qwindow_scroll_functions);
25561 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
25563 staticpro (&Qwindow_text_change_functions);
25564 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
25566 staticpro (&Qredisplay_end_trigger_functions);
25567 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
25569 staticpro (&Qinhibit_point_motion_hooks);
25570 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
25572 Qeval = intern_c_string ("eval");
25573 staticpro (&Qeval);
25575 QCdata = intern_c_string (":data");
25576 staticpro (&QCdata);
25577 Qdisplay = intern_c_string ("display");
25578 staticpro (&Qdisplay);
25579 Qspace_width = intern_c_string ("space-width");
25580 staticpro (&Qspace_width);
25581 Qraise = intern_c_string ("raise");
25582 staticpro (&Qraise);
25583 Qslice = intern_c_string ("slice");
25584 staticpro (&Qslice);
25585 Qspace = intern_c_string ("space");
25586 staticpro (&Qspace);
25587 Qmargin = intern_c_string ("margin");
25588 staticpro (&Qmargin);
25589 Qpointer = intern_c_string ("pointer");
25590 staticpro (&Qpointer);
25591 Qleft_margin = intern_c_string ("left-margin");
25592 staticpro (&Qleft_margin);
25593 Qright_margin = intern_c_string ("right-margin");
25594 staticpro (&Qright_margin);
25595 Qcenter = intern_c_string ("center");
25596 staticpro (&Qcenter);
25597 Qline_height = intern_c_string ("line-height");
25598 staticpro (&Qline_height);
25599 QCalign_to = intern_c_string (":align-to");
25600 staticpro (&QCalign_to);
25601 QCrelative_width = intern_c_string (":relative-width");
25602 staticpro (&QCrelative_width);
25603 QCrelative_height = intern_c_string (":relative-height");
25604 staticpro (&QCrelative_height);
25605 QCeval = intern_c_string (":eval");
25606 staticpro (&QCeval);
25607 QCpropertize = intern_c_string (":propertize");
25608 staticpro (&QCpropertize);
25609 QCfile = intern_c_string (":file");
25610 staticpro (&QCfile);
25611 Qfontified = intern_c_string ("fontified");
25612 staticpro (&Qfontified);
25613 Qfontification_functions = intern_c_string ("fontification-functions");
25614 staticpro (&Qfontification_functions);
25615 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
25616 staticpro (&Qtrailing_whitespace);
25617 Qescape_glyph = intern_c_string ("escape-glyph");
25618 staticpro (&Qescape_glyph);
25619 Qnobreak_space = intern_c_string ("nobreak-space");
25620 staticpro (&Qnobreak_space);
25621 Qimage = intern_c_string ("image");
25622 staticpro (&Qimage);
25623 Qtext = intern_c_string ("text");
25624 staticpro (&Qtext);
25625 Qboth = intern_c_string ("both");
25626 staticpro (&Qboth);
25627 Qboth_horiz = intern_c_string ("both-horiz");
25628 staticpro (&Qboth_horiz);
25629 Qtext_image_horiz = intern_c_string ("text-image-horiz");
25630 staticpro (&Qtext_image_horiz);
25631 QCmap = intern_c_string (":map");
25632 staticpro (&QCmap);
25633 QCpointer = intern_c_string (":pointer");
25634 staticpro (&QCpointer);
25635 Qrect = intern_c_string ("rect");
25636 staticpro (&Qrect);
25637 Qcircle = intern_c_string ("circle");
25638 staticpro (&Qcircle);
25639 Qpoly = intern_c_string ("poly");
25640 staticpro (&Qpoly);
25641 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
25642 staticpro (&Qmessage_truncate_lines);
25643 Qgrow_only = intern_c_string ("grow-only");
25644 staticpro (&Qgrow_only);
25645 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
25646 staticpro (&Qinhibit_menubar_update);
25647 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
25648 staticpro (&Qinhibit_eval_during_redisplay);
25649 Qposition = intern_c_string ("position");
25650 staticpro (&Qposition);
25651 Qbuffer_position = intern_c_string ("buffer-position");
25652 staticpro (&Qbuffer_position);
25653 Qobject = intern_c_string ("object");
25654 staticpro (&Qobject);
25655 Qbar = intern_c_string ("bar");
25656 staticpro (&Qbar);
25657 Qhbar = intern_c_string ("hbar");
25658 staticpro (&Qhbar);
25659 Qbox = intern_c_string ("box");
25660 staticpro (&Qbox);
25661 Qhollow = intern_c_string ("hollow");
25662 staticpro (&Qhollow);
25663 Qhand = intern_c_string ("hand");
25664 staticpro (&Qhand);
25665 Qarrow = intern_c_string ("arrow");
25666 staticpro (&Qarrow);
25667 Qtext = intern_c_string ("text");
25668 staticpro (&Qtext);
25669 Qrisky_local_variable = intern_c_string ("risky-local-variable");
25670 staticpro (&Qrisky_local_variable);
25671 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
25672 staticpro (&Qinhibit_free_realized_faces);
25674 list_of_error = Fcons (Fcons (intern_c_string ("error"),
25675 Fcons (intern_c_string ("void-variable"), Qnil)),
25676 Qnil);
25677 staticpro (&list_of_error);
25679 Qlast_arrow_position = intern_c_string ("last-arrow-position");
25680 staticpro (&Qlast_arrow_position);
25681 Qlast_arrow_string = intern_c_string ("last-arrow-string");
25682 staticpro (&Qlast_arrow_string);
25684 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
25685 staticpro (&Qoverlay_arrow_string);
25686 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
25687 staticpro (&Qoverlay_arrow_bitmap);
25689 echo_buffer[0] = echo_buffer[1] = Qnil;
25690 staticpro (&echo_buffer[0]);
25691 staticpro (&echo_buffer[1]);
25693 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
25694 staticpro (&echo_area_buffer[0]);
25695 staticpro (&echo_area_buffer[1]);
25697 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
25698 staticpro (&Vmessages_buffer_name);
25700 mode_line_proptrans_alist = Qnil;
25701 staticpro (&mode_line_proptrans_alist);
25702 mode_line_string_list = Qnil;
25703 staticpro (&mode_line_string_list);
25704 mode_line_string_face = Qnil;
25705 staticpro (&mode_line_string_face);
25706 mode_line_string_face_prop = Qnil;
25707 staticpro (&mode_line_string_face_prop);
25708 Vmode_line_unwind_vector = Qnil;
25709 staticpro (&Vmode_line_unwind_vector);
25711 help_echo_string = Qnil;
25712 staticpro (&help_echo_string);
25713 help_echo_object = Qnil;
25714 staticpro (&help_echo_object);
25715 help_echo_window = Qnil;
25716 staticpro (&help_echo_window);
25717 previous_help_echo_string = Qnil;
25718 staticpro (&previous_help_echo_string);
25719 help_echo_pos = -1;
25721 Qright_to_left = intern_c_string ("right-to-left");
25722 staticpro (&Qright_to_left);
25723 Qleft_to_right = intern_c_string ("left-to-right");
25724 staticpro (&Qleft_to_right);
25726 #ifdef HAVE_WINDOW_SYSTEM
25727 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
25728 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
25729 For example, if a block cursor is over a tab, it will be drawn as
25730 wide as that tab on the display. */);
25731 x_stretch_cursor_p = 0;
25732 #endif
25734 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
25735 doc: /* *Non-nil means highlight trailing whitespace.
25736 The face used for trailing whitespace is `trailing-whitespace'. */);
25737 Vshow_trailing_whitespace = Qnil;
25739 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
25740 doc: /* *Control highlighting of nobreak space and soft hyphen.
25741 A value of t means highlight the character itself (for nobreak space,
25742 use face `nobreak-space').
25743 A value of nil means no highlighting.
25744 Other values mean display the escape glyph followed by an ordinary
25745 space or ordinary hyphen. */);
25746 Vnobreak_char_display = Qt;
25748 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
25749 doc: /* *The pointer shape to show in void text areas.
25750 A value of nil means to show the text pointer. Other options are `arrow',
25751 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
25752 Vvoid_text_area_pointer = Qarrow;
25754 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
25755 doc: /* Non-nil means don't actually do any redisplay.
25756 This is used for internal purposes. */);
25757 Vinhibit_redisplay = Qnil;
25759 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
25760 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
25761 Vglobal_mode_string = Qnil;
25763 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
25764 doc: /* Marker for where to display an arrow on top of the buffer text.
25765 This must be the beginning of a line in order to work.
25766 See also `overlay-arrow-string'. */);
25767 Voverlay_arrow_position = Qnil;
25769 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
25770 doc: /* String to display as an arrow in non-window frames.
25771 See also `overlay-arrow-position'. */);
25772 Voverlay_arrow_string = make_pure_c_string ("=>");
25774 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
25775 doc: /* List of variables (symbols) which hold markers for overlay arrows.
25776 The symbols on this list are examined during redisplay to determine
25777 where to display overlay arrows. */);
25778 Voverlay_arrow_variable_list
25779 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
25781 DEFVAR_INT ("scroll-step", &scroll_step,
25782 doc: /* *The number of lines to try scrolling a window by when point moves out.
25783 If that fails to bring point back on frame, point is centered instead.
25784 If this is zero, point is always centered after it moves off frame.
25785 If you want scrolling to always be a line at a time, you should set
25786 `scroll-conservatively' to a large value rather than set this to 1. */);
25788 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
25789 doc: /* *Scroll up to this many lines, to bring point back on screen.
25790 If point moves off-screen, redisplay will scroll by up to
25791 `scroll-conservatively' lines in order to bring point just barely
25792 onto the screen again. If that cannot be done, then redisplay
25793 recenters point as usual.
25795 A value of zero means always recenter point if it moves off screen. */);
25796 scroll_conservatively = 0;
25798 DEFVAR_INT ("scroll-margin", &scroll_margin,
25799 doc: /* *Number of lines of margin at the top and bottom of a window.
25800 Recenter the window whenever point gets within this many lines
25801 of the top or bottom of the window. */);
25802 scroll_margin = 0;
25804 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
25805 doc: /* Pixels per inch value for non-window system displays.
25806 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
25807 Vdisplay_pixels_per_inch = make_float (72.0);
25809 #if GLYPH_DEBUG
25810 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
25811 #endif
25813 DEFVAR_LISP ("truncate-partial-width-windows",
25814 &Vtruncate_partial_width_windows,
25815 doc: /* Non-nil means truncate lines in windows narrower than the frame.
25816 For an integer value, truncate lines in each window narrower than the
25817 full frame width, provided the window width is less than that integer;
25818 otherwise, respect the value of `truncate-lines'.
25820 For any other non-nil value, truncate lines in all windows that do
25821 not span the full frame width.
25823 A value of nil means to respect the value of `truncate-lines'.
25825 If `word-wrap' is enabled, you might want to reduce this. */);
25826 Vtruncate_partial_width_windows = make_number (50);
25828 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
25829 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
25830 Any other value means to use the appropriate face, `mode-line',
25831 `header-line', or `menu' respectively. */);
25832 mode_line_inverse_video = 1;
25834 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
25835 doc: /* *Maximum buffer size for which line number should be displayed.
25836 If the buffer is bigger than this, the line number does not appear
25837 in the mode line. A value of nil means no limit. */);
25838 Vline_number_display_limit = Qnil;
25840 DEFVAR_INT ("line-number-display-limit-width",
25841 &line_number_display_limit_width,
25842 doc: /* *Maximum line width (in characters) for line number display.
25843 If the average length of the lines near point is bigger than this, then the
25844 line number may be omitted from the mode line. */);
25845 line_number_display_limit_width = 200;
25847 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
25848 doc: /* *Non-nil means highlight region even in nonselected windows. */);
25849 highlight_nonselected_windows = 0;
25851 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
25852 doc: /* Non-nil if more than one frame is visible on this display.
25853 Minibuffer-only frames don't count, but iconified frames do.
25854 This variable is not guaranteed to be accurate except while processing
25855 `frame-title-format' and `icon-title-format'. */);
25857 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
25858 doc: /* Template for displaying the title bar of visible frames.
25859 \(Assuming the window manager supports this feature.)
25861 This variable has the same structure as `mode-line-format', except that
25862 the %c and %l constructs are ignored. It is used only on frames for
25863 which no explicit name has been set \(see `modify-frame-parameters'). */);
25865 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
25866 doc: /* Template for displaying the title bar of an iconified frame.
25867 \(Assuming the window manager supports this feature.)
25868 This variable has the same structure as `mode-line-format' (which see),
25869 and is used only on frames for which no explicit name has been set
25870 \(see `modify-frame-parameters'). */);
25871 Vicon_title_format
25872 = Vframe_title_format
25873 = pure_cons (intern_c_string ("multiple-frames"),
25874 pure_cons (make_pure_c_string ("%b"),
25875 pure_cons (pure_cons (empty_unibyte_string,
25876 pure_cons (intern_c_string ("invocation-name"),
25877 pure_cons (make_pure_c_string ("@"),
25878 pure_cons (intern_c_string ("system-name"),
25879 Qnil)))),
25880 Qnil)));
25882 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
25883 doc: /* Maximum number of lines to keep in the message log buffer.
25884 If nil, disable message logging. If t, log messages but don't truncate
25885 the buffer when it becomes large. */);
25886 Vmessage_log_max = make_number (100);
25888 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
25889 doc: /* Functions called before redisplay, if window sizes have changed.
25890 The value should be a list of functions that take one argument.
25891 Just before redisplay, for each frame, if any of its windows have changed
25892 size since the last redisplay, or have been split or deleted,
25893 all the functions in the list are called, with the frame as argument. */);
25894 Vwindow_size_change_functions = Qnil;
25896 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
25897 doc: /* List of functions to call before redisplaying a window with scrolling.
25898 Each function is called with two arguments, the window and its new
25899 display-start position. Note that these functions are also called by
25900 `set-window-buffer'. Also note that the value of `window-end' is not
25901 valid when these functions are called. */);
25902 Vwindow_scroll_functions = Qnil;
25904 DEFVAR_LISP ("window-text-change-functions",
25905 &Vwindow_text_change_functions,
25906 doc: /* Functions to call in redisplay when text in the window might change. */);
25907 Vwindow_text_change_functions = Qnil;
25909 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
25910 doc: /* Functions called when redisplay of a window reaches the end trigger.
25911 Each function is called with two arguments, the window and the end trigger value.
25912 See `set-window-redisplay-end-trigger'. */);
25913 Vredisplay_end_trigger_functions = Qnil;
25915 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25916 doc: /* *Non-nil means autoselect window with mouse pointer.
25917 If nil, do not autoselect windows.
25918 A positive number means delay autoselection by that many seconds: a
25919 window is autoselected only after the mouse has remained in that
25920 window for the duration of the delay.
25921 A negative number has a similar effect, but causes windows to be
25922 autoselected only after the mouse has stopped moving. \(Because of
25923 the way Emacs compares mouse events, you will occasionally wait twice
25924 that time before the window gets selected.\)
25925 Any other value means to autoselect window instantaneously when the
25926 mouse pointer enters it.
25928 Autoselection selects the minibuffer only if it is active, and never
25929 unselects the minibuffer if it is active.
25931 When customizing this variable make sure that the actual value of
25932 `focus-follows-mouse' matches the behavior of your window manager. */);
25933 Vmouse_autoselect_window = Qnil;
25935 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25936 doc: /* *Non-nil means automatically resize tool-bars.
25937 This dynamically changes the tool-bar's height to the minimum height
25938 that is needed to make all tool-bar items visible.
25939 If value is `grow-only', the tool-bar's height is only increased
25940 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25941 Vauto_resize_tool_bars = Qt;
25943 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25944 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25945 auto_raise_tool_bar_buttons_p = 1;
25947 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25948 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25949 make_cursor_line_fully_visible_p = 1;
25951 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25952 doc: /* *Border below tool-bar in pixels.
25953 If an integer, use it as the height of the border.
25954 If it is one of `internal-border-width' or `border-width', use the
25955 value of the corresponding frame parameter.
25956 Otherwise, no border is added below the tool-bar. */);
25957 Vtool_bar_border = Qinternal_border_width;
25959 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25960 doc: /* *Margin around tool-bar buttons in pixels.
25961 If an integer, use that for both horizontal and vertical margins.
25962 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25963 HORZ specifying the horizontal margin, and VERT specifying the
25964 vertical margin. */);
25965 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25967 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25968 doc: /* *Relief thickness of tool-bar buttons. */);
25969 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25971 DEFVAR_LISP ("tool-bar-style", &Vtool_bar_style,
25972 doc: /* *Tool bar style to use.
25973 It can be one of
25974 image - show images only
25975 text - show text only
25976 both - show both, text below image
25977 both-horiz - show text to the right of the image
25978 text-image-horiz - show text to the left of the image
25979 any other - use system default or image if no system default. */);
25980 Vtool_bar_style = Qnil;
25982 DEFVAR_INT ("tool-bar-max-label-size", &tool_bar_max_label_size,
25983 doc: /* *Maximum number of characters a label can have to be shown.
25984 The tool bar style must also show labels for this to have any effect, see
25985 `tool-bar-style'. */);
25986 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
25988 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25989 doc: /* List of functions to call to fontify regions of text.
25990 Each function is called with one argument POS. Functions must
25991 fontify a region starting at POS in the current buffer, and give
25992 fontified regions the property `fontified'. */);
25993 Vfontification_functions = Qnil;
25994 Fmake_variable_buffer_local (Qfontification_functions);
25996 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25997 &unibyte_display_via_language_environment,
25998 doc: /* *Non-nil means display unibyte text according to language environment.
25999 Specifically, this means that raw bytes in the range 160-255 decimal
26000 are displayed by converting them to the equivalent multibyte characters
26001 according to the current language environment. As a result, they are
26002 displayed according to the current fontset.
26004 Note that this variable affects only how these bytes are displayed,
26005 but does not change the fact they are interpreted as raw bytes. */);
26006 unibyte_display_via_language_environment = 0;
26008 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
26009 doc: /* *Maximum height for resizing mini-windows.
26010 If a float, it specifies a fraction of the mini-window frame's height.
26011 If an integer, it specifies a number of lines. */);
26012 Vmax_mini_window_height = make_float (0.25);
26014 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
26015 doc: /* *How to resize mini-windows.
26016 A value of nil means don't automatically resize mini-windows.
26017 A value of t means resize them to fit the text displayed in them.
26018 A value of `grow-only', the default, means let mini-windows grow
26019 only, until their display becomes empty, at which point the windows
26020 go back to their normal size. */);
26021 Vresize_mini_windows = Qgrow_only;
26023 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
26024 doc: /* Alist specifying how to blink the cursor off.
26025 Each element has the form (ON-STATE . OFF-STATE). Whenever the
26026 `cursor-type' frame-parameter or variable equals ON-STATE,
26027 comparing using `equal', Emacs uses OFF-STATE to specify
26028 how to blink it off. ON-STATE and OFF-STATE are values for
26029 the `cursor-type' frame parameter.
26031 If a frame's ON-STATE has no entry in this list,
26032 the frame's other specifications determine how to blink the cursor off. */);
26033 Vblink_cursor_alist = Qnil;
26035 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
26036 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
26037 automatic_hscrolling_p = 1;
26038 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
26039 staticpro (&Qauto_hscroll_mode);
26041 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
26042 doc: /* *How many columns away from the window edge point is allowed to get
26043 before automatic hscrolling will horizontally scroll the window. */);
26044 hscroll_margin = 5;
26046 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
26047 doc: /* *How many columns to scroll the window when point gets too close to the edge.
26048 When point is less than `hscroll-margin' columns from the window
26049 edge, automatic hscrolling will scroll the window by the amount of columns
26050 determined by this variable. If its value is a positive integer, scroll that
26051 many columns. If it's a positive floating-point number, it specifies the
26052 fraction of the window's width to scroll. If it's nil or zero, point will be
26053 centered horizontally after the scroll. Any other value, including negative
26054 numbers, are treated as if the value were zero.
26056 Automatic hscrolling always moves point outside the scroll margin, so if
26057 point was more than scroll step columns inside the margin, the window will
26058 scroll more than the value given by the scroll step.
26060 Note that the lower bound for automatic hscrolling specified by `scroll-left'
26061 and `scroll-right' overrides this variable's effect. */);
26062 Vhscroll_step = make_number (0);
26064 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
26065 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
26066 Bind this around calls to `message' to let it take effect. */);
26067 message_truncate_lines = 0;
26069 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
26070 doc: /* Normal hook run to update the menu bar definitions.
26071 Redisplay runs this hook before it redisplays the menu bar.
26072 This is used to update submenus such as Buffers,
26073 whose contents depend on various data. */);
26074 Vmenu_bar_update_hook = Qnil;
26076 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
26077 doc: /* Frame for which we are updating a menu.
26078 The enable predicate for a menu binding should check this variable. */);
26079 Vmenu_updating_frame = Qnil;
26081 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
26082 doc: /* Non-nil means don't update menu bars. Internal use only. */);
26083 inhibit_menubar_update = 0;
26085 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
26086 doc: /* Prefix prepended to all continuation lines at display time.
26087 The value may be a string, an image, or a stretch-glyph; it is
26088 interpreted in the same way as the value of a `display' text property.
26090 This variable is overridden by any `wrap-prefix' text or overlay
26091 property.
26093 To add a prefix to non-continuation lines, use `line-prefix'. */);
26094 Vwrap_prefix = Qnil;
26095 staticpro (&Qwrap_prefix);
26096 Qwrap_prefix = intern_c_string ("wrap-prefix");
26097 Fmake_variable_buffer_local (Qwrap_prefix);
26099 DEFVAR_LISP ("line-prefix", &Vline_prefix,
26100 doc: /* Prefix prepended to all non-continuation lines at display time.
26101 The value may be a string, an image, or a stretch-glyph; it is
26102 interpreted in the same way as the value of a `display' text property.
26104 This variable is overridden by any `line-prefix' text or overlay
26105 property.
26107 To add a prefix to continuation lines, use `wrap-prefix'. */);
26108 Vline_prefix = Qnil;
26109 staticpro (&Qline_prefix);
26110 Qline_prefix = intern_c_string ("line-prefix");
26111 Fmake_variable_buffer_local (Qline_prefix);
26113 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
26114 doc: /* Non-nil means don't eval Lisp during redisplay. */);
26115 inhibit_eval_during_redisplay = 0;
26117 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
26118 doc: /* Non-nil means don't free realized faces. Internal use only. */);
26119 inhibit_free_realized_faces = 0;
26121 #if GLYPH_DEBUG
26122 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
26123 doc: /* Inhibit try_window_id display optimization. */);
26124 inhibit_try_window_id = 0;
26126 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
26127 doc: /* Inhibit try_window_reusing display optimization. */);
26128 inhibit_try_window_reusing = 0;
26130 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
26131 doc: /* Inhibit try_cursor_movement display optimization. */);
26132 inhibit_try_cursor_movement = 0;
26133 #endif /* GLYPH_DEBUG */
26135 DEFVAR_INT ("overline-margin", &overline_margin,
26136 doc: /* *Space between overline and text, in pixels.
26137 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
26138 margin to the caracter height. */);
26139 overline_margin = 2;
26141 DEFVAR_INT ("underline-minimum-offset",
26142 &underline_minimum_offset,
26143 doc: /* Minimum distance between baseline and underline.
26144 This can improve legibility of underlined text at small font sizes,
26145 particularly when using variable `x-use-underline-position-properties'
26146 with fonts that specify an UNDERLINE_POSITION relatively close to the
26147 baseline. The default value is 1. */);
26148 underline_minimum_offset = 1;
26150 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
26151 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
26152 display_hourglass_p = 1;
26154 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
26155 doc: /* *Seconds to wait before displaying an hourglass pointer.
26156 Value must be an integer or float. */);
26157 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
26159 hourglass_atimer = NULL;
26160 hourglass_shown_p = 0;
26164 /* Initialize this module when Emacs starts. */
26166 void
26167 init_xdisp (void)
26169 Lisp_Object root_window;
26170 struct window *mini_w;
26172 current_header_line_height = current_mode_line_height = -1;
26174 CHARPOS (this_line_start_pos) = 0;
26176 mini_w = XWINDOW (minibuf_window);
26177 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
26179 if (!noninteractive)
26181 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
26182 int i;
26184 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
26185 set_window_height (root_window,
26186 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
26188 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
26189 set_window_height (minibuf_window, 1, 0);
26191 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
26192 mini_w->total_cols = make_number (FRAME_COLS (f));
26194 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
26195 scratch_glyph_row.glyphs[TEXT_AREA + 1]
26196 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
26198 /* The default ellipsis glyphs `...'. */
26199 for (i = 0; i < 3; ++i)
26200 default_invis_vector[i] = make_number ('.');
26204 /* Allocate the buffer for frame titles.
26205 Also used for `format-mode-line'. */
26206 int size = 100;
26207 mode_line_noprop_buf = (char *) xmalloc (size);
26208 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
26209 mode_line_noprop_ptr = mode_line_noprop_buf;
26210 mode_line_target = MODE_LINE_DISPLAY;
26213 help_echo_showing_p = 0;
26216 /* Since w32 does not support atimers, it defines its own implementation of
26217 the following three functions in w32fns.c. */
26218 #ifndef WINDOWSNT
26220 /* Platform-independent portion of hourglass implementation. */
26222 /* Return non-zero if houglass timer has been started or hourglass is shown. */
26224 hourglass_started (void)
26226 return hourglass_shown_p || hourglass_atimer != NULL;
26229 /* Cancel a currently active hourglass timer, and start a new one. */
26230 void
26231 start_hourglass (void)
26233 #if defined (HAVE_WINDOW_SYSTEM)
26234 EMACS_TIME delay;
26235 int secs, usecs = 0;
26237 cancel_hourglass ();
26239 if (INTEGERP (Vhourglass_delay)
26240 && XINT (Vhourglass_delay) > 0)
26241 secs = XFASTINT (Vhourglass_delay);
26242 else if (FLOATP (Vhourglass_delay)
26243 && XFLOAT_DATA (Vhourglass_delay) > 0)
26245 Lisp_Object tem;
26246 tem = Ftruncate (Vhourglass_delay, Qnil);
26247 secs = XFASTINT (tem);
26248 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
26250 else
26251 secs = DEFAULT_HOURGLASS_DELAY;
26253 EMACS_SET_SECS_USECS (delay, secs, usecs);
26254 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
26255 show_hourglass, NULL);
26256 #endif
26260 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
26261 shown. */
26262 void
26263 cancel_hourglass (void)
26265 #if defined (HAVE_WINDOW_SYSTEM)
26266 if (hourglass_atimer)
26268 cancel_atimer (hourglass_atimer);
26269 hourglass_atimer = NULL;
26272 if (hourglass_shown_p)
26273 hide_hourglass ();
26274 #endif
26276 #endif /* ! WINDOWSNT */
26278 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
26279 (do not change this comment) */