upstream
[emacs.git] / src / xdisp.c
blob2d255bd0e39d4994c263c08f4ec0e4474173ef2d
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 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2488 /* Are lines in the display truncated? */
2489 if (base_face_id != DEFAULT_FACE_ID
2490 || XINT (it->w->hscroll)
2491 || (! WINDOW_FULL_WIDTH_P (it->w)
2492 && ((!NILP (Vtruncate_partial_width_windows)
2493 && !INTEGERP (Vtruncate_partial_width_windows))
2494 || (INTEGERP (Vtruncate_partial_width_windows)
2495 && (WINDOW_TOTAL_COLS (it->w)
2496 < XINT (Vtruncate_partial_width_windows))))))
2497 it->line_wrap = TRUNCATE;
2498 else if (NILP (BVAR (current_buffer, truncate_lines)))
2499 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2500 ? WINDOW_WRAP : WORD_WRAP;
2501 else
2502 it->line_wrap = TRUNCATE;
2504 /* Get dimensions of truncation and continuation glyphs. These are
2505 displayed as fringe bitmaps under X, so we don't need them for such
2506 frames. */
2507 if (!FRAME_WINDOW_P (it->f))
2509 if (it->line_wrap == TRUNCATE)
2511 /* We will need the truncation glyph. */
2512 xassert (it->glyph_row == NULL);
2513 produce_special_glyphs (it, IT_TRUNCATION);
2514 it->truncation_pixel_width = it->pixel_width;
2516 else
2518 /* We will need the continuation glyph. */
2519 xassert (it->glyph_row == NULL);
2520 produce_special_glyphs (it, IT_CONTINUATION);
2521 it->continuation_pixel_width = it->pixel_width;
2524 /* Reset these values to zero because the produce_special_glyphs
2525 above has changed them. */
2526 it->pixel_width = it->ascent = it->descent = 0;
2527 it->phys_ascent = it->phys_descent = 0;
2530 /* Set this after getting the dimensions of truncation and
2531 continuation glyphs, so that we don't produce glyphs when calling
2532 produce_special_glyphs, above. */
2533 it->glyph_row = row;
2534 it->area = TEXT_AREA;
2536 /* Forget any previous info about this row being reversed. */
2537 if (it->glyph_row)
2538 it->glyph_row->reversed_p = 0;
2540 /* Get the dimensions of the display area. The display area
2541 consists of the visible window area plus a horizontally scrolled
2542 part to the left of the window. All x-values are relative to the
2543 start of this total display area. */
2544 if (base_face_id != DEFAULT_FACE_ID)
2546 /* Mode lines, menu bar in terminal frames. */
2547 it->first_visible_x = 0;
2548 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2550 else
2552 it->first_visible_x
2553 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2554 it->last_visible_x = (it->first_visible_x
2555 + window_box_width (w, TEXT_AREA));
2557 /* If we truncate lines, leave room for the truncator glyph(s) at
2558 the right margin. Otherwise, leave room for the continuation
2559 glyph(s). Truncation and continuation glyphs are not inserted
2560 for window-based redisplay. */
2561 if (!FRAME_WINDOW_P (it->f))
2563 if (it->line_wrap == TRUNCATE)
2564 it->last_visible_x -= it->truncation_pixel_width;
2565 else
2566 it->last_visible_x -= it->continuation_pixel_width;
2569 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2570 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2573 /* Leave room for a border glyph. */
2574 if (!FRAME_WINDOW_P (it->f)
2575 && !WINDOW_RIGHTMOST_P (it->w))
2576 it->last_visible_x -= 1;
2578 it->last_visible_y = window_text_bottom_y (w);
2580 /* For mode lines and alike, arrange for the first glyph having a
2581 left box line if the face specifies a box. */
2582 if (base_face_id != DEFAULT_FACE_ID)
2584 struct face *face;
2586 it->face_id = remapped_base_face_id;
2588 /* If we have a boxed mode line, make the first character appear
2589 with a left box line. */
2590 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2591 if (face->box != FACE_NO_BOX)
2592 it->start_of_box_run_p = 1;
2595 /* If a buffer position was specified, set the iterator there,
2596 getting overlays and face properties from that position. */
2597 if (charpos >= BUF_BEG (current_buffer))
2599 it->end_charpos = ZV;
2600 it->face_id = -1;
2601 IT_CHARPOS (*it) = charpos;
2603 /* Compute byte position if not specified. */
2604 if (bytepos < charpos)
2605 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2606 else
2607 IT_BYTEPOS (*it) = bytepos;
2609 it->start = it->current;
2610 /* Do we need to reorder bidirectional text? Not if this is a
2611 unibyte buffer: by definition, none of the single-byte
2612 characters are strong R2L, so no reordering is needed. And
2613 bidi.c doesn't support unibyte buffers anyway. */
2614 it->bidi_p =
2615 !NILP (BVAR (current_buffer, bidi_display_reordering))
2616 && it->multibyte_p;
2618 /* If we are to reorder bidirectional text, init the bidi
2619 iterator. */
2620 if (it->bidi_p)
2622 /* Note the paragraph direction that this buffer wants to
2623 use. */
2624 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2625 Qleft_to_right))
2626 it->paragraph_embedding = L2R;
2627 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2628 Qright_to_left))
2629 it->paragraph_embedding = R2L;
2630 else
2631 it->paragraph_embedding = NEUTRAL_DIR;
2632 bidi_unshelve_cache (NULL, 0);
2633 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2634 &it->bidi_it);
2637 /* Compute faces etc. */
2638 reseat (it, it->current.pos, 1);
2641 CHECK_IT (it);
2645 /* Initialize IT for the display of window W with window start POS. */
2647 void
2648 start_display (struct it *it, struct window *w, struct text_pos pos)
2650 struct glyph_row *row;
2651 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2653 row = w->desired_matrix->rows + first_vpos;
2654 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2655 it->first_vpos = first_vpos;
2657 /* Don't reseat to previous visible line start if current start
2658 position is in a string or image. */
2659 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2661 int start_at_line_beg_p;
2662 int first_y = it->current_y;
2664 /* If window start is not at a line start, skip forward to POS to
2665 get the correct continuation lines width. */
2666 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2667 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2668 if (!start_at_line_beg_p)
2670 int new_x;
2672 reseat_at_previous_visible_line_start (it);
2673 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2675 new_x = it->current_x + it->pixel_width;
2677 /* If lines are continued, this line may end in the middle
2678 of a multi-glyph character (e.g. a control character
2679 displayed as \003, or in the middle of an overlay
2680 string). In this case move_it_to above will not have
2681 taken us to the start of the continuation line but to the
2682 end of the continued line. */
2683 if (it->current_x > 0
2684 && it->line_wrap != TRUNCATE /* Lines are continued. */
2685 && (/* And glyph doesn't fit on the line. */
2686 new_x > it->last_visible_x
2687 /* Or it fits exactly and we're on a window
2688 system frame. */
2689 || (new_x == it->last_visible_x
2690 && FRAME_WINDOW_P (it->f))))
2692 if (it->current.dpvec_index >= 0
2693 || it->current.overlay_string_index >= 0)
2695 set_iterator_to_next (it, 1);
2696 move_it_in_display_line_to (it, -1, -1, 0);
2699 it->continuation_lines_width += it->current_x;
2702 /* We're starting a new display line, not affected by the
2703 height of the continued line, so clear the appropriate
2704 fields in the iterator structure. */
2705 it->max_ascent = it->max_descent = 0;
2706 it->max_phys_ascent = it->max_phys_descent = 0;
2708 it->current_y = first_y;
2709 it->vpos = 0;
2710 it->current_x = it->hpos = 0;
2716 /* Return 1 if POS is a position in ellipses displayed for invisible
2717 text. W is the window we display, for text property lookup. */
2719 static int
2720 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2722 Lisp_Object prop, window;
2723 int ellipses_p = 0;
2724 EMACS_INT charpos = CHARPOS (pos->pos);
2726 /* If POS specifies a position in a display vector, this might
2727 be for an ellipsis displayed for invisible text. We won't
2728 get the iterator set up for delivering that ellipsis unless
2729 we make sure that it gets aware of the invisible text. */
2730 if (pos->dpvec_index >= 0
2731 && pos->overlay_string_index < 0
2732 && CHARPOS (pos->string_pos) < 0
2733 && charpos > BEGV
2734 && (XSETWINDOW (window, w),
2735 prop = Fget_char_property (make_number (charpos),
2736 Qinvisible, window),
2737 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2739 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2740 window);
2741 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2744 return ellipses_p;
2748 /* Initialize IT for stepping through current_buffer in window W,
2749 starting at position POS that includes overlay string and display
2750 vector/ control character translation position information. Value
2751 is zero if there are overlay strings with newlines at POS. */
2753 static int
2754 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2756 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2757 int i, overlay_strings_with_newlines = 0;
2759 /* If POS specifies a position in a display vector, this might
2760 be for an ellipsis displayed for invisible text. We won't
2761 get the iterator set up for delivering that ellipsis unless
2762 we make sure that it gets aware of the invisible text. */
2763 if (in_ellipses_for_invisible_text_p (pos, w))
2765 --charpos;
2766 bytepos = 0;
2769 /* Keep in mind: the call to reseat in init_iterator skips invisible
2770 text, so we might end up at a position different from POS. This
2771 is only a problem when POS is a row start after a newline and an
2772 overlay starts there with an after-string, and the overlay has an
2773 invisible property. Since we don't skip invisible text in
2774 display_line and elsewhere immediately after consuming the
2775 newline before the row start, such a POS will not be in a string,
2776 but the call to init_iterator below will move us to the
2777 after-string. */
2778 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2780 /* This only scans the current chunk -- it should scan all chunks.
2781 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2782 to 16 in 22.1 to make this a lesser problem. */
2783 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2785 const char *s = SSDATA (it->overlay_strings[i]);
2786 const char *e = s + SBYTES (it->overlay_strings[i]);
2788 while (s < e && *s != '\n')
2789 ++s;
2791 if (s < e)
2793 overlay_strings_with_newlines = 1;
2794 break;
2798 /* If position is within an overlay string, set up IT to the right
2799 overlay string. */
2800 if (pos->overlay_string_index >= 0)
2802 int relative_index;
2804 /* If the first overlay string happens to have a `display'
2805 property for an image, the iterator will be set up for that
2806 image, and we have to undo that setup first before we can
2807 correct the overlay string index. */
2808 if (it->method == GET_FROM_IMAGE)
2809 pop_it (it);
2811 /* We already have the first chunk of overlay strings in
2812 IT->overlay_strings. Load more until the one for
2813 pos->overlay_string_index is in IT->overlay_strings. */
2814 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2816 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2817 it->current.overlay_string_index = 0;
2818 while (n--)
2820 load_overlay_strings (it, 0);
2821 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2825 it->current.overlay_string_index = pos->overlay_string_index;
2826 relative_index = (it->current.overlay_string_index
2827 % OVERLAY_STRING_CHUNK_SIZE);
2828 it->string = it->overlay_strings[relative_index];
2829 xassert (STRINGP (it->string));
2830 it->current.string_pos = pos->string_pos;
2831 it->method = GET_FROM_STRING;
2834 if (CHARPOS (pos->string_pos) >= 0)
2836 /* Recorded position is not in an overlay string, but in another
2837 string. This can only be a string from a `display' property.
2838 IT should already be filled with that string. */
2839 it->current.string_pos = pos->string_pos;
2840 xassert (STRINGP (it->string));
2843 /* Restore position in display vector translations, control
2844 character translations or ellipses. */
2845 if (pos->dpvec_index >= 0)
2847 if (it->dpvec == NULL)
2848 get_next_display_element (it);
2849 xassert (it->dpvec && it->current.dpvec_index == 0);
2850 it->current.dpvec_index = pos->dpvec_index;
2853 CHECK_IT (it);
2854 return !overlay_strings_with_newlines;
2858 /* Initialize IT for stepping through current_buffer in window W
2859 starting at ROW->start. */
2861 static void
2862 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2864 init_from_display_pos (it, w, &row->start);
2865 it->start = row->start;
2866 it->continuation_lines_width = row->continuation_lines_width;
2867 CHECK_IT (it);
2871 /* Initialize IT for stepping through current_buffer in window W
2872 starting in the line following ROW, i.e. starting at ROW->end.
2873 Value is zero if there are overlay strings with newlines at ROW's
2874 end position. */
2876 static int
2877 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2879 int success = 0;
2881 if (init_from_display_pos (it, w, &row->end))
2883 if (row->continued_p)
2884 it->continuation_lines_width
2885 = row->continuation_lines_width + row->pixel_width;
2886 CHECK_IT (it);
2887 success = 1;
2890 return success;
2896 /***********************************************************************
2897 Text properties
2898 ***********************************************************************/
2900 /* Called when IT reaches IT->stop_charpos. Handle text property and
2901 overlay changes. Set IT->stop_charpos to the next position where
2902 to stop. */
2904 static void
2905 handle_stop (struct it *it)
2907 enum prop_handled handled;
2908 int handle_overlay_change_p;
2909 struct props *p;
2911 it->dpvec = NULL;
2912 it->current.dpvec_index = -1;
2913 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2914 it->ignore_overlay_strings_at_pos_p = 0;
2915 it->ellipsis_p = 0;
2917 /* Use face of preceding text for ellipsis (if invisible) */
2918 if (it->selective_display_ellipsis_p)
2919 it->saved_face_id = it->face_id;
2923 handled = HANDLED_NORMALLY;
2925 /* Call text property handlers. */
2926 for (p = it_props; p->handler; ++p)
2928 handled = p->handler (it);
2930 if (handled == HANDLED_RECOMPUTE_PROPS)
2931 break;
2932 else if (handled == HANDLED_RETURN)
2934 /* We still want to show before and after strings from
2935 overlays even if the actual buffer text is replaced. */
2936 if (!handle_overlay_change_p
2937 || it->sp > 1
2938 || !get_overlay_strings_1 (it, 0, 0))
2940 if (it->ellipsis_p)
2941 setup_for_ellipsis (it, 0);
2942 /* When handling a display spec, we might load an
2943 empty string. In that case, discard it here. We
2944 used to discard it in handle_single_display_spec,
2945 but that causes get_overlay_strings_1, above, to
2946 ignore overlay strings that we must check. */
2947 if (STRINGP (it->string) && !SCHARS (it->string))
2948 pop_it (it);
2949 return;
2951 else if (STRINGP (it->string) && !SCHARS (it->string))
2952 pop_it (it);
2953 else
2955 it->ignore_overlay_strings_at_pos_p = 1;
2956 it->string_from_display_prop_p = 0;
2957 it->from_disp_prop_p = 0;
2958 handle_overlay_change_p = 0;
2960 handled = HANDLED_RECOMPUTE_PROPS;
2961 break;
2963 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2964 handle_overlay_change_p = 0;
2967 if (handled != HANDLED_RECOMPUTE_PROPS)
2969 /* Don't check for overlay strings below when set to deliver
2970 characters from a display vector. */
2971 if (it->method == GET_FROM_DISPLAY_VECTOR)
2972 handle_overlay_change_p = 0;
2974 /* Handle overlay changes.
2975 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2976 if it finds overlays. */
2977 if (handle_overlay_change_p)
2978 handled = handle_overlay_change (it);
2981 if (it->ellipsis_p)
2983 setup_for_ellipsis (it, 0);
2984 break;
2987 while (handled == HANDLED_RECOMPUTE_PROPS);
2989 /* Determine where to stop next. */
2990 if (handled == HANDLED_NORMALLY)
2991 compute_stop_pos (it);
2995 /* Compute IT->stop_charpos from text property and overlay change
2996 information for IT's current position. */
2998 static void
2999 compute_stop_pos (struct it *it)
3001 register INTERVAL iv, next_iv;
3002 Lisp_Object object, limit, position;
3003 EMACS_INT charpos, bytepos;
3005 /* If nowhere else, stop at the end. */
3006 it->stop_charpos = it->end_charpos;
3008 if (STRINGP (it->string))
3010 /* Strings are usually short, so don't limit the search for
3011 properties. */
3012 object = it->string;
3013 limit = Qnil;
3014 charpos = IT_STRING_CHARPOS (*it);
3015 bytepos = IT_STRING_BYTEPOS (*it);
3017 else
3019 EMACS_INT pos;
3021 /* If next overlay change is in front of the current stop pos
3022 (which is IT->end_charpos), stop there. Note: value of
3023 next_overlay_change is point-max if no overlay change
3024 follows. */
3025 charpos = IT_CHARPOS (*it);
3026 bytepos = IT_BYTEPOS (*it);
3027 pos = next_overlay_change (charpos);
3028 if (pos < it->stop_charpos)
3029 it->stop_charpos = pos;
3031 /* If showing the region, we have to stop at the region
3032 start or end because the face might change there. */
3033 if (it->region_beg_charpos > 0)
3035 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3036 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3037 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3038 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3041 /* Set up variables for computing the stop position from text
3042 property changes. */
3043 XSETBUFFER (object, current_buffer);
3044 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3047 /* Get the interval containing IT's position. Value is a null
3048 interval if there isn't such an interval. */
3049 position = make_number (charpos);
3050 iv = validate_interval_range (object, &position, &position, 0);
3051 if (!NULL_INTERVAL_P (iv))
3053 Lisp_Object values_here[LAST_PROP_IDX];
3054 struct props *p;
3056 /* Get properties here. */
3057 for (p = it_props; p->handler; ++p)
3058 values_here[p->idx] = textget (iv->plist, *p->name);
3060 /* Look for an interval following iv that has different
3061 properties. */
3062 for (next_iv = next_interval (iv);
3063 (!NULL_INTERVAL_P (next_iv)
3064 && (NILP (limit)
3065 || XFASTINT (limit) > next_iv->position));
3066 next_iv = next_interval (next_iv))
3068 for (p = it_props; p->handler; ++p)
3070 Lisp_Object new_value;
3072 new_value = textget (next_iv->plist, *p->name);
3073 if (!EQ (values_here[p->idx], new_value))
3074 break;
3077 if (p->handler)
3078 break;
3081 if (!NULL_INTERVAL_P (next_iv))
3083 if (INTEGERP (limit)
3084 && next_iv->position >= XFASTINT (limit))
3085 /* No text property change up to limit. */
3086 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3087 else
3088 /* Text properties change in next_iv. */
3089 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3093 if (it->cmp_it.id < 0)
3095 EMACS_INT stoppos = it->end_charpos;
3097 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3098 stoppos = -1;
3099 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3100 stoppos, it->string);
3103 xassert (STRINGP (it->string)
3104 || (it->stop_charpos >= BEGV
3105 && it->stop_charpos >= IT_CHARPOS (*it)));
3109 /* Return the position of the next overlay change after POS in
3110 current_buffer. Value is point-max if no overlay change
3111 follows. This is like `next-overlay-change' but doesn't use
3112 xmalloc. */
3114 static EMACS_INT
3115 next_overlay_change (EMACS_INT pos)
3117 ptrdiff_t i, noverlays;
3118 EMACS_INT endpos;
3119 Lisp_Object *overlays;
3121 /* Get all overlays at the given position. */
3122 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3124 /* If any of these overlays ends before endpos,
3125 use its ending point instead. */
3126 for (i = 0; i < noverlays; ++i)
3128 Lisp_Object oend;
3129 EMACS_INT oendpos;
3131 oend = OVERLAY_END (overlays[i]);
3132 oendpos = OVERLAY_POSITION (oend);
3133 endpos = min (endpos, oendpos);
3136 return endpos;
3139 /* How many characters forward to search for a display property or
3140 display string. Enough for a screenful of 100 lines x 50
3141 characters in a line. */
3142 #define MAX_DISP_SCAN 5000
3144 /* Return the character position of a display string at or after
3145 position specified by POSITION. If no display string exists at or
3146 after POSITION, return ZV. A display string is either an overlay
3147 with `display' property whose value is a string, or a `display'
3148 text property whose value is a string. STRING is data about the
3149 string to iterate; if STRING->lstring is nil, we are iterating a
3150 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3151 on a GUI frame. DISP_PROP is set to zero if we searched
3152 MAX_DISP_SCAN characters forward without finding any display
3153 strings, non-zero otherwise. It is set to 2 if the display string
3154 uses any kind of `(space ...)' spec that will produce a stretch of
3155 white space in the text area. */
3156 EMACS_INT
3157 compute_display_string_pos (struct text_pos *position,
3158 struct bidi_string_data *string,
3159 int frame_window_p, int *disp_prop)
3161 /* OBJECT = nil means current buffer. */
3162 Lisp_Object object =
3163 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3164 Lisp_Object pos, spec, limpos;
3165 int string_p = (string && (STRINGP (string->lstring) || string->s));
3166 EMACS_INT eob = string_p ? string->schars : ZV;
3167 EMACS_INT begb = string_p ? 0 : BEGV;
3168 EMACS_INT bufpos, charpos = CHARPOS (*position);
3169 EMACS_INT lim =
3170 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3171 struct text_pos tpos;
3172 int rv = 0;
3174 *disp_prop = 1;
3176 if (charpos >= eob
3177 /* We don't support display properties whose values are strings
3178 that have display string properties. */
3179 || string->from_disp_str
3180 /* C strings cannot have display properties. */
3181 || (string->s && !STRINGP (object)))
3183 *disp_prop = 0;
3184 return eob;
3187 /* If the character at CHARPOS is where the display string begins,
3188 return CHARPOS. */
3189 pos = make_number (charpos);
3190 if (STRINGP (object))
3191 bufpos = string->bufpos;
3192 else
3193 bufpos = charpos;
3194 tpos = *position;
3195 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3196 && (charpos <= begb
3197 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3198 object),
3199 spec))
3200 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3201 frame_window_p)))
3203 if (rv == 2)
3204 *disp_prop = 2;
3205 return charpos;
3208 /* Look forward for the first character with a `display' property
3209 that will replace the underlying text when displayed. */
3210 limpos = make_number (lim);
3211 do {
3212 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3213 CHARPOS (tpos) = XFASTINT (pos);
3214 if (CHARPOS (tpos) >= lim)
3216 *disp_prop = 0;
3217 break;
3219 if (STRINGP (object))
3220 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3221 else
3222 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3223 spec = Fget_char_property (pos, Qdisplay, object);
3224 if (!STRINGP (object))
3225 bufpos = CHARPOS (tpos);
3226 } while (NILP (spec)
3227 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3228 bufpos, frame_window_p)));
3229 if (rv == 2)
3230 *disp_prop = 2;
3232 return CHARPOS (tpos);
3235 /* Return the character position of the end of the display string that
3236 started at CHARPOS. A display string is either an overlay with
3237 `display' property whose value is a string or a `display' text
3238 property whose value is a string. */
3239 EMACS_INT
3240 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3242 /* OBJECT = nil means current buffer. */
3243 Lisp_Object object =
3244 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3245 Lisp_Object pos = make_number (charpos);
3246 EMACS_INT eob =
3247 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3249 if (charpos >= eob || (string->s && !STRINGP (object)))
3250 return eob;
3252 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3253 abort ();
3255 /* Look forward for the first character where the `display' property
3256 changes. */
3257 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3259 return XFASTINT (pos);
3264 /***********************************************************************
3265 Fontification
3266 ***********************************************************************/
3268 /* Handle changes in the `fontified' property of the current buffer by
3269 calling hook functions from Qfontification_functions to fontify
3270 regions of text. */
3272 static enum prop_handled
3273 handle_fontified_prop (struct it *it)
3275 Lisp_Object prop, pos;
3276 enum prop_handled handled = HANDLED_NORMALLY;
3278 if (!NILP (Vmemory_full))
3279 return handled;
3281 /* Get the value of the `fontified' property at IT's current buffer
3282 position. (The `fontified' property doesn't have a special
3283 meaning in strings.) If the value is nil, call functions from
3284 Qfontification_functions. */
3285 if (!STRINGP (it->string)
3286 && it->s == NULL
3287 && !NILP (Vfontification_functions)
3288 && !NILP (Vrun_hooks)
3289 && (pos = make_number (IT_CHARPOS (*it)),
3290 prop = Fget_char_property (pos, Qfontified, Qnil),
3291 /* Ignore the special cased nil value always present at EOB since
3292 no amount of fontifying will be able to change it. */
3293 NILP (prop) && IT_CHARPOS (*it) < Z))
3295 int count = SPECPDL_INDEX ();
3296 Lisp_Object val;
3297 struct buffer *obuf = current_buffer;
3298 int begv = BEGV, zv = ZV;
3299 int old_clip_changed = current_buffer->clip_changed;
3301 val = Vfontification_functions;
3302 specbind (Qfontification_functions, Qnil);
3304 xassert (it->end_charpos == ZV);
3306 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3307 safe_call1 (val, pos);
3308 else
3310 Lisp_Object fns, fn;
3311 struct gcpro gcpro1, gcpro2;
3313 fns = Qnil;
3314 GCPRO2 (val, fns);
3316 for (; CONSP (val); val = XCDR (val))
3318 fn = XCAR (val);
3320 if (EQ (fn, Qt))
3322 /* A value of t indicates this hook has a local
3323 binding; it means to run the global binding too.
3324 In a global value, t should not occur. If it
3325 does, we must ignore it to avoid an endless
3326 loop. */
3327 for (fns = Fdefault_value (Qfontification_functions);
3328 CONSP (fns);
3329 fns = XCDR (fns))
3331 fn = XCAR (fns);
3332 if (!EQ (fn, Qt))
3333 safe_call1 (fn, pos);
3336 else
3337 safe_call1 (fn, pos);
3340 UNGCPRO;
3343 unbind_to (count, Qnil);
3345 /* Fontification functions routinely call `save-restriction'.
3346 Normally, this tags clip_changed, which can confuse redisplay
3347 (see discussion in Bug#6671). Since we don't perform any
3348 special handling of fontification changes in the case where
3349 `save-restriction' isn't called, there's no point doing so in
3350 this case either. So, if the buffer's restrictions are
3351 actually left unchanged, reset clip_changed. */
3352 if (obuf == current_buffer)
3354 if (begv == BEGV && zv == ZV)
3355 current_buffer->clip_changed = old_clip_changed;
3357 /* There isn't much we can reasonably do to protect against
3358 misbehaving fontification, but here's a fig leaf. */
3359 else if (!NILP (BVAR (obuf, name)))
3360 set_buffer_internal_1 (obuf);
3362 /* The fontification code may have added/removed text.
3363 It could do even a lot worse, but let's at least protect against
3364 the most obvious case where only the text past `pos' gets changed',
3365 as is/was done in grep.el where some escapes sequences are turned
3366 into face properties (bug#7876). */
3367 it->end_charpos = ZV;
3369 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3370 something. This avoids an endless loop if they failed to
3371 fontify the text for which reason ever. */
3372 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3373 handled = HANDLED_RECOMPUTE_PROPS;
3376 return handled;
3381 /***********************************************************************
3382 Faces
3383 ***********************************************************************/
3385 /* Set up iterator IT from face properties at its current position.
3386 Called from handle_stop. */
3388 static enum prop_handled
3389 handle_face_prop (struct it *it)
3391 int new_face_id;
3392 EMACS_INT next_stop;
3394 if (!STRINGP (it->string))
3396 new_face_id
3397 = face_at_buffer_position (it->w,
3398 IT_CHARPOS (*it),
3399 it->region_beg_charpos,
3400 it->region_end_charpos,
3401 &next_stop,
3402 (IT_CHARPOS (*it)
3403 + TEXT_PROP_DISTANCE_LIMIT),
3404 0, it->base_face_id);
3406 /* Is this a start of a run of characters with box face?
3407 Caveat: this can be called for a freshly initialized
3408 iterator; face_id is -1 in this case. We know that the new
3409 face will not change until limit, i.e. if the new face has a
3410 box, all characters up to limit will have one. But, as
3411 usual, we don't know whether limit is really the end. */
3412 if (new_face_id != it->face_id)
3414 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3416 /* If new face has a box but old face has not, this is
3417 the start of a run of characters with box, i.e. it has
3418 a shadow on the left side. The value of face_id of the
3419 iterator will be -1 if this is the initial call that gets
3420 the face. In this case, we have to look in front of IT's
3421 position and see whether there is a face != new_face_id. */
3422 it->start_of_box_run_p
3423 = (new_face->box != FACE_NO_BOX
3424 && (it->face_id >= 0
3425 || IT_CHARPOS (*it) == BEG
3426 || new_face_id != face_before_it_pos (it)));
3427 it->face_box_p = new_face->box != FACE_NO_BOX;
3430 else
3432 int base_face_id;
3433 EMACS_INT bufpos;
3434 int i;
3435 Lisp_Object from_overlay
3436 = (it->current.overlay_string_index >= 0
3437 ? it->string_overlays[it->current.overlay_string_index]
3438 : Qnil);
3440 /* See if we got to this string directly or indirectly from
3441 an overlay property. That includes the before-string or
3442 after-string of an overlay, strings in display properties
3443 provided by an overlay, their text properties, etc.
3445 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3446 if (! NILP (from_overlay))
3447 for (i = it->sp - 1; i >= 0; i--)
3449 if (it->stack[i].current.overlay_string_index >= 0)
3450 from_overlay
3451 = it->string_overlays[it->stack[i].current.overlay_string_index];
3452 else if (! NILP (it->stack[i].from_overlay))
3453 from_overlay = it->stack[i].from_overlay;
3455 if (!NILP (from_overlay))
3456 break;
3459 if (! NILP (from_overlay))
3461 bufpos = IT_CHARPOS (*it);
3462 /* For a string from an overlay, the base face depends
3463 only on text properties and ignores overlays. */
3464 base_face_id
3465 = face_for_overlay_string (it->w,
3466 IT_CHARPOS (*it),
3467 it->region_beg_charpos,
3468 it->region_end_charpos,
3469 &next_stop,
3470 (IT_CHARPOS (*it)
3471 + TEXT_PROP_DISTANCE_LIMIT),
3473 from_overlay);
3475 else
3477 bufpos = 0;
3479 /* For strings from a `display' property, use the face at
3480 IT's current buffer position as the base face to merge
3481 with, so that overlay strings appear in the same face as
3482 surrounding text, unless they specify their own
3483 faces. */
3484 base_face_id = underlying_face_id (it);
3487 new_face_id = face_at_string_position (it->w,
3488 it->string,
3489 IT_STRING_CHARPOS (*it),
3490 bufpos,
3491 it->region_beg_charpos,
3492 it->region_end_charpos,
3493 &next_stop,
3494 base_face_id, 0);
3496 /* Is this a start of a run of characters with box? Caveat:
3497 this can be called for a freshly allocated iterator; face_id
3498 is -1 is this case. We know that the new face will not
3499 change until the next check pos, i.e. if the new face has a
3500 box, all characters up to that position will have a
3501 box. But, as usual, we don't know whether that position
3502 is really the end. */
3503 if (new_face_id != it->face_id)
3505 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3506 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3508 /* If new face has a box but old face hasn't, this is the
3509 start of a run of characters with box, i.e. it has a
3510 shadow on the left side. */
3511 it->start_of_box_run_p
3512 = new_face->box && (old_face == NULL || !old_face->box);
3513 it->face_box_p = new_face->box != FACE_NO_BOX;
3517 it->face_id = new_face_id;
3518 return HANDLED_NORMALLY;
3522 /* Return the ID of the face ``underlying'' IT's current position,
3523 which is in a string. If the iterator is associated with a
3524 buffer, return the face at IT's current buffer position.
3525 Otherwise, use the iterator's base_face_id. */
3527 static int
3528 underlying_face_id (struct it *it)
3530 int face_id = it->base_face_id, i;
3532 xassert (STRINGP (it->string));
3534 for (i = it->sp - 1; i >= 0; --i)
3535 if (NILP (it->stack[i].string))
3536 face_id = it->stack[i].face_id;
3538 return face_id;
3542 /* Compute the face one character before or after the current position
3543 of IT, in the visual order. BEFORE_P non-zero means get the face
3544 in front (to the left in L2R paragraphs, to the right in R2L
3545 paragraphs) of IT's screen position. Value is the ID of the face. */
3547 static int
3548 face_before_or_after_it_pos (struct it *it, int before_p)
3550 int face_id, limit;
3551 EMACS_INT next_check_charpos;
3552 struct it it_copy;
3553 void *it_copy_data = NULL;
3555 xassert (it->s == NULL);
3557 if (STRINGP (it->string))
3559 EMACS_INT bufpos, charpos;
3560 int base_face_id;
3562 /* No face change past the end of the string (for the case
3563 we are padding with spaces). No face change before the
3564 string start. */
3565 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3566 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3567 return it->face_id;
3569 if (!it->bidi_p)
3571 /* Set charpos to the position before or after IT's current
3572 position, in the logical order, which in the non-bidi
3573 case is the same as the visual order. */
3574 if (before_p)
3575 charpos = IT_STRING_CHARPOS (*it) - 1;
3576 else if (it->what == IT_COMPOSITION)
3577 /* For composition, we must check the character after the
3578 composition. */
3579 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3580 else
3581 charpos = IT_STRING_CHARPOS (*it) + 1;
3583 else
3585 if (before_p)
3587 /* With bidi iteration, the character before the current
3588 in the visual order cannot be found by simple
3589 iteration, because "reverse" reordering is not
3590 supported. Instead, we need to use the move_it_*
3591 family of functions. */
3592 /* Ignore face changes before the first visible
3593 character on this display line. */
3594 if (it->current_x <= it->first_visible_x)
3595 return it->face_id;
3596 SAVE_IT (it_copy, *it, it_copy_data);
3597 /* Implementation note: Since move_it_in_display_line
3598 works in the iterator geometry, and thinks the first
3599 character is always the leftmost, even in R2L lines,
3600 we don't need to distinguish between the R2L and L2R
3601 cases here. */
3602 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3603 it_copy.current_x - 1, MOVE_TO_X);
3604 charpos = IT_STRING_CHARPOS (it_copy);
3605 RESTORE_IT (it, it, it_copy_data);
3607 else
3609 /* Set charpos to the string position of the character
3610 that comes after IT's current position in the visual
3611 order. */
3612 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3614 it_copy = *it;
3615 while (n--)
3616 bidi_move_to_visually_next (&it_copy.bidi_it);
3618 charpos = it_copy.bidi_it.charpos;
3621 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3623 if (it->current.overlay_string_index >= 0)
3624 bufpos = IT_CHARPOS (*it);
3625 else
3626 bufpos = 0;
3628 base_face_id = underlying_face_id (it);
3630 /* Get the face for ASCII, or unibyte. */
3631 face_id = face_at_string_position (it->w,
3632 it->string,
3633 charpos,
3634 bufpos,
3635 it->region_beg_charpos,
3636 it->region_end_charpos,
3637 &next_check_charpos,
3638 base_face_id, 0);
3640 /* Correct the face for charsets different from ASCII. Do it
3641 for the multibyte case only. The face returned above is
3642 suitable for unibyte text if IT->string is unibyte. */
3643 if (STRING_MULTIBYTE (it->string))
3645 struct text_pos pos1 = string_pos (charpos, it->string);
3646 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3647 int c, len;
3648 struct face *face = FACE_FROM_ID (it->f, face_id);
3650 c = string_char_and_length (p, &len);
3651 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3654 else
3656 struct text_pos pos;
3658 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3659 || (IT_CHARPOS (*it) <= BEGV && before_p))
3660 return it->face_id;
3662 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3663 pos = it->current.pos;
3665 if (!it->bidi_p)
3667 if (before_p)
3668 DEC_TEXT_POS (pos, it->multibyte_p);
3669 else
3671 if (it->what == IT_COMPOSITION)
3673 /* For composition, we must check the position after
3674 the composition. */
3675 pos.charpos += it->cmp_it.nchars;
3676 pos.bytepos += it->len;
3678 else
3679 INC_TEXT_POS (pos, it->multibyte_p);
3682 else
3684 if (before_p)
3686 /* With bidi iteration, the character before the current
3687 in the visual order cannot be found by simple
3688 iteration, because "reverse" reordering is not
3689 supported. Instead, we need to use the move_it_*
3690 family of functions. */
3691 /* Ignore face changes before the first visible
3692 character on this display line. */
3693 if (it->current_x <= it->first_visible_x)
3694 return it->face_id;
3695 SAVE_IT (it_copy, *it, it_copy_data);
3696 /* Implementation note: Since move_it_in_display_line
3697 works in the iterator geometry, and thinks the first
3698 character is always the leftmost, even in R2L lines,
3699 we don't need to distinguish between the R2L and L2R
3700 cases here. */
3701 move_it_in_display_line (&it_copy, ZV,
3702 it_copy.current_x - 1, MOVE_TO_X);
3703 pos = it_copy.current.pos;
3704 RESTORE_IT (it, it, it_copy_data);
3706 else
3708 /* Set charpos to the buffer position of the character
3709 that comes after IT's current position in the visual
3710 order. */
3711 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3713 it_copy = *it;
3714 while (n--)
3715 bidi_move_to_visually_next (&it_copy.bidi_it);
3717 SET_TEXT_POS (pos,
3718 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3721 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3723 /* Determine face for CHARSET_ASCII, or unibyte. */
3724 face_id = face_at_buffer_position (it->w,
3725 CHARPOS (pos),
3726 it->region_beg_charpos,
3727 it->region_end_charpos,
3728 &next_check_charpos,
3729 limit, 0, -1);
3731 /* Correct the face for charsets different from ASCII. Do it
3732 for the multibyte case only. The face returned above is
3733 suitable for unibyte text if current_buffer is unibyte. */
3734 if (it->multibyte_p)
3736 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3737 struct face *face = FACE_FROM_ID (it->f, face_id);
3738 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3742 return face_id;
3747 /***********************************************************************
3748 Invisible text
3749 ***********************************************************************/
3751 /* Set up iterator IT from invisible properties at its current
3752 position. Called from handle_stop. */
3754 static enum prop_handled
3755 handle_invisible_prop (struct it *it)
3757 enum prop_handled handled = HANDLED_NORMALLY;
3759 if (STRINGP (it->string))
3761 Lisp_Object prop, end_charpos, limit, charpos;
3763 /* Get the value of the invisible text property at the
3764 current position. Value will be nil if there is no such
3765 property. */
3766 charpos = make_number (IT_STRING_CHARPOS (*it));
3767 prop = Fget_text_property (charpos, Qinvisible, it->string);
3769 if (!NILP (prop)
3770 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3772 EMACS_INT endpos;
3774 handled = HANDLED_RECOMPUTE_PROPS;
3776 /* Get the position at which the next change of the
3777 invisible text property can be found in IT->string.
3778 Value will be nil if the property value is the same for
3779 all the rest of IT->string. */
3780 XSETINT (limit, SCHARS (it->string));
3781 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3782 it->string, limit);
3784 /* Text at current position is invisible. The next
3785 change in the property is at position end_charpos.
3786 Move IT's current position to that position. */
3787 if (INTEGERP (end_charpos)
3788 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3790 struct text_pos old;
3791 EMACS_INT oldpos;
3793 old = it->current.string_pos;
3794 oldpos = CHARPOS (old);
3795 if (it->bidi_p)
3797 if (it->bidi_it.first_elt
3798 && it->bidi_it.charpos < SCHARS (it->string))
3799 bidi_paragraph_init (it->paragraph_embedding,
3800 &it->bidi_it, 1);
3801 /* Bidi-iterate out of the invisible text. */
3804 bidi_move_to_visually_next (&it->bidi_it);
3806 while (oldpos <= it->bidi_it.charpos
3807 && it->bidi_it.charpos < endpos);
3809 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3810 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3811 if (IT_CHARPOS (*it) >= endpos)
3812 it->prev_stop = endpos;
3814 else
3816 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3817 compute_string_pos (&it->current.string_pos, old, it->string);
3820 else
3822 /* The rest of the string is invisible. If this is an
3823 overlay string, proceed with the next overlay string
3824 or whatever comes and return a character from there. */
3825 if (it->current.overlay_string_index >= 0)
3827 next_overlay_string (it);
3828 /* Don't check for overlay strings when we just
3829 finished processing them. */
3830 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3832 else
3834 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3835 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3840 else
3842 int invis_p;
3843 EMACS_INT newpos, next_stop, start_charpos, tem;
3844 Lisp_Object pos, prop, overlay;
3846 /* First of all, is there invisible text at this position? */
3847 tem = start_charpos = IT_CHARPOS (*it);
3848 pos = make_number (tem);
3849 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3850 &overlay);
3851 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3853 /* If we are on invisible text, skip over it. */
3854 if (invis_p && start_charpos < it->end_charpos)
3856 /* Record whether we have to display an ellipsis for the
3857 invisible text. */
3858 int display_ellipsis_p = invis_p == 2;
3860 handled = HANDLED_RECOMPUTE_PROPS;
3862 /* Loop skipping over invisible text. The loop is left at
3863 ZV or with IT on the first char being visible again. */
3866 /* Try to skip some invisible text. Return value is the
3867 position reached which can be equal to where we start
3868 if there is nothing invisible there. This skips both
3869 over invisible text properties and overlays with
3870 invisible property. */
3871 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3873 /* If we skipped nothing at all we weren't at invisible
3874 text in the first place. If everything to the end of
3875 the buffer was skipped, end the loop. */
3876 if (newpos == tem || newpos >= ZV)
3877 invis_p = 0;
3878 else
3880 /* We skipped some characters but not necessarily
3881 all there are. Check if we ended up on visible
3882 text. Fget_char_property returns the property of
3883 the char before the given position, i.e. if we
3884 get invis_p = 0, this means that the char at
3885 newpos is visible. */
3886 pos = make_number (newpos);
3887 prop = Fget_char_property (pos, Qinvisible, it->window);
3888 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3891 /* If we ended up on invisible text, proceed to
3892 skip starting with next_stop. */
3893 if (invis_p)
3894 tem = next_stop;
3896 /* If there are adjacent invisible texts, don't lose the
3897 second one's ellipsis. */
3898 if (invis_p == 2)
3899 display_ellipsis_p = 1;
3901 while (invis_p);
3903 /* The position newpos is now either ZV or on visible text. */
3904 if (it->bidi_p && newpos < ZV)
3906 /* With bidi iteration, the region of invisible text
3907 could start and/or end in the middle of a non-base
3908 embedding level. Therefore, we need to skip
3909 invisible text using the bidi iterator, starting at
3910 IT's current position, until we find ourselves
3911 outside the invisible text. Skipping invisible text
3912 _after_ bidi iteration avoids affecting the visual
3913 order of the displayed text when invisible properties
3914 are added or removed. */
3915 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3917 /* If we were `reseat'ed to a new paragraph,
3918 determine the paragraph base direction. We need
3919 to do it now because next_element_from_buffer may
3920 not have a chance to do it, if we are going to
3921 skip any text at the beginning, which resets the
3922 FIRST_ELT flag. */
3923 bidi_paragraph_init (it->paragraph_embedding,
3924 &it->bidi_it, 1);
3928 bidi_move_to_visually_next (&it->bidi_it);
3930 while (it->stop_charpos <= it->bidi_it.charpos
3931 && it->bidi_it.charpos < newpos);
3932 IT_CHARPOS (*it) = it->bidi_it.charpos;
3933 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3934 /* If we overstepped NEWPOS, record its position in the
3935 iterator, so that we skip invisible text if later the
3936 bidi iteration lands us in the invisible region
3937 again. */
3938 if (IT_CHARPOS (*it) >= newpos)
3939 it->prev_stop = newpos;
3941 else
3943 IT_CHARPOS (*it) = newpos;
3944 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3947 /* If there are before-strings at the start of invisible
3948 text, and the text is invisible because of a text
3949 property, arrange to show before-strings because 20.x did
3950 it that way. (If the text is invisible because of an
3951 overlay property instead of a text property, this is
3952 already handled in the overlay code.) */
3953 if (NILP (overlay)
3954 && get_overlay_strings (it, it->stop_charpos))
3956 handled = HANDLED_RECOMPUTE_PROPS;
3957 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3959 else if (display_ellipsis_p)
3961 /* Make sure that the glyphs of the ellipsis will get
3962 correct `charpos' values. If we would not update
3963 it->position here, the glyphs would belong to the
3964 last visible character _before_ the invisible
3965 text, which confuses `set_cursor_from_row'.
3967 We use the last invisible position instead of the
3968 first because this way the cursor is always drawn on
3969 the first "." of the ellipsis, whenever PT is inside
3970 the invisible text. Otherwise the cursor would be
3971 placed _after_ the ellipsis when the point is after the
3972 first invisible character. */
3973 if (!STRINGP (it->object))
3975 it->position.charpos = newpos - 1;
3976 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3978 it->ellipsis_p = 1;
3979 /* Let the ellipsis display before
3980 considering any properties of the following char.
3981 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3982 handled = HANDLED_RETURN;
3987 return handled;
3991 /* Make iterator IT return `...' next.
3992 Replaces LEN characters from buffer. */
3994 static void
3995 setup_for_ellipsis (struct it *it, int len)
3997 /* Use the display table definition for `...'. Invalid glyphs
3998 will be handled by the method returning elements from dpvec. */
3999 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4001 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4002 it->dpvec = v->contents;
4003 it->dpend = v->contents + v->header.size;
4005 else
4007 /* Default `...'. */
4008 it->dpvec = default_invis_vector;
4009 it->dpend = default_invis_vector + 3;
4012 it->dpvec_char_len = len;
4013 it->current.dpvec_index = 0;
4014 it->dpvec_face_id = -1;
4016 /* Remember the current face id in case glyphs specify faces.
4017 IT's face is restored in set_iterator_to_next.
4018 saved_face_id was set to preceding char's face in handle_stop. */
4019 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4020 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4022 it->method = GET_FROM_DISPLAY_VECTOR;
4023 it->ellipsis_p = 1;
4028 /***********************************************************************
4029 'display' property
4030 ***********************************************************************/
4032 /* Set up iterator IT from `display' property at its current position.
4033 Called from handle_stop.
4034 We return HANDLED_RETURN if some part of the display property
4035 overrides the display of the buffer text itself.
4036 Otherwise we return HANDLED_NORMALLY. */
4038 static enum prop_handled
4039 handle_display_prop (struct it *it)
4041 Lisp_Object propval, object, overlay;
4042 struct text_pos *position;
4043 EMACS_INT bufpos;
4044 /* Nonzero if some property replaces the display of the text itself. */
4045 int display_replaced_p = 0;
4047 if (STRINGP (it->string))
4049 object = it->string;
4050 position = &it->current.string_pos;
4051 bufpos = CHARPOS (it->current.pos);
4053 else
4055 XSETWINDOW (object, it->w);
4056 position = &it->current.pos;
4057 bufpos = CHARPOS (*position);
4060 /* Reset those iterator values set from display property values. */
4061 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4062 it->space_width = Qnil;
4063 it->font_height = Qnil;
4064 it->voffset = 0;
4066 /* We don't support recursive `display' properties, i.e. string
4067 values that have a string `display' property, that have a string
4068 `display' property etc. */
4069 if (!it->string_from_display_prop_p)
4070 it->area = TEXT_AREA;
4072 propval = get_char_property_and_overlay (make_number (position->charpos),
4073 Qdisplay, object, &overlay);
4074 if (NILP (propval))
4075 return HANDLED_NORMALLY;
4076 /* Now OVERLAY is the overlay that gave us this property, or nil
4077 if it was a text property. */
4079 if (!STRINGP (it->string))
4080 object = it->w->buffer;
4082 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4083 position, bufpos,
4084 FRAME_WINDOW_P (it->f));
4086 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4089 /* Subroutine of handle_display_prop. Returns non-zero if the display
4090 specification in SPEC is a replacing specification, i.e. it would
4091 replace the text covered by `display' property with something else,
4092 such as an image or a display string. If SPEC includes any kind or
4093 `(space ...) specification, the value is 2; this is used by
4094 compute_display_string_pos, which see.
4096 See handle_single_display_spec for documentation of arguments.
4097 frame_window_p is non-zero if the window being redisplayed is on a
4098 GUI frame; this argument is used only if IT is NULL, see below.
4100 IT can be NULL, if this is called by the bidi reordering code
4101 through compute_display_string_pos, which see. In that case, this
4102 function only examines SPEC, but does not otherwise "handle" it, in
4103 the sense that it doesn't set up members of IT from the display
4104 spec. */
4105 static int
4106 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4107 Lisp_Object overlay, struct text_pos *position,
4108 EMACS_INT bufpos, int frame_window_p)
4110 int replacing_p = 0;
4111 int rv;
4113 if (CONSP (spec)
4114 /* Simple specerties. */
4115 && !EQ (XCAR (spec), Qimage)
4116 #ifdef HAVE_XWIDGETS
4117 && !EQ (XCAR (spec), Qxwidget)
4118 #endif
4119 && !EQ (XCAR (spec), Qspace)
4120 && !EQ (XCAR (spec), Qwhen)
4121 && !EQ (XCAR (spec), Qslice)
4122 && !EQ (XCAR (spec), Qspace_width)
4123 && !EQ (XCAR (spec), Qheight)
4124 && !EQ (XCAR (spec), Qraise)
4125 /* Marginal area specifications. */
4126 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4127 && !EQ (XCAR (spec), Qleft_fringe)
4128 && !EQ (XCAR (spec), Qright_fringe)
4129 && !NILP (XCAR (spec)))
4131 for (; CONSP (spec); spec = XCDR (spec))
4133 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4134 overlay, position, bufpos,
4135 replacing_p, frame_window_p)))
4137 replacing_p = rv;
4138 /* If some text in a string is replaced, `position' no
4139 longer points to the position of `object'. */
4140 if (!it || STRINGP (object))
4141 break;
4145 else if (VECTORP (spec))
4147 int i;
4148 for (i = 0; i < ASIZE (spec); ++i)
4149 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4150 overlay, position, bufpos,
4151 replacing_p, frame_window_p)))
4153 replacing_p = rv;
4154 /* If some text in a string is replaced, `position' no
4155 longer points to the position of `object'. */
4156 if (!it || STRINGP (object))
4157 break;
4160 else
4162 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4163 position, bufpos, 0,
4164 frame_window_p)))
4165 replacing_p = rv;
4168 return replacing_p;
4171 /* Value is the position of the end of the `display' property starting
4172 at START_POS in OBJECT. */
4174 static struct text_pos
4175 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4177 Lisp_Object end;
4178 struct text_pos end_pos;
4180 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4181 Qdisplay, object, Qnil);
4182 CHARPOS (end_pos) = XFASTINT (end);
4183 if (STRINGP (object))
4184 compute_string_pos (&end_pos, start_pos, it->string);
4185 else
4186 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4188 return end_pos;
4192 /* Set up IT from a single `display' property specification SPEC. OBJECT
4193 is the object in which the `display' property was found. *POSITION
4194 is the position in OBJECT at which the `display' property was found.
4195 BUFPOS is the buffer position of OBJECT (different from POSITION if
4196 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4197 previously saw a display specification which already replaced text
4198 display with something else, for example an image; we ignore such
4199 properties after the first one has been processed.
4201 OVERLAY is the overlay this `display' property came from,
4202 or nil if it was a text property.
4204 If SPEC is a `space' or `image' specification, and in some other
4205 cases too, set *POSITION to the position where the `display'
4206 property ends.
4208 If IT is NULL, only examine the property specification in SPEC, but
4209 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4210 is intended to be displayed in a window on a GUI frame.
4212 Value is non-zero if something was found which replaces the display
4213 of buffer or string text. */
4215 static int
4216 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4217 Lisp_Object overlay, struct text_pos *position,
4218 EMACS_INT bufpos, int display_replaced_p,
4219 int frame_window_p)
4221 Lisp_Object form;
4222 Lisp_Object location, value;
4223 struct text_pos start_pos = *position;
4224 int valid_p;
4226 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4227 If the result is non-nil, use VALUE instead of SPEC. */
4228 form = Qt;
4229 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4231 spec = XCDR (spec);
4232 if (!CONSP (spec))
4233 return 0;
4234 form = XCAR (spec);
4235 spec = XCDR (spec);
4238 if (!NILP (form) && !EQ (form, Qt))
4240 int count = SPECPDL_INDEX ();
4241 struct gcpro gcpro1;
4243 /* Bind `object' to the object having the `display' property, a
4244 buffer or string. Bind `position' to the position in the
4245 object where the property was found, and `buffer-position'
4246 to the current position in the buffer. */
4248 if (NILP (object))
4249 XSETBUFFER (object, current_buffer);
4250 specbind (Qobject, object);
4251 specbind (Qposition, make_number (CHARPOS (*position)));
4252 specbind (Qbuffer_position, make_number (bufpos));
4253 GCPRO1 (form);
4254 form = safe_eval (form);
4255 UNGCPRO;
4256 unbind_to (count, Qnil);
4259 if (NILP (form))
4260 return 0;
4262 /* Handle `(height HEIGHT)' specifications. */
4263 if (CONSP (spec)
4264 && EQ (XCAR (spec), Qheight)
4265 && CONSP (XCDR (spec)))
4267 if (it)
4269 if (!FRAME_WINDOW_P (it->f))
4270 return 0;
4272 it->font_height = XCAR (XCDR (spec));
4273 if (!NILP (it->font_height))
4275 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4276 int new_height = -1;
4278 if (CONSP (it->font_height)
4279 && (EQ (XCAR (it->font_height), Qplus)
4280 || EQ (XCAR (it->font_height), Qminus))
4281 && CONSP (XCDR (it->font_height))
4282 && INTEGERP (XCAR (XCDR (it->font_height))))
4284 /* `(+ N)' or `(- N)' where N is an integer. */
4285 int steps = XINT (XCAR (XCDR (it->font_height)));
4286 if (EQ (XCAR (it->font_height), Qplus))
4287 steps = - steps;
4288 it->face_id = smaller_face (it->f, it->face_id, steps);
4290 else if (FUNCTIONP (it->font_height))
4292 /* Call function with current height as argument.
4293 Value is the new height. */
4294 Lisp_Object height;
4295 height = safe_call1 (it->font_height,
4296 face->lface[LFACE_HEIGHT_INDEX]);
4297 if (NUMBERP (height))
4298 new_height = XFLOATINT (height);
4300 else if (NUMBERP (it->font_height))
4302 /* Value is a multiple of the canonical char height. */
4303 struct face *f;
4305 f = FACE_FROM_ID (it->f,
4306 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4307 new_height = (XFLOATINT (it->font_height)
4308 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4310 else
4312 /* Evaluate IT->font_height with `height' bound to the
4313 current specified height to get the new height. */
4314 int count = SPECPDL_INDEX ();
4316 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4317 value = safe_eval (it->font_height);
4318 unbind_to (count, Qnil);
4320 if (NUMBERP (value))
4321 new_height = XFLOATINT (value);
4324 if (new_height > 0)
4325 it->face_id = face_with_height (it->f, it->face_id, new_height);
4329 return 0;
4332 /* Handle `(space-width WIDTH)'. */
4333 if (CONSP (spec)
4334 && EQ (XCAR (spec), Qspace_width)
4335 && CONSP (XCDR (spec)))
4337 if (it)
4339 if (!FRAME_WINDOW_P (it->f))
4340 return 0;
4342 value = XCAR (XCDR (spec));
4343 if (NUMBERP (value) && XFLOATINT (value) > 0)
4344 it->space_width = value;
4347 return 0;
4350 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4351 if (CONSP (spec)
4352 && EQ (XCAR (spec), Qslice))
4354 Lisp_Object tem;
4356 if (it)
4358 if (!FRAME_WINDOW_P (it->f))
4359 return 0;
4361 if (tem = XCDR (spec), CONSP (tem))
4363 it->slice.x = XCAR (tem);
4364 if (tem = XCDR (tem), CONSP (tem))
4366 it->slice.y = XCAR (tem);
4367 if (tem = XCDR (tem), CONSP (tem))
4369 it->slice.width = XCAR (tem);
4370 if (tem = XCDR (tem), CONSP (tem))
4371 it->slice.height = XCAR (tem);
4377 return 0;
4380 /* Handle `(raise FACTOR)'. */
4381 if (CONSP (spec)
4382 && EQ (XCAR (spec), Qraise)
4383 && CONSP (XCDR (spec)))
4385 if (it)
4387 if (!FRAME_WINDOW_P (it->f))
4388 return 0;
4390 #ifdef HAVE_WINDOW_SYSTEM
4391 value = XCAR (XCDR (spec));
4392 if (NUMBERP (value))
4394 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4395 it->voffset = - (XFLOATINT (value)
4396 * (FONT_HEIGHT (face->font)));
4398 #endif /* HAVE_WINDOW_SYSTEM */
4401 return 0;
4404 /* Don't handle the other kinds of display specifications
4405 inside a string that we got from a `display' property. */
4406 if (it && it->string_from_display_prop_p)
4407 return 0;
4409 /* Characters having this form of property are not displayed, so
4410 we have to find the end of the property. */
4411 if (it)
4413 start_pos = *position;
4414 *position = display_prop_end (it, object, start_pos);
4416 value = Qnil;
4418 /* Stop the scan at that end position--we assume that all
4419 text properties change there. */
4420 if (it)
4421 it->stop_charpos = position->charpos;
4423 /* Handle `(left-fringe BITMAP [FACE])'
4424 and `(right-fringe BITMAP [FACE])'. */
4425 if (CONSP (spec)
4426 && (EQ (XCAR (spec), Qleft_fringe)
4427 || EQ (XCAR (spec), Qright_fringe))
4428 && CONSP (XCDR (spec)))
4430 int fringe_bitmap;
4432 if (it)
4434 if (!FRAME_WINDOW_P (it->f))
4435 /* If we return here, POSITION has been advanced
4436 across the text with this property. */
4437 return 0;
4439 else if (!frame_window_p)
4440 return 0;
4442 #ifdef HAVE_WINDOW_SYSTEM
4443 value = XCAR (XCDR (spec));
4444 if (!SYMBOLP (value)
4445 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4446 /* If we return here, POSITION has been advanced
4447 across the text with this property. */
4448 return 0;
4450 if (it)
4452 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4454 if (CONSP (XCDR (XCDR (spec))))
4456 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4457 int face_id2 = lookup_derived_face (it->f, face_name,
4458 FRINGE_FACE_ID, 0);
4459 if (face_id2 >= 0)
4460 face_id = face_id2;
4463 /* Save current settings of IT so that we can restore them
4464 when we are finished with the glyph property value. */
4465 push_it (it, position);
4467 it->area = TEXT_AREA;
4468 it->what = IT_IMAGE;
4469 it->image_id = -1; /* no image */
4470 it->position = start_pos;
4471 it->object = NILP (object) ? it->w->buffer : object;
4472 it->method = GET_FROM_IMAGE;
4473 it->from_overlay = Qnil;
4474 it->face_id = face_id;
4475 it->from_disp_prop_p = 1;
4477 /* Say that we haven't consumed the characters with
4478 `display' property yet. The call to pop_it in
4479 set_iterator_to_next will clean this up. */
4480 *position = start_pos;
4482 if (EQ (XCAR (spec), Qleft_fringe))
4484 it->left_user_fringe_bitmap = fringe_bitmap;
4485 it->left_user_fringe_face_id = face_id;
4487 else
4489 it->right_user_fringe_bitmap = fringe_bitmap;
4490 it->right_user_fringe_face_id = face_id;
4493 #endif /* HAVE_WINDOW_SYSTEM */
4494 return 1;
4497 /* Prepare to handle `((margin left-margin) ...)',
4498 `((margin right-margin) ...)' and `((margin nil) ...)'
4499 prefixes for display specifications. */
4500 location = Qunbound;
4501 if (CONSP (spec) && CONSP (XCAR (spec)))
4503 Lisp_Object tem;
4505 value = XCDR (spec);
4506 if (CONSP (value))
4507 value = XCAR (value);
4509 tem = XCAR (spec);
4510 if (EQ (XCAR (tem), Qmargin)
4511 && (tem = XCDR (tem),
4512 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4513 (NILP (tem)
4514 || EQ (tem, Qleft_margin)
4515 || EQ (tem, Qright_margin))))
4516 location = tem;
4519 if (EQ (location, Qunbound))
4521 location = Qnil;
4522 value = spec;
4525 /* After this point, VALUE is the property after any
4526 margin prefix has been stripped. It must be a string,
4527 an image specification, or `(space ...)'.
4529 LOCATION specifies where to display: `left-margin',
4530 `right-margin' or nil. */
4532 valid_p = (STRINGP (value)
4533 #ifdef HAVE_WINDOW_SYSTEM
4534 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4535 && valid_image_p (value))
4536 #endif /* not HAVE_WINDOW_SYSTEM */
4537 || (CONSP (value) && EQ (XCAR (value), Qspace))
4538 #ifdef HAVE_XWIDGETS
4539 || XWIDGETP(value)
4540 #endif
4543 if (valid_p && !display_replaced_p)
4545 int retval = 1;
4547 if (!it)
4549 /* Callers need to know whether the display spec is any kind
4550 of `(space ...)' spec that is about to affect text-area
4551 display. */
4552 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4553 retval = 2;
4554 return retval;
4557 /* Save current settings of IT so that we can restore them
4558 when we are finished with the glyph property value. */
4559 push_it (it, position);
4560 it->from_overlay = overlay;
4561 it->from_disp_prop_p = 1;
4563 if (NILP (location))
4564 it->area = TEXT_AREA;
4565 else if (EQ (location, Qleft_margin))
4566 it->area = LEFT_MARGIN_AREA;
4567 else
4568 it->area = RIGHT_MARGIN_AREA;
4570 if (STRINGP (value))
4572 it->string = value;
4573 it->multibyte_p = STRING_MULTIBYTE (it->string);
4574 it->current.overlay_string_index = -1;
4575 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4576 it->end_charpos = it->string_nchars = SCHARS (it->string);
4577 it->method = GET_FROM_STRING;
4578 it->stop_charpos = 0;
4579 it->prev_stop = 0;
4580 it->base_level_stop = 0;
4581 it->string_from_display_prop_p = 1;
4582 /* Say that we haven't consumed the characters with
4583 `display' property yet. The call to pop_it in
4584 set_iterator_to_next will clean this up. */
4585 if (BUFFERP (object))
4586 *position = start_pos;
4588 /* Force paragraph direction to be that of the parent
4589 object. If the parent object's paragraph direction is
4590 not yet determined, default to L2R. */
4591 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4592 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4593 else
4594 it->paragraph_embedding = L2R;
4596 /* Set up the bidi iterator for this display string. */
4597 if (it->bidi_p)
4599 it->bidi_it.string.lstring = it->string;
4600 it->bidi_it.string.s = NULL;
4601 it->bidi_it.string.schars = it->end_charpos;
4602 it->bidi_it.string.bufpos = bufpos;
4603 it->bidi_it.string.from_disp_str = 1;
4604 it->bidi_it.string.unibyte = !it->multibyte_p;
4605 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4608 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4610 it->method = GET_FROM_STRETCH;
4611 it->object = value;
4612 *position = it->position = start_pos;
4613 retval = 1 + (it->area == TEXT_AREA);
4615 #ifdef HAVE_XWIDGETS
4616 else if (XWIDGETP(value))
4618 //printf("handle_single_display_spec: im an xwidget!!\n");
4619 it->what = IT_XWIDGET;
4620 it->method = GET_FROM_XWIDGET;
4621 it->position = start_pos;
4622 it->object = NILP (object) ? it->w->buffer : object;
4623 *position = start_pos;
4625 it->xwidget = lookup_xwidget(value);
4627 #endif
4628 #ifdef HAVE_WINDOW_SYSTEM
4629 else
4631 it->what = IT_IMAGE;
4632 it->image_id = lookup_image (it->f, value);
4633 it->position = start_pos;
4634 it->object = NILP (object) ? it->w->buffer : object;
4635 it->method = GET_FROM_IMAGE;
4637 /* Say that we haven't consumed the characters with
4638 `display' property yet. The call to pop_it in
4639 set_iterator_to_next will clean this up. */
4640 *position = start_pos;
4642 #endif /* HAVE_WINDOW_SYSTEM */
4644 return retval;
4647 /* Invalid property or property not supported. Restore
4648 POSITION to what it was before. */
4649 *position = start_pos;
4650 return 0;
4653 /* Check if PROP is a display property value whose text should be
4654 treated as intangible. OVERLAY is the overlay from which PROP
4655 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4656 specify the buffer position covered by PROP. */
4659 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4660 EMACS_INT charpos, EMACS_INT bytepos)
4662 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4663 struct text_pos position;
4665 SET_TEXT_POS (position, charpos, bytepos);
4666 return handle_display_spec (NULL, prop, Qnil, overlay,
4667 &position, charpos, frame_window_p);
4671 /* Return 1 if PROP is a display sub-property value containing STRING.
4673 Implementation note: this and the following function are really
4674 special cases of handle_display_spec and
4675 handle_single_display_spec, and should ideally use the same code.
4676 Until they do, these two pairs must be consistent and must be
4677 modified in sync. */
4679 static int
4680 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4682 if (EQ (string, prop))
4683 return 1;
4685 /* Skip over `when FORM'. */
4686 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4688 prop = XCDR (prop);
4689 if (!CONSP (prop))
4690 return 0;
4691 /* Actually, the condition following `when' should be eval'ed,
4692 like handle_single_display_spec does, and we should return
4693 zero if it evaluates to nil. However, this function is
4694 called only when the buffer was already displayed and some
4695 glyph in the glyph matrix was found to come from a display
4696 string. Therefore, the condition was already evaluated, and
4697 the result was non-nil, otherwise the display string wouldn't
4698 have been displayed and we would have never been called for
4699 this property. Thus, we can skip the evaluation and assume
4700 its result is non-nil. */
4701 prop = XCDR (prop);
4704 if (CONSP (prop))
4705 /* Skip over `margin LOCATION'. */
4706 if (EQ (XCAR (prop), Qmargin))
4708 prop = XCDR (prop);
4709 if (!CONSP (prop))
4710 return 0;
4712 prop = XCDR (prop);
4713 if (!CONSP (prop))
4714 return 0;
4717 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4721 /* Return 1 if STRING appears in the `display' property PROP. */
4723 static int
4724 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4726 if (CONSP (prop)
4727 && !EQ (XCAR (prop), Qwhen)
4728 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4730 /* A list of sub-properties. */
4731 while (CONSP (prop))
4733 if (single_display_spec_string_p (XCAR (prop), string))
4734 return 1;
4735 prop = XCDR (prop);
4738 else if (VECTORP (prop))
4740 /* A vector of sub-properties. */
4741 int i;
4742 for (i = 0; i < ASIZE (prop); ++i)
4743 if (single_display_spec_string_p (AREF (prop, i), string))
4744 return 1;
4746 else
4747 return single_display_spec_string_p (prop, string);
4749 return 0;
4752 /* Look for STRING in overlays and text properties in the current
4753 buffer, between character positions FROM and TO (excluding TO).
4754 BACK_P non-zero means look back (in this case, TO is supposed to be
4755 less than FROM).
4756 Value is the first character position where STRING was found, or
4757 zero if it wasn't found before hitting TO.
4759 This function may only use code that doesn't eval because it is
4760 called asynchronously from note_mouse_highlight. */
4762 static EMACS_INT
4763 string_buffer_position_lim (Lisp_Object string,
4764 EMACS_INT from, EMACS_INT to, int back_p)
4766 Lisp_Object limit, prop, pos;
4767 int found = 0;
4769 pos = make_number (from);
4771 if (!back_p) /* looking forward */
4773 limit = make_number (min (to, ZV));
4774 while (!found && !EQ (pos, limit))
4776 prop = Fget_char_property (pos, Qdisplay, Qnil);
4777 if (!NILP (prop) && display_prop_string_p (prop, string))
4778 found = 1;
4779 else
4780 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4781 limit);
4784 else /* looking back */
4786 limit = make_number (max (to, BEGV));
4787 while (!found && !EQ (pos, limit))
4789 prop = Fget_char_property (pos, Qdisplay, Qnil);
4790 if (!NILP (prop) && display_prop_string_p (prop, string))
4791 found = 1;
4792 else
4793 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4794 limit);
4798 return found ? XINT (pos) : 0;
4801 /* Determine which buffer position in current buffer STRING comes from.
4802 AROUND_CHARPOS is an approximate position where it could come from.
4803 Value is the buffer position or 0 if it couldn't be determined.
4805 This function is necessary because we don't record buffer positions
4806 in glyphs generated from strings (to keep struct glyph small).
4807 This function may only use code that doesn't eval because it is
4808 called asynchronously from note_mouse_highlight. */
4810 static EMACS_INT
4811 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4813 const int MAX_DISTANCE = 1000;
4814 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4815 around_charpos + MAX_DISTANCE,
4818 if (!found)
4819 found = string_buffer_position_lim (string, around_charpos,
4820 around_charpos - MAX_DISTANCE, 1);
4821 return found;
4826 /***********************************************************************
4827 `composition' property
4828 ***********************************************************************/
4830 /* Set up iterator IT from `composition' property at its current
4831 position. Called from handle_stop. */
4833 static enum prop_handled
4834 handle_composition_prop (struct it *it)
4836 Lisp_Object prop, string;
4837 EMACS_INT pos, pos_byte, start, end;
4839 if (STRINGP (it->string))
4841 unsigned char *s;
4843 pos = IT_STRING_CHARPOS (*it);
4844 pos_byte = IT_STRING_BYTEPOS (*it);
4845 string = it->string;
4846 s = SDATA (string) + pos_byte;
4847 it->c = STRING_CHAR (s);
4849 else
4851 pos = IT_CHARPOS (*it);
4852 pos_byte = IT_BYTEPOS (*it);
4853 string = Qnil;
4854 it->c = FETCH_CHAR (pos_byte);
4857 /* If there's a valid composition and point is not inside of the
4858 composition (in the case that the composition is from the current
4859 buffer), draw a glyph composed from the composition components. */
4860 if (find_composition (pos, -1, &start, &end, &prop, string)
4861 && COMPOSITION_VALID_P (start, end, prop)
4862 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4864 if (start < pos)
4865 /* As we can't handle this situation (perhaps font-lock added
4866 a new composition), we just return here hoping that next
4867 redisplay will detect this composition much earlier. */
4868 return HANDLED_NORMALLY;
4869 if (start != pos)
4871 if (STRINGP (it->string))
4872 pos_byte = string_char_to_byte (it->string, start);
4873 else
4874 pos_byte = CHAR_TO_BYTE (start);
4876 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4877 prop, string);
4879 if (it->cmp_it.id >= 0)
4881 it->cmp_it.ch = -1;
4882 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4883 it->cmp_it.nglyphs = -1;
4887 return HANDLED_NORMALLY;
4892 /***********************************************************************
4893 Overlay strings
4894 ***********************************************************************/
4896 /* The following structure is used to record overlay strings for
4897 later sorting in load_overlay_strings. */
4899 struct overlay_entry
4901 Lisp_Object overlay;
4902 Lisp_Object string;
4903 int priority;
4904 int after_string_p;
4908 /* Set up iterator IT from overlay strings at its current position.
4909 Called from handle_stop. */
4911 static enum prop_handled
4912 handle_overlay_change (struct it *it)
4914 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4915 return HANDLED_RECOMPUTE_PROPS;
4916 else
4917 return HANDLED_NORMALLY;
4921 /* Set up the next overlay string for delivery by IT, if there is an
4922 overlay string to deliver. Called by set_iterator_to_next when the
4923 end of the current overlay string is reached. If there are more
4924 overlay strings to display, IT->string and
4925 IT->current.overlay_string_index are set appropriately here.
4926 Otherwise IT->string is set to nil. */
4928 static void
4929 next_overlay_string (struct it *it)
4931 ++it->current.overlay_string_index;
4932 if (it->current.overlay_string_index == it->n_overlay_strings)
4934 /* No more overlay strings. Restore IT's settings to what
4935 they were before overlay strings were processed, and
4936 continue to deliver from current_buffer. */
4938 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4939 pop_it (it);
4940 xassert (it->sp > 0
4941 || (NILP (it->string)
4942 && it->method == GET_FROM_BUFFER
4943 && it->stop_charpos >= BEGV
4944 && it->stop_charpos <= it->end_charpos));
4945 it->current.overlay_string_index = -1;
4946 it->n_overlay_strings = 0;
4947 it->overlay_strings_charpos = -1;
4949 /* If we're at the end of the buffer, record that we have
4950 processed the overlay strings there already, so that
4951 next_element_from_buffer doesn't try it again. */
4952 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4953 it->overlay_strings_at_end_processed_p = 1;
4955 else
4957 /* There are more overlay strings to process. If
4958 IT->current.overlay_string_index has advanced to a position
4959 where we must load IT->overlay_strings with more strings, do
4960 it. We must load at the IT->overlay_strings_charpos where
4961 IT->n_overlay_strings was originally computed; when invisible
4962 text is present, this might not be IT_CHARPOS (Bug#7016). */
4963 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4965 if (it->current.overlay_string_index && i == 0)
4966 load_overlay_strings (it, it->overlay_strings_charpos);
4968 /* Initialize IT to deliver display elements from the overlay
4969 string. */
4970 it->string = it->overlay_strings[i];
4971 it->multibyte_p = STRING_MULTIBYTE (it->string);
4972 SET_TEXT_POS (it->current.string_pos, 0, 0);
4973 it->method = GET_FROM_STRING;
4974 it->stop_charpos = 0;
4975 if (it->cmp_it.stop_pos >= 0)
4976 it->cmp_it.stop_pos = 0;
4977 it->prev_stop = 0;
4978 it->base_level_stop = 0;
4980 /* Set up the bidi iterator for this overlay string. */
4981 if (it->bidi_p)
4983 it->bidi_it.string.lstring = it->string;
4984 it->bidi_it.string.s = NULL;
4985 it->bidi_it.string.schars = SCHARS (it->string);
4986 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4987 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4988 it->bidi_it.string.unibyte = !it->multibyte_p;
4989 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4993 CHECK_IT (it);
4997 /* Compare two overlay_entry structures E1 and E2. Used as a
4998 comparison function for qsort in load_overlay_strings. Overlay
4999 strings for the same position are sorted so that
5001 1. All after-strings come in front of before-strings, except
5002 when they come from the same overlay.
5004 2. Within after-strings, strings are sorted so that overlay strings
5005 from overlays with higher priorities come first.
5007 2. Within before-strings, strings are sorted so that overlay
5008 strings from overlays with higher priorities come last.
5010 Value is analogous to strcmp. */
5013 static int
5014 compare_overlay_entries (const void *e1, const void *e2)
5016 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5017 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5018 int result;
5020 if (entry1->after_string_p != entry2->after_string_p)
5022 /* Let after-strings appear in front of before-strings if
5023 they come from different overlays. */
5024 if (EQ (entry1->overlay, entry2->overlay))
5025 result = entry1->after_string_p ? 1 : -1;
5026 else
5027 result = entry1->after_string_p ? -1 : 1;
5029 else if (entry1->after_string_p)
5030 /* After-strings sorted in order of decreasing priority. */
5031 result = entry2->priority - entry1->priority;
5032 else
5033 /* Before-strings sorted in order of increasing priority. */
5034 result = entry1->priority - entry2->priority;
5036 return result;
5040 /* Load the vector IT->overlay_strings with overlay strings from IT's
5041 current buffer position, or from CHARPOS if that is > 0. Set
5042 IT->n_overlays to the total number of overlay strings found.
5044 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5045 a time. On entry into load_overlay_strings,
5046 IT->current.overlay_string_index gives the number of overlay
5047 strings that have already been loaded by previous calls to this
5048 function.
5050 IT->add_overlay_start contains an additional overlay start
5051 position to consider for taking overlay strings from, if non-zero.
5052 This position comes into play when the overlay has an `invisible'
5053 property, and both before and after-strings. When we've skipped to
5054 the end of the overlay, because of its `invisible' property, we
5055 nevertheless want its before-string to appear.
5056 IT->add_overlay_start will contain the overlay start position
5057 in this case.
5059 Overlay strings are sorted so that after-string strings come in
5060 front of before-string strings. Within before and after-strings,
5061 strings are sorted by overlay priority. See also function
5062 compare_overlay_entries. */
5064 static void
5065 load_overlay_strings (struct it *it, EMACS_INT charpos)
5067 Lisp_Object overlay, window, str, invisible;
5068 struct Lisp_Overlay *ov;
5069 EMACS_INT start, end;
5070 int size = 20;
5071 int n = 0, i, j, invis_p;
5072 struct overlay_entry *entries
5073 = (struct overlay_entry *) alloca (size * sizeof *entries);
5075 if (charpos <= 0)
5076 charpos = IT_CHARPOS (*it);
5078 /* Append the overlay string STRING of overlay OVERLAY to vector
5079 `entries' which has size `size' and currently contains `n'
5080 elements. AFTER_P non-zero means STRING is an after-string of
5081 OVERLAY. */
5082 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5083 do \
5085 Lisp_Object priority; \
5087 if (n == size) \
5089 int new_size = 2 * size; \
5090 struct overlay_entry *old = entries; \
5091 entries = \
5092 (struct overlay_entry *) alloca (new_size \
5093 * sizeof *entries); \
5094 memcpy (entries, old, size * sizeof *entries); \
5095 size = new_size; \
5098 entries[n].string = (STRING); \
5099 entries[n].overlay = (OVERLAY); \
5100 priority = Foverlay_get ((OVERLAY), Qpriority); \
5101 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5102 entries[n].after_string_p = (AFTER_P); \
5103 ++n; \
5105 while (0)
5107 /* Process overlay before the overlay center. */
5108 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5110 XSETMISC (overlay, ov);
5111 xassert (OVERLAYP (overlay));
5112 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5113 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5115 if (end < charpos)
5116 break;
5118 /* Skip this overlay if it doesn't start or end at IT's current
5119 position. */
5120 if (end != charpos && start != charpos)
5121 continue;
5123 /* Skip this overlay if it doesn't apply to IT->w. */
5124 window = Foverlay_get (overlay, Qwindow);
5125 if (WINDOWP (window) && XWINDOW (window) != it->w)
5126 continue;
5128 /* If the text ``under'' the overlay is invisible, both before-
5129 and after-strings from this overlay are visible; start and
5130 end position are indistinguishable. */
5131 invisible = Foverlay_get (overlay, Qinvisible);
5132 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5134 /* If overlay has a non-empty before-string, record it. */
5135 if ((start == charpos || (end == charpos && invis_p))
5136 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5137 && SCHARS (str))
5138 RECORD_OVERLAY_STRING (overlay, str, 0);
5140 /* If overlay has a non-empty after-string, record it. */
5141 if ((end == charpos || (start == charpos && invis_p))
5142 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5143 && SCHARS (str))
5144 RECORD_OVERLAY_STRING (overlay, str, 1);
5147 /* Process overlays after the overlay center. */
5148 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5150 XSETMISC (overlay, ov);
5151 xassert (OVERLAYP (overlay));
5152 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5153 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5155 if (start > charpos)
5156 break;
5158 /* Skip this overlay if it doesn't start or end at IT's current
5159 position. */
5160 if (end != charpos && start != charpos)
5161 continue;
5163 /* Skip this overlay if it doesn't apply to IT->w. */
5164 window = Foverlay_get (overlay, Qwindow);
5165 if (WINDOWP (window) && XWINDOW (window) != it->w)
5166 continue;
5168 /* If the text ``under'' the overlay is invisible, it has a zero
5169 dimension, and both before- and after-strings apply. */
5170 invisible = Foverlay_get (overlay, Qinvisible);
5171 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5173 /* If overlay has a non-empty before-string, record it. */
5174 if ((start == charpos || (end == charpos && invis_p))
5175 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5176 && SCHARS (str))
5177 RECORD_OVERLAY_STRING (overlay, str, 0);
5179 /* If overlay has a non-empty after-string, record it. */
5180 if ((end == charpos || (start == charpos && invis_p))
5181 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5182 && SCHARS (str))
5183 RECORD_OVERLAY_STRING (overlay, str, 1);
5186 #undef RECORD_OVERLAY_STRING
5188 /* Sort entries. */
5189 if (n > 1)
5190 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5192 /* Record number of overlay strings, and where we computed it. */
5193 it->n_overlay_strings = n;
5194 it->overlay_strings_charpos = charpos;
5196 /* IT->current.overlay_string_index is the number of overlay strings
5197 that have already been consumed by IT. Copy some of the
5198 remaining overlay strings to IT->overlay_strings. */
5199 i = 0;
5200 j = it->current.overlay_string_index;
5201 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5203 it->overlay_strings[i] = entries[j].string;
5204 it->string_overlays[i++] = entries[j++].overlay;
5207 CHECK_IT (it);
5211 /* Get the first chunk of overlay strings at IT's current buffer
5212 position, or at CHARPOS if that is > 0. Value is non-zero if at
5213 least one overlay string was found. */
5215 static int
5216 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5218 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5219 process. This fills IT->overlay_strings with strings, and sets
5220 IT->n_overlay_strings to the total number of strings to process.
5221 IT->pos.overlay_string_index has to be set temporarily to zero
5222 because load_overlay_strings needs this; it must be set to -1
5223 when no overlay strings are found because a zero value would
5224 indicate a position in the first overlay string. */
5225 it->current.overlay_string_index = 0;
5226 load_overlay_strings (it, charpos);
5228 /* If we found overlay strings, set up IT to deliver display
5229 elements from the first one. Otherwise set up IT to deliver
5230 from current_buffer. */
5231 if (it->n_overlay_strings)
5233 /* Make sure we know settings in current_buffer, so that we can
5234 restore meaningful values when we're done with the overlay
5235 strings. */
5236 if (compute_stop_p)
5237 compute_stop_pos (it);
5238 xassert (it->face_id >= 0);
5240 /* Save IT's settings. They are restored after all overlay
5241 strings have been processed. */
5242 xassert (!compute_stop_p || it->sp == 0);
5244 /* When called from handle_stop, there might be an empty display
5245 string loaded. In that case, don't bother saving it. */
5246 if (!STRINGP (it->string) || SCHARS (it->string))
5247 push_it (it, NULL);
5249 /* Set up IT to deliver display elements from the first overlay
5250 string. */
5251 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5252 it->string = it->overlay_strings[0];
5253 it->from_overlay = Qnil;
5254 it->stop_charpos = 0;
5255 xassert (STRINGP (it->string));
5256 it->end_charpos = SCHARS (it->string);
5257 it->prev_stop = 0;
5258 it->base_level_stop = 0;
5259 it->multibyte_p = STRING_MULTIBYTE (it->string);
5260 it->method = GET_FROM_STRING;
5261 it->from_disp_prop_p = 0;
5263 /* Force paragraph direction to be that of the parent
5264 buffer. */
5265 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5266 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5267 else
5268 it->paragraph_embedding = L2R;
5270 /* Set up the bidi iterator for this overlay string. */
5271 if (it->bidi_p)
5273 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5275 it->bidi_it.string.lstring = it->string;
5276 it->bidi_it.string.s = NULL;
5277 it->bidi_it.string.schars = SCHARS (it->string);
5278 it->bidi_it.string.bufpos = pos;
5279 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5280 it->bidi_it.string.unibyte = !it->multibyte_p;
5281 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5283 return 1;
5286 it->current.overlay_string_index = -1;
5287 return 0;
5290 static int
5291 get_overlay_strings (struct it *it, EMACS_INT charpos)
5293 it->string = Qnil;
5294 it->method = GET_FROM_BUFFER;
5296 (void) get_overlay_strings_1 (it, charpos, 1);
5298 CHECK_IT (it);
5300 /* Value is non-zero if we found at least one overlay string. */
5301 return STRINGP (it->string);
5306 /***********************************************************************
5307 Saving and restoring state
5308 ***********************************************************************/
5310 /* Save current settings of IT on IT->stack. Called, for example,
5311 before setting up IT for an overlay string, to be able to restore
5312 IT's settings to what they were after the overlay string has been
5313 processed. If POSITION is non-NULL, it is the position to save on
5314 the stack instead of IT->position. */
5316 static void
5317 push_it (struct it *it, struct text_pos *position)
5319 struct iterator_stack_entry *p;
5321 xassert (it->sp < IT_STACK_SIZE);
5322 p = it->stack + it->sp;
5324 p->stop_charpos = it->stop_charpos;
5325 p->prev_stop = it->prev_stop;
5326 p->base_level_stop = it->base_level_stop;
5327 p->cmp_it = it->cmp_it;
5328 xassert (it->face_id >= 0);
5329 p->face_id = it->face_id;
5330 p->string = it->string;
5331 p->method = it->method;
5332 p->from_overlay = it->from_overlay;
5333 switch (p->method)
5335 case GET_FROM_IMAGE:
5336 p->u.image.object = it->object;
5337 p->u.image.image_id = it->image_id;
5338 p->u.image.slice = it->slice;
5339 break;
5340 case GET_FROM_STRETCH:
5341 p->u.stretch.object = it->object;
5342 break;
5343 #ifdef HAVE_XWIDGETS
5344 case GET_FROM_XWIDGET:
5345 p->u.xwidget.object = it->object;
5346 break;
5347 #endif
5349 p->position = position ? *position : it->position;
5350 p->current = it->current;
5351 p->end_charpos = it->end_charpos;
5352 p->string_nchars = it->string_nchars;
5353 p->area = it->area;
5354 p->multibyte_p = it->multibyte_p;
5355 p->avoid_cursor_p = it->avoid_cursor_p;
5356 p->space_width = it->space_width;
5357 p->font_height = it->font_height;
5358 p->voffset = it->voffset;
5359 p->string_from_display_prop_p = it->string_from_display_prop_p;
5360 p->display_ellipsis_p = 0;
5361 p->line_wrap = it->line_wrap;
5362 p->bidi_p = it->bidi_p;
5363 p->paragraph_embedding = it->paragraph_embedding;
5364 p->from_disp_prop_p = it->from_disp_prop_p;
5365 ++it->sp;
5367 /* Save the state of the bidi iterator as well. */
5368 if (it->bidi_p)
5369 bidi_push_it (&it->bidi_it);
5372 static void
5373 iterate_out_of_display_property (struct it *it)
5375 int buffer_p = BUFFERP (it->object);
5376 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5377 EMACS_INT bob = (buffer_p ? BEGV : 0);
5379 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5381 /* Maybe initialize paragraph direction. If we are at the beginning
5382 of a new paragraph, next_element_from_buffer may not have a
5383 chance to do that. */
5384 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5385 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5386 /* prev_stop can be zero, so check against BEGV as well. */
5387 while (it->bidi_it.charpos >= bob
5388 && it->prev_stop <= it->bidi_it.charpos
5389 && it->bidi_it.charpos < CHARPOS (it->position)
5390 && it->bidi_it.charpos < eob)
5391 bidi_move_to_visually_next (&it->bidi_it);
5392 /* Record the stop_pos we just crossed, for when we cross it
5393 back, maybe. */
5394 if (it->bidi_it.charpos > CHARPOS (it->position))
5395 it->prev_stop = CHARPOS (it->position);
5396 /* If we ended up not where pop_it put us, resync IT's
5397 positional members with the bidi iterator. */
5398 if (it->bidi_it.charpos != CHARPOS (it->position))
5399 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5400 if (buffer_p)
5401 it->current.pos = it->position;
5402 else
5403 it->current.string_pos = it->position;
5406 /* Restore IT's settings from IT->stack. Called, for example, when no
5407 more overlay strings must be processed, and we return to delivering
5408 display elements from a buffer, or when the end of a string from a
5409 `display' property is reached and we return to delivering display
5410 elements from an overlay string, or from a buffer. */
5412 static void
5413 pop_it (struct it *it)
5415 struct iterator_stack_entry *p;
5416 int from_display_prop = it->from_disp_prop_p;
5418 xassert (it->sp > 0);
5419 --it->sp;
5420 p = it->stack + it->sp;
5421 it->stop_charpos = p->stop_charpos;
5422 it->prev_stop = p->prev_stop;
5423 it->base_level_stop = p->base_level_stop;
5424 it->cmp_it = p->cmp_it;
5425 it->face_id = p->face_id;
5426 it->current = p->current;
5427 it->position = p->position;
5428 it->string = p->string;
5429 it->from_overlay = p->from_overlay;
5430 if (NILP (it->string))
5431 SET_TEXT_POS (it->current.string_pos, -1, -1);
5432 it->method = p->method;
5433 switch (it->method)
5435 case GET_FROM_IMAGE:
5436 it->image_id = p->u.image.image_id;
5437 it->object = p->u.image.object;
5438 it->slice = p->u.image.slice;
5439 break;
5440 #ifdef HAVE_XWIDGETS
5441 case GET_FROM_XWIDGET:
5442 it->object = p->u.xwidget.object;
5443 break;
5444 #endif
5445 case GET_FROM_STRETCH:
5446 it->object = p->u.stretch.object;
5447 break;
5448 case GET_FROM_BUFFER:
5449 it->object = it->w->buffer;
5450 break;
5451 case GET_FROM_STRING:
5452 it->object = it->string;
5453 break;
5454 case GET_FROM_DISPLAY_VECTOR:
5455 if (it->s)
5456 it->method = GET_FROM_C_STRING;
5457 else if (STRINGP (it->string))
5458 it->method = GET_FROM_STRING;
5459 else
5461 it->method = GET_FROM_BUFFER;
5462 it->object = it->w->buffer;
5465 it->end_charpos = p->end_charpos;
5466 it->string_nchars = p->string_nchars;
5467 it->area = p->area;
5468 it->multibyte_p = p->multibyte_p;
5469 it->avoid_cursor_p = p->avoid_cursor_p;
5470 it->space_width = p->space_width;
5471 it->font_height = p->font_height;
5472 it->voffset = p->voffset;
5473 it->string_from_display_prop_p = p->string_from_display_prop_p;
5474 it->line_wrap = p->line_wrap;
5475 it->bidi_p = p->bidi_p;
5476 it->paragraph_embedding = p->paragraph_embedding;
5477 it->from_disp_prop_p = p->from_disp_prop_p;
5478 if (it->bidi_p)
5480 bidi_pop_it (&it->bidi_it);
5481 /* Bidi-iterate until we get out of the portion of text, if any,
5482 covered by a `display' text property or by an overlay with
5483 `display' property. (We cannot just jump there, because the
5484 internal coherency of the bidi iterator state can not be
5485 preserved across such jumps.) We also must determine the
5486 paragraph base direction if the overlay we just processed is
5487 at the beginning of a new paragraph. */
5488 if (from_display_prop
5489 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5490 iterate_out_of_display_property (it);
5492 xassert ((BUFFERP (it->object)
5493 && IT_CHARPOS (*it) == it->bidi_it.charpos
5494 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5495 || (STRINGP (it->object)
5496 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5497 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos));
5503 /***********************************************************************
5504 Moving over lines
5505 ***********************************************************************/
5507 /* Set IT's current position to the previous line start. */
5509 static void
5510 back_to_previous_line_start (struct it *it)
5512 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5513 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5517 /* Move IT to the next line start.
5519 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5520 we skipped over part of the text (as opposed to moving the iterator
5521 continuously over the text). Otherwise, don't change the value
5522 of *SKIPPED_P.
5524 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5525 iterator on the newline, if it was found.
5527 Newlines may come from buffer text, overlay strings, or strings
5528 displayed via the `display' property. That's the reason we can't
5529 simply use find_next_newline_no_quit.
5531 Note that this function may not skip over invisible text that is so
5532 because of text properties and immediately follows a newline. If
5533 it would, function reseat_at_next_visible_line_start, when called
5534 from set_iterator_to_next, would effectively make invisible
5535 characters following a newline part of the wrong glyph row, which
5536 leads to wrong cursor motion. */
5538 static int
5539 forward_to_next_line_start (struct it *it, int *skipped_p,
5540 struct bidi_it *bidi_it_prev)
5542 EMACS_INT old_selective;
5543 int newline_found_p, n;
5544 const int MAX_NEWLINE_DISTANCE = 500;
5546 /* If already on a newline, just consume it to avoid unintended
5547 skipping over invisible text below. */
5548 if (it->what == IT_CHARACTER
5549 && it->c == '\n'
5550 && CHARPOS (it->position) == IT_CHARPOS (*it))
5552 if (it->bidi_p && bidi_it_prev)
5553 *bidi_it_prev = it->bidi_it;
5554 set_iterator_to_next (it, 0);
5555 it->c = 0;
5556 return 1;
5559 /* Don't handle selective display in the following. It's (a)
5560 unnecessary because it's done by the caller, and (b) leads to an
5561 infinite recursion because next_element_from_ellipsis indirectly
5562 calls this function. */
5563 old_selective = it->selective;
5564 it->selective = 0;
5566 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5567 from buffer text. */
5568 for (n = newline_found_p = 0;
5569 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5570 n += STRINGP (it->string) ? 0 : 1)
5572 if (!get_next_display_element (it))
5573 return 0;
5574 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5575 if (newline_found_p && it->bidi_p && bidi_it_prev)
5576 *bidi_it_prev = it->bidi_it;
5577 set_iterator_to_next (it, 0);
5580 /* If we didn't find a newline near enough, see if we can use a
5581 short-cut. */
5582 if (!newline_found_p)
5584 EMACS_INT start = IT_CHARPOS (*it);
5585 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5586 Lisp_Object pos;
5588 xassert (!STRINGP (it->string));
5590 /* If there isn't any `display' property in sight, and no
5591 overlays, we can just use the position of the newline in
5592 buffer text. */
5593 if (it->stop_charpos >= limit
5594 || ((pos = Fnext_single_property_change (make_number (start),
5595 Qdisplay, Qnil,
5596 make_number (limit)),
5597 NILP (pos))
5598 && next_overlay_change (start) == ZV))
5600 if (!it->bidi_p)
5602 IT_CHARPOS (*it) = limit;
5603 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5605 else
5607 struct bidi_it bprev;
5609 /* Help bidi.c avoid expensive searches for display
5610 properties and overlays, by telling it that there are
5611 none up to `limit'. */
5612 if (it->bidi_it.disp_pos < limit)
5614 it->bidi_it.disp_pos = limit;
5615 it->bidi_it.disp_prop = 0;
5617 do {
5618 bprev = it->bidi_it;
5619 bidi_move_to_visually_next (&it->bidi_it);
5620 } while (it->bidi_it.charpos != limit);
5621 IT_CHARPOS (*it) = limit;
5622 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5623 if (bidi_it_prev)
5624 *bidi_it_prev = bprev;
5626 *skipped_p = newline_found_p = 1;
5628 else
5630 while (get_next_display_element (it)
5631 && !newline_found_p)
5633 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5634 if (newline_found_p && it->bidi_p && bidi_it_prev)
5635 *bidi_it_prev = it->bidi_it;
5636 set_iterator_to_next (it, 0);
5641 it->selective = old_selective;
5642 return newline_found_p;
5646 /* Set IT's current position to the previous visible line start. Skip
5647 invisible text that is so either due to text properties or due to
5648 selective display. Caution: this does not change IT->current_x and
5649 IT->hpos. */
5651 static void
5652 back_to_previous_visible_line_start (struct it *it)
5654 while (IT_CHARPOS (*it) > BEGV)
5656 back_to_previous_line_start (it);
5658 if (IT_CHARPOS (*it) <= BEGV)
5659 break;
5661 /* If selective > 0, then lines indented more than its value are
5662 invisible. */
5663 if (it->selective > 0
5664 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5665 it->selective))
5666 continue;
5668 /* Check the newline before point for invisibility. */
5670 Lisp_Object prop;
5671 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5672 Qinvisible, it->window);
5673 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5674 continue;
5677 if (IT_CHARPOS (*it) <= BEGV)
5678 break;
5681 struct it it2;
5682 void *it2data = NULL;
5683 EMACS_INT pos;
5684 EMACS_INT beg, end;
5685 Lisp_Object val, overlay;
5687 SAVE_IT (it2, *it, it2data);
5689 /* If newline is part of a composition, continue from start of composition */
5690 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5691 && beg < IT_CHARPOS (*it))
5692 goto replaced;
5694 /* If newline is replaced by a display property, find start of overlay
5695 or interval and continue search from that point. */
5696 pos = --IT_CHARPOS (it2);
5697 --IT_BYTEPOS (it2);
5698 it2.sp = 0;
5699 bidi_unshelve_cache (NULL, 0);
5700 it2.string_from_display_prop_p = 0;
5701 it2.from_disp_prop_p = 0;
5702 if (handle_display_prop (&it2) == HANDLED_RETURN
5703 && !NILP (val = get_char_property_and_overlay
5704 (make_number (pos), Qdisplay, Qnil, &overlay))
5705 && (OVERLAYP (overlay)
5706 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5707 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5709 RESTORE_IT (it, it, it2data);
5710 goto replaced;
5713 /* Newline is not replaced by anything -- so we are done. */
5714 RESTORE_IT (it, it, it2data);
5715 break;
5717 replaced:
5718 if (beg < BEGV)
5719 beg = BEGV;
5720 IT_CHARPOS (*it) = beg;
5721 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5725 it->continuation_lines_width = 0;
5727 xassert (IT_CHARPOS (*it) >= BEGV);
5728 xassert (IT_CHARPOS (*it) == BEGV
5729 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5730 CHECK_IT (it);
5734 /* Reseat iterator IT at the previous visible line start. Skip
5735 invisible text that is so either due to text properties or due to
5736 selective display. At the end, update IT's overlay information,
5737 face information etc. */
5739 void
5740 reseat_at_previous_visible_line_start (struct it *it)
5742 back_to_previous_visible_line_start (it);
5743 reseat (it, it->current.pos, 1);
5744 CHECK_IT (it);
5748 /* Reseat iterator IT on the next visible line start in the current
5749 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5750 preceding the line start. Skip over invisible text that is so
5751 because of selective display. Compute faces, overlays etc at the
5752 new position. Note that this function does not skip over text that
5753 is invisible because of text properties. */
5755 static void
5756 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5758 int newline_found_p, skipped_p = 0;
5759 struct bidi_it bidi_it_prev;
5761 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5763 /* Skip over lines that are invisible because they are indented
5764 more than the value of IT->selective. */
5765 if (it->selective > 0)
5766 while (IT_CHARPOS (*it) < ZV
5767 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5768 it->selective))
5770 xassert (IT_BYTEPOS (*it) == BEGV
5771 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5772 newline_found_p =
5773 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5776 /* Position on the newline if that's what's requested. */
5777 if (on_newline_p && newline_found_p)
5779 if (STRINGP (it->string))
5781 if (IT_STRING_CHARPOS (*it) > 0)
5783 if (!it->bidi_p)
5785 --IT_STRING_CHARPOS (*it);
5786 --IT_STRING_BYTEPOS (*it);
5788 else
5790 /* We need to restore the bidi iterator to the state
5791 it had on the newline, and resync the IT's
5792 position with that. */
5793 it->bidi_it = bidi_it_prev;
5794 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
5795 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
5799 else if (IT_CHARPOS (*it) > BEGV)
5801 if (!it->bidi_p)
5803 --IT_CHARPOS (*it);
5804 --IT_BYTEPOS (*it);
5806 else
5808 /* We need to restore the bidi iterator to the state it
5809 had on the newline and resync IT with that. */
5810 it->bidi_it = bidi_it_prev;
5811 IT_CHARPOS (*it) = it->bidi_it.charpos;
5812 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5814 reseat (it, it->current.pos, 0);
5817 else if (skipped_p)
5818 reseat (it, it->current.pos, 0);
5820 CHECK_IT (it);
5825 /***********************************************************************
5826 Changing an iterator's position
5827 ***********************************************************************/
5829 /* Change IT's current position to POS in current_buffer. If FORCE_P
5830 is non-zero, always check for text properties at the new position.
5831 Otherwise, text properties are only looked up if POS >=
5832 IT->check_charpos of a property. */
5834 static void
5835 reseat (struct it *it, struct text_pos pos, int force_p)
5837 EMACS_INT original_pos = IT_CHARPOS (*it);
5839 reseat_1 (it, pos, 0);
5841 /* Determine where to check text properties. Avoid doing it
5842 where possible because text property lookup is very expensive. */
5843 if (force_p
5844 || CHARPOS (pos) > it->stop_charpos
5845 || CHARPOS (pos) < original_pos)
5847 if (it->bidi_p)
5849 /* For bidi iteration, we need to prime prev_stop and
5850 base_level_stop with our best estimations. */
5851 /* Implementation note: Of course, POS is not necessarily a
5852 stop position, so assigning prev_pos to it is a lie; we
5853 should have called compute_stop_backwards. However, if
5854 the current buffer does not include any R2L characters,
5855 that call would be a waste of cycles, because the
5856 iterator will never move back, and thus never cross this
5857 "fake" stop position. So we delay that backward search
5858 until the time we really need it, in next_element_from_buffer. */
5859 if (CHARPOS (pos) != it->prev_stop)
5860 it->prev_stop = CHARPOS (pos);
5861 if (CHARPOS (pos) < it->base_level_stop)
5862 it->base_level_stop = 0; /* meaning it's unknown */
5863 handle_stop (it);
5865 else
5867 handle_stop (it);
5868 it->prev_stop = it->base_level_stop = 0;
5873 CHECK_IT (it);
5877 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5878 IT->stop_pos to POS, also. */
5880 static void
5881 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5883 /* Don't call this function when scanning a C string. */
5884 xassert (it->s == NULL);
5886 /* POS must be a reasonable value. */
5887 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5889 it->current.pos = it->position = pos;
5890 it->end_charpos = ZV;
5891 it->dpvec = NULL;
5892 it->current.dpvec_index = -1;
5893 it->current.overlay_string_index = -1;
5894 IT_STRING_CHARPOS (*it) = -1;
5895 IT_STRING_BYTEPOS (*it) = -1;
5896 it->string = Qnil;
5897 it->method = GET_FROM_BUFFER;
5898 it->object = it->w->buffer;
5899 it->area = TEXT_AREA;
5900 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5901 it->sp = 0;
5902 it->string_from_display_prop_p = 0;
5903 it->from_disp_prop_p = 0;
5904 it->face_before_selective_p = 0;
5905 if (it->bidi_p)
5907 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5908 &it->bidi_it);
5909 bidi_unshelve_cache (NULL, 0);
5910 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5911 it->bidi_it.string.s = NULL;
5912 it->bidi_it.string.lstring = Qnil;
5913 it->bidi_it.string.bufpos = 0;
5914 it->bidi_it.string.unibyte = 0;
5917 if (set_stop_p)
5919 it->stop_charpos = CHARPOS (pos);
5920 it->base_level_stop = CHARPOS (pos);
5925 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5926 If S is non-null, it is a C string to iterate over. Otherwise,
5927 STRING gives a Lisp string to iterate over.
5929 If PRECISION > 0, don't return more then PRECISION number of
5930 characters from the string.
5932 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5933 characters have been returned. FIELD_WIDTH < 0 means an infinite
5934 field width.
5936 MULTIBYTE = 0 means disable processing of multibyte characters,
5937 MULTIBYTE > 0 means enable it,
5938 MULTIBYTE < 0 means use IT->multibyte_p.
5940 IT must be initialized via a prior call to init_iterator before
5941 calling this function. */
5943 static void
5944 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5945 EMACS_INT charpos, EMACS_INT precision, int field_width,
5946 int multibyte)
5948 /* No region in strings. */
5949 it->region_beg_charpos = it->region_end_charpos = -1;
5951 /* No text property checks performed by default, but see below. */
5952 it->stop_charpos = -1;
5954 /* Set iterator position and end position. */
5955 memset (&it->current, 0, sizeof it->current);
5956 it->current.overlay_string_index = -1;
5957 it->current.dpvec_index = -1;
5958 xassert (charpos >= 0);
5960 /* If STRING is specified, use its multibyteness, otherwise use the
5961 setting of MULTIBYTE, if specified. */
5962 if (multibyte >= 0)
5963 it->multibyte_p = multibyte > 0;
5965 /* Bidirectional reordering of strings is controlled by the default
5966 value of bidi-display-reordering. */
5967 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5969 if (s == NULL)
5971 xassert (STRINGP (string));
5972 it->string = string;
5973 it->s = NULL;
5974 it->end_charpos = it->string_nchars = SCHARS (string);
5975 it->method = GET_FROM_STRING;
5976 it->current.string_pos = string_pos (charpos, string);
5978 if (it->bidi_p)
5980 it->bidi_it.string.lstring = string;
5981 it->bidi_it.string.s = NULL;
5982 it->bidi_it.string.schars = it->end_charpos;
5983 it->bidi_it.string.bufpos = 0;
5984 it->bidi_it.string.from_disp_str = 0;
5985 it->bidi_it.string.unibyte = !it->multibyte_p;
5986 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5987 FRAME_WINDOW_P (it->f), &it->bidi_it);
5990 else
5992 it->s = (const unsigned char *) s;
5993 it->string = Qnil;
5995 /* Note that we use IT->current.pos, not it->current.string_pos,
5996 for displaying C strings. */
5997 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5998 if (it->multibyte_p)
6000 it->current.pos = c_string_pos (charpos, s, 1);
6001 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6003 else
6005 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6006 it->end_charpos = it->string_nchars = strlen (s);
6009 if (it->bidi_p)
6011 it->bidi_it.string.lstring = Qnil;
6012 it->bidi_it.string.s = (const unsigned char *) s;
6013 it->bidi_it.string.schars = it->end_charpos;
6014 it->bidi_it.string.bufpos = 0;
6015 it->bidi_it.string.from_disp_str = 0;
6016 it->bidi_it.string.unibyte = !it->multibyte_p;
6017 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6018 &it->bidi_it);
6020 it->method = GET_FROM_C_STRING;
6023 /* PRECISION > 0 means don't return more than PRECISION characters
6024 from the string. */
6025 if (precision > 0 && it->end_charpos - charpos > precision)
6027 it->end_charpos = it->string_nchars = charpos + precision;
6028 if (it->bidi_p)
6029 it->bidi_it.string.schars = it->end_charpos;
6032 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6033 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6034 FIELD_WIDTH < 0 means infinite field width. This is useful for
6035 padding with `-' at the end of a mode line. */
6036 if (field_width < 0)
6037 field_width = INFINITY;
6038 /* Implementation note: We deliberately don't enlarge
6039 it->bidi_it.string.schars here to fit it->end_charpos, because
6040 the bidi iterator cannot produce characters out of thin air. */
6041 if (field_width > it->end_charpos - charpos)
6042 it->end_charpos = charpos + field_width;
6044 /* Use the standard display table for displaying strings. */
6045 if (DISP_TABLE_P (Vstandard_display_table))
6046 it->dp = XCHAR_TABLE (Vstandard_display_table);
6048 it->stop_charpos = charpos;
6049 it->prev_stop = charpos;
6050 it->base_level_stop = 0;
6051 if (it->bidi_p)
6053 it->bidi_it.first_elt = 1;
6054 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6055 it->bidi_it.disp_pos = -1;
6057 if (s == NULL && it->multibyte_p)
6059 EMACS_INT endpos = SCHARS (it->string);
6060 if (endpos > it->end_charpos)
6061 endpos = it->end_charpos;
6062 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6063 it->string);
6065 CHECK_IT (it);
6070 /***********************************************************************
6071 Iteration
6072 ***********************************************************************/
6074 /* Map enum it_method value to corresponding next_element_from_* function. */
6076 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6078 next_element_from_buffer,
6079 next_element_from_display_vector,
6080 next_element_from_string,
6081 next_element_from_c_string,
6082 next_element_from_image,
6083 next_element_from_stretch
6084 #ifdef HAVE_XWIDGETS
6085 ,next_element_from_xwidget
6086 #endif
6089 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6092 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6093 (possibly with the following characters). */
6095 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6096 ((IT)->cmp_it.id >= 0 \
6097 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6098 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6099 END_CHARPOS, (IT)->w, \
6100 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6101 (IT)->string)))
6104 /* Lookup the char-table Vglyphless_char_display for character C (-1
6105 if we want information for no-font case), and return the display
6106 method symbol. By side-effect, update it->what and
6107 it->glyphless_method. This function is called from
6108 get_next_display_element for each character element, and from
6109 x_produce_glyphs when no suitable font was found. */
6111 Lisp_Object
6112 lookup_glyphless_char_display (int c, struct it *it)
6114 Lisp_Object glyphless_method = Qnil;
6116 if (CHAR_TABLE_P (Vglyphless_char_display)
6117 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6119 if (c >= 0)
6121 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6122 if (CONSP (glyphless_method))
6123 glyphless_method = FRAME_WINDOW_P (it->f)
6124 ? XCAR (glyphless_method)
6125 : XCDR (glyphless_method);
6127 else
6128 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6131 retry:
6132 if (NILP (glyphless_method))
6134 if (c >= 0)
6135 /* The default is to display the character by a proper font. */
6136 return Qnil;
6137 /* The default for the no-font case is to display an empty box. */
6138 glyphless_method = Qempty_box;
6140 if (EQ (glyphless_method, Qzero_width))
6142 if (c >= 0)
6143 return glyphless_method;
6144 /* This method can't be used for the no-font case. */
6145 glyphless_method = Qempty_box;
6147 if (EQ (glyphless_method, Qthin_space))
6148 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6149 else if (EQ (glyphless_method, Qempty_box))
6150 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6151 else if (EQ (glyphless_method, Qhex_code))
6152 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6153 else if (STRINGP (glyphless_method))
6154 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6155 else
6157 /* Invalid value. We use the default method. */
6158 glyphless_method = Qnil;
6159 goto retry;
6161 it->what = IT_GLYPHLESS;
6162 return glyphless_method;
6165 /* Load IT's display element fields with information about the next
6166 display element from the current position of IT. Value is zero if
6167 end of buffer (or C string) is reached. */
6169 static struct frame *last_escape_glyph_frame = NULL;
6170 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6171 static int last_escape_glyph_merged_face_id = 0;
6173 struct frame *last_glyphless_glyph_frame = NULL;
6174 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6175 int last_glyphless_glyph_merged_face_id = 0;
6177 static int
6178 get_next_display_element (struct it *it)
6180 /* Non-zero means that we found a display element. Zero means that
6181 we hit the end of what we iterate over. Performance note: the
6182 function pointer `method' used here turns out to be faster than
6183 using a sequence of if-statements. */
6184 int success_p;
6186 get_next:
6187 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6189 if (it->what == IT_CHARACTER)
6191 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6192 and only if (a) the resolved directionality of that character
6193 is R..." */
6194 /* FIXME: Do we need an exception for characters from display
6195 tables? */
6196 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6197 it->c = bidi_mirror_char (it->c);
6198 /* Map via display table or translate control characters.
6199 IT->c, IT->len etc. have been set to the next character by
6200 the function call above. If we have a display table, and it
6201 contains an entry for IT->c, translate it. Don't do this if
6202 IT->c itself comes from a display table, otherwise we could
6203 end up in an infinite recursion. (An alternative could be to
6204 count the recursion depth of this function and signal an
6205 error when a certain maximum depth is reached.) Is it worth
6206 it? */
6207 if (success_p && it->dpvec == NULL)
6209 Lisp_Object dv;
6210 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6211 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6212 nbsp_or_shy = char_is_other;
6213 int c = it->c; /* This is the character to display. */
6215 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6217 xassert (SINGLE_BYTE_CHAR_P (c));
6218 if (unibyte_display_via_language_environment)
6220 c = DECODE_CHAR (unibyte, c);
6221 if (c < 0)
6222 c = BYTE8_TO_CHAR (it->c);
6224 else
6225 c = BYTE8_TO_CHAR (it->c);
6228 if (it->dp
6229 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6230 VECTORP (dv)))
6232 struct Lisp_Vector *v = XVECTOR (dv);
6234 /* Return the first character from the display table
6235 entry, if not empty. If empty, don't display the
6236 current character. */
6237 if (v->header.size)
6239 it->dpvec_char_len = it->len;
6240 it->dpvec = v->contents;
6241 it->dpend = v->contents + v->header.size;
6242 it->current.dpvec_index = 0;
6243 it->dpvec_face_id = -1;
6244 it->saved_face_id = it->face_id;
6245 it->method = GET_FROM_DISPLAY_VECTOR;
6246 it->ellipsis_p = 0;
6248 else
6250 set_iterator_to_next (it, 0);
6252 goto get_next;
6255 if (! NILP (lookup_glyphless_char_display (c, it)))
6257 if (it->what == IT_GLYPHLESS)
6258 goto done;
6259 /* Don't display this character. */
6260 set_iterator_to_next (it, 0);
6261 goto get_next;
6264 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6265 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6266 : c == 0xAD ? char_is_soft_hyphen
6267 : char_is_other);
6269 /* Translate control characters into `\003' or `^C' form.
6270 Control characters coming from a display table entry are
6271 currently not translated because we use IT->dpvec to hold
6272 the translation. This could easily be changed but I
6273 don't believe that it is worth doing.
6275 NBSP and SOFT-HYPEN are property translated too.
6277 Non-printable characters and raw-byte characters are also
6278 translated to octal form. */
6279 if (((c < ' ' || c == 127) /* ASCII control chars */
6280 ? (it->area != TEXT_AREA
6281 /* In mode line, treat \n, \t like other crl chars. */
6282 || (c != '\t'
6283 && it->glyph_row
6284 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6285 || (c != '\n' && c != '\t'))
6286 : (nbsp_or_shy
6287 || CHAR_BYTE8_P (c)
6288 || ! CHAR_PRINTABLE_P (c))))
6290 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6291 or a non-printable character which must be displayed
6292 either as '\003' or as `^C' where the '\\' and '^'
6293 can be defined in the display table. Fill
6294 IT->ctl_chars with glyphs for what we have to
6295 display. Then, set IT->dpvec to these glyphs. */
6296 Lisp_Object gc;
6297 int ctl_len;
6298 int face_id;
6299 EMACS_INT lface_id = 0;
6300 int escape_glyph;
6302 /* Handle control characters with ^. */
6304 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6306 int g;
6308 g = '^'; /* default glyph for Control */
6309 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6310 if (it->dp
6311 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6312 && GLYPH_CODE_CHAR_VALID_P (gc))
6314 g = GLYPH_CODE_CHAR (gc);
6315 lface_id = GLYPH_CODE_FACE (gc);
6317 if (lface_id)
6319 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6321 else if (it->f == last_escape_glyph_frame
6322 && it->face_id == last_escape_glyph_face_id)
6324 face_id = last_escape_glyph_merged_face_id;
6326 else
6328 /* Merge the escape-glyph face into the current face. */
6329 face_id = merge_faces (it->f, Qescape_glyph, 0,
6330 it->face_id);
6331 last_escape_glyph_frame = it->f;
6332 last_escape_glyph_face_id = it->face_id;
6333 last_escape_glyph_merged_face_id = face_id;
6336 XSETINT (it->ctl_chars[0], g);
6337 XSETINT (it->ctl_chars[1], c ^ 0100);
6338 ctl_len = 2;
6339 goto display_control;
6342 /* Handle non-break space in the mode where it only gets
6343 highlighting. */
6345 if (EQ (Vnobreak_char_display, Qt)
6346 && nbsp_or_shy == char_is_nbsp)
6348 /* Merge the no-break-space face into the current face. */
6349 face_id = merge_faces (it->f, Qnobreak_space, 0,
6350 it->face_id);
6352 c = ' ';
6353 XSETINT (it->ctl_chars[0], ' ');
6354 ctl_len = 1;
6355 goto display_control;
6358 /* Handle sequences that start with the "escape glyph". */
6360 /* the default escape glyph is \. */
6361 escape_glyph = '\\';
6363 if (it->dp
6364 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6365 && GLYPH_CODE_CHAR_VALID_P (gc))
6367 escape_glyph = GLYPH_CODE_CHAR (gc);
6368 lface_id = GLYPH_CODE_FACE (gc);
6370 if (lface_id)
6372 /* The display table specified a face.
6373 Merge it into face_id and also into escape_glyph. */
6374 face_id = merge_faces (it->f, Qt, lface_id,
6375 it->face_id);
6377 else if (it->f == last_escape_glyph_frame
6378 && it->face_id == last_escape_glyph_face_id)
6380 face_id = last_escape_glyph_merged_face_id;
6382 else
6384 /* Merge the escape-glyph face into the current face. */
6385 face_id = merge_faces (it->f, Qescape_glyph, 0,
6386 it->face_id);
6387 last_escape_glyph_frame = it->f;
6388 last_escape_glyph_face_id = it->face_id;
6389 last_escape_glyph_merged_face_id = face_id;
6392 /* Handle soft hyphens in the mode where they only get
6393 highlighting. */
6395 if (EQ (Vnobreak_char_display, Qt)
6396 && nbsp_or_shy == char_is_soft_hyphen)
6398 XSETINT (it->ctl_chars[0], '-');
6399 ctl_len = 1;
6400 goto display_control;
6403 /* Handle non-break space and soft hyphen
6404 with the escape glyph. */
6406 if (nbsp_or_shy)
6408 XSETINT (it->ctl_chars[0], escape_glyph);
6409 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6410 XSETINT (it->ctl_chars[1], c);
6411 ctl_len = 2;
6412 goto display_control;
6416 char str[10];
6417 int len, i;
6419 if (CHAR_BYTE8_P (c))
6420 /* Display \200 instead of \17777600. */
6421 c = CHAR_TO_BYTE8 (c);
6422 len = sprintf (str, "%03o", c);
6424 XSETINT (it->ctl_chars[0], escape_glyph);
6425 for (i = 0; i < len; i++)
6426 XSETINT (it->ctl_chars[i + 1], str[i]);
6427 ctl_len = len + 1;
6430 display_control:
6431 /* Set up IT->dpvec and return first character from it. */
6432 it->dpvec_char_len = it->len;
6433 it->dpvec = it->ctl_chars;
6434 it->dpend = it->dpvec + ctl_len;
6435 it->current.dpvec_index = 0;
6436 it->dpvec_face_id = face_id;
6437 it->saved_face_id = it->face_id;
6438 it->method = GET_FROM_DISPLAY_VECTOR;
6439 it->ellipsis_p = 0;
6440 goto get_next;
6442 it->char_to_display = c;
6444 else if (success_p)
6446 it->char_to_display = it->c;
6450 /* Adjust face id for a multibyte character. There are no multibyte
6451 character in unibyte text. */
6452 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6453 && it->multibyte_p
6454 && success_p
6455 && FRAME_WINDOW_P (it->f))
6457 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6459 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6461 /* Automatic composition with glyph-string. */
6462 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6464 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6466 else
6468 EMACS_INT pos = (it->s ? -1
6469 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6470 : IT_CHARPOS (*it));
6471 int c;
6473 if (it->what == IT_CHARACTER)
6474 c = it->char_to_display;
6475 else
6477 struct composition *cmp = composition_table[it->cmp_it.id];
6478 int i;
6480 c = ' ';
6481 for (i = 0; i < cmp->glyph_len; i++)
6482 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6483 break;
6485 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6489 done:
6490 /* Is this character the last one of a run of characters with
6491 box? If yes, set IT->end_of_box_run_p to 1. */
6492 if (it->face_box_p
6493 && it->s == NULL)
6495 if (it->method == GET_FROM_STRING && it->sp)
6497 int face_id = underlying_face_id (it);
6498 struct face *face = FACE_FROM_ID (it->f, face_id);
6500 if (face)
6502 if (face->box == FACE_NO_BOX)
6504 /* If the box comes from face properties in a
6505 display string, check faces in that string. */
6506 int string_face_id = face_after_it_pos (it);
6507 it->end_of_box_run_p
6508 = (FACE_FROM_ID (it->f, string_face_id)->box
6509 == FACE_NO_BOX);
6511 /* Otherwise, the box comes from the underlying face.
6512 If this is the last string character displayed, check
6513 the next buffer location. */
6514 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6515 && (it->current.overlay_string_index
6516 == it->n_overlay_strings - 1))
6518 EMACS_INT ignore;
6519 int next_face_id;
6520 struct text_pos pos = it->current.pos;
6521 INC_TEXT_POS (pos, it->multibyte_p);
6523 next_face_id = face_at_buffer_position
6524 (it->w, CHARPOS (pos), it->region_beg_charpos,
6525 it->region_end_charpos, &ignore,
6526 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6527 -1);
6528 it->end_of_box_run_p
6529 = (FACE_FROM_ID (it->f, next_face_id)->box
6530 == FACE_NO_BOX);
6534 else
6536 int face_id = face_after_it_pos (it);
6537 it->end_of_box_run_p
6538 = (face_id != it->face_id
6539 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6543 /* Value is 0 if end of buffer or string reached. */
6544 return success_p;
6548 /* Move IT to the next display element.
6550 RESEAT_P non-zero means if called on a newline in buffer text,
6551 skip to the next visible line start.
6553 Functions get_next_display_element and set_iterator_to_next are
6554 separate because I find this arrangement easier to handle than a
6555 get_next_display_element function that also increments IT's
6556 position. The way it is we can first look at an iterator's current
6557 display element, decide whether it fits on a line, and if it does,
6558 increment the iterator position. The other way around we probably
6559 would either need a flag indicating whether the iterator has to be
6560 incremented the next time, or we would have to implement a
6561 decrement position function which would not be easy to write. */
6563 void
6564 set_iterator_to_next (struct it *it, int reseat_p)
6566 /* Reset flags indicating start and end of a sequence of characters
6567 with box. Reset them at the start of this function because
6568 moving the iterator to a new position might set them. */
6569 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6571 switch (it->method)
6573 case GET_FROM_BUFFER:
6574 /* The current display element of IT is a character from
6575 current_buffer. Advance in the buffer, and maybe skip over
6576 invisible lines that are so because of selective display. */
6577 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6578 reseat_at_next_visible_line_start (it, 0);
6579 else if (it->cmp_it.id >= 0)
6581 /* We are currently getting glyphs from a composition. */
6582 int i;
6584 if (! it->bidi_p)
6586 IT_CHARPOS (*it) += it->cmp_it.nchars;
6587 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6588 if (it->cmp_it.to < it->cmp_it.nglyphs)
6590 it->cmp_it.from = it->cmp_it.to;
6592 else
6594 it->cmp_it.id = -1;
6595 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6596 IT_BYTEPOS (*it),
6597 it->end_charpos, Qnil);
6600 else if (! it->cmp_it.reversed_p)
6602 /* Composition created while scanning forward. */
6603 /* Update IT's char/byte positions to point to the first
6604 character of the next grapheme cluster, or to the
6605 character visually after the current composition. */
6606 for (i = 0; i < it->cmp_it.nchars; i++)
6607 bidi_move_to_visually_next (&it->bidi_it);
6608 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6609 IT_CHARPOS (*it) = it->bidi_it.charpos;
6611 if (it->cmp_it.to < it->cmp_it.nglyphs)
6613 /* Proceed to the next grapheme cluster. */
6614 it->cmp_it.from = it->cmp_it.to;
6616 else
6618 /* No more grapheme clusters in this composition.
6619 Find the next stop position. */
6620 EMACS_INT stop = it->end_charpos;
6621 if (it->bidi_it.scan_dir < 0)
6622 /* Now we are scanning backward and don't know
6623 where to stop. */
6624 stop = -1;
6625 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6626 IT_BYTEPOS (*it), stop, Qnil);
6629 else
6631 /* Composition created while scanning backward. */
6632 /* Update IT's char/byte positions to point to the last
6633 character of the previous grapheme cluster, or the
6634 character visually after the current composition. */
6635 for (i = 0; i < it->cmp_it.nchars; i++)
6636 bidi_move_to_visually_next (&it->bidi_it);
6637 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6638 IT_CHARPOS (*it) = it->bidi_it.charpos;
6639 if (it->cmp_it.from > 0)
6641 /* Proceed to the previous grapheme cluster. */
6642 it->cmp_it.to = it->cmp_it.from;
6644 else
6646 /* No more grapheme clusters in this composition.
6647 Find the next stop position. */
6648 EMACS_INT stop = it->end_charpos;
6649 if (it->bidi_it.scan_dir < 0)
6650 /* Now we are scanning backward and don't know
6651 where to stop. */
6652 stop = -1;
6653 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6654 IT_BYTEPOS (*it), stop, Qnil);
6658 else
6660 xassert (it->len != 0);
6662 if (!it->bidi_p)
6664 IT_BYTEPOS (*it) += it->len;
6665 IT_CHARPOS (*it) += 1;
6667 else
6669 int prev_scan_dir = it->bidi_it.scan_dir;
6670 /* If this is a new paragraph, determine its base
6671 direction (a.k.a. its base embedding level). */
6672 if (it->bidi_it.new_paragraph)
6673 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6674 bidi_move_to_visually_next (&it->bidi_it);
6675 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6676 IT_CHARPOS (*it) = it->bidi_it.charpos;
6677 if (prev_scan_dir != it->bidi_it.scan_dir)
6679 /* As the scan direction was changed, we must
6680 re-compute the stop position for composition. */
6681 EMACS_INT stop = it->end_charpos;
6682 if (it->bidi_it.scan_dir < 0)
6683 stop = -1;
6684 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6685 IT_BYTEPOS (*it), stop, Qnil);
6688 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6690 break;
6692 case GET_FROM_C_STRING:
6693 /* Current display element of IT is from a C string. */
6694 if (!it->bidi_p
6695 /* If the string position is beyond string's end, it means
6696 next_element_from_c_string is padding the string with
6697 blanks, in which case we bypass the bidi iterator,
6698 because it cannot deal with such virtual characters. */
6699 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6701 IT_BYTEPOS (*it) += it->len;
6702 IT_CHARPOS (*it) += 1;
6704 else
6706 bidi_move_to_visually_next (&it->bidi_it);
6707 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6708 IT_CHARPOS (*it) = it->bidi_it.charpos;
6710 break;
6712 case GET_FROM_DISPLAY_VECTOR:
6713 /* Current display element of IT is from a display table entry.
6714 Advance in the display table definition. Reset it to null if
6715 end reached, and continue with characters from buffers/
6716 strings. */
6717 ++it->current.dpvec_index;
6719 /* Restore face of the iterator to what they were before the
6720 display vector entry (these entries may contain faces). */
6721 it->face_id = it->saved_face_id;
6723 if (it->dpvec + it->current.dpvec_index == it->dpend)
6725 int recheck_faces = it->ellipsis_p;
6727 if (it->s)
6728 it->method = GET_FROM_C_STRING;
6729 else if (STRINGP (it->string))
6730 it->method = GET_FROM_STRING;
6731 else
6733 it->method = GET_FROM_BUFFER;
6734 it->object = it->w->buffer;
6737 it->dpvec = NULL;
6738 it->current.dpvec_index = -1;
6740 /* Skip over characters which were displayed via IT->dpvec. */
6741 if (it->dpvec_char_len < 0)
6742 reseat_at_next_visible_line_start (it, 1);
6743 else if (it->dpvec_char_len > 0)
6745 if (it->method == GET_FROM_STRING
6746 && it->n_overlay_strings > 0)
6747 it->ignore_overlay_strings_at_pos_p = 1;
6748 it->len = it->dpvec_char_len;
6749 set_iterator_to_next (it, reseat_p);
6752 /* Maybe recheck faces after display vector */
6753 if (recheck_faces)
6754 it->stop_charpos = IT_CHARPOS (*it);
6756 break;
6758 case GET_FROM_STRING:
6759 /* Current display element is a character from a Lisp string. */
6760 xassert (it->s == NULL && STRINGP (it->string));
6761 if (it->cmp_it.id >= 0)
6763 int i;
6765 if (! it->bidi_p)
6767 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6768 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6769 if (it->cmp_it.to < it->cmp_it.nglyphs)
6770 it->cmp_it.from = it->cmp_it.to;
6771 else
6773 it->cmp_it.id = -1;
6774 composition_compute_stop_pos (&it->cmp_it,
6775 IT_STRING_CHARPOS (*it),
6776 IT_STRING_BYTEPOS (*it),
6777 it->end_charpos, it->string);
6780 else if (! it->cmp_it.reversed_p)
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;
6787 if (it->cmp_it.to < it->cmp_it.nglyphs)
6788 it->cmp_it.from = it->cmp_it.to;
6789 else
6791 EMACS_INT stop = it->end_charpos;
6792 if (it->bidi_it.scan_dir < 0)
6793 stop = -1;
6794 composition_compute_stop_pos (&it->cmp_it,
6795 IT_STRING_CHARPOS (*it),
6796 IT_STRING_BYTEPOS (*it), stop,
6797 it->string);
6800 else
6802 for (i = 0; i < it->cmp_it.nchars; i++)
6803 bidi_move_to_visually_next (&it->bidi_it);
6804 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6805 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6806 if (it->cmp_it.from > 0)
6807 it->cmp_it.to = it->cmp_it.from;
6808 else
6810 EMACS_INT stop = it->end_charpos;
6811 if (it->bidi_it.scan_dir < 0)
6812 stop = -1;
6813 composition_compute_stop_pos (&it->cmp_it,
6814 IT_STRING_CHARPOS (*it),
6815 IT_STRING_BYTEPOS (*it), stop,
6816 it->string);
6820 else
6822 if (!it->bidi_p
6823 /* If the string position is beyond string's end, it
6824 means next_element_from_string is padding the string
6825 with blanks, in which case we bypass the bidi
6826 iterator, because it cannot deal with such virtual
6827 characters. */
6828 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6830 IT_STRING_BYTEPOS (*it) += it->len;
6831 IT_STRING_CHARPOS (*it) += 1;
6833 else
6835 int prev_scan_dir = it->bidi_it.scan_dir;
6837 bidi_move_to_visually_next (&it->bidi_it);
6838 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6839 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6840 if (prev_scan_dir != it->bidi_it.scan_dir)
6842 EMACS_INT stop = it->end_charpos;
6844 if (it->bidi_it.scan_dir < 0)
6845 stop = -1;
6846 composition_compute_stop_pos (&it->cmp_it,
6847 IT_STRING_CHARPOS (*it),
6848 IT_STRING_BYTEPOS (*it), stop,
6849 it->string);
6854 consider_string_end:
6856 if (it->current.overlay_string_index >= 0)
6858 /* IT->string is an overlay string. Advance to the
6859 next, if there is one. */
6860 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6862 it->ellipsis_p = 0;
6863 next_overlay_string (it);
6864 if (it->ellipsis_p)
6865 setup_for_ellipsis (it, 0);
6868 else
6870 /* IT->string is not an overlay string. If we reached
6871 its end, and there is something on IT->stack, proceed
6872 with what is on the stack. This can be either another
6873 string, this time an overlay string, or a buffer. */
6874 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6875 && it->sp > 0)
6877 pop_it (it);
6878 if (it->method == GET_FROM_STRING)
6879 goto consider_string_end;
6882 break;
6884 case GET_FROM_IMAGE:
6885 case GET_FROM_STRETCH:
6886 #ifdef HAVE_XWIDGETS
6887 case GET_FROM_XWIDGET:
6889 /* The position etc with which we have to proceed are on
6890 the stack. The position may be at the end of a string,
6891 if the `display' property takes up the whole string. */
6892 xassert (it->sp > 0);
6893 pop_it (it);
6894 if (it->method == GET_FROM_STRING)
6895 goto consider_string_end;
6896 break;
6897 #endif
6898 default:
6899 /* There are no other methods defined, so this should be a bug. */
6900 abort ();
6903 xassert (it->method != GET_FROM_STRING
6904 || (STRINGP (it->string)
6905 && IT_STRING_CHARPOS (*it) >= 0));
6908 /* Load IT's display element fields with information about the next
6909 display element which comes from a display table entry or from the
6910 result of translating a control character to one of the forms `^C'
6911 or `\003'.
6913 IT->dpvec holds the glyphs to return as characters.
6914 IT->saved_face_id holds the face id before the display vector--it
6915 is restored into IT->face_id in set_iterator_to_next. */
6917 static int
6918 next_element_from_display_vector (struct it *it)
6920 Lisp_Object gc;
6922 /* Precondition. */
6923 xassert (it->dpvec && it->current.dpvec_index >= 0);
6925 it->face_id = it->saved_face_id;
6927 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6928 That seemed totally bogus - so I changed it... */
6929 gc = it->dpvec[it->current.dpvec_index];
6931 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6933 it->c = GLYPH_CODE_CHAR (gc);
6934 it->len = CHAR_BYTES (it->c);
6936 /* The entry may contain a face id to use. Such a face id is
6937 the id of a Lisp face, not a realized face. A face id of
6938 zero means no face is specified. */
6939 if (it->dpvec_face_id >= 0)
6940 it->face_id = it->dpvec_face_id;
6941 else
6943 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
6944 if (lface_id > 0)
6945 it->face_id = merge_faces (it->f, Qt, lface_id,
6946 it->saved_face_id);
6949 else
6950 /* Display table entry is invalid. Return a space. */
6951 it->c = ' ', it->len = 1;
6953 /* Don't change position and object of the iterator here. They are
6954 still the values of the character that had this display table
6955 entry or was translated, and that's what we want. */
6956 it->what = IT_CHARACTER;
6957 return 1;
6960 /* Get the first element of string/buffer in the visual order, after
6961 being reseated to a new position in a string or a buffer. */
6962 static void
6963 get_visually_first_element (struct it *it)
6965 int string_p = STRINGP (it->string) || it->s;
6966 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6967 EMACS_INT bob = (string_p ? 0 : BEGV);
6969 if (STRINGP (it->string))
6971 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6972 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6974 else
6976 it->bidi_it.charpos = IT_CHARPOS (*it);
6977 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6980 if (it->bidi_it.charpos == eob)
6982 /* Nothing to do, but reset the FIRST_ELT flag, like
6983 bidi_paragraph_init does, because we are not going to
6984 call it. */
6985 it->bidi_it.first_elt = 0;
6987 else if (it->bidi_it.charpos == bob
6988 || (!string_p
6989 /* FIXME: Should support all Unicode line separators. */
6990 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6991 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6993 /* If we are at the beginning of a line/string, we can produce
6994 the next element right away. */
6995 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6996 bidi_move_to_visually_next (&it->bidi_it);
6998 else
7000 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7002 /* We need to prime the bidi iterator starting at the line's or
7003 string's beginning, before we will be able to produce the
7004 next element. */
7005 if (string_p)
7006 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7007 else
7009 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7010 -1);
7011 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7013 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7016 /* Now return to buffer/string position where we were asked
7017 to get the next display element, and produce that. */
7018 bidi_move_to_visually_next (&it->bidi_it);
7020 while (it->bidi_it.bytepos != orig_bytepos
7021 && it->bidi_it.charpos < eob);
7024 /* Adjust IT's position information to where we ended up. */
7025 if (STRINGP (it->string))
7027 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7028 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7030 else
7032 IT_CHARPOS (*it) = it->bidi_it.charpos;
7033 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7036 if (STRINGP (it->string) || !it->s)
7038 EMACS_INT stop, charpos, bytepos;
7040 if (STRINGP (it->string))
7042 xassert (!it->s);
7043 stop = SCHARS (it->string);
7044 if (stop > it->end_charpos)
7045 stop = it->end_charpos;
7046 charpos = IT_STRING_CHARPOS (*it);
7047 bytepos = IT_STRING_BYTEPOS (*it);
7049 else
7051 stop = it->end_charpos;
7052 charpos = IT_CHARPOS (*it);
7053 bytepos = IT_BYTEPOS (*it);
7055 if (it->bidi_it.scan_dir < 0)
7056 stop = -1;
7057 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7058 it->string);
7062 /* Load IT with the next display element from Lisp string IT->string.
7063 IT->current.string_pos is the current position within the string.
7064 If IT->current.overlay_string_index >= 0, the Lisp string is an
7065 overlay string. */
7067 static int
7068 next_element_from_string (struct it *it)
7070 struct text_pos position;
7072 xassert (STRINGP (it->string));
7073 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7074 xassert (IT_STRING_CHARPOS (*it) >= 0);
7075 position = it->current.string_pos;
7077 /* With bidi reordering, the character to display might not be the
7078 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7079 that we were reseat()ed to a new string, whose paragraph
7080 direction is not known. */
7081 if (it->bidi_p && it->bidi_it.first_elt)
7083 get_visually_first_element (it);
7084 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7087 /* Time to check for invisible text? */
7088 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7090 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7092 if (!(!it->bidi_p
7093 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7094 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7096 /* With bidi non-linear iteration, we could find
7097 ourselves far beyond the last computed stop_charpos,
7098 with several other stop positions in between that we
7099 missed. Scan them all now, in buffer's logical
7100 order, until we find and handle the last stop_charpos
7101 that precedes our current position. */
7102 handle_stop_backwards (it, it->stop_charpos);
7103 return GET_NEXT_DISPLAY_ELEMENT (it);
7105 else
7107 if (it->bidi_p)
7109 /* Take note of the stop position we just moved
7110 across, for when we will move back across it. */
7111 it->prev_stop = it->stop_charpos;
7112 /* If we are at base paragraph embedding level, take
7113 note of the last stop position seen at this
7114 level. */
7115 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7116 it->base_level_stop = it->stop_charpos;
7118 handle_stop (it);
7120 /* Since a handler may have changed IT->method, we must
7121 recurse here. */
7122 return GET_NEXT_DISPLAY_ELEMENT (it);
7125 else if (it->bidi_p
7126 /* If we are before prev_stop, we may have overstepped
7127 on our way backwards a stop_pos, and if so, we need
7128 to handle that stop_pos. */
7129 && IT_STRING_CHARPOS (*it) < it->prev_stop
7130 /* We can sometimes back up for reasons that have nothing
7131 to do with bidi reordering. E.g., compositions. The
7132 code below is only needed when we are above the base
7133 embedding level, so test for that explicitly. */
7134 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7136 /* If we lost track of base_level_stop, we have no better
7137 place for handle_stop_backwards to start from than string
7138 beginning. This happens, e.g., when we were reseated to
7139 the previous screenful of text by vertical-motion. */
7140 if (it->base_level_stop <= 0
7141 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7142 it->base_level_stop = 0;
7143 handle_stop_backwards (it, it->base_level_stop);
7144 return GET_NEXT_DISPLAY_ELEMENT (it);
7148 if (it->current.overlay_string_index >= 0)
7150 /* Get the next character from an overlay string. In overlay
7151 strings, There is no field width or padding with spaces to
7152 do. */
7153 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7155 it->what = IT_EOB;
7156 return 0;
7158 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7159 IT_STRING_BYTEPOS (*it),
7160 it->bidi_it.scan_dir < 0
7161 ? -1
7162 : SCHARS (it->string))
7163 && next_element_from_composition (it))
7165 return 1;
7167 else if (STRING_MULTIBYTE (it->string))
7169 const unsigned char *s = (SDATA (it->string)
7170 + IT_STRING_BYTEPOS (*it));
7171 it->c = string_char_and_length (s, &it->len);
7173 else
7175 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7176 it->len = 1;
7179 else
7181 /* Get the next character from a Lisp string that is not an
7182 overlay string. Such strings come from the mode line, for
7183 example. We may have to pad with spaces, or truncate the
7184 string. See also next_element_from_c_string. */
7185 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7187 it->what = IT_EOB;
7188 return 0;
7190 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7192 /* Pad with spaces. */
7193 it->c = ' ', it->len = 1;
7194 CHARPOS (position) = BYTEPOS (position) = -1;
7196 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7197 IT_STRING_BYTEPOS (*it),
7198 it->bidi_it.scan_dir < 0
7199 ? -1
7200 : it->string_nchars)
7201 && next_element_from_composition (it))
7203 return 1;
7205 else if (STRING_MULTIBYTE (it->string))
7207 const unsigned char *s = (SDATA (it->string)
7208 + IT_STRING_BYTEPOS (*it));
7209 it->c = string_char_and_length (s, &it->len);
7211 else
7213 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7214 it->len = 1;
7218 /* Record what we have and where it came from. */
7219 it->what = IT_CHARACTER;
7220 it->object = it->string;
7221 it->position = position;
7222 return 1;
7226 /* Load IT with next display element from C string IT->s.
7227 IT->string_nchars is the maximum number of characters to return
7228 from the string. IT->end_charpos may be greater than
7229 IT->string_nchars when this function is called, in which case we
7230 may have to return padding spaces. Value is zero if end of string
7231 reached, including padding spaces. */
7233 static int
7234 next_element_from_c_string (struct it *it)
7236 int success_p = 1;
7238 xassert (it->s);
7239 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7240 it->what = IT_CHARACTER;
7241 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7242 it->object = Qnil;
7244 /* With bidi reordering, the character to display might not be the
7245 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7246 we were reseated to a new string, whose paragraph direction is
7247 not known. */
7248 if (it->bidi_p && it->bidi_it.first_elt)
7249 get_visually_first_element (it);
7251 /* IT's position can be greater than IT->string_nchars in case a
7252 field width or precision has been specified when the iterator was
7253 initialized. */
7254 if (IT_CHARPOS (*it) >= it->end_charpos)
7256 /* End of the game. */
7257 it->what = IT_EOB;
7258 success_p = 0;
7260 else if (IT_CHARPOS (*it) >= it->string_nchars)
7262 /* Pad with spaces. */
7263 it->c = ' ', it->len = 1;
7264 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7266 else if (it->multibyte_p)
7267 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7268 else
7269 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7271 return success_p;
7275 /* Set up IT to return characters from an ellipsis, if appropriate.
7276 The definition of the ellipsis glyphs may come from a display table
7277 entry. This function fills IT with the first glyph from the
7278 ellipsis if an ellipsis is to be displayed. */
7280 static int
7281 next_element_from_ellipsis (struct it *it)
7283 if (it->selective_display_ellipsis_p)
7284 setup_for_ellipsis (it, it->len);
7285 else
7287 /* The face at the current position may be different from the
7288 face we find after the invisible text. Remember what it
7289 was in IT->saved_face_id, and signal that it's there by
7290 setting face_before_selective_p. */
7291 it->saved_face_id = it->face_id;
7292 it->method = GET_FROM_BUFFER;
7293 it->object = it->w->buffer;
7294 reseat_at_next_visible_line_start (it, 1);
7295 it->face_before_selective_p = 1;
7298 return GET_NEXT_DISPLAY_ELEMENT (it);
7302 /* Deliver an image display element. The iterator IT is already
7303 filled with image information (done in handle_display_prop). Value
7304 is always 1. */
7307 static int
7308 next_element_from_image (struct it *it)
7310 it->what = IT_IMAGE;
7311 it->ignore_overlay_strings_at_pos_p = 0;
7312 return 1;
7315 #ifdef HAVE_XWIDGETS
7316 /* im not sure about this FIXME JAVE*/
7317 static int
7318 next_element_from_xwidget (struct it *it)
7320 it->what = IT_XWIDGET;
7321 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7322 //this is shaky because why do we set "what" if we dont set the other parts??
7323 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7324 return 1;
7326 #endif
7329 /* Fill iterator IT with next display element from a stretch glyph
7330 property. IT->object is the value of the text property. Value is
7331 always 1. */
7333 static int
7334 next_element_from_stretch (struct it *it)
7336 it->what = IT_STRETCH;
7337 return 1;
7340 /* Scan backwards from IT's current position until we find a stop
7341 position, or until BEGV. This is called when we find ourself
7342 before both the last known prev_stop and base_level_stop while
7343 reordering bidirectional text. */
7345 static void
7346 compute_stop_pos_backwards (struct it *it)
7348 const int SCAN_BACK_LIMIT = 1000;
7349 struct text_pos pos;
7350 struct display_pos save_current = it->current;
7351 struct text_pos save_position = it->position;
7352 EMACS_INT charpos = IT_CHARPOS (*it);
7353 EMACS_INT where_we_are = charpos;
7354 EMACS_INT save_stop_pos = it->stop_charpos;
7355 EMACS_INT save_end_pos = it->end_charpos;
7357 xassert (NILP (it->string) && !it->s);
7358 xassert (it->bidi_p);
7359 it->bidi_p = 0;
7362 it->end_charpos = min (charpos + 1, ZV);
7363 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7364 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7365 reseat_1 (it, pos, 0);
7366 compute_stop_pos (it);
7367 /* We must advance forward, right? */
7368 if (it->stop_charpos <= charpos)
7369 abort ();
7371 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7373 if (it->stop_charpos <= where_we_are)
7374 it->prev_stop = it->stop_charpos;
7375 else
7376 it->prev_stop = BEGV;
7377 it->bidi_p = 1;
7378 it->current = save_current;
7379 it->position = save_position;
7380 it->stop_charpos = save_stop_pos;
7381 it->end_charpos = save_end_pos;
7384 /* Scan forward from CHARPOS in the current buffer/string, until we
7385 find a stop position > current IT's position. Then handle the stop
7386 position before that. This is called when we bump into a stop
7387 position while reordering bidirectional text. CHARPOS should be
7388 the last previously processed stop_pos (or BEGV/0, if none were
7389 processed yet) whose position is less that IT's current
7390 position. */
7392 static void
7393 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7395 int bufp = !STRINGP (it->string);
7396 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7397 struct display_pos save_current = it->current;
7398 struct text_pos save_position = it->position;
7399 struct text_pos pos1;
7400 EMACS_INT next_stop;
7402 /* Scan in strict logical order. */
7403 xassert (it->bidi_p);
7404 it->bidi_p = 0;
7407 it->prev_stop = charpos;
7408 if (bufp)
7410 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7411 reseat_1 (it, pos1, 0);
7413 else
7414 it->current.string_pos = string_pos (charpos, it->string);
7415 compute_stop_pos (it);
7416 /* We must advance forward, right? */
7417 if (it->stop_charpos <= it->prev_stop)
7418 abort ();
7419 charpos = it->stop_charpos;
7421 while (charpos <= where_we_are);
7423 it->bidi_p = 1;
7424 it->current = save_current;
7425 it->position = save_position;
7426 next_stop = it->stop_charpos;
7427 it->stop_charpos = it->prev_stop;
7428 handle_stop (it);
7429 it->stop_charpos = next_stop;
7432 /* Load IT with the next display element from current_buffer. Value
7433 is zero if end of buffer reached. IT->stop_charpos is the next
7434 position at which to stop and check for text properties or buffer
7435 end. */
7437 static int
7438 next_element_from_buffer (struct it *it)
7440 int success_p = 1;
7442 xassert (IT_CHARPOS (*it) >= BEGV);
7443 xassert (NILP (it->string) && !it->s);
7444 xassert (!it->bidi_p
7445 || (EQ (it->bidi_it.string.lstring, Qnil)
7446 && it->bidi_it.string.s == NULL));
7448 /* With bidi reordering, the character to display might not be the
7449 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7450 we were reseat()ed to a new buffer position, which is potentially
7451 a different paragraph. */
7452 if (it->bidi_p && it->bidi_it.first_elt)
7454 get_visually_first_element (it);
7455 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7458 if (IT_CHARPOS (*it) >= it->stop_charpos)
7460 if (IT_CHARPOS (*it) >= it->end_charpos)
7462 int overlay_strings_follow_p;
7464 /* End of the game, except when overlay strings follow that
7465 haven't been returned yet. */
7466 if (it->overlay_strings_at_end_processed_p)
7467 overlay_strings_follow_p = 0;
7468 else
7470 it->overlay_strings_at_end_processed_p = 1;
7471 overlay_strings_follow_p = get_overlay_strings (it, 0);
7474 if (overlay_strings_follow_p)
7475 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7476 else
7478 it->what = IT_EOB;
7479 it->position = it->current.pos;
7480 success_p = 0;
7483 else if (!(!it->bidi_p
7484 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7485 || IT_CHARPOS (*it) == it->stop_charpos))
7487 /* With bidi non-linear iteration, we could find ourselves
7488 far beyond the last computed stop_charpos, with several
7489 other stop positions in between that we missed. Scan
7490 them all now, in buffer's logical order, until we find
7491 and handle the last stop_charpos that precedes our
7492 current position. */
7493 handle_stop_backwards (it, it->stop_charpos);
7494 return GET_NEXT_DISPLAY_ELEMENT (it);
7496 else
7498 if (it->bidi_p)
7500 /* Take note of the stop position we just moved across,
7501 for when we will move back across it. */
7502 it->prev_stop = it->stop_charpos;
7503 /* If we are at base paragraph embedding level, take
7504 note of the last stop position seen at this
7505 level. */
7506 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7507 it->base_level_stop = it->stop_charpos;
7509 handle_stop (it);
7510 return GET_NEXT_DISPLAY_ELEMENT (it);
7513 else if (it->bidi_p
7514 /* If we are before prev_stop, we may have overstepped on
7515 our way backwards a stop_pos, and if so, we need to
7516 handle that stop_pos. */
7517 && IT_CHARPOS (*it) < it->prev_stop
7518 /* We can sometimes back up for reasons that have nothing
7519 to do with bidi reordering. E.g., compositions. The
7520 code below is only needed when we are above the base
7521 embedding level, so test for that explicitly. */
7522 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7524 if (it->base_level_stop <= 0
7525 || IT_CHARPOS (*it) < it->base_level_stop)
7527 /* If we lost track of base_level_stop, we need to find
7528 prev_stop by looking backwards. This happens, e.g., when
7529 we were reseated to the previous screenful of text by
7530 vertical-motion. */
7531 it->base_level_stop = BEGV;
7532 compute_stop_pos_backwards (it);
7533 handle_stop_backwards (it, it->prev_stop);
7535 else
7536 handle_stop_backwards (it, it->base_level_stop);
7537 return GET_NEXT_DISPLAY_ELEMENT (it);
7539 else
7541 /* No face changes, overlays etc. in sight, so just return a
7542 character from current_buffer. */
7543 unsigned char *p;
7544 EMACS_INT stop;
7546 /* Maybe run the redisplay end trigger hook. Performance note:
7547 This doesn't seem to cost measurable time. */
7548 if (it->redisplay_end_trigger_charpos
7549 && it->glyph_row
7550 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7551 run_redisplay_end_trigger_hook (it);
7553 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7554 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7555 stop)
7556 && next_element_from_composition (it))
7558 return 1;
7561 /* Get the next character, maybe multibyte. */
7562 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7563 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7564 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7565 else
7566 it->c = *p, it->len = 1;
7568 /* Record what we have and where it came from. */
7569 it->what = IT_CHARACTER;
7570 it->object = it->w->buffer;
7571 it->position = it->current.pos;
7573 /* Normally we return the character found above, except when we
7574 really want to return an ellipsis for selective display. */
7575 if (it->selective)
7577 if (it->c == '\n')
7579 /* A value of selective > 0 means hide lines indented more
7580 than that number of columns. */
7581 if (it->selective > 0
7582 && IT_CHARPOS (*it) + 1 < ZV
7583 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7584 IT_BYTEPOS (*it) + 1,
7585 it->selective))
7587 success_p = next_element_from_ellipsis (it);
7588 it->dpvec_char_len = -1;
7591 else if (it->c == '\r' && it->selective == -1)
7593 /* A value of selective == -1 means that everything from the
7594 CR to the end of the line is invisible, with maybe an
7595 ellipsis displayed for it. */
7596 success_p = next_element_from_ellipsis (it);
7597 it->dpvec_char_len = -1;
7602 /* Value is zero if end of buffer reached. */
7603 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7604 return success_p;
7608 /* Run the redisplay end trigger hook for IT. */
7610 static void
7611 run_redisplay_end_trigger_hook (struct it *it)
7613 Lisp_Object args[3];
7615 /* IT->glyph_row should be non-null, i.e. we should be actually
7616 displaying something, or otherwise we should not run the hook. */
7617 xassert (it->glyph_row);
7619 /* Set up hook arguments. */
7620 args[0] = Qredisplay_end_trigger_functions;
7621 args[1] = it->window;
7622 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7623 it->redisplay_end_trigger_charpos = 0;
7625 /* Since we are *trying* to run these functions, don't try to run
7626 them again, even if they get an error. */
7627 it->w->redisplay_end_trigger = Qnil;
7628 Frun_hook_with_args (3, args);
7630 /* Notice if it changed the face of the character we are on. */
7631 handle_face_prop (it);
7635 /* Deliver a composition display element. Unlike the other
7636 next_element_from_XXX, this function is not registered in the array
7637 get_next_element[]. It is called from next_element_from_buffer and
7638 next_element_from_string when necessary. */
7640 static int
7641 next_element_from_composition (struct it *it)
7643 it->what = IT_COMPOSITION;
7644 it->len = it->cmp_it.nbytes;
7645 if (STRINGP (it->string))
7647 if (it->c < 0)
7649 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7650 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7651 return 0;
7653 it->position = it->current.string_pos;
7654 it->object = it->string;
7655 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7656 IT_STRING_BYTEPOS (*it), it->string);
7658 else
7660 if (it->c < 0)
7662 IT_CHARPOS (*it) += it->cmp_it.nchars;
7663 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7664 if (it->bidi_p)
7666 if (it->bidi_it.new_paragraph)
7667 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7668 /* Resync the bidi iterator with IT's new position.
7669 FIXME: this doesn't support bidirectional text. */
7670 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7671 bidi_move_to_visually_next (&it->bidi_it);
7673 return 0;
7675 it->position = it->current.pos;
7676 it->object = it->w->buffer;
7677 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7678 IT_BYTEPOS (*it), Qnil);
7680 return 1;
7685 /***********************************************************************
7686 Moving an iterator without producing glyphs
7687 ***********************************************************************/
7689 /* Check if iterator is at a position corresponding to a valid buffer
7690 position after some move_it_ call. */
7692 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7693 ((it)->method == GET_FROM_STRING \
7694 ? IT_STRING_CHARPOS (*it) == 0 \
7695 : 1)
7698 /* Move iterator IT to a specified buffer or X position within one
7699 line on the display without producing glyphs.
7701 OP should be a bit mask including some or all of these bits:
7702 MOVE_TO_X: Stop upon reaching x-position TO_X.
7703 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7704 Regardless of OP's value, stop upon reaching the end of the display line.
7706 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7707 This means, in particular, that TO_X includes window's horizontal
7708 scroll amount.
7710 The return value has several possible values that
7711 say what condition caused the scan to stop:
7713 MOVE_POS_MATCH_OR_ZV
7714 - when TO_POS or ZV was reached.
7716 MOVE_X_REACHED
7717 -when TO_X was reached before TO_POS or ZV were reached.
7719 MOVE_LINE_CONTINUED
7720 - when we reached the end of the display area and the line must
7721 be continued.
7723 MOVE_LINE_TRUNCATED
7724 - when we reached the end of the display area and the line is
7725 truncated.
7727 MOVE_NEWLINE_OR_CR
7728 - when we stopped at a line end, i.e. a newline or a CR and selective
7729 display is on. */
7731 static enum move_it_result
7732 move_it_in_display_line_to (struct it *it,
7733 EMACS_INT to_charpos, int to_x,
7734 enum move_operation_enum op)
7736 enum move_it_result result = MOVE_UNDEFINED;
7737 struct glyph_row *saved_glyph_row;
7738 struct it wrap_it, atpos_it, atx_it, ppos_it;
7739 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7740 void *ppos_data = NULL;
7741 int may_wrap = 0;
7742 enum it_method prev_method = it->method;
7743 EMACS_INT prev_pos = IT_CHARPOS (*it);
7744 int saw_smaller_pos = prev_pos < to_charpos;
7746 /* Don't produce glyphs in produce_glyphs. */
7747 saved_glyph_row = it->glyph_row;
7748 it->glyph_row = NULL;
7750 /* Use wrap_it to save a copy of IT wherever a word wrap could
7751 occur. Use atpos_it to save a copy of IT at the desired buffer
7752 position, if found, so that we can scan ahead and check if the
7753 word later overshoots the window edge. Use atx_it similarly, for
7754 pixel positions. */
7755 wrap_it.sp = -1;
7756 atpos_it.sp = -1;
7757 atx_it.sp = -1;
7759 /* Use ppos_it under bidi reordering to save a copy of IT for the
7760 position > CHARPOS that is the closest to CHARPOS. We restore
7761 that position in IT when we have scanned the entire display line
7762 without finding a match for CHARPOS and all the character
7763 positions are greater than CHARPOS. */
7764 if (it->bidi_p)
7766 SAVE_IT (ppos_it, *it, ppos_data);
7767 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7768 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7769 SAVE_IT (ppos_it, *it, ppos_data);
7772 #define BUFFER_POS_REACHED_P() \
7773 ((op & MOVE_TO_POS) != 0 \
7774 && BUFFERP (it->object) \
7775 && (IT_CHARPOS (*it) == to_charpos \
7776 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos) \
7777 || (it->what == IT_COMPOSITION \
7778 && ((IT_CHARPOS (*it) > to_charpos \
7779 && to_charpos >= it->cmp_it.charpos) \
7780 || (IT_CHARPOS (*it) < to_charpos \
7781 && to_charpos <= it->cmp_it.charpos)))) \
7782 && (it->method == GET_FROM_BUFFER \
7783 || (it->method == GET_FROM_DISPLAY_VECTOR \
7784 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7786 /* If there's a line-/wrap-prefix, handle it. */
7787 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7788 && it->current_y < it->last_visible_y)
7789 handle_line_prefix (it);
7791 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7792 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7794 while (1)
7796 int x, i, ascent = 0, descent = 0;
7798 /* Utility macro to reset an iterator with x, ascent, and descent. */
7799 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7800 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7801 (IT)->max_descent = descent)
7803 /* Stop if we move beyond TO_CHARPOS (after an image or a
7804 display string or stretch glyph). */
7805 if ((op & MOVE_TO_POS) != 0
7806 && BUFFERP (it->object)
7807 && it->method == GET_FROM_BUFFER
7808 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7809 || (it->bidi_p
7810 && (prev_method == GET_FROM_IMAGE
7811 || prev_method == GET_FROM_STRETCH
7812 || prev_method == GET_FROM_STRING)
7813 /* Passed TO_CHARPOS from left to right. */
7814 && ((prev_pos < to_charpos
7815 && IT_CHARPOS (*it) > to_charpos)
7816 /* Passed TO_CHARPOS from right to left. */
7817 || (prev_pos > to_charpos
7818 && IT_CHARPOS (*it) < to_charpos)))))
7820 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7822 result = MOVE_POS_MATCH_OR_ZV;
7823 break;
7825 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7826 /* If wrap_it is valid, the current position might be in a
7827 word that is wrapped. So, save the iterator in
7828 atpos_it and continue to see if wrapping happens. */
7829 SAVE_IT (atpos_it, *it, atpos_data);
7832 /* Stop when ZV reached.
7833 We used to stop here when TO_CHARPOS reached as well, but that is
7834 too soon if this glyph does not fit on this line. So we handle it
7835 explicitly below. */
7836 if (!get_next_display_element (it))
7838 result = MOVE_POS_MATCH_OR_ZV;
7839 break;
7842 if (it->line_wrap == TRUNCATE)
7844 if (BUFFER_POS_REACHED_P ())
7846 result = MOVE_POS_MATCH_OR_ZV;
7847 break;
7850 else
7852 if (it->line_wrap == WORD_WRAP)
7854 if (IT_DISPLAYING_WHITESPACE (it))
7855 may_wrap = 1;
7856 else if (may_wrap)
7858 /* We have reached a glyph that follows one or more
7859 whitespace characters. If the position is
7860 already found, we are done. */
7861 if (atpos_it.sp >= 0)
7863 RESTORE_IT (it, &atpos_it, atpos_data);
7864 result = MOVE_POS_MATCH_OR_ZV;
7865 goto done;
7867 if (atx_it.sp >= 0)
7869 RESTORE_IT (it, &atx_it, atx_data);
7870 result = MOVE_X_REACHED;
7871 goto done;
7873 /* Otherwise, we can wrap here. */
7874 SAVE_IT (wrap_it, *it, wrap_data);
7875 may_wrap = 0;
7880 /* Remember the line height for the current line, in case
7881 the next element doesn't fit on the line. */
7882 ascent = it->max_ascent;
7883 descent = it->max_descent;
7885 /* The call to produce_glyphs will get the metrics of the
7886 display element IT is loaded with. Record the x-position
7887 before this display element, in case it doesn't fit on the
7888 line. */
7889 x = it->current_x;
7891 PRODUCE_GLYPHS (it);
7893 if (it->area != TEXT_AREA)
7895 prev_method = it->method;
7896 if (it->method == GET_FROM_BUFFER)
7897 prev_pos = IT_CHARPOS (*it);
7898 set_iterator_to_next (it, 1);
7899 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7900 SET_TEXT_POS (this_line_min_pos,
7901 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7902 if (it->bidi_p
7903 && (op & MOVE_TO_POS)
7904 && IT_CHARPOS (*it) > to_charpos
7905 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
7906 SAVE_IT (ppos_it, *it, ppos_data);
7907 continue;
7910 /* The number of glyphs we get back in IT->nglyphs will normally
7911 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7912 character on a terminal frame, or (iii) a line end. For the
7913 second case, IT->nglyphs - 1 padding glyphs will be present.
7914 (On X frames, there is only one glyph produced for a
7915 composite character.)
7917 The behavior implemented below means, for continuation lines,
7918 that as many spaces of a TAB as fit on the current line are
7919 displayed there. For terminal frames, as many glyphs of a
7920 multi-glyph character are displayed in the current line, too.
7921 This is what the old redisplay code did, and we keep it that
7922 way. Under X, the whole shape of a complex character must
7923 fit on the line or it will be completely displayed in the
7924 next line.
7926 Note that both for tabs and padding glyphs, all glyphs have
7927 the same width. */
7928 if (it->nglyphs)
7930 /* More than one glyph or glyph doesn't fit on line. All
7931 glyphs have the same width. */
7932 int single_glyph_width = it->pixel_width / it->nglyphs;
7933 int new_x;
7934 int x_before_this_char = x;
7935 int hpos_before_this_char = it->hpos;
7937 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7939 new_x = x + single_glyph_width;
7941 /* We want to leave anything reaching TO_X to the caller. */
7942 if ((op & MOVE_TO_X) && new_x > to_x)
7944 if (BUFFER_POS_REACHED_P ())
7946 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7947 goto buffer_pos_reached;
7948 if (atpos_it.sp < 0)
7950 SAVE_IT (atpos_it, *it, atpos_data);
7951 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7954 else
7956 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7958 it->current_x = x;
7959 result = MOVE_X_REACHED;
7960 break;
7962 if (atx_it.sp < 0)
7964 SAVE_IT (atx_it, *it, atx_data);
7965 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7970 if (/* Lines are continued. */
7971 it->line_wrap != TRUNCATE
7972 && (/* And glyph doesn't fit on the line. */
7973 new_x > it->last_visible_x
7974 /* Or it fits exactly and we're on a window
7975 system frame. */
7976 || (new_x == it->last_visible_x
7977 && FRAME_WINDOW_P (it->f))))
7979 if (/* IT->hpos == 0 means the very first glyph
7980 doesn't fit on the line, e.g. a wide image. */
7981 it->hpos == 0
7982 || (new_x == it->last_visible_x
7983 && FRAME_WINDOW_P (it->f)))
7985 ++it->hpos;
7986 it->current_x = new_x;
7988 /* The character's last glyph just barely fits
7989 in this row. */
7990 if (i == it->nglyphs - 1)
7992 /* If this is the destination position,
7993 return a position *before* it in this row,
7994 now that we know it fits in this row. */
7995 if (BUFFER_POS_REACHED_P ())
7997 if (it->line_wrap != WORD_WRAP
7998 || wrap_it.sp < 0)
8000 it->hpos = hpos_before_this_char;
8001 it->current_x = x_before_this_char;
8002 result = MOVE_POS_MATCH_OR_ZV;
8003 break;
8005 if (it->line_wrap == WORD_WRAP
8006 && atpos_it.sp < 0)
8008 SAVE_IT (atpos_it, *it, atpos_data);
8009 atpos_it.current_x = x_before_this_char;
8010 atpos_it.hpos = hpos_before_this_char;
8014 prev_method = it->method;
8015 if (it->method == GET_FROM_BUFFER)
8016 prev_pos = IT_CHARPOS (*it);
8017 set_iterator_to_next (it, 1);
8018 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8019 SET_TEXT_POS (this_line_min_pos,
8020 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8021 /* On graphical terminals, newlines may
8022 "overflow" into the fringe if
8023 overflow-newline-into-fringe is non-nil.
8024 On text-only terminals, newlines may
8025 overflow into the last glyph on the
8026 display line.*/
8027 if (!FRAME_WINDOW_P (it->f)
8028 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8030 if (!get_next_display_element (it))
8032 result = MOVE_POS_MATCH_OR_ZV;
8033 break;
8035 if (BUFFER_POS_REACHED_P ())
8037 if (ITERATOR_AT_END_OF_LINE_P (it))
8038 result = MOVE_POS_MATCH_OR_ZV;
8039 else
8040 result = MOVE_LINE_CONTINUED;
8041 break;
8043 if (ITERATOR_AT_END_OF_LINE_P (it))
8045 result = MOVE_NEWLINE_OR_CR;
8046 break;
8051 else
8052 IT_RESET_X_ASCENT_DESCENT (it);
8054 if (wrap_it.sp >= 0)
8056 RESTORE_IT (it, &wrap_it, wrap_data);
8057 atpos_it.sp = -1;
8058 atx_it.sp = -1;
8061 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8062 IT_CHARPOS (*it)));
8063 result = MOVE_LINE_CONTINUED;
8064 break;
8067 if (BUFFER_POS_REACHED_P ())
8069 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8070 goto buffer_pos_reached;
8071 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8073 SAVE_IT (atpos_it, *it, atpos_data);
8074 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8078 if (new_x > it->first_visible_x)
8080 /* Glyph is visible. Increment number of glyphs that
8081 would be displayed. */
8082 ++it->hpos;
8086 if (result != MOVE_UNDEFINED)
8087 break;
8089 else if (BUFFER_POS_REACHED_P ())
8091 buffer_pos_reached:
8092 IT_RESET_X_ASCENT_DESCENT (it);
8093 result = MOVE_POS_MATCH_OR_ZV;
8094 break;
8096 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8098 /* Stop when TO_X specified and reached. This check is
8099 necessary here because of lines consisting of a line end,
8100 only. The line end will not produce any glyphs and we
8101 would never get MOVE_X_REACHED. */
8102 xassert (it->nglyphs == 0);
8103 result = MOVE_X_REACHED;
8104 break;
8107 /* Is this a line end? If yes, we're done. */
8108 if (ITERATOR_AT_END_OF_LINE_P (it))
8110 /* If we are past TO_CHARPOS, but never saw any character
8111 positions smaller than TO_CHARPOS, return
8112 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8113 did. */
8114 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8116 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8118 if (IT_CHARPOS (ppos_it) < ZV)
8120 RESTORE_IT (it, &ppos_it, ppos_data);
8121 result = MOVE_POS_MATCH_OR_ZV;
8123 else
8124 goto buffer_pos_reached;
8126 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8127 && IT_CHARPOS (*it) > to_charpos)
8128 goto buffer_pos_reached;
8129 else
8130 result = MOVE_NEWLINE_OR_CR;
8132 else
8133 result = MOVE_NEWLINE_OR_CR;
8134 break;
8137 prev_method = it->method;
8138 if (it->method == GET_FROM_BUFFER)
8139 prev_pos = IT_CHARPOS (*it);
8140 /* The current display element has been consumed. Advance
8141 to the next. */
8142 set_iterator_to_next (it, 1);
8143 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8144 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8145 if (IT_CHARPOS (*it) < to_charpos)
8146 saw_smaller_pos = 1;
8147 if (it->bidi_p
8148 && (op & MOVE_TO_POS)
8149 && IT_CHARPOS (*it) >= to_charpos
8150 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8151 SAVE_IT (ppos_it, *it, ppos_data);
8153 /* Stop if lines are truncated and IT's current x-position is
8154 past the right edge of the window now. */
8155 if (it->line_wrap == TRUNCATE
8156 && it->current_x >= it->last_visible_x)
8158 if (!FRAME_WINDOW_P (it->f)
8159 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8161 int at_eob_p = 0;
8163 if ((at_eob_p = !get_next_display_element (it))
8164 || BUFFER_POS_REACHED_P ()
8165 /* If we are past TO_CHARPOS, but never saw any
8166 character positions smaller than TO_CHARPOS,
8167 return MOVE_POS_MATCH_OR_ZV, like the
8168 unidirectional display did. */
8169 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8170 && !saw_smaller_pos
8171 && IT_CHARPOS (*it) > to_charpos))
8173 if (!at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8174 RESTORE_IT (it, &ppos_it, ppos_data);
8175 result = MOVE_POS_MATCH_OR_ZV;
8176 break;
8178 if (ITERATOR_AT_END_OF_LINE_P (it))
8180 result = MOVE_NEWLINE_OR_CR;
8181 break;
8184 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8185 && !saw_smaller_pos
8186 && IT_CHARPOS (*it) > to_charpos)
8188 if (IT_CHARPOS (ppos_it) < ZV)
8189 RESTORE_IT (it, &ppos_it, ppos_data);
8190 result = MOVE_POS_MATCH_OR_ZV;
8191 break;
8193 result = MOVE_LINE_TRUNCATED;
8194 break;
8196 #undef IT_RESET_X_ASCENT_DESCENT
8199 #undef BUFFER_POS_REACHED_P
8201 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8202 restore the saved iterator. */
8203 if (atpos_it.sp >= 0)
8204 RESTORE_IT (it, &atpos_it, atpos_data);
8205 else if (atx_it.sp >= 0)
8206 RESTORE_IT (it, &atx_it, atx_data);
8208 done:
8210 if (atpos_data)
8211 bidi_unshelve_cache (atpos_data, 1);
8212 if (atx_data)
8213 bidi_unshelve_cache (atx_data, 1);
8214 if (wrap_data)
8215 bidi_unshelve_cache (wrap_data, 1);
8216 if (ppos_data)
8217 bidi_unshelve_cache (ppos_data, 1);
8219 /* Restore the iterator settings altered at the beginning of this
8220 function. */
8221 it->glyph_row = saved_glyph_row;
8222 return result;
8225 /* For external use. */
8226 void
8227 move_it_in_display_line (struct it *it,
8228 EMACS_INT to_charpos, int to_x,
8229 enum move_operation_enum op)
8231 if (it->line_wrap == WORD_WRAP
8232 && (op & MOVE_TO_X))
8234 struct it save_it;
8235 void *save_data = NULL;
8236 int skip;
8238 SAVE_IT (save_it, *it, save_data);
8239 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8240 /* When word-wrap is on, TO_X may lie past the end
8241 of a wrapped line. Then it->current is the
8242 character on the next line, so backtrack to the
8243 space before the wrap point. */
8244 if (skip == MOVE_LINE_CONTINUED)
8246 int prev_x = max (it->current_x - 1, 0);
8247 RESTORE_IT (it, &save_it, save_data);
8248 move_it_in_display_line_to
8249 (it, -1, prev_x, MOVE_TO_X);
8251 else
8252 bidi_unshelve_cache (save_data, 1);
8254 else
8255 move_it_in_display_line_to (it, to_charpos, to_x, op);
8259 /* Move IT forward until it satisfies one or more of the criteria in
8260 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8262 OP is a bit-mask that specifies where to stop, and in particular,
8263 which of those four position arguments makes a difference. See the
8264 description of enum move_operation_enum.
8266 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8267 screen line, this function will set IT to the next position that is
8268 displayed to the right of TO_CHARPOS on the screen. */
8270 void
8271 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8273 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8274 int line_height, line_start_x = 0, reached = 0;
8275 void *backup_data = NULL;
8277 for (;;)
8279 if (op & MOVE_TO_VPOS)
8281 /* If no TO_CHARPOS and no TO_X specified, stop at the
8282 start of the line TO_VPOS. */
8283 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8285 if (it->vpos == to_vpos)
8287 reached = 1;
8288 break;
8290 else
8291 skip = move_it_in_display_line_to (it, -1, -1, 0);
8293 else
8295 /* TO_VPOS >= 0 means stop at TO_X in the line at
8296 TO_VPOS, or at TO_POS, whichever comes first. */
8297 if (it->vpos == to_vpos)
8299 reached = 2;
8300 break;
8303 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8305 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8307 reached = 3;
8308 break;
8310 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8312 /* We have reached TO_X but not in the line we want. */
8313 skip = move_it_in_display_line_to (it, to_charpos,
8314 -1, MOVE_TO_POS);
8315 if (skip == MOVE_POS_MATCH_OR_ZV)
8317 reached = 4;
8318 break;
8323 else if (op & MOVE_TO_Y)
8325 struct it it_backup;
8327 if (it->line_wrap == WORD_WRAP)
8328 SAVE_IT (it_backup, *it, backup_data);
8330 /* TO_Y specified means stop at TO_X in the line containing
8331 TO_Y---or at TO_CHARPOS if this is reached first. The
8332 problem is that we can't really tell whether the line
8333 contains TO_Y before we have completely scanned it, and
8334 this may skip past TO_X. What we do is to first scan to
8335 TO_X.
8337 If TO_X is not specified, use a TO_X of zero. The reason
8338 is to make the outcome of this function more predictable.
8339 If we didn't use TO_X == 0, we would stop at the end of
8340 the line which is probably not what a caller would expect
8341 to happen. */
8342 skip = move_it_in_display_line_to
8343 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8344 (MOVE_TO_X | (op & MOVE_TO_POS)));
8346 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8347 if (skip == MOVE_POS_MATCH_OR_ZV)
8348 reached = 5;
8349 else if (skip == MOVE_X_REACHED)
8351 /* If TO_X was reached, we want to know whether TO_Y is
8352 in the line. We know this is the case if the already
8353 scanned glyphs make the line tall enough. Otherwise,
8354 we must check by scanning the rest of the line. */
8355 line_height = it->max_ascent + it->max_descent;
8356 if (to_y >= it->current_y
8357 && to_y < it->current_y + line_height)
8359 reached = 6;
8360 break;
8362 SAVE_IT (it_backup, *it, backup_data);
8363 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8364 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8365 op & MOVE_TO_POS);
8366 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8367 line_height = it->max_ascent + it->max_descent;
8368 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8370 if (to_y >= it->current_y
8371 && to_y < it->current_y + line_height)
8373 /* If TO_Y is in this line and TO_X was reached
8374 above, we scanned too far. We have to restore
8375 IT's settings to the ones before skipping. */
8376 RESTORE_IT (it, &it_backup, backup_data);
8377 reached = 6;
8379 else
8381 skip = skip2;
8382 if (skip == MOVE_POS_MATCH_OR_ZV)
8383 reached = 7;
8386 else
8388 /* Check whether TO_Y is in this line. */
8389 line_height = it->max_ascent + it->max_descent;
8390 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8392 if (to_y >= it->current_y
8393 && to_y < it->current_y + line_height)
8395 /* When word-wrap is on, TO_X may lie past the end
8396 of a wrapped line. Then it->current is the
8397 character on the next line, so backtrack to the
8398 space before the wrap point. */
8399 if (skip == MOVE_LINE_CONTINUED
8400 && it->line_wrap == WORD_WRAP)
8402 int prev_x = max (it->current_x - 1, 0);
8403 RESTORE_IT (it, &it_backup, backup_data);
8404 skip = move_it_in_display_line_to
8405 (it, -1, prev_x, MOVE_TO_X);
8407 reached = 6;
8411 if (reached)
8412 break;
8414 else if (BUFFERP (it->object)
8415 && (it->method == GET_FROM_BUFFER
8416 || it->method == GET_FROM_STRETCH)
8417 && IT_CHARPOS (*it) >= to_charpos
8418 /* Under bidi iteration, a call to set_iterator_to_next
8419 can scan far beyond to_charpos if the initial
8420 portion of the next line needs to be reordered. In
8421 that case, give move_it_in_display_line_to another
8422 chance below. */
8423 && !(it->bidi_p
8424 && it->bidi_it.scan_dir == -1))
8425 skip = MOVE_POS_MATCH_OR_ZV;
8426 else
8427 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8429 switch (skip)
8431 case MOVE_POS_MATCH_OR_ZV:
8432 reached = 8;
8433 goto out;
8435 case MOVE_NEWLINE_OR_CR:
8436 set_iterator_to_next (it, 1);
8437 it->continuation_lines_width = 0;
8438 break;
8440 case MOVE_LINE_TRUNCATED:
8441 it->continuation_lines_width = 0;
8442 reseat_at_next_visible_line_start (it, 0);
8443 if ((op & MOVE_TO_POS) != 0
8444 && IT_CHARPOS (*it) > to_charpos)
8446 reached = 9;
8447 goto out;
8449 break;
8451 case MOVE_LINE_CONTINUED:
8452 /* For continued lines ending in a tab, some of the glyphs
8453 associated with the tab are displayed on the current
8454 line. Since it->current_x does not include these glyphs,
8455 we use it->last_visible_x instead. */
8456 if (it->c == '\t')
8458 it->continuation_lines_width += it->last_visible_x;
8459 /* When moving by vpos, ensure that the iterator really
8460 advances to the next line (bug#847, bug#969). Fixme:
8461 do we need to do this in other circumstances? */
8462 if (it->current_x != it->last_visible_x
8463 && (op & MOVE_TO_VPOS)
8464 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8466 line_start_x = it->current_x + it->pixel_width
8467 - it->last_visible_x;
8468 set_iterator_to_next (it, 0);
8471 else
8472 it->continuation_lines_width += it->current_x;
8473 break;
8475 default:
8476 abort ();
8479 /* Reset/increment for the next run. */
8480 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8481 it->current_x = line_start_x;
8482 line_start_x = 0;
8483 it->hpos = 0;
8484 it->current_y += it->max_ascent + it->max_descent;
8485 ++it->vpos;
8486 last_height = it->max_ascent + it->max_descent;
8487 last_max_ascent = it->max_ascent;
8488 it->max_ascent = it->max_descent = 0;
8491 out:
8493 /* On text terminals, we may stop at the end of a line in the middle
8494 of a multi-character glyph. If the glyph itself is continued,
8495 i.e. it is actually displayed on the next line, don't treat this
8496 stopping point as valid; move to the next line instead (unless
8497 that brings us offscreen). */
8498 if (!FRAME_WINDOW_P (it->f)
8499 && op & MOVE_TO_POS
8500 && IT_CHARPOS (*it) == to_charpos
8501 && it->what == IT_CHARACTER
8502 && it->nglyphs > 1
8503 && it->line_wrap == WINDOW_WRAP
8504 && it->current_x == it->last_visible_x - 1
8505 && it->c != '\n'
8506 && it->c != '\t'
8507 && it->vpos < XFASTINT (it->w->window_end_vpos))
8509 it->continuation_lines_width += it->current_x;
8510 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8511 it->current_y += it->max_ascent + it->max_descent;
8512 ++it->vpos;
8513 last_height = it->max_ascent + it->max_descent;
8514 last_max_ascent = it->max_ascent;
8517 if (backup_data)
8518 bidi_unshelve_cache (backup_data, 1);
8520 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8524 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8526 If DY > 0, move IT backward at least that many pixels. DY = 0
8527 means move IT backward to the preceding line start or BEGV. This
8528 function may move over more than DY pixels if IT->current_y - DY
8529 ends up in the middle of a line; in this case IT->current_y will be
8530 set to the top of the line moved to. */
8532 void
8533 move_it_vertically_backward (struct it *it, int dy)
8535 int nlines, h;
8536 struct it it2, it3;
8537 void *it2data = NULL, *it3data = NULL;
8538 EMACS_INT start_pos;
8540 move_further_back:
8541 xassert (dy >= 0);
8543 start_pos = IT_CHARPOS (*it);
8545 /* Estimate how many newlines we must move back. */
8546 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8548 /* Set the iterator's position that many lines back. */
8549 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8550 back_to_previous_visible_line_start (it);
8552 /* Reseat the iterator here. When moving backward, we don't want
8553 reseat to skip forward over invisible text, set up the iterator
8554 to deliver from overlay strings at the new position etc. So,
8555 use reseat_1 here. */
8556 reseat_1 (it, it->current.pos, 1);
8558 /* We are now surely at a line start. */
8559 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8560 reordering is in effect. */
8561 it->continuation_lines_width = 0;
8563 /* Move forward and see what y-distance we moved. First move to the
8564 start of the next line so that we get its height. We need this
8565 height to be able to tell whether we reached the specified
8566 y-distance. */
8567 SAVE_IT (it2, *it, it2data);
8568 it2.max_ascent = it2.max_descent = 0;
8571 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8572 MOVE_TO_POS | MOVE_TO_VPOS);
8574 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8575 xassert (IT_CHARPOS (*it) >= BEGV);
8576 SAVE_IT (it3, it2, it3data);
8578 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8579 xassert (IT_CHARPOS (*it) >= BEGV);
8580 /* H is the actual vertical distance from the position in *IT
8581 and the starting position. */
8582 h = it2.current_y - it->current_y;
8583 /* NLINES is the distance in number of lines. */
8584 nlines = it2.vpos - it->vpos;
8586 /* Correct IT's y and vpos position
8587 so that they are relative to the starting point. */
8588 it->vpos -= nlines;
8589 it->current_y -= h;
8591 if (dy == 0)
8593 /* DY == 0 means move to the start of the screen line. The
8594 value of nlines is > 0 if continuation lines were involved,
8595 or if the original IT position was at start of a line. */
8596 RESTORE_IT (it, it, it2data);
8597 if (nlines > 0)
8598 move_it_by_lines (it, nlines);
8599 /* The above code moves us to some position NLINES down,
8600 usually to its first glyph (leftmost in an L2R line), but
8601 that's not necessarily the start of the line, under bidi
8602 reordering. We want to get to the character position
8603 that is immediately after the newline of the previous
8604 line. */
8605 if (it->bidi_p && IT_CHARPOS (*it) > BEGV
8606 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8608 EMACS_INT nl_pos =
8609 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8611 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8613 bidi_unshelve_cache (it3data, 1);
8615 else
8617 /* The y-position we try to reach, relative to *IT.
8618 Note that H has been subtracted in front of the if-statement. */
8619 int target_y = it->current_y + h - dy;
8620 int y0 = it3.current_y;
8621 int y1;
8622 int line_height;
8624 RESTORE_IT (&it3, &it3, it3data);
8625 y1 = line_bottom_y (&it3);
8626 line_height = y1 - y0;
8627 RESTORE_IT (it, it, it2data);
8628 /* If we did not reach target_y, try to move further backward if
8629 we can. If we moved too far backward, try to move forward. */
8630 if (target_y < it->current_y
8631 /* This is heuristic. In a window that's 3 lines high, with
8632 a line height of 13 pixels each, recentering with point
8633 on the bottom line will try to move -39/2 = 19 pixels
8634 backward. Try to avoid moving into the first line. */
8635 && (it->current_y - target_y
8636 > min (window_box_height (it->w), line_height * 2 / 3))
8637 && IT_CHARPOS (*it) > BEGV)
8639 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8640 target_y - it->current_y));
8641 dy = it->current_y - target_y;
8642 goto move_further_back;
8644 else if (target_y >= it->current_y + line_height
8645 && IT_CHARPOS (*it) < ZV)
8647 /* Should move forward by at least one line, maybe more.
8649 Note: Calling move_it_by_lines can be expensive on
8650 terminal frames, where compute_motion is used (via
8651 vmotion) to do the job, when there are very long lines
8652 and truncate-lines is nil. That's the reason for
8653 treating terminal frames specially here. */
8655 if (!FRAME_WINDOW_P (it->f))
8656 move_it_vertically (it, target_y - (it->current_y + line_height));
8657 else
8661 move_it_by_lines (it, 1);
8663 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8670 /* Move IT by a specified amount of pixel lines DY. DY negative means
8671 move backwards. DY = 0 means move to start of screen line. At the
8672 end, IT will be on the start of a screen line. */
8674 void
8675 move_it_vertically (struct it *it, int dy)
8677 if (dy <= 0)
8678 move_it_vertically_backward (it, -dy);
8679 else
8681 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8682 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8683 MOVE_TO_POS | MOVE_TO_Y);
8684 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8686 /* If buffer ends in ZV without a newline, move to the start of
8687 the line to satisfy the post-condition. */
8688 if (IT_CHARPOS (*it) == ZV
8689 && ZV > BEGV
8690 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8691 move_it_by_lines (it, 0);
8696 /* Move iterator IT past the end of the text line it is in. */
8698 void
8699 move_it_past_eol (struct it *it)
8701 enum move_it_result rc;
8703 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8704 if (rc == MOVE_NEWLINE_OR_CR)
8705 set_iterator_to_next (it, 0);
8709 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8710 negative means move up. DVPOS == 0 means move to the start of the
8711 screen line.
8713 Optimization idea: If we would know that IT->f doesn't use
8714 a face with proportional font, we could be faster for
8715 truncate-lines nil. */
8717 void
8718 move_it_by_lines (struct it *it, int dvpos)
8721 /* The commented-out optimization uses vmotion on terminals. This
8722 gives bad results, because elements like it->what, on which
8723 callers such as pos_visible_p rely, aren't updated. */
8724 /* struct position pos;
8725 if (!FRAME_WINDOW_P (it->f))
8727 struct text_pos textpos;
8729 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8730 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8731 reseat (it, textpos, 1);
8732 it->vpos += pos.vpos;
8733 it->current_y += pos.vpos;
8735 else */
8737 if (dvpos == 0)
8739 /* DVPOS == 0 means move to the start of the screen line. */
8740 move_it_vertically_backward (it, 0);
8741 xassert (it->current_x == 0 && it->hpos == 0);
8742 /* Let next call to line_bottom_y calculate real line height */
8743 last_height = 0;
8745 else if (dvpos > 0)
8747 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8748 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8749 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8751 else
8753 struct it it2;
8754 void *it2data = NULL;
8755 EMACS_INT start_charpos, i;
8757 /* Start at the beginning of the screen line containing IT's
8758 position. This may actually move vertically backwards,
8759 in case of overlays, so adjust dvpos accordingly. */
8760 dvpos += it->vpos;
8761 move_it_vertically_backward (it, 0);
8762 dvpos -= it->vpos;
8764 /* Go back -DVPOS visible lines and reseat the iterator there. */
8765 start_charpos = IT_CHARPOS (*it);
8766 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8767 back_to_previous_visible_line_start (it);
8768 reseat (it, it->current.pos, 1);
8770 /* Move further back if we end up in a string or an image. */
8771 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8773 /* First try to move to start of display line. */
8774 dvpos += it->vpos;
8775 move_it_vertically_backward (it, 0);
8776 dvpos -= it->vpos;
8777 if (IT_POS_VALID_AFTER_MOVE_P (it))
8778 break;
8779 /* If start of line is still in string or image,
8780 move further back. */
8781 back_to_previous_visible_line_start (it);
8782 reseat (it, it->current.pos, 1);
8783 dvpos--;
8786 it->current_x = it->hpos = 0;
8788 /* Above call may have moved too far if continuation lines
8789 are involved. Scan forward and see if it did. */
8790 SAVE_IT (it2, *it, it2data);
8791 it2.vpos = it2.current_y = 0;
8792 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8793 it->vpos -= it2.vpos;
8794 it->current_y -= it2.current_y;
8795 it->current_x = it->hpos = 0;
8797 /* If we moved too far back, move IT some lines forward. */
8798 if (it2.vpos > -dvpos)
8800 int delta = it2.vpos + dvpos;
8802 RESTORE_IT (&it2, &it2, it2data);
8803 SAVE_IT (it2, *it, it2data);
8804 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8805 /* Move back again if we got too far ahead. */
8806 if (IT_CHARPOS (*it) >= start_charpos)
8807 RESTORE_IT (it, &it2, it2data);
8808 else
8809 bidi_unshelve_cache (it2data, 1);
8811 else
8812 RESTORE_IT (it, it, it2data);
8816 /* Return 1 if IT points into the middle of a display vector. */
8819 in_display_vector_p (struct it *it)
8821 return (it->method == GET_FROM_DISPLAY_VECTOR
8822 && it->current.dpvec_index > 0
8823 && it->dpvec + it->current.dpvec_index != it->dpend);
8827 /***********************************************************************
8828 Messages
8829 ***********************************************************************/
8832 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8833 to *Messages*. */
8835 void
8836 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8838 Lisp_Object args[3];
8839 Lisp_Object msg, fmt;
8840 char *buffer;
8841 EMACS_INT len;
8842 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8843 USE_SAFE_ALLOCA;
8845 /* Do nothing if called asynchronously. Inserting text into
8846 a buffer may call after-change-functions and alike and
8847 that would means running Lisp asynchronously. */
8848 if (handling_signal)
8849 return;
8851 fmt = msg = Qnil;
8852 GCPRO4 (fmt, msg, arg1, arg2);
8854 args[0] = fmt = build_string (format);
8855 args[1] = arg1;
8856 args[2] = arg2;
8857 msg = Fformat (3, args);
8859 len = SBYTES (msg) + 1;
8860 SAFE_ALLOCA (buffer, char *, len);
8861 memcpy (buffer, SDATA (msg), len);
8863 message_dolog (buffer, len - 1, 1, 0);
8864 SAFE_FREE ();
8866 UNGCPRO;
8870 /* Output a newline in the *Messages* buffer if "needs" one. */
8872 void
8873 message_log_maybe_newline (void)
8875 if (message_log_need_newline)
8876 message_dolog ("", 0, 1, 0);
8880 /* Add a string M of length NBYTES to the message log, optionally
8881 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8882 nonzero, means interpret the contents of M as multibyte. This
8883 function calls low-level routines in order to bypass text property
8884 hooks, etc. which might not be safe to run.
8886 This may GC (insert may run before/after change hooks),
8887 so the buffer M must NOT point to a Lisp string. */
8889 void
8890 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8892 const unsigned char *msg = (const unsigned char *) m;
8894 if (!NILP (Vmemory_full))
8895 return;
8897 if (!NILP (Vmessage_log_max))
8899 struct buffer *oldbuf;
8900 Lisp_Object oldpoint, oldbegv, oldzv;
8901 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8902 EMACS_INT point_at_end = 0;
8903 EMACS_INT zv_at_end = 0;
8904 Lisp_Object old_deactivate_mark, tem;
8905 struct gcpro gcpro1;
8907 old_deactivate_mark = Vdeactivate_mark;
8908 oldbuf = current_buffer;
8909 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8910 BVAR (current_buffer, undo_list) = Qt;
8912 oldpoint = message_dolog_marker1;
8913 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8914 oldbegv = message_dolog_marker2;
8915 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8916 oldzv = message_dolog_marker3;
8917 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8918 GCPRO1 (old_deactivate_mark);
8920 if (PT == Z)
8921 point_at_end = 1;
8922 if (ZV == Z)
8923 zv_at_end = 1;
8925 BEGV = BEG;
8926 BEGV_BYTE = BEG_BYTE;
8927 ZV = Z;
8928 ZV_BYTE = Z_BYTE;
8929 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8931 /* Insert the string--maybe converting multibyte to single byte
8932 or vice versa, so that all the text fits the buffer. */
8933 if (multibyte
8934 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8936 EMACS_INT i;
8937 int c, char_bytes;
8938 char work[1];
8940 /* Convert a multibyte string to single-byte
8941 for the *Message* buffer. */
8942 for (i = 0; i < nbytes; i += char_bytes)
8944 c = string_char_and_length (msg + i, &char_bytes);
8945 work[0] = (ASCII_CHAR_P (c)
8947 : multibyte_char_to_unibyte (c));
8948 insert_1_both (work, 1, 1, 1, 0, 0);
8951 else if (! multibyte
8952 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8954 EMACS_INT i;
8955 int c, char_bytes;
8956 unsigned char str[MAX_MULTIBYTE_LENGTH];
8957 /* Convert a single-byte string to multibyte
8958 for the *Message* buffer. */
8959 for (i = 0; i < nbytes; i++)
8961 c = msg[i];
8962 MAKE_CHAR_MULTIBYTE (c);
8963 char_bytes = CHAR_STRING (c, str);
8964 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8967 else if (nbytes)
8968 insert_1 (m, nbytes, 1, 0, 0);
8970 if (nlflag)
8972 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8973 printmax_t dups;
8974 insert_1 ("\n", 1, 1, 0, 0);
8976 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8977 this_bol = PT;
8978 this_bol_byte = PT_BYTE;
8980 /* See if this line duplicates the previous one.
8981 If so, combine duplicates. */
8982 if (this_bol > BEG)
8984 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8985 prev_bol = PT;
8986 prev_bol_byte = PT_BYTE;
8988 dups = message_log_check_duplicate (prev_bol_byte,
8989 this_bol_byte);
8990 if (dups)
8992 del_range_both (prev_bol, prev_bol_byte,
8993 this_bol, this_bol_byte, 0);
8994 if (dups > 1)
8996 char dupstr[sizeof " [ times]"
8997 + INT_STRLEN_BOUND (printmax_t)];
8998 int duplen;
9000 /* If you change this format, don't forget to also
9001 change message_log_check_duplicate. */
9002 sprintf (dupstr, " [%"pMd" times]", dups);
9003 duplen = strlen (dupstr);
9004 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9005 insert_1 (dupstr, duplen, 1, 0, 1);
9010 /* If we have more than the desired maximum number of lines
9011 in the *Messages* buffer now, delete the oldest ones.
9012 This is safe because we don't have undo in this buffer. */
9014 if (NATNUMP (Vmessage_log_max))
9016 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9017 -XFASTINT (Vmessage_log_max) - 1, 0);
9018 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9021 BEGV = XMARKER (oldbegv)->charpos;
9022 BEGV_BYTE = marker_byte_position (oldbegv);
9024 if (zv_at_end)
9026 ZV = Z;
9027 ZV_BYTE = Z_BYTE;
9029 else
9031 ZV = XMARKER (oldzv)->charpos;
9032 ZV_BYTE = marker_byte_position (oldzv);
9035 if (point_at_end)
9036 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9037 else
9038 /* We can't do Fgoto_char (oldpoint) because it will run some
9039 Lisp code. */
9040 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9041 XMARKER (oldpoint)->bytepos);
9043 UNGCPRO;
9044 unchain_marker (XMARKER (oldpoint));
9045 unchain_marker (XMARKER (oldbegv));
9046 unchain_marker (XMARKER (oldzv));
9048 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9049 set_buffer_internal (oldbuf);
9050 if (NILP (tem))
9051 windows_or_buffers_changed = old_windows_or_buffers_changed;
9052 message_log_need_newline = !nlflag;
9053 Vdeactivate_mark = old_deactivate_mark;
9058 /* We are at the end of the buffer after just having inserted a newline.
9059 (Note: We depend on the fact we won't be crossing the gap.)
9060 Check to see if the most recent message looks a lot like the previous one.
9061 Return 0 if different, 1 if the new one should just replace it, or a
9062 value N > 1 if we should also append " [N times]". */
9064 static intmax_t
9065 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9067 EMACS_INT i;
9068 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9069 int seen_dots = 0;
9070 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9071 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9073 for (i = 0; i < len; i++)
9075 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9076 seen_dots = 1;
9077 if (p1[i] != p2[i])
9078 return seen_dots;
9080 p1 += len;
9081 if (*p1 == '\n')
9082 return 2;
9083 if (*p1++ == ' ' && *p1++ == '[')
9085 char *pend;
9086 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9087 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9088 return n+1;
9090 return 0;
9094 /* Display an echo area message M with a specified length of NBYTES
9095 bytes. The string may include null characters. If M is 0, clear
9096 out any existing message, and let the mini-buffer text show
9097 through.
9099 This may GC, so the buffer M must NOT point to a Lisp string. */
9101 void
9102 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9104 /* First flush out any partial line written with print. */
9105 message_log_maybe_newline ();
9106 if (m)
9107 message_dolog (m, nbytes, 1, multibyte);
9108 message2_nolog (m, nbytes, multibyte);
9112 /* The non-logging counterpart of message2. */
9114 void
9115 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9117 struct frame *sf = SELECTED_FRAME ();
9118 message_enable_multibyte = multibyte;
9120 if (FRAME_INITIAL_P (sf))
9122 if (noninteractive_need_newline)
9123 putc ('\n', stderr);
9124 noninteractive_need_newline = 0;
9125 if (m)
9126 fwrite (m, nbytes, 1, stderr);
9127 if (cursor_in_echo_area == 0)
9128 fprintf (stderr, "\n");
9129 fflush (stderr);
9131 /* A null message buffer means that the frame hasn't really been
9132 initialized yet. Error messages get reported properly by
9133 cmd_error, so this must be just an informative message; toss it. */
9134 else if (INTERACTIVE
9135 && sf->glyphs_initialized_p
9136 && FRAME_MESSAGE_BUF (sf))
9138 Lisp_Object mini_window;
9139 struct frame *f;
9141 /* Get the frame containing the mini-buffer
9142 that the selected frame is using. */
9143 mini_window = FRAME_MINIBUF_WINDOW (sf);
9144 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9146 FRAME_SAMPLE_VISIBILITY (f);
9147 if (FRAME_VISIBLE_P (sf)
9148 && ! FRAME_VISIBLE_P (f))
9149 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9151 if (m)
9153 set_message (m, Qnil, nbytes, multibyte);
9154 if (minibuffer_auto_raise)
9155 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9157 else
9158 clear_message (1, 1);
9160 do_pending_window_change (0);
9161 echo_area_display (1);
9162 do_pending_window_change (0);
9163 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9164 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9169 /* Display an echo area message M with a specified length of NBYTES
9170 bytes. The string may include null characters. If M is not a
9171 string, clear out any existing message, and let the mini-buffer
9172 text show through.
9174 This function cancels echoing. */
9176 void
9177 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9179 struct gcpro gcpro1;
9181 GCPRO1 (m);
9182 clear_message (1,1);
9183 cancel_echoing ();
9185 /* First flush out any partial line written with print. */
9186 message_log_maybe_newline ();
9187 if (STRINGP (m))
9189 char *buffer;
9190 USE_SAFE_ALLOCA;
9192 SAFE_ALLOCA (buffer, char *, nbytes);
9193 memcpy (buffer, SDATA (m), nbytes);
9194 message_dolog (buffer, nbytes, 1, multibyte);
9195 SAFE_FREE ();
9197 message3_nolog (m, nbytes, multibyte);
9199 UNGCPRO;
9203 /* The non-logging version of message3.
9204 This does not cancel echoing, because it is used for echoing.
9205 Perhaps we need to make a separate function for echoing
9206 and make this cancel echoing. */
9208 void
9209 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9211 struct frame *sf = SELECTED_FRAME ();
9212 message_enable_multibyte = multibyte;
9214 if (FRAME_INITIAL_P (sf))
9216 if (noninteractive_need_newline)
9217 putc ('\n', stderr);
9218 noninteractive_need_newline = 0;
9219 if (STRINGP (m))
9220 fwrite (SDATA (m), nbytes, 1, stderr);
9221 if (cursor_in_echo_area == 0)
9222 fprintf (stderr, "\n");
9223 fflush (stderr);
9225 /* A null message buffer means that the frame hasn't really been
9226 initialized yet. Error messages get reported properly by
9227 cmd_error, so this must be just an informative message; toss it. */
9228 else if (INTERACTIVE
9229 && sf->glyphs_initialized_p
9230 && FRAME_MESSAGE_BUF (sf))
9232 Lisp_Object mini_window;
9233 Lisp_Object frame;
9234 struct frame *f;
9236 /* Get the frame containing the mini-buffer
9237 that the selected frame is using. */
9238 mini_window = FRAME_MINIBUF_WINDOW (sf);
9239 frame = XWINDOW (mini_window)->frame;
9240 f = XFRAME (frame);
9242 FRAME_SAMPLE_VISIBILITY (f);
9243 if (FRAME_VISIBLE_P (sf)
9244 && !FRAME_VISIBLE_P (f))
9245 Fmake_frame_visible (frame);
9247 if (STRINGP (m) && SCHARS (m) > 0)
9249 set_message (NULL, m, nbytes, multibyte);
9250 if (minibuffer_auto_raise)
9251 Fraise_frame (frame);
9252 /* Assume we are not echoing.
9253 (If we are, echo_now will override this.) */
9254 echo_message_buffer = Qnil;
9256 else
9257 clear_message (1, 1);
9259 do_pending_window_change (0);
9260 echo_area_display (1);
9261 do_pending_window_change (0);
9262 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9263 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9268 /* Display a null-terminated echo area message M. If M is 0, clear
9269 out any existing message, and let the mini-buffer text show through.
9271 The buffer M must continue to exist until after the echo area gets
9272 cleared or some other message gets displayed there. Do not pass
9273 text that is stored in a Lisp string. Do not pass text in a buffer
9274 that was alloca'd. */
9276 void
9277 message1 (const char *m)
9279 message2 (m, (m ? strlen (m) : 0), 0);
9283 /* The non-logging counterpart of message1. */
9285 void
9286 message1_nolog (const char *m)
9288 message2_nolog (m, (m ? strlen (m) : 0), 0);
9291 /* Display a message M which contains a single %s
9292 which gets replaced with STRING. */
9294 void
9295 message_with_string (const char *m, Lisp_Object string, int log)
9297 CHECK_STRING (string);
9299 if (noninteractive)
9301 if (m)
9303 if (noninteractive_need_newline)
9304 putc ('\n', stderr);
9305 noninteractive_need_newline = 0;
9306 fprintf (stderr, m, SDATA (string));
9307 if (!cursor_in_echo_area)
9308 fprintf (stderr, "\n");
9309 fflush (stderr);
9312 else if (INTERACTIVE)
9314 /* The frame whose minibuffer we're going to display the message on.
9315 It may be larger than the selected frame, so we need
9316 to use its buffer, not the selected frame's buffer. */
9317 Lisp_Object mini_window;
9318 struct frame *f, *sf = SELECTED_FRAME ();
9320 /* Get the frame containing the minibuffer
9321 that the selected frame is using. */
9322 mini_window = FRAME_MINIBUF_WINDOW (sf);
9323 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9325 /* A null message buffer means that the frame hasn't really been
9326 initialized yet. Error messages get reported properly by
9327 cmd_error, so this must be just an informative message; toss it. */
9328 if (FRAME_MESSAGE_BUF (f))
9330 Lisp_Object args[2], msg;
9331 struct gcpro gcpro1, gcpro2;
9333 args[0] = build_string (m);
9334 args[1] = msg = string;
9335 GCPRO2 (args[0], msg);
9336 gcpro1.nvars = 2;
9338 msg = Fformat (2, args);
9340 if (log)
9341 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9342 else
9343 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9345 UNGCPRO;
9347 /* Print should start at the beginning of the message
9348 buffer next time. */
9349 message_buf_print = 0;
9355 /* Dump an informative message to the minibuf. If M is 0, clear out
9356 any existing message, and let the mini-buffer text show through. */
9358 static void
9359 vmessage (const char *m, va_list ap)
9361 if (noninteractive)
9363 if (m)
9365 if (noninteractive_need_newline)
9366 putc ('\n', stderr);
9367 noninteractive_need_newline = 0;
9368 vfprintf (stderr, m, ap);
9369 if (cursor_in_echo_area == 0)
9370 fprintf (stderr, "\n");
9371 fflush (stderr);
9374 else if (INTERACTIVE)
9376 /* The frame whose mini-buffer we're going to display the message
9377 on. It may be larger than the selected frame, so we need to
9378 use its buffer, not the selected frame's buffer. */
9379 Lisp_Object mini_window;
9380 struct frame *f, *sf = SELECTED_FRAME ();
9382 /* Get the frame containing the mini-buffer
9383 that the selected frame is using. */
9384 mini_window = FRAME_MINIBUF_WINDOW (sf);
9385 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9387 /* A null message buffer means that the frame hasn't really been
9388 initialized yet. Error messages get reported properly by
9389 cmd_error, so this must be just an informative message; toss
9390 it. */
9391 if (FRAME_MESSAGE_BUF (f))
9393 if (m)
9395 ptrdiff_t len;
9397 len = doprnt (FRAME_MESSAGE_BUF (f),
9398 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9400 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9402 else
9403 message1 (0);
9405 /* Print should start at the beginning of the message
9406 buffer next time. */
9407 message_buf_print = 0;
9412 void
9413 message (const char *m, ...)
9415 va_list ap;
9416 va_start (ap, m);
9417 vmessage (m, ap);
9418 va_end (ap);
9422 #if 0
9423 /* The non-logging version of message. */
9425 void
9426 message_nolog (const char *m, ...)
9428 Lisp_Object old_log_max;
9429 va_list ap;
9430 va_start (ap, m);
9431 old_log_max = Vmessage_log_max;
9432 Vmessage_log_max = Qnil;
9433 vmessage (m, ap);
9434 Vmessage_log_max = old_log_max;
9435 va_end (ap);
9437 #endif
9440 /* Display the current message in the current mini-buffer. This is
9441 only called from error handlers in process.c, and is not time
9442 critical. */
9444 void
9445 update_echo_area (void)
9447 if (!NILP (echo_area_buffer[0]))
9449 Lisp_Object string;
9450 string = Fcurrent_message ();
9451 message3 (string, SBYTES (string),
9452 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9457 /* Make sure echo area buffers in `echo_buffers' are live.
9458 If they aren't, make new ones. */
9460 static void
9461 ensure_echo_area_buffers (void)
9463 int i;
9465 for (i = 0; i < 2; ++i)
9466 if (!BUFFERP (echo_buffer[i])
9467 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9469 char name[30];
9470 Lisp_Object old_buffer;
9471 int j;
9473 old_buffer = echo_buffer[i];
9474 sprintf (name, " *Echo Area %d*", i);
9475 echo_buffer[i] = Fget_buffer_create (build_string (name));
9476 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9477 /* to force word wrap in echo area -
9478 it was decided to postpone this*/
9479 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9481 for (j = 0; j < 2; ++j)
9482 if (EQ (old_buffer, echo_area_buffer[j]))
9483 echo_area_buffer[j] = echo_buffer[i];
9488 /* Call FN with args A1..A4 with either the current or last displayed
9489 echo_area_buffer as current buffer.
9491 WHICH zero means use the current message buffer
9492 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9493 from echo_buffer[] and clear it.
9495 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9496 suitable buffer from echo_buffer[] and clear it.
9498 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9499 that the current message becomes the last displayed one, make
9500 choose a suitable buffer for echo_area_buffer[0], and clear it.
9502 Value is what FN returns. */
9504 static int
9505 with_echo_area_buffer (struct window *w, int which,
9506 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9507 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9509 Lisp_Object buffer;
9510 int this_one, the_other, clear_buffer_p, rc;
9511 int count = SPECPDL_INDEX ();
9513 /* If buffers aren't live, make new ones. */
9514 ensure_echo_area_buffers ();
9516 clear_buffer_p = 0;
9518 if (which == 0)
9519 this_one = 0, the_other = 1;
9520 else if (which > 0)
9521 this_one = 1, the_other = 0;
9522 else
9524 this_one = 0, the_other = 1;
9525 clear_buffer_p = 1;
9527 /* We need a fresh one in case the current echo buffer equals
9528 the one containing the last displayed echo area message. */
9529 if (!NILP (echo_area_buffer[this_one])
9530 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9531 echo_area_buffer[this_one] = Qnil;
9534 /* Choose a suitable buffer from echo_buffer[] is we don't
9535 have one. */
9536 if (NILP (echo_area_buffer[this_one]))
9538 echo_area_buffer[this_one]
9539 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9540 ? echo_buffer[the_other]
9541 : echo_buffer[this_one]);
9542 clear_buffer_p = 1;
9545 buffer = echo_area_buffer[this_one];
9547 /* Don't get confused by reusing the buffer used for echoing
9548 for a different purpose. */
9549 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9550 cancel_echoing ();
9552 record_unwind_protect (unwind_with_echo_area_buffer,
9553 with_echo_area_buffer_unwind_data (w));
9555 /* Make the echo area buffer current. Note that for display
9556 purposes, it is not necessary that the displayed window's buffer
9557 == current_buffer, except for text property lookup. So, let's
9558 only set that buffer temporarily here without doing a full
9559 Fset_window_buffer. We must also change w->pointm, though,
9560 because otherwise an assertions in unshow_buffer fails, and Emacs
9561 aborts. */
9562 set_buffer_internal_1 (XBUFFER (buffer));
9563 if (w)
9565 w->buffer = buffer;
9566 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9569 BVAR (current_buffer, undo_list) = Qt;
9570 BVAR (current_buffer, read_only) = Qnil;
9571 specbind (Qinhibit_read_only, Qt);
9572 specbind (Qinhibit_modification_hooks, Qt);
9574 if (clear_buffer_p && Z > BEG)
9575 del_range (BEG, Z);
9577 xassert (BEGV >= BEG);
9578 xassert (ZV <= Z && ZV >= BEGV);
9580 rc = fn (a1, a2, a3, a4);
9582 xassert (BEGV >= BEG);
9583 xassert (ZV <= Z && ZV >= BEGV);
9585 unbind_to (count, Qnil);
9586 return rc;
9590 /* Save state that should be preserved around the call to the function
9591 FN called in with_echo_area_buffer. */
9593 static Lisp_Object
9594 with_echo_area_buffer_unwind_data (struct window *w)
9596 int i = 0;
9597 Lisp_Object vector, tmp;
9599 /* Reduce consing by keeping one vector in
9600 Vwith_echo_area_save_vector. */
9601 vector = Vwith_echo_area_save_vector;
9602 Vwith_echo_area_save_vector = Qnil;
9604 if (NILP (vector))
9605 vector = Fmake_vector (make_number (7), Qnil);
9607 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9608 ASET (vector, i, Vdeactivate_mark); ++i;
9609 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9611 if (w)
9613 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9614 ASET (vector, i, w->buffer); ++i;
9615 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9616 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9618 else
9620 int end = i + 4;
9621 for (; i < end; ++i)
9622 ASET (vector, i, Qnil);
9625 xassert (i == ASIZE (vector));
9626 return vector;
9630 /* Restore global state from VECTOR which was created by
9631 with_echo_area_buffer_unwind_data. */
9633 static Lisp_Object
9634 unwind_with_echo_area_buffer (Lisp_Object vector)
9636 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9637 Vdeactivate_mark = AREF (vector, 1);
9638 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9640 if (WINDOWP (AREF (vector, 3)))
9642 struct window *w;
9643 Lisp_Object buffer, charpos, bytepos;
9645 w = XWINDOW (AREF (vector, 3));
9646 buffer = AREF (vector, 4);
9647 charpos = AREF (vector, 5);
9648 bytepos = AREF (vector, 6);
9650 w->buffer = buffer;
9651 set_marker_both (w->pointm, buffer,
9652 XFASTINT (charpos), XFASTINT (bytepos));
9655 Vwith_echo_area_save_vector = vector;
9656 return Qnil;
9660 /* Set up the echo area for use by print functions. MULTIBYTE_P
9661 non-zero means we will print multibyte. */
9663 void
9664 setup_echo_area_for_printing (int multibyte_p)
9666 /* If we can't find an echo area any more, exit. */
9667 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9668 Fkill_emacs (Qnil);
9670 ensure_echo_area_buffers ();
9672 if (!message_buf_print)
9674 /* A message has been output since the last time we printed.
9675 Choose a fresh echo area buffer. */
9676 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9677 echo_area_buffer[0] = echo_buffer[1];
9678 else
9679 echo_area_buffer[0] = echo_buffer[0];
9681 /* Switch to that buffer and clear it. */
9682 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9683 BVAR (current_buffer, truncate_lines) = Qnil;
9685 if (Z > BEG)
9687 int count = SPECPDL_INDEX ();
9688 specbind (Qinhibit_read_only, Qt);
9689 /* Note that undo recording is always disabled. */
9690 del_range (BEG, Z);
9691 unbind_to (count, Qnil);
9693 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9695 /* Set up the buffer for the multibyteness we need. */
9696 if (multibyte_p
9697 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9698 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9700 /* Raise the frame containing the echo area. */
9701 if (minibuffer_auto_raise)
9703 struct frame *sf = SELECTED_FRAME ();
9704 Lisp_Object mini_window;
9705 mini_window = FRAME_MINIBUF_WINDOW (sf);
9706 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9709 message_log_maybe_newline ();
9710 message_buf_print = 1;
9712 else
9714 if (NILP (echo_area_buffer[0]))
9716 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9717 echo_area_buffer[0] = echo_buffer[1];
9718 else
9719 echo_area_buffer[0] = echo_buffer[0];
9722 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9724 /* Someone switched buffers between print requests. */
9725 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9726 BVAR (current_buffer, truncate_lines) = Qnil;
9732 /* Display an echo area message in window W. Value is non-zero if W's
9733 height is changed. If display_last_displayed_message_p is
9734 non-zero, display the message that was last displayed, otherwise
9735 display the current message. */
9737 static int
9738 display_echo_area (struct window *w)
9740 int i, no_message_p, window_height_changed_p, count;
9742 /* Temporarily disable garbage collections while displaying the echo
9743 area. This is done because a GC can print a message itself.
9744 That message would modify the echo area buffer's contents while a
9745 redisplay of the buffer is going on, and seriously confuse
9746 redisplay. */
9747 count = inhibit_garbage_collection ();
9749 /* If there is no message, we must call display_echo_area_1
9750 nevertheless because it resizes the window. But we will have to
9751 reset the echo_area_buffer in question to nil at the end because
9752 with_echo_area_buffer will sets it to an empty buffer. */
9753 i = display_last_displayed_message_p ? 1 : 0;
9754 no_message_p = NILP (echo_area_buffer[i]);
9756 window_height_changed_p
9757 = with_echo_area_buffer (w, display_last_displayed_message_p,
9758 display_echo_area_1,
9759 (intptr_t) w, Qnil, 0, 0);
9761 if (no_message_p)
9762 echo_area_buffer[i] = Qnil;
9764 unbind_to (count, Qnil);
9765 return window_height_changed_p;
9769 /* Helper for display_echo_area. Display the current buffer which
9770 contains the current echo area message in window W, a mini-window,
9771 a pointer to which is passed in A1. A2..A4 are currently not used.
9772 Change the height of W so that all of the message is displayed.
9773 Value is non-zero if height of W was changed. */
9775 static int
9776 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9778 intptr_t i1 = a1;
9779 struct window *w = (struct window *) i1;
9780 Lisp_Object window;
9781 struct text_pos start;
9782 int window_height_changed_p = 0;
9784 /* Do this before displaying, so that we have a large enough glyph
9785 matrix for the display. If we can't get enough space for the
9786 whole text, display the last N lines. That works by setting w->start. */
9787 window_height_changed_p = resize_mini_window (w, 0);
9789 /* Use the starting position chosen by resize_mini_window. */
9790 SET_TEXT_POS_FROM_MARKER (start, w->start);
9792 /* Display. */
9793 clear_glyph_matrix (w->desired_matrix);
9794 XSETWINDOW (window, w);
9795 try_window (window, start, 0);
9797 return window_height_changed_p;
9801 /* Resize the echo area window to exactly the size needed for the
9802 currently displayed message, if there is one. If a mini-buffer
9803 is active, don't shrink it. */
9805 void
9806 resize_echo_area_exactly (void)
9808 if (BUFFERP (echo_area_buffer[0])
9809 && WINDOWP (echo_area_window))
9811 struct window *w = XWINDOW (echo_area_window);
9812 int resized_p;
9813 Lisp_Object resize_exactly;
9815 if (minibuf_level == 0)
9816 resize_exactly = Qt;
9817 else
9818 resize_exactly = Qnil;
9820 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9821 (intptr_t) w, resize_exactly,
9822 0, 0);
9823 if (resized_p)
9825 ++windows_or_buffers_changed;
9826 ++update_mode_lines;
9827 redisplay_internal ();
9833 /* Callback function for with_echo_area_buffer, when used from
9834 resize_echo_area_exactly. A1 contains a pointer to the window to
9835 resize, EXACTLY non-nil means resize the mini-window exactly to the
9836 size of the text displayed. A3 and A4 are not used. Value is what
9837 resize_mini_window returns. */
9839 static int
9840 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9842 intptr_t i1 = a1;
9843 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9847 /* Resize mini-window W to fit the size of its contents. EXACT_P
9848 means size the window exactly to the size needed. Otherwise, it's
9849 only enlarged until W's buffer is empty.
9851 Set W->start to the right place to begin display. If the whole
9852 contents fit, start at the beginning. Otherwise, start so as
9853 to make the end of the contents appear. This is particularly
9854 important for y-or-n-p, but seems desirable generally.
9856 Value is non-zero if the window height has been changed. */
9859 resize_mini_window (struct window *w, int exact_p)
9861 struct frame *f = XFRAME (w->frame);
9862 int window_height_changed_p = 0;
9864 xassert (MINI_WINDOW_P (w));
9866 /* By default, start display at the beginning. */
9867 set_marker_both (w->start, w->buffer,
9868 BUF_BEGV (XBUFFER (w->buffer)),
9869 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9871 /* Don't resize windows while redisplaying a window; it would
9872 confuse redisplay functions when the size of the window they are
9873 displaying changes from under them. Such a resizing can happen,
9874 for instance, when which-func prints a long message while
9875 we are running fontification-functions. We're running these
9876 functions with safe_call which binds inhibit-redisplay to t. */
9877 if (!NILP (Vinhibit_redisplay))
9878 return 0;
9880 /* Nil means don't try to resize. */
9881 if (NILP (Vresize_mini_windows)
9882 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9883 return 0;
9885 if (!FRAME_MINIBUF_ONLY_P (f))
9887 struct it it;
9888 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9889 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9890 int height, max_height;
9891 int unit = FRAME_LINE_HEIGHT (f);
9892 struct text_pos start;
9893 struct buffer *old_current_buffer = NULL;
9895 if (current_buffer != XBUFFER (w->buffer))
9897 old_current_buffer = current_buffer;
9898 set_buffer_internal (XBUFFER (w->buffer));
9901 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9903 /* Compute the max. number of lines specified by the user. */
9904 if (FLOATP (Vmax_mini_window_height))
9905 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9906 else if (INTEGERP (Vmax_mini_window_height))
9907 max_height = XINT (Vmax_mini_window_height);
9908 else
9909 max_height = total_height / 4;
9911 /* Correct that max. height if it's bogus. */
9912 max_height = max (1, max_height);
9913 max_height = min (total_height, max_height);
9915 /* Find out the height of the text in the window. */
9916 if (it.line_wrap == TRUNCATE)
9917 height = 1;
9918 else
9920 last_height = 0;
9921 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9922 if (it.max_ascent == 0 && it.max_descent == 0)
9923 height = it.current_y + last_height;
9924 else
9925 height = it.current_y + it.max_ascent + it.max_descent;
9926 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9927 height = (height + unit - 1) / unit;
9930 /* Compute a suitable window start. */
9931 if (height > max_height)
9933 height = max_height;
9934 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9935 move_it_vertically_backward (&it, (height - 1) * unit);
9936 start = it.current.pos;
9938 else
9939 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9940 SET_MARKER_FROM_TEXT_POS (w->start, start);
9942 if (EQ (Vresize_mini_windows, Qgrow_only))
9944 /* Let it grow only, until we display an empty message, in which
9945 case the window shrinks again. */
9946 if (height > WINDOW_TOTAL_LINES (w))
9948 int old_height = WINDOW_TOTAL_LINES (w);
9949 freeze_window_starts (f, 1);
9950 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9951 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9953 else if (height < WINDOW_TOTAL_LINES (w)
9954 && (exact_p || BEGV == ZV))
9956 int old_height = WINDOW_TOTAL_LINES (w);
9957 freeze_window_starts (f, 0);
9958 shrink_mini_window (w);
9959 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9962 else
9964 /* Always resize to exact size needed. */
9965 if (height > WINDOW_TOTAL_LINES (w))
9967 int old_height = WINDOW_TOTAL_LINES (w);
9968 freeze_window_starts (f, 1);
9969 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9970 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9972 else if (height < WINDOW_TOTAL_LINES (w))
9974 int old_height = WINDOW_TOTAL_LINES (w);
9975 freeze_window_starts (f, 0);
9976 shrink_mini_window (w);
9978 if (height)
9980 freeze_window_starts (f, 1);
9981 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9984 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9988 if (old_current_buffer)
9989 set_buffer_internal (old_current_buffer);
9992 return window_height_changed_p;
9996 /* Value is the current message, a string, or nil if there is no
9997 current message. */
9999 Lisp_Object
10000 current_message (void)
10002 Lisp_Object msg;
10004 if (!BUFFERP (echo_area_buffer[0]))
10005 msg = Qnil;
10006 else
10008 with_echo_area_buffer (0, 0, current_message_1,
10009 (intptr_t) &msg, Qnil, 0, 0);
10010 if (NILP (msg))
10011 echo_area_buffer[0] = Qnil;
10014 return msg;
10018 static int
10019 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10021 intptr_t i1 = a1;
10022 Lisp_Object *msg = (Lisp_Object *) i1;
10024 if (Z > BEG)
10025 *msg = make_buffer_string (BEG, Z, 1);
10026 else
10027 *msg = Qnil;
10028 return 0;
10032 /* Push the current message on Vmessage_stack for later restauration
10033 by restore_message. Value is non-zero if the current message isn't
10034 empty. This is a relatively infrequent operation, so it's not
10035 worth optimizing. */
10038 push_message (void)
10040 Lisp_Object msg;
10041 msg = current_message ();
10042 Vmessage_stack = Fcons (msg, Vmessage_stack);
10043 return STRINGP (msg);
10047 /* Restore message display from the top of Vmessage_stack. */
10049 void
10050 restore_message (void)
10052 Lisp_Object msg;
10054 xassert (CONSP (Vmessage_stack));
10055 msg = XCAR (Vmessage_stack);
10056 if (STRINGP (msg))
10057 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10058 else
10059 message3_nolog (msg, 0, 0);
10063 /* Handler for record_unwind_protect calling pop_message. */
10065 Lisp_Object
10066 pop_message_unwind (Lisp_Object dummy)
10068 pop_message ();
10069 return Qnil;
10072 /* Pop the top-most entry off Vmessage_stack. */
10074 static void
10075 pop_message (void)
10077 xassert (CONSP (Vmessage_stack));
10078 Vmessage_stack = XCDR (Vmessage_stack);
10082 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10083 exits. If the stack is not empty, we have a missing pop_message
10084 somewhere. */
10086 void
10087 check_message_stack (void)
10089 if (!NILP (Vmessage_stack))
10090 abort ();
10094 /* Truncate to NCHARS what will be displayed in the echo area the next
10095 time we display it---but don't redisplay it now. */
10097 void
10098 truncate_echo_area (EMACS_INT nchars)
10100 if (nchars == 0)
10101 echo_area_buffer[0] = Qnil;
10102 /* A null message buffer means that the frame hasn't really been
10103 initialized yet. Error messages get reported properly by
10104 cmd_error, so this must be just an informative message; toss it. */
10105 else if (!noninteractive
10106 && INTERACTIVE
10107 && !NILP (echo_area_buffer[0]))
10109 struct frame *sf = SELECTED_FRAME ();
10110 if (FRAME_MESSAGE_BUF (sf))
10111 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10116 /* Helper function for truncate_echo_area. Truncate the current
10117 message to at most NCHARS characters. */
10119 static int
10120 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10122 if (BEG + nchars < Z)
10123 del_range (BEG + nchars, Z);
10124 if (Z == BEG)
10125 echo_area_buffer[0] = Qnil;
10126 return 0;
10130 /* Set the current message to a substring of S or STRING.
10132 If STRING is a Lisp string, set the message to the first NBYTES
10133 bytes from STRING. NBYTES zero means use the whole string. If
10134 STRING is multibyte, the message will be displayed multibyte.
10136 If S is not null, set the message to the first LEN bytes of S. LEN
10137 zero means use the whole string. MULTIBYTE_P non-zero means S is
10138 multibyte. Display the message multibyte in that case.
10140 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10141 to t before calling set_message_1 (which calls insert).
10144 static void
10145 set_message (const char *s, Lisp_Object string,
10146 EMACS_INT nbytes, int multibyte_p)
10148 message_enable_multibyte
10149 = ((s && multibyte_p)
10150 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10152 with_echo_area_buffer (0, -1, set_message_1,
10153 (intptr_t) s, string, nbytes, multibyte_p);
10154 message_buf_print = 0;
10155 help_echo_showing_p = 0;
10159 /* Helper function for set_message. Arguments have the same meaning
10160 as there, with A1 corresponding to S and A2 corresponding to STRING
10161 This function is called with the echo area buffer being
10162 current. */
10164 static int
10165 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10167 intptr_t i1 = a1;
10168 const char *s = (const char *) i1;
10169 const unsigned char *msg = (const unsigned char *) s;
10170 Lisp_Object string = a2;
10172 /* Change multibyteness of the echo buffer appropriately. */
10173 if (message_enable_multibyte
10174 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10175 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10177 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10178 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10179 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10181 /* Insert new message at BEG. */
10182 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10184 if (STRINGP (string))
10186 EMACS_INT nchars;
10188 if (nbytes == 0)
10189 nbytes = SBYTES (string);
10190 nchars = string_byte_to_char (string, nbytes);
10192 /* This function takes care of single/multibyte conversion. We
10193 just have to ensure that the echo area buffer has the right
10194 setting of enable_multibyte_characters. */
10195 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10197 else if (s)
10199 if (nbytes == 0)
10200 nbytes = strlen (s);
10202 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10204 /* Convert from multi-byte to single-byte. */
10205 EMACS_INT i;
10206 int c, n;
10207 char work[1];
10209 /* Convert a multibyte string to single-byte. */
10210 for (i = 0; i < nbytes; i += n)
10212 c = string_char_and_length (msg + i, &n);
10213 work[0] = (ASCII_CHAR_P (c)
10215 : multibyte_char_to_unibyte (c));
10216 insert_1_both (work, 1, 1, 1, 0, 0);
10219 else if (!multibyte_p
10220 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10222 /* Convert from single-byte to multi-byte. */
10223 EMACS_INT i;
10224 int c, n;
10225 unsigned char str[MAX_MULTIBYTE_LENGTH];
10227 /* Convert a single-byte string to multibyte. */
10228 for (i = 0; i < nbytes; i++)
10230 c = msg[i];
10231 MAKE_CHAR_MULTIBYTE (c);
10232 n = CHAR_STRING (c, str);
10233 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10236 else
10237 insert_1 (s, nbytes, 1, 0, 0);
10240 return 0;
10244 /* Clear messages. CURRENT_P non-zero means clear the current
10245 message. LAST_DISPLAYED_P non-zero means clear the message
10246 last displayed. */
10248 void
10249 clear_message (int current_p, int last_displayed_p)
10251 if (current_p)
10253 echo_area_buffer[0] = Qnil;
10254 message_cleared_p = 1;
10257 if (last_displayed_p)
10258 echo_area_buffer[1] = Qnil;
10260 message_buf_print = 0;
10263 /* Clear garbaged frames.
10265 This function is used where the old redisplay called
10266 redraw_garbaged_frames which in turn called redraw_frame which in
10267 turn called clear_frame. The call to clear_frame was a source of
10268 flickering. I believe a clear_frame is not necessary. It should
10269 suffice in the new redisplay to invalidate all current matrices,
10270 and ensure a complete redisplay of all windows. */
10272 static void
10273 clear_garbaged_frames (void)
10275 if (frame_garbaged)
10277 Lisp_Object tail, frame;
10278 int changed_count = 0;
10280 FOR_EACH_FRAME (tail, frame)
10282 struct frame *f = XFRAME (frame);
10284 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10286 if (f->resized_p)
10288 Fredraw_frame (frame);
10289 f->force_flush_display_p = 1;
10291 clear_current_matrices (f);
10292 changed_count++;
10293 f->garbaged = 0;
10294 f->resized_p = 0;
10298 frame_garbaged = 0;
10299 if (changed_count)
10300 ++windows_or_buffers_changed;
10305 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10306 is non-zero update selected_frame. Value is non-zero if the
10307 mini-windows height has been changed. */
10309 static int
10310 echo_area_display (int update_frame_p)
10312 Lisp_Object mini_window;
10313 struct window *w;
10314 struct frame *f;
10315 int window_height_changed_p = 0;
10316 struct frame *sf = SELECTED_FRAME ();
10318 mini_window = FRAME_MINIBUF_WINDOW (sf);
10319 w = XWINDOW (mini_window);
10320 f = XFRAME (WINDOW_FRAME (w));
10322 /* Don't display if frame is invisible or not yet initialized. */
10323 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10324 return 0;
10326 #ifdef HAVE_WINDOW_SYSTEM
10327 /* When Emacs starts, selected_frame may be the initial terminal
10328 frame. If we let this through, a message would be displayed on
10329 the terminal. */
10330 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10331 return 0;
10332 #endif /* HAVE_WINDOW_SYSTEM */
10334 /* Redraw garbaged frames. */
10335 if (frame_garbaged)
10336 clear_garbaged_frames ();
10338 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10340 echo_area_window = mini_window;
10341 window_height_changed_p = display_echo_area (w);
10342 w->must_be_updated_p = 1;
10344 /* Update the display, unless called from redisplay_internal.
10345 Also don't update the screen during redisplay itself. The
10346 update will happen at the end of redisplay, and an update
10347 here could cause confusion. */
10348 if (update_frame_p && !redisplaying_p)
10350 int n = 0;
10352 /* If the display update has been interrupted by pending
10353 input, update mode lines in the frame. Due to the
10354 pending input, it might have been that redisplay hasn't
10355 been called, so that mode lines above the echo area are
10356 garbaged. This looks odd, so we prevent it here. */
10357 if (!display_completed)
10358 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10360 if (window_height_changed_p
10361 /* Don't do this if Emacs is shutting down. Redisplay
10362 needs to run hooks. */
10363 && !NILP (Vrun_hooks))
10365 /* Must update other windows. Likewise as in other
10366 cases, don't let this update be interrupted by
10367 pending input. */
10368 int count = SPECPDL_INDEX ();
10369 specbind (Qredisplay_dont_pause, Qt);
10370 windows_or_buffers_changed = 1;
10371 redisplay_internal ();
10372 unbind_to (count, Qnil);
10374 else if (FRAME_WINDOW_P (f) && n == 0)
10376 /* Window configuration is the same as before.
10377 Can do with a display update of the echo area,
10378 unless we displayed some mode lines. */
10379 update_single_window (w, 1);
10380 FRAME_RIF (f)->flush_display (f);
10382 else
10383 update_frame (f, 1, 1);
10385 /* If cursor is in the echo area, make sure that the next
10386 redisplay displays the minibuffer, so that the cursor will
10387 be replaced with what the minibuffer wants. */
10388 if (cursor_in_echo_area)
10389 ++windows_or_buffers_changed;
10392 else if (!EQ (mini_window, selected_window))
10393 windows_or_buffers_changed++;
10395 /* Last displayed message is now the current message. */
10396 echo_area_buffer[1] = echo_area_buffer[0];
10397 /* Inform read_char that we're not echoing. */
10398 echo_message_buffer = Qnil;
10400 /* Prevent redisplay optimization in redisplay_internal by resetting
10401 this_line_start_pos. This is done because the mini-buffer now
10402 displays the message instead of its buffer text. */
10403 if (EQ (mini_window, selected_window))
10404 CHARPOS (this_line_start_pos) = 0;
10406 return window_height_changed_p;
10411 /***********************************************************************
10412 Mode Lines and Frame Titles
10413 ***********************************************************************/
10415 /* A buffer for constructing non-propertized mode-line strings and
10416 frame titles in it; allocated from the heap in init_xdisp and
10417 resized as needed in store_mode_line_noprop_char. */
10419 static char *mode_line_noprop_buf;
10421 /* The buffer's end, and a current output position in it. */
10423 static char *mode_line_noprop_buf_end;
10424 static char *mode_line_noprop_ptr;
10426 #define MODE_LINE_NOPROP_LEN(start) \
10427 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10429 static enum {
10430 MODE_LINE_DISPLAY = 0,
10431 MODE_LINE_TITLE,
10432 MODE_LINE_NOPROP,
10433 MODE_LINE_STRING
10434 } mode_line_target;
10436 /* Alist that caches the results of :propertize.
10437 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10438 static Lisp_Object mode_line_proptrans_alist;
10440 /* List of strings making up the mode-line. */
10441 static Lisp_Object mode_line_string_list;
10443 /* Base face property when building propertized mode line string. */
10444 static Lisp_Object mode_line_string_face;
10445 static Lisp_Object mode_line_string_face_prop;
10448 /* Unwind data for mode line strings */
10450 static Lisp_Object Vmode_line_unwind_vector;
10452 static Lisp_Object
10453 format_mode_line_unwind_data (struct buffer *obuf,
10454 Lisp_Object owin,
10455 int save_proptrans)
10457 Lisp_Object vector, tmp;
10459 /* Reduce consing by keeping one vector in
10460 Vwith_echo_area_save_vector. */
10461 vector = Vmode_line_unwind_vector;
10462 Vmode_line_unwind_vector = Qnil;
10464 if (NILP (vector))
10465 vector = Fmake_vector (make_number (8), Qnil);
10467 ASET (vector, 0, make_number (mode_line_target));
10468 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10469 ASET (vector, 2, mode_line_string_list);
10470 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10471 ASET (vector, 4, mode_line_string_face);
10472 ASET (vector, 5, mode_line_string_face_prop);
10474 if (obuf)
10475 XSETBUFFER (tmp, obuf);
10476 else
10477 tmp = Qnil;
10478 ASET (vector, 6, tmp);
10479 ASET (vector, 7, owin);
10481 return vector;
10484 static Lisp_Object
10485 unwind_format_mode_line (Lisp_Object vector)
10487 mode_line_target = XINT (AREF (vector, 0));
10488 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10489 mode_line_string_list = AREF (vector, 2);
10490 if (! EQ (AREF (vector, 3), Qt))
10491 mode_line_proptrans_alist = AREF (vector, 3);
10492 mode_line_string_face = AREF (vector, 4);
10493 mode_line_string_face_prop = AREF (vector, 5);
10495 if (!NILP (AREF (vector, 7)))
10496 /* Select window before buffer, since it may change the buffer. */
10497 Fselect_window (AREF (vector, 7), Qt);
10499 if (!NILP (AREF (vector, 6)))
10501 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10502 ASET (vector, 6, Qnil);
10505 Vmode_line_unwind_vector = vector;
10506 return Qnil;
10510 /* Store a single character C for the frame title in mode_line_noprop_buf.
10511 Re-allocate mode_line_noprop_buf if necessary. */
10513 static void
10514 store_mode_line_noprop_char (char c)
10516 /* If output position has reached the end of the allocated buffer,
10517 increase the buffer's size. */
10518 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10520 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10521 ptrdiff_t size = len;
10522 mode_line_noprop_buf =
10523 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10524 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10525 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10528 *mode_line_noprop_ptr++ = c;
10532 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10533 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10534 characters that yield more columns than PRECISION; PRECISION <= 0
10535 means copy the whole string. Pad with spaces until FIELD_WIDTH
10536 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10537 pad. Called from display_mode_element when it is used to build a
10538 frame title. */
10540 static int
10541 store_mode_line_noprop (const char *string, int field_width, int precision)
10543 const unsigned char *str = (const unsigned char *) string;
10544 int n = 0;
10545 EMACS_INT dummy, nbytes;
10547 /* Copy at most PRECISION chars from STR. */
10548 nbytes = strlen (string);
10549 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10550 while (nbytes--)
10551 store_mode_line_noprop_char (*str++);
10553 /* Fill up with spaces until FIELD_WIDTH reached. */
10554 while (field_width > 0
10555 && n < field_width)
10557 store_mode_line_noprop_char (' ');
10558 ++n;
10561 return n;
10564 /***********************************************************************
10565 Frame Titles
10566 ***********************************************************************/
10568 #ifdef HAVE_WINDOW_SYSTEM
10570 /* Set the title of FRAME, if it has changed. The title format is
10571 Vicon_title_format if FRAME is iconified, otherwise it is
10572 frame_title_format. */
10574 static void
10575 x_consider_frame_title (Lisp_Object frame)
10577 struct frame *f = XFRAME (frame);
10579 if (FRAME_WINDOW_P (f)
10580 || FRAME_MINIBUF_ONLY_P (f)
10581 || f->explicit_name)
10583 /* Do we have more than one visible frame on this X display? */
10584 Lisp_Object tail;
10585 Lisp_Object fmt;
10586 ptrdiff_t title_start;
10587 char *title;
10588 ptrdiff_t len;
10589 struct it it;
10590 int count = SPECPDL_INDEX ();
10592 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10594 Lisp_Object other_frame = XCAR (tail);
10595 struct frame *tf = XFRAME (other_frame);
10597 if (tf != f
10598 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10599 && !FRAME_MINIBUF_ONLY_P (tf)
10600 && !EQ (other_frame, tip_frame)
10601 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10602 break;
10605 /* Set global variable indicating that multiple frames exist. */
10606 multiple_frames = CONSP (tail);
10608 /* Switch to the buffer of selected window of the frame. Set up
10609 mode_line_target so that display_mode_element will output into
10610 mode_line_noprop_buf; then display the title. */
10611 record_unwind_protect (unwind_format_mode_line,
10612 format_mode_line_unwind_data
10613 (current_buffer, selected_window, 0));
10615 Fselect_window (f->selected_window, Qt);
10616 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10617 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10619 mode_line_target = MODE_LINE_TITLE;
10620 title_start = MODE_LINE_NOPROP_LEN (0);
10621 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10622 NULL, DEFAULT_FACE_ID);
10623 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10624 len = MODE_LINE_NOPROP_LEN (title_start);
10625 title = mode_line_noprop_buf + title_start;
10626 unbind_to (count, Qnil);
10628 /* Set the title only if it's changed. This avoids consing in
10629 the common case where it hasn't. (If it turns out that we've
10630 already wasted too much time by walking through the list with
10631 display_mode_element, then we might need to optimize at a
10632 higher level than this.) */
10633 if (! STRINGP (f->name)
10634 || SBYTES (f->name) != len
10635 || memcmp (title, SDATA (f->name), len) != 0)
10636 x_implicitly_set_name (f, make_string (title, len), Qnil);
10640 #endif /* not HAVE_WINDOW_SYSTEM */
10645 /***********************************************************************
10646 Menu Bars
10647 ***********************************************************************/
10650 /* Prepare for redisplay by updating menu-bar item lists when
10651 appropriate. This can call eval. */
10653 void
10654 prepare_menu_bars (void)
10656 int all_windows;
10657 struct gcpro gcpro1, gcpro2;
10658 struct frame *f;
10659 Lisp_Object tooltip_frame;
10661 #ifdef HAVE_WINDOW_SYSTEM
10662 tooltip_frame = tip_frame;
10663 #else
10664 tooltip_frame = Qnil;
10665 #endif
10667 /* Update all frame titles based on their buffer names, etc. We do
10668 this before the menu bars so that the buffer-menu will show the
10669 up-to-date frame titles. */
10670 #ifdef HAVE_WINDOW_SYSTEM
10671 if (windows_or_buffers_changed || update_mode_lines)
10673 Lisp_Object tail, frame;
10675 FOR_EACH_FRAME (tail, frame)
10677 f = XFRAME (frame);
10678 if (!EQ (frame, tooltip_frame)
10679 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10680 x_consider_frame_title (frame);
10683 #endif /* HAVE_WINDOW_SYSTEM */
10685 /* Update the menu bar item lists, if appropriate. This has to be
10686 done before any actual redisplay or generation of display lines. */
10687 all_windows = (update_mode_lines
10688 || buffer_shared > 1
10689 || windows_or_buffers_changed);
10690 if (all_windows)
10692 Lisp_Object tail, frame;
10693 int count = SPECPDL_INDEX ();
10694 /* 1 means that update_menu_bar has run its hooks
10695 so any further calls to update_menu_bar shouldn't do so again. */
10696 int menu_bar_hooks_run = 0;
10698 record_unwind_save_match_data ();
10700 FOR_EACH_FRAME (tail, frame)
10702 f = XFRAME (frame);
10704 /* Ignore tooltip frame. */
10705 if (EQ (frame, tooltip_frame))
10706 continue;
10708 /* If a window on this frame changed size, report that to
10709 the user and clear the size-change flag. */
10710 if (FRAME_WINDOW_SIZES_CHANGED (f))
10712 Lisp_Object functions;
10714 /* Clear flag first in case we get an error below. */
10715 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10716 functions = Vwindow_size_change_functions;
10717 GCPRO2 (tail, functions);
10719 while (CONSP (functions))
10721 if (!EQ (XCAR (functions), Qt))
10722 call1 (XCAR (functions), frame);
10723 functions = XCDR (functions);
10725 UNGCPRO;
10728 GCPRO1 (tail);
10729 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10730 #ifdef HAVE_WINDOW_SYSTEM
10731 update_tool_bar (f, 0);
10732 #endif
10733 #ifdef HAVE_NS
10734 if (windows_or_buffers_changed
10735 && FRAME_NS_P (f))
10736 ns_set_doc_edited (f, Fbuffer_modified_p
10737 (XWINDOW (f->selected_window)->buffer));
10738 #endif
10739 UNGCPRO;
10742 unbind_to (count, Qnil);
10744 else
10746 struct frame *sf = SELECTED_FRAME ();
10747 update_menu_bar (sf, 1, 0);
10748 #ifdef HAVE_WINDOW_SYSTEM
10749 update_tool_bar (sf, 1);
10750 #endif
10755 /* Update the menu bar item list for frame F. This has to be done
10756 before we start to fill in any display lines, because it can call
10757 eval.
10759 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10761 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10762 already ran the menu bar hooks for this redisplay, so there
10763 is no need to run them again. The return value is the
10764 updated value of this flag, to pass to the next call. */
10766 static int
10767 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10769 Lisp_Object window;
10770 register struct window *w;
10772 /* If called recursively during a menu update, do nothing. This can
10773 happen when, for instance, an activate-menubar-hook causes a
10774 redisplay. */
10775 if (inhibit_menubar_update)
10776 return hooks_run;
10778 window = FRAME_SELECTED_WINDOW (f);
10779 w = XWINDOW (window);
10781 if (FRAME_WINDOW_P (f)
10783 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10784 || defined (HAVE_NS) || defined (USE_GTK)
10785 FRAME_EXTERNAL_MENU_BAR (f)
10786 #else
10787 FRAME_MENU_BAR_LINES (f) > 0
10788 #endif
10789 : FRAME_MENU_BAR_LINES (f) > 0)
10791 /* If the user has switched buffers or windows, we need to
10792 recompute to reflect the new bindings. But we'll
10793 recompute when update_mode_lines is set too; that means
10794 that people can use force-mode-line-update to request
10795 that the menu bar be recomputed. The adverse effect on
10796 the rest of the redisplay algorithm is about the same as
10797 windows_or_buffers_changed anyway. */
10798 if (windows_or_buffers_changed
10799 /* This used to test w->update_mode_line, but we believe
10800 there is no need to recompute the menu in that case. */
10801 || update_mode_lines
10802 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10803 < BUF_MODIFF (XBUFFER (w->buffer)))
10804 != !NILP (w->last_had_star))
10805 || ((!NILP (Vtransient_mark_mode)
10806 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10807 != !NILP (w->region_showing)))
10809 struct buffer *prev = current_buffer;
10810 int count = SPECPDL_INDEX ();
10812 specbind (Qinhibit_menubar_update, Qt);
10814 set_buffer_internal_1 (XBUFFER (w->buffer));
10815 if (save_match_data)
10816 record_unwind_save_match_data ();
10817 if (NILP (Voverriding_local_map_menu_flag))
10819 specbind (Qoverriding_terminal_local_map, Qnil);
10820 specbind (Qoverriding_local_map, Qnil);
10823 if (!hooks_run)
10825 /* Run the Lucid hook. */
10826 safe_run_hooks (Qactivate_menubar_hook);
10828 /* If it has changed current-menubar from previous value,
10829 really recompute the menu-bar from the value. */
10830 if (! NILP (Vlucid_menu_bar_dirty_flag))
10831 call0 (Qrecompute_lucid_menubar);
10833 safe_run_hooks (Qmenu_bar_update_hook);
10835 hooks_run = 1;
10838 XSETFRAME (Vmenu_updating_frame, f);
10839 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10841 /* Redisplay the menu bar in case we changed it. */
10842 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10843 || defined (HAVE_NS) || defined (USE_GTK)
10844 if (FRAME_WINDOW_P (f))
10846 #if defined (HAVE_NS)
10847 /* All frames on Mac OS share the same menubar. So only
10848 the selected frame should be allowed to set it. */
10849 if (f == SELECTED_FRAME ())
10850 #endif
10851 set_frame_menubar (f, 0, 0);
10853 else
10854 /* On a terminal screen, the menu bar is an ordinary screen
10855 line, and this makes it get updated. */
10856 w->update_mode_line = Qt;
10857 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10858 /* In the non-toolkit version, the menu bar is an ordinary screen
10859 line, and this makes it get updated. */
10860 w->update_mode_line = Qt;
10861 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10863 unbind_to (count, Qnil);
10864 set_buffer_internal_1 (prev);
10868 return hooks_run;
10873 /***********************************************************************
10874 Output Cursor
10875 ***********************************************************************/
10877 #ifdef HAVE_WINDOW_SYSTEM
10879 /* EXPORT:
10880 Nominal cursor position -- where to draw output.
10881 HPOS and VPOS are window relative glyph matrix coordinates.
10882 X and Y are window relative pixel coordinates. */
10884 struct cursor_pos output_cursor;
10887 /* EXPORT:
10888 Set the global variable output_cursor to CURSOR. All cursor
10889 positions are relative to updated_window. */
10891 void
10892 set_output_cursor (struct cursor_pos *cursor)
10894 output_cursor.hpos = cursor->hpos;
10895 output_cursor.vpos = cursor->vpos;
10896 output_cursor.x = cursor->x;
10897 output_cursor.y = cursor->y;
10901 /* EXPORT for RIF:
10902 Set a nominal cursor position.
10904 HPOS and VPOS are column/row positions in a window glyph matrix. X
10905 and Y are window text area relative pixel positions.
10907 If this is done during an update, updated_window will contain the
10908 window that is being updated and the position is the future output
10909 cursor position for that window. If updated_window is null, use
10910 selected_window and display the cursor at the given position. */
10912 void
10913 x_cursor_to (int vpos, int hpos, int y, int x)
10915 struct window *w;
10917 /* If updated_window is not set, work on selected_window. */
10918 if (updated_window)
10919 w = updated_window;
10920 else
10921 w = XWINDOW (selected_window);
10923 /* Set the output cursor. */
10924 output_cursor.hpos = hpos;
10925 output_cursor.vpos = vpos;
10926 output_cursor.x = x;
10927 output_cursor.y = y;
10929 /* If not called as part of an update, really display the cursor.
10930 This will also set the cursor position of W. */
10931 if (updated_window == NULL)
10933 BLOCK_INPUT;
10934 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10935 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10936 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10937 UNBLOCK_INPUT;
10941 #endif /* HAVE_WINDOW_SYSTEM */
10944 /***********************************************************************
10945 Tool-bars
10946 ***********************************************************************/
10948 #ifdef HAVE_WINDOW_SYSTEM
10950 /* Where the mouse was last time we reported a mouse event. */
10952 FRAME_PTR last_mouse_frame;
10954 /* Tool-bar item index of the item on which a mouse button was pressed
10955 or -1. */
10957 int last_tool_bar_item;
10960 static Lisp_Object
10961 update_tool_bar_unwind (Lisp_Object frame)
10963 selected_frame = frame;
10964 return Qnil;
10967 /* Update the tool-bar item list for frame F. This has to be done
10968 before we start to fill in any display lines. Called from
10969 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10970 and restore it here. */
10972 static void
10973 update_tool_bar (struct frame *f, int save_match_data)
10975 #if defined (USE_GTK) || defined (HAVE_NS)
10976 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10977 #else
10978 int do_update = WINDOWP (f->tool_bar_window)
10979 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10980 #endif
10982 if (do_update)
10984 Lisp_Object window;
10985 struct window *w;
10987 window = FRAME_SELECTED_WINDOW (f);
10988 w = XWINDOW (window);
10990 /* If the user has switched buffers or windows, we need to
10991 recompute to reflect the new bindings. But we'll
10992 recompute when update_mode_lines is set too; that means
10993 that people can use force-mode-line-update to request
10994 that the menu bar be recomputed. The adverse effect on
10995 the rest of the redisplay algorithm is about the same as
10996 windows_or_buffers_changed anyway. */
10997 if (windows_or_buffers_changed
10998 || !NILP (w->update_mode_line)
10999 || update_mode_lines
11000 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11001 < BUF_MODIFF (XBUFFER (w->buffer)))
11002 != !NILP (w->last_had_star))
11003 || ((!NILP (Vtransient_mark_mode)
11004 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11005 != !NILP (w->region_showing)))
11007 struct buffer *prev = current_buffer;
11008 int count = SPECPDL_INDEX ();
11009 Lisp_Object frame, new_tool_bar;
11010 int new_n_tool_bar;
11011 struct gcpro gcpro1;
11013 /* Set current_buffer to the buffer of the selected
11014 window of the frame, so that we get the right local
11015 keymaps. */
11016 set_buffer_internal_1 (XBUFFER (w->buffer));
11018 /* Save match data, if we must. */
11019 if (save_match_data)
11020 record_unwind_save_match_data ();
11022 /* Make sure that we don't accidentally use bogus keymaps. */
11023 if (NILP (Voverriding_local_map_menu_flag))
11025 specbind (Qoverriding_terminal_local_map, Qnil);
11026 specbind (Qoverriding_local_map, Qnil);
11029 GCPRO1 (new_tool_bar);
11031 /* We must temporarily set the selected frame to this frame
11032 before calling tool_bar_items, because the calculation of
11033 the tool-bar keymap uses the selected frame (see
11034 `tool-bar-make-keymap' in tool-bar.el). */
11035 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11036 XSETFRAME (frame, f);
11037 selected_frame = frame;
11039 /* Build desired tool-bar items from keymaps. */
11040 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11041 &new_n_tool_bar);
11043 /* Redisplay the tool-bar if we changed it. */
11044 if (new_n_tool_bar != f->n_tool_bar_items
11045 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11047 /* Redisplay that happens asynchronously due to an expose event
11048 may access f->tool_bar_items. Make sure we update both
11049 variables within BLOCK_INPUT so no such event interrupts. */
11050 BLOCK_INPUT;
11051 f->tool_bar_items = new_tool_bar;
11052 f->n_tool_bar_items = new_n_tool_bar;
11053 w->update_mode_line = Qt;
11054 UNBLOCK_INPUT;
11057 UNGCPRO;
11059 unbind_to (count, Qnil);
11060 set_buffer_internal_1 (prev);
11066 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11067 F's desired tool-bar contents. F->tool_bar_items must have
11068 been set up previously by calling prepare_menu_bars. */
11070 static void
11071 build_desired_tool_bar_string (struct frame *f)
11073 int i, size, size_needed;
11074 struct gcpro gcpro1, gcpro2, gcpro3;
11075 Lisp_Object image, plist, props;
11077 image = plist = props = Qnil;
11078 GCPRO3 (image, plist, props);
11080 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11081 Otherwise, make a new string. */
11083 /* The size of the string we might be able to reuse. */
11084 size = (STRINGP (f->desired_tool_bar_string)
11085 ? SCHARS (f->desired_tool_bar_string)
11086 : 0);
11088 /* We need one space in the string for each image. */
11089 size_needed = f->n_tool_bar_items;
11091 /* Reuse f->desired_tool_bar_string, if possible. */
11092 if (size < size_needed || NILP (f->desired_tool_bar_string))
11093 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11094 make_number (' '));
11095 else
11097 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11098 Fremove_text_properties (make_number (0), make_number (size),
11099 props, f->desired_tool_bar_string);
11102 /* Put a `display' property on the string for the images to display,
11103 put a `menu_item' property on tool-bar items with a value that
11104 is the index of the item in F's tool-bar item vector. */
11105 for (i = 0; i < f->n_tool_bar_items; ++i)
11107 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11109 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11110 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11111 int hmargin, vmargin, relief, idx, end;
11113 /* If image is a vector, choose the image according to the
11114 button state. */
11115 image = PROP (TOOL_BAR_ITEM_IMAGES);
11116 if (VECTORP (image))
11118 if (enabled_p)
11119 idx = (selected_p
11120 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11121 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11122 else
11123 idx = (selected_p
11124 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11125 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11127 xassert (ASIZE (image) >= idx);
11128 image = AREF (image, idx);
11130 else
11131 idx = -1;
11133 /* Ignore invalid image specifications. */
11134 if (!valid_image_p (image))
11135 continue;
11137 /* Display the tool-bar button pressed, or depressed. */
11138 plist = Fcopy_sequence (XCDR (image));
11140 /* Compute margin and relief to draw. */
11141 relief = (tool_bar_button_relief >= 0
11142 ? tool_bar_button_relief
11143 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11144 hmargin = vmargin = relief;
11146 if (INTEGERP (Vtool_bar_button_margin)
11147 && XINT (Vtool_bar_button_margin) > 0)
11149 hmargin += XFASTINT (Vtool_bar_button_margin);
11150 vmargin += XFASTINT (Vtool_bar_button_margin);
11152 else if (CONSP (Vtool_bar_button_margin))
11154 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11155 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11156 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11158 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11159 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11160 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11163 if (auto_raise_tool_bar_buttons_p)
11165 /* Add a `:relief' property to the image spec if the item is
11166 selected. */
11167 if (selected_p)
11169 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11170 hmargin -= relief;
11171 vmargin -= relief;
11174 else
11176 /* If image is selected, display it pressed, i.e. with a
11177 negative relief. If it's not selected, display it with a
11178 raised relief. */
11179 plist = Fplist_put (plist, QCrelief,
11180 (selected_p
11181 ? make_number (-relief)
11182 : make_number (relief)));
11183 hmargin -= relief;
11184 vmargin -= relief;
11187 /* Put a margin around the image. */
11188 if (hmargin || vmargin)
11190 if (hmargin == vmargin)
11191 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11192 else
11193 plist = Fplist_put (plist, QCmargin,
11194 Fcons (make_number (hmargin),
11195 make_number (vmargin)));
11198 /* If button is not enabled, and we don't have special images
11199 for the disabled state, make the image appear disabled by
11200 applying an appropriate algorithm to it. */
11201 if (!enabled_p && idx < 0)
11202 plist = Fplist_put (plist, QCconversion, Qdisabled);
11204 /* Put a `display' text property on the string for the image to
11205 display. Put a `menu-item' property on the string that gives
11206 the start of this item's properties in the tool-bar items
11207 vector. */
11208 image = Fcons (Qimage, plist);
11209 props = list4 (Qdisplay, image,
11210 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11212 /* Let the last image hide all remaining spaces in the tool bar
11213 string. The string can be longer than needed when we reuse a
11214 previous string. */
11215 if (i + 1 == f->n_tool_bar_items)
11216 end = SCHARS (f->desired_tool_bar_string);
11217 else
11218 end = i + 1;
11219 Fadd_text_properties (make_number (i), make_number (end),
11220 props, f->desired_tool_bar_string);
11221 #undef PROP
11224 UNGCPRO;
11228 /* Display one line of the tool-bar of frame IT->f.
11230 HEIGHT specifies the desired height of the tool-bar line.
11231 If the actual height of the glyph row is less than HEIGHT, the
11232 row's height is increased to HEIGHT, and the icons are centered
11233 vertically in the new height.
11235 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11236 count a final empty row in case the tool-bar width exactly matches
11237 the window width.
11240 static void
11241 display_tool_bar_line (struct it *it, int height)
11243 struct glyph_row *row = it->glyph_row;
11244 int max_x = it->last_visible_x;
11245 struct glyph *last;
11247 prepare_desired_row (row);
11248 row->y = it->current_y;
11250 /* Note that this isn't made use of if the face hasn't a box,
11251 so there's no need to check the face here. */
11252 it->start_of_box_run_p = 1;
11254 while (it->current_x < max_x)
11256 int x, n_glyphs_before, i, nglyphs;
11257 struct it it_before;
11259 /* Get the next display element. */
11260 if (!get_next_display_element (it))
11262 /* Don't count empty row if we are counting needed tool-bar lines. */
11263 if (height < 0 && !it->hpos)
11264 return;
11265 break;
11268 /* Produce glyphs. */
11269 n_glyphs_before = row->used[TEXT_AREA];
11270 it_before = *it;
11272 PRODUCE_GLYPHS (it);
11274 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11275 i = 0;
11276 x = it_before.current_x;
11277 while (i < nglyphs)
11279 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11281 if (x + glyph->pixel_width > max_x)
11283 /* Glyph doesn't fit on line. Backtrack. */
11284 row->used[TEXT_AREA] = n_glyphs_before;
11285 *it = it_before;
11286 /* If this is the only glyph on this line, it will never fit on the
11287 tool-bar, so skip it. But ensure there is at least one glyph,
11288 so we don't accidentally disable the tool-bar. */
11289 if (n_glyphs_before == 0
11290 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11291 break;
11292 goto out;
11295 ++it->hpos;
11296 x += glyph->pixel_width;
11297 ++i;
11300 /* Stop at line end. */
11301 if (ITERATOR_AT_END_OF_LINE_P (it))
11302 break;
11304 set_iterator_to_next (it, 1);
11307 out:;
11309 row->displays_text_p = row->used[TEXT_AREA] != 0;
11311 /* Use default face for the border below the tool bar.
11313 FIXME: When auto-resize-tool-bars is grow-only, there is
11314 no additional border below the possibly empty tool-bar lines.
11315 So to make the extra empty lines look "normal", we have to
11316 use the tool-bar face for the border too. */
11317 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11318 it->face_id = DEFAULT_FACE_ID;
11320 extend_face_to_end_of_line (it);
11321 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11322 last->right_box_line_p = 1;
11323 if (last == row->glyphs[TEXT_AREA])
11324 last->left_box_line_p = 1;
11326 /* Make line the desired height and center it vertically. */
11327 if ((height -= it->max_ascent + it->max_descent) > 0)
11329 /* Don't add more than one line height. */
11330 height %= FRAME_LINE_HEIGHT (it->f);
11331 it->max_ascent += height / 2;
11332 it->max_descent += (height + 1) / 2;
11335 compute_line_metrics (it);
11337 /* If line is empty, make it occupy the rest of the tool-bar. */
11338 if (!row->displays_text_p)
11340 row->height = row->phys_height = it->last_visible_y - row->y;
11341 row->visible_height = row->height;
11342 row->ascent = row->phys_ascent = 0;
11343 row->extra_line_spacing = 0;
11346 row->full_width_p = 1;
11347 row->continued_p = 0;
11348 row->truncated_on_left_p = 0;
11349 row->truncated_on_right_p = 0;
11351 it->current_x = it->hpos = 0;
11352 it->current_y += row->height;
11353 ++it->vpos;
11354 ++it->glyph_row;
11358 /* Max tool-bar height. */
11360 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11361 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11363 /* Value is the number of screen lines needed to make all tool-bar
11364 items of frame F visible. The number of actual rows needed is
11365 returned in *N_ROWS if non-NULL. */
11367 static int
11368 tool_bar_lines_needed (struct frame *f, int *n_rows)
11370 struct window *w = XWINDOW (f->tool_bar_window);
11371 struct it it;
11372 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11373 the desired matrix, so use (unused) mode-line row as temporary row to
11374 avoid destroying the first tool-bar row. */
11375 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11377 /* Initialize an iterator for iteration over
11378 F->desired_tool_bar_string in the tool-bar window of frame F. */
11379 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11380 it.first_visible_x = 0;
11381 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11382 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11383 it.paragraph_embedding = L2R;
11385 while (!ITERATOR_AT_END_P (&it))
11387 clear_glyph_row (temp_row);
11388 it.glyph_row = temp_row;
11389 display_tool_bar_line (&it, -1);
11391 clear_glyph_row (temp_row);
11393 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11394 if (n_rows)
11395 *n_rows = it.vpos > 0 ? it.vpos : -1;
11397 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11401 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11402 0, 1, 0,
11403 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11404 (Lisp_Object frame)
11406 struct frame *f;
11407 struct window *w;
11408 int nlines = 0;
11410 if (NILP (frame))
11411 frame = selected_frame;
11412 else
11413 CHECK_FRAME (frame);
11414 f = XFRAME (frame);
11416 if (WINDOWP (f->tool_bar_window)
11417 && (w = XWINDOW (f->tool_bar_window),
11418 WINDOW_TOTAL_LINES (w) > 0))
11420 update_tool_bar (f, 1);
11421 if (f->n_tool_bar_items)
11423 build_desired_tool_bar_string (f);
11424 nlines = tool_bar_lines_needed (f, NULL);
11428 return make_number (nlines);
11432 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11433 height should be changed. */
11435 static int
11436 redisplay_tool_bar (struct frame *f)
11438 struct window *w;
11439 struct it it;
11440 struct glyph_row *row;
11442 #if defined (USE_GTK) || defined (HAVE_NS)
11443 if (FRAME_EXTERNAL_TOOL_BAR (f))
11444 update_frame_tool_bar (f);
11445 return 0;
11446 #endif
11448 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11449 do anything. This means you must start with tool-bar-lines
11450 non-zero to get the auto-sizing effect. Or in other words, you
11451 can turn off tool-bars by specifying tool-bar-lines zero. */
11452 if (!WINDOWP (f->tool_bar_window)
11453 || (w = XWINDOW (f->tool_bar_window),
11454 WINDOW_TOTAL_LINES (w) == 0))
11455 return 0;
11457 /* Set up an iterator for the tool-bar window. */
11458 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11459 it.first_visible_x = 0;
11460 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11461 row = it.glyph_row;
11463 /* Build a string that represents the contents of the tool-bar. */
11464 build_desired_tool_bar_string (f);
11465 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11466 /* FIXME: This should be controlled by a user option. But it
11467 doesn't make sense to have an R2L tool bar if the menu bar cannot
11468 be drawn also R2L, and making the menu bar R2L is tricky due
11469 toolkit-specific code that implements it. If an R2L tool bar is
11470 ever supported, display_tool_bar_line should also be augmented to
11471 call unproduce_glyphs like display_line and display_string
11472 do. */
11473 it.paragraph_embedding = L2R;
11475 if (f->n_tool_bar_rows == 0)
11477 int nlines;
11479 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11480 nlines != WINDOW_TOTAL_LINES (w)))
11482 Lisp_Object frame;
11483 int old_height = WINDOW_TOTAL_LINES (w);
11485 XSETFRAME (frame, f);
11486 Fmodify_frame_parameters (frame,
11487 Fcons (Fcons (Qtool_bar_lines,
11488 make_number (nlines)),
11489 Qnil));
11490 if (WINDOW_TOTAL_LINES (w) != old_height)
11492 clear_glyph_matrix (w->desired_matrix);
11493 fonts_changed_p = 1;
11494 return 1;
11499 /* Display as many lines as needed to display all tool-bar items. */
11501 if (f->n_tool_bar_rows > 0)
11503 int border, rows, height, extra;
11505 if (INTEGERP (Vtool_bar_border))
11506 border = XINT (Vtool_bar_border);
11507 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11508 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11509 else if (EQ (Vtool_bar_border, Qborder_width))
11510 border = f->border_width;
11511 else
11512 border = 0;
11513 if (border < 0)
11514 border = 0;
11516 rows = f->n_tool_bar_rows;
11517 height = max (1, (it.last_visible_y - border) / rows);
11518 extra = it.last_visible_y - border - height * rows;
11520 while (it.current_y < it.last_visible_y)
11522 int h = 0;
11523 if (extra > 0 && rows-- > 0)
11525 h = (extra + rows - 1) / rows;
11526 extra -= h;
11528 display_tool_bar_line (&it, height + h);
11531 else
11533 while (it.current_y < it.last_visible_y)
11534 display_tool_bar_line (&it, 0);
11537 /* It doesn't make much sense to try scrolling in the tool-bar
11538 window, so don't do it. */
11539 w->desired_matrix->no_scrolling_p = 1;
11540 w->must_be_updated_p = 1;
11542 if (!NILP (Vauto_resize_tool_bars))
11544 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11545 int change_height_p = 0;
11547 /* If we couldn't display everything, change the tool-bar's
11548 height if there is room for more. */
11549 if (IT_STRING_CHARPOS (it) < it.end_charpos
11550 && it.current_y < max_tool_bar_height)
11551 change_height_p = 1;
11553 row = it.glyph_row - 1;
11555 /* If there are blank lines at the end, except for a partially
11556 visible blank line at the end that is smaller than
11557 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11558 if (!row->displays_text_p
11559 && row->height >= FRAME_LINE_HEIGHT (f))
11560 change_height_p = 1;
11562 /* If row displays tool-bar items, but is partially visible,
11563 change the tool-bar's height. */
11564 if (row->displays_text_p
11565 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11566 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11567 change_height_p = 1;
11569 /* Resize windows as needed by changing the `tool-bar-lines'
11570 frame parameter. */
11571 if (change_height_p)
11573 Lisp_Object frame;
11574 int old_height = WINDOW_TOTAL_LINES (w);
11575 int nrows;
11576 int nlines = tool_bar_lines_needed (f, &nrows);
11578 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11579 && !f->minimize_tool_bar_window_p)
11580 ? (nlines > old_height)
11581 : (nlines != old_height));
11582 f->minimize_tool_bar_window_p = 0;
11584 if (change_height_p)
11586 XSETFRAME (frame, f);
11587 Fmodify_frame_parameters (frame,
11588 Fcons (Fcons (Qtool_bar_lines,
11589 make_number (nlines)),
11590 Qnil));
11591 if (WINDOW_TOTAL_LINES (w) != old_height)
11593 clear_glyph_matrix (w->desired_matrix);
11594 f->n_tool_bar_rows = nrows;
11595 fonts_changed_p = 1;
11596 return 1;
11602 f->minimize_tool_bar_window_p = 0;
11603 return 0;
11607 /* Get information about the tool-bar item which is displayed in GLYPH
11608 on frame F. Return in *PROP_IDX the index where tool-bar item
11609 properties start in F->tool_bar_items. Value is zero if
11610 GLYPH doesn't display a tool-bar item. */
11612 static int
11613 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11615 Lisp_Object prop;
11616 int success_p;
11617 int charpos;
11619 /* This function can be called asynchronously, which means we must
11620 exclude any possibility that Fget_text_property signals an
11621 error. */
11622 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11623 charpos = max (0, charpos);
11625 /* Get the text property `menu-item' at pos. The value of that
11626 property is the start index of this item's properties in
11627 F->tool_bar_items. */
11628 prop = Fget_text_property (make_number (charpos),
11629 Qmenu_item, f->current_tool_bar_string);
11630 if (INTEGERP (prop))
11632 *prop_idx = XINT (prop);
11633 success_p = 1;
11635 else
11636 success_p = 0;
11638 return success_p;
11642 /* Get information about the tool-bar item at position X/Y on frame F.
11643 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11644 the current matrix of the tool-bar window of F, or NULL if not
11645 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11646 item in F->tool_bar_items. Value is
11648 -1 if X/Y is not on a tool-bar item
11649 0 if X/Y is on the same item that was highlighted before.
11650 1 otherwise. */
11652 static int
11653 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11654 int *hpos, int *vpos, int *prop_idx)
11656 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11657 struct window *w = XWINDOW (f->tool_bar_window);
11658 int area;
11660 /* Find the glyph under X/Y. */
11661 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11662 if (*glyph == NULL)
11663 return -1;
11665 /* Get the start of this tool-bar item's properties in
11666 f->tool_bar_items. */
11667 if (!tool_bar_item_info (f, *glyph, prop_idx))
11668 return -1;
11670 /* Is mouse on the highlighted item? */
11671 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11672 && *vpos >= hlinfo->mouse_face_beg_row
11673 && *vpos <= hlinfo->mouse_face_end_row
11674 && (*vpos > hlinfo->mouse_face_beg_row
11675 || *hpos >= hlinfo->mouse_face_beg_col)
11676 && (*vpos < hlinfo->mouse_face_end_row
11677 || *hpos < hlinfo->mouse_face_end_col
11678 || hlinfo->mouse_face_past_end))
11679 return 0;
11681 return 1;
11685 /* EXPORT:
11686 Handle mouse button event on the tool-bar of frame F, at
11687 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11688 0 for button release. MODIFIERS is event modifiers for button
11689 release. */
11691 void
11692 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11693 unsigned int modifiers)
11695 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11696 struct window *w = XWINDOW (f->tool_bar_window);
11697 int hpos, vpos, prop_idx;
11698 struct glyph *glyph;
11699 Lisp_Object enabled_p;
11701 /* If not on the highlighted tool-bar item, return. */
11702 frame_to_window_pixel_xy (w, &x, &y);
11703 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11704 return;
11706 /* If item is disabled, do nothing. */
11707 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11708 if (NILP (enabled_p))
11709 return;
11711 if (down_p)
11713 /* Show item in pressed state. */
11714 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11715 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11716 last_tool_bar_item = prop_idx;
11718 else
11720 Lisp_Object key, frame;
11721 struct input_event event;
11722 EVENT_INIT (event);
11724 /* Show item in released state. */
11725 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11726 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11728 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11730 XSETFRAME (frame, f);
11731 event.kind = TOOL_BAR_EVENT;
11732 event.frame_or_window = frame;
11733 event.arg = frame;
11734 kbd_buffer_store_event (&event);
11736 event.kind = TOOL_BAR_EVENT;
11737 event.frame_or_window = frame;
11738 event.arg = key;
11739 event.modifiers = modifiers;
11740 kbd_buffer_store_event (&event);
11741 last_tool_bar_item = -1;
11746 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11747 tool-bar window-relative coordinates X/Y. Called from
11748 note_mouse_highlight. */
11750 static void
11751 note_tool_bar_highlight (struct frame *f, int x, int y)
11753 Lisp_Object window = f->tool_bar_window;
11754 struct window *w = XWINDOW (window);
11755 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11756 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11757 int hpos, vpos;
11758 struct glyph *glyph;
11759 struct glyph_row *row;
11760 int i;
11761 Lisp_Object enabled_p;
11762 int prop_idx;
11763 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11764 int mouse_down_p, rc;
11766 /* Function note_mouse_highlight is called with negative X/Y
11767 values when mouse moves outside of the frame. */
11768 if (x <= 0 || y <= 0)
11770 clear_mouse_face (hlinfo);
11771 return;
11774 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11775 if (rc < 0)
11777 /* Not on tool-bar item. */
11778 clear_mouse_face (hlinfo);
11779 return;
11781 else if (rc == 0)
11782 /* On same tool-bar item as before. */
11783 goto set_help_echo;
11785 clear_mouse_face (hlinfo);
11787 /* Mouse is down, but on different tool-bar item? */
11788 mouse_down_p = (dpyinfo->grabbed
11789 && f == last_mouse_frame
11790 && FRAME_LIVE_P (f));
11791 if (mouse_down_p
11792 && last_tool_bar_item != prop_idx)
11793 return;
11795 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11796 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11798 /* If tool-bar item is not enabled, don't highlight it. */
11799 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11800 if (!NILP (enabled_p))
11802 /* Compute the x-position of the glyph. In front and past the
11803 image is a space. We include this in the highlighted area. */
11804 row = MATRIX_ROW (w->current_matrix, vpos);
11805 for (i = x = 0; i < hpos; ++i)
11806 x += row->glyphs[TEXT_AREA][i].pixel_width;
11808 /* Record this as the current active region. */
11809 hlinfo->mouse_face_beg_col = hpos;
11810 hlinfo->mouse_face_beg_row = vpos;
11811 hlinfo->mouse_face_beg_x = x;
11812 hlinfo->mouse_face_beg_y = row->y;
11813 hlinfo->mouse_face_past_end = 0;
11815 hlinfo->mouse_face_end_col = hpos + 1;
11816 hlinfo->mouse_face_end_row = vpos;
11817 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11818 hlinfo->mouse_face_end_y = row->y;
11819 hlinfo->mouse_face_window = window;
11820 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11822 /* Display it as active. */
11823 show_mouse_face (hlinfo, draw);
11824 hlinfo->mouse_face_image_state = draw;
11827 set_help_echo:
11829 /* Set help_echo_string to a help string to display for this tool-bar item.
11830 XTread_socket does the rest. */
11831 help_echo_object = help_echo_window = Qnil;
11832 help_echo_pos = -1;
11833 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11834 if (NILP (help_echo_string))
11835 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11838 #endif /* HAVE_WINDOW_SYSTEM */
11842 /************************************************************************
11843 Horizontal scrolling
11844 ************************************************************************/
11846 static int hscroll_window_tree (Lisp_Object);
11847 static int hscroll_windows (Lisp_Object);
11849 /* For all leaf windows in the window tree rooted at WINDOW, set their
11850 hscroll value so that PT is (i) visible in the window, and (ii) so
11851 that it is not within a certain margin at the window's left and
11852 right border. Value is non-zero if any window's hscroll has been
11853 changed. */
11855 static int
11856 hscroll_window_tree (Lisp_Object window)
11858 int hscrolled_p = 0;
11859 int hscroll_relative_p = FLOATP (Vhscroll_step);
11860 int hscroll_step_abs = 0;
11861 double hscroll_step_rel = 0;
11863 if (hscroll_relative_p)
11865 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11866 if (hscroll_step_rel < 0)
11868 hscroll_relative_p = 0;
11869 hscroll_step_abs = 0;
11872 else if (INTEGERP (Vhscroll_step))
11874 hscroll_step_abs = XINT (Vhscroll_step);
11875 if (hscroll_step_abs < 0)
11876 hscroll_step_abs = 0;
11878 else
11879 hscroll_step_abs = 0;
11881 while (WINDOWP (window))
11883 struct window *w = XWINDOW (window);
11885 if (WINDOWP (w->hchild))
11886 hscrolled_p |= hscroll_window_tree (w->hchild);
11887 else if (WINDOWP (w->vchild))
11888 hscrolled_p |= hscroll_window_tree (w->vchild);
11889 else if (w->cursor.vpos >= 0)
11891 int h_margin;
11892 int text_area_width;
11893 struct glyph_row *current_cursor_row
11894 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11895 struct glyph_row *desired_cursor_row
11896 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11897 struct glyph_row *cursor_row
11898 = (desired_cursor_row->enabled_p
11899 ? desired_cursor_row
11900 : current_cursor_row);
11902 text_area_width = window_box_width (w, TEXT_AREA);
11904 /* Scroll when cursor is inside this scroll margin. */
11905 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11907 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11908 && ((XFASTINT (w->hscroll)
11909 && w->cursor.x <= h_margin)
11910 || (cursor_row->enabled_p
11911 && cursor_row->truncated_on_right_p
11912 && (w->cursor.x >= text_area_width - h_margin))))
11914 struct it it;
11915 int hscroll;
11916 struct buffer *saved_current_buffer;
11917 EMACS_INT pt;
11918 int wanted_x;
11920 /* Find point in a display of infinite width. */
11921 saved_current_buffer = current_buffer;
11922 current_buffer = XBUFFER (w->buffer);
11924 if (w == XWINDOW (selected_window))
11925 pt = PT;
11926 else
11928 pt = marker_position (w->pointm);
11929 pt = max (BEGV, pt);
11930 pt = min (ZV, pt);
11933 /* Move iterator to pt starting at cursor_row->start in
11934 a line with infinite width. */
11935 init_to_row_start (&it, w, cursor_row);
11936 it.last_visible_x = INFINITY;
11937 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11938 current_buffer = saved_current_buffer;
11940 /* Position cursor in window. */
11941 if (!hscroll_relative_p && hscroll_step_abs == 0)
11942 hscroll = max (0, (it.current_x
11943 - (ITERATOR_AT_END_OF_LINE_P (&it)
11944 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11945 : (text_area_width / 2))))
11946 / FRAME_COLUMN_WIDTH (it.f);
11947 else if (w->cursor.x >= text_area_width - h_margin)
11949 if (hscroll_relative_p)
11950 wanted_x = text_area_width * (1 - hscroll_step_rel)
11951 - h_margin;
11952 else
11953 wanted_x = text_area_width
11954 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11955 - h_margin;
11956 hscroll
11957 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11959 else
11961 if (hscroll_relative_p)
11962 wanted_x = text_area_width * hscroll_step_rel
11963 + h_margin;
11964 else
11965 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11966 + h_margin;
11967 hscroll
11968 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11970 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11972 /* Don't call Fset_window_hscroll if value hasn't
11973 changed because it will prevent redisplay
11974 optimizations. */
11975 if (XFASTINT (w->hscroll) != hscroll)
11977 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11978 w->hscroll = make_number (hscroll);
11979 hscrolled_p = 1;
11984 window = w->next;
11987 /* Value is non-zero if hscroll of any leaf window has been changed. */
11988 return hscrolled_p;
11992 /* Set hscroll so that cursor is visible and not inside horizontal
11993 scroll margins for all windows in the tree rooted at WINDOW. See
11994 also hscroll_window_tree above. Value is non-zero if any window's
11995 hscroll has been changed. If it has, desired matrices on the frame
11996 of WINDOW are cleared. */
11998 static int
11999 hscroll_windows (Lisp_Object window)
12001 int hscrolled_p = hscroll_window_tree (window);
12002 if (hscrolled_p)
12003 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12004 return hscrolled_p;
12009 /************************************************************************
12010 Redisplay
12011 ************************************************************************/
12013 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12014 to a non-zero value. This is sometimes handy to have in a debugger
12015 session. */
12017 #if GLYPH_DEBUG
12019 /* First and last unchanged row for try_window_id. */
12021 static int debug_first_unchanged_at_end_vpos;
12022 static int debug_last_unchanged_at_beg_vpos;
12024 /* Delta vpos and y. */
12026 static int debug_dvpos, debug_dy;
12028 /* Delta in characters and bytes for try_window_id. */
12030 static EMACS_INT debug_delta, debug_delta_bytes;
12032 /* Values of window_end_pos and window_end_vpos at the end of
12033 try_window_id. */
12035 static EMACS_INT debug_end_vpos;
12037 /* Append a string to W->desired_matrix->method. FMT is a printf
12038 format string. If trace_redisplay_p is non-zero also printf the
12039 resulting string to stderr. */
12041 static void debug_method_add (struct window *, char const *, ...)
12042 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12044 static void
12045 debug_method_add (struct window *w, char const *fmt, ...)
12047 char buffer[512];
12048 char *method = w->desired_matrix->method;
12049 int len = strlen (method);
12050 int size = sizeof w->desired_matrix->method;
12051 int remaining = size - len - 1;
12052 va_list ap;
12054 va_start (ap, fmt);
12055 vsprintf (buffer, fmt, ap);
12056 va_end (ap);
12057 if (len && remaining)
12059 method[len] = '|';
12060 --remaining, ++len;
12063 strncpy (method + len, buffer, remaining);
12065 if (trace_redisplay_p)
12066 fprintf (stderr, "%p (%s): %s\n",
12068 ((BUFFERP (w->buffer)
12069 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12070 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12071 : "no buffer"),
12072 buffer);
12075 #endif /* GLYPH_DEBUG */
12078 /* Value is non-zero if all changes in window W, which displays
12079 current_buffer, are in the text between START and END. START is a
12080 buffer position, END is given as a distance from Z. Used in
12081 redisplay_internal for display optimization. */
12083 static inline int
12084 text_outside_line_unchanged_p (struct window *w,
12085 EMACS_INT start, EMACS_INT end)
12087 int unchanged_p = 1;
12089 /* If text or overlays have changed, see where. */
12090 if (XFASTINT (w->last_modified) < MODIFF
12091 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12093 /* Gap in the line? */
12094 if (GPT < start || Z - GPT < end)
12095 unchanged_p = 0;
12097 /* Changes start in front of the line, or end after it? */
12098 if (unchanged_p
12099 && (BEG_UNCHANGED < start - 1
12100 || END_UNCHANGED < end))
12101 unchanged_p = 0;
12103 /* If selective display, can't optimize if changes start at the
12104 beginning of the line. */
12105 if (unchanged_p
12106 && INTEGERP (BVAR (current_buffer, selective_display))
12107 && XINT (BVAR (current_buffer, selective_display)) > 0
12108 && (BEG_UNCHANGED < start || GPT <= start))
12109 unchanged_p = 0;
12111 /* If there are overlays at the start or end of the line, these
12112 may have overlay strings with newlines in them. A change at
12113 START, for instance, may actually concern the display of such
12114 overlay strings as well, and they are displayed on different
12115 lines. So, quickly rule out this case. (For the future, it
12116 might be desirable to implement something more telling than
12117 just BEG/END_UNCHANGED.) */
12118 if (unchanged_p)
12120 if (BEG + BEG_UNCHANGED == start
12121 && overlay_touches_p (start))
12122 unchanged_p = 0;
12123 if (END_UNCHANGED == end
12124 && overlay_touches_p (Z - end))
12125 unchanged_p = 0;
12128 /* Under bidi reordering, adding or deleting a character in the
12129 beginning of a paragraph, before the first strong directional
12130 character, can change the base direction of the paragraph (unless
12131 the buffer specifies a fixed paragraph direction), which will
12132 require to redisplay the whole paragraph. It might be worthwhile
12133 to find the paragraph limits and widen the range of redisplayed
12134 lines to that, but for now just give up this optimization. */
12135 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12136 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12137 unchanged_p = 0;
12140 return unchanged_p;
12144 /* Do a frame update, taking possible shortcuts into account. This is
12145 the main external entry point for redisplay.
12147 If the last redisplay displayed an echo area message and that message
12148 is no longer requested, we clear the echo area or bring back the
12149 mini-buffer if that is in use. */
12151 void
12152 redisplay (void)
12154 redisplay_internal ();
12158 static Lisp_Object
12159 overlay_arrow_string_or_property (Lisp_Object var)
12161 Lisp_Object val;
12163 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12164 return val;
12166 return Voverlay_arrow_string;
12169 /* Return 1 if there are any overlay-arrows in current_buffer. */
12170 static int
12171 overlay_arrow_in_current_buffer_p (void)
12173 Lisp_Object vlist;
12175 for (vlist = Voverlay_arrow_variable_list;
12176 CONSP (vlist);
12177 vlist = XCDR (vlist))
12179 Lisp_Object var = XCAR (vlist);
12180 Lisp_Object val;
12182 if (!SYMBOLP (var))
12183 continue;
12184 val = find_symbol_value (var);
12185 if (MARKERP (val)
12186 && current_buffer == XMARKER (val)->buffer)
12187 return 1;
12189 return 0;
12193 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12194 has changed. */
12196 static int
12197 overlay_arrows_changed_p (void)
12199 Lisp_Object vlist;
12201 for (vlist = Voverlay_arrow_variable_list;
12202 CONSP (vlist);
12203 vlist = XCDR (vlist))
12205 Lisp_Object var = XCAR (vlist);
12206 Lisp_Object val, pstr;
12208 if (!SYMBOLP (var))
12209 continue;
12210 val = find_symbol_value (var);
12211 if (!MARKERP (val))
12212 continue;
12213 if (! EQ (COERCE_MARKER (val),
12214 Fget (var, Qlast_arrow_position))
12215 || ! (pstr = overlay_arrow_string_or_property (var),
12216 EQ (pstr, Fget (var, Qlast_arrow_string))))
12217 return 1;
12219 return 0;
12222 /* Mark overlay arrows to be updated on next redisplay. */
12224 static void
12225 update_overlay_arrows (int up_to_date)
12227 Lisp_Object vlist;
12229 for (vlist = Voverlay_arrow_variable_list;
12230 CONSP (vlist);
12231 vlist = XCDR (vlist))
12233 Lisp_Object var = XCAR (vlist);
12235 if (!SYMBOLP (var))
12236 continue;
12238 if (up_to_date > 0)
12240 Lisp_Object val = find_symbol_value (var);
12241 Fput (var, Qlast_arrow_position,
12242 COERCE_MARKER (val));
12243 Fput (var, Qlast_arrow_string,
12244 overlay_arrow_string_or_property (var));
12246 else if (up_to_date < 0
12247 || !NILP (Fget (var, Qlast_arrow_position)))
12249 Fput (var, Qlast_arrow_position, Qt);
12250 Fput (var, Qlast_arrow_string, Qt);
12256 /* Return overlay arrow string to display at row.
12257 Return integer (bitmap number) for arrow bitmap in left fringe.
12258 Return nil if no overlay arrow. */
12260 static Lisp_Object
12261 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12263 Lisp_Object vlist;
12265 for (vlist = Voverlay_arrow_variable_list;
12266 CONSP (vlist);
12267 vlist = XCDR (vlist))
12269 Lisp_Object var = XCAR (vlist);
12270 Lisp_Object val;
12272 if (!SYMBOLP (var))
12273 continue;
12275 val = find_symbol_value (var);
12277 if (MARKERP (val)
12278 && current_buffer == XMARKER (val)->buffer
12279 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12281 if (FRAME_WINDOW_P (it->f)
12282 /* FIXME: if ROW->reversed_p is set, this should test
12283 the right fringe, not the left one. */
12284 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12286 #ifdef HAVE_WINDOW_SYSTEM
12287 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12289 int fringe_bitmap;
12290 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12291 return make_number (fringe_bitmap);
12293 #endif
12294 return make_number (-1); /* Use default arrow bitmap */
12296 return overlay_arrow_string_or_property (var);
12300 return Qnil;
12303 /* Return 1 if point moved out of or into a composition. Otherwise
12304 return 0. PREV_BUF and PREV_PT are the last point buffer and
12305 position. BUF and PT are the current point buffer and position. */
12307 static int
12308 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12309 struct buffer *buf, EMACS_INT pt)
12311 EMACS_INT start, end;
12312 Lisp_Object prop;
12313 Lisp_Object buffer;
12315 XSETBUFFER (buffer, buf);
12316 /* Check a composition at the last point if point moved within the
12317 same buffer. */
12318 if (prev_buf == buf)
12320 if (prev_pt == pt)
12321 /* Point didn't move. */
12322 return 0;
12324 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12325 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12326 && COMPOSITION_VALID_P (start, end, prop)
12327 && start < prev_pt && end > prev_pt)
12328 /* The last point was within the composition. Return 1 iff
12329 point moved out of the composition. */
12330 return (pt <= start || pt >= end);
12333 /* Check a composition at the current point. */
12334 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12335 && find_composition (pt, -1, &start, &end, &prop, buffer)
12336 && COMPOSITION_VALID_P (start, end, prop)
12337 && start < pt && end > pt);
12341 /* Reconsider the setting of B->clip_changed which is displayed
12342 in window W. */
12344 static inline void
12345 reconsider_clip_changes (struct window *w, struct buffer *b)
12347 if (b->clip_changed
12348 && !NILP (w->window_end_valid)
12349 && w->current_matrix->buffer == b
12350 && w->current_matrix->zv == BUF_ZV (b)
12351 && w->current_matrix->begv == BUF_BEGV (b))
12352 b->clip_changed = 0;
12354 /* If display wasn't paused, and W is not a tool bar window, see if
12355 point has been moved into or out of a composition. In that case,
12356 we set b->clip_changed to 1 to force updating the screen. If
12357 b->clip_changed has already been set to 1, we can skip this
12358 check. */
12359 if (!b->clip_changed
12360 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12362 EMACS_INT pt;
12364 if (w == XWINDOW (selected_window))
12365 pt = PT;
12366 else
12367 pt = marker_position (w->pointm);
12369 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12370 || pt != XINT (w->last_point))
12371 && check_point_in_composition (w->current_matrix->buffer,
12372 XINT (w->last_point),
12373 XBUFFER (w->buffer), pt))
12374 b->clip_changed = 1;
12379 /* Select FRAME to forward the values of frame-local variables into C
12380 variables so that the redisplay routines can access those values
12381 directly. */
12383 static void
12384 select_frame_for_redisplay (Lisp_Object frame)
12386 Lisp_Object tail, tem;
12387 Lisp_Object old = selected_frame;
12388 struct Lisp_Symbol *sym;
12390 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12392 selected_frame = frame;
12394 do {
12395 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12396 if (CONSP (XCAR (tail))
12397 && (tem = XCAR (XCAR (tail)),
12398 SYMBOLP (tem))
12399 && (sym = indirect_variable (XSYMBOL (tem)),
12400 sym->redirect == SYMBOL_LOCALIZED)
12401 && sym->val.blv->frame_local)
12402 /* Use find_symbol_value rather than Fsymbol_value
12403 to avoid an error if it is void. */
12404 find_symbol_value (tem);
12405 } while (!EQ (frame, old) && (frame = old, 1));
12409 #define STOP_POLLING \
12410 do { if (! polling_stopped_here) stop_polling (); \
12411 polling_stopped_here = 1; } while (0)
12413 #define RESUME_POLLING \
12414 do { if (polling_stopped_here) start_polling (); \
12415 polling_stopped_here = 0; } while (0)
12418 /* Perhaps in the future avoid recentering windows if it
12419 is not necessary; currently that causes some problems. */
12421 static void
12422 redisplay_internal (void)
12424 struct window *w = XWINDOW (selected_window);
12425 struct window *sw;
12426 struct frame *fr;
12427 int pending;
12428 int must_finish = 0;
12429 struct text_pos tlbufpos, tlendpos;
12430 int number_of_visible_frames;
12431 int count, count1;
12432 struct frame *sf;
12433 int polling_stopped_here = 0;
12434 Lisp_Object old_frame = selected_frame;
12436 /* Non-zero means redisplay has to consider all windows on all
12437 frames. Zero means, only selected_window is considered. */
12438 int consider_all_windows_p;
12440 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12442 /* No redisplay if running in batch mode or frame is not yet fully
12443 initialized, or redisplay is explicitly turned off by setting
12444 Vinhibit_redisplay. */
12445 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12446 || !NILP (Vinhibit_redisplay))
12447 return;
12449 /* Don't examine these until after testing Vinhibit_redisplay.
12450 When Emacs is shutting down, perhaps because its connection to
12451 X has dropped, we should not look at them at all. */
12452 fr = XFRAME (w->frame);
12453 sf = SELECTED_FRAME ();
12455 if (!fr->glyphs_initialized_p)
12456 return;
12458 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12459 if (popup_activated ())
12460 return;
12461 #endif
12463 /* I don't think this happens but let's be paranoid. */
12464 if (redisplaying_p)
12465 return;
12467 /* Record a function that resets redisplaying_p to its old value
12468 when we leave this function. */
12469 count = SPECPDL_INDEX ();
12470 record_unwind_protect (unwind_redisplay,
12471 Fcons (make_number (redisplaying_p), selected_frame));
12472 ++redisplaying_p;
12473 specbind (Qinhibit_free_realized_faces, Qnil);
12476 Lisp_Object tail, frame;
12478 FOR_EACH_FRAME (tail, frame)
12480 struct frame *f = XFRAME (frame);
12481 f->already_hscrolled_p = 0;
12485 retry:
12486 /* Remember the currently selected window. */
12487 sw = w;
12489 if (!EQ (old_frame, selected_frame)
12490 && FRAME_LIVE_P (XFRAME (old_frame)))
12491 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12492 selected_frame and selected_window to be temporarily out-of-sync so
12493 when we come back here via `goto retry', we need to resync because we
12494 may need to run Elisp code (via prepare_menu_bars). */
12495 select_frame_for_redisplay (old_frame);
12497 pending = 0;
12498 reconsider_clip_changes (w, current_buffer);
12499 last_escape_glyph_frame = NULL;
12500 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12501 last_glyphless_glyph_frame = NULL;
12502 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12504 /* If new fonts have been loaded that make a glyph matrix adjustment
12505 necessary, do it. */
12506 if (fonts_changed_p)
12508 adjust_glyphs (NULL);
12509 ++windows_or_buffers_changed;
12510 fonts_changed_p = 0;
12513 /* If face_change_count is non-zero, init_iterator will free all
12514 realized faces, which includes the faces referenced from current
12515 matrices. So, we can't reuse current matrices in this case. */
12516 if (face_change_count)
12517 ++windows_or_buffers_changed;
12519 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12520 && FRAME_TTY (sf)->previous_frame != sf)
12522 /* Since frames on a single ASCII terminal share the same
12523 display area, displaying a different frame means redisplay
12524 the whole thing. */
12525 windows_or_buffers_changed++;
12526 SET_FRAME_GARBAGED (sf);
12527 #ifndef DOS_NT
12528 set_tty_color_mode (FRAME_TTY (sf), sf);
12529 #endif
12530 FRAME_TTY (sf)->previous_frame = sf;
12533 /* Set the visible flags for all frames. Do this before checking
12534 for resized or garbaged frames; they want to know if their frames
12535 are visible. See the comment in frame.h for
12536 FRAME_SAMPLE_VISIBILITY. */
12538 Lisp_Object tail, frame;
12540 number_of_visible_frames = 0;
12542 FOR_EACH_FRAME (tail, frame)
12544 struct frame *f = XFRAME (frame);
12546 FRAME_SAMPLE_VISIBILITY (f);
12547 if (FRAME_VISIBLE_P (f))
12548 ++number_of_visible_frames;
12549 clear_desired_matrices (f);
12553 /* Notice any pending interrupt request to change frame size. */
12554 do_pending_window_change (1);
12556 /* do_pending_window_change could change the selected_window due to
12557 frame resizing which makes the selected window too small. */
12558 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12560 sw = w;
12561 reconsider_clip_changes (w, current_buffer);
12564 /* Clear frames marked as garbaged. */
12565 if (frame_garbaged)
12566 clear_garbaged_frames ();
12568 /* Build menubar and tool-bar items. */
12569 if (NILP (Vmemory_full))
12570 prepare_menu_bars ();
12572 if (windows_or_buffers_changed)
12573 update_mode_lines++;
12575 /* Detect case that we need to write or remove a star in the mode line. */
12576 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12578 w->update_mode_line = Qt;
12579 if (buffer_shared > 1)
12580 update_mode_lines++;
12583 /* Avoid invocation of point motion hooks by `current_column' below. */
12584 count1 = SPECPDL_INDEX ();
12585 specbind (Qinhibit_point_motion_hooks, Qt);
12587 /* If %c is in the mode line, update it if needed. */
12588 if (!NILP (w->column_number_displayed)
12589 /* This alternative quickly identifies a common case
12590 where no change is needed. */
12591 && !(PT == XFASTINT (w->last_point)
12592 && XFASTINT (w->last_modified) >= MODIFF
12593 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12594 && (XFASTINT (w->column_number_displayed) != current_column ()))
12595 w->update_mode_line = Qt;
12597 unbind_to (count1, Qnil);
12599 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12601 /* The variable buffer_shared is set in redisplay_window and
12602 indicates that we redisplay a buffer in different windows. See
12603 there. */
12604 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12605 || cursor_type_changed);
12607 /* If specs for an arrow have changed, do thorough redisplay
12608 to ensure we remove any arrow that should no longer exist. */
12609 if (overlay_arrows_changed_p ())
12610 consider_all_windows_p = windows_or_buffers_changed = 1;
12612 /* Normally the message* functions will have already displayed and
12613 updated the echo area, but the frame may have been trashed, or
12614 the update may have been preempted, so display the echo area
12615 again here. Checking message_cleared_p captures the case that
12616 the echo area should be cleared. */
12617 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12618 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12619 || (message_cleared_p
12620 && minibuf_level == 0
12621 /* If the mini-window is currently selected, this means the
12622 echo-area doesn't show through. */
12623 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12625 int window_height_changed_p = echo_area_display (0);
12626 must_finish = 1;
12628 /* If we don't display the current message, don't clear the
12629 message_cleared_p flag, because, if we did, we wouldn't clear
12630 the echo area in the next redisplay which doesn't preserve
12631 the echo area. */
12632 if (!display_last_displayed_message_p)
12633 message_cleared_p = 0;
12635 if (fonts_changed_p)
12636 goto retry;
12637 else if (window_height_changed_p)
12639 consider_all_windows_p = 1;
12640 ++update_mode_lines;
12641 ++windows_or_buffers_changed;
12643 /* If window configuration was changed, frames may have been
12644 marked garbaged. Clear them or we will experience
12645 surprises wrt scrolling. */
12646 if (frame_garbaged)
12647 clear_garbaged_frames ();
12650 else if (EQ (selected_window, minibuf_window)
12651 && (current_buffer->clip_changed
12652 || XFASTINT (w->last_modified) < MODIFF
12653 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12654 && resize_mini_window (w, 0))
12656 /* Resized active mini-window to fit the size of what it is
12657 showing if its contents might have changed. */
12658 must_finish = 1;
12659 /* FIXME: this causes all frames to be updated, which seems unnecessary
12660 since only the current frame needs to be considered. This function needs
12661 to be rewritten with two variables, consider_all_windows and
12662 consider_all_frames. */
12663 consider_all_windows_p = 1;
12664 ++windows_or_buffers_changed;
12665 ++update_mode_lines;
12667 /* If window configuration was changed, frames may have been
12668 marked garbaged. Clear them or we will experience
12669 surprises wrt scrolling. */
12670 if (frame_garbaged)
12671 clear_garbaged_frames ();
12675 /* If showing the region, and mark has changed, we must redisplay
12676 the whole window. The assignment to this_line_start_pos prevents
12677 the optimization directly below this if-statement. */
12678 if (((!NILP (Vtransient_mark_mode)
12679 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12680 != !NILP (w->region_showing))
12681 || (!NILP (w->region_showing)
12682 && !EQ (w->region_showing,
12683 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12684 CHARPOS (this_line_start_pos) = 0;
12686 /* Optimize the case that only the line containing the cursor in the
12687 selected window has changed. Variables starting with this_ are
12688 set in display_line and record information about the line
12689 containing the cursor. */
12690 tlbufpos = this_line_start_pos;
12691 tlendpos = this_line_end_pos;
12692 if (!consider_all_windows_p
12693 && CHARPOS (tlbufpos) > 0
12694 && NILP (w->update_mode_line)
12695 && !current_buffer->clip_changed
12696 && !current_buffer->prevent_redisplay_optimizations_p
12697 && FRAME_VISIBLE_P (XFRAME (w->frame))
12698 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12699 /* Make sure recorded data applies to current buffer, etc. */
12700 && this_line_buffer == current_buffer
12701 && current_buffer == XBUFFER (w->buffer)
12702 && NILP (w->force_start)
12703 && NILP (w->optional_new_start)
12704 /* Point must be on the line that we have info recorded about. */
12705 && PT >= CHARPOS (tlbufpos)
12706 && PT <= Z - CHARPOS (tlendpos)
12707 /* All text outside that line, including its final newline,
12708 must be unchanged. */
12709 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12710 CHARPOS (tlendpos)))
12712 if (CHARPOS (tlbufpos) > BEGV
12713 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12714 && (CHARPOS (tlbufpos) == ZV
12715 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12716 /* Former continuation line has disappeared by becoming empty. */
12717 goto cancel;
12718 else if (XFASTINT (w->last_modified) < MODIFF
12719 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12720 || MINI_WINDOW_P (w))
12722 /* We have to handle the case of continuation around a
12723 wide-column character (see the comment in indent.c around
12724 line 1340).
12726 For instance, in the following case:
12728 -------- Insert --------
12729 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12730 J_I_ ==> J_I_ `^^' are cursors.
12731 ^^ ^^
12732 -------- --------
12734 As we have to redraw the line above, we cannot use this
12735 optimization. */
12737 struct it it;
12738 int line_height_before = this_line_pixel_height;
12740 /* Note that start_display will handle the case that the
12741 line starting at tlbufpos is a continuation line. */
12742 start_display (&it, w, tlbufpos);
12744 /* Implementation note: It this still necessary? */
12745 if (it.current_x != this_line_start_x)
12746 goto cancel;
12748 TRACE ((stderr, "trying display optimization 1\n"));
12749 w->cursor.vpos = -1;
12750 overlay_arrow_seen = 0;
12751 it.vpos = this_line_vpos;
12752 it.current_y = this_line_y;
12753 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12754 display_line (&it);
12756 /* If line contains point, is not continued,
12757 and ends at same distance from eob as before, we win. */
12758 if (w->cursor.vpos >= 0
12759 /* Line is not continued, otherwise this_line_start_pos
12760 would have been set to 0 in display_line. */
12761 && CHARPOS (this_line_start_pos)
12762 /* Line ends as before. */
12763 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12764 /* Line has same height as before. Otherwise other lines
12765 would have to be shifted up or down. */
12766 && this_line_pixel_height == line_height_before)
12768 /* If this is not the window's last line, we must adjust
12769 the charstarts of the lines below. */
12770 if (it.current_y < it.last_visible_y)
12772 struct glyph_row *row
12773 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12774 EMACS_INT delta, delta_bytes;
12776 /* We used to distinguish between two cases here,
12777 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12778 when the line ends in a newline or the end of the
12779 buffer's accessible portion. But both cases did
12780 the same, so they were collapsed. */
12781 delta = (Z
12782 - CHARPOS (tlendpos)
12783 - MATRIX_ROW_START_CHARPOS (row));
12784 delta_bytes = (Z_BYTE
12785 - BYTEPOS (tlendpos)
12786 - MATRIX_ROW_START_BYTEPOS (row));
12788 increment_matrix_positions (w->current_matrix,
12789 this_line_vpos + 1,
12790 w->current_matrix->nrows,
12791 delta, delta_bytes);
12794 /* If this row displays text now but previously didn't,
12795 or vice versa, w->window_end_vpos may have to be
12796 adjusted. */
12797 if ((it.glyph_row - 1)->displays_text_p)
12799 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12800 XSETINT (w->window_end_vpos, this_line_vpos);
12802 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12803 && this_line_vpos > 0)
12804 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12805 w->window_end_valid = Qnil;
12807 /* Update hint: No need to try to scroll in update_window. */
12808 w->desired_matrix->no_scrolling_p = 1;
12810 #if GLYPH_DEBUG
12811 *w->desired_matrix->method = 0;
12812 debug_method_add (w, "optimization 1");
12813 #endif
12814 #if HAVE_XWIDGETS
12815 //debug optimization movement issue
12816 //w->desired_matrix->no_scrolling_p = 1;
12817 //*w->desired_matrix->method = 0;
12818 //debug_method_add (w, "optimization 1");
12819 #endif
12821 #ifdef HAVE_WINDOW_SYSTEM
12822 update_window_fringes (w, 0);
12823 #endif
12824 goto update;
12826 else
12827 goto cancel;
12829 else if (/* Cursor position hasn't changed. */
12830 PT == XFASTINT (w->last_point)
12831 /* Make sure the cursor was last displayed
12832 in this window. Otherwise we have to reposition it. */
12833 && 0 <= w->cursor.vpos
12834 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12836 if (!must_finish)
12838 do_pending_window_change (1);
12839 /* If selected_window changed, redisplay again. */
12840 if (WINDOWP (selected_window)
12841 && (w = XWINDOW (selected_window)) != sw)
12842 goto retry;
12844 /* We used to always goto end_of_redisplay here, but this
12845 isn't enough if we have a blinking cursor. */
12846 if (w->cursor_off_p == w->last_cursor_off_p)
12847 goto end_of_redisplay;
12849 goto update;
12851 /* If highlighting the region, or if the cursor is in the echo area,
12852 then we can't just move the cursor. */
12853 else if (! (!NILP (Vtransient_mark_mode)
12854 && !NILP (BVAR (current_buffer, mark_active)))
12855 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12856 || highlight_nonselected_windows)
12857 && NILP (w->region_showing)
12858 && NILP (Vshow_trailing_whitespace)
12859 && !cursor_in_echo_area)
12861 struct it it;
12862 struct glyph_row *row;
12864 /* Skip from tlbufpos to PT and see where it is. Note that
12865 PT may be in invisible text. If so, we will end at the
12866 next visible position. */
12867 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12868 NULL, DEFAULT_FACE_ID);
12869 it.current_x = this_line_start_x;
12870 it.current_y = this_line_y;
12871 it.vpos = this_line_vpos;
12873 /* The call to move_it_to stops in front of PT, but
12874 moves over before-strings. */
12875 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12877 if (it.vpos == this_line_vpos
12878 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12879 row->enabled_p))
12881 xassert (this_line_vpos == it.vpos);
12882 xassert (this_line_y == it.current_y);
12883 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12884 #if GLYPH_DEBUG
12885 *w->desired_matrix->method = 0;
12886 debug_method_add (w, "optimization 3");
12887 #endif
12888 goto update;
12890 else
12891 goto cancel;
12894 cancel:
12895 /* Text changed drastically or point moved off of line. */
12896 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12899 CHARPOS (this_line_start_pos) = 0;
12900 consider_all_windows_p |= buffer_shared > 1;
12901 ++clear_face_cache_count;
12902 #ifdef HAVE_WINDOW_SYSTEM
12903 ++clear_image_cache_count;
12904 #endif
12906 /* Build desired matrices, and update the display. If
12907 consider_all_windows_p is non-zero, do it for all windows on all
12908 frames. Otherwise do it for selected_window, only. */
12910 if (consider_all_windows_p)
12912 Lisp_Object tail, frame;
12914 FOR_EACH_FRAME (tail, frame)
12915 XFRAME (frame)->updated_p = 0;
12917 /* Recompute # windows showing selected buffer. This will be
12918 incremented each time such a window is displayed. */
12919 buffer_shared = 0;
12921 FOR_EACH_FRAME (tail, frame)
12923 struct frame *f = XFRAME (frame);
12925 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12927 if (! EQ (frame, selected_frame))
12928 /* Select the frame, for the sake of frame-local
12929 variables. */
12930 select_frame_for_redisplay (frame);
12932 /* Mark all the scroll bars to be removed; we'll redeem
12933 the ones we want when we redisplay their windows. */
12934 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12935 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12937 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12938 redisplay_windows (FRAME_ROOT_WINDOW (f));
12940 /* The X error handler may have deleted that frame. */
12941 if (!FRAME_LIVE_P (f))
12942 continue;
12944 /* Any scroll bars which redisplay_windows should have
12945 nuked should now go away. */
12946 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12947 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12949 /* If fonts changed, display again. */
12950 /* ??? rms: I suspect it is a mistake to jump all the way
12951 back to retry here. It should just retry this frame. */
12952 if (fonts_changed_p)
12953 goto retry;
12955 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12957 /* See if we have to hscroll. */
12958 if (!f->already_hscrolled_p)
12960 f->already_hscrolled_p = 1;
12961 if (hscroll_windows (f->root_window))
12962 goto retry;
12965 /* Prevent various kinds of signals during display
12966 update. stdio is not robust about handling
12967 signals, which can cause an apparent I/O
12968 error. */
12969 if (interrupt_input)
12970 unrequest_sigio ();
12971 STOP_POLLING;
12973 /* Update the display. */
12974 set_window_update_flags (XWINDOW (f->root_window), 1);
12975 pending |= update_frame (f, 0, 0);
12976 f->updated_p = 1;
12981 if (!EQ (old_frame, selected_frame)
12982 && FRAME_LIVE_P (XFRAME (old_frame)))
12983 /* We played a bit fast-and-loose above and allowed selected_frame
12984 and selected_window to be temporarily out-of-sync but let's make
12985 sure this stays contained. */
12986 select_frame_for_redisplay (old_frame);
12987 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12989 if (!pending)
12991 /* Do the mark_window_display_accurate after all windows have
12992 been redisplayed because this call resets flags in buffers
12993 which are needed for proper redisplay. */
12994 FOR_EACH_FRAME (tail, frame)
12996 struct frame *f = XFRAME (frame);
12997 if (f->updated_p)
12999 mark_window_display_accurate (f->root_window, 1);
13000 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13001 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13006 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13008 Lisp_Object mini_window;
13009 struct frame *mini_frame;
13011 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13012 /* Use list_of_error, not Qerror, so that
13013 we catch only errors and don't run the debugger. */
13014 internal_condition_case_1 (redisplay_window_1, selected_window,
13015 list_of_error,
13016 redisplay_window_error);
13018 /* Compare desired and current matrices, perform output. */
13020 update:
13021 /* If fonts changed, display again. */
13022 if (fonts_changed_p)
13023 goto retry;
13025 /* Prevent various kinds of signals during display update.
13026 stdio is not robust about handling signals,
13027 which can cause an apparent I/O error. */
13028 if (interrupt_input)
13029 unrequest_sigio ();
13030 STOP_POLLING;
13032 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13034 if (hscroll_windows (selected_window))
13035 goto retry;
13037 XWINDOW (selected_window)->must_be_updated_p = 1;
13038 pending = update_frame (sf, 0, 0);
13041 /* We may have called echo_area_display at the top of this
13042 function. If the echo area is on another frame, that may
13043 have put text on a frame other than the selected one, so the
13044 above call to update_frame would not have caught it. Catch
13045 it here. */
13046 mini_window = FRAME_MINIBUF_WINDOW (sf);
13047 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13049 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13051 XWINDOW (mini_window)->must_be_updated_p = 1;
13052 pending |= update_frame (mini_frame, 0, 0);
13053 if (!pending && hscroll_windows (mini_window))
13054 goto retry;
13058 /* If display was paused because of pending input, make sure we do a
13059 thorough update the next time. */
13060 if (pending)
13062 /* Prevent the optimization at the beginning of
13063 redisplay_internal that tries a single-line update of the
13064 line containing the cursor in the selected window. */
13065 CHARPOS (this_line_start_pos) = 0;
13067 /* Let the overlay arrow be updated the next time. */
13068 update_overlay_arrows (0);
13070 /* If we pause after scrolling, some rows in the current
13071 matrices of some windows are not valid. */
13072 if (!WINDOW_FULL_WIDTH_P (w)
13073 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13074 update_mode_lines = 1;
13076 else
13078 if (!consider_all_windows_p)
13080 /* This has already been done above if
13081 consider_all_windows_p is set. */
13082 mark_window_display_accurate_1 (w, 1);
13084 /* Say overlay arrows are up to date. */
13085 update_overlay_arrows (1);
13087 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13088 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13091 update_mode_lines = 0;
13092 windows_or_buffers_changed = 0;
13093 cursor_type_changed = 0;
13096 /* Start SIGIO interrupts coming again. Having them off during the
13097 code above makes it less likely one will discard output, but not
13098 impossible, since there might be stuff in the system buffer here.
13099 But it is much hairier to try to do anything about that. */
13100 if (interrupt_input)
13101 request_sigio ();
13102 RESUME_POLLING;
13104 /* If a frame has become visible which was not before, redisplay
13105 again, so that we display it. Expose events for such a frame
13106 (which it gets when becoming visible) don't call the parts of
13107 redisplay constructing glyphs, so simply exposing a frame won't
13108 display anything in this case. So, we have to display these
13109 frames here explicitly. */
13110 if (!pending)
13112 Lisp_Object tail, frame;
13113 int new_count = 0;
13115 FOR_EACH_FRAME (tail, frame)
13117 int this_is_visible = 0;
13119 if (XFRAME (frame)->visible)
13120 this_is_visible = 1;
13121 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13122 if (XFRAME (frame)->visible)
13123 this_is_visible = 1;
13125 if (this_is_visible)
13126 new_count++;
13129 if (new_count != number_of_visible_frames)
13130 windows_or_buffers_changed++;
13133 /* Change frame size now if a change is pending. */
13134 do_pending_window_change (1);
13136 /* If we just did a pending size change, or have additional
13137 visible frames, or selected_window changed, redisplay again. */
13138 if ((windows_or_buffers_changed && !pending)
13139 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13140 goto retry;
13142 /* Clear the face and image caches.
13144 We used to do this only if consider_all_windows_p. But the cache
13145 needs to be cleared if a timer creates images in the current
13146 buffer (e.g. the test case in Bug#6230). */
13148 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13150 clear_face_cache (0);
13151 clear_face_cache_count = 0;
13154 #ifdef HAVE_WINDOW_SYSTEM
13155 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13157 clear_image_caches (Qnil);
13158 clear_image_cache_count = 0;
13160 #endif /* HAVE_WINDOW_SYSTEM */
13162 end_of_redisplay:
13163 unbind_to (count, Qnil);
13164 RESUME_POLLING;
13168 /* Redisplay, but leave alone any recent echo area message unless
13169 another message has been requested in its place.
13171 This is useful in situations where you need to redisplay but no
13172 user action has occurred, making it inappropriate for the message
13173 area to be cleared. See tracking_off and
13174 wait_reading_process_output for examples of these situations.
13176 FROM_WHERE is an integer saying from where this function was
13177 called. This is useful for debugging. */
13179 void
13180 redisplay_preserve_echo_area (int from_where)
13182 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13184 if (!NILP (echo_area_buffer[1]))
13186 /* We have a previously displayed message, but no current
13187 message. Redisplay the previous message. */
13188 display_last_displayed_message_p = 1;
13189 redisplay_internal ();
13190 display_last_displayed_message_p = 0;
13192 else
13193 redisplay_internal ();
13195 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13196 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13197 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13201 /* Function registered with record_unwind_protect in
13202 redisplay_internal. Reset redisplaying_p to the value it had
13203 before redisplay_internal was called, and clear
13204 prevent_freeing_realized_faces_p. It also selects the previously
13205 selected frame, unless it has been deleted (by an X connection
13206 failure during redisplay, for example). */
13208 static Lisp_Object
13209 unwind_redisplay (Lisp_Object val)
13211 Lisp_Object old_redisplaying_p, old_frame;
13213 old_redisplaying_p = XCAR (val);
13214 redisplaying_p = XFASTINT (old_redisplaying_p);
13215 old_frame = XCDR (val);
13216 if (! EQ (old_frame, selected_frame)
13217 && FRAME_LIVE_P (XFRAME (old_frame)))
13218 select_frame_for_redisplay (old_frame);
13219 return Qnil;
13223 /* Mark the display of window W as accurate or inaccurate. If
13224 ACCURATE_P is non-zero mark display of W as accurate. If
13225 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13226 redisplay_internal is called. */
13228 static void
13229 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13231 if (BUFFERP (w->buffer))
13233 struct buffer *b = XBUFFER (w->buffer);
13235 w->last_modified
13236 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13237 w->last_overlay_modified
13238 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13239 w->last_had_star
13240 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13242 if (accurate_p)
13244 b->clip_changed = 0;
13245 b->prevent_redisplay_optimizations_p = 0;
13247 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13248 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13249 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13250 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13252 w->current_matrix->buffer = b;
13253 w->current_matrix->begv = BUF_BEGV (b);
13254 w->current_matrix->zv = BUF_ZV (b);
13256 w->last_cursor = w->cursor;
13257 w->last_cursor_off_p = w->cursor_off_p;
13259 if (w == XWINDOW (selected_window))
13260 w->last_point = make_number (BUF_PT (b));
13261 else
13262 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13266 if (accurate_p)
13268 w->window_end_valid = w->buffer;
13269 w->update_mode_line = Qnil;
13274 /* Mark the display of windows in the window tree rooted at WINDOW as
13275 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13276 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13277 be redisplayed the next time redisplay_internal is called. */
13279 void
13280 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13282 struct window *w;
13284 for (; !NILP (window); window = w->next)
13286 w = XWINDOW (window);
13287 mark_window_display_accurate_1 (w, accurate_p);
13289 if (!NILP (w->vchild))
13290 mark_window_display_accurate (w->vchild, accurate_p);
13291 if (!NILP (w->hchild))
13292 mark_window_display_accurate (w->hchild, accurate_p);
13295 if (accurate_p)
13297 update_overlay_arrows (1);
13299 else
13301 /* Force a thorough redisplay the next time by setting
13302 last_arrow_position and last_arrow_string to t, which is
13303 unequal to any useful value of Voverlay_arrow_... */
13304 update_overlay_arrows (-1);
13309 /* Return value in display table DP (Lisp_Char_Table *) for character
13310 C. Since a display table doesn't have any parent, we don't have to
13311 follow parent. Do not call this function directly but use the
13312 macro DISP_CHAR_VECTOR. */
13314 Lisp_Object
13315 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13317 Lisp_Object val;
13319 if (ASCII_CHAR_P (c))
13321 val = dp->ascii;
13322 if (SUB_CHAR_TABLE_P (val))
13323 val = XSUB_CHAR_TABLE (val)->contents[c];
13325 else
13327 Lisp_Object table;
13329 XSETCHAR_TABLE (table, dp);
13330 val = char_table_ref (table, c);
13332 if (NILP (val))
13333 val = dp->defalt;
13334 return val;
13339 /***********************************************************************
13340 Window Redisplay
13341 ***********************************************************************/
13343 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13345 static void
13346 redisplay_windows (Lisp_Object window)
13348 while (!NILP (window))
13350 struct window *w = XWINDOW (window);
13352 if (!NILP (w->hchild))
13353 redisplay_windows (w->hchild);
13354 else if (!NILP (w->vchild))
13355 redisplay_windows (w->vchild);
13356 else if (!NILP (w->buffer))
13358 displayed_buffer = XBUFFER (w->buffer);
13359 /* Use list_of_error, not Qerror, so that
13360 we catch only errors and don't run the debugger. */
13361 internal_condition_case_1 (redisplay_window_0, window,
13362 list_of_error,
13363 redisplay_window_error);
13366 window = w->next;
13370 static Lisp_Object
13371 redisplay_window_error (Lisp_Object ignore)
13373 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13374 return Qnil;
13377 static Lisp_Object
13378 redisplay_window_0 (Lisp_Object window)
13380 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13381 redisplay_window (window, 0);
13382 return Qnil;
13385 static Lisp_Object
13386 redisplay_window_1 (Lisp_Object window)
13388 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13389 redisplay_window (window, 1);
13390 return Qnil;
13394 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13395 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13396 which positions recorded in ROW differ from current buffer
13397 positions.
13399 Return 0 if cursor is not on this row, 1 otherwise. */
13401 static int
13402 set_cursor_from_row (struct window *w, struct glyph_row *row,
13403 struct glyph_matrix *matrix,
13404 EMACS_INT delta, EMACS_INT delta_bytes,
13405 int dy, int dvpos)
13407 struct glyph *glyph = row->glyphs[TEXT_AREA];
13408 struct glyph *end = glyph + row->used[TEXT_AREA];
13409 struct glyph *cursor = NULL;
13410 /* The last known character position in row. */
13411 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13412 int x = row->x;
13413 EMACS_INT pt_old = PT - delta;
13414 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13415 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13416 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13417 /* A glyph beyond the edge of TEXT_AREA which we should never
13418 touch. */
13419 struct glyph *glyphs_end = end;
13420 /* Non-zero means we've found a match for cursor position, but that
13421 glyph has the avoid_cursor_p flag set. */
13422 int match_with_avoid_cursor = 0;
13423 /* Non-zero means we've seen at least one glyph that came from a
13424 display string. */
13425 int string_seen = 0;
13426 /* Largest and smalles buffer positions seen so far during scan of
13427 glyph row. */
13428 EMACS_INT bpos_max = pos_before;
13429 EMACS_INT bpos_min = pos_after;
13430 /* Last buffer position covered by an overlay string with an integer
13431 `cursor' property. */
13432 EMACS_INT bpos_covered = 0;
13433 /* Non-zero means the display string on which to display the cursor
13434 comes from a text property, not from an overlay. */
13435 int string_from_text_prop = 0;
13437 /* Skip over glyphs not having an object at the start and the end of
13438 the row. These are special glyphs like truncation marks on
13439 terminal frames. */
13440 if (row->displays_text_p)
13442 if (!row->reversed_p)
13444 while (glyph < end
13445 && INTEGERP (glyph->object)
13446 && glyph->charpos < 0)
13448 x += glyph->pixel_width;
13449 ++glyph;
13451 while (end > glyph
13452 && INTEGERP ((end - 1)->object)
13453 /* CHARPOS is zero for blanks and stretch glyphs
13454 inserted by extend_face_to_end_of_line. */
13455 && (end - 1)->charpos <= 0)
13456 --end;
13457 glyph_before = glyph - 1;
13458 glyph_after = end;
13460 else
13462 struct glyph *g;
13464 /* If the glyph row is reversed, we need to process it from back
13465 to front, so swap the edge pointers. */
13466 glyphs_end = end = glyph - 1;
13467 glyph += row->used[TEXT_AREA] - 1;
13469 while (glyph > end + 1
13470 && INTEGERP (glyph->object)
13471 && glyph->charpos < 0)
13473 --glyph;
13474 x -= glyph->pixel_width;
13476 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13477 --glyph;
13478 /* By default, in reversed rows we put the cursor on the
13479 rightmost (first in the reading order) glyph. */
13480 for (g = end + 1; g < glyph; g++)
13481 x += g->pixel_width;
13482 while (end < glyph
13483 && INTEGERP ((end + 1)->object)
13484 && (end + 1)->charpos <= 0)
13485 ++end;
13486 glyph_before = glyph + 1;
13487 glyph_after = end;
13490 else if (row->reversed_p)
13492 /* In R2L rows that don't display text, put the cursor on the
13493 rightmost glyph. Case in point: an empty last line that is
13494 part of an R2L paragraph. */
13495 cursor = end - 1;
13496 /* Avoid placing the cursor on the last glyph of the row, where
13497 on terminal frames we hold the vertical border between
13498 adjacent windows. */
13499 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13500 && !WINDOW_RIGHTMOST_P (w)
13501 && cursor == row->glyphs[LAST_AREA] - 1)
13502 cursor--;
13503 x = -1; /* will be computed below, at label compute_x */
13506 /* Step 1: Try to find the glyph whose character position
13507 corresponds to point. If that's not possible, find 2 glyphs
13508 whose character positions are the closest to point, one before
13509 point, the other after it. */
13510 if (!row->reversed_p)
13511 while (/* not marched to end of glyph row */
13512 glyph < end
13513 /* glyph was not inserted by redisplay for internal purposes */
13514 && !INTEGERP (glyph->object))
13516 if (BUFFERP (glyph->object))
13518 EMACS_INT dpos = glyph->charpos - pt_old;
13520 if (glyph->charpos > bpos_max)
13521 bpos_max = glyph->charpos;
13522 if (glyph->charpos < bpos_min)
13523 bpos_min = glyph->charpos;
13524 if (!glyph->avoid_cursor_p)
13526 /* If we hit point, we've found the glyph on which to
13527 display the cursor. */
13528 if (dpos == 0)
13530 match_with_avoid_cursor = 0;
13531 break;
13533 /* See if we've found a better approximation to
13534 POS_BEFORE or to POS_AFTER. Note that we want the
13535 first (leftmost) glyph of all those that are the
13536 closest from below, and the last (rightmost) of all
13537 those from above. */
13538 if (0 > dpos && dpos > pos_before - pt_old)
13540 pos_before = glyph->charpos;
13541 glyph_before = glyph;
13543 else if (0 < dpos && dpos <= pos_after - pt_old)
13545 pos_after = glyph->charpos;
13546 glyph_after = glyph;
13549 else if (dpos == 0)
13550 match_with_avoid_cursor = 1;
13552 else if (STRINGP (glyph->object))
13554 Lisp_Object chprop;
13555 EMACS_INT glyph_pos = glyph->charpos;
13557 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13558 glyph->object);
13559 if (INTEGERP (chprop))
13561 bpos_covered = bpos_max + XINT (chprop);
13562 /* If the `cursor' property covers buffer positions up
13563 to and including point, we should display cursor on
13564 this glyph. Note that overlays and text properties
13565 with string values stop bidi reordering, so every
13566 buffer position to the left of the string is always
13567 smaller than any position to the right of the
13568 string. Therefore, if a `cursor' property on one
13569 of the string's characters has an integer value, we
13570 will break out of the loop below _before_ we get to
13571 the position match above. IOW, integer values of
13572 the `cursor' property override the "exact match for
13573 point" strategy of positioning the cursor. */
13574 /* Implementation note: bpos_max == pt_old when, e.g.,
13575 we are in an empty line, where bpos_max is set to
13576 MATRIX_ROW_START_CHARPOS, see above. */
13577 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13579 cursor = glyph;
13580 break;
13584 string_seen = 1;
13586 x += glyph->pixel_width;
13587 ++glyph;
13589 else if (glyph > end) /* row is reversed */
13590 while (!INTEGERP (glyph->object))
13592 if (BUFFERP (glyph->object))
13594 EMACS_INT dpos = glyph->charpos - pt_old;
13596 if (glyph->charpos > bpos_max)
13597 bpos_max = glyph->charpos;
13598 if (glyph->charpos < bpos_min)
13599 bpos_min = glyph->charpos;
13600 if (!glyph->avoid_cursor_p)
13602 if (dpos == 0)
13604 match_with_avoid_cursor = 0;
13605 break;
13607 if (0 > dpos && dpos > pos_before - pt_old)
13609 pos_before = glyph->charpos;
13610 glyph_before = glyph;
13612 else if (0 < dpos && dpos <= pos_after - pt_old)
13614 pos_after = glyph->charpos;
13615 glyph_after = glyph;
13618 else if (dpos == 0)
13619 match_with_avoid_cursor = 1;
13621 else if (STRINGP (glyph->object))
13623 Lisp_Object chprop;
13624 EMACS_INT glyph_pos = glyph->charpos;
13626 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13627 glyph->object);
13628 if (INTEGERP (chprop))
13630 bpos_covered = bpos_max + XINT (chprop);
13631 /* If the `cursor' property covers buffer positions up
13632 to and including point, we should display cursor on
13633 this glyph. */
13634 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13636 cursor = glyph;
13637 break;
13640 string_seen = 1;
13642 --glyph;
13643 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13645 x--; /* can't use any pixel_width */
13646 break;
13648 x -= glyph->pixel_width;
13651 /* Step 2: If we didn't find an exact match for point, we need to
13652 look for a proper place to put the cursor among glyphs between
13653 GLYPH_BEFORE and GLYPH_AFTER. */
13654 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13655 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13656 && bpos_covered < pt_old)
13658 /* An empty line has a single glyph whose OBJECT is zero and
13659 whose CHARPOS is the position of a newline on that line.
13660 Note that on a TTY, there are more glyphs after that, which
13661 were produced by extend_face_to_end_of_line, but their
13662 CHARPOS is zero or negative. */
13663 int empty_line_p =
13664 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13665 && INTEGERP (glyph->object) && glyph->charpos > 0;
13667 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13669 EMACS_INT ellipsis_pos;
13671 /* Scan back over the ellipsis glyphs. */
13672 if (!row->reversed_p)
13674 ellipsis_pos = (glyph - 1)->charpos;
13675 while (glyph > row->glyphs[TEXT_AREA]
13676 && (glyph - 1)->charpos == ellipsis_pos)
13677 glyph--, x -= glyph->pixel_width;
13678 /* That loop always goes one position too far, including
13679 the glyph before the ellipsis. So scan forward over
13680 that one. */
13681 x += glyph->pixel_width;
13682 glyph++;
13684 else /* row is reversed */
13686 ellipsis_pos = (glyph + 1)->charpos;
13687 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13688 && (glyph + 1)->charpos == ellipsis_pos)
13689 glyph++, x += glyph->pixel_width;
13690 x -= glyph->pixel_width;
13691 glyph--;
13694 else if (match_with_avoid_cursor
13695 /* A truncated row may not include PT among its
13696 character positions. Setting the cursor inside the
13697 scroll margin will trigger recalculation of hscroll
13698 in hscroll_window_tree. */
13699 || (row->truncated_on_left_p && pt_old < bpos_min)
13700 || (row->truncated_on_right_p && pt_old > bpos_max)
13701 /* Zero-width characters produce no glyphs. */
13702 || (!string_seen
13703 && !empty_line_p
13704 && (row->reversed_p
13705 ? glyph_after > glyphs_end
13706 : glyph_after < glyphs_end)))
13708 cursor = glyph_after;
13709 x = -1;
13711 else if (string_seen)
13713 int incr = row->reversed_p ? -1 : +1;
13715 /* Need to find the glyph that came out of a string which is
13716 present at point. That glyph is somewhere between
13717 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13718 positioned between POS_BEFORE and POS_AFTER in the
13719 buffer. */
13720 struct glyph *start, *stop;
13721 EMACS_INT pos = pos_before;
13723 x = -1;
13725 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13726 correspond to POS_BEFORE and POS_AFTER, respectively. We
13727 need START and STOP in the order that corresponds to the
13728 row's direction as given by its reversed_p flag. If the
13729 directionality of characters between POS_BEFORE and
13730 POS_AFTER is the opposite of the row's base direction,
13731 these characters will have been reordered for display,
13732 and we need to reverse START and STOP. */
13733 if (!row->reversed_p)
13735 start = min (glyph_before, glyph_after);
13736 stop = max (glyph_before, glyph_after);
13738 else
13740 start = max (glyph_before, glyph_after);
13741 stop = min (glyph_before, glyph_after);
13743 for (glyph = start + incr;
13744 row->reversed_p ? glyph > stop : glyph < stop; )
13747 /* Any glyphs that come from the buffer are here because
13748 of bidi reordering. Skip them, and only pay
13749 attention to glyphs that came from some string. */
13750 if (STRINGP (glyph->object))
13752 Lisp_Object str;
13753 EMACS_INT tem;
13754 /* If the display property covers the newline, we
13755 need to search for it one position farther. */
13756 EMACS_INT lim = pos_after
13757 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13759 string_from_text_prop = 0;
13760 str = glyph->object;
13761 tem = string_buffer_position_lim (str, pos, lim, 0);
13762 if (tem == 0 /* from overlay */
13763 || pos <= tem)
13765 /* If the string from which this glyph came is
13766 found in the buffer at point, then we've
13767 found the glyph we've been looking for. If
13768 it comes from an overlay (tem == 0), and it
13769 has the `cursor' property on one of its
13770 glyphs, record that glyph as a candidate for
13771 displaying the cursor. (As in the
13772 unidirectional version, we will display the
13773 cursor on the last candidate we find.) */
13774 if (tem == 0 || tem == pt_old)
13776 /* The glyphs from this string could have
13777 been reordered. Find the one with the
13778 smallest string position. Or there could
13779 be a character in the string with the
13780 `cursor' property, which means display
13781 cursor on that character's glyph. */
13782 EMACS_INT strpos = glyph->charpos;
13784 if (tem)
13786 cursor = glyph;
13787 string_from_text_prop = 1;
13789 for ( ;
13790 (row->reversed_p ? glyph > stop : glyph < stop)
13791 && EQ (glyph->object, str);
13792 glyph += incr)
13794 Lisp_Object cprop;
13795 EMACS_INT gpos = glyph->charpos;
13797 cprop = Fget_char_property (make_number (gpos),
13798 Qcursor,
13799 glyph->object);
13800 if (!NILP (cprop))
13802 cursor = glyph;
13803 break;
13805 if (tem && glyph->charpos < strpos)
13807 strpos = glyph->charpos;
13808 cursor = glyph;
13812 if (tem == pt_old)
13813 goto compute_x;
13815 if (tem)
13816 pos = tem + 1; /* don't find previous instances */
13818 /* This string is not what we want; skip all of the
13819 glyphs that came from it. */
13820 while ((row->reversed_p ? glyph > stop : glyph < stop)
13821 && EQ (glyph->object, str))
13822 glyph += incr;
13824 else
13825 glyph += incr;
13828 /* If we reached the end of the line, and END was from a string,
13829 the cursor is not on this line. */
13830 if (cursor == NULL
13831 && (row->reversed_p ? glyph <= end : glyph >= end)
13832 && STRINGP (end->object)
13833 && row->continued_p)
13834 return 0;
13838 compute_x:
13839 if (cursor != NULL)
13840 glyph = cursor;
13841 if (x < 0)
13843 struct glyph *g;
13845 /* Need to compute x that corresponds to GLYPH. */
13846 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13848 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13849 abort ();
13850 x += g->pixel_width;
13854 /* ROW could be part of a continued line, which, under bidi
13855 reordering, might have other rows whose start and end charpos
13856 occlude point. Only set w->cursor if we found a better
13857 approximation to the cursor position than we have from previously
13858 examined candidate rows belonging to the same continued line. */
13859 if (/* we already have a candidate row */
13860 w->cursor.vpos >= 0
13861 /* that candidate is not the row we are processing */
13862 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13863 /* Make sure cursor.vpos specifies a row whose start and end
13864 charpos occlude point, and it is valid candidate for being a
13865 cursor-row. This is because some callers of this function
13866 leave cursor.vpos at the row where the cursor was displayed
13867 during the last redisplay cycle. */
13868 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13869 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13870 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
13872 struct glyph *g1 =
13873 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13875 /* Don't consider glyphs that are outside TEXT_AREA. */
13876 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13877 return 0;
13878 /* Keep the candidate whose buffer position is the closest to
13879 point or has the `cursor' property. */
13880 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13881 w->cursor.hpos >= 0
13882 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13883 && ((BUFFERP (g1->object)
13884 && (g1->charpos == pt_old /* an exact match always wins */
13885 || (BUFFERP (glyph->object)
13886 && eabs (g1->charpos - pt_old)
13887 < eabs (glyph->charpos - pt_old))))
13888 /* previous candidate is a glyph from a string that has
13889 a non-nil `cursor' property */
13890 || (STRINGP (g1->object)
13891 && (!NILP (Fget_char_property (make_number (g1->charpos),
13892 Qcursor, g1->object))
13893 /* pevious candidate is from the same display
13894 string as this one, and the display string
13895 came from a text property */
13896 || (EQ (g1->object, glyph->object)
13897 && string_from_text_prop)
13898 /* this candidate is from newline and its
13899 position is not an exact match */
13900 || (INTEGERP (glyph->object)
13901 && glyph->charpos != pt_old)))))
13902 return 0;
13903 /* If this candidate gives an exact match, use that. */
13904 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
13905 /* If this candidate is a glyph created for the
13906 terminating newline of a line, and point is on that
13907 newline, it wins because it's an exact match. */
13908 || (!row->continued_p
13909 && INTEGERP (glyph->object)
13910 && glyph->charpos == 0
13911 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
13912 /* Otherwise, keep the candidate that comes from a row
13913 spanning less buffer positions. This may win when one or
13914 both candidate positions are on glyphs that came from
13915 display strings, for which we cannot compare buffer
13916 positions. */
13917 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13918 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13919 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13920 return 0;
13922 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13923 w->cursor.x = x;
13924 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13925 w->cursor.y = row->y + dy;
13927 if (w == XWINDOW (selected_window))
13929 if (!row->continued_p
13930 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13931 && row->x == 0)
13933 this_line_buffer = XBUFFER (w->buffer);
13935 CHARPOS (this_line_start_pos)
13936 = MATRIX_ROW_START_CHARPOS (row) + delta;
13937 BYTEPOS (this_line_start_pos)
13938 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13940 CHARPOS (this_line_end_pos)
13941 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13942 BYTEPOS (this_line_end_pos)
13943 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13945 this_line_y = w->cursor.y;
13946 this_line_pixel_height = row->height;
13947 this_line_vpos = w->cursor.vpos;
13948 this_line_start_x = row->x;
13950 else
13951 CHARPOS (this_line_start_pos) = 0;
13954 return 1;
13958 /* Run window scroll functions, if any, for WINDOW with new window
13959 start STARTP. Sets the window start of WINDOW to that position.
13961 We assume that the window's buffer is really current. */
13963 static inline struct text_pos
13964 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13966 struct window *w = XWINDOW (window);
13967 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13969 if (current_buffer != XBUFFER (w->buffer))
13970 abort ();
13972 if (!NILP (Vwindow_scroll_functions))
13974 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13975 make_number (CHARPOS (startp)));
13976 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13977 /* In case the hook functions switch buffers. */
13978 if (current_buffer != XBUFFER (w->buffer))
13979 set_buffer_internal_1 (XBUFFER (w->buffer));
13982 return startp;
13986 /* Make sure the line containing the cursor is fully visible.
13987 A value of 1 means there is nothing to be done.
13988 (Either the line is fully visible, or it cannot be made so,
13989 or we cannot tell.)
13991 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13992 is higher than window.
13994 A value of 0 means the caller should do scrolling
13995 as if point had gone off the screen. */
13997 static int
13998 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14000 struct glyph_matrix *matrix;
14001 struct glyph_row *row;
14002 int window_height;
14004 if (!make_cursor_line_fully_visible_p)
14005 return 1;
14007 /* It's not always possible to find the cursor, e.g, when a window
14008 is full of overlay strings. Don't do anything in that case. */
14009 if (w->cursor.vpos < 0)
14010 return 1;
14012 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14013 row = MATRIX_ROW (matrix, w->cursor.vpos);
14015 /* If the cursor row is not partially visible, there's nothing to do. */
14016 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14017 return 1;
14019 /* If the row the cursor is in is taller than the window's height,
14020 it's not clear what to do, so do nothing. */
14021 window_height = window_box_height (w);
14022 if (row->height >= window_height)
14024 if (!force_p || MINI_WINDOW_P (w)
14025 || w->vscroll || w->cursor.vpos == 0)
14026 return 1;
14028 return 0;
14032 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14033 non-zero means only WINDOW is redisplayed in redisplay_internal.
14034 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14035 in redisplay_window to bring a partially visible line into view in
14036 the case that only the cursor has moved.
14038 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14039 last screen line's vertical height extends past the end of the screen.
14041 Value is
14043 1 if scrolling succeeded
14045 0 if scrolling didn't find point.
14047 -1 if new fonts have been loaded so that we must interrupt
14048 redisplay, adjust glyph matrices, and try again. */
14050 enum
14052 SCROLLING_SUCCESS,
14053 SCROLLING_FAILED,
14054 SCROLLING_NEED_LARGER_MATRICES
14057 /* If scroll-conservatively is more than this, never recenter.
14059 If you change this, don't forget to update the doc string of
14060 `scroll-conservatively' and the Emacs manual. */
14061 #define SCROLL_LIMIT 100
14063 static int
14064 try_scrolling (Lisp_Object window, int just_this_one_p,
14065 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14066 int temp_scroll_step, int last_line_misfit)
14068 struct window *w = XWINDOW (window);
14069 struct frame *f = XFRAME (w->frame);
14070 struct text_pos pos, startp;
14071 struct it it;
14072 int this_scroll_margin, scroll_max, rc, height;
14073 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14074 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14075 Lisp_Object aggressive;
14076 /* We will never try scrolling more than this number of lines. */
14077 int scroll_limit = SCROLL_LIMIT;
14079 #if GLYPH_DEBUG
14080 debug_method_add (w, "try_scrolling");
14081 #endif
14083 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14085 /* Compute scroll margin height in pixels. We scroll when point is
14086 within this distance from the top or bottom of the window. */
14087 if (scroll_margin > 0)
14088 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14089 * FRAME_LINE_HEIGHT (f);
14090 else
14091 this_scroll_margin = 0;
14093 /* Force arg_scroll_conservatively to have a reasonable value, to
14094 avoid scrolling too far away with slow move_it_* functions. Note
14095 that the user can supply scroll-conservatively equal to
14096 `most-positive-fixnum', which can be larger than INT_MAX. */
14097 if (arg_scroll_conservatively > scroll_limit)
14099 arg_scroll_conservatively = scroll_limit + 1;
14100 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14102 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14103 /* Compute how much we should try to scroll maximally to bring
14104 point into view. */
14105 scroll_max = (max (scroll_step,
14106 max (arg_scroll_conservatively, temp_scroll_step))
14107 * FRAME_LINE_HEIGHT (f));
14108 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14109 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14110 /* We're trying to scroll because of aggressive scrolling but no
14111 scroll_step is set. Choose an arbitrary one. */
14112 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14113 else
14114 scroll_max = 0;
14116 too_near_end:
14118 /* Decide whether to scroll down. */
14119 if (PT > CHARPOS (startp))
14121 int scroll_margin_y;
14123 /* Compute the pixel ypos of the scroll margin, then move it to
14124 either that ypos or PT, whichever comes first. */
14125 start_display (&it, w, startp);
14126 scroll_margin_y = it.last_visible_y - this_scroll_margin
14127 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14128 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14129 (MOVE_TO_POS | MOVE_TO_Y));
14131 if (PT > CHARPOS (it.current.pos))
14133 int y0 = line_bottom_y (&it);
14134 /* Compute how many pixels below window bottom to stop searching
14135 for PT. This avoids costly search for PT that is far away if
14136 the user limited scrolling by a small number of lines, but
14137 always finds PT if scroll_conservatively is set to a large
14138 number, such as most-positive-fixnum. */
14139 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14140 int y_to_move = it.last_visible_y + slack;
14142 /* Compute the distance from the scroll margin to PT or to
14143 the scroll limit, whichever comes first. This should
14144 include the height of the cursor line, to make that line
14145 fully visible. */
14146 move_it_to (&it, PT, -1, y_to_move,
14147 -1, MOVE_TO_POS | MOVE_TO_Y);
14148 dy = line_bottom_y (&it) - y0;
14150 if (dy > scroll_max)
14151 return SCROLLING_FAILED;
14153 scroll_down_p = 1;
14157 if (scroll_down_p)
14159 /* Point is in or below the bottom scroll margin, so move the
14160 window start down. If scrolling conservatively, move it just
14161 enough down to make point visible. If scroll_step is set,
14162 move it down by scroll_step. */
14163 if (arg_scroll_conservatively)
14164 amount_to_scroll
14165 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14166 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14167 else if (scroll_step || temp_scroll_step)
14168 amount_to_scroll = scroll_max;
14169 else
14171 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14172 height = WINDOW_BOX_TEXT_HEIGHT (w);
14173 if (NUMBERP (aggressive))
14175 double float_amount = XFLOATINT (aggressive) * height;
14176 amount_to_scroll = float_amount;
14177 if (amount_to_scroll == 0 && float_amount > 0)
14178 amount_to_scroll = 1;
14179 /* Don't let point enter the scroll margin near top of
14180 the window. */
14181 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14182 amount_to_scroll = height - 2*this_scroll_margin + dy;
14186 if (amount_to_scroll <= 0)
14187 return SCROLLING_FAILED;
14189 start_display (&it, w, startp);
14190 if (arg_scroll_conservatively <= scroll_limit)
14191 move_it_vertically (&it, amount_to_scroll);
14192 else
14194 /* Extra precision for users who set scroll-conservatively
14195 to a large number: make sure the amount we scroll
14196 the window start is never less than amount_to_scroll,
14197 which was computed as distance from window bottom to
14198 point. This matters when lines at window top and lines
14199 below window bottom have different height. */
14200 struct it it1;
14201 void *it1data = NULL;
14202 /* We use a temporary it1 because line_bottom_y can modify
14203 its argument, if it moves one line down; see there. */
14204 int start_y;
14206 SAVE_IT (it1, it, it1data);
14207 start_y = line_bottom_y (&it1);
14208 do {
14209 RESTORE_IT (&it, &it, it1data);
14210 move_it_by_lines (&it, 1);
14211 SAVE_IT (it1, it, it1data);
14212 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14215 /* If STARTP is unchanged, move it down another screen line. */
14216 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14217 move_it_by_lines (&it, 1);
14218 startp = it.current.pos;
14220 else
14222 struct text_pos scroll_margin_pos = startp;
14224 /* See if point is inside the scroll margin at the top of the
14225 window. */
14226 if (this_scroll_margin)
14228 start_display (&it, w, startp);
14229 move_it_vertically (&it, this_scroll_margin);
14230 scroll_margin_pos = it.current.pos;
14233 if (PT < CHARPOS (scroll_margin_pos))
14235 /* Point is in the scroll margin at the top of the window or
14236 above what is displayed in the window. */
14237 int y0, y_to_move;
14239 /* Compute the vertical distance from PT to the scroll
14240 margin position. Move as far as scroll_max allows, or
14241 one screenful, or 10 screen lines, whichever is largest.
14242 Give up if distance is greater than scroll_max. */
14243 SET_TEXT_POS (pos, PT, PT_BYTE);
14244 start_display (&it, w, pos);
14245 y0 = it.current_y;
14246 y_to_move = max (it.last_visible_y,
14247 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14248 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14249 y_to_move, -1,
14250 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14251 dy = it.current_y - y0;
14252 if (dy > scroll_max)
14253 return SCROLLING_FAILED;
14255 /* Compute new window start. */
14256 start_display (&it, w, startp);
14258 if (arg_scroll_conservatively)
14259 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14260 max (scroll_step, temp_scroll_step));
14261 else if (scroll_step || temp_scroll_step)
14262 amount_to_scroll = scroll_max;
14263 else
14265 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14266 height = WINDOW_BOX_TEXT_HEIGHT (w);
14267 if (NUMBERP (aggressive))
14269 double float_amount = XFLOATINT (aggressive) * height;
14270 amount_to_scroll = float_amount;
14271 if (amount_to_scroll == 0 && float_amount > 0)
14272 amount_to_scroll = 1;
14273 amount_to_scroll -=
14274 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14275 /* Don't let point enter the scroll margin near
14276 bottom of the window. */
14277 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14278 amount_to_scroll = height - 2*this_scroll_margin + dy;
14282 if (amount_to_scroll <= 0)
14283 return SCROLLING_FAILED;
14285 move_it_vertically_backward (&it, amount_to_scroll);
14286 startp = it.current.pos;
14290 /* Run window scroll functions. */
14291 startp = run_window_scroll_functions (window, startp);
14293 /* Display the window. Give up if new fonts are loaded, or if point
14294 doesn't appear. */
14295 if (!try_window (window, startp, 0))
14296 rc = SCROLLING_NEED_LARGER_MATRICES;
14297 else if (w->cursor.vpos < 0)
14299 clear_glyph_matrix (w->desired_matrix);
14300 rc = SCROLLING_FAILED;
14302 else
14304 /* Maybe forget recorded base line for line number display. */
14305 if (!just_this_one_p
14306 || current_buffer->clip_changed
14307 || BEG_UNCHANGED < CHARPOS (startp))
14308 w->base_line_number = Qnil;
14310 /* If cursor ends up on a partially visible line,
14311 treat that as being off the bottom of the screen. */
14312 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14313 /* It's possible that the cursor is on the first line of the
14314 buffer, which is partially obscured due to a vscroll
14315 (Bug#7537). In that case, avoid looping forever . */
14316 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14318 clear_glyph_matrix (w->desired_matrix);
14319 ++extra_scroll_margin_lines;
14320 goto too_near_end;
14322 rc = SCROLLING_SUCCESS;
14325 return rc;
14329 /* Compute a suitable window start for window W if display of W starts
14330 on a continuation line. Value is non-zero if a new window start
14331 was computed.
14333 The new window start will be computed, based on W's width, starting
14334 from the start of the continued line. It is the start of the
14335 screen line with the minimum distance from the old start W->start. */
14337 static int
14338 compute_window_start_on_continuation_line (struct window *w)
14340 struct text_pos pos, start_pos;
14341 int window_start_changed_p = 0;
14343 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14345 /* If window start is on a continuation line... Window start may be
14346 < BEGV in case there's invisible text at the start of the
14347 buffer (M-x rmail, for example). */
14348 if (CHARPOS (start_pos) > BEGV
14349 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14351 struct it it;
14352 struct glyph_row *row;
14354 /* Handle the case that the window start is out of range. */
14355 if (CHARPOS (start_pos) < BEGV)
14356 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14357 else if (CHARPOS (start_pos) > ZV)
14358 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14360 /* Find the start of the continued line. This should be fast
14361 because scan_buffer is fast (newline cache). */
14362 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14363 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14364 row, DEFAULT_FACE_ID);
14365 reseat_at_previous_visible_line_start (&it);
14367 /* If the line start is "too far" away from the window start,
14368 say it takes too much time to compute a new window start. */
14369 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14370 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14372 int min_distance, distance;
14374 /* Move forward by display lines to find the new window
14375 start. If window width was enlarged, the new start can
14376 be expected to be > the old start. If window width was
14377 decreased, the new window start will be < the old start.
14378 So, we're looking for the display line start with the
14379 minimum distance from the old window start. */
14380 pos = it.current.pos;
14381 min_distance = INFINITY;
14382 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14383 distance < min_distance)
14385 min_distance = distance;
14386 pos = it.current.pos;
14387 move_it_by_lines (&it, 1);
14390 /* Set the window start there. */
14391 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14392 window_start_changed_p = 1;
14396 return window_start_changed_p;
14400 /* Try cursor movement in case text has not changed in window WINDOW,
14401 with window start STARTP. Value is
14403 CURSOR_MOVEMENT_SUCCESS if successful
14405 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14407 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14408 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14409 we want to scroll as if scroll-step were set to 1. See the code.
14411 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14412 which case we have to abort this redisplay, and adjust matrices
14413 first. */
14415 enum
14417 CURSOR_MOVEMENT_SUCCESS,
14418 CURSOR_MOVEMENT_CANNOT_BE_USED,
14419 CURSOR_MOVEMENT_MUST_SCROLL,
14420 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14423 static int
14424 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14426 struct window *w = XWINDOW (window);
14427 struct frame *f = XFRAME (w->frame);
14428 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14430 #if GLYPH_DEBUG
14431 if (inhibit_try_cursor_movement)
14432 return rc;
14433 #endif
14435 /* Handle case where text has not changed, only point, and it has
14436 not moved off the frame. */
14437 if (/* Point may be in this window. */
14438 PT >= CHARPOS (startp)
14439 /* Selective display hasn't changed. */
14440 && !current_buffer->clip_changed
14441 /* Function force-mode-line-update is used to force a thorough
14442 redisplay. It sets either windows_or_buffers_changed or
14443 update_mode_lines. So don't take a shortcut here for these
14444 cases. */
14445 && !update_mode_lines
14446 && !windows_or_buffers_changed
14447 && !cursor_type_changed
14448 /* Can't use this case if highlighting a region. When a
14449 region exists, cursor movement has to do more than just
14450 set the cursor. */
14451 && !(!NILP (Vtransient_mark_mode)
14452 && !NILP (BVAR (current_buffer, mark_active)))
14453 && NILP (w->region_showing)
14454 && NILP (Vshow_trailing_whitespace)
14455 /* Right after splitting windows, last_point may be nil. */
14456 && INTEGERP (w->last_point)
14457 /* This code is not used for mini-buffer for the sake of the case
14458 of redisplaying to replace an echo area message; since in
14459 that case the mini-buffer contents per se are usually
14460 unchanged. This code is of no real use in the mini-buffer
14461 since the handling of this_line_start_pos, etc., in redisplay
14462 handles the same cases. */
14463 && !EQ (window, minibuf_window)
14464 /* When splitting windows or for new windows, it happens that
14465 redisplay is called with a nil window_end_vpos or one being
14466 larger than the window. This should really be fixed in
14467 window.c. I don't have this on my list, now, so we do
14468 approximately the same as the old redisplay code. --gerd. */
14469 && INTEGERP (w->window_end_vpos)
14470 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14471 && (FRAME_WINDOW_P (f)
14472 || !overlay_arrow_in_current_buffer_p ()))
14474 int this_scroll_margin, top_scroll_margin;
14475 struct glyph_row *row = NULL;
14477 #if GLYPH_DEBUG
14478 debug_method_add (w, "cursor movement");
14479 #endif
14481 /* Scroll if point within this distance from the top or bottom
14482 of the window. This is a pixel value. */
14483 if (scroll_margin > 0)
14485 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14486 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14488 else
14489 this_scroll_margin = 0;
14491 top_scroll_margin = this_scroll_margin;
14492 if (WINDOW_WANTS_HEADER_LINE_P (w))
14493 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14495 /* Start with the row the cursor was displayed during the last
14496 not paused redisplay. Give up if that row is not valid. */
14497 if (w->last_cursor.vpos < 0
14498 || w->last_cursor.vpos >= w->current_matrix->nrows)
14499 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14500 else
14502 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14503 if (row->mode_line_p)
14504 ++row;
14505 if (!row->enabled_p)
14506 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14509 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14511 int scroll_p = 0, must_scroll = 0;
14512 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14514 if (PT > XFASTINT (w->last_point))
14516 /* Point has moved forward. */
14517 while (MATRIX_ROW_END_CHARPOS (row) < PT
14518 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14520 xassert (row->enabled_p);
14521 ++row;
14524 /* If the end position of a row equals the start
14525 position of the next row, and PT is at that position,
14526 we would rather display cursor in the next line. */
14527 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14528 && MATRIX_ROW_END_CHARPOS (row) == PT
14529 && row < w->current_matrix->rows
14530 + w->current_matrix->nrows - 1
14531 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14532 && !cursor_row_p (row))
14533 ++row;
14535 /* If within the scroll margin, scroll. Note that
14536 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14537 the next line would be drawn, and that
14538 this_scroll_margin can be zero. */
14539 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14540 || PT > MATRIX_ROW_END_CHARPOS (row)
14541 /* Line is completely visible last line in window
14542 and PT is to be set in the next line. */
14543 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14544 && PT == MATRIX_ROW_END_CHARPOS (row)
14545 && !row->ends_at_zv_p
14546 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14547 scroll_p = 1;
14549 else if (PT < XFASTINT (w->last_point))
14551 /* Cursor has to be moved backward. Note that PT >=
14552 CHARPOS (startp) because of the outer if-statement. */
14553 while (!row->mode_line_p
14554 && (MATRIX_ROW_START_CHARPOS (row) > PT
14555 || (MATRIX_ROW_START_CHARPOS (row) == PT
14556 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14557 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14558 row > w->current_matrix->rows
14559 && (row-1)->ends_in_newline_from_string_p))))
14560 && (row->y > top_scroll_margin
14561 || CHARPOS (startp) == BEGV))
14563 xassert (row->enabled_p);
14564 --row;
14567 /* Consider the following case: Window starts at BEGV,
14568 there is invisible, intangible text at BEGV, so that
14569 display starts at some point START > BEGV. It can
14570 happen that we are called with PT somewhere between
14571 BEGV and START. Try to handle that case. */
14572 if (row < w->current_matrix->rows
14573 || row->mode_line_p)
14575 row = w->current_matrix->rows;
14576 if (row->mode_line_p)
14577 ++row;
14580 /* Due to newlines in overlay strings, we may have to
14581 skip forward over overlay strings. */
14582 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14583 && MATRIX_ROW_END_CHARPOS (row) == PT
14584 && !cursor_row_p (row))
14585 ++row;
14587 /* If within the scroll margin, scroll. */
14588 if (row->y < top_scroll_margin
14589 && CHARPOS (startp) != BEGV)
14590 scroll_p = 1;
14592 else
14594 /* Cursor did not move. So don't scroll even if cursor line
14595 is partially visible, as it was so before. */
14596 rc = CURSOR_MOVEMENT_SUCCESS;
14599 if (PT < MATRIX_ROW_START_CHARPOS (row)
14600 || PT > MATRIX_ROW_END_CHARPOS (row))
14602 /* if PT is not in the glyph row, give up. */
14603 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14604 must_scroll = 1;
14606 else if (rc != CURSOR_MOVEMENT_SUCCESS
14607 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14609 /* If rows are bidi-reordered and point moved, back up
14610 until we find a row that does not belong to a
14611 continuation line. This is because we must consider
14612 all rows of a continued line as candidates for the
14613 new cursor positioning, since row start and end
14614 positions change non-linearly with vertical position
14615 in such rows. */
14616 /* FIXME: Revisit this when glyph ``spilling'' in
14617 continuation lines' rows is implemented for
14618 bidi-reordered rows. */
14619 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14621 xassert (row->enabled_p);
14622 --row;
14623 /* If we hit the beginning of the displayed portion
14624 without finding the first row of a continued
14625 line, give up. */
14626 if (row <= w->current_matrix->rows)
14628 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14629 break;
14634 if (must_scroll)
14636 else if (rc != CURSOR_MOVEMENT_SUCCESS
14637 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14638 && make_cursor_line_fully_visible_p)
14640 if (PT == MATRIX_ROW_END_CHARPOS (row)
14641 && !row->ends_at_zv_p
14642 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14643 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14644 else if (row->height > window_box_height (w))
14646 /* If we end up in a partially visible line, let's
14647 make it fully visible, except when it's taller
14648 than the window, in which case we can't do much
14649 about it. */
14650 *scroll_step = 1;
14651 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14653 else
14655 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14656 if (!cursor_row_fully_visible_p (w, 0, 1))
14657 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14658 else
14659 rc = CURSOR_MOVEMENT_SUCCESS;
14662 else if (scroll_p)
14663 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14664 else if (rc != CURSOR_MOVEMENT_SUCCESS
14665 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14667 /* With bidi-reordered rows, there could be more than
14668 one candidate row whose start and end positions
14669 occlude point. We need to let set_cursor_from_row
14670 find the best candidate. */
14671 /* FIXME: Revisit this when glyph ``spilling'' in
14672 continuation lines' rows is implemented for
14673 bidi-reordered rows. */
14674 int rv = 0;
14678 int at_zv_p = 0, exact_match_p = 0;
14680 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14681 && PT <= MATRIX_ROW_END_CHARPOS (row)
14682 && cursor_row_p (row))
14683 rv |= set_cursor_from_row (w, row, w->current_matrix,
14684 0, 0, 0, 0);
14685 /* As soon as we've found the exact match for point,
14686 or the first suitable row whose ends_at_zv_p flag
14687 is set, we are done. */
14688 at_zv_p =
14689 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
14690 if (!at_zv_p)
14692 struct glyph_row *candidate =
14693 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14694 struct glyph *g =
14695 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
14696 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
14698 exact_match_p =
14699 (BUFFERP (g->object) && g->charpos == PT)
14700 || (INTEGERP (g->object)
14701 && (g->charpos == PT
14702 || (g->charpos == 0 && endpos - 1 == PT)));
14704 if (rv && (at_zv_p || exact_match_p))
14706 rc = CURSOR_MOVEMENT_SUCCESS;
14707 break;
14709 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
14710 break;
14711 ++row;
14713 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
14714 || row->continued_p)
14715 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14716 || (MATRIX_ROW_START_CHARPOS (row) == PT
14717 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14718 /* If we didn't find any candidate rows, or exited the
14719 loop before all the candidates were examined, signal
14720 to the caller that this method failed. */
14721 if (rc != CURSOR_MOVEMENT_SUCCESS
14722 && !(rv
14723 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14724 && !row->continued_p))
14725 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14726 else if (rv)
14727 rc = CURSOR_MOVEMENT_SUCCESS;
14729 else
14733 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14735 rc = CURSOR_MOVEMENT_SUCCESS;
14736 break;
14738 ++row;
14740 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14741 && MATRIX_ROW_START_CHARPOS (row) == PT
14742 && cursor_row_p (row));
14747 return rc;
14750 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14751 static
14752 #endif
14753 void
14754 set_vertical_scroll_bar (struct window *w)
14756 EMACS_INT start, end, whole;
14758 /* Calculate the start and end positions for the current window.
14759 At some point, it would be nice to choose between scrollbars
14760 which reflect the whole buffer size, with special markers
14761 indicating narrowing, and scrollbars which reflect only the
14762 visible region.
14764 Note that mini-buffers sometimes aren't displaying any text. */
14765 if (!MINI_WINDOW_P (w)
14766 || (w == XWINDOW (minibuf_window)
14767 && NILP (echo_area_buffer[0])))
14769 struct buffer *buf = XBUFFER (w->buffer);
14770 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14771 start = marker_position (w->start) - BUF_BEGV (buf);
14772 /* I don't think this is guaranteed to be right. For the
14773 moment, we'll pretend it is. */
14774 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14776 if (end < start)
14777 end = start;
14778 if (whole < (end - start))
14779 whole = end - start;
14781 else
14782 start = end = whole = 0;
14784 /* Indicate what this scroll bar ought to be displaying now. */
14785 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14786 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14787 (w, end - start, whole, start);
14791 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14792 selected_window is redisplayed.
14794 We can return without actually redisplaying the window if
14795 fonts_changed_p is nonzero. In that case, redisplay_internal will
14796 retry. */
14798 static void
14799 redisplay_window (Lisp_Object window, int just_this_one_p)
14801 struct window *w = XWINDOW (window);
14802 struct frame *f = XFRAME (w->frame);
14803 struct buffer *buffer = XBUFFER (w->buffer);
14804 struct buffer *old = current_buffer;
14805 struct text_pos lpoint, opoint, startp;
14806 int update_mode_line;
14807 int tem;
14808 struct it it;
14809 /* Record it now because it's overwritten. */
14810 int current_matrix_up_to_date_p = 0;
14811 int used_current_matrix_p = 0;
14812 /* This is less strict than current_matrix_up_to_date_p.
14813 It indictes that the buffer contents and narrowing are unchanged. */
14814 int buffer_unchanged_p = 0;
14815 int temp_scroll_step = 0;
14816 int count = SPECPDL_INDEX ();
14817 int rc;
14818 int centering_position = -1;
14819 int last_line_misfit = 0;
14820 EMACS_INT beg_unchanged, end_unchanged;
14822 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14823 opoint = lpoint;
14825 /* W must be a leaf window here. */
14826 xassert (!NILP (w->buffer));
14827 #if GLYPH_DEBUG
14828 *w->desired_matrix->method = 0;
14829 #endif
14831 restart:
14832 reconsider_clip_changes (w, buffer);
14834 /* Has the mode line to be updated? */
14835 update_mode_line = (!NILP (w->update_mode_line)
14836 || update_mode_lines
14837 || buffer->clip_changed
14838 || buffer->prevent_redisplay_optimizations_p);
14840 if (MINI_WINDOW_P (w))
14842 if (w == XWINDOW (echo_area_window)
14843 && !NILP (echo_area_buffer[0]))
14845 if (update_mode_line)
14846 /* We may have to update a tty frame's menu bar or a
14847 tool-bar. Example `M-x C-h C-h C-g'. */
14848 goto finish_menu_bars;
14849 else
14850 /* We've already displayed the echo area glyphs in this window. */
14851 goto finish_scroll_bars;
14853 else if ((w != XWINDOW (minibuf_window)
14854 || minibuf_level == 0)
14855 /* When buffer is nonempty, redisplay window normally. */
14856 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14857 /* Quail displays non-mini buffers in minibuffer window.
14858 In that case, redisplay the window normally. */
14859 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14861 /* W is a mini-buffer window, but it's not active, so clear
14862 it. */
14863 int yb = window_text_bottom_y (w);
14864 struct glyph_row *row;
14865 int y;
14867 for (y = 0, row = w->desired_matrix->rows;
14868 y < yb;
14869 y += row->height, ++row)
14870 blank_row (w, row, y);
14871 goto finish_scroll_bars;
14874 clear_glyph_matrix (w->desired_matrix);
14877 /* Otherwise set up data on this window; select its buffer and point
14878 value. */
14879 /* Really select the buffer, for the sake of buffer-local
14880 variables. */
14881 set_buffer_internal_1 (XBUFFER (w->buffer));
14883 current_matrix_up_to_date_p
14884 = (!NILP (w->window_end_valid)
14885 && !current_buffer->clip_changed
14886 && !current_buffer->prevent_redisplay_optimizations_p
14887 && XFASTINT (w->last_modified) >= MODIFF
14888 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14890 /* Run the window-bottom-change-functions
14891 if it is possible that the text on the screen has changed
14892 (either due to modification of the text, or any other reason). */
14893 if (!current_matrix_up_to_date_p
14894 && !NILP (Vwindow_text_change_functions))
14896 safe_run_hooks (Qwindow_text_change_functions);
14897 goto restart;
14900 beg_unchanged = BEG_UNCHANGED;
14901 end_unchanged = END_UNCHANGED;
14903 SET_TEXT_POS (opoint, PT, PT_BYTE);
14905 specbind (Qinhibit_point_motion_hooks, Qt);
14907 buffer_unchanged_p
14908 = (!NILP (w->window_end_valid)
14909 && !current_buffer->clip_changed
14910 && XFASTINT (w->last_modified) >= MODIFF
14911 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14913 /* When windows_or_buffers_changed is non-zero, we can't rely on
14914 the window end being valid, so set it to nil there. */
14915 if (windows_or_buffers_changed)
14917 /* If window starts on a continuation line, maybe adjust the
14918 window start in case the window's width changed. */
14919 if (XMARKER (w->start)->buffer == current_buffer)
14920 compute_window_start_on_continuation_line (w);
14922 w->window_end_valid = Qnil;
14925 /* Some sanity checks. */
14926 CHECK_WINDOW_END (w);
14927 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14928 abort ();
14929 if (BYTEPOS (opoint) < CHARPOS (opoint))
14930 abort ();
14932 /* If %c is in mode line, update it if needed. */
14933 if (!NILP (w->column_number_displayed)
14934 /* This alternative quickly identifies a common case
14935 where no change is needed. */
14936 && !(PT == XFASTINT (w->last_point)
14937 && XFASTINT (w->last_modified) >= MODIFF
14938 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14939 && (XFASTINT (w->column_number_displayed) != current_column ()))
14940 update_mode_line = 1;
14942 /* Count number of windows showing the selected buffer. An indirect
14943 buffer counts as its base buffer. */
14944 if (!just_this_one_p)
14946 struct buffer *current_base, *window_base;
14947 current_base = current_buffer;
14948 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14949 if (current_base->base_buffer)
14950 current_base = current_base->base_buffer;
14951 if (window_base->base_buffer)
14952 window_base = window_base->base_buffer;
14953 if (current_base == window_base)
14954 buffer_shared++;
14957 /* Point refers normally to the selected window. For any other
14958 window, set up appropriate value. */
14959 if (!EQ (window, selected_window))
14961 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14962 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14963 if (new_pt < BEGV)
14965 new_pt = BEGV;
14966 new_pt_byte = BEGV_BYTE;
14967 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14969 else if (new_pt > (ZV - 1))
14971 new_pt = ZV;
14972 new_pt_byte = ZV_BYTE;
14973 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14976 /* We don't use SET_PT so that the point-motion hooks don't run. */
14977 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14980 /* If any of the character widths specified in the display table
14981 have changed, invalidate the width run cache. It's true that
14982 this may be a bit late to catch such changes, but the rest of
14983 redisplay goes (non-fatally) haywire when the display table is
14984 changed, so why should we worry about doing any better? */
14985 if (current_buffer->width_run_cache)
14987 struct Lisp_Char_Table *disptab = buffer_display_table ();
14989 if (! disptab_matches_widthtab (disptab,
14990 XVECTOR (BVAR (current_buffer, width_table))))
14992 invalidate_region_cache (current_buffer,
14993 current_buffer->width_run_cache,
14994 BEG, Z);
14995 recompute_width_table (current_buffer, disptab);
14999 /* If window-start is screwed up, choose a new one. */
15000 if (XMARKER (w->start)->buffer != current_buffer)
15001 goto recenter;
15003 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15005 /* If someone specified a new starting point but did not insist,
15006 check whether it can be used. */
15007 if (!NILP (w->optional_new_start)
15008 && CHARPOS (startp) >= BEGV
15009 && CHARPOS (startp) <= ZV)
15011 w->optional_new_start = Qnil;
15012 start_display (&it, w, startp);
15013 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15014 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15015 if (IT_CHARPOS (it) == PT)
15016 w->force_start = Qt;
15017 /* IT may overshoot PT if text at PT is invisible. */
15018 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15019 w->force_start = Qt;
15022 force_start:
15024 /* Handle case where place to start displaying has been specified,
15025 unless the specified location is outside the accessible range. */
15026 if (!NILP (w->force_start)
15027 || w->frozen_window_start_p)
15029 /* We set this later on if we have to adjust point. */
15030 int new_vpos = -1;
15032 w->force_start = Qnil;
15033 w->vscroll = 0;
15034 w->window_end_valid = Qnil;
15036 /* Forget any recorded base line for line number display. */
15037 if (!buffer_unchanged_p)
15038 w->base_line_number = Qnil;
15040 /* Redisplay the mode line. Select the buffer properly for that.
15041 Also, run the hook window-scroll-functions
15042 because we have scrolled. */
15043 /* Note, we do this after clearing force_start because
15044 if there's an error, it is better to forget about force_start
15045 than to get into an infinite loop calling the hook functions
15046 and having them get more errors. */
15047 if (!update_mode_line
15048 || ! NILP (Vwindow_scroll_functions))
15050 update_mode_line = 1;
15051 w->update_mode_line = Qt;
15052 startp = run_window_scroll_functions (window, startp);
15055 w->last_modified = make_number (0);
15056 w->last_overlay_modified = make_number (0);
15057 if (CHARPOS (startp) < BEGV)
15058 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15059 else if (CHARPOS (startp) > ZV)
15060 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15062 /* Redisplay, then check if cursor has been set during the
15063 redisplay. Give up if new fonts were loaded. */
15064 /* We used to issue a CHECK_MARGINS argument to try_window here,
15065 but this causes scrolling to fail when point begins inside
15066 the scroll margin (bug#148) -- cyd */
15067 if (!try_window (window, startp, 0))
15069 w->force_start = Qt;
15070 clear_glyph_matrix (w->desired_matrix);
15071 goto need_larger_matrices;
15074 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15076 /* If point does not appear, try to move point so it does
15077 appear. The desired matrix has been built above, so we
15078 can use it here. */
15079 new_vpos = window_box_height (w) / 2;
15082 if (!cursor_row_fully_visible_p (w, 0, 0))
15084 /* Point does appear, but on a line partly visible at end of window.
15085 Move it back to a fully-visible line. */
15086 new_vpos = window_box_height (w);
15089 /* If we need to move point for either of the above reasons,
15090 now actually do it. */
15091 if (new_vpos >= 0)
15093 struct glyph_row *row;
15095 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15096 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15097 ++row;
15099 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15100 MATRIX_ROW_START_BYTEPOS (row));
15102 if (w != XWINDOW (selected_window))
15103 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15104 else if (current_buffer == old)
15105 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15107 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15109 /* If we are highlighting the region, then we just changed
15110 the region, so redisplay to show it. */
15111 if (!NILP (Vtransient_mark_mode)
15112 && !NILP (BVAR (current_buffer, mark_active)))
15114 clear_glyph_matrix (w->desired_matrix);
15115 if (!try_window (window, startp, 0))
15116 goto need_larger_matrices;
15120 #if GLYPH_DEBUG
15121 debug_method_add (w, "forced window start");
15122 #endif
15123 goto done;
15126 /* Handle case where text has not changed, only point, and it has
15127 not moved off the frame, and we are not retrying after hscroll.
15128 (current_matrix_up_to_date_p is nonzero when retrying.) */
15129 if (current_matrix_up_to_date_p
15130 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15131 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15133 switch (rc)
15135 case CURSOR_MOVEMENT_SUCCESS:
15136 used_current_matrix_p = 1;
15137 goto done;
15139 case CURSOR_MOVEMENT_MUST_SCROLL:
15140 goto try_to_scroll;
15142 default:
15143 abort ();
15146 /* If current starting point was originally the beginning of a line
15147 but no longer is, find a new starting point. */
15148 else if (!NILP (w->start_at_line_beg)
15149 && !(CHARPOS (startp) <= BEGV
15150 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15152 #if GLYPH_DEBUG
15153 debug_method_add (w, "recenter 1");
15154 #endif
15155 goto recenter;
15158 /* Try scrolling with try_window_id. Value is > 0 if update has
15159 been done, it is -1 if we know that the same window start will
15160 not work. It is 0 if unsuccessful for some other reason. */
15161 else if ((tem = try_window_id (w)) != 0)
15163 #if GLYPH_DEBUG
15164 debug_method_add (w, "try_window_id %d", tem);
15165 #endif
15167 if (fonts_changed_p)
15168 goto need_larger_matrices;
15169 if (tem > 0)
15170 goto done;
15172 /* Otherwise try_window_id has returned -1 which means that we
15173 don't want the alternative below this comment to execute. */
15175 else if (CHARPOS (startp) >= BEGV
15176 && CHARPOS (startp) <= ZV
15177 && PT >= CHARPOS (startp)
15178 && (CHARPOS (startp) < ZV
15179 /* Avoid starting at end of buffer. */
15180 || CHARPOS (startp) == BEGV
15181 || (XFASTINT (w->last_modified) >= MODIFF
15182 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15184 int d1, d2, d3, d4, d5, d6;
15186 /* If first window line is a continuation line, and window start
15187 is inside the modified region, but the first change is before
15188 current window start, we must select a new window start.
15190 However, if this is the result of a down-mouse event (e.g. by
15191 extending the mouse-drag-overlay), we don't want to select a
15192 new window start, since that would change the position under
15193 the mouse, resulting in an unwanted mouse-movement rather
15194 than a simple mouse-click. */
15195 if (NILP (w->start_at_line_beg)
15196 && NILP (do_mouse_tracking)
15197 && CHARPOS (startp) > BEGV
15198 && CHARPOS (startp) > BEG + beg_unchanged
15199 && CHARPOS (startp) <= Z - end_unchanged
15200 /* Even if w->start_at_line_beg is nil, a new window may
15201 start at a line_beg, since that's how set_buffer_window
15202 sets it. So, we need to check the return value of
15203 compute_window_start_on_continuation_line. (See also
15204 bug#197). */
15205 && XMARKER (w->start)->buffer == current_buffer
15206 && compute_window_start_on_continuation_line (w)
15207 /* It doesn't make sense to force the window start like we
15208 do at label force_start if it is already known that point
15209 will not be visible in the resulting window, because
15210 doing so will move point from its correct position
15211 instead of scrolling the window to bring point into view.
15212 See bug#9324. */
15213 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15215 w->force_start = Qt;
15216 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15217 goto force_start;
15220 #if GLYPH_DEBUG
15221 debug_method_add (w, "same window start");
15222 #endif
15224 /* Try to redisplay starting at same place as before.
15225 If point has not moved off frame, accept the results. */
15226 if (!current_matrix_up_to_date_p
15227 /* Don't use try_window_reusing_current_matrix in this case
15228 because a window scroll function can have changed the
15229 buffer. */
15230 || !NILP (Vwindow_scroll_functions)
15231 || MINI_WINDOW_P (w)
15232 || !(used_current_matrix_p
15233 = try_window_reusing_current_matrix (w)))
15235 IF_DEBUG (debug_method_add (w, "1"));
15236 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15237 /* -1 means we need to scroll.
15238 0 means we need new matrices, but fonts_changed_p
15239 is set in that case, so we will detect it below. */
15240 goto try_to_scroll;
15243 if (fonts_changed_p)
15244 goto need_larger_matrices;
15246 if (w->cursor.vpos >= 0)
15248 if (!just_this_one_p
15249 || current_buffer->clip_changed
15250 || BEG_UNCHANGED < CHARPOS (startp))
15251 /* Forget any recorded base line for line number display. */
15252 w->base_line_number = Qnil;
15254 if (!cursor_row_fully_visible_p (w, 1, 0))
15256 clear_glyph_matrix (w->desired_matrix);
15257 last_line_misfit = 1;
15259 /* Drop through and scroll. */
15260 else
15261 goto done;
15263 else
15264 clear_glyph_matrix (w->desired_matrix);
15267 try_to_scroll:
15269 w->last_modified = make_number (0);
15270 w->last_overlay_modified = make_number (0);
15272 /* Redisplay the mode line. Select the buffer properly for that. */
15273 if (!update_mode_line)
15275 update_mode_line = 1;
15276 w->update_mode_line = Qt;
15279 /* Try to scroll by specified few lines. */
15280 if ((scroll_conservatively
15281 || emacs_scroll_step
15282 || temp_scroll_step
15283 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15284 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15285 && CHARPOS (startp) >= BEGV
15286 && CHARPOS (startp) <= ZV)
15288 /* The function returns -1 if new fonts were loaded, 1 if
15289 successful, 0 if not successful. */
15290 int ss = try_scrolling (window, just_this_one_p,
15291 scroll_conservatively,
15292 emacs_scroll_step,
15293 temp_scroll_step, last_line_misfit);
15294 switch (ss)
15296 case SCROLLING_SUCCESS:
15297 goto done;
15299 case SCROLLING_NEED_LARGER_MATRICES:
15300 goto need_larger_matrices;
15302 case SCROLLING_FAILED:
15303 break;
15305 default:
15306 abort ();
15310 /* Finally, just choose a place to start which positions point
15311 according to user preferences. */
15313 recenter:
15315 #if GLYPH_DEBUG
15316 debug_method_add (w, "recenter");
15317 #endif
15319 /* w->vscroll = 0; */
15321 /* Forget any previously recorded base line for line number display. */
15322 if (!buffer_unchanged_p)
15323 w->base_line_number = Qnil;
15325 /* Determine the window start relative to point. */
15326 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15327 it.current_y = it.last_visible_y;
15328 if (centering_position < 0)
15330 int margin =
15331 scroll_margin > 0
15332 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15333 : 0;
15334 EMACS_INT margin_pos = CHARPOS (startp);
15335 int scrolling_up;
15336 Lisp_Object aggressive;
15338 /* If there is a scroll margin at the top of the window, find
15339 its character position. */
15340 if (margin
15341 /* Cannot call start_display if startp is not in the
15342 accessible region of the buffer. This can happen when we
15343 have just switched to a different buffer and/or changed
15344 its restriction. In that case, startp is initialized to
15345 the character position 1 (BEG) because we did not yet
15346 have chance to display the buffer even once. */
15347 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15349 struct it it1;
15350 void *it1data = NULL;
15352 SAVE_IT (it1, it, it1data);
15353 start_display (&it1, w, startp);
15354 move_it_vertically (&it1, margin);
15355 margin_pos = IT_CHARPOS (it1);
15356 RESTORE_IT (&it, &it, it1data);
15358 scrolling_up = PT > margin_pos;
15359 aggressive =
15360 scrolling_up
15361 ? BVAR (current_buffer, scroll_up_aggressively)
15362 : BVAR (current_buffer, scroll_down_aggressively);
15364 if (!MINI_WINDOW_P (w)
15365 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15367 int pt_offset = 0;
15369 /* Setting scroll-conservatively overrides
15370 scroll-*-aggressively. */
15371 if (!scroll_conservatively && NUMBERP (aggressive))
15373 double float_amount = XFLOATINT (aggressive);
15375 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15376 if (pt_offset == 0 && float_amount > 0)
15377 pt_offset = 1;
15378 if (pt_offset)
15379 margin -= 1;
15381 /* Compute how much to move the window start backward from
15382 point so that point will be displayed where the user
15383 wants it. */
15384 if (scrolling_up)
15386 centering_position = it.last_visible_y;
15387 if (pt_offset)
15388 centering_position -= pt_offset;
15389 centering_position -=
15390 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15391 + WINDOW_HEADER_LINE_HEIGHT (w);
15392 /* Don't let point enter the scroll margin near top of
15393 the window. */
15394 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15395 centering_position = margin * FRAME_LINE_HEIGHT (f);
15397 else
15398 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15400 else
15401 /* Set the window start half the height of the window backward
15402 from point. */
15403 centering_position = window_box_height (w) / 2;
15405 move_it_vertically_backward (&it, centering_position);
15407 xassert (IT_CHARPOS (it) >= BEGV);
15409 /* The function move_it_vertically_backward may move over more
15410 than the specified y-distance. If it->w is small, e.g. a
15411 mini-buffer window, we may end up in front of the window's
15412 display area. Start displaying at the start of the line
15413 containing PT in this case. */
15414 if (it.current_y <= 0)
15416 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15417 move_it_vertically_backward (&it, 0);
15418 it.current_y = 0;
15421 it.current_x = it.hpos = 0;
15423 /* Set the window start position here explicitly, to avoid an
15424 infinite loop in case the functions in window-scroll-functions
15425 get errors. */
15426 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15428 /* Run scroll hooks. */
15429 startp = run_window_scroll_functions (window, it.current.pos);
15431 /* Redisplay the window. */
15432 if (!current_matrix_up_to_date_p
15433 || windows_or_buffers_changed
15434 || cursor_type_changed
15435 /* Don't use try_window_reusing_current_matrix in this case
15436 because it can have changed the buffer. */
15437 || !NILP (Vwindow_scroll_functions)
15438 || !just_this_one_p
15439 || MINI_WINDOW_P (w)
15440 || !(used_current_matrix_p
15441 = try_window_reusing_current_matrix (w)))
15442 try_window (window, startp, 0);
15444 /* If new fonts have been loaded (due to fontsets), give up. We
15445 have to start a new redisplay since we need to re-adjust glyph
15446 matrices. */
15447 if (fonts_changed_p)
15448 goto need_larger_matrices;
15450 /* If cursor did not appear assume that the middle of the window is
15451 in the first line of the window. Do it again with the next line.
15452 (Imagine a window of height 100, displaying two lines of height
15453 60. Moving back 50 from it->last_visible_y will end in the first
15454 line.) */
15455 if (w->cursor.vpos < 0)
15457 if (!NILP (w->window_end_valid)
15458 && PT >= Z - XFASTINT (w->window_end_pos))
15460 clear_glyph_matrix (w->desired_matrix);
15461 move_it_by_lines (&it, 1);
15462 try_window (window, it.current.pos, 0);
15464 else if (PT < IT_CHARPOS (it))
15466 clear_glyph_matrix (w->desired_matrix);
15467 move_it_by_lines (&it, -1);
15468 try_window (window, it.current.pos, 0);
15470 else
15472 /* Not much we can do about it. */
15476 /* Consider the following case: Window starts at BEGV, there is
15477 invisible, intangible text at BEGV, so that display starts at
15478 some point START > BEGV. It can happen that we are called with
15479 PT somewhere between BEGV and START. Try to handle that case. */
15480 if (w->cursor.vpos < 0)
15482 struct glyph_row *row = w->current_matrix->rows;
15483 if (row->mode_line_p)
15484 ++row;
15485 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15488 if (!cursor_row_fully_visible_p (w, 0, 0))
15490 /* If vscroll is enabled, disable it and try again. */
15491 if (w->vscroll)
15493 w->vscroll = 0;
15494 clear_glyph_matrix (w->desired_matrix);
15495 goto recenter;
15498 /* If centering point failed to make the whole line visible,
15499 put point at the top instead. That has to make the whole line
15500 visible, if it can be done. */
15501 if (centering_position == 0)
15502 goto done;
15504 clear_glyph_matrix (w->desired_matrix);
15505 centering_position = 0;
15506 goto recenter;
15509 done:
15511 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15512 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15513 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15514 ? Qt : Qnil);
15516 /* Display the mode line, if we must. */
15517 if ((update_mode_line
15518 /* If window not full width, must redo its mode line
15519 if (a) the window to its side is being redone and
15520 (b) we do a frame-based redisplay. This is a consequence
15521 of how inverted lines are drawn in frame-based redisplay. */
15522 || (!just_this_one_p
15523 && !FRAME_WINDOW_P (f)
15524 && !WINDOW_FULL_WIDTH_P (w))
15525 /* Line number to display. */
15526 || INTEGERP (w->base_line_pos)
15527 /* Column number is displayed and different from the one displayed. */
15528 || (!NILP (w->column_number_displayed)
15529 && (XFASTINT (w->column_number_displayed) != current_column ())))
15530 /* This means that the window has a mode line. */
15531 && (WINDOW_WANTS_MODELINE_P (w)
15532 || WINDOW_WANTS_HEADER_LINE_P (w)))
15534 display_mode_lines (w);
15536 /* If mode line height has changed, arrange for a thorough
15537 immediate redisplay using the correct mode line height. */
15538 if (WINDOW_WANTS_MODELINE_P (w)
15539 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15541 fonts_changed_p = 1;
15542 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15543 = DESIRED_MODE_LINE_HEIGHT (w);
15546 /* If header line height has changed, arrange for a thorough
15547 immediate redisplay using the correct header line height. */
15548 if (WINDOW_WANTS_HEADER_LINE_P (w)
15549 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15551 fonts_changed_p = 1;
15552 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15553 = DESIRED_HEADER_LINE_HEIGHT (w);
15556 if (fonts_changed_p)
15557 goto need_larger_matrices;
15560 if (!line_number_displayed
15561 && !BUFFERP (w->base_line_pos))
15563 w->base_line_pos = Qnil;
15564 w->base_line_number = Qnil;
15567 finish_menu_bars:
15569 /* When we reach a frame's selected window, redo the frame's menu bar. */
15570 if (update_mode_line
15571 && EQ (FRAME_SELECTED_WINDOW (f), window))
15573 int redisplay_menu_p = 0;
15575 if (FRAME_WINDOW_P (f))
15577 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15578 || defined (HAVE_NS) || defined (USE_GTK)
15579 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15580 #else
15581 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15582 #endif
15584 else
15585 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15587 if (redisplay_menu_p)
15588 display_menu_bar (w);
15590 #ifdef HAVE_WINDOW_SYSTEM
15591 if (FRAME_WINDOW_P (f))
15593 #if defined (USE_GTK) || defined (HAVE_NS)
15594 if (FRAME_EXTERNAL_TOOL_BAR (f))
15595 redisplay_tool_bar (f);
15596 #else
15597 if (WINDOWP (f->tool_bar_window)
15598 && (FRAME_TOOL_BAR_LINES (f) > 0
15599 || !NILP (Vauto_resize_tool_bars))
15600 && redisplay_tool_bar (f))
15601 ignore_mouse_drag_p = 1;
15602 #endif
15604 #endif
15607 #ifdef HAVE_WINDOW_SYSTEM
15608 if (FRAME_WINDOW_P (f)
15609 && update_window_fringes (w, (just_this_one_p
15610 || (!used_current_matrix_p && !overlay_arrow_seen)
15611 || w->pseudo_window_p)))
15613 update_begin (f);
15614 BLOCK_INPUT;
15615 if (draw_window_fringes (w, 1))
15616 x_draw_vertical_border (w);
15617 UNBLOCK_INPUT;
15618 update_end (f);
15620 #endif /* HAVE_WINDOW_SYSTEM */
15622 /* We go to this label, with fonts_changed_p nonzero,
15623 if it is necessary to try again using larger glyph matrices.
15624 We have to redeem the scroll bar even in this case,
15625 because the loop in redisplay_internal expects that. */
15626 need_larger_matrices:
15628 finish_scroll_bars:
15630 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15632 /* Set the thumb's position and size. */
15633 set_vertical_scroll_bar (w);
15635 /* Note that we actually used the scroll bar attached to this
15636 window, so it shouldn't be deleted at the end of redisplay. */
15637 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15638 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15641 /* Restore current_buffer and value of point in it. The window
15642 update may have changed the buffer, so first make sure `opoint'
15643 is still valid (Bug#6177). */
15644 if (CHARPOS (opoint) < BEGV)
15645 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15646 else if (CHARPOS (opoint) > ZV)
15647 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15648 else
15649 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15651 set_buffer_internal_1 (old);
15652 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15653 shorter. This can be caused by log truncation in *Messages*. */
15654 if (CHARPOS (lpoint) <= ZV)
15655 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15657 unbind_to (count, Qnil);
15661 /* Build the complete desired matrix of WINDOW with a window start
15662 buffer position POS.
15664 Value is 1 if successful. It is zero if fonts were loaded during
15665 redisplay which makes re-adjusting glyph matrices necessary, and -1
15666 if point would appear in the scroll margins.
15667 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15668 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15669 set in FLAGS.) */
15672 try_window (Lisp_Object window, struct text_pos pos, int flags)
15674 struct window *w = XWINDOW (window);
15675 struct it it;
15676 struct glyph_row *last_text_row = NULL;
15677 struct frame *f = XFRAME (w->frame);
15679 /* Make POS the new window start. */
15680 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15682 /* Mark cursor position as unknown. No overlay arrow seen. */
15683 w->cursor.vpos = -1;
15684 overlay_arrow_seen = 0;
15686 /* Initialize iterator and info to start at POS. */
15687 start_display (&it, w, pos);
15691 /* Display all lines of W. */
15692 while (it.current_y < it.last_visible_y)
15694 if (display_line (&it))
15695 last_text_row = it.glyph_row - 1;
15696 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15697 return 0;
15699 #ifdef HAVE_XWIDGETS_xxx
15700 //currently this is needed to detect xwidget movement reliably. or probably not.
15701 printf("try_window\n");
15702 return 0;
15703 #endif
15705 /* Don't let the cursor end in the scroll margins. */
15706 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15707 && !MINI_WINDOW_P (w))
15709 int this_scroll_margin;
15711 if (scroll_margin > 0)
15713 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15714 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15716 else
15717 this_scroll_margin = 0;
15719 if ((w->cursor.y >= 0 /* not vscrolled */
15720 && w->cursor.y < this_scroll_margin
15721 && CHARPOS (pos) > BEGV
15722 && IT_CHARPOS (it) < ZV)
15723 /* rms: considering make_cursor_line_fully_visible_p here
15724 seems to give wrong results. We don't want to recenter
15725 when the last line is partly visible, we want to allow
15726 that case to be handled in the usual way. */
15727 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15729 w->cursor.vpos = -1;
15730 clear_glyph_matrix (w->desired_matrix);
15731 return -1;
15735 /* If bottom moved off end of frame, change mode line percentage. */
15736 if (XFASTINT (w->window_end_pos) <= 0
15737 && Z != IT_CHARPOS (it))
15738 w->update_mode_line = Qt;
15740 /* Set window_end_pos to the offset of the last character displayed
15741 on the window from the end of current_buffer. Set
15742 window_end_vpos to its row number. */
15743 if (last_text_row)
15745 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15746 w->window_end_bytepos
15747 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15748 w->window_end_pos
15749 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15750 w->window_end_vpos
15751 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15752 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15753 ->displays_text_p);
15755 else
15757 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15758 w->window_end_pos = make_number (Z - ZV);
15759 w->window_end_vpos = make_number (0);
15762 /* But that is not valid info until redisplay finishes. */
15763 w->window_end_valid = Qnil;
15764 return 1;
15769 /************************************************************************
15770 Window redisplay reusing current matrix when buffer has not changed
15771 ************************************************************************/
15773 /* Try redisplay of window W showing an unchanged buffer with a
15774 different window start than the last time it was displayed by
15775 reusing its current matrix. Value is non-zero if successful.
15776 W->start is the new window start. */
15778 static int
15779 try_window_reusing_current_matrix (struct window *w)
15781 struct frame *f = XFRAME (w->frame);
15782 struct glyph_row *bottom_row;
15783 struct it it;
15784 struct run run;
15785 struct text_pos start, new_start;
15786 int nrows_scrolled, i;
15787 struct glyph_row *last_text_row;
15788 struct glyph_row *last_reused_text_row;
15789 struct glyph_row *start_row;
15790 int start_vpos, min_y, max_y;
15792 #if GLYPH_DEBUG
15793 if (inhibit_try_window_reusing)
15794 return 0;
15795 #endif
15797 #ifdef HAVE_XWIDGETS_xxx
15798 //currently this is needed to detect xwidget movement reliably. or probably not.
15799 printf("try_window_reusing_current_matrix\n");
15800 return 0;
15801 #endif
15804 if (/* This function doesn't handle terminal frames. */
15805 !FRAME_WINDOW_P (f)
15806 /* Don't try to reuse the display if windows have been split
15807 or such. */
15808 || windows_or_buffers_changed
15809 || cursor_type_changed)
15810 return 0;
15812 /* Can't do this if region may have changed. */
15813 if ((!NILP (Vtransient_mark_mode)
15814 && !NILP (BVAR (current_buffer, mark_active)))
15815 || !NILP (w->region_showing)
15816 || !NILP (Vshow_trailing_whitespace))
15817 return 0;
15819 /* If top-line visibility has changed, give up. */
15820 if (WINDOW_WANTS_HEADER_LINE_P (w)
15821 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15822 return 0;
15824 /* Give up if old or new display is scrolled vertically. We could
15825 make this function handle this, but right now it doesn't. */
15826 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15827 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15828 return 0;
15830 /* The variable new_start now holds the new window start. The old
15831 start `start' can be determined from the current matrix. */
15832 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15833 start = start_row->minpos;
15834 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15836 /* Clear the desired matrix for the display below. */
15837 clear_glyph_matrix (w->desired_matrix);
15839 if (CHARPOS (new_start) <= CHARPOS (start))
15841 /* Don't use this method if the display starts with an ellipsis
15842 displayed for invisible text. It's not easy to handle that case
15843 below, and it's certainly not worth the effort since this is
15844 not a frequent case. */
15845 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15846 return 0;
15848 IF_DEBUG (debug_method_add (w, "twu1"));
15850 /* Display up to a row that can be reused. The variable
15851 last_text_row is set to the last row displayed that displays
15852 text. Note that it.vpos == 0 if or if not there is a
15853 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15854 start_display (&it, w, new_start);
15855 w->cursor.vpos = -1;
15856 last_text_row = last_reused_text_row = NULL;
15858 while (it.current_y < it.last_visible_y
15859 && !fonts_changed_p)
15861 /* If we have reached into the characters in the START row,
15862 that means the line boundaries have changed. So we
15863 can't start copying with the row START. Maybe it will
15864 work to start copying with the following row. */
15865 while (IT_CHARPOS (it) > CHARPOS (start))
15867 /* Advance to the next row as the "start". */
15868 start_row++;
15869 start = start_row->minpos;
15870 /* If there are no more rows to try, or just one, give up. */
15871 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15872 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15873 || CHARPOS (start) == ZV)
15875 clear_glyph_matrix (w->desired_matrix);
15876 return 0;
15879 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15881 /* If we have reached alignment,
15882 we can copy the rest of the rows. */
15883 if (IT_CHARPOS (it) == CHARPOS (start))
15884 break;
15886 if (display_line (&it))
15887 last_text_row = it.glyph_row - 1;
15890 /* A value of current_y < last_visible_y means that we stopped
15891 at the previous window start, which in turn means that we
15892 have at least one reusable row. */
15893 if (it.current_y < it.last_visible_y)
15895 struct glyph_row *row;
15897 /* IT.vpos always starts from 0; it counts text lines. */
15898 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15900 /* Find PT if not already found in the lines displayed. */
15901 if (w->cursor.vpos < 0)
15903 int dy = it.current_y - start_row->y;
15905 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15906 row = row_containing_pos (w, PT, row, NULL, dy);
15907 if (row)
15908 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15909 dy, nrows_scrolled);
15910 else
15912 clear_glyph_matrix (w->desired_matrix);
15913 return 0;
15917 /* Scroll the display. Do it before the current matrix is
15918 changed. The problem here is that update has not yet
15919 run, i.e. part of the current matrix is not up to date.
15920 scroll_run_hook will clear the cursor, and use the
15921 current matrix to get the height of the row the cursor is
15922 in. */
15923 run.current_y = start_row->y;
15924 run.desired_y = it.current_y;
15925 run.height = it.last_visible_y - it.current_y;
15927 if (run.height > 0 && run.current_y != run.desired_y)
15929 update_begin (f);
15930 FRAME_RIF (f)->update_window_begin_hook (w);
15931 FRAME_RIF (f)->clear_window_mouse_face (w);
15932 FRAME_RIF (f)->scroll_run_hook (w, &run);
15933 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15934 update_end (f);
15937 /* Shift current matrix down by nrows_scrolled lines. */
15938 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15939 rotate_matrix (w->current_matrix,
15940 start_vpos,
15941 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15942 nrows_scrolled);
15944 /* Disable lines that must be updated. */
15945 for (i = 0; i < nrows_scrolled; ++i)
15946 (start_row + i)->enabled_p = 0;
15948 /* Re-compute Y positions. */
15949 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15950 max_y = it.last_visible_y;
15951 for (row = start_row + nrows_scrolled;
15952 row < bottom_row;
15953 ++row)
15955 row->y = it.current_y;
15956 row->visible_height = row->height;
15958 if (row->y < min_y)
15959 row->visible_height -= min_y - row->y;
15960 if (row->y + row->height > max_y)
15961 row->visible_height -= row->y + row->height - max_y;
15962 if (row->fringe_bitmap_periodic_p)
15963 row->redraw_fringe_bitmaps_p = 1;
15965 it.current_y += row->height;
15967 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15968 last_reused_text_row = row;
15969 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15970 break;
15973 /* Disable lines in the current matrix which are now
15974 below the window. */
15975 for (++row; row < bottom_row; ++row)
15976 row->enabled_p = row->mode_line_p = 0;
15979 /* Update window_end_pos etc.; last_reused_text_row is the last
15980 reused row from the current matrix containing text, if any.
15981 The value of last_text_row is the last displayed line
15982 containing text. */
15983 if (last_reused_text_row)
15985 w->window_end_bytepos
15986 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15987 w->window_end_pos
15988 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15989 w->window_end_vpos
15990 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15991 w->current_matrix));
15993 else if (last_text_row)
15995 w->window_end_bytepos
15996 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15997 w->window_end_pos
15998 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15999 w->window_end_vpos
16000 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16002 else
16004 /* This window must be completely empty. */
16005 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16006 w->window_end_pos = make_number (Z - ZV);
16007 w->window_end_vpos = make_number (0);
16009 w->window_end_valid = Qnil;
16011 /* Update hint: don't try scrolling again in update_window. */
16012 w->desired_matrix->no_scrolling_p = 1;
16014 #if GLYPH_DEBUG
16015 debug_method_add (w, "try_window_reusing_current_matrix 1");
16016 #endif
16017 return 1;
16019 else if (CHARPOS (new_start) > CHARPOS (start))
16021 struct glyph_row *pt_row, *row;
16022 struct glyph_row *first_reusable_row;
16023 struct glyph_row *first_row_to_display;
16024 int dy;
16025 int yb = window_text_bottom_y (w);
16027 /* Find the row starting at new_start, if there is one. Don't
16028 reuse a partially visible line at the end. */
16029 first_reusable_row = start_row;
16030 while (first_reusable_row->enabled_p
16031 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16032 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16033 < CHARPOS (new_start)))
16034 ++first_reusable_row;
16036 /* Give up if there is no row to reuse. */
16037 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16038 || !first_reusable_row->enabled_p
16039 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16040 != CHARPOS (new_start)))
16041 return 0;
16043 /* We can reuse fully visible rows beginning with
16044 first_reusable_row to the end of the window. Set
16045 first_row_to_display to the first row that cannot be reused.
16046 Set pt_row to the row containing point, if there is any. */
16047 pt_row = NULL;
16048 for (first_row_to_display = first_reusable_row;
16049 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16050 ++first_row_to_display)
16052 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16053 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
16054 pt_row = first_row_to_display;
16057 /* Start displaying at the start of first_row_to_display. */
16058 xassert (first_row_to_display->y < yb);
16059 init_to_row_start (&it, w, first_row_to_display);
16061 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16062 - start_vpos);
16063 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16064 - nrows_scrolled);
16065 it.current_y = (first_row_to_display->y - first_reusable_row->y
16066 + WINDOW_HEADER_LINE_HEIGHT (w));
16068 /* Display lines beginning with first_row_to_display in the
16069 desired matrix. Set last_text_row to the last row displayed
16070 that displays text. */
16071 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16072 if (pt_row == NULL)
16073 w->cursor.vpos = -1;
16074 last_text_row = NULL;
16075 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16076 if (display_line (&it))
16077 last_text_row = it.glyph_row - 1;
16079 /* If point is in a reused row, adjust y and vpos of the cursor
16080 position. */
16081 if (pt_row)
16083 w->cursor.vpos -= nrows_scrolled;
16084 w->cursor.y -= first_reusable_row->y - start_row->y;
16087 /* Give up if point isn't in a row displayed or reused. (This
16088 also handles the case where w->cursor.vpos < nrows_scrolled
16089 after the calls to display_line, which can happen with scroll
16090 margins. See bug#1295.) */
16091 if (w->cursor.vpos < 0)
16093 clear_glyph_matrix (w->desired_matrix);
16094 return 0;
16097 /* Scroll the display. */
16098 run.current_y = first_reusable_row->y;
16099 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16100 run.height = it.last_visible_y - run.current_y;
16101 dy = run.current_y - run.desired_y;
16103 if (run.height)
16105 update_begin (f);
16106 FRAME_RIF (f)->update_window_begin_hook (w);
16107 FRAME_RIF (f)->clear_window_mouse_face (w);
16108 FRAME_RIF (f)->scroll_run_hook (w, &run);
16109 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16110 update_end (f);
16113 /* Adjust Y positions of reused rows. */
16114 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16115 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16116 max_y = it.last_visible_y;
16117 for (row = first_reusable_row; row < first_row_to_display; ++row)
16119 row->y -= dy;
16120 row->visible_height = row->height;
16121 if (row->y < min_y)
16122 row->visible_height -= min_y - row->y;
16123 if (row->y + row->height > max_y)
16124 row->visible_height -= row->y + row->height - max_y;
16125 if (row->fringe_bitmap_periodic_p)
16126 row->redraw_fringe_bitmaps_p = 1;
16129 /* Scroll the current matrix. */
16130 xassert (nrows_scrolled > 0);
16131 rotate_matrix (w->current_matrix,
16132 start_vpos,
16133 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16134 -nrows_scrolled);
16136 /* Disable rows not reused. */
16137 for (row -= nrows_scrolled; row < bottom_row; ++row)
16138 row->enabled_p = 0;
16140 /* Point may have moved to a different line, so we cannot assume that
16141 the previous cursor position is valid; locate the correct row. */
16142 if (pt_row)
16144 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16145 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
16146 row++)
16148 w->cursor.vpos++;
16149 w->cursor.y = row->y;
16151 if (row < bottom_row)
16153 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16154 struct glyph *end = glyph + row->used[TEXT_AREA];
16156 /* Can't use this optimization with bidi-reordered glyph
16157 rows, unless cursor is already at point. */
16158 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16160 if (!(w->cursor.hpos >= 0
16161 && w->cursor.hpos < row->used[TEXT_AREA]
16162 && BUFFERP (glyph->object)
16163 && glyph->charpos == PT))
16164 return 0;
16166 else
16167 for (; glyph < end
16168 && (!BUFFERP (glyph->object)
16169 || glyph->charpos < PT);
16170 glyph++)
16172 w->cursor.hpos++;
16173 w->cursor.x += glyph->pixel_width;
16178 /* Adjust window end. A null value of last_text_row means that
16179 the window end is in reused rows which in turn means that
16180 only its vpos can have changed. */
16181 if (last_text_row)
16183 w->window_end_bytepos
16184 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16185 w->window_end_pos
16186 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16187 w->window_end_vpos
16188 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16190 else
16192 w->window_end_vpos
16193 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16196 w->window_end_valid = Qnil;
16197 w->desired_matrix->no_scrolling_p = 1;
16199 #if GLYPH_DEBUG
16200 debug_method_add (w, "try_window_reusing_current_matrix 2");
16201 #endif
16202 return 1;
16205 return 0;
16210 /************************************************************************
16211 Window redisplay reusing current matrix when buffer has changed
16212 ************************************************************************/
16214 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16215 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16216 EMACS_INT *, EMACS_INT *);
16217 static struct glyph_row *
16218 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16219 struct glyph_row *);
16222 /* Return the last row in MATRIX displaying text. If row START is
16223 non-null, start searching with that row. IT gives the dimensions
16224 of the display. Value is null if matrix is empty; otherwise it is
16225 a pointer to the row found. */
16227 static struct glyph_row *
16228 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16229 struct glyph_row *start)
16231 struct glyph_row *row, *row_found;
16233 /* Set row_found to the last row in IT->w's current matrix
16234 displaying text. The loop looks funny but think of partially
16235 visible lines. */
16236 row_found = NULL;
16237 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16238 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16240 xassert (row->enabled_p);
16241 row_found = row;
16242 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16243 break;
16244 ++row;
16247 return row_found;
16251 /* Return the last row in the current matrix of W that is not affected
16252 by changes at the start of current_buffer that occurred since W's
16253 current matrix was built. Value is null if no such row exists.
16255 BEG_UNCHANGED us the number of characters unchanged at the start of
16256 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16257 first changed character in current_buffer. Characters at positions <
16258 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16259 when the current matrix was built. */
16261 static struct glyph_row *
16262 find_last_unchanged_at_beg_row (struct window *w)
16264 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16265 struct glyph_row *row;
16266 struct glyph_row *row_found = NULL;
16267 int yb = window_text_bottom_y (w);
16269 /* Find the last row displaying unchanged text. */
16270 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16271 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16272 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16273 ++row)
16275 if (/* If row ends before first_changed_pos, it is unchanged,
16276 except in some case. */
16277 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16278 /* When row ends in ZV and we write at ZV it is not
16279 unchanged. */
16280 && !row->ends_at_zv_p
16281 /* When first_changed_pos is the end of a continued line,
16282 row is not unchanged because it may be no longer
16283 continued. */
16284 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16285 && (row->continued_p
16286 || row->exact_window_width_line_p)))
16287 row_found = row;
16289 /* Stop if last visible row. */
16290 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16291 break;
16294 return row_found;
16298 /* Find the first glyph row in the current matrix of W that is not
16299 affected by changes at the end of current_buffer since the
16300 time W's current matrix was built.
16302 Return in *DELTA the number of chars by which buffer positions in
16303 unchanged text at the end of current_buffer must be adjusted.
16305 Return in *DELTA_BYTES the corresponding number of bytes.
16307 Value is null if no such row exists, i.e. all rows are affected by
16308 changes. */
16310 static struct glyph_row *
16311 find_first_unchanged_at_end_row (struct window *w,
16312 EMACS_INT *delta, EMACS_INT *delta_bytes)
16314 struct glyph_row *row;
16315 struct glyph_row *row_found = NULL;
16317 *delta = *delta_bytes = 0;
16319 /* Display must not have been paused, otherwise the current matrix
16320 is not up to date. */
16321 eassert (!NILP (w->window_end_valid));
16323 /* A value of window_end_pos >= END_UNCHANGED means that the window
16324 end is in the range of changed text. If so, there is no
16325 unchanged row at the end of W's current matrix. */
16326 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16327 return NULL;
16329 /* Set row to the last row in W's current matrix displaying text. */
16330 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16332 /* If matrix is entirely empty, no unchanged row exists. */
16333 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16335 /* The value of row is the last glyph row in the matrix having a
16336 meaningful buffer position in it. The end position of row
16337 corresponds to window_end_pos. This allows us to translate
16338 buffer positions in the current matrix to current buffer
16339 positions for characters not in changed text. */
16340 EMACS_INT Z_old =
16341 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16342 EMACS_INT Z_BYTE_old =
16343 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16344 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16345 struct glyph_row *first_text_row
16346 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16348 *delta = Z - Z_old;
16349 *delta_bytes = Z_BYTE - Z_BYTE_old;
16351 /* Set last_unchanged_pos to the buffer position of the last
16352 character in the buffer that has not been changed. Z is the
16353 index + 1 of the last character in current_buffer, i.e. by
16354 subtracting END_UNCHANGED we get the index of the last
16355 unchanged character, and we have to add BEG to get its buffer
16356 position. */
16357 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16358 last_unchanged_pos_old = last_unchanged_pos - *delta;
16360 /* Search backward from ROW for a row displaying a line that
16361 starts at a minimum position >= last_unchanged_pos_old. */
16362 for (; row > first_text_row; --row)
16364 /* This used to abort, but it can happen.
16365 It is ok to just stop the search instead here. KFS. */
16366 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16367 break;
16369 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16370 row_found = row;
16374 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16376 return row_found;
16380 /* Make sure that glyph rows in the current matrix of window W
16381 reference the same glyph memory as corresponding rows in the
16382 frame's frame matrix. This function is called after scrolling W's
16383 current matrix on a terminal frame in try_window_id and
16384 try_window_reusing_current_matrix. */
16386 static void
16387 sync_frame_with_window_matrix_rows (struct window *w)
16389 struct frame *f = XFRAME (w->frame);
16390 struct glyph_row *window_row, *window_row_end, *frame_row;
16392 /* Preconditions: W must be a leaf window and full-width. Its frame
16393 must have a frame matrix. */
16394 xassert (NILP (w->hchild) && NILP (w->vchild));
16395 xassert (WINDOW_FULL_WIDTH_P (w));
16396 xassert (!FRAME_WINDOW_P (f));
16398 /* If W is a full-width window, glyph pointers in W's current matrix
16399 have, by definition, to be the same as glyph pointers in the
16400 corresponding frame matrix. Note that frame matrices have no
16401 marginal areas (see build_frame_matrix). */
16402 window_row = w->current_matrix->rows;
16403 window_row_end = window_row + w->current_matrix->nrows;
16404 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16405 while (window_row < window_row_end)
16407 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16408 struct glyph *end = window_row->glyphs[LAST_AREA];
16410 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16411 frame_row->glyphs[TEXT_AREA] = start;
16412 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16413 frame_row->glyphs[LAST_AREA] = end;
16415 /* Disable frame rows whose corresponding window rows have
16416 been disabled in try_window_id. */
16417 if (!window_row->enabled_p)
16418 frame_row->enabled_p = 0;
16420 ++window_row, ++frame_row;
16425 /* Find the glyph row in window W containing CHARPOS. Consider all
16426 rows between START and END (not inclusive). END null means search
16427 all rows to the end of the display area of W. Value is the row
16428 containing CHARPOS or null. */
16430 struct glyph_row *
16431 row_containing_pos (struct window *w, EMACS_INT charpos,
16432 struct glyph_row *start, struct glyph_row *end, int dy)
16434 struct glyph_row *row = start;
16435 struct glyph_row *best_row = NULL;
16436 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16437 int last_y;
16439 /* If we happen to start on a header-line, skip that. */
16440 if (row->mode_line_p)
16441 ++row;
16443 if ((end && row >= end) || !row->enabled_p)
16444 return NULL;
16446 last_y = window_text_bottom_y (w) - dy;
16448 while (1)
16450 /* Give up if we have gone too far. */
16451 if (end && row >= end)
16452 return NULL;
16453 /* This formerly returned if they were equal.
16454 I think that both quantities are of a "last plus one" type;
16455 if so, when they are equal, the row is within the screen. -- rms. */
16456 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16457 return NULL;
16459 /* If it is in this row, return this row. */
16460 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16461 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16462 /* The end position of a row equals the start
16463 position of the next row. If CHARPOS is there, we
16464 would rather display it in the next line, except
16465 when this line ends in ZV. */
16466 && !row->ends_at_zv_p
16467 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16468 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16470 struct glyph *g;
16472 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16473 || (!best_row && !row->continued_p))
16474 return row;
16475 /* In bidi-reordered rows, there could be several rows
16476 occluding point, all of them belonging to the same
16477 continued line. We need to find the row which fits
16478 CHARPOS the best. */
16479 for (g = row->glyphs[TEXT_AREA];
16480 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16481 g++)
16483 if (!STRINGP (g->object))
16485 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16487 mindif = eabs (g->charpos - charpos);
16488 best_row = row;
16489 /* Exact match always wins. */
16490 if (mindif == 0)
16491 return best_row;
16496 else if (best_row && !row->continued_p)
16497 return best_row;
16498 ++row;
16503 /* Try to redisplay window W by reusing its existing display. W's
16504 current matrix must be up to date when this function is called,
16505 i.e. window_end_valid must not be nil.
16507 Value is
16509 1 if display has been updated
16510 0 if otherwise unsuccessful
16511 -1 if redisplay with same window start is known not to succeed
16513 The following steps are performed:
16515 1. Find the last row in the current matrix of W that is not
16516 affected by changes at the start of current_buffer. If no such row
16517 is found, give up.
16519 2. Find the first row in W's current matrix that is not affected by
16520 changes at the end of current_buffer. Maybe there is no such row.
16522 3. Display lines beginning with the row + 1 found in step 1 to the
16523 row found in step 2 or, if step 2 didn't find a row, to the end of
16524 the window.
16526 4. If cursor is not known to appear on the window, give up.
16528 5. If display stopped at the row found in step 2, scroll the
16529 display and current matrix as needed.
16531 6. Maybe display some lines at the end of W, if we must. This can
16532 happen under various circumstances, like a partially visible line
16533 becoming fully visible, or because newly displayed lines are displayed
16534 in smaller font sizes.
16536 7. Update W's window end information. */
16538 static int
16539 try_window_id (struct window *w)
16541 struct frame *f = XFRAME (w->frame);
16542 struct glyph_matrix *current_matrix = w->current_matrix;
16543 struct glyph_matrix *desired_matrix = w->desired_matrix;
16544 struct glyph_row *last_unchanged_at_beg_row;
16545 struct glyph_row *first_unchanged_at_end_row;
16546 struct glyph_row *row;
16547 struct glyph_row *bottom_row;
16548 int bottom_vpos;
16549 struct it it;
16550 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16551 int dvpos, dy;
16552 struct text_pos start_pos;
16553 struct run run;
16554 int first_unchanged_at_end_vpos = 0;
16555 struct glyph_row *last_text_row, *last_text_row_at_end;
16556 struct text_pos start;
16557 EMACS_INT first_changed_charpos, last_changed_charpos;
16559 #if GLYPH_DEBUG
16560 if (inhibit_try_window_id)
16561 return 0;
16562 #endif
16564 #ifdef HAVE_XWIDGETS_xxx
16565 //maybe needed for proper xwidget movement
16566 printf("try_window_id\n");
16567 return -1;
16568 #endif
16571 /* This is handy for debugging. */
16572 #if 0
16573 #define GIVE_UP(X) \
16574 do { \
16575 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16576 return 0; \
16577 } while (0)
16578 #else
16579 #define GIVE_UP(X) return 0
16580 #endif
16582 SET_TEXT_POS_FROM_MARKER (start, w->start);
16584 /* Don't use this for mini-windows because these can show
16585 messages and mini-buffers, and we don't handle that here. */
16586 if (MINI_WINDOW_P (w))
16587 GIVE_UP (1);
16589 /* This flag is used to prevent redisplay optimizations. */
16590 if (windows_or_buffers_changed || cursor_type_changed)
16591 GIVE_UP (2);
16593 /* Verify that narrowing has not changed.
16594 Also verify that we were not told to prevent redisplay optimizations.
16595 It would be nice to further
16596 reduce the number of cases where this prevents try_window_id. */
16597 if (current_buffer->clip_changed
16598 || current_buffer->prevent_redisplay_optimizations_p)
16599 GIVE_UP (3);
16601 /* Window must either use window-based redisplay or be full width. */
16602 if (!FRAME_WINDOW_P (f)
16603 && (!FRAME_LINE_INS_DEL_OK (f)
16604 || !WINDOW_FULL_WIDTH_P (w)))
16605 GIVE_UP (4);
16607 /* Give up if point is known NOT to appear in W. */
16608 if (PT < CHARPOS (start))
16609 GIVE_UP (5);
16611 /* Another way to prevent redisplay optimizations. */
16612 if (XFASTINT (w->last_modified) == 0)
16613 GIVE_UP (6);
16615 /* Verify that window is not hscrolled. */
16616 if (XFASTINT (w->hscroll) != 0)
16617 GIVE_UP (7);
16619 /* Verify that display wasn't paused. */
16620 if (NILP (w->window_end_valid))
16621 GIVE_UP (8);
16623 /* Can't use this if highlighting a region because a cursor movement
16624 will do more than just set the cursor. */
16625 if (!NILP (Vtransient_mark_mode)
16626 && !NILP (BVAR (current_buffer, mark_active)))
16627 GIVE_UP (9);
16629 /* Likewise if highlighting trailing whitespace. */
16630 if (!NILP (Vshow_trailing_whitespace))
16631 GIVE_UP (11);
16633 /* Likewise if showing a region. */
16634 if (!NILP (w->region_showing))
16635 GIVE_UP (10);
16637 /* Can't use this if overlay arrow position and/or string have
16638 changed. */
16639 if (overlay_arrows_changed_p ())
16640 GIVE_UP (12);
16642 /* When word-wrap is on, adding a space to the first word of a
16643 wrapped line can change the wrap position, altering the line
16644 above it. It might be worthwhile to handle this more
16645 intelligently, but for now just redisplay from scratch. */
16646 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16647 GIVE_UP (21);
16649 /* Under bidi reordering, adding or deleting a character in the
16650 beginning of a paragraph, before the first strong directional
16651 character, can change the base direction of the paragraph (unless
16652 the buffer specifies a fixed paragraph direction), which will
16653 require to redisplay the whole paragraph. It might be worthwhile
16654 to find the paragraph limits and widen the range of redisplayed
16655 lines to that, but for now just give up this optimization and
16656 redisplay from scratch. */
16657 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16658 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16659 GIVE_UP (22);
16661 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16662 only if buffer has really changed. The reason is that the gap is
16663 initially at Z for freshly visited files. The code below would
16664 set end_unchanged to 0 in that case. */
16665 if (MODIFF > SAVE_MODIFF
16666 /* This seems to happen sometimes after saving a buffer. */
16667 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16669 if (GPT - BEG < BEG_UNCHANGED)
16670 BEG_UNCHANGED = GPT - BEG;
16671 if (Z - GPT < END_UNCHANGED)
16672 END_UNCHANGED = Z - GPT;
16675 /* The position of the first and last character that has been changed. */
16676 first_changed_charpos = BEG + BEG_UNCHANGED;
16677 last_changed_charpos = Z - END_UNCHANGED;
16679 /* If window starts after a line end, and the last change is in
16680 front of that newline, then changes don't affect the display.
16681 This case happens with stealth-fontification. Note that although
16682 the display is unchanged, glyph positions in the matrix have to
16683 be adjusted, of course. */
16684 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16685 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16686 && ((last_changed_charpos < CHARPOS (start)
16687 && CHARPOS (start) == BEGV)
16688 || (last_changed_charpos < CHARPOS (start) - 1
16689 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16691 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16692 struct glyph_row *r0;
16694 /* Compute how many chars/bytes have been added to or removed
16695 from the buffer. */
16696 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16697 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16698 Z_delta = Z - Z_old;
16699 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16701 /* Give up if PT is not in the window. Note that it already has
16702 been checked at the start of try_window_id that PT is not in
16703 front of the window start. */
16704 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16705 GIVE_UP (13);
16707 /* If window start is unchanged, we can reuse the whole matrix
16708 as is, after adjusting glyph positions. No need to compute
16709 the window end again, since its offset from Z hasn't changed. */
16710 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16711 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16712 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16713 /* PT must not be in a partially visible line. */
16714 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16715 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16717 /* Adjust positions in the glyph matrix. */
16718 if (Z_delta || Z_delta_bytes)
16720 struct glyph_row *r1
16721 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16722 increment_matrix_positions (w->current_matrix,
16723 MATRIX_ROW_VPOS (r0, current_matrix),
16724 MATRIX_ROW_VPOS (r1, current_matrix),
16725 Z_delta, Z_delta_bytes);
16728 /* Set the cursor. */
16729 row = row_containing_pos (w, PT, r0, NULL, 0);
16730 if (row)
16731 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16732 else
16733 abort ();
16734 return 1;
16738 /* Handle the case that changes are all below what is displayed in
16739 the window, and that PT is in the window. This shortcut cannot
16740 be taken if ZV is visible in the window, and text has been added
16741 there that is visible in the window. */
16742 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16743 /* ZV is not visible in the window, or there are no
16744 changes at ZV, actually. */
16745 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16746 || first_changed_charpos == last_changed_charpos))
16748 struct glyph_row *r0;
16750 /* Give up if PT is not in the window. Note that it already has
16751 been checked at the start of try_window_id that PT is not in
16752 front of the window start. */
16753 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16754 GIVE_UP (14);
16756 /* If window start is unchanged, we can reuse the whole matrix
16757 as is, without changing glyph positions since no text has
16758 been added/removed in front of the window end. */
16759 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16760 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16761 /* PT must not be in a partially visible line. */
16762 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16763 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16765 /* We have to compute the window end anew since text
16766 could have been added/removed after it. */
16767 w->window_end_pos
16768 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16769 w->window_end_bytepos
16770 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16772 /* Set the cursor. */
16773 row = row_containing_pos (w, PT, r0, NULL, 0);
16774 if (row)
16775 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16776 else
16777 abort ();
16778 return 2;
16782 /* Give up if window start is in the changed area.
16784 The condition used to read
16786 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16788 but why that was tested escapes me at the moment. */
16789 if (CHARPOS (start) >= first_changed_charpos
16790 && CHARPOS (start) <= last_changed_charpos)
16791 GIVE_UP (15);
16793 /* Check that window start agrees with the start of the first glyph
16794 row in its current matrix. Check this after we know the window
16795 start is not in changed text, otherwise positions would not be
16796 comparable. */
16797 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16798 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16799 GIVE_UP (16);
16801 /* Give up if the window ends in strings. Overlay strings
16802 at the end are difficult to handle, so don't try. */
16803 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16804 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16805 GIVE_UP (20);
16807 /* Compute the position at which we have to start displaying new
16808 lines. Some of the lines at the top of the window might be
16809 reusable because they are not displaying changed text. Find the
16810 last row in W's current matrix not affected by changes at the
16811 start of current_buffer. Value is null if changes start in the
16812 first line of window. */
16813 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16814 if (last_unchanged_at_beg_row)
16816 /* Avoid starting to display in the moddle of a character, a TAB
16817 for instance. This is easier than to set up the iterator
16818 exactly, and it's not a frequent case, so the additional
16819 effort wouldn't really pay off. */
16820 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16821 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16822 && last_unchanged_at_beg_row > w->current_matrix->rows)
16823 --last_unchanged_at_beg_row;
16825 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16826 GIVE_UP (17);
16828 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16829 GIVE_UP (18);
16830 start_pos = it.current.pos;
16832 /* Start displaying new lines in the desired matrix at the same
16833 vpos we would use in the current matrix, i.e. below
16834 last_unchanged_at_beg_row. */
16835 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16836 current_matrix);
16837 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16838 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16840 xassert (it.hpos == 0 && it.current_x == 0);
16842 else
16844 /* There are no reusable lines at the start of the window.
16845 Start displaying in the first text line. */
16846 start_display (&it, w, start);
16847 it.vpos = it.first_vpos;
16848 start_pos = it.current.pos;
16851 /* Find the first row that is not affected by changes at the end of
16852 the buffer. Value will be null if there is no unchanged row, in
16853 which case we must redisplay to the end of the window. delta
16854 will be set to the value by which buffer positions beginning with
16855 first_unchanged_at_end_row have to be adjusted due to text
16856 changes. */
16857 first_unchanged_at_end_row
16858 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16859 IF_DEBUG (debug_delta = delta);
16860 IF_DEBUG (debug_delta_bytes = delta_bytes);
16862 /* Set stop_pos to the buffer position up to which we will have to
16863 display new lines. If first_unchanged_at_end_row != NULL, this
16864 is the buffer position of the start of the line displayed in that
16865 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16866 that we don't stop at a buffer position. */
16867 stop_pos = 0;
16868 if (first_unchanged_at_end_row)
16870 xassert (last_unchanged_at_beg_row == NULL
16871 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16873 /* If this is a continuation line, move forward to the next one
16874 that isn't. Changes in lines above affect this line.
16875 Caution: this may move first_unchanged_at_end_row to a row
16876 not displaying text. */
16877 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16878 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16879 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16880 < it.last_visible_y))
16881 ++first_unchanged_at_end_row;
16883 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16884 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16885 >= it.last_visible_y))
16886 first_unchanged_at_end_row = NULL;
16887 else
16889 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16890 + delta);
16891 first_unchanged_at_end_vpos
16892 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16893 xassert (stop_pos >= Z - END_UNCHANGED);
16896 else if (last_unchanged_at_beg_row == NULL)
16897 GIVE_UP (19);
16900 #if GLYPH_DEBUG
16902 /* Either there is no unchanged row at the end, or the one we have
16903 now displays text. This is a necessary condition for the window
16904 end pos calculation at the end of this function. */
16905 xassert (first_unchanged_at_end_row == NULL
16906 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16908 debug_last_unchanged_at_beg_vpos
16909 = (last_unchanged_at_beg_row
16910 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16911 : -1);
16912 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16914 #endif /* GLYPH_DEBUG != 0 */
16917 /* Display new lines. Set last_text_row to the last new line
16918 displayed which has text on it, i.e. might end up as being the
16919 line where the window_end_vpos is. */
16920 w->cursor.vpos = -1;
16921 last_text_row = NULL;
16922 overlay_arrow_seen = 0;
16923 while (it.current_y < it.last_visible_y
16924 && !fonts_changed_p
16925 && (first_unchanged_at_end_row == NULL
16926 || IT_CHARPOS (it) < stop_pos))
16928 if (display_line (&it))
16929 last_text_row = it.glyph_row - 1;
16932 if (fonts_changed_p)
16933 return -1;
16936 /* Compute differences in buffer positions, y-positions etc. for
16937 lines reused at the bottom of the window. Compute what we can
16938 scroll. */
16939 if (first_unchanged_at_end_row
16940 /* No lines reused because we displayed everything up to the
16941 bottom of the window. */
16942 && it.current_y < it.last_visible_y)
16944 dvpos = (it.vpos
16945 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16946 current_matrix));
16947 dy = it.current_y - first_unchanged_at_end_row->y;
16948 run.current_y = first_unchanged_at_end_row->y;
16949 run.desired_y = run.current_y + dy;
16950 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16952 else
16954 delta = delta_bytes = dvpos = dy
16955 = run.current_y = run.desired_y = run.height = 0;
16956 first_unchanged_at_end_row = NULL;
16958 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16961 /* Find the cursor if not already found. We have to decide whether
16962 PT will appear on this window (it sometimes doesn't, but this is
16963 not a very frequent case.) This decision has to be made before
16964 the current matrix is altered. A value of cursor.vpos < 0 means
16965 that PT is either in one of the lines beginning at
16966 first_unchanged_at_end_row or below the window. Don't care for
16967 lines that might be displayed later at the window end; as
16968 mentioned, this is not a frequent case. */
16969 if (w->cursor.vpos < 0)
16971 /* Cursor in unchanged rows at the top? */
16972 if (PT < CHARPOS (start_pos)
16973 && last_unchanged_at_beg_row)
16975 row = row_containing_pos (w, PT,
16976 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16977 last_unchanged_at_beg_row + 1, 0);
16978 if (row)
16979 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16982 /* Start from first_unchanged_at_end_row looking for PT. */
16983 else if (first_unchanged_at_end_row)
16985 row = row_containing_pos (w, PT - delta,
16986 first_unchanged_at_end_row, NULL, 0);
16987 if (row)
16988 set_cursor_from_row (w, row, w->current_matrix, delta,
16989 delta_bytes, dy, dvpos);
16992 /* Give up if cursor was not found. */
16993 if (w->cursor.vpos < 0)
16995 clear_glyph_matrix (w->desired_matrix);
16996 return -1;
17000 /* Don't let the cursor end in the scroll margins. */
17002 int this_scroll_margin, cursor_height;
17004 this_scroll_margin =
17005 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17006 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17007 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17009 if ((w->cursor.y < this_scroll_margin
17010 && CHARPOS (start) > BEGV)
17011 /* Old redisplay didn't take scroll margin into account at the bottom,
17012 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17013 || (w->cursor.y + (make_cursor_line_fully_visible_p
17014 ? cursor_height + this_scroll_margin
17015 : 1)) > it.last_visible_y)
17017 w->cursor.vpos = -1;
17018 clear_glyph_matrix (w->desired_matrix);
17019 return -1;
17023 /* Scroll the display. Do it before changing the current matrix so
17024 that xterm.c doesn't get confused about where the cursor glyph is
17025 found. */
17026 if (dy && run.height)
17028 update_begin (f);
17030 if (FRAME_WINDOW_P (f))
17032 FRAME_RIF (f)->update_window_begin_hook (w);
17033 FRAME_RIF (f)->clear_window_mouse_face (w);
17034 FRAME_RIF (f)->scroll_run_hook (w, &run);
17035 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17037 else
17039 /* Terminal frame. In this case, dvpos gives the number of
17040 lines to scroll by; dvpos < 0 means scroll up. */
17041 int from_vpos
17042 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17043 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17044 int end = (WINDOW_TOP_EDGE_LINE (w)
17045 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17046 + window_internal_height (w));
17048 #if defined (HAVE_GPM) || defined (MSDOS)
17049 x_clear_window_mouse_face (w);
17050 #endif
17051 /* Perform the operation on the screen. */
17052 if (dvpos > 0)
17054 /* Scroll last_unchanged_at_beg_row to the end of the
17055 window down dvpos lines. */
17056 set_terminal_window (f, end);
17058 /* On dumb terminals delete dvpos lines at the end
17059 before inserting dvpos empty lines. */
17060 if (!FRAME_SCROLL_REGION_OK (f))
17061 ins_del_lines (f, end - dvpos, -dvpos);
17063 /* Insert dvpos empty lines in front of
17064 last_unchanged_at_beg_row. */
17065 ins_del_lines (f, from, dvpos);
17067 else if (dvpos < 0)
17069 /* Scroll up last_unchanged_at_beg_vpos to the end of
17070 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17071 set_terminal_window (f, end);
17073 /* Delete dvpos lines in front of
17074 last_unchanged_at_beg_vpos. ins_del_lines will set
17075 the cursor to the given vpos and emit |dvpos| delete
17076 line sequences. */
17077 ins_del_lines (f, from + dvpos, dvpos);
17079 /* On a dumb terminal insert dvpos empty lines at the
17080 end. */
17081 if (!FRAME_SCROLL_REGION_OK (f))
17082 ins_del_lines (f, end + dvpos, -dvpos);
17085 set_terminal_window (f, 0);
17088 update_end (f);
17091 /* Shift reused rows of the current matrix to the right position.
17092 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17093 text. */
17094 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17095 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17096 if (dvpos < 0)
17098 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17099 bottom_vpos, dvpos);
17100 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17101 bottom_vpos, 0);
17103 else if (dvpos > 0)
17105 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17106 bottom_vpos, dvpos);
17107 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17108 first_unchanged_at_end_vpos + dvpos, 0);
17111 /* For frame-based redisplay, make sure that current frame and window
17112 matrix are in sync with respect to glyph memory. */
17113 if (!FRAME_WINDOW_P (f))
17114 sync_frame_with_window_matrix_rows (w);
17116 /* Adjust buffer positions in reused rows. */
17117 if (delta || delta_bytes)
17118 increment_matrix_positions (current_matrix,
17119 first_unchanged_at_end_vpos + dvpos,
17120 bottom_vpos, delta, delta_bytes);
17122 /* Adjust Y positions. */
17123 if (dy)
17124 shift_glyph_matrix (w, current_matrix,
17125 first_unchanged_at_end_vpos + dvpos,
17126 bottom_vpos, dy);
17128 if (first_unchanged_at_end_row)
17130 first_unchanged_at_end_row += dvpos;
17131 if (first_unchanged_at_end_row->y >= it.last_visible_y
17132 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17133 first_unchanged_at_end_row = NULL;
17136 /* If scrolling up, there may be some lines to display at the end of
17137 the window. */
17138 last_text_row_at_end = NULL;
17139 if (dy < 0)
17141 /* Scrolling up can leave for example a partially visible line
17142 at the end of the window to be redisplayed. */
17143 /* Set last_row to the glyph row in the current matrix where the
17144 window end line is found. It has been moved up or down in
17145 the matrix by dvpos. */
17146 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17147 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17149 /* If last_row is the window end line, it should display text. */
17150 xassert (last_row->displays_text_p);
17152 /* If window end line was partially visible before, begin
17153 displaying at that line. Otherwise begin displaying with the
17154 line following it. */
17155 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17157 init_to_row_start (&it, w, last_row);
17158 it.vpos = last_vpos;
17159 it.current_y = last_row->y;
17161 else
17163 init_to_row_end (&it, w, last_row);
17164 it.vpos = 1 + last_vpos;
17165 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17166 ++last_row;
17169 /* We may start in a continuation line. If so, we have to
17170 get the right continuation_lines_width and current_x. */
17171 it.continuation_lines_width = last_row->continuation_lines_width;
17172 it.hpos = it.current_x = 0;
17174 /* Display the rest of the lines at the window end. */
17175 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17176 while (it.current_y < it.last_visible_y
17177 && !fonts_changed_p)
17179 /* Is it always sure that the display agrees with lines in
17180 the current matrix? I don't think so, so we mark rows
17181 displayed invalid in the current matrix by setting their
17182 enabled_p flag to zero. */
17183 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17184 if (display_line (&it))
17185 last_text_row_at_end = it.glyph_row - 1;
17189 /* Update window_end_pos and window_end_vpos. */
17190 if (first_unchanged_at_end_row
17191 && !last_text_row_at_end)
17193 /* Window end line if one of the preserved rows from the current
17194 matrix. Set row to the last row displaying text in current
17195 matrix starting at first_unchanged_at_end_row, after
17196 scrolling. */
17197 xassert (first_unchanged_at_end_row->displays_text_p);
17198 row = find_last_row_displaying_text (w->current_matrix, &it,
17199 first_unchanged_at_end_row);
17200 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17202 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17203 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17204 w->window_end_vpos
17205 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17206 xassert (w->window_end_bytepos >= 0);
17207 IF_DEBUG (debug_method_add (w, "A"));
17209 else if (last_text_row_at_end)
17211 w->window_end_pos
17212 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17213 w->window_end_bytepos
17214 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17215 w->window_end_vpos
17216 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17217 xassert (w->window_end_bytepos >= 0);
17218 IF_DEBUG (debug_method_add (w, "B"));
17220 else if (last_text_row)
17222 /* We have displayed either to the end of the window or at the
17223 end of the window, i.e. the last row with text is to be found
17224 in the desired matrix. */
17225 w->window_end_pos
17226 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17227 w->window_end_bytepos
17228 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17229 w->window_end_vpos
17230 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17231 xassert (w->window_end_bytepos >= 0);
17233 else if (first_unchanged_at_end_row == NULL
17234 && last_text_row == NULL
17235 && last_text_row_at_end == NULL)
17237 /* Displayed to end of window, but no line containing text was
17238 displayed. Lines were deleted at the end of the window. */
17239 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17240 int vpos = XFASTINT (w->window_end_vpos);
17241 struct glyph_row *current_row = current_matrix->rows + vpos;
17242 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17244 for (row = NULL;
17245 row == NULL && vpos >= first_vpos;
17246 --vpos, --current_row, --desired_row)
17248 if (desired_row->enabled_p)
17250 if (desired_row->displays_text_p)
17251 row = desired_row;
17253 else if (current_row->displays_text_p)
17254 row = current_row;
17257 xassert (row != NULL);
17258 w->window_end_vpos = make_number (vpos + 1);
17259 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17260 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17261 xassert (w->window_end_bytepos >= 0);
17262 IF_DEBUG (debug_method_add (w, "C"));
17264 else
17265 abort ();
17267 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17268 debug_end_vpos = XFASTINT (w->window_end_vpos));
17270 /* Record that display has not been completed. */
17271 w->window_end_valid = Qnil;
17272 w->desired_matrix->no_scrolling_p = 1;
17273 return 3;
17275 #undef GIVE_UP
17280 /***********************************************************************
17281 More debugging support
17282 ***********************************************************************/
17284 #if GLYPH_DEBUG
17286 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17287 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17288 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17291 /* Dump the contents of glyph matrix MATRIX on stderr.
17293 GLYPHS 0 means don't show glyph contents.
17294 GLYPHS 1 means show glyphs in short form
17295 GLYPHS > 1 means show glyphs in long form. */
17297 void
17298 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17300 int i;
17301 for (i = 0; i < matrix->nrows; ++i)
17302 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17306 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17307 the glyph row and area where the glyph comes from. */
17309 void
17310 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17312 if (glyph->type == CHAR_GLYPH)
17314 fprintf (stderr,
17315 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17316 glyph - row->glyphs[TEXT_AREA],
17317 'C',
17318 glyph->charpos,
17319 (BUFFERP (glyph->object)
17320 ? 'B'
17321 : (STRINGP (glyph->object)
17322 ? 'S'
17323 : '-')),
17324 glyph->pixel_width,
17325 glyph->u.ch,
17326 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17327 ? glyph->u.ch
17328 : '.'),
17329 glyph->face_id,
17330 glyph->left_box_line_p,
17331 glyph->right_box_line_p);
17333 else if (glyph->type == STRETCH_GLYPH)
17335 fprintf (stderr,
17336 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17337 glyph - row->glyphs[TEXT_AREA],
17338 'S',
17339 glyph->charpos,
17340 (BUFFERP (glyph->object)
17341 ? 'B'
17342 : (STRINGP (glyph->object)
17343 ? 'S'
17344 : '-')),
17345 glyph->pixel_width,
17347 '.',
17348 glyph->face_id,
17349 glyph->left_box_line_p,
17350 glyph->right_box_line_p);
17352 else if (glyph->type == IMAGE_GLYPH)
17354 fprintf (stderr,
17355 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17356 glyph - row->glyphs[TEXT_AREA],
17357 'I',
17358 glyph->charpos,
17359 (BUFFERP (glyph->object)
17360 ? 'B'
17361 : (STRINGP (glyph->object)
17362 ? 'S'
17363 : '-')),
17364 glyph->pixel_width,
17365 glyph->u.img_id,
17366 '.',
17367 glyph->face_id,
17368 glyph->left_box_line_p,
17369 glyph->right_box_line_p);
17371 else if (glyph->type == COMPOSITE_GLYPH)
17373 fprintf (stderr,
17374 " %5td %4c %6"pI"d %c %3d 0x%05x",
17375 glyph - row->glyphs[TEXT_AREA],
17376 '+',
17377 glyph->charpos,
17378 (BUFFERP (glyph->object)
17379 ? 'B'
17380 : (STRINGP (glyph->object)
17381 ? 'S'
17382 : '-')),
17383 glyph->pixel_width,
17384 glyph->u.cmp.id);
17385 if (glyph->u.cmp.automatic)
17386 fprintf (stderr,
17387 "[%d-%d]",
17388 glyph->slice.cmp.from, glyph->slice.cmp.to);
17389 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17390 glyph->face_id,
17391 glyph->left_box_line_p,
17392 glyph->right_box_line_p);
17394 #ifdef HAVE_XWIDGETS
17395 else if (glyph->type == XWIDGET_GLYPH)
17397 fprintf (stderr,
17398 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17399 glyph - row->glyphs[TEXT_AREA],
17400 'X',
17401 glyph->charpos,
17402 (BUFFERP (glyph->object)
17403 ? 'B'
17404 : (STRINGP (glyph->object)
17405 ? 'S'
17406 : '-')),
17407 glyph->pixel_width,
17408 glyph->u.xwidget,
17409 '.',
17410 glyph->face_id,
17411 glyph->left_box_line_p,
17412 glyph->right_box_line_p);
17414 // printf("dump xwidget glyph\n");
17416 #endif
17420 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17421 GLYPHS 0 means don't show glyph contents.
17422 GLYPHS 1 means show glyphs in short form
17423 GLYPHS > 1 means show glyphs in long form. */
17425 void
17426 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17428 if (glyphs != 1)
17430 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17431 fprintf (stderr, "======================================================================\n");
17433 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17434 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17435 vpos,
17436 MATRIX_ROW_START_CHARPOS (row),
17437 MATRIX_ROW_END_CHARPOS (row),
17438 row->used[TEXT_AREA],
17439 row->contains_overlapping_glyphs_p,
17440 row->enabled_p,
17441 row->truncated_on_left_p,
17442 row->truncated_on_right_p,
17443 row->continued_p,
17444 MATRIX_ROW_CONTINUATION_LINE_P (row),
17445 row->displays_text_p,
17446 row->ends_at_zv_p,
17447 row->fill_line_p,
17448 row->ends_in_middle_of_char_p,
17449 row->starts_in_middle_of_char_p,
17450 row->mouse_face_p,
17451 row->x,
17452 row->y,
17453 row->pixel_width,
17454 row->height,
17455 row->visible_height,
17456 row->ascent,
17457 row->phys_ascent);
17458 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17459 row->end.overlay_string_index,
17460 row->continuation_lines_width);
17461 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17462 CHARPOS (row->start.string_pos),
17463 CHARPOS (row->end.string_pos));
17464 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17465 row->end.dpvec_index);
17468 if (glyphs > 1)
17470 int area;
17472 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17474 struct glyph *glyph = row->glyphs[area];
17475 struct glyph *glyph_end = glyph + row->used[area];
17477 /* Glyph for a line end in text. */
17478 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17479 ++glyph_end;
17481 if (glyph < glyph_end)
17482 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17484 for (; glyph < glyph_end; ++glyph)
17485 dump_glyph (row, glyph, area);
17488 else if (glyphs == 1)
17490 int area;
17492 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17494 char *s = (char *) alloca (row->used[area] + 1);
17495 int i;
17497 for (i = 0; i < row->used[area]; ++i)
17499 struct glyph *glyph = row->glyphs[area] + i;
17500 if (glyph->type == CHAR_GLYPH
17501 && glyph->u.ch < 0x80
17502 && glyph->u.ch >= ' ')
17503 s[i] = glyph->u.ch;
17504 else
17505 s[i] = '.';
17508 s[i] = '\0';
17509 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17515 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17516 Sdump_glyph_matrix, 0, 1, "p",
17517 doc: /* Dump the current matrix of the selected window to stderr.
17518 Shows contents of glyph row structures. With non-nil
17519 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17520 glyphs in short form, otherwise show glyphs in long form. */)
17521 (Lisp_Object glyphs)
17523 struct window *w = XWINDOW (selected_window);
17524 struct buffer *buffer = XBUFFER (w->buffer);
17526 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17527 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17528 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17529 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17530 fprintf (stderr, "=============================================\n");
17531 dump_glyph_matrix (w->current_matrix,
17532 NILP (glyphs) ? 0 : XINT (glyphs));
17533 return Qnil;
17537 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17538 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17539 (void)
17541 struct frame *f = XFRAME (selected_frame);
17542 dump_glyph_matrix (f->current_matrix, 1);
17543 return Qnil;
17547 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17548 doc: /* Dump glyph row ROW to stderr.
17549 GLYPH 0 means don't dump glyphs.
17550 GLYPH 1 means dump glyphs in short form.
17551 GLYPH > 1 or omitted means dump glyphs in long form. */)
17552 (Lisp_Object row, Lisp_Object glyphs)
17554 struct glyph_matrix *matrix;
17555 int vpos;
17557 CHECK_NUMBER (row);
17558 matrix = XWINDOW (selected_window)->current_matrix;
17559 vpos = XINT (row);
17560 if (vpos >= 0 && vpos < matrix->nrows)
17561 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17562 vpos,
17563 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17564 return Qnil;
17568 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17569 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17570 GLYPH 0 means don't dump glyphs.
17571 GLYPH 1 means dump glyphs in short form.
17572 GLYPH > 1 or omitted means dump glyphs in long form. */)
17573 (Lisp_Object row, Lisp_Object glyphs)
17575 struct frame *sf = SELECTED_FRAME ();
17576 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17577 int vpos;
17579 CHECK_NUMBER (row);
17580 vpos = XINT (row);
17581 if (vpos >= 0 && vpos < m->nrows)
17582 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17583 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17584 return Qnil;
17588 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17589 doc: /* Toggle tracing of redisplay.
17590 With ARG, turn tracing on if and only if ARG is positive. */)
17591 (Lisp_Object arg)
17593 if (NILP (arg))
17594 trace_redisplay_p = !trace_redisplay_p;
17595 else
17597 arg = Fprefix_numeric_value (arg);
17598 trace_redisplay_p = XINT (arg) > 0;
17601 return Qnil;
17605 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17606 doc: /* Like `format', but print result to stderr.
17607 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17608 (ptrdiff_t nargs, Lisp_Object *args)
17610 Lisp_Object s = Fformat (nargs, args);
17611 fprintf (stderr, "%s", SDATA (s));
17612 return Qnil;
17615 #endif /* GLYPH_DEBUG */
17619 /***********************************************************************
17620 Building Desired Matrix Rows
17621 ***********************************************************************/
17623 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17624 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17626 static struct glyph_row *
17627 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17629 struct frame *f = XFRAME (WINDOW_FRAME (w));
17630 struct buffer *buffer = XBUFFER (w->buffer);
17631 struct buffer *old = current_buffer;
17632 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17633 int arrow_len = SCHARS (overlay_arrow_string);
17634 const unsigned char *arrow_end = arrow_string + arrow_len;
17635 const unsigned char *p;
17636 struct it it;
17637 int multibyte_p;
17638 int n_glyphs_before;
17640 set_buffer_temp (buffer);
17641 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17642 it.glyph_row->used[TEXT_AREA] = 0;
17643 SET_TEXT_POS (it.position, 0, 0);
17645 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17646 p = arrow_string;
17647 while (p < arrow_end)
17649 Lisp_Object face, ilisp;
17651 /* Get the next character. */
17652 if (multibyte_p)
17653 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17654 else
17656 it.c = it.char_to_display = *p, it.len = 1;
17657 if (! ASCII_CHAR_P (it.c))
17658 it.char_to_display = BYTE8_TO_CHAR (it.c);
17660 p += it.len;
17662 /* Get its face. */
17663 ilisp = make_number (p - arrow_string);
17664 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17665 it.face_id = compute_char_face (f, it.char_to_display, face);
17667 /* Compute its width, get its glyphs. */
17668 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17669 SET_TEXT_POS (it.position, -1, -1);
17670 PRODUCE_GLYPHS (&it);
17672 /* If this character doesn't fit any more in the line, we have
17673 to remove some glyphs. */
17674 if (it.current_x > it.last_visible_x)
17676 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17677 break;
17681 set_buffer_temp (old);
17682 return it.glyph_row;
17686 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17687 glyphs are only inserted for terminal frames since we can't really
17688 win with truncation glyphs when partially visible glyphs are
17689 involved. Which glyphs to insert is determined by
17690 produce_special_glyphs. */
17692 static void
17693 insert_left_trunc_glyphs (struct it *it)
17695 struct it truncate_it;
17696 struct glyph *from, *end, *to, *toend;
17698 xassert (!FRAME_WINDOW_P (it->f));
17700 /* Get the truncation glyphs. */
17701 truncate_it = *it;
17702 truncate_it.current_x = 0;
17703 truncate_it.face_id = DEFAULT_FACE_ID;
17704 truncate_it.glyph_row = &scratch_glyph_row;
17705 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17706 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17707 truncate_it.object = make_number (0);
17708 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17710 /* Overwrite glyphs from IT with truncation glyphs. */
17711 if (!it->glyph_row->reversed_p)
17713 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17714 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17715 to = it->glyph_row->glyphs[TEXT_AREA];
17716 toend = to + it->glyph_row->used[TEXT_AREA];
17718 while (from < end)
17719 *to++ = *from++;
17721 /* There may be padding glyphs left over. Overwrite them too. */
17722 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17724 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17725 while (from < end)
17726 *to++ = *from++;
17729 if (to > toend)
17730 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17732 else
17734 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17735 that back to front. */
17736 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17737 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17738 toend = it->glyph_row->glyphs[TEXT_AREA];
17739 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17741 while (from >= end && to >= toend)
17742 *to-- = *from--;
17743 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17745 from =
17746 truncate_it.glyph_row->glyphs[TEXT_AREA]
17747 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17748 while (from >= end && to >= toend)
17749 *to-- = *from--;
17751 if (from >= end)
17753 /* Need to free some room before prepending additional
17754 glyphs. */
17755 int move_by = from - end + 1;
17756 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17757 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17759 for ( ; g >= g0; g--)
17760 g[move_by] = *g;
17761 while (from >= end)
17762 *to-- = *from--;
17763 it->glyph_row->used[TEXT_AREA] += move_by;
17769 /* Compute the pixel height and width of IT->glyph_row.
17771 Most of the time, ascent and height of a display line will be equal
17772 to the max_ascent and max_height values of the display iterator
17773 structure. This is not the case if
17775 1. We hit ZV without displaying anything. In this case, max_ascent
17776 and max_height will be zero.
17778 2. We have some glyphs that don't contribute to the line height.
17779 (The glyph row flag contributes_to_line_height_p is for future
17780 pixmap extensions).
17782 The first case is easily covered by using default values because in
17783 these cases, the line height does not really matter, except that it
17784 must not be zero. */
17786 static void
17787 compute_line_metrics (struct it *it)
17789 struct glyph_row *row = it->glyph_row;
17791 if (FRAME_WINDOW_P (it->f))
17793 int i, min_y, max_y;
17795 /* The line may consist of one space only, that was added to
17796 place the cursor on it. If so, the row's height hasn't been
17797 computed yet. */
17798 if (row->height == 0)
17800 if (it->max_ascent + it->max_descent == 0)
17801 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17802 row->ascent = it->max_ascent;
17803 row->height = it->max_ascent + it->max_descent;
17804 row->phys_ascent = it->max_phys_ascent;
17805 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17806 row->extra_line_spacing = it->max_extra_line_spacing;
17809 /* Compute the width of this line. */
17810 row->pixel_width = row->x;
17811 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17812 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17814 xassert (row->pixel_width >= 0);
17815 xassert (row->ascent >= 0 && row->height > 0);
17817 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17818 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17820 /* If first line's physical ascent is larger than its logical
17821 ascent, use the physical ascent, and make the row taller.
17822 This makes accented characters fully visible. */
17823 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17824 && row->phys_ascent > row->ascent)
17826 row->height += row->phys_ascent - row->ascent;
17827 row->ascent = row->phys_ascent;
17830 /* Compute how much of the line is visible. */
17831 row->visible_height = row->height;
17833 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17834 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17836 if (row->y < min_y)
17837 row->visible_height -= min_y - row->y;
17838 if (row->y + row->height > max_y)
17839 row->visible_height -= row->y + row->height - max_y;
17841 else
17843 row->pixel_width = row->used[TEXT_AREA];
17844 if (row->continued_p)
17845 row->pixel_width -= it->continuation_pixel_width;
17846 else if (row->truncated_on_right_p)
17847 row->pixel_width -= it->truncation_pixel_width;
17848 row->ascent = row->phys_ascent = 0;
17849 row->height = row->phys_height = row->visible_height = 1;
17850 row->extra_line_spacing = 0;
17853 /* Compute a hash code for this row. */
17855 int area, i;
17856 row->hash = 0;
17857 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17858 for (i = 0; i < row->used[area]; ++i)
17859 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17860 + row->glyphs[area][i].u.val
17861 + row->glyphs[area][i].face_id
17862 + row->glyphs[area][i].padding_p
17863 + (row->glyphs[area][i].type << 2));
17866 it->max_ascent = it->max_descent = 0;
17867 it->max_phys_ascent = it->max_phys_descent = 0;
17871 /* Append one space to the glyph row of iterator IT if doing a
17872 window-based redisplay. The space has the same face as
17873 IT->face_id. Value is non-zero if a space was added.
17875 This function is called to make sure that there is always one glyph
17876 at the end of a glyph row that the cursor can be set on under
17877 window-systems. (If there weren't such a glyph we would not know
17878 how wide and tall a box cursor should be displayed).
17880 At the same time this space let's a nicely handle clearing to the
17881 end of the line if the row ends in italic text. */
17883 static int
17884 append_space_for_newline (struct it *it, int default_face_p)
17886 if (FRAME_WINDOW_P (it->f))
17888 int n = it->glyph_row->used[TEXT_AREA];
17890 if (it->glyph_row->glyphs[TEXT_AREA] + n
17891 < it->glyph_row->glyphs[1 + TEXT_AREA])
17893 /* Save some values that must not be changed.
17894 Must save IT->c and IT->len because otherwise
17895 ITERATOR_AT_END_P wouldn't work anymore after
17896 append_space_for_newline has been called. */
17897 enum display_element_type saved_what = it->what;
17898 int saved_c = it->c, saved_len = it->len;
17899 int saved_char_to_display = it->char_to_display;
17900 int saved_x = it->current_x;
17901 int saved_face_id = it->face_id;
17902 struct text_pos saved_pos;
17903 Lisp_Object saved_object;
17904 struct face *face;
17906 saved_object = it->object;
17907 saved_pos = it->position;
17909 it->what = IT_CHARACTER;
17910 memset (&it->position, 0, sizeof it->position);
17911 it->object = make_number (0);
17912 it->c = it->char_to_display = ' ';
17913 it->len = 1;
17915 if (default_face_p)
17916 it->face_id = DEFAULT_FACE_ID;
17917 else if (it->face_before_selective_p)
17918 it->face_id = it->saved_face_id;
17919 face = FACE_FROM_ID (it->f, it->face_id);
17920 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17922 PRODUCE_GLYPHS (it);
17924 it->override_ascent = -1;
17925 it->constrain_row_ascent_descent_p = 0;
17926 it->current_x = saved_x;
17927 it->object = saved_object;
17928 it->position = saved_pos;
17929 it->what = saved_what;
17930 it->face_id = saved_face_id;
17931 it->len = saved_len;
17932 it->c = saved_c;
17933 it->char_to_display = saved_char_to_display;
17934 return 1;
17938 return 0;
17942 /* Extend the face of the last glyph in the text area of IT->glyph_row
17943 to the end of the display line. Called from display_line. If the
17944 glyph row is empty, add a space glyph to it so that we know the
17945 face to draw. Set the glyph row flag fill_line_p. If the glyph
17946 row is R2L, prepend a stretch glyph to cover the empty space to the
17947 left of the leftmost glyph. */
17949 static void
17950 extend_face_to_end_of_line (struct it *it)
17952 struct face *face;
17953 struct frame *f = it->f;
17955 /* If line is already filled, do nothing. Non window-system frames
17956 get a grace of one more ``pixel'' because their characters are
17957 1-``pixel'' wide, so they hit the equality too early. This grace
17958 is needed only for R2L rows that are not continued, to produce
17959 one extra blank where we could display the cursor. */
17960 if (it->current_x >= it->last_visible_x
17961 + (!FRAME_WINDOW_P (f)
17962 && it->glyph_row->reversed_p
17963 && !it->glyph_row->continued_p))
17964 return;
17966 /* Face extension extends the background and box of IT->face_id
17967 to the end of the line. If the background equals the background
17968 of the frame, we don't have to do anything. */
17969 if (it->face_before_selective_p)
17970 face = FACE_FROM_ID (f, it->saved_face_id);
17971 else
17972 face = FACE_FROM_ID (f, it->face_id);
17974 if (FRAME_WINDOW_P (f)
17975 && it->glyph_row->displays_text_p
17976 && face->box == FACE_NO_BOX
17977 && face->background == FRAME_BACKGROUND_PIXEL (f)
17978 && !face->stipple
17979 && !it->glyph_row->reversed_p)
17980 return;
17982 /* Set the glyph row flag indicating that the face of the last glyph
17983 in the text area has to be drawn to the end of the text area. */
17984 it->glyph_row->fill_line_p = 1;
17986 /* If current character of IT is not ASCII, make sure we have the
17987 ASCII face. This will be automatically undone the next time
17988 get_next_display_element returns a multibyte character. Note
17989 that the character will always be single byte in unibyte
17990 text. */
17991 if (!ASCII_CHAR_P (it->c))
17993 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17996 if (FRAME_WINDOW_P (f))
17998 /* If the row is empty, add a space with the current face of IT,
17999 so that we know which face to draw. */
18000 if (it->glyph_row->used[TEXT_AREA] == 0)
18002 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18003 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
18004 it->glyph_row->used[TEXT_AREA] = 1;
18006 #ifdef HAVE_WINDOW_SYSTEM
18007 if (it->glyph_row->reversed_p)
18009 /* Prepend a stretch glyph to the row, such that the
18010 rightmost glyph will be drawn flushed all the way to the
18011 right margin of the window. The stretch glyph that will
18012 occupy the empty space, if any, to the left of the
18013 glyphs. */
18014 struct font *font = face->font ? face->font : FRAME_FONT (f);
18015 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18016 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18017 struct glyph *g;
18018 int row_width, stretch_ascent, stretch_width;
18019 struct text_pos saved_pos;
18020 int saved_face_id, saved_avoid_cursor;
18022 for (row_width = 0, g = row_start; g < row_end; g++)
18023 row_width += g->pixel_width;
18024 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18025 if (stretch_width > 0)
18027 stretch_ascent =
18028 (((it->ascent + it->descent)
18029 * FONT_BASE (font)) / FONT_HEIGHT (font));
18030 saved_pos = it->position;
18031 memset (&it->position, 0, sizeof it->position);
18032 saved_avoid_cursor = it->avoid_cursor_p;
18033 it->avoid_cursor_p = 1;
18034 saved_face_id = it->face_id;
18035 /* The last row's stretch glyph should get the default
18036 face, to avoid painting the rest of the window with
18037 the region face, if the region ends at ZV. */
18038 if (it->glyph_row->ends_at_zv_p)
18039 it->face_id = DEFAULT_FACE_ID;
18040 else
18041 it->face_id = face->id;
18042 append_stretch_glyph (it, make_number (0), stretch_width,
18043 it->ascent + it->descent, stretch_ascent);
18044 it->position = saved_pos;
18045 it->avoid_cursor_p = saved_avoid_cursor;
18046 it->face_id = saved_face_id;
18049 #endif /* HAVE_WINDOW_SYSTEM */
18051 else
18053 /* Save some values that must not be changed. */
18054 int saved_x = it->current_x;
18055 struct text_pos saved_pos;
18056 Lisp_Object saved_object;
18057 enum display_element_type saved_what = it->what;
18058 int saved_face_id = it->face_id;
18060 saved_object = it->object;
18061 saved_pos = it->position;
18063 it->what = IT_CHARACTER;
18064 memset (&it->position, 0, sizeof it->position);
18065 it->object = make_number (0);
18066 it->c = it->char_to_display = ' ';
18067 it->len = 1;
18068 /* The last row's blank glyphs should get the default face, to
18069 avoid painting the rest of the window with the region face,
18070 if the region ends at ZV. */
18071 if (it->glyph_row->ends_at_zv_p)
18072 it->face_id = DEFAULT_FACE_ID;
18073 else
18074 it->face_id = face->id;
18076 PRODUCE_GLYPHS (it);
18078 while (it->current_x <= it->last_visible_x)
18079 PRODUCE_GLYPHS (it);
18081 /* Don't count these blanks really. It would let us insert a left
18082 truncation glyph below and make us set the cursor on them, maybe. */
18083 it->current_x = saved_x;
18084 it->object = saved_object;
18085 it->position = saved_pos;
18086 it->what = saved_what;
18087 it->face_id = saved_face_id;
18092 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18093 trailing whitespace. */
18095 static int
18096 trailing_whitespace_p (EMACS_INT charpos)
18098 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18099 int c = 0;
18101 while (bytepos < ZV_BYTE
18102 && (c = FETCH_CHAR (bytepos),
18103 c == ' ' || c == '\t'))
18104 ++bytepos;
18106 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18108 if (bytepos != PT_BYTE)
18109 return 1;
18111 return 0;
18115 /* Highlight trailing whitespace, if any, in ROW. */
18117 static void
18118 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18120 int used = row->used[TEXT_AREA];
18122 if (used)
18124 struct glyph *start = row->glyphs[TEXT_AREA];
18125 struct glyph *glyph = start + used - 1;
18127 if (row->reversed_p)
18129 /* Right-to-left rows need to be processed in the opposite
18130 direction, so swap the edge pointers. */
18131 glyph = start;
18132 start = row->glyphs[TEXT_AREA] + used - 1;
18135 /* Skip over glyphs inserted to display the cursor at the
18136 end of a line, for extending the face of the last glyph
18137 to the end of the line on terminals, and for truncation
18138 and continuation glyphs. */
18139 if (!row->reversed_p)
18141 while (glyph >= start
18142 && glyph->type == CHAR_GLYPH
18143 && INTEGERP (glyph->object))
18144 --glyph;
18146 else
18148 while (glyph <= start
18149 && glyph->type == CHAR_GLYPH
18150 && INTEGERP (glyph->object))
18151 ++glyph;
18154 /* If last glyph is a space or stretch, and it's trailing
18155 whitespace, set the face of all trailing whitespace glyphs in
18156 IT->glyph_row to `trailing-whitespace'. */
18157 if ((row->reversed_p ? glyph <= start : glyph >= start)
18158 && BUFFERP (glyph->object)
18159 && (glyph->type == STRETCH_GLYPH
18160 || (glyph->type == CHAR_GLYPH
18161 && glyph->u.ch == ' '))
18162 && trailing_whitespace_p (glyph->charpos))
18164 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18165 if (face_id < 0)
18166 return;
18168 if (!row->reversed_p)
18170 while (glyph >= start
18171 && BUFFERP (glyph->object)
18172 && (glyph->type == STRETCH_GLYPH
18173 || (glyph->type == CHAR_GLYPH
18174 && glyph->u.ch == ' ')))
18175 (glyph--)->face_id = face_id;
18177 else
18179 while (glyph <= start
18180 && BUFFERP (glyph->object)
18181 && (glyph->type == STRETCH_GLYPH
18182 || (glyph->type == CHAR_GLYPH
18183 && glyph->u.ch == ' ')))
18184 (glyph++)->face_id = face_id;
18191 /* Value is non-zero if glyph row ROW should be
18192 used to hold the cursor. */
18194 static int
18195 cursor_row_p (struct glyph_row *row)
18197 int result = 1;
18199 if (PT == CHARPOS (row->end.pos)
18200 || PT == MATRIX_ROW_END_CHARPOS (row))
18202 /* Suppose the row ends on a string.
18203 Unless the row is continued, that means it ends on a newline
18204 in the string. If it's anything other than a display string
18205 (e.g. a before-string from an overlay), we don't want the
18206 cursor there. (This heuristic seems to give the optimal
18207 behavior for the various types of multi-line strings.) */
18208 if (CHARPOS (row->end.string_pos) >= 0)
18210 if (row->continued_p)
18211 result = 1;
18212 else
18214 /* Check for `display' property. */
18215 struct glyph *beg = row->glyphs[TEXT_AREA];
18216 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18217 struct glyph *glyph;
18219 result = 0;
18220 for (glyph = end; glyph >= beg; --glyph)
18221 if (STRINGP (glyph->object))
18223 Lisp_Object prop
18224 = Fget_char_property (make_number (PT),
18225 Qdisplay, Qnil);
18226 result =
18227 (!NILP (prop)
18228 && display_prop_string_p (prop, glyph->object));
18229 break;
18233 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18235 /* If the row ends in middle of a real character,
18236 and the line is continued, we want the cursor here.
18237 That's because CHARPOS (ROW->end.pos) would equal
18238 PT if PT is before the character. */
18239 if (!row->ends_in_ellipsis_p)
18240 result = row->continued_p;
18241 else
18242 /* If the row ends in an ellipsis, then
18243 CHARPOS (ROW->end.pos) will equal point after the
18244 invisible text. We want that position to be displayed
18245 after the ellipsis. */
18246 result = 0;
18248 /* If the row ends at ZV, display the cursor at the end of that
18249 row instead of at the start of the row below. */
18250 else if (row->ends_at_zv_p)
18251 result = 1;
18252 else
18253 result = 0;
18256 return result;
18261 /* Push the property PROP so that it will be rendered at the current
18262 position in IT. Return 1 if PROP was successfully pushed, 0
18263 otherwise. Called from handle_line_prefix to handle the
18264 `line-prefix' and `wrap-prefix' properties. */
18266 static int
18267 push_display_prop (struct it *it, Lisp_Object prop)
18269 struct text_pos pos =
18270 (it->method == GET_FROM_STRING) ? it->current.string_pos : it->current.pos;
18272 xassert (it->method == GET_FROM_BUFFER
18273 || it->method == GET_FROM_STRING);
18275 /* We need to save the current buffer/string position, so it will be
18276 restored by pop_it, because iterate_out_of_display_property
18277 depends on that being set correctly, but some situations leave
18278 it->position not yet set when this function is called. */
18279 push_it (it, &pos);
18281 if (STRINGP (prop))
18283 if (SCHARS (prop) == 0)
18285 pop_it (it);
18286 return 0;
18289 it->string = prop;
18290 it->multibyte_p = STRING_MULTIBYTE (it->string);
18291 it->current.overlay_string_index = -1;
18292 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18293 it->end_charpos = it->string_nchars = SCHARS (it->string);
18294 it->method = GET_FROM_STRING;
18295 it->stop_charpos = 0;
18296 it->prev_stop = 0;
18297 it->base_level_stop = 0;
18299 /* Force paragraph direction to be that of the parent
18300 buffer/string. */
18301 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18302 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18303 else
18304 it->paragraph_embedding = L2R;
18306 /* Set up the bidi iterator for this display string. */
18307 if (it->bidi_p)
18309 it->bidi_it.string.lstring = it->string;
18310 it->bidi_it.string.s = NULL;
18311 it->bidi_it.string.schars = it->end_charpos;
18312 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18313 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18314 it->bidi_it.string.unibyte = !it->multibyte_p;
18315 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18318 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18320 it->method = GET_FROM_STRETCH;
18321 it->object = prop;
18323 #ifdef HAVE_WINDOW_SYSTEM
18324 else if (IMAGEP (prop))
18326 it->what = IT_IMAGE;
18327 it->image_id = lookup_image (it->f, prop);
18328 it->method = GET_FROM_IMAGE;
18330 #endif /* HAVE_WINDOW_SYSTEM */
18331 else
18333 pop_it (it); /* bogus display property, give up */
18334 return 0;
18337 return 1;
18340 /* Return the character-property PROP at the current position in IT. */
18342 static Lisp_Object
18343 get_it_property (struct it *it, Lisp_Object prop)
18345 Lisp_Object position;
18347 if (STRINGP (it->object))
18348 position = make_number (IT_STRING_CHARPOS (*it));
18349 else if (BUFFERP (it->object))
18350 position = make_number (IT_CHARPOS (*it));
18351 else
18352 return Qnil;
18354 return Fget_char_property (position, prop, it->object);
18357 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18359 static void
18360 handle_line_prefix (struct it *it)
18362 Lisp_Object prefix;
18364 if (it->continuation_lines_width > 0)
18366 prefix = get_it_property (it, Qwrap_prefix);
18367 if (NILP (prefix))
18368 prefix = Vwrap_prefix;
18370 else
18372 prefix = get_it_property (it, Qline_prefix);
18373 if (NILP (prefix))
18374 prefix = Vline_prefix;
18376 if (! NILP (prefix) && push_display_prop (it, prefix))
18378 /* If the prefix is wider than the window, and we try to wrap
18379 it, it would acquire its own wrap prefix, and so on till the
18380 iterator stack overflows. So, don't wrap the prefix. */
18381 it->line_wrap = TRUNCATE;
18382 it->avoid_cursor_p = 1;
18388 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18389 only for R2L lines from display_line and display_string, when they
18390 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18391 the line/string needs to be continued on the next glyph row. */
18392 static void
18393 unproduce_glyphs (struct it *it, int n)
18395 struct glyph *glyph, *end;
18397 xassert (it->glyph_row);
18398 xassert (it->glyph_row->reversed_p);
18399 xassert (it->area == TEXT_AREA);
18400 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18402 if (n > it->glyph_row->used[TEXT_AREA])
18403 n = it->glyph_row->used[TEXT_AREA];
18404 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18405 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18406 for ( ; glyph < end; glyph++)
18407 glyph[-n] = *glyph;
18410 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18411 and ROW->maxpos. */
18412 static void
18413 find_row_edges (struct it *it, struct glyph_row *row,
18414 EMACS_INT min_pos, EMACS_INT min_bpos,
18415 EMACS_INT max_pos, EMACS_INT max_bpos)
18417 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18418 lines' rows is implemented for bidi-reordered rows. */
18420 /* ROW->minpos is the value of min_pos, the minimal buffer position
18421 we have in ROW, or ROW->start.pos if that is smaller. */
18422 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18423 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18424 else
18425 /* We didn't find buffer positions smaller than ROW->start, or
18426 didn't find _any_ valid buffer positions in any of the glyphs,
18427 so we must trust the iterator's computed positions. */
18428 row->minpos = row->start.pos;
18429 if (max_pos <= 0)
18431 max_pos = CHARPOS (it->current.pos);
18432 max_bpos = BYTEPOS (it->current.pos);
18435 /* Here are the various use-cases for ending the row, and the
18436 corresponding values for ROW->maxpos:
18438 Line ends in a newline from buffer eol_pos + 1
18439 Line is continued from buffer max_pos + 1
18440 Line is truncated on right it->current.pos
18441 Line ends in a newline from string max_pos
18442 Line is continued from string max_pos
18443 Line is continued from display vector max_pos
18444 Line is entirely from a string min_pos == max_pos
18445 Line is entirely from a display vector min_pos == max_pos
18446 Line that ends at ZV ZV
18448 If you discover other use-cases, please add them here as
18449 appropriate. */
18450 if (row->ends_at_zv_p)
18451 row->maxpos = it->current.pos;
18452 else if (row->used[TEXT_AREA])
18454 if (row->ends_in_newline_from_string_p)
18455 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18456 else if (CHARPOS (it->eol_pos) > 0)
18457 SET_TEXT_POS (row->maxpos,
18458 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18459 else if (row->continued_p)
18461 /* If max_pos is different from IT's current position, it
18462 means IT->method does not belong to the display element
18463 at max_pos. However, it also means that the display
18464 element at max_pos was displayed in its entirety on this
18465 line, which is equivalent to saying that the next line
18466 starts at the next buffer position. */
18467 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18468 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18469 else
18471 INC_BOTH (max_pos, max_bpos);
18472 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18475 else if (row->truncated_on_right_p)
18476 /* display_line already called reseat_at_next_visible_line_start,
18477 which puts the iterator at the beginning of the next line, in
18478 the logical order. */
18479 row->maxpos = it->current.pos;
18480 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18481 /* A line that is entirely from a string/image/stretch... */
18482 row->maxpos = row->minpos;
18483 else
18484 abort ();
18486 else
18487 row->maxpos = it->current.pos;
18490 /* Construct the glyph row IT->glyph_row in the desired matrix of
18491 IT->w from text at the current position of IT. See dispextern.h
18492 for an overview of struct it. Value is non-zero if
18493 IT->glyph_row displays text, as opposed to a line displaying ZV
18494 only. */
18496 static int
18497 display_line (struct it *it)
18499 struct glyph_row *row = it->glyph_row;
18500 Lisp_Object overlay_arrow_string;
18501 struct it wrap_it;
18502 void *wrap_data = NULL;
18503 int may_wrap = 0, wrap_x IF_LINT (= 0);
18504 int wrap_row_used = -1;
18505 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18506 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18507 int wrap_row_extra_line_spacing IF_LINT (= 0);
18508 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18509 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18510 int cvpos;
18511 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18512 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18514 /* We always start displaying at hpos zero even if hscrolled. */
18515 xassert (it->hpos == 0 && it->current_x == 0);
18517 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18518 >= it->w->desired_matrix->nrows)
18520 it->w->nrows_scale_factor++;
18521 fonts_changed_p = 1;
18522 return 0;
18525 /* Is IT->w showing the region? */
18526 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18528 /* Clear the result glyph row and enable it. */
18529 prepare_desired_row (row);
18531 row->y = it->current_y;
18532 row->start = it->start;
18533 row->continuation_lines_width = it->continuation_lines_width;
18534 row->displays_text_p = 1;
18535 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18536 it->starts_in_middle_of_char_p = 0;
18538 /* Arrange the overlays nicely for our purposes. Usually, we call
18539 display_line on only one line at a time, in which case this
18540 can't really hurt too much, or we call it on lines which appear
18541 one after another in the buffer, in which case all calls to
18542 recenter_overlay_lists but the first will be pretty cheap. */
18543 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18545 /* Move over display elements that are not visible because we are
18546 hscrolled. This may stop at an x-position < IT->first_visible_x
18547 if the first glyph is partially visible or if we hit a line end. */
18548 if (it->current_x < it->first_visible_x)
18550 this_line_min_pos = row->start.pos;
18551 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18552 MOVE_TO_POS | MOVE_TO_X);
18553 /* Record the smallest positions seen while we moved over
18554 display elements that are not visible. This is needed by
18555 redisplay_internal for optimizing the case where the cursor
18556 stays inside the same line. The rest of this function only
18557 considers positions that are actually displayed, so
18558 RECORD_MAX_MIN_POS will not otherwise record positions that
18559 are hscrolled to the left of the left edge of the window. */
18560 min_pos = CHARPOS (this_line_min_pos);
18561 min_bpos = BYTEPOS (this_line_min_pos);
18563 else
18565 /* We only do this when not calling `move_it_in_display_line_to'
18566 above, because move_it_in_display_line_to calls
18567 handle_line_prefix itself. */
18568 handle_line_prefix (it);
18571 /* Get the initial row height. This is either the height of the
18572 text hscrolled, if there is any, or zero. */
18573 row->ascent = it->max_ascent;
18574 row->height = it->max_ascent + it->max_descent;
18575 row->phys_ascent = it->max_phys_ascent;
18576 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18577 row->extra_line_spacing = it->max_extra_line_spacing;
18579 /* Utility macro to record max and min buffer positions seen until now. */
18580 #define RECORD_MAX_MIN_POS(IT) \
18581 do \
18583 int composition_p = (IT)->what == IT_COMPOSITION; \
18584 EMACS_INT current_pos = \
18585 composition_p ? (IT)->cmp_it.charpos \
18586 : IT_CHARPOS (*(IT)); \
18587 EMACS_INT current_bpos = \
18588 composition_p ? CHAR_TO_BYTE (current_pos) \
18589 : IT_BYTEPOS (*(IT)); \
18590 if (current_pos < min_pos) \
18592 min_pos = current_pos; \
18593 min_bpos = current_bpos; \
18595 if (IT_CHARPOS (*it) > max_pos) \
18597 max_pos = IT_CHARPOS (*it); \
18598 max_bpos = IT_BYTEPOS (*it); \
18601 while (0)
18603 /* Loop generating characters. The loop is left with IT on the next
18604 character to display. */
18605 while (1)
18607 int n_glyphs_before, hpos_before, x_before;
18608 int x, nglyphs;
18609 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18611 /* Retrieve the next thing to display. Value is zero if end of
18612 buffer reached. */
18613 if (!get_next_display_element (it))
18615 /* Maybe add a space at the end of this line that is used to
18616 display the cursor there under X. Set the charpos of the
18617 first glyph of blank lines not corresponding to any text
18618 to -1. */
18619 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18620 row->exact_window_width_line_p = 1;
18621 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18622 || row->used[TEXT_AREA] == 0)
18624 row->glyphs[TEXT_AREA]->charpos = -1;
18625 row->displays_text_p = 0;
18627 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18628 && (!MINI_WINDOW_P (it->w)
18629 || (minibuf_level && EQ (it->window, minibuf_window))))
18630 row->indicate_empty_line_p = 1;
18633 it->continuation_lines_width = 0;
18634 row->ends_at_zv_p = 1;
18635 /* A row that displays right-to-left text must always have
18636 its last face extended all the way to the end of line,
18637 even if this row ends in ZV, because we still write to
18638 the screen left to right. */
18639 if (row->reversed_p)
18640 extend_face_to_end_of_line (it);
18641 break;
18644 /* Now, get the metrics of what we want to display. This also
18645 generates glyphs in `row' (which is IT->glyph_row). */
18646 n_glyphs_before = row->used[TEXT_AREA];
18647 x = it->current_x;
18649 /* Remember the line height so far in case the next element doesn't
18650 fit on the line. */
18651 if (it->line_wrap != TRUNCATE)
18653 ascent = it->max_ascent;
18654 descent = it->max_descent;
18655 phys_ascent = it->max_phys_ascent;
18656 phys_descent = it->max_phys_descent;
18658 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18660 if (IT_DISPLAYING_WHITESPACE (it))
18661 may_wrap = 1;
18662 else if (may_wrap)
18664 SAVE_IT (wrap_it, *it, wrap_data);
18665 wrap_x = x;
18666 wrap_row_used = row->used[TEXT_AREA];
18667 wrap_row_ascent = row->ascent;
18668 wrap_row_height = row->height;
18669 wrap_row_phys_ascent = row->phys_ascent;
18670 wrap_row_phys_height = row->phys_height;
18671 wrap_row_extra_line_spacing = row->extra_line_spacing;
18672 wrap_row_min_pos = min_pos;
18673 wrap_row_min_bpos = min_bpos;
18674 wrap_row_max_pos = max_pos;
18675 wrap_row_max_bpos = max_bpos;
18676 may_wrap = 0;
18681 PRODUCE_GLYPHS (it);
18683 /* If this display element was in marginal areas, continue with
18684 the next one. */
18685 if (it->area != TEXT_AREA)
18687 row->ascent = max (row->ascent, it->max_ascent);
18688 row->height = max (row->height, it->max_ascent + it->max_descent);
18689 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18690 row->phys_height = max (row->phys_height,
18691 it->max_phys_ascent + it->max_phys_descent);
18692 row->extra_line_spacing = max (row->extra_line_spacing,
18693 it->max_extra_line_spacing);
18694 set_iterator_to_next (it, 1);
18695 continue;
18698 /* Does the display element fit on the line? If we truncate
18699 lines, we should draw past the right edge of the window. If
18700 we don't truncate, we want to stop so that we can display the
18701 continuation glyph before the right margin. If lines are
18702 continued, there are two possible strategies for characters
18703 resulting in more than 1 glyph (e.g. tabs): Display as many
18704 glyphs as possible in this line and leave the rest for the
18705 continuation line, or display the whole element in the next
18706 line. Original redisplay did the former, so we do it also. */
18707 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18708 hpos_before = it->hpos;
18709 x_before = x;
18711 if (/* Not a newline. */
18712 nglyphs > 0
18713 /* Glyphs produced fit entirely in the line. */
18714 && it->current_x < it->last_visible_x)
18716 it->hpos += nglyphs;
18717 row->ascent = max (row->ascent, it->max_ascent);
18718 row->height = max (row->height, it->max_ascent + it->max_descent);
18719 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18720 row->phys_height = max (row->phys_height,
18721 it->max_phys_ascent + it->max_phys_descent);
18722 row->extra_line_spacing = max (row->extra_line_spacing,
18723 it->max_extra_line_spacing);
18724 if (it->current_x - it->pixel_width < it->first_visible_x)
18725 row->x = x - it->first_visible_x;
18726 /* Record the maximum and minimum buffer positions seen so
18727 far in glyphs that will be displayed by this row. */
18728 if (it->bidi_p)
18729 RECORD_MAX_MIN_POS (it);
18731 else
18733 int i, new_x;
18734 struct glyph *glyph;
18736 for (i = 0; i < nglyphs; ++i, x = new_x)
18738 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18739 new_x = x + glyph->pixel_width;
18741 if (/* Lines are continued. */
18742 it->line_wrap != TRUNCATE
18743 && (/* Glyph doesn't fit on the line. */
18744 new_x > it->last_visible_x
18745 /* Or it fits exactly on a window system frame. */
18746 || (new_x == it->last_visible_x
18747 && FRAME_WINDOW_P (it->f))))
18749 /* End of a continued line. */
18751 if (it->hpos == 0
18752 || (new_x == it->last_visible_x
18753 && FRAME_WINDOW_P (it->f)))
18755 /* Current glyph is the only one on the line or
18756 fits exactly on the line. We must continue
18757 the line because we can't draw the cursor
18758 after the glyph. */
18759 row->continued_p = 1;
18760 it->current_x = new_x;
18761 it->continuation_lines_width += new_x;
18762 ++it->hpos;
18763 /* Record the maximum and minimum buffer
18764 positions seen so far in glyphs that will be
18765 displayed by this row. */
18766 if (it->bidi_p)
18767 RECORD_MAX_MIN_POS (it);
18768 if (i == nglyphs - 1)
18770 /* If line-wrap is on, check if a previous
18771 wrap point was found. */
18772 if (wrap_row_used > 0
18773 /* Even if there is a previous wrap
18774 point, continue the line here as
18775 usual, if (i) the previous character
18776 was a space or tab AND (ii) the
18777 current character is not. */
18778 && (!may_wrap
18779 || IT_DISPLAYING_WHITESPACE (it)))
18780 goto back_to_wrap;
18782 set_iterator_to_next (it, 1);
18783 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18785 if (!get_next_display_element (it))
18787 row->exact_window_width_line_p = 1;
18788 it->continuation_lines_width = 0;
18789 row->continued_p = 0;
18790 row->ends_at_zv_p = 1;
18792 else if (ITERATOR_AT_END_OF_LINE_P (it))
18794 row->continued_p = 0;
18795 row->exact_window_width_line_p = 1;
18800 else if (CHAR_GLYPH_PADDING_P (*glyph)
18801 && !FRAME_WINDOW_P (it->f))
18803 /* A padding glyph that doesn't fit on this line.
18804 This means the whole character doesn't fit
18805 on the line. */
18806 if (row->reversed_p)
18807 unproduce_glyphs (it, row->used[TEXT_AREA]
18808 - n_glyphs_before);
18809 row->used[TEXT_AREA] = n_glyphs_before;
18811 /* Fill the rest of the row with continuation
18812 glyphs like in 20.x. */
18813 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18814 < row->glyphs[1 + TEXT_AREA])
18815 produce_special_glyphs (it, IT_CONTINUATION);
18817 row->continued_p = 1;
18818 it->current_x = x_before;
18819 it->continuation_lines_width += x_before;
18821 /* Restore the height to what it was before the
18822 element not fitting on the line. */
18823 it->max_ascent = ascent;
18824 it->max_descent = descent;
18825 it->max_phys_ascent = phys_ascent;
18826 it->max_phys_descent = phys_descent;
18828 else if (wrap_row_used > 0)
18830 back_to_wrap:
18831 if (row->reversed_p)
18832 unproduce_glyphs (it,
18833 row->used[TEXT_AREA] - wrap_row_used);
18834 RESTORE_IT (it, &wrap_it, wrap_data);
18835 it->continuation_lines_width += wrap_x;
18836 row->used[TEXT_AREA] = wrap_row_used;
18837 row->ascent = wrap_row_ascent;
18838 row->height = wrap_row_height;
18839 row->phys_ascent = wrap_row_phys_ascent;
18840 row->phys_height = wrap_row_phys_height;
18841 row->extra_line_spacing = wrap_row_extra_line_spacing;
18842 min_pos = wrap_row_min_pos;
18843 min_bpos = wrap_row_min_bpos;
18844 max_pos = wrap_row_max_pos;
18845 max_bpos = wrap_row_max_bpos;
18846 row->continued_p = 1;
18847 row->ends_at_zv_p = 0;
18848 row->exact_window_width_line_p = 0;
18849 it->continuation_lines_width += x;
18851 /* Make sure that a non-default face is extended
18852 up to the right margin of the window. */
18853 extend_face_to_end_of_line (it);
18855 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18857 /* A TAB that extends past the right edge of the
18858 window. This produces a single glyph on
18859 window system frames. We leave the glyph in
18860 this row and let it fill the row, but don't
18861 consume the TAB. */
18862 it->continuation_lines_width += it->last_visible_x;
18863 row->ends_in_middle_of_char_p = 1;
18864 row->continued_p = 1;
18865 glyph->pixel_width = it->last_visible_x - x;
18866 it->starts_in_middle_of_char_p = 1;
18868 else
18870 /* Something other than a TAB that draws past
18871 the right edge of the window. Restore
18872 positions to values before the element. */
18873 if (row->reversed_p)
18874 unproduce_glyphs (it, row->used[TEXT_AREA]
18875 - (n_glyphs_before + i));
18876 row->used[TEXT_AREA] = n_glyphs_before + i;
18878 /* Display continuation glyphs. */
18879 if (!FRAME_WINDOW_P (it->f))
18880 produce_special_glyphs (it, IT_CONTINUATION);
18881 row->continued_p = 1;
18883 it->current_x = x_before;
18884 it->continuation_lines_width += x;
18885 extend_face_to_end_of_line (it);
18887 if (nglyphs > 1 && i > 0)
18889 row->ends_in_middle_of_char_p = 1;
18890 it->starts_in_middle_of_char_p = 1;
18893 /* Restore the height to what it was before the
18894 element not fitting on the line. */
18895 it->max_ascent = ascent;
18896 it->max_descent = descent;
18897 it->max_phys_ascent = phys_ascent;
18898 it->max_phys_descent = phys_descent;
18901 break;
18903 else if (new_x > it->first_visible_x)
18905 /* Increment number of glyphs actually displayed. */
18906 ++it->hpos;
18908 /* Record the maximum and minimum buffer positions
18909 seen so far in glyphs that will be displayed by
18910 this row. */
18911 if (it->bidi_p)
18912 RECORD_MAX_MIN_POS (it);
18914 if (x < it->first_visible_x)
18915 /* Glyph is partially visible, i.e. row starts at
18916 negative X position. */
18917 row->x = x - it->first_visible_x;
18919 else
18921 /* Glyph is completely off the left margin of the
18922 window. This should not happen because of the
18923 move_it_in_display_line at the start of this
18924 function, unless the text display area of the
18925 window is empty. */
18926 xassert (it->first_visible_x <= it->last_visible_x);
18930 row->ascent = max (row->ascent, it->max_ascent);
18931 row->height = max (row->height, it->max_ascent + it->max_descent);
18932 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18933 row->phys_height = max (row->phys_height,
18934 it->max_phys_ascent + it->max_phys_descent);
18935 row->extra_line_spacing = max (row->extra_line_spacing,
18936 it->max_extra_line_spacing);
18938 /* End of this display line if row is continued. */
18939 if (row->continued_p || row->ends_at_zv_p)
18940 break;
18943 at_end_of_line:
18944 /* Is this a line end? If yes, we're also done, after making
18945 sure that a non-default face is extended up to the right
18946 margin of the window. */
18947 if (ITERATOR_AT_END_OF_LINE_P (it))
18949 int used_before = row->used[TEXT_AREA];
18951 row->ends_in_newline_from_string_p = STRINGP (it->object);
18953 /* Add a space at the end of the line that is used to
18954 display the cursor there. */
18955 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18956 append_space_for_newline (it, 0);
18958 /* Extend the face to the end of the line. */
18959 extend_face_to_end_of_line (it);
18961 /* Make sure we have the position. */
18962 if (used_before == 0)
18963 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18965 /* Record the position of the newline, for use in
18966 find_row_edges. */
18967 it->eol_pos = it->current.pos;
18969 /* Consume the line end. This skips over invisible lines. */
18970 set_iterator_to_next (it, 1);
18971 it->continuation_lines_width = 0;
18972 break;
18975 /* Proceed with next display element. Note that this skips
18976 over lines invisible because of selective display. */
18977 set_iterator_to_next (it, 1);
18979 /* If we truncate lines, we are done when the last displayed
18980 glyphs reach past the right margin of the window. */
18981 if (it->line_wrap == TRUNCATE
18982 && (FRAME_WINDOW_P (it->f)
18983 ? (it->current_x >= it->last_visible_x)
18984 : (it->current_x > it->last_visible_x)))
18986 /* Maybe add truncation glyphs. */
18987 if (!FRAME_WINDOW_P (it->f))
18989 int i, n;
18991 if (!row->reversed_p)
18993 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18994 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18995 break;
18997 else
18999 for (i = 0; i < row->used[TEXT_AREA]; i++)
19000 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19001 break;
19002 /* Remove any padding glyphs at the front of ROW, to
19003 make room for the truncation glyphs we will be
19004 adding below. The loop below always inserts at
19005 least one truncation glyph, so also remove the
19006 last glyph added to ROW. */
19007 unproduce_glyphs (it, i + 1);
19008 /* Adjust i for the loop below. */
19009 i = row->used[TEXT_AREA] - (i + 1);
19012 for (n = row->used[TEXT_AREA]; i < n; ++i)
19014 row->used[TEXT_AREA] = i;
19015 produce_special_glyphs (it, IT_TRUNCATION);
19018 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19020 /* Don't truncate if we can overflow newline into fringe. */
19021 if (!get_next_display_element (it))
19023 it->continuation_lines_width = 0;
19024 row->ends_at_zv_p = 1;
19025 row->exact_window_width_line_p = 1;
19026 break;
19028 if (ITERATOR_AT_END_OF_LINE_P (it))
19030 row->exact_window_width_line_p = 1;
19031 goto at_end_of_line;
19035 row->truncated_on_right_p = 1;
19036 it->continuation_lines_width = 0;
19037 reseat_at_next_visible_line_start (it, 0);
19038 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19039 it->hpos = hpos_before;
19040 it->current_x = x_before;
19041 break;
19045 if (wrap_data)
19046 bidi_unshelve_cache (wrap_data, 1);
19048 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19049 at the left window margin. */
19050 if (it->first_visible_x
19051 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19053 if (!FRAME_WINDOW_P (it->f))
19054 insert_left_trunc_glyphs (it);
19055 row->truncated_on_left_p = 1;
19058 /* Remember the position at which this line ends.
19060 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19061 cannot be before the call to find_row_edges below, since that is
19062 where these positions are determined. */
19063 row->end = it->current;
19064 if (!it->bidi_p)
19066 row->minpos = row->start.pos;
19067 row->maxpos = row->end.pos;
19069 else
19071 /* ROW->minpos and ROW->maxpos must be the smallest and
19072 `1 + the largest' buffer positions in ROW. But if ROW was
19073 bidi-reordered, these two positions can be anywhere in the
19074 row, so we must determine them now. */
19075 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19078 /* If the start of this line is the overlay arrow-position, then
19079 mark this glyph row as the one containing the overlay arrow.
19080 This is clearly a mess with variable size fonts. It would be
19081 better to let it be displayed like cursors under X. */
19082 if ((row->displays_text_p || !overlay_arrow_seen)
19083 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19084 !NILP (overlay_arrow_string)))
19086 /* Overlay arrow in window redisplay is a fringe bitmap. */
19087 if (STRINGP (overlay_arrow_string))
19089 struct glyph_row *arrow_row
19090 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19091 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19092 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19093 struct glyph *p = row->glyphs[TEXT_AREA];
19094 struct glyph *p2, *end;
19096 /* Copy the arrow glyphs. */
19097 while (glyph < arrow_end)
19098 *p++ = *glyph++;
19100 /* Throw away padding glyphs. */
19101 p2 = p;
19102 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19103 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19104 ++p2;
19105 if (p2 > p)
19107 while (p2 < end)
19108 *p++ = *p2++;
19109 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19112 else
19114 xassert (INTEGERP (overlay_arrow_string));
19115 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19117 overlay_arrow_seen = 1;
19120 /* Compute pixel dimensions of this line. */
19121 compute_line_metrics (it);
19123 /* Record whether this row ends inside an ellipsis. */
19124 row->ends_in_ellipsis_p
19125 = (it->method == GET_FROM_DISPLAY_VECTOR
19126 && it->ellipsis_p);
19128 /* Save fringe bitmaps in this row. */
19129 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19130 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19131 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19132 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19134 it->left_user_fringe_bitmap = 0;
19135 it->left_user_fringe_face_id = 0;
19136 it->right_user_fringe_bitmap = 0;
19137 it->right_user_fringe_face_id = 0;
19139 /* Maybe set the cursor. */
19140 cvpos = it->w->cursor.vpos;
19141 if ((cvpos < 0
19142 /* In bidi-reordered rows, keep checking for proper cursor
19143 position even if one has been found already, because buffer
19144 positions in such rows change non-linearly with ROW->VPOS,
19145 when a line is continued. One exception: when we are at ZV,
19146 display cursor on the first suitable glyph row, since all
19147 the empty rows after that also have their position set to ZV. */
19148 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19149 lines' rows is implemented for bidi-reordered rows. */
19150 || (it->bidi_p
19151 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19152 && PT >= MATRIX_ROW_START_CHARPOS (row)
19153 && PT <= MATRIX_ROW_END_CHARPOS (row)
19154 && cursor_row_p (row))
19155 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19157 /* Highlight trailing whitespace. */
19158 if (!NILP (Vshow_trailing_whitespace))
19159 highlight_trailing_whitespace (it->f, it->glyph_row);
19161 /* Prepare for the next line. This line starts horizontally at (X
19162 HPOS) = (0 0). Vertical positions are incremented. As a
19163 convenience for the caller, IT->glyph_row is set to the next
19164 row to be used. */
19165 it->current_x = it->hpos = 0;
19166 it->current_y += row->height;
19167 SET_TEXT_POS (it->eol_pos, 0, 0);
19168 ++it->vpos;
19169 ++it->glyph_row;
19170 /* The next row should by default use the same value of the
19171 reversed_p flag as this one. set_iterator_to_next decides when
19172 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19173 the flag accordingly. */
19174 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19175 it->glyph_row->reversed_p = row->reversed_p;
19176 it->start = row->end;
19177 return row->displays_text_p;
19179 #undef RECORD_MAX_MIN_POS
19182 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19183 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19184 doc: /* Return paragraph direction at point in BUFFER.
19185 Value is either `left-to-right' or `right-to-left'.
19186 If BUFFER is omitted or nil, it defaults to the current buffer.
19188 Paragraph direction determines how the text in the paragraph is displayed.
19189 In left-to-right paragraphs, text begins at the left margin of the window
19190 and the reading direction is generally left to right. In right-to-left
19191 paragraphs, text begins at the right margin and is read from right to left.
19193 See also `bidi-paragraph-direction'. */)
19194 (Lisp_Object buffer)
19196 struct buffer *buf = current_buffer;
19197 struct buffer *old = buf;
19199 if (! NILP (buffer))
19201 CHECK_BUFFER (buffer);
19202 buf = XBUFFER (buffer);
19205 if (NILP (BVAR (buf, bidi_display_reordering))
19206 || NILP (BVAR (buf, enable_multibyte_characters)))
19207 return Qleft_to_right;
19208 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19209 return BVAR (buf, bidi_paragraph_direction);
19210 else
19212 /* Determine the direction from buffer text. We could try to
19213 use current_matrix if it is up to date, but this seems fast
19214 enough as it is. */
19215 struct bidi_it itb;
19216 EMACS_INT pos = BUF_PT (buf);
19217 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19218 int c;
19220 set_buffer_temp (buf);
19221 /* bidi_paragraph_init finds the base direction of the paragraph
19222 by searching forward from paragraph start. We need the base
19223 direction of the current or _previous_ paragraph, so we need
19224 to make sure we are within that paragraph. To that end, find
19225 the previous non-empty line. */
19226 if (pos >= ZV && pos > BEGV)
19228 pos--;
19229 bytepos = CHAR_TO_BYTE (pos);
19231 while ((c = FETCH_BYTE (bytepos)) == '\n'
19232 || c == ' ' || c == '\t' || c == '\f')
19234 if (bytepos <= BEGV_BYTE)
19235 break;
19236 bytepos--;
19237 pos--;
19239 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19240 bytepos--;
19241 itb.charpos = pos;
19242 itb.bytepos = bytepos;
19243 itb.nchars = -1;
19244 itb.string.s = NULL;
19245 itb.string.lstring = Qnil;
19246 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
19247 itb.first_elt = 1;
19248 itb.separator_limit = -1;
19249 itb.paragraph_dir = NEUTRAL_DIR;
19251 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19252 set_buffer_temp (old);
19253 switch (itb.paragraph_dir)
19255 case L2R:
19256 return Qleft_to_right;
19257 break;
19258 case R2L:
19259 return Qright_to_left;
19260 break;
19261 default:
19262 abort ();
19269 /***********************************************************************
19270 Menu Bar
19271 ***********************************************************************/
19273 /* Redisplay the menu bar in the frame for window W.
19275 The menu bar of X frames that don't have X toolkit support is
19276 displayed in a special window W->frame->menu_bar_window.
19278 The menu bar of terminal frames is treated specially as far as
19279 glyph matrices are concerned. Menu bar lines are not part of
19280 windows, so the update is done directly on the frame matrix rows
19281 for the menu bar. */
19283 static void
19284 display_menu_bar (struct window *w)
19286 struct frame *f = XFRAME (WINDOW_FRAME (w));
19287 struct it it;
19288 Lisp_Object items;
19289 int i;
19291 /* Don't do all this for graphical frames. */
19292 #ifdef HAVE_NTGUI
19293 if (FRAME_W32_P (f))
19294 return;
19295 #endif
19296 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19297 if (FRAME_X_P (f))
19298 return;
19299 #endif
19301 #ifdef HAVE_NS
19302 if (FRAME_NS_P (f))
19303 return;
19304 #endif /* HAVE_NS */
19306 #ifdef USE_X_TOOLKIT
19307 xassert (!FRAME_WINDOW_P (f));
19308 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19309 it.first_visible_x = 0;
19310 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19311 #else /* not USE_X_TOOLKIT */
19312 if (FRAME_WINDOW_P (f))
19314 /* Menu bar lines are displayed in the desired matrix of the
19315 dummy window menu_bar_window. */
19316 struct window *menu_w;
19317 xassert (WINDOWP (f->menu_bar_window));
19318 menu_w = XWINDOW (f->menu_bar_window);
19319 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19320 MENU_FACE_ID);
19321 it.first_visible_x = 0;
19322 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19324 else
19326 /* This is a TTY frame, i.e. character hpos/vpos are used as
19327 pixel x/y. */
19328 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19329 MENU_FACE_ID);
19330 it.first_visible_x = 0;
19331 it.last_visible_x = FRAME_COLS (f);
19333 #endif /* not USE_X_TOOLKIT */
19335 /* FIXME: This should be controlled by a user option. See the
19336 comments in redisplay_tool_bar and display_mode_line about
19337 this. */
19338 it.paragraph_embedding = L2R;
19340 if (! mode_line_inverse_video)
19341 /* Force the menu-bar to be displayed in the default face. */
19342 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19344 /* Clear all rows of the menu bar. */
19345 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19347 struct glyph_row *row = it.glyph_row + i;
19348 clear_glyph_row (row);
19349 row->enabled_p = 1;
19350 row->full_width_p = 1;
19353 /* Display all items of the menu bar. */
19354 items = FRAME_MENU_BAR_ITEMS (it.f);
19355 for (i = 0; i < ASIZE (items); i += 4)
19357 Lisp_Object string;
19359 /* Stop at nil string. */
19360 string = AREF (items, i + 1);
19361 if (NILP (string))
19362 break;
19364 /* Remember where item was displayed. */
19365 ASET (items, i + 3, make_number (it.hpos));
19367 /* Display the item, pad with one space. */
19368 if (it.current_x < it.last_visible_x)
19369 display_string (NULL, string, Qnil, 0, 0, &it,
19370 SCHARS (string) + 1, 0, 0, -1);
19373 /* Fill out the line with spaces. */
19374 if (it.current_x < it.last_visible_x)
19375 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19377 /* Compute the total height of the lines. */
19378 compute_line_metrics (&it);
19383 /***********************************************************************
19384 Mode Line
19385 ***********************************************************************/
19387 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19388 FORCE is non-zero, redisplay mode lines unconditionally.
19389 Otherwise, redisplay only mode lines that are garbaged. Value is
19390 the number of windows whose mode lines were redisplayed. */
19392 static int
19393 redisplay_mode_lines (Lisp_Object window, int force)
19395 int nwindows = 0;
19397 while (!NILP (window))
19399 struct window *w = XWINDOW (window);
19401 if (WINDOWP (w->hchild))
19402 nwindows += redisplay_mode_lines (w->hchild, force);
19403 else if (WINDOWP (w->vchild))
19404 nwindows += redisplay_mode_lines (w->vchild, force);
19405 else if (force
19406 || FRAME_GARBAGED_P (XFRAME (w->frame))
19407 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19409 struct text_pos lpoint;
19410 struct buffer *old = current_buffer;
19412 /* Set the window's buffer for the mode line display. */
19413 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19414 set_buffer_internal_1 (XBUFFER (w->buffer));
19416 /* Point refers normally to the selected window. For any
19417 other window, set up appropriate value. */
19418 if (!EQ (window, selected_window))
19420 struct text_pos pt;
19422 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19423 if (CHARPOS (pt) < BEGV)
19424 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19425 else if (CHARPOS (pt) > (ZV - 1))
19426 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19427 else
19428 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19431 /* Display mode lines. */
19432 clear_glyph_matrix (w->desired_matrix);
19433 if (display_mode_lines (w))
19435 ++nwindows;
19436 w->must_be_updated_p = 1;
19439 /* Restore old settings. */
19440 set_buffer_internal_1 (old);
19441 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19444 window = w->next;
19447 return nwindows;
19451 /* Display the mode and/or header line of window W. Value is the
19452 sum number of mode lines and header lines displayed. */
19454 static int
19455 display_mode_lines (struct window *w)
19457 Lisp_Object old_selected_window, old_selected_frame;
19458 int n = 0;
19460 old_selected_frame = selected_frame;
19461 selected_frame = w->frame;
19462 old_selected_window = selected_window;
19463 XSETWINDOW (selected_window, w);
19465 /* These will be set while the mode line specs are processed. */
19466 line_number_displayed = 0;
19467 w->column_number_displayed = Qnil;
19469 if (WINDOW_WANTS_MODELINE_P (w))
19471 struct window *sel_w = XWINDOW (old_selected_window);
19473 /* Select mode line face based on the real selected window. */
19474 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19475 BVAR (current_buffer, mode_line_format));
19476 ++n;
19479 if (WINDOW_WANTS_HEADER_LINE_P (w))
19481 display_mode_line (w, HEADER_LINE_FACE_ID,
19482 BVAR (current_buffer, header_line_format));
19483 ++n;
19486 selected_frame = old_selected_frame;
19487 selected_window = old_selected_window;
19488 return n;
19492 /* Display mode or header line of window W. FACE_ID specifies which
19493 line to display; it is either MODE_LINE_FACE_ID or
19494 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19495 display. Value is the pixel height of the mode/header line
19496 displayed. */
19498 static int
19499 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19501 struct it it;
19502 struct face *face;
19503 int count = SPECPDL_INDEX ();
19505 init_iterator (&it, w, -1, -1, NULL, face_id);
19506 /* Don't extend on a previously drawn mode-line.
19507 This may happen if called from pos_visible_p. */
19508 it.glyph_row->enabled_p = 0;
19509 prepare_desired_row (it.glyph_row);
19511 it.glyph_row->mode_line_p = 1;
19513 if (! mode_line_inverse_video)
19514 /* Force the mode-line to be displayed in the default face. */
19515 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19517 /* FIXME: This should be controlled by a user option. But
19518 supporting such an option is not trivial, since the mode line is
19519 made up of many separate strings. */
19520 it.paragraph_embedding = L2R;
19522 record_unwind_protect (unwind_format_mode_line,
19523 format_mode_line_unwind_data (NULL, Qnil, 0));
19525 mode_line_target = MODE_LINE_DISPLAY;
19527 /* Temporarily make frame's keyboard the current kboard so that
19528 kboard-local variables in the mode_line_format will get the right
19529 values. */
19530 push_kboard (FRAME_KBOARD (it.f));
19531 record_unwind_save_match_data ();
19532 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19533 pop_kboard ();
19535 unbind_to (count, Qnil);
19537 /* Fill up with spaces. */
19538 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19540 compute_line_metrics (&it);
19541 it.glyph_row->full_width_p = 1;
19542 it.glyph_row->continued_p = 0;
19543 it.glyph_row->truncated_on_left_p = 0;
19544 it.glyph_row->truncated_on_right_p = 0;
19546 /* Make a 3D mode-line have a shadow at its right end. */
19547 face = FACE_FROM_ID (it.f, face_id);
19548 extend_face_to_end_of_line (&it);
19549 if (face->box != FACE_NO_BOX)
19551 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19552 + it.glyph_row->used[TEXT_AREA] - 1);
19553 last->right_box_line_p = 1;
19556 return it.glyph_row->height;
19559 /* Move element ELT in LIST to the front of LIST.
19560 Return the updated list. */
19562 static Lisp_Object
19563 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19565 register Lisp_Object tail, prev;
19566 register Lisp_Object tem;
19568 tail = list;
19569 prev = Qnil;
19570 while (CONSP (tail))
19572 tem = XCAR (tail);
19574 if (EQ (elt, tem))
19576 /* Splice out the link TAIL. */
19577 if (NILP (prev))
19578 list = XCDR (tail);
19579 else
19580 Fsetcdr (prev, XCDR (tail));
19582 /* Now make it the first. */
19583 Fsetcdr (tail, list);
19584 return tail;
19586 else
19587 prev = tail;
19588 tail = XCDR (tail);
19589 QUIT;
19592 /* Not found--return unchanged LIST. */
19593 return list;
19596 /* Contribute ELT to the mode line for window IT->w. How it
19597 translates into text depends on its data type.
19599 IT describes the display environment in which we display, as usual.
19601 DEPTH is the depth in recursion. It is used to prevent
19602 infinite recursion here.
19604 FIELD_WIDTH is the number of characters the display of ELT should
19605 occupy in the mode line, and PRECISION is the maximum number of
19606 characters to display from ELT's representation. See
19607 display_string for details.
19609 Returns the hpos of the end of the text generated by ELT.
19611 PROPS is a property list to add to any string we encounter.
19613 If RISKY is nonzero, remove (disregard) any properties in any string
19614 we encounter, and ignore :eval and :propertize.
19616 The global variable `mode_line_target' determines whether the
19617 output is passed to `store_mode_line_noprop',
19618 `store_mode_line_string', or `display_string'. */
19620 static int
19621 display_mode_element (struct it *it, int depth, int field_width, int precision,
19622 Lisp_Object elt, Lisp_Object props, int risky)
19624 int n = 0, field, prec;
19625 int literal = 0;
19627 tail_recurse:
19628 if (depth > 100)
19629 elt = build_string ("*too-deep*");
19631 depth++;
19633 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19635 case Lisp_String:
19637 /* A string: output it and check for %-constructs within it. */
19638 unsigned char c;
19639 EMACS_INT offset = 0;
19641 if (SCHARS (elt) > 0
19642 && (!NILP (props) || risky))
19644 Lisp_Object oprops, aelt;
19645 oprops = Ftext_properties_at (make_number (0), elt);
19647 /* If the starting string's properties are not what
19648 we want, translate the string. Also, if the string
19649 is risky, do that anyway. */
19651 if (NILP (Fequal (props, oprops)) || risky)
19653 /* If the starting string has properties,
19654 merge the specified ones onto the existing ones. */
19655 if (! NILP (oprops) && !risky)
19657 Lisp_Object tem;
19659 oprops = Fcopy_sequence (oprops);
19660 tem = props;
19661 while (CONSP (tem))
19663 oprops = Fplist_put (oprops, XCAR (tem),
19664 XCAR (XCDR (tem)));
19665 tem = XCDR (XCDR (tem));
19667 props = oprops;
19670 aelt = Fassoc (elt, mode_line_proptrans_alist);
19671 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19673 /* AELT is what we want. Move it to the front
19674 without consing. */
19675 elt = XCAR (aelt);
19676 mode_line_proptrans_alist
19677 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19679 else
19681 Lisp_Object tem;
19683 /* If AELT has the wrong props, it is useless.
19684 so get rid of it. */
19685 if (! NILP (aelt))
19686 mode_line_proptrans_alist
19687 = Fdelq (aelt, mode_line_proptrans_alist);
19689 elt = Fcopy_sequence (elt);
19690 Fset_text_properties (make_number (0), Flength (elt),
19691 props, elt);
19692 /* Add this item to mode_line_proptrans_alist. */
19693 mode_line_proptrans_alist
19694 = Fcons (Fcons (elt, props),
19695 mode_line_proptrans_alist);
19696 /* Truncate mode_line_proptrans_alist
19697 to at most 50 elements. */
19698 tem = Fnthcdr (make_number (50),
19699 mode_line_proptrans_alist);
19700 if (! NILP (tem))
19701 XSETCDR (tem, Qnil);
19706 offset = 0;
19708 if (literal)
19710 prec = precision - n;
19711 switch (mode_line_target)
19713 case MODE_LINE_NOPROP:
19714 case MODE_LINE_TITLE:
19715 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19716 break;
19717 case MODE_LINE_STRING:
19718 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19719 break;
19720 case MODE_LINE_DISPLAY:
19721 n += display_string (NULL, elt, Qnil, 0, 0, it,
19722 0, prec, 0, STRING_MULTIBYTE (elt));
19723 break;
19726 break;
19729 /* Handle the non-literal case. */
19731 while ((precision <= 0 || n < precision)
19732 && SREF (elt, offset) != 0
19733 && (mode_line_target != MODE_LINE_DISPLAY
19734 || it->current_x < it->last_visible_x))
19736 EMACS_INT last_offset = offset;
19738 /* Advance to end of string or next format specifier. */
19739 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19742 if (offset - 1 != last_offset)
19744 EMACS_INT nchars, nbytes;
19746 /* Output to end of string or up to '%'. Field width
19747 is length of string. Don't output more than
19748 PRECISION allows us. */
19749 offset--;
19751 prec = c_string_width (SDATA (elt) + last_offset,
19752 offset - last_offset, precision - n,
19753 &nchars, &nbytes);
19755 switch (mode_line_target)
19757 case MODE_LINE_NOPROP:
19758 case MODE_LINE_TITLE:
19759 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19760 break;
19761 case MODE_LINE_STRING:
19763 EMACS_INT bytepos = last_offset;
19764 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19765 EMACS_INT endpos = (precision <= 0
19766 ? string_byte_to_char (elt, offset)
19767 : charpos + nchars);
19769 n += store_mode_line_string (NULL,
19770 Fsubstring (elt, make_number (charpos),
19771 make_number (endpos)),
19772 0, 0, 0, Qnil);
19774 break;
19775 case MODE_LINE_DISPLAY:
19777 EMACS_INT bytepos = last_offset;
19778 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19780 if (precision <= 0)
19781 nchars = string_byte_to_char (elt, offset) - charpos;
19782 n += display_string (NULL, elt, Qnil, 0, charpos,
19783 it, 0, nchars, 0,
19784 STRING_MULTIBYTE (elt));
19786 break;
19789 else /* c == '%' */
19791 EMACS_INT percent_position = offset;
19793 /* Get the specified minimum width. Zero means
19794 don't pad. */
19795 field = 0;
19796 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19797 field = field * 10 + c - '0';
19799 /* Don't pad beyond the total padding allowed. */
19800 if (field_width - n > 0 && field > field_width - n)
19801 field = field_width - n;
19803 /* Note that either PRECISION <= 0 or N < PRECISION. */
19804 prec = precision - n;
19806 if (c == 'M')
19807 n += display_mode_element (it, depth, field, prec,
19808 Vglobal_mode_string, props,
19809 risky);
19810 else if (c != 0)
19812 int multibyte;
19813 EMACS_INT bytepos, charpos;
19814 const char *spec;
19815 Lisp_Object string;
19817 bytepos = percent_position;
19818 charpos = (STRING_MULTIBYTE (elt)
19819 ? string_byte_to_char (elt, bytepos)
19820 : bytepos);
19821 spec = decode_mode_spec (it->w, c, field, &string);
19822 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19824 switch (mode_line_target)
19826 case MODE_LINE_NOPROP:
19827 case MODE_LINE_TITLE:
19828 n += store_mode_line_noprop (spec, field, prec);
19829 break;
19830 case MODE_LINE_STRING:
19832 Lisp_Object tem = build_string (spec);
19833 props = Ftext_properties_at (make_number (charpos), elt);
19834 /* Should only keep face property in props */
19835 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19837 break;
19838 case MODE_LINE_DISPLAY:
19840 int nglyphs_before, nwritten;
19842 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19843 nwritten = display_string (spec, string, elt,
19844 charpos, 0, it,
19845 field, prec, 0,
19846 multibyte);
19848 /* Assign to the glyphs written above the
19849 string where the `%x' came from, position
19850 of the `%'. */
19851 if (nwritten > 0)
19853 struct glyph *glyph
19854 = (it->glyph_row->glyphs[TEXT_AREA]
19855 + nglyphs_before);
19856 int i;
19858 for (i = 0; i < nwritten; ++i)
19860 glyph[i].object = elt;
19861 glyph[i].charpos = charpos;
19864 n += nwritten;
19867 break;
19870 else /* c == 0 */
19871 break;
19875 break;
19877 case Lisp_Symbol:
19878 /* A symbol: process the value of the symbol recursively
19879 as if it appeared here directly. Avoid error if symbol void.
19880 Special case: if value of symbol is a string, output the string
19881 literally. */
19883 register Lisp_Object tem;
19885 /* If the variable is not marked as risky to set
19886 then its contents are risky to use. */
19887 if (NILP (Fget (elt, Qrisky_local_variable)))
19888 risky = 1;
19890 tem = Fboundp (elt);
19891 if (!NILP (tem))
19893 tem = Fsymbol_value (elt);
19894 /* If value is a string, output that string literally:
19895 don't check for % within it. */
19896 if (STRINGP (tem))
19897 literal = 1;
19899 if (!EQ (tem, elt))
19901 /* Give up right away for nil or t. */
19902 elt = tem;
19903 goto tail_recurse;
19907 break;
19909 case Lisp_Cons:
19911 register Lisp_Object car, tem;
19913 /* A cons cell: five distinct cases.
19914 If first element is :eval or :propertize, do something special.
19915 If first element is a string or a cons, process all the elements
19916 and effectively concatenate them.
19917 If first element is a negative number, truncate displaying cdr to
19918 at most that many characters. If positive, pad (with spaces)
19919 to at least that many characters.
19920 If first element is a symbol, process the cadr or caddr recursively
19921 according to whether the symbol's value is non-nil or nil. */
19922 car = XCAR (elt);
19923 if (EQ (car, QCeval))
19925 /* An element of the form (:eval FORM) means evaluate FORM
19926 and use the result as mode line elements. */
19928 if (risky)
19929 break;
19931 if (CONSP (XCDR (elt)))
19933 Lisp_Object spec;
19934 spec = safe_eval (XCAR (XCDR (elt)));
19935 n += display_mode_element (it, depth, field_width - n,
19936 precision - n, spec, props,
19937 risky);
19940 else if (EQ (car, QCpropertize))
19942 /* An element of the form (:propertize ELT PROPS...)
19943 means display ELT but applying properties PROPS. */
19945 if (risky)
19946 break;
19948 if (CONSP (XCDR (elt)))
19949 n += display_mode_element (it, depth, field_width - n,
19950 precision - n, XCAR (XCDR (elt)),
19951 XCDR (XCDR (elt)), risky);
19953 else if (SYMBOLP (car))
19955 tem = Fboundp (car);
19956 elt = XCDR (elt);
19957 if (!CONSP (elt))
19958 goto invalid;
19959 /* elt is now the cdr, and we know it is a cons cell.
19960 Use its car if CAR has a non-nil value. */
19961 if (!NILP (tem))
19963 tem = Fsymbol_value (car);
19964 if (!NILP (tem))
19966 elt = XCAR (elt);
19967 goto tail_recurse;
19970 /* Symbol's value is nil (or symbol is unbound)
19971 Get the cddr of the original list
19972 and if possible find the caddr and use that. */
19973 elt = XCDR (elt);
19974 if (NILP (elt))
19975 break;
19976 else if (!CONSP (elt))
19977 goto invalid;
19978 elt = XCAR (elt);
19979 goto tail_recurse;
19981 else if (INTEGERP (car))
19983 register int lim = XINT (car);
19984 elt = XCDR (elt);
19985 if (lim < 0)
19987 /* Negative int means reduce maximum width. */
19988 if (precision <= 0)
19989 precision = -lim;
19990 else
19991 precision = min (precision, -lim);
19993 else if (lim > 0)
19995 /* Padding specified. Don't let it be more than
19996 current maximum. */
19997 if (precision > 0)
19998 lim = min (precision, lim);
20000 /* If that's more padding than already wanted, queue it.
20001 But don't reduce padding already specified even if
20002 that is beyond the current truncation point. */
20003 field_width = max (lim, field_width);
20005 goto tail_recurse;
20007 else if (STRINGP (car) || CONSP (car))
20009 Lisp_Object halftail = elt;
20010 int len = 0;
20012 while (CONSP (elt)
20013 && (precision <= 0 || n < precision))
20015 n += display_mode_element (it, depth,
20016 /* Do padding only after the last
20017 element in the list. */
20018 (! CONSP (XCDR (elt))
20019 ? field_width - n
20020 : 0),
20021 precision - n, XCAR (elt),
20022 props, risky);
20023 elt = XCDR (elt);
20024 len++;
20025 if ((len & 1) == 0)
20026 halftail = XCDR (halftail);
20027 /* Check for cycle. */
20028 if (EQ (halftail, elt))
20029 break;
20033 break;
20035 default:
20036 invalid:
20037 elt = build_string ("*invalid*");
20038 goto tail_recurse;
20041 /* Pad to FIELD_WIDTH. */
20042 if (field_width > 0 && n < field_width)
20044 switch (mode_line_target)
20046 case MODE_LINE_NOPROP:
20047 case MODE_LINE_TITLE:
20048 n += store_mode_line_noprop ("", field_width - n, 0);
20049 break;
20050 case MODE_LINE_STRING:
20051 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20052 break;
20053 case MODE_LINE_DISPLAY:
20054 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20055 0, 0, 0);
20056 break;
20060 return n;
20063 /* Store a mode-line string element in mode_line_string_list.
20065 If STRING is non-null, display that C string. Otherwise, the Lisp
20066 string LISP_STRING is displayed.
20068 FIELD_WIDTH is the minimum number of output glyphs to produce.
20069 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20070 with spaces. FIELD_WIDTH <= 0 means don't pad.
20072 PRECISION is the maximum number of characters to output from
20073 STRING. PRECISION <= 0 means don't truncate the string.
20075 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20076 properties to the string.
20078 PROPS are the properties to add to the string.
20079 The mode_line_string_face face property is always added to the string.
20082 static int
20083 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20084 int field_width, int precision, Lisp_Object props)
20086 EMACS_INT len;
20087 int n = 0;
20089 if (string != NULL)
20091 len = strlen (string);
20092 if (precision > 0 && len > precision)
20093 len = precision;
20094 lisp_string = make_string (string, len);
20095 if (NILP (props))
20096 props = mode_line_string_face_prop;
20097 else if (!NILP (mode_line_string_face))
20099 Lisp_Object face = Fplist_get (props, Qface);
20100 props = Fcopy_sequence (props);
20101 if (NILP (face))
20102 face = mode_line_string_face;
20103 else
20104 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20105 props = Fplist_put (props, Qface, face);
20107 Fadd_text_properties (make_number (0), make_number (len),
20108 props, lisp_string);
20110 else
20112 len = XFASTINT (Flength (lisp_string));
20113 if (precision > 0 && len > precision)
20115 len = precision;
20116 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20117 precision = -1;
20119 if (!NILP (mode_line_string_face))
20121 Lisp_Object face;
20122 if (NILP (props))
20123 props = Ftext_properties_at (make_number (0), lisp_string);
20124 face = Fplist_get (props, Qface);
20125 if (NILP (face))
20126 face = mode_line_string_face;
20127 else
20128 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20129 props = Fcons (Qface, Fcons (face, Qnil));
20130 if (copy_string)
20131 lisp_string = Fcopy_sequence (lisp_string);
20133 if (!NILP (props))
20134 Fadd_text_properties (make_number (0), make_number (len),
20135 props, lisp_string);
20138 if (len > 0)
20140 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20141 n += len;
20144 if (field_width > len)
20146 field_width -= len;
20147 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20148 if (!NILP (props))
20149 Fadd_text_properties (make_number (0), make_number (field_width),
20150 props, lisp_string);
20151 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20152 n += field_width;
20155 return n;
20159 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20160 1, 4, 0,
20161 doc: /* Format a string out of a mode line format specification.
20162 First arg FORMAT specifies the mode line format (see `mode-line-format'
20163 for details) to use.
20165 By default, the format is evaluated for the currently selected window.
20167 Optional second arg FACE specifies the face property to put on all
20168 characters for which no face is specified. The value nil means the
20169 default face. The value t means whatever face the window's mode line
20170 currently uses (either `mode-line' or `mode-line-inactive',
20171 depending on whether the window is the selected window or not).
20172 An integer value means the value string has no text
20173 properties.
20175 Optional third and fourth args WINDOW and BUFFER specify the window
20176 and buffer to use as the context for the formatting (defaults
20177 are the selected window and the WINDOW's buffer). */)
20178 (Lisp_Object format, Lisp_Object face,
20179 Lisp_Object window, Lisp_Object buffer)
20181 struct it it;
20182 int len;
20183 struct window *w;
20184 struct buffer *old_buffer = NULL;
20185 int face_id;
20186 int no_props = INTEGERP (face);
20187 int count = SPECPDL_INDEX ();
20188 Lisp_Object str;
20189 int string_start = 0;
20191 if (NILP (window))
20192 window = selected_window;
20193 CHECK_WINDOW (window);
20194 w = XWINDOW (window);
20196 if (NILP (buffer))
20197 buffer = w->buffer;
20198 CHECK_BUFFER (buffer);
20200 /* Make formatting the modeline a non-op when noninteractive, otherwise
20201 there will be problems later caused by a partially initialized frame. */
20202 if (NILP (format) || noninteractive)
20203 return empty_unibyte_string;
20205 if (no_props)
20206 face = Qnil;
20208 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20209 : EQ (face, Qt) ? (EQ (window, selected_window)
20210 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20211 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20212 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20213 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20214 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20215 : DEFAULT_FACE_ID;
20217 if (XBUFFER (buffer) != current_buffer)
20218 old_buffer = current_buffer;
20220 /* Save things including mode_line_proptrans_alist,
20221 and set that to nil so that we don't alter the outer value. */
20222 record_unwind_protect (unwind_format_mode_line,
20223 format_mode_line_unwind_data
20224 (old_buffer, selected_window, 1));
20225 mode_line_proptrans_alist = Qnil;
20227 Fselect_window (window, Qt);
20228 if (old_buffer)
20229 set_buffer_internal_1 (XBUFFER (buffer));
20231 init_iterator (&it, w, -1, -1, NULL, face_id);
20233 if (no_props)
20235 mode_line_target = MODE_LINE_NOPROP;
20236 mode_line_string_face_prop = Qnil;
20237 mode_line_string_list = Qnil;
20238 string_start = MODE_LINE_NOPROP_LEN (0);
20240 else
20242 mode_line_target = MODE_LINE_STRING;
20243 mode_line_string_list = Qnil;
20244 mode_line_string_face = face;
20245 mode_line_string_face_prop
20246 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20249 push_kboard (FRAME_KBOARD (it.f));
20250 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20251 pop_kboard ();
20253 if (no_props)
20255 len = MODE_LINE_NOPROP_LEN (string_start);
20256 str = make_string (mode_line_noprop_buf + string_start, len);
20258 else
20260 mode_line_string_list = Fnreverse (mode_line_string_list);
20261 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20262 empty_unibyte_string);
20265 unbind_to (count, Qnil);
20266 return str;
20269 /* Write a null-terminated, right justified decimal representation of
20270 the positive integer D to BUF using a minimal field width WIDTH. */
20272 static void
20273 pint2str (register char *buf, register int width, register EMACS_INT d)
20275 register char *p = buf;
20277 if (d <= 0)
20278 *p++ = '0';
20279 else
20281 while (d > 0)
20283 *p++ = d % 10 + '0';
20284 d /= 10;
20288 for (width -= (int) (p - buf); width > 0; --width)
20289 *p++ = ' ';
20290 *p-- = '\0';
20291 while (p > buf)
20293 d = *buf;
20294 *buf++ = *p;
20295 *p-- = d;
20299 /* Write a null-terminated, right justified decimal and "human
20300 readable" representation of the nonnegative integer D to BUF using
20301 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20303 static const char power_letter[] =
20305 0, /* no letter */
20306 'k', /* kilo */
20307 'M', /* mega */
20308 'G', /* giga */
20309 'T', /* tera */
20310 'P', /* peta */
20311 'E', /* exa */
20312 'Z', /* zetta */
20313 'Y' /* yotta */
20316 static void
20317 pint2hrstr (char *buf, int width, EMACS_INT d)
20319 /* We aim to represent the nonnegative integer D as
20320 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20321 EMACS_INT quotient = d;
20322 int remainder = 0;
20323 /* -1 means: do not use TENTHS. */
20324 int tenths = -1;
20325 int exponent = 0;
20327 /* Length of QUOTIENT.TENTHS as a string. */
20328 int length;
20330 char * psuffix;
20331 char * p;
20333 if (1000 <= quotient)
20335 /* Scale to the appropriate EXPONENT. */
20338 remainder = quotient % 1000;
20339 quotient /= 1000;
20340 exponent++;
20342 while (1000 <= quotient);
20344 /* Round to nearest and decide whether to use TENTHS or not. */
20345 if (quotient <= 9)
20347 tenths = remainder / 100;
20348 if (50 <= remainder % 100)
20350 if (tenths < 9)
20351 tenths++;
20352 else
20354 quotient++;
20355 if (quotient == 10)
20356 tenths = -1;
20357 else
20358 tenths = 0;
20362 else
20363 if (500 <= remainder)
20365 if (quotient < 999)
20366 quotient++;
20367 else
20369 quotient = 1;
20370 exponent++;
20371 tenths = 0;
20376 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20377 if (tenths == -1 && quotient <= 99)
20378 if (quotient <= 9)
20379 length = 1;
20380 else
20381 length = 2;
20382 else
20383 length = 3;
20384 p = psuffix = buf + max (width, length);
20386 /* Print EXPONENT. */
20387 *psuffix++ = power_letter[exponent];
20388 *psuffix = '\0';
20390 /* Print TENTHS. */
20391 if (tenths >= 0)
20393 *--p = '0' + tenths;
20394 *--p = '.';
20397 /* Print QUOTIENT. */
20400 int digit = quotient % 10;
20401 *--p = '0' + digit;
20403 while ((quotient /= 10) != 0);
20405 /* Print leading spaces. */
20406 while (buf < p)
20407 *--p = ' ';
20410 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20411 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20412 type of CODING_SYSTEM. Return updated pointer into BUF. */
20414 static unsigned char invalid_eol_type[] = "(*invalid*)";
20416 static char *
20417 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20419 Lisp_Object val;
20420 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20421 const unsigned char *eol_str;
20422 int eol_str_len;
20423 /* The EOL conversion we are using. */
20424 Lisp_Object eoltype;
20426 val = CODING_SYSTEM_SPEC (coding_system);
20427 eoltype = Qnil;
20429 if (!VECTORP (val)) /* Not yet decided. */
20431 if (multibyte)
20432 *buf++ = '-';
20433 if (eol_flag)
20434 eoltype = eol_mnemonic_undecided;
20435 /* Don't mention EOL conversion if it isn't decided. */
20437 else
20439 Lisp_Object attrs;
20440 Lisp_Object eolvalue;
20442 attrs = AREF (val, 0);
20443 eolvalue = AREF (val, 2);
20445 if (multibyte)
20446 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20448 if (eol_flag)
20450 /* The EOL conversion that is normal on this system. */
20452 if (NILP (eolvalue)) /* Not yet decided. */
20453 eoltype = eol_mnemonic_undecided;
20454 else if (VECTORP (eolvalue)) /* Not yet decided. */
20455 eoltype = eol_mnemonic_undecided;
20456 else /* eolvalue is Qunix, Qdos, or Qmac. */
20457 eoltype = (EQ (eolvalue, Qunix)
20458 ? eol_mnemonic_unix
20459 : (EQ (eolvalue, Qdos) == 1
20460 ? eol_mnemonic_dos : eol_mnemonic_mac));
20464 if (eol_flag)
20466 /* Mention the EOL conversion if it is not the usual one. */
20467 if (STRINGP (eoltype))
20469 eol_str = SDATA (eoltype);
20470 eol_str_len = SBYTES (eoltype);
20472 else if (CHARACTERP (eoltype))
20474 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20475 int c = XFASTINT (eoltype);
20476 eol_str_len = CHAR_STRING (c, tmp);
20477 eol_str = tmp;
20479 else
20481 eol_str = invalid_eol_type;
20482 eol_str_len = sizeof (invalid_eol_type) - 1;
20484 memcpy (buf, eol_str, eol_str_len);
20485 buf += eol_str_len;
20488 return buf;
20491 /* Return a string for the output of a mode line %-spec for window W,
20492 generated by character C. FIELD_WIDTH > 0 means pad the string
20493 returned with spaces to that value. Return a Lisp string in
20494 *STRING if the resulting string is taken from that Lisp string.
20496 Note we operate on the current buffer for most purposes,
20497 the exception being w->base_line_pos. */
20499 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20501 static const char *
20502 decode_mode_spec (struct window *w, register int c, int field_width,
20503 Lisp_Object *string)
20505 Lisp_Object obj;
20506 struct frame *f = XFRAME (WINDOW_FRAME (w));
20507 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20508 struct buffer *b = current_buffer;
20510 obj = Qnil;
20511 *string = Qnil;
20513 switch (c)
20515 case '*':
20516 if (!NILP (BVAR (b, read_only)))
20517 return "%";
20518 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20519 return "*";
20520 return "-";
20522 case '+':
20523 /* This differs from %* only for a modified read-only buffer. */
20524 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20525 return "*";
20526 if (!NILP (BVAR (b, read_only)))
20527 return "%";
20528 return "-";
20530 case '&':
20531 /* This differs from %* in ignoring read-only-ness. */
20532 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20533 return "*";
20534 return "-";
20536 case '%':
20537 return "%";
20539 case '[':
20541 int i;
20542 char *p;
20544 if (command_loop_level > 5)
20545 return "[[[... ";
20546 p = decode_mode_spec_buf;
20547 for (i = 0; i < command_loop_level; i++)
20548 *p++ = '[';
20549 *p = 0;
20550 return decode_mode_spec_buf;
20553 case ']':
20555 int i;
20556 char *p;
20558 if (command_loop_level > 5)
20559 return " ...]]]";
20560 p = decode_mode_spec_buf;
20561 for (i = 0; i < command_loop_level; i++)
20562 *p++ = ']';
20563 *p = 0;
20564 return decode_mode_spec_buf;
20567 case '-':
20569 register int i;
20571 /* Let lots_of_dashes be a string of infinite length. */
20572 if (mode_line_target == MODE_LINE_NOPROP ||
20573 mode_line_target == MODE_LINE_STRING)
20574 return "--";
20575 if (field_width <= 0
20576 || field_width > sizeof (lots_of_dashes))
20578 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20579 decode_mode_spec_buf[i] = '-';
20580 decode_mode_spec_buf[i] = '\0';
20581 return decode_mode_spec_buf;
20583 else
20584 return lots_of_dashes;
20587 case 'b':
20588 obj = BVAR (b, name);
20589 break;
20591 case 'c':
20592 /* %c and %l are ignored in `frame-title-format'.
20593 (In redisplay_internal, the frame title is drawn _before_ the
20594 windows are updated, so the stuff which depends on actual
20595 window contents (such as %l) may fail to render properly, or
20596 even crash emacs.) */
20597 if (mode_line_target == MODE_LINE_TITLE)
20598 return "";
20599 else
20601 EMACS_INT col = current_column ();
20602 w->column_number_displayed = make_number (col);
20603 pint2str (decode_mode_spec_buf, field_width, col);
20604 return decode_mode_spec_buf;
20607 case 'e':
20608 #ifndef SYSTEM_MALLOC
20610 if (NILP (Vmemory_full))
20611 return "";
20612 else
20613 return "!MEM FULL! ";
20615 #else
20616 return "";
20617 #endif
20619 case 'F':
20620 /* %F displays the frame name. */
20621 if (!NILP (f->title))
20622 return SSDATA (f->title);
20623 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20624 return SSDATA (f->name);
20625 return "Emacs";
20627 case 'f':
20628 obj = BVAR (b, filename);
20629 break;
20631 case 'i':
20633 EMACS_INT size = ZV - BEGV;
20634 pint2str (decode_mode_spec_buf, field_width, size);
20635 return decode_mode_spec_buf;
20638 case 'I':
20640 EMACS_INT size = ZV - BEGV;
20641 pint2hrstr (decode_mode_spec_buf, field_width, size);
20642 return decode_mode_spec_buf;
20645 case 'l':
20647 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20648 EMACS_INT topline, nlines, height;
20649 EMACS_INT junk;
20651 /* %c and %l are ignored in `frame-title-format'. */
20652 if (mode_line_target == MODE_LINE_TITLE)
20653 return "";
20655 startpos = XMARKER (w->start)->charpos;
20656 startpos_byte = marker_byte_position (w->start);
20657 height = WINDOW_TOTAL_LINES (w);
20659 /* If we decided that this buffer isn't suitable for line numbers,
20660 don't forget that too fast. */
20661 if (EQ (w->base_line_pos, w->buffer))
20662 goto no_value;
20663 /* But do forget it, if the window shows a different buffer now. */
20664 else if (BUFFERP (w->base_line_pos))
20665 w->base_line_pos = Qnil;
20667 /* If the buffer is very big, don't waste time. */
20668 if (INTEGERP (Vline_number_display_limit)
20669 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20671 w->base_line_pos = Qnil;
20672 w->base_line_number = Qnil;
20673 goto no_value;
20676 if (INTEGERP (w->base_line_number)
20677 && INTEGERP (w->base_line_pos)
20678 && XFASTINT (w->base_line_pos) <= startpos)
20680 line = XFASTINT (w->base_line_number);
20681 linepos = XFASTINT (w->base_line_pos);
20682 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20684 else
20686 line = 1;
20687 linepos = BUF_BEGV (b);
20688 linepos_byte = BUF_BEGV_BYTE (b);
20691 /* Count lines from base line to window start position. */
20692 nlines = display_count_lines (linepos_byte,
20693 startpos_byte,
20694 startpos, &junk);
20696 topline = nlines + line;
20698 /* Determine a new base line, if the old one is too close
20699 or too far away, or if we did not have one.
20700 "Too close" means it's plausible a scroll-down would
20701 go back past it. */
20702 if (startpos == BUF_BEGV (b))
20704 w->base_line_number = make_number (topline);
20705 w->base_line_pos = make_number (BUF_BEGV (b));
20707 else if (nlines < height + 25 || nlines > height * 3 + 50
20708 || linepos == BUF_BEGV (b))
20710 EMACS_INT limit = BUF_BEGV (b);
20711 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20712 EMACS_INT position;
20713 EMACS_INT distance =
20714 (height * 2 + 30) * line_number_display_limit_width;
20716 if (startpos - distance > limit)
20718 limit = startpos - distance;
20719 limit_byte = CHAR_TO_BYTE (limit);
20722 nlines = display_count_lines (startpos_byte,
20723 limit_byte,
20724 - (height * 2 + 30),
20725 &position);
20726 /* If we couldn't find the lines we wanted within
20727 line_number_display_limit_width chars per line,
20728 give up on line numbers for this window. */
20729 if (position == limit_byte && limit == startpos - distance)
20731 w->base_line_pos = w->buffer;
20732 w->base_line_number = Qnil;
20733 goto no_value;
20736 w->base_line_number = make_number (topline - nlines);
20737 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20740 /* Now count lines from the start pos to point. */
20741 nlines = display_count_lines (startpos_byte,
20742 PT_BYTE, PT, &junk);
20744 /* Record that we did display the line number. */
20745 line_number_displayed = 1;
20747 /* Make the string to show. */
20748 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20749 return decode_mode_spec_buf;
20750 no_value:
20752 char* p = decode_mode_spec_buf;
20753 int pad = field_width - 2;
20754 while (pad-- > 0)
20755 *p++ = ' ';
20756 *p++ = '?';
20757 *p++ = '?';
20758 *p = '\0';
20759 return decode_mode_spec_buf;
20762 break;
20764 case 'm':
20765 obj = BVAR (b, mode_name);
20766 break;
20768 case 'n':
20769 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20770 return " Narrow";
20771 break;
20773 case 'p':
20775 EMACS_INT pos = marker_position (w->start);
20776 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20778 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20780 if (pos <= BUF_BEGV (b))
20781 return "All";
20782 else
20783 return "Bottom";
20785 else if (pos <= BUF_BEGV (b))
20786 return "Top";
20787 else
20789 if (total > 1000000)
20790 /* Do it differently for a large value, to avoid overflow. */
20791 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20792 else
20793 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20794 /* We can't normally display a 3-digit number,
20795 so get us a 2-digit number that is close. */
20796 if (total == 100)
20797 total = 99;
20798 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20799 return decode_mode_spec_buf;
20803 /* Display percentage of size above the bottom of the screen. */
20804 case 'P':
20806 EMACS_INT toppos = marker_position (w->start);
20807 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20808 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20810 if (botpos >= BUF_ZV (b))
20812 if (toppos <= BUF_BEGV (b))
20813 return "All";
20814 else
20815 return "Bottom";
20817 else
20819 if (total > 1000000)
20820 /* Do it differently for a large value, to avoid overflow. */
20821 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20822 else
20823 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20824 /* We can't normally display a 3-digit number,
20825 so get us a 2-digit number that is close. */
20826 if (total == 100)
20827 total = 99;
20828 if (toppos <= BUF_BEGV (b))
20829 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20830 else
20831 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20832 return decode_mode_spec_buf;
20836 case 's':
20837 /* status of process */
20838 obj = Fget_buffer_process (Fcurrent_buffer ());
20839 if (NILP (obj))
20840 return "no process";
20841 #ifndef MSDOS
20842 obj = Fsymbol_name (Fprocess_status (obj));
20843 #endif
20844 break;
20846 case '@':
20848 int count = inhibit_garbage_collection ();
20849 Lisp_Object val = call1 (intern ("file-remote-p"),
20850 BVAR (current_buffer, directory));
20851 unbind_to (count, Qnil);
20853 if (NILP (val))
20854 return "-";
20855 else
20856 return "@";
20859 case 't': /* indicate TEXT or BINARY */
20860 return "T";
20862 case 'z':
20863 /* coding-system (not including end-of-line format) */
20864 case 'Z':
20865 /* coding-system (including end-of-line type) */
20867 int eol_flag = (c == 'Z');
20868 char *p = decode_mode_spec_buf;
20870 if (! FRAME_WINDOW_P (f))
20872 /* No need to mention EOL here--the terminal never needs
20873 to do EOL conversion. */
20874 p = decode_mode_spec_coding (CODING_ID_NAME
20875 (FRAME_KEYBOARD_CODING (f)->id),
20876 p, 0);
20877 p = decode_mode_spec_coding (CODING_ID_NAME
20878 (FRAME_TERMINAL_CODING (f)->id),
20879 p, 0);
20881 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20882 p, eol_flag);
20884 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20885 #ifdef subprocesses
20886 obj = Fget_buffer_process (Fcurrent_buffer ());
20887 if (PROCESSP (obj))
20889 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20890 p, eol_flag);
20891 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20892 p, eol_flag);
20894 #endif /* subprocesses */
20895 #endif /* 0 */
20896 *p = 0;
20897 return decode_mode_spec_buf;
20901 if (STRINGP (obj))
20903 *string = obj;
20904 return SSDATA (obj);
20906 else
20907 return "";
20911 /* Count up to COUNT lines starting from START_BYTE.
20912 But don't go beyond LIMIT_BYTE.
20913 Return the number of lines thus found (always nonnegative).
20915 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20917 static EMACS_INT
20918 display_count_lines (EMACS_INT start_byte,
20919 EMACS_INT limit_byte, EMACS_INT count,
20920 EMACS_INT *byte_pos_ptr)
20922 register unsigned char *cursor;
20923 unsigned char *base;
20925 register EMACS_INT ceiling;
20926 register unsigned char *ceiling_addr;
20927 EMACS_INT orig_count = count;
20929 /* If we are not in selective display mode,
20930 check only for newlines. */
20931 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20932 && !INTEGERP (BVAR (current_buffer, selective_display)));
20934 if (count > 0)
20936 while (start_byte < limit_byte)
20938 ceiling = BUFFER_CEILING_OF (start_byte);
20939 ceiling = min (limit_byte - 1, ceiling);
20940 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20941 base = (cursor = BYTE_POS_ADDR (start_byte));
20942 while (1)
20944 if (selective_display)
20945 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20947 else
20948 while (*cursor != '\n' && ++cursor != ceiling_addr)
20951 if (cursor != ceiling_addr)
20953 if (--count == 0)
20955 start_byte += cursor - base + 1;
20956 *byte_pos_ptr = start_byte;
20957 return orig_count;
20959 else
20960 if (++cursor == ceiling_addr)
20961 break;
20963 else
20964 break;
20966 start_byte += cursor - base;
20969 else
20971 while (start_byte > limit_byte)
20973 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20974 ceiling = max (limit_byte, ceiling);
20975 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20976 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20977 while (1)
20979 if (selective_display)
20980 while (--cursor != ceiling_addr
20981 && *cursor != '\n' && *cursor != 015)
20983 else
20984 while (--cursor != ceiling_addr && *cursor != '\n')
20987 if (cursor != ceiling_addr)
20989 if (++count == 0)
20991 start_byte += cursor - base + 1;
20992 *byte_pos_ptr = start_byte;
20993 /* When scanning backwards, we should
20994 not count the newline posterior to which we stop. */
20995 return - orig_count - 1;
20998 else
20999 break;
21001 /* Here we add 1 to compensate for the last decrement
21002 of CURSOR, which took it past the valid range. */
21003 start_byte += cursor - base + 1;
21007 *byte_pos_ptr = limit_byte;
21009 if (count < 0)
21010 return - orig_count + count;
21011 return orig_count - count;
21017 /***********************************************************************
21018 Displaying strings
21019 ***********************************************************************/
21021 /* Display a NUL-terminated string, starting with index START.
21023 If STRING is non-null, display that C string. Otherwise, the Lisp
21024 string LISP_STRING is displayed. There's a case that STRING is
21025 non-null and LISP_STRING is not nil. It means STRING is a string
21026 data of LISP_STRING. In that case, we display LISP_STRING while
21027 ignoring its text properties.
21029 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21030 FACE_STRING. Display STRING or LISP_STRING with the face at
21031 FACE_STRING_POS in FACE_STRING:
21033 Display the string in the environment given by IT, but use the
21034 standard display table, temporarily.
21036 FIELD_WIDTH is the minimum number of output glyphs to produce.
21037 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21038 with spaces. If STRING has more characters, more than FIELD_WIDTH
21039 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21041 PRECISION is the maximum number of characters to output from
21042 STRING. PRECISION < 0 means don't truncate the string.
21044 This is roughly equivalent to printf format specifiers:
21046 FIELD_WIDTH PRECISION PRINTF
21047 ----------------------------------------
21048 -1 -1 %s
21049 -1 10 %.10s
21050 10 -1 %10s
21051 20 10 %20.10s
21053 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21054 display them, and < 0 means obey the current buffer's value of
21055 enable_multibyte_characters.
21057 Value is the number of columns displayed. */
21059 static int
21060 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21061 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21062 int field_width, int precision, int max_x, int multibyte)
21064 int hpos_at_start = it->hpos;
21065 int saved_face_id = it->face_id;
21066 struct glyph_row *row = it->glyph_row;
21067 EMACS_INT it_charpos;
21069 /* Initialize the iterator IT for iteration over STRING beginning
21070 with index START. */
21071 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21072 precision, field_width, multibyte);
21073 if (string && STRINGP (lisp_string))
21074 /* LISP_STRING is the one returned by decode_mode_spec. We should
21075 ignore its text properties. */
21076 it->stop_charpos = it->end_charpos;
21078 /* If displaying STRING, set up the face of the iterator from
21079 FACE_STRING, if that's given. */
21080 if (STRINGP (face_string))
21082 EMACS_INT endptr;
21083 struct face *face;
21085 it->face_id
21086 = face_at_string_position (it->w, face_string, face_string_pos,
21087 0, it->region_beg_charpos,
21088 it->region_end_charpos,
21089 &endptr, it->base_face_id, 0);
21090 face = FACE_FROM_ID (it->f, it->face_id);
21091 it->face_box_p = face->box != FACE_NO_BOX;
21094 /* Set max_x to the maximum allowed X position. Don't let it go
21095 beyond the right edge of the window. */
21096 if (max_x <= 0)
21097 max_x = it->last_visible_x;
21098 else
21099 max_x = min (max_x, it->last_visible_x);
21101 /* Skip over display elements that are not visible. because IT->w is
21102 hscrolled. */
21103 if (it->current_x < it->first_visible_x)
21104 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21105 MOVE_TO_POS | MOVE_TO_X);
21107 row->ascent = it->max_ascent;
21108 row->height = it->max_ascent + it->max_descent;
21109 row->phys_ascent = it->max_phys_ascent;
21110 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21111 row->extra_line_spacing = it->max_extra_line_spacing;
21113 if (STRINGP (it->string))
21114 it_charpos = IT_STRING_CHARPOS (*it);
21115 else
21116 it_charpos = IT_CHARPOS (*it);
21118 /* This condition is for the case that we are called with current_x
21119 past last_visible_x. */
21120 while (it->current_x < max_x)
21122 int x_before, x, n_glyphs_before, i, nglyphs;
21124 /* Get the next display element. */
21125 if (!get_next_display_element (it))
21126 break;
21128 /* Produce glyphs. */
21129 x_before = it->current_x;
21130 n_glyphs_before = row->used[TEXT_AREA];
21131 PRODUCE_GLYPHS (it);
21133 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21134 i = 0;
21135 x = x_before;
21136 while (i < nglyphs)
21138 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21140 if (it->line_wrap != TRUNCATE
21141 && x + glyph->pixel_width > max_x)
21143 /* End of continued line or max_x reached. */
21144 if (CHAR_GLYPH_PADDING_P (*glyph))
21146 /* A wide character is unbreakable. */
21147 if (row->reversed_p)
21148 unproduce_glyphs (it, row->used[TEXT_AREA]
21149 - n_glyphs_before);
21150 row->used[TEXT_AREA] = n_glyphs_before;
21151 it->current_x = x_before;
21153 else
21155 if (row->reversed_p)
21156 unproduce_glyphs (it, row->used[TEXT_AREA]
21157 - (n_glyphs_before + i));
21158 row->used[TEXT_AREA] = n_glyphs_before + i;
21159 it->current_x = x;
21161 break;
21163 else if (x + glyph->pixel_width >= it->first_visible_x)
21165 /* Glyph is at least partially visible. */
21166 ++it->hpos;
21167 if (x < it->first_visible_x)
21168 row->x = x - it->first_visible_x;
21170 else
21172 /* Glyph is off the left margin of the display area.
21173 Should not happen. */
21174 abort ();
21177 row->ascent = max (row->ascent, it->max_ascent);
21178 row->height = max (row->height, it->max_ascent + it->max_descent);
21179 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21180 row->phys_height = max (row->phys_height,
21181 it->max_phys_ascent + it->max_phys_descent);
21182 row->extra_line_spacing = max (row->extra_line_spacing,
21183 it->max_extra_line_spacing);
21184 x += glyph->pixel_width;
21185 ++i;
21188 /* Stop if max_x reached. */
21189 if (i < nglyphs)
21190 break;
21192 /* Stop at line ends. */
21193 if (ITERATOR_AT_END_OF_LINE_P (it))
21195 it->continuation_lines_width = 0;
21196 break;
21199 set_iterator_to_next (it, 1);
21200 if (STRINGP (it->string))
21201 it_charpos = IT_STRING_CHARPOS (*it);
21202 else
21203 it_charpos = IT_CHARPOS (*it);
21205 /* Stop if truncating at the right edge. */
21206 if (it->line_wrap == TRUNCATE
21207 && it->current_x >= it->last_visible_x)
21209 /* Add truncation mark, but don't do it if the line is
21210 truncated at a padding space. */
21211 if (it_charpos < it->string_nchars)
21213 if (!FRAME_WINDOW_P (it->f))
21215 int ii, n;
21217 if (it->current_x > it->last_visible_x)
21219 if (!row->reversed_p)
21221 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21222 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21223 break;
21225 else
21227 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21228 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21229 break;
21230 unproduce_glyphs (it, ii + 1);
21231 ii = row->used[TEXT_AREA] - (ii + 1);
21233 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21235 row->used[TEXT_AREA] = ii;
21236 produce_special_glyphs (it, IT_TRUNCATION);
21239 produce_special_glyphs (it, IT_TRUNCATION);
21241 row->truncated_on_right_p = 1;
21243 break;
21247 /* Maybe insert a truncation at the left. */
21248 if (it->first_visible_x
21249 && it_charpos > 0)
21251 if (!FRAME_WINDOW_P (it->f))
21252 insert_left_trunc_glyphs (it);
21253 row->truncated_on_left_p = 1;
21256 it->face_id = saved_face_id;
21258 /* Value is number of columns displayed. */
21259 return it->hpos - hpos_at_start;
21264 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21265 appears as an element of LIST or as the car of an element of LIST.
21266 If PROPVAL is a list, compare each element against LIST in that
21267 way, and return 1/2 if any element of PROPVAL is found in LIST.
21268 Otherwise return 0. This function cannot quit.
21269 The return value is 2 if the text is invisible but with an ellipsis
21270 and 1 if it's invisible and without an ellipsis. */
21273 invisible_p (register Lisp_Object propval, Lisp_Object list)
21275 register Lisp_Object tail, proptail;
21277 for (tail = list; CONSP (tail); tail = XCDR (tail))
21279 register Lisp_Object tem;
21280 tem = XCAR (tail);
21281 if (EQ (propval, tem))
21282 return 1;
21283 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21284 return NILP (XCDR (tem)) ? 1 : 2;
21287 if (CONSP (propval))
21289 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21291 Lisp_Object propelt;
21292 propelt = XCAR (proptail);
21293 for (tail = list; CONSP (tail); tail = XCDR (tail))
21295 register Lisp_Object tem;
21296 tem = XCAR (tail);
21297 if (EQ (propelt, tem))
21298 return 1;
21299 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21300 return NILP (XCDR (tem)) ? 1 : 2;
21305 return 0;
21308 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21309 doc: /* Non-nil if the property makes the text invisible.
21310 POS-OR-PROP can be a marker or number, in which case it is taken to be
21311 a position in the current buffer and the value of the `invisible' property
21312 is checked; or it can be some other value, which is then presumed to be the
21313 value of the `invisible' property of the text of interest.
21314 The non-nil value returned can be t for truly invisible text or something
21315 else if the text is replaced by an ellipsis. */)
21316 (Lisp_Object pos_or_prop)
21318 Lisp_Object prop
21319 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21320 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21321 : pos_or_prop);
21322 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21323 return (invis == 0 ? Qnil
21324 : invis == 1 ? Qt
21325 : make_number (invis));
21328 /* Calculate a width or height in pixels from a specification using
21329 the following elements:
21331 SPEC ::=
21332 NUM - a (fractional) multiple of the default font width/height
21333 (NUM) - specifies exactly NUM pixels
21334 UNIT - a fixed number of pixels, see below.
21335 ELEMENT - size of a display element in pixels, see below.
21336 (NUM . SPEC) - equals NUM * SPEC
21337 (+ SPEC SPEC ...) - add pixel values
21338 (- SPEC SPEC ...) - subtract pixel values
21339 (- SPEC) - negate pixel value
21341 NUM ::=
21342 INT or FLOAT - a number constant
21343 SYMBOL - use symbol's (buffer local) variable binding.
21345 UNIT ::=
21346 in - pixels per inch *)
21347 mm - pixels per 1/1000 meter *)
21348 cm - pixels per 1/100 meter *)
21349 width - width of current font in pixels.
21350 height - height of current font in pixels.
21352 *) using the ratio(s) defined in display-pixels-per-inch.
21354 ELEMENT ::=
21356 left-fringe - left fringe width in pixels
21357 right-fringe - right fringe width in pixels
21359 left-margin - left margin width in pixels
21360 right-margin - right margin width in pixels
21362 scroll-bar - scroll-bar area width in pixels
21364 Examples:
21366 Pixels corresponding to 5 inches:
21367 (5 . in)
21369 Total width of non-text areas on left side of window (if scroll-bar is on left):
21370 '(space :width (+ left-fringe left-margin scroll-bar))
21372 Align to first text column (in header line):
21373 '(space :align-to 0)
21375 Align to middle of text area minus half the width of variable `my-image'
21376 containing a loaded image:
21377 '(space :align-to (0.5 . (- text my-image)))
21379 Width of left margin minus width of 1 character in the default font:
21380 '(space :width (- left-margin 1))
21382 Width of left margin minus width of 2 characters in the current font:
21383 '(space :width (- left-margin (2 . width)))
21385 Center 1 character over left-margin (in header line):
21386 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21388 Different ways to express width of left fringe plus left margin minus one pixel:
21389 '(space :width (- (+ left-fringe left-margin) (1)))
21390 '(space :width (+ left-fringe left-margin (- (1))))
21391 '(space :width (+ left-fringe left-margin (-1)))
21395 #define NUMVAL(X) \
21396 ((INTEGERP (X) || FLOATP (X)) \
21397 ? XFLOATINT (X) \
21398 : - 1)
21401 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21402 struct font *font, int width_p, int *align_to)
21404 double pixels;
21406 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21407 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21409 if (NILP (prop))
21410 return OK_PIXELS (0);
21412 xassert (FRAME_LIVE_P (it->f));
21414 if (SYMBOLP (prop))
21416 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21418 char *unit = SSDATA (SYMBOL_NAME (prop));
21420 if (unit[0] == 'i' && unit[1] == 'n')
21421 pixels = 1.0;
21422 else if (unit[0] == 'm' && unit[1] == 'm')
21423 pixels = 25.4;
21424 else if (unit[0] == 'c' && unit[1] == 'm')
21425 pixels = 2.54;
21426 else
21427 pixels = 0;
21428 if (pixels > 0)
21430 double ppi;
21431 #ifdef HAVE_WINDOW_SYSTEM
21432 if (FRAME_WINDOW_P (it->f)
21433 && (ppi = (width_p
21434 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21435 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21436 ppi > 0))
21437 return OK_PIXELS (ppi / pixels);
21438 #endif
21440 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21441 || (CONSP (Vdisplay_pixels_per_inch)
21442 && (ppi = (width_p
21443 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21444 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21445 ppi > 0)))
21446 return OK_PIXELS (ppi / pixels);
21448 return 0;
21452 #ifdef HAVE_WINDOW_SYSTEM
21453 if (EQ (prop, Qheight))
21454 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21455 if (EQ (prop, Qwidth))
21456 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21457 #else
21458 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21459 return OK_PIXELS (1);
21460 #endif
21462 if (EQ (prop, Qtext))
21463 return OK_PIXELS (width_p
21464 ? window_box_width (it->w, TEXT_AREA)
21465 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21467 if (align_to && *align_to < 0)
21469 *res = 0;
21470 if (EQ (prop, Qleft))
21471 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21472 if (EQ (prop, Qright))
21473 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21474 if (EQ (prop, Qcenter))
21475 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21476 + window_box_width (it->w, TEXT_AREA) / 2);
21477 if (EQ (prop, Qleft_fringe))
21478 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21479 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21480 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21481 if (EQ (prop, Qright_fringe))
21482 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21483 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21484 : window_box_right_offset (it->w, TEXT_AREA));
21485 if (EQ (prop, Qleft_margin))
21486 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21487 if (EQ (prop, Qright_margin))
21488 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21489 if (EQ (prop, Qscroll_bar))
21490 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21492 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21493 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21494 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21495 : 0)));
21497 else
21499 if (EQ (prop, Qleft_fringe))
21500 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21501 if (EQ (prop, Qright_fringe))
21502 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21503 if (EQ (prop, Qleft_margin))
21504 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21505 if (EQ (prop, Qright_margin))
21506 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21507 if (EQ (prop, Qscroll_bar))
21508 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21511 prop = Fbuffer_local_value (prop, it->w->buffer);
21514 if (INTEGERP (prop) || FLOATP (prop))
21516 int base_unit = (width_p
21517 ? FRAME_COLUMN_WIDTH (it->f)
21518 : FRAME_LINE_HEIGHT (it->f));
21519 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21522 if (CONSP (prop))
21524 Lisp_Object car = XCAR (prop);
21525 Lisp_Object cdr = XCDR (prop);
21527 if (SYMBOLP (car))
21529 #ifdef HAVE_WINDOW_SYSTEM
21530 if (FRAME_WINDOW_P (it->f)
21531 && valid_image_p (prop))
21533 ptrdiff_t id = lookup_image (it->f, prop);
21534 struct image *img = IMAGE_FROM_ID (it->f, id);
21536 return OK_PIXELS (width_p ? img->width : img->height);
21538 #ifdef HAVE_XWIDGETS
21539 if (FRAME_WINDOW_P (it->f) && valid_xwidget_p (prop))
21541 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
21542 return OK_PIXELS (width_p ? 100 : 100);
21544 #endif
21545 #endif
21546 if (EQ (car, Qplus) || EQ (car, Qminus))
21548 int first = 1;
21549 double px;
21551 pixels = 0;
21552 while (CONSP (cdr))
21554 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21555 font, width_p, align_to))
21556 return 0;
21557 if (first)
21558 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21559 else
21560 pixels += px;
21561 cdr = XCDR (cdr);
21563 if (EQ (car, Qminus))
21564 pixels = -pixels;
21565 return OK_PIXELS (pixels);
21568 car = Fbuffer_local_value (car, it->w->buffer);
21571 if (INTEGERP (car) || FLOATP (car))
21573 double fact;
21574 pixels = XFLOATINT (car);
21575 if (NILP (cdr))
21576 return OK_PIXELS (pixels);
21577 if (calc_pixel_width_or_height (&fact, it, cdr,
21578 font, width_p, align_to))
21579 return OK_PIXELS (pixels * fact);
21580 return 0;
21583 return 0;
21586 return 0;
21590 /***********************************************************************
21591 Glyph Display
21592 ***********************************************************************/
21594 #ifdef HAVE_WINDOW_SYSTEM
21596 #if GLYPH_DEBUG
21598 void
21599 dump_glyph_string (struct glyph_string *s)
21601 fprintf (stderr, "glyph string\n");
21602 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21603 s->x, s->y, s->width, s->height);
21604 fprintf (stderr, " ybase = %d\n", s->ybase);
21605 fprintf (stderr, " hl = %d\n", s->hl);
21606 fprintf (stderr, " left overhang = %d, right = %d\n",
21607 s->left_overhang, s->right_overhang);
21608 fprintf (stderr, " nchars = %d\n", s->nchars);
21609 fprintf (stderr, " extends to end of line = %d\n",
21610 s->extends_to_end_of_line_p);
21611 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21612 fprintf (stderr, " bg width = %d\n", s->background_width);
21615 #endif /* GLYPH_DEBUG */
21617 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21618 of XChar2b structures for S; it can't be allocated in
21619 init_glyph_string because it must be allocated via `alloca'. W
21620 is the window on which S is drawn. ROW and AREA are the glyph row
21621 and area within the row from which S is constructed. START is the
21622 index of the first glyph structure covered by S. HL is a
21623 face-override for drawing S. */
21625 #ifdef HAVE_NTGUI
21626 #define OPTIONAL_HDC(hdc) HDC hdc,
21627 #define DECLARE_HDC(hdc) HDC hdc;
21628 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21629 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21630 #endif
21632 #ifndef OPTIONAL_HDC
21633 #define OPTIONAL_HDC(hdc)
21634 #define DECLARE_HDC(hdc)
21635 #define ALLOCATE_HDC(hdc, f)
21636 #define RELEASE_HDC(hdc, f)
21637 #endif
21639 static void
21640 init_glyph_string (struct glyph_string *s,
21641 OPTIONAL_HDC (hdc)
21642 XChar2b *char2b, struct window *w, struct glyph_row *row,
21643 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21645 memset (s, 0, sizeof *s);
21646 s->w = w;
21647 s->f = XFRAME (w->frame);
21648 #ifdef HAVE_NTGUI
21649 s->hdc = hdc;
21650 #endif
21651 s->display = FRAME_X_DISPLAY (s->f);
21652 s->window = FRAME_X_WINDOW (s->f);
21653 s->char2b = char2b;
21654 s->hl = hl;
21655 s->row = row;
21656 s->area = area;
21657 s->first_glyph = row->glyphs[area] + start;
21658 s->height = row->height;
21659 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21660 s->ybase = s->y + row->ascent;
21664 /* Append the list of glyph strings with head H and tail T to the list
21665 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21667 static inline void
21668 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21669 struct glyph_string *h, struct glyph_string *t)
21671 if (h)
21673 if (*head)
21674 (*tail)->next = h;
21675 else
21676 *head = h;
21677 h->prev = *tail;
21678 *tail = t;
21683 /* Prepend the list of glyph strings with head H and tail T to the
21684 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21685 result. */
21687 static inline void
21688 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21689 struct glyph_string *h, struct glyph_string *t)
21691 if (h)
21693 if (*head)
21694 (*head)->prev = t;
21695 else
21696 *tail = t;
21697 t->next = *head;
21698 *head = h;
21703 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21704 Set *HEAD and *TAIL to the resulting list. */
21706 static inline void
21707 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21708 struct glyph_string *s)
21710 s->next = s->prev = NULL;
21711 append_glyph_string_lists (head, tail, s, s);
21715 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21716 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21717 make sure that X resources for the face returned are allocated.
21718 Value is a pointer to a realized face that is ready for display if
21719 DISPLAY_P is non-zero. */
21721 static inline struct face *
21722 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21723 XChar2b *char2b, int display_p)
21725 struct face *face = FACE_FROM_ID (f, face_id);
21727 if (face->font)
21729 unsigned code = face->font->driver->encode_char (face->font, c);
21731 if (code != FONT_INVALID_CODE)
21732 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21733 else
21734 STORE_XCHAR2B (char2b, 0, 0);
21737 /* Make sure X resources of the face are allocated. */
21738 #ifdef HAVE_X_WINDOWS
21739 if (display_p)
21740 #endif
21742 xassert (face != NULL);
21743 PREPARE_FACE_FOR_DISPLAY (f, face);
21746 return face;
21750 /* Get face and two-byte form of character glyph GLYPH on frame F.
21751 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21752 a pointer to a realized face that is ready for display. */
21754 static inline struct face *
21755 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21756 XChar2b *char2b, int *two_byte_p)
21758 struct face *face;
21760 xassert (glyph->type == CHAR_GLYPH);
21761 face = FACE_FROM_ID (f, glyph->face_id);
21763 if (two_byte_p)
21764 *two_byte_p = 0;
21766 if (face->font)
21768 unsigned code;
21770 if (CHAR_BYTE8_P (glyph->u.ch))
21771 code = CHAR_TO_BYTE8 (glyph->u.ch);
21772 else
21773 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21775 if (code != FONT_INVALID_CODE)
21776 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21777 else
21778 STORE_XCHAR2B (char2b, 0, 0);
21781 /* Make sure X resources of the face are allocated. */
21782 xassert (face != NULL);
21783 PREPARE_FACE_FOR_DISPLAY (f, face);
21784 return face;
21788 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21789 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21791 static inline int
21792 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21794 unsigned code;
21796 if (CHAR_BYTE8_P (c))
21797 code = CHAR_TO_BYTE8 (c);
21798 else
21799 code = font->driver->encode_char (font, c);
21801 if (code == FONT_INVALID_CODE)
21802 return 0;
21803 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21804 return 1;
21808 /* Fill glyph string S with composition components specified by S->cmp.
21810 BASE_FACE is the base face of the composition.
21811 S->cmp_from is the index of the first component for S.
21813 OVERLAPS non-zero means S should draw the foreground only, and use
21814 its physical height for clipping. See also draw_glyphs.
21816 Value is the index of a component not in S. */
21818 static int
21819 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21820 int overlaps)
21822 int i;
21823 /* For all glyphs of this composition, starting at the offset
21824 S->cmp_from, until we reach the end of the definition or encounter a
21825 glyph that requires the different face, add it to S. */
21826 struct face *face;
21828 xassert (s);
21830 s->for_overlaps = overlaps;
21831 s->face = NULL;
21832 s->font = NULL;
21833 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21835 int c = COMPOSITION_GLYPH (s->cmp, i);
21837 if (c != '\t')
21839 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21840 -1, Qnil);
21842 face = get_char_face_and_encoding (s->f, c, face_id,
21843 s->char2b + i, 1);
21844 if (face)
21846 if (! s->face)
21848 s->face = face;
21849 s->font = s->face->font;
21851 else if (s->face != face)
21852 break;
21855 ++s->nchars;
21857 s->cmp_to = i;
21859 /* All glyph strings for the same composition has the same width,
21860 i.e. the width set for the first component of the composition. */
21861 s->width = s->first_glyph->pixel_width;
21863 /* If the specified font could not be loaded, use the frame's
21864 default font, but record the fact that we couldn't load it in
21865 the glyph string so that we can draw rectangles for the
21866 characters of the glyph string. */
21867 if (s->font == NULL)
21869 s->font_not_found_p = 1;
21870 s->font = FRAME_FONT (s->f);
21873 /* Adjust base line for subscript/superscript text. */
21874 s->ybase += s->first_glyph->voffset;
21876 /* This glyph string must always be drawn with 16-bit functions. */
21877 s->two_byte_p = 1;
21879 return s->cmp_to;
21882 static int
21883 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21884 int start, int end, int overlaps)
21886 struct glyph *glyph, *last;
21887 Lisp_Object lgstring;
21888 int i;
21890 s->for_overlaps = overlaps;
21891 glyph = s->row->glyphs[s->area] + start;
21892 last = s->row->glyphs[s->area] + end;
21893 s->cmp_id = glyph->u.cmp.id;
21894 s->cmp_from = glyph->slice.cmp.from;
21895 s->cmp_to = glyph->slice.cmp.to + 1;
21896 s->face = FACE_FROM_ID (s->f, face_id);
21897 lgstring = composition_gstring_from_id (s->cmp_id);
21898 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21899 glyph++;
21900 while (glyph < last
21901 && glyph->u.cmp.automatic
21902 && glyph->u.cmp.id == s->cmp_id
21903 && s->cmp_to == glyph->slice.cmp.from)
21904 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21906 for (i = s->cmp_from; i < s->cmp_to; i++)
21908 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21909 unsigned code = LGLYPH_CODE (lglyph);
21911 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21913 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21914 return glyph - s->row->glyphs[s->area];
21918 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21919 See the comment of fill_glyph_string for arguments.
21920 Value is the index of the first glyph not in S. */
21923 static int
21924 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21925 int start, int end, int overlaps)
21927 struct glyph *glyph, *last;
21928 int voffset;
21930 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21931 s->for_overlaps = overlaps;
21932 glyph = s->row->glyphs[s->area] + start;
21933 last = s->row->glyphs[s->area] + end;
21934 voffset = glyph->voffset;
21935 s->face = FACE_FROM_ID (s->f, face_id);
21936 s->font = s->face->font;
21937 s->nchars = 1;
21938 s->width = glyph->pixel_width;
21939 glyph++;
21940 while (glyph < last
21941 && glyph->type == GLYPHLESS_GLYPH
21942 && glyph->voffset == voffset
21943 && glyph->face_id == face_id)
21945 s->nchars++;
21946 s->width += glyph->pixel_width;
21947 glyph++;
21949 s->ybase += voffset;
21950 return glyph - s->row->glyphs[s->area];
21954 /* Fill glyph string S from a sequence of character glyphs.
21956 FACE_ID is the face id of the string. START is the index of the
21957 first glyph to consider, END is the index of the last + 1.
21958 OVERLAPS non-zero means S should draw the foreground only, and use
21959 its physical height for clipping. See also draw_glyphs.
21961 Value is the index of the first glyph not in S. */
21963 static int
21964 fill_glyph_string (struct glyph_string *s, int face_id,
21965 int start, int end, int overlaps)
21967 struct glyph *glyph, *last;
21968 int voffset;
21969 int glyph_not_available_p;
21971 xassert (s->f == XFRAME (s->w->frame));
21972 xassert (s->nchars == 0);
21973 xassert (start >= 0 && end > start);
21975 s->for_overlaps = overlaps;
21976 glyph = s->row->glyphs[s->area] + start;
21977 last = s->row->glyphs[s->area] + end;
21978 voffset = glyph->voffset;
21979 s->padding_p = glyph->padding_p;
21980 glyph_not_available_p = glyph->glyph_not_available_p;
21982 while (glyph < last
21983 && glyph->type == CHAR_GLYPH
21984 && glyph->voffset == voffset
21985 /* Same face id implies same font, nowadays. */
21986 && glyph->face_id == face_id
21987 && glyph->glyph_not_available_p == glyph_not_available_p)
21989 int two_byte_p;
21991 s->face = get_glyph_face_and_encoding (s->f, glyph,
21992 s->char2b + s->nchars,
21993 &two_byte_p);
21994 s->two_byte_p = two_byte_p;
21995 ++s->nchars;
21996 xassert (s->nchars <= end - start);
21997 s->width += glyph->pixel_width;
21998 if (glyph++->padding_p != s->padding_p)
21999 break;
22002 s->font = s->face->font;
22004 /* If the specified font could not be loaded, use the frame's font,
22005 but record the fact that we couldn't load it in
22006 S->font_not_found_p so that we can draw rectangles for the
22007 characters of the glyph string. */
22008 if (s->font == NULL || glyph_not_available_p)
22010 s->font_not_found_p = 1;
22011 s->font = FRAME_FONT (s->f);
22014 /* Adjust base line for subscript/superscript text. */
22015 s->ybase += voffset;
22017 xassert (s->face && s->face->gc);
22018 return glyph - s->row->glyphs[s->area];
22022 /* Fill glyph string S from image glyph S->first_glyph. */
22024 static void
22025 fill_image_glyph_string (struct glyph_string *s)
22027 xassert (s->first_glyph->type == IMAGE_GLYPH);
22028 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22029 xassert (s->img);
22030 s->slice = s->first_glyph->slice.img;
22031 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22032 s->font = s->face->font;
22033 s->width = s->first_glyph->pixel_width;
22035 /* Adjust base line for subscript/superscript text. */
22036 s->ybase += s->first_glyph->voffset;
22039 #ifdef HAVE_XWIDGETS
22040 static void
22041 fill_xwidget_glyph_string (struct glyph_string *s)
22043 xassert (s->first_glyph->type == XWIDGET_GLYPH);
22044 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
22045 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22046 s->font = s->face->font;
22047 s->width = s->first_glyph->pixel_width;
22048 s->ybase += s->first_glyph->voffset;
22049 s->xwidget = s->first_glyph->u.xwidget;
22050 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
22052 #endif
22053 /* Fill glyph string S from a sequence of stretch glyphs.
22055 START is the index of the first glyph to consider,
22056 END is the index of the last + 1.
22058 Value is the index of the first glyph not in S. */
22060 static int
22061 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22063 struct glyph *glyph, *last;
22064 int voffset, face_id;
22066 xassert (s->first_glyph->type == STRETCH_GLYPH);
22068 glyph = s->row->glyphs[s->area] + start;
22069 last = s->row->glyphs[s->area] + end;
22070 face_id = glyph->face_id;
22071 s->face = FACE_FROM_ID (s->f, face_id);
22072 s->font = s->face->font;
22073 s->width = glyph->pixel_width;
22074 s->nchars = 1;
22075 voffset = glyph->voffset;
22077 for (++glyph;
22078 (glyph < last
22079 && glyph->type == STRETCH_GLYPH
22080 && glyph->voffset == voffset
22081 && glyph->face_id == face_id);
22082 ++glyph)
22083 s->width += glyph->pixel_width;
22085 /* Adjust base line for subscript/superscript text. */
22086 s->ybase += voffset;
22088 /* The case that face->gc == 0 is handled when drawing the glyph
22089 string by calling PREPARE_FACE_FOR_DISPLAY. */
22090 xassert (s->face);
22091 return glyph - s->row->glyphs[s->area];
22094 static struct font_metrics *
22095 get_per_char_metric (struct font *font, XChar2b *char2b)
22097 static struct font_metrics metrics;
22098 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22100 if (! font || code == FONT_INVALID_CODE)
22101 return NULL;
22102 font->driver->text_extents (font, &code, 1, &metrics);
22103 return &metrics;
22106 /* EXPORT for RIF:
22107 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22108 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22109 assumed to be zero. */
22111 void
22112 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22114 *left = *right = 0;
22116 if (glyph->type == CHAR_GLYPH)
22118 struct face *face;
22119 XChar2b char2b;
22120 struct font_metrics *pcm;
22122 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22123 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22125 if (pcm->rbearing > pcm->width)
22126 *right = pcm->rbearing - pcm->width;
22127 if (pcm->lbearing < 0)
22128 *left = -pcm->lbearing;
22131 else if (glyph->type == COMPOSITE_GLYPH)
22133 if (! glyph->u.cmp.automatic)
22135 struct composition *cmp = composition_table[glyph->u.cmp.id];
22137 if (cmp->rbearing > cmp->pixel_width)
22138 *right = cmp->rbearing - cmp->pixel_width;
22139 if (cmp->lbearing < 0)
22140 *left = - cmp->lbearing;
22142 else
22144 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22145 struct font_metrics metrics;
22147 composition_gstring_width (gstring, glyph->slice.cmp.from,
22148 glyph->slice.cmp.to + 1, &metrics);
22149 if (metrics.rbearing > metrics.width)
22150 *right = metrics.rbearing - metrics.width;
22151 if (metrics.lbearing < 0)
22152 *left = - metrics.lbearing;
22158 /* Return the index of the first glyph preceding glyph string S that
22159 is overwritten by S because of S's left overhang. Value is -1
22160 if no glyphs are overwritten. */
22162 static int
22163 left_overwritten (struct glyph_string *s)
22165 int k;
22167 if (s->left_overhang)
22169 int x = 0, i;
22170 struct glyph *glyphs = s->row->glyphs[s->area];
22171 int first = s->first_glyph - glyphs;
22173 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22174 x -= glyphs[i].pixel_width;
22176 k = i + 1;
22178 else
22179 k = -1;
22181 return k;
22185 /* Return the index of the first glyph preceding glyph string S that
22186 is overwriting S because of its right overhang. Value is -1 if no
22187 glyph in front of S overwrites S. */
22189 static int
22190 left_overwriting (struct glyph_string *s)
22192 int i, k, x;
22193 struct glyph *glyphs = s->row->glyphs[s->area];
22194 int first = s->first_glyph - glyphs;
22196 k = -1;
22197 x = 0;
22198 for (i = first - 1; i >= 0; --i)
22200 int left, right;
22201 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22202 if (x + right > 0)
22203 k = i;
22204 x -= glyphs[i].pixel_width;
22207 return k;
22211 /* Return the index of the last glyph following glyph string S that is
22212 overwritten by S because of S's right overhang. Value is -1 if
22213 no such glyph is found. */
22215 static int
22216 right_overwritten (struct glyph_string *s)
22218 int k = -1;
22220 if (s->right_overhang)
22222 int x = 0, i;
22223 struct glyph *glyphs = s->row->glyphs[s->area];
22224 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22225 int end = s->row->used[s->area];
22227 for (i = first; i < end && s->right_overhang > x; ++i)
22228 x += glyphs[i].pixel_width;
22230 k = i;
22233 return k;
22237 /* Return the index of the last glyph following glyph string S that
22238 overwrites S because of its left overhang. Value is negative
22239 if no such glyph is found. */
22241 static int
22242 right_overwriting (struct glyph_string *s)
22244 int i, k, x;
22245 int end = s->row->used[s->area];
22246 struct glyph *glyphs = s->row->glyphs[s->area];
22247 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22249 k = -1;
22250 x = 0;
22251 for (i = first; i < end; ++i)
22253 int left, right;
22254 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22255 if (x - left < 0)
22256 k = i;
22257 x += glyphs[i].pixel_width;
22260 return k;
22264 /* Set background width of glyph string S. START is the index of the
22265 first glyph following S. LAST_X is the right-most x-position + 1
22266 in the drawing area. */
22268 static inline void
22269 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22271 /* If the face of this glyph string has to be drawn to the end of
22272 the drawing area, set S->extends_to_end_of_line_p. */
22274 if (start == s->row->used[s->area]
22275 && s->area == TEXT_AREA
22276 && ((s->row->fill_line_p
22277 && (s->hl == DRAW_NORMAL_TEXT
22278 || s->hl == DRAW_IMAGE_RAISED
22279 || s->hl == DRAW_IMAGE_SUNKEN))
22280 || s->hl == DRAW_MOUSE_FACE))
22281 s->extends_to_end_of_line_p = 1;
22283 /* If S extends its face to the end of the line, set its
22284 background_width to the distance to the right edge of the drawing
22285 area. */
22286 if (s->extends_to_end_of_line_p)
22287 s->background_width = last_x - s->x + 1;
22288 else
22289 s->background_width = s->width;
22293 /* Compute overhangs and x-positions for glyph string S and its
22294 predecessors, or successors. X is the starting x-position for S.
22295 BACKWARD_P non-zero means process predecessors. */
22297 static void
22298 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22300 if (backward_p)
22302 while (s)
22304 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22305 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22306 x -= s->width;
22307 s->x = x;
22308 s = s->prev;
22311 else
22313 while (s)
22315 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22316 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22317 s->x = x;
22318 x += s->width;
22319 s = s->next;
22326 /* The following macros are only called from draw_glyphs below.
22327 They reference the following parameters of that function directly:
22328 `w', `row', `area', and `overlap_p'
22329 as well as the following local variables:
22330 `s', `f', and `hdc' (in W32) */
22332 #ifdef HAVE_NTGUI
22333 /* On W32, silently add local `hdc' variable to argument list of
22334 init_glyph_string. */
22335 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22336 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22337 #else
22338 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22339 init_glyph_string (s, char2b, w, row, area, start, hl)
22340 #endif
22342 /* Add a glyph string for a stretch glyph to the list of strings
22343 between HEAD and TAIL. START is the index of the stretch glyph in
22344 row area AREA of glyph row ROW. END is the index of the last glyph
22345 in that glyph row area. X is the current output position assigned
22346 to the new glyph string constructed. HL overrides that face of the
22347 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22348 is the right-most x-position of the drawing area. */
22350 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22351 and below -- keep them on one line. */
22352 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22353 do \
22355 s = (struct glyph_string *) alloca (sizeof *s); \
22356 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22357 START = fill_stretch_glyph_string (s, START, END); \
22358 append_glyph_string (&HEAD, &TAIL, s); \
22359 s->x = (X); \
22361 while (0)
22364 /* Add a glyph string for an image glyph to the list of strings
22365 between HEAD and TAIL. START is the index of the image glyph in
22366 row area AREA of glyph row ROW. END is the index of the last glyph
22367 in that glyph row area. X is the current output position assigned
22368 to the new glyph string constructed. HL overrides that face of the
22369 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22370 is the right-most x-position of the drawing area. */
22372 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22373 do \
22375 s = (struct glyph_string *) alloca (sizeof *s); \
22376 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22377 fill_image_glyph_string (s); \
22378 append_glyph_string (&HEAD, &TAIL, s); \
22379 ++START; \
22380 s->x = (X); \
22382 while (0)
22384 #ifdef HAVE_XWIDGETS
22385 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22386 do \
22388 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
22389 s = (struct glyph_string *) alloca (sizeof *s); \
22390 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22391 fill_xwidget_glyph_string (s); \
22392 append_glyph_string (&HEAD, &TAIL, s); \
22393 ++START; \
22394 s->x = (X); \
22396 while (0)
22397 #endif
22400 /* Add a glyph string for a sequence of character glyphs to the list
22401 of strings between HEAD and TAIL. START is the index of the first
22402 glyph in row area AREA of glyph row ROW that is part of the new
22403 glyph string. END is the index of the last glyph in that glyph row
22404 area. X is the current output position assigned to the new glyph
22405 string constructed. HL overrides that face of the glyph; e.g. it
22406 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22407 right-most x-position of the drawing area. */
22409 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22410 do \
22412 int face_id; \
22413 XChar2b *char2b; \
22415 face_id = (row)->glyphs[area][START].face_id; \
22417 s = (struct glyph_string *) alloca (sizeof *s); \
22418 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22419 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22420 append_glyph_string (&HEAD, &TAIL, s); \
22421 s->x = (X); \
22422 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22424 while (0)
22427 /* Add a glyph string for a composite sequence to the list of strings
22428 between HEAD and TAIL. START is the index of the first glyph in
22429 row area AREA of glyph row ROW that is part of the new glyph
22430 string. END is the index of the last glyph in that glyph row area.
22431 X is the current output position assigned to the new glyph string
22432 constructed. HL overrides that face of the glyph; e.g. it is
22433 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22434 x-position of the drawing area. */
22436 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22437 do { \
22438 int face_id = (row)->glyphs[area][START].face_id; \
22439 struct face *base_face = FACE_FROM_ID (f, face_id); \
22440 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22441 struct composition *cmp = composition_table[cmp_id]; \
22442 XChar2b *char2b; \
22443 struct glyph_string *first_s IF_LINT (= NULL); \
22444 int n; \
22446 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22448 /* Make glyph_strings for each glyph sequence that is drawable by \
22449 the same face, and append them to HEAD/TAIL. */ \
22450 for (n = 0; n < cmp->glyph_len;) \
22452 s = (struct glyph_string *) alloca (sizeof *s); \
22453 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22454 append_glyph_string (&(HEAD), &(TAIL), s); \
22455 s->cmp = cmp; \
22456 s->cmp_from = n; \
22457 s->x = (X); \
22458 if (n == 0) \
22459 first_s = s; \
22460 n = fill_composite_glyph_string (s, base_face, overlaps); \
22463 ++START; \
22464 s = first_s; \
22465 } while (0)
22468 /* Add a glyph string for a glyph-string sequence to the list of strings
22469 between HEAD and TAIL. */
22471 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22472 do { \
22473 int face_id; \
22474 XChar2b *char2b; \
22475 Lisp_Object gstring; \
22477 face_id = (row)->glyphs[area][START].face_id; \
22478 gstring = (composition_gstring_from_id \
22479 ((row)->glyphs[area][START].u.cmp.id)); \
22480 s = (struct glyph_string *) alloca (sizeof *s); \
22481 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22482 * LGSTRING_GLYPH_LEN (gstring)); \
22483 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22484 append_glyph_string (&(HEAD), &(TAIL), s); \
22485 s->x = (X); \
22486 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22487 } while (0)
22490 /* Add a glyph string for a sequence of glyphless character's glyphs
22491 to the list of strings between HEAD and TAIL. The meanings of
22492 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22494 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22495 do \
22497 int face_id; \
22499 face_id = (row)->glyphs[area][START].face_id; \
22501 s = (struct glyph_string *) alloca (sizeof *s); \
22502 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22503 append_glyph_string (&HEAD, &TAIL, s); \
22504 s->x = (X); \
22505 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22506 overlaps); \
22508 while (0)
22511 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22512 of AREA of glyph row ROW on window W between indices START and END.
22513 HL overrides the face for drawing glyph strings, e.g. it is
22514 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22515 x-positions of the drawing area.
22517 This is an ugly monster macro construct because we must use alloca
22518 to allocate glyph strings (because draw_glyphs can be called
22519 asynchronously). */
22521 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22522 do \
22524 HEAD = TAIL = NULL; \
22525 while (START < END) \
22527 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22528 switch (first_glyph->type) \
22530 case CHAR_GLYPH: \
22531 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22532 HL, X, LAST_X); \
22533 break; \
22535 case COMPOSITE_GLYPH: \
22536 if (first_glyph->u.cmp.automatic) \
22537 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22538 HL, X, LAST_X); \
22539 else \
22540 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22541 HL, X, LAST_X); \
22542 break; \
22544 case STRETCH_GLYPH: \
22545 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22546 HL, X, LAST_X); \
22547 break; \
22549 case IMAGE_GLYPH: \
22550 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22551 HL, X, LAST_X); \
22552 break; \
22553 case XWIDGET_GLYPH: \
22554 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
22555 HL, X, LAST_X); \
22556 break; \
22559 case GLYPHLESS_GLYPH: \
22560 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22561 HL, X, LAST_X); \
22562 break; \
22564 default: \
22565 abort (); \
22568 if (s) \
22570 set_glyph_string_background_width (s, START, LAST_X); \
22571 (X) += s->width; \
22574 } while (0)
22577 /* Draw glyphs between START and END in AREA of ROW on window W,
22578 starting at x-position X. X is relative to AREA in W. HL is a
22579 face-override with the following meaning:
22581 DRAW_NORMAL_TEXT draw normally
22582 DRAW_CURSOR draw in cursor face
22583 DRAW_MOUSE_FACE draw in mouse face.
22584 DRAW_INVERSE_VIDEO draw in mode line face
22585 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22586 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22588 If OVERLAPS is non-zero, draw only the foreground of characters and
22589 clip to the physical height of ROW. Non-zero value also defines
22590 the overlapping part to be drawn:
22592 OVERLAPS_PRED overlap with preceding rows
22593 OVERLAPS_SUCC overlap with succeeding rows
22594 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22595 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22597 Value is the x-position reached, relative to AREA of W. */
22599 static int
22600 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22601 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22602 enum draw_glyphs_face hl, int overlaps)
22604 struct glyph_string *head, *tail;
22605 struct glyph_string *s;
22606 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22607 int i, j, x_reached, last_x, area_left = 0;
22608 struct frame *f = XFRAME (WINDOW_FRAME (w));
22609 DECLARE_HDC (hdc);
22611 ALLOCATE_HDC (hdc, f);
22613 /* Let's rather be paranoid than getting a SEGV. */
22614 end = min (end, row->used[area]);
22615 start = max (0, start);
22616 start = min (end, start);
22618 /* Translate X to frame coordinates. Set last_x to the right
22619 end of the drawing area. */
22620 if (row->full_width_p)
22622 /* X is relative to the left edge of W, without scroll bars
22623 or fringes. */
22624 area_left = WINDOW_LEFT_EDGE_X (w);
22625 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22627 else
22629 area_left = window_box_left (w, area);
22630 last_x = area_left + window_box_width (w, area);
22632 x += area_left;
22634 /* Build a doubly-linked list of glyph_string structures between
22635 head and tail from what we have to draw. Note that the macro
22636 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22637 the reason we use a separate variable `i'. */
22638 i = start;
22639 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22640 if (tail)
22641 x_reached = tail->x + tail->background_width;
22642 else
22643 x_reached = x;
22645 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22646 the row, redraw some glyphs in front or following the glyph
22647 strings built above. */
22648 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22650 struct glyph_string *h, *t;
22651 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22652 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22653 int check_mouse_face = 0;
22654 int dummy_x = 0;
22656 /* If mouse highlighting is on, we may need to draw adjacent
22657 glyphs using mouse-face highlighting. */
22658 if (area == TEXT_AREA && row->mouse_face_p)
22660 struct glyph_row *mouse_beg_row, *mouse_end_row;
22662 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22663 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22665 if (row >= mouse_beg_row && row <= mouse_end_row)
22667 check_mouse_face = 1;
22668 mouse_beg_col = (row == mouse_beg_row)
22669 ? hlinfo->mouse_face_beg_col : 0;
22670 mouse_end_col = (row == mouse_end_row)
22671 ? hlinfo->mouse_face_end_col
22672 : row->used[TEXT_AREA];
22676 /* Compute overhangs for all glyph strings. */
22677 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22678 for (s = head; s; s = s->next)
22679 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22681 /* Prepend glyph strings for glyphs in front of the first glyph
22682 string that are overwritten because of the first glyph
22683 string's left overhang. The background of all strings
22684 prepended must be drawn because the first glyph string
22685 draws over it. */
22686 i = left_overwritten (head);
22687 if (i >= 0)
22689 enum draw_glyphs_face overlap_hl;
22691 /* If this row contains mouse highlighting, attempt to draw
22692 the overlapped glyphs with the correct highlight. This
22693 code fails if the overlap encompasses more than one glyph
22694 and mouse-highlight spans only some of these glyphs.
22695 However, making it work perfectly involves a lot more
22696 code, and I don't know if the pathological case occurs in
22697 practice, so we'll stick to this for now. --- cyd */
22698 if (check_mouse_face
22699 && mouse_beg_col < start && mouse_end_col > i)
22700 overlap_hl = DRAW_MOUSE_FACE;
22701 else
22702 overlap_hl = DRAW_NORMAL_TEXT;
22704 j = i;
22705 BUILD_GLYPH_STRINGS (j, start, h, t,
22706 overlap_hl, dummy_x, last_x);
22707 start = i;
22708 compute_overhangs_and_x (t, head->x, 1);
22709 prepend_glyph_string_lists (&head, &tail, h, t);
22710 clip_head = head;
22713 /* Prepend glyph strings for glyphs in front of the first glyph
22714 string that overwrite that glyph string because of their
22715 right overhang. For these strings, only the foreground must
22716 be drawn, because it draws over the glyph string at `head'.
22717 The background must not be drawn because this would overwrite
22718 right overhangs of preceding glyphs for which no glyph
22719 strings exist. */
22720 i = left_overwriting (head);
22721 if (i >= 0)
22723 enum draw_glyphs_face overlap_hl;
22725 if (check_mouse_face
22726 && mouse_beg_col < start && mouse_end_col > i)
22727 overlap_hl = DRAW_MOUSE_FACE;
22728 else
22729 overlap_hl = DRAW_NORMAL_TEXT;
22731 clip_head = head;
22732 BUILD_GLYPH_STRINGS (i, start, h, t,
22733 overlap_hl, dummy_x, last_x);
22734 for (s = h; s; s = s->next)
22735 s->background_filled_p = 1;
22736 compute_overhangs_and_x (t, head->x, 1);
22737 prepend_glyph_string_lists (&head, &tail, h, t);
22740 /* Append glyphs strings for glyphs following the last glyph
22741 string tail that are overwritten by tail. The background of
22742 these strings has to be drawn because tail's foreground draws
22743 over it. */
22744 i = right_overwritten (tail);
22745 if (i >= 0)
22747 enum draw_glyphs_face overlap_hl;
22749 if (check_mouse_face
22750 && mouse_beg_col < i && mouse_end_col > end)
22751 overlap_hl = DRAW_MOUSE_FACE;
22752 else
22753 overlap_hl = DRAW_NORMAL_TEXT;
22755 BUILD_GLYPH_STRINGS (end, i, h, t,
22756 overlap_hl, x, last_x);
22757 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22758 we don't have `end = i;' here. */
22759 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22760 append_glyph_string_lists (&head, &tail, h, t);
22761 clip_tail = tail;
22764 /* Append glyph strings for glyphs following the last glyph
22765 string tail that overwrite tail. The foreground of such
22766 glyphs has to be drawn because it writes into the background
22767 of tail. The background must not be drawn because it could
22768 paint over the foreground of following glyphs. */
22769 i = right_overwriting (tail);
22770 if (i >= 0)
22772 enum draw_glyphs_face overlap_hl;
22773 if (check_mouse_face
22774 && mouse_beg_col < i && mouse_end_col > end)
22775 overlap_hl = DRAW_MOUSE_FACE;
22776 else
22777 overlap_hl = DRAW_NORMAL_TEXT;
22779 clip_tail = tail;
22780 i++; /* We must include the Ith glyph. */
22781 BUILD_GLYPH_STRINGS (end, i, h, t,
22782 overlap_hl, x, last_x);
22783 for (s = h; s; s = s->next)
22784 s->background_filled_p = 1;
22785 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22786 append_glyph_string_lists (&head, &tail, h, t);
22788 if (clip_head || clip_tail)
22789 for (s = head; s; s = s->next)
22791 s->clip_head = clip_head;
22792 s->clip_tail = clip_tail;
22796 /* Draw all strings. */
22797 for (s = head; s; s = s->next)
22798 FRAME_RIF (f)->draw_glyph_string (s);
22800 #ifndef HAVE_NS
22801 /* When focus a sole frame and move horizontally, this sets on_p to 0
22802 causing a failure to erase prev cursor position. */
22803 if (area == TEXT_AREA
22804 && !row->full_width_p
22805 /* When drawing overlapping rows, only the glyph strings'
22806 foreground is drawn, which doesn't erase a cursor
22807 completely. */
22808 && !overlaps)
22810 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22811 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22812 : (tail ? tail->x + tail->background_width : x));
22813 x0 -= area_left;
22814 x1 -= area_left;
22816 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22817 row->y, MATRIX_ROW_BOTTOM_Y (row));
22819 #endif
22821 /* Value is the x-position up to which drawn, relative to AREA of W.
22822 This doesn't include parts drawn because of overhangs. */
22823 if (row->full_width_p)
22824 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22825 else
22826 x_reached -= area_left;
22828 RELEASE_HDC (hdc, f);
22830 return x_reached;
22833 /* Expand row matrix if too narrow. Don't expand if area
22834 is not present. */
22836 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22838 if (!fonts_changed_p \
22839 && (it->glyph_row->glyphs[area] \
22840 < it->glyph_row->glyphs[area + 1])) \
22842 it->w->ncols_scale_factor++; \
22843 fonts_changed_p = 1; \
22847 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22848 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22850 static inline void
22851 append_glyph (struct it *it)
22853 struct glyph *glyph;
22854 enum glyph_row_area area = it->area;
22856 xassert (it->glyph_row);
22857 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22859 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22860 if (glyph < it->glyph_row->glyphs[area + 1])
22862 /* If the glyph row is reversed, we need to prepend the glyph
22863 rather than append it. */
22864 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22866 struct glyph *g;
22868 /* Make room for the additional glyph. */
22869 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22870 g[1] = *g;
22871 glyph = it->glyph_row->glyphs[area];
22873 glyph->charpos = CHARPOS (it->position);
22874 glyph->object = it->object;
22875 if (it->pixel_width > 0)
22877 glyph->pixel_width = it->pixel_width;
22878 glyph->padding_p = 0;
22880 else
22882 /* Assure at least 1-pixel width. Otherwise, cursor can't
22883 be displayed correctly. */
22884 glyph->pixel_width = 1;
22885 glyph->padding_p = 1;
22887 glyph->ascent = it->ascent;
22888 glyph->descent = it->descent;
22889 glyph->voffset = it->voffset;
22890 glyph->type = CHAR_GLYPH;
22891 glyph->avoid_cursor_p = it->avoid_cursor_p;
22892 glyph->multibyte_p = it->multibyte_p;
22893 glyph->left_box_line_p = it->start_of_box_run_p;
22894 glyph->right_box_line_p = it->end_of_box_run_p;
22895 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22896 || it->phys_descent > it->descent);
22897 glyph->glyph_not_available_p = it->glyph_not_available_p;
22898 glyph->face_id = it->face_id;
22899 glyph->u.ch = it->char_to_display;
22900 glyph->slice.img = null_glyph_slice;
22901 glyph->font_type = FONT_TYPE_UNKNOWN;
22902 if (it->bidi_p)
22904 glyph->resolved_level = it->bidi_it.resolved_level;
22905 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22906 abort ();
22907 glyph->bidi_type = it->bidi_it.type;
22909 else
22911 glyph->resolved_level = 0;
22912 glyph->bidi_type = UNKNOWN_BT;
22914 ++it->glyph_row->used[area];
22916 else
22917 IT_EXPAND_MATRIX_WIDTH (it, area);
22920 /* Store one glyph for the composition IT->cmp_it.id in
22921 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22922 non-null. */
22924 static inline void
22925 append_composite_glyph (struct it *it)
22927 struct glyph *glyph;
22928 enum glyph_row_area area = it->area;
22930 xassert (it->glyph_row);
22932 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22933 if (glyph < it->glyph_row->glyphs[area + 1])
22935 /* If the glyph row is reversed, we need to prepend the glyph
22936 rather than append it. */
22937 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22939 struct glyph *g;
22941 /* Make room for the new glyph. */
22942 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22943 g[1] = *g;
22944 glyph = it->glyph_row->glyphs[it->area];
22946 glyph->charpos = it->cmp_it.charpos;
22947 glyph->object = it->object;
22948 glyph->pixel_width = it->pixel_width;
22949 glyph->ascent = it->ascent;
22950 glyph->descent = it->descent;
22951 glyph->voffset = it->voffset;
22952 glyph->type = COMPOSITE_GLYPH;
22953 if (it->cmp_it.ch < 0)
22955 glyph->u.cmp.automatic = 0;
22956 glyph->u.cmp.id = it->cmp_it.id;
22957 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22959 else
22961 glyph->u.cmp.automatic = 1;
22962 glyph->u.cmp.id = it->cmp_it.id;
22963 glyph->slice.cmp.from = it->cmp_it.from;
22964 glyph->slice.cmp.to = it->cmp_it.to - 1;
22966 glyph->avoid_cursor_p = it->avoid_cursor_p;
22967 glyph->multibyte_p = it->multibyte_p;
22968 glyph->left_box_line_p = it->start_of_box_run_p;
22969 glyph->right_box_line_p = it->end_of_box_run_p;
22970 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22971 || it->phys_descent > it->descent);
22972 glyph->padding_p = 0;
22973 glyph->glyph_not_available_p = 0;
22974 glyph->face_id = it->face_id;
22975 glyph->font_type = FONT_TYPE_UNKNOWN;
22976 if (it->bidi_p)
22978 glyph->resolved_level = it->bidi_it.resolved_level;
22979 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22980 abort ();
22981 glyph->bidi_type = it->bidi_it.type;
22983 ++it->glyph_row->used[area];
22985 else
22986 IT_EXPAND_MATRIX_WIDTH (it, area);
22990 /* Change IT->ascent and IT->height according to the setting of
22991 IT->voffset. */
22993 static inline void
22994 take_vertical_position_into_account (struct it *it)
22996 if (it->voffset)
22998 if (it->voffset < 0)
22999 /* Increase the ascent so that we can display the text higher
23000 in the line. */
23001 it->ascent -= it->voffset;
23002 else
23003 /* Increase the descent so that we can display the text lower
23004 in the line. */
23005 it->descent += it->voffset;
23010 /* Produce glyphs/get display metrics for the image IT is loaded with.
23011 See the description of struct display_iterator in dispextern.h for
23012 an overview of struct display_iterator. */
23014 static void
23015 produce_image_glyph (struct it *it)
23017 struct image *img;
23018 struct face *face;
23019 int glyph_ascent, crop;
23020 struct glyph_slice slice;
23022 xassert (it->what == IT_IMAGE);
23024 face = FACE_FROM_ID (it->f, it->face_id);
23025 xassert (face);
23026 /* Make sure X resources of the face is loaded. */
23027 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23029 if (it->image_id < 0)
23031 /* Fringe bitmap. */
23032 it->ascent = it->phys_ascent = 0;
23033 it->descent = it->phys_descent = 0;
23034 it->pixel_width = 0;
23035 it->nglyphs = 0;
23036 return;
23039 img = IMAGE_FROM_ID (it->f, it->image_id);
23040 xassert (img);
23041 /* Make sure X resources of the image is loaded. */
23042 prepare_image_for_display (it->f, img);
23044 slice.x = slice.y = 0;
23045 slice.width = img->width;
23046 slice.height = img->height;
23048 if (INTEGERP (it->slice.x))
23049 slice.x = XINT (it->slice.x);
23050 else if (FLOATP (it->slice.x))
23051 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23053 if (INTEGERP (it->slice.y))
23054 slice.y = XINT (it->slice.y);
23055 else if (FLOATP (it->slice.y))
23056 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23058 if (INTEGERP (it->slice.width))
23059 slice.width = XINT (it->slice.width);
23060 else if (FLOATP (it->slice.width))
23061 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23063 if (INTEGERP (it->slice.height))
23064 slice.height = XINT (it->slice.height);
23065 else if (FLOATP (it->slice.height))
23066 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23068 if (slice.x >= img->width)
23069 slice.x = img->width;
23070 if (slice.y >= img->height)
23071 slice.y = img->height;
23072 if (slice.x + slice.width >= img->width)
23073 slice.width = img->width - slice.x;
23074 if (slice.y + slice.height > img->height)
23075 slice.height = img->height - slice.y;
23077 if (slice.width == 0 || slice.height == 0)
23078 return;
23080 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23082 it->descent = slice.height - glyph_ascent;
23083 if (slice.y == 0)
23084 it->descent += img->vmargin;
23085 if (slice.y + slice.height == img->height)
23086 it->descent += img->vmargin;
23087 it->phys_descent = it->descent;
23089 it->pixel_width = slice.width;
23090 if (slice.x == 0)
23091 it->pixel_width += img->hmargin;
23092 if (slice.x + slice.width == img->width)
23093 it->pixel_width += img->hmargin;
23095 /* It's quite possible for images to have an ascent greater than
23096 their height, so don't get confused in that case. */
23097 if (it->descent < 0)
23098 it->descent = 0;
23100 it->nglyphs = 1;
23102 if (face->box != FACE_NO_BOX)
23104 if (face->box_line_width > 0)
23106 if (slice.y == 0)
23107 it->ascent += face->box_line_width;
23108 if (slice.y + slice.height == img->height)
23109 it->descent += face->box_line_width;
23112 if (it->start_of_box_run_p && slice.x == 0)
23113 it->pixel_width += eabs (face->box_line_width);
23114 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23115 it->pixel_width += eabs (face->box_line_width);
23118 take_vertical_position_into_account (it);
23120 /* Automatically crop wide image glyphs at right edge so we can
23121 draw the cursor on same display row. */
23122 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23123 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23125 it->pixel_width -= crop;
23126 slice.width -= crop;
23129 if (it->glyph_row)
23131 struct glyph *glyph;
23132 enum glyph_row_area area = it->area;
23134 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23135 if (glyph < it->glyph_row->glyphs[area + 1])
23137 glyph->charpos = CHARPOS (it->position);
23138 glyph->object = it->object;
23139 glyph->pixel_width = it->pixel_width;
23140 glyph->ascent = glyph_ascent;
23141 glyph->descent = it->descent;
23142 glyph->voffset = it->voffset;
23143 glyph->type = IMAGE_GLYPH;
23144 glyph->avoid_cursor_p = it->avoid_cursor_p;
23145 glyph->multibyte_p = it->multibyte_p;
23146 glyph->left_box_line_p = it->start_of_box_run_p;
23147 glyph->right_box_line_p = it->end_of_box_run_p;
23148 glyph->overlaps_vertically_p = 0;
23149 glyph->padding_p = 0;
23150 glyph->glyph_not_available_p = 0;
23151 glyph->face_id = it->face_id;
23152 glyph->u.img_id = img->id;
23153 glyph->slice.img = slice;
23154 glyph->font_type = FONT_TYPE_UNKNOWN;
23155 if (it->bidi_p)
23157 glyph->resolved_level = it->bidi_it.resolved_level;
23158 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23159 abort ();
23160 glyph->bidi_type = it->bidi_it.type;
23162 ++it->glyph_row->used[area];
23164 else
23165 IT_EXPAND_MATRIX_WIDTH (it, area);
23169 #ifdef HAVE_XWIDGETS
23170 static void
23171 produce_xwidget_glyph (struct it *it)
23173 struct xwidget* xw;
23174 struct face *face;
23175 int glyph_ascent, crop;
23176 printf("produce_xwidget_glyph:\n");
23177 xassert (it->what == IT_XWIDGET);
23179 face = FACE_FROM_ID (it->f, it->face_id);
23180 xassert (face);
23181 /* Make sure X resources of the face is loaded. */
23182 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23184 xw = it->xwidget;
23185 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
23186 it->descent = xw->height/2;
23187 it->phys_descent = it->descent;
23188 it->pixel_width = xw->width;
23189 /* It's quite possible for images to have an ascent greater than
23190 their height, so don't get confused in that case. */
23191 if (it->descent < 0)
23192 it->descent = 0;
23194 it->nglyphs = 1;
23196 if (face->box != FACE_NO_BOX)
23198 if (face->box_line_width > 0)
23200 it->ascent += face->box_line_width;
23201 it->descent += face->box_line_width;
23204 if (it->start_of_box_run_p)
23205 it->pixel_width += eabs (face->box_line_width);
23206 it->pixel_width += eabs (face->box_line_width);
23209 take_vertical_position_into_account (it);
23211 /* Automatically crop wide image glyphs at right edge so we can
23212 draw the cursor on same display row. */
23213 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23214 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23216 it->pixel_width -= crop;
23219 if (it->glyph_row)
23221 struct glyph *glyph;
23222 enum glyph_row_area area = it->area;
23224 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23225 if (glyph < it->glyph_row->glyphs[area + 1])
23227 glyph->charpos = CHARPOS (it->position);
23228 glyph->object = it->object;
23229 glyph->pixel_width = it->pixel_width;
23230 glyph->ascent = glyph_ascent;
23231 glyph->descent = it->descent;
23232 glyph->voffset = it->voffset;
23233 glyph->type = XWIDGET_GLYPH;
23235 glyph->multibyte_p = it->multibyte_p;
23236 glyph->left_box_line_p = it->start_of_box_run_p;
23237 glyph->right_box_line_p = it->end_of_box_run_p;
23238 glyph->overlaps_vertically_p = 0;
23239 glyph->padding_p = 0;
23240 glyph->glyph_not_available_p = 0;
23241 glyph->face_id = it->face_id;
23242 glyph->u.xwidget = it->xwidget;
23243 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
23244 glyph->font_type = FONT_TYPE_UNKNOWN;
23245 ++it->glyph_row->used[area];
23247 else
23248 IT_EXPAND_MATRIX_WIDTH (it, area);
23251 #endif
23253 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23254 of the glyph, WIDTH and HEIGHT are the width and height of the
23255 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23257 static void
23258 append_stretch_glyph (struct it *it, Lisp_Object object,
23259 int width, int height, int ascent)
23261 struct glyph *glyph;
23262 enum glyph_row_area area = it->area;
23264 xassert (ascent >= 0 && ascent <= height);
23266 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23267 if (glyph < it->glyph_row->glyphs[area + 1])
23269 /* If the glyph row is reversed, we need to prepend the glyph
23270 rather than append it. */
23271 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23273 struct glyph *g;
23275 /* Make room for the additional glyph. */
23276 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23277 g[1] = *g;
23278 glyph = it->glyph_row->glyphs[area];
23280 glyph->charpos = CHARPOS (it->position);
23281 glyph->object = object;
23282 glyph->pixel_width = width;
23283 glyph->ascent = ascent;
23284 glyph->descent = height - ascent;
23285 glyph->voffset = it->voffset;
23286 glyph->type = STRETCH_GLYPH;
23287 glyph->avoid_cursor_p = it->avoid_cursor_p;
23288 glyph->multibyte_p = it->multibyte_p;
23289 glyph->left_box_line_p = it->start_of_box_run_p;
23290 glyph->right_box_line_p = it->end_of_box_run_p;
23291 glyph->overlaps_vertically_p = 0;
23292 glyph->padding_p = 0;
23293 glyph->glyph_not_available_p = 0;
23294 glyph->face_id = it->face_id;
23295 glyph->u.stretch.ascent = ascent;
23296 glyph->u.stretch.height = height;
23297 glyph->slice.img = null_glyph_slice;
23298 glyph->font_type = FONT_TYPE_UNKNOWN;
23299 if (it->bidi_p)
23301 glyph->resolved_level = it->bidi_it.resolved_level;
23302 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23303 abort ();
23304 glyph->bidi_type = it->bidi_it.type;
23306 else
23308 glyph->resolved_level = 0;
23309 glyph->bidi_type = UNKNOWN_BT;
23311 ++it->glyph_row->used[area];
23313 else
23314 IT_EXPAND_MATRIX_WIDTH (it, area);
23317 #endif /* HAVE_WINDOW_SYSTEM */
23319 /* Produce a stretch glyph for iterator IT. IT->object is the value
23320 of the glyph property displayed. The value must be a list
23321 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23322 being recognized:
23324 1. `:width WIDTH' specifies that the space should be WIDTH *
23325 canonical char width wide. WIDTH may be an integer or floating
23326 point number.
23328 2. `:relative-width FACTOR' specifies that the width of the stretch
23329 should be computed from the width of the first character having the
23330 `glyph' property, and should be FACTOR times that width.
23332 3. `:align-to HPOS' specifies that the space should be wide enough
23333 to reach HPOS, a value in canonical character units.
23335 Exactly one of the above pairs must be present.
23337 4. `:height HEIGHT' specifies that the height of the stretch produced
23338 should be HEIGHT, measured in canonical character units.
23340 5. `:relative-height FACTOR' specifies that the height of the
23341 stretch should be FACTOR times the height of the characters having
23342 the glyph property.
23344 Either none or exactly one of 4 or 5 must be present.
23346 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23347 of the stretch should be used for the ascent of the stretch.
23348 ASCENT must be in the range 0 <= ASCENT <= 100. */
23350 void
23351 produce_stretch_glyph (struct it *it)
23353 /* (space :width WIDTH :height HEIGHT ...) */
23354 Lisp_Object prop, plist;
23355 int width = 0, height = 0, align_to = -1;
23356 int zero_width_ok_p = 0;
23357 int ascent = 0;
23358 double tem;
23359 struct face *face = NULL;
23360 struct font *font = NULL;
23362 #ifdef HAVE_WINDOW_SYSTEM
23363 int zero_height_ok_p = 0;
23365 if (FRAME_WINDOW_P (it->f))
23367 face = FACE_FROM_ID (it->f, it->face_id);
23368 font = face->font ? face->font : FRAME_FONT (it->f);
23369 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23371 #endif
23373 /* List should start with `space'. */
23374 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23375 plist = XCDR (it->object);
23377 /* Compute the width of the stretch. */
23378 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23379 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23381 /* Absolute width `:width WIDTH' specified and valid. */
23382 zero_width_ok_p = 1;
23383 width = (int)tem;
23385 #ifdef HAVE_WINDOW_SYSTEM
23386 else if (FRAME_WINDOW_P (it->f)
23387 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23389 /* Relative width `:relative-width FACTOR' specified and valid.
23390 Compute the width of the characters having the `glyph'
23391 property. */
23392 struct it it2;
23393 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23395 it2 = *it;
23396 if (it->multibyte_p)
23397 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23398 else
23400 it2.c = it2.char_to_display = *p, it2.len = 1;
23401 if (! ASCII_CHAR_P (it2.c))
23402 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23405 it2.glyph_row = NULL;
23406 it2.what = IT_CHARACTER;
23407 x_produce_glyphs (&it2);
23408 width = NUMVAL (prop) * it2.pixel_width;
23410 #endif /* HAVE_WINDOW_SYSTEM */
23411 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23412 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23414 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23415 align_to = (align_to < 0
23417 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23418 else if (align_to < 0)
23419 align_to = window_box_left_offset (it->w, TEXT_AREA);
23420 width = max (0, (int)tem + align_to - it->current_x);
23421 zero_width_ok_p = 1;
23423 else
23424 /* Nothing specified -> width defaults to canonical char width. */
23425 width = FRAME_COLUMN_WIDTH (it->f);
23427 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23428 width = 1;
23430 #ifdef HAVE_WINDOW_SYSTEM
23431 /* Compute height. */
23432 if (FRAME_WINDOW_P (it->f))
23434 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23435 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23437 height = (int)tem;
23438 zero_height_ok_p = 1;
23440 else if (prop = Fplist_get (plist, QCrelative_height),
23441 NUMVAL (prop) > 0)
23442 height = FONT_HEIGHT (font) * NUMVAL (prop);
23443 else
23444 height = FONT_HEIGHT (font);
23446 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23447 height = 1;
23449 /* Compute percentage of height used for ascent. If
23450 `:ascent ASCENT' is present and valid, use that. Otherwise,
23451 derive the ascent from the font in use. */
23452 if (prop = Fplist_get (plist, QCascent),
23453 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23454 ascent = height * NUMVAL (prop) / 100.0;
23455 else if (!NILP (prop)
23456 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23457 ascent = min (max (0, (int)tem), height);
23458 else
23459 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23461 else
23462 #endif /* HAVE_WINDOW_SYSTEM */
23463 height = 1;
23465 if (width > 0 && it->line_wrap != TRUNCATE
23466 && it->current_x + width > it->last_visible_x)
23467 width = it->last_visible_x - it->current_x - 1;
23469 if (width > 0 && height > 0 && it->glyph_row)
23471 Lisp_Object o_object = it->object;
23472 Lisp_Object object = it->stack[it->sp - 1].string;
23473 int n = width;
23475 if (!STRINGP (object))
23476 object = it->w->buffer;
23477 #ifdef HAVE_WINDOW_SYSTEM
23478 if (FRAME_WINDOW_P (it->f))
23480 append_stretch_glyph (it, object, width, height, ascent);
23481 it->ascent = it->phys_ascent = ascent;
23482 it->descent = it->phys_descent = height - it->ascent;
23483 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23484 take_vertical_position_into_account (it);
23486 else
23487 #endif
23489 it->object = object;
23490 it->char_to_display = ' ';
23491 it->pixel_width = it->len = 1;
23492 while (n--)
23493 tty_append_glyph (it);
23494 it->object = o_object;
23495 it->pixel_width = width;
23500 #ifdef HAVE_WINDOW_SYSTEM
23502 /* Calculate line-height and line-spacing properties.
23503 An integer value specifies explicit pixel value.
23504 A float value specifies relative value to current face height.
23505 A cons (float . face-name) specifies relative value to
23506 height of specified face font.
23508 Returns height in pixels, or nil. */
23511 static Lisp_Object
23512 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23513 int boff, int override)
23515 Lisp_Object face_name = Qnil;
23516 int ascent, descent, height;
23518 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23519 return val;
23521 if (CONSP (val))
23523 face_name = XCAR (val);
23524 val = XCDR (val);
23525 if (!NUMBERP (val))
23526 val = make_number (1);
23527 if (NILP (face_name))
23529 height = it->ascent + it->descent;
23530 goto scale;
23534 if (NILP (face_name))
23536 font = FRAME_FONT (it->f);
23537 boff = FRAME_BASELINE_OFFSET (it->f);
23539 else if (EQ (face_name, Qt))
23541 override = 0;
23543 else
23545 int face_id;
23546 struct face *face;
23548 face_id = lookup_named_face (it->f, face_name, 0);
23549 if (face_id < 0)
23550 return make_number (-1);
23552 face = FACE_FROM_ID (it->f, face_id);
23553 font = face->font;
23554 if (font == NULL)
23555 return make_number (-1);
23556 boff = font->baseline_offset;
23557 if (font->vertical_centering)
23558 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23561 ascent = FONT_BASE (font) + boff;
23562 descent = FONT_DESCENT (font) - boff;
23564 if (override)
23566 it->override_ascent = ascent;
23567 it->override_descent = descent;
23568 it->override_boff = boff;
23571 height = ascent + descent;
23573 scale:
23574 if (FLOATP (val))
23575 height = (int)(XFLOAT_DATA (val) * height);
23576 else if (INTEGERP (val))
23577 height *= XINT (val);
23579 return make_number (height);
23583 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23584 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23585 and only if this is for a character for which no font was found.
23587 If the display method (it->glyphless_method) is
23588 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23589 length of the acronym or the hexadecimal string, UPPER_XOFF and
23590 UPPER_YOFF are pixel offsets for the upper part of the string,
23591 LOWER_XOFF and LOWER_YOFF are for the lower part.
23593 For the other display methods, LEN through LOWER_YOFF are zero. */
23595 static void
23596 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23597 short upper_xoff, short upper_yoff,
23598 short lower_xoff, short lower_yoff)
23600 struct glyph *glyph;
23601 enum glyph_row_area area = it->area;
23603 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23604 if (glyph < it->glyph_row->glyphs[area + 1])
23606 /* If the glyph row is reversed, we need to prepend the glyph
23607 rather than append it. */
23608 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23610 struct glyph *g;
23612 /* Make room for the additional glyph. */
23613 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23614 g[1] = *g;
23615 glyph = it->glyph_row->glyphs[area];
23617 glyph->charpos = CHARPOS (it->position);
23618 glyph->object = it->object;
23619 glyph->pixel_width = it->pixel_width;
23620 glyph->ascent = it->ascent;
23621 glyph->descent = it->descent;
23622 glyph->voffset = it->voffset;
23623 glyph->type = GLYPHLESS_GLYPH;
23624 glyph->u.glyphless.method = it->glyphless_method;
23625 glyph->u.glyphless.for_no_font = for_no_font;
23626 glyph->u.glyphless.len = len;
23627 glyph->u.glyphless.ch = it->c;
23628 glyph->slice.glyphless.upper_xoff = upper_xoff;
23629 glyph->slice.glyphless.upper_yoff = upper_yoff;
23630 glyph->slice.glyphless.lower_xoff = lower_xoff;
23631 glyph->slice.glyphless.lower_yoff = lower_yoff;
23632 glyph->avoid_cursor_p = it->avoid_cursor_p;
23633 glyph->multibyte_p = it->multibyte_p;
23634 glyph->left_box_line_p = it->start_of_box_run_p;
23635 glyph->right_box_line_p = it->end_of_box_run_p;
23636 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23637 || it->phys_descent > it->descent);
23638 glyph->padding_p = 0;
23639 glyph->glyph_not_available_p = 0;
23640 glyph->face_id = face_id;
23641 glyph->font_type = FONT_TYPE_UNKNOWN;
23642 if (it->bidi_p)
23644 glyph->resolved_level = it->bidi_it.resolved_level;
23645 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23646 abort ();
23647 glyph->bidi_type = it->bidi_it.type;
23649 ++it->glyph_row->used[area];
23651 else
23652 IT_EXPAND_MATRIX_WIDTH (it, area);
23656 /* Produce a glyph for a glyphless character for iterator IT.
23657 IT->glyphless_method specifies which method to use for displaying
23658 the character. See the description of enum
23659 glyphless_display_method in dispextern.h for the detail.
23661 FOR_NO_FONT is nonzero if and only if this is for a character for
23662 which no font was found. ACRONYM, if non-nil, is an acronym string
23663 for the character. */
23665 static void
23666 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23668 int face_id;
23669 struct face *face;
23670 struct font *font;
23671 int base_width, base_height, width, height;
23672 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23673 int len;
23675 /* Get the metrics of the base font. We always refer to the current
23676 ASCII face. */
23677 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23678 font = face->font ? face->font : FRAME_FONT (it->f);
23679 it->ascent = FONT_BASE (font) + font->baseline_offset;
23680 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23681 base_height = it->ascent + it->descent;
23682 base_width = font->average_width;
23684 /* Get a face ID for the glyph by utilizing a cache (the same way as
23685 done for `escape-glyph' in get_next_display_element). */
23686 if (it->f == last_glyphless_glyph_frame
23687 && it->face_id == last_glyphless_glyph_face_id)
23689 face_id = last_glyphless_glyph_merged_face_id;
23691 else
23693 /* Merge the `glyphless-char' face into the current face. */
23694 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23695 last_glyphless_glyph_frame = it->f;
23696 last_glyphless_glyph_face_id = it->face_id;
23697 last_glyphless_glyph_merged_face_id = face_id;
23700 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23702 it->pixel_width = THIN_SPACE_WIDTH;
23703 len = 0;
23704 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23706 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23708 width = CHAR_WIDTH (it->c);
23709 if (width == 0)
23710 width = 1;
23711 else if (width > 4)
23712 width = 4;
23713 it->pixel_width = base_width * width;
23714 len = 0;
23715 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23717 else
23719 char buf[7];
23720 const char *str;
23721 unsigned int code[6];
23722 int upper_len;
23723 int ascent, descent;
23724 struct font_metrics metrics_upper, metrics_lower;
23726 face = FACE_FROM_ID (it->f, face_id);
23727 font = face->font ? face->font : FRAME_FONT (it->f);
23728 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23730 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23732 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23733 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23734 if (CONSP (acronym))
23735 acronym = XCAR (acronym);
23736 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23738 else
23740 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23741 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23742 str = buf;
23744 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23745 code[len] = font->driver->encode_char (font, str[len]);
23746 upper_len = (len + 1) / 2;
23747 font->driver->text_extents (font, code, upper_len,
23748 &metrics_upper);
23749 font->driver->text_extents (font, code + upper_len, len - upper_len,
23750 &metrics_lower);
23754 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23755 width = max (metrics_upper.width, metrics_lower.width) + 4;
23756 upper_xoff = upper_yoff = 2; /* the typical case */
23757 if (base_width >= width)
23759 /* Align the upper to the left, the lower to the right. */
23760 it->pixel_width = base_width;
23761 lower_xoff = base_width - 2 - metrics_lower.width;
23763 else
23765 /* Center the shorter one. */
23766 it->pixel_width = width;
23767 if (metrics_upper.width >= metrics_lower.width)
23768 lower_xoff = (width - metrics_lower.width) / 2;
23769 else
23771 /* FIXME: This code doesn't look right. It formerly was
23772 missing the "lower_xoff = 0;", which couldn't have
23773 been right since it left lower_xoff uninitialized. */
23774 lower_xoff = 0;
23775 upper_xoff = (width - metrics_upper.width) / 2;
23779 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23780 top, bottom, and between upper and lower strings. */
23781 height = (metrics_upper.ascent + metrics_upper.descent
23782 + metrics_lower.ascent + metrics_lower.descent) + 5;
23783 /* Center vertically.
23784 H:base_height, D:base_descent
23785 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23787 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23788 descent = D - H/2 + h/2;
23789 lower_yoff = descent - 2 - ld;
23790 upper_yoff = lower_yoff - la - 1 - ud; */
23791 ascent = - (it->descent - (base_height + height + 1) / 2);
23792 descent = it->descent - (base_height - height) / 2;
23793 lower_yoff = descent - 2 - metrics_lower.descent;
23794 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23795 - metrics_upper.descent);
23796 /* Don't make the height shorter than the base height. */
23797 if (height > base_height)
23799 it->ascent = ascent;
23800 it->descent = descent;
23804 it->phys_ascent = it->ascent;
23805 it->phys_descent = it->descent;
23806 if (it->glyph_row)
23807 append_glyphless_glyph (it, face_id, for_no_font, len,
23808 upper_xoff, upper_yoff,
23809 lower_xoff, lower_yoff);
23810 it->nglyphs = 1;
23811 take_vertical_position_into_account (it);
23815 /* RIF:
23816 Produce glyphs/get display metrics for the display element IT is
23817 loaded with. See the description of struct it in dispextern.h
23818 for an overview of struct it. */
23820 void
23821 x_produce_glyphs (struct it *it)
23823 int extra_line_spacing = it->extra_line_spacing;
23825 it->glyph_not_available_p = 0;
23827 if (it->what == IT_CHARACTER)
23829 XChar2b char2b;
23830 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23831 struct font *font = face->font;
23832 struct font_metrics *pcm = NULL;
23833 int boff; /* baseline offset */
23835 if (font == NULL)
23837 /* When no suitable font is found, display this character by
23838 the method specified in the first extra slot of
23839 Vglyphless_char_display. */
23840 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23842 xassert (it->what == IT_GLYPHLESS);
23843 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23844 goto done;
23847 boff = font->baseline_offset;
23848 if (font->vertical_centering)
23849 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23851 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23853 int stretched_p;
23855 it->nglyphs = 1;
23857 if (it->override_ascent >= 0)
23859 it->ascent = it->override_ascent;
23860 it->descent = it->override_descent;
23861 boff = it->override_boff;
23863 else
23865 it->ascent = FONT_BASE (font) + boff;
23866 it->descent = FONT_DESCENT (font) - boff;
23869 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23871 pcm = get_per_char_metric (font, &char2b);
23872 if (pcm->width == 0
23873 && pcm->rbearing == 0 && pcm->lbearing == 0)
23874 pcm = NULL;
23877 if (pcm)
23879 it->phys_ascent = pcm->ascent + boff;
23880 it->phys_descent = pcm->descent - boff;
23881 it->pixel_width = pcm->width;
23883 else
23885 it->glyph_not_available_p = 1;
23886 it->phys_ascent = it->ascent;
23887 it->phys_descent = it->descent;
23888 it->pixel_width = font->space_width;
23891 if (it->constrain_row_ascent_descent_p)
23893 if (it->descent > it->max_descent)
23895 it->ascent += it->descent - it->max_descent;
23896 it->descent = it->max_descent;
23898 if (it->ascent > it->max_ascent)
23900 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23901 it->ascent = it->max_ascent;
23903 it->phys_ascent = min (it->phys_ascent, it->ascent);
23904 it->phys_descent = min (it->phys_descent, it->descent);
23905 extra_line_spacing = 0;
23908 /* If this is a space inside a region of text with
23909 `space-width' property, change its width. */
23910 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23911 if (stretched_p)
23912 it->pixel_width *= XFLOATINT (it->space_width);
23914 /* If face has a box, add the box thickness to the character
23915 height. If character has a box line to the left and/or
23916 right, add the box line width to the character's width. */
23917 if (face->box != FACE_NO_BOX)
23919 int thick = face->box_line_width;
23921 if (thick > 0)
23923 it->ascent += thick;
23924 it->descent += thick;
23926 else
23927 thick = -thick;
23929 if (it->start_of_box_run_p)
23930 it->pixel_width += thick;
23931 if (it->end_of_box_run_p)
23932 it->pixel_width += thick;
23935 /* If face has an overline, add the height of the overline
23936 (1 pixel) and a 1 pixel margin to the character height. */
23937 if (face->overline_p)
23938 it->ascent += overline_margin;
23940 if (it->constrain_row_ascent_descent_p)
23942 if (it->ascent > it->max_ascent)
23943 it->ascent = it->max_ascent;
23944 if (it->descent > it->max_descent)
23945 it->descent = it->max_descent;
23948 take_vertical_position_into_account (it);
23950 /* If we have to actually produce glyphs, do it. */
23951 if (it->glyph_row)
23953 if (stretched_p)
23955 /* Translate a space with a `space-width' property
23956 into a stretch glyph. */
23957 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23958 / FONT_HEIGHT (font));
23959 append_stretch_glyph (it, it->object, it->pixel_width,
23960 it->ascent + it->descent, ascent);
23962 else
23963 append_glyph (it);
23965 /* If characters with lbearing or rbearing are displayed
23966 in this line, record that fact in a flag of the
23967 glyph row. This is used to optimize X output code. */
23968 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23969 it->glyph_row->contains_overlapping_glyphs_p = 1;
23971 if (! stretched_p && it->pixel_width == 0)
23972 /* We assure that all visible glyphs have at least 1-pixel
23973 width. */
23974 it->pixel_width = 1;
23976 else if (it->char_to_display == '\n')
23978 /* A newline has no width, but we need the height of the
23979 line. But if previous part of the line sets a height,
23980 don't increase that height */
23982 Lisp_Object height;
23983 Lisp_Object total_height = Qnil;
23985 it->override_ascent = -1;
23986 it->pixel_width = 0;
23987 it->nglyphs = 0;
23989 height = get_it_property (it, Qline_height);
23990 /* Split (line-height total-height) list */
23991 if (CONSP (height)
23992 && CONSP (XCDR (height))
23993 && NILP (XCDR (XCDR (height))))
23995 total_height = XCAR (XCDR (height));
23996 height = XCAR (height);
23998 height = calc_line_height_property (it, height, font, boff, 1);
24000 if (it->override_ascent >= 0)
24002 it->ascent = it->override_ascent;
24003 it->descent = it->override_descent;
24004 boff = it->override_boff;
24006 else
24008 it->ascent = FONT_BASE (font) + boff;
24009 it->descent = FONT_DESCENT (font) - boff;
24012 if (EQ (height, Qt))
24014 if (it->descent > it->max_descent)
24016 it->ascent += it->descent - it->max_descent;
24017 it->descent = it->max_descent;
24019 if (it->ascent > it->max_ascent)
24021 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24022 it->ascent = it->max_ascent;
24024 it->phys_ascent = min (it->phys_ascent, it->ascent);
24025 it->phys_descent = min (it->phys_descent, it->descent);
24026 it->constrain_row_ascent_descent_p = 1;
24027 extra_line_spacing = 0;
24029 else
24031 Lisp_Object spacing;
24033 it->phys_ascent = it->ascent;
24034 it->phys_descent = it->descent;
24036 if ((it->max_ascent > 0 || it->max_descent > 0)
24037 && face->box != FACE_NO_BOX
24038 && face->box_line_width > 0)
24040 it->ascent += face->box_line_width;
24041 it->descent += face->box_line_width;
24043 if (!NILP (height)
24044 && XINT (height) > it->ascent + it->descent)
24045 it->ascent = XINT (height) - it->descent;
24047 if (!NILP (total_height))
24048 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24049 else
24051 spacing = get_it_property (it, Qline_spacing);
24052 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24054 if (INTEGERP (spacing))
24056 extra_line_spacing = XINT (spacing);
24057 if (!NILP (total_height))
24058 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24062 else /* i.e. (it->char_to_display == '\t') */
24064 if (font->space_width > 0)
24066 int tab_width = it->tab_width * font->space_width;
24067 int x = it->current_x + it->continuation_lines_width;
24068 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24070 /* If the distance from the current position to the next tab
24071 stop is less than a space character width, use the
24072 tab stop after that. */
24073 if (next_tab_x - x < font->space_width)
24074 next_tab_x += tab_width;
24076 it->pixel_width = next_tab_x - x;
24077 it->nglyphs = 1;
24078 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24079 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24081 if (it->glyph_row)
24083 append_stretch_glyph (it, it->object, it->pixel_width,
24084 it->ascent + it->descent, it->ascent);
24087 else
24089 it->pixel_width = 0;
24090 it->nglyphs = 1;
24094 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24096 /* A static composition.
24098 Note: A composition is represented as one glyph in the
24099 glyph matrix. There are no padding glyphs.
24101 Important note: pixel_width, ascent, and descent are the
24102 values of what is drawn by draw_glyphs (i.e. the values of
24103 the overall glyphs composed). */
24104 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24105 int boff; /* baseline offset */
24106 struct composition *cmp = composition_table[it->cmp_it.id];
24107 int glyph_len = cmp->glyph_len;
24108 struct font *font = face->font;
24110 it->nglyphs = 1;
24112 /* If we have not yet calculated pixel size data of glyphs of
24113 the composition for the current face font, calculate them
24114 now. Theoretically, we have to check all fonts for the
24115 glyphs, but that requires much time and memory space. So,
24116 here we check only the font of the first glyph. This may
24117 lead to incorrect display, but it's very rare, and C-l
24118 (recenter-top-bottom) can correct the display anyway. */
24119 if (! cmp->font || cmp->font != font)
24121 /* Ascent and descent of the font of the first character
24122 of this composition (adjusted by baseline offset).
24123 Ascent and descent of overall glyphs should not be less
24124 than these, respectively. */
24125 int font_ascent, font_descent, font_height;
24126 /* Bounding box of the overall glyphs. */
24127 int leftmost, rightmost, lowest, highest;
24128 int lbearing, rbearing;
24129 int i, width, ascent, descent;
24130 int left_padded = 0, right_padded = 0;
24131 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24132 XChar2b char2b;
24133 struct font_metrics *pcm;
24134 int font_not_found_p;
24135 EMACS_INT pos;
24137 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24138 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24139 break;
24140 if (glyph_len < cmp->glyph_len)
24141 right_padded = 1;
24142 for (i = 0; i < glyph_len; i++)
24144 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24145 break;
24146 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24148 if (i > 0)
24149 left_padded = 1;
24151 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24152 : IT_CHARPOS (*it));
24153 /* If no suitable font is found, use the default font. */
24154 font_not_found_p = font == NULL;
24155 if (font_not_found_p)
24157 face = face->ascii_face;
24158 font = face->font;
24160 boff = font->baseline_offset;
24161 if (font->vertical_centering)
24162 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24163 font_ascent = FONT_BASE (font) + boff;
24164 font_descent = FONT_DESCENT (font) - boff;
24165 font_height = FONT_HEIGHT (font);
24167 cmp->font = (void *) font;
24169 pcm = NULL;
24170 if (! font_not_found_p)
24172 get_char_face_and_encoding (it->f, c, it->face_id,
24173 &char2b, 0);
24174 pcm = get_per_char_metric (font, &char2b);
24177 /* Initialize the bounding box. */
24178 if (pcm)
24180 width = pcm->width;
24181 ascent = pcm->ascent;
24182 descent = pcm->descent;
24183 lbearing = pcm->lbearing;
24184 rbearing = pcm->rbearing;
24186 else
24188 width = font->space_width;
24189 ascent = FONT_BASE (font);
24190 descent = FONT_DESCENT (font);
24191 lbearing = 0;
24192 rbearing = width;
24195 rightmost = width;
24196 leftmost = 0;
24197 lowest = - descent + boff;
24198 highest = ascent + boff;
24200 if (! font_not_found_p
24201 && font->default_ascent
24202 && CHAR_TABLE_P (Vuse_default_ascent)
24203 && !NILP (Faref (Vuse_default_ascent,
24204 make_number (it->char_to_display))))
24205 highest = font->default_ascent + boff;
24207 /* Draw the first glyph at the normal position. It may be
24208 shifted to right later if some other glyphs are drawn
24209 at the left. */
24210 cmp->offsets[i * 2] = 0;
24211 cmp->offsets[i * 2 + 1] = boff;
24212 cmp->lbearing = lbearing;
24213 cmp->rbearing = rbearing;
24215 /* Set cmp->offsets for the remaining glyphs. */
24216 for (i++; i < glyph_len; i++)
24218 int left, right, btm, top;
24219 int ch = COMPOSITION_GLYPH (cmp, i);
24220 int face_id;
24221 struct face *this_face;
24223 if (ch == '\t')
24224 ch = ' ';
24225 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24226 this_face = FACE_FROM_ID (it->f, face_id);
24227 font = this_face->font;
24229 if (font == NULL)
24230 pcm = NULL;
24231 else
24233 get_char_face_and_encoding (it->f, ch, face_id,
24234 &char2b, 0);
24235 pcm = get_per_char_metric (font, &char2b);
24237 if (! pcm)
24238 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24239 else
24241 width = pcm->width;
24242 ascent = pcm->ascent;
24243 descent = pcm->descent;
24244 lbearing = pcm->lbearing;
24245 rbearing = pcm->rbearing;
24246 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24248 /* Relative composition with or without
24249 alternate chars. */
24250 left = (leftmost + rightmost - width) / 2;
24251 btm = - descent + boff;
24252 if (font->relative_compose
24253 && (! CHAR_TABLE_P (Vignore_relative_composition)
24254 || NILP (Faref (Vignore_relative_composition,
24255 make_number (ch)))))
24258 if (- descent >= font->relative_compose)
24259 /* One extra pixel between two glyphs. */
24260 btm = highest + 1;
24261 else if (ascent <= 0)
24262 /* One extra pixel between two glyphs. */
24263 btm = lowest - 1 - ascent - descent;
24266 else
24268 /* A composition rule is specified by an integer
24269 value that encodes global and new reference
24270 points (GREF and NREF). GREF and NREF are
24271 specified by numbers as below:
24273 0---1---2 -- ascent
24277 9--10--11 -- center
24279 ---3---4---5--- baseline
24281 6---7---8 -- descent
24283 int rule = COMPOSITION_RULE (cmp, i);
24284 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24286 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24287 grefx = gref % 3, nrefx = nref % 3;
24288 grefy = gref / 3, nrefy = nref / 3;
24289 if (xoff)
24290 xoff = font_height * (xoff - 128) / 256;
24291 if (yoff)
24292 yoff = font_height * (yoff - 128) / 256;
24294 left = (leftmost
24295 + grefx * (rightmost - leftmost) / 2
24296 - nrefx * width / 2
24297 + xoff);
24299 btm = ((grefy == 0 ? highest
24300 : grefy == 1 ? 0
24301 : grefy == 2 ? lowest
24302 : (highest + lowest) / 2)
24303 - (nrefy == 0 ? ascent + descent
24304 : nrefy == 1 ? descent - boff
24305 : nrefy == 2 ? 0
24306 : (ascent + descent) / 2)
24307 + yoff);
24310 cmp->offsets[i * 2] = left;
24311 cmp->offsets[i * 2 + 1] = btm + descent;
24313 /* Update the bounding box of the overall glyphs. */
24314 if (width > 0)
24316 right = left + width;
24317 if (left < leftmost)
24318 leftmost = left;
24319 if (right > rightmost)
24320 rightmost = right;
24322 top = btm + descent + ascent;
24323 if (top > highest)
24324 highest = top;
24325 if (btm < lowest)
24326 lowest = btm;
24328 if (cmp->lbearing > left + lbearing)
24329 cmp->lbearing = left + lbearing;
24330 if (cmp->rbearing < left + rbearing)
24331 cmp->rbearing = left + rbearing;
24335 /* If there are glyphs whose x-offsets are negative,
24336 shift all glyphs to the right and make all x-offsets
24337 non-negative. */
24338 if (leftmost < 0)
24340 for (i = 0; i < cmp->glyph_len; i++)
24341 cmp->offsets[i * 2] -= leftmost;
24342 rightmost -= leftmost;
24343 cmp->lbearing -= leftmost;
24344 cmp->rbearing -= leftmost;
24347 if (left_padded && cmp->lbearing < 0)
24349 for (i = 0; i < cmp->glyph_len; i++)
24350 cmp->offsets[i * 2] -= cmp->lbearing;
24351 rightmost -= cmp->lbearing;
24352 cmp->rbearing -= cmp->lbearing;
24353 cmp->lbearing = 0;
24355 if (right_padded && rightmost < cmp->rbearing)
24357 rightmost = cmp->rbearing;
24360 cmp->pixel_width = rightmost;
24361 cmp->ascent = highest;
24362 cmp->descent = - lowest;
24363 if (cmp->ascent < font_ascent)
24364 cmp->ascent = font_ascent;
24365 if (cmp->descent < font_descent)
24366 cmp->descent = font_descent;
24369 if (it->glyph_row
24370 && (cmp->lbearing < 0
24371 || cmp->rbearing > cmp->pixel_width))
24372 it->glyph_row->contains_overlapping_glyphs_p = 1;
24374 it->pixel_width = cmp->pixel_width;
24375 it->ascent = it->phys_ascent = cmp->ascent;
24376 it->descent = it->phys_descent = cmp->descent;
24377 if (face->box != FACE_NO_BOX)
24379 int thick = face->box_line_width;
24381 if (thick > 0)
24383 it->ascent += thick;
24384 it->descent += thick;
24386 else
24387 thick = - thick;
24389 if (it->start_of_box_run_p)
24390 it->pixel_width += thick;
24391 if (it->end_of_box_run_p)
24392 it->pixel_width += thick;
24395 /* If face has an overline, add the height of the overline
24396 (1 pixel) and a 1 pixel margin to the character height. */
24397 if (face->overline_p)
24398 it->ascent += overline_margin;
24400 take_vertical_position_into_account (it);
24401 if (it->ascent < 0)
24402 it->ascent = 0;
24403 if (it->descent < 0)
24404 it->descent = 0;
24406 if (it->glyph_row)
24407 append_composite_glyph (it);
24409 else if (it->what == IT_COMPOSITION)
24411 /* A dynamic (automatic) composition. */
24412 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24413 Lisp_Object gstring;
24414 struct font_metrics metrics;
24416 it->nglyphs = 1;
24418 gstring = composition_gstring_from_id (it->cmp_it.id);
24419 it->pixel_width
24420 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24421 &metrics);
24422 if (it->glyph_row
24423 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24424 it->glyph_row->contains_overlapping_glyphs_p = 1;
24425 it->ascent = it->phys_ascent = metrics.ascent;
24426 it->descent = it->phys_descent = metrics.descent;
24427 if (face->box != FACE_NO_BOX)
24429 int thick = face->box_line_width;
24431 if (thick > 0)
24433 it->ascent += thick;
24434 it->descent += thick;
24436 else
24437 thick = - thick;
24439 if (it->start_of_box_run_p)
24440 it->pixel_width += thick;
24441 if (it->end_of_box_run_p)
24442 it->pixel_width += thick;
24444 /* If face has an overline, add the height of the overline
24445 (1 pixel) and a 1 pixel margin to the character height. */
24446 if (face->overline_p)
24447 it->ascent += overline_margin;
24448 take_vertical_position_into_account (it);
24449 if (it->ascent < 0)
24450 it->ascent = 0;
24451 if (it->descent < 0)
24452 it->descent = 0;
24454 if (it->glyph_row)
24455 append_composite_glyph (it);
24457 else if (it->what == IT_GLYPHLESS)
24458 produce_glyphless_glyph (it, 0, Qnil);
24459 else if (it->what == IT_IMAGE)
24460 produce_image_glyph (it);
24461 else if (it->what == IT_STRETCH)
24462 produce_stretch_glyph (it);
24463 #ifdef HAVE_XWIDGETS
24464 else if (it->what == IT_XWIDGET)
24465 produce_xwidget_glyph (it);
24466 #endif
24467 done:
24468 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24469 because this isn't true for images with `:ascent 100'. */
24470 xassert (it->ascent >= 0 && it->descent >= 0);
24471 if (it->area == TEXT_AREA)
24472 it->current_x += it->pixel_width;
24474 if (extra_line_spacing > 0)
24476 it->descent += extra_line_spacing;
24477 if (extra_line_spacing > it->max_extra_line_spacing)
24478 it->max_extra_line_spacing = extra_line_spacing;
24481 it->max_ascent = max (it->max_ascent, it->ascent);
24482 it->max_descent = max (it->max_descent, it->descent);
24483 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24484 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24487 /* EXPORT for RIF:
24488 Output LEN glyphs starting at START at the nominal cursor position.
24489 Advance the nominal cursor over the text. The global variable
24490 updated_window contains the window being updated, updated_row is
24491 the glyph row being updated, and updated_area is the area of that
24492 row being updated. */
24494 void
24495 x_write_glyphs (struct glyph *start, int len)
24497 int x, hpos;
24499 xassert (updated_window && updated_row);
24500 BLOCK_INPUT;
24502 /* Write glyphs. */
24504 hpos = start - updated_row->glyphs[updated_area];
24505 x = draw_glyphs (updated_window, output_cursor.x,
24506 updated_row, updated_area,
24507 hpos, hpos + len,
24508 DRAW_NORMAL_TEXT, 0);
24510 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24511 if (updated_area == TEXT_AREA
24512 && updated_window->phys_cursor_on_p
24513 && updated_window->phys_cursor.vpos == output_cursor.vpos
24514 && updated_window->phys_cursor.hpos >= hpos
24515 && updated_window->phys_cursor.hpos < hpos + len)
24516 updated_window->phys_cursor_on_p = 0;
24518 UNBLOCK_INPUT;
24520 /* Advance the output cursor. */
24521 output_cursor.hpos += len;
24522 output_cursor.x = x;
24526 /* EXPORT for RIF:
24527 Insert LEN glyphs from START at the nominal cursor position. */
24529 void
24530 x_insert_glyphs (struct glyph *start, int len)
24532 struct frame *f;
24533 struct window *w;
24534 int line_height, shift_by_width, shifted_region_width;
24535 struct glyph_row *row;
24536 struct glyph *glyph;
24537 int frame_x, frame_y;
24538 EMACS_INT hpos;
24540 xassert (updated_window && updated_row);
24541 BLOCK_INPUT;
24542 w = updated_window;
24543 f = XFRAME (WINDOW_FRAME (w));
24545 /* Get the height of the line we are in. */
24546 row = updated_row;
24547 line_height = row->height;
24549 /* Get the width of the glyphs to insert. */
24550 shift_by_width = 0;
24551 for (glyph = start; glyph < start + len; ++glyph)
24552 shift_by_width += glyph->pixel_width;
24554 /* Get the width of the region to shift right. */
24555 shifted_region_width = (window_box_width (w, updated_area)
24556 - output_cursor.x
24557 - shift_by_width);
24559 /* Shift right. */
24560 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24561 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24563 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24564 line_height, shift_by_width);
24566 /* Write the glyphs. */
24567 hpos = start - row->glyphs[updated_area];
24568 draw_glyphs (w, output_cursor.x, row, updated_area,
24569 hpos, hpos + len,
24570 DRAW_NORMAL_TEXT, 0);
24572 /* Advance the output cursor. */
24573 output_cursor.hpos += len;
24574 output_cursor.x += shift_by_width;
24575 UNBLOCK_INPUT;
24579 /* EXPORT for RIF:
24580 Erase the current text line from the nominal cursor position
24581 (inclusive) to pixel column TO_X (exclusive). The idea is that
24582 everything from TO_X onward is already erased.
24584 TO_X is a pixel position relative to updated_area of
24585 updated_window. TO_X == -1 means clear to the end of this area. */
24587 void
24588 x_clear_end_of_line (int to_x)
24590 struct frame *f;
24591 struct window *w = updated_window;
24592 int max_x, min_y, max_y;
24593 int from_x, from_y, to_y;
24595 xassert (updated_window && updated_row);
24596 f = XFRAME (w->frame);
24598 if (updated_row->full_width_p)
24599 max_x = WINDOW_TOTAL_WIDTH (w);
24600 else
24601 max_x = window_box_width (w, updated_area);
24602 max_y = window_text_bottom_y (w);
24604 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24605 of window. For TO_X > 0, truncate to end of drawing area. */
24606 if (to_x == 0)
24607 return;
24608 else if (to_x < 0)
24609 to_x = max_x;
24610 else
24611 to_x = min (to_x, max_x);
24613 to_y = min (max_y, output_cursor.y + updated_row->height);
24615 /* Notice if the cursor will be cleared by this operation. */
24616 if (!updated_row->full_width_p)
24617 notice_overwritten_cursor (w, updated_area,
24618 output_cursor.x, -1,
24619 updated_row->y,
24620 MATRIX_ROW_BOTTOM_Y (updated_row));
24622 from_x = output_cursor.x;
24624 /* Translate to frame coordinates. */
24625 if (updated_row->full_width_p)
24627 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24628 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24630 else
24632 int area_left = window_box_left (w, updated_area);
24633 from_x += area_left;
24634 to_x += area_left;
24637 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24638 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24639 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24641 /* Prevent inadvertently clearing to end of the X window. */
24642 if (to_x > from_x && to_y > from_y)
24644 BLOCK_INPUT;
24645 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24646 to_x - from_x, to_y - from_y);
24647 UNBLOCK_INPUT;
24651 #endif /* HAVE_WINDOW_SYSTEM */
24655 /***********************************************************************
24656 Cursor types
24657 ***********************************************************************/
24659 /* Value is the internal representation of the specified cursor type
24660 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24661 of the bar cursor. */
24663 static enum text_cursor_kinds
24664 get_specified_cursor_type (Lisp_Object arg, int *width)
24666 enum text_cursor_kinds type;
24668 if (NILP (arg))
24669 return NO_CURSOR;
24671 if (EQ (arg, Qbox))
24672 return FILLED_BOX_CURSOR;
24674 if (EQ (arg, Qhollow))
24675 return HOLLOW_BOX_CURSOR;
24677 if (EQ (arg, Qbar))
24679 *width = 2;
24680 return BAR_CURSOR;
24683 if (CONSP (arg)
24684 && EQ (XCAR (arg), Qbar)
24685 && INTEGERP (XCDR (arg))
24686 && XINT (XCDR (arg)) >= 0)
24688 *width = XINT (XCDR (arg));
24689 return BAR_CURSOR;
24692 if (EQ (arg, Qhbar))
24694 *width = 2;
24695 return HBAR_CURSOR;
24698 if (CONSP (arg)
24699 && EQ (XCAR (arg), Qhbar)
24700 && INTEGERP (XCDR (arg))
24701 && XINT (XCDR (arg)) >= 0)
24703 *width = XINT (XCDR (arg));
24704 return HBAR_CURSOR;
24707 /* Treat anything unknown as "hollow box cursor".
24708 It was bad to signal an error; people have trouble fixing
24709 .Xdefaults with Emacs, when it has something bad in it. */
24710 type = HOLLOW_BOX_CURSOR;
24712 return type;
24715 /* Set the default cursor types for specified frame. */
24716 void
24717 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24719 int width = 1;
24720 Lisp_Object tem;
24722 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24723 FRAME_CURSOR_WIDTH (f) = width;
24725 /* By default, set up the blink-off state depending on the on-state. */
24727 tem = Fassoc (arg, Vblink_cursor_alist);
24728 if (!NILP (tem))
24730 FRAME_BLINK_OFF_CURSOR (f)
24731 = get_specified_cursor_type (XCDR (tem), &width);
24732 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24734 else
24735 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24739 #ifdef HAVE_WINDOW_SYSTEM
24741 /* Return the cursor we want to be displayed in window W. Return
24742 width of bar/hbar cursor through WIDTH arg. Return with
24743 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24744 (i.e. if the `system caret' should track this cursor).
24746 In a mini-buffer window, we want the cursor only to appear if we
24747 are reading input from this window. For the selected window, we
24748 want the cursor type given by the frame parameter or buffer local
24749 setting of cursor-type. If explicitly marked off, draw no cursor.
24750 In all other cases, we want a hollow box cursor. */
24752 static enum text_cursor_kinds
24753 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24754 int *active_cursor)
24756 struct frame *f = XFRAME (w->frame);
24757 struct buffer *b = XBUFFER (w->buffer);
24758 int cursor_type = DEFAULT_CURSOR;
24759 Lisp_Object alt_cursor;
24760 int non_selected = 0;
24762 *active_cursor = 1;
24764 /* Echo area */
24765 if (cursor_in_echo_area
24766 && FRAME_HAS_MINIBUF_P (f)
24767 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24769 if (w == XWINDOW (echo_area_window))
24771 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24773 *width = FRAME_CURSOR_WIDTH (f);
24774 return FRAME_DESIRED_CURSOR (f);
24776 else
24777 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24780 *active_cursor = 0;
24781 non_selected = 1;
24784 /* Detect a nonselected window or nonselected frame. */
24785 else if (w != XWINDOW (f->selected_window)
24786 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24788 *active_cursor = 0;
24790 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24791 return NO_CURSOR;
24793 non_selected = 1;
24796 /* Never display a cursor in a window in which cursor-type is nil. */
24797 if (NILP (BVAR (b, cursor_type)))
24798 return NO_CURSOR;
24800 /* Get the normal cursor type for this window. */
24801 if (EQ (BVAR (b, cursor_type), Qt))
24803 cursor_type = FRAME_DESIRED_CURSOR (f);
24804 *width = FRAME_CURSOR_WIDTH (f);
24806 else
24807 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24809 /* Use cursor-in-non-selected-windows instead
24810 for non-selected window or frame. */
24811 if (non_selected)
24813 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24814 if (!EQ (Qt, alt_cursor))
24815 return get_specified_cursor_type (alt_cursor, width);
24816 /* t means modify the normal cursor type. */
24817 if (cursor_type == FILLED_BOX_CURSOR)
24818 cursor_type = HOLLOW_BOX_CURSOR;
24819 else if (cursor_type == BAR_CURSOR && *width > 1)
24820 --*width;
24821 return cursor_type;
24824 /* Use normal cursor if not blinked off. */
24825 if (!w->cursor_off_p)
24828 #ifdef HAVE_XWIDGETS
24829 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
24830 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
24831 return NO_CURSOR;
24833 #endif
24834 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24836 if (cursor_type == FILLED_BOX_CURSOR)
24838 /* Using a block cursor on large images can be very annoying.
24839 So use a hollow cursor for "large" images.
24840 If image is not transparent (no mask), also use hollow cursor. */
24841 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24842 if (img != NULL && IMAGEP (img->spec))
24844 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24845 where N = size of default frame font size.
24846 This should cover most of the "tiny" icons people may use. */
24847 if (!img->mask
24848 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24849 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24850 cursor_type = HOLLOW_BOX_CURSOR;
24853 else if (cursor_type != NO_CURSOR)
24855 /* Display current only supports BOX and HOLLOW cursors for images.
24856 So for now, unconditionally use a HOLLOW cursor when cursor is
24857 not a solid box cursor. */
24858 cursor_type = HOLLOW_BOX_CURSOR;
24861 return cursor_type;
24864 /* Cursor is blinked off, so determine how to "toggle" it. */
24866 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24867 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24868 return get_specified_cursor_type (XCDR (alt_cursor), width);
24870 /* Then see if frame has specified a specific blink off cursor type. */
24871 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24873 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24874 return FRAME_BLINK_OFF_CURSOR (f);
24877 #if 0
24878 /* Some people liked having a permanently visible blinking cursor,
24879 while others had very strong opinions against it. So it was
24880 decided to remove it. KFS 2003-09-03 */
24882 /* Finally perform built-in cursor blinking:
24883 filled box <-> hollow box
24884 wide [h]bar <-> narrow [h]bar
24885 narrow [h]bar <-> no cursor
24886 other type <-> no cursor */
24888 if (cursor_type == FILLED_BOX_CURSOR)
24889 return HOLLOW_BOX_CURSOR;
24891 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24893 *width = 1;
24894 return cursor_type;
24896 #endif
24898 return NO_CURSOR;
24902 /* Notice when the text cursor of window W has been completely
24903 overwritten by a drawing operation that outputs glyphs in AREA
24904 starting at X0 and ending at X1 in the line starting at Y0 and
24905 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24906 the rest of the line after X0 has been written. Y coordinates
24907 are window-relative. */
24909 static void
24910 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24911 int x0, int x1, int y0, int y1)
24913 int cx0, cx1, cy0, cy1;
24914 struct glyph_row *row;
24916 if (!w->phys_cursor_on_p)
24917 return;
24918 if (area != TEXT_AREA)
24919 return;
24921 if (w->phys_cursor.vpos < 0
24922 || w->phys_cursor.vpos >= w->current_matrix->nrows
24923 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24924 !(row->enabled_p && row->displays_text_p)))
24925 return;
24927 if (row->cursor_in_fringe_p)
24929 row->cursor_in_fringe_p = 0;
24930 draw_fringe_bitmap (w, row, row->reversed_p);
24931 w->phys_cursor_on_p = 0;
24932 return;
24935 cx0 = w->phys_cursor.x;
24936 cx1 = cx0 + w->phys_cursor_width;
24937 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24938 return;
24940 /* The cursor image will be completely removed from the
24941 screen if the output area intersects the cursor area in
24942 y-direction. When we draw in [y0 y1[, and some part of
24943 the cursor is at y < y0, that part must have been drawn
24944 before. When scrolling, the cursor is erased before
24945 actually scrolling, so we don't come here. When not
24946 scrolling, the rows above the old cursor row must have
24947 changed, and in this case these rows must have written
24948 over the cursor image.
24950 Likewise if part of the cursor is below y1, with the
24951 exception of the cursor being in the first blank row at
24952 the buffer and window end because update_text_area
24953 doesn't draw that row. (Except when it does, but
24954 that's handled in update_text_area.) */
24956 cy0 = w->phys_cursor.y;
24957 cy1 = cy0 + w->phys_cursor_height;
24958 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24959 return;
24961 w->phys_cursor_on_p = 0;
24964 #endif /* HAVE_WINDOW_SYSTEM */
24967 /************************************************************************
24968 Mouse Face
24969 ************************************************************************/
24971 #ifdef HAVE_WINDOW_SYSTEM
24973 /* EXPORT for RIF:
24974 Fix the display of area AREA of overlapping row ROW in window W
24975 with respect to the overlapping part OVERLAPS. */
24977 void
24978 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24979 enum glyph_row_area area, int overlaps)
24981 int i, x;
24983 BLOCK_INPUT;
24985 x = 0;
24986 for (i = 0; i < row->used[area];)
24988 if (row->glyphs[area][i].overlaps_vertically_p)
24990 int start = i, start_x = x;
24994 x += row->glyphs[area][i].pixel_width;
24995 ++i;
24997 while (i < row->used[area]
24998 && row->glyphs[area][i].overlaps_vertically_p);
25000 draw_glyphs (w, start_x, row, area,
25001 start, i,
25002 DRAW_NORMAL_TEXT, overlaps);
25004 else
25006 x += row->glyphs[area][i].pixel_width;
25007 ++i;
25011 UNBLOCK_INPUT;
25015 /* EXPORT:
25016 Draw the cursor glyph of window W in glyph row ROW. See the
25017 comment of draw_glyphs for the meaning of HL. */
25019 void
25020 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25021 enum draw_glyphs_face hl)
25023 /* If cursor hpos is out of bounds, don't draw garbage. This can
25024 happen in mini-buffer windows when switching between echo area
25025 glyphs and mini-buffer. */
25026 if ((row->reversed_p
25027 ? (w->phys_cursor.hpos >= 0)
25028 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25030 int on_p = w->phys_cursor_on_p;
25031 int x1;
25032 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
25033 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
25034 hl, 0);
25035 w->phys_cursor_on_p = on_p;
25037 if (hl == DRAW_CURSOR)
25038 w->phys_cursor_width = x1 - w->phys_cursor.x;
25039 /* When we erase the cursor, and ROW is overlapped by other
25040 rows, make sure that these overlapping parts of other rows
25041 are redrawn. */
25042 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25044 w->phys_cursor_width = x1 - w->phys_cursor.x;
25046 if (row > w->current_matrix->rows
25047 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25048 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25049 OVERLAPS_ERASED_CURSOR);
25051 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25052 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25053 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25054 OVERLAPS_ERASED_CURSOR);
25060 /* EXPORT:
25061 Erase the image of a cursor of window W from the screen. */
25063 void
25064 erase_phys_cursor (struct window *w)
25066 struct frame *f = XFRAME (w->frame);
25067 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25068 int hpos = w->phys_cursor.hpos;
25069 int vpos = w->phys_cursor.vpos;
25070 int mouse_face_here_p = 0;
25071 struct glyph_matrix *active_glyphs = w->current_matrix;
25072 struct glyph_row *cursor_row;
25073 struct glyph *cursor_glyph;
25074 enum draw_glyphs_face hl;
25076 /* No cursor displayed or row invalidated => nothing to do on the
25077 screen. */
25078 if (w->phys_cursor_type == NO_CURSOR)
25079 goto mark_cursor_off;
25081 /* VPOS >= active_glyphs->nrows means that window has been resized.
25082 Don't bother to erase the cursor. */
25083 if (vpos >= active_glyphs->nrows)
25084 goto mark_cursor_off;
25086 /* If row containing cursor is marked invalid, there is nothing we
25087 can do. */
25088 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25089 if (!cursor_row->enabled_p)
25090 goto mark_cursor_off;
25092 /* If line spacing is > 0, old cursor may only be partially visible in
25093 window after split-window. So adjust visible height. */
25094 cursor_row->visible_height = min (cursor_row->visible_height,
25095 window_text_bottom_y (w) - cursor_row->y);
25097 /* If row is completely invisible, don't attempt to delete a cursor which
25098 isn't there. This can happen if cursor is at top of a window, and
25099 we switch to a buffer with a header line in that window. */
25100 if (cursor_row->visible_height <= 0)
25101 goto mark_cursor_off;
25103 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25104 if (cursor_row->cursor_in_fringe_p)
25106 cursor_row->cursor_in_fringe_p = 0;
25107 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25108 goto mark_cursor_off;
25111 /* This can happen when the new row is shorter than the old one.
25112 In this case, either draw_glyphs or clear_end_of_line
25113 should have cleared the cursor. Note that we wouldn't be
25114 able to erase the cursor in this case because we don't have a
25115 cursor glyph at hand. */
25116 if ((cursor_row->reversed_p
25117 ? (w->phys_cursor.hpos < 0)
25118 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25119 goto mark_cursor_off;
25121 /* If the cursor is in the mouse face area, redisplay that when
25122 we clear the cursor. */
25123 if (! NILP (hlinfo->mouse_face_window)
25124 && coords_in_mouse_face_p (w, hpos, vpos)
25125 /* Don't redraw the cursor's spot in mouse face if it is at the
25126 end of a line (on a newline). The cursor appears there, but
25127 mouse highlighting does not. */
25128 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25129 mouse_face_here_p = 1;
25131 /* Maybe clear the display under the cursor. */
25132 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25134 int x, y, left_x;
25135 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25136 int width;
25138 cursor_glyph = get_phys_cursor_glyph (w);
25139 if (cursor_glyph == NULL)
25140 goto mark_cursor_off;
25142 width = cursor_glyph->pixel_width;
25143 left_x = window_box_left_offset (w, TEXT_AREA);
25144 x = w->phys_cursor.x;
25145 if (x < left_x)
25146 width -= left_x - x;
25147 width = min (width, window_box_width (w, TEXT_AREA) - x);
25148 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25149 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25151 if (width > 0)
25152 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25155 /* Erase the cursor by redrawing the character underneath it. */
25156 if (mouse_face_here_p)
25157 hl = DRAW_MOUSE_FACE;
25158 else
25159 hl = DRAW_NORMAL_TEXT;
25160 draw_phys_cursor_glyph (w, cursor_row, hl);
25162 mark_cursor_off:
25163 w->phys_cursor_on_p = 0;
25164 w->phys_cursor_type = NO_CURSOR;
25168 /* EXPORT:
25169 Display or clear cursor of window W. If ON is zero, clear the
25170 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25171 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25173 void
25174 display_and_set_cursor (struct window *w, int on,
25175 int hpos, int vpos, int x, int y)
25177 struct frame *f = XFRAME (w->frame);
25178 int new_cursor_type;
25179 int new_cursor_width;
25180 int active_cursor;
25181 struct glyph_row *glyph_row;
25182 struct glyph *glyph;
25184 /* This is pointless on invisible frames, and dangerous on garbaged
25185 windows and frames; in the latter case, the frame or window may
25186 be in the midst of changing its size, and x and y may be off the
25187 window. */
25188 if (! FRAME_VISIBLE_P (f)
25189 || FRAME_GARBAGED_P (f)
25190 || vpos >= w->current_matrix->nrows
25191 || hpos >= w->current_matrix->matrix_w)
25192 return;
25194 /* If cursor is off and we want it off, return quickly. */
25195 if (!on && !w->phys_cursor_on_p)
25196 return;
25198 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25199 /* If cursor row is not enabled, we don't really know where to
25200 display the cursor. */
25201 if (!glyph_row->enabled_p)
25203 w->phys_cursor_on_p = 0;
25204 return;
25207 glyph = NULL;
25208 if (!glyph_row->exact_window_width_line_p
25209 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25210 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25212 xassert (interrupt_input_blocked);
25214 /* Set new_cursor_type to the cursor we want to be displayed. */
25215 new_cursor_type = get_window_cursor_type (w, glyph,
25216 &new_cursor_width, &active_cursor);
25218 /* If cursor is currently being shown and we don't want it to be or
25219 it is in the wrong place, or the cursor type is not what we want,
25220 erase it. */
25221 if (w->phys_cursor_on_p
25222 && (!on
25223 || w->phys_cursor.x != x
25224 || w->phys_cursor.y != y
25225 || new_cursor_type != w->phys_cursor_type
25226 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25227 && new_cursor_width != w->phys_cursor_width)))
25228 erase_phys_cursor (w);
25230 /* Don't check phys_cursor_on_p here because that flag is only set
25231 to zero in some cases where we know that the cursor has been
25232 completely erased, to avoid the extra work of erasing the cursor
25233 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25234 still not be visible, or it has only been partly erased. */
25235 if (on)
25237 w->phys_cursor_ascent = glyph_row->ascent;
25238 w->phys_cursor_height = glyph_row->height;
25240 /* Set phys_cursor_.* before x_draw_.* is called because some
25241 of them may need the information. */
25242 w->phys_cursor.x = x;
25243 w->phys_cursor.y = glyph_row->y;
25244 w->phys_cursor.hpos = hpos;
25245 w->phys_cursor.vpos = vpos;
25248 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25249 new_cursor_type, new_cursor_width,
25250 on, active_cursor);
25254 /* Switch the display of W's cursor on or off, according to the value
25255 of ON. */
25257 static void
25258 update_window_cursor (struct window *w, int on)
25260 /* Don't update cursor in windows whose frame is in the process
25261 of being deleted. */
25262 if (w->current_matrix)
25264 BLOCK_INPUT;
25265 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
25266 w->phys_cursor.x, w->phys_cursor.y);
25267 UNBLOCK_INPUT;
25272 /* Call update_window_cursor with parameter ON_P on all leaf windows
25273 in the window tree rooted at W. */
25275 static void
25276 update_cursor_in_window_tree (struct window *w, int on_p)
25278 while (w)
25280 if (!NILP (w->hchild))
25281 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25282 else if (!NILP (w->vchild))
25283 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25284 else
25285 update_window_cursor (w, on_p);
25287 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25292 /* EXPORT:
25293 Display the cursor on window W, or clear it, according to ON_P.
25294 Don't change the cursor's position. */
25296 void
25297 x_update_cursor (struct frame *f, int on_p)
25299 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25303 /* EXPORT:
25304 Clear the cursor of window W to background color, and mark the
25305 cursor as not shown. This is used when the text where the cursor
25306 is about to be rewritten. */
25308 void
25309 x_clear_cursor (struct window *w)
25311 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25312 update_window_cursor (w, 0);
25315 #endif /* HAVE_WINDOW_SYSTEM */
25317 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25318 and MSDOS. */
25319 static void
25320 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25321 int start_hpos, int end_hpos,
25322 enum draw_glyphs_face draw)
25324 #ifdef HAVE_WINDOW_SYSTEM
25325 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25327 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25328 return;
25330 #endif
25331 #if defined (HAVE_GPM) || defined (MSDOS)
25332 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25333 #endif
25336 /* Display the active region described by mouse_face_* according to DRAW. */
25338 static void
25339 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25341 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25342 struct frame *f = XFRAME (WINDOW_FRAME (w));
25344 if (/* If window is in the process of being destroyed, don't bother
25345 to do anything. */
25346 w->current_matrix != NULL
25347 /* Don't update mouse highlight if hidden */
25348 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25349 /* Recognize when we are called to operate on rows that don't exist
25350 anymore. This can happen when a window is split. */
25351 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25353 int phys_cursor_on_p = w->phys_cursor_on_p;
25354 struct glyph_row *row, *first, *last;
25356 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25357 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25359 for (row = first; row <= last && row->enabled_p; ++row)
25361 int start_hpos, end_hpos, start_x;
25363 /* For all but the first row, the highlight starts at column 0. */
25364 if (row == first)
25366 /* R2L rows have BEG and END in reversed order, but the
25367 screen drawing geometry is always left to right. So
25368 we need to mirror the beginning and end of the
25369 highlighted area in R2L rows. */
25370 if (!row->reversed_p)
25372 start_hpos = hlinfo->mouse_face_beg_col;
25373 start_x = hlinfo->mouse_face_beg_x;
25375 else if (row == last)
25377 start_hpos = hlinfo->mouse_face_end_col;
25378 start_x = hlinfo->mouse_face_end_x;
25380 else
25382 start_hpos = 0;
25383 start_x = 0;
25386 else if (row->reversed_p && row == last)
25388 start_hpos = hlinfo->mouse_face_end_col;
25389 start_x = hlinfo->mouse_face_end_x;
25391 else
25393 start_hpos = 0;
25394 start_x = 0;
25397 if (row == last)
25399 if (!row->reversed_p)
25400 end_hpos = hlinfo->mouse_face_end_col;
25401 else if (row == first)
25402 end_hpos = hlinfo->mouse_face_beg_col;
25403 else
25405 end_hpos = row->used[TEXT_AREA];
25406 if (draw == DRAW_NORMAL_TEXT)
25407 row->fill_line_p = 1; /* Clear to end of line */
25410 else if (row->reversed_p && row == first)
25411 end_hpos = hlinfo->mouse_face_beg_col;
25412 else
25414 end_hpos = row->used[TEXT_AREA];
25415 if (draw == DRAW_NORMAL_TEXT)
25416 row->fill_line_p = 1; /* Clear to end of line */
25419 if (end_hpos > start_hpos)
25421 draw_row_with_mouse_face (w, start_x, row,
25422 start_hpos, end_hpos, draw);
25424 row->mouse_face_p
25425 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25429 #ifdef HAVE_WINDOW_SYSTEM
25430 /* When we've written over the cursor, arrange for it to
25431 be displayed again. */
25432 if (FRAME_WINDOW_P (f)
25433 && phys_cursor_on_p && !w->phys_cursor_on_p)
25435 BLOCK_INPUT;
25436 display_and_set_cursor (w, 1,
25437 w->phys_cursor.hpos, w->phys_cursor.vpos,
25438 w->phys_cursor.x, w->phys_cursor.y);
25439 UNBLOCK_INPUT;
25441 #endif /* HAVE_WINDOW_SYSTEM */
25444 #ifdef HAVE_WINDOW_SYSTEM
25445 /* Change the mouse cursor. */
25446 if (FRAME_WINDOW_P (f))
25448 if (draw == DRAW_NORMAL_TEXT
25449 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25450 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25451 else if (draw == DRAW_MOUSE_FACE)
25452 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25453 else
25454 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25456 #endif /* HAVE_WINDOW_SYSTEM */
25459 /* EXPORT:
25460 Clear out the mouse-highlighted active region.
25461 Redraw it un-highlighted first. Value is non-zero if mouse
25462 face was actually drawn unhighlighted. */
25465 clear_mouse_face (Mouse_HLInfo *hlinfo)
25467 int cleared = 0;
25469 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25471 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25472 cleared = 1;
25475 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25476 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25477 hlinfo->mouse_face_window = Qnil;
25478 hlinfo->mouse_face_overlay = Qnil;
25479 return cleared;
25482 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25483 within the mouse face on that window. */
25484 static int
25485 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25487 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25489 /* Quickly resolve the easy cases. */
25490 if (!(WINDOWP (hlinfo->mouse_face_window)
25491 && XWINDOW (hlinfo->mouse_face_window) == w))
25492 return 0;
25493 if (vpos < hlinfo->mouse_face_beg_row
25494 || vpos > hlinfo->mouse_face_end_row)
25495 return 0;
25496 if (vpos > hlinfo->mouse_face_beg_row
25497 && vpos < hlinfo->mouse_face_end_row)
25498 return 1;
25500 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25502 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25504 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25505 return 1;
25507 else if ((vpos == hlinfo->mouse_face_beg_row
25508 && hpos >= hlinfo->mouse_face_beg_col)
25509 || (vpos == hlinfo->mouse_face_end_row
25510 && hpos < hlinfo->mouse_face_end_col))
25511 return 1;
25513 else
25515 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25517 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25518 return 1;
25520 else if ((vpos == hlinfo->mouse_face_beg_row
25521 && hpos <= hlinfo->mouse_face_beg_col)
25522 || (vpos == hlinfo->mouse_face_end_row
25523 && hpos > hlinfo->mouse_face_end_col))
25524 return 1;
25526 return 0;
25530 /* EXPORT:
25531 Non-zero if physical cursor of window W is within mouse face. */
25534 cursor_in_mouse_face_p (struct window *w)
25536 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
25541 /* Find the glyph rows START_ROW and END_ROW of window W that display
25542 characters between buffer positions START_CHARPOS and END_CHARPOS
25543 (excluding END_CHARPOS). This is similar to row_containing_pos,
25544 but is more accurate when bidi reordering makes buffer positions
25545 change non-linearly with glyph rows. */
25546 static void
25547 rows_from_pos_range (struct window *w,
25548 EMACS_INT start_charpos, EMACS_INT end_charpos,
25549 struct glyph_row **start, struct glyph_row **end)
25551 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25552 int last_y = window_text_bottom_y (w);
25553 struct glyph_row *row;
25555 *start = NULL;
25556 *end = NULL;
25558 while (!first->enabled_p
25559 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25560 first++;
25562 /* Find the START row. */
25563 for (row = first;
25564 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25565 row++)
25567 /* A row can potentially be the START row if the range of the
25568 characters it displays intersects the range
25569 [START_CHARPOS..END_CHARPOS). */
25570 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25571 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25572 /* See the commentary in row_containing_pos, for the
25573 explanation of the complicated way to check whether
25574 some position is beyond the end of the characters
25575 displayed by a row. */
25576 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25577 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25578 && !row->ends_at_zv_p
25579 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25580 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25581 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25582 && !row->ends_at_zv_p
25583 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25585 /* Found a candidate row. Now make sure at least one of the
25586 glyphs it displays has a charpos from the range
25587 [START_CHARPOS..END_CHARPOS).
25589 This is not obvious because bidi reordering could make
25590 buffer positions of a row be 1,2,3,102,101,100, and if we
25591 want to highlight characters in [50..60), we don't want
25592 this row, even though [50..60) does intersect [1..103),
25593 the range of character positions given by the row's start
25594 and end positions. */
25595 struct glyph *g = row->glyphs[TEXT_AREA];
25596 struct glyph *e = g + row->used[TEXT_AREA];
25598 while (g < e)
25600 if ((BUFFERP (g->object) || INTEGERP (g->object))
25601 && start_charpos <= g->charpos && g->charpos < end_charpos)
25602 *start = row;
25603 g++;
25605 if (*start)
25606 break;
25610 /* Find the END row. */
25611 if (!*start
25612 /* If the last row is partially visible, start looking for END
25613 from that row, instead of starting from FIRST. */
25614 && !(row->enabled_p
25615 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25616 row = first;
25617 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25619 struct glyph_row *next = row + 1;
25621 if (!next->enabled_p
25622 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25623 /* The first row >= START whose range of displayed characters
25624 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25625 is the row END + 1. */
25626 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25627 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25628 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25629 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25630 && !next->ends_at_zv_p
25631 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25632 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25633 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25634 && !next->ends_at_zv_p
25635 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25637 *end = row;
25638 break;
25640 else
25642 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25643 but none of the characters it displays are in the range, it is
25644 also END + 1. */
25645 struct glyph *g = next->glyphs[TEXT_AREA];
25646 struct glyph *e = g + next->used[TEXT_AREA];
25648 while (g < e)
25650 if ((BUFFERP (g->object) || INTEGERP (g->object))
25651 && start_charpos <= g->charpos && g->charpos < end_charpos)
25652 break;
25653 g++;
25655 if (g == e)
25657 *end = row;
25658 break;
25664 /* This function sets the mouse_face_* elements of HLINFO, assuming
25665 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25666 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25667 for the overlay or run of text properties specifying the mouse
25668 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25669 before-string and after-string that must also be highlighted.
25670 COVER_STRING, if non-nil, is a display string that may cover some
25671 or all of the highlighted text. */
25673 static void
25674 mouse_face_from_buffer_pos (Lisp_Object window,
25675 Mouse_HLInfo *hlinfo,
25676 EMACS_INT mouse_charpos,
25677 EMACS_INT start_charpos,
25678 EMACS_INT end_charpos,
25679 Lisp_Object before_string,
25680 Lisp_Object after_string,
25681 Lisp_Object cover_string)
25683 struct window *w = XWINDOW (window);
25684 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25685 struct glyph_row *r1, *r2;
25686 struct glyph *glyph, *end;
25687 EMACS_INT ignore, pos;
25688 int x;
25690 xassert (NILP (cover_string) || STRINGP (cover_string));
25691 xassert (NILP (before_string) || STRINGP (before_string));
25692 xassert (NILP (after_string) || STRINGP (after_string));
25694 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25695 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25696 if (r1 == NULL)
25697 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25698 /* If the before-string or display-string contains newlines,
25699 rows_from_pos_range skips to its last row. Move back. */
25700 if (!NILP (before_string) || !NILP (cover_string))
25702 struct glyph_row *prev;
25703 while ((prev = r1 - 1, prev >= first)
25704 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25705 && prev->used[TEXT_AREA] > 0)
25707 struct glyph *beg = prev->glyphs[TEXT_AREA];
25708 glyph = beg + prev->used[TEXT_AREA];
25709 while (--glyph >= beg && INTEGERP (glyph->object));
25710 if (glyph < beg
25711 || !(EQ (glyph->object, before_string)
25712 || EQ (glyph->object, cover_string)))
25713 break;
25714 r1 = prev;
25717 if (r2 == NULL)
25719 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25720 hlinfo->mouse_face_past_end = 1;
25722 else if (!NILP (after_string))
25724 /* If the after-string has newlines, advance to its last row. */
25725 struct glyph_row *next;
25726 struct glyph_row *last
25727 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25729 for (next = r2 + 1;
25730 next <= last
25731 && next->used[TEXT_AREA] > 0
25732 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25733 ++next)
25734 r2 = next;
25736 /* The rest of the display engine assumes that mouse_face_beg_row is
25737 either above below mouse_face_end_row or identical to it. But
25738 with bidi-reordered continued lines, the row for START_CHARPOS
25739 could be below the row for END_CHARPOS. If so, swap the rows and
25740 store them in correct order. */
25741 if (r1->y > r2->y)
25743 struct glyph_row *tem = r2;
25745 r2 = r1;
25746 r1 = tem;
25749 hlinfo->mouse_face_beg_y = r1->y;
25750 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25751 hlinfo->mouse_face_end_y = r2->y;
25752 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25754 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25755 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25756 could be anywhere in the row and in any order. The strategy
25757 below is to find the leftmost and the rightmost glyph that
25758 belongs to either of these 3 strings, or whose position is
25759 between START_CHARPOS and END_CHARPOS, and highlight all the
25760 glyphs between those two. This may cover more than just the text
25761 between START_CHARPOS and END_CHARPOS if the range of characters
25762 strides the bidi level boundary, e.g. if the beginning is in R2L
25763 text while the end is in L2R text or vice versa. */
25764 if (!r1->reversed_p)
25766 /* This row is in a left to right paragraph. Scan it left to
25767 right. */
25768 glyph = r1->glyphs[TEXT_AREA];
25769 end = glyph + r1->used[TEXT_AREA];
25770 x = r1->x;
25772 /* Skip truncation glyphs at the start of the glyph row. */
25773 if (r1->displays_text_p)
25774 for (; glyph < end
25775 && INTEGERP (glyph->object)
25776 && glyph->charpos < 0;
25777 ++glyph)
25778 x += glyph->pixel_width;
25780 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25781 or COVER_STRING, and the first glyph from buffer whose
25782 position is between START_CHARPOS and END_CHARPOS. */
25783 for (; glyph < end
25784 && !INTEGERP (glyph->object)
25785 && !EQ (glyph->object, cover_string)
25786 && !(BUFFERP (glyph->object)
25787 && (glyph->charpos >= start_charpos
25788 && glyph->charpos < end_charpos));
25789 ++glyph)
25791 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25792 are present at buffer positions between START_CHARPOS and
25793 END_CHARPOS, or if they come from an overlay. */
25794 if (EQ (glyph->object, before_string))
25796 pos = string_buffer_position (before_string,
25797 start_charpos);
25798 /* If pos == 0, it means before_string came from an
25799 overlay, not from a buffer position. */
25800 if (!pos || (pos >= start_charpos && pos < end_charpos))
25801 break;
25803 else if (EQ (glyph->object, after_string))
25805 pos = string_buffer_position (after_string, end_charpos);
25806 if (!pos || (pos >= start_charpos && pos < end_charpos))
25807 break;
25809 x += glyph->pixel_width;
25811 hlinfo->mouse_face_beg_x = x;
25812 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25814 else
25816 /* This row is in a right to left paragraph. Scan it right to
25817 left. */
25818 struct glyph *g;
25820 end = r1->glyphs[TEXT_AREA] - 1;
25821 glyph = end + r1->used[TEXT_AREA];
25823 /* Skip truncation glyphs at the start of the glyph row. */
25824 if (r1->displays_text_p)
25825 for (; glyph > end
25826 && INTEGERP (glyph->object)
25827 && glyph->charpos < 0;
25828 --glyph)
25831 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25832 or COVER_STRING, and the first glyph from buffer whose
25833 position is between START_CHARPOS and END_CHARPOS. */
25834 for (; glyph > end
25835 && !INTEGERP (glyph->object)
25836 && !EQ (glyph->object, cover_string)
25837 && !(BUFFERP (glyph->object)
25838 && (glyph->charpos >= start_charpos
25839 && glyph->charpos < end_charpos));
25840 --glyph)
25842 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25843 are present at buffer positions between START_CHARPOS and
25844 END_CHARPOS, or if they come from an overlay. */
25845 if (EQ (glyph->object, before_string))
25847 pos = string_buffer_position (before_string, start_charpos);
25848 /* If pos == 0, it means before_string came from an
25849 overlay, not from a buffer position. */
25850 if (!pos || (pos >= start_charpos && pos < end_charpos))
25851 break;
25853 else if (EQ (glyph->object, after_string))
25855 pos = string_buffer_position (after_string, end_charpos);
25856 if (!pos || (pos >= start_charpos && pos < end_charpos))
25857 break;
25861 glyph++; /* first glyph to the right of the highlighted area */
25862 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25863 x += g->pixel_width;
25864 hlinfo->mouse_face_beg_x = x;
25865 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25868 /* If the highlight ends in a different row, compute GLYPH and END
25869 for the end row. Otherwise, reuse the values computed above for
25870 the row where the highlight begins. */
25871 if (r2 != r1)
25873 if (!r2->reversed_p)
25875 glyph = r2->glyphs[TEXT_AREA];
25876 end = glyph + r2->used[TEXT_AREA];
25877 x = r2->x;
25879 else
25881 end = r2->glyphs[TEXT_AREA] - 1;
25882 glyph = end + r2->used[TEXT_AREA];
25886 if (!r2->reversed_p)
25888 /* Skip truncation and continuation glyphs near the end of the
25889 row, and also blanks and stretch glyphs inserted by
25890 extend_face_to_end_of_line. */
25891 while (end > glyph
25892 && INTEGERP ((end - 1)->object)
25893 && (end - 1)->charpos <= 0)
25894 --end;
25895 /* Scan the rest of the glyph row from the end, looking for the
25896 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25897 COVER_STRING, or whose position is between START_CHARPOS
25898 and END_CHARPOS */
25899 for (--end;
25900 end > glyph
25901 && !INTEGERP (end->object)
25902 && !EQ (end->object, cover_string)
25903 && !(BUFFERP (end->object)
25904 && (end->charpos >= start_charpos
25905 && end->charpos < end_charpos));
25906 --end)
25908 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25909 are present at buffer positions between START_CHARPOS and
25910 END_CHARPOS, or if they come from an overlay. */
25911 if (EQ (end->object, before_string))
25913 pos = string_buffer_position (before_string, start_charpos);
25914 if (!pos || (pos >= start_charpos && pos < end_charpos))
25915 break;
25917 else if (EQ (end->object, after_string))
25919 pos = string_buffer_position (after_string, end_charpos);
25920 if (!pos || (pos >= start_charpos && pos < end_charpos))
25921 break;
25924 /* Find the X coordinate of the last glyph to be highlighted. */
25925 for (; glyph <= end; ++glyph)
25926 x += glyph->pixel_width;
25928 hlinfo->mouse_face_end_x = x;
25929 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25931 else
25933 /* Skip truncation and continuation glyphs near the end of the
25934 row, and also blanks and stretch glyphs inserted by
25935 extend_face_to_end_of_line. */
25936 x = r2->x;
25937 end++;
25938 while (end < glyph
25939 && INTEGERP (end->object)
25940 && end->charpos <= 0)
25942 x += end->pixel_width;
25943 ++end;
25945 /* Scan the rest of the glyph row from the end, looking for the
25946 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25947 COVER_STRING, or whose position is between START_CHARPOS
25948 and END_CHARPOS */
25949 for ( ;
25950 end < glyph
25951 && !INTEGERP (end->object)
25952 && !EQ (end->object, cover_string)
25953 && !(BUFFERP (end->object)
25954 && (end->charpos >= start_charpos
25955 && end->charpos < end_charpos));
25956 ++end)
25958 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25959 are present at buffer positions between START_CHARPOS and
25960 END_CHARPOS, or if they come from an overlay. */
25961 if (EQ (end->object, before_string))
25963 pos = string_buffer_position (before_string, start_charpos);
25964 if (!pos || (pos >= start_charpos && pos < end_charpos))
25965 break;
25967 else if (EQ (end->object, after_string))
25969 pos = string_buffer_position (after_string, end_charpos);
25970 if (!pos || (pos >= start_charpos && pos < end_charpos))
25971 break;
25973 x += end->pixel_width;
25975 hlinfo->mouse_face_end_x = x;
25976 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25979 hlinfo->mouse_face_window = window;
25980 hlinfo->mouse_face_face_id
25981 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25982 mouse_charpos + 1,
25983 !hlinfo->mouse_face_hidden, -1);
25984 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25987 /* The following function is not used anymore (replaced with
25988 mouse_face_from_string_pos), but I leave it here for the time
25989 being, in case someone would. */
25991 #if 0 /* not used */
25993 /* Find the position of the glyph for position POS in OBJECT in
25994 window W's current matrix, and return in *X, *Y the pixel
25995 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25997 RIGHT_P non-zero means return the position of the right edge of the
25998 glyph, RIGHT_P zero means return the left edge position.
26000 If no glyph for POS exists in the matrix, return the position of
26001 the glyph with the next smaller position that is in the matrix, if
26002 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26003 exists in the matrix, return the position of the glyph with the
26004 next larger position in OBJECT.
26006 Value is non-zero if a glyph was found. */
26008 static int
26009 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26010 int *hpos, int *vpos, int *x, int *y, int right_p)
26012 int yb = window_text_bottom_y (w);
26013 struct glyph_row *r;
26014 struct glyph *best_glyph = NULL;
26015 struct glyph_row *best_row = NULL;
26016 int best_x = 0;
26018 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26019 r->enabled_p && r->y < yb;
26020 ++r)
26022 struct glyph *g = r->glyphs[TEXT_AREA];
26023 struct glyph *e = g + r->used[TEXT_AREA];
26024 int gx;
26026 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26027 if (EQ (g->object, object))
26029 if (g->charpos == pos)
26031 best_glyph = g;
26032 best_x = gx;
26033 best_row = r;
26034 goto found;
26036 else if (best_glyph == NULL
26037 || ((eabs (g->charpos - pos)
26038 < eabs (best_glyph->charpos - pos))
26039 && (right_p
26040 ? g->charpos < pos
26041 : g->charpos > pos)))
26043 best_glyph = g;
26044 best_x = gx;
26045 best_row = r;
26050 found:
26052 if (best_glyph)
26054 *x = best_x;
26055 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26057 if (right_p)
26059 *x += best_glyph->pixel_width;
26060 ++*hpos;
26063 *y = best_row->y;
26064 *vpos = best_row - w->current_matrix->rows;
26067 return best_glyph != NULL;
26069 #endif /* not used */
26071 /* Find the positions of the first and the last glyphs in window W's
26072 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26073 (assumed to be a string), and return in HLINFO's mouse_face_*
26074 members the pixel and column/row coordinates of those glyphs. */
26076 static void
26077 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26078 Lisp_Object object,
26079 EMACS_INT startpos, EMACS_INT endpos)
26081 int yb = window_text_bottom_y (w);
26082 struct glyph_row *r;
26083 struct glyph *g, *e;
26084 int gx;
26085 int found = 0;
26087 /* Find the glyph row with at least one position in the range
26088 [STARTPOS..ENDPOS], and the first glyph in that row whose
26089 position belongs to that range. */
26090 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26091 r->enabled_p && r->y < yb;
26092 ++r)
26094 if (!r->reversed_p)
26096 g = r->glyphs[TEXT_AREA];
26097 e = g + r->used[TEXT_AREA];
26098 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26099 if (EQ (g->object, object)
26100 && startpos <= g->charpos && g->charpos <= endpos)
26102 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26103 hlinfo->mouse_face_beg_y = r->y;
26104 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26105 hlinfo->mouse_face_beg_x = gx;
26106 found = 1;
26107 break;
26110 else
26112 struct glyph *g1;
26114 e = r->glyphs[TEXT_AREA];
26115 g = e + r->used[TEXT_AREA];
26116 for ( ; g > e; --g)
26117 if (EQ ((g-1)->object, object)
26118 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26120 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26121 hlinfo->mouse_face_beg_y = r->y;
26122 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26123 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26124 gx += g1->pixel_width;
26125 hlinfo->mouse_face_beg_x = gx;
26126 found = 1;
26127 break;
26130 if (found)
26131 break;
26134 if (!found)
26135 return;
26137 /* Starting with the next row, look for the first row which does NOT
26138 include any glyphs whose positions are in the range. */
26139 for (++r; r->enabled_p && r->y < yb; ++r)
26141 g = r->glyphs[TEXT_AREA];
26142 e = g + r->used[TEXT_AREA];
26143 found = 0;
26144 for ( ; g < e; ++g)
26145 if (EQ (g->object, object)
26146 && startpos <= g->charpos && g->charpos <= endpos)
26148 found = 1;
26149 break;
26151 if (!found)
26152 break;
26155 /* The highlighted region ends on the previous row. */
26156 r--;
26158 /* Set the end row and its vertical pixel coordinate. */
26159 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26160 hlinfo->mouse_face_end_y = r->y;
26162 /* Compute and set the end column and the end column's horizontal
26163 pixel coordinate. */
26164 if (!r->reversed_p)
26166 g = r->glyphs[TEXT_AREA];
26167 e = g + r->used[TEXT_AREA];
26168 for ( ; e > g; --e)
26169 if (EQ ((e-1)->object, object)
26170 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26171 break;
26172 hlinfo->mouse_face_end_col = e - g;
26174 for (gx = r->x; g < e; ++g)
26175 gx += g->pixel_width;
26176 hlinfo->mouse_face_end_x = gx;
26178 else
26180 e = r->glyphs[TEXT_AREA];
26181 g = e + r->used[TEXT_AREA];
26182 for (gx = r->x ; e < g; ++e)
26184 if (EQ (e->object, object)
26185 && startpos <= e->charpos && e->charpos <= endpos)
26186 break;
26187 gx += e->pixel_width;
26189 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26190 hlinfo->mouse_face_end_x = gx;
26194 #ifdef HAVE_WINDOW_SYSTEM
26196 /* See if position X, Y is within a hot-spot of an image. */
26198 static int
26199 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26201 if (!CONSP (hot_spot))
26202 return 0;
26204 if (EQ (XCAR (hot_spot), Qrect))
26206 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26207 Lisp_Object rect = XCDR (hot_spot);
26208 Lisp_Object tem;
26209 if (!CONSP (rect))
26210 return 0;
26211 if (!CONSP (XCAR (rect)))
26212 return 0;
26213 if (!CONSP (XCDR (rect)))
26214 return 0;
26215 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26216 return 0;
26217 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26218 return 0;
26219 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26220 return 0;
26221 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26222 return 0;
26223 return 1;
26225 else if (EQ (XCAR (hot_spot), Qcircle))
26227 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26228 Lisp_Object circ = XCDR (hot_spot);
26229 Lisp_Object lr, lx0, ly0;
26230 if (CONSP (circ)
26231 && CONSP (XCAR (circ))
26232 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26233 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26234 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26236 double r = XFLOATINT (lr);
26237 double dx = XINT (lx0) - x;
26238 double dy = XINT (ly0) - y;
26239 return (dx * dx + dy * dy <= r * r);
26242 else if (EQ (XCAR (hot_spot), Qpoly))
26244 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26245 if (VECTORP (XCDR (hot_spot)))
26247 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26248 Lisp_Object *poly = v->contents;
26249 int n = v->header.size;
26250 int i;
26251 int inside = 0;
26252 Lisp_Object lx, ly;
26253 int x0, y0;
26255 /* Need an even number of coordinates, and at least 3 edges. */
26256 if (n < 6 || n & 1)
26257 return 0;
26259 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26260 If count is odd, we are inside polygon. Pixels on edges
26261 may or may not be included depending on actual geometry of the
26262 polygon. */
26263 if ((lx = poly[n-2], !INTEGERP (lx))
26264 || (ly = poly[n-1], !INTEGERP (lx)))
26265 return 0;
26266 x0 = XINT (lx), y0 = XINT (ly);
26267 for (i = 0; i < n; i += 2)
26269 int x1 = x0, y1 = y0;
26270 if ((lx = poly[i], !INTEGERP (lx))
26271 || (ly = poly[i+1], !INTEGERP (ly)))
26272 return 0;
26273 x0 = XINT (lx), y0 = XINT (ly);
26275 /* Does this segment cross the X line? */
26276 if (x0 >= x)
26278 if (x1 >= x)
26279 continue;
26281 else if (x1 < x)
26282 continue;
26283 if (y > y0 && y > y1)
26284 continue;
26285 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26286 inside = !inside;
26288 return inside;
26291 return 0;
26294 Lisp_Object
26295 find_hot_spot (Lisp_Object map, int x, int y)
26297 while (CONSP (map))
26299 if (CONSP (XCAR (map))
26300 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26301 return XCAR (map);
26302 map = XCDR (map);
26305 return Qnil;
26308 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26309 3, 3, 0,
26310 doc: /* Lookup in image map MAP coordinates X and Y.
26311 An image map is an alist where each element has the format (AREA ID PLIST).
26312 An AREA is specified as either a rectangle, a circle, or a polygon:
26313 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26314 pixel coordinates of the upper left and bottom right corners.
26315 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26316 and the radius of the circle; r may be a float or integer.
26317 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26318 vector describes one corner in the polygon.
26319 Returns the alist element for the first matching AREA in MAP. */)
26320 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26322 if (NILP (map))
26323 return Qnil;
26325 CHECK_NUMBER (x);
26326 CHECK_NUMBER (y);
26328 return find_hot_spot (map, XINT (x), XINT (y));
26332 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26333 static void
26334 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26336 /* Do not change cursor shape while dragging mouse. */
26337 if (!NILP (do_mouse_tracking))
26338 return;
26340 if (!NILP (pointer))
26342 if (EQ (pointer, Qarrow))
26343 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26344 else if (EQ (pointer, Qhand))
26345 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26346 else if (EQ (pointer, Qtext))
26347 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26348 else if (EQ (pointer, intern ("hdrag")))
26349 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26350 #ifdef HAVE_X_WINDOWS
26351 else if (EQ (pointer, intern ("vdrag")))
26352 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26353 #endif
26354 else if (EQ (pointer, intern ("hourglass")))
26355 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26356 else if (EQ (pointer, Qmodeline))
26357 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26358 else
26359 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26362 if (cursor != No_Cursor)
26363 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26366 #endif /* HAVE_WINDOW_SYSTEM */
26368 /* Take proper action when mouse has moved to the mode or header line
26369 or marginal area AREA of window W, x-position X and y-position Y.
26370 X is relative to the start of the text display area of W, so the
26371 width of bitmap areas and scroll bars must be subtracted to get a
26372 position relative to the start of the mode line. */
26374 static void
26375 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26376 enum window_part area)
26378 struct window *w = XWINDOW (window);
26379 struct frame *f = XFRAME (w->frame);
26380 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26381 #ifdef HAVE_WINDOW_SYSTEM
26382 Display_Info *dpyinfo;
26383 #endif
26384 Cursor cursor = No_Cursor;
26385 Lisp_Object pointer = Qnil;
26386 int dx, dy, width, height;
26387 EMACS_INT charpos;
26388 Lisp_Object string, object = Qnil;
26389 Lisp_Object pos, help;
26391 Lisp_Object mouse_face;
26392 int original_x_pixel = x;
26393 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26394 struct glyph_row *row;
26396 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26398 int x0;
26399 struct glyph *end;
26401 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26402 returns them in row/column units! */
26403 string = mode_line_string (w, area, &x, &y, &charpos,
26404 &object, &dx, &dy, &width, &height);
26406 row = (area == ON_MODE_LINE
26407 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26408 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26410 /* Find the glyph under the mouse pointer. */
26411 if (row->mode_line_p && row->enabled_p)
26413 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26414 end = glyph + row->used[TEXT_AREA];
26416 for (x0 = original_x_pixel;
26417 glyph < end && x0 >= glyph->pixel_width;
26418 ++glyph)
26419 x0 -= glyph->pixel_width;
26421 if (glyph >= end)
26422 glyph = NULL;
26425 else
26427 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26428 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26429 returns them in row/column units! */
26430 string = marginal_area_string (w, area, &x, &y, &charpos,
26431 &object, &dx, &dy, &width, &height);
26434 help = Qnil;
26436 #ifdef HAVE_WINDOW_SYSTEM
26437 if (IMAGEP (object))
26439 Lisp_Object image_map, hotspot;
26440 if ((image_map = Fplist_get (XCDR (object), QCmap),
26441 !NILP (image_map))
26442 && (hotspot = find_hot_spot (image_map, dx, dy),
26443 CONSP (hotspot))
26444 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26446 Lisp_Object plist;
26448 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26449 If so, we could look for mouse-enter, mouse-leave
26450 properties in PLIST (and do something...). */
26451 hotspot = XCDR (hotspot);
26452 if (CONSP (hotspot)
26453 && (plist = XCAR (hotspot), CONSP (plist)))
26455 pointer = Fplist_get (plist, Qpointer);
26456 if (NILP (pointer))
26457 pointer = Qhand;
26458 help = Fplist_get (plist, Qhelp_echo);
26459 if (!NILP (help))
26461 help_echo_string = help;
26462 /* Is this correct? ++kfs */
26463 XSETWINDOW (help_echo_window, w);
26464 help_echo_object = w->buffer;
26465 help_echo_pos = charpos;
26469 if (NILP (pointer))
26470 pointer = Fplist_get (XCDR (object), QCpointer);
26472 #endif /* HAVE_WINDOW_SYSTEM */
26474 if (STRINGP (string))
26476 pos = make_number (charpos);
26477 /* If we're on a string with `help-echo' text property, arrange
26478 for the help to be displayed. This is done by setting the
26479 global variable help_echo_string to the help string. */
26480 if (NILP (help))
26482 help = Fget_text_property (pos, Qhelp_echo, string);
26483 if (!NILP (help))
26485 help_echo_string = help;
26486 XSETWINDOW (help_echo_window, w);
26487 help_echo_object = string;
26488 help_echo_pos = charpos;
26492 #ifdef HAVE_WINDOW_SYSTEM
26493 if (FRAME_WINDOW_P (f))
26495 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26496 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26497 if (NILP (pointer))
26498 pointer = Fget_text_property (pos, Qpointer, string);
26500 /* Change the mouse pointer according to what is under X/Y. */
26501 if (NILP (pointer)
26502 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26504 Lisp_Object map;
26505 map = Fget_text_property (pos, Qlocal_map, string);
26506 if (!KEYMAPP (map))
26507 map = Fget_text_property (pos, Qkeymap, string);
26508 if (!KEYMAPP (map))
26509 cursor = dpyinfo->vertical_scroll_bar_cursor;
26512 #endif
26514 /* Change the mouse face according to what is under X/Y. */
26515 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26516 if (!NILP (mouse_face)
26517 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26518 && glyph)
26520 Lisp_Object b, e;
26522 struct glyph * tmp_glyph;
26524 int gpos;
26525 int gseq_length;
26526 int total_pixel_width;
26527 EMACS_INT begpos, endpos, ignore;
26529 int vpos, hpos;
26531 b = Fprevious_single_property_change (make_number (charpos + 1),
26532 Qmouse_face, string, Qnil);
26533 if (NILP (b))
26534 begpos = 0;
26535 else
26536 begpos = XINT (b);
26538 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26539 if (NILP (e))
26540 endpos = SCHARS (string);
26541 else
26542 endpos = XINT (e);
26544 /* Calculate the glyph position GPOS of GLYPH in the
26545 displayed string, relative to the beginning of the
26546 highlighted part of the string.
26548 Note: GPOS is different from CHARPOS. CHARPOS is the
26549 position of GLYPH in the internal string object. A mode
26550 line string format has structures which are converted to
26551 a flattened string by the Emacs Lisp interpreter. The
26552 internal string is an element of those structures. The
26553 displayed string is the flattened string. */
26554 tmp_glyph = row_start_glyph;
26555 while (tmp_glyph < glyph
26556 && (!(EQ (tmp_glyph->object, glyph->object)
26557 && begpos <= tmp_glyph->charpos
26558 && tmp_glyph->charpos < endpos)))
26559 tmp_glyph++;
26560 gpos = glyph - tmp_glyph;
26562 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26563 the highlighted part of the displayed string to which
26564 GLYPH belongs. Note: GSEQ_LENGTH is different from
26565 SCHARS (STRING), because the latter returns the length of
26566 the internal string. */
26567 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26568 tmp_glyph > glyph
26569 && (!(EQ (tmp_glyph->object, glyph->object)
26570 && begpos <= tmp_glyph->charpos
26571 && tmp_glyph->charpos < endpos));
26572 tmp_glyph--)
26574 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26576 /* Calculate the total pixel width of all the glyphs between
26577 the beginning of the highlighted area and GLYPH. */
26578 total_pixel_width = 0;
26579 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26580 total_pixel_width += tmp_glyph->pixel_width;
26582 /* Pre calculation of re-rendering position. Note: X is in
26583 column units here, after the call to mode_line_string or
26584 marginal_area_string. */
26585 hpos = x - gpos;
26586 vpos = (area == ON_MODE_LINE
26587 ? (w->current_matrix)->nrows - 1
26588 : 0);
26590 /* If GLYPH's position is included in the region that is
26591 already drawn in mouse face, we have nothing to do. */
26592 if ( EQ (window, hlinfo->mouse_face_window)
26593 && (!row->reversed_p
26594 ? (hlinfo->mouse_face_beg_col <= hpos
26595 && hpos < hlinfo->mouse_face_end_col)
26596 /* In R2L rows we swap BEG and END, see below. */
26597 : (hlinfo->mouse_face_end_col <= hpos
26598 && hpos < hlinfo->mouse_face_beg_col))
26599 && hlinfo->mouse_face_beg_row == vpos )
26600 return;
26602 if (clear_mouse_face (hlinfo))
26603 cursor = No_Cursor;
26605 if (!row->reversed_p)
26607 hlinfo->mouse_face_beg_col = hpos;
26608 hlinfo->mouse_face_beg_x = original_x_pixel
26609 - (total_pixel_width + dx);
26610 hlinfo->mouse_face_end_col = hpos + gseq_length;
26611 hlinfo->mouse_face_end_x = 0;
26613 else
26615 /* In R2L rows, show_mouse_face expects BEG and END
26616 coordinates to be swapped. */
26617 hlinfo->mouse_face_end_col = hpos;
26618 hlinfo->mouse_face_end_x = original_x_pixel
26619 - (total_pixel_width + dx);
26620 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26621 hlinfo->mouse_face_beg_x = 0;
26624 hlinfo->mouse_face_beg_row = vpos;
26625 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26626 hlinfo->mouse_face_beg_y = 0;
26627 hlinfo->mouse_face_end_y = 0;
26628 hlinfo->mouse_face_past_end = 0;
26629 hlinfo->mouse_face_window = window;
26631 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26632 charpos,
26633 0, 0, 0,
26634 &ignore,
26635 glyph->face_id,
26637 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26639 if (NILP (pointer))
26640 pointer = Qhand;
26642 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26643 clear_mouse_face (hlinfo);
26645 #ifdef HAVE_WINDOW_SYSTEM
26646 if (FRAME_WINDOW_P (f))
26647 define_frame_cursor1 (f, cursor, pointer);
26648 #endif
26652 /* EXPORT:
26653 Take proper action when the mouse has moved to position X, Y on
26654 frame F as regards highlighting characters that have mouse-face
26655 properties. Also de-highlighting chars where the mouse was before.
26656 X and Y can be negative or out of range. */
26658 void
26659 note_mouse_highlight (struct frame *f, int x, int y)
26661 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26662 enum window_part part;
26663 Lisp_Object window;
26664 struct window *w;
26665 Cursor cursor = No_Cursor;
26666 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26667 struct buffer *b;
26669 /* When a menu is active, don't highlight because this looks odd. */
26670 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26671 if (popup_activated ())
26672 return;
26673 #endif
26675 if (NILP (Vmouse_highlight)
26676 || !f->glyphs_initialized_p
26677 || f->pointer_invisible)
26678 return;
26680 hlinfo->mouse_face_mouse_x = x;
26681 hlinfo->mouse_face_mouse_y = y;
26682 hlinfo->mouse_face_mouse_frame = f;
26684 if (hlinfo->mouse_face_defer)
26685 return;
26687 if (gc_in_progress)
26689 hlinfo->mouse_face_deferred_gc = 1;
26690 return;
26693 /* Which window is that in? */
26694 window = window_from_coordinates (f, x, y, &part, 1);
26696 /* If we were displaying active text in another window, clear that.
26697 Also clear if we move out of text area in same window. */
26698 if (! EQ (window, hlinfo->mouse_face_window)
26699 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26700 && !NILP (hlinfo->mouse_face_window)))
26701 clear_mouse_face (hlinfo);
26703 /* Not on a window -> return. */
26704 if (!WINDOWP (window))
26705 return;
26707 /* Reset help_echo_string. It will get recomputed below. */
26708 help_echo_string = Qnil;
26710 /* Convert to window-relative pixel coordinates. */
26711 w = XWINDOW (window);
26712 frame_to_window_pixel_xy (w, &x, &y);
26714 #ifdef HAVE_WINDOW_SYSTEM
26715 /* Handle tool-bar window differently since it doesn't display a
26716 buffer. */
26717 if (EQ (window, f->tool_bar_window))
26719 note_tool_bar_highlight (f, x, y);
26720 return;
26722 #endif
26724 /* Mouse is on the mode, header line or margin? */
26725 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26726 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26728 note_mode_line_or_margin_highlight (window, x, y, part);
26729 return;
26732 #ifdef HAVE_WINDOW_SYSTEM
26733 if (part == ON_VERTICAL_BORDER)
26735 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26736 help_echo_string = build_string ("drag-mouse-1: resize");
26738 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26739 || part == ON_SCROLL_BAR)
26740 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26741 else
26742 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26743 #endif
26745 /* Are we in a window whose display is up to date?
26746 And verify the buffer's text has not changed. */
26747 b = XBUFFER (w->buffer);
26748 if (part == ON_TEXT
26749 && EQ (w->window_end_valid, w->buffer)
26750 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26751 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26753 int hpos, vpos, dx, dy, area;
26754 EMACS_INT pos;
26755 struct glyph *glyph;
26756 Lisp_Object object;
26757 Lisp_Object mouse_face = Qnil, position;
26758 Lisp_Object *overlay_vec = NULL;
26759 ptrdiff_t i, noverlays;
26760 struct buffer *obuf;
26761 EMACS_INT obegv, ozv;
26762 int same_region;
26764 /* Find the glyph under X/Y. */
26765 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26767 #ifdef HAVE_WINDOW_SYSTEM
26768 /* Look for :pointer property on image. */
26769 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26771 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26772 if (img != NULL && IMAGEP (img->spec))
26774 Lisp_Object image_map, hotspot;
26775 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26776 !NILP (image_map))
26777 && (hotspot = find_hot_spot (image_map,
26778 glyph->slice.img.x + dx,
26779 glyph->slice.img.y + dy),
26780 CONSP (hotspot))
26781 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26783 Lisp_Object plist;
26785 /* Could check XCAR (hotspot) to see if we enter/leave
26786 this hot-spot.
26787 If so, we could look for mouse-enter, mouse-leave
26788 properties in PLIST (and do something...). */
26789 hotspot = XCDR (hotspot);
26790 if (CONSP (hotspot)
26791 && (plist = XCAR (hotspot), CONSP (plist)))
26793 pointer = Fplist_get (plist, Qpointer);
26794 if (NILP (pointer))
26795 pointer = Qhand;
26796 help_echo_string = Fplist_get (plist, Qhelp_echo);
26797 if (!NILP (help_echo_string))
26799 help_echo_window = window;
26800 help_echo_object = glyph->object;
26801 help_echo_pos = glyph->charpos;
26805 if (NILP (pointer))
26806 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26809 #endif /* HAVE_WINDOW_SYSTEM */
26811 /* Clear mouse face if X/Y not over text. */
26812 if (glyph == NULL
26813 || area != TEXT_AREA
26814 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26815 /* Glyph's OBJECT is an integer for glyphs inserted by the
26816 display engine for its internal purposes, like truncation
26817 and continuation glyphs and blanks beyond the end of
26818 line's text on text terminals. If we are over such a
26819 glyph, we are not over any text. */
26820 || INTEGERP (glyph->object)
26821 /* R2L rows have a stretch glyph at their front, which
26822 stands for no text, whereas L2R rows have no glyphs at
26823 all beyond the end of text. Treat such stretch glyphs
26824 like we do with NULL glyphs in L2R rows. */
26825 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26826 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26827 && glyph->type == STRETCH_GLYPH
26828 && glyph->avoid_cursor_p))
26830 if (clear_mouse_face (hlinfo))
26831 cursor = No_Cursor;
26832 #ifdef HAVE_WINDOW_SYSTEM
26833 if (FRAME_WINDOW_P (f) && NILP (pointer))
26835 if (area != TEXT_AREA)
26836 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26837 else
26838 pointer = Vvoid_text_area_pointer;
26840 #endif
26841 goto set_cursor;
26844 pos = glyph->charpos;
26845 object = glyph->object;
26846 if (!STRINGP (object) && !BUFFERP (object))
26847 goto set_cursor;
26849 /* If we get an out-of-range value, return now; avoid an error. */
26850 if (BUFFERP (object) && pos > BUF_Z (b))
26851 goto set_cursor;
26853 /* Make the window's buffer temporarily current for
26854 overlays_at and compute_char_face. */
26855 obuf = current_buffer;
26856 current_buffer = b;
26857 obegv = BEGV;
26858 ozv = ZV;
26859 BEGV = BEG;
26860 ZV = Z;
26862 /* Is this char mouse-active or does it have help-echo? */
26863 position = make_number (pos);
26865 if (BUFFERP (object))
26867 /* Put all the overlays we want in a vector in overlay_vec. */
26868 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26869 /* Sort overlays into increasing priority order. */
26870 noverlays = sort_overlays (overlay_vec, noverlays, w);
26872 else
26873 noverlays = 0;
26875 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26877 if (same_region)
26878 cursor = No_Cursor;
26880 /* Check mouse-face highlighting. */
26881 if (! same_region
26882 /* If there exists an overlay with mouse-face overlapping
26883 the one we are currently highlighting, we have to
26884 check if we enter the overlapping overlay, and then
26885 highlight only that. */
26886 || (OVERLAYP (hlinfo->mouse_face_overlay)
26887 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26889 /* Find the highest priority overlay with a mouse-face. */
26890 Lisp_Object overlay = Qnil;
26891 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26893 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26894 if (!NILP (mouse_face))
26895 overlay = overlay_vec[i];
26898 /* If we're highlighting the same overlay as before, there's
26899 no need to do that again. */
26900 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26901 goto check_help_echo;
26902 hlinfo->mouse_face_overlay = overlay;
26904 /* Clear the display of the old active region, if any. */
26905 if (clear_mouse_face (hlinfo))
26906 cursor = No_Cursor;
26908 /* If no overlay applies, get a text property. */
26909 if (NILP (overlay))
26910 mouse_face = Fget_text_property (position, Qmouse_face, object);
26912 /* Next, compute the bounds of the mouse highlighting and
26913 display it. */
26914 if (!NILP (mouse_face) && STRINGP (object))
26916 /* The mouse-highlighting comes from a display string
26917 with a mouse-face. */
26918 Lisp_Object s, e;
26919 EMACS_INT ignore;
26921 s = Fprevious_single_property_change
26922 (make_number (pos + 1), Qmouse_face, object, Qnil);
26923 e = Fnext_single_property_change
26924 (position, Qmouse_face, object, Qnil);
26925 if (NILP (s))
26926 s = make_number (0);
26927 if (NILP (e))
26928 e = make_number (SCHARS (object) - 1);
26929 mouse_face_from_string_pos (w, hlinfo, object,
26930 XINT (s), XINT (e));
26931 hlinfo->mouse_face_past_end = 0;
26932 hlinfo->mouse_face_window = window;
26933 hlinfo->mouse_face_face_id
26934 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26935 glyph->face_id, 1);
26936 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26937 cursor = No_Cursor;
26939 else
26941 /* The mouse-highlighting, if any, comes from an overlay
26942 or text property in the buffer. */
26943 Lisp_Object buffer IF_LINT (= Qnil);
26944 Lisp_Object cover_string IF_LINT (= Qnil);
26946 if (STRINGP (object))
26948 /* If we are on a display string with no mouse-face,
26949 check if the text under it has one. */
26950 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26951 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26952 pos = string_buffer_position (object, start);
26953 if (pos > 0)
26955 mouse_face = get_char_property_and_overlay
26956 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26957 buffer = w->buffer;
26958 cover_string = object;
26961 else
26963 buffer = object;
26964 cover_string = Qnil;
26967 if (!NILP (mouse_face))
26969 Lisp_Object before, after;
26970 Lisp_Object before_string, after_string;
26971 /* To correctly find the limits of mouse highlight
26972 in a bidi-reordered buffer, we must not use the
26973 optimization of limiting the search in
26974 previous-single-property-change and
26975 next-single-property-change, because
26976 rows_from_pos_range needs the real start and end
26977 positions to DTRT in this case. That's because
26978 the first row visible in a window does not
26979 necessarily display the character whose position
26980 is the smallest. */
26981 Lisp_Object lim1 =
26982 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26983 ? Fmarker_position (w->start)
26984 : Qnil;
26985 Lisp_Object lim2 =
26986 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26987 ? make_number (BUF_Z (XBUFFER (buffer))
26988 - XFASTINT (w->window_end_pos))
26989 : Qnil;
26991 if (NILP (overlay))
26993 /* Handle the text property case. */
26994 before = Fprevious_single_property_change
26995 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26996 after = Fnext_single_property_change
26997 (make_number (pos), Qmouse_face, buffer, lim2);
26998 before_string = after_string = Qnil;
27000 else
27002 /* Handle the overlay case. */
27003 before = Foverlay_start (overlay);
27004 after = Foverlay_end (overlay);
27005 before_string = Foverlay_get (overlay, Qbefore_string);
27006 after_string = Foverlay_get (overlay, Qafter_string);
27008 if (!STRINGP (before_string)) before_string = Qnil;
27009 if (!STRINGP (after_string)) after_string = Qnil;
27012 mouse_face_from_buffer_pos (window, hlinfo, pos,
27013 XFASTINT (before),
27014 XFASTINT (after),
27015 before_string, after_string,
27016 cover_string);
27017 cursor = No_Cursor;
27022 check_help_echo:
27024 /* Look for a `help-echo' property. */
27025 if (NILP (help_echo_string)) {
27026 Lisp_Object help, overlay;
27028 /* Check overlays first. */
27029 help = overlay = Qnil;
27030 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27032 overlay = overlay_vec[i];
27033 help = Foverlay_get (overlay, Qhelp_echo);
27036 if (!NILP (help))
27038 help_echo_string = help;
27039 help_echo_window = window;
27040 help_echo_object = overlay;
27041 help_echo_pos = pos;
27043 else
27045 Lisp_Object obj = glyph->object;
27046 EMACS_INT charpos = glyph->charpos;
27048 /* Try text properties. */
27049 if (STRINGP (obj)
27050 && charpos >= 0
27051 && charpos < SCHARS (obj))
27053 help = Fget_text_property (make_number (charpos),
27054 Qhelp_echo, obj);
27055 if (NILP (help))
27057 /* If the string itself doesn't specify a help-echo,
27058 see if the buffer text ``under'' it does. */
27059 struct glyph_row *r
27060 = MATRIX_ROW (w->current_matrix, vpos);
27061 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27062 EMACS_INT p = string_buffer_position (obj, start);
27063 if (p > 0)
27065 help = Fget_char_property (make_number (p),
27066 Qhelp_echo, w->buffer);
27067 if (!NILP (help))
27069 charpos = p;
27070 obj = w->buffer;
27075 else if (BUFFERP (obj)
27076 && charpos >= BEGV
27077 && charpos < ZV)
27078 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27079 obj);
27081 if (!NILP (help))
27083 help_echo_string = help;
27084 help_echo_window = window;
27085 help_echo_object = obj;
27086 help_echo_pos = charpos;
27091 #ifdef HAVE_WINDOW_SYSTEM
27092 /* Look for a `pointer' property. */
27093 if (FRAME_WINDOW_P (f) && NILP (pointer))
27095 /* Check overlays first. */
27096 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27097 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27099 if (NILP (pointer))
27101 Lisp_Object obj = glyph->object;
27102 EMACS_INT charpos = glyph->charpos;
27104 /* Try text properties. */
27105 if (STRINGP (obj)
27106 && charpos >= 0
27107 && charpos < SCHARS (obj))
27109 pointer = Fget_text_property (make_number (charpos),
27110 Qpointer, obj);
27111 if (NILP (pointer))
27113 /* If the string itself doesn't specify a pointer,
27114 see if the buffer text ``under'' it does. */
27115 struct glyph_row *r
27116 = MATRIX_ROW (w->current_matrix, vpos);
27117 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27118 EMACS_INT p = string_buffer_position (obj, start);
27119 if (p > 0)
27120 pointer = Fget_char_property (make_number (p),
27121 Qpointer, w->buffer);
27124 else if (BUFFERP (obj)
27125 && charpos >= BEGV
27126 && charpos < ZV)
27127 pointer = Fget_text_property (make_number (charpos),
27128 Qpointer, obj);
27131 #endif /* HAVE_WINDOW_SYSTEM */
27133 BEGV = obegv;
27134 ZV = ozv;
27135 current_buffer = obuf;
27138 set_cursor:
27140 #ifdef HAVE_WINDOW_SYSTEM
27141 if (FRAME_WINDOW_P (f))
27142 define_frame_cursor1 (f, cursor, pointer);
27143 #else
27144 /* This is here to prevent a compiler error, about "label at end of
27145 compound statement". */
27146 return;
27147 #endif
27151 /* EXPORT for RIF:
27152 Clear any mouse-face on window W. This function is part of the
27153 redisplay interface, and is called from try_window_id and similar
27154 functions to ensure the mouse-highlight is off. */
27156 void
27157 x_clear_window_mouse_face (struct window *w)
27159 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27160 Lisp_Object window;
27162 BLOCK_INPUT;
27163 XSETWINDOW (window, w);
27164 if (EQ (window, hlinfo->mouse_face_window))
27165 clear_mouse_face (hlinfo);
27166 UNBLOCK_INPUT;
27170 /* EXPORT:
27171 Just discard the mouse face information for frame F, if any.
27172 This is used when the size of F is changed. */
27174 void
27175 cancel_mouse_face (struct frame *f)
27177 Lisp_Object window;
27178 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27180 window = hlinfo->mouse_face_window;
27181 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27183 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27184 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27185 hlinfo->mouse_face_window = Qnil;
27191 /***********************************************************************
27192 Exposure Events
27193 ***********************************************************************/
27195 #ifdef HAVE_WINDOW_SYSTEM
27197 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27198 which intersects rectangle R. R is in window-relative coordinates. */
27200 static void
27201 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27202 enum glyph_row_area area)
27204 struct glyph *first = row->glyphs[area];
27205 struct glyph *end = row->glyphs[area] + row->used[area];
27206 struct glyph *last;
27207 int first_x, start_x, x;
27209 if (area == TEXT_AREA && row->fill_line_p)
27210 /* If row extends face to end of line write the whole line. */
27211 draw_glyphs (w, 0, row, area,
27212 0, row->used[area],
27213 DRAW_NORMAL_TEXT, 0);
27214 else
27216 /* Set START_X to the window-relative start position for drawing glyphs of
27217 AREA. The first glyph of the text area can be partially visible.
27218 The first glyphs of other areas cannot. */
27219 start_x = window_box_left_offset (w, area);
27220 x = start_x;
27221 if (area == TEXT_AREA)
27222 x += row->x;
27224 /* Find the first glyph that must be redrawn. */
27225 while (first < end
27226 && x + first->pixel_width < r->x)
27228 x += first->pixel_width;
27229 ++first;
27232 /* Find the last one. */
27233 last = first;
27234 first_x = x;
27235 while (last < end
27236 && x < r->x + r->width)
27238 x += last->pixel_width;
27239 ++last;
27242 /* Repaint. */
27243 if (last > first)
27244 draw_glyphs (w, first_x - start_x, row, area,
27245 first - row->glyphs[area], last - row->glyphs[area],
27246 DRAW_NORMAL_TEXT, 0);
27251 /* Redraw the parts of the glyph row ROW on window W intersecting
27252 rectangle R. R is in window-relative coordinates. Value is
27253 non-zero if mouse-face was overwritten. */
27255 static int
27256 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27258 xassert (row->enabled_p);
27260 if (row->mode_line_p || w->pseudo_window_p)
27261 draw_glyphs (w, 0, row, TEXT_AREA,
27262 0, row->used[TEXT_AREA],
27263 DRAW_NORMAL_TEXT, 0);
27264 else
27266 if (row->used[LEFT_MARGIN_AREA])
27267 expose_area (w, row, r, LEFT_MARGIN_AREA);
27268 if (row->used[TEXT_AREA])
27269 expose_area (w, row, r, TEXT_AREA);
27270 if (row->used[RIGHT_MARGIN_AREA])
27271 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27272 draw_row_fringe_bitmaps (w, row);
27275 return row->mouse_face_p;
27279 /* Redraw those parts of glyphs rows during expose event handling that
27280 overlap other rows. Redrawing of an exposed line writes over parts
27281 of lines overlapping that exposed line; this function fixes that.
27283 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27284 row in W's current matrix that is exposed and overlaps other rows.
27285 LAST_OVERLAPPING_ROW is the last such row. */
27287 static void
27288 expose_overlaps (struct window *w,
27289 struct glyph_row *first_overlapping_row,
27290 struct glyph_row *last_overlapping_row,
27291 XRectangle *r)
27293 struct glyph_row *row;
27295 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27296 if (row->overlapping_p)
27298 xassert (row->enabled_p && !row->mode_line_p);
27300 row->clip = r;
27301 if (row->used[LEFT_MARGIN_AREA])
27302 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27304 if (row->used[TEXT_AREA])
27305 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27307 if (row->used[RIGHT_MARGIN_AREA])
27308 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27309 row->clip = NULL;
27314 /* Return non-zero if W's cursor intersects rectangle R. */
27316 static int
27317 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27319 XRectangle cr, result;
27320 struct glyph *cursor_glyph;
27321 struct glyph_row *row;
27323 if (w->phys_cursor.vpos >= 0
27324 && w->phys_cursor.vpos < w->current_matrix->nrows
27325 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27326 row->enabled_p)
27327 && row->cursor_in_fringe_p)
27329 /* Cursor is in the fringe. */
27330 cr.x = window_box_right_offset (w,
27331 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27332 ? RIGHT_MARGIN_AREA
27333 : TEXT_AREA));
27334 cr.y = row->y;
27335 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27336 cr.height = row->height;
27337 return x_intersect_rectangles (&cr, r, &result);
27340 cursor_glyph = get_phys_cursor_glyph (w);
27341 if (cursor_glyph)
27343 /* r is relative to W's box, but w->phys_cursor.x is relative
27344 to left edge of W's TEXT area. Adjust it. */
27345 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27346 cr.y = w->phys_cursor.y;
27347 cr.width = cursor_glyph->pixel_width;
27348 cr.height = w->phys_cursor_height;
27349 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27350 I assume the effect is the same -- and this is portable. */
27351 return x_intersect_rectangles (&cr, r, &result);
27353 /* If we don't understand the format, pretend we're not in the hot-spot. */
27354 return 0;
27358 /* EXPORT:
27359 Draw a vertical window border to the right of window W if W doesn't
27360 have vertical scroll bars. */
27362 void
27363 x_draw_vertical_border (struct window *w)
27365 struct frame *f = XFRAME (WINDOW_FRAME (w));
27367 /* We could do better, if we knew what type of scroll-bar the adjacent
27368 windows (on either side) have... But we don't :-(
27369 However, I think this works ok. ++KFS 2003-04-25 */
27371 /* Redraw borders between horizontally adjacent windows. Don't
27372 do it for frames with vertical scroll bars because either the
27373 right scroll bar of a window, or the left scroll bar of its
27374 neighbor will suffice as a border. */
27375 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27376 return;
27378 if (!WINDOW_RIGHTMOST_P (w)
27379 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27381 int x0, x1, y0, y1;
27383 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27384 y1 -= 1;
27386 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27387 x1 -= 1;
27389 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27391 else if (!WINDOW_LEFTMOST_P (w)
27392 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27394 int x0, x1, y0, y1;
27396 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27397 y1 -= 1;
27399 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27400 x0 -= 1;
27402 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27407 /* Redraw the part of window W intersection rectangle FR. Pixel
27408 coordinates in FR are frame-relative. Call this function with
27409 input blocked. Value is non-zero if the exposure overwrites
27410 mouse-face. */
27412 static int
27413 expose_window (struct window *w, XRectangle *fr)
27415 struct frame *f = XFRAME (w->frame);
27416 XRectangle wr, r;
27417 int mouse_face_overwritten_p = 0;
27419 /* If window is not yet fully initialized, do nothing. This can
27420 happen when toolkit scroll bars are used and a window is split.
27421 Reconfiguring the scroll bar will generate an expose for a newly
27422 created window. */
27423 if (w->current_matrix == NULL)
27424 return 0;
27426 /* When we're currently updating the window, display and current
27427 matrix usually don't agree. Arrange for a thorough display
27428 later. */
27429 if (w == updated_window)
27431 SET_FRAME_GARBAGED (f);
27432 return 0;
27435 /* Frame-relative pixel rectangle of W. */
27436 wr.x = WINDOW_LEFT_EDGE_X (w);
27437 wr.y = WINDOW_TOP_EDGE_Y (w);
27438 wr.width = WINDOW_TOTAL_WIDTH (w);
27439 wr.height = WINDOW_TOTAL_HEIGHT (w);
27441 if (x_intersect_rectangles (fr, &wr, &r))
27443 int yb = window_text_bottom_y (w);
27444 struct glyph_row *row;
27445 int cursor_cleared_p;
27446 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27448 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27449 r.x, r.y, r.width, r.height));
27451 /* Convert to window coordinates. */
27452 r.x -= WINDOW_LEFT_EDGE_X (w);
27453 r.y -= WINDOW_TOP_EDGE_Y (w);
27455 /* Turn off the cursor. */
27456 if (!w->pseudo_window_p
27457 && phys_cursor_in_rect_p (w, &r))
27459 x_clear_cursor (w);
27460 cursor_cleared_p = 1;
27462 else
27463 cursor_cleared_p = 0;
27465 /* Update lines intersecting rectangle R. */
27466 first_overlapping_row = last_overlapping_row = NULL;
27467 for (row = w->current_matrix->rows;
27468 row->enabled_p;
27469 ++row)
27471 int y0 = row->y;
27472 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27474 if ((y0 >= r.y && y0 < r.y + r.height)
27475 || (y1 > r.y && y1 < r.y + r.height)
27476 || (r.y >= y0 && r.y < y1)
27477 || (r.y + r.height > y0 && r.y + r.height < y1))
27479 /* A header line may be overlapping, but there is no need
27480 to fix overlapping areas for them. KFS 2005-02-12 */
27481 if (row->overlapping_p && !row->mode_line_p)
27483 if (first_overlapping_row == NULL)
27484 first_overlapping_row = row;
27485 last_overlapping_row = row;
27488 row->clip = fr;
27489 if (expose_line (w, row, &r))
27490 mouse_face_overwritten_p = 1;
27491 row->clip = NULL;
27493 else if (row->overlapping_p)
27495 /* We must redraw a row overlapping the exposed area. */
27496 if (y0 < r.y
27497 ? y0 + row->phys_height > r.y
27498 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27500 if (first_overlapping_row == NULL)
27501 first_overlapping_row = row;
27502 last_overlapping_row = row;
27506 if (y1 >= yb)
27507 break;
27510 /* Display the mode line if there is one. */
27511 if (WINDOW_WANTS_MODELINE_P (w)
27512 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27513 row->enabled_p)
27514 && row->y < r.y + r.height)
27516 if (expose_line (w, row, &r))
27517 mouse_face_overwritten_p = 1;
27520 if (!w->pseudo_window_p)
27522 /* Fix the display of overlapping rows. */
27523 if (first_overlapping_row)
27524 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27525 fr);
27527 /* Draw border between windows. */
27528 x_draw_vertical_border (w);
27530 /* Turn the cursor on again. */
27531 if (cursor_cleared_p)
27532 update_window_cursor (w, 1);
27536 return mouse_face_overwritten_p;
27541 /* Redraw (parts) of all windows in the window tree rooted at W that
27542 intersect R. R contains frame pixel coordinates. Value is
27543 non-zero if the exposure overwrites mouse-face. */
27545 static int
27546 expose_window_tree (struct window *w, XRectangle *r)
27548 struct frame *f = XFRAME (w->frame);
27549 int mouse_face_overwritten_p = 0;
27551 while (w && !FRAME_GARBAGED_P (f))
27553 if (!NILP (w->hchild))
27554 mouse_face_overwritten_p
27555 |= expose_window_tree (XWINDOW (w->hchild), r);
27556 else if (!NILP (w->vchild))
27557 mouse_face_overwritten_p
27558 |= expose_window_tree (XWINDOW (w->vchild), r);
27559 else
27560 mouse_face_overwritten_p |= expose_window (w, r);
27562 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27565 return mouse_face_overwritten_p;
27569 /* EXPORT:
27570 Redisplay an exposed area of frame F. X and Y are the upper-left
27571 corner of the exposed rectangle. W and H are width and height of
27572 the exposed area. All are pixel values. W or H zero means redraw
27573 the entire frame. */
27575 void
27576 expose_frame (struct frame *f, int x, int y, int w, int h)
27578 XRectangle r;
27579 int mouse_face_overwritten_p = 0;
27581 TRACE ((stderr, "expose_frame "));
27583 /* No need to redraw if frame will be redrawn soon. */
27584 if (FRAME_GARBAGED_P (f))
27586 TRACE ((stderr, " garbaged\n"));
27587 return;
27590 /* If basic faces haven't been realized yet, there is no point in
27591 trying to redraw anything. This can happen when we get an expose
27592 event while Emacs is starting, e.g. by moving another window. */
27593 if (FRAME_FACE_CACHE (f) == NULL
27594 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27596 TRACE ((stderr, " no faces\n"));
27597 return;
27600 if (w == 0 || h == 0)
27602 r.x = r.y = 0;
27603 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27604 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27606 else
27608 r.x = x;
27609 r.y = y;
27610 r.width = w;
27611 r.height = h;
27614 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27615 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27617 if (WINDOWP (f->tool_bar_window))
27618 mouse_face_overwritten_p
27619 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27621 #ifdef HAVE_X_WINDOWS
27622 #ifndef MSDOS
27623 #ifndef USE_X_TOOLKIT
27624 if (WINDOWP (f->menu_bar_window))
27625 mouse_face_overwritten_p
27626 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27627 #endif /* not USE_X_TOOLKIT */
27628 #endif
27629 #endif
27631 /* Some window managers support a focus-follows-mouse style with
27632 delayed raising of frames. Imagine a partially obscured frame,
27633 and moving the mouse into partially obscured mouse-face on that
27634 frame. The visible part of the mouse-face will be highlighted,
27635 then the WM raises the obscured frame. With at least one WM, KDE
27636 2.1, Emacs is not getting any event for the raising of the frame
27637 (even tried with SubstructureRedirectMask), only Expose events.
27638 These expose events will draw text normally, i.e. not
27639 highlighted. Which means we must redo the highlight here.
27640 Subsume it under ``we love X''. --gerd 2001-08-15 */
27641 /* Included in Windows version because Windows most likely does not
27642 do the right thing if any third party tool offers
27643 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27644 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27646 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27647 if (f == hlinfo->mouse_face_mouse_frame)
27649 int mouse_x = hlinfo->mouse_face_mouse_x;
27650 int mouse_y = hlinfo->mouse_face_mouse_y;
27651 clear_mouse_face (hlinfo);
27652 note_mouse_highlight (f, mouse_x, mouse_y);
27658 /* EXPORT:
27659 Determine the intersection of two rectangles R1 and R2. Return
27660 the intersection in *RESULT. Value is non-zero if RESULT is not
27661 empty. */
27664 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27666 XRectangle *left, *right;
27667 XRectangle *upper, *lower;
27668 int intersection_p = 0;
27670 /* Rearrange so that R1 is the left-most rectangle. */
27671 if (r1->x < r2->x)
27672 left = r1, right = r2;
27673 else
27674 left = r2, right = r1;
27676 /* X0 of the intersection is right.x0, if this is inside R1,
27677 otherwise there is no intersection. */
27678 if (right->x <= left->x + left->width)
27680 result->x = right->x;
27682 /* The right end of the intersection is the minimum of
27683 the right ends of left and right. */
27684 result->width = (min (left->x + left->width, right->x + right->width)
27685 - result->x);
27687 /* Same game for Y. */
27688 if (r1->y < r2->y)
27689 upper = r1, lower = r2;
27690 else
27691 upper = r2, lower = r1;
27693 /* The upper end of the intersection is lower.y0, if this is inside
27694 of upper. Otherwise, there is no intersection. */
27695 if (lower->y <= upper->y + upper->height)
27697 result->y = lower->y;
27699 /* The lower end of the intersection is the minimum of the lower
27700 ends of upper and lower. */
27701 result->height = (min (lower->y + lower->height,
27702 upper->y + upper->height)
27703 - result->y);
27704 intersection_p = 1;
27708 return intersection_p;
27711 #endif /* HAVE_WINDOW_SYSTEM */
27714 /***********************************************************************
27715 Initialization
27716 ***********************************************************************/
27718 void
27719 syms_of_xdisp (void)
27721 Vwith_echo_area_save_vector = Qnil;
27722 staticpro (&Vwith_echo_area_save_vector);
27724 Vmessage_stack = Qnil;
27725 staticpro (&Vmessage_stack);
27727 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
27729 message_dolog_marker1 = Fmake_marker ();
27730 staticpro (&message_dolog_marker1);
27731 message_dolog_marker2 = Fmake_marker ();
27732 staticpro (&message_dolog_marker2);
27733 message_dolog_marker3 = Fmake_marker ();
27734 staticpro (&message_dolog_marker3);
27736 #if GLYPH_DEBUG
27737 defsubr (&Sdump_frame_glyph_matrix);
27738 defsubr (&Sdump_glyph_matrix);
27739 defsubr (&Sdump_glyph_row);
27740 defsubr (&Sdump_tool_bar_row);
27741 defsubr (&Strace_redisplay);
27742 defsubr (&Strace_to_stderr);
27743 #endif
27744 #ifdef HAVE_WINDOW_SYSTEM
27745 defsubr (&Stool_bar_lines_needed);
27746 defsubr (&Slookup_image_map);
27747 #endif
27748 defsubr (&Sformat_mode_line);
27749 defsubr (&Sinvisible_p);
27750 defsubr (&Scurrent_bidi_paragraph_direction);
27752 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
27753 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
27754 DEFSYM (Qoverriding_local_map, "overriding-local-map");
27755 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
27756 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
27757 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
27758 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
27759 DEFSYM (Qeval, "eval");
27760 DEFSYM (QCdata, ":data");
27761 DEFSYM (Qdisplay, "display");
27762 DEFSYM (Qspace_width, "space-width");
27763 DEFSYM (Qraise, "raise");
27764 DEFSYM (Qslice, "slice");
27765 DEFSYM (Qspace, "space");
27766 DEFSYM (Qmargin, "margin");
27767 DEFSYM (Qpointer, "pointer");
27768 DEFSYM (Qleft_margin, "left-margin");
27769 DEFSYM (Qright_margin, "right-margin");
27770 DEFSYM (Qcenter, "center");
27771 DEFSYM (Qline_height, "line-height");
27772 DEFSYM (QCalign_to, ":align-to");
27773 DEFSYM (QCrelative_width, ":relative-width");
27774 DEFSYM (QCrelative_height, ":relative-height");
27775 DEFSYM (QCeval, ":eval");
27776 DEFSYM (QCpropertize, ":propertize");
27777 DEFSYM (QCfile, ":file");
27778 DEFSYM (Qfontified, "fontified");
27779 DEFSYM (Qfontification_functions, "fontification-functions");
27780 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
27781 DEFSYM (Qescape_glyph, "escape-glyph");
27782 DEFSYM (Qnobreak_space, "nobreak-space");
27783 DEFSYM (Qimage, "image");
27784 DEFSYM (Qtext, "text");
27785 DEFSYM (Qboth, "both");
27786 DEFSYM (Qboth_horiz, "both-horiz");
27787 DEFSYM (Qtext_image_horiz, "text-image-horiz");
27788 DEFSYM (QCmap, ":map");
27789 DEFSYM (QCpointer, ":pointer");
27790 DEFSYM (Qrect, "rect");
27791 DEFSYM (Qcircle, "circle");
27792 DEFSYM (Qpoly, "poly");
27793 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
27794 DEFSYM (Qgrow_only, "grow-only");
27795 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
27796 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
27797 DEFSYM (Qposition, "position");
27798 DEFSYM (Qbuffer_position, "buffer-position");
27799 DEFSYM (Qobject, "object");
27800 DEFSYM (Qbar, "bar");
27801 DEFSYM (Qhbar, "hbar");
27802 DEFSYM (Qbox, "box");
27803 DEFSYM (Qhollow, "hollow");
27804 DEFSYM (Qhand, "hand");
27805 DEFSYM (Qarrow, "arrow");
27806 DEFSYM (Qtext, "text");
27807 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
27809 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27810 Fcons (intern_c_string ("void-variable"), Qnil)),
27811 Qnil);
27812 staticpro (&list_of_error);
27814 DEFSYM (Qlast_arrow_position, "last-arrow-position");
27815 DEFSYM (Qlast_arrow_string, "last-arrow-string");
27816 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
27817 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
27819 echo_buffer[0] = echo_buffer[1] = Qnil;
27820 staticpro (&echo_buffer[0]);
27821 staticpro (&echo_buffer[1]);
27823 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27824 staticpro (&echo_area_buffer[0]);
27825 staticpro (&echo_area_buffer[1]);
27827 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27828 staticpro (&Vmessages_buffer_name);
27830 mode_line_proptrans_alist = Qnil;
27831 staticpro (&mode_line_proptrans_alist);
27832 mode_line_string_list = Qnil;
27833 staticpro (&mode_line_string_list);
27834 mode_line_string_face = Qnil;
27835 staticpro (&mode_line_string_face);
27836 mode_line_string_face_prop = Qnil;
27837 staticpro (&mode_line_string_face_prop);
27838 Vmode_line_unwind_vector = Qnil;
27839 staticpro (&Vmode_line_unwind_vector);
27841 help_echo_string = Qnil;
27842 staticpro (&help_echo_string);
27843 help_echo_object = Qnil;
27844 staticpro (&help_echo_object);
27845 help_echo_window = Qnil;
27846 staticpro (&help_echo_window);
27847 previous_help_echo_string = Qnil;
27848 staticpro (&previous_help_echo_string);
27849 help_echo_pos = -1;
27851 DEFSYM (Qright_to_left, "right-to-left");
27852 DEFSYM (Qleft_to_right, "left-to-right");
27854 #ifdef HAVE_WINDOW_SYSTEM
27855 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27856 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27857 For example, if a block cursor is over a tab, it will be drawn as
27858 wide as that tab on the display. */);
27859 x_stretch_cursor_p = 0;
27860 #endif
27862 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27863 doc: /* *Non-nil means highlight trailing whitespace.
27864 The face used for trailing whitespace is `trailing-whitespace'. */);
27865 Vshow_trailing_whitespace = Qnil;
27867 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27868 doc: /* *Control highlighting of nobreak space and soft hyphen.
27869 A value of t means highlight the character itself (for nobreak space,
27870 use face `nobreak-space').
27871 A value of nil means no highlighting.
27872 Other values mean display the escape glyph followed by an ordinary
27873 space or ordinary hyphen. */);
27874 Vnobreak_char_display = Qt;
27876 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27877 doc: /* *The pointer shape to show in void text areas.
27878 A value of nil means to show the text pointer. Other options are `arrow',
27879 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27880 Vvoid_text_area_pointer = Qarrow;
27882 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27883 doc: /* Non-nil means don't actually do any redisplay.
27884 This is used for internal purposes. */);
27885 Vinhibit_redisplay = Qnil;
27887 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27888 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27889 Vglobal_mode_string = Qnil;
27891 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27892 doc: /* Marker for where to display an arrow on top of the buffer text.
27893 This must be the beginning of a line in order to work.
27894 See also `overlay-arrow-string'. */);
27895 Voverlay_arrow_position = Qnil;
27897 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27898 doc: /* String to display as an arrow in non-window frames.
27899 See also `overlay-arrow-position'. */);
27900 Voverlay_arrow_string = make_pure_c_string ("=>");
27902 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27903 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27904 The symbols on this list are examined during redisplay to determine
27905 where to display overlay arrows. */);
27906 Voverlay_arrow_variable_list
27907 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27909 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27910 doc: /* *The number of lines to try scrolling a window by when point moves out.
27911 If that fails to bring point back on frame, point is centered instead.
27912 If this is zero, point is always centered after it moves off frame.
27913 If you want scrolling to always be a line at a time, you should set
27914 `scroll-conservatively' to a large value rather than set this to 1. */);
27916 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27917 doc: /* *Scroll up to this many lines, to bring point back on screen.
27918 If point moves off-screen, redisplay will scroll by up to
27919 `scroll-conservatively' lines in order to bring point just barely
27920 onto the screen again. If that cannot be done, then redisplay
27921 recenters point as usual.
27923 If the value is greater than 100, redisplay will never recenter point,
27924 but will always scroll just enough text to bring point into view, even
27925 if you move far away.
27927 A value of zero means always recenter point if it moves off screen. */);
27928 scroll_conservatively = 0;
27930 DEFVAR_INT ("scroll-margin", scroll_margin,
27931 doc: /* *Number of lines of margin at the top and bottom of a window.
27932 Recenter the window whenever point gets within this many lines
27933 of the top or bottom of the window. */);
27934 scroll_margin = 0;
27936 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27937 doc: /* Pixels per inch value for non-window system displays.
27938 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27939 Vdisplay_pixels_per_inch = make_float (72.0);
27941 #if GLYPH_DEBUG
27942 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27943 #endif
27945 DEFVAR_LISP ("truncate-partial-width-windows",
27946 Vtruncate_partial_width_windows,
27947 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27948 For an integer value, truncate lines in each window narrower than the
27949 full frame width, provided the window width is less than that integer;
27950 otherwise, respect the value of `truncate-lines'.
27952 For any other non-nil value, truncate lines in all windows that do
27953 not span the full frame width.
27955 A value of nil means to respect the value of `truncate-lines'.
27957 If `word-wrap' is enabled, you might want to reduce this. */);
27958 Vtruncate_partial_width_windows = make_number (50);
27960 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27961 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27962 Any other value means to use the appropriate face, `mode-line',
27963 `header-line', or `menu' respectively. */);
27964 mode_line_inverse_video = 1;
27966 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27967 doc: /* *Maximum buffer size for which line number should be displayed.
27968 If the buffer is bigger than this, the line number does not appear
27969 in the mode line. A value of nil means no limit. */);
27970 Vline_number_display_limit = Qnil;
27972 DEFVAR_INT ("line-number-display-limit-width",
27973 line_number_display_limit_width,
27974 doc: /* *Maximum line width (in characters) for line number display.
27975 If the average length of the lines near point is bigger than this, then the
27976 line number may be omitted from the mode line. */);
27977 line_number_display_limit_width = 200;
27979 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27980 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27981 highlight_nonselected_windows = 0;
27983 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27984 doc: /* Non-nil if more than one frame is visible on this display.
27985 Minibuffer-only frames don't count, but iconified frames do.
27986 This variable is not guaranteed to be accurate except while processing
27987 `frame-title-format' and `icon-title-format'. */);
27989 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27990 doc: /* Template for displaying the title bar of visible frames.
27991 \(Assuming the window manager supports this feature.)
27993 This variable has the same structure as `mode-line-format', except that
27994 the %c and %l constructs are ignored. It is used only on frames for
27995 which no explicit name has been set \(see `modify-frame-parameters'). */);
27997 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27998 doc: /* Template for displaying the title bar of an iconified frame.
27999 \(Assuming the window manager supports this feature.)
28000 This variable has the same structure as `mode-line-format' (which see),
28001 and is used only on frames for which no explicit name has been set
28002 \(see `modify-frame-parameters'). */);
28003 Vicon_title_format
28004 = Vframe_title_format
28005 = pure_cons (intern_c_string ("multiple-frames"),
28006 pure_cons (make_pure_c_string ("%b"),
28007 pure_cons (pure_cons (empty_unibyte_string,
28008 pure_cons (intern_c_string ("invocation-name"),
28009 pure_cons (make_pure_c_string ("@"),
28010 pure_cons (intern_c_string ("system-name"),
28011 Qnil)))),
28012 Qnil)));
28014 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28015 doc: /* Maximum number of lines to keep in the message log buffer.
28016 If nil, disable message logging. If t, log messages but don't truncate
28017 the buffer when it becomes large. */);
28018 Vmessage_log_max = make_number (100);
28020 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28021 doc: /* Functions called before redisplay, if window sizes have changed.
28022 The value should be a list of functions that take one argument.
28023 Just before redisplay, for each frame, if any of its windows have changed
28024 size since the last redisplay, or have been split or deleted,
28025 all the functions in the list are called, with the frame as argument. */);
28026 Vwindow_size_change_functions = Qnil;
28028 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28029 doc: /* List of functions to call before redisplaying a window with scrolling.
28030 Each function is called with two arguments, the window and its new
28031 display-start position. Note that these functions are also called by
28032 `set-window-buffer'. Also note that the value of `window-end' is not
28033 valid when these functions are called. */);
28034 Vwindow_scroll_functions = Qnil;
28036 DEFVAR_LISP ("window-text-change-functions",
28037 Vwindow_text_change_functions,
28038 doc: /* Functions to call in redisplay when text in the window might change. */);
28039 Vwindow_text_change_functions = Qnil;
28041 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28042 doc: /* Functions called when redisplay of a window reaches the end trigger.
28043 Each function is called with two arguments, the window and the end trigger value.
28044 See `set-window-redisplay-end-trigger'. */);
28045 Vredisplay_end_trigger_functions = Qnil;
28047 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28048 doc: /* *Non-nil means autoselect window with mouse pointer.
28049 If nil, do not autoselect windows.
28050 A positive number means delay autoselection by that many seconds: a
28051 window is autoselected only after the mouse has remained in that
28052 window for the duration of the delay.
28053 A negative number has a similar effect, but causes windows to be
28054 autoselected only after the mouse has stopped moving. \(Because of
28055 the way Emacs compares mouse events, you will occasionally wait twice
28056 that time before the window gets selected.\)
28057 Any other value means to autoselect window instantaneously when the
28058 mouse pointer enters it.
28060 Autoselection selects the minibuffer only if it is active, and never
28061 unselects the minibuffer if it is active.
28063 When customizing this variable make sure that the actual value of
28064 `focus-follows-mouse' matches the behavior of your window manager. */);
28065 Vmouse_autoselect_window = Qnil;
28067 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28068 doc: /* *Non-nil means automatically resize tool-bars.
28069 This dynamically changes the tool-bar's height to the minimum height
28070 that is needed to make all tool-bar items visible.
28071 If value is `grow-only', the tool-bar's height is only increased
28072 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28073 Vauto_resize_tool_bars = Qt;
28075 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28076 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28077 auto_raise_tool_bar_buttons_p = 1;
28079 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28080 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28081 make_cursor_line_fully_visible_p = 1;
28083 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28084 doc: /* *Border below tool-bar in pixels.
28085 If an integer, use it as the height of the border.
28086 If it is one of `internal-border-width' or `border-width', use the
28087 value of the corresponding frame parameter.
28088 Otherwise, no border is added below the tool-bar. */);
28089 Vtool_bar_border = Qinternal_border_width;
28091 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28092 doc: /* *Margin around tool-bar buttons in pixels.
28093 If an integer, use that for both horizontal and vertical margins.
28094 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28095 HORZ specifying the horizontal margin, and VERT specifying the
28096 vertical margin. */);
28097 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28099 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28100 doc: /* *Relief thickness of tool-bar buttons. */);
28101 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28103 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28104 doc: /* Tool bar style to use.
28105 It can be one of
28106 image - show images only
28107 text - show text only
28108 both - show both, text below image
28109 both-horiz - show text to the right of the image
28110 text-image-horiz - show text to the left of the image
28111 any other - use system default or image if no system default. */);
28112 Vtool_bar_style = Qnil;
28114 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28115 doc: /* *Maximum number of characters a label can have to be shown.
28116 The tool bar style must also show labels for this to have any effect, see
28117 `tool-bar-style'. */);
28118 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28120 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28121 doc: /* List of functions to call to fontify regions of text.
28122 Each function is called with one argument POS. Functions must
28123 fontify a region starting at POS in the current buffer, and give
28124 fontified regions the property `fontified'. */);
28125 Vfontification_functions = Qnil;
28126 Fmake_variable_buffer_local (Qfontification_functions);
28128 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28129 unibyte_display_via_language_environment,
28130 doc: /* *Non-nil means display unibyte text according to language environment.
28131 Specifically, this means that raw bytes in the range 160-255 decimal
28132 are displayed by converting them to the equivalent multibyte characters
28133 according to the current language environment. As a result, they are
28134 displayed according to the current fontset.
28136 Note that this variable affects only how these bytes are displayed,
28137 but does not change the fact they are interpreted as raw bytes. */);
28138 unibyte_display_via_language_environment = 0;
28140 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28141 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28142 If a float, it specifies a fraction of the mini-window frame's height.
28143 If an integer, it specifies a number of lines. */);
28144 Vmax_mini_window_height = make_float (0.25);
28146 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28147 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28148 A value of nil means don't automatically resize mini-windows.
28149 A value of t means resize them to fit the text displayed in them.
28150 A value of `grow-only', the default, means let mini-windows grow only;
28151 they return to their normal size when the minibuffer is closed, or the
28152 echo area becomes empty. */);
28153 Vresize_mini_windows = Qgrow_only;
28155 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28156 doc: /* Alist specifying how to blink the cursor off.
28157 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28158 `cursor-type' frame-parameter or variable equals ON-STATE,
28159 comparing using `equal', Emacs uses OFF-STATE to specify
28160 how to blink it off. ON-STATE and OFF-STATE are values for
28161 the `cursor-type' frame parameter.
28163 If a frame's ON-STATE has no entry in this list,
28164 the frame's other specifications determine how to blink the cursor off. */);
28165 Vblink_cursor_alist = Qnil;
28167 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28168 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28169 If non-nil, windows are automatically scrolled horizontally to make
28170 point visible. */);
28171 automatic_hscrolling_p = 1;
28172 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28174 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28175 doc: /* *How many columns away from the window edge point is allowed to get
28176 before automatic hscrolling will horizontally scroll the window. */);
28177 hscroll_margin = 5;
28179 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28180 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28181 When point is less than `hscroll-margin' columns from the window
28182 edge, automatic hscrolling will scroll the window by the amount of columns
28183 determined by this variable. If its value is a positive integer, scroll that
28184 many columns. If it's a positive floating-point number, it specifies the
28185 fraction of the window's width to scroll. If it's nil or zero, point will be
28186 centered horizontally after the scroll. Any other value, including negative
28187 numbers, are treated as if the value were zero.
28189 Automatic hscrolling always moves point outside the scroll margin, so if
28190 point was more than scroll step columns inside the margin, the window will
28191 scroll more than the value given by the scroll step.
28193 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28194 and `scroll-right' overrides this variable's effect. */);
28195 Vhscroll_step = make_number (0);
28197 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28198 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28199 Bind this around calls to `message' to let it take effect. */);
28200 message_truncate_lines = 0;
28202 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28203 doc: /* Normal hook run to update the menu bar definitions.
28204 Redisplay runs this hook before it redisplays the menu bar.
28205 This is used to update submenus such as Buffers,
28206 whose contents depend on various data. */);
28207 Vmenu_bar_update_hook = Qnil;
28209 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28210 doc: /* Frame for which we are updating a menu.
28211 The enable predicate for a menu binding should check this variable. */);
28212 Vmenu_updating_frame = Qnil;
28214 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28215 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28216 inhibit_menubar_update = 0;
28218 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28219 doc: /* Prefix prepended to all continuation lines at display time.
28220 The value may be a string, an image, or a stretch-glyph; it is
28221 interpreted in the same way as the value of a `display' text property.
28223 This variable is overridden by any `wrap-prefix' text or overlay
28224 property.
28226 To add a prefix to non-continuation lines, use `line-prefix'. */);
28227 Vwrap_prefix = Qnil;
28228 DEFSYM (Qwrap_prefix, "wrap-prefix");
28229 Fmake_variable_buffer_local (Qwrap_prefix);
28231 DEFVAR_LISP ("line-prefix", Vline_prefix,
28232 doc: /* Prefix prepended to all non-continuation lines at display time.
28233 The value may be a string, an image, or a stretch-glyph; it is
28234 interpreted in the same way as the value of a `display' text property.
28236 This variable is overridden by any `line-prefix' text or overlay
28237 property.
28239 To add a prefix to continuation lines, use `wrap-prefix'. */);
28240 Vline_prefix = Qnil;
28241 DEFSYM (Qline_prefix, "line-prefix");
28242 Fmake_variable_buffer_local (Qline_prefix);
28244 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28245 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28246 inhibit_eval_during_redisplay = 0;
28248 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28249 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28250 inhibit_free_realized_faces = 0;
28252 #if GLYPH_DEBUG
28253 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28254 doc: /* Inhibit try_window_id display optimization. */);
28255 inhibit_try_window_id = 0;
28257 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28258 doc: /* Inhibit try_window_reusing display optimization. */);
28259 inhibit_try_window_reusing = 0;
28261 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28262 doc: /* Inhibit try_cursor_movement display optimization. */);
28263 inhibit_try_cursor_movement = 0;
28264 #endif /* GLYPH_DEBUG */
28266 DEFVAR_INT ("overline-margin", overline_margin,
28267 doc: /* *Space between overline and text, in pixels.
28268 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28269 margin to the caracter height. */);
28270 overline_margin = 2;
28272 DEFVAR_INT ("underline-minimum-offset",
28273 underline_minimum_offset,
28274 doc: /* Minimum distance between baseline and underline.
28275 This can improve legibility of underlined text at small font sizes,
28276 particularly when using variable `x-use-underline-position-properties'
28277 with fonts that specify an UNDERLINE_POSITION relatively close to the
28278 baseline. The default value is 1. */);
28279 underline_minimum_offset = 1;
28281 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28282 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28283 This feature only works when on a window system that can change
28284 cursor shapes. */);
28285 display_hourglass_p = 1;
28287 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28288 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28289 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28291 hourglass_atimer = NULL;
28292 hourglass_shown_p = 0;
28294 DEFSYM (Qglyphless_char, "glyphless-char");
28295 DEFSYM (Qhex_code, "hex-code");
28296 DEFSYM (Qempty_box, "empty-box");
28297 DEFSYM (Qthin_space, "thin-space");
28298 DEFSYM (Qzero_width, "zero-width");
28300 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28301 /* Intern this now in case it isn't already done.
28302 Setting this variable twice is harmless.
28303 But don't staticpro it here--that is done in alloc.c. */
28304 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28305 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28307 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28308 doc: /* Char-table defining glyphless characters.
28309 Each element, if non-nil, should be one of the following:
28310 an ASCII acronym string: display this string in a box
28311 `hex-code': display the hexadecimal code of a character in a box
28312 `empty-box': display as an empty box
28313 `thin-space': display as 1-pixel width space
28314 `zero-width': don't display
28315 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28316 display method for graphical terminals and text terminals respectively.
28317 GRAPHICAL and TEXT should each have one of the values listed above.
28319 The char-table has one extra slot to control the display of a character for
28320 which no font is found. This slot only takes effect on graphical terminals.
28321 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28322 `thin-space'. The default is `empty-box'. */);
28323 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28324 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28325 Qempty_box);
28329 /* Initialize this module when Emacs starts. */
28331 void
28332 init_xdisp (void)
28334 current_header_line_height = current_mode_line_height = -1;
28336 CHARPOS (this_line_start_pos) = 0;
28338 if (!noninteractive)
28340 struct window *m = XWINDOW (minibuf_window);
28341 Lisp_Object frame = m->frame;
28342 struct frame *f = XFRAME (frame);
28343 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28344 struct window *r = XWINDOW (root);
28345 int i;
28347 echo_area_window = minibuf_window;
28349 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28350 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28351 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28352 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28353 XSETFASTINT (m->total_lines, 1);
28354 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28356 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28357 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28358 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28360 /* The default ellipsis glyphs `...'. */
28361 for (i = 0; i < 3; ++i)
28362 default_invis_vector[i] = make_number ('.');
28366 /* Allocate the buffer for frame titles.
28367 Also used for `format-mode-line'. */
28368 int size = 100;
28369 mode_line_noprop_buf = (char *) xmalloc (size);
28370 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28371 mode_line_noprop_ptr = mode_line_noprop_buf;
28372 mode_line_target = MODE_LINE_DISPLAY;
28375 help_echo_showing_p = 0;
28378 /* Since w32 does not support atimers, it defines its own implementation of
28379 the following three functions in w32fns.c. */
28380 #ifndef WINDOWSNT
28382 /* Platform-independent portion of hourglass implementation. */
28384 /* Return non-zero if houglass timer has been started or hourglass is shown. */
28386 hourglass_started (void)
28388 return hourglass_shown_p || hourglass_atimer != NULL;
28391 /* Cancel a currently active hourglass timer, and start a new one. */
28392 void
28393 start_hourglass (void)
28395 #if defined (HAVE_WINDOW_SYSTEM)
28396 EMACS_TIME delay;
28397 int secs, usecs = 0;
28399 cancel_hourglass ();
28401 if (INTEGERP (Vhourglass_delay)
28402 && XINT (Vhourglass_delay) > 0)
28403 secs = XFASTINT (Vhourglass_delay);
28404 else if (FLOATP (Vhourglass_delay)
28405 && XFLOAT_DATA (Vhourglass_delay) > 0)
28407 Lisp_Object tem;
28408 tem = Ftruncate (Vhourglass_delay, Qnil);
28409 secs = XFASTINT (tem);
28410 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28412 else
28413 secs = DEFAULT_HOURGLASS_DELAY;
28415 EMACS_SET_SECS_USECS (delay, secs, usecs);
28416 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28417 show_hourglass, NULL);
28418 #endif
28422 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28423 shown. */
28424 void
28425 cancel_hourglass (void)
28427 #if defined (HAVE_WINDOW_SYSTEM)
28428 if (hourglass_atimer)
28430 cancel_atimer (hourglass_atimer);
28431 hourglass_atimer = NULL;
28434 if (hourglass_shown_p)
28435 hide_hourglass ();
28436 #endif
28438 #endif /* ! WINDOWSNT */