merge upstream.
[emacs.git] / src / xdisp.c
blobb09a1547a576cac4177a0dcec0abadca858ff9ba
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
315 #include "font.h"
316 #ifdef HAVE_XWIDGETS
317 #include "xwidget.h"
318 #endif
319 #ifndef FRAME_X_OUTPUT
320 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
321 #endif
323 #define INFINITY 10000000
325 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
326 Lisp_Object Qwindow_scroll_functions;
327 static Lisp_Object Qwindow_text_change_functions;
328 static Lisp_Object Qredisplay_end_trigger_functions;
329 Lisp_Object Qinhibit_point_motion_hooks;
330 static Lisp_Object QCeval, QCpropertize;
331 Lisp_Object QCfile, QCdata;
332 static Lisp_Object Qfontified;
333 static Lisp_Object Qgrow_only;
334 static Lisp_Object Qinhibit_eval_during_redisplay;
335 static Lisp_Object Qbuffer_position, Qposition, Qobject;
336 static Lisp_Object Qright_to_left, Qleft_to_right;
338 /* Cursor shapes */
339 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
341 /* Pointer shapes */
342 static Lisp_Object Qarrow, Qhand;
343 Lisp_Object Qtext;
345 /* Holds the list (error). */
346 static Lisp_Object list_of_error;
348 static Lisp_Object Qfontification_functions;
350 static Lisp_Object Qwrap_prefix;
351 static Lisp_Object Qline_prefix;
353 /* Non-nil means don't actually do any redisplay. */
355 Lisp_Object Qinhibit_redisplay;
357 /* Names of text properties relevant for redisplay. */
359 Lisp_Object Qdisplay;
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
370 #ifdef HAVE_WINDOW_SYSTEM
372 /* Test if overflow newline into fringe. Called with iterator IT
373 at or past right window margin, and with IT->current_x set. */
375 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
376 (!NILP (Voverflow_newline_into_fringe) \
377 && FRAME_WINDOW_P ((IT)->f) \
378 && ((IT)->bidi_it.paragraph_dir == R2L \
379 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
380 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
381 && (IT)->current_x == (IT)->last_visible_x \
382 && (IT)->line_wrap != WORD_WRAP)
384 #else /* !HAVE_WINDOW_SYSTEM */
385 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
386 #endif /* HAVE_WINDOW_SYSTEM */
388 /* Test if the display element loaded in IT is a space or tab
389 character. This is used to determine word wrapping. */
391 #define IT_DISPLAYING_WHITESPACE(it) \
392 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
394 /* Name of the face used to highlight trailing whitespace. */
396 static Lisp_Object Qtrailing_whitespace;
398 /* Name and number of the face used to highlight escape glyphs. */
400 static Lisp_Object Qescape_glyph;
402 /* Name and number of the face used to highlight non-breaking spaces. */
404 static Lisp_Object Qnobreak_space;
406 /* The symbol `image' which is the car of the lists used to represent
407 images in Lisp. Also a tool bar style. */
409 Lisp_Object Qimage;
411 /* The image map types. */
412 Lisp_Object QCmap;
413 static Lisp_Object QCpointer;
414 static Lisp_Object Qrect, Qcircle, Qpoly;
416 /* Tool bar styles */
417 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
419 /* Non-zero means print newline to stdout before next mini-buffer
420 message. */
422 int noninteractive_need_newline;
424 /* Non-zero means print newline to message log before next message. */
426 static int message_log_need_newline;
428 /* Three markers that message_dolog uses.
429 It could allocate them itself, but that causes trouble
430 in handling memory-full errors. */
431 static Lisp_Object message_dolog_marker1;
432 static Lisp_Object message_dolog_marker2;
433 static Lisp_Object message_dolog_marker3;
435 /* The buffer position of the first character appearing entirely or
436 partially on the line of the selected window which contains the
437 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
438 redisplay optimization in redisplay_internal. */
440 static struct text_pos this_line_start_pos;
442 /* Number of characters past the end of the line above, including the
443 terminating newline. */
445 static struct text_pos this_line_end_pos;
447 /* The vertical positions and the height of this line. */
449 static int this_line_vpos;
450 static int this_line_y;
451 static int this_line_pixel_height;
453 /* X position at which this display line starts. Usually zero;
454 negative if first character is partially visible. */
456 static int this_line_start_x;
458 /* The smallest character position seen by move_it_* functions as they
459 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
460 hscrolled lines, see display_line. */
462 static struct text_pos this_line_min_pos;
464 /* Buffer that this_line_.* variables are referring to. */
466 static struct buffer *this_line_buffer;
469 /* Values of those variables at last redisplay are stored as
470 properties on `overlay-arrow-position' symbol. However, if
471 Voverlay_arrow_position is a marker, last-arrow-position is its
472 numerical position. */
474 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
476 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
477 properties on a symbol in overlay-arrow-variable-list. */
479 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
481 Lisp_Object Qmenu_bar_update_hook;
483 /* Nonzero if an overlay arrow has been displayed in this window. */
485 static int overlay_arrow_seen;
487 /* Number of windows showing the buffer of the selected window (or
488 another buffer with the same base buffer). keyboard.c refers to
489 this. */
491 int buffer_shared;
493 /* Vector containing glyphs for an ellipsis `...'. */
495 static Lisp_Object default_invis_vector[3];
497 /* This is the window where the echo area message was displayed. It
498 is always a mini-buffer window, but it may not be the same window
499 currently active as a mini-buffer. */
501 Lisp_Object echo_area_window;
503 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
504 pushes the current message and the value of
505 message_enable_multibyte on the stack, the function restore_message
506 pops the stack and displays MESSAGE again. */
508 static Lisp_Object Vmessage_stack;
510 /* Nonzero means multibyte characters were enabled when the echo area
511 message was specified. */
513 static int message_enable_multibyte;
515 /* Nonzero if we should redraw the mode lines on the next redisplay. */
517 int update_mode_lines;
519 /* Nonzero if window sizes or contents have changed since last
520 redisplay that finished. */
522 int windows_or_buffers_changed;
524 /* Nonzero means a frame's cursor type has been changed. */
526 int cursor_type_changed;
528 /* Nonzero after display_mode_line if %l was used and it displayed a
529 line number. */
531 static int line_number_displayed;
533 /* The name of the *Messages* buffer, a string. */
535 static Lisp_Object Vmessages_buffer_name;
537 /* Current, index 0, and last displayed echo area message. Either
538 buffers from echo_buffers, or nil to indicate no message. */
540 Lisp_Object echo_area_buffer[2];
542 /* The buffers referenced from echo_area_buffer. */
544 static Lisp_Object echo_buffer[2];
546 /* A vector saved used in with_area_buffer to reduce consing. */
548 static Lisp_Object Vwith_echo_area_save_vector;
550 /* Non-zero means display_echo_area should display the last echo area
551 message again. Set by redisplay_preserve_echo_area. */
553 static int display_last_displayed_message_p;
555 /* Nonzero if echo area is being used by print; zero if being used by
556 message. */
558 static int message_buf_print;
560 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
562 static Lisp_Object Qinhibit_menubar_update;
563 static Lisp_Object Qmessage_truncate_lines;
565 /* Set to 1 in clear_message to make redisplay_internal aware
566 of an emptied echo area. */
568 static int message_cleared_p;
570 /* A scratch glyph row with contents used for generating truncation
571 glyphs. Also used in direct_output_for_insert. */
573 #define MAX_SCRATCH_GLYPHS 100
574 static struct glyph_row scratch_glyph_row;
575 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
577 /* Ascent and height of the last line processed by move_it_to. */
579 static int last_max_ascent, last_height;
581 /* Non-zero if there's a help-echo in the echo area. */
583 int help_echo_showing_p;
585 /* If >= 0, computed, exact values of mode-line and header-line height
586 to use in the macros CURRENT_MODE_LINE_HEIGHT and
587 CURRENT_HEADER_LINE_HEIGHT. */
589 int current_mode_line_height, current_header_line_height;
591 /* The maximum distance to look ahead for text properties. Values
592 that are too small let us call compute_char_face and similar
593 functions too often which is expensive. Values that are too large
594 let us call compute_char_face and alike too often because we
595 might not be interested in text properties that far away. */
597 #define TEXT_PROP_DISTANCE_LIMIT 100
599 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
600 iterator state and later restore it. This is needed because the
601 bidi iterator on bidi.c keeps a stacked cache of its states, which
602 is really a singleton. When we use scratch iterator objects to
603 move around the buffer, we can cause the bidi cache to be pushed or
604 popped, and therefore we need to restore the cache state when we
605 return to the original iterator. */
606 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
607 do { \
608 if (CACHE) \
609 bidi_unshelve_cache (CACHE, 1); \
610 ITCOPY = ITORIG; \
611 CACHE = bidi_shelve_cache(); \
612 } while (0)
614 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
615 do { \
616 if (pITORIG != pITCOPY) \
617 *(pITORIG) = *(pITCOPY); \
618 bidi_unshelve_cache (CACHE, 0); \
619 CACHE = NULL; \
620 } while (0)
622 #if GLYPH_DEBUG
624 /* Non-zero means print traces of redisplay if compiled with
625 GLYPH_DEBUG != 0. */
627 int trace_redisplay_p;
629 #endif /* GLYPH_DEBUG */
631 #ifdef DEBUG_TRACE_MOVE
632 /* Non-zero means trace with TRACE_MOVE to stderr. */
633 int trace_move;
635 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
636 #else
637 #define TRACE_MOVE(x) (void) 0
638 #endif
640 static Lisp_Object Qauto_hscroll_mode;
642 /* Buffer being redisplayed -- for redisplay_window_error. */
644 static struct buffer *displayed_buffer;
646 /* Value returned from text property handlers (see below). */
648 enum prop_handled
650 HANDLED_NORMALLY,
651 HANDLED_RECOMPUTE_PROPS,
652 HANDLED_OVERLAY_STRING_CONSUMED,
653 HANDLED_RETURN
656 /* A description of text properties that redisplay is interested
657 in. */
659 struct props
661 /* The name of the property. */
662 Lisp_Object *name;
664 /* A unique index for the property. */
665 enum prop_idx idx;
667 /* A handler function called to set up iterator IT from the property
668 at IT's current position. Value is used to steer handle_stop. */
669 enum prop_handled (*handler) (struct it *it);
672 static enum prop_handled handle_face_prop (struct it *);
673 static enum prop_handled handle_invisible_prop (struct it *);
674 static enum prop_handled handle_display_prop (struct it *);
675 static enum prop_handled handle_composition_prop (struct it *);
676 static enum prop_handled handle_overlay_change (struct it *);
677 static enum prop_handled handle_fontified_prop (struct it *);
679 /* Properties handled by iterators. */
681 static struct props it_props[] =
683 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
684 /* Handle `face' before `display' because some sub-properties of
685 `display' need to know the face. */
686 {&Qface, FACE_PROP_IDX, handle_face_prop},
687 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
688 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
689 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
690 {NULL, 0, NULL}
693 /* Value is the position described by X. If X is a marker, value is
694 the marker_position of X. Otherwise, value is X. */
696 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
698 /* Enumeration returned by some move_it_.* functions internally. */
700 enum move_it_result
702 /* Not used. Undefined value. */
703 MOVE_UNDEFINED,
705 /* Move ended at the requested buffer position or ZV. */
706 MOVE_POS_MATCH_OR_ZV,
708 /* Move ended at the requested X pixel position. */
709 MOVE_X_REACHED,
711 /* Move within a line ended at the end of a line that must be
712 continued. */
713 MOVE_LINE_CONTINUED,
715 /* Move within a line ended at the end of a line that would
716 be displayed truncated. */
717 MOVE_LINE_TRUNCATED,
719 /* Move within a line ended at a line end. */
720 MOVE_NEWLINE_OR_CR
723 /* This counter is used to clear the face cache every once in a while
724 in redisplay_internal. It is incremented for each redisplay.
725 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
726 cleared. */
728 #define CLEAR_FACE_CACHE_COUNT 500
729 static int clear_face_cache_count;
731 /* Similarly for the image cache. */
733 #ifdef HAVE_WINDOW_SYSTEM
734 #define CLEAR_IMAGE_CACHE_COUNT 101
735 static int clear_image_cache_count;
737 /* Null glyph slice */
738 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
739 #endif
741 /* Non-zero while redisplay_internal is in progress. */
743 int redisplaying_p;
745 static Lisp_Object Qinhibit_free_realized_faces;
747 /* If a string, XTread_socket generates an event to display that string.
748 (The display is done in read_char.) */
750 Lisp_Object help_echo_string;
751 Lisp_Object help_echo_window;
752 Lisp_Object help_echo_object;
753 EMACS_INT help_echo_pos;
755 /* Temporary variable for XTread_socket. */
757 Lisp_Object previous_help_echo_string;
759 /* Platform-independent portion of hourglass implementation. */
761 /* Non-zero means an hourglass cursor is currently shown. */
762 int hourglass_shown_p;
764 /* If non-null, an asynchronous timer that, when it expires, displays
765 an hourglass cursor on all frames. */
766 struct atimer *hourglass_atimer;
768 /* Name of the face used to display glyphless characters. */
769 Lisp_Object Qglyphless_char;
771 /* Symbol for the purpose of Vglyphless_char_display. */
772 static Lisp_Object Qglyphless_char_display;
774 /* Method symbols for Vglyphless_char_display. */
775 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
777 /* Default pixel width of `thin-space' display method. */
778 #define THIN_SPACE_WIDTH 1
780 /* Default number of seconds to wait before displaying an hourglass
781 cursor. */
782 #define DEFAULT_HOURGLASS_DELAY 1
785 /* Function prototypes. */
787 static void setup_for_ellipsis (struct it *, int);
788 static void set_iterator_to_next (struct it *, int);
789 static void mark_window_display_accurate_1 (struct window *, int);
790 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
791 static int display_prop_string_p (Lisp_Object, Lisp_Object);
792 static int cursor_row_p (struct glyph_row *);
793 static int redisplay_mode_lines (Lisp_Object, int);
794 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
796 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
798 static void handle_line_prefix (struct it *);
800 static void pint2str (char *, int, EMACS_INT);
801 static void pint2hrstr (char *, int, EMACS_INT);
802 static struct text_pos run_window_scroll_functions (Lisp_Object,
803 struct text_pos);
804 static void reconsider_clip_changes (struct window *, struct buffer *);
805 static int text_outside_line_unchanged_p (struct window *,
806 EMACS_INT, EMACS_INT);
807 static void store_mode_line_noprop_char (char);
808 static int store_mode_line_noprop (const char *, int, int);
809 static void handle_stop (struct it *);
810 static void handle_stop_backwards (struct it *, EMACS_INT);
811 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
812 static void ensure_echo_area_buffers (void);
813 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
814 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
815 static int with_echo_area_buffer (struct window *, int,
816 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
817 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
818 static void clear_garbaged_frames (void);
819 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
820 static void pop_message (void);
821 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
822 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
823 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
824 static int display_echo_area (struct window *);
825 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
826 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
827 static Lisp_Object unwind_redisplay (Lisp_Object);
828 static int string_char_and_length (const unsigned char *, int *);
829 static struct text_pos display_prop_end (struct it *, Lisp_Object,
830 struct text_pos);
831 static int compute_window_start_on_continuation_line (struct window *);
832 static Lisp_Object safe_eval_handler (Lisp_Object);
833 static void insert_left_trunc_glyphs (struct it *);
834 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
835 Lisp_Object);
836 static void extend_face_to_end_of_line (struct it *);
837 static int append_space_for_newline (struct it *, int);
838 static int cursor_row_fully_visible_p (struct window *, int, int);
839 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
840 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
841 static int trailing_whitespace_p (EMACS_INT);
842 static intmax_t message_log_check_duplicate (EMACS_INT, EMACS_INT);
843 static void push_it (struct it *, struct text_pos *);
844 static void pop_it (struct it *);
845 static void sync_frame_with_window_matrix_rows (struct window *);
846 static void select_frame_for_redisplay (Lisp_Object);
847 static void redisplay_internal (void);
848 static int echo_area_display (int);
849 static void redisplay_windows (Lisp_Object);
850 static void redisplay_window (Lisp_Object, int);
851 static Lisp_Object redisplay_window_error (Lisp_Object);
852 static Lisp_Object redisplay_window_0 (Lisp_Object);
853 static Lisp_Object redisplay_window_1 (Lisp_Object);
854 static int set_cursor_from_row (struct window *, struct glyph_row *,
855 struct glyph_matrix *, EMACS_INT, EMACS_INT,
856 int, int);
857 static int update_menu_bar (struct frame *, int, int);
858 static int try_window_reusing_current_matrix (struct window *);
859 static int try_window_id (struct window *);
860 static int display_line (struct it *);
861 static int display_mode_lines (struct window *);
862 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
863 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
864 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
865 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
866 static void display_menu_bar (struct window *);
867 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
868 EMACS_INT *);
869 static int display_string (const char *, Lisp_Object, Lisp_Object,
870 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
871 static void compute_line_metrics (struct it *);
872 static void run_redisplay_end_trigger_hook (struct it *);
873 static int get_overlay_strings (struct it *, EMACS_INT);
874 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
875 static void next_overlay_string (struct it *);
876 static void reseat (struct it *, struct text_pos, int);
877 static void reseat_1 (struct it *, struct text_pos, int);
878 static void back_to_previous_visible_line_start (struct it *);
879 void reseat_at_previous_visible_line_start (struct it *);
880 static void reseat_at_next_visible_line_start (struct it *, int);
881 static int next_element_from_ellipsis (struct it *);
882 static int next_element_from_display_vector (struct it *);
883 static int next_element_from_string (struct it *);
884 static int next_element_from_c_string (struct it *);
885 static int next_element_from_buffer (struct it *);
886 static int next_element_from_composition (struct it *);
887 static int next_element_from_image (struct it *);
888 #ifdef HAVE_XWIDGETS
889 static int next_element_from_xwidget(struct it *);
890 #endif
891 static int next_element_from_stretch (struct it *);
892 static void load_overlay_strings (struct it *, EMACS_INT);
893 static int init_from_display_pos (struct it *, struct window *,
894 struct display_pos *);
895 static void reseat_to_string (struct it *, const char *,
896 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
897 static int get_next_display_element (struct it *);
898 static enum move_it_result
899 move_it_in_display_line_to (struct it *, EMACS_INT, int,
900 enum move_operation_enum);
901 void move_it_vertically_backward (struct it *, int);
902 static void init_to_row_start (struct it *, struct window *,
903 struct glyph_row *);
904 static int init_to_row_end (struct it *, struct window *,
905 struct glyph_row *);
906 static void back_to_previous_line_start (struct it *);
907 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
908 static struct text_pos string_pos_nchars_ahead (struct text_pos,
909 Lisp_Object, EMACS_INT);
910 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
911 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
912 static EMACS_INT number_of_chars (const char *, int);
913 static void compute_stop_pos (struct it *);
914 static void compute_string_pos (struct text_pos *, struct text_pos,
915 Lisp_Object);
916 static int face_before_or_after_it_pos (struct it *, int);
917 static EMACS_INT next_overlay_change (EMACS_INT);
918 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
919 Lisp_Object, struct text_pos *, EMACS_INT, int);
920 static int handle_single_display_spec (struct it *, Lisp_Object,
921 Lisp_Object, Lisp_Object,
922 struct text_pos *, EMACS_INT, int, int);
923 static int underlying_face_id (struct it *);
924 static int in_ellipses_for_invisible_text_p (struct display_pos *,
925 struct window *);
927 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
928 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
930 #ifdef HAVE_WINDOW_SYSTEM
932 static void x_consider_frame_title (Lisp_Object);
933 static int tool_bar_lines_needed (struct frame *, int *);
934 static void update_tool_bar (struct frame *, int);
935 static void build_desired_tool_bar_string (struct frame *f);
936 static int redisplay_tool_bar (struct frame *);
937 static void display_tool_bar_line (struct it *, int);
938 static void notice_overwritten_cursor (struct window *,
939 enum glyph_row_area,
940 int, int, int, int);
941 static void append_stretch_glyph (struct it *, Lisp_Object,
942 int, int, int);
945 #endif /* HAVE_WINDOW_SYSTEM */
947 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
948 static int coords_in_mouse_face_p (struct window *, int, int);
952 /***********************************************************************
953 Window display dimensions
954 ***********************************************************************/
956 /* Return the bottom boundary y-position for text lines in window W.
957 This is the first y position at which a line cannot start.
958 It is relative to the top of the window.
960 This is the height of W minus the height of a mode line, if any. */
962 inline int
963 window_text_bottom_y (struct window *w)
965 int height = WINDOW_TOTAL_HEIGHT (w);
967 if (WINDOW_WANTS_MODELINE_P (w))
968 height -= CURRENT_MODE_LINE_HEIGHT (w);
969 return height;
972 /* Return the pixel width of display area AREA of window W. AREA < 0
973 means return the total width of W, not including fringes to
974 the left and right of the window. */
976 inline int
977 window_box_width (struct window *w, int area)
979 int cols = XFASTINT (w->total_cols);
980 int pixels = 0;
982 if (!w->pseudo_window_p)
984 cols -= WINDOW_SCROLL_BAR_COLS (w);
986 if (area == TEXT_AREA)
988 if (INTEGERP (w->left_margin_cols))
989 cols -= XFASTINT (w->left_margin_cols);
990 if (INTEGERP (w->right_margin_cols))
991 cols -= XFASTINT (w->right_margin_cols);
992 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
994 else if (area == LEFT_MARGIN_AREA)
996 cols = (INTEGERP (w->left_margin_cols)
997 ? XFASTINT (w->left_margin_cols) : 0);
998 pixels = 0;
1000 else if (area == RIGHT_MARGIN_AREA)
1002 cols = (INTEGERP (w->right_margin_cols)
1003 ? XFASTINT (w->right_margin_cols) : 0);
1004 pixels = 0;
1008 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1012 /* Return the pixel height of the display area of window W, not
1013 including mode lines of W, if any. */
1015 inline int
1016 window_box_height (struct window *w)
1018 struct frame *f = XFRAME (w->frame);
1019 int height = WINDOW_TOTAL_HEIGHT (w);
1021 xassert (height >= 0);
1023 /* Note: the code below that determines the mode-line/header-line
1024 height is essentially the same as that contained in the macro
1025 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1026 the appropriate glyph row has its `mode_line_p' flag set,
1027 and if it doesn't, uses estimate_mode_line_height instead. */
1029 if (WINDOW_WANTS_MODELINE_P (w))
1031 struct glyph_row *ml_row
1032 = (w->current_matrix && w->current_matrix->rows
1033 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1034 : 0);
1035 if (ml_row && ml_row->mode_line_p)
1036 height -= ml_row->height;
1037 else
1038 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1041 if (WINDOW_WANTS_HEADER_LINE_P (w))
1043 struct glyph_row *hl_row
1044 = (w->current_matrix && w->current_matrix->rows
1045 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1046 : 0);
1047 if (hl_row && hl_row->mode_line_p)
1048 height -= hl_row->height;
1049 else
1050 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1053 /* With a very small font and a mode-line that's taller than
1054 default, we might end up with a negative height. */
1055 return max (0, height);
1058 /* Return the window-relative coordinate of the left edge of display
1059 area AREA of window W. AREA < 0 means return the left edge of the
1060 whole window, to the right of the left fringe of W. */
1062 inline int
1063 window_box_left_offset (struct window *w, int area)
1065 int x;
1067 if (w->pseudo_window_p)
1068 return 0;
1070 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1072 if (area == TEXT_AREA)
1073 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1074 + window_box_width (w, LEFT_MARGIN_AREA));
1075 else if (area == RIGHT_MARGIN_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA)
1078 + window_box_width (w, TEXT_AREA)
1079 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1081 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1082 else if (area == LEFT_MARGIN_AREA
1083 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1084 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1086 return x;
1090 /* Return the window-relative coordinate of the right edge of display
1091 area AREA of window W. AREA < 0 means return the right edge of the
1092 whole window, to the left of the right fringe of W. */
1094 inline int
1095 window_box_right_offset (struct window *w, int area)
1097 return window_box_left_offset (w, area) + window_box_width (w, area);
1100 /* Return the frame-relative coordinate of the left edge of display
1101 area AREA of window W. AREA < 0 means return the left edge of the
1102 whole window, to the right of the left fringe of W. */
1104 inline int
1105 window_box_left (struct window *w, int area)
1107 struct frame *f = XFRAME (w->frame);
1108 int x;
1110 if (w->pseudo_window_p)
1111 return FRAME_INTERNAL_BORDER_WIDTH (f);
1113 x = (WINDOW_LEFT_EDGE_X (w)
1114 + window_box_left_offset (w, area));
1116 return x;
1120 /* Return the frame-relative coordinate of the right edge of display
1121 area AREA of window W. AREA < 0 means return the right edge of the
1122 whole window, to the left of the right fringe of W. */
1124 inline int
1125 window_box_right (struct window *w, int area)
1127 return window_box_left (w, area) + window_box_width (w, area);
1130 /* Get the bounding box of the display area AREA of window W, without
1131 mode lines, in frame-relative coordinates. AREA < 0 means the
1132 whole window, not including the left and right fringes of
1133 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1134 coordinates of the upper-left corner of the box. Return in
1135 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1137 inline void
1138 window_box (struct window *w, int area, int *box_x, int *box_y,
1139 int *box_width, int *box_height)
1141 if (box_width)
1142 *box_width = window_box_width (w, area);
1143 if (box_height)
1144 *box_height = window_box_height (w);
1145 if (box_x)
1146 *box_x = window_box_left (w, area);
1147 if (box_y)
1149 *box_y = WINDOW_TOP_EDGE_Y (w);
1150 if (WINDOW_WANTS_HEADER_LINE_P (w))
1151 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1156 /* Get the bounding box of the display area AREA of window W, without
1157 mode lines. AREA < 0 means the whole window, not including the
1158 left and right fringe of the window. Return in *TOP_LEFT_X
1159 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1160 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1161 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1162 box. */
1164 static inline void
1165 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1166 int *bottom_right_x, int *bottom_right_y)
1168 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1169 bottom_right_y);
1170 *bottom_right_x += *top_left_x;
1171 *bottom_right_y += *top_left_y;
1176 /***********************************************************************
1177 Utilities
1178 ***********************************************************************/
1180 /* Return the bottom y-position of the line the iterator IT is in.
1181 This can modify IT's settings. */
1184 line_bottom_y (struct it *it)
1186 int line_height = it->max_ascent + it->max_descent;
1187 int line_top_y = it->current_y;
1189 if (line_height == 0)
1191 if (last_height)
1192 line_height = last_height;
1193 else if (IT_CHARPOS (*it) < ZV)
1195 move_it_by_lines (it, 1);
1196 line_height = (it->max_ascent || it->max_descent
1197 ? it->max_ascent + it->max_descent
1198 : last_height);
1200 else
1202 struct glyph_row *row = it->glyph_row;
1204 /* Use the default character height. */
1205 it->glyph_row = NULL;
1206 it->what = IT_CHARACTER;
1207 it->c = ' ';
1208 it->len = 1;
1209 PRODUCE_GLYPHS (it);
1210 line_height = it->ascent + it->descent;
1211 it->glyph_row = row;
1215 return line_top_y + line_height;
1219 /* Return 1 if position CHARPOS is visible in window W.
1220 CHARPOS < 0 means return info about WINDOW_END position.
1221 If visible, set *X and *Y to pixel coordinates of top left corner.
1222 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1223 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1226 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1227 int *rtop, int *rbot, int *rowh, int *vpos)
1229 struct it it;
1230 void *itdata = bidi_shelve_cache ();
1231 struct text_pos top;
1232 int visible_p = 0;
1233 struct buffer *old_buffer = NULL;
1235 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1236 return visible_p;
1238 if (XBUFFER (w->buffer) != current_buffer)
1240 old_buffer = current_buffer;
1241 set_buffer_internal_1 (XBUFFER (w->buffer));
1244 SET_TEXT_POS_FROM_MARKER (top, w->start);
1246 /* Compute exact mode line heights. */
1247 if (WINDOW_WANTS_MODELINE_P (w))
1248 current_mode_line_height
1249 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1250 BVAR (current_buffer, mode_line_format));
1252 if (WINDOW_WANTS_HEADER_LINE_P (w))
1253 current_header_line_height
1254 = display_mode_line (w, HEADER_LINE_FACE_ID,
1255 BVAR (current_buffer, header_line_format));
1257 start_display (&it, w, top);
1258 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1259 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1261 if (charpos >= 0
1262 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1263 && IT_CHARPOS (it) >= charpos)
1264 /* When scanning backwards under bidi iteration, move_it_to
1265 stops at or _before_ CHARPOS, because it stops at or to
1266 the _right_ of the character at CHARPOS. */
1267 || (it.bidi_p && it.bidi_it.scan_dir == -1
1268 && IT_CHARPOS (it) <= charpos)))
1270 /* We have reached CHARPOS, or passed it. How the call to
1271 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1272 or covered by a display property, move_it_to stops at the end
1273 of the invisible text, to the right of CHARPOS. (ii) If
1274 CHARPOS is in a display vector, move_it_to stops on its last
1275 glyph. */
1276 int top_x = it.current_x;
1277 int top_y = it.current_y;
1278 enum it_method it_method = it.method;
1279 /* Calling line_bottom_y may change it.method, it.position, etc. */
1280 int bottom_y = (last_height = 0, line_bottom_y (&it));
1281 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1283 if (top_y < window_top_y)
1284 visible_p = bottom_y > window_top_y;
1285 else if (top_y < it.last_visible_y)
1286 visible_p = 1;
1287 if (visible_p)
1289 if (it_method == GET_FROM_DISPLAY_VECTOR)
1291 /* We stopped on the last glyph of a display vector.
1292 Try and recompute. Hack alert! */
1293 if (charpos < 2 || top.charpos >= charpos)
1294 top_x = it.glyph_row->x;
1295 else
1297 struct it it2;
1298 start_display (&it2, w, top);
1299 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1300 get_next_display_element (&it2);
1301 PRODUCE_GLYPHS (&it2);
1302 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1303 || it2.current_x > it2.last_visible_x)
1304 top_x = it.glyph_row->x;
1305 else
1307 top_x = it2.current_x;
1308 top_y = it2.current_y;
1313 *x = top_x;
1314 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1315 *rtop = max (0, window_top_y - top_y);
1316 *rbot = max (0, bottom_y - it.last_visible_y);
1317 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1318 - max (top_y, window_top_y)));
1319 *vpos = it.vpos;
1322 else
1324 /* We were asked to provide info about WINDOW_END. */
1325 struct it it2;
1326 void *it2data = NULL;
1328 SAVE_IT (it2, it, it2data);
1329 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1330 move_it_by_lines (&it, 1);
1331 if (charpos < IT_CHARPOS (it)
1332 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1334 visible_p = 1;
1335 RESTORE_IT (&it2, &it2, it2data);
1336 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1337 *x = it2.current_x;
1338 *y = it2.current_y + it2.max_ascent - it2.ascent;
1339 *rtop = max (0, -it2.current_y);
1340 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1341 - it.last_visible_y));
1342 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1343 it.last_visible_y)
1344 - max (it2.current_y,
1345 WINDOW_HEADER_LINE_HEIGHT (w))));
1346 *vpos = it2.vpos;
1348 else
1349 bidi_unshelve_cache (it2data, 1);
1351 bidi_unshelve_cache (itdata, 0);
1353 if (old_buffer)
1354 set_buffer_internal_1 (old_buffer);
1356 current_header_line_height = current_mode_line_height = -1;
1358 if (visible_p && XFASTINT (w->hscroll) > 0)
1359 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1361 #if 0
1362 /* Debugging code. */
1363 if (visible_p)
1364 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1365 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1366 else
1367 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1368 #endif
1370 return visible_p;
1374 /* Return the next character from STR. Return in *LEN the length of
1375 the character. This is like STRING_CHAR_AND_LENGTH but never
1376 returns an invalid character. If we find one, we return a `?', but
1377 with the length of the invalid character. */
1379 static inline int
1380 string_char_and_length (const unsigned char *str, int *len)
1382 int c;
1384 c = STRING_CHAR_AND_LENGTH (str, *len);
1385 if (!CHAR_VALID_P (c))
1386 /* We may not change the length here because other places in Emacs
1387 don't use this function, i.e. they silently accept invalid
1388 characters. */
1389 c = '?';
1391 return c;
1396 /* Given a position POS containing a valid character and byte position
1397 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1399 static struct text_pos
1400 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1402 xassert (STRINGP (string) && nchars >= 0);
1404 if (STRING_MULTIBYTE (string))
1406 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1407 int len;
1409 while (nchars--)
1411 string_char_and_length (p, &len);
1412 p += len;
1413 CHARPOS (pos) += 1;
1414 BYTEPOS (pos) += len;
1417 else
1418 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1420 return pos;
1424 /* Value is the text position, i.e. character and byte position,
1425 for character position CHARPOS in STRING. */
1427 static inline struct text_pos
1428 string_pos (EMACS_INT charpos, Lisp_Object string)
1430 struct text_pos pos;
1431 xassert (STRINGP (string));
1432 xassert (charpos >= 0);
1433 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1434 return pos;
1438 /* Value is a text position, i.e. character and byte position, for
1439 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1440 means recognize multibyte characters. */
1442 static struct text_pos
1443 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1445 struct text_pos pos;
1447 xassert (s != NULL);
1448 xassert (charpos >= 0);
1450 if (multibyte_p)
1452 int len;
1454 SET_TEXT_POS (pos, 0, 0);
1455 while (charpos--)
1457 string_char_and_length ((const unsigned char *) s, &len);
1458 s += len;
1459 CHARPOS (pos) += 1;
1460 BYTEPOS (pos) += len;
1463 else
1464 SET_TEXT_POS (pos, charpos, charpos);
1466 return pos;
1470 /* Value is the number of characters in C string S. MULTIBYTE_P
1471 non-zero means recognize multibyte characters. */
1473 static EMACS_INT
1474 number_of_chars (const char *s, int multibyte_p)
1476 EMACS_INT nchars;
1478 if (multibyte_p)
1480 EMACS_INT rest = strlen (s);
1481 int len;
1482 const unsigned char *p = (const unsigned char *) s;
1484 for (nchars = 0; rest > 0; ++nchars)
1486 string_char_and_length (p, &len);
1487 rest -= len, p += len;
1490 else
1491 nchars = strlen (s);
1493 return nchars;
1497 /* Compute byte position NEWPOS->bytepos corresponding to
1498 NEWPOS->charpos. POS is a known position in string STRING.
1499 NEWPOS->charpos must be >= POS.charpos. */
1501 static void
1502 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1504 xassert (STRINGP (string));
1505 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1507 if (STRING_MULTIBYTE (string))
1508 *newpos = string_pos_nchars_ahead (pos, string,
1509 CHARPOS (*newpos) - CHARPOS (pos));
1510 else
1511 BYTEPOS (*newpos) = CHARPOS (*newpos);
1514 /* EXPORT:
1515 Return an estimation of the pixel height of mode or header lines on
1516 frame F. FACE_ID specifies what line's height to estimate. */
1519 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1521 #ifdef HAVE_WINDOW_SYSTEM
1522 if (FRAME_WINDOW_P (f))
1524 int height = FONT_HEIGHT (FRAME_FONT (f));
1526 /* This function is called so early when Emacs starts that the face
1527 cache and mode line face are not yet initialized. */
1528 if (FRAME_FACE_CACHE (f))
1530 struct face *face = FACE_FROM_ID (f, face_id);
1531 if (face)
1533 if (face->font)
1534 height = FONT_HEIGHT (face->font);
1535 if (face->box_line_width > 0)
1536 height += 2 * face->box_line_width;
1540 return height;
1542 #endif
1544 return 1;
1547 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1548 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1549 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1550 not force the value into range. */
1552 void
1553 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1554 int *x, int *y, NativeRectangle *bounds, int noclip)
1557 #ifdef HAVE_WINDOW_SYSTEM
1558 if (FRAME_WINDOW_P (f))
1560 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1561 even for negative values. */
1562 if (pix_x < 0)
1563 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1564 if (pix_y < 0)
1565 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1567 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1568 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1570 if (bounds)
1571 STORE_NATIVE_RECT (*bounds,
1572 FRAME_COL_TO_PIXEL_X (f, pix_x),
1573 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1574 FRAME_COLUMN_WIDTH (f) - 1,
1575 FRAME_LINE_HEIGHT (f) - 1);
1577 if (!noclip)
1579 if (pix_x < 0)
1580 pix_x = 0;
1581 else if (pix_x > FRAME_TOTAL_COLS (f))
1582 pix_x = FRAME_TOTAL_COLS (f);
1584 if (pix_y < 0)
1585 pix_y = 0;
1586 else if (pix_y > FRAME_LINES (f))
1587 pix_y = FRAME_LINES (f);
1590 #endif
1592 *x = pix_x;
1593 *y = pix_y;
1597 /* Find the glyph under window-relative coordinates X/Y in window W.
1598 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1599 strings. Return in *HPOS and *VPOS the row and column number of
1600 the glyph found. Return in *AREA the glyph area containing X.
1601 Value is a pointer to the glyph found or null if X/Y is not on
1602 text, or we can't tell because W's current matrix is not up to
1603 date. */
1605 static
1606 struct glyph *
1607 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1608 int *dx, int *dy, int *area)
1610 struct glyph *glyph, *end;
1611 struct glyph_row *row = NULL;
1612 int x0, i;
1614 /* Find row containing Y. Give up if some row is not enabled. */
1615 for (i = 0; i < w->current_matrix->nrows; ++i)
1617 row = MATRIX_ROW (w->current_matrix, i);
1618 if (!row->enabled_p)
1619 return NULL;
1620 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1621 break;
1624 *vpos = i;
1625 *hpos = 0;
1627 /* Give up if Y is not in the window. */
1628 if (i == w->current_matrix->nrows)
1629 return NULL;
1631 /* Get the glyph area containing X. */
1632 if (w->pseudo_window_p)
1634 *area = TEXT_AREA;
1635 x0 = 0;
1637 else
1639 if (x < window_box_left_offset (w, TEXT_AREA))
1641 *area = LEFT_MARGIN_AREA;
1642 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1644 else if (x < window_box_right_offset (w, TEXT_AREA))
1646 *area = TEXT_AREA;
1647 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1649 else
1651 *area = RIGHT_MARGIN_AREA;
1652 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1656 /* Find glyph containing X. */
1657 glyph = row->glyphs[*area];
1658 end = glyph + row->used[*area];
1659 x -= x0;
1660 while (glyph < end && x >= glyph->pixel_width)
1662 x -= glyph->pixel_width;
1663 ++glyph;
1666 if (glyph == end)
1667 return NULL;
1669 if (dx)
1671 *dx = x;
1672 *dy = y - (row->y + row->ascent - glyph->ascent);
1675 *hpos = glyph - row->glyphs[*area];
1676 return glyph;
1679 /* Convert frame-relative x/y to coordinates relative to window W.
1680 Takes pseudo-windows into account. */
1682 static void
1683 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1685 if (w->pseudo_window_p)
1687 /* A pseudo-window is always full-width, and starts at the
1688 left edge of the frame, plus a frame border. */
1689 struct frame *f = XFRAME (w->frame);
1690 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1691 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1693 else
1695 *x -= WINDOW_LEFT_EDGE_X (w);
1696 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1700 #ifdef HAVE_WINDOW_SYSTEM
1702 /* EXPORT:
1703 Return in RECTS[] at most N clipping rectangles for glyph string S.
1704 Return the number of stored rectangles. */
1707 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1709 XRectangle r;
1711 if (n <= 0)
1712 return 0;
1714 if (s->row->full_width_p)
1716 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1717 r.x = WINDOW_LEFT_EDGE_X (s->w);
1718 r.width = WINDOW_TOTAL_WIDTH (s->w);
1720 /* Unless displaying a mode or menu bar line, which are always
1721 fully visible, clip to the visible part of the row. */
1722 if (s->w->pseudo_window_p)
1723 r.height = s->row->visible_height;
1724 else
1725 r.height = s->height;
1727 else
1729 /* This is a text line that may be partially visible. */
1730 r.x = window_box_left (s->w, s->area);
1731 r.width = window_box_width (s->w, s->area);
1732 r.height = s->row->visible_height;
1735 if (s->clip_head)
1736 if (r.x < s->clip_head->x)
1738 if (r.width >= s->clip_head->x - r.x)
1739 r.width -= s->clip_head->x - r.x;
1740 else
1741 r.width = 0;
1742 r.x = s->clip_head->x;
1744 if (s->clip_tail)
1745 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1747 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1748 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1749 else
1750 r.width = 0;
1753 /* If S draws overlapping rows, it's sufficient to use the top and
1754 bottom of the window for clipping because this glyph string
1755 intentionally draws over other lines. */
1756 if (s->for_overlaps)
1758 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1759 r.height = window_text_bottom_y (s->w) - r.y;
1761 /* Alas, the above simple strategy does not work for the
1762 environments with anti-aliased text: if the same text is
1763 drawn onto the same place multiple times, it gets thicker.
1764 If the overlap we are processing is for the erased cursor, we
1765 take the intersection with the rectagle of the cursor. */
1766 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1768 XRectangle rc, r_save = r;
1770 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1771 rc.y = s->w->phys_cursor.y;
1772 rc.width = s->w->phys_cursor_width;
1773 rc.height = s->w->phys_cursor_height;
1775 x_intersect_rectangles (&r_save, &rc, &r);
1778 else
1780 /* Don't use S->y for clipping because it doesn't take partially
1781 visible lines into account. For example, it can be negative for
1782 partially visible lines at the top of a window. */
1783 if (!s->row->full_width_p
1784 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1785 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1786 else
1787 r.y = max (0, s->row->y);
1790 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1792 /* If drawing the cursor, don't let glyph draw outside its
1793 advertised boundaries. Cleartype does this under some circumstances. */
1794 if (s->hl == DRAW_CURSOR)
1796 struct glyph *glyph = s->first_glyph;
1797 int height, max_y;
1799 if (s->x > r.x)
1801 r.width -= s->x - r.x;
1802 r.x = s->x;
1804 r.width = min (r.width, glyph->pixel_width);
1806 /* If r.y is below window bottom, ensure that we still see a cursor. */
1807 height = min (glyph->ascent + glyph->descent,
1808 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1809 max_y = window_text_bottom_y (s->w) - height;
1810 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1811 if (s->ybase - glyph->ascent > max_y)
1813 r.y = max_y;
1814 r.height = height;
1816 else
1818 /* Don't draw cursor glyph taller than our actual glyph. */
1819 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1820 if (height < r.height)
1822 max_y = r.y + r.height;
1823 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1824 r.height = min (max_y - r.y, height);
1829 if (s->row->clip)
1831 XRectangle r_save = r;
1833 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1834 r.width = 0;
1837 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1838 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1840 #ifdef CONVERT_FROM_XRECT
1841 CONVERT_FROM_XRECT (r, *rects);
1842 #else
1843 *rects = r;
1844 #endif
1845 return 1;
1847 else
1849 /* If we are processing overlapping and allowed to return
1850 multiple clipping rectangles, we exclude the row of the glyph
1851 string from the clipping rectangle. This is to avoid drawing
1852 the same text on the environment with anti-aliasing. */
1853 #ifdef CONVERT_FROM_XRECT
1854 XRectangle rs[2];
1855 #else
1856 XRectangle *rs = rects;
1857 #endif
1858 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1860 if (s->for_overlaps & OVERLAPS_PRED)
1862 rs[i] = r;
1863 if (r.y + r.height > row_y)
1865 if (r.y < row_y)
1866 rs[i].height = row_y - r.y;
1867 else
1868 rs[i].height = 0;
1870 i++;
1872 if (s->for_overlaps & OVERLAPS_SUCC)
1874 rs[i] = r;
1875 if (r.y < row_y + s->row->visible_height)
1877 if (r.y + r.height > row_y + s->row->visible_height)
1879 rs[i].y = row_y + s->row->visible_height;
1880 rs[i].height = r.y + r.height - rs[i].y;
1882 else
1883 rs[i].height = 0;
1885 i++;
1888 n = i;
1889 #ifdef CONVERT_FROM_XRECT
1890 for (i = 0; i < n; i++)
1891 CONVERT_FROM_XRECT (rs[i], rects[i]);
1892 #endif
1893 return n;
1897 /* EXPORT:
1898 Return in *NR the clipping rectangle for glyph string S. */
1900 void
1901 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1903 get_glyph_string_clip_rects (s, nr, 1);
1907 /* EXPORT:
1908 Return the position and height of the phys cursor in window W.
1909 Set w->phys_cursor_width to width of phys cursor.
1912 void
1913 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1914 struct glyph *glyph, int *xp, int *yp, int *heightp)
1916 struct frame *f = XFRAME (WINDOW_FRAME (w));
1917 int x, y, wd, h, h0, y0;
1919 /* Compute the width of the rectangle to draw. If on a stretch
1920 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1921 rectangle as wide as the glyph, but use a canonical character
1922 width instead. */
1923 wd = glyph->pixel_width - 1;
1924 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1925 wd++; /* Why? */
1926 #endif
1928 x = w->phys_cursor.x;
1929 if (x < 0)
1931 wd += x;
1932 x = 0;
1935 if (glyph->type == STRETCH_GLYPH
1936 && !x_stretch_cursor_p)
1937 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1938 w->phys_cursor_width = wd;
1940 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1942 /* If y is below window bottom, ensure that we still see a cursor. */
1943 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1945 h = max (h0, glyph->ascent + glyph->descent);
1946 h0 = min (h0, glyph->ascent + glyph->descent);
1948 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1949 if (y < y0)
1951 h = max (h - (y0 - y) + 1, h0);
1952 y = y0 - 1;
1954 else
1956 y0 = window_text_bottom_y (w) - h0;
1957 if (y > y0)
1959 h += y - y0;
1960 y = y0;
1964 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1965 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1966 *heightp = h;
1970 * Remember which glyph the mouse is over.
1973 void
1974 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1976 Lisp_Object window;
1977 struct window *w;
1978 struct glyph_row *r, *gr, *end_row;
1979 enum window_part part;
1980 enum glyph_row_area area;
1981 int x, y, width, height;
1983 /* Try to determine frame pixel position and size of the glyph under
1984 frame pixel coordinates X/Y on frame F. */
1986 if (!f->glyphs_initialized_p
1987 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1988 NILP (window)))
1990 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1991 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1992 goto virtual_glyph;
1995 w = XWINDOW (window);
1996 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1997 height = WINDOW_FRAME_LINE_HEIGHT (w);
1999 x = window_relative_x_coord (w, part, gx);
2000 y = gy - WINDOW_TOP_EDGE_Y (w);
2002 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2003 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2005 if (w->pseudo_window_p)
2007 area = TEXT_AREA;
2008 part = ON_MODE_LINE; /* Don't adjust margin. */
2009 goto text_glyph;
2012 switch (part)
2014 case ON_LEFT_MARGIN:
2015 area = LEFT_MARGIN_AREA;
2016 goto text_glyph;
2018 case ON_RIGHT_MARGIN:
2019 area = RIGHT_MARGIN_AREA;
2020 goto text_glyph;
2022 case ON_HEADER_LINE:
2023 case ON_MODE_LINE:
2024 gr = (part == ON_HEADER_LINE
2025 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2026 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2027 gy = gr->y;
2028 area = TEXT_AREA;
2029 goto text_glyph_row_found;
2031 case ON_TEXT:
2032 area = TEXT_AREA;
2034 text_glyph:
2035 gr = 0; gy = 0;
2036 for (; r <= end_row && r->enabled_p; ++r)
2037 if (r->y + r->height > y)
2039 gr = r; gy = r->y;
2040 break;
2043 text_glyph_row_found:
2044 if (gr && gy <= y)
2046 struct glyph *g = gr->glyphs[area];
2047 struct glyph *end = g + gr->used[area];
2049 height = gr->height;
2050 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2051 if (gx + g->pixel_width > x)
2052 break;
2054 if (g < end)
2056 if (g->type == IMAGE_GLYPH)
2058 /* Don't remember when mouse is over image, as
2059 image may have hot-spots. */
2060 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2061 return;
2063 width = g->pixel_width;
2065 else
2067 /* Use nominal char spacing at end of line. */
2068 x -= gx;
2069 gx += (x / width) * width;
2072 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2073 gx += window_box_left_offset (w, area);
2075 else
2077 /* Use nominal line height at end of window. */
2078 gx = (x / width) * width;
2079 y -= gy;
2080 gy += (y / height) * height;
2082 break;
2084 case ON_LEFT_FRINGE:
2085 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2086 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2087 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2088 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2089 goto row_glyph;
2091 case ON_RIGHT_FRINGE:
2092 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2093 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2094 : window_box_right_offset (w, TEXT_AREA));
2095 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2096 goto row_glyph;
2098 case ON_SCROLL_BAR:
2099 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2101 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2102 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2103 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2104 : 0)));
2105 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2107 row_glyph:
2108 gr = 0, gy = 0;
2109 for (; r <= end_row && r->enabled_p; ++r)
2110 if (r->y + r->height > y)
2112 gr = r; gy = r->y;
2113 break;
2116 if (gr && gy <= y)
2117 height = gr->height;
2118 else
2120 /* Use nominal line height at end of window. */
2121 y -= gy;
2122 gy += (y / height) * height;
2124 break;
2126 default:
2128 virtual_glyph:
2129 /* If there is no glyph under the mouse, then we divide the screen
2130 into a grid of the smallest glyph in the frame, and use that
2131 as our "glyph". */
2133 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2134 round down even for negative values. */
2135 if (gx < 0)
2136 gx -= width - 1;
2137 if (gy < 0)
2138 gy -= height - 1;
2140 gx = (gx / width) * width;
2141 gy = (gy / height) * height;
2143 goto store_rect;
2146 gx += WINDOW_LEFT_EDGE_X (w);
2147 gy += WINDOW_TOP_EDGE_Y (w);
2149 store_rect:
2150 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2152 /* Visible feedback for debugging. */
2153 #if 0
2154 #if HAVE_X_WINDOWS
2155 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2156 f->output_data.x->normal_gc,
2157 gx, gy, width, height);
2158 #endif
2159 #endif
2163 #endif /* HAVE_WINDOW_SYSTEM */
2166 /***********************************************************************
2167 Lisp form evaluation
2168 ***********************************************************************/
2170 /* Error handler for safe_eval and safe_call. */
2172 static Lisp_Object
2173 safe_eval_handler (Lisp_Object arg)
2175 add_to_log ("Error during redisplay: %S", arg, Qnil);
2176 return Qnil;
2180 /* Evaluate SEXPR and return the result, or nil if something went
2181 wrong. Prevent redisplay during the evaluation. */
2183 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2184 Return the result, or nil if something went wrong. Prevent
2185 redisplay during the evaluation. */
2187 Lisp_Object
2188 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2190 Lisp_Object val;
2192 if (inhibit_eval_during_redisplay)
2193 val = Qnil;
2194 else
2196 int count = SPECPDL_INDEX ();
2197 struct gcpro gcpro1;
2199 GCPRO1 (args[0]);
2200 gcpro1.nvars = nargs;
2201 specbind (Qinhibit_redisplay, Qt);
2202 /* Use Qt to ensure debugger does not run,
2203 so there is no possibility of wanting to redisplay. */
2204 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2205 safe_eval_handler);
2206 UNGCPRO;
2207 val = unbind_to (count, val);
2210 return val;
2214 /* Call function FN with one argument ARG.
2215 Return the result, or nil if something went wrong. */
2217 Lisp_Object
2218 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2220 Lisp_Object args[2];
2221 args[0] = fn;
2222 args[1] = arg;
2223 return safe_call (2, args);
2226 static Lisp_Object Qeval;
2228 Lisp_Object
2229 safe_eval (Lisp_Object sexpr)
2231 return safe_call1 (Qeval, sexpr);
2234 /* Call function FN with one argument ARG.
2235 Return the result, or nil if something went wrong. */
2237 Lisp_Object
2238 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2240 Lisp_Object args[3];
2241 args[0] = fn;
2242 args[1] = arg1;
2243 args[2] = arg2;
2244 return safe_call (3, args);
2249 /***********************************************************************
2250 Debugging
2251 ***********************************************************************/
2253 #if 0
2255 /* Define CHECK_IT to perform sanity checks on iterators.
2256 This is for debugging. It is too slow to do unconditionally. */
2258 static void
2259 check_it (struct it *it)
2261 if (it->method == GET_FROM_STRING)
2263 xassert (STRINGP (it->string));
2264 xassert (IT_STRING_CHARPOS (*it) >= 0);
2266 else
2268 xassert (IT_STRING_CHARPOS (*it) < 0);
2269 if (it->method == GET_FROM_BUFFER)
2271 /* Check that character and byte positions agree. */
2272 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2276 if (it->dpvec)
2277 xassert (it->current.dpvec_index >= 0);
2278 else
2279 xassert (it->current.dpvec_index < 0);
2282 #define CHECK_IT(IT) check_it ((IT))
2284 #else /* not 0 */
2286 #define CHECK_IT(IT) (void) 0
2288 #endif /* not 0 */
2291 #if GLYPH_DEBUG && XASSERTS
2293 /* Check that the window end of window W is what we expect it
2294 to be---the last row in the current matrix displaying text. */
2296 static void
2297 check_window_end (struct window *w)
2299 if (!MINI_WINDOW_P (w)
2300 && !NILP (w->window_end_valid))
2302 struct glyph_row *row;
2303 xassert ((row = MATRIX_ROW (w->current_matrix,
2304 XFASTINT (w->window_end_vpos)),
2305 !row->enabled_p
2306 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2307 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2311 #define CHECK_WINDOW_END(W) check_window_end ((W))
2313 #else
2315 #define CHECK_WINDOW_END(W) (void) 0
2317 #endif
2321 /***********************************************************************
2322 Iterator initialization
2323 ***********************************************************************/
2325 /* Initialize IT for displaying current_buffer in window W, starting
2326 at character position CHARPOS. CHARPOS < 0 means that no buffer
2327 position is specified which is useful when the iterator is assigned
2328 a position later. BYTEPOS is the byte position corresponding to
2329 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2331 If ROW is not null, calls to produce_glyphs with IT as parameter
2332 will produce glyphs in that row.
2334 BASE_FACE_ID is the id of a base face to use. It must be one of
2335 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2336 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2337 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2339 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2340 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2341 will be initialized to use the corresponding mode line glyph row of
2342 the desired matrix of W. */
2344 void
2345 init_iterator (struct it *it, struct window *w,
2346 EMACS_INT charpos, EMACS_INT bytepos,
2347 struct glyph_row *row, enum face_id base_face_id)
2349 int highlight_region_p;
2350 enum face_id remapped_base_face_id = base_face_id;
2352 /* Some precondition checks. */
2353 xassert (w != NULL && it != NULL);
2354 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2355 && charpos <= ZV));
2357 /* If face attributes have been changed since the last redisplay,
2358 free realized faces now because they depend on face definitions
2359 that might have changed. Don't free faces while there might be
2360 desired matrices pending which reference these faces. */
2361 if (face_change_count && !inhibit_free_realized_faces)
2363 face_change_count = 0;
2364 free_all_realized_faces (Qnil);
2367 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2368 if (! NILP (Vface_remapping_alist))
2369 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2371 /* Use one of the mode line rows of W's desired matrix if
2372 appropriate. */
2373 if (row == NULL)
2375 if (base_face_id == MODE_LINE_FACE_ID
2376 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2377 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2378 else if (base_face_id == HEADER_LINE_FACE_ID)
2379 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2382 /* Clear IT. */
2383 memset (it, 0, sizeof *it);
2384 it->current.overlay_string_index = -1;
2385 it->current.dpvec_index = -1;
2386 it->base_face_id = remapped_base_face_id;
2387 it->string = Qnil;
2388 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2389 it->paragraph_embedding = L2R;
2390 it->bidi_it.string.lstring = Qnil;
2391 it->bidi_it.string.s = NULL;
2392 it->bidi_it.string.bufpos = 0;
2394 /* The window in which we iterate over current_buffer: */
2395 XSETWINDOW (it->window, w);
2396 it->w = w;
2397 it->f = XFRAME (w->frame);
2399 it->cmp_it.id = -1;
2401 /* Extra space between lines (on window systems only). */
2402 if (base_face_id == DEFAULT_FACE_ID
2403 && FRAME_WINDOW_P (it->f))
2405 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2406 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2407 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2408 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2409 * FRAME_LINE_HEIGHT (it->f));
2410 else if (it->f->extra_line_spacing > 0)
2411 it->extra_line_spacing = it->f->extra_line_spacing;
2412 it->max_extra_line_spacing = 0;
2415 /* If realized faces have been removed, e.g. because of face
2416 attribute changes of named faces, recompute them. When running
2417 in batch mode, the face cache of the initial frame is null. If
2418 we happen to get called, make a dummy face cache. */
2419 if (FRAME_FACE_CACHE (it->f) == NULL)
2420 init_frame_faces (it->f);
2421 if (FRAME_FACE_CACHE (it->f)->used == 0)
2422 recompute_basic_faces (it->f);
2424 /* Current value of the `slice', `space-width', and 'height' properties. */
2425 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2426 it->space_width = Qnil;
2427 it->font_height = Qnil;
2428 it->override_ascent = -1;
2430 /* Are control characters displayed as `^C'? */
2431 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2433 /* -1 means everything between a CR and the following line end
2434 is invisible. >0 means lines indented more than this value are
2435 invisible. */
2436 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2437 ? XINT (BVAR (current_buffer, selective_display))
2438 : (!NILP (BVAR (current_buffer, selective_display))
2439 ? -1 : 0));
2440 it->selective_display_ellipsis_p
2441 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2443 /* Display table to use. */
2444 it->dp = window_display_table (w);
2446 /* Are multibyte characters enabled in current_buffer? */
2447 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2449 /* Non-zero if we should highlight the region. */
2450 highlight_region_p
2451 = (!NILP (Vtransient_mark_mode)
2452 && !NILP (BVAR (current_buffer, mark_active))
2453 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2455 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2456 start and end of a visible region in window IT->w. Set both to
2457 -1 to indicate no region. */
2458 if (highlight_region_p
2459 /* Maybe highlight only in selected window. */
2460 && (/* Either show region everywhere. */
2461 highlight_nonselected_windows
2462 /* Or show region in the selected window. */
2463 || w == XWINDOW (selected_window)
2464 /* Or show the region if we are in the mini-buffer and W is
2465 the window the mini-buffer refers to. */
2466 || (MINI_WINDOW_P (XWINDOW (selected_window))
2467 && WINDOWP (minibuf_selected_window)
2468 && w == XWINDOW (minibuf_selected_window))))
2470 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2471 it->region_beg_charpos = min (PT, markpos);
2472 it->region_end_charpos = max (PT, markpos);
2474 else
2475 it->region_beg_charpos = it->region_end_charpos = -1;
2477 /* Get the position at which the redisplay_end_trigger hook should
2478 be run, if it is to be run at all. */
2479 if (MARKERP (w->redisplay_end_trigger)
2480 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2481 it->redisplay_end_trigger_charpos
2482 = marker_position (w->redisplay_end_trigger);
2483 else if (INTEGERP (w->redisplay_end_trigger))
2484 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2486 /* Correct bogus values of tab_width. */
2487 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2488 if (it->tab_width <= 0 || it->tab_width > 1000)
2489 it->tab_width = 8;
2491 /* Are lines in the display truncated? */
2492 if (base_face_id != DEFAULT_FACE_ID
2493 || XINT (it->w->hscroll)
2494 || (! WINDOW_FULL_WIDTH_P (it->w)
2495 && ((!NILP (Vtruncate_partial_width_windows)
2496 && !INTEGERP (Vtruncate_partial_width_windows))
2497 || (INTEGERP (Vtruncate_partial_width_windows)
2498 && (WINDOW_TOTAL_COLS (it->w)
2499 < XINT (Vtruncate_partial_width_windows))))))
2500 it->line_wrap = TRUNCATE;
2501 else if (NILP (BVAR (current_buffer, truncate_lines)))
2502 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2503 ? WINDOW_WRAP : WORD_WRAP;
2504 else
2505 it->line_wrap = TRUNCATE;
2507 /* Get dimensions of truncation and continuation glyphs. These are
2508 displayed as fringe bitmaps under X, so we don't need them for such
2509 frames. */
2510 if (!FRAME_WINDOW_P (it->f))
2512 if (it->line_wrap == TRUNCATE)
2514 /* We will need the truncation glyph. */
2515 xassert (it->glyph_row == NULL);
2516 produce_special_glyphs (it, IT_TRUNCATION);
2517 it->truncation_pixel_width = it->pixel_width;
2519 else
2521 /* We will need the continuation glyph. */
2522 xassert (it->glyph_row == NULL);
2523 produce_special_glyphs (it, IT_CONTINUATION);
2524 it->continuation_pixel_width = it->pixel_width;
2527 /* Reset these values to zero because the produce_special_glyphs
2528 above has changed them. */
2529 it->pixel_width = it->ascent = it->descent = 0;
2530 it->phys_ascent = it->phys_descent = 0;
2533 /* Set this after getting the dimensions of truncation and
2534 continuation glyphs, so that we don't produce glyphs when calling
2535 produce_special_glyphs, above. */
2536 it->glyph_row = row;
2537 it->area = TEXT_AREA;
2539 /* Forget any previous info about this row being reversed. */
2540 if (it->glyph_row)
2541 it->glyph_row->reversed_p = 0;
2543 /* Get the dimensions of the display area. The display area
2544 consists of the visible window area plus a horizontally scrolled
2545 part to the left of the window. All x-values are relative to the
2546 start of this total display area. */
2547 if (base_face_id != DEFAULT_FACE_ID)
2549 /* Mode lines, menu bar in terminal frames. */
2550 it->first_visible_x = 0;
2551 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2553 else
2555 it->first_visible_x
2556 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2557 it->last_visible_x = (it->first_visible_x
2558 + window_box_width (w, TEXT_AREA));
2560 /* If we truncate lines, leave room for the truncator glyph(s) at
2561 the right margin. Otherwise, leave room for the continuation
2562 glyph(s). Truncation and continuation glyphs are not inserted
2563 for window-based redisplay. */
2564 if (!FRAME_WINDOW_P (it->f))
2566 if (it->line_wrap == TRUNCATE)
2567 it->last_visible_x -= it->truncation_pixel_width;
2568 else
2569 it->last_visible_x -= it->continuation_pixel_width;
2572 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2573 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2576 /* Leave room for a border glyph. */
2577 if (!FRAME_WINDOW_P (it->f)
2578 && !WINDOW_RIGHTMOST_P (it->w))
2579 it->last_visible_x -= 1;
2581 it->last_visible_y = window_text_bottom_y (w);
2583 /* For mode lines and alike, arrange for the first glyph having a
2584 left box line if the face specifies a box. */
2585 if (base_face_id != DEFAULT_FACE_ID)
2587 struct face *face;
2589 it->face_id = remapped_base_face_id;
2591 /* If we have a boxed mode line, make the first character appear
2592 with a left box line. */
2593 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2594 if (face->box != FACE_NO_BOX)
2595 it->start_of_box_run_p = 1;
2598 /* If a buffer position was specified, set the iterator there,
2599 getting overlays and face properties from that position. */
2600 if (charpos >= BUF_BEG (current_buffer))
2602 it->end_charpos = ZV;
2603 it->face_id = -1;
2604 IT_CHARPOS (*it) = charpos;
2606 /* Compute byte position if not specified. */
2607 if (bytepos < charpos)
2608 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2609 else
2610 IT_BYTEPOS (*it) = bytepos;
2612 it->start = it->current;
2613 /* Do we need to reorder bidirectional text? Not if this is a
2614 unibyte buffer: by definition, none of the single-byte
2615 characters are strong R2L, so no reordering is needed. And
2616 bidi.c doesn't support unibyte buffers anyway. */
2617 it->bidi_p =
2618 !NILP (BVAR (current_buffer, bidi_display_reordering))
2619 && it->multibyte_p;
2621 /* If we are to reorder bidirectional text, init the bidi
2622 iterator. */
2623 if (it->bidi_p)
2625 /* Note the paragraph direction that this buffer wants to
2626 use. */
2627 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2628 Qleft_to_right))
2629 it->paragraph_embedding = L2R;
2630 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2631 Qright_to_left))
2632 it->paragraph_embedding = R2L;
2633 else
2634 it->paragraph_embedding = NEUTRAL_DIR;
2635 bidi_unshelve_cache (NULL, 0);
2636 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2637 &it->bidi_it);
2640 /* Compute faces etc. */
2641 reseat (it, it->current.pos, 1);
2644 CHECK_IT (it);
2648 /* Initialize IT for the display of window W with window start POS. */
2650 void
2651 start_display (struct it *it, struct window *w, struct text_pos pos)
2653 struct glyph_row *row;
2654 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2656 row = w->desired_matrix->rows + first_vpos;
2657 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2658 it->first_vpos = first_vpos;
2660 /* Don't reseat to previous visible line start if current start
2661 position is in a string or image. */
2662 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2664 int start_at_line_beg_p;
2665 int first_y = it->current_y;
2667 /* If window start is not at a line start, skip forward to POS to
2668 get the correct continuation lines width. */
2669 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2670 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2671 if (!start_at_line_beg_p)
2673 int new_x;
2675 reseat_at_previous_visible_line_start (it);
2676 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2678 new_x = it->current_x + it->pixel_width;
2680 /* If lines are continued, this line may end in the middle
2681 of a multi-glyph character (e.g. a control character
2682 displayed as \003, or in the middle of an overlay
2683 string). In this case move_it_to above will not have
2684 taken us to the start of the continuation line but to the
2685 end of the continued line. */
2686 if (it->current_x > 0
2687 && it->line_wrap != TRUNCATE /* Lines are continued. */
2688 && (/* And glyph doesn't fit on the line. */
2689 new_x > it->last_visible_x
2690 /* Or it fits exactly and we're on a window
2691 system frame. */
2692 || (new_x == it->last_visible_x
2693 && FRAME_WINDOW_P (it->f))))
2695 if (it->current.dpvec_index >= 0
2696 || it->current.overlay_string_index >= 0)
2698 set_iterator_to_next (it, 1);
2699 move_it_in_display_line_to (it, -1, -1, 0);
2702 it->continuation_lines_width += it->current_x;
2705 /* We're starting a new display line, not affected by the
2706 height of the continued line, so clear the appropriate
2707 fields in the iterator structure. */
2708 it->max_ascent = it->max_descent = 0;
2709 it->max_phys_ascent = it->max_phys_descent = 0;
2711 it->current_y = first_y;
2712 it->vpos = 0;
2713 it->current_x = it->hpos = 0;
2719 /* Return 1 if POS is a position in ellipses displayed for invisible
2720 text. W is the window we display, for text property lookup. */
2722 static int
2723 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2725 Lisp_Object prop, window;
2726 int ellipses_p = 0;
2727 EMACS_INT charpos = CHARPOS (pos->pos);
2729 /* If POS specifies a position in a display vector, this might
2730 be for an ellipsis displayed for invisible text. We won't
2731 get the iterator set up for delivering that ellipsis unless
2732 we make sure that it gets aware of the invisible text. */
2733 if (pos->dpvec_index >= 0
2734 && pos->overlay_string_index < 0
2735 && CHARPOS (pos->string_pos) < 0
2736 && charpos > BEGV
2737 && (XSETWINDOW (window, w),
2738 prop = Fget_char_property (make_number (charpos),
2739 Qinvisible, window),
2740 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2742 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2743 window);
2744 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2747 return ellipses_p;
2751 /* Initialize IT for stepping through current_buffer in window W,
2752 starting at position POS that includes overlay string and display
2753 vector/ control character translation position information. Value
2754 is zero if there are overlay strings with newlines at POS. */
2756 static int
2757 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2759 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2760 int i, overlay_strings_with_newlines = 0;
2762 /* If POS specifies a position in a display vector, this might
2763 be for an ellipsis displayed for invisible text. We won't
2764 get the iterator set up for delivering that ellipsis unless
2765 we make sure that it gets aware of the invisible text. */
2766 if (in_ellipses_for_invisible_text_p (pos, w))
2768 --charpos;
2769 bytepos = 0;
2772 /* Keep in mind: the call to reseat in init_iterator skips invisible
2773 text, so we might end up at a position different from POS. This
2774 is only a problem when POS is a row start after a newline and an
2775 overlay starts there with an after-string, and the overlay has an
2776 invisible property. Since we don't skip invisible text in
2777 display_line and elsewhere immediately after consuming the
2778 newline before the row start, such a POS will not be in a string,
2779 but the call to init_iterator below will move us to the
2780 after-string. */
2781 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2783 /* This only scans the current chunk -- it should scan all chunks.
2784 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2785 to 16 in 22.1 to make this a lesser problem. */
2786 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2788 const char *s = SSDATA (it->overlay_strings[i]);
2789 const char *e = s + SBYTES (it->overlay_strings[i]);
2791 while (s < e && *s != '\n')
2792 ++s;
2794 if (s < e)
2796 overlay_strings_with_newlines = 1;
2797 break;
2801 /* If position is within an overlay string, set up IT to the right
2802 overlay string. */
2803 if (pos->overlay_string_index >= 0)
2805 int relative_index;
2807 /* If the first overlay string happens to have a `display'
2808 property for an image, the iterator will be set up for that
2809 image, and we have to undo that setup first before we can
2810 correct the overlay string index. */
2811 if (it->method == GET_FROM_IMAGE)
2812 pop_it (it);
2814 /* We already have the first chunk of overlay strings in
2815 IT->overlay_strings. Load more until the one for
2816 pos->overlay_string_index is in IT->overlay_strings. */
2817 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2819 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2820 it->current.overlay_string_index = 0;
2821 while (n--)
2823 load_overlay_strings (it, 0);
2824 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2828 it->current.overlay_string_index = pos->overlay_string_index;
2829 relative_index = (it->current.overlay_string_index
2830 % OVERLAY_STRING_CHUNK_SIZE);
2831 it->string = it->overlay_strings[relative_index];
2832 xassert (STRINGP (it->string));
2833 it->current.string_pos = pos->string_pos;
2834 it->method = GET_FROM_STRING;
2837 if (CHARPOS (pos->string_pos) >= 0)
2839 /* Recorded position is not in an overlay string, but in another
2840 string. This can only be a string from a `display' property.
2841 IT should already be filled with that string. */
2842 it->current.string_pos = pos->string_pos;
2843 xassert (STRINGP (it->string));
2846 /* Restore position in display vector translations, control
2847 character translations or ellipses. */
2848 if (pos->dpvec_index >= 0)
2850 if (it->dpvec == NULL)
2851 get_next_display_element (it);
2852 xassert (it->dpvec && it->current.dpvec_index == 0);
2853 it->current.dpvec_index = pos->dpvec_index;
2856 CHECK_IT (it);
2857 return !overlay_strings_with_newlines;
2861 /* Initialize IT for stepping through current_buffer in window W
2862 starting at ROW->start. */
2864 static void
2865 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2867 init_from_display_pos (it, w, &row->start);
2868 it->start = row->start;
2869 it->continuation_lines_width = row->continuation_lines_width;
2870 CHECK_IT (it);
2874 /* Initialize IT for stepping through current_buffer in window W
2875 starting in the line following ROW, i.e. starting at ROW->end.
2876 Value is zero if there are overlay strings with newlines at ROW's
2877 end position. */
2879 static int
2880 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2882 int success = 0;
2884 if (init_from_display_pos (it, w, &row->end))
2886 if (row->continued_p)
2887 it->continuation_lines_width
2888 = row->continuation_lines_width + row->pixel_width;
2889 CHECK_IT (it);
2890 success = 1;
2893 return success;
2899 /***********************************************************************
2900 Text properties
2901 ***********************************************************************/
2903 /* Called when IT reaches IT->stop_charpos. Handle text property and
2904 overlay changes. Set IT->stop_charpos to the next position where
2905 to stop. */
2907 static void
2908 handle_stop (struct it *it)
2910 enum prop_handled handled;
2911 int handle_overlay_change_p;
2912 struct props *p;
2914 it->dpvec = NULL;
2915 it->current.dpvec_index = -1;
2916 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2917 it->ignore_overlay_strings_at_pos_p = 0;
2918 it->ellipsis_p = 0;
2920 /* Use face of preceding text for ellipsis (if invisible) */
2921 if (it->selective_display_ellipsis_p)
2922 it->saved_face_id = it->face_id;
2926 handled = HANDLED_NORMALLY;
2928 /* Call text property handlers. */
2929 for (p = it_props; p->handler; ++p)
2931 handled = p->handler (it);
2933 if (handled == HANDLED_RECOMPUTE_PROPS)
2934 break;
2935 else if (handled == HANDLED_RETURN)
2937 /* We still want to show before and after strings from
2938 overlays even if the actual buffer text is replaced. */
2939 if (!handle_overlay_change_p
2940 || it->sp > 1
2941 || !get_overlay_strings_1 (it, 0, 0))
2943 if (it->ellipsis_p)
2944 setup_for_ellipsis (it, 0);
2945 /* When handling a display spec, we might load an
2946 empty string. In that case, discard it here. We
2947 used to discard it in handle_single_display_spec,
2948 but that causes get_overlay_strings_1, above, to
2949 ignore overlay strings that we must check. */
2950 if (STRINGP (it->string) && !SCHARS (it->string))
2951 pop_it (it);
2952 return;
2954 else if (STRINGP (it->string) && !SCHARS (it->string))
2955 pop_it (it);
2956 else
2958 it->ignore_overlay_strings_at_pos_p = 1;
2959 it->string_from_display_prop_p = 0;
2960 it->from_disp_prop_p = 0;
2961 handle_overlay_change_p = 0;
2963 handled = HANDLED_RECOMPUTE_PROPS;
2964 break;
2966 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2967 handle_overlay_change_p = 0;
2970 if (handled != HANDLED_RECOMPUTE_PROPS)
2972 /* Don't check for overlay strings below when set to deliver
2973 characters from a display vector. */
2974 if (it->method == GET_FROM_DISPLAY_VECTOR)
2975 handle_overlay_change_p = 0;
2977 /* Handle overlay changes.
2978 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2979 if it finds overlays. */
2980 if (handle_overlay_change_p)
2981 handled = handle_overlay_change (it);
2984 if (it->ellipsis_p)
2986 setup_for_ellipsis (it, 0);
2987 break;
2990 while (handled == HANDLED_RECOMPUTE_PROPS);
2992 /* Determine where to stop next. */
2993 if (handled == HANDLED_NORMALLY)
2994 compute_stop_pos (it);
2998 /* Compute IT->stop_charpos from text property and overlay change
2999 information for IT's current position. */
3001 static void
3002 compute_stop_pos (struct it *it)
3004 register INTERVAL iv, next_iv;
3005 Lisp_Object object, limit, position;
3006 EMACS_INT charpos, bytepos;
3008 /* If nowhere else, stop at the end. */
3009 it->stop_charpos = it->end_charpos;
3011 if (STRINGP (it->string))
3013 /* Strings are usually short, so don't limit the search for
3014 properties. */
3015 object = it->string;
3016 limit = Qnil;
3017 charpos = IT_STRING_CHARPOS (*it);
3018 bytepos = IT_STRING_BYTEPOS (*it);
3020 else
3022 EMACS_INT pos;
3024 /* If next overlay change is in front of the current stop pos
3025 (which is IT->end_charpos), stop there. Note: value of
3026 next_overlay_change is point-max if no overlay change
3027 follows. */
3028 charpos = IT_CHARPOS (*it);
3029 bytepos = IT_BYTEPOS (*it);
3030 pos = next_overlay_change (charpos);
3031 if (pos < it->stop_charpos)
3032 it->stop_charpos = pos;
3034 /* If showing the region, we have to stop at the region
3035 start or end because the face might change there. */
3036 if (it->region_beg_charpos > 0)
3038 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3039 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3040 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3041 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3044 /* Set up variables for computing the stop position from text
3045 property changes. */
3046 XSETBUFFER (object, current_buffer);
3047 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3050 /* Get the interval containing IT's position. Value is a null
3051 interval if there isn't such an interval. */
3052 position = make_number (charpos);
3053 iv = validate_interval_range (object, &position, &position, 0);
3054 if (!NULL_INTERVAL_P (iv))
3056 Lisp_Object values_here[LAST_PROP_IDX];
3057 struct props *p;
3059 /* Get properties here. */
3060 for (p = it_props; p->handler; ++p)
3061 values_here[p->idx] = textget (iv->plist, *p->name);
3063 /* Look for an interval following iv that has different
3064 properties. */
3065 for (next_iv = next_interval (iv);
3066 (!NULL_INTERVAL_P (next_iv)
3067 && (NILP (limit)
3068 || XFASTINT (limit) > next_iv->position));
3069 next_iv = next_interval (next_iv))
3071 for (p = it_props; p->handler; ++p)
3073 Lisp_Object new_value;
3075 new_value = textget (next_iv->plist, *p->name);
3076 if (!EQ (values_here[p->idx], new_value))
3077 break;
3080 if (p->handler)
3081 break;
3084 if (!NULL_INTERVAL_P (next_iv))
3086 if (INTEGERP (limit)
3087 && next_iv->position >= XFASTINT (limit))
3088 /* No text property change up to limit. */
3089 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3090 else
3091 /* Text properties change in next_iv. */
3092 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3096 if (it->cmp_it.id < 0)
3098 EMACS_INT stoppos = it->end_charpos;
3100 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3101 stoppos = -1;
3102 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3103 stoppos, it->string);
3106 xassert (STRINGP (it->string)
3107 || (it->stop_charpos >= BEGV
3108 && it->stop_charpos >= IT_CHARPOS (*it)));
3112 /* Return the position of the next overlay change after POS in
3113 current_buffer. Value is point-max if no overlay change
3114 follows. This is like `next-overlay-change' but doesn't use
3115 xmalloc. */
3117 static EMACS_INT
3118 next_overlay_change (EMACS_INT pos)
3120 ptrdiff_t i, noverlays;
3121 EMACS_INT endpos;
3122 Lisp_Object *overlays;
3124 /* Get all overlays at the given position. */
3125 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3127 /* If any of these overlays ends before endpos,
3128 use its ending point instead. */
3129 for (i = 0; i < noverlays; ++i)
3131 Lisp_Object oend;
3132 EMACS_INT oendpos;
3134 oend = OVERLAY_END (overlays[i]);
3135 oendpos = OVERLAY_POSITION (oend);
3136 endpos = min (endpos, oendpos);
3139 return endpos;
3142 /* How many characters forward to search for a display property or
3143 display string. Enough for a screenful of 100 lines x 50
3144 characters in a line. */
3145 #define MAX_DISP_SCAN 5000
3147 /* Return the character position of a display string at or after
3148 position specified by POSITION. If no display string exists at or
3149 after POSITION, return ZV. A display string is either an overlay
3150 with `display' property whose value is a string, or a `display'
3151 text property whose value is a string. STRING is data about the
3152 string to iterate; if STRING->lstring is nil, we are iterating a
3153 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3154 on a GUI frame. */
3155 EMACS_INT
3156 compute_display_string_pos (struct text_pos *position,
3157 struct bidi_string_data *string,
3158 int frame_window_p, int *disp_prop_p)
3160 /* OBJECT = nil means current buffer. */
3161 Lisp_Object object =
3162 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3163 Lisp_Object pos, spec, limpos;
3164 int string_p = (string && (STRINGP (string->lstring) || string->s));
3165 EMACS_INT eob = string_p ? string->schars : ZV;
3166 EMACS_INT begb = string_p ? 0 : BEGV;
3167 EMACS_INT bufpos, charpos = CHARPOS (*position);
3168 EMACS_INT lim =
3169 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3170 struct text_pos tpos;
3172 *disp_prop_p = 1;
3174 if (charpos >= eob
3175 /* We don't support display properties whose values are strings
3176 that have display string properties. */
3177 || string->from_disp_str
3178 /* C strings cannot have display properties. */
3179 || (string->s && !STRINGP (object)))
3181 *disp_prop_p = 0;
3182 return eob;
3185 /* If the character at CHARPOS is where the display string begins,
3186 return CHARPOS. */
3187 pos = make_number (charpos);
3188 if (STRINGP (object))
3189 bufpos = string->bufpos;
3190 else
3191 bufpos = charpos;
3192 tpos = *position;
3193 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3194 && (charpos <= begb
3195 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3196 object),
3197 spec))
3198 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3199 frame_window_p))
3201 return charpos;
3204 /* Look forward for the first character with a `display' property
3205 that will replace the underlying text when displayed. */
3206 limpos = make_number (lim);
3207 do {
3208 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3209 CHARPOS (tpos) = XFASTINT (pos);
3210 if (CHARPOS (tpos) >= lim)
3212 *disp_prop_p = 0;
3213 break;
3215 if (STRINGP (object))
3216 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3217 else
3218 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3219 spec = Fget_char_property (pos, Qdisplay, object);
3220 if (!STRINGP (object))
3221 bufpos = CHARPOS (tpos);
3222 } while (NILP (spec)
3223 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3224 frame_window_p));
3226 return CHARPOS (tpos);
3229 /* Return the character position of the end of the display string that
3230 started at CHARPOS. A display string is either an overlay with
3231 `display' property whose value is a string or a `display' text
3232 property whose value is a string. */
3233 EMACS_INT
3234 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3236 /* OBJECT = nil means current buffer. */
3237 Lisp_Object object =
3238 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3239 Lisp_Object pos = make_number (charpos);
3240 EMACS_INT eob =
3241 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3243 if (charpos >= eob || (string->s && !STRINGP (object)))
3244 return eob;
3246 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3247 abort ();
3249 /* Look forward for the first character where the `display' property
3250 changes. */
3251 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3253 return XFASTINT (pos);
3258 /***********************************************************************
3259 Fontification
3260 ***********************************************************************/
3262 /* Handle changes in the `fontified' property of the current buffer by
3263 calling hook functions from Qfontification_functions to fontify
3264 regions of text. */
3266 static enum prop_handled
3267 handle_fontified_prop (struct it *it)
3269 Lisp_Object prop, pos;
3270 enum prop_handled handled = HANDLED_NORMALLY;
3272 if (!NILP (Vmemory_full))
3273 return handled;
3275 /* Get the value of the `fontified' property at IT's current buffer
3276 position. (The `fontified' property doesn't have a special
3277 meaning in strings.) If the value is nil, call functions from
3278 Qfontification_functions. */
3279 if (!STRINGP (it->string)
3280 && it->s == NULL
3281 && !NILP (Vfontification_functions)
3282 && !NILP (Vrun_hooks)
3283 && (pos = make_number (IT_CHARPOS (*it)),
3284 prop = Fget_char_property (pos, Qfontified, Qnil),
3285 /* Ignore the special cased nil value always present at EOB since
3286 no amount of fontifying will be able to change it. */
3287 NILP (prop) && IT_CHARPOS (*it) < Z))
3289 int count = SPECPDL_INDEX ();
3290 Lisp_Object val;
3291 struct buffer *obuf = current_buffer;
3292 int begv = BEGV, zv = ZV;
3293 int old_clip_changed = current_buffer->clip_changed;
3295 val = Vfontification_functions;
3296 specbind (Qfontification_functions, Qnil);
3298 xassert (it->end_charpos == ZV);
3300 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3301 safe_call1 (val, pos);
3302 else
3304 Lisp_Object fns, fn;
3305 struct gcpro gcpro1, gcpro2;
3307 fns = Qnil;
3308 GCPRO2 (val, fns);
3310 for (; CONSP (val); val = XCDR (val))
3312 fn = XCAR (val);
3314 if (EQ (fn, Qt))
3316 /* A value of t indicates this hook has a local
3317 binding; it means to run the global binding too.
3318 In a global value, t should not occur. If it
3319 does, we must ignore it to avoid an endless
3320 loop. */
3321 for (fns = Fdefault_value (Qfontification_functions);
3322 CONSP (fns);
3323 fns = XCDR (fns))
3325 fn = XCAR (fns);
3326 if (!EQ (fn, Qt))
3327 safe_call1 (fn, pos);
3330 else
3331 safe_call1 (fn, pos);
3334 UNGCPRO;
3337 unbind_to (count, Qnil);
3339 /* Fontification functions routinely call `save-restriction'.
3340 Normally, this tags clip_changed, which can confuse redisplay
3341 (see discussion in Bug#6671). Since we don't perform any
3342 special handling of fontification changes in the case where
3343 `save-restriction' isn't called, there's no point doing so in
3344 this case either. So, if the buffer's restrictions are
3345 actually left unchanged, reset clip_changed. */
3346 if (obuf == current_buffer)
3348 if (begv == BEGV && zv == ZV)
3349 current_buffer->clip_changed = old_clip_changed;
3351 /* There isn't much we can reasonably do to protect against
3352 misbehaving fontification, but here's a fig leaf. */
3353 else if (!NILP (BVAR (obuf, name)))
3354 set_buffer_internal_1 (obuf);
3356 /* The fontification code may have added/removed text.
3357 It could do even a lot worse, but let's at least protect against
3358 the most obvious case where only the text past `pos' gets changed',
3359 as is/was done in grep.el where some escapes sequences are turned
3360 into face properties (bug#7876). */
3361 it->end_charpos = ZV;
3363 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3364 something. This avoids an endless loop if they failed to
3365 fontify the text for which reason ever. */
3366 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3367 handled = HANDLED_RECOMPUTE_PROPS;
3370 return handled;
3375 /***********************************************************************
3376 Faces
3377 ***********************************************************************/
3379 /* Set up iterator IT from face properties at its current position.
3380 Called from handle_stop. */
3382 static enum prop_handled
3383 handle_face_prop (struct it *it)
3385 int new_face_id;
3386 EMACS_INT next_stop;
3388 if (!STRINGP (it->string))
3390 new_face_id
3391 = face_at_buffer_position (it->w,
3392 IT_CHARPOS (*it),
3393 it->region_beg_charpos,
3394 it->region_end_charpos,
3395 &next_stop,
3396 (IT_CHARPOS (*it)
3397 + TEXT_PROP_DISTANCE_LIMIT),
3398 0, it->base_face_id);
3400 /* Is this a start of a run of characters with box face?
3401 Caveat: this can be called for a freshly initialized
3402 iterator; face_id is -1 in this case. We know that the new
3403 face will not change until limit, i.e. if the new face has a
3404 box, all characters up to limit will have one. But, as
3405 usual, we don't know whether limit is really the end. */
3406 if (new_face_id != it->face_id)
3408 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3410 /* If new face has a box but old face has not, this is
3411 the start of a run of characters with box, i.e. it has
3412 a shadow on the left side. The value of face_id of the
3413 iterator will be -1 if this is the initial call that gets
3414 the face. In this case, we have to look in front of IT's
3415 position and see whether there is a face != new_face_id. */
3416 it->start_of_box_run_p
3417 = (new_face->box != FACE_NO_BOX
3418 && (it->face_id >= 0
3419 || IT_CHARPOS (*it) == BEG
3420 || new_face_id != face_before_it_pos (it)));
3421 it->face_box_p = new_face->box != FACE_NO_BOX;
3424 else
3426 int base_face_id;
3427 EMACS_INT bufpos;
3428 int i;
3429 Lisp_Object from_overlay
3430 = (it->current.overlay_string_index >= 0
3431 ? it->string_overlays[it->current.overlay_string_index]
3432 : Qnil);
3434 /* See if we got to this string directly or indirectly from
3435 an overlay property. That includes the before-string or
3436 after-string of an overlay, strings in display properties
3437 provided by an overlay, their text properties, etc.
3439 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3440 if (! NILP (from_overlay))
3441 for (i = it->sp - 1; i >= 0; i--)
3443 if (it->stack[i].current.overlay_string_index >= 0)
3444 from_overlay
3445 = it->string_overlays[it->stack[i].current.overlay_string_index];
3446 else if (! NILP (it->stack[i].from_overlay))
3447 from_overlay = it->stack[i].from_overlay;
3449 if (!NILP (from_overlay))
3450 break;
3453 if (! NILP (from_overlay))
3455 bufpos = IT_CHARPOS (*it);
3456 /* For a string from an overlay, the base face depends
3457 only on text properties and ignores overlays. */
3458 base_face_id
3459 = face_for_overlay_string (it->w,
3460 IT_CHARPOS (*it),
3461 it->region_beg_charpos,
3462 it->region_end_charpos,
3463 &next_stop,
3464 (IT_CHARPOS (*it)
3465 + TEXT_PROP_DISTANCE_LIMIT),
3467 from_overlay);
3469 else
3471 bufpos = 0;
3473 /* For strings from a `display' property, use the face at
3474 IT's current buffer position as the base face to merge
3475 with, so that overlay strings appear in the same face as
3476 surrounding text, unless they specify their own
3477 faces. */
3478 base_face_id = underlying_face_id (it);
3481 new_face_id = face_at_string_position (it->w,
3482 it->string,
3483 IT_STRING_CHARPOS (*it),
3484 bufpos,
3485 it->region_beg_charpos,
3486 it->region_end_charpos,
3487 &next_stop,
3488 base_face_id, 0);
3490 /* Is this a start of a run of characters with box? Caveat:
3491 this can be called for a freshly allocated iterator; face_id
3492 is -1 is this case. We know that the new face will not
3493 change until the next check pos, i.e. if the new face has a
3494 box, all characters up to that position will have a
3495 box. But, as usual, we don't know whether that position
3496 is really the end. */
3497 if (new_face_id != it->face_id)
3499 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3500 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3502 /* If new face has a box but old face hasn't, this is the
3503 start of a run of characters with box, i.e. it has a
3504 shadow on the left side. */
3505 it->start_of_box_run_p
3506 = new_face->box && (old_face == NULL || !old_face->box);
3507 it->face_box_p = new_face->box != FACE_NO_BOX;
3511 it->face_id = new_face_id;
3512 return HANDLED_NORMALLY;
3516 /* Return the ID of the face ``underlying'' IT's current position,
3517 which is in a string. If the iterator is associated with a
3518 buffer, return the face at IT's current buffer position.
3519 Otherwise, use the iterator's base_face_id. */
3521 static int
3522 underlying_face_id (struct it *it)
3524 int face_id = it->base_face_id, i;
3526 xassert (STRINGP (it->string));
3528 for (i = it->sp - 1; i >= 0; --i)
3529 if (NILP (it->stack[i].string))
3530 face_id = it->stack[i].face_id;
3532 return face_id;
3536 /* Compute the face one character before or after the current position
3537 of IT, in the visual order. BEFORE_P non-zero means get the face
3538 in front (to the left in L2R paragraphs, to the right in R2L
3539 paragraphs) of IT's screen position. Value is the ID of the face. */
3541 static int
3542 face_before_or_after_it_pos (struct it *it, int before_p)
3544 int face_id, limit;
3545 EMACS_INT next_check_charpos;
3546 struct it it_copy;
3547 void *it_copy_data = NULL;
3549 xassert (it->s == NULL);
3551 if (STRINGP (it->string))
3553 EMACS_INT bufpos, charpos;
3554 int base_face_id;
3556 /* No face change past the end of the string (for the case
3557 we are padding with spaces). No face change before the
3558 string start. */
3559 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3560 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3561 return it->face_id;
3563 if (!it->bidi_p)
3565 /* Set charpos to the position before or after IT's current
3566 position, in the logical order, which in the non-bidi
3567 case is the same as the visual order. */
3568 if (before_p)
3569 charpos = IT_STRING_CHARPOS (*it) - 1;
3570 else if (it->what == IT_COMPOSITION)
3571 /* For composition, we must check the character after the
3572 composition. */
3573 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3574 else
3575 charpos = IT_STRING_CHARPOS (*it) + 1;
3577 else
3579 if (before_p)
3581 /* With bidi iteration, the character before the current
3582 in the visual order cannot be found by simple
3583 iteration, because "reverse" reordering is not
3584 supported. Instead, we need to use the move_it_*
3585 family of functions. */
3586 /* Ignore face changes before the first visible
3587 character on this display line. */
3588 if (it->current_x <= it->first_visible_x)
3589 return it->face_id;
3590 SAVE_IT (it_copy, *it, it_copy_data);
3591 /* Implementation note: Since move_it_in_display_line
3592 works in the iterator geometry, and thinks the first
3593 character is always the leftmost, even in R2L lines,
3594 we don't need to distinguish between the R2L and L2R
3595 cases here. */
3596 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3597 it_copy.current_x - 1, MOVE_TO_X);
3598 charpos = IT_STRING_CHARPOS (it_copy);
3599 RESTORE_IT (it, it, it_copy_data);
3601 else
3603 /* Set charpos to the string position of the character
3604 that comes after IT's current position in the visual
3605 order. */
3606 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3608 it_copy = *it;
3609 while (n--)
3610 bidi_move_to_visually_next (&it_copy.bidi_it);
3612 charpos = it_copy.bidi_it.charpos;
3615 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3617 if (it->current.overlay_string_index >= 0)
3618 bufpos = IT_CHARPOS (*it);
3619 else
3620 bufpos = 0;
3622 base_face_id = underlying_face_id (it);
3624 /* Get the face for ASCII, or unibyte. */
3625 face_id = face_at_string_position (it->w,
3626 it->string,
3627 charpos,
3628 bufpos,
3629 it->region_beg_charpos,
3630 it->region_end_charpos,
3631 &next_check_charpos,
3632 base_face_id, 0);
3634 /* Correct the face for charsets different from ASCII. Do it
3635 for the multibyte case only. The face returned above is
3636 suitable for unibyte text if IT->string is unibyte. */
3637 if (STRING_MULTIBYTE (it->string))
3639 struct text_pos pos1 = string_pos (charpos, it->string);
3640 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3641 int c, len;
3642 struct face *face = FACE_FROM_ID (it->f, face_id);
3644 c = string_char_and_length (p, &len);
3645 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3648 else
3650 struct text_pos pos;
3652 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3653 || (IT_CHARPOS (*it) <= BEGV && before_p))
3654 return it->face_id;
3656 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3657 pos = it->current.pos;
3659 if (!it->bidi_p)
3661 if (before_p)
3662 DEC_TEXT_POS (pos, it->multibyte_p);
3663 else
3665 if (it->what == IT_COMPOSITION)
3667 /* For composition, we must check the position after
3668 the composition. */
3669 pos.charpos += it->cmp_it.nchars;
3670 pos.bytepos += it->len;
3672 else
3673 INC_TEXT_POS (pos, it->multibyte_p);
3676 else
3678 if (before_p)
3680 /* With bidi iteration, the character before the current
3681 in the visual order cannot be found by simple
3682 iteration, because "reverse" reordering is not
3683 supported. Instead, we need to use the move_it_*
3684 family of functions. */
3685 /* Ignore face changes before the first visible
3686 character on this display line. */
3687 if (it->current_x <= it->first_visible_x)
3688 return it->face_id;
3689 SAVE_IT (it_copy, *it, it_copy_data);
3690 /* Implementation note: Since move_it_in_display_line
3691 works in the iterator geometry, and thinks the first
3692 character is always the leftmost, even in R2L lines,
3693 we don't need to distinguish between the R2L and L2R
3694 cases here. */
3695 move_it_in_display_line (&it_copy, ZV,
3696 it_copy.current_x - 1, MOVE_TO_X);
3697 pos = it_copy.current.pos;
3698 RESTORE_IT (it, it, it_copy_data);
3700 else
3702 /* Set charpos to the buffer position of the character
3703 that comes after IT's current position in the visual
3704 order. */
3705 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3707 it_copy = *it;
3708 while (n--)
3709 bidi_move_to_visually_next (&it_copy.bidi_it);
3711 SET_TEXT_POS (pos,
3712 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3715 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3717 /* Determine face for CHARSET_ASCII, or unibyte. */
3718 face_id = face_at_buffer_position (it->w,
3719 CHARPOS (pos),
3720 it->region_beg_charpos,
3721 it->region_end_charpos,
3722 &next_check_charpos,
3723 limit, 0, -1);
3725 /* Correct the face for charsets different from ASCII. Do it
3726 for the multibyte case only. The face returned above is
3727 suitable for unibyte text if current_buffer is unibyte. */
3728 if (it->multibyte_p)
3730 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3731 struct face *face = FACE_FROM_ID (it->f, face_id);
3732 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3736 return face_id;
3741 /***********************************************************************
3742 Invisible text
3743 ***********************************************************************/
3745 /* Set up iterator IT from invisible properties at its current
3746 position. Called from handle_stop. */
3748 static enum prop_handled
3749 handle_invisible_prop (struct it *it)
3751 enum prop_handled handled = HANDLED_NORMALLY;
3753 if (STRINGP (it->string))
3755 Lisp_Object prop, end_charpos, limit, charpos;
3757 /* Get the value of the invisible text property at the
3758 current position. Value will be nil if there is no such
3759 property. */
3760 charpos = make_number (IT_STRING_CHARPOS (*it));
3761 prop = Fget_text_property (charpos, Qinvisible, it->string);
3763 if (!NILP (prop)
3764 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3766 EMACS_INT endpos;
3768 handled = HANDLED_RECOMPUTE_PROPS;
3770 /* Get the position at which the next change of the
3771 invisible text property can be found in IT->string.
3772 Value will be nil if the property value is the same for
3773 all the rest of IT->string. */
3774 XSETINT (limit, SCHARS (it->string));
3775 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3776 it->string, limit);
3778 /* Text at current position is invisible. The next
3779 change in the property is at position end_charpos.
3780 Move IT's current position to that position. */
3781 if (INTEGERP (end_charpos)
3782 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3784 struct text_pos old;
3785 EMACS_INT oldpos;
3787 old = it->current.string_pos;
3788 oldpos = CHARPOS (old);
3789 if (it->bidi_p)
3791 if (it->bidi_it.first_elt
3792 && it->bidi_it.charpos < SCHARS (it->string))
3793 bidi_paragraph_init (it->paragraph_embedding,
3794 &it->bidi_it, 1);
3795 /* Bidi-iterate out of the invisible text. */
3798 bidi_move_to_visually_next (&it->bidi_it);
3800 while (oldpos <= it->bidi_it.charpos
3801 && it->bidi_it.charpos < endpos);
3803 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3804 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3805 if (IT_CHARPOS (*it) >= endpos)
3806 it->prev_stop = endpos;
3808 else
3810 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3811 compute_string_pos (&it->current.string_pos, old, it->string);
3814 else
3816 /* The rest of the string is invisible. If this is an
3817 overlay string, proceed with the next overlay string
3818 or whatever comes and return a character from there. */
3819 if (it->current.overlay_string_index >= 0)
3821 next_overlay_string (it);
3822 /* Don't check for overlay strings when we just
3823 finished processing them. */
3824 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3826 else
3828 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3829 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3834 else
3836 int invis_p;
3837 EMACS_INT newpos, next_stop, start_charpos, tem;
3838 Lisp_Object pos, prop, overlay;
3840 /* First of all, is there invisible text at this position? */
3841 tem = start_charpos = IT_CHARPOS (*it);
3842 pos = make_number (tem);
3843 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3844 &overlay);
3845 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3847 /* If we are on invisible text, skip over it. */
3848 if (invis_p && start_charpos < it->end_charpos)
3850 /* Record whether we have to display an ellipsis for the
3851 invisible text. */
3852 int display_ellipsis_p = invis_p == 2;
3854 handled = HANDLED_RECOMPUTE_PROPS;
3856 /* Loop skipping over invisible text. The loop is left at
3857 ZV or with IT on the first char being visible again. */
3860 /* Try to skip some invisible text. Return value is the
3861 position reached which can be equal to where we start
3862 if there is nothing invisible there. This skips both
3863 over invisible text properties and overlays with
3864 invisible property. */
3865 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3867 /* If we skipped nothing at all we weren't at invisible
3868 text in the first place. If everything to the end of
3869 the buffer was skipped, end the loop. */
3870 if (newpos == tem || newpos >= ZV)
3871 invis_p = 0;
3872 else
3874 /* We skipped some characters but not necessarily
3875 all there are. Check if we ended up on visible
3876 text. Fget_char_property returns the property of
3877 the char before the given position, i.e. if we
3878 get invis_p = 0, this means that the char at
3879 newpos is visible. */
3880 pos = make_number (newpos);
3881 prop = Fget_char_property (pos, Qinvisible, it->window);
3882 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3885 /* If we ended up on invisible text, proceed to
3886 skip starting with next_stop. */
3887 if (invis_p)
3888 tem = next_stop;
3890 /* If there are adjacent invisible texts, don't lose the
3891 second one's ellipsis. */
3892 if (invis_p == 2)
3893 display_ellipsis_p = 1;
3895 while (invis_p);
3897 /* The position newpos is now either ZV or on visible text. */
3898 if (it->bidi_p && newpos < ZV)
3900 /* With bidi iteration, the region of invisible text
3901 could start and/or end in the middle of a non-base
3902 embedding level. Therefore, we need to skip
3903 invisible text using the bidi iterator, starting at
3904 IT's current position, until we find ourselves
3905 outside the invisible text. Skipping invisible text
3906 _after_ bidi iteration avoids affecting the visual
3907 order of the displayed text when invisible properties
3908 are added or removed. */
3909 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3911 /* If we were `reseat'ed to a new paragraph,
3912 determine the paragraph base direction. We need
3913 to do it now because next_element_from_buffer may
3914 not have a chance to do it, if we are going to
3915 skip any text at the beginning, which resets the
3916 FIRST_ELT flag. */
3917 bidi_paragraph_init (it->paragraph_embedding,
3918 &it->bidi_it, 1);
3922 bidi_move_to_visually_next (&it->bidi_it);
3924 while (it->stop_charpos <= it->bidi_it.charpos
3925 && it->bidi_it.charpos < newpos);
3926 IT_CHARPOS (*it) = it->bidi_it.charpos;
3927 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3928 /* If we overstepped NEWPOS, record its position in the
3929 iterator, so that we skip invisible text if later the
3930 bidi iteration lands us in the invisible region
3931 again. */
3932 if (IT_CHARPOS (*it) >= newpos)
3933 it->prev_stop = newpos;
3935 else
3937 IT_CHARPOS (*it) = newpos;
3938 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3941 /* If there are before-strings at the start of invisible
3942 text, and the text is invisible because of a text
3943 property, arrange to show before-strings because 20.x did
3944 it that way. (If the text is invisible because of an
3945 overlay property instead of a text property, this is
3946 already handled in the overlay code.) */
3947 if (NILP (overlay)
3948 && get_overlay_strings (it, it->stop_charpos))
3950 handled = HANDLED_RECOMPUTE_PROPS;
3951 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3953 else if (display_ellipsis_p)
3955 /* Make sure that the glyphs of the ellipsis will get
3956 correct `charpos' values. If we would not update
3957 it->position here, the glyphs would belong to the
3958 last visible character _before_ the invisible
3959 text, which confuses `set_cursor_from_row'.
3961 We use the last invisible position instead of the
3962 first because this way the cursor is always drawn on
3963 the first "." of the ellipsis, whenever PT is inside
3964 the invisible text. Otherwise the cursor would be
3965 placed _after_ the ellipsis when the point is after the
3966 first invisible character. */
3967 if (!STRINGP (it->object))
3969 it->position.charpos = newpos - 1;
3970 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3972 it->ellipsis_p = 1;
3973 /* Let the ellipsis display before
3974 considering any properties of the following char.
3975 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3976 handled = HANDLED_RETURN;
3981 return handled;
3985 /* Make iterator IT return `...' next.
3986 Replaces LEN characters from buffer. */
3988 static void
3989 setup_for_ellipsis (struct it *it, int len)
3991 /* Use the display table definition for `...'. Invalid glyphs
3992 will be handled by the method returning elements from dpvec. */
3993 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3995 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3996 it->dpvec = v->contents;
3997 it->dpend = v->contents + v->header.size;
3999 else
4001 /* Default `...'. */
4002 it->dpvec = default_invis_vector;
4003 it->dpend = default_invis_vector + 3;
4006 it->dpvec_char_len = len;
4007 it->current.dpvec_index = 0;
4008 it->dpvec_face_id = -1;
4010 /* Remember the current face id in case glyphs specify faces.
4011 IT's face is restored in set_iterator_to_next.
4012 saved_face_id was set to preceding char's face in handle_stop. */
4013 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4014 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4016 it->method = GET_FROM_DISPLAY_VECTOR;
4017 it->ellipsis_p = 1;
4022 /***********************************************************************
4023 'display' property
4024 ***********************************************************************/
4026 /* Set up iterator IT from `display' property at its current position.
4027 Called from handle_stop.
4028 We return HANDLED_RETURN if some part of the display property
4029 overrides the display of the buffer text itself.
4030 Otherwise we return HANDLED_NORMALLY. */
4032 static enum prop_handled
4033 handle_display_prop (struct it *it)
4035 Lisp_Object propval, object, overlay;
4036 struct text_pos *position;
4037 EMACS_INT bufpos;
4038 /* Nonzero if some property replaces the display of the text itself. */
4039 int display_replaced_p = 0;
4041 if (STRINGP (it->string))
4043 object = it->string;
4044 position = &it->current.string_pos;
4045 bufpos = CHARPOS (it->current.pos);
4047 else
4049 XSETWINDOW (object, it->w);
4050 position = &it->current.pos;
4051 bufpos = CHARPOS (*position);
4054 /* Reset those iterator values set from display property values. */
4055 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4056 it->space_width = Qnil;
4057 it->font_height = Qnil;
4058 it->voffset = 0;
4060 /* We don't support recursive `display' properties, i.e. string
4061 values that have a string `display' property, that have a string
4062 `display' property etc. */
4063 if (!it->string_from_display_prop_p)
4064 it->area = TEXT_AREA;
4066 propval = get_char_property_and_overlay (make_number (position->charpos),
4067 Qdisplay, object, &overlay);
4068 if (NILP (propval))
4069 return HANDLED_NORMALLY;
4070 /* Now OVERLAY is the overlay that gave us this property, or nil
4071 if it was a text property. */
4073 if (!STRINGP (it->string))
4074 object = it->w->buffer;
4076 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4077 position, bufpos,
4078 FRAME_WINDOW_P (it->f));
4080 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4083 /* Subroutine of handle_display_prop. Returns non-zero if the display
4084 specification in SPEC is a replacing specification, i.e. it would
4085 replace the text covered by `display' property with something else,
4086 such as an image or a display string.
4088 See handle_single_display_spec for documentation of arguments.
4089 frame_window_p is non-zero if the window being redisplayed is on a
4090 GUI frame; this argument is used only if IT is NULL, see below.
4092 IT can be NULL, if this is called by the bidi reordering code
4093 through compute_display_string_pos, which see. In that case, this
4094 function only examines SPEC, but does not otherwise "handle" it, in
4095 the sense that it doesn't set up members of IT from the display
4096 spec. */
4097 static int
4098 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4099 Lisp_Object overlay, struct text_pos *position,
4100 EMACS_INT bufpos, int frame_window_p)
4102 int replacing_p = 0;
4104 if (CONSP (spec)
4105 /* Simple specerties. */
4106 && !EQ (XCAR (spec), Qimage)
4107 #ifdef HAVE_XWIDGETS
4108 && !EQ (XCAR (spec), Qxwidget)
4109 #endif
4110 && !EQ (XCAR (spec), Qspace)
4111 && !EQ (XCAR (spec), Qwhen)
4112 && !EQ (XCAR (spec), Qslice)
4113 && !EQ (XCAR (spec), Qspace_width)
4114 && !EQ (XCAR (spec), Qheight)
4115 && !EQ (XCAR (spec), Qraise)
4116 /* Marginal area specifications. */
4117 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4118 && !EQ (XCAR (spec), Qleft_fringe)
4119 && !EQ (XCAR (spec), Qright_fringe)
4120 && !NILP (XCAR (spec)))
4122 for (; CONSP (spec); spec = XCDR (spec))
4124 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4125 position, bufpos, replacing_p,
4126 frame_window_p))
4128 replacing_p = 1;
4129 /* If some text in a string is replaced, `position' no
4130 longer points to the position of `object'. */
4131 if (!it || STRINGP (object))
4132 break;
4136 else if (VECTORP (spec))
4138 int i;
4139 for (i = 0; i < ASIZE (spec); ++i)
4140 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4141 position, bufpos, replacing_p,
4142 frame_window_p))
4144 replacing_p = 1;
4145 /* If some text in a string is replaced, `position' no
4146 longer points to the position of `object'. */
4147 if (!it || STRINGP (object))
4148 break;
4151 else
4153 if (handle_single_display_spec (it, spec, object, overlay,
4154 position, bufpos, 0, frame_window_p))
4155 replacing_p = 1;
4158 return replacing_p;
4161 /* Value is the position of the end of the `display' property starting
4162 at START_POS in OBJECT. */
4164 static struct text_pos
4165 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4167 Lisp_Object end;
4168 struct text_pos end_pos;
4170 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4171 Qdisplay, object, Qnil);
4172 CHARPOS (end_pos) = XFASTINT (end);
4173 if (STRINGP (object))
4174 compute_string_pos (&end_pos, start_pos, it->string);
4175 else
4176 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4178 return end_pos;
4182 /* Set up IT from a single `display' property specification SPEC. OBJECT
4183 is the object in which the `display' property was found. *POSITION
4184 is the position in OBJECT at which the `display' property was found.
4185 BUFPOS is the buffer position of OBJECT (different from POSITION if
4186 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4187 previously saw a display specification which already replaced text
4188 display with something else, for example an image; we ignore such
4189 properties after the first one has been processed.
4191 OVERLAY is the overlay this `display' property came from,
4192 or nil if it was a text property.
4194 If SPEC is a `space' or `image' specification, and in some other
4195 cases too, set *POSITION to the position where the `display'
4196 property ends.
4198 If IT is NULL, only examine the property specification in SPEC, but
4199 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4200 is intended to be displayed in a window on a GUI frame.
4202 Value is non-zero if something was found which replaces the display
4203 of buffer or string text. */
4205 static int
4206 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4207 Lisp_Object overlay, struct text_pos *position,
4208 EMACS_INT bufpos, int display_replaced_p,
4209 int frame_window_p)
4211 Lisp_Object form;
4212 Lisp_Object location, value;
4213 struct text_pos start_pos = *position;
4214 int valid_p;
4216 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4217 If the result is non-nil, use VALUE instead of SPEC. */
4218 form = Qt;
4219 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4221 spec = XCDR (spec);
4222 if (!CONSP (spec))
4223 return 0;
4224 form = XCAR (spec);
4225 spec = XCDR (spec);
4228 if (!NILP (form) && !EQ (form, Qt))
4230 int count = SPECPDL_INDEX ();
4231 struct gcpro gcpro1;
4233 /* Bind `object' to the object having the `display' property, a
4234 buffer or string. Bind `position' to the position in the
4235 object where the property was found, and `buffer-position'
4236 to the current position in the buffer. */
4238 if (NILP (object))
4239 XSETBUFFER (object, current_buffer);
4240 specbind (Qobject, object);
4241 specbind (Qposition, make_number (CHARPOS (*position)));
4242 specbind (Qbuffer_position, make_number (bufpos));
4243 GCPRO1 (form);
4244 form = safe_eval (form);
4245 UNGCPRO;
4246 unbind_to (count, Qnil);
4249 if (NILP (form))
4250 return 0;
4252 /* Handle `(height HEIGHT)' specifications. */
4253 if (CONSP (spec)
4254 && EQ (XCAR (spec), Qheight)
4255 && CONSP (XCDR (spec)))
4257 if (it)
4259 if (!FRAME_WINDOW_P (it->f))
4260 return 0;
4262 it->font_height = XCAR (XCDR (spec));
4263 if (!NILP (it->font_height))
4265 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4266 int new_height = -1;
4268 if (CONSP (it->font_height)
4269 && (EQ (XCAR (it->font_height), Qplus)
4270 || EQ (XCAR (it->font_height), Qminus))
4271 && CONSP (XCDR (it->font_height))
4272 && INTEGERP (XCAR (XCDR (it->font_height))))
4274 /* `(+ N)' or `(- N)' where N is an integer. */
4275 int steps = XINT (XCAR (XCDR (it->font_height)));
4276 if (EQ (XCAR (it->font_height), Qplus))
4277 steps = - steps;
4278 it->face_id = smaller_face (it->f, it->face_id, steps);
4280 else if (FUNCTIONP (it->font_height))
4282 /* Call function with current height as argument.
4283 Value is the new height. */
4284 Lisp_Object height;
4285 height = safe_call1 (it->font_height,
4286 face->lface[LFACE_HEIGHT_INDEX]);
4287 if (NUMBERP (height))
4288 new_height = XFLOATINT (height);
4290 else if (NUMBERP (it->font_height))
4292 /* Value is a multiple of the canonical char height. */
4293 struct face *f;
4295 f = FACE_FROM_ID (it->f,
4296 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4297 new_height = (XFLOATINT (it->font_height)
4298 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4300 else
4302 /* Evaluate IT->font_height with `height' bound to the
4303 current specified height to get the new height. */
4304 int count = SPECPDL_INDEX ();
4306 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4307 value = safe_eval (it->font_height);
4308 unbind_to (count, Qnil);
4310 if (NUMBERP (value))
4311 new_height = XFLOATINT (value);
4314 if (new_height > 0)
4315 it->face_id = face_with_height (it->f, it->face_id, new_height);
4319 return 0;
4322 /* Handle `(space-width WIDTH)'. */
4323 if (CONSP (spec)
4324 && EQ (XCAR (spec), Qspace_width)
4325 && CONSP (XCDR (spec)))
4327 if (it)
4329 if (!FRAME_WINDOW_P (it->f))
4330 return 0;
4332 value = XCAR (XCDR (spec));
4333 if (NUMBERP (value) && XFLOATINT (value) > 0)
4334 it->space_width = value;
4337 return 0;
4340 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4341 if (CONSP (spec)
4342 && EQ (XCAR (spec), Qslice))
4344 Lisp_Object tem;
4346 if (it)
4348 if (!FRAME_WINDOW_P (it->f))
4349 return 0;
4351 if (tem = XCDR (spec), CONSP (tem))
4353 it->slice.x = XCAR (tem);
4354 if (tem = XCDR (tem), CONSP (tem))
4356 it->slice.y = XCAR (tem);
4357 if (tem = XCDR (tem), CONSP (tem))
4359 it->slice.width = XCAR (tem);
4360 if (tem = XCDR (tem), CONSP (tem))
4361 it->slice.height = XCAR (tem);
4367 return 0;
4370 /* Handle `(raise FACTOR)'. */
4371 if (CONSP (spec)
4372 && EQ (XCAR (spec), Qraise)
4373 && CONSP (XCDR (spec)))
4375 if (it)
4377 if (!FRAME_WINDOW_P (it->f))
4378 return 0;
4380 #ifdef HAVE_WINDOW_SYSTEM
4381 value = XCAR (XCDR (spec));
4382 if (NUMBERP (value))
4384 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4385 it->voffset = - (XFLOATINT (value)
4386 * (FONT_HEIGHT (face->font)));
4388 #endif /* HAVE_WINDOW_SYSTEM */
4391 return 0;
4394 /* Don't handle the other kinds of display specifications
4395 inside a string that we got from a `display' property. */
4396 if (it && it->string_from_display_prop_p)
4397 return 0;
4399 /* Characters having this form of property are not displayed, so
4400 we have to find the end of the property. */
4401 if (it)
4403 start_pos = *position;
4404 *position = display_prop_end (it, object, start_pos);
4406 value = Qnil;
4408 /* Stop the scan at that end position--we assume that all
4409 text properties change there. */
4410 if (it)
4411 it->stop_charpos = position->charpos;
4413 /* Handle `(left-fringe BITMAP [FACE])'
4414 and `(right-fringe BITMAP [FACE])'. */
4415 if (CONSP (spec)
4416 && (EQ (XCAR (spec), Qleft_fringe)
4417 || EQ (XCAR (spec), Qright_fringe))
4418 && CONSP (XCDR (spec)))
4420 int fringe_bitmap;
4422 if (it)
4424 if (!FRAME_WINDOW_P (it->f))
4425 /* If we return here, POSITION has been advanced
4426 across the text with this property. */
4427 return 0;
4429 else if (!frame_window_p)
4430 return 0;
4432 #ifdef HAVE_WINDOW_SYSTEM
4433 value = XCAR (XCDR (spec));
4434 if (!SYMBOLP (value)
4435 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4436 /* If we return here, POSITION has been advanced
4437 across the text with this property. */
4438 return 0;
4440 if (it)
4442 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4444 if (CONSP (XCDR (XCDR (spec))))
4446 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4447 int face_id2 = lookup_derived_face (it->f, face_name,
4448 FRINGE_FACE_ID, 0);
4449 if (face_id2 >= 0)
4450 face_id = face_id2;
4453 /* Save current settings of IT so that we can restore them
4454 when we are finished with the glyph property value. */
4455 push_it (it, position);
4457 it->area = TEXT_AREA;
4458 it->what = IT_IMAGE;
4459 it->image_id = -1; /* no image */
4460 it->position = start_pos;
4461 it->object = NILP (object) ? it->w->buffer : object;
4462 it->method = GET_FROM_IMAGE;
4463 it->from_overlay = Qnil;
4464 it->face_id = face_id;
4465 it->from_disp_prop_p = 1;
4467 /* Say that we haven't consumed the characters with
4468 `display' property yet. The call to pop_it in
4469 set_iterator_to_next will clean this up. */
4470 *position = start_pos;
4472 if (EQ (XCAR (spec), Qleft_fringe))
4474 it->left_user_fringe_bitmap = fringe_bitmap;
4475 it->left_user_fringe_face_id = face_id;
4477 else
4479 it->right_user_fringe_bitmap = fringe_bitmap;
4480 it->right_user_fringe_face_id = face_id;
4483 #endif /* HAVE_WINDOW_SYSTEM */
4484 return 1;
4487 /* Prepare to handle `((margin left-margin) ...)',
4488 `((margin right-margin) ...)' and `((margin nil) ...)'
4489 prefixes for display specifications. */
4490 location = Qunbound;
4491 if (CONSP (spec) && CONSP (XCAR (spec)))
4493 Lisp_Object tem;
4495 value = XCDR (spec);
4496 if (CONSP (value))
4497 value = XCAR (value);
4499 tem = XCAR (spec);
4500 if (EQ (XCAR (tem), Qmargin)
4501 && (tem = XCDR (tem),
4502 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4503 (NILP (tem)
4504 || EQ (tem, Qleft_margin)
4505 || EQ (tem, Qright_margin))))
4506 location = tem;
4509 if (EQ (location, Qunbound))
4511 location = Qnil;
4512 value = spec;
4515 /* After this point, VALUE is the property after any
4516 margin prefix has been stripped. It must be a string,
4517 an image specification, or `(space ...)'.
4519 LOCATION specifies where to display: `left-margin',
4520 `right-margin' or nil. */
4522 valid_p = (STRINGP (value)
4523 #ifdef HAVE_WINDOW_SYSTEM
4524 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4525 && valid_image_p (value))
4526 #endif /* not HAVE_WINDOW_SYSTEM */
4527 || (CONSP (value) && EQ (XCAR (value), Qspace))
4528 #ifdef HAVE_XWIDGETS
4529 || XWIDGETP(value)
4530 #endif
4533 if (valid_p && !display_replaced_p)
4535 if (!it)
4536 return 1;
4538 /* Save current settings of IT so that we can restore them
4539 when we are finished with the glyph property value. */
4540 push_it (it, position);
4541 it->from_overlay = overlay;
4542 it->from_disp_prop_p = 1;
4544 if (NILP (location))
4545 it->area = TEXT_AREA;
4546 else if (EQ (location, Qleft_margin))
4547 it->area = LEFT_MARGIN_AREA;
4548 else
4549 it->area = RIGHT_MARGIN_AREA;
4551 if (STRINGP (value))
4553 it->string = value;
4554 it->multibyte_p = STRING_MULTIBYTE (it->string);
4555 it->current.overlay_string_index = -1;
4556 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4557 it->end_charpos = it->string_nchars = SCHARS (it->string);
4558 it->method = GET_FROM_STRING;
4559 it->stop_charpos = 0;
4560 it->prev_stop = 0;
4561 it->base_level_stop = 0;
4562 it->string_from_display_prop_p = 1;
4563 /* Say that we haven't consumed the characters with
4564 `display' property yet. The call to pop_it in
4565 set_iterator_to_next will clean this up. */
4566 if (BUFFERP (object))
4567 *position = start_pos;
4569 /* Force paragraph direction to be that of the parent
4570 object. If the parent object's paragraph direction is
4571 not yet determined, default to L2R. */
4572 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4573 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4574 else
4575 it->paragraph_embedding = L2R;
4577 /* Set up the bidi iterator for this display string. */
4578 if (it->bidi_p)
4580 it->bidi_it.string.lstring = it->string;
4581 it->bidi_it.string.s = NULL;
4582 it->bidi_it.string.schars = it->end_charpos;
4583 it->bidi_it.string.bufpos = bufpos;
4584 it->bidi_it.string.from_disp_str = 1;
4585 it->bidi_it.string.unibyte = !it->multibyte_p;
4586 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4589 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4591 it->method = GET_FROM_STRETCH;
4592 it->object = value;
4593 *position = it->position = start_pos;
4595 #ifdef HAVE_XWIDGETS
4596 else if (XWIDGETP(value))
4598 //printf("handle_single_display_spec: im an xwidget!!\n");
4599 it->what = IT_XWIDGET;
4600 it->method = GET_FROM_XWIDGET;
4601 it->position = start_pos;
4602 it->object = NILP (object) ? it->w->buffer : object;
4603 *position = start_pos;
4605 it->xwidget = lookup_xwidget(value);
4607 #endif
4608 #ifdef HAVE_WINDOW_SYSTEM
4609 else
4611 it->what = IT_IMAGE;
4612 it->image_id = lookup_image (it->f, value);
4613 it->position = start_pos;
4614 it->object = NILP (object) ? it->w->buffer : object;
4615 it->method = GET_FROM_IMAGE;
4617 /* Say that we haven't consumed the characters with
4618 `display' property yet. The call to pop_it in
4619 set_iterator_to_next will clean this up. */
4620 *position = start_pos;
4622 #endif /* HAVE_WINDOW_SYSTEM */
4624 return 1;
4627 /* Invalid property or property not supported. Restore
4628 POSITION to what it was before. */
4629 *position = start_pos;
4630 return 0;
4633 /* Check if PROP is a display property value whose text should be
4634 treated as intangible. OVERLAY is the overlay from which PROP
4635 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4636 specify the buffer position covered by PROP. */
4639 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4640 EMACS_INT charpos, EMACS_INT bytepos)
4642 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4643 struct text_pos position;
4645 SET_TEXT_POS (position, charpos, bytepos);
4646 return handle_display_spec (NULL, prop, Qnil, overlay,
4647 &position, charpos, frame_window_p);
4651 /* Return 1 if PROP is a display sub-property value containing STRING.
4653 Implementation note: this and the following function are really
4654 special cases of handle_display_spec and
4655 handle_single_display_spec, and should ideally use the same code.
4656 Until they do, these two pairs must be consistent and must be
4657 modified in sync. */
4659 static int
4660 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4662 if (EQ (string, prop))
4663 return 1;
4665 /* Skip over `when FORM'. */
4666 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4668 prop = XCDR (prop);
4669 if (!CONSP (prop))
4670 return 0;
4671 /* Actually, the condition following `when' should be eval'ed,
4672 like handle_single_display_spec does, and we should return
4673 zero if it evaluates to nil. However, this function is
4674 called only when the buffer was already displayed and some
4675 glyph in the glyph matrix was found to come from a display
4676 string. Therefore, the condition was already evaluated, and
4677 the result was non-nil, otherwise the display string wouldn't
4678 have been displayed and we would have never been called for
4679 this property. Thus, we can skip the evaluation and assume
4680 its result is non-nil. */
4681 prop = XCDR (prop);
4684 if (CONSP (prop))
4685 /* Skip over `margin LOCATION'. */
4686 if (EQ (XCAR (prop), Qmargin))
4688 prop = XCDR (prop);
4689 if (!CONSP (prop))
4690 return 0;
4692 prop = XCDR (prop);
4693 if (!CONSP (prop))
4694 return 0;
4697 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4701 /* Return 1 if STRING appears in the `display' property PROP. */
4703 static int
4704 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4706 if (CONSP (prop)
4707 && !EQ (XCAR (prop), Qwhen)
4708 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4710 /* A list of sub-properties. */
4711 while (CONSP (prop))
4713 if (single_display_spec_string_p (XCAR (prop), string))
4714 return 1;
4715 prop = XCDR (prop);
4718 else if (VECTORP (prop))
4720 /* A vector of sub-properties. */
4721 int i;
4722 for (i = 0; i < ASIZE (prop); ++i)
4723 if (single_display_spec_string_p (AREF (prop, i), string))
4724 return 1;
4726 else
4727 return single_display_spec_string_p (prop, string);
4729 return 0;
4732 /* Look for STRING in overlays and text properties in the current
4733 buffer, between character positions FROM and TO (excluding TO).
4734 BACK_P non-zero means look back (in this case, TO is supposed to be
4735 less than FROM).
4736 Value is the first character position where STRING was found, or
4737 zero if it wasn't found before hitting TO.
4739 This function may only use code that doesn't eval because it is
4740 called asynchronously from note_mouse_highlight. */
4742 static EMACS_INT
4743 string_buffer_position_lim (Lisp_Object string,
4744 EMACS_INT from, EMACS_INT to, int back_p)
4746 Lisp_Object limit, prop, pos;
4747 int found = 0;
4749 pos = make_number (from);
4751 if (!back_p) /* looking forward */
4753 limit = make_number (min (to, ZV));
4754 while (!found && !EQ (pos, limit))
4756 prop = Fget_char_property (pos, Qdisplay, Qnil);
4757 if (!NILP (prop) && display_prop_string_p (prop, string))
4758 found = 1;
4759 else
4760 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4761 limit);
4764 else /* looking back */
4766 limit = make_number (max (to, BEGV));
4767 while (!found && !EQ (pos, limit))
4769 prop = Fget_char_property (pos, Qdisplay, Qnil);
4770 if (!NILP (prop) && display_prop_string_p (prop, string))
4771 found = 1;
4772 else
4773 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4774 limit);
4778 return found ? XINT (pos) : 0;
4781 /* Determine which buffer position in current buffer STRING comes from.
4782 AROUND_CHARPOS is an approximate position where it could come from.
4783 Value is the buffer position or 0 if it couldn't be determined.
4785 This function is necessary because we don't record buffer positions
4786 in glyphs generated from strings (to keep struct glyph small).
4787 This function may only use code that doesn't eval because it is
4788 called asynchronously from note_mouse_highlight. */
4790 static EMACS_INT
4791 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4793 const int MAX_DISTANCE = 1000;
4794 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4795 around_charpos + MAX_DISTANCE,
4798 if (!found)
4799 found = string_buffer_position_lim (string, around_charpos,
4800 around_charpos - MAX_DISTANCE, 1);
4801 return found;
4806 /***********************************************************************
4807 `composition' property
4808 ***********************************************************************/
4810 /* Set up iterator IT from `composition' property at its current
4811 position. Called from handle_stop. */
4813 static enum prop_handled
4814 handle_composition_prop (struct it *it)
4816 Lisp_Object prop, string;
4817 EMACS_INT pos, pos_byte, start, end;
4819 if (STRINGP (it->string))
4821 unsigned char *s;
4823 pos = IT_STRING_CHARPOS (*it);
4824 pos_byte = IT_STRING_BYTEPOS (*it);
4825 string = it->string;
4826 s = SDATA (string) + pos_byte;
4827 it->c = STRING_CHAR (s);
4829 else
4831 pos = IT_CHARPOS (*it);
4832 pos_byte = IT_BYTEPOS (*it);
4833 string = Qnil;
4834 it->c = FETCH_CHAR (pos_byte);
4837 /* If there's a valid composition and point is not inside of the
4838 composition (in the case that the composition is from the current
4839 buffer), draw a glyph composed from the composition components. */
4840 if (find_composition (pos, -1, &start, &end, &prop, string)
4841 && COMPOSITION_VALID_P (start, end, prop)
4842 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4844 if (start < pos)
4845 /* As we can't handle this situation (perhaps font-lock added
4846 a new composition), we just return here hoping that next
4847 redisplay will detect this composition much earlier. */
4848 return HANDLED_NORMALLY;
4849 if (start != pos)
4851 if (STRINGP (it->string))
4852 pos_byte = string_char_to_byte (it->string, start);
4853 else
4854 pos_byte = CHAR_TO_BYTE (start);
4856 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4857 prop, string);
4859 if (it->cmp_it.id >= 0)
4861 it->cmp_it.ch = -1;
4862 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4863 it->cmp_it.nglyphs = -1;
4867 return HANDLED_NORMALLY;
4872 /***********************************************************************
4873 Overlay strings
4874 ***********************************************************************/
4876 /* The following structure is used to record overlay strings for
4877 later sorting in load_overlay_strings. */
4879 struct overlay_entry
4881 Lisp_Object overlay;
4882 Lisp_Object string;
4883 int priority;
4884 int after_string_p;
4888 /* Set up iterator IT from overlay strings at its current position.
4889 Called from handle_stop. */
4891 static enum prop_handled
4892 handle_overlay_change (struct it *it)
4894 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4895 return HANDLED_RECOMPUTE_PROPS;
4896 else
4897 return HANDLED_NORMALLY;
4901 /* Set up the next overlay string for delivery by IT, if there is an
4902 overlay string to deliver. Called by set_iterator_to_next when the
4903 end of the current overlay string is reached. If there are more
4904 overlay strings to display, IT->string and
4905 IT->current.overlay_string_index are set appropriately here.
4906 Otherwise IT->string is set to nil. */
4908 static void
4909 next_overlay_string (struct it *it)
4911 ++it->current.overlay_string_index;
4912 if (it->current.overlay_string_index == it->n_overlay_strings)
4914 /* No more overlay strings. Restore IT's settings to what
4915 they were before overlay strings were processed, and
4916 continue to deliver from current_buffer. */
4918 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4919 pop_it (it);
4920 xassert (it->sp > 0
4921 || (NILP (it->string)
4922 && it->method == GET_FROM_BUFFER
4923 && it->stop_charpos >= BEGV
4924 && it->stop_charpos <= it->end_charpos));
4925 it->current.overlay_string_index = -1;
4926 it->n_overlay_strings = 0;
4927 it->overlay_strings_charpos = -1;
4929 /* If we're at the end of the buffer, record that we have
4930 processed the overlay strings there already, so that
4931 next_element_from_buffer doesn't try it again. */
4932 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4933 it->overlay_strings_at_end_processed_p = 1;
4935 else
4937 /* There are more overlay strings to process. If
4938 IT->current.overlay_string_index has advanced to a position
4939 where we must load IT->overlay_strings with more strings, do
4940 it. We must load at the IT->overlay_strings_charpos where
4941 IT->n_overlay_strings was originally computed; when invisible
4942 text is present, this might not be IT_CHARPOS (Bug#7016). */
4943 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4945 if (it->current.overlay_string_index && i == 0)
4946 load_overlay_strings (it, it->overlay_strings_charpos);
4948 /* Initialize IT to deliver display elements from the overlay
4949 string. */
4950 it->string = it->overlay_strings[i];
4951 it->multibyte_p = STRING_MULTIBYTE (it->string);
4952 SET_TEXT_POS (it->current.string_pos, 0, 0);
4953 it->method = GET_FROM_STRING;
4954 it->stop_charpos = 0;
4955 if (it->cmp_it.stop_pos >= 0)
4956 it->cmp_it.stop_pos = 0;
4957 it->prev_stop = 0;
4958 it->base_level_stop = 0;
4960 /* Set up the bidi iterator for this overlay string. */
4961 if (it->bidi_p)
4963 it->bidi_it.string.lstring = it->string;
4964 it->bidi_it.string.s = NULL;
4965 it->bidi_it.string.schars = SCHARS (it->string);
4966 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4967 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4968 it->bidi_it.string.unibyte = !it->multibyte_p;
4969 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4973 CHECK_IT (it);
4977 /* Compare two overlay_entry structures E1 and E2. Used as a
4978 comparison function for qsort in load_overlay_strings. Overlay
4979 strings for the same position are sorted so that
4981 1. All after-strings come in front of before-strings, except
4982 when they come from the same overlay.
4984 2. Within after-strings, strings are sorted so that overlay strings
4985 from overlays with higher priorities come first.
4987 2. Within before-strings, strings are sorted so that overlay
4988 strings from overlays with higher priorities come last.
4990 Value is analogous to strcmp. */
4993 static int
4994 compare_overlay_entries (const void *e1, const void *e2)
4996 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4997 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4998 int result;
5000 if (entry1->after_string_p != entry2->after_string_p)
5002 /* Let after-strings appear in front of before-strings if
5003 they come from different overlays. */
5004 if (EQ (entry1->overlay, entry2->overlay))
5005 result = entry1->after_string_p ? 1 : -1;
5006 else
5007 result = entry1->after_string_p ? -1 : 1;
5009 else if (entry1->after_string_p)
5010 /* After-strings sorted in order of decreasing priority. */
5011 result = entry2->priority - entry1->priority;
5012 else
5013 /* Before-strings sorted in order of increasing priority. */
5014 result = entry1->priority - entry2->priority;
5016 return result;
5020 /* Load the vector IT->overlay_strings with overlay strings from IT's
5021 current buffer position, or from CHARPOS if that is > 0. Set
5022 IT->n_overlays to the total number of overlay strings found.
5024 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5025 a time. On entry into load_overlay_strings,
5026 IT->current.overlay_string_index gives the number of overlay
5027 strings that have already been loaded by previous calls to this
5028 function.
5030 IT->add_overlay_start contains an additional overlay start
5031 position to consider for taking overlay strings from, if non-zero.
5032 This position comes into play when the overlay has an `invisible'
5033 property, and both before and after-strings. When we've skipped to
5034 the end of the overlay, because of its `invisible' property, we
5035 nevertheless want its before-string to appear.
5036 IT->add_overlay_start will contain the overlay start position
5037 in this case.
5039 Overlay strings are sorted so that after-string strings come in
5040 front of before-string strings. Within before and after-strings,
5041 strings are sorted by overlay priority. See also function
5042 compare_overlay_entries. */
5044 static void
5045 load_overlay_strings (struct it *it, EMACS_INT charpos)
5047 Lisp_Object overlay, window, str, invisible;
5048 struct Lisp_Overlay *ov;
5049 EMACS_INT start, end;
5050 int size = 20;
5051 int n = 0, i, j, invis_p;
5052 struct overlay_entry *entries
5053 = (struct overlay_entry *) alloca (size * sizeof *entries);
5055 if (charpos <= 0)
5056 charpos = IT_CHARPOS (*it);
5058 /* Append the overlay string STRING of overlay OVERLAY to vector
5059 `entries' which has size `size' and currently contains `n'
5060 elements. AFTER_P non-zero means STRING is an after-string of
5061 OVERLAY. */
5062 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5063 do \
5065 Lisp_Object priority; \
5067 if (n == size) \
5069 int new_size = 2 * size; \
5070 struct overlay_entry *old = entries; \
5071 entries = \
5072 (struct overlay_entry *) alloca (new_size \
5073 * sizeof *entries); \
5074 memcpy (entries, old, size * sizeof *entries); \
5075 size = new_size; \
5078 entries[n].string = (STRING); \
5079 entries[n].overlay = (OVERLAY); \
5080 priority = Foverlay_get ((OVERLAY), Qpriority); \
5081 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5082 entries[n].after_string_p = (AFTER_P); \
5083 ++n; \
5085 while (0)
5087 /* Process overlay before the overlay center. */
5088 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5090 XSETMISC (overlay, ov);
5091 xassert (OVERLAYP (overlay));
5092 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5093 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5095 if (end < charpos)
5096 break;
5098 /* Skip this overlay if it doesn't start or end at IT's current
5099 position. */
5100 if (end != charpos && start != charpos)
5101 continue;
5103 /* Skip this overlay if it doesn't apply to IT->w. */
5104 window = Foverlay_get (overlay, Qwindow);
5105 if (WINDOWP (window) && XWINDOW (window) != it->w)
5106 continue;
5108 /* If the text ``under'' the overlay is invisible, both before-
5109 and after-strings from this overlay are visible; start and
5110 end position are indistinguishable. */
5111 invisible = Foverlay_get (overlay, Qinvisible);
5112 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5114 /* If overlay has a non-empty before-string, record it. */
5115 if ((start == charpos || (end == charpos && invis_p))
5116 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5117 && SCHARS (str))
5118 RECORD_OVERLAY_STRING (overlay, str, 0);
5120 /* If overlay has a non-empty after-string, record it. */
5121 if ((end == charpos || (start == charpos && invis_p))
5122 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5123 && SCHARS (str))
5124 RECORD_OVERLAY_STRING (overlay, str, 1);
5127 /* Process overlays after the overlay center. */
5128 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5130 XSETMISC (overlay, ov);
5131 xassert (OVERLAYP (overlay));
5132 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5133 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5135 if (start > charpos)
5136 break;
5138 /* Skip this overlay if it doesn't start or end at IT's current
5139 position. */
5140 if (end != charpos && start != charpos)
5141 continue;
5143 /* Skip this overlay if it doesn't apply to IT->w. */
5144 window = Foverlay_get (overlay, Qwindow);
5145 if (WINDOWP (window) && XWINDOW (window) != it->w)
5146 continue;
5148 /* If the text ``under'' the overlay is invisible, it has a zero
5149 dimension, and both before- and after-strings apply. */
5150 invisible = Foverlay_get (overlay, Qinvisible);
5151 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5153 /* If overlay has a non-empty before-string, record it. */
5154 if ((start == charpos || (end == charpos && invis_p))
5155 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5156 && SCHARS (str))
5157 RECORD_OVERLAY_STRING (overlay, str, 0);
5159 /* If overlay has a non-empty after-string, record it. */
5160 if ((end == charpos || (start == charpos && invis_p))
5161 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5162 && SCHARS (str))
5163 RECORD_OVERLAY_STRING (overlay, str, 1);
5166 #undef RECORD_OVERLAY_STRING
5168 /* Sort entries. */
5169 if (n > 1)
5170 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5172 /* Record number of overlay strings, and where we computed it. */
5173 it->n_overlay_strings = n;
5174 it->overlay_strings_charpos = charpos;
5176 /* IT->current.overlay_string_index is the number of overlay strings
5177 that have already been consumed by IT. Copy some of the
5178 remaining overlay strings to IT->overlay_strings. */
5179 i = 0;
5180 j = it->current.overlay_string_index;
5181 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5183 it->overlay_strings[i] = entries[j].string;
5184 it->string_overlays[i++] = entries[j++].overlay;
5187 CHECK_IT (it);
5191 /* Get the first chunk of overlay strings at IT's current buffer
5192 position, or at CHARPOS if that is > 0. Value is non-zero if at
5193 least one overlay string was found. */
5195 static int
5196 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5198 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5199 process. This fills IT->overlay_strings with strings, and sets
5200 IT->n_overlay_strings to the total number of strings to process.
5201 IT->pos.overlay_string_index has to be set temporarily to zero
5202 because load_overlay_strings needs this; it must be set to -1
5203 when no overlay strings are found because a zero value would
5204 indicate a position in the first overlay string. */
5205 it->current.overlay_string_index = 0;
5206 load_overlay_strings (it, charpos);
5208 /* If we found overlay strings, set up IT to deliver display
5209 elements from the first one. Otherwise set up IT to deliver
5210 from current_buffer. */
5211 if (it->n_overlay_strings)
5213 /* Make sure we know settings in current_buffer, so that we can
5214 restore meaningful values when we're done with the overlay
5215 strings. */
5216 if (compute_stop_p)
5217 compute_stop_pos (it);
5218 xassert (it->face_id >= 0);
5220 /* Save IT's settings. They are restored after all overlay
5221 strings have been processed. */
5222 xassert (!compute_stop_p || it->sp == 0);
5224 /* When called from handle_stop, there might be an empty display
5225 string loaded. In that case, don't bother saving it. */
5226 if (!STRINGP (it->string) || SCHARS (it->string))
5227 push_it (it, NULL);
5229 /* Set up IT to deliver display elements from the first overlay
5230 string. */
5231 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5232 it->string = it->overlay_strings[0];
5233 it->from_overlay = Qnil;
5234 it->stop_charpos = 0;
5235 xassert (STRINGP (it->string));
5236 it->end_charpos = SCHARS (it->string);
5237 it->prev_stop = 0;
5238 it->base_level_stop = 0;
5239 it->multibyte_p = STRING_MULTIBYTE (it->string);
5240 it->method = GET_FROM_STRING;
5241 it->from_disp_prop_p = 0;
5243 /* Force paragraph direction to be that of the parent
5244 buffer. */
5245 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5246 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5247 else
5248 it->paragraph_embedding = L2R;
5250 /* Set up the bidi iterator for this overlay string. */
5251 if (it->bidi_p)
5253 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5255 it->bidi_it.string.lstring = it->string;
5256 it->bidi_it.string.s = NULL;
5257 it->bidi_it.string.schars = SCHARS (it->string);
5258 it->bidi_it.string.bufpos = pos;
5259 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5260 it->bidi_it.string.unibyte = !it->multibyte_p;
5261 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5263 return 1;
5266 it->current.overlay_string_index = -1;
5267 return 0;
5270 static int
5271 get_overlay_strings (struct it *it, EMACS_INT charpos)
5273 it->string = Qnil;
5274 it->method = GET_FROM_BUFFER;
5276 (void) get_overlay_strings_1 (it, charpos, 1);
5278 CHECK_IT (it);
5280 /* Value is non-zero if we found at least one overlay string. */
5281 return STRINGP (it->string);
5286 /***********************************************************************
5287 Saving and restoring state
5288 ***********************************************************************/
5290 /* Save current settings of IT on IT->stack. Called, for example,
5291 before setting up IT for an overlay string, to be able to restore
5292 IT's settings to what they were after the overlay string has been
5293 processed. If POSITION is non-NULL, it is the position to save on
5294 the stack instead of IT->position. */
5296 static void
5297 push_it (struct it *it, struct text_pos *position)
5299 struct iterator_stack_entry *p;
5301 xassert (it->sp < IT_STACK_SIZE);
5302 p = it->stack + it->sp;
5304 p->stop_charpos = it->stop_charpos;
5305 p->prev_stop = it->prev_stop;
5306 p->base_level_stop = it->base_level_stop;
5307 p->cmp_it = it->cmp_it;
5308 xassert (it->face_id >= 0);
5309 p->face_id = it->face_id;
5310 p->string = it->string;
5311 p->method = it->method;
5312 p->from_overlay = it->from_overlay;
5313 switch (p->method)
5315 case GET_FROM_IMAGE:
5316 p->u.image.object = it->object;
5317 p->u.image.image_id = it->image_id;
5318 p->u.image.slice = it->slice;
5319 break;
5320 case GET_FROM_STRETCH:
5321 p->u.stretch.object = it->object;
5322 break;
5323 #ifdef HAVE_XWIDGETS
5324 case GET_FROM_XWIDGET:
5325 p->u.xwidget.object = it->object;
5326 break;
5327 #endif
5329 p->position = position ? *position : it->position;
5330 p->current = it->current;
5331 p->end_charpos = it->end_charpos;
5332 p->string_nchars = it->string_nchars;
5333 p->area = it->area;
5334 p->multibyte_p = it->multibyte_p;
5335 p->avoid_cursor_p = it->avoid_cursor_p;
5336 p->space_width = it->space_width;
5337 p->font_height = it->font_height;
5338 p->voffset = it->voffset;
5339 p->string_from_display_prop_p = it->string_from_display_prop_p;
5340 p->display_ellipsis_p = 0;
5341 p->line_wrap = it->line_wrap;
5342 p->bidi_p = it->bidi_p;
5343 p->paragraph_embedding = it->paragraph_embedding;
5344 p->from_disp_prop_p = it->from_disp_prop_p;
5345 ++it->sp;
5347 /* Save the state of the bidi iterator as well. */
5348 if (it->bidi_p)
5349 bidi_push_it (&it->bidi_it);
5352 static void
5353 iterate_out_of_display_property (struct it *it)
5355 int buffer_p = BUFFERP (it->object);
5356 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5357 EMACS_INT bob = (buffer_p ? BEGV : 0);
5359 /* Maybe initialize paragraph direction. If we are at the beginning
5360 of a new paragraph, next_element_from_buffer may not have a
5361 chance to do that. */
5362 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5363 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5364 /* prev_stop can be zero, so check against BEGV as well. */
5365 while (it->bidi_it.charpos >= bob
5366 && it->prev_stop <= it->bidi_it.charpos
5367 && it->bidi_it.charpos < CHARPOS (it->position))
5368 bidi_move_to_visually_next (&it->bidi_it);
5369 /* Record the stop_pos we just crossed, for when we cross it
5370 back, maybe. */
5371 if (it->bidi_it.charpos > CHARPOS (it->position))
5372 it->prev_stop = CHARPOS (it->position);
5373 /* If we ended up not where pop_it put us, resync IT's
5374 positional members with the bidi iterator. */
5375 if (it->bidi_it.charpos != CHARPOS (it->position))
5377 SET_TEXT_POS (it->position,
5378 it->bidi_it.charpos, it->bidi_it.bytepos);
5379 if (buffer_p)
5380 it->current.pos = it->position;
5381 else
5382 it->current.string_pos = it->position;
5386 /* Restore IT's settings from IT->stack. Called, for example, when no
5387 more overlay strings must be processed, and we return to delivering
5388 display elements from a buffer, or when the end of a string from a
5389 `display' property is reached and we return to delivering display
5390 elements from an overlay string, or from a buffer. */
5392 static void
5393 pop_it (struct it *it)
5395 struct iterator_stack_entry *p;
5396 int from_display_prop = it->from_disp_prop_p;
5398 xassert (it->sp > 0);
5399 --it->sp;
5400 p = it->stack + it->sp;
5401 it->stop_charpos = p->stop_charpos;
5402 it->prev_stop = p->prev_stop;
5403 it->base_level_stop = p->base_level_stop;
5404 it->cmp_it = p->cmp_it;
5405 it->face_id = p->face_id;
5406 it->current = p->current;
5407 it->position = p->position;
5408 it->string = p->string;
5409 it->from_overlay = p->from_overlay;
5410 if (NILP (it->string))
5411 SET_TEXT_POS (it->current.string_pos, -1, -1);
5412 it->method = p->method;
5413 switch (it->method)
5415 case GET_FROM_IMAGE:
5416 it->image_id = p->u.image.image_id;
5417 it->object = p->u.image.object;
5418 it->slice = p->u.image.slice;
5419 break;
5420 #ifdef HAVE_XWIDGETS
5421 case GET_FROM_XWIDGET:
5422 it->object = p->u.xwidget.object;
5423 break;
5424 #endif
5425 case GET_FROM_STRETCH:
5426 it->object = p->u.stretch.object;
5427 break;
5428 case GET_FROM_BUFFER:
5429 it->object = it->w->buffer;
5430 break;
5431 case GET_FROM_STRING:
5432 it->object = it->string;
5433 break;
5434 case GET_FROM_DISPLAY_VECTOR:
5435 if (it->s)
5436 it->method = GET_FROM_C_STRING;
5437 else if (STRINGP (it->string))
5438 it->method = GET_FROM_STRING;
5439 else
5441 it->method = GET_FROM_BUFFER;
5442 it->object = it->w->buffer;
5445 it->end_charpos = p->end_charpos;
5446 it->string_nchars = p->string_nchars;
5447 it->area = p->area;
5448 it->multibyte_p = p->multibyte_p;
5449 it->avoid_cursor_p = p->avoid_cursor_p;
5450 it->space_width = p->space_width;
5451 it->font_height = p->font_height;
5452 it->voffset = p->voffset;
5453 it->string_from_display_prop_p = p->string_from_display_prop_p;
5454 it->line_wrap = p->line_wrap;
5455 it->bidi_p = p->bidi_p;
5456 it->paragraph_embedding = p->paragraph_embedding;
5457 it->from_disp_prop_p = p->from_disp_prop_p;
5458 if (it->bidi_p)
5460 bidi_pop_it (&it->bidi_it);
5461 /* Bidi-iterate until we get out of the portion of text, if any,
5462 covered by a `display' text property or by an overlay with
5463 `display' property. (We cannot just jump there, because the
5464 internal coherency of the bidi iterator state can not be
5465 preserved across such jumps.) We also must determine the
5466 paragraph base direction if the overlay we just processed is
5467 at the beginning of a new paragraph. */
5468 if (from_display_prop
5469 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5470 iterate_out_of_display_property (it);
5472 xassert ((BUFFERP (it->object)
5473 && IT_CHARPOS (*it) == it->bidi_it.charpos
5474 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5475 || (STRINGP (it->object)
5476 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5477 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos));
5483 /***********************************************************************
5484 Moving over lines
5485 ***********************************************************************/
5487 /* Set IT's current position to the previous line start. */
5489 static void
5490 back_to_previous_line_start (struct it *it)
5492 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5493 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5497 /* Move IT to the next line start.
5499 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5500 we skipped over part of the text (as opposed to moving the iterator
5501 continuously over the text). Otherwise, don't change the value
5502 of *SKIPPED_P.
5504 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5505 iterator on the newline, if it was found.
5507 Newlines may come from buffer text, overlay strings, or strings
5508 displayed via the `display' property. That's the reason we can't
5509 simply use find_next_newline_no_quit.
5511 Note that this function may not skip over invisible text that is so
5512 because of text properties and immediately follows a newline. If
5513 it would, function reseat_at_next_visible_line_start, when called
5514 from set_iterator_to_next, would effectively make invisible
5515 characters following a newline part of the wrong glyph row, which
5516 leads to wrong cursor motion. */
5518 static int
5519 forward_to_next_line_start (struct it *it, int *skipped_p,
5520 struct bidi_it *bidi_it_prev)
5522 EMACS_INT old_selective;
5523 int newline_found_p, n;
5524 const int MAX_NEWLINE_DISTANCE = 500;
5526 /* If already on a newline, just consume it to avoid unintended
5527 skipping over invisible text below. */
5528 if (it->what == IT_CHARACTER
5529 && it->c == '\n'
5530 && CHARPOS (it->position) == IT_CHARPOS (*it))
5532 if (it->bidi_p && bidi_it_prev)
5533 *bidi_it_prev = it->bidi_it;
5534 set_iterator_to_next (it, 0);
5535 it->c = 0;
5536 return 1;
5539 /* Don't handle selective display in the following. It's (a)
5540 unnecessary because it's done by the caller, and (b) leads to an
5541 infinite recursion because next_element_from_ellipsis indirectly
5542 calls this function. */
5543 old_selective = it->selective;
5544 it->selective = 0;
5546 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5547 from buffer text. */
5548 for (n = newline_found_p = 0;
5549 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5550 n += STRINGP (it->string) ? 0 : 1)
5552 if (!get_next_display_element (it))
5553 return 0;
5554 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5555 if (newline_found_p && it->bidi_p && bidi_it_prev)
5556 *bidi_it_prev = it->bidi_it;
5557 set_iterator_to_next (it, 0);
5560 /* If we didn't find a newline near enough, see if we can use a
5561 short-cut. */
5562 if (!newline_found_p)
5564 EMACS_INT start = IT_CHARPOS (*it);
5565 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5566 Lisp_Object pos;
5568 xassert (!STRINGP (it->string));
5570 /* If there isn't any `display' property in sight, and no
5571 overlays, we can just use the position of the newline in
5572 buffer text. */
5573 if (it->stop_charpos >= limit
5574 || ((pos = Fnext_single_property_change (make_number (start),
5575 Qdisplay, Qnil,
5576 make_number (limit)),
5577 NILP (pos))
5578 && next_overlay_change (start) == ZV))
5580 if (!it->bidi_p)
5582 IT_CHARPOS (*it) = limit;
5583 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5585 else
5587 struct bidi_it bprev;
5589 /* Help bidi.c avoid expensive searches for display
5590 properties and overlays, by telling it that there are
5591 none up to `limit'. */
5592 if (it->bidi_it.disp_pos < limit)
5594 it->bidi_it.disp_pos = limit;
5595 it->bidi_it.disp_prop_p = 0;
5597 do {
5598 bprev = it->bidi_it;
5599 bidi_move_to_visually_next (&it->bidi_it);
5600 } while (it->bidi_it.charpos != limit);
5601 IT_CHARPOS (*it) = limit;
5602 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5603 if (bidi_it_prev)
5604 *bidi_it_prev = bprev;
5606 *skipped_p = newline_found_p = 1;
5608 else
5610 while (get_next_display_element (it)
5611 && !newline_found_p)
5613 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5614 if (newline_found_p && it->bidi_p && bidi_it_prev)
5615 *bidi_it_prev = it->bidi_it;
5616 set_iterator_to_next (it, 0);
5621 it->selective = old_selective;
5622 return newline_found_p;
5626 /* Set IT's current position to the previous visible line start. Skip
5627 invisible text that is so either due to text properties or due to
5628 selective display. Caution: this does not change IT->current_x and
5629 IT->hpos. */
5631 static void
5632 back_to_previous_visible_line_start (struct it *it)
5634 while (IT_CHARPOS (*it) > BEGV)
5636 back_to_previous_line_start (it);
5638 if (IT_CHARPOS (*it) <= BEGV)
5639 break;
5641 /* If selective > 0, then lines indented more than its value are
5642 invisible. */
5643 if (it->selective > 0
5644 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5645 it->selective))
5646 continue;
5648 /* Check the newline before point for invisibility. */
5650 Lisp_Object prop;
5651 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5652 Qinvisible, it->window);
5653 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5654 continue;
5657 if (IT_CHARPOS (*it) <= BEGV)
5658 break;
5661 struct it it2;
5662 void *it2data = NULL;
5663 EMACS_INT pos;
5664 EMACS_INT beg, end;
5665 Lisp_Object val, overlay;
5667 SAVE_IT (it2, *it, it2data);
5669 /* If newline is part of a composition, continue from start of composition */
5670 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5671 && beg < IT_CHARPOS (*it))
5672 goto replaced;
5674 /* If newline is replaced by a display property, find start of overlay
5675 or interval and continue search from that point. */
5676 pos = --IT_CHARPOS (it2);
5677 --IT_BYTEPOS (it2);
5678 it2.sp = 0;
5679 bidi_unshelve_cache (NULL, 0);
5680 it2.string_from_display_prop_p = 0;
5681 it2.from_disp_prop_p = 0;
5682 if (handle_display_prop (&it2) == HANDLED_RETURN
5683 && !NILP (val = get_char_property_and_overlay
5684 (make_number (pos), Qdisplay, Qnil, &overlay))
5685 && (OVERLAYP (overlay)
5686 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5687 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5689 RESTORE_IT (it, it, it2data);
5690 goto replaced;
5693 /* Newline is not replaced by anything -- so we are done. */
5694 RESTORE_IT (it, it, it2data);
5695 break;
5697 replaced:
5698 if (beg < BEGV)
5699 beg = BEGV;
5700 IT_CHARPOS (*it) = beg;
5701 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5705 it->continuation_lines_width = 0;
5707 xassert (IT_CHARPOS (*it) >= BEGV);
5708 xassert (IT_CHARPOS (*it) == BEGV
5709 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5710 CHECK_IT (it);
5714 /* Reseat iterator IT at the previous visible line start. Skip
5715 invisible text that is so either due to text properties or due to
5716 selective display. At the end, update IT's overlay information,
5717 face information etc. */
5719 void
5720 reseat_at_previous_visible_line_start (struct it *it)
5722 back_to_previous_visible_line_start (it);
5723 reseat (it, it->current.pos, 1);
5724 CHECK_IT (it);
5728 /* Reseat iterator IT on the next visible line start in the current
5729 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5730 preceding the line start. Skip over invisible text that is so
5731 because of selective display. Compute faces, overlays etc at the
5732 new position. Note that this function does not skip over text that
5733 is invisible because of text properties. */
5735 static void
5736 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5738 int newline_found_p, skipped_p = 0;
5739 struct bidi_it bidi_it_prev;
5741 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5743 /* Skip over lines that are invisible because they are indented
5744 more than the value of IT->selective. */
5745 if (it->selective > 0)
5746 while (IT_CHARPOS (*it) < ZV
5747 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5748 it->selective))
5750 xassert (IT_BYTEPOS (*it) == BEGV
5751 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5752 newline_found_p =
5753 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5756 /* Position on the newline if that's what's requested. */
5757 if (on_newline_p && newline_found_p)
5759 if (STRINGP (it->string))
5761 if (IT_STRING_CHARPOS (*it) > 0)
5763 if (!it->bidi_p)
5765 --IT_STRING_CHARPOS (*it);
5766 --IT_STRING_BYTEPOS (*it);
5768 else
5770 /* We need to restore the bidi iterator to the state
5771 it had on the newline, and resync the IT's
5772 position with that. */
5773 it->bidi_it = bidi_it_prev;
5774 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
5775 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
5779 else if (IT_CHARPOS (*it) > BEGV)
5781 if (!it->bidi_p)
5783 --IT_CHARPOS (*it);
5784 --IT_BYTEPOS (*it);
5786 else
5788 /* We need to restore the bidi iterator to the state it
5789 had on the newline and resync IT with that. */
5790 it->bidi_it = bidi_it_prev;
5791 IT_CHARPOS (*it) = it->bidi_it.charpos;
5792 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5794 reseat (it, it->current.pos, 0);
5797 else if (skipped_p)
5798 reseat (it, it->current.pos, 0);
5800 CHECK_IT (it);
5805 /***********************************************************************
5806 Changing an iterator's position
5807 ***********************************************************************/
5809 /* Change IT's current position to POS in current_buffer. If FORCE_P
5810 is non-zero, always check for text properties at the new position.
5811 Otherwise, text properties are only looked up if POS >=
5812 IT->check_charpos of a property. */
5814 static void
5815 reseat (struct it *it, struct text_pos pos, int force_p)
5817 EMACS_INT original_pos = IT_CHARPOS (*it);
5819 reseat_1 (it, pos, 0);
5821 /* Determine where to check text properties. Avoid doing it
5822 where possible because text property lookup is very expensive. */
5823 if (force_p
5824 || CHARPOS (pos) > it->stop_charpos
5825 || CHARPOS (pos) < original_pos)
5827 if (it->bidi_p)
5829 /* For bidi iteration, we need to prime prev_stop and
5830 base_level_stop with our best estimations. */
5831 /* Implementation note: Of course, POS is not necessarily a
5832 stop position, so assigning prev_pos to it is a lie; we
5833 should have called compute_stop_backwards. However, if
5834 the current buffer does not include any R2L characters,
5835 that call would be a waste of cycles, because the
5836 iterator will never move back, and thus never cross this
5837 "fake" stop position. So we delay that backward search
5838 until the time we really need it, in next_element_from_buffer. */
5839 if (CHARPOS (pos) != it->prev_stop)
5840 it->prev_stop = CHARPOS (pos);
5841 if (CHARPOS (pos) < it->base_level_stop)
5842 it->base_level_stop = 0; /* meaning it's unknown */
5843 handle_stop (it);
5845 else
5847 handle_stop (it);
5848 it->prev_stop = it->base_level_stop = 0;
5853 CHECK_IT (it);
5857 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5858 IT->stop_pos to POS, also. */
5860 static void
5861 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5863 /* Don't call this function when scanning a C string. */
5864 xassert (it->s == NULL);
5866 /* POS must be a reasonable value. */
5867 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5869 it->current.pos = it->position = pos;
5870 it->end_charpos = ZV;
5871 it->dpvec = NULL;
5872 it->current.dpvec_index = -1;
5873 it->current.overlay_string_index = -1;
5874 IT_STRING_CHARPOS (*it) = -1;
5875 IT_STRING_BYTEPOS (*it) = -1;
5876 it->string = Qnil;
5877 it->method = GET_FROM_BUFFER;
5878 it->object = it->w->buffer;
5879 it->area = TEXT_AREA;
5880 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5881 it->sp = 0;
5882 it->string_from_display_prop_p = 0;
5883 it->from_disp_prop_p = 0;
5884 it->face_before_selective_p = 0;
5885 if (it->bidi_p)
5887 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5888 &it->bidi_it);
5889 bidi_unshelve_cache (NULL, 0);
5890 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5891 it->bidi_it.string.s = NULL;
5892 it->bidi_it.string.lstring = Qnil;
5893 it->bidi_it.string.bufpos = 0;
5894 it->bidi_it.string.unibyte = 0;
5897 if (set_stop_p)
5899 it->stop_charpos = CHARPOS (pos);
5900 it->base_level_stop = CHARPOS (pos);
5905 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5906 If S is non-null, it is a C string to iterate over. Otherwise,
5907 STRING gives a Lisp string to iterate over.
5909 If PRECISION > 0, don't return more then PRECISION number of
5910 characters from the string.
5912 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5913 characters have been returned. FIELD_WIDTH < 0 means an infinite
5914 field width.
5916 MULTIBYTE = 0 means disable processing of multibyte characters,
5917 MULTIBYTE > 0 means enable it,
5918 MULTIBYTE < 0 means use IT->multibyte_p.
5920 IT must be initialized via a prior call to init_iterator before
5921 calling this function. */
5923 static void
5924 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5925 EMACS_INT charpos, EMACS_INT precision, int field_width,
5926 int multibyte)
5928 /* No region in strings. */
5929 it->region_beg_charpos = it->region_end_charpos = -1;
5931 /* No text property checks performed by default, but see below. */
5932 it->stop_charpos = -1;
5934 /* Set iterator position and end position. */
5935 memset (&it->current, 0, sizeof it->current);
5936 it->current.overlay_string_index = -1;
5937 it->current.dpvec_index = -1;
5938 xassert (charpos >= 0);
5940 /* If STRING is specified, use its multibyteness, otherwise use the
5941 setting of MULTIBYTE, if specified. */
5942 if (multibyte >= 0)
5943 it->multibyte_p = multibyte > 0;
5945 /* Bidirectional reordering of strings is controlled by the default
5946 value of bidi-display-reordering. */
5947 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5949 if (s == NULL)
5951 xassert (STRINGP (string));
5952 it->string = string;
5953 it->s = NULL;
5954 it->end_charpos = it->string_nchars = SCHARS (string);
5955 it->method = GET_FROM_STRING;
5956 it->current.string_pos = string_pos (charpos, string);
5958 if (it->bidi_p)
5960 it->bidi_it.string.lstring = string;
5961 it->bidi_it.string.s = NULL;
5962 it->bidi_it.string.schars = it->end_charpos;
5963 it->bidi_it.string.bufpos = 0;
5964 it->bidi_it.string.from_disp_str = 0;
5965 it->bidi_it.string.unibyte = !it->multibyte_p;
5966 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5967 FRAME_WINDOW_P (it->f), &it->bidi_it);
5970 else
5972 it->s = (const unsigned char *) s;
5973 it->string = Qnil;
5975 /* Note that we use IT->current.pos, not it->current.string_pos,
5976 for displaying C strings. */
5977 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5978 if (it->multibyte_p)
5980 it->current.pos = c_string_pos (charpos, s, 1);
5981 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5983 else
5985 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5986 it->end_charpos = it->string_nchars = strlen (s);
5989 if (it->bidi_p)
5991 it->bidi_it.string.lstring = Qnil;
5992 it->bidi_it.string.s = (const unsigned char *) s;
5993 it->bidi_it.string.schars = it->end_charpos;
5994 it->bidi_it.string.bufpos = 0;
5995 it->bidi_it.string.from_disp_str = 0;
5996 it->bidi_it.string.unibyte = !it->multibyte_p;
5997 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5998 &it->bidi_it);
6000 it->method = GET_FROM_C_STRING;
6003 /* PRECISION > 0 means don't return more than PRECISION characters
6004 from the string. */
6005 if (precision > 0 && it->end_charpos - charpos > precision)
6007 it->end_charpos = it->string_nchars = charpos + precision;
6008 if (it->bidi_p)
6009 it->bidi_it.string.schars = it->end_charpos;
6012 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6013 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6014 FIELD_WIDTH < 0 means infinite field width. This is useful for
6015 padding with `-' at the end of a mode line. */
6016 if (field_width < 0)
6017 field_width = INFINITY;
6018 /* Implementation note: We deliberately don't enlarge
6019 it->bidi_it.string.schars here to fit it->end_charpos, because
6020 the bidi iterator cannot produce characters out of thin air. */
6021 if (field_width > it->end_charpos - charpos)
6022 it->end_charpos = charpos + field_width;
6024 /* Use the standard display table for displaying strings. */
6025 if (DISP_TABLE_P (Vstandard_display_table))
6026 it->dp = XCHAR_TABLE (Vstandard_display_table);
6028 it->stop_charpos = charpos;
6029 it->prev_stop = charpos;
6030 it->base_level_stop = 0;
6031 if (it->bidi_p)
6033 it->bidi_it.first_elt = 1;
6034 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6035 it->bidi_it.disp_pos = -1;
6037 if (s == NULL && it->multibyte_p)
6039 EMACS_INT endpos = SCHARS (it->string);
6040 if (endpos > it->end_charpos)
6041 endpos = it->end_charpos;
6042 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6043 it->string);
6045 CHECK_IT (it);
6050 /***********************************************************************
6051 Iteration
6052 ***********************************************************************/
6054 /* Map enum it_method value to corresponding next_element_from_* function. */
6056 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6058 next_element_from_buffer,
6059 next_element_from_display_vector,
6060 next_element_from_string,
6061 next_element_from_c_string,
6062 next_element_from_image,
6063 next_element_from_stretch
6064 #ifdef HAVE_XWIDGETS
6065 ,next_element_from_xwidget
6066 #endif
6069 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6072 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6073 (possibly with the following characters). */
6075 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6076 ((IT)->cmp_it.id >= 0 \
6077 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6078 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6079 END_CHARPOS, (IT)->w, \
6080 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6081 (IT)->string)))
6084 /* Lookup the char-table Vglyphless_char_display for character C (-1
6085 if we want information for no-font case), and return the display
6086 method symbol. By side-effect, update it->what and
6087 it->glyphless_method. This function is called from
6088 get_next_display_element for each character element, and from
6089 x_produce_glyphs when no suitable font was found. */
6091 Lisp_Object
6092 lookup_glyphless_char_display (int c, struct it *it)
6094 Lisp_Object glyphless_method = Qnil;
6096 if (CHAR_TABLE_P (Vglyphless_char_display)
6097 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6099 if (c >= 0)
6101 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6102 if (CONSP (glyphless_method))
6103 glyphless_method = FRAME_WINDOW_P (it->f)
6104 ? XCAR (glyphless_method)
6105 : XCDR (glyphless_method);
6107 else
6108 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6111 retry:
6112 if (NILP (glyphless_method))
6114 if (c >= 0)
6115 /* The default is to display the character by a proper font. */
6116 return Qnil;
6117 /* The default for the no-font case is to display an empty box. */
6118 glyphless_method = Qempty_box;
6120 if (EQ (glyphless_method, Qzero_width))
6122 if (c >= 0)
6123 return glyphless_method;
6124 /* This method can't be used for the no-font case. */
6125 glyphless_method = Qempty_box;
6127 if (EQ (glyphless_method, Qthin_space))
6128 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6129 else if (EQ (glyphless_method, Qempty_box))
6130 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6131 else if (EQ (glyphless_method, Qhex_code))
6132 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6133 else if (STRINGP (glyphless_method))
6134 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6135 else
6137 /* Invalid value. We use the default method. */
6138 glyphless_method = Qnil;
6139 goto retry;
6141 it->what = IT_GLYPHLESS;
6142 return glyphless_method;
6145 /* Load IT's display element fields with information about the next
6146 display element from the current position of IT. Value is zero if
6147 end of buffer (or C string) is reached. */
6149 static struct frame *last_escape_glyph_frame = NULL;
6150 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6151 static int last_escape_glyph_merged_face_id = 0;
6153 struct frame *last_glyphless_glyph_frame = NULL;
6154 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6155 int last_glyphless_glyph_merged_face_id = 0;
6157 static int
6158 get_next_display_element (struct it *it)
6160 /* Non-zero means that we found a display element. Zero means that
6161 we hit the end of what we iterate over. Performance note: the
6162 function pointer `method' used here turns out to be faster than
6163 using a sequence of if-statements. */
6164 int success_p;
6166 get_next:
6167 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6169 if (it->what == IT_CHARACTER)
6171 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6172 and only if (a) the resolved directionality of that character
6173 is R..." */
6174 /* FIXME: Do we need an exception for characters from display
6175 tables? */
6176 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6177 it->c = bidi_mirror_char (it->c);
6178 /* Map via display table or translate control characters.
6179 IT->c, IT->len etc. have been set to the next character by
6180 the function call above. If we have a display table, and it
6181 contains an entry for IT->c, translate it. Don't do this if
6182 IT->c itself comes from a display table, otherwise we could
6183 end up in an infinite recursion. (An alternative could be to
6184 count the recursion depth of this function and signal an
6185 error when a certain maximum depth is reached.) Is it worth
6186 it? */
6187 if (success_p && it->dpvec == NULL)
6189 Lisp_Object dv;
6190 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6191 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6192 nbsp_or_shy = char_is_other;
6193 int c = it->c; /* This is the character to display. */
6195 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6197 xassert (SINGLE_BYTE_CHAR_P (c));
6198 if (unibyte_display_via_language_environment)
6200 c = DECODE_CHAR (unibyte, c);
6201 if (c < 0)
6202 c = BYTE8_TO_CHAR (it->c);
6204 else
6205 c = BYTE8_TO_CHAR (it->c);
6208 if (it->dp
6209 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6210 VECTORP (dv)))
6212 struct Lisp_Vector *v = XVECTOR (dv);
6214 /* Return the first character from the display table
6215 entry, if not empty. If empty, don't display the
6216 current character. */
6217 if (v->header.size)
6219 it->dpvec_char_len = it->len;
6220 it->dpvec = v->contents;
6221 it->dpend = v->contents + v->header.size;
6222 it->current.dpvec_index = 0;
6223 it->dpvec_face_id = -1;
6224 it->saved_face_id = it->face_id;
6225 it->method = GET_FROM_DISPLAY_VECTOR;
6226 it->ellipsis_p = 0;
6228 else
6230 set_iterator_to_next (it, 0);
6232 goto get_next;
6235 if (! NILP (lookup_glyphless_char_display (c, it)))
6237 if (it->what == IT_GLYPHLESS)
6238 goto done;
6239 /* Don't display this character. */
6240 set_iterator_to_next (it, 0);
6241 goto get_next;
6244 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6245 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6246 : c == 0xAD ? char_is_soft_hyphen
6247 : char_is_other);
6249 /* Translate control characters into `\003' or `^C' form.
6250 Control characters coming from a display table entry are
6251 currently not translated because we use IT->dpvec to hold
6252 the translation. This could easily be changed but I
6253 don't believe that it is worth doing.
6255 NBSP and SOFT-HYPEN are property translated too.
6257 Non-printable characters and raw-byte characters are also
6258 translated to octal form. */
6259 if (((c < ' ' || c == 127) /* ASCII control chars */
6260 ? (it->area != TEXT_AREA
6261 /* In mode line, treat \n, \t like other crl chars. */
6262 || (c != '\t'
6263 && it->glyph_row
6264 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6265 || (c != '\n' && c != '\t'))
6266 : (nbsp_or_shy
6267 || CHAR_BYTE8_P (c)
6268 || ! CHAR_PRINTABLE_P (c))))
6270 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6271 or a non-printable character which must be displayed
6272 either as '\003' or as `^C' where the '\\' and '^'
6273 can be defined in the display table. Fill
6274 IT->ctl_chars with glyphs for what we have to
6275 display. Then, set IT->dpvec to these glyphs. */
6276 Lisp_Object gc;
6277 int ctl_len;
6278 int face_id;
6279 EMACS_INT lface_id = 0;
6280 int escape_glyph;
6282 /* Handle control characters with ^. */
6284 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6286 int g;
6288 g = '^'; /* default glyph for Control */
6289 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6290 if (it->dp
6291 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6292 && GLYPH_CODE_CHAR_VALID_P (gc))
6294 g = GLYPH_CODE_CHAR (gc);
6295 lface_id = GLYPH_CODE_FACE (gc);
6297 if (lface_id)
6299 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6301 else if (it->f == last_escape_glyph_frame
6302 && it->face_id == last_escape_glyph_face_id)
6304 face_id = last_escape_glyph_merged_face_id;
6306 else
6308 /* Merge the escape-glyph face into the current face. */
6309 face_id = merge_faces (it->f, Qescape_glyph, 0,
6310 it->face_id);
6311 last_escape_glyph_frame = it->f;
6312 last_escape_glyph_face_id = it->face_id;
6313 last_escape_glyph_merged_face_id = face_id;
6316 XSETINT (it->ctl_chars[0], g);
6317 XSETINT (it->ctl_chars[1], c ^ 0100);
6318 ctl_len = 2;
6319 goto display_control;
6322 /* Handle non-break space in the mode where it only gets
6323 highlighting. */
6325 if (EQ (Vnobreak_char_display, Qt)
6326 && nbsp_or_shy == char_is_nbsp)
6328 /* Merge the no-break-space face into the current face. */
6329 face_id = merge_faces (it->f, Qnobreak_space, 0,
6330 it->face_id);
6332 c = ' ';
6333 XSETINT (it->ctl_chars[0], ' ');
6334 ctl_len = 1;
6335 goto display_control;
6338 /* Handle sequences that start with the "escape glyph". */
6340 /* the default escape glyph is \. */
6341 escape_glyph = '\\';
6343 if (it->dp
6344 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6345 && GLYPH_CODE_CHAR_VALID_P (gc))
6347 escape_glyph = GLYPH_CODE_CHAR (gc);
6348 lface_id = GLYPH_CODE_FACE (gc);
6350 if (lface_id)
6352 /* The display table specified a face.
6353 Merge it into face_id and also into escape_glyph. */
6354 face_id = merge_faces (it->f, Qt, lface_id,
6355 it->face_id);
6357 else if (it->f == last_escape_glyph_frame
6358 && it->face_id == last_escape_glyph_face_id)
6360 face_id = last_escape_glyph_merged_face_id;
6362 else
6364 /* Merge the escape-glyph face into the current face. */
6365 face_id = merge_faces (it->f, Qescape_glyph, 0,
6366 it->face_id);
6367 last_escape_glyph_frame = it->f;
6368 last_escape_glyph_face_id = it->face_id;
6369 last_escape_glyph_merged_face_id = face_id;
6372 /* Handle soft hyphens in the mode where they only get
6373 highlighting. */
6375 if (EQ (Vnobreak_char_display, Qt)
6376 && nbsp_or_shy == char_is_soft_hyphen)
6378 XSETINT (it->ctl_chars[0], '-');
6379 ctl_len = 1;
6380 goto display_control;
6383 /* Handle non-break space and soft hyphen
6384 with the escape glyph. */
6386 if (nbsp_or_shy)
6388 XSETINT (it->ctl_chars[0], escape_glyph);
6389 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6390 XSETINT (it->ctl_chars[1], c);
6391 ctl_len = 2;
6392 goto display_control;
6396 char str[10];
6397 int len, i;
6399 if (CHAR_BYTE8_P (c))
6400 /* Display \200 instead of \17777600. */
6401 c = CHAR_TO_BYTE8 (c);
6402 len = sprintf (str, "%03o", c);
6404 XSETINT (it->ctl_chars[0], escape_glyph);
6405 for (i = 0; i < len; i++)
6406 XSETINT (it->ctl_chars[i + 1], str[i]);
6407 ctl_len = len + 1;
6410 display_control:
6411 /* Set up IT->dpvec and return first character from it. */
6412 it->dpvec_char_len = it->len;
6413 it->dpvec = it->ctl_chars;
6414 it->dpend = it->dpvec + ctl_len;
6415 it->current.dpvec_index = 0;
6416 it->dpvec_face_id = face_id;
6417 it->saved_face_id = it->face_id;
6418 it->method = GET_FROM_DISPLAY_VECTOR;
6419 it->ellipsis_p = 0;
6420 goto get_next;
6422 it->char_to_display = c;
6424 else if (success_p)
6426 it->char_to_display = it->c;
6430 /* Adjust face id for a multibyte character. There are no multibyte
6431 character in unibyte text. */
6432 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6433 && it->multibyte_p
6434 && success_p
6435 && FRAME_WINDOW_P (it->f))
6437 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6439 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6441 /* Automatic composition with glyph-string. */
6442 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6444 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6446 else
6448 EMACS_INT pos = (it->s ? -1
6449 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6450 : IT_CHARPOS (*it));
6451 int c;
6453 if (it->what == IT_CHARACTER)
6454 c = it->char_to_display;
6455 else
6457 struct composition *cmp = composition_table[it->cmp_it.id];
6458 int i;
6460 c = ' ';
6461 for (i = 0; i < cmp->glyph_len; i++)
6462 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6463 break;
6465 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6469 done:
6470 /* Is this character the last one of a run of characters with
6471 box? If yes, set IT->end_of_box_run_p to 1. */
6472 if (it->face_box_p
6473 && it->s == NULL)
6475 if (it->method == GET_FROM_STRING && it->sp)
6477 int face_id = underlying_face_id (it);
6478 struct face *face = FACE_FROM_ID (it->f, face_id);
6480 if (face)
6482 if (face->box == FACE_NO_BOX)
6484 /* If the box comes from face properties in a
6485 display string, check faces in that string. */
6486 int string_face_id = face_after_it_pos (it);
6487 it->end_of_box_run_p
6488 = (FACE_FROM_ID (it->f, string_face_id)->box
6489 == FACE_NO_BOX);
6491 /* Otherwise, the box comes from the underlying face.
6492 If this is the last string character displayed, check
6493 the next buffer location. */
6494 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6495 && (it->current.overlay_string_index
6496 == it->n_overlay_strings - 1))
6498 EMACS_INT ignore;
6499 int next_face_id;
6500 struct text_pos pos = it->current.pos;
6501 INC_TEXT_POS (pos, it->multibyte_p);
6503 next_face_id = face_at_buffer_position
6504 (it->w, CHARPOS (pos), it->region_beg_charpos,
6505 it->region_end_charpos, &ignore,
6506 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6507 -1);
6508 it->end_of_box_run_p
6509 = (FACE_FROM_ID (it->f, next_face_id)->box
6510 == FACE_NO_BOX);
6514 else
6516 int face_id = face_after_it_pos (it);
6517 it->end_of_box_run_p
6518 = (face_id != it->face_id
6519 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6523 /* Value is 0 if end of buffer or string reached. */
6524 return success_p;
6528 /* Move IT to the next display element.
6530 RESEAT_P non-zero means if called on a newline in buffer text,
6531 skip to the next visible line start.
6533 Functions get_next_display_element and set_iterator_to_next are
6534 separate because I find this arrangement easier to handle than a
6535 get_next_display_element function that also increments IT's
6536 position. The way it is we can first look at an iterator's current
6537 display element, decide whether it fits on a line, and if it does,
6538 increment the iterator position. The other way around we probably
6539 would either need a flag indicating whether the iterator has to be
6540 incremented the next time, or we would have to implement a
6541 decrement position function which would not be easy to write. */
6543 void
6544 set_iterator_to_next (struct it *it, int reseat_p)
6546 /* Reset flags indicating start and end of a sequence of characters
6547 with box. Reset them at the start of this function because
6548 moving the iterator to a new position might set them. */
6549 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6551 switch (it->method)
6553 case GET_FROM_BUFFER:
6554 /* The current display element of IT is a character from
6555 current_buffer. Advance in the buffer, and maybe skip over
6556 invisible lines that are so because of selective display. */
6557 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6558 reseat_at_next_visible_line_start (it, 0);
6559 else if (it->cmp_it.id >= 0)
6561 /* We are currently getting glyphs from a composition. */
6562 int i;
6564 if (! it->bidi_p)
6566 IT_CHARPOS (*it) += it->cmp_it.nchars;
6567 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6568 if (it->cmp_it.to < it->cmp_it.nglyphs)
6570 it->cmp_it.from = it->cmp_it.to;
6572 else
6574 it->cmp_it.id = -1;
6575 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6576 IT_BYTEPOS (*it),
6577 it->end_charpos, Qnil);
6580 else if (! it->cmp_it.reversed_p)
6582 /* Composition created while scanning forward. */
6583 /* Update IT's char/byte positions to point to the first
6584 character of the next grapheme cluster, or to the
6585 character visually after the current composition. */
6586 for (i = 0; i < it->cmp_it.nchars; i++)
6587 bidi_move_to_visually_next (&it->bidi_it);
6588 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6589 IT_CHARPOS (*it) = it->bidi_it.charpos;
6591 if (it->cmp_it.to < it->cmp_it.nglyphs)
6593 /* Proceed to the next grapheme cluster. */
6594 it->cmp_it.from = it->cmp_it.to;
6596 else
6598 /* No more grapheme clusters in this composition.
6599 Find the next stop position. */
6600 EMACS_INT stop = it->end_charpos;
6601 if (it->bidi_it.scan_dir < 0)
6602 /* Now we are scanning backward and don't know
6603 where to stop. */
6604 stop = -1;
6605 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6606 IT_BYTEPOS (*it), stop, Qnil);
6609 else
6611 /* Composition created while scanning backward. */
6612 /* Update IT's char/byte positions to point to the last
6613 character of the previous grapheme cluster, or the
6614 character visually after the current composition. */
6615 for (i = 0; i < it->cmp_it.nchars; i++)
6616 bidi_move_to_visually_next (&it->bidi_it);
6617 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6618 IT_CHARPOS (*it) = it->bidi_it.charpos;
6619 if (it->cmp_it.from > 0)
6621 /* Proceed to the previous grapheme cluster. */
6622 it->cmp_it.to = it->cmp_it.from;
6624 else
6626 /* No more grapheme clusters in this composition.
6627 Find the next stop position. */
6628 EMACS_INT stop = it->end_charpos;
6629 if (it->bidi_it.scan_dir < 0)
6630 /* Now we are scanning backward and don't know
6631 where to stop. */
6632 stop = -1;
6633 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6634 IT_BYTEPOS (*it), stop, Qnil);
6638 else
6640 xassert (it->len != 0);
6642 if (!it->bidi_p)
6644 IT_BYTEPOS (*it) += it->len;
6645 IT_CHARPOS (*it) += 1;
6647 else
6649 int prev_scan_dir = it->bidi_it.scan_dir;
6650 /* If this is a new paragraph, determine its base
6651 direction (a.k.a. its base embedding level). */
6652 if (it->bidi_it.new_paragraph)
6653 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6654 bidi_move_to_visually_next (&it->bidi_it);
6655 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6656 IT_CHARPOS (*it) = it->bidi_it.charpos;
6657 if (prev_scan_dir != it->bidi_it.scan_dir)
6659 /* As the scan direction was changed, we must
6660 re-compute the stop position for composition. */
6661 EMACS_INT stop = it->end_charpos;
6662 if (it->bidi_it.scan_dir < 0)
6663 stop = -1;
6664 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6665 IT_BYTEPOS (*it), stop, Qnil);
6668 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6670 break;
6672 case GET_FROM_C_STRING:
6673 /* Current display element of IT is from a C string. */
6674 if (!it->bidi_p
6675 /* If the string position is beyond string's end, it means
6676 next_element_from_c_string is padding the string with
6677 blanks, in which case we bypass the bidi iterator,
6678 because it cannot deal with such virtual characters. */
6679 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6681 IT_BYTEPOS (*it) += it->len;
6682 IT_CHARPOS (*it) += 1;
6684 else
6686 bidi_move_to_visually_next (&it->bidi_it);
6687 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6688 IT_CHARPOS (*it) = it->bidi_it.charpos;
6690 break;
6692 case GET_FROM_DISPLAY_VECTOR:
6693 /* Current display element of IT is from a display table entry.
6694 Advance in the display table definition. Reset it to null if
6695 end reached, and continue with characters from buffers/
6696 strings. */
6697 ++it->current.dpvec_index;
6699 /* Restore face of the iterator to what they were before the
6700 display vector entry (these entries may contain faces). */
6701 it->face_id = it->saved_face_id;
6703 if (it->dpvec + it->current.dpvec_index == it->dpend)
6705 int recheck_faces = it->ellipsis_p;
6707 if (it->s)
6708 it->method = GET_FROM_C_STRING;
6709 else if (STRINGP (it->string))
6710 it->method = GET_FROM_STRING;
6711 else
6713 it->method = GET_FROM_BUFFER;
6714 it->object = it->w->buffer;
6717 it->dpvec = NULL;
6718 it->current.dpvec_index = -1;
6720 /* Skip over characters which were displayed via IT->dpvec. */
6721 if (it->dpvec_char_len < 0)
6722 reseat_at_next_visible_line_start (it, 1);
6723 else if (it->dpvec_char_len > 0)
6725 if (it->method == GET_FROM_STRING
6726 && it->n_overlay_strings > 0)
6727 it->ignore_overlay_strings_at_pos_p = 1;
6728 it->len = it->dpvec_char_len;
6729 set_iterator_to_next (it, reseat_p);
6732 /* Maybe recheck faces after display vector */
6733 if (recheck_faces)
6734 it->stop_charpos = IT_CHARPOS (*it);
6736 break;
6738 case GET_FROM_STRING:
6739 /* Current display element is a character from a Lisp string. */
6740 xassert (it->s == NULL && STRINGP (it->string));
6741 if (it->cmp_it.id >= 0)
6743 int i;
6745 if (! it->bidi_p)
6747 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6748 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6749 if (it->cmp_it.to < it->cmp_it.nglyphs)
6750 it->cmp_it.from = it->cmp_it.to;
6751 else
6753 it->cmp_it.id = -1;
6754 composition_compute_stop_pos (&it->cmp_it,
6755 IT_STRING_CHARPOS (*it),
6756 IT_STRING_BYTEPOS (*it),
6757 it->end_charpos, it->string);
6760 else if (! it->cmp_it.reversed_p)
6762 for (i = 0; i < it->cmp_it.nchars; i++)
6763 bidi_move_to_visually_next (&it->bidi_it);
6764 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6765 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6767 if (it->cmp_it.to < it->cmp_it.nglyphs)
6768 it->cmp_it.from = it->cmp_it.to;
6769 else
6771 EMACS_INT stop = it->end_charpos;
6772 if (it->bidi_it.scan_dir < 0)
6773 stop = -1;
6774 composition_compute_stop_pos (&it->cmp_it,
6775 IT_STRING_CHARPOS (*it),
6776 IT_STRING_BYTEPOS (*it), stop,
6777 it->string);
6780 else
6782 for (i = 0; i < it->cmp_it.nchars; i++)
6783 bidi_move_to_visually_next (&it->bidi_it);
6784 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6785 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6786 if (it->cmp_it.from > 0)
6787 it->cmp_it.to = it->cmp_it.from;
6788 else
6790 EMACS_INT stop = it->end_charpos;
6791 if (it->bidi_it.scan_dir < 0)
6792 stop = -1;
6793 composition_compute_stop_pos (&it->cmp_it,
6794 IT_STRING_CHARPOS (*it),
6795 IT_STRING_BYTEPOS (*it), stop,
6796 it->string);
6800 else
6802 if (!it->bidi_p
6803 /* If the string position is beyond string's end, it
6804 means next_element_from_string is padding the string
6805 with blanks, in which case we bypass the bidi
6806 iterator, because it cannot deal with such virtual
6807 characters. */
6808 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6810 IT_STRING_BYTEPOS (*it) += it->len;
6811 IT_STRING_CHARPOS (*it) += 1;
6813 else
6815 int prev_scan_dir = it->bidi_it.scan_dir;
6817 bidi_move_to_visually_next (&it->bidi_it);
6818 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6819 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6820 if (prev_scan_dir != it->bidi_it.scan_dir)
6822 EMACS_INT stop = it->end_charpos;
6824 if (it->bidi_it.scan_dir < 0)
6825 stop = -1;
6826 composition_compute_stop_pos (&it->cmp_it,
6827 IT_STRING_CHARPOS (*it),
6828 IT_STRING_BYTEPOS (*it), stop,
6829 it->string);
6834 consider_string_end:
6836 if (it->current.overlay_string_index >= 0)
6838 /* IT->string is an overlay string. Advance to the
6839 next, if there is one. */
6840 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6842 it->ellipsis_p = 0;
6843 next_overlay_string (it);
6844 if (it->ellipsis_p)
6845 setup_for_ellipsis (it, 0);
6848 else
6850 /* IT->string is not an overlay string. If we reached
6851 its end, and there is something on IT->stack, proceed
6852 with what is on the stack. This can be either another
6853 string, this time an overlay string, or a buffer. */
6854 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6855 && it->sp > 0)
6857 pop_it (it);
6858 if (it->method == GET_FROM_STRING)
6859 goto consider_string_end;
6862 break;
6864 case GET_FROM_IMAGE:
6865 case GET_FROM_STRETCH:
6866 #ifdef HAVE_XWIDGETS
6867 case GET_FROM_XWIDGET:
6869 /* The position etc with which we have to proceed are on
6870 the stack. The position may be at the end of a string,
6871 if the `display' property takes up the whole string. */
6872 xassert (it->sp > 0);
6873 pop_it (it);
6874 if (it->method == GET_FROM_STRING)
6875 goto consider_string_end;
6876 break;
6877 #endif
6878 default:
6879 /* There are no other methods defined, so this should be a bug. */
6880 abort ();
6883 xassert (it->method != GET_FROM_STRING
6884 || (STRINGP (it->string)
6885 && IT_STRING_CHARPOS (*it) >= 0));
6888 /* Load IT's display element fields with information about the next
6889 display element which comes from a display table entry or from the
6890 result of translating a control character to one of the forms `^C'
6891 or `\003'.
6893 IT->dpvec holds the glyphs to return as characters.
6894 IT->saved_face_id holds the face id before the display vector--it
6895 is restored into IT->face_id in set_iterator_to_next. */
6897 static int
6898 next_element_from_display_vector (struct it *it)
6900 Lisp_Object gc;
6902 /* Precondition. */
6903 xassert (it->dpvec && it->current.dpvec_index >= 0);
6905 it->face_id = it->saved_face_id;
6907 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6908 That seemed totally bogus - so I changed it... */
6909 gc = it->dpvec[it->current.dpvec_index];
6911 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6913 it->c = GLYPH_CODE_CHAR (gc);
6914 it->len = CHAR_BYTES (it->c);
6916 /* The entry may contain a face id to use. Such a face id is
6917 the id of a Lisp face, not a realized face. A face id of
6918 zero means no face is specified. */
6919 if (it->dpvec_face_id >= 0)
6920 it->face_id = it->dpvec_face_id;
6921 else
6923 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
6924 if (lface_id > 0)
6925 it->face_id = merge_faces (it->f, Qt, lface_id,
6926 it->saved_face_id);
6929 else
6930 /* Display table entry is invalid. Return a space. */
6931 it->c = ' ', it->len = 1;
6933 /* Don't change position and object of the iterator here. They are
6934 still the values of the character that had this display table
6935 entry or was translated, and that's what we want. */
6936 it->what = IT_CHARACTER;
6937 return 1;
6940 /* Get the first element of string/buffer in the visual order, after
6941 being reseated to a new position in a string or a buffer. */
6942 static void
6943 get_visually_first_element (struct it *it)
6945 int string_p = STRINGP (it->string) || it->s;
6946 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6947 EMACS_INT bob = (string_p ? 0 : BEGV);
6949 if (STRINGP (it->string))
6951 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6952 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6954 else
6956 it->bidi_it.charpos = IT_CHARPOS (*it);
6957 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6960 if (it->bidi_it.charpos == eob)
6962 /* Nothing to do, but reset the FIRST_ELT flag, like
6963 bidi_paragraph_init does, because we are not going to
6964 call it. */
6965 it->bidi_it.first_elt = 0;
6967 else if (it->bidi_it.charpos == bob
6968 || (!string_p
6969 /* FIXME: Should support all Unicode line separators. */
6970 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6971 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6973 /* If we are at the beginning of a line/string, we can produce
6974 the next element right away. */
6975 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6976 bidi_move_to_visually_next (&it->bidi_it);
6978 else
6980 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6982 /* We need to prime the bidi iterator starting at the line's or
6983 string's beginning, before we will be able to produce the
6984 next element. */
6985 if (string_p)
6986 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6987 else
6989 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6990 -1);
6991 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6993 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6996 /* Now return to buffer/string position where we were asked
6997 to get the next display element, and produce that. */
6998 bidi_move_to_visually_next (&it->bidi_it);
7000 while (it->bidi_it.bytepos != orig_bytepos
7001 && it->bidi_it.charpos < eob);
7004 /* Adjust IT's position information to where we ended up. */
7005 if (STRINGP (it->string))
7007 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7008 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7010 else
7012 IT_CHARPOS (*it) = it->bidi_it.charpos;
7013 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7016 if (STRINGP (it->string) || !it->s)
7018 EMACS_INT stop, charpos, bytepos;
7020 if (STRINGP (it->string))
7022 xassert (!it->s);
7023 stop = SCHARS (it->string);
7024 if (stop > it->end_charpos)
7025 stop = it->end_charpos;
7026 charpos = IT_STRING_CHARPOS (*it);
7027 bytepos = IT_STRING_BYTEPOS (*it);
7029 else
7031 stop = it->end_charpos;
7032 charpos = IT_CHARPOS (*it);
7033 bytepos = IT_BYTEPOS (*it);
7035 if (it->bidi_it.scan_dir < 0)
7036 stop = -1;
7037 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7038 it->string);
7042 /* Load IT with the next display element from Lisp string IT->string.
7043 IT->current.string_pos is the current position within the string.
7044 If IT->current.overlay_string_index >= 0, the Lisp string is an
7045 overlay string. */
7047 static int
7048 next_element_from_string (struct it *it)
7050 struct text_pos position;
7052 xassert (STRINGP (it->string));
7053 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7054 xassert (IT_STRING_CHARPOS (*it) >= 0);
7055 position = it->current.string_pos;
7057 /* With bidi reordering, the character to display might not be the
7058 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7059 that we were reseat()ed to a new string, whose paragraph
7060 direction is not known. */
7061 if (it->bidi_p && it->bidi_it.first_elt)
7063 get_visually_first_element (it);
7064 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7067 /* Time to check for invisible text? */
7068 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7070 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7072 if (!(!it->bidi_p
7073 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7074 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7076 /* With bidi non-linear iteration, we could find
7077 ourselves far beyond the last computed stop_charpos,
7078 with several other stop positions in between that we
7079 missed. Scan them all now, in buffer's logical
7080 order, until we find and handle the last stop_charpos
7081 that precedes our current position. */
7082 handle_stop_backwards (it, it->stop_charpos);
7083 return GET_NEXT_DISPLAY_ELEMENT (it);
7085 else
7087 if (it->bidi_p)
7089 /* Take note of the stop position we just moved
7090 across, for when we will move back across it. */
7091 it->prev_stop = it->stop_charpos;
7092 /* If we are at base paragraph embedding level, take
7093 note of the last stop position seen at this
7094 level. */
7095 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7096 it->base_level_stop = it->stop_charpos;
7098 handle_stop (it);
7100 /* Since a handler may have changed IT->method, we must
7101 recurse here. */
7102 return GET_NEXT_DISPLAY_ELEMENT (it);
7105 else if (it->bidi_p
7106 /* If we are before prev_stop, we may have overstepped
7107 on our way backwards a stop_pos, and if so, we need
7108 to handle that stop_pos. */
7109 && IT_STRING_CHARPOS (*it) < it->prev_stop
7110 /* We can sometimes back up for reasons that have nothing
7111 to do with bidi reordering. E.g., compositions. The
7112 code below is only needed when we are above the base
7113 embedding level, so test for that explicitly. */
7114 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7116 /* If we lost track of base_level_stop, we have no better
7117 place for handle_stop_backwards to start from than string
7118 beginning. This happens, e.g., when we were reseated to
7119 the previous screenful of text by vertical-motion. */
7120 if (it->base_level_stop <= 0
7121 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7122 it->base_level_stop = 0;
7123 handle_stop_backwards (it, it->base_level_stop);
7124 return GET_NEXT_DISPLAY_ELEMENT (it);
7128 if (it->current.overlay_string_index >= 0)
7130 /* Get the next character from an overlay string. In overlay
7131 strings, There is no field width or padding with spaces to
7132 do. */
7133 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7135 it->what = IT_EOB;
7136 return 0;
7138 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7139 IT_STRING_BYTEPOS (*it),
7140 it->bidi_it.scan_dir < 0
7141 ? -1
7142 : SCHARS (it->string))
7143 && next_element_from_composition (it))
7145 return 1;
7147 else if (STRING_MULTIBYTE (it->string))
7149 const unsigned char *s = (SDATA (it->string)
7150 + IT_STRING_BYTEPOS (*it));
7151 it->c = string_char_and_length (s, &it->len);
7153 else
7155 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7156 it->len = 1;
7159 else
7161 /* Get the next character from a Lisp string that is not an
7162 overlay string. Such strings come from the mode line, for
7163 example. We may have to pad with spaces, or truncate the
7164 string. See also next_element_from_c_string. */
7165 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7167 it->what = IT_EOB;
7168 return 0;
7170 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7172 /* Pad with spaces. */
7173 it->c = ' ', it->len = 1;
7174 CHARPOS (position) = BYTEPOS (position) = -1;
7176 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7177 IT_STRING_BYTEPOS (*it),
7178 it->bidi_it.scan_dir < 0
7179 ? -1
7180 : it->string_nchars)
7181 && next_element_from_composition (it))
7183 return 1;
7185 else if (STRING_MULTIBYTE (it->string))
7187 const unsigned char *s = (SDATA (it->string)
7188 + IT_STRING_BYTEPOS (*it));
7189 it->c = string_char_and_length (s, &it->len);
7191 else
7193 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7194 it->len = 1;
7198 /* Record what we have and where it came from. */
7199 it->what = IT_CHARACTER;
7200 it->object = it->string;
7201 it->position = position;
7202 return 1;
7206 /* Load IT with next display element from C string IT->s.
7207 IT->string_nchars is the maximum number of characters to return
7208 from the string. IT->end_charpos may be greater than
7209 IT->string_nchars when this function is called, in which case we
7210 may have to return padding spaces. Value is zero if end of string
7211 reached, including padding spaces. */
7213 static int
7214 next_element_from_c_string (struct it *it)
7216 int success_p = 1;
7218 xassert (it->s);
7219 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7220 it->what = IT_CHARACTER;
7221 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7222 it->object = Qnil;
7224 /* With bidi reordering, the character to display might not be the
7225 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7226 we were reseated to a new string, whose paragraph direction is
7227 not known. */
7228 if (it->bidi_p && it->bidi_it.first_elt)
7229 get_visually_first_element (it);
7231 /* IT's position can be greater than IT->string_nchars in case a
7232 field width or precision has been specified when the iterator was
7233 initialized. */
7234 if (IT_CHARPOS (*it) >= it->end_charpos)
7236 /* End of the game. */
7237 it->what = IT_EOB;
7238 success_p = 0;
7240 else if (IT_CHARPOS (*it) >= it->string_nchars)
7242 /* Pad with spaces. */
7243 it->c = ' ', it->len = 1;
7244 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7246 else if (it->multibyte_p)
7247 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7248 else
7249 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7251 return success_p;
7255 /* Set up IT to return characters from an ellipsis, if appropriate.
7256 The definition of the ellipsis glyphs may come from a display table
7257 entry. This function fills IT with the first glyph from the
7258 ellipsis if an ellipsis is to be displayed. */
7260 static int
7261 next_element_from_ellipsis (struct it *it)
7263 if (it->selective_display_ellipsis_p)
7264 setup_for_ellipsis (it, it->len);
7265 else
7267 /* The face at the current position may be different from the
7268 face we find after the invisible text. Remember what it
7269 was in IT->saved_face_id, and signal that it's there by
7270 setting face_before_selective_p. */
7271 it->saved_face_id = it->face_id;
7272 it->method = GET_FROM_BUFFER;
7273 it->object = it->w->buffer;
7274 reseat_at_next_visible_line_start (it, 1);
7275 it->face_before_selective_p = 1;
7278 return GET_NEXT_DISPLAY_ELEMENT (it);
7282 /* Deliver an image display element. The iterator IT is already
7283 filled with image information (done in handle_display_prop). Value
7284 is always 1. */
7287 static int
7288 next_element_from_image (struct it *it)
7290 it->what = IT_IMAGE;
7291 it->ignore_overlay_strings_at_pos_p = 0;
7292 return 1;
7295 #ifdef HAVE_XWIDGETS
7296 /* im not sure about this FIXME JAVE*/
7297 static int
7298 next_element_from_xwidget (struct it *it)
7300 it->what = IT_XWIDGET;
7301 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7302 //this is shaky because why do we set "what" if we dont set the other parts??
7303 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7304 return 1;
7306 #endif
7309 /* Fill iterator IT with next display element from a stretch glyph
7310 property. IT->object is the value of the text property. Value is
7311 always 1. */
7313 static int
7314 next_element_from_stretch (struct it *it)
7316 it->what = IT_STRETCH;
7317 return 1;
7320 /* Scan backwards from IT's current position until we find a stop
7321 position, or until BEGV. This is called when we find ourself
7322 before both the last known prev_stop and base_level_stop while
7323 reordering bidirectional text. */
7325 static void
7326 compute_stop_pos_backwards (struct it *it)
7328 const int SCAN_BACK_LIMIT = 1000;
7329 struct text_pos pos;
7330 struct display_pos save_current = it->current;
7331 struct text_pos save_position = it->position;
7332 EMACS_INT charpos = IT_CHARPOS (*it);
7333 EMACS_INT where_we_are = charpos;
7334 EMACS_INT save_stop_pos = it->stop_charpos;
7335 EMACS_INT save_end_pos = it->end_charpos;
7337 xassert (NILP (it->string) && !it->s);
7338 xassert (it->bidi_p);
7339 it->bidi_p = 0;
7342 it->end_charpos = min (charpos + 1, ZV);
7343 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7344 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7345 reseat_1 (it, pos, 0);
7346 compute_stop_pos (it);
7347 /* We must advance forward, right? */
7348 if (it->stop_charpos <= charpos)
7349 abort ();
7351 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7353 if (it->stop_charpos <= where_we_are)
7354 it->prev_stop = it->stop_charpos;
7355 else
7356 it->prev_stop = BEGV;
7357 it->bidi_p = 1;
7358 it->current = save_current;
7359 it->position = save_position;
7360 it->stop_charpos = save_stop_pos;
7361 it->end_charpos = save_end_pos;
7364 /* Scan forward from CHARPOS in the current buffer/string, until we
7365 find a stop position > current IT's position. Then handle the stop
7366 position before that. This is called when we bump into a stop
7367 position while reordering bidirectional text. CHARPOS should be
7368 the last previously processed stop_pos (or BEGV/0, if none were
7369 processed yet) whose position is less that IT's current
7370 position. */
7372 static void
7373 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7375 int bufp = !STRINGP (it->string);
7376 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7377 struct display_pos save_current = it->current;
7378 struct text_pos save_position = it->position;
7379 struct text_pos pos1;
7380 EMACS_INT next_stop;
7382 /* Scan in strict logical order. */
7383 xassert (it->bidi_p);
7384 it->bidi_p = 0;
7387 it->prev_stop = charpos;
7388 if (bufp)
7390 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7391 reseat_1 (it, pos1, 0);
7393 else
7394 it->current.string_pos = string_pos (charpos, it->string);
7395 compute_stop_pos (it);
7396 /* We must advance forward, right? */
7397 if (it->stop_charpos <= it->prev_stop)
7398 abort ();
7399 charpos = it->stop_charpos;
7401 while (charpos <= where_we_are);
7403 it->bidi_p = 1;
7404 it->current = save_current;
7405 it->position = save_position;
7406 next_stop = it->stop_charpos;
7407 it->stop_charpos = it->prev_stop;
7408 handle_stop (it);
7409 it->stop_charpos = next_stop;
7412 /* Load IT with the next display element from current_buffer. Value
7413 is zero if end of buffer reached. IT->stop_charpos is the next
7414 position at which to stop and check for text properties or buffer
7415 end. */
7417 static int
7418 next_element_from_buffer (struct it *it)
7420 int success_p = 1;
7422 xassert (IT_CHARPOS (*it) >= BEGV);
7423 xassert (NILP (it->string) && !it->s);
7424 xassert (!it->bidi_p
7425 || (EQ (it->bidi_it.string.lstring, Qnil)
7426 && it->bidi_it.string.s == NULL));
7428 /* With bidi reordering, the character to display might not be the
7429 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7430 we were reseat()ed to a new buffer position, which is potentially
7431 a different paragraph. */
7432 if (it->bidi_p && it->bidi_it.first_elt)
7434 get_visually_first_element (it);
7435 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7438 if (IT_CHARPOS (*it) >= it->stop_charpos)
7440 if (IT_CHARPOS (*it) >= it->end_charpos)
7442 int overlay_strings_follow_p;
7444 /* End of the game, except when overlay strings follow that
7445 haven't been returned yet. */
7446 if (it->overlay_strings_at_end_processed_p)
7447 overlay_strings_follow_p = 0;
7448 else
7450 it->overlay_strings_at_end_processed_p = 1;
7451 overlay_strings_follow_p = get_overlay_strings (it, 0);
7454 if (overlay_strings_follow_p)
7455 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7456 else
7458 it->what = IT_EOB;
7459 it->position = it->current.pos;
7460 success_p = 0;
7463 else if (!(!it->bidi_p
7464 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7465 || IT_CHARPOS (*it) == it->stop_charpos))
7467 /* With bidi non-linear iteration, we could find ourselves
7468 far beyond the last computed stop_charpos, with several
7469 other stop positions in between that we missed. Scan
7470 them all now, in buffer's logical order, until we find
7471 and handle the last stop_charpos that precedes our
7472 current position. */
7473 handle_stop_backwards (it, it->stop_charpos);
7474 return GET_NEXT_DISPLAY_ELEMENT (it);
7476 else
7478 if (it->bidi_p)
7480 /* Take note of the stop position we just moved across,
7481 for when we will move back across it. */
7482 it->prev_stop = it->stop_charpos;
7483 /* If we are at base paragraph embedding level, take
7484 note of the last stop position seen at this
7485 level. */
7486 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7487 it->base_level_stop = it->stop_charpos;
7489 handle_stop (it);
7490 return GET_NEXT_DISPLAY_ELEMENT (it);
7493 else if (it->bidi_p
7494 /* If we are before prev_stop, we may have overstepped on
7495 our way backwards a stop_pos, and if so, we need to
7496 handle that stop_pos. */
7497 && IT_CHARPOS (*it) < it->prev_stop
7498 /* We can sometimes back up for reasons that have nothing
7499 to do with bidi reordering. E.g., compositions. The
7500 code below is only needed when we are above the base
7501 embedding level, so test for that explicitly. */
7502 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7504 if (it->base_level_stop <= 0
7505 || IT_CHARPOS (*it) < it->base_level_stop)
7507 /* If we lost track of base_level_stop, we need to find
7508 prev_stop by looking backwards. This happens, e.g., when
7509 we were reseated to the previous screenful of text by
7510 vertical-motion. */
7511 it->base_level_stop = BEGV;
7512 compute_stop_pos_backwards (it);
7513 handle_stop_backwards (it, it->prev_stop);
7515 else
7516 handle_stop_backwards (it, it->base_level_stop);
7517 return GET_NEXT_DISPLAY_ELEMENT (it);
7519 else
7521 /* No face changes, overlays etc. in sight, so just return a
7522 character from current_buffer. */
7523 unsigned char *p;
7524 EMACS_INT stop;
7526 /* Maybe run the redisplay end trigger hook. Performance note:
7527 This doesn't seem to cost measurable time. */
7528 if (it->redisplay_end_trigger_charpos
7529 && it->glyph_row
7530 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7531 run_redisplay_end_trigger_hook (it);
7533 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7534 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7535 stop)
7536 && next_element_from_composition (it))
7538 return 1;
7541 /* Get the next character, maybe multibyte. */
7542 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7543 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7544 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7545 else
7546 it->c = *p, it->len = 1;
7548 /* Record what we have and where it came from. */
7549 it->what = IT_CHARACTER;
7550 it->object = it->w->buffer;
7551 it->position = it->current.pos;
7553 /* Normally we return the character found above, except when we
7554 really want to return an ellipsis for selective display. */
7555 if (it->selective)
7557 if (it->c == '\n')
7559 /* A value of selective > 0 means hide lines indented more
7560 than that number of columns. */
7561 if (it->selective > 0
7562 && IT_CHARPOS (*it) + 1 < ZV
7563 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7564 IT_BYTEPOS (*it) + 1,
7565 it->selective))
7567 success_p = next_element_from_ellipsis (it);
7568 it->dpvec_char_len = -1;
7571 else if (it->c == '\r' && it->selective == -1)
7573 /* A value of selective == -1 means that everything from the
7574 CR to the end of the line is invisible, with maybe an
7575 ellipsis displayed for it. */
7576 success_p = next_element_from_ellipsis (it);
7577 it->dpvec_char_len = -1;
7582 /* Value is zero if end of buffer reached. */
7583 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7584 return success_p;
7588 /* Run the redisplay end trigger hook for IT. */
7590 static void
7591 run_redisplay_end_trigger_hook (struct it *it)
7593 Lisp_Object args[3];
7595 /* IT->glyph_row should be non-null, i.e. we should be actually
7596 displaying something, or otherwise we should not run the hook. */
7597 xassert (it->glyph_row);
7599 /* Set up hook arguments. */
7600 args[0] = Qredisplay_end_trigger_functions;
7601 args[1] = it->window;
7602 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7603 it->redisplay_end_trigger_charpos = 0;
7605 /* Since we are *trying* to run these functions, don't try to run
7606 them again, even if they get an error. */
7607 it->w->redisplay_end_trigger = Qnil;
7608 Frun_hook_with_args (3, args);
7610 /* Notice if it changed the face of the character we are on. */
7611 handle_face_prop (it);
7615 /* Deliver a composition display element. Unlike the other
7616 next_element_from_XXX, this function is not registered in the array
7617 get_next_element[]. It is called from next_element_from_buffer and
7618 next_element_from_string when necessary. */
7620 static int
7621 next_element_from_composition (struct it *it)
7623 it->what = IT_COMPOSITION;
7624 it->len = it->cmp_it.nbytes;
7625 if (STRINGP (it->string))
7627 if (it->c < 0)
7629 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7630 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7631 return 0;
7633 it->position = it->current.string_pos;
7634 it->object = it->string;
7635 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7636 IT_STRING_BYTEPOS (*it), it->string);
7638 else
7640 if (it->c < 0)
7642 IT_CHARPOS (*it) += it->cmp_it.nchars;
7643 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7644 if (it->bidi_p)
7646 if (it->bidi_it.new_paragraph)
7647 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7648 /* Resync the bidi iterator with IT's new position.
7649 FIXME: this doesn't support bidirectional text. */
7650 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7651 bidi_move_to_visually_next (&it->bidi_it);
7653 return 0;
7655 it->position = it->current.pos;
7656 it->object = it->w->buffer;
7657 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7658 IT_BYTEPOS (*it), Qnil);
7660 return 1;
7665 /***********************************************************************
7666 Moving an iterator without producing glyphs
7667 ***********************************************************************/
7669 /* Check if iterator is at a position corresponding to a valid buffer
7670 position after some move_it_ call. */
7672 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7673 ((it)->method == GET_FROM_STRING \
7674 ? IT_STRING_CHARPOS (*it) == 0 \
7675 : 1)
7678 /* Move iterator IT to a specified buffer or X position within one
7679 line on the display without producing glyphs.
7681 OP should be a bit mask including some or all of these bits:
7682 MOVE_TO_X: Stop upon reaching x-position TO_X.
7683 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7684 Regardless of OP's value, stop upon reaching the end of the display line.
7686 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7687 This means, in particular, that TO_X includes window's horizontal
7688 scroll amount.
7690 The return value has several possible values that
7691 say what condition caused the scan to stop:
7693 MOVE_POS_MATCH_OR_ZV
7694 - when TO_POS or ZV was reached.
7696 MOVE_X_REACHED
7697 -when TO_X was reached before TO_POS or ZV were reached.
7699 MOVE_LINE_CONTINUED
7700 - when we reached the end of the display area and the line must
7701 be continued.
7703 MOVE_LINE_TRUNCATED
7704 - when we reached the end of the display area and the line is
7705 truncated.
7707 MOVE_NEWLINE_OR_CR
7708 - when we stopped at a line end, i.e. a newline or a CR and selective
7709 display is on. */
7711 static enum move_it_result
7712 move_it_in_display_line_to (struct it *it,
7713 EMACS_INT to_charpos, int to_x,
7714 enum move_operation_enum op)
7716 enum move_it_result result = MOVE_UNDEFINED;
7717 struct glyph_row *saved_glyph_row;
7718 struct it wrap_it, atpos_it, atx_it, ppos_it;
7719 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7720 void *ppos_data = NULL;
7721 int may_wrap = 0;
7722 enum it_method prev_method = it->method;
7723 EMACS_INT prev_pos = IT_CHARPOS (*it);
7724 int saw_smaller_pos = prev_pos < to_charpos;
7726 /* Don't produce glyphs in produce_glyphs. */
7727 saved_glyph_row = it->glyph_row;
7728 it->glyph_row = NULL;
7730 /* Use wrap_it to save a copy of IT wherever a word wrap could
7731 occur. Use atpos_it to save a copy of IT at the desired buffer
7732 position, if found, so that we can scan ahead and check if the
7733 word later overshoots the window edge. Use atx_it similarly, for
7734 pixel positions. */
7735 wrap_it.sp = -1;
7736 atpos_it.sp = -1;
7737 atx_it.sp = -1;
7739 /* Use ppos_it under bidi reordering to save a copy of IT for the
7740 position > CHARPOS that is the closest to CHARPOS. We restore
7741 that position in IT when we have scanned the entire display line
7742 without finding a match for CHARPOS and all the character
7743 positions are greater than CHARPOS. */
7744 if (it->bidi_p)
7746 SAVE_IT (ppos_it, *it, ppos_data);
7747 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7748 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7749 SAVE_IT (ppos_it, *it, ppos_data);
7752 #define BUFFER_POS_REACHED_P() \
7753 ((op & MOVE_TO_POS) != 0 \
7754 && BUFFERP (it->object) \
7755 && (IT_CHARPOS (*it) == to_charpos \
7756 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7757 && (it->method == GET_FROM_BUFFER \
7758 || (it->method == GET_FROM_DISPLAY_VECTOR \
7759 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7761 /* If there's a line-/wrap-prefix, handle it. */
7762 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7763 && it->current_y < it->last_visible_y)
7764 handle_line_prefix (it);
7766 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7767 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7769 while (1)
7771 int x, i, ascent = 0, descent = 0;
7773 /* Utility macro to reset an iterator with x, ascent, and descent. */
7774 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7775 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7776 (IT)->max_descent = descent)
7778 /* Stop if we move beyond TO_CHARPOS (after an image or a
7779 display string or stretch glyph). */
7780 if ((op & MOVE_TO_POS) != 0
7781 && BUFFERP (it->object)
7782 && it->method == GET_FROM_BUFFER
7783 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7784 || (it->bidi_p
7785 && (prev_method == GET_FROM_IMAGE
7786 || prev_method == GET_FROM_STRETCH
7787 || prev_method == GET_FROM_STRING)
7788 /* Passed TO_CHARPOS from left to right. */
7789 && ((prev_pos < to_charpos
7790 && IT_CHARPOS (*it) > to_charpos)
7791 /* Passed TO_CHARPOS from right to left. */
7792 || (prev_pos > to_charpos
7793 && IT_CHARPOS (*it) < to_charpos)))))
7795 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7797 result = MOVE_POS_MATCH_OR_ZV;
7798 break;
7800 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7801 /* If wrap_it is valid, the current position might be in a
7802 word that is wrapped. So, save the iterator in
7803 atpos_it and continue to see if wrapping happens. */
7804 SAVE_IT (atpos_it, *it, atpos_data);
7807 /* Stop when ZV reached.
7808 We used to stop here when TO_CHARPOS reached as well, but that is
7809 too soon if this glyph does not fit on this line. So we handle it
7810 explicitly below. */
7811 if (!get_next_display_element (it))
7813 result = MOVE_POS_MATCH_OR_ZV;
7814 break;
7817 if (it->line_wrap == TRUNCATE)
7819 if (BUFFER_POS_REACHED_P ())
7821 result = MOVE_POS_MATCH_OR_ZV;
7822 break;
7825 else
7827 if (it->line_wrap == WORD_WRAP)
7829 if (IT_DISPLAYING_WHITESPACE (it))
7830 may_wrap = 1;
7831 else if (may_wrap)
7833 /* We have reached a glyph that follows one or more
7834 whitespace characters. If the position is
7835 already found, we are done. */
7836 if (atpos_it.sp >= 0)
7838 RESTORE_IT (it, &atpos_it, atpos_data);
7839 result = MOVE_POS_MATCH_OR_ZV;
7840 goto done;
7842 if (atx_it.sp >= 0)
7844 RESTORE_IT (it, &atx_it, atx_data);
7845 result = MOVE_X_REACHED;
7846 goto done;
7848 /* Otherwise, we can wrap here. */
7849 SAVE_IT (wrap_it, *it, wrap_data);
7850 may_wrap = 0;
7855 /* Remember the line height for the current line, in case
7856 the next element doesn't fit on the line. */
7857 ascent = it->max_ascent;
7858 descent = it->max_descent;
7860 /* The call to produce_glyphs will get the metrics of the
7861 display element IT is loaded with. Record the x-position
7862 before this display element, in case it doesn't fit on the
7863 line. */
7864 x = it->current_x;
7866 PRODUCE_GLYPHS (it);
7868 if (it->area != TEXT_AREA)
7870 prev_method = it->method;
7871 if (it->method == GET_FROM_BUFFER)
7872 prev_pos = IT_CHARPOS (*it);
7873 set_iterator_to_next (it, 1);
7874 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7875 SET_TEXT_POS (this_line_min_pos,
7876 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7877 if (it->bidi_p
7878 && (op & MOVE_TO_POS)
7879 && IT_CHARPOS (*it) > to_charpos
7880 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
7881 SAVE_IT (ppos_it, *it, ppos_data);
7882 continue;
7885 /* The number of glyphs we get back in IT->nglyphs will normally
7886 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7887 character on a terminal frame, or (iii) a line end. For the
7888 second case, IT->nglyphs - 1 padding glyphs will be present.
7889 (On X frames, there is only one glyph produced for a
7890 composite character.)
7892 The behavior implemented below means, for continuation lines,
7893 that as many spaces of a TAB as fit on the current line are
7894 displayed there. For terminal frames, as many glyphs of a
7895 multi-glyph character are displayed in the current line, too.
7896 This is what the old redisplay code did, and we keep it that
7897 way. Under X, the whole shape of a complex character must
7898 fit on the line or it will be completely displayed in the
7899 next line.
7901 Note that both for tabs and padding glyphs, all glyphs have
7902 the same width. */
7903 if (it->nglyphs)
7905 /* More than one glyph or glyph doesn't fit on line. All
7906 glyphs have the same width. */
7907 int single_glyph_width = it->pixel_width / it->nglyphs;
7908 int new_x;
7909 int x_before_this_char = x;
7910 int hpos_before_this_char = it->hpos;
7912 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7914 new_x = x + single_glyph_width;
7916 /* We want to leave anything reaching TO_X to the caller. */
7917 if ((op & MOVE_TO_X) && new_x > to_x)
7919 if (BUFFER_POS_REACHED_P ())
7921 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7922 goto buffer_pos_reached;
7923 if (atpos_it.sp < 0)
7925 SAVE_IT (atpos_it, *it, atpos_data);
7926 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7929 else
7931 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7933 it->current_x = x;
7934 result = MOVE_X_REACHED;
7935 break;
7937 if (atx_it.sp < 0)
7939 SAVE_IT (atx_it, *it, atx_data);
7940 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7945 if (/* Lines are continued. */
7946 it->line_wrap != TRUNCATE
7947 && (/* And glyph doesn't fit on the line. */
7948 new_x > it->last_visible_x
7949 /* Or it fits exactly and we're on a window
7950 system frame. */
7951 || (new_x == it->last_visible_x
7952 && FRAME_WINDOW_P (it->f))))
7954 if (/* IT->hpos == 0 means the very first glyph
7955 doesn't fit on the line, e.g. a wide image. */
7956 it->hpos == 0
7957 || (new_x == it->last_visible_x
7958 && FRAME_WINDOW_P (it->f)))
7960 ++it->hpos;
7961 it->current_x = new_x;
7963 /* The character's last glyph just barely fits
7964 in this row. */
7965 if (i == it->nglyphs - 1)
7967 /* If this is the destination position,
7968 return a position *before* it in this row,
7969 now that we know it fits in this row. */
7970 if (BUFFER_POS_REACHED_P ())
7972 if (it->line_wrap != WORD_WRAP
7973 || wrap_it.sp < 0)
7975 it->hpos = hpos_before_this_char;
7976 it->current_x = x_before_this_char;
7977 result = MOVE_POS_MATCH_OR_ZV;
7978 break;
7980 if (it->line_wrap == WORD_WRAP
7981 && atpos_it.sp < 0)
7983 SAVE_IT (atpos_it, *it, atpos_data);
7984 atpos_it.current_x = x_before_this_char;
7985 atpos_it.hpos = hpos_before_this_char;
7989 prev_method = it->method;
7990 if (it->method == GET_FROM_BUFFER)
7991 prev_pos = IT_CHARPOS (*it);
7992 set_iterator_to_next (it, 1);
7993 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7994 SET_TEXT_POS (this_line_min_pos,
7995 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7996 /* On graphical terminals, newlines may
7997 "overflow" into the fringe if
7998 overflow-newline-into-fringe is non-nil.
7999 On text-only terminals, newlines may
8000 overflow into the last glyph on the
8001 display line.*/
8002 if (!FRAME_WINDOW_P (it->f)
8003 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8005 if (!get_next_display_element (it))
8007 result = MOVE_POS_MATCH_OR_ZV;
8008 break;
8010 if (BUFFER_POS_REACHED_P ())
8012 if (ITERATOR_AT_END_OF_LINE_P (it))
8013 result = MOVE_POS_MATCH_OR_ZV;
8014 else
8015 result = MOVE_LINE_CONTINUED;
8016 break;
8018 if (ITERATOR_AT_END_OF_LINE_P (it))
8020 result = MOVE_NEWLINE_OR_CR;
8021 break;
8026 else
8027 IT_RESET_X_ASCENT_DESCENT (it);
8029 if (wrap_it.sp >= 0)
8031 RESTORE_IT (it, &wrap_it, wrap_data);
8032 atpos_it.sp = -1;
8033 atx_it.sp = -1;
8036 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8037 IT_CHARPOS (*it)));
8038 result = MOVE_LINE_CONTINUED;
8039 break;
8042 if (BUFFER_POS_REACHED_P ())
8044 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8045 goto buffer_pos_reached;
8046 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8048 SAVE_IT (atpos_it, *it, atpos_data);
8049 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8053 if (new_x > it->first_visible_x)
8055 /* Glyph is visible. Increment number of glyphs that
8056 would be displayed. */
8057 ++it->hpos;
8061 if (result != MOVE_UNDEFINED)
8062 break;
8064 else if (BUFFER_POS_REACHED_P ())
8066 buffer_pos_reached:
8067 IT_RESET_X_ASCENT_DESCENT (it);
8068 result = MOVE_POS_MATCH_OR_ZV;
8069 break;
8071 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8073 /* Stop when TO_X specified and reached. This check is
8074 necessary here because of lines consisting of a line end,
8075 only. The line end will not produce any glyphs and we
8076 would never get MOVE_X_REACHED. */
8077 xassert (it->nglyphs == 0);
8078 result = MOVE_X_REACHED;
8079 break;
8082 /* Is this a line end? If yes, we're done. */
8083 if (ITERATOR_AT_END_OF_LINE_P (it))
8085 /* If we are past TO_CHARPOS, but never saw any character
8086 positions smaller than TO_CHARPOS, return
8087 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8088 did. */
8089 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8091 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8093 if (IT_CHARPOS (ppos_it) < ZV)
8094 RESTORE_IT (it, &ppos_it, ppos_data);
8095 goto buffer_pos_reached;
8097 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8098 && IT_CHARPOS (*it) > to_charpos)
8099 goto buffer_pos_reached;
8100 else
8101 result = MOVE_NEWLINE_OR_CR;
8103 else
8104 result = MOVE_NEWLINE_OR_CR;
8105 break;
8108 prev_method = it->method;
8109 if (it->method == GET_FROM_BUFFER)
8110 prev_pos = IT_CHARPOS (*it);
8111 /* The current display element has been consumed. Advance
8112 to the next. */
8113 set_iterator_to_next (it, 1);
8114 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8115 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8116 if (IT_CHARPOS (*it) < to_charpos)
8117 saw_smaller_pos = 1;
8118 if (it->bidi_p
8119 && (op & MOVE_TO_POS)
8120 && IT_CHARPOS (*it) >= to_charpos
8121 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8122 SAVE_IT (ppos_it, *it, ppos_data);
8124 /* Stop if lines are truncated and IT's current x-position is
8125 past the right edge of the window now. */
8126 if (it->line_wrap == TRUNCATE
8127 && it->current_x >= it->last_visible_x)
8129 if (!FRAME_WINDOW_P (it->f)
8130 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8132 int at_eob_p = 0;
8134 if ((at_eob_p = !get_next_display_element (it))
8135 || BUFFER_POS_REACHED_P ()
8136 /* If we are past TO_CHARPOS, but never saw any
8137 character positions smaller than TO_CHARPOS,
8138 return MOVE_POS_MATCH_OR_ZV, like the
8139 unidirectional display did. */
8140 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8141 && !saw_smaller_pos
8142 && IT_CHARPOS (*it) > to_charpos))
8144 if (!at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8145 RESTORE_IT (it, &ppos_it, ppos_data);
8146 goto buffer_pos_reached;
8148 if (ITERATOR_AT_END_OF_LINE_P (it))
8150 result = MOVE_NEWLINE_OR_CR;
8151 break;
8154 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8155 && !saw_smaller_pos
8156 && IT_CHARPOS (*it) > to_charpos)
8158 if (IT_CHARPOS (ppos_it) < ZV)
8159 RESTORE_IT (it, &ppos_it, ppos_data);
8160 goto buffer_pos_reached;
8162 result = MOVE_LINE_TRUNCATED;
8163 break;
8165 #undef IT_RESET_X_ASCENT_DESCENT
8168 #undef BUFFER_POS_REACHED_P
8170 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8171 restore the saved iterator. */
8172 if (atpos_it.sp >= 0)
8173 RESTORE_IT (it, &atpos_it, atpos_data);
8174 else if (atx_it.sp >= 0)
8175 RESTORE_IT (it, &atx_it, atx_data);
8177 done:
8179 if (atpos_data)
8180 bidi_unshelve_cache (atpos_data, 1);
8181 if (atx_data)
8182 bidi_unshelve_cache (atx_data, 1);
8183 if (wrap_data)
8184 bidi_unshelve_cache (wrap_data, 1);
8185 if (ppos_data)
8186 bidi_unshelve_cache (ppos_data, 1);
8188 /* Restore the iterator settings altered at the beginning of this
8189 function. */
8190 it->glyph_row = saved_glyph_row;
8191 return result;
8194 /* For external use. */
8195 void
8196 move_it_in_display_line (struct it *it,
8197 EMACS_INT to_charpos, int to_x,
8198 enum move_operation_enum op)
8200 if (it->line_wrap == WORD_WRAP
8201 && (op & MOVE_TO_X))
8203 struct it save_it;
8204 void *save_data = NULL;
8205 int skip;
8207 SAVE_IT (save_it, *it, save_data);
8208 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8209 /* When word-wrap is on, TO_X may lie past the end
8210 of a wrapped line. Then it->current is the
8211 character on the next line, so backtrack to the
8212 space before the wrap point. */
8213 if (skip == MOVE_LINE_CONTINUED)
8215 int prev_x = max (it->current_x - 1, 0);
8216 RESTORE_IT (it, &save_it, save_data);
8217 move_it_in_display_line_to
8218 (it, -1, prev_x, MOVE_TO_X);
8220 else
8221 bidi_unshelve_cache (save_data, 1);
8223 else
8224 move_it_in_display_line_to (it, to_charpos, to_x, op);
8228 /* Move IT forward until it satisfies one or more of the criteria in
8229 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8231 OP is a bit-mask that specifies where to stop, and in particular,
8232 which of those four position arguments makes a difference. See the
8233 description of enum move_operation_enum.
8235 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8236 screen line, this function will set IT to the next position that is
8237 displayed to the right of TO_CHARPOS on the screen. */
8239 void
8240 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8242 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8243 int line_height, line_start_x = 0, reached = 0;
8244 void *backup_data = NULL;
8246 for (;;)
8248 if (op & MOVE_TO_VPOS)
8250 /* If no TO_CHARPOS and no TO_X specified, stop at the
8251 start of the line TO_VPOS. */
8252 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8254 if (it->vpos == to_vpos)
8256 reached = 1;
8257 break;
8259 else
8260 skip = move_it_in_display_line_to (it, -1, -1, 0);
8262 else
8264 /* TO_VPOS >= 0 means stop at TO_X in the line at
8265 TO_VPOS, or at TO_POS, whichever comes first. */
8266 if (it->vpos == to_vpos)
8268 reached = 2;
8269 break;
8272 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8274 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8276 reached = 3;
8277 break;
8279 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8281 /* We have reached TO_X but not in the line we want. */
8282 skip = move_it_in_display_line_to (it, to_charpos,
8283 -1, MOVE_TO_POS);
8284 if (skip == MOVE_POS_MATCH_OR_ZV)
8286 reached = 4;
8287 break;
8292 else if (op & MOVE_TO_Y)
8294 struct it it_backup;
8296 if (it->line_wrap == WORD_WRAP)
8297 SAVE_IT (it_backup, *it, backup_data);
8299 /* TO_Y specified means stop at TO_X in the line containing
8300 TO_Y---or at TO_CHARPOS if this is reached first. The
8301 problem is that we can't really tell whether the line
8302 contains TO_Y before we have completely scanned it, and
8303 this may skip past TO_X. What we do is to first scan to
8304 TO_X.
8306 If TO_X is not specified, use a TO_X of zero. The reason
8307 is to make the outcome of this function more predictable.
8308 If we didn't use TO_X == 0, we would stop at the end of
8309 the line which is probably not what a caller would expect
8310 to happen. */
8311 skip = move_it_in_display_line_to
8312 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8313 (MOVE_TO_X | (op & MOVE_TO_POS)));
8315 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8316 if (skip == MOVE_POS_MATCH_OR_ZV)
8317 reached = 5;
8318 else if (skip == MOVE_X_REACHED)
8320 /* If TO_X was reached, we want to know whether TO_Y is
8321 in the line. We know this is the case if the already
8322 scanned glyphs make the line tall enough. Otherwise,
8323 we must check by scanning the rest of the line. */
8324 line_height = it->max_ascent + it->max_descent;
8325 if (to_y >= it->current_y
8326 && to_y < it->current_y + line_height)
8328 reached = 6;
8329 break;
8331 SAVE_IT (it_backup, *it, backup_data);
8332 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8333 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8334 op & MOVE_TO_POS);
8335 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8336 line_height = it->max_ascent + it->max_descent;
8337 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8339 if (to_y >= it->current_y
8340 && to_y < it->current_y + line_height)
8342 /* If TO_Y is in this line and TO_X was reached
8343 above, we scanned too far. We have to restore
8344 IT's settings to the ones before skipping. */
8345 RESTORE_IT (it, &it_backup, backup_data);
8346 reached = 6;
8348 else
8350 skip = skip2;
8351 if (skip == MOVE_POS_MATCH_OR_ZV)
8352 reached = 7;
8355 else
8357 /* Check whether TO_Y is in this line. */
8358 line_height = it->max_ascent + it->max_descent;
8359 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8361 if (to_y >= it->current_y
8362 && to_y < it->current_y + line_height)
8364 /* When word-wrap is on, TO_X may lie past the end
8365 of a wrapped line. Then it->current is the
8366 character on the next line, so backtrack to the
8367 space before the wrap point. */
8368 if (skip == MOVE_LINE_CONTINUED
8369 && it->line_wrap == WORD_WRAP)
8371 int prev_x = max (it->current_x - 1, 0);
8372 RESTORE_IT (it, &it_backup, backup_data);
8373 skip = move_it_in_display_line_to
8374 (it, -1, prev_x, MOVE_TO_X);
8376 reached = 6;
8380 if (reached)
8381 break;
8383 else if (BUFFERP (it->object)
8384 && (it->method == GET_FROM_BUFFER
8385 || it->method == GET_FROM_STRETCH)
8386 && IT_CHARPOS (*it) >= to_charpos)
8387 skip = MOVE_POS_MATCH_OR_ZV;
8388 else
8389 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8391 switch (skip)
8393 case MOVE_POS_MATCH_OR_ZV:
8394 reached = 8;
8395 goto out;
8397 case MOVE_NEWLINE_OR_CR:
8398 set_iterator_to_next (it, 1);
8399 it->continuation_lines_width = 0;
8400 break;
8402 case MOVE_LINE_TRUNCATED:
8403 it->continuation_lines_width = 0;
8404 reseat_at_next_visible_line_start (it, 0);
8405 if ((op & MOVE_TO_POS) != 0
8406 && IT_CHARPOS (*it) > to_charpos)
8408 reached = 9;
8409 goto out;
8411 break;
8413 case MOVE_LINE_CONTINUED:
8414 /* For continued lines ending in a tab, some of the glyphs
8415 associated with the tab are displayed on the current
8416 line. Since it->current_x does not include these glyphs,
8417 we use it->last_visible_x instead. */
8418 if (it->c == '\t')
8420 it->continuation_lines_width += it->last_visible_x;
8421 /* When moving by vpos, ensure that the iterator really
8422 advances to the next line (bug#847, bug#969). Fixme:
8423 do we need to do this in other circumstances? */
8424 if (it->current_x != it->last_visible_x
8425 && (op & MOVE_TO_VPOS)
8426 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8428 line_start_x = it->current_x + it->pixel_width
8429 - it->last_visible_x;
8430 set_iterator_to_next (it, 0);
8433 else
8434 it->continuation_lines_width += it->current_x;
8435 break;
8437 default:
8438 abort ();
8441 /* Reset/increment for the next run. */
8442 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8443 it->current_x = line_start_x;
8444 line_start_x = 0;
8445 it->hpos = 0;
8446 it->current_y += it->max_ascent + it->max_descent;
8447 ++it->vpos;
8448 last_height = it->max_ascent + it->max_descent;
8449 last_max_ascent = it->max_ascent;
8450 it->max_ascent = it->max_descent = 0;
8453 out:
8455 /* On text terminals, we may stop at the end of a line in the middle
8456 of a multi-character glyph. If the glyph itself is continued,
8457 i.e. it is actually displayed on the next line, don't treat this
8458 stopping point as valid; move to the next line instead (unless
8459 that brings us offscreen). */
8460 if (!FRAME_WINDOW_P (it->f)
8461 && op & MOVE_TO_POS
8462 && IT_CHARPOS (*it) == to_charpos
8463 && it->what == IT_CHARACTER
8464 && it->nglyphs > 1
8465 && it->line_wrap == WINDOW_WRAP
8466 && it->current_x == it->last_visible_x - 1
8467 && it->c != '\n'
8468 && it->c != '\t'
8469 && it->vpos < XFASTINT (it->w->window_end_vpos))
8471 it->continuation_lines_width += it->current_x;
8472 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8473 it->current_y += it->max_ascent + it->max_descent;
8474 ++it->vpos;
8475 last_height = it->max_ascent + it->max_descent;
8476 last_max_ascent = it->max_ascent;
8479 if (backup_data)
8480 bidi_unshelve_cache (backup_data, 1);
8482 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8486 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8488 If DY > 0, move IT backward at least that many pixels. DY = 0
8489 means move IT backward to the preceding line start or BEGV. This
8490 function may move over more than DY pixels if IT->current_y - DY
8491 ends up in the middle of a line; in this case IT->current_y will be
8492 set to the top of the line moved to. */
8494 void
8495 move_it_vertically_backward (struct it *it, int dy)
8497 int nlines, h;
8498 struct it it2, it3;
8499 void *it2data = NULL, *it3data = NULL;
8500 EMACS_INT start_pos;
8502 move_further_back:
8503 xassert (dy >= 0);
8505 start_pos = IT_CHARPOS (*it);
8507 /* Estimate how many newlines we must move back. */
8508 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8510 /* Set the iterator's position that many lines back. */
8511 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8512 back_to_previous_visible_line_start (it);
8514 /* Reseat the iterator here. When moving backward, we don't want
8515 reseat to skip forward over invisible text, set up the iterator
8516 to deliver from overlay strings at the new position etc. So,
8517 use reseat_1 here. */
8518 reseat_1 (it, it->current.pos, 1);
8520 /* We are now surely at a line start. */
8521 it->current_x = it->hpos = 0;
8522 it->continuation_lines_width = 0;
8524 /* Move forward and see what y-distance we moved. First move to the
8525 start of the next line so that we get its height. We need this
8526 height to be able to tell whether we reached the specified
8527 y-distance. */
8528 SAVE_IT (it2, *it, it2data);
8529 it2.max_ascent = it2.max_descent = 0;
8532 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8533 MOVE_TO_POS | MOVE_TO_VPOS);
8535 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8536 xassert (IT_CHARPOS (*it) >= BEGV);
8537 SAVE_IT (it3, it2, it3data);
8539 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8540 xassert (IT_CHARPOS (*it) >= BEGV);
8541 /* H is the actual vertical distance from the position in *IT
8542 and the starting position. */
8543 h = it2.current_y - it->current_y;
8544 /* NLINES is the distance in number of lines. */
8545 nlines = it2.vpos - it->vpos;
8547 /* Correct IT's y and vpos position
8548 so that they are relative to the starting point. */
8549 it->vpos -= nlines;
8550 it->current_y -= h;
8552 if (dy == 0)
8554 /* DY == 0 means move to the start of the screen line. The
8555 value of nlines is > 0 if continuation lines were involved. */
8556 RESTORE_IT (it, it, it2data);
8557 if (nlines > 0)
8558 move_it_by_lines (it, nlines);
8559 bidi_unshelve_cache (it3data, 1);
8561 else
8563 /* The y-position we try to reach, relative to *IT.
8564 Note that H has been subtracted in front of the if-statement. */
8565 int target_y = it->current_y + h - dy;
8566 int y0 = it3.current_y;
8567 int y1;
8568 int line_height;
8570 RESTORE_IT (&it3, &it3, it3data);
8571 y1 = line_bottom_y (&it3);
8572 line_height = y1 - y0;
8573 RESTORE_IT (it, it, it2data);
8574 /* If we did not reach target_y, try to move further backward if
8575 we can. If we moved too far backward, try to move forward. */
8576 if (target_y < it->current_y
8577 /* This is heuristic. In a window that's 3 lines high, with
8578 a line height of 13 pixels each, recentering with point
8579 on the bottom line will try to move -39/2 = 19 pixels
8580 backward. Try to avoid moving into the first line. */
8581 && (it->current_y - target_y
8582 > min (window_box_height (it->w), line_height * 2 / 3))
8583 && IT_CHARPOS (*it) > BEGV)
8585 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8586 target_y - it->current_y));
8587 dy = it->current_y - target_y;
8588 goto move_further_back;
8590 else if (target_y >= it->current_y + line_height
8591 && IT_CHARPOS (*it) < ZV)
8593 /* Should move forward by at least one line, maybe more.
8595 Note: Calling move_it_by_lines can be expensive on
8596 terminal frames, where compute_motion is used (via
8597 vmotion) to do the job, when there are very long lines
8598 and truncate-lines is nil. That's the reason for
8599 treating terminal frames specially here. */
8601 if (!FRAME_WINDOW_P (it->f))
8602 move_it_vertically (it, target_y - (it->current_y + line_height));
8603 else
8607 move_it_by_lines (it, 1);
8609 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8616 /* Move IT by a specified amount of pixel lines DY. DY negative means
8617 move backwards. DY = 0 means move to start of screen line. At the
8618 end, IT will be on the start of a screen line. */
8620 void
8621 move_it_vertically (struct it *it, int dy)
8623 if (dy <= 0)
8624 move_it_vertically_backward (it, -dy);
8625 else
8627 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8628 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8629 MOVE_TO_POS | MOVE_TO_Y);
8630 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8632 /* If buffer ends in ZV without a newline, move to the start of
8633 the line to satisfy the post-condition. */
8634 if (IT_CHARPOS (*it) == ZV
8635 && ZV > BEGV
8636 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8637 move_it_by_lines (it, 0);
8642 /* Move iterator IT past the end of the text line it is in. */
8644 void
8645 move_it_past_eol (struct it *it)
8647 enum move_it_result rc;
8649 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8650 if (rc == MOVE_NEWLINE_OR_CR)
8651 set_iterator_to_next (it, 0);
8655 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8656 negative means move up. DVPOS == 0 means move to the start of the
8657 screen line.
8659 Optimization idea: If we would know that IT->f doesn't use
8660 a face with proportional font, we could be faster for
8661 truncate-lines nil. */
8663 void
8664 move_it_by_lines (struct it *it, int dvpos)
8667 /* The commented-out optimization uses vmotion on terminals. This
8668 gives bad results, because elements like it->what, on which
8669 callers such as pos_visible_p rely, aren't updated. */
8670 /* struct position pos;
8671 if (!FRAME_WINDOW_P (it->f))
8673 struct text_pos textpos;
8675 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8676 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8677 reseat (it, textpos, 1);
8678 it->vpos += pos.vpos;
8679 it->current_y += pos.vpos;
8681 else */
8683 if (dvpos == 0)
8685 /* DVPOS == 0 means move to the start of the screen line. */
8686 move_it_vertically_backward (it, 0);
8687 xassert (it->current_x == 0 && it->hpos == 0);
8688 /* Let next call to line_bottom_y calculate real line height */
8689 last_height = 0;
8691 else if (dvpos > 0)
8693 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8694 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8695 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8697 else
8699 struct it it2;
8700 void *it2data = NULL;
8701 EMACS_INT start_charpos, i;
8703 /* Start at the beginning of the screen line containing IT's
8704 position. This may actually move vertically backwards,
8705 in case of overlays, so adjust dvpos accordingly. */
8706 dvpos += it->vpos;
8707 move_it_vertically_backward (it, 0);
8708 dvpos -= it->vpos;
8710 /* Go back -DVPOS visible lines and reseat the iterator there. */
8711 start_charpos = IT_CHARPOS (*it);
8712 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8713 back_to_previous_visible_line_start (it);
8714 reseat (it, it->current.pos, 1);
8716 /* Move further back if we end up in a string or an image. */
8717 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8719 /* First try to move to start of display line. */
8720 dvpos += it->vpos;
8721 move_it_vertically_backward (it, 0);
8722 dvpos -= it->vpos;
8723 if (IT_POS_VALID_AFTER_MOVE_P (it))
8724 break;
8725 /* If start of line is still in string or image,
8726 move further back. */
8727 back_to_previous_visible_line_start (it);
8728 reseat (it, it->current.pos, 1);
8729 dvpos--;
8732 it->current_x = it->hpos = 0;
8734 /* Above call may have moved too far if continuation lines
8735 are involved. Scan forward and see if it did. */
8736 SAVE_IT (it2, *it, it2data);
8737 it2.vpos = it2.current_y = 0;
8738 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8739 it->vpos -= it2.vpos;
8740 it->current_y -= it2.current_y;
8741 it->current_x = it->hpos = 0;
8743 /* If we moved too far back, move IT some lines forward. */
8744 if (it2.vpos > -dvpos)
8746 int delta = it2.vpos + dvpos;
8748 RESTORE_IT (&it2, &it2, it2data);
8749 SAVE_IT (it2, *it, it2data);
8750 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8751 /* Move back again if we got too far ahead. */
8752 if (IT_CHARPOS (*it) >= start_charpos)
8753 RESTORE_IT (it, &it2, it2data);
8754 else
8755 bidi_unshelve_cache (it2data, 1);
8757 else
8758 RESTORE_IT (it, it, it2data);
8762 /* Return 1 if IT points into the middle of a display vector. */
8765 in_display_vector_p (struct it *it)
8767 return (it->method == GET_FROM_DISPLAY_VECTOR
8768 && it->current.dpvec_index > 0
8769 && it->dpvec + it->current.dpvec_index != it->dpend);
8773 /***********************************************************************
8774 Messages
8775 ***********************************************************************/
8778 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8779 to *Messages*. */
8781 void
8782 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8784 Lisp_Object args[3];
8785 Lisp_Object msg, fmt;
8786 char *buffer;
8787 EMACS_INT len;
8788 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8789 USE_SAFE_ALLOCA;
8791 /* Do nothing if called asynchronously. Inserting text into
8792 a buffer may call after-change-functions and alike and
8793 that would means running Lisp asynchronously. */
8794 if (handling_signal)
8795 return;
8797 fmt = msg = Qnil;
8798 GCPRO4 (fmt, msg, arg1, arg2);
8800 args[0] = fmt = build_string (format);
8801 args[1] = arg1;
8802 args[2] = arg2;
8803 msg = Fformat (3, args);
8805 len = SBYTES (msg) + 1;
8806 SAFE_ALLOCA (buffer, char *, len);
8807 memcpy (buffer, SDATA (msg), len);
8809 message_dolog (buffer, len - 1, 1, 0);
8810 SAFE_FREE ();
8812 UNGCPRO;
8816 /* Output a newline in the *Messages* buffer if "needs" one. */
8818 void
8819 message_log_maybe_newline (void)
8821 if (message_log_need_newline)
8822 message_dolog ("", 0, 1, 0);
8826 /* Add a string M of length NBYTES to the message log, optionally
8827 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8828 nonzero, means interpret the contents of M as multibyte. This
8829 function calls low-level routines in order to bypass text property
8830 hooks, etc. which might not be safe to run.
8832 This may GC (insert may run before/after change hooks),
8833 so the buffer M must NOT point to a Lisp string. */
8835 void
8836 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8838 const unsigned char *msg = (const unsigned char *) m;
8840 if (!NILP (Vmemory_full))
8841 return;
8843 if (!NILP (Vmessage_log_max))
8845 struct buffer *oldbuf;
8846 Lisp_Object oldpoint, oldbegv, oldzv;
8847 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8848 EMACS_INT point_at_end = 0;
8849 EMACS_INT zv_at_end = 0;
8850 Lisp_Object old_deactivate_mark, tem;
8851 struct gcpro gcpro1;
8853 old_deactivate_mark = Vdeactivate_mark;
8854 oldbuf = current_buffer;
8855 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8856 BVAR (current_buffer, undo_list) = Qt;
8858 oldpoint = message_dolog_marker1;
8859 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8860 oldbegv = message_dolog_marker2;
8861 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8862 oldzv = message_dolog_marker3;
8863 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8864 GCPRO1 (old_deactivate_mark);
8866 if (PT == Z)
8867 point_at_end = 1;
8868 if (ZV == Z)
8869 zv_at_end = 1;
8871 BEGV = BEG;
8872 BEGV_BYTE = BEG_BYTE;
8873 ZV = Z;
8874 ZV_BYTE = Z_BYTE;
8875 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8877 /* Insert the string--maybe converting multibyte to single byte
8878 or vice versa, so that all the text fits the buffer. */
8879 if (multibyte
8880 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8882 EMACS_INT i;
8883 int c, char_bytes;
8884 char work[1];
8886 /* Convert a multibyte string to single-byte
8887 for the *Message* buffer. */
8888 for (i = 0; i < nbytes; i += char_bytes)
8890 c = string_char_and_length (msg + i, &char_bytes);
8891 work[0] = (ASCII_CHAR_P (c)
8893 : multibyte_char_to_unibyte (c));
8894 insert_1_both (work, 1, 1, 1, 0, 0);
8897 else if (! multibyte
8898 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8900 EMACS_INT i;
8901 int c, char_bytes;
8902 unsigned char str[MAX_MULTIBYTE_LENGTH];
8903 /* Convert a single-byte string to multibyte
8904 for the *Message* buffer. */
8905 for (i = 0; i < nbytes; i++)
8907 c = msg[i];
8908 MAKE_CHAR_MULTIBYTE (c);
8909 char_bytes = CHAR_STRING (c, str);
8910 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8913 else if (nbytes)
8914 insert_1 (m, nbytes, 1, 0, 0);
8916 if (nlflag)
8918 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8919 printmax_t dups;
8920 insert_1 ("\n", 1, 1, 0, 0);
8922 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8923 this_bol = PT;
8924 this_bol_byte = PT_BYTE;
8926 /* See if this line duplicates the previous one.
8927 If so, combine duplicates. */
8928 if (this_bol > BEG)
8930 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8931 prev_bol = PT;
8932 prev_bol_byte = PT_BYTE;
8934 dups = message_log_check_duplicate (prev_bol_byte,
8935 this_bol_byte);
8936 if (dups)
8938 del_range_both (prev_bol, prev_bol_byte,
8939 this_bol, this_bol_byte, 0);
8940 if (dups > 1)
8942 char dupstr[sizeof " [ times]"
8943 + INT_STRLEN_BOUND (printmax_t)];
8944 int duplen;
8946 /* If you change this format, don't forget to also
8947 change message_log_check_duplicate. */
8948 sprintf (dupstr, " [%"pMd" times]", dups);
8949 duplen = strlen (dupstr);
8950 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8951 insert_1 (dupstr, duplen, 1, 0, 1);
8956 /* If we have more than the desired maximum number of lines
8957 in the *Messages* buffer now, delete the oldest ones.
8958 This is safe because we don't have undo in this buffer. */
8960 if (NATNUMP (Vmessage_log_max))
8962 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8963 -XFASTINT (Vmessage_log_max) - 1, 0);
8964 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8967 BEGV = XMARKER (oldbegv)->charpos;
8968 BEGV_BYTE = marker_byte_position (oldbegv);
8970 if (zv_at_end)
8972 ZV = Z;
8973 ZV_BYTE = Z_BYTE;
8975 else
8977 ZV = XMARKER (oldzv)->charpos;
8978 ZV_BYTE = marker_byte_position (oldzv);
8981 if (point_at_end)
8982 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8983 else
8984 /* We can't do Fgoto_char (oldpoint) because it will run some
8985 Lisp code. */
8986 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8987 XMARKER (oldpoint)->bytepos);
8989 UNGCPRO;
8990 unchain_marker (XMARKER (oldpoint));
8991 unchain_marker (XMARKER (oldbegv));
8992 unchain_marker (XMARKER (oldzv));
8994 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8995 set_buffer_internal (oldbuf);
8996 if (NILP (tem))
8997 windows_or_buffers_changed = old_windows_or_buffers_changed;
8998 message_log_need_newline = !nlflag;
8999 Vdeactivate_mark = old_deactivate_mark;
9004 /* We are at the end of the buffer after just having inserted a newline.
9005 (Note: We depend on the fact we won't be crossing the gap.)
9006 Check to see if the most recent message looks a lot like the previous one.
9007 Return 0 if different, 1 if the new one should just replace it, or a
9008 value N > 1 if we should also append " [N times]". */
9010 static intmax_t
9011 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9013 EMACS_INT i;
9014 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9015 int seen_dots = 0;
9016 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9017 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9019 for (i = 0; i < len; i++)
9021 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9022 seen_dots = 1;
9023 if (p1[i] != p2[i])
9024 return seen_dots;
9026 p1 += len;
9027 if (*p1 == '\n')
9028 return 2;
9029 if (*p1++ == ' ' && *p1++ == '[')
9031 char *pend;
9032 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9033 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9034 return n+1;
9036 return 0;
9040 /* Display an echo area message M with a specified length of NBYTES
9041 bytes. The string may include null characters. If M is 0, clear
9042 out any existing message, and let the mini-buffer text show
9043 through.
9045 This may GC, so the buffer M must NOT point to a Lisp string. */
9047 void
9048 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9050 /* First flush out any partial line written with print. */
9051 message_log_maybe_newline ();
9052 if (m)
9053 message_dolog (m, nbytes, 1, multibyte);
9054 message2_nolog (m, nbytes, multibyte);
9058 /* The non-logging counterpart of message2. */
9060 void
9061 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9063 struct frame *sf = SELECTED_FRAME ();
9064 message_enable_multibyte = multibyte;
9066 if (FRAME_INITIAL_P (sf))
9068 if (noninteractive_need_newline)
9069 putc ('\n', stderr);
9070 noninteractive_need_newline = 0;
9071 if (m)
9072 fwrite (m, nbytes, 1, stderr);
9073 if (cursor_in_echo_area == 0)
9074 fprintf (stderr, "\n");
9075 fflush (stderr);
9077 /* A null message buffer means that the frame hasn't really been
9078 initialized yet. Error messages get reported properly by
9079 cmd_error, so this must be just an informative message; toss it. */
9080 else if (INTERACTIVE
9081 && sf->glyphs_initialized_p
9082 && FRAME_MESSAGE_BUF (sf))
9084 Lisp_Object mini_window;
9085 struct frame *f;
9087 /* Get the frame containing the mini-buffer
9088 that the selected frame is using. */
9089 mini_window = FRAME_MINIBUF_WINDOW (sf);
9090 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9092 FRAME_SAMPLE_VISIBILITY (f);
9093 if (FRAME_VISIBLE_P (sf)
9094 && ! FRAME_VISIBLE_P (f))
9095 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9097 if (m)
9099 set_message (m, Qnil, nbytes, multibyte);
9100 if (minibuffer_auto_raise)
9101 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9103 else
9104 clear_message (1, 1);
9106 do_pending_window_change (0);
9107 echo_area_display (1);
9108 do_pending_window_change (0);
9109 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9110 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9115 /* Display an echo area message M with a specified length of NBYTES
9116 bytes. The string may include null characters. If M is not a
9117 string, clear out any existing message, and let the mini-buffer
9118 text show through.
9120 This function cancels echoing. */
9122 void
9123 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9125 struct gcpro gcpro1;
9127 GCPRO1 (m);
9128 clear_message (1,1);
9129 cancel_echoing ();
9131 /* First flush out any partial line written with print. */
9132 message_log_maybe_newline ();
9133 if (STRINGP (m))
9135 char *buffer;
9136 USE_SAFE_ALLOCA;
9138 SAFE_ALLOCA (buffer, char *, nbytes);
9139 memcpy (buffer, SDATA (m), nbytes);
9140 message_dolog (buffer, nbytes, 1, multibyte);
9141 SAFE_FREE ();
9143 message3_nolog (m, nbytes, multibyte);
9145 UNGCPRO;
9149 /* The non-logging version of message3.
9150 This does not cancel echoing, because it is used for echoing.
9151 Perhaps we need to make a separate function for echoing
9152 and make this cancel echoing. */
9154 void
9155 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9157 struct frame *sf = SELECTED_FRAME ();
9158 message_enable_multibyte = multibyte;
9160 if (FRAME_INITIAL_P (sf))
9162 if (noninteractive_need_newline)
9163 putc ('\n', stderr);
9164 noninteractive_need_newline = 0;
9165 if (STRINGP (m))
9166 fwrite (SDATA (m), nbytes, 1, stderr);
9167 if (cursor_in_echo_area == 0)
9168 fprintf (stderr, "\n");
9169 fflush (stderr);
9171 /* A null message buffer means that the frame hasn't really been
9172 initialized yet. Error messages get reported properly by
9173 cmd_error, so this must be just an informative message; toss it. */
9174 else if (INTERACTIVE
9175 && sf->glyphs_initialized_p
9176 && FRAME_MESSAGE_BUF (sf))
9178 Lisp_Object mini_window;
9179 Lisp_Object frame;
9180 struct frame *f;
9182 /* Get the frame containing the mini-buffer
9183 that the selected frame is using. */
9184 mini_window = FRAME_MINIBUF_WINDOW (sf);
9185 frame = XWINDOW (mini_window)->frame;
9186 f = XFRAME (frame);
9188 FRAME_SAMPLE_VISIBILITY (f);
9189 if (FRAME_VISIBLE_P (sf)
9190 && !FRAME_VISIBLE_P (f))
9191 Fmake_frame_visible (frame);
9193 if (STRINGP (m) && SCHARS (m) > 0)
9195 set_message (NULL, m, nbytes, multibyte);
9196 if (minibuffer_auto_raise)
9197 Fraise_frame (frame);
9198 /* Assume we are not echoing.
9199 (If we are, echo_now will override this.) */
9200 echo_message_buffer = Qnil;
9202 else
9203 clear_message (1, 1);
9205 do_pending_window_change (0);
9206 echo_area_display (1);
9207 do_pending_window_change (0);
9208 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9209 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9214 /* Display a null-terminated echo area message M. If M is 0, clear
9215 out any existing message, and let the mini-buffer text show through.
9217 The buffer M must continue to exist until after the echo area gets
9218 cleared or some other message gets displayed there. Do not pass
9219 text that is stored in a Lisp string. Do not pass text in a buffer
9220 that was alloca'd. */
9222 void
9223 message1 (const char *m)
9225 message2 (m, (m ? strlen (m) : 0), 0);
9229 /* The non-logging counterpart of message1. */
9231 void
9232 message1_nolog (const char *m)
9234 message2_nolog (m, (m ? strlen (m) : 0), 0);
9237 /* Display a message M which contains a single %s
9238 which gets replaced with STRING. */
9240 void
9241 message_with_string (const char *m, Lisp_Object string, int log)
9243 CHECK_STRING (string);
9245 if (noninteractive)
9247 if (m)
9249 if (noninteractive_need_newline)
9250 putc ('\n', stderr);
9251 noninteractive_need_newline = 0;
9252 fprintf (stderr, m, SDATA (string));
9253 if (!cursor_in_echo_area)
9254 fprintf (stderr, "\n");
9255 fflush (stderr);
9258 else if (INTERACTIVE)
9260 /* The frame whose minibuffer we're going to display the message on.
9261 It may be larger than the selected frame, so we need
9262 to use its buffer, not the selected frame's buffer. */
9263 Lisp_Object mini_window;
9264 struct frame *f, *sf = SELECTED_FRAME ();
9266 /* Get the frame containing the minibuffer
9267 that the selected frame is using. */
9268 mini_window = FRAME_MINIBUF_WINDOW (sf);
9269 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9271 /* A null message buffer means that the frame hasn't really been
9272 initialized yet. Error messages get reported properly by
9273 cmd_error, so this must be just an informative message; toss it. */
9274 if (FRAME_MESSAGE_BUF (f))
9276 Lisp_Object args[2], msg;
9277 struct gcpro gcpro1, gcpro2;
9279 args[0] = build_string (m);
9280 args[1] = msg = string;
9281 GCPRO2 (args[0], msg);
9282 gcpro1.nvars = 2;
9284 msg = Fformat (2, args);
9286 if (log)
9287 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9288 else
9289 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9291 UNGCPRO;
9293 /* Print should start at the beginning of the message
9294 buffer next time. */
9295 message_buf_print = 0;
9301 /* Dump an informative message to the minibuf. If M is 0, clear out
9302 any existing message, and let the mini-buffer text show through. */
9304 static void
9305 vmessage (const char *m, va_list ap)
9307 if (noninteractive)
9309 if (m)
9311 if (noninteractive_need_newline)
9312 putc ('\n', stderr);
9313 noninteractive_need_newline = 0;
9314 vfprintf (stderr, m, ap);
9315 if (cursor_in_echo_area == 0)
9316 fprintf (stderr, "\n");
9317 fflush (stderr);
9320 else if (INTERACTIVE)
9322 /* The frame whose mini-buffer we're going to display the message
9323 on. It may be larger than the selected frame, so we need to
9324 use its buffer, not the selected frame's buffer. */
9325 Lisp_Object mini_window;
9326 struct frame *f, *sf = SELECTED_FRAME ();
9328 /* Get the frame containing the mini-buffer
9329 that the selected frame is using. */
9330 mini_window = FRAME_MINIBUF_WINDOW (sf);
9331 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9333 /* A null message buffer means that the frame hasn't really been
9334 initialized yet. Error messages get reported properly by
9335 cmd_error, so this must be just an informative message; toss
9336 it. */
9337 if (FRAME_MESSAGE_BUF (f))
9339 if (m)
9341 ptrdiff_t len;
9343 len = doprnt (FRAME_MESSAGE_BUF (f),
9344 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9346 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9348 else
9349 message1 (0);
9351 /* Print should start at the beginning of the message
9352 buffer next time. */
9353 message_buf_print = 0;
9358 void
9359 message (const char *m, ...)
9361 va_list ap;
9362 va_start (ap, m);
9363 vmessage (m, ap);
9364 va_end (ap);
9368 #if 0
9369 /* The non-logging version of message. */
9371 void
9372 message_nolog (const char *m, ...)
9374 Lisp_Object old_log_max;
9375 va_list ap;
9376 va_start (ap, m);
9377 old_log_max = Vmessage_log_max;
9378 Vmessage_log_max = Qnil;
9379 vmessage (m, ap);
9380 Vmessage_log_max = old_log_max;
9381 va_end (ap);
9383 #endif
9386 /* Display the current message in the current mini-buffer. This is
9387 only called from error handlers in process.c, and is not time
9388 critical. */
9390 void
9391 update_echo_area (void)
9393 if (!NILP (echo_area_buffer[0]))
9395 Lisp_Object string;
9396 string = Fcurrent_message ();
9397 message3 (string, SBYTES (string),
9398 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9403 /* Make sure echo area buffers in `echo_buffers' are live.
9404 If they aren't, make new ones. */
9406 static void
9407 ensure_echo_area_buffers (void)
9409 int i;
9411 for (i = 0; i < 2; ++i)
9412 if (!BUFFERP (echo_buffer[i])
9413 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9415 char name[30];
9416 Lisp_Object old_buffer;
9417 int j;
9419 old_buffer = echo_buffer[i];
9420 sprintf (name, " *Echo Area %d*", i);
9421 echo_buffer[i] = Fget_buffer_create (build_string (name));
9422 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9423 /* to force word wrap in echo area -
9424 it was decided to postpone this*/
9425 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9427 for (j = 0; j < 2; ++j)
9428 if (EQ (old_buffer, echo_area_buffer[j]))
9429 echo_area_buffer[j] = echo_buffer[i];
9434 /* Call FN with args A1..A4 with either the current or last displayed
9435 echo_area_buffer as current buffer.
9437 WHICH zero means use the current message buffer
9438 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9439 from echo_buffer[] and clear it.
9441 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9442 suitable buffer from echo_buffer[] and clear it.
9444 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9445 that the current message becomes the last displayed one, make
9446 choose a suitable buffer for echo_area_buffer[0], and clear it.
9448 Value is what FN returns. */
9450 static int
9451 with_echo_area_buffer (struct window *w, int which,
9452 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9453 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9455 Lisp_Object buffer;
9456 int this_one, the_other, clear_buffer_p, rc;
9457 int count = SPECPDL_INDEX ();
9459 /* If buffers aren't live, make new ones. */
9460 ensure_echo_area_buffers ();
9462 clear_buffer_p = 0;
9464 if (which == 0)
9465 this_one = 0, the_other = 1;
9466 else if (which > 0)
9467 this_one = 1, the_other = 0;
9468 else
9470 this_one = 0, the_other = 1;
9471 clear_buffer_p = 1;
9473 /* We need a fresh one in case the current echo buffer equals
9474 the one containing the last displayed echo area message. */
9475 if (!NILP (echo_area_buffer[this_one])
9476 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9477 echo_area_buffer[this_one] = Qnil;
9480 /* Choose a suitable buffer from echo_buffer[] is we don't
9481 have one. */
9482 if (NILP (echo_area_buffer[this_one]))
9484 echo_area_buffer[this_one]
9485 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9486 ? echo_buffer[the_other]
9487 : echo_buffer[this_one]);
9488 clear_buffer_p = 1;
9491 buffer = echo_area_buffer[this_one];
9493 /* Don't get confused by reusing the buffer used for echoing
9494 for a different purpose. */
9495 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9496 cancel_echoing ();
9498 record_unwind_protect (unwind_with_echo_area_buffer,
9499 with_echo_area_buffer_unwind_data (w));
9501 /* Make the echo area buffer current. Note that for display
9502 purposes, it is not necessary that the displayed window's buffer
9503 == current_buffer, except for text property lookup. So, let's
9504 only set that buffer temporarily here without doing a full
9505 Fset_window_buffer. We must also change w->pointm, though,
9506 because otherwise an assertions in unshow_buffer fails, and Emacs
9507 aborts. */
9508 set_buffer_internal_1 (XBUFFER (buffer));
9509 if (w)
9511 w->buffer = buffer;
9512 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9515 BVAR (current_buffer, undo_list) = Qt;
9516 BVAR (current_buffer, read_only) = Qnil;
9517 specbind (Qinhibit_read_only, Qt);
9518 specbind (Qinhibit_modification_hooks, Qt);
9520 if (clear_buffer_p && Z > BEG)
9521 del_range (BEG, Z);
9523 xassert (BEGV >= BEG);
9524 xassert (ZV <= Z && ZV >= BEGV);
9526 rc = fn (a1, a2, a3, a4);
9528 xassert (BEGV >= BEG);
9529 xassert (ZV <= Z && ZV >= BEGV);
9531 unbind_to (count, Qnil);
9532 return rc;
9536 /* Save state that should be preserved around the call to the function
9537 FN called in with_echo_area_buffer. */
9539 static Lisp_Object
9540 with_echo_area_buffer_unwind_data (struct window *w)
9542 int i = 0;
9543 Lisp_Object vector, tmp;
9545 /* Reduce consing by keeping one vector in
9546 Vwith_echo_area_save_vector. */
9547 vector = Vwith_echo_area_save_vector;
9548 Vwith_echo_area_save_vector = Qnil;
9550 if (NILP (vector))
9551 vector = Fmake_vector (make_number (7), Qnil);
9553 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9554 ASET (vector, i, Vdeactivate_mark); ++i;
9555 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9557 if (w)
9559 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9560 ASET (vector, i, w->buffer); ++i;
9561 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9562 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9564 else
9566 int end = i + 4;
9567 for (; i < end; ++i)
9568 ASET (vector, i, Qnil);
9571 xassert (i == ASIZE (vector));
9572 return vector;
9576 /* Restore global state from VECTOR which was created by
9577 with_echo_area_buffer_unwind_data. */
9579 static Lisp_Object
9580 unwind_with_echo_area_buffer (Lisp_Object vector)
9582 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9583 Vdeactivate_mark = AREF (vector, 1);
9584 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9586 if (WINDOWP (AREF (vector, 3)))
9588 struct window *w;
9589 Lisp_Object buffer, charpos, bytepos;
9591 w = XWINDOW (AREF (vector, 3));
9592 buffer = AREF (vector, 4);
9593 charpos = AREF (vector, 5);
9594 bytepos = AREF (vector, 6);
9596 w->buffer = buffer;
9597 set_marker_both (w->pointm, buffer,
9598 XFASTINT (charpos), XFASTINT (bytepos));
9601 Vwith_echo_area_save_vector = vector;
9602 return Qnil;
9606 /* Set up the echo area for use by print functions. MULTIBYTE_P
9607 non-zero means we will print multibyte. */
9609 void
9610 setup_echo_area_for_printing (int multibyte_p)
9612 /* If we can't find an echo area any more, exit. */
9613 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9614 Fkill_emacs (Qnil);
9616 ensure_echo_area_buffers ();
9618 if (!message_buf_print)
9620 /* A message has been output since the last time we printed.
9621 Choose a fresh echo area buffer. */
9622 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9623 echo_area_buffer[0] = echo_buffer[1];
9624 else
9625 echo_area_buffer[0] = echo_buffer[0];
9627 /* Switch to that buffer and clear it. */
9628 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9629 BVAR (current_buffer, truncate_lines) = Qnil;
9631 if (Z > BEG)
9633 int count = SPECPDL_INDEX ();
9634 specbind (Qinhibit_read_only, Qt);
9635 /* Note that undo recording is always disabled. */
9636 del_range (BEG, Z);
9637 unbind_to (count, Qnil);
9639 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9641 /* Set up the buffer for the multibyteness we need. */
9642 if (multibyte_p
9643 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9644 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9646 /* Raise the frame containing the echo area. */
9647 if (minibuffer_auto_raise)
9649 struct frame *sf = SELECTED_FRAME ();
9650 Lisp_Object mini_window;
9651 mini_window = FRAME_MINIBUF_WINDOW (sf);
9652 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9655 message_log_maybe_newline ();
9656 message_buf_print = 1;
9658 else
9660 if (NILP (echo_area_buffer[0]))
9662 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9663 echo_area_buffer[0] = echo_buffer[1];
9664 else
9665 echo_area_buffer[0] = echo_buffer[0];
9668 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9670 /* Someone switched buffers between print requests. */
9671 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9672 BVAR (current_buffer, truncate_lines) = Qnil;
9678 /* Display an echo area message in window W. Value is non-zero if W's
9679 height is changed. If display_last_displayed_message_p is
9680 non-zero, display the message that was last displayed, otherwise
9681 display the current message. */
9683 static int
9684 display_echo_area (struct window *w)
9686 int i, no_message_p, window_height_changed_p, count;
9688 /* Temporarily disable garbage collections while displaying the echo
9689 area. This is done because a GC can print a message itself.
9690 That message would modify the echo area buffer's contents while a
9691 redisplay of the buffer is going on, and seriously confuse
9692 redisplay. */
9693 count = inhibit_garbage_collection ();
9695 /* If there is no message, we must call display_echo_area_1
9696 nevertheless because it resizes the window. But we will have to
9697 reset the echo_area_buffer in question to nil at the end because
9698 with_echo_area_buffer will sets it to an empty buffer. */
9699 i = display_last_displayed_message_p ? 1 : 0;
9700 no_message_p = NILP (echo_area_buffer[i]);
9702 window_height_changed_p
9703 = with_echo_area_buffer (w, display_last_displayed_message_p,
9704 display_echo_area_1,
9705 (intptr_t) w, Qnil, 0, 0);
9707 if (no_message_p)
9708 echo_area_buffer[i] = Qnil;
9710 unbind_to (count, Qnil);
9711 return window_height_changed_p;
9715 /* Helper for display_echo_area. Display the current buffer which
9716 contains the current echo area message in window W, a mini-window,
9717 a pointer to which is passed in A1. A2..A4 are currently not used.
9718 Change the height of W so that all of the message is displayed.
9719 Value is non-zero if height of W was changed. */
9721 static int
9722 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9724 intptr_t i1 = a1;
9725 struct window *w = (struct window *) i1;
9726 Lisp_Object window;
9727 struct text_pos start;
9728 int window_height_changed_p = 0;
9730 /* Do this before displaying, so that we have a large enough glyph
9731 matrix for the display. If we can't get enough space for the
9732 whole text, display the last N lines. That works by setting w->start. */
9733 window_height_changed_p = resize_mini_window (w, 0);
9735 /* Use the starting position chosen by resize_mini_window. */
9736 SET_TEXT_POS_FROM_MARKER (start, w->start);
9738 /* Display. */
9739 clear_glyph_matrix (w->desired_matrix);
9740 XSETWINDOW (window, w);
9741 try_window (window, start, 0);
9743 return window_height_changed_p;
9747 /* Resize the echo area window to exactly the size needed for the
9748 currently displayed message, if there is one. If a mini-buffer
9749 is active, don't shrink it. */
9751 void
9752 resize_echo_area_exactly (void)
9754 if (BUFFERP (echo_area_buffer[0])
9755 && WINDOWP (echo_area_window))
9757 struct window *w = XWINDOW (echo_area_window);
9758 int resized_p;
9759 Lisp_Object resize_exactly;
9761 if (minibuf_level == 0)
9762 resize_exactly = Qt;
9763 else
9764 resize_exactly = Qnil;
9766 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9767 (intptr_t) w, resize_exactly,
9768 0, 0);
9769 if (resized_p)
9771 ++windows_or_buffers_changed;
9772 ++update_mode_lines;
9773 redisplay_internal ();
9779 /* Callback function for with_echo_area_buffer, when used from
9780 resize_echo_area_exactly. A1 contains a pointer to the window to
9781 resize, EXACTLY non-nil means resize the mini-window exactly to the
9782 size of the text displayed. A3 and A4 are not used. Value is what
9783 resize_mini_window returns. */
9785 static int
9786 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9788 intptr_t i1 = a1;
9789 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9793 /* Resize mini-window W to fit the size of its contents. EXACT_P
9794 means size the window exactly to the size needed. Otherwise, it's
9795 only enlarged until W's buffer is empty.
9797 Set W->start to the right place to begin display. If the whole
9798 contents fit, start at the beginning. Otherwise, start so as
9799 to make the end of the contents appear. This is particularly
9800 important for y-or-n-p, but seems desirable generally.
9802 Value is non-zero if the window height has been changed. */
9805 resize_mini_window (struct window *w, int exact_p)
9807 struct frame *f = XFRAME (w->frame);
9808 int window_height_changed_p = 0;
9810 xassert (MINI_WINDOW_P (w));
9812 /* By default, start display at the beginning. */
9813 set_marker_both (w->start, w->buffer,
9814 BUF_BEGV (XBUFFER (w->buffer)),
9815 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9817 /* Don't resize windows while redisplaying a window; it would
9818 confuse redisplay functions when the size of the window they are
9819 displaying changes from under them. Such a resizing can happen,
9820 for instance, when which-func prints a long message while
9821 we are running fontification-functions. We're running these
9822 functions with safe_call which binds inhibit-redisplay to t. */
9823 if (!NILP (Vinhibit_redisplay))
9824 return 0;
9826 /* Nil means don't try to resize. */
9827 if (NILP (Vresize_mini_windows)
9828 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9829 return 0;
9831 if (!FRAME_MINIBUF_ONLY_P (f))
9833 struct it it;
9834 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9835 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9836 int height, max_height;
9837 int unit = FRAME_LINE_HEIGHT (f);
9838 struct text_pos start;
9839 struct buffer *old_current_buffer = NULL;
9841 if (current_buffer != XBUFFER (w->buffer))
9843 old_current_buffer = current_buffer;
9844 set_buffer_internal (XBUFFER (w->buffer));
9847 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9849 /* Compute the max. number of lines specified by the user. */
9850 if (FLOATP (Vmax_mini_window_height))
9851 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9852 else if (INTEGERP (Vmax_mini_window_height))
9853 max_height = XINT (Vmax_mini_window_height);
9854 else
9855 max_height = total_height / 4;
9857 /* Correct that max. height if it's bogus. */
9858 max_height = max (1, max_height);
9859 max_height = min (total_height, max_height);
9861 /* Find out the height of the text in the window. */
9862 if (it.line_wrap == TRUNCATE)
9863 height = 1;
9864 else
9866 last_height = 0;
9867 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9868 if (it.max_ascent == 0 && it.max_descent == 0)
9869 height = it.current_y + last_height;
9870 else
9871 height = it.current_y + it.max_ascent + it.max_descent;
9872 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9873 height = (height + unit - 1) / unit;
9876 /* Compute a suitable window start. */
9877 if (height > max_height)
9879 height = max_height;
9880 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9881 move_it_vertically_backward (&it, (height - 1) * unit);
9882 start = it.current.pos;
9884 else
9885 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9886 SET_MARKER_FROM_TEXT_POS (w->start, start);
9888 if (EQ (Vresize_mini_windows, Qgrow_only))
9890 /* Let it grow only, until we display an empty message, in which
9891 case the window shrinks again. */
9892 if (height > WINDOW_TOTAL_LINES (w))
9894 int old_height = WINDOW_TOTAL_LINES (w);
9895 freeze_window_starts (f, 1);
9896 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9897 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9899 else if (height < WINDOW_TOTAL_LINES (w)
9900 && (exact_p || BEGV == ZV))
9902 int old_height = WINDOW_TOTAL_LINES (w);
9903 freeze_window_starts (f, 0);
9904 shrink_mini_window (w);
9905 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9908 else
9910 /* Always resize to exact size needed. */
9911 if (height > WINDOW_TOTAL_LINES (w))
9913 int old_height = WINDOW_TOTAL_LINES (w);
9914 freeze_window_starts (f, 1);
9915 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9916 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9918 else if (height < WINDOW_TOTAL_LINES (w))
9920 int old_height = WINDOW_TOTAL_LINES (w);
9921 freeze_window_starts (f, 0);
9922 shrink_mini_window (w);
9924 if (height)
9926 freeze_window_starts (f, 1);
9927 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9930 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9934 if (old_current_buffer)
9935 set_buffer_internal (old_current_buffer);
9938 return window_height_changed_p;
9942 /* Value is the current message, a string, or nil if there is no
9943 current message. */
9945 Lisp_Object
9946 current_message (void)
9948 Lisp_Object msg;
9950 if (!BUFFERP (echo_area_buffer[0]))
9951 msg = Qnil;
9952 else
9954 with_echo_area_buffer (0, 0, current_message_1,
9955 (intptr_t) &msg, Qnil, 0, 0);
9956 if (NILP (msg))
9957 echo_area_buffer[0] = Qnil;
9960 return msg;
9964 static int
9965 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9967 intptr_t i1 = a1;
9968 Lisp_Object *msg = (Lisp_Object *) i1;
9970 if (Z > BEG)
9971 *msg = make_buffer_string (BEG, Z, 1);
9972 else
9973 *msg = Qnil;
9974 return 0;
9978 /* Push the current message on Vmessage_stack for later restauration
9979 by restore_message. Value is non-zero if the current message isn't
9980 empty. This is a relatively infrequent operation, so it's not
9981 worth optimizing. */
9984 push_message (void)
9986 Lisp_Object msg;
9987 msg = current_message ();
9988 Vmessage_stack = Fcons (msg, Vmessage_stack);
9989 return STRINGP (msg);
9993 /* Restore message display from the top of Vmessage_stack. */
9995 void
9996 restore_message (void)
9998 Lisp_Object msg;
10000 xassert (CONSP (Vmessage_stack));
10001 msg = XCAR (Vmessage_stack);
10002 if (STRINGP (msg))
10003 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10004 else
10005 message3_nolog (msg, 0, 0);
10009 /* Handler for record_unwind_protect calling pop_message. */
10011 Lisp_Object
10012 pop_message_unwind (Lisp_Object dummy)
10014 pop_message ();
10015 return Qnil;
10018 /* Pop the top-most entry off Vmessage_stack. */
10020 static void
10021 pop_message (void)
10023 xassert (CONSP (Vmessage_stack));
10024 Vmessage_stack = XCDR (Vmessage_stack);
10028 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10029 exits. If the stack is not empty, we have a missing pop_message
10030 somewhere. */
10032 void
10033 check_message_stack (void)
10035 if (!NILP (Vmessage_stack))
10036 abort ();
10040 /* Truncate to NCHARS what will be displayed in the echo area the next
10041 time we display it---but don't redisplay it now. */
10043 void
10044 truncate_echo_area (EMACS_INT nchars)
10046 if (nchars == 0)
10047 echo_area_buffer[0] = Qnil;
10048 /* A null message buffer means that the frame hasn't really been
10049 initialized yet. Error messages get reported properly by
10050 cmd_error, so this must be just an informative message; toss it. */
10051 else if (!noninteractive
10052 && INTERACTIVE
10053 && !NILP (echo_area_buffer[0]))
10055 struct frame *sf = SELECTED_FRAME ();
10056 if (FRAME_MESSAGE_BUF (sf))
10057 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10062 /* Helper function for truncate_echo_area. Truncate the current
10063 message to at most NCHARS characters. */
10065 static int
10066 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10068 if (BEG + nchars < Z)
10069 del_range (BEG + nchars, Z);
10070 if (Z == BEG)
10071 echo_area_buffer[0] = Qnil;
10072 return 0;
10076 /* Set the current message to a substring of S or STRING.
10078 If STRING is a Lisp string, set the message to the first NBYTES
10079 bytes from STRING. NBYTES zero means use the whole string. If
10080 STRING is multibyte, the message will be displayed multibyte.
10082 If S is not null, set the message to the first LEN bytes of S. LEN
10083 zero means use the whole string. MULTIBYTE_P non-zero means S is
10084 multibyte. Display the message multibyte in that case.
10086 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10087 to t before calling set_message_1 (which calls insert).
10090 static void
10091 set_message (const char *s, Lisp_Object string,
10092 EMACS_INT nbytes, int multibyte_p)
10094 message_enable_multibyte
10095 = ((s && multibyte_p)
10096 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10098 with_echo_area_buffer (0, -1, set_message_1,
10099 (intptr_t) s, string, nbytes, multibyte_p);
10100 message_buf_print = 0;
10101 help_echo_showing_p = 0;
10105 /* Helper function for set_message. Arguments have the same meaning
10106 as there, with A1 corresponding to S and A2 corresponding to STRING
10107 This function is called with the echo area buffer being
10108 current. */
10110 static int
10111 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10113 intptr_t i1 = a1;
10114 const char *s = (const char *) i1;
10115 const unsigned char *msg = (const unsigned char *) s;
10116 Lisp_Object string = a2;
10118 /* Change multibyteness of the echo buffer appropriately. */
10119 if (message_enable_multibyte
10120 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10121 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10123 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10124 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10125 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10127 /* Insert new message at BEG. */
10128 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10130 if (STRINGP (string))
10132 EMACS_INT nchars;
10134 if (nbytes == 0)
10135 nbytes = SBYTES (string);
10136 nchars = string_byte_to_char (string, nbytes);
10138 /* This function takes care of single/multibyte conversion. We
10139 just have to ensure that the echo area buffer has the right
10140 setting of enable_multibyte_characters. */
10141 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10143 else if (s)
10145 if (nbytes == 0)
10146 nbytes = strlen (s);
10148 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10150 /* Convert from multi-byte to single-byte. */
10151 EMACS_INT i;
10152 int c, n;
10153 char work[1];
10155 /* Convert a multibyte string to single-byte. */
10156 for (i = 0; i < nbytes; i += n)
10158 c = string_char_and_length (msg + i, &n);
10159 work[0] = (ASCII_CHAR_P (c)
10161 : multibyte_char_to_unibyte (c));
10162 insert_1_both (work, 1, 1, 1, 0, 0);
10165 else if (!multibyte_p
10166 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10168 /* Convert from single-byte to multi-byte. */
10169 EMACS_INT i;
10170 int c, n;
10171 unsigned char str[MAX_MULTIBYTE_LENGTH];
10173 /* Convert a single-byte string to multibyte. */
10174 for (i = 0; i < nbytes; i++)
10176 c = msg[i];
10177 MAKE_CHAR_MULTIBYTE (c);
10178 n = CHAR_STRING (c, str);
10179 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10182 else
10183 insert_1 (s, nbytes, 1, 0, 0);
10186 return 0;
10190 /* Clear messages. CURRENT_P non-zero means clear the current
10191 message. LAST_DISPLAYED_P non-zero means clear the message
10192 last displayed. */
10194 void
10195 clear_message (int current_p, int last_displayed_p)
10197 if (current_p)
10199 echo_area_buffer[0] = Qnil;
10200 message_cleared_p = 1;
10203 if (last_displayed_p)
10204 echo_area_buffer[1] = Qnil;
10206 message_buf_print = 0;
10209 /* Clear garbaged frames.
10211 This function is used where the old redisplay called
10212 redraw_garbaged_frames which in turn called redraw_frame which in
10213 turn called clear_frame. The call to clear_frame was a source of
10214 flickering. I believe a clear_frame is not necessary. It should
10215 suffice in the new redisplay to invalidate all current matrices,
10216 and ensure a complete redisplay of all windows. */
10218 static void
10219 clear_garbaged_frames (void)
10221 if (frame_garbaged)
10223 Lisp_Object tail, frame;
10224 int changed_count = 0;
10226 FOR_EACH_FRAME (tail, frame)
10228 struct frame *f = XFRAME (frame);
10230 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10232 if (f->resized_p)
10234 Fredraw_frame (frame);
10235 f->force_flush_display_p = 1;
10237 clear_current_matrices (f);
10238 changed_count++;
10239 f->garbaged = 0;
10240 f->resized_p = 0;
10244 frame_garbaged = 0;
10245 if (changed_count)
10246 ++windows_or_buffers_changed;
10251 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10252 is non-zero update selected_frame. Value is non-zero if the
10253 mini-windows height has been changed. */
10255 static int
10256 echo_area_display (int update_frame_p)
10258 Lisp_Object mini_window;
10259 struct window *w;
10260 struct frame *f;
10261 int window_height_changed_p = 0;
10262 struct frame *sf = SELECTED_FRAME ();
10264 mini_window = FRAME_MINIBUF_WINDOW (sf);
10265 w = XWINDOW (mini_window);
10266 f = XFRAME (WINDOW_FRAME (w));
10268 /* Don't display if frame is invisible or not yet initialized. */
10269 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10270 return 0;
10272 #ifdef HAVE_WINDOW_SYSTEM
10273 /* When Emacs starts, selected_frame may be the initial terminal
10274 frame. If we let this through, a message would be displayed on
10275 the terminal. */
10276 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10277 return 0;
10278 #endif /* HAVE_WINDOW_SYSTEM */
10280 /* Redraw garbaged frames. */
10281 if (frame_garbaged)
10282 clear_garbaged_frames ();
10284 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10286 echo_area_window = mini_window;
10287 window_height_changed_p = display_echo_area (w);
10288 w->must_be_updated_p = 1;
10290 /* Update the display, unless called from redisplay_internal.
10291 Also don't update the screen during redisplay itself. The
10292 update will happen at the end of redisplay, and an update
10293 here could cause confusion. */
10294 if (update_frame_p && !redisplaying_p)
10296 int n = 0;
10298 /* If the display update has been interrupted by pending
10299 input, update mode lines in the frame. Due to the
10300 pending input, it might have been that redisplay hasn't
10301 been called, so that mode lines above the echo area are
10302 garbaged. This looks odd, so we prevent it here. */
10303 if (!display_completed)
10304 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10306 if (window_height_changed_p
10307 /* Don't do this if Emacs is shutting down. Redisplay
10308 needs to run hooks. */
10309 && !NILP (Vrun_hooks))
10311 /* Must update other windows. Likewise as in other
10312 cases, don't let this update be interrupted by
10313 pending input. */
10314 int count = SPECPDL_INDEX ();
10315 specbind (Qredisplay_dont_pause, Qt);
10316 windows_or_buffers_changed = 1;
10317 redisplay_internal ();
10318 unbind_to (count, Qnil);
10320 else if (FRAME_WINDOW_P (f) && n == 0)
10322 /* Window configuration is the same as before.
10323 Can do with a display update of the echo area,
10324 unless we displayed some mode lines. */
10325 update_single_window (w, 1);
10326 FRAME_RIF (f)->flush_display (f);
10328 else
10329 update_frame (f, 1, 1);
10331 /* If cursor is in the echo area, make sure that the next
10332 redisplay displays the minibuffer, so that the cursor will
10333 be replaced with what the minibuffer wants. */
10334 if (cursor_in_echo_area)
10335 ++windows_or_buffers_changed;
10338 else if (!EQ (mini_window, selected_window))
10339 windows_or_buffers_changed++;
10341 /* Last displayed message is now the current message. */
10342 echo_area_buffer[1] = echo_area_buffer[0];
10343 /* Inform read_char that we're not echoing. */
10344 echo_message_buffer = Qnil;
10346 /* Prevent redisplay optimization in redisplay_internal by resetting
10347 this_line_start_pos. This is done because the mini-buffer now
10348 displays the message instead of its buffer text. */
10349 if (EQ (mini_window, selected_window))
10350 CHARPOS (this_line_start_pos) = 0;
10352 return window_height_changed_p;
10357 /***********************************************************************
10358 Mode Lines and Frame Titles
10359 ***********************************************************************/
10361 /* A buffer for constructing non-propertized mode-line strings and
10362 frame titles in it; allocated from the heap in init_xdisp and
10363 resized as needed in store_mode_line_noprop_char. */
10365 static char *mode_line_noprop_buf;
10367 /* The buffer's end, and a current output position in it. */
10369 static char *mode_line_noprop_buf_end;
10370 static char *mode_line_noprop_ptr;
10372 #define MODE_LINE_NOPROP_LEN(start) \
10373 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10375 static enum {
10376 MODE_LINE_DISPLAY = 0,
10377 MODE_LINE_TITLE,
10378 MODE_LINE_NOPROP,
10379 MODE_LINE_STRING
10380 } mode_line_target;
10382 /* Alist that caches the results of :propertize.
10383 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10384 static Lisp_Object mode_line_proptrans_alist;
10386 /* List of strings making up the mode-line. */
10387 static Lisp_Object mode_line_string_list;
10389 /* Base face property when building propertized mode line string. */
10390 static Lisp_Object mode_line_string_face;
10391 static Lisp_Object mode_line_string_face_prop;
10394 /* Unwind data for mode line strings */
10396 static Lisp_Object Vmode_line_unwind_vector;
10398 static Lisp_Object
10399 format_mode_line_unwind_data (struct buffer *obuf,
10400 Lisp_Object owin,
10401 int save_proptrans)
10403 Lisp_Object vector, tmp;
10405 /* Reduce consing by keeping one vector in
10406 Vwith_echo_area_save_vector. */
10407 vector = Vmode_line_unwind_vector;
10408 Vmode_line_unwind_vector = Qnil;
10410 if (NILP (vector))
10411 vector = Fmake_vector (make_number (8), Qnil);
10413 ASET (vector, 0, make_number (mode_line_target));
10414 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10415 ASET (vector, 2, mode_line_string_list);
10416 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10417 ASET (vector, 4, mode_line_string_face);
10418 ASET (vector, 5, mode_line_string_face_prop);
10420 if (obuf)
10421 XSETBUFFER (tmp, obuf);
10422 else
10423 tmp = Qnil;
10424 ASET (vector, 6, tmp);
10425 ASET (vector, 7, owin);
10427 return vector;
10430 static Lisp_Object
10431 unwind_format_mode_line (Lisp_Object vector)
10433 mode_line_target = XINT (AREF (vector, 0));
10434 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10435 mode_line_string_list = AREF (vector, 2);
10436 if (! EQ (AREF (vector, 3), Qt))
10437 mode_line_proptrans_alist = AREF (vector, 3);
10438 mode_line_string_face = AREF (vector, 4);
10439 mode_line_string_face_prop = AREF (vector, 5);
10441 if (!NILP (AREF (vector, 7)))
10442 /* Select window before buffer, since it may change the buffer. */
10443 Fselect_window (AREF (vector, 7), Qt);
10445 if (!NILP (AREF (vector, 6)))
10447 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10448 ASET (vector, 6, Qnil);
10451 Vmode_line_unwind_vector = vector;
10452 return Qnil;
10456 /* Store a single character C for the frame title in mode_line_noprop_buf.
10457 Re-allocate mode_line_noprop_buf if necessary. */
10459 static void
10460 store_mode_line_noprop_char (char c)
10462 /* If output position has reached the end of the allocated buffer,
10463 double the buffer's size. */
10464 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10466 int len = MODE_LINE_NOPROP_LEN (0);
10467 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10468 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10469 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10470 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10473 *mode_line_noprop_ptr++ = c;
10477 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10478 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10479 characters that yield more columns than PRECISION; PRECISION <= 0
10480 means copy the whole string. Pad with spaces until FIELD_WIDTH
10481 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10482 pad. Called from display_mode_element when it is used to build a
10483 frame title. */
10485 static int
10486 store_mode_line_noprop (const char *string, int field_width, int precision)
10488 const unsigned char *str = (const unsigned char *) string;
10489 int n = 0;
10490 EMACS_INT dummy, nbytes;
10492 /* Copy at most PRECISION chars from STR. */
10493 nbytes = strlen (string);
10494 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10495 while (nbytes--)
10496 store_mode_line_noprop_char (*str++);
10498 /* Fill up with spaces until FIELD_WIDTH reached. */
10499 while (field_width > 0
10500 && n < field_width)
10502 store_mode_line_noprop_char (' ');
10503 ++n;
10506 return n;
10509 /***********************************************************************
10510 Frame Titles
10511 ***********************************************************************/
10513 #ifdef HAVE_WINDOW_SYSTEM
10515 /* Set the title of FRAME, if it has changed. The title format is
10516 Vicon_title_format if FRAME is iconified, otherwise it is
10517 frame_title_format. */
10519 static void
10520 x_consider_frame_title (Lisp_Object frame)
10522 struct frame *f = XFRAME (frame);
10524 if (FRAME_WINDOW_P (f)
10525 || FRAME_MINIBUF_ONLY_P (f)
10526 || f->explicit_name)
10528 /* Do we have more than one visible frame on this X display? */
10529 Lisp_Object tail;
10530 Lisp_Object fmt;
10531 int title_start;
10532 char *title;
10533 int len;
10534 struct it it;
10535 int count = SPECPDL_INDEX ();
10537 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10539 Lisp_Object other_frame = XCAR (tail);
10540 struct frame *tf = XFRAME (other_frame);
10542 if (tf != f
10543 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10544 && !FRAME_MINIBUF_ONLY_P (tf)
10545 && !EQ (other_frame, tip_frame)
10546 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10547 break;
10550 /* Set global variable indicating that multiple frames exist. */
10551 multiple_frames = CONSP (tail);
10553 /* Switch to the buffer of selected window of the frame. Set up
10554 mode_line_target so that display_mode_element will output into
10555 mode_line_noprop_buf; then display the title. */
10556 record_unwind_protect (unwind_format_mode_line,
10557 format_mode_line_unwind_data
10558 (current_buffer, selected_window, 0));
10560 Fselect_window (f->selected_window, Qt);
10561 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10562 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10564 mode_line_target = MODE_LINE_TITLE;
10565 title_start = MODE_LINE_NOPROP_LEN (0);
10566 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10567 NULL, DEFAULT_FACE_ID);
10568 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10569 len = MODE_LINE_NOPROP_LEN (title_start);
10570 title = mode_line_noprop_buf + title_start;
10571 unbind_to (count, Qnil);
10573 /* Set the title only if it's changed. This avoids consing in
10574 the common case where it hasn't. (If it turns out that we've
10575 already wasted too much time by walking through the list with
10576 display_mode_element, then we might need to optimize at a
10577 higher level than this.) */
10578 if (! STRINGP (f->name)
10579 || SBYTES (f->name) != len
10580 || memcmp (title, SDATA (f->name), len) != 0)
10581 x_implicitly_set_name (f, make_string (title, len), Qnil);
10585 #endif /* not HAVE_WINDOW_SYSTEM */
10590 /***********************************************************************
10591 Menu Bars
10592 ***********************************************************************/
10595 /* Prepare for redisplay by updating menu-bar item lists when
10596 appropriate. This can call eval. */
10598 void
10599 prepare_menu_bars (void)
10601 int all_windows;
10602 struct gcpro gcpro1, gcpro2;
10603 struct frame *f;
10604 Lisp_Object tooltip_frame;
10606 #ifdef HAVE_WINDOW_SYSTEM
10607 tooltip_frame = tip_frame;
10608 #else
10609 tooltip_frame = Qnil;
10610 #endif
10612 /* Update all frame titles based on their buffer names, etc. We do
10613 this before the menu bars so that the buffer-menu will show the
10614 up-to-date frame titles. */
10615 #ifdef HAVE_WINDOW_SYSTEM
10616 if (windows_or_buffers_changed || update_mode_lines)
10618 Lisp_Object tail, frame;
10620 FOR_EACH_FRAME (tail, frame)
10622 f = XFRAME (frame);
10623 if (!EQ (frame, tooltip_frame)
10624 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10625 x_consider_frame_title (frame);
10628 #endif /* HAVE_WINDOW_SYSTEM */
10630 /* Update the menu bar item lists, if appropriate. This has to be
10631 done before any actual redisplay or generation of display lines. */
10632 all_windows = (update_mode_lines
10633 || buffer_shared > 1
10634 || windows_or_buffers_changed);
10635 if (all_windows)
10637 Lisp_Object tail, frame;
10638 int count = SPECPDL_INDEX ();
10639 /* 1 means that update_menu_bar has run its hooks
10640 so any further calls to update_menu_bar shouldn't do so again. */
10641 int menu_bar_hooks_run = 0;
10643 record_unwind_save_match_data ();
10645 FOR_EACH_FRAME (tail, frame)
10647 f = XFRAME (frame);
10649 /* Ignore tooltip frame. */
10650 if (EQ (frame, tooltip_frame))
10651 continue;
10653 /* If a window on this frame changed size, report that to
10654 the user and clear the size-change flag. */
10655 if (FRAME_WINDOW_SIZES_CHANGED (f))
10657 Lisp_Object functions;
10659 /* Clear flag first in case we get an error below. */
10660 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10661 functions = Vwindow_size_change_functions;
10662 GCPRO2 (tail, functions);
10664 while (CONSP (functions))
10666 if (!EQ (XCAR (functions), Qt))
10667 call1 (XCAR (functions), frame);
10668 functions = XCDR (functions);
10670 UNGCPRO;
10673 GCPRO1 (tail);
10674 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10675 #ifdef HAVE_WINDOW_SYSTEM
10676 update_tool_bar (f, 0);
10677 #endif
10678 #ifdef HAVE_NS
10679 if (windows_or_buffers_changed
10680 && FRAME_NS_P (f))
10681 ns_set_doc_edited (f, Fbuffer_modified_p
10682 (XWINDOW (f->selected_window)->buffer));
10683 #endif
10684 UNGCPRO;
10687 unbind_to (count, Qnil);
10689 else
10691 struct frame *sf = SELECTED_FRAME ();
10692 update_menu_bar (sf, 1, 0);
10693 #ifdef HAVE_WINDOW_SYSTEM
10694 update_tool_bar (sf, 1);
10695 #endif
10700 /* Update the menu bar item list for frame F. This has to be done
10701 before we start to fill in any display lines, because it can call
10702 eval.
10704 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10706 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10707 already ran the menu bar hooks for this redisplay, so there
10708 is no need to run them again. The return value is the
10709 updated value of this flag, to pass to the next call. */
10711 static int
10712 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10714 Lisp_Object window;
10715 register struct window *w;
10717 /* If called recursively during a menu update, do nothing. This can
10718 happen when, for instance, an activate-menubar-hook causes a
10719 redisplay. */
10720 if (inhibit_menubar_update)
10721 return hooks_run;
10723 window = FRAME_SELECTED_WINDOW (f);
10724 w = XWINDOW (window);
10726 if (FRAME_WINDOW_P (f)
10728 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10729 || defined (HAVE_NS) || defined (USE_GTK)
10730 FRAME_EXTERNAL_MENU_BAR (f)
10731 #else
10732 FRAME_MENU_BAR_LINES (f) > 0
10733 #endif
10734 : FRAME_MENU_BAR_LINES (f) > 0)
10736 /* If the user has switched buffers or windows, we need to
10737 recompute to reflect the new bindings. But we'll
10738 recompute when update_mode_lines is set too; that means
10739 that people can use force-mode-line-update to request
10740 that the menu bar be recomputed. The adverse effect on
10741 the rest of the redisplay algorithm is about the same as
10742 windows_or_buffers_changed anyway. */
10743 if (windows_or_buffers_changed
10744 /* This used to test w->update_mode_line, but we believe
10745 there is no need to recompute the menu in that case. */
10746 || update_mode_lines
10747 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10748 < BUF_MODIFF (XBUFFER (w->buffer)))
10749 != !NILP (w->last_had_star))
10750 || ((!NILP (Vtransient_mark_mode)
10751 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10752 != !NILP (w->region_showing)))
10754 struct buffer *prev = current_buffer;
10755 int count = SPECPDL_INDEX ();
10757 specbind (Qinhibit_menubar_update, Qt);
10759 set_buffer_internal_1 (XBUFFER (w->buffer));
10760 if (save_match_data)
10761 record_unwind_save_match_data ();
10762 if (NILP (Voverriding_local_map_menu_flag))
10764 specbind (Qoverriding_terminal_local_map, Qnil);
10765 specbind (Qoverriding_local_map, Qnil);
10768 if (!hooks_run)
10770 /* Run the Lucid hook. */
10771 safe_run_hooks (Qactivate_menubar_hook);
10773 /* If it has changed current-menubar from previous value,
10774 really recompute the menu-bar from the value. */
10775 if (! NILP (Vlucid_menu_bar_dirty_flag))
10776 call0 (Qrecompute_lucid_menubar);
10778 safe_run_hooks (Qmenu_bar_update_hook);
10780 hooks_run = 1;
10783 XSETFRAME (Vmenu_updating_frame, f);
10784 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10786 /* Redisplay the menu bar in case we changed it. */
10787 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10788 || defined (HAVE_NS) || defined (USE_GTK)
10789 if (FRAME_WINDOW_P (f))
10791 #if defined (HAVE_NS)
10792 /* All frames on Mac OS share the same menubar. So only
10793 the selected frame should be allowed to set it. */
10794 if (f == SELECTED_FRAME ())
10795 #endif
10796 set_frame_menubar (f, 0, 0);
10798 else
10799 /* On a terminal screen, the menu bar is an ordinary screen
10800 line, and this makes it get updated. */
10801 w->update_mode_line = Qt;
10802 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10803 /* In the non-toolkit version, the menu bar is an ordinary screen
10804 line, and this makes it get updated. */
10805 w->update_mode_line = Qt;
10806 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10808 unbind_to (count, Qnil);
10809 set_buffer_internal_1 (prev);
10813 return hooks_run;
10818 /***********************************************************************
10819 Output Cursor
10820 ***********************************************************************/
10822 #ifdef HAVE_WINDOW_SYSTEM
10824 /* EXPORT:
10825 Nominal cursor position -- where to draw output.
10826 HPOS and VPOS are window relative glyph matrix coordinates.
10827 X and Y are window relative pixel coordinates. */
10829 struct cursor_pos output_cursor;
10832 /* EXPORT:
10833 Set the global variable output_cursor to CURSOR. All cursor
10834 positions are relative to updated_window. */
10836 void
10837 set_output_cursor (struct cursor_pos *cursor)
10839 output_cursor.hpos = cursor->hpos;
10840 output_cursor.vpos = cursor->vpos;
10841 output_cursor.x = cursor->x;
10842 output_cursor.y = cursor->y;
10846 /* EXPORT for RIF:
10847 Set a nominal cursor position.
10849 HPOS and VPOS are column/row positions in a window glyph matrix. X
10850 and Y are window text area relative pixel positions.
10852 If this is done during an update, updated_window will contain the
10853 window that is being updated and the position is the future output
10854 cursor position for that window. If updated_window is null, use
10855 selected_window and display the cursor at the given position. */
10857 void
10858 x_cursor_to (int vpos, int hpos, int y, int x)
10860 struct window *w;
10862 /* If updated_window is not set, work on selected_window. */
10863 if (updated_window)
10864 w = updated_window;
10865 else
10866 w = XWINDOW (selected_window);
10868 /* Set the output cursor. */
10869 output_cursor.hpos = hpos;
10870 output_cursor.vpos = vpos;
10871 output_cursor.x = x;
10872 output_cursor.y = y;
10874 /* If not called as part of an update, really display the cursor.
10875 This will also set the cursor position of W. */
10876 if (updated_window == NULL)
10878 BLOCK_INPUT;
10879 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10880 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10881 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10882 UNBLOCK_INPUT;
10886 #endif /* HAVE_WINDOW_SYSTEM */
10889 /***********************************************************************
10890 Tool-bars
10891 ***********************************************************************/
10893 #ifdef HAVE_WINDOW_SYSTEM
10895 /* Where the mouse was last time we reported a mouse event. */
10897 FRAME_PTR last_mouse_frame;
10899 /* Tool-bar item index of the item on which a mouse button was pressed
10900 or -1. */
10902 int last_tool_bar_item;
10905 static Lisp_Object
10906 update_tool_bar_unwind (Lisp_Object frame)
10908 selected_frame = frame;
10909 return Qnil;
10912 /* Update the tool-bar item list for frame F. This has to be done
10913 before we start to fill in any display lines. Called from
10914 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10915 and restore it here. */
10917 static void
10918 update_tool_bar (struct frame *f, int save_match_data)
10920 #if defined (USE_GTK) || defined (HAVE_NS)
10921 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10922 #else
10923 int do_update = WINDOWP (f->tool_bar_window)
10924 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10925 #endif
10927 if (do_update)
10929 Lisp_Object window;
10930 struct window *w;
10932 window = FRAME_SELECTED_WINDOW (f);
10933 w = XWINDOW (window);
10935 /* If the user has switched buffers or windows, we need to
10936 recompute to reflect the new bindings. But we'll
10937 recompute when update_mode_lines is set too; that means
10938 that people can use force-mode-line-update to request
10939 that the menu bar be recomputed. The adverse effect on
10940 the rest of the redisplay algorithm is about the same as
10941 windows_or_buffers_changed anyway. */
10942 if (windows_or_buffers_changed
10943 || !NILP (w->update_mode_line)
10944 || update_mode_lines
10945 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10946 < BUF_MODIFF (XBUFFER (w->buffer)))
10947 != !NILP (w->last_had_star))
10948 || ((!NILP (Vtransient_mark_mode)
10949 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10950 != !NILP (w->region_showing)))
10952 struct buffer *prev = current_buffer;
10953 int count = SPECPDL_INDEX ();
10954 Lisp_Object frame, new_tool_bar;
10955 int new_n_tool_bar;
10956 struct gcpro gcpro1;
10958 /* Set current_buffer to the buffer of the selected
10959 window of the frame, so that we get the right local
10960 keymaps. */
10961 set_buffer_internal_1 (XBUFFER (w->buffer));
10963 /* Save match data, if we must. */
10964 if (save_match_data)
10965 record_unwind_save_match_data ();
10967 /* Make sure that we don't accidentally use bogus keymaps. */
10968 if (NILP (Voverriding_local_map_menu_flag))
10970 specbind (Qoverriding_terminal_local_map, Qnil);
10971 specbind (Qoverriding_local_map, Qnil);
10974 GCPRO1 (new_tool_bar);
10976 /* We must temporarily set the selected frame to this frame
10977 before calling tool_bar_items, because the calculation of
10978 the tool-bar keymap uses the selected frame (see
10979 `tool-bar-make-keymap' in tool-bar.el). */
10980 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10981 XSETFRAME (frame, f);
10982 selected_frame = frame;
10984 /* Build desired tool-bar items from keymaps. */
10985 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10986 &new_n_tool_bar);
10988 /* Redisplay the tool-bar if we changed it. */
10989 if (new_n_tool_bar != f->n_tool_bar_items
10990 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10992 /* Redisplay that happens asynchronously due to an expose event
10993 may access f->tool_bar_items. Make sure we update both
10994 variables within BLOCK_INPUT so no such event interrupts. */
10995 BLOCK_INPUT;
10996 f->tool_bar_items = new_tool_bar;
10997 f->n_tool_bar_items = new_n_tool_bar;
10998 w->update_mode_line = Qt;
10999 UNBLOCK_INPUT;
11002 UNGCPRO;
11004 unbind_to (count, Qnil);
11005 set_buffer_internal_1 (prev);
11011 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11012 F's desired tool-bar contents. F->tool_bar_items must have
11013 been set up previously by calling prepare_menu_bars. */
11015 static void
11016 build_desired_tool_bar_string (struct frame *f)
11018 int i, size, size_needed;
11019 struct gcpro gcpro1, gcpro2, gcpro3;
11020 Lisp_Object image, plist, props;
11022 image = plist = props = Qnil;
11023 GCPRO3 (image, plist, props);
11025 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11026 Otherwise, make a new string. */
11028 /* The size of the string we might be able to reuse. */
11029 size = (STRINGP (f->desired_tool_bar_string)
11030 ? SCHARS (f->desired_tool_bar_string)
11031 : 0);
11033 /* We need one space in the string for each image. */
11034 size_needed = f->n_tool_bar_items;
11036 /* Reuse f->desired_tool_bar_string, if possible. */
11037 if (size < size_needed || NILP (f->desired_tool_bar_string))
11038 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11039 make_number (' '));
11040 else
11042 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11043 Fremove_text_properties (make_number (0), make_number (size),
11044 props, f->desired_tool_bar_string);
11047 /* Put a `display' property on the string for the images to display,
11048 put a `menu_item' property on tool-bar items with a value that
11049 is the index of the item in F's tool-bar item vector. */
11050 for (i = 0; i < f->n_tool_bar_items; ++i)
11052 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11054 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11055 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11056 int hmargin, vmargin, relief, idx, end;
11058 /* If image is a vector, choose the image according to the
11059 button state. */
11060 image = PROP (TOOL_BAR_ITEM_IMAGES);
11061 if (VECTORP (image))
11063 if (enabled_p)
11064 idx = (selected_p
11065 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11066 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11067 else
11068 idx = (selected_p
11069 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11070 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11072 xassert (ASIZE (image) >= idx);
11073 image = AREF (image, idx);
11075 else
11076 idx = -1;
11078 /* Ignore invalid image specifications. */
11079 if (!valid_image_p (image))
11080 continue;
11082 /* Display the tool-bar button pressed, or depressed. */
11083 plist = Fcopy_sequence (XCDR (image));
11085 /* Compute margin and relief to draw. */
11086 relief = (tool_bar_button_relief >= 0
11087 ? tool_bar_button_relief
11088 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11089 hmargin = vmargin = relief;
11091 if (INTEGERP (Vtool_bar_button_margin)
11092 && XINT (Vtool_bar_button_margin) > 0)
11094 hmargin += XFASTINT (Vtool_bar_button_margin);
11095 vmargin += XFASTINT (Vtool_bar_button_margin);
11097 else if (CONSP (Vtool_bar_button_margin))
11099 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11100 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11101 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11103 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11104 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11105 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11108 if (auto_raise_tool_bar_buttons_p)
11110 /* Add a `:relief' property to the image spec if the item is
11111 selected. */
11112 if (selected_p)
11114 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11115 hmargin -= relief;
11116 vmargin -= relief;
11119 else
11121 /* If image is selected, display it pressed, i.e. with a
11122 negative relief. If it's not selected, display it with a
11123 raised relief. */
11124 plist = Fplist_put (plist, QCrelief,
11125 (selected_p
11126 ? make_number (-relief)
11127 : make_number (relief)));
11128 hmargin -= relief;
11129 vmargin -= relief;
11132 /* Put a margin around the image. */
11133 if (hmargin || vmargin)
11135 if (hmargin == vmargin)
11136 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11137 else
11138 plist = Fplist_put (plist, QCmargin,
11139 Fcons (make_number (hmargin),
11140 make_number (vmargin)));
11143 /* If button is not enabled, and we don't have special images
11144 for the disabled state, make the image appear disabled by
11145 applying an appropriate algorithm to it. */
11146 if (!enabled_p && idx < 0)
11147 plist = Fplist_put (plist, QCconversion, Qdisabled);
11149 /* Put a `display' text property on the string for the image to
11150 display. Put a `menu-item' property on the string that gives
11151 the start of this item's properties in the tool-bar items
11152 vector. */
11153 image = Fcons (Qimage, plist);
11154 props = list4 (Qdisplay, image,
11155 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11157 /* Let the last image hide all remaining spaces in the tool bar
11158 string. The string can be longer than needed when we reuse a
11159 previous string. */
11160 if (i + 1 == f->n_tool_bar_items)
11161 end = SCHARS (f->desired_tool_bar_string);
11162 else
11163 end = i + 1;
11164 Fadd_text_properties (make_number (i), make_number (end),
11165 props, f->desired_tool_bar_string);
11166 #undef PROP
11169 UNGCPRO;
11173 /* Display one line of the tool-bar of frame IT->f.
11175 HEIGHT specifies the desired height of the tool-bar line.
11176 If the actual height of the glyph row is less than HEIGHT, the
11177 row's height is increased to HEIGHT, and the icons are centered
11178 vertically in the new height.
11180 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11181 count a final empty row in case the tool-bar width exactly matches
11182 the window width.
11185 static void
11186 display_tool_bar_line (struct it *it, int height)
11188 struct glyph_row *row = it->glyph_row;
11189 int max_x = it->last_visible_x;
11190 struct glyph *last;
11192 prepare_desired_row (row);
11193 row->y = it->current_y;
11195 /* Note that this isn't made use of if the face hasn't a box,
11196 so there's no need to check the face here. */
11197 it->start_of_box_run_p = 1;
11199 while (it->current_x < max_x)
11201 int x, n_glyphs_before, i, nglyphs;
11202 struct it it_before;
11204 /* Get the next display element. */
11205 if (!get_next_display_element (it))
11207 /* Don't count empty row if we are counting needed tool-bar lines. */
11208 if (height < 0 && !it->hpos)
11209 return;
11210 break;
11213 /* Produce glyphs. */
11214 n_glyphs_before = row->used[TEXT_AREA];
11215 it_before = *it;
11217 PRODUCE_GLYPHS (it);
11219 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11220 i = 0;
11221 x = it_before.current_x;
11222 while (i < nglyphs)
11224 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11226 if (x + glyph->pixel_width > max_x)
11228 /* Glyph doesn't fit on line. Backtrack. */
11229 row->used[TEXT_AREA] = n_glyphs_before;
11230 *it = it_before;
11231 /* If this is the only glyph on this line, it will never fit on the
11232 tool-bar, so skip it. But ensure there is at least one glyph,
11233 so we don't accidentally disable the tool-bar. */
11234 if (n_glyphs_before == 0
11235 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11236 break;
11237 goto out;
11240 ++it->hpos;
11241 x += glyph->pixel_width;
11242 ++i;
11245 /* Stop at line end. */
11246 if (ITERATOR_AT_END_OF_LINE_P (it))
11247 break;
11249 set_iterator_to_next (it, 1);
11252 out:;
11254 row->displays_text_p = row->used[TEXT_AREA] != 0;
11256 /* Use default face for the border below the tool bar.
11258 FIXME: When auto-resize-tool-bars is grow-only, there is
11259 no additional border below the possibly empty tool-bar lines.
11260 So to make the extra empty lines look "normal", we have to
11261 use the tool-bar face for the border too. */
11262 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11263 it->face_id = DEFAULT_FACE_ID;
11265 extend_face_to_end_of_line (it);
11266 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11267 last->right_box_line_p = 1;
11268 if (last == row->glyphs[TEXT_AREA])
11269 last->left_box_line_p = 1;
11271 /* Make line the desired height and center it vertically. */
11272 if ((height -= it->max_ascent + it->max_descent) > 0)
11274 /* Don't add more than one line height. */
11275 height %= FRAME_LINE_HEIGHT (it->f);
11276 it->max_ascent += height / 2;
11277 it->max_descent += (height + 1) / 2;
11280 compute_line_metrics (it);
11282 /* If line is empty, make it occupy the rest of the tool-bar. */
11283 if (!row->displays_text_p)
11285 row->height = row->phys_height = it->last_visible_y - row->y;
11286 row->visible_height = row->height;
11287 row->ascent = row->phys_ascent = 0;
11288 row->extra_line_spacing = 0;
11291 row->full_width_p = 1;
11292 row->continued_p = 0;
11293 row->truncated_on_left_p = 0;
11294 row->truncated_on_right_p = 0;
11296 it->current_x = it->hpos = 0;
11297 it->current_y += row->height;
11298 ++it->vpos;
11299 ++it->glyph_row;
11303 /* Max tool-bar height. */
11305 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11306 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11308 /* Value is the number of screen lines needed to make all tool-bar
11309 items of frame F visible. The number of actual rows needed is
11310 returned in *N_ROWS if non-NULL. */
11312 static int
11313 tool_bar_lines_needed (struct frame *f, int *n_rows)
11315 struct window *w = XWINDOW (f->tool_bar_window);
11316 struct it it;
11317 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11318 the desired matrix, so use (unused) mode-line row as temporary row to
11319 avoid destroying the first tool-bar row. */
11320 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11322 /* Initialize an iterator for iteration over
11323 F->desired_tool_bar_string in the tool-bar window of frame F. */
11324 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11325 it.first_visible_x = 0;
11326 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11327 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11328 it.paragraph_embedding = L2R;
11330 while (!ITERATOR_AT_END_P (&it))
11332 clear_glyph_row (temp_row);
11333 it.glyph_row = temp_row;
11334 display_tool_bar_line (&it, -1);
11336 clear_glyph_row (temp_row);
11338 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11339 if (n_rows)
11340 *n_rows = it.vpos > 0 ? it.vpos : -1;
11342 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11346 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11347 0, 1, 0,
11348 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11349 (Lisp_Object frame)
11351 struct frame *f;
11352 struct window *w;
11353 int nlines = 0;
11355 if (NILP (frame))
11356 frame = selected_frame;
11357 else
11358 CHECK_FRAME (frame);
11359 f = XFRAME (frame);
11361 if (WINDOWP (f->tool_bar_window)
11362 && (w = XWINDOW (f->tool_bar_window),
11363 WINDOW_TOTAL_LINES (w) > 0))
11365 update_tool_bar (f, 1);
11366 if (f->n_tool_bar_items)
11368 build_desired_tool_bar_string (f);
11369 nlines = tool_bar_lines_needed (f, NULL);
11373 return make_number (nlines);
11377 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11378 height should be changed. */
11380 static int
11381 redisplay_tool_bar (struct frame *f)
11383 struct window *w;
11384 struct it it;
11385 struct glyph_row *row;
11387 #if defined (USE_GTK) || defined (HAVE_NS)
11388 if (FRAME_EXTERNAL_TOOL_BAR (f))
11389 update_frame_tool_bar (f);
11390 return 0;
11391 #endif
11393 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11394 do anything. This means you must start with tool-bar-lines
11395 non-zero to get the auto-sizing effect. Or in other words, you
11396 can turn off tool-bars by specifying tool-bar-lines zero. */
11397 if (!WINDOWP (f->tool_bar_window)
11398 || (w = XWINDOW (f->tool_bar_window),
11399 WINDOW_TOTAL_LINES (w) == 0))
11400 return 0;
11402 /* Set up an iterator for the tool-bar window. */
11403 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11404 it.first_visible_x = 0;
11405 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11406 row = it.glyph_row;
11408 /* Build a string that represents the contents of the tool-bar. */
11409 build_desired_tool_bar_string (f);
11410 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11411 /* FIXME: This should be controlled by a user option. But it
11412 doesn't make sense to have an R2L tool bar if the menu bar cannot
11413 be drawn also R2L, and making the menu bar R2L is tricky due
11414 toolkit-specific code that implements it. If an R2L tool bar is
11415 ever supported, display_tool_bar_line should also be augmented to
11416 call unproduce_glyphs like display_line and display_string
11417 do. */
11418 it.paragraph_embedding = L2R;
11420 if (f->n_tool_bar_rows == 0)
11422 int nlines;
11424 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11425 nlines != WINDOW_TOTAL_LINES (w)))
11427 Lisp_Object frame;
11428 int old_height = WINDOW_TOTAL_LINES (w);
11430 XSETFRAME (frame, f);
11431 Fmodify_frame_parameters (frame,
11432 Fcons (Fcons (Qtool_bar_lines,
11433 make_number (nlines)),
11434 Qnil));
11435 if (WINDOW_TOTAL_LINES (w) != old_height)
11437 clear_glyph_matrix (w->desired_matrix);
11438 fonts_changed_p = 1;
11439 return 1;
11444 /* Display as many lines as needed to display all tool-bar items. */
11446 if (f->n_tool_bar_rows > 0)
11448 int border, rows, height, extra;
11450 if (INTEGERP (Vtool_bar_border))
11451 border = XINT (Vtool_bar_border);
11452 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11453 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11454 else if (EQ (Vtool_bar_border, Qborder_width))
11455 border = f->border_width;
11456 else
11457 border = 0;
11458 if (border < 0)
11459 border = 0;
11461 rows = f->n_tool_bar_rows;
11462 height = max (1, (it.last_visible_y - border) / rows);
11463 extra = it.last_visible_y - border - height * rows;
11465 while (it.current_y < it.last_visible_y)
11467 int h = 0;
11468 if (extra > 0 && rows-- > 0)
11470 h = (extra + rows - 1) / rows;
11471 extra -= h;
11473 display_tool_bar_line (&it, height + h);
11476 else
11478 while (it.current_y < it.last_visible_y)
11479 display_tool_bar_line (&it, 0);
11482 /* It doesn't make much sense to try scrolling in the tool-bar
11483 window, so don't do it. */
11484 w->desired_matrix->no_scrolling_p = 1;
11485 w->must_be_updated_p = 1;
11487 if (!NILP (Vauto_resize_tool_bars))
11489 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11490 int change_height_p = 0;
11492 /* If we couldn't display everything, change the tool-bar's
11493 height if there is room for more. */
11494 if (IT_STRING_CHARPOS (it) < it.end_charpos
11495 && it.current_y < max_tool_bar_height)
11496 change_height_p = 1;
11498 row = it.glyph_row - 1;
11500 /* If there are blank lines at the end, except for a partially
11501 visible blank line at the end that is smaller than
11502 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11503 if (!row->displays_text_p
11504 && row->height >= FRAME_LINE_HEIGHT (f))
11505 change_height_p = 1;
11507 /* If row displays tool-bar items, but is partially visible,
11508 change the tool-bar's height. */
11509 if (row->displays_text_p
11510 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11511 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11512 change_height_p = 1;
11514 /* Resize windows as needed by changing the `tool-bar-lines'
11515 frame parameter. */
11516 if (change_height_p)
11518 Lisp_Object frame;
11519 int old_height = WINDOW_TOTAL_LINES (w);
11520 int nrows;
11521 int nlines = tool_bar_lines_needed (f, &nrows);
11523 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11524 && !f->minimize_tool_bar_window_p)
11525 ? (nlines > old_height)
11526 : (nlines != old_height));
11527 f->minimize_tool_bar_window_p = 0;
11529 if (change_height_p)
11531 XSETFRAME (frame, f);
11532 Fmodify_frame_parameters (frame,
11533 Fcons (Fcons (Qtool_bar_lines,
11534 make_number (nlines)),
11535 Qnil));
11536 if (WINDOW_TOTAL_LINES (w) != old_height)
11538 clear_glyph_matrix (w->desired_matrix);
11539 f->n_tool_bar_rows = nrows;
11540 fonts_changed_p = 1;
11541 return 1;
11547 f->minimize_tool_bar_window_p = 0;
11548 return 0;
11552 /* Get information about the tool-bar item which is displayed in GLYPH
11553 on frame F. Return in *PROP_IDX the index where tool-bar item
11554 properties start in F->tool_bar_items. Value is zero if
11555 GLYPH doesn't display a tool-bar item. */
11557 static int
11558 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11560 Lisp_Object prop;
11561 int success_p;
11562 int charpos;
11564 /* This function can be called asynchronously, which means we must
11565 exclude any possibility that Fget_text_property signals an
11566 error. */
11567 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11568 charpos = max (0, charpos);
11570 /* Get the text property `menu-item' at pos. The value of that
11571 property is the start index of this item's properties in
11572 F->tool_bar_items. */
11573 prop = Fget_text_property (make_number (charpos),
11574 Qmenu_item, f->current_tool_bar_string);
11575 if (INTEGERP (prop))
11577 *prop_idx = XINT (prop);
11578 success_p = 1;
11580 else
11581 success_p = 0;
11583 return success_p;
11587 /* Get information about the tool-bar item at position X/Y on frame F.
11588 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11589 the current matrix of the tool-bar window of F, or NULL if not
11590 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11591 item in F->tool_bar_items. Value is
11593 -1 if X/Y is not on a tool-bar item
11594 0 if X/Y is on the same item that was highlighted before.
11595 1 otherwise. */
11597 static int
11598 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11599 int *hpos, int *vpos, int *prop_idx)
11601 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11602 struct window *w = XWINDOW (f->tool_bar_window);
11603 int area;
11605 /* Find the glyph under X/Y. */
11606 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11607 if (*glyph == NULL)
11608 return -1;
11610 /* Get the start of this tool-bar item's properties in
11611 f->tool_bar_items. */
11612 if (!tool_bar_item_info (f, *glyph, prop_idx))
11613 return -1;
11615 /* Is mouse on the highlighted item? */
11616 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11617 && *vpos >= hlinfo->mouse_face_beg_row
11618 && *vpos <= hlinfo->mouse_face_end_row
11619 && (*vpos > hlinfo->mouse_face_beg_row
11620 || *hpos >= hlinfo->mouse_face_beg_col)
11621 && (*vpos < hlinfo->mouse_face_end_row
11622 || *hpos < hlinfo->mouse_face_end_col
11623 || hlinfo->mouse_face_past_end))
11624 return 0;
11626 return 1;
11630 /* EXPORT:
11631 Handle mouse button event on the tool-bar of frame F, at
11632 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11633 0 for button release. MODIFIERS is event modifiers for button
11634 release. */
11636 void
11637 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11638 unsigned int modifiers)
11640 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11641 struct window *w = XWINDOW (f->tool_bar_window);
11642 int hpos, vpos, prop_idx;
11643 struct glyph *glyph;
11644 Lisp_Object enabled_p;
11646 /* If not on the highlighted tool-bar item, return. */
11647 frame_to_window_pixel_xy (w, &x, &y);
11648 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11649 return;
11651 /* If item is disabled, do nothing. */
11652 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11653 if (NILP (enabled_p))
11654 return;
11656 if (down_p)
11658 /* Show item in pressed state. */
11659 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11660 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11661 last_tool_bar_item = prop_idx;
11663 else
11665 Lisp_Object key, frame;
11666 struct input_event event;
11667 EVENT_INIT (event);
11669 /* Show item in released state. */
11670 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11671 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11673 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11675 XSETFRAME (frame, f);
11676 event.kind = TOOL_BAR_EVENT;
11677 event.frame_or_window = frame;
11678 event.arg = frame;
11679 kbd_buffer_store_event (&event);
11681 event.kind = TOOL_BAR_EVENT;
11682 event.frame_or_window = frame;
11683 event.arg = key;
11684 event.modifiers = modifiers;
11685 kbd_buffer_store_event (&event);
11686 last_tool_bar_item = -1;
11691 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11692 tool-bar window-relative coordinates X/Y. Called from
11693 note_mouse_highlight. */
11695 static void
11696 note_tool_bar_highlight (struct frame *f, int x, int y)
11698 Lisp_Object window = f->tool_bar_window;
11699 struct window *w = XWINDOW (window);
11700 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11701 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11702 int hpos, vpos;
11703 struct glyph *glyph;
11704 struct glyph_row *row;
11705 int i;
11706 Lisp_Object enabled_p;
11707 int prop_idx;
11708 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11709 int mouse_down_p, rc;
11711 /* Function note_mouse_highlight is called with negative X/Y
11712 values when mouse moves outside of the frame. */
11713 if (x <= 0 || y <= 0)
11715 clear_mouse_face (hlinfo);
11716 return;
11719 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11720 if (rc < 0)
11722 /* Not on tool-bar item. */
11723 clear_mouse_face (hlinfo);
11724 return;
11726 else if (rc == 0)
11727 /* On same tool-bar item as before. */
11728 goto set_help_echo;
11730 clear_mouse_face (hlinfo);
11732 /* Mouse is down, but on different tool-bar item? */
11733 mouse_down_p = (dpyinfo->grabbed
11734 && f == last_mouse_frame
11735 && FRAME_LIVE_P (f));
11736 if (mouse_down_p
11737 && last_tool_bar_item != prop_idx)
11738 return;
11740 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11741 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11743 /* If tool-bar item is not enabled, don't highlight it. */
11744 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11745 if (!NILP (enabled_p))
11747 /* Compute the x-position of the glyph. In front and past the
11748 image is a space. We include this in the highlighted area. */
11749 row = MATRIX_ROW (w->current_matrix, vpos);
11750 for (i = x = 0; i < hpos; ++i)
11751 x += row->glyphs[TEXT_AREA][i].pixel_width;
11753 /* Record this as the current active region. */
11754 hlinfo->mouse_face_beg_col = hpos;
11755 hlinfo->mouse_face_beg_row = vpos;
11756 hlinfo->mouse_face_beg_x = x;
11757 hlinfo->mouse_face_beg_y = row->y;
11758 hlinfo->mouse_face_past_end = 0;
11760 hlinfo->mouse_face_end_col = hpos + 1;
11761 hlinfo->mouse_face_end_row = vpos;
11762 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11763 hlinfo->mouse_face_end_y = row->y;
11764 hlinfo->mouse_face_window = window;
11765 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11767 /* Display it as active. */
11768 show_mouse_face (hlinfo, draw);
11769 hlinfo->mouse_face_image_state = draw;
11772 set_help_echo:
11774 /* Set help_echo_string to a help string to display for this tool-bar item.
11775 XTread_socket does the rest. */
11776 help_echo_object = help_echo_window = Qnil;
11777 help_echo_pos = -1;
11778 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11779 if (NILP (help_echo_string))
11780 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11783 #endif /* HAVE_WINDOW_SYSTEM */
11787 /************************************************************************
11788 Horizontal scrolling
11789 ************************************************************************/
11791 static int hscroll_window_tree (Lisp_Object);
11792 static int hscroll_windows (Lisp_Object);
11794 /* For all leaf windows in the window tree rooted at WINDOW, set their
11795 hscroll value so that PT is (i) visible in the window, and (ii) so
11796 that it is not within a certain margin at the window's left and
11797 right border. Value is non-zero if any window's hscroll has been
11798 changed. */
11800 static int
11801 hscroll_window_tree (Lisp_Object window)
11803 int hscrolled_p = 0;
11804 int hscroll_relative_p = FLOATP (Vhscroll_step);
11805 int hscroll_step_abs = 0;
11806 double hscroll_step_rel = 0;
11808 if (hscroll_relative_p)
11810 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11811 if (hscroll_step_rel < 0)
11813 hscroll_relative_p = 0;
11814 hscroll_step_abs = 0;
11817 else if (INTEGERP (Vhscroll_step))
11819 hscroll_step_abs = XINT (Vhscroll_step);
11820 if (hscroll_step_abs < 0)
11821 hscroll_step_abs = 0;
11823 else
11824 hscroll_step_abs = 0;
11826 while (WINDOWP (window))
11828 struct window *w = XWINDOW (window);
11830 if (WINDOWP (w->hchild))
11831 hscrolled_p |= hscroll_window_tree (w->hchild);
11832 else if (WINDOWP (w->vchild))
11833 hscrolled_p |= hscroll_window_tree (w->vchild);
11834 else if (w->cursor.vpos >= 0)
11836 int h_margin;
11837 int text_area_width;
11838 struct glyph_row *current_cursor_row
11839 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11840 struct glyph_row *desired_cursor_row
11841 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11842 struct glyph_row *cursor_row
11843 = (desired_cursor_row->enabled_p
11844 ? desired_cursor_row
11845 : current_cursor_row);
11847 text_area_width = window_box_width (w, TEXT_AREA);
11849 /* Scroll when cursor is inside this scroll margin. */
11850 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11852 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11853 && ((XFASTINT (w->hscroll)
11854 && w->cursor.x <= h_margin)
11855 || (cursor_row->enabled_p
11856 && cursor_row->truncated_on_right_p
11857 && (w->cursor.x >= text_area_width - h_margin))))
11859 struct it it;
11860 int hscroll;
11861 struct buffer *saved_current_buffer;
11862 EMACS_INT pt;
11863 int wanted_x;
11865 /* Find point in a display of infinite width. */
11866 saved_current_buffer = current_buffer;
11867 current_buffer = XBUFFER (w->buffer);
11869 if (w == XWINDOW (selected_window))
11870 pt = PT;
11871 else
11873 pt = marker_position (w->pointm);
11874 pt = max (BEGV, pt);
11875 pt = min (ZV, pt);
11878 /* Move iterator to pt starting at cursor_row->start in
11879 a line with infinite width. */
11880 init_to_row_start (&it, w, cursor_row);
11881 it.last_visible_x = INFINITY;
11882 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11883 current_buffer = saved_current_buffer;
11885 /* Position cursor in window. */
11886 if (!hscroll_relative_p && hscroll_step_abs == 0)
11887 hscroll = max (0, (it.current_x
11888 - (ITERATOR_AT_END_OF_LINE_P (&it)
11889 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11890 : (text_area_width / 2))))
11891 / FRAME_COLUMN_WIDTH (it.f);
11892 else if (w->cursor.x >= text_area_width - h_margin)
11894 if (hscroll_relative_p)
11895 wanted_x = text_area_width * (1 - hscroll_step_rel)
11896 - h_margin;
11897 else
11898 wanted_x = text_area_width
11899 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11900 - h_margin;
11901 hscroll
11902 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11904 else
11906 if (hscroll_relative_p)
11907 wanted_x = text_area_width * hscroll_step_rel
11908 + h_margin;
11909 else
11910 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11911 + h_margin;
11912 hscroll
11913 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11915 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11917 /* Don't call Fset_window_hscroll if value hasn't
11918 changed because it will prevent redisplay
11919 optimizations. */
11920 if (XFASTINT (w->hscroll) != hscroll)
11922 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11923 w->hscroll = make_number (hscroll);
11924 hscrolled_p = 1;
11929 window = w->next;
11932 /* Value is non-zero if hscroll of any leaf window has been changed. */
11933 return hscrolled_p;
11937 /* Set hscroll so that cursor is visible and not inside horizontal
11938 scroll margins for all windows in the tree rooted at WINDOW. See
11939 also hscroll_window_tree above. Value is non-zero if any window's
11940 hscroll has been changed. If it has, desired matrices on the frame
11941 of WINDOW are cleared. */
11943 static int
11944 hscroll_windows (Lisp_Object window)
11946 int hscrolled_p = hscroll_window_tree (window);
11947 if (hscrolled_p)
11948 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11949 return hscrolled_p;
11954 /************************************************************************
11955 Redisplay
11956 ************************************************************************/
11958 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11959 to a non-zero value. This is sometimes handy to have in a debugger
11960 session. */
11962 #if GLYPH_DEBUG
11964 /* First and last unchanged row for try_window_id. */
11966 static int debug_first_unchanged_at_end_vpos;
11967 static int debug_last_unchanged_at_beg_vpos;
11969 /* Delta vpos and y. */
11971 static int debug_dvpos, debug_dy;
11973 /* Delta in characters and bytes for try_window_id. */
11975 static EMACS_INT debug_delta, debug_delta_bytes;
11977 /* Values of window_end_pos and window_end_vpos at the end of
11978 try_window_id. */
11980 static EMACS_INT debug_end_vpos;
11982 /* Append a string to W->desired_matrix->method. FMT is a printf
11983 format string. If trace_redisplay_p is non-zero also printf the
11984 resulting string to stderr. */
11986 static void debug_method_add (struct window *, char const *, ...)
11987 ATTRIBUTE_FORMAT_PRINTF (2, 3);
11989 static void
11990 debug_method_add (struct window *w, char const *fmt, ...)
11992 char buffer[512];
11993 char *method = w->desired_matrix->method;
11994 int len = strlen (method);
11995 int size = sizeof w->desired_matrix->method;
11996 int remaining = size - len - 1;
11997 va_list ap;
11999 va_start (ap, fmt);
12000 vsprintf (buffer, fmt, ap);
12001 va_end (ap);
12002 if (len && remaining)
12004 method[len] = '|';
12005 --remaining, ++len;
12008 strncpy (method + len, buffer, remaining);
12010 if (trace_redisplay_p)
12011 fprintf (stderr, "%p (%s): %s\n",
12013 ((BUFFERP (w->buffer)
12014 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12015 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12016 : "no buffer"),
12017 buffer);
12020 #endif /* GLYPH_DEBUG */
12023 /* Value is non-zero if all changes in window W, which displays
12024 current_buffer, are in the text between START and END. START is a
12025 buffer position, END is given as a distance from Z. Used in
12026 redisplay_internal for display optimization. */
12028 static inline int
12029 text_outside_line_unchanged_p (struct window *w,
12030 EMACS_INT start, EMACS_INT end)
12032 int unchanged_p = 1;
12034 /* If text or overlays have changed, see where. */
12035 if (XFASTINT (w->last_modified) < MODIFF
12036 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12038 /* Gap in the line? */
12039 if (GPT < start || Z - GPT < end)
12040 unchanged_p = 0;
12042 /* Changes start in front of the line, or end after it? */
12043 if (unchanged_p
12044 && (BEG_UNCHANGED < start - 1
12045 || END_UNCHANGED < end))
12046 unchanged_p = 0;
12048 /* If selective display, can't optimize if changes start at the
12049 beginning of the line. */
12050 if (unchanged_p
12051 && INTEGERP (BVAR (current_buffer, selective_display))
12052 && XINT (BVAR (current_buffer, selective_display)) > 0
12053 && (BEG_UNCHANGED < start || GPT <= start))
12054 unchanged_p = 0;
12056 /* If there are overlays at the start or end of the line, these
12057 may have overlay strings with newlines in them. A change at
12058 START, for instance, may actually concern the display of such
12059 overlay strings as well, and they are displayed on different
12060 lines. So, quickly rule out this case. (For the future, it
12061 might be desirable to implement something more telling than
12062 just BEG/END_UNCHANGED.) */
12063 if (unchanged_p)
12065 if (BEG + BEG_UNCHANGED == start
12066 && overlay_touches_p (start))
12067 unchanged_p = 0;
12068 if (END_UNCHANGED == end
12069 && overlay_touches_p (Z - end))
12070 unchanged_p = 0;
12073 /* Under bidi reordering, adding or deleting a character in the
12074 beginning of a paragraph, before the first strong directional
12075 character, can change the base direction of the paragraph (unless
12076 the buffer specifies a fixed paragraph direction), which will
12077 require to redisplay the whole paragraph. It might be worthwhile
12078 to find the paragraph limits and widen the range of redisplayed
12079 lines to that, but for now just give up this optimization. */
12080 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12081 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12082 unchanged_p = 0;
12085 return unchanged_p;
12089 /* Do a frame update, taking possible shortcuts into account. This is
12090 the main external entry point for redisplay.
12092 If the last redisplay displayed an echo area message and that message
12093 is no longer requested, we clear the echo area or bring back the
12094 mini-buffer if that is in use. */
12096 void
12097 redisplay (void)
12099 redisplay_internal ();
12103 static Lisp_Object
12104 overlay_arrow_string_or_property (Lisp_Object var)
12106 Lisp_Object val;
12108 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12109 return val;
12111 return Voverlay_arrow_string;
12114 /* Return 1 if there are any overlay-arrows in current_buffer. */
12115 static int
12116 overlay_arrow_in_current_buffer_p (void)
12118 Lisp_Object vlist;
12120 for (vlist = Voverlay_arrow_variable_list;
12121 CONSP (vlist);
12122 vlist = XCDR (vlist))
12124 Lisp_Object var = XCAR (vlist);
12125 Lisp_Object val;
12127 if (!SYMBOLP (var))
12128 continue;
12129 val = find_symbol_value (var);
12130 if (MARKERP (val)
12131 && current_buffer == XMARKER (val)->buffer)
12132 return 1;
12134 return 0;
12138 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12139 has changed. */
12141 static int
12142 overlay_arrows_changed_p (void)
12144 Lisp_Object vlist;
12146 for (vlist = Voverlay_arrow_variable_list;
12147 CONSP (vlist);
12148 vlist = XCDR (vlist))
12150 Lisp_Object var = XCAR (vlist);
12151 Lisp_Object val, pstr;
12153 if (!SYMBOLP (var))
12154 continue;
12155 val = find_symbol_value (var);
12156 if (!MARKERP (val))
12157 continue;
12158 if (! EQ (COERCE_MARKER (val),
12159 Fget (var, Qlast_arrow_position))
12160 || ! (pstr = overlay_arrow_string_or_property (var),
12161 EQ (pstr, Fget (var, Qlast_arrow_string))))
12162 return 1;
12164 return 0;
12167 /* Mark overlay arrows to be updated on next redisplay. */
12169 static void
12170 update_overlay_arrows (int up_to_date)
12172 Lisp_Object vlist;
12174 for (vlist = Voverlay_arrow_variable_list;
12175 CONSP (vlist);
12176 vlist = XCDR (vlist))
12178 Lisp_Object var = XCAR (vlist);
12180 if (!SYMBOLP (var))
12181 continue;
12183 if (up_to_date > 0)
12185 Lisp_Object val = find_symbol_value (var);
12186 Fput (var, Qlast_arrow_position,
12187 COERCE_MARKER (val));
12188 Fput (var, Qlast_arrow_string,
12189 overlay_arrow_string_or_property (var));
12191 else if (up_to_date < 0
12192 || !NILP (Fget (var, Qlast_arrow_position)))
12194 Fput (var, Qlast_arrow_position, Qt);
12195 Fput (var, Qlast_arrow_string, Qt);
12201 /* Return overlay arrow string to display at row.
12202 Return integer (bitmap number) for arrow bitmap in left fringe.
12203 Return nil if no overlay arrow. */
12205 static Lisp_Object
12206 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12208 Lisp_Object vlist;
12210 for (vlist = Voverlay_arrow_variable_list;
12211 CONSP (vlist);
12212 vlist = XCDR (vlist))
12214 Lisp_Object var = XCAR (vlist);
12215 Lisp_Object val;
12217 if (!SYMBOLP (var))
12218 continue;
12220 val = find_symbol_value (var);
12222 if (MARKERP (val)
12223 && current_buffer == XMARKER (val)->buffer
12224 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12226 if (FRAME_WINDOW_P (it->f)
12227 /* FIXME: if ROW->reversed_p is set, this should test
12228 the right fringe, not the left one. */
12229 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12231 #ifdef HAVE_WINDOW_SYSTEM
12232 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12234 int fringe_bitmap;
12235 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12236 return make_number (fringe_bitmap);
12238 #endif
12239 return make_number (-1); /* Use default arrow bitmap */
12241 return overlay_arrow_string_or_property (var);
12245 return Qnil;
12248 /* Return 1 if point moved out of or into a composition. Otherwise
12249 return 0. PREV_BUF and PREV_PT are the last point buffer and
12250 position. BUF and PT are the current point buffer and position. */
12252 static int
12253 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12254 struct buffer *buf, EMACS_INT pt)
12256 EMACS_INT start, end;
12257 Lisp_Object prop;
12258 Lisp_Object buffer;
12260 XSETBUFFER (buffer, buf);
12261 /* Check a composition at the last point if point moved within the
12262 same buffer. */
12263 if (prev_buf == buf)
12265 if (prev_pt == pt)
12266 /* Point didn't move. */
12267 return 0;
12269 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12270 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12271 && COMPOSITION_VALID_P (start, end, prop)
12272 && start < prev_pt && end > prev_pt)
12273 /* The last point was within the composition. Return 1 iff
12274 point moved out of the composition. */
12275 return (pt <= start || pt >= end);
12278 /* Check a composition at the current point. */
12279 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12280 && find_composition (pt, -1, &start, &end, &prop, buffer)
12281 && COMPOSITION_VALID_P (start, end, prop)
12282 && start < pt && end > pt);
12286 /* Reconsider the setting of B->clip_changed which is displayed
12287 in window W. */
12289 static inline void
12290 reconsider_clip_changes (struct window *w, struct buffer *b)
12292 if (b->clip_changed
12293 && !NILP (w->window_end_valid)
12294 && w->current_matrix->buffer == b
12295 && w->current_matrix->zv == BUF_ZV (b)
12296 && w->current_matrix->begv == BUF_BEGV (b))
12297 b->clip_changed = 0;
12299 /* If display wasn't paused, and W is not a tool bar window, see if
12300 point has been moved into or out of a composition. In that case,
12301 we set b->clip_changed to 1 to force updating the screen. If
12302 b->clip_changed has already been set to 1, we can skip this
12303 check. */
12304 if (!b->clip_changed
12305 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12307 EMACS_INT pt;
12309 if (w == XWINDOW (selected_window))
12310 pt = PT;
12311 else
12312 pt = marker_position (w->pointm);
12314 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12315 || pt != XINT (w->last_point))
12316 && check_point_in_composition (w->current_matrix->buffer,
12317 XINT (w->last_point),
12318 XBUFFER (w->buffer), pt))
12319 b->clip_changed = 1;
12324 /* Select FRAME to forward the values of frame-local variables into C
12325 variables so that the redisplay routines can access those values
12326 directly. */
12328 static void
12329 select_frame_for_redisplay (Lisp_Object frame)
12331 Lisp_Object tail, tem;
12332 Lisp_Object old = selected_frame;
12333 struct Lisp_Symbol *sym;
12335 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12337 selected_frame = frame;
12339 do {
12340 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12341 if (CONSP (XCAR (tail))
12342 && (tem = XCAR (XCAR (tail)),
12343 SYMBOLP (tem))
12344 && (sym = indirect_variable (XSYMBOL (tem)),
12345 sym->redirect == SYMBOL_LOCALIZED)
12346 && sym->val.blv->frame_local)
12347 /* Use find_symbol_value rather than Fsymbol_value
12348 to avoid an error if it is void. */
12349 find_symbol_value (tem);
12350 } while (!EQ (frame, old) && (frame = old, 1));
12354 #define STOP_POLLING \
12355 do { if (! polling_stopped_here) stop_polling (); \
12356 polling_stopped_here = 1; } while (0)
12358 #define RESUME_POLLING \
12359 do { if (polling_stopped_here) start_polling (); \
12360 polling_stopped_here = 0; } while (0)
12363 /* Perhaps in the future avoid recentering windows if it
12364 is not necessary; currently that causes some problems. */
12366 static void
12367 redisplay_internal (void)
12369 struct window *w = XWINDOW (selected_window);
12370 struct window *sw;
12371 struct frame *fr;
12372 int pending;
12373 int must_finish = 0;
12374 struct text_pos tlbufpos, tlendpos;
12375 int number_of_visible_frames;
12376 int count, count1;
12377 struct frame *sf;
12378 int polling_stopped_here = 0;
12379 Lisp_Object old_frame = selected_frame;
12381 /* Non-zero means redisplay has to consider all windows on all
12382 frames. Zero means, only selected_window is considered. */
12383 int consider_all_windows_p;
12385 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12387 /* No redisplay if running in batch mode or frame is not yet fully
12388 initialized, or redisplay is explicitly turned off by setting
12389 Vinhibit_redisplay. */
12390 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12391 || !NILP (Vinhibit_redisplay))
12392 return;
12394 /* Don't examine these until after testing Vinhibit_redisplay.
12395 When Emacs is shutting down, perhaps because its connection to
12396 X has dropped, we should not look at them at all. */
12397 fr = XFRAME (w->frame);
12398 sf = SELECTED_FRAME ();
12400 if (!fr->glyphs_initialized_p)
12401 return;
12403 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12404 if (popup_activated ())
12405 return;
12406 #endif
12408 /* I don't think this happens but let's be paranoid. */
12409 if (redisplaying_p)
12410 return;
12412 /* Record a function that resets redisplaying_p to its old value
12413 when we leave this function. */
12414 count = SPECPDL_INDEX ();
12415 record_unwind_protect (unwind_redisplay,
12416 Fcons (make_number (redisplaying_p), selected_frame));
12417 ++redisplaying_p;
12418 specbind (Qinhibit_free_realized_faces, Qnil);
12421 Lisp_Object tail, frame;
12423 FOR_EACH_FRAME (tail, frame)
12425 struct frame *f = XFRAME (frame);
12426 f->already_hscrolled_p = 0;
12430 retry:
12431 /* Remember the currently selected window. */
12432 sw = w;
12434 if (!EQ (old_frame, selected_frame)
12435 && FRAME_LIVE_P (XFRAME (old_frame)))
12436 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12437 selected_frame and selected_window to be temporarily out-of-sync so
12438 when we come back here via `goto retry', we need to resync because we
12439 may need to run Elisp code (via prepare_menu_bars). */
12440 select_frame_for_redisplay (old_frame);
12442 pending = 0;
12443 reconsider_clip_changes (w, current_buffer);
12444 last_escape_glyph_frame = NULL;
12445 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12446 last_glyphless_glyph_frame = NULL;
12447 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12449 /* If new fonts have been loaded that make a glyph matrix adjustment
12450 necessary, do it. */
12451 if (fonts_changed_p)
12453 adjust_glyphs (NULL);
12454 ++windows_or_buffers_changed;
12455 fonts_changed_p = 0;
12458 /* If face_change_count is non-zero, init_iterator will free all
12459 realized faces, which includes the faces referenced from current
12460 matrices. So, we can't reuse current matrices in this case. */
12461 if (face_change_count)
12462 ++windows_or_buffers_changed;
12464 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12465 && FRAME_TTY (sf)->previous_frame != sf)
12467 /* Since frames on a single ASCII terminal share the same
12468 display area, displaying a different frame means redisplay
12469 the whole thing. */
12470 windows_or_buffers_changed++;
12471 SET_FRAME_GARBAGED (sf);
12472 #ifndef DOS_NT
12473 set_tty_color_mode (FRAME_TTY (sf), sf);
12474 #endif
12475 FRAME_TTY (sf)->previous_frame = sf;
12478 /* Set the visible flags for all frames. Do this before checking
12479 for resized or garbaged frames; they want to know if their frames
12480 are visible. See the comment in frame.h for
12481 FRAME_SAMPLE_VISIBILITY. */
12483 Lisp_Object tail, frame;
12485 number_of_visible_frames = 0;
12487 FOR_EACH_FRAME (tail, frame)
12489 struct frame *f = XFRAME (frame);
12491 FRAME_SAMPLE_VISIBILITY (f);
12492 if (FRAME_VISIBLE_P (f))
12493 ++number_of_visible_frames;
12494 clear_desired_matrices (f);
12498 /* Notice any pending interrupt request to change frame size. */
12499 do_pending_window_change (1);
12501 /* do_pending_window_change could change the selected_window due to
12502 frame resizing which makes the selected window too small. */
12503 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12505 sw = w;
12506 reconsider_clip_changes (w, current_buffer);
12509 /* Clear frames marked as garbaged. */
12510 if (frame_garbaged)
12511 clear_garbaged_frames ();
12513 /* Build menubar and tool-bar items. */
12514 if (NILP (Vmemory_full))
12515 prepare_menu_bars ();
12517 if (windows_or_buffers_changed)
12518 update_mode_lines++;
12520 /* Detect case that we need to write or remove a star in the mode line. */
12521 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12523 w->update_mode_line = Qt;
12524 if (buffer_shared > 1)
12525 update_mode_lines++;
12528 /* Avoid invocation of point motion hooks by `current_column' below. */
12529 count1 = SPECPDL_INDEX ();
12530 specbind (Qinhibit_point_motion_hooks, Qt);
12532 /* If %c is in the mode line, update it if needed. */
12533 if (!NILP (w->column_number_displayed)
12534 /* This alternative quickly identifies a common case
12535 where no change is needed. */
12536 && !(PT == XFASTINT (w->last_point)
12537 && XFASTINT (w->last_modified) >= MODIFF
12538 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12539 && (XFASTINT (w->column_number_displayed) != current_column ()))
12540 w->update_mode_line = Qt;
12542 unbind_to (count1, Qnil);
12544 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12546 /* The variable buffer_shared is set in redisplay_window and
12547 indicates that we redisplay a buffer in different windows. See
12548 there. */
12549 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12550 || cursor_type_changed);
12552 /* If specs for an arrow have changed, do thorough redisplay
12553 to ensure we remove any arrow that should no longer exist. */
12554 if (overlay_arrows_changed_p ())
12555 consider_all_windows_p = windows_or_buffers_changed = 1;
12557 /* Normally the message* functions will have already displayed and
12558 updated the echo area, but the frame may have been trashed, or
12559 the update may have been preempted, so display the echo area
12560 again here. Checking message_cleared_p captures the case that
12561 the echo area should be cleared. */
12562 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12563 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12564 || (message_cleared_p
12565 && minibuf_level == 0
12566 /* If the mini-window is currently selected, this means the
12567 echo-area doesn't show through. */
12568 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12570 int window_height_changed_p = echo_area_display (0);
12571 must_finish = 1;
12573 /* If we don't display the current message, don't clear the
12574 message_cleared_p flag, because, if we did, we wouldn't clear
12575 the echo area in the next redisplay which doesn't preserve
12576 the echo area. */
12577 if (!display_last_displayed_message_p)
12578 message_cleared_p = 0;
12580 if (fonts_changed_p)
12581 goto retry;
12582 else if (window_height_changed_p)
12584 consider_all_windows_p = 1;
12585 ++update_mode_lines;
12586 ++windows_or_buffers_changed;
12588 /* If window configuration was changed, frames may have been
12589 marked garbaged. Clear them or we will experience
12590 surprises wrt scrolling. */
12591 if (frame_garbaged)
12592 clear_garbaged_frames ();
12595 else if (EQ (selected_window, minibuf_window)
12596 && (current_buffer->clip_changed
12597 || XFASTINT (w->last_modified) < MODIFF
12598 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12599 && resize_mini_window (w, 0))
12601 /* Resized active mini-window to fit the size of what it is
12602 showing if its contents might have changed. */
12603 must_finish = 1;
12604 /* FIXME: this causes all frames to be updated, which seems unnecessary
12605 since only the current frame needs to be considered. This function needs
12606 to be rewritten with two variables, consider_all_windows and
12607 consider_all_frames. */
12608 consider_all_windows_p = 1;
12609 ++windows_or_buffers_changed;
12610 ++update_mode_lines;
12612 /* If window configuration was changed, frames may have been
12613 marked garbaged. Clear them or we will experience
12614 surprises wrt scrolling. */
12615 if (frame_garbaged)
12616 clear_garbaged_frames ();
12620 /* If showing the region, and mark has changed, we must redisplay
12621 the whole window. The assignment to this_line_start_pos prevents
12622 the optimization directly below this if-statement. */
12623 if (((!NILP (Vtransient_mark_mode)
12624 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12625 != !NILP (w->region_showing))
12626 || (!NILP (w->region_showing)
12627 && !EQ (w->region_showing,
12628 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12629 CHARPOS (this_line_start_pos) = 0;
12631 /* Optimize the case that only the line containing the cursor in the
12632 selected window has changed. Variables starting with this_ are
12633 set in display_line and record information about the line
12634 containing the cursor. */
12635 tlbufpos = this_line_start_pos;
12636 tlendpos = this_line_end_pos;
12637 if (!consider_all_windows_p
12638 && CHARPOS (tlbufpos) > 0
12639 && NILP (w->update_mode_line)
12640 && !current_buffer->clip_changed
12641 && !current_buffer->prevent_redisplay_optimizations_p
12642 && FRAME_VISIBLE_P (XFRAME (w->frame))
12643 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12644 /* Make sure recorded data applies to current buffer, etc. */
12645 && this_line_buffer == current_buffer
12646 && current_buffer == XBUFFER (w->buffer)
12647 && NILP (w->force_start)
12648 && NILP (w->optional_new_start)
12649 /* Point must be on the line that we have info recorded about. */
12650 && PT >= CHARPOS (tlbufpos)
12651 && PT <= Z - CHARPOS (tlendpos)
12652 /* All text outside that line, including its final newline,
12653 must be unchanged. */
12654 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12655 CHARPOS (tlendpos)))
12657 if (CHARPOS (tlbufpos) > BEGV
12658 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12659 && (CHARPOS (tlbufpos) == ZV
12660 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12661 /* Former continuation line has disappeared by becoming empty. */
12662 goto cancel;
12663 else if (XFASTINT (w->last_modified) < MODIFF
12664 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12665 || MINI_WINDOW_P (w))
12667 /* We have to handle the case of continuation around a
12668 wide-column character (see the comment in indent.c around
12669 line 1340).
12671 For instance, in the following case:
12673 -------- Insert --------
12674 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12675 J_I_ ==> J_I_ `^^' are cursors.
12676 ^^ ^^
12677 -------- --------
12679 As we have to redraw the line above, we cannot use this
12680 optimization. */
12682 struct it it;
12683 int line_height_before = this_line_pixel_height;
12685 /* Note that start_display will handle the case that the
12686 line starting at tlbufpos is a continuation line. */
12687 start_display (&it, w, tlbufpos);
12689 /* Implementation note: It this still necessary? */
12690 if (it.current_x != this_line_start_x)
12691 goto cancel;
12693 TRACE ((stderr, "trying display optimization 1\n"));
12694 w->cursor.vpos = -1;
12695 overlay_arrow_seen = 0;
12696 it.vpos = this_line_vpos;
12697 it.current_y = this_line_y;
12698 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12699 display_line (&it);
12701 /* If line contains point, is not continued,
12702 and ends at same distance from eob as before, we win. */
12703 if (w->cursor.vpos >= 0
12704 /* Line is not continued, otherwise this_line_start_pos
12705 would have been set to 0 in display_line. */
12706 && CHARPOS (this_line_start_pos)
12707 /* Line ends as before. */
12708 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12709 /* Line has same height as before. Otherwise other lines
12710 would have to be shifted up or down. */
12711 && this_line_pixel_height == line_height_before)
12713 /* If this is not the window's last line, we must adjust
12714 the charstarts of the lines below. */
12715 if (it.current_y < it.last_visible_y)
12717 struct glyph_row *row
12718 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12719 EMACS_INT delta, delta_bytes;
12721 /* We used to distinguish between two cases here,
12722 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12723 when the line ends in a newline or the end of the
12724 buffer's accessible portion. But both cases did
12725 the same, so they were collapsed. */
12726 delta = (Z
12727 - CHARPOS (tlendpos)
12728 - MATRIX_ROW_START_CHARPOS (row));
12729 delta_bytes = (Z_BYTE
12730 - BYTEPOS (tlendpos)
12731 - MATRIX_ROW_START_BYTEPOS (row));
12733 increment_matrix_positions (w->current_matrix,
12734 this_line_vpos + 1,
12735 w->current_matrix->nrows,
12736 delta, delta_bytes);
12739 /* If this row displays text now but previously didn't,
12740 or vice versa, w->window_end_vpos may have to be
12741 adjusted. */
12742 if ((it.glyph_row - 1)->displays_text_p)
12744 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12745 XSETINT (w->window_end_vpos, this_line_vpos);
12747 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12748 && this_line_vpos > 0)
12749 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12750 w->window_end_valid = Qnil;
12752 /* Update hint: No need to try to scroll in update_window. */
12753 w->desired_matrix->no_scrolling_p = 1;
12755 #if GLYPH_DEBUG
12756 *w->desired_matrix->method = 0;
12757 debug_method_add (w, "optimization 1");
12758 #endif
12759 #if HAVE_XWIDGETS
12760 //debug optimization movement issue
12761 //w->desired_matrix->no_scrolling_p = 1;
12762 //*w->desired_matrix->method = 0;
12763 //debug_method_add (w, "optimization 1");
12764 #endif
12766 #ifdef HAVE_WINDOW_SYSTEM
12767 update_window_fringes (w, 0);
12768 #endif
12769 goto update;
12771 else
12772 goto cancel;
12774 else if (/* Cursor position hasn't changed. */
12775 PT == XFASTINT (w->last_point)
12776 /* Make sure the cursor was last displayed
12777 in this window. Otherwise we have to reposition it. */
12778 && 0 <= w->cursor.vpos
12779 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12781 if (!must_finish)
12783 do_pending_window_change (1);
12784 /* If selected_window changed, redisplay again. */
12785 if (WINDOWP (selected_window)
12786 && (w = XWINDOW (selected_window)) != sw)
12787 goto retry;
12789 /* We used to always goto end_of_redisplay here, but this
12790 isn't enough if we have a blinking cursor. */
12791 if (w->cursor_off_p == w->last_cursor_off_p)
12792 goto end_of_redisplay;
12794 goto update;
12796 /* If highlighting the region, or if the cursor is in the echo area,
12797 then we can't just move the cursor. */
12798 else if (! (!NILP (Vtransient_mark_mode)
12799 && !NILP (BVAR (current_buffer, mark_active)))
12800 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12801 || highlight_nonselected_windows)
12802 && NILP (w->region_showing)
12803 && NILP (Vshow_trailing_whitespace)
12804 && !cursor_in_echo_area)
12806 struct it it;
12807 struct glyph_row *row;
12809 /* Skip from tlbufpos to PT and see where it is. Note that
12810 PT may be in invisible text. If so, we will end at the
12811 next visible position. */
12812 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12813 NULL, DEFAULT_FACE_ID);
12814 it.current_x = this_line_start_x;
12815 it.current_y = this_line_y;
12816 it.vpos = this_line_vpos;
12818 /* The call to move_it_to stops in front of PT, but
12819 moves over before-strings. */
12820 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12822 if (it.vpos == this_line_vpos
12823 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12824 row->enabled_p))
12826 xassert (this_line_vpos == it.vpos);
12827 xassert (this_line_y == it.current_y);
12828 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12829 #if GLYPH_DEBUG
12830 *w->desired_matrix->method = 0;
12831 debug_method_add (w, "optimization 3");
12832 #endif
12833 goto update;
12835 else
12836 goto cancel;
12839 cancel:
12840 /* Text changed drastically or point moved off of line. */
12841 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12844 CHARPOS (this_line_start_pos) = 0;
12845 consider_all_windows_p |= buffer_shared > 1;
12846 ++clear_face_cache_count;
12847 #ifdef HAVE_WINDOW_SYSTEM
12848 ++clear_image_cache_count;
12849 #endif
12851 /* Build desired matrices, and update the display. If
12852 consider_all_windows_p is non-zero, do it for all windows on all
12853 frames. Otherwise do it for selected_window, only. */
12855 if (consider_all_windows_p)
12857 Lisp_Object tail, frame;
12859 FOR_EACH_FRAME (tail, frame)
12860 XFRAME (frame)->updated_p = 0;
12862 /* Recompute # windows showing selected buffer. This will be
12863 incremented each time such a window is displayed. */
12864 buffer_shared = 0;
12866 FOR_EACH_FRAME (tail, frame)
12868 struct frame *f = XFRAME (frame);
12870 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12872 if (! EQ (frame, selected_frame))
12873 /* Select the frame, for the sake of frame-local
12874 variables. */
12875 select_frame_for_redisplay (frame);
12877 /* Mark all the scroll bars to be removed; we'll redeem
12878 the ones we want when we redisplay their windows. */
12879 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12880 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12882 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12883 redisplay_windows (FRAME_ROOT_WINDOW (f));
12885 /* The X error handler may have deleted that frame. */
12886 if (!FRAME_LIVE_P (f))
12887 continue;
12889 /* Any scroll bars which redisplay_windows should have
12890 nuked should now go away. */
12891 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12892 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12894 /* If fonts changed, display again. */
12895 /* ??? rms: I suspect it is a mistake to jump all the way
12896 back to retry here. It should just retry this frame. */
12897 if (fonts_changed_p)
12898 goto retry;
12900 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12902 /* See if we have to hscroll. */
12903 if (!f->already_hscrolled_p)
12905 f->already_hscrolled_p = 1;
12906 if (hscroll_windows (f->root_window))
12907 goto retry;
12910 /* Prevent various kinds of signals during display
12911 update. stdio is not robust about handling
12912 signals, which can cause an apparent I/O
12913 error. */
12914 if (interrupt_input)
12915 unrequest_sigio ();
12916 STOP_POLLING;
12918 /* Update the display. */
12919 set_window_update_flags (XWINDOW (f->root_window), 1);
12920 pending |= update_frame (f, 0, 0);
12921 f->updated_p = 1;
12926 if (!EQ (old_frame, selected_frame)
12927 && FRAME_LIVE_P (XFRAME (old_frame)))
12928 /* We played a bit fast-and-loose above and allowed selected_frame
12929 and selected_window to be temporarily out-of-sync but let's make
12930 sure this stays contained. */
12931 select_frame_for_redisplay (old_frame);
12932 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12934 if (!pending)
12936 /* Do the mark_window_display_accurate after all windows have
12937 been redisplayed because this call resets flags in buffers
12938 which are needed for proper redisplay. */
12939 FOR_EACH_FRAME (tail, frame)
12941 struct frame *f = XFRAME (frame);
12942 if (f->updated_p)
12944 mark_window_display_accurate (f->root_window, 1);
12945 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12946 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12951 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12953 Lisp_Object mini_window;
12954 struct frame *mini_frame;
12956 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12957 /* Use list_of_error, not Qerror, so that
12958 we catch only errors and don't run the debugger. */
12959 internal_condition_case_1 (redisplay_window_1, selected_window,
12960 list_of_error,
12961 redisplay_window_error);
12963 /* Compare desired and current matrices, perform output. */
12965 update:
12966 /* If fonts changed, display again. */
12967 if (fonts_changed_p)
12968 goto retry;
12970 /* Prevent various kinds of signals during display update.
12971 stdio is not robust about handling signals,
12972 which can cause an apparent I/O error. */
12973 if (interrupt_input)
12974 unrequest_sigio ();
12975 STOP_POLLING;
12977 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12979 if (hscroll_windows (selected_window))
12980 goto retry;
12982 XWINDOW (selected_window)->must_be_updated_p = 1;
12983 pending = update_frame (sf, 0, 0);
12986 /* We may have called echo_area_display at the top of this
12987 function. If the echo area is on another frame, that may
12988 have put text on a frame other than the selected one, so the
12989 above call to update_frame would not have caught it. Catch
12990 it here. */
12991 mini_window = FRAME_MINIBUF_WINDOW (sf);
12992 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12994 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12996 XWINDOW (mini_window)->must_be_updated_p = 1;
12997 pending |= update_frame (mini_frame, 0, 0);
12998 if (!pending && hscroll_windows (mini_window))
12999 goto retry;
13003 /* If display was paused because of pending input, make sure we do a
13004 thorough update the next time. */
13005 if (pending)
13007 /* Prevent the optimization at the beginning of
13008 redisplay_internal that tries a single-line update of the
13009 line containing the cursor in the selected window. */
13010 CHARPOS (this_line_start_pos) = 0;
13012 /* Let the overlay arrow be updated the next time. */
13013 update_overlay_arrows (0);
13015 /* If we pause after scrolling, some rows in the current
13016 matrices of some windows are not valid. */
13017 if (!WINDOW_FULL_WIDTH_P (w)
13018 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13019 update_mode_lines = 1;
13021 else
13023 if (!consider_all_windows_p)
13025 /* This has already been done above if
13026 consider_all_windows_p is set. */
13027 mark_window_display_accurate_1 (w, 1);
13029 /* Say overlay arrows are up to date. */
13030 update_overlay_arrows (1);
13032 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13033 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13036 update_mode_lines = 0;
13037 windows_or_buffers_changed = 0;
13038 cursor_type_changed = 0;
13041 /* Start SIGIO interrupts coming again. Having them off during the
13042 code above makes it less likely one will discard output, but not
13043 impossible, since there might be stuff in the system buffer here.
13044 But it is much hairier to try to do anything about that. */
13045 if (interrupt_input)
13046 request_sigio ();
13047 RESUME_POLLING;
13049 /* If a frame has become visible which was not before, redisplay
13050 again, so that we display it. Expose events for such a frame
13051 (which it gets when becoming visible) don't call the parts of
13052 redisplay constructing glyphs, so simply exposing a frame won't
13053 display anything in this case. So, we have to display these
13054 frames here explicitly. */
13055 if (!pending)
13057 Lisp_Object tail, frame;
13058 int new_count = 0;
13060 FOR_EACH_FRAME (tail, frame)
13062 int this_is_visible = 0;
13064 if (XFRAME (frame)->visible)
13065 this_is_visible = 1;
13066 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13067 if (XFRAME (frame)->visible)
13068 this_is_visible = 1;
13070 if (this_is_visible)
13071 new_count++;
13074 if (new_count != number_of_visible_frames)
13075 windows_or_buffers_changed++;
13078 /* Change frame size now if a change is pending. */
13079 do_pending_window_change (1);
13081 /* If we just did a pending size change, or have additional
13082 visible frames, or selected_window changed, redisplay again. */
13083 if ((windows_or_buffers_changed && !pending)
13084 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13085 goto retry;
13087 /* Clear the face and image caches.
13089 We used to do this only if consider_all_windows_p. But the cache
13090 needs to be cleared if a timer creates images in the current
13091 buffer (e.g. the test case in Bug#6230). */
13093 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13095 clear_face_cache (0);
13096 clear_face_cache_count = 0;
13099 #ifdef HAVE_WINDOW_SYSTEM
13100 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13102 clear_image_caches (Qnil);
13103 clear_image_cache_count = 0;
13105 #endif /* HAVE_WINDOW_SYSTEM */
13107 end_of_redisplay:
13108 unbind_to (count, Qnil);
13109 RESUME_POLLING;
13113 /* Redisplay, but leave alone any recent echo area message unless
13114 another message has been requested in its place.
13116 This is useful in situations where you need to redisplay but no
13117 user action has occurred, making it inappropriate for the message
13118 area to be cleared. See tracking_off and
13119 wait_reading_process_output for examples of these situations.
13121 FROM_WHERE is an integer saying from where this function was
13122 called. This is useful for debugging. */
13124 void
13125 redisplay_preserve_echo_area (int from_where)
13127 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13129 if (!NILP (echo_area_buffer[1]))
13131 /* We have a previously displayed message, but no current
13132 message. Redisplay the previous message. */
13133 display_last_displayed_message_p = 1;
13134 redisplay_internal ();
13135 display_last_displayed_message_p = 0;
13137 else
13138 redisplay_internal ();
13140 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13141 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13142 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13146 /* Function registered with record_unwind_protect in
13147 redisplay_internal. Reset redisplaying_p to the value it had
13148 before redisplay_internal was called, and clear
13149 prevent_freeing_realized_faces_p. It also selects the previously
13150 selected frame, unless it has been deleted (by an X connection
13151 failure during redisplay, for example). */
13153 static Lisp_Object
13154 unwind_redisplay (Lisp_Object val)
13156 Lisp_Object old_redisplaying_p, old_frame;
13158 old_redisplaying_p = XCAR (val);
13159 redisplaying_p = XFASTINT (old_redisplaying_p);
13160 old_frame = XCDR (val);
13161 if (! EQ (old_frame, selected_frame)
13162 && FRAME_LIVE_P (XFRAME (old_frame)))
13163 select_frame_for_redisplay (old_frame);
13164 return Qnil;
13168 /* Mark the display of window W as accurate or inaccurate. If
13169 ACCURATE_P is non-zero mark display of W as accurate. If
13170 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13171 redisplay_internal is called. */
13173 static void
13174 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13176 if (BUFFERP (w->buffer))
13178 struct buffer *b = XBUFFER (w->buffer);
13180 w->last_modified
13181 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13182 w->last_overlay_modified
13183 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13184 w->last_had_star
13185 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13187 if (accurate_p)
13189 b->clip_changed = 0;
13190 b->prevent_redisplay_optimizations_p = 0;
13192 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13193 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13194 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13195 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13197 w->current_matrix->buffer = b;
13198 w->current_matrix->begv = BUF_BEGV (b);
13199 w->current_matrix->zv = BUF_ZV (b);
13201 w->last_cursor = w->cursor;
13202 w->last_cursor_off_p = w->cursor_off_p;
13204 if (w == XWINDOW (selected_window))
13205 w->last_point = make_number (BUF_PT (b));
13206 else
13207 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13211 if (accurate_p)
13213 w->window_end_valid = w->buffer;
13214 w->update_mode_line = Qnil;
13219 /* Mark the display of windows in the window tree rooted at WINDOW as
13220 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13221 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13222 be redisplayed the next time redisplay_internal is called. */
13224 void
13225 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13227 struct window *w;
13229 for (; !NILP (window); window = w->next)
13231 w = XWINDOW (window);
13232 mark_window_display_accurate_1 (w, accurate_p);
13234 if (!NILP (w->vchild))
13235 mark_window_display_accurate (w->vchild, accurate_p);
13236 if (!NILP (w->hchild))
13237 mark_window_display_accurate (w->hchild, accurate_p);
13240 if (accurate_p)
13242 update_overlay_arrows (1);
13244 else
13246 /* Force a thorough redisplay the next time by setting
13247 last_arrow_position and last_arrow_string to t, which is
13248 unequal to any useful value of Voverlay_arrow_... */
13249 update_overlay_arrows (-1);
13254 /* Return value in display table DP (Lisp_Char_Table *) for character
13255 C. Since a display table doesn't have any parent, we don't have to
13256 follow parent. Do not call this function directly but use the
13257 macro DISP_CHAR_VECTOR. */
13259 Lisp_Object
13260 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13262 Lisp_Object val;
13264 if (ASCII_CHAR_P (c))
13266 val = dp->ascii;
13267 if (SUB_CHAR_TABLE_P (val))
13268 val = XSUB_CHAR_TABLE (val)->contents[c];
13270 else
13272 Lisp_Object table;
13274 XSETCHAR_TABLE (table, dp);
13275 val = char_table_ref (table, c);
13277 if (NILP (val))
13278 val = dp->defalt;
13279 return val;
13284 /***********************************************************************
13285 Window Redisplay
13286 ***********************************************************************/
13288 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13290 static void
13291 redisplay_windows (Lisp_Object window)
13293 while (!NILP (window))
13295 struct window *w = XWINDOW (window);
13297 if (!NILP (w->hchild))
13298 redisplay_windows (w->hchild);
13299 else if (!NILP (w->vchild))
13300 redisplay_windows (w->vchild);
13301 else if (!NILP (w->buffer))
13303 displayed_buffer = XBUFFER (w->buffer);
13304 /* Use list_of_error, not Qerror, so that
13305 we catch only errors and don't run the debugger. */
13306 internal_condition_case_1 (redisplay_window_0, window,
13307 list_of_error,
13308 redisplay_window_error);
13311 window = w->next;
13315 static Lisp_Object
13316 redisplay_window_error (Lisp_Object ignore)
13318 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13319 return Qnil;
13322 static Lisp_Object
13323 redisplay_window_0 (Lisp_Object window)
13325 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13326 redisplay_window (window, 0);
13327 return Qnil;
13330 static Lisp_Object
13331 redisplay_window_1 (Lisp_Object window)
13333 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13334 redisplay_window (window, 1);
13335 return Qnil;
13339 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13340 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13341 which positions recorded in ROW differ from current buffer
13342 positions.
13344 Return 0 if cursor is not on this row, 1 otherwise. */
13346 static int
13347 set_cursor_from_row (struct window *w, struct glyph_row *row,
13348 struct glyph_matrix *matrix,
13349 EMACS_INT delta, EMACS_INT delta_bytes,
13350 int dy, int dvpos)
13352 struct glyph *glyph = row->glyphs[TEXT_AREA];
13353 struct glyph *end = glyph + row->used[TEXT_AREA];
13354 struct glyph *cursor = NULL;
13355 /* The last known character position in row. */
13356 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13357 int x = row->x;
13358 EMACS_INT pt_old = PT - delta;
13359 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13360 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13361 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13362 /* A glyph beyond the edge of TEXT_AREA which we should never
13363 touch. */
13364 struct glyph *glyphs_end = end;
13365 /* Non-zero means we've found a match for cursor position, but that
13366 glyph has the avoid_cursor_p flag set. */
13367 int match_with_avoid_cursor = 0;
13368 /* Non-zero means we've seen at least one glyph that came from a
13369 display string. */
13370 int string_seen = 0;
13371 /* Largest and smalles buffer positions seen so far during scan of
13372 glyph row. */
13373 EMACS_INT bpos_max = pos_before;
13374 EMACS_INT bpos_min = pos_after;
13375 /* Last buffer position covered by an overlay string with an integer
13376 `cursor' property. */
13377 EMACS_INT bpos_covered = 0;
13378 /* Non-zero means the display string on which to display the cursor
13379 comes from a text property, not from an overlay. */
13380 int string_from_text_prop = 0;
13382 /* Skip over glyphs not having an object at the start and the end of
13383 the row. These are special glyphs like truncation marks on
13384 terminal frames. */
13385 if (row->displays_text_p)
13387 if (!row->reversed_p)
13389 while (glyph < end
13390 && INTEGERP (glyph->object)
13391 && glyph->charpos < 0)
13393 x += glyph->pixel_width;
13394 ++glyph;
13396 while (end > glyph
13397 && INTEGERP ((end - 1)->object)
13398 /* CHARPOS is zero for blanks and stretch glyphs
13399 inserted by extend_face_to_end_of_line. */
13400 && (end - 1)->charpos <= 0)
13401 --end;
13402 glyph_before = glyph - 1;
13403 glyph_after = end;
13405 else
13407 struct glyph *g;
13409 /* If the glyph row is reversed, we need to process it from back
13410 to front, so swap the edge pointers. */
13411 glyphs_end = end = glyph - 1;
13412 glyph += row->used[TEXT_AREA] - 1;
13414 while (glyph > end + 1
13415 && INTEGERP (glyph->object)
13416 && glyph->charpos < 0)
13418 --glyph;
13419 x -= glyph->pixel_width;
13421 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13422 --glyph;
13423 /* By default, in reversed rows we put the cursor on the
13424 rightmost (first in the reading order) glyph. */
13425 for (g = end + 1; g < glyph; g++)
13426 x += g->pixel_width;
13427 while (end < glyph
13428 && INTEGERP ((end + 1)->object)
13429 && (end + 1)->charpos <= 0)
13430 ++end;
13431 glyph_before = glyph + 1;
13432 glyph_after = end;
13435 else if (row->reversed_p)
13437 /* In R2L rows that don't display text, put the cursor on the
13438 rightmost glyph. Case in point: an empty last line that is
13439 part of an R2L paragraph. */
13440 cursor = end - 1;
13441 /* Avoid placing the cursor on the last glyph of the row, where
13442 on terminal frames we hold the vertical border between
13443 adjacent windows. */
13444 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13445 && !WINDOW_RIGHTMOST_P (w)
13446 && cursor == row->glyphs[LAST_AREA] - 1)
13447 cursor--;
13448 x = -1; /* will be computed below, at label compute_x */
13451 /* Step 1: Try to find the glyph whose character position
13452 corresponds to point. If that's not possible, find 2 glyphs
13453 whose character positions are the closest to point, one before
13454 point, the other after it. */
13455 if (!row->reversed_p)
13456 while (/* not marched to end of glyph row */
13457 glyph < end
13458 /* glyph was not inserted by redisplay for internal purposes */
13459 && !INTEGERP (glyph->object))
13461 if (BUFFERP (glyph->object))
13463 EMACS_INT dpos = glyph->charpos - pt_old;
13465 if (glyph->charpos > bpos_max)
13466 bpos_max = glyph->charpos;
13467 if (glyph->charpos < bpos_min)
13468 bpos_min = glyph->charpos;
13469 if (!glyph->avoid_cursor_p)
13471 /* If we hit point, we've found the glyph on which to
13472 display the cursor. */
13473 if (dpos == 0)
13475 match_with_avoid_cursor = 0;
13476 break;
13478 /* See if we've found a better approximation to
13479 POS_BEFORE or to POS_AFTER. Note that we want the
13480 first (leftmost) glyph of all those that are the
13481 closest from below, and the last (rightmost) of all
13482 those from above. */
13483 if (0 > dpos && dpos > pos_before - pt_old)
13485 pos_before = glyph->charpos;
13486 glyph_before = glyph;
13488 else if (0 < dpos && dpos <= pos_after - pt_old)
13490 pos_after = glyph->charpos;
13491 glyph_after = glyph;
13494 else if (dpos == 0)
13495 match_with_avoid_cursor = 1;
13497 else if (STRINGP (glyph->object))
13499 Lisp_Object chprop;
13500 EMACS_INT glyph_pos = glyph->charpos;
13502 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13503 glyph->object);
13504 if (INTEGERP (chprop))
13506 bpos_covered = bpos_max + XINT (chprop);
13507 /* If the `cursor' property covers buffer positions up
13508 to and including point, we should display cursor on
13509 this glyph. Note that overlays and text properties
13510 with string values stop bidi reordering, so every
13511 buffer position to the left of the string is always
13512 smaller than any position to the right of the
13513 string. Therefore, if a `cursor' property on one
13514 of the string's characters has an integer value, we
13515 will break out of the loop below _before_ we get to
13516 the position match above. IOW, integer values of
13517 the `cursor' property override the "exact match for
13518 point" strategy of positioning the cursor. */
13519 /* Implementation note: bpos_max == pt_old when, e.g.,
13520 we are in an empty line, where bpos_max is set to
13521 MATRIX_ROW_START_CHARPOS, see above. */
13522 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13524 cursor = glyph;
13525 break;
13529 string_seen = 1;
13531 x += glyph->pixel_width;
13532 ++glyph;
13534 else if (glyph > end) /* row is reversed */
13535 while (!INTEGERP (glyph->object))
13537 if (BUFFERP (glyph->object))
13539 EMACS_INT dpos = glyph->charpos - pt_old;
13541 if (glyph->charpos > bpos_max)
13542 bpos_max = glyph->charpos;
13543 if (glyph->charpos < bpos_min)
13544 bpos_min = glyph->charpos;
13545 if (!glyph->avoid_cursor_p)
13547 if (dpos == 0)
13549 match_with_avoid_cursor = 0;
13550 break;
13552 if (0 > dpos && dpos > pos_before - pt_old)
13554 pos_before = glyph->charpos;
13555 glyph_before = glyph;
13557 else if (0 < dpos && dpos <= pos_after - pt_old)
13559 pos_after = glyph->charpos;
13560 glyph_after = glyph;
13563 else if (dpos == 0)
13564 match_with_avoid_cursor = 1;
13566 else if (STRINGP (glyph->object))
13568 Lisp_Object chprop;
13569 EMACS_INT glyph_pos = glyph->charpos;
13571 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13572 glyph->object);
13573 if (INTEGERP (chprop))
13575 bpos_covered = bpos_max + XINT (chprop);
13576 /* If the `cursor' property covers buffer positions up
13577 to and including point, we should display cursor on
13578 this glyph. */
13579 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13581 cursor = glyph;
13582 break;
13585 string_seen = 1;
13587 --glyph;
13588 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13590 x--; /* can't use any pixel_width */
13591 break;
13593 x -= glyph->pixel_width;
13596 /* Step 2: If we didn't find an exact match for point, we need to
13597 look for a proper place to put the cursor among glyphs between
13598 GLYPH_BEFORE and GLYPH_AFTER. */
13599 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13600 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13601 && bpos_covered < pt_old)
13603 /* An empty line has a single glyph whose OBJECT is zero and
13604 whose CHARPOS is the position of a newline on that line.
13605 Note that on a TTY, there are more glyphs after that, which
13606 were produced by extend_face_to_end_of_line, but their
13607 CHARPOS is zero or negative. */
13608 int empty_line_p =
13609 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13610 && INTEGERP (glyph->object) && glyph->charpos > 0;
13612 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13614 EMACS_INT ellipsis_pos;
13616 /* Scan back over the ellipsis glyphs. */
13617 if (!row->reversed_p)
13619 ellipsis_pos = (glyph - 1)->charpos;
13620 while (glyph > row->glyphs[TEXT_AREA]
13621 && (glyph - 1)->charpos == ellipsis_pos)
13622 glyph--, x -= glyph->pixel_width;
13623 /* That loop always goes one position too far, including
13624 the glyph before the ellipsis. So scan forward over
13625 that one. */
13626 x += glyph->pixel_width;
13627 glyph++;
13629 else /* row is reversed */
13631 ellipsis_pos = (glyph + 1)->charpos;
13632 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13633 && (glyph + 1)->charpos == ellipsis_pos)
13634 glyph++, x += glyph->pixel_width;
13635 x -= glyph->pixel_width;
13636 glyph--;
13639 else if (match_with_avoid_cursor
13640 /* A truncated row may not include PT among its
13641 character positions. Setting the cursor inside the
13642 scroll margin will trigger recalculation of hscroll
13643 in hscroll_window_tree. */
13644 || (row->truncated_on_left_p && pt_old < bpos_min)
13645 || (row->truncated_on_right_p && pt_old > bpos_max)
13646 /* Zero-width characters produce no glyphs. */
13647 || (!string_seen
13648 && !empty_line_p
13649 && (row->reversed_p
13650 ? glyph_after > glyphs_end
13651 : glyph_after < glyphs_end)))
13653 cursor = glyph_after;
13654 x = -1;
13656 else if (string_seen)
13658 int incr = row->reversed_p ? -1 : +1;
13660 /* Need to find the glyph that came out of a string which is
13661 present at point. That glyph is somewhere between
13662 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13663 positioned between POS_BEFORE and POS_AFTER in the
13664 buffer. */
13665 struct glyph *start, *stop;
13666 EMACS_INT pos = pos_before;
13668 x = -1;
13670 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13671 correspond to POS_BEFORE and POS_AFTER, respectively. We
13672 need START and STOP in the order that corresponds to the
13673 row's direction as given by its reversed_p flag. If the
13674 directionality of characters between POS_BEFORE and
13675 POS_AFTER is the opposite of the row's base direction,
13676 these characters will have been reordered for display,
13677 and we need to reverse START and STOP. */
13678 if (!row->reversed_p)
13680 start = min (glyph_before, glyph_after);
13681 stop = max (glyph_before, glyph_after);
13683 else
13685 start = max (glyph_before, glyph_after);
13686 stop = min (glyph_before, glyph_after);
13688 for (glyph = start + incr;
13689 row->reversed_p ? glyph > stop : glyph < stop; )
13692 /* Any glyphs that come from the buffer are here because
13693 of bidi reordering. Skip them, and only pay
13694 attention to glyphs that came from some string. */
13695 if (STRINGP (glyph->object))
13697 Lisp_Object str;
13698 EMACS_INT tem;
13699 /* If the display property covers the newline, we
13700 need to search for it one position farther. */
13701 EMACS_INT lim = pos_after
13702 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13704 string_from_text_prop = 0;
13705 str = glyph->object;
13706 tem = string_buffer_position_lim (str, pos, lim, 0);
13707 if (tem == 0 /* from overlay */
13708 || pos <= tem)
13710 /* If the string from which this glyph came is
13711 found in the buffer at point, then we've
13712 found the glyph we've been looking for. If
13713 it comes from an overlay (tem == 0), and it
13714 has the `cursor' property on one of its
13715 glyphs, record that glyph as a candidate for
13716 displaying the cursor. (As in the
13717 unidirectional version, we will display the
13718 cursor on the last candidate we find.) */
13719 if (tem == 0 || tem == pt_old)
13721 /* The glyphs from this string could have
13722 been reordered. Find the one with the
13723 smallest string position. Or there could
13724 be a character in the string with the
13725 `cursor' property, which means display
13726 cursor on that character's glyph. */
13727 EMACS_INT strpos = glyph->charpos;
13729 if (tem)
13731 cursor = glyph;
13732 string_from_text_prop = 1;
13734 for ( ;
13735 (row->reversed_p ? glyph > stop : glyph < stop)
13736 && EQ (glyph->object, str);
13737 glyph += incr)
13739 Lisp_Object cprop;
13740 EMACS_INT gpos = glyph->charpos;
13742 cprop = Fget_char_property (make_number (gpos),
13743 Qcursor,
13744 glyph->object);
13745 if (!NILP (cprop))
13747 cursor = glyph;
13748 break;
13750 if (tem && glyph->charpos < strpos)
13752 strpos = glyph->charpos;
13753 cursor = glyph;
13757 if (tem == pt_old)
13758 goto compute_x;
13760 if (tem)
13761 pos = tem + 1; /* don't find previous instances */
13763 /* This string is not what we want; skip all of the
13764 glyphs that came from it. */
13765 while ((row->reversed_p ? glyph > stop : glyph < stop)
13766 && EQ (glyph->object, str))
13767 glyph += incr;
13769 else
13770 glyph += incr;
13773 /* If we reached the end of the line, and END was from a string,
13774 the cursor is not on this line. */
13775 if (cursor == NULL
13776 && (row->reversed_p ? glyph <= end : glyph >= end)
13777 && STRINGP (end->object)
13778 && row->continued_p)
13779 return 0;
13783 compute_x:
13784 if (cursor != NULL)
13785 glyph = cursor;
13786 if (x < 0)
13788 struct glyph *g;
13790 /* Need to compute x that corresponds to GLYPH. */
13791 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13793 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13794 abort ();
13795 x += g->pixel_width;
13799 /* ROW could be part of a continued line, which, under bidi
13800 reordering, might have other rows whose start and end charpos
13801 occlude point. Only set w->cursor if we found a better
13802 approximation to the cursor position than we have from previously
13803 examined candidate rows belonging to the same continued line. */
13804 if (/* we already have a candidate row */
13805 w->cursor.vpos >= 0
13806 /* that candidate is not the row we are processing */
13807 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13808 /* Make sure cursor.vpos specifies a row whose start and end
13809 charpos occlude point. This is because some callers of this
13810 function leave cursor.vpos at the row where the cursor was
13811 displayed during the last redisplay cycle. */
13812 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13813 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13815 struct glyph *g1 =
13816 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13818 /* Don't consider glyphs that are outside TEXT_AREA. */
13819 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13820 return 0;
13821 /* Keep the candidate whose buffer position is the closest to
13822 point or has the `cursor' property. */
13823 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13824 w->cursor.hpos >= 0
13825 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13826 && ((BUFFERP (g1->object)
13827 && (g1->charpos == pt_old /* an exact match always wins */
13828 || (BUFFERP (glyph->object)
13829 && eabs (g1->charpos - pt_old)
13830 < eabs (glyph->charpos - pt_old))))
13831 /* previous candidate is a glyph from a string that has
13832 a non-nil `cursor' property */
13833 || (STRINGP (g1->object)
13834 && (!NILP (Fget_char_property (make_number (g1->charpos),
13835 Qcursor, g1->object))
13836 /* pevious candidate is from the same display
13837 string as this one, and the display string
13838 came from a text property */
13839 || (EQ (g1->object, glyph->object)
13840 && string_from_text_prop)
13841 /* this candidate is from newline and its
13842 position is not an exact match */
13843 || (INTEGERP (glyph->object)
13844 && glyph->charpos != pt_old)))))
13845 return 0;
13846 /* If this candidate gives an exact match, use that. */
13847 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13848 /* Otherwise, keep the candidate that comes from a row
13849 spanning less buffer positions. This may win when one or
13850 both candidate positions are on glyphs that came from
13851 display strings, for which we cannot compare buffer
13852 positions. */
13853 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13854 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13855 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13856 return 0;
13858 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13859 w->cursor.x = x;
13860 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13861 w->cursor.y = row->y + dy;
13863 if (w == XWINDOW (selected_window))
13865 if (!row->continued_p
13866 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13867 && row->x == 0)
13869 this_line_buffer = XBUFFER (w->buffer);
13871 CHARPOS (this_line_start_pos)
13872 = MATRIX_ROW_START_CHARPOS (row) + delta;
13873 BYTEPOS (this_line_start_pos)
13874 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13876 CHARPOS (this_line_end_pos)
13877 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13878 BYTEPOS (this_line_end_pos)
13879 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13881 this_line_y = w->cursor.y;
13882 this_line_pixel_height = row->height;
13883 this_line_vpos = w->cursor.vpos;
13884 this_line_start_x = row->x;
13886 else
13887 CHARPOS (this_line_start_pos) = 0;
13890 return 1;
13894 /* Run window scroll functions, if any, for WINDOW with new window
13895 start STARTP. Sets the window start of WINDOW to that position.
13897 We assume that the window's buffer is really current. */
13899 static inline struct text_pos
13900 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13902 struct window *w = XWINDOW (window);
13903 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13905 if (current_buffer != XBUFFER (w->buffer))
13906 abort ();
13908 if (!NILP (Vwindow_scroll_functions))
13910 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13911 make_number (CHARPOS (startp)));
13912 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13913 /* In case the hook functions switch buffers. */
13914 if (current_buffer != XBUFFER (w->buffer))
13915 set_buffer_internal_1 (XBUFFER (w->buffer));
13918 return startp;
13922 /* Make sure the line containing the cursor is fully visible.
13923 A value of 1 means there is nothing to be done.
13924 (Either the line is fully visible, or it cannot be made so,
13925 or we cannot tell.)
13927 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13928 is higher than window.
13930 A value of 0 means the caller should do scrolling
13931 as if point had gone off the screen. */
13933 static int
13934 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13936 struct glyph_matrix *matrix;
13937 struct glyph_row *row;
13938 int window_height;
13940 if (!make_cursor_line_fully_visible_p)
13941 return 1;
13943 /* It's not always possible to find the cursor, e.g, when a window
13944 is full of overlay strings. Don't do anything in that case. */
13945 if (w->cursor.vpos < 0)
13946 return 1;
13948 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13949 row = MATRIX_ROW (matrix, w->cursor.vpos);
13951 /* If the cursor row is not partially visible, there's nothing to do. */
13952 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13953 return 1;
13955 /* If the row the cursor is in is taller than the window's height,
13956 it's not clear what to do, so do nothing. */
13957 window_height = window_box_height (w);
13958 if (row->height >= window_height)
13960 if (!force_p || MINI_WINDOW_P (w)
13961 || w->vscroll || w->cursor.vpos == 0)
13962 return 1;
13964 return 0;
13968 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13969 non-zero means only WINDOW is redisplayed in redisplay_internal.
13970 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13971 in redisplay_window to bring a partially visible line into view in
13972 the case that only the cursor has moved.
13974 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13975 last screen line's vertical height extends past the end of the screen.
13977 Value is
13979 1 if scrolling succeeded
13981 0 if scrolling didn't find point.
13983 -1 if new fonts have been loaded so that we must interrupt
13984 redisplay, adjust glyph matrices, and try again. */
13986 enum
13988 SCROLLING_SUCCESS,
13989 SCROLLING_FAILED,
13990 SCROLLING_NEED_LARGER_MATRICES
13993 /* If scroll-conservatively is more than this, never recenter.
13995 If you change this, don't forget to update the doc string of
13996 `scroll-conservatively' and the Emacs manual. */
13997 #define SCROLL_LIMIT 100
13999 static int
14000 try_scrolling (Lisp_Object window, int just_this_one_p,
14001 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14002 int temp_scroll_step, int last_line_misfit)
14004 struct window *w = XWINDOW (window);
14005 struct frame *f = XFRAME (w->frame);
14006 struct text_pos pos, startp;
14007 struct it it;
14008 int this_scroll_margin, scroll_max, rc, height;
14009 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14010 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14011 Lisp_Object aggressive;
14012 /* We will never try scrolling more than this number of lines. */
14013 int scroll_limit = SCROLL_LIMIT;
14015 #if GLYPH_DEBUG
14016 debug_method_add (w, "try_scrolling");
14017 #endif
14019 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14021 /* Compute scroll margin height in pixels. We scroll when point is
14022 within this distance from the top or bottom of the window. */
14023 if (scroll_margin > 0)
14024 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14025 * FRAME_LINE_HEIGHT (f);
14026 else
14027 this_scroll_margin = 0;
14029 /* Force arg_scroll_conservatively to have a reasonable value, to
14030 avoid scrolling too far away with slow move_it_* functions. Note
14031 that the user can supply scroll-conservatively equal to
14032 `most-positive-fixnum', which can be larger than INT_MAX. */
14033 if (arg_scroll_conservatively > scroll_limit)
14035 arg_scroll_conservatively = scroll_limit + 1;
14036 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14038 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14039 /* Compute how much we should try to scroll maximally to bring
14040 point into view. */
14041 scroll_max = (max (scroll_step,
14042 max (arg_scroll_conservatively, temp_scroll_step))
14043 * FRAME_LINE_HEIGHT (f));
14044 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14045 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14046 /* We're trying to scroll because of aggressive scrolling but no
14047 scroll_step is set. Choose an arbitrary one. */
14048 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14049 else
14050 scroll_max = 0;
14052 too_near_end:
14054 /* Decide whether to scroll down. */
14055 if (PT > CHARPOS (startp))
14057 int scroll_margin_y;
14059 /* Compute the pixel ypos of the scroll margin, then move it to
14060 either that ypos or PT, whichever comes first. */
14061 start_display (&it, w, startp);
14062 scroll_margin_y = it.last_visible_y - this_scroll_margin
14063 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14064 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14065 (MOVE_TO_POS | MOVE_TO_Y));
14067 if (PT > CHARPOS (it.current.pos))
14069 int y0 = line_bottom_y (&it);
14070 /* Compute how many pixels below window bottom to stop searching
14071 for PT. This avoids costly search for PT that is far away if
14072 the user limited scrolling by a small number of lines, but
14073 always finds PT if scroll_conservatively is set to a large
14074 number, such as most-positive-fixnum. */
14075 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14076 int y_to_move = it.last_visible_y + slack;
14078 /* Compute the distance from the scroll margin to PT or to
14079 the scroll limit, whichever comes first. This should
14080 include the height of the cursor line, to make that line
14081 fully visible. */
14082 move_it_to (&it, PT, -1, y_to_move,
14083 -1, MOVE_TO_POS | MOVE_TO_Y);
14084 dy = line_bottom_y (&it) - y0;
14086 if (dy > scroll_max)
14087 return SCROLLING_FAILED;
14089 scroll_down_p = 1;
14093 if (scroll_down_p)
14095 /* Point is in or below the bottom scroll margin, so move the
14096 window start down. If scrolling conservatively, move it just
14097 enough down to make point visible. If scroll_step is set,
14098 move it down by scroll_step. */
14099 if (arg_scroll_conservatively)
14100 amount_to_scroll
14101 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14102 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14103 else if (scroll_step || temp_scroll_step)
14104 amount_to_scroll = scroll_max;
14105 else
14107 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14108 height = WINDOW_BOX_TEXT_HEIGHT (w);
14109 if (NUMBERP (aggressive))
14111 double float_amount = XFLOATINT (aggressive) * height;
14112 amount_to_scroll = float_amount;
14113 if (amount_to_scroll == 0 && float_amount > 0)
14114 amount_to_scroll = 1;
14115 /* Don't let point enter the scroll margin near top of
14116 the window. */
14117 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14118 amount_to_scroll = height - 2*this_scroll_margin + dy;
14122 if (amount_to_scroll <= 0)
14123 return SCROLLING_FAILED;
14125 start_display (&it, w, startp);
14126 if (arg_scroll_conservatively <= scroll_limit)
14127 move_it_vertically (&it, amount_to_scroll);
14128 else
14130 /* Extra precision for users who set scroll-conservatively
14131 to a large number: make sure the amount we scroll
14132 the window start is never less than amount_to_scroll,
14133 which was computed as distance from window bottom to
14134 point. This matters when lines at window top and lines
14135 below window bottom have different height. */
14136 struct it it1;
14137 void *it1data = NULL;
14138 /* We use a temporary it1 because line_bottom_y can modify
14139 its argument, if it moves one line down; see there. */
14140 int start_y;
14142 SAVE_IT (it1, it, it1data);
14143 start_y = line_bottom_y (&it1);
14144 do {
14145 RESTORE_IT (&it, &it, it1data);
14146 move_it_by_lines (&it, 1);
14147 SAVE_IT (it1, it, it1data);
14148 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14151 /* If STARTP is unchanged, move it down another screen line. */
14152 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14153 move_it_by_lines (&it, 1);
14154 startp = it.current.pos;
14156 else
14158 struct text_pos scroll_margin_pos = startp;
14160 /* See if point is inside the scroll margin at the top of the
14161 window. */
14162 if (this_scroll_margin)
14164 start_display (&it, w, startp);
14165 move_it_vertically (&it, this_scroll_margin);
14166 scroll_margin_pos = it.current.pos;
14169 if (PT < CHARPOS (scroll_margin_pos))
14171 /* Point is in the scroll margin at the top of the window or
14172 above what is displayed in the window. */
14173 int y0, y_to_move;
14175 /* Compute the vertical distance from PT to the scroll
14176 margin position. Move as far as scroll_max allows, or
14177 one screenful, or 10 screen lines, whichever is largest.
14178 Give up if distance is greater than scroll_max. */
14179 SET_TEXT_POS (pos, PT, PT_BYTE);
14180 start_display (&it, w, pos);
14181 y0 = it.current_y;
14182 y_to_move = max (it.last_visible_y,
14183 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14184 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14185 y_to_move, -1,
14186 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14187 dy = it.current_y - y0;
14188 if (dy > scroll_max)
14189 return SCROLLING_FAILED;
14191 /* Compute new window start. */
14192 start_display (&it, w, startp);
14194 if (arg_scroll_conservatively)
14195 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14196 max (scroll_step, temp_scroll_step));
14197 else if (scroll_step || temp_scroll_step)
14198 amount_to_scroll = scroll_max;
14199 else
14201 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14202 height = WINDOW_BOX_TEXT_HEIGHT (w);
14203 if (NUMBERP (aggressive))
14205 double float_amount = XFLOATINT (aggressive) * height;
14206 amount_to_scroll = float_amount;
14207 if (amount_to_scroll == 0 && float_amount > 0)
14208 amount_to_scroll = 1;
14209 amount_to_scroll -=
14210 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14211 /* Don't let point enter the scroll margin near
14212 bottom of the window. */
14213 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14214 amount_to_scroll = height - 2*this_scroll_margin + dy;
14218 if (amount_to_scroll <= 0)
14219 return SCROLLING_FAILED;
14221 move_it_vertically_backward (&it, amount_to_scroll);
14222 startp = it.current.pos;
14226 /* Run window scroll functions. */
14227 startp = run_window_scroll_functions (window, startp);
14229 /* Display the window. Give up if new fonts are loaded, or if point
14230 doesn't appear. */
14231 if (!try_window (window, startp, 0))
14232 rc = SCROLLING_NEED_LARGER_MATRICES;
14233 else if (w->cursor.vpos < 0)
14235 clear_glyph_matrix (w->desired_matrix);
14236 rc = SCROLLING_FAILED;
14238 else
14240 /* Maybe forget recorded base line for line number display. */
14241 if (!just_this_one_p
14242 || current_buffer->clip_changed
14243 || BEG_UNCHANGED < CHARPOS (startp))
14244 w->base_line_number = Qnil;
14246 /* If cursor ends up on a partially visible line,
14247 treat that as being off the bottom of the screen. */
14248 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14249 /* It's possible that the cursor is on the first line of the
14250 buffer, which is partially obscured due to a vscroll
14251 (Bug#7537). In that case, avoid looping forever . */
14252 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14254 clear_glyph_matrix (w->desired_matrix);
14255 ++extra_scroll_margin_lines;
14256 goto too_near_end;
14258 rc = SCROLLING_SUCCESS;
14261 return rc;
14265 /* Compute a suitable window start for window W if display of W starts
14266 on a continuation line. Value is non-zero if a new window start
14267 was computed.
14269 The new window start will be computed, based on W's width, starting
14270 from the start of the continued line. It is the start of the
14271 screen line with the minimum distance from the old start W->start. */
14273 static int
14274 compute_window_start_on_continuation_line (struct window *w)
14276 struct text_pos pos, start_pos;
14277 int window_start_changed_p = 0;
14279 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14281 /* If window start is on a continuation line... Window start may be
14282 < BEGV in case there's invisible text at the start of the
14283 buffer (M-x rmail, for example). */
14284 if (CHARPOS (start_pos) > BEGV
14285 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14287 struct it it;
14288 struct glyph_row *row;
14290 /* Handle the case that the window start is out of range. */
14291 if (CHARPOS (start_pos) < BEGV)
14292 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14293 else if (CHARPOS (start_pos) > ZV)
14294 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14296 /* Find the start of the continued line. This should be fast
14297 because scan_buffer is fast (newline cache). */
14298 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14299 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14300 row, DEFAULT_FACE_ID);
14301 reseat_at_previous_visible_line_start (&it);
14303 /* If the line start is "too far" away from the window start,
14304 say it takes too much time to compute a new window start. */
14305 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14306 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14308 int min_distance, distance;
14310 /* Move forward by display lines to find the new window
14311 start. If window width was enlarged, the new start can
14312 be expected to be > the old start. If window width was
14313 decreased, the new window start will be < the old start.
14314 So, we're looking for the display line start with the
14315 minimum distance from the old window start. */
14316 pos = it.current.pos;
14317 min_distance = INFINITY;
14318 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14319 distance < min_distance)
14321 min_distance = distance;
14322 pos = it.current.pos;
14323 move_it_by_lines (&it, 1);
14326 /* Set the window start there. */
14327 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14328 window_start_changed_p = 1;
14332 return window_start_changed_p;
14336 /* Try cursor movement in case text has not changed in window WINDOW,
14337 with window start STARTP. Value is
14339 CURSOR_MOVEMENT_SUCCESS if successful
14341 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14343 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14344 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14345 we want to scroll as if scroll-step were set to 1. See the code.
14347 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14348 which case we have to abort this redisplay, and adjust matrices
14349 first. */
14351 enum
14353 CURSOR_MOVEMENT_SUCCESS,
14354 CURSOR_MOVEMENT_CANNOT_BE_USED,
14355 CURSOR_MOVEMENT_MUST_SCROLL,
14356 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14359 static int
14360 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14362 struct window *w = XWINDOW (window);
14363 struct frame *f = XFRAME (w->frame);
14364 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14366 #if GLYPH_DEBUG
14367 if (inhibit_try_cursor_movement)
14368 return rc;
14369 #endif
14371 /* Handle case where text has not changed, only point, and it has
14372 not moved off the frame. */
14373 if (/* Point may be in this window. */
14374 PT >= CHARPOS (startp)
14375 /* Selective display hasn't changed. */
14376 && !current_buffer->clip_changed
14377 /* Function force-mode-line-update is used to force a thorough
14378 redisplay. It sets either windows_or_buffers_changed or
14379 update_mode_lines. So don't take a shortcut here for these
14380 cases. */
14381 && !update_mode_lines
14382 && !windows_or_buffers_changed
14383 && !cursor_type_changed
14384 /* Can't use this case if highlighting a region. When a
14385 region exists, cursor movement has to do more than just
14386 set the cursor. */
14387 && !(!NILP (Vtransient_mark_mode)
14388 && !NILP (BVAR (current_buffer, mark_active)))
14389 && NILP (w->region_showing)
14390 && NILP (Vshow_trailing_whitespace)
14391 /* Right after splitting windows, last_point may be nil. */
14392 && INTEGERP (w->last_point)
14393 /* This code is not used for mini-buffer for the sake of the case
14394 of redisplaying to replace an echo area message; since in
14395 that case the mini-buffer contents per se are usually
14396 unchanged. This code is of no real use in the mini-buffer
14397 since the handling of this_line_start_pos, etc., in redisplay
14398 handles the same cases. */
14399 && !EQ (window, minibuf_window)
14400 /* When splitting windows or for new windows, it happens that
14401 redisplay is called with a nil window_end_vpos or one being
14402 larger than the window. This should really be fixed in
14403 window.c. I don't have this on my list, now, so we do
14404 approximately the same as the old redisplay code. --gerd. */
14405 && INTEGERP (w->window_end_vpos)
14406 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14407 && (FRAME_WINDOW_P (f)
14408 || !overlay_arrow_in_current_buffer_p ()))
14410 int this_scroll_margin, top_scroll_margin;
14411 struct glyph_row *row = NULL;
14413 #if GLYPH_DEBUG
14414 debug_method_add (w, "cursor movement");
14415 #endif
14417 /* Scroll if point within this distance from the top or bottom
14418 of the window. This is a pixel value. */
14419 if (scroll_margin > 0)
14421 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14422 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14424 else
14425 this_scroll_margin = 0;
14427 top_scroll_margin = this_scroll_margin;
14428 if (WINDOW_WANTS_HEADER_LINE_P (w))
14429 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14431 /* Start with the row the cursor was displayed during the last
14432 not paused redisplay. Give up if that row is not valid. */
14433 if (w->last_cursor.vpos < 0
14434 || w->last_cursor.vpos >= w->current_matrix->nrows)
14435 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14436 else
14438 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14439 if (row->mode_line_p)
14440 ++row;
14441 if (!row->enabled_p)
14442 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14445 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14447 int scroll_p = 0, must_scroll = 0;
14448 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14450 if (PT > XFASTINT (w->last_point))
14452 /* Point has moved forward. */
14453 while (MATRIX_ROW_END_CHARPOS (row) < PT
14454 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14456 xassert (row->enabled_p);
14457 ++row;
14460 /* If the end position of a row equals the start
14461 position of the next row, and PT is at that position,
14462 we would rather display cursor in the next line. */
14463 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14464 && MATRIX_ROW_END_CHARPOS (row) == PT
14465 && row < w->current_matrix->rows
14466 + w->current_matrix->nrows - 1
14467 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14468 && !cursor_row_p (row))
14469 ++row;
14471 /* If within the scroll margin, scroll. Note that
14472 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14473 the next line would be drawn, and that
14474 this_scroll_margin can be zero. */
14475 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14476 || PT > MATRIX_ROW_END_CHARPOS (row)
14477 /* Line is completely visible last line in window
14478 and PT is to be set in the next line. */
14479 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14480 && PT == MATRIX_ROW_END_CHARPOS (row)
14481 && !row->ends_at_zv_p
14482 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14483 scroll_p = 1;
14485 else if (PT < XFASTINT (w->last_point))
14487 /* Cursor has to be moved backward. Note that PT >=
14488 CHARPOS (startp) because of the outer if-statement. */
14489 while (!row->mode_line_p
14490 && (MATRIX_ROW_START_CHARPOS (row) > PT
14491 || (MATRIX_ROW_START_CHARPOS (row) == PT
14492 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14493 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14494 row > w->current_matrix->rows
14495 && (row-1)->ends_in_newline_from_string_p))))
14496 && (row->y > top_scroll_margin
14497 || CHARPOS (startp) == BEGV))
14499 xassert (row->enabled_p);
14500 --row;
14503 /* Consider the following case: Window starts at BEGV,
14504 there is invisible, intangible text at BEGV, so that
14505 display starts at some point START > BEGV. It can
14506 happen that we are called with PT somewhere between
14507 BEGV and START. Try to handle that case. */
14508 if (row < w->current_matrix->rows
14509 || row->mode_line_p)
14511 row = w->current_matrix->rows;
14512 if (row->mode_line_p)
14513 ++row;
14516 /* Due to newlines in overlay strings, we may have to
14517 skip forward over overlay strings. */
14518 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14519 && MATRIX_ROW_END_CHARPOS (row) == PT
14520 && !cursor_row_p (row))
14521 ++row;
14523 /* If within the scroll margin, scroll. */
14524 if (row->y < top_scroll_margin
14525 && CHARPOS (startp) != BEGV)
14526 scroll_p = 1;
14528 else
14530 /* Cursor did not move. So don't scroll even if cursor line
14531 is partially visible, as it was so before. */
14532 rc = CURSOR_MOVEMENT_SUCCESS;
14535 if (PT < MATRIX_ROW_START_CHARPOS (row)
14536 || PT > MATRIX_ROW_END_CHARPOS (row))
14538 /* if PT is not in the glyph row, give up. */
14539 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14540 must_scroll = 1;
14542 else if (rc != CURSOR_MOVEMENT_SUCCESS
14543 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14545 /* If rows are bidi-reordered and point moved, back up
14546 until we find a row that does not belong to a
14547 continuation line. This is because we must consider
14548 all rows of a continued line as candidates for the
14549 new cursor positioning, since row start and end
14550 positions change non-linearly with vertical position
14551 in such rows. */
14552 /* FIXME: Revisit this when glyph ``spilling'' in
14553 continuation lines' rows is implemented for
14554 bidi-reordered rows. */
14555 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14557 xassert (row->enabled_p);
14558 --row;
14559 /* If we hit the beginning of the displayed portion
14560 without finding the first row of a continued
14561 line, give up. */
14562 if (row <= w->current_matrix->rows)
14564 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14565 break;
14570 if (must_scroll)
14572 else if (rc != CURSOR_MOVEMENT_SUCCESS
14573 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14574 && make_cursor_line_fully_visible_p)
14576 if (PT == MATRIX_ROW_END_CHARPOS (row)
14577 && !row->ends_at_zv_p
14578 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14579 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14580 else if (row->height > window_box_height (w))
14582 /* If we end up in a partially visible line, let's
14583 make it fully visible, except when it's taller
14584 than the window, in which case we can't do much
14585 about it. */
14586 *scroll_step = 1;
14587 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14589 else
14591 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14592 if (!cursor_row_fully_visible_p (w, 0, 1))
14593 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14594 else
14595 rc = CURSOR_MOVEMENT_SUCCESS;
14598 else if (scroll_p)
14599 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14600 else if (rc != CURSOR_MOVEMENT_SUCCESS
14601 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14603 /* With bidi-reordered rows, there could be more than
14604 one candidate row whose start and end positions
14605 occlude point. We need to let set_cursor_from_row
14606 find the best candidate. */
14607 /* FIXME: Revisit this when glyph ``spilling'' in
14608 continuation lines' rows is implemented for
14609 bidi-reordered rows. */
14610 int rv = 0;
14614 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14615 && PT <= MATRIX_ROW_END_CHARPOS (row)
14616 && cursor_row_p (row))
14617 rv |= set_cursor_from_row (w, row, w->current_matrix,
14618 0, 0, 0, 0);
14619 /* As soon as we've found the first suitable row
14620 whose ends_at_zv_p flag is set, we are done. */
14621 if (rv
14622 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14624 rc = CURSOR_MOVEMENT_SUCCESS;
14625 break;
14627 ++row;
14629 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14630 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14631 || (MATRIX_ROW_START_CHARPOS (row) == PT
14632 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14633 /* If we didn't find any candidate rows, or exited the
14634 loop before all the candidates were examined, signal
14635 to the caller that this method failed. */
14636 if (rc != CURSOR_MOVEMENT_SUCCESS
14637 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14638 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14639 else if (rv)
14640 rc = CURSOR_MOVEMENT_SUCCESS;
14642 else
14646 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14648 rc = CURSOR_MOVEMENT_SUCCESS;
14649 break;
14651 ++row;
14653 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14654 && MATRIX_ROW_START_CHARPOS (row) == PT
14655 && cursor_row_p (row));
14660 return rc;
14663 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14664 static
14665 #endif
14666 void
14667 set_vertical_scroll_bar (struct window *w)
14669 EMACS_INT start, end, whole;
14671 /* Calculate the start and end positions for the current window.
14672 At some point, it would be nice to choose between scrollbars
14673 which reflect the whole buffer size, with special markers
14674 indicating narrowing, and scrollbars which reflect only the
14675 visible region.
14677 Note that mini-buffers sometimes aren't displaying any text. */
14678 if (!MINI_WINDOW_P (w)
14679 || (w == XWINDOW (minibuf_window)
14680 && NILP (echo_area_buffer[0])))
14682 struct buffer *buf = XBUFFER (w->buffer);
14683 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14684 start = marker_position (w->start) - BUF_BEGV (buf);
14685 /* I don't think this is guaranteed to be right. For the
14686 moment, we'll pretend it is. */
14687 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14689 if (end < start)
14690 end = start;
14691 if (whole < (end - start))
14692 whole = end - start;
14694 else
14695 start = end = whole = 0;
14697 /* Indicate what this scroll bar ought to be displaying now. */
14698 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14699 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14700 (w, end - start, whole, start);
14704 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14705 selected_window is redisplayed.
14707 We can return without actually redisplaying the window if
14708 fonts_changed_p is nonzero. In that case, redisplay_internal will
14709 retry. */
14711 static void
14712 redisplay_window (Lisp_Object window, int just_this_one_p)
14714 struct window *w = XWINDOW (window);
14715 struct frame *f = XFRAME (w->frame);
14716 struct buffer *buffer = XBUFFER (w->buffer);
14717 struct buffer *old = current_buffer;
14718 struct text_pos lpoint, opoint, startp;
14719 int update_mode_line;
14720 int tem;
14721 struct it it;
14722 /* Record it now because it's overwritten. */
14723 int current_matrix_up_to_date_p = 0;
14724 int used_current_matrix_p = 0;
14725 /* This is less strict than current_matrix_up_to_date_p.
14726 It indictes that the buffer contents and narrowing are unchanged. */
14727 int buffer_unchanged_p = 0;
14728 int temp_scroll_step = 0;
14729 int count = SPECPDL_INDEX ();
14730 int rc;
14731 int centering_position = -1;
14732 int last_line_misfit = 0;
14733 EMACS_INT beg_unchanged, end_unchanged;
14735 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14736 opoint = lpoint;
14738 /* W must be a leaf window here. */
14739 xassert (!NILP (w->buffer));
14740 #if GLYPH_DEBUG
14741 *w->desired_matrix->method = 0;
14742 #endif
14744 restart:
14745 reconsider_clip_changes (w, buffer);
14747 /* Has the mode line to be updated? */
14748 update_mode_line = (!NILP (w->update_mode_line)
14749 || update_mode_lines
14750 || buffer->clip_changed
14751 || buffer->prevent_redisplay_optimizations_p);
14753 if (MINI_WINDOW_P (w))
14755 if (w == XWINDOW (echo_area_window)
14756 && !NILP (echo_area_buffer[0]))
14758 if (update_mode_line)
14759 /* We may have to update a tty frame's menu bar or a
14760 tool-bar. Example `M-x C-h C-h C-g'. */
14761 goto finish_menu_bars;
14762 else
14763 /* We've already displayed the echo area glyphs in this window. */
14764 goto finish_scroll_bars;
14766 else if ((w != XWINDOW (minibuf_window)
14767 || minibuf_level == 0)
14768 /* When buffer is nonempty, redisplay window normally. */
14769 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14770 /* Quail displays non-mini buffers in minibuffer window.
14771 In that case, redisplay the window normally. */
14772 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14774 /* W is a mini-buffer window, but it's not active, so clear
14775 it. */
14776 int yb = window_text_bottom_y (w);
14777 struct glyph_row *row;
14778 int y;
14780 for (y = 0, row = w->desired_matrix->rows;
14781 y < yb;
14782 y += row->height, ++row)
14783 blank_row (w, row, y);
14784 goto finish_scroll_bars;
14787 clear_glyph_matrix (w->desired_matrix);
14790 /* Otherwise set up data on this window; select its buffer and point
14791 value. */
14792 /* Really select the buffer, for the sake of buffer-local
14793 variables. */
14794 set_buffer_internal_1 (XBUFFER (w->buffer));
14796 current_matrix_up_to_date_p
14797 = (!NILP (w->window_end_valid)
14798 && !current_buffer->clip_changed
14799 && !current_buffer->prevent_redisplay_optimizations_p
14800 && XFASTINT (w->last_modified) >= MODIFF
14801 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14803 /* Run the window-bottom-change-functions
14804 if it is possible that the text on the screen has changed
14805 (either due to modification of the text, or any other reason). */
14806 if (!current_matrix_up_to_date_p
14807 && !NILP (Vwindow_text_change_functions))
14809 safe_run_hooks (Qwindow_text_change_functions);
14810 goto restart;
14813 beg_unchanged = BEG_UNCHANGED;
14814 end_unchanged = END_UNCHANGED;
14816 SET_TEXT_POS (opoint, PT, PT_BYTE);
14818 specbind (Qinhibit_point_motion_hooks, Qt);
14820 buffer_unchanged_p
14821 = (!NILP (w->window_end_valid)
14822 && !current_buffer->clip_changed
14823 && XFASTINT (w->last_modified) >= MODIFF
14824 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14826 /* When windows_or_buffers_changed is non-zero, we can't rely on
14827 the window end being valid, so set it to nil there. */
14828 if (windows_or_buffers_changed)
14830 /* If window starts on a continuation line, maybe adjust the
14831 window start in case the window's width changed. */
14832 if (XMARKER (w->start)->buffer == current_buffer)
14833 compute_window_start_on_continuation_line (w);
14835 w->window_end_valid = Qnil;
14838 /* Some sanity checks. */
14839 CHECK_WINDOW_END (w);
14840 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14841 abort ();
14842 if (BYTEPOS (opoint) < CHARPOS (opoint))
14843 abort ();
14845 /* If %c is in mode line, update it if needed. */
14846 if (!NILP (w->column_number_displayed)
14847 /* This alternative quickly identifies a common case
14848 where no change is needed. */
14849 && !(PT == XFASTINT (w->last_point)
14850 && XFASTINT (w->last_modified) >= MODIFF
14851 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14852 && (XFASTINT (w->column_number_displayed) != current_column ()))
14853 update_mode_line = 1;
14855 /* Count number of windows showing the selected buffer. An indirect
14856 buffer counts as its base buffer. */
14857 if (!just_this_one_p)
14859 struct buffer *current_base, *window_base;
14860 current_base = current_buffer;
14861 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14862 if (current_base->base_buffer)
14863 current_base = current_base->base_buffer;
14864 if (window_base->base_buffer)
14865 window_base = window_base->base_buffer;
14866 if (current_base == window_base)
14867 buffer_shared++;
14870 /* Point refers normally to the selected window. For any other
14871 window, set up appropriate value. */
14872 if (!EQ (window, selected_window))
14874 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14875 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14876 if (new_pt < BEGV)
14878 new_pt = BEGV;
14879 new_pt_byte = BEGV_BYTE;
14880 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14882 else if (new_pt > (ZV - 1))
14884 new_pt = ZV;
14885 new_pt_byte = ZV_BYTE;
14886 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14889 /* We don't use SET_PT so that the point-motion hooks don't run. */
14890 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14893 /* If any of the character widths specified in the display table
14894 have changed, invalidate the width run cache. It's true that
14895 this may be a bit late to catch such changes, but the rest of
14896 redisplay goes (non-fatally) haywire when the display table is
14897 changed, so why should we worry about doing any better? */
14898 if (current_buffer->width_run_cache)
14900 struct Lisp_Char_Table *disptab = buffer_display_table ();
14902 if (! disptab_matches_widthtab (disptab,
14903 XVECTOR (BVAR (current_buffer, width_table))))
14905 invalidate_region_cache (current_buffer,
14906 current_buffer->width_run_cache,
14907 BEG, Z);
14908 recompute_width_table (current_buffer, disptab);
14912 /* If window-start is screwed up, choose a new one. */
14913 if (XMARKER (w->start)->buffer != current_buffer)
14914 goto recenter;
14916 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14918 /* If someone specified a new starting point but did not insist,
14919 check whether it can be used. */
14920 if (!NILP (w->optional_new_start)
14921 && CHARPOS (startp) >= BEGV
14922 && CHARPOS (startp) <= ZV)
14924 w->optional_new_start = Qnil;
14925 start_display (&it, w, startp);
14926 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14927 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14928 if (IT_CHARPOS (it) == PT)
14929 w->force_start = Qt;
14930 /* IT may overshoot PT if text at PT is invisible. */
14931 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14932 w->force_start = Qt;
14935 force_start:
14937 /* Handle case where place to start displaying has been specified,
14938 unless the specified location is outside the accessible range. */
14939 if (!NILP (w->force_start)
14940 || w->frozen_window_start_p)
14942 /* We set this later on if we have to adjust point. */
14943 int new_vpos = -1;
14945 w->force_start = Qnil;
14946 w->vscroll = 0;
14947 w->window_end_valid = Qnil;
14949 /* Forget any recorded base line for line number display. */
14950 if (!buffer_unchanged_p)
14951 w->base_line_number = Qnil;
14953 /* Redisplay the mode line. Select the buffer properly for that.
14954 Also, run the hook window-scroll-functions
14955 because we have scrolled. */
14956 /* Note, we do this after clearing force_start because
14957 if there's an error, it is better to forget about force_start
14958 than to get into an infinite loop calling the hook functions
14959 and having them get more errors. */
14960 if (!update_mode_line
14961 || ! NILP (Vwindow_scroll_functions))
14963 update_mode_line = 1;
14964 w->update_mode_line = Qt;
14965 startp = run_window_scroll_functions (window, startp);
14968 w->last_modified = make_number (0);
14969 w->last_overlay_modified = make_number (0);
14970 if (CHARPOS (startp) < BEGV)
14971 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14972 else if (CHARPOS (startp) > ZV)
14973 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14975 /* Redisplay, then check if cursor has been set during the
14976 redisplay. Give up if new fonts were loaded. */
14977 /* We used to issue a CHECK_MARGINS argument to try_window here,
14978 but this causes scrolling to fail when point begins inside
14979 the scroll margin (bug#148) -- cyd */
14980 if (!try_window (window, startp, 0))
14982 w->force_start = Qt;
14983 clear_glyph_matrix (w->desired_matrix);
14984 goto need_larger_matrices;
14987 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14989 /* If point does not appear, try to move point so it does
14990 appear. The desired matrix has been built above, so we
14991 can use it here. */
14992 new_vpos = window_box_height (w) / 2;
14995 if (!cursor_row_fully_visible_p (w, 0, 0))
14997 /* Point does appear, but on a line partly visible at end of window.
14998 Move it back to a fully-visible line. */
14999 new_vpos = window_box_height (w);
15002 /* If we need to move point for either of the above reasons,
15003 now actually do it. */
15004 if (new_vpos >= 0)
15006 struct glyph_row *row;
15008 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15009 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15010 ++row;
15012 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15013 MATRIX_ROW_START_BYTEPOS (row));
15015 if (w != XWINDOW (selected_window))
15016 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15017 else if (current_buffer == old)
15018 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15020 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15022 /* If we are highlighting the region, then we just changed
15023 the region, so redisplay to show it. */
15024 if (!NILP (Vtransient_mark_mode)
15025 && !NILP (BVAR (current_buffer, mark_active)))
15027 clear_glyph_matrix (w->desired_matrix);
15028 if (!try_window (window, startp, 0))
15029 goto need_larger_matrices;
15033 #if GLYPH_DEBUG
15034 debug_method_add (w, "forced window start");
15035 #endif
15036 goto done;
15039 /* Handle case where text has not changed, only point, and it has
15040 not moved off the frame, and we are not retrying after hscroll.
15041 (current_matrix_up_to_date_p is nonzero when retrying.) */
15042 if (current_matrix_up_to_date_p
15043 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15044 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15046 switch (rc)
15048 case CURSOR_MOVEMENT_SUCCESS:
15049 used_current_matrix_p = 1;
15050 goto done;
15052 case CURSOR_MOVEMENT_MUST_SCROLL:
15053 goto try_to_scroll;
15055 default:
15056 abort ();
15059 /* If current starting point was originally the beginning of a line
15060 but no longer is, find a new starting point. */
15061 else if (!NILP (w->start_at_line_beg)
15062 && !(CHARPOS (startp) <= BEGV
15063 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15065 #if GLYPH_DEBUG
15066 debug_method_add (w, "recenter 1");
15067 #endif
15068 goto recenter;
15071 /* Try scrolling with try_window_id. Value is > 0 if update has
15072 been done, it is -1 if we know that the same window start will
15073 not work. It is 0 if unsuccessful for some other reason. */
15074 else if ((tem = try_window_id (w)) != 0)
15076 #if GLYPH_DEBUG
15077 debug_method_add (w, "try_window_id %d", tem);
15078 #endif
15080 if (fonts_changed_p)
15081 goto need_larger_matrices;
15082 if (tem > 0)
15083 goto done;
15085 /* Otherwise try_window_id has returned -1 which means that we
15086 don't want the alternative below this comment to execute. */
15088 else if (CHARPOS (startp) >= BEGV
15089 && CHARPOS (startp) <= ZV
15090 && PT >= CHARPOS (startp)
15091 && (CHARPOS (startp) < ZV
15092 /* Avoid starting at end of buffer. */
15093 || CHARPOS (startp) == BEGV
15094 || (XFASTINT (w->last_modified) >= MODIFF
15095 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15098 /* If first window line is a continuation line, and window start
15099 is inside the modified region, but the first change is before
15100 current window start, we must select a new window start.
15102 However, if this is the result of a down-mouse event (e.g. by
15103 extending the mouse-drag-overlay), we don't want to select a
15104 new window start, since that would change the position under
15105 the mouse, resulting in an unwanted mouse-movement rather
15106 than a simple mouse-click. */
15107 if (NILP (w->start_at_line_beg)
15108 && NILP (do_mouse_tracking)
15109 && CHARPOS (startp) > BEGV
15110 && CHARPOS (startp) > BEG + beg_unchanged
15111 && CHARPOS (startp) <= Z - end_unchanged
15112 /* Even if w->start_at_line_beg is nil, a new window may
15113 start at a line_beg, since that's how set_buffer_window
15114 sets it. So, we need to check the return value of
15115 compute_window_start_on_continuation_line. (See also
15116 bug#197). */
15117 && XMARKER (w->start)->buffer == current_buffer
15118 && compute_window_start_on_continuation_line (w))
15120 w->force_start = Qt;
15121 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15122 goto force_start;
15125 #if GLYPH_DEBUG
15126 debug_method_add (w, "same window start");
15127 #endif
15129 /* Try to redisplay starting at same place as before.
15130 If point has not moved off frame, accept the results. */
15131 if (!current_matrix_up_to_date_p
15132 /* Don't use try_window_reusing_current_matrix in this case
15133 because a window scroll function can have changed the
15134 buffer. */
15135 || !NILP (Vwindow_scroll_functions)
15136 || MINI_WINDOW_P (w)
15137 || !(used_current_matrix_p
15138 = try_window_reusing_current_matrix (w)))
15140 IF_DEBUG (debug_method_add (w, "1"));
15141 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15142 /* -1 means we need to scroll.
15143 0 means we need new matrices, but fonts_changed_p
15144 is set in that case, so we will detect it below. */
15145 goto try_to_scroll;
15148 if (fonts_changed_p)
15149 goto need_larger_matrices;
15151 if (w->cursor.vpos >= 0)
15153 if (!just_this_one_p
15154 || current_buffer->clip_changed
15155 || BEG_UNCHANGED < CHARPOS (startp))
15156 /* Forget any recorded base line for line number display. */
15157 w->base_line_number = Qnil;
15159 if (!cursor_row_fully_visible_p (w, 1, 0))
15161 clear_glyph_matrix (w->desired_matrix);
15162 last_line_misfit = 1;
15164 /* Drop through and scroll. */
15165 else
15166 goto done;
15168 else
15169 clear_glyph_matrix (w->desired_matrix);
15172 try_to_scroll:
15174 w->last_modified = make_number (0);
15175 w->last_overlay_modified = make_number (0);
15177 /* Redisplay the mode line. Select the buffer properly for that. */
15178 if (!update_mode_line)
15180 update_mode_line = 1;
15181 w->update_mode_line = Qt;
15184 /* Try to scroll by specified few lines. */
15185 if ((scroll_conservatively
15186 || emacs_scroll_step
15187 || temp_scroll_step
15188 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15189 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15190 && CHARPOS (startp) >= BEGV
15191 && CHARPOS (startp) <= ZV)
15193 /* The function returns -1 if new fonts were loaded, 1 if
15194 successful, 0 if not successful. */
15195 int ss = try_scrolling (window, just_this_one_p,
15196 scroll_conservatively,
15197 emacs_scroll_step,
15198 temp_scroll_step, last_line_misfit);
15199 switch (ss)
15201 case SCROLLING_SUCCESS:
15202 goto done;
15204 case SCROLLING_NEED_LARGER_MATRICES:
15205 goto need_larger_matrices;
15207 case SCROLLING_FAILED:
15208 break;
15210 default:
15211 abort ();
15215 /* Finally, just choose a place to start which positions point
15216 according to user preferences. */
15218 recenter:
15220 #if GLYPH_DEBUG
15221 debug_method_add (w, "recenter");
15222 #endif
15224 /* w->vscroll = 0; */
15226 /* Forget any previously recorded base line for line number display. */
15227 if (!buffer_unchanged_p)
15228 w->base_line_number = Qnil;
15230 /* Determine the window start relative to point. */
15231 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15232 it.current_y = it.last_visible_y;
15233 if (centering_position < 0)
15235 int margin =
15236 scroll_margin > 0
15237 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15238 : 0;
15239 EMACS_INT margin_pos = CHARPOS (startp);
15240 int scrolling_up;
15241 Lisp_Object aggressive;
15243 /* If there is a scroll margin at the top of the window, find
15244 its character position. */
15245 if (margin
15246 /* Cannot call start_display if startp is not in the
15247 accessible region of the buffer. This can happen when we
15248 have just switched to a different buffer and/or changed
15249 its restriction. In that case, startp is initialized to
15250 the character position 1 (BEG) because we did not yet
15251 have chance to display the buffer even once. */
15252 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15254 struct it it1;
15255 void *it1data = NULL;
15257 SAVE_IT (it1, it, it1data);
15258 start_display (&it1, w, startp);
15259 move_it_vertically (&it1, margin);
15260 margin_pos = IT_CHARPOS (it1);
15261 RESTORE_IT (&it, &it, it1data);
15263 scrolling_up = PT > margin_pos;
15264 aggressive =
15265 scrolling_up
15266 ? BVAR (current_buffer, scroll_up_aggressively)
15267 : BVAR (current_buffer, scroll_down_aggressively);
15269 if (!MINI_WINDOW_P (w)
15270 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15272 int pt_offset = 0;
15274 /* Setting scroll-conservatively overrides
15275 scroll-*-aggressively. */
15276 if (!scroll_conservatively && NUMBERP (aggressive))
15278 double float_amount = XFLOATINT (aggressive);
15280 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15281 if (pt_offset == 0 && float_amount > 0)
15282 pt_offset = 1;
15283 if (pt_offset)
15284 margin -= 1;
15286 /* Compute how much to move the window start backward from
15287 point so that point will be displayed where the user
15288 wants it. */
15289 if (scrolling_up)
15291 centering_position = it.last_visible_y;
15292 if (pt_offset)
15293 centering_position -= pt_offset;
15294 centering_position -=
15295 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
15296 /* Don't let point enter the scroll margin near top of
15297 the window. */
15298 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15299 centering_position = margin * FRAME_LINE_HEIGHT (f);
15301 else
15302 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15304 else
15305 /* Set the window start half the height of the window backward
15306 from point. */
15307 centering_position = window_box_height (w) / 2;
15309 move_it_vertically_backward (&it, centering_position);
15311 xassert (IT_CHARPOS (it) >= BEGV);
15313 /* The function move_it_vertically_backward may move over more
15314 than the specified y-distance. If it->w is small, e.g. a
15315 mini-buffer window, we may end up in front of the window's
15316 display area. Start displaying at the start of the line
15317 containing PT in this case. */
15318 if (it.current_y <= 0)
15320 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15321 move_it_vertically_backward (&it, 0);
15322 it.current_y = 0;
15325 it.current_x = it.hpos = 0;
15327 /* Set the window start position here explicitly, to avoid an
15328 infinite loop in case the functions in window-scroll-functions
15329 get errors. */
15330 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15332 /* Run scroll hooks. */
15333 startp = run_window_scroll_functions (window, it.current.pos);
15335 /* Redisplay the window. */
15336 if (!current_matrix_up_to_date_p
15337 || windows_or_buffers_changed
15338 || cursor_type_changed
15339 /* Don't use try_window_reusing_current_matrix in this case
15340 because it can have changed the buffer. */
15341 || !NILP (Vwindow_scroll_functions)
15342 || !just_this_one_p
15343 || MINI_WINDOW_P (w)
15344 || !(used_current_matrix_p
15345 = try_window_reusing_current_matrix (w)))
15346 try_window (window, startp, 0);
15348 /* If new fonts have been loaded (due to fontsets), give up. We
15349 have to start a new redisplay since we need to re-adjust glyph
15350 matrices. */
15351 if (fonts_changed_p)
15352 goto need_larger_matrices;
15354 /* If cursor did not appear assume that the middle of the window is
15355 in the first line of the window. Do it again with the next line.
15356 (Imagine a window of height 100, displaying two lines of height
15357 60. Moving back 50 from it->last_visible_y will end in the first
15358 line.) */
15359 if (w->cursor.vpos < 0)
15361 if (!NILP (w->window_end_valid)
15362 && PT >= Z - XFASTINT (w->window_end_pos))
15364 clear_glyph_matrix (w->desired_matrix);
15365 move_it_by_lines (&it, 1);
15366 try_window (window, it.current.pos, 0);
15368 else if (PT < IT_CHARPOS (it))
15370 clear_glyph_matrix (w->desired_matrix);
15371 move_it_by_lines (&it, -1);
15372 try_window (window, it.current.pos, 0);
15374 else
15376 /* Not much we can do about it. */
15380 /* Consider the following case: Window starts at BEGV, there is
15381 invisible, intangible text at BEGV, so that display starts at
15382 some point START > BEGV. It can happen that we are called with
15383 PT somewhere between BEGV and START. Try to handle that case. */
15384 if (w->cursor.vpos < 0)
15386 struct glyph_row *row = w->current_matrix->rows;
15387 if (row->mode_line_p)
15388 ++row;
15389 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15392 if (!cursor_row_fully_visible_p (w, 0, 0))
15394 /* If vscroll is enabled, disable it and try again. */
15395 if (w->vscroll)
15397 w->vscroll = 0;
15398 clear_glyph_matrix (w->desired_matrix);
15399 goto recenter;
15402 /* If centering point failed to make the whole line visible,
15403 put point at the top instead. That has to make the whole line
15404 visible, if it can be done. */
15405 if (centering_position == 0)
15406 goto done;
15408 clear_glyph_matrix (w->desired_matrix);
15409 centering_position = 0;
15410 goto recenter;
15413 done:
15415 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15416 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15417 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15418 ? Qt : Qnil);
15420 /* Display the mode line, if we must. */
15421 if ((update_mode_line
15422 /* If window not full width, must redo its mode line
15423 if (a) the window to its side is being redone and
15424 (b) we do a frame-based redisplay. This is a consequence
15425 of how inverted lines are drawn in frame-based redisplay. */
15426 || (!just_this_one_p
15427 && !FRAME_WINDOW_P (f)
15428 && !WINDOW_FULL_WIDTH_P (w))
15429 /* Line number to display. */
15430 || INTEGERP (w->base_line_pos)
15431 /* Column number is displayed and different from the one displayed. */
15432 || (!NILP (w->column_number_displayed)
15433 && (XFASTINT (w->column_number_displayed) != current_column ())))
15434 /* This means that the window has a mode line. */
15435 && (WINDOW_WANTS_MODELINE_P (w)
15436 || WINDOW_WANTS_HEADER_LINE_P (w)))
15438 display_mode_lines (w);
15440 /* If mode line height has changed, arrange for a thorough
15441 immediate redisplay using the correct mode line height. */
15442 if (WINDOW_WANTS_MODELINE_P (w)
15443 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15445 fonts_changed_p = 1;
15446 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15447 = DESIRED_MODE_LINE_HEIGHT (w);
15450 /* If header line height has changed, arrange for a thorough
15451 immediate redisplay using the correct header line height. */
15452 if (WINDOW_WANTS_HEADER_LINE_P (w)
15453 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15455 fonts_changed_p = 1;
15456 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15457 = DESIRED_HEADER_LINE_HEIGHT (w);
15460 if (fonts_changed_p)
15461 goto need_larger_matrices;
15464 if (!line_number_displayed
15465 && !BUFFERP (w->base_line_pos))
15467 w->base_line_pos = Qnil;
15468 w->base_line_number = Qnil;
15471 finish_menu_bars:
15473 /* When we reach a frame's selected window, redo the frame's menu bar. */
15474 if (update_mode_line
15475 && EQ (FRAME_SELECTED_WINDOW (f), window))
15477 int redisplay_menu_p = 0;
15479 if (FRAME_WINDOW_P (f))
15481 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15482 || defined (HAVE_NS) || defined (USE_GTK)
15483 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15484 #else
15485 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15486 #endif
15488 else
15489 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15491 if (redisplay_menu_p)
15492 display_menu_bar (w);
15494 #ifdef HAVE_WINDOW_SYSTEM
15495 if (FRAME_WINDOW_P (f))
15497 #if defined (USE_GTK) || defined (HAVE_NS)
15498 if (FRAME_EXTERNAL_TOOL_BAR (f))
15499 redisplay_tool_bar (f);
15500 #else
15501 if (WINDOWP (f->tool_bar_window)
15502 && (FRAME_TOOL_BAR_LINES (f) > 0
15503 || !NILP (Vauto_resize_tool_bars))
15504 && redisplay_tool_bar (f))
15505 ignore_mouse_drag_p = 1;
15506 #endif
15508 #endif
15511 #ifdef HAVE_WINDOW_SYSTEM
15512 if (FRAME_WINDOW_P (f)
15513 && update_window_fringes (w, (just_this_one_p
15514 || (!used_current_matrix_p && !overlay_arrow_seen)
15515 || w->pseudo_window_p)))
15517 update_begin (f);
15518 BLOCK_INPUT;
15519 if (draw_window_fringes (w, 1))
15520 x_draw_vertical_border (w);
15521 UNBLOCK_INPUT;
15522 update_end (f);
15524 #endif /* HAVE_WINDOW_SYSTEM */
15526 /* We go to this label, with fonts_changed_p nonzero,
15527 if it is necessary to try again using larger glyph matrices.
15528 We have to redeem the scroll bar even in this case,
15529 because the loop in redisplay_internal expects that. */
15530 need_larger_matrices:
15532 finish_scroll_bars:
15534 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15536 /* Set the thumb's position and size. */
15537 set_vertical_scroll_bar (w);
15539 /* Note that we actually used the scroll bar attached to this
15540 window, so it shouldn't be deleted at the end of redisplay. */
15541 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15542 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15545 /* Restore current_buffer and value of point in it. The window
15546 update may have changed the buffer, so first make sure `opoint'
15547 is still valid (Bug#6177). */
15548 if (CHARPOS (opoint) < BEGV)
15549 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15550 else if (CHARPOS (opoint) > ZV)
15551 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15552 else
15553 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15555 set_buffer_internal_1 (old);
15556 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15557 shorter. This can be caused by log truncation in *Messages*. */
15558 if (CHARPOS (lpoint) <= ZV)
15559 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15561 unbind_to (count, Qnil);
15565 /* Build the complete desired matrix of WINDOW with a window start
15566 buffer position POS.
15568 Value is 1 if successful. It is zero if fonts were loaded during
15569 redisplay which makes re-adjusting glyph matrices necessary, and -1
15570 if point would appear in the scroll margins.
15571 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15572 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15573 set in FLAGS.) */
15576 try_window (Lisp_Object window, struct text_pos pos, int flags)
15578 struct window *w = XWINDOW (window);
15579 struct it it;
15580 struct glyph_row *last_text_row = NULL;
15581 struct frame *f = XFRAME (w->frame);
15583 /* Make POS the new window start. */
15584 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15586 /* Mark cursor position as unknown. No overlay arrow seen. */
15587 w->cursor.vpos = -1;
15588 overlay_arrow_seen = 0;
15590 /* Initialize iterator and info to start at POS. */
15591 start_display (&it, w, pos);
15595 /* Display all lines of W. */
15596 while (it.current_y < it.last_visible_y)
15598 if (display_line (&it))
15599 last_text_row = it.glyph_row - 1;
15600 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15601 return 0;
15603 #ifdef HAVE_XWIDGETS_xxx
15604 //currently this is needed to detect xwidget movement reliably. or probably not.
15605 printf("try_window\n");
15606 return 0;
15607 #endif
15609 /* Don't let the cursor end in the scroll margins. */
15610 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15611 && !MINI_WINDOW_P (w))
15613 int this_scroll_margin;
15615 if (scroll_margin > 0)
15617 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15618 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15620 else
15621 this_scroll_margin = 0;
15623 if ((w->cursor.y >= 0 /* not vscrolled */
15624 && w->cursor.y < this_scroll_margin
15625 && CHARPOS (pos) > BEGV
15626 && IT_CHARPOS (it) < ZV)
15627 /* rms: considering make_cursor_line_fully_visible_p here
15628 seems to give wrong results. We don't want to recenter
15629 when the last line is partly visible, we want to allow
15630 that case to be handled in the usual way. */
15631 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15633 w->cursor.vpos = -1;
15634 clear_glyph_matrix (w->desired_matrix);
15635 return -1;
15639 /* If bottom moved off end of frame, change mode line percentage. */
15640 if (XFASTINT (w->window_end_pos) <= 0
15641 && Z != IT_CHARPOS (it))
15642 w->update_mode_line = Qt;
15644 /* Set window_end_pos to the offset of the last character displayed
15645 on the window from the end of current_buffer. Set
15646 window_end_vpos to its row number. */
15647 if (last_text_row)
15649 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15650 w->window_end_bytepos
15651 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15652 w->window_end_pos
15653 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15654 w->window_end_vpos
15655 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15656 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15657 ->displays_text_p);
15659 else
15661 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15662 w->window_end_pos = make_number (Z - ZV);
15663 w->window_end_vpos = make_number (0);
15666 /* But that is not valid info until redisplay finishes. */
15667 w->window_end_valid = Qnil;
15668 return 1;
15673 /************************************************************************
15674 Window redisplay reusing current matrix when buffer has not changed
15675 ************************************************************************/
15677 /* Try redisplay of window W showing an unchanged buffer with a
15678 different window start than the last time it was displayed by
15679 reusing its current matrix. Value is non-zero if successful.
15680 W->start is the new window start. */
15682 static int
15683 try_window_reusing_current_matrix (struct window *w)
15685 struct frame *f = XFRAME (w->frame);
15686 struct glyph_row *bottom_row;
15687 struct it it;
15688 struct run run;
15689 struct text_pos start, new_start;
15690 int nrows_scrolled, i;
15691 struct glyph_row *last_text_row;
15692 struct glyph_row *last_reused_text_row;
15693 struct glyph_row *start_row;
15694 int start_vpos, min_y, max_y;
15696 #if GLYPH_DEBUG
15697 if (inhibit_try_window_reusing)
15698 return 0;
15699 #endif
15701 #ifdef HAVE_XWIDGETS
15702 //currently this is needed to detect xwidget movement reliably. or probably not.
15703 printf("try_window_reusing_current_matrix\n");
15704 return 0;
15705 #endif
15708 if (/* This function doesn't handle terminal frames. */
15709 !FRAME_WINDOW_P (f)
15710 /* Don't try to reuse the display if windows have been split
15711 or such. */
15712 || windows_or_buffers_changed
15713 || cursor_type_changed)
15714 return 0;
15716 /* Can't do this if region may have changed. */
15717 if ((!NILP (Vtransient_mark_mode)
15718 && !NILP (BVAR (current_buffer, mark_active)))
15719 || !NILP (w->region_showing)
15720 || !NILP (Vshow_trailing_whitespace))
15721 return 0;
15723 /* If top-line visibility has changed, give up. */
15724 if (WINDOW_WANTS_HEADER_LINE_P (w)
15725 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15726 return 0;
15728 /* Give up if old or new display is scrolled vertically. We could
15729 make this function handle this, but right now it doesn't. */
15730 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15731 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15732 return 0;
15734 /* The variable new_start now holds the new window start. The old
15735 start `start' can be determined from the current matrix. */
15736 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15737 start = start_row->minpos;
15738 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15740 /* Clear the desired matrix for the display below. */
15741 clear_glyph_matrix (w->desired_matrix);
15743 if (CHARPOS (new_start) <= CHARPOS (start))
15745 /* Don't use this method if the display starts with an ellipsis
15746 displayed for invisible text. It's not easy to handle that case
15747 below, and it's certainly not worth the effort since this is
15748 not a frequent case. */
15749 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15750 return 0;
15752 IF_DEBUG (debug_method_add (w, "twu1"));
15754 /* Display up to a row that can be reused. The variable
15755 last_text_row is set to the last row displayed that displays
15756 text. Note that it.vpos == 0 if or if not there is a
15757 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15758 start_display (&it, w, new_start);
15759 w->cursor.vpos = -1;
15760 last_text_row = last_reused_text_row = NULL;
15762 while (it.current_y < it.last_visible_y
15763 && !fonts_changed_p)
15765 /* If we have reached into the characters in the START row,
15766 that means the line boundaries have changed. So we
15767 can't start copying with the row START. Maybe it will
15768 work to start copying with the following row. */
15769 while (IT_CHARPOS (it) > CHARPOS (start))
15771 /* Advance to the next row as the "start". */
15772 start_row++;
15773 start = start_row->minpos;
15774 /* If there are no more rows to try, or just one, give up. */
15775 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15776 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15777 || CHARPOS (start) == ZV)
15779 clear_glyph_matrix (w->desired_matrix);
15780 return 0;
15783 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15785 /* If we have reached alignment,
15786 we can copy the rest of the rows. */
15787 if (IT_CHARPOS (it) == CHARPOS (start))
15788 break;
15790 if (display_line (&it))
15791 last_text_row = it.glyph_row - 1;
15794 /* A value of current_y < last_visible_y means that we stopped
15795 at the previous window start, which in turn means that we
15796 have at least one reusable row. */
15797 if (it.current_y < it.last_visible_y)
15799 struct glyph_row *row;
15801 /* IT.vpos always starts from 0; it counts text lines. */
15802 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15804 /* Find PT if not already found in the lines displayed. */
15805 if (w->cursor.vpos < 0)
15807 int dy = it.current_y - start_row->y;
15809 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15810 row = row_containing_pos (w, PT, row, NULL, dy);
15811 if (row)
15812 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15813 dy, nrows_scrolled);
15814 else
15816 clear_glyph_matrix (w->desired_matrix);
15817 return 0;
15821 /* Scroll the display. Do it before the current matrix is
15822 changed. The problem here is that update has not yet
15823 run, i.e. part of the current matrix is not up to date.
15824 scroll_run_hook will clear the cursor, and use the
15825 current matrix to get the height of the row the cursor is
15826 in. */
15827 run.current_y = start_row->y;
15828 run.desired_y = it.current_y;
15829 run.height = it.last_visible_y - it.current_y;
15831 if (run.height > 0 && run.current_y != run.desired_y)
15833 update_begin (f);
15834 FRAME_RIF (f)->update_window_begin_hook (w);
15835 FRAME_RIF (f)->clear_window_mouse_face (w);
15836 FRAME_RIF (f)->scroll_run_hook (w, &run);
15837 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15838 update_end (f);
15841 /* Shift current matrix down by nrows_scrolled lines. */
15842 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15843 rotate_matrix (w->current_matrix,
15844 start_vpos,
15845 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15846 nrows_scrolled);
15848 /* Disable lines that must be updated. */
15849 for (i = 0; i < nrows_scrolled; ++i)
15850 (start_row + i)->enabled_p = 0;
15852 /* Re-compute Y positions. */
15853 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15854 max_y = it.last_visible_y;
15855 for (row = start_row + nrows_scrolled;
15856 row < bottom_row;
15857 ++row)
15859 row->y = it.current_y;
15860 row->visible_height = row->height;
15862 if (row->y < min_y)
15863 row->visible_height -= min_y - row->y;
15864 if (row->y + row->height > max_y)
15865 row->visible_height -= row->y + row->height - max_y;
15866 if (row->fringe_bitmap_periodic_p)
15867 row->redraw_fringe_bitmaps_p = 1;
15869 it.current_y += row->height;
15871 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15872 last_reused_text_row = row;
15873 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15874 break;
15877 /* Disable lines in the current matrix which are now
15878 below the window. */
15879 for (++row; row < bottom_row; ++row)
15880 row->enabled_p = row->mode_line_p = 0;
15883 /* Update window_end_pos etc.; last_reused_text_row is the last
15884 reused row from the current matrix containing text, if any.
15885 The value of last_text_row is the last displayed line
15886 containing text. */
15887 if (last_reused_text_row)
15889 w->window_end_bytepos
15890 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15891 w->window_end_pos
15892 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15893 w->window_end_vpos
15894 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15895 w->current_matrix));
15897 else if (last_text_row)
15899 w->window_end_bytepos
15900 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15901 w->window_end_pos
15902 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15903 w->window_end_vpos
15904 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15906 else
15908 /* This window must be completely empty. */
15909 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15910 w->window_end_pos = make_number (Z - ZV);
15911 w->window_end_vpos = make_number (0);
15913 w->window_end_valid = Qnil;
15915 /* Update hint: don't try scrolling again in update_window. */
15916 w->desired_matrix->no_scrolling_p = 1;
15918 #if GLYPH_DEBUG
15919 debug_method_add (w, "try_window_reusing_current_matrix 1");
15920 #endif
15921 return 1;
15923 else if (CHARPOS (new_start) > CHARPOS (start))
15925 struct glyph_row *pt_row, *row;
15926 struct glyph_row *first_reusable_row;
15927 struct glyph_row *first_row_to_display;
15928 int dy;
15929 int yb = window_text_bottom_y (w);
15931 /* Find the row starting at new_start, if there is one. Don't
15932 reuse a partially visible line at the end. */
15933 first_reusable_row = start_row;
15934 while (first_reusable_row->enabled_p
15935 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15936 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15937 < CHARPOS (new_start)))
15938 ++first_reusable_row;
15940 /* Give up if there is no row to reuse. */
15941 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15942 || !first_reusable_row->enabled_p
15943 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15944 != CHARPOS (new_start)))
15945 return 0;
15947 /* We can reuse fully visible rows beginning with
15948 first_reusable_row to the end of the window. Set
15949 first_row_to_display to the first row that cannot be reused.
15950 Set pt_row to the row containing point, if there is any. */
15951 pt_row = NULL;
15952 for (first_row_to_display = first_reusable_row;
15953 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15954 ++first_row_to_display)
15956 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15957 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15958 pt_row = first_row_to_display;
15961 /* Start displaying at the start of first_row_to_display. */
15962 xassert (first_row_to_display->y < yb);
15963 init_to_row_start (&it, w, first_row_to_display);
15965 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15966 - start_vpos);
15967 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15968 - nrows_scrolled);
15969 it.current_y = (first_row_to_display->y - first_reusable_row->y
15970 + WINDOW_HEADER_LINE_HEIGHT (w));
15972 /* Display lines beginning with first_row_to_display in the
15973 desired matrix. Set last_text_row to the last row displayed
15974 that displays text. */
15975 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15976 if (pt_row == NULL)
15977 w->cursor.vpos = -1;
15978 last_text_row = NULL;
15979 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15980 if (display_line (&it))
15981 last_text_row = it.glyph_row - 1;
15983 /* If point is in a reused row, adjust y and vpos of the cursor
15984 position. */
15985 if (pt_row)
15987 w->cursor.vpos -= nrows_scrolled;
15988 w->cursor.y -= first_reusable_row->y - start_row->y;
15991 /* Give up if point isn't in a row displayed or reused. (This
15992 also handles the case where w->cursor.vpos < nrows_scrolled
15993 after the calls to display_line, which can happen with scroll
15994 margins. See bug#1295.) */
15995 if (w->cursor.vpos < 0)
15997 clear_glyph_matrix (w->desired_matrix);
15998 return 0;
16001 /* Scroll the display. */
16002 run.current_y = first_reusable_row->y;
16003 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16004 run.height = it.last_visible_y - run.current_y;
16005 dy = run.current_y - run.desired_y;
16007 if (run.height)
16009 update_begin (f);
16010 FRAME_RIF (f)->update_window_begin_hook (w);
16011 FRAME_RIF (f)->clear_window_mouse_face (w);
16012 FRAME_RIF (f)->scroll_run_hook (w, &run);
16013 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16014 update_end (f);
16017 /* Adjust Y positions of reused rows. */
16018 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16019 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16020 max_y = it.last_visible_y;
16021 for (row = first_reusable_row; row < first_row_to_display; ++row)
16023 row->y -= dy;
16024 row->visible_height = row->height;
16025 if (row->y < min_y)
16026 row->visible_height -= min_y - row->y;
16027 if (row->y + row->height > max_y)
16028 row->visible_height -= row->y + row->height - max_y;
16029 if (row->fringe_bitmap_periodic_p)
16030 row->redraw_fringe_bitmaps_p = 1;
16033 /* Scroll the current matrix. */
16034 xassert (nrows_scrolled > 0);
16035 rotate_matrix (w->current_matrix,
16036 start_vpos,
16037 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16038 -nrows_scrolled);
16040 /* Disable rows not reused. */
16041 for (row -= nrows_scrolled; row < bottom_row; ++row)
16042 row->enabled_p = 0;
16044 /* Point may have moved to a different line, so we cannot assume that
16045 the previous cursor position is valid; locate the correct row. */
16046 if (pt_row)
16048 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16049 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
16050 row++)
16052 w->cursor.vpos++;
16053 w->cursor.y = row->y;
16055 if (row < bottom_row)
16057 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16058 struct glyph *end = glyph + row->used[TEXT_AREA];
16060 /* Can't use this optimization with bidi-reordered glyph
16061 rows, unless cursor is already at point. */
16062 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16064 if (!(w->cursor.hpos >= 0
16065 && w->cursor.hpos < row->used[TEXT_AREA]
16066 && BUFFERP (glyph->object)
16067 && glyph->charpos == PT))
16068 return 0;
16070 else
16071 for (; glyph < end
16072 && (!BUFFERP (glyph->object)
16073 || glyph->charpos < PT);
16074 glyph++)
16076 w->cursor.hpos++;
16077 w->cursor.x += glyph->pixel_width;
16082 /* Adjust window end. A null value of last_text_row means that
16083 the window end is in reused rows which in turn means that
16084 only its vpos can have changed. */
16085 if (last_text_row)
16087 w->window_end_bytepos
16088 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16089 w->window_end_pos
16090 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16091 w->window_end_vpos
16092 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16094 else
16096 w->window_end_vpos
16097 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16100 w->window_end_valid = Qnil;
16101 w->desired_matrix->no_scrolling_p = 1;
16103 #if GLYPH_DEBUG
16104 debug_method_add (w, "try_window_reusing_current_matrix 2");
16105 #endif
16106 return 1;
16109 return 0;
16114 /************************************************************************
16115 Window redisplay reusing current matrix when buffer has changed
16116 ************************************************************************/
16118 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16119 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16120 EMACS_INT *, EMACS_INT *);
16121 static struct glyph_row *
16122 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16123 struct glyph_row *);
16126 /* Return the last row in MATRIX displaying text. If row START is
16127 non-null, start searching with that row. IT gives the dimensions
16128 of the display. Value is null if matrix is empty; otherwise it is
16129 a pointer to the row found. */
16131 static struct glyph_row *
16132 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16133 struct glyph_row *start)
16135 struct glyph_row *row, *row_found;
16137 /* Set row_found to the last row in IT->w's current matrix
16138 displaying text. The loop looks funny but think of partially
16139 visible lines. */
16140 row_found = NULL;
16141 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16142 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16144 xassert (row->enabled_p);
16145 row_found = row;
16146 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16147 break;
16148 ++row;
16151 return row_found;
16155 /* Return the last row in the current matrix of W that is not affected
16156 by changes at the start of current_buffer that occurred since W's
16157 current matrix was built. Value is null if no such row exists.
16159 BEG_UNCHANGED us the number of characters unchanged at the start of
16160 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16161 first changed character in current_buffer. Characters at positions <
16162 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16163 when the current matrix was built. */
16165 static struct glyph_row *
16166 find_last_unchanged_at_beg_row (struct window *w)
16168 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16169 struct glyph_row *row;
16170 struct glyph_row *row_found = NULL;
16171 int yb = window_text_bottom_y (w);
16173 /* Find the last row displaying unchanged text. */
16174 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16175 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16176 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16177 ++row)
16179 if (/* If row ends before first_changed_pos, it is unchanged,
16180 except in some case. */
16181 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16182 /* When row ends in ZV and we write at ZV it is not
16183 unchanged. */
16184 && !row->ends_at_zv_p
16185 /* When first_changed_pos is the end of a continued line,
16186 row is not unchanged because it may be no longer
16187 continued. */
16188 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16189 && (row->continued_p
16190 || row->exact_window_width_line_p)))
16191 row_found = row;
16193 /* Stop if last visible row. */
16194 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16195 break;
16198 return row_found;
16202 /* Find the first glyph row in the current matrix of W that is not
16203 affected by changes at the end of current_buffer since the
16204 time W's current matrix was built.
16206 Return in *DELTA the number of chars by which buffer positions in
16207 unchanged text at the end of current_buffer must be adjusted.
16209 Return in *DELTA_BYTES the corresponding number of bytes.
16211 Value is null if no such row exists, i.e. all rows are affected by
16212 changes. */
16214 static struct glyph_row *
16215 find_first_unchanged_at_end_row (struct window *w,
16216 EMACS_INT *delta, EMACS_INT *delta_bytes)
16218 struct glyph_row *row;
16219 struct glyph_row *row_found = NULL;
16221 *delta = *delta_bytes = 0;
16223 /* Display must not have been paused, otherwise the current matrix
16224 is not up to date. */
16225 eassert (!NILP (w->window_end_valid));
16227 /* A value of window_end_pos >= END_UNCHANGED means that the window
16228 end is in the range of changed text. If so, there is no
16229 unchanged row at the end of W's current matrix. */
16230 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16231 return NULL;
16233 /* Set row to the last row in W's current matrix displaying text. */
16234 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16236 /* If matrix is entirely empty, no unchanged row exists. */
16237 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16239 /* The value of row is the last glyph row in the matrix having a
16240 meaningful buffer position in it. The end position of row
16241 corresponds to window_end_pos. This allows us to translate
16242 buffer positions in the current matrix to current buffer
16243 positions for characters not in changed text. */
16244 EMACS_INT Z_old =
16245 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16246 EMACS_INT Z_BYTE_old =
16247 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16248 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16249 struct glyph_row *first_text_row
16250 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16252 *delta = Z - Z_old;
16253 *delta_bytes = Z_BYTE - Z_BYTE_old;
16255 /* Set last_unchanged_pos to the buffer position of the last
16256 character in the buffer that has not been changed. Z is the
16257 index + 1 of the last character in current_buffer, i.e. by
16258 subtracting END_UNCHANGED we get the index of the last
16259 unchanged character, and we have to add BEG to get its buffer
16260 position. */
16261 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16262 last_unchanged_pos_old = last_unchanged_pos - *delta;
16264 /* Search backward from ROW for a row displaying a line that
16265 starts at a minimum position >= last_unchanged_pos_old. */
16266 for (; row > first_text_row; --row)
16268 /* This used to abort, but it can happen.
16269 It is ok to just stop the search instead here. KFS. */
16270 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16271 break;
16273 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16274 row_found = row;
16278 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16280 return row_found;
16284 /* Make sure that glyph rows in the current matrix of window W
16285 reference the same glyph memory as corresponding rows in the
16286 frame's frame matrix. This function is called after scrolling W's
16287 current matrix on a terminal frame in try_window_id and
16288 try_window_reusing_current_matrix. */
16290 static void
16291 sync_frame_with_window_matrix_rows (struct window *w)
16293 struct frame *f = XFRAME (w->frame);
16294 struct glyph_row *window_row, *window_row_end, *frame_row;
16296 /* Preconditions: W must be a leaf window and full-width. Its frame
16297 must have a frame matrix. */
16298 xassert (NILP (w->hchild) && NILP (w->vchild));
16299 xassert (WINDOW_FULL_WIDTH_P (w));
16300 xassert (!FRAME_WINDOW_P (f));
16302 /* If W is a full-width window, glyph pointers in W's current matrix
16303 have, by definition, to be the same as glyph pointers in the
16304 corresponding frame matrix. Note that frame matrices have no
16305 marginal areas (see build_frame_matrix). */
16306 window_row = w->current_matrix->rows;
16307 window_row_end = window_row + w->current_matrix->nrows;
16308 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16309 while (window_row < window_row_end)
16311 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16312 struct glyph *end = window_row->glyphs[LAST_AREA];
16314 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16315 frame_row->glyphs[TEXT_AREA] = start;
16316 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16317 frame_row->glyphs[LAST_AREA] = end;
16319 /* Disable frame rows whose corresponding window rows have
16320 been disabled in try_window_id. */
16321 if (!window_row->enabled_p)
16322 frame_row->enabled_p = 0;
16324 ++window_row, ++frame_row;
16329 /* Find the glyph row in window W containing CHARPOS. Consider all
16330 rows between START and END (not inclusive). END null means search
16331 all rows to the end of the display area of W. Value is the row
16332 containing CHARPOS or null. */
16334 struct glyph_row *
16335 row_containing_pos (struct window *w, EMACS_INT charpos,
16336 struct glyph_row *start, struct glyph_row *end, int dy)
16338 struct glyph_row *row = start;
16339 struct glyph_row *best_row = NULL;
16340 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16341 int last_y;
16343 /* If we happen to start on a header-line, skip that. */
16344 if (row->mode_line_p)
16345 ++row;
16347 if ((end && row >= end) || !row->enabled_p)
16348 return NULL;
16350 last_y = window_text_bottom_y (w) - dy;
16352 while (1)
16354 /* Give up if we have gone too far. */
16355 if (end && row >= end)
16356 return NULL;
16357 /* This formerly returned if they were equal.
16358 I think that both quantities are of a "last plus one" type;
16359 if so, when they are equal, the row is within the screen. -- rms. */
16360 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16361 return NULL;
16363 /* If it is in this row, return this row. */
16364 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16365 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16366 /* The end position of a row equals the start
16367 position of the next row. If CHARPOS is there, we
16368 would rather display it in the next line, except
16369 when this line ends in ZV. */
16370 && !row->ends_at_zv_p
16371 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16372 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16374 struct glyph *g;
16376 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16377 || (!best_row && !row->continued_p))
16378 return row;
16379 /* In bidi-reordered rows, there could be several rows
16380 occluding point, all of them belonging to the same
16381 continued line. We need to find the row which fits
16382 CHARPOS the best. */
16383 for (g = row->glyphs[TEXT_AREA];
16384 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16385 g++)
16387 if (!STRINGP (g->object))
16389 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16391 mindif = eabs (g->charpos - charpos);
16392 best_row = row;
16393 /* Exact match always wins. */
16394 if (mindif == 0)
16395 return best_row;
16400 else if (best_row && !row->continued_p)
16401 return best_row;
16402 ++row;
16407 /* Try to redisplay window W by reusing its existing display. W's
16408 current matrix must be up to date when this function is called,
16409 i.e. window_end_valid must not be nil.
16411 Value is
16413 1 if display has been updated
16414 0 if otherwise unsuccessful
16415 -1 if redisplay with same window start is known not to succeed
16417 The following steps are performed:
16419 1. Find the last row in the current matrix of W that is not
16420 affected by changes at the start of current_buffer. If no such row
16421 is found, give up.
16423 2. Find the first row in W's current matrix that is not affected by
16424 changes at the end of current_buffer. Maybe there is no such row.
16426 3. Display lines beginning with the row + 1 found in step 1 to the
16427 row found in step 2 or, if step 2 didn't find a row, to the end of
16428 the window.
16430 4. If cursor is not known to appear on the window, give up.
16432 5. If display stopped at the row found in step 2, scroll the
16433 display and current matrix as needed.
16435 6. Maybe display some lines at the end of W, if we must. This can
16436 happen under various circumstances, like a partially visible line
16437 becoming fully visible, or because newly displayed lines are displayed
16438 in smaller font sizes.
16440 7. Update W's window end information. */
16442 static int
16443 try_window_id (struct window *w)
16445 struct frame *f = XFRAME (w->frame);
16446 struct glyph_matrix *current_matrix = w->current_matrix;
16447 struct glyph_matrix *desired_matrix = w->desired_matrix;
16448 struct glyph_row *last_unchanged_at_beg_row;
16449 struct glyph_row *first_unchanged_at_end_row;
16450 struct glyph_row *row;
16451 struct glyph_row *bottom_row;
16452 int bottom_vpos;
16453 struct it it;
16454 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16455 int dvpos, dy;
16456 struct text_pos start_pos;
16457 struct run run;
16458 int first_unchanged_at_end_vpos = 0;
16459 struct glyph_row *last_text_row, *last_text_row_at_end;
16460 struct text_pos start;
16461 EMACS_INT first_changed_charpos, last_changed_charpos;
16463 #if GLYPH_DEBUG
16464 if (inhibit_try_window_id)
16465 return 0;
16466 #endif
16468 #ifdef HAVE_XWIDGETS
16469 //maybe needed for proper xwidget movement
16470 printf("try_window_id\n");
16471 return -1;
16472 #endif
16475 /* This is handy for debugging. */
16476 #if 0
16477 #define GIVE_UP(X) \
16478 do { \
16479 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16480 return 0; \
16481 } while (0)
16482 #else
16483 #define GIVE_UP(X) return 0
16484 #endif
16486 SET_TEXT_POS_FROM_MARKER (start, w->start);
16488 /* Don't use this for mini-windows because these can show
16489 messages and mini-buffers, and we don't handle that here. */
16490 if (MINI_WINDOW_P (w))
16491 GIVE_UP (1);
16493 /* This flag is used to prevent redisplay optimizations. */
16494 if (windows_or_buffers_changed || cursor_type_changed)
16495 GIVE_UP (2);
16497 /* Verify that narrowing has not changed.
16498 Also verify that we were not told to prevent redisplay optimizations.
16499 It would be nice to further
16500 reduce the number of cases where this prevents try_window_id. */
16501 if (current_buffer->clip_changed
16502 || current_buffer->prevent_redisplay_optimizations_p)
16503 GIVE_UP (3);
16505 /* Window must either use window-based redisplay or be full width. */
16506 if (!FRAME_WINDOW_P (f)
16507 && (!FRAME_LINE_INS_DEL_OK (f)
16508 || !WINDOW_FULL_WIDTH_P (w)))
16509 GIVE_UP (4);
16511 /* Give up if point is known NOT to appear in W. */
16512 if (PT < CHARPOS (start))
16513 GIVE_UP (5);
16515 /* Another way to prevent redisplay optimizations. */
16516 if (XFASTINT (w->last_modified) == 0)
16517 GIVE_UP (6);
16519 /* Verify that window is not hscrolled. */
16520 if (XFASTINT (w->hscroll) != 0)
16521 GIVE_UP (7);
16523 /* Verify that display wasn't paused. */
16524 if (NILP (w->window_end_valid))
16525 GIVE_UP (8);
16527 /* Can't use this if highlighting a region because a cursor movement
16528 will do more than just set the cursor. */
16529 if (!NILP (Vtransient_mark_mode)
16530 && !NILP (BVAR (current_buffer, mark_active)))
16531 GIVE_UP (9);
16533 /* Likewise if highlighting trailing whitespace. */
16534 if (!NILP (Vshow_trailing_whitespace))
16535 GIVE_UP (11);
16537 /* Likewise if showing a region. */
16538 if (!NILP (w->region_showing))
16539 GIVE_UP (10);
16541 /* Can't use this if overlay arrow position and/or string have
16542 changed. */
16543 if (overlay_arrows_changed_p ())
16544 GIVE_UP (12);
16546 /* When word-wrap is on, adding a space to the first word of a
16547 wrapped line can change the wrap position, altering the line
16548 above it. It might be worthwhile to handle this more
16549 intelligently, but for now just redisplay from scratch. */
16550 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16551 GIVE_UP (21);
16553 /* Under bidi reordering, adding or deleting a character in the
16554 beginning of a paragraph, before the first strong directional
16555 character, can change the base direction of the paragraph (unless
16556 the buffer specifies a fixed paragraph direction), which will
16557 require to redisplay the whole paragraph. It might be worthwhile
16558 to find the paragraph limits and widen the range of redisplayed
16559 lines to that, but for now just give up this optimization and
16560 redisplay from scratch. */
16561 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16562 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16563 GIVE_UP (22);
16565 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16566 only if buffer has really changed. The reason is that the gap is
16567 initially at Z for freshly visited files. The code below would
16568 set end_unchanged to 0 in that case. */
16569 if (MODIFF > SAVE_MODIFF
16570 /* This seems to happen sometimes after saving a buffer. */
16571 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16573 if (GPT - BEG < BEG_UNCHANGED)
16574 BEG_UNCHANGED = GPT - BEG;
16575 if (Z - GPT < END_UNCHANGED)
16576 END_UNCHANGED = Z - GPT;
16579 /* The position of the first and last character that has been changed. */
16580 first_changed_charpos = BEG + BEG_UNCHANGED;
16581 last_changed_charpos = Z - END_UNCHANGED;
16583 /* If window starts after a line end, and the last change is in
16584 front of that newline, then changes don't affect the display.
16585 This case happens with stealth-fontification. Note that although
16586 the display is unchanged, glyph positions in the matrix have to
16587 be adjusted, of course. */
16588 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16589 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16590 && ((last_changed_charpos < CHARPOS (start)
16591 && CHARPOS (start) == BEGV)
16592 || (last_changed_charpos < CHARPOS (start) - 1
16593 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16595 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16596 struct glyph_row *r0;
16598 /* Compute how many chars/bytes have been added to or removed
16599 from the buffer. */
16600 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16601 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16602 Z_delta = Z - Z_old;
16603 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16605 /* Give up if PT is not in the window. Note that it already has
16606 been checked at the start of try_window_id that PT is not in
16607 front of the window start. */
16608 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16609 GIVE_UP (13);
16611 /* If window start is unchanged, we can reuse the whole matrix
16612 as is, after adjusting glyph positions. No need to compute
16613 the window end again, since its offset from Z hasn't changed. */
16614 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16615 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16616 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16617 /* PT must not be in a partially visible line. */
16618 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16619 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16621 /* Adjust positions in the glyph matrix. */
16622 if (Z_delta || Z_delta_bytes)
16624 struct glyph_row *r1
16625 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16626 increment_matrix_positions (w->current_matrix,
16627 MATRIX_ROW_VPOS (r0, current_matrix),
16628 MATRIX_ROW_VPOS (r1, current_matrix),
16629 Z_delta, Z_delta_bytes);
16632 /* Set the cursor. */
16633 row = row_containing_pos (w, PT, r0, NULL, 0);
16634 if (row)
16635 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16636 else
16637 abort ();
16638 return 1;
16642 /* Handle the case that changes are all below what is displayed in
16643 the window, and that PT is in the window. This shortcut cannot
16644 be taken if ZV is visible in the window, and text has been added
16645 there that is visible in the window. */
16646 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16647 /* ZV is not visible in the window, or there are no
16648 changes at ZV, actually. */
16649 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16650 || first_changed_charpos == last_changed_charpos))
16652 struct glyph_row *r0;
16654 /* Give up if PT is not in the window. Note that it already has
16655 been checked at the start of try_window_id that PT is not in
16656 front of the window start. */
16657 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16658 GIVE_UP (14);
16660 /* If window start is unchanged, we can reuse the whole matrix
16661 as is, without changing glyph positions since no text has
16662 been added/removed in front of the window end. */
16663 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16664 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16665 /* PT must not be in a partially visible line. */
16666 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16667 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16669 /* We have to compute the window end anew since text
16670 could have been added/removed after it. */
16671 w->window_end_pos
16672 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16673 w->window_end_bytepos
16674 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16676 /* Set the cursor. */
16677 row = row_containing_pos (w, PT, r0, NULL, 0);
16678 if (row)
16679 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16680 else
16681 abort ();
16682 return 2;
16686 /* Give up if window start is in the changed area.
16688 The condition used to read
16690 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16692 but why that was tested escapes me at the moment. */
16693 if (CHARPOS (start) >= first_changed_charpos
16694 && CHARPOS (start) <= last_changed_charpos)
16695 GIVE_UP (15);
16697 /* Check that window start agrees with the start of the first glyph
16698 row in its current matrix. Check this after we know the window
16699 start is not in changed text, otherwise positions would not be
16700 comparable. */
16701 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16702 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16703 GIVE_UP (16);
16705 /* Give up if the window ends in strings. Overlay strings
16706 at the end are difficult to handle, so don't try. */
16707 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16708 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16709 GIVE_UP (20);
16711 /* Compute the position at which we have to start displaying new
16712 lines. Some of the lines at the top of the window might be
16713 reusable because they are not displaying changed text. Find the
16714 last row in W's current matrix not affected by changes at the
16715 start of current_buffer. Value is null if changes start in the
16716 first line of window. */
16717 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16718 if (last_unchanged_at_beg_row)
16720 /* Avoid starting to display in the moddle of a character, a TAB
16721 for instance. This is easier than to set up the iterator
16722 exactly, and it's not a frequent case, so the additional
16723 effort wouldn't really pay off. */
16724 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16725 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16726 && last_unchanged_at_beg_row > w->current_matrix->rows)
16727 --last_unchanged_at_beg_row;
16729 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16730 GIVE_UP (17);
16732 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16733 GIVE_UP (18);
16734 start_pos = it.current.pos;
16736 /* Start displaying new lines in the desired matrix at the same
16737 vpos we would use in the current matrix, i.e. below
16738 last_unchanged_at_beg_row. */
16739 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16740 current_matrix);
16741 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16742 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16744 xassert (it.hpos == 0 && it.current_x == 0);
16746 else
16748 /* There are no reusable lines at the start of the window.
16749 Start displaying in the first text line. */
16750 start_display (&it, w, start);
16751 it.vpos = it.first_vpos;
16752 start_pos = it.current.pos;
16755 /* Find the first row that is not affected by changes at the end of
16756 the buffer. Value will be null if there is no unchanged row, in
16757 which case we must redisplay to the end of the window. delta
16758 will be set to the value by which buffer positions beginning with
16759 first_unchanged_at_end_row have to be adjusted due to text
16760 changes. */
16761 first_unchanged_at_end_row
16762 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16763 IF_DEBUG (debug_delta = delta);
16764 IF_DEBUG (debug_delta_bytes = delta_bytes);
16766 /* Set stop_pos to the buffer position up to which we will have to
16767 display new lines. If first_unchanged_at_end_row != NULL, this
16768 is the buffer position of the start of the line displayed in that
16769 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16770 that we don't stop at a buffer position. */
16771 stop_pos = 0;
16772 if (first_unchanged_at_end_row)
16774 xassert (last_unchanged_at_beg_row == NULL
16775 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16777 /* If this is a continuation line, move forward to the next one
16778 that isn't. Changes in lines above affect this line.
16779 Caution: this may move first_unchanged_at_end_row to a row
16780 not displaying text. */
16781 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16782 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16783 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16784 < it.last_visible_y))
16785 ++first_unchanged_at_end_row;
16787 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16788 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16789 >= it.last_visible_y))
16790 first_unchanged_at_end_row = NULL;
16791 else
16793 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16794 + delta);
16795 first_unchanged_at_end_vpos
16796 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16797 xassert (stop_pos >= Z - END_UNCHANGED);
16800 else if (last_unchanged_at_beg_row == NULL)
16801 GIVE_UP (19);
16804 #if GLYPH_DEBUG
16806 /* Either there is no unchanged row at the end, or the one we have
16807 now displays text. This is a necessary condition for the window
16808 end pos calculation at the end of this function. */
16809 xassert (first_unchanged_at_end_row == NULL
16810 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16812 debug_last_unchanged_at_beg_vpos
16813 = (last_unchanged_at_beg_row
16814 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16815 : -1);
16816 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16818 #endif /* GLYPH_DEBUG != 0 */
16821 /* Display new lines. Set last_text_row to the last new line
16822 displayed which has text on it, i.e. might end up as being the
16823 line where the window_end_vpos is. */
16824 w->cursor.vpos = -1;
16825 last_text_row = NULL;
16826 overlay_arrow_seen = 0;
16827 while (it.current_y < it.last_visible_y
16828 && !fonts_changed_p
16829 && (first_unchanged_at_end_row == NULL
16830 || IT_CHARPOS (it) < stop_pos))
16832 if (display_line (&it))
16833 last_text_row = it.glyph_row - 1;
16836 if (fonts_changed_p)
16837 return -1;
16840 /* Compute differences in buffer positions, y-positions etc. for
16841 lines reused at the bottom of the window. Compute what we can
16842 scroll. */
16843 if (first_unchanged_at_end_row
16844 /* No lines reused because we displayed everything up to the
16845 bottom of the window. */
16846 && it.current_y < it.last_visible_y)
16848 dvpos = (it.vpos
16849 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16850 current_matrix));
16851 dy = it.current_y - first_unchanged_at_end_row->y;
16852 run.current_y = first_unchanged_at_end_row->y;
16853 run.desired_y = run.current_y + dy;
16854 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16856 else
16858 delta = delta_bytes = dvpos = dy
16859 = run.current_y = run.desired_y = run.height = 0;
16860 first_unchanged_at_end_row = NULL;
16862 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16865 /* Find the cursor if not already found. We have to decide whether
16866 PT will appear on this window (it sometimes doesn't, but this is
16867 not a very frequent case.) This decision has to be made before
16868 the current matrix is altered. A value of cursor.vpos < 0 means
16869 that PT is either in one of the lines beginning at
16870 first_unchanged_at_end_row or below the window. Don't care for
16871 lines that might be displayed later at the window end; as
16872 mentioned, this is not a frequent case. */
16873 if (w->cursor.vpos < 0)
16875 /* Cursor in unchanged rows at the top? */
16876 if (PT < CHARPOS (start_pos)
16877 && last_unchanged_at_beg_row)
16879 row = row_containing_pos (w, PT,
16880 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16881 last_unchanged_at_beg_row + 1, 0);
16882 if (row)
16883 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16886 /* Start from first_unchanged_at_end_row looking for PT. */
16887 else if (first_unchanged_at_end_row)
16889 row = row_containing_pos (w, PT - delta,
16890 first_unchanged_at_end_row, NULL, 0);
16891 if (row)
16892 set_cursor_from_row (w, row, w->current_matrix, delta,
16893 delta_bytes, dy, dvpos);
16896 /* Give up if cursor was not found. */
16897 if (w->cursor.vpos < 0)
16899 clear_glyph_matrix (w->desired_matrix);
16900 return -1;
16904 /* Don't let the cursor end in the scroll margins. */
16906 int this_scroll_margin, cursor_height;
16908 this_scroll_margin = max (0, scroll_margin);
16909 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16910 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16911 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16913 if ((w->cursor.y < this_scroll_margin
16914 && CHARPOS (start) > BEGV)
16915 /* Old redisplay didn't take scroll margin into account at the bottom,
16916 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16917 || (w->cursor.y + (make_cursor_line_fully_visible_p
16918 ? cursor_height + this_scroll_margin
16919 : 1)) > it.last_visible_y)
16921 w->cursor.vpos = -1;
16922 clear_glyph_matrix (w->desired_matrix);
16923 return -1;
16927 /* Scroll the display. Do it before changing the current matrix so
16928 that xterm.c doesn't get confused about where the cursor glyph is
16929 found. */
16930 if (dy && run.height)
16932 update_begin (f);
16934 if (FRAME_WINDOW_P (f))
16936 FRAME_RIF (f)->update_window_begin_hook (w);
16937 FRAME_RIF (f)->clear_window_mouse_face (w);
16938 FRAME_RIF (f)->scroll_run_hook (w, &run);
16939 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16941 else
16943 /* Terminal frame. In this case, dvpos gives the number of
16944 lines to scroll by; dvpos < 0 means scroll up. */
16945 int from_vpos
16946 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16947 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16948 int end = (WINDOW_TOP_EDGE_LINE (w)
16949 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16950 + window_internal_height (w));
16952 #if defined (HAVE_GPM) || defined (MSDOS)
16953 x_clear_window_mouse_face (w);
16954 #endif
16955 /* Perform the operation on the screen. */
16956 if (dvpos > 0)
16958 /* Scroll last_unchanged_at_beg_row to the end of the
16959 window down dvpos lines. */
16960 set_terminal_window (f, end);
16962 /* On dumb terminals delete dvpos lines at the end
16963 before inserting dvpos empty lines. */
16964 if (!FRAME_SCROLL_REGION_OK (f))
16965 ins_del_lines (f, end - dvpos, -dvpos);
16967 /* Insert dvpos empty lines in front of
16968 last_unchanged_at_beg_row. */
16969 ins_del_lines (f, from, dvpos);
16971 else if (dvpos < 0)
16973 /* Scroll up last_unchanged_at_beg_vpos to the end of
16974 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16975 set_terminal_window (f, end);
16977 /* Delete dvpos lines in front of
16978 last_unchanged_at_beg_vpos. ins_del_lines will set
16979 the cursor to the given vpos and emit |dvpos| delete
16980 line sequences. */
16981 ins_del_lines (f, from + dvpos, dvpos);
16983 /* On a dumb terminal insert dvpos empty lines at the
16984 end. */
16985 if (!FRAME_SCROLL_REGION_OK (f))
16986 ins_del_lines (f, end + dvpos, -dvpos);
16989 set_terminal_window (f, 0);
16992 update_end (f);
16995 /* Shift reused rows of the current matrix to the right position.
16996 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16997 text. */
16998 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16999 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17000 if (dvpos < 0)
17002 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17003 bottom_vpos, dvpos);
17004 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17005 bottom_vpos, 0);
17007 else if (dvpos > 0)
17009 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17010 bottom_vpos, dvpos);
17011 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17012 first_unchanged_at_end_vpos + dvpos, 0);
17015 /* For frame-based redisplay, make sure that current frame and window
17016 matrix are in sync with respect to glyph memory. */
17017 if (!FRAME_WINDOW_P (f))
17018 sync_frame_with_window_matrix_rows (w);
17020 /* Adjust buffer positions in reused rows. */
17021 if (delta || delta_bytes)
17022 increment_matrix_positions (current_matrix,
17023 first_unchanged_at_end_vpos + dvpos,
17024 bottom_vpos, delta, delta_bytes);
17026 /* Adjust Y positions. */
17027 if (dy)
17028 shift_glyph_matrix (w, current_matrix,
17029 first_unchanged_at_end_vpos + dvpos,
17030 bottom_vpos, dy);
17032 if (first_unchanged_at_end_row)
17034 first_unchanged_at_end_row += dvpos;
17035 if (first_unchanged_at_end_row->y >= it.last_visible_y
17036 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17037 first_unchanged_at_end_row = NULL;
17040 /* If scrolling up, there may be some lines to display at the end of
17041 the window. */
17042 last_text_row_at_end = NULL;
17043 if (dy < 0)
17045 /* Scrolling up can leave for example a partially visible line
17046 at the end of the window to be redisplayed. */
17047 /* Set last_row to the glyph row in the current matrix where the
17048 window end line is found. It has been moved up or down in
17049 the matrix by dvpos. */
17050 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17051 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17053 /* If last_row is the window end line, it should display text. */
17054 xassert (last_row->displays_text_p);
17056 /* If window end line was partially visible before, begin
17057 displaying at that line. Otherwise begin displaying with the
17058 line following it. */
17059 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17061 init_to_row_start (&it, w, last_row);
17062 it.vpos = last_vpos;
17063 it.current_y = last_row->y;
17065 else
17067 init_to_row_end (&it, w, last_row);
17068 it.vpos = 1 + last_vpos;
17069 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17070 ++last_row;
17073 /* We may start in a continuation line. If so, we have to
17074 get the right continuation_lines_width and current_x. */
17075 it.continuation_lines_width = last_row->continuation_lines_width;
17076 it.hpos = it.current_x = 0;
17078 /* Display the rest of the lines at the window end. */
17079 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17080 while (it.current_y < it.last_visible_y
17081 && !fonts_changed_p)
17083 /* Is it always sure that the display agrees with lines in
17084 the current matrix? I don't think so, so we mark rows
17085 displayed invalid in the current matrix by setting their
17086 enabled_p flag to zero. */
17087 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17088 if (display_line (&it))
17089 last_text_row_at_end = it.glyph_row - 1;
17093 /* Update window_end_pos and window_end_vpos. */
17094 if (first_unchanged_at_end_row
17095 && !last_text_row_at_end)
17097 /* Window end line if one of the preserved rows from the current
17098 matrix. Set row to the last row displaying text in current
17099 matrix starting at first_unchanged_at_end_row, after
17100 scrolling. */
17101 xassert (first_unchanged_at_end_row->displays_text_p);
17102 row = find_last_row_displaying_text (w->current_matrix, &it,
17103 first_unchanged_at_end_row);
17104 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17106 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17107 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17108 w->window_end_vpos
17109 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17110 xassert (w->window_end_bytepos >= 0);
17111 IF_DEBUG (debug_method_add (w, "A"));
17113 else if (last_text_row_at_end)
17115 w->window_end_pos
17116 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17117 w->window_end_bytepos
17118 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17119 w->window_end_vpos
17120 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17121 xassert (w->window_end_bytepos >= 0);
17122 IF_DEBUG (debug_method_add (w, "B"));
17124 else if (last_text_row)
17126 /* We have displayed either to the end of the window or at the
17127 end of the window, i.e. the last row with text is to be found
17128 in the desired matrix. */
17129 w->window_end_pos
17130 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17131 w->window_end_bytepos
17132 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17133 w->window_end_vpos
17134 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17135 xassert (w->window_end_bytepos >= 0);
17137 else if (first_unchanged_at_end_row == NULL
17138 && last_text_row == NULL
17139 && last_text_row_at_end == NULL)
17141 /* Displayed to end of window, but no line containing text was
17142 displayed. Lines were deleted at the end of the window. */
17143 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17144 int vpos = XFASTINT (w->window_end_vpos);
17145 struct glyph_row *current_row = current_matrix->rows + vpos;
17146 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17148 for (row = NULL;
17149 row == NULL && vpos >= first_vpos;
17150 --vpos, --current_row, --desired_row)
17152 if (desired_row->enabled_p)
17154 if (desired_row->displays_text_p)
17155 row = desired_row;
17157 else if (current_row->displays_text_p)
17158 row = current_row;
17161 xassert (row != NULL);
17162 w->window_end_vpos = make_number (vpos + 1);
17163 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17164 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17165 xassert (w->window_end_bytepos >= 0);
17166 IF_DEBUG (debug_method_add (w, "C"));
17168 else
17169 abort ();
17171 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17172 debug_end_vpos = XFASTINT (w->window_end_vpos));
17174 /* Record that display has not been completed. */
17175 w->window_end_valid = Qnil;
17176 w->desired_matrix->no_scrolling_p = 1;
17177 return 3;
17179 #undef GIVE_UP
17184 /***********************************************************************
17185 More debugging support
17186 ***********************************************************************/
17188 #if GLYPH_DEBUG
17190 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17191 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17192 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17195 /* Dump the contents of glyph matrix MATRIX on stderr.
17197 GLYPHS 0 means don't show glyph contents.
17198 GLYPHS 1 means show glyphs in short form
17199 GLYPHS > 1 means show glyphs in long form. */
17201 void
17202 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17204 int i;
17205 for (i = 0; i < matrix->nrows; ++i)
17206 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17210 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17211 the glyph row and area where the glyph comes from. */
17213 void
17214 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17216 if (glyph->type == CHAR_GLYPH)
17218 fprintf (stderr,
17219 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17220 glyph - row->glyphs[TEXT_AREA],
17221 'C',
17222 glyph->charpos,
17223 (BUFFERP (glyph->object)
17224 ? 'B'
17225 : (STRINGP (glyph->object)
17226 ? 'S'
17227 : '-')),
17228 glyph->pixel_width,
17229 glyph->u.ch,
17230 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17231 ? glyph->u.ch
17232 : '.'),
17233 glyph->face_id,
17234 glyph->left_box_line_p,
17235 glyph->right_box_line_p);
17237 else if (glyph->type == STRETCH_GLYPH)
17239 fprintf (stderr,
17240 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17241 glyph - row->glyphs[TEXT_AREA],
17242 'S',
17243 glyph->charpos,
17244 (BUFFERP (glyph->object)
17245 ? 'B'
17246 : (STRINGP (glyph->object)
17247 ? 'S'
17248 : '-')),
17249 glyph->pixel_width,
17251 '.',
17252 glyph->face_id,
17253 glyph->left_box_line_p,
17254 glyph->right_box_line_p);
17256 else if (glyph->type == IMAGE_GLYPH)
17258 fprintf (stderr,
17259 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17260 glyph - row->glyphs[TEXT_AREA],
17261 'I',
17262 glyph->charpos,
17263 (BUFFERP (glyph->object)
17264 ? 'B'
17265 : (STRINGP (glyph->object)
17266 ? 'S'
17267 : '-')),
17268 glyph->pixel_width,
17269 glyph->u.img_id,
17270 '.',
17271 glyph->face_id,
17272 glyph->left_box_line_p,
17273 glyph->right_box_line_p);
17275 else if (glyph->type == COMPOSITE_GLYPH)
17277 fprintf (stderr,
17278 " %5td %4c %6"pI"d %c %3d 0x%05x",
17279 glyph - row->glyphs[TEXT_AREA],
17280 '+',
17281 glyph->charpos,
17282 (BUFFERP (glyph->object)
17283 ? 'B'
17284 : (STRINGP (glyph->object)
17285 ? 'S'
17286 : '-')),
17287 glyph->pixel_width,
17288 glyph->u.cmp.id);
17289 if (glyph->u.cmp.automatic)
17290 fprintf (stderr,
17291 "[%d-%d]",
17292 glyph->slice.cmp.from, glyph->slice.cmp.to);
17293 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17294 glyph->face_id,
17295 glyph->left_box_line_p,
17296 glyph->right_box_line_p);
17298 #ifdef HAVE_XWIDGETS
17299 else if (glyph->type == XWIDGET_GLYPH)
17301 fprintf (stderr,
17302 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17303 glyph - row->glyphs[TEXT_AREA],
17304 'X',
17305 glyph->charpos,
17306 (BUFFERP (glyph->object)
17307 ? 'B'
17308 : (STRINGP (glyph->object)
17309 ? 'S'
17310 : '-')),
17311 glyph->pixel_width,
17312 glyph->u.xwidget,
17313 '.',
17314 glyph->face_id,
17315 glyph->left_box_line_p,
17316 glyph->right_box_line_p);
17318 // printf("dump xwidget glyph\n");
17320 #endif
17324 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17325 GLYPHS 0 means don't show glyph contents.
17326 GLYPHS 1 means show glyphs in short form
17327 GLYPHS > 1 means show glyphs in long form. */
17329 void
17330 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17332 if (glyphs != 1)
17334 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17335 fprintf (stderr, "======================================================================\n");
17337 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17338 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17339 vpos,
17340 MATRIX_ROW_START_CHARPOS (row),
17341 MATRIX_ROW_END_CHARPOS (row),
17342 row->used[TEXT_AREA],
17343 row->contains_overlapping_glyphs_p,
17344 row->enabled_p,
17345 row->truncated_on_left_p,
17346 row->truncated_on_right_p,
17347 row->continued_p,
17348 MATRIX_ROW_CONTINUATION_LINE_P (row),
17349 row->displays_text_p,
17350 row->ends_at_zv_p,
17351 row->fill_line_p,
17352 row->ends_in_middle_of_char_p,
17353 row->starts_in_middle_of_char_p,
17354 row->mouse_face_p,
17355 row->x,
17356 row->y,
17357 row->pixel_width,
17358 row->height,
17359 row->visible_height,
17360 row->ascent,
17361 row->phys_ascent);
17362 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17363 row->end.overlay_string_index,
17364 row->continuation_lines_width);
17365 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17366 CHARPOS (row->start.string_pos),
17367 CHARPOS (row->end.string_pos));
17368 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17369 row->end.dpvec_index);
17372 if (glyphs > 1)
17374 int area;
17376 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17378 struct glyph *glyph = row->glyphs[area];
17379 struct glyph *glyph_end = glyph + row->used[area];
17381 /* Glyph for a line end in text. */
17382 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17383 ++glyph_end;
17385 if (glyph < glyph_end)
17386 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17388 for (; glyph < glyph_end; ++glyph)
17389 dump_glyph (row, glyph, area);
17392 else if (glyphs == 1)
17394 int area;
17396 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17398 char *s = (char *) alloca (row->used[area] + 1);
17399 int i;
17401 for (i = 0; i < row->used[area]; ++i)
17403 struct glyph *glyph = row->glyphs[area] + i;
17404 if (glyph->type == CHAR_GLYPH
17405 && glyph->u.ch < 0x80
17406 && glyph->u.ch >= ' ')
17407 s[i] = glyph->u.ch;
17408 else
17409 s[i] = '.';
17412 s[i] = '\0';
17413 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17419 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17420 Sdump_glyph_matrix, 0, 1, "p",
17421 doc: /* Dump the current matrix of the selected window to stderr.
17422 Shows contents of glyph row structures. With non-nil
17423 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17424 glyphs in short form, otherwise show glyphs in long form. */)
17425 (Lisp_Object glyphs)
17427 struct window *w = XWINDOW (selected_window);
17428 struct buffer *buffer = XBUFFER (w->buffer);
17430 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17431 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17432 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17433 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17434 fprintf (stderr, "=============================================\n");
17435 dump_glyph_matrix (w->current_matrix,
17436 NILP (glyphs) ? 0 : XINT (glyphs));
17437 return Qnil;
17441 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17442 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17443 (void)
17445 struct frame *f = XFRAME (selected_frame);
17446 dump_glyph_matrix (f->current_matrix, 1);
17447 return Qnil;
17451 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17452 doc: /* Dump glyph row ROW to stderr.
17453 GLYPH 0 means don't dump glyphs.
17454 GLYPH 1 means dump glyphs in short form.
17455 GLYPH > 1 or omitted means dump glyphs in long form. */)
17456 (Lisp_Object row, Lisp_Object glyphs)
17458 struct glyph_matrix *matrix;
17459 int vpos;
17461 CHECK_NUMBER (row);
17462 matrix = XWINDOW (selected_window)->current_matrix;
17463 vpos = XINT (row);
17464 if (vpos >= 0 && vpos < matrix->nrows)
17465 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17466 vpos,
17467 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17468 return Qnil;
17472 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17473 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17474 GLYPH 0 means don't dump glyphs.
17475 GLYPH 1 means dump glyphs in short form.
17476 GLYPH > 1 or omitted means dump glyphs in long form. */)
17477 (Lisp_Object row, Lisp_Object glyphs)
17479 struct frame *sf = SELECTED_FRAME ();
17480 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17481 int vpos;
17483 CHECK_NUMBER (row);
17484 vpos = XINT (row);
17485 if (vpos >= 0 && vpos < m->nrows)
17486 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17487 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17488 return Qnil;
17492 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17493 doc: /* Toggle tracing of redisplay.
17494 With ARG, turn tracing on if and only if ARG is positive. */)
17495 (Lisp_Object arg)
17497 if (NILP (arg))
17498 trace_redisplay_p = !trace_redisplay_p;
17499 else
17501 arg = Fprefix_numeric_value (arg);
17502 trace_redisplay_p = XINT (arg) > 0;
17505 return Qnil;
17509 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17510 doc: /* Like `format', but print result to stderr.
17511 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17512 (ptrdiff_t nargs, Lisp_Object *args)
17514 Lisp_Object s = Fformat (nargs, args);
17515 fprintf (stderr, "%s", SDATA (s));
17516 return Qnil;
17519 #endif /* GLYPH_DEBUG */
17523 /***********************************************************************
17524 Building Desired Matrix Rows
17525 ***********************************************************************/
17527 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17528 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17530 static struct glyph_row *
17531 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17533 struct frame *f = XFRAME (WINDOW_FRAME (w));
17534 struct buffer *buffer = XBUFFER (w->buffer);
17535 struct buffer *old = current_buffer;
17536 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17537 int arrow_len = SCHARS (overlay_arrow_string);
17538 const unsigned char *arrow_end = arrow_string + arrow_len;
17539 const unsigned char *p;
17540 struct it it;
17541 int multibyte_p;
17542 int n_glyphs_before;
17544 set_buffer_temp (buffer);
17545 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17546 it.glyph_row->used[TEXT_AREA] = 0;
17547 SET_TEXT_POS (it.position, 0, 0);
17549 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17550 p = arrow_string;
17551 while (p < arrow_end)
17553 Lisp_Object face, ilisp;
17555 /* Get the next character. */
17556 if (multibyte_p)
17557 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17558 else
17560 it.c = it.char_to_display = *p, it.len = 1;
17561 if (! ASCII_CHAR_P (it.c))
17562 it.char_to_display = BYTE8_TO_CHAR (it.c);
17564 p += it.len;
17566 /* Get its face. */
17567 ilisp = make_number (p - arrow_string);
17568 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17569 it.face_id = compute_char_face (f, it.char_to_display, face);
17571 /* Compute its width, get its glyphs. */
17572 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17573 SET_TEXT_POS (it.position, -1, -1);
17574 PRODUCE_GLYPHS (&it);
17576 /* If this character doesn't fit any more in the line, we have
17577 to remove some glyphs. */
17578 if (it.current_x > it.last_visible_x)
17580 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17581 break;
17585 set_buffer_temp (old);
17586 return it.glyph_row;
17590 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17591 glyphs are only inserted for terminal frames since we can't really
17592 win with truncation glyphs when partially visible glyphs are
17593 involved. Which glyphs to insert is determined by
17594 produce_special_glyphs. */
17596 static void
17597 insert_left_trunc_glyphs (struct it *it)
17599 struct it truncate_it;
17600 struct glyph *from, *end, *to, *toend;
17602 xassert (!FRAME_WINDOW_P (it->f));
17604 /* Get the truncation glyphs. */
17605 truncate_it = *it;
17606 truncate_it.current_x = 0;
17607 truncate_it.face_id = DEFAULT_FACE_ID;
17608 truncate_it.glyph_row = &scratch_glyph_row;
17609 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17610 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17611 truncate_it.object = make_number (0);
17612 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17614 /* Overwrite glyphs from IT with truncation glyphs. */
17615 if (!it->glyph_row->reversed_p)
17617 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17618 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17619 to = it->glyph_row->glyphs[TEXT_AREA];
17620 toend = to + it->glyph_row->used[TEXT_AREA];
17622 while (from < end)
17623 *to++ = *from++;
17625 /* There may be padding glyphs left over. Overwrite them too. */
17626 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17628 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17629 while (from < end)
17630 *to++ = *from++;
17633 if (to > toend)
17634 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17636 else
17638 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17639 that back to front. */
17640 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17641 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17642 toend = it->glyph_row->glyphs[TEXT_AREA];
17643 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17645 while (from >= end && to >= toend)
17646 *to-- = *from--;
17647 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17649 from =
17650 truncate_it.glyph_row->glyphs[TEXT_AREA]
17651 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17652 while (from >= end && to >= toend)
17653 *to-- = *from--;
17655 if (from >= end)
17657 /* Need to free some room before prepending additional
17658 glyphs. */
17659 int move_by = from - end + 1;
17660 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17661 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17663 for ( ; g >= g0; g--)
17664 g[move_by] = *g;
17665 while (from >= end)
17666 *to-- = *from--;
17667 it->glyph_row->used[TEXT_AREA] += move_by;
17673 /* Compute the pixel height and width of IT->glyph_row.
17675 Most of the time, ascent and height of a display line will be equal
17676 to the max_ascent and max_height values of the display iterator
17677 structure. This is not the case if
17679 1. We hit ZV without displaying anything. In this case, max_ascent
17680 and max_height will be zero.
17682 2. We have some glyphs that don't contribute to the line height.
17683 (The glyph row flag contributes_to_line_height_p is for future
17684 pixmap extensions).
17686 The first case is easily covered by using default values because in
17687 these cases, the line height does not really matter, except that it
17688 must not be zero. */
17690 static void
17691 compute_line_metrics (struct it *it)
17693 struct glyph_row *row = it->glyph_row;
17695 if (FRAME_WINDOW_P (it->f))
17697 int i, min_y, max_y;
17699 /* The line may consist of one space only, that was added to
17700 place the cursor on it. If so, the row's height hasn't been
17701 computed yet. */
17702 if (row->height == 0)
17704 if (it->max_ascent + it->max_descent == 0)
17705 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17706 row->ascent = it->max_ascent;
17707 row->height = it->max_ascent + it->max_descent;
17708 row->phys_ascent = it->max_phys_ascent;
17709 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17710 row->extra_line_spacing = it->max_extra_line_spacing;
17713 /* Compute the width of this line. */
17714 row->pixel_width = row->x;
17715 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17716 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17718 xassert (row->pixel_width >= 0);
17719 xassert (row->ascent >= 0 && row->height > 0);
17721 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17722 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17724 /* If first line's physical ascent is larger than its logical
17725 ascent, use the physical ascent, and make the row taller.
17726 This makes accented characters fully visible. */
17727 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17728 && row->phys_ascent > row->ascent)
17730 row->height += row->phys_ascent - row->ascent;
17731 row->ascent = row->phys_ascent;
17734 /* Compute how much of the line is visible. */
17735 row->visible_height = row->height;
17737 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17738 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17740 if (row->y < min_y)
17741 row->visible_height -= min_y - row->y;
17742 if (row->y + row->height > max_y)
17743 row->visible_height -= row->y + row->height - max_y;
17745 else
17747 row->pixel_width = row->used[TEXT_AREA];
17748 if (row->continued_p)
17749 row->pixel_width -= it->continuation_pixel_width;
17750 else if (row->truncated_on_right_p)
17751 row->pixel_width -= it->truncation_pixel_width;
17752 row->ascent = row->phys_ascent = 0;
17753 row->height = row->phys_height = row->visible_height = 1;
17754 row->extra_line_spacing = 0;
17757 /* Compute a hash code for this row. */
17759 int area, i;
17760 row->hash = 0;
17761 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17762 for (i = 0; i < row->used[area]; ++i)
17763 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17764 + row->glyphs[area][i].u.val
17765 + row->glyphs[area][i].face_id
17766 + row->glyphs[area][i].padding_p
17767 + (row->glyphs[area][i].type << 2));
17770 it->max_ascent = it->max_descent = 0;
17771 it->max_phys_ascent = it->max_phys_descent = 0;
17775 /* Append one space to the glyph row of iterator IT if doing a
17776 window-based redisplay. The space has the same face as
17777 IT->face_id. Value is non-zero if a space was added.
17779 This function is called to make sure that there is always one glyph
17780 at the end of a glyph row that the cursor can be set on under
17781 window-systems. (If there weren't such a glyph we would not know
17782 how wide and tall a box cursor should be displayed).
17784 At the same time this space let's a nicely handle clearing to the
17785 end of the line if the row ends in italic text. */
17787 static int
17788 append_space_for_newline (struct it *it, int default_face_p)
17790 if (FRAME_WINDOW_P (it->f))
17792 int n = it->glyph_row->used[TEXT_AREA];
17794 if (it->glyph_row->glyphs[TEXT_AREA] + n
17795 < it->glyph_row->glyphs[1 + TEXT_AREA])
17797 /* Save some values that must not be changed.
17798 Must save IT->c and IT->len because otherwise
17799 ITERATOR_AT_END_P wouldn't work anymore after
17800 append_space_for_newline has been called. */
17801 enum display_element_type saved_what = it->what;
17802 int saved_c = it->c, saved_len = it->len;
17803 int saved_char_to_display = it->char_to_display;
17804 int saved_x = it->current_x;
17805 int saved_face_id = it->face_id;
17806 struct text_pos saved_pos;
17807 Lisp_Object saved_object;
17808 struct face *face;
17810 saved_object = it->object;
17811 saved_pos = it->position;
17813 it->what = IT_CHARACTER;
17814 memset (&it->position, 0, sizeof it->position);
17815 it->object = make_number (0);
17816 it->c = it->char_to_display = ' ';
17817 it->len = 1;
17819 if (default_face_p)
17820 it->face_id = DEFAULT_FACE_ID;
17821 else if (it->face_before_selective_p)
17822 it->face_id = it->saved_face_id;
17823 face = FACE_FROM_ID (it->f, it->face_id);
17824 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17826 PRODUCE_GLYPHS (it);
17828 it->override_ascent = -1;
17829 it->constrain_row_ascent_descent_p = 0;
17830 it->current_x = saved_x;
17831 it->object = saved_object;
17832 it->position = saved_pos;
17833 it->what = saved_what;
17834 it->face_id = saved_face_id;
17835 it->len = saved_len;
17836 it->c = saved_c;
17837 it->char_to_display = saved_char_to_display;
17838 return 1;
17842 return 0;
17846 /* Extend the face of the last glyph in the text area of IT->glyph_row
17847 to the end of the display line. Called from display_line. If the
17848 glyph row is empty, add a space glyph to it so that we know the
17849 face to draw. Set the glyph row flag fill_line_p. If the glyph
17850 row is R2L, prepend a stretch glyph to cover the empty space to the
17851 left of the leftmost glyph. */
17853 static void
17854 extend_face_to_end_of_line (struct it *it)
17856 struct face *face;
17857 struct frame *f = it->f;
17859 /* If line is already filled, do nothing. Non window-system frames
17860 get a grace of one more ``pixel'' because their characters are
17861 1-``pixel'' wide, so they hit the equality too early. This grace
17862 is needed only for R2L rows that are not continued, to produce
17863 one extra blank where we could display the cursor. */
17864 if (it->current_x >= it->last_visible_x
17865 + (!FRAME_WINDOW_P (f)
17866 && it->glyph_row->reversed_p
17867 && !it->glyph_row->continued_p))
17868 return;
17870 /* Face extension extends the background and box of IT->face_id
17871 to the end of the line. If the background equals the background
17872 of the frame, we don't have to do anything. */
17873 if (it->face_before_selective_p)
17874 face = FACE_FROM_ID (f, it->saved_face_id);
17875 else
17876 face = FACE_FROM_ID (f, it->face_id);
17878 if (FRAME_WINDOW_P (f)
17879 && it->glyph_row->displays_text_p
17880 && face->box == FACE_NO_BOX
17881 && face->background == FRAME_BACKGROUND_PIXEL (f)
17882 && !face->stipple
17883 && !it->glyph_row->reversed_p)
17884 return;
17886 /* Set the glyph row flag indicating that the face of the last glyph
17887 in the text area has to be drawn to the end of the text area. */
17888 it->glyph_row->fill_line_p = 1;
17890 /* If current character of IT is not ASCII, make sure we have the
17891 ASCII face. This will be automatically undone the next time
17892 get_next_display_element returns a multibyte character. Note
17893 that the character will always be single byte in unibyte
17894 text. */
17895 if (!ASCII_CHAR_P (it->c))
17897 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17900 if (FRAME_WINDOW_P (f))
17902 /* If the row is empty, add a space with the current face of IT,
17903 so that we know which face to draw. */
17904 if (it->glyph_row->used[TEXT_AREA] == 0)
17906 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17907 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17908 it->glyph_row->used[TEXT_AREA] = 1;
17910 #ifdef HAVE_WINDOW_SYSTEM
17911 if (it->glyph_row->reversed_p)
17913 /* Prepend a stretch glyph to the row, such that the
17914 rightmost glyph will be drawn flushed all the way to the
17915 right margin of the window. The stretch glyph that will
17916 occupy the empty space, if any, to the left of the
17917 glyphs. */
17918 struct font *font = face->font ? face->font : FRAME_FONT (f);
17919 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17920 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17921 struct glyph *g;
17922 int row_width, stretch_ascent, stretch_width;
17923 struct text_pos saved_pos;
17924 int saved_face_id, saved_avoid_cursor;
17926 for (row_width = 0, g = row_start; g < row_end; g++)
17927 row_width += g->pixel_width;
17928 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17929 if (stretch_width > 0)
17931 stretch_ascent =
17932 (((it->ascent + it->descent)
17933 * FONT_BASE (font)) / FONT_HEIGHT (font));
17934 saved_pos = it->position;
17935 memset (&it->position, 0, sizeof it->position);
17936 saved_avoid_cursor = it->avoid_cursor_p;
17937 it->avoid_cursor_p = 1;
17938 saved_face_id = it->face_id;
17939 /* The last row's stretch glyph should get the default
17940 face, to avoid painting the rest of the window with
17941 the region face, if the region ends at ZV. */
17942 if (it->glyph_row->ends_at_zv_p)
17943 it->face_id = DEFAULT_FACE_ID;
17944 else
17945 it->face_id = face->id;
17946 append_stretch_glyph (it, make_number (0), stretch_width,
17947 it->ascent + it->descent, stretch_ascent);
17948 it->position = saved_pos;
17949 it->avoid_cursor_p = saved_avoid_cursor;
17950 it->face_id = saved_face_id;
17953 #endif /* HAVE_WINDOW_SYSTEM */
17955 else
17957 /* Save some values that must not be changed. */
17958 int saved_x = it->current_x;
17959 struct text_pos saved_pos;
17960 Lisp_Object saved_object;
17961 enum display_element_type saved_what = it->what;
17962 int saved_face_id = it->face_id;
17964 saved_object = it->object;
17965 saved_pos = it->position;
17967 it->what = IT_CHARACTER;
17968 memset (&it->position, 0, sizeof it->position);
17969 it->object = make_number (0);
17970 it->c = it->char_to_display = ' ';
17971 it->len = 1;
17972 /* The last row's blank glyphs should get the default face, to
17973 avoid painting the rest of the window with the region face,
17974 if the region ends at ZV. */
17975 if (it->glyph_row->ends_at_zv_p)
17976 it->face_id = DEFAULT_FACE_ID;
17977 else
17978 it->face_id = face->id;
17980 PRODUCE_GLYPHS (it);
17982 while (it->current_x <= it->last_visible_x)
17983 PRODUCE_GLYPHS (it);
17985 /* Don't count these blanks really. It would let us insert a left
17986 truncation glyph below and make us set the cursor on them, maybe. */
17987 it->current_x = saved_x;
17988 it->object = saved_object;
17989 it->position = saved_pos;
17990 it->what = saved_what;
17991 it->face_id = saved_face_id;
17996 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17997 trailing whitespace. */
17999 static int
18000 trailing_whitespace_p (EMACS_INT charpos)
18002 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18003 int c = 0;
18005 while (bytepos < ZV_BYTE
18006 && (c = FETCH_CHAR (bytepos),
18007 c == ' ' || c == '\t'))
18008 ++bytepos;
18010 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18012 if (bytepos != PT_BYTE)
18013 return 1;
18015 return 0;
18019 /* Highlight trailing whitespace, if any, in ROW. */
18021 static void
18022 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18024 int used = row->used[TEXT_AREA];
18026 if (used)
18028 struct glyph *start = row->glyphs[TEXT_AREA];
18029 struct glyph *glyph = start + used - 1;
18031 if (row->reversed_p)
18033 /* Right-to-left rows need to be processed in the opposite
18034 direction, so swap the edge pointers. */
18035 glyph = start;
18036 start = row->glyphs[TEXT_AREA] + used - 1;
18039 /* Skip over glyphs inserted to display the cursor at the
18040 end of a line, for extending the face of the last glyph
18041 to the end of the line on terminals, and for truncation
18042 and continuation glyphs. */
18043 if (!row->reversed_p)
18045 while (glyph >= start
18046 && glyph->type == CHAR_GLYPH
18047 && INTEGERP (glyph->object))
18048 --glyph;
18050 else
18052 while (glyph <= start
18053 && glyph->type == CHAR_GLYPH
18054 && INTEGERP (glyph->object))
18055 ++glyph;
18058 /* If last glyph is a space or stretch, and it's trailing
18059 whitespace, set the face of all trailing whitespace glyphs in
18060 IT->glyph_row to `trailing-whitespace'. */
18061 if ((row->reversed_p ? glyph <= start : glyph >= start)
18062 && BUFFERP (glyph->object)
18063 && (glyph->type == STRETCH_GLYPH
18064 || (glyph->type == CHAR_GLYPH
18065 && glyph->u.ch == ' '))
18066 && trailing_whitespace_p (glyph->charpos))
18068 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18069 if (face_id < 0)
18070 return;
18072 if (!row->reversed_p)
18074 while (glyph >= start
18075 && BUFFERP (glyph->object)
18076 && (glyph->type == STRETCH_GLYPH
18077 || (glyph->type == CHAR_GLYPH
18078 && glyph->u.ch == ' ')))
18079 (glyph--)->face_id = face_id;
18081 else
18083 while (glyph <= start
18084 && BUFFERP (glyph->object)
18085 && (glyph->type == STRETCH_GLYPH
18086 || (glyph->type == CHAR_GLYPH
18087 && glyph->u.ch == ' ')))
18088 (glyph++)->face_id = face_id;
18095 /* Value is non-zero if glyph row ROW should be
18096 used to hold the cursor. */
18098 static int
18099 cursor_row_p (struct glyph_row *row)
18101 int result = 1;
18103 if (PT == CHARPOS (row->end.pos))
18105 /* Suppose the row ends on a string.
18106 Unless the row is continued, that means it ends on a newline
18107 in the string. If it's anything other than a display string
18108 (e.g. a before-string from an overlay), we don't want the
18109 cursor there. (This heuristic seems to give the optimal
18110 behavior for the various types of multi-line strings.) */
18111 if (CHARPOS (row->end.string_pos) >= 0)
18113 if (row->continued_p)
18114 result = 1;
18115 else
18117 /* Check for `display' property. */
18118 struct glyph *beg = row->glyphs[TEXT_AREA];
18119 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18120 struct glyph *glyph;
18122 result = 0;
18123 for (glyph = end; glyph >= beg; --glyph)
18124 if (STRINGP (glyph->object))
18126 Lisp_Object prop
18127 = Fget_char_property (make_number (PT),
18128 Qdisplay, Qnil);
18129 result =
18130 (!NILP (prop)
18131 && display_prop_string_p (prop, glyph->object));
18132 break;
18136 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18138 /* If the row ends in middle of a real character,
18139 and the line is continued, we want the cursor here.
18140 That's because CHARPOS (ROW->end.pos) would equal
18141 PT if PT is before the character. */
18142 if (!row->ends_in_ellipsis_p)
18143 result = row->continued_p;
18144 else
18145 /* If the row ends in an ellipsis, then
18146 CHARPOS (ROW->end.pos) will equal point after the
18147 invisible text. We want that position to be displayed
18148 after the ellipsis. */
18149 result = 0;
18151 /* If the row ends at ZV, display the cursor at the end of that
18152 row instead of at the start of the row below. */
18153 else if (row->ends_at_zv_p)
18154 result = 1;
18155 else
18156 result = 0;
18159 return result;
18164 /* Push the display property PROP so that it will be rendered at the
18165 current position in IT. Return 1 if PROP was successfully pushed,
18166 0 otherwise. */
18168 static int
18169 push_display_prop (struct it *it, Lisp_Object prop)
18171 xassert (it->method == GET_FROM_BUFFER);
18173 push_it (it, NULL);
18175 if (STRINGP (prop))
18177 if (SCHARS (prop) == 0)
18179 pop_it (it);
18180 return 0;
18183 it->string = prop;
18184 it->multibyte_p = STRING_MULTIBYTE (it->string);
18185 it->current.overlay_string_index = -1;
18186 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18187 it->end_charpos = it->string_nchars = SCHARS (it->string);
18188 it->method = GET_FROM_STRING;
18189 it->stop_charpos = 0;
18190 it->prev_stop = 0;
18191 it->base_level_stop = 0;
18192 it->string_from_display_prop_p = 1;
18193 it->from_disp_prop_p = 1;
18195 /* Force paragraph direction to be that of the parent
18196 buffer. */
18197 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18198 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18199 else
18200 it->paragraph_embedding = L2R;
18202 /* Set up the bidi iterator for this display string. */
18203 if (it->bidi_p)
18205 it->bidi_it.string.lstring = it->string;
18206 it->bidi_it.string.s = NULL;
18207 it->bidi_it.string.schars = it->end_charpos;
18208 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18209 it->bidi_it.string.from_disp_str = 1;
18210 it->bidi_it.string.unibyte = !it->multibyte_p;
18211 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18214 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18216 it->method = GET_FROM_STRETCH;
18217 it->object = prop;
18219 #ifdef HAVE_WINDOW_SYSTEM
18220 else if (IMAGEP (prop))
18222 it->what = IT_IMAGE;
18223 it->image_id = lookup_image (it->f, prop);
18224 it->method = GET_FROM_IMAGE;
18226 #endif /* HAVE_WINDOW_SYSTEM */
18227 else
18229 pop_it (it); /* bogus display property, give up */
18230 return 0;
18233 return 1;
18236 /* Return the character-property PROP at the current position in IT. */
18238 static Lisp_Object
18239 get_it_property (struct it *it, Lisp_Object prop)
18241 Lisp_Object position;
18243 if (STRINGP (it->object))
18244 position = make_number (IT_STRING_CHARPOS (*it));
18245 else if (BUFFERP (it->object))
18246 position = make_number (IT_CHARPOS (*it));
18247 else
18248 return Qnil;
18250 return Fget_char_property (position, prop, it->object);
18253 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18255 static void
18256 handle_line_prefix (struct it *it)
18258 Lisp_Object prefix;
18260 if (it->continuation_lines_width > 0)
18262 prefix = get_it_property (it, Qwrap_prefix);
18263 if (NILP (prefix))
18264 prefix = Vwrap_prefix;
18266 else
18268 prefix = get_it_property (it, Qline_prefix);
18269 if (NILP (prefix))
18270 prefix = Vline_prefix;
18272 if (! NILP (prefix) && push_display_prop (it, prefix))
18274 /* If the prefix is wider than the window, and we try to wrap
18275 it, it would acquire its own wrap prefix, and so on till the
18276 iterator stack overflows. So, don't wrap the prefix. */
18277 it->line_wrap = TRUNCATE;
18278 it->avoid_cursor_p = 1;
18284 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18285 only for R2L lines from display_line and display_string, when they
18286 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18287 the line/string needs to be continued on the next glyph row. */
18288 static void
18289 unproduce_glyphs (struct it *it, int n)
18291 struct glyph *glyph, *end;
18293 xassert (it->glyph_row);
18294 xassert (it->glyph_row->reversed_p);
18295 xassert (it->area == TEXT_AREA);
18296 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18298 if (n > it->glyph_row->used[TEXT_AREA])
18299 n = it->glyph_row->used[TEXT_AREA];
18300 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18301 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18302 for ( ; glyph < end; glyph++)
18303 glyph[-n] = *glyph;
18306 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18307 and ROW->maxpos. */
18308 static void
18309 find_row_edges (struct it *it, struct glyph_row *row,
18310 EMACS_INT min_pos, EMACS_INT min_bpos,
18311 EMACS_INT max_pos, EMACS_INT max_bpos)
18313 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18314 lines' rows is implemented for bidi-reordered rows. */
18316 /* ROW->minpos is the value of min_pos, the minimal buffer position
18317 we have in ROW, or ROW->start.pos if that is smaller. */
18318 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18319 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18320 else
18321 /* We didn't find buffer positions smaller than ROW->start, or
18322 didn't find _any_ valid buffer positions in any of the glyphs,
18323 so we must trust the iterator's computed positions. */
18324 row->minpos = row->start.pos;
18325 if (max_pos <= 0)
18327 max_pos = CHARPOS (it->current.pos);
18328 max_bpos = BYTEPOS (it->current.pos);
18331 /* Here are the various use-cases for ending the row, and the
18332 corresponding values for ROW->maxpos:
18334 Line ends in a newline from buffer eol_pos + 1
18335 Line is continued from buffer max_pos + 1
18336 Line is truncated on right it->current.pos
18337 Line ends in a newline from string max_pos
18338 Line is continued from string max_pos
18339 Line is continued from display vector max_pos
18340 Line is entirely from a string min_pos == max_pos
18341 Line is entirely from a display vector min_pos == max_pos
18342 Line that ends at ZV ZV
18344 If you discover other use-cases, please add them here as
18345 appropriate. */
18346 if (row->ends_at_zv_p)
18347 row->maxpos = it->current.pos;
18348 else if (row->used[TEXT_AREA])
18350 if (row->ends_in_newline_from_string_p)
18351 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18352 else if (CHARPOS (it->eol_pos) > 0)
18353 SET_TEXT_POS (row->maxpos,
18354 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18355 else if (row->continued_p)
18357 /* If max_pos is different from IT's current position, it
18358 means IT->method does not belong to the display element
18359 at max_pos. However, it also means that the display
18360 element at max_pos was displayed in its entirety on this
18361 line, which is equivalent to saying that the next line
18362 starts at the next buffer position. */
18363 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18364 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18365 else
18367 INC_BOTH (max_pos, max_bpos);
18368 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18371 else if (row->truncated_on_right_p)
18372 /* display_line already called reseat_at_next_visible_line_start,
18373 which puts the iterator at the beginning of the next line, in
18374 the logical order. */
18375 row->maxpos = it->current.pos;
18376 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18377 /* A line that is entirely from a string/image/stretch... */
18378 row->maxpos = row->minpos;
18379 else
18380 abort ();
18382 else
18383 row->maxpos = it->current.pos;
18386 /* Construct the glyph row IT->glyph_row in the desired matrix of
18387 IT->w from text at the current position of IT. See dispextern.h
18388 for an overview of struct it. Value is non-zero if
18389 IT->glyph_row displays text, as opposed to a line displaying ZV
18390 only. */
18392 static int
18393 display_line (struct it *it)
18395 struct glyph_row *row = it->glyph_row;
18396 Lisp_Object overlay_arrow_string;
18397 struct it wrap_it;
18398 void *wrap_data = NULL;
18399 int may_wrap = 0, wrap_x IF_LINT (= 0);
18400 int wrap_row_used = -1;
18401 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18402 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18403 int wrap_row_extra_line_spacing IF_LINT (= 0);
18404 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18405 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18406 int cvpos;
18407 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18408 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18410 /* We always start displaying at hpos zero even if hscrolled. */
18411 xassert (it->hpos == 0 && it->current_x == 0);
18413 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18414 >= it->w->desired_matrix->nrows)
18416 it->w->nrows_scale_factor++;
18417 fonts_changed_p = 1;
18418 return 0;
18421 /* Is IT->w showing the region? */
18422 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18424 /* Clear the result glyph row and enable it. */
18425 prepare_desired_row (row);
18427 row->y = it->current_y;
18428 row->start = it->start;
18429 row->continuation_lines_width = it->continuation_lines_width;
18430 row->displays_text_p = 1;
18431 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18432 it->starts_in_middle_of_char_p = 0;
18434 /* Arrange the overlays nicely for our purposes. Usually, we call
18435 display_line on only one line at a time, in which case this
18436 can't really hurt too much, or we call it on lines which appear
18437 one after another in the buffer, in which case all calls to
18438 recenter_overlay_lists but the first will be pretty cheap. */
18439 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18441 /* Move over display elements that are not visible because we are
18442 hscrolled. This may stop at an x-position < IT->first_visible_x
18443 if the first glyph is partially visible or if we hit a line end. */
18444 if (it->current_x < it->first_visible_x)
18446 this_line_min_pos = row->start.pos;
18447 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18448 MOVE_TO_POS | MOVE_TO_X);
18449 /* Record the smallest positions seen while we moved over
18450 display elements that are not visible. This is needed by
18451 redisplay_internal for optimizing the case where the cursor
18452 stays inside the same line. The rest of this function only
18453 considers positions that are actually displayed, so
18454 RECORD_MAX_MIN_POS will not otherwise record positions that
18455 are hscrolled to the left of the left edge of the window. */
18456 min_pos = CHARPOS (this_line_min_pos);
18457 min_bpos = BYTEPOS (this_line_min_pos);
18459 else
18461 /* We only do this when not calling `move_it_in_display_line_to'
18462 above, because move_it_in_display_line_to calls
18463 handle_line_prefix itself. */
18464 handle_line_prefix (it);
18467 /* Get the initial row height. This is either the height of the
18468 text hscrolled, if there is any, or zero. */
18469 row->ascent = it->max_ascent;
18470 row->height = it->max_ascent + it->max_descent;
18471 row->phys_ascent = it->max_phys_ascent;
18472 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18473 row->extra_line_spacing = it->max_extra_line_spacing;
18475 /* Utility macro to record max and min buffer positions seen until now. */
18476 #define RECORD_MAX_MIN_POS(IT) \
18477 do \
18479 if (IT_CHARPOS (*(IT)) < min_pos) \
18481 min_pos = IT_CHARPOS (*(IT)); \
18482 min_bpos = IT_BYTEPOS (*(IT)); \
18484 if (IT_CHARPOS (*(IT)) > max_pos) \
18486 max_pos = IT_CHARPOS (*(IT)); \
18487 max_bpos = IT_BYTEPOS (*(IT)); \
18490 while (0)
18492 /* Loop generating characters. The loop is left with IT on the next
18493 character to display. */
18494 while (1)
18496 int n_glyphs_before, hpos_before, x_before;
18497 int x, nglyphs;
18498 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18500 /* Retrieve the next thing to display. Value is zero if end of
18501 buffer reached. */
18502 if (!get_next_display_element (it))
18504 /* Maybe add a space at the end of this line that is used to
18505 display the cursor there under X. Set the charpos of the
18506 first glyph of blank lines not corresponding to any text
18507 to -1. */
18508 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18509 row->exact_window_width_line_p = 1;
18510 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18511 || row->used[TEXT_AREA] == 0)
18513 row->glyphs[TEXT_AREA]->charpos = -1;
18514 row->displays_text_p = 0;
18516 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18517 && (!MINI_WINDOW_P (it->w)
18518 || (minibuf_level && EQ (it->window, minibuf_window))))
18519 row->indicate_empty_line_p = 1;
18522 it->continuation_lines_width = 0;
18523 row->ends_at_zv_p = 1;
18524 /* A row that displays right-to-left text must always have
18525 its last face extended all the way to the end of line,
18526 even if this row ends in ZV, because we still write to
18527 the screen left to right. */
18528 if (row->reversed_p)
18529 extend_face_to_end_of_line (it);
18530 break;
18533 /* Now, get the metrics of what we want to display. This also
18534 generates glyphs in `row' (which is IT->glyph_row). */
18535 n_glyphs_before = row->used[TEXT_AREA];
18536 x = it->current_x;
18538 /* Remember the line height so far in case the next element doesn't
18539 fit on the line. */
18540 if (it->line_wrap != TRUNCATE)
18542 ascent = it->max_ascent;
18543 descent = it->max_descent;
18544 phys_ascent = it->max_phys_ascent;
18545 phys_descent = it->max_phys_descent;
18547 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18549 if (IT_DISPLAYING_WHITESPACE (it))
18550 may_wrap = 1;
18551 else if (may_wrap)
18553 SAVE_IT (wrap_it, *it, wrap_data);
18554 wrap_x = x;
18555 wrap_row_used = row->used[TEXT_AREA];
18556 wrap_row_ascent = row->ascent;
18557 wrap_row_height = row->height;
18558 wrap_row_phys_ascent = row->phys_ascent;
18559 wrap_row_phys_height = row->phys_height;
18560 wrap_row_extra_line_spacing = row->extra_line_spacing;
18561 wrap_row_min_pos = min_pos;
18562 wrap_row_min_bpos = min_bpos;
18563 wrap_row_max_pos = max_pos;
18564 wrap_row_max_bpos = max_bpos;
18565 may_wrap = 0;
18570 PRODUCE_GLYPHS (it);
18572 /* If this display element was in marginal areas, continue with
18573 the next one. */
18574 if (it->area != TEXT_AREA)
18576 row->ascent = max (row->ascent, it->max_ascent);
18577 row->height = max (row->height, it->max_ascent + it->max_descent);
18578 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18579 row->phys_height = max (row->phys_height,
18580 it->max_phys_ascent + it->max_phys_descent);
18581 row->extra_line_spacing = max (row->extra_line_spacing,
18582 it->max_extra_line_spacing);
18583 set_iterator_to_next (it, 1);
18584 continue;
18587 /* Does the display element fit on the line? If we truncate
18588 lines, we should draw past the right edge of the window. If
18589 we don't truncate, we want to stop so that we can display the
18590 continuation glyph before the right margin. If lines are
18591 continued, there are two possible strategies for characters
18592 resulting in more than 1 glyph (e.g. tabs): Display as many
18593 glyphs as possible in this line and leave the rest for the
18594 continuation line, or display the whole element in the next
18595 line. Original redisplay did the former, so we do it also. */
18596 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18597 hpos_before = it->hpos;
18598 x_before = x;
18600 if (/* Not a newline. */
18601 nglyphs > 0
18602 /* Glyphs produced fit entirely in the line. */
18603 && it->current_x < it->last_visible_x)
18605 it->hpos += nglyphs;
18606 row->ascent = max (row->ascent, it->max_ascent);
18607 row->height = max (row->height, it->max_ascent + it->max_descent);
18608 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18609 row->phys_height = max (row->phys_height,
18610 it->max_phys_ascent + it->max_phys_descent);
18611 row->extra_line_spacing = max (row->extra_line_spacing,
18612 it->max_extra_line_spacing);
18613 if (it->current_x - it->pixel_width < it->first_visible_x)
18614 row->x = x - it->first_visible_x;
18615 /* Record the maximum and minimum buffer positions seen so
18616 far in glyphs that will be displayed by this row. */
18617 if (it->bidi_p)
18618 RECORD_MAX_MIN_POS (it);
18620 else
18622 int i, new_x;
18623 struct glyph *glyph;
18625 for (i = 0; i < nglyphs; ++i, x = new_x)
18627 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18628 new_x = x + glyph->pixel_width;
18630 if (/* Lines are continued. */
18631 it->line_wrap != TRUNCATE
18632 && (/* Glyph doesn't fit on the line. */
18633 new_x > it->last_visible_x
18634 /* Or it fits exactly on a window system frame. */
18635 || (new_x == it->last_visible_x
18636 && FRAME_WINDOW_P (it->f))))
18638 /* End of a continued line. */
18640 if (it->hpos == 0
18641 || (new_x == it->last_visible_x
18642 && FRAME_WINDOW_P (it->f)))
18644 /* Current glyph is the only one on the line or
18645 fits exactly on the line. We must continue
18646 the line because we can't draw the cursor
18647 after the glyph. */
18648 row->continued_p = 1;
18649 it->current_x = new_x;
18650 it->continuation_lines_width += new_x;
18651 ++it->hpos;
18652 /* Record the maximum and minimum buffer
18653 positions seen so far in glyphs that will be
18654 displayed by this row. */
18655 if (it->bidi_p)
18656 RECORD_MAX_MIN_POS (it);
18657 if (i == nglyphs - 1)
18659 /* If line-wrap is on, check if a previous
18660 wrap point was found. */
18661 if (wrap_row_used > 0
18662 /* Even if there is a previous wrap
18663 point, continue the line here as
18664 usual, if (i) the previous character
18665 was a space or tab AND (ii) the
18666 current character is not. */
18667 && (!may_wrap
18668 || IT_DISPLAYING_WHITESPACE (it)))
18669 goto back_to_wrap;
18671 set_iterator_to_next (it, 1);
18672 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18674 if (!get_next_display_element (it))
18676 row->exact_window_width_line_p = 1;
18677 it->continuation_lines_width = 0;
18678 row->continued_p = 0;
18679 row->ends_at_zv_p = 1;
18681 else if (ITERATOR_AT_END_OF_LINE_P (it))
18683 row->continued_p = 0;
18684 row->exact_window_width_line_p = 1;
18689 else if (CHAR_GLYPH_PADDING_P (*glyph)
18690 && !FRAME_WINDOW_P (it->f))
18692 /* A padding glyph that doesn't fit on this line.
18693 This means the whole character doesn't fit
18694 on the line. */
18695 if (row->reversed_p)
18696 unproduce_glyphs (it, row->used[TEXT_AREA]
18697 - n_glyphs_before);
18698 row->used[TEXT_AREA] = n_glyphs_before;
18700 /* Fill the rest of the row with continuation
18701 glyphs like in 20.x. */
18702 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18703 < row->glyphs[1 + TEXT_AREA])
18704 produce_special_glyphs (it, IT_CONTINUATION);
18706 row->continued_p = 1;
18707 it->current_x = x_before;
18708 it->continuation_lines_width += x_before;
18710 /* Restore the height to what it was before the
18711 element not fitting on the line. */
18712 it->max_ascent = ascent;
18713 it->max_descent = descent;
18714 it->max_phys_ascent = phys_ascent;
18715 it->max_phys_descent = phys_descent;
18717 else if (wrap_row_used > 0)
18719 back_to_wrap:
18720 if (row->reversed_p)
18721 unproduce_glyphs (it,
18722 row->used[TEXT_AREA] - wrap_row_used);
18723 RESTORE_IT (it, &wrap_it, wrap_data);
18724 it->continuation_lines_width += wrap_x;
18725 row->used[TEXT_AREA] = wrap_row_used;
18726 row->ascent = wrap_row_ascent;
18727 row->height = wrap_row_height;
18728 row->phys_ascent = wrap_row_phys_ascent;
18729 row->phys_height = wrap_row_phys_height;
18730 row->extra_line_spacing = wrap_row_extra_line_spacing;
18731 min_pos = wrap_row_min_pos;
18732 min_bpos = wrap_row_min_bpos;
18733 max_pos = wrap_row_max_pos;
18734 max_bpos = wrap_row_max_bpos;
18735 row->continued_p = 1;
18736 row->ends_at_zv_p = 0;
18737 row->exact_window_width_line_p = 0;
18738 it->continuation_lines_width += x;
18740 /* Make sure that a non-default face is extended
18741 up to the right margin of the window. */
18742 extend_face_to_end_of_line (it);
18744 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18746 /* A TAB that extends past the right edge of the
18747 window. This produces a single glyph on
18748 window system frames. We leave the glyph in
18749 this row and let it fill the row, but don't
18750 consume the TAB. */
18751 it->continuation_lines_width += it->last_visible_x;
18752 row->ends_in_middle_of_char_p = 1;
18753 row->continued_p = 1;
18754 glyph->pixel_width = it->last_visible_x - x;
18755 it->starts_in_middle_of_char_p = 1;
18757 else
18759 /* Something other than a TAB that draws past
18760 the right edge of the window. Restore
18761 positions to values before the element. */
18762 if (row->reversed_p)
18763 unproduce_glyphs (it, row->used[TEXT_AREA]
18764 - (n_glyphs_before + i));
18765 row->used[TEXT_AREA] = n_glyphs_before + i;
18767 /* Display continuation glyphs. */
18768 if (!FRAME_WINDOW_P (it->f))
18769 produce_special_glyphs (it, IT_CONTINUATION);
18770 row->continued_p = 1;
18772 it->current_x = x_before;
18773 it->continuation_lines_width += x;
18774 extend_face_to_end_of_line (it);
18776 if (nglyphs > 1 && i > 0)
18778 row->ends_in_middle_of_char_p = 1;
18779 it->starts_in_middle_of_char_p = 1;
18782 /* Restore the height to what it was before the
18783 element not fitting on the line. */
18784 it->max_ascent = ascent;
18785 it->max_descent = descent;
18786 it->max_phys_ascent = phys_ascent;
18787 it->max_phys_descent = phys_descent;
18790 break;
18792 else if (new_x > it->first_visible_x)
18794 /* Increment number of glyphs actually displayed. */
18795 ++it->hpos;
18797 /* Record the maximum and minimum buffer positions
18798 seen so far in glyphs that will be displayed by
18799 this row. */
18800 if (it->bidi_p)
18801 RECORD_MAX_MIN_POS (it);
18803 if (x < it->first_visible_x)
18804 /* Glyph is partially visible, i.e. row starts at
18805 negative X position. */
18806 row->x = x - it->first_visible_x;
18808 else
18810 /* Glyph is completely off the left margin of the
18811 window. This should not happen because of the
18812 move_it_in_display_line at the start of this
18813 function, unless the text display area of the
18814 window is empty. */
18815 xassert (it->first_visible_x <= it->last_visible_x);
18819 row->ascent = max (row->ascent, it->max_ascent);
18820 row->height = max (row->height, it->max_ascent + it->max_descent);
18821 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18822 row->phys_height = max (row->phys_height,
18823 it->max_phys_ascent + it->max_phys_descent);
18824 row->extra_line_spacing = max (row->extra_line_spacing,
18825 it->max_extra_line_spacing);
18827 /* End of this display line if row is continued. */
18828 if (row->continued_p || row->ends_at_zv_p)
18829 break;
18832 at_end_of_line:
18833 /* Is this a line end? If yes, we're also done, after making
18834 sure that a non-default face is extended up to the right
18835 margin of the window. */
18836 if (ITERATOR_AT_END_OF_LINE_P (it))
18838 int used_before = row->used[TEXT_AREA];
18840 row->ends_in_newline_from_string_p = STRINGP (it->object);
18842 /* Add a space at the end of the line that is used to
18843 display the cursor there. */
18844 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18845 append_space_for_newline (it, 0);
18847 /* Extend the face to the end of the line. */
18848 extend_face_to_end_of_line (it);
18850 /* Make sure we have the position. */
18851 if (used_before == 0)
18852 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18854 /* Record the position of the newline, for use in
18855 find_row_edges. */
18856 it->eol_pos = it->current.pos;
18858 /* Consume the line end. This skips over invisible lines. */
18859 set_iterator_to_next (it, 1);
18860 it->continuation_lines_width = 0;
18861 break;
18864 /* Proceed with next display element. Note that this skips
18865 over lines invisible because of selective display. */
18866 set_iterator_to_next (it, 1);
18868 /* If we truncate lines, we are done when the last displayed
18869 glyphs reach past the right margin of the window. */
18870 if (it->line_wrap == TRUNCATE
18871 && (FRAME_WINDOW_P (it->f)
18872 ? (it->current_x >= it->last_visible_x)
18873 : (it->current_x > it->last_visible_x)))
18875 /* Maybe add truncation glyphs. */
18876 if (!FRAME_WINDOW_P (it->f))
18878 int i, n;
18880 if (!row->reversed_p)
18882 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18883 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18884 break;
18886 else
18888 for (i = 0; i < row->used[TEXT_AREA]; i++)
18889 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18890 break;
18891 /* Remove any padding glyphs at the front of ROW, to
18892 make room for the truncation glyphs we will be
18893 adding below. The loop below always inserts at
18894 least one truncation glyph, so also remove the
18895 last glyph added to ROW. */
18896 unproduce_glyphs (it, i + 1);
18897 /* Adjust i for the loop below. */
18898 i = row->used[TEXT_AREA] - (i + 1);
18901 for (n = row->used[TEXT_AREA]; i < n; ++i)
18903 row->used[TEXT_AREA] = i;
18904 produce_special_glyphs (it, IT_TRUNCATION);
18907 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18909 /* Don't truncate if we can overflow newline into fringe. */
18910 if (!get_next_display_element (it))
18912 it->continuation_lines_width = 0;
18913 row->ends_at_zv_p = 1;
18914 row->exact_window_width_line_p = 1;
18915 break;
18917 if (ITERATOR_AT_END_OF_LINE_P (it))
18919 row->exact_window_width_line_p = 1;
18920 goto at_end_of_line;
18924 row->truncated_on_right_p = 1;
18925 it->continuation_lines_width = 0;
18926 reseat_at_next_visible_line_start (it, 0);
18927 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18928 it->hpos = hpos_before;
18929 it->current_x = x_before;
18930 break;
18934 if (wrap_data)
18935 bidi_unshelve_cache (wrap_data, 1);
18937 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18938 at the left window margin. */
18939 if (it->first_visible_x
18940 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18942 if (!FRAME_WINDOW_P (it->f))
18943 insert_left_trunc_glyphs (it);
18944 row->truncated_on_left_p = 1;
18947 /* Remember the position at which this line ends.
18949 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18950 cannot be before the call to find_row_edges below, since that is
18951 where these positions are determined. */
18952 row->end = it->current;
18953 if (!it->bidi_p)
18955 row->minpos = row->start.pos;
18956 row->maxpos = row->end.pos;
18958 else
18960 /* ROW->minpos and ROW->maxpos must be the smallest and
18961 `1 + the largest' buffer positions in ROW. But if ROW was
18962 bidi-reordered, these two positions can be anywhere in the
18963 row, so we must determine them now. */
18964 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18967 /* If the start of this line is the overlay arrow-position, then
18968 mark this glyph row as the one containing the overlay arrow.
18969 This is clearly a mess with variable size fonts. It would be
18970 better to let it be displayed like cursors under X. */
18971 if ((row->displays_text_p || !overlay_arrow_seen)
18972 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18973 !NILP (overlay_arrow_string)))
18975 /* Overlay arrow in window redisplay is a fringe bitmap. */
18976 if (STRINGP (overlay_arrow_string))
18978 struct glyph_row *arrow_row
18979 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18980 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18981 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18982 struct glyph *p = row->glyphs[TEXT_AREA];
18983 struct glyph *p2, *end;
18985 /* Copy the arrow glyphs. */
18986 while (glyph < arrow_end)
18987 *p++ = *glyph++;
18989 /* Throw away padding glyphs. */
18990 p2 = p;
18991 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18992 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18993 ++p2;
18994 if (p2 > p)
18996 while (p2 < end)
18997 *p++ = *p2++;
18998 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19001 else
19003 xassert (INTEGERP (overlay_arrow_string));
19004 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19006 overlay_arrow_seen = 1;
19009 /* Compute pixel dimensions of this line. */
19010 compute_line_metrics (it);
19012 /* Record whether this row ends inside an ellipsis. */
19013 row->ends_in_ellipsis_p
19014 = (it->method == GET_FROM_DISPLAY_VECTOR
19015 && it->ellipsis_p);
19017 /* Save fringe bitmaps in this row. */
19018 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19019 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19020 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19021 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19023 it->left_user_fringe_bitmap = 0;
19024 it->left_user_fringe_face_id = 0;
19025 it->right_user_fringe_bitmap = 0;
19026 it->right_user_fringe_face_id = 0;
19028 /* Maybe set the cursor. */
19029 cvpos = it->w->cursor.vpos;
19030 if ((cvpos < 0
19031 /* In bidi-reordered rows, keep checking for proper cursor
19032 position even if one has been found already, because buffer
19033 positions in such rows change non-linearly with ROW->VPOS,
19034 when a line is continued. One exception: when we are at ZV,
19035 display cursor on the first suitable glyph row, since all
19036 the empty rows after that also have their position set to ZV. */
19037 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19038 lines' rows is implemented for bidi-reordered rows. */
19039 || (it->bidi_p
19040 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19041 && PT >= MATRIX_ROW_START_CHARPOS (row)
19042 && PT <= MATRIX_ROW_END_CHARPOS (row)
19043 && cursor_row_p (row))
19044 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19046 /* Highlight trailing whitespace. */
19047 if (!NILP (Vshow_trailing_whitespace))
19048 highlight_trailing_whitespace (it->f, it->glyph_row);
19050 /* Prepare for the next line. This line starts horizontally at (X
19051 HPOS) = (0 0). Vertical positions are incremented. As a
19052 convenience for the caller, IT->glyph_row is set to the next
19053 row to be used. */
19054 it->current_x = it->hpos = 0;
19055 it->current_y += row->height;
19056 SET_TEXT_POS (it->eol_pos, 0, 0);
19057 ++it->vpos;
19058 ++it->glyph_row;
19059 /* The next row should by default use the same value of the
19060 reversed_p flag as this one. set_iterator_to_next decides when
19061 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19062 the flag accordingly. */
19063 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19064 it->glyph_row->reversed_p = row->reversed_p;
19065 it->start = row->end;
19066 return row->displays_text_p;
19068 #undef RECORD_MAX_MIN_POS
19071 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19072 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19073 doc: /* Return paragraph direction at point in BUFFER.
19074 Value is either `left-to-right' or `right-to-left'.
19075 If BUFFER is omitted or nil, it defaults to the current buffer.
19077 Paragraph direction determines how the text in the paragraph is displayed.
19078 In left-to-right paragraphs, text begins at the left margin of the window
19079 and the reading direction is generally left to right. In right-to-left
19080 paragraphs, text begins at the right margin and is read from right to left.
19082 See also `bidi-paragraph-direction'. */)
19083 (Lisp_Object buffer)
19085 struct buffer *buf = current_buffer;
19086 struct buffer *old = buf;
19088 if (! NILP (buffer))
19090 CHECK_BUFFER (buffer);
19091 buf = XBUFFER (buffer);
19094 if (NILP (BVAR (buf, bidi_display_reordering)))
19095 return Qleft_to_right;
19096 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19097 return BVAR (buf, bidi_paragraph_direction);
19098 else
19100 /* Determine the direction from buffer text. We could try to
19101 use current_matrix if it is up to date, but this seems fast
19102 enough as it is. */
19103 struct bidi_it itb;
19104 EMACS_INT pos = BUF_PT (buf);
19105 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19106 int c;
19108 set_buffer_temp (buf);
19109 /* bidi_paragraph_init finds the base direction of the paragraph
19110 by searching forward from paragraph start. We need the base
19111 direction of the current or _previous_ paragraph, so we need
19112 to make sure we are within that paragraph. To that end, find
19113 the previous non-empty line. */
19114 if (pos >= ZV && pos > BEGV)
19116 pos--;
19117 bytepos = CHAR_TO_BYTE (pos);
19119 while ((c = FETCH_BYTE (bytepos)) == '\n'
19120 || c == ' ' || c == '\t' || c == '\f')
19122 if (bytepos <= BEGV_BYTE)
19123 break;
19124 bytepos--;
19125 pos--;
19127 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19128 bytepos--;
19129 itb.charpos = pos;
19130 itb.bytepos = bytepos;
19131 itb.nchars = -1;
19132 itb.string.s = NULL;
19133 itb.string.lstring = Qnil;
19134 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
19135 itb.first_elt = 1;
19136 itb.separator_limit = -1;
19137 itb.paragraph_dir = NEUTRAL_DIR;
19139 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19140 set_buffer_temp (old);
19141 switch (itb.paragraph_dir)
19143 case L2R:
19144 return Qleft_to_right;
19145 break;
19146 case R2L:
19147 return Qright_to_left;
19148 break;
19149 default:
19150 abort ();
19157 /***********************************************************************
19158 Menu Bar
19159 ***********************************************************************/
19161 /* Redisplay the menu bar in the frame for window W.
19163 The menu bar of X frames that don't have X toolkit support is
19164 displayed in a special window W->frame->menu_bar_window.
19166 The menu bar of terminal frames is treated specially as far as
19167 glyph matrices are concerned. Menu bar lines are not part of
19168 windows, so the update is done directly on the frame matrix rows
19169 for the menu bar. */
19171 static void
19172 display_menu_bar (struct window *w)
19174 struct frame *f = XFRAME (WINDOW_FRAME (w));
19175 struct it it;
19176 Lisp_Object items;
19177 int i;
19179 /* Don't do all this for graphical frames. */
19180 #ifdef HAVE_NTGUI
19181 if (FRAME_W32_P (f))
19182 return;
19183 #endif
19184 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19185 if (FRAME_X_P (f))
19186 return;
19187 #endif
19189 #ifdef HAVE_NS
19190 if (FRAME_NS_P (f))
19191 return;
19192 #endif /* HAVE_NS */
19194 #ifdef USE_X_TOOLKIT
19195 xassert (!FRAME_WINDOW_P (f));
19196 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19197 it.first_visible_x = 0;
19198 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19199 #else /* not USE_X_TOOLKIT */
19200 if (FRAME_WINDOW_P (f))
19202 /* Menu bar lines are displayed in the desired matrix of the
19203 dummy window menu_bar_window. */
19204 struct window *menu_w;
19205 xassert (WINDOWP (f->menu_bar_window));
19206 menu_w = XWINDOW (f->menu_bar_window);
19207 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19208 MENU_FACE_ID);
19209 it.first_visible_x = 0;
19210 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19212 else
19214 /* This is a TTY frame, i.e. character hpos/vpos are used as
19215 pixel x/y. */
19216 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19217 MENU_FACE_ID);
19218 it.first_visible_x = 0;
19219 it.last_visible_x = FRAME_COLS (f);
19221 #endif /* not USE_X_TOOLKIT */
19223 /* FIXME: This should be controlled by a user option. See the
19224 comments in redisplay_tool_bar and display_mode_line about
19225 this. */
19226 it.paragraph_embedding = L2R;
19228 if (! mode_line_inverse_video)
19229 /* Force the menu-bar to be displayed in the default face. */
19230 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19232 /* Clear all rows of the menu bar. */
19233 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19235 struct glyph_row *row = it.glyph_row + i;
19236 clear_glyph_row (row);
19237 row->enabled_p = 1;
19238 row->full_width_p = 1;
19241 /* Display all items of the menu bar. */
19242 items = FRAME_MENU_BAR_ITEMS (it.f);
19243 for (i = 0; i < ASIZE (items); i += 4)
19245 Lisp_Object string;
19247 /* Stop at nil string. */
19248 string = AREF (items, i + 1);
19249 if (NILP (string))
19250 break;
19252 /* Remember where item was displayed. */
19253 ASET (items, i + 3, make_number (it.hpos));
19255 /* Display the item, pad with one space. */
19256 if (it.current_x < it.last_visible_x)
19257 display_string (NULL, string, Qnil, 0, 0, &it,
19258 SCHARS (string) + 1, 0, 0, -1);
19261 /* Fill out the line with spaces. */
19262 if (it.current_x < it.last_visible_x)
19263 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19265 /* Compute the total height of the lines. */
19266 compute_line_metrics (&it);
19271 /***********************************************************************
19272 Mode Line
19273 ***********************************************************************/
19275 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19276 FORCE is non-zero, redisplay mode lines unconditionally.
19277 Otherwise, redisplay only mode lines that are garbaged. Value is
19278 the number of windows whose mode lines were redisplayed. */
19280 static int
19281 redisplay_mode_lines (Lisp_Object window, int force)
19283 int nwindows = 0;
19285 while (!NILP (window))
19287 struct window *w = XWINDOW (window);
19289 if (WINDOWP (w->hchild))
19290 nwindows += redisplay_mode_lines (w->hchild, force);
19291 else if (WINDOWP (w->vchild))
19292 nwindows += redisplay_mode_lines (w->vchild, force);
19293 else if (force
19294 || FRAME_GARBAGED_P (XFRAME (w->frame))
19295 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19297 struct text_pos lpoint;
19298 struct buffer *old = current_buffer;
19300 /* Set the window's buffer for the mode line display. */
19301 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19302 set_buffer_internal_1 (XBUFFER (w->buffer));
19304 /* Point refers normally to the selected window. For any
19305 other window, set up appropriate value. */
19306 if (!EQ (window, selected_window))
19308 struct text_pos pt;
19310 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19311 if (CHARPOS (pt) < BEGV)
19312 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19313 else if (CHARPOS (pt) > (ZV - 1))
19314 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19315 else
19316 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19319 /* Display mode lines. */
19320 clear_glyph_matrix (w->desired_matrix);
19321 if (display_mode_lines (w))
19323 ++nwindows;
19324 w->must_be_updated_p = 1;
19327 /* Restore old settings. */
19328 set_buffer_internal_1 (old);
19329 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19332 window = w->next;
19335 return nwindows;
19339 /* Display the mode and/or header line of window W. Value is the
19340 sum number of mode lines and header lines displayed. */
19342 static int
19343 display_mode_lines (struct window *w)
19345 Lisp_Object old_selected_window, old_selected_frame;
19346 int n = 0;
19348 old_selected_frame = selected_frame;
19349 selected_frame = w->frame;
19350 old_selected_window = selected_window;
19351 XSETWINDOW (selected_window, w);
19353 /* These will be set while the mode line specs are processed. */
19354 line_number_displayed = 0;
19355 w->column_number_displayed = Qnil;
19357 if (WINDOW_WANTS_MODELINE_P (w))
19359 struct window *sel_w = XWINDOW (old_selected_window);
19361 /* Select mode line face based on the real selected window. */
19362 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19363 BVAR (current_buffer, mode_line_format));
19364 ++n;
19367 if (WINDOW_WANTS_HEADER_LINE_P (w))
19369 display_mode_line (w, HEADER_LINE_FACE_ID,
19370 BVAR (current_buffer, header_line_format));
19371 ++n;
19374 selected_frame = old_selected_frame;
19375 selected_window = old_selected_window;
19376 return n;
19380 /* Display mode or header line of window W. FACE_ID specifies which
19381 line to display; it is either MODE_LINE_FACE_ID or
19382 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19383 display. Value is the pixel height of the mode/header line
19384 displayed. */
19386 static int
19387 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19389 struct it it;
19390 struct face *face;
19391 int count = SPECPDL_INDEX ();
19393 init_iterator (&it, w, -1, -1, NULL, face_id);
19394 /* Don't extend on a previously drawn mode-line.
19395 This may happen if called from pos_visible_p. */
19396 it.glyph_row->enabled_p = 0;
19397 prepare_desired_row (it.glyph_row);
19399 it.glyph_row->mode_line_p = 1;
19401 if (! mode_line_inverse_video)
19402 /* Force the mode-line to be displayed in the default face. */
19403 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19405 /* FIXME: This should be controlled by a user option. But
19406 supporting such an option is not trivial, since the mode line is
19407 made up of many separate strings. */
19408 it.paragraph_embedding = L2R;
19410 record_unwind_protect (unwind_format_mode_line,
19411 format_mode_line_unwind_data (NULL, Qnil, 0));
19413 mode_line_target = MODE_LINE_DISPLAY;
19415 /* Temporarily make frame's keyboard the current kboard so that
19416 kboard-local variables in the mode_line_format will get the right
19417 values. */
19418 push_kboard (FRAME_KBOARD (it.f));
19419 record_unwind_save_match_data ();
19420 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19421 pop_kboard ();
19423 unbind_to (count, Qnil);
19425 /* Fill up with spaces. */
19426 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19428 compute_line_metrics (&it);
19429 it.glyph_row->full_width_p = 1;
19430 it.glyph_row->continued_p = 0;
19431 it.glyph_row->truncated_on_left_p = 0;
19432 it.glyph_row->truncated_on_right_p = 0;
19434 /* Make a 3D mode-line have a shadow at its right end. */
19435 face = FACE_FROM_ID (it.f, face_id);
19436 extend_face_to_end_of_line (&it);
19437 if (face->box != FACE_NO_BOX)
19439 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19440 + it.glyph_row->used[TEXT_AREA] - 1);
19441 last->right_box_line_p = 1;
19444 return it.glyph_row->height;
19447 /* Move element ELT in LIST to the front of LIST.
19448 Return the updated list. */
19450 static Lisp_Object
19451 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19453 register Lisp_Object tail, prev;
19454 register Lisp_Object tem;
19456 tail = list;
19457 prev = Qnil;
19458 while (CONSP (tail))
19460 tem = XCAR (tail);
19462 if (EQ (elt, tem))
19464 /* Splice out the link TAIL. */
19465 if (NILP (prev))
19466 list = XCDR (tail);
19467 else
19468 Fsetcdr (prev, XCDR (tail));
19470 /* Now make it the first. */
19471 Fsetcdr (tail, list);
19472 return tail;
19474 else
19475 prev = tail;
19476 tail = XCDR (tail);
19477 QUIT;
19480 /* Not found--return unchanged LIST. */
19481 return list;
19484 /* Contribute ELT to the mode line for window IT->w. How it
19485 translates into text depends on its data type.
19487 IT describes the display environment in which we display, as usual.
19489 DEPTH is the depth in recursion. It is used to prevent
19490 infinite recursion here.
19492 FIELD_WIDTH is the number of characters the display of ELT should
19493 occupy in the mode line, and PRECISION is the maximum number of
19494 characters to display from ELT's representation. See
19495 display_string for details.
19497 Returns the hpos of the end of the text generated by ELT.
19499 PROPS is a property list to add to any string we encounter.
19501 If RISKY is nonzero, remove (disregard) any properties in any string
19502 we encounter, and ignore :eval and :propertize.
19504 The global variable `mode_line_target' determines whether the
19505 output is passed to `store_mode_line_noprop',
19506 `store_mode_line_string', or `display_string'. */
19508 static int
19509 display_mode_element (struct it *it, int depth, int field_width, int precision,
19510 Lisp_Object elt, Lisp_Object props, int risky)
19512 int n = 0, field, prec;
19513 int literal = 0;
19515 tail_recurse:
19516 if (depth > 100)
19517 elt = build_string ("*too-deep*");
19519 depth++;
19521 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19523 case Lisp_String:
19525 /* A string: output it and check for %-constructs within it. */
19526 unsigned char c;
19527 EMACS_INT offset = 0;
19529 if (SCHARS (elt) > 0
19530 && (!NILP (props) || risky))
19532 Lisp_Object oprops, aelt;
19533 oprops = Ftext_properties_at (make_number (0), elt);
19535 /* If the starting string's properties are not what
19536 we want, translate the string. Also, if the string
19537 is risky, do that anyway. */
19539 if (NILP (Fequal (props, oprops)) || risky)
19541 /* If the starting string has properties,
19542 merge the specified ones onto the existing ones. */
19543 if (! NILP (oprops) && !risky)
19545 Lisp_Object tem;
19547 oprops = Fcopy_sequence (oprops);
19548 tem = props;
19549 while (CONSP (tem))
19551 oprops = Fplist_put (oprops, XCAR (tem),
19552 XCAR (XCDR (tem)));
19553 tem = XCDR (XCDR (tem));
19555 props = oprops;
19558 aelt = Fassoc (elt, mode_line_proptrans_alist);
19559 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19561 /* AELT is what we want. Move it to the front
19562 without consing. */
19563 elt = XCAR (aelt);
19564 mode_line_proptrans_alist
19565 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19567 else
19569 Lisp_Object tem;
19571 /* If AELT has the wrong props, it is useless.
19572 so get rid of it. */
19573 if (! NILP (aelt))
19574 mode_line_proptrans_alist
19575 = Fdelq (aelt, mode_line_proptrans_alist);
19577 elt = Fcopy_sequence (elt);
19578 Fset_text_properties (make_number (0), Flength (elt),
19579 props, elt);
19580 /* Add this item to mode_line_proptrans_alist. */
19581 mode_line_proptrans_alist
19582 = Fcons (Fcons (elt, props),
19583 mode_line_proptrans_alist);
19584 /* Truncate mode_line_proptrans_alist
19585 to at most 50 elements. */
19586 tem = Fnthcdr (make_number (50),
19587 mode_line_proptrans_alist);
19588 if (! NILP (tem))
19589 XSETCDR (tem, Qnil);
19594 offset = 0;
19596 if (literal)
19598 prec = precision - n;
19599 switch (mode_line_target)
19601 case MODE_LINE_NOPROP:
19602 case MODE_LINE_TITLE:
19603 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19604 break;
19605 case MODE_LINE_STRING:
19606 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19607 break;
19608 case MODE_LINE_DISPLAY:
19609 n += display_string (NULL, elt, Qnil, 0, 0, it,
19610 0, prec, 0, STRING_MULTIBYTE (elt));
19611 break;
19614 break;
19617 /* Handle the non-literal case. */
19619 while ((precision <= 0 || n < precision)
19620 && SREF (elt, offset) != 0
19621 && (mode_line_target != MODE_LINE_DISPLAY
19622 || it->current_x < it->last_visible_x))
19624 EMACS_INT last_offset = offset;
19626 /* Advance to end of string or next format specifier. */
19627 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19630 if (offset - 1 != last_offset)
19632 EMACS_INT nchars, nbytes;
19634 /* Output to end of string or up to '%'. Field width
19635 is length of string. Don't output more than
19636 PRECISION allows us. */
19637 offset--;
19639 prec = c_string_width (SDATA (elt) + last_offset,
19640 offset - last_offset, precision - n,
19641 &nchars, &nbytes);
19643 switch (mode_line_target)
19645 case MODE_LINE_NOPROP:
19646 case MODE_LINE_TITLE:
19647 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19648 break;
19649 case MODE_LINE_STRING:
19651 EMACS_INT bytepos = last_offset;
19652 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19653 EMACS_INT endpos = (precision <= 0
19654 ? string_byte_to_char (elt, offset)
19655 : charpos + nchars);
19657 n += store_mode_line_string (NULL,
19658 Fsubstring (elt, make_number (charpos),
19659 make_number (endpos)),
19660 0, 0, 0, Qnil);
19662 break;
19663 case MODE_LINE_DISPLAY:
19665 EMACS_INT bytepos = last_offset;
19666 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19668 if (precision <= 0)
19669 nchars = string_byte_to_char (elt, offset) - charpos;
19670 n += display_string (NULL, elt, Qnil, 0, charpos,
19671 it, 0, nchars, 0,
19672 STRING_MULTIBYTE (elt));
19674 break;
19677 else /* c == '%' */
19679 EMACS_INT percent_position = offset;
19681 /* Get the specified minimum width. Zero means
19682 don't pad. */
19683 field = 0;
19684 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19685 field = field * 10 + c - '0';
19687 /* Don't pad beyond the total padding allowed. */
19688 if (field_width - n > 0 && field > field_width - n)
19689 field = field_width - n;
19691 /* Note that either PRECISION <= 0 or N < PRECISION. */
19692 prec = precision - n;
19694 if (c == 'M')
19695 n += display_mode_element (it, depth, field, prec,
19696 Vglobal_mode_string, props,
19697 risky);
19698 else if (c != 0)
19700 int multibyte;
19701 EMACS_INT bytepos, charpos;
19702 const char *spec;
19703 Lisp_Object string;
19705 bytepos = percent_position;
19706 charpos = (STRING_MULTIBYTE (elt)
19707 ? string_byte_to_char (elt, bytepos)
19708 : bytepos);
19709 spec = decode_mode_spec (it->w, c, field, &string);
19710 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19712 switch (mode_line_target)
19714 case MODE_LINE_NOPROP:
19715 case MODE_LINE_TITLE:
19716 n += store_mode_line_noprop (spec, field, prec);
19717 break;
19718 case MODE_LINE_STRING:
19720 Lisp_Object tem = build_string (spec);
19721 props = Ftext_properties_at (make_number (charpos), elt);
19722 /* Should only keep face property in props */
19723 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19725 break;
19726 case MODE_LINE_DISPLAY:
19728 int nglyphs_before, nwritten;
19730 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19731 nwritten = display_string (spec, string, elt,
19732 charpos, 0, it,
19733 field, prec, 0,
19734 multibyte);
19736 /* Assign to the glyphs written above the
19737 string where the `%x' came from, position
19738 of the `%'. */
19739 if (nwritten > 0)
19741 struct glyph *glyph
19742 = (it->glyph_row->glyphs[TEXT_AREA]
19743 + nglyphs_before);
19744 int i;
19746 for (i = 0; i < nwritten; ++i)
19748 glyph[i].object = elt;
19749 glyph[i].charpos = charpos;
19752 n += nwritten;
19755 break;
19758 else /* c == 0 */
19759 break;
19763 break;
19765 case Lisp_Symbol:
19766 /* A symbol: process the value of the symbol recursively
19767 as if it appeared here directly. Avoid error if symbol void.
19768 Special case: if value of symbol is a string, output the string
19769 literally. */
19771 register Lisp_Object tem;
19773 /* If the variable is not marked as risky to set
19774 then its contents are risky to use. */
19775 if (NILP (Fget (elt, Qrisky_local_variable)))
19776 risky = 1;
19778 tem = Fboundp (elt);
19779 if (!NILP (tem))
19781 tem = Fsymbol_value (elt);
19782 /* If value is a string, output that string literally:
19783 don't check for % within it. */
19784 if (STRINGP (tem))
19785 literal = 1;
19787 if (!EQ (tem, elt))
19789 /* Give up right away for nil or t. */
19790 elt = tem;
19791 goto tail_recurse;
19795 break;
19797 case Lisp_Cons:
19799 register Lisp_Object car, tem;
19801 /* A cons cell: five distinct cases.
19802 If first element is :eval or :propertize, do something special.
19803 If first element is a string or a cons, process all the elements
19804 and effectively concatenate them.
19805 If first element is a negative number, truncate displaying cdr to
19806 at most that many characters. If positive, pad (with spaces)
19807 to at least that many characters.
19808 If first element is a symbol, process the cadr or caddr recursively
19809 according to whether the symbol's value is non-nil or nil. */
19810 car = XCAR (elt);
19811 if (EQ (car, QCeval))
19813 /* An element of the form (:eval FORM) means evaluate FORM
19814 and use the result as mode line elements. */
19816 if (risky)
19817 break;
19819 if (CONSP (XCDR (elt)))
19821 Lisp_Object spec;
19822 spec = safe_eval (XCAR (XCDR (elt)));
19823 n += display_mode_element (it, depth, field_width - n,
19824 precision - n, spec, props,
19825 risky);
19828 else if (EQ (car, QCpropertize))
19830 /* An element of the form (:propertize ELT PROPS...)
19831 means display ELT but applying properties PROPS. */
19833 if (risky)
19834 break;
19836 if (CONSP (XCDR (elt)))
19837 n += display_mode_element (it, depth, field_width - n,
19838 precision - n, XCAR (XCDR (elt)),
19839 XCDR (XCDR (elt)), risky);
19841 else if (SYMBOLP (car))
19843 tem = Fboundp (car);
19844 elt = XCDR (elt);
19845 if (!CONSP (elt))
19846 goto invalid;
19847 /* elt is now the cdr, and we know it is a cons cell.
19848 Use its car if CAR has a non-nil value. */
19849 if (!NILP (tem))
19851 tem = Fsymbol_value (car);
19852 if (!NILP (tem))
19854 elt = XCAR (elt);
19855 goto tail_recurse;
19858 /* Symbol's value is nil (or symbol is unbound)
19859 Get the cddr of the original list
19860 and if possible find the caddr and use that. */
19861 elt = XCDR (elt);
19862 if (NILP (elt))
19863 break;
19864 else if (!CONSP (elt))
19865 goto invalid;
19866 elt = XCAR (elt);
19867 goto tail_recurse;
19869 else if (INTEGERP (car))
19871 register int lim = XINT (car);
19872 elt = XCDR (elt);
19873 if (lim < 0)
19875 /* Negative int means reduce maximum width. */
19876 if (precision <= 0)
19877 precision = -lim;
19878 else
19879 precision = min (precision, -lim);
19881 else if (lim > 0)
19883 /* Padding specified. Don't let it be more than
19884 current maximum. */
19885 if (precision > 0)
19886 lim = min (precision, lim);
19888 /* If that's more padding than already wanted, queue it.
19889 But don't reduce padding already specified even if
19890 that is beyond the current truncation point. */
19891 field_width = max (lim, field_width);
19893 goto tail_recurse;
19895 else if (STRINGP (car) || CONSP (car))
19897 Lisp_Object halftail = elt;
19898 int len = 0;
19900 while (CONSP (elt)
19901 && (precision <= 0 || n < precision))
19903 n += display_mode_element (it, depth,
19904 /* Do padding only after the last
19905 element in the list. */
19906 (! CONSP (XCDR (elt))
19907 ? field_width - n
19908 : 0),
19909 precision - n, XCAR (elt),
19910 props, risky);
19911 elt = XCDR (elt);
19912 len++;
19913 if ((len & 1) == 0)
19914 halftail = XCDR (halftail);
19915 /* Check for cycle. */
19916 if (EQ (halftail, elt))
19917 break;
19921 break;
19923 default:
19924 invalid:
19925 elt = build_string ("*invalid*");
19926 goto tail_recurse;
19929 /* Pad to FIELD_WIDTH. */
19930 if (field_width > 0 && n < field_width)
19932 switch (mode_line_target)
19934 case MODE_LINE_NOPROP:
19935 case MODE_LINE_TITLE:
19936 n += store_mode_line_noprop ("", field_width - n, 0);
19937 break;
19938 case MODE_LINE_STRING:
19939 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19940 break;
19941 case MODE_LINE_DISPLAY:
19942 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19943 0, 0, 0);
19944 break;
19948 return n;
19951 /* Store a mode-line string element in mode_line_string_list.
19953 If STRING is non-null, display that C string. Otherwise, the Lisp
19954 string LISP_STRING is displayed.
19956 FIELD_WIDTH is the minimum number of output glyphs to produce.
19957 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19958 with spaces. FIELD_WIDTH <= 0 means don't pad.
19960 PRECISION is the maximum number of characters to output from
19961 STRING. PRECISION <= 0 means don't truncate the string.
19963 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19964 properties to the string.
19966 PROPS are the properties to add to the string.
19967 The mode_line_string_face face property is always added to the string.
19970 static int
19971 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19972 int field_width, int precision, Lisp_Object props)
19974 EMACS_INT len;
19975 int n = 0;
19977 if (string != NULL)
19979 len = strlen (string);
19980 if (precision > 0 && len > precision)
19981 len = precision;
19982 lisp_string = make_string (string, len);
19983 if (NILP (props))
19984 props = mode_line_string_face_prop;
19985 else if (!NILP (mode_line_string_face))
19987 Lisp_Object face = Fplist_get (props, Qface);
19988 props = Fcopy_sequence (props);
19989 if (NILP (face))
19990 face = mode_line_string_face;
19991 else
19992 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19993 props = Fplist_put (props, Qface, face);
19995 Fadd_text_properties (make_number (0), make_number (len),
19996 props, lisp_string);
19998 else
20000 len = XFASTINT (Flength (lisp_string));
20001 if (precision > 0 && len > precision)
20003 len = precision;
20004 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20005 precision = -1;
20007 if (!NILP (mode_line_string_face))
20009 Lisp_Object face;
20010 if (NILP (props))
20011 props = Ftext_properties_at (make_number (0), lisp_string);
20012 face = Fplist_get (props, Qface);
20013 if (NILP (face))
20014 face = mode_line_string_face;
20015 else
20016 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20017 props = Fcons (Qface, Fcons (face, Qnil));
20018 if (copy_string)
20019 lisp_string = Fcopy_sequence (lisp_string);
20021 if (!NILP (props))
20022 Fadd_text_properties (make_number (0), make_number (len),
20023 props, lisp_string);
20026 if (len > 0)
20028 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20029 n += len;
20032 if (field_width > len)
20034 field_width -= len;
20035 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20036 if (!NILP (props))
20037 Fadd_text_properties (make_number (0), make_number (field_width),
20038 props, lisp_string);
20039 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20040 n += field_width;
20043 return n;
20047 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20048 1, 4, 0,
20049 doc: /* Format a string out of a mode line format specification.
20050 First arg FORMAT specifies the mode line format (see `mode-line-format'
20051 for details) to use.
20053 By default, the format is evaluated for the currently selected window.
20055 Optional second arg FACE specifies the face property to put on all
20056 characters for which no face is specified. The value nil means the
20057 default face. The value t means whatever face the window's mode line
20058 currently uses (either `mode-line' or `mode-line-inactive',
20059 depending on whether the window is the selected window or not).
20060 An integer value means the value string has no text
20061 properties.
20063 Optional third and fourth args WINDOW and BUFFER specify the window
20064 and buffer to use as the context for the formatting (defaults
20065 are the selected window and the WINDOW's buffer). */)
20066 (Lisp_Object format, Lisp_Object face,
20067 Lisp_Object window, Lisp_Object buffer)
20069 struct it it;
20070 int len;
20071 struct window *w;
20072 struct buffer *old_buffer = NULL;
20073 int face_id;
20074 int no_props = INTEGERP (face);
20075 int count = SPECPDL_INDEX ();
20076 Lisp_Object str;
20077 int string_start = 0;
20079 if (NILP (window))
20080 window = selected_window;
20081 CHECK_WINDOW (window);
20082 w = XWINDOW (window);
20084 if (NILP (buffer))
20085 buffer = w->buffer;
20086 CHECK_BUFFER (buffer);
20088 /* Make formatting the modeline a non-op when noninteractive, otherwise
20089 there will be problems later caused by a partially initialized frame. */
20090 if (NILP (format) || noninteractive)
20091 return empty_unibyte_string;
20093 if (no_props)
20094 face = Qnil;
20096 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20097 : EQ (face, Qt) ? (EQ (window, selected_window)
20098 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20099 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20100 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20101 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20102 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20103 : DEFAULT_FACE_ID;
20105 if (XBUFFER (buffer) != current_buffer)
20106 old_buffer = current_buffer;
20108 /* Save things including mode_line_proptrans_alist,
20109 and set that to nil so that we don't alter the outer value. */
20110 record_unwind_protect (unwind_format_mode_line,
20111 format_mode_line_unwind_data
20112 (old_buffer, selected_window, 1));
20113 mode_line_proptrans_alist = Qnil;
20115 Fselect_window (window, Qt);
20116 if (old_buffer)
20117 set_buffer_internal_1 (XBUFFER (buffer));
20119 init_iterator (&it, w, -1, -1, NULL, face_id);
20121 if (no_props)
20123 mode_line_target = MODE_LINE_NOPROP;
20124 mode_line_string_face_prop = Qnil;
20125 mode_line_string_list = Qnil;
20126 string_start = MODE_LINE_NOPROP_LEN (0);
20128 else
20130 mode_line_target = MODE_LINE_STRING;
20131 mode_line_string_list = Qnil;
20132 mode_line_string_face = face;
20133 mode_line_string_face_prop
20134 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20137 push_kboard (FRAME_KBOARD (it.f));
20138 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20139 pop_kboard ();
20141 if (no_props)
20143 len = MODE_LINE_NOPROP_LEN (string_start);
20144 str = make_string (mode_line_noprop_buf + string_start, len);
20146 else
20148 mode_line_string_list = Fnreverse (mode_line_string_list);
20149 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20150 empty_unibyte_string);
20153 unbind_to (count, Qnil);
20154 return str;
20157 /* Write a null-terminated, right justified decimal representation of
20158 the positive integer D to BUF using a minimal field width WIDTH. */
20160 static void
20161 pint2str (register char *buf, register int width, register EMACS_INT d)
20163 register char *p = buf;
20165 if (d <= 0)
20166 *p++ = '0';
20167 else
20169 while (d > 0)
20171 *p++ = d % 10 + '0';
20172 d /= 10;
20176 for (width -= (int) (p - buf); width > 0; --width)
20177 *p++ = ' ';
20178 *p-- = '\0';
20179 while (p > buf)
20181 d = *buf;
20182 *buf++ = *p;
20183 *p-- = d;
20187 /* Write a null-terminated, right justified decimal and "human
20188 readable" representation of the nonnegative integer D to BUF using
20189 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20191 static const char power_letter[] =
20193 0, /* no letter */
20194 'k', /* kilo */
20195 'M', /* mega */
20196 'G', /* giga */
20197 'T', /* tera */
20198 'P', /* peta */
20199 'E', /* exa */
20200 'Z', /* zetta */
20201 'Y' /* yotta */
20204 static void
20205 pint2hrstr (char *buf, int width, EMACS_INT d)
20207 /* We aim to represent the nonnegative integer D as
20208 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20209 EMACS_INT quotient = d;
20210 int remainder = 0;
20211 /* -1 means: do not use TENTHS. */
20212 int tenths = -1;
20213 int exponent = 0;
20215 /* Length of QUOTIENT.TENTHS as a string. */
20216 int length;
20218 char * psuffix;
20219 char * p;
20221 if (1000 <= quotient)
20223 /* Scale to the appropriate EXPONENT. */
20226 remainder = quotient % 1000;
20227 quotient /= 1000;
20228 exponent++;
20230 while (1000 <= quotient);
20232 /* Round to nearest and decide whether to use TENTHS or not. */
20233 if (quotient <= 9)
20235 tenths = remainder / 100;
20236 if (50 <= remainder % 100)
20238 if (tenths < 9)
20239 tenths++;
20240 else
20242 quotient++;
20243 if (quotient == 10)
20244 tenths = -1;
20245 else
20246 tenths = 0;
20250 else
20251 if (500 <= remainder)
20253 if (quotient < 999)
20254 quotient++;
20255 else
20257 quotient = 1;
20258 exponent++;
20259 tenths = 0;
20264 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20265 if (tenths == -1 && quotient <= 99)
20266 if (quotient <= 9)
20267 length = 1;
20268 else
20269 length = 2;
20270 else
20271 length = 3;
20272 p = psuffix = buf + max (width, length);
20274 /* Print EXPONENT. */
20275 *psuffix++ = power_letter[exponent];
20276 *psuffix = '\0';
20278 /* Print TENTHS. */
20279 if (tenths >= 0)
20281 *--p = '0' + tenths;
20282 *--p = '.';
20285 /* Print QUOTIENT. */
20288 int digit = quotient % 10;
20289 *--p = '0' + digit;
20291 while ((quotient /= 10) != 0);
20293 /* Print leading spaces. */
20294 while (buf < p)
20295 *--p = ' ';
20298 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20299 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20300 type of CODING_SYSTEM. Return updated pointer into BUF. */
20302 static unsigned char invalid_eol_type[] = "(*invalid*)";
20304 static char *
20305 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20307 Lisp_Object val;
20308 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20309 const unsigned char *eol_str;
20310 int eol_str_len;
20311 /* The EOL conversion we are using. */
20312 Lisp_Object eoltype;
20314 val = CODING_SYSTEM_SPEC (coding_system);
20315 eoltype = Qnil;
20317 if (!VECTORP (val)) /* Not yet decided. */
20319 if (multibyte)
20320 *buf++ = '-';
20321 if (eol_flag)
20322 eoltype = eol_mnemonic_undecided;
20323 /* Don't mention EOL conversion if it isn't decided. */
20325 else
20327 Lisp_Object attrs;
20328 Lisp_Object eolvalue;
20330 attrs = AREF (val, 0);
20331 eolvalue = AREF (val, 2);
20333 if (multibyte)
20334 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20336 if (eol_flag)
20338 /* The EOL conversion that is normal on this system. */
20340 if (NILP (eolvalue)) /* Not yet decided. */
20341 eoltype = eol_mnemonic_undecided;
20342 else if (VECTORP (eolvalue)) /* Not yet decided. */
20343 eoltype = eol_mnemonic_undecided;
20344 else /* eolvalue is Qunix, Qdos, or Qmac. */
20345 eoltype = (EQ (eolvalue, Qunix)
20346 ? eol_mnemonic_unix
20347 : (EQ (eolvalue, Qdos) == 1
20348 ? eol_mnemonic_dos : eol_mnemonic_mac));
20352 if (eol_flag)
20354 /* Mention the EOL conversion if it is not the usual one. */
20355 if (STRINGP (eoltype))
20357 eol_str = SDATA (eoltype);
20358 eol_str_len = SBYTES (eoltype);
20360 else if (CHARACTERP (eoltype))
20362 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20363 int c = XFASTINT (eoltype);
20364 eol_str_len = CHAR_STRING (c, tmp);
20365 eol_str = tmp;
20367 else
20369 eol_str = invalid_eol_type;
20370 eol_str_len = sizeof (invalid_eol_type) - 1;
20372 memcpy (buf, eol_str, eol_str_len);
20373 buf += eol_str_len;
20376 return buf;
20379 /* Return a string for the output of a mode line %-spec for window W,
20380 generated by character C. FIELD_WIDTH > 0 means pad the string
20381 returned with spaces to that value. Return a Lisp string in
20382 *STRING if the resulting string is taken from that Lisp string.
20384 Note we operate on the current buffer for most purposes,
20385 the exception being w->base_line_pos. */
20387 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20389 static const char *
20390 decode_mode_spec (struct window *w, register int c, int field_width,
20391 Lisp_Object *string)
20393 Lisp_Object obj;
20394 struct frame *f = XFRAME (WINDOW_FRAME (w));
20395 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20396 struct buffer *b = current_buffer;
20398 obj = Qnil;
20399 *string = Qnil;
20401 switch (c)
20403 case '*':
20404 if (!NILP (BVAR (b, read_only)))
20405 return "%";
20406 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20407 return "*";
20408 return "-";
20410 case '+':
20411 /* This differs from %* only for a modified read-only buffer. */
20412 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20413 return "*";
20414 if (!NILP (BVAR (b, read_only)))
20415 return "%";
20416 return "-";
20418 case '&':
20419 /* This differs from %* in ignoring read-only-ness. */
20420 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20421 return "*";
20422 return "-";
20424 case '%':
20425 return "%";
20427 case '[':
20429 int i;
20430 char *p;
20432 if (command_loop_level > 5)
20433 return "[[[... ";
20434 p = decode_mode_spec_buf;
20435 for (i = 0; i < command_loop_level; i++)
20436 *p++ = '[';
20437 *p = 0;
20438 return decode_mode_spec_buf;
20441 case ']':
20443 int i;
20444 char *p;
20446 if (command_loop_level > 5)
20447 return " ...]]]";
20448 p = decode_mode_spec_buf;
20449 for (i = 0; i < command_loop_level; i++)
20450 *p++ = ']';
20451 *p = 0;
20452 return decode_mode_spec_buf;
20455 case '-':
20457 register int i;
20459 /* Let lots_of_dashes be a string of infinite length. */
20460 if (mode_line_target == MODE_LINE_NOPROP ||
20461 mode_line_target == MODE_LINE_STRING)
20462 return "--";
20463 if (field_width <= 0
20464 || field_width > sizeof (lots_of_dashes))
20466 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20467 decode_mode_spec_buf[i] = '-';
20468 decode_mode_spec_buf[i] = '\0';
20469 return decode_mode_spec_buf;
20471 else
20472 return lots_of_dashes;
20475 case 'b':
20476 obj = BVAR (b, name);
20477 break;
20479 case 'c':
20480 /* %c and %l are ignored in `frame-title-format'.
20481 (In redisplay_internal, the frame title is drawn _before_ the
20482 windows are updated, so the stuff which depends on actual
20483 window contents (such as %l) may fail to render properly, or
20484 even crash emacs.) */
20485 if (mode_line_target == MODE_LINE_TITLE)
20486 return "";
20487 else
20489 EMACS_INT col = current_column ();
20490 w->column_number_displayed = make_number (col);
20491 pint2str (decode_mode_spec_buf, field_width, col);
20492 return decode_mode_spec_buf;
20495 case 'e':
20496 #ifndef SYSTEM_MALLOC
20498 if (NILP (Vmemory_full))
20499 return "";
20500 else
20501 return "!MEM FULL! ";
20503 #else
20504 return "";
20505 #endif
20507 case 'F':
20508 /* %F displays the frame name. */
20509 if (!NILP (f->title))
20510 return SSDATA (f->title);
20511 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20512 return SSDATA (f->name);
20513 return "Emacs";
20515 case 'f':
20516 obj = BVAR (b, filename);
20517 break;
20519 case 'i':
20521 EMACS_INT size = ZV - BEGV;
20522 pint2str (decode_mode_spec_buf, field_width, size);
20523 return decode_mode_spec_buf;
20526 case 'I':
20528 EMACS_INT size = ZV - BEGV;
20529 pint2hrstr (decode_mode_spec_buf, field_width, size);
20530 return decode_mode_spec_buf;
20533 case 'l':
20535 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20536 EMACS_INT topline, nlines, height;
20537 EMACS_INT junk;
20539 /* %c and %l are ignored in `frame-title-format'. */
20540 if (mode_line_target == MODE_LINE_TITLE)
20541 return "";
20543 startpos = XMARKER (w->start)->charpos;
20544 startpos_byte = marker_byte_position (w->start);
20545 height = WINDOW_TOTAL_LINES (w);
20547 /* If we decided that this buffer isn't suitable for line numbers,
20548 don't forget that too fast. */
20549 if (EQ (w->base_line_pos, w->buffer))
20550 goto no_value;
20551 /* But do forget it, if the window shows a different buffer now. */
20552 else if (BUFFERP (w->base_line_pos))
20553 w->base_line_pos = Qnil;
20555 /* If the buffer is very big, don't waste time. */
20556 if (INTEGERP (Vline_number_display_limit)
20557 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20559 w->base_line_pos = Qnil;
20560 w->base_line_number = Qnil;
20561 goto no_value;
20564 if (INTEGERP (w->base_line_number)
20565 && INTEGERP (w->base_line_pos)
20566 && XFASTINT (w->base_line_pos) <= startpos)
20568 line = XFASTINT (w->base_line_number);
20569 linepos = XFASTINT (w->base_line_pos);
20570 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20572 else
20574 line = 1;
20575 linepos = BUF_BEGV (b);
20576 linepos_byte = BUF_BEGV_BYTE (b);
20579 /* Count lines from base line to window start position. */
20580 nlines = display_count_lines (linepos_byte,
20581 startpos_byte,
20582 startpos, &junk);
20584 topline = nlines + line;
20586 /* Determine a new base line, if the old one is too close
20587 or too far away, or if we did not have one.
20588 "Too close" means it's plausible a scroll-down would
20589 go back past it. */
20590 if (startpos == BUF_BEGV (b))
20592 w->base_line_number = make_number (topline);
20593 w->base_line_pos = make_number (BUF_BEGV (b));
20595 else if (nlines < height + 25 || nlines > height * 3 + 50
20596 || linepos == BUF_BEGV (b))
20598 EMACS_INT limit = BUF_BEGV (b);
20599 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20600 EMACS_INT position;
20601 EMACS_INT distance =
20602 (height * 2 + 30) * line_number_display_limit_width;
20604 if (startpos - distance > limit)
20606 limit = startpos - distance;
20607 limit_byte = CHAR_TO_BYTE (limit);
20610 nlines = display_count_lines (startpos_byte,
20611 limit_byte,
20612 - (height * 2 + 30),
20613 &position);
20614 /* If we couldn't find the lines we wanted within
20615 line_number_display_limit_width chars per line,
20616 give up on line numbers for this window. */
20617 if (position == limit_byte && limit == startpos - distance)
20619 w->base_line_pos = w->buffer;
20620 w->base_line_number = Qnil;
20621 goto no_value;
20624 w->base_line_number = make_number (topline - nlines);
20625 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20628 /* Now count lines from the start pos to point. */
20629 nlines = display_count_lines (startpos_byte,
20630 PT_BYTE, PT, &junk);
20632 /* Record that we did display the line number. */
20633 line_number_displayed = 1;
20635 /* Make the string to show. */
20636 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20637 return decode_mode_spec_buf;
20638 no_value:
20640 char* p = decode_mode_spec_buf;
20641 int pad = field_width - 2;
20642 while (pad-- > 0)
20643 *p++ = ' ';
20644 *p++ = '?';
20645 *p++ = '?';
20646 *p = '\0';
20647 return decode_mode_spec_buf;
20650 break;
20652 case 'm':
20653 obj = BVAR (b, mode_name);
20654 break;
20656 case 'n':
20657 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20658 return " Narrow";
20659 break;
20661 case 'p':
20663 EMACS_INT pos = marker_position (w->start);
20664 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20666 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20668 if (pos <= BUF_BEGV (b))
20669 return "All";
20670 else
20671 return "Bottom";
20673 else if (pos <= BUF_BEGV (b))
20674 return "Top";
20675 else
20677 if (total > 1000000)
20678 /* Do it differently for a large value, to avoid overflow. */
20679 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20680 else
20681 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20682 /* We can't normally display a 3-digit number,
20683 so get us a 2-digit number that is close. */
20684 if (total == 100)
20685 total = 99;
20686 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20687 return decode_mode_spec_buf;
20691 /* Display percentage of size above the bottom of the screen. */
20692 case 'P':
20694 EMACS_INT toppos = marker_position (w->start);
20695 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20696 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20698 if (botpos >= BUF_ZV (b))
20700 if (toppos <= BUF_BEGV (b))
20701 return "All";
20702 else
20703 return "Bottom";
20705 else
20707 if (total > 1000000)
20708 /* Do it differently for a large value, to avoid overflow. */
20709 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20710 else
20711 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20712 /* We can't normally display a 3-digit number,
20713 so get us a 2-digit number that is close. */
20714 if (total == 100)
20715 total = 99;
20716 if (toppos <= BUF_BEGV (b))
20717 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20718 else
20719 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20720 return decode_mode_spec_buf;
20724 case 's':
20725 /* status of process */
20726 obj = Fget_buffer_process (Fcurrent_buffer ());
20727 if (NILP (obj))
20728 return "no process";
20729 #ifndef MSDOS
20730 obj = Fsymbol_name (Fprocess_status (obj));
20731 #endif
20732 break;
20734 case '@':
20736 int count = inhibit_garbage_collection ();
20737 Lisp_Object val = call1 (intern ("file-remote-p"),
20738 BVAR (current_buffer, directory));
20739 unbind_to (count, Qnil);
20741 if (NILP (val))
20742 return "-";
20743 else
20744 return "@";
20747 case 't': /* indicate TEXT or BINARY */
20748 return "T";
20750 case 'z':
20751 /* coding-system (not including end-of-line format) */
20752 case 'Z':
20753 /* coding-system (including end-of-line type) */
20755 int eol_flag = (c == 'Z');
20756 char *p = decode_mode_spec_buf;
20758 if (! FRAME_WINDOW_P (f))
20760 /* No need to mention EOL here--the terminal never needs
20761 to do EOL conversion. */
20762 p = decode_mode_spec_coding (CODING_ID_NAME
20763 (FRAME_KEYBOARD_CODING (f)->id),
20764 p, 0);
20765 p = decode_mode_spec_coding (CODING_ID_NAME
20766 (FRAME_TERMINAL_CODING (f)->id),
20767 p, 0);
20769 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20770 p, eol_flag);
20772 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20773 #ifdef subprocesses
20774 obj = Fget_buffer_process (Fcurrent_buffer ());
20775 if (PROCESSP (obj))
20777 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20778 p, eol_flag);
20779 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20780 p, eol_flag);
20782 #endif /* subprocesses */
20783 #endif /* 0 */
20784 *p = 0;
20785 return decode_mode_spec_buf;
20789 if (STRINGP (obj))
20791 *string = obj;
20792 return SSDATA (obj);
20794 else
20795 return "";
20799 /* Count up to COUNT lines starting from START_BYTE.
20800 But don't go beyond LIMIT_BYTE.
20801 Return the number of lines thus found (always nonnegative).
20803 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20805 static EMACS_INT
20806 display_count_lines (EMACS_INT start_byte,
20807 EMACS_INT limit_byte, EMACS_INT count,
20808 EMACS_INT *byte_pos_ptr)
20810 register unsigned char *cursor;
20811 unsigned char *base;
20813 register EMACS_INT ceiling;
20814 register unsigned char *ceiling_addr;
20815 EMACS_INT orig_count = count;
20817 /* If we are not in selective display mode,
20818 check only for newlines. */
20819 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20820 && !INTEGERP (BVAR (current_buffer, selective_display)));
20822 if (count > 0)
20824 while (start_byte < limit_byte)
20826 ceiling = BUFFER_CEILING_OF (start_byte);
20827 ceiling = min (limit_byte - 1, ceiling);
20828 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20829 base = (cursor = BYTE_POS_ADDR (start_byte));
20830 while (1)
20832 if (selective_display)
20833 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20835 else
20836 while (*cursor != '\n' && ++cursor != ceiling_addr)
20839 if (cursor != ceiling_addr)
20841 if (--count == 0)
20843 start_byte += cursor - base + 1;
20844 *byte_pos_ptr = start_byte;
20845 return orig_count;
20847 else
20848 if (++cursor == ceiling_addr)
20849 break;
20851 else
20852 break;
20854 start_byte += cursor - base;
20857 else
20859 while (start_byte > limit_byte)
20861 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20862 ceiling = max (limit_byte, ceiling);
20863 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20864 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20865 while (1)
20867 if (selective_display)
20868 while (--cursor != ceiling_addr
20869 && *cursor != '\n' && *cursor != 015)
20871 else
20872 while (--cursor != ceiling_addr && *cursor != '\n')
20875 if (cursor != ceiling_addr)
20877 if (++count == 0)
20879 start_byte += cursor - base + 1;
20880 *byte_pos_ptr = start_byte;
20881 /* When scanning backwards, we should
20882 not count the newline posterior to which we stop. */
20883 return - orig_count - 1;
20886 else
20887 break;
20889 /* Here we add 1 to compensate for the last decrement
20890 of CURSOR, which took it past the valid range. */
20891 start_byte += cursor - base + 1;
20895 *byte_pos_ptr = limit_byte;
20897 if (count < 0)
20898 return - orig_count + count;
20899 return orig_count - count;
20905 /***********************************************************************
20906 Displaying strings
20907 ***********************************************************************/
20909 /* Display a NUL-terminated string, starting with index START.
20911 If STRING is non-null, display that C string. Otherwise, the Lisp
20912 string LISP_STRING is displayed. There's a case that STRING is
20913 non-null and LISP_STRING is not nil. It means STRING is a string
20914 data of LISP_STRING. In that case, we display LISP_STRING while
20915 ignoring its text properties.
20917 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20918 FACE_STRING. Display STRING or LISP_STRING with the face at
20919 FACE_STRING_POS in FACE_STRING:
20921 Display the string in the environment given by IT, but use the
20922 standard display table, temporarily.
20924 FIELD_WIDTH is the minimum number of output glyphs to produce.
20925 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20926 with spaces. If STRING has more characters, more than FIELD_WIDTH
20927 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20929 PRECISION is the maximum number of characters to output from
20930 STRING. PRECISION < 0 means don't truncate the string.
20932 This is roughly equivalent to printf format specifiers:
20934 FIELD_WIDTH PRECISION PRINTF
20935 ----------------------------------------
20936 -1 -1 %s
20937 -1 10 %.10s
20938 10 -1 %10s
20939 20 10 %20.10s
20941 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20942 display them, and < 0 means obey the current buffer's value of
20943 enable_multibyte_characters.
20945 Value is the number of columns displayed. */
20947 static int
20948 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20949 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20950 int field_width, int precision, int max_x, int multibyte)
20952 int hpos_at_start = it->hpos;
20953 int saved_face_id = it->face_id;
20954 struct glyph_row *row = it->glyph_row;
20955 EMACS_INT it_charpos;
20957 /* Initialize the iterator IT for iteration over STRING beginning
20958 with index START. */
20959 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20960 precision, field_width, multibyte);
20961 if (string && STRINGP (lisp_string))
20962 /* LISP_STRING is the one returned by decode_mode_spec. We should
20963 ignore its text properties. */
20964 it->stop_charpos = it->end_charpos;
20966 /* If displaying STRING, set up the face of the iterator from
20967 FACE_STRING, if that's given. */
20968 if (STRINGP (face_string))
20970 EMACS_INT endptr;
20971 struct face *face;
20973 it->face_id
20974 = face_at_string_position (it->w, face_string, face_string_pos,
20975 0, it->region_beg_charpos,
20976 it->region_end_charpos,
20977 &endptr, it->base_face_id, 0);
20978 face = FACE_FROM_ID (it->f, it->face_id);
20979 it->face_box_p = face->box != FACE_NO_BOX;
20982 /* Set max_x to the maximum allowed X position. Don't let it go
20983 beyond the right edge of the window. */
20984 if (max_x <= 0)
20985 max_x = it->last_visible_x;
20986 else
20987 max_x = min (max_x, it->last_visible_x);
20989 /* Skip over display elements that are not visible. because IT->w is
20990 hscrolled. */
20991 if (it->current_x < it->first_visible_x)
20992 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20993 MOVE_TO_POS | MOVE_TO_X);
20995 row->ascent = it->max_ascent;
20996 row->height = it->max_ascent + it->max_descent;
20997 row->phys_ascent = it->max_phys_ascent;
20998 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20999 row->extra_line_spacing = it->max_extra_line_spacing;
21001 if (STRINGP (it->string))
21002 it_charpos = IT_STRING_CHARPOS (*it);
21003 else
21004 it_charpos = IT_CHARPOS (*it);
21006 /* This condition is for the case that we are called with current_x
21007 past last_visible_x. */
21008 while (it->current_x < max_x)
21010 int x_before, x, n_glyphs_before, i, nglyphs;
21012 /* Get the next display element. */
21013 if (!get_next_display_element (it))
21014 break;
21016 /* Produce glyphs. */
21017 x_before = it->current_x;
21018 n_glyphs_before = row->used[TEXT_AREA];
21019 PRODUCE_GLYPHS (it);
21021 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21022 i = 0;
21023 x = x_before;
21024 while (i < nglyphs)
21026 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21028 if (it->line_wrap != TRUNCATE
21029 && x + glyph->pixel_width > max_x)
21031 /* End of continued line or max_x reached. */
21032 if (CHAR_GLYPH_PADDING_P (*glyph))
21034 /* A wide character is unbreakable. */
21035 if (row->reversed_p)
21036 unproduce_glyphs (it, row->used[TEXT_AREA]
21037 - n_glyphs_before);
21038 row->used[TEXT_AREA] = n_glyphs_before;
21039 it->current_x = x_before;
21041 else
21043 if (row->reversed_p)
21044 unproduce_glyphs (it, row->used[TEXT_AREA]
21045 - (n_glyphs_before + i));
21046 row->used[TEXT_AREA] = n_glyphs_before + i;
21047 it->current_x = x;
21049 break;
21051 else if (x + glyph->pixel_width >= it->first_visible_x)
21053 /* Glyph is at least partially visible. */
21054 ++it->hpos;
21055 if (x < it->first_visible_x)
21056 row->x = x - it->first_visible_x;
21058 else
21060 /* Glyph is off the left margin of the display area.
21061 Should not happen. */
21062 abort ();
21065 row->ascent = max (row->ascent, it->max_ascent);
21066 row->height = max (row->height, it->max_ascent + it->max_descent);
21067 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21068 row->phys_height = max (row->phys_height,
21069 it->max_phys_ascent + it->max_phys_descent);
21070 row->extra_line_spacing = max (row->extra_line_spacing,
21071 it->max_extra_line_spacing);
21072 x += glyph->pixel_width;
21073 ++i;
21076 /* Stop if max_x reached. */
21077 if (i < nglyphs)
21078 break;
21080 /* Stop at line ends. */
21081 if (ITERATOR_AT_END_OF_LINE_P (it))
21083 it->continuation_lines_width = 0;
21084 break;
21087 set_iterator_to_next (it, 1);
21088 if (STRINGP (it->string))
21089 it_charpos = IT_STRING_CHARPOS (*it);
21090 else
21091 it_charpos = IT_CHARPOS (*it);
21093 /* Stop if truncating at the right edge. */
21094 if (it->line_wrap == TRUNCATE
21095 && it->current_x >= it->last_visible_x)
21097 /* Add truncation mark, but don't do it if the line is
21098 truncated at a padding space. */
21099 if (it_charpos < it->string_nchars)
21101 if (!FRAME_WINDOW_P (it->f))
21103 int ii, n;
21105 if (it->current_x > it->last_visible_x)
21107 if (!row->reversed_p)
21109 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21110 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21111 break;
21113 else
21115 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21116 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21117 break;
21118 unproduce_glyphs (it, ii + 1);
21119 ii = row->used[TEXT_AREA] - (ii + 1);
21121 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21123 row->used[TEXT_AREA] = ii;
21124 produce_special_glyphs (it, IT_TRUNCATION);
21127 produce_special_glyphs (it, IT_TRUNCATION);
21129 row->truncated_on_right_p = 1;
21131 break;
21135 /* Maybe insert a truncation at the left. */
21136 if (it->first_visible_x
21137 && it_charpos > 0)
21139 if (!FRAME_WINDOW_P (it->f))
21140 insert_left_trunc_glyphs (it);
21141 row->truncated_on_left_p = 1;
21144 it->face_id = saved_face_id;
21146 /* Value is number of columns displayed. */
21147 return it->hpos - hpos_at_start;
21152 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21153 appears as an element of LIST or as the car of an element of LIST.
21154 If PROPVAL is a list, compare each element against LIST in that
21155 way, and return 1/2 if any element of PROPVAL is found in LIST.
21156 Otherwise return 0. This function cannot quit.
21157 The return value is 2 if the text is invisible but with an ellipsis
21158 and 1 if it's invisible and without an ellipsis. */
21161 invisible_p (register Lisp_Object propval, Lisp_Object list)
21163 register Lisp_Object tail, proptail;
21165 for (tail = list; CONSP (tail); tail = XCDR (tail))
21167 register Lisp_Object tem;
21168 tem = XCAR (tail);
21169 if (EQ (propval, tem))
21170 return 1;
21171 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21172 return NILP (XCDR (tem)) ? 1 : 2;
21175 if (CONSP (propval))
21177 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21179 Lisp_Object propelt;
21180 propelt = XCAR (proptail);
21181 for (tail = list; CONSP (tail); tail = XCDR (tail))
21183 register Lisp_Object tem;
21184 tem = XCAR (tail);
21185 if (EQ (propelt, tem))
21186 return 1;
21187 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21188 return NILP (XCDR (tem)) ? 1 : 2;
21193 return 0;
21196 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21197 doc: /* Non-nil if the property makes the text invisible.
21198 POS-OR-PROP can be a marker or number, in which case it is taken to be
21199 a position in the current buffer and the value of the `invisible' property
21200 is checked; or it can be some other value, which is then presumed to be the
21201 value of the `invisible' property of the text of interest.
21202 The non-nil value returned can be t for truly invisible text or something
21203 else if the text is replaced by an ellipsis. */)
21204 (Lisp_Object pos_or_prop)
21206 Lisp_Object prop
21207 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21208 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21209 : pos_or_prop);
21210 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21211 return (invis == 0 ? Qnil
21212 : invis == 1 ? Qt
21213 : make_number (invis));
21216 /* Calculate a width or height in pixels from a specification using
21217 the following elements:
21219 SPEC ::=
21220 NUM - a (fractional) multiple of the default font width/height
21221 (NUM) - specifies exactly NUM pixels
21222 UNIT - a fixed number of pixels, see below.
21223 ELEMENT - size of a display element in pixels, see below.
21224 (NUM . SPEC) - equals NUM * SPEC
21225 (+ SPEC SPEC ...) - add pixel values
21226 (- SPEC SPEC ...) - subtract pixel values
21227 (- SPEC) - negate pixel value
21229 NUM ::=
21230 INT or FLOAT - a number constant
21231 SYMBOL - use symbol's (buffer local) variable binding.
21233 UNIT ::=
21234 in - pixels per inch *)
21235 mm - pixels per 1/1000 meter *)
21236 cm - pixels per 1/100 meter *)
21237 width - width of current font in pixels.
21238 height - height of current font in pixels.
21240 *) using the ratio(s) defined in display-pixels-per-inch.
21242 ELEMENT ::=
21244 left-fringe - left fringe width in pixels
21245 right-fringe - right fringe width in pixels
21247 left-margin - left margin width in pixels
21248 right-margin - right margin width in pixels
21250 scroll-bar - scroll-bar area width in pixels
21252 Examples:
21254 Pixels corresponding to 5 inches:
21255 (5 . in)
21257 Total width of non-text areas on left side of window (if scroll-bar is on left):
21258 '(space :width (+ left-fringe left-margin scroll-bar))
21260 Align to first text column (in header line):
21261 '(space :align-to 0)
21263 Align to middle of text area minus half the width of variable `my-image'
21264 containing a loaded image:
21265 '(space :align-to (0.5 . (- text my-image)))
21267 Width of left margin minus width of 1 character in the default font:
21268 '(space :width (- left-margin 1))
21270 Width of left margin minus width of 2 characters in the current font:
21271 '(space :width (- left-margin (2 . width)))
21273 Center 1 character over left-margin (in header line):
21274 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21276 Different ways to express width of left fringe plus left margin minus one pixel:
21277 '(space :width (- (+ left-fringe left-margin) (1)))
21278 '(space :width (+ left-fringe left-margin (- (1))))
21279 '(space :width (+ left-fringe left-margin (-1)))
21283 #define NUMVAL(X) \
21284 ((INTEGERP (X) || FLOATP (X)) \
21285 ? XFLOATINT (X) \
21286 : - 1)
21289 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21290 struct font *font, int width_p, int *align_to)
21292 double pixels;
21294 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21295 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21297 if (NILP (prop))
21298 return OK_PIXELS (0);
21300 xassert (FRAME_LIVE_P (it->f));
21302 if (SYMBOLP (prop))
21304 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21306 char *unit = SSDATA (SYMBOL_NAME (prop));
21308 if (unit[0] == 'i' && unit[1] == 'n')
21309 pixels = 1.0;
21310 else if (unit[0] == 'm' && unit[1] == 'm')
21311 pixels = 25.4;
21312 else if (unit[0] == 'c' && unit[1] == 'm')
21313 pixels = 2.54;
21314 else
21315 pixels = 0;
21316 if (pixels > 0)
21318 double ppi;
21319 #ifdef HAVE_WINDOW_SYSTEM
21320 if (FRAME_WINDOW_P (it->f)
21321 && (ppi = (width_p
21322 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21323 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21324 ppi > 0))
21325 return OK_PIXELS (ppi / pixels);
21326 #endif
21328 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21329 || (CONSP (Vdisplay_pixels_per_inch)
21330 && (ppi = (width_p
21331 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21332 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21333 ppi > 0)))
21334 return OK_PIXELS (ppi / pixels);
21336 return 0;
21340 #ifdef HAVE_WINDOW_SYSTEM
21341 if (EQ (prop, Qheight))
21342 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21343 if (EQ (prop, Qwidth))
21344 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21345 #else
21346 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21347 return OK_PIXELS (1);
21348 #endif
21350 if (EQ (prop, Qtext))
21351 return OK_PIXELS (width_p
21352 ? window_box_width (it->w, TEXT_AREA)
21353 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21355 if (align_to && *align_to < 0)
21357 *res = 0;
21358 if (EQ (prop, Qleft))
21359 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21360 if (EQ (prop, Qright))
21361 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21362 if (EQ (prop, Qcenter))
21363 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21364 + window_box_width (it->w, TEXT_AREA) / 2);
21365 if (EQ (prop, Qleft_fringe))
21366 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21367 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21368 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21369 if (EQ (prop, Qright_fringe))
21370 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21371 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21372 : window_box_right_offset (it->w, TEXT_AREA));
21373 if (EQ (prop, Qleft_margin))
21374 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21375 if (EQ (prop, Qright_margin))
21376 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21377 if (EQ (prop, Qscroll_bar))
21378 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21380 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21381 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21382 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21383 : 0)));
21385 else
21387 if (EQ (prop, Qleft_fringe))
21388 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21389 if (EQ (prop, Qright_fringe))
21390 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21391 if (EQ (prop, Qleft_margin))
21392 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21393 if (EQ (prop, Qright_margin))
21394 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21395 if (EQ (prop, Qscroll_bar))
21396 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21399 prop = Fbuffer_local_value (prop, it->w->buffer);
21402 if (INTEGERP (prop) || FLOATP (prop))
21404 int base_unit = (width_p
21405 ? FRAME_COLUMN_WIDTH (it->f)
21406 : FRAME_LINE_HEIGHT (it->f));
21407 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21410 if (CONSP (prop))
21412 Lisp_Object car = XCAR (prop);
21413 Lisp_Object cdr = XCDR (prop);
21415 if (SYMBOLP (car))
21417 #ifdef HAVE_WINDOW_SYSTEM
21418 if (FRAME_WINDOW_P (it->f)
21419 && valid_image_p (prop))
21421 int id = lookup_image (it->f, prop);
21422 struct image *img = IMAGE_FROM_ID (it->f, id);
21424 return OK_PIXELS (width_p ? img->width : img->height);
21426 #ifdef HAVE_XWIDGETS
21427 if (FRAME_WINDOW_P (it->f) && valid_xwidget_p (prop))
21429 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
21430 return OK_PIXELS (width_p ? 100 : 100);
21432 #endif
21433 #endif
21434 if (EQ (car, Qplus) || EQ (car, Qminus))
21436 int first = 1;
21437 double px;
21439 pixels = 0;
21440 while (CONSP (cdr))
21442 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21443 font, width_p, align_to))
21444 return 0;
21445 if (first)
21446 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21447 else
21448 pixels += px;
21449 cdr = XCDR (cdr);
21451 if (EQ (car, Qminus))
21452 pixels = -pixels;
21453 return OK_PIXELS (pixels);
21456 car = Fbuffer_local_value (car, it->w->buffer);
21459 if (INTEGERP (car) || FLOATP (car))
21461 double fact;
21462 pixels = XFLOATINT (car);
21463 if (NILP (cdr))
21464 return OK_PIXELS (pixels);
21465 if (calc_pixel_width_or_height (&fact, it, cdr,
21466 font, width_p, align_to))
21467 return OK_PIXELS (pixels * fact);
21468 return 0;
21471 return 0;
21474 return 0;
21478 /***********************************************************************
21479 Glyph Display
21480 ***********************************************************************/
21482 #ifdef HAVE_WINDOW_SYSTEM
21484 #if GLYPH_DEBUG
21486 void
21487 dump_glyph_string (struct glyph_string *s)
21489 fprintf (stderr, "glyph string\n");
21490 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21491 s->x, s->y, s->width, s->height);
21492 fprintf (stderr, " ybase = %d\n", s->ybase);
21493 fprintf (stderr, " hl = %d\n", s->hl);
21494 fprintf (stderr, " left overhang = %d, right = %d\n",
21495 s->left_overhang, s->right_overhang);
21496 fprintf (stderr, " nchars = %d\n", s->nchars);
21497 fprintf (stderr, " extends to end of line = %d\n",
21498 s->extends_to_end_of_line_p);
21499 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21500 fprintf (stderr, " bg width = %d\n", s->background_width);
21503 #endif /* GLYPH_DEBUG */
21505 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21506 of XChar2b structures for S; it can't be allocated in
21507 init_glyph_string because it must be allocated via `alloca'. W
21508 is the window on which S is drawn. ROW and AREA are the glyph row
21509 and area within the row from which S is constructed. START is the
21510 index of the first glyph structure covered by S. HL is a
21511 face-override for drawing S. */
21513 #ifdef HAVE_NTGUI
21514 #define OPTIONAL_HDC(hdc) HDC hdc,
21515 #define DECLARE_HDC(hdc) HDC hdc;
21516 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21517 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21518 #endif
21520 #ifndef OPTIONAL_HDC
21521 #define OPTIONAL_HDC(hdc)
21522 #define DECLARE_HDC(hdc)
21523 #define ALLOCATE_HDC(hdc, f)
21524 #define RELEASE_HDC(hdc, f)
21525 #endif
21527 static void
21528 init_glyph_string (struct glyph_string *s,
21529 OPTIONAL_HDC (hdc)
21530 XChar2b *char2b, struct window *w, struct glyph_row *row,
21531 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21533 memset (s, 0, sizeof *s);
21534 s->w = w;
21535 s->f = XFRAME (w->frame);
21536 #ifdef HAVE_NTGUI
21537 s->hdc = hdc;
21538 #endif
21539 s->display = FRAME_X_DISPLAY (s->f);
21540 s->window = FRAME_X_WINDOW (s->f);
21541 s->char2b = char2b;
21542 s->hl = hl;
21543 s->row = row;
21544 s->area = area;
21545 s->first_glyph = row->glyphs[area] + start;
21546 s->height = row->height;
21547 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21548 s->ybase = s->y + row->ascent;
21552 /* Append the list of glyph strings with head H and tail T to the list
21553 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21555 static inline void
21556 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21557 struct glyph_string *h, struct glyph_string *t)
21559 if (h)
21561 if (*head)
21562 (*tail)->next = h;
21563 else
21564 *head = h;
21565 h->prev = *tail;
21566 *tail = t;
21571 /* Prepend the list of glyph strings with head H and tail T to the
21572 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21573 result. */
21575 static inline void
21576 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21577 struct glyph_string *h, struct glyph_string *t)
21579 if (h)
21581 if (*head)
21582 (*head)->prev = t;
21583 else
21584 *tail = t;
21585 t->next = *head;
21586 *head = h;
21591 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21592 Set *HEAD and *TAIL to the resulting list. */
21594 static inline void
21595 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21596 struct glyph_string *s)
21598 s->next = s->prev = NULL;
21599 append_glyph_string_lists (head, tail, s, s);
21603 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21604 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21605 make sure that X resources for the face returned are allocated.
21606 Value is a pointer to a realized face that is ready for display if
21607 DISPLAY_P is non-zero. */
21609 static inline struct face *
21610 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21611 XChar2b *char2b, int display_p)
21613 struct face *face = FACE_FROM_ID (f, face_id);
21615 if (face->font)
21617 unsigned code = face->font->driver->encode_char (face->font, c);
21619 if (code != FONT_INVALID_CODE)
21620 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21621 else
21622 STORE_XCHAR2B (char2b, 0, 0);
21625 /* Make sure X resources of the face are allocated. */
21626 #ifdef HAVE_X_WINDOWS
21627 if (display_p)
21628 #endif
21630 xassert (face != NULL);
21631 PREPARE_FACE_FOR_DISPLAY (f, face);
21634 return face;
21638 /* Get face and two-byte form of character glyph GLYPH on frame F.
21639 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21640 a pointer to a realized face that is ready for display. */
21642 static inline struct face *
21643 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21644 XChar2b *char2b, int *two_byte_p)
21646 struct face *face;
21648 xassert (glyph->type == CHAR_GLYPH);
21649 face = FACE_FROM_ID (f, glyph->face_id);
21651 if (two_byte_p)
21652 *two_byte_p = 0;
21654 if (face->font)
21656 unsigned code;
21658 if (CHAR_BYTE8_P (glyph->u.ch))
21659 code = CHAR_TO_BYTE8 (glyph->u.ch);
21660 else
21661 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21663 if (code != FONT_INVALID_CODE)
21664 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21665 else
21666 STORE_XCHAR2B (char2b, 0, 0);
21669 /* Make sure X resources of the face are allocated. */
21670 xassert (face != NULL);
21671 PREPARE_FACE_FOR_DISPLAY (f, face);
21672 return face;
21676 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21677 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21679 static inline int
21680 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21682 unsigned code;
21684 if (CHAR_BYTE8_P (c))
21685 code = CHAR_TO_BYTE8 (c);
21686 else
21687 code = font->driver->encode_char (font, c);
21689 if (code == FONT_INVALID_CODE)
21690 return 0;
21691 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21692 return 1;
21696 /* Fill glyph string S with composition components specified by S->cmp.
21698 BASE_FACE is the base face of the composition.
21699 S->cmp_from is the index of the first component for S.
21701 OVERLAPS non-zero means S should draw the foreground only, and use
21702 its physical height for clipping. See also draw_glyphs.
21704 Value is the index of a component not in S. */
21706 static int
21707 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21708 int overlaps)
21710 int i;
21711 /* For all glyphs of this composition, starting at the offset
21712 S->cmp_from, until we reach the end of the definition or encounter a
21713 glyph that requires the different face, add it to S. */
21714 struct face *face;
21716 xassert (s);
21718 s->for_overlaps = overlaps;
21719 s->face = NULL;
21720 s->font = NULL;
21721 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21723 int c = COMPOSITION_GLYPH (s->cmp, i);
21725 if (c != '\t')
21727 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21728 -1, Qnil);
21730 face = get_char_face_and_encoding (s->f, c, face_id,
21731 s->char2b + i, 1);
21732 if (face)
21734 if (! s->face)
21736 s->face = face;
21737 s->font = s->face->font;
21739 else if (s->face != face)
21740 break;
21743 ++s->nchars;
21745 s->cmp_to = i;
21747 /* All glyph strings for the same composition has the same width,
21748 i.e. the width set for the first component of the composition. */
21749 s->width = s->first_glyph->pixel_width;
21751 /* If the specified font could not be loaded, use the frame's
21752 default font, but record the fact that we couldn't load it in
21753 the glyph string so that we can draw rectangles for the
21754 characters of the glyph string. */
21755 if (s->font == NULL)
21757 s->font_not_found_p = 1;
21758 s->font = FRAME_FONT (s->f);
21761 /* Adjust base line for subscript/superscript text. */
21762 s->ybase += s->first_glyph->voffset;
21764 /* This glyph string must always be drawn with 16-bit functions. */
21765 s->two_byte_p = 1;
21767 return s->cmp_to;
21770 static int
21771 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21772 int start, int end, int overlaps)
21774 struct glyph *glyph, *last;
21775 Lisp_Object lgstring;
21776 int i;
21778 s->for_overlaps = overlaps;
21779 glyph = s->row->glyphs[s->area] + start;
21780 last = s->row->glyphs[s->area] + end;
21781 s->cmp_id = glyph->u.cmp.id;
21782 s->cmp_from = glyph->slice.cmp.from;
21783 s->cmp_to = glyph->slice.cmp.to + 1;
21784 s->face = FACE_FROM_ID (s->f, face_id);
21785 lgstring = composition_gstring_from_id (s->cmp_id);
21786 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21787 glyph++;
21788 while (glyph < last
21789 && glyph->u.cmp.automatic
21790 && glyph->u.cmp.id == s->cmp_id
21791 && s->cmp_to == glyph->slice.cmp.from)
21792 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21794 for (i = s->cmp_from; i < s->cmp_to; i++)
21796 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21797 unsigned code = LGLYPH_CODE (lglyph);
21799 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21801 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21802 return glyph - s->row->glyphs[s->area];
21806 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21807 See the comment of fill_glyph_string for arguments.
21808 Value is the index of the first glyph not in S. */
21811 static int
21812 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21813 int start, int end, int overlaps)
21815 struct glyph *glyph, *last;
21816 int voffset;
21818 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21819 s->for_overlaps = overlaps;
21820 glyph = s->row->glyphs[s->area] + start;
21821 last = s->row->glyphs[s->area] + end;
21822 voffset = glyph->voffset;
21823 s->face = FACE_FROM_ID (s->f, face_id);
21824 s->font = s->face->font;
21825 s->nchars = 1;
21826 s->width = glyph->pixel_width;
21827 glyph++;
21828 while (glyph < last
21829 && glyph->type == GLYPHLESS_GLYPH
21830 && glyph->voffset == voffset
21831 && glyph->face_id == face_id)
21833 s->nchars++;
21834 s->width += glyph->pixel_width;
21835 glyph++;
21837 s->ybase += voffset;
21838 return glyph - s->row->glyphs[s->area];
21842 /* Fill glyph string S from a sequence of character glyphs.
21844 FACE_ID is the face id of the string. START is the index of the
21845 first glyph to consider, END is the index of the last + 1.
21846 OVERLAPS non-zero means S should draw the foreground only, and use
21847 its physical height for clipping. See also draw_glyphs.
21849 Value is the index of the first glyph not in S. */
21851 static int
21852 fill_glyph_string (struct glyph_string *s, int face_id,
21853 int start, int end, int overlaps)
21855 struct glyph *glyph, *last;
21856 int voffset;
21857 int glyph_not_available_p;
21859 xassert (s->f == XFRAME (s->w->frame));
21860 xassert (s->nchars == 0);
21861 xassert (start >= 0 && end > start);
21863 s->for_overlaps = overlaps;
21864 glyph = s->row->glyphs[s->area] + start;
21865 last = s->row->glyphs[s->area] + end;
21866 voffset = glyph->voffset;
21867 s->padding_p = glyph->padding_p;
21868 glyph_not_available_p = glyph->glyph_not_available_p;
21870 while (glyph < last
21871 && glyph->type == CHAR_GLYPH
21872 && glyph->voffset == voffset
21873 /* Same face id implies same font, nowadays. */
21874 && glyph->face_id == face_id
21875 && glyph->glyph_not_available_p == glyph_not_available_p)
21877 int two_byte_p;
21879 s->face = get_glyph_face_and_encoding (s->f, glyph,
21880 s->char2b + s->nchars,
21881 &two_byte_p);
21882 s->two_byte_p = two_byte_p;
21883 ++s->nchars;
21884 xassert (s->nchars <= end - start);
21885 s->width += glyph->pixel_width;
21886 if (glyph++->padding_p != s->padding_p)
21887 break;
21890 s->font = s->face->font;
21892 /* If the specified font could not be loaded, use the frame's font,
21893 but record the fact that we couldn't load it in
21894 S->font_not_found_p so that we can draw rectangles for the
21895 characters of the glyph string. */
21896 if (s->font == NULL || glyph_not_available_p)
21898 s->font_not_found_p = 1;
21899 s->font = FRAME_FONT (s->f);
21902 /* Adjust base line for subscript/superscript text. */
21903 s->ybase += voffset;
21905 xassert (s->face && s->face->gc);
21906 return glyph - s->row->glyphs[s->area];
21910 /* Fill glyph string S from image glyph S->first_glyph. */
21912 static void
21913 fill_image_glyph_string (struct glyph_string *s)
21915 xassert (s->first_glyph->type == IMAGE_GLYPH);
21916 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21917 xassert (s->img);
21918 s->slice = s->first_glyph->slice.img;
21919 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21920 s->font = s->face->font;
21921 s->width = s->first_glyph->pixel_width;
21923 /* Adjust base line for subscript/superscript text. */
21924 s->ybase += s->first_glyph->voffset;
21927 #ifdef HAVE_XWIDGETS
21928 static void
21929 fill_xwidget_glyph_string (struct glyph_string *s)
21931 xassert (s->first_glyph->type == XWIDGET_GLYPH);
21932 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
21933 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21934 s->font = s->face->font;
21935 s->width = s->first_glyph->pixel_width;
21936 s->ybase += s->first_glyph->voffset;
21937 s->xwidget = s->first_glyph->u.xwidget;
21938 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
21940 #endif
21941 /* Fill glyph string S from a sequence of stretch glyphs.
21943 START is the index of the first glyph to consider,
21944 END is the index of the last + 1.
21946 Value is the index of the first glyph not in S. */
21948 static int
21949 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21951 struct glyph *glyph, *last;
21952 int voffset, face_id;
21954 xassert (s->first_glyph->type == STRETCH_GLYPH);
21956 glyph = s->row->glyphs[s->area] + start;
21957 last = s->row->glyphs[s->area] + end;
21958 face_id = glyph->face_id;
21959 s->face = FACE_FROM_ID (s->f, face_id);
21960 s->font = s->face->font;
21961 s->width = glyph->pixel_width;
21962 s->nchars = 1;
21963 voffset = glyph->voffset;
21965 for (++glyph;
21966 (glyph < last
21967 && glyph->type == STRETCH_GLYPH
21968 && glyph->voffset == voffset
21969 && glyph->face_id == face_id);
21970 ++glyph)
21971 s->width += glyph->pixel_width;
21973 /* Adjust base line for subscript/superscript text. */
21974 s->ybase += voffset;
21976 /* The case that face->gc == 0 is handled when drawing the glyph
21977 string by calling PREPARE_FACE_FOR_DISPLAY. */
21978 xassert (s->face);
21979 return glyph - s->row->glyphs[s->area];
21982 static struct font_metrics *
21983 get_per_char_metric (struct font *font, XChar2b *char2b)
21985 static struct font_metrics metrics;
21986 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21988 if (! font || code == FONT_INVALID_CODE)
21989 return NULL;
21990 font->driver->text_extents (font, &code, 1, &metrics);
21991 return &metrics;
21994 /* EXPORT for RIF:
21995 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21996 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21997 assumed to be zero. */
21999 void
22000 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22002 *left = *right = 0;
22004 if (glyph->type == CHAR_GLYPH)
22006 struct face *face;
22007 XChar2b char2b;
22008 struct font_metrics *pcm;
22010 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22011 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22013 if (pcm->rbearing > pcm->width)
22014 *right = pcm->rbearing - pcm->width;
22015 if (pcm->lbearing < 0)
22016 *left = -pcm->lbearing;
22019 else if (glyph->type == COMPOSITE_GLYPH)
22021 if (! glyph->u.cmp.automatic)
22023 struct composition *cmp = composition_table[glyph->u.cmp.id];
22025 if (cmp->rbearing > cmp->pixel_width)
22026 *right = cmp->rbearing - cmp->pixel_width;
22027 if (cmp->lbearing < 0)
22028 *left = - cmp->lbearing;
22030 else
22032 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22033 struct font_metrics metrics;
22035 composition_gstring_width (gstring, glyph->slice.cmp.from,
22036 glyph->slice.cmp.to + 1, &metrics);
22037 if (metrics.rbearing > metrics.width)
22038 *right = metrics.rbearing - metrics.width;
22039 if (metrics.lbearing < 0)
22040 *left = - metrics.lbearing;
22046 /* Return the index of the first glyph preceding glyph string S that
22047 is overwritten by S because of S's left overhang. Value is -1
22048 if no glyphs are overwritten. */
22050 static int
22051 left_overwritten (struct glyph_string *s)
22053 int k;
22055 if (s->left_overhang)
22057 int x = 0, i;
22058 struct glyph *glyphs = s->row->glyphs[s->area];
22059 int first = s->first_glyph - glyphs;
22061 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22062 x -= glyphs[i].pixel_width;
22064 k = i + 1;
22066 else
22067 k = -1;
22069 return k;
22073 /* Return the index of the first glyph preceding glyph string S that
22074 is overwriting S because of its right overhang. Value is -1 if no
22075 glyph in front of S overwrites S. */
22077 static int
22078 left_overwriting (struct glyph_string *s)
22080 int i, k, x;
22081 struct glyph *glyphs = s->row->glyphs[s->area];
22082 int first = s->first_glyph - glyphs;
22084 k = -1;
22085 x = 0;
22086 for (i = first - 1; i >= 0; --i)
22088 int left, right;
22089 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22090 if (x + right > 0)
22091 k = i;
22092 x -= glyphs[i].pixel_width;
22095 return k;
22099 /* Return the index of the last glyph following glyph string S that is
22100 overwritten by S because of S's right overhang. Value is -1 if
22101 no such glyph is found. */
22103 static int
22104 right_overwritten (struct glyph_string *s)
22106 int k = -1;
22108 if (s->right_overhang)
22110 int x = 0, i;
22111 struct glyph *glyphs = s->row->glyphs[s->area];
22112 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22113 int end = s->row->used[s->area];
22115 for (i = first; i < end && s->right_overhang > x; ++i)
22116 x += glyphs[i].pixel_width;
22118 k = i;
22121 return k;
22125 /* Return the index of the last glyph following glyph string S that
22126 overwrites S because of its left overhang. Value is negative
22127 if no such glyph is found. */
22129 static int
22130 right_overwriting (struct glyph_string *s)
22132 int i, k, x;
22133 int end = s->row->used[s->area];
22134 struct glyph *glyphs = s->row->glyphs[s->area];
22135 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22137 k = -1;
22138 x = 0;
22139 for (i = first; i < end; ++i)
22141 int left, right;
22142 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22143 if (x - left < 0)
22144 k = i;
22145 x += glyphs[i].pixel_width;
22148 return k;
22152 /* Set background width of glyph string S. START is the index of the
22153 first glyph following S. LAST_X is the right-most x-position + 1
22154 in the drawing area. */
22156 static inline void
22157 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22159 /* If the face of this glyph string has to be drawn to the end of
22160 the drawing area, set S->extends_to_end_of_line_p. */
22162 if (start == s->row->used[s->area]
22163 && s->area == TEXT_AREA
22164 && ((s->row->fill_line_p
22165 && (s->hl == DRAW_NORMAL_TEXT
22166 || s->hl == DRAW_IMAGE_RAISED
22167 || s->hl == DRAW_IMAGE_SUNKEN))
22168 || s->hl == DRAW_MOUSE_FACE))
22169 s->extends_to_end_of_line_p = 1;
22171 /* If S extends its face to the end of the line, set its
22172 background_width to the distance to the right edge of the drawing
22173 area. */
22174 if (s->extends_to_end_of_line_p)
22175 s->background_width = last_x - s->x + 1;
22176 else
22177 s->background_width = s->width;
22181 /* Compute overhangs and x-positions for glyph string S and its
22182 predecessors, or successors. X is the starting x-position for S.
22183 BACKWARD_P non-zero means process predecessors. */
22185 static void
22186 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22188 if (backward_p)
22190 while (s)
22192 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22193 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22194 x -= s->width;
22195 s->x = x;
22196 s = s->prev;
22199 else
22201 while (s)
22203 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22204 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22205 s->x = x;
22206 x += s->width;
22207 s = s->next;
22214 /* The following macros are only called from draw_glyphs below.
22215 They reference the following parameters of that function directly:
22216 `w', `row', `area', and `overlap_p'
22217 as well as the following local variables:
22218 `s', `f', and `hdc' (in W32) */
22220 #ifdef HAVE_NTGUI
22221 /* On W32, silently add local `hdc' variable to argument list of
22222 init_glyph_string. */
22223 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22224 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22225 #else
22226 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22227 init_glyph_string (s, char2b, w, row, area, start, hl)
22228 #endif
22230 /* Add a glyph string for a stretch glyph to the list of strings
22231 between HEAD and TAIL. START is the index of the stretch glyph in
22232 row area AREA of glyph row ROW. END is the index of the last glyph
22233 in that glyph row area. X is the current output position assigned
22234 to the new glyph string constructed. HL overrides that face of the
22235 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22236 is the right-most x-position of the drawing area. */
22238 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22239 and below -- keep them on one line. */
22240 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22241 do \
22243 s = (struct glyph_string *) alloca (sizeof *s); \
22244 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22245 START = fill_stretch_glyph_string (s, START, END); \
22246 append_glyph_string (&HEAD, &TAIL, s); \
22247 s->x = (X); \
22249 while (0)
22252 /* Add a glyph string for an image glyph to the list of strings
22253 between HEAD and TAIL. START is the index of the image glyph in
22254 row area AREA of glyph row ROW. END is the index of the last glyph
22255 in that glyph row area. X is the current output position assigned
22256 to the new glyph string constructed. HL overrides that face of the
22257 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22258 is the right-most x-position of the drawing area. */
22260 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22261 do \
22263 s = (struct glyph_string *) alloca (sizeof *s); \
22264 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22265 fill_image_glyph_string (s); \
22266 append_glyph_string (&HEAD, &TAIL, s); \
22267 ++START; \
22268 s->x = (X); \
22270 while (0)
22272 #ifdef HAVE_XWIDGETS
22273 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22274 do \
22276 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
22277 s = (struct glyph_string *) alloca (sizeof *s); \
22278 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22279 fill_xwidget_glyph_string (s); \
22280 append_glyph_string (&HEAD, &TAIL, s); \
22281 ++START; \
22282 s->x = (X); \
22284 while (0)
22285 #endif
22288 /* Add a glyph string for a sequence of character glyphs to the list
22289 of strings between HEAD and TAIL. START is the index of the first
22290 glyph in row area AREA of glyph row ROW that is part of the new
22291 glyph string. END is the index of the last glyph in that glyph row
22292 area. X is the current output position assigned to the new glyph
22293 string constructed. HL overrides that face of the glyph; e.g. it
22294 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22295 right-most x-position of the drawing area. */
22297 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22298 do \
22300 int face_id; \
22301 XChar2b *char2b; \
22303 face_id = (row)->glyphs[area][START].face_id; \
22305 s = (struct glyph_string *) alloca (sizeof *s); \
22306 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22307 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22308 append_glyph_string (&HEAD, &TAIL, s); \
22309 s->x = (X); \
22310 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22312 while (0)
22315 /* Add a glyph string for a composite sequence to the list of strings
22316 between HEAD and TAIL. START is the index of the first glyph in
22317 row area AREA of glyph row ROW that is part of the new glyph
22318 string. END is the index of the last glyph in that glyph row area.
22319 X is the current output position assigned to the new glyph string
22320 constructed. HL overrides that face of the glyph; e.g. it is
22321 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22322 x-position of the drawing area. */
22324 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22325 do { \
22326 int face_id = (row)->glyphs[area][START].face_id; \
22327 struct face *base_face = FACE_FROM_ID (f, face_id); \
22328 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22329 struct composition *cmp = composition_table[cmp_id]; \
22330 XChar2b *char2b; \
22331 struct glyph_string *first_s IF_LINT (= NULL); \
22332 int n; \
22334 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22336 /* Make glyph_strings for each glyph sequence that is drawable by \
22337 the same face, and append them to HEAD/TAIL. */ \
22338 for (n = 0; n < cmp->glyph_len;) \
22340 s = (struct glyph_string *) alloca (sizeof *s); \
22341 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22342 append_glyph_string (&(HEAD), &(TAIL), s); \
22343 s->cmp = cmp; \
22344 s->cmp_from = n; \
22345 s->x = (X); \
22346 if (n == 0) \
22347 first_s = s; \
22348 n = fill_composite_glyph_string (s, base_face, overlaps); \
22351 ++START; \
22352 s = first_s; \
22353 } while (0)
22356 /* Add a glyph string for a glyph-string sequence to the list of strings
22357 between HEAD and TAIL. */
22359 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22360 do { \
22361 int face_id; \
22362 XChar2b *char2b; \
22363 Lisp_Object gstring; \
22365 face_id = (row)->glyphs[area][START].face_id; \
22366 gstring = (composition_gstring_from_id \
22367 ((row)->glyphs[area][START].u.cmp.id)); \
22368 s = (struct glyph_string *) alloca (sizeof *s); \
22369 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22370 * LGSTRING_GLYPH_LEN (gstring)); \
22371 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22372 append_glyph_string (&(HEAD), &(TAIL), s); \
22373 s->x = (X); \
22374 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22375 } while (0)
22378 /* Add a glyph string for a sequence of glyphless character's glyphs
22379 to the list of strings between HEAD and TAIL. The meanings of
22380 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22382 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22383 do \
22385 int face_id; \
22387 face_id = (row)->glyphs[area][START].face_id; \
22389 s = (struct glyph_string *) alloca (sizeof *s); \
22390 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22391 append_glyph_string (&HEAD, &TAIL, s); \
22392 s->x = (X); \
22393 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22394 overlaps); \
22396 while (0)
22399 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22400 of AREA of glyph row ROW on window W between indices START and END.
22401 HL overrides the face for drawing glyph strings, e.g. it is
22402 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22403 x-positions of the drawing area.
22405 This is an ugly monster macro construct because we must use alloca
22406 to allocate glyph strings (because draw_glyphs can be called
22407 asynchronously). */
22409 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22410 do \
22412 HEAD = TAIL = NULL; \
22413 while (START < END) \
22415 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22416 switch (first_glyph->type) \
22418 case CHAR_GLYPH: \
22419 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22420 HL, X, LAST_X); \
22421 break; \
22423 case COMPOSITE_GLYPH: \
22424 if (first_glyph->u.cmp.automatic) \
22425 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22426 HL, X, LAST_X); \
22427 else \
22428 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22429 HL, X, LAST_X); \
22430 break; \
22432 case STRETCH_GLYPH: \
22433 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22434 HL, X, LAST_X); \
22435 break; \
22437 case IMAGE_GLYPH: \
22438 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22439 HL, X, LAST_X); \
22440 break; \
22441 case XWIDGET_GLYPH: \
22442 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
22443 HL, X, LAST_X); \
22444 break; \
22447 case GLYPHLESS_GLYPH: \
22448 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22449 HL, X, LAST_X); \
22450 break; \
22452 default: \
22453 abort (); \
22456 if (s) \
22458 set_glyph_string_background_width (s, START, LAST_X); \
22459 (X) += s->width; \
22462 } while (0)
22465 /* Draw glyphs between START and END in AREA of ROW on window W,
22466 starting at x-position X. X is relative to AREA in W. HL is a
22467 face-override with the following meaning:
22469 DRAW_NORMAL_TEXT draw normally
22470 DRAW_CURSOR draw in cursor face
22471 DRAW_MOUSE_FACE draw in mouse face.
22472 DRAW_INVERSE_VIDEO draw in mode line face
22473 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22474 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22476 If OVERLAPS is non-zero, draw only the foreground of characters and
22477 clip to the physical height of ROW. Non-zero value also defines
22478 the overlapping part to be drawn:
22480 OVERLAPS_PRED overlap with preceding rows
22481 OVERLAPS_SUCC overlap with succeeding rows
22482 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22483 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22485 Value is the x-position reached, relative to AREA of W. */
22487 static int
22488 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22489 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22490 enum draw_glyphs_face hl, int overlaps)
22492 struct glyph_string *head, *tail;
22493 struct glyph_string *s;
22494 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22495 int i, j, x_reached, last_x, area_left = 0;
22496 struct frame *f = XFRAME (WINDOW_FRAME (w));
22497 DECLARE_HDC (hdc);
22499 ALLOCATE_HDC (hdc, f);
22501 /* Let's rather be paranoid than getting a SEGV. */
22502 end = min (end, row->used[area]);
22503 start = max (0, start);
22504 start = min (end, start);
22506 /* Translate X to frame coordinates. Set last_x to the right
22507 end of the drawing area. */
22508 if (row->full_width_p)
22510 /* X is relative to the left edge of W, without scroll bars
22511 or fringes. */
22512 area_left = WINDOW_LEFT_EDGE_X (w);
22513 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22515 else
22517 area_left = window_box_left (w, area);
22518 last_x = area_left + window_box_width (w, area);
22520 x += area_left;
22522 /* Build a doubly-linked list of glyph_string structures between
22523 head and tail from what we have to draw. Note that the macro
22524 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22525 the reason we use a separate variable `i'. */
22526 i = start;
22527 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22528 if (tail)
22529 x_reached = tail->x + tail->background_width;
22530 else
22531 x_reached = x;
22533 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22534 the row, redraw some glyphs in front or following the glyph
22535 strings built above. */
22536 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22538 struct glyph_string *h, *t;
22539 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22540 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22541 int check_mouse_face = 0;
22542 int dummy_x = 0;
22544 /* If mouse highlighting is on, we may need to draw adjacent
22545 glyphs using mouse-face highlighting. */
22546 if (area == TEXT_AREA && row->mouse_face_p)
22548 struct glyph_row *mouse_beg_row, *mouse_end_row;
22550 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22551 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22553 if (row >= mouse_beg_row && row <= mouse_end_row)
22555 check_mouse_face = 1;
22556 mouse_beg_col = (row == mouse_beg_row)
22557 ? hlinfo->mouse_face_beg_col : 0;
22558 mouse_end_col = (row == mouse_end_row)
22559 ? hlinfo->mouse_face_end_col
22560 : row->used[TEXT_AREA];
22564 /* Compute overhangs for all glyph strings. */
22565 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22566 for (s = head; s; s = s->next)
22567 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22569 /* Prepend glyph strings for glyphs in front of the first glyph
22570 string that are overwritten because of the first glyph
22571 string's left overhang. The background of all strings
22572 prepended must be drawn because the first glyph string
22573 draws over it. */
22574 i = left_overwritten (head);
22575 if (i >= 0)
22577 enum draw_glyphs_face overlap_hl;
22579 /* If this row contains mouse highlighting, attempt to draw
22580 the overlapped glyphs with the correct highlight. This
22581 code fails if the overlap encompasses more than one glyph
22582 and mouse-highlight spans only some of these glyphs.
22583 However, making it work perfectly involves a lot more
22584 code, and I don't know if the pathological case occurs in
22585 practice, so we'll stick to this for now. --- cyd */
22586 if (check_mouse_face
22587 && mouse_beg_col < start && mouse_end_col > i)
22588 overlap_hl = DRAW_MOUSE_FACE;
22589 else
22590 overlap_hl = DRAW_NORMAL_TEXT;
22592 j = i;
22593 BUILD_GLYPH_STRINGS (j, start, h, t,
22594 overlap_hl, dummy_x, last_x);
22595 start = i;
22596 compute_overhangs_and_x (t, head->x, 1);
22597 prepend_glyph_string_lists (&head, &tail, h, t);
22598 clip_head = head;
22601 /* Prepend glyph strings for glyphs in front of the first glyph
22602 string that overwrite that glyph string because of their
22603 right overhang. For these strings, only the foreground must
22604 be drawn, because it draws over the glyph string at `head'.
22605 The background must not be drawn because this would overwrite
22606 right overhangs of preceding glyphs for which no glyph
22607 strings exist. */
22608 i = left_overwriting (head);
22609 if (i >= 0)
22611 enum draw_glyphs_face overlap_hl;
22613 if (check_mouse_face
22614 && mouse_beg_col < start && mouse_end_col > i)
22615 overlap_hl = DRAW_MOUSE_FACE;
22616 else
22617 overlap_hl = DRAW_NORMAL_TEXT;
22619 clip_head = head;
22620 BUILD_GLYPH_STRINGS (i, start, h, t,
22621 overlap_hl, dummy_x, last_x);
22622 for (s = h; s; s = s->next)
22623 s->background_filled_p = 1;
22624 compute_overhangs_and_x (t, head->x, 1);
22625 prepend_glyph_string_lists (&head, &tail, h, t);
22628 /* Append glyphs strings for glyphs following the last glyph
22629 string tail that are overwritten by tail. The background of
22630 these strings has to be drawn because tail's foreground draws
22631 over it. */
22632 i = right_overwritten (tail);
22633 if (i >= 0)
22635 enum draw_glyphs_face overlap_hl;
22637 if (check_mouse_face
22638 && mouse_beg_col < i && mouse_end_col > end)
22639 overlap_hl = DRAW_MOUSE_FACE;
22640 else
22641 overlap_hl = DRAW_NORMAL_TEXT;
22643 BUILD_GLYPH_STRINGS (end, i, h, t,
22644 overlap_hl, x, last_x);
22645 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22646 we don't have `end = i;' here. */
22647 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22648 append_glyph_string_lists (&head, &tail, h, t);
22649 clip_tail = tail;
22652 /* Append glyph strings for glyphs following the last glyph
22653 string tail that overwrite tail. The foreground of such
22654 glyphs has to be drawn because it writes into the background
22655 of tail. The background must not be drawn because it could
22656 paint over the foreground of following glyphs. */
22657 i = right_overwriting (tail);
22658 if (i >= 0)
22660 enum draw_glyphs_face overlap_hl;
22661 if (check_mouse_face
22662 && mouse_beg_col < i && mouse_end_col > end)
22663 overlap_hl = DRAW_MOUSE_FACE;
22664 else
22665 overlap_hl = DRAW_NORMAL_TEXT;
22667 clip_tail = tail;
22668 i++; /* We must include the Ith glyph. */
22669 BUILD_GLYPH_STRINGS (end, i, h, t,
22670 overlap_hl, x, last_x);
22671 for (s = h; s; s = s->next)
22672 s->background_filled_p = 1;
22673 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22674 append_glyph_string_lists (&head, &tail, h, t);
22676 if (clip_head || clip_tail)
22677 for (s = head; s; s = s->next)
22679 s->clip_head = clip_head;
22680 s->clip_tail = clip_tail;
22684 /* Draw all strings. */
22685 for (s = head; s; s = s->next)
22686 FRAME_RIF (f)->draw_glyph_string (s);
22688 #ifndef HAVE_NS
22689 /* When focus a sole frame and move horizontally, this sets on_p to 0
22690 causing a failure to erase prev cursor position. */
22691 if (area == TEXT_AREA
22692 && !row->full_width_p
22693 /* When drawing overlapping rows, only the glyph strings'
22694 foreground is drawn, which doesn't erase a cursor
22695 completely. */
22696 && !overlaps)
22698 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22699 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22700 : (tail ? tail->x + tail->background_width : x));
22701 x0 -= area_left;
22702 x1 -= area_left;
22704 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22705 row->y, MATRIX_ROW_BOTTOM_Y (row));
22707 #endif
22709 /* Value is the x-position up to which drawn, relative to AREA of W.
22710 This doesn't include parts drawn because of overhangs. */
22711 if (row->full_width_p)
22712 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22713 else
22714 x_reached -= area_left;
22716 RELEASE_HDC (hdc, f);
22718 return x_reached;
22721 /* Expand row matrix if too narrow. Don't expand if area
22722 is not present. */
22724 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22726 if (!fonts_changed_p \
22727 && (it->glyph_row->glyphs[area] \
22728 < it->glyph_row->glyphs[area + 1])) \
22730 it->w->ncols_scale_factor++; \
22731 fonts_changed_p = 1; \
22735 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22736 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22738 static inline void
22739 append_glyph (struct it *it)
22741 struct glyph *glyph;
22742 enum glyph_row_area area = it->area;
22744 xassert (it->glyph_row);
22745 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22747 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22748 if (glyph < it->glyph_row->glyphs[area + 1])
22750 /* If the glyph row is reversed, we need to prepend the glyph
22751 rather than append it. */
22752 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22754 struct glyph *g;
22756 /* Make room for the additional glyph. */
22757 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22758 g[1] = *g;
22759 glyph = it->glyph_row->glyphs[area];
22761 glyph->charpos = CHARPOS (it->position);
22762 glyph->object = it->object;
22763 if (it->pixel_width > 0)
22765 glyph->pixel_width = it->pixel_width;
22766 glyph->padding_p = 0;
22768 else
22770 /* Assure at least 1-pixel width. Otherwise, cursor can't
22771 be displayed correctly. */
22772 glyph->pixel_width = 1;
22773 glyph->padding_p = 1;
22775 glyph->ascent = it->ascent;
22776 glyph->descent = it->descent;
22777 glyph->voffset = it->voffset;
22778 glyph->type = CHAR_GLYPH;
22779 glyph->avoid_cursor_p = it->avoid_cursor_p;
22780 glyph->multibyte_p = it->multibyte_p;
22781 glyph->left_box_line_p = it->start_of_box_run_p;
22782 glyph->right_box_line_p = it->end_of_box_run_p;
22783 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22784 || it->phys_descent > it->descent);
22785 glyph->glyph_not_available_p = it->glyph_not_available_p;
22786 glyph->face_id = it->face_id;
22787 glyph->u.ch = it->char_to_display;
22788 glyph->slice.img = null_glyph_slice;
22789 glyph->font_type = FONT_TYPE_UNKNOWN;
22790 if (it->bidi_p)
22792 glyph->resolved_level = it->bidi_it.resolved_level;
22793 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22794 abort ();
22795 glyph->bidi_type = it->bidi_it.type;
22797 else
22799 glyph->resolved_level = 0;
22800 glyph->bidi_type = UNKNOWN_BT;
22802 ++it->glyph_row->used[area];
22804 else
22805 IT_EXPAND_MATRIX_WIDTH (it, area);
22808 /* Store one glyph for the composition IT->cmp_it.id in
22809 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22810 non-null. */
22812 static inline void
22813 append_composite_glyph (struct it *it)
22815 struct glyph *glyph;
22816 enum glyph_row_area area = it->area;
22818 xassert (it->glyph_row);
22820 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22821 if (glyph < it->glyph_row->glyphs[area + 1])
22823 /* If the glyph row is reversed, we need to prepend the glyph
22824 rather than append it. */
22825 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22827 struct glyph *g;
22829 /* Make room for the new glyph. */
22830 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22831 g[1] = *g;
22832 glyph = it->glyph_row->glyphs[it->area];
22834 glyph->charpos = it->cmp_it.charpos;
22835 glyph->object = it->object;
22836 glyph->pixel_width = it->pixel_width;
22837 glyph->ascent = it->ascent;
22838 glyph->descent = it->descent;
22839 glyph->voffset = it->voffset;
22840 glyph->type = COMPOSITE_GLYPH;
22841 if (it->cmp_it.ch < 0)
22843 glyph->u.cmp.automatic = 0;
22844 glyph->u.cmp.id = it->cmp_it.id;
22845 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22847 else
22849 glyph->u.cmp.automatic = 1;
22850 glyph->u.cmp.id = it->cmp_it.id;
22851 glyph->slice.cmp.from = it->cmp_it.from;
22852 glyph->slice.cmp.to = it->cmp_it.to - 1;
22854 glyph->avoid_cursor_p = it->avoid_cursor_p;
22855 glyph->multibyte_p = it->multibyte_p;
22856 glyph->left_box_line_p = it->start_of_box_run_p;
22857 glyph->right_box_line_p = it->end_of_box_run_p;
22858 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22859 || it->phys_descent > it->descent);
22860 glyph->padding_p = 0;
22861 glyph->glyph_not_available_p = 0;
22862 glyph->face_id = it->face_id;
22863 glyph->font_type = FONT_TYPE_UNKNOWN;
22864 if (it->bidi_p)
22866 glyph->resolved_level = it->bidi_it.resolved_level;
22867 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22868 abort ();
22869 glyph->bidi_type = it->bidi_it.type;
22871 ++it->glyph_row->used[area];
22873 else
22874 IT_EXPAND_MATRIX_WIDTH (it, area);
22878 /* Change IT->ascent and IT->height according to the setting of
22879 IT->voffset. */
22881 static inline void
22882 take_vertical_position_into_account (struct it *it)
22884 if (it->voffset)
22886 if (it->voffset < 0)
22887 /* Increase the ascent so that we can display the text higher
22888 in the line. */
22889 it->ascent -= it->voffset;
22890 else
22891 /* Increase the descent so that we can display the text lower
22892 in the line. */
22893 it->descent += it->voffset;
22898 /* Produce glyphs/get display metrics for the image IT is loaded with.
22899 See the description of struct display_iterator in dispextern.h for
22900 an overview of struct display_iterator. */
22902 static void
22903 produce_image_glyph (struct it *it)
22905 struct image *img;
22906 struct face *face;
22907 int glyph_ascent, crop;
22908 struct glyph_slice slice;
22910 xassert (it->what == IT_IMAGE);
22912 face = FACE_FROM_ID (it->f, it->face_id);
22913 xassert (face);
22914 /* Make sure X resources of the face is loaded. */
22915 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22917 if (it->image_id < 0)
22919 /* Fringe bitmap. */
22920 it->ascent = it->phys_ascent = 0;
22921 it->descent = it->phys_descent = 0;
22922 it->pixel_width = 0;
22923 it->nglyphs = 0;
22924 return;
22927 img = IMAGE_FROM_ID (it->f, it->image_id);
22928 xassert (img);
22929 /* Make sure X resources of the image is loaded. */
22930 prepare_image_for_display (it->f, img);
22932 slice.x = slice.y = 0;
22933 slice.width = img->width;
22934 slice.height = img->height;
22936 if (INTEGERP (it->slice.x))
22937 slice.x = XINT (it->slice.x);
22938 else if (FLOATP (it->slice.x))
22939 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22941 if (INTEGERP (it->slice.y))
22942 slice.y = XINT (it->slice.y);
22943 else if (FLOATP (it->slice.y))
22944 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22946 if (INTEGERP (it->slice.width))
22947 slice.width = XINT (it->slice.width);
22948 else if (FLOATP (it->slice.width))
22949 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22951 if (INTEGERP (it->slice.height))
22952 slice.height = XINT (it->slice.height);
22953 else if (FLOATP (it->slice.height))
22954 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22956 if (slice.x >= img->width)
22957 slice.x = img->width;
22958 if (slice.y >= img->height)
22959 slice.y = img->height;
22960 if (slice.x + slice.width >= img->width)
22961 slice.width = img->width - slice.x;
22962 if (slice.y + slice.height > img->height)
22963 slice.height = img->height - slice.y;
22965 if (slice.width == 0 || slice.height == 0)
22966 return;
22968 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22970 it->descent = slice.height - glyph_ascent;
22971 if (slice.y == 0)
22972 it->descent += img->vmargin;
22973 if (slice.y + slice.height == img->height)
22974 it->descent += img->vmargin;
22975 it->phys_descent = it->descent;
22977 it->pixel_width = slice.width;
22978 if (slice.x == 0)
22979 it->pixel_width += img->hmargin;
22980 if (slice.x + slice.width == img->width)
22981 it->pixel_width += img->hmargin;
22983 /* It's quite possible for images to have an ascent greater than
22984 their height, so don't get confused in that case. */
22985 if (it->descent < 0)
22986 it->descent = 0;
22988 it->nglyphs = 1;
22990 if (face->box != FACE_NO_BOX)
22992 if (face->box_line_width > 0)
22994 if (slice.y == 0)
22995 it->ascent += face->box_line_width;
22996 if (slice.y + slice.height == img->height)
22997 it->descent += face->box_line_width;
23000 if (it->start_of_box_run_p && slice.x == 0)
23001 it->pixel_width += eabs (face->box_line_width);
23002 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23003 it->pixel_width += eabs (face->box_line_width);
23006 take_vertical_position_into_account (it);
23008 /* Automatically crop wide image glyphs at right edge so we can
23009 draw the cursor on same display row. */
23010 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23011 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23013 it->pixel_width -= crop;
23014 slice.width -= crop;
23017 if (it->glyph_row)
23019 struct glyph *glyph;
23020 enum glyph_row_area area = it->area;
23022 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23023 if (glyph < it->glyph_row->glyphs[area + 1])
23025 glyph->charpos = CHARPOS (it->position);
23026 glyph->object = it->object;
23027 glyph->pixel_width = it->pixel_width;
23028 glyph->ascent = glyph_ascent;
23029 glyph->descent = it->descent;
23030 glyph->voffset = it->voffset;
23031 glyph->type = IMAGE_GLYPH;
23032 glyph->avoid_cursor_p = it->avoid_cursor_p;
23033 glyph->multibyte_p = it->multibyte_p;
23034 glyph->left_box_line_p = it->start_of_box_run_p;
23035 glyph->right_box_line_p = it->end_of_box_run_p;
23036 glyph->overlaps_vertically_p = 0;
23037 glyph->padding_p = 0;
23038 glyph->glyph_not_available_p = 0;
23039 glyph->face_id = it->face_id;
23040 glyph->u.img_id = img->id;
23041 glyph->slice.img = slice;
23042 glyph->font_type = FONT_TYPE_UNKNOWN;
23043 if (it->bidi_p)
23045 glyph->resolved_level = it->bidi_it.resolved_level;
23046 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23047 abort ();
23048 glyph->bidi_type = it->bidi_it.type;
23050 ++it->glyph_row->used[area];
23052 else
23053 IT_EXPAND_MATRIX_WIDTH (it, area);
23057 #ifdef HAVE_XWIDGETS
23058 static void
23059 produce_xwidget_glyph (struct it *it)
23061 struct xwidget* xw;
23062 struct face *face;
23063 int glyph_ascent, crop;
23064 printf("produce_xwidget_glyph:\n");
23065 xassert (it->what == IT_XWIDGET);
23067 face = FACE_FROM_ID (it->f, it->face_id);
23068 xassert (face);
23069 /* Make sure X resources of the face is loaded. */
23070 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23072 xw = it->xwidget;
23073 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
23074 it->descent = xw->height/2;
23075 it->phys_descent = it->descent;
23076 it->pixel_width = xw->width;
23077 /* It's quite possible for images to have an ascent greater than
23078 their height, so don't get confused in that case. */
23079 if (it->descent < 0)
23080 it->descent = 0;
23082 it->nglyphs = 1;
23084 if (face->box != FACE_NO_BOX)
23086 if (face->box_line_width > 0)
23088 it->ascent += face->box_line_width;
23089 it->descent += face->box_line_width;
23092 if (it->start_of_box_run_p)
23093 it->pixel_width += eabs (face->box_line_width);
23094 it->pixel_width += eabs (face->box_line_width);
23097 take_vertical_position_into_account (it);
23099 /* Automatically crop wide image glyphs at right edge so we can
23100 draw the cursor on same display row. */
23101 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23102 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23104 it->pixel_width -= crop;
23107 if (it->glyph_row)
23109 struct glyph *glyph;
23110 enum glyph_row_area area = it->area;
23112 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23113 if (glyph < it->glyph_row->glyphs[area + 1])
23115 glyph->charpos = CHARPOS (it->position);
23116 glyph->object = it->object;
23117 glyph->pixel_width = it->pixel_width;
23118 glyph->ascent = glyph_ascent;
23119 glyph->descent = it->descent;
23120 glyph->voffset = it->voffset;
23121 glyph->type = XWIDGET_GLYPH;
23123 glyph->multibyte_p = it->multibyte_p;
23124 glyph->left_box_line_p = it->start_of_box_run_p;
23125 glyph->right_box_line_p = it->end_of_box_run_p;
23126 glyph->overlaps_vertically_p = 0;
23127 glyph->padding_p = 0;
23128 glyph->glyph_not_available_p = 0;
23129 glyph->face_id = it->face_id;
23130 glyph->u.xwidget = it->xwidget;
23131 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
23132 glyph->font_type = FONT_TYPE_UNKNOWN;
23133 ++it->glyph_row->used[area];
23135 else
23136 IT_EXPAND_MATRIX_WIDTH (it, area);
23139 #endif
23141 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23142 of the glyph, WIDTH and HEIGHT are the width and height of the
23143 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23145 static void
23146 append_stretch_glyph (struct it *it, Lisp_Object object,
23147 int width, int height, int ascent)
23149 struct glyph *glyph;
23150 enum glyph_row_area area = it->area;
23152 xassert (ascent >= 0 && ascent <= height);
23154 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23155 if (glyph < it->glyph_row->glyphs[area + 1])
23157 /* If the glyph row is reversed, we need to prepend the glyph
23158 rather than append it. */
23159 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23161 struct glyph *g;
23163 /* Make room for the additional glyph. */
23164 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23165 g[1] = *g;
23166 glyph = it->glyph_row->glyphs[area];
23168 glyph->charpos = CHARPOS (it->position);
23169 glyph->object = object;
23170 glyph->pixel_width = width;
23171 glyph->ascent = ascent;
23172 glyph->descent = height - ascent;
23173 glyph->voffset = it->voffset;
23174 glyph->type = STRETCH_GLYPH;
23175 glyph->avoid_cursor_p = it->avoid_cursor_p;
23176 glyph->multibyte_p = it->multibyte_p;
23177 glyph->left_box_line_p = it->start_of_box_run_p;
23178 glyph->right_box_line_p = it->end_of_box_run_p;
23179 glyph->overlaps_vertically_p = 0;
23180 glyph->padding_p = 0;
23181 glyph->glyph_not_available_p = 0;
23182 glyph->face_id = it->face_id;
23183 glyph->u.stretch.ascent = ascent;
23184 glyph->u.stretch.height = height;
23185 glyph->slice.img = null_glyph_slice;
23186 glyph->font_type = FONT_TYPE_UNKNOWN;
23187 if (it->bidi_p)
23189 glyph->resolved_level = it->bidi_it.resolved_level;
23190 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23191 abort ();
23192 glyph->bidi_type = it->bidi_it.type;
23194 else
23196 glyph->resolved_level = 0;
23197 glyph->bidi_type = UNKNOWN_BT;
23199 ++it->glyph_row->used[area];
23201 else
23202 IT_EXPAND_MATRIX_WIDTH (it, area);
23206 /* Produce a stretch glyph for iterator IT. IT->object is the value
23207 of the glyph property displayed. The value must be a list
23208 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23209 being recognized:
23211 1. `:width WIDTH' specifies that the space should be WIDTH *
23212 canonical char width wide. WIDTH may be an integer or floating
23213 point number.
23215 2. `:relative-width FACTOR' specifies that the width of the stretch
23216 should be computed from the width of the first character having the
23217 `glyph' property, and should be FACTOR times that width.
23219 3. `:align-to HPOS' specifies that the space should be wide enough
23220 to reach HPOS, a value in canonical character units.
23222 Exactly one of the above pairs must be present.
23224 4. `:height HEIGHT' specifies that the height of the stretch produced
23225 should be HEIGHT, measured in canonical character units.
23227 5. `:relative-height FACTOR' specifies that the height of the
23228 stretch should be FACTOR times the height of the characters having
23229 the glyph property.
23231 Either none or exactly one of 4 or 5 must be present.
23233 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23234 of the stretch should be used for the ascent of the stretch.
23235 ASCENT must be in the range 0 <= ASCENT <= 100. */
23237 static void
23238 produce_stretch_glyph (struct it *it)
23240 /* (space :width WIDTH :height HEIGHT ...) */
23241 Lisp_Object prop, plist;
23242 int width = 0, height = 0, align_to = -1;
23243 int zero_width_ok_p = 0, zero_height_ok_p = 0;
23244 int ascent = 0;
23245 double tem;
23246 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23247 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
23249 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23251 /* List should start with `space'. */
23252 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23253 plist = XCDR (it->object);
23255 /* Compute the width of the stretch. */
23256 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23257 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23259 /* Absolute width `:width WIDTH' specified and valid. */
23260 zero_width_ok_p = 1;
23261 width = (int)tem;
23263 else if (prop = Fplist_get (plist, QCrelative_width),
23264 NUMVAL (prop) > 0)
23266 /* Relative width `:relative-width FACTOR' specified and valid.
23267 Compute the width of the characters having the `glyph'
23268 property. */
23269 struct it it2;
23270 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23272 it2 = *it;
23273 if (it->multibyte_p)
23274 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23275 else
23277 it2.c = it2.char_to_display = *p, it2.len = 1;
23278 if (! ASCII_CHAR_P (it2.c))
23279 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23282 it2.glyph_row = NULL;
23283 it2.what = IT_CHARACTER;
23284 x_produce_glyphs (&it2);
23285 width = NUMVAL (prop) * it2.pixel_width;
23287 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23288 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23290 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23291 align_to = (align_to < 0
23293 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23294 else if (align_to < 0)
23295 align_to = window_box_left_offset (it->w, TEXT_AREA);
23296 width = max (0, (int)tem + align_to - it->current_x);
23297 zero_width_ok_p = 1;
23299 else
23300 /* Nothing specified -> width defaults to canonical char width. */
23301 width = FRAME_COLUMN_WIDTH (it->f);
23303 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23304 width = 1;
23306 /* Compute height. */
23307 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23308 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23310 height = (int)tem;
23311 zero_height_ok_p = 1;
23313 else if (prop = Fplist_get (plist, QCrelative_height),
23314 NUMVAL (prop) > 0)
23315 height = FONT_HEIGHT (font) * NUMVAL (prop);
23316 else
23317 height = FONT_HEIGHT (font);
23319 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23320 height = 1;
23322 /* Compute percentage of height used for ascent. If
23323 `:ascent ASCENT' is present and valid, use that. Otherwise,
23324 derive the ascent from the font in use. */
23325 if (prop = Fplist_get (plist, QCascent),
23326 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23327 ascent = height * NUMVAL (prop) / 100.0;
23328 else if (!NILP (prop)
23329 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23330 ascent = min (max (0, (int)tem), height);
23331 else
23332 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23334 if (width > 0 && it->line_wrap != TRUNCATE
23335 && it->current_x + width > it->last_visible_x)
23336 width = it->last_visible_x - it->current_x - 1;
23338 if (width > 0 && height > 0 && it->glyph_row)
23340 Lisp_Object object = it->stack[it->sp - 1].string;
23341 if (!STRINGP (object))
23342 object = it->w->buffer;
23343 append_stretch_glyph (it, object, width, height, ascent);
23346 it->pixel_width = width;
23347 it->ascent = it->phys_ascent = ascent;
23348 it->descent = it->phys_descent = height - it->ascent;
23349 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23351 take_vertical_position_into_account (it);
23354 /* Calculate line-height and line-spacing properties.
23355 An integer value specifies explicit pixel value.
23356 A float value specifies relative value to current face height.
23357 A cons (float . face-name) specifies relative value to
23358 height of specified face font.
23360 Returns height in pixels, or nil. */
23363 static Lisp_Object
23364 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23365 int boff, int override)
23367 Lisp_Object face_name = Qnil;
23368 int ascent, descent, height;
23370 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23371 return val;
23373 if (CONSP (val))
23375 face_name = XCAR (val);
23376 val = XCDR (val);
23377 if (!NUMBERP (val))
23378 val = make_number (1);
23379 if (NILP (face_name))
23381 height = it->ascent + it->descent;
23382 goto scale;
23386 if (NILP (face_name))
23388 font = FRAME_FONT (it->f);
23389 boff = FRAME_BASELINE_OFFSET (it->f);
23391 else if (EQ (face_name, Qt))
23393 override = 0;
23395 else
23397 int face_id;
23398 struct face *face;
23400 face_id = lookup_named_face (it->f, face_name, 0);
23401 if (face_id < 0)
23402 return make_number (-1);
23404 face = FACE_FROM_ID (it->f, face_id);
23405 font = face->font;
23406 if (font == NULL)
23407 return make_number (-1);
23408 boff = font->baseline_offset;
23409 if (font->vertical_centering)
23410 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23413 ascent = FONT_BASE (font) + boff;
23414 descent = FONT_DESCENT (font) - boff;
23416 if (override)
23418 it->override_ascent = ascent;
23419 it->override_descent = descent;
23420 it->override_boff = boff;
23423 height = ascent + descent;
23425 scale:
23426 if (FLOATP (val))
23427 height = (int)(XFLOAT_DATA (val) * height);
23428 else if (INTEGERP (val))
23429 height *= XINT (val);
23431 return make_number (height);
23435 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23436 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23437 and only if this is for a character for which no font was found.
23439 If the display method (it->glyphless_method) is
23440 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23441 length of the acronym or the hexadecimal string, UPPER_XOFF and
23442 UPPER_YOFF are pixel offsets for the upper part of the string,
23443 LOWER_XOFF and LOWER_YOFF are for the lower part.
23445 For the other display methods, LEN through LOWER_YOFF are zero. */
23447 static void
23448 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23449 short upper_xoff, short upper_yoff,
23450 short lower_xoff, short lower_yoff)
23452 struct glyph *glyph;
23453 enum glyph_row_area area = it->area;
23455 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23456 if (glyph < it->glyph_row->glyphs[area + 1])
23458 /* If the glyph row is reversed, we need to prepend the glyph
23459 rather than append it. */
23460 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23462 struct glyph *g;
23464 /* Make room for the additional glyph. */
23465 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23466 g[1] = *g;
23467 glyph = it->glyph_row->glyphs[area];
23469 glyph->charpos = CHARPOS (it->position);
23470 glyph->object = it->object;
23471 glyph->pixel_width = it->pixel_width;
23472 glyph->ascent = it->ascent;
23473 glyph->descent = it->descent;
23474 glyph->voffset = it->voffset;
23475 glyph->type = GLYPHLESS_GLYPH;
23476 glyph->u.glyphless.method = it->glyphless_method;
23477 glyph->u.glyphless.for_no_font = for_no_font;
23478 glyph->u.glyphless.len = len;
23479 glyph->u.glyphless.ch = it->c;
23480 glyph->slice.glyphless.upper_xoff = upper_xoff;
23481 glyph->slice.glyphless.upper_yoff = upper_yoff;
23482 glyph->slice.glyphless.lower_xoff = lower_xoff;
23483 glyph->slice.glyphless.lower_yoff = lower_yoff;
23484 glyph->avoid_cursor_p = it->avoid_cursor_p;
23485 glyph->multibyte_p = it->multibyte_p;
23486 glyph->left_box_line_p = it->start_of_box_run_p;
23487 glyph->right_box_line_p = it->end_of_box_run_p;
23488 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23489 || it->phys_descent > it->descent);
23490 glyph->padding_p = 0;
23491 glyph->glyph_not_available_p = 0;
23492 glyph->face_id = face_id;
23493 glyph->font_type = FONT_TYPE_UNKNOWN;
23494 if (it->bidi_p)
23496 glyph->resolved_level = it->bidi_it.resolved_level;
23497 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23498 abort ();
23499 glyph->bidi_type = it->bidi_it.type;
23501 ++it->glyph_row->used[area];
23503 else
23504 IT_EXPAND_MATRIX_WIDTH (it, area);
23508 /* Produce a glyph for a glyphless character for iterator IT.
23509 IT->glyphless_method specifies which method to use for displaying
23510 the character. See the description of enum
23511 glyphless_display_method in dispextern.h for the detail.
23513 FOR_NO_FONT is nonzero if and only if this is for a character for
23514 which no font was found. ACRONYM, if non-nil, is an acronym string
23515 for the character. */
23517 static void
23518 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23520 int face_id;
23521 struct face *face;
23522 struct font *font;
23523 int base_width, base_height, width, height;
23524 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23525 int len;
23527 /* Get the metrics of the base font. We always refer to the current
23528 ASCII face. */
23529 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23530 font = face->font ? face->font : FRAME_FONT (it->f);
23531 it->ascent = FONT_BASE (font) + font->baseline_offset;
23532 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23533 base_height = it->ascent + it->descent;
23534 base_width = font->average_width;
23536 /* Get a face ID for the glyph by utilizing a cache (the same way as
23537 done for `escape-glyph' in get_next_display_element). */
23538 if (it->f == last_glyphless_glyph_frame
23539 && it->face_id == last_glyphless_glyph_face_id)
23541 face_id = last_glyphless_glyph_merged_face_id;
23543 else
23545 /* Merge the `glyphless-char' face into the current face. */
23546 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23547 last_glyphless_glyph_frame = it->f;
23548 last_glyphless_glyph_face_id = it->face_id;
23549 last_glyphless_glyph_merged_face_id = face_id;
23552 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23554 it->pixel_width = THIN_SPACE_WIDTH;
23555 len = 0;
23556 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23558 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23560 width = CHAR_WIDTH (it->c);
23561 if (width == 0)
23562 width = 1;
23563 else if (width > 4)
23564 width = 4;
23565 it->pixel_width = base_width * width;
23566 len = 0;
23567 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23569 else
23571 char buf[7];
23572 const char *str;
23573 unsigned int code[6];
23574 int upper_len;
23575 int ascent, descent;
23576 struct font_metrics metrics_upper, metrics_lower;
23578 face = FACE_FROM_ID (it->f, face_id);
23579 font = face->font ? face->font : FRAME_FONT (it->f);
23580 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23582 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23584 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23585 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23586 if (CONSP (acronym))
23587 acronym = XCAR (acronym);
23588 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23590 else
23592 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23593 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23594 str = buf;
23596 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23597 code[len] = font->driver->encode_char (font, str[len]);
23598 upper_len = (len + 1) / 2;
23599 font->driver->text_extents (font, code, upper_len,
23600 &metrics_upper);
23601 font->driver->text_extents (font, code + upper_len, len - upper_len,
23602 &metrics_lower);
23606 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23607 width = max (metrics_upper.width, metrics_lower.width) + 4;
23608 upper_xoff = upper_yoff = 2; /* the typical case */
23609 if (base_width >= width)
23611 /* Align the upper to the left, the lower to the right. */
23612 it->pixel_width = base_width;
23613 lower_xoff = base_width - 2 - metrics_lower.width;
23615 else
23617 /* Center the shorter one. */
23618 it->pixel_width = width;
23619 if (metrics_upper.width >= metrics_lower.width)
23620 lower_xoff = (width - metrics_lower.width) / 2;
23621 else
23623 /* FIXME: This code doesn't look right. It formerly was
23624 missing the "lower_xoff = 0;", which couldn't have
23625 been right since it left lower_xoff uninitialized. */
23626 lower_xoff = 0;
23627 upper_xoff = (width - metrics_upper.width) / 2;
23631 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23632 top, bottom, and between upper and lower strings. */
23633 height = (metrics_upper.ascent + metrics_upper.descent
23634 + metrics_lower.ascent + metrics_lower.descent) + 5;
23635 /* Center vertically.
23636 H:base_height, D:base_descent
23637 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23639 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23640 descent = D - H/2 + h/2;
23641 lower_yoff = descent - 2 - ld;
23642 upper_yoff = lower_yoff - la - 1 - ud; */
23643 ascent = - (it->descent - (base_height + height + 1) / 2);
23644 descent = it->descent - (base_height - height) / 2;
23645 lower_yoff = descent - 2 - metrics_lower.descent;
23646 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23647 - metrics_upper.descent);
23648 /* Don't make the height shorter than the base height. */
23649 if (height > base_height)
23651 it->ascent = ascent;
23652 it->descent = descent;
23656 it->phys_ascent = it->ascent;
23657 it->phys_descent = it->descent;
23658 if (it->glyph_row)
23659 append_glyphless_glyph (it, face_id, for_no_font, len,
23660 upper_xoff, upper_yoff,
23661 lower_xoff, lower_yoff);
23662 it->nglyphs = 1;
23663 take_vertical_position_into_account (it);
23667 /* RIF:
23668 Produce glyphs/get display metrics for the display element IT is
23669 loaded with. See the description of struct it in dispextern.h
23670 for an overview of struct it. */
23672 void
23673 x_produce_glyphs (struct it *it)
23675 int extra_line_spacing = it->extra_line_spacing;
23677 it->glyph_not_available_p = 0;
23679 if (it->what == IT_CHARACTER)
23681 XChar2b char2b;
23682 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23683 struct font *font = face->font;
23684 struct font_metrics *pcm = NULL;
23685 int boff; /* baseline offset */
23687 if (font == NULL)
23689 /* When no suitable font is found, display this character by
23690 the method specified in the first extra slot of
23691 Vglyphless_char_display. */
23692 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23694 xassert (it->what == IT_GLYPHLESS);
23695 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23696 goto done;
23699 boff = font->baseline_offset;
23700 if (font->vertical_centering)
23701 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23703 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23705 int stretched_p;
23707 it->nglyphs = 1;
23709 if (it->override_ascent >= 0)
23711 it->ascent = it->override_ascent;
23712 it->descent = it->override_descent;
23713 boff = it->override_boff;
23715 else
23717 it->ascent = FONT_BASE (font) + boff;
23718 it->descent = FONT_DESCENT (font) - boff;
23721 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23723 pcm = get_per_char_metric (font, &char2b);
23724 if (pcm->width == 0
23725 && pcm->rbearing == 0 && pcm->lbearing == 0)
23726 pcm = NULL;
23729 if (pcm)
23731 it->phys_ascent = pcm->ascent + boff;
23732 it->phys_descent = pcm->descent - boff;
23733 it->pixel_width = pcm->width;
23735 else
23737 it->glyph_not_available_p = 1;
23738 it->phys_ascent = it->ascent;
23739 it->phys_descent = it->descent;
23740 it->pixel_width = font->space_width;
23743 if (it->constrain_row_ascent_descent_p)
23745 if (it->descent > it->max_descent)
23747 it->ascent += it->descent - it->max_descent;
23748 it->descent = it->max_descent;
23750 if (it->ascent > it->max_ascent)
23752 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23753 it->ascent = it->max_ascent;
23755 it->phys_ascent = min (it->phys_ascent, it->ascent);
23756 it->phys_descent = min (it->phys_descent, it->descent);
23757 extra_line_spacing = 0;
23760 /* If this is a space inside a region of text with
23761 `space-width' property, change its width. */
23762 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23763 if (stretched_p)
23764 it->pixel_width *= XFLOATINT (it->space_width);
23766 /* If face has a box, add the box thickness to the character
23767 height. If character has a box line to the left and/or
23768 right, add the box line width to the character's width. */
23769 if (face->box != FACE_NO_BOX)
23771 int thick = face->box_line_width;
23773 if (thick > 0)
23775 it->ascent += thick;
23776 it->descent += thick;
23778 else
23779 thick = -thick;
23781 if (it->start_of_box_run_p)
23782 it->pixel_width += thick;
23783 if (it->end_of_box_run_p)
23784 it->pixel_width += thick;
23787 /* If face has an overline, add the height of the overline
23788 (1 pixel) and a 1 pixel margin to the character height. */
23789 if (face->overline_p)
23790 it->ascent += overline_margin;
23792 if (it->constrain_row_ascent_descent_p)
23794 if (it->ascent > it->max_ascent)
23795 it->ascent = it->max_ascent;
23796 if (it->descent > it->max_descent)
23797 it->descent = it->max_descent;
23800 take_vertical_position_into_account (it);
23802 /* If we have to actually produce glyphs, do it. */
23803 if (it->glyph_row)
23805 if (stretched_p)
23807 /* Translate a space with a `space-width' property
23808 into a stretch glyph. */
23809 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23810 / FONT_HEIGHT (font));
23811 append_stretch_glyph (it, it->object, it->pixel_width,
23812 it->ascent + it->descent, ascent);
23814 else
23815 append_glyph (it);
23817 /* If characters with lbearing or rbearing are displayed
23818 in this line, record that fact in a flag of the
23819 glyph row. This is used to optimize X output code. */
23820 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23821 it->glyph_row->contains_overlapping_glyphs_p = 1;
23823 if (! stretched_p && it->pixel_width == 0)
23824 /* We assure that all visible glyphs have at least 1-pixel
23825 width. */
23826 it->pixel_width = 1;
23828 else if (it->char_to_display == '\n')
23830 /* A newline has no width, but we need the height of the
23831 line. But if previous part of the line sets a height,
23832 don't increase that height */
23834 Lisp_Object height;
23835 Lisp_Object total_height = Qnil;
23837 it->override_ascent = -1;
23838 it->pixel_width = 0;
23839 it->nglyphs = 0;
23841 height = get_it_property (it, Qline_height);
23842 /* Split (line-height total-height) list */
23843 if (CONSP (height)
23844 && CONSP (XCDR (height))
23845 && NILP (XCDR (XCDR (height))))
23847 total_height = XCAR (XCDR (height));
23848 height = XCAR (height);
23850 height = calc_line_height_property (it, height, font, boff, 1);
23852 if (it->override_ascent >= 0)
23854 it->ascent = it->override_ascent;
23855 it->descent = it->override_descent;
23856 boff = it->override_boff;
23858 else
23860 it->ascent = FONT_BASE (font) + boff;
23861 it->descent = FONT_DESCENT (font) - boff;
23864 if (EQ (height, Qt))
23866 if (it->descent > it->max_descent)
23868 it->ascent += it->descent - it->max_descent;
23869 it->descent = it->max_descent;
23871 if (it->ascent > it->max_ascent)
23873 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23874 it->ascent = it->max_ascent;
23876 it->phys_ascent = min (it->phys_ascent, it->ascent);
23877 it->phys_descent = min (it->phys_descent, it->descent);
23878 it->constrain_row_ascent_descent_p = 1;
23879 extra_line_spacing = 0;
23881 else
23883 Lisp_Object spacing;
23885 it->phys_ascent = it->ascent;
23886 it->phys_descent = it->descent;
23888 if ((it->max_ascent > 0 || it->max_descent > 0)
23889 && face->box != FACE_NO_BOX
23890 && face->box_line_width > 0)
23892 it->ascent += face->box_line_width;
23893 it->descent += face->box_line_width;
23895 if (!NILP (height)
23896 && XINT (height) > it->ascent + it->descent)
23897 it->ascent = XINT (height) - it->descent;
23899 if (!NILP (total_height))
23900 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23901 else
23903 spacing = get_it_property (it, Qline_spacing);
23904 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23906 if (INTEGERP (spacing))
23908 extra_line_spacing = XINT (spacing);
23909 if (!NILP (total_height))
23910 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23914 else /* i.e. (it->char_to_display == '\t') */
23916 if (font->space_width > 0)
23918 int tab_width = it->tab_width * font->space_width;
23919 int x = it->current_x + it->continuation_lines_width;
23920 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23922 /* If the distance from the current position to the next tab
23923 stop is less than a space character width, use the
23924 tab stop after that. */
23925 if (next_tab_x - x < font->space_width)
23926 next_tab_x += tab_width;
23928 it->pixel_width = next_tab_x - x;
23929 it->nglyphs = 1;
23930 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23931 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23933 if (it->glyph_row)
23935 append_stretch_glyph (it, it->object, it->pixel_width,
23936 it->ascent + it->descent, it->ascent);
23939 else
23941 it->pixel_width = 0;
23942 it->nglyphs = 1;
23946 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23948 /* A static composition.
23950 Note: A composition is represented as one glyph in the
23951 glyph matrix. There are no padding glyphs.
23953 Important note: pixel_width, ascent, and descent are the
23954 values of what is drawn by draw_glyphs (i.e. the values of
23955 the overall glyphs composed). */
23956 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23957 int boff; /* baseline offset */
23958 struct composition *cmp = composition_table[it->cmp_it.id];
23959 int glyph_len = cmp->glyph_len;
23960 struct font *font = face->font;
23962 it->nglyphs = 1;
23964 /* If we have not yet calculated pixel size data of glyphs of
23965 the composition for the current face font, calculate them
23966 now. Theoretically, we have to check all fonts for the
23967 glyphs, but that requires much time and memory space. So,
23968 here we check only the font of the first glyph. This may
23969 lead to incorrect display, but it's very rare, and C-l
23970 (recenter-top-bottom) can correct the display anyway. */
23971 if (! cmp->font || cmp->font != font)
23973 /* Ascent and descent of the font of the first character
23974 of this composition (adjusted by baseline offset).
23975 Ascent and descent of overall glyphs should not be less
23976 than these, respectively. */
23977 int font_ascent, font_descent, font_height;
23978 /* Bounding box of the overall glyphs. */
23979 int leftmost, rightmost, lowest, highest;
23980 int lbearing, rbearing;
23981 int i, width, ascent, descent;
23982 int left_padded = 0, right_padded = 0;
23983 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23984 XChar2b char2b;
23985 struct font_metrics *pcm;
23986 int font_not_found_p;
23987 EMACS_INT pos;
23989 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23990 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23991 break;
23992 if (glyph_len < cmp->glyph_len)
23993 right_padded = 1;
23994 for (i = 0; i < glyph_len; i++)
23996 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23997 break;
23998 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24000 if (i > 0)
24001 left_padded = 1;
24003 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24004 : IT_CHARPOS (*it));
24005 /* If no suitable font is found, use the default font. */
24006 font_not_found_p = font == NULL;
24007 if (font_not_found_p)
24009 face = face->ascii_face;
24010 font = face->font;
24012 boff = font->baseline_offset;
24013 if (font->vertical_centering)
24014 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24015 font_ascent = FONT_BASE (font) + boff;
24016 font_descent = FONT_DESCENT (font) - boff;
24017 font_height = FONT_HEIGHT (font);
24019 cmp->font = (void *) font;
24021 pcm = NULL;
24022 if (! font_not_found_p)
24024 get_char_face_and_encoding (it->f, c, it->face_id,
24025 &char2b, 0);
24026 pcm = get_per_char_metric (font, &char2b);
24029 /* Initialize the bounding box. */
24030 if (pcm)
24032 width = pcm->width;
24033 ascent = pcm->ascent;
24034 descent = pcm->descent;
24035 lbearing = pcm->lbearing;
24036 rbearing = pcm->rbearing;
24038 else
24040 width = font->space_width;
24041 ascent = FONT_BASE (font);
24042 descent = FONT_DESCENT (font);
24043 lbearing = 0;
24044 rbearing = width;
24047 rightmost = width;
24048 leftmost = 0;
24049 lowest = - descent + boff;
24050 highest = ascent + boff;
24052 if (! font_not_found_p
24053 && font->default_ascent
24054 && CHAR_TABLE_P (Vuse_default_ascent)
24055 && !NILP (Faref (Vuse_default_ascent,
24056 make_number (it->char_to_display))))
24057 highest = font->default_ascent + boff;
24059 /* Draw the first glyph at the normal position. It may be
24060 shifted to right later if some other glyphs are drawn
24061 at the left. */
24062 cmp->offsets[i * 2] = 0;
24063 cmp->offsets[i * 2 + 1] = boff;
24064 cmp->lbearing = lbearing;
24065 cmp->rbearing = rbearing;
24067 /* Set cmp->offsets for the remaining glyphs. */
24068 for (i++; i < glyph_len; i++)
24070 int left, right, btm, top;
24071 int ch = COMPOSITION_GLYPH (cmp, i);
24072 int face_id;
24073 struct face *this_face;
24075 if (ch == '\t')
24076 ch = ' ';
24077 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24078 this_face = FACE_FROM_ID (it->f, face_id);
24079 font = this_face->font;
24081 if (font == NULL)
24082 pcm = NULL;
24083 else
24085 get_char_face_and_encoding (it->f, ch, face_id,
24086 &char2b, 0);
24087 pcm = get_per_char_metric (font, &char2b);
24089 if (! pcm)
24090 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24091 else
24093 width = pcm->width;
24094 ascent = pcm->ascent;
24095 descent = pcm->descent;
24096 lbearing = pcm->lbearing;
24097 rbearing = pcm->rbearing;
24098 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24100 /* Relative composition with or without
24101 alternate chars. */
24102 left = (leftmost + rightmost - width) / 2;
24103 btm = - descent + boff;
24104 if (font->relative_compose
24105 && (! CHAR_TABLE_P (Vignore_relative_composition)
24106 || NILP (Faref (Vignore_relative_composition,
24107 make_number (ch)))))
24110 if (- descent >= font->relative_compose)
24111 /* One extra pixel between two glyphs. */
24112 btm = highest + 1;
24113 else if (ascent <= 0)
24114 /* One extra pixel between two glyphs. */
24115 btm = lowest - 1 - ascent - descent;
24118 else
24120 /* A composition rule is specified by an integer
24121 value that encodes global and new reference
24122 points (GREF and NREF). GREF and NREF are
24123 specified by numbers as below:
24125 0---1---2 -- ascent
24129 9--10--11 -- center
24131 ---3---4---5--- baseline
24133 6---7---8 -- descent
24135 int rule = COMPOSITION_RULE (cmp, i);
24136 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24138 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24139 grefx = gref % 3, nrefx = nref % 3;
24140 grefy = gref / 3, nrefy = nref / 3;
24141 if (xoff)
24142 xoff = font_height * (xoff - 128) / 256;
24143 if (yoff)
24144 yoff = font_height * (yoff - 128) / 256;
24146 left = (leftmost
24147 + grefx * (rightmost - leftmost) / 2
24148 - nrefx * width / 2
24149 + xoff);
24151 btm = ((grefy == 0 ? highest
24152 : grefy == 1 ? 0
24153 : grefy == 2 ? lowest
24154 : (highest + lowest) / 2)
24155 - (nrefy == 0 ? ascent + descent
24156 : nrefy == 1 ? descent - boff
24157 : nrefy == 2 ? 0
24158 : (ascent + descent) / 2)
24159 + yoff);
24162 cmp->offsets[i * 2] = left;
24163 cmp->offsets[i * 2 + 1] = btm + descent;
24165 /* Update the bounding box of the overall glyphs. */
24166 if (width > 0)
24168 right = left + width;
24169 if (left < leftmost)
24170 leftmost = left;
24171 if (right > rightmost)
24172 rightmost = right;
24174 top = btm + descent + ascent;
24175 if (top > highest)
24176 highest = top;
24177 if (btm < lowest)
24178 lowest = btm;
24180 if (cmp->lbearing > left + lbearing)
24181 cmp->lbearing = left + lbearing;
24182 if (cmp->rbearing < left + rbearing)
24183 cmp->rbearing = left + rbearing;
24187 /* If there are glyphs whose x-offsets are negative,
24188 shift all glyphs to the right and make all x-offsets
24189 non-negative. */
24190 if (leftmost < 0)
24192 for (i = 0; i < cmp->glyph_len; i++)
24193 cmp->offsets[i * 2] -= leftmost;
24194 rightmost -= leftmost;
24195 cmp->lbearing -= leftmost;
24196 cmp->rbearing -= leftmost;
24199 if (left_padded && cmp->lbearing < 0)
24201 for (i = 0; i < cmp->glyph_len; i++)
24202 cmp->offsets[i * 2] -= cmp->lbearing;
24203 rightmost -= cmp->lbearing;
24204 cmp->rbearing -= cmp->lbearing;
24205 cmp->lbearing = 0;
24207 if (right_padded && rightmost < cmp->rbearing)
24209 rightmost = cmp->rbearing;
24212 cmp->pixel_width = rightmost;
24213 cmp->ascent = highest;
24214 cmp->descent = - lowest;
24215 if (cmp->ascent < font_ascent)
24216 cmp->ascent = font_ascent;
24217 if (cmp->descent < font_descent)
24218 cmp->descent = font_descent;
24221 if (it->glyph_row
24222 && (cmp->lbearing < 0
24223 || cmp->rbearing > cmp->pixel_width))
24224 it->glyph_row->contains_overlapping_glyphs_p = 1;
24226 it->pixel_width = cmp->pixel_width;
24227 it->ascent = it->phys_ascent = cmp->ascent;
24228 it->descent = it->phys_descent = cmp->descent;
24229 if (face->box != FACE_NO_BOX)
24231 int thick = face->box_line_width;
24233 if (thick > 0)
24235 it->ascent += thick;
24236 it->descent += thick;
24238 else
24239 thick = - thick;
24241 if (it->start_of_box_run_p)
24242 it->pixel_width += thick;
24243 if (it->end_of_box_run_p)
24244 it->pixel_width += thick;
24247 /* If face has an overline, add the height of the overline
24248 (1 pixel) and a 1 pixel margin to the character height. */
24249 if (face->overline_p)
24250 it->ascent += overline_margin;
24252 take_vertical_position_into_account (it);
24253 if (it->ascent < 0)
24254 it->ascent = 0;
24255 if (it->descent < 0)
24256 it->descent = 0;
24258 if (it->glyph_row)
24259 append_composite_glyph (it);
24261 else if (it->what == IT_COMPOSITION)
24263 /* A dynamic (automatic) composition. */
24264 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24265 Lisp_Object gstring;
24266 struct font_metrics metrics;
24268 gstring = composition_gstring_from_id (it->cmp_it.id);
24269 it->pixel_width
24270 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24271 &metrics);
24272 if (it->glyph_row
24273 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24274 it->glyph_row->contains_overlapping_glyphs_p = 1;
24275 it->ascent = it->phys_ascent = metrics.ascent;
24276 it->descent = it->phys_descent = metrics.descent;
24277 if (face->box != FACE_NO_BOX)
24279 int thick = face->box_line_width;
24281 if (thick > 0)
24283 it->ascent += thick;
24284 it->descent += thick;
24286 else
24287 thick = - thick;
24289 if (it->start_of_box_run_p)
24290 it->pixel_width += thick;
24291 if (it->end_of_box_run_p)
24292 it->pixel_width += thick;
24294 /* If face has an overline, add the height of the overline
24295 (1 pixel) and a 1 pixel margin to the character height. */
24296 if (face->overline_p)
24297 it->ascent += overline_margin;
24298 take_vertical_position_into_account (it);
24299 if (it->ascent < 0)
24300 it->ascent = 0;
24301 if (it->descent < 0)
24302 it->descent = 0;
24304 if (it->glyph_row)
24305 append_composite_glyph (it);
24307 else if (it->what == IT_GLYPHLESS)
24308 produce_glyphless_glyph (it, 0, Qnil);
24309 else if (it->what == IT_IMAGE)
24310 produce_image_glyph (it);
24311 else if (it->what == IT_STRETCH)
24312 produce_stretch_glyph (it);
24313 #ifdef HAVE_XWIDGETS
24314 else if (it->what == IT_XWIDGET)
24315 produce_xwidget_glyph (it);
24316 #endif
24317 done:
24318 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24319 because this isn't true for images with `:ascent 100'. */
24320 xassert (it->ascent >= 0 && it->descent >= 0);
24321 if (it->area == TEXT_AREA)
24322 it->current_x += it->pixel_width;
24324 if (extra_line_spacing > 0)
24326 it->descent += extra_line_spacing;
24327 if (extra_line_spacing > it->max_extra_line_spacing)
24328 it->max_extra_line_spacing = extra_line_spacing;
24331 it->max_ascent = max (it->max_ascent, it->ascent);
24332 it->max_descent = max (it->max_descent, it->descent);
24333 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24334 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24337 /* EXPORT for RIF:
24338 Output LEN glyphs starting at START at the nominal cursor position.
24339 Advance the nominal cursor over the text. The global variable
24340 updated_window contains the window being updated, updated_row is
24341 the glyph row being updated, and updated_area is the area of that
24342 row being updated. */
24344 void
24345 x_write_glyphs (struct glyph *start, int len)
24347 int x, hpos;
24349 xassert (updated_window && updated_row);
24350 BLOCK_INPUT;
24352 /* Write glyphs. */
24354 hpos = start - updated_row->glyphs[updated_area];
24355 x = draw_glyphs (updated_window, output_cursor.x,
24356 updated_row, updated_area,
24357 hpos, hpos + len,
24358 DRAW_NORMAL_TEXT, 0);
24360 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24361 if (updated_area == TEXT_AREA
24362 && updated_window->phys_cursor_on_p
24363 && updated_window->phys_cursor.vpos == output_cursor.vpos
24364 && updated_window->phys_cursor.hpos >= hpos
24365 && updated_window->phys_cursor.hpos < hpos + len)
24366 updated_window->phys_cursor_on_p = 0;
24368 UNBLOCK_INPUT;
24370 /* Advance the output cursor. */
24371 output_cursor.hpos += len;
24372 output_cursor.x = x;
24376 /* EXPORT for RIF:
24377 Insert LEN glyphs from START at the nominal cursor position. */
24379 void
24380 x_insert_glyphs (struct glyph *start, int len)
24382 struct frame *f;
24383 struct window *w;
24384 int line_height, shift_by_width, shifted_region_width;
24385 struct glyph_row *row;
24386 struct glyph *glyph;
24387 int frame_x, frame_y;
24388 EMACS_INT hpos;
24390 xassert (updated_window && updated_row);
24391 BLOCK_INPUT;
24392 w = updated_window;
24393 f = XFRAME (WINDOW_FRAME (w));
24395 /* Get the height of the line we are in. */
24396 row = updated_row;
24397 line_height = row->height;
24399 /* Get the width of the glyphs to insert. */
24400 shift_by_width = 0;
24401 for (glyph = start; glyph < start + len; ++glyph)
24402 shift_by_width += glyph->pixel_width;
24404 /* Get the width of the region to shift right. */
24405 shifted_region_width = (window_box_width (w, updated_area)
24406 - output_cursor.x
24407 - shift_by_width);
24409 /* Shift right. */
24410 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24411 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24413 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24414 line_height, shift_by_width);
24416 /* Write the glyphs. */
24417 hpos = start - row->glyphs[updated_area];
24418 draw_glyphs (w, output_cursor.x, row, updated_area,
24419 hpos, hpos + len,
24420 DRAW_NORMAL_TEXT, 0);
24422 /* Advance the output cursor. */
24423 output_cursor.hpos += len;
24424 output_cursor.x += shift_by_width;
24425 UNBLOCK_INPUT;
24429 /* EXPORT for RIF:
24430 Erase the current text line from the nominal cursor position
24431 (inclusive) to pixel column TO_X (exclusive). The idea is that
24432 everything from TO_X onward is already erased.
24434 TO_X is a pixel position relative to updated_area of
24435 updated_window. TO_X == -1 means clear to the end of this area. */
24437 void
24438 x_clear_end_of_line (int to_x)
24440 struct frame *f;
24441 struct window *w = updated_window;
24442 int max_x, min_y, max_y;
24443 int from_x, from_y, to_y;
24445 xassert (updated_window && updated_row);
24446 f = XFRAME (w->frame);
24448 if (updated_row->full_width_p)
24449 max_x = WINDOW_TOTAL_WIDTH (w);
24450 else
24451 max_x = window_box_width (w, updated_area);
24452 max_y = window_text_bottom_y (w);
24454 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24455 of window. For TO_X > 0, truncate to end of drawing area. */
24456 if (to_x == 0)
24457 return;
24458 else if (to_x < 0)
24459 to_x = max_x;
24460 else
24461 to_x = min (to_x, max_x);
24463 to_y = min (max_y, output_cursor.y + updated_row->height);
24465 /* Notice if the cursor will be cleared by this operation. */
24466 if (!updated_row->full_width_p)
24467 notice_overwritten_cursor (w, updated_area,
24468 output_cursor.x, -1,
24469 updated_row->y,
24470 MATRIX_ROW_BOTTOM_Y (updated_row));
24472 from_x = output_cursor.x;
24474 /* Translate to frame coordinates. */
24475 if (updated_row->full_width_p)
24477 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24478 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24480 else
24482 int area_left = window_box_left (w, updated_area);
24483 from_x += area_left;
24484 to_x += area_left;
24487 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24488 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24489 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24491 /* Prevent inadvertently clearing to end of the X window. */
24492 if (to_x > from_x && to_y > from_y)
24494 BLOCK_INPUT;
24495 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24496 to_x - from_x, to_y - from_y);
24497 UNBLOCK_INPUT;
24501 #endif /* HAVE_WINDOW_SYSTEM */
24505 /***********************************************************************
24506 Cursor types
24507 ***********************************************************************/
24509 /* Value is the internal representation of the specified cursor type
24510 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24511 of the bar cursor. */
24513 static enum text_cursor_kinds
24514 get_specified_cursor_type (Lisp_Object arg, int *width)
24516 enum text_cursor_kinds type;
24518 if (NILP (arg))
24519 return NO_CURSOR;
24521 if (EQ (arg, Qbox))
24522 return FILLED_BOX_CURSOR;
24524 if (EQ (arg, Qhollow))
24525 return HOLLOW_BOX_CURSOR;
24527 if (EQ (arg, Qbar))
24529 *width = 2;
24530 return BAR_CURSOR;
24533 if (CONSP (arg)
24534 && EQ (XCAR (arg), Qbar)
24535 && INTEGERP (XCDR (arg))
24536 && XINT (XCDR (arg)) >= 0)
24538 *width = XINT (XCDR (arg));
24539 return BAR_CURSOR;
24542 if (EQ (arg, Qhbar))
24544 *width = 2;
24545 return HBAR_CURSOR;
24548 if (CONSP (arg)
24549 && EQ (XCAR (arg), Qhbar)
24550 && INTEGERP (XCDR (arg))
24551 && XINT (XCDR (arg)) >= 0)
24553 *width = XINT (XCDR (arg));
24554 return HBAR_CURSOR;
24557 /* Treat anything unknown as "hollow box cursor".
24558 It was bad to signal an error; people have trouble fixing
24559 .Xdefaults with Emacs, when it has something bad in it. */
24560 type = HOLLOW_BOX_CURSOR;
24562 return type;
24565 /* Set the default cursor types for specified frame. */
24566 void
24567 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24569 int width = 1;
24570 Lisp_Object tem;
24572 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24573 FRAME_CURSOR_WIDTH (f) = width;
24575 /* By default, set up the blink-off state depending on the on-state. */
24577 tem = Fassoc (arg, Vblink_cursor_alist);
24578 if (!NILP (tem))
24580 FRAME_BLINK_OFF_CURSOR (f)
24581 = get_specified_cursor_type (XCDR (tem), &width);
24582 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24584 else
24585 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24589 #ifdef HAVE_WINDOW_SYSTEM
24591 /* Return the cursor we want to be displayed in window W. Return
24592 width of bar/hbar cursor through WIDTH arg. Return with
24593 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24594 (i.e. if the `system caret' should track this cursor).
24596 In a mini-buffer window, we want the cursor only to appear if we
24597 are reading input from this window. For the selected window, we
24598 want the cursor type given by the frame parameter or buffer local
24599 setting of cursor-type. If explicitly marked off, draw no cursor.
24600 In all other cases, we want a hollow box cursor. */
24602 static enum text_cursor_kinds
24603 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24604 int *active_cursor)
24606 struct frame *f = XFRAME (w->frame);
24607 struct buffer *b = XBUFFER (w->buffer);
24608 int cursor_type = DEFAULT_CURSOR;
24609 Lisp_Object alt_cursor;
24610 int non_selected = 0;
24612 *active_cursor = 1;
24614 /* Echo area */
24615 if (cursor_in_echo_area
24616 && FRAME_HAS_MINIBUF_P (f)
24617 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24619 if (w == XWINDOW (echo_area_window))
24621 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24623 *width = FRAME_CURSOR_WIDTH (f);
24624 return FRAME_DESIRED_CURSOR (f);
24626 else
24627 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24630 *active_cursor = 0;
24631 non_selected = 1;
24634 /* Detect a nonselected window or nonselected frame. */
24635 else if (w != XWINDOW (f->selected_window)
24636 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24638 *active_cursor = 0;
24640 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24641 return NO_CURSOR;
24643 non_selected = 1;
24646 /* Never display a cursor in a window in which cursor-type is nil. */
24647 if (NILP (BVAR (b, cursor_type)))
24648 return NO_CURSOR;
24650 /* Get the normal cursor type for this window. */
24651 if (EQ (BVAR (b, cursor_type), Qt))
24653 cursor_type = FRAME_DESIRED_CURSOR (f);
24654 *width = FRAME_CURSOR_WIDTH (f);
24656 else
24657 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24659 /* Use cursor-in-non-selected-windows instead
24660 for non-selected window or frame. */
24661 if (non_selected)
24663 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24664 if (!EQ (Qt, alt_cursor))
24665 return get_specified_cursor_type (alt_cursor, width);
24666 /* t means modify the normal cursor type. */
24667 if (cursor_type == FILLED_BOX_CURSOR)
24668 cursor_type = HOLLOW_BOX_CURSOR;
24669 else if (cursor_type == BAR_CURSOR && *width > 1)
24670 --*width;
24671 return cursor_type;
24674 /* Use normal cursor if not blinked off. */
24675 if (!w->cursor_off_p)
24678 #ifdef HAVE_XWIDGETS
24679 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
24680 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
24681 return NO_CURSOR;
24683 #endif
24684 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24686 if (cursor_type == FILLED_BOX_CURSOR)
24688 /* Using a block cursor on large images can be very annoying.
24689 So use a hollow cursor for "large" images.
24690 If image is not transparent (no mask), also use hollow cursor. */
24691 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24692 if (img != NULL && IMAGEP (img->spec))
24694 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24695 where N = size of default frame font size.
24696 This should cover most of the "tiny" icons people may use. */
24697 if (!img->mask
24698 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24699 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24700 cursor_type = HOLLOW_BOX_CURSOR;
24703 else if (cursor_type != NO_CURSOR)
24705 /* Display current only supports BOX and HOLLOW cursors for images.
24706 So for now, unconditionally use a HOLLOW cursor when cursor is
24707 not a solid box cursor. */
24708 cursor_type = HOLLOW_BOX_CURSOR;
24711 return cursor_type;
24714 /* Cursor is blinked off, so determine how to "toggle" it. */
24716 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24717 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24718 return get_specified_cursor_type (XCDR (alt_cursor), width);
24720 /* Then see if frame has specified a specific blink off cursor type. */
24721 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24723 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24724 return FRAME_BLINK_OFF_CURSOR (f);
24727 #if 0
24728 /* Some people liked having a permanently visible blinking cursor,
24729 while others had very strong opinions against it. So it was
24730 decided to remove it. KFS 2003-09-03 */
24732 /* Finally perform built-in cursor blinking:
24733 filled box <-> hollow box
24734 wide [h]bar <-> narrow [h]bar
24735 narrow [h]bar <-> no cursor
24736 other type <-> no cursor */
24738 if (cursor_type == FILLED_BOX_CURSOR)
24739 return HOLLOW_BOX_CURSOR;
24741 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24743 *width = 1;
24744 return cursor_type;
24746 #endif
24748 return NO_CURSOR;
24752 /* Notice when the text cursor of window W has been completely
24753 overwritten by a drawing operation that outputs glyphs in AREA
24754 starting at X0 and ending at X1 in the line starting at Y0 and
24755 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24756 the rest of the line after X0 has been written. Y coordinates
24757 are window-relative. */
24759 static void
24760 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24761 int x0, int x1, int y0, int y1)
24763 int cx0, cx1, cy0, cy1;
24764 struct glyph_row *row;
24766 if (!w->phys_cursor_on_p)
24767 return;
24768 if (area != TEXT_AREA)
24769 return;
24771 if (w->phys_cursor.vpos < 0
24772 || w->phys_cursor.vpos >= w->current_matrix->nrows
24773 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24774 !(row->enabled_p && row->displays_text_p)))
24775 return;
24777 if (row->cursor_in_fringe_p)
24779 row->cursor_in_fringe_p = 0;
24780 draw_fringe_bitmap (w, row, row->reversed_p);
24781 w->phys_cursor_on_p = 0;
24782 return;
24785 cx0 = w->phys_cursor.x;
24786 cx1 = cx0 + w->phys_cursor_width;
24787 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24788 return;
24790 /* The cursor image will be completely removed from the
24791 screen if the output area intersects the cursor area in
24792 y-direction. When we draw in [y0 y1[, and some part of
24793 the cursor is at y < y0, that part must have been drawn
24794 before. When scrolling, the cursor is erased before
24795 actually scrolling, so we don't come here. When not
24796 scrolling, the rows above the old cursor row must have
24797 changed, and in this case these rows must have written
24798 over the cursor image.
24800 Likewise if part of the cursor is below y1, with the
24801 exception of the cursor being in the first blank row at
24802 the buffer and window end because update_text_area
24803 doesn't draw that row. (Except when it does, but
24804 that's handled in update_text_area.) */
24806 cy0 = w->phys_cursor.y;
24807 cy1 = cy0 + w->phys_cursor_height;
24808 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24809 return;
24811 w->phys_cursor_on_p = 0;
24814 #endif /* HAVE_WINDOW_SYSTEM */
24817 /************************************************************************
24818 Mouse Face
24819 ************************************************************************/
24821 #ifdef HAVE_WINDOW_SYSTEM
24823 /* EXPORT for RIF:
24824 Fix the display of area AREA of overlapping row ROW in window W
24825 with respect to the overlapping part OVERLAPS. */
24827 void
24828 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24829 enum glyph_row_area area, int overlaps)
24831 int i, x;
24833 BLOCK_INPUT;
24835 x = 0;
24836 for (i = 0; i < row->used[area];)
24838 if (row->glyphs[area][i].overlaps_vertically_p)
24840 int start = i, start_x = x;
24844 x += row->glyphs[area][i].pixel_width;
24845 ++i;
24847 while (i < row->used[area]
24848 && row->glyphs[area][i].overlaps_vertically_p);
24850 draw_glyphs (w, start_x, row, area,
24851 start, i,
24852 DRAW_NORMAL_TEXT, overlaps);
24854 else
24856 x += row->glyphs[area][i].pixel_width;
24857 ++i;
24861 UNBLOCK_INPUT;
24865 /* EXPORT:
24866 Draw the cursor glyph of window W in glyph row ROW. See the
24867 comment of draw_glyphs for the meaning of HL. */
24869 void
24870 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24871 enum draw_glyphs_face hl)
24873 /* If cursor hpos is out of bounds, don't draw garbage. This can
24874 happen in mini-buffer windows when switching between echo area
24875 glyphs and mini-buffer. */
24876 if ((row->reversed_p
24877 ? (w->phys_cursor.hpos >= 0)
24878 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24880 int on_p = w->phys_cursor_on_p;
24881 int x1;
24882 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24883 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24884 hl, 0);
24885 w->phys_cursor_on_p = on_p;
24887 if (hl == DRAW_CURSOR)
24888 w->phys_cursor_width = x1 - w->phys_cursor.x;
24889 /* When we erase the cursor, and ROW is overlapped by other
24890 rows, make sure that these overlapping parts of other rows
24891 are redrawn. */
24892 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24894 w->phys_cursor_width = x1 - w->phys_cursor.x;
24896 if (row > w->current_matrix->rows
24897 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24898 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24899 OVERLAPS_ERASED_CURSOR);
24901 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24902 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24903 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24904 OVERLAPS_ERASED_CURSOR);
24910 /* EXPORT:
24911 Erase the image of a cursor of window W from the screen. */
24913 void
24914 erase_phys_cursor (struct window *w)
24916 struct frame *f = XFRAME (w->frame);
24917 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24918 int hpos = w->phys_cursor.hpos;
24919 int vpos = w->phys_cursor.vpos;
24920 int mouse_face_here_p = 0;
24921 struct glyph_matrix *active_glyphs = w->current_matrix;
24922 struct glyph_row *cursor_row;
24923 struct glyph *cursor_glyph;
24924 enum draw_glyphs_face hl;
24926 /* No cursor displayed or row invalidated => nothing to do on the
24927 screen. */
24928 if (w->phys_cursor_type == NO_CURSOR)
24929 goto mark_cursor_off;
24931 /* VPOS >= active_glyphs->nrows means that window has been resized.
24932 Don't bother to erase the cursor. */
24933 if (vpos >= active_glyphs->nrows)
24934 goto mark_cursor_off;
24936 /* If row containing cursor is marked invalid, there is nothing we
24937 can do. */
24938 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24939 if (!cursor_row->enabled_p)
24940 goto mark_cursor_off;
24942 /* If line spacing is > 0, old cursor may only be partially visible in
24943 window after split-window. So adjust visible height. */
24944 cursor_row->visible_height = min (cursor_row->visible_height,
24945 window_text_bottom_y (w) - cursor_row->y);
24947 /* If row is completely invisible, don't attempt to delete a cursor which
24948 isn't there. This can happen if cursor is at top of a window, and
24949 we switch to a buffer with a header line in that window. */
24950 if (cursor_row->visible_height <= 0)
24951 goto mark_cursor_off;
24953 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24954 if (cursor_row->cursor_in_fringe_p)
24956 cursor_row->cursor_in_fringe_p = 0;
24957 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24958 goto mark_cursor_off;
24961 /* This can happen when the new row is shorter than the old one.
24962 In this case, either draw_glyphs or clear_end_of_line
24963 should have cleared the cursor. Note that we wouldn't be
24964 able to erase the cursor in this case because we don't have a
24965 cursor glyph at hand. */
24966 if ((cursor_row->reversed_p
24967 ? (w->phys_cursor.hpos < 0)
24968 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24969 goto mark_cursor_off;
24971 /* If the cursor is in the mouse face area, redisplay that when
24972 we clear the cursor. */
24973 if (! NILP (hlinfo->mouse_face_window)
24974 && coords_in_mouse_face_p (w, hpos, vpos)
24975 /* Don't redraw the cursor's spot in mouse face if it is at the
24976 end of a line (on a newline). The cursor appears there, but
24977 mouse highlighting does not. */
24978 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24979 mouse_face_here_p = 1;
24981 /* Maybe clear the display under the cursor. */
24982 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24984 int x, y, left_x;
24985 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24986 int width;
24988 cursor_glyph = get_phys_cursor_glyph (w);
24989 if (cursor_glyph == NULL)
24990 goto mark_cursor_off;
24992 width = cursor_glyph->pixel_width;
24993 left_x = window_box_left_offset (w, TEXT_AREA);
24994 x = w->phys_cursor.x;
24995 if (x < left_x)
24996 width -= left_x - x;
24997 width = min (width, window_box_width (w, TEXT_AREA) - x);
24998 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24999 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25001 if (width > 0)
25002 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25005 /* Erase the cursor by redrawing the character underneath it. */
25006 if (mouse_face_here_p)
25007 hl = DRAW_MOUSE_FACE;
25008 else
25009 hl = DRAW_NORMAL_TEXT;
25010 draw_phys_cursor_glyph (w, cursor_row, hl);
25012 mark_cursor_off:
25013 w->phys_cursor_on_p = 0;
25014 w->phys_cursor_type = NO_CURSOR;
25018 /* EXPORT:
25019 Display or clear cursor of window W. If ON is zero, clear the
25020 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25021 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25023 void
25024 display_and_set_cursor (struct window *w, int on,
25025 int hpos, int vpos, int x, int y)
25027 struct frame *f = XFRAME (w->frame);
25028 int new_cursor_type;
25029 int new_cursor_width;
25030 int active_cursor;
25031 struct glyph_row *glyph_row;
25032 struct glyph *glyph;
25034 /* This is pointless on invisible frames, and dangerous on garbaged
25035 windows and frames; in the latter case, the frame or window may
25036 be in the midst of changing its size, and x and y may be off the
25037 window. */
25038 if (! FRAME_VISIBLE_P (f)
25039 || FRAME_GARBAGED_P (f)
25040 || vpos >= w->current_matrix->nrows
25041 || hpos >= w->current_matrix->matrix_w)
25042 return;
25044 /* If cursor is off and we want it off, return quickly. */
25045 if (!on && !w->phys_cursor_on_p)
25046 return;
25048 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25049 /* If cursor row is not enabled, we don't really know where to
25050 display the cursor. */
25051 if (!glyph_row->enabled_p)
25053 w->phys_cursor_on_p = 0;
25054 return;
25057 glyph = NULL;
25058 if (!glyph_row->exact_window_width_line_p
25059 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25060 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25062 xassert (interrupt_input_blocked);
25064 /* Set new_cursor_type to the cursor we want to be displayed. */
25065 new_cursor_type = get_window_cursor_type (w, glyph,
25066 &new_cursor_width, &active_cursor);
25068 /* If cursor is currently being shown and we don't want it to be or
25069 it is in the wrong place, or the cursor type is not what we want,
25070 erase it. */
25071 if (w->phys_cursor_on_p
25072 && (!on
25073 || w->phys_cursor.x != x
25074 || w->phys_cursor.y != y
25075 || new_cursor_type != w->phys_cursor_type
25076 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25077 && new_cursor_width != w->phys_cursor_width)))
25078 erase_phys_cursor (w);
25080 /* Don't check phys_cursor_on_p here because that flag is only set
25081 to zero in some cases where we know that the cursor has been
25082 completely erased, to avoid the extra work of erasing the cursor
25083 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25084 still not be visible, or it has only been partly erased. */
25085 if (on)
25087 w->phys_cursor_ascent = glyph_row->ascent;
25088 w->phys_cursor_height = glyph_row->height;
25090 /* Set phys_cursor_.* before x_draw_.* is called because some
25091 of them may need the information. */
25092 w->phys_cursor.x = x;
25093 w->phys_cursor.y = glyph_row->y;
25094 w->phys_cursor.hpos = hpos;
25095 w->phys_cursor.vpos = vpos;
25098 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25099 new_cursor_type, new_cursor_width,
25100 on, active_cursor);
25104 /* Switch the display of W's cursor on or off, according to the value
25105 of ON. */
25107 static void
25108 update_window_cursor (struct window *w, int on)
25110 /* Don't update cursor in windows whose frame is in the process
25111 of being deleted. */
25112 if (w->current_matrix)
25114 BLOCK_INPUT;
25115 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
25116 w->phys_cursor.x, w->phys_cursor.y);
25117 UNBLOCK_INPUT;
25122 /* Call update_window_cursor with parameter ON_P on all leaf windows
25123 in the window tree rooted at W. */
25125 static void
25126 update_cursor_in_window_tree (struct window *w, int on_p)
25128 while (w)
25130 if (!NILP (w->hchild))
25131 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25132 else if (!NILP (w->vchild))
25133 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25134 else
25135 update_window_cursor (w, on_p);
25137 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25142 /* EXPORT:
25143 Display the cursor on window W, or clear it, according to ON_P.
25144 Don't change the cursor's position. */
25146 void
25147 x_update_cursor (struct frame *f, int on_p)
25149 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25153 /* EXPORT:
25154 Clear the cursor of window W to background color, and mark the
25155 cursor as not shown. This is used when the text where the cursor
25156 is about to be rewritten. */
25158 void
25159 x_clear_cursor (struct window *w)
25161 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25162 update_window_cursor (w, 0);
25165 #endif /* HAVE_WINDOW_SYSTEM */
25167 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25168 and MSDOS. */
25169 static void
25170 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25171 int start_hpos, int end_hpos,
25172 enum draw_glyphs_face draw)
25174 #ifdef HAVE_WINDOW_SYSTEM
25175 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25177 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25178 return;
25180 #endif
25181 #if defined (HAVE_GPM) || defined (MSDOS)
25182 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25183 #endif
25186 /* Display the active region described by mouse_face_* according to DRAW. */
25188 static void
25189 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25191 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25192 struct frame *f = XFRAME (WINDOW_FRAME (w));
25194 if (/* If window is in the process of being destroyed, don't bother
25195 to do anything. */
25196 w->current_matrix != NULL
25197 /* Don't update mouse highlight if hidden */
25198 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25199 /* Recognize when we are called to operate on rows that don't exist
25200 anymore. This can happen when a window is split. */
25201 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25203 int phys_cursor_on_p = w->phys_cursor_on_p;
25204 struct glyph_row *row, *first, *last;
25206 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25207 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25209 for (row = first; row <= last && row->enabled_p; ++row)
25211 int start_hpos, end_hpos, start_x;
25213 /* For all but the first row, the highlight starts at column 0. */
25214 if (row == first)
25216 /* R2L rows have BEG and END in reversed order, but the
25217 screen drawing geometry is always left to right. So
25218 we need to mirror the beginning and end of the
25219 highlighted area in R2L rows. */
25220 if (!row->reversed_p)
25222 start_hpos = hlinfo->mouse_face_beg_col;
25223 start_x = hlinfo->mouse_face_beg_x;
25225 else if (row == last)
25227 start_hpos = hlinfo->mouse_face_end_col;
25228 start_x = hlinfo->mouse_face_end_x;
25230 else
25232 start_hpos = 0;
25233 start_x = 0;
25236 else if (row->reversed_p && row == last)
25238 start_hpos = hlinfo->mouse_face_end_col;
25239 start_x = hlinfo->mouse_face_end_x;
25241 else
25243 start_hpos = 0;
25244 start_x = 0;
25247 if (row == last)
25249 if (!row->reversed_p)
25250 end_hpos = hlinfo->mouse_face_end_col;
25251 else if (row == first)
25252 end_hpos = hlinfo->mouse_face_beg_col;
25253 else
25255 end_hpos = row->used[TEXT_AREA];
25256 if (draw == DRAW_NORMAL_TEXT)
25257 row->fill_line_p = 1; /* Clear to end of line */
25260 else if (row->reversed_p && row == first)
25261 end_hpos = hlinfo->mouse_face_beg_col;
25262 else
25264 end_hpos = row->used[TEXT_AREA];
25265 if (draw == DRAW_NORMAL_TEXT)
25266 row->fill_line_p = 1; /* Clear to end of line */
25269 if (end_hpos > start_hpos)
25271 draw_row_with_mouse_face (w, start_x, row,
25272 start_hpos, end_hpos, draw);
25274 row->mouse_face_p
25275 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25279 #ifdef HAVE_WINDOW_SYSTEM
25280 /* When we've written over the cursor, arrange for it to
25281 be displayed again. */
25282 if (FRAME_WINDOW_P (f)
25283 && phys_cursor_on_p && !w->phys_cursor_on_p)
25285 BLOCK_INPUT;
25286 display_and_set_cursor (w, 1,
25287 w->phys_cursor.hpos, w->phys_cursor.vpos,
25288 w->phys_cursor.x, w->phys_cursor.y);
25289 UNBLOCK_INPUT;
25291 #endif /* HAVE_WINDOW_SYSTEM */
25294 #ifdef HAVE_WINDOW_SYSTEM
25295 /* Change the mouse cursor. */
25296 if (FRAME_WINDOW_P (f))
25298 if (draw == DRAW_NORMAL_TEXT
25299 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25300 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25301 else if (draw == DRAW_MOUSE_FACE)
25302 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25303 else
25304 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25306 #endif /* HAVE_WINDOW_SYSTEM */
25309 /* EXPORT:
25310 Clear out the mouse-highlighted active region.
25311 Redraw it un-highlighted first. Value is non-zero if mouse
25312 face was actually drawn unhighlighted. */
25315 clear_mouse_face (Mouse_HLInfo *hlinfo)
25317 int cleared = 0;
25319 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25321 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25322 cleared = 1;
25325 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25326 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25327 hlinfo->mouse_face_window = Qnil;
25328 hlinfo->mouse_face_overlay = Qnil;
25329 return cleared;
25332 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25333 within the mouse face on that window. */
25334 static int
25335 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25337 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25339 /* Quickly resolve the easy cases. */
25340 if (!(WINDOWP (hlinfo->mouse_face_window)
25341 && XWINDOW (hlinfo->mouse_face_window) == w))
25342 return 0;
25343 if (vpos < hlinfo->mouse_face_beg_row
25344 || vpos > hlinfo->mouse_face_end_row)
25345 return 0;
25346 if (vpos > hlinfo->mouse_face_beg_row
25347 && vpos < hlinfo->mouse_face_end_row)
25348 return 1;
25350 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25352 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25354 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25355 return 1;
25357 else if ((vpos == hlinfo->mouse_face_beg_row
25358 && hpos >= hlinfo->mouse_face_beg_col)
25359 || (vpos == hlinfo->mouse_face_end_row
25360 && hpos < hlinfo->mouse_face_end_col))
25361 return 1;
25363 else
25365 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25367 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25368 return 1;
25370 else if ((vpos == hlinfo->mouse_face_beg_row
25371 && hpos <= hlinfo->mouse_face_beg_col)
25372 || (vpos == hlinfo->mouse_face_end_row
25373 && hpos > hlinfo->mouse_face_end_col))
25374 return 1;
25376 return 0;
25380 /* EXPORT:
25381 Non-zero if physical cursor of window W is within mouse face. */
25384 cursor_in_mouse_face_p (struct window *w)
25386 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
25391 /* Find the glyph rows START_ROW and END_ROW of window W that display
25392 characters between buffer positions START_CHARPOS and END_CHARPOS
25393 (excluding END_CHARPOS). This is similar to row_containing_pos,
25394 but is more accurate when bidi reordering makes buffer positions
25395 change non-linearly with glyph rows. */
25396 static void
25397 rows_from_pos_range (struct window *w,
25398 EMACS_INT start_charpos, EMACS_INT end_charpos,
25399 struct glyph_row **start, struct glyph_row **end)
25401 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25402 int last_y = window_text_bottom_y (w);
25403 struct glyph_row *row;
25405 *start = NULL;
25406 *end = NULL;
25408 while (!first->enabled_p
25409 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25410 first++;
25412 /* Find the START row. */
25413 for (row = first;
25414 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25415 row++)
25417 /* A row can potentially be the START row if the range of the
25418 characters it displays intersects the range
25419 [START_CHARPOS..END_CHARPOS). */
25420 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25421 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25422 /* See the commentary in row_containing_pos, for the
25423 explanation of the complicated way to check whether
25424 some position is beyond the end of the characters
25425 displayed by a row. */
25426 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25427 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25428 && !row->ends_at_zv_p
25429 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25430 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25431 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25432 && !row->ends_at_zv_p
25433 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25435 /* Found a candidate row. Now make sure at least one of the
25436 glyphs it displays has a charpos from the range
25437 [START_CHARPOS..END_CHARPOS).
25439 This is not obvious because bidi reordering could make
25440 buffer positions of a row be 1,2,3,102,101,100, and if we
25441 want to highlight characters in [50..60), we don't want
25442 this row, even though [50..60) does intersect [1..103),
25443 the range of character positions given by the row's start
25444 and end positions. */
25445 struct glyph *g = row->glyphs[TEXT_AREA];
25446 struct glyph *e = g + row->used[TEXT_AREA];
25448 while (g < e)
25450 if ((BUFFERP (g->object) || INTEGERP (g->object))
25451 && start_charpos <= g->charpos && g->charpos < end_charpos)
25452 *start = row;
25453 g++;
25455 if (*start)
25456 break;
25460 /* Find the END row. */
25461 if (!*start
25462 /* If the last row is partially visible, start looking for END
25463 from that row, instead of starting from FIRST. */
25464 && !(row->enabled_p
25465 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25466 row = first;
25467 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25469 struct glyph_row *next = row + 1;
25471 if (!next->enabled_p
25472 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25473 /* The first row >= START whose range of displayed characters
25474 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25475 is the row END + 1. */
25476 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25477 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25478 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25479 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25480 && !next->ends_at_zv_p
25481 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25482 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25483 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25484 && !next->ends_at_zv_p
25485 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25487 *end = row;
25488 break;
25490 else
25492 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25493 but none of the characters it displays are in the range, it is
25494 also END + 1. */
25495 struct glyph *g = next->glyphs[TEXT_AREA];
25496 struct glyph *e = g + next->used[TEXT_AREA];
25498 while (g < e)
25500 if ((BUFFERP (g->object) || INTEGERP (g->object))
25501 && start_charpos <= g->charpos && g->charpos < end_charpos)
25502 break;
25503 g++;
25505 if (g == e)
25507 *end = row;
25508 break;
25514 /* This function sets the mouse_face_* elements of HLINFO, assuming
25515 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25516 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25517 for the overlay or run of text properties specifying the mouse
25518 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25519 before-string and after-string that must also be highlighted.
25520 COVER_STRING, if non-nil, is a display string that may cover some
25521 or all of the highlighted text. */
25523 static void
25524 mouse_face_from_buffer_pos (Lisp_Object window,
25525 Mouse_HLInfo *hlinfo,
25526 EMACS_INT mouse_charpos,
25527 EMACS_INT start_charpos,
25528 EMACS_INT end_charpos,
25529 Lisp_Object before_string,
25530 Lisp_Object after_string,
25531 Lisp_Object cover_string)
25533 struct window *w = XWINDOW (window);
25534 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25535 struct glyph_row *r1, *r2;
25536 struct glyph *glyph, *end;
25537 EMACS_INT ignore, pos;
25538 int x;
25540 xassert (NILP (cover_string) || STRINGP (cover_string));
25541 xassert (NILP (before_string) || STRINGP (before_string));
25542 xassert (NILP (after_string) || STRINGP (after_string));
25544 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25545 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25546 if (r1 == NULL)
25547 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25548 /* If the before-string or display-string contains newlines,
25549 rows_from_pos_range skips to its last row. Move back. */
25550 if (!NILP (before_string) || !NILP (cover_string))
25552 struct glyph_row *prev;
25553 while ((prev = r1 - 1, prev >= first)
25554 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25555 && prev->used[TEXT_AREA] > 0)
25557 struct glyph *beg = prev->glyphs[TEXT_AREA];
25558 glyph = beg + prev->used[TEXT_AREA];
25559 while (--glyph >= beg && INTEGERP (glyph->object));
25560 if (glyph < beg
25561 || !(EQ (glyph->object, before_string)
25562 || EQ (glyph->object, cover_string)))
25563 break;
25564 r1 = prev;
25567 if (r2 == NULL)
25569 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25570 hlinfo->mouse_face_past_end = 1;
25572 else if (!NILP (after_string))
25574 /* If the after-string has newlines, advance to its last row. */
25575 struct glyph_row *next;
25576 struct glyph_row *last
25577 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25579 for (next = r2 + 1;
25580 next <= last
25581 && next->used[TEXT_AREA] > 0
25582 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25583 ++next)
25584 r2 = next;
25586 /* The rest of the display engine assumes that mouse_face_beg_row is
25587 either above below mouse_face_end_row or identical to it. But
25588 with bidi-reordered continued lines, the row for START_CHARPOS
25589 could be below the row for END_CHARPOS. If so, swap the rows and
25590 store them in correct order. */
25591 if (r1->y > r2->y)
25593 struct glyph_row *tem = r2;
25595 r2 = r1;
25596 r1 = tem;
25599 hlinfo->mouse_face_beg_y = r1->y;
25600 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25601 hlinfo->mouse_face_end_y = r2->y;
25602 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25604 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25605 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25606 could be anywhere in the row and in any order. The strategy
25607 below is to find the leftmost and the rightmost glyph that
25608 belongs to either of these 3 strings, or whose position is
25609 between START_CHARPOS and END_CHARPOS, and highlight all the
25610 glyphs between those two. This may cover more than just the text
25611 between START_CHARPOS and END_CHARPOS if the range of characters
25612 strides the bidi level boundary, e.g. if the beginning is in R2L
25613 text while the end is in L2R text or vice versa. */
25614 if (!r1->reversed_p)
25616 /* This row is in a left to right paragraph. Scan it left to
25617 right. */
25618 glyph = r1->glyphs[TEXT_AREA];
25619 end = glyph + r1->used[TEXT_AREA];
25620 x = r1->x;
25622 /* Skip truncation glyphs at the start of the glyph row. */
25623 if (r1->displays_text_p)
25624 for (; glyph < end
25625 && INTEGERP (glyph->object)
25626 && glyph->charpos < 0;
25627 ++glyph)
25628 x += glyph->pixel_width;
25630 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25631 or COVER_STRING, and the first glyph from buffer whose
25632 position is between START_CHARPOS and END_CHARPOS. */
25633 for (; glyph < end
25634 && !INTEGERP (glyph->object)
25635 && !EQ (glyph->object, cover_string)
25636 && !(BUFFERP (glyph->object)
25637 && (glyph->charpos >= start_charpos
25638 && glyph->charpos < end_charpos));
25639 ++glyph)
25641 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25642 are present at buffer positions between START_CHARPOS and
25643 END_CHARPOS, or if they come from an overlay. */
25644 if (EQ (glyph->object, before_string))
25646 pos = string_buffer_position (before_string,
25647 start_charpos);
25648 /* If pos == 0, it means before_string came from an
25649 overlay, not from a buffer position. */
25650 if (!pos || (pos >= start_charpos && pos < end_charpos))
25651 break;
25653 else if (EQ (glyph->object, after_string))
25655 pos = string_buffer_position (after_string, end_charpos);
25656 if (!pos || (pos >= start_charpos && pos < end_charpos))
25657 break;
25659 x += glyph->pixel_width;
25661 hlinfo->mouse_face_beg_x = x;
25662 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25664 else
25666 /* This row is in a right to left paragraph. Scan it right to
25667 left. */
25668 struct glyph *g;
25670 end = r1->glyphs[TEXT_AREA] - 1;
25671 glyph = end + r1->used[TEXT_AREA];
25673 /* Skip truncation glyphs at the start of the glyph row. */
25674 if (r1->displays_text_p)
25675 for (; glyph > end
25676 && INTEGERP (glyph->object)
25677 && glyph->charpos < 0;
25678 --glyph)
25681 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25682 or COVER_STRING, and the first glyph from buffer whose
25683 position is between START_CHARPOS and END_CHARPOS. */
25684 for (; glyph > end
25685 && !INTEGERP (glyph->object)
25686 && !EQ (glyph->object, cover_string)
25687 && !(BUFFERP (glyph->object)
25688 && (glyph->charpos >= start_charpos
25689 && glyph->charpos < end_charpos));
25690 --glyph)
25692 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25693 are present at buffer positions between START_CHARPOS and
25694 END_CHARPOS, or if they come from an overlay. */
25695 if (EQ (glyph->object, before_string))
25697 pos = string_buffer_position (before_string, start_charpos);
25698 /* If pos == 0, it means before_string came from an
25699 overlay, not from a buffer position. */
25700 if (!pos || (pos >= start_charpos && pos < end_charpos))
25701 break;
25703 else if (EQ (glyph->object, after_string))
25705 pos = string_buffer_position (after_string, end_charpos);
25706 if (!pos || (pos >= start_charpos && pos < end_charpos))
25707 break;
25711 glyph++; /* first glyph to the right of the highlighted area */
25712 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25713 x += g->pixel_width;
25714 hlinfo->mouse_face_beg_x = x;
25715 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25718 /* If the highlight ends in a different row, compute GLYPH and END
25719 for the end row. Otherwise, reuse the values computed above for
25720 the row where the highlight begins. */
25721 if (r2 != r1)
25723 if (!r2->reversed_p)
25725 glyph = r2->glyphs[TEXT_AREA];
25726 end = glyph + r2->used[TEXT_AREA];
25727 x = r2->x;
25729 else
25731 end = r2->glyphs[TEXT_AREA] - 1;
25732 glyph = end + r2->used[TEXT_AREA];
25736 if (!r2->reversed_p)
25738 /* Skip truncation and continuation glyphs near the end of the
25739 row, and also blanks and stretch glyphs inserted by
25740 extend_face_to_end_of_line. */
25741 while (end > glyph
25742 && INTEGERP ((end - 1)->object)
25743 && (end - 1)->charpos <= 0)
25744 --end;
25745 /* Scan the rest of the glyph row from the end, looking for the
25746 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25747 COVER_STRING, or whose position is between START_CHARPOS
25748 and END_CHARPOS */
25749 for (--end;
25750 end > glyph
25751 && !INTEGERP (end->object)
25752 && !EQ (end->object, cover_string)
25753 && !(BUFFERP (end->object)
25754 && (end->charpos >= start_charpos
25755 && end->charpos < end_charpos));
25756 --end)
25758 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25759 are present at buffer positions between START_CHARPOS and
25760 END_CHARPOS, or if they come from an overlay. */
25761 if (EQ (end->object, before_string))
25763 pos = string_buffer_position (before_string, start_charpos);
25764 if (!pos || (pos >= start_charpos && pos < end_charpos))
25765 break;
25767 else if (EQ (end->object, after_string))
25769 pos = string_buffer_position (after_string, end_charpos);
25770 if (!pos || (pos >= start_charpos && pos < end_charpos))
25771 break;
25774 /* Find the X coordinate of the last glyph to be highlighted. */
25775 for (; glyph <= end; ++glyph)
25776 x += glyph->pixel_width;
25778 hlinfo->mouse_face_end_x = x;
25779 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25781 else
25783 /* Skip truncation and continuation glyphs near the end of the
25784 row, and also blanks and stretch glyphs inserted by
25785 extend_face_to_end_of_line. */
25786 x = r2->x;
25787 end++;
25788 while (end < glyph
25789 && INTEGERP (end->object)
25790 && end->charpos <= 0)
25792 x += end->pixel_width;
25793 ++end;
25795 /* Scan the rest of the glyph row from the end, looking for the
25796 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25797 COVER_STRING, or whose position is between START_CHARPOS
25798 and END_CHARPOS */
25799 for ( ;
25800 end < glyph
25801 && !INTEGERP (end->object)
25802 && !EQ (end->object, cover_string)
25803 && !(BUFFERP (end->object)
25804 && (end->charpos >= start_charpos
25805 && end->charpos < end_charpos));
25806 ++end)
25808 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25809 are present at buffer positions between START_CHARPOS and
25810 END_CHARPOS, or if they come from an overlay. */
25811 if (EQ (end->object, before_string))
25813 pos = string_buffer_position (before_string, start_charpos);
25814 if (!pos || (pos >= start_charpos && pos < end_charpos))
25815 break;
25817 else if (EQ (end->object, after_string))
25819 pos = string_buffer_position (after_string, end_charpos);
25820 if (!pos || (pos >= start_charpos && pos < end_charpos))
25821 break;
25823 x += end->pixel_width;
25825 hlinfo->mouse_face_end_x = x;
25826 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25829 hlinfo->mouse_face_window = window;
25830 hlinfo->mouse_face_face_id
25831 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25832 mouse_charpos + 1,
25833 !hlinfo->mouse_face_hidden, -1);
25834 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25837 /* The following function is not used anymore (replaced with
25838 mouse_face_from_string_pos), but I leave it here for the time
25839 being, in case someone would. */
25841 #if 0 /* not used */
25843 /* Find the position of the glyph for position POS in OBJECT in
25844 window W's current matrix, and return in *X, *Y the pixel
25845 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25847 RIGHT_P non-zero means return the position of the right edge of the
25848 glyph, RIGHT_P zero means return the left edge position.
25850 If no glyph for POS exists in the matrix, return the position of
25851 the glyph with the next smaller position that is in the matrix, if
25852 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25853 exists in the matrix, return the position of the glyph with the
25854 next larger position in OBJECT.
25856 Value is non-zero if a glyph was found. */
25858 static int
25859 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25860 int *hpos, int *vpos, int *x, int *y, int right_p)
25862 int yb = window_text_bottom_y (w);
25863 struct glyph_row *r;
25864 struct glyph *best_glyph = NULL;
25865 struct glyph_row *best_row = NULL;
25866 int best_x = 0;
25868 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25869 r->enabled_p && r->y < yb;
25870 ++r)
25872 struct glyph *g = r->glyphs[TEXT_AREA];
25873 struct glyph *e = g + r->used[TEXT_AREA];
25874 int gx;
25876 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25877 if (EQ (g->object, object))
25879 if (g->charpos == pos)
25881 best_glyph = g;
25882 best_x = gx;
25883 best_row = r;
25884 goto found;
25886 else if (best_glyph == NULL
25887 || ((eabs (g->charpos - pos)
25888 < eabs (best_glyph->charpos - pos))
25889 && (right_p
25890 ? g->charpos < pos
25891 : g->charpos > pos)))
25893 best_glyph = g;
25894 best_x = gx;
25895 best_row = r;
25900 found:
25902 if (best_glyph)
25904 *x = best_x;
25905 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25907 if (right_p)
25909 *x += best_glyph->pixel_width;
25910 ++*hpos;
25913 *y = best_row->y;
25914 *vpos = best_row - w->current_matrix->rows;
25917 return best_glyph != NULL;
25919 #endif /* not used */
25921 /* Find the positions of the first and the last glyphs in window W's
25922 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25923 (assumed to be a string), and return in HLINFO's mouse_face_*
25924 members the pixel and column/row coordinates of those glyphs. */
25926 static void
25927 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25928 Lisp_Object object,
25929 EMACS_INT startpos, EMACS_INT endpos)
25931 int yb = window_text_bottom_y (w);
25932 struct glyph_row *r;
25933 struct glyph *g, *e;
25934 int gx;
25935 int found = 0;
25937 /* Find the glyph row with at least one position in the range
25938 [STARTPOS..ENDPOS], and the first glyph in that row whose
25939 position belongs to that range. */
25940 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25941 r->enabled_p && r->y < yb;
25942 ++r)
25944 if (!r->reversed_p)
25946 g = r->glyphs[TEXT_AREA];
25947 e = g + r->used[TEXT_AREA];
25948 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25949 if (EQ (g->object, object)
25950 && startpos <= g->charpos && g->charpos <= endpos)
25952 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25953 hlinfo->mouse_face_beg_y = r->y;
25954 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25955 hlinfo->mouse_face_beg_x = gx;
25956 found = 1;
25957 break;
25960 else
25962 struct glyph *g1;
25964 e = r->glyphs[TEXT_AREA];
25965 g = e + r->used[TEXT_AREA];
25966 for ( ; g > e; --g)
25967 if (EQ ((g-1)->object, object)
25968 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25970 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25971 hlinfo->mouse_face_beg_y = r->y;
25972 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25973 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25974 gx += g1->pixel_width;
25975 hlinfo->mouse_face_beg_x = gx;
25976 found = 1;
25977 break;
25980 if (found)
25981 break;
25984 if (!found)
25985 return;
25987 /* Starting with the next row, look for the first row which does NOT
25988 include any glyphs whose positions are in the range. */
25989 for (++r; r->enabled_p && r->y < yb; ++r)
25991 g = r->glyphs[TEXT_AREA];
25992 e = g + r->used[TEXT_AREA];
25993 found = 0;
25994 for ( ; g < e; ++g)
25995 if (EQ (g->object, object)
25996 && startpos <= g->charpos && g->charpos <= endpos)
25998 found = 1;
25999 break;
26001 if (!found)
26002 break;
26005 /* The highlighted region ends on the previous row. */
26006 r--;
26008 /* Set the end row and its vertical pixel coordinate. */
26009 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26010 hlinfo->mouse_face_end_y = r->y;
26012 /* Compute and set the end column and the end column's horizontal
26013 pixel coordinate. */
26014 if (!r->reversed_p)
26016 g = r->glyphs[TEXT_AREA];
26017 e = g + r->used[TEXT_AREA];
26018 for ( ; e > g; --e)
26019 if (EQ ((e-1)->object, object)
26020 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26021 break;
26022 hlinfo->mouse_face_end_col = e - g;
26024 for (gx = r->x; g < e; ++g)
26025 gx += g->pixel_width;
26026 hlinfo->mouse_face_end_x = gx;
26028 else
26030 e = r->glyphs[TEXT_AREA];
26031 g = e + r->used[TEXT_AREA];
26032 for (gx = r->x ; e < g; ++e)
26034 if (EQ (e->object, object)
26035 && startpos <= e->charpos && e->charpos <= endpos)
26036 break;
26037 gx += e->pixel_width;
26039 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26040 hlinfo->mouse_face_end_x = gx;
26044 #ifdef HAVE_WINDOW_SYSTEM
26046 /* See if position X, Y is within a hot-spot of an image. */
26048 static int
26049 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26051 if (!CONSP (hot_spot))
26052 return 0;
26054 if (EQ (XCAR (hot_spot), Qrect))
26056 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26057 Lisp_Object rect = XCDR (hot_spot);
26058 Lisp_Object tem;
26059 if (!CONSP (rect))
26060 return 0;
26061 if (!CONSP (XCAR (rect)))
26062 return 0;
26063 if (!CONSP (XCDR (rect)))
26064 return 0;
26065 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26066 return 0;
26067 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26068 return 0;
26069 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26070 return 0;
26071 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26072 return 0;
26073 return 1;
26075 else if (EQ (XCAR (hot_spot), Qcircle))
26077 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26078 Lisp_Object circ = XCDR (hot_spot);
26079 Lisp_Object lr, lx0, ly0;
26080 if (CONSP (circ)
26081 && CONSP (XCAR (circ))
26082 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26083 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26084 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26086 double r = XFLOATINT (lr);
26087 double dx = XINT (lx0) - x;
26088 double dy = XINT (ly0) - y;
26089 return (dx * dx + dy * dy <= r * r);
26092 else if (EQ (XCAR (hot_spot), Qpoly))
26094 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26095 if (VECTORP (XCDR (hot_spot)))
26097 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26098 Lisp_Object *poly = v->contents;
26099 int n = v->header.size;
26100 int i;
26101 int inside = 0;
26102 Lisp_Object lx, ly;
26103 int x0, y0;
26105 /* Need an even number of coordinates, and at least 3 edges. */
26106 if (n < 6 || n & 1)
26107 return 0;
26109 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26110 If count is odd, we are inside polygon. Pixels on edges
26111 may or may not be included depending on actual geometry of the
26112 polygon. */
26113 if ((lx = poly[n-2], !INTEGERP (lx))
26114 || (ly = poly[n-1], !INTEGERP (lx)))
26115 return 0;
26116 x0 = XINT (lx), y0 = XINT (ly);
26117 for (i = 0; i < n; i += 2)
26119 int x1 = x0, y1 = y0;
26120 if ((lx = poly[i], !INTEGERP (lx))
26121 || (ly = poly[i+1], !INTEGERP (ly)))
26122 return 0;
26123 x0 = XINT (lx), y0 = XINT (ly);
26125 /* Does this segment cross the X line? */
26126 if (x0 >= x)
26128 if (x1 >= x)
26129 continue;
26131 else if (x1 < x)
26132 continue;
26133 if (y > y0 && y > y1)
26134 continue;
26135 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26136 inside = !inside;
26138 return inside;
26141 return 0;
26144 Lisp_Object
26145 find_hot_spot (Lisp_Object map, int x, int y)
26147 while (CONSP (map))
26149 if (CONSP (XCAR (map))
26150 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26151 return XCAR (map);
26152 map = XCDR (map);
26155 return Qnil;
26158 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26159 3, 3, 0,
26160 doc: /* Lookup in image map MAP coordinates X and Y.
26161 An image map is an alist where each element has the format (AREA ID PLIST).
26162 An AREA is specified as either a rectangle, a circle, or a polygon:
26163 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26164 pixel coordinates of the upper left and bottom right corners.
26165 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26166 and the radius of the circle; r may be a float or integer.
26167 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26168 vector describes one corner in the polygon.
26169 Returns the alist element for the first matching AREA in MAP. */)
26170 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26172 if (NILP (map))
26173 return Qnil;
26175 CHECK_NUMBER (x);
26176 CHECK_NUMBER (y);
26178 return find_hot_spot (map, XINT (x), XINT (y));
26182 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26183 static void
26184 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26186 /* Do not change cursor shape while dragging mouse. */
26187 if (!NILP (do_mouse_tracking))
26188 return;
26190 if (!NILP (pointer))
26192 if (EQ (pointer, Qarrow))
26193 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26194 else if (EQ (pointer, Qhand))
26195 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26196 else if (EQ (pointer, Qtext))
26197 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26198 else if (EQ (pointer, intern ("hdrag")))
26199 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26200 #ifdef HAVE_X_WINDOWS
26201 else if (EQ (pointer, intern ("vdrag")))
26202 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26203 #endif
26204 else if (EQ (pointer, intern ("hourglass")))
26205 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26206 else if (EQ (pointer, Qmodeline))
26207 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26208 else
26209 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26212 if (cursor != No_Cursor)
26213 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26216 #endif /* HAVE_WINDOW_SYSTEM */
26218 /* Take proper action when mouse has moved to the mode or header line
26219 or marginal area AREA of window W, x-position X and y-position Y.
26220 X is relative to the start of the text display area of W, so the
26221 width of bitmap areas and scroll bars must be subtracted to get a
26222 position relative to the start of the mode line. */
26224 static void
26225 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26226 enum window_part area)
26228 struct window *w = XWINDOW (window);
26229 struct frame *f = XFRAME (w->frame);
26230 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26231 #ifdef HAVE_WINDOW_SYSTEM
26232 Display_Info *dpyinfo;
26233 #endif
26234 Cursor cursor = No_Cursor;
26235 Lisp_Object pointer = Qnil;
26236 int dx, dy, width, height;
26237 EMACS_INT charpos;
26238 Lisp_Object string, object = Qnil;
26239 Lisp_Object pos, help;
26241 Lisp_Object mouse_face;
26242 int original_x_pixel = x;
26243 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26244 struct glyph_row *row;
26246 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26248 int x0;
26249 struct glyph *end;
26251 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26252 returns them in row/column units! */
26253 string = mode_line_string (w, area, &x, &y, &charpos,
26254 &object, &dx, &dy, &width, &height);
26256 row = (area == ON_MODE_LINE
26257 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26258 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26260 /* Find the glyph under the mouse pointer. */
26261 if (row->mode_line_p && row->enabled_p)
26263 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26264 end = glyph + row->used[TEXT_AREA];
26266 for (x0 = original_x_pixel;
26267 glyph < end && x0 >= glyph->pixel_width;
26268 ++glyph)
26269 x0 -= glyph->pixel_width;
26271 if (glyph >= end)
26272 glyph = NULL;
26275 else
26277 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26278 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26279 returns them in row/column units! */
26280 string = marginal_area_string (w, area, &x, &y, &charpos,
26281 &object, &dx, &dy, &width, &height);
26284 help = Qnil;
26286 #ifdef HAVE_WINDOW_SYSTEM
26287 if (IMAGEP (object))
26289 Lisp_Object image_map, hotspot;
26290 if ((image_map = Fplist_get (XCDR (object), QCmap),
26291 !NILP (image_map))
26292 && (hotspot = find_hot_spot (image_map, dx, dy),
26293 CONSP (hotspot))
26294 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26296 Lisp_Object plist;
26298 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26299 If so, we could look for mouse-enter, mouse-leave
26300 properties in PLIST (and do something...). */
26301 hotspot = XCDR (hotspot);
26302 if (CONSP (hotspot)
26303 && (plist = XCAR (hotspot), CONSP (plist)))
26305 pointer = Fplist_get (plist, Qpointer);
26306 if (NILP (pointer))
26307 pointer = Qhand;
26308 help = Fplist_get (plist, Qhelp_echo);
26309 if (!NILP (help))
26311 help_echo_string = help;
26312 /* Is this correct? ++kfs */
26313 XSETWINDOW (help_echo_window, w);
26314 help_echo_object = w->buffer;
26315 help_echo_pos = charpos;
26319 if (NILP (pointer))
26320 pointer = Fplist_get (XCDR (object), QCpointer);
26322 #endif /* HAVE_WINDOW_SYSTEM */
26324 if (STRINGP (string))
26326 pos = make_number (charpos);
26327 /* If we're on a string with `help-echo' text property, arrange
26328 for the help to be displayed. This is done by setting the
26329 global variable help_echo_string to the help string. */
26330 if (NILP (help))
26332 help = Fget_text_property (pos, Qhelp_echo, string);
26333 if (!NILP (help))
26335 help_echo_string = help;
26336 XSETWINDOW (help_echo_window, w);
26337 help_echo_object = string;
26338 help_echo_pos = charpos;
26342 #ifdef HAVE_WINDOW_SYSTEM
26343 if (FRAME_WINDOW_P (f))
26345 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26346 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26347 if (NILP (pointer))
26348 pointer = Fget_text_property (pos, Qpointer, string);
26350 /* Change the mouse pointer according to what is under X/Y. */
26351 if (NILP (pointer)
26352 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26354 Lisp_Object map;
26355 map = Fget_text_property (pos, Qlocal_map, string);
26356 if (!KEYMAPP (map))
26357 map = Fget_text_property (pos, Qkeymap, string);
26358 if (!KEYMAPP (map))
26359 cursor = dpyinfo->vertical_scroll_bar_cursor;
26362 #endif
26364 /* Change the mouse face according to what is under X/Y. */
26365 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26366 if (!NILP (mouse_face)
26367 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26368 && glyph)
26370 Lisp_Object b, e;
26372 struct glyph * tmp_glyph;
26374 int gpos;
26375 int gseq_length;
26376 int total_pixel_width;
26377 EMACS_INT begpos, endpos, ignore;
26379 int vpos, hpos;
26381 b = Fprevious_single_property_change (make_number (charpos + 1),
26382 Qmouse_face, string, Qnil);
26383 if (NILP (b))
26384 begpos = 0;
26385 else
26386 begpos = XINT (b);
26388 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26389 if (NILP (e))
26390 endpos = SCHARS (string);
26391 else
26392 endpos = XINT (e);
26394 /* Calculate the glyph position GPOS of GLYPH in the
26395 displayed string, relative to the beginning of the
26396 highlighted part of the string.
26398 Note: GPOS is different from CHARPOS. CHARPOS is the
26399 position of GLYPH in the internal string object. A mode
26400 line string format has structures which are converted to
26401 a flattened string by the Emacs Lisp interpreter. The
26402 internal string is an element of those structures. The
26403 displayed string is the flattened string. */
26404 tmp_glyph = row_start_glyph;
26405 while (tmp_glyph < glyph
26406 && (!(EQ (tmp_glyph->object, glyph->object)
26407 && begpos <= tmp_glyph->charpos
26408 && tmp_glyph->charpos < endpos)))
26409 tmp_glyph++;
26410 gpos = glyph - tmp_glyph;
26412 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26413 the highlighted part of the displayed string to which
26414 GLYPH belongs. Note: GSEQ_LENGTH is different from
26415 SCHARS (STRING), because the latter returns the length of
26416 the internal string. */
26417 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26418 tmp_glyph > glyph
26419 && (!(EQ (tmp_glyph->object, glyph->object)
26420 && begpos <= tmp_glyph->charpos
26421 && tmp_glyph->charpos < endpos));
26422 tmp_glyph--)
26424 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26426 /* Calculate the total pixel width of all the glyphs between
26427 the beginning of the highlighted area and GLYPH. */
26428 total_pixel_width = 0;
26429 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26430 total_pixel_width += tmp_glyph->pixel_width;
26432 /* Pre calculation of re-rendering position. Note: X is in
26433 column units here, after the call to mode_line_string or
26434 marginal_area_string. */
26435 hpos = x - gpos;
26436 vpos = (area == ON_MODE_LINE
26437 ? (w->current_matrix)->nrows - 1
26438 : 0);
26440 /* If GLYPH's position is included in the region that is
26441 already drawn in mouse face, we have nothing to do. */
26442 if ( EQ (window, hlinfo->mouse_face_window)
26443 && (!row->reversed_p
26444 ? (hlinfo->mouse_face_beg_col <= hpos
26445 && hpos < hlinfo->mouse_face_end_col)
26446 /* In R2L rows we swap BEG and END, see below. */
26447 : (hlinfo->mouse_face_end_col <= hpos
26448 && hpos < hlinfo->mouse_face_beg_col))
26449 && hlinfo->mouse_face_beg_row == vpos )
26450 return;
26452 if (clear_mouse_face (hlinfo))
26453 cursor = No_Cursor;
26455 if (!row->reversed_p)
26457 hlinfo->mouse_face_beg_col = hpos;
26458 hlinfo->mouse_face_beg_x = original_x_pixel
26459 - (total_pixel_width + dx);
26460 hlinfo->mouse_face_end_col = hpos + gseq_length;
26461 hlinfo->mouse_face_end_x = 0;
26463 else
26465 /* In R2L rows, show_mouse_face expects BEG and END
26466 coordinates to be swapped. */
26467 hlinfo->mouse_face_end_col = hpos;
26468 hlinfo->mouse_face_end_x = original_x_pixel
26469 - (total_pixel_width + dx);
26470 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26471 hlinfo->mouse_face_beg_x = 0;
26474 hlinfo->mouse_face_beg_row = vpos;
26475 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26476 hlinfo->mouse_face_beg_y = 0;
26477 hlinfo->mouse_face_end_y = 0;
26478 hlinfo->mouse_face_past_end = 0;
26479 hlinfo->mouse_face_window = window;
26481 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26482 charpos,
26483 0, 0, 0,
26484 &ignore,
26485 glyph->face_id,
26487 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26489 if (NILP (pointer))
26490 pointer = Qhand;
26492 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26493 clear_mouse_face (hlinfo);
26495 #ifdef HAVE_WINDOW_SYSTEM
26496 if (FRAME_WINDOW_P (f))
26497 define_frame_cursor1 (f, cursor, pointer);
26498 #endif
26502 /* EXPORT:
26503 Take proper action when the mouse has moved to position X, Y on
26504 frame F as regards highlighting characters that have mouse-face
26505 properties. Also de-highlighting chars where the mouse was before.
26506 X and Y can be negative or out of range. */
26508 void
26509 note_mouse_highlight (struct frame *f, int x, int y)
26511 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26512 enum window_part part;
26513 Lisp_Object window;
26514 struct window *w;
26515 Cursor cursor = No_Cursor;
26516 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26517 struct buffer *b;
26519 /* When a menu is active, don't highlight because this looks odd. */
26520 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26521 if (popup_activated ())
26522 return;
26523 #endif
26525 if (NILP (Vmouse_highlight)
26526 || !f->glyphs_initialized_p
26527 || f->pointer_invisible)
26528 return;
26530 hlinfo->mouse_face_mouse_x = x;
26531 hlinfo->mouse_face_mouse_y = y;
26532 hlinfo->mouse_face_mouse_frame = f;
26534 if (hlinfo->mouse_face_defer)
26535 return;
26537 if (gc_in_progress)
26539 hlinfo->mouse_face_deferred_gc = 1;
26540 return;
26543 /* Which window is that in? */
26544 window = window_from_coordinates (f, x, y, &part, 1);
26546 /* If we were displaying active text in another window, clear that.
26547 Also clear if we move out of text area in same window. */
26548 if (! EQ (window, hlinfo->mouse_face_window)
26549 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26550 && !NILP (hlinfo->mouse_face_window)))
26551 clear_mouse_face (hlinfo);
26553 /* Not on a window -> return. */
26554 if (!WINDOWP (window))
26555 return;
26557 /* Reset help_echo_string. It will get recomputed below. */
26558 help_echo_string = Qnil;
26560 /* Convert to window-relative pixel coordinates. */
26561 w = XWINDOW (window);
26562 frame_to_window_pixel_xy (w, &x, &y);
26564 #ifdef HAVE_WINDOW_SYSTEM
26565 /* Handle tool-bar window differently since it doesn't display a
26566 buffer. */
26567 if (EQ (window, f->tool_bar_window))
26569 note_tool_bar_highlight (f, x, y);
26570 return;
26572 #endif
26574 /* Mouse is on the mode, header line or margin? */
26575 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26576 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26578 note_mode_line_or_margin_highlight (window, x, y, part);
26579 return;
26582 #ifdef HAVE_WINDOW_SYSTEM
26583 if (part == ON_VERTICAL_BORDER)
26585 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26586 help_echo_string = build_string ("drag-mouse-1: resize");
26588 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26589 || part == ON_SCROLL_BAR)
26590 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26591 else
26592 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26593 #endif
26595 /* Are we in a window whose display is up to date?
26596 And verify the buffer's text has not changed. */
26597 b = XBUFFER (w->buffer);
26598 if (part == ON_TEXT
26599 && EQ (w->window_end_valid, w->buffer)
26600 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26601 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26603 int hpos, vpos, dx, dy, area;
26604 EMACS_INT pos;
26605 struct glyph *glyph;
26606 Lisp_Object object;
26607 Lisp_Object mouse_face = Qnil, position;
26608 Lisp_Object *overlay_vec = NULL;
26609 ptrdiff_t i, noverlays;
26610 struct buffer *obuf;
26611 EMACS_INT obegv, ozv;
26612 int same_region;
26614 /* Find the glyph under X/Y. */
26615 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26617 #ifdef HAVE_WINDOW_SYSTEM
26618 /* Look for :pointer property on image. */
26619 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26621 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26622 if (img != NULL && IMAGEP (img->spec))
26624 Lisp_Object image_map, hotspot;
26625 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26626 !NILP (image_map))
26627 && (hotspot = find_hot_spot (image_map,
26628 glyph->slice.img.x + dx,
26629 glyph->slice.img.y + dy),
26630 CONSP (hotspot))
26631 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26633 Lisp_Object plist;
26635 /* Could check XCAR (hotspot) to see if we enter/leave
26636 this hot-spot.
26637 If so, we could look for mouse-enter, mouse-leave
26638 properties in PLIST (and do something...). */
26639 hotspot = XCDR (hotspot);
26640 if (CONSP (hotspot)
26641 && (plist = XCAR (hotspot), CONSP (plist)))
26643 pointer = Fplist_get (plist, Qpointer);
26644 if (NILP (pointer))
26645 pointer = Qhand;
26646 help_echo_string = Fplist_get (plist, Qhelp_echo);
26647 if (!NILP (help_echo_string))
26649 help_echo_window = window;
26650 help_echo_object = glyph->object;
26651 help_echo_pos = glyph->charpos;
26655 if (NILP (pointer))
26656 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26659 #endif /* HAVE_WINDOW_SYSTEM */
26661 /* Clear mouse face if X/Y not over text. */
26662 if (glyph == NULL
26663 || area != TEXT_AREA
26664 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26665 /* Glyph's OBJECT is an integer for glyphs inserted by the
26666 display engine for its internal purposes, like truncation
26667 and continuation glyphs and blanks beyond the end of
26668 line's text on text terminals. If we are over such a
26669 glyph, we are not over any text. */
26670 || INTEGERP (glyph->object)
26671 /* R2L rows have a stretch glyph at their front, which
26672 stands for no text, whereas L2R rows have no glyphs at
26673 all beyond the end of text. Treat such stretch glyphs
26674 like we do with NULL glyphs in L2R rows. */
26675 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26676 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26677 && glyph->type == STRETCH_GLYPH
26678 && glyph->avoid_cursor_p))
26680 if (clear_mouse_face (hlinfo))
26681 cursor = No_Cursor;
26682 #ifdef HAVE_WINDOW_SYSTEM
26683 if (FRAME_WINDOW_P (f) && NILP (pointer))
26685 if (area != TEXT_AREA)
26686 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26687 else
26688 pointer = Vvoid_text_area_pointer;
26690 #endif
26691 goto set_cursor;
26694 pos = glyph->charpos;
26695 object = glyph->object;
26696 if (!STRINGP (object) && !BUFFERP (object))
26697 goto set_cursor;
26699 /* If we get an out-of-range value, return now; avoid an error. */
26700 if (BUFFERP (object) && pos > BUF_Z (b))
26701 goto set_cursor;
26703 /* Make the window's buffer temporarily current for
26704 overlays_at and compute_char_face. */
26705 obuf = current_buffer;
26706 current_buffer = b;
26707 obegv = BEGV;
26708 ozv = ZV;
26709 BEGV = BEG;
26710 ZV = Z;
26712 /* Is this char mouse-active or does it have help-echo? */
26713 position = make_number (pos);
26715 if (BUFFERP (object))
26717 /* Put all the overlays we want in a vector in overlay_vec. */
26718 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26719 /* Sort overlays into increasing priority order. */
26720 noverlays = sort_overlays (overlay_vec, noverlays, w);
26722 else
26723 noverlays = 0;
26725 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26727 if (same_region)
26728 cursor = No_Cursor;
26730 /* Check mouse-face highlighting. */
26731 if (! same_region
26732 /* If there exists an overlay with mouse-face overlapping
26733 the one we are currently highlighting, we have to
26734 check if we enter the overlapping overlay, and then
26735 highlight only that. */
26736 || (OVERLAYP (hlinfo->mouse_face_overlay)
26737 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26739 /* Find the highest priority overlay with a mouse-face. */
26740 Lisp_Object overlay = Qnil;
26741 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26743 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26744 if (!NILP (mouse_face))
26745 overlay = overlay_vec[i];
26748 /* If we're highlighting the same overlay as before, there's
26749 no need to do that again. */
26750 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26751 goto check_help_echo;
26752 hlinfo->mouse_face_overlay = overlay;
26754 /* Clear the display of the old active region, if any. */
26755 if (clear_mouse_face (hlinfo))
26756 cursor = No_Cursor;
26758 /* If no overlay applies, get a text property. */
26759 if (NILP (overlay))
26760 mouse_face = Fget_text_property (position, Qmouse_face, object);
26762 /* Next, compute the bounds of the mouse highlighting and
26763 display it. */
26764 if (!NILP (mouse_face) && STRINGP (object))
26766 /* The mouse-highlighting comes from a display string
26767 with a mouse-face. */
26768 Lisp_Object s, e;
26769 EMACS_INT ignore;
26771 s = Fprevious_single_property_change
26772 (make_number (pos + 1), Qmouse_face, object, Qnil);
26773 e = Fnext_single_property_change
26774 (position, Qmouse_face, object, Qnil);
26775 if (NILP (s))
26776 s = make_number (0);
26777 if (NILP (e))
26778 e = make_number (SCHARS (object) - 1);
26779 mouse_face_from_string_pos (w, hlinfo, object,
26780 XINT (s), XINT (e));
26781 hlinfo->mouse_face_past_end = 0;
26782 hlinfo->mouse_face_window = window;
26783 hlinfo->mouse_face_face_id
26784 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26785 glyph->face_id, 1);
26786 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26787 cursor = No_Cursor;
26789 else
26791 /* The mouse-highlighting, if any, comes from an overlay
26792 or text property in the buffer. */
26793 Lisp_Object buffer IF_LINT (= Qnil);
26794 Lisp_Object cover_string IF_LINT (= Qnil);
26796 if (STRINGP (object))
26798 /* If we are on a display string with no mouse-face,
26799 check if the text under it has one. */
26800 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26801 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26802 pos = string_buffer_position (object, start);
26803 if (pos > 0)
26805 mouse_face = get_char_property_and_overlay
26806 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26807 buffer = w->buffer;
26808 cover_string = object;
26811 else
26813 buffer = object;
26814 cover_string = Qnil;
26817 if (!NILP (mouse_face))
26819 Lisp_Object before, after;
26820 Lisp_Object before_string, after_string;
26821 /* To correctly find the limits of mouse highlight
26822 in a bidi-reordered buffer, we must not use the
26823 optimization of limiting the search in
26824 previous-single-property-change and
26825 next-single-property-change, because
26826 rows_from_pos_range needs the real start and end
26827 positions to DTRT in this case. That's because
26828 the first row visible in a window does not
26829 necessarily display the character whose position
26830 is the smallest. */
26831 Lisp_Object lim1 =
26832 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26833 ? Fmarker_position (w->start)
26834 : Qnil;
26835 Lisp_Object lim2 =
26836 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26837 ? make_number (BUF_Z (XBUFFER (buffer))
26838 - XFASTINT (w->window_end_pos))
26839 : Qnil;
26841 if (NILP (overlay))
26843 /* Handle the text property case. */
26844 before = Fprevious_single_property_change
26845 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26846 after = Fnext_single_property_change
26847 (make_number (pos), Qmouse_face, buffer, lim2);
26848 before_string = after_string = Qnil;
26850 else
26852 /* Handle the overlay case. */
26853 before = Foverlay_start (overlay);
26854 after = Foverlay_end (overlay);
26855 before_string = Foverlay_get (overlay, Qbefore_string);
26856 after_string = Foverlay_get (overlay, Qafter_string);
26858 if (!STRINGP (before_string)) before_string = Qnil;
26859 if (!STRINGP (after_string)) after_string = Qnil;
26862 mouse_face_from_buffer_pos (window, hlinfo, pos,
26863 XFASTINT (before),
26864 XFASTINT (after),
26865 before_string, after_string,
26866 cover_string);
26867 cursor = No_Cursor;
26872 check_help_echo:
26874 /* Look for a `help-echo' property. */
26875 if (NILP (help_echo_string)) {
26876 Lisp_Object help, overlay;
26878 /* Check overlays first. */
26879 help = overlay = Qnil;
26880 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26882 overlay = overlay_vec[i];
26883 help = Foverlay_get (overlay, Qhelp_echo);
26886 if (!NILP (help))
26888 help_echo_string = help;
26889 help_echo_window = window;
26890 help_echo_object = overlay;
26891 help_echo_pos = pos;
26893 else
26895 Lisp_Object obj = glyph->object;
26896 EMACS_INT charpos = glyph->charpos;
26898 /* Try text properties. */
26899 if (STRINGP (obj)
26900 && charpos >= 0
26901 && charpos < SCHARS (obj))
26903 help = Fget_text_property (make_number (charpos),
26904 Qhelp_echo, obj);
26905 if (NILP (help))
26907 /* If the string itself doesn't specify a help-echo,
26908 see if the buffer text ``under'' it does. */
26909 struct glyph_row *r
26910 = MATRIX_ROW (w->current_matrix, vpos);
26911 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26912 EMACS_INT p = string_buffer_position (obj, start);
26913 if (p > 0)
26915 help = Fget_char_property (make_number (p),
26916 Qhelp_echo, w->buffer);
26917 if (!NILP (help))
26919 charpos = p;
26920 obj = w->buffer;
26925 else if (BUFFERP (obj)
26926 && charpos >= BEGV
26927 && charpos < ZV)
26928 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26929 obj);
26931 if (!NILP (help))
26933 help_echo_string = help;
26934 help_echo_window = window;
26935 help_echo_object = obj;
26936 help_echo_pos = charpos;
26941 #ifdef HAVE_WINDOW_SYSTEM
26942 /* Look for a `pointer' property. */
26943 if (FRAME_WINDOW_P (f) && NILP (pointer))
26945 /* Check overlays first. */
26946 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26947 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26949 if (NILP (pointer))
26951 Lisp_Object obj = glyph->object;
26952 EMACS_INT charpos = glyph->charpos;
26954 /* Try text properties. */
26955 if (STRINGP (obj)
26956 && charpos >= 0
26957 && charpos < SCHARS (obj))
26959 pointer = Fget_text_property (make_number (charpos),
26960 Qpointer, obj);
26961 if (NILP (pointer))
26963 /* If the string itself doesn't specify a pointer,
26964 see if the buffer text ``under'' it does. */
26965 struct glyph_row *r
26966 = MATRIX_ROW (w->current_matrix, vpos);
26967 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26968 EMACS_INT p = string_buffer_position (obj, start);
26969 if (p > 0)
26970 pointer = Fget_char_property (make_number (p),
26971 Qpointer, w->buffer);
26974 else if (BUFFERP (obj)
26975 && charpos >= BEGV
26976 && charpos < ZV)
26977 pointer = Fget_text_property (make_number (charpos),
26978 Qpointer, obj);
26981 #endif /* HAVE_WINDOW_SYSTEM */
26983 BEGV = obegv;
26984 ZV = ozv;
26985 current_buffer = obuf;
26988 set_cursor:
26990 #ifdef HAVE_WINDOW_SYSTEM
26991 if (FRAME_WINDOW_P (f))
26992 define_frame_cursor1 (f, cursor, pointer);
26993 #else
26994 /* This is here to prevent a compiler error, about "label at end of
26995 compound statement". */
26996 return;
26997 #endif
27001 /* EXPORT for RIF:
27002 Clear any mouse-face on window W. This function is part of the
27003 redisplay interface, and is called from try_window_id and similar
27004 functions to ensure the mouse-highlight is off. */
27006 void
27007 x_clear_window_mouse_face (struct window *w)
27009 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27010 Lisp_Object window;
27012 BLOCK_INPUT;
27013 XSETWINDOW (window, w);
27014 if (EQ (window, hlinfo->mouse_face_window))
27015 clear_mouse_face (hlinfo);
27016 UNBLOCK_INPUT;
27020 /* EXPORT:
27021 Just discard the mouse face information for frame F, if any.
27022 This is used when the size of F is changed. */
27024 void
27025 cancel_mouse_face (struct frame *f)
27027 Lisp_Object window;
27028 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27030 window = hlinfo->mouse_face_window;
27031 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27033 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27034 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27035 hlinfo->mouse_face_window = Qnil;
27041 /***********************************************************************
27042 Exposure Events
27043 ***********************************************************************/
27045 #ifdef HAVE_WINDOW_SYSTEM
27047 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27048 which intersects rectangle R. R is in window-relative coordinates. */
27050 static void
27051 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27052 enum glyph_row_area area)
27054 struct glyph *first = row->glyphs[area];
27055 struct glyph *end = row->glyphs[area] + row->used[area];
27056 struct glyph *last;
27057 int first_x, start_x, x;
27059 if (area == TEXT_AREA && row->fill_line_p)
27060 /* If row extends face to end of line write the whole line. */
27061 draw_glyphs (w, 0, row, area,
27062 0, row->used[area],
27063 DRAW_NORMAL_TEXT, 0);
27064 else
27066 /* Set START_X to the window-relative start position for drawing glyphs of
27067 AREA. The first glyph of the text area can be partially visible.
27068 The first glyphs of other areas cannot. */
27069 start_x = window_box_left_offset (w, area);
27070 x = start_x;
27071 if (area == TEXT_AREA)
27072 x += row->x;
27074 /* Find the first glyph that must be redrawn. */
27075 while (first < end
27076 && x + first->pixel_width < r->x)
27078 x += first->pixel_width;
27079 ++first;
27082 /* Find the last one. */
27083 last = first;
27084 first_x = x;
27085 while (last < end
27086 && x < r->x + r->width)
27088 x += last->pixel_width;
27089 ++last;
27092 /* Repaint. */
27093 if (last > first)
27094 draw_glyphs (w, first_x - start_x, row, area,
27095 first - row->glyphs[area], last - row->glyphs[area],
27096 DRAW_NORMAL_TEXT, 0);
27101 /* Redraw the parts of the glyph row ROW on window W intersecting
27102 rectangle R. R is in window-relative coordinates. Value is
27103 non-zero if mouse-face was overwritten. */
27105 static int
27106 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27108 xassert (row->enabled_p);
27110 if (row->mode_line_p || w->pseudo_window_p)
27111 draw_glyphs (w, 0, row, TEXT_AREA,
27112 0, row->used[TEXT_AREA],
27113 DRAW_NORMAL_TEXT, 0);
27114 else
27116 if (row->used[LEFT_MARGIN_AREA])
27117 expose_area (w, row, r, LEFT_MARGIN_AREA);
27118 if (row->used[TEXT_AREA])
27119 expose_area (w, row, r, TEXT_AREA);
27120 if (row->used[RIGHT_MARGIN_AREA])
27121 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27122 draw_row_fringe_bitmaps (w, row);
27125 return row->mouse_face_p;
27129 /* Redraw those parts of glyphs rows during expose event handling that
27130 overlap other rows. Redrawing of an exposed line writes over parts
27131 of lines overlapping that exposed line; this function fixes that.
27133 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27134 row in W's current matrix that is exposed and overlaps other rows.
27135 LAST_OVERLAPPING_ROW is the last such row. */
27137 static void
27138 expose_overlaps (struct window *w,
27139 struct glyph_row *first_overlapping_row,
27140 struct glyph_row *last_overlapping_row,
27141 XRectangle *r)
27143 struct glyph_row *row;
27145 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27146 if (row->overlapping_p)
27148 xassert (row->enabled_p && !row->mode_line_p);
27150 row->clip = r;
27151 if (row->used[LEFT_MARGIN_AREA])
27152 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27154 if (row->used[TEXT_AREA])
27155 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27157 if (row->used[RIGHT_MARGIN_AREA])
27158 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27159 row->clip = NULL;
27164 /* Return non-zero if W's cursor intersects rectangle R. */
27166 static int
27167 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27169 XRectangle cr, result;
27170 struct glyph *cursor_glyph;
27171 struct glyph_row *row;
27173 if (w->phys_cursor.vpos >= 0
27174 && w->phys_cursor.vpos < w->current_matrix->nrows
27175 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27176 row->enabled_p)
27177 && row->cursor_in_fringe_p)
27179 /* Cursor is in the fringe. */
27180 cr.x = window_box_right_offset (w,
27181 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27182 ? RIGHT_MARGIN_AREA
27183 : TEXT_AREA));
27184 cr.y = row->y;
27185 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27186 cr.height = row->height;
27187 return x_intersect_rectangles (&cr, r, &result);
27190 cursor_glyph = get_phys_cursor_glyph (w);
27191 if (cursor_glyph)
27193 /* r is relative to W's box, but w->phys_cursor.x is relative
27194 to left edge of W's TEXT area. Adjust it. */
27195 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27196 cr.y = w->phys_cursor.y;
27197 cr.width = cursor_glyph->pixel_width;
27198 cr.height = w->phys_cursor_height;
27199 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27200 I assume the effect is the same -- and this is portable. */
27201 return x_intersect_rectangles (&cr, r, &result);
27203 /* If we don't understand the format, pretend we're not in the hot-spot. */
27204 return 0;
27208 /* EXPORT:
27209 Draw a vertical window border to the right of window W if W doesn't
27210 have vertical scroll bars. */
27212 void
27213 x_draw_vertical_border (struct window *w)
27215 struct frame *f = XFRAME (WINDOW_FRAME (w));
27217 /* We could do better, if we knew what type of scroll-bar the adjacent
27218 windows (on either side) have... But we don't :-(
27219 However, I think this works ok. ++KFS 2003-04-25 */
27221 /* Redraw borders between horizontally adjacent windows. Don't
27222 do it for frames with vertical scroll bars because either the
27223 right scroll bar of a window, or the left scroll bar of its
27224 neighbor will suffice as a border. */
27225 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27226 return;
27228 if (!WINDOW_RIGHTMOST_P (w)
27229 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27231 int x0, x1, y0, y1;
27233 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27234 y1 -= 1;
27236 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27237 x1 -= 1;
27239 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27241 else if (!WINDOW_LEFTMOST_P (w)
27242 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27244 int x0, x1, y0, y1;
27246 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27247 y1 -= 1;
27249 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27250 x0 -= 1;
27252 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27257 /* Redraw the part of window W intersection rectangle FR. Pixel
27258 coordinates in FR are frame-relative. Call this function with
27259 input blocked. Value is non-zero if the exposure overwrites
27260 mouse-face. */
27262 static int
27263 expose_window (struct window *w, XRectangle *fr)
27265 struct frame *f = XFRAME (w->frame);
27266 XRectangle wr, r;
27267 int mouse_face_overwritten_p = 0;
27269 /* If window is not yet fully initialized, do nothing. This can
27270 happen when toolkit scroll bars are used and a window is split.
27271 Reconfiguring the scroll bar will generate an expose for a newly
27272 created window. */
27273 if (w->current_matrix == NULL)
27274 return 0;
27276 /* When we're currently updating the window, display and current
27277 matrix usually don't agree. Arrange for a thorough display
27278 later. */
27279 if (w == updated_window)
27281 SET_FRAME_GARBAGED (f);
27282 return 0;
27285 /* Frame-relative pixel rectangle of W. */
27286 wr.x = WINDOW_LEFT_EDGE_X (w);
27287 wr.y = WINDOW_TOP_EDGE_Y (w);
27288 wr.width = WINDOW_TOTAL_WIDTH (w);
27289 wr.height = WINDOW_TOTAL_HEIGHT (w);
27291 if (x_intersect_rectangles (fr, &wr, &r))
27293 int yb = window_text_bottom_y (w);
27294 struct glyph_row *row;
27295 int cursor_cleared_p;
27296 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27298 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27299 r.x, r.y, r.width, r.height));
27301 /* Convert to window coordinates. */
27302 r.x -= WINDOW_LEFT_EDGE_X (w);
27303 r.y -= WINDOW_TOP_EDGE_Y (w);
27305 /* Turn off the cursor. */
27306 if (!w->pseudo_window_p
27307 && phys_cursor_in_rect_p (w, &r))
27309 x_clear_cursor (w);
27310 cursor_cleared_p = 1;
27312 else
27313 cursor_cleared_p = 0;
27315 /* Update lines intersecting rectangle R. */
27316 first_overlapping_row = last_overlapping_row = NULL;
27317 for (row = w->current_matrix->rows;
27318 row->enabled_p;
27319 ++row)
27321 int y0 = row->y;
27322 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27324 if ((y0 >= r.y && y0 < r.y + r.height)
27325 || (y1 > r.y && y1 < r.y + r.height)
27326 || (r.y >= y0 && r.y < y1)
27327 || (r.y + r.height > y0 && r.y + r.height < y1))
27329 /* A header line may be overlapping, but there is no need
27330 to fix overlapping areas for them. KFS 2005-02-12 */
27331 if (row->overlapping_p && !row->mode_line_p)
27333 if (first_overlapping_row == NULL)
27334 first_overlapping_row = row;
27335 last_overlapping_row = row;
27338 row->clip = fr;
27339 if (expose_line (w, row, &r))
27340 mouse_face_overwritten_p = 1;
27341 row->clip = NULL;
27343 else if (row->overlapping_p)
27345 /* We must redraw a row overlapping the exposed area. */
27346 if (y0 < r.y
27347 ? y0 + row->phys_height > r.y
27348 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27350 if (first_overlapping_row == NULL)
27351 first_overlapping_row = row;
27352 last_overlapping_row = row;
27356 if (y1 >= yb)
27357 break;
27360 /* Display the mode line if there is one. */
27361 if (WINDOW_WANTS_MODELINE_P (w)
27362 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27363 row->enabled_p)
27364 && row->y < r.y + r.height)
27366 if (expose_line (w, row, &r))
27367 mouse_face_overwritten_p = 1;
27370 if (!w->pseudo_window_p)
27372 /* Fix the display of overlapping rows. */
27373 if (first_overlapping_row)
27374 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27375 fr);
27377 /* Draw border between windows. */
27378 x_draw_vertical_border (w);
27380 /* Turn the cursor on again. */
27381 if (cursor_cleared_p)
27382 update_window_cursor (w, 1);
27386 return mouse_face_overwritten_p;
27391 /* Redraw (parts) of all windows in the window tree rooted at W that
27392 intersect R. R contains frame pixel coordinates. Value is
27393 non-zero if the exposure overwrites mouse-face. */
27395 static int
27396 expose_window_tree (struct window *w, XRectangle *r)
27398 struct frame *f = XFRAME (w->frame);
27399 int mouse_face_overwritten_p = 0;
27401 while (w && !FRAME_GARBAGED_P (f))
27403 if (!NILP (w->hchild))
27404 mouse_face_overwritten_p
27405 |= expose_window_tree (XWINDOW (w->hchild), r);
27406 else if (!NILP (w->vchild))
27407 mouse_face_overwritten_p
27408 |= expose_window_tree (XWINDOW (w->vchild), r);
27409 else
27410 mouse_face_overwritten_p |= expose_window (w, r);
27412 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27415 return mouse_face_overwritten_p;
27419 /* EXPORT:
27420 Redisplay an exposed area of frame F. X and Y are the upper-left
27421 corner of the exposed rectangle. W and H are width and height of
27422 the exposed area. All are pixel values. W or H zero means redraw
27423 the entire frame. */
27425 void
27426 expose_frame (struct frame *f, int x, int y, int w, int h)
27428 XRectangle r;
27429 int mouse_face_overwritten_p = 0;
27431 TRACE ((stderr, "expose_frame "));
27433 /* No need to redraw if frame will be redrawn soon. */
27434 if (FRAME_GARBAGED_P (f))
27436 TRACE ((stderr, " garbaged\n"));
27437 return;
27440 /* If basic faces haven't been realized yet, there is no point in
27441 trying to redraw anything. This can happen when we get an expose
27442 event while Emacs is starting, e.g. by moving another window. */
27443 if (FRAME_FACE_CACHE (f) == NULL
27444 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27446 TRACE ((stderr, " no faces\n"));
27447 return;
27450 if (w == 0 || h == 0)
27452 r.x = r.y = 0;
27453 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27454 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27456 else
27458 r.x = x;
27459 r.y = y;
27460 r.width = w;
27461 r.height = h;
27464 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27465 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27467 if (WINDOWP (f->tool_bar_window))
27468 mouse_face_overwritten_p
27469 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27471 #ifdef HAVE_X_WINDOWS
27472 #ifndef MSDOS
27473 #ifndef USE_X_TOOLKIT
27474 if (WINDOWP (f->menu_bar_window))
27475 mouse_face_overwritten_p
27476 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27477 #endif /* not USE_X_TOOLKIT */
27478 #endif
27479 #endif
27481 /* Some window managers support a focus-follows-mouse style with
27482 delayed raising of frames. Imagine a partially obscured frame,
27483 and moving the mouse into partially obscured mouse-face on that
27484 frame. The visible part of the mouse-face will be highlighted,
27485 then the WM raises the obscured frame. With at least one WM, KDE
27486 2.1, Emacs is not getting any event for the raising of the frame
27487 (even tried with SubstructureRedirectMask), only Expose events.
27488 These expose events will draw text normally, i.e. not
27489 highlighted. Which means we must redo the highlight here.
27490 Subsume it under ``we love X''. --gerd 2001-08-15 */
27491 /* Included in Windows version because Windows most likely does not
27492 do the right thing if any third party tool offers
27493 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27494 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27496 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27497 if (f == hlinfo->mouse_face_mouse_frame)
27499 int mouse_x = hlinfo->mouse_face_mouse_x;
27500 int mouse_y = hlinfo->mouse_face_mouse_y;
27501 clear_mouse_face (hlinfo);
27502 note_mouse_highlight (f, mouse_x, mouse_y);
27508 /* EXPORT:
27509 Determine the intersection of two rectangles R1 and R2. Return
27510 the intersection in *RESULT. Value is non-zero if RESULT is not
27511 empty. */
27514 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27516 XRectangle *left, *right;
27517 XRectangle *upper, *lower;
27518 int intersection_p = 0;
27520 /* Rearrange so that R1 is the left-most rectangle. */
27521 if (r1->x < r2->x)
27522 left = r1, right = r2;
27523 else
27524 left = r2, right = r1;
27526 /* X0 of the intersection is right.x0, if this is inside R1,
27527 otherwise there is no intersection. */
27528 if (right->x <= left->x + left->width)
27530 result->x = right->x;
27532 /* The right end of the intersection is the minimum of
27533 the right ends of left and right. */
27534 result->width = (min (left->x + left->width, right->x + right->width)
27535 - result->x);
27537 /* Same game for Y. */
27538 if (r1->y < r2->y)
27539 upper = r1, lower = r2;
27540 else
27541 upper = r2, lower = r1;
27543 /* The upper end of the intersection is lower.y0, if this is inside
27544 of upper. Otherwise, there is no intersection. */
27545 if (lower->y <= upper->y + upper->height)
27547 result->y = lower->y;
27549 /* The lower end of the intersection is the minimum of the lower
27550 ends of upper and lower. */
27551 result->height = (min (lower->y + lower->height,
27552 upper->y + upper->height)
27553 - result->y);
27554 intersection_p = 1;
27558 return intersection_p;
27561 #endif /* HAVE_WINDOW_SYSTEM */
27564 /***********************************************************************
27565 Initialization
27566 ***********************************************************************/
27568 void
27569 syms_of_xdisp (void)
27571 Vwith_echo_area_save_vector = Qnil;
27572 staticpro (&Vwith_echo_area_save_vector);
27574 Vmessage_stack = Qnil;
27575 staticpro (&Vmessage_stack);
27577 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
27579 message_dolog_marker1 = Fmake_marker ();
27580 staticpro (&message_dolog_marker1);
27581 message_dolog_marker2 = Fmake_marker ();
27582 staticpro (&message_dolog_marker2);
27583 message_dolog_marker3 = Fmake_marker ();
27584 staticpro (&message_dolog_marker3);
27586 #if GLYPH_DEBUG
27587 defsubr (&Sdump_frame_glyph_matrix);
27588 defsubr (&Sdump_glyph_matrix);
27589 defsubr (&Sdump_glyph_row);
27590 defsubr (&Sdump_tool_bar_row);
27591 defsubr (&Strace_redisplay);
27592 defsubr (&Strace_to_stderr);
27593 #endif
27594 #ifdef HAVE_WINDOW_SYSTEM
27595 defsubr (&Stool_bar_lines_needed);
27596 defsubr (&Slookup_image_map);
27597 #endif
27598 defsubr (&Sformat_mode_line);
27599 defsubr (&Sinvisible_p);
27600 defsubr (&Scurrent_bidi_paragraph_direction);
27602 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
27603 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
27604 DEFSYM (Qoverriding_local_map, "overriding-local-map");
27605 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
27606 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
27607 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
27608 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
27609 DEFSYM (Qeval, "eval");
27610 DEFSYM (QCdata, ":data");
27611 DEFSYM (Qdisplay, "display");
27612 DEFSYM (Qspace_width, "space-width");
27613 DEFSYM (Qraise, "raise");
27614 DEFSYM (Qslice, "slice");
27615 DEFSYM (Qspace, "space");
27616 DEFSYM (Qmargin, "margin");
27617 DEFSYM (Qpointer, "pointer");
27618 DEFSYM (Qleft_margin, "left-margin");
27619 DEFSYM (Qright_margin, "right-margin");
27620 DEFSYM (Qcenter, "center");
27621 DEFSYM (Qline_height, "line-height");
27622 DEFSYM (QCalign_to, ":align-to");
27623 DEFSYM (QCrelative_width, ":relative-width");
27624 DEFSYM (QCrelative_height, ":relative-height");
27625 DEFSYM (QCeval, ":eval");
27626 DEFSYM (QCpropertize, ":propertize");
27627 DEFSYM (QCfile, ":file");
27628 DEFSYM (Qfontified, "fontified");
27629 DEFSYM (Qfontification_functions, "fontification-functions");
27630 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
27631 DEFSYM (Qescape_glyph, "escape-glyph");
27632 DEFSYM (Qnobreak_space, "nobreak-space");
27633 DEFSYM (Qimage, "image");
27634 DEFSYM (Qtext, "text");
27635 DEFSYM (Qboth, "both");
27636 DEFSYM (Qboth_horiz, "both-horiz");
27637 DEFSYM (Qtext_image_horiz, "text-image-horiz");
27638 DEFSYM (QCmap, ":map");
27639 DEFSYM (QCpointer, ":pointer");
27640 DEFSYM (Qrect, "rect");
27641 DEFSYM (Qcircle, "circle");
27642 DEFSYM (Qpoly, "poly");
27643 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
27644 DEFSYM (Qgrow_only, "grow-only");
27645 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
27646 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
27647 DEFSYM (Qposition, "position");
27648 DEFSYM (Qbuffer_position, "buffer-position");
27649 DEFSYM (Qobject, "object");
27650 DEFSYM (Qbar, "bar");
27651 DEFSYM (Qhbar, "hbar");
27652 DEFSYM (Qbox, "box");
27653 DEFSYM (Qhollow, "hollow");
27654 DEFSYM (Qhand, "hand");
27655 DEFSYM (Qarrow, "arrow");
27656 DEFSYM (Qtext, "text");
27657 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
27659 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27660 Fcons (intern_c_string ("void-variable"), Qnil)),
27661 Qnil);
27662 staticpro (&list_of_error);
27664 DEFSYM (Qlast_arrow_position, "last-arrow-position");
27665 DEFSYM (Qlast_arrow_string, "last-arrow-string");
27666 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
27667 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
27669 echo_buffer[0] = echo_buffer[1] = Qnil;
27670 staticpro (&echo_buffer[0]);
27671 staticpro (&echo_buffer[1]);
27673 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27674 staticpro (&echo_area_buffer[0]);
27675 staticpro (&echo_area_buffer[1]);
27677 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27678 staticpro (&Vmessages_buffer_name);
27680 mode_line_proptrans_alist = Qnil;
27681 staticpro (&mode_line_proptrans_alist);
27682 mode_line_string_list = Qnil;
27683 staticpro (&mode_line_string_list);
27684 mode_line_string_face = Qnil;
27685 staticpro (&mode_line_string_face);
27686 mode_line_string_face_prop = Qnil;
27687 staticpro (&mode_line_string_face_prop);
27688 Vmode_line_unwind_vector = Qnil;
27689 staticpro (&Vmode_line_unwind_vector);
27691 help_echo_string = Qnil;
27692 staticpro (&help_echo_string);
27693 help_echo_object = Qnil;
27694 staticpro (&help_echo_object);
27695 help_echo_window = Qnil;
27696 staticpro (&help_echo_window);
27697 previous_help_echo_string = Qnil;
27698 staticpro (&previous_help_echo_string);
27699 help_echo_pos = -1;
27701 DEFSYM (Qright_to_left, "right-to-left");
27702 DEFSYM (Qleft_to_right, "left-to-right");
27704 #ifdef HAVE_WINDOW_SYSTEM
27705 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27706 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27707 For example, if a block cursor is over a tab, it will be drawn as
27708 wide as that tab on the display. */);
27709 x_stretch_cursor_p = 0;
27710 #endif
27712 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27713 doc: /* *Non-nil means highlight trailing whitespace.
27714 The face used for trailing whitespace is `trailing-whitespace'. */);
27715 Vshow_trailing_whitespace = Qnil;
27717 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27718 doc: /* *Control highlighting of nobreak space and soft hyphen.
27719 A value of t means highlight the character itself (for nobreak space,
27720 use face `nobreak-space').
27721 A value of nil means no highlighting.
27722 Other values mean display the escape glyph followed by an ordinary
27723 space or ordinary hyphen. */);
27724 Vnobreak_char_display = Qt;
27726 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27727 doc: /* *The pointer shape to show in void text areas.
27728 A value of nil means to show the text pointer. Other options are `arrow',
27729 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27730 Vvoid_text_area_pointer = Qarrow;
27732 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27733 doc: /* Non-nil means don't actually do any redisplay.
27734 This is used for internal purposes. */);
27735 Vinhibit_redisplay = Qnil;
27737 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27738 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27739 Vglobal_mode_string = Qnil;
27741 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27742 doc: /* Marker for where to display an arrow on top of the buffer text.
27743 This must be the beginning of a line in order to work.
27744 See also `overlay-arrow-string'. */);
27745 Voverlay_arrow_position = Qnil;
27747 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27748 doc: /* String to display as an arrow in non-window frames.
27749 See also `overlay-arrow-position'. */);
27750 Voverlay_arrow_string = make_pure_c_string ("=>");
27752 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27753 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27754 The symbols on this list are examined during redisplay to determine
27755 where to display overlay arrows. */);
27756 Voverlay_arrow_variable_list
27757 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27759 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27760 doc: /* *The number of lines to try scrolling a window by when point moves out.
27761 If that fails to bring point back on frame, point is centered instead.
27762 If this is zero, point is always centered after it moves off frame.
27763 If you want scrolling to always be a line at a time, you should set
27764 `scroll-conservatively' to a large value rather than set this to 1. */);
27766 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27767 doc: /* *Scroll up to this many lines, to bring point back on screen.
27768 If point moves off-screen, redisplay will scroll by up to
27769 `scroll-conservatively' lines in order to bring point just barely
27770 onto the screen again. If that cannot be done, then redisplay
27771 recenters point as usual.
27773 If the value is greater than 100, redisplay will never recenter point,
27774 but will always scroll just enough text to bring point into view, even
27775 if you move far away.
27777 A value of zero means always recenter point if it moves off screen. */);
27778 scroll_conservatively = 0;
27780 DEFVAR_INT ("scroll-margin", scroll_margin,
27781 doc: /* *Number of lines of margin at the top and bottom of a window.
27782 Recenter the window whenever point gets within this many lines
27783 of the top or bottom of the window. */);
27784 scroll_margin = 0;
27786 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27787 doc: /* Pixels per inch value for non-window system displays.
27788 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27789 Vdisplay_pixels_per_inch = make_float (72.0);
27791 #if GLYPH_DEBUG
27792 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27793 #endif
27795 DEFVAR_LISP ("truncate-partial-width-windows",
27796 Vtruncate_partial_width_windows,
27797 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27798 For an integer value, truncate lines in each window narrower than the
27799 full frame width, provided the window width is less than that integer;
27800 otherwise, respect the value of `truncate-lines'.
27802 For any other non-nil value, truncate lines in all windows that do
27803 not span the full frame width.
27805 A value of nil means to respect the value of `truncate-lines'.
27807 If `word-wrap' is enabled, you might want to reduce this. */);
27808 Vtruncate_partial_width_windows = make_number (50);
27810 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27811 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27812 Any other value means to use the appropriate face, `mode-line',
27813 `header-line', or `menu' respectively. */);
27814 mode_line_inverse_video = 1;
27816 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27817 doc: /* *Maximum buffer size for which line number should be displayed.
27818 If the buffer is bigger than this, the line number does not appear
27819 in the mode line. A value of nil means no limit. */);
27820 Vline_number_display_limit = Qnil;
27822 DEFVAR_INT ("line-number-display-limit-width",
27823 line_number_display_limit_width,
27824 doc: /* *Maximum line width (in characters) for line number display.
27825 If the average length of the lines near point is bigger than this, then the
27826 line number may be omitted from the mode line. */);
27827 line_number_display_limit_width = 200;
27829 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27830 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27831 highlight_nonselected_windows = 0;
27833 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27834 doc: /* Non-nil if more than one frame is visible on this display.
27835 Minibuffer-only frames don't count, but iconified frames do.
27836 This variable is not guaranteed to be accurate except while processing
27837 `frame-title-format' and `icon-title-format'. */);
27839 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27840 doc: /* Template for displaying the title bar of visible frames.
27841 \(Assuming the window manager supports this feature.)
27843 This variable has the same structure as `mode-line-format', except that
27844 the %c and %l constructs are ignored. It is used only on frames for
27845 which no explicit name has been set \(see `modify-frame-parameters'). */);
27847 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27848 doc: /* Template for displaying the title bar of an iconified frame.
27849 \(Assuming the window manager supports this feature.)
27850 This variable has the same structure as `mode-line-format' (which see),
27851 and is used only on frames for which no explicit name has been set
27852 \(see `modify-frame-parameters'). */);
27853 Vicon_title_format
27854 = Vframe_title_format
27855 = pure_cons (intern_c_string ("multiple-frames"),
27856 pure_cons (make_pure_c_string ("%b"),
27857 pure_cons (pure_cons (empty_unibyte_string,
27858 pure_cons (intern_c_string ("invocation-name"),
27859 pure_cons (make_pure_c_string ("@"),
27860 pure_cons (intern_c_string ("system-name"),
27861 Qnil)))),
27862 Qnil)));
27864 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27865 doc: /* Maximum number of lines to keep in the message log buffer.
27866 If nil, disable message logging. If t, log messages but don't truncate
27867 the buffer when it becomes large. */);
27868 Vmessage_log_max = make_number (100);
27870 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27871 doc: /* Functions called before redisplay, if window sizes have changed.
27872 The value should be a list of functions that take one argument.
27873 Just before redisplay, for each frame, if any of its windows have changed
27874 size since the last redisplay, or have been split or deleted,
27875 all the functions in the list are called, with the frame as argument. */);
27876 Vwindow_size_change_functions = Qnil;
27878 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27879 doc: /* List of functions to call before redisplaying a window with scrolling.
27880 Each function is called with two arguments, the window and its new
27881 display-start position. Note that these functions are also called by
27882 `set-window-buffer'. Also note that the value of `window-end' is not
27883 valid when these functions are called. */);
27884 Vwindow_scroll_functions = Qnil;
27886 DEFVAR_LISP ("window-text-change-functions",
27887 Vwindow_text_change_functions,
27888 doc: /* Functions to call in redisplay when text in the window might change. */);
27889 Vwindow_text_change_functions = Qnil;
27891 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27892 doc: /* Functions called when redisplay of a window reaches the end trigger.
27893 Each function is called with two arguments, the window and the end trigger value.
27894 See `set-window-redisplay-end-trigger'. */);
27895 Vredisplay_end_trigger_functions = Qnil;
27897 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27898 doc: /* *Non-nil means autoselect window with mouse pointer.
27899 If nil, do not autoselect windows.
27900 A positive number means delay autoselection by that many seconds: a
27901 window is autoselected only after the mouse has remained in that
27902 window for the duration of the delay.
27903 A negative number has a similar effect, but causes windows to be
27904 autoselected only after the mouse has stopped moving. \(Because of
27905 the way Emacs compares mouse events, you will occasionally wait twice
27906 that time before the window gets selected.\)
27907 Any other value means to autoselect window instantaneously when the
27908 mouse pointer enters it.
27910 Autoselection selects the minibuffer only if it is active, and never
27911 unselects the minibuffer if it is active.
27913 When customizing this variable make sure that the actual value of
27914 `focus-follows-mouse' matches the behavior of your window manager. */);
27915 Vmouse_autoselect_window = Qnil;
27917 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27918 doc: /* *Non-nil means automatically resize tool-bars.
27919 This dynamically changes the tool-bar's height to the minimum height
27920 that is needed to make all tool-bar items visible.
27921 If value is `grow-only', the tool-bar's height is only increased
27922 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27923 Vauto_resize_tool_bars = Qt;
27925 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27926 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27927 auto_raise_tool_bar_buttons_p = 1;
27929 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27930 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27931 make_cursor_line_fully_visible_p = 1;
27933 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27934 doc: /* *Border below tool-bar in pixels.
27935 If an integer, use it as the height of the border.
27936 If it is one of `internal-border-width' or `border-width', use the
27937 value of the corresponding frame parameter.
27938 Otherwise, no border is added below the tool-bar. */);
27939 Vtool_bar_border = Qinternal_border_width;
27941 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27942 doc: /* *Margin around tool-bar buttons in pixels.
27943 If an integer, use that for both horizontal and vertical margins.
27944 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27945 HORZ specifying the horizontal margin, and VERT specifying the
27946 vertical margin. */);
27947 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27949 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27950 doc: /* *Relief thickness of tool-bar buttons. */);
27951 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27953 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27954 doc: /* Tool bar style to use.
27955 It can be one of
27956 image - show images only
27957 text - show text only
27958 both - show both, text below image
27959 both-horiz - show text to the right of the image
27960 text-image-horiz - show text to the left of the image
27961 any other - use system default or image if no system default. */);
27962 Vtool_bar_style = Qnil;
27964 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27965 doc: /* *Maximum number of characters a label can have to be shown.
27966 The tool bar style must also show labels for this to have any effect, see
27967 `tool-bar-style'. */);
27968 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27970 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27971 doc: /* List of functions to call to fontify regions of text.
27972 Each function is called with one argument POS. Functions must
27973 fontify a region starting at POS in the current buffer, and give
27974 fontified regions the property `fontified'. */);
27975 Vfontification_functions = Qnil;
27976 Fmake_variable_buffer_local (Qfontification_functions);
27978 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27979 unibyte_display_via_language_environment,
27980 doc: /* *Non-nil means display unibyte text according to language environment.
27981 Specifically, this means that raw bytes in the range 160-255 decimal
27982 are displayed by converting them to the equivalent multibyte characters
27983 according to the current language environment. As a result, they are
27984 displayed according to the current fontset.
27986 Note that this variable affects only how these bytes are displayed,
27987 but does not change the fact they are interpreted as raw bytes. */);
27988 unibyte_display_via_language_environment = 0;
27990 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27991 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
27992 If a float, it specifies a fraction of the mini-window frame's height.
27993 If an integer, it specifies a number of lines. */);
27994 Vmax_mini_window_height = make_float (0.25);
27996 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27997 doc: /* How to resize mini-windows (the minibuffer and the echo area).
27998 A value of nil means don't automatically resize mini-windows.
27999 A value of t means resize them to fit the text displayed in them.
28000 A value of `grow-only', the default, means let mini-windows grow only;
28001 they return to their normal size when the minibuffer is closed, or the
28002 echo area becomes empty. */);
28003 Vresize_mini_windows = Qgrow_only;
28005 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28006 doc: /* Alist specifying how to blink the cursor off.
28007 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28008 `cursor-type' frame-parameter or variable equals ON-STATE,
28009 comparing using `equal', Emacs uses OFF-STATE to specify
28010 how to blink it off. ON-STATE and OFF-STATE are values for
28011 the `cursor-type' frame parameter.
28013 If a frame's ON-STATE has no entry in this list,
28014 the frame's other specifications determine how to blink the cursor off. */);
28015 Vblink_cursor_alist = Qnil;
28017 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28018 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28019 If non-nil, windows are automatically scrolled horizontally to make
28020 point visible. */);
28021 automatic_hscrolling_p = 1;
28022 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28024 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28025 doc: /* *How many columns away from the window edge point is allowed to get
28026 before automatic hscrolling will horizontally scroll the window. */);
28027 hscroll_margin = 5;
28029 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28030 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28031 When point is less than `hscroll-margin' columns from the window
28032 edge, automatic hscrolling will scroll the window by the amount of columns
28033 determined by this variable. If its value is a positive integer, scroll that
28034 many columns. If it's a positive floating-point number, it specifies the
28035 fraction of the window's width to scroll. If it's nil or zero, point will be
28036 centered horizontally after the scroll. Any other value, including negative
28037 numbers, are treated as if the value were zero.
28039 Automatic hscrolling always moves point outside the scroll margin, so if
28040 point was more than scroll step columns inside the margin, the window will
28041 scroll more than the value given by the scroll step.
28043 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28044 and `scroll-right' overrides this variable's effect. */);
28045 Vhscroll_step = make_number (0);
28047 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28048 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28049 Bind this around calls to `message' to let it take effect. */);
28050 message_truncate_lines = 0;
28052 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28053 doc: /* Normal hook run to update the menu bar definitions.
28054 Redisplay runs this hook before it redisplays the menu bar.
28055 This is used to update submenus such as Buffers,
28056 whose contents depend on various data. */);
28057 Vmenu_bar_update_hook = Qnil;
28059 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28060 doc: /* Frame for which we are updating a menu.
28061 The enable predicate for a menu binding should check this variable. */);
28062 Vmenu_updating_frame = Qnil;
28064 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28065 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28066 inhibit_menubar_update = 0;
28068 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28069 doc: /* Prefix prepended to all continuation lines at display time.
28070 The value may be a string, an image, or a stretch-glyph; it is
28071 interpreted in the same way as the value of a `display' text property.
28073 This variable is overridden by any `wrap-prefix' text or overlay
28074 property.
28076 To add a prefix to non-continuation lines, use `line-prefix'. */);
28077 Vwrap_prefix = Qnil;
28078 DEFSYM (Qwrap_prefix, "wrap-prefix");
28079 Fmake_variable_buffer_local (Qwrap_prefix);
28081 DEFVAR_LISP ("line-prefix", Vline_prefix,
28082 doc: /* Prefix prepended to all non-continuation lines at display time.
28083 The value may be a string, an image, or a stretch-glyph; it is
28084 interpreted in the same way as the value of a `display' text property.
28086 This variable is overridden by any `line-prefix' text or overlay
28087 property.
28089 To add a prefix to continuation lines, use `wrap-prefix'. */);
28090 Vline_prefix = Qnil;
28091 DEFSYM (Qline_prefix, "line-prefix");
28092 Fmake_variable_buffer_local (Qline_prefix);
28094 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28095 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28096 inhibit_eval_during_redisplay = 0;
28098 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28099 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28100 inhibit_free_realized_faces = 0;
28102 #if GLYPH_DEBUG
28103 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28104 doc: /* Inhibit try_window_id display optimization. */);
28105 inhibit_try_window_id = 0;
28107 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28108 doc: /* Inhibit try_window_reusing display optimization. */);
28109 inhibit_try_window_reusing = 0;
28111 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28112 doc: /* Inhibit try_cursor_movement display optimization. */);
28113 inhibit_try_cursor_movement = 0;
28114 #endif /* GLYPH_DEBUG */
28116 DEFVAR_INT ("overline-margin", overline_margin,
28117 doc: /* *Space between overline and text, in pixels.
28118 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28119 margin to the caracter height. */);
28120 overline_margin = 2;
28122 DEFVAR_INT ("underline-minimum-offset",
28123 underline_minimum_offset,
28124 doc: /* Minimum distance between baseline and underline.
28125 This can improve legibility of underlined text at small font sizes,
28126 particularly when using variable `x-use-underline-position-properties'
28127 with fonts that specify an UNDERLINE_POSITION relatively close to the
28128 baseline. The default value is 1. */);
28129 underline_minimum_offset = 1;
28131 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28132 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28133 This feature only works when on a window system that can change
28134 cursor shapes. */);
28135 display_hourglass_p = 1;
28137 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28138 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28139 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28141 hourglass_atimer = NULL;
28142 hourglass_shown_p = 0;
28144 DEFSYM (Qglyphless_char, "glyphless-char");
28145 DEFSYM (Qhex_code, "hex-code");
28146 DEFSYM (Qempty_box, "empty-box");
28147 DEFSYM (Qthin_space, "thin-space");
28148 DEFSYM (Qzero_width, "zero-width");
28150 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28151 /* Intern this now in case it isn't already done.
28152 Setting this variable twice is harmless.
28153 But don't staticpro it here--that is done in alloc.c. */
28154 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28155 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28157 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28158 doc: /* Char-table defining glyphless characters.
28159 Each element, if non-nil, should be one of the following:
28160 an ASCII acronym string: display this string in a box
28161 `hex-code': display the hexadecimal code of a character in a box
28162 `empty-box': display as an empty box
28163 `thin-space': display as 1-pixel width space
28164 `zero-width': don't display
28165 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28166 display method for graphical terminals and text terminals respectively.
28167 GRAPHICAL and TEXT should each have one of the values listed above.
28169 The char-table has one extra slot to control the display of a character for
28170 which no font is found. This slot only takes effect on graphical terminals.
28171 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28172 `thin-space'. The default is `empty-box'. */);
28173 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28174 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28175 Qempty_box);
28179 /* Initialize this module when Emacs starts. */
28181 void
28182 init_xdisp (void)
28184 current_header_line_height = current_mode_line_height = -1;
28186 CHARPOS (this_line_start_pos) = 0;
28188 if (!noninteractive)
28190 struct window *m = XWINDOW (minibuf_window);
28191 Lisp_Object frame = m->frame;
28192 struct frame *f = XFRAME (frame);
28193 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28194 struct window *r = XWINDOW (root);
28195 int i;
28197 echo_area_window = minibuf_window;
28199 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28200 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28201 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28202 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28203 XSETFASTINT (m->total_lines, 1);
28204 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28206 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28207 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28208 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28210 /* The default ellipsis glyphs `...'. */
28211 for (i = 0; i < 3; ++i)
28212 default_invis_vector[i] = make_number ('.');
28216 /* Allocate the buffer for frame titles.
28217 Also used for `format-mode-line'. */
28218 int size = 100;
28219 mode_line_noprop_buf = (char *) xmalloc (size);
28220 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28221 mode_line_noprop_ptr = mode_line_noprop_buf;
28222 mode_line_target = MODE_LINE_DISPLAY;
28225 help_echo_showing_p = 0;
28228 /* Since w32 does not support atimers, it defines its own implementation of
28229 the following three functions in w32fns.c. */
28230 #ifndef WINDOWSNT
28232 /* Platform-independent portion of hourglass implementation. */
28234 /* Return non-zero if houglass timer has been started or hourglass is shown. */
28236 hourglass_started (void)
28238 return hourglass_shown_p || hourglass_atimer != NULL;
28241 /* Cancel a currently active hourglass timer, and start a new one. */
28242 void
28243 start_hourglass (void)
28245 #if defined (HAVE_WINDOW_SYSTEM)
28246 EMACS_TIME delay;
28247 int secs, usecs = 0;
28249 cancel_hourglass ();
28251 if (INTEGERP (Vhourglass_delay)
28252 && XINT (Vhourglass_delay) > 0)
28253 secs = XFASTINT (Vhourglass_delay);
28254 else if (FLOATP (Vhourglass_delay)
28255 && XFLOAT_DATA (Vhourglass_delay) > 0)
28257 Lisp_Object tem;
28258 tem = Ftruncate (Vhourglass_delay, Qnil);
28259 secs = XFASTINT (tem);
28260 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28262 else
28263 secs = DEFAULT_HOURGLASS_DELAY;
28265 EMACS_SET_SECS_USECS (delay, secs, usecs);
28266 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28267 show_hourglass, NULL);
28268 #endif
28272 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28273 shown. */
28274 void
28275 cancel_hourglass (void)
28277 #if defined (HAVE_WINDOW_SYSTEM)
28278 if (hourglass_atimer)
28280 cancel_atimer (hourglass_atimer);
28281 hourglass_atimer = NULL;
28284 if (hourglass_shown_p)
28285 hide_hourglass ();
28286 #endif
28288 #endif /* ! WINDOWSNT */