Support bidi reordering of unibyte strings. Fix crash displaying "All" in mode line...
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
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.
28
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.
34
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.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
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.
61
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.
71
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.
77
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:
83
84 . try_cursor_movement
85
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.
89
90 . try_window_reusing_current_matrix
91
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).
95
96 . try_window_id
97
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.
101
102 . try_window
103
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.)
109
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.
114
115 Desired matrices.
116
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.
123
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.
130
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.
140
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.
147
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).
159
160 Frame matrices.
161
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.
168
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.
180
181 Bidirectional display.
182
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.
195
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...
204
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.
223
224 Bidirectional display and character compositions
225
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.
230
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.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
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 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. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
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"
301
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
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
391
392 /* Name of the face used to highlight trailing whitespace. */
393
394 static Lisp_Object Qtrailing_whitespace;
395
396 /* Name and number of the face used to highlight escape glyphs. */
397
398 static Lisp_Object Qescape_glyph;
399
400 /* Name and number of the face used to highlight non-breaking spaces. */
401
402 static Lisp_Object Qnobreak_space;
403
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
406
407 Lisp_Object Qimage;
408
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
413
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
419
420 int noninteractive_need_newline;
421
422 /* Non-zero means print newline to message log before next message. */
423
424 static int message_log_need_newline;
425
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
432 \f
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
437
438 static struct text_pos this_line_start_pos;
439
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
442
443 static struct text_pos this_line_end_pos;
444
445 /* The vertical positions and the height of this line. */
446
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
450
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
453
454 static int this_line_start_x;
455
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
459
460 static struct text_pos this_line_min_pos;
461
462 /* Buffer that this_line_.* variables are referring to. */
463
464 static struct buffer *this_line_buffer;
465
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 Lisp_Object Qmenu_bar_update_hook;
480
481 /* Nonzero if an overlay arrow has been displayed in this window. */
482
483 static int overlay_arrow_seen;
484
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
488
489 int buffer_shared;
490
491 /* Vector containing glyphs for an ellipsis `...'. */
492
493 static Lisp_Object default_invis_vector[3];
494
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
498
499 Lisp_Object echo_area_window;
500
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
505
506 static Lisp_Object Vmessage_stack;
507
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
510
511 static int message_enable_multibyte;
512
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
514
515 int update_mode_lines;
516
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero means a frame's cursor type has been changed. */
523
524 int cursor_type_changed;
525
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
528
529 static int line_number_displayed;
530
531 /* The name of the *Messages* buffer, a string. */
532
533 static Lisp_Object Vmessages_buffer_name;
534
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
537
538 Lisp_Object echo_area_buffer[2];
539
540 /* The buffers referenced from echo_area_buffer. */
541
542 static Lisp_Object echo_buffer[2];
543
544 /* A vector saved used in with_area_buffer to reduce consing. */
545
546 static Lisp_Object Vwith_echo_area_save_vector;
547
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
550
551 static int display_last_displayed_message_p;
552
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
555
556 static int message_buf_print;
557
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
559
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
562
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
565
566 static int message_cleared_p;
567
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
570
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
574
575 /* Ascent and height of the last line processed by move_it_to. */
576
577 static int last_max_ascent, last_height;
578
579 /* Non-zero if there's a help-echo in the echo area. */
580
581 int help_echo_showing_p;
582
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
586
587 int current_mode_line_height, current_header_line_height;
588
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
594
595 #define TEXT_PROP_DISTANCE_LIMIT 100
596
597 #if GLYPH_DEBUG
598
599 /* Non-zero means print traces of redisplay if compiled with
600 GLYPH_DEBUG != 0. */
601
602 int trace_redisplay_p;
603
604 #endif /* GLYPH_DEBUG */
605
606 #ifdef DEBUG_TRACE_MOVE
607 /* Non-zero means trace with TRACE_MOVE to stderr. */
608 int trace_move;
609
610 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
611 #else
612 #define TRACE_MOVE(x) (void) 0
613 #endif
614
615 static Lisp_Object Qauto_hscroll_mode;
616
617 /* Buffer being redisplayed -- for redisplay_window_error. */
618
619 static struct buffer *displayed_buffer;
620
621 /* Value returned from text property handlers (see below). */
622
623 enum prop_handled
624 {
625 HANDLED_NORMALLY,
626 HANDLED_RECOMPUTE_PROPS,
627 HANDLED_OVERLAY_STRING_CONSUMED,
628 HANDLED_RETURN
629 };
630
631 /* A description of text properties that redisplay is interested
632 in. */
633
634 struct props
635 {
636 /* The name of the property. */
637 Lisp_Object *name;
638
639 /* A unique index for the property. */
640 enum prop_idx idx;
641
642 /* A handler function called to set up iterator IT from the property
643 at IT's current position. Value is used to steer handle_stop. */
644 enum prop_handled (*handler) (struct it *it);
645 };
646
647 static enum prop_handled handle_face_prop (struct it *);
648 static enum prop_handled handle_invisible_prop (struct it *);
649 static enum prop_handled handle_display_prop (struct it *);
650 static enum prop_handled handle_composition_prop (struct it *);
651 static enum prop_handled handle_overlay_change (struct it *);
652 static enum prop_handled handle_fontified_prop (struct it *);
653
654 /* Properties handled by iterators. */
655
656 static struct props it_props[] =
657 {
658 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
659 /* Handle `face' before `display' because some sub-properties of
660 `display' need to know the face. */
661 {&Qface, FACE_PROP_IDX, handle_face_prop},
662 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
663 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
664 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
665 {NULL, 0, NULL}
666 };
667
668 /* Value is the position described by X. If X is a marker, value is
669 the marker_position of X. Otherwise, value is X. */
670
671 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
672
673 /* Enumeration returned by some move_it_.* functions internally. */
674
675 enum move_it_result
676 {
677 /* Not used. Undefined value. */
678 MOVE_UNDEFINED,
679
680 /* Move ended at the requested buffer position or ZV. */
681 MOVE_POS_MATCH_OR_ZV,
682
683 /* Move ended at the requested X pixel position. */
684 MOVE_X_REACHED,
685
686 /* Move within a line ended at the end of a line that must be
687 continued. */
688 MOVE_LINE_CONTINUED,
689
690 /* Move within a line ended at the end of a line that would
691 be displayed truncated. */
692 MOVE_LINE_TRUNCATED,
693
694 /* Move within a line ended at a line end. */
695 MOVE_NEWLINE_OR_CR
696 };
697
698 /* This counter is used to clear the face cache every once in a while
699 in redisplay_internal. It is incremented for each redisplay.
700 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
701 cleared. */
702
703 #define CLEAR_FACE_CACHE_COUNT 500
704 static int clear_face_cache_count;
705
706 /* Similarly for the image cache. */
707
708 #ifdef HAVE_WINDOW_SYSTEM
709 #define CLEAR_IMAGE_CACHE_COUNT 101
710 static int clear_image_cache_count;
711
712 /* Null glyph slice */
713 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
714 #endif
715
716 /* Non-zero while redisplay_internal is in progress. */
717
718 int redisplaying_p;
719
720 static Lisp_Object Qinhibit_free_realized_faces;
721
722 /* If a string, XTread_socket generates an event to display that string.
723 (The display is done in read_char.) */
724
725 Lisp_Object help_echo_string;
726 Lisp_Object help_echo_window;
727 Lisp_Object help_echo_object;
728 EMACS_INT help_echo_pos;
729
730 /* Temporary variable for XTread_socket. */
731
732 Lisp_Object previous_help_echo_string;
733
734 /* Platform-independent portion of hourglass implementation. */
735
736 /* Non-zero means an hourglass cursor is currently shown. */
737 int hourglass_shown_p;
738
739 /* If non-null, an asynchronous timer that, when it expires, displays
740 an hourglass cursor on all frames. */
741 struct atimer *hourglass_atimer;
742
743 /* Name of the face used to display glyphless characters. */
744 Lisp_Object Qglyphless_char;
745
746 /* Symbol for the purpose of Vglyphless_char_display. */
747 static Lisp_Object Qglyphless_char_display;
748
749 /* Method symbols for Vglyphless_char_display. */
750 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
751
752 /* Default pixel width of `thin-space' display method. */
753 #define THIN_SPACE_WIDTH 1
754
755 /* Default number of seconds to wait before displaying an hourglass
756 cursor. */
757 #define DEFAULT_HOURGLASS_DELAY 1
758
759 \f
760 /* Function prototypes. */
761
762 static void setup_for_ellipsis (struct it *, int);
763 static void set_iterator_to_next (struct it *, int);
764 static void mark_window_display_accurate_1 (struct window *, int);
765 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
766 static int display_prop_string_p (Lisp_Object, Lisp_Object);
767 static int cursor_row_p (struct glyph_row *);
768 static int redisplay_mode_lines (Lisp_Object, int);
769 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
770
771 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
772
773 static void handle_line_prefix (struct it *);
774
775 static void pint2str (char *, int, EMACS_INT);
776 static void pint2hrstr (char *, int, EMACS_INT);
777 static struct text_pos run_window_scroll_functions (Lisp_Object,
778 struct text_pos);
779 static void reconsider_clip_changes (struct window *, struct buffer *);
780 static int text_outside_line_unchanged_p (struct window *,
781 EMACS_INT, EMACS_INT);
782 static void store_mode_line_noprop_char (char);
783 static int store_mode_line_noprop (const char *, int, int);
784 static void handle_stop (struct it *);
785 static void handle_stop_backwards (struct it *, EMACS_INT);
786 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
787 static void ensure_echo_area_buffers (void);
788 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
789 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
790 static int with_echo_area_buffer (struct window *, int,
791 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
792 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
793 static void clear_garbaged_frames (void);
794 static int current_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
795 static void pop_message (void);
796 static int truncate_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
797 static void set_message (const char *, Lisp_Object, EMACS_INT, int);
798 static int set_message_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
799 static int display_echo_area (struct window *);
800 static int display_echo_area_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
801 static int resize_mini_window_1 (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT);
802 static Lisp_Object unwind_redisplay (Lisp_Object);
803 static int string_char_and_length (const unsigned char *, int *);
804 static struct text_pos display_prop_end (struct it *, Lisp_Object,
805 struct text_pos);
806 static int compute_window_start_on_continuation_line (struct window *);
807 static Lisp_Object safe_eval_handler (Lisp_Object);
808 static void insert_left_trunc_glyphs (struct it *);
809 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
810 Lisp_Object);
811 static void extend_face_to_end_of_line (struct it *);
812 static int append_space_for_newline (struct it *, int);
813 static int cursor_row_fully_visible_p (struct window *, int, int);
814 static int try_scrolling (Lisp_Object, int, EMACS_INT, EMACS_INT, int, int);
815 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
816 static int trailing_whitespace_p (EMACS_INT);
817 static unsigned long int message_log_check_duplicate (EMACS_INT, EMACS_INT);
818 static void push_it (struct it *, struct text_pos *);
819 static void pop_it (struct it *);
820 static void sync_frame_with_window_matrix_rows (struct window *);
821 static void select_frame_for_redisplay (Lisp_Object);
822 static void redisplay_internal (void);
823 static int echo_area_display (int);
824 static void redisplay_windows (Lisp_Object);
825 static void redisplay_window (Lisp_Object, int);
826 static Lisp_Object redisplay_window_error (Lisp_Object);
827 static Lisp_Object redisplay_window_0 (Lisp_Object);
828 static Lisp_Object redisplay_window_1 (Lisp_Object);
829 static int set_cursor_from_row (struct window *, struct glyph_row *,
830 struct glyph_matrix *, EMACS_INT, EMACS_INT,
831 int, int);
832 static int update_menu_bar (struct frame *, int, int);
833 static int try_window_reusing_current_matrix (struct window *);
834 static int try_window_id (struct window *);
835 static int display_line (struct it *);
836 static int display_mode_lines (struct window *);
837 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
838 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
839 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
840 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
841 static void display_menu_bar (struct window *);
842 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
843 EMACS_INT *);
844 static int display_string (const char *, Lisp_Object, Lisp_Object,
845 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
846 static void compute_line_metrics (struct it *);
847 static void run_redisplay_end_trigger_hook (struct it *);
848 static int get_overlay_strings (struct it *, EMACS_INT);
849 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
850 static void next_overlay_string (struct it *);
851 static void reseat (struct it *, struct text_pos, int);
852 static void reseat_1 (struct it *, struct text_pos, int);
853 static void back_to_previous_visible_line_start (struct it *);
854 void reseat_at_previous_visible_line_start (struct it *);
855 static void reseat_at_next_visible_line_start (struct it *, int);
856 static int next_element_from_ellipsis (struct it *);
857 static int next_element_from_display_vector (struct it *);
858 static int next_element_from_string (struct it *);
859 static int next_element_from_c_string (struct it *);
860 static int next_element_from_buffer (struct it *);
861 static int next_element_from_composition (struct it *);
862 static int next_element_from_image (struct it *);
863 static int next_element_from_stretch (struct it *);
864 static void load_overlay_strings (struct it *, EMACS_INT);
865 static int init_from_display_pos (struct it *, struct window *,
866 struct display_pos *);
867 static void reseat_to_string (struct it *, const char *,
868 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
869 static int get_next_display_element (struct it *);
870 static enum move_it_result
871 move_it_in_display_line_to (struct it *, EMACS_INT, int,
872 enum move_operation_enum);
873 void move_it_vertically_backward (struct it *, int);
874 static void init_to_row_start (struct it *, struct window *,
875 struct glyph_row *);
876 static int init_to_row_end (struct it *, struct window *,
877 struct glyph_row *);
878 static void back_to_previous_line_start (struct it *);
879 static int forward_to_next_line_start (struct it *, int *);
880 static struct text_pos string_pos_nchars_ahead (struct text_pos,
881 Lisp_Object, EMACS_INT);
882 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
883 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
884 static EMACS_INT number_of_chars (const char *, int);
885 static void compute_stop_pos (struct it *);
886 static void compute_string_pos (struct text_pos *, struct text_pos,
887 Lisp_Object);
888 static int face_before_or_after_it_pos (struct it *, int);
889 static EMACS_INT next_overlay_change (EMACS_INT);
890 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
891 Lisp_Object, struct text_pos *, EMACS_INT, int);
892 static int handle_single_display_spec (struct it *, Lisp_Object,
893 Lisp_Object, Lisp_Object,
894 struct text_pos *, EMACS_INT, int, int);
895 static int underlying_face_id (struct it *);
896 static int in_ellipses_for_invisible_text_p (struct display_pos *,
897 struct window *);
898
899 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
900 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
901
902 #ifdef HAVE_WINDOW_SYSTEM
903
904 static void x_consider_frame_title (Lisp_Object);
905 static int tool_bar_lines_needed (struct frame *, int *);
906 static void update_tool_bar (struct frame *, int);
907 static void build_desired_tool_bar_string (struct frame *f);
908 static int redisplay_tool_bar (struct frame *);
909 static void display_tool_bar_line (struct it *, int);
910 static void notice_overwritten_cursor (struct window *,
911 enum glyph_row_area,
912 int, int, int, int);
913 static void append_stretch_glyph (struct it *, Lisp_Object,
914 int, int, int);
915
916
917 #endif /* HAVE_WINDOW_SYSTEM */
918
919 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
920 static int coords_in_mouse_face_p (struct window *, int, int);
921
922
923 \f
924 /***********************************************************************
925 Window display dimensions
926 ***********************************************************************/
927
928 /* Return the bottom boundary y-position for text lines in window W.
929 This is the first y position at which a line cannot start.
930 It is relative to the top of the window.
931
932 This is the height of W minus the height of a mode line, if any. */
933
934 INLINE int
935 window_text_bottom_y (struct window *w)
936 {
937 int height = WINDOW_TOTAL_HEIGHT (w);
938
939 if (WINDOW_WANTS_MODELINE_P (w))
940 height -= CURRENT_MODE_LINE_HEIGHT (w);
941 return height;
942 }
943
944 /* Return the pixel width of display area AREA of window W. AREA < 0
945 means return the total width of W, not including fringes to
946 the left and right of the window. */
947
948 INLINE int
949 window_box_width (struct window *w, int area)
950 {
951 int cols = XFASTINT (w->total_cols);
952 int pixels = 0;
953
954 if (!w->pseudo_window_p)
955 {
956 cols -= WINDOW_SCROLL_BAR_COLS (w);
957
958 if (area == TEXT_AREA)
959 {
960 if (INTEGERP (w->left_margin_cols))
961 cols -= XFASTINT (w->left_margin_cols);
962 if (INTEGERP (w->right_margin_cols))
963 cols -= XFASTINT (w->right_margin_cols);
964 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
965 }
966 else if (area == LEFT_MARGIN_AREA)
967 {
968 cols = (INTEGERP (w->left_margin_cols)
969 ? XFASTINT (w->left_margin_cols) : 0);
970 pixels = 0;
971 }
972 else if (area == RIGHT_MARGIN_AREA)
973 {
974 cols = (INTEGERP (w->right_margin_cols)
975 ? XFASTINT (w->right_margin_cols) : 0);
976 pixels = 0;
977 }
978 }
979
980 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
981 }
982
983
984 /* Return the pixel height of the display area of window W, not
985 including mode lines of W, if any. */
986
987 INLINE int
988 window_box_height (struct window *w)
989 {
990 struct frame *f = XFRAME (w->frame);
991 int height = WINDOW_TOTAL_HEIGHT (w);
992
993 xassert (height >= 0);
994
995 /* Note: the code below that determines the mode-line/header-line
996 height is essentially the same as that contained in the macro
997 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
998 the appropriate glyph row has its `mode_line_p' flag set,
999 and if it doesn't, uses estimate_mode_line_height instead. */
1000
1001 if (WINDOW_WANTS_MODELINE_P (w))
1002 {
1003 struct glyph_row *ml_row
1004 = (w->current_matrix && w->current_matrix->rows
1005 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1006 : 0);
1007 if (ml_row && ml_row->mode_line_p)
1008 height -= ml_row->height;
1009 else
1010 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1011 }
1012
1013 if (WINDOW_WANTS_HEADER_LINE_P (w))
1014 {
1015 struct glyph_row *hl_row
1016 = (w->current_matrix && w->current_matrix->rows
1017 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1018 : 0);
1019 if (hl_row && hl_row->mode_line_p)
1020 height -= hl_row->height;
1021 else
1022 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1023 }
1024
1025 /* With a very small font and a mode-line that's taller than
1026 default, we might end up with a negative height. */
1027 return max (0, height);
1028 }
1029
1030 /* Return the window-relative coordinate of the left edge of display
1031 area AREA of window W. AREA < 0 means return the left edge of the
1032 whole window, to the right of the left fringe of W. */
1033
1034 INLINE int
1035 window_box_left_offset (struct window *w, int area)
1036 {
1037 int x;
1038
1039 if (w->pseudo_window_p)
1040 return 0;
1041
1042 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1043
1044 if (area == TEXT_AREA)
1045 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1046 + window_box_width (w, LEFT_MARGIN_AREA));
1047 else if (area == RIGHT_MARGIN_AREA)
1048 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1049 + window_box_width (w, LEFT_MARGIN_AREA)
1050 + window_box_width (w, TEXT_AREA)
1051 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1052 ? 0
1053 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1054 else if (area == LEFT_MARGIN_AREA
1055 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1056 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1057
1058 return x;
1059 }
1060
1061
1062 /* Return the window-relative coordinate of the right edge of display
1063 area AREA of window W. AREA < 0 means return the right edge of the
1064 whole window, to the left of the right fringe of W. */
1065
1066 INLINE int
1067 window_box_right_offset (struct window *w, int area)
1068 {
1069 return window_box_left_offset (w, area) + window_box_width (w, area);
1070 }
1071
1072 /* Return the frame-relative coordinate of the left edge of display
1073 area AREA of window W. AREA < 0 means return the left edge of the
1074 whole window, to the right of the left fringe of W. */
1075
1076 INLINE int
1077 window_box_left (struct window *w, int area)
1078 {
1079 struct frame *f = XFRAME (w->frame);
1080 int x;
1081
1082 if (w->pseudo_window_p)
1083 return FRAME_INTERNAL_BORDER_WIDTH (f);
1084
1085 x = (WINDOW_LEFT_EDGE_X (w)
1086 + window_box_left_offset (w, area));
1087
1088 return x;
1089 }
1090
1091
1092 /* Return the frame-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the right edge of the
1094 whole window, to the left of the right fringe of W. */
1095
1096 INLINE int
1097 window_box_right (struct window *w, int area)
1098 {
1099 return window_box_left (w, area) + window_box_width (w, area);
1100 }
1101
1102 /* Get the bounding box of the display area AREA of window W, without
1103 mode lines, in frame-relative coordinates. AREA < 0 means the
1104 whole window, not including the left and right fringes of
1105 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1106 coordinates of the upper-left corner of the box. Return in
1107 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1108
1109 INLINE void
1110 window_box (struct window *w, int area, int *box_x, int *box_y,
1111 int *box_width, int *box_height)
1112 {
1113 if (box_width)
1114 *box_width = window_box_width (w, area);
1115 if (box_height)
1116 *box_height = window_box_height (w);
1117 if (box_x)
1118 *box_x = window_box_left (w, area);
1119 if (box_y)
1120 {
1121 *box_y = WINDOW_TOP_EDGE_Y (w);
1122 if (WINDOW_WANTS_HEADER_LINE_P (w))
1123 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1124 }
1125 }
1126
1127
1128 /* Get the bounding box of the display area AREA of window W, without
1129 mode lines. AREA < 0 means the whole window, not including the
1130 left and right fringe of the window. Return in *TOP_LEFT_X
1131 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1132 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1133 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1134 box. */
1135
1136 static INLINE void
1137 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1138 int *bottom_right_x, int *bottom_right_y)
1139 {
1140 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1141 bottom_right_y);
1142 *bottom_right_x += *top_left_x;
1143 *bottom_right_y += *top_left_y;
1144 }
1145
1146
1147 \f
1148 /***********************************************************************
1149 Utilities
1150 ***********************************************************************/
1151
1152 /* Return the bottom y-position of the line the iterator IT is in.
1153 This can modify IT's settings. */
1154
1155 int
1156 line_bottom_y (struct it *it)
1157 {
1158 int line_height = it->max_ascent + it->max_descent;
1159 int line_top_y = it->current_y;
1160
1161 if (line_height == 0)
1162 {
1163 if (last_height)
1164 line_height = last_height;
1165 else if (IT_CHARPOS (*it) < ZV)
1166 {
1167 move_it_by_lines (it, 1);
1168 line_height = (it->max_ascent || it->max_descent
1169 ? it->max_ascent + it->max_descent
1170 : last_height);
1171 }
1172 else
1173 {
1174 struct glyph_row *row = it->glyph_row;
1175
1176 /* Use the default character height. */
1177 it->glyph_row = NULL;
1178 it->what = IT_CHARACTER;
1179 it->c = ' ';
1180 it->len = 1;
1181 PRODUCE_GLYPHS (it);
1182 line_height = it->ascent + it->descent;
1183 it->glyph_row = row;
1184 }
1185 }
1186
1187 return line_top_y + line_height;
1188 }
1189
1190
1191 /* Return 1 if position CHARPOS is visible in window W.
1192 CHARPOS < 0 means return info about WINDOW_END position.
1193 If visible, set *X and *Y to pixel coordinates of top left corner.
1194 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1195 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1196
1197 int
1198 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1199 int *rtop, int *rbot, int *rowh, int *vpos)
1200 {
1201 struct it it;
1202 struct text_pos top;
1203 int visible_p = 0;
1204 struct buffer *old_buffer = NULL;
1205
1206 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1207 return visible_p;
1208
1209 if (XBUFFER (w->buffer) != current_buffer)
1210 {
1211 old_buffer = current_buffer;
1212 set_buffer_internal_1 (XBUFFER (w->buffer));
1213 }
1214
1215 SET_TEXT_POS_FROM_MARKER (top, w->start);
1216
1217 /* Compute exact mode line heights. */
1218 if (WINDOW_WANTS_MODELINE_P (w))
1219 current_mode_line_height
1220 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1221 BVAR (current_buffer, mode_line_format));
1222
1223 if (WINDOW_WANTS_HEADER_LINE_P (w))
1224 current_header_line_height
1225 = display_mode_line (w, HEADER_LINE_FACE_ID,
1226 BVAR (current_buffer, header_line_format));
1227
1228 start_display (&it, w, top);
1229 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1230 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1231
1232 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1233 {
1234 /* We have reached CHARPOS, or passed it. How the call to
1235 move_it_to can overshoot: (i) If CHARPOS is on invisible
1236 text, move_it_to stops at the end of the invisible text,
1237 after CHARPOS. (ii) If CHARPOS is in a display vector,
1238 move_it_to stops on its last glyph. */
1239 int top_x = it.current_x;
1240 int top_y = it.current_y;
1241 enum it_method it_method = it.method;
1242 /* Calling line_bottom_y may change it.method, it.position, etc. */
1243 int bottom_y = (last_height = 0, line_bottom_y (&it));
1244 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1245
1246 if (top_y < window_top_y)
1247 visible_p = bottom_y > window_top_y;
1248 else if (top_y < it.last_visible_y)
1249 visible_p = 1;
1250 if (visible_p)
1251 {
1252 if (it_method == GET_FROM_DISPLAY_VECTOR)
1253 {
1254 /* We stopped on the last glyph of a display vector.
1255 Try and recompute. Hack alert! */
1256 if (charpos < 2 || top.charpos >= charpos)
1257 top_x = it.glyph_row->x;
1258 else
1259 {
1260 struct it it2;
1261 start_display (&it2, w, top);
1262 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1263 get_next_display_element (&it2);
1264 PRODUCE_GLYPHS (&it2);
1265 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1266 || it2.current_x > it2.last_visible_x)
1267 top_x = it.glyph_row->x;
1268 else
1269 {
1270 top_x = it2.current_x;
1271 top_y = it2.current_y;
1272 }
1273 }
1274 }
1275
1276 *x = top_x;
1277 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1278 *rtop = max (0, window_top_y - top_y);
1279 *rbot = max (0, bottom_y - it.last_visible_y);
1280 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1281 - max (top_y, window_top_y)));
1282 *vpos = it.vpos;
1283 }
1284 }
1285 else
1286 {
1287 struct it it2;
1288
1289 it2 = it;
1290 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1291 move_it_by_lines (&it, 1);
1292 if (charpos < IT_CHARPOS (it)
1293 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1294 {
1295 visible_p = 1;
1296 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1297 *x = it2.current_x;
1298 *y = it2.current_y + it2.max_ascent - it2.ascent;
1299 *rtop = max (0, -it2.current_y);
1300 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1301 - it.last_visible_y));
1302 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1303 it.last_visible_y)
1304 - max (it2.current_y,
1305 WINDOW_HEADER_LINE_HEIGHT (w))));
1306 *vpos = it2.vpos;
1307 }
1308 }
1309
1310 if (old_buffer)
1311 set_buffer_internal_1 (old_buffer);
1312
1313 current_header_line_height = current_mode_line_height = -1;
1314
1315 if (visible_p && XFASTINT (w->hscroll) > 0)
1316 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1317
1318 #if 0
1319 /* Debugging code. */
1320 if (visible_p)
1321 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1322 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1323 else
1324 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1325 #endif
1326
1327 return visible_p;
1328 }
1329
1330
1331 /* Return the next character from STR. Return in *LEN the length of
1332 the character. This is like STRING_CHAR_AND_LENGTH but never
1333 returns an invalid character. If we find one, we return a `?', but
1334 with the length of the invalid character. */
1335
1336 static INLINE int
1337 string_char_and_length (const unsigned char *str, int *len)
1338 {
1339 int c;
1340
1341 c = STRING_CHAR_AND_LENGTH (str, *len);
1342 if (!CHAR_VALID_P (c, 1))
1343 /* We may not change the length here because other places in Emacs
1344 don't use this function, i.e. they silently accept invalid
1345 characters. */
1346 c = '?';
1347
1348 return c;
1349 }
1350
1351
1352
1353 /* Given a position POS containing a valid character and byte position
1354 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1355
1356 static struct text_pos
1357 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1358 {
1359 xassert (STRINGP (string) && nchars >= 0);
1360
1361 if (STRING_MULTIBYTE (string))
1362 {
1363 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1364 int len;
1365
1366 while (nchars--)
1367 {
1368 string_char_and_length (p, &len);
1369 p += len;
1370 CHARPOS (pos) += 1;
1371 BYTEPOS (pos) += len;
1372 }
1373 }
1374 else
1375 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1376
1377 return pos;
1378 }
1379
1380
1381 /* Value is the text position, i.e. character and byte position,
1382 for character position CHARPOS in STRING. */
1383
1384 static INLINE struct text_pos
1385 string_pos (EMACS_INT charpos, Lisp_Object string)
1386 {
1387 struct text_pos pos;
1388 xassert (STRINGP (string));
1389 xassert (charpos >= 0);
1390 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1391 return pos;
1392 }
1393
1394
1395 /* Value is a text position, i.e. character and byte position, for
1396 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1397 means recognize multibyte characters. */
1398
1399 static struct text_pos
1400 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1401 {
1402 struct text_pos pos;
1403
1404 xassert (s != NULL);
1405 xassert (charpos >= 0);
1406
1407 if (multibyte_p)
1408 {
1409 int len;
1410
1411 SET_TEXT_POS (pos, 0, 0);
1412 while (charpos--)
1413 {
1414 string_char_and_length ((const unsigned char *) s, &len);
1415 s += len;
1416 CHARPOS (pos) += 1;
1417 BYTEPOS (pos) += len;
1418 }
1419 }
1420 else
1421 SET_TEXT_POS (pos, charpos, charpos);
1422
1423 return pos;
1424 }
1425
1426
1427 /* Value is the number of characters in C string S. MULTIBYTE_P
1428 non-zero means recognize multibyte characters. */
1429
1430 static EMACS_INT
1431 number_of_chars (const char *s, int multibyte_p)
1432 {
1433 EMACS_INT nchars;
1434
1435 if (multibyte_p)
1436 {
1437 EMACS_INT rest = strlen (s);
1438 int len;
1439 const unsigned char *p = (const unsigned char *) s;
1440
1441 for (nchars = 0; rest > 0; ++nchars)
1442 {
1443 string_char_and_length (p, &len);
1444 rest -= len, p += len;
1445 }
1446 }
1447 else
1448 nchars = strlen (s);
1449
1450 return nchars;
1451 }
1452
1453
1454 /* Compute byte position NEWPOS->bytepos corresponding to
1455 NEWPOS->charpos. POS is a known position in string STRING.
1456 NEWPOS->charpos must be >= POS.charpos. */
1457
1458 static void
1459 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1460 {
1461 xassert (STRINGP (string));
1462 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1463
1464 if (STRING_MULTIBYTE (string))
1465 *newpos = string_pos_nchars_ahead (pos, string,
1466 CHARPOS (*newpos) - CHARPOS (pos));
1467 else
1468 BYTEPOS (*newpos) = CHARPOS (*newpos);
1469 }
1470
1471 /* EXPORT:
1472 Return an estimation of the pixel height of mode or header lines on
1473 frame F. FACE_ID specifies what line's height to estimate. */
1474
1475 int
1476 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1477 {
1478 #ifdef HAVE_WINDOW_SYSTEM
1479 if (FRAME_WINDOW_P (f))
1480 {
1481 int height = FONT_HEIGHT (FRAME_FONT (f));
1482
1483 /* This function is called so early when Emacs starts that the face
1484 cache and mode line face are not yet initialized. */
1485 if (FRAME_FACE_CACHE (f))
1486 {
1487 struct face *face = FACE_FROM_ID (f, face_id);
1488 if (face)
1489 {
1490 if (face->font)
1491 height = FONT_HEIGHT (face->font);
1492 if (face->box_line_width > 0)
1493 height += 2 * face->box_line_width;
1494 }
1495 }
1496
1497 return height;
1498 }
1499 #endif
1500
1501 return 1;
1502 }
1503
1504 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1505 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1506 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1507 not force the value into range. */
1508
1509 void
1510 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1511 int *x, int *y, NativeRectangle *bounds, int noclip)
1512 {
1513
1514 #ifdef HAVE_WINDOW_SYSTEM
1515 if (FRAME_WINDOW_P (f))
1516 {
1517 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1518 even for negative values. */
1519 if (pix_x < 0)
1520 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1521 if (pix_y < 0)
1522 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1523
1524 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1525 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1526
1527 if (bounds)
1528 STORE_NATIVE_RECT (*bounds,
1529 FRAME_COL_TO_PIXEL_X (f, pix_x),
1530 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1531 FRAME_COLUMN_WIDTH (f) - 1,
1532 FRAME_LINE_HEIGHT (f) - 1);
1533
1534 if (!noclip)
1535 {
1536 if (pix_x < 0)
1537 pix_x = 0;
1538 else if (pix_x > FRAME_TOTAL_COLS (f))
1539 pix_x = FRAME_TOTAL_COLS (f);
1540
1541 if (pix_y < 0)
1542 pix_y = 0;
1543 else if (pix_y > FRAME_LINES (f))
1544 pix_y = FRAME_LINES (f);
1545 }
1546 }
1547 #endif
1548
1549 *x = pix_x;
1550 *y = pix_y;
1551 }
1552
1553
1554 /* Find the glyph under window-relative coordinates X/Y in window W.
1555 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1556 strings. Return in *HPOS and *VPOS the row and column number of
1557 the glyph found. Return in *AREA the glyph area containing X.
1558 Value is a pointer to the glyph found or null if X/Y is not on
1559 text, or we can't tell because W's current matrix is not up to
1560 date. */
1561
1562 static
1563 struct glyph *
1564 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1565 int *dx, int *dy, int *area)
1566 {
1567 struct glyph *glyph, *end;
1568 struct glyph_row *row = NULL;
1569 int x0, i;
1570
1571 /* Find row containing Y. Give up if some row is not enabled. */
1572 for (i = 0; i < w->current_matrix->nrows; ++i)
1573 {
1574 row = MATRIX_ROW (w->current_matrix, i);
1575 if (!row->enabled_p)
1576 return NULL;
1577 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1578 break;
1579 }
1580
1581 *vpos = i;
1582 *hpos = 0;
1583
1584 /* Give up if Y is not in the window. */
1585 if (i == w->current_matrix->nrows)
1586 return NULL;
1587
1588 /* Get the glyph area containing X. */
1589 if (w->pseudo_window_p)
1590 {
1591 *area = TEXT_AREA;
1592 x0 = 0;
1593 }
1594 else
1595 {
1596 if (x < window_box_left_offset (w, TEXT_AREA))
1597 {
1598 *area = LEFT_MARGIN_AREA;
1599 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1600 }
1601 else if (x < window_box_right_offset (w, TEXT_AREA))
1602 {
1603 *area = TEXT_AREA;
1604 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1605 }
1606 else
1607 {
1608 *area = RIGHT_MARGIN_AREA;
1609 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1610 }
1611 }
1612
1613 /* Find glyph containing X. */
1614 glyph = row->glyphs[*area];
1615 end = glyph + row->used[*area];
1616 x -= x0;
1617 while (glyph < end && x >= glyph->pixel_width)
1618 {
1619 x -= glyph->pixel_width;
1620 ++glyph;
1621 }
1622
1623 if (glyph == end)
1624 return NULL;
1625
1626 if (dx)
1627 {
1628 *dx = x;
1629 *dy = y - (row->y + row->ascent - glyph->ascent);
1630 }
1631
1632 *hpos = glyph - row->glyphs[*area];
1633 return glyph;
1634 }
1635
1636 /* Convert frame-relative x/y to coordinates relative to window W.
1637 Takes pseudo-windows into account. */
1638
1639 static void
1640 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1641 {
1642 if (w->pseudo_window_p)
1643 {
1644 /* A pseudo-window is always full-width, and starts at the
1645 left edge of the frame, plus a frame border. */
1646 struct frame *f = XFRAME (w->frame);
1647 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1648 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1649 }
1650 else
1651 {
1652 *x -= WINDOW_LEFT_EDGE_X (w);
1653 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1654 }
1655 }
1656
1657 #ifdef HAVE_WINDOW_SYSTEM
1658
1659 /* EXPORT:
1660 Return in RECTS[] at most N clipping rectangles for glyph string S.
1661 Return the number of stored rectangles. */
1662
1663 int
1664 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1665 {
1666 XRectangle r;
1667
1668 if (n <= 0)
1669 return 0;
1670
1671 if (s->row->full_width_p)
1672 {
1673 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1674 r.x = WINDOW_LEFT_EDGE_X (s->w);
1675 r.width = WINDOW_TOTAL_WIDTH (s->w);
1676
1677 /* Unless displaying a mode or menu bar line, which are always
1678 fully visible, clip to the visible part of the row. */
1679 if (s->w->pseudo_window_p)
1680 r.height = s->row->visible_height;
1681 else
1682 r.height = s->height;
1683 }
1684 else
1685 {
1686 /* This is a text line that may be partially visible. */
1687 r.x = window_box_left (s->w, s->area);
1688 r.width = window_box_width (s->w, s->area);
1689 r.height = s->row->visible_height;
1690 }
1691
1692 if (s->clip_head)
1693 if (r.x < s->clip_head->x)
1694 {
1695 if (r.width >= s->clip_head->x - r.x)
1696 r.width -= s->clip_head->x - r.x;
1697 else
1698 r.width = 0;
1699 r.x = s->clip_head->x;
1700 }
1701 if (s->clip_tail)
1702 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1703 {
1704 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1705 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1706 else
1707 r.width = 0;
1708 }
1709
1710 /* If S draws overlapping rows, it's sufficient to use the top and
1711 bottom of the window for clipping because this glyph string
1712 intentionally draws over other lines. */
1713 if (s->for_overlaps)
1714 {
1715 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1716 r.height = window_text_bottom_y (s->w) - r.y;
1717
1718 /* Alas, the above simple strategy does not work for the
1719 environments with anti-aliased text: if the same text is
1720 drawn onto the same place multiple times, it gets thicker.
1721 If the overlap we are processing is for the erased cursor, we
1722 take the intersection with the rectagle of the cursor. */
1723 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1724 {
1725 XRectangle rc, r_save = r;
1726
1727 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1728 rc.y = s->w->phys_cursor.y;
1729 rc.width = s->w->phys_cursor_width;
1730 rc.height = s->w->phys_cursor_height;
1731
1732 x_intersect_rectangles (&r_save, &rc, &r);
1733 }
1734 }
1735 else
1736 {
1737 /* Don't use S->y for clipping because it doesn't take partially
1738 visible lines into account. For example, it can be negative for
1739 partially visible lines at the top of a window. */
1740 if (!s->row->full_width_p
1741 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1742 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1743 else
1744 r.y = max (0, s->row->y);
1745 }
1746
1747 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1748
1749 /* If drawing the cursor, don't let glyph draw outside its
1750 advertised boundaries. Cleartype does this under some circumstances. */
1751 if (s->hl == DRAW_CURSOR)
1752 {
1753 struct glyph *glyph = s->first_glyph;
1754 int height, max_y;
1755
1756 if (s->x > r.x)
1757 {
1758 r.width -= s->x - r.x;
1759 r.x = s->x;
1760 }
1761 r.width = min (r.width, glyph->pixel_width);
1762
1763 /* If r.y is below window bottom, ensure that we still see a cursor. */
1764 height = min (glyph->ascent + glyph->descent,
1765 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1766 max_y = window_text_bottom_y (s->w) - height;
1767 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1768 if (s->ybase - glyph->ascent > max_y)
1769 {
1770 r.y = max_y;
1771 r.height = height;
1772 }
1773 else
1774 {
1775 /* Don't draw cursor glyph taller than our actual glyph. */
1776 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1777 if (height < r.height)
1778 {
1779 max_y = r.y + r.height;
1780 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1781 r.height = min (max_y - r.y, height);
1782 }
1783 }
1784 }
1785
1786 if (s->row->clip)
1787 {
1788 XRectangle r_save = r;
1789
1790 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1791 r.width = 0;
1792 }
1793
1794 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1795 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1796 {
1797 #ifdef CONVERT_FROM_XRECT
1798 CONVERT_FROM_XRECT (r, *rects);
1799 #else
1800 *rects = r;
1801 #endif
1802 return 1;
1803 }
1804 else
1805 {
1806 /* If we are processing overlapping and allowed to return
1807 multiple clipping rectangles, we exclude the row of the glyph
1808 string from the clipping rectangle. This is to avoid drawing
1809 the same text on the environment with anti-aliasing. */
1810 #ifdef CONVERT_FROM_XRECT
1811 XRectangle rs[2];
1812 #else
1813 XRectangle *rs = rects;
1814 #endif
1815 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1816
1817 if (s->for_overlaps & OVERLAPS_PRED)
1818 {
1819 rs[i] = r;
1820 if (r.y + r.height > row_y)
1821 {
1822 if (r.y < row_y)
1823 rs[i].height = row_y - r.y;
1824 else
1825 rs[i].height = 0;
1826 }
1827 i++;
1828 }
1829 if (s->for_overlaps & OVERLAPS_SUCC)
1830 {
1831 rs[i] = r;
1832 if (r.y < row_y + s->row->visible_height)
1833 {
1834 if (r.y + r.height > row_y + s->row->visible_height)
1835 {
1836 rs[i].y = row_y + s->row->visible_height;
1837 rs[i].height = r.y + r.height - rs[i].y;
1838 }
1839 else
1840 rs[i].height = 0;
1841 }
1842 i++;
1843 }
1844
1845 n = i;
1846 #ifdef CONVERT_FROM_XRECT
1847 for (i = 0; i < n; i++)
1848 CONVERT_FROM_XRECT (rs[i], rects[i]);
1849 #endif
1850 return n;
1851 }
1852 }
1853
1854 /* EXPORT:
1855 Return in *NR the clipping rectangle for glyph string S. */
1856
1857 void
1858 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1859 {
1860 get_glyph_string_clip_rects (s, nr, 1);
1861 }
1862
1863
1864 /* EXPORT:
1865 Return the position and height of the phys cursor in window W.
1866 Set w->phys_cursor_width to width of phys cursor.
1867 */
1868
1869 void
1870 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1871 struct glyph *glyph, int *xp, int *yp, int *heightp)
1872 {
1873 struct frame *f = XFRAME (WINDOW_FRAME (w));
1874 int x, y, wd, h, h0, y0;
1875
1876 /* Compute the width of the rectangle to draw. If on a stretch
1877 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1878 rectangle as wide as the glyph, but use a canonical character
1879 width instead. */
1880 wd = glyph->pixel_width - 1;
1881 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1882 wd++; /* Why? */
1883 #endif
1884
1885 x = w->phys_cursor.x;
1886 if (x < 0)
1887 {
1888 wd += x;
1889 x = 0;
1890 }
1891
1892 if (glyph->type == STRETCH_GLYPH
1893 && !x_stretch_cursor_p)
1894 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1895 w->phys_cursor_width = wd;
1896
1897 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1898
1899 /* If y is below window bottom, ensure that we still see a cursor. */
1900 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1901
1902 h = max (h0, glyph->ascent + glyph->descent);
1903 h0 = min (h0, glyph->ascent + glyph->descent);
1904
1905 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1906 if (y < y0)
1907 {
1908 h = max (h - (y0 - y) + 1, h0);
1909 y = y0 - 1;
1910 }
1911 else
1912 {
1913 y0 = window_text_bottom_y (w) - h0;
1914 if (y > y0)
1915 {
1916 h += y - y0;
1917 y = y0;
1918 }
1919 }
1920
1921 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1922 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1923 *heightp = h;
1924 }
1925
1926 /*
1927 * Remember which glyph the mouse is over.
1928 */
1929
1930 void
1931 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1932 {
1933 Lisp_Object window;
1934 struct window *w;
1935 struct glyph_row *r, *gr, *end_row;
1936 enum window_part part;
1937 enum glyph_row_area area;
1938 int x, y, width, height;
1939
1940 /* Try to determine frame pixel position and size of the glyph under
1941 frame pixel coordinates X/Y on frame F. */
1942
1943 if (!f->glyphs_initialized_p
1944 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1945 NILP (window)))
1946 {
1947 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1948 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1949 goto virtual_glyph;
1950 }
1951
1952 w = XWINDOW (window);
1953 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1954 height = WINDOW_FRAME_LINE_HEIGHT (w);
1955
1956 x = window_relative_x_coord (w, part, gx);
1957 y = gy - WINDOW_TOP_EDGE_Y (w);
1958
1959 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1960 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1961
1962 if (w->pseudo_window_p)
1963 {
1964 area = TEXT_AREA;
1965 part = ON_MODE_LINE; /* Don't adjust margin. */
1966 goto text_glyph;
1967 }
1968
1969 switch (part)
1970 {
1971 case ON_LEFT_MARGIN:
1972 area = LEFT_MARGIN_AREA;
1973 goto text_glyph;
1974
1975 case ON_RIGHT_MARGIN:
1976 area = RIGHT_MARGIN_AREA;
1977 goto text_glyph;
1978
1979 case ON_HEADER_LINE:
1980 case ON_MODE_LINE:
1981 gr = (part == ON_HEADER_LINE
1982 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1983 : MATRIX_MODE_LINE_ROW (w->current_matrix));
1984 gy = gr->y;
1985 area = TEXT_AREA;
1986 goto text_glyph_row_found;
1987
1988 case ON_TEXT:
1989 area = TEXT_AREA;
1990
1991 text_glyph:
1992 gr = 0; gy = 0;
1993 for (; r <= end_row && r->enabled_p; ++r)
1994 if (r->y + r->height > y)
1995 {
1996 gr = r; gy = r->y;
1997 break;
1998 }
1999
2000 text_glyph_row_found:
2001 if (gr && gy <= y)
2002 {
2003 struct glyph *g = gr->glyphs[area];
2004 struct glyph *end = g + gr->used[area];
2005
2006 height = gr->height;
2007 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2008 if (gx + g->pixel_width > x)
2009 break;
2010
2011 if (g < end)
2012 {
2013 if (g->type == IMAGE_GLYPH)
2014 {
2015 /* Don't remember when mouse is over image, as
2016 image may have hot-spots. */
2017 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2018 return;
2019 }
2020 width = g->pixel_width;
2021 }
2022 else
2023 {
2024 /* Use nominal char spacing at end of line. */
2025 x -= gx;
2026 gx += (x / width) * width;
2027 }
2028
2029 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2030 gx += window_box_left_offset (w, area);
2031 }
2032 else
2033 {
2034 /* Use nominal line height at end of window. */
2035 gx = (x / width) * width;
2036 y -= gy;
2037 gy += (y / height) * height;
2038 }
2039 break;
2040
2041 case ON_LEFT_FRINGE:
2042 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2043 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2044 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2045 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2046 goto row_glyph;
2047
2048 case ON_RIGHT_FRINGE:
2049 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2050 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2051 : window_box_right_offset (w, TEXT_AREA));
2052 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2053 goto row_glyph;
2054
2055 case ON_SCROLL_BAR:
2056 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2057 ? 0
2058 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2059 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2060 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2061 : 0)));
2062 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2063
2064 row_glyph:
2065 gr = 0, gy = 0;
2066 for (; r <= end_row && r->enabled_p; ++r)
2067 if (r->y + r->height > y)
2068 {
2069 gr = r; gy = r->y;
2070 break;
2071 }
2072
2073 if (gr && gy <= y)
2074 height = gr->height;
2075 else
2076 {
2077 /* Use nominal line height at end of window. */
2078 y -= gy;
2079 gy += (y / height) * height;
2080 }
2081 break;
2082
2083 default:
2084 ;
2085 virtual_glyph:
2086 /* If there is no glyph under the mouse, then we divide the screen
2087 into a grid of the smallest glyph in the frame, and use that
2088 as our "glyph". */
2089
2090 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2091 round down even for negative values. */
2092 if (gx < 0)
2093 gx -= width - 1;
2094 if (gy < 0)
2095 gy -= height - 1;
2096
2097 gx = (gx / width) * width;
2098 gy = (gy / height) * height;
2099
2100 goto store_rect;
2101 }
2102
2103 gx += WINDOW_LEFT_EDGE_X (w);
2104 gy += WINDOW_TOP_EDGE_Y (w);
2105
2106 store_rect:
2107 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2108
2109 /* Visible feedback for debugging. */
2110 #if 0
2111 #if HAVE_X_WINDOWS
2112 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2113 f->output_data.x->normal_gc,
2114 gx, gy, width, height);
2115 #endif
2116 #endif
2117 }
2118
2119
2120 #endif /* HAVE_WINDOW_SYSTEM */
2121
2122 \f
2123 /***********************************************************************
2124 Lisp form evaluation
2125 ***********************************************************************/
2126
2127 /* Error handler for safe_eval and safe_call. */
2128
2129 static Lisp_Object
2130 safe_eval_handler (Lisp_Object arg)
2131 {
2132 add_to_log ("Error during redisplay: %S", arg, Qnil);
2133 return Qnil;
2134 }
2135
2136
2137 /* Evaluate SEXPR and return the result, or nil if something went
2138 wrong. Prevent redisplay during the evaluation. */
2139
2140 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2141 Return the result, or nil if something went wrong. Prevent
2142 redisplay during the evaluation. */
2143
2144 Lisp_Object
2145 safe_call (size_t nargs, Lisp_Object *args)
2146 {
2147 Lisp_Object val;
2148
2149 if (inhibit_eval_during_redisplay)
2150 val = Qnil;
2151 else
2152 {
2153 int count = SPECPDL_INDEX ();
2154 struct gcpro gcpro1;
2155
2156 GCPRO1 (args[0]);
2157 gcpro1.nvars = nargs;
2158 specbind (Qinhibit_redisplay, Qt);
2159 /* Use Qt to ensure debugger does not run,
2160 so there is no possibility of wanting to redisplay. */
2161 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2162 safe_eval_handler);
2163 UNGCPRO;
2164 val = unbind_to (count, val);
2165 }
2166
2167 return val;
2168 }
2169
2170
2171 /* Call function FN with one argument ARG.
2172 Return the result, or nil if something went wrong. */
2173
2174 Lisp_Object
2175 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2176 {
2177 Lisp_Object args[2];
2178 args[0] = fn;
2179 args[1] = arg;
2180 return safe_call (2, args);
2181 }
2182
2183 static Lisp_Object Qeval;
2184
2185 Lisp_Object
2186 safe_eval (Lisp_Object sexpr)
2187 {
2188 return safe_call1 (Qeval, sexpr);
2189 }
2190
2191 /* Call function FN with one argument ARG.
2192 Return the result, or nil if something went wrong. */
2193
2194 Lisp_Object
2195 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2196 {
2197 Lisp_Object args[3];
2198 args[0] = fn;
2199 args[1] = arg1;
2200 args[2] = arg2;
2201 return safe_call (3, args);
2202 }
2203
2204
2205 \f
2206 /***********************************************************************
2207 Debugging
2208 ***********************************************************************/
2209
2210 #if 0
2211
2212 /* Define CHECK_IT to perform sanity checks on iterators.
2213 This is for debugging. It is too slow to do unconditionally. */
2214
2215 static void
2216 check_it (it)
2217 struct it *it;
2218 {
2219 if (it->method == GET_FROM_STRING)
2220 {
2221 xassert (STRINGP (it->string));
2222 xassert (IT_STRING_CHARPOS (*it) >= 0);
2223 }
2224 else
2225 {
2226 xassert (IT_STRING_CHARPOS (*it) < 0);
2227 if (it->method == GET_FROM_BUFFER)
2228 {
2229 /* Check that character and byte positions agree. */
2230 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2231 }
2232 }
2233
2234 if (it->dpvec)
2235 xassert (it->current.dpvec_index >= 0);
2236 else
2237 xassert (it->current.dpvec_index < 0);
2238 }
2239
2240 #define CHECK_IT(IT) check_it ((IT))
2241
2242 #else /* not 0 */
2243
2244 #define CHECK_IT(IT) (void) 0
2245
2246 #endif /* not 0 */
2247
2248
2249 #if GLYPH_DEBUG
2250
2251 /* Check that the window end of window W is what we expect it
2252 to be---the last row in the current matrix displaying text. */
2253
2254 static void
2255 check_window_end (w)
2256 struct window *w;
2257 {
2258 if (!MINI_WINDOW_P (w)
2259 && !NILP (w->window_end_valid))
2260 {
2261 struct glyph_row *row;
2262 xassert ((row = MATRIX_ROW (w->current_matrix,
2263 XFASTINT (w->window_end_vpos)),
2264 !row->enabled_p
2265 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2266 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2267 }
2268 }
2269
2270 #define CHECK_WINDOW_END(W) check_window_end ((W))
2271
2272 #else /* not GLYPH_DEBUG */
2273
2274 #define CHECK_WINDOW_END(W) (void) 0
2275
2276 #endif /* not GLYPH_DEBUG */
2277
2278
2279 \f
2280 /***********************************************************************
2281 Iterator initialization
2282 ***********************************************************************/
2283
2284 /* Initialize IT for displaying current_buffer in window W, starting
2285 at character position CHARPOS. CHARPOS < 0 means that no buffer
2286 position is specified which is useful when the iterator is assigned
2287 a position later. BYTEPOS is the byte position corresponding to
2288 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2289
2290 If ROW is not null, calls to produce_glyphs with IT as parameter
2291 will produce glyphs in that row.
2292
2293 BASE_FACE_ID is the id of a base face to use. It must be one of
2294 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2295 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2296 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2297
2298 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2299 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2300 will be initialized to use the corresponding mode line glyph row of
2301 the desired matrix of W. */
2302
2303 void
2304 init_iterator (struct it *it, struct window *w,
2305 EMACS_INT charpos, EMACS_INT bytepos,
2306 struct glyph_row *row, enum face_id base_face_id)
2307 {
2308 int highlight_region_p;
2309 enum face_id remapped_base_face_id = base_face_id;
2310
2311 /* Some precondition checks. */
2312 xassert (w != NULL && it != NULL);
2313 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2314 && charpos <= ZV));
2315
2316 /* If face attributes have been changed since the last redisplay,
2317 free realized faces now because they depend on face definitions
2318 that might have changed. Don't free faces while there might be
2319 desired matrices pending which reference these faces. */
2320 if (face_change_count && !inhibit_free_realized_faces)
2321 {
2322 face_change_count = 0;
2323 free_all_realized_faces (Qnil);
2324 }
2325
2326 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2327 if (! NILP (Vface_remapping_alist))
2328 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2329
2330 /* Use one of the mode line rows of W's desired matrix if
2331 appropriate. */
2332 if (row == NULL)
2333 {
2334 if (base_face_id == MODE_LINE_FACE_ID
2335 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2336 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2337 else if (base_face_id == HEADER_LINE_FACE_ID)
2338 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2339 }
2340
2341 /* Clear IT. */
2342 memset (it, 0, sizeof *it);
2343 it->current.overlay_string_index = -1;
2344 it->current.dpvec_index = -1;
2345 it->base_face_id = remapped_base_face_id;
2346 it->string = Qnil;
2347 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2348 it->paragraph_embedding = L2R;
2349 it->bidi_it.string.lstring = Qnil;
2350 it->bidi_it.string.s = NULL;
2351 it->bidi_it.string.bufpos = 0;
2352
2353 /* The window in which we iterate over current_buffer: */
2354 XSETWINDOW (it->window, w);
2355 it->w = w;
2356 it->f = XFRAME (w->frame);
2357
2358 it->cmp_it.id = -1;
2359
2360 /* Extra space between lines (on window systems only). */
2361 if (base_face_id == DEFAULT_FACE_ID
2362 && FRAME_WINDOW_P (it->f))
2363 {
2364 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2365 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2366 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2367 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2368 * FRAME_LINE_HEIGHT (it->f));
2369 else if (it->f->extra_line_spacing > 0)
2370 it->extra_line_spacing = it->f->extra_line_spacing;
2371 it->max_extra_line_spacing = 0;
2372 }
2373
2374 /* If realized faces have been removed, e.g. because of face
2375 attribute changes of named faces, recompute them. When running
2376 in batch mode, the face cache of the initial frame is null. If
2377 we happen to get called, make a dummy face cache. */
2378 if (FRAME_FACE_CACHE (it->f) == NULL)
2379 init_frame_faces (it->f);
2380 if (FRAME_FACE_CACHE (it->f)->used == 0)
2381 recompute_basic_faces (it->f);
2382
2383 /* Current value of the `slice', `space-width', and 'height' properties. */
2384 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2385 it->space_width = Qnil;
2386 it->font_height = Qnil;
2387 it->override_ascent = -1;
2388
2389 /* Are control characters displayed as `^C'? */
2390 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2391
2392 /* -1 means everything between a CR and the following line end
2393 is invisible. >0 means lines indented more than this value are
2394 invisible. */
2395 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2396 ? XFASTINT (BVAR (current_buffer, selective_display))
2397 : (!NILP (BVAR (current_buffer, selective_display))
2398 ? -1 : 0));
2399 it->selective_display_ellipsis_p
2400 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2401
2402 /* Display table to use. */
2403 it->dp = window_display_table (w);
2404
2405 /* Are multibyte characters enabled in current_buffer? */
2406 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2407
2408 /* Non-zero if we should highlight the region. */
2409 highlight_region_p
2410 = (!NILP (Vtransient_mark_mode)
2411 && !NILP (BVAR (current_buffer, mark_active))
2412 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2413
2414 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2415 start and end of a visible region in window IT->w. Set both to
2416 -1 to indicate no region. */
2417 if (highlight_region_p
2418 /* Maybe highlight only in selected window. */
2419 && (/* Either show region everywhere. */
2420 highlight_nonselected_windows
2421 /* Or show region in the selected window. */
2422 || w == XWINDOW (selected_window)
2423 /* Or show the region if we are in the mini-buffer and W is
2424 the window the mini-buffer refers to. */
2425 || (MINI_WINDOW_P (XWINDOW (selected_window))
2426 && WINDOWP (minibuf_selected_window)
2427 && w == XWINDOW (minibuf_selected_window))))
2428 {
2429 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2430 it->region_beg_charpos = min (PT, markpos);
2431 it->region_end_charpos = max (PT, markpos);
2432 }
2433 else
2434 it->region_beg_charpos = it->region_end_charpos = -1;
2435
2436 /* Get the position at which the redisplay_end_trigger hook should
2437 be run, if it is to be run at all. */
2438 if (MARKERP (w->redisplay_end_trigger)
2439 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2440 it->redisplay_end_trigger_charpos
2441 = marker_position (w->redisplay_end_trigger);
2442 else if (INTEGERP (w->redisplay_end_trigger))
2443 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2444
2445 /* Correct bogus values of tab_width. */
2446 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2447 if (it->tab_width <= 0 || it->tab_width > 1000)
2448 it->tab_width = 8;
2449
2450 /* Are lines in the display truncated? */
2451 if (base_face_id != DEFAULT_FACE_ID
2452 || XINT (it->w->hscroll)
2453 || (! WINDOW_FULL_WIDTH_P (it->w)
2454 && ((!NILP (Vtruncate_partial_width_windows)
2455 && !INTEGERP (Vtruncate_partial_width_windows))
2456 || (INTEGERP (Vtruncate_partial_width_windows)
2457 && (WINDOW_TOTAL_COLS (it->w)
2458 < XINT (Vtruncate_partial_width_windows))))))
2459 it->line_wrap = TRUNCATE;
2460 else if (NILP (BVAR (current_buffer, truncate_lines)))
2461 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2462 ? WINDOW_WRAP : WORD_WRAP;
2463 else
2464 it->line_wrap = TRUNCATE;
2465
2466 /* Get dimensions of truncation and continuation glyphs. These are
2467 displayed as fringe bitmaps under X, so we don't need them for such
2468 frames. */
2469 if (!FRAME_WINDOW_P (it->f))
2470 {
2471 if (it->line_wrap == TRUNCATE)
2472 {
2473 /* We will need the truncation glyph. */
2474 xassert (it->glyph_row == NULL);
2475 produce_special_glyphs (it, IT_TRUNCATION);
2476 it->truncation_pixel_width = it->pixel_width;
2477 }
2478 else
2479 {
2480 /* We will need the continuation glyph. */
2481 xassert (it->glyph_row == NULL);
2482 produce_special_glyphs (it, IT_CONTINUATION);
2483 it->continuation_pixel_width = it->pixel_width;
2484 }
2485
2486 /* Reset these values to zero because the produce_special_glyphs
2487 above has changed them. */
2488 it->pixel_width = it->ascent = it->descent = 0;
2489 it->phys_ascent = it->phys_descent = 0;
2490 }
2491
2492 /* Set this after getting the dimensions of truncation and
2493 continuation glyphs, so that we don't produce glyphs when calling
2494 produce_special_glyphs, above. */
2495 it->glyph_row = row;
2496 it->area = TEXT_AREA;
2497
2498 /* Forget any previous info about this row being reversed. */
2499 if (it->glyph_row)
2500 it->glyph_row->reversed_p = 0;
2501
2502 /* Get the dimensions of the display area. The display area
2503 consists of the visible window area plus a horizontally scrolled
2504 part to the left of the window. All x-values are relative to the
2505 start of this total display area. */
2506 if (base_face_id != DEFAULT_FACE_ID)
2507 {
2508 /* Mode lines, menu bar in terminal frames. */
2509 it->first_visible_x = 0;
2510 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2511 }
2512 else
2513 {
2514 it->first_visible_x
2515 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2516 it->last_visible_x = (it->first_visible_x
2517 + window_box_width (w, TEXT_AREA));
2518
2519 /* If we truncate lines, leave room for the truncator glyph(s) at
2520 the right margin. Otherwise, leave room for the continuation
2521 glyph(s). Truncation and continuation glyphs are not inserted
2522 for window-based redisplay. */
2523 if (!FRAME_WINDOW_P (it->f))
2524 {
2525 if (it->line_wrap == TRUNCATE)
2526 it->last_visible_x -= it->truncation_pixel_width;
2527 else
2528 it->last_visible_x -= it->continuation_pixel_width;
2529 }
2530
2531 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2532 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2533 }
2534
2535 /* Leave room for a border glyph. */
2536 if (!FRAME_WINDOW_P (it->f)
2537 && !WINDOW_RIGHTMOST_P (it->w))
2538 it->last_visible_x -= 1;
2539
2540 it->last_visible_y = window_text_bottom_y (w);
2541
2542 /* For mode lines and alike, arrange for the first glyph having a
2543 left box line if the face specifies a box. */
2544 if (base_face_id != DEFAULT_FACE_ID)
2545 {
2546 struct face *face;
2547
2548 it->face_id = remapped_base_face_id;
2549
2550 /* If we have a boxed mode line, make the first character appear
2551 with a left box line. */
2552 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2553 if (face->box != FACE_NO_BOX)
2554 it->start_of_box_run_p = 1;
2555 }
2556
2557 /* If a buffer position was specified, set the iterator there,
2558 getting overlays and face properties from that position. */
2559 if (charpos >= BUF_BEG (current_buffer))
2560 {
2561 it->end_charpos = ZV;
2562 it->face_id = -1;
2563 IT_CHARPOS (*it) = charpos;
2564
2565 /* Compute byte position if not specified. */
2566 if (bytepos < charpos)
2567 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2568 else
2569 IT_BYTEPOS (*it) = bytepos;
2570
2571 it->start = it->current;
2572 /* Do we need to reorder bidirectional text? Not if this is a
2573 unibyte buffer: by definition, none of the single-byte
2574 characters are strong R2L, so no reordering is needed. And
2575 bidi.c doesn't support unibyte buffers anyway. */
2576 it->bidi_p =
2577 !NILP (BVAR (current_buffer, bidi_display_reordering))
2578 && it->multibyte_p;
2579
2580 /* If we are to reorder bidirectional text, init the bidi
2581 iterator. */
2582 if (it->bidi_p)
2583 {
2584 /* Note the paragraph direction that this buffer wants to
2585 use. */
2586 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2587 Qleft_to_right))
2588 it->paragraph_embedding = L2R;
2589 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2590 Qright_to_left))
2591 it->paragraph_embedding = R2L;
2592 else
2593 it->paragraph_embedding = NEUTRAL_DIR;
2594 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2595 &it->bidi_it);
2596 }
2597
2598 /* Compute faces etc. */
2599 reseat (it, it->current.pos, 1);
2600 }
2601
2602 CHECK_IT (it);
2603 }
2604
2605
2606 /* Initialize IT for the display of window W with window start POS. */
2607
2608 void
2609 start_display (struct it *it, struct window *w, struct text_pos pos)
2610 {
2611 struct glyph_row *row;
2612 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2613
2614 row = w->desired_matrix->rows + first_vpos;
2615 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2616 it->first_vpos = first_vpos;
2617
2618 /* Don't reseat to previous visible line start if current start
2619 position is in a string or image. */
2620 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2621 {
2622 int start_at_line_beg_p;
2623 int first_y = it->current_y;
2624
2625 /* If window start is not at a line start, skip forward to POS to
2626 get the correct continuation lines width. */
2627 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2628 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2629 if (!start_at_line_beg_p)
2630 {
2631 int new_x;
2632
2633 reseat_at_previous_visible_line_start (it);
2634 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2635
2636 new_x = it->current_x + it->pixel_width;
2637
2638 /* If lines are continued, this line may end in the middle
2639 of a multi-glyph character (e.g. a control character
2640 displayed as \003, or in the middle of an overlay
2641 string). In this case move_it_to above will not have
2642 taken us to the start of the continuation line but to the
2643 end of the continued line. */
2644 if (it->current_x > 0
2645 && it->line_wrap != TRUNCATE /* Lines are continued. */
2646 && (/* And glyph doesn't fit on the line. */
2647 new_x > it->last_visible_x
2648 /* Or it fits exactly and we're on a window
2649 system frame. */
2650 || (new_x == it->last_visible_x
2651 && FRAME_WINDOW_P (it->f))))
2652 {
2653 if (it->current.dpvec_index >= 0
2654 || it->current.overlay_string_index >= 0)
2655 {
2656 set_iterator_to_next (it, 1);
2657 move_it_in_display_line_to (it, -1, -1, 0);
2658 }
2659
2660 it->continuation_lines_width += it->current_x;
2661 }
2662
2663 /* We're starting a new display line, not affected by the
2664 height of the continued line, so clear the appropriate
2665 fields in the iterator structure. */
2666 it->max_ascent = it->max_descent = 0;
2667 it->max_phys_ascent = it->max_phys_descent = 0;
2668
2669 it->current_y = first_y;
2670 it->vpos = 0;
2671 it->current_x = it->hpos = 0;
2672 }
2673 }
2674 }
2675
2676
2677 /* Return 1 if POS is a position in ellipses displayed for invisible
2678 text. W is the window we display, for text property lookup. */
2679
2680 static int
2681 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2682 {
2683 Lisp_Object prop, window;
2684 int ellipses_p = 0;
2685 EMACS_INT charpos = CHARPOS (pos->pos);
2686
2687 /* If POS specifies a position in a display vector, this might
2688 be for an ellipsis displayed for invisible text. We won't
2689 get the iterator set up for delivering that ellipsis unless
2690 we make sure that it gets aware of the invisible text. */
2691 if (pos->dpvec_index >= 0
2692 && pos->overlay_string_index < 0
2693 && CHARPOS (pos->string_pos) < 0
2694 && charpos > BEGV
2695 && (XSETWINDOW (window, w),
2696 prop = Fget_char_property (make_number (charpos),
2697 Qinvisible, window),
2698 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2699 {
2700 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2701 window);
2702 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2703 }
2704
2705 return ellipses_p;
2706 }
2707
2708
2709 /* Initialize IT for stepping through current_buffer in window W,
2710 starting at position POS that includes overlay string and display
2711 vector/ control character translation position information. Value
2712 is zero if there are overlay strings with newlines at POS. */
2713
2714 static int
2715 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2716 {
2717 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2718 int i, overlay_strings_with_newlines = 0;
2719
2720 /* If POS specifies a position in a display vector, this might
2721 be for an ellipsis displayed for invisible text. We won't
2722 get the iterator set up for delivering that ellipsis unless
2723 we make sure that it gets aware of the invisible text. */
2724 if (in_ellipses_for_invisible_text_p (pos, w))
2725 {
2726 --charpos;
2727 bytepos = 0;
2728 }
2729
2730 /* Keep in mind: the call to reseat in init_iterator skips invisible
2731 text, so we might end up at a position different from POS. This
2732 is only a problem when POS is a row start after a newline and an
2733 overlay starts there with an after-string, and the overlay has an
2734 invisible property. Since we don't skip invisible text in
2735 display_line and elsewhere immediately after consuming the
2736 newline before the row start, such a POS will not be in a string,
2737 but the call to init_iterator below will move us to the
2738 after-string. */
2739 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2740
2741 /* This only scans the current chunk -- it should scan all chunks.
2742 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2743 to 16 in 22.1 to make this a lesser problem. */
2744 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2745 {
2746 const char *s = SSDATA (it->overlay_strings[i]);
2747 const char *e = s + SBYTES (it->overlay_strings[i]);
2748
2749 while (s < e && *s != '\n')
2750 ++s;
2751
2752 if (s < e)
2753 {
2754 overlay_strings_with_newlines = 1;
2755 break;
2756 }
2757 }
2758
2759 /* If position is within an overlay string, set up IT to the right
2760 overlay string. */
2761 if (pos->overlay_string_index >= 0)
2762 {
2763 int relative_index;
2764
2765 /* If the first overlay string happens to have a `display'
2766 property for an image, the iterator will be set up for that
2767 image, and we have to undo that setup first before we can
2768 correct the overlay string index. */
2769 if (it->method == GET_FROM_IMAGE)
2770 pop_it (it);
2771
2772 /* We already have the first chunk of overlay strings in
2773 IT->overlay_strings. Load more until the one for
2774 pos->overlay_string_index is in IT->overlay_strings. */
2775 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2776 {
2777 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2778 it->current.overlay_string_index = 0;
2779 while (n--)
2780 {
2781 load_overlay_strings (it, 0);
2782 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2783 }
2784 }
2785
2786 it->current.overlay_string_index = pos->overlay_string_index;
2787 relative_index = (it->current.overlay_string_index
2788 % OVERLAY_STRING_CHUNK_SIZE);
2789 it->string = it->overlay_strings[relative_index];
2790 xassert (STRINGP (it->string));
2791 it->current.string_pos = pos->string_pos;
2792 it->method = GET_FROM_STRING;
2793 }
2794
2795 if (CHARPOS (pos->string_pos) >= 0)
2796 {
2797 /* Recorded position is not in an overlay string, but in another
2798 string. This can only be a string from a `display' property.
2799 IT should already be filled with that string. */
2800 it->current.string_pos = pos->string_pos;
2801 xassert (STRINGP (it->string));
2802 }
2803
2804 /* Restore position in display vector translations, control
2805 character translations or ellipses. */
2806 if (pos->dpvec_index >= 0)
2807 {
2808 if (it->dpvec == NULL)
2809 get_next_display_element (it);
2810 xassert (it->dpvec && it->current.dpvec_index == 0);
2811 it->current.dpvec_index = pos->dpvec_index;
2812 }
2813
2814 CHECK_IT (it);
2815 return !overlay_strings_with_newlines;
2816 }
2817
2818
2819 /* Initialize IT for stepping through current_buffer in window W
2820 starting at ROW->start. */
2821
2822 static void
2823 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2824 {
2825 init_from_display_pos (it, w, &row->start);
2826 it->start = row->start;
2827 it->continuation_lines_width = row->continuation_lines_width;
2828 CHECK_IT (it);
2829 }
2830
2831
2832 /* Initialize IT for stepping through current_buffer in window W
2833 starting in the line following ROW, i.e. starting at ROW->end.
2834 Value is zero if there are overlay strings with newlines at ROW's
2835 end position. */
2836
2837 static int
2838 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2839 {
2840 int success = 0;
2841
2842 if (init_from_display_pos (it, w, &row->end))
2843 {
2844 if (row->continued_p)
2845 it->continuation_lines_width
2846 = row->continuation_lines_width + row->pixel_width;
2847 CHECK_IT (it);
2848 success = 1;
2849 }
2850
2851 return success;
2852 }
2853
2854
2855
2856 \f
2857 /***********************************************************************
2858 Text properties
2859 ***********************************************************************/
2860
2861 /* Called when IT reaches IT->stop_charpos. Handle text property and
2862 overlay changes. Set IT->stop_charpos to the next position where
2863 to stop. */
2864
2865 static void
2866 handle_stop (struct it *it)
2867 {
2868 enum prop_handled handled;
2869 int handle_overlay_change_p;
2870 struct props *p;
2871
2872 it->dpvec = NULL;
2873 it->current.dpvec_index = -1;
2874 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2875 it->ignore_overlay_strings_at_pos_p = 0;
2876 it->ellipsis_p = 0;
2877
2878 /* Use face of preceding text for ellipsis (if invisible) */
2879 if (it->selective_display_ellipsis_p)
2880 it->saved_face_id = it->face_id;
2881
2882 do
2883 {
2884 handled = HANDLED_NORMALLY;
2885
2886 /* Call text property handlers. */
2887 for (p = it_props; p->handler; ++p)
2888 {
2889 handled = p->handler (it);
2890
2891 if (handled == HANDLED_RECOMPUTE_PROPS)
2892 break;
2893 else if (handled == HANDLED_RETURN)
2894 {
2895 /* We still want to show before and after strings from
2896 overlays even if the actual buffer text is replaced. */
2897 if (!handle_overlay_change_p
2898 || it->sp > 1
2899 || !get_overlay_strings_1 (it, 0, 0))
2900 {
2901 if (it->ellipsis_p)
2902 setup_for_ellipsis (it, 0);
2903 /* When handling a display spec, we might load an
2904 empty string. In that case, discard it here. We
2905 used to discard it in handle_single_display_spec,
2906 but that causes get_overlay_strings_1, above, to
2907 ignore overlay strings that we must check. */
2908 if (STRINGP (it->string) && !SCHARS (it->string))
2909 pop_it (it);
2910 return;
2911 }
2912 else if (STRINGP (it->string) && !SCHARS (it->string))
2913 pop_it (it);
2914 else
2915 {
2916 it->ignore_overlay_strings_at_pos_p = 1;
2917 it->string_from_display_prop_p = 0;
2918 handle_overlay_change_p = 0;
2919 }
2920 handled = HANDLED_RECOMPUTE_PROPS;
2921 break;
2922 }
2923 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2924 handle_overlay_change_p = 0;
2925 }
2926
2927 if (handled != HANDLED_RECOMPUTE_PROPS)
2928 {
2929 /* Don't check for overlay strings below when set to deliver
2930 characters from a display vector. */
2931 if (it->method == GET_FROM_DISPLAY_VECTOR)
2932 handle_overlay_change_p = 0;
2933
2934 /* Handle overlay changes.
2935 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2936 if it finds overlays. */
2937 if (handle_overlay_change_p)
2938 handled = handle_overlay_change (it);
2939 }
2940
2941 if (it->ellipsis_p)
2942 {
2943 setup_for_ellipsis (it, 0);
2944 break;
2945 }
2946 }
2947 while (handled == HANDLED_RECOMPUTE_PROPS);
2948
2949 /* Determine where to stop next. */
2950 if (handled == HANDLED_NORMALLY)
2951 compute_stop_pos (it);
2952 }
2953
2954
2955 /* Compute IT->stop_charpos from text property and overlay change
2956 information for IT's current position. */
2957
2958 static void
2959 compute_stop_pos (struct it *it)
2960 {
2961 register INTERVAL iv, next_iv;
2962 Lisp_Object object, limit, position;
2963 EMACS_INT charpos, bytepos;
2964
2965 /* If nowhere else, stop at the end. */
2966 it->stop_charpos = it->end_charpos;
2967
2968 if (STRINGP (it->string))
2969 {
2970 /* Strings are usually short, so don't limit the search for
2971 properties. */
2972 object = it->string;
2973 limit = Qnil;
2974 charpos = IT_STRING_CHARPOS (*it);
2975 bytepos = IT_STRING_BYTEPOS (*it);
2976 }
2977 else
2978 {
2979 EMACS_INT pos;
2980
2981 /* If next overlay change is in front of the current stop pos
2982 (which is IT->end_charpos), stop there. Note: value of
2983 next_overlay_change is point-max if no overlay change
2984 follows. */
2985 charpos = IT_CHARPOS (*it);
2986 bytepos = IT_BYTEPOS (*it);
2987 pos = next_overlay_change (charpos);
2988 if (pos < it->stop_charpos)
2989 it->stop_charpos = pos;
2990
2991 /* If showing the region, we have to stop at the region
2992 start or end because the face might change there. */
2993 if (it->region_beg_charpos > 0)
2994 {
2995 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2996 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2997 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2998 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2999 }
3000
3001 /* Set up variables for computing the stop position from text
3002 property changes. */
3003 XSETBUFFER (object, current_buffer);
3004 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3005 }
3006
3007 /* Get the interval containing IT's position. Value is a null
3008 interval if there isn't such an interval. */
3009 position = make_number (charpos);
3010 iv = validate_interval_range (object, &position, &position, 0);
3011 if (!NULL_INTERVAL_P (iv))
3012 {
3013 Lisp_Object values_here[LAST_PROP_IDX];
3014 struct props *p;
3015
3016 /* Get properties here. */
3017 for (p = it_props; p->handler; ++p)
3018 values_here[p->idx] = textget (iv->plist, *p->name);
3019
3020 /* Look for an interval following iv that has different
3021 properties. */
3022 for (next_iv = next_interval (iv);
3023 (!NULL_INTERVAL_P (next_iv)
3024 && (NILP (limit)
3025 || XFASTINT (limit) > next_iv->position));
3026 next_iv = next_interval (next_iv))
3027 {
3028 for (p = it_props; p->handler; ++p)
3029 {
3030 Lisp_Object new_value;
3031
3032 new_value = textget (next_iv->plist, *p->name);
3033 if (!EQ (values_here[p->idx], new_value))
3034 break;
3035 }
3036
3037 if (p->handler)
3038 break;
3039 }
3040
3041 if (!NULL_INTERVAL_P (next_iv))
3042 {
3043 if (INTEGERP (limit)
3044 && next_iv->position >= XFASTINT (limit))
3045 /* No text property change up to limit. */
3046 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3047 else
3048 /* Text properties change in next_iv. */
3049 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3050 }
3051 }
3052
3053 if (it->cmp_it.id < 0)
3054 {
3055 EMACS_INT stoppos = it->end_charpos;
3056
3057 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3058 stoppos = -1;
3059 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3060 stoppos, it->string);
3061 }
3062
3063 xassert (STRINGP (it->string)
3064 || (it->stop_charpos >= BEGV
3065 && it->stop_charpos >= IT_CHARPOS (*it)));
3066 }
3067
3068
3069 /* Return the position of the next overlay change after POS in
3070 current_buffer. Value is point-max if no overlay change
3071 follows. This is like `next-overlay-change' but doesn't use
3072 xmalloc. */
3073
3074 static EMACS_INT
3075 next_overlay_change (EMACS_INT pos)
3076 {
3077 int noverlays;
3078 EMACS_INT endpos;
3079 Lisp_Object *overlays;
3080 int i;
3081
3082 /* Get all overlays at the given position. */
3083 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3084
3085 /* If any of these overlays ends before endpos,
3086 use its ending point instead. */
3087 for (i = 0; i < noverlays; ++i)
3088 {
3089 Lisp_Object oend;
3090 EMACS_INT oendpos;
3091
3092 oend = OVERLAY_END (overlays[i]);
3093 oendpos = OVERLAY_POSITION (oend);
3094 endpos = min (endpos, oendpos);
3095 }
3096
3097 return endpos;
3098 }
3099
3100 /* Return the character position of a display string at or after
3101 position specified by POSITION. If no display string exists at or
3102 after POSITION, return ZV. A display string is either an overlay
3103 with `display' property whose value is a string, or a `display'
3104 text property whose value is a string. STRING is data about the
3105 string to iterate; if STRING->lstring is nil, we are iterating a
3106 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3107 on a GUI frame. */
3108 EMACS_INT
3109 compute_display_string_pos (struct text_pos *position,
3110 struct bidi_string_data *string, int frame_window_p)
3111 {
3112 /* OBJECT = nil means current buffer. */
3113 Lisp_Object object =
3114 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3115 Lisp_Object pos, spec;
3116 int string_p = (string && (STRINGP (string->lstring) || string->s));
3117 EMACS_INT eob = string_p ? string->schars : ZV;
3118 EMACS_INT begb = string_p ? 0 : BEGV;
3119 EMACS_INT bufpos, charpos = CHARPOS (*position);
3120 struct text_pos tpos;
3121
3122 if (charpos >= eob
3123 /* We don't support display properties whose values are strings
3124 that have display string properties. */
3125 || string->from_disp_str
3126 /* C strings cannot have display properties. */
3127 || (string->s && !STRINGP (object)))
3128 return eob;
3129
3130 /* If the character at CHARPOS is where the display string begins,
3131 return CHARPOS. */
3132 pos = make_number (charpos);
3133 if (STRINGP (object))
3134 bufpos = string->bufpos;
3135 else
3136 bufpos = charpos;
3137 tpos = *position;
3138 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3139 && (charpos <= begb
3140 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3141 object),
3142 spec))
3143 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3144 frame_window_p))
3145 return charpos;
3146
3147 /* Look forward for the first character with a `display' property
3148 that will replace the underlying text when displayed. */
3149 do {
3150 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3151 CHARPOS (tpos) = XFASTINT (pos);
3152 if (STRINGP (object))
3153 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3154 else
3155 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3156 if (CHARPOS (tpos) >= eob)
3157 break;
3158 spec = Fget_char_property (pos, Qdisplay, object);
3159 if (!STRINGP (object))
3160 bufpos = CHARPOS (tpos);
3161 } while (NILP (spec)
3162 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3163 frame_window_p));
3164
3165 return CHARPOS (tpos);
3166 }
3167
3168 /* Return the character position of the end of the display string that
3169 started at CHARPOS. A display string is either an overlay with
3170 `display' property whose value is a string or a `display' text
3171 property whose value is a string. */
3172 EMACS_INT
3173 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3174 {
3175 /* OBJECT = nil means current buffer. */
3176 Lisp_Object object =
3177 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3178 Lisp_Object pos = make_number (charpos);
3179 EMACS_INT eob =
3180 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3181
3182 if (charpos >= eob || (string->s && !STRINGP (object)))
3183 return eob;
3184
3185 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3186 abort ();
3187
3188 /* Look forward for the first character where the `display' property
3189 changes. */
3190 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3191
3192 return XFASTINT (pos);
3193 }
3194
3195
3196 \f
3197 /***********************************************************************
3198 Fontification
3199 ***********************************************************************/
3200
3201 /* Handle changes in the `fontified' property of the current buffer by
3202 calling hook functions from Qfontification_functions to fontify
3203 regions of text. */
3204
3205 static enum prop_handled
3206 handle_fontified_prop (struct it *it)
3207 {
3208 Lisp_Object prop, pos;
3209 enum prop_handled handled = HANDLED_NORMALLY;
3210
3211 if (!NILP (Vmemory_full))
3212 return handled;
3213
3214 /* Get the value of the `fontified' property at IT's current buffer
3215 position. (The `fontified' property doesn't have a special
3216 meaning in strings.) If the value is nil, call functions from
3217 Qfontification_functions. */
3218 if (!STRINGP (it->string)
3219 && it->s == NULL
3220 && !NILP (Vfontification_functions)
3221 && !NILP (Vrun_hooks)
3222 && (pos = make_number (IT_CHARPOS (*it)),
3223 prop = Fget_char_property (pos, Qfontified, Qnil),
3224 /* Ignore the special cased nil value always present at EOB since
3225 no amount of fontifying will be able to change it. */
3226 NILP (prop) && IT_CHARPOS (*it) < Z))
3227 {
3228 int count = SPECPDL_INDEX ();
3229 Lisp_Object val;
3230 struct buffer *obuf = current_buffer;
3231 int begv = BEGV, zv = ZV;
3232 int old_clip_changed = current_buffer->clip_changed;
3233
3234 val = Vfontification_functions;
3235 specbind (Qfontification_functions, Qnil);
3236
3237 xassert (it->end_charpos == ZV);
3238
3239 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3240 safe_call1 (val, pos);
3241 else
3242 {
3243 Lisp_Object fns, fn;
3244 struct gcpro gcpro1, gcpro2;
3245
3246 fns = Qnil;
3247 GCPRO2 (val, fns);
3248
3249 for (; CONSP (val); val = XCDR (val))
3250 {
3251 fn = XCAR (val);
3252
3253 if (EQ (fn, Qt))
3254 {
3255 /* A value of t indicates this hook has a local
3256 binding; it means to run the global binding too.
3257 In a global value, t should not occur. If it
3258 does, we must ignore it to avoid an endless
3259 loop. */
3260 for (fns = Fdefault_value (Qfontification_functions);
3261 CONSP (fns);
3262 fns = XCDR (fns))
3263 {
3264 fn = XCAR (fns);
3265 if (!EQ (fn, Qt))
3266 safe_call1 (fn, pos);
3267 }
3268 }
3269 else
3270 safe_call1 (fn, pos);
3271 }
3272
3273 UNGCPRO;
3274 }
3275
3276 unbind_to (count, Qnil);
3277
3278 /* Fontification functions routinely call `save-restriction'.
3279 Normally, this tags clip_changed, which can confuse redisplay
3280 (see discussion in Bug#6671). Since we don't perform any
3281 special handling of fontification changes in the case where
3282 `save-restriction' isn't called, there's no point doing so in
3283 this case either. So, if the buffer's restrictions are
3284 actually left unchanged, reset clip_changed. */
3285 if (obuf == current_buffer)
3286 {
3287 if (begv == BEGV && zv == ZV)
3288 current_buffer->clip_changed = old_clip_changed;
3289 }
3290 /* There isn't much we can reasonably do to protect against
3291 misbehaving fontification, but here's a fig leaf. */
3292 else if (!NILP (BVAR (obuf, name)))
3293 set_buffer_internal_1 (obuf);
3294
3295 /* The fontification code may have added/removed text.
3296 It could do even a lot worse, but let's at least protect against
3297 the most obvious case where only the text past `pos' gets changed',
3298 as is/was done in grep.el where some escapes sequences are turned
3299 into face properties (bug#7876). */
3300 it->end_charpos = ZV;
3301
3302 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3303 something. This avoids an endless loop if they failed to
3304 fontify the text for which reason ever. */
3305 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3306 handled = HANDLED_RECOMPUTE_PROPS;
3307 }
3308
3309 return handled;
3310 }
3311
3312
3313 \f
3314 /***********************************************************************
3315 Faces
3316 ***********************************************************************/
3317
3318 /* Set up iterator IT from face properties at its current position.
3319 Called from handle_stop. */
3320
3321 static enum prop_handled
3322 handle_face_prop (struct it *it)
3323 {
3324 int new_face_id;
3325 EMACS_INT next_stop;
3326
3327 if (!STRINGP (it->string))
3328 {
3329 new_face_id
3330 = face_at_buffer_position (it->w,
3331 IT_CHARPOS (*it),
3332 it->region_beg_charpos,
3333 it->region_end_charpos,
3334 &next_stop,
3335 (IT_CHARPOS (*it)
3336 + TEXT_PROP_DISTANCE_LIMIT),
3337 0, it->base_face_id);
3338
3339 /* Is this a start of a run of characters with box face?
3340 Caveat: this can be called for a freshly initialized
3341 iterator; face_id is -1 in this case. We know that the new
3342 face will not change until limit, i.e. if the new face has a
3343 box, all characters up to limit will have one. But, as
3344 usual, we don't know whether limit is really the end. */
3345 if (new_face_id != it->face_id)
3346 {
3347 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3348
3349 /* If new face has a box but old face has not, this is
3350 the start of a run of characters with box, i.e. it has
3351 a shadow on the left side. The value of face_id of the
3352 iterator will be -1 if this is the initial call that gets
3353 the face. In this case, we have to look in front of IT's
3354 position and see whether there is a face != new_face_id. */
3355 it->start_of_box_run_p
3356 = (new_face->box != FACE_NO_BOX
3357 && (it->face_id >= 0
3358 || IT_CHARPOS (*it) == BEG
3359 || new_face_id != face_before_it_pos (it)));
3360 it->face_box_p = new_face->box != FACE_NO_BOX;
3361 }
3362 }
3363 else
3364 {
3365 int base_face_id;
3366 EMACS_INT bufpos;
3367 int i;
3368 Lisp_Object from_overlay
3369 = (it->current.overlay_string_index >= 0
3370 ? it->string_overlays[it->current.overlay_string_index]
3371 : Qnil);
3372
3373 /* See if we got to this string directly or indirectly from
3374 an overlay property. That includes the before-string or
3375 after-string of an overlay, strings in display properties
3376 provided by an overlay, their text properties, etc.
3377
3378 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3379 if (! NILP (from_overlay))
3380 for (i = it->sp - 1; i >= 0; i--)
3381 {
3382 if (it->stack[i].current.overlay_string_index >= 0)
3383 from_overlay
3384 = it->string_overlays[it->stack[i].current.overlay_string_index];
3385 else if (! NILP (it->stack[i].from_overlay))
3386 from_overlay = it->stack[i].from_overlay;
3387
3388 if (!NILP (from_overlay))
3389 break;
3390 }
3391
3392 if (! NILP (from_overlay))
3393 {
3394 bufpos = IT_CHARPOS (*it);
3395 /* For a string from an overlay, the base face depends
3396 only on text properties and ignores overlays. */
3397 base_face_id
3398 = face_for_overlay_string (it->w,
3399 IT_CHARPOS (*it),
3400 it->region_beg_charpos,
3401 it->region_end_charpos,
3402 &next_stop,
3403 (IT_CHARPOS (*it)
3404 + TEXT_PROP_DISTANCE_LIMIT),
3405 0,
3406 from_overlay);
3407 }
3408 else
3409 {
3410 bufpos = 0;
3411
3412 /* For strings from a `display' property, use the face at
3413 IT's current buffer position as the base face to merge
3414 with, so that overlay strings appear in the same face as
3415 surrounding text, unless they specify their own
3416 faces. */
3417 base_face_id = underlying_face_id (it);
3418 }
3419
3420 new_face_id = face_at_string_position (it->w,
3421 it->string,
3422 IT_STRING_CHARPOS (*it),
3423 bufpos,
3424 it->region_beg_charpos,
3425 it->region_end_charpos,
3426 &next_stop,
3427 base_face_id, 0);
3428
3429 /* Is this a start of a run of characters with box? Caveat:
3430 this can be called for a freshly allocated iterator; face_id
3431 is -1 is this case. We know that the new face will not
3432 change until the next check pos, i.e. if the new face has a
3433 box, all characters up to that position will have a
3434 box. But, as usual, we don't know whether that position
3435 is really the end. */
3436 if (new_face_id != it->face_id)
3437 {
3438 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3439 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3440
3441 /* If new face has a box but old face hasn't, this is the
3442 start of a run of characters with box, i.e. it has a
3443 shadow on the left side. */
3444 it->start_of_box_run_p
3445 = new_face->box && (old_face == NULL || !old_face->box);
3446 it->face_box_p = new_face->box != FACE_NO_BOX;
3447 }
3448 }
3449
3450 it->face_id = new_face_id;
3451 return HANDLED_NORMALLY;
3452 }
3453
3454
3455 /* Return the ID of the face ``underlying'' IT's current position,
3456 which is in a string. If the iterator is associated with a
3457 buffer, return the face at IT's current buffer position.
3458 Otherwise, use the iterator's base_face_id. */
3459
3460 static int
3461 underlying_face_id (struct it *it)
3462 {
3463 int face_id = it->base_face_id, i;
3464
3465 xassert (STRINGP (it->string));
3466
3467 for (i = it->sp - 1; i >= 0; --i)
3468 if (NILP (it->stack[i].string))
3469 face_id = it->stack[i].face_id;
3470
3471 return face_id;
3472 }
3473
3474
3475 /* Compute the face one character before or after the current position
3476 of IT, in the visual order. BEFORE_P non-zero means get the face
3477 in front (to the left in L2R paragraphs, to the right in R2L
3478 paragraphs) of IT's screen position. Value is the ID of the face. */
3479
3480 static int
3481 face_before_or_after_it_pos (struct it *it, int before_p)
3482 {
3483 int face_id, limit;
3484 EMACS_INT next_check_charpos;
3485 struct it it_copy;
3486
3487 xassert (it->s == NULL);
3488
3489 if (STRINGP (it->string))
3490 {
3491 EMACS_INT bufpos, charpos;
3492 int base_face_id;
3493
3494 /* No face change past the end of the string (for the case
3495 we are padding with spaces). No face change before the
3496 string start. */
3497 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3498 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3499 return it->face_id;
3500
3501 if (!it->bidi_p)
3502 {
3503 /* Set charpos to the position before or after IT's current
3504 position, in the logical order, which in the non-bidi
3505 case is the same as the visual order. */
3506 if (before_p)
3507 charpos = IT_STRING_CHARPOS (*it) - 1;
3508 else if (it->what == IT_COMPOSITION)
3509 /* For composition, we must check the character after the
3510 composition. */
3511 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3512 else
3513 charpos = IT_STRING_CHARPOS (*it) + 1;
3514 }
3515 else
3516 {
3517 if (before_p)
3518 {
3519 /* With bidi iteration, the character before the current
3520 in the visual order cannot be found by simple
3521 iteration, because "reverse" reordering is not
3522 supported. Instead, we need to use the move_it_*
3523 family of functions. */
3524 /* Ignore face changes before the first visible
3525 character on this display line. */
3526 if (it->current_x <= it->first_visible_x)
3527 return it->face_id;
3528 it_copy = *it;
3529 /* Implementation note: Since move_it_in_display_line
3530 works in the iterator geometry, and thinks the first
3531 character is always the leftmost, even in R2L lines,
3532 we don't need to distinguish between the R2L and L2R
3533 cases here. */
3534 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3535 it_copy.current_x - 1, MOVE_TO_X);
3536 charpos = IT_STRING_CHARPOS (it_copy);
3537 }
3538 else
3539 {
3540 /* Set charpos to the string position of the character
3541 that comes after IT's current position in the visual
3542 order. */
3543 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3544
3545 it_copy = *it;
3546 while (n--)
3547 bidi_move_to_visually_next (&it_copy.bidi_it);
3548
3549 charpos = it_copy.bidi_it.charpos;
3550 }
3551 }
3552 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3553
3554 if (it->current.overlay_string_index >= 0)
3555 bufpos = IT_CHARPOS (*it);
3556 else
3557 bufpos = 0;
3558
3559 base_face_id = underlying_face_id (it);
3560
3561 /* Get the face for ASCII, or unibyte. */
3562 face_id = face_at_string_position (it->w,
3563 it->string,
3564 charpos,
3565 bufpos,
3566 it->region_beg_charpos,
3567 it->region_end_charpos,
3568 &next_check_charpos,
3569 base_face_id, 0);
3570
3571 /* Correct the face for charsets different from ASCII. Do it
3572 for the multibyte case only. The face returned above is
3573 suitable for unibyte text if IT->string is unibyte. */
3574 if (STRING_MULTIBYTE (it->string))
3575 {
3576 struct text_pos pos1 = string_pos (charpos, it->string);
3577 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3578 int c, len;
3579 struct face *face = FACE_FROM_ID (it->f, face_id);
3580
3581 c = string_char_and_length (p, &len);
3582 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3583 }
3584 }
3585 else
3586 {
3587 struct text_pos pos;
3588
3589 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3590 || (IT_CHARPOS (*it) <= BEGV && before_p))
3591 return it->face_id;
3592
3593 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3594 pos = it->current.pos;
3595
3596 if (!it->bidi_p)
3597 {
3598 if (before_p)
3599 DEC_TEXT_POS (pos, it->multibyte_p);
3600 else
3601 {
3602 if (it->what == IT_COMPOSITION)
3603 {
3604 /* For composition, we must check the position after
3605 the composition. */
3606 pos.charpos += it->cmp_it.nchars;
3607 pos.bytepos += it->len;
3608 }
3609 else
3610 INC_TEXT_POS (pos, it->multibyte_p);
3611 }
3612 }
3613 else
3614 {
3615 if (before_p)
3616 {
3617 /* With bidi iteration, the character before the current
3618 in the visual order cannot be found by simple
3619 iteration, because "reverse" reordering is not
3620 supported. Instead, we need to use the move_it_*
3621 family of functions. */
3622 /* Ignore face changes before the first visible
3623 character on this display line. */
3624 if (it->current_x <= it->first_visible_x)
3625 return it->face_id;
3626 it_copy = *it;
3627 /* Implementation note: Since move_it_in_display_line
3628 works in the iterator geometry, and thinks the first
3629 character is always the leftmost, even in R2L lines,
3630 we don't need to distinguish between the R2L and L2R
3631 cases here. */
3632 move_it_in_display_line (&it_copy, ZV,
3633 it_copy.current_x - 1, MOVE_TO_X);
3634 pos = it_copy.current.pos;
3635 }
3636 else
3637 {
3638 /* Set charpos to the buffer position of the character
3639 that comes after IT's current position in the visual
3640 order. */
3641 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3642
3643 it_copy = *it;
3644 while (n--)
3645 bidi_move_to_visually_next (&it_copy.bidi_it);
3646
3647 SET_TEXT_POS (pos,
3648 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3649 }
3650 }
3651 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3652
3653 /* Determine face for CHARSET_ASCII, or unibyte. */
3654 face_id = face_at_buffer_position (it->w,
3655 CHARPOS (pos),
3656 it->region_beg_charpos,
3657 it->region_end_charpos,
3658 &next_check_charpos,
3659 limit, 0, -1);
3660
3661 /* Correct the face for charsets different from ASCII. Do it
3662 for the multibyte case only. The face returned above is
3663 suitable for unibyte text if current_buffer is unibyte. */
3664 if (it->multibyte_p)
3665 {
3666 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3667 struct face *face = FACE_FROM_ID (it->f, face_id);
3668 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3669 }
3670 }
3671
3672 return face_id;
3673 }
3674
3675
3676 \f
3677 /***********************************************************************
3678 Invisible text
3679 ***********************************************************************/
3680
3681 /* Set up iterator IT from invisible properties at its current
3682 position. Called from handle_stop. */
3683
3684 static enum prop_handled
3685 handle_invisible_prop (struct it *it)
3686 {
3687 enum prop_handled handled = HANDLED_NORMALLY;
3688
3689 if (STRINGP (it->string))
3690 {
3691 Lisp_Object prop, end_charpos, limit, charpos;
3692
3693 /* Get the value of the invisible text property at the
3694 current position. Value will be nil if there is no such
3695 property. */
3696 charpos = make_number (IT_STRING_CHARPOS (*it));
3697 prop = Fget_text_property (charpos, Qinvisible, it->string);
3698
3699 if (!NILP (prop)
3700 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3701 {
3702 EMACS_INT endpos;
3703
3704 handled = HANDLED_RECOMPUTE_PROPS;
3705
3706 /* Get the position at which the next change of the
3707 invisible text property can be found in IT->string.
3708 Value will be nil if the property value is the same for
3709 all the rest of IT->string. */
3710 XSETINT (limit, SCHARS (it->string));
3711 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3712 it->string, limit);
3713
3714 /* Text at current position is invisible. The next
3715 change in the property is at position end_charpos.
3716 Move IT's current position to that position. */
3717 if (INTEGERP (end_charpos)
3718 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3719 {
3720 struct text_pos old;
3721 EMACS_INT oldpos;
3722
3723 old = it->current.string_pos;
3724 oldpos = CHARPOS (old);
3725 if (it->bidi_p)
3726 {
3727 if (it->bidi_it.first_elt
3728 && it->bidi_it.charpos < SCHARS (it->string))
3729 bidi_paragraph_init (it->paragraph_embedding,
3730 &it->bidi_it, 1);
3731 /* Bidi-iterate out of the invisible text. */
3732 do
3733 {
3734 bidi_move_to_visually_next (&it->bidi_it);
3735 }
3736 while (oldpos <= it->bidi_it.charpos
3737 && it->bidi_it.charpos < endpos);
3738
3739 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3740 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3741 if (IT_CHARPOS (*it) >= endpos)
3742 it->prev_stop = endpos;
3743 }
3744 else
3745 {
3746 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3747 compute_string_pos (&it->current.string_pos, old, it->string);
3748 }
3749 }
3750 else
3751 {
3752 /* The rest of the string is invisible. If this is an
3753 overlay string, proceed with the next overlay string
3754 or whatever comes and return a character from there. */
3755 if (it->current.overlay_string_index >= 0)
3756 {
3757 next_overlay_string (it);
3758 /* Don't check for overlay strings when we just
3759 finished processing them. */
3760 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3761 }
3762 else
3763 {
3764 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3765 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3766 }
3767 }
3768 }
3769 }
3770 else
3771 {
3772 int invis_p;
3773 EMACS_INT newpos, next_stop, start_charpos, tem;
3774 Lisp_Object pos, prop, overlay;
3775
3776 /* First of all, is there invisible text at this position? */
3777 tem = start_charpos = IT_CHARPOS (*it);
3778 pos = make_number (tem);
3779 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3780 &overlay);
3781 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3782
3783 /* If we are on invisible text, skip over it. */
3784 if (invis_p && start_charpos < it->end_charpos)
3785 {
3786 /* Record whether we have to display an ellipsis for the
3787 invisible text. */
3788 int display_ellipsis_p = invis_p == 2;
3789
3790 handled = HANDLED_RECOMPUTE_PROPS;
3791
3792 /* Loop skipping over invisible text. The loop is left at
3793 ZV or with IT on the first char being visible again. */
3794 do
3795 {
3796 /* Try to skip some invisible text. Return value is the
3797 position reached which can be equal to where we start
3798 if there is nothing invisible there. This skips both
3799 over invisible text properties and overlays with
3800 invisible property. */
3801 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3802
3803 /* If we skipped nothing at all we weren't at invisible
3804 text in the first place. If everything to the end of
3805 the buffer was skipped, end the loop. */
3806 if (newpos == tem || newpos >= ZV)
3807 invis_p = 0;
3808 else
3809 {
3810 /* We skipped some characters but not necessarily
3811 all there are. Check if we ended up on visible
3812 text. Fget_char_property returns the property of
3813 the char before the given position, i.e. if we
3814 get invis_p = 0, this means that the char at
3815 newpos is visible. */
3816 pos = make_number (newpos);
3817 prop = Fget_char_property (pos, Qinvisible, it->window);
3818 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3819 }
3820
3821 /* If we ended up on invisible text, proceed to
3822 skip starting with next_stop. */
3823 if (invis_p)
3824 tem = next_stop;
3825
3826 /* If there are adjacent invisible texts, don't lose the
3827 second one's ellipsis. */
3828 if (invis_p == 2)
3829 display_ellipsis_p = 1;
3830 }
3831 while (invis_p);
3832
3833 /* The position newpos is now either ZV or on visible text. */
3834 if (it->bidi_p && newpos < ZV)
3835 {
3836 /* With bidi iteration, the region of invisible text
3837 could start and/or end in the middle of a non-base
3838 embedding level. Therefore, we need to skip
3839 invisible text using the bidi iterator, starting at
3840 IT's current position, until we find ourselves
3841 outside the invisible text. Skipping invisible text
3842 _after_ bidi iteration avoids affecting the visual
3843 order of the displayed text when invisible properties
3844 are added or removed. */
3845 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3846 {
3847 /* If we were `reseat'ed to a new paragraph,
3848 determine the paragraph base direction. We need
3849 to do it now because next_element_from_buffer may
3850 not have a chance to do it, if we are going to
3851 skip any text at the beginning, which resets the
3852 FIRST_ELT flag. */
3853 bidi_paragraph_init (it->paragraph_embedding,
3854 &it->bidi_it, 1);
3855 }
3856 do
3857 {
3858 bidi_move_to_visually_next (&it->bidi_it);
3859 }
3860 while (it->stop_charpos <= it->bidi_it.charpos
3861 && it->bidi_it.charpos < newpos);
3862 IT_CHARPOS (*it) = it->bidi_it.charpos;
3863 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3864 /* If we overstepped NEWPOS, record its position in the
3865 iterator, so that we skip invisible text if later the
3866 bidi iteration lands us in the invisible region
3867 again. */
3868 if (IT_CHARPOS (*it) >= newpos)
3869 it->prev_stop = newpos;
3870 }
3871 else
3872 {
3873 IT_CHARPOS (*it) = newpos;
3874 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3875 }
3876
3877 /* If there are before-strings at the start of invisible
3878 text, and the text is invisible because of a text
3879 property, arrange to show before-strings because 20.x did
3880 it that way. (If the text is invisible because of an
3881 overlay property instead of a text property, this is
3882 already handled in the overlay code.) */
3883 if (NILP (overlay)
3884 && get_overlay_strings (it, it->stop_charpos))
3885 {
3886 handled = HANDLED_RECOMPUTE_PROPS;
3887 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3888 }
3889 else if (display_ellipsis_p)
3890 {
3891 /* Make sure that the glyphs of the ellipsis will get
3892 correct `charpos' values. If we would not update
3893 it->position here, the glyphs would belong to the
3894 last visible character _before_ the invisible
3895 text, which confuses `set_cursor_from_row'.
3896
3897 We use the last invisible position instead of the
3898 first because this way the cursor is always drawn on
3899 the first "." of the ellipsis, whenever PT is inside
3900 the invisible text. Otherwise the cursor would be
3901 placed _after_ the ellipsis when the point is after the
3902 first invisible character. */
3903 if (!STRINGP (it->object))
3904 {
3905 it->position.charpos = newpos - 1;
3906 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3907 }
3908 it->ellipsis_p = 1;
3909 /* Let the ellipsis display before
3910 considering any properties of the following char.
3911 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3912 handled = HANDLED_RETURN;
3913 }
3914 }
3915 }
3916
3917 return handled;
3918 }
3919
3920
3921 /* Make iterator IT return `...' next.
3922 Replaces LEN characters from buffer. */
3923
3924 static void
3925 setup_for_ellipsis (struct it *it, int len)
3926 {
3927 /* Use the display table definition for `...'. Invalid glyphs
3928 will be handled by the method returning elements from dpvec. */
3929 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3930 {
3931 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3932 it->dpvec = v->contents;
3933 it->dpend = v->contents + v->header.size;
3934 }
3935 else
3936 {
3937 /* Default `...'. */
3938 it->dpvec = default_invis_vector;
3939 it->dpend = default_invis_vector + 3;
3940 }
3941
3942 it->dpvec_char_len = len;
3943 it->current.dpvec_index = 0;
3944 it->dpvec_face_id = -1;
3945
3946 /* Remember the current face id in case glyphs specify faces.
3947 IT's face is restored in set_iterator_to_next.
3948 saved_face_id was set to preceding char's face in handle_stop. */
3949 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3950 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3951
3952 it->method = GET_FROM_DISPLAY_VECTOR;
3953 it->ellipsis_p = 1;
3954 }
3955
3956
3957 \f
3958 /***********************************************************************
3959 'display' property
3960 ***********************************************************************/
3961
3962 /* Set up iterator IT from `display' property at its current position.
3963 Called from handle_stop.
3964 We return HANDLED_RETURN if some part of the display property
3965 overrides the display of the buffer text itself.
3966 Otherwise we return HANDLED_NORMALLY. */
3967
3968 static enum prop_handled
3969 handle_display_prop (struct it *it)
3970 {
3971 Lisp_Object propval, object, overlay;
3972 struct text_pos *position;
3973 EMACS_INT bufpos;
3974 /* Nonzero if some property replaces the display of the text itself. */
3975 int display_replaced_p = 0;
3976
3977 if (STRINGP (it->string))
3978 {
3979 object = it->string;
3980 position = &it->current.string_pos;
3981 bufpos = CHARPOS (it->current.pos);
3982 }
3983 else
3984 {
3985 XSETWINDOW (object, it->w);
3986 position = &it->current.pos;
3987 bufpos = CHARPOS (*position);
3988 }
3989
3990 /* Reset those iterator values set from display property values. */
3991 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3992 it->space_width = Qnil;
3993 it->font_height = Qnil;
3994 it->voffset = 0;
3995
3996 /* We don't support recursive `display' properties, i.e. string
3997 values that have a string `display' property, that have a string
3998 `display' property etc. */
3999 if (!it->string_from_display_prop_p)
4000 it->area = TEXT_AREA;
4001
4002 propval = get_char_property_and_overlay (make_number (position->charpos),
4003 Qdisplay, object, &overlay);
4004 if (NILP (propval))
4005 return HANDLED_NORMALLY;
4006 /* Now OVERLAY is the overlay that gave us this property, or nil
4007 if it was a text property. */
4008
4009 if (!STRINGP (it->string))
4010 object = it->w->buffer;
4011
4012 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4013 position, bufpos,
4014 FRAME_WINDOW_P (it->f));
4015
4016 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4017 }
4018
4019 /* Subroutine of handle_display_prop. Returns non-zero if the display
4020 specification in SPEC is a replacing specification, i.e. it would
4021 replace the text covered by `display' property with something else,
4022 such as an image or a display string.
4023
4024 See handle_single_display_spec for documentation of arguments.
4025 frame_window_p is non-zero if the window being redisplayed is on a
4026 GUI frame; this argument is used only if IT is NULL, see below.
4027
4028 IT can be NULL, if this is called by the bidi reordering code
4029 through compute_display_string_pos, which see. In that case, this
4030 function only examines SPEC, but does not otherwise "handle" it, in
4031 the sense that it doesn't set up members of IT from the display
4032 spec. */
4033 static int
4034 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4035 Lisp_Object overlay, struct text_pos *position,
4036 EMACS_INT bufpos, int frame_window_p)
4037 {
4038 int replacing_p = 0;
4039
4040 if (CONSP (spec)
4041 /* Simple specerties. */
4042 && !EQ (XCAR (spec), Qimage)
4043 && !EQ (XCAR (spec), Qspace)
4044 && !EQ (XCAR (spec), Qwhen)
4045 && !EQ (XCAR (spec), Qslice)
4046 && !EQ (XCAR (spec), Qspace_width)
4047 && !EQ (XCAR (spec), Qheight)
4048 && !EQ (XCAR (spec), Qraise)
4049 /* Marginal area specifications. */
4050 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4051 && !EQ (XCAR (spec), Qleft_fringe)
4052 && !EQ (XCAR (spec), Qright_fringe)
4053 && !NILP (XCAR (spec)))
4054 {
4055 for (; CONSP (spec); spec = XCDR (spec))
4056 {
4057 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4058 position, bufpos, replacing_p,
4059 frame_window_p))
4060 {
4061 replacing_p = 1;
4062 /* If some text in a string is replaced, `position' no
4063 longer points to the position of `object'. */
4064 if (!it || STRINGP (object))
4065 break;
4066 }
4067 }
4068 }
4069 else if (VECTORP (spec))
4070 {
4071 int i;
4072 for (i = 0; i < ASIZE (spec); ++i)
4073 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4074 position, bufpos, replacing_p,
4075 frame_window_p))
4076 {
4077 replacing_p = 1;
4078 /* If some text in a string is replaced, `position' no
4079 longer points to the position of `object'. */
4080 if (!it || STRINGP (object))
4081 break;
4082 }
4083 }
4084 else
4085 {
4086 if (handle_single_display_spec (it, spec, object, overlay,
4087 position, bufpos, 0, frame_window_p))
4088 replacing_p = 1;
4089 }
4090
4091 return replacing_p;
4092 }
4093
4094 /* Value is the position of the end of the `display' property starting
4095 at START_POS in OBJECT. */
4096
4097 static struct text_pos
4098 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4099 {
4100 Lisp_Object end;
4101 struct text_pos end_pos;
4102
4103 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4104 Qdisplay, object, Qnil);
4105 CHARPOS (end_pos) = XFASTINT (end);
4106 if (STRINGP (object))
4107 compute_string_pos (&end_pos, start_pos, it->string);
4108 else
4109 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4110
4111 return end_pos;
4112 }
4113
4114
4115 /* Set up IT from a single `display' property specification SPEC. OBJECT
4116 is the object in which the `display' property was found. *POSITION
4117 is the position in OBJECT at which the `display' property was found.
4118 BUFPOS is the buffer position of OBJECT (different from POSITION if
4119 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4120 previously saw a display specification which already replaced text
4121 display with something else, for example an image; we ignore such
4122 properties after the first one has been processed.
4123
4124 OVERLAY is the overlay this `display' property came from,
4125 or nil if it was a text property.
4126
4127 If SPEC is a `space' or `image' specification, and in some other
4128 cases too, set *POSITION to the position where the `display'
4129 property ends.
4130
4131 If IT is NULL, only examine the property specification in SPEC, but
4132 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4133 is intended to be displayed in a window on a GUI frame.
4134
4135 Value is non-zero if something was found which replaces the display
4136 of buffer or string text. */
4137
4138 static int
4139 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4140 Lisp_Object overlay, struct text_pos *position,
4141 EMACS_INT bufpos, int display_replaced_p,
4142 int frame_window_p)
4143 {
4144 Lisp_Object form;
4145 Lisp_Object location, value;
4146 struct text_pos start_pos = *position;
4147 int valid_p;
4148
4149 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4150 If the result is non-nil, use VALUE instead of SPEC. */
4151 form = Qt;
4152 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4153 {
4154 spec = XCDR (spec);
4155 if (!CONSP (spec))
4156 return 0;
4157 form = XCAR (spec);
4158 spec = XCDR (spec);
4159 }
4160
4161 if (!NILP (form) && !EQ (form, Qt))
4162 {
4163 int count = SPECPDL_INDEX ();
4164 struct gcpro gcpro1;
4165
4166 /* Bind `object' to the object having the `display' property, a
4167 buffer or string. Bind `position' to the position in the
4168 object where the property was found, and `buffer-position'
4169 to the current position in the buffer. */
4170
4171 if (NILP (object))
4172 XSETBUFFER (object, current_buffer);
4173 specbind (Qobject, object);
4174 specbind (Qposition, make_number (CHARPOS (*position)));
4175 specbind (Qbuffer_position, make_number (bufpos));
4176 GCPRO1 (form);
4177 form = safe_eval (form);
4178 UNGCPRO;
4179 unbind_to (count, Qnil);
4180 }
4181
4182 if (NILP (form))
4183 return 0;
4184
4185 /* Handle `(height HEIGHT)' specifications. */
4186 if (CONSP (spec)
4187 && EQ (XCAR (spec), Qheight)
4188 && CONSP (XCDR (spec)))
4189 {
4190 if (it)
4191 {
4192 if (!FRAME_WINDOW_P (it->f))
4193 return 0;
4194
4195 it->font_height = XCAR (XCDR (spec));
4196 if (!NILP (it->font_height))
4197 {
4198 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4199 int new_height = -1;
4200
4201 if (CONSP (it->font_height)
4202 && (EQ (XCAR (it->font_height), Qplus)
4203 || EQ (XCAR (it->font_height), Qminus))
4204 && CONSP (XCDR (it->font_height))
4205 && INTEGERP (XCAR (XCDR (it->font_height))))
4206 {
4207 /* `(+ N)' or `(- N)' where N is an integer. */
4208 int steps = XINT (XCAR (XCDR (it->font_height)));
4209 if (EQ (XCAR (it->font_height), Qplus))
4210 steps = - steps;
4211 it->face_id = smaller_face (it->f, it->face_id, steps);
4212 }
4213 else if (FUNCTIONP (it->font_height))
4214 {
4215 /* Call function with current height as argument.
4216 Value is the new height. */
4217 Lisp_Object height;
4218 height = safe_call1 (it->font_height,
4219 face->lface[LFACE_HEIGHT_INDEX]);
4220 if (NUMBERP (height))
4221 new_height = XFLOATINT (height);
4222 }
4223 else if (NUMBERP (it->font_height))
4224 {
4225 /* Value is a multiple of the canonical char height. */
4226 struct face *f;
4227
4228 f = FACE_FROM_ID (it->f,
4229 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4230 new_height = (XFLOATINT (it->font_height)
4231 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4232 }
4233 else
4234 {
4235 /* Evaluate IT->font_height with `height' bound to the
4236 current specified height to get the new height. */
4237 int count = SPECPDL_INDEX ();
4238
4239 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4240 value = safe_eval (it->font_height);
4241 unbind_to (count, Qnil);
4242
4243 if (NUMBERP (value))
4244 new_height = XFLOATINT (value);
4245 }
4246
4247 if (new_height > 0)
4248 it->face_id = face_with_height (it->f, it->face_id, new_height);
4249 }
4250 }
4251
4252 return 0;
4253 }
4254
4255 /* Handle `(space-width WIDTH)'. */
4256 if (CONSP (spec)
4257 && EQ (XCAR (spec), Qspace_width)
4258 && CONSP (XCDR (spec)))
4259 {
4260 if (it)
4261 {
4262 if (!FRAME_WINDOW_P (it->f))
4263 return 0;
4264
4265 value = XCAR (XCDR (spec));
4266 if (NUMBERP (value) && XFLOATINT (value) > 0)
4267 it->space_width = value;
4268 }
4269
4270 return 0;
4271 }
4272
4273 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4274 if (CONSP (spec)
4275 && EQ (XCAR (spec), Qslice))
4276 {
4277 Lisp_Object tem;
4278
4279 if (it)
4280 {
4281 if (!FRAME_WINDOW_P (it->f))
4282 return 0;
4283
4284 if (tem = XCDR (spec), CONSP (tem))
4285 {
4286 it->slice.x = XCAR (tem);
4287 if (tem = XCDR (tem), CONSP (tem))
4288 {
4289 it->slice.y = XCAR (tem);
4290 if (tem = XCDR (tem), CONSP (tem))
4291 {
4292 it->slice.width = XCAR (tem);
4293 if (tem = XCDR (tem), CONSP (tem))
4294 it->slice.height = XCAR (tem);
4295 }
4296 }
4297 }
4298 }
4299
4300 return 0;
4301 }
4302
4303 /* Handle `(raise FACTOR)'. */
4304 if (CONSP (spec)
4305 && EQ (XCAR (spec), Qraise)
4306 && CONSP (XCDR (spec)))
4307 {
4308 if (it)
4309 {
4310 if (!FRAME_WINDOW_P (it->f))
4311 return 0;
4312
4313 #ifdef HAVE_WINDOW_SYSTEM
4314 value = XCAR (XCDR (spec));
4315 if (NUMBERP (value))
4316 {
4317 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4318 it->voffset = - (XFLOATINT (value)
4319 * (FONT_HEIGHT (face->font)));
4320 }
4321 #endif /* HAVE_WINDOW_SYSTEM */
4322 }
4323
4324 return 0;
4325 }
4326
4327 /* Don't handle the other kinds of display specifications
4328 inside a string that we got from a `display' property. */
4329 if (it && it->string_from_display_prop_p)
4330 return 0;
4331
4332 /* Characters having this form of property are not displayed, so
4333 we have to find the end of the property. */
4334 if (it)
4335 {
4336 start_pos = *position;
4337 *position = display_prop_end (it, object, start_pos);
4338 }
4339 value = Qnil;
4340
4341 /* Stop the scan at that end position--we assume that all
4342 text properties change there. */
4343 if (it)
4344 it->stop_charpos = position->charpos;
4345
4346 /* Handle `(left-fringe BITMAP [FACE])'
4347 and `(right-fringe BITMAP [FACE])'. */
4348 if (CONSP (spec)
4349 && (EQ (XCAR (spec), Qleft_fringe)
4350 || EQ (XCAR (spec), Qright_fringe))
4351 && CONSP (XCDR (spec)))
4352 {
4353 int fringe_bitmap;
4354
4355 if (it)
4356 {
4357 if (!FRAME_WINDOW_P (it->f))
4358 /* If we return here, POSITION has been advanced
4359 across the text with this property. */
4360 return 0;
4361 }
4362 else if (!frame_window_p)
4363 return 0;
4364
4365 #ifdef HAVE_WINDOW_SYSTEM
4366 value = XCAR (XCDR (spec));
4367 if (!SYMBOLP (value)
4368 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4369 /* If we return here, POSITION has been advanced
4370 across the text with this property. */
4371 return 0;
4372
4373 if (it)
4374 {
4375 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4376
4377 if (CONSP (XCDR (XCDR (spec))))
4378 {
4379 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4380 int face_id2 = lookup_derived_face (it->f, face_name,
4381 FRINGE_FACE_ID, 0);
4382 if (face_id2 >= 0)
4383 face_id = face_id2;
4384 }
4385
4386 /* Save current settings of IT so that we can restore them
4387 when we are finished with the glyph property value. */
4388 push_it (it, position);
4389
4390 it->area = TEXT_AREA;
4391 it->what = IT_IMAGE;
4392 it->image_id = -1; /* no image */
4393 it->position = start_pos;
4394 it->object = NILP (object) ? it->w->buffer : object;
4395 it->method = GET_FROM_IMAGE;
4396 it->from_overlay = Qnil;
4397 it->face_id = face_id;
4398
4399 /* Say that we haven't consumed the characters with
4400 `display' property yet. The call to pop_it in
4401 set_iterator_to_next will clean this up. */
4402 *position = start_pos;
4403
4404 if (EQ (XCAR (spec), Qleft_fringe))
4405 {
4406 it->left_user_fringe_bitmap = fringe_bitmap;
4407 it->left_user_fringe_face_id = face_id;
4408 }
4409 else
4410 {
4411 it->right_user_fringe_bitmap = fringe_bitmap;
4412 it->right_user_fringe_face_id = face_id;
4413 }
4414 }
4415 #endif /* HAVE_WINDOW_SYSTEM */
4416 return 1;
4417 }
4418
4419 /* Prepare to handle `((margin left-margin) ...)',
4420 `((margin right-margin) ...)' and `((margin nil) ...)'
4421 prefixes for display specifications. */
4422 location = Qunbound;
4423 if (CONSP (spec) && CONSP (XCAR (spec)))
4424 {
4425 Lisp_Object tem;
4426
4427 value = XCDR (spec);
4428 if (CONSP (value))
4429 value = XCAR (value);
4430
4431 tem = XCAR (spec);
4432 if (EQ (XCAR (tem), Qmargin)
4433 && (tem = XCDR (tem),
4434 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4435 (NILP (tem)
4436 || EQ (tem, Qleft_margin)
4437 || EQ (tem, Qright_margin))))
4438 location = tem;
4439 }
4440
4441 if (EQ (location, Qunbound))
4442 {
4443 location = Qnil;
4444 value = spec;
4445 }
4446
4447 /* After this point, VALUE is the property after any
4448 margin prefix has been stripped. It must be a string,
4449 an image specification, or `(space ...)'.
4450
4451 LOCATION specifies where to display: `left-margin',
4452 `right-margin' or nil. */
4453
4454 valid_p = (STRINGP (value)
4455 #ifdef HAVE_WINDOW_SYSTEM
4456 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4457 && valid_image_p (value))
4458 #endif /* not HAVE_WINDOW_SYSTEM */
4459 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4460
4461 if (valid_p && !display_replaced_p)
4462 {
4463 if (!it)
4464 return 1;
4465
4466 /* Save current settings of IT so that we can restore them
4467 when we are finished with the glyph property value. */
4468 push_it (it, position);
4469 it->from_overlay = overlay;
4470
4471 if (NILP (location))
4472 it->area = TEXT_AREA;
4473 else if (EQ (location, Qleft_margin))
4474 it->area = LEFT_MARGIN_AREA;
4475 else
4476 it->area = RIGHT_MARGIN_AREA;
4477
4478 if (STRINGP (value))
4479 {
4480 it->string = value;
4481 it->multibyte_p = STRING_MULTIBYTE (it->string);
4482 it->current.overlay_string_index = -1;
4483 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4484 it->end_charpos = it->string_nchars = SCHARS (it->string);
4485 it->method = GET_FROM_STRING;
4486 it->stop_charpos = 0;
4487 it->prev_stop = 0;
4488 it->base_level_stop = 0;
4489 it->string_from_display_prop_p = 1;
4490 /* Say that we haven't consumed the characters with
4491 `display' property yet. The call to pop_it in
4492 set_iterator_to_next will clean this up. */
4493 if (BUFFERP (object))
4494 *position = start_pos;
4495
4496 /* Force paragraph direction to be that of the parent
4497 object. */
4498 it->paragraph_embedding =
4499 (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
4500
4501 /* Set up the bidi iterator for this display string. */
4502 if (it->bidi_p)
4503 {
4504 it->bidi_it.string.lstring = it->string;
4505 it->bidi_it.string.s = NULL;
4506 it->bidi_it.string.schars = it->end_charpos;
4507 it->bidi_it.string.bufpos = bufpos;
4508 it->bidi_it.string.from_disp_str = 1;
4509 it->bidi_it.string.unibyte = !it->multibyte_p;
4510 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4511 }
4512 }
4513 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4514 {
4515 it->method = GET_FROM_STRETCH;
4516 it->object = value;
4517 *position = it->position = start_pos;
4518 }
4519 #ifdef HAVE_WINDOW_SYSTEM
4520 else
4521 {
4522 it->what = IT_IMAGE;
4523 it->image_id = lookup_image (it->f, value);
4524 it->position = start_pos;
4525 it->object = NILP (object) ? it->w->buffer : object;
4526 it->method = GET_FROM_IMAGE;
4527
4528 /* Say that we haven't consumed the characters with
4529 `display' property yet. The call to pop_it in
4530 set_iterator_to_next will clean this up. */
4531 *position = start_pos;
4532 }
4533 #endif /* HAVE_WINDOW_SYSTEM */
4534
4535 return 1;
4536 }
4537
4538 /* Invalid property or property not supported. Restore
4539 POSITION to what it was before. */
4540 *position = start_pos;
4541 return 0;
4542 }
4543
4544 /* Check if PROP is a display property value whose text should be
4545 treated as intangible. OVERLAY is the overlay from which PROP
4546 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4547 specify the buffer position covered by PROP. */
4548
4549 int
4550 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4551 EMACS_INT charpos, EMACS_INT bytepos)
4552 {
4553 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4554 struct text_pos position;
4555
4556 SET_TEXT_POS (position, charpos, bytepos);
4557 return handle_display_spec (NULL, prop, Qnil, overlay,
4558 &position, charpos, frame_window_p);
4559 }
4560
4561
4562 /* Return 1 if PROP is a display sub-property value containing STRING.
4563
4564 Implementation note: this and the following function are really
4565 special cases of handle_display_spec and
4566 handle_single_display_spec, and should ideally use the same code.
4567 Until they do, these two pairs must be consistent and must be
4568 modified in sync. */
4569
4570 static int
4571 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4572 {
4573 if (EQ (string, prop))
4574 return 1;
4575
4576 /* Skip over `when FORM'. */
4577 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4578 {
4579 prop = XCDR (prop);
4580 if (!CONSP (prop))
4581 return 0;
4582 /* Actually, the condition following `when' should be eval'ed,
4583 like handle_single_display_spec does, and we should return
4584 zero if it evaluates to nil. However, this function is
4585 called only when the buffer was already displayed and some
4586 glyph in the glyph matrix was found to come from a display
4587 string. Therefore, the condition was already evaluated, and
4588 the result was non-nil, otherwise the display string wouldn't
4589 have been displayed and we would have never been called for
4590 this property. Thus, we can skip the evaluation and assume
4591 its result is non-nil. */
4592 prop = XCDR (prop);
4593 }
4594
4595 if (CONSP (prop))
4596 /* Skip over `margin LOCATION'. */
4597 if (EQ (XCAR (prop), Qmargin))
4598 {
4599 prop = XCDR (prop);
4600 if (!CONSP (prop))
4601 return 0;
4602
4603 prop = XCDR (prop);
4604 if (!CONSP (prop))
4605 return 0;
4606 }
4607
4608 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4609 }
4610
4611
4612 /* Return 1 if STRING appears in the `display' property PROP. */
4613
4614 static int
4615 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4616 {
4617 if (CONSP (prop)
4618 && !EQ (XCAR (prop), Qwhen)
4619 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4620 {
4621 /* A list of sub-properties. */
4622 while (CONSP (prop))
4623 {
4624 if (single_display_spec_string_p (XCAR (prop), string))
4625 return 1;
4626 prop = XCDR (prop);
4627 }
4628 }
4629 else if (VECTORP (prop))
4630 {
4631 /* A vector of sub-properties. */
4632 int i;
4633 for (i = 0; i < ASIZE (prop); ++i)
4634 if (single_display_spec_string_p (AREF (prop, i), string))
4635 return 1;
4636 }
4637 else
4638 return single_display_spec_string_p (prop, string);
4639
4640 return 0;
4641 }
4642
4643 /* Look for STRING in overlays and text properties in the current
4644 buffer, between character positions FROM and TO (excluding TO).
4645 BACK_P non-zero means look back (in this case, TO is supposed to be
4646 less than FROM).
4647 Value is the first character position where STRING was found, or
4648 zero if it wasn't found before hitting TO.
4649
4650 This function may only use code that doesn't eval because it is
4651 called asynchronously from note_mouse_highlight. */
4652
4653 static EMACS_INT
4654 string_buffer_position_lim (Lisp_Object string,
4655 EMACS_INT from, EMACS_INT to, int back_p)
4656 {
4657 Lisp_Object limit, prop, pos;
4658 int found = 0;
4659
4660 pos = make_number (from);
4661
4662 if (!back_p) /* looking forward */
4663 {
4664 limit = make_number (min (to, ZV));
4665 while (!found && !EQ (pos, limit))
4666 {
4667 prop = Fget_char_property (pos, Qdisplay, Qnil);
4668 if (!NILP (prop) && display_prop_string_p (prop, string))
4669 found = 1;
4670 else
4671 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4672 limit);
4673 }
4674 }
4675 else /* looking back */
4676 {
4677 limit = make_number (max (to, BEGV));
4678 while (!found && !EQ (pos, limit))
4679 {
4680 prop = Fget_char_property (pos, Qdisplay, Qnil);
4681 if (!NILP (prop) && display_prop_string_p (prop, string))
4682 found = 1;
4683 else
4684 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4685 limit);
4686 }
4687 }
4688
4689 return found ? XINT (pos) : 0;
4690 }
4691
4692 /* Determine which buffer position in current buffer STRING comes from.
4693 AROUND_CHARPOS is an approximate position where it could come from.
4694 Value is the buffer position or 0 if it couldn't be determined.
4695
4696 This function is necessary because we don't record buffer positions
4697 in glyphs generated from strings (to keep struct glyph small).
4698 This function may only use code that doesn't eval because it is
4699 called asynchronously from note_mouse_highlight. */
4700
4701 static EMACS_INT
4702 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4703 {
4704 const int MAX_DISTANCE = 1000;
4705 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4706 around_charpos + MAX_DISTANCE,
4707 0);
4708
4709 if (!found)
4710 found = string_buffer_position_lim (string, around_charpos,
4711 around_charpos - MAX_DISTANCE, 1);
4712 return found;
4713 }
4714
4715
4716 \f
4717 /***********************************************************************
4718 `composition' property
4719 ***********************************************************************/
4720
4721 /* Set up iterator IT from `composition' property at its current
4722 position. Called from handle_stop. */
4723
4724 static enum prop_handled
4725 handle_composition_prop (struct it *it)
4726 {
4727 Lisp_Object prop, string;
4728 EMACS_INT pos, pos_byte, start, end;
4729
4730 if (STRINGP (it->string))
4731 {
4732 unsigned char *s;
4733
4734 pos = IT_STRING_CHARPOS (*it);
4735 pos_byte = IT_STRING_BYTEPOS (*it);
4736 string = it->string;
4737 s = SDATA (string) + pos_byte;
4738 it->c = STRING_CHAR (s);
4739 }
4740 else
4741 {
4742 pos = IT_CHARPOS (*it);
4743 pos_byte = IT_BYTEPOS (*it);
4744 string = Qnil;
4745 it->c = FETCH_CHAR (pos_byte);
4746 }
4747
4748 /* If there's a valid composition and point is not inside of the
4749 composition (in the case that the composition is from the current
4750 buffer), draw a glyph composed from the composition components. */
4751 if (find_composition (pos, -1, &start, &end, &prop, string)
4752 && COMPOSITION_VALID_P (start, end, prop)
4753 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4754 {
4755 if (start != pos)
4756 {
4757 if (STRINGP (it->string))
4758 pos_byte = string_char_to_byte (it->string, start);
4759 else
4760 pos_byte = CHAR_TO_BYTE (start);
4761 }
4762 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4763 prop, string);
4764
4765 if (it->cmp_it.id >= 0)
4766 {
4767 it->cmp_it.ch = -1;
4768 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4769 it->cmp_it.nglyphs = -1;
4770 }
4771 }
4772
4773 return HANDLED_NORMALLY;
4774 }
4775
4776
4777 \f
4778 /***********************************************************************
4779 Overlay strings
4780 ***********************************************************************/
4781
4782 /* The following structure is used to record overlay strings for
4783 later sorting in load_overlay_strings. */
4784
4785 struct overlay_entry
4786 {
4787 Lisp_Object overlay;
4788 Lisp_Object string;
4789 int priority;
4790 int after_string_p;
4791 };
4792
4793
4794 /* Set up iterator IT from overlay strings at its current position.
4795 Called from handle_stop. */
4796
4797 static enum prop_handled
4798 handle_overlay_change (struct it *it)
4799 {
4800 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4801 return HANDLED_RECOMPUTE_PROPS;
4802 else
4803 return HANDLED_NORMALLY;
4804 }
4805
4806
4807 /* Set up the next overlay string for delivery by IT, if there is an
4808 overlay string to deliver. Called by set_iterator_to_next when the
4809 end of the current overlay string is reached. If there are more
4810 overlay strings to display, IT->string and
4811 IT->current.overlay_string_index are set appropriately here.
4812 Otherwise IT->string is set to nil. */
4813
4814 static void
4815 next_overlay_string (struct it *it)
4816 {
4817 ++it->current.overlay_string_index;
4818 if (it->current.overlay_string_index == it->n_overlay_strings)
4819 {
4820 /* No more overlay strings. Restore IT's settings to what
4821 they were before overlay strings were processed, and
4822 continue to deliver from current_buffer. */
4823
4824 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4825 pop_it (it);
4826 xassert (it->sp > 0
4827 || (NILP (it->string)
4828 && it->method == GET_FROM_BUFFER
4829 && it->stop_charpos >= BEGV
4830 && it->stop_charpos <= it->end_charpos));
4831 it->current.overlay_string_index = -1;
4832 it->n_overlay_strings = 0;
4833 it->overlay_strings_charpos = -1;
4834
4835 /* If we're at the end of the buffer, record that we have
4836 processed the overlay strings there already, so that
4837 next_element_from_buffer doesn't try it again. */
4838 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4839 it->overlay_strings_at_end_processed_p = 1;
4840 }
4841 else
4842 {
4843 /* There are more overlay strings to process. If
4844 IT->current.overlay_string_index has advanced to a position
4845 where we must load IT->overlay_strings with more strings, do
4846 it. We must load at the IT->overlay_strings_charpos where
4847 IT->n_overlay_strings was originally computed; when invisible
4848 text is present, this might not be IT_CHARPOS (Bug#7016). */
4849 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4850
4851 if (it->current.overlay_string_index && i == 0)
4852 load_overlay_strings (it, it->overlay_strings_charpos);
4853
4854 /* Initialize IT to deliver display elements from the overlay
4855 string. */
4856 it->string = it->overlay_strings[i];
4857 it->multibyte_p = STRING_MULTIBYTE (it->string);
4858 SET_TEXT_POS (it->current.string_pos, 0, 0);
4859 it->method = GET_FROM_STRING;
4860 it->stop_charpos = 0;
4861 if (it->cmp_it.stop_pos >= 0)
4862 it->cmp_it.stop_pos = 0;
4863 it->prev_stop = 0;
4864 it->base_level_stop = 0;
4865
4866 /* Set up the bidi iterator for this overlay string. */
4867 if (it->bidi_p)
4868 {
4869 it->bidi_it.string.lstring = it->string;
4870 it->bidi_it.string.s = NULL;
4871 it->bidi_it.string.schars = SCHARS (it->string);
4872 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4873 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4874 it->bidi_it.string.unibyte = !it->multibyte_p;
4875 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4876 }
4877 }
4878
4879 CHECK_IT (it);
4880 }
4881
4882
4883 /* Compare two overlay_entry structures E1 and E2. Used as a
4884 comparison function for qsort in load_overlay_strings. Overlay
4885 strings for the same position are sorted so that
4886
4887 1. All after-strings come in front of before-strings, except
4888 when they come from the same overlay.
4889
4890 2. Within after-strings, strings are sorted so that overlay strings
4891 from overlays with higher priorities come first.
4892
4893 2. Within before-strings, strings are sorted so that overlay
4894 strings from overlays with higher priorities come last.
4895
4896 Value is analogous to strcmp. */
4897
4898
4899 static int
4900 compare_overlay_entries (const void *e1, const void *e2)
4901 {
4902 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4903 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4904 int result;
4905
4906 if (entry1->after_string_p != entry2->after_string_p)
4907 {
4908 /* Let after-strings appear in front of before-strings if
4909 they come from different overlays. */
4910 if (EQ (entry1->overlay, entry2->overlay))
4911 result = entry1->after_string_p ? 1 : -1;
4912 else
4913 result = entry1->after_string_p ? -1 : 1;
4914 }
4915 else if (entry1->after_string_p)
4916 /* After-strings sorted in order of decreasing priority. */
4917 result = entry2->priority - entry1->priority;
4918 else
4919 /* Before-strings sorted in order of increasing priority. */
4920 result = entry1->priority - entry2->priority;
4921
4922 return result;
4923 }
4924
4925
4926 /* Load the vector IT->overlay_strings with overlay strings from IT's
4927 current buffer position, or from CHARPOS if that is > 0. Set
4928 IT->n_overlays to the total number of overlay strings found.
4929
4930 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4931 a time. On entry into load_overlay_strings,
4932 IT->current.overlay_string_index gives the number of overlay
4933 strings that have already been loaded by previous calls to this
4934 function.
4935
4936 IT->add_overlay_start contains an additional overlay start
4937 position to consider for taking overlay strings from, if non-zero.
4938 This position comes into play when the overlay has an `invisible'
4939 property, and both before and after-strings. When we've skipped to
4940 the end of the overlay, because of its `invisible' property, we
4941 nevertheless want its before-string to appear.
4942 IT->add_overlay_start will contain the overlay start position
4943 in this case.
4944
4945 Overlay strings are sorted so that after-string strings come in
4946 front of before-string strings. Within before and after-strings,
4947 strings are sorted by overlay priority. See also function
4948 compare_overlay_entries. */
4949
4950 static void
4951 load_overlay_strings (struct it *it, EMACS_INT charpos)
4952 {
4953 Lisp_Object overlay, window, str, invisible;
4954 struct Lisp_Overlay *ov;
4955 EMACS_INT start, end;
4956 int size = 20;
4957 int n = 0, i, j, invis_p;
4958 struct overlay_entry *entries
4959 = (struct overlay_entry *) alloca (size * sizeof *entries);
4960
4961 if (charpos <= 0)
4962 charpos = IT_CHARPOS (*it);
4963
4964 /* Append the overlay string STRING of overlay OVERLAY to vector
4965 `entries' which has size `size' and currently contains `n'
4966 elements. AFTER_P non-zero means STRING is an after-string of
4967 OVERLAY. */
4968 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4969 do \
4970 { \
4971 Lisp_Object priority; \
4972 \
4973 if (n == size) \
4974 { \
4975 int new_size = 2 * size; \
4976 struct overlay_entry *old = entries; \
4977 entries = \
4978 (struct overlay_entry *) alloca (new_size \
4979 * sizeof *entries); \
4980 memcpy (entries, old, size * sizeof *entries); \
4981 size = new_size; \
4982 } \
4983 \
4984 entries[n].string = (STRING); \
4985 entries[n].overlay = (OVERLAY); \
4986 priority = Foverlay_get ((OVERLAY), Qpriority); \
4987 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4988 entries[n].after_string_p = (AFTER_P); \
4989 ++n; \
4990 } \
4991 while (0)
4992
4993 /* Process overlay before the overlay center. */
4994 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4995 {
4996 XSETMISC (overlay, ov);
4997 xassert (OVERLAYP (overlay));
4998 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4999 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5000
5001 if (end < charpos)
5002 break;
5003
5004 /* Skip this overlay if it doesn't start or end at IT's current
5005 position. */
5006 if (end != charpos && start != charpos)
5007 continue;
5008
5009 /* Skip this overlay if it doesn't apply to IT->w. */
5010 window = Foverlay_get (overlay, Qwindow);
5011 if (WINDOWP (window) && XWINDOW (window) != it->w)
5012 continue;
5013
5014 /* If the text ``under'' the overlay is invisible, both before-
5015 and after-strings from this overlay are visible; start and
5016 end position are indistinguishable. */
5017 invisible = Foverlay_get (overlay, Qinvisible);
5018 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5019
5020 /* If overlay has a non-empty before-string, record it. */
5021 if ((start == charpos || (end == charpos && invis_p))
5022 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5023 && SCHARS (str))
5024 RECORD_OVERLAY_STRING (overlay, str, 0);
5025
5026 /* If overlay has a non-empty after-string, record it. */
5027 if ((end == charpos || (start == charpos && invis_p))
5028 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5029 && SCHARS (str))
5030 RECORD_OVERLAY_STRING (overlay, str, 1);
5031 }
5032
5033 /* Process overlays after the overlay center. */
5034 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5035 {
5036 XSETMISC (overlay, ov);
5037 xassert (OVERLAYP (overlay));
5038 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5039 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5040
5041 if (start > charpos)
5042 break;
5043
5044 /* Skip this overlay if it doesn't start or end at IT's current
5045 position. */
5046 if (end != charpos && start != charpos)
5047 continue;
5048
5049 /* Skip this overlay if it doesn't apply to IT->w. */
5050 window = Foverlay_get (overlay, Qwindow);
5051 if (WINDOWP (window) && XWINDOW (window) != it->w)
5052 continue;
5053
5054 /* If the text ``under'' the overlay is invisible, it has a zero
5055 dimension, and both before- and after-strings apply. */
5056 invisible = Foverlay_get (overlay, Qinvisible);
5057 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5058
5059 /* If overlay has a non-empty before-string, record it. */
5060 if ((start == charpos || (end == charpos && invis_p))
5061 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5062 && SCHARS (str))
5063 RECORD_OVERLAY_STRING (overlay, str, 0);
5064
5065 /* If overlay has a non-empty after-string, record it. */
5066 if ((end == charpos || (start == charpos && invis_p))
5067 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5068 && SCHARS (str))
5069 RECORD_OVERLAY_STRING (overlay, str, 1);
5070 }
5071
5072 #undef RECORD_OVERLAY_STRING
5073
5074 /* Sort entries. */
5075 if (n > 1)
5076 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5077
5078 /* Record number of overlay strings, and where we computed it. */
5079 it->n_overlay_strings = n;
5080 it->overlay_strings_charpos = charpos;
5081
5082 /* IT->current.overlay_string_index is the number of overlay strings
5083 that have already been consumed by IT. Copy some of the
5084 remaining overlay strings to IT->overlay_strings. */
5085 i = 0;
5086 j = it->current.overlay_string_index;
5087 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5088 {
5089 it->overlay_strings[i] = entries[j].string;
5090 it->string_overlays[i++] = entries[j++].overlay;
5091 }
5092
5093 CHECK_IT (it);
5094 }
5095
5096
5097 /* Get the first chunk of overlay strings at IT's current buffer
5098 position, or at CHARPOS if that is > 0. Value is non-zero if at
5099 least one overlay string was found. */
5100
5101 static int
5102 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5103 {
5104 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5105 process. This fills IT->overlay_strings with strings, and sets
5106 IT->n_overlay_strings to the total number of strings to process.
5107 IT->pos.overlay_string_index has to be set temporarily to zero
5108 because load_overlay_strings needs this; it must be set to -1
5109 when no overlay strings are found because a zero value would
5110 indicate a position in the first overlay string. */
5111 it->current.overlay_string_index = 0;
5112 load_overlay_strings (it, charpos);
5113
5114 /* If we found overlay strings, set up IT to deliver display
5115 elements from the first one. Otherwise set up IT to deliver
5116 from current_buffer. */
5117 if (it->n_overlay_strings)
5118 {
5119 /* Make sure we know settings in current_buffer, so that we can
5120 restore meaningful values when we're done with the overlay
5121 strings. */
5122 if (compute_stop_p)
5123 compute_stop_pos (it);
5124 xassert (it->face_id >= 0);
5125
5126 /* Save IT's settings. They are restored after all overlay
5127 strings have been processed. */
5128 xassert (!compute_stop_p || it->sp == 0);
5129
5130 /* When called from handle_stop, there might be an empty display
5131 string loaded. In that case, don't bother saving it. */
5132 if (!STRINGP (it->string) || SCHARS (it->string))
5133 push_it (it, NULL);
5134
5135 /* Set up IT to deliver display elements from the first overlay
5136 string. */
5137 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5138 it->string = it->overlay_strings[0];
5139 it->from_overlay = Qnil;
5140 it->stop_charpos = 0;
5141 xassert (STRINGP (it->string));
5142 it->end_charpos = SCHARS (it->string);
5143 it->prev_stop = 0;
5144 it->base_level_stop = 0;
5145 it->multibyte_p = STRING_MULTIBYTE (it->string);
5146 it->method = GET_FROM_STRING;
5147
5148 /* Force paragraph direction to be that of the parent
5149 buffer. */
5150 it->paragraph_embedding = (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
5151
5152 /* Set up the bidi iterator for this overlay string. */
5153 if (it->bidi_p)
5154 {
5155 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5156
5157 it->bidi_it.string.lstring = it->string;
5158 it->bidi_it.string.s = NULL;
5159 it->bidi_it.string.schars = SCHARS (it->string);
5160 it->bidi_it.string.bufpos = pos;
5161 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5162 it->bidi_it.string.unibyte = !it->multibyte_p;
5163 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5164 }
5165 return 1;
5166 }
5167
5168 it->current.overlay_string_index = -1;
5169 return 0;
5170 }
5171
5172 static int
5173 get_overlay_strings (struct it *it, EMACS_INT charpos)
5174 {
5175 it->string = Qnil;
5176 it->method = GET_FROM_BUFFER;
5177
5178 (void) get_overlay_strings_1 (it, charpos, 1);
5179
5180 CHECK_IT (it);
5181
5182 /* Value is non-zero if we found at least one overlay string. */
5183 return STRINGP (it->string);
5184 }
5185
5186
5187 \f
5188 /***********************************************************************
5189 Saving and restoring state
5190 ***********************************************************************/
5191
5192 /* Save current settings of IT on IT->stack. Called, for example,
5193 before setting up IT for an overlay string, to be able to restore
5194 IT's settings to what they were after the overlay string has been
5195 processed. If POSITION is non-NULL, it is the position to save on
5196 the stack instead of IT->position. */
5197
5198 static void
5199 push_it (struct it *it, struct text_pos *position)
5200 {
5201 struct iterator_stack_entry *p;
5202
5203 xassert (it->sp < IT_STACK_SIZE);
5204 p = it->stack + it->sp;
5205
5206 p->stop_charpos = it->stop_charpos;
5207 p->prev_stop = it->prev_stop;
5208 p->base_level_stop = it->base_level_stop;
5209 p->cmp_it = it->cmp_it;
5210 xassert (it->face_id >= 0);
5211 p->face_id = it->face_id;
5212 p->string = it->string;
5213 p->method = it->method;
5214 p->from_overlay = it->from_overlay;
5215 switch (p->method)
5216 {
5217 case GET_FROM_IMAGE:
5218 p->u.image.object = it->object;
5219 p->u.image.image_id = it->image_id;
5220 p->u.image.slice = it->slice;
5221 break;
5222 case GET_FROM_STRETCH:
5223 p->u.stretch.object = it->object;
5224 break;
5225 }
5226 p->position = position ? *position : it->position;
5227 p->current = it->current;
5228 p->end_charpos = it->end_charpos;
5229 p->string_nchars = it->string_nchars;
5230 p->area = it->area;
5231 p->multibyte_p = it->multibyte_p;
5232 p->avoid_cursor_p = it->avoid_cursor_p;
5233 p->space_width = it->space_width;
5234 p->font_height = it->font_height;
5235 p->voffset = it->voffset;
5236 p->string_from_display_prop_p = it->string_from_display_prop_p;
5237 p->display_ellipsis_p = 0;
5238 p->line_wrap = it->line_wrap;
5239 p->bidi_p = it->bidi_p;
5240 p->paragraph_embedding = it->paragraph_embedding;
5241 ++it->sp;
5242
5243 /* Save the state of the bidi iterator as well. */
5244 if (it->bidi_p)
5245 bidi_push_it (&it->bidi_it);
5246 }
5247
5248 static void
5249 iterate_out_of_display_property (struct it *it)
5250 {
5251 int buffer_p = BUFFERP (it->object);
5252 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5253 EMACS_INT bob = (buffer_p ? BEGV : 0);
5254
5255 /* Maybe initialize paragraph direction. If we are at the beginning
5256 of a new paragraph, next_element_from_buffer may not have a
5257 chance to do that. */
5258 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5259 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5260 /* prev_stop can be zero, so check against BEGV as well. */
5261 while (it->bidi_it.charpos >= bob
5262 && it->prev_stop <= it->bidi_it.charpos
5263 && it->bidi_it.charpos < CHARPOS (it->position))
5264 bidi_move_to_visually_next (&it->bidi_it);
5265 /* Record the stop_pos we just crossed, for when we cross it
5266 back, maybe. */
5267 if (it->bidi_it.charpos > CHARPOS (it->position))
5268 it->prev_stop = CHARPOS (it->position);
5269 /* If we ended up not where pop_it put us, resync IT's
5270 positional members with the bidi iterator. */
5271 if (it->bidi_it.charpos != CHARPOS (it->position))
5272 {
5273 SET_TEXT_POS (it->position,
5274 it->bidi_it.charpos, it->bidi_it.bytepos);
5275 if (buffer_p)
5276 it->current.pos = it->position;
5277 else
5278 it->current.string_pos = it->position;
5279 }
5280 }
5281
5282 /* Restore IT's settings from IT->stack. Called, for example, when no
5283 more overlay strings must be processed, and we return to delivering
5284 display elements from a buffer, or when the end of a string from a
5285 `display' property is reached and we return to delivering display
5286 elements from an overlay string, or from a buffer. */
5287
5288 static void
5289 pop_it (struct it *it)
5290 {
5291 struct iterator_stack_entry *p;
5292
5293 xassert (it->sp > 0);
5294 --it->sp;
5295 p = it->stack + it->sp;
5296 it->stop_charpos = p->stop_charpos;
5297 it->prev_stop = p->prev_stop;
5298 it->base_level_stop = p->base_level_stop;
5299 it->cmp_it = p->cmp_it;
5300 it->face_id = p->face_id;
5301 it->current = p->current;
5302 it->position = p->position;
5303 it->string = p->string;
5304 it->from_overlay = p->from_overlay;
5305 if (NILP (it->string))
5306 SET_TEXT_POS (it->current.string_pos, -1, -1);
5307 it->method = p->method;
5308 switch (it->method)
5309 {
5310 case GET_FROM_IMAGE:
5311 it->image_id = p->u.image.image_id;
5312 it->object = p->u.image.object;
5313 it->slice = p->u.image.slice;
5314 break;
5315 case GET_FROM_STRETCH:
5316 it->object = p->u.comp.object;
5317 break;
5318 case GET_FROM_BUFFER:
5319 it->object = it->w->buffer;
5320 break;
5321 case GET_FROM_STRING:
5322 it->object = it->string;
5323 break;
5324 case GET_FROM_DISPLAY_VECTOR:
5325 if (it->s)
5326 it->method = GET_FROM_C_STRING;
5327 else if (STRINGP (it->string))
5328 it->method = GET_FROM_STRING;
5329 else
5330 {
5331 it->method = GET_FROM_BUFFER;
5332 it->object = it->w->buffer;
5333 }
5334 }
5335 it->end_charpos = p->end_charpos;
5336 it->string_nchars = p->string_nchars;
5337 it->area = p->area;
5338 it->multibyte_p = p->multibyte_p;
5339 it->avoid_cursor_p = p->avoid_cursor_p;
5340 it->space_width = p->space_width;
5341 it->font_height = p->font_height;
5342 it->voffset = p->voffset;
5343 it->string_from_display_prop_p = p->string_from_display_prop_p;
5344 it->line_wrap = p->line_wrap;
5345 it->bidi_p = p->bidi_p;
5346 it->paragraph_embedding = p->paragraph_embedding;
5347 if (it->bidi_p)
5348 {
5349 bidi_pop_it (&it->bidi_it);
5350 /* Bidi-iterate until we get out of the portion of text, if any,
5351 covered by a `display' text property or by an overlay with
5352 `display' property. (We cannot just jump there, because the
5353 internal coherency of the bidi iterator state can not be
5354 preserved across such jumps.) We also must determine the
5355 paragraph base direction if the overlay we just processed is
5356 at the beginning of a new paragraph. */
5357 if (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING)
5358 iterate_out_of_display_property (it);
5359 }
5360 }
5361
5362
5363 \f
5364 /***********************************************************************
5365 Moving over lines
5366 ***********************************************************************/
5367
5368 /* Set IT's current position to the previous line start. */
5369
5370 static void
5371 back_to_previous_line_start (struct it *it)
5372 {
5373 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5374 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5375 }
5376
5377
5378 /* Move IT to the next line start.
5379
5380 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5381 we skipped over part of the text (as opposed to moving the iterator
5382 continuously over the text). Otherwise, don't change the value
5383 of *SKIPPED_P.
5384
5385 Newlines may come from buffer text, overlay strings, or strings
5386 displayed via the `display' property. That's the reason we can't
5387 simply use find_next_newline_no_quit.
5388
5389 Note that this function may not skip over invisible text that is so
5390 because of text properties and immediately follows a newline. If
5391 it would, function reseat_at_next_visible_line_start, when called
5392 from set_iterator_to_next, would effectively make invisible
5393 characters following a newline part of the wrong glyph row, which
5394 leads to wrong cursor motion. */
5395
5396 static int
5397 forward_to_next_line_start (struct it *it, int *skipped_p)
5398 {
5399 int old_selective, newline_found_p, n;
5400 const int MAX_NEWLINE_DISTANCE = 500;
5401
5402 /* If already on a newline, just consume it to avoid unintended
5403 skipping over invisible text below. */
5404 if (it->what == IT_CHARACTER
5405 && it->c == '\n'
5406 && CHARPOS (it->position) == IT_CHARPOS (*it))
5407 {
5408 set_iterator_to_next (it, 0);
5409 it->c = 0;
5410 return 1;
5411 }
5412
5413 /* Don't handle selective display in the following. It's (a)
5414 unnecessary because it's done by the caller, and (b) leads to an
5415 infinite recursion because next_element_from_ellipsis indirectly
5416 calls this function. */
5417 old_selective = it->selective;
5418 it->selective = 0;
5419
5420 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5421 from buffer text. */
5422 for (n = newline_found_p = 0;
5423 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5424 n += STRINGP (it->string) ? 0 : 1)
5425 {
5426 if (!get_next_display_element (it))
5427 return 0;
5428 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5429 set_iterator_to_next (it, 0);
5430 }
5431
5432 /* If we didn't find a newline near enough, see if we can use a
5433 short-cut. */
5434 if (!newline_found_p)
5435 {
5436 EMACS_INT start = IT_CHARPOS (*it);
5437 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5438 Lisp_Object pos;
5439
5440 xassert (!STRINGP (it->string));
5441
5442 /* If we are not bidi-reordering, and there isn't any `display'
5443 property in sight, and no overlays, we can just use the
5444 position of the newline in buffer text. */
5445 if (!it->bidi_p
5446 && (it->stop_charpos >= limit
5447 || ((pos = Fnext_single_property_change (make_number (start),
5448 Qdisplay, Qnil,
5449 make_number (limit)),
5450 NILP (pos))
5451 && next_overlay_change (start) == ZV)))
5452 {
5453 IT_CHARPOS (*it) = limit;
5454 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5455 *skipped_p = newline_found_p = 1;
5456 }
5457 else
5458 {
5459 while (get_next_display_element (it)
5460 && !newline_found_p)
5461 {
5462 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5463 set_iterator_to_next (it, 0);
5464 }
5465 }
5466 }
5467
5468 it->selective = old_selective;
5469 return newline_found_p;
5470 }
5471
5472
5473 /* Set IT's current position to the previous visible line start. Skip
5474 invisible text that is so either due to text properties or due to
5475 selective display. Caution: this does not change IT->current_x and
5476 IT->hpos. */
5477
5478 static void
5479 back_to_previous_visible_line_start (struct it *it)
5480 {
5481 while (IT_CHARPOS (*it) > BEGV)
5482 {
5483 back_to_previous_line_start (it);
5484
5485 if (IT_CHARPOS (*it) <= BEGV)
5486 break;
5487
5488 /* If selective > 0, then lines indented more than its value are
5489 invisible. */
5490 if (it->selective > 0
5491 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5492 (double) it->selective)) /* iftc */
5493 continue;
5494
5495 /* Check the newline before point for invisibility. */
5496 {
5497 Lisp_Object prop;
5498 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5499 Qinvisible, it->window);
5500 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5501 continue;
5502 }
5503
5504 if (IT_CHARPOS (*it) <= BEGV)
5505 break;
5506
5507 {
5508 struct it it2;
5509 EMACS_INT pos;
5510 EMACS_INT beg, end;
5511 Lisp_Object val, overlay;
5512
5513 /* If newline is part of a composition, continue from start of composition */
5514 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5515 && beg < IT_CHARPOS (*it))
5516 goto replaced;
5517
5518 /* If newline is replaced by a display property, find start of overlay
5519 or interval and continue search from that point. */
5520 it2 = *it;
5521 pos = --IT_CHARPOS (it2);
5522 --IT_BYTEPOS (it2);
5523 it2.sp = 0;
5524 it2.string_from_display_prop_p = 0;
5525 if (handle_display_prop (&it2) == HANDLED_RETURN
5526 && !NILP (val = get_char_property_and_overlay
5527 (make_number (pos), Qdisplay, Qnil, &overlay))
5528 && (OVERLAYP (overlay)
5529 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5530 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5531 {
5532 /* If the call to handle_display_prop above pushed the
5533 iterator state, that causes side effects for the bidi
5534 iterator by calling bidi_push_it. Undo those side
5535 effects. */
5536 while (it2.sp > 0)
5537 {
5538 /* push_it calls bidi_push_it only if the bidi_p flag
5539 is set in the iterator being pushed. */
5540 if (it2.stack[--it2.sp].bidi_p)
5541 bidi_pop_it (&it2.bidi_it);
5542 }
5543 goto replaced;
5544 }
5545
5546 /* Newline is not replaced by anything -- so we are done. */
5547 break;
5548
5549 replaced:
5550 if (beg < BEGV)
5551 beg = BEGV;
5552 IT_CHARPOS (*it) = beg;
5553 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5554 }
5555 }
5556
5557 it->continuation_lines_width = 0;
5558
5559 xassert (IT_CHARPOS (*it) >= BEGV);
5560 xassert (IT_CHARPOS (*it) == BEGV
5561 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5562 CHECK_IT (it);
5563 }
5564
5565
5566 /* Reseat iterator IT at the previous visible line start. Skip
5567 invisible text that is so either due to text properties or due to
5568 selective display. At the end, update IT's overlay information,
5569 face information etc. */
5570
5571 void
5572 reseat_at_previous_visible_line_start (struct it *it)
5573 {
5574 back_to_previous_visible_line_start (it);
5575 reseat (it, it->current.pos, 1);
5576 CHECK_IT (it);
5577 }
5578
5579
5580 /* Reseat iterator IT on the next visible line start in the current
5581 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5582 preceding the line start. Skip over invisible text that is so
5583 because of selective display. Compute faces, overlays etc at the
5584 new position. Note that this function does not skip over text that
5585 is invisible because of text properties. */
5586
5587 static void
5588 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5589 {
5590 int newline_found_p, skipped_p = 0;
5591
5592 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5593
5594 /* Skip over lines that are invisible because they are indented
5595 more than the value of IT->selective. */
5596 if (it->selective > 0)
5597 while (IT_CHARPOS (*it) < ZV
5598 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5599 (double) it->selective)) /* iftc */
5600 {
5601 xassert (IT_BYTEPOS (*it) == BEGV
5602 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5603 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5604 }
5605
5606 /* Position on the newline if that's what's requested. */
5607 if (on_newline_p && newline_found_p)
5608 {
5609 if (STRINGP (it->string))
5610 {
5611 if (IT_STRING_CHARPOS (*it) > 0)
5612 {
5613 if (!it->bidi_p)
5614 {
5615 --IT_STRING_CHARPOS (*it);
5616 --IT_STRING_BYTEPOS (*it);
5617 }
5618 else
5619 /* Setting this flag will cause
5620 bidi_move_to_visually_next not to advance, but
5621 instead deliver the current character (newline),
5622 which is what the ON_NEWLINE_P flag wants. */
5623 it->bidi_it.first_elt = 1;
5624 }
5625 }
5626 else if (IT_CHARPOS (*it) > BEGV)
5627 {
5628 if (!it->bidi_p)
5629 {
5630 --IT_CHARPOS (*it);
5631 --IT_BYTEPOS (*it);
5632 }
5633 /* With bidi iteration, the call to `reseat' will cause
5634 bidi_move_to_visually_next deliver the current character,
5635 the newline, instead of advancing. */
5636 reseat (it, it->current.pos, 0);
5637 }
5638 }
5639 else if (skipped_p)
5640 reseat (it, it->current.pos, 0);
5641
5642 CHECK_IT (it);
5643 }
5644
5645
5646 \f
5647 /***********************************************************************
5648 Changing an iterator's position
5649 ***********************************************************************/
5650
5651 /* Change IT's current position to POS in current_buffer. If FORCE_P
5652 is non-zero, always check for text properties at the new position.
5653 Otherwise, text properties are only looked up if POS >=
5654 IT->check_charpos of a property. */
5655
5656 static void
5657 reseat (struct it *it, struct text_pos pos, int force_p)
5658 {
5659 EMACS_INT original_pos = IT_CHARPOS (*it);
5660
5661 reseat_1 (it, pos, 0);
5662
5663 /* Determine where to check text properties. Avoid doing it
5664 where possible because text property lookup is very expensive. */
5665 if (force_p
5666 || CHARPOS (pos) > it->stop_charpos
5667 || CHARPOS (pos) < original_pos)
5668 {
5669 if (it->bidi_p)
5670 {
5671 /* For bidi iteration, we need to prime prev_stop and
5672 base_level_stop with our best estimations. */
5673 if (CHARPOS (pos) < it->prev_stop)
5674 {
5675 handle_stop_backwards (it, BEGV);
5676 if (CHARPOS (pos) < it->base_level_stop)
5677 it->base_level_stop = 0;
5678 }
5679 else if (CHARPOS (pos) > it->stop_charpos
5680 && it->stop_charpos >= BEGV)
5681 handle_stop_backwards (it, it->stop_charpos);
5682 else /* force_p */
5683 handle_stop (it);
5684 }
5685 else
5686 {
5687 handle_stop (it);
5688 it->prev_stop = it->base_level_stop = 0;
5689 }
5690
5691 }
5692
5693 CHECK_IT (it);
5694 }
5695
5696
5697 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5698 IT->stop_pos to POS, also. */
5699
5700 static void
5701 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5702 {
5703 /* Don't call this function when scanning a C string. */
5704 xassert (it->s == NULL);
5705
5706 /* POS must be a reasonable value. */
5707 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5708
5709 it->current.pos = it->position = pos;
5710 it->end_charpos = ZV;
5711 it->dpvec = NULL;
5712 it->current.dpvec_index = -1;
5713 it->current.overlay_string_index = -1;
5714 IT_STRING_CHARPOS (*it) = -1;
5715 IT_STRING_BYTEPOS (*it) = -1;
5716 it->string = Qnil;
5717 it->method = GET_FROM_BUFFER;
5718 it->object = it->w->buffer;
5719 it->area = TEXT_AREA;
5720 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5721 it->sp = 0;
5722 it->string_from_display_prop_p = 0;
5723 it->face_before_selective_p = 0;
5724 if (it->bidi_p)
5725 {
5726 it->bidi_it.first_elt = 1;
5727 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5728 it->bidi_it.disp_pos = -1;
5729 it->bidi_it.string.s = NULL;
5730 it->bidi_it.string.lstring = Qnil;
5731 it->bidi_it.string.bufpos = 0;
5732 it->bidi_it.string.unibyte = 0;
5733 }
5734
5735 if (set_stop_p)
5736 {
5737 it->stop_charpos = CHARPOS (pos);
5738 it->base_level_stop = CHARPOS (pos);
5739 }
5740 }
5741
5742
5743 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5744 If S is non-null, it is a C string to iterate over. Otherwise,
5745 STRING gives a Lisp string to iterate over.
5746
5747 If PRECISION > 0, don't return more then PRECISION number of
5748 characters from the string.
5749
5750 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5751 characters have been returned. FIELD_WIDTH < 0 means an infinite
5752 field width.
5753
5754 MULTIBYTE = 0 means disable processing of multibyte characters,
5755 MULTIBYTE > 0 means enable it,
5756 MULTIBYTE < 0 means use IT->multibyte_p.
5757
5758 IT must be initialized via a prior call to init_iterator before
5759 calling this function. */
5760
5761 static void
5762 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5763 EMACS_INT charpos, EMACS_INT precision, int field_width,
5764 int multibyte)
5765 {
5766 /* No region in strings. */
5767 it->region_beg_charpos = it->region_end_charpos = -1;
5768
5769 /* No text property checks performed by default, but see below. */
5770 it->stop_charpos = -1;
5771
5772 /* Set iterator position and end position. */
5773 memset (&it->current, 0, sizeof it->current);
5774 it->current.overlay_string_index = -1;
5775 it->current.dpvec_index = -1;
5776 xassert (charpos >= 0);
5777
5778 /* If STRING is specified, use its multibyteness, otherwise use the
5779 setting of MULTIBYTE, if specified. */
5780 if (multibyte >= 0)
5781 it->multibyte_p = multibyte > 0;
5782
5783 /* Bidirectional reordering of strings is controlled by the default
5784 value of bidi-display-reordering. */
5785 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5786
5787 if (s == NULL)
5788 {
5789 xassert (STRINGP (string));
5790 it->string = string;
5791 it->s = NULL;
5792 it->end_charpos = it->string_nchars = SCHARS (string);
5793 it->method = GET_FROM_STRING;
5794 it->current.string_pos = string_pos (charpos, string);
5795
5796 if (it->bidi_p)
5797 {
5798 it->bidi_it.string.lstring = string;
5799 it->bidi_it.string.s = NULL;
5800 it->bidi_it.string.schars = it->end_charpos;
5801 it->bidi_it.string.bufpos = 0;
5802 it->bidi_it.string.from_disp_str = 0;
5803 it->bidi_it.string.unibyte = !it->multibyte_p;
5804 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5805 FRAME_WINDOW_P (it->f), &it->bidi_it);
5806 }
5807 }
5808 else
5809 {
5810 it->s = (const unsigned char *) s;
5811 it->string = Qnil;
5812
5813 /* Note that we use IT->current.pos, not it->current.string_pos,
5814 for displaying C strings. */
5815 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5816 if (it->multibyte_p)
5817 {
5818 it->current.pos = c_string_pos (charpos, s, 1);
5819 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5820 }
5821 else
5822 {
5823 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5824 it->end_charpos = it->string_nchars = strlen (s);
5825 }
5826
5827 if (it->bidi_p)
5828 {
5829 it->bidi_it.string.lstring = Qnil;
5830 it->bidi_it.string.s = s;
5831 it->bidi_it.string.schars = it->end_charpos;
5832 it->bidi_it.string.bufpos = 0;
5833 it->bidi_it.string.from_disp_str = 0;
5834 it->bidi_it.string.unibyte = !it->multibyte_p;
5835 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5836 &it->bidi_it);
5837 }
5838 it->method = GET_FROM_C_STRING;
5839 }
5840
5841 /* PRECISION > 0 means don't return more than PRECISION characters
5842 from the string. */
5843 if (precision > 0 && it->end_charpos - charpos > precision)
5844 {
5845 it->end_charpos = it->string_nchars = charpos + precision;
5846 if (it->bidi_p)
5847 it->bidi_it.string.schars = it->end_charpos;
5848 }
5849
5850 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5851 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5852 FIELD_WIDTH < 0 means infinite field width. This is useful for
5853 padding with `-' at the end of a mode line. */
5854 if (field_width < 0)
5855 field_width = INFINITY;
5856 /* Implementation note: We deliberately don't enlarge
5857 it->bidi_it.string.schars here to fit it->end_charpos, because
5858 the bidi iterator cannot produce characters out of thin air. */
5859 if (field_width > it->end_charpos - charpos)
5860 it->end_charpos = charpos + field_width;
5861
5862 /* Use the standard display table for displaying strings. */
5863 if (DISP_TABLE_P (Vstandard_display_table))
5864 it->dp = XCHAR_TABLE (Vstandard_display_table);
5865
5866 it->stop_charpos = charpos;
5867 it->prev_stop = charpos;
5868 it->base_level_stop = 0;
5869 if (it->bidi_p)
5870 {
5871 it->bidi_it.first_elt = 1;
5872 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5873 it->bidi_it.disp_pos = -1;
5874 }
5875 if (s == NULL && it->multibyte_p)
5876 {
5877 EMACS_INT endpos = SCHARS (it->string);
5878 if (endpos > it->end_charpos)
5879 endpos = it->end_charpos;
5880 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5881 it->string);
5882 }
5883 CHECK_IT (it);
5884 }
5885
5886
5887 \f
5888 /***********************************************************************
5889 Iteration
5890 ***********************************************************************/
5891
5892 /* Map enum it_method value to corresponding next_element_from_* function. */
5893
5894 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5895 {
5896 next_element_from_buffer,
5897 next_element_from_display_vector,
5898 next_element_from_string,
5899 next_element_from_c_string,
5900 next_element_from_image,
5901 next_element_from_stretch
5902 };
5903
5904 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5905
5906
5907 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5908 (possibly with the following characters). */
5909
5910 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5911 ((IT)->cmp_it.id >= 0 \
5912 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5913 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5914 END_CHARPOS, (IT)->w, \
5915 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5916 (IT)->string)))
5917
5918
5919 /* Lookup the char-table Vglyphless_char_display for character C (-1
5920 if we want information for no-font case), and return the display
5921 method symbol. By side-effect, update it->what and
5922 it->glyphless_method. This function is called from
5923 get_next_display_element for each character element, and from
5924 x_produce_glyphs when no suitable font was found. */
5925
5926 Lisp_Object
5927 lookup_glyphless_char_display (int c, struct it *it)
5928 {
5929 Lisp_Object glyphless_method = Qnil;
5930
5931 if (CHAR_TABLE_P (Vglyphless_char_display)
5932 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5933 {
5934 if (c >= 0)
5935 {
5936 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5937 if (CONSP (glyphless_method))
5938 glyphless_method = FRAME_WINDOW_P (it->f)
5939 ? XCAR (glyphless_method)
5940 : XCDR (glyphless_method);
5941 }
5942 else
5943 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
5944 }
5945
5946 retry:
5947 if (NILP (glyphless_method))
5948 {
5949 if (c >= 0)
5950 /* The default is to display the character by a proper font. */
5951 return Qnil;
5952 /* The default for the no-font case is to display an empty box. */
5953 glyphless_method = Qempty_box;
5954 }
5955 if (EQ (glyphless_method, Qzero_width))
5956 {
5957 if (c >= 0)
5958 return glyphless_method;
5959 /* This method can't be used for the no-font case. */
5960 glyphless_method = Qempty_box;
5961 }
5962 if (EQ (glyphless_method, Qthin_space))
5963 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
5964 else if (EQ (glyphless_method, Qempty_box))
5965 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
5966 else if (EQ (glyphless_method, Qhex_code))
5967 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
5968 else if (STRINGP (glyphless_method))
5969 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
5970 else
5971 {
5972 /* Invalid value. We use the default method. */
5973 glyphless_method = Qnil;
5974 goto retry;
5975 }
5976 it->what = IT_GLYPHLESS;
5977 return glyphless_method;
5978 }
5979
5980 /* Load IT's display element fields with information about the next
5981 display element from the current position of IT. Value is zero if
5982 end of buffer (or C string) is reached. */
5983
5984 static struct frame *last_escape_glyph_frame = NULL;
5985 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5986 static int last_escape_glyph_merged_face_id = 0;
5987
5988 struct frame *last_glyphless_glyph_frame = NULL;
5989 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
5990 int last_glyphless_glyph_merged_face_id = 0;
5991
5992 static int
5993 get_next_display_element (struct it *it)
5994 {
5995 /* Non-zero means that we found a display element. Zero means that
5996 we hit the end of what we iterate over. Performance note: the
5997 function pointer `method' used here turns out to be faster than
5998 using a sequence of if-statements. */
5999 int success_p;
6000
6001 get_next:
6002 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6003
6004 if (it->what == IT_CHARACTER)
6005 {
6006 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6007 and only if (a) the resolved directionality of that character
6008 is R..." */
6009 /* FIXME: Do we need an exception for characters from display
6010 tables? */
6011 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6012 it->c = bidi_mirror_char (it->c);
6013 /* Map via display table or translate control characters.
6014 IT->c, IT->len etc. have been set to the next character by
6015 the function call above. If we have a display table, and it
6016 contains an entry for IT->c, translate it. Don't do this if
6017 IT->c itself comes from a display table, otherwise we could
6018 end up in an infinite recursion. (An alternative could be to
6019 count the recursion depth of this function and signal an
6020 error when a certain maximum depth is reached.) Is it worth
6021 it? */
6022 if (success_p && it->dpvec == NULL)
6023 {
6024 Lisp_Object dv;
6025 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6026 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6027 nbsp_or_shy = char_is_other;
6028 int c = it->c; /* This is the character to display. */
6029
6030 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6031 {
6032 xassert (SINGLE_BYTE_CHAR_P (c));
6033 if (unibyte_display_via_language_environment)
6034 {
6035 c = DECODE_CHAR (unibyte, c);
6036 if (c < 0)
6037 c = BYTE8_TO_CHAR (it->c);
6038 }
6039 else
6040 c = BYTE8_TO_CHAR (it->c);
6041 }
6042
6043 if (it->dp
6044 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6045 VECTORP (dv)))
6046 {
6047 struct Lisp_Vector *v = XVECTOR (dv);
6048
6049 /* Return the first character from the display table
6050 entry, if not empty. If empty, don't display the
6051 current character. */
6052 if (v->header.size)
6053 {
6054 it->dpvec_char_len = it->len;
6055 it->dpvec = v->contents;
6056 it->dpend = v->contents + v->header.size;
6057 it->current.dpvec_index = 0;
6058 it->dpvec_face_id = -1;
6059 it->saved_face_id = it->face_id;
6060 it->method = GET_FROM_DISPLAY_VECTOR;
6061 it->ellipsis_p = 0;
6062 }
6063 else
6064 {
6065 set_iterator_to_next (it, 0);
6066 }
6067 goto get_next;
6068 }
6069
6070 if (! NILP (lookup_glyphless_char_display (c, it)))
6071 {
6072 if (it->what == IT_GLYPHLESS)
6073 goto done;
6074 /* Don't display this character. */
6075 set_iterator_to_next (it, 0);
6076 goto get_next;
6077 }
6078
6079 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6080 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6081 : c == 0xAD ? char_is_soft_hyphen
6082 : char_is_other);
6083
6084 /* Translate control characters into `\003' or `^C' form.
6085 Control characters coming from a display table entry are
6086 currently not translated because we use IT->dpvec to hold
6087 the translation. This could easily be changed but I
6088 don't believe that it is worth doing.
6089
6090 NBSP and SOFT-HYPEN are property translated too.
6091
6092 Non-printable characters and raw-byte characters are also
6093 translated to octal form. */
6094 if (((c < ' ' || c == 127) /* ASCII control chars */
6095 ? (it->area != TEXT_AREA
6096 /* In mode line, treat \n, \t like other crl chars. */
6097 || (c != '\t'
6098 && it->glyph_row
6099 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6100 || (c != '\n' && c != '\t'))
6101 : (nbsp_or_shy
6102 || CHAR_BYTE8_P (c)
6103 || ! CHAR_PRINTABLE_P (c))))
6104 {
6105 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6106 or a non-printable character which must be displayed
6107 either as '\003' or as `^C' where the '\\' and '^'
6108 can be defined in the display table. Fill
6109 IT->ctl_chars with glyphs for what we have to
6110 display. Then, set IT->dpvec to these glyphs. */
6111 Lisp_Object gc;
6112 int ctl_len;
6113 int face_id, lface_id = 0 ;
6114 int escape_glyph;
6115
6116 /* Handle control characters with ^. */
6117
6118 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6119 {
6120 int g;
6121
6122 g = '^'; /* default glyph for Control */
6123 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6124 if (it->dp
6125 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6126 && GLYPH_CODE_CHAR_VALID_P (gc))
6127 {
6128 g = GLYPH_CODE_CHAR (gc);
6129 lface_id = GLYPH_CODE_FACE (gc);
6130 }
6131 if (lface_id)
6132 {
6133 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6134 }
6135 else if (it->f == last_escape_glyph_frame
6136 && it->face_id == last_escape_glyph_face_id)
6137 {
6138 face_id = last_escape_glyph_merged_face_id;
6139 }
6140 else
6141 {
6142 /* Merge the escape-glyph face into the current face. */
6143 face_id = merge_faces (it->f, Qescape_glyph, 0,
6144 it->face_id);
6145 last_escape_glyph_frame = it->f;
6146 last_escape_glyph_face_id = it->face_id;
6147 last_escape_glyph_merged_face_id = face_id;
6148 }
6149
6150 XSETINT (it->ctl_chars[0], g);
6151 XSETINT (it->ctl_chars[1], c ^ 0100);
6152 ctl_len = 2;
6153 goto display_control;
6154 }
6155
6156 /* Handle non-break space in the mode where it only gets
6157 highlighting. */
6158
6159 if (EQ (Vnobreak_char_display, Qt)
6160 && nbsp_or_shy == char_is_nbsp)
6161 {
6162 /* Merge the no-break-space face into the current face. */
6163 face_id = merge_faces (it->f, Qnobreak_space, 0,
6164 it->face_id);
6165
6166 c = ' ';
6167 XSETINT (it->ctl_chars[0], ' ');
6168 ctl_len = 1;
6169 goto display_control;
6170 }
6171
6172 /* Handle sequences that start with the "escape glyph". */
6173
6174 /* the default escape glyph is \. */
6175 escape_glyph = '\\';
6176
6177 if (it->dp
6178 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6179 && GLYPH_CODE_CHAR_VALID_P (gc))
6180 {
6181 escape_glyph = GLYPH_CODE_CHAR (gc);
6182 lface_id = GLYPH_CODE_FACE (gc);
6183 }
6184 if (lface_id)
6185 {
6186 /* The display table specified a face.
6187 Merge it into face_id and also into escape_glyph. */
6188 face_id = merge_faces (it->f, Qt, lface_id,
6189 it->face_id);
6190 }
6191 else if (it->f == last_escape_glyph_frame
6192 && it->face_id == last_escape_glyph_face_id)
6193 {
6194 face_id = last_escape_glyph_merged_face_id;
6195 }
6196 else
6197 {
6198 /* Merge the escape-glyph face into the current face. */
6199 face_id = merge_faces (it->f, Qescape_glyph, 0,
6200 it->face_id);
6201 last_escape_glyph_frame = it->f;
6202 last_escape_glyph_face_id = it->face_id;
6203 last_escape_glyph_merged_face_id = face_id;
6204 }
6205
6206 /* Handle soft hyphens in the mode where they only get
6207 highlighting. */
6208
6209 if (EQ (Vnobreak_char_display, Qt)
6210 && nbsp_or_shy == char_is_soft_hyphen)
6211 {
6212 XSETINT (it->ctl_chars[0], '-');
6213 ctl_len = 1;
6214 goto display_control;
6215 }
6216
6217 /* Handle non-break space and soft hyphen
6218 with the escape glyph. */
6219
6220 if (nbsp_or_shy)
6221 {
6222 XSETINT (it->ctl_chars[0], escape_glyph);
6223 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6224 XSETINT (it->ctl_chars[1], c);
6225 ctl_len = 2;
6226 goto display_control;
6227 }
6228
6229 {
6230 char str[10];
6231 int len, i;
6232
6233 if (CHAR_BYTE8_P (c))
6234 /* Display \200 instead of \17777600. */
6235 c = CHAR_TO_BYTE8 (c);
6236 len = sprintf (str, "%03o", c);
6237
6238 XSETINT (it->ctl_chars[0], escape_glyph);
6239 for (i = 0; i < len; i++)
6240 XSETINT (it->ctl_chars[i + 1], str[i]);
6241 ctl_len = len + 1;
6242 }
6243
6244 display_control:
6245 /* Set up IT->dpvec and return first character from it. */
6246 it->dpvec_char_len = it->len;
6247 it->dpvec = it->ctl_chars;
6248 it->dpend = it->dpvec + ctl_len;
6249 it->current.dpvec_index = 0;
6250 it->dpvec_face_id = face_id;
6251 it->saved_face_id = it->face_id;
6252 it->method = GET_FROM_DISPLAY_VECTOR;
6253 it->ellipsis_p = 0;
6254 goto get_next;
6255 }
6256 it->char_to_display = c;
6257 }
6258 else if (success_p)
6259 {
6260 it->char_to_display = it->c;
6261 }
6262 }
6263
6264 /* Adjust face id for a multibyte character. There are no multibyte
6265 character in unibyte text. */
6266 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6267 && it->multibyte_p
6268 && success_p
6269 && FRAME_WINDOW_P (it->f))
6270 {
6271 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6272
6273 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6274 {
6275 /* Automatic composition with glyph-string. */
6276 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6277
6278 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6279 }
6280 else
6281 {
6282 EMACS_INT pos = (it->s ? -1
6283 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6284 : IT_CHARPOS (*it));
6285
6286 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6287 it->string);
6288 }
6289 }
6290
6291 done:
6292 /* Is this character the last one of a run of characters with
6293 box? If yes, set IT->end_of_box_run_p to 1. */
6294 if (it->face_box_p
6295 && it->s == NULL)
6296 {
6297 if (it->method == GET_FROM_STRING && it->sp)
6298 {
6299 int face_id = underlying_face_id (it);
6300 struct face *face = FACE_FROM_ID (it->f, face_id);
6301
6302 if (face)
6303 {
6304 if (face->box == FACE_NO_BOX)
6305 {
6306 /* If the box comes from face properties in a
6307 display string, check faces in that string. */
6308 int string_face_id = face_after_it_pos (it);
6309 it->end_of_box_run_p
6310 = (FACE_FROM_ID (it->f, string_face_id)->box
6311 == FACE_NO_BOX);
6312 }
6313 /* Otherwise, the box comes from the underlying face.
6314 If this is the last string character displayed, check
6315 the next buffer location. */
6316 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6317 && (it->current.overlay_string_index
6318 == it->n_overlay_strings - 1))
6319 {
6320 EMACS_INT ignore;
6321 int next_face_id;
6322 struct text_pos pos = it->current.pos;
6323 INC_TEXT_POS (pos, it->multibyte_p);
6324
6325 next_face_id = face_at_buffer_position
6326 (it->w, CHARPOS (pos), it->region_beg_charpos,
6327 it->region_end_charpos, &ignore,
6328 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6329 -1);
6330 it->end_of_box_run_p
6331 = (FACE_FROM_ID (it->f, next_face_id)->box
6332 == FACE_NO_BOX);
6333 }
6334 }
6335 }
6336 else
6337 {
6338 int face_id = face_after_it_pos (it);
6339 it->end_of_box_run_p
6340 = (face_id != it->face_id
6341 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6342 }
6343 }
6344
6345 /* Value is 0 if end of buffer or string reached. */
6346 return success_p;
6347 }
6348
6349
6350 /* Move IT to the next display element.
6351
6352 RESEAT_P non-zero means if called on a newline in buffer text,
6353 skip to the next visible line start.
6354
6355 Functions get_next_display_element and set_iterator_to_next are
6356 separate because I find this arrangement easier to handle than a
6357 get_next_display_element function that also increments IT's
6358 position. The way it is we can first look at an iterator's current
6359 display element, decide whether it fits on a line, and if it does,
6360 increment the iterator position. The other way around we probably
6361 would either need a flag indicating whether the iterator has to be
6362 incremented the next time, or we would have to implement a
6363 decrement position function which would not be easy to write. */
6364
6365 void
6366 set_iterator_to_next (struct it *it, int reseat_p)
6367 {
6368 /* Reset flags indicating start and end of a sequence of characters
6369 with box. Reset them at the start of this function because
6370 moving the iterator to a new position might set them. */
6371 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6372
6373 switch (it->method)
6374 {
6375 case GET_FROM_BUFFER:
6376 /* The current display element of IT is a character from
6377 current_buffer. Advance in the buffer, and maybe skip over
6378 invisible lines that are so because of selective display. */
6379 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6380 reseat_at_next_visible_line_start (it, 0);
6381 else if (it->cmp_it.id >= 0)
6382 {
6383 /* We are currently getting glyphs from a composition. */
6384 int i;
6385
6386 if (! it->bidi_p)
6387 {
6388 IT_CHARPOS (*it) += it->cmp_it.nchars;
6389 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6390 if (it->cmp_it.to < it->cmp_it.nglyphs)
6391 {
6392 it->cmp_it.from = it->cmp_it.to;
6393 }
6394 else
6395 {
6396 it->cmp_it.id = -1;
6397 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6398 IT_BYTEPOS (*it),
6399 it->end_charpos, Qnil);
6400 }
6401 }
6402 else if (! it->cmp_it.reversed_p)
6403 {
6404 /* Composition created while scanning forward. */
6405 /* Update IT's char/byte positions to point to the first
6406 character of the next grapheme cluster, or to the
6407 character visually after the current composition. */
6408 for (i = 0; i < it->cmp_it.nchars; i++)
6409 bidi_move_to_visually_next (&it->bidi_it);
6410 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6411 IT_CHARPOS (*it) = it->bidi_it.charpos;
6412
6413 if (it->cmp_it.to < it->cmp_it.nglyphs)
6414 {
6415 /* Proceed to the next grapheme cluster. */
6416 it->cmp_it.from = it->cmp_it.to;
6417 }
6418 else
6419 {
6420 /* No more grapheme clusters in this composition.
6421 Find the next stop position. */
6422 EMACS_INT stop = it->end_charpos;
6423 if (it->bidi_it.scan_dir < 0)
6424 /* Now we are scanning backward and don't know
6425 where to stop. */
6426 stop = -1;
6427 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6428 IT_BYTEPOS (*it), stop, Qnil);
6429 }
6430 }
6431 else
6432 {
6433 /* Composition created while scanning backward. */
6434 /* Update IT's char/byte positions to point to the last
6435 character of the previous grapheme cluster, or the
6436 character visually after the current composition. */
6437 for (i = 0; i < it->cmp_it.nchars; i++)
6438 bidi_move_to_visually_next (&it->bidi_it);
6439 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6440 IT_CHARPOS (*it) = it->bidi_it.charpos;
6441 if (it->cmp_it.from > 0)
6442 {
6443 /* Proceed to the previous grapheme cluster. */
6444 it->cmp_it.to = it->cmp_it.from;
6445 }
6446 else
6447 {
6448 /* No more grapheme clusters in this composition.
6449 Find the next stop position. */
6450 EMACS_INT stop = it->end_charpos;
6451 if (it->bidi_it.scan_dir < 0)
6452 /* Now we are scanning backward and don't know
6453 where to stop. */
6454 stop = -1;
6455 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6456 IT_BYTEPOS (*it), stop, Qnil);
6457 }
6458 }
6459 }
6460 else
6461 {
6462 xassert (it->len != 0);
6463
6464 if (!it->bidi_p)
6465 {
6466 IT_BYTEPOS (*it) += it->len;
6467 IT_CHARPOS (*it) += 1;
6468 }
6469 else
6470 {
6471 int prev_scan_dir = it->bidi_it.scan_dir;
6472 /* If this is a new paragraph, determine its base
6473 direction (a.k.a. its base embedding level). */
6474 if (it->bidi_it.new_paragraph)
6475 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6476 bidi_move_to_visually_next (&it->bidi_it);
6477 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6478 IT_CHARPOS (*it) = it->bidi_it.charpos;
6479 if (prev_scan_dir != it->bidi_it.scan_dir)
6480 {
6481 /* As the scan direction was changed, we must
6482 re-compute the stop position for composition. */
6483 EMACS_INT stop = it->end_charpos;
6484 if (it->bidi_it.scan_dir < 0)
6485 stop = -1;
6486 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6487 IT_BYTEPOS (*it), stop, Qnil);
6488 }
6489 }
6490 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6491 }
6492 break;
6493
6494 case GET_FROM_C_STRING:
6495 /* Current display element of IT is from a C string. */
6496 if (!it->bidi_p
6497 /* If the string position is beyond string's end, it means
6498 next_element_from_c_string is padding the string with
6499 blanks, in which case we bypass the bidi iterator,
6500 because it cannot deal with such virtual characters. */
6501 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6502 {
6503 IT_BYTEPOS (*it) += it->len;
6504 IT_CHARPOS (*it) += 1;
6505 }
6506 else
6507 {
6508 bidi_move_to_visually_next (&it->bidi_it);
6509 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6510 IT_CHARPOS (*it) = it->bidi_it.charpos;
6511 }
6512 break;
6513
6514 case GET_FROM_DISPLAY_VECTOR:
6515 /* Current display element of IT is from a display table entry.
6516 Advance in the display table definition. Reset it to null if
6517 end reached, and continue with characters from buffers/
6518 strings. */
6519 ++it->current.dpvec_index;
6520
6521 /* Restore face of the iterator to what they were before the
6522 display vector entry (these entries may contain faces). */
6523 it->face_id = it->saved_face_id;
6524
6525 if (it->dpvec + it->current.dpvec_index == it->dpend)
6526 {
6527 int recheck_faces = it->ellipsis_p;
6528
6529 if (it->s)
6530 it->method = GET_FROM_C_STRING;
6531 else if (STRINGP (it->string))
6532 it->method = GET_FROM_STRING;
6533 else
6534 {
6535 it->method = GET_FROM_BUFFER;
6536 it->object = it->w->buffer;
6537 }
6538
6539 it->dpvec = NULL;
6540 it->current.dpvec_index = -1;
6541
6542 /* Skip over characters which were displayed via IT->dpvec. */
6543 if (it->dpvec_char_len < 0)
6544 reseat_at_next_visible_line_start (it, 1);
6545 else if (it->dpvec_char_len > 0)
6546 {
6547 if (it->method == GET_FROM_STRING
6548 && it->n_overlay_strings > 0)
6549 it->ignore_overlay_strings_at_pos_p = 1;
6550 it->len = it->dpvec_char_len;
6551 set_iterator_to_next (it, reseat_p);
6552 }
6553
6554 /* Maybe recheck faces after display vector */
6555 if (recheck_faces)
6556 it->stop_charpos = IT_CHARPOS (*it);
6557 }
6558 break;
6559
6560 case GET_FROM_STRING:
6561 /* Current display element is a character from a Lisp string. */
6562 xassert (it->s == NULL && STRINGP (it->string));
6563 if (it->cmp_it.id >= 0)
6564 {
6565 int i;
6566
6567 if (! it->bidi_p)
6568 {
6569 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6570 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6571 if (it->cmp_it.to < it->cmp_it.nglyphs)
6572 it->cmp_it.from = it->cmp_it.to;
6573 else
6574 {
6575 it->cmp_it.id = -1;
6576 composition_compute_stop_pos (&it->cmp_it,
6577 IT_STRING_CHARPOS (*it),
6578 IT_STRING_BYTEPOS (*it),
6579 it->end_charpos, it->string);
6580 }
6581 }
6582 else if (! it->cmp_it.reversed_p)
6583 {
6584 for (i = 0; i < it->cmp_it.nchars; i++)
6585 bidi_move_to_visually_next (&it->bidi_it);
6586 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6587 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6588
6589 if (it->cmp_it.to < it->cmp_it.nglyphs)
6590 it->cmp_it.from = it->cmp_it.to;
6591 else
6592 {
6593 EMACS_INT stop = it->end_charpos;
6594 if (it->bidi_it.scan_dir < 0)
6595 stop = -1;
6596 composition_compute_stop_pos (&it->cmp_it,
6597 IT_STRING_CHARPOS (*it),
6598 IT_STRING_BYTEPOS (*it), stop,
6599 it->string);
6600 }
6601 }
6602 else
6603 {
6604 for (i = 0; i < it->cmp_it.nchars; i++)
6605 bidi_move_to_visually_next (&it->bidi_it);
6606 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6607 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6608 if (it->cmp_it.from > 0)
6609 it->cmp_it.to = it->cmp_it.from;
6610 else
6611 {
6612 EMACS_INT stop = it->end_charpos;
6613 if (it->bidi_it.scan_dir < 0)
6614 stop = -1;
6615 composition_compute_stop_pos (&it->cmp_it,
6616 IT_STRING_CHARPOS (*it),
6617 IT_STRING_BYTEPOS (*it), stop,
6618 it->string);
6619 }
6620 }
6621 }
6622 else
6623 {
6624 if (!it->bidi_p
6625 /* If the string position is beyond string's end, it
6626 means next_element_from_string is padding the string
6627 with blanks, in which case we bypass the bidi
6628 iterator, because it cannot deal with such virtual
6629 characters. */
6630 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6631 {
6632 IT_STRING_BYTEPOS (*it) += it->len;
6633 IT_STRING_CHARPOS (*it) += 1;
6634 }
6635 else
6636 {
6637 int prev_scan_dir = it->bidi_it.scan_dir;
6638
6639 bidi_move_to_visually_next (&it->bidi_it);
6640 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6641 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6642 if (prev_scan_dir != it->bidi_it.scan_dir)
6643 {
6644 EMACS_INT stop = it->end_charpos;
6645
6646 if (it->bidi_it.scan_dir < 0)
6647 stop = -1;
6648 composition_compute_stop_pos (&it->cmp_it,
6649 IT_STRING_CHARPOS (*it),
6650 IT_STRING_BYTEPOS (*it), stop,
6651 it->string);
6652 }
6653 }
6654 }
6655
6656 consider_string_end:
6657
6658 if (it->current.overlay_string_index >= 0)
6659 {
6660 /* IT->string is an overlay string. Advance to the
6661 next, if there is one. */
6662 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6663 {
6664 it->ellipsis_p = 0;
6665 next_overlay_string (it);
6666 if (it->ellipsis_p)
6667 setup_for_ellipsis (it, 0);
6668 }
6669 }
6670 else
6671 {
6672 /* IT->string is not an overlay string. If we reached
6673 its end, and there is something on IT->stack, proceed
6674 with what is on the stack. This can be either another
6675 string, this time an overlay string, or a buffer. */
6676 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6677 && it->sp > 0)
6678 {
6679 pop_it (it);
6680 if (it->method == GET_FROM_STRING)
6681 goto consider_string_end;
6682 }
6683 }
6684 break;
6685
6686 case GET_FROM_IMAGE:
6687 case GET_FROM_STRETCH:
6688 /* The position etc with which we have to proceed are on
6689 the stack. The position may be at the end of a string,
6690 if the `display' property takes up the whole string. */
6691 xassert (it->sp > 0);
6692 pop_it (it);
6693 if (it->method == GET_FROM_STRING)
6694 goto consider_string_end;
6695 break;
6696
6697 default:
6698 /* There are no other methods defined, so this should be a bug. */
6699 abort ();
6700 }
6701
6702 xassert (it->method != GET_FROM_STRING
6703 || (STRINGP (it->string)
6704 && IT_STRING_CHARPOS (*it) >= 0));
6705 }
6706
6707 /* Load IT's display element fields with information about the next
6708 display element which comes from a display table entry or from the
6709 result of translating a control character to one of the forms `^C'
6710 or `\003'.
6711
6712 IT->dpvec holds the glyphs to return as characters.
6713 IT->saved_face_id holds the face id before the display vector--it
6714 is restored into IT->face_id in set_iterator_to_next. */
6715
6716 static int
6717 next_element_from_display_vector (struct it *it)
6718 {
6719 Lisp_Object gc;
6720
6721 /* Precondition. */
6722 xassert (it->dpvec && it->current.dpvec_index >= 0);
6723
6724 it->face_id = it->saved_face_id;
6725
6726 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6727 That seemed totally bogus - so I changed it... */
6728 gc = it->dpvec[it->current.dpvec_index];
6729
6730 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6731 {
6732 it->c = GLYPH_CODE_CHAR (gc);
6733 it->len = CHAR_BYTES (it->c);
6734
6735 /* The entry may contain a face id to use. Such a face id is
6736 the id of a Lisp face, not a realized face. A face id of
6737 zero means no face is specified. */
6738 if (it->dpvec_face_id >= 0)
6739 it->face_id = it->dpvec_face_id;
6740 else
6741 {
6742 int lface_id = GLYPH_CODE_FACE (gc);
6743 if (lface_id > 0)
6744 it->face_id = merge_faces (it->f, Qt, lface_id,
6745 it->saved_face_id);
6746 }
6747 }
6748 else
6749 /* Display table entry is invalid. Return a space. */
6750 it->c = ' ', it->len = 1;
6751
6752 /* Don't change position and object of the iterator here. They are
6753 still the values of the character that had this display table
6754 entry or was translated, and that's what we want. */
6755 it->what = IT_CHARACTER;
6756 return 1;
6757 }
6758
6759 /* Get the first element of string/buffer in the visual order, after
6760 being reseated to a new position in a string or a buffer. */
6761 static void
6762 get_visually_first_element (struct it *it)
6763 {
6764 int string_p = STRINGP (it->string) || it->s;
6765 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6766 EMACS_INT bob = (string_p ? 0 : BEGV);
6767
6768 if (STRINGP (it->string))
6769 {
6770 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6771 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6772 }
6773 else
6774 {
6775 it->bidi_it.charpos = IT_CHARPOS (*it);
6776 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6777 }
6778
6779 if (it->bidi_it.charpos == eob)
6780 {
6781 /* Nothing to do, but reset the FIRST_ELT flag, like
6782 bidi_paragraph_init does, because we are not going to
6783 call it. */
6784 it->bidi_it.first_elt = 0;
6785 }
6786 else if (it->bidi_it.charpos == bob
6787 || (!string_p
6788 /* FIXME: Should support all Unicode line separators. */
6789 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6790 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6791 {
6792 /* If we are at the beginning of a line/string, we can produce
6793 the next element right away. */
6794 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6795 bidi_move_to_visually_next (&it->bidi_it);
6796 }
6797 else
6798 {
6799 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6800
6801 /* We need to prime the bidi iterator starting at the line's or
6802 string's beginning, before we will be able to produce the
6803 next element. */
6804 if (string_p)
6805 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6806 else
6807 {
6808 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6809 -1);
6810 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6811 }
6812 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6813 do
6814 {
6815 /* Now return to buffer/string position where we were asked
6816 to get the next display element, and produce that. */
6817 bidi_move_to_visually_next (&it->bidi_it);
6818 }
6819 while (it->bidi_it.bytepos != orig_bytepos
6820 && it->bidi_it.charpos < eob);
6821 }
6822
6823 /* Adjust IT's position information to where we ended up. */
6824 if (STRINGP (it->string))
6825 {
6826 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6827 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6828 }
6829 else
6830 {
6831 IT_CHARPOS (*it) = it->bidi_it.charpos;
6832 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6833 }
6834
6835 if (STRINGP (it->string) || !it->s)
6836 {
6837 EMACS_INT stop, charpos, bytepos;
6838
6839 if (STRINGP (it->string))
6840 {
6841 xassert (!it->s);
6842 stop = SCHARS (it->string);
6843 if (stop > it->end_charpos)
6844 stop = it->end_charpos;
6845 charpos = IT_STRING_CHARPOS (*it);
6846 bytepos = IT_STRING_BYTEPOS (*it);
6847 }
6848 else
6849 {
6850 stop = it->end_charpos;
6851 charpos = IT_CHARPOS (*it);
6852 bytepos = IT_BYTEPOS (*it);
6853 }
6854 if (it->bidi_it.scan_dir < 0)
6855 stop = -1;
6856 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6857 it->string);
6858 }
6859 }
6860
6861 /* Load IT with the next display element from Lisp string IT->string.
6862 IT->current.string_pos is the current position within the string.
6863 If IT->current.overlay_string_index >= 0, the Lisp string is an
6864 overlay string. */
6865
6866 static int
6867 next_element_from_string (struct it *it)
6868 {
6869 struct text_pos position;
6870
6871 xassert (STRINGP (it->string));
6872 xassert (!it->bidi_p || it->string == it->bidi_it.string.lstring);
6873 xassert (IT_STRING_CHARPOS (*it) >= 0);
6874 position = it->current.string_pos;
6875
6876 /* With bidi reordering, the character to display might not be the
6877 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
6878 that we were reseat()ed to a new string, whose paragraph
6879 direction is not known. */
6880 if (it->bidi_p && it->bidi_it.first_elt)
6881 {
6882 get_visually_first_element (it);
6883 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
6884 }
6885
6886 /* Time to check for invisible text? */
6887 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
6888 {
6889 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
6890 {
6891 if (!(!it->bidi_p
6892 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6893 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
6894 {
6895 /* With bidi non-linear iteration, we could find
6896 ourselves far beyond the last computed stop_charpos,
6897 with several other stop positions in between that we
6898 missed. Scan them all now, in buffer's logical
6899 order, until we find and handle the last stop_charpos
6900 that precedes our current position. */
6901 handle_stop_backwards (it, it->stop_charpos);
6902 return GET_NEXT_DISPLAY_ELEMENT (it);
6903 }
6904 else
6905 {
6906 if (it->bidi_p)
6907 {
6908 /* Take note of the stop position we just moved
6909 across, for when we will move back across it. */
6910 it->prev_stop = it->stop_charpos;
6911 /* If we are at base paragraph embedding level, take
6912 note of the last stop position seen at this
6913 level. */
6914 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6915 it->base_level_stop = it->stop_charpos;
6916 }
6917 handle_stop (it);
6918
6919 /* Since a handler may have changed IT->method, we must
6920 recurse here. */
6921 return GET_NEXT_DISPLAY_ELEMENT (it);
6922 }
6923 }
6924 else if (it->bidi_p
6925 /* If we are before prev_stop, we may have overstepped
6926 on our way backwards a stop_pos, and if so, we need
6927 to handle that stop_pos. */
6928 && IT_STRING_CHARPOS (*it) < it->prev_stop
6929 /* We can sometimes back up for reasons that have nothing
6930 to do with bidi reordering. E.g., compositions. The
6931 code below is only needed when we are above the base
6932 embedding level, so test for that explicitly. */
6933 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
6934 {
6935 /* If we lost track of base_level_stop, we have no better place
6936 for handle_stop_backwards to start from than BEGV. This
6937 happens, e.g., when we were reseated to the previous
6938 screenful of text by vertical-motion. */
6939 if (it->base_level_stop <= 0
6940 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
6941 it->base_level_stop = 0;
6942 handle_stop_backwards (it, it->base_level_stop);
6943 return GET_NEXT_DISPLAY_ELEMENT (it);
6944 }
6945 }
6946
6947 if (it->current.overlay_string_index >= 0)
6948 {
6949 /* Get the next character from an overlay string. In overlay
6950 strings, There is no field width or padding with spaces to
6951 do. */
6952 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6953 {
6954 it->what = IT_EOB;
6955 return 0;
6956 }
6957 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6958 IT_STRING_BYTEPOS (*it),
6959 it->bidi_it.scan_dir < 0
6960 ? -1
6961 : SCHARS (it->string))
6962 && next_element_from_composition (it))
6963 {
6964 return 1;
6965 }
6966 else if (STRING_MULTIBYTE (it->string))
6967 {
6968 const unsigned char *s = (SDATA (it->string)
6969 + IT_STRING_BYTEPOS (*it));
6970 it->c = string_char_and_length (s, &it->len);
6971 }
6972 else
6973 {
6974 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6975 it->len = 1;
6976 }
6977 }
6978 else
6979 {
6980 /* Get the next character from a Lisp string that is not an
6981 overlay string. Such strings come from the mode line, for
6982 example. We may have to pad with spaces, or truncate the
6983 string. See also next_element_from_c_string. */
6984 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6985 {
6986 it->what = IT_EOB;
6987 return 0;
6988 }
6989 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6990 {
6991 /* Pad with spaces. */
6992 it->c = ' ', it->len = 1;
6993 CHARPOS (position) = BYTEPOS (position) = -1;
6994 }
6995 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6996 IT_STRING_BYTEPOS (*it),
6997 it->bidi_it.scan_dir < 0
6998 ? -1
6999 : it->string_nchars)
7000 && next_element_from_composition (it))
7001 {
7002 return 1;
7003 }
7004 else if (STRING_MULTIBYTE (it->string))
7005 {
7006 const unsigned char *s = (SDATA (it->string)
7007 + IT_STRING_BYTEPOS (*it));
7008 it->c = string_char_and_length (s, &it->len);
7009 }
7010 else
7011 {
7012 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7013 it->len = 1;
7014 }
7015 }
7016
7017 /* Record what we have and where it came from. */
7018 it->what = IT_CHARACTER;
7019 it->object = it->string;
7020 it->position = position;
7021 return 1;
7022 }
7023
7024
7025 /* Load IT with next display element from C string IT->s.
7026 IT->string_nchars is the maximum number of characters to return
7027 from the string. IT->end_charpos may be greater than
7028 IT->string_nchars when this function is called, in which case we
7029 may have to return padding spaces. Value is zero if end of string
7030 reached, including padding spaces. */
7031
7032 static int
7033 next_element_from_c_string (struct it *it)
7034 {
7035 int success_p = 1;
7036
7037 xassert (it->s);
7038 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7039 it->what = IT_CHARACTER;
7040 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7041 it->object = Qnil;
7042
7043 /* With bidi reordering, the character to display might not be the
7044 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7045 we were reseated to a new string, whose paragraph direction is
7046 not known. */
7047 if (it->bidi_p && it->bidi_it.first_elt)
7048 get_visually_first_element (it);
7049
7050 /* IT's position can be greater than IT->string_nchars in case a
7051 field width or precision has been specified when the iterator was
7052 initialized. */
7053 if (IT_CHARPOS (*it) >= it->end_charpos)
7054 {
7055 /* End of the game. */
7056 it->what = IT_EOB;
7057 success_p = 0;
7058 }
7059 else if (IT_CHARPOS (*it) >= it->string_nchars)
7060 {
7061 /* Pad with spaces. */
7062 it->c = ' ', it->len = 1;
7063 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7064 }
7065 else if (it->multibyte_p)
7066 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7067 else
7068 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7069
7070 return success_p;
7071 }
7072
7073
7074 /* Set up IT to return characters from an ellipsis, if appropriate.
7075 The definition of the ellipsis glyphs may come from a display table
7076 entry. This function fills IT with the first glyph from the
7077 ellipsis if an ellipsis is to be displayed. */
7078
7079 static int
7080 next_element_from_ellipsis (struct it *it)
7081 {
7082 if (it->selective_display_ellipsis_p)
7083 setup_for_ellipsis (it, it->len);
7084 else
7085 {
7086 /* The face at the current position may be different from the
7087 face we find after the invisible text. Remember what it
7088 was in IT->saved_face_id, and signal that it's there by
7089 setting face_before_selective_p. */
7090 it->saved_face_id = it->face_id;
7091 it->method = GET_FROM_BUFFER;
7092 it->object = it->w->buffer;
7093 reseat_at_next_visible_line_start (it, 1);
7094 it->face_before_selective_p = 1;
7095 }
7096
7097 return GET_NEXT_DISPLAY_ELEMENT (it);
7098 }
7099
7100
7101 /* Deliver an image display element. The iterator IT is already
7102 filled with image information (done in handle_display_prop). Value
7103 is always 1. */
7104
7105
7106 static int
7107 next_element_from_image (struct it *it)
7108 {
7109 it->what = IT_IMAGE;
7110 it->ignore_overlay_strings_at_pos_p = 0;
7111 return 1;
7112 }
7113
7114
7115 /* Fill iterator IT with next display element from a stretch glyph
7116 property. IT->object is the value of the text property. Value is
7117 always 1. */
7118
7119 static int
7120 next_element_from_stretch (struct it *it)
7121 {
7122 it->what = IT_STRETCH;
7123 return 1;
7124 }
7125
7126 /* Scan forward from CHARPOS in the current buffer/string, until we
7127 find a stop position > current IT's position. Then handle the stop
7128 position before that. This is called when we bump into a stop
7129 position while reordering bidirectional text. CHARPOS should be
7130 the last previously processed stop_pos (or BEGV/0, if none were
7131 processed yet) whose position is less that IT's current
7132 position. */
7133
7134 static void
7135 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7136 {
7137 int bufp = !STRINGP (it->string);
7138 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7139 struct display_pos save_current = it->current;
7140 struct text_pos save_position = it->position;
7141 struct text_pos pos1;
7142 EMACS_INT next_stop;
7143
7144 /* Scan in strict logical order. */
7145 it->bidi_p = 0;
7146 do
7147 {
7148 it->prev_stop = charpos;
7149 if (bufp)
7150 {
7151 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7152 reseat_1 (it, pos1, 0);
7153 }
7154 else
7155 it->current.string_pos = string_pos (charpos, it->string);
7156 compute_stop_pos (it);
7157 /* We must advance forward, right? */
7158 if (it->stop_charpos <= it->prev_stop)
7159 abort ();
7160 charpos = it->stop_charpos;
7161 }
7162 while (charpos <= where_we_are);
7163
7164 next_stop = it->stop_charpos;
7165 it->stop_charpos = it->prev_stop;
7166 it->bidi_p = 1;
7167 it->current = save_current;
7168 it->position = save_position;
7169 handle_stop (it);
7170 it->stop_charpos = next_stop;
7171 }
7172
7173 /* Load IT with the next display element from current_buffer. Value
7174 is zero if end of buffer reached. IT->stop_charpos is the next
7175 position at which to stop and check for text properties or buffer
7176 end. */
7177
7178 static int
7179 next_element_from_buffer (struct it *it)
7180 {
7181 int success_p = 1;
7182
7183 xassert (IT_CHARPOS (*it) >= BEGV);
7184 xassert (NILP (it->string) && !it->s);
7185 xassert (!it->bidi_p
7186 || (it->bidi_it.string.lstring == Qnil
7187 && it->bidi_it.string.s == NULL));
7188
7189 /* With bidi reordering, the character to display might not be the
7190 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7191 we were reseat()ed to a new buffer position, which is potentially
7192 a different paragraph. */
7193 if (it->bidi_p && it->bidi_it.first_elt)
7194 {
7195 get_visually_first_element (it);
7196 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7197 }
7198
7199 if (IT_CHARPOS (*it) >= it->stop_charpos)
7200 {
7201 if (IT_CHARPOS (*it) >= it->end_charpos)
7202 {
7203 int overlay_strings_follow_p;
7204
7205 /* End of the game, except when overlay strings follow that
7206 haven't been returned yet. */
7207 if (it->overlay_strings_at_end_processed_p)
7208 overlay_strings_follow_p = 0;
7209 else
7210 {
7211 it->overlay_strings_at_end_processed_p = 1;
7212 overlay_strings_follow_p = get_overlay_strings (it, 0);
7213 }
7214
7215 if (overlay_strings_follow_p)
7216 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7217 else
7218 {
7219 it->what = IT_EOB;
7220 it->position = it->current.pos;
7221 success_p = 0;
7222 }
7223 }
7224 else if (!(!it->bidi_p
7225 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7226 || IT_CHARPOS (*it) == it->stop_charpos))
7227 {
7228 /* With bidi non-linear iteration, we could find ourselves
7229 far beyond the last computed stop_charpos, with several
7230 other stop positions in between that we missed. Scan
7231 them all now, in buffer's logical order, until we find
7232 and handle the last stop_charpos that precedes our
7233 current position. */
7234 handle_stop_backwards (it, it->stop_charpos);
7235 return GET_NEXT_DISPLAY_ELEMENT (it);
7236 }
7237 else
7238 {
7239 if (it->bidi_p)
7240 {
7241 /* Take note of the stop position we just moved across,
7242 for when we will move back across it. */
7243 it->prev_stop = it->stop_charpos;
7244 /* If we are at base paragraph embedding level, take
7245 note of the last stop position seen at this
7246 level. */
7247 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7248 it->base_level_stop = it->stop_charpos;
7249 }
7250 handle_stop (it);
7251 return GET_NEXT_DISPLAY_ELEMENT (it);
7252 }
7253 }
7254 else if (it->bidi_p
7255 /* If we are before prev_stop, we may have overstepped on
7256 our way backwards a stop_pos, and if so, we need to
7257 handle that stop_pos. */
7258 && IT_CHARPOS (*it) < it->prev_stop
7259 /* We can sometimes back up for reasons that have nothing
7260 to do with bidi reordering. E.g., compositions. The
7261 code below is only needed when we are above the base
7262 embedding level, so test for that explicitly. */
7263 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7264 {
7265 /* If we lost track of base_level_stop, we have no better place
7266 for handle_stop_backwards to start from than BEGV. This
7267 happens, e.g., when we were reseated to the previous
7268 screenful of text by vertical-motion. */
7269 if (it->base_level_stop <= 0
7270 || IT_CHARPOS (*it) < it->base_level_stop)
7271 it->base_level_stop = BEGV;
7272 handle_stop_backwards (it, it->base_level_stop);
7273 return GET_NEXT_DISPLAY_ELEMENT (it);
7274 }
7275 else
7276 {
7277 /* No face changes, overlays etc. in sight, so just return a
7278 character from current_buffer. */
7279 unsigned char *p;
7280 EMACS_INT stop;
7281
7282 /* Maybe run the redisplay end trigger hook. Performance note:
7283 This doesn't seem to cost measurable time. */
7284 if (it->redisplay_end_trigger_charpos
7285 && it->glyph_row
7286 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7287 run_redisplay_end_trigger_hook (it);
7288
7289 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7290 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7291 stop)
7292 && next_element_from_composition (it))
7293 {
7294 return 1;
7295 }
7296
7297 /* Get the next character, maybe multibyte. */
7298 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7299 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7300 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7301 else
7302 it->c = *p, it->len = 1;
7303
7304 /* Record what we have and where it came from. */
7305 it->what = IT_CHARACTER;
7306 it->object = it->w->buffer;
7307 it->position = it->current.pos;
7308
7309 /* Normally we return the character found above, except when we
7310 really want to return an ellipsis for selective display. */
7311 if (it->selective)
7312 {
7313 if (it->c == '\n')
7314 {
7315 /* A value of selective > 0 means hide lines indented more
7316 than that number of columns. */
7317 if (it->selective > 0
7318 && IT_CHARPOS (*it) + 1 < ZV
7319 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7320 IT_BYTEPOS (*it) + 1,
7321 (double) it->selective)) /* iftc */
7322 {
7323 success_p = next_element_from_ellipsis (it);
7324 it->dpvec_char_len = -1;
7325 }
7326 }
7327 else if (it->c == '\r' && it->selective == -1)
7328 {
7329 /* A value of selective == -1 means that everything from the
7330 CR to the end of the line is invisible, with maybe an
7331 ellipsis displayed for it. */
7332 success_p = next_element_from_ellipsis (it);
7333 it->dpvec_char_len = -1;
7334 }
7335 }
7336 }
7337
7338 /* Value is zero if end of buffer reached. */
7339 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7340 return success_p;
7341 }
7342
7343
7344 /* Run the redisplay end trigger hook for IT. */
7345
7346 static void
7347 run_redisplay_end_trigger_hook (struct it *it)
7348 {
7349 Lisp_Object args[3];
7350
7351 /* IT->glyph_row should be non-null, i.e. we should be actually
7352 displaying something, or otherwise we should not run the hook. */
7353 xassert (it->glyph_row);
7354
7355 /* Set up hook arguments. */
7356 args[0] = Qredisplay_end_trigger_functions;
7357 args[1] = it->window;
7358 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7359 it->redisplay_end_trigger_charpos = 0;
7360
7361 /* Since we are *trying* to run these functions, don't try to run
7362 them again, even if they get an error. */
7363 it->w->redisplay_end_trigger = Qnil;
7364 Frun_hook_with_args (3, args);
7365
7366 /* Notice if it changed the face of the character we are on. */
7367 handle_face_prop (it);
7368 }
7369
7370
7371 /* Deliver a composition display element. Unlike the other
7372 next_element_from_XXX, this function is not registered in the array
7373 get_next_element[]. It is called from next_element_from_buffer and
7374 next_element_from_string when necessary. */
7375
7376 static int
7377 next_element_from_composition (struct it *it)
7378 {
7379 it->what = IT_COMPOSITION;
7380 it->len = it->cmp_it.nbytes;
7381 if (STRINGP (it->string))
7382 {
7383 if (it->c < 0)
7384 {
7385 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7386 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7387 return 0;
7388 }
7389 it->position = it->current.string_pos;
7390 it->object = it->string;
7391 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7392 IT_STRING_BYTEPOS (*it), it->string);
7393 }
7394 else
7395 {
7396 if (it->c < 0)
7397 {
7398 IT_CHARPOS (*it) += it->cmp_it.nchars;
7399 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7400 if (it->bidi_p)
7401 {
7402 if (it->bidi_it.new_paragraph)
7403 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7404 /* Resync the bidi iterator with IT's new position.
7405 FIXME: this doesn't support bidirectional text. */
7406 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7407 bidi_move_to_visually_next (&it->bidi_it);
7408 }
7409 return 0;
7410 }
7411 it->position = it->current.pos;
7412 it->object = it->w->buffer;
7413 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7414 IT_BYTEPOS (*it), Qnil);
7415 }
7416 return 1;
7417 }
7418
7419
7420 \f
7421 /***********************************************************************
7422 Moving an iterator without producing glyphs
7423 ***********************************************************************/
7424
7425 /* Check if iterator is at a position corresponding to a valid buffer
7426 position after some move_it_ call. */
7427
7428 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7429 ((it)->method == GET_FROM_STRING \
7430 ? IT_STRING_CHARPOS (*it) == 0 \
7431 : 1)
7432
7433
7434 /* Move iterator IT to a specified buffer or X position within one
7435 line on the display without producing glyphs.
7436
7437 OP should be a bit mask including some or all of these bits:
7438 MOVE_TO_X: Stop upon reaching x-position TO_X.
7439 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7440 Regardless of OP's value, stop upon reaching the end of the display line.
7441
7442 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7443 This means, in particular, that TO_X includes window's horizontal
7444 scroll amount.
7445
7446 The return value has several possible values that
7447 say what condition caused the scan to stop:
7448
7449 MOVE_POS_MATCH_OR_ZV
7450 - when TO_POS or ZV was reached.
7451
7452 MOVE_X_REACHED
7453 -when TO_X was reached before TO_POS or ZV were reached.
7454
7455 MOVE_LINE_CONTINUED
7456 - when we reached the end of the display area and the line must
7457 be continued.
7458
7459 MOVE_LINE_TRUNCATED
7460 - when we reached the end of the display area and the line is
7461 truncated.
7462
7463 MOVE_NEWLINE_OR_CR
7464 - when we stopped at a line end, i.e. a newline or a CR and selective
7465 display is on. */
7466
7467 static enum move_it_result
7468 move_it_in_display_line_to (struct it *it,
7469 EMACS_INT to_charpos, int to_x,
7470 enum move_operation_enum op)
7471 {
7472 enum move_it_result result = MOVE_UNDEFINED;
7473 struct glyph_row *saved_glyph_row;
7474 struct it wrap_it, atpos_it, atx_it;
7475 int may_wrap = 0;
7476 enum it_method prev_method = it->method;
7477 EMACS_INT prev_pos = IT_CHARPOS (*it);
7478
7479 /* Don't produce glyphs in produce_glyphs. */
7480 saved_glyph_row = it->glyph_row;
7481 it->glyph_row = NULL;
7482
7483 /* Use wrap_it to save a copy of IT wherever a word wrap could
7484 occur. Use atpos_it to save a copy of IT at the desired buffer
7485 position, if found, so that we can scan ahead and check if the
7486 word later overshoots the window edge. Use atx_it similarly, for
7487 pixel positions. */
7488 wrap_it.sp = -1;
7489 atpos_it.sp = -1;
7490 atx_it.sp = -1;
7491
7492 #define BUFFER_POS_REACHED_P() \
7493 ((op & MOVE_TO_POS) != 0 \
7494 && BUFFERP (it->object) \
7495 && (IT_CHARPOS (*it) == to_charpos \
7496 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7497 && (it->method == GET_FROM_BUFFER \
7498 || (it->method == GET_FROM_DISPLAY_VECTOR \
7499 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7500
7501 /* If there's a line-/wrap-prefix, handle it. */
7502 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7503 && it->current_y < it->last_visible_y)
7504 handle_line_prefix (it);
7505
7506 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7507 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7508
7509 while (1)
7510 {
7511 int x, i, ascent = 0, descent = 0;
7512
7513 /* Utility macro to reset an iterator with x, ascent, and descent. */
7514 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7515 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7516 (IT)->max_descent = descent)
7517
7518 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
7519 glyph). */
7520 if ((op & MOVE_TO_POS) != 0
7521 && BUFFERP (it->object)
7522 && it->method == GET_FROM_BUFFER
7523 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7524 || (it->bidi_p
7525 && (prev_method == GET_FROM_IMAGE
7526 || prev_method == GET_FROM_STRETCH)
7527 /* Passed TO_CHARPOS from left to right. */
7528 && ((prev_pos < to_charpos
7529 && IT_CHARPOS (*it) > to_charpos)
7530 /* Passed TO_CHARPOS from right to left. */
7531 || (prev_pos > to_charpos
7532 && IT_CHARPOS (*it) < to_charpos)))))
7533 {
7534 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7535 {
7536 result = MOVE_POS_MATCH_OR_ZV;
7537 break;
7538 }
7539 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7540 /* If wrap_it is valid, the current position might be in a
7541 word that is wrapped. So, save the iterator in
7542 atpos_it and continue to see if wrapping happens. */
7543 atpos_it = *it;
7544 }
7545
7546 prev_method = it->method;
7547 if (it->method == GET_FROM_BUFFER)
7548 prev_pos = IT_CHARPOS (*it);
7549 /* Stop when ZV reached.
7550 We used to stop here when TO_CHARPOS reached as well, but that is
7551 too soon if this glyph does not fit on this line. So we handle it
7552 explicitly below. */
7553 if (!get_next_display_element (it))
7554 {
7555 result = MOVE_POS_MATCH_OR_ZV;
7556 break;
7557 }
7558
7559 if (it->line_wrap == TRUNCATE)
7560 {
7561 if (BUFFER_POS_REACHED_P ())
7562 {
7563 result = MOVE_POS_MATCH_OR_ZV;
7564 break;
7565 }
7566 }
7567 else
7568 {
7569 if (it->line_wrap == WORD_WRAP)
7570 {
7571 if (IT_DISPLAYING_WHITESPACE (it))
7572 may_wrap = 1;
7573 else if (may_wrap)
7574 {
7575 /* We have reached a glyph that follows one or more
7576 whitespace characters. If the position is
7577 already found, we are done. */
7578 if (atpos_it.sp >= 0)
7579 {
7580 *it = atpos_it;
7581 result = MOVE_POS_MATCH_OR_ZV;
7582 goto done;
7583 }
7584 if (atx_it.sp >= 0)
7585 {
7586 *it = atx_it;
7587 result = MOVE_X_REACHED;
7588 goto done;
7589 }
7590 /* Otherwise, we can wrap here. */
7591 wrap_it = *it;
7592 may_wrap = 0;
7593 }
7594 }
7595 }
7596
7597 /* Remember the line height for the current line, in case
7598 the next element doesn't fit on the line. */
7599 ascent = it->max_ascent;
7600 descent = it->max_descent;
7601
7602 /* The call to produce_glyphs will get the metrics of the
7603 display element IT is loaded with. Record the x-position
7604 before this display element, in case it doesn't fit on the
7605 line. */
7606 x = it->current_x;
7607
7608 PRODUCE_GLYPHS (it);
7609
7610 if (it->area != TEXT_AREA)
7611 {
7612 set_iterator_to_next (it, 1);
7613 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7614 SET_TEXT_POS (this_line_min_pos,
7615 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7616 continue;
7617 }
7618
7619 /* The number of glyphs we get back in IT->nglyphs will normally
7620 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7621 character on a terminal frame, or (iii) a line end. For the
7622 second case, IT->nglyphs - 1 padding glyphs will be present.
7623 (On X frames, there is only one glyph produced for a
7624 composite character.)
7625
7626 The behavior implemented below means, for continuation lines,
7627 that as many spaces of a TAB as fit on the current line are
7628 displayed there. For terminal frames, as many glyphs of a
7629 multi-glyph character are displayed in the current line, too.
7630 This is what the old redisplay code did, and we keep it that
7631 way. Under X, the whole shape of a complex character must
7632 fit on the line or it will be completely displayed in the
7633 next line.
7634
7635 Note that both for tabs and padding glyphs, all glyphs have
7636 the same width. */
7637 if (it->nglyphs)
7638 {
7639 /* More than one glyph or glyph doesn't fit on line. All
7640 glyphs have the same width. */
7641 int single_glyph_width = it->pixel_width / it->nglyphs;
7642 int new_x;
7643 int x_before_this_char = x;
7644 int hpos_before_this_char = it->hpos;
7645
7646 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7647 {
7648 new_x = x + single_glyph_width;
7649
7650 /* We want to leave anything reaching TO_X to the caller. */
7651 if ((op & MOVE_TO_X) && new_x > to_x)
7652 {
7653 if (BUFFER_POS_REACHED_P ())
7654 {
7655 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7656 goto buffer_pos_reached;
7657 if (atpos_it.sp < 0)
7658 {
7659 atpos_it = *it;
7660 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7661 }
7662 }
7663 else
7664 {
7665 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7666 {
7667 it->current_x = x;
7668 result = MOVE_X_REACHED;
7669 break;
7670 }
7671 if (atx_it.sp < 0)
7672 {
7673 atx_it = *it;
7674 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7675 }
7676 }
7677 }
7678
7679 if (/* Lines are continued. */
7680 it->line_wrap != TRUNCATE
7681 && (/* And glyph doesn't fit on the line. */
7682 new_x > it->last_visible_x
7683 /* Or it fits exactly and we're on a window
7684 system frame. */
7685 || (new_x == it->last_visible_x
7686 && FRAME_WINDOW_P (it->f))))
7687 {
7688 if (/* IT->hpos == 0 means the very first glyph
7689 doesn't fit on the line, e.g. a wide image. */
7690 it->hpos == 0
7691 || (new_x == it->last_visible_x
7692 && FRAME_WINDOW_P (it->f)))
7693 {
7694 ++it->hpos;
7695 it->current_x = new_x;
7696
7697 /* The character's last glyph just barely fits
7698 in this row. */
7699 if (i == it->nglyphs - 1)
7700 {
7701 /* If this is the destination position,
7702 return a position *before* it in this row,
7703 now that we know it fits in this row. */
7704 if (BUFFER_POS_REACHED_P ())
7705 {
7706 if (it->line_wrap != WORD_WRAP
7707 || wrap_it.sp < 0)
7708 {
7709 it->hpos = hpos_before_this_char;
7710 it->current_x = x_before_this_char;
7711 result = MOVE_POS_MATCH_OR_ZV;
7712 break;
7713 }
7714 if (it->line_wrap == WORD_WRAP
7715 && atpos_it.sp < 0)
7716 {
7717 atpos_it = *it;
7718 atpos_it.current_x = x_before_this_char;
7719 atpos_it.hpos = hpos_before_this_char;
7720 }
7721 }
7722
7723 set_iterator_to_next (it, 1);
7724 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7725 SET_TEXT_POS (this_line_min_pos,
7726 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7727 /* On graphical terminals, newlines may
7728 "overflow" into the fringe if
7729 overflow-newline-into-fringe is non-nil.
7730 On text-only terminals, newlines may
7731 overflow into the last glyph on the
7732 display line.*/
7733 if (!FRAME_WINDOW_P (it->f)
7734 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7735 {
7736 if (!get_next_display_element (it))
7737 {
7738 result = MOVE_POS_MATCH_OR_ZV;
7739 break;
7740 }
7741 if (BUFFER_POS_REACHED_P ())
7742 {
7743 if (ITERATOR_AT_END_OF_LINE_P (it))
7744 result = MOVE_POS_MATCH_OR_ZV;
7745 else
7746 result = MOVE_LINE_CONTINUED;
7747 break;
7748 }
7749 if (ITERATOR_AT_END_OF_LINE_P (it))
7750 {
7751 result = MOVE_NEWLINE_OR_CR;
7752 break;
7753 }
7754 }
7755 }
7756 }
7757 else
7758 IT_RESET_X_ASCENT_DESCENT (it);
7759
7760 if (wrap_it.sp >= 0)
7761 {
7762 *it = wrap_it;
7763 atpos_it.sp = -1;
7764 atx_it.sp = -1;
7765 }
7766
7767 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7768 IT_CHARPOS (*it)));
7769 result = MOVE_LINE_CONTINUED;
7770 break;
7771 }
7772
7773 if (BUFFER_POS_REACHED_P ())
7774 {
7775 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7776 goto buffer_pos_reached;
7777 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7778 {
7779 atpos_it = *it;
7780 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7781 }
7782 }
7783
7784 if (new_x > it->first_visible_x)
7785 {
7786 /* Glyph is visible. Increment number of glyphs that
7787 would be displayed. */
7788 ++it->hpos;
7789 }
7790 }
7791
7792 if (result != MOVE_UNDEFINED)
7793 break;
7794 }
7795 else if (BUFFER_POS_REACHED_P ())
7796 {
7797 buffer_pos_reached:
7798 IT_RESET_X_ASCENT_DESCENT (it);
7799 result = MOVE_POS_MATCH_OR_ZV;
7800 break;
7801 }
7802 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7803 {
7804 /* Stop when TO_X specified and reached. This check is
7805 necessary here because of lines consisting of a line end,
7806 only. The line end will not produce any glyphs and we
7807 would never get MOVE_X_REACHED. */
7808 xassert (it->nglyphs == 0);
7809 result = MOVE_X_REACHED;
7810 break;
7811 }
7812
7813 /* Is this a line end? If yes, we're done. */
7814 if (ITERATOR_AT_END_OF_LINE_P (it))
7815 {
7816 result = MOVE_NEWLINE_OR_CR;
7817 break;
7818 }
7819
7820 if (it->method == GET_FROM_BUFFER)
7821 prev_pos = IT_CHARPOS (*it);
7822 /* The current display element has been consumed. Advance
7823 to the next. */
7824 set_iterator_to_next (it, 1);
7825 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7826 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7827
7828 /* Stop if lines are truncated and IT's current x-position is
7829 past the right edge of the window now. */
7830 if (it->line_wrap == TRUNCATE
7831 && it->current_x >= it->last_visible_x)
7832 {
7833 if (!FRAME_WINDOW_P (it->f)
7834 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7835 {
7836 if (!get_next_display_element (it)
7837 || BUFFER_POS_REACHED_P ())
7838 {
7839 result = MOVE_POS_MATCH_OR_ZV;
7840 break;
7841 }
7842 if (ITERATOR_AT_END_OF_LINE_P (it))
7843 {
7844 result = MOVE_NEWLINE_OR_CR;
7845 break;
7846 }
7847 }
7848 result = MOVE_LINE_TRUNCATED;
7849 break;
7850 }
7851 #undef IT_RESET_X_ASCENT_DESCENT
7852 }
7853
7854 #undef BUFFER_POS_REACHED_P
7855
7856 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7857 restore the saved iterator. */
7858 if (atpos_it.sp >= 0)
7859 *it = atpos_it;
7860 else if (atx_it.sp >= 0)
7861 *it = atx_it;
7862
7863 done:
7864
7865 /* Restore the iterator settings altered at the beginning of this
7866 function. */
7867 it->glyph_row = saved_glyph_row;
7868 return result;
7869 }
7870
7871 /* For external use. */
7872 void
7873 move_it_in_display_line (struct it *it,
7874 EMACS_INT to_charpos, int to_x,
7875 enum move_operation_enum op)
7876 {
7877 if (it->line_wrap == WORD_WRAP
7878 && (op & MOVE_TO_X))
7879 {
7880 struct it save_it = *it;
7881 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7882 /* When word-wrap is on, TO_X may lie past the end
7883 of a wrapped line. Then it->current is the
7884 character on the next line, so backtrack to the
7885 space before the wrap point. */
7886 if (skip == MOVE_LINE_CONTINUED)
7887 {
7888 int prev_x = max (it->current_x - 1, 0);
7889 *it = save_it;
7890 move_it_in_display_line_to
7891 (it, -1, prev_x, MOVE_TO_X);
7892 }
7893 }
7894 else
7895 move_it_in_display_line_to (it, to_charpos, to_x, op);
7896 }
7897
7898
7899 /* Move IT forward until it satisfies one or more of the criteria in
7900 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7901
7902 OP is a bit-mask that specifies where to stop, and in particular,
7903 which of those four position arguments makes a difference. See the
7904 description of enum move_operation_enum.
7905
7906 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7907 screen line, this function will set IT to the next position >
7908 TO_CHARPOS. */
7909
7910 void
7911 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
7912 {
7913 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7914 int line_height, line_start_x = 0, reached = 0;
7915
7916 for (;;)
7917 {
7918 if (op & MOVE_TO_VPOS)
7919 {
7920 /* If no TO_CHARPOS and no TO_X specified, stop at the
7921 start of the line TO_VPOS. */
7922 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7923 {
7924 if (it->vpos == to_vpos)
7925 {
7926 reached = 1;
7927 break;
7928 }
7929 else
7930 skip = move_it_in_display_line_to (it, -1, -1, 0);
7931 }
7932 else
7933 {
7934 /* TO_VPOS >= 0 means stop at TO_X in the line at
7935 TO_VPOS, or at TO_POS, whichever comes first. */
7936 if (it->vpos == to_vpos)
7937 {
7938 reached = 2;
7939 break;
7940 }
7941
7942 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7943
7944 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7945 {
7946 reached = 3;
7947 break;
7948 }
7949 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7950 {
7951 /* We have reached TO_X but not in the line we want. */
7952 skip = move_it_in_display_line_to (it, to_charpos,
7953 -1, MOVE_TO_POS);
7954 if (skip == MOVE_POS_MATCH_OR_ZV)
7955 {
7956 reached = 4;
7957 break;
7958 }
7959 }
7960 }
7961 }
7962 else if (op & MOVE_TO_Y)
7963 {
7964 struct it it_backup;
7965
7966 if (it->line_wrap == WORD_WRAP)
7967 it_backup = *it;
7968
7969 /* TO_Y specified means stop at TO_X in the line containing
7970 TO_Y---or at TO_CHARPOS if this is reached first. The
7971 problem is that we can't really tell whether the line
7972 contains TO_Y before we have completely scanned it, and
7973 this may skip past TO_X. What we do is to first scan to
7974 TO_X.
7975
7976 If TO_X is not specified, use a TO_X of zero. The reason
7977 is to make the outcome of this function more predictable.
7978 If we didn't use TO_X == 0, we would stop at the end of
7979 the line which is probably not what a caller would expect
7980 to happen. */
7981 skip = move_it_in_display_line_to
7982 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7983 (MOVE_TO_X | (op & MOVE_TO_POS)));
7984
7985 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7986 if (skip == MOVE_POS_MATCH_OR_ZV)
7987 reached = 5;
7988 else if (skip == MOVE_X_REACHED)
7989 {
7990 /* If TO_X was reached, we want to know whether TO_Y is
7991 in the line. We know this is the case if the already
7992 scanned glyphs make the line tall enough. Otherwise,
7993 we must check by scanning the rest of the line. */
7994 line_height = it->max_ascent + it->max_descent;
7995 if (to_y >= it->current_y
7996 && to_y < it->current_y + line_height)
7997 {
7998 reached = 6;
7999 break;
8000 }
8001 it_backup = *it;
8002 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8003 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8004 op & MOVE_TO_POS);
8005 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8006 line_height = it->max_ascent + it->max_descent;
8007 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8008
8009 if (to_y >= it->current_y
8010 && to_y < it->current_y + line_height)
8011 {
8012 /* If TO_Y is in this line and TO_X was reached
8013 above, we scanned too far. We have to restore
8014 IT's settings to the ones before skipping. */
8015 *it = it_backup;
8016 reached = 6;
8017 }
8018 else
8019 {
8020 skip = skip2;
8021 if (skip == MOVE_POS_MATCH_OR_ZV)
8022 reached = 7;
8023 }
8024 }
8025 else
8026 {
8027 /* Check whether TO_Y is in this line. */
8028 line_height = it->max_ascent + it->max_descent;
8029 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8030
8031 if (to_y >= it->current_y
8032 && to_y < it->current_y + line_height)
8033 {
8034 /* When word-wrap is on, TO_X may lie past the end
8035 of a wrapped line. Then it->current is the
8036 character on the next line, so backtrack to the
8037 space before the wrap point. */
8038 if (skip == MOVE_LINE_CONTINUED
8039 && it->line_wrap == WORD_WRAP)
8040 {
8041 int prev_x = max (it->current_x - 1, 0);
8042 *it = it_backup;
8043 skip = move_it_in_display_line_to
8044 (it, -1, prev_x, MOVE_TO_X);
8045 }
8046 reached = 6;
8047 }
8048 }
8049
8050 if (reached)
8051 break;
8052 }
8053 else if (BUFFERP (it->object)
8054 && (it->method == GET_FROM_BUFFER
8055 || it->method == GET_FROM_STRETCH)
8056 && IT_CHARPOS (*it) >= to_charpos)
8057 skip = MOVE_POS_MATCH_OR_ZV;
8058 else
8059 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8060
8061 switch (skip)
8062 {
8063 case MOVE_POS_MATCH_OR_ZV:
8064 reached = 8;
8065 goto out;
8066
8067 case MOVE_NEWLINE_OR_CR:
8068 set_iterator_to_next (it, 1);
8069 it->continuation_lines_width = 0;
8070 break;
8071
8072 case MOVE_LINE_TRUNCATED:
8073 it->continuation_lines_width = 0;
8074 reseat_at_next_visible_line_start (it, 0);
8075 if ((op & MOVE_TO_POS) != 0
8076 && IT_CHARPOS (*it) > to_charpos)
8077 {
8078 reached = 9;
8079 goto out;
8080 }
8081 break;
8082
8083 case MOVE_LINE_CONTINUED:
8084 /* For continued lines ending in a tab, some of the glyphs
8085 associated with the tab are displayed on the current
8086 line. Since it->current_x does not include these glyphs,
8087 we use it->last_visible_x instead. */
8088 if (it->c == '\t')
8089 {
8090 it->continuation_lines_width += it->last_visible_x;
8091 /* When moving by vpos, ensure that the iterator really
8092 advances to the next line (bug#847, bug#969). Fixme:
8093 do we need to do this in other circumstances? */
8094 if (it->current_x != it->last_visible_x
8095 && (op & MOVE_TO_VPOS)
8096 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8097 {
8098 line_start_x = it->current_x + it->pixel_width
8099 - it->last_visible_x;
8100 set_iterator_to_next (it, 0);
8101 }
8102 }
8103 else
8104 it->continuation_lines_width += it->current_x;
8105 break;
8106
8107 default:
8108 abort ();
8109 }
8110
8111 /* Reset/increment for the next run. */
8112 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8113 it->current_x = line_start_x;
8114 line_start_x = 0;
8115 it->hpos = 0;
8116 it->current_y += it->max_ascent + it->max_descent;
8117 ++it->vpos;
8118 last_height = it->max_ascent + it->max_descent;
8119 last_max_ascent = it->max_ascent;
8120 it->max_ascent = it->max_descent = 0;
8121 }
8122
8123 out:
8124
8125 /* On text terminals, we may stop at the end of a line in the middle
8126 of a multi-character glyph. If the glyph itself is continued,
8127 i.e. it is actually displayed on the next line, don't treat this
8128 stopping point as valid; move to the next line instead (unless
8129 that brings us offscreen). */
8130 if (!FRAME_WINDOW_P (it->f)
8131 && op & MOVE_TO_POS
8132 && IT_CHARPOS (*it) == to_charpos
8133 && it->what == IT_CHARACTER
8134 && it->nglyphs > 1
8135 && it->line_wrap == WINDOW_WRAP
8136 && it->current_x == it->last_visible_x - 1
8137 && it->c != '\n'
8138 && it->c != '\t'
8139 && it->vpos < XFASTINT (it->w->window_end_vpos))
8140 {
8141 it->continuation_lines_width += it->current_x;
8142 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8143 it->current_y += it->max_ascent + it->max_descent;
8144 ++it->vpos;
8145 last_height = it->max_ascent + it->max_descent;
8146 last_max_ascent = it->max_ascent;
8147 }
8148
8149 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8150 }
8151
8152
8153 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8154
8155 If DY > 0, move IT backward at least that many pixels. DY = 0
8156 means move IT backward to the preceding line start or BEGV. This
8157 function may move over more than DY pixels if IT->current_y - DY
8158 ends up in the middle of a line; in this case IT->current_y will be
8159 set to the top of the line moved to. */
8160
8161 void
8162 move_it_vertically_backward (struct it *it, int dy)
8163 {
8164 int nlines, h;
8165 struct it it2, it3;
8166 EMACS_INT start_pos;
8167
8168 move_further_back:
8169 xassert (dy >= 0);
8170
8171 start_pos = IT_CHARPOS (*it);
8172
8173 /* Estimate how many newlines we must move back. */
8174 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8175
8176 /* Set the iterator's position that many lines back. */
8177 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8178 back_to_previous_visible_line_start (it);
8179
8180 /* Reseat the iterator here. When moving backward, we don't want
8181 reseat to skip forward over invisible text, set up the iterator
8182 to deliver from overlay strings at the new position etc. So,
8183 use reseat_1 here. */
8184 reseat_1 (it, it->current.pos, 1);
8185
8186 /* We are now surely at a line start. */
8187 it->current_x = it->hpos = 0;
8188 it->continuation_lines_width = 0;
8189
8190 /* Move forward and see what y-distance we moved. First move to the
8191 start of the next line so that we get its height. We need this
8192 height to be able to tell whether we reached the specified
8193 y-distance. */
8194 it2 = *it;
8195 it2.max_ascent = it2.max_descent = 0;
8196 do
8197 {
8198 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8199 MOVE_TO_POS | MOVE_TO_VPOS);
8200 }
8201 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8202 xassert (IT_CHARPOS (*it) >= BEGV);
8203 it3 = it2;
8204
8205 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8206 xassert (IT_CHARPOS (*it) >= BEGV);
8207 /* H is the actual vertical distance from the position in *IT
8208 and the starting position. */
8209 h = it2.current_y - it->current_y;
8210 /* NLINES is the distance in number of lines. */
8211 nlines = it2.vpos - it->vpos;
8212
8213 /* Correct IT's y and vpos position
8214 so that they are relative to the starting point. */
8215 it->vpos -= nlines;
8216 it->current_y -= h;
8217
8218 if (dy == 0)
8219 {
8220 /* DY == 0 means move to the start of the screen line. The
8221 value of nlines is > 0 if continuation lines were involved. */
8222 if (nlines > 0)
8223 move_it_by_lines (it, nlines);
8224 }
8225 else
8226 {
8227 /* The y-position we try to reach, relative to *IT.
8228 Note that H has been subtracted in front of the if-statement. */
8229 int target_y = it->current_y + h - dy;
8230 int y0 = it3.current_y;
8231 int y1 = line_bottom_y (&it3);
8232 int line_height = y1 - y0;
8233
8234 /* If we did not reach target_y, try to move further backward if
8235 we can. If we moved too far backward, try to move forward. */
8236 if (target_y < it->current_y
8237 /* This is heuristic. In a window that's 3 lines high, with
8238 a line height of 13 pixels each, recentering with point
8239 on the bottom line will try to move -39/2 = 19 pixels
8240 backward. Try to avoid moving into the first line. */
8241 && (it->current_y - target_y
8242 > min (window_box_height (it->w), line_height * 2 / 3))
8243 && IT_CHARPOS (*it) > BEGV)
8244 {
8245 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8246 target_y - it->current_y));
8247 dy = it->current_y - target_y;
8248 goto move_further_back;
8249 }
8250 else if (target_y >= it->current_y + line_height
8251 && IT_CHARPOS (*it) < ZV)
8252 {
8253 /* Should move forward by at least one line, maybe more.
8254
8255 Note: Calling move_it_by_lines can be expensive on
8256 terminal frames, where compute_motion is used (via
8257 vmotion) to do the job, when there are very long lines
8258 and truncate-lines is nil. That's the reason for
8259 treating terminal frames specially here. */
8260
8261 if (!FRAME_WINDOW_P (it->f))
8262 move_it_vertically (it, target_y - (it->current_y + line_height));
8263 else
8264 {
8265 do
8266 {
8267 move_it_by_lines (it, 1);
8268 }
8269 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8270 }
8271 }
8272 }
8273 }
8274
8275
8276 /* Move IT by a specified amount of pixel lines DY. DY negative means
8277 move backwards. DY = 0 means move to start of screen line. At the
8278 end, IT will be on the start of a screen line. */
8279
8280 void
8281 move_it_vertically (struct it *it, int dy)
8282 {
8283 if (dy <= 0)
8284 move_it_vertically_backward (it, -dy);
8285 else
8286 {
8287 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8288 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8289 MOVE_TO_POS | MOVE_TO_Y);
8290 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8291
8292 /* If buffer ends in ZV without a newline, move to the start of
8293 the line to satisfy the post-condition. */
8294 if (IT_CHARPOS (*it) == ZV
8295 && ZV > BEGV
8296 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8297 move_it_by_lines (it, 0);
8298 }
8299 }
8300
8301
8302 /* Move iterator IT past the end of the text line it is in. */
8303
8304 void
8305 move_it_past_eol (struct it *it)
8306 {
8307 enum move_it_result rc;
8308
8309 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8310 if (rc == MOVE_NEWLINE_OR_CR)
8311 set_iterator_to_next (it, 0);
8312 }
8313
8314
8315 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8316 negative means move up. DVPOS == 0 means move to the start of the
8317 screen line.
8318
8319 Optimization idea: If we would know that IT->f doesn't use
8320 a face with proportional font, we could be faster for
8321 truncate-lines nil. */
8322
8323 void
8324 move_it_by_lines (struct it *it, int dvpos)
8325 {
8326
8327 /* The commented-out optimization uses vmotion on terminals. This
8328 gives bad results, because elements like it->what, on which
8329 callers such as pos_visible_p rely, aren't updated. */
8330 /* struct position pos;
8331 if (!FRAME_WINDOW_P (it->f))
8332 {
8333 struct text_pos textpos;
8334
8335 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8336 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8337 reseat (it, textpos, 1);
8338 it->vpos += pos.vpos;
8339 it->current_y += pos.vpos;
8340 }
8341 else */
8342
8343 if (dvpos == 0)
8344 {
8345 /* DVPOS == 0 means move to the start of the screen line. */
8346 move_it_vertically_backward (it, 0);
8347 xassert (it->current_x == 0 && it->hpos == 0);
8348 /* Let next call to line_bottom_y calculate real line height */
8349 last_height = 0;
8350 }
8351 else if (dvpos > 0)
8352 {
8353 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8354 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8355 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8356 }
8357 else
8358 {
8359 struct it it2;
8360 EMACS_INT start_charpos, i;
8361
8362 /* Start at the beginning of the screen line containing IT's
8363 position. This may actually move vertically backwards,
8364 in case of overlays, so adjust dvpos accordingly. */
8365 dvpos += it->vpos;
8366 move_it_vertically_backward (it, 0);
8367 dvpos -= it->vpos;
8368
8369 /* Go back -DVPOS visible lines and reseat the iterator there. */
8370 start_charpos = IT_CHARPOS (*it);
8371 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8372 back_to_previous_visible_line_start (it);
8373 reseat (it, it->current.pos, 1);
8374
8375 /* Move further back if we end up in a string or an image. */
8376 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8377 {
8378 /* First try to move to start of display line. */
8379 dvpos += it->vpos;
8380 move_it_vertically_backward (it, 0);
8381 dvpos -= it->vpos;
8382 if (IT_POS_VALID_AFTER_MOVE_P (it))
8383 break;
8384 /* If start of line is still in string or image,
8385 move further back. */
8386 back_to_previous_visible_line_start (it);
8387 reseat (it, it->current.pos, 1);
8388 dvpos--;
8389 }
8390
8391 it->current_x = it->hpos = 0;
8392
8393 /* Above call may have moved too far if continuation lines
8394 are involved. Scan forward and see if it did. */
8395 it2 = *it;
8396 it2.vpos = it2.current_y = 0;
8397 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8398 it->vpos -= it2.vpos;
8399 it->current_y -= it2.current_y;
8400 it->current_x = it->hpos = 0;
8401
8402 /* If we moved too far back, move IT some lines forward. */
8403 if (it2.vpos > -dvpos)
8404 {
8405 int delta = it2.vpos + dvpos;
8406 it2 = *it;
8407 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8408 /* Move back again if we got too far ahead. */
8409 if (IT_CHARPOS (*it) >= start_charpos)
8410 *it = it2;
8411 }
8412 }
8413 }
8414
8415 /* Return 1 if IT points into the middle of a display vector. */
8416
8417 int
8418 in_display_vector_p (struct it *it)
8419 {
8420 return (it->method == GET_FROM_DISPLAY_VECTOR
8421 && it->current.dpvec_index > 0
8422 && it->dpvec + it->current.dpvec_index != it->dpend);
8423 }
8424
8425 \f
8426 /***********************************************************************
8427 Messages
8428 ***********************************************************************/
8429
8430
8431 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8432 to *Messages*. */
8433
8434 void
8435 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8436 {
8437 Lisp_Object args[3];
8438 Lisp_Object msg, fmt;
8439 char *buffer;
8440 EMACS_INT len;
8441 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8442 USE_SAFE_ALLOCA;
8443
8444 /* Do nothing if called asynchronously. Inserting text into
8445 a buffer may call after-change-functions and alike and
8446 that would means running Lisp asynchronously. */
8447 if (handling_signal)
8448 return;
8449
8450 fmt = msg = Qnil;
8451 GCPRO4 (fmt, msg, arg1, arg2);
8452
8453 args[0] = fmt = build_string (format);
8454 args[1] = arg1;
8455 args[2] = arg2;
8456 msg = Fformat (3, args);
8457
8458 len = SBYTES (msg) + 1;
8459 SAFE_ALLOCA (buffer, char *, len);
8460 memcpy (buffer, SDATA (msg), len);
8461
8462 message_dolog (buffer, len - 1, 1, 0);
8463 SAFE_FREE ();
8464
8465 UNGCPRO;
8466 }
8467
8468
8469 /* Output a newline in the *Messages* buffer if "needs" one. */
8470
8471 void
8472 message_log_maybe_newline (void)
8473 {
8474 if (message_log_need_newline)
8475 message_dolog ("", 0, 1, 0);
8476 }
8477
8478
8479 /* Add a string M of length NBYTES to the message log, optionally
8480 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8481 nonzero, means interpret the contents of M as multibyte. This
8482 function calls low-level routines in order to bypass text property
8483 hooks, etc. which might not be safe to run.
8484
8485 This may GC (insert may run before/after change hooks),
8486 so the buffer M must NOT point to a Lisp string. */
8487
8488 void
8489 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8490 {
8491 const unsigned char *msg = (const unsigned char *) m;
8492
8493 if (!NILP (Vmemory_full))
8494 return;
8495
8496 if (!NILP (Vmessage_log_max))
8497 {
8498 struct buffer *oldbuf;
8499 Lisp_Object oldpoint, oldbegv, oldzv;
8500 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8501 EMACS_INT point_at_end = 0;
8502 EMACS_INT zv_at_end = 0;
8503 Lisp_Object old_deactivate_mark, tem;
8504 struct gcpro gcpro1;
8505
8506 old_deactivate_mark = Vdeactivate_mark;
8507 oldbuf = current_buffer;
8508 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8509 BVAR (current_buffer, undo_list) = Qt;
8510
8511 oldpoint = message_dolog_marker1;
8512 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8513 oldbegv = message_dolog_marker2;
8514 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8515 oldzv = message_dolog_marker3;
8516 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8517 GCPRO1 (old_deactivate_mark);
8518
8519 if (PT == Z)
8520 point_at_end = 1;
8521 if (ZV == Z)
8522 zv_at_end = 1;
8523
8524 BEGV = BEG;
8525 BEGV_BYTE = BEG_BYTE;
8526 ZV = Z;
8527 ZV_BYTE = Z_BYTE;
8528 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8529
8530 /* Insert the string--maybe converting multibyte to single byte
8531 or vice versa, so that all the text fits the buffer. */
8532 if (multibyte
8533 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8534 {
8535 EMACS_INT i;
8536 int c, char_bytes;
8537 char work[1];
8538
8539 /* Convert a multibyte string to single-byte
8540 for the *Message* buffer. */
8541 for (i = 0; i < nbytes; i += char_bytes)
8542 {
8543 c = string_char_and_length (msg + i, &char_bytes);
8544 work[0] = (ASCII_CHAR_P (c)
8545 ? c
8546 : multibyte_char_to_unibyte (c));
8547 insert_1_both (work, 1, 1, 1, 0, 0);
8548 }
8549 }
8550 else if (! multibyte
8551 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8552 {
8553 EMACS_INT i;
8554 int c, char_bytes;
8555 unsigned char str[MAX_MULTIBYTE_LENGTH];
8556 /* Convert a single-byte string to multibyte
8557 for the *Message* buffer. */
8558 for (i = 0; i < nbytes; i++)
8559 {
8560 c = msg[i];
8561 MAKE_CHAR_MULTIBYTE (c);
8562 char_bytes = CHAR_STRING (c, str);
8563 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8564 }
8565 }
8566 else if (nbytes)
8567 insert_1 (m, nbytes, 1, 0, 0);
8568
8569 if (nlflag)
8570 {
8571 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8572 unsigned long int dups;
8573 insert_1 ("\n", 1, 1, 0, 0);
8574
8575 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8576 this_bol = PT;
8577 this_bol_byte = PT_BYTE;
8578
8579 /* See if this line duplicates the previous one.
8580 If so, combine duplicates. */
8581 if (this_bol > BEG)
8582 {
8583 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8584 prev_bol = PT;
8585 prev_bol_byte = PT_BYTE;
8586
8587 dups = message_log_check_duplicate (prev_bol_byte,
8588 this_bol_byte);
8589 if (dups)
8590 {
8591 del_range_both (prev_bol, prev_bol_byte,
8592 this_bol, this_bol_byte, 0);
8593 if (dups > 1)
8594 {
8595 char dupstr[40];
8596 int duplen;
8597
8598 /* If you change this format, don't forget to also
8599 change message_log_check_duplicate. */
8600 sprintf (dupstr, " [%lu times]", dups);
8601 duplen = strlen (dupstr);
8602 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8603 insert_1 (dupstr, duplen, 1, 0, 1);
8604 }
8605 }
8606 }
8607
8608 /* If we have more than the desired maximum number of lines
8609 in the *Messages* buffer now, delete the oldest ones.
8610 This is safe because we don't have undo in this buffer. */
8611
8612 if (NATNUMP (Vmessage_log_max))
8613 {
8614 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8615 -XFASTINT (Vmessage_log_max) - 1, 0);
8616 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8617 }
8618 }
8619 BEGV = XMARKER (oldbegv)->charpos;
8620 BEGV_BYTE = marker_byte_position (oldbegv);
8621
8622 if (zv_at_end)
8623 {
8624 ZV = Z;
8625 ZV_BYTE = Z_BYTE;
8626 }
8627 else
8628 {
8629 ZV = XMARKER (oldzv)->charpos;
8630 ZV_BYTE = marker_byte_position (oldzv);
8631 }
8632
8633 if (point_at_end)
8634 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8635 else
8636 /* We can't do Fgoto_char (oldpoint) because it will run some
8637 Lisp code. */
8638 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8639 XMARKER (oldpoint)->bytepos);
8640
8641 UNGCPRO;
8642 unchain_marker (XMARKER (oldpoint));
8643 unchain_marker (XMARKER (oldbegv));
8644 unchain_marker (XMARKER (oldzv));
8645
8646 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8647 set_buffer_internal (oldbuf);
8648 if (NILP (tem))
8649 windows_or_buffers_changed = old_windows_or_buffers_changed;
8650 message_log_need_newline = !nlflag;
8651 Vdeactivate_mark = old_deactivate_mark;
8652 }
8653 }
8654
8655
8656 /* We are at the end of the buffer after just having inserted a newline.
8657 (Note: We depend on the fact we won't be crossing the gap.)
8658 Check to see if the most recent message looks a lot like the previous one.
8659 Return 0 if different, 1 if the new one should just replace it, or a
8660 value N > 1 if we should also append " [N times]". */
8661
8662 static unsigned long int
8663 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8664 {
8665 EMACS_INT i;
8666 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8667 int seen_dots = 0;
8668 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8669 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8670
8671 for (i = 0; i < len; i++)
8672 {
8673 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8674 seen_dots = 1;
8675 if (p1[i] != p2[i])
8676 return seen_dots;
8677 }
8678 p1 += len;
8679 if (*p1 == '\n')
8680 return 2;
8681 if (*p1++ == ' ' && *p1++ == '[')
8682 {
8683 char *pend;
8684 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8685 if (strncmp (pend, " times]\n", 8) == 0)
8686 return n+1;
8687 }
8688 return 0;
8689 }
8690 \f
8691
8692 /* Display an echo area message M with a specified length of NBYTES
8693 bytes. The string may include null characters. If M is 0, clear
8694 out any existing message, and let the mini-buffer text show
8695 through.
8696
8697 This may GC, so the buffer M must NOT point to a Lisp string. */
8698
8699 void
8700 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8701 {
8702 /* First flush out any partial line written with print. */
8703 message_log_maybe_newline ();
8704 if (m)
8705 message_dolog (m, nbytes, 1, multibyte);
8706 message2_nolog (m, nbytes, multibyte);
8707 }
8708
8709
8710 /* The non-logging counterpart of message2. */
8711
8712 void
8713 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8714 {
8715 struct frame *sf = SELECTED_FRAME ();
8716 message_enable_multibyte = multibyte;
8717
8718 if (FRAME_INITIAL_P (sf))
8719 {
8720 if (noninteractive_need_newline)
8721 putc ('\n', stderr);
8722 noninteractive_need_newline = 0;
8723 if (m)
8724 fwrite (m, nbytes, 1, stderr);
8725 if (cursor_in_echo_area == 0)
8726 fprintf (stderr, "\n");
8727 fflush (stderr);
8728 }
8729 /* A null message buffer means that the frame hasn't really been
8730 initialized yet. Error messages get reported properly by
8731 cmd_error, so this must be just an informative message; toss it. */
8732 else if (INTERACTIVE
8733 && sf->glyphs_initialized_p
8734 && FRAME_MESSAGE_BUF (sf))
8735 {
8736 Lisp_Object mini_window;
8737 struct frame *f;
8738
8739 /* Get the frame containing the mini-buffer
8740 that the selected frame is using. */
8741 mini_window = FRAME_MINIBUF_WINDOW (sf);
8742 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8743
8744 FRAME_SAMPLE_VISIBILITY (f);
8745 if (FRAME_VISIBLE_P (sf)
8746 && ! FRAME_VISIBLE_P (f))
8747 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8748
8749 if (m)
8750 {
8751 set_message (m, Qnil, nbytes, multibyte);
8752 if (minibuffer_auto_raise)
8753 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8754 }
8755 else
8756 clear_message (1, 1);
8757
8758 do_pending_window_change (0);
8759 echo_area_display (1);
8760 do_pending_window_change (0);
8761 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8762 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8763 }
8764 }
8765
8766
8767 /* Display an echo area message M with a specified length of NBYTES
8768 bytes. The string may include null characters. If M is not a
8769 string, clear out any existing message, and let the mini-buffer
8770 text show through.
8771
8772 This function cancels echoing. */
8773
8774 void
8775 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8776 {
8777 struct gcpro gcpro1;
8778
8779 GCPRO1 (m);
8780 clear_message (1,1);
8781 cancel_echoing ();
8782
8783 /* First flush out any partial line written with print. */
8784 message_log_maybe_newline ();
8785 if (STRINGP (m))
8786 {
8787 char *buffer;
8788 USE_SAFE_ALLOCA;
8789
8790 SAFE_ALLOCA (buffer, char *, nbytes);
8791 memcpy (buffer, SDATA (m), nbytes);
8792 message_dolog (buffer, nbytes, 1, multibyte);
8793 SAFE_FREE ();
8794 }
8795 message3_nolog (m, nbytes, multibyte);
8796
8797 UNGCPRO;
8798 }
8799
8800
8801 /* The non-logging version of message3.
8802 This does not cancel echoing, because it is used for echoing.
8803 Perhaps we need to make a separate function for echoing
8804 and make this cancel echoing. */
8805
8806 void
8807 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8808 {
8809 struct frame *sf = SELECTED_FRAME ();
8810 message_enable_multibyte = multibyte;
8811
8812 if (FRAME_INITIAL_P (sf))
8813 {
8814 if (noninteractive_need_newline)
8815 putc ('\n', stderr);
8816 noninteractive_need_newline = 0;
8817 if (STRINGP (m))
8818 fwrite (SDATA (m), nbytes, 1, stderr);
8819 if (cursor_in_echo_area == 0)
8820 fprintf (stderr, "\n");
8821 fflush (stderr);
8822 }
8823 /* A null message buffer means that the frame hasn't really been
8824 initialized yet. Error messages get reported properly by
8825 cmd_error, so this must be just an informative message; toss it. */
8826 else if (INTERACTIVE
8827 && sf->glyphs_initialized_p
8828 && FRAME_MESSAGE_BUF (sf))
8829 {
8830 Lisp_Object mini_window;
8831 Lisp_Object frame;
8832 struct frame *f;
8833
8834 /* Get the frame containing the mini-buffer
8835 that the selected frame is using. */
8836 mini_window = FRAME_MINIBUF_WINDOW (sf);
8837 frame = XWINDOW (mini_window)->frame;
8838 f = XFRAME (frame);
8839
8840 FRAME_SAMPLE_VISIBILITY (f);
8841 if (FRAME_VISIBLE_P (sf)
8842 && !FRAME_VISIBLE_P (f))
8843 Fmake_frame_visible (frame);
8844
8845 if (STRINGP (m) && SCHARS (m) > 0)
8846 {
8847 set_message (NULL, m, nbytes, multibyte);
8848 if (minibuffer_auto_raise)
8849 Fraise_frame (frame);
8850 /* Assume we are not echoing.
8851 (If we are, echo_now will override this.) */
8852 echo_message_buffer = Qnil;
8853 }
8854 else
8855 clear_message (1, 1);
8856
8857 do_pending_window_change (0);
8858 echo_area_display (1);
8859 do_pending_window_change (0);
8860 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8861 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8862 }
8863 }
8864
8865
8866 /* Display a null-terminated echo area message M. If M is 0, clear
8867 out any existing message, and let the mini-buffer text show through.
8868
8869 The buffer M must continue to exist until after the echo area gets
8870 cleared or some other message gets displayed there. Do not pass
8871 text that is stored in a Lisp string. Do not pass text in a buffer
8872 that was alloca'd. */
8873
8874 void
8875 message1 (const char *m)
8876 {
8877 message2 (m, (m ? strlen (m) : 0), 0);
8878 }
8879
8880
8881 /* The non-logging counterpart of message1. */
8882
8883 void
8884 message1_nolog (const char *m)
8885 {
8886 message2_nolog (m, (m ? strlen (m) : 0), 0);
8887 }
8888
8889 /* Display a message M which contains a single %s
8890 which gets replaced with STRING. */
8891
8892 void
8893 message_with_string (const char *m, Lisp_Object string, int log)
8894 {
8895 CHECK_STRING (string);
8896
8897 if (noninteractive)
8898 {
8899 if (m)
8900 {
8901 if (noninteractive_need_newline)
8902 putc ('\n', stderr);
8903 noninteractive_need_newline = 0;
8904 fprintf (stderr, m, SDATA (string));
8905 if (!cursor_in_echo_area)
8906 fprintf (stderr, "\n");
8907 fflush (stderr);
8908 }
8909 }
8910 else if (INTERACTIVE)
8911 {
8912 /* The frame whose minibuffer we're going to display the message on.
8913 It may be larger than the selected frame, so we need
8914 to use its buffer, not the selected frame's buffer. */
8915 Lisp_Object mini_window;
8916 struct frame *f, *sf = SELECTED_FRAME ();
8917
8918 /* Get the frame containing the minibuffer
8919 that the selected frame is using. */
8920 mini_window = FRAME_MINIBUF_WINDOW (sf);
8921 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8922
8923 /* A null message buffer means that the frame hasn't really been
8924 initialized yet. Error messages get reported properly by
8925 cmd_error, so this must be just an informative message; toss it. */
8926 if (FRAME_MESSAGE_BUF (f))
8927 {
8928 Lisp_Object args[2], msg;
8929 struct gcpro gcpro1, gcpro2;
8930
8931 args[0] = build_string (m);
8932 args[1] = msg = string;
8933 GCPRO2 (args[0], msg);
8934 gcpro1.nvars = 2;
8935
8936 msg = Fformat (2, args);
8937
8938 if (log)
8939 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8940 else
8941 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8942
8943 UNGCPRO;
8944
8945 /* Print should start at the beginning of the message
8946 buffer next time. */
8947 message_buf_print = 0;
8948 }
8949 }
8950 }
8951
8952
8953 /* Dump an informative message to the minibuf. If M is 0, clear out
8954 any existing message, and let the mini-buffer text show through. */
8955
8956 static void
8957 vmessage (const char *m, va_list ap)
8958 {
8959 if (noninteractive)
8960 {
8961 if (m)
8962 {
8963 if (noninteractive_need_newline)
8964 putc ('\n', stderr);
8965 noninteractive_need_newline = 0;
8966 vfprintf (stderr, m, ap);
8967 if (cursor_in_echo_area == 0)
8968 fprintf (stderr, "\n");
8969 fflush (stderr);
8970 }
8971 }
8972 else if (INTERACTIVE)
8973 {
8974 /* The frame whose mini-buffer we're going to display the message
8975 on. It may be larger than the selected frame, so we need to
8976 use its buffer, not the selected frame's buffer. */
8977 Lisp_Object mini_window;
8978 struct frame *f, *sf = SELECTED_FRAME ();
8979
8980 /* Get the frame containing the mini-buffer
8981 that the selected frame is using. */
8982 mini_window = FRAME_MINIBUF_WINDOW (sf);
8983 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8984
8985 /* A null message buffer means that the frame hasn't really been
8986 initialized yet. Error messages get reported properly by
8987 cmd_error, so this must be just an informative message; toss
8988 it. */
8989 if (FRAME_MESSAGE_BUF (f))
8990 {
8991 if (m)
8992 {
8993 size_t len;
8994
8995 len = doprnt (FRAME_MESSAGE_BUF (f),
8996 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
8997
8998 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8999 }
9000 else
9001 message1 (0);
9002
9003 /* Print should start at the beginning of the message
9004 buffer next time. */
9005 message_buf_print = 0;
9006 }
9007 }
9008 }
9009
9010 void
9011 message (const char *m, ...)
9012 {
9013 va_list ap;
9014 va_start (ap, m);
9015 vmessage (m, ap);
9016 va_end (ap);
9017 }
9018
9019
9020 #if 0
9021 /* The non-logging version of message. */
9022
9023 void
9024 message_nolog (const char *m, ...)
9025 {
9026 Lisp_Object old_log_max;
9027 va_list ap;
9028 va_start (ap, m);
9029 old_log_max = Vmessage_log_max;
9030 Vmessage_log_max = Qnil;
9031 vmessage (m, ap);
9032 Vmessage_log_max = old_log_max;
9033 va_end (ap);
9034 }
9035 #endif
9036
9037
9038 /* Display the current message in the current mini-buffer. This is
9039 only called from error handlers in process.c, and is not time
9040 critical. */
9041
9042 void
9043 update_echo_area (void)
9044 {
9045 if (!NILP (echo_area_buffer[0]))
9046 {
9047 Lisp_Object string;
9048 string = Fcurrent_message ();
9049 message3 (string, SBYTES (string),
9050 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9051 }
9052 }
9053
9054
9055 /* Make sure echo area buffers in `echo_buffers' are live.
9056 If they aren't, make new ones. */
9057
9058 static void
9059 ensure_echo_area_buffers (void)
9060 {
9061 int i;
9062
9063 for (i = 0; i < 2; ++i)
9064 if (!BUFFERP (echo_buffer[i])
9065 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9066 {
9067 char name[30];
9068 Lisp_Object old_buffer;
9069 int j;
9070
9071 old_buffer = echo_buffer[i];
9072 sprintf (name, " *Echo Area %d*", i);
9073 echo_buffer[i] = Fget_buffer_create (build_string (name));
9074 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9075 /* to force word wrap in echo area -
9076 it was decided to postpone this*/
9077 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9078
9079 for (j = 0; j < 2; ++j)
9080 if (EQ (old_buffer, echo_area_buffer[j]))
9081 echo_area_buffer[j] = echo_buffer[i];
9082 }
9083 }
9084
9085
9086 /* Call FN with args A1..A4 with either the current or last displayed
9087 echo_area_buffer as current buffer.
9088
9089 WHICH zero means use the current message buffer
9090 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9091 from echo_buffer[] and clear it.
9092
9093 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9094 suitable buffer from echo_buffer[] and clear it.
9095
9096 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9097 that the current message becomes the last displayed one, make
9098 choose a suitable buffer for echo_area_buffer[0], and clear it.
9099
9100 Value is what FN returns. */
9101
9102 static int
9103 with_echo_area_buffer (struct window *w, int which,
9104 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9105 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9106 {
9107 Lisp_Object buffer;
9108 int this_one, the_other, clear_buffer_p, rc;
9109 int count = SPECPDL_INDEX ();
9110
9111 /* If buffers aren't live, make new ones. */
9112 ensure_echo_area_buffers ();
9113
9114 clear_buffer_p = 0;
9115
9116 if (which == 0)
9117 this_one = 0, the_other = 1;
9118 else if (which > 0)
9119 this_one = 1, the_other = 0;
9120 else
9121 {
9122 this_one = 0, the_other = 1;
9123 clear_buffer_p = 1;
9124
9125 /* We need a fresh one in case the current echo buffer equals
9126 the one containing the last displayed echo area message. */
9127 if (!NILP (echo_area_buffer[this_one])
9128 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9129 echo_area_buffer[this_one] = Qnil;
9130 }
9131
9132 /* Choose a suitable buffer from echo_buffer[] is we don't
9133 have one. */
9134 if (NILP (echo_area_buffer[this_one]))
9135 {
9136 echo_area_buffer[this_one]
9137 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9138 ? echo_buffer[the_other]
9139 : echo_buffer[this_one]);
9140 clear_buffer_p = 1;
9141 }
9142
9143 buffer = echo_area_buffer[this_one];
9144
9145 /* Don't get confused by reusing the buffer used for echoing
9146 for a different purpose. */
9147 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9148 cancel_echoing ();
9149
9150 record_unwind_protect (unwind_with_echo_area_buffer,
9151 with_echo_area_buffer_unwind_data (w));
9152
9153 /* Make the echo area buffer current. Note that for display
9154 purposes, it is not necessary that the displayed window's buffer
9155 == current_buffer, except for text property lookup. So, let's
9156 only set that buffer temporarily here without doing a full
9157 Fset_window_buffer. We must also change w->pointm, though,
9158 because otherwise an assertions in unshow_buffer fails, and Emacs
9159 aborts. */
9160 set_buffer_internal_1 (XBUFFER (buffer));
9161 if (w)
9162 {
9163 w->buffer = buffer;
9164 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9165 }
9166
9167 BVAR (current_buffer, undo_list) = Qt;
9168 BVAR (current_buffer, read_only) = Qnil;
9169 specbind (Qinhibit_read_only, Qt);
9170 specbind (Qinhibit_modification_hooks, Qt);
9171
9172 if (clear_buffer_p && Z > BEG)
9173 del_range (BEG, Z);
9174
9175 xassert (BEGV >= BEG);
9176 xassert (ZV <= Z && ZV >= BEGV);
9177
9178 rc = fn (a1, a2, a3, a4);
9179
9180 xassert (BEGV >= BEG);
9181 xassert (ZV <= Z && ZV >= BEGV);
9182
9183 unbind_to (count, Qnil);
9184 return rc;
9185 }
9186
9187
9188 /* Save state that should be preserved around the call to the function
9189 FN called in with_echo_area_buffer. */
9190
9191 static Lisp_Object
9192 with_echo_area_buffer_unwind_data (struct window *w)
9193 {
9194 int i = 0;
9195 Lisp_Object vector, tmp;
9196
9197 /* Reduce consing by keeping one vector in
9198 Vwith_echo_area_save_vector. */
9199 vector = Vwith_echo_area_save_vector;
9200 Vwith_echo_area_save_vector = Qnil;
9201
9202 if (NILP (vector))
9203 vector = Fmake_vector (make_number (7), Qnil);
9204
9205 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9206 ASET (vector, i, Vdeactivate_mark); ++i;
9207 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9208
9209 if (w)
9210 {
9211 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9212 ASET (vector, i, w->buffer); ++i;
9213 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9214 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9215 }
9216 else
9217 {
9218 int end = i + 4;
9219 for (; i < end; ++i)
9220 ASET (vector, i, Qnil);
9221 }
9222
9223 xassert (i == ASIZE (vector));
9224 return vector;
9225 }
9226
9227
9228 /* Restore global state from VECTOR which was created by
9229 with_echo_area_buffer_unwind_data. */
9230
9231 static Lisp_Object
9232 unwind_with_echo_area_buffer (Lisp_Object vector)
9233 {
9234 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9235 Vdeactivate_mark = AREF (vector, 1);
9236 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9237
9238 if (WINDOWP (AREF (vector, 3)))
9239 {
9240 struct window *w;
9241 Lisp_Object buffer, charpos, bytepos;
9242
9243 w = XWINDOW (AREF (vector, 3));
9244 buffer = AREF (vector, 4);
9245 charpos = AREF (vector, 5);
9246 bytepos = AREF (vector, 6);
9247
9248 w->buffer = buffer;
9249 set_marker_both (w->pointm, buffer,
9250 XFASTINT (charpos), XFASTINT (bytepos));
9251 }
9252
9253 Vwith_echo_area_save_vector = vector;
9254 return Qnil;
9255 }
9256
9257
9258 /* Set up the echo area for use by print functions. MULTIBYTE_P
9259 non-zero means we will print multibyte. */
9260
9261 void
9262 setup_echo_area_for_printing (int multibyte_p)
9263 {
9264 /* If we can't find an echo area any more, exit. */
9265 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9266 Fkill_emacs (Qnil);
9267
9268 ensure_echo_area_buffers ();
9269
9270 if (!message_buf_print)
9271 {
9272 /* A message has been output since the last time we printed.
9273 Choose a fresh echo area buffer. */
9274 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9275 echo_area_buffer[0] = echo_buffer[1];
9276 else
9277 echo_area_buffer[0] = echo_buffer[0];
9278
9279 /* Switch to that buffer and clear it. */
9280 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9281 BVAR (current_buffer, truncate_lines) = Qnil;
9282
9283 if (Z > BEG)
9284 {
9285 int count = SPECPDL_INDEX ();
9286 specbind (Qinhibit_read_only, Qt);
9287 /* Note that undo recording is always disabled. */
9288 del_range (BEG, Z);
9289 unbind_to (count, Qnil);
9290 }
9291 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9292
9293 /* Set up the buffer for the multibyteness we need. */
9294 if (multibyte_p
9295 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9296 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9297
9298 /* Raise the frame containing the echo area. */
9299 if (minibuffer_auto_raise)
9300 {
9301 struct frame *sf = SELECTED_FRAME ();
9302 Lisp_Object mini_window;
9303 mini_window = FRAME_MINIBUF_WINDOW (sf);
9304 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9305 }
9306
9307 message_log_maybe_newline ();
9308 message_buf_print = 1;
9309 }
9310 else
9311 {
9312 if (NILP (echo_area_buffer[0]))
9313 {
9314 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9315 echo_area_buffer[0] = echo_buffer[1];
9316 else
9317 echo_area_buffer[0] = echo_buffer[0];
9318 }
9319
9320 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9321 {
9322 /* Someone switched buffers between print requests. */
9323 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9324 BVAR (current_buffer, truncate_lines) = Qnil;
9325 }
9326 }
9327 }
9328
9329
9330 /* Display an echo area message in window W. Value is non-zero if W's
9331 height is changed. If display_last_displayed_message_p is
9332 non-zero, display the message that was last displayed, otherwise
9333 display the current message. */
9334
9335 static int
9336 display_echo_area (struct window *w)
9337 {
9338 int i, no_message_p, window_height_changed_p, count;
9339
9340 /* Temporarily disable garbage collections while displaying the echo
9341 area. This is done because a GC can print a message itself.
9342 That message would modify the echo area buffer's contents while a
9343 redisplay of the buffer is going on, and seriously confuse
9344 redisplay. */
9345 count = inhibit_garbage_collection ();
9346
9347 /* If there is no message, we must call display_echo_area_1
9348 nevertheless because it resizes the window. But we will have to
9349 reset the echo_area_buffer in question to nil at the end because
9350 with_echo_area_buffer will sets it to an empty buffer. */
9351 i = display_last_displayed_message_p ? 1 : 0;
9352 no_message_p = NILP (echo_area_buffer[i]);
9353
9354 window_height_changed_p
9355 = with_echo_area_buffer (w, display_last_displayed_message_p,
9356 display_echo_area_1,
9357 (intptr_t) w, Qnil, 0, 0);
9358
9359 if (no_message_p)
9360 echo_area_buffer[i] = Qnil;
9361
9362 unbind_to (count, Qnil);
9363 return window_height_changed_p;
9364 }
9365
9366
9367 /* Helper for display_echo_area. Display the current buffer which
9368 contains the current echo area message in window W, a mini-window,
9369 a pointer to which is passed in A1. A2..A4 are currently not used.
9370 Change the height of W so that all of the message is displayed.
9371 Value is non-zero if height of W was changed. */
9372
9373 static int
9374 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9375 {
9376 intptr_t i1 = a1;
9377 struct window *w = (struct window *) i1;
9378 Lisp_Object window;
9379 struct text_pos start;
9380 int window_height_changed_p = 0;
9381
9382 /* Do this before displaying, so that we have a large enough glyph
9383 matrix for the display. If we can't get enough space for the
9384 whole text, display the last N lines. That works by setting w->start. */
9385 window_height_changed_p = resize_mini_window (w, 0);
9386
9387 /* Use the starting position chosen by resize_mini_window. */
9388 SET_TEXT_POS_FROM_MARKER (start, w->start);
9389
9390 /* Display. */
9391 clear_glyph_matrix (w->desired_matrix);
9392 XSETWINDOW (window, w);
9393 try_window (window, start, 0);
9394
9395 return window_height_changed_p;
9396 }
9397
9398
9399 /* Resize the echo area window to exactly the size needed for the
9400 currently displayed message, if there is one. If a mini-buffer
9401 is active, don't shrink it. */
9402
9403 void
9404 resize_echo_area_exactly (void)
9405 {
9406 if (BUFFERP (echo_area_buffer[0])
9407 && WINDOWP (echo_area_window))
9408 {
9409 struct window *w = XWINDOW (echo_area_window);
9410 int resized_p;
9411 Lisp_Object resize_exactly;
9412
9413 if (minibuf_level == 0)
9414 resize_exactly = Qt;
9415 else
9416 resize_exactly = Qnil;
9417
9418 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9419 (intptr_t) w, resize_exactly,
9420 0, 0);
9421 if (resized_p)
9422 {
9423 ++windows_or_buffers_changed;
9424 ++update_mode_lines;
9425 redisplay_internal ();
9426 }
9427 }
9428 }
9429
9430
9431 /* Callback function for with_echo_area_buffer, when used from
9432 resize_echo_area_exactly. A1 contains a pointer to the window to
9433 resize, EXACTLY non-nil means resize the mini-window exactly to the
9434 size of the text displayed. A3 and A4 are not used. Value is what
9435 resize_mini_window returns. */
9436
9437 static int
9438 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9439 {
9440 intptr_t i1 = a1;
9441 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9442 }
9443
9444
9445 /* Resize mini-window W to fit the size of its contents. EXACT_P
9446 means size the window exactly to the size needed. Otherwise, it's
9447 only enlarged until W's buffer is empty.
9448
9449 Set W->start to the right place to begin display. If the whole
9450 contents fit, start at the beginning. Otherwise, start so as
9451 to make the end of the contents appear. This is particularly
9452 important for y-or-n-p, but seems desirable generally.
9453
9454 Value is non-zero if the window height has been changed. */
9455
9456 int
9457 resize_mini_window (struct window *w, int exact_p)
9458 {
9459 struct frame *f = XFRAME (w->frame);
9460 int window_height_changed_p = 0;
9461
9462 xassert (MINI_WINDOW_P (w));
9463
9464 /* By default, start display at the beginning. */
9465 set_marker_both (w->start, w->buffer,
9466 BUF_BEGV (XBUFFER (w->buffer)),
9467 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9468
9469 /* Don't resize windows while redisplaying a window; it would
9470 confuse redisplay functions when the size of the window they are
9471 displaying changes from under them. Such a resizing can happen,
9472 for instance, when which-func prints a long message while
9473 we are running fontification-functions. We're running these
9474 functions with safe_call which binds inhibit-redisplay to t. */
9475 if (!NILP (Vinhibit_redisplay))
9476 return 0;
9477
9478 /* Nil means don't try to resize. */
9479 if (NILP (Vresize_mini_windows)
9480 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9481 return 0;
9482
9483 if (!FRAME_MINIBUF_ONLY_P (f))
9484 {
9485 struct it it;
9486 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9487 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9488 int height, max_height;
9489 int unit = FRAME_LINE_HEIGHT (f);
9490 struct text_pos start;
9491 struct buffer *old_current_buffer = NULL;
9492
9493 if (current_buffer != XBUFFER (w->buffer))
9494 {
9495 old_current_buffer = current_buffer;
9496 set_buffer_internal (XBUFFER (w->buffer));
9497 }
9498
9499 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9500
9501 /* Compute the max. number of lines specified by the user. */
9502 if (FLOATP (Vmax_mini_window_height))
9503 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9504 else if (INTEGERP (Vmax_mini_window_height))
9505 max_height = XINT (Vmax_mini_window_height);
9506 else
9507 max_height = total_height / 4;
9508
9509 /* Correct that max. height if it's bogus. */
9510 max_height = max (1, max_height);
9511 max_height = min (total_height, max_height);
9512
9513 /* Find out the height of the text in the window. */
9514 if (it.line_wrap == TRUNCATE)
9515 height = 1;
9516 else
9517 {
9518 last_height = 0;
9519 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9520 if (it.max_ascent == 0 && it.max_descent == 0)
9521 height = it.current_y + last_height;
9522 else
9523 height = it.current_y + it.max_ascent + it.max_descent;
9524 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9525 height = (height + unit - 1) / unit;
9526 }
9527
9528 /* Compute a suitable window start. */
9529 if (height > max_height)
9530 {
9531 height = max_height;
9532 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9533 move_it_vertically_backward (&it, (height - 1) * unit);
9534 start = it.current.pos;
9535 }
9536 else
9537 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9538 SET_MARKER_FROM_TEXT_POS (w->start, start);
9539
9540 if (EQ (Vresize_mini_windows, Qgrow_only))
9541 {
9542 /* Let it grow only, until we display an empty message, in which
9543 case the window shrinks again. */
9544 if (height > WINDOW_TOTAL_LINES (w))
9545 {
9546 int old_height = WINDOW_TOTAL_LINES (w);
9547 freeze_window_starts (f, 1);
9548 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9549 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9550 }
9551 else if (height < WINDOW_TOTAL_LINES (w)
9552 && (exact_p || BEGV == ZV))
9553 {
9554 int old_height = WINDOW_TOTAL_LINES (w);
9555 freeze_window_starts (f, 0);
9556 shrink_mini_window (w);
9557 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9558 }
9559 }
9560 else
9561 {
9562 /* Always resize to exact size needed. */
9563 if (height > WINDOW_TOTAL_LINES (w))
9564 {
9565 int old_height = WINDOW_TOTAL_LINES (w);
9566 freeze_window_starts (f, 1);
9567 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9568 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9569 }
9570 else if (height < WINDOW_TOTAL_LINES (w))
9571 {
9572 int old_height = WINDOW_TOTAL_LINES (w);
9573 freeze_window_starts (f, 0);
9574 shrink_mini_window (w);
9575
9576 if (height)
9577 {
9578 freeze_window_starts (f, 1);
9579 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9580 }
9581
9582 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9583 }
9584 }
9585
9586 if (old_current_buffer)
9587 set_buffer_internal (old_current_buffer);
9588 }
9589
9590 return window_height_changed_p;
9591 }
9592
9593
9594 /* Value is the current message, a string, or nil if there is no
9595 current message. */
9596
9597 Lisp_Object
9598 current_message (void)
9599 {
9600 Lisp_Object msg;
9601
9602 if (!BUFFERP (echo_area_buffer[0]))
9603 msg = Qnil;
9604 else
9605 {
9606 with_echo_area_buffer (0, 0, current_message_1,
9607 (intptr_t) &msg, Qnil, 0, 0);
9608 if (NILP (msg))
9609 echo_area_buffer[0] = Qnil;
9610 }
9611
9612 return msg;
9613 }
9614
9615
9616 static int
9617 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9618 {
9619 intptr_t i1 = a1;
9620 Lisp_Object *msg = (Lisp_Object *) i1;
9621
9622 if (Z > BEG)
9623 *msg = make_buffer_string (BEG, Z, 1);
9624 else
9625 *msg = Qnil;
9626 return 0;
9627 }
9628
9629
9630 /* Push the current message on Vmessage_stack for later restauration
9631 by restore_message. Value is non-zero if the current message isn't
9632 empty. This is a relatively infrequent operation, so it's not
9633 worth optimizing. */
9634
9635 int
9636 push_message (void)
9637 {
9638 Lisp_Object msg;
9639 msg = current_message ();
9640 Vmessage_stack = Fcons (msg, Vmessage_stack);
9641 return STRINGP (msg);
9642 }
9643
9644
9645 /* Restore message display from the top of Vmessage_stack. */
9646
9647 void
9648 restore_message (void)
9649 {
9650 Lisp_Object msg;
9651
9652 xassert (CONSP (Vmessage_stack));
9653 msg = XCAR (Vmessage_stack);
9654 if (STRINGP (msg))
9655 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9656 else
9657 message3_nolog (msg, 0, 0);
9658 }
9659
9660
9661 /* Handler for record_unwind_protect calling pop_message. */
9662
9663 Lisp_Object
9664 pop_message_unwind (Lisp_Object dummy)
9665 {
9666 pop_message ();
9667 return Qnil;
9668 }
9669
9670 /* Pop the top-most entry off Vmessage_stack. */
9671
9672 static void
9673 pop_message (void)
9674 {
9675 xassert (CONSP (Vmessage_stack));
9676 Vmessage_stack = XCDR (Vmessage_stack);
9677 }
9678
9679
9680 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9681 exits. If the stack is not empty, we have a missing pop_message
9682 somewhere. */
9683
9684 void
9685 check_message_stack (void)
9686 {
9687 if (!NILP (Vmessage_stack))
9688 abort ();
9689 }
9690
9691
9692 /* Truncate to NCHARS what will be displayed in the echo area the next
9693 time we display it---but don't redisplay it now. */
9694
9695 void
9696 truncate_echo_area (EMACS_INT nchars)
9697 {
9698 if (nchars == 0)
9699 echo_area_buffer[0] = Qnil;
9700 /* A null message buffer means that the frame hasn't really been
9701 initialized yet. Error messages get reported properly by
9702 cmd_error, so this must be just an informative message; toss it. */
9703 else if (!noninteractive
9704 && INTERACTIVE
9705 && !NILP (echo_area_buffer[0]))
9706 {
9707 struct frame *sf = SELECTED_FRAME ();
9708 if (FRAME_MESSAGE_BUF (sf))
9709 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9710 }
9711 }
9712
9713
9714 /* Helper function for truncate_echo_area. Truncate the current
9715 message to at most NCHARS characters. */
9716
9717 static int
9718 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9719 {
9720 if (BEG + nchars < Z)
9721 del_range (BEG + nchars, Z);
9722 if (Z == BEG)
9723 echo_area_buffer[0] = Qnil;
9724 return 0;
9725 }
9726
9727
9728 /* Set the current message to a substring of S or STRING.
9729
9730 If STRING is a Lisp string, set the message to the first NBYTES
9731 bytes from STRING. NBYTES zero means use the whole string. If
9732 STRING is multibyte, the message will be displayed multibyte.
9733
9734 If S is not null, set the message to the first LEN bytes of S. LEN
9735 zero means use the whole string. MULTIBYTE_P non-zero means S is
9736 multibyte. Display the message multibyte in that case.
9737
9738 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9739 to t before calling set_message_1 (which calls insert).
9740 */
9741
9742 static void
9743 set_message (const char *s, Lisp_Object string,
9744 EMACS_INT nbytes, int multibyte_p)
9745 {
9746 message_enable_multibyte
9747 = ((s && multibyte_p)
9748 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9749
9750 with_echo_area_buffer (0, -1, set_message_1,
9751 (intptr_t) s, string, nbytes, multibyte_p);
9752 message_buf_print = 0;
9753 help_echo_showing_p = 0;
9754 }
9755
9756
9757 /* Helper function for set_message. Arguments have the same meaning
9758 as there, with A1 corresponding to S and A2 corresponding to STRING
9759 This function is called with the echo area buffer being
9760 current. */
9761
9762 static int
9763 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9764 {
9765 intptr_t i1 = a1;
9766 const char *s = (const char *) i1;
9767 const unsigned char *msg = (const unsigned char *) s;
9768 Lisp_Object string = a2;
9769
9770 /* Change multibyteness of the echo buffer appropriately. */
9771 if (message_enable_multibyte
9772 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9773 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9774
9775 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9776 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9777 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9778
9779 /* Insert new message at BEG. */
9780 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9781
9782 if (STRINGP (string))
9783 {
9784 EMACS_INT nchars;
9785
9786 if (nbytes == 0)
9787 nbytes = SBYTES (string);
9788 nchars = string_byte_to_char (string, nbytes);
9789
9790 /* This function takes care of single/multibyte conversion. We
9791 just have to ensure that the echo area buffer has the right
9792 setting of enable_multibyte_characters. */
9793 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9794 }
9795 else if (s)
9796 {
9797 if (nbytes == 0)
9798 nbytes = strlen (s);
9799
9800 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9801 {
9802 /* Convert from multi-byte to single-byte. */
9803 EMACS_INT i;
9804 int c, n;
9805 char work[1];
9806
9807 /* Convert a multibyte string to single-byte. */
9808 for (i = 0; i < nbytes; i += n)
9809 {
9810 c = string_char_and_length (msg + i, &n);
9811 work[0] = (ASCII_CHAR_P (c)
9812 ? c
9813 : multibyte_char_to_unibyte (c));
9814 insert_1_both (work, 1, 1, 1, 0, 0);
9815 }
9816 }
9817 else if (!multibyte_p
9818 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9819 {
9820 /* Convert from single-byte to multi-byte. */
9821 EMACS_INT i;
9822 int c, n;
9823 unsigned char str[MAX_MULTIBYTE_LENGTH];
9824
9825 /* Convert a single-byte string to multibyte. */
9826 for (i = 0; i < nbytes; i++)
9827 {
9828 c = msg[i];
9829 MAKE_CHAR_MULTIBYTE (c);
9830 n = CHAR_STRING (c, str);
9831 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9832 }
9833 }
9834 else
9835 insert_1 (s, nbytes, 1, 0, 0);
9836 }
9837
9838 return 0;
9839 }
9840
9841
9842 /* Clear messages. CURRENT_P non-zero means clear the current
9843 message. LAST_DISPLAYED_P non-zero means clear the message
9844 last displayed. */
9845
9846 void
9847 clear_message (int current_p, int last_displayed_p)
9848 {
9849 if (current_p)
9850 {
9851 echo_area_buffer[0] = Qnil;
9852 message_cleared_p = 1;
9853 }
9854
9855 if (last_displayed_p)
9856 echo_area_buffer[1] = Qnil;
9857
9858 message_buf_print = 0;
9859 }
9860
9861 /* Clear garbaged frames.
9862
9863 This function is used where the old redisplay called
9864 redraw_garbaged_frames which in turn called redraw_frame which in
9865 turn called clear_frame. The call to clear_frame was a source of
9866 flickering. I believe a clear_frame is not necessary. It should
9867 suffice in the new redisplay to invalidate all current matrices,
9868 and ensure a complete redisplay of all windows. */
9869
9870 static void
9871 clear_garbaged_frames (void)
9872 {
9873 if (frame_garbaged)
9874 {
9875 Lisp_Object tail, frame;
9876 int changed_count = 0;
9877
9878 FOR_EACH_FRAME (tail, frame)
9879 {
9880 struct frame *f = XFRAME (frame);
9881
9882 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9883 {
9884 if (f->resized_p)
9885 {
9886 Fredraw_frame (frame);
9887 f->force_flush_display_p = 1;
9888 }
9889 clear_current_matrices (f);
9890 changed_count++;
9891 f->garbaged = 0;
9892 f->resized_p = 0;
9893 }
9894 }
9895
9896 frame_garbaged = 0;
9897 if (changed_count)
9898 ++windows_or_buffers_changed;
9899 }
9900 }
9901
9902
9903 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9904 is non-zero update selected_frame. Value is non-zero if the
9905 mini-windows height has been changed. */
9906
9907 static int
9908 echo_area_display (int update_frame_p)
9909 {
9910 Lisp_Object mini_window;
9911 struct window *w;
9912 struct frame *f;
9913 int window_height_changed_p = 0;
9914 struct frame *sf = SELECTED_FRAME ();
9915
9916 mini_window = FRAME_MINIBUF_WINDOW (sf);
9917 w = XWINDOW (mini_window);
9918 f = XFRAME (WINDOW_FRAME (w));
9919
9920 /* Don't display if frame is invisible or not yet initialized. */
9921 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9922 return 0;
9923
9924 #ifdef HAVE_WINDOW_SYSTEM
9925 /* When Emacs starts, selected_frame may be the initial terminal
9926 frame. If we let this through, a message would be displayed on
9927 the terminal. */
9928 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9929 return 0;
9930 #endif /* HAVE_WINDOW_SYSTEM */
9931
9932 /* Redraw garbaged frames. */
9933 if (frame_garbaged)
9934 clear_garbaged_frames ();
9935
9936 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9937 {
9938 echo_area_window = mini_window;
9939 window_height_changed_p = display_echo_area (w);
9940 w->must_be_updated_p = 1;
9941
9942 /* Update the display, unless called from redisplay_internal.
9943 Also don't update the screen during redisplay itself. The
9944 update will happen at the end of redisplay, and an update
9945 here could cause confusion. */
9946 if (update_frame_p && !redisplaying_p)
9947 {
9948 int n = 0;
9949
9950 /* If the display update has been interrupted by pending
9951 input, update mode lines in the frame. Due to the
9952 pending input, it might have been that redisplay hasn't
9953 been called, so that mode lines above the echo area are
9954 garbaged. This looks odd, so we prevent it here. */
9955 if (!display_completed)
9956 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9957
9958 if (window_height_changed_p
9959 /* Don't do this if Emacs is shutting down. Redisplay
9960 needs to run hooks. */
9961 && !NILP (Vrun_hooks))
9962 {
9963 /* Must update other windows. Likewise as in other
9964 cases, don't let this update be interrupted by
9965 pending input. */
9966 int count = SPECPDL_INDEX ();
9967 specbind (Qredisplay_dont_pause, Qt);
9968 windows_or_buffers_changed = 1;
9969 redisplay_internal ();
9970 unbind_to (count, Qnil);
9971 }
9972 else if (FRAME_WINDOW_P (f) && n == 0)
9973 {
9974 /* Window configuration is the same as before.
9975 Can do with a display update of the echo area,
9976 unless we displayed some mode lines. */
9977 update_single_window (w, 1);
9978 FRAME_RIF (f)->flush_display (f);
9979 }
9980 else
9981 update_frame (f, 1, 1);
9982
9983 /* If cursor is in the echo area, make sure that the next
9984 redisplay displays the minibuffer, so that the cursor will
9985 be replaced with what the minibuffer wants. */
9986 if (cursor_in_echo_area)
9987 ++windows_or_buffers_changed;
9988 }
9989 }
9990 else if (!EQ (mini_window, selected_window))
9991 windows_or_buffers_changed++;
9992
9993 /* Last displayed message is now the current message. */
9994 echo_area_buffer[1] = echo_area_buffer[0];
9995 /* Inform read_char that we're not echoing. */
9996 echo_message_buffer = Qnil;
9997
9998 /* Prevent redisplay optimization in redisplay_internal by resetting
9999 this_line_start_pos. This is done because the mini-buffer now
10000 displays the message instead of its buffer text. */
10001 if (EQ (mini_window, selected_window))
10002 CHARPOS (this_line_start_pos) = 0;
10003
10004 return window_height_changed_p;
10005 }
10006
10007
10008 \f
10009 /***********************************************************************
10010 Mode Lines and Frame Titles
10011 ***********************************************************************/
10012
10013 /* A buffer for constructing non-propertized mode-line strings and
10014 frame titles in it; allocated from the heap in init_xdisp and
10015 resized as needed in store_mode_line_noprop_char. */
10016
10017 static char *mode_line_noprop_buf;
10018
10019 /* The buffer's end, and a current output position in it. */
10020
10021 static char *mode_line_noprop_buf_end;
10022 static char *mode_line_noprop_ptr;
10023
10024 #define MODE_LINE_NOPROP_LEN(start) \
10025 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10026
10027 static enum {
10028 MODE_LINE_DISPLAY = 0,
10029 MODE_LINE_TITLE,
10030 MODE_LINE_NOPROP,
10031 MODE_LINE_STRING
10032 } mode_line_target;
10033
10034 /* Alist that caches the results of :propertize.
10035 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10036 static Lisp_Object mode_line_proptrans_alist;
10037
10038 /* List of strings making up the mode-line. */
10039 static Lisp_Object mode_line_string_list;
10040
10041 /* Base face property when building propertized mode line string. */
10042 static Lisp_Object mode_line_string_face;
10043 static Lisp_Object mode_line_string_face_prop;
10044
10045
10046 /* Unwind data for mode line strings */
10047
10048 static Lisp_Object Vmode_line_unwind_vector;
10049
10050 static Lisp_Object
10051 format_mode_line_unwind_data (struct buffer *obuf,
10052 Lisp_Object owin,
10053 int save_proptrans)
10054 {
10055 Lisp_Object vector, tmp;
10056
10057 /* Reduce consing by keeping one vector in
10058 Vwith_echo_area_save_vector. */
10059 vector = Vmode_line_unwind_vector;
10060 Vmode_line_unwind_vector = Qnil;
10061
10062 if (NILP (vector))
10063 vector = Fmake_vector (make_number (8), Qnil);
10064
10065 ASET (vector, 0, make_number (mode_line_target));
10066 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10067 ASET (vector, 2, mode_line_string_list);
10068 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10069 ASET (vector, 4, mode_line_string_face);
10070 ASET (vector, 5, mode_line_string_face_prop);
10071
10072 if (obuf)
10073 XSETBUFFER (tmp, obuf);
10074 else
10075 tmp = Qnil;
10076 ASET (vector, 6, tmp);
10077 ASET (vector, 7, owin);
10078
10079 return vector;
10080 }
10081
10082 static Lisp_Object
10083 unwind_format_mode_line (Lisp_Object vector)
10084 {
10085 mode_line_target = XINT (AREF (vector, 0));
10086 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10087 mode_line_string_list = AREF (vector, 2);
10088 if (! EQ (AREF (vector, 3), Qt))
10089 mode_line_proptrans_alist = AREF (vector, 3);
10090 mode_line_string_face = AREF (vector, 4);
10091 mode_line_string_face_prop = AREF (vector, 5);
10092
10093 if (!NILP (AREF (vector, 7)))
10094 /* Select window before buffer, since it may change the buffer. */
10095 Fselect_window (AREF (vector, 7), Qt);
10096
10097 if (!NILP (AREF (vector, 6)))
10098 {
10099 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10100 ASET (vector, 6, Qnil);
10101 }
10102
10103 Vmode_line_unwind_vector = vector;
10104 return Qnil;
10105 }
10106
10107
10108 /* Store a single character C for the frame title in mode_line_noprop_buf.
10109 Re-allocate mode_line_noprop_buf if necessary. */
10110
10111 static void
10112 store_mode_line_noprop_char (char c)
10113 {
10114 /* If output position has reached the end of the allocated buffer,
10115 double the buffer's size. */
10116 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10117 {
10118 int len = MODE_LINE_NOPROP_LEN (0);
10119 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10120 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10121 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10122 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10123 }
10124
10125 *mode_line_noprop_ptr++ = c;
10126 }
10127
10128
10129 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10130 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10131 characters that yield more columns than PRECISION; PRECISION <= 0
10132 means copy the whole string. Pad with spaces until FIELD_WIDTH
10133 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10134 pad. Called from display_mode_element when it is used to build a
10135 frame title. */
10136
10137 static int
10138 store_mode_line_noprop (const char *string, int field_width, int precision)
10139 {
10140 const unsigned char *str = (const unsigned char *) string;
10141 int n = 0;
10142 EMACS_INT dummy, nbytes;
10143
10144 /* Copy at most PRECISION chars from STR. */
10145 nbytes = strlen (string);
10146 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10147 while (nbytes--)
10148 store_mode_line_noprop_char (*str++);
10149
10150 /* Fill up with spaces until FIELD_WIDTH reached. */
10151 while (field_width > 0
10152 && n < field_width)
10153 {
10154 store_mode_line_noprop_char (' ');
10155 ++n;
10156 }
10157
10158 return n;
10159 }
10160
10161 /***********************************************************************
10162 Frame Titles
10163 ***********************************************************************/
10164
10165 #ifdef HAVE_WINDOW_SYSTEM
10166
10167 /* Set the title of FRAME, if it has changed. The title format is
10168 Vicon_title_format if FRAME is iconified, otherwise it is
10169 frame_title_format. */
10170
10171 static void
10172 x_consider_frame_title (Lisp_Object frame)
10173 {
10174 struct frame *f = XFRAME (frame);
10175
10176 if (FRAME_WINDOW_P (f)
10177 || FRAME_MINIBUF_ONLY_P (f)
10178 || f->explicit_name)
10179 {
10180 /* Do we have more than one visible frame on this X display? */
10181 Lisp_Object tail;
10182 Lisp_Object fmt;
10183 int title_start;
10184 char *title;
10185 int len;
10186 struct it it;
10187 int count = SPECPDL_INDEX ();
10188
10189 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10190 {
10191 Lisp_Object other_frame = XCAR (tail);
10192 struct frame *tf = XFRAME (other_frame);
10193
10194 if (tf != f
10195 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10196 && !FRAME_MINIBUF_ONLY_P (tf)
10197 && !EQ (other_frame, tip_frame)
10198 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10199 break;
10200 }
10201
10202 /* Set global variable indicating that multiple frames exist. */
10203 multiple_frames = CONSP (tail);
10204
10205 /* Switch to the buffer of selected window of the frame. Set up
10206 mode_line_target so that display_mode_element will output into
10207 mode_line_noprop_buf; then display the title. */
10208 record_unwind_protect (unwind_format_mode_line,
10209 format_mode_line_unwind_data
10210 (current_buffer, selected_window, 0));
10211
10212 Fselect_window (f->selected_window, Qt);
10213 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10214 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10215
10216 mode_line_target = MODE_LINE_TITLE;
10217 title_start = MODE_LINE_NOPROP_LEN (0);
10218 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10219 NULL, DEFAULT_FACE_ID);
10220 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10221 len = MODE_LINE_NOPROP_LEN (title_start);
10222 title = mode_line_noprop_buf + title_start;
10223 unbind_to (count, Qnil);
10224
10225 /* Set the title only if it's changed. This avoids consing in
10226 the common case where it hasn't. (If it turns out that we've
10227 already wasted too much time by walking through the list with
10228 display_mode_element, then we might need to optimize at a
10229 higher level than this.) */
10230 if (! STRINGP (f->name)
10231 || SBYTES (f->name) != len
10232 || memcmp (title, SDATA (f->name), len) != 0)
10233 x_implicitly_set_name (f, make_string (title, len), Qnil);
10234 }
10235 }
10236
10237 #endif /* not HAVE_WINDOW_SYSTEM */
10238
10239
10240
10241 \f
10242 /***********************************************************************
10243 Menu Bars
10244 ***********************************************************************/
10245
10246
10247 /* Prepare for redisplay by updating menu-bar item lists when
10248 appropriate. This can call eval. */
10249
10250 void
10251 prepare_menu_bars (void)
10252 {
10253 int all_windows;
10254 struct gcpro gcpro1, gcpro2;
10255 struct frame *f;
10256 Lisp_Object tooltip_frame;
10257
10258 #ifdef HAVE_WINDOW_SYSTEM
10259 tooltip_frame = tip_frame;
10260 #else
10261 tooltip_frame = Qnil;
10262 #endif
10263
10264 /* Update all frame titles based on their buffer names, etc. We do
10265 this before the menu bars so that the buffer-menu will show the
10266 up-to-date frame titles. */
10267 #ifdef HAVE_WINDOW_SYSTEM
10268 if (windows_or_buffers_changed || update_mode_lines)
10269 {
10270 Lisp_Object tail, frame;
10271
10272 FOR_EACH_FRAME (tail, frame)
10273 {
10274 f = XFRAME (frame);
10275 if (!EQ (frame, tooltip_frame)
10276 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10277 x_consider_frame_title (frame);
10278 }
10279 }
10280 #endif /* HAVE_WINDOW_SYSTEM */
10281
10282 /* Update the menu bar item lists, if appropriate. This has to be
10283 done before any actual redisplay or generation of display lines. */
10284 all_windows = (update_mode_lines
10285 || buffer_shared > 1
10286 || windows_or_buffers_changed);
10287 if (all_windows)
10288 {
10289 Lisp_Object tail, frame;
10290 int count = SPECPDL_INDEX ();
10291 /* 1 means that update_menu_bar has run its hooks
10292 so any further calls to update_menu_bar shouldn't do so again. */
10293 int menu_bar_hooks_run = 0;
10294
10295 record_unwind_save_match_data ();
10296
10297 FOR_EACH_FRAME (tail, frame)
10298 {
10299 f = XFRAME (frame);
10300
10301 /* Ignore tooltip frame. */
10302 if (EQ (frame, tooltip_frame))
10303 continue;
10304
10305 /* If a window on this frame changed size, report that to
10306 the user and clear the size-change flag. */
10307 if (FRAME_WINDOW_SIZES_CHANGED (f))
10308 {
10309 Lisp_Object functions;
10310
10311 /* Clear flag first in case we get an error below. */
10312 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10313 functions = Vwindow_size_change_functions;
10314 GCPRO2 (tail, functions);
10315
10316 while (CONSP (functions))
10317 {
10318 if (!EQ (XCAR (functions), Qt))
10319 call1 (XCAR (functions), frame);
10320 functions = XCDR (functions);
10321 }
10322 UNGCPRO;
10323 }
10324
10325 GCPRO1 (tail);
10326 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10327 #ifdef HAVE_WINDOW_SYSTEM
10328 update_tool_bar (f, 0);
10329 #endif
10330 #ifdef HAVE_NS
10331 if (windows_or_buffers_changed
10332 && FRAME_NS_P (f))
10333 ns_set_doc_edited (f, Fbuffer_modified_p
10334 (XWINDOW (f->selected_window)->buffer));
10335 #endif
10336 UNGCPRO;
10337 }
10338
10339 unbind_to (count, Qnil);
10340 }
10341 else
10342 {
10343 struct frame *sf = SELECTED_FRAME ();
10344 update_menu_bar (sf, 1, 0);
10345 #ifdef HAVE_WINDOW_SYSTEM
10346 update_tool_bar (sf, 1);
10347 #endif
10348 }
10349 }
10350
10351
10352 /* Update the menu bar item list for frame F. This has to be done
10353 before we start to fill in any display lines, because it can call
10354 eval.
10355
10356 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10357
10358 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10359 already ran the menu bar hooks for this redisplay, so there
10360 is no need to run them again. The return value is the
10361 updated value of this flag, to pass to the next call. */
10362
10363 static int
10364 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10365 {
10366 Lisp_Object window;
10367 register struct window *w;
10368
10369 /* If called recursively during a menu update, do nothing. This can
10370 happen when, for instance, an activate-menubar-hook causes a
10371 redisplay. */
10372 if (inhibit_menubar_update)
10373 return hooks_run;
10374
10375 window = FRAME_SELECTED_WINDOW (f);
10376 w = XWINDOW (window);
10377
10378 if (FRAME_WINDOW_P (f)
10379 ?
10380 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10381 || defined (HAVE_NS) || defined (USE_GTK)
10382 FRAME_EXTERNAL_MENU_BAR (f)
10383 #else
10384 FRAME_MENU_BAR_LINES (f) > 0
10385 #endif
10386 : FRAME_MENU_BAR_LINES (f) > 0)
10387 {
10388 /* If the user has switched buffers or windows, we need to
10389 recompute to reflect the new bindings. But we'll
10390 recompute when update_mode_lines is set too; that means
10391 that people can use force-mode-line-update to request
10392 that the menu bar be recomputed. The adverse effect on
10393 the rest of the redisplay algorithm is about the same as
10394 windows_or_buffers_changed anyway. */
10395 if (windows_or_buffers_changed
10396 /* This used to test w->update_mode_line, but we believe
10397 there is no need to recompute the menu in that case. */
10398 || update_mode_lines
10399 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10400 < BUF_MODIFF (XBUFFER (w->buffer)))
10401 != !NILP (w->last_had_star))
10402 || ((!NILP (Vtransient_mark_mode)
10403 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10404 != !NILP (w->region_showing)))
10405 {
10406 struct buffer *prev = current_buffer;
10407 int count = SPECPDL_INDEX ();
10408
10409 specbind (Qinhibit_menubar_update, Qt);
10410
10411 set_buffer_internal_1 (XBUFFER (w->buffer));
10412 if (save_match_data)
10413 record_unwind_save_match_data ();
10414 if (NILP (Voverriding_local_map_menu_flag))
10415 {
10416 specbind (Qoverriding_terminal_local_map, Qnil);
10417 specbind (Qoverriding_local_map, Qnil);
10418 }
10419
10420 if (!hooks_run)
10421 {
10422 /* Run the Lucid hook. */
10423 safe_run_hooks (Qactivate_menubar_hook);
10424
10425 /* If it has changed current-menubar from previous value,
10426 really recompute the menu-bar from the value. */
10427 if (! NILP (Vlucid_menu_bar_dirty_flag))
10428 call0 (Qrecompute_lucid_menubar);
10429
10430 safe_run_hooks (Qmenu_bar_update_hook);
10431
10432 hooks_run = 1;
10433 }
10434
10435 XSETFRAME (Vmenu_updating_frame, f);
10436 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10437
10438 /* Redisplay the menu bar in case we changed it. */
10439 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10440 || defined (HAVE_NS) || defined (USE_GTK)
10441 if (FRAME_WINDOW_P (f))
10442 {
10443 #if defined (HAVE_NS)
10444 /* All frames on Mac OS share the same menubar. So only
10445 the selected frame should be allowed to set it. */
10446 if (f == SELECTED_FRAME ())
10447 #endif
10448 set_frame_menubar (f, 0, 0);
10449 }
10450 else
10451 /* On a terminal screen, the menu bar is an ordinary screen
10452 line, and this makes it get updated. */
10453 w->update_mode_line = Qt;
10454 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10455 /* In the non-toolkit version, the menu bar is an ordinary screen
10456 line, and this makes it get updated. */
10457 w->update_mode_line = Qt;
10458 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10459
10460 unbind_to (count, Qnil);
10461 set_buffer_internal_1 (prev);
10462 }
10463 }
10464
10465 return hooks_run;
10466 }
10467
10468
10469 \f
10470 /***********************************************************************
10471 Output Cursor
10472 ***********************************************************************/
10473
10474 #ifdef HAVE_WINDOW_SYSTEM
10475
10476 /* EXPORT:
10477 Nominal cursor position -- where to draw output.
10478 HPOS and VPOS are window relative glyph matrix coordinates.
10479 X and Y are window relative pixel coordinates. */
10480
10481 struct cursor_pos output_cursor;
10482
10483
10484 /* EXPORT:
10485 Set the global variable output_cursor to CURSOR. All cursor
10486 positions are relative to updated_window. */
10487
10488 void
10489 set_output_cursor (struct cursor_pos *cursor)
10490 {
10491 output_cursor.hpos = cursor->hpos;
10492 output_cursor.vpos = cursor->vpos;
10493 output_cursor.x = cursor->x;
10494 output_cursor.y = cursor->y;
10495 }
10496
10497
10498 /* EXPORT for RIF:
10499 Set a nominal cursor position.
10500
10501 HPOS and VPOS are column/row positions in a window glyph matrix. X
10502 and Y are window text area relative pixel positions.
10503
10504 If this is done during an update, updated_window will contain the
10505 window that is being updated and the position is the future output
10506 cursor position for that window. If updated_window is null, use
10507 selected_window and display the cursor at the given position. */
10508
10509 void
10510 x_cursor_to (int vpos, int hpos, int y, int x)
10511 {
10512 struct window *w;
10513
10514 /* If updated_window is not set, work on selected_window. */
10515 if (updated_window)
10516 w = updated_window;
10517 else
10518 w = XWINDOW (selected_window);
10519
10520 /* Set the output cursor. */
10521 output_cursor.hpos = hpos;
10522 output_cursor.vpos = vpos;
10523 output_cursor.x = x;
10524 output_cursor.y = y;
10525
10526 /* If not called as part of an update, really display the cursor.
10527 This will also set the cursor position of W. */
10528 if (updated_window == NULL)
10529 {
10530 BLOCK_INPUT;
10531 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10532 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10533 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10534 UNBLOCK_INPUT;
10535 }
10536 }
10537
10538 #endif /* HAVE_WINDOW_SYSTEM */
10539
10540 \f
10541 /***********************************************************************
10542 Tool-bars
10543 ***********************************************************************/
10544
10545 #ifdef HAVE_WINDOW_SYSTEM
10546
10547 /* Where the mouse was last time we reported a mouse event. */
10548
10549 FRAME_PTR last_mouse_frame;
10550
10551 /* Tool-bar item index of the item on which a mouse button was pressed
10552 or -1. */
10553
10554 int last_tool_bar_item;
10555
10556
10557 static Lisp_Object
10558 update_tool_bar_unwind (Lisp_Object frame)
10559 {
10560 selected_frame = frame;
10561 return Qnil;
10562 }
10563
10564 /* Update the tool-bar item list for frame F. This has to be done
10565 before we start to fill in any display lines. Called from
10566 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10567 and restore it here. */
10568
10569 static void
10570 update_tool_bar (struct frame *f, int save_match_data)
10571 {
10572 #if defined (USE_GTK) || defined (HAVE_NS)
10573 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10574 #else
10575 int do_update = WINDOWP (f->tool_bar_window)
10576 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10577 #endif
10578
10579 if (do_update)
10580 {
10581 Lisp_Object window;
10582 struct window *w;
10583
10584 window = FRAME_SELECTED_WINDOW (f);
10585 w = XWINDOW (window);
10586
10587 /* If the user has switched buffers or windows, we need to
10588 recompute to reflect the new bindings. But we'll
10589 recompute when update_mode_lines is set too; that means
10590 that people can use force-mode-line-update to request
10591 that the menu bar be recomputed. The adverse effect on
10592 the rest of the redisplay algorithm is about the same as
10593 windows_or_buffers_changed anyway. */
10594 if (windows_or_buffers_changed
10595 || !NILP (w->update_mode_line)
10596 || update_mode_lines
10597 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10598 < BUF_MODIFF (XBUFFER (w->buffer)))
10599 != !NILP (w->last_had_star))
10600 || ((!NILP (Vtransient_mark_mode)
10601 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10602 != !NILP (w->region_showing)))
10603 {
10604 struct buffer *prev = current_buffer;
10605 int count = SPECPDL_INDEX ();
10606 Lisp_Object frame, new_tool_bar;
10607 int new_n_tool_bar;
10608 struct gcpro gcpro1;
10609
10610 /* Set current_buffer to the buffer of the selected
10611 window of the frame, so that we get the right local
10612 keymaps. */
10613 set_buffer_internal_1 (XBUFFER (w->buffer));
10614
10615 /* Save match data, if we must. */
10616 if (save_match_data)
10617 record_unwind_save_match_data ();
10618
10619 /* Make sure that we don't accidentally use bogus keymaps. */
10620 if (NILP (Voverriding_local_map_menu_flag))
10621 {
10622 specbind (Qoverriding_terminal_local_map, Qnil);
10623 specbind (Qoverriding_local_map, Qnil);
10624 }
10625
10626 GCPRO1 (new_tool_bar);
10627
10628 /* We must temporarily set the selected frame to this frame
10629 before calling tool_bar_items, because the calculation of
10630 the tool-bar keymap uses the selected frame (see
10631 `tool-bar-make-keymap' in tool-bar.el). */
10632 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10633 XSETFRAME (frame, f);
10634 selected_frame = frame;
10635
10636 /* Build desired tool-bar items from keymaps. */
10637 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10638 &new_n_tool_bar);
10639
10640 /* Redisplay the tool-bar if we changed it. */
10641 if (new_n_tool_bar != f->n_tool_bar_items
10642 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10643 {
10644 /* Redisplay that happens asynchronously due to an expose event
10645 may access f->tool_bar_items. Make sure we update both
10646 variables within BLOCK_INPUT so no such event interrupts. */
10647 BLOCK_INPUT;
10648 f->tool_bar_items = new_tool_bar;
10649 f->n_tool_bar_items = new_n_tool_bar;
10650 w->update_mode_line = Qt;
10651 UNBLOCK_INPUT;
10652 }
10653
10654 UNGCPRO;
10655
10656 unbind_to (count, Qnil);
10657 set_buffer_internal_1 (prev);
10658 }
10659 }
10660 }
10661
10662
10663 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10664 F's desired tool-bar contents. F->tool_bar_items must have
10665 been set up previously by calling prepare_menu_bars. */
10666
10667 static void
10668 build_desired_tool_bar_string (struct frame *f)
10669 {
10670 int i, size, size_needed;
10671 struct gcpro gcpro1, gcpro2, gcpro3;
10672 Lisp_Object image, plist, props;
10673
10674 image = plist = props = Qnil;
10675 GCPRO3 (image, plist, props);
10676
10677 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10678 Otherwise, make a new string. */
10679
10680 /* The size of the string we might be able to reuse. */
10681 size = (STRINGP (f->desired_tool_bar_string)
10682 ? SCHARS (f->desired_tool_bar_string)
10683 : 0);
10684
10685 /* We need one space in the string for each image. */
10686 size_needed = f->n_tool_bar_items;
10687
10688 /* Reuse f->desired_tool_bar_string, if possible. */
10689 if (size < size_needed || NILP (f->desired_tool_bar_string))
10690 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10691 make_number (' '));
10692 else
10693 {
10694 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10695 Fremove_text_properties (make_number (0), make_number (size),
10696 props, f->desired_tool_bar_string);
10697 }
10698
10699 /* Put a `display' property on the string for the images to display,
10700 put a `menu_item' property on tool-bar items with a value that
10701 is the index of the item in F's tool-bar item vector. */
10702 for (i = 0; i < f->n_tool_bar_items; ++i)
10703 {
10704 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10705
10706 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10707 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10708 int hmargin, vmargin, relief, idx, end;
10709
10710 /* If image is a vector, choose the image according to the
10711 button state. */
10712 image = PROP (TOOL_BAR_ITEM_IMAGES);
10713 if (VECTORP (image))
10714 {
10715 if (enabled_p)
10716 idx = (selected_p
10717 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10718 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10719 else
10720 idx = (selected_p
10721 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10722 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10723
10724 xassert (ASIZE (image) >= idx);
10725 image = AREF (image, idx);
10726 }
10727 else
10728 idx = -1;
10729
10730 /* Ignore invalid image specifications. */
10731 if (!valid_image_p (image))
10732 continue;
10733
10734 /* Display the tool-bar button pressed, or depressed. */
10735 plist = Fcopy_sequence (XCDR (image));
10736
10737 /* Compute margin and relief to draw. */
10738 relief = (tool_bar_button_relief >= 0
10739 ? tool_bar_button_relief
10740 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10741 hmargin = vmargin = relief;
10742
10743 if (INTEGERP (Vtool_bar_button_margin)
10744 && XINT (Vtool_bar_button_margin) > 0)
10745 {
10746 hmargin += XFASTINT (Vtool_bar_button_margin);
10747 vmargin += XFASTINT (Vtool_bar_button_margin);
10748 }
10749 else if (CONSP (Vtool_bar_button_margin))
10750 {
10751 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10752 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10753 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10754
10755 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10756 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10757 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10758 }
10759
10760 if (auto_raise_tool_bar_buttons_p)
10761 {
10762 /* Add a `:relief' property to the image spec if the item is
10763 selected. */
10764 if (selected_p)
10765 {
10766 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10767 hmargin -= relief;
10768 vmargin -= relief;
10769 }
10770 }
10771 else
10772 {
10773 /* If image is selected, display it pressed, i.e. with a
10774 negative relief. If it's not selected, display it with a
10775 raised relief. */
10776 plist = Fplist_put (plist, QCrelief,
10777 (selected_p
10778 ? make_number (-relief)
10779 : make_number (relief)));
10780 hmargin -= relief;
10781 vmargin -= relief;
10782 }
10783
10784 /* Put a margin around the image. */
10785 if (hmargin || vmargin)
10786 {
10787 if (hmargin == vmargin)
10788 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10789 else
10790 plist = Fplist_put (plist, QCmargin,
10791 Fcons (make_number (hmargin),
10792 make_number (vmargin)));
10793 }
10794
10795 /* If button is not enabled, and we don't have special images
10796 for the disabled state, make the image appear disabled by
10797 applying an appropriate algorithm to it. */
10798 if (!enabled_p && idx < 0)
10799 plist = Fplist_put (plist, QCconversion, Qdisabled);
10800
10801 /* Put a `display' text property on the string for the image to
10802 display. Put a `menu-item' property on the string that gives
10803 the start of this item's properties in the tool-bar items
10804 vector. */
10805 image = Fcons (Qimage, plist);
10806 props = list4 (Qdisplay, image,
10807 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10808
10809 /* Let the last image hide all remaining spaces in the tool bar
10810 string. The string can be longer than needed when we reuse a
10811 previous string. */
10812 if (i + 1 == f->n_tool_bar_items)
10813 end = SCHARS (f->desired_tool_bar_string);
10814 else
10815 end = i + 1;
10816 Fadd_text_properties (make_number (i), make_number (end),
10817 props, f->desired_tool_bar_string);
10818 #undef PROP
10819 }
10820
10821 UNGCPRO;
10822 }
10823
10824
10825 /* Display one line of the tool-bar of frame IT->f.
10826
10827 HEIGHT specifies the desired height of the tool-bar line.
10828 If the actual height of the glyph row is less than HEIGHT, the
10829 row's height is increased to HEIGHT, and the icons are centered
10830 vertically in the new height.
10831
10832 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10833 count a final empty row in case the tool-bar width exactly matches
10834 the window width.
10835 */
10836
10837 static void
10838 display_tool_bar_line (struct it *it, int height)
10839 {
10840 struct glyph_row *row = it->glyph_row;
10841 int max_x = it->last_visible_x;
10842 struct glyph *last;
10843
10844 prepare_desired_row (row);
10845 row->y = it->current_y;
10846
10847 /* Note that this isn't made use of if the face hasn't a box,
10848 so there's no need to check the face here. */
10849 it->start_of_box_run_p = 1;
10850
10851 while (it->current_x < max_x)
10852 {
10853 int x, n_glyphs_before, i, nglyphs;
10854 struct it it_before;
10855
10856 /* Get the next display element. */
10857 if (!get_next_display_element (it))
10858 {
10859 /* Don't count empty row if we are counting needed tool-bar lines. */
10860 if (height < 0 && !it->hpos)
10861 return;
10862 break;
10863 }
10864
10865 /* Produce glyphs. */
10866 n_glyphs_before = row->used[TEXT_AREA];
10867 it_before = *it;
10868
10869 PRODUCE_GLYPHS (it);
10870
10871 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10872 i = 0;
10873 x = it_before.current_x;
10874 while (i < nglyphs)
10875 {
10876 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10877
10878 if (x + glyph->pixel_width > max_x)
10879 {
10880 /* Glyph doesn't fit on line. Backtrack. */
10881 row->used[TEXT_AREA] = n_glyphs_before;
10882 *it = it_before;
10883 /* If this is the only glyph on this line, it will never fit on the
10884 tool-bar, so skip it. But ensure there is at least one glyph,
10885 so we don't accidentally disable the tool-bar. */
10886 if (n_glyphs_before == 0
10887 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10888 break;
10889 goto out;
10890 }
10891
10892 ++it->hpos;
10893 x += glyph->pixel_width;
10894 ++i;
10895 }
10896
10897 /* Stop at line end. */
10898 if (ITERATOR_AT_END_OF_LINE_P (it))
10899 break;
10900
10901 set_iterator_to_next (it, 1);
10902 }
10903
10904 out:;
10905
10906 row->displays_text_p = row->used[TEXT_AREA] != 0;
10907
10908 /* Use default face for the border below the tool bar.
10909
10910 FIXME: When auto-resize-tool-bars is grow-only, there is
10911 no additional border below the possibly empty tool-bar lines.
10912 So to make the extra empty lines look "normal", we have to
10913 use the tool-bar face for the border too. */
10914 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10915 it->face_id = DEFAULT_FACE_ID;
10916
10917 extend_face_to_end_of_line (it);
10918 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10919 last->right_box_line_p = 1;
10920 if (last == row->glyphs[TEXT_AREA])
10921 last->left_box_line_p = 1;
10922
10923 /* Make line the desired height and center it vertically. */
10924 if ((height -= it->max_ascent + it->max_descent) > 0)
10925 {
10926 /* Don't add more than one line height. */
10927 height %= FRAME_LINE_HEIGHT (it->f);
10928 it->max_ascent += height / 2;
10929 it->max_descent += (height + 1) / 2;
10930 }
10931
10932 compute_line_metrics (it);
10933
10934 /* If line is empty, make it occupy the rest of the tool-bar. */
10935 if (!row->displays_text_p)
10936 {
10937 row->height = row->phys_height = it->last_visible_y - row->y;
10938 row->visible_height = row->height;
10939 row->ascent = row->phys_ascent = 0;
10940 row->extra_line_spacing = 0;
10941 }
10942
10943 row->full_width_p = 1;
10944 row->continued_p = 0;
10945 row->truncated_on_left_p = 0;
10946 row->truncated_on_right_p = 0;
10947
10948 it->current_x = it->hpos = 0;
10949 it->current_y += row->height;
10950 ++it->vpos;
10951 ++it->glyph_row;
10952 }
10953
10954
10955 /* Max tool-bar height. */
10956
10957 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10958 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10959
10960 /* Value is the number of screen lines needed to make all tool-bar
10961 items of frame F visible. The number of actual rows needed is
10962 returned in *N_ROWS if non-NULL. */
10963
10964 static int
10965 tool_bar_lines_needed (struct frame *f, int *n_rows)
10966 {
10967 struct window *w = XWINDOW (f->tool_bar_window);
10968 struct it it;
10969 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10970 the desired matrix, so use (unused) mode-line row as temporary row to
10971 avoid destroying the first tool-bar row. */
10972 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10973
10974 /* Initialize an iterator for iteration over
10975 F->desired_tool_bar_string in the tool-bar window of frame F. */
10976 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10977 it.first_visible_x = 0;
10978 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10979 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10980 it.paragraph_embedding = L2R;
10981
10982 while (!ITERATOR_AT_END_P (&it))
10983 {
10984 clear_glyph_row (temp_row);
10985 it.glyph_row = temp_row;
10986 display_tool_bar_line (&it, -1);
10987 }
10988 clear_glyph_row (temp_row);
10989
10990 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10991 if (n_rows)
10992 *n_rows = it.vpos > 0 ? it.vpos : -1;
10993
10994 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10995 }
10996
10997
10998 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10999 0, 1, 0,
11000 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11001 (Lisp_Object frame)
11002 {
11003 struct frame *f;
11004 struct window *w;
11005 int nlines = 0;
11006
11007 if (NILP (frame))
11008 frame = selected_frame;
11009 else
11010 CHECK_FRAME (frame);
11011 f = XFRAME (frame);
11012
11013 if (WINDOWP (f->tool_bar_window)
11014 || (w = XWINDOW (f->tool_bar_window),
11015 WINDOW_TOTAL_LINES (w) > 0))
11016 {
11017 update_tool_bar (f, 1);
11018 if (f->n_tool_bar_items)
11019 {
11020 build_desired_tool_bar_string (f);
11021 nlines = tool_bar_lines_needed (f, NULL);
11022 }
11023 }
11024
11025 return make_number (nlines);
11026 }
11027
11028
11029 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11030 height should be changed. */
11031
11032 static int
11033 redisplay_tool_bar (struct frame *f)
11034 {
11035 struct window *w;
11036 struct it it;
11037 struct glyph_row *row;
11038
11039 #if defined (USE_GTK) || defined (HAVE_NS)
11040 if (FRAME_EXTERNAL_TOOL_BAR (f))
11041 update_frame_tool_bar (f);
11042 return 0;
11043 #endif
11044
11045 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11046 do anything. This means you must start with tool-bar-lines
11047 non-zero to get the auto-sizing effect. Or in other words, you
11048 can turn off tool-bars by specifying tool-bar-lines zero. */
11049 if (!WINDOWP (f->tool_bar_window)
11050 || (w = XWINDOW (f->tool_bar_window),
11051 WINDOW_TOTAL_LINES (w) == 0))
11052 return 0;
11053
11054 /* Set up an iterator for the tool-bar window. */
11055 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11056 it.first_visible_x = 0;
11057 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11058 row = it.glyph_row;
11059
11060 /* Build a string that represents the contents of the tool-bar. */
11061 build_desired_tool_bar_string (f);
11062 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11063 /* FIXME: This should be controlled by a user option. But it
11064 doesn't make sense to have an R2L tool bar if the menu bar cannot
11065 be drawn also R2L, and making the menu bar R2L is tricky due to
11066 unibyte strings it uses and toolkit-specific code that implements
11067 it. If an R2L tool bar is ever supported, display_tool_bar_line
11068 should also be augmented to call unproduce_glyphs like
11069 display_line and display_string do. */
11070 it.paragraph_embedding = L2R;
11071
11072 if (f->n_tool_bar_rows == 0)
11073 {
11074 int nlines;
11075
11076 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11077 nlines != WINDOW_TOTAL_LINES (w)))
11078 {
11079 Lisp_Object frame;
11080 int old_height = WINDOW_TOTAL_LINES (w);
11081
11082 XSETFRAME (frame, f);
11083 Fmodify_frame_parameters (frame,
11084 Fcons (Fcons (Qtool_bar_lines,
11085 make_number (nlines)),
11086 Qnil));
11087 if (WINDOW_TOTAL_LINES (w) != old_height)
11088 {
11089 clear_glyph_matrix (w->desired_matrix);
11090 fonts_changed_p = 1;
11091 return 1;
11092 }
11093 }
11094 }
11095
11096 /* Display as many lines as needed to display all tool-bar items. */
11097
11098 if (f->n_tool_bar_rows > 0)
11099 {
11100 int border, rows, height, extra;
11101
11102 if (INTEGERP (Vtool_bar_border))
11103 border = XINT (Vtool_bar_border);
11104 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11105 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11106 else if (EQ (Vtool_bar_border, Qborder_width))
11107 border = f->border_width;
11108 else
11109 border = 0;
11110 if (border < 0)
11111 border = 0;
11112
11113 rows = f->n_tool_bar_rows;
11114 height = max (1, (it.last_visible_y - border) / rows);
11115 extra = it.last_visible_y - border - height * rows;
11116
11117 while (it.current_y < it.last_visible_y)
11118 {
11119 int h = 0;
11120 if (extra > 0 && rows-- > 0)
11121 {
11122 h = (extra + rows - 1) / rows;
11123 extra -= h;
11124 }
11125 display_tool_bar_line (&it, height + h);
11126 }
11127 }
11128 else
11129 {
11130 while (it.current_y < it.last_visible_y)
11131 display_tool_bar_line (&it, 0);
11132 }
11133
11134 /* It doesn't make much sense to try scrolling in the tool-bar
11135 window, so don't do it. */
11136 w->desired_matrix->no_scrolling_p = 1;
11137 w->must_be_updated_p = 1;
11138
11139 if (!NILP (Vauto_resize_tool_bars))
11140 {
11141 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11142 int change_height_p = 0;
11143
11144 /* If we couldn't display everything, change the tool-bar's
11145 height if there is room for more. */
11146 if (IT_STRING_CHARPOS (it) < it.end_charpos
11147 && it.current_y < max_tool_bar_height)
11148 change_height_p = 1;
11149
11150 row = it.glyph_row - 1;
11151
11152 /* If there are blank lines at the end, except for a partially
11153 visible blank line at the end that is smaller than
11154 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11155 if (!row->displays_text_p
11156 && row->height >= FRAME_LINE_HEIGHT (f))
11157 change_height_p = 1;
11158
11159 /* If row displays tool-bar items, but is partially visible,
11160 change the tool-bar's height. */
11161 if (row->displays_text_p
11162 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11163 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11164 change_height_p = 1;
11165
11166 /* Resize windows as needed by changing the `tool-bar-lines'
11167 frame parameter. */
11168 if (change_height_p)
11169 {
11170 Lisp_Object frame;
11171 int old_height = WINDOW_TOTAL_LINES (w);
11172 int nrows;
11173 int nlines = tool_bar_lines_needed (f, &nrows);
11174
11175 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11176 && !f->minimize_tool_bar_window_p)
11177 ? (nlines > old_height)
11178 : (nlines != old_height));
11179 f->minimize_tool_bar_window_p = 0;
11180
11181 if (change_height_p)
11182 {
11183 XSETFRAME (frame, f);
11184 Fmodify_frame_parameters (frame,
11185 Fcons (Fcons (Qtool_bar_lines,
11186 make_number (nlines)),
11187 Qnil));
11188 if (WINDOW_TOTAL_LINES (w) != old_height)
11189 {
11190 clear_glyph_matrix (w->desired_matrix);
11191 f->n_tool_bar_rows = nrows;
11192 fonts_changed_p = 1;
11193 return 1;
11194 }
11195 }
11196 }
11197 }
11198
11199 f->minimize_tool_bar_window_p = 0;
11200 return 0;
11201 }
11202
11203
11204 /* Get information about the tool-bar item which is displayed in GLYPH
11205 on frame F. Return in *PROP_IDX the index where tool-bar item
11206 properties start in F->tool_bar_items. Value is zero if
11207 GLYPH doesn't display a tool-bar item. */
11208
11209 static int
11210 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11211 {
11212 Lisp_Object prop;
11213 int success_p;
11214 int charpos;
11215
11216 /* This function can be called asynchronously, which means we must
11217 exclude any possibility that Fget_text_property signals an
11218 error. */
11219 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11220 charpos = max (0, charpos);
11221
11222 /* Get the text property `menu-item' at pos. The value of that
11223 property is the start index of this item's properties in
11224 F->tool_bar_items. */
11225 prop = Fget_text_property (make_number (charpos),
11226 Qmenu_item, f->current_tool_bar_string);
11227 if (INTEGERP (prop))
11228 {
11229 *prop_idx = XINT (prop);
11230 success_p = 1;
11231 }
11232 else
11233 success_p = 0;
11234
11235 return success_p;
11236 }
11237
11238 \f
11239 /* Get information about the tool-bar item at position X/Y on frame F.
11240 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11241 the current matrix of the tool-bar window of F, or NULL if not
11242 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11243 item in F->tool_bar_items. Value is
11244
11245 -1 if X/Y is not on a tool-bar item
11246 0 if X/Y is on the same item that was highlighted before.
11247 1 otherwise. */
11248
11249 static int
11250 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11251 int *hpos, int *vpos, int *prop_idx)
11252 {
11253 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11254 struct window *w = XWINDOW (f->tool_bar_window);
11255 int area;
11256
11257 /* Find the glyph under X/Y. */
11258 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11259 if (*glyph == NULL)
11260 return -1;
11261
11262 /* Get the start of this tool-bar item's properties in
11263 f->tool_bar_items. */
11264 if (!tool_bar_item_info (f, *glyph, prop_idx))
11265 return -1;
11266
11267 /* Is mouse on the highlighted item? */
11268 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11269 && *vpos >= hlinfo->mouse_face_beg_row
11270 && *vpos <= hlinfo->mouse_face_end_row
11271 && (*vpos > hlinfo->mouse_face_beg_row
11272 || *hpos >= hlinfo->mouse_face_beg_col)
11273 && (*vpos < hlinfo->mouse_face_end_row
11274 || *hpos < hlinfo->mouse_face_end_col
11275 || hlinfo->mouse_face_past_end))
11276 return 0;
11277
11278 return 1;
11279 }
11280
11281
11282 /* EXPORT:
11283 Handle mouse button event on the tool-bar of frame F, at
11284 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11285 0 for button release. MODIFIERS is event modifiers for button
11286 release. */
11287
11288 void
11289 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11290 unsigned int modifiers)
11291 {
11292 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11293 struct window *w = XWINDOW (f->tool_bar_window);
11294 int hpos, vpos, prop_idx;
11295 struct glyph *glyph;
11296 Lisp_Object enabled_p;
11297
11298 /* If not on the highlighted tool-bar item, return. */
11299 frame_to_window_pixel_xy (w, &x, &y);
11300 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11301 return;
11302
11303 /* If item is disabled, do nothing. */
11304 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11305 if (NILP (enabled_p))
11306 return;
11307
11308 if (down_p)
11309 {
11310 /* Show item in pressed state. */
11311 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11312 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11313 last_tool_bar_item = prop_idx;
11314 }
11315 else
11316 {
11317 Lisp_Object key, frame;
11318 struct input_event event;
11319 EVENT_INIT (event);
11320
11321 /* Show item in released state. */
11322 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11323 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11324
11325 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11326
11327 XSETFRAME (frame, f);
11328 event.kind = TOOL_BAR_EVENT;
11329 event.frame_or_window = frame;
11330 event.arg = frame;
11331 kbd_buffer_store_event (&event);
11332
11333 event.kind = TOOL_BAR_EVENT;
11334 event.frame_or_window = frame;
11335 event.arg = key;
11336 event.modifiers = modifiers;
11337 kbd_buffer_store_event (&event);
11338 last_tool_bar_item = -1;
11339 }
11340 }
11341
11342
11343 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11344 tool-bar window-relative coordinates X/Y. Called from
11345 note_mouse_highlight. */
11346
11347 static void
11348 note_tool_bar_highlight (struct frame *f, int x, int y)
11349 {
11350 Lisp_Object window = f->tool_bar_window;
11351 struct window *w = XWINDOW (window);
11352 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11353 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11354 int hpos, vpos;
11355 struct glyph *glyph;
11356 struct glyph_row *row;
11357 int i;
11358 Lisp_Object enabled_p;
11359 int prop_idx;
11360 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11361 int mouse_down_p, rc;
11362
11363 /* Function note_mouse_highlight is called with negative X/Y
11364 values when mouse moves outside of the frame. */
11365 if (x <= 0 || y <= 0)
11366 {
11367 clear_mouse_face (hlinfo);
11368 return;
11369 }
11370
11371 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11372 if (rc < 0)
11373 {
11374 /* Not on tool-bar item. */
11375 clear_mouse_face (hlinfo);
11376 return;
11377 }
11378 else if (rc == 0)
11379 /* On same tool-bar item as before. */
11380 goto set_help_echo;
11381
11382 clear_mouse_face (hlinfo);
11383
11384 /* Mouse is down, but on different tool-bar item? */
11385 mouse_down_p = (dpyinfo->grabbed
11386 && f == last_mouse_frame
11387 && FRAME_LIVE_P (f));
11388 if (mouse_down_p
11389 && last_tool_bar_item != prop_idx)
11390 return;
11391
11392 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11393 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11394
11395 /* If tool-bar item is not enabled, don't highlight it. */
11396 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11397 if (!NILP (enabled_p))
11398 {
11399 /* Compute the x-position of the glyph. In front and past the
11400 image is a space. We include this in the highlighted area. */
11401 row = MATRIX_ROW (w->current_matrix, vpos);
11402 for (i = x = 0; i < hpos; ++i)
11403 x += row->glyphs[TEXT_AREA][i].pixel_width;
11404
11405 /* Record this as the current active region. */
11406 hlinfo->mouse_face_beg_col = hpos;
11407 hlinfo->mouse_face_beg_row = vpos;
11408 hlinfo->mouse_face_beg_x = x;
11409 hlinfo->mouse_face_beg_y = row->y;
11410 hlinfo->mouse_face_past_end = 0;
11411
11412 hlinfo->mouse_face_end_col = hpos + 1;
11413 hlinfo->mouse_face_end_row = vpos;
11414 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11415 hlinfo->mouse_face_end_y = row->y;
11416 hlinfo->mouse_face_window = window;
11417 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11418
11419 /* Display it as active. */
11420 show_mouse_face (hlinfo, draw);
11421 hlinfo->mouse_face_image_state = draw;
11422 }
11423
11424 set_help_echo:
11425
11426 /* Set help_echo_string to a help string to display for this tool-bar item.
11427 XTread_socket does the rest. */
11428 help_echo_object = help_echo_window = Qnil;
11429 help_echo_pos = -1;
11430 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11431 if (NILP (help_echo_string))
11432 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11433 }
11434
11435 #endif /* HAVE_WINDOW_SYSTEM */
11436
11437
11438 \f
11439 /************************************************************************
11440 Horizontal scrolling
11441 ************************************************************************/
11442
11443 static int hscroll_window_tree (Lisp_Object);
11444 static int hscroll_windows (Lisp_Object);
11445
11446 /* For all leaf windows in the window tree rooted at WINDOW, set their
11447 hscroll value so that PT is (i) visible in the window, and (ii) so
11448 that it is not within a certain margin at the window's left and
11449 right border. Value is non-zero if any window's hscroll has been
11450 changed. */
11451
11452 static int
11453 hscroll_window_tree (Lisp_Object window)
11454 {
11455 int hscrolled_p = 0;
11456 int hscroll_relative_p = FLOATP (Vhscroll_step);
11457 int hscroll_step_abs = 0;
11458 double hscroll_step_rel = 0;
11459
11460 if (hscroll_relative_p)
11461 {
11462 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11463 if (hscroll_step_rel < 0)
11464 {
11465 hscroll_relative_p = 0;
11466 hscroll_step_abs = 0;
11467 }
11468 }
11469 else if (INTEGERP (Vhscroll_step))
11470 {
11471 hscroll_step_abs = XINT (Vhscroll_step);
11472 if (hscroll_step_abs < 0)
11473 hscroll_step_abs = 0;
11474 }
11475 else
11476 hscroll_step_abs = 0;
11477
11478 while (WINDOWP (window))
11479 {
11480 struct window *w = XWINDOW (window);
11481
11482 if (WINDOWP (w->hchild))
11483 hscrolled_p |= hscroll_window_tree (w->hchild);
11484 else if (WINDOWP (w->vchild))
11485 hscrolled_p |= hscroll_window_tree (w->vchild);
11486 else if (w->cursor.vpos >= 0)
11487 {
11488 int h_margin;
11489 int text_area_width;
11490 struct glyph_row *current_cursor_row
11491 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11492 struct glyph_row *desired_cursor_row
11493 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11494 struct glyph_row *cursor_row
11495 = (desired_cursor_row->enabled_p
11496 ? desired_cursor_row
11497 : current_cursor_row);
11498
11499 text_area_width = window_box_width (w, TEXT_AREA);
11500
11501 /* Scroll when cursor is inside this scroll margin. */
11502 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11503
11504 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11505 && ((XFASTINT (w->hscroll)
11506 && w->cursor.x <= h_margin)
11507 || (cursor_row->enabled_p
11508 && cursor_row->truncated_on_right_p
11509 && (w->cursor.x >= text_area_width - h_margin))))
11510 {
11511 struct it it;
11512 int hscroll;
11513 struct buffer *saved_current_buffer;
11514 EMACS_INT pt;
11515 int wanted_x;
11516
11517 /* Find point in a display of infinite width. */
11518 saved_current_buffer = current_buffer;
11519 current_buffer = XBUFFER (w->buffer);
11520
11521 if (w == XWINDOW (selected_window))
11522 pt = PT;
11523 else
11524 {
11525 pt = marker_position (w->pointm);
11526 pt = max (BEGV, pt);
11527 pt = min (ZV, pt);
11528 }
11529
11530 /* Move iterator to pt starting at cursor_row->start in
11531 a line with infinite width. */
11532 init_to_row_start (&it, w, cursor_row);
11533 it.last_visible_x = INFINITY;
11534 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11535 current_buffer = saved_current_buffer;
11536
11537 /* Position cursor in window. */
11538 if (!hscroll_relative_p && hscroll_step_abs == 0)
11539 hscroll = max (0, (it.current_x
11540 - (ITERATOR_AT_END_OF_LINE_P (&it)
11541 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11542 : (text_area_width / 2))))
11543 / FRAME_COLUMN_WIDTH (it.f);
11544 else if (w->cursor.x >= text_area_width - h_margin)
11545 {
11546 if (hscroll_relative_p)
11547 wanted_x = text_area_width * (1 - hscroll_step_rel)
11548 - h_margin;
11549 else
11550 wanted_x = text_area_width
11551 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11552 - h_margin;
11553 hscroll
11554 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11555 }
11556 else
11557 {
11558 if (hscroll_relative_p)
11559 wanted_x = text_area_width * hscroll_step_rel
11560 + h_margin;
11561 else
11562 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11563 + h_margin;
11564 hscroll
11565 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11566 }
11567 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11568
11569 /* Don't call Fset_window_hscroll if value hasn't
11570 changed because it will prevent redisplay
11571 optimizations. */
11572 if (XFASTINT (w->hscroll) != hscroll)
11573 {
11574 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11575 w->hscroll = make_number (hscroll);
11576 hscrolled_p = 1;
11577 }
11578 }
11579 }
11580
11581 window = w->next;
11582 }
11583
11584 /* Value is non-zero if hscroll of any leaf window has been changed. */
11585 return hscrolled_p;
11586 }
11587
11588
11589 /* Set hscroll so that cursor is visible and not inside horizontal
11590 scroll margins for all windows in the tree rooted at WINDOW. See
11591 also hscroll_window_tree above. Value is non-zero if any window's
11592 hscroll has been changed. If it has, desired matrices on the frame
11593 of WINDOW are cleared. */
11594
11595 static int
11596 hscroll_windows (Lisp_Object window)
11597 {
11598 int hscrolled_p = hscroll_window_tree (window);
11599 if (hscrolled_p)
11600 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11601 return hscrolled_p;
11602 }
11603
11604
11605 \f
11606 /************************************************************************
11607 Redisplay
11608 ************************************************************************/
11609
11610 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11611 to a non-zero value. This is sometimes handy to have in a debugger
11612 session. */
11613
11614 #if GLYPH_DEBUG
11615
11616 /* First and last unchanged row for try_window_id. */
11617
11618 int debug_first_unchanged_at_end_vpos;
11619 int debug_last_unchanged_at_beg_vpos;
11620
11621 /* Delta vpos and y. */
11622
11623 int debug_dvpos, debug_dy;
11624
11625 /* Delta in characters and bytes for try_window_id. */
11626
11627 EMACS_INT debug_delta, debug_delta_bytes;
11628
11629 /* Values of window_end_pos and window_end_vpos at the end of
11630 try_window_id. */
11631
11632 EMACS_INT debug_end_vpos;
11633
11634 /* Append a string to W->desired_matrix->method. FMT is a printf
11635 format string. A1...A9 are a supplement for a variable-length
11636 argument list. If trace_redisplay_p is non-zero also printf the
11637 resulting string to stderr. */
11638
11639 static void
11640 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11641 struct window *w;
11642 char *fmt;
11643 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11644 {
11645 char buffer[512];
11646 char *method = w->desired_matrix->method;
11647 int len = strlen (method);
11648 int size = sizeof w->desired_matrix->method;
11649 int remaining = size - len - 1;
11650
11651 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11652 if (len && remaining)
11653 {
11654 method[len] = '|';
11655 --remaining, ++len;
11656 }
11657
11658 strncpy (method + len, buffer, remaining);
11659
11660 if (trace_redisplay_p)
11661 fprintf (stderr, "%p (%s): %s\n",
11662 w,
11663 ((BUFFERP (w->buffer)
11664 && STRINGP (XBUFFER (w->buffer)->name))
11665 ? SSDATA (XBUFFER (w->buffer)->name)
11666 : "no buffer"),
11667 buffer);
11668 }
11669
11670 #endif /* GLYPH_DEBUG */
11671
11672
11673 /* Value is non-zero if all changes in window W, which displays
11674 current_buffer, are in the text between START and END. START is a
11675 buffer position, END is given as a distance from Z. Used in
11676 redisplay_internal for display optimization. */
11677
11678 static INLINE int
11679 text_outside_line_unchanged_p (struct window *w,
11680 EMACS_INT start, EMACS_INT end)
11681 {
11682 int unchanged_p = 1;
11683
11684 /* If text or overlays have changed, see where. */
11685 if (XFASTINT (w->last_modified) < MODIFF
11686 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11687 {
11688 /* Gap in the line? */
11689 if (GPT < start || Z - GPT < end)
11690 unchanged_p = 0;
11691
11692 /* Changes start in front of the line, or end after it? */
11693 if (unchanged_p
11694 && (BEG_UNCHANGED < start - 1
11695 || END_UNCHANGED < end))
11696 unchanged_p = 0;
11697
11698 /* If selective display, can't optimize if changes start at the
11699 beginning of the line. */
11700 if (unchanged_p
11701 && INTEGERP (BVAR (current_buffer, selective_display))
11702 && XINT (BVAR (current_buffer, selective_display)) > 0
11703 && (BEG_UNCHANGED < start || GPT <= start))
11704 unchanged_p = 0;
11705
11706 /* If there are overlays at the start or end of the line, these
11707 may have overlay strings with newlines in them. A change at
11708 START, for instance, may actually concern the display of such
11709 overlay strings as well, and they are displayed on different
11710 lines. So, quickly rule out this case. (For the future, it
11711 might be desirable to implement something more telling than
11712 just BEG/END_UNCHANGED.) */
11713 if (unchanged_p)
11714 {
11715 if (BEG + BEG_UNCHANGED == start
11716 && overlay_touches_p (start))
11717 unchanged_p = 0;
11718 if (END_UNCHANGED == end
11719 && overlay_touches_p (Z - end))
11720 unchanged_p = 0;
11721 }
11722
11723 /* Under bidi reordering, adding or deleting a character in the
11724 beginning of a paragraph, before the first strong directional
11725 character, can change the base direction of the paragraph (unless
11726 the buffer specifies a fixed paragraph direction), which will
11727 require to redisplay the whole paragraph. It might be worthwhile
11728 to find the paragraph limits and widen the range of redisplayed
11729 lines to that, but for now just give up this optimization. */
11730 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11731 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11732 unchanged_p = 0;
11733 }
11734
11735 return unchanged_p;
11736 }
11737
11738
11739 /* Do a frame update, taking possible shortcuts into account. This is
11740 the main external entry point for redisplay.
11741
11742 If the last redisplay displayed an echo area message and that message
11743 is no longer requested, we clear the echo area or bring back the
11744 mini-buffer if that is in use. */
11745
11746 void
11747 redisplay (void)
11748 {
11749 redisplay_internal ();
11750 }
11751
11752
11753 static Lisp_Object
11754 overlay_arrow_string_or_property (Lisp_Object var)
11755 {
11756 Lisp_Object val;
11757
11758 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11759 return val;
11760
11761 return Voverlay_arrow_string;
11762 }
11763
11764 /* Return 1 if there are any overlay-arrows in current_buffer. */
11765 static int
11766 overlay_arrow_in_current_buffer_p (void)
11767 {
11768 Lisp_Object vlist;
11769
11770 for (vlist = Voverlay_arrow_variable_list;
11771 CONSP (vlist);
11772 vlist = XCDR (vlist))
11773 {
11774 Lisp_Object var = XCAR (vlist);
11775 Lisp_Object val;
11776
11777 if (!SYMBOLP (var))
11778 continue;
11779 val = find_symbol_value (var);
11780 if (MARKERP (val)
11781 && current_buffer == XMARKER (val)->buffer)
11782 return 1;
11783 }
11784 return 0;
11785 }
11786
11787
11788 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11789 has changed. */
11790
11791 static int
11792 overlay_arrows_changed_p (void)
11793 {
11794 Lisp_Object vlist;
11795
11796 for (vlist = Voverlay_arrow_variable_list;
11797 CONSP (vlist);
11798 vlist = XCDR (vlist))
11799 {
11800 Lisp_Object var = XCAR (vlist);
11801 Lisp_Object val, pstr;
11802
11803 if (!SYMBOLP (var))
11804 continue;
11805 val = find_symbol_value (var);
11806 if (!MARKERP (val))
11807 continue;
11808 if (! EQ (COERCE_MARKER (val),
11809 Fget (var, Qlast_arrow_position))
11810 || ! (pstr = overlay_arrow_string_or_property (var),
11811 EQ (pstr, Fget (var, Qlast_arrow_string))))
11812 return 1;
11813 }
11814 return 0;
11815 }
11816
11817 /* Mark overlay arrows to be updated on next redisplay. */
11818
11819 static void
11820 update_overlay_arrows (int up_to_date)
11821 {
11822 Lisp_Object vlist;
11823
11824 for (vlist = Voverlay_arrow_variable_list;
11825 CONSP (vlist);
11826 vlist = XCDR (vlist))
11827 {
11828 Lisp_Object var = XCAR (vlist);
11829
11830 if (!SYMBOLP (var))
11831 continue;
11832
11833 if (up_to_date > 0)
11834 {
11835 Lisp_Object val = find_symbol_value (var);
11836 Fput (var, Qlast_arrow_position,
11837 COERCE_MARKER (val));
11838 Fput (var, Qlast_arrow_string,
11839 overlay_arrow_string_or_property (var));
11840 }
11841 else if (up_to_date < 0
11842 || !NILP (Fget (var, Qlast_arrow_position)))
11843 {
11844 Fput (var, Qlast_arrow_position, Qt);
11845 Fput (var, Qlast_arrow_string, Qt);
11846 }
11847 }
11848 }
11849
11850
11851 /* Return overlay arrow string to display at row.
11852 Return integer (bitmap number) for arrow bitmap in left fringe.
11853 Return nil if no overlay arrow. */
11854
11855 static Lisp_Object
11856 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11857 {
11858 Lisp_Object vlist;
11859
11860 for (vlist = Voverlay_arrow_variable_list;
11861 CONSP (vlist);
11862 vlist = XCDR (vlist))
11863 {
11864 Lisp_Object var = XCAR (vlist);
11865 Lisp_Object val;
11866
11867 if (!SYMBOLP (var))
11868 continue;
11869
11870 val = find_symbol_value (var);
11871
11872 if (MARKERP (val)
11873 && current_buffer == XMARKER (val)->buffer
11874 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11875 {
11876 if (FRAME_WINDOW_P (it->f)
11877 /* FIXME: if ROW->reversed_p is set, this should test
11878 the right fringe, not the left one. */
11879 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11880 {
11881 #ifdef HAVE_WINDOW_SYSTEM
11882 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11883 {
11884 int fringe_bitmap;
11885 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11886 return make_number (fringe_bitmap);
11887 }
11888 #endif
11889 return make_number (-1); /* Use default arrow bitmap */
11890 }
11891 return overlay_arrow_string_or_property (var);
11892 }
11893 }
11894
11895 return Qnil;
11896 }
11897
11898 /* Return 1 if point moved out of or into a composition. Otherwise
11899 return 0. PREV_BUF and PREV_PT are the last point buffer and
11900 position. BUF and PT are the current point buffer and position. */
11901
11902 static int
11903 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
11904 struct buffer *buf, EMACS_INT pt)
11905 {
11906 EMACS_INT start, end;
11907 Lisp_Object prop;
11908 Lisp_Object buffer;
11909
11910 XSETBUFFER (buffer, buf);
11911 /* Check a composition at the last point if point moved within the
11912 same buffer. */
11913 if (prev_buf == buf)
11914 {
11915 if (prev_pt == pt)
11916 /* Point didn't move. */
11917 return 0;
11918
11919 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11920 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11921 && COMPOSITION_VALID_P (start, end, prop)
11922 && start < prev_pt && end > prev_pt)
11923 /* The last point was within the composition. Return 1 iff
11924 point moved out of the composition. */
11925 return (pt <= start || pt >= end);
11926 }
11927
11928 /* Check a composition at the current point. */
11929 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11930 && find_composition (pt, -1, &start, &end, &prop, buffer)
11931 && COMPOSITION_VALID_P (start, end, prop)
11932 && start < pt && end > pt);
11933 }
11934
11935
11936 /* Reconsider the setting of B->clip_changed which is displayed
11937 in window W. */
11938
11939 static INLINE void
11940 reconsider_clip_changes (struct window *w, struct buffer *b)
11941 {
11942 if (b->clip_changed
11943 && !NILP (w->window_end_valid)
11944 && w->current_matrix->buffer == b
11945 && w->current_matrix->zv == BUF_ZV (b)
11946 && w->current_matrix->begv == BUF_BEGV (b))
11947 b->clip_changed = 0;
11948
11949 /* If display wasn't paused, and W is not a tool bar window, see if
11950 point has been moved into or out of a composition. In that case,
11951 we set b->clip_changed to 1 to force updating the screen. If
11952 b->clip_changed has already been set to 1, we can skip this
11953 check. */
11954 if (!b->clip_changed
11955 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11956 {
11957 EMACS_INT pt;
11958
11959 if (w == XWINDOW (selected_window))
11960 pt = PT;
11961 else
11962 pt = marker_position (w->pointm);
11963
11964 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11965 || pt != XINT (w->last_point))
11966 && check_point_in_composition (w->current_matrix->buffer,
11967 XINT (w->last_point),
11968 XBUFFER (w->buffer), pt))
11969 b->clip_changed = 1;
11970 }
11971 }
11972 \f
11973
11974 /* Select FRAME to forward the values of frame-local variables into C
11975 variables so that the redisplay routines can access those values
11976 directly. */
11977
11978 static void
11979 select_frame_for_redisplay (Lisp_Object frame)
11980 {
11981 Lisp_Object tail, tem;
11982 Lisp_Object old = selected_frame;
11983 struct Lisp_Symbol *sym;
11984
11985 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11986
11987 selected_frame = frame;
11988
11989 do {
11990 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11991 if (CONSP (XCAR (tail))
11992 && (tem = XCAR (XCAR (tail)),
11993 SYMBOLP (tem))
11994 && (sym = indirect_variable (XSYMBOL (tem)),
11995 sym->redirect == SYMBOL_LOCALIZED)
11996 && sym->val.blv->frame_local)
11997 /* Use find_symbol_value rather than Fsymbol_value
11998 to avoid an error if it is void. */
11999 find_symbol_value (tem);
12000 } while (!EQ (frame, old) && (frame = old, 1));
12001 }
12002
12003
12004 #define STOP_POLLING \
12005 do { if (! polling_stopped_here) stop_polling (); \
12006 polling_stopped_here = 1; } while (0)
12007
12008 #define RESUME_POLLING \
12009 do { if (polling_stopped_here) start_polling (); \
12010 polling_stopped_here = 0; } while (0)
12011
12012
12013 /* Perhaps in the future avoid recentering windows if it
12014 is not necessary; currently that causes some problems. */
12015
12016 static void
12017 redisplay_internal (void)
12018 {
12019 struct window *w = XWINDOW (selected_window);
12020 struct window *sw;
12021 struct frame *fr;
12022 int pending;
12023 int must_finish = 0;
12024 struct text_pos tlbufpos, tlendpos;
12025 int number_of_visible_frames;
12026 int count, count1;
12027 struct frame *sf;
12028 int polling_stopped_here = 0;
12029 Lisp_Object old_frame = selected_frame;
12030
12031 /* Non-zero means redisplay has to consider all windows on all
12032 frames. Zero means, only selected_window is considered. */
12033 int consider_all_windows_p;
12034
12035 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12036
12037 /* No redisplay if running in batch mode or frame is not yet fully
12038 initialized, or redisplay is explicitly turned off by setting
12039 Vinhibit_redisplay. */
12040 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12041 || !NILP (Vinhibit_redisplay))
12042 return;
12043
12044 /* Don't examine these until after testing Vinhibit_redisplay.
12045 When Emacs is shutting down, perhaps because its connection to
12046 X has dropped, we should not look at them at all. */
12047 fr = XFRAME (w->frame);
12048 sf = SELECTED_FRAME ();
12049
12050 if (!fr->glyphs_initialized_p)
12051 return;
12052
12053 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12054 if (popup_activated ())
12055 return;
12056 #endif
12057
12058 /* I don't think this happens but let's be paranoid. */
12059 if (redisplaying_p)
12060 return;
12061
12062 /* Record a function that resets redisplaying_p to its old value
12063 when we leave this function. */
12064 count = SPECPDL_INDEX ();
12065 record_unwind_protect (unwind_redisplay,
12066 Fcons (make_number (redisplaying_p), selected_frame));
12067 ++redisplaying_p;
12068 specbind (Qinhibit_free_realized_faces, Qnil);
12069
12070 {
12071 Lisp_Object tail, frame;
12072
12073 FOR_EACH_FRAME (tail, frame)
12074 {
12075 struct frame *f = XFRAME (frame);
12076 f->already_hscrolled_p = 0;
12077 }
12078 }
12079
12080 retry:
12081 /* Remember the currently selected window. */
12082 sw = w;
12083
12084 if (!EQ (old_frame, selected_frame)
12085 && FRAME_LIVE_P (XFRAME (old_frame)))
12086 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12087 selected_frame and selected_window to be temporarily out-of-sync so
12088 when we come back here via `goto retry', we need to resync because we
12089 may need to run Elisp code (via prepare_menu_bars). */
12090 select_frame_for_redisplay (old_frame);
12091
12092 pending = 0;
12093 reconsider_clip_changes (w, current_buffer);
12094 last_escape_glyph_frame = NULL;
12095 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12096 last_glyphless_glyph_frame = NULL;
12097 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12098
12099 /* If new fonts have been loaded that make a glyph matrix adjustment
12100 necessary, do it. */
12101 if (fonts_changed_p)
12102 {
12103 adjust_glyphs (NULL);
12104 ++windows_or_buffers_changed;
12105 fonts_changed_p = 0;
12106 }
12107
12108 /* If face_change_count is non-zero, init_iterator will free all
12109 realized faces, which includes the faces referenced from current
12110 matrices. So, we can't reuse current matrices in this case. */
12111 if (face_change_count)
12112 ++windows_or_buffers_changed;
12113
12114 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12115 && FRAME_TTY (sf)->previous_frame != sf)
12116 {
12117 /* Since frames on a single ASCII terminal share the same
12118 display area, displaying a different frame means redisplay
12119 the whole thing. */
12120 windows_or_buffers_changed++;
12121 SET_FRAME_GARBAGED (sf);
12122 #ifndef DOS_NT
12123 set_tty_color_mode (FRAME_TTY (sf), sf);
12124 #endif
12125 FRAME_TTY (sf)->previous_frame = sf;
12126 }
12127
12128 /* Set the visible flags for all frames. Do this before checking
12129 for resized or garbaged frames; they want to know if their frames
12130 are visible. See the comment in frame.h for
12131 FRAME_SAMPLE_VISIBILITY. */
12132 {
12133 Lisp_Object tail, frame;
12134
12135 number_of_visible_frames = 0;
12136
12137 FOR_EACH_FRAME (tail, frame)
12138 {
12139 struct frame *f = XFRAME (frame);
12140
12141 FRAME_SAMPLE_VISIBILITY (f);
12142 if (FRAME_VISIBLE_P (f))
12143 ++number_of_visible_frames;
12144 clear_desired_matrices (f);
12145 }
12146 }
12147
12148 /* Notice any pending interrupt request to change frame size. */
12149 do_pending_window_change (1);
12150
12151 /* do_pending_window_change could change the selected_window due to
12152 frame resizing which makes the selected window too small. */
12153 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12154 {
12155 sw = w;
12156 reconsider_clip_changes (w, current_buffer);
12157 }
12158
12159 /* Clear frames marked as garbaged. */
12160 if (frame_garbaged)
12161 clear_garbaged_frames ();
12162
12163 /* Build menubar and tool-bar items. */
12164 if (NILP (Vmemory_full))
12165 prepare_menu_bars ();
12166
12167 if (windows_or_buffers_changed)
12168 update_mode_lines++;
12169
12170 /* Detect case that we need to write or remove a star in the mode line. */
12171 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12172 {
12173 w->update_mode_line = Qt;
12174 if (buffer_shared > 1)
12175 update_mode_lines++;
12176 }
12177
12178 /* Avoid invocation of point motion hooks by `current_column' below. */
12179 count1 = SPECPDL_INDEX ();
12180 specbind (Qinhibit_point_motion_hooks, Qt);
12181
12182 /* If %c is in the mode line, update it if needed. */
12183 if (!NILP (w->column_number_displayed)
12184 /* This alternative quickly identifies a common case
12185 where no change is needed. */
12186 && !(PT == XFASTINT (w->last_point)
12187 && XFASTINT (w->last_modified) >= MODIFF
12188 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12189 && (XFASTINT (w->column_number_displayed) != current_column ()))
12190 w->update_mode_line = Qt;
12191
12192 unbind_to (count1, Qnil);
12193
12194 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12195
12196 /* The variable buffer_shared is set in redisplay_window and
12197 indicates that we redisplay a buffer in different windows. See
12198 there. */
12199 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12200 || cursor_type_changed);
12201
12202 /* If specs for an arrow have changed, do thorough redisplay
12203 to ensure we remove any arrow that should no longer exist. */
12204 if (overlay_arrows_changed_p ())
12205 consider_all_windows_p = windows_or_buffers_changed = 1;
12206
12207 /* Normally the message* functions will have already displayed and
12208 updated the echo area, but the frame may have been trashed, or
12209 the update may have been preempted, so display the echo area
12210 again here. Checking message_cleared_p captures the case that
12211 the echo area should be cleared. */
12212 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12213 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12214 || (message_cleared_p
12215 && minibuf_level == 0
12216 /* If the mini-window is currently selected, this means the
12217 echo-area doesn't show through. */
12218 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12219 {
12220 int window_height_changed_p = echo_area_display (0);
12221 must_finish = 1;
12222
12223 /* If we don't display the current message, don't clear the
12224 message_cleared_p flag, because, if we did, we wouldn't clear
12225 the echo area in the next redisplay which doesn't preserve
12226 the echo area. */
12227 if (!display_last_displayed_message_p)
12228 message_cleared_p = 0;
12229
12230 if (fonts_changed_p)
12231 goto retry;
12232 else if (window_height_changed_p)
12233 {
12234 consider_all_windows_p = 1;
12235 ++update_mode_lines;
12236 ++windows_or_buffers_changed;
12237
12238 /* If window configuration was changed, frames may have been
12239 marked garbaged. Clear them or we will experience
12240 surprises wrt scrolling. */
12241 if (frame_garbaged)
12242 clear_garbaged_frames ();
12243 }
12244 }
12245 else if (EQ (selected_window, minibuf_window)
12246 && (current_buffer->clip_changed
12247 || XFASTINT (w->last_modified) < MODIFF
12248 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12249 && resize_mini_window (w, 0))
12250 {
12251 /* Resized active mini-window to fit the size of what it is
12252 showing if its contents might have changed. */
12253 must_finish = 1;
12254 /* FIXME: this causes all frames to be updated, which seems unnecessary
12255 since only the current frame needs to be considered. This function needs
12256 to be rewritten with two variables, consider_all_windows and
12257 consider_all_frames. */
12258 consider_all_windows_p = 1;
12259 ++windows_or_buffers_changed;
12260 ++update_mode_lines;
12261
12262 /* If window configuration was changed, frames may have been
12263 marked garbaged. Clear them or we will experience
12264 surprises wrt scrolling. */
12265 if (frame_garbaged)
12266 clear_garbaged_frames ();
12267 }
12268
12269
12270 /* If showing the region, and mark has changed, we must redisplay
12271 the whole window. The assignment to this_line_start_pos prevents
12272 the optimization directly below this if-statement. */
12273 if (((!NILP (Vtransient_mark_mode)
12274 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12275 != !NILP (w->region_showing))
12276 || (!NILP (w->region_showing)
12277 && !EQ (w->region_showing,
12278 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12279 CHARPOS (this_line_start_pos) = 0;
12280
12281 /* Optimize the case that only the line containing the cursor in the
12282 selected window has changed. Variables starting with this_ are
12283 set in display_line and record information about the line
12284 containing the cursor. */
12285 tlbufpos = this_line_start_pos;
12286 tlendpos = this_line_end_pos;
12287 if (!consider_all_windows_p
12288 && CHARPOS (tlbufpos) > 0
12289 && NILP (w->update_mode_line)
12290 && !current_buffer->clip_changed
12291 && !current_buffer->prevent_redisplay_optimizations_p
12292 && FRAME_VISIBLE_P (XFRAME (w->frame))
12293 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12294 /* Make sure recorded data applies to current buffer, etc. */
12295 && this_line_buffer == current_buffer
12296 && current_buffer == XBUFFER (w->buffer)
12297 && NILP (w->force_start)
12298 && NILP (w->optional_new_start)
12299 /* Point must be on the line that we have info recorded about. */
12300 && PT >= CHARPOS (tlbufpos)
12301 && PT <= Z - CHARPOS (tlendpos)
12302 /* All text outside that line, including its final newline,
12303 must be unchanged. */
12304 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12305 CHARPOS (tlendpos)))
12306 {
12307 if (CHARPOS (tlbufpos) > BEGV
12308 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12309 && (CHARPOS (tlbufpos) == ZV
12310 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12311 /* Former continuation line has disappeared by becoming empty. */
12312 goto cancel;
12313 else if (XFASTINT (w->last_modified) < MODIFF
12314 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12315 || MINI_WINDOW_P (w))
12316 {
12317 /* We have to handle the case of continuation around a
12318 wide-column character (see the comment in indent.c around
12319 line 1340).
12320
12321 For instance, in the following case:
12322
12323 -------- Insert --------
12324 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12325 J_I_ ==> J_I_ `^^' are cursors.
12326 ^^ ^^
12327 -------- --------
12328
12329 As we have to redraw the line above, we cannot use this
12330 optimization. */
12331
12332 struct it it;
12333 int line_height_before = this_line_pixel_height;
12334
12335 /* Note that start_display will handle the case that the
12336 line starting at tlbufpos is a continuation line. */
12337 start_display (&it, w, tlbufpos);
12338
12339 /* Implementation note: It this still necessary? */
12340 if (it.current_x != this_line_start_x)
12341 goto cancel;
12342
12343 TRACE ((stderr, "trying display optimization 1\n"));
12344 w->cursor.vpos = -1;
12345 overlay_arrow_seen = 0;
12346 it.vpos = this_line_vpos;
12347 it.current_y = this_line_y;
12348 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12349 display_line (&it);
12350
12351 /* If line contains point, is not continued,
12352 and ends at same distance from eob as before, we win. */
12353 if (w->cursor.vpos >= 0
12354 /* Line is not continued, otherwise this_line_start_pos
12355 would have been set to 0 in display_line. */
12356 && CHARPOS (this_line_start_pos)
12357 /* Line ends as before. */
12358 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12359 /* Line has same height as before. Otherwise other lines
12360 would have to be shifted up or down. */
12361 && this_line_pixel_height == line_height_before)
12362 {
12363 /* If this is not the window's last line, we must adjust
12364 the charstarts of the lines below. */
12365 if (it.current_y < it.last_visible_y)
12366 {
12367 struct glyph_row *row
12368 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12369 EMACS_INT delta, delta_bytes;
12370
12371 /* We used to distinguish between two cases here,
12372 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12373 when the line ends in a newline or the end of the
12374 buffer's accessible portion. But both cases did
12375 the same, so they were collapsed. */
12376 delta = (Z
12377 - CHARPOS (tlendpos)
12378 - MATRIX_ROW_START_CHARPOS (row));
12379 delta_bytes = (Z_BYTE
12380 - BYTEPOS (tlendpos)
12381 - MATRIX_ROW_START_BYTEPOS (row));
12382
12383 increment_matrix_positions (w->current_matrix,
12384 this_line_vpos + 1,
12385 w->current_matrix->nrows,
12386 delta, delta_bytes);
12387 }
12388
12389 /* If this row displays text now but previously didn't,
12390 or vice versa, w->window_end_vpos may have to be
12391 adjusted. */
12392 if ((it.glyph_row - 1)->displays_text_p)
12393 {
12394 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12395 XSETINT (w->window_end_vpos, this_line_vpos);
12396 }
12397 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12398 && this_line_vpos > 0)
12399 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12400 w->window_end_valid = Qnil;
12401
12402 /* Update hint: No need to try to scroll in update_window. */
12403 w->desired_matrix->no_scrolling_p = 1;
12404
12405 #if GLYPH_DEBUG
12406 *w->desired_matrix->method = 0;
12407 debug_method_add (w, "optimization 1");
12408 #endif
12409 #ifdef HAVE_WINDOW_SYSTEM
12410 update_window_fringes (w, 0);
12411 #endif
12412 goto update;
12413 }
12414 else
12415 goto cancel;
12416 }
12417 else if (/* Cursor position hasn't changed. */
12418 PT == XFASTINT (w->last_point)
12419 /* Make sure the cursor was last displayed
12420 in this window. Otherwise we have to reposition it. */
12421 && 0 <= w->cursor.vpos
12422 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12423 {
12424 if (!must_finish)
12425 {
12426 do_pending_window_change (1);
12427 /* If selected_window changed, redisplay again. */
12428 if (WINDOWP (selected_window)
12429 && (w = XWINDOW (selected_window)) != sw)
12430 goto retry;
12431
12432 /* We used to always goto end_of_redisplay here, but this
12433 isn't enough if we have a blinking cursor. */
12434 if (w->cursor_off_p == w->last_cursor_off_p)
12435 goto end_of_redisplay;
12436 }
12437 goto update;
12438 }
12439 /* If highlighting the region, or if the cursor is in the echo area,
12440 then we can't just move the cursor. */
12441 else if (! (!NILP (Vtransient_mark_mode)
12442 && !NILP (BVAR (current_buffer, mark_active)))
12443 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12444 || highlight_nonselected_windows)
12445 && NILP (w->region_showing)
12446 && NILP (Vshow_trailing_whitespace)
12447 && !cursor_in_echo_area)
12448 {
12449 struct it it;
12450 struct glyph_row *row;
12451
12452 /* Skip from tlbufpos to PT and see where it is. Note that
12453 PT may be in invisible text. If so, we will end at the
12454 next visible position. */
12455 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12456 NULL, DEFAULT_FACE_ID);
12457 it.current_x = this_line_start_x;
12458 it.current_y = this_line_y;
12459 it.vpos = this_line_vpos;
12460
12461 /* The call to move_it_to stops in front of PT, but
12462 moves over before-strings. */
12463 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12464
12465 if (it.vpos == this_line_vpos
12466 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12467 row->enabled_p))
12468 {
12469 xassert (this_line_vpos == it.vpos);
12470 xassert (this_line_y == it.current_y);
12471 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12472 #if GLYPH_DEBUG
12473 *w->desired_matrix->method = 0;
12474 debug_method_add (w, "optimization 3");
12475 #endif
12476 goto update;
12477 }
12478 else
12479 goto cancel;
12480 }
12481
12482 cancel:
12483 /* Text changed drastically or point moved off of line. */
12484 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12485 }
12486
12487 CHARPOS (this_line_start_pos) = 0;
12488 consider_all_windows_p |= buffer_shared > 1;
12489 ++clear_face_cache_count;
12490 #ifdef HAVE_WINDOW_SYSTEM
12491 ++clear_image_cache_count;
12492 #endif
12493
12494 /* Build desired matrices, and update the display. If
12495 consider_all_windows_p is non-zero, do it for all windows on all
12496 frames. Otherwise do it for selected_window, only. */
12497
12498 if (consider_all_windows_p)
12499 {
12500 Lisp_Object tail, frame;
12501
12502 FOR_EACH_FRAME (tail, frame)
12503 XFRAME (frame)->updated_p = 0;
12504
12505 /* Recompute # windows showing selected buffer. This will be
12506 incremented each time such a window is displayed. */
12507 buffer_shared = 0;
12508
12509 FOR_EACH_FRAME (tail, frame)
12510 {
12511 struct frame *f = XFRAME (frame);
12512
12513 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12514 {
12515 if (! EQ (frame, selected_frame))
12516 /* Select the frame, for the sake of frame-local
12517 variables. */
12518 select_frame_for_redisplay (frame);
12519
12520 /* Mark all the scroll bars to be removed; we'll redeem
12521 the ones we want when we redisplay their windows. */
12522 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12523 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12524
12525 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12526 redisplay_windows (FRAME_ROOT_WINDOW (f));
12527
12528 /* The X error handler may have deleted that frame. */
12529 if (!FRAME_LIVE_P (f))
12530 continue;
12531
12532 /* Any scroll bars which redisplay_windows should have
12533 nuked should now go away. */
12534 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12535 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12536
12537 /* If fonts changed, display again. */
12538 /* ??? rms: I suspect it is a mistake to jump all the way
12539 back to retry here. It should just retry this frame. */
12540 if (fonts_changed_p)
12541 goto retry;
12542
12543 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12544 {
12545 /* See if we have to hscroll. */
12546 if (!f->already_hscrolled_p)
12547 {
12548 f->already_hscrolled_p = 1;
12549 if (hscroll_windows (f->root_window))
12550 goto retry;
12551 }
12552
12553 /* Prevent various kinds of signals during display
12554 update. stdio is not robust about handling
12555 signals, which can cause an apparent I/O
12556 error. */
12557 if (interrupt_input)
12558 unrequest_sigio ();
12559 STOP_POLLING;
12560
12561 /* Update the display. */
12562 set_window_update_flags (XWINDOW (f->root_window), 1);
12563 pending |= update_frame (f, 0, 0);
12564 f->updated_p = 1;
12565 }
12566 }
12567 }
12568
12569 if (!EQ (old_frame, selected_frame)
12570 && FRAME_LIVE_P (XFRAME (old_frame)))
12571 /* We played a bit fast-and-loose above and allowed selected_frame
12572 and selected_window to be temporarily out-of-sync but let's make
12573 sure this stays contained. */
12574 select_frame_for_redisplay (old_frame);
12575 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12576
12577 if (!pending)
12578 {
12579 /* Do the mark_window_display_accurate after all windows have
12580 been redisplayed because this call resets flags in buffers
12581 which are needed for proper redisplay. */
12582 FOR_EACH_FRAME (tail, frame)
12583 {
12584 struct frame *f = XFRAME (frame);
12585 if (f->updated_p)
12586 {
12587 mark_window_display_accurate (f->root_window, 1);
12588 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12589 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12590 }
12591 }
12592 }
12593 }
12594 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12595 {
12596 Lisp_Object mini_window;
12597 struct frame *mini_frame;
12598
12599 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12600 /* Use list_of_error, not Qerror, so that
12601 we catch only errors and don't run the debugger. */
12602 internal_condition_case_1 (redisplay_window_1, selected_window,
12603 list_of_error,
12604 redisplay_window_error);
12605
12606 /* Compare desired and current matrices, perform output. */
12607
12608 update:
12609 /* If fonts changed, display again. */
12610 if (fonts_changed_p)
12611 goto retry;
12612
12613 /* Prevent various kinds of signals during display update.
12614 stdio is not robust about handling signals,
12615 which can cause an apparent I/O error. */
12616 if (interrupt_input)
12617 unrequest_sigio ();
12618 STOP_POLLING;
12619
12620 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12621 {
12622 if (hscroll_windows (selected_window))
12623 goto retry;
12624
12625 XWINDOW (selected_window)->must_be_updated_p = 1;
12626 pending = update_frame (sf, 0, 0);
12627 }
12628
12629 /* We may have called echo_area_display at the top of this
12630 function. If the echo area is on another frame, that may
12631 have put text on a frame other than the selected one, so the
12632 above call to update_frame would not have caught it. Catch
12633 it here. */
12634 mini_window = FRAME_MINIBUF_WINDOW (sf);
12635 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12636
12637 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12638 {
12639 XWINDOW (mini_window)->must_be_updated_p = 1;
12640 pending |= update_frame (mini_frame, 0, 0);
12641 if (!pending && hscroll_windows (mini_window))
12642 goto retry;
12643 }
12644 }
12645
12646 /* If display was paused because of pending input, make sure we do a
12647 thorough update the next time. */
12648 if (pending)
12649 {
12650 /* Prevent the optimization at the beginning of
12651 redisplay_internal that tries a single-line update of the
12652 line containing the cursor in the selected window. */
12653 CHARPOS (this_line_start_pos) = 0;
12654
12655 /* Let the overlay arrow be updated the next time. */
12656 update_overlay_arrows (0);
12657
12658 /* If we pause after scrolling, some rows in the current
12659 matrices of some windows are not valid. */
12660 if (!WINDOW_FULL_WIDTH_P (w)
12661 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12662 update_mode_lines = 1;
12663 }
12664 else
12665 {
12666 if (!consider_all_windows_p)
12667 {
12668 /* This has already been done above if
12669 consider_all_windows_p is set. */
12670 mark_window_display_accurate_1 (w, 1);
12671
12672 /* Say overlay arrows are up to date. */
12673 update_overlay_arrows (1);
12674
12675 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12676 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12677 }
12678
12679 update_mode_lines = 0;
12680 windows_or_buffers_changed = 0;
12681 cursor_type_changed = 0;
12682 }
12683
12684 /* Start SIGIO interrupts coming again. Having them off during the
12685 code above makes it less likely one will discard output, but not
12686 impossible, since there might be stuff in the system buffer here.
12687 But it is much hairier to try to do anything about that. */
12688 if (interrupt_input)
12689 request_sigio ();
12690 RESUME_POLLING;
12691
12692 /* If a frame has become visible which was not before, redisplay
12693 again, so that we display it. Expose events for such a frame
12694 (which it gets when becoming visible) don't call the parts of
12695 redisplay constructing glyphs, so simply exposing a frame won't
12696 display anything in this case. So, we have to display these
12697 frames here explicitly. */
12698 if (!pending)
12699 {
12700 Lisp_Object tail, frame;
12701 int new_count = 0;
12702
12703 FOR_EACH_FRAME (tail, frame)
12704 {
12705 int this_is_visible = 0;
12706
12707 if (XFRAME (frame)->visible)
12708 this_is_visible = 1;
12709 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12710 if (XFRAME (frame)->visible)
12711 this_is_visible = 1;
12712
12713 if (this_is_visible)
12714 new_count++;
12715 }
12716
12717 if (new_count != number_of_visible_frames)
12718 windows_or_buffers_changed++;
12719 }
12720
12721 /* Change frame size now if a change is pending. */
12722 do_pending_window_change (1);
12723
12724 /* If we just did a pending size change, or have additional
12725 visible frames, or selected_window changed, redisplay again. */
12726 if ((windows_or_buffers_changed && !pending)
12727 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12728 goto retry;
12729
12730 /* Clear the face and image caches.
12731
12732 We used to do this only if consider_all_windows_p. But the cache
12733 needs to be cleared if a timer creates images in the current
12734 buffer (e.g. the test case in Bug#6230). */
12735
12736 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12737 {
12738 clear_face_cache (0);
12739 clear_face_cache_count = 0;
12740 }
12741
12742 #ifdef HAVE_WINDOW_SYSTEM
12743 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12744 {
12745 clear_image_caches (Qnil);
12746 clear_image_cache_count = 0;
12747 }
12748 #endif /* HAVE_WINDOW_SYSTEM */
12749
12750 end_of_redisplay:
12751 unbind_to (count, Qnil);
12752 RESUME_POLLING;
12753 }
12754
12755
12756 /* Redisplay, but leave alone any recent echo area message unless
12757 another message has been requested in its place.
12758
12759 This is useful in situations where you need to redisplay but no
12760 user action has occurred, making it inappropriate for the message
12761 area to be cleared. See tracking_off and
12762 wait_reading_process_output for examples of these situations.
12763
12764 FROM_WHERE is an integer saying from where this function was
12765 called. This is useful for debugging. */
12766
12767 void
12768 redisplay_preserve_echo_area (int from_where)
12769 {
12770 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12771
12772 if (!NILP (echo_area_buffer[1]))
12773 {
12774 /* We have a previously displayed message, but no current
12775 message. Redisplay the previous message. */
12776 display_last_displayed_message_p = 1;
12777 redisplay_internal ();
12778 display_last_displayed_message_p = 0;
12779 }
12780 else
12781 redisplay_internal ();
12782
12783 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12784 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12785 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12786 }
12787
12788
12789 /* Function registered with record_unwind_protect in
12790 redisplay_internal. Reset redisplaying_p to the value it had
12791 before redisplay_internal was called, and clear
12792 prevent_freeing_realized_faces_p. It also selects the previously
12793 selected frame, unless it has been deleted (by an X connection
12794 failure during redisplay, for example). */
12795
12796 static Lisp_Object
12797 unwind_redisplay (Lisp_Object val)
12798 {
12799 Lisp_Object old_redisplaying_p, old_frame;
12800
12801 old_redisplaying_p = XCAR (val);
12802 redisplaying_p = XFASTINT (old_redisplaying_p);
12803 old_frame = XCDR (val);
12804 if (! EQ (old_frame, selected_frame)
12805 && FRAME_LIVE_P (XFRAME (old_frame)))
12806 select_frame_for_redisplay (old_frame);
12807 return Qnil;
12808 }
12809
12810
12811 /* Mark the display of window W as accurate or inaccurate. If
12812 ACCURATE_P is non-zero mark display of W as accurate. If
12813 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12814 redisplay_internal is called. */
12815
12816 static void
12817 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12818 {
12819 if (BUFFERP (w->buffer))
12820 {
12821 struct buffer *b = XBUFFER (w->buffer);
12822
12823 w->last_modified
12824 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12825 w->last_overlay_modified
12826 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12827 w->last_had_star
12828 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12829
12830 if (accurate_p)
12831 {
12832 b->clip_changed = 0;
12833 b->prevent_redisplay_optimizations_p = 0;
12834
12835 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12836 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12837 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12838 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12839
12840 w->current_matrix->buffer = b;
12841 w->current_matrix->begv = BUF_BEGV (b);
12842 w->current_matrix->zv = BUF_ZV (b);
12843
12844 w->last_cursor = w->cursor;
12845 w->last_cursor_off_p = w->cursor_off_p;
12846
12847 if (w == XWINDOW (selected_window))
12848 w->last_point = make_number (BUF_PT (b));
12849 else
12850 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12851 }
12852 }
12853
12854 if (accurate_p)
12855 {
12856 w->window_end_valid = w->buffer;
12857 w->update_mode_line = Qnil;
12858 }
12859 }
12860
12861
12862 /* Mark the display of windows in the window tree rooted at WINDOW as
12863 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12864 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12865 be redisplayed the next time redisplay_internal is called. */
12866
12867 void
12868 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12869 {
12870 struct window *w;
12871
12872 for (; !NILP (window); window = w->next)
12873 {
12874 w = XWINDOW (window);
12875 mark_window_display_accurate_1 (w, accurate_p);
12876
12877 if (!NILP (w->vchild))
12878 mark_window_display_accurate (w->vchild, accurate_p);
12879 if (!NILP (w->hchild))
12880 mark_window_display_accurate (w->hchild, accurate_p);
12881 }
12882
12883 if (accurate_p)
12884 {
12885 update_overlay_arrows (1);
12886 }
12887 else
12888 {
12889 /* Force a thorough redisplay the next time by setting
12890 last_arrow_position and last_arrow_string to t, which is
12891 unequal to any useful value of Voverlay_arrow_... */
12892 update_overlay_arrows (-1);
12893 }
12894 }
12895
12896
12897 /* Return value in display table DP (Lisp_Char_Table *) for character
12898 C. Since a display table doesn't have any parent, we don't have to
12899 follow parent. Do not call this function directly but use the
12900 macro DISP_CHAR_VECTOR. */
12901
12902 Lisp_Object
12903 disp_char_vector (struct Lisp_Char_Table *dp, int c)
12904 {
12905 Lisp_Object val;
12906
12907 if (ASCII_CHAR_P (c))
12908 {
12909 val = dp->ascii;
12910 if (SUB_CHAR_TABLE_P (val))
12911 val = XSUB_CHAR_TABLE (val)->contents[c];
12912 }
12913 else
12914 {
12915 Lisp_Object table;
12916
12917 XSETCHAR_TABLE (table, dp);
12918 val = char_table_ref (table, c);
12919 }
12920 if (NILP (val))
12921 val = dp->defalt;
12922 return val;
12923 }
12924
12925
12926 \f
12927 /***********************************************************************
12928 Window Redisplay
12929 ***********************************************************************/
12930
12931 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12932
12933 static void
12934 redisplay_windows (Lisp_Object window)
12935 {
12936 while (!NILP (window))
12937 {
12938 struct window *w = XWINDOW (window);
12939
12940 if (!NILP (w->hchild))
12941 redisplay_windows (w->hchild);
12942 else if (!NILP (w->vchild))
12943 redisplay_windows (w->vchild);
12944 else if (!NILP (w->buffer))
12945 {
12946 displayed_buffer = XBUFFER (w->buffer);
12947 /* Use list_of_error, not Qerror, so that
12948 we catch only errors and don't run the debugger. */
12949 internal_condition_case_1 (redisplay_window_0, window,
12950 list_of_error,
12951 redisplay_window_error);
12952 }
12953
12954 window = w->next;
12955 }
12956 }
12957
12958 static Lisp_Object
12959 redisplay_window_error (Lisp_Object ignore)
12960 {
12961 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12962 return Qnil;
12963 }
12964
12965 static Lisp_Object
12966 redisplay_window_0 (Lisp_Object window)
12967 {
12968 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12969 redisplay_window (window, 0);
12970 return Qnil;
12971 }
12972
12973 static Lisp_Object
12974 redisplay_window_1 (Lisp_Object window)
12975 {
12976 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12977 redisplay_window (window, 1);
12978 return Qnil;
12979 }
12980 \f
12981
12982 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12983 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12984 which positions recorded in ROW differ from current buffer
12985 positions.
12986
12987 Return 0 if cursor is not on this row, 1 otherwise. */
12988
12989 static int
12990 set_cursor_from_row (struct window *w, struct glyph_row *row,
12991 struct glyph_matrix *matrix,
12992 EMACS_INT delta, EMACS_INT delta_bytes,
12993 int dy, int dvpos)
12994 {
12995 struct glyph *glyph = row->glyphs[TEXT_AREA];
12996 struct glyph *end = glyph + row->used[TEXT_AREA];
12997 struct glyph *cursor = NULL;
12998 /* The last known character position in row. */
12999 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13000 int x = row->x;
13001 EMACS_INT pt_old = PT - delta;
13002 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13003 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13004 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13005 /* A glyph beyond the edge of TEXT_AREA which we should never
13006 touch. */
13007 struct glyph *glyphs_end = end;
13008 /* Non-zero means we've found a match for cursor position, but that
13009 glyph has the avoid_cursor_p flag set. */
13010 int match_with_avoid_cursor = 0;
13011 /* Non-zero means we've seen at least one glyph that came from a
13012 display string. */
13013 int string_seen = 0;
13014 /* Largest and smalles buffer positions seen so far during scan of
13015 glyph row. */
13016 EMACS_INT bpos_max = pos_before;
13017 EMACS_INT bpos_min = pos_after;
13018 /* Last buffer position covered by an overlay string with an integer
13019 `cursor' property. */
13020 EMACS_INT bpos_covered = 0;
13021
13022 /* Skip over glyphs not having an object at the start and the end of
13023 the row. These are special glyphs like truncation marks on
13024 terminal frames. */
13025 if (row->displays_text_p)
13026 {
13027 if (!row->reversed_p)
13028 {
13029 while (glyph < end
13030 && INTEGERP (glyph->object)
13031 && glyph->charpos < 0)
13032 {
13033 x += glyph->pixel_width;
13034 ++glyph;
13035 }
13036 while (end > glyph
13037 && INTEGERP ((end - 1)->object)
13038 /* CHARPOS is zero for blanks and stretch glyphs
13039 inserted by extend_face_to_end_of_line. */
13040 && (end - 1)->charpos <= 0)
13041 --end;
13042 glyph_before = glyph - 1;
13043 glyph_after = end;
13044 }
13045 else
13046 {
13047 struct glyph *g;
13048
13049 /* If the glyph row is reversed, we need to process it from back
13050 to front, so swap the edge pointers. */
13051 glyphs_end = end = glyph - 1;
13052 glyph += row->used[TEXT_AREA] - 1;
13053
13054 while (glyph > end + 1
13055 && INTEGERP (glyph->object)
13056 && glyph->charpos < 0)
13057 {
13058 --glyph;
13059 x -= glyph->pixel_width;
13060 }
13061 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13062 --glyph;
13063 /* By default, in reversed rows we put the cursor on the
13064 rightmost (first in the reading order) glyph. */
13065 for (g = end + 1; g < glyph; g++)
13066 x += g->pixel_width;
13067 while (end < glyph
13068 && INTEGERP ((end + 1)->object)
13069 && (end + 1)->charpos <= 0)
13070 ++end;
13071 glyph_before = glyph + 1;
13072 glyph_after = end;
13073 }
13074 }
13075 else if (row->reversed_p)
13076 {
13077 /* In R2L rows that don't display text, put the cursor on the
13078 rightmost glyph. Case in point: an empty last line that is
13079 part of an R2L paragraph. */
13080 cursor = end - 1;
13081 /* Avoid placing the cursor on the last glyph of the row, where
13082 on terminal frames we hold the vertical border between
13083 adjacent windows. */
13084 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13085 && !WINDOW_RIGHTMOST_P (w)
13086 && cursor == row->glyphs[LAST_AREA] - 1)
13087 cursor--;
13088 x = -1; /* will be computed below, at label compute_x */
13089 }
13090
13091 /* Step 1: Try to find the glyph whose character position
13092 corresponds to point. If that's not possible, find 2 glyphs
13093 whose character positions are the closest to point, one before
13094 point, the other after it. */
13095 if (!row->reversed_p)
13096 while (/* not marched to end of glyph row */
13097 glyph < end
13098 /* glyph was not inserted by redisplay for internal purposes */
13099 && !INTEGERP (glyph->object))
13100 {
13101 if (BUFFERP (glyph->object))
13102 {
13103 EMACS_INT dpos = glyph->charpos - pt_old;
13104
13105 if (glyph->charpos > bpos_max)
13106 bpos_max = glyph->charpos;
13107 if (glyph->charpos < bpos_min)
13108 bpos_min = glyph->charpos;
13109 if (!glyph->avoid_cursor_p)
13110 {
13111 /* If we hit point, we've found the glyph on which to
13112 display the cursor. */
13113 if (dpos == 0)
13114 {
13115 match_with_avoid_cursor = 0;
13116 break;
13117 }
13118 /* See if we've found a better approximation to
13119 POS_BEFORE or to POS_AFTER. Note that we want the
13120 first (leftmost) glyph of all those that are the
13121 closest from below, and the last (rightmost) of all
13122 those from above. */
13123 if (0 > dpos && dpos > pos_before - pt_old)
13124 {
13125 pos_before = glyph->charpos;
13126 glyph_before = glyph;
13127 }
13128 else if (0 < dpos && dpos <= pos_after - pt_old)
13129 {
13130 pos_after = glyph->charpos;
13131 glyph_after = glyph;
13132 }
13133 }
13134 else if (dpos == 0)
13135 match_with_avoid_cursor = 1;
13136 }
13137 else if (STRINGP (glyph->object))
13138 {
13139 Lisp_Object chprop;
13140 EMACS_INT glyph_pos = glyph->charpos;
13141
13142 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13143 glyph->object);
13144 if (INTEGERP (chprop))
13145 {
13146 bpos_covered = bpos_max + XINT (chprop);
13147 /* If the `cursor' property covers buffer positions up
13148 to and including point, we should display cursor on
13149 this glyph. Note that overlays and text properties
13150 with string values stop bidi reordering, so every
13151 buffer position to the left of the string is always
13152 smaller than any position to the right of the
13153 string. Therefore, if a `cursor' property on one
13154 of the string's characters has an integer value, we
13155 will break out of the loop below _before_ we get to
13156 the position match above. IOW, integer values of
13157 the `cursor' property override the "exact match for
13158 point" strategy of positioning the cursor. */
13159 /* Implementation note: bpos_max == pt_old when, e.g.,
13160 we are in an empty line, where bpos_max is set to
13161 MATRIX_ROW_START_CHARPOS, see above. */
13162 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13163 {
13164 cursor = glyph;
13165 break;
13166 }
13167 }
13168
13169 string_seen = 1;
13170 }
13171 x += glyph->pixel_width;
13172 ++glyph;
13173 }
13174 else if (glyph > end) /* row is reversed */
13175 while (!INTEGERP (glyph->object))
13176 {
13177 if (BUFFERP (glyph->object))
13178 {
13179 EMACS_INT dpos = glyph->charpos - pt_old;
13180
13181 if (glyph->charpos > bpos_max)
13182 bpos_max = glyph->charpos;
13183 if (glyph->charpos < bpos_min)
13184 bpos_min = glyph->charpos;
13185 if (!glyph->avoid_cursor_p)
13186 {
13187 if (dpos == 0)
13188 {
13189 match_with_avoid_cursor = 0;
13190 break;
13191 }
13192 if (0 > dpos && dpos > pos_before - pt_old)
13193 {
13194 pos_before = glyph->charpos;
13195 glyph_before = glyph;
13196 }
13197 else if (0 < dpos && dpos <= pos_after - pt_old)
13198 {
13199 pos_after = glyph->charpos;
13200 glyph_after = glyph;
13201 }
13202 }
13203 else if (dpos == 0)
13204 match_with_avoid_cursor = 1;
13205 }
13206 else if (STRINGP (glyph->object))
13207 {
13208 Lisp_Object chprop;
13209 EMACS_INT glyph_pos = glyph->charpos;
13210
13211 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13212 glyph->object);
13213 if (INTEGERP (chprop))
13214 {
13215 bpos_covered = bpos_max + XINT (chprop);
13216 /* If the `cursor' property covers buffer positions up
13217 to and including point, we should display cursor on
13218 this glyph. */
13219 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13220 {
13221 cursor = glyph;
13222 break;
13223 }
13224 }
13225 string_seen = 1;
13226 }
13227 --glyph;
13228 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13229 {
13230 x--; /* can't use any pixel_width */
13231 break;
13232 }
13233 x -= glyph->pixel_width;
13234 }
13235
13236 /* Step 2: If we didn't find an exact match for point, we need to
13237 look for a proper place to put the cursor among glyphs between
13238 GLYPH_BEFORE and GLYPH_AFTER. */
13239 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13240 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13241 && bpos_covered < pt_old)
13242 {
13243 /* An empty line has a single glyph whose OBJECT is zero and
13244 whose CHARPOS is the position of a newline on that line.
13245 Note that on a TTY, there are more glyphs after that, which
13246 were produced by extend_face_to_end_of_line, but their
13247 CHARPOS is zero or negative. */
13248 int empty_line_p =
13249 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13250 && INTEGERP (glyph->object) && glyph->charpos > 0;
13251
13252 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13253 {
13254 EMACS_INT ellipsis_pos;
13255
13256 /* Scan back over the ellipsis glyphs. */
13257 if (!row->reversed_p)
13258 {
13259 ellipsis_pos = (glyph - 1)->charpos;
13260 while (glyph > row->glyphs[TEXT_AREA]
13261 && (glyph - 1)->charpos == ellipsis_pos)
13262 glyph--, x -= glyph->pixel_width;
13263 /* That loop always goes one position too far, including
13264 the glyph before the ellipsis. So scan forward over
13265 that one. */
13266 x += glyph->pixel_width;
13267 glyph++;
13268 }
13269 else /* row is reversed */
13270 {
13271 ellipsis_pos = (glyph + 1)->charpos;
13272 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13273 && (glyph + 1)->charpos == ellipsis_pos)
13274 glyph++, x += glyph->pixel_width;
13275 x -= glyph->pixel_width;
13276 glyph--;
13277 }
13278 }
13279 else if (match_with_avoid_cursor
13280 /* A truncated row may not include PT among its
13281 character positions. Setting the cursor inside the
13282 scroll margin will trigger recalculation of hscroll
13283 in hscroll_window_tree. */
13284 || (row->truncated_on_left_p && pt_old < bpos_min)
13285 || (row->truncated_on_right_p && pt_old > bpos_max)
13286 /* Zero-width characters produce no glyphs. */
13287 || (!string_seen
13288 && !empty_line_p
13289 && (row->reversed_p
13290 ? glyph_after > glyphs_end
13291 : glyph_after < glyphs_end)))
13292 {
13293 cursor = glyph_after;
13294 x = -1;
13295 }
13296 else if (string_seen)
13297 {
13298 int incr = row->reversed_p ? -1 : +1;
13299
13300 /* Need to find the glyph that came out of a string which is
13301 present at point. That glyph is somewhere between
13302 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13303 positioned between POS_BEFORE and POS_AFTER in the
13304 buffer. */
13305 struct glyph *start, *stop;
13306 EMACS_INT pos = pos_before;
13307
13308 x = -1;
13309
13310 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13311 correspond to POS_BEFORE and POS_AFTER, respectively. We
13312 need START and STOP in the order that corresponds to the
13313 row's direction as given by its reversed_p flag. If the
13314 directionality of characters between POS_BEFORE and
13315 POS_AFTER is the opposite of the row's base direction,
13316 these characters will have been reordered for display,
13317 and we need to reverse START and STOP. */
13318 if (!row->reversed_p)
13319 {
13320 start = min (glyph_before, glyph_after);
13321 stop = max (glyph_before, glyph_after);
13322 }
13323 else
13324 {
13325 start = max (glyph_before, glyph_after);
13326 stop = min (glyph_before, glyph_after);
13327 }
13328 for (glyph = start + incr;
13329 row->reversed_p ? glyph > stop : glyph < stop; )
13330 {
13331
13332 /* Any glyphs that come from the buffer are here because
13333 of bidi reordering. Skip them, and only pay
13334 attention to glyphs that came from some string. */
13335 if (STRINGP (glyph->object))
13336 {
13337 Lisp_Object str;
13338 EMACS_INT tem;
13339
13340 str = glyph->object;
13341 tem = string_buffer_position_lim (str, pos, pos_after, 0);
13342 if (tem == 0 /* from overlay */
13343 || pos <= tem)
13344 {
13345 /* If the string from which this glyph came is
13346 found in the buffer at point, then we've
13347 found the glyph we've been looking for. If
13348 it comes from an overlay (tem == 0), and it
13349 has the `cursor' property on one of its
13350 glyphs, record that glyph as a candidate for
13351 displaying the cursor. (As in the
13352 unidirectional version, we will display the
13353 cursor on the last candidate we find.) */
13354 if (tem == 0 || tem == pt_old)
13355 {
13356 /* The glyphs from this string could have
13357 been reordered. Find the one with the
13358 smallest string position. Or there could
13359 be a character in the string with the
13360 `cursor' property, which means display
13361 cursor on that character's glyph. */
13362 EMACS_INT strpos = glyph->charpos;
13363
13364 if (tem)
13365 cursor = glyph;
13366 for ( ;
13367 (row->reversed_p ? glyph > stop : glyph < stop)
13368 && EQ (glyph->object, str);
13369 glyph += incr)
13370 {
13371 Lisp_Object cprop;
13372 EMACS_INT gpos = glyph->charpos;
13373
13374 cprop = Fget_char_property (make_number (gpos),
13375 Qcursor,
13376 glyph->object);
13377 if (!NILP (cprop))
13378 {
13379 cursor = glyph;
13380 break;
13381 }
13382 if (tem && glyph->charpos < strpos)
13383 {
13384 strpos = glyph->charpos;
13385 cursor = glyph;
13386 }
13387 }
13388
13389 if (tem == pt_old)
13390 goto compute_x;
13391 }
13392 if (tem)
13393 pos = tem + 1; /* don't find previous instances */
13394 }
13395 /* This string is not what we want; skip all of the
13396 glyphs that came from it. */
13397 while ((row->reversed_p ? glyph > stop : glyph < stop)
13398 && EQ (glyph->object, str))
13399 glyph += incr;
13400 }
13401 else
13402 glyph += incr;
13403 }
13404
13405 /* If we reached the end of the line, and END was from a string,
13406 the cursor is not on this line. */
13407 if (cursor == NULL
13408 && (row->reversed_p ? glyph <= end : glyph >= end)
13409 && STRINGP (end->object)
13410 && row->continued_p)
13411 return 0;
13412 }
13413 }
13414
13415 compute_x:
13416 if (cursor != NULL)
13417 glyph = cursor;
13418 if (x < 0)
13419 {
13420 struct glyph *g;
13421
13422 /* Need to compute x that corresponds to GLYPH. */
13423 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13424 {
13425 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13426 abort ();
13427 x += g->pixel_width;
13428 }
13429 }
13430
13431 /* ROW could be part of a continued line, which, under bidi
13432 reordering, might have other rows whose start and end charpos
13433 occlude point. Only set w->cursor if we found a better
13434 approximation to the cursor position than we have from previously
13435 examined candidate rows belonging to the same continued line. */
13436 if (/* we already have a candidate row */
13437 w->cursor.vpos >= 0
13438 /* that candidate is not the row we are processing */
13439 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13440 /* the row we are processing is part of a continued line */
13441 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13442 /* Make sure cursor.vpos specifies a row whose start and end
13443 charpos occlude point. This is because some callers of this
13444 function leave cursor.vpos at the row where the cursor was
13445 displayed during the last redisplay cycle. */
13446 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13447 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13448 {
13449 struct glyph *g1 =
13450 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13451
13452 /* Don't consider glyphs that are outside TEXT_AREA. */
13453 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13454 return 0;
13455 /* Keep the candidate whose buffer position is the closest to
13456 point. */
13457 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13458 w->cursor.hpos >= 0
13459 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13460 && BUFFERP (g1->object)
13461 && (g1->charpos == pt_old /* an exact match always wins */
13462 || (BUFFERP (glyph->object)
13463 && eabs (g1->charpos - pt_old)
13464 < eabs (glyph->charpos - pt_old))))
13465 return 0;
13466 /* If this candidate gives an exact match, use that. */
13467 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13468 /* Otherwise, keep the candidate that comes from a row
13469 spanning less buffer positions. This may win when one or
13470 both candidate positions are on glyphs that came from
13471 display strings, for which we cannot compare buffer
13472 positions. */
13473 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13474 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13475 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13476 return 0;
13477 }
13478 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13479 w->cursor.x = x;
13480 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13481 w->cursor.y = row->y + dy;
13482
13483 if (w == XWINDOW (selected_window))
13484 {
13485 if (!row->continued_p
13486 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13487 && row->x == 0)
13488 {
13489 this_line_buffer = XBUFFER (w->buffer);
13490
13491 CHARPOS (this_line_start_pos)
13492 = MATRIX_ROW_START_CHARPOS (row) + delta;
13493 BYTEPOS (this_line_start_pos)
13494 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13495
13496 CHARPOS (this_line_end_pos)
13497 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13498 BYTEPOS (this_line_end_pos)
13499 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13500
13501 this_line_y = w->cursor.y;
13502 this_line_pixel_height = row->height;
13503 this_line_vpos = w->cursor.vpos;
13504 this_line_start_x = row->x;
13505 }
13506 else
13507 CHARPOS (this_line_start_pos) = 0;
13508 }
13509
13510 return 1;
13511 }
13512
13513
13514 /* Run window scroll functions, if any, for WINDOW with new window
13515 start STARTP. Sets the window start of WINDOW to that position.
13516
13517 We assume that the window's buffer is really current. */
13518
13519 static INLINE struct text_pos
13520 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13521 {
13522 struct window *w = XWINDOW (window);
13523 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13524
13525 if (current_buffer != XBUFFER (w->buffer))
13526 abort ();
13527
13528 if (!NILP (Vwindow_scroll_functions))
13529 {
13530 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13531 make_number (CHARPOS (startp)));
13532 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13533 /* In case the hook functions switch buffers. */
13534 if (current_buffer != XBUFFER (w->buffer))
13535 set_buffer_internal_1 (XBUFFER (w->buffer));
13536 }
13537
13538 return startp;
13539 }
13540
13541
13542 /* Make sure the line containing the cursor is fully visible.
13543 A value of 1 means there is nothing to be done.
13544 (Either the line is fully visible, or it cannot be made so,
13545 or we cannot tell.)
13546
13547 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13548 is higher than window.
13549
13550 A value of 0 means the caller should do scrolling
13551 as if point had gone off the screen. */
13552
13553 static int
13554 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13555 {
13556 struct glyph_matrix *matrix;
13557 struct glyph_row *row;
13558 int window_height;
13559
13560 if (!make_cursor_line_fully_visible_p)
13561 return 1;
13562
13563 /* It's not always possible to find the cursor, e.g, when a window
13564 is full of overlay strings. Don't do anything in that case. */
13565 if (w->cursor.vpos < 0)
13566 return 1;
13567
13568 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13569 row = MATRIX_ROW (matrix, w->cursor.vpos);
13570
13571 /* If the cursor row is not partially visible, there's nothing to do. */
13572 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13573 return 1;
13574
13575 /* If the row the cursor is in is taller than the window's height,
13576 it's not clear what to do, so do nothing. */
13577 window_height = window_box_height (w);
13578 if (row->height >= window_height)
13579 {
13580 if (!force_p || MINI_WINDOW_P (w)
13581 || w->vscroll || w->cursor.vpos == 0)
13582 return 1;
13583 }
13584 return 0;
13585 }
13586
13587
13588 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13589 non-zero means only WINDOW is redisplayed in redisplay_internal.
13590 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13591 in redisplay_window to bring a partially visible line into view in
13592 the case that only the cursor has moved.
13593
13594 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13595 last screen line's vertical height extends past the end of the screen.
13596
13597 Value is
13598
13599 1 if scrolling succeeded
13600
13601 0 if scrolling didn't find point.
13602
13603 -1 if new fonts have been loaded so that we must interrupt
13604 redisplay, adjust glyph matrices, and try again. */
13605
13606 enum
13607 {
13608 SCROLLING_SUCCESS,
13609 SCROLLING_FAILED,
13610 SCROLLING_NEED_LARGER_MATRICES
13611 };
13612
13613 /* If scroll-conservatively is more than this, never recenter.
13614
13615 If you change this, don't forget to update the doc string of
13616 `scroll-conservatively' and the Emacs manual. */
13617 #define SCROLL_LIMIT 100
13618
13619 static int
13620 try_scrolling (Lisp_Object window, int just_this_one_p,
13621 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13622 int temp_scroll_step, int last_line_misfit)
13623 {
13624 struct window *w = XWINDOW (window);
13625 struct frame *f = XFRAME (w->frame);
13626 struct text_pos pos, startp;
13627 struct it it;
13628 int this_scroll_margin, scroll_max, rc, height;
13629 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13630 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13631 Lisp_Object aggressive;
13632 /* We will never try scrolling more than this number of lines. */
13633 int scroll_limit = SCROLL_LIMIT;
13634
13635 #if GLYPH_DEBUG
13636 debug_method_add (w, "try_scrolling");
13637 #endif
13638
13639 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13640
13641 /* Compute scroll margin height in pixels. We scroll when point is
13642 within this distance from the top or bottom of the window. */
13643 if (scroll_margin > 0)
13644 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13645 * FRAME_LINE_HEIGHT (f);
13646 else
13647 this_scroll_margin = 0;
13648
13649 /* Force arg_scroll_conservatively to have a reasonable value, to
13650 avoid scrolling too far away with slow move_it_* functions. Note
13651 that the user can supply scroll-conservatively equal to
13652 `most-positive-fixnum', which can be larger than INT_MAX. */
13653 if (arg_scroll_conservatively > scroll_limit)
13654 {
13655 arg_scroll_conservatively = scroll_limit + 1;
13656 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13657 }
13658 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13659 /* Compute how much we should try to scroll maximally to bring
13660 point into view. */
13661 scroll_max = (max (scroll_step,
13662 max (arg_scroll_conservatively, temp_scroll_step))
13663 * FRAME_LINE_HEIGHT (f));
13664 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13665 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13666 /* We're trying to scroll because of aggressive scrolling but no
13667 scroll_step is set. Choose an arbitrary one. */
13668 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13669 else
13670 scroll_max = 0;
13671
13672 too_near_end:
13673
13674 /* Decide whether to scroll down. */
13675 if (PT > CHARPOS (startp))
13676 {
13677 int scroll_margin_y;
13678
13679 /* Compute the pixel ypos of the scroll margin, then move it to
13680 either that ypos or PT, whichever comes first. */
13681 start_display (&it, w, startp);
13682 scroll_margin_y = it.last_visible_y - this_scroll_margin
13683 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13684 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13685 (MOVE_TO_POS | MOVE_TO_Y));
13686
13687 if (PT > CHARPOS (it.current.pos))
13688 {
13689 int y0 = line_bottom_y (&it);
13690 /* Compute how many pixels below window bottom to stop searching
13691 for PT. This avoids costly search for PT that is far away if
13692 the user limited scrolling by a small number of lines, but
13693 always finds PT if scroll_conservatively is set to a large
13694 number, such as most-positive-fixnum. */
13695 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13696 int y_to_move = it.last_visible_y + slack;
13697
13698 /* Compute the distance from the scroll margin to PT or to
13699 the scroll limit, whichever comes first. This should
13700 include the height of the cursor line, to make that line
13701 fully visible. */
13702 move_it_to (&it, PT, -1, y_to_move,
13703 -1, MOVE_TO_POS | MOVE_TO_Y);
13704 dy = line_bottom_y (&it) - y0;
13705
13706 if (dy > scroll_max)
13707 return SCROLLING_FAILED;
13708
13709 scroll_down_p = 1;
13710 }
13711 }
13712
13713 if (scroll_down_p)
13714 {
13715 /* Point is in or below the bottom scroll margin, so move the
13716 window start down. If scrolling conservatively, move it just
13717 enough down to make point visible. If scroll_step is set,
13718 move it down by scroll_step. */
13719 if (arg_scroll_conservatively)
13720 amount_to_scroll
13721 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13722 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13723 else if (scroll_step || temp_scroll_step)
13724 amount_to_scroll = scroll_max;
13725 else
13726 {
13727 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13728 height = WINDOW_BOX_TEXT_HEIGHT (w);
13729 if (NUMBERP (aggressive))
13730 {
13731 double float_amount = XFLOATINT (aggressive) * height;
13732 amount_to_scroll = float_amount;
13733 if (amount_to_scroll == 0 && float_amount > 0)
13734 amount_to_scroll = 1;
13735 /* Don't let point enter the scroll margin near top of
13736 the window. */
13737 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13738 amount_to_scroll = height - 2*this_scroll_margin + dy;
13739 }
13740 }
13741
13742 if (amount_to_scroll <= 0)
13743 return SCROLLING_FAILED;
13744
13745 start_display (&it, w, startp);
13746 if (arg_scroll_conservatively <= scroll_limit)
13747 move_it_vertically (&it, amount_to_scroll);
13748 else
13749 {
13750 /* Extra precision for users who set scroll-conservatively
13751 to a large number: make sure the amount we scroll
13752 the window start is never less than amount_to_scroll,
13753 which was computed as distance from window bottom to
13754 point. This matters when lines at window top and lines
13755 below window bottom have different height. */
13756 struct it it1 = it;
13757 /* We use a temporary it1 because line_bottom_y can modify
13758 its argument, if it moves one line down; see there. */
13759 int start_y = line_bottom_y (&it1);
13760
13761 do {
13762 move_it_by_lines (&it, 1);
13763 it1 = it;
13764 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13765 }
13766
13767 /* If STARTP is unchanged, move it down another screen line. */
13768 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13769 move_it_by_lines (&it, 1);
13770 startp = it.current.pos;
13771 }
13772 else
13773 {
13774 struct text_pos scroll_margin_pos = startp;
13775
13776 /* See if point is inside the scroll margin at the top of the
13777 window. */
13778 if (this_scroll_margin)
13779 {
13780 start_display (&it, w, startp);
13781 move_it_vertically (&it, this_scroll_margin);
13782 scroll_margin_pos = it.current.pos;
13783 }
13784
13785 if (PT < CHARPOS (scroll_margin_pos))
13786 {
13787 /* Point is in the scroll margin at the top of the window or
13788 above what is displayed in the window. */
13789 int y0, y_to_move;
13790
13791 /* Compute the vertical distance from PT to the scroll
13792 margin position. Move as far as scroll_max allows, or
13793 one screenful, or 10 screen lines, whichever is largest.
13794 Give up if distance is greater than scroll_max. */
13795 SET_TEXT_POS (pos, PT, PT_BYTE);
13796 start_display (&it, w, pos);
13797 y0 = it.current_y;
13798 y_to_move = max (it.last_visible_y,
13799 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13800 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13801 y_to_move, -1,
13802 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13803 dy = it.current_y - y0;
13804 if (dy > scroll_max)
13805 return SCROLLING_FAILED;
13806
13807 /* Compute new window start. */
13808 start_display (&it, w, startp);
13809
13810 if (arg_scroll_conservatively)
13811 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13812 max (scroll_step, temp_scroll_step));
13813 else if (scroll_step || temp_scroll_step)
13814 amount_to_scroll = scroll_max;
13815 else
13816 {
13817 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13818 height = WINDOW_BOX_TEXT_HEIGHT (w);
13819 if (NUMBERP (aggressive))
13820 {
13821 double float_amount = XFLOATINT (aggressive) * height;
13822 amount_to_scroll = float_amount;
13823 if (amount_to_scroll == 0 && float_amount > 0)
13824 amount_to_scroll = 1;
13825 amount_to_scroll -=
13826 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13827 /* Don't let point enter the scroll margin near
13828 bottom of the window. */
13829 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13830 amount_to_scroll = height - 2*this_scroll_margin + dy;
13831 }
13832 }
13833
13834 if (amount_to_scroll <= 0)
13835 return SCROLLING_FAILED;
13836
13837 move_it_vertically_backward (&it, amount_to_scroll);
13838 startp = it.current.pos;
13839 }
13840 }
13841
13842 /* Run window scroll functions. */
13843 startp = run_window_scroll_functions (window, startp);
13844
13845 /* Display the window. Give up if new fonts are loaded, or if point
13846 doesn't appear. */
13847 if (!try_window (window, startp, 0))
13848 rc = SCROLLING_NEED_LARGER_MATRICES;
13849 else if (w->cursor.vpos < 0)
13850 {
13851 clear_glyph_matrix (w->desired_matrix);
13852 rc = SCROLLING_FAILED;
13853 }
13854 else
13855 {
13856 /* Maybe forget recorded base line for line number display. */
13857 if (!just_this_one_p
13858 || current_buffer->clip_changed
13859 || BEG_UNCHANGED < CHARPOS (startp))
13860 w->base_line_number = Qnil;
13861
13862 /* If cursor ends up on a partially visible line,
13863 treat that as being off the bottom of the screen. */
13864 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13865 /* It's possible that the cursor is on the first line of the
13866 buffer, which is partially obscured due to a vscroll
13867 (Bug#7537). In that case, avoid looping forever . */
13868 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13869 {
13870 clear_glyph_matrix (w->desired_matrix);
13871 ++extra_scroll_margin_lines;
13872 goto too_near_end;
13873 }
13874 rc = SCROLLING_SUCCESS;
13875 }
13876
13877 return rc;
13878 }
13879
13880
13881 /* Compute a suitable window start for window W if display of W starts
13882 on a continuation line. Value is non-zero if a new window start
13883 was computed.
13884
13885 The new window start will be computed, based on W's width, starting
13886 from the start of the continued line. It is the start of the
13887 screen line with the minimum distance from the old start W->start. */
13888
13889 static int
13890 compute_window_start_on_continuation_line (struct window *w)
13891 {
13892 struct text_pos pos, start_pos;
13893 int window_start_changed_p = 0;
13894
13895 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
13896
13897 /* If window start is on a continuation line... Window start may be
13898 < BEGV in case there's invisible text at the start of the
13899 buffer (M-x rmail, for example). */
13900 if (CHARPOS (start_pos) > BEGV
13901 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
13902 {
13903 struct it it;
13904 struct glyph_row *row;
13905
13906 /* Handle the case that the window start is out of range. */
13907 if (CHARPOS (start_pos) < BEGV)
13908 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
13909 else if (CHARPOS (start_pos) > ZV)
13910 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
13911
13912 /* Find the start of the continued line. This should be fast
13913 because scan_buffer is fast (newline cache). */
13914 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
13915 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
13916 row, DEFAULT_FACE_ID);
13917 reseat_at_previous_visible_line_start (&it);
13918
13919 /* If the line start is "too far" away from the window start,
13920 say it takes too much time to compute a new window start. */
13921 if (CHARPOS (start_pos) - IT_CHARPOS (it)
13922 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
13923 {
13924 int min_distance, distance;
13925
13926 /* Move forward by display lines to find the new window
13927 start. If window width was enlarged, the new start can
13928 be expected to be > the old start. If window width was
13929 decreased, the new window start will be < the old start.
13930 So, we're looking for the display line start with the
13931 minimum distance from the old window start. */
13932 pos = it.current.pos;
13933 min_distance = INFINITY;
13934 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
13935 distance < min_distance)
13936 {
13937 min_distance = distance;
13938 pos = it.current.pos;
13939 move_it_by_lines (&it, 1);
13940 }
13941
13942 /* Set the window start there. */
13943 SET_MARKER_FROM_TEXT_POS (w->start, pos);
13944 window_start_changed_p = 1;
13945 }
13946 }
13947
13948 return window_start_changed_p;
13949 }
13950
13951
13952 /* Try cursor movement in case text has not changed in window WINDOW,
13953 with window start STARTP. Value is
13954
13955 CURSOR_MOVEMENT_SUCCESS if successful
13956
13957 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
13958
13959 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
13960 display. *SCROLL_STEP is set to 1, under certain circumstances, if
13961 we want to scroll as if scroll-step were set to 1. See the code.
13962
13963 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
13964 which case we have to abort this redisplay, and adjust matrices
13965 first. */
13966
13967 enum
13968 {
13969 CURSOR_MOVEMENT_SUCCESS,
13970 CURSOR_MOVEMENT_CANNOT_BE_USED,
13971 CURSOR_MOVEMENT_MUST_SCROLL,
13972 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
13973 };
13974
13975 static int
13976 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
13977 {
13978 struct window *w = XWINDOW (window);
13979 struct frame *f = XFRAME (w->frame);
13980 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
13981
13982 #if GLYPH_DEBUG
13983 if (inhibit_try_cursor_movement)
13984 return rc;
13985 #endif
13986
13987 /* Handle case where text has not changed, only point, and it has
13988 not moved off the frame. */
13989 if (/* Point may be in this window. */
13990 PT >= CHARPOS (startp)
13991 /* Selective display hasn't changed. */
13992 && !current_buffer->clip_changed
13993 /* Function force-mode-line-update is used to force a thorough
13994 redisplay. It sets either windows_or_buffers_changed or
13995 update_mode_lines. So don't take a shortcut here for these
13996 cases. */
13997 && !update_mode_lines
13998 && !windows_or_buffers_changed
13999 && !cursor_type_changed
14000 /* Can't use this case if highlighting a region. When a
14001 region exists, cursor movement has to do more than just
14002 set the cursor. */
14003 && !(!NILP (Vtransient_mark_mode)
14004 && !NILP (BVAR (current_buffer, mark_active)))
14005 && NILP (w->region_showing)
14006 && NILP (Vshow_trailing_whitespace)
14007 /* Right after splitting windows, last_point may be nil. */
14008 && INTEGERP (w->last_point)
14009 /* This code is not used for mini-buffer for the sake of the case
14010 of redisplaying to replace an echo area message; since in
14011 that case the mini-buffer contents per se are usually
14012 unchanged. This code is of no real use in the mini-buffer
14013 since the handling of this_line_start_pos, etc., in redisplay
14014 handles the same cases. */
14015 && !EQ (window, minibuf_window)
14016 /* When splitting windows or for new windows, it happens that
14017 redisplay is called with a nil window_end_vpos or one being
14018 larger than the window. This should really be fixed in
14019 window.c. I don't have this on my list, now, so we do
14020 approximately the same as the old redisplay code. --gerd. */
14021 && INTEGERP (w->window_end_vpos)
14022 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14023 && (FRAME_WINDOW_P (f)
14024 || !overlay_arrow_in_current_buffer_p ()))
14025 {
14026 int this_scroll_margin, top_scroll_margin;
14027 struct glyph_row *row = NULL;
14028
14029 #if GLYPH_DEBUG
14030 debug_method_add (w, "cursor movement");
14031 #endif
14032
14033 /* Scroll if point within this distance from the top or bottom
14034 of the window. This is a pixel value. */
14035 if (scroll_margin > 0)
14036 {
14037 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14038 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14039 }
14040 else
14041 this_scroll_margin = 0;
14042
14043 top_scroll_margin = this_scroll_margin;
14044 if (WINDOW_WANTS_HEADER_LINE_P (w))
14045 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14046
14047 /* Start with the row the cursor was displayed during the last
14048 not paused redisplay. Give up if that row is not valid. */
14049 if (w->last_cursor.vpos < 0
14050 || w->last_cursor.vpos >= w->current_matrix->nrows)
14051 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14052 else
14053 {
14054 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14055 if (row->mode_line_p)
14056 ++row;
14057 if (!row->enabled_p)
14058 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14059 }
14060
14061 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14062 {
14063 int scroll_p = 0, must_scroll = 0;
14064 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14065
14066 if (PT > XFASTINT (w->last_point))
14067 {
14068 /* Point has moved forward. */
14069 while (MATRIX_ROW_END_CHARPOS (row) < PT
14070 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14071 {
14072 xassert (row->enabled_p);
14073 ++row;
14074 }
14075
14076 /* If the end position of a row equals the start
14077 position of the next row, and PT is at that position,
14078 we would rather display cursor in the next line. */
14079 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14080 && MATRIX_ROW_END_CHARPOS (row) == PT
14081 && row < w->current_matrix->rows
14082 + w->current_matrix->nrows - 1
14083 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14084 && !cursor_row_p (row))
14085 ++row;
14086
14087 /* If within the scroll margin, scroll. Note that
14088 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14089 the next line would be drawn, and that
14090 this_scroll_margin can be zero. */
14091 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14092 || PT > MATRIX_ROW_END_CHARPOS (row)
14093 /* Line is completely visible last line in window
14094 and PT is to be set in the next line. */
14095 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14096 && PT == MATRIX_ROW_END_CHARPOS (row)
14097 && !row->ends_at_zv_p
14098 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14099 scroll_p = 1;
14100 }
14101 else if (PT < XFASTINT (w->last_point))
14102 {
14103 /* Cursor has to be moved backward. Note that PT >=
14104 CHARPOS (startp) because of the outer if-statement. */
14105 while (!row->mode_line_p
14106 && (MATRIX_ROW_START_CHARPOS (row) > PT
14107 || (MATRIX_ROW_START_CHARPOS (row) == PT
14108 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14109 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14110 row > w->current_matrix->rows
14111 && (row-1)->ends_in_newline_from_string_p))))
14112 && (row->y > top_scroll_margin
14113 || CHARPOS (startp) == BEGV))
14114 {
14115 xassert (row->enabled_p);
14116 --row;
14117 }
14118
14119 /* Consider the following case: Window starts at BEGV,
14120 there is invisible, intangible text at BEGV, so that
14121 display starts at some point START > BEGV. It can
14122 happen that we are called with PT somewhere between
14123 BEGV and START. Try to handle that case. */
14124 if (row < w->current_matrix->rows
14125 || row->mode_line_p)
14126 {
14127 row = w->current_matrix->rows;
14128 if (row->mode_line_p)
14129 ++row;
14130 }
14131
14132 /* Due to newlines in overlay strings, we may have to
14133 skip forward over overlay strings. */
14134 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14135 && MATRIX_ROW_END_CHARPOS (row) == PT
14136 && !cursor_row_p (row))
14137 ++row;
14138
14139 /* If within the scroll margin, scroll. */
14140 if (row->y < top_scroll_margin
14141 && CHARPOS (startp) != BEGV)
14142 scroll_p = 1;
14143 }
14144 else
14145 {
14146 /* Cursor did not move. So don't scroll even if cursor line
14147 is partially visible, as it was so before. */
14148 rc = CURSOR_MOVEMENT_SUCCESS;
14149 }
14150
14151 if (PT < MATRIX_ROW_START_CHARPOS (row)
14152 || PT > MATRIX_ROW_END_CHARPOS (row))
14153 {
14154 /* if PT is not in the glyph row, give up. */
14155 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14156 must_scroll = 1;
14157 }
14158 else if (rc != CURSOR_MOVEMENT_SUCCESS
14159 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14160 {
14161 /* If rows are bidi-reordered and point moved, back up
14162 until we find a row that does not belong to a
14163 continuation line. This is because we must consider
14164 all rows of a continued line as candidates for the
14165 new cursor positioning, since row start and end
14166 positions change non-linearly with vertical position
14167 in such rows. */
14168 /* FIXME: Revisit this when glyph ``spilling'' in
14169 continuation lines' rows is implemented for
14170 bidi-reordered rows. */
14171 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14172 {
14173 xassert (row->enabled_p);
14174 --row;
14175 /* If we hit the beginning of the displayed portion
14176 without finding the first row of a continued
14177 line, give up. */
14178 if (row <= w->current_matrix->rows)
14179 {
14180 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14181 break;
14182 }
14183
14184 }
14185 }
14186 if (must_scroll)
14187 ;
14188 else if (rc != CURSOR_MOVEMENT_SUCCESS
14189 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14190 && make_cursor_line_fully_visible_p)
14191 {
14192 if (PT == MATRIX_ROW_END_CHARPOS (row)
14193 && !row->ends_at_zv_p
14194 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14195 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14196 else if (row->height > window_box_height (w))
14197 {
14198 /* If we end up in a partially visible line, let's
14199 make it fully visible, except when it's taller
14200 than the window, in which case we can't do much
14201 about it. */
14202 *scroll_step = 1;
14203 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14204 }
14205 else
14206 {
14207 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14208 if (!cursor_row_fully_visible_p (w, 0, 1))
14209 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14210 else
14211 rc = CURSOR_MOVEMENT_SUCCESS;
14212 }
14213 }
14214 else if (scroll_p)
14215 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14216 else if (rc != CURSOR_MOVEMENT_SUCCESS
14217 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14218 {
14219 /* With bidi-reordered rows, there could be more than
14220 one candidate row whose start and end positions
14221 occlude point. We need to let set_cursor_from_row
14222 find the best candidate. */
14223 /* FIXME: Revisit this when glyph ``spilling'' in
14224 continuation lines' rows is implemented for
14225 bidi-reordered rows. */
14226 int rv = 0;
14227
14228 do
14229 {
14230 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14231 && PT <= MATRIX_ROW_END_CHARPOS (row)
14232 && cursor_row_p (row))
14233 rv |= set_cursor_from_row (w, row, w->current_matrix,
14234 0, 0, 0, 0);
14235 /* As soon as we've found the first suitable row
14236 whose ends_at_zv_p flag is set, we are done. */
14237 if (rv
14238 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14239 {
14240 rc = CURSOR_MOVEMENT_SUCCESS;
14241 break;
14242 }
14243 ++row;
14244 }
14245 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14246 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14247 || (MATRIX_ROW_START_CHARPOS (row) == PT
14248 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14249 /* If we didn't find any candidate rows, or exited the
14250 loop before all the candidates were examined, signal
14251 to the caller that this method failed. */
14252 if (rc != CURSOR_MOVEMENT_SUCCESS
14253 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14254 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14255 else if (rv)
14256 rc = CURSOR_MOVEMENT_SUCCESS;
14257 }
14258 else
14259 {
14260 do
14261 {
14262 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14263 {
14264 rc = CURSOR_MOVEMENT_SUCCESS;
14265 break;
14266 }
14267 ++row;
14268 }
14269 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14270 && MATRIX_ROW_START_CHARPOS (row) == PT
14271 && cursor_row_p (row));
14272 }
14273 }
14274 }
14275
14276 return rc;
14277 }
14278
14279 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14280 static
14281 #endif
14282 void
14283 set_vertical_scroll_bar (struct window *w)
14284 {
14285 EMACS_INT start, end, whole;
14286
14287 /* Calculate the start and end positions for the current window.
14288 At some point, it would be nice to choose between scrollbars
14289 which reflect the whole buffer size, with special markers
14290 indicating narrowing, and scrollbars which reflect only the
14291 visible region.
14292
14293 Note that mini-buffers sometimes aren't displaying any text. */
14294 if (!MINI_WINDOW_P (w)
14295 || (w == XWINDOW (minibuf_window)
14296 && NILP (echo_area_buffer[0])))
14297 {
14298 struct buffer *buf = XBUFFER (w->buffer);
14299 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14300 start = marker_position (w->start) - BUF_BEGV (buf);
14301 /* I don't think this is guaranteed to be right. For the
14302 moment, we'll pretend it is. */
14303 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14304
14305 if (end < start)
14306 end = start;
14307 if (whole < (end - start))
14308 whole = end - start;
14309 }
14310 else
14311 start = end = whole = 0;
14312
14313 /* Indicate what this scroll bar ought to be displaying now. */
14314 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14315 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14316 (w, end - start, whole, start);
14317 }
14318
14319
14320 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14321 selected_window is redisplayed.
14322
14323 We can return without actually redisplaying the window if
14324 fonts_changed_p is nonzero. In that case, redisplay_internal will
14325 retry. */
14326
14327 static void
14328 redisplay_window (Lisp_Object window, int just_this_one_p)
14329 {
14330 struct window *w = XWINDOW (window);
14331 struct frame *f = XFRAME (w->frame);
14332 struct buffer *buffer = XBUFFER (w->buffer);
14333 struct buffer *old = current_buffer;
14334 struct text_pos lpoint, opoint, startp;
14335 int update_mode_line;
14336 int tem;
14337 struct it it;
14338 /* Record it now because it's overwritten. */
14339 int current_matrix_up_to_date_p = 0;
14340 int used_current_matrix_p = 0;
14341 /* This is less strict than current_matrix_up_to_date_p.
14342 It indictes that the buffer contents and narrowing are unchanged. */
14343 int buffer_unchanged_p = 0;
14344 int temp_scroll_step = 0;
14345 int count = SPECPDL_INDEX ();
14346 int rc;
14347 int centering_position = -1;
14348 int last_line_misfit = 0;
14349 EMACS_INT beg_unchanged, end_unchanged;
14350
14351 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14352 opoint = lpoint;
14353
14354 /* W must be a leaf window here. */
14355 xassert (!NILP (w->buffer));
14356 #if GLYPH_DEBUG
14357 *w->desired_matrix->method = 0;
14358 #endif
14359
14360 restart:
14361 reconsider_clip_changes (w, buffer);
14362
14363 /* Has the mode line to be updated? */
14364 update_mode_line = (!NILP (w->update_mode_line)
14365 || update_mode_lines
14366 || buffer->clip_changed
14367 || buffer->prevent_redisplay_optimizations_p);
14368
14369 if (MINI_WINDOW_P (w))
14370 {
14371 if (w == XWINDOW (echo_area_window)
14372 && !NILP (echo_area_buffer[0]))
14373 {
14374 if (update_mode_line)
14375 /* We may have to update a tty frame's menu bar or a
14376 tool-bar. Example `M-x C-h C-h C-g'. */
14377 goto finish_menu_bars;
14378 else
14379 /* We've already displayed the echo area glyphs in this window. */
14380 goto finish_scroll_bars;
14381 }
14382 else if ((w != XWINDOW (minibuf_window)
14383 || minibuf_level == 0)
14384 /* When buffer is nonempty, redisplay window normally. */
14385 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14386 /* Quail displays non-mini buffers in minibuffer window.
14387 In that case, redisplay the window normally. */
14388 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14389 {
14390 /* W is a mini-buffer window, but it's not active, so clear
14391 it. */
14392 int yb = window_text_bottom_y (w);
14393 struct glyph_row *row;
14394 int y;
14395
14396 for (y = 0, row = w->desired_matrix->rows;
14397 y < yb;
14398 y += row->height, ++row)
14399 blank_row (w, row, y);
14400 goto finish_scroll_bars;
14401 }
14402
14403 clear_glyph_matrix (w->desired_matrix);
14404 }
14405
14406 /* Otherwise set up data on this window; select its buffer and point
14407 value. */
14408 /* Really select the buffer, for the sake of buffer-local
14409 variables. */
14410 set_buffer_internal_1 (XBUFFER (w->buffer));
14411
14412 current_matrix_up_to_date_p
14413 = (!NILP (w->window_end_valid)
14414 && !current_buffer->clip_changed
14415 && !current_buffer->prevent_redisplay_optimizations_p
14416 && XFASTINT (w->last_modified) >= MODIFF
14417 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14418
14419 /* Run the window-bottom-change-functions
14420 if it is possible that the text on the screen has changed
14421 (either due to modification of the text, or any other reason). */
14422 if (!current_matrix_up_to_date_p
14423 && !NILP (Vwindow_text_change_functions))
14424 {
14425 safe_run_hooks (Qwindow_text_change_functions);
14426 goto restart;
14427 }
14428
14429 beg_unchanged = BEG_UNCHANGED;
14430 end_unchanged = END_UNCHANGED;
14431
14432 SET_TEXT_POS (opoint, PT, PT_BYTE);
14433
14434 specbind (Qinhibit_point_motion_hooks, Qt);
14435
14436 buffer_unchanged_p
14437 = (!NILP (w->window_end_valid)
14438 && !current_buffer->clip_changed
14439 && XFASTINT (w->last_modified) >= MODIFF
14440 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14441
14442 /* When windows_or_buffers_changed is non-zero, we can't rely on
14443 the window end being valid, so set it to nil there. */
14444 if (windows_or_buffers_changed)
14445 {
14446 /* If window starts on a continuation line, maybe adjust the
14447 window start in case the window's width changed. */
14448 if (XMARKER (w->start)->buffer == current_buffer)
14449 compute_window_start_on_continuation_line (w);
14450
14451 w->window_end_valid = Qnil;
14452 }
14453
14454 /* Some sanity checks. */
14455 CHECK_WINDOW_END (w);
14456 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14457 abort ();
14458 if (BYTEPOS (opoint) < CHARPOS (opoint))
14459 abort ();
14460
14461 /* If %c is in mode line, update it if needed. */
14462 if (!NILP (w->column_number_displayed)
14463 /* This alternative quickly identifies a common case
14464 where no change is needed. */
14465 && !(PT == XFASTINT (w->last_point)
14466 && XFASTINT (w->last_modified) >= MODIFF
14467 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14468 && (XFASTINT (w->column_number_displayed) != current_column ()))
14469 update_mode_line = 1;
14470
14471 /* Count number of windows showing the selected buffer. An indirect
14472 buffer counts as its base buffer. */
14473 if (!just_this_one_p)
14474 {
14475 struct buffer *current_base, *window_base;
14476 current_base = current_buffer;
14477 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14478 if (current_base->base_buffer)
14479 current_base = current_base->base_buffer;
14480 if (window_base->base_buffer)
14481 window_base = window_base->base_buffer;
14482 if (current_base == window_base)
14483 buffer_shared++;
14484 }
14485
14486 /* Point refers normally to the selected window. For any other
14487 window, set up appropriate value. */
14488 if (!EQ (window, selected_window))
14489 {
14490 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14491 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14492 if (new_pt < BEGV)
14493 {
14494 new_pt = BEGV;
14495 new_pt_byte = BEGV_BYTE;
14496 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14497 }
14498 else if (new_pt > (ZV - 1))
14499 {
14500 new_pt = ZV;
14501 new_pt_byte = ZV_BYTE;
14502 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14503 }
14504
14505 /* We don't use SET_PT so that the point-motion hooks don't run. */
14506 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14507 }
14508
14509 /* If any of the character widths specified in the display table
14510 have changed, invalidate the width run cache. It's true that
14511 this may be a bit late to catch such changes, but the rest of
14512 redisplay goes (non-fatally) haywire when the display table is
14513 changed, so why should we worry about doing any better? */
14514 if (current_buffer->width_run_cache)
14515 {
14516 struct Lisp_Char_Table *disptab = buffer_display_table ();
14517
14518 if (! disptab_matches_widthtab (disptab,
14519 XVECTOR (BVAR (current_buffer, width_table))))
14520 {
14521 invalidate_region_cache (current_buffer,
14522 current_buffer->width_run_cache,
14523 BEG, Z);
14524 recompute_width_table (current_buffer, disptab);
14525 }
14526 }
14527
14528 /* If window-start is screwed up, choose a new one. */
14529 if (XMARKER (w->start)->buffer != current_buffer)
14530 goto recenter;
14531
14532 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14533
14534 /* If someone specified a new starting point but did not insist,
14535 check whether it can be used. */
14536 if (!NILP (w->optional_new_start)
14537 && CHARPOS (startp) >= BEGV
14538 && CHARPOS (startp) <= ZV)
14539 {
14540 w->optional_new_start = Qnil;
14541 start_display (&it, w, startp);
14542 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14543 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14544 if (IT_CHARPOS (it) == PT)
14545 w->force_start = Qt;
14546 /* IT may overshoot PT if text at PT is invisible. */
14547 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14548 w->force_start = Qt;
14549 }
14550
14551 force_start:
14552
14553 /* Handle case where place to start displaying has been specified,
14554 unless the specified location is outside the accessible range. */
14555 if (!NILP (w->force_start)
14556 || w->frozen_window_start_p)
14557 {
14558 /* We set this later on if we have to adjust point. */
14559 int new_vpos = -1;
14560
14561 w->force_start = Qnil;
14562 w->vscroll = 0;
14563 w->window_end_valid = Qnil;
14564
14565 /* Forget any recorded base line for line number display. */
14566 if (!buffer_unchanged_p)
14567 w->base_line_number = Qnil;
14568
14569 /* Redisplay the mode line. Select the buffer properly for that.
14570 Also, run the hook window-scroll-functions
14571 because we have scrolled. */
14572 /* Note, we do this after clearing force_start because
14573 if there's an error, it is better to forget about force_start
14574 than to get into an infinite loop calling the hook functions
14575 and having them get more errors. */
14576 if (!update_mode_line
14577 || ! NILP (Vwindow_scroll_functions))
14578 {
14579 update_mode_line = 1;
14580 w->update_mode_line = Qt;
14581 startp = run_window_scroll_functions (window, startp);
14582 }
14583
14584 w->last_modified = make_number (0);
14585 w->last_overlay_modified = make_number (0);
14586 if (CHARPOS (startp) < BEGV)
14587 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14588 else if (CHARPOS (startp) > ZV)
14589 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14590
14591 /* Redisplay, then check if cursor has been set during the
14592 redisplay. Give up if new fonts were loaded. */
14593 /* We used to issue a CHECK_MARGINS argument to try_window here,
14594 but this causes scrolling to fail when point begins inside
14595 the scroll margin (bug#148) -- cyd */
14596 if (!try_window (window, startp, 0))
14597 {
14598 w->force_start = Qt;
14599 clear_glyph_matrix (w->desired_matrix);
14600 goto need_larger_matrices;
14601 }
14602
14603 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14604 {
14605 /* If point does not appear, try to move point so it does
14606 appear. The desired matrix has been built above, so we
14607 can use it here. */
14608 new_vpos = window_box_height (w) / 2;
14609 }
14610
14611 if (!cursor_row_fully_visible_p (w, 0, 0))
14612 {
14613 /* Point does appear, but on a line partly visible at end of window.
14614 Move it back to a fully-visible line. */
14615 new_vpos = window_box_height (w);
14616 }
14617
14618 /* If we need to move point for either of the above reasons,
14619 now actually do it. */
14620 if (new_vpos >= 0)
14621 {
14622 struct glyph_row *row;
14623
14624 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14625 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14626 ++row;
14627
14628 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14629 MATRIX_ROW_START_BYTEPOS (row));
14630
14631 if (w != XWINDOW (selected_window))
14632 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14633 else if (current_buffer == old)
14634 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14635
14636 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14637
14638 /* If we are highlighting the region, then we just changed
14639 the region, so redisplay to show it. */
14640 if (!NILP (Vtransient_mark_mode)
14641 && !NILP (BVAR (current_buffer, mark_active)))
14642 {
14643 clear_glyph_matrix (w->desired_matrix);
14644 if (!try_window (window, startp, 0))
14645 goto need_larger_matrices;
14646 }
14647 }
14648
14649 #if GLYPH_DEBUG
14650 debug_method_add (w, "forced window start");
14651 #endif
14652 goto done;
14653 }
14654
14655 /* Handle case where text has not changed, only point, and it has
14656 not moved off the frame, and we are not retrying after hscroll.
14657 (current_matrix_up_to_date_p is nonzero when retrying.) */
14658 if (current_matrix_up_to_date_p
14659 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14660 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14661 {
14662 switch (rc)
14663 {
14664 case CURSOR_MOVEMENT_SUCCESS:
14665 used_current_matrix_p = 1;
14666 goto done;
14667
14668 case CURSOR_MOVEMENT_MUST_SCROLL:
14669 goto try_to_scroll;
14670
14671 default:
14672 abort ();
14673 }
14674 }
14675 /* If current starting point was originally the beginning of a line
14676 but no longer is, find a new starting point. */
14677 else if (!NILP (w->start_at_line_beg)
14678 && !(CHARPOS (startp) <= BEGV
14679 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14680 {
14681 #if GLYPH_DEBUG
14682 debug_method_add (w, "recenter 1");
14683 #endif
14684 goto recenter;
14685 }
14686
14687 /* Try scrolling with try_window_id. Value is > 0 if update has
14688 been done, it is -1 if we know that the same window start will
14689 not work. It is 0 if unsuccessful for some other reason. */
14690 else if ((tem = try_window_id (w)) != 0)
14691 {
14692 #if GLYPH_DEBUG
14693 debug_method_add (w, "try_window_id %d", tem);
14694 #endif
14695
14696 if (fonts_changed_p)
14697 goto need_larger_matrices;
14698 if (tem > 0)
14699 goto done;
14700
14701 /* Otherwise try_window_id has returned -1 which means that we
14702 don't want the alternative below this comment to execute. */
14703 }
14704 else if (CHARPOS (startp) >= BEGV
14705 && CHARPOS (startp) <= ZV
14706 && PT >= CHARPOS (startp)
14707 && (CHARPOS (startp) < ZV
14708 /* Avoid starting at end of buffer. */
14709 || CHARPOS (startp) == BEGV
14710 || (XFASTINT (w->last_modified) >= MODIFF
14711 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14712 {
14713
14714 /* If first window line is a continuation line, and window start
14715 is inside the modified region, but the first change is before
14716 current window start, we must select a new window start.
14717
14718 However, if this is the result of a down-mouse event (e.g. by
14719 extending the mouse-drag-overlay), we don't want to select a
14720 new window start, since that would change the position under
14721 the mouse, resulting in an unwanted mouse-movement rather
14722 than a simple mouse-click. */
14723 if (NILP (w->start_at_line_beg)
14724 && NILP (do_mouse_tracking)
14725 && CHARPOS (startp) > BEGV
14726 && CHARPOS (startp) > BEG + beg_unchanged
14727 && CHARPOS (startp) <= Z - end_unchanged
14728 /* Even if w->start_at_line_beg is nil, a new window may
14729 start at a line_beg, since that's how set_buffer_window
14730 sets it. So, we need to check the return value of
14731 compute_window_start_on_continuation_line. (See also
14732 bug#197). */
14733 && XMARKER (w->start)->buffer == current_buffer
14734 && compute_window_start_on_continuation_line (w))
14735 {
14736 w->force_start = Qt;
14737 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14738 goto force_start;
14739 }
14740
14741 #if GLYPH_DEBUG
14742 debug_method_add (w, "same window start");
14743 #endif
14744
14745 /* Try to redisplay starting at same place as before.
14746 If point has not moved off frame, accept the results. */
14747 if (!current_matrix_up_to_date_p
14748 /* Don't use try_window_reusing_current_matrix in this case
14749 because a window scroll function can have changed the
14750 buffer. */
14751 || !NILP (Vwindow_scroll_functions)
14752 || MINI_WINDOW_P (w)
14753 || !(used_current_matrix_p
14754 = try_window_reusing_current_matrix (w)))
14755 {
14756 IF_DEBUG (debug_method_add (w, "1"));
14757 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14758 /* -1 means we need to scroll.
14759 0 means we need new matrices, but fonts_changed_p
14760 is set in that case, so we will detect it below. */
14761 goto try_to_scroll;
14762 }
14763
14764 if (fonts_changed_p)
14765 goto need_larger_matrices;
14766
14767 if (w->cursor.vpos >= 0)
14768 {
14769 if (!just_this_one_p
14770 || current_buffer->clip_changed
14771 || BEG_UNCHANGED < CHARPOS (startp))
14772 /* Forget any recorded base line for line number display. */
14773 w->base_line_number = Qnil;
14774
14775 if (!cursor_row_fully_visible_p (w, 1, 0))
14776 {
14777 clear_glyph_matrix (w->desired_matrix);
14778 last_line_misfit = 1;
14779 }
14780 /* Drop through and scroll. */
14781 else
14782 goto done;
14783 }
14784 else
14785 clear_glyph_matrix (w->desired_matrix);
14786 }
14787
14788 try_to_scroll:
14789
14790 w->last_modified = make_number (0);
14791 w->last_overlay_modified = make_number (0);
14792
14793 /* Redisplay the mode line. Select the buffer properly for that. */
14794 if (!update_mode_line)
14795 {
14796 update_mode_line = 1;
14797 w->update_mode_line = Qt;
14798 }
14799
14800 /* Try to scroll by specified few lines. */
14801 if ((scroll_conservatively
14802 || emacs_scroll_step
14803 || temp_scroll_step
14804 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14805 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14806 && CHARPOS (startp) >= BEGV
14807 && CHARPOS (startp) <= ZV)
14808 {
14809 /* The function returns -1 if new fonts were loaded, 1 if
14810 successful, 0 if not successful. */
14811 int ss = try_scrolling (window, just_this_one_p,
14812 scroll_conservatively,
14813 emacs_scroll_step,
14814 temp_scroll_step, last_line_misfit);
14815 switch (ss)
14816 {
14817 case SCROLLING_SUCCESS:
14818 goto done;
14819
14820 case SCROLLING_NEED_LARGER_MATRICES:
14821 goto need_larger_matrices;
14822
14823 case SCROLLING_FAILED:
14824 break;
14825
14826 default:
14827 abort ();
14828 }
14829 }
14830
14831 /* Finally, just choose a place to start which positions point
14832 according to user preferences. */
14833
14834 recenter:
14835
14836 #if GLYPH_DEBUG
14837 debug_method_add (w, "recenter");
14838 #endif
14839
14840 /* w->vscroll = 0; */
14841
14842 /* Forget any previously recorded base line for line number display. */
14843 if (!buffer_unchanged_p)
14844 w->base_line_number = Qnil;
14845
14846 /* Determine the window start relative to point. */
14847 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14848 it.current_y = it.last_visible_y;
14849 if (centering_position < 0)
14850 {
14851 int margin =
14852 scroll_margin > 0
14853 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14854 : 0;
14855 EMACS_INT margin_pos = CHARPOS (startp);
14856 int scrolling_up;
14857 Lisp_Object aggressive;
14858
14859 /* If there is a scroll margin at the top of the window, find
14860 its character position. */
14861 if (margin
14862 /* Cannot call start_display if startp is not in the
14863 accessible region of the buffer. This can happen when we
14864 have just switched to a different buffer and/or changed
14865 its restriction. In that case, startp is initialized to
14866 the character position 1 (BEG) because we did not yet
14867 have chance to display the buffer even once. */
14868 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14869 {
14870 struct it it1;
14871
14872 start_display (&it1, w, startp);
14873 move_it_vertically (&it1, margin);
14874 margin_pos = IT_CHARPOS (it1);
14875 }
14876 scrolling_up = PT > margin_pos;
14877 aggressive =
14878 scrolling_up
14879 ? BVAR (current_buffer, scroll_up_aggressively)
14880 : BVAR (current_buffer, scroll_down_aggressively);
14881
14882 if (!MINI_WINDOW_P (w)
14883 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
14884 {
14885 int pt_offset = 0;
14886
14887 /* Setting scroll-conservatively overrides
14888 scroll-*-aggressively. */
14889 if (!scroll_conservatively && NUMBERP (aggressive))
14890 {
14891 double float_amount = XFLOATINT (aggressive);
14892
14893 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
14894 if (pt_offset == 0 && float_amount > 0)
14895 pt_offset = 1;
14896 if (pt_offset)
14897 margin -= 1;
14898 }
14899 /* Compute how much to move the window start backward from
14900 point so that point will be displayed where the user
14901 wants it. */
14902 if (scrolling_up)
14903 {
14904 centering_position = it.last_visible_y;
14905 if (pt_offset)
14906 centering_position -= pt_offset;
14907 centering_position -=
14908 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
14909 /* Don't let point enter the scroll margin near top of
14910 the window. */
14911 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
14912 centering_position = margin * FRAME_LINE_HEIGHT (f);
14913 }
14914 else
14915 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
14916 }
14917 else
14918 /* Set the window start half the height of the window backward
14919 from point. */
14920 centering_position = window_box_height (w) / 2;
14921 }
14922 move_it_vertically_backward (&it, centering_position);
14923
14924 xassert (IT_CHARPOS (it) >= BEGV);
14925
14926 /* The function move_it_vertically_backward may move over more
14927 than the specified y-distance. If it->w is small, e.g. a
14928 mini-buffer window, we may end up in front of the window's
14929 display area. Start displaying at the start of the line
14930 containing PT in this case. */
14931 if (it.current_y <= 0)
14932 {
14933 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14934 move_it_vertically_backward (&it, 0);
14935 it.current_y = 0;
14936 }
14937
14938 it.current_x = it.hpos = 0;
14939
14940 /* Set the window start position here explicitly, to avoid an
14941 infinite loop in case the functions in window-scroll-functions
14942 get errors. */
14943 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
14944
14945 /* Run scroll hooks. */
14946 startp = run_window_scroll_functions (window, it.current.pos);
14947
14948 /* Redisplay the window. */
14949 if (!current_matrix_up_to_date_p
14950 || windows_or_buffers_changed
14951 || cursor_type_changed
14952 /* Don't use try_window_reusing_current_matrix in this case
14953 because it can have changed the buffer. */
14954 || !NILP (Vwindow_scroll_functions)
14955 || !just_this_one_p
14956 || MINI_WINDOW_P (w)
14957 || !(used_current_matrix_p
14958 = try_window_reusing_current_matrix (w)))
14959 try_window (window, startp, 0);
14960
14961 /* If new fonts have been loaded (due to fontsets), give up. We
14962 have to start a new redisplay since we need to re-adjust glyph
14963 matrices. */
14964 if (fonts_changed_p)
14965 goto need_larger_matrices;
14966
14967 /* If cursor did not appear assume that the middle of the window is
14968 in the first line of the window. Do it again with the next line.
14969 (Imagine a window of height 100, displaying two lines of height
14970 60. Moving back 50 from it->last_visible_y will end in the first
14971 line.) */
14972 if (w->cursor.vpos < 0)
14973 {
14974 if (!NILP (w->window_end_valid)
14975 && PT >= Z - XFASTINT (w->window_end_pos))
14976 {
14977 clear_glyph_matrix (w->desired_matrix);
14978 move_it_by_lines (&it, 1);
14979 try_window (window, it.current.pos, 0);
14980 }
14981 else if (PT < IT_CHARPOS (it))
14982 {
14983 clear_glyph_matrix (w->desired_matrix);
14984 move_it_by_lines (&it, -1);
14985 try_window (window, it.current.pos, 0);
14986 }
14987 else
14988 {
14989 /* Not much we can do about it. */
14990 }
14991 }
14992
14993 /* Consider the following case: Window starts at BEGV, there is
14994 invisible, intangible text at BEGV, so that display starts at
14995 some point START > BEGV. It can happen that we are called with
14996 PT somewhere between BEGV and START. Try to handle that case. */
14997 if (w->cursor.vpos < 0)
14998 {
14999 struct glyph_row *row = w->current_matrix->rows;
15000 if (row->mode_line_p)
15001 ++row;
15002 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15003 }
15004
15005 if (!cursor_row_fully_visible_p (w, 0, 0))
15006 {
15007 /* If vscroll is enabled, disable it and try again. */
15008 if (w->vscroll)
15009 {
15010 w->vscroll = 0;
15011 clear_glyph_matrix (w->desired_matrix);
15012 goto recenter;
15013 }
15014
15015 /* If centering point failed to make the whole line visible,
15016 put point at the top instead. That has to make the whole line
15017 visible, if it can be done. */
15018 if (centering_position == 0)
15019 goto done;
15020
15021 clear_glyph_matrix (w->desired_matrix);
15022 centering_position = 0;
15023 goto recenter;
15024 }
15025
15026 done:
15027
15028 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15029 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15030 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15031 ? Qt : Qnil);
15032
15033 /* Display the mode line, if we must. */
15034 if ((update_mode_line
15035 /* If window not full width, must redo its mode line
15036 if (a) the window to its side is being redone and
15037 (b) we do a frame-based redisplay. This is a consequence
15038 of how inverted lines are drawn in frame-based redisplay. */
15039 || (!just_this_one_p
15040 && !FRAME_WINDOW_P (f)
15041 && !WINDOW_FULL_WIDTH_P (w))
15042 /* Line number to display. */
15043 || INTEGERP (w->base_line_pos)
15044 /* Column number is displayed and different from the one displayed. */
15045 || (!NILP (w->column_number_displayed)
15046 && (XFASTINT (w->column_number_displayed) != current_column ())))
15047 /* This means that the window has a mode line. */
15048 && (WINDOW_WANTS_MODELINE_P (w)
15049 || WINDOW_WANTS_HEADER_LINE_P (w)))
15050 {
15051 display_mode_lines (w);
15052
15053 /* If mode line height has changed, arrange for a thorough
15054 immediate redisplay using the correct mode line height. */
15055 if (WINDOW_WANTS_MODELINE_P (w)
15056 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15057 {
15058 fonts_changed_p = 1;
15059 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15060 = DESIRED_MODE_LINE_HEIGHT (w);
15061 }
15062
15063 /* If header line height has changed, arrange for a thorough
15064 immediate redisplay using the correct header line height. */
15065 if (WINDOW_WANTS_HEADER_LINE_P (w)
15066 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15067 {
15068 fonts_changed_p = 1;
15069 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15070 = DESIRED_HEADER_LINE_HEIGHT (w);
15071 }
15072
15073 if (fonts_changed_p)
15074 goto need_larger_matrices;
15075 }
15076
15077 if (!line_number_displayed
15078 && !BUFFERP (w->base_line_pos))
15079 {
15080 w->base_line_pos = Qnil;
15081 w->base_line_number = Qnil;
15082 }
15083
15084 finish_menu_bars:
15085
15086 /* When we reach a frame's selected window, redo the frame's menu bar. */
15087 if (update_mode_line
15088 && EQ (FRAME_SELECTED_WINDOW (f), window))
15089 {
15090 int redisplay_menu_p = 0;
15091
15092 if (FRAME_WINDOW_P (f))
15093 {
15094 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15095 || defined (HAVE_NS) || defined (USE_GTK)
15096 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15097 #else
15098 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15099 #endif
15100 }
15101 else
15102 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15103
15104 if (redisplay_menu_p)
15105 display_menu_bar (w);
15106
15107 #ifdef HAVE_WINDOW_SYSTEM
15108 if (FRAME_WINDOW_P (f))
15109 {
15110 #if defined (USE_GTK) || defined (HAVE_NS)
15111 if (FRAME_EXTERNAL_TOOL_BAR (f))
15112 redisplay_tool_bar (f);
15113 #else
15114 if (WINDOWP (f->tool_bar_window)
15115 && (FRAME_TOOL_BAR_LINES (f) > 0
15116 || !NILP (Vauto_resize_tool_bars))
15117 && redisplay_tool_bar (f))
15118 ignore_mouse_drag_p = 1;
15119 #endif
15120 }
15121 #endif
15122 }
15123
15124 #ifdef HAVE_WINDOW_SYSTEM
15125 if (FRAME_WINDOW_P (f)
15126 && update_window_fringes (w, (just_this_one_p
15127 || (!used_current_matrix_p && !overlay_arrow_seen)
15128 || w->pseudo_window_p)))
15129 {
15130 update_begin (f);
15131 BLOCK_INPUT;
15132 if (draw_window_fringes (w, 1))
15133 x_draw_vertical_border (w);
15134 UNBLOCK_INPUT;
15135 update_end (f);
15136 }
15137 #endif /* HAVE_WINDOW_SYSTEM */
15138
15139 /* We go to this label, with fonts_changed_p nonzero,
15140 if it is necessary to try again using larger glyph matrices.
15141 We have to redeem the scroll bar even in this case,
15142 because the loop in redisplay_internal expects that. */
15143 need_larger_matrices:
15144 ;
15145 finish_scroll_bars:
15146
15147 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15148 {
15149 /* Set the thumb's position and size. */
15150 set_vertical_scroll_bar (w);
15151
15152 /* Note that we actually used the scroll bar attached to this
15153 window, so it shouldn't be deleted at the end of redisplay. */
15154 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15155 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15156 }
15157
15158 /* Restore current_buffer and value of point in it. The window
15159 update may have changed the buffer, so first make sure `opoint'
15160 is still valid (Bug#6177). */
15161 if (CHARPOS (opoint) < BEGV)
15162 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15163 else if (CHARPOS (opoint) > ZV)
15164 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15165 else
15166 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15167
15168 set_buffer_internal_1 (old);
15169 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15170 shorter. This can be caused by log truncation in *Messages*. */
15171 if (CHARPOS (lpoint) <= ZV)
15172 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15173
15174 unbind_to (count, Qnil);
15175 }
15176
15177
15178 /* Build the complete desired matrix of WINDOW with a window start
15179 buffer position POS.
15180
15181 Value is 1 if successful. It is zero if fonts were loaded during
15182 redisplay which makes re-adjusting glyph matrices necessary, and -1
15183 if point would appear in the scroll margins.
15184 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15185 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15186 set in FLAGS.) */
15187
15188 int
15189 try_window (Lisp_Object window, struct text_pos pos, int flags)
15190 {
15191 struct window *w = XWINDOW (window);
15192 struct it it;
15193 struct glyph_row *last_text_row = NULL;
15194 struct frame *f = XFRAME (w->frame);
15195
15196 /* Make POS the new window start. */
15197 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15198
15199 /* Mark cursor position as unknown. No overlay arrow seen. */
15200 w->cursor.vpos = -1;
15201 overlay_arrow_seen = 0;
15202
15203 /* Initialize iterator and info to start at POS. */
15204 start_display (&it, w, pos);
15205
15206 /* Display all lines of W. */
15207 while (it.current_y < it.last_visible_y)
15208 {
15209 if (display_line (&it))
15210 last_text_row = it.glyph_row - 1;
15211 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15212 return 0;
15213 }
15214
15215 /* Don't let the cursor end in the scroll margins. */
15216 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15217 && !MINI_WINDOW_P (w))
15218 {
15219 int this_scroll_margin;
15220
15221 if (scroll_margin > 0)
15222 {
15223 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15224 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15225 }
15226 else
15227 this_scroll_margin = 0;
15228
15229 if ((w->cursor.y >= 0 /* not vscrolled */
15230 && w->cursor.y < this_scroll_margin
15231 && CHARPOS (pos) > BEGV
15232 && IT_CHARPOS (it) < ZV)
15233 /* rms: considering make_cursor_line_fully_visible_p here
15234 seems to give wrong results. We don't want to recenter
15235 when the last line is partly visible, we want to allow
15236 that case to be handled in the usual way. */
15237 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15238 {
15239 w->cursor.vpos = -1;
15240 clear_glyph_matrix (w->desired_matrix);
15241 return -1;
15242 }
15243 }
15244
15245 /* If bottom moved off end of frame, change mode line percentage. */
15246 if (XFASTINT (w->window_end_pos) <= 0
15247 && Z != IT_CHARPOS (it))
15248 w->update_mode_line = Qt;
15249
15250 /* Set window_end_pos to the offset of the last character displayed
15251 on the window from the end of current_buffer. Set
15252 window_end_vpos to its row number. */
15253 if (last_text_row)
15254 {
15255 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15256 w->window_end_bytepos
15257 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15258 w->window_end_pos
15259 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15260 w->window_end_vpos
15261 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15262 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15263 ->displays_text_p);
15264 }
15265 else
15266 {
15267 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15268 w->window_end_pos = make_number (Z - ZV);
15269 w->window_end_vpos = make_number (0);
15270 }
15271
15272 /* But that is not valid info until redisplay finishes. */
15273 w->window_end_valid = Qnil;
15274 return 1;
15275 }
15276
15277
15278 \f
15279 /************************************************************************
15280 Window redisplay reusing current matrix when buffer has not changed
15281 ************************************************************************/
15282
15283 /* Try redisplay of window W showing an unchanged buffer with a
15284 different window start than the last time it was displayed by
15285 reusing its current matrix. Value is non-zero if successful.
15286 W->start is the new window start. */
15287
15288 static int
15289 try_window_reusing_current_matrix (struct window *w)
15290 {
15291 struct frame *f = XFRAME (w->frame);
15292 struct glyph_row *bottom_row;
15293 struct it it;
15294 struct run run;
15295 struct text_pos start, new_start;
15296 int nrows_scrolled, i;
15297 struct glyph_row *last_text_row;
15298 struct glyph_row *last_reused_text_row;
15299 struct glyph_row *start_row;
15300 int start_vpos, min_y, max_y;
15301
15302 #if GLYPH_DEBUG
15303 if (inhibit_try_window_reusing)
15304 return 0;
15305 #endif
15306
15307 if (/* This function doesn't handle terminal frames. */
15308 !FRAME_WINDOW_P (f)
15309 /* Don't try to reuse the display if windows have been split
15310 or such. */
15311 || windows_or_buffers_changed
15312 || cursor_type_changed)
15313 return 0;
15314
15315 /* Can't do this if region may have changed. */
15316 if ((!NILP (Vtransient_mark_mode)
15317 && !NILP (BVAR (current_buffer, mark_active)))
15318 || !NILP (w->region_showing)
15319 || !NILP (Vshow_trailing_whitespace))
15320 return 0;
15321
15322 /* If top-line visibility has changed, give up. */
15323 if (WINDOW_WANTS_HEADER_LINE_P (w)
15324 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15325 return 0;
15326
15327 /* Give up if old or new display is scrolled vertically. We could
15328 make this function handle this, but right now it doesn't. */
15329 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15330 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15331 return 0;
15332
15333 /* The variable new_start now holds the new window start. The old
15334 start `start' can be determined from the current matrix. */
15335 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15336 start = start_row->minpos;
15337 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15338
15339 /* Clear the desired matrix for the display below. */
15340 clear_glyph_matrix (w->desired_matrix);
15341
15342 if (CHARPOS (new_start) <= CHARPOS (start))
15343 {
15344 /* Don't use this method if the display starts with an ellipsis
15345 displayed for invisible text. It's not easy to handle that case
15346 below, and it's certainly not worth the effort since this is
15347 not a frequent case. */
15348 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15349 return 0;
15350
15351 IF_DEBUG (debug_method_add (w, "twu1"));
15352
15353 /* Display up to a row that can be reused. The variable
15354 last_text_row is set to the last row displayed that displays
15355 text. Note that it.vpos == 0 if or if not there is a
15356 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15357 start_display (&it, w, new_start);
15358 w->cursor.vpos = -1;
15359 last_text_row = last_reused_text_row = NULL;
15360
15361 while (it.current_y < it.last_visible_y
15362 && !fonts_changed_p)
15363 {
15364 /* If we have reached into the characters in the START row,
15365 that means the line boundaries have changed. So we
15366 can't start copying with the row START. Maybe it will
15367 work to start copying with the following row. */
15368 while (IT_CHARPOS (it) > CHARPOS (start))
15369 {
15370 /* Advance to the next row as the "start". */
15371 start_row++;
15372 start = start_row->minpos;
15373 /* If there are no more rows to try, or just one, give up. */
15374 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15375 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15376 || CHARPOS (start) == ZV)
15377 {
15378 clear_glyph_matrix (w->desired_matrix);
15379 return 0;
15380 }
15381
15382 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15383 }
15384 /* If we have reached alignment,
15385 we can copy the rest of the rows. */
15386 if (IT_CHARPOS (it) == CHARPOS (start))
15387 break;
15388
15389 if (display_line (&it))
15390 last_text_row = it.glyph_row - 1;
15391 }
15392
15393 /* A value of current_y < last_visible_y means that we stopped
15394 at the previous window start, which in turn means that we
15395 have at least one reusable row. */
15396 if (it.current_y < it.last_visible_y)
15397 {
15398 struct glyph_row *row;
15399
15400 /* IT.vpos always starts from 0; it counts text lines. */
15401 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15402
15403 /* Find PT if not already found in the lines displayed. */
15404 if (w->cursor.vpos < 0)
15405 {
15406 int dy = it.current_y - start_row->y;
15407
15408 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15409 row = row_containing_pos (w, PT, row, NULL, dy);
15410 if (row)
15411 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15412 dy, nrows_scrolled);
15413 else
15414 {
15415 clear_glyph_matrix (w->desired_matrix);
15416 return 0;
15417 }
15418 }
15419
15420 /* Scroll the display. Do it before the current matrix is
15421 changed. The problem here is that update has not yet
15422 run, i.e. part of the current matrix is not up to date.
15423 scroll_run_hook will clear the cursor, and use the
15424 current matrix to get the height of the row the cursor is
15425 in. */
15426 run.current_y = start_row->y;
15427 run.desired_y = it.current_y;
15428 run.height = it.last_visible_y - it.current_y;
15429
15430 if (run.height > 0 && run.current_y != run.desired_y)
15431 {
15432 update_begin (f);
15433 FRAME_RIF (f)->update_window_begin_hook (w);
15434 FRAME_RIF (f)->clear_window_mouse_face (w);
15435 FRAME_RIF (f)->scroll_run_hook (w, &run);
15436 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15437 update_end (f);
15438 }
15439
15440 /* Shift current matrix down by nrows_scrolled lines. */
15441 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15442 rotate_matrix (w->current_matrix,
15443 start_vpos,
15444 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15445 nrows_scrolled);
15446
15447 /* Disable lines that must be updated. */
15448 for (i = 0; i < nrows_scrolled; ++i)
15449 (start_row + i)->enabled_p = 0;
15450
15451 /* Re-compute Y positions. */
15452 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15453 max_y = it.last_visible_y;
15454 for (row = start_row + nrows_scrolled;
15455 row < bottom_row;
15456 ++row)
15457 {
15458 row->y = it.current_y;
15459 row->visible_height = row->height;
15460
15461 if (row->y < min_y)
15462 row->visible_height -= min_y - row->y;
15463 if (row->y + row->height > max_y)
15464 row->visible_height -= row->y + row->height - max_y;
15465 row->redraw_fringe_bitmaps_p = 1;
15466
15467 it.current_y += row->height;
15468
15469 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15470 last_reused_text_row = row;
15471 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15472 break;
15473 }
15474
15475 /* Disable lines in the current matrix which are now
15476 below the window. */
15477 for (++row; row < bottom_row; ++row)
15478 row->enabled_p = row->mode_line_p = 0;
15479 }
15480
15481 /* Update window_end_pos etc.; last_reused_text_row is the last
15482 reused row from the current matrix containing text, if any.
15483 The value of last_text_row is the last displayed line
15484 containing text. */
15485 if (last_reused_text_row)
15486 {
15487 w->window_end_bytepos
15488 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15489 w->window_end_pos
15490 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15491 w->window_end_vpos
15492 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15493 w->current_matrix));
15494 }
15495 else if (last_text_row)
15496 {
15497 w->window_end_bytepos
15498 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15499 w->window_end_pos
15500 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15501 w->window_end_vpos
15502 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15503 }
15504 else
15505 {
15506 /* This window must be completely empty. */
15507 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15508 w->window_end_pos = make_number (Z - ZV);
15509 w->window_end_vpos = make_number (0);
15510 }
15511 w->window_end_valid = Qnil;
15512
15513 /* Update hint: don't try scrolling again in update_window. */
15514 w->desired_matrix->no_scrolling_p = 1;
15515
15516 #if GLYPH_DEBUG
15517 debug_method_add (w, "try_window_reusing_current_matrix 1");
15518 #endif
15519 return 1;
15520 }
15521 else if (CHARPOS (new_start) > CHARPOS (start))
15522 {
15523 struct glyph_row *pt_row, *row;
15524 struct glyph_row *first_reusable_row;
15525 struct glyph_row *first_row_to_display;
15526 int dy;
15527 int yb = window_text_bottom_y (w);
15528
15529 /* Find the row starting at new_start, if there is one. Don't
15530 reuse a partially visible line at the end. */
15531 first_reusable_row = start_row;
15532 while (first_reusable_row->enabled_p
15533 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15534 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15535 < CHARPOS (new_start)))
15536 ++first_reusable_row;
15537
15538 /* Give up if there is no row to reuse. */
15539 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15540 || !first_reusable_row->enabled_p
15541 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15542 != CHARPOS (new_start)))
15543 return 0;
15544
15545 /* We can reuse fully visible rows beginning with
15546 first_reusable_row to the end of the window. Set
15547 first_row_to_display to the first row that cannot be reused.
15548 Set pt_row to the row containing point, if there is any. */
15549 pt_row = NULL;
15550 for (first_row_to_display = first_reusable_row;
15551 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15552 ++first_row_to_display)
15553 {
15554 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15555 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15556 pt_row = first_row_to_display;
15557 }
15558
15559 /* Start displaying at the start of first_row_to_display. */
15560 xassert (first_row_to_display->y < yb);
15561 init_to_row_start (&it, w, first_row_to_display);
15562
15563 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15564 - start_vpos);
15565 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15566 - nrows_scrolled);
15567 it.current_y = (first_row_to_display->y - first_reusable_row->y
15568 + WINDOW_HEADER_LINE_HEIGHT (w));
15569
15570 /* Display lines beginning with first_row_to_display in the
15571 desired matrix. Set last_text_row to the last row displayed
15572 that displays text. */
15573 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15574 if (pt_row == NULL)
15575 w->cursor.vpos = -1;
15576 last_text_row = NULL;
15577 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15578 if (display_line (&it))
15579 last_text_row = it.glyph_row - 1;
15580
15581 /* If point is in a reused row, adjust y and vpos of the cursor
15582 position. */
15583 if (pt_row)
15584 {
15585 w->cursor.vpos -= nrows_scrolled;
15586 w->cursor.y -= first_reusable_row->y - start_row->y;
15587 }
15588
15589 /* Give up if point isn't in a row displayed or reused. (This
15590 also handles the case where w->cursor.vpos < nrows_scrolled
15591 after the calls to display_line, which can happen with scroll
15592 margins. See bug#1295.) */
15593 if (w->cursor.vpos < 0)
15594 {
15595 clear_glyph_matrix (w->desired_matrix);
15596 return 0;
15597 }
15598
15599 /* Scroll the display. */
15600 run.current_y = first_reusable_row->y;
15601 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15602 run.height = it.last_visible_y - run.current_y;
15603 dy = run.current_y - run.desired_y;
15604
15605 if (run.height)
15606 {
15607 update_begin (f);
15608 FRAME_RIF (f)->update_window_begin_hook (w);
15609 FRAME_RIF (f)->clear_window_mouse_face (w);
15610 FRAME_RIF (f)->scroll_run_hook (w, &run);
15611 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15612 update_end (f);
15613 }
15614
15615 /* Adjust Y positions of reused rows. */
15616 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15617 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15618 max_y = it.last_visible_y;
15619 for (row = first_reusable_row; row < first_row_to_display; ++row)
15620 {
15621 row->y -= dy;
15622 row->visible_height = row->height;
15623 if (row->y < min_y)
15624 row->visible_height -= min_y - row->y;
15625 if (row->y + row->height > max_y)
15626 row->visible_height -= row->y + row->height - max_y;
15627 row->redraw_fringe_bitmaps_p = 1;
15628 }
15629
15630 /* Scroll the current matrix. */
15631 xassert (nrows_scrolled > 0);
15632 rotate_matrix (w->current_matrix,
15633 start_vpos,
15634 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15635 -nrows_scrolled);
15636
15637 /* Disable rows not reused. */
15638 for (row -= nrows_scrolled; row < bottom_row; ++row)
15639 row->enabled_p = 0;
15640
15641 /* Point may have moved to a different line, so we cannot assume that
15642 the previous cursor position is valid; locate the correct row. */
15643 if (pt_row)
15644 {
15645 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15646 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15647 row++)
15648 {
15649 w->cursor.vpos++;
15650 w->cursor.y = row->y;
15651 }
15652 if (row < bottom_row)
15653 {
15654 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15655 struct glyph *end = glyph + row->used[TEXT_AREA];
15656
15657 /* Can't use this optimization with bidi-reordered glyph
15658 rows, unless cursor is already at point. */
15659 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15660 {
15661 if (!(w->cursor.hpos >= 0
15662 && w->cursor.hpos < row->used[TEXT_AREA]
15663 && BUFFERP (glyph->object)
15664 && glyph->charpos == PT))
15665 return 0;
15666 }
15667 else
15668 for (; glyph < end
15669 && (!BUFFERP (glyph->object)
15670 || glyph->charpos < PT);
15671 glyph++)
15672 {
15673 w->cursor.hpos++;
15674 w->cursor.x += glyph->pixel_width;
15675 }
15676 }
15677 }
15678
15679 /* Adjust window end. A null value of last_text_row means that
15680 the window end is in reused rows which in turn means that
15681 only its vpos can have changed. */
15682 if (last_text_row)
15683 {
15684 w->window_end_bytepos
15685 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15686 w->window_end_pos
15687 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15688 w->window_end_vpos
15689 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15690 }
15691 else
15692 {
15693 w->window_end_vpos
15694 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15695 }
15696
15697 w->window_end_valid = Qnil;
15698 w->desired_matrix->no_scrolling_p = 1;
15699
15700 #if GLYPH_DEBUG
15701 debug_method_add (w, "try_window_reusing_current_matrix 2");
15702 #endif
15703 return 1;
15704 }
15705
15706 return 0;
15707 }
15708
15709
15710 \f
15711 /************************************************************************
15712 Window redisplay reusing current matrix when buffer has changed
15713 ************************************************************************/
15714
15715 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15716 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15717 EMACS_INT *, EMACS_INT *);
15718 static struct glyph_row *
15719 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15720 struct glyph_row *);
15721
15722
15723 /* Return the last row in MATRIX displaying text. If row START is
15724 non-null, start searching with that row. IT gives the dimensions
15725 of the display. Value is null if matrix is empty; otherwise it is
15726 a pointer to the row found. */
15727
15728 static struct glyph_row *
15729 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15730 struct glyph_row *start)
15731 {
15732 struct glyph_row *row, *row_found;
15733
15734 /* Set row_found to the last row in IT->w's current matrix
15735 displaying text. The loop looks funny but think of partially
15736 visible lines. */
15737 row_found = NULL;
15738 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15739 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15740 {
15741 xassert (row->enabled_p);
15742 row_found = row;
15743 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15744 break;
15745 ++row;
15746 }
15747
15748 return row_found;
15749 }
15750
15751
15752 /* Return the last row in the current matrix of W that is not affected
15753 by changes at the start of current_buffer that occurred since W's
15754 current matrix was built. Value is null if no such row exists.
15755
15756 BEG_UNCHANGED us the number of characters unchanged at the start of
15757 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15758 first changed character in current_buffer. Characters at positions <
15759 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15760 when the current matrix was built. */
15761
15762 static struct glyph_row *
15763 find_last_unchanged_at_beg_row (struct window *w)
15764 {
15765 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15766 struct glyph_row *row;
15767 struct glyph_row *row_found = NULL;
15768 int yb = window_text_bottom_y (w);
15769
15770 /* Find the last row displaying unchanged text. */
15771 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15772 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15773 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15774 ++row)
15775 {
15776 if (/* If row ends before first_changed_pos, it is unchanged,
15777 except in some case. */
15778 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15779 /* When row ends in ZV and we write at ZV it is not
15780 unchanged. */
15781 && !row->ends_at_zv_p
15782 /* When first_changed_pos is the end of a continued line,
15783 row is not unchanged because it may be no longer
15784 continued. */
15785 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15786 && (row->continued_p
15787 || row->exact_window_width_line_p)))
15788 row_found = row;
15789
15790 /* Stop if last visible row. */
15791 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15792 break;
15793 }
15794
15795 return row_found;
15796 }
15797
15798
15799 /* Find the first glyph row in the current matrix of W that is not
15800 affected by changes at the end of current_buffer since the
15801 time W's current matrix was built.
15802
15803 Return in *DELTA the number of chars by which buffer positions in
15804 unchanged text at the end of current_buffer must be adjusted.
15805
15806 Return in *DELTA_BYTES the corresponding number of bytes.
15807
15808 Value is null if no such row exists, i.e. all rows are affected by
15809 changes. */
15810
15811 static struct glyph_row *
15812 find_first_unchanged_at_end_row (struct window *w,
15813 EMACS_INT *delta, EMACS_INT *delta_bytes)
15814 {
15815 struct glyph_row *row;
15816 struct glyph_row *row_found = NULL;
15817
15818 *delta = *delta_bytes = 0;
15819
15820 /* Display must not have been paused, otherwise the current matrix
15821 is not up to date. */
15822 eassert (!NILP (w->window_end_valid));
15823
15824 /* A value of window_end_pos >= END_UNCHANGED means that the window
15825 end is in the range of changed text. If so, there is no
15826 unchanged row at the end of W's current matrix. */
15827 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15828 return NULL;
15829
15830 /* Set row to the last row in W's current matrix displaying text. */
15831 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15832
15833 /* If matrix is entirely empty, no unchanged row exists. */
15834 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15835 {
15836 /* The value of row is the last glyph row in the matrix having a
15837 meaningful buffer position in it. The end position of row
15838 corresponds to window_end_pos. This allows us to translate
15839 buffer positions in the current matrix to current buffer
15840 positions for characters not in changed text. */
15841 EMACS_INT Z_old =
15842 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15843 EMACS_INT Z_BYTE_old =
15844 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15845 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15846 struct glyph_row *first_text_row
15847 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15848
15849 *delta = Z - Z_old;
15850 *delta_bytes = Z_BYTE - Z_BYTE_old;
15851
15852 /* Set last_unchanged_pos to the buffer position of the last
15853 character in the buffer that has not been changed. Z is the
15854 index + 1 of the last character in current_buffer, i.e. by
15855 subtracting END_UNCHANGED we get the index of the last
15856 unchanged character, and we have to add BEG to get its buffer
15857 position. */
15858 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15859 last_unchanged_pos_old = last_unchanged_pos - *delta;
15860
15861 /* Search backward from ROW for a row displaying a line that
15862 starts at a minimum position >= last_unchanged_pos_old. */
15863 for (; row > first_text_row; --row)
15864 {
15865 /* This used to abort, but it can happen.
15866 It is ok to just stop the search instead here. KFS. */
15867 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15868 break;
15869
15870 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15871 row_found = row;
15872 }
15873 }
15874
15875 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15876
15877 return row_found;
15878 }
15879
15880
15881 /* Make sure that glyph rows in the current matrix of window W
15882 reference the same glyph memory as corresponding rows in the
15883 frame's frame matrix. This function is called after scrolling W's
15884 current matrix on a terminal frame in try_window_id and
15885 try_window_reusing_current_matrix. */
15886
15887 static void
15888 sync_frame_with_window_matrix_rows (struct window *w)
15889 {
15890 struct frame *f = XFRAME (w->frame);
15891 struct glyph_row *window_row, *window_row_end, *frame_row;
15892
15893 /* Preconditions: W must be a leaf window and full-width. Its frame
15894 must have a frame matrix. */
15895 xassert (NILP (w->hchild) && NILP (w->vchild));
15896 xassert (WINDOW_FULL_WIDTH_P (w));
15897 xassert (!FRAME_WINDOW_P (f));
15898
15899 /* If W is a full-width window, glyph pointers in W's current matrix
15900 have, by definition, to be the same as glyph pointers in the
15901 corresponding frame matrix. Note that frame matrices have no
15902 marginal areas (see build_frame_matrix). */
15903 window_row = w->current_matrix->rows;
15904 window_row_end = window_row + w->current_matrix->nrows;
15905 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
15906 while (window_row < window_row_end)
15907 {
15908 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
15909 struct glyph *end = window_row->glyphs[LAST_AREA];
15910
15911 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
15912 frame_row->glyphs[TEXT_AREA] = start;
15913 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
15914 frame_row->glyphs[LAST_AREA] = end;
15915
15916 /* Disable frame rows whose corresponding window rows have
15917 been disabled in try_window_id. */
15918 if (!window_row->enabled_p)
15919 frame_row->enabled_p = 0;
15920
15921 ++window_row, ++frame_row;
15922 }
15923 }
15924
15925
15926 /* Find the glyph row in window W containing CHARPOS. Consider all
15927 rows between START and END (not inclusive). END null means search
15928 all rows to the end of the display area of W. Value is the row
15929 containing CHARPOS or null. */
15930
15931 struct glyph_row *
15932 row_containing_pos (struct window *w, EMACS_INT charpos,
15933 struct glyph_row *start, struct glyph_row *end, int dy)
15934 {
15935 struct glyph_row *row = start;
15936 struct glyph_row *best_row = NULL;
15937 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
15938 int last_y;
15939
15940 /* If we happen to start on a header-line, skip that. */
15941 if (row->mode_line_p)
15942 ++row;
15943
15944 if ((end && row >= end) || !row->enabled_p)
15945 return NULL;
15946
15947 last_y = window_text_bottom_y (w) - dy;
15948
15949 while (1)
15950 {
15951 /* Give up if we have gone too far. */
15952 if (end && row >= end)
15953 return NULL;
15954 /* This formerly returned if they were equal.
15955 I think that both quantities are of a "last plus one" type;
15956 if so, when they are equal, the row is within the screen. -- rms. */
15957 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
15958 return NULL;
15959
15960 /* If it is in this row, return this row. */
15961 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
15962 || (MATRIX_ROW_END_CHARPOS (row) == charpos
15963 /* The end position of a row equals the start
15964 position of the next row. If CHARPOS is there, we
15965 would rather display it in the next line, except
15966 when this line ends in ZV. */
15967 && !row->ends_at_zv_p
15968 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15969 && charpos >= MATRIX_ROW_START_CHARPOS (row))
15970 {
15971 struct glyph *g;
15972
15973 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
15974 || (!best_row && !row->continued_p))
15975 return row;
15976 /* In bidi-reordered rows, there could be several rows
15977 occluding point, all of them belonging to the same
15978 continued line. We need to find the row which fits
15979 CHARPOS the best. */
15980 for (g = row->glyphs[TEXT_AREA];
15981 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15982 g++)
15983 {
15984 if (!STRINGP (g->object))
15985 {
15986 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
15987 {
15988 mindif = eabs (g->charpos - charpos);
15989 best_row = row;
15990 /* Exact match always wins. */
15991 if (mindif == 0)
15992 return best_row;
15993 }
15994 }
15995 }
15996 }
15997 else if (best_row && !row->continued_p)
15998 return best_row;
15999 ++row;
16000 }
16001 }
16002
16003
16004 /* Try to redisplay window W by reusing its existing display. W's
16005 current matrix must be up to date when this function is called,
16006 i.e. window_end_valid must not be nil.
16007
16008 Value is
16009
16010 1 if display has been updated
16011 0 if otherwise unsuccessful
16012 -1 if redisplay with same window start is known not to succeed
16013
16014 The following steps are performed:
16015
16016 1. Find the last row in the current matrix of W that is not
16017 affected by changes at the start of current_buffer. If no such row
16018 is found, give up.
16019
16020 2. Find the first row in W's current matrix that is not affected by
16021 changes at the end of current_buffer. Maybe there is no such row.
16022
16023 3. Display lines beginning with the row + 1 found in step 1 to the
16024 row found in step 2 or, if step 2 didn't find a row, to the end of
16025 the window.
16026
16027 4. If cursor is not known to appear on the window, give up.
16028
16029 5. If display stopped at the row found in step 2, scroll the
16030 display and current matrix as needed.
16031
16032 6. Maybe display some lines at the end of W, if we must. This can
16033 happen under various circumstances, like a partially visible line
16034 becoming fully visible, or because newly displayed lines are displayed
16035 in smaller font sizes.
16036
16037 7. Update W's window end information. */
16038
16039 static int
16040 try_window_id (struct window *w)
16041 {
16042 struct frame *f = XFRAME (w->frame);
16043 struct glyph_matrix *current_matrix = w->current_matrix;
16044 struct glyph_matrix *desired_matrix = w->desired_matrix;
16045 struct glyph_row *last_unchanged_at_beg_row;
16046 struct glyph_row *first_unchanged_at_end_row;
16047 struct glyph_row *row;
16048 struct glyph_row *bottom_row;
16049 int bottom_vpos;
16050 struct it it;
16051 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16052 int dvpos, dy;
16053 struct text_pos start_pos;
16054 struct run run;
16055 int first_unchanged_at_end_vpos = 0;
16056 struct glyph_row *last_text_row, *last_text_row_at_end;
16057 struct text_pos start;
16058 EMACS_INT first_changed_charpos, last_changed_charpos;
16059
16060 #if GLYPH_DEBUG
16061 if (inhibit_try_window_id)
16062 return 0;
16063 #endif
16064
16065 /* This is handy for debugging. */
16066 #if 0
16067 #define GIVE_UP(X) \
16068 do { \
16069 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16070 return 0; \
16071 } while (0)
16072 #else
16073 #define GIVE_UP(X) return 0
16074 #endif
16075
16076 SET_TEXT_POS_FROM_MARKER (start, w->start);
16077
16078 /* Don't use this for mini-windows because these can show
16079 messages and mini-buffers, and we don't handle that here. */
16080 if (MINI_WINDOW_P (w))
16081 GIVE_UP (1);
16082
16083 /* This flag is used to prevent redisplay optimizations. */
16084 if (windows_or_buffers_changed || cursor_type_changed)
16085 GIVE_UP (2);
16086
16087 /* Verify that narrowing has not changed.
16088 Also verify that we were not told to prevent redisplay optimizations.
16089 It would be nice to further
16090 reduce the number of cases where this prevents try_window_id. */
16091 if (current_buffer->clip_changed
16092 || current_buffer->prevent_redisplay_optimizations_p)
16093 GIVE_UP (3);
16094
16095 /* Window must either use window-based redisplay or be full width. */
16096 if (!FRAME_WINDOW_P (f)
16097 && (!FRAME_LINE_INS_DEL_OK (f)
16098 || !WINDOW_FULL_WIDTH_P (w)))
16099 GIVE_UP (4);
16100
16101 /* Give up if point is known NOT to appear in W. */
16102 if (PT < CHARPOS (start))
16103 GIVE_UP (5);
16104
16105 /* Another way to prevent redisplay optimizations. */
16106 if (XFASTINT (w->last_modified) == 0)
16107 GIVE_UP (6);
16108
16109 /* Verify that window is not hscrolled. */
16110 if (XFASTINT (w->hscroll) != 0)
16111 GIVE_UP (7);
16112
16113 /* Verify that display wasn't paused. */
16114 if (NILP (w->window_end_valid))
16115 GIVE_UP (8);
16116
16117 /* Can't use this if highlighting a region because a cursor movement
16118 will do more than just set the cursor. */
16119 if (!NILP (Vtransient_mark_mode)
16120 && !NILP (BVAR (current_buffer, mark_active)))
16121 GIVE_UP (9);
16122
16123 /* Likewise if highlighting trailing whitespace. */
16124 if (!NILP (Vshow_trailing_whitespace))
16125 GIVE_UP (11);
16126
16127 /* Likewise if showing a region. */
16128 if (!NILP (w->region_showing))
16129 GIVE_UP (10);
16130
16131 /* Can't use this if overlay arrow position and/or string have
16132 changed. */
16133 if (overlay_arrows_changed_p ())
16134 GIVE_UP (12);
16135
16136 /* When word-wrap is on, adding a space to the first word of a
16137 wrapped line can change the wrap position, altering the line
16138 above it. It might be worthwhile to handle this more
16139 intelligently, but for now just redisplay from scratch. */
16140 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16141 GIVE_UP (21);
16142
16143 /* Under bidi reordering, adding or deleting a character in the
16144 beginning of a paragraph, before the first strong directional
16145 character, can change the base direction of the paragraph (unless
16146 the buffer specifies a fixed paragraph direction), which will
16147 require to redisplay the whole paragraph. It might be worthwhile
16148 to find the paragraph limits and widen the range of redisplayed
16149 lines to that, but for now just give up this optimization and
16150 redisplay from scratch. */
16151 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16152 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16153 GIVE_UP (22);
16154
16155 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16156 only if buffer has really changed. The reason is that the gap is
16157 initially at Z for freshly visited files. The code below would
16158 set end_unchanged to 0 in that case. */
16159 if (MODIFF > SAVE_MODIFF
16160 /* This seems to happen sometimes after saving a buffer. */
16161 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16162 {
16163 if (GPT - BEG < BEG_UNCHANGED)
16164 BEG_UNCHANGED = GPT - BEG;
16165 if (Z - GPT < END_UNCHANGED)
16166 END_UNCHANGED = Z - GPT;
16167 }
16168
16169 /* The position of the first and last character that has been changed. */
16170 first_changed_charpos = BEG + BEG_UNCHANGED;
16171 last_changed_charpos = Z - END_UNCHANGED;
16172
16173 /* If window starts after a line end, and the last change is in
16174 front of that newline, then changes don't affect the display.
16175 This case happens with stealth-fontification. Note that although
16176 the display is unchanged, glyph positions in the matrix have to
16177 be adjusted, of course. */
16178 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16179 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16180 && ((last_changed_charpos < CHARPOS (start)
16181 && CHARPOS (start) == BEGV)
16182 || (last_changed_charpos < CHARPOS (start) - 1
16183 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16184 {
16185 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16186 struct glyph_row *r0;
16187
16188 /* Compute how many chars/bytes have been added to or removed
16189 from the buffer. */
16190 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16191 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16192 Z_delta = Z - Z_old;
16193 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16194
16195 /* Give up if PT is not in the window. Note that it already has
16196 been checked at the start of try_window_id that PT is not in
16197 front of the window start. */
16198 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16199 GIVE_UP (13);
16200
16201 /* If window start is unchanged, we can reuse the whole matrix
16202 as is, after adjusting glyph positions. No need to compute
16203 the window end again, since its offset from Z hasn't changed. */
16204 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16205 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16206 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16207 /* PT must not be in a partially visible line. */
16208 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16209 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16210 {
16211 /* Adjust positions in the glyph matrix. */
16212 if (Z_delta || Z_delta_bytes)
16213 {
16214 struct glyph_row *r1
16215 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16216 increment_matrix_positions (w->current_matrix,
16217 MATRIX_ROW_VPOS (r0, current_matrix),
16218 MATRIX_ROW_VPOS (r1, current_matrix),
16219 Z_delta, Z_delta_bytes);
16220 }
16221
16222 /* Set the cursor. */
16223 row = row_containing_pos (w, PT, r0, NULL, 0);
16224 if (row)
16225 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16226 else
16227 abort ();
16228 return 1;
16229 }
16230 }
16231
16232 /* Handle the case that changes are all below what is displayed in
16233 the window, and that PT is in the window. This shortcut cannot
16234 be taken if ZV is visible in the window, and text has been added
16235 there that is visible in the window. */
16236 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16237 /* ZV is not visible in the window, or there are no
16238 changes at ZV, actually. */
16239 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16240 || first_changed_charpos == last_changed_charpos))
16241 {
16242 struct glyph_row *r0;
16243
16244 /* Give up if PT is not in the window. Note that it already has
16245 been checked at the start of try_window_id that PT is not in
16246 front of the window start. */
16247 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16248 GIVE_UP (14);
16249
16250 /* If window start is unchanged, we can reuse the whole matrix
16251 as is, without changing glyph positions since no text has
16252 been added/removed in front of the window end. */
16253 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16254 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16255 /* PT must not be in a partially visible line. */
16256 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16257 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16258 {
16259 /* We have to compute the window end anew since text
16260 could have been added/removed after it. */
16261 w->window_end_pos
16262 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16263 w->window_end_bytepos
16264 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16265
16266 /* Set the cursor. */
16267 row = row_containing_pos (w, PT, r0, NULL, 0);
16268 if (row)
16269 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16270 else
16271 abort ();
16272 return 2;
16273 }
16274 }
16275
16276 /* Give up if window start is in the changed area.
16277
16278 The condition used to read
16279
16280 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16281
16282 but why that was tested escapes me at the moment. */
16283 if (CHARPOS (start) >= first_changed_charpos
16284 && CHARPOS (start) <= last_changed_charpos)
16285 GIVE_UP (15);
16286
16287 /* Check that window start agrees with the start of the first glyph
16288 row in its current matrix. Check this after we know the window
16289 start is not in changed text, otherwise positions would not be
16290 comparable. */
16291 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16292 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16293 GIVE_UP (16);
16294
16295 /* Give up if the window ends in strings. Overlay strings
16296 at the end are difficult to handle, so don't try. */
16297 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16298 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16299 GIVE_UP (20);
16300
16301 /* Compute the position at which we have to start displaying new
16302 lines. Some of the lines at the top of the window might be
16303 reusable because they are not displaying changed text. Find the
16304 last row in W's current matrix not affected by changes at the
16305 start of current_buffer. Value is null if changes start in the
16306 first line of window. */
16307 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16308 if (last_unchanged_at_beg_row)
16309 {
16310 /* Avoid starting to display in the moddle of a character, a TAB
16311 for instance. This is easier than to set up the iterator
16312 exactly, and it's not a frequent case, so the additional
16313 effort wouldn't really pay off. */
16314 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16315 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16316 && last_unchanged_at_beg_row > w->current_matrix->rows)
16317 --last_unchanged_at_beg_row;
16318
16319 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16320 GIVE_UP (17);
16321
16322 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16323 GIVE_UP (18);
16324 start_pos = it.current.pos;
16325
16326 /* Start displaying new lines in the desired matrix at the same
16327 vpos we would use in the current matrix, i.e. below
16328 last_unchanged_at_beg_row. */
16329 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16330 current_matrix);
16331 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16332 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16333
16334 xassert (it.hpos == 0 && it.current_x == 0);
16335 }
16336 else
16337 {
16338 /* There are no reusable lines at the start of the window.
16339 Start displaying in the first text line. */
16340 start_display (&it, w, start);
16341 it.vpos = it.first_vpos;
16342 start_pos = it.current.pos;
16343 }
16344
16345 /* Find the first row that is not affected by changes at the end of
16346 the buffer. Value will be null if there is no unchanged row, in
16347 which case we must redisplay to the end of the window. delta
16348 will be set to the value by which buffer positions beginning with
16349 first_unchanged_at_end_row have to be adjusted due to text
16350 changes. */
16351 first_unchanged_at_end_row
16352 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16353 IF_DEBUG (debug_delta = delta);
16354 IF_DEBUG (debug_delta_bytes = delta_bytes);
16355
16356 /* Set stop_pos to the buffer position up to which we will have to
16357 display new lines. If first_unchanged_at_end_row != NULL, this
16358 is the buffer position of the start of the line displayed in that
16359 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16360 that we don't stop at a buffer position. */
16361 stop_pos = 0;
16362 if (first_unchanged_at_end_row)
16363 {
16364 xassert (last_unchanged_at_beg_row == NULL
16365 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16366
16367 /* If this is a continuation line, move forward to the next one
16368 that isn't. Changes in lines above affect this line.
16369 Caution: this may move first_unchanged_at_end_row to a row
16370 not displaying text. */
16371 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16372 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16373 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16374 < it.last_visible_y))
16375 ++first_unchanged_at_end_row;
16376
16377 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16378 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16379 >= it.last_visible_y))
16380 first_unchanged_at_end_row = NULL;
16381 else
16382 {
16383 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16384 + delta);
16385 first_unchanged_at_end_vpos
16386 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16387 xassert (stop_pos >= Z - END_UNCHANGED);
16388 }
16389 }
16390 else if (last_unchanged_at_beg_row == NULL)
16391 GIVE_UP (19);
16392
16393
16394 #if GLYPH_DEBUG
16395
16396 /* Either there is no unchanged row at the end, or the one we have
16397 now displays text. This is a necessary condition for the window
16398 end pos calculation at the end of this function. */
16399 xassert (first_unchanged_at_end_row == NULL
16400 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16401
16402 debug_last_unchanged_at_beg_vpos
16403 = (last_unchanged_at_beg_row
16404 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16405 : -1);
16406 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16407
16408 #endif /* GLYPH_DEBUG != 0 */
16409
16410
16411 /* Display new lines. Set last_text_row to the last new line
16412 displayed which has text on it, i.e. might end up as being the
16413 line where the window_end_vpos is. */
16414 w->cursor.vpos = -1;
16415 last_text_row = NULL;
16416 overlay_arrow_seen = 0;
16417 while (it.current_y < it.last_visible_y
16418 && !fonts_changed_p
16419 && (first_unchanged_at_end_row == NULL
16420 || IT_CHARPOS (it) < stop_pos))
16421 {
16422 if (display_line (&it))
16423 last_text_row = it.glyph_row - 1;
16424 }
16425
16426 if (fonts_changed_p)
16427 return -1;
16428
16429
16430 /* Compute differences in buffer positions, y-positions etc. for
16431 lines reused at the bottom of the window. Compute what we can
16432 scroll. */
16433 if (first_unchanged_at_end_row
16434 /* No lines reused because we displayed everything up to the
16435 bottom of the window. */
16436 && it.current_y < it.last_visible_y)
16437 {
16438 dvpos = (it.vpos
16439 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16440 current_matrix));
16441 dy = it.current_y - first_unchanged_at_end_row->y;
16442 run.current_y = first_unchanged_at_end_row->y;
16443 run.desired_y = run.current_y + dy;
16444 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16445 }
16446 else
16447 {
16448 delta = delta_bytes = dvpos = dy
16449 = run.current_y = run.desired_y = run.height = 0;
16450 first_unchanged_at_end_row = NULL;
16451 }
16452 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16453
16454
16455 /* Find the cursor if not already found. We have to decide whether
16456 PT will appear on this window (it sometimes doesn't, but this is
16457 not a very frequent case.) This decision has to be made before
16458 the current matrix is altered. A value of cursor.vpos < 0 means
16459 that PT is either in one of the lines beginning at
16460 first_unchanged_at_end_row or below the window. Don't care for
16461 lines that might be displayed later at the window end; as
16462 mentioned, this is not a frequent case. */
16463 if (w->cursor.vpos < 0)
16464 {
16465 /* Cursor in unchanged rows at the top? */
16466 if (PT < CHARPOS (start_pos)
16467 && last_unchanged_at_beg_row)
16468 {
16469 row = row_containing_pos (w, PT,
16470 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16471 last_unchanged_at_beg_row + 1, 0);
16472 if (row)
16473 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16474 }
16475
16476 /* Start from first_unchanged_at_end_row looking for PT. */
16477 else if (first_unchanged_at_end_row)
16478 {
16479 row = row_containing_pos (w, PT - delta,
16480 first_unchanged_at_end_row, NULL, 0);
16481 if (row)
16482 set_cursor_from_row (w, row, w->current_matrix, delta,
16483 delta_bytes, dy, dvpos);
16484 }
16485
16486 /* Give up if cursor was not found. */
16487 if (w->cursor.vpos < 0)
16488 {
16489 clear_glyph_matrix (w->desired_matrix);
16490 return -1;
16491 }
16492 }
16493
16494 /* Don't let the cursor end in the scroll margins. */
16495 {
16496 int this_scroll_margin, cursor_height;
16497
16498 this_scroll_margin = max (0, scroll_margin);
16499 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16500 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16501 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16502
16503 if ((w->cursor.y < this_scroll_margin
16504 && CHARPOS (start) > BEGV)
16505 /* Old redisplay didn't take scroll margin into account at the bottom,
16506 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16507 || (w->cursor.y + (make_cursor_line_fully_visible_p
16508 ? cursor_height + this_scroll_margin
16509 : 1)) > it.last_visible_y)
16510 {
16511 w->cursor.vpos = -1;
16512 clear_glyph_matrix (w->desired_matrix);
16513 return -1;
16514 }
16515 }
16516
16517 /* Scroll the display. Do it before changing the current matrix so
16518 that xterm.c doesn't get confused about where the cursor glyph is
16519 found. */
16520 if (dy && run.height)
16521 {
16522 update_begin (f);
16523
16524 if (FRAME_WINDOW_P (f))
16525 {
16526 FRAME_RIF (f)->update_window_begin_hook (w);
16527 FRAME_RIF (f)->clear_window_mouse_face (w);
16528 FRAME_RIF (f)->scroll_run_hook (w, &run);
16529 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16530 }
16531 else
16532 {
16533 /* Terminal frame. In this case, dvpos gives the number of
16534 lines to scroll by; dvpos < 0 means scroll up. */
16535 int from_vpos
16536 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16537 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16538 int end = (WINDOW_TOP_EDGE_LINE (w)
16539 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16540 + window_internal_height (w));
16541
16542 #if defined (HAVE_GPM) || defined (MSDOS)
16543 x_clear_window_mouse_face (w);
16544 #endif
16545 /* Perform the operation on the screen. */
16546 if (dvpos > 0)
16547 {
16548 /* Scroll last_unchanged_at_beg_row to the end of the
16549 window down dvpos lines. */
16550 set_terminal_window (f, end);
16551
16552 /* On dumb terminals delete dvpos lines at the end
16553 before inserting dvpos empty lines. */
16554 if (!FRAME_SCROLL_REGION_OK (f))
16555 ins_del_lines (f, end - dvpos, -dvpos);
16556
16557 /* Insert dvpos empty lines in front of
16558 last_unchanged_at_beg_row. */
16559 ins_del_lines (f, from, dvpos);
16560 }
16561 else if (dvpos < 0)
16562 {
16563 /* Scroll up last_unchanged_at_beg_vpos to the end of
16564 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16565 set_terminal_window (f, end);
16566
16567 /* Delete dvpos lines in front of
16568 last_unchanged_at_beg_vpos. ins_del_lines will set
16569 the cursor to the given vpos and emit |dvpos| delete
16570 line sequences. */
16571 ins_del_lines (f, from + dvpos, dvpos);
16572
16573 /* On a dumb terminal insert dvpos empty lines at the
16574 end. */
16575 if (!FRAME_SCROLL_REGION_OK (f))
16576 ins_del_lines (f, end + dvpos, -dvpos);
16577 }
16578
16579 set_terminal_window (f, 0);
16580 }
16581
16582 update_end (f);
16583 }
16584
16585 /* Shift reused rows of the current matrix to the right position.
16586 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16587 text. */
16588 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16589 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16590 if (dvpos < 0)
16591 {
16592 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16593 bottom_vpos, dvpos);
16594 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16595 bottom_vpos, 0);
16596 }
16597 else if (dvpos > 0)
16598 {
16599 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16600 bottom_vpos, dvpos);
16601 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16602 first_unchanged_at_end_vpos + dvpos, 0);
16603 }
16604
16605 /* For frame-based redisplay, make sure that current frame and window
16606 matrix are in sync with respect to glyph memory. */
16607 if (!FRAME_WINDOW_P (f))
16608 sync_frame_with_window_matrix_rows (w);
16609
16610 /* Adjust buffer positions in reused rows. */
16611 if (delta || delta_bytes)
16612 increment_matrix_positions (current_matrix,
16613 first_unchanged_at_end_vpos + dvpos,
16614 bottom_vpos, delta, delta_bytes);
16615
16616 /* Adjust Y positions. */
16617 if (dy)
16618 shift_glyph_matrix (w, current_matrix,
16619 first_unchanged_at_end_vpos + dvpos,
16620 bottom_vpos, dy);
16621
16622 if (first_unchanged_at_end_row)
16623 {
16624 first_unchanged_at_end_row += dvpos;
16625 if (first_unchanged_at_end_row->y >= it.last_visible_y
16626 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16627 first_unchanged_at_end_row = NULL;
16628 }
16629
16630 /* If scrolling up, there may be some lines to display at the end of
16631 the window. */
16632 last_text_row_at_end = NULL;
16633 if (dy < 0)
16634 {
16635 /* Scrolling up can leave for example a partially visible line
16636 at the end of the window to be redisplayed. */
16637 /* Set last_row to the glyph row in the current matrix where the
16638 window end line is found. It has been moved up or down in
16639 the matrix by dvpos. */
16640 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16641 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16642
16643 /* If last_row is the window end line, it should display text. */
16644 xassert (last_row->displays_text_p);
16645
16646 /* If window end line was partially visible before, begin
16647 displaying at that line. Otherwise begin displaying with the
16648 line following it. */
16649 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16650 {
16651 init_to_row_start (&it, w, last_row);
16652 it.vpos = last_vpos;
16653 it.current_y = last_row->y;
16654 }
16655 else
16656 {
16657 init_to_row_end (&it, w, last_row);
16658 it.vpos = 1 + last_vpos;
16659 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16660 ++last_row;
16661 }
16662
16663 /* We may start in a continuation line. If so, we have to
16664 get the right continuation_lines_width and current_x. */
16665 it.continuation_lines_width = last_row->continuation_lines_width;
16666 it.hpos = it.current_x = 0;
16667
16668 /* Display the rest of the lines at the window end. */
16669 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16670 while (it.current_y < it.last_visible_y
16671 && !fonts_changed_p)
16672 {
16673 /* Is it always sure that the display agrees with lines in
16674 the current matrix? I don't think so, so we mark rows
16675 displayed invalid in the current matrix by setting their
16676 enabled_p flag to zero. */
16677 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16678 if (display_line (&it))
16679 last_text_row_at_end = it.glyph_row - 1;
16680 }
16681 }
16682
16683 /* Update window_end_pos and window_end_vpos. */
16684 if (first_unchanged_at_end_row
16685 && !last_text_row_at_end)
16686 {
16687 /* Window end line if one of the preserved rows from the current
16688 matrix. Set row to the last row displaying text in current
16689 matrix starting at first_unchanged_at_end_row, after
16690 scrolling. */
16691 xassert (first_unchanged_at_end_row->displays_text_p);
16692 row = find_last_row_displaying_text (w->current_matrix, &it,
16693 first_unchanged_at_end_row);
16694 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16695
16696 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16697 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16698 w->window_end_vpos
16699 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16700 xassert (w->window_end_bytepos >= 0);
16701 IF_DEBUG (debug_method_add (w, "A"));
16702 }
16703 else if (last_text_row_at_end)
16704 {
16705 w->window_end_pos
16706 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16707 w->window_end_bytepos
16708 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16709 w->window_end_vpos
16710 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16711 xassert (w->window_end_bytepos >= 0);
16712 IF_DEBUG (debug_method_add (w, "B"));
16713 }
16714 else if (last_text_row)
16715 {
16716 /* We have displayed either to the end of the window or at the
16717 end of the window, i.e. the last row with text is to be found
16718 in the desired matrix. */
16719 w->window_end_pos
16720 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16721 w->window_end_bytepos
16722 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16723 w->window_end_vpos
16724 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16725 xassert (w->window_end_bytepos >= 0);
16726 }
16727 else if (first_unchanged_at_end_row == NULL
16728 && last_text_row == NULL
16729 && last_text_row_at_end == NULL)
16730 {
16731 /* Displayed to end of window, but no line containing text was
16732 displayed. Lines were deleted at the end of the window. */
16733 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16734 int vpos = XFASTINT (w->window_end_vpos);
16735 struct glyph_row *current_row = current_matrix->rows + vpos;
16736 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16737
16738 for (row = NULL;
16739 row == NULL && vpos >= first_vpos;
16740 --vpos, --current_row, --desired_row)
16741 {
16742 if (desired_row->enabled_p)
16743 {
16744 if (desired_row->displays_text_p)
16745 row = desired_row;
16746 }
16747 else if (current_row->displays_text_p)
16748 row = current_row;
16749 }
16750
16751 xassert (row != NULL);
16752 w->window_end_vpos = make_number (vpos + 1);
16753 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16754 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16755 xassert (w->window_end_bytepos >= 0);
16756 IF_DEBUG (debug_method_add (w, "C"));
16757 }
16758 else
16759 abort ();
16760
16761 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16762 debug_end_vpos = XFASTINT (w->window_end_vpos));
16763
16764 /* Record that display has not been completed. */
16765 w->window_end_valid = Qnil;
16766 w->desired_matrix->no_scrolling_p = 1;
16767 return 3;
16768
16769 #undef GIVE_UP
16770 }
16771
16772
16773 \f
16774 /***********************************************************************
16775 More debugging support
16776 ***********************************************************************/
16777
16778 #if GLYPH_DEBUG
16779
16780 void dump_glyph_row (struct glyph_row *, int, int);
16781 void dump_glyph_matrix (struct glyph_matrix *, int);
16782 void dump_glyph (struct glyph_row *, struct glyph *, int);
16783
16784
16785 /* Dump the contents of glyph matrix MATRIX on stderr.
16786
16787 GLYPHS 0 means don't show glyph contents.
16788 GLYPHS 1 means show glyphs in short form
16789 GLYPHS > 1 means show glyphs in long form. */
16790
16791 void
16792 dump_glyph_matrix (matrix, glyphs)
16793 struct glyph_matrix *matrix;
16794 int glyphs;
16795 {
16796 int i;
16797 for (i = 0; i < matrix->nrows; ++i)
16798 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16799 }
16800
16801
16802 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16803 the glyph row and area where the glyph comes from. */
16804
16805 void
16806 dump_glyph (row, glyph, area)
16807 struct glyph_row *row;
16808 struct glyph *glyph;
16809 int area;
16810 {
16811 if (glyph->type == CHAR_GLYPH)
16812 {
16813 fprintf (stderr,
16814 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16815 glyph - row->glyphs[TEXT_AREA],
16816 'C',
16817 glyph->charpos,
16818 (BUFFERP (glyph->object)
16819 ? 'B'
16820 : (STRINGP (glyph->object)
16821 ? 'S'
16822 : '-')),
16823 glyph->pixel_width,
16824 glyph->u.ch,
16825 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16826 ? glyph->u.ch
16827 : '.'),
16828 glyph->face_id,
16829 glyph->left_box_line_p,
16830 glyph->right_box_line_p);
16831 }
16832 else if (glyph->type == STRETCH_GLYPH)
16833 {
16834 fprintf (stderr,
16835 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16836 glyph - row->glyphs[TEXT_AREA],
16837 'S',
16838 glyph->charpos,
16839 (BUFFERP (glyph->object)
16840 ? 'B'
16841 : (STRINGP (glyph->object)
16842 ? 'S'
16843 : '-')),
16844 glyph->pixel_width,
16845 0,
16846 '.',
16847 glyph->face_id,
16848 glyph->left_box_line_p,
16849 glyph->right_box_line_p);
16850 }
16851 else if (glyph->type == IMAGE_GLYPH)
16852 {
16853 fprintf (stderr,
16854 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16855 glyph - row->glyphs[TEXT_AREA],
16856 'I',
16857 glyph->charpos,
16858 (BUFFERP (glyph->object)
16859 ? 'B'
16860 : (STRINGP (glyph->object)
16861 ? 'S'
16862 : '-')),
16863 glyph->pixel_width,
16864 glyph->u.img_id,
16865 '.',
16866 glyph->face_id,
16867 glyph->left_box_line_p,
16868 glyph->right_box_line_p);
16869 }
16870 else if (glyph->type == COMPOSITE_GLYPH)
16871 {
16872 fprintf (stderr,
16873 " %5d %4c %6d %c %3d 0x%05x",
16874 glyph - row->glyphs[TEXT_AREA],
16875 '+',
16876 glyph->charpos,
16877 (BUFFERP (glyph->object)
16878 ? 'B'
16879 : (STRINGP (glyph->object)
16880 ? 'S'
16881 : '-')),
16882 glyph->pixel_width,
16883 glyph->u.cmp.id);
16884 if (glyph->u.cmp.automatic)
16885 fprintf (stderr,
16886 "[%d-%d]",
16887 glyph->slice.cmp.from, glyph->slice.cmp.to);
16888 fprintf (stderr, " . %4d %1.1d%1.1d\n",
16889 glyph->face_id,
16890 glyph->left_box_line_p,
16891 glyph->right_box_line_p);
16892 }
16893 }
16894
16895
16896 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
16897 GLYPHS 0 means don't show glyph contents.
16898 GLYPHS 1 means show glyphs in short form
16899 GLYPHS > 1 means show glyphs in long form. */
16900
16901 void
16902 dump_glyph_row (row, vpos, glyphs)
16903 struct glyph_row *row;
16904 int vpos, glyphs;
16905 {
16906 if (glyphs != 1)
16907 {
16908 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
16909 fprintf (stderr, "======================================================================\n");
16910
16911 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
16912 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
16913 vpos,
16914 MATRIX_ROW_START_CHARPOS (row),
16915 MATRIX_ROW_END_CHARPOS (row),
16916 row->used[TEXT_AREA],
16917 row->contains_overlapping_glyphs_p,
16918 row->enabled_p,
16919 row->truncated_on_left_p,
16920 row->truncated_on_right_p,
16921 row->continued_p,
16922 MATRIX_ROW_CONTINUATION_LINE_P (row),
16923 row->displays_text_p,
16924 row->ends_at_zv_p,
16925 row->fill_line_p,
16926 row->ends_in_middle_of_char_p,
16927 row->starts_in_middle_of_char_p,
16928 row->mouse_face_p,
16929 row->x,
16930 row->y,
16931 row->pixel_width,
16932 row->height,
16933 row->visible_height,
16934 row->ascent,
16935 row->phys_ascent);
16936 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
16937 row->end.overlay_string_index,
16938 row->continuation_lines_width);
16939 fprintf (stderr, "%9d %5d\n",
16940 CHARPOS (row->start.string_pos),
16941 CHARPOS (row->end.string_pos));
16942 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
16943 row->end.dpvec_index);
16944 }
16945
16946 if (glyphs > 1)
16947 {
16948 int area;
16949
16950 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16951 {
16952 struct glyph *glyph = row->glyphs[area];
16953 struct glyph *glyph_end = glyph + row->used[area];
16954
16955 /* Glyph for a line end in text. */
16956 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
16957 ++glyph_end;
16958
16959 if (glyph < glyph_end)
16960 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
16961
16962 for (; glyph < glyph_end; ++glyph)
16963 dump_glyph (row, glyph, area);
16964 }
16965 }
16966 else if (glyphs == 1)
16967 {
16968 int area;
16969
16970 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16971 {
16972 char *s = (char *) alloca (row->used[area] + 1);
16973 int i;
16974
16975 for (i = 0; i < row->used[area]; ++i)
16976 {
16977 struct glyph *glyph = row->glyphs[area] + i;
16978 if (glyph->type == CHAR_GLYPH
16979 && glyph->u.ch < 0x80
16980 && glyph->u.ch >= ' ')
16981 s[i] = glyph->u.ch;
16982 else
16983 s[i] = '.';
16984 }
16985
16986 s[i] = '\0';
16987 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
16988 }
16989 }
16990 }
16991
16992
16993 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
16994 Sdump_glyph_matrix, 0, 1, "p",
16995 doc: /* Dump the current matrix of the selected window to stderr.
16996 Shows contents of glyph row structures. With non-nil
16997 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
16998 glyphs in short form, otherwise show glyphs in long form. */)
16999 (Lisp_Object glyphs)
17000 {
17001 struct window *w = XWINDOW (selected_window);
17002 struct buffer *buffer = XBUFFER (w->buffer);
17003
17004 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
17005 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17006 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17007 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17008 fprintf (stderr, "=============================================\n");
17009 dump_glyph_matrix (w->current_matrix,
17010 NILP (glyphs) ? 0 : XINT (glyphs));
17011 return Qnil;
17012 }
17013
17014
17015 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17016 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17017 (void)
17018 {
17019 struct frame *f = XFRAME (selected_frame);
17020 dump_glyph_matrix (f->current_matrix, 1);
17021 return Qnil;
17022 }
17023
17024
17025 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17026 doc: /* Dump glyph row ROW to stderr.
17027 GLYPH 0 means don't dump glyphs.
17028 GLYPH 1 means dump glyphs in short form.
17029 GLYPH > 1 or omitted means dump glyphs in long form. */)
17030 (Lisp_Object row, Lisp_Object glyphs)
17031 {
17032 struct glyph_matrix *matrix;
17033 int vpos;
17034
17035 CHECK_NUMBER (row);
17036 matrix = XWINDOW (selected_window)->current_matrix;
17037 vpos = XINT (row);
17038 if (vpos >= 0 && vpos < matrix->nrows)
17039 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17040 vpos,
17041 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17042 return Qnil;
17043 }
17044
17045
17046 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17047 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17048 GLYPH 0 means don't dump glyphs.
17049 GLYPH 1 means dump glyphs in short form.
17050 GLYPH > 1 or omitted means dump glyphs in long form. */)
17051 (Lisp_Object row, Lisp_Object glyphs)
17052 {
17053 struct frame *sf = SELECTED_FRAME ();
17054 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17055 int vpos;
17056
17057 CHECK_NUMBER (row);
17058 vpos = XINT (row);
17059 if (vpos >= 0 && vpos < m->nrows)
17060 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17061 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17062 return Qnil;
17063 }
17064
17065
17066 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17067 doc: /* Toggle tracing of redisplay.
17068 With ARG, turn tracing on if and only if ARG is positive. */)
17069 (Lisp_Object arg)
17070 {
17071 if (NILP (arg))
17072 trace_redisplay_p = !trace_redisplay_p;
17073 else
17074 {
17075 arg = Fprefix_numeric_value (arg);
17076 trace_redisplay_p = XINT (arg) > 0;
17077 }
17078
17079 return Qnil;
17080 }
17081
17082
17083 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17084 doc: /* Like `format', but print result to stderr.
17085 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17086 (size_t nargs, Lisp_Object *args)
17087 {
17088 Lisp_Object s = Fformat (nargs, args);
17089 fprintf (stderr, "%s", SDATA (s));
17090 return Qnil;
17091 }
17092
17093 #endif /* GLYPH_DEBUG */
17094
17095
17096 \f
17097 /***********************************************************************
17098 Building Desired Matrix Rows
17099 ***********************************************************************/
17100
17101 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17102 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17103
17104 static struct glyph_row *
17105 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17106 {
17107 struct frame *f = XFRAME (WINDOW_FRAME (w));
17108 struct buffer *buffer = XBUFFER (w->buffer);
17109 struct buffer *old = current_buffer;
17110 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17111 int arrow_len = SCHARS (overlay_arrow_string);
17112 const unsigned char *arrow_end = arrow_string + arrow_len;
17113 const unsigned char *p;
17114 struct it it;
17115 int multibyte_p;
17116 int n_glyphs_before;
17117
17118 set_buffer_temp (buffer);
17119 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17120 it.glyph_row->used[TEXT_AREA] = 0;
17121 SET_TEXT_POS (it.position, 0, 0);
17122
17123 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17124 p = arrow_string;
17125 while (p < arrow_end)
17126 {
17127 Lisp_Object face, ilisp;
17128
17129 /* Get the next character. */
17130 if (multibyte_p)
17131 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17132 else
17133 {
17134 it.c = it.char_to_display = *p, it.len = 1;
17135 if (! ASCII_CHAR_P (it.c))
17136 it.char_to_display = BYTE8_TO_CHAR (it.c);
17137 }
17138 p += it.len;
17139
17140 /* Get its face. */
17141 ilisp = make_number (p - arrow_string);
17142 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17143 it.face_id = compute_char_face (f, it.char_to_display, face);
17144
17145 /* Compute its width, get its glyphs. */
17146 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17147 SET_TEXT_POS (it.position, -1, -1);
17148 PRODUCE_GLYPHS (&it);
17149
17150 /* If this character doesn't fit any more in the line, we have
17151 to remove some glyphs. */
17152 if (it.current_x > it.last_visible_x)
17153 {
17154 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17155 break;
17156 }
17157 }
17158
17159 set_buffer_temp (old);
17160 return it.glyph_row;
17161 }
17162
17163
17164 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17165 glyphs are only inserted for terminal frames since we can't really
17166 win with truncation glyphs when partially visible glyphs are
17167 involved. Which glyphs to insert is determined by
17168 produce_special_glyphs. */
17169
17170 static void
17171 insert_left_trunc_glyphs (struct it *it)
17172 {
17173 struct it truncate_it;
17174 struct glyph *from, *end, *to, *toend;
17175
17176 xassert (!FRAME_WINDOW_P (it->f));
17177
17178 /* Get the truncation glyphs. */
17179 truncate_it = *it;
17180 truncate_it.current_x = 0;
17181 truncate_it.face_id = DEFAULT_FACE_ID;
17182 truncate_it.glyph_row = &scratch_glyph_row;
17183 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17184 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17185 truncate_it.object = make_number (0);
17186 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17187
17188 /* Overwrite glyphs from IT with truncation glyphs. */
17189 if (!it->glyph_row->reversed_p)
17190 {
17191 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17192 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17193 to = it->glyph_row->glyphs[TEXT_AREA];
17194 toend = to + it->glyph_row->used[TEXT_AREA];
17195
17196 while (from < end)
17197 *to++ = *from++;
17198
17199 /* There may be padding glyphs left over. Overwrite them too. */
17200 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17201 {
17202 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17203 while (from < end)
17204 *to++ = *from++;
17205 }
17206
17207 if (to > toend)
17208 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17209 }
17210 else
17211 {
17212 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17213 that back to front. */
17214 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17215 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17216 toend = it->glyph_row->glyphs[TEXT_AREA];
17217 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17218
17219 while (from >= end && to >= toend)
17220 *to-- = *from--;
17221 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17222 {
17223 from =
17224 truncate_it.glyph_row->glyphs[TEXT_AREA]
17225 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17226 while (from >= end && to >= toend)
17227 *to-- = *from--;
17228 }
17229 if (from >= end)
17230 {
17231 /* Need to free some room before prepending additional
17232 glyphs. */
17233 int move_by = from - end + 1;
17234 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17235 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17236
17237 for ( ; g >= g0; g--)
17238 g[move_by] = *g;
17239 while (from >= end)
17240 *to-- = *from--;
17241 it->glyph_row->used[TEXT_AREA] += move_by;
17242 }
17243 }
17244 }
17245
17246
17247 /* Compute the pixel height and width of IT->glyph_row.
17248
17249 Most of the time, ascent and height of a display line will be equal
17250 to the max_ascent and max_height values of the display iterator
17251 structure. This is not the case if
17252
17253 1. We hit ZV without displaying anything. In this case, max_ascent
17254 and max_height will be zero.
17255
17256 2. We have some glyphs that don't contribute to the line height.
17257 (The glyph row flag contributes_to_line_height_p is for future
17258 pixmap extensions).
17259
17260 The first case is easily covered by using default values because in
17261 these cases, the line height does not really matter, except that it
17262 must not be zero. */
17263
17264 static void
17265 compute_line_metrics (struct it *it)
17266 {
17267 struct glyph_row *row = it->glyph_row;
17268
17269 if (FRAME_WINDOW_P (it->f))
17270 {
17271 int i, min_y, max_y;
17272
17273 /* The line may consist of one space only, that was added to
17274 place the cursor on it. If so, the row's height hasn't been
17275 computed yet. */
17276 if (row->height == 0)
17277 {
17278 if (it->max_ascent + it->max_descent == 0)
17279 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17280 row->ascent = it->max_ascent;
17281 row->height = it->max_ascent + it->max_descent;
17282 row->phys_ascent = it->max_phys_ascent;
17283 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17284 row->extra_line_spacing = it->max_extra_line_spacing;
17285 }
17286
17287 /* Compute the width of this line. */
17288 row->pixel_width = row->x;
17289 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17290 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17291
17292 xassert (row->pixel_width >= 0);
17293 xassert (row->ascent >= 0 && row->height > 0);
17294
17295 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17296 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17297
17298 /* If first line's physical ascent is larger than its logical
17299 ascent, use the physical ascent, and make the row taller.
17300 This makes accented characters fully visible. */
17301 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17302 && row->phys_ascent > row->ascent)
17303 {
17304 row->height += row->phys_ascent - row->ascent;
17305 row->ascent = row->phys_ascent;
17306 }
17307
17308 /* Compute how much of the line is visible. */
17309 row->visible_height = row->height;
17310
17311 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17312 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17313
17314 if (row->y < min_y)
17315 row->visible_height -= min_y - row->y;
17316 if (row->y + row->height > max_y)
17317 row->visible_height -= row->y + row->height - max_y;
17318 }
17319 else
17320 {
17321 row->pixel_width = row->used[TEXT_AREA];
17322 if (row->continued_p)
17323 row->pixel_width -= it->continuation_pixel_width;
17324 else if (row->truncated_on_right_p)
17325 row->pixel_width -= it->truncation_pixel_width;
17326 row->ascent = row->phys_ascent = 0;
17327 row->height = row->phys_height = row->visible_height = 1;
17328 row->extra_line_spacing = 0;
17329 }
17330
17331 /* Compute a hash code for this row. */
17332 {
17333 int area, i;
17334 row->hash = 0;
17335 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17336 for (i = 0; i < row->used[area]; ++i)
17337 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17338 + row->glyphs[area][i].u.val
17339 + row->glyphs[area][i].face_id
17340 + row->glyphs[area][i].padding_p
17341 + (row->glyphs[area][i].type << 2));
17342 }
17343
17344 it->max_ascent = it->max_descent = 0;
17345 it->max_phys_ascent = it->max_phys_descent = 0;
17346 }
17347
17348
17349 /* Append one space to the glyph row of iterator IT if doing a
17350 window-based redisplay. The space has the same face as
17351 IT->face_id. Value is non-zero if a space was added.
17352
17353 This function is called to make sure that there is always one glyph
17354 at the end of a glyph row that the cursor can be set on under
17355 window-systems. (If there weren't such a glyph we would not know
17356 how wide and tall a box cursor should be displayed).
17357
17358 At the same time this space let's a nicely handle clearing to the
17359 end of the line if the row ends in italic text. */
17360
17361 static int
17362 append_space_for_newline (struct it *it, int default_face_p)
17363 {
17364 if (FRAME_WINDOW_P (it->f))
17365 {
17366 int n = it->glyph_row->used[TEXT_AREA];
17367
17368 if (it->glyph_row->glyphs[TEXT_AREA] + n
17369 < it->glyph_row->glyphs[1 + TEXT_AREA])
17370 {
17371 /* Save some values that must not be changed.
17372 Must save IT->c and IT->len because otherwise
17373 ITERATOR_AT_END_P wouldn't work anymore after
17374 append_space_for_newline has been called. */
17375 enum display_element_type saved_what = it->what;
17376 int saved_c = it->c, saved_len = it->len;
17377 int saved_char_to_display = it->char_to_display;
17378 int saved_x = it->current_x;
17379 int saved_face_id = it->face_id;
17380 struct text_pos saved_pos;
17381 Lisp_Object saved_object;
17382 struct face *face;
17383
17384 saved_object = it->object;
17385 saved_pos = it->position;
17386
17387 it->what = IT_CHARACTER;
17388 memset (&it->position, 0, sizeof it->position);
17389 it->object = make_number (0);
17390 it->c = it->char_to_display = ' ';
17391 it->len = 1;
17392
17393 if (default_face_p)
17394 it->face_id = DEFAULT_FACE_ID;
17395 else if (it->face_before_selective_p)
17396 it->face_id = it->saved_face_id;
17397 face = FACE_FROM_ID (it->f, it->face_id);
17398 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17399
17400 PRODUCE_GLYPHS (it);
17401
17402 it->override_ascent = -1;
17403 it->constrain_row_ascent_descent_p = 0;
17404 it->current_x = saved_x;
17405 it->object = saved_object;
17406 it->position = saved_pos;
17407 it->what = saved_what;
17408 it->face_id = saved_face_id;
17409 it->len = saved_len;
17410 it->c = saved_c;
17411 it->char_to_display = saved_char_to_display;
17412 return 1;
17413 }
17414 }
17415
17416 return 0;
17417 }
17418
17419
17420 /* Extend the face of the last glyph in the text area of IT->glyph_row
17421 to the end of the display line. Called from display_line. If the
17422 glyph row is empty, add a space glyph to it so that we know the
17423 face to draw. Set the glyph row flag fill_line_p. If the glyph
17424 row is R2L, prepend a stretch glyph to cover the empty space to the
17425 left of the leftmost glyph. */
17426
17427 static void
17428 extend_face_to_end_of_line (struct it *it)
17429 {
17430 struct face *face;
17431 struct frame *f = it->f;
17432
17433 /* If line is already filled, do nothing. Non window-system frames
17434 get a grace of one more ``pixel'' because their characters are
17435 1-``pixel'' wide, so they hit the equality too early. This grace
17436 is needed only for R2L rows that are not continued, to produce
17437 one extra blank where we could display the cursor. */
17438 if (it->current_x >= it->last_visible_x
17439 + (!FRAME_WINDOW_P (f)
17440 && it->glyph_row->reversed_p
17441 && !it->glyph_row->continued_p))
17442 return;
17443
17444 /* Face extension extends the background and box of IT->face_id
17445 to the end of the line. If the background equals the background
17446 of the frame, we don't have to do anything. */
17447 if (it->face_before_selective_p)
17448 face = FACE_FROM_ID (f, it->saved_face_id);
17449 else
17450 face = FACE_FROM_ID (f, it->face_id);
17451
17452 if (FRAME_WINDOW_P (f)
17453 && it->glyph_row->displays_text_p
17454 && face->box == FACE_NO_BOX
17455 && face->background == FRAME_BACKGROUND_PIXEL (f)
17456 && !face->stipple
17457 && !it->glyph_row->reversed_p)
17458 return;
17459
17460 /* Set the glyph row flag indicating that the face of the last glyph
17461 in the text area has to be drawn to the end of the text area. */
17462 it->glyph_row->fill_line_p = 1;
17463
17464 /* If current character of IT is not ASCII, make sure we have the
17465 ASCII face. This will be automatically undone the next time
17466 get_next_display_element returns a multibyte character. Note
17467 that the character will always be single byte in unibyte
17468 text. */
17469 if (!ASCII_CHAR_P (it->c))
17470 {
17471 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17472 }
17473
17474 if (FRAME_WINDOW_P (f))
17475 {
17476 /* If the row is empty, add a space with the current face of IT,
17477 so that we know which face to draw. */
17478 if (it->glyph_row->used[TEXT_AREA] == 0)
17479 {
17480 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17481 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17482 it->glyph_row->used[TEXT_AREA] = 1;
17483 }
17484 #ifdef HAVE_WINDOW_SYSTEM
17485 if (it->glyph_row->reversed_p)
17486 {
17487 /* Prepend a stretch glyph to the row, such that the
17488 rightmost glyph will be drawn flushed all the way to the
17489 right margin of the window. The stretch glyph that will
17490 occupy the empty space, if any, to the left of the
17491 glyphs. */
17492 struct font *font = face->font ? face->font : FRAME_FONT (f);
17493 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17494 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17495 struct glyph *g;
17496 int row_width, stretch_ascent, stretch_width;
17497 struct text_pos saved_pos;
17498 int saved_face_id, saved_avoid_cursor;
17499
17500 for (row_width = 0, g = row_start; g < row_end; g++)
17501 row_width += g->pixel_width;
17502 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17503 if (stretch_width > 0)
17504 {
17505 stretch_ascent =
17506 (((it->ascent + it->descent)
17507 * FONT_BASE (font)) / FONT_HEIGHT (font));
17508 saved_pos = it->position;
17509 memset (&it->position, 0, sizeof it->position);
17510 saved_avoid_cursor = it->avoid_cursor_p;
17511 it->avoid_cursor_p = 1;
17512 saved_face_id = it->face_id;
17513 /* The last row's stretch glyph should get the default
17514 face, to avoid painting the rest of the window with
17515 the region face, if the region ends at ZV. */
17516 if (it->glyph_row->ends_at_zv_p)
17517 it->face_id = DEFAULT_FACE_ID;
17518 else
17519 it->face_id = face->id;
17520 append_stretch_glyph (it, make_number (0), stretch_width,
17521 it->ascent + it->descent, stretch_ascent);
17522 it->position = saved_pos;
17523 it->avoid_cursor_p = saved_avoid_cursor;
17524 it->face_id = saved_face_id;
17525 }
17526 }
17527 #endif /* HAVE_WINDOW_SYSTEM */
17528 }
17529 else
17530 {
17531 /* Save some values that must not be changed. */
17532 int saved_x = it->current_x;
17533 struct text_pos saved_pos;
17534 Lisp_Object saved_object;
17535 enum display_element_type saved_what = it->what;
17536 int saved_face_id = it->face_id;
17537
17538 saved_object = it->object;
17539 saved_pos = it->position;
17540
17541 it->what = IT_CHARACTER;
17542 memset (&it->position, 0, sizeof it->position);
17543 it->object = make_number (0);
17544 it->c = it->char_to_display = ' ';
17545 it->len = 1;
17546 /* The last row's blank glyphs should get the default face, to
17547 avoid painting the rest of the window with the region face,
17548 if the region ends at ZV. */
17549 if (it->glyph_row->ends_at_zv_p)
17550 it->face_id = DEFAULT_FACE_ID;
17551 else
17552 it->face_id = face->id;
17553
17554 PRODUCE_GLYPHS (it);
17555
17556 while (it->current_x <= it->last_visible_x)
17557 PRODUCE_GLYPHS (it);
17558
17559 /* Don't count these blanks really. It would let us insert a left
17560 truncation glyph below and make us set the cursor on them, maybe. */
17561 it->current_x = saved_x;
17562 it->object = saved_object;
17563 it->position = saved_pos;
17564 it->what = saved_what;
17565 it->face_id = saved_face_id;
17566 }
17567 }
17568
17569
17570 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17571 trailing whitespace. */
17572
17573 static int
17574 trailing_whitespace_p (EMACS_INT charpos)
17575 {
17576 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17577 int c = 0;
17578
17579 while (bytepos < ZV_BYTE
17580 && (c = FETCH_CHAR (bytepos),
17581 c == ' ' || c == '\t'))
17582 ++bytepos;
17583
17584 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17585 {
17586 if (bytepos != PT_BYTE)
17587 return 1;
17588 }
17589 return 0;
17590 }
17591
17592
17593 /* Highlight trailing whitespace, if any, in ROW. */
17594
17595 static void
17596 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17597 {
17598 int used = row->used[TEXT_AREA];
17599
17600 if (used)
17601 {
17602 struct glyph *start = row->glyphs[TEXT_AREA];
17603 struct glyph *glyph = start + used - 1;
17604
17605 if (row->reversed_p)
17606 {
17607 /* Right-to-left rows need to be processed in the opposite
17608 direction, so swap the edge pointers. */
17609 glyph = start;
17610 start = row->glyphs[TEXT_AREA] + used - 1;
17611 }
17612
17613 /* Skip over glyphs inserted to display the cursor at the
17614 end of a line, for extending the face of the last glyph
17615 to the end of the line on terminals, and for truncation
17616 and continuation glyphs. */
17617 if (!row->reversed_p)
17618 {
17619 while (glyph >= start
17620 && glyph->type == CHAR_GLYPH
17621 && INTEGERP (glyph->object))
17622 --glyph;
17623 }
17624 else
17625 {
17626 while (glyph <= start
17627 && glyph->type == CHAR_GLYPH
17628 && INTEGERP (glyph->object))
17629 ++glyph;
17630 }
17631
17632 /* If last glyph is a space or stretch, and it's trailing
17633 whitespace, set the face of all trailing whitespace glyphs in
17634 IT->glyph_row to `trailing-whitespace'. */
17635 if ((row->reversed_p ? glyph <= start : glyph >= start)
17636 && BUFFERP (glyph->object)
17637 && (glyph->type == STRETCH_GLYPH
17638 || (glyph->type == CHAR_GLYPH
17639 && glyph->u.ch == ' '))
17640 && trailing_whitespace_p (glyph->charpos))
17641 {
17642 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17643 if (face_id < 0)
17644 return;
17645
17646 if (!row->reversed_p)
17647 {
17648 while (glyph >= start
17649 && BUFFERP (glyph->object)
17650 && (glyph->type == STRETCH_GLYPH
17651 || (glyph->type == CHAR_GLYPH
17652 && glyph->u.ch == ' ')))
17653 (glyph--)->face_id = face_id;
17654 }
17655 else
17656 {
17657 while (glyph <= start
17658 && BUFFERP (glyph->object)
17659 && (glyph->type == STRETCH_GLYPH
17660 || (glyph->type == CHAR_GLYPH
17661 && glyph->u.ch == ' ')))
17662 (glyph++)->face_id = face_id;
17663 }
17664 }
17665 }
17666 }
17667
17668
17669 /* Value is non-zero if glyph row ROW should be
17670 used to hold the cursor. */
17671
17672 static int
17673 cursor_row_p (struct glyph_row *row)
17674 {
17675 int result = 1;
17676
17677 if (PT == CHARPOS (row->end.pos))
17678 {
17679 /* Suppose the row ends on a string.
17680 Unless the row is continued, that means it ends on a newline
17681 in the string. If it's anything other than a display string
17682 (e.g. a before-string from an overlay), we don't want the
17683 cursor there. (This heuristic seems to give the optimal
17684 behavior for the various types of multi-line strings.) */
17685 if (CHARPOS (row->end.string_pos) >= 0)
17686 {
17687 if (row->continued_p)
17688 result = 1;
17689 else
17690 {
17691 /* Check for `display' property. */
17692 struct glyph *beg = row->glyphs[TEXT_AREA];
17693 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17694 struct glyph *glyph;
17695
17696 result = 0;
17697 for (glyph = end; glyph >= beg; --glyph)
17698 if (STRINGP (glyph->object))
17699 {
17700 Lisp_Object prop
17701 = Fget_char_property (make_number (PT),
17702 Qdisplay, Qnil);
17703 result =
17704 (!NILP (prop)
17705 && display_prop_string_p (prop, glyph->object));
17706 break;
17707 }
17708 }
17709 }
17710 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17711 {
17712 /* If the row ends in middle of a real character,
17713 and the line is continued, we want the cursor here.
17714 That's because CHARPOS (ROW->end.pos) would equal
17715 PT if PT is before the character. */
17716 if (!row->ends_in_ellipsis_p)
17717 result = row->continued_p;
17718 else
17719 /* If the row ends in an ellipsis, then
17720 CHARPOS (ROW->end.pos) will equal point after the
17721 invisible text. We want that position to be displayed
17722 after the ellipsis. */
17723 result = 0;
17724 }
17725 /* If the row ends at ZV, display the cursor at the end of that
17726 row instead of at the start of the row below. */
17727 else if (row->ends_at_zv_p)
17728 result = 1;
17729 else
17730 result = 0;
17731 }
17732
17733 return result;
17734 }
17735
17736 \f
17737
17738 /* Push the display property PROP so that it will be rendered at the
17739 current position in IT. Return 1 if PROP was successfully pushed,
17740 0 otherwise. */
17741
17742 static int
17743 push_display_prop (struct it *it, Lisp_Object prop)
17744 {
17745 xassert (it->method == GET_FROM_BUFFER);
17746
17747 push_it (it, NULL);
17748
17749 if (STRINGP (prop))
17750 {
17751 if (SCHARS (prop) == 0)
17752 {
17753 pop_it (it);
17754 return 0;
17755 }
17756
17757 it->string = prop;
17758 it->multibyte_p = STRING_MULTIBYTE (it->string);
17759 it->current.overlay_string_index = -1;
17760 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17761 it->end_charpos = it->string_nchars = SCHARS (it->string);
17762 it->method = GET_FROM_STRING;
17763 it->stop_charpos = 0;
17764 it->prev_stop = 0;
17765 it->base_level_stop = 0;
17766 it->string_from_display_prop_p = 1;
17767
17768 /* Force paragraph direction to be that of the parent
17769 buffer. */
17770 it->paragraph_embedding = (it->bidi_p ? it->bidi_it.paragraph_dir : L2R);
17771
17772 /* Set up the bidi iterator for this display string. */
17773 if (it->bidi_p)
17774 {
17775 it->bidi_it.string.lstring = it->string;
17776 it->bidi_it.string.s = NULL;
17777 it->bidi_it.string.schars = it->end_charpos;
17778 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
17779 it->bidi_it.string.from_disp_str = 1;
17780 it->bidi_it.string.unibyte = !it->multibyte_p;
17781 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
17782 }
17783 }
17784 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17785 {
17786 it->method = GET_FROM_STRETCH;
17787 it->object = prop;
17788 }
17789 #ifdef HAVE_WINDOW_SYSTEM
17790 else if (IMAGEP (prop))
17791 {
17792 it->what = IT_IMAGE;
17793 it->image_id = lookup_image (it->f, prop);
17794 it->method = GET_FROM_IMAGE;
17795 }
17796 #endif /* HAVE_WINDOW_SYSTEM */
17797 else
17798 {
17799 pop_it (it); /* bogus display property, give up */
17800 return 0;
17801 }
17802
17803 return 1;
17804 }
17805
17806 /* Return the character-property PROP at the current position in IT. */
17807
17808 static Lisp_Object
17809 get_it_property (struct it *it, Lisp_Object prop)
17810 {
17811 Lisp_Object position;
17812
17813 if (STRINGP (it->object))
17814 position = make_number (IT_STRING_CHARPOS (*it));
17815 else if (BUFFERP (it->object))
17816 position = make_number (IT_CHARPOS (*it));
17817 else
17818 return Qnil;
17819
17820 return Fget_char_property (position, prop, it->object);
17821 }
17822
17823 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17824
17825 static void
17826 handle_line_prefix (struct it *it)
17827 {
17828 Lisp_Object prefix;
17829
17830 if (it->continuation_lines_width > 0)
17831 {
17832 prefix = get_it_property (it, Qwrap_prefix);
17833 if (NILP (prefix))
17834 prefix = Vwrap_prefix;
17835 }
17836 else
17837 {
17838 prefix = get_it_property (it, Qline_prefix);
17839 if (NILP (prefix))
17840 prefix = Vline_prefix;
17841 }
17842 if (! NILP (prefix) && push_display_prop (it, prefix))
17843 {
17844 /* If the prefix is wider than the window, and we try to wrap
17845 it, it would acquire its own wrap prefix, and so on till the
17846 iterator stack overflows. So, don't wrap the prefix. */
17847 it->line_wrap = TRUNCATE;
17848 it->avoid_cursor_p = 1;
17849 }
17850 }
17851
17852 \f
17853
17854 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17855 only for R2L lines from display_line and display_string, when they
17856 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
17857 the line/string needs to be continued on the next glyph row. */
17858 static void
17859 unproduce_glyphs (struct it *it, int n)
17860 {
17861 struct glyph *glyph, *end;
17862
17863 xassert (it->glyph_row);
17864 xassert (it->glyph_row->reversed_p);
17865 xassert (it->area == TEXT_AREA);
17866 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17867
17868 if (n > it->glyph_row->used[TEXT_AREA])
17869 n = it->glyph_row->used[TEXT_AREA];
17870 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17871 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17872 for ( ; glyph < end; glyph++)
17873 glyph[-n] = *glyph;
17874 }
17875
17876 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17877 and ROW->maxpos. */
17878 static void
17879 find_row_edges (struct it *it, struct glyph_row *row,
17880 EMACS_INT min_pos, EMACS_INT min_bpos,
17881 EMACS_INT max_pos, EMACS_INT max_bpos)
17882 {
17883 /* FIXME: Revisit this when glyph ``spilling'' in continuation
17884 lines' rows is implemented for bidi-reordered rows. */
17885
17886 /* ROW->minpos is the value of min_pos, the minimal buffer position
17887 we have in ROW. */
17888 if (min_pos <= ZV)
17889 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
17890 else
17891 /* We didn't find _any_ valid buffer positions in any of the
17892 glyphs, so we must trust the iterator's computed positions. */
17893 row->minpos = row->start.pos;
17894 if (max_pos <= 0)
17895 {
17896 max_pos = CHARPOS (it->current.pos);
17897 max_bpos = BYTEPOS (it->current.pos);
17898 }
17899
17900 /* Here are the various use-cases for ending the row, and the
17901 corresponding values for ROW->maxpos:
17902
17903 Line ends in a newline from buffer eol_pos + 1
17904 Line is continued from buffer max_pos + 1
17905 Line is truncated on right it->current.pos
17906 Line ends in a newline from string max_pos
17907 Line is continued from string max_pos
17908 Line is continued from display vector max_pos
17909 Line is entirely from a string min_pos == max_pos
17910 Line is entirely from a display vector min_pos == max_pos
17911 Line that ends at ZV ZV
17912
17913 If you discover other use-cases, please add them here as
17914 appropriate. */
17915 if (row->ends_at_zv_p)
17916 row->maxpos = it->current.pos;
17917 else if (row->used[TEXT_AREA])
17918 {
17919 if (row->ends_in_newline_from_string_p)
17920 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17921 else if (CHARPOS (it->eol_pos) > 0)
17922 SET_TEXT_POS (row->maxpos,
17923 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
17924 else if (row->continued_p)
17925 {
17926 /* If max_pos is different from IT's current position, it
17927 means IT->method does not belong to the display element
17928 at max_pos. However, it also means that the display
17929 element at max_pos was displayed in its entirety on this
17930 line, which is equivalent to saying that the next line
17931 starts at the next buffer position. */
17932 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
17933 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17934 else
17935 {
17936 INC_BOTH (max_pos, max_bpos);
17937 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
17938 }
17939 }
17940 else if (row->truncated_on_right_p)
17941 /* display_line already called reseat_at_next_visible_line_start,
17942 which puts the iterator at the beginning of the next line, in
17943 the logical order. */
17944 row->maxpos = it->current.pos;
17945 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
17946 /* A line that is entirely from a string/image/stretch... */
17947 row->maxpos = row->minpos;
17948 else
17949 abort ();
17950 }
17951 else
17952 row->maxpos = it->current.pos;
17953 }
17954
17955 /* Construct the glyph row IT->glyph_row in the desired matrix of
17956 IT->w from text at the current position of IT. See dispextern.h
17957 for an overview of struct it. Value is non-zero if
17958 IT->glyph_row displays text, as opposed to a line displaying ZV
17959 only. */
17960
17961 static int
17962 display_line (struct it *it)
17963 {
17964 struct glyph_row *row = it->glyph_row;
17965 Lisp_Object overlay_arrow_string;
17966 struct it wrap_it;
17967 int may_wrap = 0, wrap_x IF_LINT (= 0);
17968 int wrap_row_used = -1;
17969 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
17970 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
17971 int wrap_row_extra_line_spacing IF_LINT (= 0);
17972 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
17973 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
17974 int cvpos;
17975 EMACS_INT min_pos = ZV + 1, max_pos = 0;
17976 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
17977
17978 /* We always start displaying at hpos zero even if hscrolled. */
17979 xassert (it->hpos == 0 && it->current_x == 0);
17980
17981 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
17982 >= it->w->desired_matrix->nrows)
17983 {
17984 it->w->nrows_scale_factor++;
17985 fonts_changed_p = 1;
17986 return 0;
17987 }
17988
17989 /* Is IT->w showing the region? */
17990 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
17991
17992 /* Clear the result glyph row and enable it. */
17993 prepare_desired_row (row);
17994
17995 row->y = it->current_y;
17996 row->start = it->start;
17997 row->continuation_lines_width = it->continuation_lines_width;
17998 row->displays_text_p = 1;
17999 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18000 it->starts_in_middle_of_char_p = 0;
18001
18002 /* Arrange the overlays nicely for our purposes. Usually, we call
18003 display_line on only one line at a time, in which case this
18004 can't really hurt too much, or we call it on lines which appear
18005 one after another in the buffer, in which case all calls to
18006 recenter_overlay_lists but the first will be pretty cheap. */
18007 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18008
18009 /* Move over display elements that are not visible because we are
18010 hscrolled. This may stop at an x-position < IT->first_visible_x
18011 if the first glyph is partially visible or if we hit a line end. */
18012 if (it->current_x < it->first_visible_x)
18013 {
18014 this_line_min_pos = row->start.pos;
18015 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18016 MOVE_TO_POS | MOVE_TO_X);
18017 /* Record the smallest positions seen while we moved over
18018 display elements that are not visible. This is needed by
18019 redisplay_internal for optimizing the case where the cursor
18020 stays inside the same line. The rest of this function only
18021 considers positions that are actually displayed, so
18022 RECORD_MAX_MIN_POS will not otherwise record positions that
18023 are hscrolled to the left of the left edge of the window. */
18024 min_pos = CHARPOS (this_line_min_pos);
18025 min_bpos = BYTEPOS (this_line_min_pos);
18026 }
18027 else
18028 {
18029 /* We only do this when not calling `move_it_in_display_line_to'
18030 above, because move_it_in_display_line_to calls
18031 handle_line_prefix itself. */
18032 handle_line_prefix (it);
18033 }
18034
18035 /* Get the initial row height. This is either the height of the
18036 text hscrolled, if there is any, or zero. */
18037 row->ascent = it->max_ascent;
18038 row->height = it->max_ascent + it->max_descent;
18039 row->phys_ascent = it->max_phys_ascent;
18040 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18041 row->extra_line_spacing = it->max_extra_line_spacing;
18042
18043 /* Utility macro to record max and min buffer positions seen until now. */
18044 #define RECORD_MAX_MIN_POS(IT) \
18045 do \
18046 { \
18047 if (IT_CHARPOS (*(IT)) < min_pos) \
18048 { \
18049 min_pos = IT_CHARPOS (*(IT)); \
18050 min_bpos = IT_BYTEPOS (*(IT)); \
18051 } \
18052 if (IT_CHARPOS (*(IT)) > max_pos) \
18053 { \
18054 max_pos = IT_CHARPOS (*(IT)); \
18055 max_bpos = IT_BYTEPOS (*(IT)); \
18056 } \
18057 } \
18058 while (0)
18059
18060 /* Loop generating characters. The loop is left with IT on the next
18061 character to display. */
18062 while (1)
18063 {
18064 int n_glyphs_before, hpos_before, x_before;
18065 int x, nglyphs;
18066 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18067
18068 /* Retrieve the next thing to display. Value is zero if end of
18069 buffer reached. */
18070 if (!get_next_display_element (it))
18071 {
18072 /* Maybe add a space at the end of this line that is used to
18073 display the cursor there under X. Set the charpos of the
18074 first glyph of blank lines not corresponding to any text
18075 to -1. */
18076 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18077 row->exact_window_width_line_p = 1;
18078 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18079 || row->used[TEXT_AREA] == 0)
18080 {
18081 row->glyphs[TEXT_AREA]->charpos = -1;
18082 row->displays_text_p = 0;
18083
18084 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18085 && (!MINI_WINDOW_P (it->w)
18086 || (minibuf_level && EQ (it->window, minibuf_window))))
18087 row->indicate_empty_line_p = 1;
18088 }
18089
18090 it->continuation_lines_width = 0;
18091 row->ends_at_zv_p = 1;
18092 /* A row that displays right-to-left text must always have
18093 its last face extended all the way to the end of line,
18094 even if this row ends in ZV, because we still write to
18095 the screen left to right. */
18096 if (row->reversed_p)
18097 extend_face_to_end_of_line (it);
18098 break;
18099 }
18100
18101 /* Now, get the metrics of what we want to display. This also
18102 generates glyphs in `row' (which is IT->glyph_row). */
18103 n_glyphs_before = row->used[TEXT_AREA];
18104 x = it->current_x;
18105
18106 /* Remember the line height so far in case the next element doesn't
18107 fit on the line. */
18108 if (it->line_wrap != TRUNCATE)
18109 {
18110 ascent = it->max_ascent;
18111 descent = it->max_descent;
18112 phys_ascent = it->max_phys_ascent;
18113 phys_descent = it->max_phys_descent;
18114
18115 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18116 {
18117 if (IT_DISPLAYING_WHITESPACE (it))
18118 may_wrap = 1;
18119 else if (may_wrap)
18120 {
18121 wrap_it = *it;
18122 wrap_x = x;
18123 wrap_row_used = row->used[TEXT_AREA];
18124 wrap_row_ascent = row->ascent;
18125 wrap_row_height = row->height;
18126 wrap_row_phys_ascent = row->phys_ascent;
18127 wrap_row_phys_height = row->phys_height;
18128 wrap_row_extra_line_spacing = row->extra_line_spacing;
18129 wrap_row_min_pos = min_pos;
18130 wrap_row_min_bpos = min_bpos;
18131 wrap_row_max_pos = max_pos;
18132 wrap_row_max_bpos = max_bpos;
18133 may_wrap = 0;
18134 }
18135 }
18136 }
18137
18138 PRODUCE_GLYPHS (it);
18139
18140 /* If this display element was in marginal areas, continue with
18141 the next one. */
18142 if (it->area != TEXT_AREA)
18143 {
18144 row->ascent = max (row->ascent, it->max_ascent);
18145 row->height = max (row->height, it->max_ascent + it->max_descent);
18146 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18147 row->phys_height = max (row->phys_height,
18148 it->max_phys_ascent + it->max_phys_descent);
18149 row->extra_line_spacing = max (row->extra_line_spacing,
18150 it->max_extra_line_spacing);
18151 set_iterator_to_next (it, 1);
18152 continue;
18153 }
18154
18155 /* Does the display element fit on the line? If we truncate
18156 lines, we should draw past the right edge of the window. If
18157 we don't truncate, we want to stop so that we can display the
18158 continuation glyph before the right margin. If lines are
18159 continued, there are two possible strategies for characters
18160 resulting in more than 1 glyph (e.g. tabs): Display as many
18161 glyphs as possible in this line and leave the rest for the
18162 continuation line, or display the whole element in the next
18163 line. Original redisplay did the former, so we do it also. */
18164 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18165 hpos_before = it->hpos;
18166 x_before = x;
18167
18168 if (/* Not a newline. */
18169 nglyphs > 0
18170 /* Glyphs produced fit entirely in the line. */
18171 && it->current_x < it->last_visible_x)
18172 {
18173 it->hpos += nglyphs;
18174 row->ascent = max (row->ascent, it->max_ascent);
18175 row->height = max (row->height, it->max_ascent + it->max_descent);
18176 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18177 row->phys_height = max (row->phys_height,
18178 it->max_phys_ascent + it->max_phys_descent);
18179 row->extra_line_spacing = max (row->extra_line_spacing,
18180 it->max_extra_line_spacing);
18181 if (it->current_x - it->pixel_width < it->first_visible_x)
18182 row->x = x - it->first_visible_x;
18183 /* Record the maximum and minimum buffer positions seen so
18184 far in glyphs that will be displayed by this row. */
18185 if (it->bidi_p)
18186 RECORD_MAX_MIN_POS (it);
18187 }
18188 else
18189 {
18190 int i, new_x;
18191 struct glyph *glyph;
18192
18193 for (i = 0; i < nglyphs; ++i, x = new_x)
18194 {
18195 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18196 new_x = x + glyph->pixel_width;
18197
18198 if (/* Lines are continued. */
18199 it->line_wrap != TRUNCATE
18200 && (/* Glyph doesn't fit on the line. */
18201 new_x > it->last_visible_x
18202 /* Or it fits exactly on a window system frame. */
18203 || (new_x == it->last_visible_x
18204 && FRAME_WINDOW_P (it->f))))
18205 {
18206 /* End of a continued line. */
18207
18208 if (it->hpos == 0
18209 || (new_x == it->last_visible_x
18210 && FRAME_WINDOW_P (it->f)))
18211 {
18212 /* Current glyph is the only one on the line or
18213 fits exactly on the line. We must continue
18214 the line because we can't draw the cursor
18215 after the glyph. */
18216 row->continued_p = 1;
18217 it->current_x = new_x;
18218 it->continuation_lines_width += new_x;
18219 ++it->hpos;
18220 /* Record the maximum and minimum buffer
18221 positions seen so far in glyphs that will be
18222 displayed by this row. */
18223 if (it->bidi_p)
18224 RECORD_MAX_MIN_POS (it);
18225 if (i == nglyphs - 1)
18226 {
18227 /* If line-wrap is on, check if a previous
18228 wrap point was found. */
18229 if (wrap_row_used > 0
18230 /* Even if there is a previous wrap
18231 point, continue the line here as
18232 usual, if (i) the previous character
18233 was a space or tab AND (ii) the
18234 current character is not. */
18235 && (!may_wrap
18236 || IT_DISPLAYING_WHITESPACE (it)))
18237 goto back_to_wrap;
18238
18239 set_iterator_to_next (it, 1);
18240 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18241 {
18242 if (!get_next_display_element (it))
18243 {
18244 row->exact_window_width_line_p = 1;
18245 it->continuation_lines_width = 0;
18246 row->continued_p = 0;
18247 row->ends_at_zv_p = 1;
18248 }
18249 else if (ITERATOR_AT_END_OF_LINE_P (it))
18250 {
18251 row->continued_p = 0;
18252 row->exact_window_width_line_p = 1;
18253 }
18254 }
18255 }
18256 }
18257 else if (CHAR_GLYPH_PADDING_P (*glyph)
18258 && !FRAME_WINDOW_P (it->f))
18259 {
18260 /* A padding glyph that doesn't fit on this line.
18261 This means the whole character doesn't fit
18262 on the line. */
18263 if (row->reversed_p)
18264 unproduce_glyphs (it, row->used[TEXT_AREA]
18265 - n_glyphs_before);
18266 row->used[TEXT_AREA] = n_glyphs_before;
18267
18268 /* Fill the rest of the row with continuation
18269 glyphs like in 20.x. */
18270 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18271 < row->glyphs[1 + TEXT_AREA])
18272 produce_special_glyphs (it, IT_CONTINUATION);
18273
18274 row->continued_p = 1;
18275 it->current_x = x_before;
18276 it->continuation_lines_width += x_before;
18277
18278 /* Restore the height to what it was before the
18279 element not fitting on the line. */
18280 it->max_ascent = ascent;
18281 it->max_descent = descent;
18282 it->max_phys_ascent = phys_ascent;
18283 it->max_phys_descent = phys_descent;
18284 }
18285 else if (wrap_row_used > 0)
18286 {
18287 back_to_wrap:
18288 if (row->reversed_p)
18289 unproduce_glyphs (it,
18290 row->used[TEXT_AREA] - wrap_row_used);
18291 *it = wrap_it;
18292 it->continuation_lines_width += wrap_x;
18293 row->used[TEXT_AREA] = wrap_row_used;
18294 row->ascent = wrap_row_ascent;
18295 row->height = wrap_row_height;
18296 row->phys_ascent = wrap_row_phys_ascent;
18297 row->phys_height = wrap_row_phys_height;
18298 row->extra_line_spacing = wrap_row_extra_line_spacing;
18299 min_pos = wrap_row_min_pos;
18300 min_bpos = wrap_row_min_bpos;
18301 max_pos = wrap_row_max_pos;
18302 max_bpos = wrap_row_max_bpos;
18303 row->continued_p = 1;
18304 row->ends_at_zv_p = 0;
18305 row->exact_window_width_line_p = 0;
18306 it->continuation_lines_width += x;
18307
18308 /* Make sure that a non-default face is extended
18309 up to the right margin of the window. */
18310 extend_face_to_end_of_line (it);
18311 }
18312 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18313 {
18314 /* A TAB that extends past the right edge of the
18315 window. This produces a single glyph on
18316 window system frames. We leave the glyph in
18317 this row and let it fill the row, but don't
18318 consume the TAB. */
18319 it->continuation_lines_width += it->last_visible_x;
18320 row->ends_in_middle_of_char_p = 1;
18321 row->continued_p = 1;
18322 glyph->pixel_width = it->last_visible_x - x;
18323 it->starts_in_middle_of_char_p = 1;
18324 }
18325 else
18326 {
18327 /* Something other than a TAB that draws past
18328 the right edge of the window. Restore
18329 positions to values before the element. */
18330 if (row->reversed_p)
18331 unproduce_glyphs (it, row->used[TEXT_AREA]
18332 - (n_glyphs_before + i));
18333 row->used[TEXT_AREA] = n_glyphs_before + i;
18334
18335 /* Display continuation glyphs. */
18336 if (!FRAME_WINDOW_P (it->f))
18337 produce_special_glyphs (it, IT_CONTINUATION);
18338 row->continued_p = 1;
18339
18340 it->current_x = x_before;
18341 it->continuation_lines_width += x;
18342 extend_face_to_end_of_line (it);
18343
18344 if (nglyphs > 1 && i > 0)
18345 {
18346 row->ends_in_middle_of_char_p = 1;
18347 it->starts_in_middle_of_char_p = 1;
18348 }
18349
18350 /* Restore the height to what it was before the
18351 element not fitting on the line. */
18352 it->max_ascent = ascent;
18353 it->max_descent = descent;
18354 it->max_phys_ascent = phys_ascent;
18355 it->max_phys_descent = phys_descent;
18356 }
18357
18358 break;
18359 }
18360 else if (new_x > it->first_visible_x)
18361 {
18362 /* Increment number of glyphs actually displayed. */
18363 ++it->hpos;
18364
18365 /* Record the maximum and minimum buffer positions
18366 seen so far in glyphs that will be displayed by
18367 this row. */
18368 if (it->bidi_p)
18369 RECORD_MAX_MIN_POS (it);
18370
18371 if (x < it->first_visible_x)
18372 /* Glyph is partially visible, i.e. row starts at
18373 negative X position. */
18374 row->x = x - it->first_visible_x;
18375 }
18376 else
18377 {
18378 /* Glyph is completely off the left margin of the
18379 window. This should not happen because of the
18380 move_it_in_display_line at the start of this
18381 function, unless the text display area of the
18382 window is empty. */
18383 xassert (it->first_visible_x <= it->last_visible_x);
18384 }
18385 }
18386
18387 row->ascent = max (row->ascent, it->max_ascent);
18388 row->height = max (row->height, it->max_ascent + it->max_descent);
18389 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18390 row->phys_height = max (row->phys_height,
18391 it->max_phys_ascent + it->max_phys_descent);
18392 row->extra_line_spacing = max (row->extra_line_spacing,
18393 it->max_extra_line_spacing);
18394
18395 /* End of this display line if row is continued. */
18396 if (row->continued_p || row->ends_at_zv_p)
18397 break;
18398 }
18399
18400 at_end_of_line:
18401 /* Is this a line end? If yes, we're also done, after making
18402 sure that a non-default face is extended up to the right
18403 margin of the window. */
18404 if (ITERATOR_AT_END_OF_LINE_P (it))
18405 {
18406 int used_before = row->used[TEXT_AREA];
18407
18408 row->ends_in_newline_from_string_p = STRINGP (it->object);
18409
18410 /* Add a space at the end of the line that is used to
18411 display the cursor there. */
18412 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18413 append_space_for_newline (it, 0);
18414
18415 /* Extend the face to the end of the line. */
18416 extend_face_to_end_of_line (it);
18417
18418 /* Make sure we have the position. */
18419 if (used_before == 0)
18420 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18421
18422 /* Record the position of the newline, for use in
18423 find_row_edges. */
18424 it->eol_pos = it->current.pos;
18425
18426 /* Consume the line end. This skips over invisible lines. */
18427 set_iterator_to_next (it, 1);
18428 it->continuation_lines_width = 0;
18429 break;
18430 }
18431
18432 /* Proceed with next display element. Note that this skips
18433 over lines invisible because of selective display. */
18434 set_iterator_to_next (it, 1);
18435
18436 /* If we truncate lines, we are done when the last displayed
18437 glyphs reach past the right margin of the window. */
18438 if (it->line_wrap == TRUNCATE
18439 && (FRAME_WINDOW_P (it->f)
18440 ? (it->current_x >= it->last_visible_x)
18441 : (it->current_x > it->last_visible_x)))
18442 {
18443 /* Maybe add truncation glyphs. */
18444 if (!FRAME_WINDOW_P (it->f))
18445 {
18446 int i, n;
18447
18448 if (!row->reversed_p)
18449 {
18450 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18451 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18452 break;
18453 }
18454 else
18455 {
18456 for (i = 0; i < row->used[TEXT_AREA]; i++)
18457 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18458 break;
18459 /* Remove any padding glyphs at the front of ROW, to
18460 make room for the truncation glyphs we will be
18461 adding below. The loop below always inserts at
18462 least one truncation glyph, so also remove the
18463 last glyph added to ROW. */
18464 unproduce_glyphs (it, i + 1);
18465 /* Adjust i for the loop below. */
18466 i = row->used[TEXT_AREA] - (i + 1);
18467 }
18468
18469 for (n = row->used[TEXT_AREA]; i < n; ++i)
18470 {
18471 row->used[TEXT_AREA] = i;
18472 produce_special_glyphs (it, IT_TRUNCATION);
18473 }
18474 }
18475 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18476 {
18477 /* Don't truncate if we can overflow newline into fringe. */
18478 if (!get_next_display_element (it))
18479 {
18480 it->continuation_lines_width = 0;
18481 row->ends_at_zv_p = 1;
18482 row->exact_window_width_line_p = 1;
18483 break;
18484 }
18485 if (ITERATOR_AT_END_OF_LINE_P (it))
18486 {
18487 row->exact_window_width_line_p = 1;
18488 goto at_end_of_line;
18489 }
18490 }
18491
18492 row->truncated_on_right_p = 1;
18493 it->continuation_lines_width = 0;
18494 reseat_at_next_visible_line_start (it, 0);
18495 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18496 it->hpos = hpos_before;
18497 it->current_x = x_before;
18498 break;
18499 }
18500 }
18501
18502 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18503 at the left window margin. */
18504 if (it->first_visible_x
18505 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18506 {
18507 if (!FRAME_WINDOW_P (it->f))
18508 insert_left_trunc_glyphs (it);
18509 row->truncated_on_left_p = 1;
18510 }
18511
18512 /* Remember the position at which this line ends.
18513
18514 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18515 cannot be before the call to find_row_edges below, since that is
18516 where these positions are determined. */
18517 row->end = it->current;
18518 if (!it->bidi_p)
18519 {
18520 row->minpos = row->start.pos;
18521 row->maxpos = row->end.pos;
18522 }
18523 else
18524 {
18525 /* ROW->minpos and ROW->maxpos must be the smallest and
18526 `1 + the largest' buffer positions in ROW. But if ROW was
18527 bidi-reordered, these two positions can be anywhere in the
18528 row, so we must determine them now. */
18529 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18530 }
18531
18532 /* If the start of this line is the overlay arrow-position, then
18533 mark this glyph row as the one containing the overlay arrow.
18534 This is clearly a mess with variable size fonts. It would be
18535 better to let it be displayed like cursors under X. */
18536 if ((row->displays_text_p || !overlay_arrow_seen)
18537 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18538 !NILP (overlay_arrow_string)))
18539 {
18540 /* Overlay arrow in window redisplay is a fringe bitmap. */
18541 if (STRINGP (overlay_arrow_string))
18542 {
18543 struct glyph_row *arrow_row
18544 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18545 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18546 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18547 struct glyph *p = row->glyphs[TEXT_AREA];
18548 struct glyph *p2, *end;
18549
18550 /* Copy the arrow glyphs. */
18551 while (glyph < arrow_end)
18552 *p++ = *glyph++;
18553
18554 /* Throw away padding glyphs. */
18555 p2 = p;
18556 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18557 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18558 ++p2;
18559 if (p2 > p)
18560 {
18561 while (p2 < end)
18562 *p++ = *p2++;
18563 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18564 }
18565 }
18566 else
18567 {
18568 xassert (INTEGERP (overlay_arrow_string));
18569 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18570 }
18571 overlay_arrow_seen = 1;
18572 }
18573
18574 /* Compute pixel dimensions of this line. */
18575 compute_line_metrics (it);
18576
18577 /* Record whether this row ends inside an ellipsis. */
18578 row->ends_in_ellipsis_p
18579 = (it->method == GET_FROM_DISPLAY_VECTOR
18580 && it->ellipsis_p);
18581
18582 /* Save fringe bitmaps in this row. */
18583 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18584 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18585 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18586 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18587
18588 it->left_user_fringe_bitmap = 0;
18589 it->left_user_fringe_face_id = 0;
18590 it->right_user_fringe_bitmap = 0;
18591 it->right_user_fringe_face_id = 0;
18592
18593 /* Maybe set the cursor. */
18594 cvpos = it->w->cursor.vpos;
18595 if ((cvpos < 0
18596 /* In bidi-reordered rows, keep checking for proper cursor
18597 position even if one has been found already, because buffer
18598 positions in such rows change non-linearly with ROW->VPOS,
18599 when a line is continued. One exception: when we are at ZV,
18600 display cursor on the first suitable glyph row, since all
18601 the empty rows after that also have their position set to ZV. */
18602 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18603 lines' rows is implemented for bidi-reordered rows. */
18604 || (it->bidi_p
18605 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18606 && PT >= MATRIX_ROW_START_CHARPOS (row)
18607 && PT <= MATRIX_ROW_END_CHARPOS (row)
18608 && cursor_row_p (row))
18609 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18610
18611 /* Highlight trailing whitespace. */
18612 if (!NILP (Vshow_trailing_whitespace))
18613 highlight_trailing_whitespace (it->f, it->glyph_row);
18614
18615 /* Prepare for the next line. This line starts horizontally at (X
18616 HPOS) = (0 0). Vertical positions are incremented. As a
18617 convenience for the caller, IT->glyph_row is set to the next
18618 row to be used. */
18619 it->current_x = it->hpos = 0;
18620 it->current_y += row->height;
18621 SET_TEXT_POS (it->eol_pos, 0, 0);
18622 ++it->vpos;
18623 ++it->glyph_row;
18624 /* The next row should by default use the same value of the
18625 reversed_p flag as this one. set_iterator_to_next decides when
18626 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18627 the flag accordingly. */
18628 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18629 it->glyph_row->reversed_p = row->reversed_p;
18630 it->start = row->end;
18631 return row->displays_text_p;
18632
18633 #undef RECORD_MAX_MIN_POS
18634 }
18635
18636 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18637 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18638 doc: /* Return paragraph direction at point in BUFFER.
18639 Value is either `left-to-right' or `right-to-left'.
18640 If BUFFER is omitted or nil, it defaults to the current buffer.
18641
18642 Paragraph direction determines how the text in the paragraph is displayed.
18643 In left-to-right paragraphs, text begins at the left margin of the window
18644 and the reading direction is generally left to right. In right-to-left
18645 paragraphs, text begins at the right margin and is read from right to left.
18646
18647 See also `bidi-paragraph-direction'. */)
18648 (Lisp_Object buffer)
18649 {
18650 struct buffer *buf = current_buffer;
18651 struct buffer *old = buf;
18652
18653 if (! NILP (buffer))
18654 {
18655 CHECK_BUFFER (buffer);
18656 buf = XBUFFER (buffer);
18657 }
18658
18659 if (NILP (BVAR (buf, bidi_display_reordering)))
18660 return Qleft_to_right;
18661 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18662 return BVAR (buf, bidi_paragraph_direction);
18663 else
18664 {
18665 /* Determine the direction from buffer text. We could try to
18666 use current_matrix if it is up to date, but this seems fast
18667 enough as it is. */
18668 struct bidi_it itb;
18669 EMACS_INT pos = BUF_PT (buf);
18670 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18671 int c;
18672
18673 set_buffer_temp (buf);
18674 /* bidi_paragraph_init finds the base direction of the paragraph
18675 by searching forward from paragraph start. We need the base
18676 direction of the current or _previous_ paragraph, so we need
18677 to make sure we are within that paragraph. To that end, find
18678 the previous non-empty line. */
18679 if (pos >= ZV && pos > BEGV)
18680 {
18681 pos--;
18682 bytepos = CHAR_TO_BYTE (pos);
18683 }
18684 while ((c = FETCH_BYTE (bytepos)) == '\n'
18685 || c == ' ' || c == '\t' || c == '\f')
18686 {
18687 if (bytepos <= BEGV_BYTE)
18688 break;
18689 bytepos--;
18690 pos--;
18691 }
18692 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18693 bytepos--;
18694 itb.charpos = pos;
18695 itb.bytepos = bytepos;
18696 itb.nchars = -1;
18697 itb.string.s = NULL;
18698 itb.string.lstring = Qnil;
18699 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18700 itb.first_elt = 1;
18701 itb.separator_limit = -1;
18702 itb.paragraph_dir = NEUTRAL_DIR;
18703
18704 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18705 set_buffer_temp (old);
18706 switch (itb.paragraph_dir)
18707 {
18708 case L2R:
18709 return Qleft_to_right;
18710 break;
18711 case R2L:
18712 return Qright_to_left;
18713 break;
18714 default:
18715 abort ();
18716 }
18717 }
18718 }
18719
18720
18721 \f
18722 /***********************************************************************
18723 Menu Bar
18724 ***********************************************************************/
18725
18726 /* Redisplay the menu bar in the frame for window W.
18727
18728 The menu bar of X frames that don't have X toolkit support is
18729 displayed in a special window W->frame->menu_bar_window.
18730
18731 The menu bar of terminal frames is treated specially as far as
18732 glyph matrices are concerned. Menu bar lines are not part of
18733 windows, so the update is done directly on the frame matrix rows
18734 for the menu bar. */
18735
18736 static void
18737 display_menu_bar (struct window *w)
18738 {
18739 struct frame *f = XFRAME (WINDOW_FRAME (w));
18740 struct it it;
18741 Lisp_Object items;
18742 int i;
18743
18744 /* Don't do all this for graphical frames. */
18745 #ifdef HAVE_NTGUI
18746 if (FRAME_W32_P (f))
18747 return;
18748 #endif
18749 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18750 if (FRAME_X_P (f))
18751 return;
18752 #endif
18753
18754 #ifdef HAVE_NS
18755 if (FRAME_NS_P (f))
18756 return;
18757 #endif /* HAVE_NS */
18758
18759 #ifdef USE_X_TOOLKIT
18760 xassert (!FRAME_WINDOW_P (f));
18761 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18762 it.first_visible_x = 0;
18763 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18764 #else /* not USE_X_TOOLKIT */
18765 if (FRAME_WINDOW_P (f))
18766 {
18767 /* Menu bar lines are displayed in the desired matrix of the
18768 dummy window menu_bar_window. */
18769 struct window *menu_w;
18770 xassert (WINDOWP (f->menu_bar_window));
18771 menu_w = XWINDOW (f->menu_bar_window);
18772 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18773 MENU_FACE_ID);
18774 it.first_visible_x = 0;
18775 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18776 }
18777 else
18778 {
18779 /* This is a TTY frame, i.e. character hpos/vpos are used as
18780 pixel x/y. */
18781 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18782 MENU_FACE_ID);
18783 it.first_visible_x = 0;
18784 it.last_visible_x = FRAME_COLS (f);
18785 }
18786 #endif /* not USE_X_TOOLKIT */
18787
18788 /* FIXME: This should be controlled by a user option. See the
18789 comments in redisplay_tool_bar and display_mode_line about
18790 this. */
18791 it.paragraph_embedding = L2R;
18792
18793 if (! mode_line_inverse_video)
18794 /* Force the menu-bar to be displayed in the default face. */
18795 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18796
18797 /* Clear all rows of the menu bar. */
18798 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18799 {
18800 struct glyph_row *row = it.glyph_row + i;
18801 clear_glyph_row (row);
18802 row->enabled_p = 1;
18803 row->full_width_p = 1;
18804 }
18805
18806 /* Display all items of the menu bar. */
18807 items = FRAME_MENU_BAR_ITEMS (it.f);
18808 for (i = 0; i < ASIZE (items); i += 4)
18809 {
18810 Lisp_Object string;
18811
18812 /* Stop at nil string. */
18813 string = AREF (items, i + 1);
18814 if (NILP (string))
18815 break;
18816
18817 /* Remember where item was displayed. */
18818 ASET (items, i + 3, make_number (it.hpos));
18819
18820 /* Display the item, pad with one space. */
18821 if (it.current_x < it.last_visible_x)
18822 display_string (NULL, string, Qnil, 0, 0, &it,
18823 SCHARS (string) + 1, 0, 0, -1);
18824 }
18825
18826 /* Fill out the line with spaces. */
18827 if (it.current_x < it.last_visible_x)
18828 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18829
18830 /* Compute the total height of the lines. */
18831 compute_line_metrics (&it);
18832 }
18833
18834
18835 \f
18836 /***********************************************************************
18837 Mode Line
18838 ***********************************************************************/
18839
18840 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18841 FORCE is non-zero, redisplay mode lines unconditionally.
18842 Otherwise, redisplay only mode lines that are garbaged. Value is
18843 the number of windows whose mode lines were redisplayed. */
18844
18845 static int
18846 redisplay_mode_lines (Lisp_Object window, int force)
18847 {
18848 int nwindows = 0;
18849
18850 while (!NILP (window))
18851 {
18852 struct window *w = XWINDOW (window);
18853
18854 if (WINDOWP (w->hchild))
18855 nwindows += redisplay_mode_lines (w->hchild, force);
18856 else if (WINDOWP (w->vchild))
18857 nwindows += redisplay_mode_lines (w->vchild, force);
18858 else if (force
18859 || FRAME_GARBAGED_P (XFRAME (w->frame))
18860 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18861 {
18862 struct text_pos lpoint;
18863 struct buffer *old = current_buffer;
18864
18865 /* Set the window's buffer for the mode line display. */
18866 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18867 set_buffer_internal_1 (XBUFFER (w->buffer));
18868
18869 /* Point refers normally to the selected window. For any
18870 other window, set up appropriate value. */
18871 if (!EQ (window, selected_window))
18872 {
18873 struct text_pos pt;
18874
18875 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18876 if (CHARPOS (pt) < BEGV)
18877 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
18878 else if (CHARPOS (pt) > (ZV - 1))
18879 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
18880 else
18881 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
18882 }
18883
18884 /* Display mode lines. */
18885 clear_glyph_matrix (w->desired_matrix);
18886 if (display_mode_lines (w))
18887 {
18888 ++nwindows;
18889 w->must_be_updated_p = 1;
18890 }
18891
18892 /* Restore old settings. */
18893 set_buffer_internal_1 (old);
18894 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
18895 }
18896
18897 window = w->next;
18898 }
18899
18900 return nwindows;
18901 }
18902
18903
18904 /* Display the mode and/or header line of window W. Value is the
18905 sum number of mode lines and header lines displayed. */
18906
18907 static int
18908 display_mode_lines (struct window *w)
18909 {
18910 Lisp_Object old_selected_window, old_selected_frame;
18911 int n = 0;
18912
18913 old_selected_frame = selected_frame;
18914 selected_frame = w->frame;
18915 old_selected_window = selected_window;
18916 XSETWINDOW (selected_window, w);
18917
18918 /* These will be set while the mode line specs are processed. */
18919 line_number_displayed = 0;
18920 w->column_number_displayed = Qnil;
18921
18922 if (WINDOW_WANTS_MODELINE_P (w))
18923 {
18924 struct window *sel_w = XWINDOW (old_selected_window);
18925
18926 /* Select mode line face based on the real selected window. */
18927 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
18928 BVAR (current_buffer, mode_line_format));
18929 ++n;
18930 }
18931
18932 if (WINDOW_WANTS_HEADER_LINE_P (w))
18933 {
18934 display_mode_line (w, HEADER_LINE_FACE_ID,
18935 BVAR (current_buffer, header_line_format));
18936 ++n;
18937 }
18938
18939 selected_frame = old_selected_frame;
18940 selected_window = old_selected_window;
18941 return n;
18942 }
18943
18944
18945 /* Display mode or header line of window W. FACE_ID specifies which
18946 line to display; it is either MODE_LINE_FACE_ID or
18947 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
18948 display. Value is the pixel height of the mode/header line
18949 displayed. */
18950
18951 static int
18952 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
18953 {
18954 struct it it;
18955 struct face *face;
18956 int count = SPECPDL_INDEX ();
18957
18958 init_iterator (&it, w, -1, -1, NULL, face_id);
18959 /* Don't extend on a previously drawn mode-line.
18960 This may happen if called from pos_visible_p. */
18961 it.glyph_row->enabled_p = 0;
18962 prepare_desired_row (it.glyph_row);
18963
18964 it.glyph_row->mode_line_p = 1;
18965
18966 if (! mode_line_inverse_video)
18967 /* Force the mode-line to be displayed in the default face. */
18968 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18969
18970 /* FIXME: This should be controlled by a user option. But
18971 supporting such an option is not trivial, since the mode line is
18972 made up of many separate strings, most of which are normally
18973 unibyte, and unibyte strings currently don't get reordered for
18974 display. */
18975 it.paragraph_embedding = L2R;
18976
18977 record_unwind_protect (unwind_format_mode_line,
18978 format_mode_line_unwind_data (NULL, Qnil, 0));
18979
18980 mode_line_target = MODE_LINE_DISPLAY;
18981
18982 /* Temporarily make frame's keyboard the current kboard so that
18983 kboard-local variables in the mode_line_format will get the right
18984 values. */
18985 push_kboard (FRAME_KBOARD (it.f));
18986 record_unwind_save_match_data ();
18987 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18988 pop_kboard ();
18989
18990 unbind_to (count, Qnil);
18991
18992 /* Fill up with spaces. */
18993 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
18994
18995 compute_line_metrics (&it);
18996 it.glyph_row->full_width_p = 1;
18997 it.glyph_row->continued_p = 0;
18998 it.glyph_row->truncated_on_left_p = 0;
18999 it.glyph_row->truncated_on_right_p = 0;
19000
19001 /* Make a 3D mode-line have a shadow at its right end. */
19002 face = FACE_FROM_ID (it.f, face_id);
19003 extend_face_to_end_of_line (&it);
19004 if (face->box != FACE_NO_BOX)
19005 {
19006 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19007 + it.glyph_row->used[TEXT_AREA] - 1);
19008 last->right_box_line_p = 1;
19009 }
19010
19011 return it.glyph_row->height;
19012 }
19013
19014 /* Move element ELT in LIST to the front of LIST.
19015 Return the updated list. */
19016
19017 static Lisp_Object
19018 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19019 {
19020 register Lisp_Object tail, prev;
19021 register Lisp_Object tem;
19022
19023 tail = list;
19024 prev = Qnil;
19025 while (CONSP (tail))
19026 {
19027 tem = XCAR (tail);
19028
19029 if (EQ (elt, tem))
19030 {
19031 /* Splice out the link TAIL. */
19032 if (NILP (prev))
19033 list = XCDR (tail);
19034 else
19035 Fsetcdr (prev, XCDR (tail));
19036
19037 /* Now make it the first. */
19038 Fsetcdr (tail, list);
19039 return tail;
19040 }
19041 else
19042 prev = tail;
19043 tail = XCDR (tail);
19044 QUIT;
19045 }
19046
19047 /* Not found--return unchanged LIST. */
19048 return list;
19049 }
19050
19051 /* Contribute ELT to the mode line for window IT->w. How it
19052 translates into text depends on its data type.
19053
19054 IT describes the display environment in which we display, as usual.
19055
19056 DEPTH is the depth in recursion. It is used to prevent
19057 infinite recursion here.
19058
19059 FIELD_WIDTH is the number of characters the display of ELT should
19060 occupy in the mode line, and PRECISION is the maximum number of
19061 characters to display from ELT's representation. See
19062 display_string for details.
19063
19064 Returns the hpos of the end of the text generated by ELT.
19065
19066 PROPS is a property list to add to any string we encounter.
19067
19068 If RISKY is nonzero, remove (disregard) any properties in any string
19069 we encounter, and ignore :eval and :propertize.
19070
19071 The global variable `mode_line_target' determines whether the
19072 output is passed to `store_mode_line_noprop',
19073 `store_mode_line_string', or `display_string'. */
19074
19075 static int
19076 display_mode_element (struct it *it, int depth, int field_width, int precision,
19077 Lisp_Object elt, Lisp_Object props, int risky)
19078 {
19079 int n = 0, field, prec;
19080 int literal = 0;
19081
19082 tail_recurse:
19083 if (depth > 100)
19084 elt = build_string ("*too-deep*");
19085
19086 depth++;
19087
19088 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19089 {
19090 case Lisp_String:
19091 {
19092 /* A string: output it and check for %-constructs within it. */
19093 unsigned char c;
19094 EMACS_INT offset = 0;
19095
19096 if (SCHARS (elt) > 0
19097 && (!NILP (props) || risky))
19098 {
19099 Lisp_Object oprops, aelt;
19100 oprops = Ftext_properties_at (make_number (0), elt);
19101
19102 /* If the starting string's properties are not what
19103 we want, translate the string. Also, if the string
19104 is risky, do that anyway. */
19105
19106 if (NILP (Fequal (props, oprops)) || risky)
19107 {
19108 /* If the starting string has properties,
19109 merge the specified ones onto the existing ones. */
19110 if (! NILP (oprops) && !risky)
19111 {
19112 Lisp_Object tem;
19113
19114 oprops = Fcopy_sequence (oprops);
19115 tem = props;
19116 while (CONSP (tem))
19117 {
19118 oprops = Fplist_put (oprops, XCAR (tem),
19119 XCAR (XCDR (tem)));
19120 tem = XCDR (XCDR (tem));
19121 }
19122 props = oprops;
19123 }
19124
19125 aelt = Fassoc (elt, mode_line_proptrans_alist);
19126 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19127 {
19128 /* AELT is what we want. Move it to the front
19129 without consing. */
19130 elt = XCAR (aelt);
19131 mode_line_proptrans_alist
19132 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19133 }
19134 else
19135 {
19136 Lisp_Object tem;
19137
19138 /* If AELT has the wrong props, it is useless.
19139 so get rid of it. */
19140 if (! NILP (aelt))
19141 mode_line_proptrans_alist
19142 = Fdelq (aelt, mode_line_proptrans_alist);
19143
19144 elt = Fcopy_sequence (elt);
19145 Fset_text_properties (make_number (0), Flength (elt),
19146 props, elt);
19147 /* Add this item to mode_line_proptrans_alist. */
19148 mode_line_proptrans_alist
19149 = Fcons (Fcons (elt, props),
19150 mode_line_proptrans_alist);
19151 /* Truncate mode_line_proptrans_alist
19152 to at most 50 elements. */
19153 tem = Fnthcdr (make_number (50),
19154 mode_line_proptrans_alist);
19155 if (! NILP (tem))
19156 XSETCDR (tem, Qnil);
19157 }
19158 }
19159 }
19160
19161 offset = 0;
19162
19163 if (literal)
19164 {
19165 prec = precision - n;
19166 switch (mode_line_target)
19167 {
19168 case MODE_LINE_NOPROP:
19169 case MODE_LINE_TITLE:
19170 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19171 break;
19172 case MODE_LINE_STRING:
19173 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19174 break;
19175 case MODE_LINE_DISPLAY:
19176 n += display_string (NULL, elt, Qnil, 0, 0, it,
19177 0, prec, 0, STRING_MULTIBYTE (elt));
19178 break;
19179 }
19180
19181 break;
19182 }
19183
19184 /* Handle the non-literal case. */
19185
19186 while ((precision <= 0 || n < precision)
19187 && SREF (elt, offset) != 0
19188 && (mode_line_target != MODE_LINE_DISPLAY
19189 || it->current_x < it->last_visible_x))
19190 {
19191 EMACS_INT last_offset = offset;
19192
19193 /* Advance to end of string or next format specifier. */
19194 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19195 ;
19196
19197 if (offset - 1 != last_offset)
19198 {
19199 EMACS_INT nchars, nbytes;
19200
19201 /* Output to end of string or up to '%'. Field width
19202 is length of string. Don't output more than
19203 PRECISION allows us. */
19204 offset--;
19205
19206 prec = c_string_width (SDATA (elt) + last_offset,
19207 offset - last_offset, precision - n,
19208 &nchars, &nbytes);
19209
19210 switch (mode_line_target)
19211 {
19212 case MODE_LINE_NOPROP:
19213 case MODE_LINE_TITLE:
19214 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19215 break;
19216 case MODE_LINE_STRING:
19217 {
19218 EMACS_INT bytepos = last_offset;
19219 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19220 EMACS_INT endpos = (precision <= 0
19221 ? string_byte_to_char (elt, offset)
19222 : charpos + nchars);
19223
19224 n += store_mode_line_string (NULL,
19225 Fsubstring (elt, make_number (charpos),
19226 make_number (endpos)),
19227 0, 0, 0, Qnil);
19228 }
19229 break;
19230 case MODE_LINE_DISPLAY:
19231 {
19232 EMACS_INT bytepos = last_offset;
19233 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19234
19235 if (precision <= 0)
19236 nchars = string_byte_to_char (elt, offset) - charpos;
19237 n += display_string (NULL, elt, Qnil, 0, charpos,
19238 it, 0, nchars, 0,
19239 STRING_MULTIBYTE (elt));
19240 }
19241 break;
19242 }
19243 }
19244 else /* c == '%' */
19245 {
19246 EMACS_INT percent_position = offset;
19247
19248 /* Get the specified minimum width. Zero means
19249 don't pad. */
19250 field = 0;
19251 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19252 field = field * 10 + c - '0';
19253
19254 /* Don't pad beyond the total padding allowed. */
19255 if (field_width - n > 0 && field > field_width - n)
19256 field = field_width - n;
19257
19258 /* Note that either PRECISION <= 0 or N < PRECISION. */
19259 prec = precision - n;
19260
19261 if (c == 'M')
19262 n += display_mode_element (it, depth, field, prec,
19263 Vglobal_mode_string, props,
19264 risky);
19265 else if (c != 0)
19266 {
19267 int multibyte;
19268 EMACS_INT bytepos, charpos;
19269 const char *spec;
19270 Lisp_Object string;
19271
19272 bytepos = percent_position;
19273 charpos = (STRING_MULTIBYTE (elt)
19274 ? string_byte_to_char (elt, bytepos)
19275 : bytepos);
19276 spec = decode_mode_spec (it->w, c, field, &string);
19277 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19278
19279 switch (mode_line_target)
19280 {
19281 case MODE_LINE_NOPROP:
19282 case MODE_LINE_TITLE:
19283 n += store_mode_line_noprop (spec, field, prec);
19284 break;
19285 case MODE_LINE_STRING:
19286 {
19287 int len = strlen (spec);
19288 Lisp_Object tem = make_string (spec, len);
19289 props = Ftext_properties_at (make_number (charpos), elt);
19290 /* Should only keep face property in props */
19291 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19292 }
19293 break;
19294 case MODE_LINE_DISPLAY:
19295 {
19296 int nglyphs_before, nwritten;
19297
19298 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19299 nwritten = display_string (spec, string, elt,
19300 charpos, 0, it,
19301 field, prec, 0,
19302 multibyte);
19303
19304 /* Assign to the glyphs written above the
19305 string where the `%x' came from, position
19306 of the `%'. */
19307 if (nwritten > 0)
19308 {
19309 struct glyph *glyph
19310 = (it->glyph_row->glyphs[TEXT_AREA]
19311 + nglyphs_before);
19312 int i;
19313
19314 for (i = 0; i < nwritten; ++i)
19315 {
19316 glyph[i].object = elt;
19317 glyph[i].charpos = charpos;
19318 }
19319
19320 n += nwritten;
19321 }
19322 }
19323 break;
19324 }
19325 }
19326 else /* c == 0 */
19327 break;
19328 }
19329 }
19330 }
19331 break;
19332
19333 case Lisp_Symbol:
19334 /* A symbol: process the value of the symbol recursively
19335 as if it appeared here directly. Avoid error if symbol void.
19336 Special case: if value of symbol is a string, output the string
19337 literally. */
19338 {
19339 register Lisp_Object tem;
19340
19341 /* If the variable is not marked as risky to set
19342 then its contents are risky to use. */
19343 if (NILP (Fget (elt, Qrisky_local_variable)))
19344 risky = 1;
19345
19346 tem = Fboundp (elt);
19347 if (!NILP (tem))
19348 {
19349 tem = Fsymbol_value (elt);
19350 /* If value is a string, output that string literally:
19351 don't check for % within it. */
19352 if (STRINGP (tem))
19353 literal = 1;
19354
19355 if (!EQ (tem, elt))
19356 {
19357 /* Give up right away for nil or t. */
19358 elt = tem;
19359 goto tail_recurse;
19360 }
19361 }
19362 }
19363 break;
19364
19365 case Lisp_Cons:
19366 {
19367 register Lisp_Object car, tem;
19368
19369 /* A cons cell: five distinct cases.
19370 If first element is :eval or :propertize, do something special.
19371 If first element is a string or a cons, process all the elements
19372 and effectively concatenate them.
19373 If first element is a negative number, truncate displaying cdr to
19374 at most that many characters. If positive, pad (with spaces)
19375 to at least that many characters.
19376 If first element is a symbol, process the cadr or caddr recursively
19377 according to whether the symbol's value is non-nil or nil. */
19378 car = XCAR (elt);
19379 if (EQ (car, QCeval))
19380 {
19381 /* An element of the form (:eval FORM) means evaluate FORM
19382 and use the result as mode line elements. */
19383
19384 if (risky)
19385 break;
19386
19387 if (CONSP (XCDR (elt)))
19388 {
19389 Lisp_Object spec;
19390 spec = safe_eval (XCAR (XCDR (elt)));
19391 n += display_mode_element (it, depth, field_width - n,
19392 precision - n, spec, props,
19393 risky);
19394 }
19395 }
19396 else if (EQ (car, QCpropertize))
19397 {
19398 /* An element of the form (:propertize ELT PROPS...)
19399 means display ELT but applying properties PROPS. */
19400
19401 if (risky)
19402 break;
19403
19404 if (CONSP (XCDR (elt)))
19405 n += display_mode_element (it, depth, field_width - n,
19406 precision - n, XCAR (XCDR (elt)),
19407 XCDR (XCDR (elt)), risky);
19408 }
19409 else if (SYMBOLP (car))
19410 {
19411 tem = Fboundp (car);
19412 elt = XCDR (elt);
19413 if (!CONSP (elt))
19414 goto invalid;
19415 /* elt is now the cdr, and we know it is a cons cell.
19416 Use its car if CAR has a non-nil value. */
19417 if (!NILP (tem))
19418 {
19419 tem = Fsymbol_value (car);
19420 if (!NILP (tem))
19421 {
19422 elt = XCAR (elt);
19423 goto tail_recurse;
19424 }
19425 }
19426 /* Symbol's value is nil (or symbol is unbound)
19427 Get the cddr of the original list
19428 and if possible find the caddr and use that. */
19429 elt = XCDR (elt);
19430 if (NILP (elt))
19431 break;
19432 else if (!CONSP (elt))
19433 goto invalid;
19434 elt = XCAR (elt);
19435 goto tail_recurse;
19436 }
19437 else if (INTEGERP (car))
19438 {
19439 register int lim = XINT (car);
19440 elt = XCDR (elt);
19441 if (lim < 0)
19442 {
19443 /* Negative int means reduce maximum width. */
19444 if (precision <= 0)
19445 precision = -lim;
19446 else
19447 precision = min (precision, -lim);
19448 }
19449 else if (lim > 0)
19450 {
19451 /* Padding specified. Don't let it be more than
19452 current maximum. */
19453 if (precision > 0)
19454 lim = min (precision, lim);
19455
19456 /* If that's more padding than already wanted, queue it.
19457 But don't reduce padding already specified even if
19458 that is beyond the current truncation point. */
19459 field_width = max (lim, field_width);
19460 }
19461 goto tail_recurse;
19462 }
19463 else if (STRINGP (car) || CONSP (car))
19464 {
19465 Lisp_Object halftail = elt;
19466 int len = 0;
19467
19468 while (CONSP (elt)
19469 && (precision <= 0 || n < precision))
19470 {
19471 n += display_mode_element (it, depth,
19472 /* Do padding only after the last
19473 element in the list. */
19474 (! CONSP (XCDR (elt))
19475 ? field_width - n
19476 : 0),
19477 precision - n, XCAR (elt),
19478 props, risky);
19479 elt = XCDR (elt);
19480 len++;
19481 if ((len & 1) == 0)
19482 halftail = XCDR (halftail);
19483 /* Check for cycle. */
19484 if (EQ (halftail, elt))
19485 break;
19486 }
19487 }
19488 }
19489 break;
19490
19491 default:
19492 invalid:
19493 elt = build_string ("*invalid*");
19494 goto tail_recurse;
19495 }
19496
19497 /* Pad to FIELD_WIDTH. */
19498 if (field_width > 0 && n < field_width)
19499 {
19500 switch (mode_line_target)
19501 {
19502 case MODE_LINE_NOPROP:
19503 case MODE_LINE_TITLE:
19504 n += store_mode_line_noprop ("", field_width - n, 0);
19505 break;
19506 case MODE_LINE_STRING:
19507 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19508 break;
19509 case MODE_LINE_DISPLAY:
19510 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19511 0, 0, 0);
19512 break;
19513 }
19514 }
19515
19516 return n;
19517 }
19518
19519 /* Store a mode-line string element in mode_line_string_list.
19520
19521 If STRING is non-null, display that C string. Otherwise, the Lisp
19522 string LISP_STRING is displayed.
19523
19524 FIELD_WIDTH is the minimum number of output glyphs to produce.
19525 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19526 with spaces. FIELD_WIDTH <= 0 means don't pad.
19527
19528 PRECISION is the maximum number of characters to output from
19529 STRING. PRECISION <= 0 means don't truncate the string.
19530
19531 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19532 properties to the string.
19533
19534 PROPS are the properties to add to the string.
19535 The mode_line_string_face face property is always added to the string.
19536 */
19537
19538 static int
19539 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19540 int field_width, int precision, Lisp_Object props)
19541 {
19542 EMACS_INT len;
19543 int n = 0;
19544
19545 if (string != NULL)
19546 {
19547 len = strlen (string);
19548 if (precision > 0 && len > precision)
19549 len = precision;
19550 lisp_string = make_string (string, len);
19551 if (NILP (props))
19552 props = mode_line_string_face_prop;
19553 else if (!NILP (mode_line_string_face))
19554 {
19555 Lisp_Object face = Fplist_get (props, Qface);
19556 props = Fcopy_sequence (props);
19557 if (NILP (face))
19558 face = mode_line_string_face;
19559 else
19560 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19561 props = Fplist_put (props, Qface, face);
19562 }
19563 Fadd_text_properties (make_number (0), make_number (len),
19564 props, lisp_string);
19565 }
19566 else
19567 {
19568 len = XFASTINT (Flength (lisp_string));
19569 if (precision > 0 && len > precision)
19570 {
19571 len = precision;
19572 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19573 precision = -1;
19574 }
19575 if (!NILP (mode_line_string_face))
19576 {
19577 Lisp_Object face;
19578 if (NILP (props))
19579 props = Ftext_properties_at (make_number (0), lisp_string);
19580 face = Fplist_get (props, Qface);
19581 if (NILP (face))
19582 face = mode_line_string_face;
19583 else
19584 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19585 props = Fcons (Qface, Fcons (face, Qnil));
19586 if (copy_string)
19587 lisp_string = Fcopy_sequence (lisp_string);
19588 }
19589 if (!NILP (props))
19590 Fadd_text_properties (make_number (0), make_number (len),
19591 props, lisp_string);
19592 }
19593
19594 if (len > 0)
19595 {
19596 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19597 n += len;
19598 }
19599
19600 if (field_width > len)
19601 {
19602 field_width -= len;
19603 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19604 if (!NILP (props))
19605 Fadd_text_properties (make_number (0), make_number (field_width),
19606 props, lisp_string);
19607 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19608 n += field_width;
19609 }
19610
19611 return n;
19612 }
19613
19614
19615 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19616 1, 4, 0,
19617 doc: /* Format a string out of a mode line format specification.
19618 First arg FORMAT specifies the mode line format (see `mode-line-format'
19619 for details) to use.
19620
19621 By default, the format is evaluated for the currently selected window.
19622
19623 Optional second arg FACE specifies the face property to put on all
19624 characters for which no face is specified. The value nil means the
19625 default face. The value t means whatever face the window's mode line
19626 currently uses (either `mode-line' or `mode-line-inactive',
19627 depending on whether the window is the selected window or not).
19628 An integer value means the value string has no text
19629 properties.
19630
19631 Optional third and fourth args WINDOW and BUFFER specify the window
19632 and buffer to use as the context for the formatting (defaults
19633 are the selected window and the WINDOW's buffer). */)
19634 (Lisp_Object format, Lisp_Object face,
19635 Lisp_Object window, Lisp_Object buffer)
19636 {
19637 struct it it;
19638 int len;
19639 struct window *w;
19640 struct buffer *old_buffer = NULL;
19641 int face_id;
19642 int no_props = INTEGERP (face);
19643 int count = SPECPDL_INDEX ();
19644 Lisp_Object str;
19645 int string_start = 0;
19646
19647 if (NILP (window))
19648 window = selected_window;
19649 CHECK_WINDOW (window);
19650 w = XWINDOW (window);
19651
19652 if (NILP (buffer))
19653 buffer = w->buffer;
19654 CHECK_BUFFER (buffer);
19655
19656 /* Make formatting the modeline a non-op when noninteractive, otherwise
19657 there will be problems later caused by a partially initialized frame. */
19658 if (NILP (format) || noninteractive)
19659 return empty_unibyte_string;
19660
19661 if (no_props)
19662 face = Qnil;
19663
19664 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19665 : EQ (face, Qt) ? (EQ (window, selected_window)
19666 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19667 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19668 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19669 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19670 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19671 : DEFAULT_FACE_ID;
19672
19673 if (XBUFFER (buffer) != current_buffer)
19674 old_buffer = current_buffer;
19675
19676 /* Save things including mode_line_proptrans_alist,
19677 and set that to nil so that we don't alter the outer value. */
19678 record_unwind_protect (unwind_format_mode_line,
19679 format_mode_line_unwind_data
19680 (old_buffer, selected_window, 1));
19681 mode_line_proptrans_alist = Qnil;
19682
19683 Fselect_window (window, Qt);
19684 if (old_buffer)
19685 set_buffer_internal_1 (XBUFFER (buffer));
19686
19687 init_iterator (&it, w, -1, -1, NULL, face_id);
19688
19689 if (no_props)
19690 {
19691 mode_line_target = MODE_LINE_NOPROP;
19692 mode_line_string_face_prop = Qnil;
19693 mode_line_string_list = Qnil;
19694 string_start = MODE_LINE_NOPROP_LEN (0);
19695 }
19696 else
19697 {
19698 mode_line_target = MODE_LINE_STRING;
19699 mode_line_string_list = Qnil;
19700 mode_line_string_face = face;
19701 mode_line_string_face_prop
19702 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19703 }
19704
19705 push_kboard (FRAME_KBOARD (it.f));
19706 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19707 pop_kboard ();
19708
19709 if (no_props)
19710 {
19711 len = MODE_LINE_NOPROP_LEN (string_start);
19712 str = make_string (mode_line_noprop_buf + string_start, len);
19713 }
19714 else
19715 {
19716 mode_line_string_list = Fnreverse (mode_line_string_list);
19717 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19718 empty_unibyte_string);
19719 }
19720
19721 unbind_to (count, Qnil);
19722 return str;
19723 }
19724
19725 /* Write a null-terminated, right justified decimal representation of
19726 the positive integer D to BUF using a minimal field width WIDTH. */
19727
19728 static void
19729 pint2str (register char *buf, register int width, register EMACS_INT d)
19730 {
19731 register char *p = buf;
19732
19733 if (d <= 0)
19734 *p++ = '0';
19735 else
19736 {
19737 while (d > 0)
19738 {
19739 *p++ = d % 10 + '0';
19740 d /= 10;
19741 }
19742 }
19743
19744 for (width -= (int) (p - buf); width > 0; --width)
19745 *p++ = ' ';
19746 *p-- = '\0';
19747 while (p > buf)
19748 {
19749 d = *buf;
19750 *buf++ = *p;
19751 *p-- = d;
19752 }
19753 }
19754
19755 /* Write a null-terminated, right justified decimal and "human
19756 readable" representation of the nonnegative integer D to BUF using
19757 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19758
19759 static const char power_letter[] =
19760 {
19761 0, /* no letter */
19762 'k', /* kilo */
19763 'M', /* mega */
19764 'G', /* giga */
19765 'T', /* tera */
19766 'P', /* peta */
19767 'E', /* exa */
19768 'Z', /* zetta */
19769 'Y' /* yotta */
19770 };
19771
19772 static void
19773 pint2hrstr (char *buf, int width, EMACS_INT d)
19774 {
19775 /* We aim to represent the nonnegative integer D as
19776 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19777 EMACS_INT quotient = d;
19778 int remainder = 0;
19779 /* -1 means: do not use TENTHS. */
19780 int tenths = -1;
19781 int exponent = 0;
19782
19783 /* Length of QUOTIENT.TENTHS as a string. */
19784 int length;
19785
19786 char * psuffix;
19787 char * p;
19788
19789 if (1000 <= quotient)
19790 {
19791 /* Scale to the appropriate EXPONENT. */
19792 do
19793 {
19794 remainder = quotient % 1000;
19795 quotient /= 1000;
19796 exponent++;
19797 }
19798 while (1000 <= quotient);
19799
19800 /* Round to nearest and decide whether to use TENTHS or not. */
19801 if (quotient <= 9)
19802 {
19803 tenths = remainder / 100;
19804 if (50 <= remainder % 100)
19805 {
19806 if (tenths < 9)
19807 tenths++;
19808 else
19809 {
19810 quotient++;
19811 if (quotient == 10)
19812 tenths = -1;
19813 else
19814 tenths = 0;
19815 }
19816 }
19817 }
19818 else
19819 if (500 <= remainder)
19820 {
19821 if (quotient < 999)
19822 quotient++;
19823 else
19824 {
19825 quotient = 1;
19826 exponent++;
19827 tenths = 0;
19828 }
19829 }
19830 }
19831
19832 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19833 if (tenths == -1 && quotient <= 99)
19834 if (quotient <= 9)
19835 length = 1;
19836 else
19837 length = 2;
19838 else
19839 length = 3;
19840 p = psuffix = buf + max (width, length);
19841
19842 /* Print EXPONENT. */
19843 *psuffix++ = power_letter[exponent];
19844 *psuffix = '\0';
19845
19846 /* Print TENTHS. */
19847 if (tenths >= 0)
19848 {
19849 *--p = '0' + tenths;
19850 *--p = '.';
19851 }
19852
19853 /* Print QUOTIENT. */
19854 do
19855 {
19856 int digit = quotient % 10;
19857 *--p = '0' + digit;
19858 }
19859 while ((quotient /= 10) != 0);
19860
19861 /* Print leading spaces. */
19862 while (buf < p)
19863 *--p = ' ';
19864 }
19865
19866 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19867 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19868 type of CODING_SYSTEM. Return updated pointer into BUF. */
19869
19870 static unsigned char invalid_eol_type[] = "(*invalid*)";
19871
19872 static char *
19873 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19874 {
19875 Lisp_Object val;
19876 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19877 const unsigned char *eol_str;
19878 int eol_str_len;
19879 /* The EOL conversion we are using. */
19880 Lisp_Object eoltype;
19881
19882 val = CODING_SYSTEM_SPEC (coding_system);
19883 eoltype = Qnil;
19884
19885 if (!VECTORP (val)) /* Not yet decided. */
19886 {
19887 if (multibyte)
19888 *buf++ = '-';
19889 if (eol_flag)
19890 eoltype = eol_mnemonic_undecided;
19891 /* Don't mention EOL conversion if it isn't decided. */
19892 }
19893 else
19894 {
19895 Lisp_Object attrs;
19896 Lisp_Object eolvalue;
19897
19898 attrs = AREF (val, 0);
19899 eolvalue = AREF (val, 2);
19900
19901 if (multibyte)
19902 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
19903
19904 if (eol_flag)
19905 {
19906 /* The EOL conversion that is normal on this system. */
19907
19908 if (NILP (eolvalue)) /* Not yet decided. */
19909 eoltype = eol_mnemonic_undecided;
19910 else if (VECTORP (eolvalue)) /* Not yet decided. */
19911 eoltype = eol_mnemonic_undecided;
19912 else /* eolvalue is Qunix, Qdos, or Qmac. */
19913 eoltype = (EQ (eolvalue, Qunix)
19914 ? eol_mnemonic_unix
19915 : (EQ (eolvalue, Qdos) == 1
19916 ? eol_mnemonic_dos : eol_mnemonic_mac));
19917 }
19918 }
19919
19920 if (eol_flag)
19921 {
19922 /* Mention the EOL conversion if it is not the usual one. */
19923 if (STRINGP (eoltype))
19924 {
19925 eol_str = SDATA (eoltype);
19926 eol_str_len = SBYTES (eoltype);
19927 }
19928 else if (CHARACTERP (eoltype))
19929 {
19930 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
19931 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
19932 eol_str = tmp;
19933 }
19934 else
19935 {
19936 eol_str = invalid_eol_type;
19937 eol_str_len = sizeof (invalid_eol_type) - 1;
19938 }
19939 memcpy (buf, eol_str, eol_str_len);
19940 buf += eol_str_len;
19941 }
19942
19943 return buf;
19944 }
19945
19946 /* Return a string for the output of a mode line %-spec for window W,
19947 generated by character C. FIELD_WIDTH > 0 means pad the string
19948 returned with spaces to that value. Return a Lisp string in
19949 *STRING if the resulting string is taken from that Lisp string.
19950
19951 Note we operate on the current buffer for most purposes,
19952 the exception being w->base_line_pos. */
19953
19954 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
19955
19956 static const char *
19957 decode_mode_spec (struct window *w, register int c, int field_width,
19958 Lisp_Object *string)
19959 {
19960 Lisp_Object obj;
19961 struct frame *f = XFRAME (WINDOW_FRAME (w));
19962 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
19963 struct buffer *b = current_buffer;
19964
19965 obj = Qnil;
19966 *string = Qnil;
19967
19968 switch (c)
19969 {
19970 case '*':
19971 if (!NILP (BVAR (b, read_only)))
19972 return "%";
19973 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19974 return "*";
19975 return "-";
19976
19977 case '+':
19978 /* This differs from %* only for a modified read-only buffer. */
19979 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19980 return "*";
19981 if (!NILP (BVAR (b, read_only)))
19982 return "%";
19983 return "-";
19984
19985 case '&':
19986 /* This differs from %* in ignoring read-only-ness. */
19987 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
19988 return "*";
19989 return "-";
19990
19991 case '%':
19992 return "%";
19993
19994 case '[':
19995 {
19996 int i;
19997 char *p;
19998
19999 if (command_loop_level > 5)
20000 return "[[[... ";
20001 p = decode_mode_spec_buf;
20002 for (i = 0; i < command_loop_level; i++)
20003 *p++ = '[';
20004 *p = 0;
20005 return decode_mode_spec_buf;
20006 }
20007
20008 case ']':
20009 {
20010 int i;
20011 char *p;
20012
20013 if (command_loop_level > 5)
20014 return " ...]]]";
20015 p = decode_mode_spec_buf;
20016 for (i = 0; i < command_loop_level; i++)
20017 *p++ = ']';
20018 *p = 0;
20019 return decode_mode_spec_buf;
20020 }
20021
20022 case '-':
20023 {
20024 register int i;
20025
20026 /* Let lots_of_dashes be a string of infinite length. */
20027 if (mode_line_target == MODE_LINE_NOPROP ||
20028 mode_line_target == MODE_LINE_STRING)
20029 return "--";
20030 if (field_width <= 0
20031 || field_width > sizeof (lots_of_dashes))
20032 {
20033 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20034 decode_mode_spec_buf[i] = '-';
20035 decode_mode_spec_buf[i] = '\0';
20036 return decode_mode_spec_buf;
20037 }
20038 else
20039 return lots_of_dashes;
20040 }
20041
20042 case 'b':
20043 obj = BVAR (b, name);
20044 break;
20045
20046 case 'c':
20047 /* %c and %l are ignored in `frame-title-format'.
20048 (In redisplay_internal, the frame title is drawn _before_ the
20049 windows are updated, so the stuff which depends on actual
20050 window contents (such as %l) may fail to render properly, or
20051 even crash emacs.) */
20052 if (mode_line_target == MODE_LINE_TITLE)
20053 return "";
20054 else
20055 {
20056 EMACS_INT col = current_column ();
20057 w->column_number_displayed = make_number (col);
20058 pint2str (decode_mode_spec_buf, field_width, col);
20059 return decode_mode_spec_buf;
20060 }
20061
20062 case 'e':
20063 #ifndef SYSTEM_MALLOC
20064 {
20065 if (NILP (Vmemory_full))
20066 return "";
20067 else
20068 return "!MEM FULL! ";
20069 }
20070 #else
20071 return "";
20072 #endif
20073
20074 case 'F':
20075 /* %F displays the frame name. */
20076 if (!NILP (f->title))
20077 return SSDATA (f->title);
20078 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20079 return SSDATA (f->name);
20080 return "Emacs";
20081
20082 case 'f':
20083 obj = BVAR (b, filename);
20084 break;
20085
20086 case 'i':
20087 {
20088 EMACS_INT size = ZV - BEGV;
20089 pint2str (decode_mode_spec_buf, field_width, size);
20090 return decode_mode_spec_buf;
20091 }
20092
20093 case 'I':
20094 {
20095 EMACS_INT size = ZV - BEGV;
20096 pint2hrstr (decode_mode_spec_buf, field_width, size);
20097 return decode_mode_spec_buf;
20098 }
20099
20100 case 'l':
20101 {
20102 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20103 EMACS_INT topline, nlines, height;
20104 EMACS_INT junk;
20105
20106 /* %c and %l are ignored in `frame-title-format'. */
20107 if (mode_line_target == MODE_LINE_TITLE)
20108 return "";
20109
20110 startpos = XMARKER (w->start)->charpos;
20111 startpos_byte = marker_byte_position (w->start);
20112 height = WINDOW_TOTAL_LINES (w);
20113
20114 /* If we decided that this buffer isn't suitable for line numbers,
20115 don't forget that too fast. */
20116 if (EQ (w->base_line_pos, w->buffer))
20117 goto no_value;
20118 /* But do forget it, if the window shows a different buffer now. */
20119 else if (BUFFERP (w->base_line_pos))
20120 w->base_line_pos = Qnil;
20121
20122 /* If the buffer is very big, don't waste time. */
20123 if (INTEGERP (Vline_number_display_limit)
20124 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20125 {
20126 w->base_line_pos = Qnil;
20127 w->base_line_number = Qnil;
20128 goto no_value;
20129 }
20130
20131 if (INTEGERP (w->base_line_number)
20132 && INTEGERP (w->base_line_pos)
20133 && XFASTINT (w->base_line_pos) <= startpos)
20134 {
20135 line = XFASTINT (w->base_line_number);
20136 linepos = XFASTINT (w->base_line_pos);
20137 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20138 }
20139 else
20140 {
20141 line = 1;
20142 linepos = BUF_BEGV (b);
20143 linepos_byte = BUF_BEGV_BYTE (b);
20144 }
20145
20146 /* Count lines from base line to window start position. */
20147 nlines = display_count_lines (linepos_byte,
20148 startpos_byte,
20149 startpos, &junk);
20150
20151 topline = nlines + line;
20152
20153 /* Determine a new base line, if the old one is too close
20154 or too far away, or if we did not have one.
20155 "Too close" means it's plausible a scroll-down would
20156 go back past it. */
20157 if (startpos == BUF_BEGV (b))
20158 {
20159 w->base_line_number = make_number (topline);
20160 w->base_line_pos = make_number (BUF_BEGV (b));
20161 }
20162 else if (nlines < height + 25 || nlines > height * 3 + 50
20163 || linepos == BUF_BEGV (b))
20164 {
20165 EMACS_INT limit = BUF_BEGV (b);
20166 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20167 EMACS_INT position;
20168 EMACS_INT distance =
20169 (height * 2 + 30) * line_number_display_limit_width;
20170
20171 if (startpos - distance > limit)
20172 {
20173 limit = startpos - distance;
20174 limit_byte = CHAR_TO_BYTE (limit);
20175 }
20176
20177 nlines = display_count_lines (startpos_byte,
20178 limit_byte,
20179 - (height * 2 + 30),
20180 &position);
20181 /* If we couldn't find the lines we wanted within
20182 line_number_display_limit_width chars per line,
20183 give up on line numbers for this window. */
20184 if (position == limit_byte && limit == startpos - distance)
20185 {
20186 w->base_line_pos = w->buffer;
20187 w->base_line_number = Qnil;
20188 goto no_value;
20189 }
20190
20191 w->base_line_number = make_number (topline - nlines);
20192 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20193 }
20194
20195 /* Now count lines from the start pos to point. */
20196 nlines = display_count_lines (startpos_byte,
20197 PT_BYTE, PT, &junk);
20198
20199 /* Record that we did display the line number. */
20200 line_number_displayed = 1;
20201
20202 /* Make the string to show. */
20203 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20204 return decode_mode_spec_buf;
20205 no_value:
20206 {
20207 char* p = decode_mode_spec_buf;
20208 int pad = field_width - 2;
20209 while (pad-- > 0)
20210 *p++ = ' ';
20211 *p++ = '?';
20212 *p++ = '?';
20213 *p = '\0';
20214 return decode_mode_spec_buf;
20215 }
20216 }
20217 break;
20218
20219 case 'm':
20220 obj = BVAR (b, mode_name);
20221 break;
20222
20223 case 'n':
20224 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20225 return " Narrow";
20226 break;
20227
20228 case 'p':
20229 {
20230 EMACS_INT pos = marker_position (w->start);
20231 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20232
20233 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20234 {
20235 if (pos <= BUF_BEGV (b))
20236 return "All";
20237 else
20238 return "Bottom";
20239 }
20240 else if (pos <= BUF_BEGV (b))
20241 return "Top";
20242 else
20243 {
20244 if (total > 1000000)
20245 /* Do it differently for a large value, to avoid overflow. */
20246 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20247 else
20248 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20249 /* We can't normally display a 3-digit number,
20250 so get us a 2-digit number that is close. */
20251 if (total == 100)
20252 total = 99;
20253 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20254 return decode_mode_spec_buf;
20255 }
20256 }
20257
20258 /* Display percentage of size above the bottom of the screen. */
20259 case 'P':
20260 {
20261 EMACS_INT toppos = marker_position (w->start);
20262 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20263 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20264
20265 if (botpos >= BUF_ZV (b))
20266 {
20267 if (toppos <= BUF_BEGV (b))
20268 return "All";
20269 else
20270 return "Bottom";
20271 }
20272 else
20273 {
20274 if (total > 1000000)
20275 /* Do it differently for a large value, to avoid overflow. */
20276 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20277 else
20278 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20279 /* We can't normally display a 3-digit number,
20280 so get us a 2-digit number that is close. */
20281 if (total == 100)
20282 total = 99;
20283 if (toppos <= BUF_BEGV (b))
20284 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20285 else
20286 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20287 return decode_mode_spec_buf;
20288 }
20289 }
20290
20291 case 's':
20292 /* status of process */
20293 obj = Fget_buffer_process (Fcurrent_buffer ());
20294 if (NILP (obj))
20295 return "no process";
20296 #ifndef MSDOS
20297 obj = Fsymbol_name (Fprocess_status (obj));
20298 #endif
20299 break;
20300
20301 case '@':
20302 {
20303 int count = inhibit_garbage_collection ();
20304 Lisp_Object val = call1 (intern ("file-remote-p"),
20305 BVAR (current_buffer, directory));
20306 unbind_to (count, Qnil);
20307
20308 if (NILP (val))
20309 return "-";
20310 else
20311 return "@";
20312 }
20313
20314 case 't': /* indicate TEXT or BINARY */
20315 return "T";
20316
20317 case 'z':
20318 /* coding-system (not including end-of-line format) */
20319 case 'Z':
20320 /* coding-system (including end-of-line type) */
20321 {
20322 int eol_flag = (c == 'Z');
20323 char *p = decode_mode_spec_buf;
20324
20325 if (! FRAME_WINDOW_P (f))
20326 {
20327 /* No need to mention EOL here--the terminal never needs
20328 to do EOL conversion. */
20329 p = decode_mode_spec_coding (CODING_ID_NAME
20330 (FRAME_KEYBOARD_CODING (f)->id),
20331 p, 0);
20332 p = decode_mode_spec_coding (CODING_ID_NAME
20333 (FRAME_TERMINAL_CODING (f)->id),
20334 p, 0);
20335 }
20336 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20337 p, eol_flag);
20338
20339 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20340 #ifdef subprocesses
20341 obj = Fget_buffer_process (Fcurrent_buffer ());
20342 if (PROCESSP (obj))
20343 {
20344 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20345 p, eol_flag);
20346 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20347 p, eol_flag);
20348 }
20349 #endif /* subprocesses */
20350 #endif /* 0 */
20351 *p = 0;
20352 return decode_mode_spec_buf;
20353 }
20354 }
20355
20356 if (STRINGP (obj))
20357 {
20358 *string = obj;
20359 return SSDATA (obj);
20360 }
20361 else
20362 return "";
20363 }
20364
20365
20366 /* Count up to COUNT lines starting from START_BYTE.
20367 But don't go beyond LIMIT_BYTE.
20368 Return the number of lines thus found (always nonnegative).
20369
20370 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20371
20372 static EMACS_INT
20373 display_count_lines (EMACS_INT start_byte,
20374 EMACS_INT limit_byte, EMACS_INT count,
20375 EMACS_INT *byte_pos_ptr)
20376 {
20377 register unsigned char *cursor;
20378 unsigned char *base;
20379
20380 register EMACS_INT ceiling;
20381 register unsigned char *ceiling_addr;
20382 EMACS_INT orig_count = count;
20383
20384 /* If we are not in selective display mode,
20385 check only for newlines. */
20386 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20387 && !INTEGERP (BVAR (current_buffer, selective_display)));
20388
20389 if (count > 0)
20390 {
20391 while (start_byte < limit_byte)
20392 {
20393 ceiling = BUFFER_CEILING_OF (start_byte);
20394 ceiling = min (limit_byte - 1, ceiling);
20395 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20396 base = (cursor = BYTE_POS_ADDR (start_byte));
20397 while (1)
20398 {
20399 if (selective_display)
20400 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20401 ;
20402 else
20403 while (*cursor != '\n' && ++cursor != ceiling_addr)
20404 ;
20405
20406 if (cursor != ceiling_addr)
20407 {
20408 if (--count == 0)
20409 {
20410 start_byte += cursor - base + 1;
20411 *byte_pos_ptr = start_byte;
20412 return orig_count;
20413 }
20414 else
20415 if (++cursor == ceiling_addr)
20416 break;
20417 }
20418 else
20419 break;
20420 }
20421 start_byte += cursor - base;
20422 }
20423 }
20424 else
20425 {
20426 while (start_byte > limit_byte)
20427 {
20428 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20429 ceiling = max (limit_byte, ceiling);
20430 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20431 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20432 while (1)
20433 {
20434 if (selective_display)
20435 while (--cursor != ceiling_addr
20436 && *cursor != '\n' && *cursor != 015)
20437 ;
20438 else
20439 while (--cursor != ceiling_addr && *cursor != '\n')
20440 ;
20441
20442 if (cursor != ceiling_addr)
20443 {
20444 if (++count == 0)
20445 {
20446 start_byte += cursor - base + 1;
20447 *byte_pos_ptr = start_byte;
20448 /* When scanning backwards, we should
20449 not count the newline posterior to which we stop. */
20450 return - orig_count - 1;
20451 }
20452 }
20453 else
20454 break;
20455 }
20456 /* Here we add 1 to compensate for the last decrement
20457 of CURSOR, which took it past the valid range. */
20458 start_byte += cursor - base + 1;
20459 }
20460 }
20461
20462 *byte_pos_ptr = limit_byte;
20463
20464 if (count < 0)
20465 return - orig_count + count;
20466 return orig_count - count;
20467
20468 }
20469
20470
20471 \f
20472 /***********************************************************************
20473 Displaying strings
20474 ***********************************************************************/
20475
20476 /* Display a NUL-terminated string, starting with index START.
20477
20478 If STRING is non-null, display that C string. Otherwise, the Lisp
20479 string LISP_STRING is displayed. There's a case that STRING is
20480 non-null and LISP_STRING is not nil. It means STRING is a string
20481 data of LISP_STRING. In that case, we display LISP_STRING while
20482 ignoring its text properties.
20483
20484 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20485 FACE_STRING. Display STRING or LISP_STRING with the face at
20486 FACE_STRING_POS in FACE_STRING:
20487
20488 Display the string in the environment given by IT, but use the
20489 standard display table, temporarily.
20490
20491 FIELD_WIDTH is the minimum number of output glyphs to produce.
20492 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20493 with spaces. If STRING has more characters, more than FIELD_WIDTH
20494 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20495
20496 PRECISION is the maximum number of characters to output from
20497 STRING. PRECISION < 0 means don't truncate the string.
20498
20499 This is roughly equivalent to printf format specifiers:
20500
20501 FIELD_WIDTH PRECISION PRINTF
20502 ----------------------------------------
20503 -1 -1 %s
20504 -1 10 %.10s
20505 10 -1 %10s
20506 20 10 %20.10s
20507
20508 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20509 display them, and < 0 means obey the current buffer's value of
20510 enable_multibyte_characters.
20511
20512 Value is the number of columns displayed. */
20513
20514 static int
20515 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20516 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20517 int field_width, int precision, int max_x, int multibyte)
20518 {
20519 int hpos_at_start = it->hpos;
20520 int saved_face_id = it->face_id;
20521 struct glyph_row *row = it->glyph_row;
20522 EMACS_INT it_charpos;
20523
20524 /* Initialize the iterator IT for iteration over STRING beginning
20525 with index START. */
20526 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20527 precision, field_width, multibyte);
20528 if (string && STRINGP (lisp_string))
20529 /* LISP_STRING is the one returned by decode_mode_spec. We should
20530 ignore its text properties. */
20531 it->stop_charpos = it->end_charpos;
20532
20533 /* If displaying STRING, set up the face of the iterator from
20534 FACE_STRING, if that's given. */
20535 if (STRINGP (face_string))
20536 {
20537 EMACS_INT endptr;
20538 struct face *face;
20539
20540 it->face_id
20541 = face_at_string_position (it->w, face_string, face_string_pos,
20542 0, it->region_beg_charpos,
20543 it->region_end_charpos,
20544 &endptr, it->base_face_id, 0);
20545 face = FACE_FROM_ID (it->f, it->face_id);
20546 it->face_box_p = face->box != FACE_NO_BOX;
20547 }
20548
20549 /* Set max_x to the maximum allowed X position. Don't let it go
20550 beyond the right edge of the window. */
20551 if (max_x <= 0)
20552 max_x = it->last_visible_x;
20553 else
20554 max_x = min (max_x, it->last_visible_x);
20555
20556 /* Skip over display elements that are not visible. because IT->w is
20557 hscrolled. */
20558 if (it->current_x < it->first_visible_x)
20559 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20560 MOVE_TO_POS | MOVE_TO_X);
20561
20562 row->ascent = it->max_ascent;
20563 row->height = it->max_ascent + it->max_descent;
20564 row->phys_ascent = it->max_phys_ascent;
20565 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20566 row->extra_line_spacing = it->max_extra_line_spacing;
20567
20568 if (STRINGP (it->string))
20569 it_charpos = IT_STRING_CHARPOS (*it);
20570 else
20571 it_charpos = IT_CHARPOS (*it);
20572
20573 /* This condition is for the case that we are called with current_x
20574 past last_visible_x. */
20575 while (it->current_x < max_x)
20576 {
20577 int x_before, x, n_glyphs_before, i, nglyphs;
20578
20579 /* Get the next display element. */
20580 if (!get_next_display_element (it))
20581 break;
20582
20583 /* Produce glyphs. */
20584 x_before = it->current_x;
20585 n_glyphs_before = row->used[TEXT_AREA];
20586 PRODUCE_GLYPHS (it);
20587
20588 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20589 i = 0;
20590 x = x_before;
20591 while (i < nglyphs)
20592 {
20593 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20594
20595 if (it->line_wrap != TRUNCATE
20596 && x + glyph->pixel_width > max_x)
20597 {
20598 /* End of continued line or max_x reached. */
20599 if (CHAR_GLYPH_PADDING_P (*glyph))
20600 {
20601 /* A wide character is unbreakable. */
20602 if (row->reversed_p)
20603 unproduce_glyphs (it, row->used[TEXT_AREA]
20604 - n_glyphs_before);
20605 row->used[TEXT_AREA] = n_glyphs_before;
20606 it->current_x = x_before;
20607 }
20608 else
20609 {
20610 if (row->reversed_p)
20611 unproduce_glyphs (it, row->used[TEXT_AREA]
20612 - (n_glyphs_before + i));
20613 row->used[TEXT_AREA] = n_glyphs_before + i;
20614 it->current_x = x;
20615 }
20616 break;
20617 }
20618 else if (x + glyph->pixel_width >= it->first_visible_x)
20619 {
20620 /* Glyph is at least partially visible. */
20621 ++it->hpos;
20622 if (x < it->first_visible_x)
20623 row->x = x - it->first_visible_x;
20624 }
20625 else
20626 {
20627 /* Glyph is off the left margin of the display area.
20628 Should not happen. */
20629 abort ();
20630 }
20631
20632 row->ascent = max (row->ascent, it->max_ascent);
20633 row->height = max (row->height, it->max_ascent + it->max_descent);
20634 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20635 row->phys_height = max (row->phys_height,
20636 it->max_phys_ascent + it->max_phys_descent);
20637 row->extra_line_spacing = max (row->extra_line_spacing,
20638 it->max_extra_line_spacing);
20639 x += glyph->pixel_width;
20640 ++i;
20641 }
20642
20643 /* Stop if max_x reached. */
20644 if (i < nglyphs)
20645 break;
20646
20647 /* Stop at line ends. */
20648 if (ITERATOR_AT_END_OF_LINE_P (it))
20649 {
20650 it->continuation_lines_width = 0;
20651 break;
20652 }
20653
20654 set_iterator_to_next (it, 1);
20655 if (STRINGP (it->string))
20656 it_charpos = IT_STRING_CHARPOS (*it);
20657 else
20658 it_charpos = IT_CHARPOS (*it);
20659
20660 /* Stop if truncating at the right edge. */
20661 if (it->line_wrap == TRUNCATE
20662 && it->current_x >= it->last_visible_x)
20663 {
20664 /* Add truncation mark, but don't do it if the line is
20665 truncated at a padding space. */
20666 if (it_charpos < it->string_nchars)
20667 {
20668 if (!FRAME_WINDOW_P (it->f))
20669 {
20670 int ii, n;
20671
20672 if (it->current_x > it->last_visible_x)
20673 {
20674 if (!row->reversed_p)
20675 {
20676 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20677 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20678 break;
20679 }
20680 else
20681 {
20682 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
20683 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20684 break;
20685 unproduce_glyphs (it, ii + 1);
20686 ii = row->used[TEXT_AREA] - (ii + 1);
20687 }
20688 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20689 {
20690 row->used[TEXT_AREA] = ii;
20691 produce_special_glyphs (it, IT_TRUNCATION);
20692 }
20693 }
20694 produce_special_glyphs (it, IT_TRUNCATION);
20695 }
20696 row->truncated_on_right_p = 1;
20697 }
20698 break;
20699 }
20700 }
20701
20702 /* Maybe insert a truncation at the left. */
20703 if (it->first_visible_x
20704 && it_charpos > 0)
20705 {
20706 if (!FRAME_WINDOW_P (it->f))
20707 insert_left_trunc_glyphs (it);
20708 row->truncated_on_left_p = 1;
20709 }
20710
20711 it->face_id = saved_face_id;
20712
20713 /* Value is number of columns displayed. */
20714 return it->hpos - hpos_at_start;
20715 }
20716
20717
20718 \f
20719 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20720 appears as an element of LIST or as the car of an element of LIST.
20721 If PROPVAL is a list, compare each element against LIST in that
20722 way, and return 1/2 if any element of PROPVAL is found in LIST.
20723 Otherwise return 0. This function cannot quit.
20724 The return value is 2 if the text is invisible but with an ellipsis
20725 and 1 if it's invisible and without an ellipsis. */
20726
20727 int
20728 invisible_p (register Lisp_Object propval, Lisp_Object list)
20729 {
20730 register Lisp_Object tail, proptail;
20731
20732 for (tail = list; CONSP (tail); tail = XCDR (tail))
20733 {
20734 register Lisp_Object tem;
20735 tem = XCAR (tail);
20736 if (EQ (propval, tem))
20737 return 1;
20738 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20739 return NILP (XCDR (tem)) ? 1 : 2;
20740 }
20741
20742 if (CONSP (propval))
20743 {
20744 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20745 {
20746 Lisp_Object propelt;
20747 propelt = XCAR (proptail);
20748 for (tail = list; CONSP (tail); tail = XCDR (tail))
20749 {
20750 register Lisp_Object tem;
20751 tem = XCAR (tail);
20752 if (EQ (propelt, tem))
20753 return 1;
20754 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20755 return NILP (XCDR (tem)) ? 1 : 2;
20756 }
20757 }
20758 }
20759
20760 return 0;
20761 }
20762
20763 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20764 doc: /* Non-nil if the property makes the text invisible.
20765 POS-OR-PROP can be a marker or number, in which case it is taken to be
20766 a position in the current buffer and the value of the `invisible' property
20767 is checked; or it can be some other value, which is then presumed to be the
20768 value of the `invisible' property of the text of interest.
20769 The non-nil value returned can be t for truly invisible text or something
20770 else if the text is replaced by an ellipsis. */)
20771 (Lisp_Object pos_or_prop)
20772 {
20773 Lisp_Object prop
20774 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20775 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20776 : pos_or_prop);
20777 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20778 return (invis == 0 ? Qnil
20779 : invis == 1 ? Qt
20780 : make_number (invis));
20781 }
20782
20783 /* Calculate a width or height in pixels from a specification using
20784 the following elements:
20785
20786 SPEC ::=
20787 NUM - a (fractional) multiple of the default font width/height
20788 (NUM) - specifies exactly NUM pixels
20789 UNIT - a fixed number of pixels, see below.
20790 ELEMENT - size of a display element in pixels, see below.
20791 (NUM . SPEC) - equals NUM * SPEC
20792 (+ SPEC SPEC ...) - add pixel values
20793 (- SPEC SPEC ...) - subtract pixel values
20794 (- SPEC) - negate pixel value
20795
20796 NUM ::=
20797 INT or FLOAT - a number constant
20798 SYMBOL - use symbol's (buffer local) variable binding.
20799
20800 UNIT ::=
20801 in - pixels per inch *)
20802 mm - pixels per 1/1000 meter *)
20803 cm - pixels per 1/100 meter *)
20804 width - width of current font in pixels.
20805 height - height of current font in pixels.
20806
20807 *) using the ratio(s) defined in display-pixels-per-inch.
20808
20809 ELEMENT ::=
20810
20811 left-fringe - left fringe width in pixels
20812 right-fringe - right fringe width in pixels
20813
20814 left-margin - left margin width in pixels
20815 right-margin - right margin width in pixels
20816
20817 scroll-bar - scroll-bar area width in pixels
20818
20819 Examples:
20820
20821 Pixels corresponding to 5 inches:
20822 (5 . in)
20823
20824 Total width of non-text areas on left side of window (if scroll-bar is on left):
20825 '(space :width (+ left-fringe left-margin scroll-bar))
20826
20827 Align to first text column (in header line):
20828 '(space :align-to 0)
20829
20830 Align to middle of text area minus half the width of variable `my-image'
20831 containing a loaded image:
20832 '(space :align-to (0.5 . (- text my-image)))
20833
20834 Width of left margin minus width of 1 character in the default font:
20835 '(space :width (- left-margin 1))
20836
20837 Width of left margin minus width of 2 characters in the current font:
20838 '(space :width (- left-margin (2 . width)))
20839
20840 Center 1 character over left-margin (in header line):
20841 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20842
20843 Different ways to express width of left fringe plus left margin minus one pixel:
20844 '(space :width (- (+ left-fringe left-margin) (1)))
20845 '(space :width (+ left-fringe left-margin (- (1))))
20846 '(space :width (+ left-fringe left-margin (-1)))
20847
20848 */
20849
20850 #define NUMVAL(X) \
20851 ((INTEGERP (X) || FLOATP (X)) \
20852 ? XFLOATINT (X) \
20853 : - 1)
20854
20855 int
20856 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20857 struct font *font, int width_p, int *align_to)
20858 {
20859 double pixels;
20860
20861 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20862 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20863
20864 if (NILP (prop))
20865 return OK_PIXELS (0);
20866
20867 xassert (FRAME_LIVE_P (it->f));
20868
20869 if (SYMBOLP (prop))
20870 {
20871 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20872 {
20873 char *unit = SSDATA (SYMBOL_NAME (prop));
20874
20875 if (unit[0] == 'i' && unit[1] == 'n')
20876 pixels = 1.0;
20877 else if (unit[0] == 'm' && unit[1] == 'm')
20878 pixels = 25.4;
20879 else if (unit[0] == 'c' && unit[1] == 'm')
20880 pixels = 2.54;
20881 else
20882 pixels = 0;
20883 if (pixels > 0)
20884 {
20885 double ppi;
20886 #ifdef HAVE_WINDOW_SYSTEM
20887 if (FRAME_WINDOW_P (it->f)
20888 && (ppi = (width_p
20889 ? FRAME_X_DISPLAY_INFO (it->f)->resx
20890 : FRAME_X_DISPLAY_INFO (it->f)->resy),
20891 ppi > 0))
20892 return OK_PIXELS (ppi / pixels);
20893 #endif
20894
20895 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
20896 || (CONSP (Vdisplay_pixels_per_inch)
20897 && (ppi = (width_p
20898 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
20899 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
20900 ppi > 0)))
20901 return OK_PIXELS (ppi / pixels);
20902
20903 return 0;
20904 }
20905 }
20906
20907 #ifdef HAVE_WINDOW_SYSTEM
20908 if (EQ (prop, Qheight))
20909 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
20910 if (EQ (prop, Qwidth))
20911 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
20912 #else
20913 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
20914 return OK_PIXELS (1);
20915 #endif
20916
20917 if (EQ (prop, Qtext))
20918 return OK_PIXELS (width_p
20919 ? window_box_width (it->w, TEXT_AREA)
20920 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
20921
20922 if (align_to && *align_to < 0)
20923 {
20924 *res = 0;
20925 if (EQ (prop, Qleft))
20926 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
20927 if (EQ (prop, Qright))
20928 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
20929 if (EQ (prop, Qcenter))
20930 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
20931 + window_box_width (it->w, TEXT_AREA) / 2);
20932 if (EQ (prop, Qleft_fringe))
20933 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20934 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
20935 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
20936 if (EQ (prop, Qright_fringe))
20937 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20938 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20939 : window_box_right_offset (it->w, TEXT_AREA));
20940 if (EQ (prop, Qleft_margin))
20941 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
20942 if (EQ (prop, Qright_margin))
20943 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
20944 if (EQ (prop, Qscroll_bar))
20945 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
20946 ? 0
20947 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
20948 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
20949 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20950 : 0)));
20951 }
20952 else
20953 {
20954 if (EQ (prop, Qleft_fringe))
20955 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
20956 if (EQ (prop, Qright_fringe))
20957 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
20958 if (EQ (prop, Qleft_margin))
20959 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
20960 if (EQ (prop, Qright_margin))
20961 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
20962 if (EQ (prop, Qscroll_bar))
20963 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
20964 }
20965
20966 prop = Fbuffer_local_value (prop, it->w->buffer);
20967 }
20968
20969 if (INTEGERP (prop) || FLOATP (prop))
20970 {
20971 int base_unit = (width_p
20972 ? FRAME_COLUMN_WIDTH (it->f)
20973 : FRAME_LINE_HEIGHT (it->f));
20974 return OK_PIXELS (XFLOATINT (prop) * base_unit);
20975 }
20976
20977 if (CONSP (prop))
20978 {
20979 Lisp_Object car = XCAR (prop);
20980 Lisp_Object cdr = XCDR (prop);
20981
20982 if (SYMBOLP (car))
20983 {
20984 #ifdef HAVE_WINDOW_SYSTEM
20985 if (FRAME_WINDOW_P (it->f)
20986 && valid_image_p (prop))
20987 {
20988 int id = lookup_image (it->f, prop);
20989 struct image *img = IMAGE_FROM_ID (it->f, id);
20990
20991 return OK_PIXELS (width_p ? img->width : img->height);
20992 }
20993 #endif
20994 if (EQ (car, Qplus) || EQ (car, Qminus))
20995 {
20996 int first = 1;
20997 double px;
20998
20999 pixels = 0;
21000 while (CONSP (cdr))
21001 {
21002 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21003 font, width_p, align_to))
21004 return 0;
21005 if (first)
21006 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21007 else
21008 pixels += px;
21009 cdr = XCDR (cdr);
21010 }
21011 if (EQ (car, Qminus))
21012 pixels = -pixels;
21013 return OK_PIXELS (pixels);
21014 }
21015
21016 car = Fbuffer_local_value (car, it->w->buffer);
21017 }
21018
21019 if (INTEGERP (car) || FLOATP (car))
21020 {
21021 double fact;
21022 pixels = XFLOATINT (car);
21023 if (NILP (cdr))
21024 return OK_PIXELS (pixels);
21025 if (calc_pixel_width_or_height (&fact, it, cdr,
21026 font, width_p, align_to))
21027 return OK_PIXELS (pixels * fact);
21028 return 0;
21029 }
21030
21031 return 0;
21032 }
21033
21034 return 0;
21035 }
21036
21037 \f
21038 /***********************************************************************
21039 Glyph Display
21040 ***********************************************************************/
21041
21042 #ifdef HAVE_WINDOW_SYSTEM
21043
21044 #if GLYPH_DEBUG
21045
21046 void
21047 dump_glyph_string (s)
21048 struct glyph_string *s;
21049 {
21050 fprintf (stderr, "glyph string\n");
21051 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21052 s->x, s->y, s->width, s->height);
21053 fprintf (stderr, " ybase = %d\n", s->ybase);
21054 fprintf (stderr, " hl = %d\n", s->hl);
21055 fprintf (stderr, " left overhang = %d, right = %d\n",
21056 s->left_overhang, s->right_overhang);
21057 fprintf (stderr, " nchars = %d\n", s->nchars);
21058 fprintf (stderr, " extends to end of line = %d\n",
21059 s->extends_to_end_of_line_p);
21060 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21061 fprintf (stderr, " bg width = %d\n", s->background_width);
21062 }
21063
21064 #endif /* GLYPH_DEBUG */
21065
21066 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21067 of XChar2b structures for S; it can't be allocated in
21068 init_glyph_string because it must be allocated via `alloca'. W
21069 is the window on which S is drawn. ROW and AREA are the glyph row
21070 and area within the row from which S is constructed. START is the
21071 index of the first glyph structure covered by S. HL is a
21072 face-override for drawing S. */
21073
21074 #ifdef HAVE_NTGUI
21075 #define OPTIONAL_HDC(hdc) HDC hdc,
21076 #define DECLARE_HDC(hdc) HDC hdc;
21077 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21078 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21079 #endif
21080
21081 #ifndef OPTIONAL_HDC
21082 #define OPTIONAL_HDC(hdc)
21083 #define DECLARE_HDC(hdc)
21084 #define ALLOCATE_HDC(hdc, f)
21085 #define RELEASE_HDC(hdc, f)
21086 #endif
21087
21088 static void
21089 init_glyph_string (struct glyph_string *s,
21090 OPTIONAL_HDC (hdc)
21091 XChar2b *char2b, struct window *w, struct glyph_row *row,
21092 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21093 {
21094 memset (s, 0, sizeof *s);
21095 s->w = w;
21096 s->f = XFRAME (w->frame);
21097 #ifdef HAVE_NTGUI
21098 s->hdc = hdc;
21099 #endif
21100 s->display = FRAME_X_DISPLAY (s->f);
21101 s->window = FRAME_X_WINDOW (s->f);
21102 s->char2b = char2b;
21103 s->hl = hl;
21104 s->row = row;
21105 s->area = area;
21106 s->first_glyph = row->glyphs[area] + start;
21107 s->height = row->height;
21108 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21109 s->ybase = s->y + row->ascent;
21110 }
21111
21112
21113 /* Append the list of glyph strings with head H and tail T to the list
21114 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21115
21116 static INLINE void
21117 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21118 struct glyph_string *h, struct glyph_string *t)
21119 {
21120 if (h)
21121 {
21122 if (*head)
21123 (*tail)->next = h;
21124 else
21125 *head = h;
21126 h->prev = *tail;
21127 *tail = t;
21128 }
21129 }
21130
21131
21132 /* Prepend the list of glyph strings with head H and tail T to the
21133 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21134 result. */
21135
21136 static INLINE void
21137 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21138 struct glyph_string *h, struct glyph_string *t)
21139 {
21140 if (h)
21141 {
21142 if (*head)
21143 (*head)->prev = t;
21144 else
21145 *tail = t;
21146 t->next = *head;
21147 *head = h;
21148 }
21149 }
21150
21151
21152 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21153 Set *HEAD and *TAIL to the resulting list. */
21154
21155 static INLINE void
21156 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21157 struct glyph_string *s)
21158 {
21159 s->next = s->prev = NULL;
21160 append_glyph_string_lists (head, tail, s, s);
21161 }
21162
21163
21164 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21165 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21166 make sure that X resources for the face returned are allocated.
21167 Value is a pointer to a realized face that is ready for display if
21168 DISPLAY_P is non-zero. */
21169
21170 static INLINE struct face *
21171 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21172 XChar2b *char2b, int display_p)
21173 {
21174 struct face *face = FACE_FROM_ID (f, face_id);
21175
21176 if (face->font)
21177 {
21178 unsigned code = face->font->driver->encode_char (face->font, c);
21179
21180 if (code != FONT_INVALID_CODE)
21181 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21182 else
21183 STORE_XCHAR2B (char2b, 0, 0);
21184 }
21185
21186 /* Make sure X resources of the face are allocated. */
21187 #ifdef HAVE_X_WINDOWS
21188 if (display_p)
21189 #endif
21190 {
21191 xassert (face != NULL);
21192 PREPARE_FACE_FOR_DISPLAY (f, face);
21193 }
21194
21195 return face;
21196 }
21197
21198
21199 /* Get face and two-byte form of character glyph GLYPH on frame F.
21200 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21201 a pointer to a realized face that is ready for display. */
21202
21203 static INLINE struct face *
21204 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21205 XChar2b *char2b, int *two_byte_p)
21206 {
21207 struct face *face;
21208
21209 xassert (glyph->type == CHAR_GLYPH);
21210 face = FACE_FROM_ID (f, glyph->face_id);
21211
21212 if (two_byte_p)
21213 *two_byte_p = 0;
21214
21215 if (face->font)
21216 {
21217 unsigned code;
21218
21219 if (CHAR_BYTE8_P (glyph->u.ch))
21220 code = CHAR_TO_BYTE8 (glyph->u.ch);
21221 else
21222 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21223
21224 if (code != FONT_INVALID_CODE)
21225 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21226 else
21227 STORE_XCHAR2B (char2b, 0, 0);
21228 }
21229
21230 /* Make sure X resources of the face are allocated. */
21231 xassert (face != NULL);
21232 PREPARE_FACE_FOR_DISPLAY (f, face);
21233 return face;
21234 }
21235
21236
21237 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21238 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21239
21240 static INLINE int
21241 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21242 {
21243 unsigned code;
21244
21245 if (CHAR_BYTE8_P (c))
21246 code = CHAR_TO_BYTE8 (c);
21247 else
21248 code = font->driver->encode_char (font, c);
21249
21250 if (code == FONT_INVALID_CODE)
21251 return 0;
21252 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21253 return 1;
21254 }
21255
21256
21257 /* Fill glyph string S with composition components specified by S->cmp.
21258
21259 BASE_FACE is the base face of the composition.
21260 S->cmp_from is the index of the first component for S.
21261
21262 OVERLAPS non-zero means S should draw the foreground only, and use
21263 its physical height for clipping. See also draw_glyphs.
21264
21265 Value is the index of a component not in S. */
21266
21267 static int
21268 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21269 int overlaps)
21270 {
21271 int i;
21272 /* For all glyphs of this composition, starting at the offset
21273 S->cmp_from, until we reach the end of the definition or encounter a
21274 glyph that requires the different face, add it to S. */
21275 struct face *face;
21276
21277 xassert (s);
21278
21279 s->for_overlaps = overlaps;
21280 s->face = NULL;
21281 s->font = NULL;
21282 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21283 {
21284 int c = COMPOSITION_GLYPH (s->cmp, i);
21285
21286 if (c != '\t')
21287 {
21288 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21289 -1, Qnil);
21290
21291 face = get_char_face_and_encoding (s->f, c, face_id,
21292 s->char2b + i, 1);
21293 if (face)
21294 {
21295 if (! s->face)
21296 {
21297 s->face = face;
21298 s->font = s->face->font;
21299 }
21300 else if (s->face != face)
21301 break;
21302 }
21303 }
21304 ++s->nchars;
21305 }
21306 s->cmp_to = i;
21307
21308 /* All glyph strings for the same composition has the same width,
21309 i.e. the width set for the first component of the composition. */
21310 s->width = s->first_glyph->pixel_width;
21311
21312 /* If the specified font could not be loaded, use the frame's
21313 default font, but record the fact that we couldn't load it in
21314 the glyph string so that we can draw rectangles for the
21315 characters of the glyph string. */
21316 if (s->font == NULL)
21317 {
21318 s->font_not_found_p = 1;
21319 s->font = FRAME_FONT (s->f);
21320 }
21321
21322 /* Adjust base line for subscript/superscript text. */
21323 s->ybase += s->first_glyph->voffset;
21324
21325 /* This glyph string must always be drawn with 16-bit functions. */
21326 s->two_byte_p = 1;
21327
21328 return s->cmp_to;
21329 }
21330
21331 static int
21332 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21333 int start, int end, int overlaps)
21334 {
21335 struct glyph *glyph, *last;
21336 Lisp_Object lgstring;
21337 int i;
21338
21339 s->for_overlaps = overlaps;
21340 glyph = s->row->glyphs[s->area] + start;
21341 last = s->row->glyphs[s->area] + end;
21342 s->cmp_id = glyph->u.cmp.id;
21343 s->cmp_from = glyph->slice.cmp.from;
21344 s->cmp_to = glyph->slice.cmp.to + 1;
21345 s->face = FACE_FROM_ID (s->f, face_id);
21346 lgstring = composition_gstring_from_id (s->cmp_id);
21347 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21348 glyph++;
21349 while (glyph < last
21350 && glyph->u.cmp.automatic
21351 && glyph->u.cmp.id == s->cmp_id
21352 && s->cmp_to == glyph->slice.cmp.from)
21353 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21354
21355 for (i = s->cmp_from; i < s->cmp_to; i++)
21356 {
21357 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21358 unsigned code = LGLYPH_CODE (lglyph);
21359
21360 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21361 }
21362 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21363 return glyph - s->row->glyphs[s->area];
21364 }
21365
21366
21367 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21368 See the comment of fill_glyph_string for arguments.
21369 Value is the index of the first glyph not in S. */
21370
21371
21372 static int
21373 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21374 int start, int end, int overlaps)
21375 {
21376 struct glyph *glyph, *last;
21377 int voffset;
21378
21379 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21380 s->for_overlaps = overlaps;
21381 glyph = s->row->glyphs[s->area] + start;
21382 last = s->row->glyphs[s->area] + end;
21383 voffset = glyph->voffset;
21384 s->face = FACE_FROM_ID (s->f, face_id);
21385 s->font = s->face->font;
21386 s->nchars = 1;
21387 s->width = glyph->pixel_width;
21388 glyph++;
21389 while (glyph < last
21390 && glyph->type == GLYPHLESS_GLYPH
21391 && glyph->voffset == voffset
21392 && glyph->face_id == face_id)
21393 {
21394 s->nchars++;
21395 s->width += glyph->pixel_width;
21396 glyph++;
21397 }
21398 s->ybase += voffset;
21399 return glyph - s->row->glyphs[s->area];
21400 }
21401
21402
21403 /* Fill glyph string S from a sequence of character glyphs.
21404
21405 FACE_ID is the face id of the string. START is the index of the
21406 first glyph to consider, END is the index of the last + 1.
21407 OVERLAPS non-zero means S should draw the foreground only, and use
21408 its physical height for clipping. See also draw_glyphs.
21409
21410 Value is the index of the first glyph not in S. */
21411
21412 static int
21413 fill_glyph_string (struct glyph_string *s, int face_id,
21414 int start, int end, int overlaps)
21415 {
21416 struct glyph *glyph, *last;
21417 int voffset;
21418 int glyph_not_available_p;
21419
21420 xassert (s->f == XFRAME (s->w->frame));
21421 xassert (s->nchars == 0);
21422 xassert (start >= 0 && end > start);
21423
21424 s->for_overlaps = overlaps;
21425 glyph = s->row->glyphs[s->area] + start;
21426 last = s->row->glyphs[s->area] + end;
21427 voffset = glyph->voffset;
21428 s->padding_p = glyph->padding_p;
21429 glyph_not_available_p = glyph->glyph_not_available_p;
21430
21431 while (glyph < last
21432 && glyph->type == CHAR_GLYPH
21433 && glyph->voffset == voffset
21434 /* Same face id implies same font, nowadays. */
21435 && glyph->face_id == face_id
21436 && glyph->glyph_not_available_p == glyph_not_available_p)
21437 {
21438 int two_byte_p;
21439
21440 s->face = get_glyph_face_and_encoding (s->f, glyph,
21441 s->char2b + s->nchars,
21442 &two_byte_p);
21443 s->two_byte_p = two_byte_p;
21444 ++s->nchars;
21445 xassert (s->nchars <= end - start);
21446 s->width += glyph->pixel_width;
21447 if (glyph++->padding_p != s->padding_p)
21448 break;
21449 }
21450
21451 s->font = s->face->font;
21452
21453 /* If the specified font could not be loaded, use the frame's font,
21454 but record the fact that we couldn't load it in
21455 S->font_not_found_p so that we can draw rectangles for the
21456 characters of the glyph string. */
21457 if (s->font == NULL || glyph_not_available_p)
21458 {
21459 s->font_not_found_p = 1;
21460 s->font = FRAME_FONT (s->f);
21461 }
21462
21463 /* Adjust base line for subscript/superscript text. */
21464 s->ybase += voffset;
21465
21466 xassert (s->face && s->face->gc);
21467 return glyph - s->row->glyphs[s->area];
21468 }
21469
21470
21471 /* Fill glyph string S from image glyph S->first_glyph. */
21472
21473 static void
21474 fill_image_glyph_string (struct glyph_string *s)
21475 {
21476 xassert (s->first_glyph->type == IMAGE_GLYPH);
21477 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21478 xassert (s->img);
21479 s->slice = s->first_glyph->slice.img;
21480 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21481 s->font = s->face->font;
21482 s->width = s->first_glyph->pixel_width;
21483
21484 /* Adjust base line for subscript/superscript text. */
21485 s->ybase += s->first_glyph->voffset;
21486 }
21487
21488
21489 /* Fill glyph string S from a sequence of stretch glyphs.
21490
21491 START is the index of the first glyph to consider,
21492 END is the index of the last + 1.
21493
21494 Value is the index of the first glyph not in S. */
21495
21496 static int
21497 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21498 {
21499 struct glyph *glyph, *last;
21500 int voffset, face_id;
21501
21502 xassert (s->first_glyph->type == STRETCH_GLYPH);
21503
21504 glyph = s->row->glyphs[s->area] + start;
21505 last = s->row->glyphs[s->area] + end;
21506 face_id = glyph->face_id;
21507 s->face = FACE_FROM_ID (s->f, face_id);
21508 s->font = s->face->font;
21509 s->width = glyph->pixel_width;
21510 s->nchars = 1;
21511 voffset = glyph->voffset;
21512
21513 for (++glyph;
21514 (glyph < last
21515 && glyph->type == STRETCH_GLYPH
21516 && glyph->voffset == voffset
21517 && glyph->face_id == face_id);
21518 ++glyph)
21519 s->width += glyph->pixel_width;
21520
21521 /* Adjust base line for subscript/superscript text. */
21522 s->ybase += voffset;
21523
21524 /* The case that face->gc == 0 is handled when drawing the glyph
21525 string by calling PREPARE_FACE_FOR_DISPLAY. */
21526 xassert (s->face);
21527 return glyph - s->row->glyphs[s->area];
21528 }
21529
21530 static struct font_metrics *
21531 get_per_char_metric (struct font *font, XChar2b *char2b)
21532 {
21533 static struct font_metrics metrics;
21534 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21535
21536 if (! font || code == FONT_INVALID_CODE)
21537 return NULL;
21538 font->driver->text_extents (font, &code, 1, &metrics);
21539 return &metrics;
21540 }
21541
21542 /* EXPORT for RIF:
21543 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21544 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21545 assumed to be zero. */
21546
21547 void
21548 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21549 {
21550 *left = *right = 0;
21551
21552 if (glyph->type == CHAR_GLYPH)
21553 {
21554 struct face *face;
21555 XChar2b char2b;
21556 struct font_metrics *pcm;
21557
21558 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21559 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21560 {
21561 if (pcm->rbearing > pcm->width)
21562 *right = pcm->rbearing - pcm->width;
21563 if (pcm->lbearing < 0)
21564 *left = -pcm->lbearing;
21565 }
21566 }
21567 else if (glyph->type == COMPOSITE_GLYPH)
21568 {
21569 if (! glyph->u.cmp.automatic)
21570 {
21571 struct composition *cmp = composition_table[glyph->u.cmp.id];
21572
21573 if (cmp->rbearing > cmp->pixel_width)
21574 *right = cmp->rbearing - cmp->pixel_width;
21575 if (cmp->lbearing < 0)
21576 *left = - cmp->lbearing;
21577 }
21578 else
21579 {
21580 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21581 struct font_metrics metrics;
21582
21583 composition_gstring_width (gstring, glyph->slice.cmp.from,
21584 glyph->slice.cmp.to + 1, &metrics);
21585 if (metrics.rbearing > metrics.width)
21586 *right = metrics.rbearing - metrics.width;
21587 if (metrics.lbearing < 0)
21588 *left = - metrics.lbearing;
21589 }
21590 }
21591 }
21592
21593
21594 /* Return the index of the first glyph preceding glyph string S that
21595 is overwritten by S because of S's left overhang. Value is -1
21596 if no glyphs are overwritten. */
21597
21598 static int
21599 left_overwritten (struct glyph_string *s)
21600 {
21601 int k;
21602
21603 if (s->left_overhang)
21604 {
21605 int x = 0, i;
21606 struct glyph *glyphs = s->row->glyphs[s->area];
21607 int first = s->first_glyph - glyphs;
21608
21609 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21610 x -= glyphs[i].pixel_width;
21611
21612 k = i + 1;
21613 }
21614 else
21615 k = -1;
21616
21617 return k;
21618 }
21619
21620
21621 /* Return the index of the first glyph preceding glyph string S that
21622 is overwriting S because of its right overhang. Value is -1 if no
21623 glyph in front of S overwrites S. */
21624
21625 static int
21626 left_overwriting (struct glyph_string *s)
21627 {
21628 int i, k, x;
21629 struct glyph *glyphs = s->row->glyphs[s->area];
21630 int first = s->first_glyph - glyphs;
21631
21632 k = -1;
21633 x = 0;
21634 for (i = first - 1; i >= 0; --i)
21635 {
21636 int left, right;
21637 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21638 if (x + right > 0)
21639 k = i;
21640 x -= glyphs[i].pixel_width;
21641 }
21642
21643 return k;
21644 }
21645
21646
21647 /* Return the index of the last glyph following glyph string S that is
21648 overwritten by S because of S's right overhang. Value is -1 if
21649 no such glyph is found. */
21650
21651 static int
21652 right_overwritten (struct glyph_string *s)
21653 {
21654 int k = -1;
21655
21656 if (s->right_overhang)
21657 {
21658 int x = 0, i;
21659 struct glyph *glyphs = s->row->glyphs[s->area];
21660 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21661 int end = s->row->used[s->area];
21662
21663 for (i = first; i < end && s->right_overhang > x; ++i)
21664 x += glyphs[i].pixel_width;
21665
21666 k = i;
21667 }
21668
21669 return k;
21670 }
21671
21672
21673 /* Return the index of the last glyph following glyph string S that
21674 overwrites S because of its left overhang. Value is negative
21675 if no such glyph is found. */
21676
21677 static int
21678 right_overwriting (struct glyph_string *s)
21679 {
21680 int i, k, x;
21681 int end = s->row->used[s->area];
21682 struct glyph *glyphs = s->row->glyphs[s->area];
21683 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21684
21685 k = -1;
21686 x = 0;
21687 for (i = first; i < end; ++i)
21688 {
21689 int left, right;
21690 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21691 if (x - left < 0)
21692 k = i;
21693 x += glyphs[i].pixel_width;
21694 }
21695
21696 return k;
21697 }
21698
21699
21700 /* Set background width of glyph string S. START is the index of the
21701 first glyph following S. LAST_X is the right-most x-position + 1
21702 in the drawing area. */
21703
21704 static INLINE void
21705 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21706 {
21707 /* If the face of this glyph string has to be drawn to the end of
21708 the drawing area, set S->extends_to_end_of_line_p. */
21709
21710 if (start == s->row->used[s->area]
21711 && s->area == TEXT_AREA
21712 && ((s->row->fill_line_p
21713 && (s->hl == DRAW_NORMAL_TEXT
21714 || s->hl == DRAW_IMAGE_RAISED
21715 || s->hl == DRAW_IMAGE_SUNKEN))
21716 || s->hl == DRAW_MOUSE_FACE))
21717 s->extends_to_end_of_line_p = 1;
21718
21719 /* If S extends its face to the end of the line, set its
21720 background_width to the distance to the right edge of the drawing
21721 area. */
21722 if (s->extends_to_end_of_line_p)
21723 s->background_width = last_x - s->x + 1;
21724 else
21725 s->background_width = s->width;
21726 }
21727
21728
21729 /* Compute overhangs and x-positions for glyph string S and its
21730 predecessors, or successors. X is the starting x-position for S.
21731 BACKWARD_P non-zero means process predecessors. */
21732
21733 static void
21734 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21735 {
21736 if (backward_p)
21737 {
21738 while (s)
21739 {
21740 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21741 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21742 x -= s->width;
21743 s->x = x;
21744 s = s->prev;
21745 }
21746 }
21747 else
21748 {
21749 while (s)
21750 {
21751 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21752 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21753 s->x = x;
21754 x += s->width;
21755 s = s->next;
21756 }
21757 }
21758 }
21759
21760
21761
21762 /* The following macros are only called from draw_glyphs below.
21763 They reference the following parameters of that function directly:
21764 `w', `row', `area', and `overlap_p'
21765 as well as the following local variables:
21766 `s', `f', and `hdc' (in W32) */
21767
21768 #ifdef HAVE_NTGUI
21769 /* On W32, silently add local `hdc' variable to argument list of
21770 init_glyph_string. */
21771 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21772 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21773 #else
21774 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21775 init_glyph_string (s, char2b, w, row, area, start, hl)
21776 #endif
21777
21778 /* Add a glyph string for a stretch glyph to the list of strings
21779 between HEAD and TAIL. START is the index of the stretch glyph in
21780 row area AREA of glyph row ROW. END is the index of the last glyph
21781 in that glyph row area. X is the current output position assigned
21782 to the new glyph string constructed. HL overrides that face of the
21783 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21784 is the right-most x-position of the drawing area. */
21785
21786 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21787 and below -- keep them on one line. */
21788 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21789 do \
21790 { \
21791 s = (struct glyph_string *) alloca (sizeof *s); \
21792 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21793 START = fill_stretch_glyph_string (s, START, END); \
21794 append_glyph_string (&HEAD, &TAIL, s); \
21795 s->x = (X); \
21796 } \
21797 while (0)
21798
21799
21800 /* Add a glyph string for an image glyph to the list of strings
21801 between HEAD and TAIL. START is the index of the image glyph in
21802 row area AREA of glyph row ROW. END is the index of the last glyph
21803 in that glyph row area. X is the current output position assigned
21804 to the new glyph string constructed. HL overrides that face of the
21805 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21806 is the right-most x-position of the drawing area. */
21807
21808 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21809 do \
21810 { \
21811 s = (struct glyph_string *) alloca (sizeof *s); \
21812 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21813 fill_image_glyph_string (s); \
21814 append_glyph_string (&HEAD, &TAIL, s); \
21815 ++START; \
21816 s->x = (X); \
21817 } \
21818 while (0)
21819
21820
21821 /* Add a glyph string for a sequence of character glyphs to the list
21822 of strings between HEAD and TAIL. START is the index of the first
21823 glyph in row area AREA of glyph row ROW that is part of the new
21824 glyph string. END is the index of the last glyph in that glyph row
21825 area. X is the current output position assigned to the new glyph
21826 string constructed. HL overrides that face of the glyph; e.g. it
21827 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21828 right-most x-position of the drawing area. */
21829
21830 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21831 do \
21832 { \
21833 int face_id; \
21834 XChar2b *char2b; \
21835 \
21836 face_id = (row)->glyphs[area][START].face_id; \
21837 \
21838 s = (struct glyph_string *) alloca (sizeof *s); \
21839 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21840 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21841 append_glyph_string (&HEAD, &TAIL, s); \
21842 s->x = (X); \
21843 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21844 } \
21845 while (0)
21846
21847
21848 /* Add a glyph string for a composite sequence to the list of strings
21849 between HEAD and TAIL. START is the index of the first glyph in
21850 row area AREA of glyph row ROW that is part of the new glyph
21851 string. END is the index of the last glyph in that glyph row area.
21852 X is the current output position assigned to the new glyph string
21853 constructed. HL overrides that face of the glyph; e.g. it is
21854 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21855 x-position of the drawing area. */
21856
21857 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21858 do { \
21859 int face_id = (row)->glyphs[area][START].face_id; \
21860 struct face *base_face = FACE_FROM_ID (f, face_id); \
21861 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21862 struct composition *cmp = composition_table[cmp_id]; \
21863 XChar2b *char2b; \
21864 struct glyph_string *first_s IF_LINT (= NULL); \
21865 int n; \
21866 \
21867 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21868 \
21869 /* Make glyph_strings for each glyph sequence that is drawable by \
21870 the same face, and append them to HEAD/TAIL. */ \
21871 for (n = 0; n < cmp->glyph_len;) \
21872 { \
21873 s = (struct glyph_string *) alloca (sizeof *s); \
21874 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21875 append_glyph_string (&(HEAD), &(TAIL), s); \
21876 s->cmp = cmp; \
21877 s->cmp_from = n; \
21878 s->x = (X); \
21879 if (n == 0) \
21880 first_s = s; \
21881 n = fill_composite_glyph_string (s, base_face, overlaps); \
21882 } \
21883 \
21884 ++START; \
21885 s = first_s; \
21886 } while (0)
21887
21888
21889 /* Add a glyph string for a glyph-string sequence to the list of strings
21890 between HEAD and TAIL. */
21891
21892 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21893 do { \
21894 int face_id; \
21895 XChar2b *char2b; \
21896 Lisp_Object gstring; \
21897 \
21898 face_id = (row)->glyphs[area][START].face_id; \
21899 gstring = (composition_gstring_from_id \
21900 ((row)->glyphs[area][START].u.cmp.id)); \
21901 s = (struct glyph_string *) alloca (sizeof *s); \
21902 char2b = (XChar2b *) alloca ((sizeof *char2b) \
21903 * LGSTRING_GLYPH_LEN (gstring)); \
21904 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21905 append_glyph_string (&(HEAD), &(TAIL), s); \
21906 s->x = (X); \
21907 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
21908 } while (0)
21909
21910
21911 /* Add a glyph string for a sequence of glyphless character's glyphs
21912 to the list of strings between HEAD and TAIL. The meanings of
21913 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
21914
21915 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21916 do \
21917 { \
21918 int face_id; \
21919 \
21920 face_id = (row)->glyphs[area][START].face_id; \
21921 \
21922 s = (struct glyph_string *) alloca (sizeof *s); \
21923 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21924 append_glyph_string (&HEAD, &TAIL, s); \
21925 s->x = (X); \
21926 START = fill_glyphless_glyph_string (s, face_id, START, END, \
21927 overlaps); \
21928 } \
21929 while (0)
21930
21931
21932 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
21933 of AREA of glyph row ROW on window W between indices START and END.
21934 HL overrides the face for drawing glyph strings, e.g. it is
21935 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
21936 x-positions of the drawing area.
21937
21938 This is an ugly monster macro construct because we must use alloca
21939 to allocate glyph strings (because draw_glyphs can be called
21940 asynchronously). */
21941
21942 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21943 do \
21944 { \
21945 HEAD = TAIL = NULL; \
21946 while (START < END) \
21947 { \
21948 struct glyph *first_glyph = (row)->glyphs[area] + START; \
21949 switch (first_glyph->type) \
21950 { \
21951 case CHAR_GLYPH: \
21952 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
21953 HL, X, LAST_X); \
21954 break; \
21955 \
21956 case COMPOSITE_GLYPH: \
21957 if (first_glyph->u.cmp.automatic) \
21958 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
21959 HL, X, LAST_X); \
21960 else \
21961 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
21962 HL, X, LAST_X); \
21963 break; \
21964 \
21965 case STRETCH_GLYPH: \
21966 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
21967 HL, X, LAST_X); \
21968 break; \
21969 \
21970 case IMAGE_GLYPH: \
21971 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
21972 HL, X, LAST_X); \
21973 break; \
21974 \
21975 case GLYPHLESS_GLYPH: \
21976 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
21977 HL, X, LAST_X); \
21978 break; \
21979 \
21980 default: \
21981 abort (); \
21982 } \
21983 \
21984 if (s) \
21985 { \
21986 set_glyph_string_background_width (s, START, LAST_X); \
21987 (X) += s->width; \
21988 } \
21989 } \
21990 } while (0)
21991
21992
21993 /* Draw glyphs between START and END in AREA of ROW on window W,
21994 starting at x-position X. X is relative to AREA in W. HL is a
21995 face-override with the following meaning:
21996
21997 DRAW_NORMAL_TEXT draw normally
21998 DRAW_CURSOR draw in cursor face
21999 DRAW_MOUSE_FACE draw in mouse face.
22000 DRAW_INVERSE_VIDEO draw in mode line face
22001 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22002 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22003
22004 If OVERLAPS is non-zero, draw only the foreground of characters and
22005 clip to the physical height of ROW. Non-zero value also defines
22006 the overlapping part to be drawn:
22007
22008 OVERLAPS_PRED overlap with preceding rows
22009 OVERLAPS_SUCC overlap with succeeding rows
22010 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22011 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22012
22013 Value is the x-position reached, relative to AREA of W. */
22014
22015 static int
22016 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22017 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22018 enum draw_glyphs_face hl, int overlaps)
22019 {
22020 struct glyph_string *head, *tail;
22021 struct glyph_string *s;
22022 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22023 int i, j, x_reached, last_x, area_left = 0;
22024 struct frame *f = XFRAME (WINDOW_FRAME (w));
22025 DECLARE_HDC (hdc);
22026
22027 ALLOCATE_HDC (hdc, f);
22028
22029 /* Let's rather be paranoid than getting a SEGV. */
22030 end = min (end, row->used[area]);
22031 start = max (0, start);
22032 start = min (end, start);
22033
22034 /* Translate X to frame coordinates. Set last_x to the right
22035 end of the drawing area. */
22036 if (row->full_width_p)
22037 {
22038 /* X is relative to the left edge of W, without scroll bars
22039 or fringes. */
22040 area_left = WINDOW_LEFT_EDGE_X (w);
22041 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22042 }
22043 else
22044 {
22045 area_left = window_box_left (w, area);
22046 last_x = area_left + window_box_width (w, area);
22047 }
22048 x += area_left;
22049
22050 /* Build a doubly-linked list of glyph_string structures between
22051 head and tail from what we have to draw. Note that the macro
22052 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22053 the reason we use a separate variable `i'. */
22054 i = start;
22055 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22056 if (tail)
22057 x_reached = tail->x + tail->background_width;
22058 else
22059 x_reached = x;
22060
22061 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22062 the row, redraw some glyphs in front or following the glyph
22063 strings built above. */
22064 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22065 {
22066 struct glyph_string *h, *t;
22067 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22068 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22069 int check_mouse_face = 0;
22070 int dummy_x = 0;
22071
22072 /* If mouse highlighting is on, we may need to draw adjacent
22073 glyphs using mouse-face highlighting. */
22074 if (area == TEXT_AREA && row->mouse_face_p)
22075 {
22076 struct glyph_row *mouse_beg_row, *mouse_end_row;
22077
22078 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22079 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22080
22081 if (row >= mouse_beg_row && row <= mouse_end_row)
22082 {
22083 check_mouse_face = 1;
22084 mouse_beg_col = (row == mouse_beg_row)
22085 ? hlinfo->mouse_face_beg_col : 0;
22086 mouse_end_col = (row == mouse_end_row)
22087 ? hlinfo->mouse_face_end_col
22088 : row->used[TEXT_AREA];
22089 }
22090 }
22091
22092 /* Compute overhangs for all glyph strings. */
22093 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22094 for (s = head; s; s = s->next)
22095 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22096
22097 /* Prepend glyph strings for glyphs in front of the first glyph
22098 string that are overwritten because of the first glyph
22099 string's left overhang. The background of all strings
22100 prepended must be drawn because the first glyph string
22101 draws over it. */
22102 i = left_overwritten (head);
22103 if (i >= 0)
22104 {
22105 enum draw_glyphs_face overlap_hl;
22106
22107 /* If this row contains mouse highlighting, attempt to draw
22108 the overlapped glyphs with the correct highlight. This
22109 code fails if the overlap encompasses more than one glyph
22110 and mouse-highlight spans only some of these glyphs.
22111 However, making it work perfectly involves a lot more
22112 code, and I don't know if the pathological case occurs in
22113 practice, so we'll stick to this for now. --- cyd */
22114 if (check_mouse_face
22115 && mouse_beg_col < start && mouse_end_col > i)
22116 overlap_hl = DRAW_MOUSE_FACE;
22117 else
22118 overlap_hl = DRAW_NORMAL_TEXT;
22119
22120 j = i;
22121 BUILD_GLYPH_STRINGS (j, start, h, t,
22122 overlap_hl, dummy_x, last_x);
22123 start = i;
22124 compute_overhangs_and_x (t, head->x, 1);
22125 prepend_glyph_string_lists (&head, &tail, h, t);
22126 clip_head = head;
22127 }
22128
22129 /* Prepend glyph strings for glyphs in front of the first glyph
22130 string that overwrite that glyph string because of their
22131 right overhang. For these strings, only the foreground must
22132 be drawn, because it draws over the glyph string at `head'.
22133 The background must not be drawn because this would overwrite
22134 right overhangs of preceding glyphs for which no glyph
22135 strings exist. */
22136 i = left_overwriting (head);
22137 if (i >= 0)
22138 {
22139 enum draw_glyphs_face overlap_hl;
22140
22141 if (check_mouse_face
22142 && mouse_beg_col < start && mouse_end_col > i)
22143 overlap_hl = DRAW_MOUSE_FACE;
22144 else
22145 overlap_hl = DRAW_NORMAL_TEXT;
22146
22147 clip_head = head;
22148 BUILD_GLYPH_STRINGS (i, start, h, t,
22149 overlap_hl, dummy_x, last_x);
22150 for (s = h; s; s = s->next)
22151 s->background_filled_p = 1;
22152 compute_overhangs_and_x (t, head->x, 1);
22153 prepend_glyph_string_lists (&head, &tail, h, t);
22154 }
22155
22156 /* Append glyphs strings for glyphs following the last glyph
22157 string tail that are overwritten by tail. The background of
22158 these strings has to be drawn because tail's foreground draws
22159 over it. */
22160 i = right_overwritten (tail);
22161 if (i >= 0)
22162 {
22163 enum draw_glyphs_face overlap_hl;
22164
22165 if (check_mouse_face
22166 && mouse_beg_col < i && mouse_end_col > end)
22167 overlap_hl = DRAW_MOUSE_FACE;
22168 else
22169 overlap_hl = DRAW_NORMAL_TEXT;
22170
22171 BUILD_GLYPH_STRINGS (end, i, h, t,
22172 overlap_hl, x, last_x);
22173 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22174 we don't have `end = i;' here. */
22175 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22176 append_glyph_string_lists (&head, &tail, h, t);
22177 clip_tail = tail;
22178 }
22179
22180 /* Append glyph strings for glyphs following the last glyph
22181 string tail that overwrite tail. The foreground of such
22182 glyphs has to be drawn because it writes into the background
22183 of tail. The background must not be drawn because it could
22184 paint over the foreground of following glyphs. */
22185 i = right_overwriting (tail);
22186 if (i >= 0)
22187 {
22188 enum draw_glyphs_face overlap_hl;
22189 if (check_mouse_face
22190 && mouse_beg_col < i && mouse_end_col > end)
22191 overlap_hl = DRAW_MOUSE_FACE;
22192 else
22193 overlap_hl = DRAW_NORMAL_TEXT;
22194
22195 clip_tail = tail;
22196 i++; /* We must include the Ith glyph. */
22197 BUILD_GLYPH_STRINGS (end, i, h, t,
22198 overlap_hl, x, last_x);
22199 for (s = h; s; s = s->next)
22200 s->background_filled_p = 1;
22201 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22202 append_glyph_string_lists (&head, &tail, h, t);
22203 }
22204 if (clip_head || clip_tail)
22205 for (s = head; s; s = s->next)
22206 {
22207 s->clip_head = clip_head;
22208 s->clip_tail = clip_tail;
22209 }
22210 }
22211
22212 /* Draw all strings. */
22213 for (s = head; s; s = s->next)
22214 FRAME_RIF (f)->draw_glyph_string (s);
22215
22216 #ifndef HAVE_NS
22217 /* When focus a sole frame and move horizontally, this sets on_p to 0
22218 causing a failure to erase prev cursor position. */
22219 if (area == TEXT_AREA
22220 && !row->full_width_p
22221 /* When drawing overlapping rows, only the glyph strings'
22222 foreground is drawn, which doesn't erase a cursor
22223 completely. */
22224 && !overlaps)
22225 {
22226 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22227 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22228 : (tail ? tail->x + tail->background_width : x));
22229 x0 -= area_left;
22230 x1 -= area_left;
22231
22232 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22233 row->y, MATRIX_ROW_BOTTOM_Y (row));
22234 }
22235 #endif
22236
22237 /* Value is the x-position up to which drawn, relative to AREA of W.
22238 This doesn't include parts drawn because of overhangs. */
22239 if (row->full_width_p)
22240 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22241 else
22242 x_reached -= area_left;
22243
22244 RELEASE_HDC (hdc, f);
22245
22246 return x_reached;
22247 }
22248
22249 /* Expand row matrix if too narrow. Don't expand if area
22250 is not present. */
22251
22252 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22253 { \
22254 if (!fonts_changed_p \
22255 && (it->glyph_row->glyphs[area] \
22256 < it->glyph_row->glyphs[area + 1])) \
22257 { \
22258 it->w->ncols_scale_factor++; \
22259 fonts_changed_p = 1; \
22260 } \
22261 }
22262
22263 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22264 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22265
22266 static INLINE void
22267 append_glyph (struct it *it)
22268 {
22269 struct glyph *glyph;
22270 enum glyph_row_area area = it->area;
22271
22272 xassert (it->glyph_row);
22273 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22274
22275 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22276 if (glyph < it->glyph_row->glyphs[area + 1])
22277 {
22278 /* If the glyph row is reversed, we need to prepend the glyph
22279 rather than append it. */
22280 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22281 {
22282 struct glyph *g;
22283
22284 /* Make room for the additional glyph. */
22285 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22286 g[1] = *g;
22287 glyph = it->glyph_row->glyphs[area];
22288 }
22289 glyph->charpos = CHARPOS (it->position);
22290 glyph->object = it->object;
22291 if (it->pixel_width > 0)
22292 {
22293 glyph->pixel_width = it->pixel_width;
22294 glyph->padding_p = 0;
22295 }
22296 else
22297 {
22298 /* Assure at least 1-pixel width. Otherwise, cursor can't
22299 be displayed correctly. */
22300 glyph->pixel_width = 1;
22301 glyph->padding_p = 1;
22302 }
22303 glyph->ascent = it->ascent;
22304 glyph->descent = it->descent;
22305 glyph->voffset = it->voffset;
22306 glyph->type = CHAR_GLYPH;
22307 glyph->avoid_cursor_p = it->avoid_cursor_p;
22308 glyph->multibyte_p = it->multibyte_p;
22309 glyph->left_box_line_p = it->start_of_box_run_p;
22310 glyph->right_box_line_p = it->end_of_box_run_p;
22311 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22312 || it->phys_descent > it->descent);
22313 glyph->glyph_not_available_p = it->glyph_not_available_p;
22314 glyph->face_id = it->face_id;
22315 glyph->u.ch = it->char_to_display;
22316 glyph->slice.img = null_glyph_slice;
22317 glyph->font_type = FONT_TYPE_UNKNOWN;
22318 if (it->bidi_p)
22319 {
22320 glyph->resolved_level = it->bidi_it.resolved_level;
22321 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22322 abort ();
22323 glyph->bidi_type = it->bidi_it.type;
22324 }
22325 else
22326 {
22327 glyph->resolved_level = 0;
22328 glyph->bidi_type = UNKNOWN_BT;
22329 }
22330 ++it->glyph_row->used[area];
22331 }
22332 else
22333 IT_EXPAND_MATRIX_WIDTH (it, area);
22334 }
22335
22336 /* Store one glyph for the composition IT->cmp_it.id in
22337 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22338 non-null. */
22339
22340 static INLINE void
22341 append_composite_glyph (struct it *it)
22342 {
22343 struct glyph *glyph;
22344 enum glyph_row_area area = it->area;
22345
22346 xassert (it->glyph_row);
22347
22348 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22349 if (glyph < it->glyph_row->glyphs[area + 1])
22350 {
22351 /* If the glyph row is reversed, we need to prepend the glyph
22352 rather than append it. */
22353 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22354 {
22355 struct glyph *g;
22356
22357 /* Make room for the new glyph. */
22358 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22359 g[1] = *g;
22360 glyph = it->glyph_row->glyphs[it->area];
22361 }
22362 glyph->charpos = it->cmp_it.charpos;
22363 glyph->object = it->object;
22364 glyph->pixel_width = it->pixel_width;
22365 glyph->ascent = it->ascent;
22366 glyph->descent = it->descent;
22367 glyph->voffset = it->voffset;
22368 glyph->type = COMPOSITE_GLYPH;
22369 if (it->cmp_it.ch < 0)
22370 {
22371 glyph->u.cmp.automatic = 0;
22372 glyph->u.cmp.id = it->cmp_it.id;
22373 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22374 }
22375 else
22376 {
22377 glyph->u.cmp.automatic = 1;
22378 glyph->u.cmp.id = it->cmp_it.id;
22379 glyph->slice.cmp.from = it->cmp_it.from;
22380 glyph->slice.cmp.to = it->cmp_it.to - 1;
22381 }
22382 glyph->avoid_cursor_p = it->avoid_cursor_p;
22383 glyph->multibyte_p = it->multibyte_p;
22384 glyph->left_box_line_p = it->start_of_box_run_p;
22385 glyph->right_box_line_p = it->end_of_box_run_p;
22386 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22387 || it->phys_descent > it->descent);
22388 glyph->padding_p = 0;
22389 glyph->glyph_not_available_p = 0;
22390 glyph->face_id = it->face_id;
22391 glyph->font_type = FONT_TYPE_UNKNOWN;
22392 if (it->bidi_p)
22393 {
22394 glyph->resolved_level = it->bidi_it.resolved_level;
22395 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22396 abort ();
22397 glyph->bidi_type = it->bidi_it.type;
22398 }
22399 ++it->glyph_row->used[area];
22400 }
22401 else
22402 IT_EXPAND_MATRIX_WIDTH (it, area);
22403 }
22404
22405
22406 /* Change IT->ascent and IT->height according to the setting of
22407 IT->voffset. */
22408
22409 static INLINE void
22410 take_vertical_position_into_account (struct it *it)
22411 {
22412 if (it->voffset)
22413 {
22414 if (it->voffset < 0)
22415 /* Increase the ascent so that we can display the text higher
22416 in the line. */
22417 it->ascent -= it->voffset;
22418 else
22419 /* Increase the descent so that we can display the text lower
22420 in the line. */
22421 it->descent += it->voffset;
22422 }
22423 }
22424
22425
22426 /* Produce glyphs/get display metrics for the image IT is loaded with.
22427 See the description of struct display_iterator in dispextern.h for
22428 an overview of struct display_iterator. */
22429
22430 static void
22431 produce_image_glyph (struct it *it)
22432 {
22433 struct image *img;
22434 struct face *face;
22435 int glyph_ascent, crop;
22436 struct glyph_slice slice;
22437
22438 xassert (it->what == IT_IMAGE);
22439
22440 face = FACE_FROM_ID (it->f, it->face_id);
22441 xassert (face);
22442 /* Make sure X resources of the face is loaded. */
22443 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22444
22445 if (it->image_id < 0)
22446 {
22447 /* Fringe bitmap. */
22448 it->ascent = it->phys_ascent = 0;
22449 it->descent = it->phys_descent = 0;
22450 it->pixel_width = 0;
22451 it->nglyphs = 0;
22452 return;
22453 }
22454
22455 img = IMAGE_FROM_ID (it->f, it->image_id);
22456 xassert (img);
22457 /* Make sure X resources of the image is loaded. */
22458 prepare_image_for_display (it->f, img);
22459
22460 slice.x = slice.y = 0;
22461 slice.width = img->width;
22462 slice.height = img->height;
22463
22464 if (INTEGERP (it->slice.x))
22465 slice.x = XINT (it->slice.x);
22466 else if (FLOATP (it->slice.x))
22467 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22468
22469 if (INTEGERP (it->slice.y))
22470 slice.y = XINT (it->slice.y);
22471 else if (FLOATP (it->slice.y))
22472 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22473
22474 if (INTEGERP (it->slice.width))
22475 slice.width = XINT (it->slice.width);
22476 else if (FLOATP (it->slice.width))
22477 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22478
22479 if (INTEGERP (it->slice.height))
22480 slice.height = XINT (it->slice.height);
22481 else if (FLOATP (it->slice.height))
22482 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22483
22484 if (slice.x >= img->width)
22485 slice.x = img->width;
22486 if (slice.y >= img->height)
22487 slice.y = img->height;
22488 if (slice.x + slice.width >= img->width)
22489 slice.width = img->width - slice.x;
22490 if (slice.y + slice.height > img->height)
22491 slice.height = img->height - slice.y;
22492
22493 if (slice.width == 0 || slice.height == 0)
22494 return;
22495
22496 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22497
22498 it->descent = slice.height - glyph_ascent;
22499 if (slice.y == 0)
22500 it->descent += img->vmargin;
22501 if (slice.y + slice.height == img->height)
22502 it->descent += img->vmargin;
22503 it->phys_descent = it->descent;
22504
22505 it->pixel_width = slice.width;
22506 if (slice.x == 0)
22507 it->pixel_width += img->hmargin;
22508 if (slice.x + slice.width == img->width)
22509 it->pixel_width += img->hmargin;
22510
22511 /* It's quite possible for images to have an ascent greater than
22512 their height, so don't get confused in that case. */
22513 if (it->descent < 0)
22514 it->descent = 0;
22515
22516 it->nglyphs = 1;
22517
22518 if (face->box != FACE_NO_BOX)
22519 {
22520 if (face->box_line_width > 0)
22521 {
22522 if (slice.y == 0)
22523 it->ascent += face->box_line_width;
22524 if (slice.y + slice.height == img->height)
22525 it->descent += face->box_line_width;
22526 }
22527
22528 if (it->start_of_box_run_p && slice.x == 0)
22529 it->pixel_width += eabs (face->box_line_width);
22530 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22531 it->pixel_width += eabs (face->box_line_width);
22532 }
22533
22534 take_vertical_position_into_account (it);
22535
22536 /* Automatically crop wide image glyphs at right edge so we can
22537 draw the cursor on same display row. */
22538 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22539 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22540 {
22541 it->pixel_width -= crop;
22542 slice.width -= crop;
22543 }
22544
22545 if (it->glyph_row)
22546 {
22547 struct glyph *glyph;
22548 enum glyph_row_area area = it->area;
22549
22550 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22551 if (glyph < it->glyph_row->glyphs[area + 1])
22552 {
22553 glyph->charpos = CHARPOS (it->position);
22554 glyph->object = it->object;
22555 glyph->pixel_width = it->pixel_width;
22556 glyph->ascent = glyph_ascent;
22557 glyph->descent = it->descent;
22558 glyph->voffset = it->voffset;
22559 glyph->type = IMAGE_GLYPH;
22560 glyph->avoid_cursor_p = it->avoid_cursor_p;
22561 glyph->multibyte_p = it->multibyte_p;
22562 glyph->left_box_line_p = it->start_of_box_run_p;
22563 glyph->right_box_line_p = it->end_of_box_run_p;
22564 glyph->overlaps_vertically_p = 0;
22565 glyph->padding_p = 0;
22566 glyph->glyph_not_available_p = 0;
22567 glyph->face_id = it->face_id;
22568 glyph->u.img_id = img->id;
22569 glyph->slice.img = slice;
22570 glyph->font_type = FONT_TYPE_UNKNOWN;
22571 if (it->bidi_p)
22572 {
22573 glyph->resolved_level = it->bidi_it.resolved_level;
22574 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22575 abort ();
22576 glyph->bidi_type = it->bidi_it.type;
22577 }
22578 ++it->glyph_row->used[area];
22579 }
22580 else
22581 IT_EXPAND_MATRIX_WIDTH (it, area);
22582 }
22583 }
22584
22585
22586 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22587 of the glyph, WIDTH and HEIGHT are the width and height of the
22588 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22589
22590 static void
22591 append_stretch_glyph (struct it *it, Lisp_Object object,
22592 int width, int height, int ascent)
22593 {
22594 struct glyph *glyph;
22595 enum glyph_row_area area = it->area;
22596
22597 xassert (ascent >= 0 && ascent <= height);
22598
22599 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22600 if (glyph < it->glyph_row->glyphs[area + 1])
22601 {
22602 /* If the glyph row is reversed, we need to prepend the glyph
22603 rather than append it. */
22604 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22605 {
22606 struct glyph *g;
22607
22608 /* Make room for the additional glyph. */
22609 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22610 g[1] = *g;
22611 glyph = it->glyph_row->glyphs[area];
22612 }
22613 glyph->charpos = CHARPOS (it->position);
22614 glyph->object = object;
22615 glyph->pixel_width = width;
22616 glyph->ascent = ascent;
22617 glyph->descent = height - ascent;
22618 glyph->voffset = it->voffset;
22619 glyph->type = STRETCH_GLYPH;
22620 glyph->avoid_cursor_p = it->avoid_cursor_p;
22621 glyph->multibyte_p = it->multibyte_p;
22622 glyph->left_box_line_p = it->start_of_box_run_p;
22623 glyph->right_box_line_p = it->end_of_box_run_p;
22624 glyph->overlaps_vertically_p = 0;
22625 glyph->padding_p = 0;
22626 glyph->glyph_not_available_p = 0;
22627 glyph->face_id = it->face_id;
22628 glyph->u.stretch.ascent = ascent;
22629 glyph->u.stretch.height = height;
22630 glyph->slice.img = null_glyph_slice;
22631 glyph->font_type = FONT_TYPE_UNKNOWN;
22632 if (it->bidi_p)
22633 {
22634 glyph->resolved_level = it->bidi_it.resolved_level;
22635 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22636 abort ();
22637 glyph->bidi_type = it->bidi_it.type;
22638 }
22639 else
22640 {
22641 glyph->resolved_level = 0;
22642 glyph->bidi_type = UNKNOWN_BT;
22643 }
22644 ++it->glyph_row->used[area];
22645 }
22646 else
22647 IT_EXPAND_MATRIX_WIDTH (it, area);
22648 }
22649
22650
22651 /* Produce a stretch glyph for iterator IT. IT->object is the value
22652 of the glyph property displayed. The value must be a list
22653 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22654 being recognized:
22655
22656 1. `:width WIDTH' specifies that the space should be WIDTH *
22657 canonical char width wide. WIDTH may be an integer or floating
22658 point number.
22659
22660 2. `:relative-width FACTOR' specifies that the width of the stretch
22661 should be computed from the width of the first character having the
22662 `glyph' property, and should be FACTOR times that width.
22663
22664 3. `:align-to HPOS' specifies that the space should be wide enough
22665 to reach HPOS, a value in canonical character units.
22666
22667 Exactly one of the above pairs must be present.
22668
22669 4. `:height HEIGHT' specifies that the height of the stretch produced
22670 should be HEIGHT, measured in canonical character units.
22671
22672 5. `:relative-height FACTOR' specifies that the height of the
22673 stretch should be FACTOR times the height of the characters having
22674 the glyph property.
22675
22676 Either none or exactly one of 4 or 5 must be present.
22677
22678 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22679 of the stretch should be used for the ascent of the stretch.
22680 ASCENT must be in the range 0 <= ASCENT <= 100. */
22681
22682 static void
22683 produce_stretch_glyph (struct it *it)
22684 {
22685 /* (space :width WIDTH :height HEIGHT ...) */
22686 Lisp_Object prop, plist;
22687 int width = 0, height = 0, align_to = -1;
22688 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22689 int ascent = 0;
22690 double tem;
22691 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22692 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22693
22694 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22695
22696 /* List should start with `space'. */
22697 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22698 plist = XCDR (it->object);
22699
22700 /* Compute the width of the stretch. */
22701 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22702 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22703 {
22704 /* Absolute width `:width WIDTH' specified and valid. */
22705 zero_width_ok_p = 1;
22706 width = (int)tem;
22707 }
22708 else if (prop = Fplist_get (plist, QCrelative_width),
22709 NUMVAL (prop) > 0)
22710 {
22711 /* Relative width `:relative-width FACTOR' specified and valid.
22712 Compute the width of the characters having the `glyph'
22713 property. */
22714 struct it it2;
22715 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22716
22717 it2 = *it;
22718 if (it->multibyte_p)
22719 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22720 else
22721 {
22722 it2.c = it2.char_to_display = *p, it2.len = 1;
22723 if (! ASCII_CHAR_P (it2.c))
22724 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22725 }
22726
22727 it2.glyph_row = NULL;
22728 it2.what = IT_CHARACTER;
22729 x_produce_glyphs (&it2);
22730 width = NUMVAL (prop) * it2.pixel_width;
22731 }
22732 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22733 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22734 {
22735 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22736 align_to = (align_to < 0
22737 ? 0
22738 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22739 else if (align_to < 0)
22740 align_to = window_box_left_offset (it->w, TEXT_AREA);
22741 width = max (0, (int)tem + align_to - it->current_x);
22742 zero_width_ok_p = 1;
22743 }
22744 else
22745 /* Nothing specified -> width defaults to canonical char width. */
22746 width = FRAME_COLUMN_WIDTH (it->f);
22747
22748 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22749 width = 1;
22750
22751 /* Compute height. */
22752 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22753 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22754 {
22755 height = (int)tem;
22756 zero_height_ok_p = 1;
22757 }
22758 else if (prop = Fplist_get (plist, QCrelative_height),
22759 NUMVAL (prop) > 0)
22760 height = FONT_HEIGHT (font) * NUMVAL (prop);
22761 else
22762 height = FONT_HEIGHT (font);
22763
22764 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22765 height = 1;
22766
22767 /* Compute percentage of height used for ascent. If
22768 `:ascent ASCENT' is present and valid, use that. Otherwise,
22769 derive the ascent from the font in use. */
22770 if (prop = Fplist_get (plist, QCascent),
22771 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22772 ascent = height * NUMVAL (prop) / 100.0;
22773 else if (!NILP (prop)
22774 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22775 ascent = min (max (0, (int)tem), height);
22776 else
22777 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22778
22779 if (width > 0 && it->line_wrap != TRUNCATE
22780 && it->current_x + width > it->last_visible_x)
22781 width = it->last_visible_x - it->current_x - 1;
22782
22783 if (width > 0 && height > 0 && it->glyph_row)
22784 {
22785 Lisp_Object object = it->stack[it->sp - 1].string;
22786 if (!STRINGP (object))
22787 object = it->w->buffer;
22788 append_stretch_glyph (it, object, width, height, ascent);
22789 }
22790
22791 it->pixel_width = width;
22792 it->ascent = it->phys_ascent = ascent;
22793 it->descent = it->phys_descent = height - it->ascent;
22794 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22795
22796 take_vertical_position_into_account (it);
22797 }
22798
22799 /* Calculate line-height and line-spacing properties.
22800 An integer value specifies explicit pixel value.
22801 A float value specifies relative value to current face height.
22802 A cons (float . face-name) specifies relative value to
22803 height of specified face font.
22804
22805 Returns height in pixels, or nil. */
22806
22807
22808 static Lisp_Object
22809 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22810 int boff, int override)
22811 {
22812 Lisp_Object face_name = Qnil;
22813 int ascent, descent, height;
22814
22815 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22816 return val;
22817
22818 if (CONSP (val))
22819 {
22820 face_name = XCAR (val);
22821 val = XCDR (val);
22822 if (!NUMBERP (val))
22823 val = make_number (1);
22824 if (NILP (face_name))
22825 {
22826 height = it->ascent + it->descent;
22827 goto scale;
22828 }
22829 }
22830
22831 if (NILP (face_name))
22832 {
22833 font = FRAME_FONT (it->f);
22834 boff = FRAME_BASELINE_OFFSET (it->f);
22835 }
22836 else if (EQ (face_name, Qt))
22837 {
22838 override = 0;
22839 }
22840 else
22841 {
22842 int face_id;
22843 struct face *face;
22844
22845 face_id = lookup_named_face (it->f, face_name, 0);
22846 if (face_id < 0)
22847 return make_number (-1);
22848
22849 face = FACE_FROM_ID (it->f, face_id);
22850 font = face->font;
22851 if (font == NULL)
22852 return make_number (-1);
22853 boff = font->baseline_offset;
22854 if (font->vertical_centering)
22855 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22856 }
22857
22858 ascent = FONT_BASE (font) + boff;
22859 descent = FONT_DESCENT (font) - boff;
22860
22861 if (override)
22862 {
22863 it->override_ascent = ascent;
22864 it->override_descent = descent;
22865 it->override_boff = boff;
22866 }
22867
22868 height = ascent + descent;
22869
22870 scale:
22871 if (FLOATP (val))
22872 height = (int)(XFLOAT_DATA (val) * height);
22873 else if (INTEGERP (val))
22874 height *= XINT (val);
22875
22876 return make_number (height);
22877 }
22878
22879
22880 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
22881 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
22882 and only if this is for a character for which no font was found.
22883
22884 If the display method (it->glyphless_method) is
22885 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
22886 length of the acronym or the hexadecimal string, UPPER_XOFF and
22887 UPPER_YOFF are pixel offsets for the upper part of the string,
22888 LOWER_XOFF and LOWER_YOFF are for the lower part.
22889
22890 For the other display methods, LEN through LOWER_YOFF are zero. */
22891
22892 static void
22893 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
22894 short upper_xoff, short upper_yoff,
22895 short lower_xoff, short lower_yoff)
22896 {
22897 struct glyph *glyph;
22898 enum glyph_row_area area = it->area;
22899
22900 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22901 if (glyph < it->glyph_row->glyphs[area + 1])
22902 {
22903 /* If the glyph row is reversed, we need to prepend the glyph
22904 rather than append it. */
22905 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22906 {
22907 struct glyph *g;
22908
22909 /* Make room for the additional glyph. */
22910 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22911 g[1] = *g;
22912 glyph = it->glyph_row->glyphs[area];
22913 }
22914 glyph->charpos = CHARPOS (it->position);
22915 glyph->object = it->object;
22916 glyph->pixel_width = it->pixel_width;
22917 glyph->ascent = it->ascent;
22918 glyph->descent = it->descent;
22919 glyph->voffset = it->voffset;
22920 glyph->type = GLYPHLESS_GLYPH;
22921 glyph->u.glyphless.method = it->glyphless_method;
22922 glyph->u.glyphless.for_no_font = for_no_font;
22923 glyph->u.glyphless.len = len;
22924 glyph->u.glyphless.ch = it->c;
22925 glyph->slice.glyphless.upper_xoff = upper_xoff;
22926 glyph->slice.glyphless.upper_yoff = upper_yoff;
22927 glyph->slice.glyphless.lower_xoff = lower_xoff;
22928 glyph->slice.glyphless.lower_yoff = lower_yoff;
22929 glyph->avoid_cursor_p = it->avoid_cursor_p;
22930 glyph->multibyte_p = it->multibyte_p;
22931 glyph->left_box_line_p = it->start_of_box_run_p;
22932 glyph->right_box_line_p = it->end_of_box_run_p;
22933 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22934 || it->phys_descent > it->descent);
22935 glyph->padding_p = 0;
22936 glyph->glyph_not_available_p = 0;
22937 glyph->face_id = face_id;
22938 glyph->font_type = FONT_TYPE_UNKNOWN;
22939 if (it->bidi_p)
22940 {
22941 glyph->resolved_level = it->bidi_it.resolved_level;
22942 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22943 abort ();
22944 glyph->bidi_type = it->bidi_it.type;
22945 }
22946 ++it->glyph_row->used[area];
22947 }
22948 else
22949 IT_EXPAND_MATRIX_WIDTH (it, area);
22950 }
22951
22952
22953 /* Produce a glyph for a glyphless character for iterator IT.
22954 IT->glyphless_method specifies which method to use for displaying
22955 the character. See the description of enum
22956 glyphless_display_method in dispextern.h for the detail.
22957
22958 FOR_NO_FONT is nonzero if and only if this is for a character for
22959 which no font was found. ACRONYM, if non-nil, is an acronym string
22960 for the character. */
22961
22962 static void
22963 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
22964 {
22965 int face_id;
22966 struct face *face;
22967 struct font *font;
22968 int base_width, base_height, width, height;
22969 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
22970 int len;
22971
22972 /* Get the metrics of the base font. We always refer to the current
22973 ASCII face. */
22974 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
22975 font = face->font ? face->font : FRAME_FONT (it->f);
22976 it->ascent = FONT_BASE (font) + font->baseline_offset;
22977 it->descent = FONT_DESCENT (font) - font->baseline_offset;
22978 base_height = it->ascent + it->descent;
22979 base_width = font->average_width;
22980
22981 /* Get a face ID for the glyph by utilizing a cache (the same way as
22982 doen for `escape-glyph' in get_next_display_element). */
22983 if (it->f == last_glyphless_glyph_frame
22984 && it->face_id == last_glyphless_glyph_face_id)
22985 {
22986 face_id = last_glyphless_glyph_merged_face_id;
22987 }
22988 else
22989 {
22990 /* Merge the `glyphless-char' face into the current face. */
22991 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
22992 last_glyphless_glyph_frame = it->f;
22993 last_glyphless_glyph_face_id = it->face_id;
22994 last_glyphless_glyph_merged_face_id = face_id;
22995 }
22996
22997 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
22998 {
22999 it->pixel_width = THIN_SPACE_WIDTH;
23000 len = 0;
23001 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23002 }
23003 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23004 {
23005 width = CHAR_WIDTH (it->c);
23006 if (width == 0)
23007 width = 1;
23008 else if (width > 4)
23009 width = 4;
23010 it->pixel_width = base_width * width;
23011 len = 0;
23012 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23013 }
23014 else
23015 {
23016 char buf[7];
23017 const char *str;
23018 unsigned int code[6];
23019 int upper_len;
23020 int ascent, descent;
23021 struct font_metrics metrics_upper, metrics_lower;
23022
23023 face = FACE_FROM_ID (it->f, face_id);
23024 font = face->font ? face->font : FRAME_FONT (it->f);
23025 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23026
23027 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23028 {
23029 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23030 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23031 if (CONSP (acronym))
23032 acronym = XCAR (acronym);
23033 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23034 }
23035 else
23036 {
23037 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23038 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23039 str = buf;
23040 }
23041 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23042 code[len] = font->driver->encode_char (font, str[len]);
23043 upper_len = (len + 1) / 2;
23044 font->driver->text_extents (font, code, upper_len,
23045 &metrics_upper);
23046 font->driver->text_extents (font, code + upper_len, len - upper_len,
23047 &metrics_lower);
23048
23049
23050
23051 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23052 width = max (metrics_upper.width, metrics_lower.width) + 4;
23053 upper_xoff = upper_yoff = 2; /* the typical case */
23054 if (base_width >= width)
23055 {
23056 /* Align the upper to the left, the lower to the right. */
23057 it->pixel_width = base_width;
23058 lower_xoff = base_width - 2 - metrics_lower.width;
23059 }
23060 else
23061 {
23062 /* Center the shorter one. */
23063 it->pixel_width = width;
23064 if (metrics_upper.width >= metrics_lower.width)
23065 lower_xoff = (width - metrics_lower.width) / 2;
23066 else
23067 {
23068 /* FIXME: This code doesn't look right. It formerly was
23069 missing the "lower_xoff = 0;", which couldn't have
23070 been right since it left lower_xoff uninitialized. */
23071 lower_xoff = 0;
23072 upper_xoff = (width - metrics_upper.width) / 2;
23073 }
23074 }
23075
23076 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23077 top, bottom, and between upper and lower strings. */
23078 height = (metrics_upper.ascent + metrics_upper.descent
23079 + metrics_lower.ascent + metrics_lower.descent) + 5;
23080 /* Center vertically.
23081 H:base_height, D:base_descent
23082 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23083
23084 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23085 descent = D - H/2 + h/2;
23086 lower_yoff = descent - 2 - ld;
23087 upper_yoff = lower_yoff - la - 1 - ud; */
23088 ascent = - (it->descent - (base_height + height + 1) / 2);
23089 descent = it->descent - (base_height - height) / 2;
23090 lower_yoff = descent - 2 - metrics_lower.descent;
23091 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23092 - metrics_upper.descent);
23093 /* Don't make the height shorter than the base height. */
23094 if (height > base_height)
23095 {
23096 it->ascent = ascent;
23097 it->descent = descent;
23098 }
23099 }
23100
23101 it->phys_ascent = it->ascent;
23102 it->phys_descent = it->descent;
23103 if (it->glyph_row)
23104 append_glyphless_glyph (it, face_id, for_no_font, len,
23105 upper_xoff, upper_yoff,
23106 lower_xoff, lower_yoff);
23107 it->nglyphs = 1;
23108 take_vertical_position_into_account (it);
23109 }
23110
23111
23112 /* RIF:
23113 Produce glyphs/get display metrics for the display element IT is
23114 loaded with. See the description of struct it in dispextern.h
23115 for an overview of struct it. */
23116
23117 void
23118 x_produce_glyphs (struct it *it)
23119 {
23120 int extra_line_spacing = it->extra_line_spacing;
23121
23122 it->glyph_not_available_p = 0;
23123
23124 if (it->what == IT_CHARACTER)
23125 {
23126 XChar2b char2b;
23127 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23128 struct font *font = face->font;
23129 struct font_metrics *pcm = NULL;
23130 int boff; /* baseline offset */
23131
23132 if (font == NULL)
23133 {
23134 /* When no suitable font is found, display this character by
23135 the method specified in the first extra slot of
23136 Vglyphless_char_display. */
23137 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23138
23139 xassert (it->what == IT_GLYPHLESS);
23140 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23141 goto done;
23142 }
23143
23144 boff = font->baseline_offset;
23145 if (font->vertical_centering)
23146 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23147
23148 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23149 {
23150 int stretched_p;
23151
23152 it->nglyphs = 1;
23153
23154 if (it->override_ascent >= 0)
23155 {
23156 it->ascent = it->override_ascent;
23157 it->descent = it->override_descent;
23158 boff = it->override_boff;
23159 }
23160 else
23161 {
23162 it->ascent = FONT_BASE (font) + boff;
23163 it->descent = FONT_DESCENT (font) - boff;
23164 }
23165
23166 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23167 {
23168 pcm = get_per_char_metric (font, &char2b);
23169 if (pcm->width == 0
23170 && pcm->rbearing == 0 && pcm->lbearing == 0)
23171 pcm = NULL;
23172 }
23173
23174 if (pcm)
23175 {
23176 it->phys_ascent = pcm->ascent + boff;
23177 it->phys_descent = pcm->descent - boff;
23178 it->pixel_width = pcm->width;
23179 }
23180 else
23181 {
23182 it->glyph_not_available_p = 1;
23183 it->phys_ascent = it->ascent;
23184 it->phys_descent = it->descent;
23185 it->pixel_width = font->space_width;
23186 }
23187
23188 if (it->constrain_row_ascent_descent_p)
23189 {
23190 if (it->descent > it->max_descent)
23191 {
23192 it->ascent += it->descent - it->max_descent;
23193 it->descent = it->max_descent;
23194 }
23195 if (it->ascent > it->max_ascent)
23196 {
23197 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23198 it->ascent = it->max_ascent;
23199 }
23200 it->phys_ascent = min (it->phys_ascent, it->ascent);
23201 it->phys_descent = min (it->phys_descent, it->descent);
23202 extra_line_spacing = 0;
23203 }
23204
23205 /* If this is a space inside a region of text with
23206 `space-width' property, change its width. */
23207 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23208 if (stretched_p)
23209 it->pixel_width *= XFLOATINT (it->space_width);
23210
23211 /* If face has a box, add the box thickness to the character
23212 height. If character has a box line to the left and/or
23213 right, add the box line width to the character's width. */
23214 if (face->box != FACE_NO_BOX)
23215 {
23216 int thick = face->box_line_width;
23217
23218 if (thick > 0)
23219 {
23220 it->ascent += thick;
23221 it->descent += thick;
23222 }
23223 else
23224 thick = -thick;
23225
23226 if (it->start_of_box_run_p)
23227 it->pixel_width += thick;
23228 if (it->end_of_box_run_p)
23229 it->pixel_width += thick;
23230 }
23231
23232 /* If face has an overline, add the height of the overline
23233 (1 pixel) and a 1 pixel margin to the character height. */
23234 if (face->overline_p)
23235 it->ascent += overline_margin;
23236
23237 if (it->constrain_row_ascent_descent_p)
23238 {
23239 if (it->ascent > it->max_ascent)
23240 it->ascent = it->max_ascent;
23241 if (it->descent > it->max_descent)
23242 it->descent = it->max_descent;
23243 }
23244
23245 take_vertical_position_into_account (it);
23246
23247 /* If we have to actually produce glyphs, do it. */
23248 if (it->glyph_row)
23249 {
23250 if (stretched_p)
23251 {
23252 /* Translate a space with a `space-width' property
23253 into a stretch glyph. */
23254 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23255 / FONT_HEIGHT (font));
23256 append_stretch_glyph (it, it->object, it->pixel_width,
23257 it->ascent + it->descent, ascent);
23258 }
23259 else
23260 append_glyph (it);
23261
23262 /* If characters with lbearing or rbearing are displayed
23263 in this line, record that fact in a flag of the
23264 glyph row. This is used to optimize X output code. */
23265 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23266 it->glyph_row->contains_overlapping_glyphs_p = 1;
23267 }
23268 if (! stretched_p && it->pixel_width == 0)
23269 /* We assure that all visible glyphs have at least 1-pixel
23270 width. */
23271 it->pixel_width = 1;
23272 }
23273 else if (it->char_to_display == '\n')
23274 {
23275 /* A newline has no width, but we need the height of the
23276 line. But if previous part of the line sets a height,
23277 don't increase that height */
23278
23279 Lisp_Object height;
23280 Lisp_Object total_height = Qnil;
23281
23282 it->override_ascent = -1;
23283 it->pixel_width = 0;
23284 it->nglyphs = 0;
23285
23286 height = get_it_property (it, Qline_height);
23287 /* Split (line-height total-height) list */
23288 if (CONSP (height)
23289 && CONSP (XCDR (height))
23290 && NILP (XCDR (XCDR (height))))
23291 {
23292 total_height = XCAR (XCDR (height));
23293 height = XCAR (height);
23294 }
23295 height = calc_line_height_property (it, height, font, boff, 1);
23296
23297 if (it->override_ascent >= 0)
23298 {
23299 it->ascent = it->override_ascent;
23300 it->descent = it->override_descent;
23301 boff = it->override_boff;
23302 }
23303 else
23304 {
23305 it->ascent = FONT_BASE (font) + boff;
23306 it->descent = FONT_DESCENT (font) - boff;
23307 }
23308
23309 if (EQ (height, Qt))
23310 {
23311 if (it->descent > it->max_descent)
23312 {
23313 it->ascent += it->descent - it->max_descent;
23314 it->descent = it->max_descent;
23315 }
23316 if (it->ascent > it->max_ascent)
23317 {
23318 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23319 it->ascent = it->max_ascent;
23320 }
23321 it->phys_ascent = min (it->phys_ascent, it->ascent);
23322 it->phys_descent = min (it->phys_descent, it->descent);
23323 it->constrain_row_ascent_descent_p = 1;
23324 extra_line_spacing = 0;
23325 }
23326 else
23327 {
23328 Lisp_Object spacing;
23329
23330 it->phys_ascent = it->ascent;
23331 it->phys_descent = it->descent;
23332
23333 if ((it->max_ascent > 0 || it->max_descent > 0)
23334 && face->box != FACE_NO_BOX
23335 && face->box_line_width > 0)
23336 {
23337 it->ascent += face->box_line_width;
23338 it->descent += face->box_line_width;
23339 }
23340 if (!NILP (height)
23341 && XINT (height) > it->ascent + it->descent)
23342 it->ascent = XINT (height) - it->descent;
23343
23344 if (!NILP (total_height))
23345 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23346 else
23347 {
23348 spacing = get_it_property (it, Qline_spacing);
23349 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23350 }
23351 if (INTEGERP (spacing))
23352 {
23353 extra_line_spacing = XINT (spacing);
23354 if (!NILP (total_height))
23355 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23356 }
23357 }
23358 }
23359 else /* i.e. (it->char_to_display == '\t') */
23360 {
23361 if (font->space_width > 0)
23362 {
23363 int tab_width = it->tab_width * font->space_width;
23364 int x = it->current_x + it->continuation_lines_width;
23365 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23366
23367 /* If the distance from the current position to the next tab
23368 stop is less than a space character width, use the
23369 tab stop after that. */
23370 if (next_tab_x - x < font->space_width)
23371 next_tab_x += tab_width;
23372
23373 it->pixel_width = next_tab_x - x;
23374 it->nglyphs = 1;
23375 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23376 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23377
23378 if (it->glyph_row)
23379 {
23380 append_stretch_glyph (it, it->object, it->pixel_width,
23381 it->ascent + it->descent, it->ascent);
23382 }
23383 }
23384 else
23385 {
23386 it->pixel_width = 0;
23387 it->nglyphs = 1;
23388 }
23389 }
23390 }
23391 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23392 {
23393 /* A static composition.
23394
23395 Note: A composition is represented as one glyph in the
23396 glyph matrix. There are no padding glyphs.
23397
23398 Important note: pixel_width, ascent, and descent are the
23399 values of what is drawn by draw_glyphs (i.e. the values of
23400 the overall glyphs composed). */
23401 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23402 int boff; /* baseline offset */
23403 struct composition *cmp = composition_table[it->cmp_it.id];
23404 int glyph_len = cmp->glyph_len;
23405 struct font *font = face->font;
23406
23407 it->nglyphs = 1;
23408
23409 /* If we have not yet calculated pixel size data of glyphs of
23410 the composition for the current face font, calculate them
23411 now. Theoretically, we have to check all fonts for the
23412 glyphs, but that requires much time and memory space. So,
23413 here we check only the font of the first glyph. This may
23414 lead to incorrect display, but it's very rare, and C-l
23415 (recenter-top-bottom) can correct the display anyway. */
23416 if (! cmp->font || cmp->font != font)
23417 {
23418 /* Ascent and descent of the font of the first character
23419 of this composition (adjusted by baseline offset).
23420 Ascent and descent of overall glyphs should not be less
23421 than these, respectively. */
23422 int font_ascent, font_descent, font_height;
23423 /* Bounding box of the overall glyphs. */
23424 int leftmost, rightmost, lowest, highest;
23425 int lbearing, rbearing;
23426 int i, width, ascent, descent;
23427 int left_padded = 0, right_padded = 0;
23428 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23429 XChar2b char2b;
23430 struct font_metrics *pcm;
23431 int font_not_found_p;
23432 EMACS_INT pos;
23433
23434 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23435 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23436 break;
23437 if (glyph_len < cmp->glyph_len)
23438 right_padded = 1;
23439 for (i = 0; i < glyph_len; i++)
23440 {
23441 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23442 break;
23443 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23444 }
23445 if (i > 0)
23446 left_padded = 1;
23447
23448 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23449 : IT_CHARPOS (*it));
23450 /* If no suitable font is found, use the default font. */
23451 font_not_found_p = font == NULL;
23452 if (font_not_found_p)
23453 {
23454 face = face->ascii_face;
23455 font = face->font;
23456 }
23457 boff = font->baseline_offset;
23458 if (font->vertical_centering)
23459 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23460 font_ascent = FONT_BASE (font) + boff;
23461 font_descent = FONT_DESCENT (font) - boff;
23462 font_height = FONT_HEIGHT (font);
23463
23464 cmp->font = (void *) font;
23465
23466 pcm = NULL;
23467 if (! font_not_found_p)
23468 {
23469 get_char_face_and_encoding (it->f, c, it->face_id,
23470 &char2b, 0);
23471 pcm = get_per_char_metric (font, &char2b);
23472 }
23473
23474 /* Initialize the bounding box. */
23475 if (pcm)
23476 {
23477 width = pcm->width;
23478 ascent = pcm->ascent;
23479 descent = pcm->descent;
23480 lbearing = pcm->lbearing;
23481 rbearing = pcm->rbearing;
23482 }
23483 else
23484 {
23485 width = font->space_width;
23486 ascent = FONT_BASE (font);
23487 descent = FONT_DESCENT (font);
23488 lbearing = 0;
23489 rbearing = width;
23490 }
23491
23492 rightmost = width;
23493 leftmost = 0;
23494 lowest = - descent + boff;
23495 highest = ascent + boff;
23496
23497 if (! font_not_found_p
23498 && font->default_ascent
23499 && CHAR_TABLE_P (Vuse_default_ascent)
23500 && !NILP (Faref (Vuse_default_ascent,
23501 make_number (it->char_to_display))))
23502 highest = font->default_ascent + boff;
23503
23504 /* Draw the first glyph at the normal position. It may be
23505 shifted to right later if some other glyphs are drawn
23506 at the left. */
23507 cmp->offsets[i * 2] = 0;
23508 cmp->offsets[i * 2 + 1] = boff;
23509 cmp->lbearing = lbearing;
23510 cmp->rbearing = rbearing;
23511
23512 /* Set cmp->offsets for the remaining glyphs. */
23513 for (i++; i < glyph_len; i++)
23514 {
23515 int left, right, btm, top;
23516 int ch = COMPOSITION_GLYPH (cmp, i);
23517 int face_id;
23518 struct face *this_face;
23519
23520 if (ch == '\t')
23521 ch = ' ';
23522 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23523 this_face = FACE_FROM_ID (it->f, face_id);
23524 font = this_face->font;
23525
23526 if (font == NULL)
23527 pcm = NULL;
23528 else
23529 {
23530 get_char_face_and_encoding (it->f, ch, face_id,
23531 &char2b, 0);
23532 pcm = get_per_char_metric (font, &char2b);
23533 }
23534 if (! pcm)
23535 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23536 else
23537 {
23538 width = pcm->width;
23539 ascent = pcm->ascent;
23540 descent = pcm->descent;
23541 lbearing = pcm->lbearing;
23542 rbearing = pcm->rbearing;
23543 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23544 {
23545 /* Relative composition with or without
23546 alternate chars. */
23547 left = (leftmost + rightmost - width) / 2;
23548 btm = - descent + boff;
23549 if (font->relative_compose
23550 && (! CHAR_TABLE_P (Vignore_relative_composition)
23551 || NILP (Faref (Vignore_relative_composition,
23552 make_number (ch)))))
23553 {
23554
23555 if (- descent >= font->relative_compose)
23556 /* One extra pixel between two glyphs. */
23557 btm = highest + 1;
23558 else if (ascent <= 0)
23559 /* One extra pixel between two glyphs. */
23560 btm = lowest - 1 - ascent - descent;
23561 }
23562 }
23563 else
23564 {
23565 /* A composition rule is specified by an integer
23566 value that encodes global and new reference
23567 points (GREF and NREF). GREF and NREF are
23568 specified by numbers as below:
23569
23570 0---1---2 -- ascent
23571 | |
23572 | |
23573 | |
23574 9--10--11 -- center
23575 | |
23576 ---3---4---5--- baseline
23577 | |
23578 6---7---8 -- descent
23579 */
23580 int rule = COMPOSITION_RULE (cmp, i);
23581 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23582
23583 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23584 grefx = gref % 3, nrefx = nref % 3;
23585 grefy = gref / 3, nrefy = nref / 3;
23586 if (xoff)
23587 xoff = font_height * (xoff - 128) / 256;
23588 if (yoff)
23589 yoff = font_height * (yoff - 128) / 256;
23590
23591 left = (leftmost
23592 + grefx * (rightmost - leftmost) / 2
23593 - nrefx * width / 2
23594 + xoff);
23595
23596 btm = ((grefy == 0 ? highest
23597 : grefy == 1 ? 0
23598 : grefy == 2 ? lowest
23599 : (highest + lowest) / 2)
23600 - (nrefy == 0 ? ascent + descent
23601 : nrefy == 1 ? descent - boff
23602 : nrefy == 2 ? 0
23603 : (ascent + descent) / 2)
23604 + yoff);
23605 }
23606
23607 cmp->offsets[i * 2] = left;
23608 cmp->offsets[i * 2 + 1] = btm + descent;
23609
23610 /* Update the bounding box of the overall glyphs. */
23611 if (width > 0)
23612 {
23613 right = left + width;
23614 if (left < leftmost)
23615 leftmost = left;
23616 if (right > rightmost)
23617 rightmost = right;
23618 }
23619 top = btm + descent + ascent;
23620 if (top > highest)
23621 highest = top;
23622 if (btm < lowest)
23623 lowest = btm;
23624
23625 if (cmp->lbearing > left + lbearing)
23626 cmp->lbearing = left + lbearing;
23627 if (cmp->rbearing < left + rbearing)
23628 cmp->rbearing = left + rbearing;
23629 }
23630 }
23631
23632 /* If there are glyphs whose x-offsets are negative,
23633 shift all glyphs to the right and make all x-offsets
23634 non-negative. */
23635 if (leftmost < 0)
23636 {
23637 for (i = 0; i < cmp->glyph_len; i++)
23638 cmp->offsets[i * 2] -= leftmost;
23639 rightmost -= leftmost;
23640 cmp->lbearing -= leftmost;
23641 cmp->rbearing -= leftmost;
23642 }
23643
23644 if (left_padded && cmp->lbearing < 0)
23645 {
23646 for (i = 0; i < cmp->glyph_len; i++)
23647 cmp->offsets[i * 2] -= cmp->lbearing;
23648 rightmost -= cmp->lbearing;
23649 cmp->rbearing -= cmp->lbearing;
23650 cmp->lbearing = 0;
23651 }
23652 if (right_padded && rightmost < cmp->rbearing)
23653 {
23654 rightmost = cmp->rbearing;
23655 }
23656
23657 cmp->pixel_width = rightmost;
23658 cmp->ascent = highest;
23659 cmp->descent = - lowest;
23660 if (cmp->ascent < font_ascent)
23661 cmp->ascent = font_ascent;
23662 if (cmp->descent < font_descent)
23663 cmp->descent = font_descent;
23664 }
23665
23666 if (it->glyph_row
23667 && (cmp->lbearing < 0
23668 || cmp->rbearing > cmp->pixel_width))
23669 it->glyph_row->contains_overlapping_glyphs_p = 1;
23670
23671 it->pixel_width = cmp->pixel_width;
23672 it->ascent = it->phys_ascent = cmp->ascent;
23673 it->descent = it->phys_descent = cmp->descent;
23674 if (face->box != FACE_NO_BOX)
23675 {
23676 int thick = face->box_line_width;
23677
23678 if (thick > 0)
23679 {
23680 it->ascent += thick;
23681 it->descent += thick;
23682 }
23683 else
23684 thick = - thick;
23685
23686 if (it->start_of_box_run_p)
23687 it->pixel_width += thick;
23688 if (it->end_of_box_run_p)
23689 it->pixel_width += thick;
23690 }
23691
23692 /* If face has an overline, add the height of the overline
23693 (1 pixel) and a 1 pixel margin to the character height. */
23694 if (face->overline_p)
23695 it->ascent += overline_margin;
23696
23697 take_vertical_position_into_account (it);
23698 if (it->ascent < 0)
23699 it->ascent = 0;
23700 if (it->descent < 0)
23701 it->descent = 0;
23702
23703 if (it->glyph_row)
23704 append_composite_glyph (it);
23705 }
23706 else if (it->what == IT_COMPOSITION)
23707 {
23708 /* A dynamic (automatic) composition. */
23709 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23710 Lisp_Object gstring;
23711 struct font_metrics metrics;
23712
23713 gstring = composition_gstring_from_id (it->cmp_it.id);
23714 it->pixel_width
23715 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23716 &metrics);
23717 if (it->glyph_row
23718 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23719 it->glyph_row->contains_overlapping_glyphs_p = 1;
23720 it->ascent = it->phys_ascent = metrics.ascent;
23721 it->descent = it->phys_descent = metrics.descent;
23722 if (face->box != FACE_NO_BOX)
23723 {
23724 int thick = face->box_line_width;
23725
23726 if (thick > 0)
23727 {
23728 it->ascent += thick;
23729 it->descent += thick;
23730 }
23731 else
23732 thick = - thick;
23733
23734 if (it->start_of_box_run_p)
23735 it->pixel_width += thick;
23736 if (it->end_of_box_run_p)
23737 it->pixel_width += thick;
23738 }
23739 /* If face has an overline, add the height of the overline
23740 (1 pixel) and a 1 pixel margin to the character height. */
23741 if (face->overline_p)
23742 it->ascent += overline_margin;
23743 take_vertical_position_into_account (it);
23744 if (it->ascent < 0)
23745 it->ascent = 0;
23746 if (it->descent < 0)
23747 it->descent = 0;
23748
23749 if (it->glyph_row)
23750 append_composite_glyph (it);
23751 }
23752 else if (it->what == IT_GLYPHLESS)
23753 produce_glyphless_glyph (it, 0, Qnil);
23754 else if (it->what == IT_IMAGE)
23755 produce_image_glyph (it);
23756 else if (it->what == IT_STRETCH)
23757 produce_stretch_glyph (it);
23758
23759 done:
23760 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23761 because this isn't true for images with `:ascent 100'. */
23762 xassert (it->ascent >= 0 && it->descent >= 0);
23763 if (it->area == TEXT_AREA)
23764 it->current_x += it->pixel_width;
23765
23766 if (extra_line_spacing > 0)
23767 {
23768 it->descent += extra_line_spacing;
23769 if (extra_line_spacing > it->max_extra_line_spacing)
23770 it->max_extra_line_spacing = extra_line_spacing;
23771 }
23772
23773 it->max_ascent = max (it->max_ascent, it->ascent);
23774 it->max_descent = max (it->max_descent, it->descent);
23775 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23776 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23777 }
23778
23779 /* EXPORT for RIF:
23780 Output LEN glyphs starting at START at the nominal cursor position.
23781 Advance the nominal cursor over the text. The global variable
23782 updated_window contains the window being updated, updated_row is
23783 the glyph row being updated, and updated_area is the area of that
23784 row being updated. */
23785
23786 void
23787 x_write_glyphs (struct glyph *start, int len)
23788 {
23789 int x, hpos;
23790
23791 xassert (updated_window && updated_row);
23792 BLOCK_INPUT;
23793
23794 /* Write glyphs. */
23795
23796 hpos = start - updated_row->glyphs[updated_area];
23797 x = draw_glyphs (updated_window, output_cursor.x,
23798 updated_row, updated_area,
23799 hpos, hpos + len,
23800 DRAW_NORMAL_TEXT, 0);
23801
23802 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23803 if (updated_area == TEXT_AREA
23804 && updated_window->phys_cursor_on_p
23805 && updated_window->phys_cursor.vpos == output_cursor.vpos
23806 && updated_window->phys_cursor.hpos >= hpos
23807 && updated_window->phys_cursor.hpos < hpos + len)
23808 updated_window->phys_cursor_on_p = 0;
23809
23810 UNBLOCK_INPUT;
23811
23812 /* Advance the output cursor. */
23813 output_cursor.hpos += len;
23814 output_cursor.x = x;
23815 }
23816
23817
23818 /* EXPORT for RIF:
23819 Insert LEN glyphs from START at the nominal cursor position. */
23820
23821 void
23822 x_insert_glyphs (struct glyph *start, int len)
23823 {
23824 struct frame *f;
23825 struct window *w;
23826 int line_height, shift_by_width, shifted_region_width;
23827 struct glyph_row *row;
23828 struct glyph *glyph;
23829 int frame_x, frame_y;
23830 EMACS_INT hpos;
23831
23832 xassert (updated_window && updated_row);
23833 BLOCK_INPUT;
23834 w = updated_window;
23835 f = XFRAME (WINDOW_FRAME (w));
23836
23837 /* Get the height of the line we are in. */
23838 row = updated_row;
23839 line_height = row->height;
23840
23841 /* Get the width of the glyphs to insert. */
23842 shift_by_width = 0;
23843 for (glyph = start; glyph < start + len; ++glyph)
23844 shift_by_width += glyph->pixel_width;
23845
23846 /* Get the width of the region to shift right. */
23847 shifted_region_width = (window_box_width (w, updated_area)
23848 - output_cursor.x
23849 - shift_by_width);
23850
23851 /* Shift right. */
23852 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23853 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23854
23855 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23856 line_height, shift_by_width);
23857
23858 /* Write the glyphs. */
23859 hpos = start - row->glyphs[updated_area];
23860 draw_glyphs (w, output_cursor.x, row, updated_area,
23861 hpos, hpos + len,
23862 DRAW_NORMAL_TEXT, 0);
23863
23864 /* Advance the output cursor. */
23865 output_cursor.hpos += len;
23866 output_cursor.x += shift_by_width;
23867 UNBLOCK_INPUT;
23868 }
23869
23870
23871 /* EXPORT for RIF:
23872 Erase the current text line from the nominal cursor position
23873 (inclusive) to pixel column TO_X (exclusive). The idea is that
23874 everything from TO_X onward is already erased.
23875
23876 TO_X is a pixel position relative to updated_area of
23877 updated_window. TO_X == -1 means clear to the end of this area. */
23878
23879 void
23880 x_clear_end_of_line (int to_x)
23881 {
23882 struct frame *f;
23883 struct window *w = updated_window;
23884 int max_x, min_y, max_y;
23885 int from_x, from_y, to_y;
23886
23887 xassert (updated_window && updated_row);
23888 f = XFRAME (w->frame);
23889
23890 if (updated_row->full_width_p)
23891 max_x = WINDOW_TOTAL_WIDTH (w);
23892 else
23893 max_x = window_box_width (w, updated_area);
23894 max_y = window_text_bottom_y (w);
23895
23896 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
23897 of window. For TO_X > 0, truncate to end of drawing area. */
23898 if (to_x == 0)
23899 return;
23900 else if (to_x < 0)
23901 to_x = max_x;
23902 else
23903 to_x = min (to_x, max_x);
23904
23905 to_y = min (max_y, output_cursor.y + updated_row->height);
23906
23907 /* Notice if the cursor will be cleared by this operation. */
23908 if (!updated_row->full_width_p)
23909 notice_overwritten_cursor (w, updated_area,
23910 output_cursor.x, -1,
23911 updated_row->y,
23912 MATRIX_ROW_BOTTOM_Y (updated_row));
23913
23914 from_x = output_cursor.x;
23915
23916 /* Translate to frame coordinates. */
23917 if (updated_row->full_width_p)
23918 {
23919 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
23920 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
23921 }
23922 else
23923 {
23924 int area_left = window_box_left (w, updated_area);
23925 from_x += area_left;
23926 to_x += area_left;
23927 }
23928
23929 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
23930 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
23931 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
23932
23933 /* Prevent inadvertently clearing to end of the X window. */
23934 if (to_x > from_x && to_y > from_y)
23935 {
23936 BLOCK_INPUT;
23937 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
23938 to_x - from_x, to_y - from_y);
23939 UNBLOCK_INPUT;
23940 }
23941 }
23942
23943 #endif /* HAVE_WINDOW_SYSTEM */
23944
23945
23946 \f
23947 /***********************************************************************
23948 Cursor types
23949 ***********************************************************************/
23950
23951 /* Value is the internal representation of the specified cursor type
23952 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
23953 of the bar cursor. */
23954
23955 static enum text_cursor_kinds
23956 get_specified_cursor_type (Lisp_Object arg, int *width)
23957 {
23958 enum text_cursor_kinds type;
23959
23960 if (NILP (arg))
23961 return NO_CURSOR;
23962
23963 if (EQ (arg, Qbox))
23964 return FILLED_BOX_CURSOR;
23965
23966 if (EQ (arg, Qhollow))
23967 return HOLLOW_BOX_CURSOR;
23968
23969 if (EQ (arg, Qbar))
23970 {
23971 *width = 2;
23972 return BAR_CURSOR;
23973 }
23974
23975 if (CONSP (arg)
23976 && EQ (XCAR (arg), Qbar)
23977 && INTEGERP (XCDR (arg))
23978 && XINT (XCDR (arg)) >= 0)
23979 {
23980 *width = XINT (XCDR (arg));
23981 return BAR_CURSOR;
23982 }
23983
23984 if (EQ (arg, Qhbar))
23985 {
23986 *width = 2;
23987 return HBAR_CURSOR;
23988 }
23989
23990 if (CONSP (arg)
23991 && EQ (XCAR (arg), Qhbar)
23992 && INTEGERP (XCDR (arg))
23993 && XINT (XCDR (arg)) >= 0)
23994 {
23995 *width = XINT (XCDR (arg));
23996 return HBAR_CURSOR;
23997 }
23998
23999 /* Treat anything unknown as "hollow box cursor".
24000 It was bad to signal an error; people have trouble fixing
24001 .Xdefaults with Emacs, when it has something bad in it. */
24002 type = HOLLOW_BOX_CURSOR;
24003
24004 return type;
24005 }
24006
24007 /* Set the default cursor types for specified frame. */
24008 void
24009 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24010 {
24011 int width = 1;
24012 Lisp_Object tem;
24013
24014 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24015 FRAME_CURSOR_WIDTH (f) = width;
24016
24017 /* By default, set up the blink-off state depending on the on-state. */
24018
24019 tem = Fassoc (arg, Vblink_cursor_alist);
24020 if (!NILP (tem))
24021 {
24022 FRAME_BLINK_OFF_CURSOR (f)
24023 = get_specified_cursor_type (XCDR (tem), &width);
24024 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24025 }
24026 else
24027 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24028 }
24029
24030
24031 #ifdef HAVE_WINDOW_SYSTEM
24032
24033 /* Return the cursor we want to be displayed in window W. Return
24034 width of bar/hbar cursor through WIDTH arg. Return with
24035 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24036 (i.e. if the `system caret' should track this cursor).
24037
24038 In a mini-buffer window, we want the cursor only to appear if we
24039 are reading input from this window. For the selected window, we
24040 want the cursor type given by the frame parameter or buffer local
24041 setting of cursor-type. If explicitly marked off, draw no cursor.
24042 In all other cases, we want a hollow box cursor. */
24043
24044 static enum text_cursor_kinds
24045 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24046 int *active_cursor)
24047 {
24048 struct frame *f = XFRAME (w->frame);
24049 struct buffer *b = XBUFFER (w->buffer);
24050 int cursor_type = DEFAULT_CURSOR;
24051 Lisp_Object alt_cursor;
24052 int non_selected = 0;
24053
24054 *active_cursor = 1;
24055
24056 /* Echo area */
24057 if (cursor_in_echo_area
24058 && FRAME_HAS_MINIBUF_P (f)
24059 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24060 {
24061 if (w == XWINDOW (echo_area_window))
24062 {
24063 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24064 {
24065 *width = FRAME_CURSOR_WIDTH (f);
24066 return FRAME_DESIRED_CURSOR (f);
24067 }
24068 else
24069 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24070 }
24071
24072 *active_cursor = 0;
24073 non_selected = 1;
24074 }
24075
24076 /* Detect a nonselected window or nonselected frame. */
24077 else if (w != XWINDOW (f->selected_window)
24078 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24079 {
24080 *active_cursor = 0;
24081
24082 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24083 return NO_CURSOR;
24084
24085 non_selected = 1;
24086 }
24087
24088 /* Never display a cursor in a window in which cursor-type is nil. */
24089 if (NILP (BVAR (b, cursor_type)))
24090 return NO_CURSOR;
24091
24092 /* Get the normal cursor type for this window. */
24093 if (EQ (BVAR (b, cursor_type), Qt))
24094 {
24095 cursor_type = FRAME_DESIRED_CURSOR (f);
24096 *width = FRAME_CURSOR_WIDTH (f);
24097 }
24098 else
24099 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24100
24101 /* Use cursor-in-non-selected-windows instead
24102 for non-selected window or frame. */
24103 if (non_selected)
24104 {
24105 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24106 if (!EQ (Qt, alt_cursor))
24107 return get_specified_cursor_type (alt_cursor, width);
24108 /* t means modify the normal cursor type. */
24109 if (cursor_type == FILLED_BOX_CURSOR)
24110 cursor_type = HOLLOW_BOX_CURSOR;
24111 else if (cursor_type == BAR_CURSOR && *width > 1)
24112 --*width;
24113 return cursor_type;
24114 }
24115
24116 /* Use normal cursor if not blinked off. */
24117 if (!w->cursor_off_p)
24118 {
24119 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24120 {
24121 if (cursor_type == FILLED_BOX_CURSOR)
24122 {
24123 /* Using a block cursor on large images can be very annoying.
24124 So use a hollow cursor for "large" images.
24125 If image is not transparent (no mask), also use hollow cursor. */
24126 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24127 if (img != NULL && IMAGEP (img->spec))
24128 {
24129 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24130 where N = size of default frame font size.
24131 This should cover most of the "tiny" icons people may use. */
24132 if (!img->mask
24133 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24134 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24135 cursor_type = HOLLOW_BOX_CURSOR;
24136 }
24137 }
24138 else if (cursor_type != NO_CURSOR)
24139 {
24140 /* Display current only supports BOX and HOLLOW cursors for images.
24141 So for now, unconditionally use a HOLLOW cursor when cursor is
24142 not a solid box cursor. */
24143 cursor_type = HOLLOW_BOX_CURSOR;
24144 }
24145 }
24146 return cursor_type;
24147 }
24148
24149 /* Cursor is blinked off, so determine how to "toggle" it. */
24150
24151 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24152 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24153 return get_specified_cursor_type (XCDR (alt_cursor), width);
24154
24155 /* Then see if frame has specified a specific blink off cursor type. */
24156 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24157 {
24158 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24159 return FRAME_BLINK_OFF_CURSOR (f);
24160 }
24161
24162 #if 0
24163 /* Some people liked having a permanently visible blinking cursor,
24164 while others had very strong opinions against it. So it was
24165 decided to remove it. KFS 2003-09-03 */
24166
24167 /* Finally perform built-in cursor blinking:
24168 filled box <-> hollow box
24169 wide [h]bar <-> narrow [h]bar
24170 narrow [h]bar <-> no cursor
24171 other type <-> no cursor */
24172
24173 if (cursor_type == FILLED_BOX_CURSOR)
24174 return HOLLOW_BOX_CURSOR;
24175
24176 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24177 {
24178 *width = 1;
24179 return cursor_type;
24180 }
24181 #endif
24182
24183 return NO_CURSOR;
24184 }
24185
24186
24187 /* Notice when the text cursor of window W has been completely
24188 overwritten by a drawing operation that outputs glyphs in AREA
24189 starting at X0 and ending at X1 in the line starting at Y0 and
24190 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24191 the rest of the line after X0 has been written. Y coordinates
24192 are window-relative. */
24193
24194 static void
24195 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24196 int x0, int x1, int y0, int y1)
24197 {
24198 int cx0, cx1, cy0, cy1;
24199 struct glyph_row *row;
24200
24201 if (!w->phys_cursor_on_p)
24202 return;
24203 if (area != TEXT_AREA)
24204 return;
24205
24206 if (w->phys_cursor.vpos < 0
24207 || w->phys_cursor.vpos >= w->current_matrix->nrows
24208 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24209 !(row->enabled_p && row->displays_text_p)))
24210 return;
24211
24212 if (row->cursor_in_fringe_p)
24213 {
24214 row->cursor_in_fringe_p = 0;
24215 draw_fringe_bitmap (w, row, row->reversed_p);
24216 w->phys_cursor_on_p = 0;
24217 return;
24218 }
24219
24220 cx0 = w->phys_cursor.x;
24221 cx1 = cx0 + w->phys_cursor_width;
24222 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24223 return;
24224
24225 /* The cursor image will be completely removed from the
24226 screen if the output area intersects the cursor area in
24227 y-direction. When we draw in [y0 y1[, and some part of
24228 the cursor is at y < y0, that part must have been drawn
24229 before. When scrolling, the cursor is erased before
24230 actually scrolling, so we don't come here. When not
24231 scrolling, the rows above the old cursor row must have
24232 changed, and in this case these rows must have written
24233 over the cursor image.
24234
24235 Likewise if part of the cursor is below y1, with the
24236 exception of the cursor being in the first blank row at
24237 the buffer and window end because update_text_area
24238 doesn't draw that row. (Except when it does, but
24239 that's handled in update_text_area.) */
24240
24241 cy0 = w->phys_cursor.y;
24242 cy1 = cy0 + w->phys_cursor_height;
24243 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24244 return;
24245
24246 w->phys_cursor_on_p = 0;
24247 }
24248
24249 #endif /* HAVE_WINDOW_SYSTEM */
24250
24251 \f
24252 /************************************************************************
24253 Mouse Face
24254 ************************************************************************/
24255
24256 #ifdef HAVE_WINDOW_SYSTEM
24257
24258 /* EXPORT for RIF:
24259 Fix the display of area AREA of overlapping row ROW in window W
24260 with respect to the overlapping part OVERLAPS. */
24261
24262 void
24263 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24264 enum glyph_row_area area, int overlaps)
24265 {
24266 int i, x;
24267
24268 BLOCK_INPUT;
24269
24270 x = 0;
24271 for (i = 0; i < row->used[area];)
24272 {
24273 if (row->glyphs[area][i].overlaps_vertically_p)
24274 {
24275 int start = i, start_x = x;
24276
24277 do
24278 {
24279 x += row->glyphs[area][i].pixel_width;
24280 ++i;
24281 }
24282 while (i < row->used[area]
24283 && row->glyphs[area][i].overlaps_vertically_p);
24284
24285 draw_glyphs (w, start_x, row, area,
24286 start, i,
24287 DRAW_NORMAL_TEXT, overlaps);
24288 }
24289 else
24290 {
24291 x += row->glyphs[area][i].pixel_width;
24292 ++i;
24293 }
24294 }
24295
24296 UNBLOCK_INPUT;
24297 }
24298
24299
24300 /* EXPORT:
24301 Draw the cursor glyph of window W in glyph row ROW. See the
24302 comment of draw_glyphs for the meaning of HL. */
24303
24304 void
24305 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24306 enum draw_glyphs_face hl)
24307 {
24308 /* If cursor hpos is out of bounds, don't draw garbage. This can
24309 happen in mini-buffer windows when switching between echo area
24310 glyphs and mini-buffer. */
24311 if ((row->reversed_p
24312 ? (w->phys_cursor.hpos >= 0)
24313 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24314 {
24315 int on_p = w->phys_cursor_on_p;
24316 int x1;
24317 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24318 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24319 hl, 0);
24320 w->phys_cursor_on_p = on_p;
24321
24322 if (hl == DRAW_CURSOR)
24323 w->phys_cursor_width = x1 - w->phys_cursor.x;
24324 /* When we erase the cursor, and ROW is overlapped by other
24325 rows, make sure that these overlapping parts of other rows
24326 are redrawn. */
24327 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24328 {
24329 w->phys_cursor_width = x1 - w->phys_cursor.x;
24330
24331 if (row > w->current_matrix->rows
24332 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24333 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24334 OVERLAPS_ERASED_CURSOR);
24335
24336 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24337 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24338 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24339 OVERLAPS_ERASED_CURSOR);
24340 }
24341 }
24342 }
24343
24344
24345 /* EXPORT:
24346 Erase the image of a cursor of window W from the screen. */
24347
24348 void
24349 erase_phys_cursor (struct window *w)
24350 {
24351 struct frame *f = XFRAME (w->frame);
24352 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24353 int hpos = w->phys_cursor.hpos;
24354 int vpos = w->phys_cursor.vpos;
24355 int mouse_face_here_p = 0;
24356 struct glyph_matrix *active_glyphs = w->current_matrix;
24357 struct glyph_row *cursor_row;
24358 struct glyph *cursor_glyph;
24359 enum draw_glyphs_face hl;
24360
24361 /* No cursor displayed or row invalidated => nothing to do on the
24362 screen. */
24363 if (w->phys_cursor_type == NO_CURSOR)
24364 goto mark_cursor_off;
24365
24366 /* VPOS >= active_glyphs->nrows means that window has been resized.
24367 Don't bother to erase the cursor. */
24368 if (vpos >= active_glyphs->nrows)
24369 goto mark_cursor_off;
24370
24371 /* If row containing cursor is marked invalid, there is nothing we
24372 can do. */
24373 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24374 if (!cursor_row->enabled_p)
24375 goto mark_cursor_off;
24376
24377 /* If line spacing is > 0, old cursor may only be partially visible in
24378 window after split-window. So adjust visible height. */
24379 cursor_row->visible_height = min (cursor_row->visible_height,
24380 window_text_bottom_y (w) - cursor_row->y);
24381
24382 /* If row is completely invisible, don't attempt to delete a cursor which
24383 isn't there. This can happen if cursor is at top of a window, and
24384 we switch to a buffer with a header line in that window. */
24385 if (cursor_row->visible_height <= 0)
24386 goto mark_cursor_off;
24387
24388 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24389 if (cursor_row->cursor_in_fringe_p)
24390 {
24391 cursor_row->cursor_in_fringe_p = 0;
24392 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24393 goto mark_cursor_off;
24394 }
24395
24396 /* This can happen when the new row is shorter than the old one.
24397 In this case, either draw_glyphs or clear_end_of_line
24398 should have cleared the cursor. Note that we wouldn't be
24399 able to erase the cursor in this case because we don't have a
24400 cursor glyph at hand. */
24401 if ((cursor_row->reversed_p
24402 ? (w->phys_cursor.hpos < 0)
24403 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24404 goto mark_cursor_off;
24405
24406 /* If the cursor is in the mouse face area, redisplay that when
24407 we clear the cursor. */
24408 if (! NILP (hlinfo->mouse_face_window)
24409 && coords_in_mouse_face_p (w, hpos, vpos)
24410 /* Don't redraw the cursor's spot in mouse face if it is at the
24411 end of a line (on a newline). The cursor appears there, but
24412 mouse highlighting does not. */
24413 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24414 mouse_face_here_p = 1;
24415
24416 /* Maybe clear the display under the cursor. */
24417 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24418 {
24419 int x, y, left_x;
24420 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24421 int width;
24422
24423 cursor_glyph = get_phys_cursor_glyph (w);
24424 if (cursor_glyph == NULL)
24425 goto mark_cursor_off;
24426
24427 width = cursor_glyph->pixel_width;
24428 left_x = window_box_left_offset (w, TEXT_AREA);
24429 x = w->phys_cursor.x;
24430 if (x < left_x)
24431 width -= left_x - x;
24432 width = min (width, window_box_width (w, TEXT_AREA) - x);
24433 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24434 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24435
24436 if (width > 0)
24437 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24438 }
24439
24440 /* Erase the cursor by redrawing the character underneath it. */
24441 if (mouse_face_here_p)
24442 hl = DRAW_MOUSE_FACE;
24443 else
24444 hl = DRAW_NORMAL_TEXT;
24445 draw_phys_cursor_glyph (w, cursor_row, hl);
24446
24447 mark_cursor_off:
24448 w->phys_cursor_on_p = 0;
24449 w->phys_cursor_type = NO_CURSOR;
24450 }
24451
24452
24453 /* EXPORT:
24454 Display or clear cursor of window W. If ON is zero, clear the
24455 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24456 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24457
24458 void
24459 display_and_set_cursor (struct window *w, int on,
24460 int hpos, int vpos, int x, int y)
24461 {
24462 struct frame *f = XFRAME (w->frame);
24463 int new_cursor_type;
24464 int new_cursor_width;
24465 int active_cursor;
24466 struct glyph_row *glyph_row;
24467 struct glyph *glyph;
24468
24469 /* This is pointless on invisible frames, and dangerous on garbaged
24470 windows and frames; in the latter case, the frame or window may
24471 be in the midst of changing its size, and x and y may be off the
24472 window. */
24473 if (! FRAME_VISIBLE_P (f)
24474 || FRAME_GARBAGED_P (f)
24475 || vpos >= w->current_matrix->nrows
24476 || hpos >= w->current_matrix->matrix_w)
24477 return;
24478
24479 /* If cursor is off and we want it off, return quickly. */
24480 if (!on && !w->phys_cursor_on_p)
24481 return;
24482
24483 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24484 /* If cursor row is not enabled, we don't really know where to
24485 display the cursor. */
24486 if (!glyph_row->enabled_p)
24487 {
24488 w->phys_cursor_on_p = 0;
24489 return;
24490 }
24491
24492 glyph = NULL;
24493 if (!glyph_row->exact_window_width_line_p
24494 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24495 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24496
24497 xassert (interrupt_input_blocked);
24498
24499 /* Set new_cursor_type to the cursor we want to be displayed. */
24500 new_cursor_type = get_window_cursor_type (w, glyph,
24501 &new_cursor_width, &active_cursor);
24502
24503 /* If cursor is currently being shown and we don't want it to be or
24504 it is in the wrong place, or the cursor type is not what we want,
24505 erase it. */
24506 if (w->phys_cursor_on_p
24507 && (!on
24508 || w->phys_cursor.x != x
24509 || w->phys_cursor.y != y
24510 || new_cursor_type != w->phys_cursor_type
24511 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24512 && new_cursor_width != w->phys_cursor_width)))
24513 erase_phys_cursor (w);
24514
24515 /* Don't check phys_cursor_on_p here because that flag is only set
24516 to zero in some cases where we know that the cursor has been
24517 completely erased, to avoid the extra work of erasing the cursor
24518 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24519 still not be visible, or it has only been partly erased. */
24520 if (on)
24521 {
24522 w->phys_cursor_ascent = glyph_row->ascent;
24523 w->phys_cursor_height = glyph_row->height;
24524
24525 /* Set phys_cursor_.* before x_draw_.* is called because some
24526 of them may need the information. */
24527 w->phys_cursor.x = x;
24528 w->phys_cursor.y = glyph_row->y;
24529 w->phys_cursor.hpos = hpos;
24530 w->phys_cursor.vpos = vpos;
24531 }
24532
24533 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24534 new_cursor_type, new_cursor_width,
24535 on, active_cursor);
24536 }
24537
24538
24539 /* Switch the display of W's cursor on or off, according to the value
24540 of ON. */
24541
24542 static void
24543 update_window_cursor (struct window *w, int on)
24544 {
24545 /* Don't update cursor in windows whose frame is in the process
24546 of being deleted. */
24547 if (w->current_matrix)
24548 {
24549 BLOCK_INPUT;
24550 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24551 w->phys_cursor.x, w->phys_cursor.y);
24552 UNBLOCK_INPUT;
24553 }
24554 }
24555
24556
24557 /* Call update_window_cursor with parameter ON_P on all leaf windows
24558 in the window tree rooted at W. */
24559
24560 static void
24561 update_cursor_in_window_tree (struct window *w, int on_p)
24562 {
24563 while (w)
24564 {
24565 if (!NILP (w->hchild))
24566 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24567 else if (!NILP (w->vchild))
24568 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24569 else
24570 update_window_cursor (w, on_p);
24571
24572 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24573 }
24574 }
24575
24576
24577 /* EXPORT:
24578 Display the cursor on window W, or clear it, according to ON_P.
24579 Don't change the cursor's position. */
24580
24581 void
24582 x_update_cursor (struct frame *f, int on_p)
24583 {
24584 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24585 }
24586
24587
24588 /* EXPORT:
24589 Clear the cursor of window W to background color, and mark the
24590 cursor as not shown. This is used when the text where the cursor
24591 is about to be rewritten. */
24592
24593 void
24594 x_clear_cursor (struct window *w)
24595 {
24596 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24597 update_window_cursor (w, 0);
24598 }
24599
24600 #endif /* HAVE_WINDOW_SYSTEM */
24601
24602 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24603 and MSDOS. */
24604 static void
24605 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24606 int start_hpos, int end_hpos,
24607 enum draw_glyphs_face draw)
24608 {
24609 #ifdef HAVE_WINDOW_SYSTEM
24610 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24611 {
24612 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24613 return;
24614 }
24615 #endif
24616 #if defined (HAVE_GPM) || defined (MSDOS)
24617 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24618 #endif
24619 }
24620
24621 /* Display the active region described by mouse_face_* according to DRAW. */
24622
24623 static void
24624 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24625 {
24626 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24627 struct frame *f = XFRAME (WINDOW_FRAME (w));
24628
24629 if (/* If window is in the process of being destroyed, don't bother
24630 to do anything. */
24631 w->current_matrix != NULL
24632 /* Don't update mouse highlight if hidden */
24633 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24634 /* Recognize when we are called to operate on rows that don't exist
24635 anymore. This can happen when a window is split. */
24636 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24637 {
24638 int phys_cursor_on_p = w->phys_cursor_on_p;
24639 struct glyph_row *row, *first, *last;
24640
24641 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24642 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24643
24644 for (row = first; row <= last && row->enabled_p; ++row)
24645 {
24646 int start_hpos, end_hpos, start_x;
24647
24648 /* For all but the first row, the highlight starts at column 0. */
24649 if (row == first)
24650 {
24651 /* R2L rows have BEG and END in reversed order, but the
24652 screen drawing geometry is always left to right. So
24653 we need to mirror the beginning and end of the
24654 highlighted area in R2L rows. */
24655 if (!row->reversed_p)
24656 {
24657 start_hpos = hlinfo->mouse_face_beg_col;
24658 start_x = hlinfo->mouse_face_beg_x;
24659 }
24660 else if (row == last)
24661 {
24662 start_hpos = hlinfo->mouse_face_end_col;
24663 start_x = hlinfo->mouse_face_end_x;
24664 }
24665 else
24666 {
24667 start_hpos = 0;
24668 start_x = 0;
24669 }
24670 }
24671 else if (row->reversed_p && row == last)
24672 {
24673 start_hpos = hlinfo->mouse_face_end_col;
24674 start_x = hlinfo->mouse_face_end_x;
24675 }
24676 else
24677 {
24678 start_hpos = 0;
24679 start_x = 0;
24680 }
24681
24682 if (row == last)
24683 {
24684 if (!row->reversed_p)
24685 end_hpos = hlinfo->mouse_face_end_col;
24686 else if (row == first)
24687 end_hpos = hlinfo->mouse_face_beg_col;
24688 else
24689 {
24690 end_hpos = row->used[TEXT_AREA];
24691 if (draw == DRAW_NORMAL_TEXT)
24692 row->fill_line_p = 1; /* Clear to end of line */
24693 }
24694 }
24695 else if (row->reversed_p && row == first)
24696 end_hpos = hlinfo->mouse_face_beg_col;
24697 else
24698 {
24699 end_hpos = row->used[TEXT_AREA];
24700 if (draw == DRAW_NORMAL_TEXT)
24701 row->fill_line_p = 1; /* Clear to end of line */
24702 }
24703
24704 if (end_hpos > start_hpos)
24705 {
24706 draw_row_with_mouse_face (w, start_x, row,
24707 start_hpos, end_hpos, draw);
24708
24709 row->mouse_face_p
24710 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24711 }
24712 }
24713
24714 #ifdef HAVE_WINDOW_SYSTEM
24715 /* When we've written over the cursor, arrange for it to
24716 be displayed again. */
24717 if (FRAME_WINDOW_P (f)
24718 && phys_cursor_on_p && !w->phys_cursor_on_p)
24719 {
24720 BLOCK_INPUT;
24721 display_and_set_cursor (w, 1,
24722 w->phys_cursor.hpos, w->phys_cursor.vpos,
24723 w->phys_cursor.x, w->phys_cursor.y);
24724 UNBLOCK_INPUT;
24725 }
24726 #endif /* HAVE_WINDOW_SYSTEM */
24727 }
24728
24729 #ifdef HAVE_WINDOW_SYSTEM
24730 /* Change the mouse cursor. */
24731 if (FRAME_WINDOW_P (f))
24732 {
24733 if (draw == DRAW_NORMAL_TEXT
24734 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24735 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24736 else if (draw == DRAW_MOUSE_FACE)
24737 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24738 else
24739 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24740 }
24741 #endif /* HAVE_WINDOW_SYSTEM */
24742 }
24743
24744 /* EXPORT:
24745 Clear out the mouse-highlighted active region.
24746 Redraw it un-highlighted first. Value is non-zero if mouse
24747 face was actually drawn unhighlighted. */
24748
24749 int
24750 clear_mouse_face (Mouse_HLInfo *hlinfo)
24751 {
24752 int cleared = 0;
24753
24754 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24755 {
24756 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24757 cleared = 1;
24758 }
24759
24760 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24761 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24762 hlinfo->mouse_face_window = Qnil;
24763 hlinfo->mouse_face_overlay = Qnil;
24764 return cleared;
24765 }
24766
24767 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24768 within the mouse face on that window. */
24769 static int
24770 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24771 {
24772 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24773
24774 /* Quickly resolve the easy cases. */
24775 if (!(WINDOWP (hlinfo->mouse_face_window)
24776 && XWINDOW (hlinfo->mouse_face_window) == w))
24777 return 0;
24778 if (vpos < hlinfo->mouse_face_beg_row
24779 || vpos > hlinfo->mouse_face_end_row)
24780 return 0;
24781 if (vpos > hlinfo->mouse_face_beg_row
24782 && vpos < hlinfo->mouse_face_end_row)
24783 return 1;
24784
24785 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24786 {
24787 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24788 {
24789 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24790 return 1;
24791 }
24792 else if ((vpos == hlinfo->mouse_face_beg_row
24793 && hpos >= hlinfo->mouse_face_beg_col)
24794 || (vpos == hlinfo->mouse_face_end_row
24795 && hpos < hlinfo->mouse_face_end_col))
24796 return 1;
24797 }
24798 else
24799 {
24800 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24801 {
24802 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24803 return 1;
24804 }
24805 else if ((vpos == hlinfo->mouse_face_beg_row
24806 && hpos <= hlinfo->mouse_face_beg_col)
24807 || (vpos == hlinfo->mouse_face_end_row
24808 && hpos > hlinfo->mouse_face_end_col))
24809 return 1;
24810 }
24811 return 0;
24812 }
24813
24814
24815 /* EXPORT:
24816 Non-zero if physical cursor of window W is within mouse face. */
24817
24818 int
24819 cursor_in_mouse_face_p (struct window *w)
24820 {
24821 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24822 }
24823
24824
24825 \f
24826 /* Find the glyph rows START_ROW and END_ROW of window W that display
24827 characters between buffer positions START_CHARPOS and END_CHARPOS
24828 (excluding END_CHARPOS). This is similar to row_containing_pos,
24829 but is more accurate when bidi reordering makes buffer positions
24830 change non-linearly with glyph rows. */
24831 static void
24832 rows_from_pos_range (struct window *w,
24833 EMACS_INT start_charpos, EMACS_INT end_charpos,
24834 struct glyph_row **start, struct glyph_row **end)
24835 {
24836 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24837 int last_y = window_text_bottom_y (w);
24838 struct glyph_row *row;
24839
24840 *start = NULL;
24841 *end = NULL;
24842
24843 while (!first->enabled_p
24844 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24845 first++;
24846
24847 /* Find the START row. */
24848 for (row = first;
24849 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24850 row++)
24851 {
24852 /* A row can potentially be the START row if the range of the
24853 characters it displays intersects the range
24854 [START_CHARPOS..END_CHARPOS). */
24855 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24856 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24857 /* See the commentary in row_containing_pos, for the
24858 explanation of the complicated way to check whether
24859 some position is beyond the end of the characters
24860 displayed by a row. */
24861 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24862 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24863 && !row->ends_at_zv_p
24864 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24865 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24866 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24867 && !row->ends_at_zv_p
24868 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24869 {
24870 /* Found a candidate row. Now make sure at least one of the
24871 glyphs it displays has a charpos from the range
24872 [START_CHARPOS..END_CHARPOS).
24873
24874 This is not obvious because bidi reordering could make
24875 buffer positions of a row be 1,2,3,102,101,100, and if we
24876 want to highlight characters in [50..60), we don't want
24877 this row, even though [50..60) does intersect [1..103),
24878 the range of character positions given by the row's start
24879 and end positions. */
24880 struct glyph *g = row->glyphs[TEXT_AREA];
24881 struct glyph *e = g + row->used[TEXT_AREA];
24882
24883 while (g < e)
24884 {
24885 if (BUFFERP (g->object)
24886 && start_charpos <= g->charpos && g->charpos < end_charpos)
24887 *start = row;
24888 g++;
24889 }
24890 if (*start)
24891 break;
24892 }
24893 }
24894
24895 /* Find the END row. */
24896 if (!*start
24897 /* If the last row is partially visible, start looking for END
24898 from that row, instead of starting from FIRST. */
24899 && !(row->enabled_p
24900 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
24901 row = first;
24902 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
24903 {
24904 struct glyph_row *next = row + 1;
24905
24906 if (!next->enabled_p
24907 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
24908 /* The first row >= START whose range of displayed characters
24909 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
24910 is the row END + 1. */
24911 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
24912 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
24913 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
24914 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
24915 && !next->ends_at_zv_p
24916 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
24917 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
24918 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
24919 && !next->ends_at_zv_p
24920 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
24921 {
24922 *end = row;
24923 break;
24924 }
24925 else
24926 {
24927 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
24928 but none of the characters it displays are in the range, it is
24929 also END + 1. */
24930 struct glyph *g = next->glyphs[TEXT_AREA];
24931 struct glyph *e = g + next->used[TEXT_AREA];
24932
24933 while (g < e)
24934 {
24935 if (BUFFERP (g->object)
24936 && start_charpos <= g->charpos && g->charpos < end_charpos)
24937 break;
24938 g++;
24939 }
24940 if (g == e)
24941 {
24942 *end = row;
24943 break;
24944 }
24945 }
24946 }
24947 }
24948
24949 /* This function sets the mouse_face_* elements of HLINFO, assuming
24950 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
24951 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
24952 for the overlay or run of text properties specifying the mouse
24953 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
24954 before-string and after-string that must also be highlighted.
24955 COVER_STRING, if non-nil, is a display string that may cover some
24956 or all of the highlighted text. */
24957
24958 static void
24959 mouse_face_from_buffer_pos (Lisp_Object window,
24960 Mouse_HLInfo *hlinfo,
24961 EMACS_INT mouse_charpos,
24962 EMACS_INT start_charpos,
24963 EMACS_INT end_charpos,
24964 Lisp_Object before_string,
24965 Lisp_Object after_string,
24966 Lisp_Object cover_string)
24967 {
24968 struct window *w = XWINDOW (window);
24969 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24970 struct glyph_row *r1, *r2;
24971 struct glyph *glyph, *end;
24972 EMACS_INT ignore, pos;
24973 int x;
24974
24975 xassert (NILP (cover_string) || STRINGP (cover_string));
24976 xassert (NILP (before_string) || STRINGP (before_string));
24977 xassert (NILP (after_string) || STRINGP (after_string));
24978
24979 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
24980 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
24981 if (r1 == NULL)
24982 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
24983 /* If the before-string or display-string contains newlines,
24984 rows_from_pos_range skips to its last row. Move back. */
24985 if (!NILP (before_string) || !NILP (cover_string))
24986 {
24987 struct glyph_row *prev;
24988 while ((prev = r1 - 1, prev >= first)
24989 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
24990 && prev->used[TEXT_AREA] > 0)
24991 {
24992 struct glyph *beg = prev->glyphs[TEXT_AREA];
24993 glyph = beg + prev->used[TEXT_AREA];
24994 while (--glyph >= beg && INTEGERP (glyph->object));
24995 if (glyph < beg
24996 || !(EQ (glyph->object, before_string)
24997 || EQ (glyph->object, cover_string)))
24998 break;
24999 r1 = prev;
25000 }
25001 }
25002 if (r2 == NULL)
25003 {
25004 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25005 hlinfo->mouse_face_past_end = 1;
25006 }
25007 else if (!NILP (after_string))
25008 {
25009 /* If the after-string has newlines, advance to its last row. */
25010 struct glyph_row *next;
25011 struct glyph_row *last
25012 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25013
25014 for (next = r2 + 1;
25015 next <= last
25016 && next->used[TEXT_AREA] > 0
25017 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25018 ++next)
25019 r2 = next;
25020 }
25021 /* The rest of the display engine assumes that mouse_face_beg_row is
25022 either above below mouse_face_end_row or identical to it. But
25023 with bidi-reordered continued lines, the row for START_CHARPOS
25024 could be below the row for END_CHARPOS. If so, swap the rows and
25025 store them in correct order. */
25026 if (r1->y > r2->y)
25027 {
25028 struct glyph_row *tem = r2;
25029
25030 r2 = r1;
25031 r1 = tem;
25032 }
25033
25034 hlinfo->mouse_face_beg_y = r1->y;
25035 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25036 hlinfo->mouse_face_end_y = r2->y;
25037 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25038
25039 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25040 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25041 could be anywhere in the row and in any order. The strategy
25042 below is to find the leftmost and the rightmost glyph that
25043 belongs to either of these 3 strings, or whose position is
25044 between START_CHARPOS and END_CHARPOS, and highlight all the
25045 glyphs between those two. This may cover more than just the text
25046 between START_CHARPOS and END_CHARPOS if the range of characters
25047 strides the bidi level boundary, e.g. if the beginning is in R2L
25048 text while the end is in L2R text or vice versa. */
25049 if (!r1->reversed_p)
25050 {
25051 /* This row is in a left to right paragraph. Scan it left to
25052 right. */
25053 glyph = r1->glyphs[TEXT_AREA];
25054 end = glyph + r1->used[TEXT_AREA];
25055 x = r1->x;
25056
25057 /* Skip truncation glyphs at the start of the glyph row. */
25058 if (r1->displays_text_p)
25059 for (; glyph < end
25060 && INTEGERP (glyph->object)
25061 && glyph->charpos < 0;
25062 ++glyph)
25063 x += glyph->pixel_width;
25064
25065 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25066 or COVER_STRING, and the first glyph from buffer whose
25067 position is between START_CHARPOS and END_CHARPOS. */
25068 for (; glyph < end
25069 && !INTEGERP (glyph->object)
25070 && !EQ (glyph->object, cover_string)
25071 && !(BUFFERP (glyph->object)
25072 && (glyph->charpos >= start_charpos
25073 && glyph->charpos < end_charpos));
25074 ++glyph)
25075 {
25076 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25077 are present at buffer positions between START_CHARPOS and
25078 END_CHARPOS, or if they come from an overlay. */
25079 if (EQ (glyph->object, before_string))
25080 {
25081 pos = string_buffer_position (before_string,
25082 start_charpos);
25083 /* If pos == 0, it means before_string came from an
25084 overlay, not from a buffer position. */
25085 if (!pos || (pos >= start_charpos && pos < end_charpos))
25086 break;
25087 }
25088 else if (EQ (glyph->object, after_string))
25089 {
25090 pos = string_buffer_position (after_string, end_charpos);
25091 if (!pos || (pos >= start_charpos && pos < end_charpos))
25092 break;
25093 }
25094 x += glyph->pixel_width;
25095 }
25096 hlinfo->mouse_face_beg_x = x;
25097 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25098 }
25099 else
25100 {
25101 /* This row is in a right to left paragraph. Scan it right to
25102 left. */
25103 struct glyph *g;
25104
25105 end = r1->glyphs[TEXT_AREA] - 1;
25106 glyph = end + r1->used[TEXT_AREA];
25107
25108 /* Skip truncation glyphs at the start of the glyph row. */
25109 if (r1->displays_text_p)
25110 for (; glyph > end
25111 && INTEGERP (glyph->object)
25112 && glyph->charpos < 0;
25113 --glyph)
25114 ;
25115
25116 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25117 or COVER_STRING, and the first glyph from buffer whose
25118 position is between START_CHARPOS and END_CHARPOS. */
25119 for (; glyph > end
25120 && !INTEGERP (glyph->object)
25121 && !EQ (glyph->object, cover_string)
25122 && !(BUFFERP (glyph->object)
25123 && (glyph->charpos >= start_charpos
25124 && glyph->charpos < end_charpos));
25125 --glyph)
25126 {
25127 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25128 are present at buffer positions between START_CHARPOS and
25129 END_CHARPOS, or if they come from an overlay. */
25130 if (EQ (glyph->object, before_string))
25131 {
25132 pos = string_buffer_position (before_string, start_charpos);
25133 /* If pos == 0, it means before_string came from an
25134 overlay, not from a buffer position. */
25135 if (!pos || (pos >= start_charpos && pos < end_charpos))
25136 break;
25137 }
25138 else if (EQ (glyph->object, after_string))
25139 {
25140 pos = string_buffer_position (after_string, end_charpos);
25141 if (!pos || (pos >= start_charpos && pos < end_charpos))
25142 break;
25143 }
25144 }
25145
25146 glyph++; /* first glyph to the right of the highlighted area */
25147 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25148 x += g->pixel_width;
25149 hlinfo->mouse_face_beg_x = x;
25150 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25151 }
25152
25153 /* If the highlight ends in a different row, compute GLYPH and END
25154 for the end row. Otherwise, reuse the values computed above for
25155 the row where the highlight begins. */
25156 if (r2 != r1)
25157 {
25158 if (!r2->reversed_p)
25159 {
25160 glyph = r2->glyphs[TEXT_AREA];
25161 end = glyph + r2->used[TEXT_AREA];
25162 x = r2->x;
25163 }
25164 else
25165 {
25166 end = r2->glyphs[TEXT_AREA] - 1;
25167 glyph = end + r2->used[TEXT_AREA];
25168 }
25169 }
25170
25171 if (!r2->reversed_p)
25172 {
25173 /* Skip truncation and continuation glyphs near the end of the
25174 row, and also blanks and stretch glyphs inserted by
25175 extend_face_to_end_of_line. */
25176 while (end > glyph
25177 && INTEGERP ((end - 1)->object)
25178 && (end - 1)->charpos <= 0)
25179 --end;
25180 /* Scan the rest of the glyph row from the end, looking for the
25181 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25182 COVER_STRING, or whose position is between START_CHARPOS
25183 and END_CHARPOS */
25184 for (--end;
25185 end > glyph
25186 && !INTEGERP (end->object)
25187 && !EQ (end->object, cover_string)
25188 && !(BUFFERP (end->object)
25189 && (end->charpos >= start_charpos
25190 && end->charpos < end_charpos));
25191 --end)
25192 {
25193 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25194 are present at buffer positions between START_CHARPOS and
25195 END_CHARPOS, or if they come from an overlay. */
25196 if (EQ (end->object, before_string))
25197 {
25198 pos = string_buffer_position (before_string, start_charpos);
25199 if (!pos || (pos >= start_charpos && pos < end_charpos))
25200 break;
25201 }
25202 else if (EQ (end->object, after_string))
25203 {
25204 pos = string_buffer_position (after_string, end_charpos);
25205 if (!pos || (pos >= start_charpos && pos < end_charpos))
25206 break;
25207 }
25208 }
25209 /* Find the X coordinate of the last glyph to be highlighted. */
25210 for (; glyph <= end; ++glyph)
25211 x += glyph->pixel_width;
25212
25213 hlinfo->mouse_face_end_x = x;
25214 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25215 }
25216 else
25217 {
25218 /* Skip truncation and continuation glyphs near the end of the
25219 row, and also blanks and stretch glyphs inserted by
25220 extend_face_to_end_of_line. */
25221 x = r2->x;
25222 end++;
25223 while (end < glyph
25224 && INTEGERP (end->object)
25225 && end->charpos <= 0)
25226 {
25227 x += end->pixel_width;
25228 ++end;
25229 }
25230 /* Scan the rest of the glyph row from the end, looking for the
25231 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25232 COVER_STRING, or whose position is between START_CHARPOS
25233 and END_CHARPOS */
25234 for ( ;
25235 end < glyph
25236 && !INTEGERP (end->object)
25237 && !EQ (end->object, cover_string)
25238 && !(BUFFERP (end->object)
25239 && (end->charpos >= start_charpos
25240 && end->charpos < end_charpos));
25241 ++end)
25242 {
25243 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25244 are present at buffer positions between START_CHARPOS and
25245 END_CHARPOS, or if they come from an overlay. */
25246 if (EQ (end->object, before_string))
25247 {
25248 pos = string_buffer_position (before_string, start_charpos);
25249 if (!pos || (pos >= start_charpos && pos < end_charpos))
25250 break;
25251 }
25252 else if (EQ (end->object, after_string))
25253 {
25254 pos = string_buffer_position (after_string, end_charpos);
25255 if (!pos || (pos >= start_charpos && pos < end_charpos))
25256 break;
25257 }
25258 x += end->pixel_width;
25259 }
25260 hlinfo->mouse_face_end_x = x;
25261 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25262 }
25263
25264 hlinfo->mouse_face_window = window;
25265 hlinfo->mouse_face_face_id
25266 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25267 mouse_charpos + 1,
25268 !hlinfo->mouse_face_hidden, -1);
25269 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25270 }
25271
25272 /* The following function is not used anymore (replaced with
25273 mouse_face_from_string_pos), but I leave it here for the time
25274 being, in case someone would. */
25275
25276 #if 0 /* not used */
25277
25278 /* Find the position of the glyph for position POS in OBJECT in
25279 window W's current matrix, and return in *X, *Y the pixel
25280 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25281
25282 RIGHT_P non-zero means return the position of the right edge of the
25283 glyph, RIGHT_P zero means return the left edge position.
25284
25285 If no glyph for POS exists in the matrix, return the position of
25286 the glyph with the next smaller position that is in the matrix, if
25287 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25288 exists in the matrix, return the position of the glyph with the
25289 next larger position in OBJECT.
25290
25291 Value is non-zero if a glyph was found. */
25292
25293 static int
25294 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25295 int *hpos, int *vpos, int *x, int *y, int right_p)
25296 {
25297 int yb = window_text_bottom_y (w);
25298 struct glyph_row *r;
25299 struct glyph *best_glyph = NULL;
25300 struct glyph_row *best_row = NULL;
25301 int best_x = 0;
25302
25303 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25304 r->enabled_p && r->y < yb;
25305 ++r)
25306 {
25307 struct glyph *g = r->glyphs[TEXT_AREA];
25308 struct glyph *e = g + r->used[TEXT_AREA];
25309 int gx;
25310
25311 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25312 if (EQ (g->object, object))
25313 {
25314 if (g->charpos == pos)
25315 {
25316 best_glyph = g;
25317 best_x = gx;
25318 best_row = r;
25319 goto found;
25320 }
25321 else if (best_glyph == NULL
25322 || ((eabs (g->charpos - pos)
25323 < eabs (best_glyph->charpos - pos))
25324 && (right_p
25325 ? g->charpos < pos
25326 : g->charpos > pos)))
25327 {
25328 best_glyph = g;
25329 best_x = gx;
25330 best_row = r;
25331 }
25332 }
25333 }
25334
25335 found:
25336
25337 if (best_glyph)
25338 {
25339 *x = best_x;
25340 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25341
25342 if (right_p)
25343 {
25344 *x += best_glyph->pixel_width;
25345 ++*hpos;
25346 }
25347
25348 *y = best_row->y;
25349 *vpos = best_row - w->current_matrix->rows;
25350 }
25351
25352 return best_glyph != NULL;
25353 }
25354 #endif /* not used */
25355
25356 /* Find the positions of the first and the last glyphs in window W's
25357 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25358 (assumed to be a string), and return in HLINFO's mouse_face_*
25359 members the pixel and column/row coordinates of those glyphs. */
25360
25361 static void
25362 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25363 Lisp_Object object,
25364 EMACS_INT startpos, EMACS_INT endpos)
25365 {
25366 int yb = window_text_bottom_y (w);
25367 struct glyph_row *r;
25368 struct glyph *g, *e;
25369 int gx;
25370 int found = 0;
25371
25372 /* Find the glyph row with at least one position in the range
25373 [STARTPOS..ENDPOS], and the first glyph in that row whose
25374 position belongs to that range. */
25375 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25376 r->enabled_p && r->y < yb;
25377 ++r)
25378 {
25379 if (!r->reversed_p)
25380 {
25381 g = r->glyphs[TEXT_AREA];
25382 e = g + r->used[TEXT_AREA];
25383 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25384 if (EQ (g->object, object)
25385 && startpos <= g->charpos && g->charpos <= endpos)
25386 {
25387 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25388 hlinfo->mouse_face_beg_y = r->y;
25389 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25390 hlinfo->mouse_face_beg_x = gx;
25391 found = 1;
25392 break;
25393 }
25394 }
25395 else
25396 {
25397 struct glyph *g1;
25398
25399 e = r->glyphs[TEXT_AREA];
25400 g = e + r->used[TEXT_AREA];
25401 for ( ; g > e; --g)
25402 if (EQ ((g-1)->object, object)
25403 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25404 {
25405 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25406 hlinfo->mouse_face_beg_y = r->y;
25407 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25408 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25409 gx += g1->pixel_width;
25410 hlinfo->mouse_face_beg_x = gx;
25411 found = 1;
25412 break;
25413 }
25414 }
25415 if (found)
25416 break;
25417 }
25418
25419 if (!found)
25420 return;
25421
25422 /* Starting with the next row, look for the first row which does NOT
25423 include any glyphs whose positions are in the range. */
25424 for (++r; r->enabled_p && r->y < yb; ++r)
25425 {
25426 g = r->glyphs[TEXT_AREA];
25427 e = g + r->used[TEXT_AREA];
25428 found = 0;
25429 for ( ; g < e; ++g)
25430 if (EQ (g->object, object)
25431 && startpos <= g->charpos && g->charpos <= endpos)
25432 {
25433 found = 1;
25434 break;
25435 }
25436 if (!found)
25437 break;
25438 }
25439
25440 /* The highlighted region ends on the previous row. */
25441 r--;
25442
25443 /* Set the end row and its vertical pixel coordinate. */
25444 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25445 hlinfo->mouse_face_end_y = r->y;
25446
25447 /* Compute and set the end column and the end column's horizontal
25448 pixel coordinate. */
25449 if (!r->reversed_p)
25450 {
25451 g = r->glyphs[TEXT_AREA];
25452 e = g + r->used[TEXT_AREA];
25453 for ( ; e > g; --e)
25454 if (EQ ((e-1)->object, object)
25455 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25456 break;
25457 hlinfo->mouse_face_end_col = e - g;
25458
25459 for (gx = r->x; g < e; ++g)
25460 gx += g->pixel_width;
25461 hlinfo->mouse_face_end_x = gx;
25462 }
25463 else
25464 {
25465 e = r->glyphs[TEXT_AREA];
25466 g = e + r->used[TEXT_AREA];
25467 for (gx = r->x ; e < g; ++e)
25468 {
25469 if (EQ (e->object, object)
25470 && startpos <= e->charpos && e->charpos <= endpos)
25471 break;
25472 gx += e->pixel_width;
25473 }
25474 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25475 hlinfo->mouse_face_end_x = gx;
25476 }
25477 }
25478
25479 #ifdef HAVE_WINDOW_SYSTEM
25480
25481 /* See if position X, Y is within a hot-spot of an image. */
25482
25483 static int
25484 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25485 {
25486 if (!CONSP (hot_spot))
25487 return 0;
25488
25489 if (EQ (XCAR (hot_spot), Qrect))
25490 {
25491 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25492 Lisp_Object rect = XCDR (hot_spot);
25493 Lisp_Object tem;
25494 if (!CONSP (rect))
25495 return 0;
25496 if (!CONSP (XCAR (rect)))
25497 return 0;
25498 if (!CONSP (XCDR (rect)))
25499 return 0;
25500 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25501 return 0;
25502 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25503 return 0;
25504 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25505 return 0;
25506 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25507 return 0;
25508 return 1;
25509 }
25510 else if (EQ (XCAR (hot_spot), Qcircle))
25511 {
25512 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25513 Lisp_Object circ = XCDR (hot_spot);
25514 Lisp_Object lr, lx0, ly0;
25515 if (CONSP (circ)
25516 && CONSP (XCAR (circ))
25517 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25518 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25519 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25520 {
25521 double r = XFLOATINT (lr);
25522 double dx = XINT (lx0) - x;
25523 double dy = XINT (ly0) - y;
25524 return (dx * dx + dy * dy <= r * r);
25525 }
25526 }
25527 else if (EQ (XCAR (hot_spot), Qpoly))
25528 {
25529 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25530 if (VECTORP (XCDR (hot_spot)))
25531 {
25532 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25533 Lisp_Object *poly = v->contents;
25534 int n = v->header.size;
25535 int i;
25536 int inside = 0;
25537 Lisp_Object lx, ly;
25538 int x0, y0;
25539
25540 /* Need an even number of coordinates, and at least 3 edges. */
25541 if (n < 6 || n & 1)
25542 return 0;
25543
25544 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25545 If count is odd, we are inside polygon. Pixels on edges
25546 may or may not be included depending on actual geometry of the
25547 polygon. */
25548 if ((lx = poly[n-2], !INTEGERP (lx))
25549 || (ly = poly[n-1], !INTEGERP (lx)))
25550 return 0;
25551 x0 = XINT (lx), y0 = XINT (ly);
25552 for (i = 0; i < n; i += 2)
25553 {
25554 int x1 = x0, y1 = y0;
25555 if ((lx = poly[i], !INTEGERP (lx))
25556 || (ly = poly[i+1], !INTEGERP (ly)))
25557 return 0;
25558 x0 = XINT (lx), y0 = XINT (ly);
25559
25560 /* Does this segment cross the X line? */
25561 if (x0 >= x)
25562 {
25563 if (x1 >= x)
25564 continue;
25565 }
25566 else if (x1 < x)
25567 continue;
25568 if (y > y0 && y > y1)
25569 continue;
25570 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25571 inside = !inside;
25572 }
25573 return inside;
25574 }
25575 }
25576 return 0;
25577 }
25578
25579 Lisp_Object
25580 find_hot_spot (Lisp_Object map, int x, int y)
25581 {
25582 while (CONSP (map))
25583 {
25584 if (CONSP (XCAR (map))
25585 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25586 return XCAR (map);
25587 map = XCDR (map);
25588 }
25589
25590 return Qnil;
25591 }
25592
25593 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25594 3, 3, 0,
25595 doc: /* Lookup in image map MAP coordinates X and Y.
25596 An image map is an alist where each element has the format (AREA ID PLIST).
25597 An AREA is specified as either a rectangle, a circle, or a polygon:
25598 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25599 pixel coordinates of the upper left and bottom right corners.
25600 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25601 and the radius of the circle; r may be a float or integer.
25602 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25603 vector describes one corner in the polygon.
25604 Returns the alist element for the first matching AREA in MAP. */)
25605 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25606 {
25607 if (NILP (map))
25608 return Qnil;
25609
25610 CHECK_NUMBER (x);
25611 CHECK_NUMBER (y);
25612
25613 return find_hot_spot (map, XINT (x), XINT (y));
25614 }
25615
25616
25617 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25618 static void
25619 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25620 {
25621 /* Do not change cursor shape while dragging mouse. */
25622 if (!NILP (do_mouse_tracking))
25623 return;
25624
25625 if (!NILP (pointer))
25626 {
25627 if (EQ (pointer, Qarrow))
25628 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25629 else if (EQ (pointer, Qhand))
25630 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25631 else if (EQ (pointer, Qtext))
25632 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25633 else if (EQ (pointer, intern ("hdrag")))
25634 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25635 #ifdef HAVE_X_WINDOWS
25636 else if (EQ (pointer, intern ("vdrag")))
25637 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25638 #endif
25639 else if (EQ (pointer, intern ("hourglass")))
25640 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25641 else if (EQ (pointer, Qmodeline))
25642 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25643 else
25644 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25645 }
25646
25647 if (cursor != No_Cursor)
25648 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25649 }
25650
25651 #endif /* HAVE_WINDOW_SYSTEM */
25652
25653 /* Take proper action when mouse has moved to the mode or header line
25654 or marginal area AREA of window W, x-position X and y-position Y.
25655 X is relative to the start of the text display area of W, so the
25656 width of bitmap areas and scroll bars must be subtracted to get a
25657 position relative to the start of the mode line. */
25658
25659 static void
25660 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25661 enum window_part area)
25662 {
25663 struct window *w = XWINDOW (window);
25664 struct frame *f = XFRAME (w->frame);
25665 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25666 #ifdef HAVE_WINDOW_SYSTEM
25667 Display_Info *dpyinfo;
25668 #endif
25669 Cursor cursor = No_Cursor;
25670 Lisp_Object pointer = Qnil;
25671 int dx, dy, width, height;
25672 EMACS_INT charpos;
25673 Lisp_Object string, object = Qnil;
25674 Lisp_Object pos, help;
25675
25676 Lisp_Object mouse_face;
25677 int original_x_pixel = x;
25678 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25679 struct glyph_row *row;
25680
25681 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25682 {
25683 int x0;
25684 struct glyph *end;
25685
25686 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25687 returns them in row/column units! */
25688 string = mode_line_string (w, area, &x, &y, &charpos,
25689 &object, &dx, &dy, &width, &height);
25690
25691 row = (area == ON_MODE_LINE
25692 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25693 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25694
25695 /* Find the glyph under the mouse pointer. */
25696 if (row->mode_line_p && row->enabled_p)
25697 {
25698 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25699 end = glyph + row->used[TEXT_AREA];
25700
25701 for (x0 = original_x_pixel;
25702 glyph < end && x0 >= glyph->pixel_width;
25703 ++glyph)
25704 x0 -= glyph->pixel_width;
25705
25706 if (glyph >= end)
25707 glyph = NULL;
25708 }
25709 }
25710 else
25711 {
25712 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25713 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25714 returns them in row/column units! */
25715 string = marginal_area_string (w, area, &x, &y, &charpos,
25716 &object, &dx, &dy, &width, &height);
25717 }
25718
25719 help = Qnil;
25720
25721 #ifdef HAVE_WINDOW_SYSTEM
25722 if (IMAGEP (object))
25723 {
25724 Lisp_Object image_map, hotspot;
25725 if ((image_map = Fplist_get (XCDR (object), QCmap),
25726 !NILP (image_map))
25727 && (hotspot = find_hot_spot (image_map, dx, dy),
25728 CONSP (hotspot))
25729 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25730 {
25731 Lisp_Object plist;
25732
25733 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25734 If so, we could look for mouse-enter, mouse-leave
25735 properties in PLIST (and do something...). */
25736 hotspot = XCDR (hotspot);
25737 if (CONSP (hotspot)
25738 && (plist = XCAR (hotspot), CONSP (plist)))
25739 {
25740 pointer = Fplist_get (plist, Qpointer);
25741 if (NILP (pointer))
25742 pointer = Qhand;
25743 help = Fplist_get (plist, Qhelp_echo);
25744 if (!NILP (help))
25745 {
25746 help_echo_string = help;
25747 /* Is this correct? ++kfs */
25748 XSETWINDOW (help_echo_window, w);
25749 help_echo_object = w->buffer;
25750 help_echo_pos = charpos;
25751 }
25752 }
25753 }
25754 if (NILP (pointer))
25755 pointer = Fplist_get (XCDR (object), QCpointer);
25756 }
25757 #endif /* HAVE_WINDOW_SYSTEM */
25758
25759 if (STRINGP (string))
25760 {
25761 pos = make_number (charpos);
25762 /* If we're on a string with `help-echo' text property, arrange
25763 for the help to be displayed. This is done by setting the
25764 global variable help_echo_string to the help string. */
25765 if (NILP (help))
25766 {
25767 help = Fget_text_property (pos, Qhelp_echo, string);
25768 if (!NILP (help))
25769 {
25770 help_echo_string = help;
25771 XSETWINDOW (help_echo_window, w);
25772 help_echo_object = string;
25773 help_echo_pos = charpos;
25774 }
25775 }
25776
25777 #ifdef HAVE_WINDOW_SYSTEM
25778 if (FRAME_WINDOW_P (f))
25779 {
25780 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25781 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25782 if (NILP (pointer))
25783 pointer = Fget_text_property (pos, Qpointer, string);
25784
25785 /* Change the mouse pointer according to what is under X/Y. */
25786 if (NILP (pointer)
25787 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25788 {
25789 Lisp_Object map;
25790 map = Fget_text_property (pos, Qlocal_map, string);
25791 if (!KEYMAPP (map))
25792 map = Fget_text_property (pos, Qkeymap, string);
25793 if (!KEYMAPP (map))
25794 cursor = dpyinfo->vertical_scroll_bar_cursor;
25795 }
25796 }
25797 #endif
25798
25799 /* Change the mouse face according to what is under X/Y. */
25800 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25801 if (!NILP (mouse_face)
25802 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25803 && glyph)
25804 {
25805 Lisp_Object b, e;
25806
25807 struct glyph * tmp_glyph;
25808
25809 int gpos;
25810 int gseq_length;
25811 int total_pixel_width;
25812 EMACS_INT begpos, endpos, ignore;
25813
25814 int vpos, hpos;
25815
25816 b = Fprevious_single_property_change (make_number (charpos + 1),
25817 Qmouse_face, string, Qnil);
25818 if (NILP (b))
25819 begpos = 0;
25820 else
25821 begpos = XINT (b);
25822
25823 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25824 if (NILP (e))
25825 endpos = SCHARS (string);
25826 else
25827 endpos = XINT (e);
25828
25829 /* Calculate the glyph position GPOS of GLYPH in the
25830 displayed string, relative to the beginning of the
25831 highlighted part of the string.
25832
25833 Note: GPOS is different from CHARPOS. CHARPOS is the
25834 position of GLYPH in the internal string object. A mode
25835 line string format has structures which are converted to
25836 a flattened string by the Emacs Lisp interpreter. The
25837 internal string is an element of those structures. The
25838 displayed string is the flattened string. */
25839 tmp_glyph = row_start_glyph;
25840 while (tmp_glyph < glyph
25841 && (!(EQ (tmp_glyph->object, glyph->object)
25842 && begpos <= tmp_glyph->charpos
25843 && tmp_glyph->charpos < endpos)))
25844 tmp_glyph++;
25845 gpos = glyph - tmp_glyph;
25846
25847 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25848 the highlighted part of the displayed string to which
25849 GLYPH belongs. Note: GSEQ_LENGTH is different from
25850 SCHARS (STRING), because the latter returns the length of
25851 the internal string. */
25852 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25853 tmp_glyph > glyph
25854 && (!(EQ (tmp_glyph->object, glyph->object)
25855 && begpos <= tmp_glyph->charpos
25856 && tmp_glyph->charpos < endpos));
25857 tmp_glyph--)
25858 ;
25859 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25860
25861 /* Calculate the total pixel width of all the glyphs between
25862 the beginning of the highlighted area and GLYPH. */
25863 total_pixel_width = 0;
25864 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25865 total_pixel_width += tmp_glyph->pixel_width;
25866
25867 /* Pre calculation of re-rendering position. Note: X is in
25868 column units here, after the call to mode_line_string or
25869 marginal_area_string. */
25870 hpos = x - gpos;
25871 vpos = (area == ON_MODE_LINE
25872 ? (w->current_matrix)->nrows - 1
25873 : 0);
25874
25875 /* If GLYPH's position is included in the region that is
25876 already drawn in mouse face, we have nothing to do. */
25877 if ( EQ (window, hlinfo->mouse_face_window)
25878 && (!row->reversed_p
25879 ? (hlinfo->mouse_face_beg_col <= hpos
25880 && hpos < hlinfo->mouse_face_end_col)
25881 /* In R2L rows we swap BEG and END, see below. */
25882 : (hlinfo->mouse_face_end_col <= hpos
25883 && hpos < hlinfo->mouse_face_beg_col))
25884 && hlinfo->mouse_face_beg_row == vpos )
25885 return;
25886
25887 if (clear_mouse_face (hlinfo))
25888 cursor = No_Cursor;
25889
25890 if (!row->reversed_p)
25891 {
25892 hlinfo->mouse_face_beg_col = hpos;
25893 hlinfo->mouse_face_beg_x = original_x_pixel
25894 - (total_pixel_width + dx);
25895 hlinfo->mouse_face_end_col = hpos + gseq_length;
25896 hlinfo->mouse_face_end_x = 0;
25897 }
25898 else
25899 {
25900 /* In R2L rows, show_mouse_face expects BEG and END
25901 coordinates to be swapped. */
25902 hlinfo->mouse_face_end_col = hpos;
25903 hlinfo->mouse_face_end_x = original_x_pixel
25904 - (total_pixel_width + dx);
25905 hlinfo->mouse_face_beg_col = hpos + gseq_length;
25906 hlinfo->mouse_face_beg_x = 0;
25907 }
25908
25909 hlinfo->mouse_face_beg_row = vpos;
25910 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
25911 hlinfo->mouse_face_beg_y = 0;
25912 hlinfo->mouse_face_end_y = 0;
25913 hlinfo->mouse_face_past_end = 0;
25914 hlinfo->mouse_face_window = window;
25915
25916 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
25917 charpos,
25918 0, 0, 0,
25919 &ignore,
25920 glyph->face_id,
25921 1);
25922 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25923
25924 if (NILP (pointer))
25925 pointer = Qhand;
25926 }
25927 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25928 clear_mouse_face (hlinfo);
25929 }
25930 #ifdef HAVE_WINDOW_SYSTEM
25931 if (FRAME_WINDOW_P (f))
25932 define_frame_cursor1 (f, cursor, pointer);
25933 #endif
25934 }
25935
25936
25937 /* EXPORT:
25938 Take proper action when the mouse has moved to position X, Y on
25939 frame F as regards highlighting characters that have mouse-face
25940 properties. Also de-highlighting chars where the mouse was before.
25941 X and Y can be negative or out of range. */
25942
25943 void
25944 note_mouse_highlight (struct frame *f, int x, int y)
25945 {
25946 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25947 enum window_part part;
25948 Lisp_Object window;
25949 struct window *w;
25950 Cursor cursor = No_Cursor;
25951 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
25952 struct buffer *b;
25953
25954 /* When a menu is active, don't highlight because this looks odd. */
25955 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
25956 if (popup_activated ())
25957 return;
25958 #endif
25959
25960 if (NILP (Vmouse_highlight)
25961 || !f->glyphs_initialized_p
25962 || f->pointer_invisible)
25963 return;
25964
25965 hlinfo->mouse_face_mouse_x = x;
25966 hlinfo->mouse_face_mouse_y = y;
25967 hlinfo->mouse_face_mouse_frame = f;
25968
25969 if (hlinfo->mouse_face_defer)
25970 return;
25971
25972 if (gc_in_progress)
25973 {
25974 hlinfo->mouse_face_deferred_gc = 1;
25975 return;
25976 }
25977
25978 /* Which window is that in? */
25979 window = window_from_coordinates (f, x, y, &part, 1);
25980
25981 /* If we were displaying active text in another window, clear that.
25982 Also clear if we move out of text area in same window. */
25983 if (! EQ (window, hlinfo->mouse_face_window)
25984 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
25985 && !NILP (hlinfo->mouse_face_window)))
25986 clear_mouse_face (hlinfo);
25987
25988 /* Not on a window -> return. */
25989 if (!WINDOWP (window))
25990 return;
25991
25992 /* Reset help_echo_string. It will get recomputed below. */
25993 help_echo_string = Qnil;
25994
25995 /* Convert to window-relative pixel coordinates. */
25996 w = XWINDOW (window);
25997 frame_to_window_pixel_xy (w, &x, &y);
25998
25999 #ifdef HAVE_WINDOW_SYSTEM
26000 /* Handle tool-bar window differently since it doesn't display a
26001 buffer. */
26002 if (EQ (window, f->tool_bar_window))
26003 {
26004 note_tool_bar_highlight (f, x, y);
26005 return;
26006 }
26007 #endif
26008
26009 /* Mouse is on the mode, header line or margin? */
26010 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26011 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26012 {
26013 note_mode_line_or_margin_highlight (window, x, y, part);
26014 return;
26015 }
26016
26017 #ifdef HAVE_WINDOW_SYSTEM
26018 if (part == ON_VERTICAL_BORDER)
26019 {
26020 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26021 help_echo_string = build_string ("drag-mouse-1: resize");
26022 }
26023 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26024 || part == ON_SCROLL_BAR)
26025 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26026 else
26027 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26028 #endif
26029
26030 /* Are we in a window whose display is up to date?
26031 And verify the buffer's text has not changed. */
26032 b = XBUFFER (w->buffer);
26033 if (part == ON_TEXT
26034 && EQ (w->window_end_valid, w->buffer)
26035 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26036 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26037 {
26038 int hpos, vpos, i, dx, dy, area;
26039 EMACS_INT pos;
26040 struct glyph *glyph;
26041 Lisp_Object object;
26042 Lisp_Object mouse_face = Qnil, position;
26043 Lisp_Object *overlay_vec = NULL;
26044 int noverlays;
26045 struct buffer *obuf;
26046 EMACS_INT obegv, ozv;
26047 int same_region;
26048
26049 /* Find the glyph under X/Y. */
26050 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26051
26052 #ifdef HAVE_WINDOW_SYSTEM
26053 /* Look for :pointer property on image. */
26054 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26055 {
26056 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26057 if (img != NULL && IMAGEP (img->spec))
26058 {
26059 Lisp_Object image_map, hotspot;
26060 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26061 !NILP (image_map))
26062 && (hotspot = find_hot_spot (image_map,
26063 glyph->slice.img.x + dx,
26064 glyph->slice.img.y + dy),
26065 CONSP (hotspot))
26066 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26067 {
26068 Lisp_Object plist;
26069
26070 /* Could check XCAR (hotspot) to see if we enter/leave
26071 this hot-spot.
26072 If so, we could look for mouse-enter, mouse-leave
26073 properties in PLIST (and do something...). */
26074 hotspot = XCDR (hotspot);
26075 if (CONSP (hotspot)
26076 && (plist = XCAR (hotspot), CONSP (plist)))
26077 {
26078 pointer = Fplist_get (plist, Qpointer);
26079 if (NILP (pointer))
26080 pointer = Qhand;
26081 help_echo_string = Fplist_get (plist, Qhelp_echo);
26082 if (!NILP (help_echo_string))
26083 {
26084 help_echo_window = window;
26085 help_echo_object = glyph->object;
26086 help_echo_pos = glyph->charpos;
26087 }
26088 }
26089 }
26090 if (NILP (pointer))
26091 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26092 }
26093 }
26094 #endif /* HAVE_WINDOW_SYSTEM */
26095
26096 /* Clear mouse face if X/Y not over text. */
26097 if (glyph == NULL
26098 || area != TEXT_AREA
26099 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26100 /* Glyph's OBJECT is an integer for glyphs inserted by the
26101 display engine for its internal purposes, like truncation
26102 and continuation glyphs and blanks beyond the end of
26103 line's text on text terminals. If we are over such a
26104 glyph, we are not over any text. */
26105 || INTEGERP (glyph->object)
26106 /* R2L rows have a stretch glyph at their front, which
26107 stands for no text, whereas L2R rows have no glyphs at
26108 all beyond the end of text. Treat such stretch glyphs
26109 like we do with NULL glyphs in L2R rows. */
26110 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26111 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26112 && glyph->type == STRETCH_GLYPH
26113 && glyph->avoid_cursor_p))
26114 {
26115 if (clear_mouse_face (hlinfo))
26116 cursor = No_Cursor;
26117 #ifdef HAVE_WINDOW_SYSTEM
26118 if (FRAME_WINDOW_P (f) && NILP (pointer))
26119 {
26120 if (area != TEXT_AREA)
26121 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26122 else
26123 pointer = Vvoid_text_area_pointer;
26124 }
26125 #endif
26126 goto set_cursor;
26127 }
26128
26129 pos = glyph->charpos;
26130 object = glyph->object;
26131 if (!STRINGP (object) && !BUFFERP (object))
26132 goto set_cursor;
26133
26134 /* If we get an out-of-range value, return now; avoid an error. */
26135 if (BUFFERP (object) && pos > BUF_Z (b))
26136 goto set_cursor;
26137
26138 /* Make the window's buffer temporarily current for
26139 overlays_at and compute_char_face. */
26140 obuf = current_buffer;
26141 current_buffer = b;
26142 obegv = BEGV;
26143 ozv = ZV;
26144 BEGV = BEG;
26145 ZV = Z;
26146
26147 /* Is this char mouse-active or does it have help-echo? */
26148 position = make_number (pos);
26149
26150 if (BUFFERP (object))
26151 {
26152 /* Put all the overlays we want in a vector in overlay_vec. */
26153 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26154 /* Sort overlays into increasing priority order. */
26155 noverlays = sort_overlays (overlay_vec, noverlays, w);
26156 }
26157 else
26158 noverlays = 0;
26159
26160 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26161
26162 if (same_region)
26163 cursor = No_Cursor;
26164
26165 /* Check mouse-face highlighting. */
26166 if (! same_region
26167 /* If there exists an overlay with mouse-face overlapping
26168 the one we are currently highlighting, we have to
26169 check if we enter the overlapping overlay, and then
26170 highlight only that. */
26171 || (OVERLAYP (hlinfo->mouse_face_overlay)
26172 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26173 {
26174 /* Find the highest priority overlay with a mouse-face. */
26175 Lisp_Object overlay = Qnil;
26176 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26177 {
26178 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26179 if (!NILP (mouse_face))
26180 overlay = overlay_vec[i];
26181 }
26182
26183 /* If we're highlighting the same overlay as before, there's
26184 no need to do that again. */
26185 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26186 goto check_help_echo;
26187 hlinfo->mouse_face_overlay = overlay;
26188
26189 /* Clear the display of the old active region, if any. */
26190 if (clear_mouse_face (hlinfo))
26191 cursor = No_Cursor;
26192
26193 /* If no overlay applies, get a text property. */
26194 if (NILP (overlay))
26195 mouse_face = Fget_text_property (position, Qmouse_face, object);
26196
26197 /* Next, compute the bounds of the mouse highlighting and
26198 display it. */
26199 if (!NILP (mouse_face) && STRINGP (object))
26200 {
26201 /* The mouse-highlighting comes from a display string
26202 with a mouse-face. */
26203 Lisp_Object s, e;
26204 EMACS_INT ignore;
26205
26206 s = Fprevious_single_property_change
26207 (make_number (pos + 1), Qmouse_face, object, Qnil);
26208 e = Fnext_single_property_change
26209 (position, Qmouse_face, object, Qnil);
26210 if (NILP (s))
26211 s = make_number (0);
26212 if (NILP (e))
26213 e = make_number (SCHARS (object) - 1);
26214 mouse_face_from_string_pos (w, hlinfo, object,
26215 XINT (s), XINT (e));
26216 hlinfo->mouse_face_past_end = 0;
26217 hlinfo->mouse_face_window = window;
26218 hlinfo->mouse_face_face_id
26219 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26220 glyph->face_id, 1);
26221 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26222 cursor = No_Cursor;
26223 }
26224 else
26225 {
26226 /* The mouse-highlighting, if any, comes from an overlay
26227 or text property in the buffer. */
26228 Lisp_Object buffer IF_LINT (= Qnil);
26229 Lisp_Object cover_string IF_LINT (= Qnil);
26230
26231 if (STRINGP (object))
26232 {
26233 /* If we are on a display string with no mouse-face,
26234 check if the text under it has one. */
26235 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26236 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26237 pos = string_buffer_position (object, start);
26238 if (pos > 0)
26239 {
26240 mouse_face = get_char_property_and_overlay
26241 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26242 buffer = w->buffer;
26243 cover_string = object;
26244 }
26245 }
26246 else
26247 {
26248 buffer = object;
26249 cover_string = Qnil;
26250 }
26251
26252 if (!NILP (mouse_face))
26253 {
26254 Lisp_Object before, after;
26255 Lisp_Object before_string, after_string;
26256 /* To correctly find the limits of mouse highlight
26257 in a bidi-reordered buffer, we must not use the
26258 optimization of limiting the search in
26259 previous-single-property-change and
26260 next-single-property-change, because
26261 rows_from_pos_range needs the real start and end
26262 positions to DTRT in this case. That's because
26263 the first row visible in a window does not
26264 necessarily display the character whose position
26265 is the smallest. */
26266 Lisp_Object lim1 =
26267 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26268 ? Fmarker_position (w->start)
26269 : Qnil;
26270 Lisp_Object lim2 =
26271 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26272 ? make_number (BUF_Z (XBUFFER (buffer))
26273 - XFASTINT (w->window_end_pos))
26274 : Qnil;
26275
26276 if (NILP (overlay))
26277 {
26278 /* Handle the text property case. */
26279 before = Fprevious_single_property_change
26280 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26281 after = Fnext_single_property_change
26282 (make_number (pos), Qmouse_face, buffer, lim2);
26283 before_string = after_string = Qnil;
26284 }
26285 else
26286 {
26287 /* Handle the overlay case. */
26288 before = Foverlay_start (overlay);
26289 after = Foverlay_end (overlay);
26290 before_string = Foverlay_get (overlay, Qbefore_string);
26291 after_string = Foverlay_get (overlay, Qafter_string);
26292
26293 if (!STRINGP (before_string)) before_string = Qnil;
26294 if (!STRINGP (after_string)) after_string = Qnil;
26295 }
26296
26297 mouse_face_from_buffer_pos (window, hlinfo, pos,
26298 XFASTINT (before),
26299 XFASTINT (after),
26300 before_string, after_string,
26301 cover_string);
26302 cursor = No_Cursor;
26303 }
26304 }
26305 }
26306
26307 check_help_echo:
26308
26309 /* Look for a `help-echo' property. */
26310 if (NILP (help_echo_string)) {
26311 Lisp_Object help, overlay;
26312
26313 /* Check overlays first. */
26314 help = overlay = Qnil;
26315 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26316 {
26317 overlay = overlay_vec[i];
26318 help = Foverlay_get (overlay, Qhelp_echo);
26319 }
26320
26321 if (!NILP (help))
26322 {
26323 help_echo_string = help;
26324 help_echo_window = window;
26325 help_echo_object = overlay;
26326 help_echo_pos = pos;
26327 }
26328 else
26329 {
26330 Lisp_Object obj = glyph->object;
26331 EMACS_INT charpos = glyph->charpos;
26332
26333 /* Try text properties. */
26334 if (STRINGP (obj)
26335 && charpos >= 0
26336 && charpos < SCHARS (obj))
26337 {
26338 help = Fget_text_property (make_number (charpos),
26339 Qhelp_echo, obj);
26340 if (NILP (help))
26341 {
26342 /* If the string itself doesn't specify a help-echo,
26343 see if the buffer text ``under'' it does. */
26344 struct glyph_row *r
26345 = MATRIX_ROW (w->current_matrix, vpos);
26346 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26347 EMACS_INT p = string_buffer_position (obj, start);
26348 if (p > 0)
26349 {
26350 help = Fget_char_property (make_number (p),
26351 Qhelp_echo, w->buffer);
26352 if (!NILP (help))
26353 {
26354 charpos = p;
26355 obj = w->buffer;
26356 }
26357 }
26358 }
26359 }
26360 else if (BUFFERP (obj)
26361 && charpos >= BEGV
26362 && charpos < ZV)
26363 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26364 obj);
26365
26366 if (!NILP (help))
26367 {
26368 help_echo_string = help;
26369 help_echo_window = window;
26370 help_echo_object = obj;
26371 help_echo_pos = charpos;
26372 }
26373 }
26374 }
26375
26376 #ifdef HAVE_WINDOW_SYSTEM
26377 /* Look for a `pointer' property. */
26378 if (FRAME_WINDOW_P (f) && NILP (pointer))
26379 {
26380 /* Check overlays first. */
26381 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26382 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26383
26384 if (NILP (pointer))
26385 {
26386 Lisp_Object obj = glyph->object;
26387 EMACS_INT charpos = glyph->charpos;
26388
26389 /* Try text properties. */
26390 if (STRINGP (obj)
26391 && charpos >= 0
26392 && charpos < SCHARS (obj))
26393 {
26394 pointer = Fget_text_property (make_number (charpos),
26395 Qpointer, obj);
26396 if (NILP (pointer))
26397 {
26398 /* If the string itself doesn't specify a pointer,
26399 see if the buffer text ``under'' it does. */
26400 struct glyph_row *r
26401 = MATRIX_ROW (w->current_matrix, vpos);
26402 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26403 EMACS_INT p = string_buffer_position (obj, start);
26404 if (p > 0)
26405 pointer = Fget_char_property (make_number (p),
26406 Qpointer, w->buffer);
26407 }
26408 }
26409 else if (BUFFERP (obj)
26410 && charpos >= BEGV
26411 && charpos < ZV)
26412 pointer = Fget_text_property (make_number (charpos),
26413 Qpointer, obj);
26414 }
26415 }
26416 #endif /* HAVE_WINDOW_SYSTEM */
26417
26418 BEGV = obegv;
26419 ZV = ozv;
26420 current_buffer = obuf;
26421 }
26422
26423 set_cursor:
26424
26425 #ifdef HAVE_WINDOW_SYSTEM
26426 if (FRAME_WINDOW_P (f))
26427 define_frame_cursor1 (f, cursor, pointer);
26428 #else
26429 /* This is here to prevent a compiler error, about "label at end of
26430 compound statement". */
26431 return;
26432 #endif
26433 }
26434
26435
26436 /* EXPORT for RIF:
26437 Clear any mouse-face on window W. This function is part of the
26438 redisplay interface, and is called from try_window_id and similar
26439 functions to ensure the mouse-highlight is off. */
26440
26441 void
26442 x_clear_window_mouse_face (struct window *w)
26443 {
26444 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26445 Lisp_Object window;
26446
26447 BLOCK_INPUT;
26448 XSETWINDOW (window, w);
26449 if (EQ (window, hlinfo->mouse_face_window))
26450 clear_mouse_face (hlinfo);
26451 UNBLOCK_INPUT;
26452 }
26453
26454
26455 /* EXPORT:
26456 Just discard the mouse face information for frame F, if any.
26457 This is used when the size of F is changed. */
26458
26459 void
26460 cancel_mouse_face (struct frame *f)
26461 {
26462 Lisp_Object window;
26463 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26464
26465 window = hlinfo->mouse_face_window;
26466 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26467 {
26468 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26469 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26470 hlinfo->mouse_face_window = Qnil;
26471 }
26472 }
26473
26474
26475 \f
26476 /***********************************************************************
26477 Exposure Events
26478 ***********************************************************************/
26479
26480 #ifdef HAVE_WINDOW_SYSTEM
26481
26482 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26483 which intersects rectangle R. R is in window-relative coordinates. */
26484
26485 static void
26486 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26487 enum glyph_row_area area)
26488 {
26489 struct glyph *first = row->glyphs[area];
26490 struct glyph *end = row->glyphs[area] + row->used[area];
26491 struct glyph *last;
26492 int first_x, start_x, x;
26493
26494 if (area == TEXT_AREA && row->fill_line_p)
26495 /* If row extends face to end of line write the whole line. */
26496 draw_glyphs (w, 0, row, area,
26497 0, row->used[area],
26498 DRAW_NORMAL_TEXT, 0);
26499 else
26500 {
26501 /* Set START_X to the window-relative start position for drawing glyphs of
26502 AREA. The first glyph of the text area can be partially visible.
26503 The first glyphs of other areas cannot. */
26504 start_x = window_box_left_offset (w, area);
26505 x = start_x;
26506 if (area == TEXT_AREA)
26507 x += row->x;
26508
26509 /* Find the first glyph that must be redrawn. */
26510 while (first < end
26511 && x + first->pixel_width < r->x)
26512 {
26513 x += first->pixel_width;
26514 ++first;
26515 }
26516
26517 /* Find the last one. */
26518 last = first;
26519 first_x = x;
26520 while (last < end
26521 && x < r->x + r->width)
26522 {
26523 x += last->pixel_width;
26524 ++last;
26525 }
26526
26527 /* Repaint. */
26528 if (last > first)
26529 draw_glyphs (w, first_x - start_x, row, area,
26530 first - row->glyphs[area], last - row->glyphs[area],
26531 DRAW_NORMAL_TEXT, 0);
26532 }
26533 }
26534
26535
26536 /* Redraw the parts of the glyph row ROW on window W intersecting
26537 rectangle R. R is in window-relative coordinates. Value is
26538 non-zero if mouse-face was overwritten. */
26539
26540 static int
26541 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26542 {
26543 xassert (row->enabled_p);
26544
26545 if (row->mode_line_p || w->pseudo_window_p)
26546 draw_glyphs (w, 0, row, TEXT_AREA,
26547 0, row->used[TEXT_AREA],
26548 DRAW_NORMAL_TEXT, 0);
26549 else
26550 {
26551 if (row->used[LEFT_MARGIN_AREA])
26552 expose_area (w, row, r, LEFT_MARGIN_AREA);
26553 if (row->used[TEXT_AREA])
26554 expose_area (w, row, r, TEXT_AREA);
26555 if (row->used[RIGHT_MARGIN_AREA])
26556 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26557 draw_row_fringe_bitmaps (w, row);
26558 }
26559
26560 return row->mouse_face_p;
26561 }
26562
26563
26564 /* Redraw those parts of glyphs rows during expose event handling that
26565 overlap other rows. Redrawing of an exposed line writes over parts
26566 of lines overlapping that exposed line; this function fixes that.
26567
26568 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26569 row in W's current matrix that is exposed and overlaps other rows.
26570 LAST_OVERLAPPING_ROW is the last such row. */
26571
26572 static void
26573 expose_overlaps (struct window *w,
26574 struct glyph_row *first_overlapping_row,
26575 struct glyph_row *last_overlapping_row,
26576 XRectangle *r)
26577 {
26578 struct glyph_row *row;
26579
26580 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26581 if (row->overlapping_p)
26582 {
26583 xassert (row->enabled_p && !row->mode_line_p);
26584
26585 row->clip = r;
26586 if (row->used[LEFT_MARGIN_AREA])
26587 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26588
26589 if (row->used[TEXT_AREA])
26590 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26591
26592 if (row->used[RIGHT_MARGIN_AREA])
26593 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26594 row->clip = NULL;
26595 }
26596 }
26597
26598
26599 /* Return non-zero if W's cursor intersects rectangle R. */
26600
26601 static int
26602 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26603 {
26604 XRectangle cr, result;
26605 struct glyph *cursor_glyph;
26606 struct glyph_row *row;
26607
26608 if (w->phys_cursor.vpos >= 0
26609 && w->phys_cursor.vpos < w->current_matrix->nrows
26610 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26611 row->enabled_p)
26612 && row->cursor_in_fringe_p)
26613 {
26614 /* Cursor is in the fringe. */
26615 cr.x = window_box_right_offset (w,
26616 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26617 ? RIGHT_MARGIN_AREA
26618 : TEXT_AREA));
26619 cr.y = row->y;
26620 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26621 cr.height = row->height;
26622 return x_intersect_rectangles (&cr, r, &result);
26623 }
26624
26625 cursor_glyph = get_phys_cursor_glyph (w);
26626 if (cursor_glyph)
26627 {
26628 /* r is relative to W's box, but w->phys_cursor.x is relative
26629 to left edge of W's TEXT area. Adjust it. */
26630 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26631 cr.y = w->phys_cursor.y;
26632 cr.width = cursor_glyph->pixel_width;
26633 cr.height = w->phys_cursor_height;
26634 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26635 I assume the effect is the same -- and this is portable. */
26636 return x_intersect_rectangles (&cr, r, &result);
26637 }
26638 /* If we don't understand the format, pretend we're not in the hot-spot. */
26639 return 0;
26640 }
26641
26642
26643 /* EXPORT:
26644 Draw a vertical window border to the right of window W if W doesn't
26645 have vertical scroll bars. */
26646
26647 void
26648 x_draw_vertical_border (struct window *w)
26649 {
26650 struct frame *f = XFRAME (WINDOW_FRAME (w));
26651
26652 /* We could do better, if we knew what type of scroll-bar the adjacent
26653 windows (on either side) have... But we don't :-(
26654 However, I think this works ok. ++KFS 2003-04-25 */
26655
26656 /* Redraw borders between horizontally adjacent windows. Don't
26657 do it for frames with vertical scroll bars because either the
26658 right scroll bar of a window, or the left scroll bar of its
26659 neighbor will suffice as a border. */
26660 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26661 return;
26662
26663 if (!WINDOW_RIGHTMOST_P (w)
26664 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26665 {
26666 int x0, x1, y0, y1;
26667
26668 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26669 y1 -= 1;
26670
26671 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26672 x1 -= 1;
26673
26674 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26675 }
26676 else if (!WINDOW_LEFTMOST_P (w)
26677 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26678 {
26679 int x0, x1, y0, y1;
26680
26681 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26682 y1 -= 1;
26683
26684 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26685 x0 -= 1;
26686
26687 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26688 }
26689 }
26690
26691
26692 /* Redraw the part of window W intersection rectangle FR. Pixel
26693 coordinates in FR are frame-relative. Call this function with
26694 input blocked. Value is non-zero if the exposure overwrites
26695 mouse-face. */
26696
26697 static int
26698 expose_window (struct window *w, XRectangle *fr)
26699 {
26700 struct frame *f = XFRAME (w->frame);
26701 XRectangle wr, r;
26702 int mouse_face_overwritten_p = 0;
26703
26704 /* If window is not yet fully initialized, do nothing. This can
26705 happen when toolkit scroll bars are used and a window is split.
26706 Reconfiguring the scroll bar will generate an expose for a newly
26707 created window. */
26708 if (w->current_matrix == NULL)
26709 return 0;
26710
26711 /* When we're currently updating the window, display and current
26712 matrix usually don't agree. Arrange for a thorough display
26713 later. */
26714 if (w == updated_window)
26715 {
26716 SET_FRAME_GARBAGED (f);
26717 return 0;
26718 }
26719
26720 /* Frame-relative pixel rectangle of W. */
26721 wr.x = WINDOW_LEFT_EDGE_X (w);
26722 wr.y = WINDOW_TOP_EDGE_Y (w);
26723 wr.width = WINDOW_TOTAL_WIDTH (w);
26724 wr.height = WINDOW_TOTAL_HEIGHT (w);
26725
26726 if (x_intersect_rectangles (fr, &wr, &r))
26727 {
26728 int yb = window_text_bottom_y (w);
26729 struct glyph_row *row;
26730 int cursor_cleared_p;
26731 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26732
26733 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26734 r.x, r.y, r.width, r.height));
26735
26736 /* Convert to window coordinates. */
26737 r.x -= WINDOW_LEFT_EDGE_X (w);
26738 r.y -= WINDOW_TOP_EDGE_Y (w);
26739
26740 /* Turn off the cursor. */
26741 if (!w->pseudo_window_p
26742 && phys_cursor_in_rect_p (w, &r))
26743 {
26744 x_clear_cursor (w);
26745 cursor_cleared_p = 1;
26746 }
26747 else
26748 cursor_cleared_p = 0;
26749
26750 /* Update lines intersecting rectangle R. */
26751 first_overlapping_row = last_overlapping_row = NULL;
26752 for (row = w->current_matrix->rows;
26753 row->enabled_p;
26754 ++row)
26755 {
26756 int y0 = row->y;
26757 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26758
26759 if ((y0 >= r.y && y0 < r.y + r.height)
26760 || (y1 > r.y && y1 < r.y + r.height)
26761 || (r.y >= y0 && r.y < y1)
26762 || (r.y + r.height > y0 && r.y + r.height < y1))
26763 {
26764 /* A header line may be overlapping, but there is no need
26765 to fix overlapping areas for them. KFS 2005-02-12 */
26766 if (row->overlapping_p && !row->mode_line_p)
26767 {
26768 if (first_overlapping_row == NULL)
26769 first_overlapping_row = row;
26770 last_overlapping_row = row;
26771 }
26772
26773 row->clip = fr;
26774 if (expose_line (w, row, &r))
26775 mouse_face_overwritten_p = 1;
26776 row->clip = NULL;
26777 }
26778 else if (row->overlapping_p)
26779 {
26780 /* We must redraw a row overlapping the exposed area. */
26781 if (y0 < r.y
26782 ? y0 + row->phys_height > r.y
26783 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26784 {
26785 if (first_overlapping_row == NULL)
26786 first_overlapping_row = row;
26787 last_overlapping_row = row;
26788 }
26789 }
26790
26791 if (y1 >= yb)
26792 break;
26793 }
26794
26795 /* Display the mode line if there is one. */
26796 if (WINDOW_WANTS_MODELINE_P (w)
26797 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26798 row->enabled_p)
26799 && row->y < r.y + r.height)
26800 {
26801 if (expose_line (w, row, &r))
26802 mouse_face_overwritten_p = 1;
26803 }
26804
26805 if (!w->pseudo_window_p)
26806 {
26807 /* Fix the display of overlapping rows. */
26808 if (first_overlapping_row)
26809 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26810 fr);
26811
26812 /* Draw border between windows. */
26813 x_draw_vertical_border (w);
26814
26815 /* Turn the cursor on again. */
26816 if (cursor_cleared_p)
26817 update_window_cursor (w, 1);
26818 }
26819 }
26820
26821 return mouse_face_overwritten_p;
26822 }
26823
26824
26825
26826 /* Redraw (parts) of all windows in the window tree rooted at W that
26827 intersect R. R contains frame pixel coordinates. Value is
26828 non-zero if the exposure overwrites mouse-face. */
26829
26830 static int
26831 expose_window_tree (struct window *w, XRectangle *r)
26832 {
26833 struct frame *f = XFRAME (w->frame);
26834 int mouse_face_overwritten_p = 0;
26835
26836 while (w && !FRAME_GARBAGED_P (f))
26837 {
26838 if (!NILP (w->hchild))
26839 mouse_face_overwritten_p
26840 |= expose_window_tree (XWINDOW (w->hchild), r);
26841 else if (!NILP (w->vchild))
26842 mouse_face_overwritten_p
26843 |= expose_window_tree (XWINDOW (w->vchild), r);
26844 else
26845 mouse_face_overwritten_p |= expose_window (w, r);
26846
26847 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26848 }
26849
26850 return mouse_face_overwritten_p;
26851 }
26852
26853
26854 /* EXPORT:
26855 Redisplay an exposed area of frame F. X and Y are the upper-left
26856 corner of the exposed rectangle. W and H are width and height of
26857 the exposed area. All are pixel values. W or H zero means redraw
26858 the entire frame. */
26859
26860 void
26861 expose_frame (struct frame *f, int x, int y, int w, int h)
26862 {
26863 XRectangle r;
26864 int mouse_face_overwritten_p = 0;
26865
26866 TRACE ((stderr, "expose_frame "));
26867
26868 /* No need to redraw if frame will be redrawn soon. */
26869 if (FRAME_GARBAGED_P (f))
26870 {
26871 TRACE ((stderr, " garbaged\n"));
26872 return;
26873 }
26874
26875 /* If basic faces haven't been realized yet, there is no point in
26876 trying to redraw anything. This can happen when we get an expose
26877 event while Emacs is starting, e.g. by moving another window. */
26878 if (FRAME_FACE_CACHE (f) == NULL
26879 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
26880 {
26881 TRACE ((stderr, " no faces\n"));
26882 return;
26883 }
26884
26885 if (w == 0 || h == 0)
26886 {
26887 r.x = r.y = 0;
26888 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
26889 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
26890 }
26891 else
26892 {
26893 r.x = x;
26894 r.y = y;
26895 r.width = w;
26896 r.height = h;
26897 }
26898
26899 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
26900 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
26901
26902 if (WINDOWP (f->tool_bar_window))
26903 mouse_face_overwritten_p
26904 |= expose_window (XWINDOW (f->tool_bar_window), &r);
26905
26906 #ifdef HAVE_X_WINDOWS
26907 #ifndef MSDOS
26908 #ifndef USE_X_TOOLKIT
26909 if (WINDOWP (f->menu_bar_window))
26910 mouse_face_overwritten_p
26911 |= expose_window (XWINDOW (f->menu_bar_window), &r);
26912 #endif /* not USE_X_TOOLKIT */
26913 #endif
26914 #endif
26915
26916 /* Some window managers support a focus-follows-mouse style with
26917 delayed raising of frames. Imagine a partially obscured frame,
26918 and moving the mouse into partially obscured mouse-face on that
26919 frame. The visible part of the mouse-face will be highlighted,
26920 then the WM raises the obscured frame. With at least one WM, KDE
26921 2.1, Emacs is not getting any event for the raising of the frame
26922 (even tried with SubstructureRedirectMask), only Expose events.
26923 These expose events will draw text normally, i.e. not
26924 highlighted. Which means we must redo the highlight here.
26925 Subsume it under ``we love X''. --gerd 2001-08-15 */
26926 /* Included in Windows version because Windows most likely does not
26927 do the right thing if any third party tool offers
26928 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
26929 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
26930 {
26931 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26932 if (f == hlinfo->mouse_face_mouse_frame)
26933 {
26934 int mouse_x = hlinfo->mouse_face_mouse_x;
26935 int mouse_y = hlinfo->mouse_face_mouse_y;
26936 clear_mouse_face (hlinfo);
26937 note_mouse_highlight (f, mouse_x, mouse_y);
26938 }
26939 }
26940 }
26941
26942
26943 /* EXPORT:
26944 Determine the intersection of two rectangles R1 and R2. Return
26945 the intersection in *RESULT. Value is non-zero if RESULT is not
26946 empty. */
26947
26948 int
26949 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
26950 {
26951 XRectangle *left, *right;
26952 XRectangle *upper, *lower;
26953 int intersection_p = 0;
26954
26955 /* Rearrange so that R1 is the left-most rectangle. */
26956 if (r1->x < r2->x)
26957 left = r1, right = r2;
26958 else
26959 left = r2, right = r1;
26960
26961 /* X0 of the intersection is right.x0, if this is inside R1,
26962 otherwise there is no intersection. */
26963 if (right->x <= left->x + left->width)
26964 {
26965 result->x = right->x;
26966
26967 /* The right end of the intersection is the minimum of the
26968 the right ends of left and right. */
26969 result->width = (min (left->x + left->width, right->x + right->width)
26970 - result->x);
26971
26972 /* Same game for Y. */
26973 if (r1->y < r2->y)
26974 upper = r1, lower = r2;
26975 else
26976 upper = r2, lower = r1;
26977
26978 /* The upper end of the intersection is lower.y0, if this is inside
26979 of upper. Otherwise, there is no intersection. */
26980 if (lower->y <= upper->y + upper->height)
26981 {
26982 result->y = lower->y;
26983
26984 /* The lower end of the intersection is the minimum of the lower
26985 ends of upper and lower. */
26986 result->height = (min (lower->y + lower->height,
26987 upper->y + upper->height)
26988 - result->y);
26989 intersection_p = 1;
26990 }
26991 }
26992
26993 return intersection_p;
26994 }
26995
26996 #endif /* HAVE_WINDOW_SYSTEM */
26997
26998 \f
26999 /***********************************************************************
27000 Initialization
27001 ***********************************************************************/
27002
27003 void
27004 syms_of_xdisp (void)
27005 {
27006 Vwith_echo_area_save_vector = Qnil;
27007 staticpro (&Vwith_echo_area_save_vector);
27008
27009 Vmessage_stack = Qnil;
27010 staticpro (&Vmessage_stack);
27011
27012 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
27013 staticpro (&Qinhibit_redisplay);
27014
27015 message_dolog_marker1 = Fmake_marker ();
27016 staticpro (&message_dolog_marker1);
27017 message_dolog_marker2 = Fmake_marker ();
27018 staticpro (&message_dolog_marker2);
27019 message_dolog_marker3 = Fmake_marker ();
27020 staticpro (&message_dolog_marker3);
27021
27022 #if GLYPH_DEBUG
27023 defsubr (&Sdump_frame_glyph_matrix);
27024 defsubr (&Sdump_glyph_matrix);
27025 defsubr (&Sdump_glyph_row);
27026 defsubr (&Sdump_tool_bar_row);
27027 defsubr (&Strace_redisplay);
27028 defsubr (&Strace_to_stderr);
27029 #endif
27030 #ifdef HAVE_WINDOW_SYSTEM
27031 defsubr (&Stool_bar_lines_needed);
27032 defsubr (&Slookup_image_map);
27033 #endif
27034 defsubr (&Sformat_mode_line);
27035 defsubr (&Sinvisible_p);
27036 defsubr (&Scurrent_bidi_paragraph_direction);
27037
27038 staticpro (&Qmenu_bar_update_hook);
27039 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
27040
27041 staticpro (&Qoverriding_terminal_local_map);
27042 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
27043
27044 staticpro (&Qoverriding_local_map);
27045 Qoverriding_local_map = intern_c_string ("overriding-local-map");
27046
27047 staticpro (&Qwindow_scroll_functions);
27048 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
27049
27050 staticpro (&Qwindow_text_change_functions);
27051 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
27052
27053 staticpro (&Qredisplay_end_trigger_functions);
27054 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
27055
27056 staticpro (&Qinhibit_point_motion_hooks);
27057 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
27058
27059 Qeval = intern_c_string ("eval");
27060 staticpro (&Qeval);
27061
27062 QCdata = intern_c_string (":data");
27063 staticpro (&QCdata);
27064 Qdisplay = intern_c_string ("display");
27065 staticpro (&Qdisplay);
27066 Qspace_width = intern_c_string ("space-width");
27067 staticpro (&Qspace_width);
27068 Qraise = intern_c_string ("raise");
27069 staticpro (&Qraise);
27070 Qslice = intern_c_string ("slice");
27071 staticpro (&Qslice);
27072 Qspace = intern_c_string ("space");
27073 staticpro (&Qspace);
27074 Qmargin = intern_c_string ("margin");
27075 staticpro (&Qmargin);
27076 Qpointer = intern_c_string ("pointer");
27077 staticpro (&Qpointer);
27078 Qleft_margin = intern_c_string ("left-margin");
27079 staticpro (&Qleft_margin);
27080 Qright_margin = intern_c_string ("right-margin");
27081 staticpro (&Qright_margin);
27082 Qcenter = intern_c_string ("center");
27083 staticpro (&Qcenter);
27084 Qline_height = intern_c_string ("line-height");
27085 staticpro (&Qline_height);
27086 QCalign_to = intern_c_string (":align-to");
27087 staticpro (&QCalign_to);
27088 QCrelative_width = intern_c_string (":relative-width");
27089 staticpro (&QCrelative_width);
27090 QCrelative_height = intern_c_string (":relative-height");
27091 staticpro (&QCrelative_height);
27092 QCeval = intern_c_string (":eval");
27093 staticpro (&QCeval);
27094 QCpropertize = intern_c_string (":propertize");
27095 staticpro (&QCpropertize);
27096 QCfile = intern_c_string (":file");
27097 staticpro (&QCfile);
27098 Qfontified = intern_c_string ("fontified");
27099 staticpro (&Qfontified);
27100 Qfontification_functions = intern_c_string ("fontification-functions");
27101 staticpro (&Qfontification_functions);
27102 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
27103 staticpro (&Qtrailing_whitespace);
27104 Qescape_glyph = intern_c_string ("escape-glyph");
27105 staticpro (&Qescape_glyph);
27106 Qnobreak_space = intern_c_string ("nobreak-space");
27107 staticpro (&Qnobreak_space);
27108 Qimage = intern_c_string ("image");
27109 staticpro (&Qimage);
27110 Qtext = intern_c_string ("text");
27111 staticpro (&Qtext);
27112 Qboth = intern_c_string ("both");
27113 staticpro (&Qboth);
27114 Qboth_horiz = intern_c_string ("both-horiz");
27115 staticpro (&Qboth_horiz);
27116 Qtext_image_horiz = intern_c_string ("text-image-horiz");
27117 staticpro (&Qtext_image_horiz);
27118 QCmap = intern_c_string (":map");
27119 staticpro (&QCmap);
27120 QCpointer = intern_c_string (":pointer");
27121 staticpro (&QCpointer);
27122 Qrect = intern_c_string ("rect");
27123 staticpro (&Qrect);
27124 Qcircle = intern_c_string ("circle");
27125 staticpro (&Qcircle);
27126 Qpoly = intern_c_string ("poly");
27127 staticpro (&Qpoly);
27128 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
27129 staticpro (&Qmessage_truncate_lines);
27130 Qgrow_only = intern_c_string ("grow-only");
27131 staticpro (&Qgrow_only);
27132 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
27133 staticpro (&Qinhibit_menubar_update);
27134 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
27135 staticpro (&Qinhibit_eval_during_redisplay);
27136 Qposition = intern_c_string ("position");
27137 staticpro (&Qposition);
27138 Qbuffer_position = intern_c_string ("buffer-position");
27139 staticpro (&Qbuffer_position);
27140 Qobject = intern_c_string ("object");
27141 staticpro (&Qobject);
27142 Qbar = intern_c_string ("bar");
27143 staticpro (&Qbar);
27144 Qhbar = intern_c_string ("hbar");
27145 staticpro (&Qhbar);
27146 Qbox = intern_c_string ("box");
27147 staticpro (&Qbox);
27148 Qhollow = intern_c_string ("hollow");
27149 staticpro (&Qhollow);
27150 Qhand = intern_c_string ("hand");
27151 staticpro (&Qhand);
27152 Qarrow = intern_c_string ("arrow");
27153 staticpro (&Qarrow);
27154 Qtext = intern_c_string ("text");
27155 staticpro (&Qtext);
27156 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
27157 staticpro (&Qinhibit_free_realized_faces);
27158
27159 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27160 Fcons (intern_c_string ("void-variable"), Qnil)),
27161 Qnil);
27162 staticpro (&list_of_error);
27163
27164 Qlast_arrow_position = intern_c_string ("last-arrow-position");
27165 staticpro (&Qlast_arrow_position);
27166 Qlast_arrow_string = intern_c_string ("last-arrow-string");
27167 staticpro (&Qlast_arrow_string);
27168
27169 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
27170 staticpro (&Qoverlay_arrow_string);
27171 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
27172 staticpro (&Qoverlay_arrow_bitmap);
27173
27174 echo_buffer[0] = echo_buffer[1] = Qnil;
27175 staticpro (&echo_buffer[0]);
27176 staticpro (&echo_buffer[1]);
27177
27178 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27179 staticpro (&echo_area_buffer[0]);
27180 staticpro (&echo_area_buffer[1]);
27181
27182 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27183 staticpro (&Vmessages_buffer_name);
27184
27185 mode_line_proptrans_alist = Qnil;
27186 staticpro (&mode_line_proptrans_alist);
27187 mode_line_string_list = Qnil;
27188 staticpro (&mode_line_string_list);
27189 mode_line_string_face = Qnil;
27190 staticpro (&mode_line_string_face);
27191 mode_line_string_face_prop = Qnil;
27192 staticpro (&mode_line_string_face_prop);
27193 Vmode_line_unwind_vector = Qnil;
27194 staticpro (&Vmode_line_unwind_vector);
27195
27196 help_echo_string = Qnil;
27197 staticpro (&help_echo_string);
27198 help_echo_object = Qnil;
27199 staticpro (&help_echo_object);
27200 help_echo_window = Qnil;
27201 staticpro (&help_echo_window);
27202 previous_help_echo_string = Qnil;
27203 staticpro (&previous_help_echo_string);
27204 help_echo_pos = -1;
27205
27206 Qright_to_left = intern_c_string ("right-to-left");
27207 staticpro (&Qright_to_left);
27208 Qleft_to_right = intern_c_string ("left-to-right");
27209 staticpro (&Qleft_to_right);
27210
27211 #ifdef HAVE_WINDOW_SYSTEM
27212 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27213 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27214 For example, if a block cursor is over a tab, it will be drawn as
27215 wide as that tab on the display. */);
27216 x_stretch_cursor_p = 0;
27217 #endif
27218
27219 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27220 doc: /* *Non-nil means highlight trailing whitespace.
27221 The face used for trailing whitespace is `trailing-whitespace'. */);
27222 Vshow_trailing_whitespace = Qnil;
27223
27224 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27225 doc: /* *Control highlighting of nobreak space and soft hyphen.
27226 A value of t means highlight the character itself (for nobreak space,
27227 use face `nobreak-space').
27228 A value of nil means no highlighting.
27229 Other values mean display the escape glyph followed by an ordinary
27230 space or ordinary hyphen. */);
27231 Vnobreak_char_display = Qt;
27232
27233 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27234 doc: /* *The pointer shape to show in void text areas.
27235 A value of nil means to show the text pointer. Other options are `arrow',
27236 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27237 Vvoid_text_area_pointer = Qarrow;
27238
27239 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27240 doc: /* Non-nil means don't actually do any redisplay.
27241 This is used for internal purposes. */);
27242 Vinhibit_redisplay = Qnil;
27243
27244 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27245 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27246 Vglobal_mode_string = Qnil;
27247
27248 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27249 doc: /* Marker for where to display an arrow on top of the buffer text.
27250 This must be the beginning of a line in order to work.
27251 See also `overlay-arrow-string'. */);
27252 Voverlay_arrow_position = Qnil;
27253
27254 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27255 doc: /* String to display as an arrow in non-window frames.
27256 See also `overlay-arrow-position'. */);
27257 Voverlay_arrow_string = make_pure_c_string ("=>");
27258
27259 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27260 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27261 The symbols on this list are examined during redisplay to determine
27262 where to display overlay arrows. */);
27263 Voverlay_arrow_variable_list
27264 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27265
27266 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27267 doc: /* *The number of lines to try scrolling a window by when point moves out.
27268 If that fails to bring point back on frame, point is centered instead.
27269 If this is zero, point is always centered after it moves off frame.
27270 If you want scrolling to always be a line at a time, you should set
27271 `scroll-conservatively' to a large value rather than set this to 1. */);
27272
27273 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27274 doc: /* *Scroll up to this many lines, to bring point back on screen.
27275 If point moves off-screen, redisplay will scroll by up to
27276 `scroll-conservatively' lines in order to bring point just barely
27277 onto the screen again. If that cannot be done, then redisplay
27278 recenters point as usual.
27279
27280 If the value is greater than 100, redisplay will never recenter point,
27281 but will always scroll just enough text to bring point into view, even
27282 if you move far away.
27283
27284 A value of zero means always recenter point if it moves off screen. */);
27285 scroll_conservatively = 0;
27286
27287 DEFVAR_INT ("scroll-margin", scroll_margin,
27288 doc: /* *Number of lines of margin at the top and bottom of a window.
27289 Recenter the window whenever point gets within this many lines
27290 of the top or bottom of the window. */);
27291 scroll_margin = 0;
27292
27293 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27294 doc: /* Pixels per inch value for non-window system displays.
27295 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27296 Vdisplay_pixels_per_inch = make_float (72.0);
27297
27298 #if GLYPH_DEBUG
27299 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27300 #endif
27301
27302 DEFVAR_LISP ("truncate-partial-width-windows",
27303 Vtruncate_partial_width_windows,
27304 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27305 For an integer value, truncate lines in each window narrower than the
27306 full frame width, provided the window width is less than that integer;
27307 otherwise, respect the value of `truncate-lines'.
27308
27309 For any other non-nil value, truncate lines in all windows that do
27310 not span the full frame width.
27311
27312 A value of nil means to respect the value of `truncate-lines'.
27313
27314 If `word-wrap' is enabled, you might want to reduce this. */);
27315 Vtruncate_partial_width_windows = make_number (50);
27316
27317 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27318 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27319 Any other value means to use the appropriate face, `mode-line',
27320 `header-line', or `menu' respectively. */);
27321 mode_line_inverse_video = 1;
27322
27323 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27324 doc: /* *Maximum buffer size for which line number should be displayed.
27325 If the buffer is bigger than this, the line number does not appear
27326 in the mode line. A value of nil means no limit. */);
27327 Vline_number_display_limit = Qnil;
27328
27329 DEFVAR_INT ("line-number-display-limit-width",
27330 line_number_display_limit_width,
27331 doc: /* *Maximum line width (in characters) for line number display.
27332 If the average length of the lines near point is bigger than this, then the
27333 line number may be omitted from the mode line. */);
27334 line_number_display_limit_width = 200;
27335
27336 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27337 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27338 highlight_nonselected_windows = 0;
27339
27340 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27341 doc: /* Non-nil if more than one frame is visible on this display.
27342 Minibuffer-only frames don't count, but iconified frames do.
27343 This variable is not guaranteed to be accurate except while processing
27344 `frame-title-format' and `icon-title-format'. */);
27345
27346 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27347 doc: /* Template for displaying the title bar of visible frames.
27348 \(Assuming the window manager supports this feature.)
27349
27350 This variable has the same structure as `mode-line-format', except that
27351 the %c and %l constructs are ignored. It is used only on frames for
27352 which no explicit name has been set \(see `modify-frame-parameters'). */);
27353
27354 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27355 doc: /* Template for displaying the title bar of an iconified frame.
27356 \(Assuming the window manager supports this feature.)
27357 This variable has the same structure as `mode-line-format' (which see),
27358 and is used only on frames for which no explicit name has been set
27359 \(see `modify-frame-parameters'). */);
27360 Vicon_title_format
27361 = Vframe_title_format
27362 = pure_cons (intern_c_string ("multiple-frames"),
27363 pure_cons (make_pure_c_string ("%b"),
27364 pure_cons (pure_cons (empty_unibyte_string,
27365 pure_cons (intern_c_string ("invocation-name"),
27366 pure_cons (make_pure_c_string ("@"),
27367 pure_cons (intern_c_string ("system-name"),
27368 Qnil)))),
27369 Qnil)));
27370
27371 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27372 doc: /* Maximum number of lines to keep in the message log buffer.
27373 If nil, disable message logging. If t, log messages but don't truncate
27374 the buffer when it becomes large. */);
27375 Vmessage_log_max = make_number (100);
27376
27377 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27378 doc: /* Functions called before redisplay, if window sizes have changed.
27379 The value should be a list of functions that take one argument.
27380 Just before redisplay, for each frame, if any of its windows have changed
27381 size since the last redisplay, or have been split or deleted,
27382 all the functions in the list are called, with the frame as argument. */);
27383 Vwindow_size_change_functions = Qnil;
27384
27385 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27386 doc: /* List of functions to call before redisplaying a window with scrolling.
27387 Each function is called with two arguments, the window and its new
27388 display-start position. Note that these functions are also called by
27389 `set-window-buffer'. Also note that the value of `window-end' is not
27390 valid when these functions are called. */);
27391 Vwindow_scroll_functions = Qnil;
27392
27393 DEFVAR_LISP ("window-text-change-functions",
27394 Vwindow_text_change_functions,
27395 doc: /* Functions to call in redisplay when text in the window might change. */);
27396 Vwindow_text_change_functions = Qnil;
27397
27398 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27399 doc: /* Functions called when redisplay of a window reaches the end trigger.
27400 Each function is called with two arguments, the window and the end trigger value.
27401 See `set-window-redisplay-end-trigger'. */);
27402 Vredisplay_end_trigger_functions = Qnil;
27403
27404 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27405 doc: /* *Non-nil means autoselect window with mouse pointer.
27406 If nil, do not autoselect windows.
27407 A positive number means delay autoselection by that many seconds: a
27408 window is autoselected only after the mouse has remained in that
27409 window for the duration of the delay.
27410 A negative number has a similar effect, but causes windows to be
27411 autoselected only after the mouse has stopped moving. \(Because of
27412 the way Emacs compares mouse events, you will occasionally wait twice
27413 that time before the window gets selected.\)
27414 Any other value means to autoselect window instantaneously when the
27415 mouse pointer enters it.
27416
27417 Autoselection selects the minibuffer only if it is active, and never
27418 unselects the minibuffer if it is active.
27419
27420 When customizing this variable make sure that the actual value of
27421 `focus-follows-mouse' matches the behavior of your window manager. */);
27422 Vmouse_autoselect_window = Qnil;
27423
27424 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27425 doc: /* *Non-nil means automatically resize tool-bars.
27426 This dynamically changes the tool-bar's height to the minimum height
27427 that is needed to make all tool-bar items visible.
27428 If value is `grow-only', the tool-bar's height is only increased
27429 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27430 Vauto_resize_tool_bars = Qt;
27431
27432 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27433 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27434 auto_raise_tool_bar_buttons_p = 1;
27435
27436 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27437 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27438 make_cursor_line_fully_visible_p = 1;
27439
27440 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27441 doc: /* *Border below tool-bar in pixels.
27442 If an integer, use it as the height of the border.
27443 If it is one of `internal-border-width' or `border-width', use the
27444 value of the corresponding frame parameter.
27445 Otherwise, no border is added below the tool-bar. */);
27446 Vtool_bar_border = Qinternal_border_width;
27447
27448 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27449 doc: /* *Margin around tool-bar buttons in pixels.
27450 If an integer, use that for both horizontal and vertical margins.
27451 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27452 HORZ specifying the horizontal margin, and VERT specifying the
27453 vertical margin. */);
27454 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27455
27456 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27457 doc: /* *Relief thickness of tool-bar buttons. */);
27458 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27459
27460 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27461 doc: /* Tool bar style to use.
27462 It can be one of
27463 image - show images only
27464 text - show text only
27465 both - show both, text below image
27466 both-horiz - show text to the right of the image
27467 text-image-horiz - show text to the left of the image
27468 any other - use system default or image if no system default. */);
27469 Vtool_bar_style = Qnil;
27470
27471 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27472 doc: /* *Maximum number of characters a label can have to be shown.
27473 The tool bar style must also show labels for this to have any effect, see
27474 `tool-bar-style'. */);
27475 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27476
27477 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27478 doc: /* List of functions to call to fontify regions of text.
27479 Each function is called with one argument POS. Functions must
27480 fontify a region starting at POS in the current buffer, and give
27481 fontified regions the property `fontified'. */);
27482 Vfontification_functions = Qnil;
27483 Fmake_variable_buffer_local (Qfontification_functions);
27484
27485 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27486 unibyte_display_via_language_environment,
27487 doc: /* *Non-nil means display unibyte text according to language environment.
27488 Specifically, this means that raw bytes in the range 160-255 decimal
27489 are displayed by converting them to the equivalent multibyte characters
27490 according to the current language environment. As a result, they are
27491 displayed according to the current fontset.
27492
27493 Note that this variable affects only how these bytes are displayed,
27494 but does not change the fact they are interpreted as raw bytes. */);
27495 unibyte_display_via_language_environment = 0;
27496
27497 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27498 doc: /* *Maximum height for resizing mini-windows.
27499 If a float, it specifies a fraction of the mini-window frame's height.
27500 If an integer, it specifies a number of lines. */);
27501 Vmax_mini_window_height = make_float (0.25);
27502
27503 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27504 doc: /* *How to resize mini-windows.
27505 A value of nil means don't automatically resize mini-windows.
27506 A value of t means resize them to fit the text displayed in them.
27507 A value of `grow-only', the default, means let mini-windows grow
27508 only, until their display becomes empty, at which point the windows
27509 go back to their normal size. */);
27510 Vresize_mini_windows = Qgrow_only;
27511
27512 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27513 doc: /* Alist specifying how to blink the cursor off.
27514 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27515 `cursor-type' frame-parameter or variable equals ON-STATE,
27516 comparing using `equal', Emacs uses OFF-STATE to specify
27517 how to blink it off. ON-STATE and OFF-STATE are values for
27518 the `cursor-type' frame parameter.
27519
27520 If a frame's ON-STATE has no entry in this list,
27521 the frame's other specifications determine how to blink the cursor off. */);
27522 Vblink_cursor_alist = Qnil;
27523
27524 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27525 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27526 If non-nil, windows are automatically scrolled horizontally to make
27527 point visible. */);
27528 automatic_hscrolling_p = 1;
27529 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
27530 staticpro (&Qauto_hscroll_mode);
27531
27532 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27533 doc: /* *How many columns away from the window edge point is allowed to get
27534 before automatic hscrolling will horizontally scroll the window. */);
27535 hscroll_margin = 5;
27536
27537 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27538 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27539 When point is less than `hscroll-margin' columns from the window
27540 edge, automatic hscrolling will scroll the window by the amount of columns
27541 determined by this variable. If its value is a positive integer, scroll that
27542 many columns. If it's a positive floating-point number, it specifies the
27543 fraction of the window's width to scroll. If it's nil or zero, point will be
27544 centered horizontally after the scroll. Any other value, including negative
27545 numbers, are treated as if the value were zero.
27546
27547 Automatic hscrolling always moves point outside the scroll margin, so if
27548 point was more than scroll step columns inside the margin, the window will
27549 scroll more than the value given by the scroll step.
27550
27551 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27552 and `scroll-right' overrides this variable's effect. */);
27553 Vhscroll_step = make_number (0);
27554
27555 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27556 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27557 Bind this around calls to `message' to let it take effect. */);
27558 message_truncate_lines = 0;
27559
27560 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27561 doc: /* Normal hook run to update the menu bar definitions.
27562 Redisplay runs this hook before it redisplays the menu bar.
27563 This is used to update submenus such as Buffers,
27564 whose contents depend on various data. */);
27565 Vmenu_bar_update_hook = Qnil;
27566
27567 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27568 doc: /* Frame for which we are updating a menu.
27569 The enable predicate for a menu binding should check this variable. */);
27570 Vmenu_updating_frame = Qnil;
27571
27572 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27573 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27574 inhibit_menubar_update = 0;
27575
27576 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27577 doc: /* Prefix prepended to all continuation lines at display time.
27578 The value may be a string, an image, or a stretch-glyph; it is
27579 interpreted in the same way as the value of a `display' text property.
27580
27581 This variable is overridden by any `wrap-prefix' text or overlay
27582 property.
27583
27584 To add a prefix to non-continuation lines, use `line-prefix'. */);
27585 Vwrap_prefix = Qnil;
27586 staticpro (&Qwrap_prefix);
27587 Qwrap_prefix = intern_c_string ("wrap-prefix");
27588 Fmake_variable_buffer_local (Qwrap_prefix);
27589
27590 DEFVAR_LISP ("line-prefix", Vline_prefix,
27591 doc: /* Prefix prepended to all non-continuation lines at display time.
27592 The value may be a string, an image, or a stretch-glyph; it is
27593 interpreted in the same way as the value of a `display' text property.
27594
27595 This variable is overridden by any `line-prefix' text or overlay
27596 property.
27597
27598 To add a prefix to continuation lines, use `wrap-prefix'. */);
27599 Vline_prefix = Qnil;
27600 staticpro (&Qline_prefix);
27601 Qline_prefix = intern_c_string ("line-prefix");
27602 Fmake_variable_buffer_local (Qline_prefix);
27603
27604 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27605 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27606 inhibit_eval_during_redisplay = 0;
27607
27608 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27609 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27610 inhibit_free_realized_faces = 0;
27611
27612 #if GLYPH_DEBUG
27613 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27614 doc: /* Inhibit try_window_id display optimization. */);
27615 inhibit_try_window_id = 0;
27616
27617 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27618 doc: /* Inhibit try_window_reusing display optimization. */);
27619 inhibit_try_window_reusing = 0;
27620
27621 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27622 doc: /* Inhibit try_cursor_movement display optimization. */);
27623 inhibit_try_cursor_movement = 0;
27624 #endif /* GLYPH_DEBUG */
27625
27626 DEFVAR_INT ("overline-margin", overline_margin,
27627 doc: /* *Space between overline and text, in pixels.
27628 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27629 margin to the caracter height. */);
27630 overline_margin = 2;
27631
27632 DEFVAR_INT ("underline-minimum-offset",
27633 underline_minimum_offset,
27634 doc: /* Minimum distance between baseline and underline.
27635 This can improve legibility of underlined text at small font sizes,
27636 particularly when using variable `x-use-underline-position-properties'
27637 with fonts that specify an UNDERLINE_POSITION relatively close to the
27638 baseline. The default value is 1. */);
27639 underline_minimum_offset = 1;
27640
27641 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27642 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27643 This feature only works when on a window system that can change
27644 cursor shapes. */);
27645 display_hourglass_p = 1;
27646
27647 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27648 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27649 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27650
27651 hourglass_atimer = NULL;
27652 hourglass_shown_p = 0;
27653
27654 DEFSYM (Qglyphless_char, "glyphless-char");
27655 DEFSYM (Qhex_code, "hex-code");
27656 DEFSYM (Qempty_box, "empty-box");
27657 DEFSYM (Qthin_space, "thin-space");
27658 DEFSYM (Qzero_width, "zero-width");
27659
27660 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27661 /* Intern this now in case it isn't already done.
27662 Setting this variable twice is harmless.
27663 But don't staticpro it here--that is done in alloc.c. */
27664 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27665 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27666
27667 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27668 doc: /* Char-table defining glyphless characters.
27669 Each element, if non-nil, should be one of the following:
27670 an ASCII acronym string: display this string in a box
27671 `hex-code': display the hexadecimal code of a character in a box
27672 `empty-box': display as an empty box
27673 `thin-space': display as 1-pixel width space
27674 `zero-width': don't display
27675 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27676 display method for graphical terminals and text terminals respectively.
27677 GRAPHICAL and TEXT should each have one of the values listed above.
27678
27679 The char-table has one extra slot to control the display of a character for
27680 which no font is found. This slot only takes effect on graphical terminals.
27681 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27682 `thin-space'. The default is `empty-box'. */);
27683 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27684 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27685 Qempty_box);
27686 }
27687
27688
27689 /* Initialize this module when Emacs starts. */
27690
27691 void
27692 init_xdisp (void)
27693 {
27694 Lisp_Object root_window;
27695 struct window *mini_w;
27696
27697 current_header_line_height = current_mode_line_height = -1;
27698
27699 CHARPOS (this_line_start_pos) = 0;
27700
27701 mini_w = XWINDOW (minibuf_window);
27702 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27703 echo_area_window = minibuf_window;
27704
27705 if (!noninteractive)
27706 {
27707 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27708 int i;
27709
27710 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27711 set_window_height (root_window,
27712 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27713 0);
27714 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27715 set_window_height (minibuf_window, 1, 0);
27716
27717 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27718 mini_w->total_cols = make_number (FRAME_COLS (f));
27719
27720 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27721 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27722 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27723
27724 /* The default ellipsis glyphs `...'. */
27725 for (i = 0; i < 3; ++i)
27726 default_invis_vector[i] = make_number ('.');
27727 }
27728
27729 {
27730 /* Allocate the buffer for frame titles.
27731 Also used for `format-mode-line'. */
27732 int size = 100;
27733 mode_line_noprop_buf = (char *) xmalloc (size);
27734 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27735 mode_line_noprop_ptr = mode_line_noprop_buf;
27736 mode_line_target = MODE_LINE_DISPLAY;
27737 }
27738
27739 help_echo_showing_p = 0;
27740 }
27741
27742 /* Since w32 does not support atimers, it defines its own implementation of
27743 the following three functions in w32fns.c. */
27744 #ifndef WINDOWSNT
27745
27746 /* Platform-independent portion of hourglass implementation. */
27747
27748 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27749 int
27750 hourglass_started (void)
27751 {
27752 return hourglass_shown_p || hourglass_atimer != NULL;
27753 }
27754
27755 /* Cancel a currently active hourglass timer, and start a new one. */
27756 void
27757 start_hourglass (void)
27758 {
27759 #if defined (HAVE_WINDOW_SYSTEM)
27760 EMACS_TIME delay;
27761 int secs, usecs = 0;
27762
27763 cancel_hourglass ();
27764
27765 if (INTEGERP (Vhourglass_delay)
27766 && XINT (Vhourglass_delay) > 0)
27767 secs = XFASTINT (Vhourglass_delay);
27768 else if (FLOATP (Vhourglass_delay)
27769 && XFLOAT_DATA (Vhourglass_delay) > 0)
27770 {
27771 Lisp_Object tem;
27772 tem = Ftruncate (Vhourglass_delay, Qnil);
27773 secs = XFASTINT (tem);
27774 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27775 }
27776 else
27777 secs = DEFAULT_HOURGLASS_DELAY;
27778
27779 EMACS_SET_SECS_USECS (delay, secs, usecs);
27780 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27781 show_hourglass, NULL);
27782 #endif
27783 }
27784
27785
27786 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27787 shown. */
27788 void
27789 cancel_hourglass (void)
27790 {
27791 #if defined (HAVE_WINDOW_SYSTEM)
27792 if (hourglass_atimer)
27793 {
27794 cancel_atimer (hourglass_atimer);
27795 hourglass_atimer = NULL;
27796 }
27797
27798 if (hourglass_shown_p)
27799 hide_hourglass ();
27800 #endif
27801 }
27802 #endif /* ! WINDOWSNT */