(perl-font-lock-special-syntactic-constructs):
[emacs.git] / src / xdisp.c
blob0618c8d4bca0b51c0523d1295cae928041b1a1f2
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 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
62 expose_window (asynchronous) |
64 X expose events -----+
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
88 Direct operations.
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
109 Desired matrices.
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
147 Frame matrices.
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
168 #include <config.h>
169 #include <stdio.h>
170 #include <limits.h>
171 #include <setjmp.h>
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "character.h"
181 #include "charset.h"
182 #include "indent.h"
183 #include "commands.h"
184 #include "keymap.h"
185 #include "macros.h"
186 #include "disptab.h"
187 #include "termhooks.h"
188 #include "intervals.h"
189 #include "coding.h"
190 #include "process.h"
191 #include "region-cache.h"
192 #include "font.h"
193 #include "fontset.h"
194 #include "blockinput.h"
196 #ifdef HAVE_X_WINDOWS
197 #include "xterm.h"
198 #endif
199 #ifdef WINDOWSNT
200 #include "w32term.h"
201 #endif
202 #ifdef HAVE_NS
203 #include "nsterm.h"
204 #endif
205 #ifdef USE_GTK
206 #include "gtkutil.h"
207 #endif
209 #include "font.h"
211 #ifndef FRAME_X_OUTPUT
212 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
213 #endif
215 #define INFINITY 10000000
217 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
218 || defined(HAVE_NS) || defined (USE_GTK)
219 extern void set_frame_menubar P_ ((struct frame *f, int, int));
220 extern int pending_menu_activation;
221 #endif
223 extern int interrupt_input;
224 extern int command_loop_level;
226 extern Lisp_Object do_mouse_tracking;
228 extern int minibuffer_auto_raise;
229 extern Lisp_Object Vminibuffer_list;
231 extern Lisp_Object Qface;
232 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234 extern Lisp_Object Voverriding_local_map;
235 extern Lisp_Object Voverriding_local_map_menu_flag;
236 extern Lisp_Object Qmenu_item;
237 extern Lisp_Object Qwhen;
238 extern Lisp_Object Qhelp_echo;
239 extern Lisp_Object Qbefore_string, Qafter_string;
241 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
242 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
243 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
244 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
245 Lisp_Object Qinhibit_point_motion_hooks;
246 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
247 Lisp_Object Qfontified;
248 Lisp_Object Qgrow_only;
249 Lisp_Object Qinhibit_eval_during_redisplay;
250 Lisp_Object Qbuffer_position, Qposition, Qobject;
252 /* Cursor shapes */
253 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
255 /* Pointer shapes */
256 Lisp_Object Qarrow, Qhand, Qtext;
258 Lisp_Object Qrisky_local_variable;
260 /* Holds the list (error). */
261 Lisp_Object list_of_error;
263 /* Functions called to fontify regions of text. */
265 Lisp_Object Vfontification_functions;
266 Lisp_Object Qfontification_functions;
268 /* Non-nil means automatically select any window when the mouse
269 cursor moves into it. */
270 Lisp_Object Vmouse_autoselect_window;
272 Lisp_Object Vwrap_prefix, Qwrap_prefix;
273 Lisp_Object Vline_prefix, Qline_prefix;
275 /* Non-zero means draw tool bar buttons raised when the mouse moves
276 over them. */
278 int auto_raise_tool_bar_buttons_p;
280 /* Non-zero means to reposition window if cursor line is only partially visible. */
282 int make_cursor_line_fully_visible_p;
284 /* Margin below tool bar in pixels. 0 or nil means no margin.
285 If value is `internal-border-width' or `border-width',
286 the corresponding frame parameter is used. */
288 Lisp_Object Vtool_bar_border;
290 /* Margin around tool bar buttons in pixels. */
292 Lisp_Object Vtool_bar_button_margin;
294 /* Thickness of shadow to draw around tool bar buttons. */
296 EMACS_INT tool_bar_button_relief;
298 /* Non-nil means automatically resize tool-bars so that all tool-bar
299 items are visible, and no blank lines remain.
301 If value is `grow-only', only make tool-bar bigger. */
303 Lisp_Object Vauto_resize_tool_bars;
305 /* Non-zero means draw block and hollow cursor as wide as the glyph
306 under it. For example, if a block cursor is over a tab, it will be
307 drawn as wide as that tab on the display. */
309 int x_stretch_cursor_p;
311 /* Non-nil means don't actually do any redisplay. */
313 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
315 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
317 int inhibit_eval_during_redisplay;
319 /* Names of text properties relevant for redisplay. */
321 Lisp_Object Qdisplay;
322 extern Lisp_Object Qface, Qinvisible, Qwidth;
324 /* Symbols used in text property values. */
326 Lisp_Object Vdisplay_pixels_per_inch;
327 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
328 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
329 Lisp_Object Qslice;
330 Lisp_Object Qcenter;
331 Lisp_Object Qmargin, Qpointer;
332 Lisp_Object Qline_height;
333 extern Lisp_Object Qheight;
334 extern Lisp_Object QCwidth, QCheight, QCascent;
335 extern Lisp_Object Qscroll_bar;
336 extern Lisp_Object Qcursor;
338 /* Non-nil means highlight trailing whitespace. */
340 Lisp_Object Vshow_trailing_whitespace;
342 /* Non-nil means escape non-break space and hyphens. */
344 Lisp_Object Vnobreak_char_display;
346 #ifdef HAVE_WINDOW_SYSTEM
347 extern Lisp_Object Voverflow_newline_into_fringe;
349 /* Test if overflow newline into fringe. Called with iterator IT
350 at or past right window margin, and with IT->current_x set. */
352 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
353 (!NILP (Voverflow_newline_into_fringe) \
354 && FRAME_WINDOW_P (it->f) \
355 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
356 && it->current_x == it->last_visible_x \
357 && it->line_wrap != WORD_WRAP)
359 #else /* !HAVE_WINDOW_SYSTEM */
360 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
361 #endif /* HAVE_WINDOW_SYSTEM */
363 /* Test if the display element loaded in IT is a space or tab
364 character. This is used to determine word wrapping. */
366 #define IT_DISPLAYING_WHITESPACE(it) \
367 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
369 /* Non-nil means show the text cursor in void text areas
370 i.e. in blank areas after eol and eob. This used to be
371 the default in 21.3. */
373 Lisp_Object Vvoid_text_area_pointer;
375 /* Name of the face used to highlight trailing whitespace. */
377 Lisp_Object Qtrailing_whitespace;
379 /* Name and number of the face used to highlight escape glyphs. */
381 Lisp_Object Qescape_glyph;
383 /* Name and number of the face used to highlight non-breaking spaces. */
385 Lisp_Object Qnobreak_space;
387 /* The symbol `image' which is the car of the lists used to represent
388 images in Lisp. */
390 Lisp_Object Qimage;
392 /* The image map types. */
393 Lisp_Object QCmap, QCpointer;
394 Lisp_Object Qrect, Qcircle, Qpoly;
396 /* Non-zero means print newline to stdout before next mini-buffer
397 message. */
399 int noninteractive_need_newline;
401 /* Non-zero means print newline to message log before next message. */
403 static int message_log_need_newline;
405 /* Three markers that message_dolog uses.
406 It could allocate them itself, but that causes trouble
407 in handling memory-full errors. */
408 static Lisp_Object message_dolog_marker1;
409 static Lisp_Object message_dolog_marker2;
410 static Lisp_Object message_dolog_marker3;
412 /* The buffer position of the first character appearing entirely or
413 partially on the line of the selected window which contains the
414 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
415 redisplay optimization in redisplay_internal. */
417 static struct text_pos this_line_start_pos;
419 /* Number of characters past the end of the line above, including the
420 terminating newline. */
422 static struct text_pos this_line_end_pos;
424 /* The vertical positions and the height of this line. */
426 static int this_line_vpos;
427 static int this_line_y;
428 static int this_line_pixel_height;
430 /* X position at which this display line starts. Usually zero;
431 negative if first character is partially visible. */
433 static int this_line_start_x;
435 /* Buffer that this_line_.* variables are referring to. */
437 static struct buffer *this_line_buffer;
439 /* Nonzero means truncate lines in all windows less wide than the
440 frame. */
442 Lisp_Object Vtruncate_partial_width_windows;
444 /* A flag to control how to display unibyte 8-bit character. */
446 int unibyte_display_via_language_environment;
448 /* Nonzero means we have more than one non-mini-buffer-only frame.
449 Not guaranteed to be accurate except while parsing
450 frame-title-format. */
452 int multiple_frames;
454 Lisp_Object Vglobal_mode_string;
457 /* List of variables (symbols) which hold markers for overlay arrows.
458 The symbols on this list are examined during redisplay to determine
459 where to display overlay arrows. */
461 Lisp_Object Voverlay_arrow_variable_list;
463 /* Marker for where to display an arrow on top of the buffer text. */
465 Lisp_Object Voverlay_arrow_position;
467 /* String to display for the arrow. Only used on terminal frames. */
469 Lisp_Object Voverlay_arrow_string;
471 /* Values of those variables at last redisplay are stored as
472 properties on `overlay-arrow-position' symbol. However, if
473 Voverlay_arrow_position is a marker, last-arrow-position is its
474 numerical position. */
476 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
478 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
479 properties on a symbol in overlay-arrow-variable-list. */
481 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
483 /* Like mode-line-format, but for the title bar on a visible frame. */
485 Lisp_Object Vframe_title_format;
487 /* Like mode-line-format, but for the title bar on an iconified frame. */
489 Lisp_Object Vicon_title_format;
491 /* List of functions to call when a window's size changes. These
492 functions get one arg, a frame on which one or more windows' sizes
493 have changed. */
495 static Lisp_Object Vwindow_size_change_functions;
497 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
499 /* Nonzero if an overlay arrow has been displayed in this window. */
501 static int overlay_arrow_seen;
503 /* Nonzero means highlight the region even in nonselected windows. */
505 int highlight_nonselected_windows;
507 /* If cursor motion alone moves point off frame, try scrolling this
508 many lines up or down if that will bring it back. */
510 static EMACS_INT scroll_step;
512 /* Nonzero means scroll just far enough to bring point back on the
513 screen, when appropriate. */
515 static EMACS_INT scroll_conservatively;
517 /* Recenter the window whenever point gets within this many lines of
518 the top or bottom of the window. This value is translated into a
519 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
520 that there is really a fixed pixel height scroll margin. */
522 EMACS_INT scroll_margin;
524 /* Number of windows showing the buffer of the selected window (or
525 another buffer with the same base buffer). keyboard.c refers to
526 this. */
528 int buffer_shared;
530 /* Vector containing glyphs for an ellipsis `...'. */
532 static Lisp_Object default_invis_vector[3];
534 /* Zero means display the mode-line/header-line/menu-bar in the default face
535 (this slightly odd definition is for compatibility with previous versions
536 of emacs), non-zero means display them using their respective faces.
538 This variable is deprecated. */
540 int mode_line_inverse_video;
542 /* Prompt to display in front of the mini-buffer contents. */
544 Lisp_Object minibuf_prompt;
546 /* Width of current mini-buffer prompt. Only set after display_line
547 of the line that contains the prompt. */
549 int minibuf_prompt_width;
551 /* This is the window where the echo area message was displayed. It
552 is always a mini-buffer window, but it may not be the same window
553 currently active as a mini-buffer. */
555 Lisp_Object echo_area_window;
557 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
558 pushes the current message and the value of
559 message_enable_multibyte on the stack, the function restore_message
560 pops the stack and displays MESSAGE again. */
562 Lisp_Object Vmessage_stack;
564 /* Nonzero means multibyte characters were enabled when the echo area
565 message was specified. */
567 int message_enable_multibyte;
569 /* Nonzero if we should redraw the mode lines on the next redisplay. */
571 int update_mode_lines;
573 /* Nonzero if window sizes or contents have changed since last
574 redisplay that finished. */
576 int windows_or_buffers_changed;
578 /* Nonzero means a frame's cursor type has been changed. */
580 int cursor_type_changed;
582 /* Nonzero after display_mode_line if %l was used and it displayed a
583 line number. */
585 int line_number_displayed;
587 /* Maximum buffer size for which to display line numbers. */
589 Lisp_Object Vline_number_display_limit;
591 /* Line width to consider when repositioning for line number display. */
593 static EMACS_INT line_number_display_limit_width;
595 /* Number of lines to keep in the message log buffer. t means
596 infinite. nil means don't log at all. */
598 Lisp_Object Vmessage_log_max;
600 /* The name of the *Messages* buffer, a string. */
602 static Lisp_Object Vmessages_buffer_name;
604 /* Current, index 0, and last displayed echo area message. Either
605 buffers from echo_buffers, or nil to indicate no message. */
607 Lisp_Object echo_area_buffer[2];
609 /* The buffers referenced from echo_area_buffer. */
611 static Lisp_Object echo_buffer[2];
613 /* A vector saved used in with_area_buffer to reduce consing. */
615 static Lisp_Object Vwith_echo_area_save_vector;
617 /* Non-zero means display_echo_area should display the last echo area
618 message again. Set by redisplay_preserve_echo_area. */
620 static int display_last_displayed_message_p;
622 /* Nonzero if echo area is being used by print; zero if being used by
623 message. */
625 int message_buf_print;
627 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
629 Lisp_Object Qinhibit_menubar_update;
630 int inhibit_menubar_update;
632 /* When evaluating expressions from menu bar items (enable conditions,
633 for instance), this is the frame they are being processed for. */
635 Lisp_Object Vmenu_updating_frame;
637 /* Maximum height for resizing mini-windows. Either a float
638 specifying a fraction of the available height, or an integer
639 specifying a number of lines. */
641 Lisp_Object Vmax_mini_window_height;
643 /* Non-zero means messages should be displayed with truncated
644 lines instead of being continued. */
646 int message_truncate_lines;
647 Lisp_Object Qmessage_truncate_lines;
649 /* Set to 1 in clear_message to make redisplay_internal aware
650 of an emptied echo area. */
652 static int message_cleared_p;
654 /* How to blink the default frame cursor off. */
655 Lisp_Object Vblink_cursor_alist;
657 /* A scratch glyph row with contents used for generating truncation
658 glyphs. Also used in direct_output_for_insert. */
660 #define MAX_SCRATCH_GLYPHS 100
661 struct glyph_row scratch_glyph_row;
662 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
664 /* Ascent and height of the last line processed by move_it_to. */
666 static int last_max_ascent, last_height;
668 /* Non-zero if there's a help-echo in the echo area. */
670 int help_echo_showing_p;
672 /* If >= 0, computed, exact values of mode-line and header-line height
673 to use in the macros CURRENT_MODE_LINE_HEIGHT and
674 CURRENT_HEADER_LINE_HEIGHT. */
676 int current_mode_line_height, current_header_line_height;
678 /* The maximum distance to look ahead for text properties. Values
679 that are too small let us call compute_char_face and similar
680 functions too often which is expensive. Values that are too large
681 let us call compute_char_face and alike too often because we
682 might not be interested in text properties that far away. */
684 #define TEXT_PROP_DISTANCE_LIMIT 100
686 #if GLYPH_DEBUG
688 /* Variables to turn off display optimizations from Lisp. */
690 int inhibit_try_window_id, inhibit_try_window_reusing;
691 int inhibit_try_cursor_movement;
693 /* Non-zero means print traces of redisplay if compiled with
694 GLYPH_DEBUG != 0. */
696 int trace_redisplay_p;
698 #endif /* GLYPH_DEBUG */
700 #ifdef DEBUG_TRACE_MOVE
701 /* Non-zero means trace with TRACE_MOVE to stderr. */
702 int trace_move;
704 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
705 #else
706 #define TRACE_MOVE(x) (void) 0
707 #endif
709 /* Non-zero means automatically scroll windows horizontally to make
710 point visible. */
712 int automatic_hscrolling_p;
713 Lisp_Object Qauto_hscroll_mode;
715 /* How close to the margin can point get before the window is scrolled
716 horizontally. */
717 EMACS_INT hscroll_margin;
719 /* How much to scroll horizontally when point is inside the above margin. */
720 Lisp_Object Vhscroll_step;
722 /* The variable `resize-mini-windows'. If nil, don't resize
723 mini-windows. If t, always resize them to fit the text they
724 display. If `grow-only', let mini-windows grow only until they
725 become empty. */
727 Lisp_Object Vresize_mini_windows;
729 /* Buffer being redisplayed -- for redisplay_window_error. */
731 struct buffer *displayed_buffer;
733 /* Space between overline and text. */
735 EMACS_INT overline_margin;
737 /* Require underline to be at least this many screen pixels below baseline
738 This to avoid underline "merging" with the base of letters at small
739 font sizes, particularly when x_use_underline_position_properties is on. */
741 EMACS_INT underline_minimum_offset;
743 /* Value returned from text property handlers (see below). */
745 enum prop_handled
747 HANDLED_NORMALLY,
748 HANDLED_RECOMPUTE_PROPS,
749 HANDLED_OVERLAY_STRING_CONSUMED,
750 HANDLED_RETURN
753 /* A description of text properties that redisplay is interested
754 in. */
756 struct props
758 /* The name of the property. */
759 Lisp_Object *name;
761 /* A unique index for the property. */
762 enum prop_idx idx;
764 /* A handler function called to set up iterator IT from the property
765 at IT's current position. Value is used to steer handle_stop. */
766 enum prop_handled (*handler) P_ ((struct it *it));
769 static enum prop_handled handle_face_prop P_ ((struct it *));
770 static enum prop_handled handle_invisible_prop P_ ((struct it *));
771 static enum prop_handled handle_display_prop P_ ((struct it *));
772 static enum prop_handled handle_composition_prop P_ ((struct it *));
773 static enum prop_handled handle_overlay_change P_ ((struct it *));
774 static enum prop_handled handle_fontified_prop P_ ((struct it *));
776 /* Properties handled by iterators. */
778 static struct props it_props[] =
780 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
781 /* Handle `face' before `display' because some sub-properties of
782 `display' need to know the face. */
783 {&Qface, FACE_PROP_IDX, handle_face_prop},
784 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
785 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
786 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
787 {NULL, 0, NULL}
790 /* Value is the position described by X. If X is a marker, value is
791 the marker_position of X. Otherwise, value is X. */
793 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
795 /* Enumeration returned by some move_it_.* functions internally. */
797 enum move_it_result
799 /* Not used. Undefined value. */
800 MOVE_UNDEFINED,
802 /* Move ended at the requested buffer position or ZV. */
803 MOVE_POS_MATCH_OR_ZV,
805 /* Move ended at the requested X pixel position. */
806 MOVE_X_REACHED,
808 /* Move within a line ended at the end of a line that must be
809 continued. */
810 MOVE_LINE_CONTINUED,
812 /* Move within a line ended at the end of a line that would
813 be displayed truncated. */
814 MOVE_LINE_TRUNCATED,
816 /* Move within a line ended at a line end. */
817 MOVE_NEWLINE_OR_CR
820 /* This counter is used to clear the face cache every once in a while
821 in redisplay_internal. It is incremented for each redisplay.
822 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
823 cleared. */
825 #define CLEAR_FACE_CACHE_COUNT 500
826 static int clear_face_cache_count;
828 /* Similarly for the image cache. */
830 #ifdef HAVE_WINDOW_SYSTEM
831 #define CLEAR_IMAGE_CACHE_COUNT 101
832 static int clear_image_cache_count;
833 #endif
835 /* Non-zero while redisplay_internal is in progress. */
837 int redisplaying_p;
839 /* Non-zero means don't free realized faces. Bound while freeing
840 realized faces is dangerous because glyph matrices might still
841 reference them. */
843 int inhibit_free_realized_faces;
844 Lisp_Object Qinhibit_free_realized_faces;
846 /* If a string, XTread_socket generates an event to display that string.
847 (The display is done in read_char.) */
849 Lisp_Object help_echo_string;
850 Lisp_Object help_echo_window;
851 Lisp_Object help_echo_object;
852 int help_echo_pos;
854 /* Temporary variable for XTread_socket. */
856 Lisp_Object previous_help_echo_string;
858 /* Null glyph slice */
860 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
862 /* Platform-independent portion of hourglass implementation. */
864 /* Non-zero means we're allowed to display a hourglass pointer. */
865 int display_hourglass_p;
867 /* Non-zero means an hourglass cursor is currently shown. */
868 int hourglass_shown_p;
870 /* If non-null, an asynchronous timer that, when it expires, displays
871 an hourglass cursor on all frames. */
872 struct atimer *hourglass_atimer;
874 /* Number of seconds to wait before displaying an hourglass cursor. */
875 Lisp_Object Vhourglass_delay;
877 /* Default number of seconds to wait before displaying an hourglass
878 cursor. */
879 #define DEFAULT_HOURGLASS_DELAY 1
882 /* Function prototypes. */
884 static void setup_for_ellipsis P_ ((struct it *, int));
885 static void mark_window_display_accurate_1 P_ ((struct window *, int));
886 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
887 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
888 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
889 static int redisplay_mode_lines P_ ((Lisp_Object, int));
890 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
892 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
894 static void handle_line_prefix P_ ((struct it *));
896 static void pint2str P_ ((char *, int, int));
897 static void pint2hrstr P_ ((char *, int, int));
898 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
899 struct text_pos));
900 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
901 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
902 static void store_mode_line_noprop_char P_ ((char));
903 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
904 static void x_consider_frame_title P_ ((Lisp_Object));
905 static void handle_stop P_ ((struct it *));
906 static int tool_bar_lines_needed P_ ((struct frame *, int *));
907 static int single_display_spec_intangible_p P_ ((Lisp_Object));
908 static void ensure_echo_area_buffers P_ ((void));
909 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
910 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
911 static int with_echo_area_buffer P_ ((struct window *, int,
912 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
913 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
914 static void clear_garbaged_frames P_ ((void));
915 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
916 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static int display_echo_area P_ ((struct window *));
919 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
920 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
922 static int string_char_and_length P_ ((const unsigned char *, int *));
923 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
924 struct text_pos));
925 static int compute_window_start_on_continuation_line P_ ((struct window *));
926 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
927 static void insert_left_trunc_glyphs P_ ((struct it *));
928 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
929 Lisp_Object));
930 static void extend_face_to_end_of_line P_ ((struct it *));
931 static int append_space_for_newline P_ ((struct it *, int));
932 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
933 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
934 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
935 static int trailing_whitespace_p P_ ((int));
936 static int message_log_check_duplicate P_ ((int, int, int, int));
937 static void push_it P_ ((struct it *));
938 static void pop_it P_ ((struct it *));
939 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
940 static void select_frame_for_redisplay P_ ((Lisp_Object));
941 static void redisplay_internal P_ ((int));
942 static int echo_area_display P_ ((int));
943 static void redisplay_windows P_ ((Lisp_Object));
944 static void redisplay_window P_ ((Lisp_Object, int));
945 static Lisp_Object redisplay_window_error ();
946 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
947 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
948 static int update_menu_bar P_ ((struct frame *, int, int));
949 static int try_window_reusing_current_matrix P_ ((struct window *));
950 static int try_window_id P_ ((struct window *));
951 static int display_line P_ ((struct it *));
952 static int display_mode_lines P_ ((struct window *));
953 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
954 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
955 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
956 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
957 static void display_menu_bar P_ ((struct window *));
958 static int display_count_lines P_ ((int, int, int, int, int *));
959 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
960 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
961 static void compute_line_metrics P_ ((struct it *));
962 static void run_redisplay_end_trigger_hook P_ ((struct it *));
963 static int get_overlay_strings P_ ((struct it *, int));
964 static int get_overlay_strings_1 P_ ((struct it *, int, int));
965 static void next_overlay_string P_ ((struct it *));
966 static void reseat P_ ((struct it *, struct text_pos, int));
967 static void reseat_1 P_ ((struct it *, struct text_pos, int));
968 static void back_to_previous_visible_line_start P_ ((struct it *));
969 void reseat_at_previous_visible_line_start P_ ((struct it *));
970 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
971 static int next_element_from_ellipsis P_ ((struct it *));
972 static int next_element_from_display_vector P_ ((struct it *));
973 static int next_element_from_string P_ ((struct it *));
974 static int next_element_from_c_string P_ ((struct it *));
975 static int next_element_from_buffer P_ ((struct it *));
976 static int next_element_from_composition P_ ((struct it *));
977 static int next_element_from_image P_ ((struct it *));
978 static int next_element_from_stretch P_ ((struct it *));
979 static void load_overlay_strings P_ ((struct it *, int));
980 static int init_from_display_pos P_ ((struct it *, struct window *,
981 struct display_pos *));
982 static void reseat_to_string P_ ((struct it *, unsigned char *,
983 Lisp_Object, int, int, int, int));
984 static enum move_it_result
985 move_it_in_display_line_to (struct it *, EMACS_INT, int,
986 enum move_operation_enum);
987 void move_it_vertically_backward P_ ((struct it *, int));
988 static void init_to_row_start P_ ((struct it *, struct window *,
989 struct glyph_row *));
990 static int init_to_row_end P_ ((struct it *, struct window *,
991 struct glyph_row *));
992 static void back_to_previous_line_start P_ ((struct it *));
993 static int forward_to_next_line_start P_ ((struct it *, int *));
994 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
995 Lisp_Object, int));
996 static struct text_pos string_pos P_ ((int, Lisp_Object));
997 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
998 static int number_of_chars P_ ((unsigned char *, int));
999 static void compute_stop_pos P_ ((struct it *));
1000 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1001 Lisp_Object));
1002 static int face_before_or_after_it_pos P_ ((struct it *, int));
1003 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1004 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1005 Lisp_Object, Lisp_Object,
1006 struct text_pos *, int));
1007 static int underlying_face_id P_ ((struct it *));
1008 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1009 struct window *));
1011 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1012 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1014 #ifdef HAVE_WINDOW_SYSTEM
1016 static void update_tool_bar P_ ((struct frame *, int));
1017 static void build_desired_tool_bar_string P_ ((struct frame *f));
1018 static int redisplay_tool_bar P_ ((struct frame *));
1019 static void display_tool_bar_line P_ ((struct it *, int));
1020 static void notice_overwritten_cursor P_ ((struct window *,
1021 enum glyph_row_area,
1022 int, int, int, int));
1026 #endif /* HAVE_WINDOW_SYSTEM */
1029 /***********************************************************************
1030 Window display dimensions
1031 ***********************************************************************/
1033 /* Return the bottom boundary y-position for text lines in window W.
1034 This is the first y position at which a line cannot start.
1035 It is relative to the top of the window.
1037 This is the height of W minus the height of a mode line, if any. */
1039 INLINE int
1040 window_text_bottom_y (w)
1041 struct window *w;
1043 int height = WINDOW_TOTAL_HEIGHT (w);
1045 if (WINDOW_WANTS_MODELINE_P (w))
1046 height -= CURRENT_MODE_LINE_HEIGHT (w);
1047 return height;
1050 /* Return the pixel width of display area AREA of window W. AREA < 0
1051 means return the total width of W, not including fringes to
1052 the left and right of the window. */
1054 INLINE int
1055 window_box_width (w, area)
1056 struct window *w;
1057 int area;
1059 int cols = XFASTINT (w->total_cols);
1060 int pixels = 0;
1062 if (!w->pseudo_window_p)
1064 cols -= WINDOW_SCROLL_BAR_COLS (w);
1066 if (area == TEXT_AREA)
1068 if (INTEGERP (w->left_margin_cols))
1069 cols -= XFASTINT (w->left_margin_cols);
1070 if (INTEGERP (w->right_margin_cols))
1071 cols -= XFASTINT (w->right_margin_cols);
1072 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1074 else if (area == LEFT_MARGIN_AREA)
1076 cols = (INTEGERP (w->left_margin_cols)
1077 ? XFASTINT (w->left_margin_cols) : 0);
1078 pixels = 0;
1080 else if (area == RIGHT_MARGIN_AREA)
1082 cols = (INTEGERP (w->right_margin_cols)
1083 ? XFASTINT (w->right_margin_cols) : 0);
1084 pixels = 0;
1088 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1092 /* Return the pixel height of the display area of window W, not
1093 including mode lines of W, if any. */
1095 INLINE int
1096 window_box_height (w)
1097 struct window *w;
1099 struct frame *f = XFRAME (w->frame);
1100 int height = WINDOW_TOTAL_HEIGHT (w);
1102 xassert (height >= 0);
1104 /* Note: the code below that determines the mode-line/header-line
1105 height is essentially the same as that contained in the macro
1106 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1107 the appropriate glyph row has its `mode_line_p' flag set,
1108 and if it doesn't, uses estimate_mode_line_height instead. */
1110 if (WINDOW_WANTS_MODELINE_P (w))
1112 struct glyph_row *ml_row
1113 = (w->current_matrix && w->current_matrix->rows
1114 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1115 : 0);
1116 if (ml_row && ml_row->mode_line_p)
1117 height -= ml_row->height;
1118 else
1119 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1122 if (WINDOW_WANTS_HEADER_LINE_P (w))
1124 struct glyph_row *hl_row
1125 = (w->current_matrix && w->current_matrix->rows
1126 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1127 : 0);
1128 if (hl_row && hl_row->mode_line_p)
1129 height -= hl_row->height;
1130 else
1131 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1134 /* With a very small font and a mode-line that's taller than
1135 default, we might end up with a negative height. */
1136 return max (0, height);
1139 /* Return the window-relative coordinate of the left edge of display
1140 area AREA of window W. AREA < 0 means return the left edge of the
1141 whole window, to the right of the left fringe of W. */
1143 INLINE int
1144 window_box_left_offset (w, area)
1145 struct window *w;
1146 int area;
1148 int x;
1150 if (w->pseudo_window_p)
1151 return 0;
1153 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1155 if (area == TEXT_AREA)
1156 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1157 + window_box_width (w, LEFT_MARGIN_AREA));
1158 else if (area == RIGHT_MARGIN_AREA)
1159 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1160 + window_box_width (w, LEFT_MARGIN_AREA)
1161 + window_box_width (w, TEXT_AREA)
1162 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1164 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1165 else if (area == LEFT_MARGIN_AREA
1166 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1167 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1169 return x;
1173 /* Return the window-relative coordinate of the right edge of display
1174 area AREA of window W. AREA < 0 means return the left edge of the
1175 whole window, to the left of the right fringe of W. */
1177 INLINE int
1178 window_box_right_offset (w, area)
1179 struct window *w;
1180 int area;
1182 return window_box_left_offset (w, area) + window_box_width (w, area);
1185 /* Return the frame-relative coordinate of the left edge of display
1186 area AREA of window W. AREA < 0 means return the left edge of the
1187 whole window, to the right of the left fringe of W. */
1189 INLINE int
1190 window_box_left (w, area)
1191 struct window *w;
1192 int area;
1194 struct frame *f = XFRAME (w->frame);
1195 int x;
1197 if (w->pseudo_window_p)
1198 return FRAME_INTERNAL_BORDER_WIDTH (f);
1200 x = (WINDOW_LEFT_EDGE_X (w)
1201 + window_box_left_offset (w, area));
1203 return x;
1207 /* Return the frame-relative coordinate of the right edge of display
1208 area AREA of window W. AREA < 0 means return the left edge of the
1209 whole window, to the left of the right fringe of W. */
1211 INLINE int
1212 window_box_right (w, area)
1213 struct window *w;
1214 int area;
1216 return window_box_left (w, area) + window_box_width (w, area);
1219 /* Get the bounding box of the display area AREA of window W, without
1220 mode lines, in frame-relative coordinates. AREA < 0 means the
1221 whole window, not including the left and right fringes of
1222 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1223 coordinates of the upper-left corner of the box. Return in
1224 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1226 INLINE void
1227 window_box (w, area, box_x, box_y, box_width, box_height)
1228 struct window *w;
1229 int area;
1230 int *box_x, *box_y, *box_width, *box_height;
1232 if (box_width)
1233 *box_width = window_box_width (w, area);
1234 if (box_height)
1235 *box_height = window_box_height (w);
1236 if (box_x)
1237 *box_x = window_box_left (w, area);
1238 if (box_y)
1240 *box_y = WINDOW_TOP_EDGE_Y (w);
1241 if (WINDOW_WANTS_HEADER_LINE_P (w))
1242 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1247 /* Get the bounding box of the display area AREA of window W, without
1248 mode lines. AREA < 0 means the whole window, not including the
1249 left and right fringe of the window. Return in *TOP_LEFT_X
1250 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1251 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1252 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1253 box. */
1255 INLINE void
1256 window_box_edges (w, area, top_left_x, top_left_y,
1257 bottom_right_x, bottom_right_y)
1258 struct window *w;
1259 int area;
1260 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1262 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1263 bottom_right_y);
1264 *bottom_right_x += *top_left_x;
1265 *bottom_right_y += *top_left_y;
1270 /***********************************************************************
1271 Utilities
1272 ***********************************************************************/
1274 /* Return the bottom y-position of the line the iterator IT is in.
1275 This can modify IT's settings. */
1278 line_bottom_y (it)
1279 struct it *it;
1281 int line_height = it->max_ascent + it->max_descent;
1282 int line_top_y = it->current_y;
1284 if (line_height == 0)
1286 if (last_height)
1287 line_height = last_height;
1288 else if (IT_CHARPOS (*it) < ZV)
1290 move_it_by_lines (it, 1, 1);
1291 line_height = (it->max_ascent || it->max_descent
1292 ? it->max_ascent + it->max_descent
1293 : last_height);
1295 else
1297 struct glyph_row *row = it->glyph_row;
1299 /* Use the default character height. */
1300 it->glyph_row = NULL;
1301 it->what = IT_CHARACTER;
1302 it->c = ' ';
1303 it->len = 1;
1304 PRODUCE_GLYPHS (it);
1305 line_height = it->ascent + it->descent;
1306 it->glyph_row = row;
1310 return line_top_y + line_height;
1314 /* Return 1 if position CHARPOS is visible in window W.
1315 CHARPOS < 0 means return info about WINDOW_END position.
1316 If visible, set *X and *Y to pixel coordinates of top left corner.
1317 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1318 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1321 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1322 struct window *w;
1323 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1325 struct it it;
1326 struct text_pos top;
1327 int visible_p = 0;
1328 struct buffer *old_buffer = NULL;
1330 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1331 return visible_p;
1333 if (XBUFFER (w->buffer) != current_buffer)
1335 old_buffer = current_buffer;
1336 set_buffer_internal_1 (XBUFFER (w->buffer));
1339 SET_TEXT_POS_FROM_MARKER (top, w->start);
1341 /* Compute exact mode line heights. */
1342 if (WINDOW_WANTS_MODELINE_P (w))
1343 current_mode_line_height
1344 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1345 current_buffer->mode_line_format);
1347 if (WINDOW_WANTS_HEADER_LINE_P (w))
1348 current_header_line_height
1349 = display_mode_line (w, HEADER_LINE_FACE_ID,
1350 current_buffer->header_line_format);
1352 start_display (&it, w, top);
1353 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1354 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1356 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1358 /* We have reached CHARPOS, or passed it. How the call to
1359 move_it_to can overshoot: (i) If CHARPOS is on invisible
1360 text, move_it_to stops at the end of the invisible text,
1361 after CHARPOS. (ii) If CHARPOS is in a display vector,
1362 move_it_to stops on its last glyph. */
1363 int top_x = it.current_x;
1364 int top_y = it.current_y;
1365 enum it_method it_method = it.method;
1366 /* Calling line_bottom_y may change it.method. */
1367 int bottom_y = (last_height = 0, line_bottom_y (&it));
1368 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1370 if (top_y < window_top_y)
1371 visible_p = bottom_y > window_top_y;
1372 else if (top_y < it.last_visible_y)
1373 visible_p = 1;
1374 if (visible_p)
1376 if (it_method == GET_FROM_BUFFER)
1378 Lisp_Object window, prop;
1380 XSETWINDOW (window, w);
1381 prop = Fget_char_property (make_number (it.position.charpos),
1382 Qinvisible, window);
1384 /* If charpos coincides with invisible text covered with an
1385 ellipsis, use the first glyph of the ellipsis to compute
1386 the pixel positions. */
1387 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1389 struct glyph_row *row = it.glyph_row;
1390 struct glyph *glyph = row->glyphs[TEXT_AREA];
1391 struct glyph *end = glyph + row->used[TEXT_AREA];
1392 int x = row->x;
1394 for (; glyph < end
1395 && (!BUFFERP (glyph->object)
1396 || glyph->charpos < charpos);
1397 glyph++)
1398 x += glyph->pixel_width;
1399 top_x = x;
1402 else if (it_method == GET_FROM_DISPLAY_VECTOR)
1404 /* We stopped on the last glyph of a display vector.
1405 Try and recompute. Hack alert! */
1406 if (charpos < 2 || top.charpos >= charpos)
1407 top_x = it.glyph_row->x;
1408 else
1410 struct it it2;
1411 start_display (&it2, w, top);
1412 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1413 get_next_display_element (&it2);
1414 PRODUCE_GLYPHS (&it2);
1415 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1416 || it2.current_x > it2.last_visible_x)
1417 top_x = it.glyph_row->x;
1418 else
1420 top_x = it2.current_x;
1421 top_y = it2.current_y;
1426 *x = top_x;
1427 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1428 *rtop = max (0, window_top_y - top_y);
1429 *rbot = max (0, bottom_y - it.last_visible_y);
1430 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1431 - max (top_y, window_top_y)));
1432 *vpos = it.vpos;
1435 else
1437 struct it it2;
1439 it2 = it;
1440 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1441 move_it_by_lines (&it, 1, 0);
1442 if (charpos < IT_CHARPOS (it)
1443 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1445 visible_p = 1;
1446 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1447 *x = it2.current_x;
1448 *y = it2.current_y + it2.max_ascent - it2.ascent;
1449 *rtop = max (0, -it2.current_y);
1450 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1451 - it.last_visible_y));
1452 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1453 it.last_visible_y)
1454 - max (it2.current_y,
1455 WINDOW_HEADER_LINE_HEIGHT (w))));
1456 *vpos = it2.vpos;
1460 if (old_buffer)
1461 set_buffer_internal_1 (old_buffer);
1463 current_header_line_height = current_mode_line_height = -1;
1465 if (visible_p && XFASTINT (w->hscroll) > 0)
1466 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1468 #if 0
1469 /* Debugging code. */
1470 if (visible_p)
1471 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1472 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1473 else
1474 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1475 #endif
1477 return visible_p;
1481 /* Return the next character from STR which is MAXLEN bytes long.
1482 Return in *LEN the length of the character. This is like
1483 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1484 we find one, we return a `?', but with the length of the invalid
1485 character. */
1487 static INLINE int
1488 string_char_and_length (str, len)
1489 const unsigned char *str;
1490 int *len;
1492 int c;
1494 c = STRING_CHAR_AND_LENGTH (str, *len);
1495 if (!CHAR_VALID_P (c, 1))
1496 /* We may not change the length here because other places in Emacs
1497 don't use this function, i.e. they silently accept invalid
1498 characters. */
1499 c = '?';
1501 return c;
1506 /* Given a position POS containing a valid character and byte position
1507 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1509 static struct text_pos
1510 string_pos_nchars_ahead (pos, string, nchars)
1511 struct text_pos pos;
1512 Lisp_Object string;
1513 int nchars;
1515 xassert (STRINGP (string) && nchars >= 0);
1517 if (STRING_MULTIBYTE (string))
1519 int rest = SBYTES (string) - BYTEPOS (pos);
1520 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1521 int len;
1523 while (nchars--)
1525 string_char_and_length (p, &len);
1526 p += len, rest -= len;
1527 xassert (rest >= 0);
1528 CHARPOS (pos) += 1;
1529 BYTEPOS (pos) += len;
1532 else
1533 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1535 return pos;
1539 /* Value is the text position, i.e. character and byte position,
1540 for character position CHARPOS in STRING. */
1542 static INLINE struct text_pos
1543 string_pos (charpos, string)
1544 int charpos;
1545 Lisp_Object string;
1547 struct text_pos pos;
1548 xassert (STRINGP (string));
1549 xassert (charpos >= 0);
1550 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1551 return pos;
1555 /* Value is a text position, i.e. character and byte position, for
1556 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1557 means recognize multibyte characters. */
1559 static struct text_pos
1560 c_string_pos (charpos, s, multibyte_p)
1561 int charpos;
1562 unsigned char *s;
1563 int multibyte_p;
1565 struct text_pos pos;
1567 xassert (s != NULL);
1568 xassert (charpos >= 0);
1570 if (multibyte_p)
1572 int rest = strlen (s), len;
1574 SET_TEXT_POS (pos, 0, 0);
1575 while (charpos--)
1577 string_char_and_length (s, &len);
1578 s += len, rest -= len;
1579 xassert (rest >= 0);
1580 CHARPOS (pos) += 1;
1581 BYTEPOS (pos) += len;
1584 else
1585 SET_TEXT_POS (pos, charpos, charpos);
1587 return pos;
1591 /* Value is the number of characters in C string S. MULTIBYTE_P
1592 non-zero means recognize multibyte characters. */
1594 static int
1595 number_of_chars (s, multibyte_p)
1596 unsigned char *s;
1597 int multibyte_p;
1599 int nchars;
1601 if (multibyte_p)
1603 int rest = strlen (s), len;
1604 unsigned char *p = (unsigned char *) s;
1606 for (nchars = 0; rest > 0; ++nchars)
1608 string_char_and_length (p, &len);
1609 rest -= len, p += len;
1612 else
1613 nchars = strlen (s);
1615 return nchars;
1619 /* Compute byte position NEWPOS->bytepos corresponding to
1620 NEWPOS->charpos. POS is a known position in string STRING.
1621 NEWPOS->charpos must be >= POS.charpos. */
1623 static void
1624 compute_string_pos (newpos, pos, string)
1625 struct text_pos *newpos, pos;
1626 Lisp_Object string;
1628 xassert (STRINGP (string));
1629 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1631 if (STRING_MULTIBYTE (string))
1632 *newpos = string_pos_nchars_ahead (pos, string,
1633 CHARPOS (*newpos) - CHARPOS (pos));
1634 else
1635 BYTEPOS (*newpos) = CHARPOS (*newpos);
1638 /* EXPORT:
1639 Return an estimation of the pixel height of mode or header lines on
1640 frame F. FACE_ID specifies what line's height to estimate. */
1643 estimate_mode_line_height (f, face_id)
1644 struct frame *f;
1645 enum face_id face_id;
1647 #ifdef HAVE_WINDOW_SYSTEM
1648 if (FRAME_WINDOW_P (f))
1650 int height = FONT_HEIGHT (FRAME_FONT (f));
1652 /* This function is called so early when Emacs starts that the face
1653 cache and mode line face are not yet initialized. */
1654 if (FRAME_FACE_CACHE (f))
1656 struct face *face = FACE_FROM_ID (f, face_id);
1657 if (face)
1659 if (face->font)
1660 height = FONT_HEIGHT (face->font);
1661 if (face->box_line_width > 0)
1662 height += 2 * face->box_line_width;
1666 return height;
1668 #endif
1670 return 1;
1673 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1674 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1675 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1676 not force the value into range. */
1678 void
1679 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1680 FRAME_PTR f;
1681 register int pix_x, pix_y;
1682 int *x, *y;
1683 NativeRectangle *bounds;
1684 int noclip;
1687 #ifdef HAVE_WINDOW_SYSTEM
1688 if (FRAME_WINDOW_P (f))
1690 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1691 even for negative values. */
1692 if (pix_x < 0)
1693 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1694 if (pix_y < 0)
1695 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1697 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1698 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1700 if (bounds)
1701 STORE_NATIVE_RECT (*bounds,
1702 FRAME_COL_TO_PIXEL_X (f, pix_x),
1703 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1704 FRAME_COLUMN_WIDTH (f) - 1,
1705 FRAME_LINE_HEIGHT (f) - 1);
1707 if (!noclip)
1709 if (pix_x < 0)
1710 pix_x = 0;
1711 else if (pix_x > FRAME_TOTAL_COLS (f))
1712 pix_x = FRAME_TOTAL_COLS (f);
1714 if (pix_y < 0)
1715 pix_y = 0;
1716 else if (pix_y > FRAME_LINES (f))
1717 pix_y = FRAME_LINES (f);
1720 #endif
1722 *x = pix_x;
1723 *y = pix_y;
1727 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1728 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1729 can't tell the positions because W's display is not up to date,
1730 return 0. */
1733 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1734 struct window *w;
1735 int hpos, vpos;
1736 int *frame_x, *frame_y;
1738 #ifdef HAVE_WINDOW_SYSTEM
1739 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1741 int success_p;
1743 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1744 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1746 if (display_completed)
1748 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1749 struct glyph *glyph = row->glyphs[TEXT_AREA];
1750 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1752 hpos = row->x;
1753 vpos = row->y;
1754 while (glyph < end)
1756 hpos += glyph->pixel_width;
1757 ++glyph;
1760 /* If first glyph is partially visible, its first visible position is still 0. */
1761 if (hpos < 0)
1762 hpos = 0;
1764 success_p = 1;
1766 else
1768 hpos = vpos = 0;
1769 success_p = 0;
1772 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1773 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1774 return success_p;
1776 #endif
1778 *frame_x = hpos;
1779 *frame_y = vpos;
1780 return 1;
1784 #ifdef HAVE_WINDOW_SYSTEM
1786 /* Find the glyph under window-relative coordinates X/Y in window W.
1787 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1788 strings. Return in *HPOS and *VPOS the row and column number of
1789 the glyph found. Return in *AREA the glyph area containing X.
1790 Value is a pointer to the glyph found or null if X/Y is not on
1791 text, or we can't tell because W's current matrix is not up to
1792 date. */
1794 static
1795 struct glyph *
1796 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1797 struct window *w;
1798 int x, y;
1799 int *hpos, *vpos, *dx, *dy, *area;
1801 struct glyph *glyph, *end;
1802 struct glyph_row *row = NULL;
1803 int x0, i;
1805 /* Find row containing Y. Give up if some row is not enabled. */
1806 for (i = 0; i < w->current_matrix->nrows; ++i)
1808 row = MATRIX_ROW (w->current_matrix, i);
1809 if (!row->enabled_p)
1810 return NULL;
1811 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1812 break;
1815 *vpos = i;
1816 *hpos = 0;
1818 /* Give up if Y is not in the window. */
1819 if (i == w->current_matrix->nrows)
1820 return NULL;
1822 /* Get the glyph area containing X. */
1823 if (w->pseudo_window_p)
1825 *area = TEXT_AREA;
1826 x0 = 0;
1828 else
1830 if (x < window_box_left_offset (w, TEXT_AREA))
1832 *area = LEFT_MARGIN_AREA;
1833 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1835 else if (x < window_box_right_offset (w, TEXT_AREA))
1837 *area = TEXT_AREA;
1838 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1840 else
1842 *area = RIGHT_MARGIN_AREA;
1843 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1847 /* Find glyph containing X. */
1848 glyph = row->glyphs[*area];
1849 end = glyph + row->used[*area];
1850 x -= x0;
1851 while (glyph < end && x >= glyph->pixel_width)
1853 x -= glyph->pixel_width;
1854 ++glyph;
1857 if (glyph == end)
1858 return NULL;
1860 if (dx)
1862 *dx = x;
1863 *dy = y - (row->y + row->ascent - glyph->ascent);
1866 *hpos = glyph - row->glyphs[*area];
1867 return glyph;
1871 /* EXPORT:
1872 Convert frame-relative x/y to coordinates relative to window W.
1873 Takes pseudo-windows into account. */
1875 void
1876 frame_to_window_pixel_xy (w, x, y)
1877 struct window *w;
1878 int *x, *y;
1880 if (w->pseudo_window_p)
1882 /* A pseudo-window is always full-width, and starts at the
1883 left edge of the frame, plus a frame border. */
1884 struct frame *f = XFRAME (w->frame);
1885 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1886 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1888 else
1890 *x -= WINDOW_LEFT_EDGE_X (w);
1891 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1895 /* EXPORT:
1896 Return in RECTS[] at most N clipping rectangles for glyph string S.
1897 Return the number of stored rectangles. */
1900 get_glyph_string_clip_rects (s, rects, n)
1901 struct glyph_string *s;
1902 NativeRectangle *rects;
1903 int n;
1905 XRectangle r;
1907 if (n <= 0)
1908 return 0;
1910 if (s->row->full_width_p)
1912 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1913 r.x = WINDOW_LEFT_EDGE_X (s->w);
1914 r.width = WINDOW_TOTAL_WIDTH (s->w);
1916 /* Unless displaying a mode or menu bar line, which are always
1917 fully visible, clip to the visible part of the row. */
1918 if (s->w->pseudo_window_p)
1919 r.height = s->row->visible_height;
1920 else
1921 r.height = s->height;
1923 else
1925 /* This is a text line that may be partially visible. */
1926 r.x = window_box_left (s->w, s->area);
1927 r.width = window_box_width (s->w, s->area);
1928 r.height = s->row->visible_height;
1931 if (s->clip_head)
1932 if (r.x < s->clip_head->x)
1934 if (r.width >= s->clip_head->x - r.x)
1935 r.width -= s->clip_head->x - r.x;
1936 else
1937 r.width = 0;
1938 r.x = s->clip_head->x;
1940 if (s->clip_tail)
1941 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1943 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1944 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1945 else
1946 r.width = 0;
1949 /* If S draws overlapping rows, it's sufficient to use the top and
1950 bottom of the window for clipping because this glyph string
1951 intentionally draws over other lines. */
1952 if (s->for_overlaps)
1954 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1955 r.height = window_text_bottom_y (s->w) - r.y;
1957 /* Alas, the above simple strategy does not work for the
1958 environments with anti-aliased text: if the same text is
1959 drawn onto the same place multiple times, it gets thicker.
1960 If the overlap we are processing is for the erased cursor, we
1961 take the intersection with the rectagle of the cursor. */
1962 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1964 XRectangle rc, r_save = r;
1966 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1967 rc.y = s->w->phys_cursor.y;
1968 rc.width = s->w->phys_cursor_width;
1969 rc.height = s->w->phys_cursor_height;
1971 x_intersect_rectangles (&r_save, &rc, &r);
1974 else
1976 /* Don't use S->y for clipping because it doesn't take partially
1977 visible lines into account. For example, it can be negative for
1978 partially visible lines at the top of a window. */
1979 if (!s->row->full_width_p
1980 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1981 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1982 else
1983 r.y = max (0, s->row->y);
1985 /* If drawing a tool-bar window, draw it over the internal border
1986 at the top of the window. */
1987 if (WINDOWP (s->f->tool_bar_window)
1988 && s->w == XWINDOW (s->f->tool_bar_window))
1989 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1992 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1994 /* If drawing the cursor, don't let glyph draw outside its
1995 advertised boundaries. Cleartype does this under some circumstances. */
1996 if (s->hl == DRAW_CURSOR)
1998 struct glyph *glyph = s->first_glyph;
1999 int height, max_y;
2001 if (s->x > r.x)
2003 r.width -= s->x - r.x;
2004 r.x = s->x;
2006 r.width = min (r.width, glyph->pixel_width);
2008 /* If r.y is below window bottom, ensure that we still see a cursor. */
2009 height = min (glyph->ascent + glyph->descent,
2010 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2011 max_y = window_text_bottom_y (s->w) - height;
2012 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2013 if (s->ybase - glyph->ascent > max_y)
2015 r.y = max_y;
2016 r.height = height;
2018 else
2020 /* Don't draw cursor glyph taller than our actual glyph. */
2021 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2022 if (height < r.height)
2024 max_y = r.y + r.height;
2025 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2026 r.height = min (max_y - r.y, height);
2031 if (s->row->clip)
2033 XRectangle r_save = r;
2035 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2036 r.width = 0;
2039 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2040 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2042 #ifdef CONVERT_FROM_XRECT
2043 CONVERT_FROM_XRECT (r, *rects);
2044 #else
2045 *rects = r;
2046 #endif
2047 return 1;
2049 else
2051 /* If we are processing overlapping and allowed to return
2052 multiple clipping rectangles, we exclude the row of the glyph
2053 string from the clipping rectangle. This is to avoid drawing
2054 the same text on the environment with anti-aliasing. */
2055 #ifdef CONVERT_FROM_XRECT
2056 XRectangle rs[2];
2057 #else
2058 XRectangle *rs = rects;
2059 #endif
2060 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2062 if (s->for_overlaps & OVERLAPS_PRED)
2064 rs[i] = r;
2065 if (r.y + r.height > row_y)
2067 if (r.y < row_y)
2068 rs[i].height = row_y - r.y;
2069 else
2070 rs[i].height = 0;
2072 i++;
2074 if (s->for_overlaps & OVERLAPS_SUCC)
2076 rs[i] = r;
2077 if (r.y < row_y + s->row->visible_height)
2079 if (r.y + r.height > row_y + s->row->visible_height)
2081 rs[i].y = row_y + s->row->visible_height;
2082 rs[i].height = r.y + r.height - rs[i].y;
2084 else
2085 rs[i].height = 0;
2087 i++;
2090 n = i;
2091 #ifdef CONVERT_FROM_XRECT
2092 for (i = 0; i < n; i++)
2093 CONVERT_FROM_XRECT (rs[i], rects[i]);
2094 #endif
2095 return n;
2099 /* EXPORT:
2100 Return in *NR the clipping rectangle for glyph string S. */
2102 void
2103 get_glyph_string_clip_rect (s, nr)
2104 struct glyph_string *s;
2105 NativeRectangle *nr;
2107 get_glyph_string_clip_rects (s, nr, 1);
2111 /* EXPORT:
2112 Return the position and height of the phys cursor in window W.
2113 Set w->phys_cursor_width to width of phys cursor.
2116 void
2117 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2118 struct window *w;
2119 struct glyph_row *row;
2120 struct glyph *glyph;
2121 int *xp, *yp, *heightp;
2123 struct frame *f = XFRAME (WINDOW_FRAME (w));
2124 int x, y, wd, h, h0, y0;
2126 /* Compute the width of the rectangle to draw. If on a stretch
2127 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2128 rectangle as wide as the glyph, but use a canonical character
2129 width instead. */
2130 wd = glyph->pixel_width - 1;
2131 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2132 wd++; /* Why? */
2133 #endif
2135 x = w->phys_cursor.x;
2136 if (x < 0)
2138 wd += x;
2139 x = 0;
2142 if (glyph->type == STRETCH_GLYPH
2143 && !x_stretch_cursor_p)
2144 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2145 w->phys_cursor_width = wd;
2147 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2149 /* If y is below window bottom, ensure that we still see a cursor. */
2150 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2152 h = max (h0, glyph->ascent + glyph->descent);
2153 h0 = min (h0, glyph->ascent + glyph->descent);
2155 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2156 if (y < y0)
2158 h = max (h - (y0 - y) + 1, h0);
2159 y = y0 - 1;
2161 else
2163 y0 = window_text_bottom_y (w) - h0;
2164 if (y > y0)
2166 h += y - y0;
2167 y = y0;
2171 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2172 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2173 *heightp = h;
2177 * Remember which glyph the mouse is over.
2180 void
2181 remember_mouse_glyph (f, gx, gy, rect)
2182 struct frame *f;
2183 int gx, gy;
2184 NativeRectangle *rect;
2186 Lisp_Object window;
2187 struct window *w;
2188 struct glyph_row *r, *gr, *end_row;
2189 enum window_part part;
2190 enum glyph_row_area area;
2191 int x, y, width, height;
2193 /* Try to determine frame pixel position and size of the glyph under
2194 frame pixel coordinates X/Y on frame F. */
2196 if (!f->glyphs_initialized_p
2197 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2198 NILP (window)))
2200 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2201 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2202 goto virtual_glyph;
2205 w = XWINDOW (window);
2206 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2207 height = WINDOW_FRAME_LINE_HEIGHT (w);
2209 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2210 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2212 if (w->pseudo_window_p)
2214 area = TEXT_AREA;
2215 part = ON_MODE_LINE; /* Don't adjust margin. */
2216 goto text_glyph;
2219 switch (part)
2221 case ON_LEFT_MARGIN:
2222 area = LEFT_MARGIN_AREA;
2223 goto text_glyph;
2225 case ON_RIGHT_MARGIN:
2226 area = RIGHT_MARGIN_AREA;
2227 goto text_glyph;
2229 case ON_HEADER_LINE:
2230 case ON_MODE_LINE:
2231 gr = (part == ON_HEADER_LINE
2232 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2233 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2234 gy = gr->y;
2235 area = TEXT_AREA;
2236 goto text_glyph_row_found;
2238 case ON_TEXT:
2239 area = TEXT_AREA;
2241 text_glyph:
2242 gr = 0; gy = 0;
2243 for (; r <= end_row && r->enabled_p; ++r)
2244 if (r->y + r->height > y)
2246 gr = r; gy = r->y;
2247 break;
2250 text_glyph_row_found:
2251 if (gr && gy <= y)
2253 struct glyph *g = gr->glyphs[area];
2254 struct glyph *end = g + gr->used[area];
2256 height = gr->height;
2257 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2258 if (gx + g->pixel_width > x)
2259 break;
2261 if (g < end)
2263 if (g->type == IMAGE_GLYPH)
2265 /* Don't remember when mouse is over image, as
2266 image may have hot-spots. */
2267 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2268 return;
2270 width = g->pixel_width;
2272 else
2274 /* Use nominal char spacing at end of line. */
2275 x -= gx;
2276 gx += (x / width) * width;
2279 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2280 gx += window_box_left_offset (w, area);
2282 else
2284 /* Use nominal line height at end of window. */
2285 gx = (x / width) * width;
2286 y -= gy;
2287 gy += (y / height) * height;
2289 break;
2291 case ON_LEFT_FRINGE:
2292 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2293 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2294 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2295 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2296 goto row_glyph;
2298 case ON_RIGHT_FRINGE:
2299 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2300 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2301 : window_box_right_offset (w, TEXT_AREA));
2302 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2303 goto row_glyph;
2305 case ON_SCROLL_BAR:
2306 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2308 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2309 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2310 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2311 : 0)));
2312 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2314 row_glyph:
2315 gr = 0, gy = 0;
2316 for (; r <= end_row && r->enabled_p; ++r)
2317 if (r->y + r->height > y)
2319 gr = r; gy = r->y;
2320 break;
2323 if (gr && gy <= y)
2324 height = gr->height;
2325 else
2327 /* Use nominal line height at end of window. */
2328 y -= gy;
2329 gy += (y / height) * height;
2331 break;
2333 default:
2335 virtual_glyph:
2336 /* If there is no glyph under the mouse, then we divide the screen
2337 into a grid of the smallest glyph in the frame, and use that
2338 as our "glyph". */
2340 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2341 round down even for negative values. */
2342 if (gx < 0)
2343 gx -= width - 1;
2344 if (gy < 0)
2345 gy -= height - 1;
2347 gx = (gx / width) * width;
2348 gy = (gy / height) * height;
2350 goto store_rect;
2353 gx += WINDOW_LEFT_EDGE_X (w);
2354 gy += WINDOW_TOP_EDGE_Y (w);
2356 store_rect:
2357 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2359 /* Visible feedback for debugging. */
2360 #if 0
2361 #if HAVE_X_WINDOWS
2362 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2363 f->output_data.x->normal_gc,
2364 gx, gy, width, height);
2365 #endif
2366 #endif
2370 #endif /* HAVE_WINDOW_SYSTEM */
2373 /***********************************************************************
2374 Lisp form evaluation
2375 ***********************************************************************/
2377 /* Error handler for safe_eval and safe_call. */
2379 static Lisp_Object
2380 safe_eval_handler (arg)
2381 Lisp_Object arg;
2383 add_to_log ("Error during redisplay: %s", arg, Qnil);
2384 return Qnil;
2388 /* Evaluate SEXPR and return the result, or nil if something went
2389 wrong. Prevent redisplay during the evaluation. */
2391 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2392 Return the result, or nil if something went wrong. Prevent
2393 redisplay during the evaluation. */
2395 Lisp_Object
2396 safe_call (nargs, args)
2397 int nargs;
2398 Lisp_Object *args;
2400 Lisp_Object val;
2402 if (inhibit_eval_during_redisplay)
2403 val = Qnil;
2404 else
2406 int count = SPECPDL_INDEX ();
2407 struct gcpro gcpro1;
2409 GCPRO1 (args[0]);
2410 gcpro1.nvars = nargs;
2411 specbind (Qinhibit_redisplay, Qt);
2412 /* Use Qt to ensure debugger does not run,
2413 so there is no possibility of wanting to redisplay. */
2414 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2415 safe_eval_handler);
2416 UNGCPRO;
2417 val = unbind_to (count, val);
2420 return val;
2424 /* Call function FN with one argument ARG.
2425 Return the result, or nil if something went wrong. */
2427 Lisp_Object
2428 safe_call1 (fn, arg)
2429 Lisp_Object fn, arg;
2431 Lisp_Object args[2];
2432 args[0] = fn;
2433 args[1] = arg;
2434 return safe_call (2, args);
2437 static Lisp_Object Qeval;
2439 Lisp_Object
2440 safe_eval (Lisp_Object sexpr)
2442 return safe_call1 (Qeval, sexpr);
2445 /* Call function FN with one argument ARG.
2446 Return the result, or nil if something went wrong. */
2448 Lisp_Object
2449 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2451 Lisp_Object args[3];
2452 args[0] = fn;
2453 args[1] = arg1;
2454 args[2] = arg2;
2455 return safe_call (3, args);
2460 /***********************************************************************
2461 Debugging
2462 ***********************************************************************/
2464 #if 0
2466 /* Define CHECK_IT to perform sanity checks on iterators.
2467 This is for debugging. It is too slow to do unconditionally. */
2469 static void
2470 check_it (it)
2471 struct it *it;
2473 if (it->method == GET_FROM_STRING)
2475 xassert (STRINGP (it->string));
2476 xassert (IT_STRING_CHARPOS (*it) >= 0);
2478 else
2480 xassert (IT_STRING_CHARPOS (*it) < 0);
2481 if (it->method == GET_FROM_BUFFER)
2483 /* Check that character and byte positions agree. */
2484 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2488 if (it->dpvec)
2489 xassert (it->current.dpvec_index >= 0);
2490 else
2491 xassert (it->current.dpvec_index < 0);
2494 #define CHECK_IT(IT) check_it ((IT))
2496 #else /* not 0 */
2498 #define CHECK_IT(IT) (void) 0
2500 #endif /* not 0 */
2503 #if GLYPH_DEBUG
2505 /* Check that the window end of window W is what we expect it
2506 to be---the last row in the current matrix displaying text. */
2508 static void
2509 check_window_end (w)
2510 struct window *w;
2512 if (!MINI_WINDOW_P (w)
2513 && !NILP (w->window_end_valid))
2515 struct glyph_row *row;
2516 xassert ((row = MATRIX_ROW (w->current_matrix,
2517 XFASTINT (w->window_end_vpos)),
2518 !row->enabled_p
2519 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2520 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2524 #define CHECK_WINDOW_END(W) check_window_end ((W))
2526 #else /* not GLYPH_DEBUG */
2528 #define CHECK_WINDOW_END(W) (void) 0
2530 #endif /* not GLYPH_DEBUG */
2534 /***********************************************************************
2535 Iterator initialization
2536 ***********************************************************************/
2538 /* Initialize IT for displaying current_buffer in window W, starting
2539 at character position CHARPOS. CHARPOS < 0 means that no buffer
2540 position is specified which is useful when the iterator is assigned
2541 a position later. BYTEPOS is the byte position corresponding to
2542 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2544 If ROW is not null, calls to produce_glyphs with IT as parameter
2545 will produce glyphs in that row.
2547 BASE_FACE_ID is the id of a base face to use. It must be one of
2548 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2549 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2550 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2552 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2553 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2554 will be initialized to use the corresponding mode line glyph row of
2555 the desired matrix of W. */
2557 void
2558 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2559 struct it *it;
2560 struct window *w;
2561 int charpos, bytepos;
2562 struct glyph_row *row;
2563 enum face_id base_face_id;
2565 int highlight_region_p;
2566 enum face_id remapped_base_face_id = base_face_id;
2568 /* Some precondition checks. */
2569 xassert (w != NULL && it != NULL);
2570 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2571 && charpos <= ZV));
2573 /* If face attributes have been changed since the last redisplay,
2574 free realized faces now because they depend on face definitions
2575 that might have changed. Don't free faces while there might be
2576 desired matrices pending which reference these faces. */
2577 if (face_change_count && !inhibit_free_realized_faces)
2579 face_change_count = 0;
2580 free_all_realized_faces (Qnil);
2583 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2584 if (! NILP (Vface_remapping_alist))
2585 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2587 /* Use one of the mode line rows of W's desired matrix if
2588 appropriate. */
2589 if (row == NULL)
2591 if (base_face_id == MODE_LINE_FACE_ID
2592 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2593 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2594 else if (base_face_id == HEADER_LINE_FACE_ID)
2595 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2598 /* Clear IT. */
2599 bzero (it, sizeof *it);
2600 it->current.overlay_string_index = -1;
2601 it->current.dpvec_index = -1;
2602 it->base_face_id = remapped_base_face_id;
2603 it->string = Qnil;
2604 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2606 /* The window in which we iterate over current_buffer: */
2607 XSETWINDOW (it->window, w);
2608 it->w = w;
2609 it->f = XFRAME (w->frame);
2611 it->cmp_it.id = -1;
2613 /* Extra space between lines (on window systems only). */
2614 if (base_face_id == DEFAULT_FACE_ID
2615 && FRAME_WINDOW_P (it->f))
2617 if (NATNUMP (current_buffer->extra_line_spacing))
2618 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2619 else if (FLOATP (current_buffer->extra_line_spacing))
2620 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2621 * FRAME_LINE_HEIGHT (it->f));
2622 else if (it->f->extra_line_spacing > 0)
2623 it->extra_line_spacing = it->f->extra_line_spacing;
2624 it->max_extra_line_spacing = 0;
2627 /* If realized faces have been removed, e.g. because of face
2628 attribute changes of named faces, recompute them. When running
2629 in batch mode, the face cache of the initial frame is null. If
2630 we happen to get called, make a dummy face cache. */
2631 if (FRAME_FACE_CACHE (it->f) == NULL)
2632 init_frame_faces (it->f);
2633 if (FRAME_FACE_CACHE (it->f)->used == 0)
2634 recompute_basic_faces (it->f);
2636 /* Current value of the `slice', `space-width', and 'height' properties. */
2637 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2638 it->space_width = Qnil;
2639 it->font_height = Qnil;
2640 it->override_ascent = -1;
2642 /* Are control characters displayed as `^C'? */
2643 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2645 /* -1 means everything between a CR and the following line end
2646 is invisible. >0 means lines indented more than this value are
2647 invisible. */
2648 it->selective = (INTEGERP (current_buffer->selective_display)
2649 ? XFASTINT (current_buffer->selective_display)
2650 : (!NILP (current_buffer->selective_display)
2651 ? -1 : 0));
2652 it->selective_display_ellipsis_p
2653 = !NILP (current_buffer->selective_display_ellipses);
2655 /* Display table to use. */
2656 it->dp = window_display_table (w);
2658 /* Are multibyte characters enabled in current_buffer? */
2659 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2661 /* Non-zero if we should highlight the region. */
2662 highlight_region_p
2663 = (!NILP (Vtransient_mark_mode)
2664 && !NILP (current_buffer->mark_active)
2665 && XMARKER (current_buffer->mark)->buffer != 0);
2667 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2668 start and end of a visible region in window IT->w. Set both to
2669 -1 to indicate no region. */
2670 if (highlight_region_p
2671 /* Maybe highlight only in selected window. */
2672 && (/* Either show region everywhere. */
2673 highlight_nonselected_windows
2674 /* Or show region in the selected window. */
2675 || w == XWINDOW (selected_window)
2676 /* Or show the region if we are in the mini-buffer and W is
2677 the window the mini-buffer refers to. */
2678 || (MINI_WINDOW_P (XWINDOW (selected_window))
2679 && WINDOWP (minibuf_selected_window)
2680 && w == XWINDOW (minibuf_selected_window))))
2682 int charpos = marker_position (current_buffer->mark);
2683 it->region_beg_charpos = min (PT, charpos);
2684 it->region_end_charpos = max (PT, charpos);
2686 else
2687 it->region_beg_charpos = it->region_end_charpos = -1;
2689 /* Get the position at which the redisplay_end_trigger hook should
2690 be run, if it is to be run at all. */
2691 if (MARKERP (w->redisplay_end_trigger)
2692 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2693 it->redisplay_end_trigger_charpos
2694 = marker_position (w->redisplay_end_trigger);
2695 else if (INTEGERP (w->redisplay_end_trigger))
2696 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2698 /* Correct bogus values of tab_width. */
2699 it->tab_width = XINT (current_buffer->tab_width);
2700 if (it->tab_width <= 0 || it->tab_width > 1000)
2701 it->tab_width = 8;
2703 /* Are lines in the display truncated? */
2704 if (base_face_id != DEFAULT_FACE_ID
2705 || XINT (it->w->hscroll)
2706 || (! WINDOW_FULL_WIDTH_P (it->w)
2707 && ((!NILP (Vtruncate_partial_width_windows)
2708 && !INTEGERP (Vtruncate_partial_width_windows))
2709 || (INTEGERP (Vtruncate_partial_width_windows)
2710 && (WINDOW_TOTAL_COLS (it->w)
2711 < XINT (Vtruncate_partial_width_windows))))))
2712 it->line_wrap = TRUNCATE;
2713 else if (NILP (current_buffer->truncate_lines))
2714 it->line_wrap = NILP (current_buffer->word_wrap)
2715 ? WINDOW_WRAP : WORD_WRAP;
2716 else
2717 it->line_wrap = TRUNCATE;
2719 /* Get dimensions of truncation and continuation glyphs. These are
2720 displayed as fringe bitmaps under X, so we don't need them for such
2721 frames. */
2722 if (!FRAME_WINDOW_P (it->f))
2724 if (it->line_wrap == TRUNCATE)
2726 /* We will need the truncation glyph. */
2727 xassert (it->glyph_row == NULL);
2728 produce_special_glyphs (it, IT_TRUNCATION);
2729 it->truncation_pixel_width = it->pixel_width;
2731 else
2733 /* We will need the continuation glyph. */
2734 xassert (it->glyph_row == NULL);
2735 produce_special_glyphs (it, IT_CONTINUATION);
2736 it->continuation_pixel_width = it->pixel_width;
2739 /* Reset these values to zero because the produce_special_glyphs
2740 above has changed them. */
2741 it->pixel_width = it->ascent = it->descent = 0;
2742 it->phys_ascent = it->phys_descent = 0;
2745 /* Set this after getting the dimensions of truncation and
2746 continuation glyphs, so that we don't produce glyphs when calling
2747 produce_special_glyphs, above. */
2748 it->glyph_row = row;
2749 it->area = TEXT_AREA;
2751 /* Get the dimensions of the display area. The display area
2752 consists of the visible window area plus a horizontally scrolled
2753 part to the left of the window. All x-values are relative to the
2754 start of this total display area. */
2755 if (base_face_id != DEFAULT_FACE_ID)
2757 /* Mode lines, menu bar in terminal frames. */
2758 it->first_visible_x = 0;
2759 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2761 else
2763 it->first_visible_x
2764 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2765 it->last_visible_x = (it->first_visible_x
2766 + window_box_width (w, TEXT_AREA));
2768 /* If we truncate lines, leave room for the truncator glyph(s) at
2769 the right margin. Otherwise, leave room for the continuation
2770 glyph(s). Truncation and continuation glyphs are not inserted
2771 for window-based redisplay. */
2772 if (!FRAME_WINDOW_P (it->f))
2774 if (it->line_wrap == TRUNCATE)
2775 it->last_visible_x -= it->truncation_pixel_width;
2776 else
2777 it->last_visible_x -= it->continuation_pixel_width;
2780 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2781 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2784 /* Leave room for a border glyph. */
2785 if (!FRAME_WINDOW_P (it->f)
2786 && !WINDOW_RIGHTMOST_P (it->w))
2787 it->last_visible_x -= 1;
2789 it->last_visible_y = window_text_bottom_y (w);
2791 /* For mode lines and alike, arrange for the first glyph having a
2792 left box line if the face specifies a box. */
2793 if (base_face_id != DEFAULT_FACE_ID)
2795 struct face *face;
2797 it->face_id = remapped_base_face_id;
2799 /* If we have a boxed mode line, make the first character appear
2800 with a left box line. */
2801 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2802 if (face->box != FACE_NO_BOX)
2803 it->start_of_box_run_p = 1;
2806 /* If a buffer position was specified, set the iterator there,
2807 getting overlays and face properties from that position. */
2808 if (charpos >= BUF_BEG (current_buffer))
2810 it->end_charpos = ZV;
2811 it->face_id = -1;
2812 IT_CHARPOS (*it) = charpos;
2814 /* Compute byte position if not specified. */
2815 if (bytepos < charpos)
2816 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2817 else
2818 IT_BYTEPOS (*it) = bytepos;
2820 it->start = it->current;
2822 /* Compute faces etc. */
2823 reseat (it, it->current.pos, 1);
2826 CHECK_IT (it);
2830 /* Initialize IT for the display of window W with window start POS. */
2832 void
2833 start_display (it, w, pos)
2834 struct it *it;
2835 struct window *w;
2836 struct text_pos pos;
2838 struct glyph_row *row;
2839 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2841 row = w->desired_matrix->rows + first_vpos;
2842 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2843 it->first_vpos = first_vpos;
2845 /* Don't reseat to previous visible line start if current start
2846 position is in a string or image. */
2847 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2849 int start_at_line_beg_p;
2850 int first_y = it->current_y;
2852 /* If window start is not at a line start, skip forward to POS to
2853 get the correct continuation lines width. */
2854 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2855 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2856 if (!start_at_line_beg_p)
2858 int new_x;
2860 reseat_at_previous_visible_line_start (it);
2861 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2863 new_x = it->current_x + it->pixel_width;
2865 /* If lines are continued, this line may end in the middle
2866 of a multi-glyph character (e.g. a control character
2867 displayed as \003, or in the middle of an overlay
2868 string). In this case move_it_to above will not have
2869 taken us to the start of the continuation line but to the
2870 end of the continued line. */
2871 if (it->current_x > 0
2872 && it->line_wrap != TRUNCATE /* Lines are continued. */
2873 && (/* And glyph doesn't fit on the line. */
2874 new_x > it->last_visible_x
2875 /* Or it fits exactly and we're on a window
2876 system frame. */
2877 || (new_x == it->last_visible_x
2878 && FRAME_WINDOW_P (it->f))))
2880 if (it->current.dpvec_index >= 0
2881 || it->current.overlay_string_index >= 0)
2883 set_iterator_to_next (it, 1);
2884 move_it_in_display_line_to (it, -1, -1, 0);
2887 it->continuation_lines_width += it->current_x;
2890 /* We're starting a new display line, not affected by the
2891 height of the continued line, so clear the appropriate
2892 fields in the iterator structure. */
2893 it->max_ascent = it->max_descent = 0;
2894 it->max_phys_ascent = it->max_phys_descent = 0;
2896 it->current_y = first_y;
2897 it->vpos = 0;
2898 it->current_x = it->hpos = 0;
2904 /* Return 1 if POS is a position in ellipses displayed for invisible
2905 text. W is the window we display, for text property lookup. */
2907 static int
2908 in_ellipses_for_invisible_text_p (pos, w)
2909 struct display_pos *pos;
2910 struct window *w;
2912 Lisp_Object prop, window;
2913 int ellipses_p = 0;
2914 int charpos = CHARPOS (pos->pos);
2916 /* If POS specifies a position in a display vector, this might
2917 be for an ellipsis displayed for invisible text. We won't
2918 get the iterator set up for delivering that ellipsis unless
2919 we make sure that it gets aware of the invisible text. */
2920 if (pos->dpvec_index >= 0
2921 && pos->overlay_string_index < 0
2922 && CHARPOS (pos->string_pos) < 0
2923 && charpos > BEGV
2924 && (XSETWINDOW (window, w),
2925 prop = Fget_char_property (make_number (charpos),
2926 Qinvisible, window),
2927 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2929 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2930 window);
2931 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2934 return ellipses_p;
2938 /* Initialize IT for stepping through current_buffer in window W,
2939 starting at position POS that includes overlay string and display
2940 vector/ control character translation position information. Value
2941 is zero if there are overlay strings with newlines at POS. */
2943 static int
2944 init_from_display_pos (it, w, pos)
2945 struct it *it;
2946 struct window *w;
2947 struct display_pos *pos;
2949 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2950 int i, overlay_strings_with_newlines = 0;
2952 /* If POS specifies a position in a display vector, this might
2953 be for an ellipsis displayed for invisible text. We won't
2954 get the iterator set up for delivering that ellipsis unless
2955 we make sure that it gets aware of the invisible text. */
2956 if (in_ellipses_for_invisible_text_p (pos, w))
2958 --charpos;
2959 bytepos = 0;
2962 /* Keep in mind: the call to reseat in init_iterator skips invisible
2963 text, so we might end up at a position different from POS. This
2964 is only a problem when POS is a row start after a newline and an
2965 overlay starts there with an after-string, and the overlay has an
2966 invisible property. Since we don't skip invisible text in
2967 display_line and elsewhere immediately after consuming the
2968 newline before the row start, such a POS will not be in a string,
2969 but the call to init_iterator below will move us to the
2970 after-string. */
2971 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2973 /* This only scans the current chunk -- it should scan all chunks.
2974 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2975 to 16 in 22.1 to make this a lesser problem. */
2976 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2978 const char *s = SDATA (it->overlay_strings[i]);
2979 const char *e = s + SBYTES (it->overlay_strings[i]);
2981 while (s < e && *s != '\n')
2982 ++s;
2984 if (s < e)
2986 overlay_strings_with_newlines = 1;
2987 break;
2991 /* If position is within an overlay string, set up IT to the right
2992 overlay string. */
2993 if (pos->overlay_string_index >= 0)
2995 int relative_index;
2997 /* If the first overlay string happens to have a `display'
2998 property for an image, the iterator will be set up for that
2999 image, and we have to undo that setup first before we can
3000 correct the overlay string index. */
3001 if (it->method == GET_FROM_IMAGE)
3002 pop_it (it);
3004 /* We already have the first chunk of overlay strings in
3005 IT->overlay_strings. Load more until the one for
3006 pos->overlay_string_index is in IT->overlay_strings. */
3007 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3009 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3010 it->current.overlay_string_index = 0;
3011 while (n--)
3013 load_overlay_strings (it, 0);
3014 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3018 it->current.overlay_string_index = pos->overlay_string_index;
3019 relative_index = (it->current.overlay_string_index
3020 % OVERLAY_STRING_CHUNK_SIZE);
3021 it->string = it->overlay_strings[relative_index];
3022 xassert (STRINGP (it->string));
3023 it->current.string_pos = pos->string_pos;
3024 it->method = GET_FROM_STRING;
3027 if (CHARPOS (pos->string_pos) >= 0)
3029 /* Recorded position is not in an overlay string, but in another
3030 string. This can only be a string from a `display' property.
3031 IT should already be filled with that string. */
3032 it->current.string_pos = pos->string_pos;
3033 xassert (STRINGP (it->string));
3036 /* Restore position in display vector translations, control
3037 character translations or ellipses. */
3038 if (pos->dpvec_index >= 0)
3040 if (it->dpvec == NULL)
3041 get_next_display_element (it);
3042 xassert (it->dpvec && it->current.dpvec_index == 0);
3043 it->current.dpvec_index = pos->dpvec_index;
3046 CHECK_IT (it);
3047 return !overlay_strings_with_newlines;
3051 /* Initialize IT for stepping through current_buffer in window W
3052 starting at ROW->start. */
3054 static void
3055 init_to_row_start (it, w, row)
3056 struct it *it;
3057 struct window *w;
3058 struct glyph_row *row;
3060 init_from_display_pos (it, w, &row->start);
3061 it->start = row->start;
3062 it->continuation_lines_width = row->continuation_lines_width;
3063 CHECK_IT (it);
3067 /* Initialize IT for stepping through current_buffer in window W
3068 starting in the line following ROW, i.e. starting at ROW->end.
3069 Value is zero if there are overlay strings with newlines at ROW's
3070 end position. */
3072 static int
3073 init_to_row_end (it, w, row)
3074 struct it *it;
3075 struct window *w;
3076 struct glyph_row *row;
3078 int success = 0;
3080 if (init_from_display_pos (it, w, &row->end))
3082 if (row->continued_p)
3083 it->continuation_lines_width
3084 = row->continuation_lines_width + row->pixel_width;
3085 CHECK_IT (it);
3086 success = 1;
3089 return success;
3095 /***********************************************************************
3096 Text properties
3097 ***********************************************************************/
3099 /* Called when IT reaches IT->stop_charpos. Handle text property and
3100 overlay changes. Set IT->stop_charpos to the next position where
3101 to stop. */
3103 static void
3104 handle_stop (it)
3105 struct it *it;
3107 enum prop_handled handled;
3108 int handle_overlay_change_p;
3109 struct props *p;
3111 it->dpvec = NULL;
3112 it->current.dpvec_index = -1;
3113 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3114 it->ignore_overlay_strings_at_pos_p = 0;
3115 it->ellipsis_p = 0;
3117 /* Use face of preceding text for ellipsis (if invisible) */
3118 if (it->selective_display_ellipsis_p)
3119 it->saved_face_id = it->face_id;
3123 handled = HANDLED_NORMALLY;
3125 /* Call text property handlers. */
3126 for (p = it_props; p->handler; ++p)
3128 handled = p->handler (it);
3130 if (handled == HANDLED_RECOMPUTE_PROPS)
3131 break;
3132 else if (handled == HANDLED_RETURN)
3134 /* We still want to show before and after strings from
3135 overlays even if the actual buffer text is replaced. */
3136 if (!handle_overlay_change_p
3137 || it->sp > 1
3138 || !get_overlay_strings_1 (it, 0, 0))
3140 if (it->ellipsis_p)
3141 setup_for_ellipsis (it, 0);
3142 /* When handling a display spec, we might load an
3143 empty string. In that case, discard it here. We
3144 used to discard it in handle_single_display_spec,
3145 but that causes get_overlay_strings_1, above, to
3146 ignore overlay strings that we must check. */
3147 if (STRINGP (it->string) && !SCHARS (it->string))
3148 pop_it (it);
3149 return;
3151 else if (STRINGP (it->string) && !SCHARS (it->string))
3152 pop_it (it);
3153 else
3155 it->ignore_overlay_strings_at_pos_p = 1;
3156 it->string_from_display_prop_p = 0;
3157 handle_overlay_change_p = 0;
3159 handled = HANDLED_RECOMPUTE_PROPS;
3160 break;
3162 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3163 handle_overlay_change_p = 0;
3166 if (handled != HANDLED_RECOMPUTE_PROPS)
3168 /* Don't check for overlay strings below when set to deliver
3169 characters from a display vector. */
3170 if (it->method == GET_FROM_DISPLAY_VECTOR)
3171 handle_overlay_change_p = 0;
3173 /* Handle overlay changes.
3174 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3175 if it finds overlays. */
3176 if (handle_overlay_change_p)
3177 handled = handle_overlay_change (it);
3180 if (it->ellipsis_p)
3182 setup_for_ellipsis (it, 0);
3183 break;
3186 while (handled == HANDLED_RECOMPUTE_PROPS);
3188 /* Determine where to stop next. */
3189 if (handled == HANDLED_NORMALLY)
3190 compute_stop_pos (it);
3194 /* Compute IT->stop_charpos from text property and overlay change
3195 information for IT's current position. */
3197 static void
3198 compute_stop_pos (it)
3199 struct it *it;
3201 register INTERVAL iv, next_iv;
3202 Lisp_Object object, limit, position;
3203 EMACS_INT charpos, bytepos;
3205 /* If nowhere else, stop at the end. */
3206 it->stop_charpos = it->end_charpos;
3208 if (STRINGP (it->string))
3210 /* Strings are usually short, so don't limit the search for
3211 properties. */
3212 object = it->string;
3213 limit = Qnil;
3214 charpos = IT_STRING_CHARPOS (*it);
3215 bytepos = IT_STRING_BYTEPOS (*it);
3217 else
3219 EMACS_INT pos;
3221 /* If next overlay change is in front of the current stop pos
3222 (which is IT->end_charpos), stop there. Note: value of
3223 next_overlay_change is point-max if no overlay change
3224 follows. */
3225 charpos = IT_CHARPOS (*it);
3226 bytepos = IT_BYTEPOS (*it);
3227 pos = next_overlay_change (charpos);
3228 if (pos < it->stop_charpos)
3229 it->stop_charpos = pos;
3231 /* If showing the region, we have to stop at the region
3232 start or end because the face might change there. */
3233 if (it->region_beg_charpos > 0)
3235 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3236 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3237 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3238 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3241 /* Set up variables for computing the stop position from text
3242 property changes. */
3243 XSETBUFFER (object, current_buffer);
3244 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3247 /* Get the interval containing IT's position. Value is a null
3248 interval if there isn't such an interval. */
3249 position = make_number (charpos);
3250 iv = validate_interval_range (object, &position, &position, 0);
3251 if (!NULL_INTERVAL_P (iv))
3253 Lisp_Object values_here[LAST_PROP_IDX];
3254 struct props *p;
3256 /* Get properties here. */
3257 for (p = it_props; p->handler; ++p)
3258 values_here[p->idx] = textget (iv->plist, *p->name);
3260 /* Look for an interval following iv that has different
3261 properties. */
3262 for (next_iv = next_interval (iv);
3263 (!NULL_INTERVAL_P (next_iv)
3264 && (NILP (limit)
3265 || XFASTINT (limit) > next_iv->position));
3266 next_iv = next_interval (next_iv))
3268 for (p = it_props; p->handler; ++p)
3270 Lisp_Object new_value;
3272 new_value = textget (next_iv->plist, *p->name);
3273 if (!EQ (values_here[p->idx], new_value))
3274 break;
3277 if (p->handler)
3278 break;
3281 if (!NULL_INTERVAL_P (next_iv))
3283 if (INTEGERP (limit)
3284 && next_iv->position >= XFASTINT (limit))
3285 /* No text property change up to limit. */
3286 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3287 else
3288 /* Text properties change in next_iv. */
3289 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3293 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3294 it->stop_charpos, it->string);
3296 xassert (STRINGP (it->string)
3297 || (it->stop_charpos >= BEGV
3298 && it->stop_charpos >= IT_CHARPOS (*it)));
3302 /* Return the position of the next overlay change after POS in
3303 current_buffer. Value is point-max if no overlay change
3304 follows. This is like `next-overlay-change' but doesn't use
3305 xmalloc. */
3307 static EMACS_INT
3308 next_overlay_change (pos)
3309 EMACS_INT pos;
3311 int noverlays;
3312 EMACS_INT endpos;
3313 Lisp_Object *overlays;
3314 int i;
3316 /* Get all overlays at the given position. */
3317 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3319 /* If any of these overlays ends before endpos,
3320 use its ending point instead. */
3321 for (i = 0; i < noverlays; ++i)
3323 Lisp_Object oend;
3324 EMACS_INT oendpos;
3326 oend = OVERLAY_END (overlays[i]);
3327 oendpos = OVERLAY_POSITION (oend);
3328 endpos = min (endpos, oendpos);
3331 return endpos;
3336 /***********************************************************************
3337 Fontification
3338 ***********************************************************************/
3340 /* Handle changes in the `fontified' property of the current buffer by
3341 calling hook functions from Qfontification_functions to fontify
3342 regions of text. */
3344 static enum prop_handled
3345 handle_fontified_prop (it)
3346 struct it *it;
3348 Lisp_Object prop, pos;
3349 enum prop_handled handled = HANDLED_NORMALLY;
3351 if (!NILP (Vmemory_full))
3352 return handled;
3354 /* Get the value of the `fontified' property at IT's current buffer
3355 position. (The `fontified' property doesn't have a special
3356 meaning in strings.) If the value is nil, call functions from
3357 Qfontification_functions. */
3358 if (!STRINGP (it->string)
3359 && it->s == NULL
3360 && !NILP (Vfontification_functions)
3361 && !NILP (Vrun_hooks)
3362 && (pos = make_number (IT_CHARPOS (*it)),
3363 prop = Fget_char_property (pos, Qfontified, Qnil),
3364 /* Ignore the special cased nil value always present at EOB since
3365 no amount of fontifying will be able to change it. */
3366 NILP (prop) && IT_CHARPOS (*it) < Z))
3368 int count = SPECPDL_INDEX ();
3369 Lisp_Object val;
3371 val = Vfontification_functions;
3372 specbind (Qfontification_functions, Qnil);
3374 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3375 safe_call1 (val, pos);
3376 else
3378 Lisp_Object globals, fn;
3379 struct gcpro gcpro1, gcpro2;
3381 globals = Qnil;
3382 GCPRO2 (val, globals);
3384 for (; CONSP (val); val = XCDR (val))
3386 fn = XCAR (val);
3388 if (EQ (fn, Qt))
3390 /* A value of t indicates this hook has a local
3391 binding; it means to run the global binding too.
3392 In a global value, t should not occur. If it
3393 does, we must ignore it to avoid an endless
3394 loop. */
3395 for (globals = Fdefault_value (Qfontification_functions);
3396 CONSP (globals);
3397 globals = XCDR (globals))
3399 fn = XCAR (globals);
3400 if (!EQ (fn, Qt))
3401 safe_call1 (fn, pos);
3404 else
3405 safe_call1 (fn, pos);
3408 UNGCPRO;
3411 unbind_to (count, Qnil);
3413 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3414 something. This avoids an endless loop if they failed to
3415 fontify the text for which reason ever. */
3416 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3417 handled = HANDLED_RECOMPUTE_PROPS;
3420 return handled;
3425 /***********************************************************************
3426 Faces
3427 ***********************************************************************/
3429 /* Set up iterator IT from face properties at its current position.
3430 Called from handle_stop. */
3432 static enum prop_handled
3433 handle_face_prop (it)
3434 struct it *it;
3436 int new_face_id;
3437 EMACS_INT next_stop;
3439 if (!STRINGP (it->string))
3441 new_face_id
3442 = face_at_buffer_position (it->w,
3443 IT_CHARPOS (*it),
3444 it->region_beg_charpos,
3445 it->region_end_charpos,
3446 &next_stop,
3447 (IT_CHARPOS (*it)
3448 + TEXT_PROP_DISTANCE_LIMIT),
3449 0, it->base_face_id);
3451 /* Is this a start of a run of characters with box face?
3452 Caveat: this can be called for a freshly initialized
3453 iterator; face_id is -1 in this case. We know that the new
3454 face will not change until limit, i.e. if the new face has a
3455 box, all characters up to limit will have one. But, as
3456 usual, we don't know whether limit is really the end. */
3457 if (new_face_id != it->face_id)
3459 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3461 /* If new face has a box but old face has not, this is
3462 the start of a run of characters with box, i.e. it has
3463 a shadow on the left side. The value of face_id of the
3464 iterator will be -1 if this is the initial call that gets
3465 the face. In this case, we have to look in front of IT's
3466 position and see whether there is a face != new_face_id. */
3467 it->start_of_box_run_p
3468 = (new_face->box != FACE_NO_BOX
3469 && (it->face_id >= 0
3470 || IT_CHARPOS (*it) == BEG
3471 || new_face_id != face_before_it_pos (it)));
3472 it->face_box_p = new_face->box != FACE_NO_BOX;
3475 else
3477 int base_face_id, bufpos;
3478 int i;
3479 Lisp_Object from_overlay
3480 = (it->current.overlay_string_index >= 0
3481 ? it->string_overlays[it->current.overlay_string_index]
3482 : Qnil);
3484 /* See if we got to this string directly or indirectly from
3485 an overlay property. That includes the before-string or
3486 after-string of an overlay, strings in display properties
3487 provided by an overlay, their text properties, etc.
3489 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3490 if (! NILP (from_overlay))
3491 for (i = it->sp - 1; i >= 0; i--)
3493 if (it->stack[i].current.overlay_string_index >= 0)
3494 from_overlay
3495 = it->string_overlays[it->stack[i].current.overlay_string_index];
3496 else if (! NILP (it->stack[i].from_overlay))
3497 from_overlay = it->stack[i].from_overlay;
3499 if (!NILP (from_overlay))
3500 break;
3503 if (! NILP (from_overlay))
3505 bufpos = IT_CHARPOS (*it);
3506 /* For a string from an overlay, the base face depends
3507 only on text properties and ignores overlays. */
3508 base_face_id
3509 = face_for_overlay_string (it->w,
3510 IT_CHARPOS (*it),
3511 it->region_beg_charpos,
3512 it->region_end_charpos,
3513 &next_stop,
3514 (IT_CHARPOS (*it)
3515 + TEXT_PROP_DISTANCE_LIMIT),
3517 from_overlay);
3519 else
3521 bufpos = 0;
3523 /* For strings from a `display' property, use the face at
3524 IT's current buffer position as the base face to merge
3525 with, so that overlay strings appear in the same face as
3526 surrounding text, unless they specify their own
3527 faces. */
3528 base_face_id = underlying_face_id (it);
3531 new_face_id = face_at_string_position (it->w,
3532 it->string,
3533 IT_STRING_CHARPOS (*it),
3534 bufpos,
3535 it->region_beg_charpos,
3536 it->region_end_charpos,
3537 &next_stop,
3538 base_face_id, 0);
3540 /* Is this a start of a run of characters with box? Caveat:
3541 this can be called for a freshly allocated iterator; face_id
3542 is -1 is this case. We know that the new face will not
3543 change until the next check pos, i.e. if the new face has a
3544 box, all characters up to that position will have a
3545 box. But, as usual, we don't know whether that position
3546 is really the end. */
3547 if (new_face_id != it->face_id)
3549 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3550 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3552 /* If new face has a box but old face hasn't, this is the
3553 start of a run of characters with box, i.e. it has a
3554 shadow on the left side. */
3555 it->start_of_box_run_p
3556 = new_face->box && (old_face == NULL || !old_face->box);
3557 it->face_box_p = new_face->box != FACE_NO_BOX;
3561 it->face_id = new_face_id;
3562 return HANDLED_NORMALLY;
3566 /* Return the ID of the face ``underlying'' IT's current position,
3567 which is in a string. If the iterator is associated with a
3568 buffer, return the face at IT's current buffer position.
3569 Otherwise, use the iterator's base_face_id. */
3571 static int
3572 underlying_face_id (it)
3573 struct it *it;
3575 int face_id = it->base_face_id, i;
3577 xassert (STRINGP (it->string));
3579 for (i = it->sp - 1; i >= 0; --i)
3580 if (NILP (it->stack[i].string))
3581 face_id = it->stack[i].face_id;
3583 return face_id;
3587 /* Compute the face one character before or after the current position
3588 of IT. BEFORE_P non-zero means get the face in front of IT's
3589 position. Value is the id of the face. */
3591 static int
3592 face_before_or_after_it_pos (it, before_p)
3593 struct it *it;
3594 int before_p;
3596 int face_id, limit;
3597 EMACS_INT next_check_charpos;
3598 struct text_pos pos;
3600 xassert (it->s == NULL);
3602 if (STRINGP (it->string))
3604 int bufpos, base_face_id;
3606 /* No face change past the end of the string (for the case
3607 we are padding with spaces). No face change before the
3608 string start. */
3609 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3610 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3611 return it->face_id;
3613 /* Set pos to the position before or after IT's current position. */
3614 if (before_p)
3615 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3616 else
3617 /* For composition, we must check the character after the
3618 composition. */
3619 pos = (it->what == IT_COMPOSITION
3620 ? string_pos (IT_STRING_CHARPOS (*it)
3621 + it->cmp_it.nchars, it->string)
3622 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3624 if (it->current.overlay_string_index >= 0)
3625 bufpos = IT_CHARPOS (*it);
3626 else
3627 bufpos = 0;
3629 base_face_id = underlying_face_id (it);
3631 /* Get the face for ASCII, or unibyte. */
3632 face_id = face_at_string_position (it->w,
3633 it->string,
3634 CHARPOS (pos),
3635 bufpos,
3636 it->region_beg_charpos,
3637 it->region_end_charpos,
3638 &next_check_charpos,
3639 base_face_id, 0);
3641 /* Correct the face for charsets different from ASCII. Do it
3642 for the multibyte case only. The face returned above is
3643 suitable for unibyte text if IT->string is unibyte. */
3644 if (STRING_MULTIBYTE (it->string))
3646 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3647 int rest = SBYTES (it->string) - BYTEPOS (pos);
3648 int c, len;
3649 struct face *face = FACE_FROM_ID (it->f, face_id);
3651 c = string_char_and_length (p, &len);
3652 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3655 else
3657 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3658 || (IT_CHARPOS (*it) <= BEGV && before_p))
3659 return it->face_id;
3661 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3662 pos = it->current.pos;
3664 if (before_p)
3665 DEC_TEXT_POS (pos, it->multibyte_p);
3666 else
3668 if (it->what == IT_COMPOSITION)
3669 /* For composition, we must check the position after the
3670 composition. */
3671 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3672 else
3673 INC_TEXT_POS (pos, it->multibyte_p);
3676 /* Determine face for CHARSET_ASCII, or unibyte. */
3677 face_id = face_at_buffer_position (it->w,
3678 CHARPOS (pos),
3679 it->region_beg_charpos,
3680 it->region_end_charpos,
3681 &next_check_charpos,
3682 limit, 0, -1);
3684 /* Correct the face for charsets different from ASCII. Do it
3685 for the multibyte case only. The face returned above is
3686 suitable for unibyte text if current_buffer is unibyte. */
3687 if (it->multibyte_p)
3689 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3690 struct face *face = FACE_FROM_ID (it->f, face_id);
3691 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3695 return face_id;
3700 /***********************************************************************
3701 Invisible text
3702 ***********************************************************************/
3704 /* Set up iterator IT from invisible properties at its current
3705 position. Called from handle_stop. */
3707 static enum prop_handled
3708 handle_invisible_prop (it)
3709 struct it *it;
3711 enum prop_handled handled = HANDLED_NORMALLY;
3713 if (STRINGP (it->string))
3715 extern Lisp_Object Qinvisible;
3716 Lisp_Object prop, end_charpos, limit, charpos;
3718 /* Get the value of the invisible text property at the
3719 current position. Value will be nil if there is no such
3720 property. */
3721 charpos = make_number (IT_STRING_CHARPOS (*it));
3722 prop = Fget_text_property (charpos, Qinvisible, it->string);
3724 if (!NILP (prop)
3725 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3727 handled = HANDLED_RECOMPUTE_PROPS;
3729 /* Get the position at which the next change of the
3730 invisible text property can be found in IT->string.
3731 Value will be nil if the property value is the same for
3732 all the rest of IT->string. */
3733 XSETINT (limit, SCHARS (it->string));
3734 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3735 it->string, limit);
3737 /* Text at current position is invisible. The next
3738 change in the property is at position end_charpos.
3739 Move IT's current position to that position. */
3740 if (INTEGERP (end_charpos)
3741 && XFASTINT (end_charpos) < XFASTINT (limit))
3743 struct text_pos old;
3744 old = it->current.string_pos;
3745 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3746 compute_string_pos (&it->current.string_pos, old, it->string);
3748 else
3750 /* The rest of the string is invisible. If this is an
3751 overlay string, proceed with the next overlay string
3752 or whatever comes and return a character from there. */
3753 if (it->current.overlay_string_index >= 0)
3755 next_overlay_string (it);
3756 /* Don't check for overlay strings when we just
3757 finished processing them. */
3758 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3760 else
3762 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3763 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3768 else
3770 int invis_p;
3771 EMACS_INT newpos, next_stop, start_charpos;
3772 Lisp_Object pos, prop, overlay;
3774 /* First of all, is there invisible text at this position? */
3775 start_charpos = IT_CHARPOS (*it);
3776 pos = make_number (IT_CHARPOS (*it));
3777 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3778 &overlay);
3779 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3781 /* If we are on invisible text, skip over it. */
3782 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3784 /* Record whether we have to display an ellipsis for the
3785 invisible text. */
3786 int display_ellipsis_p = invis_p == 2;
3788 handled = HANDLED_RECOMPUTE_PROPS;
3790 /* Loop skipping over invisible text. The loop is left at
3791 ZV or with IT on the first char being visible again. */
3794 /* Try to skip some invisible text. Return value is the
3795 position reached which can be equal to IT's position
3796 if there is nothing invisible here. This skips both
3797 over invisible text properties and overlays with
3798 invisible property. */
3799 newpos = skip_invisible (IT_CHARPOS (*it),
3800 &next_stop, ZV, it->window);
3802 /* If we skipped nothing at all we weren't at invisible
3803 text in the first place. If everything to the end of
3804 the buffer was skipped, end the loop. */
3805 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3806 invis_p = 0;
3807 else
3809 /* We skipped some characters but not necessarily
3810 all there are. Check if we ended up on visible
3811 text. Fget_char_property returns the property of
3812 the char before the given position, i.e. if we
3813 get invis_p = 0, this means that the char at
3814 newpos is visible. */
3815 pos = make_number (newpos);
3816 prop = Fget_char_property (pos, Qinvisible, it->window);
3817 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3820 /* If we ended up on invisible text, proceed to
3821 skip starting with next_stop. */
3822 if (invis_p)
3823 IT_CHARPOS (*it) = next_stop;
3825 /* If there are adjacent invisible texts, don't lose the
3826 second one's ellipsis. */
3827 if (invis_p == 2)
3828 display_ellipsis_p = 1;
3830 while (invis_p);
3832 /* The position newpos is now either ZV or on visible text. */
3833 IT_CHARPOS (*it) = newpos;
3834 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3836 /* If there are before-strings at the start of invisible
3837 text, and the text is invisible because of a text
3838 property, arrange to show before-strings because 20.x did
3839 it that way. (If the text is invisible because of an
3840 overlay property instead of a text property, this is
3841 already handled in the overlay code.) */
3842 if (NILP (overlay)
3843 && get_overlay_strings (it, start_charpos))
3845 handled = HANDLED_RECOMPUTE_PROPS;
3846 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3848 else if (display_ellipsis_p)
3850 /* Make sure that the glyphs of the ellipsis will get
3851 correct `charpos' values. If we would not update
3852 it->position here, the glyphs would belong to the
3853 last visible character _before_ the invisible
3854 text, which confuses `set_cursor_from_row'.
3856 We use the last invisible position instead of the
3857 first because this way the cursor is always drawn on
3858 the first "." of the ellipsis, whenever PT is inside
3859 the invisible text. Otherwise the cursor would be
3860 placed _after_ the ellipsis when the point is after the
3861 first invisible character. */
3862 if (!STRINGP (it->object))
3864 it->position.charpos = IT_CHARPOS (*it) - 1;
3865 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3867 it->ellipsis_p = 1;
3868 /* Let the ellipsis display before
3869 considering any properties of the following char.
3870 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3871 handled = HANDLED_RETURN;
3876 return handled;
3880 /* Make iterator IT return `...' next.
3881 Replaces LEN characters from buffer. */
3883 static void
3884 setup_for_ellipsis (it, len)
3885 struct it *it;
3886 int len;
3888 /* Use the display table definition for `...'. Invalid glyphs
3889 will be handled by the method returning elements from dpvec. */
3890 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3892 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3893 it->dpvec = v->contents;
3894 it->dpend = v->contents + v->size;
3896 else
3898 /* Default `...'. */
3899 it->dpvec = default_invis_vector;
3900 it->dpend = default_invis_vector + 3;
3903 it->dpvec_char_len = len;
3904 it->current.dpvec_index = 0;
3905 it->dpvec_face_id = -1;
3907 /* Remember the current face id in case glyphs specify faces.
3908 IT's face is restored in set_iterator_to_next.
3909 saved_face_id was set to preceding char's face in handle_stop. */
3910 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3911 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3913 it->method = GET_FROM_DISPLAY_VECTOR;
3914 it->ellipsis_p = 1;
3919 /***********************************************************************
3920 'display' property
3921 ***********************************************************************/
3923 /* Set up iterator IT from `display' property at its current position.
3924 Called from handle_stop.
3925 We return HANDLED_RETURN if some part of the display property
3926 overrides the display of the buffer text itself.
3927 Otherwise we return HANDLED_NORMALLY. */
3929 static enum prop_handled
3930 handle_display_prop (it)
3931 struct it *it;
3933 Lisp_Object prop, object, overlay;
3934 struct text_pos *position;
3935 /* Nonzero if some property replaces the display of the text itself. */
3936 int display_replaced_p = 0;
3938 if (STRINGP (it->string))
3940 object = it->string;
3941 position = &it->current.string_pos;
3943 else
3945 XSETWINDOW (object, it->w);
3946 position = &it->current.pos;
3949 /* Reset those iterator values set from display property values. */
3950 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3951 it->space_width = Qnil;
3952 it->font_height = Qnil;
3953 it->voffset = 0;
3955 /* We don't support recursive `display' properties, i.e. string
3956 values that have a string `display' property, that have a string
3957 `display' property etc. */
3958 if (!it->string_from_display_prop_p)
3959 it->area = TEXT_AREA;
3961 prop = get_char_property_and_overlay (make_number (position->charpos),
3962 Qdisplay, object, &overlay);
3963 if (NILP (prop))
3964 return HANDLED_NORMALLY;
3965 /* Now OVERLAY is the overlay that gave us this property, or nil
3966 if it was a text property. */
3968 if (!STRINGP (it->string))
3969 object = it->w->buffer;
3971 if (CONSP (prop)
3972 /* Simple properties. */
3973 && !EQ (XCAR (prop), Qimage)
3974 && !EQ (XCAR (prop), Qspace)
3975 && !EQ (XCAR (prop), Qwhen)
3976 && !EQ (XCAR (prop), Qslice)
3977 && !EQ (XCAR (prop), Qspace_width)
3978 && !EQ (XCAR (prop), Qheight)
3979 && !EQ (XCAR (prop), Qraise)
3980 /* Marginal area specifications. */
3981 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3982 && !EQ (XCAR (prop), Qleft_fringe)
3983 && !EQ (XCAR (prop), Qright_fringe)
3984 && !NILP (XCAR (prop)))
3986 for (; CONSP (prop); prop = XCDR (prop))
3988 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3989 position, display_replaced_p))
3991 display_replaced_p = 1;
3992 /* If some text in a string is replaced, `position' no
3993 longer points to the position of `object'. */
3994 if (STRINGP (object))
3995 break;
3999 else if (VECTORP (prop))
4001 int i;
4002 for (i = 0; i < ASIZE (prop); ++i)
4003 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
4004 position, display_replaced_p))
4006 display_replaced_p = 1;
4007 /* If some text in a string is replaced, `position' no
4008 longer points to the position of `object'. */
4009 if (STRINGP (object))
4010 break;
4013 else
4015 if (handle_single_display_spec (it, prop, object, overlay,
4016 position, 0))
4017 display_replaced_p = 1;
4020 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4024 /* Value is the position of the end of the `display' property starting
4025 at START_POS in OBJECT. */
4027 static struct text_pos
4028 display_prop_end (it, object, start_pos)
4029 struct it *it;
4030 Lisp_Object object;
4031 struct text_pos start_pos;
4033 Lisp_Object end;
4034 struct text_pos end_pos;
4036 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4037 Qdisplay, object, Qnil);
4038 CHARPOS (end_pos) = XFASTINT (end);
4039 if (STRINGP (object))
4040 compute_string_pos (&end_pos, start_pos, it->string);
4041 else
4042 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4044 return end_pos;
4048 /* Set up IT from a single `display' specification PROP. OBJECT
4049 is the object in which the `display' property was found. *POSITION
4050 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4051 means that we previously saw a display specification which already
4052 replaced text display with something else, for example an image;
4053 we ignore such properties after the first one has been processed.
4055 OVERLAY is the overlay this `display' property came from,
4056 or nil if it was a text property.
4058 If PROP is a `space' or `image' specification, and in some other
4059 cases too, set *POSITION to the position where the `display'
4060 property ends.
4062 Value is non-zero if something was found which replaces the display
4063 of buffer or string text. */
4065 static int
4066 handle_single_display_spec (it, spec, object, overlay, position,
4067 display_replaced_before_p)
4068 struct it *it;
4069 Lisp_Object spec;
4070 Lisp_Object object;
4071 Lisp_Object overlay;
4072 struct text_pos *position;
4073 int display_replaced_before_p;
4075 Lisp_Object form;
4076 Lisp_Object location, value;
4077 struct text_pos start_pos, save_pos;
4078 int valid_p;
4080 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4081 If the result is non-nil, use VALUE instead of SPEC. */
4082 form = Qt;
4083 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4085 spec = XCDR (spec);
4086 if (!CONSP (spec))
4087 return 0;
4088 form = XCAR (spec);
4089 spec = XCDR (spec);
4092 if (!NILP (form) && !EQ (form, Qt))
4094 int count = SPECPDL_INDEX ();
4095 struct gcpro gcpro1;
4097 /* Bind `object' to the object having the `display' property, a
4098 buffer or string. Bind `position' to the position in the
4099 object where the property was found, and `buffer-position'
4100 to the current position in the buffer. */
4101 specbind (Qobject, object);
4102 specbind (Qposition, make_number (CHARPOS (*position)));
4103 specbind (Qbuffer_position,
4104 make_number (STRINGP (object)
4105 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4106 GCPRO1 (form);
4107 form = safe_eval (form);
4108 UNGCPRO;
4109 unbind_to (count, Qnil);
4112 if (NILP (form))
4113 return 0;
4115 /* Handle `(height HEIGHT)' specifications. */
4116 if (CONSP (spec)
4117 && EQ (XCAR (spec), Qheight)
4118 && CONSP (XCDR (spec)))
4120 if (!FRAME_WINDOW_P (it->f))
4121 return 0;
4123 it->font_height = XCAR (XCDR (spec));
4124 if (!NILP (it->font_height))
4126 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4127 int new_height = -1;
4129 if (CONSP (it->font_height)
4130 && (EQ (XCAR (it->font_height), Qplus)
4131 || EQ (XCAR (it->font_height), Qminus))
4132 && CONSP (XCDR (it->font_height))
4133 && INTEGERP (XCAR (XCDR (it->font_height))))
4135 /* `(+ N)' or `(- N)' where N is an integer. */
4136 int steps = XINT (XCAR (XCDR (it->font_height)));
4137 if (EQ (XCAR (it->font_height), Qplus))
4138 steps = - steps;
4139 it->face_id = smaller_face (it->f, it->face_id, steps);
4141 else if (FUNCTIONP (it->font_height))
4143 /* Call function with current height as argument.
4144 Value is the new height. */
4145 Lisp_Object height;
4146 height = safe_call1 (it->font_height,
4147 face->lface[LFACE_HEIGHT_INDEX]);
4148 if (NUMBERP (height))
4149 new_height = XFLOATINT (height);
4151 else if (NUMBERP (it->font_height))
4153 /* Value is a multiple of the canonical char height. */
4154 struct face *face;
4156 face = FACE_FROM_ID (it->f,
4157 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4158 new_height = (XFLOATINT (it->font_height)
4159 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4161 else
4163 /* Evaluate IT->font_height with `height' bound to the
4164 current specified height to get the new height. */
4165 int count = SPECPDL_INDEX ();
4167 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4168 value = safe_eval (it->font_height);
4169 unbind_to (count, Qnil);
4171 if (NUMBERP (value))
4172 new_height = XFLOATINT (value);
4175 if (new_height > 0)
4176 it->face_id = face_with_height (it->f, it->face_id, new_height);
4179 return 0;
4182 /* Handle `(space-width WIDTH)'. */
4183 if (CONSP (spec)
4184 && EQ (XCAR (spec), Qspace_width)
4185 && CONSP (XCDR (spec)))
4187 if (!FRAME_WINDOW_P (it->f))
4188 return 0;
4190 value = XCAR (XCDR (spec));
4191 if (NUMBERP (value) && XFLOATINT (value) > 0)
4192 it->space_width = value;
4194 return 0;
4197 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4198 if (CONSP (spec)
4199 && EQ (XCAR (spec), Qslice))
4201 Lisp_Object tem;
4203 if (!FRAME_WINDOW_P (it->f))
4204 return 0;
4206 if (tem = XCDR (spec), CONSP (tem))
4208 it->slice.x = XCAR (tem);
4209 if (tem = XCDR (tem), CONSP (tem))
4211 it->slice.y = XCAR (tem);
4212 if (tem = XCDR (tem), CONSP (tem))
4214 it->slice.width = XCAR (tem);
4215 if (tem = XCDR (tem), CONSP (tem))
4216 it->slice.height = XCAR (tem);
4221 return 0;
4224 /* Handle `(raise FACTOR)'. */
4225 if (CONSP (spec)
4226 && EQ (XCAR (spec), Qraise)
4227 && CONSP (XCDR (spec)))
4229 if (!FRAME_WINDOW_P (it->f))
4230 return 0;
4232 #ifdef HAVE_WINDOW_SYSTEM
4233 value = XCAR (XCDR (spec));
4234 if (NUMBERP (value))
4236 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4237 it->voffset = - (XFLOATINT (value)
4238 * (FONT_HEIGHT (face->font)));
4240 #endif /* HAVE_WINDOW_SYSTEM */
4242 return 0;
4245 /* Don't handle the other kinds of display specifications
4246 inside a string that we got from a `display' property. */
4247 if (it->string_from_display_prop_p)
4248 return 0;
4250 /* Characters having this form of property are not displayed, so
4251 we have to find the end of the property. */
4252 start_pos = *position;
4253 *position = display_prop_end (it, object, start_pos);
4254 value = Qnil;
4256 /* Stop the scan at that end position--we assume that all
4257 text properties change there. */
4258 it->stop_charpos = position->charpos;
4260 /* Handle `(left-fringe BITMAP [FACE])'
4261 and `(right-fringe BITMAP [FACE])'. */
4262 if (CONSP (spec)
4263 && (EQ (XCAR (spec), Qleft_fringe)
4264 || EQ (XCAR (spec), Qright_fringe))
4265 && CONSP (XCDR (spec)))
4267 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4268 int fringe_bitmap;
4270 if (!FRAME_WINDOW_P (it->f))
4271 /* If we return here, POSITION has been advanced
4272 across the text with this property. */
4273 return 0;
4275 #ifdef HAVE_WINDOW_SYSTEM
4276 value = XCAR (XCDR (spec));
4277 if (!SYMBOLP (value)
4278 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4279 /* If we return here, POSITION has been advanced
4280 across the text with this property. */
4281 return 0;
4283 if (CONSP (XCDR (XCDR (spec))))
4285 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4286 int face_id2 = lookup_derived_face (it->f, face_name,
4287 FRINGE_FACE_ID, 0);
4288 if (face_id2 >= 0)
4289 face_id = face_id2;
4292 /* Save current settings of IT so that we can restore them
4293 when we are finished with the glyph property value. */
4295 save_pos = it->position;
4296 it->position = *position;
4297 push_it (it);
4298 it->position = save_pos;
4300 it->area = TEXT_AREA;
4301 it->what = IT_IMAGE;
4302 it->image_id = -1; /* no image */
4303 it->position = start_pos;
4304 it->object = NILP (object) ? it->w->buffer : object;
4305 it->method = GET_FROM_IMAGE;
4306 it->from_overlay = Qnil;
4307 it->face_id = face_id;
4309 /* Say that we haven't consumed the characters with
4310 `display' property yet. The call to pop_it in
4311 set_iterator_to_next will clean this up. */
4312 *position = start_pos;
4314 if (EQ (XCAR (spec), Qleft_fringe))
4316 it->left_user_fringe_bitmap = fringe_bitmap;
4317 it->left_user_fringe_face_id = face_id;
4319 else
4321 it->right_user_fringe_bitmap = fringe_bitmap;
4322 it->right_user_fringe_face_id = face_id;
4324 #endif /* HAVE_WINDOW_SYSTEM */
4325 return 1;
4328 /* Prepare to handle `((margin left-margin) ...)',
4329 `((margin right-margin) ...)' and `((margin nil) ...)'
4330 prefixes for display specifications. */
4331 location = Qunbound;
4332 if (CONSP (spec) && CONSP (XCAR (spec)))
4334 Lisp_Object tem;
4336 value = XCDR (spec);
4337 if (CONSP (value))
4338 value = XCAR (value);
4340 tem = XCAR (spec);
4341 if (EQ (XCAR (tem), Qmargin)
4342 && (tem = XCDR (tem),
4343 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4344 (NILP (tem)
4345 || EQ (tem, Qleft_margin)
4346 || EQ (tem, Qright_margin))))
4347 location = tem;
4350 if (EQ (location, Qunbound))
4352 location = Qnil;
4353 value = spec;
4356 /* After this point, VALUE is the property after any
4357 margin prefix has been stripped. It must be a string,
4358 an image specification, or `(space ...)'.
4360 LOCATION specifies where to display: `left-margin',
4361 `right-margin' or nil. */
4363 valid_p = (STRINGP (value)
4364 #ifdef HAVE_WINDOW_SYSTEM
4365 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4366 #endif /* not HAVE_WINDOW_SYSTEM */
4367 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4369 if (valid_p && !display_replaced_before_p)
4371 /* Save current settings of IT so that we can restore them
4372 when we are finished with the glyph property value. */
4373 save_pos = it->position;
4374 it->position = *position;
4375 push_it (it);
4376 it->position = save_pos;
4377 it->from_overlay = overlay;
4379 if (NILP (location))
4380 it->area = TEXT_AREA;
4381 else if (EQ (location, Qleft_margin))
4382 it->area = LEFT_MARGIN_AREA;
4383 else
4384 it->area = RIGHT_MARGIN_AREA;
4386 if (STRINGP (value))
4388 it->string = value;
4389 it->multibyte_p = STRING_MULTIBYTE (it->string);
4390 it->current.overlay_string_index = -1;
4391 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4392 it->end_charpos = it->string_nchars = SCHARS (it->string);
4393 it->method = GET_FROM_STRING;
4394 it->stop_charpos = 0;
4395 it->string_from_display_prop_p = 1;
4396 /* Say that we haven't consumed the characters with
4397 `display' property yet. The call to pop_it in
4398 set_iterator_to_next will clean this up. */
4399 if (BUFFERP (object))
4400 *position = start_pos;
4402 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4404 it->method = GET_FROM_STRETCH;
4405 it->object = value;
4406 *position = it->position = start_pos;
4408 #ifdef HAVE_WINDOW_SYSTEM
4409 else
4411 it->what = IT_IMAGE;
4412 it->image_id = lookup_image (it->f, value);
4413 it->position = start_pos;
4414 it->object = NILP (object) ? it->w->buffer : object;
4415 it->method = GET_FROM_IMAGE;
4417 /* Say that we haven't consumed the characters with
4418 `display' property yet. The call to pop_it in
4419 set_iterator_to_next will clean this up. */
4420 *position = start_pos;
4422 #endif /* HAVE_WINDOW_SYSTEM */
4424 return 1;
4427 /* Invalid property or property not supported. Restore
4428 POSITION to what it was before. */
4429 *position = start_pos;
4430 return 0;
4434 /* Check if SPEC is a display sub-property value whose text should be
4435 treated as intangible. */
4437 static int
4438 single_display_spec_intangible_p (prop)
4439 Lisp_Object prop;
4441 /* Skip over `when FORM'. */
4442 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4444 prop = XCDR (prop);
4445 if (!CONSP (prop))
4446 return 0;
4447 prop = XCDR (prop);
4450 if (STRINGP (prop))
4451 return 1;
4453 if (!CONSP (prop))
4454 return 0;
4456 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4457 we don't need to treat text as intangible. */
4458 if (EQ (XCAR (prop), Qmargin))
4460 prop = XCDR (prop);
4461 if (!CONSP (prop))
4462 return 0;
4464 prop = XCDR (prop);
4465 if (!CONSP (prop)
4466 || EQ (XCAR (prop), Qleft_margin)
4467 || EQ (XCAR (prop), Qright_margin))
4468 return 0;
4471 return (CONSP (prop)
4472 && (EQ (XCAR (prop), Qimage)
4473 || EQ (XCAR (prop), Qspace)));
4477 /* Check if PROP is a display property value whose text should be
4478 treated as intangible. */
4481 display_prop_intangible_p (prop)
4482 Lisp_Object prop;
4484 if (CONSP (prop)
4485 && CONSP (XCAR (prop))
4486 && !EQ (Qmargin, XCAR (XCAR (prop))))
4488 /* A list of sub-properties. */
4489 while (CONSP (prop))
4491 if (single_display_spec_intangible_p (XCAR (prop)))
4492 return 1;
4493 prop = XCDR (prop);
4496 else if (VECTORP (prop))
4498 /* A vector of sub-properties. */
4499 int i;
4500 for (i = 0; i < ASIZE (prop); ++i)
4501 if (single_display_spec_intangible_p (AREF (prop, i)))
4502 return 1;
4504 else
4505 return single_display_spec_intangible_p (prop);
4507 return 0;
4511 /* Return 1 if PROP is a display sub-property value containing STRING. */
4513 static int
4514 single_display_spec_string_p (prop, string)
4515 Lisp_Object prop, string;
4517 if (EQ (string, prop))
4518 return 1;
4520 /* Skip over `when FORM'. */
4521 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4523 prop = XCDR (prop);
4524 if (!CONSP (prop))
4525 return 0;
4526 prop = XCDR (prop);
4529 if (CONSP (prop))
4530 /* Skip over `margin LOCATION'. */
4531 if (EQ (XCAR (prop), Qmargin))
4533 prop = XCDR (prop);
4534 if (!CONSP (prop))
4535 return 0;
4537 prop = XCDR (prop);
4538 if (!CONSP (prop))
4539 return 0;
4542 return CONSP (prop) && EQ (XCAR (prop), string);
4546 /* Return 1 if STRING appears in the `display' property PROP. */
4548 static int
4549 display_prop_string_p (prop, string)
4550 Lisp_Object prop, string;
4552 if (CONSP (prop)
4553 && CONSP (XCAR (prop))
4554 && !EQ (Qmargin, XCAR (XCAR (prop))))
4556 /* A list of sub-properties. */
4557 while (CONSP (prop))
4559 if (single_display_spec_string_p (XCAR (prop), string))
4560 return 1;
4561 prop = XCDR (prop);
4564 else if (VECTORP (prop))
4566 /* A vector of sub-properties. */
4567 int i;
4568 for (i = 0; i < ASIZE (prop); ++i)
4569 if (single_display_spec_string_p (AREF (prop, i), string))
4570 return 1;
4572 else
4573 return single_display_spec_string_p (prop, string);
4575 return 0;
4579 /* Determine which buffer position in W's buffer STRING comes from.
4580 AROUND_CHARPOS is an approximate position where it could come from.
4581 Value is the buffer position or 0 if it couldn't be determined.
4583 W's buffer must be current.
4585 This function is necessary because we don't record buffer positions
4586 in glyphs generated from strings (to keep struct glyph small).
4587 This function may only use code that doesn't eval because it is
4588 called asynchronously from note_mouse_highlight. */
4591 string_buffer_position (w, string, around_charpos)
4592 struct window *w;
4593 Lisp_Object string;
4594 int around_charpos;
4596 Lisp_Object limit, prop, pos;
4597 const int MAX_DISTANCE = 1000;
4598 int found = 0;
4600 pos = make_number (around_charpos);
4601 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4602 while (!found && !EQ (pos, limit))
4604 prop = Fget_char_property (pos, Qdisplay, Qnil);
4605 if (!NILP (prop) && display_prop_string_p (prop, string))
4606 found = 1;
4607 else
4608 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4611 if (!found)
4613 pos = make_number (around_charpos);
4614 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4615 while (!found && !EQ (pos, limit))
4617 prop = Fget_char_property (pos, Qdisplay, Qnil);
4618 if (!NILP (prop) && display_prop_string_p (prop, string))
4619 found = 1;
4620 else
4621 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4622 limit);
4626 return found ? XINT (pos) : 0;
4631 /***********************************************************************
4632 `composition' property
4633 ***********************************************************************/
4635 /* Set up iterator IT from `composition' property at its current
4636 position. Called from handle_stop. */
4638 static enum prop_handled
4639 handle_composition_prop (it)
4640 struct it *it;
4642 Lisp_Object prop, string;
4643 EMACS_INT pos, pos_byte, start, end;
4645 if (STRINGP (it->string))
4647 unsigned char *s;
4649 pos = IT_STRING_CHARPOS (*it);
4650 pos_byte = IT_STRING_BYTEPOS (*it);
4651 string = it->string;
4652 s = SDATA (string) + pos_byte;
4653 it->c = STRING_CHAR (s);
4655 else
4657 pos = IT_CHARPOS (*it);
4658 pos_byte = IT_BYTEPOS (*it);
4659 string = Qnil;
4660 it->c = FETCH_CHAR (pos_byte);
4663 /* If there's a valid composition and point is not inside of the
4664 composition (in the case that the composition is from the current
4665 buffer), draw a glyph composed from the composition components. */
4666 if (find_composition (pos, -1, &start, &end, &prop, string)
4667 && COMPOSITION_VALID_P (start, end, prop)
4668 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4670 if (start != pos)
4672 if (STRINGP (it->string))
4673 pos_byte = string_char_to_byte (it->string, start);
4674 else
4675 pos_byte = CHAR_TO_BYTE (start);
4677 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4678 prop, string);
4680 if (it->cmp_it.id >= 0)
4682 it->cmp_it.ch = -1;
4683 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4684 it->cmp_it.nglyphs = -1;
4688 return HANDLED_NORMALLY;
4693 /***********************************************************************
4694 Overlay strings
4695 ***********************************************************************/
4697 /* The following structure is used to record overlay strings for
4698 later sorting in load_overlay_strings. */
4700 struct overlay_entry
4702 Lisp_Object overlay;
4703 Lisp_Object string;
4704 int priority;
4705 int after_string_p;
4709 /* Set up iterator IT from overlay strings at its current position.
4710 Called from handle_stop. */
4712 static enum prop_handled
4713 handle_overlay_change (it)
4714 struct it *it;
4716 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4717 return HANDLED_RECOMPUTE_PROPS;
4718 else
4719 return HANDLED_NORMALLY;
4723 /* Set up the next overlay string for delivery by IT, if there is an
4724 overlay string to deliver. Called by set_iterator_to_next when the
4725 end of the current overlay string is reached. If there are more
4726 overlay strings to display, IT->string and
4727 IT->current.overlay_string_index are set appropriately here.
4728 Otherwise IT->string is set to nil. */
4730 static void
4731 next_overlay_string (it)
4732 struct it *it;
4734 ++it->current.overlay_string_index;
4735 if (it->current.overlay_string_index == it->n_overlay_strings)
4737 /* No more overlay strings. Restore IT's settings to what
4738 they were before overlay strings were processed, and
4739 continue to deliver from current_buffer. */
4741 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4742 pop_it (it);
4743 xassert (it->sp > 0
4744 || (NILP (it->string)
4745 && it->method == GET_FROM_BUFFER
4746 && it->stop_charpos >= BEGV
4747 && it->stop_charpos <= it->end_charpos));
4748 it->current.overlay_string_index = -1;
4749 it->n_overlay_strings = 0;
4751 /* If we're at the end of the buffer, record that we have
4752 processed the overlay strings there already, so that
4753 next_element_from_buffer doesn't try it again. */
4754 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4755 it->overlay_strings_at_end_processed_p = 1;
4757 else
4759 /* There are more overlay strings to process. If
4760 IT->current.overlay_string_index has advanced to a position
4761 where we must load IT->overlay_strings with more strings, do
4762 it. */
4763 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4765 if (it->current.overlay_string_index && i == 0)
4766 load_overlay_strings (it, 0);
4768 /* Initialize IT to deliver display elements from the overlay
4769 string. */
4770 it->string = it->overlay_strings[i];
4771 it->multibyte_p = STRING_MULTIBYTE (it->string);
4772 SET_TEXT_POS (it->current.string_pos, 0, 0);
4773 it->method = GET_FROM_STRING;
4774 it->stop_charpos = 0;
4775 if (it->cmp_it.stop_pos >= 0)
4776 it->cmp_it.stop_pos = 0;
4779 CHECK_IT (it);
4783 /* Compare two overlay_entry structures E1 and E2. Used as a
4784 comparison function for qsort in load_overlay_strings. Overlay
4785 strings for the same position are sorted so that
4787 1. All after-strings come in front of before-strings, except
4788 when they come from the same overlay.
4790 2. Within after-strings, strings are sorted so that overlay strings
4791 from overlays with higher priorities come first.
4793 2. Within before-strings, strings are sorted so that overlay
4794 strings from overlays with higher priorities come last.
4796 Value is analogous to strcmp. */
4799 static int
4800 compare_overlay_entries (e1, e2)
4801 void *e1, *e2;
4803 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4804 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4805 int result;
4807 if (entry1->after_string_p != entry2->after_string_p)
4809 /* Let after-strings appear in front of before-strings if
4810 they come from different overlays. */
4811 if (EQ (entry1->overlay, entry2->overlay))
4812 result = entry1->after_string_p ? 1 : -1;
4813 else
4814 result = entry1->after_string_p ? -1 : 1;
4816 else if (entry1->after_string_p)
4817 /* After-strings sorted in order of decreasing priority. */
4818 result = entry2->priority - entry1->priority;
4819 else
4820 /* Before-strings sorted in order of increasing priority. */
4821 result = entry1->priority - entry2->priority;
4823 return result;
4827 /* Load the vector IT->overlay_strings with overlay strings from IT's
4828 current buffer position, or from CHARPOS if that is > 0. Set
4829 IT->n_overlays to the total number of overlay strings found.
4831 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4832 a time. On entry into load_overlay_strings,
4833 IT->current.overlay_string_index gives the number of overlay
4834 strings that have already been loaded by previous calls to this
4835 function.
4837 IT->add_overlay_start contains an additional overlay start
4838 position to consider for taking overlay strings from, if non-zero.
4839 This position comes into play when the overlay has an `invisible'
4840 property, and both before and after-strings. When we've skipped to
4841 the end of the overlay, because of its `invisible' property, we
4842 nevertheless want its before-string to appear.
4843 IT->add_overlay_start will contain the overlay start position
4844 in this case.
4846 Overlay strings are sorted so that after-string strings come in
4847 front of before-string strings. Within before and after-strings,
4848 strings are sorted by overlay priority. See also function
4849 compare_overlay_entries. */
4851 static void
4852 load_overlay_strings (it, charpos)
4853 struct it *it;
4854 int charpos;
4856 extern Lisp_Object Qwindow, Qpriority;
4857 Lisp_Object overlay, window, str, invisible;
4858 struct Lisp_Overlay *ov;
4859 int start, end;
4860 int size = 20;
4861 int n = 0, i, j, invis_p;
4862 struct overlay_entry *entries
4863 = (struct overlay_entry *) alloca (size * sizeof *entries);
4865 if (charpos <= 0)
4866 charpos = IT_CHARPOS (*it);
4868 /* Append the overlay string STRING of overlay OVERLAY to vector
4869 `entries' which has size `size' and currently contains `n'
4870 elements. AFTER_P non-zero means STRING is an after-string of
4871 OVERLAY. */
4872 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4873 do \
4875 Lisp_Object priority; \
4877 if (n == size) \
4879 int new_size = 2 * size; \
4880 struct overlay_entry *old = entries; \
4881 entries = \
4882 (struct overlay_entry *) alloca (new_size \
4883 * sizeof *entries); \
4884 bcopy (old, entries, size * sizeof *entries); \
4885 size = new_size; \
4888 entries[n].string = (STRING); \
4889 entries[n].overlay = (OVERLAY); \
4890 priority = Foverlay_get ((OVERLAY), Qpriority); \
4891 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4892 entries[n].after_string_p = (AFTER_P); \
4893 ++n; \
4895 while (0)
4897 /* Process overlay before the overlay center. */
4898 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4900 XSETMISC (overlay, ov);
4901 xassert (OVERLAYP (overlay));
4902 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4903 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4905 if (end < charpos)
4906 break;
4908 /* Skip this overlay if it doesn't start or end at IT's current
4909 position. */
4910 if (end != charpos && start != charpos)
4911 continue;
4913 /* Skip this overlay if it doesn't apply to IT->w. */
4914 window = Foverlay_get (overlay, Qwindow);
4915 if (WINDOWP (window) && XWINDOW (window) != it->w)
4916 continue;
4918 /* If the text ``under'' the overlay is invisible, both before-
4919 and after-strings from this overlay are visible; start and
4920 end position are indistinguishable. */
4921 invisible = Foverlay_get (overlay, Qinvisible);
4922 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4924 /* If overlay has a non-empty before-string, record it. */
4925 if ((start == charpos || (end == charpos && invis_p))
4926 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4927 && SCHARS (str))
4928 RECORD_OVERLAY_STRING (overlay, str, 0);
4930 /* If overlay has a non-empty after-string, record it. */
4931 if ((end == charpos || (start == charpos && invis_p))
4932 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4933 && SCHARS (str))
4934 RECORD_OVERLAY_STRING (overlay, str, 1);
4937 /* Process overlays after the overlay center. */
4938 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4940 XSETMISC (overlay, ov);
4941 xassert (OVERLAYP (overlay));
4942 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4943 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4945 if (start > charpos)
4946 break;
4948 /* Skip this overlay if it doesn't start or end at IT's current
4949 position. */
4950 if (end != charpos && start != charpos)
4951 continue;
4953 /* Skip this overlay if it doesn't apply to IT->w. */
4954 window = Foverlay_get (overlay, Qwindow);
4955 if (WINDOWP (window) && XWINDOW (window) != it->w)
4956 continue;
4958 /* If the text ``under'' the overlay is invisible, it has a zero
4959 dimension, and both before- and after-strings apply. */
4960 invisible = Foverlay_get (overlay, Qinvisible);
4961 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4963 /* If overlay has a non-empty before-string, record it. */
4964 if ((start == charpos || (end == charpos && invis_p))
4965 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4966 && SCHARS (str))
4967 RECORD_OVERLAY_STRING (overlay, str, 0);
4969 /* If overlay has a non-empty after-string, record it. */
4970 if ((end == charpos || (start == charpos && invis_p))
4971 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4972 && SCHARS (str))
4973 RECORD_OVERLAY_STRING (overlay, str, 1);
4976 #undef RECORD_OVERLAY_STRING
4978 /* Sort entries. */
4979 if (n > 1)
4980 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4982 /* Record the total number of strings to process. */
4983 it->n_overlay_strings = n;
4985 /* IT->current.overlay_string_index is the number of overlay strings
4986 that have already been consumed by IT. Copy some of the
4987 remaining overlay strings to IT->overlay_strings. */
4988 i = 0;
4989 j = it->current.overlay_string_index;
4990 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4992 it->overlay_strings[i] = entries[j].string;
4993 it->string_overlays[i++] = entries[j++].overlay;
4996 CHECK_IT (it);
5000 /* Get the first chunk of overlay strings at IT's current buffer
5001 position, or at CHARPOS if that is > 0. Value is non-zero if at
5002 least one overlay string was found. */
5004 static int
5005 get_overlay_strings_1 (it, charpos, compute_stop_p)
5006 struct it *it;
5007 int charpos;
5008 int compute_stop_p;
5010 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5011 process. This fills IT->overlay_strings with strings, and sets
5012 IT->n_overlay_strings to the total number of strings to process.
5013 IT->pos.overlay_string_index has to be set temporarily to zero
5014 because load_overlay_strings needs this; it must be set to -1
5015 when no overlay strings are found because a zero value would
5016 indicate a position in the first overlay string. */
5017 it->current.overlay_string_index = 0;
5018 load_overlay_strings (it, charpos);
5020 /* If we found overlay strings, set up IT to deliver display
5021 elements from the first one. Otherwise set up IT to deliver
5022 from current_buffer. */
5023 if (it->n_overlay_strings)
5025 /* Make sure we know settings in current_buffer, so that we can
5026 restore meaningful values when we're done with the overlay
5027 strings. */
5028 if (compute_stop_p)
5029 compute_stop_pos (it);
5030 xassert (it->face_id >= 0);
5032 /* Save IT's settings. They are restored after all overlay
5033 strings have been processed. */
5034 xassert (!compute_stop_p || it->sp == 0);
5036 /* When called from handle_stop, there might be an empty display
5037 string loaded. In that case, don't bother saving it. */
5038 if (!STRINGP (it->string) || SCHARS (it->string))
5039 push_it (it);
5041 /* Set up IT to deliver display elements from the first overlay
5042 string. */
5043 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5044 it->string = it->overlay_strings[0];
5045 it->from_overlay = Qnil;
5046 it->stop_charpos = 0;
5047 xassert (STRINGP (it->string));
5048 it->end_charpos = SCHARS (it->string);
5049 it->multibyte_p = STRING_MULTIBYTE (it->string);
5050 it->method = GET_FROM_STRING;
5051 return 1;
5054 it->current.overlay_string_index = -1;
5055 return 0;
5058 static int
5059 get_overlay_strings (it, charpos)
5060 struct it *it;
5061 int charpos;
5063 it->string = Qnil;
5064 it->method = GET_FROM_BUFFER;
5066 (void) get_overlay_strings_1 (it, charpos, 1);
5068 CHECK_IT (it);
5070 /* Value is non-zero if we found at least one overlay string. */
5071 return STRINGP (it->string);
5076 /***********************************************************************
5077 Saving and restoring state
5078 ***********************************************************************/
5080 /* Save current settings of IT on IT->stack. Called, for example,
5081 before setting up IT for an overlay string, to be able to restore
5082 IT's settings to what they were after the overlay string has been
5083 processed. */
5085 static void
5086 push_it (it)
5087 struct it *it;
5089 struct iterator_stack_entry *p;
5091 xassert (it->sp < IT_STACK_SIZE);
5092 p = it->stack + it->sp;
5094 p->stop_charpos = it->stop_charpos;
5095 p->cmp_it = it->cmp_it;
5096 xassert (it->face_id >= 0);
5097 p->face_id = it->face_id;
5098 p->string = it->string;
5099 p->method = it->method;
5100 p->from_overlay = it->from_overlay;
5101 switch (p->method)
5103 case GET_FROM_IMAGE:
5104 p->u.image.object = it->object;
5105 p->u.image.image_id = it->image_id;
5106 p->u.image.slice = it->slice;
5107 break;
5108 case GET_FROM_STRETCH:
5109 p->u.stretch.object = it->object;
5110 break;
5112 p->position = it->position;
5113 p->current = it->current;
5114 p->end_charpos = it->end_charpos;
5115 p->string_nchars = it->string_nchars;
5116 p->area = it->area;
5117 p->multibyte_p = it->multibyte_p;
5118 p->avoid_cursor_p = it->avoid_cursor_p;
5119 p->space_width = it->space_width;
5120 p->font_height = it->font_height;
5121 p->voffset = it->voffset;
5122 p->string_from_display_prop_p = it->string_from_display_prop_p;
5123 p->display_ellipsis_p = 0;
5124 p->line_wrap = it->line_wrap;
5125 ++it->sp;
5129 /* Restore IT's settings from IT->stack. Called, for example, when no
5130 more overlay strings must be processed, and we return to delivering
5131 display elements from a buffer, or when the end of a string from a
5132 `display' property is reached and we return to delivering display
5133 elements from an overlay string, or from a buffer. */
5135 static void
5136 pop_it (it)
5137 struct it *it;
5139 struct iterator_stack_entry *p;
5141 xassert (it->sp > 0);
5142 --it->sp;
5143 p = it->stack + it->sp;
5144 it->stop_charpos = p->stop_charpos;
5145 it->cmp_it = p->cmp_it;
5146 it->face_id = p->face_id;
5147 it->current = p->current;
5148 it->position = p->position;
5149 it->string = p->string;
5150 it->from_overlay = p->from_overlay;
5151 if (NILP (it->string))
5152 SET_TEXT_POS (it->current.string_pos, -1, -1);
5153 it->method = p->method;
5154 switch (it->method)
5156 case GET_FROM_IMAGE:
5157 it->image_id = p->u.image.image_id;
5158 it->object = p->u.image.object;
5159 it->slice = p->u.image.slice;
5160 break;
5161 case GET_FROM_STRETCH:
5162 it->object = p->u.comp.object;
5163 break;
5164 case GET_FROM_BUFFER:
5165 it->object = it->w->buffer;
5166 break;
5167 case GET_FROM_STRING:
5168 it->object = it->string;
5169 break;
5170 case GET_FROM_DISPLAY_VECTOR:
5171 if (it->s)
5172 it->method = GET_FROM_C_STRING;
5173 else if (STRINGP (it->string))
5174 it->method = GET_FROM_STRING;
5175 else
5177 it->method = GET_FROM_BUFFER;
5178 it->object = it->w->buffer;
5181 it->end_charpos = p->end_charpos;
5182 it->string_nchars = p->string_nchars;
5183 it->area = p->area;
5184 it->multibyte_p = p->multibyte_p;
5185 it->avoid_cursor_p = p->avoid_cursor_p;
5186 it->space_width = p->space_width;
5187 it->font_height = p->font_height;
5188 it->voffset = p->voffset;
5189 it->string_from_display_prop_p = p->string_from_display_prop_p;
5190 it->line_wrap = p->line_wrap;
5195 /***********************************************************************
5196 Moving over lines
5197 ***********************************************************************/
5199 /* Set IT's current position to the previous line start. */
5201 static void
5202 back_to_previous_line_start (it)
5203 struct it *it;
5205 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5206 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5210 /* Move IT to the next line start.
5212 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5213 we skipped over part of the text (as opposed to moving the iterator
5214 continuously over the text). Otherwise, don't change the value
5215 of *SKIPPED_P.
5217 Newlines may come from buffer text, overlay strings, or strings
5218 displayed via the `display' property. That's the reason we can't
5219 simply use find_next_newline_no_quit.
5221 Note that this function may not skip over invisible text that is so
5222 because of text properties and immediately follows a newline. If
5223 it would, function reseat_at_next_visible_line_start, when called
5224 from set_iterator_to_next, would effectively make invisible
5225 characters following a newline part of the wrong glyph row, which
5226 leads to wrong cursor motion. */
5228 static int
5229 forward_to_next_line_start (it, skipped_p)
5230 struct it *it;
5231 int *skipped_p;
5233 int old_selective, newline_found_p, n;
5234 const int MAX_NEWLINE_DISTANCE = 500;
5236 /* If already on a newline, just consume it to avoid unintended
5237 skipping over invisible text below. */
5238 if (it->what == IT_CHARACTER
5239 && it->c == '\n'
5240 && CHARPOS (it->position) == IT_CHARPOS (*it))
5242 set_iterator_to_next (it, 0);
5243 it->c = 0;
5244 return 1;
5247 /* Don't handle selective display in the following. It's (a)
5248 unnecessary because it's done by the caller, and (b) leads to an
5249 infinite recursion because next_element_from_ellipsis indirectly
5250 calls this function. */
5251 old_selective = it->selective;
5252 it->selective = 0;
5254 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5255 from buffer text. */
5256 for (n = newline_found_p = 0;
5257 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5258 n += STRINGP (it->string) ? 0 : 1)
5260 if (!get_next_display_element (it))
5261 return 0;
5262 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5263 set_iterator_to_next (it, 0);
5266 /* If we didn't find a newline near enough, see if we can use a
5267 short-cut. */
5268 if (!newline_found_p)
5270 int start = IT_CHARPOS (*it);
5271 int limit = find_next_newline_no_quit (start, 1);
5272 Lisp_Object pos;
5274 xassert (!STRINGP (it->string));
5276 /* If there isn't any `display' property in sight, and no
5277 overlays, we can just use the position of the newline in
5278 buffer text. */
5279 if (it->stop_charpos >= limit
5280 || ((pos = Fnext_single_property_change (make_number (start),
5281 Qdisplay,
5282 Qnil, make_number (limit)),
5283 NILP (pos))
5284 && next_overlay_change (start) == ZV))
5286 IT_CHARPOS (*it) = limit;
5287 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5288 *skipped_p = newline_found_p = 1;
5290 else
5292 while (get_next_display_element (it)
5293 && !newline_found_p)
5295 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5296 set_iterator_to_next (it, 0);
5301 it->selective = old_selective;
5302 return newline_found_p;
5306 /* Set IT's current position to the previous visible line start. Skip
5307 invisible text that is so either due to text properties or due to
5308 selective display. Caution: this does not change IT->current_x and
5309 IT->hpos. */
5311 static void
5312 back_to_previous_visible_line_start (it)
5313 struct it *it;
5315 while (IT_CHARPOS (*it) > BEGV)
5317 back_to_previous_line_start (it);
5319 if (IT_CHARPOS (*it) <= BEGV)
5320 break;
5322 /* If selective > 0, then lines indented more than that values
5323 are invisible. */
5324 if (it->selective > 0
5325 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5326 (double) it->selective)) /* iftc */
5327 continue;
5329 /* Check the newline before point for invisibility. */
5331 Lisp_Object prop;
5332 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5333 Qinvisible, it->window);
5334 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5335 continue;
5338 if (IT_CHARPOS (*it) <= BEGV)
5339 break;
5342 struct it it2;
5343 int pos;
5344 EMACS_INT beg, end;
5345 Lisp_Object val, overlay;
5347 /* If newline is part of a composition, continue from start of composition */
5348 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5349 && beg < IT_CHARPOS (*it))
5350 goto replaced;
5352 /* If newline is replaced by a display property, find start of overlay
5353 or interval and continue search from that point. */
5354 it2 = *it;
5355 pos = --IT_CHARPOS (it2);
5356 --IT_BYTEPOS (it2);
5357 it2.sp = 0;
5358 it2.string_from_display_prop_p = 0;
5359 if (handle_display_prop (&it2) == HANDLED_RETURN
5360 && !NILP (val = get_char_property_and_overlay
5361 (make_number (pos), Qdisplay, Qnil, &overlay))
5362 && (OVERLAYP (overlay)
5363 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5364 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5365 goto replaced;
5367 /* Newline is not replaced by anything -- so we are done. */
5368 break;
5370 replaced:
5371 if (beg < BEGV)
5372 beg = BEGV;
5373 IT_CHARPOS (*it) = beg;
5374 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5378 it->continuation_lines_width = 0;
5380 xassert (IT_CHARPOS (*it) >= BEGV);
5381 xassert (IT_CHARPOS (*it) == BEGV
5382 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5383 CHECK_IT (it);
5387 /* Reseat iterator IT at the previous visible line start. Skip
5388 invisible text that is so either due to text properties or due to
5389 selective display. At the end, update IT's overlay information,
5390 face information etc. */
5392 void
5393 reseat_at_previous_visible_line_start (it)
5394 struct it *it;
5396 back_to_previous_visible_line_start (it);
5397 reseat (it, it->current.pos, 1);
5398 CHECK_IT (it);
5402 /* Reseat iterator IT on the next visible line start in the current
5403 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5404 preceding the line start. Skip over invisible text that is so
5405 because of selective display. Compute faces, overlays etc at the
5406 new position. Note that this function does not skip over text that
5407 is invisible because of text properties. */
5409 static void
5410 reseat_at_next_visible_line_start (it, on_newline_p)
5411 struct it *it;
5412 int on_newline_p;
5414 int newline_found_p, skipped_p = 0;
5416 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5418 /* Skip over lines that are invisible because they are indented
5419 more than the value of IT->selective. */
5420 if (it->selective > 0)
5421 while (IT_CHARPOS (*it) < ZV
5422 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5423 (double) it->selective)) /* iftc */
5425 xassert (IT_BYTEPOS (*it) == BEGV
5426 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5427 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5430 /* Position on the newline if that's what's requested. */
5431 if (on_newline_p && newline_found_p)
5433 if (STRINGP (it->string))
5435 if (IT_STRING_CHARPOS (*it) > 0)
5437 --IT_STRING_CHARPOS (*it);
5438 --IT_STRING_BYTEPOS (*it);
5441 else if (IT_CHARPOS (*it) > BEGV)
5443 --IT_CHARPOS (*it);
5444 --IT_BYTEPOS (*it);
5445 reseat (it, it->current.pos, 0);
5448 else if (skipped_p)
5449 reseat (it, it->current.pos, 0);
5451 CHECK_IT (it);
5456 /***********************************************************************
5457 Changing an iterator's position
5458 ***********************************************************************/
5460 /* Change IT's current position to POS in current_buffer. If FORCE_P
5461 is non-zero, always check for text properties at the new position.
5462 Otherwise, text properties are only looked up if POS >=
5463 IT->check_charpos of a property. */
5465 static void
5466 reseat (it, pos, force_p)
5467 struct it *it;
5468 struct text_pos pos;
5469 int force_p;
5471 int original_pos = IT_CHARPOS (*it);
5473 reseat_1 (it, pos, 0);
5475 /* Determine where to check text properties. Avoid doing it
5476 where possible because text property lookup is very expensive. */
5477 if (force_p
5478 || CHARPOS (pos) > it->stop_charpos
5479 || CHARPOS (pos) < original_pos)
5480 handle_stop (it);
5482 CHECK_IT (it);
5486 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5487 IT->stop_pos to POS, also. */
5489 static void
5490 reseat_1 (it, pos, set_stop_p)
5491 struct it *it;
5492 struct text_pos pos;
5493 int set_stop_p;
5495 /* Don't call this function when scanning a C string. */
5496 xassert (it->s == NULL);
5498 /* POS must be a reasonable value. */
5499 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5501 it->current.pos = it->position = pos;
5502 it->end_charpos = ZV;
5503 it->dpvec = NULL;
5504 it->current.dpvec_index = -1;
5505 it->current.overlay_string_index = -1;
5506 IT_STRING_CHARPOS (*it) = -1;
5507 IT_STRING_BYTEPOS (*it) = -1;
5508 it->string = Qnil;
5509 it->string_from_display_prop_p = 0;
5510 it->method = GET_FROM_BUFFER;
5511 it->object = it->w->buffer;
5512 it->area = TEXT_AREA;
5513 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5514 it->sp = 0;
5515 it->string_from_display_prop_p = 0;
5516 it->face_before_selective_p = 0;
5518 if (set_stop_p)
5519 it->stop_charpos = CHARPOS (pos);
5523 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5524 If S is non-null, it is a C string to iterate over. Otherwise,
5525 STRING gives a Lisp string to iterate over.
5527 If PRECISION > 0, don't return more then PRECISION number of
5528 characters from the string.
5530 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5531 characters have been returned. FIELD_WIDTH < 0 means an infinite
5532 field width.
5534 MULTIBYTE = 0 means disable processing of multibyte characters,
5535 MULTIBYTE > 0 means enable it,
5536 MULTIBYTE < 0 means use IT->multibyte_p.
5538 IT must be initialized via a prior call to init_iterator before
5539 calling this function. */
5541 static void
5542 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5543 struct it *it;
5544 unsigned char *s;
5545 Lisp_Object string;
5546 int charpos;
5547 int precision, field_width, multibyte;
5549 /* No region in strings. */
5550 it->region_beg_charpos = it->region_end_charpos = -1;
5552 /* No text property checks performed by default, but see below. */
5553 it->stop_charpos = -1;
5555 /* Set iterator position and end position. */
5556 bzero (&it->current, sizeof it->current);
5557 it->current.overlay_string_index = -1;
5558 it->current.dpvec_index = -1;
5559 xassert (charpos >= 0);
5561 /* If STRING is specified, use its multibyteness, otherwise use the
5562 setting of MULTIBYTE, if specified. */
5563 if (multibyte >= 0)
5564 it->multibyte_p = multibyte > 0;
5566 if (s == NULL)
5568 xassert (STRINGP (string));
5569 it->string = string;
5570 it->s = NULL;
5571 it->end_charpos = it->string_nchars = SCHARS (string);
5572 it->method = GET_FROM_STRING;
5573 it->current.string_pos = string_pos (charpos, string);
5575 else
5577 it->s = s;
5578 it->string = Qnil;
5580 /* Note that we use IT->current.pos, not it->current.string_pos,
5581 for displaying C strings. */
5582 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5583 if (it->multibyte_p)
5585 it->current.pos = c_string_pos (charpos, s, 1);
5586 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5588 else
5590 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5591 it->end_charpos = it->string_nchars = strlen (s);
5594 it->method = GET_FROM_C_STRING;
5597 /* PRECISION > 0 means don't return more than PRECISION characters
5598 from the string. */
5599 if (precision > 0 && it->end_charpos - charpos > precision)
5600 it->end_charpos = it->string_nchars = charpos + precision;
5602 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5603 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5604 FIELD_WIDTH < 0 means infinite field width. This is useful for
5605 padding with `-' at the end of a mode line. */
5606 if (field_width < 0)
5607 field_width = INFINITY;
5608 if (field_width > it->end_charpos - charpos)
5609 it->end_charpos = charpos + field_width;
5611 /* Use the standard display table for displaying strings. */
5612 if (DISP_TABLE_P (Vstandard_display_table))
5613 it->dp = XCHAR_TABLE (Vstandard_display_table);
5615 it->stop_charpos = charpos;
5616 CHECK_IT (it);
5621 /***********************************************************************
5622 Iteration
5623 ***********************************************************************/
5625 /* Map enum it_method value to corresponding next_element_from_* function. */
5627 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5629 next_element_from_buffer,
5630 next_element_from_display_vector,
5631 next_element_from_string,
5632 next_element_from_c_string,
5633 next_element_from_image,
5634 next_element_from_stretch
5637 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5640 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5641 (possibly with the following characters). */
5643 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS) \
5644 ((IT)->cmp_it.id >= 0 \
5645 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5646 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5647 (IT)->end_charpos, (IT)->w, \
5648 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5649 (IT)->string)))
5652 /* Load IT's display element fields with information about the next
5653 display element from the current position of IT. Value is zero if
5654 end of buffer (or C string) is reached. */
5656 static struct frame *last_escape_glyph_frame = NULL;
5657 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5658 static int last_escape_glyph_merged_face_id = 0;
5661 get_next_display_element (it)
5662 struct it *it;
5664 /* Non-zero means that we found a display element. Zero means that
5665 we hit the end of what we iterate over. Performance note: the
5666 function pointer `method' used here turns out to be faster than
5667 using a sequence of if-statements. */
5668 int success_p;
5670 get_next:
5671 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5673 if (it->what == IT_CHARACTER)
5675 /* Map via display table or translate control characters.
5676 IT->c, IT->len etc. have been set to the next character by
5677 the function call above. If we have a display table, and it
5678 contains an entry for IT->c, translate it. Don't do this if
5679 IT->c itself comes from a display table, otherwise we could
5680 end up in an infinite recursion. (An alternative could be to
5681 count the recursion depth of this function and signal an
5682 error when a certain maximum depth is reached.) Is it worth
5683 it? */
5684 if (success_p && it->dpvec == NULL)
5686 Lisp_Object dv;
5687 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5688 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5689 nbsp_or_shy = char_is_other;
5690 int decoded = it->c;
5692 if (it->dp
5693 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5694 VECTORP (dv)))
5696 struct Lisp_Vector *v = XVECTOR (dv);
5698 /* Return the first character from the display table
5699 entry, if not empty. If empty, don't display the
5700 current character. */
5701 if (v->size)
5703 it->dpvec_char_len = it->len;
5704 it->dpvec = v->contents;
5705 it->dpend = v->contents + v->size;
5706 it->current.dpvec_index = 0;
5707 it->dpvec_face_id = -1;
5708 it->saved_face_id = it->face_id;
5709 it->method = GET_FROM_DISPLAY_VECTOR;
5710 it->ellipsis_p = 0;
5712 else
5714 set_iterator_to_next (it, 0);
5716 goto get_next;
5719 if (unibyte_display_via_language_environment
5720 && !ASCII_CHAR_P (it->c))
5721 decoded = DECODE_CHAR (unibyte, it->c);
5723 if (it->c >= 0x80 && ! NILP (Vnobreak_char_display))
5725 if (it->multibyte_p)
5726 nbsp_or_shy = (it->c == 0xA0 ? char_is_nbsp
5727 : it->c == 0xAD ? char_is_soft_hyphen
5728 : char_is_other);
5729 else if (unibyte_display_via_language_environment)
5730 nbsp_or_shy = (decoded == 0xA0 ? char_is_nbsp
5731 : decoded == 0xAD ? char_is_soft_hyphen
5732 : char_is_other);
5735 /* Translate control characters into `\003' or `^C' form.
5736 Control characters coming from a display table entry are
5737 currently not translated because we use IT->dpvec to hold
5738 the translation. This could easily be changed but I
5739 don't believe that it is worth doing.
5741 If it->multibyte_p is nonzero, non-printable non-ASCII
5742 characters are also translated to octal form.
5744 If it->multibyte_p is zero, eight-bit characters that
5745 don't have corresponding multibyte char code are also
5746 translated to octal form. */
5747 if ((it->c < ' '
5748 ? (it->area != TEXT_AREA
5749 /* In mode line, treat \n, \t like other crl chars. */
5750 || (it->c != '\t'
5751 && it->glyph_row
5752 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5753 || (it->c != '\n' && it->c != '\t'))
5754 : (nbsp_or_shy
5755 || (it->multibyte_p
5756 ? ! CHAR_PRINTABLE_P (it->c)
5757 : (! unibyte_display_via_language_environment
5758 ? it->c >= 0x80
5759 : (decoded >= 0x80 && decoded < 0xA0))))))
5761 /* IT->c is a control character which must be displayed
5762 either as '\003' or as `^C' where the '\\' and '^'
5763 can be defined in the display table. Fill
5764 IT->ctl_chars with glyphs for what we have to
5765 display. Then, set IT->dpvec to these glyphs. */
5766 Lisp_Object gc;
5767 int ctl_len;
5768 int face_id, lface_id = 0 ;
5769 int escape_glyph;
5771 /* Handle control characters with ^. */
5773 if (it->c < 128 && it->ctl_arrow_p)
5775 int g;
5777 g = '^'; /* default glyph for Control */
5778 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5779 if (it->dp
5780 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5781 && GLYPH_CODE_CHAR_VALID_P (gc))
5783 g = GLYPH_CODE_CHAR (gc);
5784 lface_id = GLYPH_CODE_FACE (gc);
5786 if (lface_id)
5788 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5790 else if (it->f == last_escape_glyph_frame
5791 && it->face_id == last_escape_glyph_face_id)
5793 face_id = last_escape_glyph_merged_face_id;
5795 else
5797 /* Merge the escape-glyph face into the current face. */
5798 face_id = merge_faces (it->f, Qescape_glyph, 0,
5799 it->face_id);
5800 last_escape_glyph_frame = it->f;
5801 last_escape_glyph_face_id = it->face_id;
5802 last_escape_glyph_merged_face_id = face_id;
5805 XSETINT (it->ctl_chars[0], g);
5806 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5807 ctl_len = 2;
5808 goto display_control;
5811 /* Handle non-break space in the mode where it only gets
5812 highlighting. */
5814 if (EQ (Vnobreak_char_display, Qt)
5815 && nbsp_or_shy == char_is_nbsp)
5817 /* Merge the no-break-space face into the current face. */
5818 face_id = merge_faces (it->f, Qnobreak_space, 0,
5819 it->face_id);
5821 it->c = ' ';
5822 XSETINT (it->ctl_chars[0], ' ');
5823 ctl_len = 1;
5824 goto display_control;
5827 /* Handle sequences that start with the "escape glyph". */
5829 /* the default escape glyph is \. */
5830 escape_glyph = '\\';
5832 if (it->dp
5833 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5834 && GLYPH_CODE_CHAR_VALID_P (gc))
5836 escape_glyph = GLYPH_CODE_CHAR (gc);
5837 lface_id = GLYPH_CODE_FACE (gc);
5839 if (lface_id)
5841 /* The display table specified a face.
5842 Merge it into face_id and also into escape_glyph. */
5843 face_id = merge_faces (it->f, Qt, lface_id,
5844 it->face_id);
5846 else if (it->f == last_escape_glyph_frame
5847 && it->face_id == last_escape_glyph_face_id)
5849 face_id = last_escape_glyph_merged_face_id;
5851 else
5853 /* Merge the escape-glyph face into the current face. */
5854 face_id = merge_faces (it->f, Qescape_glyph, 0,
5855 it->face_id);
5856 last_escape_glyph_frame = it->f;
5857 last_escape_glyph_face_id = it->face_id;
5858 last_escape_glyph_merged_face_id = face_id;
5861 /* Handle soft hyphens in the mode where they only get
5862 highlighting. */
5864 if (EQ (Vnobreak_char_display, Qt)
5865 && nbsp_or_shy == char_is_soft_hyphen)
5867 it->c = '-';
5868 XSETINT (it->ctl_chars[0], '-');
5869 ctl_len = 1;
5870 goto display_control;
5873 /* Handle non-break space and soft hyphen
5874 with the escape glyph. */
5876 if (nbsp_or_shy)
5878 XSETINT (it->ctl_chars[0], escape_glyph);
5879 it->c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5880 XSETINT (it->ctl_chars[1], it->c);
5881 ctl_len = 2;
5882 goto display_control;
5886 unsigned char str[MAX_MULTIBYTE_LENGTH];
5887 int len;
5888 int i;
5890 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5891 if (CHAR_BYTE8_P (it->c))
5893 str[0] = CHAR_TO_BYTE8 (it->c);
5894 len = 1;
5896 else if (it->c < 256)
5898 str[0] = it->c;
5899 len = 1;
5901 else
5903 /* It's an invalid character, which shouldn't
5904 happen actually, but due to bugs it may
5905 happen. Let's print the char as is, there's
5906 not much meaningful we can do with it. */
5907 str[0] = it->c;
5908 str[1] = it->c >> 8;
5909 str[2] = it->c >> 16;
5910 str[3] = it->c >> 24;
5911 len = 4;
5914 for (i = 0; i < len; i++)
5916 int g;
5917 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5918 /* Insert three more glyphs into IT->ctl_chars for
5919 the octal display of the character. */
5920 g = ((str[i] >> 6) & 7) + '0';
5921 XSETINT (it->ctl_chars[i * 4 + 1], g);
5922 g = ((str[i] >> 3) & 7) + '0';
5923 XSETINT (it->ctl_chars[i * 4 + 2], g);
5924 g = (str[i] & 7) + '0';
5925 XSETINT (it->ctl_chars[i * 4 + 3], g);
5927 ctl_len = len * 4;
5930 display_control:
5931 /* Set up IT->dpvec and return first character from it. */
5932 it->dpvec_char_len = it->len;
5933 it->dpvec = it->ctl_chars;
5934 it->dpend = it->dpvec + ctl_len;
5935 it->current.dpvec_index = 0;
5936 it->dpvec_face_id = face_id;
5937 it->saved_face_id = it->face_id;
5938 it->method = GET_FROM_DISPLAY_VECTOR;
5939 it->ellipsis_p = 0;
5940 goto get_next;
5945 #ifdef HAVE_WINDOW_SYSTEM
5946 /* Adjust face id for a multibyte character. There are no multibyte
5947 character in unibyte text. */
5948 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5949 && it->multibyte_p
5950 && success_p
5951 && FRAME_WINDOW_P (it->f))
5953 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5955 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5957 /* Automatic composition with glyph-string. */
5958 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5960 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5962 else
5964 int pos = (it->s ? -1
5965 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5966 : IT_CHARPOS (*it));
5968 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
5971 #endif
5973 /* Is this character the last one of a run of characters with
5974 box? If yes, set IT->end_of_box_run_p to 1. */
5975 if (it->face_box_p
5976 && it->s == NULL)
5978 if (it->method == GET_FROM_STRING && it->sp)
5980 int face_id = underlying_face_id (it);
5981 struct face *face = FACE_FROM_ID (it->f, face_id);
5983 if (face)
5985 if (face->box == FACE_NO_BOX)
5987 /* If the box comes from face properties in a
5988 display string, check faces in that string. */
5989 int string_face_id = face_after_it_pos (it);
5990 it->end_of_box_run_p
5991 = (FACE_FROM_ID (it->f, string_face_id)->box
5992 == FACE_NO_BOX);
5994 /* Otherwise, the box comes from the underlying face.
5995 If this is the last string character displayed, check
5996 the next buffer location. */
5997 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5998 && (it->current.overlay_string_index
5999 == it->n_overlay_strings - 1))
6001 EMACS_INT ignore;
6002 int next_face_id;
6003 struct text_pos pos = it->current.pos;
6004 INC_TEXT_POS (pos, it->multibyte_p);
6006 next_face_id = face_at_buffer_position
6007 (it->w, CHARPOS (pos), it->region_beg_charpos,
6008 it->region_end_charpos, &ignore,
6009 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6010 -1);
6011 it->end_of_box_run_p
6012 = (FACE_FROM_ID (it->f, next_face_id)->box
6013 == FACE_NO_BOX);
6017 else
6019 int face_id = face_after_it_pos (it);
6020 it->end_of_box_run_p
6021 = (face_id != it->face_id
6022 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6026 /* Value is 0 if end of buffer or string reached. */
6027 return success_p;
6031 /* Move IT to the next display element.
6033 RESEAT_P non-zero means if called on a newline in buffer text,
6034 skip to the next visible line start.
6036 Functions get_next_display_element and set_iterator_to_next are
6037 separate because I find this arrangement easier to handle than a
6038 get_next_display_element function that also increments IT's
6039 position. The way it is we can first look at an iterator's current
6040 display element, decide whether it fits on a line, and if it does,
6041 increment the iterator position. The other way around we probably
6042 would either need a flag indicating whether the iterator has to be
6043 incremented the next time, or we would have to implement a
6044 decrement position function which would not be easy to write. */
6046 void
6047 set_iterator_to_next (it, reseat_p)
6048 struct it *it;
6049 int reseat_p;
6051 /* Reset flags indicating start and end of a sequence of characters
6052 with box. Reset them at the start of this function because
6053 moving the iterator to a new position might set them. */
6054 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6056 switch (it->method)
6058 case GET_FROM_BUFFER:
6059 /* The current display element of IT is a character from
6060 current_buffer. Advance in the buffer, and maybe skip over
6061 invisible lines that are so because of selective display. */
6062 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6063 reseat_at_next_visible_line_start (it, 0);
6064 else if (it->cmp_it.id >= 0)
6066 IT_CHARPOS (*it) += it->cmp_it.nchars;
6067 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6068 if (it->cmp_it.to < it->cmp_it.nglyphs)
6069 it->cmp_it.from = it->cmp_it.to;
6070 else
6072 it->cmp_it.id = -1;
6073 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6074 IT_BYTEPOS (*it), it->stop_charpos,
6075 Qnil);
6078 else
6080 xassert (it->len != 0);
6081 IT_BYTEPOS (*it) += it->len;
6082 IT_CHARPOS (*it) += 1;
6083 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6085 break;
6087 case GET_FROM_C_STRING:
6088 /* Current display element of IT is from a C string. */
6089 IT_BYTEPOS (*it) += it->len;
6090 IT_CHARPOS (*it) += 1;
6091 break;
6093 case GET_FROM_DISPLAY_VECTOR:
6094 /* Current display element of IT is from a display table entry.
6095 Advance in the display table definition. Reset it to null if
6096 end reached, and continue with characters from buffers/
6097 strings. */
6098 ++it->current.dpvec_index;
6100 /* Restore face of the iterator to what they were before the
6101 display vector entry (these entries may contain faces). */
6102 it->face_id = it->saved_face_id;
6104 if (it->dpvec + it->current.dpvec_index == it->dpend)
6106 int recheck_faces = it->ellipsis_p;
6108 if (it->s)
6109 it->method = GET_FROM_C_STRING;
6110 else if (STRINGP (it->string))
6111 it->method = GET_FROM_STRING;
6112 else
6114 it->method = GET_FROM_BUFFER;
6115 it->object = it->w->buffer;
6118 it->dpvec = NULL;
6119 it->current.dpvec_index = -1;
6121 /* Skip over characters which were displayed via IT->dpvec. */
6122 if (it->dpvec_char_len < 0)
6123 reseat_at_next_visible_line_start (it, 1);
6124 else if (it->dpvec_char_len > 0)
6126 if (it->method == GET_FROM_STRING
6127 && it->n_overlay_strings > 0)
6128 it->ignore_overlay_strings_at_pos_p = 1;
6129 it->len = it->dpvec_char_len;
6130 set_iterator_to_next (it, reseat_p);
6133 /* Maybe recheck faces after display vector */
6134 if (recheck_faces)
6135 it->stop_charpos = IT_CHARPOS (*it);
6137 break;
6139 case GET_FROM_STRING:
6140 /* Current display element is a character from a Lisp string. */
6141 xassert (it->s == NULL && STRINGP (it->string));
6142 if (it->cmp_it.id >= 0)
6144 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6145 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6146 if (it->cmp_it.to < it->cmp_it.nglyphs)
6147 it->cmp_it.from = it->cmp_it.to;
6148 else
6150 it->cmp_it.id = -1;
6151 composition_compute_stop_pos (&it->cmp_it,
6152 IT_STRING_CHARPOS (*it),
6153 IT_STRING_BYTEPOS (*it),
6154 it->stop_charpos, it->string);
6157 else
6159 IT_STRING_BYTEPOS (*it) += it->len;
6160 IT_STRING_CHARPOS (*it) += 1;
6163 consider_string_end:
6165 if (it->current.overlay_string_index >= 0)
6167 /* IT->string is an overlay string. Advance to the
6168 next, if there is one. */
6169 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6171 it->ellipsis_p = 0;
6172 next_overlay_string (it);
6173 if (it->ellipsis_p)
6174 setup_for_ellipsis (it, 0);
6177 else
6179 /* IT->string is not an overlay string. If we reached
6180 its end, and there is something on IT->stack, proceed
6181 with what is on the stack. This can be either another
6182 string, this time an overlay string, or a buffer. */
6183 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6184 && it->sp > 0)
6186 pop_it (it);
6187 if (it->method == GET_FROM_STRING)
6188 goto consider_string_end;
6191 break;
6193 case GET_FROM_IMAGE:
6194 case GET_FROM_STRETCH:
6195 /* The position etc with which we have to proceed are on
6196 the stack. The position may be at the end of a string,
6197 if the `display' property takes up the whole string. */
6198 xassert (it->sp > 0);
6199 pop_it (it);
6200 if (it->method == GET_FROM_STRING)
6201 goto consider_string_end;
6202 break;
6204 default:
6205 /* There are no other methods defined, so this should be a bug. */
6206 abort ();
6209 xassert (it->method != GET_FROM_STRING
6210 || (STRINGP (it->string)
6211 && IT_STRING_CHARPOS (*it) >= 0));
6214 /* Load IT's display element fields with information about the next
6215 display element which comes from a display table entry or from the
6216 result of translating a control character to one of the forms `^C'
6217 or `\003'.
6219 IT->dpvec holds the glyphs to return as characters.
6220 IT->saved_face_id holds the face id before the display vector--
6221 it is restored into IT->face_idin set_iterator_to_next. */
6223 static int
6224 next_element_from_display_vector (it)
6225 struct it *it;
6227 Lisp_Object gc;
6229 /* Precondition. */
6230 xassert (it->dpvec && it->current.dpvec_index >= 0);
6232 it->face_id = it->saved_face_id;
6234 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6235 That seemed totally bogus - so I changed it... */
6236 gc = it->dpvec[it->current.dpvec_index];
6238 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6240 it->c = GLYPH_CODE_CHAR (gc);
6241 it->len = CHAR_BYTES (it->c);
6243 /* The entry may contain a face id to use. Such a face id is
6244 the id of a Lisp face, not a realized face. A face id of
6245 zero means no face is specified. */
6246 if (it->dpvec_face_id >= 0)
6247 it->face_id = it->dpvec_face_id;
6248 else
6250 int lface_id = GLYPH_CODE_FACE (gc);
6251 if (lface_id > 0)
6252 it->face_id = merge_faces (it->f, Qt, lface_id,
6253 it->saved_face_id);
6256 else
6257 /* Display table entry is invalid. Return a space. */
6258 it->c = ' ', it->len = 1;
6260 /* Don't change position and object of the iterator here. They are
6261 still the values of the character that had this display table
6262 entry or was translated, and that's what we want. */
6263 it->what = IT_CHARACTER;
6264 return 1;
6268 /* Load IT with the next display element from Lisp string IT->string.
6269 IT->current.string_pos is the current position within the string.
6270 If IT->current.overlay_string_index >= 0, the Lisp string is an
6271 overlay string. */
6273 static int
6274 next_element_from_string (it)
6275 struct it *it;
6277 struct text_pos position;
6279 xassert (STRINGP (it->string));
6280 xassert (IT_STRING_CHARPOS (*it) >= 0);
6281 position = it->current.string_pos;
6283 /* Time to check for invisible text? */
6284 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6285 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6287 handle_stop (it);
6289 /* Since a handler may have changed IT->method, we must
6290 recurse here. */
6291 return GET_NEXT_DISPLAY_ELEMENT (it);
6294 if (it->current.overlay_string_index >= 0)
6296 /* Get the next character from an overlay string. In overlay
6297 strings, There is no field width or padding with spaces to
6298 do. */
6299 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6301 it->what = IT_EOB;
6302 return 0;
6304 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6305 IT_STRING_BYTEPOS (*it))
6306 && next_element_from_composition (it))
6308 return 1;
6310 else if (STRING_MULTIBYTE (it->string))
6312 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6313 const unsigned char *s = (SDATA (it->string)
6314 + IT_STRING_BYTEPOS (*it));
6315 it->c = string_char_and_length (s, &it->len);
6317 else
6319 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6320 it->len = 1;
6323 else
6325 /* Get the next character from a Lisp string that is not an
6326 overlay string. Such strings come from the mode line, for
6327 example. We may have to pad with spaces, or truncate the
6328 string. See also next_element_from_c_string. */
6329 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6331 it->what = IT_EOB;
6332 return 0;
6334 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6336 /* Pad with spaces. */
6337 it->c = ' ', it->len = 1;
6338 CHARPOS (position) = BYTEPOS (position) = -1;
6340 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6341 IT_STRING_BYTEPOS (*it))
6342 && next_element_from_composition (it))
6344 return 1;
6346 else if (STRING_MULTIBYTE (it->string))
6348 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6349 const unsigned char *s = (SDATA (it->string)
6350 + IT_STRING_BYTEPOS (*it));
6351 it->c = string_char_and_length (s, &it->len);
6353 else
6355 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6356 it->len = 1;
6360 /* Record what we have and where it came from. */
6361 it->what = IT_CHARACTER;
6362 it->object = it->string;
6363 it->position = position;
6364 return 1;
6368 /* Load IT with next display element from C string IT->s.
6369 IT->string_nchars is the maximum number of characters to return
6370 from the string. IT->end_charpos may be greater than
6371 IT->string_nchars when this function is called, in which case we
6372 may have to return padding spaces. Value is zero if end of string
6373 reached, including padding spaces. */
6375 static int
6376 next_element_from_c_string (it)
6377 struct it *it;
6379 int success_p = 1;
6381 xassert (it->s);
6382 it->what = IT_CHARACTER;
6383 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6384 it->object = Qnil;
6386 /* IT's position can be greater IT->string_nchars in case a field
6387 width or precision has been specified when the iterator was
6388 initialized. */
6389 if (IT_CHARPOS (*it) >= it->end_charpos)
6391 /* End of the game. */
6392 it->what = IT_EOB;
6393 success_p = 0;
6395 else if (IT_CHARPOS (*it) >= it->string_nchars)
6397 /* Pad with spaces. */
6398 it->c = ' ', it->len = 1;
6399 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6401 else if (it->multibyte_p)
6403 /* Implementation note: The calls to strlen apparently aren't a
6404 performance problem because there is no noticeable performance
6405 difference between Emacs running in unibyte or multibyte mode. */
6406 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6407 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6409 else
6410 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6412 return success_p;
6416 /* Set up IT to return characters from an ellipsis, if appropriate.
6417 The definition of the ellipsis glyphs may come from a display table
6418 entry. This function Fills IT with the first glyph from the
6419 ellipsis if an ellipsis is to be displayed. */
6421 static int
6422 next_element_from_ellipsis (it)
6423 struct it *it;
6425 if (it->selective_display_ellipsis_p)
6426 setup_for_ellipsis (it, it->len);
6427 else
6429 /* The face at the current position may be different from the
6430 face we find after the invisible text. Remember what it
6431 was in IT->saved_face_id, and signal that it's there by
6432 setting face_before_selective_p. */
6433 it->saved_face_id = it->face_id;
6434 it->method = GET_FROM_BUFFER;
6435 it->object = it->w->buffer;
6436 reseat_at_next_visible_line_start (it, 1);
6437 it->face_before_selective_p = 1;
6440 return GET_NEXT_DISPLAY_ELEMENT (it);
6444 /* Deliver an image display element. The iterator IT is already
6445 filled with image information (done in handle_display_prop). Value
6446 is always 1. */
6449 static int
6450 next_element_from_image (it)
6451 struct it *it;
6453 it->what = IT_IMAGE;
6454 return 1;
6458 /* Fill iterator IT with next display element from a stretch glyph
6459 property. IT->object is the value of the text property. Value is
6460 always 1. */
6462 static int
6463 next_element_from_stretch (it)
6464 struct it *it;
6466 it->what = IT_STRETCH;
6467 return 1;
6471 /* Load IT with the next display element from current_buffer. Value
6472 is zero if end of buffer reached. IT->stop_charpos is the next
6473 position at which to stop and check for text properties or buffer
6474 end. */
6476 static int
6477 next_element_from_buffer (it)
6478 struct it *it;
6480 int success_p = 1;
6482 xassert (IT_CHARPOS (*it) >= BEGV);
6484 if (IT_CHARPOS (*it) >= it->stop_charpos)
6486 if (IT_CHARPOS (*it) >= it->end_charpos)
6488 int overlay_strings_follow_p;
6490 /* End of the game, except when overlay strings follow that
6491 haven't been returned yet. */
6492 if (it->overlay_strings_at_end_processed_p)
6493 overlay_strings_follow_p = 0;
6494 else
6496 it->overlay_strings_at_end_processed_p = 1;
6497 overlay_strings_follow_p = get_overlay_strings (it, 0);
6500 if (overlay_strings_follow_p)
6501 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6502 else
6504 it->what = IT_EOB;
6505 it->position = it->current.pos;
6506 success_p = 0;
6509 else
6511 handle_stop (it);
6512 return GET_NEXT_DISPLAY_ELEMENT (it);
6515 else
6517 /* No face changes, overlays etc. in sight, so just return a
6518 character from current_buffer. */
6519 unsigned char *p;
6521 /* Maybe run the redisplay end trigger hook. Performance note:
6522 This doesn't seem to cost measurable time. */
6523 if (it->redisplay_end_trigger_charpos
6524 && it->glyph_row
6525 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6526 run_redisplay_end_trigger_hook (it);
6528 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it))
6529 && next_element_from_composition (it))
6531 return 1;
6534 /* Get the next character, maybe multibyte. */
6535 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6536 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6537 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6538 else
6539 it->c = *p, it->len = 1;
6541 /* Record what we have and where it came from. */
6542 it->what = IT_CHARACTER;
6543 it->object = it->w->buffer;
6544 it->position = it->current.pos;
6546 /* Normally we return the character found above, except when we
6547 really want to return an ellipsis for selective display. */
6548 if (it->selective)
6550 if (it->c == '\n')
6552 /* A value of selective > 0 means hide lines indented more
6553 than that number of columns. */
6554 if (it->selective > 0
6555 && IT_CHARPOS (*it) + 1 < ZV
6556 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6557 IT_BYTEPOS (*it) + 1,
6558 (double) it->selective)) /* iftc */
6560 success_p = next_element_from_ellipsis (it);
6561 it->dpvec_char_len = -1;
6564 else if (it->c == '\r' && it->selective == -1)
6566 /* A value of selective == -1 means that everything from the
6567 CR to the end of the line is invisible, with maybe an
6568 ellipsis displayed for it. */
6569 success_p = next_element_from_ellipsis (it);
6570 it->dpvec_char_len = -1;
6575 /* Value is zero if end of buffer reached. */
6576 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6577 return success_p;
6581 /* Run the redisplay end trigger hook for IT. */
6583 static void
6584 run_redisplay_end_trigger_hook (it)
6585 struct it *it;
6587 Lisp_Object args[3];
6589 /* IT->glyph_row should be non-null, i.e. we should be actually
6590 displaying something, or otherwise we should not run the hook. */
6591 xassert (it->glyph_row);
6593 /* Set up hook arguments. */
6594 args[0] = Qredisplay_end_trigger_functions;
6595 args[1] = it->window;
6596 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6597 it->redisplay_end_trigger_charpos = 0;
6599 /* Since we are *trying* to run these functions, don't try to run
6600 them again, even if they get an error. */
6601 it->w->redisplay_end_trigger = Qnil;
6602 Frun_hook_with_args (3, args);
6604 /* Notice if it changed the face of the character we are on. */
6605 handle_face_prop (it);
6609 /* Deliver a composition display element. Unlike the other
6610 next_element_from_XXX, this function is not registered in the array
6611 get_next_element[]. It is called from next_element_from_buffer and
6612 next_element_from_string when necessary. */
6614 static int
6615 next_element_from_composition (it)
6616 struct it *it;
6618 it->what = IT_COMPOSITION;
6619 it->len = it->cmp_it.nbytes;
6620 if (STRINGP (it->string))
6622 if (it->c < 0)
6624 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6625 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6626 return 0;
6628 it->position = it->current.string_pos;
6629 it->object = it->string;
6630 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6631 IT_STRING_BYTEPOS (*it), it->string);
6633 else
6635 if (it->c < 0)
6637 IT_CHARPOS (*it) += it->cmp_it.nchars;
6638 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6639 return 0;
6641 it->position = it->current.pos;
6642 it->object = it->w->buffer;
6643 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6644 IT_BYTEPOS (*it), Qnil);
6646 return 1;
6651 /***********************************************************************
6652 Moving an iterator without producing glyphs
6653 ***********************************************************************/
6655 /* Check if iterator is at a position corresponding to a valid buffer
6656 position after some move_it_ call. */
6658 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6659 ((it)->method == GET_FROM_STRING \
6660 ? IT_STRING_CHARPOS (*it) == 0 \
6661 : 1)
6664 /* Move iterator IT to a specified buffer or X position within one
6665 line on the display without producing glyphs.
6667 OP should be a bit mask including some or all of these bits:
6668 MOVE_TO_X: Stop on reaching x-position TO_X.
6669 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6670 Regardless of OP's value, stop in reaching the end of the display line.
6672 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6673 This means, in particular, that TO_X includes window's horizontal
6674 scroll amount.
6676 The return value has several possible values that
6677 say what condition caused the scan to stop:
6679 MOVE_POS_MATCH_OR_ZV
6680 - when TO_POS or ZV was reached.
6682 MOVE_X_REACHED
6683 -when TO_X was reached before TO_POS or ZV were reached.
6685 MOVE_LINE_CONTINUED
6686 - when we reached the end of the display area and the line must
6687 be continued.
6689 MOVE_LINE_TRUNCATED
6690 - when we reached the end of the display area and the line is
6691 truncated.
6693 MOVE_NEWLINE_OR_CR
6694 - when we stopped at a line end, i.e. a newline or a CR and selective
6695 display is on. */
6697 static enum move_it_result
6698 move_it_in_display_line_to (struct it *it,
6699 EMACS_INT to_charpos, int to_x,
6700 enum move_operation_enum op)
6702 enum move_it_result result = MOVE_UNDEFINED;
6703 struct glyph_row *saved_glyph_row;
6704 struct it wrap_it, atpos_it, atx_it;
6705 int may_wrap = 0;
6707 /* Don't produce glyphs in produce_glyphs. */
6708 saved_glyph_row = it->glyph_row;
6709 it->glyph_row = NULL;
6711 /* Use wrap_it to save a copy of IT wherever a word wrap could
6712 occur. Use atpos_it to save a copy of IT at the desired buffer
6713 position, if found, so that we can scan ahead and check if the
6714 word later overshoots the window edge. Use atx_it similarly, for
6715 pixel positions. */
6716 wrap_it.sp = -1;
6717 atpos_it.sp = -1;
6718 atx_it.sp = -1;
6720 #define BUFFER_POS_REACHED_P() \
6721 ((op & MOVE_TO_POS) != 0 \
6722 && BUFFERP (it->object) \
6723 && IT_CHARPOS (*it) >= to_charpos \
6724 && (it->method == GET_FROM_BUFFER \
6725 || (it->method == GET_FROM_DISPLAY_VECTOR \
6726 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6728 /* If there's a line-/wrap-prefix, handle it. */
6729 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6730 && it->current_y < it->last_visible_y)
6731 handle_line_prefix (it);
6733 while (1)
6735 int x, i, ascent = 0, descent = 0;
6737 /* Utility macro to reset an iterator with x, ascent, and descent. */
6738 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6739 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6740 (IT)->max_descent = descent)
6742 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6743 glyph). */
6744 if ((op & MOVE_TO_POS) != 0
6745 && BUFFERP (it->object)
6746 && it->method == GET_FROM_BUFFER
6747 && IT_CHARPOS (*it) > to_charpos)
6749 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6751 result = MOVE_POS_MATCH_OR_ZV;
6752 break;
6754 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6755 /* If wrap_it is valid, the current position might be in a
6756 word that is wrapped. So, save the iterator in
6757 atpos_it and continue to see if wrapping happens. */
6758 atpos_it = *it;
6761 /* Stop when ZV reached.
6762 We used to stop here when TO_CHARPOS reached as well, but that is
6763 too soon if this glyph does not fit on this line. So we handle it
6764 explicitly below. */
6765 if (!get_next_display_element (it))
6767 result = MOVE_POS_MATCH_OR_ZV;
6768 break;
6771 if (it->line_wrap == TRUNCATE)
6773 if (BUFFER_POS_REACHED_P ())
6775 result = MOVE_POS_MATCH_OR_ZV;
6776 break;
6779 else
6781 if (it->line_wrap == WORD_WRAP)
6783 if (IT_DISPLAYING_WHITESPACE (it))
6784 may_wrap = 1;
6785 else if (may_wrap)
6787 /* We have reached a glyph that follows one or more
6788 whitespace characters. If the position is
6789 already found, we are done. */
6790 if (atpos_it.sp >= 0)
6792 *it = atpos_it;
6793 result = MOVE_POS_MATCH_OR_ZV;
6794 goto done;
6796 if (atx_it.sp >= 0)
6798 *it = atx_it;
6799 result = MOVE_X_REACHED;
6800 goto done;
6802 /* Otherwise, we can wrap here. */
6803 wrap_it = *it;
6804 may_wrap = 0;
6809 /* Remember the line height for the current line, in case
6810 the next element doesn't fit on the line. */
6811 ascent = it->max_ascent;
6812 descent = it->max_descent;
6814 /* The call to produce_glyphs will get the metrics of the
6815 display element IT is loaded with. Record the x-position
6816 before this display element, in case it doesn't fit on the
6817 line. */
6818 x = it->current_x;
6820 PRODUCE_GLYPHS (it);
6822 if (it->area != TEXT_AREA)
6824 set_iterator_to_next (it, 1);
6825 continue;
6828 /* The number of glyphs we get back in IT->nglyphs will normally
6829 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6830 character on a terminal frame, or (iii) a line end. For the
6831 second case, IT->nglyphs - 1 padding glyphs will be present.
6832 (On X frames, there is only one glyph produced for a
6833 composite character.)
6835 The behavior implemented below means, for continuation lines,
6836 that as many spaces of a TAB as fit on the current line are
6837 displayed there. For terminal frames, as many glyphs of a
6838 multi-glyph character are displayed in the current line, too.
6839 This is what the old redisplay code did, and we keep it that
6840 way. Under X, the whole shape of a complex character must
6841 fit on the line or it will be completely displayed in the
6842 next line.
6844 Note that both for tabs and padding glyphs, all glyphs have
6845 the same width. */
6846 if (it->nglyphs)
6848 /* More than one glyph or glyph doesn't fit on line. All
6849 glyphs have the same width. */
6850 int single_glyph_width = it->pixel_width / it->nglyphs;
6851 int new_x;
6852 int x_before_this_char = x;
6853 int hpos_before_this_char = it->hpos;
6855 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6857 new_x = x + single_glyph_width;
6859 /* We want to leave anything reaching TO_X to the caller. */
6860 if ((op & MOVE_TO_X) && new_x > to_x)
6862 if (BUFFER_POS_REACHED_P ())
6864 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6865 goto buffer_pos_reached;
6866 if (atpos_it.sp < 0)
6868 atpos_it = *it;
6869 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6872 else
6874 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6876 it->current_x = x;
6877 result = MOVE_X_REACHED;
6878 break;
6880 if (atx_it.sp < 0)
6882 atx_it = *it;
6883 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6888 if (/* Lines are continued. */
6889 it->line_wrap != TRUNCATE
6890 && (/* And glyph doesn't fit on the line. */
6891 new_x > it->last_visible_x
6892 /* Or it fits exactly and we're on a window
6893 system frame. */
6894 || (new_x == it->last_visible_x
6895 && FRAME_WINDOW_P (it->f))))
6897 if (/* IT->hpos == 0 means the very first glyph
6898 doesn't fit on the line, e.g. a wide image. */
6899 it->hpos == 0
6900 || (new_x == it->last_visible_x
6901 && FRAME_WINDOW_P (it->f)))
6903 ++it->hpos;
6904 it->current_x = new_x;
6906 /* The character's last glyph just barely fits
6907 in this row. */
6908 if (i == it->nglyphs - 1)
6910 /* If this is the destination position,
6911 return a position *before* it in this row,
6912 now that we know it fits in this row. */
6913 if (BUFFER_POS_REACHED_P ())
6915 if (it->line_wrap != WORD_WRAP
6916 || wrap_it.sp < 0)
6918 it->hpos = hpos_before_this_char;
6919 it->current_x = x_before_this_char;
6920 result = MOVE_POS_MATCH_OR_ZV;
6921 break;
6923 if (it->line_wrap == WORD_WRAP
6924 && atpos_it.sp < 0)
6926 atpos_it = *it;
6927 atpos_it.current_x = x_before_this_char;
6928 atpos_it.hpos = hpos_before_this_char;
6932 set_iterator_to_next (it, 1);
6933 /* On graphical terminals, newlines may
6934 "overflow" into the fringe if
6935 overflow-newline-into-fringe is non-nil.
6936 On text-only terminals, newlines may
6937 overflow into the last glyph on the
6938 display line.*/
6939 if (!FRAME_WINDOW_P (it->f)
6940 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6942 if (!get_next_display_element (it))
6944 result = MOVE_POS_MATCH_OR_ZV;
6945 break;
6947 if (BUFFER_POS_REACHED_P ())
6949 if (ITERATOR_AT_END_OF_LINE_P (it))
6950 result = MOVE_POS_MATCH_OR_ZV;
6951 else
6952 result = MOVE_LINE_CONTINUED;
6953 break;
6955 if (ITERATOR_AT_END_OF_LINE_P (it))
6957 result = MOVE_NEWLINE_OR_CR;
6958 break;
6963 else
6964 IT_RESET_X_ASCENT_DESCENT (it);
6966 if (wrap_it.sp >= 0)
6968 *it = wrap_it;
6969 atpos_it.sp = -1;
6970 atx_it.sp = -1;
6973 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6974 IT_CHARPOS (*it)));
6975 result = MOVE_LINE_CONTINUED;
6976 break;
6979 if (BUFFER_POS_REACHED_P ())
6981 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6982 goto buffer_pos_reached;
6983 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6985 atpos_it = *it;
6986 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6990 if (new_x > it->first_visible_x)
6992 /* Glyph is visible. Increment number of glyphs that
6993 would be displayed. */
6994 ++it->hpos;
6998 if (result != MOVE_UNDEFINED)
6999 break;
7001 else if (BUFFER_POS_REACHED_P ())
7003 buffer_pos_reached:
7004 IT_RESET_X_ASCENT_DESCENT (it);
7005 result = MOVE_POS_MATCH_OR_ZV;
7006 break;
7008 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7010 /* Stop when TO_X specified and reached. This check is
7011 necessary here because of lines consisting of a line end,
7012 only. The line end will not produce any glyphs and we
7013 would never get MOVE_X_REACHED. */
7014 xassert (it->nglyphs == 0);
7015 result = MOVE_X_REACHED;
7016 break;
7019 /* Is this a line end? If yes, we're done. */
7020 if (ITERATOR_AT_END_OF_LINE_P (it))
7022 result = MOVE_NEWLINE_OR_CR;
7023 break;
7026 /* The current display element has been consumed. Advance
7027 to the next. */
7028 set_iterator_to_next (it, 1);
7030 /* Stop if lines are truncated and IT's current x-position is
7031 past the right edge of the window now. */
7032 if (it->line_wrap == TRUNCATE
7033 && it->current_x >= it->last_visible_x)
7035 if (!FRAME_WINDOW_P (it->f)
7036 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7038 if (!get_next_display_element (it)
7039 || BUFFER_POS_REACHED_P ())
7041 result = MOVE_POS_MATCH_OR_ZV;
7042 break;
7044 if (ITERATOR_AT_END_OF_LINE_P (it))
7046 result = MOVE_NEWLINE_OR_CR;
7047 break;
7050 result = MOVE_LINE_TRUNCATED;
7051 break;
7053 #undef IT_RESET_X_ASCENT_DESCENT
7056 #undef BUFFER_POS_REACHED_P
7058 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7059 restore the saved iterator. */
7060 if (atpos_it.sp >= 0)
7061 *it = atpos_it;
7062 else if (atx_it.sp >= 0)
7063 *it = atx_it;
7065 done:
7067 /* Restore the iterator settings altered at the beginning of this
7068 function. */
7069 it->glyph_row = saved_glyph_row;
7070 return result;
7073 /* For external use. */
7074 void
7075 move_it_in_display_line (struct it *it,
7076 EMACS_INT to_charpos, int to_x,
7077 enum move_operation_enum op)
7079 if (it->line_wrap == WORD_WRAP
7080 && (op & MOVE_TO_X))
7082 struct it save_it = *it;
7083 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7084 /* When word-wrap is on, TO_X may lie past the end
7085 of a wrapped line. Then it->current is the
7086 character on the next line, so backtrack to the
7087 space before the wrap point. */
7088 if (skip == MOVE_LINE_CONTINUED)
7090 int prev_x = max (it->current_x - 1, 0);
7091 *it = save_it;
7092 move_it_in_display_line_to
7093 (it, -1, prev_x, MOVE_TO_X);
7096 else
7097 move_it_in_display_line_to (it, to_charpos, to_x, op);
7101 /* Move IT forward until it satisfies one or more of the criteria in
7102 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7104 OP is a bit-mask that specifies where to stop, and in particular,
7105 which of those four position arguments makes a difference. See the
7106 description of enum move_operation_enum.
7108 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7109 screen line, this function will set IT to the next position >
7110 TO_CHARPOS. */
7112 void
7113 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7114 struct it *it;
7115 int to_charpos, to_x, to_y, to_vpos;
7116 int op;
7118 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7119 int line_height, line_start_x = 0, reached = 0;
7121 for (;;)
7123 if (op & MOVE_TO_VPOS)
7125 /* If no TO_CHARPOS and no TO_X specified, stop at the
7126 start of the line TO_VPOS. */
7127 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7129 if (it->vpos == to_vpos)
7131 reached = 1;
7132 break;
7134 else
7135 skip = move_it_in_display_line_to (it, -1, -1, 0);
7137 else
7139 /* TO_VPOS >= 0 means stop at TO_X in the line at
7140 TO_VPOS, or at TO_POS, whichever comes first. */
7141 if (it->vpos == to_vpos)
7143 reached = 2;
7144 break;
7147 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7149 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7151 reached = 3;
7152 break;
7154 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7156 /* We have reached TO_X but not in the line we want. */
7157 skip = move_it_in_display_line_to (it, to_charpos,
7158 -1, MOVE_TO_POS);
7159 if (skip == MOVE_POS_MATCH_OR_ZV)
7161 reached = 4;
7162 break;
7167 else if (op & MOVE_TO_Y)
7169 struct it it_backup;
7171 if (it->line_wrap == WORD_WRAP)
7172 it_backup = *it;
7174 /* TO_Y specified means stop at TO_X in the line containing
7175 TO_Y---or at TO_CHARPOS if this is reached first. The
7176 problem is that we can't really tell whether the line
7177 contains TO_Y before we have completely scanned it, and
7178 this may skip past TO_X. What we do is to first scan to
7179 TO_X.
7181 If TO_X is not specified, use a TO_X of zero. The reason
7182 is to make the outcome of this function more predictable.
7183 If we didn't use TO_X == 0, we would stop at the end of
7184 the line which is probably not what a caller would expect
7185 to happen. */
7186 skip = move_it_in_display_line_to
7187 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7188 (MOVE_TO_X | (op & MOVE_TO_POS)));
7190 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7191 if (skip == MOVE_POS_MATCH_OR_ZV)
7192 reached = 5;
7193 else if (skip == MOVE_X_REACHED)
7195 /* If TO_X was reached, we want to know whether TO_Y is
7196 in the line. We know this is the case if the already
7197 scanned glyphs make the line tall enough. Otherwise,
7198 we must check by scanning the rest of the line. */
7199 line_height = it->max_ascent + it->max_descent;
7200 if (to_y >= it->current_y
7201 && to_y < it->current_y + line_height)
7203 reached = 6;
7204 break;
7206 it_backup = *it;
7207 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7208 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7209 op & MOVE_TO_POS);
7210 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7211 line_height = it->max_ascent + it->max_descent;
7212 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7214 if (to_y >= it->current_y
7215 && to_y < it->current_y + line_height)
7217 /* If TO_Y is in this line and TO_X was reached
7218 above, we scanned too far. We have to restore
7219 IT's settings to the ones before skipping. */
7220 *it = it_backup;
7221 reached = 6;
7223 else
7225 skip = skip2;
7226 if (skip == MOVE_POS_MATCH_OR_ZV)
7227 reached = 7;
7230 else
7232 /* Check whether TO_Y is in this line. */
7233 line_height = it->max_ascent + it->max_descent;
7234 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7236 if (to_y >= it->current_y
7237 && to_y < it->current_y + line_height)
7239 /* When word-wrap is on, TO_X may lie past the end
7240 of a wrapped line. Then it->current is the
7241 character on the next line, so backtrack to the
7242 space before the wrap point. */
7243 if (skip == MOVE_LINE_CONTINUED
7244 && it->line_wrap == WORD_WRAP)
7246 int prev_x = max (it->current_x - 1, 0);
7247 *it = it_backup;
7248 skip = move_it_in_display_line_to
7249 (it, -1, prev_x, MOVE_TO_X);
7251 reached = 6;
7255 if (reached)
7256 break;
7258 else if (BUFFERP (it->object)
7259 && (it->method == GET_FROM_BUFFER
7260 || it->method == GET_FROM_STRETCH)
7261 && IT_CHARPOS (*it) >= to_charpos)
7262 skip = MOVE_POS_MATCH_OR_ZV;
7263 else
7264 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7266 switch (skip)
7268 case MOVE_POS_MATCH_OR_ZV:
7269 reached = 8;
7270 goto out;
7272 case MOVE_NEWLINE_OR_CR:
7273 set_iterator_to_next (it, 1);
7274 it->continuation_lines_width = 0;
7275 break;
7277 case MOVE_LINE_TRUNCATED:
7278 it->continuation_lines_width = 0;
7279 reseat_at_next_visible_line_start (it, 0);
7280 if ((op & MOVE_TO_POS) != 0
7281 && IT_CHARPOS (*it) > to_charpos)
7283 reached = 9;
7284 goto out;
7286 break;
7288 case MOVE_LINE_CONTINUED:
7289 /* For continued lines ending in a tab, some of the glyphs
7290 associated with the tab are displayed on the current
7291 line. Since it->current_x does not include these glyphs,
7292 we use it->last_visible_x instead. */
7293 if (it->c == '\t')
7295 it->continuation_lines_width += it->last_visible_x;
7296 /* When moving by vpos, ensure that the iterator really
7297 advances to the next line (bug#847, bug#969). Fixme:
7298 do we need to do this in other circumstances? */
7299 if (it->current_x != it->last_visible_x
7300 && (op & MOVE_TO_VPOS)
7301 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7303 line_start_x = it->current_x + it->pixel_width
7304 - it->last_visible_x;
7305 set_iterator_to_next (it, 0);
7308 else
7309 it->continuation_lines_width += it->current_x;
7310 break;
7312 default:
7313 abort ();
7316 /* Reset/increment for the next run. */
7317 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7318 it->current_x = line_start_x;
7319 line_start_x = 0;
7320 it->hpos = 0;
7321 it->current_y += it->max_ascent + it->max_descent;
7322 ++it->vpos;
7323 last_height = it->max_ascent + it->max_descent;
7324 last_max_ascent = it->max_ascent;
7325 it->max_ascent = it->max_descent = 0;
7328 out:
7330 /* On text terminals, we may stop at the end of a line in the middle
7331 of a multi-character glyph. If the glyph itself is continued,
7332 i.e. it is actually displayed on the next line, don't treat this
7333 stopping point as valid; move to the next line instead (unless
7334 that brings us offscreen). */
7335 if (!FRAME_WINDOW_P (it->f)
7336 && op & MOVE_TO_POS
7337 && IT_CHARPOS (*it) == to_charpos
7338 && it->what == IT_CHARACTER
7339 && it->nglyphs > 1
7340 && it->line_wrap == WINDOW_WRAP
7341 && it->current_x == it->last_visible_x - 1
7342 && it->c != '\n'
7343 && it->c != '\t'
7344 && it->vpos < XFASTINT (it->w->window_end_vpos))
7346 it->continuation_lines_width += it->current_x;
7347 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7348 it->current_y += it->max_ascent + it->max_descent;
7349 ++it->vpos;
7350 last_height = it->max_ascent + it->max_descent;
7351 last_max_ascent = it->max_ascent;
7354 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7358 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7360 If DY > 0, move IT backward at least that many pixels. DY = 0
7361 means move IT backward to the preceding line start or BEGV. This
7362 function may move over more than DY pixels if IT->current_y - DY
7363 ends up in the middle of a line; in this case IT->current_y will be
7364 set to the top of the line moved to. */
7366 void
7367 move_it_vertically_backward (it, dy)
7368 struct it *it;
7369 int dy;
7371 int nlines, h;
7372 struct it it2, it3;
7373 int start_pos;
7375 move_further_back:
7376 xassert (dy >= 0);
7378 start_pos = IT_CHARPOS (*it);
7380 /* Estimate how many newlines we must move back. */
7381 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7383 /* Set the iterator's position that many lines back. */
7384 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7385 back_to_previous_visible_line_start (it);
7387 /* Reseat the iterator here. When moving backward, we don't want
7388 reseat to skip forward over invisible text, set up the iterator
7389 to deliver from overlay strings at the new position etc. So,
7390 use reseat_1 here. */
7391 reseat_1 (it, it->current.pos, 1);
7393 /* We are now surely at a line start. */
7394 it->current_x = it->hpos = 0;
7395 it->continuation_lines_width = 0;
7397 /* Move forward and see what y-distance we moved. First move to the
7398 start of the next line so that we get its height. We need this
7399 height to be able to tell whether we reached the specified
7400 y-distance. */
7401 it2 = *it;
7402 it2.max_ascent = it2.max_descent = 0;
7405 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7406 MOVE_TO_POS | MOVE_TO_VPOS);
7408 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7409 xassert (IT_CHARPOS (*it) >= BEGV);
7410 it3 = it2;
7412 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7413 xassert (IT_CHARPOS (*it) >= BEGV);
7414 /* H is the actual vertical distance from the position in *IT
7415 and the starting position. */
7416 h = it2.current_y - it->current_y;
7417 /* NLINES is the distance in number of lines. */
7418 nlines = it2.vpos - it->vpos;
7420 /* Correct IT's y and vpos position
7421 so that they are relative to the starting point. */
7422 it->vpos -= nlines;
7423 it->current_y -= h;
7425 if (dy == 0)
7427 /* DY == 0 means move to the start of the screen line. The
7428 value of nlines is > 0 if continuation lines were involved. */
7429 if (nlines > 0)
7430 move_it_by_lines (it, nlines, 1);
7432 else
7434 /* The y-position we try to reach, relative to *IT.
7435 Note that H has been subtracted in front of the if-statement. */
7436 int target_y = it->current_y + h - dy;
7437 int y0 = it3.current_y;
7438 int y1 = line_bottom_y (&it3);
7439 int line_height = y1 - y0;
7441 /* If we did not reach target_y, try to move further backward if
7442 we can. If we moved too far backward, try to move forward. */
7443 if (target_y < it->current_y
7444 /* This is heuristic. In a window that's 3 lines high, with
7445 a line height of 13 pixels each, recentering with point
7446 on the bottom line will try to move -39/2 = 19 pixels
7447 backward. Try to avoid moving into the first line. */
7448 && (it->current_y - target_y
7449 > min (window_box_height (it->w), line_height * 2 / 3))
7450 && IT_CHARPOS (*it) > BEGV)
7452 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7453 target_y - it->current_y));
7454 dy = it->current_y - target_y;
7455 goto move_further_back;
7457 else if (target_y >= it->current_y + line_height
7458 && IT_CHARPOS (*it) < ZV)
7460 /* Should move forward by at least one line, maybe more.
7462 Note: Calling move_it_by_lines can be expensive on
7463 terminal frames, where compute_motion is used (via
7464 vmotion) to do the job, when there are very long lines
7465 and truncate-lines is nil. That's the reason for
7466 treating terminal frames specially here. */
7468 if (!FRAME_WINDOW_P (it->f))
7469 move_it_vertically (it, target_y - (it->current_y + line_height));
7470 else
7474 move_it_by_lines (it, 1, 1);
7476 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7483 /* Move IT by a specified amount of pixel lines DY. DY negative means
7484 move backwards. DY = 0 means move to start of screen line. At the
7485 end, IT will be on the start of a screen line. */
7487 void
7488 move_it_vertically (it, dy)
7489 struct it *it;
7490 int dy;
7492 if (dy <= 0)
7493 move_it_vertically_backward (it, -dy);
7494 else
7496 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7497 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7498 MOVE_TO_POS | MOVE_TO_Y);
7499 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7501 /* If buffer ends in ZV without a newline, move to the start of
7502 the line to satisfy the post-condition. */
7503 if (IT_CHARPOS (*it) == ZV
7504 && ZV > BEGV
7505 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7506 move_it_by_lines (it, 0, 0);
7511 /* Move iterator IT past the end of the text line it is in. */
7513 void
7514 move_it_past_eol (it)
7515 struct it *it;
7517 enum move_it_result rc;
7519 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7520 if (rc == MOVE_NEWLINE_OR_CR)
7521 set_iterator_to_next (it, 0);
7525 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7526 negative means move up. DVPOS == 0 means move to the start of the
7527 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7528 NEED_Y_P is zero, IT->current_y will be left unchanged.
7530 Further optimization ideas: If we would know that IT->f doesn't use
7531 a face with proportional font, we could be faster for
7532 truncate-lines nil. */
7534 void
7535 move_it_by_lines (it, dvpos, need_y_p)
7536 struct it *it;
7537 int dvpos, need_y_p;
7539 struct position pos;
7541 /* The commented-out optimization uses vmotion on terminals. This
7542 gives bad results, because elements like it->what, on which
7543 callers such as pos_visible_p rely, aren't updated. */
7544 /* if (!FRAME_WINDOW_P (it->f))
7546 struct text_pos textpos;
7548 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7549 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7550 reseat (it, textpos, 1);
7551 it->vpos += pos.vpos;
7552 it->current_y += pos.vpos;
7554 else */
7556 if (dvpos == 0)
7558 /* DVPOS == 0 means move to the start of the screen line. */
7559 move_it_vertically_backward (it, 0);
7560 xassert (it->current_x == 0 && it->hpos == 0);
7561 /* Let next call to line_bottom_y calculate real line height */
7562 last_height = 0;
7564 else if (dvpos > 0)
7566 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7567 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7568 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7570 else
7572 struct it it2;
7573 int start_charpos, i;
7575 /* Start at the beginning of the screen line containing IT's
7576 position. This may actually move vertically backwards,
7577 in case of overlays, so adjust dvpos accordingly. */
7578 dvpos += it->vpos;
7579 move_it_vertically_backward (it, 0);
7580 dvpos -= it->vpos;
7582 /* Go back -DVPOS visible lines and reseat the iterator there. */
7583 start_charpos = IT_CHARPOS (*it);
7584 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7585 back_to_previous_visible_line_start (it);
7586 reseat (it, it->current.pos, 1);
7588 /* Move further back if we end up in a string or an image. */
7589 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7591 /* First try to move to start of display line. */
7592 dvpos += it->vpos;
7593 move_it_vertically_backward (it, 0);
7594 dvpos -= it->vpos;
7595 if (IT_POS_VALID_AFTER_MOVE_P (it))
7596 break;
7597 /* If start of line is still in string or image,
7598 move further back. */
7599 back_to_previous_visible_line_start (it);
7600 reseat (it, it->current.pos, 1);
7601 dvpos--;
7604 it->current_x = it->hpos = 0;
7606 /* Above call may have moved too far if continuation lines
7607 are involved. Scan forward and see if it did. */
7608 it2 = *it;
7609 it2.vpos = it2.current_y = 0;
7610 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7611 it->vpos -= it2.vpos;
7612 it->current_y -= it2.current_y;
7613 it->current_x = it->hpos = 0;
7615 /* If we moved too far back, move IT some lines forward. */
7616 if (it2.vpos > -dvpos)
7618 int delta = it2.vpos + dvpos;
7619 it2 = *it;
7620 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7621 /* Move back again if we got too far ahead. */
7622 if (IT_CHARPOS (*it) >= start_charpos)
7623 *it = it2;
7628 /* Return 1 if IT points into the middle of a display vector. */
7631 in_display_vector_p (it)
7632 struct it *it;
7634 return (it->method == GET_FROM_DISPLAY_VECTOR
7635 && it->current.dpvec_index > 0
7636 && it->dpvec + it->current.dpvec_index != it->dpend);
7640 /***********************************************************************
7641 Messages
7642 ***********************************************************************/
7645 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7646 to *Messages*. */
7648 void
7649 add_to_log (format, arg1, arg2)
7650 char *format;
7651 Lisp_Object arg1, arg2;
7653 Lisp_Object args[3];
7654 Lisp_Object msg, fmt;
7655 char *buffer;
7656 int len;
7657 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7658 USE_SAFE_ALLOCA;
7660 /* Do nothing if called asynchronously. Inserting text into
7661 a buffer may call after-change-functions and alike and
7662 that would means running Lisp asynchronously. */
7663 if (handling_signal)
7664 return;
7666 fmt = msg = Qnil;
7667 GCPRO4 (fmt, msg, arg1, arg2);
7669 args[0] = fmt = build_string (format);
7670 args[1] = arg1;
7671 args[2] = arg2;
7672 msg = Fformat (3, args);
7674 len = SBYTES (msg) + 1;
7675 SAFE_ALLOCA (buffer, char *, len);
7676 bcopy (SDATA (msg), buffer, len);
7678 message_dolog (buffer, len - 1, 1, 0);
7679 SAFE_FREE ();
7681 UNGCPRO;
7685 /* Output a newline in the *Messages* buffer if "needs" one. */
7687 void
7688 message_log_maybe_newline ()
7690 if (message_log_need_newline)
7691 message_dolog ("", 0, 1, 0);
7695 /* Add a string M of length NBYTES to the message log, optionally
7696 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7697 nonzero, means interpret the contents of M as multibyte. This
7698 function calls low-level routines in order to bypass text property
7699 hooks, etc. which might not be safe to run.
7701 This may GC (insert may run before/after change hooks),
7702 so the buffer M must NOT point to a Lisp string. */
7704 void
7705 message_dolog (m, nbytes, nlflag, multibyte)
7706 const char *m;
7707 int nbytes, nlflag, multibyte;
7709 if (!NILP (Vmemory_full))
7710 return;
7712 if (!NILP (Vmessage_log_max))
7714 struct buffer *oldbuf;
7715 Lisp_Object oldpoint, oldbegv, oldzv;
7716 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7717 int point_at_end = 0;
7718 int zv_at_end = 0;
7719 Lisp_Object old_deactivate_mark, tem;
7720 struct gcpro gcpro1;
7722 old_deactivate_mark = Vdeactivate_mark;
7723 oldbuf = current_buffer;
7724 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7725 current_buffer->undo_list = Qt;
7727 oldpoint = message_dolog_marker1;
7728 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7729 oldbegv = message_dolog_marker2;
7730 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7731 oldzv = message_dolog_marker3;
7732 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7733 GCPRO1 (old_deactivate_mark);
7735 if (PT == Z)
7736 point_at_end = 1;
7737 if (ZV == Z)
7738 zv_at_end = 1;
7740 BEGV = BEG;
7741 BEGV_BYTE = BEG_BYTE;
7742 ZV = Z;
7743 ZV_BYTE = Z_BYTE;
7744 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7746 /* Insert the string--maybe converting multibyte to single byte
7747 or vice versa, so that all the text fits the buffer. */
7748 if (multibyte
7749 && NILP (current_buffer->enable_multibyte_characters))
7751 int i, c, char_bytes;
7752 unsigned char work[1];
7754 /* Convert a multibyte string to single-byte
7755 for the *Message* buffer. */
7756 for (i = 0; i < nbytes; i += char_bytes)
7758 c = string_char_and_length (m + i, &char_bytes);
7759 work[0] = (ASCII_CHAR_P (c)
7761 : multibyte_char_to_unibyte (c, Qnil));
7762 insert_1_both (work, 1, 1, 1, 0, 0);
7765 else if (! multibyte
7766 && ! NILP (current_buffer->enable_multibyte_characters))
7768 int i, c, char_bytes;
7769 unsigned char *msg = (unsigned char *) m;
7770 unsigned char str[MAX_MULTIBYTE_LENGTH];
7771 /* Convert a single-byte string to multibyte
7772 for the *Message* buffer. */
7773 for (i = 0; i < nbytes; i++)
7775 c = msg[i];
7776 MAKE_CHAR_MULTIBYTE (c);
7777 char_bytes = CHAR_STRING (c, str);
7778 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7781 else if (nbytes)
7782 insert_1 (m, nbytes, 1, 0, 0);
7784 if (nlflag)
7786 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7787 insert_1 ("\n", 1, 1, 0, 0);
7789 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7790 this_bol = PT;
7791 this_bol_byte = PT_BYTE;
7793 /* See if this line duplicates the previous one.
7794 If so, combine duplicates. */
7795 if (this_bol > BEG)
7797 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7798 prev_bol = PT;
7799 prev_bol_byte = PT_BYTE;
7801 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7802 this_bol, this_bol_byte);
7803 if (dup)
7805 del_range_both (prev_bol, prev_bol_byte,
7806 this_bol, this_bol_byte, 0);
7807 if (dup > 1)
7809 char dupstr[40];
7810 int duplen;
7812 /* If you change this format, don't forget to also
7813 change message_log_check_duplicate. */
7814 sprintf (dupstr, " [%d times]", dup);
7815 duplen = strlen (dupstr);
7816 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7817 insert_1 (dupstr, duplen, 1, 0, 1);
7822 /* If we have more than the desired maximum number of lines
7823 in the *Messages* buffer now, delete the oldest ones.
7824 This is safe because we don't have undo in this buffer. */
7826 if (NATNUMP (Vmessage_log_max))
7828 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7829 -XFASTINT (Vmessage_log_max) - 1, 0);
7830 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7833 BEGV = XMARKER (oldbegv)->charpos;
7834 BEGV_BYTE = marker_byte_position (oldbegv);
7836 if (zv_at_end)
7838 ZV = Z;
7839 ZV_BYTE = Z_BYTE;
7841 else
7843 ZV = XMARKER (oldzv)->charpos;
7844 ZV_BYTE = marker_byte_position (oldzv);
7847 if (point_at_end)
7848 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7849 else
7850 /* We can't do Fgoto_char (oldpoint) because it will run some
7851 Lisp code. */
7852 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7853 XMARKER (oldpoint)->bytepos);
7855 UNGCPRO;
7856 unchain_marker (XMARKER (oldpoint));
7857 unchain_marker (XMARKER (oldbegv));
7858 unchain_marker (XMARKER (oldzv));
7860 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7861 set_buffer_internal (oldbuf);
7862 if (NILP (tem))
7863 windows_or_buffers_changed = old_windows_or_buffers_changed;
7864 message_log_need_newline = !nlflag;
7865 Vdeactivate_mark = old_deactivate_mark;
7870 /* We are at the end of the buffer after just having inserted a newline.
7871 (Note: We depend on the fact we won't be crossing the gap.)
7872 Check to see if the most recent message looks a lot like the previous one.
7873 Return 0 if different, 1 if the new one should just replace it, or a
7874 value N > 1 if we should also append " [N times]". */
7876 static int
7877 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7878 int prev_bol, this_bol;
7879 int prev_bol_byte, this_bol_byte;
7881 int i;
7882 int len = Z_BYTE - 1 - this_bol_byte;
7883 int seen_dots = 0;
7884 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7885 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7887 for (i = 0; i < len; i++)
7889 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7890 seen_dots = 1;
7891 if (p1[i] != p2[i])
7892 return seen_dots;
7894 p1 += len;
7895 if (*p1 == '\n')
7896 return 2;
7897 if (*p1++ == ' ' && *p1++ == '[')
7899 int n = 0;
7900 while (*p1 >= '0' && *p1 <= '9')
7901 n = n * 10 + *p1++ - '0';
7902 if (strncmp (p1, " times]\n", 8) == 0)
7903 return n+1;
7905 return 0;
7909 /* Display an echo area message M with a specified length of NBYTES
7910 bytes. The string may include null characters. If M is 0, clear
7911 out any existing message, and let the mini-buffer text show
7912 through.
7914 This may GC, so the buffer M must NOT point to a Lisp string. */
7916 void
7917 message2 (m, nbytes, multibyte)
7918 const char *m;
7919 int nbytes;
7920 int multibyte;
7922 /* First flush out any partial line written with print. */
7923 message_log_maybe_newline ();
7924 if (m)
7925 message_dolog (m, nbytes, 1, multibyte);
7926 message2_nolog (m, nbytes, multibyte);
7930 /* The non-logging counterpart of message2. */
7932 void
7933 message2_nolog (m, nbytes, multibyte)
7934 const char *m;
7935 int nbytes, multibyte;
7937 struct frame *sf = SELECTED_FRAME ();
7938 message_enable_multibyte = multibyte;
7940 if (FRAME_INITIAL_P (sf))
7942 if (noninteractive_need_newline)
7943 putc ('\n', stderr);
7944 noninteractive_need_newline = 0;
7945 if (m)
7946 fwrite (m, nbytes, 1, stderr);
7947 if (cursor_in_echo_area == 0)
7948 fprintf (stderr, "\n");
7949 fflush (stderr);
7951 /* A null message buffer means that the frame hasn't really been
7952 initialized yet. Error messages get reported properly by
7953 cmd_error, so this must be just an informative message; toss it. */
7954 else if (INTERACTIVE
7955 && sf->glyphs_initialized_p
7956 && FRAME_MESSAGE_BUF (sf))
7958 Lisp_Object mini_window;
7959 struct frame *f;
7961 /* Get the frame containing the mini-buffer
7962 that the selected frame is using. */
7963 mini_window = FRAME_MINIBUF_WINDOW (sf);
7964 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7966 FRAME_SAMPLE_VISIBILITY (f);
7967 if (FRAME_VISIBLE_P (sf)
7968 && ! FRAME_VISIBLE_P (f))
7969 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7971 if (m)
7973 set_message (m, Qnil, nbytes, multibyte);
7974 if (minibuffer_auto_raise)
7975 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7977 else
7978 clear_message (1, 1);
7980 do_pending_window_change (0);
7981 echo_area_display (1);
7982 do_pending_window_change (0);
7983 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7984 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7989 /* Display an echo area message M with a specified length of NBYTES
7990 bytes. The string may include null characters. If M is not a
7991 string, clear out any existing message, and let the mini-buffer
7992 text show through.
7994 This function cancels echoing. */
7996 void
7997 message3 (m, nbytes, multibyte)
7998 Lisp_Object m;
7999 int nbytes;
8000 int multibyte;
8002 struct gcpro gcpro1;
8004 GCPRO1 (m);
8005 clear_message (1,1);
8006 cancel_echoing ();
8008 /* First flush out any partial line written with print. */
8009 message_log_maybe_newline ();
8010 if (STRINGP (m))
8012 char *buffer;
8013 USE_SAFE_ALLOCA;
8015 SAFE_ALLOCA (buffer, char *, nbytes);
8016 bcopy (SDATA (m), buffer, nbytes);
8017 message_dolog (buffer, nbytes, 1, multibyte);
8018 SAFE_FREE ();
8020 message3_nolog (m, nbytes, multibyte);
8022 UNGCPRO;
8026 /* The non-logging version of message3.
8027 This does not cancel echoing, because it is used for echoing.
8028 Perhaps we need to make a separate function for echoing
8029 and make this cancel echoing. */
8031 void
8032 message3_nolog (m, nbytes, multibyte)
8033 Lisp_Object m;
8034 int nbytes, multibyte;
8036 struct frame *sf = SELECTED_FRAME ();
8037 message_enable_multibyte = multibyte;
8039 if (FRAME_INITIAL_P (sf))
8041 if (noninteractive_need_newline)
8042 putc ('\n', stderr);
8043 noninteractive_need_newline = 0;
8044 if (STRINGP (m))
8045 fwrite (SDATA (m), nbytes, 1, stderr);
8046 if (cursor_in_echo_area == 0)
8047 fprintf (stderr, "\n");
8048 fflush (stderr);
8050 /* A null message buffer means that the frame hasn't really been
8051 initialized yet. Error messages get reported properly by
8052 cmd_error, so this must be just an informative message; toss it. */
8053 else if (INTERACTIVE
8054 && sf->glyphs_initialized_p
8055 && FRAME_MESSAGE_BUF (sf))
8057 Lisp_Object mini_window;
8058 Lisp_Object frame;
8059 struct frame *f;
8061 /* Get the frame containing the mini-buffer
8062 that the selected frame is using. */
8063 mini_window = FRAME_MINIBUF_WINDOW (sf);
8064 frame = XWINDOW (mini_window)->frame;
8065 f = XFRAME (frame);
8067 FRAME_SAMPLE_VISIBILITY (f);
8068 if (FRAME_VISIBLE_P (sf)
8069 && !FRAME_VISIBLE_P (f))
8070 Fmake_frame_visible (frame);
8072 if (STRINGP (m) && SCHARS (m) > 0)
8074 set_message (NULL, m, nbytes, multibyte);
8075 if (minibuffer_auto_raise)
8076 Fraise_frame (frame);
8077 /* Assume we are not echoing.
8078 (If we are, echo_now will override this.) */
8079 echo_message_buffer = Qnil;
8081 else
8082 clear_message (1, 1);
8084 do_pending_window_change (0);
8085 echo_area_display (1);
8086 do_pending_window_change (0);
8087 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8088 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8093 /* Display a null-terminated echo area message M. If M is 0, clear
8094 out any existing message, and let the mini-buffer text show through.
8096 The buffer M must continue to exist until after the echo area gets
8097 cleared or some other message gets displayed there. Do not pass
8098 text that is stored in a Lisp string. Do not pass text in a buffer
8099 that was alloca'd. */
8101 void
8102 message1 (m)
8103 char *m;
8105 message2 (m, (m ? strlen (m) : 0), 0);
8109 /* The non-logging counterpart of message1. */
8111 void
8112 message1_nolog (m)
8113 char *m;
8115 message2_nolog (m, (m ? strlen (m) : 0), 0);
8118 /* Display a message M which contains a single %s
8119 which gets replaced with STRING. */
8121 void
8122 message_with_string (m, string, log)
8123 char *m;
8124 Lisp_Object string;
8125 int log;
8127 CHECK_STRING (string);
8129 if (noninteractive)
8131 if (m)
8133 if (noninteractive_need_newline)
8134 putc ('\n', stderr);
8135 noninteractive_need_newline = 0;
8136 fprintf (stderr, m, SDATA (string));
8137 if (!cursor_in_echo_area)
8138 fprintf (stderr, "\n");
8139 fflush (stderr);
8142 else if (INTERACTIVE)
8144 /* The frame whose minibuffer we're going to display the message on.
8145 It may be larger than the selected frame, so we need
8146 to use its buffer, not the selected frame's buffer. */
8147 Lisp_Object mini_window;
8148 struct frame *f, *sf = SELECTED_FRAME ();
8150 /* Get the frame containing the minibuffer
8151 that the selected frame is using. */
8152 mini_window = FRAME_MINIBUF_WINDOW (sf);
8153 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8155 /* A null message buffer means that the frame hasn't really been
8156 initialized yet. Error messages get reported properly by
8157 cmd_error, so this must be just an informative message; toss it. */
8158 if (FRAME_MESSAGE_BUF (f))
8160 Lisp_Object args[2], message;
8161 struct gcpro gcpro1, gcpro2;
8163 args[0] = build_string (m);
8164 args[1] = message = string;
8165 GCPRO2 (args[0], message);
8166 gcpro1.nvars = 2;
8168 message = Fformat (2, args);
8170 if (log)
8171 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8172 else
8173 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8175 UNGCPRO;
8177 /* Print should start at the beginning of the message
8178 buffer next time. */
8179 message_buf_print = 0;
8185 /* Dump an informative message to the minibuf. If M is 0, clear out
8186 any existing message, and let the mini-buffer text show through. */
8188 /* VARARGS 1 */
8189 void
8190 message (m, a1, a2, a3)
8191 char *m;
8192 EMACS_INT a1, a2, a3;
8194 if (noninteractive)
8196 if (m)
8198 if (noninteractive_need_newline)
8199 putc ('\n', stderr);
8200 noninteractive_need_newline = 0;
8201 fprintf (stderr, m, a1, a2, a3);
8202 if (cursor_in_echo_area == 0)
8203 fprintf (stderr, "\n");
8204 fflush (stderr);
8207 else if (INTERACTIVE)
8209 /* The frame whose mini-buffer we're going to display the message
8210 on. It may be larger than the selected frame, so we need to
8211 use its buffer, not the selected frame's buffer. */
8212 Lisp_Object mini_window;
8213 struct frame *f, *sf = SELECTED_FRAME ();
8215 /* Get the frame containing the mini-buffer
8216 that the selected frame is using. */
8217 mini_window = FRAME_MINIBUF_WINDOW (sf);
8218 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8220 /* A null message buffer means that the frame hasn't really been
8221 initialized yet. Error messages get reported properly by
8222 cmd_error, so this must be just an informative message; toss
8223 it. */
8224 if (FRAME_MESSAGE_BUF (f))
8226 if (m)
8228 int len;
8229 #ifdef NO_ARG_ARRAY
8230 char *a[3];
8231 a[0] = (char *) a1;
8232 a[1] = (char *) a2;
8233 a[2] = (char *) a3;
8235 len = doprnt (FRAME_MESSAGE_BUF (f),
8236 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8237 #else
8238 len = doprnt (FRAME_MESSAGE_BUF (f),
8239 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8240 (char **) &a1);
8241 #endif /* NO_ARG_ARRAY */
8243 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8245 else
8246 message1 (0);
8248 /* Print should start at the beginning of the message
8249 buffer next time. */
8250 message_buf_print = 0;
8256 /* The non-logging version of message. */
8258 void
8259 message_nolog (m, a1, a2, a3)
8260 char *m;
8261 EMACS_INT a1, a2, a3;
8263 Lisp_Object old_log_max;
8264 old_log_max = Vmessage_log_max;
8265 Vmessage_log_max = Qnil;
8266 message (m, a1, a2, a3);
8267 Vmessage_log_max = old_log_max;
8271 /* Display the current message in the current mini-buffer. This is
8272 only called from error handlers in process.c, and is not time
8273 critical. */
8275 void
8276 update_echo_area ()
8278 if (!NILP (echo_area_buffer[0]))
8280 Lisp_Object string;
8281 string = Fcurrent_message ();
8282 message3 (string, SBYTES (string),
8283 !NILP (current_buffer->enable_multibyte_characters));
8288 /* Make sure echo area buffers in `echo_buffers' are live.
8289 If they aren't, make new ones. */
8291 static void
8292 ensure_echo_area_buffers ()
8294 int i;
8296 for (i = 0; i < 2; ++i)
8297 if (!BUFFERP (echo_buffer[i])
8298 || NILP (XBUFFER (echo_buffer[i])->name))
8300 char name[30];
8301 Lisp_Object old_buffer;
8302 int j;
8304 old_buffer = echo_buffer[i];
8305 sprintf (name, " *Echo Area %d*", i);
8306 echo_buffer[i] = Fget_buffer_create (build_string (name));
8307 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8308 /* to force word wrap in echo area -
8309 it was decided to postpone this*/
8310 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8312 for (j = 0; j < 2; ++j)
8313 if (EQ (old_buffer, echo_area_buffer[j]))
8314 echo_area_buffer[j] = echo_buffer[i];
8319 /* Call FN with args A1..A4 with either the current or last displayed
8320 echo_area_buffer as current buffer.
8322 WHICH zero means use the current message buffer
8323 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8324 from echo_buffer[] and clear it.
8326 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8327 suitable buffer from echo_buffer[] and clear it.
8329 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8330 that the current message becomes the last displayed one, make
8331 choose a suitable buffer for echo_area_buffer[0], and clear it.
8333 Value is what FN returns. */
8335 static int
8336 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8337 struct window *w;
8338 int which;
8339 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8340 EMACS_INT a1;
8341 Lisp_Object a2;
8342 EMACS_INT a3, a4;
8344 Lisp_Object buffer;
8345 int this_one, the_other, clear_buffer_p, rc;
8346 int count = SPECPDL_INDEX ();
8348 /* If buffers aren't live, make new ones. */
8349 ensure_echo_area_buffers ();
8351 clear_buffer_p = 0;
8353 if (which == 0)
8354 this_one = 0, the_other = 1;
8355 else if (which > 0)
8356 this_one = 1, the_other = 0;
8357 else
8359 this_one = 0, the_other = 1;
8360 clear_buffer_p = 1;
8362 /* We need a fresh one in case the current echo buffer equals
8363 the one containing the last displayed echo area message. */
8364 if (!NILP (echo_area_buffer[this_one])
8365 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8366 echo_area_buffer[this_one] = Qnil;
8369 /* Choose a suitable buffer from echo_buffer[] is we don't
8370 have one. */
8371 if (NILP (echo_area_buffer[this_one]))
8373 echo_area_buffer[this_one]
8374 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8375 ? echo_buffer[the_other]
8376 : echo_buffer[this_one]);
8377 clear_buffer_p = 1;
8380 buffer = echo_area_buffer[this_one];
8382 /* Don't get confused by reusing the buffer used for echoing
8383 for a different purpose. */
8384 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8385 cancel_echoing ();
8387 record_unwind_protect (unwind_with_echo_area_buffer,
8388 with_echo_area_buffer_unwind_data (w));
8390 /* Make the echo area buffer current. Note that for display
8391 purposes, it is not necessary that the displayed window's buffer
8392 == current_buffer, except for text property lookup. So, let's
8393 only set that buffer temporarily here without doing a full
8394 Fset_window_buffer. We must also change w->pointm, though,
8395 because otherwise an assertions in unshow_buffer fails, and Emacs
8396 aborts. */
8397 set_buffer_internal_1 (XBUFFER (buffer));
8398 if (w)
8400 w->buffer = buffer;
8401 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8404 current_buffer->undo_list = Qt;
8405 current_buffer->read_only = Qnil;
8406 specbind (Qinhibit_read_only, Qt);
8407 specbind (Qinhibit_modification_hooks, Qt);
8409 if (clear_buffer_p && Z > BEG)
8410 del_range (BEG, Z);
8412 xassert (BEGV >= BEG);
8413 xassert (ZV <= Z && ZV >= BEGV);
8415 rc = fn (a1, a2, a3, a4);
8417 xassert (BEGV >= BEG);
8418 xassert (ZV <= Z && ZV >= BEGV);
8420 unbind_to (count, Qnil);
8421 return rc;
8425 /* Save state that should be preserved around the call to the function
8426 FN called in with_echo_area_buffer. */
8428 static Lisp_Object
8429 with_echo_area_buffer_unwind_data (w)
8430 struct window *w;
8432 int i = 0;
8433 Lisp_Object vector, tmp;
8435 /* Reduce consing by keeping one vector in
8436 Vwith_echo_area_save_vector. */
8437 vector = Vwith_echo_area_save_vector;
8438 Vwith_echo_area_save_vector = Qnil;
8440 if (NILP (vector))
8441 vector = Fmake_vector (make_number (7), Qnil);
8443 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8444 ASET (vector, i, Vdeactivate_mark); ++i;
8445 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8447 if (w)
8449 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8450 ASET (vector, i, w->buffer); ++i;
8451 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8452 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8454 else
8456 int end = i + 4;
8457 for (; i < end; ++i)
8458 ASET (vector, i, Qnil);
8461 xassert (i == ASIZE (vector));
8462 return vector;
8466 /* Restore global state from VECTOR which was created by
8467 with_echo_area_buffer_unwind_data. */
8469 static Lisp_Object
8470 unwind_with_echo_area_buffer (vector)
8471 Lisp_Object vector;
8473 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8474 Vdeactivate_mark = AREF (vector, 1);
8475 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8477 if (WINDOWP (AREF (vector, 3)))
8479 struct window *w;
8480 Lisp_Object buffer, charpos, bytepos;
8482 w = XWINDOW (AREF (vector, 3));
8483 buffer = AREF (vector, 4);
8484 charpos = AREF (vector, 5);
8485 bytepos = AREF (vector, 6);
8487 w->buffer = buffer;
8488 set_marker_both (w->pointm, buffer,
8489 XFASTINT (charpos), XFASTINT (bytepos));
8492 Vwith_echo_area_save_vector = vector;
8493 return Qnil;
8497 /* Set up the echo area for use by print functions. MULTIBYTE_P
8498 non-zero means we will print multibyte. */
8500 void
8501 setup_echo_area_for_printing (multibyte_p)
8502 int multibyte_p;
8504 /* If we can't find an echo area any more, exit. */
8505 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8506 Fkill_emacs (Qnil);
8508 ensure_echo_area_buffers ();
8510 if (!message_buf_print)
8512 /* A message has been output since the last time we printed.
8513 Choose a fresh echo area buffer. */
8514 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8515 echo_area_buffer[0] = echo_buffer[1];
8516 else
8517 echo_area_buffer[0] = echo_buffer[0];
8519 /* Switch to that buffer and clear it. */
8520 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8521 current_buffer->truncate_lines = Qnil;
8523 if (Z > BEG)
8525 int count = SPECPDL_INDEX ();
8526 specbind (Qinhibit_read_only, Qt);
8527 /* Note that undo recording is always disabled. */
8528 del_range (BEG, Z);
8529 unbind_to (count, Qnil);
8531 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8533 /* Set up the buffer for the multibyteness we need. */
8534 if (multibyte_p
8535 != !NILP (current_buffer->enable_multibyte_characters))
8536 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8538 /* Raise the frame containing the echo area. */
8539 if (minibuffer_auto_raise)
8541 struct frame *sf = SELECTED_FRAME ();
8542 Lisp_Object mini_window;
8543 mini_window = FRAME_MINIBUF_WINDOW (sf);
8544 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8547 message_log_maybe_newline ();
8548 message_buf_print = 1;
8550 else
8552 if (NILP (echo_area_buffer[0]))
8554 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8555 echo_area_buffer[0] = echo_buffer[1];
8556 else
8557 echo_area_buffer[0] = echo_buffer[0];
8560 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8562 /* Someone switched buffers between print requests. */
8563 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8564 current_buffer->truncate_lines = Qnil;
8570 /* Display an echo area message in window W. Value is non-zero if W's
8571 height is changed. If display_last_displayed_message_p is
8572 non-zero, display the message that was last displayed, otherwise
8573 display the current message. */
8575 static int
8576 display_echo_area (w)
8577 struct window *w;
8579 int i, no_message_p, window_height_changed_p, count;
8581 /* Temporarily disable garbage collections while displaying the echo
8582 area. This is done because a GC can print a message itself.
8583 That message would modify the echo area buffer's contents while a
8584 redisplay of the buffer is going on, and seriously confuse
8585 redisplay. */
8586 count = inhibit_garbage_collection ();
8588 /* If there is no message, we must call display_echo_area_1
8589 nevertheless because it resizes the window. But we will have to
8590 reset the echo_area_buffer in question to nil at the end because
8591 with_echo_area_buffer will sets it to an empty buffer. */
8592 i = display_last_displayed_message_p ? 1 : 0;
8593 no_message_p = NILP (echo_area_buffer[i]);
8595 window_height_changed_p
8596 = with_echo_area_buffer (w, display_last_displayed_message_p,
8597 display_echo_area_1,
8598 (EMACS_INT) w, Qnil, 0, 0);
8600 if (no_message_p)
8601 echo_area_buffer[i] = Qnil;
8603 unbind_to (count, Qnil);
8604 return window_height_changed_p;
8608 /* Helper for display_echo_area. Display the current buffer which
8609 contains the current echo area message in window W, a mini-window,
8610 a pointer to which is passed in A1. A2..A4 are currently not used.
8611 Change the height of W so that all of the message is displayed.
8612 Value is non-zero if height of W was changed. */
8614 static int
8615 display_echo_area_1 (a1, a2, a3, a4)
8616 EMACS_INT a1;
8617 Lisp_Object a2;
8618 EMACS_INT a3, a4;
8620 struct window *w = (struct window *) a1;
8621 Lisp_Object window;
8622 struct text_pos start;
8623 int window_height_changed_p = 0;
8625 /* Do this before displaying, so that we have a large enough glyph
8626 matrix for the display. If we can't get enough space for the
8627 whole text, display the last N lines. That works by setting w->start. */
8628 window_height_changed_p = resize_mini_window (w, 0);
8630 /* Use the starting position chosen by resize_mini_window. */
8631 SET_TEXT_POS_FROM_MARKER (start, w->start);
8633 /* Display. */
8634 clear_glyph_matrix (w->desired_matrix);
8635 XSETWINDOW (window, w);
8636 try_window (window, start, 0);
8638 return window_height_changed_p;
8642 /* Resize the echo area window to exactly the size needed for the
8643 currently displayed message, if there is one. If a mini-buffer
8644 is active, don't shrink it. */
8646 void
8647 resize_echo_area_exactly ()
8649 if (BUFFERP (echo_area_buffer[0])
8650 && WINDOWP (echo_area_window))
8652 struct window *w = XWINDOW (echo_area_window);
8653 int resized_p;
8654 Lisp_Object resize_exactly;
8656 if (minibuf_level == 0)
8657 resize_exactly = Qt;
8658 else
8659 resize_exactly = Qnil;
8661 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8662 (EMACS_INT) w, resize_exactly, 0, 0);
8663 if (resized_p)
8665 ++windows_or_buffers_changed;
8666 ++update_mode_lines;
8667 redisplay_internal (0);
8673 /* Callback function for with_echo_area_buffer, when used from
8674 resize_echo_area_exactly. A1 contains a pointer to the window to
8675 resize, EXACTLY non-nil means resize the mini-window exactly to the
8676 size of the text displayed. A3 and A4 are not used. Value is what
8677 resize_mini_window returns. */
8679 static int
8680 resize_mini_window_1 (a1, exactly, a3, a4)
8681 EMACS_INT a1;
8682 Lisp_Object exactly;
8683 EMACS_INT a3, a4;
8685 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8689 /* Resize mini-window W to fit the size of its contents. EXACT_P
8690 means size the window exactly to the size needed. Otherwise, it's
8691 only enlarged until W's buffer is empty.
8693 Set W->start to the right place to begin display. If the whole
8694 contents fit, start at the beginning. Otherwise, start so as
8695 to make the end of the contents appear. This is particularly
8696 important for y-or-n-p, but seems desirable generally.
8698 Value is non-zero if the window height has been changed. */
8701 resize_mini_window (w, exact_p)
8702 struct window *w;
8703 int exact_p;
8705 struct frame *f = XFRAME (w->frame);
8706 int window_height_changed_p = 0;
8708 xassert (MINI_WINDOW_P (w));
8710 /* By default, start display at the beginning. */
8711 set_marker_both (w->start, w->buffer,
8712 BUF_BEGV (XBUFFER (w->buffer)),
8713 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8715 /* Don't resize windows while redisplaying a window; it would
8716 confuse redisplay functions when the size of the window they are
8717 displaying changes from under them. Such a resizing can happen,
8718 for instance, when which-func prints a long message while
8719 we are running fontification-functions. We're running these
8720 functions with safe_call which binds inhibit-redisplay to t. */
8721 if (!NILP (Vinhibit_redisplay))
8722 return 0;
8724 /* Nil means don't try to resize. */
8725 if (NILP (Vresize_mini_windows)
8726 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8727 return 0;
8729 if (!FRAME_MINIBUF_ONLY_P (f))
8731 struct it it;
8732 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8733 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8734 int height, max_height;
8735 int unit = FRAME_LINE_HEIGHT (f);
8736 struct text_pos start;
8737 struct buffer *old_current_buffer = NULL;
8739 if (current_buffer != XBUFFER (w->buffer))
8741 old_current_buffer = current_buffer;
8742 set_buffer_internal (XBUFFER (w->buffer));
8745 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8747 /* Compute the max. number of lines specified by the user. */
8748 if (FLOATP (Vmax_mini_window_height))
8749 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8750 else if (INTEGERP (Vmax_mini_window_height))
8751 max_height = XINT (Vmax_mini_window_height);
8752 else
8753 max_height = total_height / 4;
8755 /* Correct that max. height if it's bogus. */
8756 max_height = max (1, max_height);
8757 max_height = min (total_height, max_height);
8759 /* Find out the height of the text in the window. */
8760 if (it.line_wrap == TRUNCATE)
8761 height = 1;
8762 else
8764 last_height = 0;
8765 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8766 if (it.max_ascent == 0 && it.max_descent == 0)
8767 height = it.current_y + last_height;
8768 else
8769 height = it.current_y + it.max_ascent + it.max_descent;
8770 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8771 height = (height + unit - 1) / unit;
8774 /* Compute a suitable window start. */
8775 if (height > max_height)
8777 height = max_height;
8778 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8779 move_it_vertically_backward (&it, (height - 1) * unit);
8780 start = it.current.pos;
8782 else
8783 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8784 SET_MARKER_FROM_TEXT_POS (w->start, start);
8786 if (EQ (Vresize_mini_windows, Qgrow_only))
8788 /* Let it grow only, until we display an empty message, in which
8789 case the window shrinks again. */
8790 if (height > WINDOW_TOTAL_LINES (w))
8792 int old_height = WINDOW_TOTAL_LINES (w);
8793 freeze_window_starts (f, 1);
8794 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8795 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8797 else if (height < WINDOW_TOTAL_LINES (w)
8798 && (exact_p || BEGV == ZV))
8800 int old_height = WINDOW_TOTAL_LINES (w);
8801 freeze_window_starts (f, 0);
8802 shrink_mini_window (w);
8803 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8806 else
8808 /* Always resize to exact size needed. */
8809 if (height > WINDOW_TOTAL_LINES (w))
8811 int old_height = WINDOW_TOTAL_LINES (w);
8812 freeze_window_starts (f, 1);
8813 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8814 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8816 else if (height < WINDOW_TOTAL_LINES (w))
8818 int old_height = WINDOW_TOTAL_LINES (w);
8819 freeze_window_starts (f, 0);
8820 shrink_mini_window (w);
8822 if (height)
8824 freeze_window_starts (f, 1);
8825 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8828 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8832 if (old_current_buffer)
8833 set_buffer_internal (old_current_buffer);
8836 return window_height_changed_p;
8840 /* Value is the current message, a string, or nil if there is no
8841 current message. */
8843 Lisp_Object
8844 current_message ()
8846 Lisp_Object msg;
8848 if (!BUFFERP (echo_area_buffer[0]))
8849 msg = Qnil;
8850 else
8852 with_echo_area_buffer (0, 0, current_message_1,
8853 (EMACS_INT) &msg, Qnil, 0, 0);
8854 if (NILP (msg))
8855 echo_area_buffer[0] = Qnil;
8858 return msg;
8862 static int
8863 current_message_1 (a1, a2, a3, a4)
8864 EMACS_INT a1;
8865 Lisp_Object a2;
8866 EMACS_INT a3, a4;
8868 Lisp_Object *msg = (Lisp_Object *) a1;
8870 if (Z > BEG)
8871 *msg = make_buffer_string (BEG, Z, 1);
8872 else
8873 *msg = Qnil;
8874 return 0;
8878 /* Push the current message on Vmessage_stack for later restauration
8879 by restore_message. Value is non-zero if the current message isn't
8880 empty. This is a relatively infrequent operation, so it's not
8881 worth optimizing. */
8884 push_message ()
8886 Lisp_Object msg;
8887 msg = current_message ();
8888 Vmessage_stack = Fcons (msg, Vmessage_stack);
8889 return STRINGP (msg);
8893 /* Restore message display from the top of Vmessage_stack. */
8895 void
8896 restore_message ()
8898 Lisp_Object msg;
8900 xassert (CONSP (Vmessage_stack));
8901 msg = XCAR (Vmessage_stack);
8902 if (STRINGP (msg))
8903 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8904 else
8905 message3_nolog (msg, 0, 0);
8909 /* Handler for record_unwind_protect calling pop_message. */
8911 Lisp_Object
8912 pop_message_unwind (dummy)
8913 Lisp_Object dummy;
8915 pop_message ();
8916 return Qnil;
8919 /* Pop the top-most entry off Vmessage_stack. */
8921 void
8922 pop_message ()
8924 xassert (CONSP (Vmessage_stack));
8925 Vmessage_stack = XCDR (Vmessage_stack);
8929 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8930 exits. If the stack is not empty, we have a missing pop_message
8931 somewhere. */
8933 void
8934 check_message_stack ()
8936 if (!NILP (Vmessage_stack))
8937 abort ();
8941 /* Truncate to NCHARS what will be displayed in the echo area the next
8942 time we display it---but don't redisplay it now. */
8944 void
8945 truncate_echo_area (nchars)
8946 int nchars;
8948 if (nchars == 0)
8949 echo_area_buffer[0] = Qnil;
8950 /* A null message buffer means that the frame hasn't really been
8951 initialized yet. Error messages get reported properly by
8952 cmd_error, so this must be just an informative message; toss it. */
8953 else if (!noninteractive
8954 && INTERACTIVE
8955 && !NILP (echo_area_buffer[0]))
8957 struct frame *sf = SELECTED_FRAME ();
8958 if (FRAME_MESSAGE_BUF (sf))
8959 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8964 /* Helper function for truncate_echo_area. Truncate the current
8965 message to at most NCHARS characters. */
8967 static int
8968 truncate_message_1 (nchars, a2, a3, a4)
8969 EMACS_INT nchars;
8970 Lisp_Object a2;
8971 EMACS_INT a3, a4;
8973 if (BEG + nchars < Z)
8974 del_range (BEG + nchars, Z);
8975 if (Z == BEG)
8976 echo_area_buffer[0] = Qnil;
8977 return 0;
8981 /* Set the current message to a substring of S or STRING.
8983 If STRING is a Lisp string, set the message to the first NBYTES
8984 bytes from STRING. NBYTES zero means use the whole string. If
8985 STRING is multibyte, the message will be displayed multibyte.
8987 If S is not null, set the message to the first LEN bytes of S. LEN
8988 zero means use the whole string. MULTIBYTE_P non-zero means S is
8989 multibyte. Display the message multibyte in that case.
8991 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8992 to t before calling set_message_1 (which calls insert).
8995 void
8996 set_message (s, string, nbytes, multibyte_p)
8997 const char *s;
8998 Lisp_Object string;
8999 int nbytes, multibyte_p;
9001 message_enable_multibyte
9002 = ((s && multibyte_p)
9003 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9005 with_echo_area_buffer (0, -1, set_message_1,
9006 (EMACS_INT) s, string, nbytes, multibyte_p);
9007 message_buf_print = 0;
9008 help_echo_showing_p = 0;
9012 /* Helper function for set_message. Arguments have the same meaning
9013 as there, with A1 corresponding to S and A2 corresponding to STRING
9014 This function is called with the echo area buffer being
9015 current. */
9017 static int
9018 set_message_1 (a1, a2, nbytes, multibyte_p)
9019 EMACS_INT a1;
9020 Lisp_Object a2;
9021 EMACS_INT nbytes, multibyte_p;
9023 const char *s = (const char *) a1;
9024 Lisp_Object string = a2;
9026 /* Change multibyteness of the echo buffer appropriately. */
9027 if (message_enable_multibyte
9028 != !NILP (current_buffer->enable_multibyte_characters))
9029 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9031 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
9033 /* Insert new message at BEG. */
9034 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9036 if (STRINGP (string))
9038 int nchars;
9040 if (nbytes == 0)
9041 nbytes = SBYTES (string);
9042 nchars = string_byte_to_char (string, nbytes);
9044 /* This function takes care of single/multibyte conversion. We
9045 just have to ensure that the echo area buffer has the right
9046 setting of enable_multibyte_characters. */
9047 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9049 else if (s)
9051 if (nbytes == 0)
9052 nbytes = strlen (s);
9054 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9056 /* Convert from multi-byte to single-byte. */
9057 int i, c, n;
9058 unsigned char work[1];
9060 /* Convert a multibyte string to single-byte. */
9061 for (i = 0; i < nbytes; i += n)
9063 c = string_char_and_length (s + i, &n);
9064 work[0] = (ASCII_CHAR_P (c)
9066 : multibyte_char_to_unibyte (c, Qnil));
9067 insert_1_both (work, 1, 1, 1, 0, 0);
9070 else if (!multibyte_p
9071 && !NILP (current_buffer->enable_multibyte_characters))
9073 /* Convert from single-byte to multi-byte. */
9074 int i, c, n;
9075 const unsigned char *msg = (const unsigned char *) s;
9076 unsigned char str[MAX_MULTIBYTE_LENGTH];
9078 /* Convert a single-byte string to multibyte. */
9079 for (i = 0; i < nbytes; i++)
9081 c = msg[i];
9082 MAKE_CHAR_MULTIBYTE (c);
9083 n = CHAR_STRING (c, str);
9084 insert_1_both (str, 1, n, 1, 0, 0);
9087 else
9088 insert_1 (s, nbytes, 1, 0, 0);
9091 return 0;
9095 /* Clear messages. CURRENT_P non-zero means clear the current
9096 message. LAST_DISPLAYED_P non-zero means clear the message
9097 last displayed. */
9099 void
9100 clear_message (current_p, last_displayed_p)
9101 int current_p, last_displayed_p;
9103 if (current_p)
9105 echo_area_buffer[0] = Qnil;
9106 message_cleared_p = 1;
9109 if (last_displayed_p)
9110 echo_area_buffer[1] = Qnil;
9112 message_buf_print = 0;
9115 /* Clear garbaged frames.
9117 This function is used where the old redisplay called
9118 redraw_garbaged_frames which in turn called redraw_frame which in
9119 turn called clear_frame. The call to clear_frame was a source of
9120 flickering. I believe a clear_frame is not necessary. It should
9121 suffice in the new redisplay to invalidate all current matrices,
9122 and ensure a complete redisplay of all windows. */
9124 static void
9125 clear_garbaged_frames ()
9127 if (frame_garbaged)
9129 Lisp_Object tail, frame;
9130 int changed_count = 0;
9132 FOR_EACH_FRAME (tail, frame)
9134 struct frame *f = XFRAME (frame);
9136 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9138 if (f->resized_p)
9140 Fredraw_frame (frame);
9141 f->force_flush_display_p = 1;
9143 clear_current_matrices (f);
9144 changed_count++;
9145 f->garbaged = 0;
9146 f->resized_p = 0;
9150 frame_garbaged = 0;
9151 if (changed_count)
9152 ++windows_or_buffers_changed;
9157 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9158 is non-zero update selected_frame. Value is non-zero if the
9159 mini-windows height has been changed. */
9161 static int
9162 echo_area_display (update_frame_p)
9163 int update_frame_p;
9165 Lisp_Object mini_window;
9166 struct window *w;
9167 struct frame *f;
9168 int window_height_changed_p = 0;
9169 struct frame *sf = SELECTED_FRAME ();
9171 mini_window = FRAME_MINIBUF_WINDOW (sf);
9172 w = XWINDOW (mini_window);
9173 f = XFRAME (WINDOW_FRAME (w));
9175 /* Don't display if frame is invisible or not yet initialized. */
9176 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9177 return 0;
9179 #ifdef HAVE_WINDOW_SYSTEM
9180 /* When Emacs starts, selected_frame may be the initial terminal
9181 frame. If we let this through, a message would be displayed on
9182 the terminal. */
9183 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9184 return 0;
9185 #endif /* HAVE_WINDOW_SYSTEM */
9187 /* Redraw garbaged frames. */
9188 if (frame_garbaged)
9189 clear_garbaged_frames ();
9191 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9193 echo_area_window = mini_window;
9194 window_height_changed_p = display_echo_area (w);
9195 w->must_be_updated_p = 1;
9197 /* Update the display, unless called from redisplay_internal.
9198 Also don't update the screen during redisplay itself. The
9199 update will happen at the end of redisplay, and an update
9200 here could cause confusion. */
9201 if (update_frame_p && !redisplaying_p)
9203 int n = 0;
9205 /* If the display update has been interrupted by pending
9206 input, update mode lines in the frame. Due to the
9207 pending input, it might have been that redisplay hasn't
9208 been called, so that mode lines above the echo area are
9209 garbaged. This looks odd, so we prevent it here. */
9210 if (!display_completed)
9211 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9213 if (window_height_changed_p
9214 /* Don't do this if Emacs is shutting down. Redisplay
9215 needs to run hooks. */
9216 && !NILP (Vrun_hooks))
9218 /* Must update other windows. Likewise as in other
9219 cases, don't let this update be interrupted by
9220 pending input. */
9221 int count = SPECPDL_INDEX ();
9222 specbind (Qredisplay_dont_pause, Qt);
9223 windows_or_buffers_changed = 1;
9224 redisplay_internal (0);
9225 unbind_to (count, Qnil);
9227 else if (FRAME_WINDOW_P (f) && n == 0)
9229 /* Window configuration is the same as before.
9230 Can do with a display update of the echo area,
9231 unless we displayed some mode lines. */
9232 update_single_window (w, 1);
9233 FRAME_RIF (f)->flush_display (f);
9235 else
9236 update_frame (f, 1, 1);
9238 /* If cursor is in the echo area, make sure that the next
9239 redisplay displays the minibuffer, so that the cursor will
9240 be replaced with what the minibuffer wants. */
9241 if (cursor_in_echo_area)
9242 ++windows_or_buffers_changed;
9245 else if (!EQ (mini_window, selected_window))
9246 windows_or_buffers_changed++;
9248 /* Last displayed message is now the current message. */
9249 echo_area_buffer[1] = echo_area_buffer[0];
9250 /* Inform read_char that we're not echoing. */
9251 echo_message_buffer = Qnil;
9253 /* Prevent redisplay optimization in redisplay_internal by resetting
9254 this_line_start_pos. This is done because the mini-buffer now
9255 displays the message instead of its buffer text. */
9256 if (EQ (mini_window, selected_window))
9257 CHARPOS (this_line_start_pos) = 0;
9259 return window_height_changed_p;
9264 /***********************************************************************
9265 Mode Lines and Frame Titles
9266 ***********************************************************************/
9268 /* A buffer for constructing non-propertized mode-line strings and
9269 frame titles in it; allocated from the heap in init_xdisp and
9270 resized as needed in store_mode_line_noprop_char. */
9272 static char *mode_line_noprop_buf;
9274 /* The buffer's end, and a current output position in it. */
9276 static char *mode_line_noprop_buf_end;
9277 static char *mode_line_noprop_ptr;
9279 #define MODE_LINE_NOPROP_LEN(start) \
9280 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9282 static enum {
9283 MODE_LINE_DISPLAY = 0,
9284 MODE_LINE_TITLE,
9285 MODE_LINE_NOPROP,
9286 MODE_LINE_STRING
9287 } mode_line_target;
9289 /* Alist that caches the results of :propertize.
9290 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9291 static Lisp_Object mode_line_proptrans_alist;
9293 /* List of strings making up the mode-line. */
9294 static Lisp_Object mode_line_string_list;
9296 /* Base face property when building propertized mode line string. */
9297 static Lisp_Object mode_line_string_face;
9298 static Lisp_Object mode_line_string_face_prop;
9301 /* Unwind data for mode line strings */
9303 static Lisp_Object Vmode_line_unwind_vector;
9305 static Lisp_Object
9306 format_mode_line_unwind_data (struct buffer *obuf,
9307 Lisp_Object owin,
9308 int save_proptrans)
9310 Lisp_Object vector, tmp;
9312 /* Reduce consing by keeping one vector in
9313 Vwith_echo_area_save_vector. */
9314 vector = Vmode_line_unwind_vector;
9315 Vmode_line_unwind_vector = Qnil;
9317 if (NILP (vector))
9318 vector = Fmake_vector (make_number (8), Qnil);
9320 ASET (vector, 0, make_number (mode_line_target));
9321 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9322 ASET (vector, 2, mode_line_string_list);
9323 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9324 ASET (vector, 4, mode_line_string_face);
9325 ASET (vector, 5, mode_line_string_face_prop);
9327 if (obuf)
9328 XSETBUFFER (tmp, obuf);
9329 else
9330 tmp = Qnil;
9331 ASET (vector, 6, tmp);
9332 ASET (vector, 7, owin);
9334 return vector;
9337 static Lisp_Object
9338 unwind_format_mode_line (vector)
9339 Lisp_Object vector;
9341 mode_line_target = XINT (AREF (vector, 0));
9342 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9343 mode_line_string_list = AREF (vector, 2);
9344 if (! EQ (AREF (vector, 3), Qt))
9345 mode_line_proptrans_alist = AREF (vector, 3);
9346 mode_line_string_face = AREF (vector, 4);
9347 mode_line_string_face_prop = AREF (vector, 5);
9349 if (!NILP (AREF (vector, 7)))
9350 /* Select window before buffer, since it may change the buffer. */
9351 Fselect_window (AREF (vector, 7), Qt);
9353 if (!NILP (AREF (vector, 6)))
9355 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9356 ASET (vector, 6, Qnil);
9359 Vmode_line_unwind_vector = vector;
9360 return Qnil;
9364 /* Store a single character C for the frame title in mode_line_noprop_buf.
9365 Re-allocate mode_line_noprop_buf if necessary. */
9367 static void
9368 #ifdef PROTOTYPES
9369 store_mode_line_noprop_char (char c)
9370 #else
9371 store_mode_line_noprop_char (c)
9372 char c;
9373 #endif
9375 /* If output position has reached the end of the allocated buffer,
9376 double the buffer's size. */
9377 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9379 int len = MODE_LINE_NOPROP_LEN (0);
9380 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9381 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9382 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9383 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9386 *mode_line_noprop_ptr++ = c;
9390 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9391 mode_line_noprop_ptr. STR is the string to store. Do not copy
9392 characters that yield more columns than PRECISION; PRECISION <= 0
9393 means copy the whole string. Pad with spaces until FIELD_WIDTH
9394 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9395 pad. Called from display_mode_element when it is used to build a
9396 frame title. */
9398 static int
9399 store_mode_line_noprop (str, field_width, precision)
9400 const unsigned char *str;
9401 int field_width, precision;
9403 int n = 0;
9404 int dummy, nbytes;
9406 /* Copy at most PRECISION chars from STR. */
9407 nbytes = strlen (str);
9408 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9409 while (nbytes--)
9410 store_mode_line_noprop_char (*str++);
9412 /* Fill up with spaces until FIELD_WIDTH reached. */
9413 while (field_width > 0
9414 && n < field_width)
9416 store_mode_line_noprop_char (' ');
9417 ++n;
9420 return n;
9423 /***********************************************************************
9424 Frame Titles
9425 ***********************************************************************/
9427 #ifdef HAVE_WINDOW_SYSTEM
9429 /* Set the title of FRAME, if it has changed. The title format is
9430 Vicon_title_format if FRAME is iconified, otherwise it is
9431 frame_title_format. */
9433 static void
9434 x_consider_frame_title (frame)
9435 Lisp_Object frame;
9437 struct frame *f = XFRAME (frame);
9439 if (FRAME_WINDOW_P (f)
9440 || FRAME_MINIBUF_ONLY_P (f)
9441 || f->explicit_name)
9443 /* Do we have more than one visible frame on this X display? */
9444 Lisp_Object tail;
9445 Lisp_Object fmt;
9446 int title_start;
9447 char *title;
9448 int len;
9449 struct it it;
9450 int count = SPECPDL_INDEX ();
9452 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9454 Lisp_Object other_frame = XCAR (tail);
9455 struct frame *tf = XFRAME (other_frame);
9457 if (tf != f
9458 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9459 && !FRAME_MINIBUF_ONLY_P (tf)
9460 && !EQ (other_frame, tip_frame)
9461 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9462 break;
9465 /* Set global variable indicating that multiple frames exist. */
9466 multiple_frames = CONSP (tail);
9468 /* Switch to the buffer of selected window of the frame. Set up
9469 mode_line_target so that display_mode_element will output into
9470 mode_line_noprop_buf; then display the title. */
9471 record_unwind_protect (unwind_format_mode_line,
9472 format_mode_line_unwind_data
9473 (current_buffer, selected_window, 0));
9475 Fselect_window (f->selected_window, Qt);
9476 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9477 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9479 mode_line_target = MODE_LINE_TITLE;
9480 title_start = MODE_LINE_NOPROP_LEN (0);
9481 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9482 NULL, DEFAULT_FACE_ID);
9483 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9484 len = MODE_LINE_NOPROP_LEN (title_start);
9485 title = mode_line_noprop_buf + title_start;
9486 unbind_to (count, Qnil);
9488 /* Set the title only if it's changed. This avoids consing in
9489 the common case where it hasn't. (If it turns out that we've
9490 already wasted too much time by walking through the list with
9491 display_mode_element, then we might need to optimize at a
9492 higher level than this.) */
9493 if (! STRINGP (f->name)
9494 || SBYTES (f->name) != len
9495 || bcmp (title, SDATA (f->name), len) != 0)
9497 #ifdef HAVE_NS
9498 if (FRAME_NS_P (f))
9500 if (!MINI_WINDOW_P(XWINDOW(f->selected_window)))
9502 if (EQ (fmt, Qt))
9503 ns_set_name_as_filename (f);
9504 else
9505 x_implicitly_set_name (f, make_string(title, len),
9506 Qnil);
9509 else
9510 #endif
9511 x_implicitly_set_name (f, make_string (title, len), Qnil);
9513 #ifdef HAVE_NS
9514 if (FRAME_NS_P (f))
9516 /* do this also for frames with explicit names */
9517 ns_implicitly_set_icon_type(f);
9518 ns_set_doc_edited(f, Fbuffer_modified_p
9519 (XWINDOW (f->selected_window)->buffer), Qnil);
9521 #endif
9525 #endif /* not HAVE_WINDOW_SYSTEM */
9530 /***********************************************************************
9531 Menu Bars
9532 ***********************************************************************/
9535 /* Prepare for redisplay by updating menu-bar item lists when
9536 appropriate. This can call eval. */
9538 void
9539 prepare_menu_bars ()
9541 int all_windows;
9542 struct gcpro gcpro1, gcpro2;
9543 struct frame *f;
9544 Lisp_Object tooltip_frame;
9546 #ifdef HAVE_WINDOW_SYSTEM
9547 tooltip_frame = tip_frame;
9548 #else
9549 tooltip_frame = Qnil;
9550 #endif
9552 /* Update all frame titles based on their buffer names, etc. We do
9553 this before the menu bars so that the buffer-menu will show the
9554 up-to-date frame titles. */
9555 #ifdef HAVE_WINDOW_SYSTEM
9556 if (windows_or_buffers_changed || update_mode_lines)
9558 Lisp_Object tail, frame;
9560 FOR_EACH_FRAME (tail, frame)
9562 f = XFRAME (frame);
9563 if (!EQ (frame, tooltip_frame)
9564 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9565 x_consider_frame_title (frame);
9568 #endif /* HAVE_WINDOW_SYSTEM */
9570 /* Update the menu bar item lists, if appropriate. This has to be
9571 done before any actual redisplay or generation of display lines. */
9572 all_windows = (update_mode_lines
9573 || buffer_shared > 1
9574 || windows_or_buffers_changed);
9575 if (all_windows)
9577 Lisp_Object tail, frame;
9578 int count = SPECPDL_INDEX ();
9579 /* 1 means that update_menu_bar has run its hooks
9580 so any further calls to update_menu_bar shouldn't do so again. */
9581 int menu_bar_hooks_run = 0;
9583 record_unwind_save_match_data ();
9585 FOR_EACH_FRAME (tail, frame)
9587 f = XFRAME (frame);
9589 /* Ignore tooltip frame. */
9590 if (EQ (frame, tooltip_frame))
9591 continue;
9593 /* If a window on this frame changed size, report that to
9594 the user and clear the size-change flag. */
9595 if (FRAME_WINDOW_SIZES_CHANGED (f))
9597 Lisp_Object functions;
9599 /* Clear flag first in case we get an error below. */
9600 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9601 functions = Vwindow_size_change_functions;
9602 GCPRO2 (tail, functions);
9604 while (CONSP (functions))
9606 if (!EQ (XCAR (functions), Qt))
9607 call1 (XCAR (functions), frame);
9608 functions = XCDR (functions);
9610 UNGCPRO;
9613 GCPRO1 (tail);
9614 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9615 #ifdef HAVE_WINDOW_SYSTEM
9616 update_tool_bar (f, 0);
9617 #endif
9618 UNGCPRO;
9621 unbind_to (count, Qnil);
9623 else
9625 struct frame *sf = SELECTED_FRAME ();
9626 update_menu_bar (sf, 1, 0);
9627 #ifdef HAVE_WINDOW_SYSTEM
9628 update_tool_bar (sf, 1);
9629 #endif
9632 /* Motif needs this. See comment in xmenu.c. Turn it off when
9633 pending_menu_activation is not defined. */
9634 #ifdef USE_X_TOOLKIT
9635 pending_menu_activation = 0;
9636 #endif
9640 /* Update the menu bar item list for frame F. This has to be done
9641 before we start to fill in any display lines, because it can call
9642 eval.
9644 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9646 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9647 already ran the menu bar hooks for this redisplay, so there
9648 is no need to run them again. The return value is the
9649 updated value of this flag, to pass to the next call. */
9651 static int
9652 update_menu_bar (f, save_match_data, hooks_run)
9653 struct frame *f;
9654 int save_match_data;
9655 int hooks_run;
9657 Lisp_Object window;
9658 register struct window *w;
9660 /* If called recursively during a menu update, do nothing. This can
9661 happen when, for instance, an activate-menubar-hook causes a
9662 redisplay. */
9663 if (inhibit_menubar_update)
9664 return hooks_run;
9666 window = FRAME_SELECTED_WINDOW (f);
9667 w = XWINDOW (window);
9669 if (FRAME_WINDOW_P (f)
9671 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9672 || defined (HAVE_NS) || defined (USE_GTK)
9673 FRAME_EXTERNAL_MENU_BAR (f)
9674 #else
9675 FRAME_MENU_BAR_LINES (f) > 0
9676 #endif
9677 : FRAME_MENU_BAR_LINES (f) > 0)
9679 /* If the user has switched buffers or windows, we need to
9680 recompute to reflect the new bindings. But we'll
9681 recompute when update_mode_lines is set too; that means
9682 that people can use force-mode-line-update to request
9683 that the menu bar be recomputed. The adverse effect on
9684 the rest of the redisplay algorithm is about the same as
9685 windows_or_buffers_changed anyway. */
9686 if (windows_or_buffers_changed
9687 /* This used to test w->update_mode_line, but we believe
9688 there is no need to recompute the menu in that case. */
9689 || update_mode_lines
9690 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9691 < BUF_MODIFF (XBUFFER (w->buffer)))
9692 != !NILP (w->last_had_star))
9693 || ((!NILP (Vtransient_mark_mode)
9694 && !NILP (XBUFFER (w->buffer)->mark_active))
9695 != !NILP (w->region_showing)))
9697 struct buffer *prev = current_buffer;
9698 int count = SPECPDL_INDEX ();
9700 specbind (Qinhibit_menubar_update, Qt);
9702 set_buffer_internal_1 (XBUFFER (w->buffer));
9703 if (save_match_data)
9704 record_unwind_save_match_data ();
9705 if (NILP (Voverriding_local_map_menu_flag))
9707 specbind (Qoverriding_terminal_local_map, Qnil);
9708 specbind (Qoverriding_local_map, Qnil);
9711 if (!hooks_run)
9713 /* Run the Lucid hook. */
9714 safe_run_hooks (Qactivate_menubar_hook);
9716 /* If it has changed current-menubar from previous value,
9717 really recompute the menu-bar from the value. */
9718 if (! NILP (Vlucid_menu_bar_dirty_flag))
9719 call0 (Qrecompute_lucid_menubar);
9721 safe_run_hooks (Qmenu_bar_update_hook);
9723 hooks_run = 1;
9726 XSETFRAME (Vmenu_updating_frame, f);
9727 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9729 /* Redisplay the menu bar in case we changed it. */
9730 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9731 || defined (HAVE_NS) || defined (USE_GTK)
9732 if (FRAME_WINDOW_P (f))
9734 #if defined (HAVE_NS)
9735 /* All frames on Mac OS share the same menubar. So only
9736 the selected frame should be allowed to set it. */
9737 if (f == SELECTED_FRAME ())
9738 #endif
9739 set_frame_menubar (f, 0, 0);
9741 else
9742 /* On a terminal screen, the menu bar is an ordinary screen
9743 line, and this makes it get updated. */
9744 w->update_mode_line = Qt;
9745 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9746 /* In the non-toolkit version, the menu bar is an ordinary screen
9747 line, and this makes it get updated. */
9748 w->update_mode_line = Qt;
9749 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9751 unbind_to (count, Qnil);
9752 set_buffer_internal_1 (prev);
9756 return hooks_run;
9761 /***********************************************************************
9762 Output Cursor
9763 ***********************************************************************/
9765 #ifdef HAVE_WINDOW_SYSTEM
9767 /* EXPORT:
9768 Nominal cursor position -- where to draw output.
9769 HPOS and VPOS are window relative glyph matrix coordinates.
9770 X and Y are window relative pixel coordinates. */
9772 struct cursor_pos output_cursor;
9775 /* EXPORT:
9776 Set the global variable output_cursor to CURSOR. All cursor
9777 positions are relative to updated_window. */
9779 void
9780 set_output_cursor (cursor)
9781 struct cursor_pos *cursor;
9783 output_cursor.hpos = cursor->hpos;
9784 output_cursor.vpos = cursor->vpos;
9785 output_cursor.x = cursor->x;
9786 output_cursor.y = cursor->y;
9790 /* EXPORT for RIF:
9791 Set a nominal cursor position.
9793 HPOS and VPOS are column/row positions in a window glyph matrix. X
9794 and Y are window text area relative pixel positions.
9796 If this is done during an update, updated_window will contain the
9797 window that is being updated and the position is the future output
9798 cursor position for that window. If updated_window is null, use
9799 selected_window and display the cursor at the given position. */
9801 void
9802 x_cursor_to (vpos, hpos, y, x)
9803 int vpos, hpos, y, x;
9805 struct window *w;
9807 /* If updated_window is not set, work on selected_window. */
9808 if (updated_window)
9809 w = updated_window;
9810 else
9811 w = XWINDOW (selected_window);
9813 /* Set the output cursor. */
9814 output_cursor.hpos = hpos;
9815 output_cursor.vpos = vpos;
9816 output_cursor.x = x;
9817 output_cursor.y = y;
9819 /* If not called as part of an update, really display the cursor.
9820 This will also set the cursor position of W. */
9821 if (updated_window == NULL)
9823 BLOCK_INPUT;
9824 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9825 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9826 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9827 UNBLOCK_INPUT;
9831 #endif /* HAVE_WINDOW_SYSTEM */
9834 /***********************************************************************
9835 Tool-bars
9836 ***********************************************************************/
9838 #ifdef HAVE_WINDOW_SYSTEM
9840 /* Where the mouse was last time we reported a mouse event. */
9842 FRAME_PTR last_mouse_frame;
9844 /* Tool-bar item index of the item on which a mouse button was pressed
9845 or -1. */
9847 int last_tool_bar_item;
9850 static Lisp_Object
9851 update_tool_bar_unwind (frame)
9852 Lisp_Object frame;
9854 selected_frame = frame;
9855 return Qnil;
9858 /* Update the tool-bar item list for frame F. This has to be done
9859 before we start to fill in any display lines. Called from
9860 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9861 and restore it here. */
9863 static void
9864 update_tool_bar (f, save_match_data)
9865 struct frame *f;
9866 int save_match_data;
9868 #if defined (USE_GTK) || defined (HAVE_NS)
9869 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9870 #else
9871 int do_update = WINDOWP (f->tool_bar_window)
9872 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9873 #endif
9875 if (do_update)
9877 Lisp_Object window;
9878 struct window *w;
9880 window = FRAME_SELECTED_WINDOW (f);
9881 w = XWINDOW (window);
9883 /* If the user has switched buffers or windows, we need to
9884 recompute to reflect the new bindings. But we'll
9885 recompute when update_mode_lines is set too; that means
9886 that people can use force-mode-line-update to request
9887 that the menu bar be recomputed. The adverse effect on
9888 the rest of the redisplay algorithm is about the same as
9889 windows_or_buffers_changed anyway. */
9890 if (windows_or_buffers_changed
9891 || !NILP (w->update_mode_line)
9892 || update_mode_lines
9893 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9894 < BUF_MODIFF (XBUFFER (w->buffer)))
9895 != !NILP (w->last_had_star))
9896 || ((!NILP (Vtransient_mark_mode)
9897 && !NILP (XBUFFER (w->buffer)->mark_active))
9898 != !NILP (w->region_showing)))
9900 struct buffer *prev = current_buffer;
9901 int count = SPECPDL_INDEX ();
9902 Lisp_Object frame, new_tool_bar;
9903 int new_n_tool_bar;
9904 struct gcpro gcpro1;
9906 /* Set current_buffer to the buffer of the selected
9907 window of the frame, so that we get the right local
9908 keymaps. */
9909 set_buffer_internal_1 (XBUFFER (w->buffer));
9911 /* Save match data, if we must. */
9912 if (save_match_data)
9913 record_unwind_save_match_data ();
9915 /* Make sure that we don't accidentally use bogus keymaps. */
9916 if (NILP (Voverriding_local_map_menu_flag))
9918 specbind (Qoverriding_terminal_local_map, Qnil);
9919 specbind (Qoverriding_local_map, Qnil);
9922 GCPRO1 (new_tool_bar);
9924 /* We must temporarily set the selected frame to this frame
9925 before calling tool_bar_items, because the calculation of
9926 the tool-bar keymap uses the selected frame (see
9927 `tool-bar-make-keymap' in tool-bar.el). */
9928 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9929 XSETFRAME (frame, f);
9930 selected_frame = frame;
9932 /* Build desired tool-bar items from keymaps. */
9933 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9934 &new_n_tool_bar);
9936 /* Redisplay the tool-bar if we changed it. */
9937 if (new_n_tool_bar != f->n_tool_bar_items
9938 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9940 /* Redisplay that happens asynchronously due to an expose event
9941 may access f->tool_bar_items. Make sure we update both
9942 variables within BLOCK_INPUT so no such event interrupts. */
9943 BLOCK_INPUT;
9944 f->tool_bar_items = new_tool_bar;
9945 f->n_tool_bar_items = new_n_tool_bar;
9946 w->update_mode_line = Qt;
9947 UNBLOCK_INPUT;
9950 UNGCPRO;
9952 unbind_to (count, Qnil);
9953 set_buffer_internal_1 (prev);
9959 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9960 F's desired tool-bar contents. F->tool_bar_items must have
9961 been set up previously by calling prepare_menu_bars. */
9963 static void
9964 build_desired_tool_bar_string (f)
9965 struct frame *f;
9967 int i, size, size_needed;
9968 struct gcpro gcpro1, gcpro2, gcpro3;
9969 Lisp_Object image, plist, props;
9971 image = plist = props = Qnil;
9972 GCPRO3 (image, plist, props);
9974 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9975 Otherwise, make a new string. */
9977 /* The size of the string we might be able to reuse. */
9978 size = (STRINGP (f->desired_tool_bar_string)
9979 ? SCHARS (f->desired_tool_bar_string)
9980 : 0);
9982 /* We need one space in the string for each image. */
9983 size_needed = f->n_tool_bar_items;
9985 /* Reuse f->desired_tool_bar_string, if possible. */
9986 if (size < size_needed || NILP (f->desired_tool_bar_string))
9987 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9988 make_number (' '));
9989 else
9991 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9992 Fremove_text_properties (make_number (0), make_number (size),
9993 props, f->desired_tool_bar_string);
9996 /* Put a `display' property on the string for the images to display,
9997 put a `menu_item' property on tool-bar items with a value that
9998 is the index of the item in F's tool-bar item vector. */
9999 for (i = 0; i < f->n_tool_bar_items; ++i)
10001 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10003 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10004 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10005 int hmargin, vmargin, relief, idx, end;
10006 extern Lisp_Object QCrelief, QCmargin, QCconversion;
10008 /* If image is a vector, choose the image according to the
10009 button state. */
10010 image = PROP (TOOL_BAR_ITEM_IMAGES);
10011 if (VECTORP (image))
10013 if (enabled_p)
10014 idx = (selected_p
10015 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10016 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10017 else
10018 idx = (selected_p
10019 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10020 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10022 xassert (ASIZE (image) >= idx);
10023 image = AREF (image, idx);
10025 else
10026 idx = -1;
10028 /* Ignore invalid image specifications. */
10029 if (!valid_image_p (image))
10030 continue;
10032 /* Display the tool-bar button pressed, or depressed. */
10033 plist = Fcopy_sequence (XCDR (image));
10035 /* Compute margin and relief to draw. */
10036 relief = (tool_bar_button_relief >= 0
10037 ? tool_bar_button_relief
10038 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10039 hmargin = vmargin = relief;
10041 if (INTEGERP (Vtool_bar_button_margin)
10042 && XINT (Vtool_bar_button_margin) > 0)
10044 hmargin += XFASTINT (Vtool_bar_button_margin);
10045 vmargin += XFASTINT (Vtool_bar_button_margin);
10047 else if (CONSP (Vtool_bar_button_margin))
10049 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10050 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10051 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10053 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10054 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10055 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10058 if (auto_raise_tool_bar_buttons_p)
10060 /* Add a `:relief' property to the image spec if the item is
10061 selected. */
10062 if (selected_p)
10064 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10065 hmargin -= relief;
10066 vmargin -= relief;
10069 else
10071 /* If image is selected, display it pressed, i.e. with a
10072 negative relief. If it's not selected, display it with a
10073 raised relief. */
10074 plist = Fplist_put (plist, QCrelief,
10075 (selected_p
10076 ? make_number (-relief)
10077 : make_number (relief)));
10078 hmargin -= relief;
10079 vmargin -= relief;
10082 /* Put a margin around the image. */
10083 if (hmargin || vmargin)
10085 if (hmargin == vmargin)
10086 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10087 else
10088 plist = Fplist_put (plist, QCmargin,
10089 Fcons (make_number (hmargin),
10090 make_number (vmargin)));
10093 /* If button is not enabled, and we don't have special images
10094 for the disabled state, make the image appear disabled by
10095 applying an appropriate algorithm to it. */
10096 if (!enabled_p && idx < 0)
10097 plist = Fplist_put (plist, QCconversion, Qdisabled);
10099 /* Put a `display' text property on the string for the image to
10100 display. Put a `menu-item' property on the string that gives
10101 the start of this item's properties in the tool-bar items
10102 vector. */
10103 image = Fcons (Qimage, plist);
10104 props = list4 (Qdisplay, image,
10105 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10107 /* Let the last image hide all remaining spaces in the tool bar
10108 string. The string can be longer than needed when we reuse a
10109 previous string. */
10110 if (i + 1 == f->n_tool_bar_items)
10111 end = SCHARS (f->desired_tool_bar_string);
10112 else
10113 end = i + 1;
10114 Fadd_text_properties (make_number (i), make_number (end),
10115 props, f->desired_tool_bar_string);
10116 #undef PROP
10119 UNGCPRO;
10123 /* Display one line of the tool-bar of frame IT->f.
10125 HEIGHT specifies the desired height of the tool-bar line.
10126 If the actual height of the glyph row is less than HEIGHT, the
10127 row's height is increased to HEIGHT, and the icons are centered
10128 vertically in the new height.
10130 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10131 count a final empty row in case the tool-bar width exactly matches
10132 the window width.
10135 static void
10136 display_tool_bar_line (it, height)
10137 struct it *it;
10138 int height;
10140 struct glyph_row *row = it->glyph_row;
10141 int max_x = it->last_visible_x;
10142 struct glyph *last;
10144 prepare_desired_row (row);
10145 row->y = it->current_y;
10147 /* Note that this isn't made use of if the face hasn't a box,
10148 so there's no need to check the face here. */
10149 it->start_of_box_run_p = 1;
10151 while (it->current_x < max_x)
10153 int x, n_glyphs_before, i, nglyphs;
10154 struct it it_before;
10156 /* Get the next display element. */
10157 if (!get_next_display_element (it))
10159 /* Don't count empty row if we are counting needed tool-bar lines. */
10160 if (height < 0 && !it->hpos)
10161 return;
10162 break;
10165 /* Produce glyphs. */
10166 n_glyphs_before = row->used[TEXT_AREA];
10167 it_before = *it;
10169 PRODUCE_GLYPHS (it);
10171 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10172 i = 0;
10173 x = it_before.current_x;
10174 while (i < nglyphs)
10176 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10178 if (x + glyph->pixel_width > max_x)
10180 /* Glyph doesn't fit on line. Backtrack. */
10181 row->used[TEXT_AREA] = n_glyphs_before;
10182 *it = it_before;
10183 /* If this is the only glyph on this line, it will never fit on the
10184 toolbar, so skip it. But ensure there is at least one glyph,
10185 so we don't accidentally disable the tool-bar. */
10186 if (n_glyphs_before == 0
10187 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10188 break;
10189 goto out;
10192 ++it->hpos;
10193 x += glyph->pixel_width;
10194 ++i;
10197 /* Stop at line ends. */
10198 if (ITERATOR_AT_END_OF_LINE_P (it))
10199 break;
10201 set_iterator_to_next (it, 1);
10204 out:;
10206 row->displays_text_p = row->used[TEXT_AREA] != 0;
10208 /* Use default face for the border below the tool bar.
10210 FIXME: When auto-resize-tool-bars is grow-only, there is
10211 no additional border below the possibly empty tool-bar lines.
10212 So to make the extra empty lines look "normal", we have to
10213 use the tool-bar face for the border too. */
10214 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10215 it->face_id = DEFAULT_FACE_ID;
10217 extend_face_to_end_of_line (it);
10218 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10219 last->right_box_line_p = 1;
10220 if (last == row->glyphs[TEXT_AREA])
10221 last->left_box_line_p = 1;
10223 /* Make line the desired height and center it vertically. */
10224 if ((height -= it->max_ascent + it->max_descent) > 0)
10226 /* Don't add more than one line height. */
10227 height %= FRAME_LINE_HEIGHT (it->f);
10228 it->max_ascent += height / 2;
10229 it->max_descent += (height + 1) / 2;
10232 compute_line_metrics (it);
10234 /* If line is empty, make it occupy the rest of the tool-bar. */
10235 if (!row->displays_text_p)
10237 row->height = row->phys_height = it->last_visible_y - row->y;
10238 row->visible_height = row->height;
10239 row->ascent = row->phys_ascent = 0;
10240 row->extra_line_spacing = 0;
10243 row->full_width_p = 1;
10244 row->continued_p = 0;
10245 row->truncated_on_left_p = 0;
10246 row->truncated_on_right_p = 0;
10248 it->current_x = it->hpos = 0;
10249 it->current_y += row->height;
10250 ++it->vpos;
10251 ++it->glyph_row;
10255 /* Max tool-bar height. */
10257 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10258 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10260 /* Value is the number of screen lines needed to make all tool-bar
10261 items of frame F visible. The number of actual rows needed is
10262 returned in *N_ROWS if non-NULL. */
10264 static int
10265 tool_bar_lines_needed (f, n_rows)
10266 struct frame *f;
10267 int *n_rows;
10269 struct window *w = XWINDOW (f->tool_bar_window);
10270 struct it it;
10271 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10272 the desired matrix, so use (unused) mode-line row as temporary row to
10273 avoid destroying the first tool-bar row. */
10274 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10276 /* Initialize an iterator for iteration over
10277 F->desired_tool_bar_string in the tool-bar window of frame F. */
10278 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10279 it.first_visible_x = 0;
10280 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10281 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10283 while (!ITERATOR_AT_END_P (&it))
10285 clear_glyph_row (temp_row);
10286 it.glyph_row = temp_row;
10287 display_tool_bar_line (&it, -1);
10289 clear_glyph_row (temp_row);
10291 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10292 if (n_rows)
10293 *n_rows = it.vpos > 0 ? it.vpos : -1;
10295 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10299 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10300 0, 1, 0,
10301 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10302 (frame)
10303 Lisp_Object frame;
10305 struct frame *f;
10306 struct window *w;
10307 int nlines = 0;
10309 if (NILP (frame))
10310 frame = selected_frame;
10311 else
10312 CHECK_FRAME (frame);
10313 f = XFRAME (frame);
10315 if (WINDOWP (f->tool_bar_window)
10316 || (w = XWINDOW (f->tool_bar_window),
10317 WINDOW_TOTAL_LINES (w) > 0))
10319 update_tool_bar (f, 1);
10320 if (f->n_tool_bar_items)
10322 build_desired_tool_bar_string (f);
10323 nlines = tool_bar_lines_needed (f, NULL);
10327 return make_number (nlines);
10331 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10332 height should be changed. */
10334 static int
10335 redisplay_tool_bar (f)
10336 struct frame *f;
10338 struct window *w;
10339 struct it it;
10340 struct glyph_row *row;
10342 #if defined (USE_GTK) || defined (HAVE_NS)
10343 if (FRAME_EXTERNAL_TOOL_BAR (f))
10344 update_frame_tool_bar (f);
10345 return 0;
10346 #endif
10348 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10349 do anything. This means you must start with tool-bar-lines
10350 non-zero to get the auto-sizing effect. Or in other words, you
10351 can turn off tool-bars by specifying tool-bar-lines zero. */
10352 if (!WINDOWP (f->tool_bar_window)
10353 || (w = XWINDOW (f->tool_bar_window),
10354 WINDOW_TOTAL_LINES (w) == 0))
10355 return 0;
10357 /* Set up an iterator for the tool-bar window. */
10358 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10359 it.first_visible_x = 0;
10360 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10361 row = it.glyph_row;
10363 /* Build a string that represents the contents of the tool-bar. */
10364 build_desired_tool_bar_string (f);
10365 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10367 if (f->n_tool_bar_rows == 0)
10369 int nlines;
10371 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10372 nlines != WINDOW_TOTAL_LINES (w)))
10374 extern Lisp_Object Qtool_bar_lines;
10375 Lisp_Object frame;
10376 int old_height = WINDOW_TOTAL_LINES (w);
10378 XSETFRAME (frame, f);
10379 Fmodify_frame_parameters (frame,
10380 Fcons (Fcons (Qtool_bar_lines,
10381 make_number (nlines)),
10382 Qnil));
10383 if (WINDOW_TOTAL_LINES (w) != old_height)
10385 clear_glyph_matrix (w->desired_matrix);
10386 fonts_changed_p = 1;
10387 return 1;
10392 /* Display as many lines as needed to display all tool-bar items. */
10394 if (f->n_tool_bar_rows > 0)
10396 int border, rows, height, extra;
10398 if (INTEGERP (Vtool_bar_border))
10399 border = XINT (Vtool_bar_border);
10400 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10401 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10402 else if (EQ (Vtool_bar_border, Qborder_width))
10403 border = f->border_width;
10404 else
10405 border = 0;
10406 if (border < 0)
10407 border = 0;
10409 rows = f->n_tool_bar_rows;
10410 height = max (1, (it.last_visible_y - border) / rows);
10411 extra = it.last_visible_y - border - height * rows;
10413 while (it.current_y < it.last_visible_y)
10415 int h = 0;
10416 if (extra > 0 && rows-- > 0)
10418 h = (extra + rows - 1) / rows;
10419 extra -= h;
10421 display_tool_bar_line (&it, height + h);
10424 else
10426 while (it.current_y < it.last_visible_y)
10427 display_tool_bar_line (&it, 0);
10430 /* It doesn't make much sense to try scrolling in the tool-bar
10431 window, so don't do it. */
10432 w->desired_matrix->no_scrolling_p = 1;
10433 w->must_be_updated_p = 1;
10435 if (!NILP (Vauto_resize_tool_bars))
10437 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10438 int change_height_p = 0;
10440 /* If we couldn't display everything, change the tool-bar's
10441 height if there is room for more. */
10442 if (IT_STRING_CHARPOS (it) < it.end_charpos
10443 && it.current_y < max_tool_bar_height)
10444 change_height_p = 1;
10446 row = it.glyph_row - 1;
10448 /* If there are blank lines at the end, except for a partially
10449 visible blank line at the end that is smaller than
10450 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10451 if (!row->displays_text_p
10452 && row->height >= FRAME_LINE_HEIGHT (f))
10453 change_height_p = 1;
10455 /* If row displays tool-bar items, but is partially visible,
10456 change the tool-bar's height. */
10457 if (row->displays_text_p
10458 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10459 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10460 change_height_p = 1;
10462 /* Resize windows as needed by changing the `tool-bar-lines'
10463 frame parameter. */
10464 if (change_height_p)
10466 extern Lisp_Object Qtool_bar_lines;
10467 Lisp_Object frame;
10468 int old_height = WINDOW_TOTAL_LINES (w);
10469 int nrows;
10470 int nlines = tool_bar_lines_needed (f, &nrows);
10472 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10473 && !f->minimize_tool_bar_window_p)
10474 ? (nlines > old_height)
10475 : (nlines != old_height));
10476 f->minimize_tool_bar_window_p = 0;
10478 if (change_height_p)
10480 XSETFRAME (frame, f);
10481 Fmodify_frame_parameters (frame,
10482 Fcons (Fcons (Qtool_bar_lines,
10483 make_number (nlines)),
10484 Qnil));
10485 if (WINDOW_TOTAL_LINES (w) != old_height)
10487 clear_glyph_matrix (w->desired_matrix);
10488 f->n_tool_bar_rows = nrows;
10489 fonts_changed_p = 1;
10490 return 1;
10496 f->minimize_tool_bar_window_p = 0;
10497 return 0;
10501 /* Get information about the tool-bar item which is displayed in GLYPH
10502 on frame F. Return in *PROP_IDX the index where tool-bar item
10503 properties start in F->tool_bar_items. Value is zero if
10504 GLYPH doesn't display a tool-bar item. */
10506 static int
10507 tool_bar_item_info (f, glyph, prop_idx)
10508 struct frame *f;
10509 struct glyph *glyph;
10510 int *prop_idx;
10512 Lisp_Object prop;
10513 int success_p;
10514 int charpos;
10516 /* This function can be called asynchronously, which means we must
10517 exclude any possibility that Fget_text_property signals an
10518 error. */
10519 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10520 charpos = max (0, charpos);
10522 /* Get the text property `menu-item' at pos. The value of that
10523 property is the start index of this item's properties in
10524 F->tool_bar_items. */
10525 prop = Fget_text_property (make_number (charpos),
10526 Qmenu_item, f->current_tool_bar_string);
10527 if (INTEGERP (prop))
10529 *prop_idx = XINT (prop);
10530 success_p = 1;
10532 else
10533 success_p = 0;
10535 return success_p;
10539 /* Get information about the tool-bar item at position X/Y on frame F.
10540 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10541 the current matrix of the tool-bar window of F, or NULL if not
10542 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10543 item in F->tool_bar_items. Value is
10545 -1 if X/Y is not on a tool-bar item
10546 0 if X/Y is on the same item that was highlighted before.
10547 1 otherwise. */
10549 static int
10550 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10551 struct frame *f;
10552 int x, y;
10553 struct glyph **glyph;
10554 int *hpos, *vpos, *prop_idx;
10556 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10557 struct window *w = XWINDOW (f->tool_bar_window);
10558 int area;
10560 /* Find the glyph under X/Y. */
10561 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10562 if (*glyph == NULL)
10563 return -1;
10565 /* Get the start of this tool-bar item's properties in
10566 f->tool_bar_items. */
10567 if (!tool_bar_item_info (f, *glyph, prop_idx))
10568 return -1;
10570 /* Is mouse on the highlighted item? */
10571 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10572 && *vpos >= dpyinfo->mouse_face_beg_row
10573 && *vpos <= dpyinfo->mouse_face_end_row
10574 && (*vpos > dpyinfo->mouse_face_beg_row
10575 || *hpos >= dpyinfo->mouse_face_beg_col)
10576 && (*vpos < dpyinfo->mouse_face_end_row
10577 || *hpos < dpyinfo->mouse_face_end_col
10578 || dpyinfo->mouse_face_past_end))
10579 return 0;
10581 return 1;
10585 /* EXPORT:
10586 Handle mouse button event on the tool-bar of frame F, at
10587 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10588 0 for button release. MODIFIERS is event modifiers for button
10589 release. */
10591 void
10592 handle_tool_bar_click (f, x, y, down_p, modifiers)
10593 struct frame *f;
10594 int x, y, down_p;
10595 unsigned int modifiers;
10597 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10598 struct window *w = XWINDOW (f->tool_bar_window);
10599 int hpos, vpos, prop_idx;
10600 struct glyph *glyph;
10601 Lisp_Object enabled_p;
10603 /* If not on the highlighted tool-bar item, return. */
10604 frame_to_window_pixel_xy (w, &x, &y);
10605 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10606 return;
10608 /* If item is disabled, do nothing. */
10609 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10610 if (NILP (enabled_p))
10611 return;
10613 if (down_p)
10615 /* Show item in pressed state. */
10616 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10617 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10618 last_tool_bar_item = prop_idx;
10620 else
10622 Lisp_Object key, frame;
10623 struct input_event event;
10624 EVENT_INIT (event);
10626 /* Show item in released state. */
10627 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10628 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10630 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10632 XSETFRAME (frame, f);
10633 event.kind = TOOL_BAR_EVENT;
10634 event.frame_or_window = frame;
10635 event.arg = frame;
10636 kbd_buffer_store_event (&event);
10638 event.kind = TOOL_BAR_EVENT;
10639 event.frame_or_window = frame;
10640 event.arg = key;
10641 event.modifiers = modifiers;
10642 kbd_buffer_store_event (&event);
10643 last_tool_bar_item = -1;
10648 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10649 tool-bar window-relative coordinates X/Y. Called from
10650 note_mouse_highlight. */
10652 static void
10653 note_tool_bar_highlight (f, x, y)
10654 struct frame *f;
10655 int x, y;
10657 Lisp_Object window = f->tool_bar_window;
10658 struct window *w = XWINDOW (window);
10659 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10660 int hpos, vpos;
10661 struct glyph *glyph;
10662 struct glyph_row *row;
10663 int i;
10664 Lisp_Object enabled_p;
10665 int prop_idx;
10666 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10667 int mouse_down_p, rc;
10669 /* Function note_mouse_highlight is called with negative x(y
10670 values when mouse moves outside of the frame. */
10671 if (x <= 0 || y <= 0)
10673 clear_mouse_face (dpyinfo);
10674 return;
10677 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10678 if (rc < 0)
10680 /* Not on tool-bar item. */
10681 clear_mouse_face (dpyinfo);
10682 return;
10684 else if (rc == 0)
10685 /* On same tool-bar item as before. */
10686 goto set_help_echo;
10688 clear_mouse_face (dpyinfo);
10690 /* Mouse is down, but on different tool-bar item? */
10691 mouse_down_p = (dpyinfo->grabbed
10692 && f == last_mouse_frame
10693 && FRAME_LIVE_P (f));
10694 if (mouse_down_p
10695 && last_tool_bar_item != prop_idx)
10696 return;
10698 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10699 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10701 /* If tool-bar item is not enabled, don't highlight it. */
10702 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10703 if (!NILP (enabled_p))
10705 /* Compute the x-position of the glyph. In front and past the
10706 image is a space. We include this in the highlighted area. */
10707 row = MATRIX_ROW (w->current_matrix, vpos);
10708 for (i = x = 0; i < hpos; ++i)
10709 x += row->glyphs[TEXT_AREA][i].pixel_width;
10711 /* Record this as the current active region. */
10712 dpyinfo->mouse_face_beg_col = hpos;
10713 dpyinfo->mouse_face_beg_row = vpos;
10714 dpyinfo->mouse_face_beg_x = x;
10715 dpyinfo->mouse_face_beg_y = row->y;
10716 dpyinfo->mouse_face_past_end = 0;
10718 dpyinfo->mouse_face_end_col = hpos + 1;
10719 dpyinfo->mouse_face_end_row = vpos;
10720 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10721 dpyinfo->mouse_face_end_y = row->y;
10722 dpyinfo->mouse_face_window = window;
10723 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10725 /* Display it as active. */
10726 show_mouse_face (dpyinfo, draw);
10727 dpyinfo->mouse_face_image_state = draw;
10730 set_help_echo:
10732 /* Set help_echo_string to a help string to display for this tool-bar item.
10733 XTread_socket does the rest. */
10734 help_echo_object = help_echo_window = Qnil;
10735 help_echo_pos = -1;
10736 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10737 if (NILP (help_echo_string))
10738 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10741 #endif /* HAVE_WINDOW_SYSTEM */
10745 /************************************************************************
10746 Horizontal scrolling
10747 ************************************************************************/
10749 static int hscroll_window_tree P_ ((Lisp_Object));
10750 static int hscroll_windows P_ ((Lisp_Object));
10752 /* For all leaf windows in the window tree rooted at WINDOW, set their
10753 hscroll value so that PT is (i) visible in the window, and (ii) so
10754 that it is not within a certain margin at the window's left and
10755 right border. Value is non-zero if any window's hscroll has been
10756 changed. */
10758 static int
10759 hscroll_window_tree (window)
10760 Lisp_Object window;
10762 int hscrolled_p = 0;
10763 int hscroll_relative_p = FLOATP (Vhscroll_step);
10764 int hscroll_step_abs = 0;
10765 double hscroll_step_rel = 0;
10767 if (hscroll_relative_p)
10769 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10770 if (hscroll_step_rel < 0)
10772 hscroll_relative_p = 0;
10773 hscroll_step_abs = 0;
10776 else if (INTEGERP (Vhscroll_step))
10778 hscroll_step_abs = XINT (Vhscroll_step);
10779 if (hscroll_step_abs < 0)
10780 hscroll_step_abs = 0;
10782 else
10783 hscroll_step_abs = 0;
10785 while (WINDOWP (window))
10787 struct window *w = XWINDOW (window);
10789 if (WINDOWP (w->hchild))
10790 hscrolled_p |= hscroll_window_tree (w->hchild);
10791 else if (WINDOWP (w->vchild))
10792 hscrolled_p |= hscroll_window_tree (w->vchild);
10793 else if (w->cursor.vpos >= 0)
10795 int h_margin;
10796 int text_area_width;
10797 struct glyph_row *current_cursor_row
10798 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10799 struct glyph_row *desired_cursor_row
10800 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10801 struct glyph_row *cursor_row
10802 = (desired_cursor_row->enabled_p
10803 ? desired_cursor_row
10804 : current_cursor_row);
10806 text_area_width = window_box_width (w, TEXT_AREA);
10808 /* Scroll when cursor is inside this scroll margin. */
10809 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10811 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10812 && ((XFASTINT (w->hscroll)
10813 && w->cursor.x <= h_margin)
10814 || (cursor_row->enabled_p
10815 && cursor_row->truncated_on_right_p
10816 && (w->cursor.x >= text_area_width - h_margin))))
10818 struct it it;
10819 int hscroll;
10820 struct buffer *saved_current_buffer;
10821 int pt;
10822 int wanted_x;
10824 /* Find point in a display of infinite width. */
10825 saved_current_buffer = current_buffer;
10826 current_buffer = XBUFFER (w->buffer);
10828 if (w == XWINDOW (selected_window))
10829 pt = BUF_PT (current_buffer);
10830 else
10832 pt = marker_position (w->pointm);
10833 pt = max (BEGV, pt);
10834 pt = min (ZV, pt);
10837 /* Move iterator to pt starting at cursor_row->start in
10838 a line with infinite width. */
10839 init_to_row_start (&it, w, cursor_row);
10840 it.last_visible_x = INFINITY;
10841 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10842 current_buffer = saved_current_buffer;
10844 /* Position cursor in window. */
10845 if (!hscroll_relative_p && hscroll_step_abs == 0)
10846 hscroll = max (0, (it.current_x
10847 - (ITERATOR_AT_END_OF_LINE_P (&it)
10848 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10849 : (text_area_width / 2))))
10850 / FRAME_COLUMN_WIDTH (it.f);
10851 else if (w->cursor.x >= text_area_width - h_margin)
10853 if (hscroll_relative_p)
10854 wanted_x = text_area_width * (1 - hscroll_step_rel)
10855 - h_margin;
10856 else
10857 wanted_x = text_area_width
10858 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10859 - h_margin;
10860 hscroll
10861 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10863 else
10865 if (hscroll_relative_p)
10866 wanted_x = text_area_width * hscroll_step_rel
10867 + h_margin;
10868 else
10869 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10870 + h_margin;
10871 hscroll
10872 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10874 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10876 /* Don't call Fset_window_hscroll if value hasn't
10877 changed because it will prevent redisplay
10878 optimizations. */
10879 if (XFASTINT (w->hscroll) != hscroll)
10881 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10882 w->hscroll = make_number (hscroll);
10883 hscrolled_p = 1;
10888 window = w->next;
10891 /* Value is non-zero if hscroll of any leaf window has been changed. */
10892 return hscrolled_p;
10896 /* Set hscroll so that cursor is visible and not inside horizontal
10897 scroll margins for all windows in the tree rooted at WINDOW. See
10898 also hscroll_window_tree above. Value is non-zero if any window's
10899 hscroll has been changed. If it has, desired matrices on the frame
10900 of WINDOW are cleared. */
10902 static int
10903 hscroll_windows (window)
10904 Lisp_Object window;
10906 int hscrolled_p = hscroll_window_tree (window);
10907 if (hscrolled_p)
10908 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10909 return hscrolled_p;
10914 /************************************************************************
10915 Redisplay
10916 ************************************************************************/
10918 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10919 to a non-zero value. This is sometimes handy to have in a debugger
10920 session. */
10922 #if GLYPH_DEBUG
10924 /* First and last unchanged row for try_window_id. */
10926 int debug_first_unchanged_at_end_vpos;
10927 int debug_last_unchanged_at_beg_vpos;
10929 /* Delta vpos and y. */
10931 int debug_dvpos, debug_dy;
10933 /* Delta in characters and bytes for try_window_id. */
10935 int debug_delta, debug_delta_bytes;
10937 /* Values of window_end_pos and window_end_vpos at the end of
10938 try_window_id. */
10940 EMACS_INT debug_end_pos, debug_end_vpos;
10942 /* Append a string to W->desired_matrix->method. FMT is a printf
10943 format string. A1...A9 are a supplement for a variable-length
10944 argument list. If trace_redisplay_p is non-zero also printf the
10945 resulting string to stderr. */
10947 static void
10948 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10949 struct window *w;
10950 char *fmt;
10951 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10953 char buffer[512];
10954 char *method = w->desired_matrix->method;
10955 int len = strlen (method);
10956 int size = sizeof w->desired_matrix->method;
10957 int remaining = size - len - 1;
10959 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10960 if (len && remaining)
10962 method[len] = '|';
10963 --remaining, ++len;
10966 strncpy (method + len, buffer, remaining);
10968 if (trace_redisplay_p)
10969 fprintf (stderr, "%p (%s): %s\n",
10971 ((BUFFERP (w->buffer)
10972 && STRINGP (XBUFFER (w->buffer)->name))
10973 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10974 : "no buffer"),
10975 buffer);
10978 #endif /* GLYPH_DEBUG */
10981 /* Value is non-zero if all changes in window W, which displays
10982 current_buffer, are in the text between START and END. START is a
10983 buffer position, END is given as a distance from Z. Used in
10984 redisplay_internal for display optimization. */
10986 static INLINE int
10987 text_outside_line_unchanged_p (w, start, end)
10988 struct window *w;
10989 int start, end;
10991 int unchanged_p = 1;
10993 /* If text or overlays have changed, see where. */
10994 if (XFASTINT (w->last_modified) < MODIFF
10995 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10997 /* Gap in the line? */
10998 if (GPT < start || Z - GPT < end)
10999 unchanged_p = 0;
11001 /* Changes start in front of the line, or end after it? */
11002 if (unchanged_p
11003 && (BEG_UNCHANGED < start - 1
11004 || END_UNCHANGED < end))
11005 unchanged_p = 0;
11007 /* If selective display, can't optimize if changes start at the
11008 beginning of the line. */
11009 if (unchanged_p
11010 && INTEGERP (current_buffer->selective_display)
11011 && XINT (current_buffer->selective_display) > 0
11012 && (BEG_UNCHANGED < start || GPT <= start))
11013 unchanged_p = 0;
11015 /* If there are overlays at the start or end of the line, these
11016 may have overlay strings with newlines in them. A change at
11017 START, for instance, may actually concern the display of such
11018 overlay strings as well, and they are displayed on different
11019 lines. So, quickly rule out this case. (For the future, it
11020 might be desirable to implement something more telling than
11021 just BEG/END_UNCHANGED.) */
11022 if (unchanged_p)
11024 if (BEG + BEG_UNCHANGED == start
11025 && overlay_touches_p (start))
11026 unchanged_p = 0;
11027 if (END_UNCHANGED == end
11028 && overlay_touches_p (Z - end))
11029 unchanged_p = 0;
11033 return unchanged_p;
11037 /* Do a frame update, taking possible shortcuts into account. This is
11038 the main external entry point for redisplay.
11040 If the last redisplay displayed an echo area message and that message
11041 is no longer requested, we clear the echo area or bring back the
11042 mini-buffer if that is in use. */
11044 void
11045 redisplay ()
11047 redisplay_internal (0);
11051 static Lisp_Object
11052 overlay_arrow_string_or_property (var)
11053 Lisp_Object var;
11055 Lisp_Object val;
11057 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11058 return val;
11060 return Voverlay_arrow_string;
11063 /* Return 1 if there are any overlay-arrows in current_buffer. */
11064 static int
11065 overlay_arrow_in_current_buffer_p ()
11067 Lisp_Object vlist;
11069 for (vlist = Voverlay_arrow_variable_list;
11070 CONSP (vlist);
11071 vlist = XCDR (vlist))
11073 Lisp_Object var = XCAR (vlist);
11074 Lisp_Object val;
11076 if (!SYMBOLP (var))
11077 continue;
11078 val = find_symbol_value (var);
11079 if (MARKERP (val)
11080 && current_buffer == XMARKER (val)->buffer)
11081 return 1;
11083 return 0;
11087 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11088 has changed. */
11090 static int
11091 overlay_arrows_changed_p ()
11093 Lisp_Object vlist;
11095 for (vlist = Voverlay_arrow_variable_list;
11096 CONSP (vlist);
11097 vlist = XCDR (vlist))
11099 Lisp_Object var = XCAR (vlist);
11100 Lisp_Object val, pstr;
11102 if (!SYMBOLP (var))
11103 continue;
11104 val = find_symbol_value (var);
11105 if (!MARKERP (val))
11106 continue;
11107 if (! EQ (COERCE_MARKER (val),
11108 Fget (var, Qlast_arrow_position))
11109 || ! (pstr = overlay_arrow_string_or_property (var),
11110 EQ (pstr, Fget (var, Qlast_arrow_string))))
11111 return 1;
11113 return 0;
11116 /* Mark overlay arrows to be updated on next redisplay. */
11118 static void
11119 update_overlay_arrows (up_to_date)
11120 int up_to_date;
11122 Lisp_Object vlist;
11124 for (vlist = Voverlay_arrow_variable_list;
11125 CONSP (vlist);
11126 vlist = XCDR (vlist))
11128 Lisp_Object var = XCAR (vlist);
11130 if (!SYMBOLP (var))
11131 continue;
11133 if (up_to_date > 0)
11135 Lisp_Object val = find_symbol_value (var);
11136 Fput (var, Qlast_arrow_position,
11137 COERCE_MARKER (val));
11138 Fput (var, Qlast_arrow_string,
11139 overlay_arrow_string_or_property (var));
11141 else if (up_to_date < 0
11142 || !NILP (Fget (var, Qlast_arrow_position)))
11144 Fput (var, Qlast_arrow_position, Qt);
11145 Fput (var, Qlast_arrow_string, Qt);
11151 /* Return overlay arrow string to display at row.
11152 Return integer (bitmap number) for arrow bitmap in left fringe.
11153 Return nil if no overlay arrow. */
11155 static Lisp_Object
11156 overlay_arrow_at_row (it, row)
11157 struct it *it;
11158 struct glyph_row *row;
11160 Lisp_Object vlist;
11162 for (vlist = Voverlay_arrow_variable_list;
11163 CONSP (vlist);
11164 vlist = XCDR (vlist))
11166 Lisp_Object var = XCAR (vlist);
11167 Lisp_Object val;
11169 if (!SYMBOLP (var))
11170 continue;
11172 val = find_symbol_value (var);
11174 if (MARKERP (val)
11175 && current_buffer == XMARKER (val)->buffer
11176 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11178 if (FRAME_WINDOW_P (it->f)
11179 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11181 #ifdef HAVE_WINDOW_SYSTEM
11182 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11184 int fringe_bitmap;
11185 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11186 return make_number (fringe_bitmap);
11188 #endif
11189 return make_number (-1); /* Use default arrow bitmap */
11191 return overlay_arrow_string_or_property (var);
11195 return Qnil;
11198 /* Return 1 if point moved out of or into a composition. Otherwise
11199 return 0. PREV_BUF and PREV_PT are the last point buffer and
11200 position. BUF and PT are the current point buffer and position. */
11203 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11204 struct buffer *prev_buf, *buf;
11205 int prev_pt, pt;
11207 EMACS_INT start, end;
11208 Lisp_Object prop;
11209 Lisp_Object buffer;
11211 XSETBUFFER (buffer, buf);
11212 /* Check a composition at the last point if point moved within the
11213 same buffer. */
11214 if (prev_buf == buf)
11216 if (prev_pt == pt)
11217 /* Point didn't move. */
11218 return 0;
11220 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11221 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11222 && COMPOSITION_VALID_P (start, end, prop)
11223 && start < prev_pt && end > prev_pt)
11224 /* The last point was within the composition. Return 1 iff
11225 point moved out of the composition. */
11226 return (pt <= start || pt >= end);
11229 /* Check a composition at the current point. */
11230 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11231 && find_composition (pt, -1, &start, &end, &prop, buffer)
11232 && COMPOSITION_VALID_P (start, end, prop)
11233 && start < pt && end > pt);
11237 /* Reconsider the setting of B->clip_changed which is displayed
11238 in window W. */
11240 static INLINE void
11241 reconsider_clip_changes (w, b)
11242 struct window *w;
11243 struct buffer *b;
11245 if (b->clip_changed
11246 && !NILP (w->window_end_valid)
11247 && w->current_matrix->buffer == b
11248 && w->current_matrix->zv == BUF_ZV (b)
11249 && w->current_matrix->begv == BUF_BEGV (b))
11250 b->clip_changed = 0;
11252 /* If display wasn't paused, and W is not a tool bar window, see if
11253 point has been moved into or out of a composition. In that case,
11254 we set b->clip_changed to 1 to force updating the screen. If
11255 b->clip_changed has already been set to 1, we can skip this
11256 check. */
11257 if (!b->clip_changed
11258 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11260 int pt;
11262 if (w == XWINDOW (selected_window))
11263 pt = BUF_PT (current_buffer);
11264 else
11265 pt = marker_position (w->pointm);
11267 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11268 || pt != XINT (w->last_point))
11269 && check_point_in_composition (w->current_matrix->buffer,
11270 XINT (w->last_point),
11271 XBUFFER (w->buffer), pt))
11272 b->clip_changed = 1;
11277 /* Select FRAME to forward the values of frame-local variables into C
11278 variables so that the redisplay routines can access those values
11279 directly. */
11281 static void
11282 select_frame_for_redisplay (frame)
11283 Lisp_Object frame;
11285 Lisp_Object tail, symbol, val;
11286 Lisp_Object old = selected_frame;
11287 struct Lisp_Symbol *sym;
11289 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11291 selected_frame = frame;
11295 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11296 if (CONSP (XCAR (tail))
11297 && (symbol = XCAR (XCAR (tail)),
11298 SYMBOLP (symbol))
11299 && (sym = indirect_variable (XSYMBOL (symbol)),
11300 val = sym->value,
11301 (BUFFER_LOCAL_VALUEP (val)))
11302 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11303 /* Use find_symbol_value rather than Fsymbol_value
11304 to avoid an error if it is void. */
11305 find_symbol_value (symbol);
11306 } while (!EQ (frame, old) && (frame = old, 1));
11310 #define STOP_POLLING \
11311 do { if (! polling_stopped_here) stop_polling (); \
11312 polling_stopped_here = 1; } while (0)
11314 #define RESUME_POLLING \
11315 do { if (polling_stopped_here) start_polling (); \
11316 polling_stopped_here = 0; } while (0)
11319 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11320 response to any user action; therefore, we should preserve the echo
11321 area. (Actually, our caller does that job.) Perhaps in the future
11322 avoid recentering windows if it is not necessary; currently that
11323 causes some problems. */
11325 static void
11326 redisplay_internal (preserve_echo_area)
11327 int preserve_echo_area;
11329 struct window *w = XWINDOW (selected_window);
11330 struct frame *f;
11331 int pause;
11332 int must_finish = 0;
11333 struct text_pos tlbufpos, tlendpos;
11334 int number_of_visible_frames;
11335 int count, count1;
11336 struct frame *sf;
11337 int polling_stopped_here = 0;
11338 Lisp_Object old_frame = selected_frame;
11340 /* Non-zero means redisplay has to consider all windows on all
11341 frames. Zero means, only selected_window is considered. */
11342 int consider_all_windows_p;
11344 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11346 /* No redisplay if running in batch mode or frame is not yet fully
11347 initialized, or redisplay is explicitly turned off by setting
11348 Vinhibit_redisplay. */
11349 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11350 || !NILP (Vinhibit_redisplay))
11351 return;
11353 /* Don't examine these until after testing Vinhibit_redisplay.
11354 When Emacs is shutting down, perhaps because its connection to
11355 X has dropped, we should not look at them at all. */
11356 f = XFRAME (w->frame);
11357 sf = SELECTED_FRAME ();
11359 if (!f->glyphs_initialized_p)
11360 return;
11362 /* The flag redisplay_performed_directly_p is set by
11363 direct_output_for_insert when it already did the whole screen
11364 update necessary. */
11365 if (redisplay_performed_directly_p)
11367 redisplay_performed_directly_p = 0;
11368 if (!hscroll_windows (selected_window))
11369 return;
11372 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11373 if (popup_activated ())
11374 return;
11375 #endif
11377 /* I don't think this happens but let's be paranoid. */
11378 if (redisplaying_p)
11379 return;
11381 /* Record a function that resets redisplaying_p to its old value
11382 when we leave this function. */
11383 count = SPECPDL_INDEX ();
11384 record_unwind_protect (unwind_redisplay,
11385 Fcons (make_number (redisplaying_p), selected_frame));
11386 ++redisplaying_p;
11387 specbind (Qinhibit_free_realized_faces, Qnil);
11390 Lisp_Object tail, frame;
11392 FOR_EACH_FRAME (tail, frame)
11394 struct frame *f = XFRAME (frame);
11395 f->already_hscrolled_p = 0;
11399 retry:
11400 if (!EQ (old_frame, selected_frame)
11401 && FRAME_LIVE_P (XFRAME (old_frame)))
11402 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11403 selected_frame and selected_window to be temporarily out-of-sync so
11404 when we come back here via `goto retry', we need to resync because we
11405 may need to run Elisp code (via prepare_menu_bars). */
11406 select_frame_for_redisplay (old_frame);
11408 pause = 0;
11409 reconsider_clip_changes (w, current_buffer);
11410 last_escape_glyph_frame = NULL;
11411 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11413 /* If new fonts have been loaded that make a glyph matrix adjustment
11414 necessary, do it. */
11415 if (fonts_changed_p)
11417 adjust_glyphs (NULL);
11418 ++windows_or_buffers_changed;
11419 fonts_changed_p = 0;
11422 /* If face_change_count is non-zero, init_iterator will free all
11423 realized faces, which includes the faces referenced from current
11424 matrices. So, we can't reuse current matrices in this case. */
11425 if (face_change_count)
11426 ++windows_or_buffers_changed;
11428 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11429 && FRAME_TTY (sf)->previous_frame != sf)
11431 /* Since frames on a single ASCII terminal share the same
11432 display area, displaying a different frame means redisplay
11433 the whole thing. */
11434 windows_or_buffers_changed++;
11435 SET_FRAME_GARBAGED (sf);
11436 #ifndef DOS_NT
11437 set_tty_color_mode (FRAME_TTY (sf), sf);
11438 #endif
11439 FRAME_TTY (sf)->previous_frame = sf;
11442 /* Set the visible flags for all frames. Do this before checking
11443 for resized or garbaged frames; they want to know if their frames
11444 are visible. See the comment in frame.h for
11445 FRAME_SAMPLE_VISIBILITY. */
11447 Lisp_Object tail, frame;
11449 number_of_visible_frames = 0;
11451 FOR_EACH_FRAME (tail, frame)
11453 struct frame *f = XFRAME (frame);
11455 FRAME_SAMPLE_VISIBILITY (f);
11456 if (FRAME_VISIBLE_P (f))
11457 ++number_of_visible_frames;
11458 clear_desired_matrices (f);
11462 /* Notice any pending interrupt request to change frame size. */
11463 do_pending_window_change (1);
11465 /* Clear frames marked as garbaged. */
11466 if (frame_garbaged)
11467 clear_garbaged_frames ();
11469 /* Build menubar and tool-bar items. */
11470 if (NILP (Vmemory_full))
11471 prepare_menu_bars ();
11473 if (windows_or_buffers_changed)
11474 update_mode_lines++;
11476 /* Detect case that we need to write or remove a star in the mode line. */
11477 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11479 w->update_mode_line = Qt;
11480 if (buffer_shared > 1)
11481 update_mode_lines++;
11484 /* Avoid invocation of point motion hooks by `current_column' below. */
11485 count1 = SPECPDL_INDEX ();
11486 specbind (Qinhibit_point_motion_hooks, Qt);
11488 /* If %c is in the mode line, update it if needed. */
11489 if (!NILP (w->column_number_displayed)
11490 /* This alternative quickly identifies a common case
11491 where no change is needed. */
11492 && !(PT == XFASTINT (w->last_point)
11493 && XFASTINT (w->last_modified) >= MODIFF
11494 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11495 && (XFASTINT (w->column_number_displayed)
11496 != (int) current_column ())) /* iftc */
11497 w->update_mode_line = Qt;
11499 unbind_to (count1, Qnil);
11501 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11503 /* The variable buffer_shared is set in redisplay_window and
11504 indicates that we redisplay a buffer in different windows. See
11505 there. */
11506 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11507 || cursor_type_changed);
11509 /* If specs for an arrow have changed, do thorough redisplay
11510 to ensure we remove any arrow that should no longer exist. */
11511 if (overlay_arrows_changed_p ())
11512 consider_all_windows_p = windows_or_buffers_changed = 1;
11514 /* Normally the message* functions will have already displayed and
11515 updated the echo area, but the frame may have been trashed, or
11516 the update may have been preempted, so display the echo area
11517 again here. Checking message_cleared_p captures the case that
11518 the echo area should be cleared. */
11519 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11520 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11521 || (message_cleared_p
11522 && minibuf_level == 0
11523 /* If the mini-window is currently selected, this means the
11524 echo-area doesn't show through. */
11525 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11527 int window_height_changed_p = echo_area_display (0);
11528 must_finish = 1;
11530 /* If we don't display the current message, don't clear the
11531 message_cleared_p flag, because, if we did, we wouldn't clear
11532 the echo area in the next redisplay which doesn't preserve
11533 the echo area. */
11534 if (!display_last_displayed_message_p)
11535 message_cleared_p = 0;
11537 if (fonts_changed_p)
11538 goto retry;
11539 else if (window_height_changed_p)
11541 consider_all_windows_p = 1;
11542 ++update_mode_lines;
11543 ++windows_or_buffers_changed;
11545 /* If window configuration was changed, frames may have been
11546 marked garbaged. Clear them or we will experience
11547 surprises wrt scrolling. */
11548 if (frame_garbaged)
11549 clear_garbaged_frames ();
11552 else if (EQ (selected_window, minibuf_window)
11553 && (current_buffer->clip_changed
11554 || XFASTINT (w->last_modified) < MODIFF
11555 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11556 && resize_mini_window (w, 0))
11558 /* Resized active mini-window to fit the size of what it is
11559 showing if its contents might have changed. */
11560 must_finish = 1;
11561 /* FIXME: this causes all frames to be updated, which seems unnecessary
11562 since only the current frame needs to be considered. This function needs
11563 to be rewritten with two variables, consider_all_windows and
11564 consider_all_frames. */
11565 consider_all_windows_p = 1;
11566 ++windows_or_buffers_changed;
11567 ++update_mode_lines;
11569 /* If window configuration was changed, frames may have been
11570 marked garbaged. Clear them or we will experience
11571 surprises wrt scrolling. */
11572 if (frame_garbaged)
11573 clear_garbaged_frames ();
11577 /* If showing the region, and mark has changed, we must redisplay
11578 the whole window. The assignment to this_line_start_pos prevents
11579 the optimization directly below this if-statement. */
11580 if (((!NILP (Vtransient_mark_mode)
11581 && !NILP (XBUFFER (w->buffer)->mark_active))
11582 != !NILP (w->region_showing))
11583 || (!NILP (w->region_showing)
11584 && !EQ (w->region_showing,
11585 Fmarker_position (XBUFFER (w->buffer)->mark))))
11586 CHARPOS (this_line_start_pos) = 0;
11588 /* Optimize the case that only the line containing the cursor in the
11589 selected window has changed. Variables starting with this_ are
11590 set in display_line and record information about the line
11591 containing the cursor. */
11592 tlbufpos = this_line_start_pos;
11593 tlendpos = this_line_end_pos;
11594 if (!consider_all_windows_p
11595 && CHARPOS (tlbufpos) > 0
11596 && NILP (w->update_mode_line)
11597 && !current_buffer->clip_changed
11598 && !current_buffer->prevent_redisplay_optimizations_p
11599 && FRAME_VISIBLE_P (XFRAME (w->frame))
11600 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11601 /* Make sure recorded data applies to current buffer, etc. */
11602 && this_line_buffer == current_buffer
11603 && current_buffer == XBUFFER (w->buffer)
11604 && NILP (w->force_start)
11605 && NILP (w->optional_new_start)
11606 /* Point must be on the line that we have info recorded about. */
11607 && PT >= CHARPOS (tlbufpos)
11608 && PT <= Z - CHARPOS (tlendpos)
11609 /* All text outside that line, including its final newline,
11610 must be unchanged. */
11611 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11612 CHARPOS (tlendpos)))
11614 if (CHARPOS (tlbufpos) > BEGV
11615 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11616 && (CHARPOS (tlbufpos) == ZV
11617 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11618 /* Former continuation line has disappeared by becoming empty. */
11619 goto cancel;
11620 else if (XFASTINT (w->last_modified) < MODIFF
11621 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11622 || MINI_WINDOW_P (w))
11624 /* We have to handle the case of continuation around a
11625 wide-column character (see the comment in indent.c around
11626 line 1340).
11628 For instance, in the following case:
11630 -------- Insert --------
11631 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11632 J_I_ ==> J_I_ `^^' are cursors.
11633 ^^ ^^
11634 -------- --------
11636 As we have to redraw the line above, we cannot use this
11637 optimization. */
11639 struct it it;
11640 int line_height_before = this_line_pixel_height;
11642 /* Note that start_display will handle the case that the
11643 line starting at tlbufpos is a continuation line. */
11644 start_display (&it, w, tlbufpos);
11646 /* Implementation note: It this still necessary? */
11647 if (it.current_x != this_line_start_x)
11648 goto cancel;
11650 TRACE ((stderr, "trying display optimization 1\n"));
11651 w->cursor.vpos = -1;
11652 overlay_arrow_seen = 0;
11653 it.vpos = this_line_vpos;
11654 it.current_y = this_line_y;
11655 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11656 display_line (&it);
11658 /* If line contains point, is not continued,
11659 and ends at same distance from eob as before, we win. */
11660 if (w->cursor.vpos >= 0
11661 /* Line is not continued, otherwise this_line_start_pos
11662 would have been set to 0 in display_line. */
11663 && CHARPOS (this_line_start_pos)
11664 /* Line ends as before. */
11665 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11666 /* Line has same height as before. Otherwise other lines
11667 would have to be shifted up or down. */
11668 && this_line_pixel_height == line_height_before)
11670 /* If this is not the window's last line, we must adjust
11671 the charstarts of the lines below. */
11672 if (it.current_y < it.last_visible_y)
11674 struct glyph_row *row
11675 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11676 int delta, delta_bytes;
11678 /* We used to distinguish between two cases here,
11679 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11680 when the line ends in a newline or the end of the
11681 buffer's accessible portion. But both cases did
11682 the same, so they were collapsed. */
11683 delta = (Z
11684 - CHARPOS (tlendpos)
11685 - MATRIX_ROW_START_CHARPOS (row));
11686 delta_bytes = (Z_BYTE
11687 - BYTEPOS (tlendpos)
11688 - MATRIX_ROW_START_BYTEPOS (row));
11690 increment_matrix_positions (w->current_matrix,
11691 this_line_vpos + 1,
11692 w->current_matrix->nrows,
11693 delta, delta_bytes);
11696 /* If this row displays text now but previously didn't,
11697 or vice versa, w->window_end_vpos may have to be
11698 adjusted. */
11699 if ((it.glyph_row - 1)->displays_text_p)
11701 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11702 XSETINT (w->window_end_vpos, this_line_vpos);
11704 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11705 && this_line_vpos > 0)
11706 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11707 w->window_end_valid = Qnil;
11709 /* Update hint: No need to try to scroll in update_window. */
11710 w->desired_matrix->no_scrolling_p = 1;
11712 #if GLYPH_DEBUG
11713 *w->desired_matrix->method = 0;
11714 debug_method_add (w, "optimization 1");
11715 #endif
11716 #ifdef HAVE_WINDOW_SYSTEM
11717 update_window_fringes (w, 0);
11718 #endif
11719 goto update;
11721 else
11722 goto cancel;
11724 else if (/* Cursor position hasn't changed. */
11725 PT == XFASTINT (w->last_point)
11726 /* Make sure the cursor was last displayed
11727 in this window. Otherwise we have to reposition it. */
11728 && 0 <= w->cursor.vpos
11729 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11731 if (!must_finish)
11733 do_pending_window_change (1);
11735 /* We used to always goto end_of_redisplay here, but this
11736 isn't enough if we have a blinking cursor. */
11737 if (w->cursor_off_p == w->last_cursor_off_p)
11738 goto end_of_redisplay;
11740 goto update;
11742 /* If highlighting the region, or if the cursor is in the echo area,
11743 then we can't just move the cursor. */
11744 else if (! (!NILP (Vtransient_mark_mode)
11745 && !NILP (current_buffer->mark_active))
11746 && (EQ (selected_window, current_buffer->last_selected_window)
11747 || highlight_nonselected_windows)
11748 && NILP (w->region_showing)
11749 && NILP (Vshow_trailing_whitespace)
11750 && !cursor_in_echo_area)
11752 struct it it;
11753 struct glyph_row *row;
11755 /* Skip from tlbufpos to PT and see where it is. Note that
11756 PT may be in invisible text. If so, we will end at the
11757 next visible position. */
11758 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11759 NULL, DEFAULT_FACE_ID);
11760 it.current_x = this_line_start_x;
11761 it.current_y = this_line_y;
11762 it.vpos = this_line_vpos;
11764 /* The call to move_it_to stops in front of PT, but
11765 moves over before-strings. */
11766 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11768 if (it.vpos == this_line_vpos
11769 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11770 row->enabled_p))
11772 xassert (this_line_vpos == it.vpos);
11773 xassert (this_line_y == it.current_y);
11774 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11775 #if GLYPH_DEBUG
11776 *w->desired_matrix->method = 0;
11777 debug_method_add (w, "optimization 3");
11778 #endif
11779 goto update;
11781 else
11782 goto cancel;
11785 cancel:
11786 /* Text changed drastically or point moved off of line. */
11787 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11790 CHARPOS (this_line_start_pos) = 0;
11791 consider_all_windows_p |= buffer_shared > 1;
11792 ++clear_face_cache_count;
11793 #ifdef HAVE_WINDOW_SYSTEM
11794 ++clear_image_cache_count;
11795 #endif
11797 /* Build desired matrices, and update the display. If
11798 consider_all_windows_p is non-zero, do it for all windows on all
11799 frames. Otherwise do it for selected_window, only. */
11801 if (consider_all_windows_p)
11803 Lisp_Object tail, frame;
11805 FOR_EACH_FRAME (tail, frame)
11806 XFRAME (frame)->updated_p = 0;
11808 /* Recompute # windows showing selected buffer. This will be
11809 incremented each time such a window is displayed. */
11810 buffer_shared = 0;
11812 FOR_EACH_FRAME (tail, frame)
11814 struct frame *f = XFRAME (frame);
11816 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11818 if (! EQ (frame, selected_frame))
11819 /* Select the frame, for the sake of frame-local
11820 variables. */
11821 select_frame_for_redisplay (frame);
11823 /* Mark all the scroll bars to be removed; we'll redeem
11824 the ones we want when we redisplay their windows. */
11825 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11826 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11828 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11829 redisplay_windows (FRAME_ROOT_WINDOW (f));
11831 /* The X error handler may have deleted that frame. */
11832 if (!FRAME_LIVE_P (f))
11833 continue;
11835 /* Any scroll bars which redisplay_windows should have
11836 nuked should now go away. */
11837 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11838 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11840 /* If fonts changed, display again. */
11841 /* ??? rms: I suspect it is a mistake to jump all the way
11842 back to retry here. It should just retry this frame. */
11843 if (fonts_changed_p)
11844 goto retry;
11846 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11848 /* See if we have to hscroll. */
11849 if (!f->already_hscrolled_p)
11851 f->already_hscrolled_p = 1;
11852 if (hscroll_windows (f->root_window))
11853 goto retry;
11856 /* Prevent various kinds of signals during display
11857 update. stdio is not robust about handling
11858 signals, which can cause an apparent I/O
11859 error. */
11860 if (interrupt_input)
11861 unrequest_sigio ();
11862 STOP_POLLING;
11864 /* Update the display. */
11865 set_window_update_flags (XWINDOW (f->root_window), 1);
11866 pause |= update_frame (f, 0, 0);
11867 f->updated_p = 1;
11872 if (!EQ (old_frame, selected_frame)
11873 && FRAME_LIVE_P (XFRAME (old_frame)))
11874 /* We played a bit fast-and-loose above and allowed selected_frame
11875 and selected_window to be temporarily out-of-sync but let's make
11876 sure this stays contained. */
11877 select_frame_for_redisplay (old_frame);
11878 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11880 if (!pause)
11882 /* Do the mark_window_display_accurate after all windows have
11883 been redisplayed because this call resets flags in buffers
11884 which are needed for proper redisplay. */
11885 FOR_EACH_FRAME (tail, frame)
11887 struct frame *f = XFRAME (frame);
11888 if (f->updated_p)
11890 mark_window_display_accurate (f->root_window, 1);
11891 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11892 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11897 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11899 Lisp_Object mini_window;
11900 struct frame *mini_frame;
11902 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11903 /* Use list_of_error, not Qerror, so that
11904 we catch only errors and don't run the debugger. */
11905 internal_condition_case_1 (redisplay_window_1, selected_window,
11906 list_of_error,
11907 redisplay_window_error);
11909 /* Compare desired and current matrices, perform output. */
11911 update:
11912 /* If fonts changed, display again. */
11913 if (fonts_changed_p)
11914 goto retry;
11916 /* Prevent various kinds of signals during display update.
11917 stdio is not robust about handling signals,
11918 which can cause an apparent I/O error. */
11919 if (interrupt_input)
11920 unrequest_sigio ();
11921 STOP_POLLING;
11923 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11925 if (hscroll_windows (selected_window))
11926 goto retry;
11928 XWINDOW (selected_window)->must_be_updated_p = 1;
11929 pause = update_frame (sf, 0, 0);
11932 /* We may have called echo_area_display at the top of this
11933 function. If the echo area is on another frame, that may
11934 have put text on a frame other than the selected one, so the
11935 above call to update_frame would not have caught it. Catch
11936 it here. */
11937 mini_window = FRAME_MINIBUF_WINDOW (sf);
11938 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11940 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11942 XWINDOW (mini_window)->must_be_updated_p = 1;
11943 pause |= update_frame (mini_frame, 0, 0);
11944 if (!pause && hscroll_windows (mini_window))
11945 goto retry;
11949 /* If display was paused because of pending input, make sure we do a
11950 thorough update the next time. */
11951 if (pause)
11953 /* Prevent the optimization at the beginning of
11954 redisplay_internal that tries a single-line update of the
11955 line containing the cursor in the selected window. */
11956 CHARPOS (this_line_start_pos) = 0;
11958 /* Let the overlay arrow be updated the next time. */
11959 update_overlay_arrows (0);
11961 /* If we pause after scrolling, some rows in the current
11962 matrices of some windows are not valid. */
11963 if (!WINDOW_FULL_WIDTH_P (w)
11964 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11965 update_mode_lines = 1;
11967 else
11969 if (!consider_all_windows_p)
11971 /* This has already been done above if
11972 consider_all_windows_p is set. */
11973 mark_window_display_accurate_1 (w, 1);
11975 /* Say overlay arrows are up to date. */
11976 update_overlay_arrows (1);
11978 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11979 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11982 update_mode_lines = 0;
11983 windows_or_buffers_changed = 0;
11984 cursor_type_changed = 0;
11987 /* Start SIGIO interrupts coming again. Having them off during the
11988 code above makes it less likely one will discard output, but not
11989 impossible, since there might be stuff in the system buffer here.
11990 But it is much hairier to try to do anything about that. */
11991 if (interrupt_input)
11992 request_sigio ();
11993 RESUME_POLLING;
11995 /* If a frame has become visible which was not before, redisplay
11996 again, so that we display it. Expose events for such a frame
11997 (which it gets when becoming visible) don't call the parts of
11998 redisplay constructing glyphs, so simply exposing a frame won't
11999 display anything in this case. So, we have to display these
12000 frames here explicitly. */
12001 if (!pause)
12003 Lisp_Object tail, frame;
12004 int new_count = 0;
12006 FOR_EACH_FRAME (tail, frame)
12008 int this_is_visible = 0;
12010 if (XFRAME (frame)->visible)
12011 this_is_visible = 1;
12012 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12013 if (XFRAME (frame)->visible)
12014 this_is_visible = 1;
12016 if (this_is_visible)
12017 new_count++;
12020 if (new_count != number_of_visible_frames)
12021 windows_or_buffers_changed++;
12024 /* Change frame size now if a change is pending. */
12025 do_pending_window_change (1);
12027 /* If we just did a pending size change, or have additional
12028 visible frames, redisplay again. */
12029 if (windows_or_buffers_changed && !pause)
12030 goto retry;
12032 /* Clear the face cache eventually. */
12033 if (consider_all_windows_p)
12035 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12037 clear_face_cache (0);
12038 clear_face_cache_count = 0;
12040 #ifdef HAVE_WINDOW_SYSTEM
12041 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12043 clear_image_caches (Qnil);
12044 clear_image_cache_count = 0;
12046 #endif /* HAVE_WINDOW_SYSTEM */
12049 end_of_redisplay:
12050 unbind_to (count, Qnil);
12051 RESUME_POLLING;
12055 /* Redisplay, but leave alone any recent echo area message unless
12056 another message has been requested in its place.
12058 This is useful in situations where you need to redisplay but no
12059 user action has occurred, making it inappropriate for the message
12060 area to be cleared. See tracking_off and
12061 wait_reading_process_output for examples of these situations.
12063 FROM_WHERE is an integer saying from where this function was
12064 called. This is useful for debugging. */
12066 void
12067 redisplay_preserve_echo_area (from_where)
12068 int from_where;
12070 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12072 if (!NILP (echo_area_buffer[1]))
12074 /* We have a previously displayed message, but no current
12075 message. Redisplay the previous message. */
12076 display_last_displayed_message_p = 1;
12077 redisplay_internal (1);
12078 display_last_displayed_message_p = 0;
12080 else
12081 redisplay_internal (1);
12083 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12084 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12085 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12089 /* Function registered with record_unwind_protect in
12090 redisplay_internal. Reset redisplaying_p to the value it had
12091 before redisplay_internal was called, and clear
12092 prevent_freeing_realized_faces_p. It also selects the previously
12093 selected frame, unless it has been deleted (by an X connection
12094 failure during redisplay, for example). */
12096 static Lisp_Object
12097 unwind_redisplay (val)
12098 Lisp_Object val;
12100 Lisp_Object old_redisplaying_p, old_frame;
12102 old_redisplaying_p = XCAR (val);
12103 redisplaying_p = XFASTINT (old_redisplaying_p);
12104 old_frame = XCDR (val);
12105 if (! EQ (old_frame, selected_frame)
12106 && FRAME_LIVE_P (XFRAME (old_frame)))
12107 select_frame_for_redisplay (old_frame);
12108 return Qnil;
12112 /* Mark the display of window W as accurate or inaccurate. If
12113 ACCURATE_P is non-zero mark display of W as accurate. If
12114 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12115 redisplay_internal is called. */
12117 static void
12118 mark_window_display_accurate_1 (w, accurate_p)
12119 struct window *w;
12120 int accurate_p;
12122 if (BUFFERP (w->buffer))
12124 struct buffer *b = XBUFFER (w->buffer);
12126 w->last_modified
12127 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12128 w->last_overlay_modified
12129 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12130 w->last_had_star
12131 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12133 if (accurate_p)
12135 b->clip_changed = 0;
12136 b->prevent_redisplay_optimizations_p = 0;
12138 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12139 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12140 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12141 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12143 w->current_matrix->buffer = b;
12144 w->current_matrix->begv = BUF_BEGV (b);
12145 w->current_matrix->zv = BUF_ZV (b);
12147 w->last_cursor = w->cursor;
12148 w->last_cursor_off_p = w->cursor_off_p;
12150 if (w == XWINDOW (selected_window))
12151 w->last_point = make_number (BUF_PT (b));
12152 else
12153 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12157 if (accurate_p)
12159 w->window_end_valid = w->buffer;
12160 w->update_mode_line = Qnil;
12165 /* Mark the display of windows in the window tree rooted at WINDOW as
12166 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12167 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12168 be redisplayed the next time redisplay_internal is called. */
12170 void
12171 mark_window_display_accurate (window, accurate_p)
12172 Lisp_Object window;
12173 int accurate_p;
12175 struct window *w;
12177 for (; !NILP (window); window = w->next)
12179 w = XWINDOW (window);
12180 mark_window_display_accurate_1 (w, accurate_p);
12182 if (!NILP (w->vchild))
12183 mark_window_display_accurate (w->vchild, accurate_p);
12184 if (!NILP (w->hchild))
12185 mark_window_display_accurate (w->hchild, accurate_p);
12188 if (accurate_p)
12190 update_overlay_arrows (1);
12192 else
12194 /* Force a thorough redisplay the next time by setting
12195 last_arrow_position and last_arrow_string to t, which is
12196 unequal to any useful value of Voverlay_arrow_... */
12197 update_overlay_arrows (-1);
12202 /* Return value in display table DP (Lisp_Char_Table *) for character
12203 C. Since a display table doesn't have any parent, we don't have to
12204 follow parent. Do not call this function directly but use the
12205 macro DISP_CHAR_VECTOR. */
12207 Lisp_Object
12208 disp_char_vector (dp, c)
12209 struct Lisp_Char_Table *dp;
12210 int c;
12212 Lisp_Object val;
12214 if (ASCII_CHAR_P (c))
12216 val = dp->ascii;
12217 if (SUB_CHAR_TABLE_P (val))
12218 val = XSUB_CHAR_TABLE (val)->contents[c];
12220 else
12222 Lisp_Object table;
12224 XSETCHAR_TABLE (table, dp);
12225 val = char_table_ref (table, c);
12227 if (NILP (val))
12228 val = dp->defalt;
12229 return val;
12234 /***********************************************************************
12235 Window Redisplay
12236 ***********************************************************************/
12238 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12240 static void
12241 redisplay_windows (window)
12242 Lisp_Object window;
12244 while (!NILP (window))
12246 struct window *w = XWINDOW (window);
12248 if (!NILP (w->hchild))
12249 redisplay_windows (w->hchild);
12250 else if (!NILP (w->vchild))
12251 redisplay_windows (w->vchild);
12252 else if (!NILP (w->buffer))
12254 displayed_buffer = XBUFFER (w->buffer);
12255 /* Use list_of_error, not Qerror, so that
12256 we catch only errors and don't run the debugger. */
12257 internal_condition_case_1 (redisplay_window_0, window,
12258 list_of_error,
12259 redisplay_window_error);
12262 window = w->next;
12266 static Lisp_Object
12267 redisplay_window_error ()
12269 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12270 return Qnil;
12273 static Lisp_Object
12274 redisplay_window_0 (window)
12275 Lisp_Object window;
12277 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12278 redisplay_window (window, 0);
12279 return Qnil;
12282 static Lisp_Object
12283 redisplay_window_1 (window)
12284 Lisp_Object window;
12286 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12287 redisplay_window (window, 1);
12288 return Qnil;
12292 /* Increment GLYPH until it reaches END or CONDITION fails while
12293 adding (GLYPH)->pixel_width to X. */
12295 #define SKIP_GLYPHS(glyph, end, x, condition) \
12296 do \
12298 (x) += (glyph)->pixel_width; \
12299 ++(glyph); \
12301 while ((glyph) < (end) && (condition))
12304 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12305 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12306 which positions recorded in ROW differ from current buffer
12307 positions.
12309 Return 0 if cursor is not on this row, 1 otherwise. */
12312 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12313 struct window *w;
12314 struct glyph_row *row;
12315 struct glyph_matrix *matrix;
12316 int delta, delta_bytes, dy, dvpos;
12318 struct glyph *glyph = row->glyphs[TEXT_AREA];
12319 struct glyph *end = glyph + row->used[TEXT_AREA];
12320 struct glyph *cursor = NULL;
12321 /* The first glyph that starts a sequence of glyphs from a string
12322 that is a value of a display property. */
12323 struct glyph *string_start;
12324 /* The X coordinate of string_start. */
12325 int string_start_x;
12326 /* The last known character position in row. */
12327 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12328 /* The last known character position before string_start. */
12329 int string_before_pos;
12330 int x = row->x;
12331 int cursor_x = x;
12332 /* Last buffer position covered by an overlay. */
12333 int cursor_from_overlay_pos = 0;
12334 int pt_old = PT - delta;
12336 /* Skip over glyphs not having an object at the start of the row.
12337 These are special glyphs like truncation marks on terminal
12338 frames. */
12339 if (row->displays_text_p)
12340 while (glyph < end
12341 && INTEGERP (glyph->object)
12342 && glyph->charpos < 0)
12344 x += glyph->pixel_width;
12345 ++glyph;
12348 string_start = NULL;
12349 while (glyph < end
12350 && !INTEGERP (glyph->object)
12351 && (!BUFFERP (glyph->object)
12352 || (last_pos = glyph->charpos) < pt_old
12353 || glyph->avoid_cursor_p))
12355 if (! STRINGP (glyph->object))
12357 string_start = NULL;
12358 x += glyph->pixel_width;
12359 ++glyph;
12360 /* If we are beyond the cursor position computed from the
12361 last overlay seen, that overlay is not in effect for
12362 current cursor position. Reset the cursor information
12363 computed from that overlay. */
12364 if (cursor_from_overlay_pos
12365 && last_pos >= cursor_from_overlay_pos)
12367 cursor_from_overlay_pos = 0;
12368 cursor = NULL;
12371 else
12373 if (string_start == NULL)
12375 string_before_pos = last_pos;
12376 string_start = glyph;
12377 string_start_x = x;
12379 /* Skip all glyphs from a string. */
12382 Lisp_Object cprop;
12383 int pos;
12384 if ((cursor == NULL || glyph > cursor)
12385 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12386 Qcursor, (glyph)->object),
12387 !NILP (cprop))
12388 && (pos = string_buffer_position (w, glyph->object,
12389 string_before_pos),
12390 (pos == 0 /* from overlay */
12391 || pos == pt_old)))
12393 /* Compute the first buffer position after the overlay.
12394 If the `cursor' property tells us how many positions
12395 are associated with the overlay, use that. Otherwise,
12396 estimate from the buffer positions of the glyphs
12397 before and after the overlay. */
12398 cursor_from_overlay_pos = (pos ? 0 : last_pos
12399 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12400 cursor = glyph;
12401 cursor_x = x;
12403 x += glyph->pixel_width;
12404 ++glyph;
12406 while (glyph < end && EQ (glyph->object, string_start->object));
12410 if (cursor != NULL)
12412 glyph = cursor;
12413 x = cursor_x;
12415 else if (row->ends_in_ellipsis_p && glyph == end)
12417 /* Scan back over the ellipsis glyphs, decrementing positions. */
12418 while (glyph > row->glyphs[TEXT_AREA]
12419 && (glyph - 1)->charpos == last_pos)
12420 glyph--, x -= glyph->pixel_width;
12421 /* That loop always goes one position too far, including the
12422 glyph before the ellipsis. So scan forward over that one. */
12423 x += glyph->pixel_width;
12424 glyph++;
12426 else if (string_start
12427 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12429 /* We may have skipped over point because the previous glyphs
12430 are from string. As there's no easy way to know the
12431 character position of the current glyph, find the correct
12432 glyph on point by scanning from string_start again. */
12433 Lisp_Object limit;
12434 Lisp_Object string;
12435 struct glyph *stop = glyph;
12436 int pos;
12438 limit = make_number (pt_old + 1);
12439 glyph = string_start;
12440 x = string_start_x;
12441 string = glyph->object;
12442 pos = string_buffer_position (w, string, string_before_pos);
12443 /* If POS == 0, STRING is from overlay. We skip such glyphs
12444 because we always put the cursor after overlay strings. */
12445 while (pos == 0 && glyph < stop)
12447 string = glyph->object;
12448 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12449 if (glyph < stop)
12450 pos = string_buffer_position (w, glyph->object, string_before_pos);
12453 while (glyph < stop)
12455 pos = XINT (Fnext_single_char_property_change
12456 (make_number (pos), Qdisplay, Qnil, limit));
12457 if (pos > pt_old)
12458 break;
12459 /* Skip glyphs from the same string. */
12460 string = glyph->object;
12461 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12462 /* Skip glyphs from an overlay. */
12463 while (glyph < stop
12464 && ! string_buffer_position (w, glyph->object, pos))
12466 string = glyph->object;
12467 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12471 /* If we reached the end of the line, and END was from a string,
12472 the cursor is not on this line. */
12473 if (glyph == end && row->continued_p)
12474 return 0;
12477 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12478 w->cursor.x = x;
12479 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12480 w->cursor.y = row->y + dy;
12482 if (w == XWINDOW (selected_window))
12484 if (!row->continued_p
12485 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12486 && row->x == 0)
12488 this_line_buffer = XBUFFER (w->buffer);
12490 CHARPOS (this_line_start_pos)
12491 = MATRIX_ROW_START_CHARPOS (row) + delta;
12492 BYTEPOS (this_line_start_pos)
12493 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12495 CHARPOS (this_line_end_pos)
12496 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12497 BYTEPOS (this_line_end_pos)
12498 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12500 this_line_y = w->cursor.y;
12501 this_line_pixel_height = row->height;
12502 this_line_vpos = w->cursor.vpos;
12503 this_line_start_x = row->x;
12505 else
12506 CHARPOS (this_line_start_pos) = 0;
12509 return 1;
12513 /* Run window scroll functions, if any, for WINDOW with new window
12514 start STARTP. Sets the window start of WINDOW to that position.
12516 We assume that the window's buffer is really current. */
12518 static INLINE struct text_pos
12519 run_window_scroll_functions (window, startp)
12520 Lisp_Object window;
12521 struct text_pos startp;
12523 struct window *w = XWINDOW (window);
12524 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12526 if (current_buffer != XBUFFER (w->buffer))
12527 abort ();
12529 if (!NILP (Vwindow_scroll_functions))
12531 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12532 make_number (CHARPOS (startp)));
12533 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12534 /* In case the hook functions switch buffers. */
12535 if (current_buffer != XBUFFER (w->buffer))
12536 set_buffer_internal_1 (XBUFFER (w->buffer));
12539 return startp;
12543 /* Make sure the line containing the cursor is fully visible.
12544 A value of 1 means there is nothing to be done.
12545 (Either the line is fully visible, or it cannot be made so,
12546 or we cannot tell.)
12548 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12549 is higher than window.
12551 A value of 0 means the caller should do scrolling
12552 as if point had gone off the screen. */
12554 static int
12555 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12556 struct window *w;
12557 int force_p;
12558 int current_matrix_p;
12560 struct glyph_matrix *matrix;
12561 struct glyph_row *row;
12562 int window_height;
12564 if (!make_cursor_line_fully_visible_p)
12565 return 1;
12567 /* It's not always possible to find the cursor, e.g, when a window
12568 is full of overlay strings. Don't do anything in that case. */
12569 if (w->cursor.vpos < 0)
12570 return 1;
12572 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12573 row = MATRIX_ROW (matrix, w->cursor.vpos);
12575 /* If the cursor row is not partially visible, there's nothing to do. */
12576 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12577 return 1;
12579 /* If the row the cursor is in is taller than the window's height,
12580 it's not clear what to do, so do nothing. */
12581 window_height = window_box_height (w);
12582 if (row->height >= window_height)
12584 if (!force_p || MINI_WINDOW_P (w)
12585 || w->vscroll || w->cursor.vpos == 0)
12586 return 1;
12588 return 0;
12592 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12593 non-zero means only WINDOW is redisplayed in redisplay_internal.
12594 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12595 in redisplay_window to bring a partially visible line into view in
12596 the case that only the cursor has moved.
12598 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12599 last screen line's vertical height extends past the end of the screen.
12601 Value is
12603 1 if scrolling succeeded
12605 0 if scrolling didn't find point.
12607 -1 if new fonts have been loaded so that we must interrupt
12608 redisplay, adjust glyph matrices, and try again. */
12610 enum
12612 SCROLLING_SUCCESS,
12613 SCROLLING_FAILED,
12614 SCROLLING_NEED_LARGER_MATRICES
12617 static int
12618 try_scrolling (window, just_this_one_p, scroll_conservatively,
12619 scroll_step, temp_scroll_step, last_line_misfit)
12620 Lisp_Object window;
12621 int just_this_one_p;
12622 EMACS_INT scroll_conservatively, scroll_step;
12623 int temp_scroll_step;
12624 int last_line_misfit;
12626 struct window *w = XWINDOW (window);
12627 struct frame *f = XFRAME (w->frame);
12628 struct text_pos pos, startp;
12629 struct it it;
12630 int this_scroll_margin, scroll_max, rc, height;
12631 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12632 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12633 Lisp_Object aggressive;
12634 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12636 #if GLYPH_DEBUG
12637 debug_method_add (w, "try_scrolling");
12638 #endif
12640 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12642 /* Compute scroll margin height in pixels. We scroll when point is
12643 within this distance from the top or bottom of the window. */
12644 if (scroll_margin > 0)
12645 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12646 * FRAME_LINE_HEIGHT (f);
12647 else
12648 this_scroll_margin = 0;
12650 /* Force scroll_conservatively to have a reasonable value, to avoid
12651 overflow while computing how much to scroll. Note that the user
12652 can supply scroll-conservatively equal to `most-positive-fixnum',
12653 which can be larger than INT_MAX. */
12654 if (scroll_conservatively > scroll_limit)
12656 scroll_conservatively = scroll_limit;
12657 scroll_max = INT_MAX;
12659 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12660 /* Compute how much we should try to scroll maximally to bring
12661 point into view. */
12662 scroll_max = (max (scroll_step,
12663 max (scroll_conservatively, temp_scroll_step))
12664 * FRAME_LINE_HEIGHT (f));
12665 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12666 || NUMBERP (current_buffer->scroll_up_aggressively))
12667 /* We're trying to scroll because of aggressive scrolling but no
12668 scroll_step is set. Choose an arbitrary one. */
12669 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12670 else
12671 scroll_max = 0;
12673 too_near_end:
12675 /* Decide whether to scroll down. */
12676 if (PT > CHARPOS (startp))
12678 int scroll_margin_y;
12680 /* Compute the pixel ypos of the scroll margin, then move it to
12681 either that ypos or PT, whichever comes first. */
12682 start_display (&it, w, startp);
12683 scroll_margin_y = it.last_visible_y - this_scroll_margin
12684 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12685 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12686 (MOVE_TO_POS | MOVE_TO_Y));
12688 if (PT > CHARPOS (it.current.pos))
12690 int y0 = line_bottom_y (&it);
12692 /* Compute the distance from the scroll margin to PT
12693 (including the height of the cursor line). Moving the
12694 iterator unconditionally to PT can be slow if PT is far
12695 away, so stop 10 lines past the window bottom (is there a
12696 way to do the right thing quickly?). */
12697 move_it_to (&it, PT, -1,
12698 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12699 -1, MOVE_TO_POS | MOVE_TO_Y);
12700 dy = line_bottom_y (&it) - y0;
12702 if (dy > scroll_max)
12703 return SCROLLING_FAILED;
12705 scroll_down_p = 1;
12709 if (scroll_down_p)
12711 /* Point is in or below the bottom scroll margin, so move the
12712 window start down. If scrolling conservatively, move it just
12713 enough down to make point visible. If scroll_step is set,
12714 move it down by scroll_step. */
12715 if (scroll_conservatively)
12716 amount_to_scroll
12717 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12718 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12719 else if (scroll_step || temp_scroll_step)
12720 amount_to_scroll = scroll_max;
12721 else
12723 aggressive = current_buffer->scroll_up_aggressively;
12724 height = WINDOW_BOX_TEXT_HEIGHT (w);
12725 if (NUMBERP (aggressive))
12727 double float_amount = XFLOATINT (aggressive) * height;
12728 amount_to_scroll = float_amount;
12729 if (amount_to_scroll == 0 && float_amount > 0)
12730 amount_to_scroll = 1;
12734 if (amount_to_scroll <= 0)
12735 return SCROLLING_FAILED;
12737 start_display (&it, w, startp);
12738 move_it_vertically (&it, amount_to_scroll);
12740 /* If STARTP is unchanged, move it down another screen line. */
12741 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12742 move_it_by_lines (&it, 1, 1);
12743 startp = it.current.pos;
12745 else
12747 struct text_pos scroll_margin_pos = startp;
12749 /* See if point is inside the scroll margin at the top of the
12750 window. */
12751 if (this_scroll_margin)
12753 start_display (&it, w, startp);
12754 move_it_vertically (&it, this_scroll_margin);
12755 scroll_margin_pos = it.current.pos;
12758 if (PT < CHARPOS (scroll_margin_pos))
12760 /* Point is in the scroll margin at the top of the window or
12761 above what is displayed in the window. */
12762 int y0;
12764 /* Compute the vertical distance from PT to the scroll
12765 margin position. Give up if distance is greater than
12766 scroll_max. */
12767 SET_TEXT_POS (pos, PT, PT_BYTE);
12768 start_display (&it, w, pos);
12769 y0 = it.current_y;
12770 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12771 it.last_visible_y, -1,
12772 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12773 dy = it.current_y - y0;
12774 if (dy > scroll_max)
12775 return SCROLLING_FAILED;
12777 /* Compute new window start. */
12778 start_display (&it, w, startp);
12780 if (scroll_conservatively)
12781 amount_to_scroll
12782 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12783 else if (scroll_step || temp_scroll_step)
12784 amount_to_scroll = scroll_max;
12785 else
12787 aggressive = current_buffer->scroll_down_aggressively;
12788 height = WINDOW_BOX_TEXT_HEIGHT (w);
12789 if (NUMBERP (aggressive))
12791 double float_amount = XFLOATINT (aggressive) * height;
12792 amount_to_scroll = float_amount;
12793 if (amount_to_scroll == 0 && float_amount > 0)
12794 amount_to_scroll = 1;
12798 if (amount_to_scroll <= 0)
12799 return SCROLLING_FAILED;
12801 move_it_vertically_backward (&it, amount_to_scroll);
12802 startp = it.current.pos;
12806 /* Run window scroll functions. */
12807 startp = run_window_scroll_functions (window, startp);
12809 /* Display the window. Give up if new fonts are loaded, or if point
12810 doesn't appear. */
12811 if (!try_window (window, startp, 0))
12812 rc = SCROLLING_NEED_LARGER_MATRICES;
12813 else if (w->cursor.vpos < 0)
12815 clear_glyph_matrix (w->desired_matrix);
12816 rc = SCROLLING_FAILED;
12818 else
12820 /* Maybe forget recorded base line for line number display. */
12821 if (!just_this_one_p
12822 || current_buffer->clip_changed
12823 || BEG_UNCHANGED < CHARPOS (startp))
12824 w->base_line_number = Qnil;
12826 /* If cursor ends up on a partially visible line,
12827 treat that as being off the bottom of the screen. */
12828 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12830 clear_glyph_matrix (w->desired_matrix);
12831 ++extra_scroll_margin_lines;
12832 goto too_near_end;
12834 rc = SCROLLING_SUCCESS;
12837 return rc;
12841 /* Compute a suitable window start for window W if display of W starts
12842 on a continuation line. Value is non-zero if a new window start
12843 was computed.
12845 The new window start will be computed, based on W's width, starting
12846 from the start of the continued line. It is the start of the
12847 screen line with the minimum distance from the old start W->start. */
12849 static int
12850 compute_window_start_on_continuation_line (w)
12851 struct window *w;
12853 struct text_pos pos, start_pos;
12854 int window_start_changed_p = 0;
12856 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12858 /* If window start is on a continuation line... Window start may be
12859 < BEGV in case there's invisible text at the start of the
12860 buffer (M-x rmail, for example). */
12861 if (CHARPOS (start_pos) > BEGV
12862 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12864 struct it it;
12865 struct glyph_row *row;
12867 /* Handle the case that the window start is out of range. */
12868 if (CHARPOS (start_pos) < BEGV)
12869 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12870 else if (CHARPOS (start_pos) > ZV)
12871 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12873 /* Find the start of the continued line. This should be fast
12874 because scan_buffer is fast (newline cache). */
12875 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12876 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12877 row, DEFAULT_FACE_ID);
12878 reseat_at_previous_visible_line_start (&it);
12880 /* If the line start is "too far" away from the window start,
12881 say it takes too much time to compute a new window start. */
12882 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12883 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12885 int min_distance, distance;
12887 /* Move forward by display lines to find the new window
12888 start. If window width was enlarged, the new start can
12889 be expected to be > the old start. If window width was
12890 decreased, the new window start will be < the old start.
12891 So, we're looking for the display line start with the
12892 minimum distance from the old window start. */
12893 pos = it.current.pos;
12894 min_distance = INFINITY;
12895 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12896 distance < min_distance)
12898 min_distance = distance;
12899 pos = it.current.pos;
12900 move_it_by_lines (&it, 1, 0);
12903 /* Set the window start there. */
12904 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12905 window_start_changed_p = 1;
12909 return window_start_changed_p;
12913 /* Try cursor movement in case text has not changed in window WINDOW,
12914 with window start STARTP. Value is
12916 CURSOR_MOVEMENT_SUCCESS if successful
12918 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12920 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12921 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12922 we want to scroll as if scroll-step were set to 1. See the code.
12924 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12925 which case we have to abort this redisplay, and adjust matrices
12926 first. */
12928 enum
12930 CURSOR_MOVEMENT_SUCCESS,
12931 CURSOR_MOVEMENT_CANNOT_BE_USED,
12932 CURSOR_MOVEMENT_MUST_SCROLL,
12933 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12936 static int
12937 try_cursor_movement (window, startp, scroll_step)
12938 Lisp_Object window;
12939 struct text_pos startp;
12940 int *scroll_step;
12942 struct window *w = XWINDOW (window);
12943 struct frame *f = XFRAME (w->frame);
12944 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12946 #if GLYPH_DEBUG
12947 if (inhibit_try_cursor_movement)
12948 return rc;
12949 #endif
12951 /* Handle case where text has not changed, only point, and it has
12952 not moved off the frame. */
12953 if (/* Point may be in this window. */
12954 PT >= CHARPOS (startp)
12955 /* Selective display hasn't changed. */
12956 && !current_buffer->clip_changed
12957 /* Function force-mode-line-update is used to force a thorough
12958 redisplay. It sets either windows_or_buffers_changed or
12959 update_mode_lines. So don't take a shortcut here for these
12960 cases. */
12961 && !update_mode_lines
12962 && !windows_or_buffers_changed
12963 && !cursor_type_changed
12964 /* Can't use this case if highlighting a region. When a
12965 region exists, cursor movement has to do more than just
12966 set the cursor. */
12967 && !(!NILP (Vtransient_mark_mode)
12968 && !NILP (current_buffer->mark_active))
12969 && NILP (w->region_showing)
12970 && NILP (Vshow_trailing_whitespace)
12971 /* Right after splitting windows, last_point may be nil. */
12972 && INTEGERP (w->last_point)
12973 /* This code is not used for mini-buffer for the sake of the case
12974 of redisplaying to replace an echo area message; since in
12975 that case the mini-buffer contents per se are usually
12976 unchanged. This code is of no real use in the mini-buffer
12977 since the handling of this_line_start_pos, etc., in redisplay
12978 handles the same cases. */
12979 && !EQ (window, minibuf_window)
12980 /* When splitting windows or for new windows, it happens that
12981 redisplay is called with a nil window_end_vpos or one being
12982 larger than the window. This should really be fixed in
12983 window.c. I don't have this on my list, now, so we do
12984 approximately the same as the old redisplay code. --gerd. */
12985 && INTEGERP (w->window_end_vpos)
12986 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12987 && (FRAME_WINDOW_P (f)
12988 || !overlay_arrow_in_current_buffer_p ()))
12990 int this_scroll_margin, top_scroll_margin;
12991 struct glyph_row *row = NULL;
12993 #if GLYPH_DEBUG
12994 debug_method_add (w, "cursor movement");
12995 #endif
12997 /* Scroll if point within this distance from the top or bottom
12998 of the window. This is a pixel value. */
12999 if (scroll_margin > 0)
13001 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13002 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13004 else
13005 this_scroll_margin = 0;
13007 top_scroll_margin = this_scroll_margin;
13008 if (WINDOW_WANTS_HEADER_LINE_P (w))
13009 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
13011 /* Start with the row the cursor was displayed during the last
13012 not paused redisplay. Give up if that row is not valid. */
13013 if (w->last_cursor.vpos < 0
13014 || w->last_cursor.vpos >= w->current_matrix->nrows)
13015 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13016 else
13018 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
13019 if (row->mode_line_p)
13020 ++row;
13021 if (!row->enabled_p)
13022 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13025 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
13027 int scroll_p = 0;
13028 int last_y = window_text_bottom_y (w) - this_scroll_margin;
13030 if (PT > XFASTINT (w->last_point))
13032 /* Point has moved forward. */
13033 while (MATRIX_ROW_END_CHARPOS (row) < PT
13034 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13036 xassert (row->enabled_p);
13037 ++row;
13040 /* The end position of a row equals the start position
13041 of the next row. If PT is there, we would rather
13042 display it in the next line. */
13043 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13044 && MATRIX_ROW_END_CHARPOS (row) == PT
13045 && !cursor_row_p (w, row))
13046 ++row;
13048 /* If within the scroll margin, scroll. Note that
13049 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13050 the next line would be drawn, and that
13051 this_scroll_margin can be zero. */
13052 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13053 || PT > MATRIX_ROW_END_CHARPOS (row)
13054 /* Line is completely visible last line in window
13055 and PT is to be set in the next line. */
13056 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13057 && PT == MATRIX_ROW_END_CHARPOS (row)
13058 && !row->ends_at_zv_p
13059 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13060 scroll_p = 1;
13062 else if (PT < XFASTINT (w->last_point))
13064 /* Cursor has to be moved backward. Note that PT >=
13065 CHARPOS (startp) because of the outer if-statement. */
13066 while (!row->mode_line_p
13067 && (MATRIX_ROW_START_CHARPOS (row) > PT
13068 || (MATRIX_ROW_START_CHARPOS (row) == PT
13069 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13070 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13071 row > w->current_matrix->rows
13072 && (row-1)->ends_in_newline_from_string_p))))
13073 && (row->y > top_scroll_margin
13074 || CHARPOS (startp) == BEGV))
13076 xassert (row->enabled_p);
13077 --row;
13080 /* Consider the following case: Window starts at BEGV,
13081 there is invisible, intangible text at BEGV, so that
13082 display starts at some point START > BEGV. It can
13083 happen that we are called with PT somewhere between
13084 BEGV and START. Try to handle that case. */
13085 if (row < w->current_matrix->rows
13086 || row->mode_line_p)
13088 row = w->current_matrix->rows;
13089 if (row->mode_line_p)
13090 ++row;
13093 /* Due to newlines in overlay strings, we may have to
13094 skip forward over overlay strings. */
13095 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13096 && MATRIX_ROW_END_CHARPOS (row) == PT
13097 && !cursor_row_p (w, row))
13098 ++row;
13100 /* If within the scroll margin, scroll. */
13101 if (row->y < top_scroll_margin
13102 && CHARPOS (startp) != BEGV)
13103 scroll_p = 1;
13105 else
13107 /* Cursor did not move. So don't scroll even if cursor line
13108 is partially visible, as it was so before. */
13109 rc = CURSOR_MOVEMENT_SUCCESS;
13112 if (PT < MATRIX_ROW_START_CHARPOS (row)
13113 || PT > MATRIX_ROW_END_CHARPOS (row))
13115 /* if PT is not in the glyph row, give up. */
13116 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13118 else if (rc != CURSOR_MOVEMENT_SUCCESS
13119 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13120 && make_cursor_line_fully_visible_p)
13122 if (PT == MATRIX_ROW_END_CHARPOS (row)
13123 && !row->ends_at_zv_p
13124 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13125 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13126 else if (row->height > window_box_height (w))
13128 /* If we end up in a partially visible line, let's
13129 make it fully visible, except when it's taller
13130 than the window, in which case we can't do much
13131 about it. */
13132 *scroll_step = 1;
13133 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13135 else
13137 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13138 if (!cursor_row_fully_visible_p (w, 0, 1))
13139 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13140 else
13141 rc = CURSOR_MOVEMENT_SUCCESS;
13144 else if (scroll_p)
13145 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13146 else
13150 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13152 rc = CURSOR_MOVEMENT_SUCCESS;
13153 break;
13155 ++row;
13157 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13158 && MATRIX_ROW_START_CHARPOS (row) == PT
13159 && cursor_row_p (w, row));
13164 return rc;
13167 void
13168 set_vertical_scroll_bar (w)
13169 struct window *w;
13171 int start, end, whole;
13173 /* Calculate the start and end positions for the current window.
13174 At some point, it would be nice to choose between scrollbars
13175 which reflect the whole buffer size, with special markers
13176 indicating narrowing, and scrollbars which reflect only the
13177 visible region.
13179 Note that mini-buffers sometimes aren't displaying any text. */
13180 if (!MINI_WINDOW_P (w)
13181 || (w == XWINDOW (minibuf_window)
13182 && NILP (echo_area_buffer[0])))
13184 struct buffer *buf = XBUFFER (w->buffer);
13185 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13186 start = marker_position (w->start) - BUF_BEGV (buf);
13187 /* I don't think this is guaranteed to be right. For the
13188 moment, we'll pretend it is. */
13189 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13191 if (end < start)
13192 end = start;
13193 if (whole < (end - start))
13194 whole = end - start;
13196 else
13197 start = end = whole = 0;
13199 /* Indicate what this scroll bar ought to be displaying now. */
13200 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13201 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13202 (w, end - start, whole, start);
13206 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13207 selected_window is redisplayed.
13209 We can return without actually redisplaying the window if
13210 fonts_changed_p is nonzero. In that case, redisplay_internal will
13211 retry. */
13213 static void
13214 redisplay_window (window, just_this_one_p)
13215 Lisp_Object window;
13216 int just_this_one_p;
13218 struct window *w = XWINDOW (window);
13219 struct frame *f = XFRAME (w->frame);
13220 struct buffer *buffer = XBUFFER (w->buffer);
13221 struct buffer *old = current_buffer;
13222 struct text_pos lpoint, opoint, startp;
13223 int update_mode_line;
13224 int tem;
13225 struct it it;
13226 /* Record it now because it's overwritten. */
13227 int current_matrix_up_to_date_p = 0;
13228 int used_current_matrix_p = 0;
13229 /* This is less strict than current_matrix_up_to_date_p.
13230 It indictes that the buffer contents and narrowing are unchanged. */
13231 int buffer_unchanged_p = 0;
13232 int temp_scroll_step = 0;
13233 int count = SPECPDL_INDEX ();
13234 int rc;
13235 int centering_position = -1;
13236 int last_line_misfit = 0;
13237 int beg_unchanged, end_unchanged;
13239 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13240 opoint = lpoint;
13242 /* W must be a leaf window here. */
13243 xassert (!NILP (w->buffer));
13244 #if GLYPH_DEBUG
13245 *w->desired_matrix->method = 0;
13246 #endif
13248 restart:
13249 reconsider_clip_changes (w, buffer);
13251 /* Has the mode line to be updated? */
13252 update_mode_line = (!NILP (w->update_mode_line)
13253 || update_mode_lines
13254 || buffer->clip_changed
13255 || buffer->prevent_redisplay_optimizations_p);
13257 if (MINI_WINDOW_P (w))
13259 if (w == XWINDOW (echo_area_window)
13260 && !NILP (echo_area_buffer[0]))
13262 if (update_mode_line)
13263 /* We may have to update a tty frame's menu bar or a
13264 tool-bar. Example `M-x C-h C-h C-g'. */
13265 goto finish_menu_bars;
13266 else
13267 /* We've already displayed the echo area glyphs in this window. */
13268 goto finish_scroll_bars;
13270 else if ((w != XWINDOW (minibuf_window)
13271 || minibuf_level == 0)
13272 /* When buffer is nonempty, redisplay window normally. */
13273 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13274 /* Quail displays non-mini buffers in minibuffer window.
13275 In that case, redisplay the window normally. */
13276 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13278 /* W is a mini-buffer window, but it's not active, so clear
13279 it. */
13280 int yb = window_text_bottom_y (w);
13281 struct glyph_row *row;
13282 int y;
13284 for (y = 0, row = w->desired_matrix->rows;
13285 y < yb;
13286 y += row->height, ++row)
13287 blank_row (w, row, y);
13288 goto finish_scroll_bars;
13291 clear_glyph_matrix (w->desired_matrix);
13294 /* Otherwise set up data on this window; select its buffer and point
13295 value. */
13296 /* Really select the buffer, for the sake of buffer-local
13297 variables. */
13298 set_buffer_internal_1 (XBUFFER (w->buffer));
13300 current_matrix_up_to_date_p
13301 = (!NILP (w->window_end_valid)
13302 && !current_buffer->clip_changed
13303 && !current_buffer->prevent_redisplay_optimizations_p
13304 && XFASTINT (w->last_modified) >= MODIFF
13305 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13307 /* Run the window-bottom-change-functions
13308 if it is possible that the text on the screen has changed
13309 (either due to modification of the text, or any other reason). */
13310 if (!current_matrix_up_to_date_p
13311 && !NILP (Vwindow_text_change_functions))
13313 safe_run_hooks (Qwindow_text_change_functions);
13314 goto restart;
13317 beg_unchanged = BEG_UNCHANGED;
13318 end_unchanged = END_UNCHANGED;
13320 SET_TEXT_POS (opoint, PT, PT_BYTE);
13322 specbind (Qinhibit_point_motion_hooks, Qt);
13324 buffer_unchanged_p
13325 = (!NILP (w->window_end_valid)
13326 && !current_buffer->clip_changed
13327 && XFASTINT (w->last_modified) >= MODIFF
13328 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13330 /* When windows_or_buffers_changed is non-zero, we can't rely on
13331 the window end being valid, so set it to nil there. */
13332 if (windows_or_buffers_changed)
13334 /* If window starts on a continuation line, maybe adjust the
13335 window start in case the window's width changed. */
13336 if (XMARKER (w->start)->buffer == current_buffer)
13337 compute_window_start_on_continuation_line (w);
13339 w->window_end_valid = Qnil;
13342 /* Some sanity checks. */
13343 CHECK_WINDOW_END (w);
13344 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13345 abort ();
13346 if (BYTEPOS (opoint) < CHARPOS (opoint))
13347 abort ();
13349 /* If %c is in mode line, update it if needed. */
13350 if (!NILP (w->column_number_displayed)
13351 /* This alternative quickly identifies a common case
13352 where no change is needed. */
13353 && !(PT == XFASTINT (w->last_point)
13354 && XFASTINT (w->last_modified) >= MODIFF
13355 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13356 && (XFASTINT (w->column_number_displayed)
13357 != (int) current_column ())) /* iftc */
13358 update_mode_line = 1;
13360 /* Count number of windows showing the selected buffer. An indirect
13361 buffer counts as its base buffer. */
13362 if (!just_this_one_p)
13364 struct buffer *current_base, *window_base;
13365 current_base = current_buffer;
13366 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13367 if (current_base->base_buffer)
13368 current_base = current_base->base_buffer;
13369 if (window_base->base_buffer)
13370 window_base = window_base->base_buffer;
13371 if (current_base == window_base)
13372 buffer_shared++;
13375 /* Point refers normally to the selected window. For any other
13376 window, set up appropriate value. */
13377 if (!EQ (window, selected_window))
13379 int new_pt = XMARKER (w->pointm)->charpos;
13380 int new_pt_byte = marker_byte_position (w->pointm);
13381 if (new_pt < BEGV)
13383 new_pt = BEGV;
13384 new_pt_byte = BEGV_BYTE;
13385 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13387 else if (new_pt > (ZV - 1))
13389 new_pt = ZV;
13390 new_pt_byte = ZV_BYTE;
13391 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13394 /* We don't use SET_PT so that the point-motion hooks don't run. */
13395 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13398 /* If any of the character widths specified in the display table
13399 have changed, invalidate the width run cache. It's true that
13400 this may be a bit late to catch such changes, but the rest of
13401 redisplay goes (non-fatally) haywire when the display table is
13402 changed, so why should we worry about doing any better? */
13403 if (current_buffer->width_run_cache)
13405 struct Lisp_Char_Table *disptab = buffer_display_table ();
13407 if (! disptab_matches_widthtab (disptab,
13408 XVECTOR (current_buffer->width_table)))
13410 invalidate_region_cache (current_buffer,
13411 current_buffer->width_run_cache,
13412 BEG, Z);
13413 recompute_width_table (current_buffer, disptab);
13417 /* If window-start is screwed up, choose a new one. */
13418 if (XMARKER (w->start)->buffer != current_buffer)
13419 goto recenter;
13421 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13423 /* If someone specified a new starting point but did not insist,
13424 check whether it can be used. */
13425 if (!NILP (w->optional_new_start)
13426 && CHARPOS (startp) >= BEGV
13427 && CHARPOS (startp) <= ZV)
13429 w->optional_new_start = Qnil;
13430 start_display (&it, w, startp);
13431 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13432 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13433 if (IT_CHARPOS (it) == PT)
13434 w->force_start = Qt;
13435 /* IT may overshoot PT if text at PT is invisible. */
13436 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13437 w->force_start = Qt;
13440 force_start:
13442 /* Handle case where place to start displaying has been specified,
13443 unless the specified location is outside the accessible range. */
13444 if (!NILP (w->force_start)
13445 || w->frozen_window_start_p)
13447 /* We set this later on if we have to adjust point. */
13448 int new_vpos = -1;
13450 w->force_start = Qnil;
13451 w->vscroll = 0;
13452 w->window_end_valid = Qnil;
13454 /* Forget any recorded base line for line number display. */
13455 if (!buffer_unchanged_p)
13456 w->base_line_number = Qnil;
13458 /* Redisplay the mode line. Select the buffer properly for that.
13459 Also, run the hook window-scroll-functions
13460 because we have scrolled. */
13461 /* Note, we do this after clearing force_start because
13462 if there's an error, it is better to forget about force_start
13463 than to get into an infinite loop calling the hook functions
13464 and having them get more errors. */
13465 if (!update_mode_line
13466 || ! NILP (Vwindow_scroll_functions))
13468 update_mode_line = 1;
13469 w->update_mode_line = Qt;
13470 startp = run_window_scroll_functions (window, startp);
13473 w->last_modified = make_number (0);
13474 w->last_overlay_modified = make_number (0);
13475 if (CHARPOS (startp) < BEGV)
13476 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13477 else if (CHARPOS (startp) > ZV)
13478 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13480 /* Redisplay, then check if cursor has been set during the
13481 redisplay. Give up if new fonts were loaded. */
13482 /* We used to issue a CHECK_MARGINS argument to try_window here,
13483 but this causes scrolling to fail when point begins inside
13484 the scroll margin (bug#148) -- cyd */
13485 if (!try_window (window, startp, 0))
13487 w->force_start = Qt;
13488 clear_glyph_matrix (w->desired_matrix);
13489 goto need_larger_matrices;
13492 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13494 /* If point does not appear, try to move point so it does
13495 appear. The desired matrix has been built above, so we
13496 can use it here. */
13497 new_vpos = window_box_height (w) / 2;
13500 if (!cursor_row_fully_visible_p (w, 0, 0))
13502 /* Point does appear, but on a line partly visible at end of window.
13503 Move it back to a fully-visible line. */
13504 new_vpos = window_box_height (w);
13507 /* If we need to move point for either of the above reasons,
13508 now actually do it. */
13509 if (new_vpos >= 0)
13511 struct glyph_row *row;
13513 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13514 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13515 ++row;
13517 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13518 MATRIX_ROW_START_BYTEPOS (row));
13520 if (w != XWINDOW (selected_window))
13521 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13522 else if (current_buffer == old)
13523 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13525 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13527 /* If we are highlighting the region, then we just changed
13528 the region, so redisplay to show it. */
13529 if (!NILP (Vtransient_mark_mode)
13530 && !NILP (current_buffer->mark_active))
13532 clear_glyph_matrix (w->desired_matrix);
13533 if (!try_window (window, startp, 0))
13534 goto need_larger_matrices;
13538 #if GLYPH_DEBUG
13539 debug_method_add (w, "forced window start");
13540 #endif
13541 goto done;
13544 /* Handle case where text has not changed, only point, and it has
13545 not moved off the frame, and we are not retrying after hscroll.
13546 (current_matrix_up_to_date_p is nonzero when retrying.) */
13547 if (current_matrix_up_to_date_p
13548 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13549 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13551 switch (rc)
13553 case CURSOR_MOVEMENT_SUCCESS:
13554 used_current_matrix_p = 1;
13555 goto done;
13557 case CURSOR_MOVEMENT_MUST_SCROLL:
13558 goto try_to_scroll;
13560 default:
13561 abort ();
13564 /* If current starting point was originally the beginning of a line
13565 but no longer is, find a new starting point. */
13566 else if (!NILP (w->start_at_line_beg)
13567 && !(CHARPOS (startp) <= BEGV
13568 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13570 #if GLYPH_DEBUG
13571 debug_method_add (w, "recenter 1");
13572 #endif
13573 goto recenter;
13576 /* Try scrolling with try_window_id. Value is > 0 if update has
13577 been done, it is -1 if we know that the same window start will
13578 not work. It is 0 if unsuccessful for some other reason. */
13579 else if ((tem = try_window_id (w)) != 0)
13581 #if GLYPH_DEBUG
13582 debug_method_add (w, "try_window_id %d", tem);
13583 #endif
13585 if (fonts_changed_p)
13586 goto need_larger_matrices;
13587 if (tem > 0)
13588 goto done;
13590 /* Otherwise try_window_id has returned -1 which means that we
13591 don't want the alternative below this comment to execute. */
13593 else if (CHARPOS (startp) >= BEGV
13594 && CHARPOS (startp) <= ZV
13595 && PT >= CHARPOS (startp)
13596 && (CHARPOS (startp) < ZV
13597 /* Avoid starting at end of buffer. */
13598 || CHARPOS (startp) == BEGV
13599 || (XFASTINT (w->last_modified) >= MODIFF
13600 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13603 /* If first window line is a continuation line, and window start
13604 is inside the modified region, but the first change is before
13605 current window start, we must select a new window start.
13607 However, if this is the result of a down-mouse event (e.g. by
13608 extending the mouse-drag-overlay), we don't want to select a
13609 new window start, since that would change the position under
13610 the mouse, resulting in an unwanted mouse-movement rather
13611 than a simple mouse-click. */
13612 if (NILP (w->start_at_line_beg)
13613 && NILP (do_mouse_tracking)
13614 && CHARPOS (startp) > BEGV
13615 && CHARPOS (startp) > BEG + beg_unchanged
13616 && CHARPOS (startp) <= Z - end_unchanged
13617 /* Even if w->start_at_line_beg is nil, a new window may
13618 start at a line_beg, since that's how set_buffer_window
13619 sets it. So, we need to check the return value of
13620 compute_window_start_on_continuation_line. (See also
13621 bug#197). */
13622 && XMARKER (w->start)->buffer == current_buffer
13623 && compute_window_start_on_continuation_line (w))
13625 w->force_start = Qt;
13626 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13627 goto force_start;
13630 #if GLYPH_DEBUG
13631 debug_method_add (w, "same window start");
13632 #endif
13634 /* Try to redisplay starting at same place as before.
13635 If point has not moved off frame, accept the results. */
13636 if (!current_matrix_up_to_date_p
13637 /* Don't use try_window_reusing_current_matrix in this case
13638 because a window scroll function can have changed the
13639 buffer. */
13640 || !NILP (Vwindow_scroll_functions)
13641 || MINI_WINDOW_P (w)
13642 || !(used_current_matrix_p
13643 = try_window_reusing_current_matrix (w)))
13645 IF_DEBUG (debug_method_add (w, "1"));
13646 if (try_window (window, startp, 1) < 0)
13647 /* -1 means we need to scroll.
13648 0 means we need new matrices, but fonts_changed_p
13649 is set in that case, so we will detect it below. */
13650 goto try_to_scroll;
13653 if (fonts_changed_p)
13654 goto need_larger_matrices;
13656 if (w->cursor.vpos >= 0)
13658 if (!just_this_one_p
13659 || current_buffer->clip_changed
13660 || BEG_UNCHANGED < CHARPOS (startp))
13661 /* Forget any recorded base line for line number display. */
13662 w->base_line_number = Qnil;
13664 if (!cursor_row_fully_visible_p (w, 1, 0))
13666 clear_glyph_matrix (w->desired_matrix);
13667 last_line_misfit = 1;
13669 /* Drop through and scroll. */
13670 else
13671 goto done;
13673 else
13674 clear_glyph_matrix (w->desired_matrix);
13677 try_to_scroll:
13679 w->last_modified = make_number (0);
13680 w->last_overlay_modified = make_number (0);
13682 /* Redisplay the mode line. Select the buffer properly for that. */
13683 if (!update_mode_line)
13685 update_mode_line = 1;
13686 w->update_mode_line = Qt;
13689 /* Try to scroll by specified few lines. */
13690 if ((scroll_conservatively
13691 || scroll_step
13692 || temp_scroll_step
13693 || NUMBERP (current_buffer->scroll_up_aggressively)
13694 || NUMBERP (current_buffer->scroll_down_aggressively))
13695 && !current_buffer->clip_changed
13696 && CHARPOS (startp) >= BEGV
13697 && CHARPOS (startp) <= ZV)
13699 /* The function returns -1 if new fonts were loaded, 1 if
13700 successful, 0 if not successful. */
13701 int rc = try_scrolling (window, just_this_one_p,
13702 scroll_conservatively,
13703 scroll_step,
13704 temp_scroll_step, last_line_misfit);
13705 switch (rc)
13707 case SCROLLING_SUCCESS:
13708 goto done;
13710 case SCROLLING_NEED_LARGER_MATRICES:
13711 goto need_larger_matrices;
13713 case SCROLLING_FAILED:
13714 break;
13716 default:
13717 abort ();
13721 /* Finally, just choose place to start which centers point */
13723 recenter:
13724 if (centering_position < 0)
13725 centering_position = window_box_height (w) / 2;
13727 #if GLYPH_DEBUG
13728 debug_method_add (w, "recenter");
13729 #endif
13731 /* w->vscroll = 0; */
13733 /* Forget any previously recorded base line for line number display. */
13734 if (!buffer_unchanged_p)
13735 w->base_line_number = Qnil;
13737 /* Move backward half the height of the window. */
13738 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13739 it.current_y = it.last_visible_y;
13740 move_it_vertically_backward (&it, centering_position);
13741 xassert (IT_CHARPOS (it) >= BEGV);
13743 /* The function move_it_vertically_backward may move over more
13744 than the specified y-distance. If it->w is small, e.g. a
13745 mini-buffer window, we may end up in front of the window's
13746 display area. Start displaying at the start of the line
13747 containing PT in this case. */
13748 if (it.current_y <= 0)
13750 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13751 move_it_vertically_backward (&it, 0);
13752 it.current_y = 0;
13755 it.current_x = it.hpos = 0;
13757 /* Set startp here explicitly in case that helps avoid an infinite loop
13758 in case the window-scroll-functions functions get errors. */
13759 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13761 /* Run scroll hooks. */
13762 startp = run_window_scroll_functions (window, it.current.pos);
13764 /* Redisplay the window. */
13765 if (!current_matrix_up_to_date_p
13766 || windows_or_buffers_changed
13767 || cursor_type_changed
13768 /* Don't use try_window_reusing_current_matrix in this case
13769 because it can have changed the buffer. */
13770 || !NILP (Vwindow_scroll_functions)
13771 || !just_this_one_p
13772 || MINI_WINDOW_P (w)
13773 || !(used_current_matrix_p
13774 = try_window_reusing_current_matrix (w)))
13775 try_window (window, startp, 0);
13777 /* If new fonts have been loaded (due to fontsets), give up. We
13778 have to start a new redisplay since we need to re-adjust glyph
13779 matrices. */
13780 if (fonts_changed_p)
13781 goto need_larger_matrices;
13783 /* If cursor did not appear assume that the middle of the window is
13784 in the first line of the window. Do it again with the next line.
13785 (Imagine a window of height 100, displaying two lines of height
13786 60. Moving back 50 from it->last_visible_y will end in the first
13787 line.) */
13788 if (w->cursor.vpos < 0)
13790 if (!NILP (w->window_end_valid)
13791 && PT >= Z - XFASTINT (w->window_end_pos))
13793 clear_glyph_matrix (w->desired_matrix);
13794 move_it_by_lines (&it, 1, 0);
13795 try_window (window, it.current.pos, 0);
13797 else if (PT < IT_CHARPOS (it))
13799 clear_glyph_matrix (w->desired_matrix);
13800 move_it_by_lines (&it, -1, 0);
13801 try_window (window, it.current.pos, 0);
13803 else
13805 /* Not much we can do about it. */
13809 /* Consider the following case: Window starts at BEGV, there is
13810 invisible, intangible text at BEGV, so that display starts at
13811 some point START > BEGV. It can happen that we are called with
13812 PT somewhere between BEGV and START. Try to handle that case. */
13813 if (w->cursor.vpos < 0)
13815 struct glyph_row *row = w->current_matrix->rows;
13816 if (row->mode_line_p)
13817 ++row;
13818 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13821 if (!cursor_row_fully_visible_p (w, 0, 0))
13823 /* If vscroll is enabled, disable it and try again. */
13824 if (w->vscroll)
13826 w->vscroll = 0;
13827 clear_glyph_matrix (w->desired_matrix);
13828 goto recenter;
13831 /* If centering point failed to make the whole line visible,
13832 put point at the top instead. That has to make the whole line
13833 visible, if it can be done. */
13834 if (centering_position == 0)
13835 goto done;
13837 clear_glyph_matrix (w->desired_matrix);
13838 centering_position = 0;
13839 goto recenter;
13842 done:
13844 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13845 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13846 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13847 ? Qt : Qnil);
13849 /* Display the mode line, if we must. */
13850 if ((update_mode_line
13851 /* If window not full width, must redo its mode line
13852 if (a) the window to its side is being redone and
13853 (b) we do a frame-based redisplay. This is a consequence
13854 of how inverted lines are drawn in frame-based redisplay. */
13855 || (!just_this_one_p
13856 && !FRAME_WINDOW_P (f)
13857 && !WINDOW_FULL_WIDTH_P (w))
13858 /* Line number to display. */
13859 || INTEGERP (w->base_line_pos)
13860 /* Column number is displayed and different from the one displayed. */
13861 || (!NILP (w->column_number_displayed)
13862 && (XFASTINT (w->column_number_displayed)
13863 != (int) current_column ()))) /* iftc */
13864 /* This means that the window has a mode line. */
13865 && (WINDOW_WANTS_MODELINE_P (w)
13866 || WINDOW_WANTS_HEADER_LINE_P (w)))
13868 display_mode_lines (w);
13870 /* If mode line height has changed, arrange for a thorough
13871 immediate redisplay using the correct mode line height. */
13872 if (WINDOW_WANTS_MODELINE_P (w)
13873 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13875 fonts_changed_p = 1;
13876 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13877 = DESIRED_MODE_LINE_HEIGHT (w);
13880 /* If header line height has changed, arrange for a thorough
13881 immediate redisplay using the correct header line height. */
13882 if (WINDOW_WANTS_HEADER_LINE_P (w)
13883 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13885 fonts_changed_p = 1;
13886 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13887 = DESIRED_HEADER_LINE_HEIGHT (w);
13890 if (fonts_changed_p)
13891 goto need_larger_matrices;
13894 if (!line_number_displayed
13895 && !BUFFERP (w->base_line_pos))
13897 w->base_line_pos = Qnil;
13898 w->base_line_number = Qnil;
13901 finish_menu_bars:
13903 /* When we reach a frame's selected window, redo the frame's menu bar. */
13904 if (update_mode_line
13905 && EQ (FRAME_SELECTED_WINDOW (f), window))
13907 int redisplay_menu_p = 0;
13908 int redisplay_tool_bar_p = 0;
13910 if (FRAME_WINDOW_P (f))
13912 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13913 || defined (HAVE_NS) || defined (USE_GTK)
13914 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13915 #else
13916 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13917 #endif
13919 else
13920 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13922 if (redisplay_menu_p)
13923 display_menu_bar (w);
13925 #ifdef HAVE_WINDOW_SYSTEM
13926 if (FRAME_WINDOW_P (f))
13928 #if defined (USE_GTK) || defined (HAVE_NS)
13929 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13930 #else
13931 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13932 && (FRAME_TOOL_BAR_LINES (f) > 0
13933 || !NILP (Vauto_resize_tool_bars));
13934 #endif
13936 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13938 extern int ignore_mouse_drag_p;
13939 ignore_mouse_drag_p = 1;
13942 #endif
13945 #ifdef HAVE_WINDOW_SYSTEM
13946 if (FRAME_WINDOW_P (f)
13947 && update_window_fringes (w, (just_this_one_p
13948 || (!used_current_matrix_p && !overlay_arrow_seen)
13949 || w->pseudo_window_p)))
13951 update_begin (f);
13952 BLOCK_INPUT;
13953 if (draw_window_fringes (w, 1))
13954 x_draw_vertical_border (w);
13955 UNBLOCK_INPUT;
13956 update_end (f);
13958 #endif /* HAVE_WINDOW_SYSTEM */
13960 /* We go to this label, with fonts_changed_p nonzero,
13961 if it is necessary to try again using larger glyph matrices.
13962 We have to redeem the scroll bar even in this case,
13963 because the loop in redisplay_internal expects that. */
13964 need_larger_matrices:
13966 finish_scroll_bars:
13968 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13970 /* Set the thumb's position and size. */
13971 set_vertical_scroll_bar (w);
13973 /* Note that we actually used the scroll bar attached to this
13974 window, so it shouldn't be deleted at the end of redisplay. */
13975 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13976 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13979 /* Restore current_buffer and value of point in it. */
13980 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13981 set_buffer_internal_1 (old);
13982 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13983 shorter. This can be caused by log truncation in *Messages*. */
13984 if (CHARPOS (lpoint) <= ZV)
13985 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13987 unbind_to (count, Qnil);
13991 /* Build the complete desired matrix of WINDOW with a window start
13992 buffer position POS.
13994 Value is 1 if successful. It is zero if fonts were loaded during
13995 redisplay which makes re-adjusting glyph matrices necessary, and -1
13996 if point would appear in the scroll margins.
13997 (We check that only if CHECK_MARGINS is nonzero. */
14000 try_window (window, pos, check_margins)
14001 Lisp_Object window;
14002 struct text_pos pos;
14003 int check_margins;
14005 struct window *w = XWINDOW (window);
14006 struct it it;
14007 struct glyph_row *last_text_row = NULL;
14008 struct frame *f = XFRAME (w->frame);
14010 /* Make POS the new window start. */
14011 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
14013 /* Mark cursor position as unknown. No overlay arrow seen. */
14014 w->cursor.vpos = -1;
14015 overlay_arrow_seen = 0;
14017 /* Initialize iterator and info to start at POS. */
14018 start_display (&it, w, pos);
14020 /* Display all lines of W. */
14021 while (it.current_y < it.last_visible_y)
14023 if (display_line (&it))
14024 last_text_row = it.glyph_row - 1;
14025 if (fonts_changed_p)
14026 return 0;
14029 /* Don't let the cursor end in the scroll margins. */
14030 if (check_margins
14031 && !MINI_WINDOW_P (w))
14033 int this_scroll_margin;
14035 if (scroll_margin > 0)
14037 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14038 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14040 else
14041 this_scroll_margin = 0;
14043 if ((w->cursor.y >= 0 /* not vscrolled */
14044 && w->cursor.y < this_scroll_margin
14045 && CHARPOS (pos) > BEGV
14046 && IT_CHARPOS (it) < ZV)
14047 /* rms: considering make_cursor_line_fully_visible_p here
14048 seems to give wrong results. We don't want to recenter
14049 when the last line is partly visible, we want to allow
14050 that case to be handled in the usual way. */
14051 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14053 w->cursor.vpos = -1;
14054 clear_glyph_matrix (w->desired_matrix);
14055 return -1;
14059 /* If bottom moved off end of frame, change mode line percentage. */
14060 if (XFASTINT (w->window_end_pos) <= 0
14061 && Z != IT_CHARPOS (it))
14062 w->update_mode_line = Qt;
14064 /* Set window_end_pos to the offset of the last character displayed
14065 on the window from the end of current_buffer. Set
14066 window_end_vpos to its row number. */
14067 if (last_text_row)
14069 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14070 w->window_end_bytepos
14071 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14072 w->window_end_pos
14073 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14074 w->window_end_vpos
14075 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14076 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14077 ->displays_text_p);
14079 else
14081 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14082 w->window_end_pos = make_number (Z - ZV);
14083 w->window_end_vpos = make_number (0);
14086 /* But that is not valid info until redisplay finishes. */
14087 w->window_end_valid = Qnil;
14088 return 1;
14093 /************************************************************************
14094 Window redisplay reusing current matrix when buffer has not changed
14095 ************************************************************************/
14097 /* Try redisplay of window W showing an unchanged buffer with a
14098 different window start than the last time it was displayed by
14099 reusing its current matrix. Value is non-zero if successful.
14100 W->start is the new window start. */
14102 static int
14103 try_window_reusing_current_matrix (w)
14104 struct window *w;
14106 struct frame *f = XFRAME (w->frame);
14107 struct glyph_row *row, *bottom_row;
14108 struct it it;
14109 struct run run;
14110 struct text_pos start, new_start;
14111 int nrows_scrolled, i;
14112 struct glyph_row *last_text_row;
14113 struct glyph_row *last_reused_text_row;
14114 struct glyph_row *start_row;
14115 int start_vpos, min_y, max_y;
14117 #if GLYPH_DEBUG
14118 if (inhibit_try_window_reusing)
14119 return 0;
14120 #endif
14122 if (/* This function doesn't handle terminal frames. */
14123 !FRAME_WINDOW_P (f)
14124 /* Don't try to reuse the display if windows have been split
14125 or such. */
14126 || windows_or_buffers_changed
14127 || cursor_type_changed)
14128 return 0;
14130 /* Can't do this if region may have changed. */
14131 if ((!NILP (Vtransient_mark_mode)
14132 && !NILP (current_buffer->mark_active))
14133 || !NILP (w->region_showing)
14134 || !NILP (Vshow_trailing_whitespace))
14135 return 0;
14137 /* If top-line visibility has changed, give up. */
14138 if (WINDOW_WANTS_HEADER_LINE_P (w)
14139 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14140 return 0;
14142 /* Give up if old or new display is scrolled vertically. We could
14143 make this function handle this, but right now it doesn't. */
14144 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14145 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14146 return 0;
14148 /* The variable new_start now holds the new window start. The old
14149 start `start' can be determined from the current matrix. */
14150 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14151 start = start_row->start.pos;
14152 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14154 /* Clear the desired matrix for the display below. */
14155 clear_glyph_matrix (w->desired_matrix);
14157 if (CHARPOS (new_start) <= CHARPOS (start))
14159 int first_row_y;
14161 /* Don't use this method if the display starts with an ellipsis
14162 displayed for invisible text. It's not easy to handle that case
14163 below, and it's certainly not worth the effort since this is
14164 not a frequent case. */
14165 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14166 return 0;
14168 IF_DEBUG (debug_method_add (w, "twu1"));
14170 /* Display up to a row that can be reused. The variable
14171 last_text_row is set to the last row displayed that displays
14172 text. Note that it.vpos == 0 if or if not there is a
14173 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14174 start_display (&it, w, new_start);
14175 first_row_y = it.current_y;
14176 w->cursor.vpos = -1;
14177 last_text_row = last_reused_text_row = NULL;
14179 while (it.current_y < it.last_visible_y
14180 && !fonts_changed_p)
14182 /* If we have reached into the characters in the START row,
14183 that means the line boundaries have changed. So we
14184 can't start copying with the row START. Maybe it will
14185 work to start copying with the following row. */
14186 while (IT_CHARPOS (it) > CHARPOS (start))
14188 /* Advance to the next row as the "start". */
14189 start_row++;
14190 start = start_row->start.pos;
14191 /* If there are no more rows to try, or just one, give up. */
14192 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14193 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14194 || CHARPOS (start) == ZV)
14196 clear_glyph_matrix (w->desired_matrix);
14197 return 0;
14200 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14202 /* If we have reached alignment,
14203 we can copy the rest of the rows. */
14204 if (IT_CHARPOS (it) == CHARPOS (start))
14205 break;
14207 if (display_line (&it))
14208 last_text_row = it.glyph_row - 1;
14211 /* A value of current_y < last_visible_y means that we stopped
14212 at the previous window start, which in turn means that we
14213 have at least one reusable row. */
14214 if (it.current_y < it.last_visible_y)
14216 /* IT.vpos always starts from 0; it counts text lines. */
14217 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14219 /* Find PT if not already found in the lines displayed. */
14220 if (w->cursor.vpos < 0)
14222 int dy = it.current_y - start_row->y;
14224 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14225 row = row_containing_pos (w, PT, row, NULL, dy);
14226 if (row)
14227 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14228 dy, nrows_scrolled);
14229 else
14231 clear_glyph_matrix (w->desired_matrix);
14232 return 0;
14236 /* Scroll the display. Do it before the current matrix is
14237 changed. The problem here is that update has not yet
14238 run, i.e. part of the current matrix is not up to date.
14239 scroll_run_hook will clear the cursor, and use the
14240 current matrix to get the height of the row the cursor is
14241 in. */
14242 run.current_y = start_row->y;
14243 run.desired_y = it.current_y;
14244 run.height = it.last_visible_y - it.current_y;
14246 if (run.height > 0 && run.current_y != run.desired_y)
14248 update_begin (f);
14249 FRAME_RIF (f)->update_window_begin_hook (w);
14250 FRAME_RIF (f)->clear_window_mouse_face (w);
14251 FRAME_RIF (f)->scroll_run_hook (w, &run);
14252 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14253 update_end (f);
14256 /* Shift current matrix down by nrows_scrolled lines. */
14257 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14258 rotate_matrix (w->current_matrix,
14259 start_vpos,
14260 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14261 nrows_scrolled);
14263 /* Disable lines that must be updated. */
14264 for (i = 0; i < nrows_scrolled; ++i)
14265 (start_row + i)->enabled_p = 0;
14267 /* Re-compute Y positions. */
14268 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14269 max_y = it.last_visible_y;
14270 for (row = start_row + nrows_scrolled;
14271 row < bottom_row;
14272 ++row)
14274 row->y = it.current_y;
14275 row->visible_height = row->height;
14277 if (row->y < min_y)
14278 row->visible_height -= min_y - row->y;
14279 if (row->y + row->height > max_y)
14280 row->visible_height -= row->y + row->height - max_y;
14281 row->redraw_fringe_bitmaps_p = 1;
14283 it.current_y += row->height;
14285 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14286 last_reused_text_row = row;
14287 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14288 break;
14291 /* Disable lines in the current matrix which are now
14292 below the window. */
14293 for (++row; row < bottom_row; ++row)
14294 row->enabled_p = row->mode_line_p = 0;
14297 /* Update window_end_pos etc.; last_reused_text_row is the last
14298 reused row from the current matrix containing text, if any.
14299 The value of last_text_row is the last displayed line
14300 containing text. */
14301 if (last_reused_text_row)
14303 w->window_end_bytepos
14304 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14305 w->window_end_pos
14306 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14307 w->window_end_vpos
14308 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14309 w->current_matrix));
14311 else if (last_text_row)
14313 w->window_end_bytepos
14314 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14315 w->window_end_pos
14316 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14317 w->window_end_vpos
14318 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14320 else
14322 /* This window must be completely empty. */
14323 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14324 w->window_end_pos = make_number (Z - ZV);
14325 w->window_end_vpos = make_number (0);
14327 w->window_end_valid = Qnil;
14329 /* Update hint: don't try scrolling again in update_window. */
14330 w->desired_matrix->no_scrolling_p = 1;
14332 #if GLYPH_DEBUG
14333 debug_method_add (w, "try_window_reusing_current_matrix 1");
14334 #endif
14335 return 1;
14337 else if (CHARPOS (new_start) > CHARPOS (start))
14339 struct glyph_row *pt_row, *row;
14340 struct glyph_row *first_reusable_row;
14341 struct glyph_row *first_row_to_display;
14342 int dy;
14343 int yb = window_text_bottom_y (w);
14345 /* Find the row starting at new_start, if there is one. Don't
14346 reuse a partially visible line at the end. */
14347 first_reusable_row = start_row;
14348 while (first_reusable_row->enabled_p
14349 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14350 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14351 < CHARPOS (new_start)))
14352 ++first_reusable_row;
14354 /* Give up if there is no row to reuse. */
14355 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14356 || !first_reusable_row->enabled_p
14357 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14358 != CHARPOS (new_start)))
14359 return 0;
14361 /* We can reuse fully visible rows beginning with
14362 first_reusable_row to the end of the window. Set
14363 first_row_to_display to the first row that cannot be reused.
14364 Set pt_row to the row containing point, if there is any. */
14365 pt_row = NULL;
14366 for (first_row_to_display = first_reusable_row;
14367 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14368 ++first_row_to_display)
14370 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14371 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14372 pt_row = first_row_to_display;
14375 /* Start displaying at the start of first_row_to_display. */
14376 xassert (first_row_to_display->y < yb);
14377 init_to_row_start (&it, w, first_row_to_display);
14379 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14380 - start_vpos);
14381 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14382 - nrows_scrolled);
14383 it.current_y = (first_row_to_display->y - first_reusable_row->y
14384 + WINDOW_HEADER_LINE_HEIGHT (w));
14386 /* Display lines beginning with first_row_to_display in the
14387 desired matrix. Set last_text_row to the last row displayed
14388 that displays text. */
14389 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14390 if (pt_row == NULL)
14391 w->cursor.vpos = -1;
14392 last_text_row = NULL;
14393 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14394 if (display_line (&it))
14395 last_text_row = it.glyph_row - 1;
14397 /* If point is in a reused row, adjust y and vpos of the cursor
14398 position. */
14399 if (pt_row)
14401 w->cursor.vpos -= nrows_scrolled;
14402 w->cursor.y -= first_reusable_row->y - start_row->y;
14405 /* Give up if point isn't in a row displayed or reused. (This
14406 also handles the case where w->cursor.vpos < nrows_scrolled
14407 after the calls to display_line, which can happen with scroll
14408 margins. See bug#1295.) */
14409 if (w->cursor.vpos < 0)
14411 clear_glyph_matrix (w->desired_matrix);
14412 return 0;
14415 /* Scroll the display. */
14416 run.current_y = first_reusable_row->y;
14417 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14418 run.height = it.last_visible_y - run.current_y;
14419 dy = run.current_y - run.desired_y;
14421 if (run.height)
14423 update_begin (f);
14424 FRAME_RIF (f)->update_window_begin_hook (w);
14425 FRAME_RIF (f)->clear_window_mouse_face (w);
14426 FRAME_RIF (f)->scroll_run_hook (w, &run);
14427 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14428 update_end (f);
14431 /* Adjust Y positions of reused rows. */
14432 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14433 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14434 max_y = it.last_visible_y;
14435 for (row = first_reusable_row; row < first_row_to_display; ++row)
14437 row->y -= dy;
14438 row->visible_height = row->height;
14439 if (row->y < min_y)
14440 row->visible_height -= min_y - row->y;
14441 if (row->y + row->height > max_y)
14442 row->visible_height -= row->y + row->height - max_y;
14443 row->redraw_fringe_bitmaps_p = 1;
14446 /* Scroll the current matrix. */
14447 xassert (nrows_scrolled > 0);
14448 rotate_matrix (w->current_matrix,
14449 start_vpos,
14450 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14451 -nrows_scrolled);
14453 /* Disable rows not reused. */
14454 for (row -= nrows_scrolled; row < bottom_row; ++row)
14455 row->enabled_p = 0;
14457 /* Point may have moved to a different line, so we cannot assume that
14458 the previous cursor position is valid; locate the correct row. */
14459 if (pt_row)
14461 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14462 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14463 row++)
14465 w->cursor.vpos++;
14466 w->cursor.y = row->y;
14468 if (row < bottom_row)
14470 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14471 struct glyph *end = glyph + row->used[TEXT_AREA];
14473 for (; glyph < end
14474 && (!BUFFERP (glyph->object)
14475 || glyph->charpos < PT);
14476 glyph++)
14478 w->cursor.hpos++;
14479 w->cursor.x += glyph->pixel_width;
14484 /* Adjust window end. A null value of last_text_row means that
14485 the window end is in reused rows which in turn means that
14486 only its vpos can have changed. */
14487 if (last_text_row)
14489 w->window_end_bytepos
14490 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14491 w->window_end_pos
14492 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14493 w->window_end_vpos
14494 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14496 else
14498 w->window_end_vpos
14499 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14502 w->window_end_valid = Qnil;
14503 w->desired_matrix->no_scrolling_p = 1;
14505 #if GLYPH_DEBUG
14506 debug_method_add (w, "try_window_reusing_current_matrix 2");
14507 #endif
14508 return 1;
14511 return 0;
14516 /************************************************************************
14517 Window redisplay reusing current matrix when buffer has changed
14518 ************************************************************************/
14520 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14521 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14522 int *, int *));
14523 static struct glyph_row *
14524 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14525 struct glyph_row *));
14528 /* Return the last row in MATRIX displaying text. If row START is
14529 non-null, start searching with that row. IT gives the dimensions
14530 of the display. Value is null if matrix is empty; otherwise it is
14531 a pointer to the row found. */
14533 static struct glyph_row *
14534 find_last_row_displaying_text (matrix, it, start)
14535 struct glyph_matrix *matrix;
14536 struct it *it;
14537 struct glyph_row *start;
14539 struct glyph_row *row, *row_found;
14541 /* Set row_found to the last row in IT->w's current matrix
14542 displaying text. The loop looks funny but think of partially
14543 visible lines. */
14544 row_found = NULL;
14545 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14546 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14548 xassert (row->enabled_p);
14549 row_found = row;
14550 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14551 break;
14552 ++row;
14555 return row_found;
14559 /* Return the last row in the current matrix of W that is not affected
14560 by changes at the start of current_buffer that occurred since W's
14561 current matrix was built. Value is null if no such row exists.
14563 BEG_UNCHANGED us the number of characters unchanged at the start of
14564 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14565 first changed character in current_buffer. Characters at positions <
14566 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14567 when the current matrix was built. */
14569 static struct glyph_row *
14570 find_last_unchanged_at_beg_row (w)
14571 struct window *w;
14573 int first_changed_pos = BEG + BEG_UNCHANGED;
14574 struct glyph_row *row;
14575 struct glyph_row *row_found = NULL;
14576 int yb = window_text_bottom_y (w);
14578 /* Find the last row displaying unchanged text. */
14579 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14580 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14581 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14582 ++row)
14584 if (/* If row ends before first_changed_pos, it is unchanged,
14585 except in some case. */
14586 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14587 /* When row ends in ZV and we write at ZV it is not
14588 unchanged. */
14589 && !row->ends_at_zv_p
14590 /* When first_changed_pos is the end of a continued line,
14591 row is not unchanged because it may be no longer
14592 continued. */
14593 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14594 && (row->continued_p
14595 || row->exact_window_width_line_p)))
14596 row_found = row;
14598 /* Stop if last visible row. */
14599 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14600 break;
14603 return row_found;
14607 /* Find the first glyph row in the current matrix of W that is not
14608 affected by changes at the end of current_buffer since the
14609 time W's current matrix was built.
14611 Return in *DELTA the number of chars by which buffer positions in
14612 unchanged text at the end of current_buffer must be adjusted.
14614 Return in *DELTA_BYTES the corresponding number of bytes.
14616 Value is null if no such row exists, i.e. all rows are affected by
14617 changes. */
14619 static struct glyph_row *
14620 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14621 struct window *w;
14622 int *delta, *delta_bytes;
14624 struct glyph_row *row;
14625 struct glyph_row *row_found = NULL;
14627 *delta = *delta_bytes = 0;
14629 /* Display must not have been paused, otherwise the current matrix
14630 is not up to date. */
14631 eassert (!NILP (w->window_end_valid));
14633 /* A value of window_end_pos >= END_UNCHANGED means that the window
14634 end is in the range of changed text. If so, there is no
14635 unchanged row at the end of W's current matrix. */
14636 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14637 return NULL;
14639 /* Set row to the last row in W's current matrix displaying text. */
14640 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14642 /* If matrix is entirely empty, no unchanged row exists. */
14643 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14645 /* The value of row is the last glyph row in the matrix having a
14646 meaningful buffer position in it. The end position of row
14647 corresponds to window_end_pos. This allows us to translate
14648 buffer positions in the current matrix to current buffer
14649 positions for characters not in changed text. */
14650 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14651 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14652 int last_unchanged_pos, last_unchanged_pos_old;
14653 struct glyph_row *first_text_row
14654 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14656 *delta = Z - Z_old;
14657 *delta_bytes = Z_BYTE - Z_BYTE_old;
14659 /* Set last_unchanged_pos to the buffer position of the last
14660 character in the buffer that has not been changed. Z is the
14661 index + 1 of the last character in current_buffer, i.e. by
14662 subtracting END_UNCHANGED we get the index of the last
14663 unchanged character, and we have to add BEG to get its buffer
14664 position. */
14665 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14666 last_unchanged_pos_old = last_unchanged_pos - *delta;
14668 /* Search backward from ROW for a row displaying a line that
14669 starts at a minimum position >= last_unchanged_pos_old. */
14670 for (; row > first_text_row; --row)
14672 /* This used to abort, but it can happen.
14673 It is ok to just stop the search instead here. KFS. */
14674 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14675 break;
14677 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14678 row_found = row;
14682 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14684 return row_found;
14688 /* Make sure that glyph rows in the current matrix of window W
14689 reference the same glyph memory as corresponding rows in the
14690 frame's frame matrix. This function is called after scrolling W's
14691 current matrix on a terminal frame in try_window_id and
14692 try_window_reusing_current_matrix. */
14694 static void
14695 sync_frame_with_window_matrix_rows (w)
14696 struct window *w;
14698 struct frame *f = XFRAME (w->frame);
14699 struct glyph_row *window_row, *window_row_end, *frame_row;
14701 /* Preconditions: W must be a leaf window and full-width. Its frame
14702 must have a frame matrix. */
14703 xassert (NILP (w->hchild) && NILP (w->vchild));
14704 xassert (WINDOW_FULL_WIDTH_P (w));
14705 xassert (!FRAME_WINDOW_P (f));
14707 /* If W is a full-width window, glyph pointers in W's current matrix
14708 have, by definition, to be the same as glyph pointers in the
14709 corresponding frame matrix. Note that frame matrices have no
14710 marginal areas (see build_frame_matrix). */
14711 window_row = w->current_matrix->rows;
14712 window_row_end = window_row + w->current_matrix->nrows;
14713 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14714 while (window_row < window_row_end)
14716 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14717 struct glyph *end = window_row->glyphs[LAST_AREA];
14719 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14720 frame_row->glyphs[TEXT_AREA] = start;
14721 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14722 frame_row->glyphs[LAST_AREA] = end;
14724 /* Disable frame rows whose corresponding window rows have
14725 been disabled in try_window_id. */
14726 if (!window_row->enabled_p)
14727 frame_row->enabled_p = 0;
14729 ++window_row, ++frame_row;
14734 /* Find the glyph row in window W containing CHARPOS. Consider all
14735 rows between START and END (not inclusive). END null means search
14736 all rows to the end of the display area of W. Value is the row
14737 containing CHARPOS or null. */
14739 struct glyph_row *
14740 row_containing_pos (w, charpos, start, end, dy)
14741 struct window *w;
14742 int charpos;
14743 struct glyph_row *start, *end;
14744 int dy;
14746 struct glyph_row *row = start;
14747 int last_y;
14749 /* If we happen to start on a header-line, skip that. */
14750 if (row->mode_line_p)
14751 ++row;
14753 if ((end && row >= end) || !row->enabled_p)
14754 return NULL;
14756 last_y = window_text_bottom_y (w) - dy;
14758 while (1)
14760 /* Give up if we have gone too far. */
14761 if (end && row >= end)
14762 return NULL;
14763 /* This formerly returned if they were equal.
14764 I think that both quantities are of a "last plus one" type;
14765 if so, when they are equal, the row is within the screen. -- rms. */
14766 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14767 return NULL;
14769 /* If it is in this row, return this row. */
14770 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14771 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14772 /* The end position of a row equals the start
14773 position of the next row. If CHARPOS is there, we
14774 would rather display it in the next line, except
14775 when this line ends in ZV. */
14776 && !row->ends_at_zv_p
14777 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14778 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14779 return row;
14780 ++row;
14785 /* Try to redisplay window W by reusing its existing display. W's
14786 current matrix must be up to date when this function is called,
14787 i.e. window_end_valid must not be nil.
14789 Value is
14791 1 if display has been updated
14792 0 if otherwise unsuccessful
14793 -1 if redisplay with same window start is known not to succeed
14795 The following steps are performed:
14797 1. Find the last row in the current matrix of W that is not
14798 affected by changes at the start of current_buffer. If no such row
14799 is found, give up.
14801 2. Find the first row in W's current matrix that is not affected by
14802 changes at the end of current_buffer. Maybe there is no such row.
14804 3. Display lines beginning with the row + 1 found in step 1 to the
14805 row found in step 2 or, if step 2 didn't find a row, to the end of
14806 the window.
14808 4. If cursor is not known to appear on the window, give up.
14810 5. If display stopped at the row found in step 2, scroll the
14811 display and current matrix as needed.
14813 6. Maybe display some lines at the end of W, if we must. This can
14814 happen under various circumstances, like a partially visible line
14815 becoming fully visible, or because newly displayed lines are displayed
14816 in smaller font sizes.
14818 7. Update W's window end information. */
14820 static int
14821 try_window_id (w)
14822 struct window *w;
14824 struct frame *f = XFRAME (w->frame);
14825 struct glyph_matrix *current_matrix = w->current_matrix;
14826 struct glyph_matrix *desired_matrix = w->desired_matrix;
14827 struct glyph_row *last_unchanged_at_beg_row;
14828 struct glyph_row *first_unchanged_at_end_row;
14829 struct glyph_row *row;
14830 struct glyph_row *bottom_row;
14831 int bottom_vpos;
14832 struct it it;
14833 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14834 struct text_pos start_pos;
14835 struct run run;
14836 int first_unchanged_at_end_vpos = 0;
14837 struct glyph_row *last_text_row, *last_text_row_at_end;
14838 struct text_pos start;
14839 int first_changed_charpos, last_changed_charpos;
14841 #if GLYPH_DEBUG
14842 if (inhibit_try_window_id)
14843 return 0;
14844 #endif
14846 /* This is handy for debugging. */
14847 #if 0
14848 #define GIVE_UP(X) \
14849 do { \
14850 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14851 return 0; \
14852 } while (0)
14853 #else
14854 #define GIVE_UP(X) return 0
14855 #endif
14857 SET_TEXT_POS_FROM_MARKER (start, w->start);
14859 /* Don't use this for mini-windows because these can show
14860 messages and mini-buffers, and we don't handle that here. */
14861 if (MINI_WINDOW_P (w))
14862 GIVE_UP (1);
14864 /* This flag is used to prevent redisplay optimizations. */
14865 if (windows_or_buffers_changed || cursor_type_changed)
14866 GIVE_UP (2);
14868 /* Verify that narrowing has not changed.
14869 Also verify that we were not told to prevent redisplay optimizations.
14870 It would be nice to further
14871 reduce the number of cases where this prevents try_window_id. */
14872 if (current_buffer->clip_changed
14873 || current_buffer->prevent_redisplay_optimizations_p)
14874 GIVE_UP (3);
14876 /* Window must either use window-based redisplay or be full width. */
14877 if (!FRAME_WINDOW_P (f)
14878 && (!FRAME_LINE_INS_DEL_OK (f)
14879 || !WINDOW_FULL_WIDTH_P (w)))
14880 GIVE_UP (4);
14882 /* Give up if point is known NOT to appear in W. */
14883 if (PT < CHARPOS (start))
14884 GIVE_UP (5);
14886 /* Another way to prevent redisplay optimizations. */
14887 if (XFASTINT (w->last_modified) == 0)
14888 GIVE_UP (6);
14890 /* Verify that window is not hscrolled. */
14891 if (XFASTINT (w->hscroll) != 0)
14892 GIVE_UP (7);
14894 /* Verify that display wasn't paused. */
14895 if (NILP (w->window_end_valid))
14896 GIVE_UP (8);
14898 /* Can't use this if highlighting a region because a cursor movement
14899 will do more than just set the cursor. */
14900 if (!NILP (Vtransient_mark_mode)
14901 && !NILP (current_buffer->mark_active))
14902 GIVE_UP (9);
14904 /* Likewise if highlighting trailing whitespace. */
14905 if (!NILP (Vshow_trailing_whitespace))
14906 GIVE_UP (11);
14908 /* Likewise if showing a region. */
14909 if (!NILP (w->region_showing))
14910 GIVE_UP (10);
14912 /* Can't use this if overlay arrow position and/or string have
14913 changed. */
14914 if (overlay_arrows_changed_p ())
14915 GIVE_UP (12);
14917 /* When word-wrap is on, adding a space to the first word of a
14918 wrapped line can change the wrap position, altering the line
14919 above it. It might be worthwhile to handle this more
14920 intelligently, but for now just redisplay from scratch. */
14921 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14922 GIVE_UP (21);
14924 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14925 only if buffer has really changed. The reason is that the gap is
14926 initially at Z for freshly visited files. The code below would
14927 set end_unchanged to 0 in that case. */
14928 if (MODIFF > SAVE_MODIFF
14929 /* This seems to happen sometimes after saving a buffer. */
14930 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14932 if (GPT - BEG < BEG_UNCHANGED)
14933 BEG_UNCHANGED = GPT - BEG;
14934 if (Z - GPT < END_UNCHANGED)
14935 END_UNCHANGED = Z - GPT;
14938 /* The position of the first and last character that has been changed. */
14939 first_changed_charpos = BEG + BEG_UNCHANGED;
14940 last_changed_charpos = Z - END_UNCHANGED;
14942 /* If window starts after a line end, and the last change is in
14943 front of that newline, then changes don't affect the display.
14944 This case happens with stealth-fontification. Note that although
14945 the display is unchanged, glyph positions in the matrix have to
14946 be adjusted, of course. */
14947 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14948 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14949 && ((last_changed_charpos < CHARPOS (start)
14950 && CHARPOS (start) == BEGV)
14951 || (last_changed_charpos < CHARPOS (start) - 1
14952 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14954 int Z_old, delta, Z_BYTE_old, delta_bytes;
14955 struct glyph_row *r0;
14957 /* Compute how many chars/bytes have been added to or removed
14958 from the buffer. */
14959 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14960 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14961 delta = Z - Z_old;
14962 delta_bytes = Z_BYTE - Z_BYTE_old;
14964 /* Give up if PT is not in the window. Note that it already has
14965 been checked at the start of try_window_id that PT is not in
14966 front of the window start. */
14967 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14968 GIVE_UP (13);
14970 /* If window start is unchanged, we can reuse the whole matrix
14971 as is, after adjusting glyph positions. No need to compute
14972 the window end again, since its offset from Z hasn't changed. */
14973 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14974 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14975 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14976 /* PT must not be in a partially visible line. */
14977 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14978 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14980 /* Adjust positions in the glyph matrix. */
14981 if (delta || delta_bytes)
14983 struct glyph_row *r1
14984 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14985 increment_matrix_positions (w->current_matrix,
14986 MATRIX_ROW_VPOS (r0, current_matrix),
14987 MATRIX_ROW_VPOS (r1, current_matrix),
14988 delta, delta_bytes);
14991 /* Set the cursor. */
14992 row = row_containing_pos (w, PT, r0, NULL, 0);
14993 if (row)
14994 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14995 else
14996 abort ();
14997 return 1;
15001 /* Handle the case that changes are all below what is displayed in
15002 the window, and that PT is in the window. This shortcut cannot
15003 be taken if ZV is visible in the window, and text has been added
15004 there that is visible in the window. */
15005 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
15006 /* ZV is not visible in the window, or there are no
15007 changes at ZV, actually. */
15008 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
15009 || first_changed_charpos == last_changed_charpos))
15011 struct glyph_row *r0;
15013 /* Give up if PT is not in the window. Note that it already has
15014 been checked at the start of try_window_id that PT is not in
15015 front of the window start. */
15016 if (PT >= MATRIX_ROW_END_CHARPOS (row))
15017 GIVE_UP (14);
15019 /* If window start is unchanged, we can reuse the whole matrix
15020 as is, without changing glyph positions since no text has
15021 been added/removed in front of the window end. */
15022 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15023 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15024 /* PT must not be in a partially visible line. */
15025 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15026 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15028 /* We have to compute the window end anew since text
15029 can have been added/removed after it. */
15030 w->window_end_pos
15031 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15032 w->window_end_bytepos
15033 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15035 /* Set the cursor. */
15036 row = row_containing_pos (w, PT, r0, NULL, 0);
15037 if (row)
15038 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15039 else
15040 abort ();
15041 return 2;
15045 /* Give up if window start is in the changed area.
15047 The condition used to read
15049 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15051 but why that was tested escapes me at the moment. */
15052 if (CHARPOS (start) >= first_changed_charpos
15053 && CHARPOS (start) <= last_changed_charpos)
15054 GIVE_UP (15);
15056 /* Check that window start agrees with the start of the first glyph
15057 row in its current matrix. Check this after we know the window
15058 start is not in changed text, otherwise positions would not be
15059 comparable. */
15060 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15061 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15062 GIVE_UP (16);
15064 /* Give up if the window ends in strings. Overlay strings
15065 at the end are difficult to handle, so don't try. */
15066 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15067 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15068 GIVE_UP (20);
15070 /* Compute the position at which we have to start displaying new
15071 lines. Some of the lines at the top of the window might be
15072 reusable because they are not displaying changed text. Find the
15073 last row in W's current matrix not affected by changes at the
15074 start of current_buffer. Value is null if changes start in the
15075 first line of window. */
15076 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15077 if (last_unchanged_at_beg_row)
15079 /* Avoid starting to display in the moddle of a character, a TAB
15080 for instance. This is easier than to set up the iterator
15081 exactly, and it's not a frequent case, so the additional
15082 effort wouldn't really pay off. */
15083 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15084 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15085 && last_unchanged_at_beg_row > w->current_matrix->rows)
15086 --last_unchanged_at_beg_row;
15088 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15089 GIVE_UP (17);
15091 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15092 GIVE_UP (18);
15093 start_pos = it.current.pos;
15095 /* Start displaying new lines in the desired matrix at the same
15096 vpos we would use in the current matrix, i.e. below
15097 last_unchanged_at_beg_row. */
15098 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15099 current_matrix);
15100 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15101 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15103 xassert (it.hpos == 0 && it.current_x == 0);
15105 else
15107 /* There are no reusable lines at the start of the window.
15108 Start displaying in the first text line. */
15109 start_display (&it, w, start);
15110 it.vpos = it.first_vpos;
15111 start_pos = it.current.pos;
15114 /* Find the first row that is not affected by changes at the end of
15115 the buffer. Value will be null if there is no unchanged row, in
15116 which case we must redisplay to the end of the window. delta
15117 will be set to the value by which buffer positions beginning with
15118 first_unchanged_at_end_row have to be adjusted due to text
15119 changes. */
15120 first_unchanged_at_end_row
15121 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15122 IF_DEBUG (debug_delta = delta);
15123 IF_DEBUG (debug_delta_bytes = delta_bytes);
15125 /* Set stop_pos to the buffer position up to which we will have to
15126 display new lines. If first_unchanged_at_end_row != NULL, this
15127 is the buffer position of the start of the line displayed in that
15128 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15129 that we don't stop at a buffer position. */
15130 stop_pos = 0;
15131 if (first_unchanged_at_end_row)
15133 xassert (last_unchanged_at_beg_row == NULL
15134 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15136 /* If this is a continuation line, move forward to the next one
15137 that isn't. Changes in lines above affect this line.
15138 Caution: this may move first_unchanged_at_end_row to a row
15139 not displaying text. */
15140 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15141 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15142 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15143 < it.last_visible_y))
15144 ++first_unchanged_at_end_row;
15146 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15147 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15148 >= it.last_visible_y))
15149 first_unchanged_at_end_row = NULL;
15150 else
15152 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15153 + delta);
15154 first_unchanged_at_end_vpos
15155 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15156 xassert (stop_pos >= Z - END_UNCHANGED);
15159 else if (last_unchanged_at_beg_row == NULL)
15160 GIVE_UP (19);
15163 #if GLYPH_DEBUG
15165 /* Either there is no unchanged row at the end, or the one we have
15166 now displays text. This is a necessary condition for the window
15167 end pos calculation at the end of this function. */
15168 xassert (first_unchanged_at_end_row == NULL
15169 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15171 debug_last_unchanged_at_beg_vpos
15172 = (last_unchanged_at_beg_row
15173 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15174 : -1);
15175 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15177 #endif /* GLYPH_DEBUG != 0 */
15180 /* Display new lines. Set last_text_row to the last new line
15181 displayed which has text on it, i.e. might end up as being the
15182 line where the window_end_vpos is. */
15183 w->cursor.vpos = -1;
15184 last_text_row = NULL;
15185 overlay_arrow_seen = 0;
15186 while (it.current_y < it.last_visible_y
15187 && !fonts_changed_p
15188 && (first_unchanged_at_end_row == NULL
15189 || IT_CHARPOS (it) < stop_pos))
15191 if (display_line (&it))
15192 last_text_row = it.glyph_row - 1;
15195 if (fonts_changed_p)
15196 return -1;
15199 /* Compute differences in buffer positions, y-positions etc. for
15200 lines reused at the bottom of the window. Compute what we can
15201 scroll. */
15202 if (first_unchanged_at_end_row
15203 /* No lines reused because we displayed everything up to the
15204 bottom of the window. */
15205 && it.current_y < it.last_visible_y)
15207 dvpos = (it.vpos
15208 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15209 current_matrix));
15210 dy = it.current_y - first_unchanged_at_end_row->y;
15211 run.current_y = first_unchanged_at_end_row->y;
15212 run.desired_y = run.current_y + dy;
15213 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15215 else
15217 delta = delta_bytes = dvpos = dy
15218 = run.current_y = run.desired_y = run.height = 0;
15219 first_unchanged_at_end_row = NULL;
15221 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15224 /* Find the cursor if not already found. We have to decide whether
15225 PT will appear on this window (it sometimes doesn't, but this is
15226 not a very frequent case.) This decision has to be made before
15227 the current matrix is altered. A value of cursor.vpos < 0 means
15228 that PT is either in one of the lines beginning at
15229 first_unchanged_at_end_row or below the window. Don't care for
15230 lines that might be displayed later at the window end; as
15231 mentioned, this is not a frequent case. */
15232 if (w->cursor.vpos < 0)
15234 /* Cursor in unchanged rows at the top? */
15235 if (PT < CHARPOS (start_pos)
15236 && last_unchanged_at_beg_row)
15238 row = row_containing_pos (w, PT,
15239 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15240 last_unchanged_at_beg_row + 1, 0);
15241 if (row)
15242 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15245 /* Start from first_unchanged_at_end_row looking for PT. */
15246 else if (first_unchanged_at_end_row)
15248 row = row_containing_pos (w, PT - delta,
15249 first_unchanged_at_end_row, NULL, 0);
15250 if (row)
15251 set_cursor_from_row (w, row, w->current_matrix, delta,
15252 delta_bytes, dy, dvpos);
15255 /* Give up if cursor was not found. */
15256 if (w->cursor.vpos < 0)
15258 clear_glyph_matrix (w->desired_matrix);
15259 return -1;
15263 /* Don't let the cursor end in the scroll margins. */
15265 int this_scroll_margin, cursor_height;
15267 this_scroll_margin = max (0, scroll_margin);
15268 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15269 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15270 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15272 if ((w->cursor.y < this_scroll_margin
15273 && CHARPOS (start) > BEGV)
15274 /* Old redisplay didn't take scroll margin into account at the bottom,
15275 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15276 || (w->cursor.y + (make_cursor_line_fully_visible_p
15277 ? cursor_height + this_scroll_margin
15278 : 1)) > it.last_visible_y)
15280 w->cursor.vpos = -1;
15281 clear_glyph_matrix (w->desired_matrix);
15282 return -1;
15286 /* Scroll the display. Do it before changing the current matrix so
15287 that xterm.c doesn't get confused about where the cursor glyph is
15288 found. */
15289 if (dy && run.height)
15291 update_begin (f);
15293 if (FRAME_WINDOW_P (f))
15295 FRAME_RIF (f)->update_window_begin_hook (w);
15296 FRAME_RIF (f)->clear_window_mouse_face (w);
15297 FRAME_RIF (f)->scroll_run_hook (w, &run);
15298 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15300 else
15302 /* Terminal frame. In this case, dvpos gives the number of
15303 lines to scroll by; dvpos < 0 means scroll up. */
15304 int first_unchanged_at_end_vpos
15305 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15306 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15307 int end = (WINDOW_TOP_EDGE_LINE (w)
15308 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15309 + window_internal_height (w));
15311 /* Perform the operation on the screen. */
15312 if (dvpos > 0)
15314 /* Scroll last_unchanged_at_beg_row to the end of the
15315 window down dvpos lines. */
15316 set_terminal_window (f, end);
15318 /* On dumb terminals delete dvpos lines at the end
15319 before inserting dvpos empty lines. */
15320 if (!FRAME_SCROLL_REGION_OK (f))
15321 ins_del_lines (f, end - dvpos, -dvpos);
15323 /* Insert dvpos empty lines in front of
15324 last_unchanged_at_beg_row. */
15325 ins_del_lines (f, from, dvpos);
15327 else if (dvpos < 0)
15329 /* Scroll up last_unchanged_at_beg_vpos to the end of
15330 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15331 set_terminal_window (f, end);
15333 /* Delete dvpos lines in front of
15334 last_unchanged_at_beg_vpos. ins_del_lines will set
15335 the cursor to the given vpos and emit |dvpos| delete
15336 line sequences. */
15337 ins_del_lines (f, from + dvpos, dvpos);
15339 /* On a dumb terminal insert dvpos empty lines at the
15340 end. */
15341 if (!FRAME_SCROLL_REGION_OK (f))
15342 ins_del_lines (f, end + dvpos, -dvpos);
15345 set_terminal_window (f, 0);
15348 update_end (f);
15351 /* Shift reused rows of the current matrix to the right position.
15352 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15353 text. */
15354 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15355 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15356 if (dvpos < 0)
15358 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15359 bottom_vpos, dvpos);
15360 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15361 bottom_vpos, 0);
15363 else if (dvpos > 0)
15365 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15366 bottom_vpos, dvpos);
15367 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15368 first_unchanged_at_end_vpos + dvpos, 0);
15371 /* For frame-based redisplay, make sure that current frame and window
15372 matrix are in sync with respect to glyph memory. */
15373 if (!FRAME_WINDOW_P (f))
15374 sync_frame_with_window_matrix_rows (w);
15376 /* Adjust buffer positions in reused rows. */
15377 if (delta || delta_bytes)
15378 increment_matrix_positions (current_matrix,
15379 first_unchanged_at_end_vpos + dvpos,
15380 bottom_vpos, delta, delta_bytes);
15382 /* Adjust Y positions. */
15383 if (dy)
15384 shift_glyph_matrix (w, current_matrix,
15385 first_unchanged_at_end_vpos + dvpos,
15386 bottom_vpos, dy);
15388 if (first_unchanged_at_end_row)
15390 first_unchanged_at_end_row += dvpos;
15391 if (first_unchanged_at_end_row->y >= it.last_visible_y
15392 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15393 first_unchanged_at_end_row = NULL;
15396 /* If scrolling up, there may be some lines to display at the end of
15397 the window. */
15398 last_text_row_at_end = NULL;
15399 if (dy < 0)
15401 /* Scrolling up can leave for example a partially visible line
15402 at the end of the window to be redisplayed. */
15403 /* Set last_row to the glyph row in the current matrix where the
15404 window end line is found. It has been moved up or down in
15405 the matrix by dvpos. */
15406 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15407 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15409 /* If last_row is the window end line, it should display text. */
15410 xassert (last_row->displays_text_p);
15412 /* If window end line was partially visible before, begin
15413 displaying at that line. Otherwise begin displaying with the
15414 line following it. */
15415 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15417 init_to_row_start (&it, w, last_row);
15418 it.vpos = last_vpos;
15419 it.current_y = last_row->y;
15421 else
15423 init_to_row_end (&it, w, last_row);
15424 it.vpos = 1 + last_vpos;
15425 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15426 ++last_row;
15429 /* We may start in a continuation line. If so, we have to
15430 get the right continuation_lines_width and current_x. */
15431 it.continuation_lines_width = last_row->continuation_lines_width;
15432 it.hpos = it.current_x = 0;
15434 /* Display the rest of the lines at the window end. */
15435 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15436 while (it.current_y < it.last_visible_y
15437 && !fonts_changed_p)
15439 /* Is it always sure that the display agrees with lines in
15440 the current matrix? I don't think so, so we mark rows
15441 displayed invalid in the current matrix by setting their
15442 enabled_p flag to zero. */
15443 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15444 if (display_line (&it))
15445 last_text_row_at_end = it.glyph_row - 1;
15449 /* Update window_end_pos and window_end_vpos. */
15450 if (first_unchanged_at_end_row
15451 && !last_text_row_at_end)
15453 /* Window end line if one of the preserved rows from the current
15454 matrix. Set row to the last row displaying text in current
15455 matrix starting at first_unchanged_at_end_row, after
15456 scrolling. */
15457 xassert (first_unchanged_at_end_row->displays_text_p);
15458 row = find_last_row_displaying_text (w->current_matrix, &it,
15459 first_unchanged_at_end_row);
15460 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15462 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15463 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15464 w->window_end_vpos
15465 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15466 xassert (w->window_end_bytepos >= 0);
15467 IF_DEBUG (debug_method_add (w, "A"));
15469 else if (last_text_row_at_end)
15471 w->window_end_pos
15472 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15473 w->window_end_bytepos
15474 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15475 w->window_end_vpos
15476 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15477 xassert (w->window_end_bytepos >= 0);
15478 IF_DEBUG (debug_method_add (w, "B"));
15480 else if (last_text_row)
15482 /* We have displayed either to the end of the window or at the
15483 end of the window, i.e. the last row with text is to be found
15484 in the desired matrix. */
15485 w->window_end_pos
15486 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15487 w->window_end_bytepos
15488 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15489 w->window_end_vpos
15490 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15491 xassert (w->window_end_bytepos >= 0);
15493 else if (first_unchanged_at_end_row == NULL
15494 && last_text_row == NULL
15495 && last_text_row_at_end == NULL)
15497 /* Displayed to end of window, but no line containing text was
15498 displayed. Lines were deleted at the end of the window. */
15499 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15500 int vpos = XFASTINT (w->window_end_vpos);
15501 struct glyph_row *current_row = current_matrix->rows + vpos;
15502 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15504 for (row = NULL;
15505 row == NULL && vpos >= first_vpos;
15506 --vpos, --current_row, --desired_row)
15508 if (desired_row->enabled_p)
15510 if (desired_row->displays_text_p)
15511 row = desired_row;
15513 else if (current_row->displays_text_p)
15514 row = current_row;
15517 xassert (row != NULL);
15518 w->window_end_vpos = make_number (vpos + 1);
15519 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15520 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15521 xassert (w->window_end_bytepos >= 0);
15522 IF_DEBUG (debug_method_add (w, "C"));
15524 else
15525 abort ();
15527 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15528 debug_end_vpos = XFASTINT (w->window_end_vpos));
15530 /* Record that display has not been completed. */
15531 w->window_end_valid = Qnil;
15532 w->desired_matrix->no_scrolling_p = 1;
15533 return 3;
15535 #undef GIVE_UP
15540 /***********************************************************************
15541 More debugging support
15542 ***********************************************************************/
15544 #if GLYPH_DEBUG
15546 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15547 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15548 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15551 /* Dump the contents of glyph matrix MATRIX on stderr.
15553 GLYPHS 0 means don't show glyph contents.
15554 GLYPHS 1 means show glyphs in short form
15555 GLYPHS > 1 means show glyphs in long form. */
15557 void
15558 dump_glyph_matrix (matrix, glyphs)
15559 struct glyph_matrix *matrix;
15560 int glyphs;
15562 int i;
15563 for (i = 0; i < matrix->nrows; ++i)
15564 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15568 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15569 the glyph row and area where the glyph comes from. */
15571 void
15572 dump_glyph (row, glyph, area)
15573 struct glyph_row *row;
15574 struct glyph *glyph;
15575 int area;
15577 if (glyph->type == CHAR_GLYPH)
15579 fprintf (stderr,
15580 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15581 glyph - row->glyphs[TEXT_AREA],
15582 'C',
15583 glyph->charpos,
15584 (BUFFERP (glyph->object)
15585 ? 'B'
15586 : (STRINGP (glyph->object)
15587 ? 'S'
15588 : '-')),
15589 glyph->pixel_width,
15590 glyph->u.ch,
15591 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15592 ? glyph->u.ch
15593 : '.'),
15594 glyph->face_id,
15595 glyph->left_box_line_p,
15596 glyph->right_box_line_p);
15598 else if (glyph->type == STRETCH_GLYPH)
15600 fprintf (stderr,
15601 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15602 glyph - row->glyphs[TEXT_AREA],
15603 'S',
15604 glyph->charpos,
15605 (BUFFERP (glyph->object)
15606 ? 'B'
15607 : (STRINGP (glyph->object)
15608 ? 'S'
15609 : '-')),
15610 glyph->pixel_width,
15612 '.',
15613 glyph->face_id,
15614 glyph->left_box_line_p,
15615 glyph->right_box_line_p);
15617 else if (glyph->type == IMAGE_GLYPH)
15619 fprintf (stderr,
15620 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15621 glyph - row->glyphs[TEXT_AREA],
15622 'I',
15623 glyph->charpos,
15624 (BUFFERP (glyph->object)
15625 ? 'B'
15626 : (STRINGP (glyph->object)
15627 ? 'S'
15628 : '-')),
15629 glyph->pixel_width,
15630 glyph->u.img_id,
15631 '.',
15632 glyph->face_id,
15633 glyph->left_box_line_p,
15634 glyph->right_box_line_p);
15636 else if (glyph->type == COMPOSITE_GLYPH)
15638 fprintf (stderr,
15639 " %5d %4c %6d %c %3d 0x%05x",
15640 glyph - row->glyphs[TEXT_AREA],
15641 '+',
15642 glyph->charpos,
15643 (BUFFERP (glyph->object)
15644 ? 'B'
15645 : (STRINGP (glyph->object)
15646 ? 'S'
15647 : '-')),
15648 glyph->pixel_width,
15649 glyph->u.cmp.id);
15650 if (glyph->u.cmp.automatic)
15651 fprintf (stderr,
15652 "[%d-%d]",
15653 glyph->u.cmp.from, glyph->u.cmp.to);
15654 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15655 glyph->face_id,
15656 glyph->left_box_line_p,
15657 glyph->right_box_line_p);
15662 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15663 GLYPHS 0 means don't show glyph contents.
15664 GLYPHS 1 means show glyphs in short form
15665 GLYPHS > 1 means show glyphs in long form. */
15667 void
15668 dump_glyph_row (row, vpos, glyphs)
15669 struct glyph_row *row;
15670 int vpos, glyphs;
15672 if (glyphs != 1)
15674 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15675 fprintf (stderr, "======================================================================\n");
15677 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15678 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15679 vpos,
15680 MATRIX_ROW_START_CHARPOS (row),
15681 MATRIX_ROW_END_CHARPOS (row),
15682 row->used[TEXT_AREA],
15683 row->contains_overlapping_glyphs_p,
15684 row->enabled_p,
15685 row->truncated_on_left_p,
15686 row->truncated_on_right_p,
15687 row->continued_p,
15688 MATRIX_ROW_CONTINUATION_LINE_P (row),
15689 row->displays_text_p,
15690 row->ends_at_zv_p,
15691 row->fill_line_p,
15692 row->ends_in_middle_of_char_p,
15693 row->starts_in_middle_of_char_p,
15694 row->mouse_face_p,
15695 row->x,
15696 row->y,
15697 row->pixel_width,
15698 row->height,
15699 row->visible_height,
15700 row->ascent,
15701 row->phys_ascent);
15702 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15703 row->end.overlay_string_index,
15704 row->continuation_lines_width);
15705 fprintf (stderr, "%9d %5d\n",
15706 CHARPOS (row->start.string_pos),
15707 CHARPOS (row->end.string_pos));
15708 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15709 row->end.dpvec_index);
15712 if (glyphs > 1)
15714 int area;
15716 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15718 struct glyph *glyph = row->glyphs[area];
15719 struct glyph *glyph_end = glyph + row->used[area];
15721 /* Glyph for a line end in text. */
15722 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15723 ++glyph_end;
15725 if (glyph < glyph_end)
15726 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15728 for (; glyph < glyph_end; ++glyph)
15729 dump_glyph (row, glyph, area);
15732 else if (glyphs == 1)
15734 int area;
15736 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15738 char *s = (char *) alloca (row->used[area] + 1);
15739 int i;
15741 for (i = 0; i < row->used[area]; ++i)
15743 struct glyph *glyph = row->glyphs[area] + i;
15744 if (glyph->type == CHAR_GLYPH
15745 && glyph->u.ch < 0x80
15746 && glyph->u.ch >= ' ')
15747 s[i] = glyph->u.ch;
15748 else
15749 s[i] = '.';
15752 s[i] = '\0';
15753 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15759 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15760 Sdump_glyph_matrix, 0, 1, "p",
15761 doc: /* Dump the current matrix of the selected window to stderr.
15762 Shows contents of glyph row structures. With non-nil
15763 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15764 glyphs in short form, otherwise show glyphs in long form. */)
15765 (glyphs)
15766 Lisp_Object glyphs;
15768 struct window *w = XWINDOW (selected_window);
15769 struct buffer *buffer = XBUFFER (w->buffer);
15771 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15772 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15773 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15774 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15775 fprintf (stderr, "=============================================\n");
15776 dump_glyph_matrix (w->current_matrix,
15777 NILP (glyphs) ? 0 : XINT (glyphs));
15778 return Qnil;
15782 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15783 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15786 struct frame *f = XFRAME (selected_frame);
15787 dump_glyph_matrix (f->current_matrix, 1);
15788 return Qnil;
15792 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15793 doc: /* Dump glyph row ROW to stderr.
15794 GLYPH 0 means don't dump glyphs.
15795 GLYPH 1 means dump glyphs in short form.
15796 GLYPH > 1 or omitted means dump glyphs in long form. */)
15797 (row, glyphs)
15798 Lisp_Object row, glyphs;
15800 struct glyph_matrix *matrix;
15801 int vpos;
15803 CHECK_NUMBER (row);
15804 matrix = XWINDOW (selected_window)->current_matrix;
15805 vpos = XINT (row);
15806 if (vpos >= 0 && vpos < matrix->nrows)
15807 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15808 vpos,
15809 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15810 return Qnil;
15814 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15815 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15816 GLYPH 0 means don't dump glyphs.
15817 GLYPH 1 means dump glyphs in short form.
15818 GLYPH > 1 or omitted means dump glyphs in long form. */)
15819 (row, glyphs)
15820 Lisp_Object row, glyphs;
15822 struct frame *sf = SELECTED_FRAME ();
15823 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15824 int vpos;
15826 CHECK_NUMBER (row);
15827 vpos = XINT (row);
15828 if (vpos >= 0 && vpos < m->nrows)
15829 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15830 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15831 return Qnil;
15835 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15836 doc: /* Toggle tracing of redisplay.
15837 With ARG, turn tracing on if and only if ARG is positive. */)
15838 (arg)
15839 Lisp_Object arg;
15841 if (NILP (arg))
15842 trace_redisplay_p = !trace_redisplay_p;
15843 else
15845 arg = Fprefix_numeric_value (arg);
15846 trace_redisplay_p = XINT (arg) > 0;
15849 return Qnil;
15853 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15854 doc: /* Like `format', but print result to stderr.
15855 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15856 (nargs, args)
15857 int nargs;
15858 Lisp_Object *args;
15860 Lisp_Object s = Fformat (nargs, args);
15861 fprintf (stderr, "%s", SDATA (s));
15862 return Qnil;
15865 #endif /* GLYPH_DEBUG */
15869 /***********************************************************************
15870 Building Desired Matrix Rows
15871 ***********************************************************************/
15873 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15874 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15876 static struct glyph_row *
15877 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15878 struct window *w;
15879 Lisp_Object overlay_arrow_string;
15881 struct frame *f = XFRAME (WINDOW_FRAME (w));
15882 struct buffer *buffer = XBUFFER (w->buffer);
15883 struct buffer *old = current_buffer;
15884 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15885 int arrow_len = SCHARS (overlay_arrow_string);
15886 const unsigned char *arrow_end = arrow_string + arrow_len;
15887 const unsigned char *p;
15888 struct it it;
15889 int multibyte_p;
15890 int n_glyphs_before;
15892 set_buffer_temp (buffer);
15893 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15894 it.glyph_row->used[TEXT_AREA] = 0;
15895 SET_TEXT_POS (it.position, 0, 0);
15897 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15898 p = arrow_string;
15899 while (p < arrow_end)
15901 Lisp_Object face, ilisp;
15903 /* Get the next character. */
15904 if (multibyte_p)
15905 it.c = string_char_and_length (p, &it.len);
15906 else
15907 it.c = *p, it.len = 1;
15908 p += it.len;
15910 /* Get its face. */
15911 ilisp = make_number (p - arrow_string);
15912 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15913 it.face_id = compute_char_face (f, it.c, face);
15915 /* Compute its width, get its glyphs. */
15916 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15917 SET_TEXT_POS (it.position, -1, -1);
15918 PRODUCE_GLYPHS (&it);
15920 /* If this character doesn't fit any more in the line, we have
15921 to remove some glyphs. */
15922 if (it.current_x > it.last_visible_x)
15924 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15925 break;
15929 set_buffer_temp (old);
15930 return it.glyph_row;
15934 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15935 glyphs are only inserted for terminal frames since we can't really
15936 win with truncation glyphs when partially visible glyphs are
15937 involved. Which glyphs to insert is determined by
15938 produce_special_glyphs. */
15940 static void
15941 insert_left_trunc_glyphs (it)
15942 struct it *it;
15944 struct it truncate_it;
15945 struct glyph *from, *end, *to, *toend;
15947 xassert (!FRAME_WINDOW_P (it->f));
15949 /* Get the truncation glyphs. */
15950 truncate_it = *it;
15951 truncate_it.current_x = 0;
15952 truncate_it.face_id = DEFAULT_FACE_ID;
15953 truncate_it.glyph_row = &scratch_glyph_row;
15954 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15955 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15956 truncate_it.object = make_number (0);
15957 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15959 /* Overwrite glyphs from IT with truncation glyphs. */
15960 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15961 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15962 to = it->glyph_row->glyphs[TEXT_AREA];
15963 toend = to + it->glyph_row->used[TEXT_AREA];
15965 while (from < end)
15966 *to++ = *from++;
15968 /* There may be padding glyphs left over. Overwrite them too. */
15969 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15971 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15972 while (from < end)
15973 *to++ = *from++;
15976 if (to > toend)
15977 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15981 /* Compute the pixel height and width of IT->glyph_row.
15983 Most of the time, ascent and height of a display line will be equal
15984 to the max_ascent and max_height values of the display iterator
15985 structure. This is not the case if
15987 1. We hit ZV without displaying anything. In this case, max_ascent
15988 and max_height will be zero.
15990 2. We have some glyphs that don't contribute to the line height.
15991 (The glyph row flag contributes_to_line_height_p is for future
15992 pixmap extensions).
15994 The first case is easily covered by using default values because in
15995 these cases, the line height does not really matter, except that it
15996 must not be zero. */
15998 static void
15999 compute_line_metrics (it)
16000 struct it *it;
16002 struct glyph_row *row = it->glyph_row;
16003 int area, i;
16005 if (FRAME_WINDOW_P (it->f))
16007 int i, min_y, max_y;
16009 /* The line may consist of one space only, that was added to
16010 place the cursor on it. If so, the row's height hasn't been
16011 computed yet. */
16012 if (row->height == 0)
16014 if (it->max_ascent + it->max_descent == 0)
16015 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
16016 row->ascent = it->max_ascent;
16017 row->height = it->max_ascent + it->max_descent;
16018 row->phys_ascent = it->max_phys_ascent;
16019 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16020 row->extra_line_spacing = it->max_extra_line_spacing;
16023 /* Compute the width of this line. */
16024 row->pixel_width = row->x;
16025 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16026 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16028 xassert (row->pixel_width >= 0);
16029 xassert (row->ascent >= 0 && row->height > 0);
16031 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16032 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16034 /* If first line's physical ascent is larger than its logical
16035 ascent, use the physical ascent, and make the row taller.
16036 This makes accented characters fully visible. */
16037 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16038 && row->phys_ascent > row->ascent)
16040 row->height += row->phys_ascent - row->ascent;
16041 row->ascent = row->phys_ascent;
16044 /* Compute how much of the line is visible. */
16045 row->visible_height = row->height;
16047 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16048 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16050 if (row->y < min_y)
16051 row->visible_height -= min_y - row->y;
16052 if (row->y + row->height > max_y)
16053 row->visible_height -= row->y + row->height - max_y;
16055 else
16057 row->pixel_width = row->used[TEXT_AREA];
16058 if (row->continued_p)
16059 row->pixel_width -= it->continuation_pixel_width;
16060 else if (row->truncated_on_right_p)
16061 row->pixel_width -= it->truncation_pixel_width;
16062 row->ascent = row->phys_ascent = 0;
16063 row->height = row->phys_height = row->visible_height = 1;
16064 row->extra_line_spacing = 0;
16067 /* Compute a hash code for this row. */
16068 row->hash = 0;
16069 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16070 for (i = 0; i < row->used[area]; ++i)
16071 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16072 + row->glyphs[area][i].u.val
16073 + row->glyphs[area][i].face_id
16074 + row->glyphs[area][i].padding_p
16075 + (row->glyphs[area][i].type << 2));
16077 it->max_ascent = it->max_descent = 0;
16078 it->max_phys_ascent = it->max_phys_descent = 0;
16082 /* Append one space to the glyph row of iterator IT if doing a
16083 window-based redisplay. The space has the same face as
16084 IT->face_id. Value is non-zero if a space was added.
16086 This function is called to make sure that there is always one glyph
16087 at the end of a glyph row that the cursor can be set on under
16088 window-systems. (If there weren't such a glyph we would not know
16089 how wide and tall a box cursor should be displayed).
16091 At the same time this space let's a nicely handle clearing to the
16092 end of the line if the row ends in italic text. */
16094 static int
16095 append_space_for_newline (it, default_face_p)
16096 struct it *it;
16097 int default_face_p;
16099 if (FRAME_WINDOW_P (it->f))
16101 int n = it->glyph_row->used[TEXT_AREA];
16103 if (it->glyph_row->glyphs[TEXT_AREA] + n
16104 < it->glyph_row->glyphs[1 + TEXT_AREA])
16106 /* Save some values that must not be changed.
16107 Must save IT->c and IT->len because otherwise
16108 ITERATOR_AT_END_P wouldn't work anymore after
16109 append_space_for_newline has been called. */
16110 enum display_element_type saved_what = it->what;
16111 int saved_c = it->c, saved_len = it->len;
16112 int saved_x = it->current_x;
16113 int saved_face_id = it->face_id;
16114 struct text_pos saved_pos;
16115 Lisp_Object saved_object;
16116 struct face *face;
16118 saved_object = it->object;
16119 saved_pos = it->position;
16121 it->what = IT_CHARACTER;
16122 bzero (&it->position, sizeof it->position);
16123 it->object = make_number (0);
16124 it->c = ' ';
16125 it->len = 1;
16127 if (default_face_p)
16128 it->face_id = DEFAULT_FACE_ID;
16129 else if (it->face_before_selective_p)
16130 it->face_id = it->saved_face_id;
16131 face = FACE_FROM_ID (it->f, it->face_id);
16132 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16134 PRODUCE_GLYPHS (it);
16136 it->override_ascent = -1;
16137 it->constrain_row_ascent_descent_p = 0;
16138 it->current_x = saved_x;
16139 it->object = saved_object;
16140 it->position = saved_pos;
16141 it->what = saved_what;
16142 it->face_id = saved_face_id;
16143 it->len = saved_len;
16144 it->c = saved_c;
16145 return 1;
16149 return 0;
16153 /* Extend the face of the last glyph in the text area of IT->glyph_row
16154 to the end of the display line. Called from display_line.
16155 If the glyph row is empty, add a space glyph to it so that we
16156 know the face to draw. Set the glyph row flag fill_line_p. */
16158 static void
16159 extend_face_to_end_of_line (it)
16160 struct it *it;
16162 struct face *face;
16163 struct frame *f = it->f;
16165 /* If line is already filled, do nothing. */
16166 if (it->current_x >= it->last_visible_x)
16167 return;
16169 /* Face extension extends the background and box of IT->face_id
16170 to the end of the line. If the background equals the background
16171 of the frame, we don't have to do anything. */
16172 if (it->face_before_selective_p)
16173 face = FACE_FROM_ID (it->f, it->saved_face_id);
16174 else
16175 face = FACE_FROM_ID (f, it->face_id);
16177 if (FRAME_WINDOW_P (f)
16178 && it->glyph_row->displays_text_p
16179 && face->box == FACE_NO_BOX
16180 && face->background == FRAME_BACKGROUND_PIXEL (f)
16181 && !face->stipple)
16182 return;
16184 /* Set the glyph row flag indicating that the face of the last glyph
16185 in the text area has to be drawn to the end of the text area. */
16186 it->glyph_row->fill_line_p = 1;
16188 /* If current character of IT is not ASCII, make sure we have the
16189 ASCII face. This will be automatically undone the next time
16190 get_next_display_element returns a multibyte character. Note
16191 that the character will always be single byte in unibyte
16192 text. */
16193 if (!ASCII_CHAR_P (it->c))
16195 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16198 if (FRAME_WINDOW_P (f))
16200 /* If the row is empty, add a space with the current face of IT,
16201 so that we know which face to draw. */
16202 if (it->glyph_row->used[TEXT_AREA] == 0)
16204 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16205 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16206 it->glyph_row->used[TEXT_AREA] = 1;
16209 else
16211 /* Save some values that must not be changed. */
16212 int saved_x = it->current_x;
16213 struct text_pos saved_pos;
16214 Lisp_Object saved_object;
16215 enum display_element_type saved_what = it->what;
16216 int saved_face_id = it->face_id;
16218 saved_object = it->object;
16219 saved_pos = it->position;
16221 it->what = IT_CHARACTER;
16222 bzero (&it->position, sizeof it->position);
16223 it->object = make_number (0);
16224 it->c = ' ';
16225 it->len = 1;
16226 it->face_id = face->id;
16228 PRODUCE_GLYPHS (it);
16230 while (it->current_x <= it->last_visible_x)
16231 PRODUCE_GLYPHS (it);
16233 /* Don't count these blanks really. It would let us insert a left
16234 truncation glyph below and make us set the cursor on them, maybe. */
16235 it->current_x = saved_x;
16236 it->object = saved_object;
16237 it->position = saved_pos;
16238 it->what = saved_what;
16239 it->face_id = saved_face_id;
16244 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16245 trailing whitespace. */
16247 static int
16248 trailing_whitespace_p (charpos)
16249 int charpos;
16251 int bytepos = CHAR_TO_BYTE (charpos);
16252 int c = 0;
16254 while (bytepos < ZV_BYTE
16255 && (c = FETCH_CHAR (bytepos),
16256 c == ' ' || c == '\t'))
16257 ++bytepos;
16259 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16261 if (bytepos != PT_BYTE)
16262 return 1;
16264 return 0;
16268 /* Highlight trailing whitespace, if any, in ROW. */
16270 void
16271 highlight_trailing_whitespace (f, row)
16272 struct frame *f;
16273 struct glyph_row *row;
16275 int used = row->used[TEXT_AREA];
16277 if (used)
16279 struct glyph *start = row->glyphs[TEXT_AREA];
16280 struct glyph *glyph = start + used - 1;
16282 /* Skip over glyphs inserted to display the cursor at the
16283 end of a line, for extending the face of the last glyph
16284 to the end of the line on terminals, and for truncation
16285 and continuation glyphs. */
16286 while (glyph >= start
16287 && glyph->type == CHAR_GLYPH
16288 && INTEGERP (glyph->object))
16289 --glyph;
16291 /* If last glyph is a space or stretch, and it's trailing
16292 whitespace, set the face of all trailing whitespace glyphs in
16293 IT->glyph_row to `trailing-whitespace'. */
16294 if (glyph >= start
16295 && BUFFERP (glyph->object)
16296 && (glyph->type == STRETCH_GLYPH
16297 || (glyph->type == CHAR_GLYPH
16298 && glyph->u.ch == ' '))
16299 && trailing_whitespace_p (glyph->charpos))
16301 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16302 if (face_id < 0)
16303 return;
16305 while (glyph >= start
16306 && BUFFERP (glyph->object)
16307 && (glyph->type == STRETCH_GLYPH
16308 || (glyph->type == CHAR_GLYPH
16309 && glyph->u.ch == ' ')))
16310 (glyph--)->face_id = face_id;
16316 /* Value is non-zero if glyph row ROW in window W should be
16317 used to hold the cursor. */
16319 static int
16320 cursor_row_p (w, row)
16321 struct window *w;
16322 struct glyph_row *row;
16324 int cursor_row_p = 1;
16326 if (PT == MATRIX_ROW_END_CHARPOS (row))
16328 /* Suppose the row ends on a string.
16329 Unless the row is continued, that means it ends on a newline
16330 in the string. If it's anything other than a display string
16331 (e.g. a before-string from an overlay), we don't want the
16332 cursor there. (This heuristic seems to give the optimal
16333 behavior for the various types of multi-line strings.) */
16334 if (CHARPOS (row->end.string_pos) >= 0)
16336 if (row->continued_p)
16337 cursor_row_p = 1;
16338 else
16340 /* Check for `display' property. */
16341 struct glyph *beg = row->glyphs[TEXT_AREA];
16342 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16343 struct glyph *glyph;
16345 cursor_row_p = 0;
16346 for (glyph = end; glyph >= beg; --glyph)
16347 if (STRINGP (glyph->object))
16349 Lisp_Object prop
16350 = Fget_char_property (make_number (PT),
16351 Qdisplay, Qnil);
16352 cursor_row_p =
16353 (!NILP (prop)
16354 && display_prop_string_p (prop, glyph->object));
16355 break;
16359 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16361 /* If the row ends in middle of a real character,
16362 and the line is continued, we want the cursor here.
16363 That's because MATRIX_ROW_END_CHARPOS would equal
16364 PT if PT is before the character. */
16365 if (!row->ends_in_ellipsis_p)
16366 cursor_row_p = row->continued_p;
16367 else
16368 /* If the row ends in an ellipsis, then
16369 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16370 We want that position to be displayed after the ellipsis. */
16371 cursor_row_p = 0;
16373 /* If the row ends at ZV, display the cursor at the end of that
16374 row instead of at the start of the row below. */
16375 else if (row->ends_at_zv_p)
16376 cursor_row_p = 1;
16377 else
16378 cursor_row_p = 0;
16381 return cursor_row_p;
16386 /* Push the display property PROP so that it will be rendered at the
16387 current position in IT. */
16389 static void
16390 push_display_prop (struct it *it, Lisp_Object prop)
16392 push_it (it);
16394 /* Never display a cursor on the prefix. */
16395 it->avoid_cursor_p = 1;
16397 if (STRINGP (prop))
16399 if (SCHARS (prop) == 0)
16401 pop_it (it);
16402 return;
16405 it->string = prop;
16406 it->multibyte_p = STRING_MULTIBYTE (it->string);
16407 it->current.overlay_string_index = -1;
16408 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16409 it->end_charpos = it->string_nchars = SCHARS (it->string);
16410 it->method = GET_FROM_STRING;
16411 it->stop_charpos = 0;
16413 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16415 it->method = GET_FROM_STRETCH;
16416 it->object = prop;
16418 #ifdef HAVE_WINDOW_SYSTEM
16419 else if (IMAGEP (prop))
16421 it->what = IT_IMAGE;
16422 it->image_id = lookup_image (it->f, prop);
16423 it->method = GET_FROM_IMAGE;
16425 #endif /* HAVE_WINDOW_SYSTEM */
16426 else
16428 pop_it (it); /* bogus display property, give up */
16429 return;
16433 /* Return the character-property PROP at the current position in IT. */
16435 static Lisp_Object
16436 get_it_property (it, prop)
16437 struct it *it;
16438 Lisp_Object prop;
16440 Lisp_Object position;
16442 if (STRINGP (it->object))
16443 position = make_number (IT_STRING_CHARPOS (*it));
16444 else if (BUFFERP (it->object))
16445 position = make_number (IT_CHARPOS (*it));
16446 else
16447 return Qnil;
16449 return Fget_char_property (position, prop, it->object);
16452 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16454 static void
16455 handle_line_prefix (struct it *it)
16457 Lisp_Object prefix;
16458 if (it->continuation_lines_width > 0)
16460 prefix = get_it_property (it, Qwrap_prefix);
16461 if (NILP (prefix))
16462 prefix = Vwrap_prefix;
16464 else
16466 prefix = get_it_property (it, Qline_prefix);
16467 if (NILP (prefix))
16468 prefix = Vline_prefix;
16470 if (! NILP (prefix))
16472 push_display_prop (it, prefix);
16473 /* If the prefix is wider than the window, and we try to wrap
16474 it, it would acquire its own wrap prefix, and so on till the
16475 iterator stack overflows. So, don't wrap the prefix. */
16476 it->line_wrap = TRUNCATE;
16482 /* Construct the glyph row IT->glyph_row in the desired matrix of
16483 IT->w from text at the current position of IT. See dispextern.h
16484 for an overview of struct it. Value is non-zero if
16485 IT->glyph_row displays text, as opposed to a line displaying ZV
16486 only. */
16488 static int
16489 display_line (it)
16490 struct it *it;
16492 struct glyph_row *row = it->glyph_row;
16493 Lisp_Object overlay_arrow_string;
16494 struct it wrap_it;
16495 int may_wrap = 0, wrap_x;
16496 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16497 int wrap_row_phys_ascent, wrap_row_phys_height;
16498 int wrap_row_extra_line_spacing;
16500 /* We always start displaying at hpos zero even if hscrolled. */
16501 xassert (it->hpos == 0 && it->current_x == 0);
16503 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16504 >= it->w->desired_matrix->nrows)
16506 it->w->nrows_scale_factor++;
16507 fonts_changed_p = 1;
16508 return 0;
16511 /* Is IT->w showing the region? */
16512 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16514 /* Clear the result glyph row and enable it. */
16515 prepare_desired_row (row);
16517 row->y = it->current_y;
16518 row->start = it->start;
16519 row->continuation_lines_width = it->continuation_lines_width;
16520 row->displays_text_p = 1;
16521 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16522 it->starts_in_middle_of_char_p = 0;
16524 /* Arrange the overlays nicely for our purposes. Usually, we call
16525 display_line on only one line at a time, in which case this
16526 can't really hurt too much, or we call it on lines which appear
16527 one after another in the buffer, in which case all calls to
16528 recenter_overlay_lists but the first will be pretty cheap. */
16529 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16531 /* Move over display elements that are not visible because we are
16532 hscrolled. This may stop at an x-position < IT->first_visible_x
16533 if the first glyph is partially visible or if we hit a line end. */
16534 if (it->current_x < it->first_visible_x)
16536 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16537 MOVE_TO_POS | MOVE_TO_X);
16539 else
16541 /* We only do this when not calling `move_it_in_display_line_to'
16542 above, because move_it_in_display_line_to calls
16543 handle_line_prefix itself. */
16544 handle_line_prefix (it);
16547 /* Get the initial row height. This is either the height of the
16548 text hscrolled, if there is any, or zero. */
16549 row->ascent = it->max_ascent;
16550 row->height = it->max_ascent + it->max_descent;
16551 row->phys_ascent = it->max_phys_ascent;
16552 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16553 row->extra_line_spacing = it->max_extra_line_spacing;
16555 /* Loop generating characters. The loop is left with IT on the next
16556 character to display. */
16557 while (1)
16559 int n_glyphs_before, hpos_before, x_before;
16560 int x, i, nglyphs;
16561 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16563 /* Retrieve the next thing to display. Value is zero if end of
16564 buffer reached. */
16565 if (!get_next_display_element (it))
16567 /* Maybe add a space at the end of this line that is used to
16568 display the cursor there under X. Set the charpos of the
16569 first glyph of blank lines not corresponding to any text
16570 to -1. */
16571 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16572 row->exact_window_width_line_p = 1;
16573 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16574 || row->used[TEXT_AREA] == 0)
16576 row->glyphs[TEXT_AREA]->charpos = -1;
16577 row->displays_text_p = 0;
16579 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16580 && (!MINI_WINDOW_P (it->w)
16581 || (minibuf_level && EQ (it->window, minibuf_window))))
16582 row->indicate_empty_line_p = 1;
16585 it->continuation_lines_width = 0;
16586 row->ends_at_zv_p = 1;
16587 break;
16590 /* Now, get the metrics of what we want to display. This also
16591 generates glyphs in `row' (which is IT->glyph_row). */
16592 n_glyphs_before = row->used[TEXT_AREA];
16593 x = it->current_x;
16595 /* Remember the line height so far in case the next element doesn't
16596 fit on the line. */
16597 if (it->line_wrap != TRUNCATE)
16599 ascent = it->max_ascent;
16600 descent = it->max_descent;
16601 phys_ascent = it->max_phys_ascent;
16602 phys_descent = it->max_phys_descent;
16604 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16606 if (IT_DISPLAYING_WHITESPACE (it))
16607 may_wrap = 1;
16608 else if (may_wrap)
16610 wrap_it = *it;
16611 wrap_x = x;
16612 wrap_row_used = row->used[TEXT_AREA];
16613 wrap_row_ascent = row->ascent;
16614 wrap_row_height = row->height;
16615 wrap_row_phys_ascent = row->phys_ascent;
16616 wrap_row_phys_height = row->phys_height;
16617 wrap_row_extra_line_spacing = row->extra_line_spacing;
16618 may_wrap = 0;
16623 PRODUCE_GLYPHS (it);
16625 /* If this display element was in marginal areas, continue with
16626 the next one. */
16627 if (it->area != TEXT_AREA)
16629 row->ascent = max (row->ascent, it->max_ascent);
16630 row->height = max (row->height, it->max_ascent + it->max_descent);
16631 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16632 row->phys_height = max (row->phys_height,
16633 it->max_phys_ascent + it->max_phys_descent);
16634 row->extra_line_spacing = max (row->extra_line_spacing,
16635 it->max_extra_line_spacing);
16636 set_iterator_to_next (it, 1);
16637 continue;
16640 /* Does the display element fit on the line? If we truncate
16641 lines, we should draw past the right edge of the window. If
16642 we don't truncate, we want to stop so that we can display the
16643 continuation glyph before the right margin. If lines are
16644 continued, there are two possible strategies for characters
16645 resulting in more than 1 glyph (e.g. tabs): Display as many
16646 glyphs as possible in this line and leave the rest for the
16647 continuation line, or display the whole element in the next
16648 line. Original redisplay did the former, so we do it also. */
16649 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16650 hpos_before = it->hpos;
16651 x_before = x;
16653 if (/* Not a newline. */
16654 nglyphs > 0
16655 /* Glyphs produced fit entirely in the line. */
16656 && it->current_x < it->last_visible_x)
16658 it->hpos += nglyphs;
16659 row->ascent = max (row->ascent, it->max_ascent);
16660 row->height = max (row->height, it->max_ascent + it->max_descent);
16661 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16662 row->phys_height = max (row->phys_height,
16663 it->max_phys_ascent + it->max_phys_descent);
16664 row->extra_line_spacing = max (row->extra_line_spacing,
16665 it->max_extra_line_spacing);
16666 if (it->current_x - it->pixel_width < it->first_visible_x)
16667 row->x = x - it->first_visible_x;
16669 else
16671 int new_x;
16672 struct glyph *glyph;
16674 for (i = 0; i < nglyphs; ++i, x = new_x)
16676 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16677 new_x = x + glyph->pixel_width;
16679 if (/* Lines are continued. */
16680 it->line_wrap != TRUNCATE
16681 && (/* Glyph doesn't fit on the line. */
16682 new_x > it->last_visible_x
16683 /* Or it fits exactly on a window system frame. */
16684 || (new_x == it->last_visible_x
16685 && FRAME_WINDOW_P (it->f))))
16687 /* End of a continued line. */
16689 if (it->hpos == 0
16690 || (new_x == it->last_visible_x
16691 && FRAME_WINDOW_P (it->f)))
16693 /* Current glyph is the only one on the line or
16694 fits exactly on the line. We must continue
16695 the line because we can't draw the cursor
16696 after the glyph. */
16697 row->continued_p = 1;
16698 it->current_x = new_x;
16699 it->continuation_lines_width += new_x;
16700 ++it->hpos;
16701 if (i == nglyphs - 1)
16703 /* If line-wrap is on, check if a previous
16704 wrap point was found. */
16705 if (wrap_row_used > 0
16706 /* Even if there is a previous wrap
16707 point, continue the line here as
16708 usual, if (i) the previous character
16709 was a space or tab AND (ii) the
16710 current character is not. */
16711 && (!may_wrap
16712 || IT_DISPLAYING_WHITESPACE (it)))
16713 goto back_to_wrap;
16715 set_iterator_to_next (it, 1);
16716 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16718 if (!get_next_display_element (it))
16720 row->exact_window_width_line_p = 1;
16721 it->continuation_lines_width = 0;
16722 row->continued_p = 0;
16723 row->ends_at_zv_p = 1;
16725 else if (ITERATOR_AT_END_OF_LINE_P (it))
16727 row->continued_p = 0;
16728 row->exact_window_width_line_p = 1;
16733 else if (CHAR_GLYPH_PADDING_P (*glyph)
16734 && !FRAME_WINDOW_P (it->f))
16736 /* A padding glyph that doesn't fit on this line.
16737 This means the whole character doesn't fit
16738 on the line. */
16739 row->used[TEXT_AREA] = n_glyphs_before;
16741 /* Fill the rest of the row with continuation
16742 glyphs like in 20.x. */
16743 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16744 < row->glyphs[1 + TEXT_AREA])
16745 produce_special_glyphs (it, IT_CONTINUATION);
16747 row->continued_p = 1;
16748 it->current_x = x_before;
16749 it->continuation_lines_width += x_before;
16751 /* Restore the height to what it was before the
16752 element not fitting on the line. */
16753 it->max_ascent = ascent;
16754 it->max_descent = descent;
16755 it->max_phys_ascent = phys_ascent;
16756 it->max_phys_descent = phys_descent;
16758 else if (wrap_row_used > 0)
16760 back_to_wrap:
16761 *it = wrap_it;
16762 it->continuation_lines_width += wrap_x;
16763 row->used[TEXT_AREA] = wrap_row_used;
16764 row->ascent = wrap_row_ascent;
16765 row->height = wrap_row_height;
16766 row->phys_ascent = wrap_row_phys_ascent;
16767 row->phys_height = wrap_row_phys_height;
16768 row->extra_line_spacing = wrap_row_extra_line_spacing;
16769 row->continued_p = 1;
16770 row->ends_at_zv_p = 0;
16771 row->exact_window_width_line_p = 0;
16772 it->continuation_lines_width += x;
16774 /* Make sure that a non-default face is extended
16775 up to the right margin of the window. */
16776 extend_face_to_end_of_line (it);
16778 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16780 /* A TAB that extends past the right edge of the
16781 window. This produces a single glyph on
16782 window system frames. We leave the glyph in
16783 this row and let it fill the row, but don't
16784 consume the TAB. */
16785 it->continuation_lines_width += it->last_visible_x;
16786 row->ends_in_middle_of_char_p = 1;
16787 row->continued_p = 1;
16788 glyph->pixel_width = it->last_visible_x - x;
16789 it->starts_in_middle_of_char_p = 1;
16791 else
16793 /* Something other than a TAB that draws past
16794 the right edge of the window. Restore
16795 positions to values before the element. */
16796 row->used[TEXT_AREA] = n_glyphs_before + i;
16798 /* Display continuation glyphs. */
16799 if (!FRAME_WINDOW_P (it->f))
16800 produce_special_glyphs (it, IT_CONTINUATION);
16801 row->continued_p = 1;
16803 it->current_x = x_before;
16804 it->continuation_lines_width += x;
16805 extend_face_to_end_of_line (it);
16807 if (nglyphs > 1 && i > 0)
16809 row->ends_in_middle_of_char_p = 1;
16810 it->starts_in_middle_of_char_p = 1;
16813 /* Restore the height to what it was before the
16814 element not fitting on the line. */
16815 it->max_ascent = ascent;
16816 it->max_descent = descent;
16817 it->max_phys_ascent = phys_ascent;
16818 it->max_phys_descent = phys_descent;
16821 break;
16823 else if (new_x > it->first_visible_x)
16825 /* Increment number of glyphs actually displayed. */
16826 ++it->hpos;
16828 if (x < it->first_visible_x)
16829 /* Glyph is partially visible, i.e. row starts at
16830 negative X position. */
16831 row->x = x - it->first_visible_x;
16833 else
16835 /* Glyph is completely off the left margin of the
16836 window. This should not happen because of the
16837 move_it_in_display_line at the start of this
16838 function, unless the text display area of the
16839 window is empty. */
16840 xassert (it->first_visible_x <= it->last_visible_x);
16844 row->ascent = max (row->ascent, it->max_ascent);
16845 row->height = max (row->height, it->max_ascent + it->max_descent);
16846 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16847 row->phys_height = max (row->phys_height,
16848 it->max_phys_ascent + it->max_phys_descent);
16849 row->extra_line_spacing = max (row->extra_line_spacing,
16850 it->max_extra_line_spacing);
16852 /* End of this display line if row is continued. */
16853 if (row->continued_p || row->ends_at_zv_p)
16854 break;
16857 at_end_of_line:
16858 /* Is this a line end? If yes, we're also done, after making
16859 sure that a non-default face is extended up to the right
16860 margin of the window. */
16861 if (ITERATOR_AT_END_OF_LINE_P (it))
16863 int used_before = row->used[TEXT_AREA];
16865 row->ends_in_newline_from_string_p = STRINGP (it->object);
16867 /* Add a space at the end of the line that is used to
16868 display the cursor there. */
16869 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16870 append_space_for_newline (it, 0);
16872 /* Extend the face to the end of the line. */
16873 extend_face_to_end_of_line (it);
16875 /* Make sure we have the position. */
16876 if (used_before == 0)
16877 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16879 /* Consume the line end. This skips over invisible lines. */
16880 set_iterator_to_next (it, 1);
16881 it->continuation_lines_width = 0;
16882 break;
16885 /* Proceed with next display element. Note that this skips
16886 over lines invisible because of selective display. */
16887 set_iterator_to_next (it, 1);
16889 /* If we truncate lines, we are done when the last displayed
16890 glyphs reach past the right margin of the window. */
16891 if (it->line_wrap == TRUNCATE
16892 && (FRAME_WINDOW_P (it->f)
16893 ? (it->current_x >= it->last_visible_x)
16894 : (it->current_x > it->last_visible_x)))
16896 /* Maybe add truncation glyphs. */
16897 if (!FRAME_WINDOW_P (it->f))
16899 int i, n;
16901 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16902 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16903 break;
16905 for (n = row->used[TEXT_AREA]; i < n; ++i)
16907 row->used[TEXT_AREA] = i;
16908 produce_special_glyphs (it, IT_TRUNCATION);
16911 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16913 /* Don't truncate if we can overflow newline into fringe. */
16914 if (!get_next_display_element (it))
16916 it->continuation_lines_width = 0;
16917 row->ends_at_zv_p = 1;
16918 row->exact_window_width_line_p = 1;
16919 break;
16921 if (ITERATOR_AT_END_OF_LINE_P (it))
16923 row->exact_window_width_line_p = 1;
16924 goto at_end_of_line;
16928 row->truncated_on_right_p = 1;
16929 it->continuation_lines_width = 0;
16930 reseat_at_next_visible_line_start (it, 0);
16931 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16932 it->hpos = hpos_before;
16933 it->current_x = x_before;
16934 break;
16938 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16939 at the left window margin. */
16940 if (it->first_visible_x
16941 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16943 if (!FRAME_WINDOW_P (it->f))
16944 insert_left_trunc_glyphs (it);
16945 row->truncated_on_left_p = 1;
16948 /* If the start of this line is the overlay arrow-position, then
16949 mark this glyph row as the one containing the overlay arrow.
16950 This is clearly a mess with variable size fonts. It would be
16951 better to let it be displayed like cursors under X. */
16952 if ((row->displays_text_p || !overlay_arrow_seen)
16953 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16954 !NILP (overlay_arrow_string)))
16956 /* Overlay arrow in window redisplay is a fringe bitmap. */
16957 if (STRINGP (overlay_arrow_string))
16959 struct glyph_row *arrow_row
16960 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16961 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16962 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16963 struct glyph *p = row->glyphs[TEXT_AREA];
16964 struct glyph *p2, *end;
16966 /* Copy the arrow glyphs. */
16967 while (glyph < arrow_end)
16968 *p++ = *glyph++;
16970 /* Throw away padding glyphs. */
16971 p2 = p;
16972 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16973 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16974 ++p2;
16975 if (p2 > p)
16977 while (p2 < end)
16978 *p++ = *p2++;
16979 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16982 else
16984 xassert (INTEGERP (overlay_arrow_string));
16985 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16987 overlay_arrow_seen = 1;
16990 /* Compute pixel dimensions of this line. */
16991 compute_line_metrics (it);
16993 /* Remember the position at which this line ends. */
16994 row->end = it->current;
16996 /* Record whether this row ends inside an ellipsis. */
16997 row->ends_in_ellipsis_p
16998 = (it->method == GET_FROM_DISPLAY_VECTOR
16999 && it->ellipsis_p);
17001 /* Save fringe bitmaps in this row. */
17002 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
17003 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
17004 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
17005 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
17007 it->left_user_fringe_bitmap = 0;
17008 it->left_user_fringe_face_id = 0;
17009 it->right_user_fringe_bitmap = 0;
17010 it->right_user_fringe_face_id = 0;
17012 /* Maybe set the cursor. */
17013 if (it->w->cursor.vpos < 0
17014 && PT >= MATRIX_ROW_START_CHARPOS (row)
17015 && PT <= MATRIX_ROW_END_CHARPOS (row)
17016 && cursor_row_p (it->w, row))
17017 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17019 /* Highlight trailing whitespace. */
17020 if (!NILP (Vshow_trailing_whitespace))
17021 highlight_trailing_whitespace (it->f, it->glyph_row);
17023 /* Prepare for the next line. This line starts horizontally at (X
17024 HPOS) = (0 0). Vertical positions are incremented. As a
17025 convenience for the caller, IT->glyph_row is set to the next
17026 row to be used. */
17027 it->current_x = it->hpos = 0;
17028 it->current_y += row->height;
17029 ++it->vpos;
17030 ++it->glyph_row;
17031 it->start = it->current;
17032 return row->displays_text_p;
17037 /***********************************************************************
17038 Menu Bar
17039 ***********************************************************************/
17041 /* Redisplay the menu bar in the frame for window W.
17043 The menu bar of X frames that don't have X toolkit support is
17044 displayed in a special window W->frame->menu_bar_window.
17046 The menu bar of terminal frames is treated specially as far as
17047 glyph matrices are concerned. Menu bar lines are not part of
17048 windows, so the update is done directly on the frame matrix rows
17049 for the menu bar. */
17051 static void
17052 display_menu_bar (w)
17053 struct window *w;
17055 struct frame *f = XFRAME (WINDOW_FRAME (w));
17056 struct it it;
17057 Lisp_Object items;
17058 int i;
17060 /* Don't do all this for graphical frames. */
17061 #ifdef HAVE_NTGUI
17062 if (FRAME_W32_P (f))
17063 return;
17064 #endif
17065 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17066 if (FRAME_X_P (f))
17067 return;
17068 #endif
17070 #ifdef HAVE_NS
17071 if (FRAME_NS_P (f))
17072 return;
17073 #endif /* HAVE_NS */
17075 #ifdef USE_X_TOOLKIT
17076 xassert (!FRAME_WINDOW_P (f));
17077 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17078 it.first_visible_x = 0;
17079 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17080 #else /* not USE_X_TOOLKIT */
17081 if (FRAME_WINDOW_P (f))
17083 /* Menu bar lines are displayed in the desired matrix of the
17084 dummy window menu_bar_window. */
17085 struct window *menu_w;
17086 xassert (WINDOWP (f->menu_bar_window));
17087 menu_w = XWINDOW (f->menu_bar_window);
17088 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17089 MENU_FACE_ID);
17090 it.first_visible_x = 0;
17091 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17093 else
17095 /* This is a TTY frame, i.e. character hpos/vpos are used as
17096 pixel x/y. */
17097 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17098 MENU_FACE_ID);
17099 it.first_visible_x = 0;
17100 it.last_visible_x = FRAME_COLS (f);
17102 #endif /* not USE_X_TOOLKIT */
17104 if (! mode_line_inverse_video)
17105 /* Force the menu-bar to be displayed in the default face. */
17106 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17108 /* Clear all rows of the menu bar. */
17109 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17111 struct glyph_row *row = it.glyph_row + i;
17112 clear_glyph_row (row);
17113 row->enabled_p = 1;
17114 row->full_width_p = 1;
17117 /* Display all items of the menu bar. */
17118 items = FRAME_MENU_BAR_ITEMS (it.f);
17119 for (i = 0; i < XVECTOR (items)->size; i += 4)
17121 Lisp_Object string;
17123 /* Stop at nil string. */
17124 string = AREF (items, i + 1);
17125 if (NILP (string))
17126 break;
17128 /* Remember where item was displayed. */
17129 ASET (items, i + 3, make_number (it.hpos));
17131 /* Display the item, pad with one space. */
17132 if (it.current_x < it.last_visible_x)
17133 display_string (NULL, string, Qnil, 0, 0, &it,
17134 SCHARS (string) + 1, 0, 0, -1);
17137 /* Fill out the line with spaces. */
17138 if (it.current_x < it.last_visible_x)
17139 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17141 /* Compute the total height of the lines. */
17142 compute_line_metrics (&it);
17147 /***********************************************************************
17148 Mode Line
17149 ***********************************************************************/
17151 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17152 FORCE is non-zero, redisplay mode lines unconditionally.
17153 Otherwise, redisplay only mode lines that are garbaged. Value is
17154 the number of windows whose mode lines were redisplayed. */
17156 static int
17157 redisplay_mode_lines (window, force)
17158 Lisp_Object window;
17159 int force;
17161 int nwindows = 0;
17163 while (!NILP (window))
17165 struct window *w = XWINDOW (window);
17167 if (WINDOWP (w->hchild))
17168 nwindows += redisplay_mode_lines (w->hchild, force);
17169 else if (WINDOWP (w->vchild))
17170 nwindows += redisplay_mode_lines (w->vchild, force);
17171 else if (force
17172 || FRAME_GARBAGED_P (XFRAME (w->frame))
17173 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17175 struct text_pos lpoint;
17176 struct buffer *old = current_buffer;
17178 /* Set the window's buffer for the mode line display. */
17179 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17180 set_buffer_internal_1 (XBUFFER (w->buffer));
17182 /* Point refers normally to the selected window. For any
17183 other window, set up appropriate value. */
17184 if (!EQ (window, selected_window))
17186 struct text_pos pt;
17188 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17189 if (CHARPOS (pt) < BEGV)
17190 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17191 else if (CHARPOS (pt) > (ZV - 1))
17192 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17193 else
17194 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17197 /* Display mode lines. */
17198 clear_glyph_matrix (w->desired_matrix);
17199 if (display_mode_lines (w))
17201 ++nwindows;
17202 w->must_be_updated_p = 1;
17205 /* Restore old settings. */
17206 set_buffer_internal_1 (old);
17207 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17210 window = w->next;
17213 return nwindows;
17217 /* Display the mode and/or header line of window W. Value is the
17218 sum number of mode lines and header lines displayed. */
17220 static int
17221 display_mode_lines (w)
17222 struct window *w;
17224 Lisp_Object old_selected_window, old_selected_frame;
17225 int n = 0;
17227 old_selected_frame = selected_frame;
17228 selected_frame = w->frame;
17229 old_selected_window = selected_window;
17230 XSETWINDOW (selected_window, w);
17232 /* These will be set while the mode line specs are processed. */
17233 line_number_displayed = 0;
17234 w->column_number_displayed = Qnil;
17236 if (WINDOW_WANTS_MODELINE_P (w))
17238 struct window *sel_w = XWINDOW (old_selected_window);
17240 /* Select mode line face based on the real selected window. */
17241 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17242 current_buffer->mode_line_format);
17243 ++n;
17246 if (WINDOW_WANTS_HEADER_LINE_P (w))
17248 display_mode_line (w, HEADER_LINE_FACE_ID,
17249 current_buffer->header_line_format);
17250 ++n;
17253 selected_frame = old_selected_frame;
17254 selected_window = old_selected_window;
17255 return n;
17259 /* Display mode or header line of window W. FACE_ID specifies which
17260 line to display; it is either MODE_LINE_FACE_ID or
17261 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17262 display. Value is the pixel height of the mode/header line
17263 displayed. */
17265 static int
17266 display_mode_line (w, face_id, format)
17267 struct window *w;
17268 enum face_id face_id;
17269 Lisp_Object format;
17271 struct it it;
17272 struct face *face;
17273 int count = SPECPDL_INDEX ();
17275 init_iterator (&it, w, -1, -1, NULL, face_id);
17276 /* Don't extend on a previously drawn mode-line.
17277 This may happen if called from pos_visible_p. */
17278 it.glyph_row->enabled_p = 0;
17279 prepare_desired_row (it.glyph_row);
17281 it.glyph_row->mode_line_p = 1;
17283 if (! mode_line_inverse_video)
17284 /* Force the mode-line to be displayed in the default face. */
17285 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17287 record_unwind_protect (unwind_format_mode_line,
17288 format_mode_line_unwind_data (NULL, Qnil, 0));
17290 mode_line_target = MODE_LINE_DISPLAY;
17292 /* Temporarily make frame's keyboard the current kboard so that
17293 kboard-local variables in the mode_line_format will get the right
17294 values. */
17295 push_kboard (FRAME_KBOARD (it.f));
17296 record_unwind_save_match_data ();
17297 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17298 pop_kboard ();
17300 unbind_to (count, Qnil);
17302 /* Fill up with spaces. */
17303 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17305 compute_line_metrics (&it);
17306 it.glyph_row->full_width_p = 1;
17307 it.glyph_row->continued_p = 0;
17308 it.glyph_row->truncated_on_left_p = 0;
17309 it.glyph_row->truncated_on_right_p = 0;
17311 /* Make a 3D mode-line have a shadow at its right end. */
17312 face = FACE_FROM_ID (it.f, face_id);
17313 extend_face_to_end_of_line (&it);
17314 if (face->box != FACE_NO_BOX)
17316 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17317 + it.glyph_row->used[TEXT_AREA] - 1);
17318 last->right_box_line_p = 1;
17321 return it.glyph_row->height;
17324 /* Move element ELT in LIST to the front of LIST.
17325 Return the updated list. */
17327 static Lisp_Object
17328 move_elt_to_front (elt, list)
17329 Lisp_Object elt, list;
17331 register Lisp_Object tail, prev;
17332 register Lisp_Object tem;
17334 tail = list;
17335 prev = Qnil;
17336 while (CONSP (tail))
17338 tem = XCAR (tail);
17340 if (EQ (elt, tem))
17342 /* Splice out the link TAIL. */
17343 if (NILP (prev))
17344 list = XCDR (tail);
17345 else
17346 Fsetcdr (prev, XCDR (tail));
17348 /* Now make it the first. */
17349 Fsetcdr (tail, list);
17350 return tail;
17352 else
17353 prev = tail;
17354 tail = XCDR (tail);
17355 QUIT;
17358 /* Not found--return unchanged LIST. */
17359 return list;
17362 /* Contribute ELT to the mode line for window IT->w. How it
17363 translates into text depends on its data type.
17365 IT describes the display environment in which we display, as usual.
17367 DEPTH is the depth in recursion. It is used to prevent
17368 infinite recursion here.
17370 FIELD_WIDTH is the number of characters the display of ELT should
17371 occupy in the mode line, and PRECISION is the maximum number of
17372 characters to display from ELT's representation. See
17373 display_string for details.
17375 Returns the hpos of the end of the text generated by ELT.
17377 PROPS is a property list to add to any string we encounter.
17379 If RISKY is nonzero, remove (disregard) any properties in any string
17380 we encounter, and ignore :eval and :propertize.
17382 The global variable `mode_line_target' determines whether the
17383 output is passed to `store_mode_line_noprop',
17384 `store_mode_line_string', or `display_string'. */
17386 static int
17387 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17388 struct it *it;
17389 int depth;
17390 int field_width, precision;
17391 Lisp_Object elt, props;
17392 int risky;
17394 int n = 0, field, prec;
17395 int literal = 0;
17397 tail_recurse:
17398 if (depth > 100)
17399 elt = build_string ("*too-deep*");
17401 depth++;
17403 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17405 case Lisp_String:
17407 /* A string: output it and check for %-constructs within it. */
17408 unsigned char c;
17409 int offset = 0;
17411 if (SCHARS (elt) > 0
17412 && (!NILP (props) || risky))
17414 Lisp_Object oprops, aelt;
17415 oprops = Ftext_properties_at (make_number (0), elt);
17417 /* If the starting string's properties are not what
17418 we want, translate the string. Also, if the string
17419 is risky, do that anyway. */
17421 if (NILP (Fequal (props, oprops)) || risky)
17423 /* If the starting string has properties,
17424 merge the specified ones onto the existing ones. */
17425 if (! NILP (oprops) && !risky)
17427 Lisp_Object tem;
17429 oprops = Fcopy_sequence (oprops);
17430 tem = props;
17431 while (CONSP (tem))
17433 oprops = Fplist_put (oprops, XCAR (tem),
17434 XCAR (XCDR (tem)));
17435 tem = XCDR (XCDR (tem));
17437 props = oprops;
17440 aelt = Fassoc (elt, mode_line_proptrans_alist);
17441 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17443 /* AELT is what we want. Move it to the front
17444 without consing. */
17445 elt = XCAR (aelt);
17446 mode_line_proptrans_alist
17447 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17449 else
17451 Lisp_Object tem;
17453 /* If AELT has the wrong props, it is useless.
17454 so get rid of it. */
17455 if (! NILP (aelt))
17456 mode_line_proptrans_alist
17457 = Fdelq (aelt, mode_line_proptrans_alist);
17459 elt = Fcopy_sequence (elt);
17460 Fset_text_properties (make_number (0), Flength (elt),
17461 props, elt);
17462 /* Add this item to mode_line_proptrans_alist. */
17463 mode_line_proptrans_alist
17464 = Fcons (Fcons (elt, props),
17465 mode_line_proptrans_alist);
17466 /* Truncate mode_line_proptrans_alist
17467 to at most 50 elements. */
17468 tem = Fnthcdr (make_number (50),
17469 mode_line_proptrans_alist);
17470 if (! NILP (tem))
17471 XSETCDR (tem, Qnil);
17476 offset = 0;
17478 if (literal)
17480 prec = precision - n;
17481 switch (mode_line_target)
17483 case MODE_LINE_NOPROP:
17484 case MODE_LINE_TITLE:
17485 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17486 break;
17487 case MODE_LINE_STRING:
17488 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17489 break;
17490 case MODE_LINE_DISPLAY:
17491 n += display_string (NULL, elt, Qnil, 0, 0, it,
17492 0, prec, 0, STRING_MULTIBYTE (elt));
17493 break;
17496 break;
17499 /* Handle the non-literal case. */
17501 while ((precision <= 0 || n < precision)
17502 && SREF (elt, offset) != 0
17503 && (mode_line_target != MODE_LINE_DISPLAY
17504 || it->current_x < it->last_visible_x))
17506 int last_offset = offset;
17508 /* Advance to end of string or next format specifier. */
17509 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17512 if (offset - 1 != last_offset)
17514 int nchars, nbytes;
17516 /* Output to end of string or up to '%'. Field width
17517 is length of string. Don't output more than
17518 PRECISION allows us. */
17519 offset--;
17521 prec = c_string_width (SDATA (elt) + last_offset,
17522 offset - last_offset, precision - n,
17523 &nchars, &nbytes);
17525 switch (mode_line_target)
17527 case MODE_LINE_NOPROP:
17528 case MODE_LINE_TITLE:
17529 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17530 break;
17531 case MODE_LINE_STRING:
17533 int bytepos = last_offset;
17534 int charpos = string_byte_to_char (elt, bytepos);
17535 int endpos = (precision <= 0
17536 ? string_byte_to_char (elt, offset)
17537 : charpos + nchars);
17539 n += store_mode_line_string (NULL,
17540 Fsubstring (elt, make_number (charpos),
17541 make_number (endpos)),
17542 0, 0, 0, Qnil);
17544 break;
17545 case MODE_LINE_DISPLAY:
17547 int bytepos = last_offset;
17548 int charpos = string_byte_to_char (elt, bytepos);
17550 if (precision <= 0)
17551 nchars = string_byte_to_char (elt, offset) - charpos;
17552 n += display_string (NULL, elt, Qnil, 0, charpos,
17553 it, 0, nchars, 0,
17554 STRING_MULTIBYTE (elt));
17556 break;
17559 else /* c == '%' */
17561 int percent_position = offset;
17563 /* Get the specified minimum width. Zero means
17564 don't pad. */
17565 field = 0;
17566 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17567 field = field * 10 + c - '0';
17569 /* Don't pad beyond the total padding allowed. */
17570 if (field_width - n > 0 && field > field_width - n)
17571 field = field_width - n;
17573 /* Note that either PRECISION <= 0 or N < PRECISION. */
17574 prec = precision - n;
17576 if (c == 'M')
17577 n += display_mode_element (it, depth, field, prec,
17578 Vglobal_mode_string, props,
17579 risky);
17580 else if (c != 0)
17582 int multibyte;
17583 int bytepos, charpos;
17584 unsigned char *spec;
17586 bytepos = percent_position;
17587 charpos = (STRING_MULTIBYTE (elt)
17588 ? string_byte_to_char (elt, bytepos)
17589 : bytepos);
17590 spec
17591 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17593 switch (mode_line_target)
17595 case MODE_LINE_NOPROP:
17596 case MODE_LINE_TITLE:
17597 n += store_mode_line_noprop (spec, field, prec);
17598 break;
17599 case MODE_LINE_STRING:
17601 int len = strlen (spec);
17602 Lisp_Object tem = make_string (spec, len);
17603 props = Ftext_properties_at (make_number (charpos), elt);
17604 /* Should only keep face property in props */
17605 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17607 break;
17608 case MODE_LINE_DISPLAY:
17610 int nglyphs_before, nwritten;
17612 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17613 nwritten = display_string (spec, Qnil, elt,
17614 charpos, 0, it,
17615 field, prec, 0,
17616 multibyte);
17618 /* Assign to the glyphs written above the
17619 string where the `%x' came from, position
17620 of the `%'. */
17621 if (nwritten > 0)
17623 struct glyph *glyph
17624 = (it->glyph_row->glyphs[TEXT_AREA]
17625 + nglyphs_before);
17626 int i;
17628 for (i = 0; i < nwritten; ++i)
17630 glyph[i].object = elt;
17631 glyph[i].charpos = charpos;
17634 n += nwritten;
17637 break;
17640 else /* c == 0 */
17641 break;
17645 break;
17647 case Lisp_Symbol:
17648 /* A symbol: process the value of the symbol recursively
17649 as if it appeared here directly. Avoid error if symbol void.
17650 Special case: if value of symbol is a string, output the string
17651 literally. */
17653 register Lisp_Object tem;
17655 /* If the variable is not marked as risky to set
17656 then its contents are risky to use. */
17657 if (NILP (Fget (elt, Qrisky_local_variable)))
17658 risky = 1;
17660 tem = Fboundp (elt);
17661 if (!NILP (tem))
17663 tem = Fsymbol_value (elt);
17664 /* If value is a string, output that string literally:
17665 don't check for % within it. */
17666 if (STRINGP (tem))
17667 literal = 1;
17669 if (!EQ (tem, elt))
17671 /* Give up right away for nil or t. */
17672 elt = tem;
17673 goto tail_recurse;
17677 break;
17679 case Lisp_Cons:
17681 register Lisp_Object car, tem;
17683 /* A cons cell: five distinct cases.
17684 If first element is :eval or :propertize, do something special.
17685 If first element is a string or a cons, process all the elements
17686 and effectively concatenate them.
17687 If first element is a negative number, truncate displaying cdr to
17688 at most that many characters. If positive, pad (with spaces)
17689 to at least that many characters.
17690 If first element is a symbol, process the cadr or caddr recursively
17691 according to whether the symbol's value is non-nil or nil. */
17692 car = XCAR (elt);
17693 if (EQ (car, QCeval))
17695 /* An element of the form (:eval FORM) means evaluate FORM
17696 and use the result as mode line elements. */
17698 if (risky)
17699 break;
17701 if (CONSP (XCDR (elt)))
17703 Lisp_Object spec;
17704 spec = safe_eval (XCAR (XCDR (elt)));
17705 n += display_mode_element (it, depth, field_width - n,
17706 precision - n, spec, props,
17707 risky);
17710 else if (EQ (car, QCpropertize))
17712 /* An element of the form (:propertize ELT PROPS...)
17713 means display ELT but applying properties PROPS. */
17715 if (risky)
17716 break;
17718 if (CONSP (XCDR (elt)))
17719 n += display_mode_element (it, depth, field_width - n,
17720 precision - n, XCAR (XCDR (elt)),
17721 XCDR (XCDR (elt)), risky);
17723 else if (SYMBOLP (car))
17725 tem = Fboundp (car);
17726 elt = XCDR (elt);
17727 if (!CONSP (elt))
17728 goto invalid;
17729 /* elt is now the cdr, and we know it is a cons cell.
17730 Use its car if CAR has a non-nil value. */
17731 if (!NILP (tem))
17733 tem = Fsymbol_value (car);
17734 if (!NILP (tem))
17736 elt = XCAR (elt);
17737 goto tail_recurse;
17740 /* Symbol's value is nil (or symbol is unbound)
17741 Get the cddr of the original list
17742 and if possible find the caddr and use that. */
17743 elt = XCDR (elt);
17744 if (NILP (elt))
17745 break;
17746 else if (!CONSP (elt))
17747 goto invalid;
17748 elt = XCAR (elt);
17749 goto tail_recurse;
17751 else if (INTEGERP (car))
17753 register int lim = XINT (car);
17754 elt = XCDR (elt);
17755 if (lim < 0)
17757 /* Negative int means reduce maximum width. */
17758 if (precision <= 0)
17759 precision = -lim;
17760 else
17761 precision = min (precision, -lim);
17763 else if (lim > 0)
17765 /* Padding specified. Don't let it be more than
17766 current maximum. */
17767 if (precision > 0)
17768 lim = min (precision, lim);
17770 /* If that's more padding than already wanted, queue it.
17771 But don't reduce padding already specified even if
17772 that is beyond the current truncation point. */
17773 field_width = max (lim, field_width);
17775 goto tail_recurse;
17777 else if (STRINGP (car) || CONSP (car))
17779 Lisp_Object halftail = elt;
17780 int len = 0;
17782 while (CONSP (elt)
17783 && (precision <= 0 || n < precision))
17785 n += display_mode_element (it, depth,
17786 /* Do padding only after the last
17787 element in the list. */
17788 (! CONSP (XCDR (elt))
17789 ? field_width - n
17790 : 0),
17791 precision - n, XCAR (elt),
17792 props, risky);
17793 elt = XCDR (elt);
17794 len++;
17795 if ((len & 1) == 0)
17796 halftail = XCDR (halftail);
17797 /* Check for cycle. */
17798 if (EQ (halftail, elt))
17799 break;
17803 break;
17805 default:
17806 invalid:
17807 elt = build_string ("*invalid*");
17808 goto tail_recurse;
17811 /* Pad to FIELD_WIDTH. */
17812 if (field_width > 0 && n < field_width)
17814 switch (mode_line_target)
17816 case MODE_LINE_NOPROP:
17817 case MODE_LINE_TITLE:
17818 n += store_mode_line_noprop ("", field_width - n, 0);
17819 break;
17820 case MODE_LINE_STRING:
17821 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17822 break;
17823 case MODE_LINE_DISPLAY:
17824 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17825 0, 0, 0);
17826 break;
17830 return n;
17833 /* Store a mode-line string element in mode_line_string_list.
17835 If STRING is non-null, display that C string. Otherwise, the Lisp
17836 string LISP_STRING is displayed.
17838 FIELD_WIDTH is the minimum number of output glyphs to produce.
17839 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17840 with spaces. FIELD_WIDTH <= 0 means don't pad.
17842 PRECISION is the maximum number of characters to output from
17843 STRING. PRECISION <= 0 means don't truncate the string.
17845 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17846 properties to the string.
17848 PROPS are the properties to add to the string.
17849 The mode_line_string_face face property is always added to the string.
17852 static int
17853 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17854 char *string;
17855 Lisp_Object lisp_string;
17856 int copy_string;
17857 int field_width;
17858 int precision;
17859 Lisp_Object props;
17861 int len;
17862 int n = 0;
17864 if (string != NULL)
17866 len = strlen (string);
17867 if (precision > 0 && len > precision)
17868 len = precision;
17869 lisp_string = make_string (string, len);
17870 if (NILP (props))
17871 props = mode_line_string_face_prop;
17872 else if (!NILP (mode_line_string_face))
17874 Lisp_Object face = Fplist_get (props, Qface);
17875 props = Fcopy_sequence (props);
17876 if (NILP (face))
17877 face = mode_line_string_face;
17878 else
17879 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17880 props = Fplist_put (props, Qface, face);
17882 Fadd_text_properties (make_number (0), make_number (len),
17883 props, lisp_string);
17885 else
17887 len = XFASTINT (Flength (lisp_string));
17888 if (precision > 0 && len > precision)
17890 len = precision;
17891 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17892 precision = -1;
17894 if (!NILP (mode_line_string_face))
17896 Lisp_Object face;
17897 if (NILP (props))
17898 props = Ftext_properties_at (make_number (0), lisp_string);
17899 face = Fplist_get (props, Qface);
17900 if (NILP (face))
17901 face = mode_line_string_face;
17902 else
17903 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17904 props = Fcons (Qface, Fcons (face, Qnil));
17905 if (copy_string)
17906 lisp_string = Fcopy_sequence (lisp_string);
17908 if (!NILP (props))
17909 Fadd_text_properties (make_number (0), make_number (len),
17910 props, lisp_string);
17913 if (len > 0)
17915 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17916 n += len;
17919 if (field_width > len)
17921 field_width -= len;
17922 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17923 if (!NILP (props))
17924 Fadd_text_properties (make_number (0), make_number (field_width),
17925 props, lisp_string);
17926 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17927 n += field_width;
17930 return n;
17934 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17935 1, 4, 0,
17936 doc: /* Format a string out of a mode line format specification.
17937 First arg FORMAT specifies the mode line format (see `mode-line-format'
17938 for details) to use.
17940 Optional second arg FACE specifies the face property to put
17941 on all characters for which no face is specified.
17942 The value t means whatever face the window's mode line currently uses
17943 \(either `mode-line' or `mode-line-inactive', depending).
17944 A value of nil means the default is no face property.
17945 If FACE is an integer, the value string has no text properties.
17947 Optional third and fourth args WINDOW and BUFFER specify the window
17948 and buffer to use as the context for the formatting (defaults
17949 are the selected window and the window's buffer). */)
17950 (format, face, window, buffer)
17951 Lisp_Object format, face, window, buffer;
17953 struct it it;
17954 int len;
17955 struct window *w;
17956 struct buffer *old_buffer = NULL;
17957 int face_id = -1;
17958 int no_props = INTEGERP (face);
17959 int count = SPECPDL_INDEX ();
17960 Lisp_Object str;
17961 int string_start = 0;
17963 if (NILP (window))
17964 window = selected_window;
17965 CHECK_WINDOW (window);
17966 w = XWINDOW (window);
17968 if (NILP (buffer))
17969 buffer = w->buffer;
17970 CHECK_BUFFER (buffer);
17972 /* Make formatting the modeline a non-op when noninteractive, otherwise
17973 there will be problems later caused by a partially initialized frame. */
17974 if (NILP (format) || noninteractive)
17975 return empty_unibyte_string;
17977 if (no_props)
17978 face = Qnil;
17980 if (!NILP (face))
17982 if (EQ (face, Qt))
17983 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17984 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17987 if (face_id < 0)
17988 face_id = DEFAULT_FACE_ID;
17990 if (XBUFFER (buffer) != current_buffer)
17991 old_buffer = current_buffer;
17993 /* Save things including mode_line_proptrans_alist,
17994 and set that to nil so that we don't alter the outer value. */
17995 record_unwind_protect (unwind_format_mode_line,
17996 format_mode_line_unwind_data
17997 (old_buffer, selected_window, 1));
17998 mode_line_proptrans_alist = Qnil;
18000 Fselect_window (window, Qt);
18001 if (old_buffer)
18002 set_buffer_internal_1 (XBUFFER (buffer));
18004 init_iterator (&it, w, -1, -1, NULL, face_id);
18006 if (no_props)
18008 mode_line_target = MODE_LINE_NOPROP;
18009 mode_line_string_face_prop = Qnil;
18010 mode_line_string_list = Qnil;
18011 string_start = MODE_LINE_NOPROP_LEN (0);
18013 else
18015 mode_line_target = MODE_LINE_STRING;
18016 mode_line_string_list = Qnil;
18017 mode_line_string_face = face;
18018 mode_line_string_face_prop
18019 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18022 push_kboard (FRAME_KBOARD (it.f));
18023 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18024 pop_kboard ();
18026 if (no_props)
18028 len = MODE_LINE_NOPROP_LEN (string_start);
18029 str = make_string (mode_line_noprop_buf + string_start, len);
18031 else
18033 mode_line_string_list = Fnreverse (mode_line_string_list);
18034 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18035 empty_unibyte_string);
18038 unbind_to (count, Qnil);
18039 return str;
18042 /* Write a null-terminated, right justified decimal representation of
18043 the positive integer D to BUF using a minimal field width WIDTH. */
18045 static void
18046 pint2str (buf, width, d)
18047 register char *buf;
18048 register int width;
18049 register int d;
18051 register char *p = buf;
18053 if (d <= 0)
18054 *p++ = '0';
18055 else
18057 while (d > 0)
18059 *p++ = d % 10 + '0';
18060 d /= 10;
18064 for (width -= (int) (p - buf); width > 0; --width)
18065 *p++ = ' ';
18066 *p-- = '\0';
18067 while (p > buf)
18069 d = *buf;
18070 *buf++ = *p;
18071 *p-- = d;
18075 /* Write a null-terminated, right justified decimal and "human
18076 readable" representation of the nonnegative integer D to BUF using
18077 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18079 static const char power_letter[] =
18081 0, /* not used */
18082 'k', /* kilo */
18083 'M', /* mega */
18084 'G', /* giga */
18085 'T', /* tera */
18086 'P', /* peta */
18087 'E', /* exa */
18088 'Z', /* zetta */
18089 'Y' /* yotta */
18092 static void
18093 pint2hrstr (buf, width, d)
18094 char *buf;
18095 int width;
18096 int d;
18098 /* We aim to represent the nonnegative integer D as
18099 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18100 int quotient = d;
18101 int remainder = 0;
18102 /* -1 means: do not use TENTHS. */
18103 int tenths = -1;
18104 int exponent = 0;
18106 /* Length of QUOTIENT.TENTHS as a string. */
18107 int length;
18109 char * psuffix;
18110 char * p;
18112 if (1000 <= quotient)
18114 /* Scale to the appropriate EXPONENT. */
18117 remainder = quotient % 1000;
18118 quotient /= 1000;
18119 exponent++;
18121 while (1000 <= quotient);
18123 /* Round to nearest and decide whether to use TENTHS or not. */
18124 if (quotient <= 9)
18126 tenths = remainder / 100;
18127 if (50 <= remainder % 100)
18129 if (tenths < 9)
18130 tenths++;
18131 else
18133 quotient++;
18134 if (quotient == 10)
18135 tenths = -1;
18136 else
18137 tenths = 0;
18141 else
18142 if (500 <= remainder)
18144 if (quotient < 999)
18145 quotient++;
18146 else
18148 quotient = 1;
18149 exponent++;
18150 tenths = 0;
18155 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18156 if (tenths == -1 && quotient <= 99)
18157 if (quotient <= 9)
18158 length = 1;
18159 else
18160 length = 2;
18161 else
18162 length = 3;
18163 p = psuffix = buf + max (width, length);
18165 /* Print EXPONENT. */
18166 if (exponent)
18167 *psuffix++ = power_letter[exponent];
18168 *psuffix = '\0';
18170 /* Print TENTHS. */
18171 if (tenths >= 0)
18173 *--p = '0' + tenths;
18174 *--p = '.';
18177 /* Print QUOTIENT. */
18180 int digit = quotient % 10;
18181 *--p = '0' + digit;
18183 while ((quotient /= 10) != 0);
18185 /* Print leading spaces. */
18186 while (buf < p)
18187 *--p = ' ';
18190 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18191 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18192 type of CODING_SYSTEM. Return updated pointer into BUF. */
18194 static unsigned char invalid_eol_type[] = "(*invalid*)";
18196 static char *
18197 decode_mode_spec_coding (coding_system, buf, eol_flag)
18198 Lisp_Object coding_system;
18199 register char *buf;
18200 int eol_flag;
18202 Lisp_Object val;
18203 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18204 const unsigned char *eol_str;
18205 int eol_str_len;
18206 /* The EOL conversion we are using. */
18207 Lisp_Object eoltype;
18209 val = CODING_SYSTEM_SPEC (coding_system);
18210 eoltype = Qnil;
18212 if (!VECTORP (val)) /* Not yet decided. */
18214 if (multibyte)
18215 *buf++ = '-';
18216 if (eol_flag)
18217 eoltype = eol_mnemonic_undecided;
18218 /* Don't mention EOL conversion if it isn't decided. */
18220 else
18222 Lisp_Object attrs;
18223 Lisp_Object eolvalue;
18225 attrs = AREF (val, 0);
18226 eolvalue = AREF (val, 2);
18228 if (multibyte)
18229 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18231 if (eol_flag)
18233 /* The EOL conversion that is normal on this system. */
18235 if (NILP (eolvalue)) /* Not yet decided. */
18236 eoltype = eol_mnemonic_undecided;
18237 else if (VECTORP (eolvalue)) /* Not yet decided. */
18238 eoltype = eol_mnemonic_undecided;
18239 else /* eolvalue is Qunix, Qdos, or Qmac. */
18240 eoltype = (EQ (eolvalue, Qunix)
18241 ? eol_mnemonic_unix
18242 : (EQ (eolvalue, Qdos) == 1
18243 ? eol_mnemonic_dos : eol_mnemonic_mac));
18247 if (eol_flag)
18249 /* Mention the EOL conversion if it is not the usual one. */
18250 if (STRINGP (eoltype))
18252 eol_str = SDATA (eoltype);
18253 eol_str_len = SBYTES (eoltype);
18255 else if (CHARACTERP (eoltype))
18257 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18258 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18259 eol_str = tmp;
18261 else
18263 eol_str = invalid_eol_type;
18264 eol_str_len = sizeof (invalid_eol_type) - 1;
18266 bcopy (eol_str, buf, eol_str_len);
18267 buf += eol_str_len;
18270 return buf;
18273 /* Return a string for the output of a mode line %-spec for window W,
18274 generated by character C. PRECISION >= 0 means don't return a
18275 string longer than that value. FIELD_WIDTH > 0 means pad the
18276 string returned with spaces to that value. Return 1 in *MULTIBYTE
18277 if the result is multibyte text.
18279 Note we operate on the current buffer for most purposes,
18280 the exception being w->base_line_pos. */
18282 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18284 static char *
18285 decode_mode_spec (w, c, field_width, precision, multibyte)
18286 struct window *w;
18287 register int c;
18288 int field_width, precision;
18289 int *multibyte;
18291 Lisp_Object obj;
18292 struct frame *f = XFRAME (WINDOW_FRAME (w));
18293 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18294 struct buffer *b = current_buffer;
18296 obj = Qnil;
18297 *multibyte = 0;
18299 switch (c)
18301 case '*':
18302 if (!NILP (b->read_only))
18303 return "%";
18304 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18305 return "*";
18306 return "-";
18308 case '+':
18309 /* This differs from %* only for a modified read-only buffer. */
18310 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18311 return "*";
18312 if (!NILP (b->read_only))
18313 return "%";
18314 return "-";
18316 case '&':
18317 /* This differs from %* in ignoring read-only-ness. */
18318 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18319 return "*";
18320 return "-";
18322 case '%':
18323 return "%";
18325 case '[':
18327 int i;
18328 char *p;
18330 if (command_loop_level > 5)
18331 return "[[[... ";
18332 p = decode_mode_spec_buf;
18333 for (i = 0; i < command_loop_level; i++)
18334 *p++ = '[';
18335 *p = 0;
18336 return decode_mode_spec_buf;
18339 case ']':
18341 int i;
18342 char *p;
18344 if (command_loop_level > 5)
18345 return " ...]]]";
18346 p = decode_mode_spec_buf;
18347 for (i = 0; i < command_loop_level; i++)
18348 *p++ = ']';
18349 *p = 0;
18350 return decode_mode_spec_buf;
18353 case '-':
18355 register int i;
18357 /* Let lots_of_dashes be a string of infinite length. */
18358 if (mode_line_target == MODE_LINE_NOPROP ||
18359 mode_line_target == MODE_LINE_STRING)
18360 return "--";
18361 if (field_width <= 0
18362 || field_width > sizeof (lots_of_dashes))
18364 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18365 decode_mode_spec_buf[i] = '-';
18366 decode_mode_spec_buf[i] = '\0';
18367 return decode_mode_spec_buf;
18369 else
18370 return lots_of_dashes;
18373 case 'b':
18374 obj = b->name;
18375 break;
18377 case 'c':
18378 /* %c and %l are ignored in `frame-title-format'.
18379 (In redisplay_internal, the frame title is drawn _before_ the
18380 windows are updated, so the stuff which depends on actual
18381 window contents (such as %l) may fail to render properly, or
18382 even crash emacs.) */
18383 if (mode_line_target == MODE_LINE_TITLE)
18384 return "";
18385 else
18387 int col = (int) current_column (); /* iftc */
18388 w->column_number_displayed = make_number (col);
18389 pint2str (decode_mode_spec_buf, field_width, col);
18390 return decode_mode_spec_buf;
18393 case 'e':
18394 #ifndef SYSTEM_MALLOC
18396 if (NILP (Vmemory_full))
18397 return "";
18398 else
18399 return "!MEM FULL! ";
18401 #else
18402 return "";
18403 #endif
18405 case 'F':
18406 /* %F displays the frame name. */
18407 if (!NILP (f->title))
18408 return (char *) SDATA (f->title);
18409 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18410 return (char *) SDATA (f->name);
18411 return "Emacs";
18413 case 'f':
18414 obj = b->filename;
18415 break;
18417 case 'i':
18419 int size = ZV - BEGV;
18420 pint2str (decode_mode_spec_buf, field_width, size);
18421 return decode_mode_spec_buf;
18424 case 'I':
18426 int size = ZV - BEGV;
18427 pint2hrstr (decode_mode_spec_buf, field_width, size);
18428 return decode_mode_spec_buf;
18431 case 'l':
18433 int startpos, startpos_byte, line, linepos, linepos_byte;
18434 int topline, nlines, junk, height;
18436 /* %c and %l are ignored in `frame-title-format'. */
18437 if (mode_line_target == MODE_LINE_TITLE)
18438 return "";
18440 startpos = XMARKER (w->start)->charpos;
18441 startpos_byte = marker_byte_position (w->start);
18442 height = WINDOW_TOTAL_LINES (w);
18444 /* If we decided that this buffer isn't suitable for line numbers,
18445 don't forget that too fast. */
18446 if (EQ (w->base_line_pos, w->buffer))
18447 goto no_value;
18448 /* But do forget it, if the window shows a different buffer now. */
18449 else if (BUFFERP (w->base_line_pos))
18450 w->base_line_pos = Qnil;
18452 /* If the buffer is very big, don't waste time. */
18453 if (INTEGERP (Vline_number_display_limit)
18454 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18456 w->base_line_pos = Qnil;
18457 w->base_line_number = Qnil;
18458 goto no_value;
18461 if (INTEGERP (w->base_line_number)
18462 && INTEGERP (w->base_line_pos)
18463 && XFASTINT (w->base_line_pos) <= startpos)
18465 line = XFASTINT (w->base_line_number);
18466 linepos = XFASTINT (w->base_line_pos);
18467 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18469 else
18471 line = 1;
18472 linepos = BUF_BEGV (b);
18473 linepos_byte = BUF_BEGV_BYTE (b);
18476 /* Count lines from base line to window start position. */
18477 nlines = display_count_lines (linepos, linepos_byte,
18478 startpos_byte,
18479 startpos, &junk);
18481 topline = nlines + line;
18483 /* Determine a new base line, if the old one is too close
18484 or too far away, or if we did not have one.
18485 "Too close" means it's plausible a scroll-down would
18486 go back past it. */
18487 if (startpos == BUF_BEGV (b))
18489 w->base_line_number = make_number (topline);
18490 w->base_line_pos = make_number (BUF_BEGV (b));
18492 else if (nlines < height + 25 || nlines > height * 3 + 50
18493 || linepos == BUF_BEGV (b))
18495 int limit = BUF_BEGV (b);
18496 int limit_byte = BUF_BEGV_BYTE (b);
18497 int position;
18498 int distance = (height * 2 + 30) * line_number_display_limit_width;
18500 if (startpos - distance > limit)
18502 limit = startpos - distance;
18503 limit_byte = CHAR_TO_BYTE (limit);
18506 nlines = display_count_lines (startpos, startpos_byte,
18507 limit_byte,
18508 - (height * 2 + 30),
18509 &position);
18510 /* If we couldn't find the lines we wanted within
18511 line_number_display_limit_width chars per line,
18512 give up on line numbers for this window. */
18513 if (position == limit_byte && limit == startpos - distance)
18515 w->base_line_pos = w->buffer;
18516 w->base_line_number = Qnil;
18517 goto no_value;
18520 w->base_line_number = make_number (topline - nlines);
18521 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18524 /* Now count lines from the start pos to point. */
18525 nlines = display_count_lines (startpos, startpos_byte,
18526 PT_BYTE, PT, &junk);
18528 /* Record that we did display the line number. */
18529 line_number_displayed = 1;
18531 /* Make the string to show. */
18532 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18533 return decode_mode_spec_buf;
18534 no_value:
18536 char* p = decode_mode_spec_buf;
18537 int pad = field_width - 2;
18538 while (pad-- > 0)
18539 *p++ = ' ';
18540 *p++ = '?';
18541 *p++ = '?';
18542 *p = '\0';
18543 return decode_mode_spec_buf;
18546 break;
18548 case 'm':
18549 obj = b->mode_name;
18550 break;
18552 case 'n':
18553 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18554 return " Narrow";
18555 break;
18557 case 'p':
18559 int pos = marker_position (w->start);
18560 int total = BUF_ZV (b) - BUF_BEGV (b);
18562 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18564 if (pos <= BUF_BEGV (b))
18565 return "All";
18566 else
18567 return "Bottom";
18569 else if (pos <= BUF_BEGV (b))
18570 return "Top";
18571 else
18573 if (total > 1000000)
18574 /* Do it differently for a large value, to avoid overflow. */
18575 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18576 else
18577 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18578 /* We can't normally display a 3-digit number,
18579 so get us a 2-digit number that is close. */
18580 if (total == 100)
18581 total = 99;
18582 sprintf (decode_mode_spec_buf, "%2d%%", total);
18583 return decode_mode_spec_buf;
18587 /* Display percentage of size above the bottom of the screen. */
18588 case 'P':
18590 int toppos = marker_position (w->start);
18591 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18592 int total = BUF_ZV (b) - BUF_BEGV (b);
18594 if (botpos >= BUF_ZV (b))
18596 if (toppos <= BUF_BEGV (b))
18597 return "All";
18598 else
18599 return "Bottom";
18601 else
18603 if (total > 1000000)
18604 /* Do it differently for a large value, to avoid overflow. */
18605 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18606 else
18607 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18608 /* We can't normally display a 3-digit number,
18609 so get us a 2-digit number that is close. */
18610 if (total == 100)
18611 total = 99;
18612 if (toppos <= BUF_BEGV (b))
18613 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18614 else
18615 sprintf (decode_mode_spec_buf, "%2d%%", total);
18616 return decode_mode_spec_buf;
18620 case 's':
18621 /* status of process */
18622 obj = Fget_buffer_process (Fcurrent_buffer ());
18623 if (NILP (obj))
18624 return "no process";
18625 #ifdef subprocesses
18626 obj = Fsymbol_name (Fprocess_status (obj));
18627 #endif
18628 break;
18630 case '@':
18632 Lisp_Object val;
18633 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18634 if (NILP (val))
18635 return "-";
18636 else
18637 return "@";
18640 case 't': /* indicate TEXT or BINARY */
18641 #ifdef MODE_LINE_BINARY_TEXT
18642 return MODE_LINE_BINARY_TEXT (b);
18643 #else
18644 return "T";
18645 #endif
18647 case 'z':
18648 /* coding-system (not including end-of-line format) */
18649 case 'Z':
18650 /* coding-system (including end-of-line type) */
18652 int eol_flag = (c == 'Z');
18653 char *p = decode_mode_spec_buf;
18655 if (! FRAME_WINDOW_P (f))
18657 /* No need to mention EOL here--the terminal never needs
18658 to do EOL conversion. */
18659 p = decode_mode_spec_coding (CODING_ID_NAME
18660 (FRAME_KEYBOARD_CODING (f)->id),
18661 p, 0);
18662 p = decode_mode_spec_coding (CODING_ID_NAME
18663 (FRAME_TERMINAL_CODING (f)->id),
18664 p, 0);
18666 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18667 p, eol_flag);
18669 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18670 #ifdef subprocesses
18671 obj = Fget_buffer_process (Fcurrent_buffer ());
18672 if (PROCESSP (obj))
18674 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18675 p, eol_flag);
18676 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18677 p, eol_flag);
18679 #endif /* subprocesses */
18680 #endif /* 0 */
18681 *p = 0;
18682 return decode_mode_spec_buf;
18686 if (STRINGP (obj))
18688 *multibyte = STRING_MULTIBYTE (obj);
18689 return (char *) SDATA (obj);
18691 else
18692 return "";
18696 /* Count up to COUNT lines starting from START / START_BYTE.
18697 But don't go beyond LIMIT_BYTE.
18698 Return the number of lines thus found (always nonnegative).
18700 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18702 static int
18703 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18704 int start, start_byte, limit_byte, count;
18705 int *byte_pos_ptr;
18707 register unsigned char *cursor;
18708 unsigned char *base;
18710 register int ceiling;
18711 register unsigned char *ceiling_addr;
18712 int orig_count = count;
18714 /* If we are not in selective display mode,
18715 check only for newlines. */
18716 int selective_display = (!NILP (current_buffer->selective_display)
18717 && !INTEGERP (current_buffer->selective_display));
18719 if (count > 0)
18721 while (start_byte < limit_byte)
18723 ceiling = BUFFER_CEILING_OF (start_byte);
18724 ceiling = min (limit_byte - 1, ceiling);
18725 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18726 base = (cursor = BYTE_POS_ADDR (start_byte));
18727 while (1)
18729 if (selective_display)
18730 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18732 else
18733 while (*cursor != '\n' && ++cursor != ceiling_addr)
18736 if (cursor != ceiling_addr)
18738 if (--count == 0)
18740 start_byte += cursor - base + 1;
18741 *byte_pos_ptr = start_byte;
18742 return orig_count;
18744 else
18745 if (++cursor == ceiling_addr)
18746 break;
18748 else
18749 break;
18751 start_byte += cursor - base;
18754 else
18756 while (start_byte > limit_byte)
18758 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18759 ceiling = max (limit_byte, ceiling);
18760 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18761 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18762 while (1)
18764 if (selective_display)
18765 while (--cursor != ceiling_addr
18766 && *cursor != '\n' && *cursor != 015)
18768 else
18769 while (--cursor != ceiling_addr && *cursor != '\n')
18772 if (cursor != ceiling_addr)
18774 if (++count == 0)
18776 start_byte += cursor - base + 1;
18777 *byte_pos_ptr = start_byte;
18778 /* When scanning backwards, we should
18779 not count the newline posterior to which we stop. */
18780 return - orig_count - 1;
18783 else
18784 break;
18786 /* Here we add 1 to compensate for the last decrement
18787 of CURSOR, which took it past the valid range. */
18788 start_byte += cursor - base + 1;
18792 *byte_pos_ptr = limit_byte;
18794 if (count < 0)
18795 return - orig_count + count;
18796 return orig_count - count;
18802 /***********************************************************************
18803 Displaying strings
18804 ***********************************************************************/
18806 /* Display a NUL-terminated string, starting with index START.
18808 If STRING is non-null, display that C string. Otherwise, the Lisp
18809 string LISP_STRING is displayed.
18811 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18812 FACE_STRING. Display STRING or LISP_STRING with the face at
18813 FACE_STRING_POS in FACE_STRING:
18815 Display the string in the environment given by IT, but use the
18816 standard display table, temporarily.
18818 FIELD_WIDTH is the minimum number of output glyphs to produce.
18819 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18820 with spaces. If STRING has more characters, more than FIELD_WIDTH
18821 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18823 PRECISION is the maximum number of characters to output from
18824 STRING. PRECISION < 0 means don't truncate the string.
18826 This is roughly equivalent to printf format specifiers:
18828 FIELD_WIDTH PRECISION PRINTF
18829 ----------------------------------------
18830 -1 -1 %s
18831 -1 10 %.10s
18832 10 -1 %10s
18833 20 10 %20.10s
18835 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18836 display them, and < 0 means obey the current buffer's value of
18837 enable_multibyte_characters.
18839 Value is the number of columns displayed. */
18841 static int
18842 display_string (string, lisp_string, face_string, face_string_pos,
18843 start, it, field_width, precision, max_x, multibyte)
18844 unsigned char *string;
18845 Lisp_Object lisp_string;
18846 Lisp_Object face_string;
18847 EMACS_INT face_string_pos;
18848 EMACS_INT start;
18849 struct it *it;
18850 int field_width, precision, max_x;
18851 int multibyte;
18853 int hpos_at_start = it->hpos;
18854 int saved_face_id = it->face_id;
18855 struct glyph_row *row = it->glyph_row;
18857 /* Initialize the iterator IT for iteration over STRING beginning
18858 with index START. */
18859 reseat_to_string (it, string, lisp_string, start,
18860 precision, field_width, multibyte);
18862 /* If displaying STRING, set up the face of the iterator
18863 from LISP_STRING, if that's given. */
18864 if (STRINGP (face_string))
18866 EMACS_INT endptr;
18867 struct face *face;
18869 it->face_id
18870 = face_at_string_position (it->w, face_string, face_string_pos,
18871 0, it->region_beg_charpos,
18872 it->region_end_charpos,
18873 &endptr, it->base_face_id, 0);
18874 face = FACE_FROM_ID (it->f, it->face_id);
18875 it->face_box_p = face->box != FACE_NO_BOX;
18878 /* Set max_x to the maximum allowed X position. Don't let it go
18879 beyond the right edge of the window. */
18880 if (max_x <= 0)
18881 max_x = it->last_visible_x;
18882 else
18883 max_x = min (max_x, it->last_visible_x);
18885 /* Skip over display elements that are not visible. because IT->w is
18886 hscrolled. */
18887 if (it->current_x < it->first_visible_x)
18888 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18889 MOVE_TO_POS | MOVE_TO_X);
18891 row->ascent = it->max_ascent;
18892 row->height = it->max_ascent + it->max_descent;
18893 row->phys_ascent = it->max_phys_ascent;
18894 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18895 row->extra_line_spacing = it->max_extra_line_spacing;
18897 /* This condition is for the case that we are called with current_x
18898 past last_visible_x. */
18899 while (it->current_x < max_x)
18901 int x_before, x, n_glyphs_before, i, nglyphs;
18903 /* Get the next display element. */
18904 if (!get_next_display_element (it))
18905 break;
18907 /* Produce glyphs. */
18908 x_before = it->current_x;
18909 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18910 PRODUCE_GLYPHS (it);
18912 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18913 i = 0;
18914 x = x_before;
18915 while (i < nglyphs)
18917 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18919 if (it->line_wrap != TRUNCATE
18920 && x + glyph->pixel_width > max_x)
18922 /* End of continued line or max_x reached. */
18923 if (CHAR_GLYPH_PADDING_P (*glyph))
18925 /* A wide character is unbreakable. */
18926 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18927 it->current_x = x_before;
18929 else
18931 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18932 it->current_x = x;
18934 break;
18936 else if (x + glyph->pixel_width >= it->first_visible_x)
18938 /* Glyph is at least partially visible. */
18939 ++it->hpos;
18940 if (x < it->first_visible_x)
18941 it->glyph_row->x = x - it->first_visible_x;
18943 else
18945 /* Glyph is off the left margin of the display area.
18946 Should not happen. */
18947 abort ();
18950 row->ascent = max (row->ascent, it->max_ascent);
18951 row->height = max (row->height, it->max_ascent + it->max_descent);
18952 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18953 row->phys_height = max (row->phys_height,
18954 it->max_phys_ascent + it->max_phys_descent);
18955 row->extra_line_spacing = max (row->extra_line_spacing,
18956 it->max_extra_line_spacing);
18957 x += glyph->pixel_width;
18958 ++i;
18961 /* Stop if max_x reached. */
18962 if (i < nglyphs)
18963 break;
18965 /* Stop at line ends. */
18966 if (ITERATOR_AT_END_OF_LINE_P (it))
18968 it->continuation_lines_width = 0;
18969 break;
18972 set_iterator_to_next (it, 1);
18974 /* Stop if truncating at the right edge. */
18975 if (it->line_wrap == TRUNCATE
18976 && it->current_x >= it->last_visible_x)
18978 /* Add truncation mark, but don't do it if the line is
18979 truncated at a padding space. */
18980 if (IT_CHARPOS (*it) < it->string_nchars)
18982 if (!FRAME_WINDOW_P (it->f))
18984 int i, n;
18986 if (it->current_x > it->last_visible_x)
18988 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18989 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18990 break;
18991 for (n = row->used[TEXT_AREA]; i < n; ++i)
18993 row->used[TEXT_AREA] = i;
18994 produce_special_glyphs (it, IT_TRUNCATION);
18997 produce_special_glyphs (it, IT_TRUNCATION);
18999 it->glyph_row->truncated_on_right_p = 1;
19001 break;
19005 /* Maybe insert a truncation at the left. */
19006 if (it->first_visible_x
19007 && IT_CHARPOS (*it) > 0)
19009 if (!FRAME_WINDOW_P (it->f))
19010 insert_left_trunc_glyphs (it);
19011 it->glyph_row->truncated_on_left_p = 1;
19014 it->face_id = saved_face_id;
19016 /* Value is number of columns displayed. */
19017 return it->hpos - hpos_at_start;
19022 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19023 appears as an element of LIST or as the car of an element of LIST.
19024 If PROPVAL is a list, compare each element against LIST in that
19025 way, and return 1/2 if any element of PROPVAL is found in LIST.
19026 Otherwise return 0. This function cannot quit.
19027 The return value is 2 if the text is invisible but with an ellipsis
19028 and 1 if it's invisible and without an ellipsis. */
19031 invisible_p (propval, list)
19032 register Lisp_Object propval;
19033 Lisp_Object list;
19035 register Lisp_Object tail, proptail;
19037 for (tail = list; CONSP (tail); tail = XCDR (tail))
19039 register Lisp_Object tem;
19040 tem = XCAR (tail);
19041 if (EQ (propval, tem))
19042 return 1;
19043 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19044 return NILP (XCDR (tem)) ? 1 : 2;
19047 if (CONSP (propval))
19049 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19051 Lisp_Object propelt;
19052 propelt = XCAR (proptail);
19053 for (tail = list; CONSP (tail); tail = XCDR (tail))
19055 register Lisp_Object tem;
19056 tem = XCAR (tail);
19057 if (EQ (propelt, tem))
19058 return 1;
19059 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19060 return NILP (XCDR (tem)) ? 1 : 2;
19065 return 0;
19068 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19069 doc: /* Non-nil if the property makes the text invisible.
19070 POS-OR-PROP can be a marker or number, in which case it is taken to be
19071 a position in the current buffer and the value of the `invisible' property
19072 is checked; or it can be some other value, which is then presumed to be the
19073 value of the `invisible' property of the text of interest.
19074 The non-nil value returned can be t for truly invisible text or something
19075 else if the text is replaced by an ellipsis. */)
19076 (pos_or_prop)
19077 Lisp_Object pos_or_prop;
19079 Lisp_Object prop
19080 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19081 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19082 : pos_or_prop);
19083 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19084 return (invis == 0 ? Qnil
19085 : invis == 1 ? Qt
19086 : make_number (invis));
19089 /* Calculate a width or height in pixels from a specification using
19090 the following elements:
19092 SPEC ::=
19093 NUM - a (fractional) multiple of the default font width/height
19094 (NUM) - specifies exactly NUM pixels
19095 UNIT - a fixed number of pixels, see below.
19096 ELEMENT - size of a display element in pixels, see below.
19097 (NUM . SPEC) - equals NUM * SPEC
19098 (+ SPEC SPEC ...) - add pixel values
19099 (- SPEC SPEC ...) - subtract pixel values
19100 (- SPEC) - negate pixel value
19102 NUM ::=
19103 INT or FLOAT - a number constant
19104 SYMBOL - use symbol's (buffer local) variable binding.
19106 UNIT ::=
19107 in - pixels per inch *)
19108 mm - pixels per 1/1000 meter *)
19109 cm - pixels per 1/100 meter *)
19110 width - width of current font in pixels.
19111 height - height of current font in pixels.
19113 *) using the ratio(s) defined in display-pixels-per-inch.
19115 ELEMENT ::=
19117 left-fringe - left fringe width in pixels
19118 right-fringe - right fringe width in pixels
19120 left-margin - left margin width in pixels
19121 right-margin - right margin width in pixels
19123 scroll-bar - scroll-bar area width in pixels
19125 Examples:
19127 Pixels corresponding to 5 inches:
19128 (5 . in)
19130 Total width of non-text areas on left side of window (if scroll-bar is on left):
19131 '(space :width (+ left-fringe left-margin scroll-bar))
19133 Align to first text column (in header line):
19134 '(space :align-to 0)
19136 Align to middle of text area minus half the width of variable `my-image'
19137 containing a loaded image:
19138 '(space :align-to (0.5 . (- text my-image)))
19140 Width of left margin minus width of 1 character in the default font:
19141 '(space :width (- left-margin 1))
19143 Width of left margin minus width of 2 characters in the current font:
19144 '(space :width (- left-margin (2 . width)))
19146 Center 1 character over left-margin (in header line):
19147 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19149 Different ways to express width of left fringe plus left margin minus one pixel:
19150 '(space :width (- (+ left-fringe left-margin) (1)))
19151 '(space :width (+ left-fringe left-margin (- (1))))
19152 '(space :width (+ left-fringe left-margin (-1)))
19156 #define NUMVAL(X) \
19157 ((INTEGERP (X) || FLOATP (X)) \
19158 ? XFLOATINT (X) \
19159 : - 1)
19162 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19163 double *res;
19164 struct it *it;
19165 Lisp_Object prop;
19166 struct font *font;
19167 int width_p, *align_to;
19169 double pixels;
19171 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19172 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19174 if (NILP (prop))
19175 return OK_PIXELS (0);
19177 xassert (FRAME_LIVE_P (it->f));
19179 if (SYMBOLP (prop))
19181 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19183 char *unit = SDATA (SYMBOL_NAME (prop));
19185 if (unit[0] == 'i' && unit[1] == 'n')
19186 pixels = 1.0;
19187 else if (unit[0] == 'm' && unit[1] == 'm')
19188 pixels = 25.4;
19189 else if (unit[0] == 'c' && unit[1] == 'm')
19190 pixels = 2.54;
19191 else
19192 pixels = 0;
19193 if (pixels > 0)
19195 double ppi;
19196 #ifdef HAVE_WINDOW_SYSTEM
19197 if (FRAME_WINDOW_P (it->f)
19198 && (ppi = (width_p
19199 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19200 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19201 ppi > 0))
19202 return OK_PIXELS (ppi / pixels);
19203 #endif
19205 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19206 || (CONSP (Vdisplay_pixels_per_inch)
19207 && (ppi = (width_p
19208 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19209 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19210 ppi > 0)))
19211 return OK_PIXELS (ppi / pixels);
19213 return 0;
19217 #ifdef HAVE_WINDOW_SYSTEM
19218 if (EQ (prop, Qheight))
19219 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19220 if (EQ (prop, Qwidth))
19221 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19222 #else
19223 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19224 return OK_PIXELS (1);
19225 #endif
19227 if (EQ (prop, Qtext))
19228 return OK_PIXELS (width_p
19229 ? window_box_width (it->w, TEXT_AREA)
19230 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19232 if (align_to && *align_to < 0)
19234 *res = 0;
19235 if (EQ (prop, Qleft))
19236 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19237 if (EQ (prop, Qright))
19238 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19239 if (EQ (prop, Qcenter))
19240 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19241 + window_box_width (it->w, TEXT_AREA) / 2);
19242 if (EQ (prop, Qleft_fringe))
19243 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19244 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19245 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19246 if (EQ (prop, Qright_fringe))
19247 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19248 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19249 : window_box_right_offset (it->w, TEXT_AREA));
19250 if (EQ (prop, Qleft_margin))
19251 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19252 if (EQ (prop, Qright_margin))
19253 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19254 if (EQ (prop, Qscroll_bar))
19255 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19257 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19258 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19259 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19260 : 0)));
19262 else
19264 if (EQ (prop, Qleft_fringe))
19265 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19266 if (EQ (prop, Qright_fringe))
19267 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19268 if (EQ (prop, Qleft_margin))
19269 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19270 if (EQ (prop, Qright_margin))
19271 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19272 if (EQ (prop, Qscroll_bar))
19273 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19276 prop = Fbuffer_local_value (prop, it->w->buffer);
19279 if (INTEGERP (prop) || FLOATP (prop))
19281 int base_unit = (width_p
19282 ? FRAME_COLUMN_WIDTH (it->f)
19283 : FRAME_LINE_HEIGHT (it->f));
19284 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19287 if (CONSP (prop))
19289 Lisp_Object car = XCAR (prop);
19290 Lisp_Object cdr = XCDR (prop);
19292 if (SYMBOLP (car))
19294 #ifdef HAVE_WINDOW_SYSTEM
19295 if (FRAME_WINDOW_P (it->f)
19296 && valid_image_p (prop))
19298 int id = lookup_image (it->f, prop);
19299 struct image *img = IMAGE_FROM_ID (it->f, id);
19301 return OK_PIXELS (width_p ? img->width : img->height);
19303 #endif
19304 if (EQ (car, Qplus) || EQ (car, Qminus))
19306 int first = 1;
19307 double px;
19309 pixels = 0;
19310 while (CONSP (cdr))
19312 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19313 font, width_p, align_to))
19314 return 0;
19315 if (first)
19316 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19317 else
19318 pixels += px;
19319 cdr = XCDR (cdr);
19321 if (EQ (car, Qminus))
19322 pixels = -pixels;
19323 return OK_PIXELS (pixels);
19326 car = Fbuffer_local_value (car, it->w->buffer);
19329 if (INTEGERP (car) || FLOATP (car))
19331 double fact;
19332 pixels = XFLOATINT (car);
19333 if (NILP (cdr))
19334 return OK_PIXELS (pixels);
19335 if (calc_pixel_width_or_height (&fact, it, cdr,
19336 font, width_p, align_to))
19337 return OK_PIXELS (pixels * fact);
19338 return 0;
19341 return 0;
19344 return 0;
19348 /***********************************************************************
19349 Glyph Display
19350 ***********************************************************************/
19352 #ifdef HAVE_WINDOW_SYSTEM
19354 #if GLYPH_DEBUG
19356 void
19357 dump_glyph_string (s)
19358 struct glyph_string *s;
19360 fprintf (stderr, "glyph string\n");
19361 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19362 s->x, s->y, s->width, s->height);
19363 fprintf (stderr, " ybase = %d\n", s->ybase);
19364 fprintf (stderr, " hl = %d\n", s->hl);
19365 fprintf (stderr, " left overhang = %d, right = %d\n",
19366 s->left_overhang, s->right_overhang);
19367 fprintf (stderr, " nchars = %d\n", s->nchars);
19368 fprintf (stderr, " extends to end of line = %d\n",
19369 s->extends_to_end_of_line_p);
19370 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19371 fprintf (stderr, " bg width = %d\n", s->background_width);
19374 #endif /* GLYPH_DEBUG */
19376 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19377 of XChar2b structures for S; it can't be allocated in
19378 init_glyph_string because it must be allocated via `alloca'. W
19379 is the window on which S is drawn. ROW and AREA are the glyph row
19380 and area within the row from which S is constructed. START is the
19381 index of the first glyph structure covered by S. HL is a
19382 face-override for drawing S. */
19384 #ifdef HAVE_NTGUI
19385 #define OPTIONAL_HDC(hdc) hdc,
19386 #define DECLARE_HDC(hdc) HDC hdc;
19387 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19388 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19389 #endif
19391 #ifndef OPTIONAL_HDC
19392 #define OPTIONAL_HDC(hdc)
19393 #define DECLARE_HDC(hdc)
19394 #define ALLOCATE_HDC(hdc, f)
19395 #define RELEASE_HDC(hdc, f)
19396 #endif
19398 static void
19399 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19400 struct glyph_string *s;
19401 DECLARE_HDC (hdc)
19402 XChar2b *char2b;
19403 struct window *w;
19404 struct glyph_row *row;
19405 enum glyph_row_area area;
19406 int start;
19407 enum draw_glyphs_face hl;
19409 bzero (s, sizeof *s);
19410 s->w = w;
19411 s->f = XFRAME (w->frame);
19412 #ifdef HAVE_NTGUI
19413 s->hdc = hdc;
19414 #endif
19415 s->display = FRAME_X_DISPLAY (s->f);
19416 s->window = FRAME_X_WINDOW (s->f);
19417 s->char2b = char2b;
19418 s->hl = hl;
19419 s->row = row;
19420 s->area = area;
19421 s->first_glyph = row->glyphs[area] + start;
19422 s->height = row->height;
19423 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19425 /* Display the internal border below the tool-bar window. */
19426 if (WINDOWP (s->f->tool_bar_window)
19427 && s->w == XWINDOW (s->f->tool_bar_window))
19428 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19430 s->ybase = s->y + row->ascent;
19434 /* Append the list of glyph strings with head H and tail T to the list
19435 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19437 static INLINE void
19438 append_glyph_string_lists (head, tail, h, t)
19439 struct glyph_string **head, **tail;
19440 struct glyph_string *h, *t;
19442 if (h)
19444 if (*head)
19445 (*tail)->next = h;
19446 else
19447 *head = h;
19448 h->prev = *tail;
19449 *tail = t;
19454 /* Prepend the list of glyph strings with head H and tail T to the
19455 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19456 result. */
19458 static INLINE void
19459 prepend_glyph_string_lists (head, tail, h, t)
19460 struct glyph_string **head, **tail;
19461 struct glyph_string *h, *t;
19463 if (h)
19465 if (*head)
19466 (*head)->prev = t;
19467 else
19468 *tail = t;
19469 t->next = *head;
19470 *head = h;
19475 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19476 Set *HEAD and *TAIL to the resulting list. */
19478 static INLINE void
19479 append_glyph_string (head, tail, s)
19480 struct glyph_string **head, **tail;
19481 struct glyph_string *s;
19483 s->next = s->prev = NULL;
19484 append_glyph_string_lists (head, tail, s, s);
19488 /* Get face and two-byte form of character C in face FACE_ID on frame
19489 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19490 means we want to display multibyte text. DISPLAY_P non-zero means
19491 make sure that X resources for the face returned are allocated.
19492 Value is a pointer to a realized face that is ready for display if
19493 DISPLAY_P is non-zero. */
19495 static INLINE struct face *
19496 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19497 struct frame *f;
19498 int c, face_id;
19499 XChar2b *char2b;
19500 int multibyte_p, display_p;
19502 struct face *face = FACE_FROM_ID (f, face_id);
19504 if (face->font)
19506 unsigned code = face->font->driver->encode_char (face->font, c);
19508 if (code != FONT_INVALID_CODE)
19509 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19510 else
19511 STORE_XCHAR2B (char2b, 0, 0);
19514 /* Make sure X resources of the face are allocated. */
19515 #ifdef HAVE_X_WINDOWS
19516 if (display_p)
19517 #endif
19519 xassert (face != NULL);
19520 PREPARE_FACE_FOR_DISPLAY (f, face);
19523 return face;
19527 /* Get face and two-byte form of character glyph GLYPH on frame F.
19528 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19529 a pointer to a realized face that is ready for display. */
19531 static INLINE struct face *
19532 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19533 struct frame *f;
19534 struct glyph *glyph;
19535 XChar2b *char2b;
19536 int *two_byte_p;
19538 struct face *face;
19540 xassert (glyph->type == CHAR_GLYPH);
19541 face = FACE_FROM_ID (f, glyph->face_id);
19543 if (two_byte_p)
19544 *two_byte_p = 0;
19546 if (face->font)
19548 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19550 if (code != FONT_INVALID_CODE)
19551 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19552 else
19553 STORE_XCHAR2B (char2b, 0, 0);
19556 /* Make sure X resources of the face are allocated. */
19557 xassert (face != NULL);
19558 PREPARE_FACE_FOR_DISPLAY (f, face);
19559 return face;
19563 /* Fill glyph string S with composition components specified by S->cmp.
19565 BASE_FACE is the base face of the composition.
19566 S->cmp_from is the index of the first component for S.
19568 OVERLAPS non-zero means S should draw the foreground only, and use
19569 its physical height for clipping. See also draw_glyphs.
19571 Value is the index of a component not in S. */
19573 static int
19574 fill_composite_glyph_string (s, base_face, overlaps)
19575 struct glyph_string *s;
19576 struct face *base_face;
19577 int overlaps;
19579 int i;
19580 /* For all glyphs of this composition, starting at the offset
19581 S->cmp_from, until we reach the end of the definition or encounter a
19582 glyph that requires the different face, add it to S. */
19583 struct face *face;
19585 xassert (s);
19587 s->for_overlaps = overlaps;
19588 s->face = NULL;
19589 s->font = NULL;
19590 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19592 int c = COMPOSITION_GLYPH (s->cmp, i);
19594 if (c != '\t')
19596 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19597 -1, Qnil);
19599 face = get_char_face_and_encoding (s->f, c, face_id,
19600 s->char2b + i, 1, 1);
19601 if (face)
19603 if (! s->face)
19605 s->face = face;
19606 s->font = s->face->font;
19608 else if (s->face != face)
19609 break;
19612 ++s->nchars;
19614 s->cmp_to = i;
19616 /* All glyph strings for the same composition has the same width,
19617 i.e. the width set for the first component of the composition. */
19618 s->width = s->first_glyph->pixel_width;
19620 /* If the specified font could not be loaded, use the frame's
19621 default font, but record the fact that we couldn't load it in
19622 the glyph string so that we can draw rectangles for the
19623 characters of the glyph string. */
19624 if (s->font == NULL)
19626 s->font_not_found_p = 1;
19627 s->font = FRAME_FONT (s->f);
19630 /* Adjust base line for subscript/superscript text. */
19631 s->ybase += s->first_glyph->voffset;
19633 /* This glyph string must always be drawn with 16-bit functions. */
19634 s->two_byte_p = 1;
19636 return s->cmp_to;
19639 static int
19640 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19641 struct glyph_string *s;
19642 int face_id;
19643 int start, end, overlaps;
19645 struct glyph *glyph, *last;
19646 Lisp_Object lgstring;
19647 int i;
19649 s->for_overlaps = overlaps;
19650 glyph = s->row->glyphs[s->area] + start;
19651 last = s->row->glyphs[s->area] + end;
19652 s->cmp_id = glyph->u.cmp.id;
19653 s->cmp_from = glyph->u.cmp.from;
19654 s->cmp_to = glyph->u.cmp.to + 1;
19655 s->face = FACE_FROM_ID (s->f, face_id);
19656 lgstring = composition_gstring_from_id (s->cmp_id);
19657 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19658 glyph++;
19659 while (glyph < last
19660 && glyph->u.cmp.automatic
19661 && glyph->u.cmp.id == s->cmp_id
19662 && s->cmp_to == glyph->u.cmp.from)
19663 s->cmp_to = (glyph++)->u.cmp.to + 1;
19665 for (i = s->cmp_from; i < s->cmp_to; i++)
19667 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19668 unsigned code = LGLYPH_CODE (lglyph);
19670 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19672 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19673 return glyph - s->row->glyphs[s->area];
19677 /* Fill glyph string S from a sequence of character glyphs.
19679 FACE_ID is the face id of the string. START is the index of the
19680 first glyph to consider, END is the index of the last + 1.
19681 OVERLAPS non-zero means S should draw the foreground only, and use
19682 its physical height for clipping. See also draw_glyphs.
19684 Value is the index of the first glyph not in S. */
19686 static int
19687 fill_glyph_string (s, face_id, start, end, overlaps)
19688 struct glyph_string *s;
19689 int face_id;
19690 int start, end, overlaps;
19692 struct glyph *glyph, *last;
19693 int voffset;
19694 int glyph_not_available_p;
19696 xassert (s->f == XFRAME (s->w->frame));
19697 xassert (s->nchars == 0);
19698 xassert (start >= 0 && end > start);
19700 s->for_overlaps = overlaps;
19701 glyph = s->row->glyphs[s->area] + start;
19702 last = s->row->glyphs[s->area] + end;
19703 voffset = glyph->voffset;
19704 s->padding_p = glyph->padding_p;
19705 glyph_not_available_p = glyph->glyph_not_available_p;
19707 while (glyph < last
19708 && glyph->type == CHAR_GLYPH
19709 && glyph->voffset == voffset
19710 /* Same face id implies same font, nowadays. */
19711 && glyph->face_id == face_id
19712 && glyph->glyph_not_available_p == glyph_not_available_p)
19714 int two_byte_p;
19716 s->face = get_glyph_face_and_encoding (s->f, glyph,
19717 s->char2b + s->nchars,
19718 &two_byte_p);
19719 s->two_byte_p = two_byte_p;
19720 ++s->nchars;
19721 xassert (s->nchars <= end - start);
19722 s->width += glyph->pixel_width;
19723 if (glyph++->padding_p != s->padding_p)
19724 break;
19727 s->font = s->face->font;
19729 /* If the specified font could not be loaded, use the frame's font,
19730 but record the fact that we couldn't load it in
19731 S->font_not_found_p so that we can draw rectangles for the
19732 characters of the glyph string. */
19733 if (s->font == NULL || glyph_not_available_p)
19735 s->font_not_found_p = 1;
19736 s->font = FRAME_FONT (s->f);
19739 /* Adjust base line for subscript/superscript text. */
19740 s->ybase += voffset;
19742 xassert (s->face && s->face->gc);
19743 return glyph - s->row->glyphs[s->area];
19747 /* Fill glyph string S from image glyph S->first_glyph. */
19749 static void
19750 fill_image_glyph_string (s)
19751 struct glyph_string *s;
19753 xassert (s->first_glyph->type == IMAGE_GLYPH);
19754 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19755 xassert (s->img);
19756 s->slice = s->first_glyph->slice;
19757 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19758 s->font = s->face->font;
19759 s->width = s->first_glyph->pixel_width;
19761 /* Adjust base line for subscript/superscript text. */
19762 s->ybase += s->first_glyph->voffset;
19766 /* Fill glyph string S from a sequence of stretch glyphs.
19768 ROW is the glyph row in which the glyphs are found, AREA is the
19769 area within the row. START is the index of the first glyph to
19770 consider, END is the index of the last + 1.
19772 Value is the index of the first glyph not in S. */
19774 static int
19775 fill_stretch_glyph_string (s, row, area, start, end)
19776 struct glyph_string *s;
19777 struct glyph_row *row;
19778 enum glyph_row_area area;
19779 int start, end;
19781 struct glyph *glyph, *last;
19782 int voffset, face_id;
19784 xassert (s->first_glyph->type == STRETCH_GLYPH);
19786 glyph = s->row->glyphs[s->area] + start;
19787 last = s->row->glyphs[s->area] + end;
19788 face_id = glyph->face_id;
19789 s->face = FACE_FROM_ID (s->f, face_id);
19790 s->font = s->face->font;
19791 s->width = glyph->pixel_width;
19792 s->nchars = 1;
19793 voffset = glyph->voffset;
19795 for (++glyph;
19796 (glyph < last
19797 && glyph->type == STRETCH_GLYPH
19798 && glyph->voffset == voffset
19799 && glyph->face_id == face_id);
19800 ++glyph)
19801 s->width += glyph->pixel_width;
19803 /* Adjust base line for subscript/superscript text. */
19804 s->ybase += voffset;
19806 /* The case that face->gc == 0 is handled when drawing the glyph
19807 string by calling PREPARE_FACE_FOR_DISPLAY. */
19808 xassert (s->face);
19809 return glyph - s->row->glyphs[s->area];
19812 static struct font_metrics *
19813 get_per_char_metric (f, font, char2b)
19814 struct frame *f;
19815 struct font *font;
19816 XChar2b *char2b;
19818 static struct font_metrics metrics;
19819 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19821 if (! font || code == FONT_INVALID_CODE)
19822 return NULL;
19823 font->driver->text_extents (font, &code, 1, &metrics);
19824 return &metrics;
19827 /* EXPORT for RIF:
19828 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19829 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19830 assumed to be zero. */
19832 void
19833 x_get_glyph_overhangs (glyph, f, left, right)
19834 struct glyph *glyph;
19835 struct frame *f;
19836 int *left, *right;
19838 *left = *right = 0;
19840 if (glyph->type == CHAR_GLYPH)
19842 struct face *face;
19843 XChar2b char2b;
19844 struct font_metrics *pcm;
19846 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19847 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19849 if (pcm->rbearing > pcm->width)
19850 *right = pcm->rbearing - pcm->width;
19851 if (pcm->lbearing < 0)
19852 *left = -pcm->lbearing;
19855 else if (glyph->type == COMPOSITE_GLYPH)
19857 if (! glyph->u.cmp.automatic)
19859 struct composition *cmp = composition_table[glyph->u.cmp.id];
19861 if (cmp->rbearing > cmp->pixel_width)
19862 *right = cmp->rbearing - cmp->pixel_width;
19863 if (cmp->lbearing < 0)
19864 *left = - cmp->lbearing;
19866 else
19868 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19869 struct font_metrics metrics;
19871 composition_gstring_width (gstring, glyph->u.cmp.from,
19872 glyph->u.cmp.to + 1, &metrics);
19873 if (metrics.rbearing > metrics.width)
19874 *right = metrics.rbearing - metrics.width;
19875 if (metrics.lbearing < 0)
19876 *left = - metrics.lbearing;
19882 /* Return the index of the first glyph preceding glyph string S that
19883 is overwritten by S because of S's left overhang. Value is -1
19884 if no glyphs are overwritten. */
19886 static int
19887 left_overwritten (s)
19888 struct glyph_string *s;
19890 int k;
19892 if (s->left_overhang)
19894 int x = 0, i;
19895 struct glyph *glyphs = s->row->glyphs[s->area];
19896 int first = s->first_glyph - glyphs;
19898 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19899 x -= glyphs[i].pixel_width;
19901 k = i + 1;
19903 else
19904 k = -1;
19906 return k;
19910 /* Return the index of the first glyph preceding glyph string S that
19911 is overwriting S because of its right overhang. Value is -1 if no
19912 glyph in front of S overwrites S. */
19914 static int
19915 left_overwriting (s)
19916 struct glyph_string *s;
19918 int i, k, x;
19919 struct glyph *glyphs = s->row->glyphs[s->area];
19920 int first = s->first_glyph - glyphs;
19922 k = -1;
19923 x = 0;
19924 for (i = first - 1; i >= 0; --i)
19926 int left, right;
19927 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19928 if (x + right > 0)
19929 k = i;
19930 x -= glyphs[i].pixel_width;
19933 return k;
19937 /* Return the index of the last glyph following glyph string S that is
19938 overwritten by S because of S's right overhang. Value is -1 if
19939 no such glyph is found. */
19941 static int
19942 right_overwritten (s)
19943 struct glyph_string *s;
19945 int k = -1;
19947 if (s->right_overhang)
19949 int x = 0, i;
19950 struct glyph *glyphs = s->row->glyphs[s->area];
19951 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19952 int end = s->row->used[s->area];
19954 for (i = first; i < end && s->right_overhang > x; ++i)
19955 x += glyphs[i].pixel_width;
19957 k = i;
19960 return k;
19964 /* Return the index of the last glyph following glyph string S that
19965 overwrites S because of its left overhang. Value is negative
19966 if no such glyph is found. */
19968 static int
19969 right_overwriting (s)
19970 struct glyph_string *s;
19972 int i, k, x;
19973 int end = s->row->used[s->area];
19974 struct glyph *glyphs = s->row->glyphs[s->area];
19975 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19977 k = -1;
19978 x = 0;
19979 for (i = first; i < end; ++i)
19981 int left, right;
19982 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19983 if (x - left < 0)
19984 k = i;
19985 x += glyphs[i].pixel_width;
19988 return k;
19992 /* Set background width of glyph string S. START is the index of the
19993 first glyph following S. LAST_X is the right-most x-position + 1
19994 in the drawing area. */
19996 static INLINE void
19997 set_glyph_string_background_width (s, start, last_x)
19998 struct glyph_string *s;
19999 int start;
20000 int last_x;
20002 /* If the face of this glyph string has to be drawn to the end of
20003 the drawing area, set S->extends_to_end_of_line_p. */
20005 if (start == s->row->used[s->area]
20006 && s->area == TEXT_AREA
20007 && ((s->row->fill_line_p
20008 && (s->hl == DRAW_NORMAL_TEXT
20009 || s->hl == DRAW_IMAGE_RAISED
20010 || s->hl == DRAW_IMAGE_SUNKEN))
20011 || s->hl == DRAW_MOUSE_FACE))
20012 s->extends_to_end_of_line_p = 1;
20014 /* If S extends its face to the end of the line, set its
20015 background_width to the distance to the right edge of the drawing
20016 area. */
20017 if (s->extends_to_end_of_line_p)
20018 s->background_width = last_x - s->x + 1;
20019 else
20020 s->background_width = s->width;
20024 /* Compute overhangs and x-positions for glyph string S and its
20025 predecessors, or successors. X is the starting x-position for S.
20026 BACKWARD_P non-zero means process predecessors. */
20028 static void
20029 compute_overhangs_and_x (s, x, backward_p)
20030 struct glyph_string *s;
20031 int x;
20032 int backward_p;
20034 if (backward_p)
20036 while (s)
20038 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20039 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20040 x -= s->width;
20041 s->x = x;
20042 s = s->prev;
20045 else
20047 while (s)
20049 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20050 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20051 s->x = x;
20052 x += s->width;
20053 s = s->next;
20060 /* The following macros are only called from draw_glyphs below.
20061 They reference the following parameters of that function directly:
20062 `w', `row', `area', and `overlap_p'
20063 as well as the following local variables:
20064 `s', `f', and `hdc' (in W32) */
20066 #ifdef HAVE_NTGUI
20067 /* On W32, silently add local `hdc' variable to argument list of
20068 init_glyph_string. */
20069 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20070 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20071 #else
20072 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20073 init_glyph_string (s, char2b, w, row, area, start, hl)
20074 #endif
20076 /* Add a glyph string for a stretch glyph to the list of strings
20077 between HEAD and TAIL. START is the index of the stretch glyph in
20078 row area AREA of glyph row ROW. END is the index of the last glyph
20079 in that glyph row area. X is the current output position assigned
20080 to the new glyph string constructed. HL overrides that face of the
20081 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20082 is the right-most x-position of the drawing area. */
20084 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20085 and below -- keep them on one line. */
20086 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20087 do \
20089 s = (struct glyph_string *) alloca (sizeof *s); \
20090 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20091 START = fill_stretch_glyph_string (s, row, area, START, END); \
20092 append_glyph_string (&HEAD, &TAIL, s); \
20093 s->x = (X); \
20095 while (0)
20098 /* Add a glyph string for an image glyph to the list of strings
20099 between HEAD and TAIL. START is the index of the image glyph in
20100 row area AREA of glyph row ROW. END is the index of the last glyph
20101 in that glyph row area. X is the current output position assigned
20102 to the new glyph string constructed. HL overrides that face of the
20103 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20104 is the right-most x-position of the drawing area. */
20106 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20107 do \
20109 s = (struct glyph_string *) alloca (sizeof *s); \
20110 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20111 fill_image_glyph_string (s); \
20112 append_glyph_string (&HEAD, &TAIL, s); \
20113 ++START; \
20114 s->x = (X); \
20116 while (0)
20119 /* Add a glyph string for a sequence of character glyphs to the list
20120 of strings between HEAD and TAIL. START is the index of the first
20121 glyph in row area AREA of glyph row ROW that is part of the new
20122 glyph string. END is the index of the last glyph in that glyph row
20123 area. X is the current output position assigned to the new glyph
20124 string constructed. HL overrides that face of the glyph; e.g. it
20125 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20126 right-most x-position of the drawing area. */
20128 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20129 do \
20131 int face_id; \
20132 XChar2b *char2b; \
20134 face_id = (row)->glyphs[area][START].face_id; \
20136 s = (struct glyph_string *) alloca (sizeof *s); \
20137 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20138 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20139 append_glyph_string (&HEAD, &TAIL, s); \
20140 s->x = (X); \
20141 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20143 while (0)
20146 /* Add a glyph string for a composite sequence to the list of strings
20147 between HEAD and TAIL. START is the index of the first glyph in
20148 row area AREA of glyph row ROW that is part of the new glyph
20149 string. END is the index of the last glyph in that glyph row area.
20150 X is the current output position assigned to the new glyph string
20151 constructed. HL overrides that face of the glyph; e.g. it is
20152 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20153 x-position of the drawing area. */
20155 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20156 do { \
20157 int face_id = (row)->glyphs[area][START].face_id; \
20158 struct face *base_face = FACE_FROM_ID (f, face_id); \
20159 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20160 struct composition *cmp = composition_table[cmp_id]; \
20161 XChar2b *char2b; \
20162 struct glyph_string *first_s; \
20163 int n; \
20165 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20167 /* Make glyph_strings for each glyph sequence that is drawable by \
20168 the same face, and append them to HEAD/TAIL. */ \
20169 for (n = 0; n < cmp->glyph_len;) \
20171 s = (struct glyph_string *) alloca (sizeof *s); \
20172 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20173 append_glyph_string (&(HEAD), &(TAIL), s); \
20174 s->cmp = cmp; \
20175 s->cmp_from = n; \
20176 s->x = (X); \
20177 if (n == 0) \
20178 first_s = s; \
20179 n = fill_composite_glyph_string (s, base_face, overlaps); \
20182 ++START; \
20183 s = first_s; \
20184 } while (0)
20187 /* Add a glyph string for a glyph-string sequence to the list of strings
20188 between HEAD and TAIL. */
20190 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20191 do { \
20192 int face_id; \
20193 XChar2b *char2b; \
20194 Lisp_Object gstring; \
20196 face_id = (row)->glyphs[area][START].face_id; \
20197 gstring = (composition_gstring_from_id \
20198 ((row)->glyphs[area][START].u.cmp.id)); \
20199 s = (struct glyph_string *) alloca (sizeof *s); \
20200 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20201 * LGSTRING_GLYPH_LEN (gstring)); \
20202 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20203 append_glyph_string (&(HEAD), &(TAIL), s); \
20204 s->x = (X); \
20205 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20206 } while (0)
20209 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20210 of AREA of glyph row ROW on window W between indices START and END.
20211 HL overrides the face for drawing glyph strings, e.g. it is
20212 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20213 x-positions of the drawing area.
20215 This is an ugly monster macro construct because we must use alloca
20216 to allocate glyph strings (because draw_glyphs can be called
20217 asynchronously). */
20219 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20220 do \
20222 HEAD = TAIL = NULL; \
20223 while (START < END) \
20225 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20226 switch (first_glyph->type) \
20228 case CHAR_GLYPH: \
20229 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20230 HL, X, LAST_X); \
20231 break; \
20233 case COMPOSITE_GLYPH: \
20234 if (first_glyph->u.cmp.automatic) \
20235 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20236 HL, X, LAST_X); \
20237 else \
20238 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20239 HL, X, LAST_X); \
20240 break; \
20242 case STRETCH_GLYPH: \
20243 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20244 HL, X, LAST_X); \
20245 break; \
20247 case IMAGE_GLYPH: \
20248 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20249 HL, X, LAST_X); \
20250 break; \
20252 default: \
20253 abort (); \
20256 if (s) \
20258 set_glyph_string_background_width (s, START, LAST_X); \
20259 (X) += s->width; \
20262 } while (0)
20265 /* Draw glyphs between START and END in AREA of ROW on window W,
20266 starting at x-position X. X is relative to AREA in W. HL is a
20267 face-override with the following meaning:
20269 DRAW_NORMAL_TEXT draw normally
20270 DRAW_CURSOR draw in cursor face
20271 DRAW_MOUSE_FACE draw in mouse face.
20272 DRAW_INVERSE_VIDEO draw in mode line face
20273 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20274 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20276 If OVERLAPS is non-zero, draw only the foreground of characters and
20277 clip to the physical height of ROW. Non-zero value also defines
20278 the overlapping part to be drawn:
20280 OVERLAPS_PRED overlap with preceding rows
20281 OVERLAPS_SUCC overlap with succeeding rows
20282 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20283 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20285 Value is the x-position reached, relative to AREA of W. */
20287 static int
20288 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20289 struct window *w;
20290 int x;
20291 struct glyph_row *row;
20292 enum glyph_row_area area;
20293 EMACS_INT start, end;
20294 enum draw_glyphs_face hl;
20295 int overlaps;
20297 struct glyph_string *head, *tail;
20298 struct glyph_string *s;
20299 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20300 int i, j, x_reached, last_x, area_left = 0;
20301 struct frame *f = XFRAME (WINDOW_FRAME (w));
20302 DECLARE_HDC (hdc);
20304 ALLOCATE_HDC (hdc, f);
20306 /* Let's rather be paranoid than getting a SEGV. */
20307 end = min (end, row->used[area]);
20308 start = max (0, start);
20309 start = min (end, start);
20311 /* Translate X to frame coordinates. Set last_x to the right
20312 end of the drawing area. */
20313 if (row->full_width_p)
20315 /* X is relative to the left edge of W, without scroll bars
20316 or fringes. */
20317 area_left = WINDOW_LEFT_EDGE_X (w);
20318 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20320 else
20322 area_left = window_box_left (w, area);
20323 last_x = area_left + window_box_width (w, area);
20325 x += area_left;
20327 /* Build a doubly-linked list of glyph_string structures between
20328 head and tail from what we have to draw. Note that the macro
20329 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20330 the reason we use a separate variable `i'. */
20331 i = start;
20332 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20333 if (tail)
20334 x_reached = tail->x + tail->background_width;
20335 else
20336 x_reached = x;
20338 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20339 the row, redraw some glyphs in front or following the glyph
20340 strings built above. */
20341 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20343 struct glyph_string *h, *t;
20344 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20345 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20346 int dummy_x = 0;
20348 /* If mouse highlighting is on, we may need to draw adjacent
20349 glyphs using mouse-face highlighting. */
20350 if (area == TEXT_AREA && row->mouse_face_p)
20352 struct glyph_row *mouse_beg_row, *mouse_end_row;
20354 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20355 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20357 if (row >= mouse_beg_row && row <= mouse_end_row)
20359 check_mouse_face = 1;
20360 mouse_beg_col = (row == mouse_beg_row)
20361 ? dpyinfo->mouse_face_beg_col : 0;
20362 mouse_end_col = (row == mouse_end_row)
20363 ? dpyinfo->mouse_face_end_col
20364 : row->used[TEXT_AREA];
20368 /* Compute overhangs for all glyph strings. */
20369 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20370 for (s = head; s; s = s->next)
20371 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20373 /* Prepend glyph strings for glyphs in front of the first glyph
20374 string that are overwritten because of the first glyph
20375 string's left overhang. The background of all strings
20376 prepended must be drawn because the first glyph string
20377 draws over it. */
20378 i = left_overwritten (head);
20379 if (i >= 0)
20381 enum draw_glyphs_face overlap_hl;
20383 /* If this row contains mouse highlighting, attempt to draw
20384 the overlapped glyphs with the correct highlight. This
20385 code fails if the overlap encompasses more than one glyph
20386 and mouse-highlight spans only some of these glyphs.
20387 However, making it work perfectly involves a lot more
20388 code, and I don't know if the pathological case occurs in
20389 practice, so we'll stick to this for now. --- cyd */
20390 if (check_mouse_face
20391 && mouse_beg_col < start && mouse_end_col > i)
20392 overlap_hl = DRAW_MOUSE_FACE;
20393 else
20394 overlap_hl = DRAW_NORMAL_TEXT;
20396 j = i;
20397 BUILD_GLYPH_STRINGS (j, start, h, t,
20398 overlap_hl, dummy_x, last_x);
20399 compute_overhangs_and_x (t, head->x, 1);
20400 prepend_glyph_string_lists (&head, &tail, h, t);
20401 clip_head = head;
20404 /* Prepend glyph strings for glyphs in front of the first glyph
20405 string that overwrite that glyph string because of their
20406 right overhang. For these strings, only the foreground must
20407 be drawn, because it draws over the glyph string at `head'.
20408 The background must not be drawn because this would overwrite
20409 right overhangs of preceding glyphs for which no glyph
20410 strings exist. */
20411 i = left_overwriting (head);
20412 if (i >= 0)
20414 enum draw_glyphs_face overlap_hl;
20416 if (check_mouse_face
20417 && mouse_beg_col < start && mouse_end_col > i)
20418 overlap_hl = DRAW_MOUSE_FACE;
20419 else
20420 overlap_hl = DRAW_NORMAL_TEXT;
20422 clip_head = head;
20423 BUILD_GLYPH_STRINGS (i, start, h, t,
20424 overlap_hl, dummy_x, last_x);
20425 for (s = h; s; s = s->next)
20426 s->background_filled_p = 1;
20427 compute_overhangs_and_x (t, head->x, 1);
20428 prepend_glyph_string_lists (&head, &tail, h, t);
20431 /* Append glyphs strings for glyphs following the last glyph
20432 string tail that are overwritten by tail. The background of
20433 these strings has to be drawn because tail's foreground draws
20434 over it. */
20435 i = right_overwritten (tail);
20436 if (i >= 0)
20438 enum draw_glyphs_face overlap_hl;
20440 if (check_mouse_face
20441 && mouse_beg_col < i && mouse_end_col > end)
20442 overlap_hl = DRAW_MOUSE_FACE;
20443 else
20444 overlap_hl = DRAW_NORMAL_TEXT;
20446 BUILD_GLYPH_STRINGS (end, i, h, t,
20447 overlap_hl, x, last_x);
20448 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20449 append_glyph_string_lists (&head, &tail, h, t);
20450 clip_tail = tail;
20453 /* Append glyph strings for glyphs following the last glyph
20454 string tail that overwrite tail. The foreground of such
20455 glyphs has to be drawn because it writes into the background
20456 of tail. The background must not be drawn because it could
20457 paint over the foreground of following glyphs. */
20458 i = right_overwriting (tail);
20459 if (i >= 0)
20461 enum draw_glyphs_face overlap_hl;
20462 if (check_mouse_face
20463 && mouse_beg_col < i && mouse_end_col > end)
20464 overlap_hl = DRAW_MOUSE_FACE;
20465 else
20466 overlap_hl = DRAW_NORMAL_TEXT;
20468 clip_tail = tail;
20469 i++; /* We must include the Ith glyph. */
20470 BUILD_GLYPH_STRINGS (end, i, h, t,
20471 overlap_hl, x, last_x);
20472 for (s = h; s; s = s->next)
20473 s->background_filled_p = 1;
20474 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20475 append_glyph_string_lists (&head, &tail, h, t);
20477 if (clip_head || clip_tail)
20478 for (s = head; s; s = s->next)
20480 s->clip_head = clip_head;
20481 s->clip_tail = clip_tail;
20485 /* Draw all strings. */
20486 for (s = head; s; s = s->next)
20487 FRAME_RIF (f)->draw_glyph_string (s);
20489 #ifndef HAVE_NS
20490 /* When focus a sole frame and move horizontally, this sets on_p to 0
20491 causing a failure to erase prev cursor position. */
20492 if (area == TEXT_AREA
20493 && !row->full_width_p
20494 /* When drawing overlapping rows, only the glyph strings'
20495 foreground is drawn, which doesn't erase a cursor
20496 completely. */
20497 && !overlaps)
20499 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20500 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20501 : (tail ? tail->x + tail->background_width : x));
20502 x0 -= area_left;
20503 x1 -= area_left;
20505 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20506 row->y, MATRIX_ROW_BOTTOM_Y (row));
20508 #endif
20510 /* Value is the x-position up to which drawn, relative to AREA of W.
20511 This doesn't include parts drawn because of overhangs. */
20512 if (row->full_width_p)
20513 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20514 else
20515 x_reached -= area_left;
20517 RELEASE_HDC (hdc, f);
20519 return x_reached;
20522 /* Expand row matrix if too narrow. Don't expand if area
20523 is not present. */
20525 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20527 if (!fonts_changed_p \
20528 && (it->glyph_row->glyphs[area] \
20529 < it->glyph_row->glyphs[area + 1])) \
20531 it->w->ncols_scale_factor++; \
20532 fonts_changed_p = 1; \
20536 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20537 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20539 static INLINE void
20540 append_glyph (it)
20541 struct it *it;
20543 struct glyph *glyph;
20544 enum glyph_row_area area = it->area;
20546 xassert (it->glyph_row);
20547 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20549 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20550 if (glyph < it->glyph_row->glyphs[area + 1])
20552 glyph->charpos = CHARPOS (it->position);
20553 glyph->object = it->object;
20554 if (it->pixel_width > 0)
20556 glyph->pixel_width = it->pixel_width;
20557 glyph->padding_p = 0;
20559 else
20561 /* Assure at least 1-pixel width. Otherwise, cursor can't
20562 be displayed correctly. */
20563 glyph->pixel_width = 1;
20564 glyph->padding_p = 1;
20566 glyph->ascent = it->ascent;
20567 glyph->descent = it->descent;
20568 glyph->voffset = it->voffset;
20569 glyph->type = CHAR_GLYPH;
20570 glyph->avoid_cursor_p = it->avoid_cursor_p;
20571 glyph->multibyte_p = it->multibyte_p;
20572 glyph->left_box_line_p = it->start_of_box_run_p;
20573 glyph->right_box_line_p = it->end_of_box_run_p;
20574 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20575 || it->phys_descent > it->descent);
20576 glyph->glyph_not_available_p = it->glyph_not_available_p;
20577 glyph->face_id = it->face_id;
20578 glyph->u.ch = it->char_to_display;
20579 glyph->slice = null_glyph_slice;
20580 glyph->font_type = FONT_TYPE_UNKNOWN;
20581 ++it->glyph_row->used[area];
20583 else
20584 IT_EXPAND_MATRIX_WIDTH (it, area);
20587 /* Store one glyph for the composition IT->cmp_it.id in
20588 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20589 non-null. */
20591 static INLINE void
20592 append_composite_glyph (it)
20593 struct it *it;
20595 struct glyph *glyph;
20596 enum glyph_row_area area = it->area;
20598 xassert (it->glyph_row);
20600 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20601 if (glyph < it->glyph_row->glyphs[area + 1])
20603 glyph->charpos = CHARPOS (it->position);
20604 glyph->object = it->object;
20605 glyph->pixel_width = it->pixel_width;
20606 glyph->ascent = it->ascent;
20607 glyph->descent = it->descent;
20608 glyph->voffset = it->voffset;
20609 glyph->type = COMPOSITE_GLYPH;
20610 if (it->cmp_it.ch < 0)
20612 glyph->u.cmp.automatic = 0;
20613 glyph->u.cmp.id = it->cmp_it.id;
20615 else
20617 glyph->u.cmp.automatic = 1;
20618 glyph->u.cmp.id = it->cmp_it.id;
20619 glyph->u.cmp.from = it->cmp_it.from;
20620 glyph->u.cmp.to = it->cmp_it.to - 1;
20622 glyph->avoid_cursor_p = it->avoid_cursor_p;
20623 glyph->multibyte_p = it->multibyte_p;
20624 glyph->left_box_line_p = it->start_of_box_run_p;
20625 glyph->right_box_line_p = it->end_of_box_run_p;
20626 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20627 || it->phys_descent > it->descent);
20628 glyph->padding_p = 0;
20629 glyph->glyph_not_available_p = 0;
20630 glyph->face_id = it->face_id;
20631 glyph->slice = null_glyph_slice;
20632 glyph->font_type = FONT_TYPE_UNKNOWN;
20633 ++it->glyph_row->used[area];
20635 else
20636 IT_EXPAND_MATRIX_WIDTH (it, area);
20640 /* Change IT->ascent and IT->height according to the setting of
20641 IT->voffset. */
20643 static INLINE void
20644 take_vertical_position_into_account (it)
20645 struct it *it;
20647 if (it->voffset)
20649 if (it->voffset < 0)
20650 /* Increase the ascent so that we can display the text higher
20651 in the line. */
20652 it->ascent -= it->voffset;
20653 else
20654 /* Increase the descent so that we can display the text lower
20655 in the line. */
20656 it->descent += it->voffset;
20661 /* Produce glyphs/get display metrics for the image IT is loaded with.
20662 See the description of struct display_iterator in dispextern.h for
20663 an overview of struct display_iterator. */
20665 static void
20666 produce_image_glyph (it)
20667 struct it *it;
20669 struct image *img;
20670 struct face *face;
20671 int glyph_ascent, crop;
20672 struct glyph_slice slice;
20674 xassert (it->what == IT_IMAGE);
20676 face = FACE_FROM_ID (it->f, it->face_id);
20677 xassert (face);
20678 /* Make sure X resources of the face is loaded. */
20679 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20681 if (it->image_id < 0)
20683 /* Fringe bitmap. */
20684 it->ascent = it->phys_ascent = 0;
20685 it->descent = it->phys_descent = 0;
20686 it->pixel_width = 0;
20687 it->nglyphs = 0;
20688 return;
20691 img = IMAGE_FROM_ID (it->f, it->image_id);
20692 xassert (img);
20693 /* Make sure X resources of the image is loaded. */
20694 prepare_image_for_display (it->f, img);
20696 slice.x = slice.y = 0;
20697 slice.width = img->width;
20698 slice.height = img->height;
20700 if (INTEGERP (it->slice.x))
20701 slice.x = XINT (it->slice.x);
20702 else if (FLOATP (it->slice.x))
20703 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20705 if (INTEGERP (it->slice.y))
20706 slice.y = XINT (it->slice.y);
20707 else if (FLOATP (it->slice.y))
20708 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20710 if (INTEGERP (it->slice.width))
20711 slice.width = XINT (it->slice.width);
20712 else if (FLOATP (it->slice.width))
20713 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20715 if (INTEGERP (it->slice.height))
20716 slice.height = XINT (it->slice.height);
20717 else if (FLOATP (it->slice.height))
20718 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20720 if (slice.x >= img->width)
20721 slice.x = img->width;
20722 if (slice.y >= img->height)
20723 slice.y = img->height;
20724 if (slice.x + slice.width >= img->width)
20725 slice.width = img->width - slice.x;
20726 if (slice.y + slice.height > img->height)
20727 slice.height = img->height - slice.y;
20729 if (slice.width == 0 || slice.height == 0)
20730 return;
20732 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20734 it->descent = slice.height - glyph_ascent;
20735 if (slice.y == 0)
20736 it->descent += img->vmargin;
20737 if (slice.y + slice.height == img->height)
20738 it->descent += img->vmargin;
20739 it->phys_descent = it->descent;
20741 it->pixel_width = slice.width;
20742 if (slice.x == 0)
20743 it->pixel_width += img->hmargin;
20744 if (slice.x + slice.width == img->width)
20745 it->pixel_width += img->hmargin;
20747 /* It's quite possible for images to have an ascent greater than
20748 their height, so don't get confused in that case. */
20749 if (it->descent < 0)
20750 it->descent = 0;
20752 it->nglyphs = 1;
20754 if (face->box != FACE_NO_BOX)
20756 if (face->box_line_width > 0)
20758 if (slice.y == 0)
20759 it->ascent += face->box_line_width;
20760 if (slice.y + slice.height == img->height)
20761 it->descent += face->box_line_width;
20764 if (it->start_of_box_run_p && slice.x == 0)
20765 it->pixel_width += eabs (face->box_line_width);
20766 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20767 it->pixel_width += eabs (face->box_line_width);
20770 take_vertical_position_into_account (it);
20772 /* Automatically crop wide image glyphs at right edge so we can
20773 draw the cursor on same display row. */
20774 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20775 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20777 it->pixel_width -= crop;
20778 slice.width -= crop;
20781 if (it->glyph_row)
20783 struct glyph *glyph;
20784 enum glyph_row_area area = it->area;
20786 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20787 if (glyph < it->glyph_row->glyphs[area + 1])
20789 glyph->charpos = CHARPOS (it->position);
20790 glyph->object = it->object;
20791 glyph->pixel_width = it->pixel_width;
20792 glyph->ascent = glyph_ascent;
20793 glyph->descent = it->descent;
20794 glyph->voffset = it->voffset;
20795 glyph->type = IMAGE_GLYPH;
20796 glyph->avoid_cursor_p = it->avoid_cursor_p;
20797 glyph->multibyte_p = it->multibyte_p;
20798 glyph->left_box_line_p = it->start_of_box_run_p;
20799 glyph->right_box_line_p = it->end_of_box_run_p;
20800 glyph->overlaps_vertically_p = 0;
20801 glyph->padding_p = 0;
20802 glyph->glyph_not_available_p = 0;
20803 glyph->face_id = it->face_id;
20804 glyph->u.img_id = img->id;
20805 glyph->slice = slice;
20806 glyph->font_type = FONT_TYPE_UNKNOWN;
20807 ++it->glyph_row->used[area];
20809 else
20810 IT_EXPAND_MATRIX_WIDTH (it, area);
20815 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20816 of the glyph, WIDTH and HEIGHT are the width and height of the
20817 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20819 static void
20820 append_stretch_glyph (it, object, width, height, ascent)
20821 struct it *it;
20822 Lisp_Object object;
20823 int width, height;
20824 int ascent;
20826 struct glyph *glyph;
20827 enum glyph_row_area area = it->area;
20829 xassert (ascent >= 0 && ascent <= height);
20831 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20832 if (glyph < it->glyph_row->glyphs[area + 1])
20834 glyph->charpos = CHARPOS (it->position);
20835 glyph->object = object;
20836 glyph->pixel_width = width;
20837 glyph->ascent = ascent;
20838 glyph->descent = height - ascent;
20839 glyph->voffset = it->voffset;
20840 glyph->type = STRETCH_GLYPH;
20841 glyph->avoid_cursor_p = it->avoid_cursor_p;
20842 glyph->multibyte_p = it->multibyte_p;
20843 glyph->left_box_line_p = it->start_of_box_run_p;
20844 glyph->right_box_line_p = it->end_of_box_run_p;
20845 glyph->overlaps_vertically_p = 0;
20846 glyph->padding_p = 0;
20847 glyph->glyph_not_available_p = 0;
20848 glyph->face_id = it->face_id;
20849 glyph->u.stretch.ascent = ascent;
20850 glyph->u.stretch.height = height;
20851 glyph->slice = null_glyph_slice;
20852 glyph->font_type = FONT_TYPE_UNKNOWN;
20853 ++it->glyph_row->used[area];
20855 else
20856 IT_EXPAND_MATRIX_WIDTH (it, area);
20860 /* Produce a stretch glyph for iterator IT. IT->object is the value
20861 of the glyph property displayed. The value must be a list
20862 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20863 being recognized:
20865 1. `:width WIDTH' specifies that the space should be WIDTH *
20866 canonical char width wide. WIDTH may be an integer or floating
20867 point number.
20869 2. `:relative-width FACTOR' specifies that the width of the stretch
20870 should be computed from the width of the first character having the
20871 `glyph' property, and should be FACTOR times that width.
20873 3. `:align-to HPOS' specifies that the space should be wide enough
20874 to reach HPOS, a value in canonical character units.
20876 Exactly one of the above pairs must be present.
20878 4. `:height HEIGHT' specifies that the height of the stretch produced
20879 should be HEIGHT, measured in canonical character units.
20881 5. `:relative-height FACTOR' specifies that the height of the
20882 stretch should be FACTOR times the height of the characters having
20883 the glyph property.
20885 Either none or exactly one of 4 or 5 must be present.
20887 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20888 of the stretch should be used for the ascent of the stretch.
20889 ASCENT must be in the range 0 <= ASCENT <= 100. */
20891 static void
20892 produce_stretch_glyph (it)
20893 struct it *it;
20895 /* (space :width WIDTH :height HEIGHT ...) */
20896 Lisp_Object prop, plist;
20897 int width = 0, height = 0, align_to = -1;
20898 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20899 int ascent = 0;
20900 double tem;
20901 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20902 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20904 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20906 /* List should start with `space'. */
20907 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20908 plist = XCDR (it->object);
20910 /* Compute the width of the stretch. */
20911 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20912 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20914 /* Absolute width `:width WIDTH' specified and valid. */
20915 zero_width_ok_p = 1;
20916 width = (int)tem;
20918 else if (prop = Fplist_get (plist, QCrelative_width),
20919 NUMVAL (prop) > 0)
20921 /* Relative width `:relative-width FACTOR' specified and valid.
20922 Compute the width of the characters having the `glyph'
20923 property. */
20924 struct it it2;
20925 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20927 it2 = *it;
20928 if (it->multibyte_p)
20930 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20931 - IT_BYTEPOS (*it));
20932 it2.c = STRING_CHAR_AND_LENGTH (p, it2.len);
20934 else
20935 it2.c = *p, it2.len = 1;
20937 it2.glyph_row = NULL;
20938 it2.what = IT_CHARACTER;
20939 x_produce_glyphs (&it2);
20940 width = NUMVAL (prop) * it2.pixel_width;
20942 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20943 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20945 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20946 align_to = (align_to < 0
20948 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20949 else if (align_to < 0)
20950 align_to = window_box_left_offset (it->w, TEXT_AREA);
20951 width = max (0, (int)tem + align_to - it->current_x);
20952 zero_width_ok_p = 1;
20954 else
20955 /* Nothing specified -> width defaults to canonical char width. */
20956 width = FRAME_COLUMN_WIDTH (it->f);
20958 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20959 width = 1;
20961 /* Compute height. */
20962 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20963 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20965 height = (int)tem;
20966 zero_height_ok_p = 1;
20968 else if (prop = Fplist_get (plist, QCrelative_height),
20969 NUMVAL (prop) > 0)
20970 height = FONT_HEIGHT (font) * NUMVAL (prop);
20971 else
20972 height = FONT_HEIGHT (font);
20974 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20975 height = 1;
20977 /* Compute percentage of height used for ascent. If
20978 `:ascent ASCENT' is present and valid, use that. Otherwise,
20979 derive the ascent from the font in use. */
20980 if (prop = Fplist_get (plist, QCascent),
20981 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20982 ascent = height * NUMVAL (prop) / 100.0;
20983 else if (!NILP (prop)
20984 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20985 ascent = min (max (0, (int)tem), height);
20986 else
20987 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20989 if (width > 0 && it->line_wrap != TRUNCATE
20990 && it->current_x + width > it->last_visible_x)
20991 width = it->last_visible_x - it->current_x - 1;
20993 if (width > 0 && height > 0 && it->glyph_row)
20995 Lisp_Object object = it->stack[it->sp - 1].string;
20996 if (!STRINGP (object))
20997 object = it->w->buffer;
20998 append_stretch_glyph (it, object, width, height, ascent);
21001 it->pixel_width = width;
21002 it->ascent = it->phys_ascent = ascent;
21003 it->descent = it->phys_descent = height - it->ascent;
21004 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21006 take_vertical_position_into_account (it);
21009 /* Calculate line-height and line-spacing properties.
21010 An integer value specifies explicit pixel value.
21011 A float value specifies relative value to current face height.
21012 A cons (float . face-name) specifies relative value to
21013 height of specified face font.
21015 Returns height in pixels, or nil. */
21018 static Lisp_Object
21019 calc_line_height_property (it, val, font, boff, override)
21020 struct it *it;
21021 Lisp_Object val;
21022 struct font *font;
21023 int boff, override;
21025 Lisp_Object face_name = Qnil;
21026 int ascent, descent, height;
21028 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21029 return val;
21031 if (CONSP (val))
21033 face_name = XCAR (val);
21034 val = XCDR (val);
21035 if (!NUMBERP (val))
21036 val = make_number (1);
21037 if (NILP (face_name))
21039 height = it->ascent + it->descent;
21040 goto scale;
21044 if (NILP (face_name))
21046 font = FRAME_FONT (it->f);
21047 boff = FRAME_BASELINE_OFFSET (it->f);
21049 else if (EQ (face_name, Qt))
21051 override = 0;
21053 else
21055 int face_id;
21056 struct face *face;
21058 face_id = lookup_named_face (it->f, face_name, 0);
21059 if (face_id < 0)
21060 return make_number (-1);
21062 face = FACE_FROM_ID (it->f, face_id);
21063 font = face->font;
21064 if (font == NULL)
21065 return make_number (-1);
21066 boff = font->baseline_offset;
21067 if (font->vertical_centering)
21068 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21071 ascent = FONT_BASE (font) + boff;
21072 descent = FONT_DESCENT (font) - boff;
21074 if (override)
21076 it->override_ascent = ascent;
21077 it->override_descent = descent;
21078 it->override_boff = boff;
21081 height = ascent + descent;
21083 scale:
21084 if (FLOATP (val))
21085 height = (int)(XFLOAT_DATA (val) * height);
21086 else if (INTEGERP (val))
21087 height *= XINT (val);
21089 return make_number (height);
21093 /* RIF:
21094 Produce glyphs/get display metrics for the display element IT is
21095 loaded with. See the description of struct it in dispextern.h
21096 for an overview of struct it. */
21098 void
21099 x_produce_glyphs (it)
21100 struct it *it;
21102 int extra_line_spacing = it->extra_line_spacing;
21104 it->glyph_not_available_p = 0;
21106 if (it->what == IT_CHARACTER)
21108 XChar2b char2b;
21109 struct font *font;
21110 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21111 struct font_metrics *pcm;
21112 int font_not_found_p;
21113 int boff; /* baseline offset */
21114 /* We may change it->multibyte_p upon unibyte<->multibyte
21115 conversion. So, save the current value now and restore it
21116 later.
21118 Note: It seems that we don't have to record multibyte_p in
21119 struct glyph because the character code itself tells whether
21120 or not the character is multibyte. Thus, in the future, we
21121 must consider eliminating the field `multibyte_p' in the
21122 struct glyph. */
21123 int saved_multibyte_p = it->multibyte_p;
21125 /* Maybe translate single-byte characters to multibyte, or the
21126 other way. */
21127 it->char_to_display = it->c;
21128 if (!ASCII_BYTE_P (it->c)
21129 && ! it->multibyte_p)
21131 if (SINGLE_BYTE_CHAR_P (it->c)
21132 && unibyte_display_via_language_environment)
21134 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
21136 /* get_next_display_element assures that this decoding
21137 never fails. */
21138 it->char_to_display = DECODE_CHAR (unibyte, it->c);
21139 it->multibyte_p = 1;
21140 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21141 -1, Qnil);
21142 face = FACE_FROM_ID (it->f, it->face_id);
21146 /* Get font to use. Encode IT->char_to_display. */
21147 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21148 &char2b, it->multibyte_p, 0);
21149 font = face->font;
21151 font_not_found_p = font == NULL;
21152 if (font_not_found_p)
21154 /* When no suitable font found, display an empty box based
21155 on the metrics of the font of the default face (or what
21156 remapped). */
21157 struct face *no_font_face
21158 = FACE_FROM_ID (it->f,
21159 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21160 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21161 font = no_font_face->font;
21162 boff = font->baseline_offset;
21164 else
21166 boff = font->baseline_offset;
21167 if (font->vertical_centering)
21168 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21171 if (it->char_to_display >= ' '
21172 && (!it->multibyte_p || it->char_to_display < 128))
21174 /* Either unibyte or ASCII. */
21175 int stretched_p;
21177 it->nglyphs = 1;
21179 pcm = get_per_char_metric (it->f, font, &char2b);
21181 if (it->override_ascent >= 0)
21183 it->ascent = it->override_ascent;
21184 it->descent = it->override_descent;
21185 boff = it->override_boff;
21187 else
21189 it->ascent = FONT_BASE (font) + boff;
21190 it->descent = FONT_DESCENT (font) - boff;
21193 if (pcm)
21195 it->phys_ascent = pcm->ascent + boff;
21196 it->phys_descent = pcm->descent - boff;
21197 it->pixel_width = pcm->width;
21199 else
21201 it->glyph_not_available_p = 1;
21202 it->phys_ascent = it->ascent;
21203 it->phys_descent = it->descent;
21204 it->pixel_width = FONT_WIDTH (font);
21207 if (it->constrain_row_ascent_descent_p)
21209 if (it->descent > it->max_descent)
21211 it->ascent += it->descent - it->max_descent;
21212 it->descent = it->max_descent;
21214 if (it->ascent > it->max_ascent)
21216 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21217 it->ascent = it->max_ascent;
21219 it->phys_ascent = min (it->phys_ascent, it->ascent);
21220 it->phys_descent = min (it->phys_descent, it->descent);
21221 extra_line_spacing = 0;
21224 /* If this is a space inside a region of text with
21225 `space-width' property, change its width. */
21226 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21227 if (stretched_p)
21228 it->pixel_width *= XFLOATINT (it->space_width);
21230 /* If face has a box, add the box thickness to the character
21231 height. If character has a box line to the left and/or
21232 right, add the box line width to the character's width. */
21233 if (face->box != FACE_NO_BOX)
21235 int thick = face->box_line_width;
21237 if (thick > 0)
21239 it->ascent += thick;
21240 it->descent += thick;
21242 else
21243 thick = -thick;
21245 if (it->start_of_box_run_p)
21246 it->pixel_width += thick;
21247 if (it->end_of_box_run_p)
21248 it->pixel_width += thick;
21251 /* If face has an overline, add the height of the overline
21252 (1 pixel) and a 1 pixel margin to the character height. */
21253 if (face->overline_p)
21254 it->ascent += overline_margin;
21256 if (it->constrain_row_ascent_descent_p)
21258 if (it->ascent > it->max_ascent)
21259 it->ascent = it->max_ascent;
21260 if (it->descent > it->max_descent)
21261 it->descent = it->max_descent;
21264 take_vertical_position_into_account (it);
21266 /* If we have to actually produce glyphs, do it. */
21267 if (it->glyph_row)
21269 if (stretched_p)
21271 /* Translate a space with a `space-width' property
21272 into a stretch glyph. */
21273 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21274 / FONT_HEIGHT (font));
21275 append_stretch_glyph (it, it->object, it->pixel_width,
21276 it->ascent + it->descent, ascent);
21278 else
21279 append_glyph (it);
21281 /* If characters with lbearing or rbearing are displayed
21282 in this line, record that fact in a flag of the
21283 glyph row. This is used to optimize X output code. */
21284 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21285 it->glyph_row->contains_overlapping_glyphs_p = 1;
21287 if (! stretched_p && it->pixel_width == 0)
21288 /* We assure that all visible glyphs have at least 1-pixel
21289 width. */
21290 it->pixel_width = 1;
21292 else if (it->char_to_display == '\n')
21294 /* A newline has no width, but we need the height of the
21295 line. But if previous part of the line sets a height,
21296 don't increase that height */
21298 Lisp_Object height;
21299 Lisp_Object total_height = Qnil;
21301 it->override_ascent = -1;
21302 it->pixel_width = 0;
21303 it->nglyphs = 0;
21305 height = get_it_property(it, Qline_height);
21306 /* Split (line-height total-height) list */
21307 if (CONSP (height)
21308 && CONSP (XCDR (height))
21309 && NILP (XCDR (XCDR (height))))
21311 total_height = XCAR (XCDR (height));
21312 height = XCAR (height);
21314 height = calc_line_height_property(it, height, font, boff, 1);
21316 if (it->override_ascent >= 0)
21318 it->ascent = it->override_ascent;
21319 it->descent = it->override_descent;
21320 boff = it->override_boff;
21322 else
21324 it->ascent = FONT_BASE (font) + boff;
21325 it->descent = FONT_DESCENT (font) - boff;
21328 if (EQ (height, Qt))
21330 if (it->descent > it->max_descent)
21332 it->ascent += it->descent - it->max_descent;
21333 it->descent = it->max_descent;
21335 if (it->ascent > it->max_ascent)
21337 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21338 it->ascent = it->max_ascent;
21340 it->phys_ascent = min (it->phys_ascent, it->ascent);
21341 it->phys_descent = min (it->phys_descent, it->descent);
21342 it->constrain_row_ascent_descent_p = 1;
21343 extra_line_spacing = 0;
21345 else
21347 Lisp_Object spacing;
21349 it->phys_ascent = it->ascent;
21350 it->phys_descent = it->descent;
21352 if ((it->max_ascent > 0 || it->max_descent > 0)
21353 && face->box != FACE_NO_BOX
21354 && face->box_line_width > 0)
21356 it->ascent += face->box_line_width;
21357 it->descent += face->box_line_width;
21359 if (!NILP (height)
21360 && XINT (height) > it->ascent + it->descent)
21361 it->ascent = XINT (height) - it->descent;
21363 if (!NILP (total_height))
21364 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21365 else
21367 spacing = get_it_property(it, Qline_spacing);
21368 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21370 if (INTEGERP (spacing))
21372 extra_line_spacing = XINT (spacing);
21373 if (!NILP (total_height))
21374 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21378 else if (it->char_to_display == '\t')
21380 if (font->space_width > 0)
21382 int tab_width = it->tab_width * font->space_width;
21383 int x = it->current_x + it->continuation_lines_width;
21384 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21386 /* If the distance from the current position to the next tab
21387 stop is less than a space character width, use the
21388 tab stop after that. */
21389 if (next_tab_x - x < font->space_width)
21390 next_tab_x += tab_width;
21392 it->pixel_width = next_tab_x - x;
21393 it->nglyphs = 1;
21394 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21395 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21397 if (it->glyph_row)
21399 append_stretch_glyph (it, it->object, it->pixel_width,
21400 it->ascent + it->descent, it->ascent);
21403 else
21405 it->pixel_width = 0;
21406 it->nglyphs = 1;
21409 else
21411 /* A multi-byte character. Assume that the display width of the
21412 character is the width of the character multiplied by the
21413 width of the font. */
21415 /* If we found a font, this font should give us the right
21416 metrics. If we didn't find a font, use the frame's
21417 default font and calculate the width of the character by
21418 multiplying the width of font by the width of the
21419 character. */
21421 pcm = get_per_char_metric (it->f, font, &char2b);
21423 if (font_not_found_p || !pcm)
21425 int char_width = CHAR_WIDTH (it->char_to_display);
21427 if (char_width == 0)
21428 /* This is a non spacing character. But, as we are
21429 going to display an empty box, the box must occupy
21430 at least one column. */
21431 char_width = 1;
21432 it->glyph_not_available_p = 1;
21433 it->pixel_width = font->space_width * char_width;
21434 it->phys_ascent = FONT_BASE (font) + boff;
21435 it->phys_descent = FONT_DESCENT (font) - boff;
21437 else
21439 it->pixel_width = pcm->width;
21440 it->phys_ascent = pcm->ascent + boff;
21441 it->phys_descent = pcm->descent - boff;
21442 if (it->glyph_row
21443 && (pcm->lbearing < 0
21444 || pcm->rbearing > pcm->width))
21445 it->glyph_row->contains_overlapping_glyphs_p = 1;
21447 it->nglyphs = 1;
21448 it->ascent = FONT_BASE (font) + boff;
21449 it->descent = FONT_DESCENT (font) - boff;
21450 if (face->box != FACE_NO_BOX)
21452 int thick = face->box_line_width;
21454 if (thick > 0)
21456 it->ascent += thick;
21457 it->descent += thick;
21459 else
21460 thick = - thick;
21462 if (it->start_of_box_run_p)
21463 it->pixel_width += thick;
21464 if (it->end_of_box_run_p)
21465 it->pixel_width += thick;
21468 /* If face has an overline, add the height of the overline
21469 (1 pixel) and a 1 pixel margin to the character height. */
21470 if (face->overline_p)
21471 it->ascent += overline_margin;
21473 take_vertical_position_into_account (it);
21475 if (it->ascent < 0)
21476 it->ascent = 0;
21477 if (it->descent < 0)
21478 it->descent = 0;
21480 if (it->glyph_row)
21481 append_glyph (it);
21482 if (it->pixel_width == 0)
21483 /* We assure that all visible glyphs have at least 1-pixel
21484 width. */
21485 it->pixel_width = 1;
21487 it->multibyte_p = saved_multibyte_p;
21489 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21491 /* A static composition.
21493 Note: A composition is represented as one glyph in the
21494 glyph matrix. There are no padding glyphs.
21496 Important note: pixel_width, ascent, and descent are the
21497 values of what is drawn by draw_glyphs (i.e. the values of
21498 the overall glyphs composed). */
21499 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21500 int boff; /* baseline offset */
21501 struct composition *cmp = composition_table[it->cmp_it.id];
21502 int glyph_len = cmp->glyph_len;
21503 struct font *font = face->font;
21505 it->nglyphs = 1;
21507 /* If we have not yet calculated pixel size data of glyphs of
21508 the composition for the current face font, calculate them
21509 now. Theoretically, we have to check all fonts for the
21510 glyphs, but that requires much time and memory space. So,
21511 here we check only the font of the first glyph. This may
21512 lead to incorrect display, but it's very rare, and C-l
21513 (recenter-top-bottom) can correct the display anyway. */
21514 if (! cmp->font || cmp->font != font)
21516 /* Ascent and descent of the font of the first character
21517 of this composition (adjusted by baseline offset).
21518 Ascent and descent of overall glyphs should not be less
21519 than these, respectively. */
21520 int font_ascent, font_descent, font_height;
21521 /* Bounding box of the overall glyphs. */
21522 int leftmost, rightmost, lowest, highest;
21523 int lbearing, rbearing;
21524 int i, width, ascent, descent;
21525 int left_padded = 0, right_padded = 0;
21526 int c;
21527 XChar2b char2b;
21528 struct font_metrics *pcm;
21529 int font_not_found_p;
21530 int pos;
21532 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21533 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21534 break;
21535 if (glyph_len < cmp->glyph_len)
21536 right_padded = 1;
21537 for (i = 0; i < glyph_len; i++)
21539 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21540 break;
21541 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21543 if (i > 0)
21544 left_padded = 1;
21546 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21547 : IT_CHARPOS (*it));
21548 /* If no suitable font is found, use the default font. */
21549 font_not_found_p = font == NULL;
21550 if (font_not_found_p)
21552 face = face->ascii_face;
21553 font = face->font;
21555 boff = font->baseline_offset;
21556 if (font->vertical_centering)
21557 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21558 font_ascent = FONT_BASE (font) + boff;
21559 font_descent = FONT_DESCENT (font) - boff;
21560 font_height = FONT_HEIGHT (font);
21562 cmp->font = (void *) font;
21564 pcm = NULL;
21565 if (! font_not_found_p)
21567 get_char_face_and_encoding (it->f, c, it->face_id,
21568 &char2b, it->multibyte_p, 0);
21569 pcm = get_per_char_metric (it->f, font, &char2b);
21572 /* Initialize the bounding box. */
21573 if (pcm)
21575 width = pcm->width;
21576 ascent = pcm->ascent;
21577 descent = pcm->descent;
21578 lbearing = pcm->lbearing;
21579 rbearing = pcm->rbearing;
21581 else
21583 width = FONT_WIDTH (font);
21584 ascent = FONT_BASE (font);
21585 descent = FONT_DESCENT (font);
21586 lbearing = 0;
21587 rbearing = width;
21590 rightmost = width;
21591 leftmost = 0;
21592 lowest = - descent + boff;
21593 highest = ascent + boff;
21595 if (! font_not_found_p
21596 && font->default_ascent
21597 && CHAR_TABLE_P (Vuse_default_ascent)
21598 && !NILP (Faref (Vuse_default_ascent,
21599 make_number (it->char_to_display))))
21600 highest = font->default_ascent + boff;
21602 /* Draw the first glyph at the normal position. It may be
21603 shifted to right later if some other glyphs are drawn
21604 at the left. */
21605 cmp->offsets[i * 2] = 0;
21606 cmp->offsets[i * 2 + 1] = boff;
21607 cmp->lbearing = lbearing;
21608 cmp->rbearing = rbearing;
21610 /* Set cmp->offsets for the remaining glyphs. */
21611 for (i++; i < glyph_len; i++)
21613 int left, right, btm, top;
21614 int ch = COMPOSITION_GLYPH (cmp, i);
21615 int face_id;
21616 struct face *this_face;
21617 int this_boff;
21619 if (ch == '\t')
21620 ch = ' ';
21621 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21622 this_face = FACE_FROM_ID (it->f, face_id);
21623 font = this_face->font;
21625 if (font == NULL)
21626 pcm = NULL;
21627 else
21629 this_boff = font->baseline_offset;
21630 if (font->vertical_centering)
21631 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21632 get_char_face_and_encoding (it->f, ch, face_id,
21633 &char2b, it->multibyte_p, 0);
21634 pcm = get_per_char_metric (it->f, font, &char2b);
21636 if (! pcm)
21637 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21638 else
21640 width = pcm->width;
21641 ascent = pcm->ascent;
21642 descent = pcm->descent;
21643 lbearing = pcm->lbearing;
21644 rbearing = pcm->rbearing;
21645 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21647 /* Relative composition with or without
21648 alternate chars. */
21649 left = (leftmost + rightmost - width) / 2;
21650 btm = - descent + boff;
21651 if (font->relative_compose
21652 && (! CHAR_TABLE_P (Vignore_relative_composition)
21653 || NILP (Faref (Vignore_relative_composition,
21654 make_number (ch)))))
21657 if (- descent >= font->relative_compose)
21658 /* One extra pixel between two glyphs. */
21659 btm = highest + 1;
21660 else if (ascent <= 0)
21661 /* One extra pixel between two glyphs. */
21662 btm = lowest - 1 - ascent - descent;
21665 else
21667 /* A composition rule is specified by an integer
21668 value that encodes global and new reference
21669 points (GREF and NREF). GREF and NREF are
21670 specified by numbers as below:
21672 0---1---2 -- ascent
21676 9--10--11 -- center
21678 ---3---4---5--- baseline
21680 6---7---8 -- descent
21682 int rule = COMPOSITION_RULE (cmp, i);
21683 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21685 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21686 grefx = gref % 3, nrefx = nref % 3;
21687 grefy = gref / 3, nrefy = nref / 3;
21688 if (xoff)
21689 xoff = font_height * (xoff - 128) / 256;
21690 if (yoff)
21691 yoff = font_height * (yoff - 128) / 256;
21693 left = (leftmost
21694 + grefx * (rightmost - leftmost) / 2
21695 - nrefx * width / 2
21696 + xoff);
21698 btm = ((grefy == 0 ? highest
21699 : grefy == 1 ? 0
21700 : grefy == 2 ? lowest
21701 : (highest + lowest) / 2)
21702 - (nrefy == 0 ? ascent + descent
21703 : nrefy == 1 ? descent - boff
21704 : nrefy == 2 ? 0
21705 : (ascent + descent) / 2)
21706 + yoff);
21709 cmp->offsets[i * 2] = left;
21710 cmp->offsets[i * 2 + 1] = btm + descent;
21712 /* Update the bounding box of the overall glyphs. */
21713 if (width > 0)
21715 right = left + width;
21716 if (left < leftmost)
21717 leftmost = left;
21718 if (right > rightmost)
21719 rightmost = right;
21721 top = btm + descent + ascent;
21722 if (top > highest)
21723 highest = top;
21724 if (btm < lowest)
21725 lowest = btm;
21727 if (cmp->lbearing > left + lbearing)
21728 cmp->lbearing = left + lbearing;
21729 if (cmp->rbearing < left + rbearing)
21730 cmp->rbearing = left + rbearing;
21734 /* If there are glyphs whose x-offsets are negative,
21735 shift all glyphs to the right and make all x-offsets
21736 non-negative. */
21737 if (leftmost < 0)
21739 for (i = 0; i < cmp->glyph_len; i++)
21740 cmp->offsets[i * 2] -= leftmost;
21741 rightmost -= leftmost;
21742 cmp->lbearing -= leftmost;
21743 cmp->rbearing -= leftmost;
21746 if (left_padded && cmp->lbearing < 0)
21748 for (i = 0; i < cmp->glyph_len; i++)
21749 cmp->offsets[i * 2] -= cmp->lbearing;
21750 rightmost -= cmp->lbearing;
21751 cmp->rbearing -= cmp->lbearing;
21752 cmp->lbearing = 0;
21754 if (right_padded && rightmost < cmp->rbearing)
21756 rightmost = cmp->rbearing;
21759 cmp->pixel_width = rightmost;
21760 cmp->ascent = highest;
21761 cmp->descent = - lowest;
21762 if (cmp->ascent < font_ascent)
21763 cmp->ascent = font_ascent;
21764 if (cmp->descent < font_descent)
21765 cmp->descent = font_descent;
21768 if (it->glyph_row
21769 && (cmp->lbearing < 0
21770 || cmp->rbearing > cmp->pixel_width))
21771 it->glyph_row->contains_overlapping_glyphs_p = 1;
21773 it->pixel_width = cmp->pixel_width;
21774 it->ascent = it->phys_ascent = cmp->ascent;
21775 it->descent = it->phys_descent = cmp->descent;
21776 if (face->box != FACE_NO_BOX)
21778 int thick = face->box_line_width;
21780 if (thick > 0)
21782 it->ascent += thick;
21783 it->descent += thick;
21785 else
21786 thick = - thick;
21788 if (it->start_of_box_run_p)
21789 it->pixel_width += thick;
21790 if (it->end_of_box_run_p)
21791 it->pixel_width += thick;
21794 /* If face has an overline, add the height of the overline
21795 (1 pixel) and a 1 pixel margin to the character height. */
21796 if (face->overline_p)
21797 it->ascent += overline_margin;
21799 take_vertical_position_into_account (it);
21800 if (it->ascent < 0)
21801 it->ascent = 0;
21802 if (it->descent < 0)
21803 it->descent = 0;
21805 if (it->glyph_row)
21806 append_composite_glyph (it);
21808 else if (it->what == IT_COMPOSITION)
21810 /* A dynamic (automatic) composition. */
21811 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21812 Lisp_Object gstring;
21813 struct font_metrics metrics;
21815 gstring = composition_gstring_from_id (it->cmp_it.id);
21816 it->pixel_width
21817 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21818 &metrics);
21819 if (it->glyph_row
21820 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21821 it->glyph_row->contains_overlapping_glyphs_p = 1;
21822 it->ascent = it->phys_ascent = metrics.ascent;
21823 it->descent = it->phys_descent = metrics.descent;
21824 if (face->box != FACE_NO_BOX)
21826 int thick = face->box_line_width;
21828 if (thick > 0)
21830 it->ascent += thick;
21831 it->descent += thick;
21833 else
21834 thick = - thick;
21836 if (it->start_of_box_run_p)
21837 it->pixel_width += thick;
21838 if (it->end_of_box_run_p)
21839 it->pixel_width += thick;
21841 /* If face has an overline, add the height of the overline
21842 (1 pixel) and a 1 pixel margin to the character height. */
21843 if (face->overline_p)
21844 it->ascent += overline_margin;
21845 take_vertical_position_into_account (it);
21846 if (it->ascent < 0)
21847 it->ascent = 0;
21848 if (it->descent < 0)
21849 it->descent = 0;
21851 if (it->glyph_row)
21852 append_composite_glyph (it);
21854 else if (it->what == IT_IMAGE)
21855 produce_image_glyph (it);
21856 else if (it->what == IT_STRETCH)
21857 produce_stretch_glyph (it);
21859 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21860 because this isn't true for images with `:ascent 100'. */
21861 xassert (it->ascent >= 0 && it->descent >= 0);
21862 if (it->area == TEXT_AREA)
21863 it->current_x += it->pixel_width;
21865 if (extra_line_spacing > 0)
21867 it->descent += extra_line_spacing;
21868 if (extra_line_spacing > it->max_extra_line_spacing)
21869 it->max_extra_line_spacing = extra_line_spacing;
21872 it->max_ascent = max (it->max_ascent, it->ascent);
21873 it->max_descent = max (it->max_descent, it->descent);
21874 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21875 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21878 /* EXPORT for RIF:
21879 Output LEN glyphs starting at START at the nominal cursor position.
21880 Advance the nominal cursor over the text. The global variable
21881 updated_window contains the window being updated, updated_row is
21882 the glyph row being updated, and updated_area is the area of that
21883 row being updated. */
21885 void
21886 x_write_glyphs (start, len)
21887 struct glyph *start;
21888 int len;
21890 int x, hpos;
21892 xassert (updated_window && updated_row);
21893 BLOCK_INPUT;
21895 /* Write glyphs. */
21897 hpos = start - updated_row->glyphs[updated_area];
21898 x = draw_glyphs (updated_window, output_cursor.x,
21899 updated_row, updated_area,
21900 hpos, hpos + len,
21901 DRAW_NORMAL_TEXT, 0);
21903 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21904 if (updated_area == TEXT_AREA
21905 && updated_window->phys_cursor_on_p
21906 && updated_window->phys_cursor.vpos == output_cursor.vpos
21907 && updated_window->phys_cursor.hpos >= hpos
21908 && updated_window->phys_cursor.hpos < hpos + len)
21909 updated_window->phys_cursor_on_p = 0;
21911 UNBLOCK_INPUT;
21913 /* Advance the output cursor. */
21914 output_cursor.hpos += len;
21915 output_cursor.x = x;
21919 /* EXPORT for RIF:
21920 Insert LEN glyphs from START at the nominal cursor position. */
21922 void
21923 x_insert_glyphs (start, len)
21924 struct glyph *start;
21925 int len;
21927 struct frame *f;
21928 struct window *w;
21929 int line_height, shift_by_width, shifted_region_width;
21930 struct glyph_row *row;
21931 struct glyph *glyph;
21932 int frame_x, frame_y;
21933 EMACS_INT hpos;
21935 xassert (updated_window && updated_row);
21936 BLOCK_INPUT;
21937 w = updated_window;
21938 f = XFRAME (WINDOW_FRAME (w));
21940 /* Get the height of the line we are in. */
21941 row = updated_row;
21942 line_height = row->height;
21944 /* Get the width of the glyphs to insert. */
21945 shift_by_width = 0;
21946 for (glyph = start; glyph < start + len; ++glyph)
21947 shift_by_width += glyph->pixel_width;
21949 /* Get the width of the region to shift right. */
21950 shifted_region_width = (window_box_width (w, updated_area)
21951 - output_cursor.x
21952 - shift_by_width);
21954 /* Shift right. */
21955 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21956 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21958 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21959 line_height, shift_by_width);
21961 /* Write the glyphs. */
21962 hpos = start - row->glyphs[updated_area];
21963 draw_glyphs (w, output_cursor.x, row, updated_area,
21964 hpos, hpos + len,
21965 DRAW_NORMAL_TEXT, 0);
21967 /* Advance the output cursor. */
21968 output_cursor.hpos += len;
21969 output_cursor.x += shift_by_width;
21970 UNBLOCK_INPUT;
21974 /* EXPORT for RIF:
21975 Erase the current text line from the nominal cursor position
21976 (inclusive) to pixel column TO_X (exclusive). The idea is that
21977 everything from TO_X onward is already erased.
21979 TO_X is a pixel position relative to updated_area of
21980 updated_window. TO_X == -1 means clear to the end of this area. */
21982 void
21983 x_clear_end_of_line (to_x)
21984 int to_x;
21986 struct frame *f;
21987 struct window *w = updated_window;
21988 int max_x, min_y, max_y;
21989 int from_x, from_y, to_y;
21991 xassert (updated_window && updated_row);
21992 f = XFRAME (w->frame);
21994 if (updated_row->full_width_p)
21995 max_x = WINDOW_TOTAL_WIDTH (w);
21996 else
21997 max_x = window_box_width (w, updated_area);
21998 max_y = window_text_bottom_y (w);
22000 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
22001 of window. For TO_X > 0, truncate to end of drawing area. */
22002 if (to_x == 0)
22003 return;
22004 else if (to_x < 0)
22005 to_x = max_x;
22006 else
22007 to_x = min (to_x, max_x);
22009 to_y = min (max_y, output_cursor.y + updated_row->height);
22011 /* Notice if the cursor will be cleared by this operation. */
22012 if (!updated_row->full_width_p)
22013 notice_overwritten_cursor (w, updated_area,
22014 output_cursor.x, -1,
22015 updated_row->y,
22016 MATRIX_ROW_BOTTOM_Y (updated_row));
22018 from_x = output_cursor.x;
22020 /* Translate to frame coordinates. */
22021 if (updated_row->full_width_p)
22023 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
22024 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
22026 else
22028 int area_left = window_box_left (w, updated_area);
22029 from_x += area_left;
22030 to_x += area_left;
22033 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
22034 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
22035 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
22037 /* Prevent inadvertently clearing to end of the X window. */
22038 if (to_x > from_x && to_y > from_y)
22040 BLOCK_INPUT;
22041 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
22042 to_x - from_x, to_y - from_y);
22043 UNBLOCK_INPUT;
22047 #endif /* HAVE_WINDOW_SYSTEM */
22051 /***********************************************************************
22052 Cursor types
22053 ***********************************************************************/
22055 /* Value is the internal representation of the specified cursor type
22056 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
22057 of the bar cursor. */
22059 static enum text_cursor_kinds
22060 get_specified_cursor_type (arg, width)
22061 Lisp_Object arg;
22062 int *width;
22064 enum text_cursor_kinds type;
22066 if (NILP (arg))
22067 return NO_CURSOR;
22069 if (EQ (arg, Qbox))
22070 return FILLED_BOX_CURSOR;
22072 if (EQ (arg, Qhollow))
22073 return HOLLOW_BOX_CURSOR;
22075 if (EQ (arg, Qbar))
22077 *width = 2;
22078 return BAR_CURSOR;
22081 if (CONSP (arg)
22082 && EQ (XCAR (arg), Qbar)
22083 && INTEGERP (XCDR (arg))
22084 && XINT (XCDR (arg)) >= 0)
22086 *width = XINT (XCDR (arg));
22087 return BAR_CURSOR;
22090 if (EQ (arg, Qhbar))
22092 *width = 2;
22093 return HBAR_CURSOR;
22096 if (CONSP (arg)
22097 && EQ (XCAR (arg), Qhbar)
22098 && INTEGERP (XCDR (arg))
22099 && XINT (XCDR (arg)) >= 0)
22101 *width = XINT (XCDR (arg));
22102 return HBAR_CURSOR;
22105 /* Treat anything unknown as "hollow box cursor".
22106 It was bad to signal an error; people have trouble fixing
22107 .Xdefaults with Emacs, when it has something bad in it. */
22108 type = HOLLOW_BOX_CURSOR;
22110 return type;
22113 /* Set the default cursor types for specified frame. */
22114 void
22115 set_frame_cursor_types (f, arg)
22116 struct frame *f;
22117 Lisp_Object arg;
22119 int width;
22120 Lisp_Object tem;
22122 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22123 FRAME_CURSOR_WIDTH (f) = width;
22125 /* By default, set up the blink-off state depending on the on-state. */
22127 tem = Fassoc (arg, Vblink_cursor_alist);
22128 if (!NILP (tem))
22130 FRAME_BLINK_OFF_CURSOR (f)
22131 = get_specified_cursor_type (XCDR (tem), &width);
22132 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22134 else
22135 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22139 /* Return the cursor we want to be displayed in window W. Return
22140 width of bar/hbar cursor through WIDTH arg. Return with
22141 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22142 (i.e. if the `system caret' should track this cursor).
22144 In a mini-buffer window, we want the cursor only to appear if we
22145 are reading input from this window. For the selected window, we
22146 want the cursor type given by the frame parameter or buffer local
22147 setting of cursor-type. If explicitly marked off, draw no cursor.
22148 In all other cases, we want a hollow box cursor. */
22150 static enum text_cursor_kinds
22151 get_window_cursor_type (w, glyph, width, active_cursor)
22152 struct window *w;
22153 struct glyph *glyph;
22154 int *width;
22155 int *active_cursor;
22157 struct frame *f = XFRAME (w->frame);
22158 struct buffer *b = XBUFFER (w->buffer);
22159 int cursor_type = DEFAULT_CURSOR;
22160 Lisp_Object alt_cursor;
22161 int non_selected = 0;
22163 *active_cursor = 1;
22165 /* Echo area */
22166 if (cursor_in_echo_area
22167 && FRAME_HAS_MINIBUF_P (f)
22168 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22170 if (w == XWINDOW (echo_area_window))
22172 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22174 *width = FRAME_CURSOR_WIDTH (f);
22175 return FRAME_DESIRED_CURSOR (f);
22177 else
22178 return get_specified_cursor_type (b->cursor_type, width);
22181 *active_cursor = 0;
22182 non_selected = 1;
22185 /* Detect a nonselected window or nonselected frame. */
22186 else if (w != XWINDOW (f->selected_window)
22187 #ifdef HAVE_WINDOW_SYSTEM
22188 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22189 #endif
22192 *active_cursor = 0;
22194 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22195 return NO_CURSOR;
22197 non_selected = 1;
22200 /* Never display a cursor in a window in which cursor-type is nil. */
22201 if (NILP (b->cursor_type))
22202 return NO_CURSOR;
22204 /* Get the normal cursor type for this window. */
22205 if (EQ (b->cursor_type, Qt))
22207 cursor_type = FRAME_DESIRED_CURSOR (f);
22208 *width = FRAME_CURSOR_WIDTH (f);
22210 else
22211 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22213 /* Use cursor-in-non-selected-windows instead
22214 for non-selected window or frame. */
22215 if (non_selected)
22217 alt_cursor = b->cursor_in_non_selected_windows;
22218 if (!EQ (Qt, alt_cursor))
22219 return get_specified_cursor_type (alt_cursor, width);
22220 /* t means modify the normal cursor type. */
22221 if (cursor_type == FILLED_BOX_CURSOR)
22222 cursor_type = HOLLOW_BOX_CURSOR;
22223 else if (cursor_type == BAR_CURSOR && *width > 1)
22224 --*width;
22225 return cursor_type;
22228 /* Use normal cursor if not blinked off. */
22229 if (!w->cursor_off_p)
22231 #ifdef HAVE_WINDOW_SYSTEM
22232 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22234 if (cursor_type == FILLED_BOX_CURSOR)
22236 /* Using a block cursor on large images can be very annoying.
22237 So use a hollow cursor for "large" images.
22238 If image is not transparent (no mask), also use hollow cursor. */
22239 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22240 if (img != NULL && IMAGEP (img->spec))
22242 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22243 where N = size of default frame font size.
22244 This should cover most of the "tiny" icons people may use. */
22245 if (!img->mask
22246 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22247 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22248 cursor_type = HOLLOW_BOX_CURSOR;
22251 else if (cursor_type != NO_CURSOR)
22253 /* Display current only supports BOX and HOLLOW cursors for images.
22254 So for now, unconditionally use a HOLLOW cursor when cursor is
22255 not a solid box cursor. */
22256 cursor_type = HOLLOW_BOX_CURSOR;
22259 #endif
22260 return cursor_type;
22263 /* Cursor is blinked off, so determine how to "toggle" it. */
22265 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22266 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22267 return get_specified_cursor_type (XCDR (alt_cursor), width);
22269 /* Then see if frame has specified a specific blink off cursor type. */
22270 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22272 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22273 return FRAME_BLINK_OFF_CURSOR (f);
22276 #if 0
22277 /* Some people liked having a permanently visible blinking cursor,
22278 while others had very strong opinions against it. So it was
22279 decided to remove it. KFS 2003-09-03 */
22281 /* Finally perform built-in cursor blinking:
22282 filled box <-> hollow box
22283 wide [h]bar <-> narrow [h]bar
22284 narrow [h]bar <-> no cursor
22285 other type <-> no cursor */
22287 if (cursor_type == FILLED_BOX_CURSOR)
22288 return HOLLOW_BOX_CURSOR;
22290 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22292 *width = 1;
22293 return cursor_type;
22295 #endif
22297 return NO_CURSOR;
22301 #ifdef HAVE_WINDOW_SYSTEM
22303 /* Notice when the text cursor of window W has been completely
22304 overwritten by a drawing operation that outputs glyphs in AREA
22305 starting at X0 and ending at X1 in the line starting at Y0 and
22306 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22307 the rest of the line after X0 has been written. Y coordinates
22308 are window-relative. */
22310 static void
22311 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22312 struct window *w;
22313 enum glyph_row_area area;
22314 int x0, y0, x1, y1;
22316 int cx0, cx1, cy0, cy1;
22317 struct glyph_row *row;
22319 if (!w->phys_cursor_on_p)
22320 return;
22321 if (area != TEXT_AREA)
22322 return;
22324 if (w->phys_cursor.vpos < 0
22325 || w->phys_cursor.vpos >= w->current_matrix->nrows
22326 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22327 !(row->enabled_p && row->displays_text_p)))
22328 return;
22330 if (row->cursor_in_fringe_p)
22332 row->cursor_in_fringe_p = 0;
22333 draw_fringe_bitmap (w, row, 0);
22334 w->phys_cursor_on_p = 0;
22335 return;
22338 cx0 = w->phys_cursor.x;
22339 cx1 = cx0 + w->phys_cursor_width;
22340 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22341 return;
22343 /* The cursor image will be completely removed from the
22344 screen if the output area intersects the cursor area in
22345 y-direction. When we draw in [y0 y1[, and some part of
22346 the cursor is at y < y0, that part must have been drawn
22347 before. When scrolling, the cursor is erased before
22348 actually scrolling, so we don't come here. When not
22349 scrolling, the rows above the old cursor row must have
22350 changed, and in this case these rows must have written
22351 over the cursor image.
22353 Likewise if part of the cursor is below y1, with the
22354 exception of the cursor being in the first blank row at
22355 the buffer and window end because update_text_area
22356 doesn't draw that row. (Except when it does, but
22357 that's handled in update_text_area.) */
22359 cy0 = w->phys_cursor.y;
22360 cy1 = cy0 + w->phys_cursor_height;
22361 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22362 return;
22364 w->phys_cursor_on_p = 0;
22367 #endif /* HAVE_WINDOW_SYSTEM */
22370 /************************************************************************
22371 Mouse Face
22372 ************************************************************************/
22374 #ifdef HAVE_WINDOW_SYSTEM
22376 /* EXPORT for RIF:
22377 Fix the display of area AREA of overlapping row ROW in window W
22378 with respect to the overlapping part OVERLAPS. */
22380 void
22381 x_fix_overlapping_area (w, row, area, overlaps)
22382 struct window *w;
22383 struct glyph_row *row;
22384 enum glyph_row_area area;
22385 int overlaps;
22387 int i, x;
22389 BLOCK_INPUT;
22391 x = 0;
22392 for (i = 0; i < row->used[area];)
22394 if (row->glyphs[area][i].overlaps_vertically_p)
22396 int start = i, start_x = x;
22400 x += row->glyphs[area][i].pixel_width;
22401 ++i;
22403 while (i < row->used[area]
22404 && row->glyphs[area][i].overlaps_vertically_p);
22406 draw_glyphs (w, start_x, row, area,
22407 start, i,
22408 DRAW_NORMAL_TEXT, overlaps);
22410 else
22412 x += row->glyphs[area][i].pixel_width;
22413 ++i;
22417 UNBLOCK_INPUT;
22421 /* EXPORT:
22422 Draw the cursor glyph of window W in glyph row ROW. See the
22423 comment of draw_glyphs for the meaning of HL. */
22425 void
22426 draw_phys_cursor_glyph (w, row, hl)
22427 struct window *w;
22428 struct glyph_row *row;
22429 enum draw_glyphs_face hl;
22431 /* If cursor hpos is out of bounds, don't draw garbage. This can
22432 happen in mini-buffer windows when switching between echo area
22433 glyphs and mini-buffer. */
22434 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22436 int on_p = w->phys_cursor_on_p;
22437 int x1;
22438 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22439 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22440 hl, 0);
22441 w->phys_cursor_on_p = on_p;
22443 if (hl == DRAW_CURSOR)
22444 w->phys_cursor_width = x1 - w->phys_cursor.x;
22445 /* When we erase the cursor, and ROW is overlapped by other
22446 rows, make sure that these overlapping parts of other rows
22447 are redrawn. */
22448 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22450 w->phys_cursor_width = x1 - w->phys_cursor.x;
22452 if (row > w->current_matrix->rows
22453 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22454 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22455 OVERLAPS_ERASED_CURSOR);
22457 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22458 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22459 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22460 OVERLAPS_ERASED_CURSOR);
22466 /* EXPORT:
22467 Erase the image of a cursor of window W from the screen. */
22469 void
22470 erase_phys_cursor (w)
22471 struct window *w;
22473 struct frame *f = XFRAME (w->frame);
22474 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22475 int hpos = w->phys_cursor.hpos;
22476 int vpos = w->phys_cursor.vpos;
22477 int mouse_face_here_p = 0;
22478 struct glyph_matrix *active_glyphs = w->current_matrix;
22479 struct glyph_row *cursor_row;
22480 struct glyph *cursor_glyph;
22481 enum draw_glyphs_face hl;
22483 /* No cursor displayed or row invalidated => nothing to do on the
22484 screen. */
22485 if (w->phys_cursor_type == NO_CURSOR)
22486 goto mark_cursor_off;
22488 /* VPOS >= active_glyphs->nrows means that window has been resized.
22489 Don't bother to erase the cursor. */
22490 if (vpos >= active_glyphs->nrows)
22491 goto mark_cursor_off;
22493 /* If row containing cursor is marked invalid, there is nothing we
22494 can do. */
22495 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22496 if (!cursor_row->enabled_p)
22497 goto mark_cursor_off;
22499 /* If line spacing is > 0, old cursor may only be partially visible in
22500 window after split-window. So adjust visible height. */
22501 cursor_row->visible_height = min (cursor_row->visible_height,
22502 window_text_bottom_y (w) - cursor_row->y);
22504 /* If row is completely invisible, don't attempt to delete a cursor which
22505 isn't there. This can happen if cursor is at top of a window, and
22506 we switch to a buffer with a header line in that window. */
22507 if (cursor_row->visible_height <= 0)
22508 goto mark_cursor_off;
22510 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22511 if (cursor_row->cursor_in_fringe_p)
22513 cursor_row->cursor_in_fringe_p = 0;
22514 draw_fringe_bitmap (w, cursor_row, 0);
22515 goto mark_cursor_off;
22518 /* This can happen when the new row is shorter than the old one.
22519 In this case, either draw_glyphs or clear_end_of_line
22520 should have cleared the cursor. Note that we wouldn't be
22521 able to erase the cursor in this case because we don't have a
22522 cursor glyph at hand. */
22523 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22524 goto mark_cursor_off;
22526 /* If the cursor is in the mouse face area, redisplay that when
22527 we clear the cursor. */
22528 if (! NILP (dpyinfo->mouse_face_window)
22529 && w == XWINDOW (dpyinfo->mouse_face_window)
22530 && (vpos > dpyinfo->mouse_face_beg_row
22531 || (vpos == dpyinfo->mouse_face_beg_row
22532 && hpos >= dpyinfo->mouse_face_beg_col))
22533 && (vpos < dpyinfo->mouse_face_end_row
22534 || (vpos == dpyinfo->mouse_face_end_row
22535 && hpos < dpyinfo->mouse_face_end_col))
22536 /* Don't redraw the cursor's spot in mouse face if it is at the
22537 end of a line (on a newline). The cursor appears there, but
22538 mouse highlighting does not. */
22539 && cursor_row->used[TEXT_AREA] > hpos)
22540 mouse_face_here_p = 1;
22542 /* Maybe clear the display under the cursor. */
22543 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22545 int x, y, left_x;
22546 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22547 int width;
22549 cursor_glyph = get_phys_cursor_glyph (w);
22550 if (cursor_glyph == NULL)
22551 goto mark_cursor_off;
22553 width = cursor_glyph->pixel_width;
22554 left_x = window_box_left_offset (w, TEXT_AREA);
22555 x = w->phys_cursor.x;
22556 if (x < left_x)
22557 width -= left_x - x;
22558 width = min (width, window_box_width (w, TEXT_AREA) - x);
22559 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22560 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22562 if (width > 0)
22563 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22566 /* Erase the cursor by redrawing the character underneath it. */
22567 if (mouse_face_here_p)
22568 hl = DRAW_MOUSE_FACE;
22569 else
22570 hl = DRAW_NORMAL_TEXT;
22571 draw_phys_cursor_glyph (w, cursor_row, hl);
22573 mark_cursor_off:
22574 w->phys_cursor_on_p = 0;
22575 w->phys_cursor_type = NO_CURSOR;
22579 /* EXPORT:
22580 Display or clear cursor of window W. If ON is zero, clear the
22581 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22582 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22584 void
22585 display_and_set_cursor (w, on, hpos, vpos, x, y)
22586 struct window *w;
22587 int on, hpos, vpos, x, y;
22589 struct frame *f = XFRAME (w->frame);
22590 int new_cursor_type;
22591 int new_cursor_width;
22592 int active_cursor;
22593 struct glyph_row *glyph_row;
22594 struct glyph *glyph;
22596 /* This is pointless on invisible frames, and dangerous on garbaged
22597 windows and frames; in the latter case, the frame or window may
22598 be in the midst of changing its size, and x and y may be off the
22599 window. */
22600 if (! FRAME_VISIBLE_P (f)
22601 || FRAME_GARBAGED_P (f)
22602 || vpos >= w->current_matrix->nrows
22603 || hpos >= w->current_matrix->matrix_w)
22604 return;
22606 /* If cursor is off and we want it off, return quickly. */
22607 if (!on && !w->phys_cursor_on_p)
22608 return;
22610 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22611 /* If cursor row is not enabled, we don't really know where to
22612 display the cursor. */
22613 if (!glyph_row->enabled_p)
22615 w->phys_cursor_on_p = 0;
22616 return;
22619 glyph = NULL;
22620 if (!glyph_row->exact_window_width_line_p
22621 || hpos < glyph_row->used[TEXT_AREA])
22622 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22624 xassert (interrupt_input_blocked);
22626 /* Set new_cursor_type to the cursor we want to be displayed. */
22627 new_cursor_type = get_window_cursor_type (w, glyph,
22628 &new_cursor_width, &active_cursor);
22630 /* If cursor is currently being shown and we don't want it to be or
22631 it is in the wrong place, or the cursor type is not what we want,
22632 erase it. */
22633 if (w->phys_cursor_on_p
22634 && (!on
22635 || w->phys_cursor.x != x
22636 || w->phys_cursor.y != y
22637 || new_cursor_type != w->phys_cursor_type
22638 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22639 && new_cursor_width != w->phys_cursor_width)))
22640 erase_phys_cursor (w);
22642 /* Don't check phys_cursor_on_p here because that flag is only set
22643 to zero in some cases where we know that the cursor has been
22644 completely erased, to avoid the extra work of erasing the cursor
22645 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22646 still not be visible, or it has only been partly erased. */
22647 if (on)
22649 w->phys_cursor_ascent = glyph_row->ascent;
22650 w->phys_cursor_height = glyph_row->height;
22652 /* Set phys_cursor_.* before x_draw_.* is called because some
22653 of them may need the information. */
22654 w->phys_cursor.x = x;
22655 w->phys_cursor.y = glyph_row->y;
22656 w->phys_cursor.hpos = hpos;
22657 w->phys_cursor.vpos = vpos;
22660 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22661 new_cursor_type, new_cursor_width,
22662 on, active_cursor);
22666 /* Switch the display of W's cursor on or off, according to the value
22667 of ON. */
22669 #ifndef HAVE_NS
22670 static
22671 #endif
22672 void
22673 update_window_cursor (w, on)
22674 struct window *w;
22675 int on;
22677 /* Don't update cursor in windows whose frame is in the process
22678 of being deleted. */
22679 if (w->current_matrix)
22681 BLOCK_INPUT;
22682 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22683 w->phys_cursor.x, w->phys_cursor.y);
22684 UNBLOCK_INPUT;
22689 /* Call update_window_cursor with parameter ON_P on all leaf windows
22690 in the window tree rooted at W. */
22692 static void
22693 update_cursor_in_window_tree (w, on_p)
22694 struct window *w;
22695 int on_p;
22697 while (w)
22699 if (!NILP (w->hchild))
22700 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22701 else if (!NILP (w->vchild))
22702 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22703 else
22704 update_window_cursor (w, on_p);
22706 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22711 /* EXPORT:
22712 Display the cursor on window W, or clear it, according to ON_P.
22713 Don't change the cursor's position. */
22715 void
22716 x_update_cursor (f, on_p)
22717 struct frame *f;
22718 int on_p;
22720 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22724 /* EXPORT:
22725 Clear the cursor of window W to background color, and mark the
22726 cursor as not shown. This is used when the text where the cursor
22727 is about to be rewritten. */
22729 void
22730 x_clear_cursor (w)
22731 struct window *w;
22733 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22734 update_window_cursor (w, 0);
22738 /* EXPORT:
22739 Display the active region described by mouse_face_* according to DRAW. */
22741 void
22742 show_mouse_face (dpyinfo, draw)
22743 Display_Info *dpyinfo;
22744 enum draw_glyphs_face draw;
22746 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22747 struct frame *f = XFRAME (WINDOW_FRAME (w));
22749 if (/* If window is in the process of being destroyed, don't bother
22750 to do anything. */
22751 w->current_matrix != NULL
22752 /* Don't update mouse highlight if hidden */
22753 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22754 /* Recognize when we are called to operate on rows that don't exist
22755 anymore. This can happen when a window is split. */
22756 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22758 int phys_cursor_on_p = w->phys_cursor_on_p;
22759 struct glyph_row *row, *first, *last;
22761 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22762 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22764 for (row = first; row <= last && row->enabled_p; ++row)
22766 int start_hpos, end_hpos, start_x;
22768 /* For all but the first row, the highlight starts at column 0. */
22769 if (row == first)
22771 start_hpos = dpyinfo->mouse_face_beg_col;
22772 start_x = dpyinfo->mouse_face_beg_x;
22774 else
22776 start_hpos = 0;
22777 start_x = 0;
22780 if (row == last)
22781 end_hpos = dpyinfo->mouse_face_end_col;
22782 else
22784 end_hpos = row->used[TEXT_AREA];
22785 if (draw == DRAW_NORMAL_TEXT)
22786 row->fill_line_p = 1; /* Clear to end of line */
22789 if (end_hpos > start_hpos)
22791 draw_glyphs (w, start_x, row, TEXT_AREA,
22792 start_hpos, end_hpos,
22793 draw, 0);
22795 row->mouse_face_p
22796 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22800 /* When we've written over the cursor, arrange for it to
22801 be displayed again. */
22802 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22804 BLOCK_INPUT;
22805 display_and_set_cursor (w, 1,
22806 w->phys_cursor.hpos, w->phys_cursor.vpos,
22807 w->phys_cursor.x, w->phys_cursor.y);
22808 UNBLOCK_INPUT;
22812 /* Change the mouse cursor. */
22813 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22814 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22815 else if (draw == DRAW_MOUSE_FACE)
22816 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22817 else
22818 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22821 /* EXPORT:
22822 Clear out the mouse-highlighted active region.
22823 Redraw it un-highlighted first. Value is non-zero if mouse
22824 face was actually drawn unhighlighted. */
22827 clear_mouse_face (dpyinfo)
22828 Display_Info *dpyinfo;
22830 int cleared = 0;
22832 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22834 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22835 cleared = 1;
22838 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22839 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22840 dpyinfo->mouse_face_window = Qnil;
22841 dpyinfo->mouse_face_overlay = Qnil;
22842 return cleared;
22846 /* EXPORT:
22847 Non-zero if physical cursor of window W is within mouse face. */
22850 cursor_in_mouse_face_p (w)
22851 struct window *w;
22853 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22854 int in_mouse_face = 0;
22856 if (WINDOWP (dpyinfo->mouse_face_window)
22857 && XWINDOW (dpyinfo->mouse_face_window) == w)
22859 int hpos = w->phys_cursor.hpos;
22860 int vpos = w->phys_cursor.vpos;
22862 if (vpos >= dpyinfo->mouse_face_beg_row
22863 && vpos <= dpyinfo->mouse_face_end_row
22864 && (vpos > dpyinfo->mouse_face_beg_row
22865 || hpos >= dpyinfo->mouse_face_beg_col)
22866 && (vpos < dpyinfo->mouse_face_end_row
22867 || hpos < dpyinfo->mouse_face_end_col
22868 || dpyinfo->mouse_face_past_end))
22869 in_mouse_face = 1;
22872 return in_mouse_face;
22878 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22879 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22880 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22881 for the overlay or run of text properties specifying the mouse
22882 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22883 before-string and after-string that must also be highlighted.
22884 DISPLAY_STRING, if non-nil, is a display string that may cover some
22885 or all of the highlighted text. */
22887 static void
22888 mouse_face_from_buffer_pos (Lisp_Object window,
22889 Display_Info *dpyinfo,
22890 EMACS_INT mouse_charpos,
22891 EMACS_INT start_charpos,
22892 EMACS_INT end_charpos,
22893 Lisp_Object before_string,
22894 Lisp_Object after_string,
22895 Lisp_Object display_string)
22897 struct window *w = XWINDOW (window);
22898 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22899 struct glyph_row *row;
22900 struct glyph *glyph, *end;
22901 EMACS_INT ignore;
22902 int x;
22904 xassert (NILP (display_string) || STRINGP (display_string));
22905 xassert (NILP (before_string) || STRINGP (before_string));
22906 xassert (NILP (after_string) || STRINGP (after_string));
22908 /* Find the first highlighted glyph. */
22909 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22911 dpyinfo->mouse_face_beg_col = 0;
22912 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22913 dpyinfo->mouse_face_beg_x = first->x;
22914 dpyinfo->mouse_face_beg_y = first->y;
22916 else
22918 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22919 if (row == NULL)
22920 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22922 /* If the before-string or display-string contains newlines,
22923 row_containing_pos skips to its last row. Move back. */
22924 if (!NILP (before_string) || !NILP (display_string))
22926 struct glyph_row *prev;
22927 while ((prev = row - 1, prev >= first)
22928 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22929 && prev->used[TEXT_AREA] > 0)
22931 struct glyph *beg = prev->glyphs[TEXT_AREA];
22932 glyph = beg + prev->used[TEXT_AREA];
22933 while (--glyph >= beg && INTEGERP (glyph->object));
22934 if (glyph < beg
22935 || !(EQ (glyph->object, before_string)
22936 || EQ (glyph->object, display_string)))
22937 break;
22938 row = prev;
22942 glyph = row->glyphs[TEXT_AREA];
22943 end = glyph + row->used[TEXT_AREA];
22944 x = row->x;
22945 dpyinfo->mouse_face_beg_y = row->y;
22946 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22948 /* Skip truncation glyphs at the start of the glyph row. */
22949 if (row->displays_text_p)
22950 for (; glyph < end
22951 && INTEGERP (glyph->object)
22952 && glyph->charpos < 0;
22953 ++glyph)
22954 x += glyph->pixel_width;
22956 /* Scan the glyph row, stopping before BEFORE_STRING or
22957 DISPLAY_STRING or START_CHARPOS. */
22958 for (; glyph < end
22959 && !INTEGERP (glyph->object)
22960 && !EQ (glyph->object, before_string)
22961 && !EQ (glyph->object, display_string)
22962 && !(BUFFERP (glyph->object)
22963 && glyph->charpos >= start_charpos);
22964 ++glyph)
22965 x += glyph->pixel_width;
22967 dpyinfo->mouse_face_beg_x = x;
22968 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22971 /* Find the last highlighted glyph. */
22972 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22973 if (row == NULL)
22975 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22976 dpyinfo->mouse_face_past_end = 1;
22978 else if (!NILP (after_string))
22980 /* If the after-string has newlines, advance to its last row. */
22981 struct glyph_row *next;
22982 struct glyph_row *last
22983 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22985 for (next = row + 1;
22986 next <= last
22987 && next->used[TEXT_AREA] > 0
22988 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22989 ++next)
22990 row = next;
22993 glyph = row->glyphs[TEXT_AREA];
22994 end = glyph + row->used[TEXT_AREA];
22995 x = row->x;
22996 dpyinfo->mouse_face_end_y = row->y;
22997 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22999 /* Skip truncation glyphs at the start of the row. */
23000 if (row->displays_text_p)
23001 for (; glyph < end
23002 && INTEGERP (glyph->object)
23003 && glyph->charpos < 0;
23004 ++glyph)
23005 x += glyph->pixel_width;
23007 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
23008 AFTER_STRING. */
23009 for (; glyph < end
23010 && !INTEGERP (glyph->object)
23011 && !EQ (glyph->object, after_string)
23012 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
23013 ++glyph)
23014 x += glyph->pixel_width;
23016 /* If we found AFTER_STRING, consume it and stop. */
23017 if (EQ (glyph->object, after_string))
23019 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
23020 x += glyph->pixel_width;
23022 else
23024 /* If there's no after-string, we must check if we overshot,
23025 which might be the case if we stopped after a string glyph.
23026 That glyph may belong to a before-string or display-string
23027 associated with the end position, which must not be
23028 highlighted. */
23029 Lisp_Object prev_object;
23030 int pos;
23032 while (glyph > row->glyphs[TEXT_AREA])
23034 prev_object = (glyph - 1)->object;
23035 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
23036 break;
23038 pos = string_buffer_position (w, prev_object, end_charpos);
23039 if (pos && pos < end_charpos)
23040 break;
23042 for (; glyph > row->glyphs[TEXT_AREA]
23043 && EQ ((glyph - 1)->object, prev_object);
23044 --glyph)
23045 x -= (glyph - 1)->pixel_width;
23049 dpyinfo->mouse_face_end_x = x;
23050 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
23051 dpyinfo->mouse_face_window = window;
23052 dpyinfo->mouse_face_face_id
23053 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
23054 mouse_charpos + 1,
23055 !dpyinfo->mouse_face_hidden, -1);
23056 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23060 /* Find the position of the glyph for position POS in OBJECT in
23061 window W's current matrix, and return in *X, *Y the pixel
23062 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
23064 RIGHT_P non-zero means return the position of the right edge of the
23065 glyph, RIGHT_P zero means return the left edge position.
23067 If no glyph for POS exists in the matrix, return the position of
23068 the glyph with the next smaller position that is in the matrix, if
23069 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
23070 exists in the matrix, return the position of the glyph with the
23071 next larger position in OBJECT.
23073 Value is non-zero if a glyph was found. */
23075 static int
23076 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
23077 struct window *w;
23078 EMACS_INT pos;
23079 Lisp_Object object;
23080 int *hpos, *vpos, *x, *y;
23081 int right_p;
23083 int yb = window_text_bottom_y (w);
23084 struct glyph_row *r;
23085 struct glyph *best_glyph = NULL;
23086 struct glyph_row *best_row = NULL;
23087 int best_x = 0;
23089 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
23090 r->enabled_p && r->y < yb;
23091 ++r)
23093 struct glyph *g = r->glyphs[TEXT_AREA];
23094 struct glyph *e = g + r->used[TEXT_AREA];
23095 int gx;
23097 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23098 if (EQ (g->object, object))
23100 if (g->charpos == pos)
23102 best_glyph = g;
23103 best_x = gx;
23104 best_row = r;
23105 goto found;
23107 else if (best_glyph == NULL
23108 || ((eabs (g->charpos - pos)
23109 < eabs (best_glyph->charpos - pos))
23110 && (right_p
23111 ? g->charpos < pos
23112 : g->charpos > pos)))
23114 best_glyph = g;
23115 best_x = gx;
23116 best_row = r;
23121 found:
23123 if (best_glyph)
23125 *x = best_x;
23126 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23128 if (right_p)
23130 *x += best_glyph->pixel_width;
23131 ++*hpos;
23134 *y = best_row->y;
23135 *vpos = best_row - w->current_matrix->rows;
23138 return best_glyph != NULL;
23142 /* See if position X, Y is within a hot-spot of an image. */
23144 static int
23145 on_hot_spot_p (hot_spot, x, y)
23146 Lisp_Object hot_spot;
23147 int x, y;
23149 if (!CONSP (hot_spot))
23150 return 0;
23152 if (EQ (XCAR (hot_spot), Qrect))
23154 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23155 Lisp_Object rect = XCDR (hot_spot);
23156 Lisp_Object tem;
23157 if (!CONSP (rect))
23158 return 0;
23159 if (!CONSP (XCAR (rect)))
23160 return 0;
23161 if (!CONSP (XCDR (rect)))
23162 return 0;
23163 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23164 return 0;
23165 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23166 return 0;
23167 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23168 return 0;
23169 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23170 return 0;
23171 return 1;
23173 else if (EQ (XCAR (hot_spot), Qcircle))
23175 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23176 Lisp_Object circ = XCDR (hot_spot);
23177 Lisp_Object lr, lx0, ly0;
23178 if (CONSP (circ)
23179 && CONSP (XCAR (circ))
23180 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23181 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23182 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23184 double r = XFLOATINT (lr);
23185 double dx = XINT (lx0) - x;
23186 double dy = XINT (ly0) - y;
23187 return (dx * dx + dy * dy <= r * r);
23190 else if (EQ (XCAR (hot_spot), Qpoly))
23192 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23193 if (VECTORP (XCDR (hot_spot)))
23195 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23196 Lisp_Object *poly = v->contents;
23197 int n = v->size;
23198 int i;
23199 int inside = 0;
23200 Lisp_Object lx, ly;
23201 int x0, y0;
23203 /* Need an even number of coordinates, and at least 3 edges. */
23204 if (n < 6 || n & 1)
23205 return 0;
23207 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23208 If count is odd, we are inside polygon. Pixels on edges
23209 may or may not be included depending on actual geometry of the
23210 polygon. */
23211 if ((lx = poly[n-2], !INTEGERP (lx))
23212 || (ly = poly[n-1], !INTEGERP (lx)))
23213 return 0;
23214 x0 = XINT (lx), y0 = XINT (ly);
23215 for (i = 0; i < n; i += 2)
23217 int x1 = x0, y1 = y0;
23218 if ((lx = poly[i], !INTEGERP (lx))
23219 || (ly = poly[i+1], !INTEGERP (ly)))
23220 return 0;
23221 x0 = XINT (lx), y0 = XINT (ly);
23223 /* Does this segment cross the X line? */
23224 if (x0 >= x)
23226 if (x1 >= x)
23227 continue;
23229 else if (x1 < x)
23230 continue;
23231 if (y > y0 && y > y1)
23232 continue;
23233 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23234 inside = !inside;
23236 return inside;
23239 return 0;
23242 Lisp_Object
23243 find_hot_spot (map, x, y)
23244 Lisp_Object map;
23245 int x, y;
23247 while (CONSP (map))
23249 if (CONSP (XCAR (map))
23250 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23251 return XCAR (map);
23252 map = XCDR (map);
23255 return Qnil;
23258 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23259 3, 3, 0,
23260 doc: /* Lookup in image map MAP coordinates X and Y.
23261 An image map is an alist where each element has the format (AREA ID PLIST).
23262 An AREA is specified as either a rectangle, a circle, or a polygon:
23263 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23264 pixel coordinates of the upper left and bottom right corners.
23265 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23266 and the radius of the circle; r may be a float or integer.
23267 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23268 vector describes one corner in the polygon.
23269 Returns the alist element for the first matching AREA in MAP. */)
23270 (map, x, y)
23271 Lisp_Object map;
23272 Lisp_Object x, y;
23274 if (NILP (map))
23275 return Qnil;
23277 CHECK_NUMBER (x);
23278 CHECK_NUMBER (y);
23280 return find_hot_spot (map, XINT (x), XINT (y));
23284 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23285 static void
23286 define_frame_cursor1 (f, cursor, pointer)
23287 struct frame *f;
23288 Cursor cursor;
23289 Lisp_Object pointer;
23291 /* Do not change cursor shape while dragging mouse. */
23292 if (!NILP (do_mouse_tracking))
23293 return;
23295 if (!NILP (pointer))
23297 if (EQ (pointer, Qarrow))
23298 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23299 else if (EQ (pointer, Qhand))
23300 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23301 else if (EQ (pointer, Qtext))
23302 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23303 else if (EQ (pointer, intern ("hdrag")))
23304 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23305 #ifdef HAVE_X_WINDOWS
23306 else if (EQ (pointer, intern ("vdrag")))
23307 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23308 #endif
23309 else if (EQ (pointer, intern ("hourglass")))
23310 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23311 else if (EQ (pointer, Qmodeline))
23312 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23313 else
23314 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23317 if (cursor != No_Cursor)
23318 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23321 /* Take proper action when mouse has moved to the mode or header line
23322 or marginal area AREA of window W, x-position X and y-position Y.
23323 X is relative to the start of the text display area of W, so the
23324 width of bitmap areas and scroll bars must be subtracted to get a
23325 position relative to the start of the mode line. */
23327 static void
23328 note_mode_line_or_margin_highlight (window, x, y, area)
23329 Lisp_Object window;
23330 int x, y;
23331 enum window_part area;
23333 struct window *w = XWINDOW (window);
23334 struct frame *f = XFRAME (w->frame);
23335 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23336 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23337 Lisp_Object pointer = Qnil;
23338 int charpos, dx, dy, width, height;
23339 Lisp_Object string, object = Qnil;
23340 Lisp_Object pos, help;
23342 Lisp_Object mouse_face;
23343 int original_x_pixel = x;
23344 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23345 struct glyph_row *row;
23347 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23349 int x0;
23350 struct glyph *end;
23352 string = mode_line_string (w, area, &x, &y, &charpos,
23353 &object, &dx, &dy, &width, &height);
23355 row = (area == ON_MODE_LINE
23356 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23357 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23359 /* Find glyph */
23360 if (row->mode_line_p && row->enabled_p)
23362 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23363 end = glyph + row->used[TEXT_AREA];
23365 for (x0 = original_x_pixel;
23366 glyph < end && x0 >= glyph->pixel_width;
23367 ++glyph)
23368 x0 -= glyph->pixel_width;
23370 if (glyph >= end)
23371 glyph = NULL;
23374 else
23376 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23377 string = marginal_area_string (w, area, &x, &y, &charpos,
23378 &object, &dx, &dy, &width, &height);
23381 help = Qnil;
23383 if (IMAGEP (object))
23385 Lisp_Object image_map, hotspot;
23386 if ((image_map = Fplist_get (XCDR (object), QCmap),
23387 !NILP (image_map))
23388 && (hotspot = find_hot_spot (image_map, dx, dy),
23389 CONSP (hotspot))
23390 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23392 Lisp_Object area_id, plist;
23394 area_id = XCAR (hotspot);
23395 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23396 If so, we could look for mouse-enter, mouse-leave
23397 properties in PLIST (and do something...). */
23398 hotspot = XCDR (hotspot);
23399 if (CONSP (hotspot)
23400 && (plist = XCAR (hotspot), CONSP (plist)))
23402 pointer = Fplist_get (plist, Qpointer);
23403 if (NILP (pointer))
23404 pointer = Qhand;
23405 help = Fplist_get (plist, Qhelp_echo);
23406 if (!NILP (help))
23408 help_echo_string = help;
23409 /* Is this correct? ++kfs */
23410 XSETWINDOW (help_echo_window, w);
23411 help_echo_object = w->buffer;
23412 help_echo_pos = charpos;
23416 if (NILP (pointer))
23417 pointer = Fplist_get (XCDR (object), QCpointer);
23420 if (STRINGP (string))
23422 pos = make_number (charpos);
23423 /* If we're on a string with `help-echo' text property, arrange
23424 for the help to be displayed. This is done by setting the
23425 global variable help_echo_string to the help string. */
23426 if (NILP (help))
23428 help = Fget_text_property (pos, Qhelp_echo, string);
23429 if (!NILP (help))
23431 help_echo_string = help;
23432 XSETWINDOW (help_echo_window, w);
23433 help_echo_object = string;
23434 help_echo_pos = charpos;
23438 if (NILP (pointer))
23439 pointer = Fget_text_property (pos, Qpointer, string);
23441 /* Change the mouse pointer according to what is under X/Y. */
23442 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23444 Lisp_Object map;
23445 map = Fget_text_property (pos, Qlocal_map, string);
23446 if (!KEYMAPP (map))
23447 map = Fget_text_property (pos, Qkeymap, string);
23448 if (!KEYMAPP (map))
23449 cursor = dpyinfo->vertical_scroll_bar_cursor;
23452 /* Change the mouse face according to what is under X/Y. */
23453 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23454 if (!NILP (mouse_face)
23455 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23456 && glyph)
23458 Lisp_Object b, e;
23460 struct glyph * tmp_glyph;
23462 int gpos;
23463 int gseq_length;
23464 int total_pixel_width;
23465 EMACS_INT ignore;
23467 int vpos, hpos;
23469 b = Fprevious_single_property_change (make_number (charpos + 1),
23470 Qmouse_face, string, Qnil);
23471 if (NILP (b))
23472 b = make_number (0);
23474 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23475 if (NILP (e))
23476 e = make_number (SCHARS (string));
23478 /* Calculate the position(glyph position: GPOS) of GLYPH in
23479 displayed string. GPOS is different from CHARPOS.
23481 CHARPOS is the position of glyph in internal string
23482 object. A mode line string format has structures which
23483 is converted to a flatten by emacs lisp interpreter.
23484 The internal string is an element of the structures.
23485 The displayed string is the flatten string. */
23486 gpos = 0;
23487 if (glyph > row_start_glyph)
23489 tmp_glyph = glyph - 1;
23490 while (tmp_glyph >= row_start_glyph
23491 && tmp_glyph->charpos >= XINT (b)
23492 && EQ (tmp_glyph->object, glyph->object))
23494 tmp_glyph--;
23495 gpos++;
23499 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23500 displayed string holding GLYPH.
23502 GSEQ_LENGTH is different from SCHARS (STRING).
23503 SCHARS (STRING) returns the length of the internal string. */
23504 for (tmp_glyph = glyph, gseq_length = gpos;
23505 tmp_glyph->charpos < XINT (e);
23506 tmp_glyph++, gseq_length++)
23508 if (!EQ (tmp_glyph->object, glyph->object))
23509 break;
23512 total_pixel_width = 0;
23513 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23514 total_pixel_width += tmp_glyph->pixel_width;
23516 /* Pre calculation of re-rendering position */
23517 vpos = (x - gpos);
23518 hpos = (area == ON_MODE_LINE
23519 ? (w->current_matrix)->nrows - 1
23520 : 0);
23522 /* If the re-rendering position is included in the last
23523 re-rendering area, we should do nothing. */
23524 if ( EQ (window, dpyinfo->mouse_face_window)
23525 && dpyinfo->mouse_face_beg_col <= vpos
23526 && vpos < dpyinfo->mouse_face_end_col
23527 && dpyinfo->mouse_face_beg_row == hpos )
23528 return;
23530 if (clear_mouse_face (dpyinfo))
23531 cursor = No_Cursor;
23533 dpyinfo->mouse_face_beg_col = vpos;
23534 dpyinfo->mouse_face_beg_row = hpos;
23536 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23537 dpyinfo->mouse_face_beg_y = 0;
23539 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23540 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23542 dpyinfo->mouse_face_end_x = 0;
23543 dpyinfo->mouse_face_end_y = 0;
23545 dpyinfo->mouse_face_past_end = 0;
23546 dpyinfo->mouse_face_window = window;
23548 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23549 charpos,
23550 0, 0, 0, &ignore,
23551 glyph->face_id, 1);
23552 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23554 if (NILP (pointer))
23555 pointer = Qhand;
23557 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23558 clear_mouse_face (dpyinfo);
23560 define_frame_cursor1 (f, cursor, pointer);
23564 /* EXPORT:
23565 Take proper action when the mouse has moved to position X, Y on
23566 frame F as regards highlighting characters that have mouse-face
23567 properties. Also de-highlighting chars where the mouse was before.
23568 X and Y can be negative or out of range. */
23570 void
23571 note_mouse_highlight (f, x, y)
23572 struct frame *f;
23573 int x, y;
23575 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23576 enum window_part part;
23577 Lisp_Object window;
23578 struct window *w;
23579 Cursor cursor = No_Cursor;
23580 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23581 struct buffer *b;
23583 /* When a menu is active, don't highlight because this looks odd. */
23584 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23585 if (popup_activated ())
23586 return;
23587 #endif
23589 if (NILP (Vmouse_highlight)
23590 || !f->glyphs_initialized_p)
23591 return;
23593 dpyinfo->mouse_face_mouse_x = x;
23594 dpyinfo->mouse_face_mouse_y = y;
23595 dpyinfo->mouse_face_mouse_frame = f;
23597 if (dpyinfo->mouse_face_defer)
23598 return;
23600 if (gc_in_progress)
23602 dpyinfo->mouse_face_deferred_gc = 1;
23603 return;
23606 /* Which window is that in? */
23607 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23609 /* If we were displaying active text in another window, clear that.
23610 Also clear if we move out of text area in same window. */
23611 if (! EQ (window, dpyinfo->mouse_face_window)
23612 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23613 && !NILP (dpyinfo->mouse_face_window)))
23614 clear_mouse_face (dpyinfo);
23616 /* Not on a window -> return. */
23617 if (!WINDOWP (window))
23618 return;
23620 /* Reset help_echo_string. It will get recomputed below. */
23621 help_echo_string = Qnil;
23623 /* Convert to window-relative pixel coordinates. */
23624 w = XWINDOW (window);
23625 frame_to_window_pixel_xy (w, &x, &y);
23627 /* Handle tool-bar window differently since it doesn't display a
23628 buffer. */
23629 if (EQ (window, f->tool_bar_window))
23631 note_tool_bar_highlight (f, x, y);
23632 return;
23635 /* Mouse is on the mode, header line or margin? */
23636 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23637 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23639 note_mode_line_or_margin_highlight (window, x, y, part);
23640 return;
23643 if (part == ON_VERTICAL_BORDER)
23645 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23646 help_echo_string = build_string ("drag-mouse-1: resize");
23648 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23649 || part == ON_SCROLL_BAR)
23650 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23651 else
23652 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23654 /* Are we in a window whose display is up to date?
23655 And verify the buffer's text has not changed. */
23656 b = XBUFFER (w->buffer);
23657 if (part == ON_TEXT
23658 && EQ (w->window_end_valid, w->buffer)
23659 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23660 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23662 int hpos, vpos, pos, i, dx, dy, area;
23663 struct glyph *glyph;
23664 Lisp_Object object;
23665 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23666 Lisp_Object *overlay_vec = NULL;
23667 int noverlays;
23668 struct buffer *obuf;
23669 int obegv, ozv, same_region;
23671 /* Find the glyph under X/Y. */
23672 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23674 /* Look for :pointer property on image. */
23675 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23677 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23678 if (img != NULL && IMAGEP (img->spec))
23680 Lisp_Object image_map, hotspot;
23681 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23682 !NILP (image_map))
23683 && (hotspot = find_hot_spot (image_map,
23684 glyph->slice.x + dx,
23685 glyph->slice.y + dy),
23686 CONSP (hotspot))
23687 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23689 Lisp_Object area_id, plist;
23691 area_id = XCAR (hotspot);
23692 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23693 If so, we could look for mouse-enter, mouse-leave
23694 properties in PLIST (and do something...). */
23695 hotspot = XCDR (hotspot);
23696 if (CONSP (hotspot)
23697 && (plist = XCAR (hotspot), CONSP (plist)))
23699 pointer = Fplist_get (plist, Qpointer);
23700 if (NILP (pointer))
23701 pointer = Qhand;
23702 help_echo_string = Fplist_get (plist, Qhelp_echo);
23703 if (!NILP (help_echo_string))
23705 help_echo_window = window;
23706 help_echo_object = glyph->object;
23707 help_echo_pos = glyph->charpos;
23711 if (NILP (pointer))
23712 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23716 /* Clear mouse face if X/Y not over text. */
23717 if (glyph == NULL
23718 || area != TEXT_AREA
23719 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23721 if (clear_mouse_face (dpyinfo))
23722 cursor = No_Cursor;
23723 if (NILP (pointer))
23725 if (area != TEXT_AREA)
23726 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23727 else
23728 pointer = Vvoid_text_area_pointer;
23730 goto set_cursor;
23733 pos = glyph->charpos;
23734 object = glyph->object;
23735 if (!STRINGP (object) && !BUFFERP (object))
23736 goto set_cursor;
23738 /* If we get an out-of-range value, return now; avoid an error. */
23739 if (BUFFERP (object) && pos > BUF_Z (b))
23740 goto set_cursor;
23742 /* Make the window's buffer temporarily current for
23743 overlays_at and compute_char_face. */
23744 obuf = current_buffer;
23745 current_buffer = b;
23746 obegv = BEGV;
23747 ozv = ZV;
23748 BEGV = BEG;
23749 ZV = Z;
23751 /* Is this char mouse-active or does it have help-echo? */
23752 position = make_number (pos);
23754 if (BUFFERP (object))
23756 /* Put all the overlays we want in a vector in overlay_vec. */
23757 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23758 /* Sort overlays into increasing priority order. */
23759 noverlays = sort_overlays (overlay_vec, noverlays, w);
23761 else
23762 noverlays = 0;
23764 same_region = (EQ (window, dpyinfo->mouse_face_window)
23765 && vpos >= dpyinfo->mouse_face_beg_row
23766 && vpos <= dpyinfo->mouse_face_end_row
23767 && (vpos > dpyinfo->mouse_face_beg_row
23768 || hpos >= dpyinfo->mouse_face_beg_col)
23769 && (vpos < dpyinfo->mouse_face_end_row
23770 || hpos < dpyinfo->mouse_face_end_col
23771 || dpyinfo->mouse_face_past_end));
23773 if (same_region)
23774 cursor = No_Cursor;
23776 /* Check mouse-face highlighting. */
23777 if (! same_region
23778 /* If there exists an overlay with mouse-face overlapping
23779 the one we are currently highlighting, we have to
23780 check if we enter the overlapping overlay, and then
23781 highlight only that. */
23782 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23783 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23785 /* Find the highest priority overlay with a mouse-face. */
23786 overlay = Qnil;
23787 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23789 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23790 if (!NILP (mouse_face))
23791 overlay = overlay_vec[i];
23794 /* If we're highlighting the same overlay as before, there's
23795 no need to do that again. */
23796 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23797 goto check_help_echo;
23798 dpyinfo->mouse_face_overlay = overlay;
23800 /* Clear the display of the old active region, if any. */
23801 if (clear_mouse_face (dpyinfo))
23802 cursor = No_Cursor;
23804 /* If no overlay applies, get a text property. */
23805 if (NILP (overlay))
23806 mouse_face = Fget_text_property (position, Qmouse_face, object);
23808 /* Next, compute the bounds of the mouse highlighting and
23809 display it. */
23810 if (!NILP (mouse_face) && STRINGP (object))
23812 /* The mouse-highlighting comes from a display string
23813 with a mouse-face. */
23814 Lisp_Object b, e;
23815 EMACS_INT ignore;
23817 b = Fprevious_single_property_change
23818 (make_number (pos + 1), Qmouse_face, object, Qnil);
23819 e = Fnext_single_property_change
23820 (position, Qmouse_face, object, Qnil);
23821 if (NILP (b))
23822 b = make_number (0);
23823 if (NILP (e))
23824 e = make_number (SCHARS (object) - 1);
23826 fast_find_string_pos (w, XINT (b), object,
23827 &dpyinfo->mouse_face_beg_col,
23828 &dpyinfo->mouse_face_beg_row,
23829 &dpyinfo->mouse_face_beg_x,
23830 &dpyinfo->mouse_face_beg_y, 0);
23831 fast_find_string_pos (w, XINT (e), object,
23832 &dpyinfo->mouse_face_end_col,
23833 &dpyinfo->mouse_face_end_row,
23834 &dpyinfo->mouse_face_end_x,
23835 &dpyinfo->mouse_face_end_y, 1);
23836 dpyinfo->mouse_face_past_end = 0;
23837 dpyinfo->mouse_face_window = window;
23838 dpyinfo->mouse_face_face_id
23839 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23840 glyph->face_id, 1);
23841 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23842 cursor = No_Cursor;
23844 else
23846 /* The mouse-highlighting, if any, comes from an overlay
23847 or text property in the buffer. */
23848 Lisp_Object buffer, display_string;
23850 if (STRINGP (object))
23852 /* If we are on a display string with no mouse-face,
23853 check if the text under it has one. */
23854 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23855 int start = MATRIX_ROW_START_CHARPOS (r);
23856 pos = string_buffer_position (w, object, start);
23857 if (pos > 0)
23859 mouse_face = get_char_property_and_overlay
23860 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23861 buffer = w->buffer;
23862 display_string = object;
23865 else
23867 buffer = object;
23868 display_string = Qnil;
23871 if (!NILP (mouse_face))
23873 Lisp_Object before, after;
23874 Lisp_Object before_string, after_string;
23876 if (NILP (overlay))
23878 /* Handle the text property case. */
23879 before = Fprevious_single_property_change
23880 (make_number (pos + 1), Qmouse_face, buffer,
23881 Fmarker_position (w->start));
23882 after = Fnext_single_property_change
23883 (make_number (pos), Qmouse_face, buffer,
23884 make_number (BUF_Z (XBUFFER (buffer))
23885 - XFASTINT (w->window_end_pos)));
23886 before_string = after_string = Qnil;
23888 else
23890 /* Handle the overlay case. */
23891 before = Foverlay_start (overlay);
23892 after = Foverlay_end (overlay);
23893 before_string = Foverlay_get (overlay, Qbefore_string);
23894 after_string = Foverlay_get (overlay, Qafter_string);
23896 if (!STRINGP (before_string)) before_string = Qnil;
23897 if (!STRINGP (after_string)) after_string = Qnil;
23900 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23901 XFASTINT (before),
23902 XFASTINT (after),
23903 before_string, after_string,
23904 display_string);
23905 cursor = No_Cursor;
23910 check_help_echo:
23912 /* Look for a `help-echo' property. */
23913 if (NILP (help_echo_string)) {
23914 Lisp_Object help, overlay;
23916 /* Check overlays first. */
23917 help = overlay = Qnil;
23918 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23920 overlay = overlay_vec[i];
23921 help = Foverlay_get (overlay, Qhelp_echo);
23924 if (!NILP (help))
23926 help_echo_string = help;
23927 help_echo_window = window;
23928 help_echo_object = overlay;
23929 help_echo_pos = pos;
23931 else
23933 Lisp_Object object = glyph->object;
23934 int charpos = glyph->charpos;
23936 /* Try text properties. */
23937 if (STRINGP (object)
23938 && charpos >= 0
23939 && charpos < SCHARS (object))
23941 help = Fget_text_property (make_number (charpos),
23942 Qhelp_echo, object);
23943 if (NILP (help))
23945 /* If the string itself doesn't specify a help-echo,
23946 see if the buffer text ``under'' it does. */
23947 struct glyph_row *r
23948 = MATRIX_ROW (w->current_matrix, vpos);
23949 int start = MATRIX_ROW_START_CHARPOS (r);
23950 int pos = string_buffer_position (w, object, start);
23951 if (pos > 0)
23953 help = Fget_char_property (make_number (pos),
23954 Qhelp_echo, w->buffer);
23955 if (!NILP (help))
23957 charpos = pos;
23958 object = w->buffer;
23963 else if (BUFFERP (object)
23964 && charpos >= BEGV
23965 && charpos < ZV)
23966 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23967 object);
23969 if (!NILP (help))
23971 help_echo_string = help;
23972 help_echo_window = window;
23973 help_echo_object = object;
23974 help_echo_pos = charpos;
23979 /* Look for a `pointer' property. */
23980 if (NILP (pointer))
23982 /* Check overlays first. */
23983 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23984 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23986 if (NILP (pointer))
23988 Lisp_Object object = glyph->object;
23989 int charpos = glyph->charpos;
23991 /* Try text properties. */
23992 if (STRINGP (object)
23993 && charpos >= 0
23994 && charpos < SCHARS (object))
23996 pointer = Fget_text_property (make_number (charpos),
23997 Qpointer, object);
23998 if (NILP (pointer))
24000 /* If the string itself doesn't specify a pointer,
24001 see if the buffer text ``under'' it does. */
24002 struct glyph_row *r
24003 = MATRIX_ROW (w->current_matrix, vpos);
24004 int start = MATRIX_ROW_START_CHARPOS (r);
24005 int pos = string_buffer_position (w, object, start);
24006 if (pos > 0)
24007 pointer = Fget_char_property (make_number (pos),
24008 Qpointer, w->buffer);
24011 else if (BUFFERP (object)
24012 && charpos >= BEGV
24013 && charpos < ZV)
24014 pointer = Fget_text_property (make_number (charpos),
24015 Qpointer, object);
24019 BEGV = obegv;
24020 ZV = ozv;
24021 current_buffer = obuf;
24024 set_cursor:
24026 define_frame_cursor1 (f, cursor, pointer);
24030 /* EXPORT for RIF:
24031 Clear any mouse-face on window W. This function is part of the
24032 redisplay interface, and is called from try_window_id and similar
24033 functions to ensure the mouse-highlight is off. */
24035 void
24036 x_clear_window_mouse_face (w)
24037 struct window *w;
24039 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
24040 Lisp_Object window;
24042 BLOCK_INPUT;
24043 XSETWINDOW (window, w);
24044 if (EQ (window, dpyinfo->mouse_face_window))
24045 clear_mouse_face (dpyinfo);
24046 UNBLOCK_INPUT;
24050 /* EXPORT:
24051 Just discard the mouse face information for frame F, if any.
24052 This is used when the size of F is changed. */
24054 void
24055 cancel_mouse_face (f)
24056 struct frame *f;
24058 Lisp_Object window;
24059 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24061 window = dpyinfo->mouse_face_window;
24062 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
24064 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
24065 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
24066 dpyinfo->mouse_face_window = Qnil;
24071 #endif /* HAVE_WINDOW_SYSTEM */
24074 /***********************************************************************
24075 Exposure Events
24076 ***********************************************************************/
24078 #ifdef HAVE_WINDOW_SYSTEM
24080 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24081 which intersects rectangle R. R is in window-relative coordinates. */
24083 static void
24084 expose_area (w, row, r, area)
24085 struct window *w;
24086 struct glyph_row *row;
24087 XRectangle *r;
24088 enum glyph_row_area area;
24090 struct glyph *first = row->glyphs[area];
24091 struct glyph *end = row->glyphs[area] + row->used[area];
24092 struct glyph *last;
24093 int first_x, start_x, x;
24095 if (area == TEXT_AREA && row->fill_line_p)
24096 /* If row extends face to end of line write the whole line. */
24097 draw_glyphs (w, 0, row, area,
24098 0, row->used[area],
24099 DRAW_NORMAL_TEXT, 0);
24100 else
24102 /* Set START_X to the window-relative start position for drawing glyphs of
24103 AREA. The first glyph of the text area can be partially visible.
24104 The first glyphs of other areas cannot. */
24105 start_x = window_box_left_offset (w, area);
24106 x = start_x;
24107 if (area == TEXT_AREA)
24108 x += row->x;
24110 /* Find the first glyph that must be redrawn. */
24111 while (first < end
24112 && x + first->pixel_width < r->x)
24114 x += first->pixel_width;
24115 ++first;
24118 /* Find the last one. */
24119 last = first;
24120 first_x = x;
24121 while (last < end
24122 && x < r->x + r->width)
24124 x += last->pixel_width;
24125 ++last;
24128 /* Repaint. */
24129 if (last > first)
24130 draw_glyphs (w, first_x - start_x, row, area,
24131 first - row->glyphs[area], last - row->glyphs[area],
24132 DRAW_NORMAL_TEXT, 0);
24137 /* Redraw the parts of the glyph row ROW on window W intersecting
24138 rectangle R. R is in window-relative coordinates. Value is
24139 non-zero if mouse-face was overwritten. */
24141 static int
24142 expose_line (w, row, r)
24143 struct window *w;
24144 struct glyph_row *row;
24145 XRectangle *r;
24147 xassert (row->enabled_p);
24149 if (row->mode_line_p || w->pseudo_window_p)
24150 draw_glyphs (w, 0, row, TEXT_AREA,
24151 0, row->used[TEXT_AREA],
24152 DRAW_NORMAL_TEXT, 0);
24153 else
24155 if (row->used[LEFT_MARGIN_AREA])
24156 expose_area (w, row, r, LEFT_MARGIN_AREA);
24157 if (row->used[TEXT_AREA])
24158 expose_area (w, row, r, TEXT_AREA);
24159 if (row->used[RIGHT_MARGIN_AREA])
24160 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24161 draw_row_fringe_bitmaps (w, row);
24164 return row->mouse_face_p;
24168 /* Redraw those parts of glyphs rows during expose event handling that
24169 overlap other rows. Redrawing of an exposed line writes over parts
24170 of lines overlapping that exposed line; this function fixes that.
24172 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24173 row in W's current matrix that is exposed and overlaps other rows.
24174 LAST_OVERLAPPING_ROW is the last such row. */
24176 static void
24177 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24178 struct window *w;
24179 struct glyph_row *first_overlapping_row;
24180 struct glyph_row *last_overlapping_row;
24181 XRectangle *r;
24183 struct glyph_row *row;
24185 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24186 if (row->overlapping_p)
24188 xassert (row->enabled_p && !row->mode_line_p);
24190 row->clip = r;
24191 if (row->used[LEFT_MARGIN_AREA])
24192 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24194 if (row->used[TEXT_AREA])
24195 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24197 if (row->used[RIGHT_MARGIN_AREA])
24198 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24199 row->clip = NULL;
24204 /* Return non-zero if W's cursor intersects rectangle R. */
24206 static int
24207 phys_cursor_in_rect_p (w, r)
24208 struct window *w;
24209 XRectangle *r;
24211 XRectangle cr, result;
24212 struct glyph *cursor_glyph;
24213 struct glyph_row *row;
24215 if (w->phys_cursor.vpos >= 0
24216 && w->phys_cursor.vpos < w->current_matrix->nrows
24217 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24218 row->enabled_p)
24219 && row->cursor_in_fringe_p)
24221 /* Cursor is in the fringe. */
24222 cr.x = window_box_right_offset (w,
24223 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24224 ? RIGHT_MARGIN_AREA
24225 : TEXT_AREA));
24226 cr.y = row->y;
24227 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24228 cr.height = row->height;
24229 return x_intersect_rectangles (&cr, r, &result);
24232 cursor_glyph = get_phys_cursor_glyph (w);
24233 if (cursor_glyph)
24235 /* r is relative to W's box, but w->phys_cursor.x is relative
24236 to left edge of W's TEXT area. Adjust it. */
24237 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24238 cr.y = w->phys_cursor.y;
24239 cr.width = cursor_glyph->pixel_width;
24240 cr.height = w->phys_cursor_height;
24241 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24242 I assume the effect is the same -- and this is portable. */
24243 return x_intersect_rectangles (&cr, r, &result);
24245 /* If we don't understand the format, pretend we're not in the hot-spot. */
24246 return 0;
24250 /* EXPORT:
24251 Draw a vertical window border to the right of window W if W doesn't
24252 have vertical scroll bars. */
24254 void
24255 x_draw_vertical_border (w)
24256 struct window *w;
24258 struct frame *f = XFRAME (WINDOW_FRAME (w));
24260 /* We could do better, if we knew what type of scroll-bar the adjacent
24261 windows (on either side) have... But we don't :-(
24262 However, I think this works ok. ++KFS 2003-04-25 */
24264 /* Redraw borders between horizontally adjacent windows. Don't
24265 do it for frames with vertical scroll bars because either the
24266 right scroll bar of a window, or the left scroll bar of its
24267 neighbor will suffice as a border. */
24268 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24269 return;
24271 if (!WINDOW_RIGHTMOST_P (w)
24272 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24274 int x0, x1, y0, y1;
24276 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24277 y1 -= 1;
24279 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24280 x1 -= 1;
24282 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24284 else if (!WINDOW_LEFTMOST_P (w)
24285 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24287 int x0, x1, y0, y1;
24289 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24290 y1 -= 1;
24292 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24293 x0 -= 1;
24295 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24300 /* Redraw the part of window W intersection rectangle FR. Pixel
24301 coordinates in FR are frame-relative. Call this function with
24302 input blocked. Value is non-zero if the exposure overwrites
24303 mouse-face. */
24305 static int
24306 expose_window (w, fr)
24307 struct window *w;
24308 XRectangle *fr;
24310 struct frame *f = XFRAME (w->frame);
24311 XRectangle wr, r;
24312 int mouse_face_overwritten_p = 0;
24314 /* If window is not yet fully initialized, do nothing. This can
24315 happen when toolkit scroll bars are used and a window is split.
24316 Reconfiguring the scroll bar will generate an expose for a newly
24317 created window. */
24318 if (w->current_matrix == NULL)
24319 return 0;
24321 /* When we're currently updating the window, display and current
24322 matrix usually don't agree. Arrange for a thorough display
24323 later. */
24324 if (w == updated_window)
24326 SET_FRAME_GARBAGED (f);
24327 return 0;
24330 /* Frame-relative pixel rectangle of W. */
24331 wr.x = WINDOW_LEFT_EDGE_X (w);
24332 wr.y = WINDOW_TOP_EDGE_Y (w);
24333 wr.width = WINDOW_TOTAL_WIDTH (w);
24334 wr.height = WINDOW_TOTAL_HEIGHT (w);
24336 if (x_intersect_rectangles (fr, &wr, &r))
24338 int yb = window_text_bottom_y (w);
24339 struct glyph_row *row;
24340 int cursor_cleared_p;
24341 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24343 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24344 r.x, r.y, r.width, r.height));
24346 /* Convert to window coordinates. */
24347 r.x -= WINDOW_LEFT_EDGE_X (w);
24348 r.y -= WINDOW_TOP_EDGE_Y (w);
24350 /* Turn off the cursor. */
24351 if (!w->pseudo_window_p
24352 && phys_cursor_in_rect_p (w, &r))
24354 x_clear_cursor (w);
24355 cursor_cleared_p = 1;
24357 else
24358 cursor_cleared_p = 0;
24360 /* Update lines intersecting rectangle R. */
24361 first_overlapping_row = last_overlapping_row = NULL;
24362 for (row = w->current_matrix->rows;
24363 row->enabled_p;
24364 ++row)
24366 int y0 = row->y;
24367 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24369 if ((y0 >= r.y && y0 < r.y + r.height)
24370 || (y1 > r.y && y1 < r.y + r.height)
24371 || (r.y >= y0 && r.y < y1)
24372 || (r.y + r.height > y0 && r.y + r.height < y1))
24374 /* A header line may be overlapping, but there is no need
24375 to fix overlapping areas for them. KFS 2005-02-12 */
24376 if (row->overlapping_p && !row->mode_line_p)
24378 if (first_overlapping_row == NULL)
24379 first_overlapping_row = row;
24380 last_overlapping_row = row;
24383 row->clip = fr;
24384 if (expose_line (w, row, &r))
24385 mouse_face_overwritten_p = 1;
24386 row->clip = NULL;
24388 else if (row->overlapping_p)
24390 /* We must redraw a row overlapping the exposed area. */
24391 if (y0 < r.y
24392 ? y0 + row->phys_height > r.y
24393 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24395 if (first_overlapping_row == NULL)
24396 first_overlapping_row = row;
24397 last_overlapping_row = row;
24401 if (y1 >= yb)
24402 break;
24405 /* Display the mode line if there is one. */
24406 if (WINDOW_WANTS_MODELINE_P (w)
24407 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24408 row->enabled_p)
24409 && row->y < r.y + r.height)
24411 if (expose_line (w, row, &r))
24412 mouse_face_overwritten_p = 1;
24415 if (!w->pseudo_window_p)
24417 /* Fix the display of overlapping rows. */
24418 if (first_overlapping_row)
24419 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24420 fr);
24422 /* Draw border between windows. */
24423 x_draw_vertical_border (w);
24425 /* Turn the cursor on again. */
24426 if (cursor_cleared_p)
24427 update_window_cursor (w, 1);
24431 return mouse_face_overwritten_p;
24436 /* Redraw (parts) of all windows in the window tree rooted at W that
24437 intersect R. R contains frame pixel coordinates. Value is
24438 non-zero if the exposure overwrites mouse-face. */
24440 static int
24441 expose_window_tree (w, r)
24442 struct window *w;
24443 XRectangle *r;
24445 struct frame *f = XFRAME (w->frame);
24446 int mouse_face_overwritten_p = 0;
24448 while (w && !FRAME_GARBAGED_P (f))
24450 if (!NILP (w->hchild))
24451 mouse_face_overwritten_p
24452 |= expose_window_tree (XWINDOW (w->hchild), r);
24453 else if (!NILP (w->vchild))
24454 mouse_face_overwritten_p
24455 |= expose_window_tree (XWINDOW (w->vchild), r);
24456 else
24457 mouse_face_overwritten_p |= expose_window (w, r);
24459 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24462 return mouse_face_overwritten_p;
24466 /* EXPORT:
24467 Redisplay an exposed area of frame F. X and Y are the upper-left
24468 corner of the exposed rectangle. W and H are width and height of
24469 the exposed area. All are pixel values. W or H zero means redraw
24470 the entire frame. */
24472 void
24473 expose_frame (f, x, y, w, h)
24474 struct frame *f;
24475 int x, y, w, h;
24477 XRectangle r;
24478 int mouse_face_overwritten_p = 0;
24480 TRACE ((stderr, "expose_frame "));
24482 /* No need to redraw if frame will be redrawn soon. */
24483 if (FRAME_GARBAGED_P (f))
24485 TRACE ((stderr, " garbaged\n"));
24486 return;
24489 /* If basic faces haven't been realized yet, there is no point in
24490 trying to redraw anything. This can happen when we get an expose
24491 event while Emacs is starting, e.g. by moving another window. */
24492 if (FRAME_FACE_CACHE (f) == NULL
24493 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24495 TRACE ((stderr, " no faces\n"));
24496 return;
24499 if (w == 0 || h == 0)
24501 r.x = r.y = 0;
24502 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24503 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24505 else
24507 r.x = x;
24508 r.y = y;
24509 r.width = w;
24510 r.height = h;
24513 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24514 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24516 if (WINDOWP (f->tool_bar_window))
24517 mouse_face_overwritten_p
24518 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24520 #ifdef HAVE_X_WINDOWS
24521 #ifndef MSDOS
24522 #ifndef USE_X_TOOLKIT
24523 if (WINDOWP (f->menu_bar_window))
24524 mouse_face_overwritten_p
24525 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24526 #endif /* not USE_X_TOOLKIT */
24527 #endif
24528 #endif
24530 /* Some window managers support a focus-follows-mouse style with
24531 delayed raising of frames. Imagine a partially obscured frame,
24532 and moving the mouse into partially obscured mouse-face on that
24533 frame. The visible part of the mouse-face will be highlighted,
24534 then the WM raises the obscured frame. With at least one WM, KDE
24535 2.1, Emacs is not getting any event for the raising of the frame
24536 (even tried with SubstructureRedirectMask), only Expose events.
24537 These expose events will draw text normally, i.e. not
24538 highlighted. Which means we must redo the highlight here.
24539 Subsume it under ``we love X''. --gerd 2001-08-15 */
24540 /* Included in Windows version because Windows most likely does not
24541 do the right thing if any third party tool offers
24542 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24543 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24545 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24546 if (f == dpyinfo->mouse_face_mouse_frame)
24548 int x = dpyinfo->mouse_face_mouse_x;
24549 int y = dpyinfo->mouse_face_mouse_y;
24550 clear_mouse_face (dpyinfo);
24551 note_mouse_highlight (f, x, y);
24557 /* EXPORT:
24558 Determine the intersection of two rectangles R1 and R2. Return
24559 the intersection in *RESULT. Value is non-zero if RESULT is not
24560 empty. */
24563 x_intersect_rectangles (r1, r2, result)
24564 XRectangle *r1, *r2, *result;
24566 XRectangle *left, *right;
24567 XRectangle *upper, *lower;
24568 int intersection_p = 0;
24570 /* Rearrange so that R1 is the left-most rectangle. */
24571 if (r1->x < r2->x)
24572 left = r1, right = r2;
24573 else
24574 left = r2, right = r1;
24576 /* X0 of the intersection is right.x0, if this is inside R1,
24577 otherwise there is no intersection. */
24578 if (right->x <= left->x + left->width)
24580 result->x = right->x;
24582 /* The right end of the intersection is the minimum of the
24583 the right ends of left and right. */
24584 result->width = (min (left->x + left->width, right->x + right->width)
24585 - result->x);
24587 /* Same game for Y. */
24588 if (r1->y < r2->y)
24589 upper = r1, lower = r2;
24590 else
24591 upper = r2, lower = r1;
24593 /* The upper end of the intersection is lower.y0, if this is inside
24594 of upper. Otherwise, there is no intersection. */
24595 if (lower->y <= upper->y + upper->height)
24597 result->y = lower->y;
24599 /* The lower end of the intersection is the minimum of the lower
24600 ends of upper and lower. */
24601 result->height = (min (lower->y + lower->height,
24602 upper->y + upper->height)
24603 - result->y);
24604 intersection_p = 1;
24608 return intersection_p;
24611 #endif /* HAVE_WINDOW_SYSTEM */
24614 /***********************************************************************
24615 Initialization
24616 ***********************************************************************/
24618 void
24619 syms_of_xdisp ()
24621 Vwith_echo_area_save_vector = Qnil;
24622 staticpro (&Vwith_echo_area_save_vector);
24624 Vmessage_stack = Qnil;
24625 staticpro (&Vmessage_stack);
24627 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24628 staticpro (&Qinhibit_redisplay);
24630 message_dolog_marker1 = Fmake_marker ();
24631 staticpro (&message_dolog_marker1);
24632 message_dolog_marker2 = Fmake_marker ();
24633 staticpro (&message_dolog_marker2);
24634 message_dolog_marker3 = Fmake_marker ();
24635 staticpro (&message_dolog_marker3);
24637 #if GLYPH_DEBUG
24638 defsubr (&Sdump_frame_glyph_matrix);
24639 defsubr (&Sdump_glyph_matrix);
24640 defsubr (&Sdump_glyph_row);
24641 defsubr (&Sdump_tool_bar_row);
24642 defsubr (&Strace_redisplay);
24643 defsubr (&Strace_to_stderr);
24644 #endif
24645 #ifdef HAVE_WINDOW_SYSTEM
24646 defsubr (&Stool_bar_lines_needed);
24647 defsubr (&Slookup_image_map);
24648 #endif
24649 defsubr (&Sformat_mode_line);
24650 defsubr (&Sinvisible_p);
24652 staticpro (&Qmenu_bar_update_hook);
24653 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
24655 staticpro (&Qoverriding_terminal_local_map);
24656 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
24658 staticpro (&Qoverriding_local_map);
24659 Qoverriding_local_map = intern_c_string ("overriding-local-map");
24661 staticpro (&Qwindow_scroll_functions);
24662 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
24664 staticpro (&Qwindow_text_change_functions);
24665 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
24667 staticpro (&Qredisplay_end_trigger_functions);
24668 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
24670 staticpro (&Qinhibit_point_motion_hooks);
24671 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
24673 Qeval = intern_c_string ("eval");
24674 staticpro (&Qeval);
24676 QCdata = intern_c_string (":data");
24677 staticpro (&QCdata);
24678 Qdisplay = intern_c_string ("display");
24679 staticpro (&Qdisplay);
24680 Qspace_width = intern_c_string ("space-width");
24681 staticpro (&Qspace_width);
24682 Qraise = intern_c_string ("raise");
24683 staticpro (&Qraise);
24684 Qslice = intern_c_string ("slice");
24685 staticpro (&Qslice);
24686 Qspace = intern_c_string ("space");
24687 staticpro (&Qspace);
24688 Qmargin = intern_c_string ("margin");
24689 staticpro (&Qmargin);
24690 Qpointer = intern_c_string ("pointer");
24691 staticpro (&Qpointer);
24692 Qleft_margin = intern_c_string ("left-margin");
24693 staticpro (&Qleft_margin);
24694 Qright_margin = intern_c_string ("right-margin");
24695 staticpro (&Qright_margin);
24696 Qcenter = intern_c_string ("center");
24697 staticpro (&Qcenter);
24698 Qline_height = intern_c_string ("line-height");
24699 staticpro (&Qline_height);
24700 QCalign_to = intern_c_string (":align-to");
24701 staticpro (&QCalign_to);
24702 QCrelative_width = intern_c_string (":relative-width");
24703 staticpro (&QCrelative_width);
24704 QCrelative_height = intern_c_string (":relative-height");
24705 staticpro (&QCrelative_height);
24706 QCeval = intern_c_string (":eval");
24707 staticpro (&QCeval);
24708 QCpropertize = intern_c_string (":propertize");
24709 staticpro (&QCpropertize);
24710 QCfile = intern_c_string (":file");
24711 staticpro (&QCfile);
24712 Qfontified = intern_c_string ("fontified");
24713 staticpro (&Qfontified);
24714 Qfontification_functions = intern_c_string ("fontification-functions");
24715 staticpro (&Qfontification_functions);
24716 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
24717 staticpro (&Qtrailing_whitespace);
24718 Qescape_glyph = intern_c_string ("escape-glyph");
24719 staticpro (&Qescape_glyph);
24720 Qnobreak_space = intern_c_string ("nobreak-space");
24721 staticpro (&Qnobreak_space);
24722 Qimage = intern_c_string ("image");
24723 staticpro (&Qimage);
24724 QCmap = intern_c_string (":map");
24725 staticpro (&QCmap);
24726 QCpointer = intern_c_string (":pointer");
24727 staticpro (&QCpointer);
24728 Qrect = intern_c_string ("rect");
24729 staticpro (&Qrect);
24730 Qcircle = intern_c_string ("circle");
24731 staticpro (&Qcircle);
24732 Qpoly = intern_c_string ("poly");
24733 staticpro (&Qpoly);
24734 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
24735 staticpro (&Qmessage_truncate_lines);
24736 Qgrow_only = intern_c_string ("grow-only");
24737 staticpro (&Qgrow_only);
24738 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
24739 staticpro (&Qinhibit_menubar_update);
24740 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
24741 staticpro (&Qinhibit_eval_during_redisplay);
24742 Qposition = intern_c_string ("position");
24743 staticpro (&Qposition);
24744 Qbuffer_position = intern_c_string ("buffer-position");
24745 staticpro (&Qbuffer_position);
24746 Qobject = intern_c_string ("object");
24747 staticpro (&Qobject);
24748 Qbar = intern_c_string ("bar");
24749 staticpro (&Qbar);
24750 Qhbar = intern_c_string ("hbar");
24751 staticpro (&Qhbar);
24752 Qbox = intern_c_string ("box");
24753 staticpro (&Qbox);
24754 Qhollow = intern_c_string ("hollow");
24755 staticpro (&Qhollow);
24756 Qhand = intern_c_string ("hand");
24757 staticpro (&Qhand);
24758 Qarrow = intern_c_string ("arrow");
24759 staticpro (&Qarrow);
24760 Qtext = intern_c_string ("text");
24761 staticpro (&Qtext);
24762 Qrisky_local_variable = intern_c_string ("risky-local-variable");
24763 staticpro (&Qrisky_local_variable);
24764 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
24765 staticpro (&Qinhibit_free_realized_faces);
24767 list_of_error = Fcons (Fcons (intern_c_string ("error"),
24768 Fcons (intern_c_string ("void-variable"), Qnil)),
24769 Qnil);
24770 staticpro (&list_of_error);
24772 Qlast_arrow_position = intern_c_string ("last-arrow-position");
24773 staticpro (&Qlast_arrow_position);
24774 Qlast_arrow_string = intern_c_string ("last-arrow-string");
24775 staticpro (&Qlast_arrow_string);
24777 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
24778 staticpro (&Qoverlay_arrow_string);
24779 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
24780 staticpro (&Qoverlay_arrow_bitmap);
24782 echo_buffer[0] = echo_buffer[1] = Qnil;
24783 staticpro (&echo_buffer[0]);
24784 staticpro (&echo_buffer[1]);
24786 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24787 staticpro (&echo_area_buffer[0]);
24788 staticpro (&echo_area_buffer[1]);
24790 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
24791 staticpro (&Vmessages_buffer_name);
24793 mode_line_proptrans_alist = Qnil;
24794 staticpro (&mode_line_proptrans_alist);
24795 mode_line_string_list = Qnil;
24796 staticpro (&mode_line_string_list);
24797 mode_line_string_face = Qnil;
24798 staticpro (&mode_line_string_face);
24799 mode_line_string_face_prop = Qnil;
24800 staticpro (&mode_line_string_face_prop);
24801 Vmode_line_unwind_vector = Qnil;
24802 staticpro (&Vmode_line_unwind_vector);
24804 help_echo_string = Qnil;
24805 staticpro (&help_echo_string);
24806 help_echo_object = Qnil;
24807 staticpro (&help_echo_object);
24808 help_echo_window = Qnil;
24809 staticpro (&help_echo_window);
24810 previous_help_echo_string = Qnil;
24811 staticpro (&previous_help_echo_string);
24812 help_echo_pos = -1;
24814 #ifdef HAVE_WINDOW_SYSTEM
24815 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24816 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24817 For example, if a block cursor is over a tab, it will be drawn as
24818 wide as that tab on the display. */);
24819 x_stretch_cursor_p = 0;
24820 #endif
24822 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24823 doc: /* *Non-nil means highlight trailing whitespace.
24824 The face used for trailing whitespace is `trailing-whitespace'. */);
24825 Vshow_trailing_whitespace = Qnil;
24827 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24828 doc: /* *Control highlighting of nobreak space and soft hyphen.
24829 A value of t means highlight the character itself (for nobreak space,
24830 use face `nobreak-space').
24831 A value of nil means no highlighting.
24832 Other values mean display the escape glyph followed by an ordinary
24833 space or ordinary hyphen. */);
24834 Vnobreak_char_display = Qt;
24836 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24837 doc: /* *The pointer shape to show in void text areas.
24838 A value of nil means to show the text pointer. Other options are `arrow',
24839 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24840 Vvoid_text_area_pointer = Qarrow;
24842 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24843 doc: /* Non-nil means don't actually do any redisplay.
24844 This is used for internal purposes. */);
24845 Vinhibit_redisplay = Qnil;
24847 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24848 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24849 Vglobal_mode_string = Qnil;
24851 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24852 doc: /* Marker for where to display an arrow on top of the buffer text.
24853 This must be the beginning of a line in order to work.
24854 See also `overlay-arrow-string'. */);
24855 Voverlay_arrow_position = Qnil;
24857 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24858 doc: /* String to display as an arrow in non-window frames.
24859 See also `overlay-arrow-position'. */);
24860 Voverlay_arrow_string = make_pure_c_string ("=>");
24862 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24863 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24864 The symbols on this list are examined during redisplay to determine
24865 where to display overlay arrows. */);
24866 Voverlay_arrow_variable_list
24867 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
24869 DEFVAR_INT ("scroll-step", &scroll_step,
24870 doc: /* *The number of lines to try scrolling a window by when point moves out.
24871 If that fails to bring point back on frame, point is centered instead.
24872 If this is zero, point is always centered after it moves off frame.
24873 If you want scrolling to always be a line at a time, you should set
24874 `scroll-conservatively' to a large value rather than set this to 1. */);
24876 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24877 doc: /* *Scroll up to this many lines, to bring point back on screen.
24878 If point moves off-screen, redisplay will scroll by up to
24879 `scroll-conservatively' lines in order to bring point just barely
24880 onto the screen again. If that cannot be done, then redisplay
24881 recenters point as usual.
24883 A value of zero means always recenter point if it moves off screen. */);
24884 scroll_conservatively = 0;
24886 DEFVAR_INT ("scroll-margin", &scroll_margin,
24887 doc: /* *Number of lines of margin at the top and bottom of a window.
24888 Recenter the window whenever point gets within this many lines
24889 of the top or bottom of the window. */);
24890 scroll_margin = 0;
24892 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24893 doc: /* Pixels per inch value for non-window system displays.
24894 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24895 Vdisplay_pixels_per_inch = make_float (72.0);
24897 #if GLYPH_DEBUG
24898 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24899 #endif
24901 DEFVAR_LISP ("truncate-partial-width-windows",
24902 &Vtruncate_partial_width_windows,
24903 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24904 For an integer value, truncate lines in each window narrower than the
24905 full frame width, provided the window width is less than that integer;
24906 otherwise, respect the value of `truncate-lines'.
24908 For any other non-nil value, truncate lines in all windows that do
24909 not span the full frame width.
24911 A value of nil means to respect the value of `truncate-lines'.
24913 If `word-wrap' is enabled, you might want to reduce this. */);
24914 Vtruncate_partial_width_windows = make_number (50);
24916 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24917 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24918 Any other value means to use the appropriate face, `mode-line',
24919 `header-line', or `menu' respectively. */);
24920 mode_line_inverse_video = 1;
24922 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24923 doc: /* *Maximum buffer size for which line number should be displayed.
24924 If the buffer is bigger than this, the line number does not appear
24925 in the mode line. A value of nil means no limit. */);
24926 Vline_number_display_limit = Qnil;
24928 DEFVAR_INT ("line-number-display-limit-width",
24929 &line_number_display_limit_width,
24930 doc: /* *Maximum line width (in characters) for line number display.
24931 If the average length of the lines near point is bigger than this, then the
24932 line number may be omitted from the mode line. */);
24933 line_number_display_limit_width = 200;
24935 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24936 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24937 highlight_nonselected_windows = 0;
24939 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24940 doc: /* Non-nil if more than one frame is visible on this display.
24941 Minibuffer-only frames don't count, but iconified frames do.
24942 This variable is not guaranteed to be accurate except while processing
24943 `frame-title-format' and `icon-title-format'. */);
24945 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24946 doc: /* Template for displaying the title bar of visible frames.
24947 \(Assuming the window manager supports this feature.)
24949 This variable has the same structure as `mode-line-format', except that
24950 the %c and %l constructs are ignored. It is used only on frames for
24951 which no explicit name has been set \(see `modify-frame-parameters'). */);
24953 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24954 doc: /* Template for displaying the title bar of an iconified frame.
24955 \(Assuming the window manager supports this feature.)
24956 This variable has the same structure as `mode-line-format' (which see),
24957 and is used only on frames for which no explicit name has been set
24958 \(see `modify-frame-parameters'). */);
24959 Vicon_title_format
24960 = Vframe_title_format
24961 = pure_cons (intern_c_string ("multiple-frames"),
24962 pure_cons (make_pure_c_string ("%b"),
24963 pure_cons (pure_cons (empty_unibyte_string,
24964 pure_cons (intern_c_string ("invocation-name"),
24965 pure_cons (make_pure_c_string ("@"),
24966 pure_cons (intern_c_string ("system-name"),
24967 Qnil)))),
24968 Qnil)));
24970 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24971 doc: /* Maximum number of lines to keep in the message log buffer.
24972 If nil, disable message logging. If t, log messages but don't truncate
24973 the buffer when it becomes large. */);
24974 Vmessage_log_max = make_number (100);
24976 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24977 doc: /* Functions called before redisplay, if window sizes have changed.
24978 The value should be a list of functions that take one argument.
24979 Just before redisplay, for each frame, if any of its windows have changed
24980 size since the last redisplay, or have been split or deleted,
24981 all the functions in the list are called, with the frame as argument. */);
24982 Vwindow_size_change_functions = Qnil;
24984 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24985 doc: /* List of functions to call before redisplaying a window with scrolling.
24986 Each function is called with two arguments, the window and its new
24987 display-start position. Note that these functions are also called by
24988 `set-window-buffer'. Also note that the value of `window-end' is not
24989 valid when these functions are called. */);
24990 Vwindow_scroll_functions = Qnil;
24992 DEFVAR_LISP ("window-text-change-functions",
24993 &Vwindow_text_change_functions,
24994 doc: /* Functions to call in redisplay when text in the window might change. */);
24995 Vwindow_text_change_functions = Qnil;
24997 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24998 doc: /* Functions called when redisplay of a window reaches the end trigger.
24999 Each function is called with two arguments, the window and the end trigger value.
25000 See `set-window-redisplay-end-trigger'. */);
25001 Vredisplay_end_trigger_functions = Qnil;
25003 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
25004 doc: /* *Non-nil means autoselect window with mouse pointer.
25005 If nil, do not autoselect windows.
25006 A positive number means delay autoselection by that many seconds: a
25007 window is autoselected only after the mouse has remained in that
25008 window for the duration of the delay.
25009 A negative number has a similar effect, but causes windows to be
25010 autoselected only after the mouse has stopped moving. \(Because of
25011 the way Emacs compares mouse events, you will occasionally wait twice
25012 that time before the window gets selected.\)
25013 Any other value means to autoselect window instantaneously when the
25014 mouse pointer enters it.
25016 Autoselection selects the minibuffer only if it is active, and never
25017 unselects the minibuffer if it is active.
25019 When customizing this variable make sure that the actual value of
25020 `focus-follows-mouse' matches the behavior of your window manager. */);
25021 Vmouse_autoselect_window = Qnil;
25023 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
25024 doc: /* *Non-nil means automatically resize tool-bars.
25025 This dynamically changes the tool-bar's height to the minimum height
25026 that is needed to make all tool-bar items visible.
25027 If value is `grow-only', the tool-bar's height is only increased
25028 automatically; to decrease the tool-bar height, use \\[recenter]. */);
25029 Vauto_resize_tool_bars = Qt;
25031 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
25032 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
25033 auto_raise_tool_bar_buttons_p = 1;
25035 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
25036 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
25037 make_cursor_line_fully_visible_p = 1;
25039 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
25040 doc: /* *Border below tool-bar in pixels.
25041 If an integer, use it as the height of the border.
25042 If it is one of `internal-border-width' or `border-width', use the
25043 value of the corresponding frame parameter.
25044 Otherwise, no border is added below the tool-bar. */);
25045 Vtool_bar_border = Qinternal_border_width;
25047 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
25048 doc: /* *Margin around tool-bar buttons in pixels.
25049 If an integer, use that for both horizontal and vertical margins.
25050 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
25051 HORZ specifying the horizontal margin, and VERT specifying the
25052 vertical margin. */);
25053 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
25055 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
25056 doc: /* *Relief thickness of tool-bar buttons. */);
25057 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
25059 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
25060 doc: /* List of functions to call to fontify regions of text.
25061 Each function is called with one argument POS. Functions must
25062 fontify a region starting at POS in the current buffer, and give
25063 fontified regions the property `fontified'. */);
25064 Vfontification_functions = Qnil;
25065 Fmake_variable_buffer_local (Qfontification_functions);
25067 DEFVAR_BOOL ("unibyte-display-via-language-environment",
25068 &unibyte_display_via_language_environment,
25069 doc: /* *Non-nil means display unibyte text according to language environment.
25070 Specifically, this means that raw bytes in the range 160-255 decimal
25071 are displayed by converting them to the equivalent multibyte characters
25072 according to the current language environment. As a result, they are
25073 displayed according to the current fontset.
25075 Note that this variable affects only how these bytes are displayed,
25076 but does not change the fact they are interpreted as raw bytes. */);
25077 unibyte_display_via_language_environment = 0;
25079 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
25080 doc: /* *Maximum height for resizing mini-windows.
25081 If a float, it specifies a fraction of the mini-window frame's height.
25082 If an integer, it specifies a number of lines. */);
25083 Vmax_mini_window_height = make_float (0.25);
25085 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
25086 doc: /* *How to resize mini-windows.
25087 A value of nil means don't automatically resize mini-windows.
25088 A value of t means resize them to fit the text displayed in them.
25089 A value of `grow-only', the default, means let mini-windows grow
25090 only, until their display becomes empty, at which point the windows
25091 go back to their normal size. */);
25092 Vresize_mini_windows = Qgrow_only;
25094 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25095 doc: /* Alist specifying how to blink the cursor off.
25096 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25097 `cursor-type' frame-parameter or variable equals ON-STATE,
25098 comparing using `equal', Emacs uses OFF-STATE to specify
25099 how to blink it off. ON-STATE and OFF-STATE are values for
25100 the `cursor-type' frame parameter.
25102 If a frame's ON-STATE has no entry in this list,
25103 the frame's other specifications determine how to blink the cursor off. */);
25104 Vblink_cursor_alist = Qnil;
25106 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25107 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25108 automatic_hscrolling_p = 1;
25109 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25110 staticpro (&Qauto_hscroll_mode);
25112 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25113 doc: /* *How many columns away from the window edge point is allowed to get
25114 before automatic hscrolling will horizontally scroll the window. */);
25115 hscroll_margin = 5;
25117 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25118 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25119 When point is less than `hscroll-margin' columns from the window
25120 edge, automatic hscrolling will scroll the window by the amount of columns
25121 determined by this variable. If its value is a positive integer, scroll that
25122 many columns. If it's a positive floating-point number, it specifies the
25123 fraction of the window's width to scroll. If it's nil or zero, point will be
25124 centered horizontally after the scroll. Any other value, including negative
25125 numbers, are treated as if the value were zero.
25127 Automatic hscrolling always moves point outside the scroll margin, so if
25128 point was more than scroll step columns inside the margin, the window will
25129 scroll more than the value given by the scroll step.
25131 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25132 and `scroll-right' overrides this variable's effect. */);
25133 Vhscroll_step = make_number (0);
25135 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25136 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25137 Bind this around calls to `message' to let it take effect. */);
25138 message_truncate_lines = 0;
25140 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25141 doc: /* Normal hook run to update the menu bar definitions.
25142 Redisplay runs this hook before it redisplays the menu bar.
25143 This is used to update submenus such as Buffers,
25144 whose contents depend on various data. */);
25145 Vmenu_bar_update_hook = Qnil;
25147 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25148 doc: /* Frame for which we are updating a menu.
25149 The enable predicate for a menu binding should check this variable. */);
25150 Vmenu_updating_frame = Qnil;
25152 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25153 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25154 inhibit_menubar_update = 0;
25156 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25157 doc: /* Prefix prepended to all continuation lines at display time.
25158 The value may be a string, an image, or a stretch-glyph; it is
25159 interpreted in the same way as the value of a `display' text property.
25161 This variable is overridden by any `wrap-prefix' text or overlay
25162 property.
25164 To add a prefix to non-continuation lines, use `line-prefix'. */);
25165 Vwrap_prefix = Qnil;
25166 staticpro (&Qwrap_prefix);
25167 Qwrap_prefix = intern_c_string ("wrap-prefix");
25168 Fmake_variable_buffer_local (Qwrap_prefix);
25170 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25171 doc: /* Prefix prepended to all non-continuation lines at display time.
25172 The value may be a string, an image, or a stretch-glyph; it is
25173 interpreted in the same way as the value of a `display' text property.
25175 This variable is overridden by any `line-prefix' text or overlay
25176 property.
25178 To add a prefix to continuation lines, use `wrap-prefix'. */);
25179 Vline_prefix = Qnil;
25180 staticpro (&Qline_prefix);
25181 Qline_prefix = intern_c_string ("line-prefix");
25182 Fmake_variable_buffer_local (Qline_prefix);
25184 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25185 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25186 inhibit_eval_during_redisplay = 0;
25188 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25189 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25190 inhibit_free_realized_faces = 0;
25192 #if GLYPH_DEBUG
25193 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25194 doc: /* Inhibit try_window_id display optimization. */);
25195 inhibit_try_window_id = 0;
25197 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25198 doc: /* Inhibit try_window_reusing display optimization. */);
25199 inhibit_try_window_reusing = 0;
25201 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25202 doc: /* Inhibit try_cursor_movement display optimization. */);
25203 inhibit_try_cursor_movement = 0;
25204 #endif /* GLYPH_DEBUG */
25206 DEFVAR_INT ("overline-margin", &overline_margin,
25207 doc: /* *Space between overline and text, in pixels.
25208 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25209 margin to the caracter height. */);
25210 overline_margin = 2;
25212 DEFVAR_INT ("underline-minimum-offset",
25213 &underline_minimum_offset,
25214 doc: /* Minimum distance between baseline and underline.
25215 This can improve legibility of underlined text at small font sizes,
25216 particularly when using variable `x-use-underline-position-properties'
25217 with fonts that specify an UNDERLINE_POSITION relatively close to the
25218 baseline. The default value is 1. */);
25219 underline_minimum_offset = 1;
25221 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25222 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25223 display_hourglass_p = 1;
25225 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25226 doc: /* *Seconds to wait before displaying an hourglass pointer.
25227 Value must be an integer or float. */);
25228 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25230 hourglass_atimer = NULL;
25231 hourglass_shown_p = 0;
25235 /* Initialize this module when Emacs starts. */
25237 void
25238 init_xdisp ()
25240 Lisp_Object root_window;
25241 struct window *mini_w;
25243 current_header_line_height = current_mode_line_height = -1;
25245 CHARPOS (this_line_start_pos) = 0;
25247 mini_w = XWINDOW (minibuf_window);
25248 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25250 if (!noninteractive)
25252 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25253 int i;
25255 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25256 set_window_height (root_window,
25257 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25259 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25260 set_window_height (minibuf_window, 1, 0);
25262 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25263 mini_w->total_cols = make_number (FRAME_COLS (f));
25265 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25266 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25267 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25269 /* The default ellipsis glyphs `...'. */
25270 for (i = 0; i < 3; ++i)
25271 default_invis_vector[i] = make_number ('.');
25275 /* Allocate the buffer for frame titles.
25276 Also used for `format-mode-line'. */
25277 int size = 100;
25278 mode_line_noprop_buf = (char *) xmalloc (size);
25279 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25280 mode_line_noprop_ptr = mode_line_noprop_buf;
25281 mode_line_target = MODE_LINE_DISPLAY;
25284 help_echo_showing_p = 0;
25287 /* Since w32 does not support atimers, it defines its own implementation of
25288 the following three functions in w32fns.c. */
25289 #ifndef WINDOWSNT
25291 /* Platform-independent portion of hourglass implementation. */
25293 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25295 hourglass_started ()
25297 return hourglass_shown_p || hourglass_atimer != NULL;
25300 /* Cancel a currently active hourglass timer, and start a new one. */
25301 void
25302 start_hourglass ()
25304 #if defined (HAVE_WINDOW_SYSTEM)
25305 EMACS_TIME delay;
25306 int secs, usecs = 0;
25308 cancel_hourglass ();
25310 if (INTEGERP (Vhourglass_delay)
25311 && XINT (Vhourglass_delay) > 0)
25312 secs = XFASTINT (Vhourglass_delay);
25313 else if (FLOATP (Vhourglass_delay)
25314 && XFLOAT_DATA (Vhourglass_delay) > 0)
25316 Lisp_Object tem;
25317 tem = Ftruncate (Vhourglass_delay, Qnil);
25318 secs = XFASTINT (tem);
25319 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25321 else
25322 secs = DEFAULT_HOURGLASS_DELAY;
25324 EMACS_SET_SECS_USECS (delay, secs, usecs);
25325 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25326 show_hourglass, NULL);
25327 #endif
25331 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25332 shown. */
25333 void
25334 cancel_hourglass ()
25336 #if defined (HAVE_WINDOW_SYSTEM)
25337 if (hourglass_atimer)
25339 cancel_atimer (hourglass_atimer);
25340 hourglass_atimer = NULL;
25343 if (hourglass_shown_p)
25344 hide_hourglass ();
25345 #endif
25347 #endif /* ! WINDOWSNT */
25349 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25350 (do not change this comment) */